org.el 649 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416
  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.32
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum)
  71. (require 'calendar))
  72. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  73. ;; the file noutline.el being loaded.
  74. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  75. ;; We require noutline, which might be provided in outline.el
  76. (require 'outline) (require 'noutline)
  77. ;; Other stuff we need.
  78. (require 'time-date)
  79. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  80. (require 'easymenu)
  81. (require 'org-macs)
  82. (require 'org-compat)
  83. (require 'org-faces)
  84. (require 'org-list)
  85. (require 'org-src)
  86. (require 'org-footnote)
  87. ;;;; Customization variables
  88. ;;; Version
  89. (defconst org-version "6.32"
  90. "The version number of the file org.el.")
  91. (defun org-version (&optional here)
  92. "Show the org-mode version in the echo area.
  93. With prefix arg HERE, insert it at point."
  94. (interactive "P")
  95. (let* ((version org-version)
  96. (git-version)
  97. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  98. (if (and (file-exists-p (expand-file-name ".git" dir))
  99. (executable-find "git"))
  100. (let ((pwd (substring (pwd) 10)))
  101. (cd dir)
  102. (if (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  103. (save-excursion
  104. (set-buffer "*Shell Command Output*")
  105. (goto-char (point-min))
  106. (re-search-forward "[^\n]+")
  107. (setq git-version (match-string 0))
  108. (subst-char-in-string ?- ?. git-version t)
  109. (shell-command "git diff-index --name-only HEAD --")
  110. (unless (eql 1 (point-max))
  111. (setq git-version (concat git-version ".dirty")))
  112. (setq version (concat version " (" git-version ")")))
  113. (cd pwd))))
  114. (setq version (format "Org-mode version %s" version))
  115. (if here (insert version))
  116. (message version)
  117. version))
  118. ;;; Compatibility constants
  119. ;;; The custom variables
  120. (defgroup org nil
  121. "Outline-based notes management and organizer."
  122. :tag "Org"
  123. :group 'outlines
  124. :group 'hypermedia
  125. :group 'calendar)
  126. (defcustom org-mode-hook nil
  127. "Mode hook for Org-mode, run after the mode was turned on."
  128. :group 'org
  129. :type 'hook)
  130. (defcustom org-load-hook nil
  131. "Hook that is run after org.el has been loaded."
  132. :group 'org
  133. :type 'hook)
  134. (defvar org-modules) ; defined below
  135. (defvar org-modules-loaded nil
  136. "Have the modules been loaded already?")
  137. (defun org-load-modules-maybe (&optional force)
  138. "Load all extensions listed in `org-modules'."
  139. (when (or force (not org-modules-loaded))
  140. (mapc (lambda (ext)
  141. (condition-case nil (require ext)
  142. (error (message "Problems while trying to load feature `%s'" ext))))
  143. org-modules)
  144. (setq org-modules-loaded t)))
  145. (defun org-set-modules (var value)
  146. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  147. (set var value)
  148. (when (featurep 'org)
  149. (org-load-modules-maybe 'force)))
  150. (when (org-bound-and-true-p org-modules)
  151. (let ((a (member 'org-infojs org-modules)))
  152. (and a (setcar a 'org-jsinfo))))
  153. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  154. "Modules that should always be loaded together with org.el.
  155. If a description starts with <C>, the file is not part of Emacs
  156. and loading it will require that you have downloaded and properly installed
  157. the org-mode distribution.
  158. You can also use this system to load external packages (i.e. neither Org
  159. core modules, nor modules from the CONTRIB directory). Just add symbols
  160. to the end of the list. If the package is called org-xyz.el, then you need
  161. to add the symbol `xyz', and the package must have a call to
  162. (provide 'org-xyz)"
  163. :group 'org
  164. :set 'org-set-modules
  165. :type
  166. '(set :greedy t
  167. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  168. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  169. (const :tag " crypt: Encryption of subtrees" org-crypt)
  170. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  171. (const :tag " id: Global IDs for identifying entries" org-id)
  172. (const :tag " info: Links to Info nodes" org-info)
  173. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  174. (const :tag " habit: Track your consistency with habits" org-habit)
  175. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  176. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  177. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  178. (const :tag " mew Links to Mew folders/messages" org-mew)
  179. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  180. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  181. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  182. (const :tag " vm: Links to VM folders/messages" org-vm)
  183. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  184. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  185. (const :tag " mouse: Additional mouse support" org-mouse)
  186. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  187. (const :tag "C annotation-helper: Call Remember directly from Browser\n\t\t\t(OBSOLETE, use org-protocol)" org-annotation-helper)
  188. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  189. (const :tag "C browser-url: Store link, directly from Browser\n\t\t\t(OBSOLETE, use org-protocol)" org-browser-url)
  190. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  191. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  192. (const :tag "C collector: Collect properties into tables" org-collector)
  193. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  194. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  195. (const :tag "C eval: Include command output as text" org-eval)
  196. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  197. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  198. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  199. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  200. (const :tag "C invoice Help manage client invoices in Org-mode" org-invoice)
  201. (const :tag "C jira Add a jira:ticket protocol to Org-mode" org-jira)
  202. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  203. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  204. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  205. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  206. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  207. (const :tag "C R: Computation using the R language" org-R)
  208. (const :tag "C registry: A registry for Org-mode links" org-registry)
  209. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  210. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  211. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  212. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  213. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  214. (const :tag "C track: Keep up with Org-mode development" org-track)
  215. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  216. (defcustom org-support-shift-select nil
  217. "Non-nil means, make shift-cursor commands select text when possible.
  218. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  219. selecting a region, or enlarge thusly regions started in this way.
  220. In Org-mode, in special contexts, these same keys are used for other
  221. purposes, important enough to compete with shift selection. Org tries
  222. to balance these needs by supporting `shift-select-mode' outside these
  223. special contexts, under control of this variable.
  224. The default of this variable is nil, to avoid confusing behavior. Shifted
  225. cursor keys will then execute Org commands in the following contexts:
  226. - on a headline, changing TODO state (left/right) and priority (up/down)
  227. - on a time stamp, changing the time
  228. - in a plain list item, changing the bullet type
  229. - in a property definition line, switching between allowed values
  230. - in the BEGIN line of a clock table (changing the time block).
  231. Outside these contexts, the commands will throw an error.
  232. When this variable is t and the cursor is not in a special context,
  233. Org-mode will support shift-selection for making and enlarging regions.
  234. To make this more effective, the bullet cycling will no longer happen
  235. anywhere in an item line, but only if the cursor is exactly on the bullet.
  236. If you set this variable to the symbol `always', then the keys
  237. will not be special in headlines, property lines, and item lines, to make
  238. shift selection work there as well. If this is what you want, you can
  239. use the following alternative commands: `C-c C-t' and `C-c ,' to
  240. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  241. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  242. edited by hand or in column view.
  243. However, when the cursor is on a timestamp, shift-cursor commands
  244. will still edit the time stamp - this is just too good to give up.
  245. XEmacs user should have this variable set to nil, because shift-select-mode
  246. is Emacs 23 only."
  247. :group 'org
  248. :type '(choice
  249. (const :tag "Never" nil)
  250. (const :tag "When outside special context" t)
  251. (const :tag "Everywhere except timestamps" always)))
  252. (defgroup org-startup nil
  253. "Options concerning startup of Org-mode."
  254. :tag "Org Startup"
  255. :group 'org)
  256. (defcustom org-startup-folded t
  257. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  258. This can also be configured on a per-file basis by adding one of
  259. the following lines anywhere in the buffer:
  260. #+STARTUP: fold (or `overview', this is equivalent)
  261. #+STARTUP: nofold (or `showall', this is equivalent)
  262. #+STARTUP: content
  263. #+STARTUP: showeverything"
  264. :group 'org-startup
  265. :type '(choice
  266. (const :tag "nofold: show all" nil)
  267. (const :tag "fold: overview" t)
  268. (const :tag "content: all headlines" content)
  269. (const :tag "show everything, even drawers" showeverything)))
  270. (defcustom org-startup-truncated t
  271. "Non-nil means, entering Org-mode will set `truncate-lines'.
  272. This is useful since some lines containing links can be very long and
  273. uninteresting. Also tables look terrible when wrapped."
  274. :group 'org-startup
  275. :type 'boolean)
  276. (defcustom org-startup-indented nil
  277. "Non-nil means, turn on `org-indent-mode' on startup.
  278. This can also be configured on a per-file basis by adding one of
  279. the following lines anywhere in the buffer:
  280. #+STARTUP: indent
  281. #+STARTUP: noindent"
  282. :group 'org-structure
  283. :type '(choice
  284. (const :tag "Not" nil)
  285. (const :tag "Globally (slow on startup in large files)" t)))
  286. (defcustom org-startup-align-all-tables nil
  287. "Non-nil means, align all tables when visiting a file.
  288. This is useful when the column width in tables is forced with <N> cookies
  289. in table fields. Such tables will look correct only after the first re-align.
  290. This can also be configured on a per-file basis by adding one of
  291. the following lines anywhere in the buffer:
  292. #+STARTUP: align
  293. #+STARTUP: noalign"
  294. :group 'org-startup
  295. :type 'boolean)
  296. (defcustom org-insert-mode-line-in-empty-file nil
  297. "Non-nil means insert the first line setting Org-mode in empty files.
  298. When the function `org-mode' is called interactively in an empty file, this
  299. normally means that the file name does not automatically trigger Org-mode.
  300. To ensure that the file will always be in Org-mode in the future, a
  301. line enforcing Org-mode will be inserted into the buffer, if this option
  302. has been set."
  303. :group 'org-startup
  304. :type 'boolean)
  305. (defcustom org-replace-disputed-keys nil
  306. "Non-nil means use alternative key bindings for some keys.
  307. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  308. These keys are also used by other packages like shift-selection-mode'
  309. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  310. If you want to use Org-mode together with one of these other modes,
  311. or more generally if you would like to move some Org-mode commands to
  312. other keys, set this variable and configure the keys with the variable
  313. `org-disputed-keys'.
  314. This option is only relevant at load-time of Org-mode, and must be set
  315. *before* org.el is loaded. Changing it requires a restart of Emacs to
  316. become effective."
  317. :group 'org-startup
  318. :type 'boolean)
  319. (defcustom org-use-extra-keys nil
  320. "Non-nil means use extra key sequence definitions for certain
  321. commands. This happens automatically if you run XEmacs or if
  322. window-system is nil. This variable lets you do the same
  323. manually. You must set it before loading org.
  324. Example: on Carbon Emacs 22 running graphically, with an external
  325. keyboard on a Powerbook, the default way of setting M-left might
  326. not work for either Alt or ESC. Setting this variable will make
  327. it work for ESC."
  328. :group 'org-startup
  329. :type 'boolean)
  330. (if (fboundp 'defvaralias)
  331. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  332. (defcustom org-disputed-keys
  333. '(([(shift up)] . [(meta p)])
  334. ([(shift down)] . [(meta n)])
  335. ([(shift left)] . [(meta -)])
  336. ([(shift right)] . [(meta +)])
  337. ([(control shift right)] . [(meta shift +)])
  338. ([(control shift left)] . [(meta shift -)]))
  339. "Keys for which Org-mode and other modes compete.
  340. This is an alist, cars are the default keys, second element specifies
  341. the alternative to use when `org-replace-disputed-keys' is t.
  342. Keys can be specified in any syntax supported by `define-key'.
  343. The value of this option takes effect only at Org-mode's startup,
  344. therefore you'll have to restart Emacs to apply it after changing."
  345. :group 'org-startup
  346. :type 'alist)
  347. (defun org-key (key)
  348. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  349. Or return the original if not disputed."
  350. (if org-replace-disputed-keys
  351. (let* ((nkey (key-description key))
  352. (x (org-find-if (lambda (x)
  353. (equal (key-description (car x)) nkey))
  354. org-disputed-keys)))
  355. (if x (cdr x) key))
  356. key))
  357. (defun org-find-if (predicate seq)
  358. (catch 'exit
  359. (while seq
  360. (if (funcall predicate (car seq))
  361. (throw 'exit (car seq))
  362. (pop seq)))))
  363. (defun org-defkey (keymap key def)
  364. "Define a key, possibly translated, as returned by `org-key'."
  365. (define-key keymap (org-key key) def))
  366. (defcustom org-ellipsis nil
  367. "The ellipsis to use in the Org-mode outline.
  368. When nil, just use the standard three dots. When a string, use that instead,
  369. When a face, use the standard 3 dots, but with the specified face.
  370. The change affects only Org-mode (which will then use its own display table).
  371. Changing this requires executing `M-x org-mode' in a buffer to become
  372. effective."
  373. :group 'org-startup
  374. :type '(choice (const :tag "Default" nil)
  375. (face :tag "Face" :value org-warning)
  376. (string :tag "String" :value "...#")))
  377. (defvar org-display-table nil
  378. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  379. (defgroup org-keywords nil
  380. "Keywords in Org-mode."
  381. :tag "Org Keywords"
  382. :group 'org)
  383. (defcustom org-deadline-string "DEADLINE:"
  384. "String to mark deadline entries.
  385. A deadline is this string, followed by a time stamp. Should be a word,
  386. terminated by a colon. You can insert a schedule keyword and
  387. a timestamp with \\[org-deadline].
  388. Changes become only effective after restarting Emacs."
  389. :group 'org-keywords
  390. :type 'string)
  391. (defcustom org-scheduled-string "SCHEDULED:"
  392. "String to mark scheduled TODO entries.
  393. A schedule is this string, followed by a time stamp. Should be a word,
  394. terminated by a colon. You can insert a schedule keyword and
  395. a timestamp with \\[org-schedule].
  396. Changes become only effective after restarting Emacs."
  397. :group 'org-keywords
  398. :type 'string)
  399. (defcustom org-closed-string "CLOSED:"
  400. "String used as the prefix for timestamps logging closing a TODO entry."
  401. :group 'org-keywords
  402. :type 'string)
  403. (defcustom org-clock-string "CLOCK:"
  404. "String used as prefix for timestamps clocking work hours on an item."
  405. :group 'org-keywords
  406. :type 'string)
  407. (defcustom org-comment-string "COMMENT"
  408. "Entries starting with this keyword will never be exported.
  409. An entry can be toggled between COMMENT and normal with
  410. \\[org-toggle-comment].
  411. Changes become only effective after restarting Emacs."
  412. :group 'org-keywords
  413. :type 'string)
  414. (defcustom org-quote-string "QUOTE"
  415. "Entries starting with this keyword will be exported in fixed-width font.
  416. Quoting applies only to the text in the entry following the headline, and does
  417. not extend beyond the next headline, even if that is lower level.
  418. An entry can be toggled between QUOTE and normal with
  419. \\[org-toggle-fixed-width-section]."
  420. :group 'org-keywords
  421. :type 'string)
  422. (defconst org-repeat-re
  423. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  424. "Regular expression for specifying repeated events.
  425. After a match, group 1 contains the repeat expression.")
  426. (defgroup org-structure nil
  427. "Options concerning the general structure of Org-mode files."
  428. :tag "Org Structure"
  429. :group 'org)
  430. (defgroup org-reveal-location nil
  431. "Options about how to make context of a location visible."
  432. :tag "Org Reveal Location"
  433. :group 'org-structure)
  434. (defconst org-context-choice
  435. '(choice
  436. (const :tag "Always" t)
  437. (const :tag "Never" nil)
  438. (repeat :greedy t :tag "Individual contexts"
  439. (cons
  440. (choice :tag "Context"
  441. (const agenda)
  442. (const org-goto)
  443. (const occur-tree)
  444. (const tags-tree)
  445. (const link-search)
  446. (const mark-goto)
  447. (const bookmark-jump)
  448. (const isearch)
  449. (const default))
  450. (boolean))))
  451. "Contexts for the reveal options.")
  452. (defcustom org-show-hierarchy-above '((default . t))
  453. "Non-nil means, show full hierarchy when revealing a location.
  454. Org-mode often shows locations in an org-mode file which might have
  455. been invisible before. When this is set, the hierarchy of headings
  456. above the exposed location is shown.
  457. Turning this off for example for sparse trees makes them very compact.
  458. Instead of t, this can also be an alist specifying this option for different
  459. contexts. Valid contexts are
  460. agenda when exposing an entry from the agenda
  461. org-goto when using the command `org-goto' on key C-c C-j
  462. occur-tree when using the command `org-occur' on key C-c /
  463. tags-tree when constructing a sparse tree based on tags matches
  464. link-search when exposing search matches associated with a link
  465. mark-goto when exposing the jump goal of a mark
  466. bookmark-jump when exposing a bookmark location
  467. isearch when exiting from an incremental search
  468. default default for all contexts not set explicitly"
  469. :group 'org-reveal-location
  470. :type org-context-choice)
  471. (defcustom org-show-following-heading '((default . nil))
  472. "Non-nil means, show following heading when revealing a location.
  473. Org-mode often shows locations in an org-mode file which might have
  474. been invisible before. When this is set, the heading following the
  475. match is shown.
  476. Turning this off for example for sparse trees makes them very compact,
  477. but makes it harder to edit the location of the match. In such a case,
  478. use the command \\[org-reveal] to show more context.
  479. Instead of t, this can also be an alist specifying this option for different
  480. contexts. See `org-show-hierarchy-above' for valid contexts."
  481. :group 'org-reveal-location
  482. :type org-context-choice)
  483. (defcustom org-show-siblings '((default . nil) (isearch t))
  484. "Non-nil means, show all sibling heading when revealing a location.
  485. Org-mode often shows locations in an org-mode file which might have
  486. been invisible before. When this is set, the sibling of the current entry
  487. heading are all made visible. If `org-show-hierarchy-above' is t,
  488. the same happens on each level of the hierarchy above the current entry.
  489. By default this is on for the isearch context, off for all other contexts.
  490. Turning this off for example for sparse trees makes them very compact,
  491. but makes it harder to edit the location of the match. In such a case,
  492. use the command \\[org-reveal] to show more context.
  493. Instead of t, this can also be an alist specifying this option for different
  494. contexts. See `org-show-hierarchy-above' for valid contexts."
  495. :group 'org-reveal-location
  496. :type org-context-choice)
  497. (defcustom org-show-entry-below '((default . nil))
  498. "Non-nil means, show the entry below a headline when revealing a location.
  499. Org-mode often shows locations in an org-mode file which might have
  500. been invisible before. When this is set, the text below the headline that is
  501. exposed is also shown.
  502. By default this is off for all contexts.
  503. Instead of t, this can also be an alist specifying this option for different
  504. contexts. See `org-show-hierarchy-above' for valid contexts."
  505. :group 'org-reveal-location
  506. :type org-context-choice)
  507. (defcustom org-indirect-buffer-display 'other-window
  508. "How should indirect tree buffers be displayed?
  509. This applies to indirect buffers created with the commands
  510. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  511. Valid values are:
  512. current-window Display in the current window
  513. other-window Just display in another window.
  514. dedicated-frame Create one new frame, and re-use it each time.
  515. new-frame Make a new frame each time. Note that in this case
  516. previously-made indirect buffers are kept, and you need to
  517. kill these buffers yourself."
  518. :group 'org-structure
  519. :group 'org-agenda-windows
  520. :type '(choice
  521. (const :tag "In current window" current-window)
  522. (const :tag "In current frame, other window" other-window)
  523. (const :tag "Each time a new frame" new-frame)
  524. (const :tag "One dedicated frame" dedicated-frame)))
  525. (defgroup org-cycle nil
  526. "Options concerning visibility cycling in Org-mode."
  527. :tag "Org Cycle"
  528. :group 'org-structure)
  529. (defcustom org-cycle-skip-children-state-if-no-children t
  530. "Non-nil means, skip CHILDREN state in entries that don't have any."
  531. :group 'org-cycle
  532. :type 'boolean)
  533. (defcustom org-cycle-max-level nil
  534. "Maximum level which should still be subject to visibility cycling.
  535. Levels higher than this will, for cycling, be treated as text, not a headline.
  536. When `org-odd-levels-only' is set, a value of N in this variable actually
  537. means 2N-1 stars as the limiting headline.
  538. When nil, cycle all levels.
  539. Note that the limiting level of cycling is also influenced by
  540. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  541. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  542. than its value."
  543. :group 'org-cycle
  544. :type '(choice
  545. (const :tag "No limit" nil)
  546. (integer :tag "Maximum level")))
  547. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  548. "Names of drawers. Drawers are not opened by cycling on the headline above.
  549. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  550. this:
  551. :DRAWERNAME:
  552. .....
  553. :END:
  554. The drawer \"PROPERTIES\" is special for capturing properties through
  555. the property API.
  556. Drawers can be defined on the per-file basis with a line like:
  557. #+DRAWERS: HIDDEN STATE PROPERTIES"
  558. :group 'org-structure
  559. :group 'org-cycle
  560. :type '(repeat (string :tag "Drawer Name")))
  561. (defcustom org-hide-block-startup nil
  562. "Non-nil means, , entering Org-mode will fold all blocks.
  563. This can also be set in on a per-file basis with
  564. #+STARTUP: hideblocks
  565. #+STARTUP: showblocks"
  566. :group 'org-startup
  567. :group 'org-cycle
  568. :type 'boolean)
  569. (defcustom org-cycle-global-at-bob nil
  570. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  571. This makes it possible to do global cycling without having to use S-TAB or
  572. C-u TAB. For this special case to work, the first line of the buffer
  573. must not be a headline - it may be empty or some other text. When used in
  574. this way, `org-cycle-hook' is disables temporarily, to make sure the
  575. cursor stays at the beginning of the buffer.
  576. When this option is nil, don't do anything special at the beginning
  577. of the buffer."
  578. :group 'org-cycle
  579. :type 'boolean)
  580. (defcustom org-cycle-emulate-tab t
  581. "Where should `org-cycle' emulate TAB.
  582. nil Never
  583. white Only in completely white lines
  584. whitestart Only at the beginning of lines, before the first non-white char
  585. t Everywhere except in headlines
  586. exc-hl-bol Everywhere except at the start of a headline
  587. If TAB is used in a place where it does not emulate TAB, the current subtree
  588. visibility is cycled."
  589. :group 'org-cycle
  590. :type '(choice (const :tag "Never" nil)
  591. (const :tag "Only in completely white lines" white)
  592. (const :tag "Before first char in a line" whitestart)
  593. (const :tag "Everywhere except in headlines" t)
  594. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  595. ))
  596. (defcustom org-cycle-separator-lines 2
  597. "Number of empty lines needed to keep an empty line between collapsed trees.
  598. If you leave an empty line between the end of a subtree and the following
  599. headline, this empty line is hidden when the subtree is folded.
  600. Org-mode will leave (exactly) one empty line visible if the number of
  601. empty lines is equal or larger to the number given in this variable.
  602. So the default 2 means, at least 2 empty lines after the end of a subtree
  603. are needed to produce free space between a collapsed subtree and the
  604. following headline.
  605. If the number is negative, and the number of empty lines is at least -N,
  606. all empty lines are shown.
  607. Special case: when 0, never leave empty lines in collapsed view."
  608. :group 'org-cycle
  609. :type 'integer)
  610. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  611. (defcustom org-pre-cycle-hook nil
  612. "Hook that is run before visibility cycling is happening.
  613. The function(s) in this hook must accept a single argument which indicates
  614. the new state that will be set right after running this hook. The
  615. argument is a symbol. Before a global state change, it can have the values
  616. `overview', `content', or `all'. Before a local state change, it can have
  617. the values `folded', `children', or `subtree'."
  618. :group 'org-cycle
  619. :type 'hook)
  620. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  621. org-cycle-hide-drawers
  622. org-cycle-show-empty-lines
  623. org-optimize-window-after-visibility-change)
  624. "Hook that is run after `org-cycle' has changed the buffer visibility.
  625. The function(s) in this hook must accept a single argument which indicates
  626. the new state that was set by the most recent `org-cycle' command. The
  627. argument is a symbol. After a global state change, it can have the values
  628. `overview', `content', or `all'. After a local state change, it can have
  629. the values `folded', `children', or `subtree'."
  630. :group 'org-cycle
  631. :type 'hook)
  632. (defgroup org-edit-structure nil
  633. "Options concerning structure editing in Org-mode."
  634. :tag "Org Edit Structure"
  635. :group 'org-structure)
  636. (defcustom org-odd-levels-only nil
  637. "Non-nil means, skip even levels and only use odd levels for the outline.
  638. This has the effect that two stars are being added/taken away in
  639. promotion/demotion commands. It also influences how levels are
  640. handled by the exporters.
  641. Changing it requires restart of `font-lock-mode' to become effective
  642. for fontification also in regions already fontified.
  643. You may also set this on a per-file basis by adding one of the following
  644. lines to the buffer:
  645. #+STARTUP: odd
  646. #+STARTUP: oddeven"
  647. :group 'org-edit-structure
  648. :group 'org-font-lock
  649. :type 'boolean)
  650. (defcustom org-adapt-indentation t
  651. "Non-nil means, adapt indentation to outline node level.
  652. When this variable is set, Org assumes that you write outlines by
  653. indenting text in each node to align with the headline (after the stars).
  654. The following issues are influenced by this variable:
  655. - When this is set and the *entire* text in an entry is indented, the
  656. indentation is increased by one space in a demotion command, and
  657. decreased by one in a promotion command. If any line in the entry
  658. body starts with text at column 0, indentation is not changed at all.
  659. - Property drawers and planning information is inserted indented when
  660. this variable s set. When nil, they will not be indented.
  661. - TAB indents a line relative to context. The lines below a headline
  662. will be indented when this variable is set.
  663. Note that this is all about true indentation, by adding and removing
  664. space characters. See also `org-indent.el' which does level-dependent
  665. indentation in a virtual way, i.e. at display time in Emacs."
  666. :group 'org-edit-structure
  667. :type 'boolean)
  668. (defcustom org-special-ctrl-a/e nil
  669. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  670. When t, `C-a' will bring back the cursor to the beginning of the
  671. headline text, i.e. after the stars and after a possible TODO keyword.
  672. In an item, this will be the position after the bullet.
  673. When the cursor is already at that position, another `C-a' will bring
  674. it to the beginning of the line.
  675. `C-e' will jump to the end of the headline, ignoring the presence of tags
  676. in the headline. A second `C-e' will then jump to the true end of the
  677. line, after any tags. This also means that, when this variable is
  678. non-nil, `C-e' also will never jump beyond the end of the heading of a
  679. folded section, i.e. not after the ellipses.
  680. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  681. going to the true line boundary first. Only a directly following, identical
  682. keypress will bring the cursor to the special positions.
  683. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  684. set separately."
  685. :group 'org-edit-structure
  686. :type '(choice
  687. (const :tag "off" nil)
  688. (const :tag "on: after stars/bullet and before tags first" t)
  689. (const :tag "reversed: true line boundary first" reversed)
  690. (cons :tag "Set C-a and C-e separately"
  691. (choice :tag "Special C-a"
  692. (const :tag "off" nil)
  693. (const :tag "on: after stars/bullet first" t)
  694. (const :tag "reversed: before stars/bullet first" reversed))
  695. (choice :tag "Special C-e"
  696. (const :tag "off" nil)
  697. (const :tag "on: before tags first" t)
  698. (const :tag "reversed: after tags first" reversed)))))
  699. (if (fboundp 'defvaralias)
  700. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  701. (defcustom org-special-ctrl-k nil
  702. "Non-nil means `C-k' will behave specially in headlines.
  703. When nil, `C-k' will call the default `kill-line' command.
  704. When t, the following will happen while the cursor is in the headline:
  705. - When the cursor is at the beginning of a headline, kill the entire
  706. line and possible the folded subtree below the line.
  707. - When in the middle of the headline text, kill the headline up to the tags.
  708. - When after the headline text, kill the tags."
  709. :group 'org-edit-structure
  710. :type 'boolean)
  711. (defcustom org-yank-folded-subtrees t
  712. "Non-nil means, when yanking subtrees, fold them.
  713. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  714. it starts with a heading and all other headings in it are either children
  715. or siblings, then fold all the subtrees. However, do this only if no
  716. text after the yank would be swallowed into a folded tree by this action."
  717. :group 'org-edit-structure
  718. :type 'boolean)
  719. (defcustom org-yank-adjusted-subtrees nil
  720. "Non-nil means, when yanking subtrees, adjust the level.
  721. With this setting, `org-paste-subtree' is used to insert the subtree, see
  722. this function for details."
  723. :group 'org-edit-structure
  724. :type 'boolean)
  725. (defcustom org-M-RET-may-split-line '((default . t))
  726. "Non-nil means, M-RET will split the line at the cursor position.
  727. When nil, it will go to the end of the line before making a
  728. new line.
  729. You may also set this option in a different way for different
  730. contexts. Valid contexts are:
  731. headline when creating a new headline
  732. item when creating a new item
  733. table in a table field
  734. default the value to be used for all contexts not explicitly
  735. customized"
  736. :group 'org-structure
  737. :group 'org-table
  738. :type '(choice
  739. (const :tag "Always" t)
  740. (const :tag "Never" nil)
  741. (repeat :greedy t :tag "Individual contexts"
  742. (cons
  743. (choice :tag "Context"
  744. (const headline)
  745. (const item)
  746. (const table)
  747. (const default))
  748. (boolean)))))
  749. (defcustom org-insert-heading-respect-content nil
  750. "Non-nil means, insert new headings after the current subtree.
  751. When nil, the new heading is created directly after the current line.
  752. The commands \\[org-insert-heading-respect-content] and
  753. \\[org-insert-todo-heading-respect-content] turn this variable on
  754. for the duration of the command."
  755. :group 'org-structure
  756. :type 'boolean)
  757. (defcustom org-blank-before-new-entry '((heading . auto)
  758. (plain-list-item . auto))
  759. "Should `org-insert-heading' leave a blank line before new heading/item?
  760. The value is an alist, with `heading' and `plain-list-item' as car,
  761. and a boolean flag as cdr. For plain lists, if the variable
  762. `org-empty-line-terminates-plain-lists' is set, the setting here
  763. is ignored and no empty line is inserted, to keep the list in tact."
  764. :group 'org-edit-structure
  765. :type '(list
  766. (cons (const heading)
  767. (choice (const :tag "Never" nil)
  768. (const :tag "Always" t)
  769. (const :tag "Auto" auto)))
  770. (cons (const plain-list-item)
  771. (choice (const :tag "Never" nil)
  772. (const :tag "Always" t)
  773. (const :tag "Auto" auto)))))
  774. (defcustom org-insert-heading-hook nil
  775. "Hook being run after inserting a new heading."
  776. :group 'org-edit-structure
  777. :type 'hook)
  778. (defcustom org-enable-fixed-width-editor t
  779. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  780. This currently only means, they are never auto-wrapped.
  781. When nil, such lines will be treated like ordinary lines.
  782. See also the QUOTE keyword."
  783. :group 'org-edit-structure
  784. :type 'boolean)
  785. (defcustom org-goto-auto-isearch t
  786. "Non-nil means, typing characters in org-goto starts incremental search."
  787. :group 'org-edit-structure
  788. :type 'boolean)
  789. (defgroup org-sparse-trees nil
  790. "Options concerning sparse trees in Org-mode."
  791. :tag "Org Sparse Trees"
  792. :group 'org-structure)
  793. (defcustom org-highlight-sparse-tree-matches t
  794. "Non-nil means, highlight all matches that define a sparse tree.
  795. The highlights will automatically disappear the next time the buffer is
  796. changed by an edit command."
  797. :group 'org-sparse-trees
  798. :type 'boolean)
  799. (defcustom org-remove-highlights-with-change t
  800. "Non-nil means, any change to the buffer will remove temporary highlights.
  801. Such highlights are created by `org-occur' and `org-clock-display'.
  802. When nil, `C-c C-c needs to be used to get rid of the highlights.
  803. The highlights created by `org-preview-latex-fragment' always need
  804. `C-c C-c' to be removed."
  805. :group 'org-sparse-trees
  806. :group 'org-time
  807. :type 'boolean)
  808. (defcustom org-occur-hook '(org-first-headline-recenter)
  809. "Hook that is run after `org-occur' has constructed a sparse tree.
  810. This can be used to recenter the window to show as much of the structure
  811. as possible."
  812. :group 'org-sparse-trees
  813. :type 'hook)
  814. (defgroup org-imenu-and-speedbar nil
  815. "Options concerning imenu and speedbar in Org-mode."
  816. :tag "Org Imenu and Speedbar"
  817. :group 'org-structure)
  818. (defcustom org-imenu-depth 2
  819. "The maximum level for Imenu access to Org-mode headlines.
  820. This also applied for speedbar access."
  821. :group 'org-imenu-and-speedbar
  822. :type 'integer)
  823. (defgroup org-table nil
  824. "Options concerning tables in Org-mode."
  825. :tag "Org Table"
  826. :group 'org)
  827. (defcustom org-enable-table-editor 'optimized
  828. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  829. When nil, such lines will be treated like ordinary lines.
  830. When equal to the symbol `optimized', the table editor will be optimized to
  831. do the following:
  832. - Automatic overwrite mode in front of whitespace in table fields.
  833. This makes the structure of the table stay in tact as long as the edited
  834. field does not exceed the column width.
  835. - Minimize the number of realigns. Normally, the table is aligned each time
  836. TAB or RET are pressed to move to another field. With optimization this
  837. happens only if changes to a field might have changed the column width.
  838. Optimization requires replacing the functions `self-insert-command',
  839. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  840. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  841. very good at guessing when a re-align will be necessary, but you can always
  842. force one with \\[org-ctrl-c-ctrl-c].
  843. If you would like to use the optimized version in Org-mode, but the
  844. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  845. This variable can be used to turn on and off the table editor during a session,
  846. but in order to toggle optimization, a restart is required.
  847. See also the variable `org-table-auto-blank-field'."
  848. :group 'org-table
  849. :type '(choice
  850. (const :tag "off" nil)
  851. (const :tag "on" t)
  852. (const :tag "on, optimized" optimized)))
  853. (defcustom org-self-insert-cluster-for-undo t
  854. "Non-nil means cluster self-insert commands for undo when possible.
  855. If this is set, then, like in the Emacs command loop, 20 consequtive
  856. characters will be undone together.
  857. This is configurable, because there is some impact on typing performance."
  858. :group 'org-table
  859. :type 'boolean)
  860. (defcustom org-table-tab-recognizes-table.el t
  861. "Non-nil means, TAB will automatically notice a table.el table.
  862. When it sees such a table, it moves point into it and - if necessary -
  863. calls `table-recognize-table'."
  864. :group 'org-table-editing
  865. :type 'boolean)
  866. (defgroup org-link nil
  867. "Options concerning links in Org-mode."
  868. :tag "Org Link"
  869. :group 'org)
  870. (defvar org-link-abbrev-alist-local nil
  871. "Buffer-local version of `org-link-abbrev-alist', which see.
  872. The value of this is taken from the #+LINK lines.")
  873. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  874. (defcustom org-link-abbrev-alist nil
  875. "Alist of link abbreviations.
  876. The car of each element is a string, to be replaced at the start of a link.
  877. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  878. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  879. [[linkkey:tag][description]]
  880. The 'linkkey' must be a word word, starting with a letter, followed
  881. by letters, numbers, '-' or '_'.
  882. If REPLACE is a string, the tag will simply be appended to create the link.
  883. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  884. the placeholder \"%h\" will cause a url-encoded version of the tag to
  885. be inserted at that point (see the function `url-hexify-string').
  886. REPLACE may also be a function that will be called with the tag as the
  887. only argument to create the link, which should be returned as a string.
  888. See the manual for examples."
  889. :group 'org-link
  890. :type '(repeat
  891. (cons
  892. (string :tag "Protocol")
  893. (choice
  894. (string :tag "Format")
  895. (function)))))
  896. (defcustom org-descriptive-links t
  897. "Non-nil means, hide link part and only show description of bracket links.
  898. Bracket links are like [[link][description]]. This variable sets the initial
  899. state in new org-mode buffers. The setting can then be toggled on a
  900. per-buffer basis from the Org->Hyperlinks menu."
  901. :group 'org-link
  902. :type 'boolean)
  903. (defcustom org-link-file-path-type 'adaptive
  904. "How the path name in file links should be stored.
  905. Valid values are:
  906. relative Relative to the current directory, i.e. the directory of the file
  907. into which the link is being inserted.
  908. absolute Absolute path, if possible with ~ for home directory.
  909. noabbrev Absolute path, no abbreviation of home directory.
  910. adaptive Use relative path for files in the current directory and sub-
  911. directories of it. For other files, use an absolute path."
  912. :group 'org-link
  913. :type '(choice
  914. (const relative)
  915. (const absolute)
  916. (const noabbrev)
  917. (const adaptive)))
  918. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  919. "Types of links that should be activated in Org-mode files.
  920. This is a list of symbols, each leading to the activation of a certain link
  921. type. In principle, it does not hurt to turn on most link types - there may
  922. be a small gain when turning off unused link types. The types are:
  923. bracket The recommended [[link][description]] or [[link]] links with hiding.
  924. angular Links in angular brackets that may contain whitespace like
  925. <bbdb:Carsten Dominik>.
  926. plain Plain links in normal text, no whitespace, like http://google.com.
  927. radio Text that is matched by a radio target, see manual for details.
  928. tag Tag settings in a headline (link to tag search).
  929. date Time stamps (link to calendar).
  930. footnote Footnote labels.
  931. Changing this variable requires a restart of Emacs to become effective."
  932. :group 'org-link
  933. :type '(set :greedy t
  934. (const :tag "Double bracket links (new style)" bracket)
  935. (const :tag "Angular bracket links (old style)" angular)
  936. (const :tag "Plain text links" plain)
  937. (const :tag "Radio target matches" radio)
  938. (const :tag "Tags" tag)
  939. (const :tag "Timestamps" date)
  940. (const :tag "Footnotes" footnote)))
  941. (defcustom org-make-link-description-function nil
  942. "Function to use to generate link descriptions from links. If
  943. nil the link location will be used. This function must take two
  944. parameters; the first is the link and the second the description
  945. org-insert-link has generated, and should return the description
  946. to use."
  947. :group 'org-link
  948. :type 'function)
  949. (defgroup org-link-store nil
  950. "Options concerning storing links in Org-mode."
  951. :tag "Org Store Link"
  952. :group 'org-link)
  953. (defcustom org-email-link-description-format "Email %c: %.30s"
  954. "Format of the description part of a link to an email or usenet message.
  955. The following %-escapes will be replaced by corresponding information:
  956. %F full \"From\" field
  957. %f name, taken from \"From\" field, address if no name
  958. %T full \"To\" field
  959. %t first name in \"To\" field, address if no name
  960. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  961. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  962. %s subject
  963. %m message-id.
  964. You may use normal field width specification between the % and the letter.
  965. This is for example useful to limit the length of the subject.
  966. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  967. :group 'org-link-store
  968. :type 'string)
  969. (defcustom org-from-is-user-regexp
  970. (let (r1 r2)
  971. (when (and user-mail-address (not (string= user-mail-address "")))
  972. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  973. (when (and user-full-name (not (string= user-full-name "")))
  974. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  975. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  976. "Regexp matched against the \"From:\" header of an email or usenet message.
  977. It should match if the message is from the user him/herself."
  978. :group 'org-link-store
  979. :type 'regexp)
  980. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  981. "Non-nil means, storing a link to an Org file will use entry IDs.
  982. Note that before this variable is even considered, org-id must be loaded,
  983. so please customize `org-modules' and turn it on.
  984. The variable can have the following values:
  985. t Create an ID if needed to make a link to the current entry.
  986. create-if-interactive
  987. If `org-store-link' is called directly (interactively, as a user
  988. command), do create an ID to support the link. But when doing the
  989. job for remember, only use the ID if it already exists. The
  990. purpose of this setting is to avoid proliferation of unwanted
  991. IDs, just because you happen to be in an Org file when you
  992. call `org-remember' that automatically and preemptively
  993. creates a link. If you do want to get an ID link in a remember
  994. template to an entry not having an ID, create it first by
  995. explicitly creating a link to it, using `C-c C-l' first.
  996. create-if-interactive-and-no-custom-id
  997. Like create-if-interactive, but do not create an ID if there is
  998. a CUSTOM_ID property defined in the entry. This is the default.
  999. use-existing
  1000. Use existing ID, do not create one.
  1001. nil Never use an ID to make a link, instead link using a text search for
  1002. the headline text."
  1003. :group 'org-link-store
  1004. :type '(choice
  1005. (const :tag "Create ID to make link" t)
  1006. (const :tag "Create if storing link interactively"
  1007. create-if-interactive)
  1008. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1009. create-if-interactive-and-no-custom-id)
  1010. (const :tag "Only use existing" use-existing)
  1011. (const :tag "Do not use ID to create link" nil)))
  1012. (defcustom org-context-in-file-links t
  1013. "Non-nil means, file links from `org-store-link' contain context.
  1014. A search string will be added to the file name with :: as separator and
  1015. used to find the context when the link is activated by the command
  1016. `org-open-at-point'.
  1017. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1018. negates this setting for the duration of the command."
  1019. :group 'org-link-store
  1020. :type 'boolean)
  1021. (defcustom org-keep-stored-link-after-insertion nil
  1022. "Non-nil means, keep link in list for entire session.
  1023. The command `org-store-link' adds a link pointing to the current
  1024. location to an internal list. These links accumulate during a session.
  1025. The command `org-insert-link' can be used to insert links into any
  1026. Org-mode file (offering completion for all stored links). When this
  1027. option is nil, every link which has been inserted once using \\[org-insert-link]
  1028. will be removed from the list, to make completing the unused links
  1029. more efficient."
  1030. :group 'org-link-store
  1031. :type 'boolean)
  1032. (defgroup org-link-follow nil
  1033. "Options concerning following links in Org-mode."
  1034. :tag "Org Follow Link"
  1035. :group 'org-link)
  1036. (defcustom org-link-translation-function nil
  1037. "Function to translate links with different syntax to Org syntax.
  1038. This can be used to translate links created for example by the Planner
  1039. or emacs-wiki packages to Org syntax.
  1040. The function must accept two parameters, a TYPE containing the link
  1041. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1042. which is everything after the link protocol. It should return a cons
  1043. with possibly modified values of type and path.
  1044. Org contains a function for this, so if you set this variable to
  1045. `org-translate-link-from-planner', you should be able follow many
  1046. links created by planner."
  1047. :group 'org-link-follow
  1048. :type 'function)
  1049. (defcustom org-follow-link-hook nil
  1050. "Hook that is run after a link has been followed."
  1051. :group 'org-link-follow
  1052. :type 'hook)
  1053. (defcustom org-tab-follows-link nil
  1054. "Non-nil means, on links TAB will follow the link.
  1055. Needs to be set before org.el is loaded.
  1056. This really should not be used, it does not make sense, and the
  1057. implementation is bad."
  1058. :group 'org-link-follow
  1059. :type 'boolean)
  1060. (defcustom org-return-follows-link nil
  1061. "Non-nil means, on links RET will follow the link.
  1062. Needs to be set before org.el is loaded."
  1063. :group 'org-link-follow
  1064. :type 'boolean)
  1065. (defcustom org-mouse-1-follows-link
  1066. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1067. "Non-nil means, mouse-1 on a link will follow the link.
  1068. A longer mouse click will still set point. Does not work on XEmacs.
  1069. Needs to be set before org.el is loaded."
  1070. :group 'org-link-follow
  1071. :type 'boolean)
  1072. (defcustom org-mark-ring-length 4
  1073. "Number of different positions to be recorded in the ring
  1074. Changing this requires a restart of Emacs to work correctly."
  1075. :group 'org-link-follow
  1076. :type 'integer)
  1077. (defcustom org-link-frame-setup
  1078. '((vm . vm-visit-folder-other-frame)
  1079. (gnus . gnus-other-frame)
  1080. (file . find-file-other-window))
  1081. "Setup the frame configuration for following links.
  1082. When following a link with Emacs, it may often be useful to display
  1083. this link in another window or frame. This variable can be used to
  1084. set this up for the different types of links.
  1085. For VM, use any of
  1086. `vm-visit-folder'
  1087. `vm-visit-folder-other-frame'
  1088. For Gnus, use any of
  1089. `gnus'
  1090. `gnus-other-frame'
  1091. `org-gnus-no-new-news'
  1092. For FILE, use any of
  1093. `find-file'
  1094. `find-file-other-window'
  1095. `find-file-other-frame'
  1096. For the calendar, use the variable `calendar-setup'.
  1097. For BBDB, it is currently only possible to display the matches in
  1098. another window."
  1099. :group 'org-link-follow
  1100. :type '(list
  1101. (cons (const vm)
  1102. (choice
  1103. (const vm-visit-folder)
  1104. (const vm-visit-folder-other-window)
  1105. (const vm-visit-folder-other-frame)))
  1106. (cons (const gnus)
  1107. (choice
  1108. (const gnus)
  1109. (const gnus-other-frame)
  1110. (const org-gnus-no-new-news)))
  1111. (cons (const file)
  1112. (choice
  1113. (const find-file)
  1114. (const find-file-other-window)
  1115. (const find-file-other-frame)))))
  1116. (defcustom org-display-internal-link-with-indirect-buffer nil
  1117. "Non-nil means, use indirect buffer to display infile links.
  1118. Activating internal links (from one location in a file to another location
  1119. in the same file) normally just jumps to the location. When the link is
  1120. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1121. another window. When this option is set, the other window actually displays
  1122. an indirect buffer clone of the current buffer, to avoid any visibility
  1123. changes to the current buffer."
  1124. :group 'org-link-follow
  1125. :type 'boolean)
  1126. (defcustom org-open-non-existing-files nil
  1127. "Non-nil means, `org-open-file' will open non-existing files.
  1128. When nil, an error will be generated.
  1129. This variable applies only to external applications because they
  1130. might choke on non-existing files. If the link is to a file that
  1131. will be openend in Emacs, the variable is ignored."
  1132. :group 'org-link-follow
  1133. :type 'boolean)
  1134. (defcustom org-open-directory-means-index-dot-org nil
  1135. "Non-nil means, a link to a directory really means to index.org.
  1136. When nil, following a directory link will run dired or open a finder/explorer
  1137. window on that directory."
  1138. :group 'org-link-follow
  1139. :type 'boolean)
  1140. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1141. "Function and arguments to call for following mailto links.
  1142. This is a list with the first element being a lisp function, and the
  1143. remaining elements being arguments to the function. In string arguments,
  1144. %a will be replaced by the address, and %s will be replaced by the subject
  1145. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1146. :group 'org-link-follow
  1147. :type '(choice
  1148. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1149. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1150. (const :tag "message-mail" (message-mail "%a" "%s"))
  1151. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1152. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1153. "Non-nil means, ask for confirmation before executing shell links.
  1154. Shell links can be dangerous: just think about a link
  1155. [[shell:rm -rf ~/*][Google Search]]
  1156. This link would show up in your Org-mode document as \"Google Search\",
  1157. but really it would remove your entire home directory.
  1158. Therefore we advise against setting this variable to nil.
  1159. Just change it to `y-or-n-p' if you want to confirm with a
  1160. single keystroke rather than having to type \"yes\"."
  1161. :group 'org-link-follow
  1162. :type '(choice
  1163. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1164. (const :tag "with y-or-n (faster)" y-or-n-p)
  1165. (const :tag "no confirmation (dangerous)" nil)))
  1166. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1167. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1168. Elisp links can be dangerous: just think about a link
  1169. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1170. This link would show up in your Org-mode document as \"Google Search\",
  1171. but really it would remove your entire home directory.
  1172. Therefore we advise against setting this variable to nil.
  1173. Just change it to `y-or-n-p' if you want to confirm with a
  1174. single keystroke rather than having to type \"yes\"."
  1175. :group 'org-link-follow
  1176. :type '(choice
  1177. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1178. (const :tag "with y-or-n (faster)" y-or-n-p)
  1179. (const :tag "no confirmation (dangerous)" nil)))
  1180. (defconst org-file-apps-defaults-gnu
  1181. '((remote . emacs)
  1182. (system . mailcap)
  1183. (t . mailcap))
  1184. "Default file applications on a UNIX or GNU/Linux system.
  1185. See `org-file-apps'.")
  1186. (defconst org-file-apps-defaults-macosx
  1187. '((remote . emacs)
  1188. (t . "open %s")
  1189. (system . "open %s")
  1190. ("ps.gz" . "gv %s")
  1191. ("eps.gz" . "gv %s")
  1192. ("dvi" . "xdvi %s")
  1193. ("fig" . "xfig %s"))
  1194. "Default file applications on a MacOS X system.
  1195. The system \"open\" is known as a default, but we use X11 applications
  1196. for some files for which the OS does not have a good default.
  1197. See `org-file-apps'.")
  1198. (defconst org-file-apps-defaults-windowsnt
  1199. (list
  1200. '(remote . emacs)
  1201. (cons t
  1202. (list (if (featurep 'xemacs)
  1203. 'mswindows-shell-execute
  1204. 'w32-shell-execute)
  1205. "open" 'file))
  1206. (cons 'system
  1207. (list (if (featurep 'xemacs)
  1208. 'mswindows-shell-execute
  1209. 'w32-shell-execute)
  1210. "open" 'file)))
  1211. "Default file applications on a Windows NT system.
  1212. The system \"open\" is used for most files.
  1213. See `org-file-apps'.")
  1214. (defcustom org-file-apps
  1215. '(
  1216. (auto-mode . emacs)
  1217. ("\\.x?html?\\'" . default)
  1218. ("\\.pdf\\'" . default)
  1219. )
  1220. "External applications for opening `file:path' items in a document.
  1221. Org-mode uses system defaults for different file types, but
  1222. you can use this variable to set the application for a given file
  1223. extension. The entries in this list are cons cells where the car identifies
  1224. files and the cdr the corresponding command. Possible values for the
  1225. file identifier are
  1226. \"regex\" Regular expression matched against the file name. For backward
  1227. compatibility, this can also be a string with only alphanumeric
  1228. characters, which is then interpreted as an extension.
  1229. `directory' Matches a directory
  1230. `remote' Matches a remote file, accessible through tramp or efs.
  1231. Remote files most likely should be visited through Emacs
  1232. because external applications cannot handle such paths.
  1233. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1234. so all files Emacs knows how to handle. Using this with
  1235. command `emacs' will open most files in Emacs. Beware that this
  1236. will also open html files inside Emacs, unless you add
  1237. (\"html\" . default) to the list as well.
  1238. t Default for files not matched by any of the other options.
  1239. `system' The system command to open files, like `open' on Windows
  1240. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1241. that will be selected if you call `C-c C-o' with a double
  1242. `C-u C-u' prefix.
  1243. Possible values for the command are:
  1244. `emacs' The file will be visited by the current Emacs process.
  1245. `default' Use the default application for this file type, which is the
  1246. association for t in the list, most likely in the system-specific
  1247. part.
  1248. This can be used to overrule an unwanted setting in the
  1249. system-specific variable.
  1250. `system' Use the system command for opening files, like \"open\".
  1251. This command is specified by the entry whose car is `system'.
  1252. Most likely, the system-specific version of this variable
  1253. does define this command, but you can overrule/replace it
  1254. here.
  1255. string A command to be executed by a shell; %s will be replaced
  1256. by the path to the file.
  1257. sexp A Lisp form which will be evaluated. The file path will
  1258. be available in the Lisp variable `file'.
  1259. For more examples, see the system specific constants
  1260. `org-file-apps-defaults-macosx'
  1261. `org-file-apps-defaults-windowsnt'
  1262. `org-file-apps-defaults-gnu'."
  1263. :group 'org-link-follow
  1264. :type '(repeat
  1265. (cons (choice :value ""
  1266. (string :tag "Extension")
  1267. (const :tag "System command to open files" system)
  1268. (const :tag "Default for unrecognized files" t)
  1269. (const :tag "Remote file" remote)
  1270. (const :tag "Links to a directory" directory)
  1271. (const :tag "Any files that have Emacs modes"
  1272. auto-mode))
  1273. (choice :value ""
  1274. (const :tag "Visit with Emacs" emacs)
  1275. (const :tag "Use default" default)
  1276. (const :tag "Use the system command" system)
  1277. (string :tag "Command")
  1278. (sexp :tag "Lisp form")))))
  1279. (defgroup org-refile nil
  1280. "Options concerning refiling entries in Org-mode."
  1281. :tag "Org Refile"
  1282. :group 'org)
  1283. (defcustom org-directory "~/org"
  1284. "Directory with org files.
  1285. This is just a default location to look for Org files. There is no need
  1286. at all to put your files into this directory. It is only used in the
  1287. following situations:
  1288. 1. When a remember template specifies a target file that is not an
  1289. absolute path. The path will then be interpreted relative to
  1290. `org-directory'
  1291. 2. When a remember note is filed away in an interactive way (when exiting the
  1292. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1293. with `org-directory' as the default path."
  1294. :group 'org-refile
  1295. :group 'org-remember
  1296. :type 'directory)
  1297. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1298. "Default target for storing notes.
  1299. Used by the hooks for remember.el. This can be a string, or nil to mean
  1300. the value of `remember-data-file'.
  1301. You can set this on a per-template basis with the variable
  1302. `org-remember-templates'."
  1303. :group 'org-refile
  1304. :group 'org-remember
  1305. :type '(choice
  1306. (const :tag "Default from remember-data-file" nil)
  1307. file))
  1308. (defcustom org-goto-interface 'outline
  1309. "The default interface to be used for `org-goto'.
  1310. Allowed values are:
  1311. outline The interface shows an outline of the relevant file
  1312. and the correct heading is found by moving through
  1313. the outline or by searching with incremental search.
  1314. outline-path-completion Headlines in the current buffer are offered via
  1315. completion. This is the interface also used by
  1316. the refile command."
  1317. :group 'org-refile
  1318. :type '(choice
  1319. (const :tag "Outline" outline)
  1320. (const :tag "Outline-path-completion" outline-path-completion)))
  1321. (defcustom org-goto-max-level 5
  1322. "Maximum level to be considered when running org-goto with refile interface."
  1323. :group 'org-refile
  1324. :type 'integer)
  1325. (defcustom org-reverse-note-order nil
  1326. "Non-nil means, store new notes at the beginning of a file or entry.
  1327. When nil, new notes will be filed to the end of a file or entry.
  1328. This can also be a list with cons cells of regular expressions that
  1329. are matched against file names, and values."
  1330. :group 'org-remember
  1331. :group 'org-refile
  1332. :type '(choice
  1333. (const :tag "Reverse always" t)
  1334. (const :tag "Reverse never" nil)
  1335. (repeat :tag "By file name regexp"
  1336. (cons regexp boolean))))
  1337. (defcustom org-refile-targets nil
  1338. "Targets for refiling entries with \\[org-refile].
  1339. This is list of cons cells. Each cell contains:
  1340. - a specification of the files to be considered, either a list of files,
  1341. or a symbol whose function or variable value will be used to retrieve
  1342. a file name or a list of file names. If you use `org-agenda-files' for
  1343. that, all agenda files will be scanned for targets. Nil means, consider
  1344. headings in the current buffer.
  1345. - A specification of how to find candidate refile targets. This may be
  1346. any of:
  1347. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1348. This tag has to be present in all target headlines, inheritance will
  1349. not be considered.
  1350. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1351. todo keyword.
  1352. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1353. headlines that are refiling targets.
  1354. - a cons cell (:level . N). Any headline of level N is considered a target.
  1355. Note that, when `org-odd-levels-only' is set, level corresponds to
  1356. order in hierarchy, not to the number of stars.
  1357. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1358. Note that, when `org-odd-levels-only' is set, level corresponds to
  1359. order in hierarchy, not to the number of stars.
  1360. You can set the variable `org-refile-target-verify-function' to a function
  1361. to verify each headline found by the simple critery above.
  1362. When this variable is nil, all top-level headlines in the current buffer
  1363. are used, equivalent to the value `((nil . (:level . 1))'."
  1364. :group 'org-refile
  1365. :type '(repeat
  1366. (cons
  1367. (choice :value org-agenda-files
  1368. (const :tag "All agenda files" org-agenda-files)
  1369. (const :tag "Current buffer" nil)
  1370. (function) (variable) (file))
  1371. (choice :tag "Identify target headline by"
  1372. (cons :tag "Specific tag" (const :value :tag) (string))
  1373. (cons :tag "TODO keyword" (const :value :todo) (string))
  1374. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1375. (cons :tag "Level number" (const :value :level) (integer))
  1376. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1377. (defcustom org-refile-target-verify-function nil
  1378. "Function to verify if the headline at point should be a refile target.
  1379. The function will be called without arguments, with point at the
  1380. beginning of the headline. It should return t and leave point
  1381. where it is if the headline is a valid target for refiling.
  1382. If the target should not be selected, the function must return nil.
  1383. In addition to this, it may move point to a place from where the search
  1384. should be continued. For example, the function may decide that the entire
  1385. subtree of the current entry should be excluded and move point to the end
  1386. of the subtree."
  1387. :group 'org-refile
  1388. :type 'function)
  1389. (defcustom org-refile-use-outline-path nil
  1390. "Non-nil means, provide refile targets as paths.
  1391. So a level 3 headline will be available as level1/level2/level3.
  1392. When the value is `file', also include the file name (without directory)
  1393. into the path. In this case, you can also stop the completion after
  1394. the file name, to get entries inserted as top level in the file.
  1395. When `full-file-path', include the full file path."
  1396. :group 'org-refile
  1397. :type '(choice
  1398. (const :tag "Not" nil)
  1399. (const :tag "Yes" t)
  1400. (const :tag "Start with file name" file)
  1401. (const :tag "Start with full file path" full-file-path)))
  1402. (defcustom org-outline-path-complete-in-steps t
  1403. "Non-nil means, complete the outline path in hierarchical steps.
  1404. When Org-mode uses the refile interface to select an outline path
  1405. \(see variable `org-refile-use-outline-path'), the completion of
  1406. the path can be done is a single go, or if can be done in steps down
  1407. the headline hierarchy. Going in steps is probably the best if you
  1408. do not use a special completion package like `ido' or `icicles'.
  1409. However, when using these packages, going in one step can be very
  1410. fast, while still showing the whole path to the entry."
  1411. :group 'org-refile
  1412. :type 'boolean)
  1413. (defcustom org-refile-allow-creating-parent-nodes nil
  1414. "Non-nil means, allow to create new nodes as refile targets.
  1415. New nodes are then created by adding \"/new node name\" to the completion
  1416. of an existing node. When the value of this variable is `confirm',
  1417. new node creation must be confirmed by the user (recommended)
  1418. When nil, the completion must match an existing entry.
  1419. Note that, if the new heading is not seen by the criteria
  1420. listed in `org-refile-targets', multiple instances of the same
  1421. heading would be created by trying again to file under the new
  1422. heading."
  1423. :group 'org-refile
  1424. :type '(choice
  1425. (const :tag "Never" nil)
  1426. (const :tag "Always" t)
  1427. (const :tag "Prompt for confirmation" confirm)))
  1428. (defgroup org-todo nil
  1429. "Options concerning TODO items in Org-mode."
  1430. :tag "Org TODO"
  1431. :group 'org)
  1432. (defgroup org-progress nil
  1433. "Options concerning Progress logging in Org-mode."
  1434. :tag "Org Progress"
  1435. :group 'org-time)
  1436. (defvar org-todo-interpretation-widgets
  1437. '(
  1438. (:tag "Sequence (cycling hits every state)" sequence)
  1439. (:tag "Type (cycling directly to DONE)" type))
  1440. "The available interpretation symbols for customizing
  1441. `org-todo-keywords'.
  1442. Interested libraries should add to this list.")
  1443. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1444. "List of TODO entry keyword sequences and their interpretation.
  1445. \\<org-mode-map>This is a list of sequences.
  1446. Each sequence starts with a symbol, either `sequence' or `type',
  1447. indicating if the keywords should be interpreted as a sequence of
  1448. action steps, or as different types of TODO items. The first
  1449. keywords are states requiring action - these states will select a headline
  1450. for inclusion into the global TODO list Org-mode produces. If one of
  1451. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1452. signify that no further action is necessary. If \"|\" is not found,
  1453. the last keyword is treated as the only DONE state of the sequence.
  1454. The command \\[org-todo] cycles an entry through these states, and one
  1455. additional state where no keyword is present. For details about this
  1456. cycling, see the manual.
  1457. TODO keywords and interpretation can also be set on a per-file basis with
  1458. the special #+SEQ_TODO and #+TYP_TODO lines.
  1459. Each keyword can optionally specify a character for fast state selection
  1460. \(in combination with the variable `org-use-fast-todo-selection')
  1461. and specifiers for state change logging, using the same syntax
  1462. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1463. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1464. indicates to record a time stamp each time this state is selected.
  1465. Each keyword may also specify if a timestamp or a note should be
  1466. recorded when entering or leaving the state, by adding additional
  1467. characters in the parenthesis after the keyword. This looks like this:
  1468. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1469. record only the time of the state change. With X and Y being either
  1470. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1471. Y when leaving the state if and only if the *target* state does not
  1472. define X. You may omit any of the fast-selection key or X or /Y,
  1473. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1474. For backward compatibility, this variable may also be just a list
  1475. of keywords - in this case the interpretation (sequence or type) will be
  1476. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1477. :group 'org-todo
  1478. :group 'org-keywords
  1479. :type '(choice
  1480. (repeat :tag "Old syntax, just keywords"
  1481. (string :tag "Keyword"))
  1482. (repeat :tag "New syntax"
  1483. (cons
  1484. (choice
  1485. :tag "Interpretation"
  1486. ;;Quick and dirty way to see
  1487. ;;`org-todo-interpretations'. This takes the
  1488. ;;place of item arguments
  1489. :convert-widget
  1490. (lambda (widget)
  1491. (widget-put widget
  1492. :args (mapcar
  1493. #'(lambda (x)
  1494. (widget-convert
  1495. (cons 'const x)))
  1496. org-todo-interpretation-widgets))
  1497. widget))
  1498. (repeat
  1499. (string :tag "Keyword"))))))
  1500. (defvar org-todo-keywords-1 nil
  1501. "All TODO and DONE keywords active in a buffer.")
  1502. (make-variable-buffer-local 'org-todo-keywords-1)
  1503. (defvar org-todo-keywords-for-agenda nil)
  1504. (defvar org-done-keywords-for-agenda nil)
  1505. (defvar org-drawers-for-agenda nil)
  1506. (defvar org-todo-keyword-alist-for-agenda nil)
  1507. (defvar org-tag-alist-for-agenda nil)
  1508. (defvar org-agenda-contributing-files nil)
  1509. (defvar org-not-done-keywords nil)
  1510. (make-variable-buffer-local 'org-not-done-keywords)
  1511. (defvar org-done-keywords nil)
  1512. (make-variable-buffer-local 'org-done-keywords)
  1513. (defvar org-todo-heads nil)
  1514. (make-variable-buffer-local 'org-todo-heads)
  1515. (defvar org-todo-sets nil)
  1516. (make-variable-buffer-local 'org-todo-sets)
  1517. (defvar org-todo-log-states nil)
  1518. (make-variable-buffer-local 'org-todo-log-states)
  1519. (defvar org-todo-kwd-alist nil)
  1520. (make-variable-buffer-local 'org-todo-kwd-alist)
  1521. (defvar org-todo-key-alist nil)
  1522. (make-variable-buffer-local 'org-todo-key-alist)
  1523. (defvar org-todo-key-trigger nil)
  1524. (make-variable-buffer-local 'org-todo-key-trigger)
  1525. (defcustom org-todo-interpretation 'sequence
  1526. "Controls how TODO keywords are interpreted.
  1527. This variable is in principle obsolete and is only used for
  1528. backward compatibility, if the interpretation of todo keywords is
  1529. not given already in `org-todo-keywords'. See that variable for
  1530. more information."
  1531. :group 'org-todo
  1532. :group 'org-keywords
  1533. :type '(choice (const sequence)
  1534. (const type)))
  1535. (defcustom org-use-fast-todo-selection t
  1536. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1537. This variable describes if and under what circumstances the cycling
  1538. mechanism for TODO keywords will be replaced by a single-key, direct
  1539. selection scheme.
  1540. When nil, fast selection is never used.
  1541. When the symbol `prefix', it will be used when `org-todo' is called with
  1542. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1543. in an agenda buffer.
  1544. When t, fast selection is used by default. In this case, the prefix
  1545. argument forces cycling instead.
  1546. In all cases, the special interface is only used if access keys have actually
  1547. been assigned by the user, i.e. if keywords in the configuration are followed
  1548. by a letter in parenthesis, like TODO(t)."
  1549. :group 'org-todo
  1550. :type '(choice
  1551. (const :tag "Never" nil)
  1552. (const :tag "By default" t)
  1553. (const :tag "Only with C-u C-c C-t" prefix)))
  1554. (defcustom org-provide-todo-statistics t
  1555. "Non-nil means, update todo statistics after insert and toggle.
  1556. ALL-HEADLINES means update todo statistics by including headlines
  1557. with no TODO keyword as well, counting them as not done.
  1558. A list of TODO keywords means the same, but skip keywords that are
  1559. not in this list.
  1560. When this is set, todo statistics is updated in the parent of the
  1561. current entry each time a todo state is changed."
  1562. :group 'org-todo
  1563. :type '(choice
  1564. (const :tag "Yes, only for TODO entries" t)
  1565. (const :tag "Yes, including all entries" 'all-headlines)
  1566. (repeat :tag "Yes, for TODOs in this list"
  1567. (string :tag "TODO keyword"))
  1568. (other :tag "No TODO statistics" nil)))
  1569. (defcustom org-hierarchical-todo-statistics t
  1570. "Non-nil means, TODO statistics covers just direct children.
  1571. When nil, all entries in the subtree are considered.
  1572. This has only an effect if `org-provide-todo-statistics' is set.
  1573. To set this to nil for only a single subtree, use a COOKIE_DATA
  1574. property and include the word \"recursive\" into the value."
  1575. :group 'org-todo
  1576. :type 'boolean)
  1577. (defcustom org-after-todo-state-change-hook nil
  1578. "Hook which is run after the state of a TODO item was changed.
  1579. The new state (a string with a TODO keyword, or nil) is available in the
  1580. Lisp variable `state'."
  1581. :group 'org-todo
  1582. :type 'hook)
  1583. (defvar org-blocker-hook nil
  1584. "Hook for functions that are allowed to block a state change.
  1585. Each function gets as its single argument a property list, see
  1586. `org-trigger-hook' for more information about this list.
  1587. If any of the functions in this hook returns nil, the state change
  1588. is blocked.")
  1589. (defvar org-trigger-hook nil
  1590. "Hook for functions that are triggered by a state change.
  1591. Each function gets as its single argument a property list with at least
  1592. the following elements:
  1593. (:type type-of-change :position pos-at-entry-start
  1594. :from old-state :to new-state)
  1595. Depending on the type, more properties may be present.
  1596. This mechanism is currently implemented for:
  1597. TODO state changes
  1598. ------------------
  1599. :type todo-state-change
  1600. :from previous state (keyword as a string), or nil, or a symbol
  1601. 'todo' or 'done', to indicate the general type of state.
  1602. :to new state, like in :from")
  1603. (defcustom org-enforce-todo-dependencies nil
  1604. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1605. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1606. be blocked if any prior sibling is not yet done.
  1607. Finally, if the parent is blocked because of ordered siblings of its own,
  1608. the child will also be blocked.
  1609. This variable needs to be set before org.el is loaded, and you need to
  1610. restart Emacs after a change to make the change effective. The only way
  1611. to change is while Emacs is running is through the customize interface."
  1612. :set (lambda (var val)
  1613. (set var val)
  1614. (if val
  1615. (add-hook 'org-blocker-hook
  1616. 'org-block-todo-from-children-or-siblings-or-parent)
  1617. (remove-hook 'org-blocker-hook
  1618. 'org-block-todo-from-children-or-siblings-or-parent)))
  1619. :group 'org-todo
  1620. :type 'boolean)
  1621. (defcustom org-enforce-todo-checkbox-dependencies nil
  1622. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1623. When this is nil, checkboxes have no influence on switching TODO states.
  1624. When non-nil, you first need to check off all check boxes before the TODO
  1625. entry can be switched to DONE.
  1626. This variable needs to be set before org.el is loaded, and you need to
  1627. restart Emacs after a change to make the change effective. The only way
  1628. to change is while Emacs is running is through the customize interface."
  1629. :set (lambda (var val)
  1630. (set var val)
  1631. (if val
  1632. (add-hook 'org-blocker-hook
  1633. 'org-block-todo-from-checkboxes)
  1634. (remove-hook 'org-blocker-hook
  1635. 'org-block-todo-from-checkboxes)))
  1636. :group 'org-todo
  1637. :type 'boolean)
  1638. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1639. "Non-nil means, inserting a TODO heading is treated as state change.
  1640. So when the command \\[org-insert-todo-heading] is used, state change
  1641. logging will apply if appropriate. When nil, the new TODO item will
  1642. be inserted directly, and no logging will take place."
  1643. :group 'org-todo
  1644. :type 'boolean)
  1645. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1646. "Non-nil means, switching TODO states with S-cursor counts as state change.
  1647. This is the default behavior. However, setting this to nil allows a
  1648. convenient way to select a TODO state and bypass any logging associated
  1649. with that."
  1650. :group 'org-todo
  1651. :type 'boolean)
  1652. (defcustom org-todo-state-tags-triggers nil
  1653. "Tag changes that should be triggered by TODO state changes.
  1654. This is a list. Each entry is
  1655. (state-change (tag . flag) .......)
  1656. State-change can be a string with a state, and empty string to indicate the
  1657. state that has no TODO keyword, or it can be one of the symbols `todo'
  1658. or `done', meaning any not-done or done state, respectively."
  1659. :group 'org-todo
  1660. :group 'org-tags
  1661. :type '(repeat
  1662. (cons (choice :tag "When changing to"
  1663. (const :tag "Not-done state" todo)
  1664. (const :tag "Done state" done)
  1665. (string :tag "State"))
  1666. (repeat
  1667. (cons :tag "Tag action"
  1668. (string :tag "Tag")
  1669. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1670. (defcustom org-log-done nil
  1671. "Information to record when a task moves to the DONE state.
  1672. Possible values are:
  1673. nil Don't add anything, just change the keyword
  1674. time Add a time stamp to the task
  1675. note Prompt a closing note and add it with template `org-log-note-headings'
  1676. This option can also be set with on a per-file-basis with
  1677. #+STARTUP: nologdone
  1678. #+STARTUP: logdone
  1679. #+STARTUP: lognotedone
  1680. You can have local logging settings for a subtree by setting the LOGGING
  1681. property to one or more of these keywords."
  1682. :group 'org-todo
  1683. :group 'org-progress
  1684. :type '(choice
  1685. (const :tag "No logging" nil)
  1686. (const :tag "Record CLOSED timestamp" time)
  1687. (const :tag "Record CLOSED timestamp with closing note." note)))
  1688. ;; Normalize old uses of org-log-done.
  1689. (cond
  1690. ((eq org-log-done t) (setq org-log-done 'time))
  1691. ((and (listp org-log-done) (memq 'done org-log-done))
  1692. (setq org-log-done 'note)))
  1693. (defcustom org-log-note-clock-out nil
  1694. "Non-nil means, record a note when clocking out of an item.
  1695. This can also be configured on a per-file basis by adding one of
  1696. the following lines anywhere in the buffer:
  1697. #+STARTUP: lognoteclock-out
  1698. #+STARTUP: nolognoteclock-out"
  1699. :group 'org-todo
  1700. :group 'org-progress
  1701. :type 'boolean)
  1702. (defcustom org-log-done-with-time t
  1703. "Non-nil means, the CLOSED time stamp will contain date and time.
  1704. When nil, only the date will be recorded."
  1705. :group 'org-progress
  1706. :type 'boolean)
  1707. (defcustom org-log-note-headings
  1708. '((done . "CLOSING NOTE %t")
  1709. (state . "State %-12s from %-12S %t")
  1710. (note . "Note taken on %t")
  1711. (clock-out . ""))
  1712. "Headings for notes added to entries.
  1713. The value is an alist, with the car being a symbol indicating the note
  1714. context, and the cdr is the heading to be used. The heading may also be the
  1715. empty string.
  1716. %t in the heading will be replaced by a time stamp.
  1717. %s will be replaced by the new TODO state, in double quotes.
  1718. %S will be replaced by the old TODO state, in double quotes.
  1719. %u will be replaced by the user name.
  1720. %U will be replaced by the full user name."
  1721. :group 'org-todo
  1722. :group 'org-progress
  1723. :type '(list :greedy t
  1724. (cons (const :tag "Heading when closing an item" done) string)
  1725. (cons (const :tag
  1726. "Heading when changing todo state (todo sequence only)"
  1727. state) string)
  1728. (cons (const :tag "Heading when just taking a note" note) string)
  1729. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1730. (unless (assq 'note org-log-note-headings)
  1731. (push '(note . "%t") org-log-note-headings))
  1732. (defcustom org-log-into-drawer nil
  1733. "Non-nil means, insert state change notes and time stamps into a drawer.
  1734. When nil, state changes notes will be inserted after the headline and
  1735. any scheduling and clock lines, but not inside a drawer.
  1736. The value of this variable should be the name of the drawer to use.
  1737. LOGBOOK is proposed at the default drawer for this purpose, you can
  1738. also set this to a string to define the drawer of your choice.
  1739. A value of t is also allowed, representing \"LOGBOOK\".
  1740. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1741. will be ignored.
  1742. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1743. a subtree."
  1744. :group 'org-todo
  1745. :group 'org-progress
  1746. :type '(choice
  1747. (const :tag "Not into a drawer" nil)
  1748. (const :tag "LOGBOOK" t)
  1749. (string :tag "Other")))
  1750. (if (fboundp 'defvaralias)
  1751. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1752. (defun org-log-into-drawer ()
  1753. "Return the value of `org-log-into-drawer', but let properties overrule.
  1754. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1755. used instead of the default value."
  1756. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1757. (cond
  1758. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1759. ((equal p "t") "LOGBOOK")
  1760. (t p))))
  1761. (defcustom org-log-state-notes-insert-after-drawers nil
  1762. "Non-nil means, insert state change notes after any drawers in entry.
  1763. Only the drawers that *immediately* follow the headline and the
  1764. deadline/scheduled line are skipped.
  1765. When nil, insert notes right after the heading and perhaps the line
  1766. with deadline/scheduling if present.
  1767. This variable will have no effect if `org-log-into-drawer' is
  1768. set."
  1769. :group 'org-todo
  1770. :group 'org-progress
  1771. :type 'boolean)
  1772. (defcustom org-log-states-order-reversed t
  1773. "Non-nil means, the latest state change note will be directly after heading.
  1774. When nil, the notes will be orderer according to time."
  1775. :group 'org-todo
  1776. :group 'org-progress
  1777. :type 'boolean)
  1778. (defcustom org-log-repeat 'time
  1779. "Non-nil means, record moving through the DONE state when triggering repeat.
  1780. An auto-repeating task is immediately switched back to TODO when
  1781. marked DONE. If you are not logging state changes (by adding \"@\"
  1782. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  1783. record a closing note, there will be no record of the task moving
  1784. through DONE. This variable forces taking a note anyway.
  1785. nil Don't force a record
  1786. time Record a time stamp
  1787. note Record a note
  1788. This option can also be set with on a per-file-basis with
  1789. #+STARTUP: logrepeat
  1790. #+STARTUP: lognoterepeat
  1791. #+STARTUP: nologrepeat
  1792. You can have local logging settings for a subtree by setting the LOGGING
  1793. property to one or more of these keywords."
  1794. :group 'org-todo
  1795. :group 'org-progress
  1796. :type '(choice
  1797. (const :tag "Don't force a record" nil)
  1798. (const :tag "Force recording the DONE state" time)
  1799. (const :tag "Force recording a note with the DONE state" note)))
  1800. (defgroup org-priorities nil
  1801. "Priorities in Org-mode."
  1802. :tag "Org Priorities"
  1803. :group 'org-todo)
  1804. (defcustom org-enable-priority-commands t
  1805. "Non-nil means, priority commands are active.
  1806. When nil, these commands will be disabled, so that you never accidentally
  1807. set a priority."
  1808. :group 'org-priorities
  1809. :type 'boolean)
  1810. (defcustom org-highest-priority ?A
  1811. "The highest priority of TODO items. A character like ?A, ?B etc.
  1812. Must have a smaller ASCII number than `org-lowest-priority'."
  1813. :group 'org-priorities
  1814. :type 'character)
  1815. (defcustom org-lowest-priority ?C
  1816. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1817. Must have a larger ASCII number than `org-highest-priority'."
  1818. :group 'org-priorities
  1819. :type 'character)
  1820. (defcustom org-default-priority ?B
  1821. "The default priority of TODO items.
  1822. This is the priority an item get if no explicit priority is given."
  1823. :group 'org-priorities
  1824. :type 'character)
  1825. (defcustom org-priority-start-cycle-with-default t
  1826. "Non-nil means, start with default priority when starting to cycle.
  1827. When this is nil, the first step in the cycle will be (depending on the
  1828. command used) one higher or lower that the default priority."
  1829. :group 'org-priorities
  1830. :type 'boolean)
  1831. (defgroup org-time nil
  1832. "Options concerning time stamps and deadlines in Org-mode."
  1833. :tag "Org Time"
  1834. :group 'org)
  1835. (defcustom org-insert-labeled-timestamps-at-point nil
  1836. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1837. When nil, these labeled time stamps are forces into the second line of an
  1838. entry, just after the headline. When scheduling from the global TODO list,
  1839. the time stamp will always be forced into the second line."
  1840. :group 'org-time
  1841. :type 'boolean)
  1842. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1843. "Formats for `format-time-string' which are used for time stamps.
  1844. It is not recommended to change this constant.")
  1845. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1846. "Number of minutes to round time stamps to.
  1847. These are two values, the first applies when first creating a time stamp.
  1848. The second applies when changing it with the commands `S-up' and `S-down'.
  1849. When changing the time stamp, this means that it will change in steps
  1850. of N minutes, as given by the second value.
  1851. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1852. numbers should be factors of 60, so for example 5, 10, 15.
  1853. When this is larger than 1, you can still force an exact time-stamp by using
  1854. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1855. and by using a prefix arg to `S-up/down' to specify the exact number
  1856. of minutes to shift."
  1857. :group 'org-time
  1858. :get '(lambda (var) ; Make sure all entries have 5 elements
  1859. (if (integerp (default-value var))
  1860. (list (default-value var) 5)
  1861. (default-value var)))
  1862. :type '(list
  1863. (integer :tag "when inserting times")
  1864. (integer :tag "when modifying times")))
  1865. ;; Normalize old customizations of this variable.
  1866. (when (integerp org-time-stamp-rounding-minutes)
  1867. (setq org-time-stamp-rounding-minutes
  1868. (list org-time-stamp-rounding-minutes
  1869. org-time-stamp-rounding-minutes)))
  1870. (defcustom org-display-custom-times nil
  1871. "Non-nil means, overlay custom formats over all time stamps.
  1872. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1873. To turn this on on a per-file basis, insert anywhere in the file:
  1874. #+STARTUP: customtime"
  1875. :group 'org-time
  1876. :set 'set-default
  1877. :type 'sexp)
  1878. (make-variable-buffer-local 'org-display-custom-times)
  1879. (defcustom org-time-stamp-custom-formats
  1880. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1881. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1882. These are overlayed over the default ISO format if the variable
  1883. `org-display-custom-times' is set. Time like %H:%M should be at the
  1884. end of the second format. The custom formats are also honored by export
  1885. commands, if custom time display is turned on at the time of export."
  1886. :group 'org-time
  1887. :type 'sexp)
  1888. (defun org-time-stamp-format (&optional long inactive)
  1889. "Get the right format for a time string."
  1890. (let ((f (if long (cdr org-time-stamp-formats)
  1891. (car org-time-stamp-formats))))
  1892. (if inactive
  1893. (concat "[" (substring f 1 -1) "]")
  1894. f)))
  1895. (defcustom org-time-clocksum-format "%d:%02d"
  1896. "The format string used when creating CLOCKSUM lines, or when
  1897. org-mode generates a time duration."
  1898. :group 'org-time
  1899. :type 'string)
  1900. (defcustom org-time-clocksum-use-fractional nil
  1901. "If non-nil, \\[org-clock-display] uses fractional times.
  1902. org-mode generates a time duration."
  1903. :group 'org-time
  1904. :type 'boolean)
  1905. (defcustom org-time-clocksum-fractional-format "%.2f"
  1906. "The format string used when creating CLOCKSUM lines, or when
  1907. org-mode generates a time duration."
  1908. :group 'org-time
  1909. :type 'string)
  1910. (defcustom org-deadline-warning-days 14
  1911. "No. of days before expiration during which a deadline becomes active.
  1912. This variable governs the display in sparse trees and in the agenda.
  1913. When 0 or negative, it means use this number (the absolute value of it)
  1914. even if a deadline has a different individual lead time specified.
  1915. Custom commands can set this variable in the options section."
  1916. :group 'org-time
  1917. :group 'org-agenda-daily/weekly
  1918. :type 'integer)
  1919. (defcustom org-read-date-prefer-future t
  1920. "Non-nil means, assume future for incomplete date input from user.
  1921. This affects the following situations:
  1922. 1. The user gives a month but not a year.
  1923. For example, if it is april and you enter \"feb 2\", this will be read
  1924. as feb 2, *next* year. \"May 5\", however, will be this year.
  1925. 2. The user gives a day, but no month.
  1926. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1927. read this as the third of *next* month. However, if you enter \"17\",
  1928. it will be considered as *this* month.
  1929. If you set this variable to the symbol `time', then also the following
  1930. will work:
  1931. 3. If the user gives a time, but no day. If the time is before now,
  1932. to will be interpreted as tomorrow.
  1933. Currently none of this works for ISO week specifications.
  1934. When this option is nil, the current day, month and year will always be
  1935. used as defaults."
  1936. :group 'org-time
  1937. :type '(choice
  1938. (const :tag "Never" nil)
  1939. (const :tag "Check month and day" t)
  1940. (const :tag "Check month, day, and time" time)))
  1941. (defcustom org-read-date-display-live t
  1942. "Non-nil means, display current interpretation of date prompt live.
  1943. This display will be in an overlay, in the minibuffer."
  1944. :group 'org-time
  1945. :type 'boolean)
  1946. (defcustom org-read-date-popup-calendar t
  1947. "Non-nil means, pop up a calendar when prompting for a date.
  1948. In the calendar, the date can be selected with mouse-1. However, the
  1949. minibuffer will also be active, and you can simply enter the date as well.
  1950. When nil, only the minibuffer will be available."
  1951. :group 'org-time
  1952. :type 'boolean)
  1953. (if (fboundp 'defvaralias)
  1954. (defvaralias 'org-popup-calendar-for-date-prompt
  1955. 'org-read-date-popup-calendar))
  1956. (defcustom org-read-date-minibuffer-setup-hook nil
  1957. "Hook to be used to set up keys for the date/time interface.
  1958. Add key definitions to `minibuffer-local-map', which will be a temporary
  1959. copy."
  1960. :group 'org-time
  1961. :type 'hook)
  1962. (defcustom org-extend-today-until 0
  1963. "The hour when your day really ends. Must be an integer.
  1964. This has influence for the following applications:
  1965. - When switching the agenda to \"today\". It it is still earlier than
  1966. the time given here, the day recognized as TODAY is actually yesterday.
  1967. - When a date is read from the user and it is still before the time given
  1968. here, the current date and time will be assumed to be yesterday, 23:59.
  1969. Also, timestamps inserted in remember templates follow this rule.
  1970. IMPORTANT: This is a feature whose implementation is and likely will
  1971. remain incomplete. Really, it is only here because past midnight seems to
  1972. be the favorite working time of John Wiegley :-)"
  1973. :group 'org-time
  1974. :type 'integer)
  1975. (defcustom org-edit-timestamp-down-means-later nil
  1976. "Non-nil means, S-down will increase the time in a time stamp.
  1977. When nil, S-up will increase."
  1978. :group 'org-time
  1979. :type 'boolean)
  1980. (defcustom org-calendar-follow-timestamp-change t
  1981. "Non-nil means, make the calendar window follow timestamp changes.
  1982. When a timestamp is modified and the calendar window is visible, it will be
  1983. moved to the new date."
  1984. :group 'org-time
  1985. :type 'boolean)
  1986. (defgroup org-tags nil
  1987. "Options concerning tags in Org-mode."
  1988. :tag "Org Tags"
  1989. :group 'org)
  1990. (defcustom org-tag-alist nil
  1991. "List of tags allowed in Org-mode files.
  1992. When this list is nil, Org-mode will base TAG input on what is already in the
  1993. buffer.
  1994. The value of this variable is an alist, the car of each entry must be a
  1995. keyword as a string, the cdr may be a character that is used to select
  1996. that tag through the fast-tag-selection interface.
  1997. See the manual for details."
  1998. :group 'org-tags
  1999. :type '(repeat
  2000. (choice
  2001. (cons (string :tag "Tag name")
  2002. (character :tag "Access char"))
  2003. (list :tag "Start radio group"
  2004. (const :startgroup)
  2005. (option (string :tag "Group description")))
  2006. (list :tag "End radio group"
  2007. (const :endgroup)
  2008. (option (string :tag "Group description")))
  2009. (const :tag "New line" (:newline)))))
  2010. (defcustom org-tag-persistent-alist nil
  2011. "List of tags that will always appear in all Org-mode files.
  2012. This is in addition to any in buffer settings or customizations
  2013. of `org-tag-alist'.
  2014. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2015. The value of this variable is an alist, the car of each entry must be a
  2016. keyword as a string, the cdr may be a character that is used to select
  2017. that tag through the fast-tag-selection interface.
  2018. See the manual for details.
  2019. To disable these tags on a per-file basis, insert anywhere in the file:
  2020. #+STARTUP: noptag"
  2021. :group 'org-tags
  2022. :type '(repeat
  2023. (choice
  2024. (cons (string :tag "Tag name")
  2025. (character :tag "Access char"))
  2026. (const :tag "Start radio group" (:startgroup))
  2027. (const :tag "End radio group" (:endgroup))
  2028. (const :tag "New line" (:newline)))))
  2029. (defvar org-file-tags nil
  2030. "List of tags that can be inherited by all entries in the file.
  2031. The tags will be inherited if the variable `org-use-tag-inheritance'
  2032. says they should be.
  2033. This variable is populated from #+FILETAGS lines.")
  2034. (defcustom org-use-fast-tag-selection 'auto
  2035. "Non-nil means, use fast tag selection scheme.
  2036. This is a special interface to select and deselect tags with single keys.
  2037. When nil, fast selection is never used.
  2038. When the symbol `auto', fast selection is used if and only if selection
  2039. characters for tags have been configured, either through the variable
  2040. `org-tag-alist' or through a #+TAGS line in the buffer.
  2041. When t, fast selection is always used and selection keys are assigned
  2042. automatically if necessary."
  2043. :group 'org-tags
  2044. :type '(choice
  2045. (const :tag "Always" t)
  2046. (const :tag "Never" nil)
  2047. (const :tag "When selection characters are configured" 'auto)))
  2048. (defcustom org-fast-tag-selection-single-key nil
  2049. "Non-nil means, fast tag selection exits after first change.
  2050. When nil, you have to press RET to exit it.
  2051. During fast tag selection, you can toggle this flag with `C-c'.
  2052. This variable can also have the value `expert'. In this case, the window
  2053. displaying the tags menu is not even shown, until you press C-c again."
  2054. :group 'org-tags
  2055. :type '(choice
  2056. (const :tag "No" nil)
  2057. (const :tag "Yes" t)
  2058. (const :tag "Expert" expert)))
  2059. (defvar org-fast-tag-selection-include-todo nil
  2060. "Non-nil means, fast tags selection interface will also offer TODO states.
  2061. This is an undocumented feature, you should not rely on it.")
  2062. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2063. "The column to which tags should be indented in a headline.
  2064. If this number is positive, it specifies the column. If it is negative,
  2065. it means that the tags should be flushright to that column. For example,
  2066. -80 works well for a normal 80 character screen."
  2067. :group 'org-tags
  2068. :type 'integer)
  2069. (defcustom org-auto-align-tags t
  2070. "Non-nil means, realign tags after pro/demotion of TODO state change.
  2071. These operations change the length of a headline and therefore shift
  2072. the tags around. With this options turned on, after each such operation
  2073. the tags are again aligned to `org-tags-column'."
  2074. :group 'org-tags
  2075. :type 'boolean)
  2076. (defcustom org-use-tag-inheritance t
  2077. "Non-nil means, tags in levels apply also for sublevels.
  2078. When nil, only the tags directly given in a specific line apply there.
  2079. This may also be a list of tags that should be inherited, or a regexp that
  2080. matches tags that should be inherited. Additional control is possible
  2081. with the variable `org-tags-exclude-from-inheritance' which gives an
  2082. explicit list of tags to be excluded from inheritance., even if the value of
  2083. `org-use-tag-inheritance' would select it for inheritance.
  2084. If this option is t, a match early-on in a tree can lead to a large
  2085. number of matches in the subtree when constructing the agenda or creating
  2086. a sparse tree. If you only want to see the first match in a tree during
  2087. a search, check out the variable `org-tags-match-list-sublevels'."
  2088. :group 'org-tags
  2089. :type '(choice
  2090. (const :tag "Not" nil)
  2091. (const :tag "Always" t)
  2092. (repeat :tag "Specific tags" (string :tag "Tag"))
  2093. (regexp :tag "Tags matched by regexp")))
  2094. (defcustom org-tags-exclude-from-inheritance nil
  2095. "List of tags that should never be inherited.
  2096. This is a way to exclude a few tags from inheritance. For way to do
  2097. the opposite, to actively allow inheritance for selected tags,
  2098. see the variable `org-use-tag-inheritance'."
  2099. :group 'org-tags
  2100. :type '(repeat (string :tag "Tag")))
  2101. (defun org-tag-inherit-p (tag)
  2102. "Check if TAG is one that should be inherited."
  2103. (cond
  2104. ((member tag org-tags-exclude-from-inheritance) nil)
  2105. ((eq org-use-tag-inheritance t) t)
  2106. ((not org-use-tag-inheritance) nil)
  2107. ((stringp org-use-tag-inheritance)
  2108. (string-match org-use-tag-inheritance tag))
  2109. ((listp org-use-tag-inheritance)
  2110. (member tag org-use-tag-inheritance))
  2111. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2112. (defcustom org-tags-match-list-sublevels t
  2113. "Non-nil means list also sublevels of headlines matching a search.
  2114. This variable applies to tags/property searches, and also to stuck
  2115. projects because this search is based on a tags match as well.
  2116. When set to the symbol `indented', sublevels are indented with
  2117. leading dots.
  2118. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2119. the sublevels of a headline matching a tag search often also match
  2120. the same search. Listing all of them can create very long lists.
  2121. Setting this variable to nil causes subtrees of a match to be skipped.
  2122. This variable is semi-obsolete and probably should always be true. It
  2123. is better to limit inheritance to certain tags using the variables
  2124. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2125. :group 'org-tags
  2126. :type '(choice
  2127. (const :tag "No, don't list them" nil)
  2128. (const :tag "Yes, do list them" t)
  2129. (const :tag "List them, indented with leading dots" indented)))
  2130. (defcustom org-tags-sort-function nil
  2131. "When set, tags are sorted using this function as a comparator"
  2132. :group 'org-tags
  2133. :type '(choice
  2134. (const :tag "No sorting" nil)
  2135. (const :tag "Alphabetical" string<)
  2136. (const :tag "Reverse alphabetical" string>)
  2137. (function :tag "Custom function" nil)))
  2138. (defvar org-tags-history nil
  2139. "History of minibuffer reads for tags.")
  2140. (defvar org-last-tags-completion-table nil
  2141. "The last used completion table for tags.")
  2142. (defvar org-after-tags-change-hook nil
  2143. "Hook that is run after the tags in a line have changed.")
  2144. (defgroup org-properties nil
  2145. "Options concerning properties in Org-mode."
  2146. :tag "Org Properties"
  2147. :group 'org)
  2148. (defcustom org-property-format "%-10s %s"
  2149. "How property key/value pairs should be formatted by `indent-line'.
  2150. When `indent-line' hits a property definition, it will format the line
  2151. according to this format, mainly to make sure that the values are
  2152. lined-up with respect to each other."
  2153. :group 'org-properties
  2154. :type 'string)
  2155. (defcustom org-use-property-inheritance nil
  2156. "Non-nil means, properties apply also for sublevels.
  2157. This setting is chiefly used during property searches. Turning it on can
  2158. cause significant overhead when doing a search, which is why it is not
  2159. on by default.
  2160. When nil, only the properties directly given in the current entry count.
  2161. When t, every property is inherited. The value may also be a list of
  2162. properties that should have inheritance, or a regular expression matching
  2163. properties that should be inherited.
  2164. However, note that some special properties use inheritance under special
  2165. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2166. and the properties ending in \"_ALL\" when they are used as descriptor
  2167. for valid values of a property.
  2168. Note for programmers:
  2169. When querying an entry with `org-entry-get', you can control if inheritance
  2170. should be used. By default, `org-entry-get' looks only at the local
  2171. properties. You can request inheritance by setting the inherit argument
  2172. to t (to force inheritance) or to `selective' (to respect the setting
  2173. in this variable)."
  2174. :group 'org-properties
  2175. :type '(choice
  2176. (const :tag "Not" nil)
  2177. (const :tag "Always" t)
  2178. (repeat :tag "Specific properties" (string :tag "Property"))
  2179. (regexp :tag "Properties matched by regexp")))
  2180. (defun org-property-inherit-p (property)
  2181. "Check if PROPERTY is one that should be inherited."
  2182. (cond
  2183. ((eq org-use-property-inheritance t) t)
  2184. ((not org-use-property-inheritance) nil)
  2185. ((stringp org-use-property-inheritance)
  2186. (string-match org-use-property-inheritance property))
  2187. ((listp org-use-property-inheritance)
  2188. (member property org-use-property-inheritance))
  2189. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2190. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2191. "The default column format, if no other format has been defined.
  2192. This variable can be set on the per-file basis by inserting a line
  2193. #+COLUMNS: %25ITEM ....."
  2194. :group 'org-properties
  2195. :type 'string)
  2196. (defcustom org-columns-ellipses ".."
  2197. "The ellipses to be used when a field in column view is truncated.
  2198. When this is the empty string, as many characters as possible are shown,
  2199. but then there will be no visual indication that the field has been truncated.
  2200. When this is a string of length N, the last N characters of a truncated
  2201. field are replaced by this string. If the column is narrower than the
  2202. ellipses string, only part of the ellipses string will be shown."
  2203. :group 'org-properties
  2204. :type 'string)
  2205. (defcustom org-columns-modify-value-for-display-function nil
  2206. "Function that modifies values for display in column view.
  2207. For example, it can be used to cut out a certain part from a time stamp.
  2208. The function must take 2 arguments:
  2209. column-title The title of the column (*not* the property name)
  2210. value The value that should be modified.
  2211. The function should return the value that should be displayed,
  2212. or nil if the normal value should be used."
  2213. :group 'org-properties
  2214. :type 'function)
  2215. (defcustom org-effort-property "Effort"
  2216. "The property that is being used to keep track of effort estimates.
  2217. Effort estimates given in this property need to have the format H:MM."
  2218. :group 'org-properties
  2219. :group 'org-progress
  2220. :type '(string :tag "Property"))
  2221. (defconst org-global-properties-fixed
  2222. '(("VISIBILITY_ALL" . "folded children content all")
  2223. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2224. "List of property/value pairs that can be inherited by any entry.
  2225. These are fixed values, for the preset properties. The user variable
  2226. that can be used to add to this list is `org-global-properties'.
  2227. The entries in this list are cons cells where the car is a property
  2228. name and cdr is a string with the value. If the value represents
  2229. multiple items like an \"_ALL\" property, separate the items by
  2230. spaces.")
  2231. (defcustom org-global-properties nil
  2232. "List of property/value pairs that can be inherited by any entry.
  2233. This list will be combined with the constant `org-global-properties-fixed'.
  2234. The entries in this list are cons cells where the car is a property
  2235. name and cdr is a string with the value.
  2236. You can set buffer-local values for the same purpose in the variable
  2237. `org-file-properties' this by adding lines like
  2238. #+PROPERTY: NAME VALUE"
  2239. :group 'org-properties
  2240. :type '(repeat
  2241. (cons (string :tag "Property")
  2242. (string :tag "Value"))))
  2243. (defvar org-file-properties nil
  2244. "List of property/value pairs that can be inherited by any entry.
  2245. Valid for the current buffer.
  2246. This variable is populated from #+PROPERTY lines.")
  2247. (make-variable-buffer-local 'org-file-properties)
  2248. (defgroup org-agenda nil
  2249. "Options concerning agenda views in Org-mode."
  2250. :tag "Org Agenda"
  2251. :group 'org)
  2252. (defvar org-category nil
  2253. "Variable used by org files to set a category for agenda display.
  2254. Such files should use a file variable to set it, for example
  2255. # -*- mode: org; org-category: \"ELisp\"
  2256. or contain a special line
  2257. #+CATEGORY: ELisp
  2258. If the file does not specify a category, then file's base name
  2259. is used instead.")
  2260. (make-variable-buffer-local 'org-category)
  2261. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2262. (defcustom org-agenda-files nil
  2263. "The files to be used for agenda display.
  2264. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2265. \\[org-remove-file]. You can also use customize to edit the list.
  2266. If an entry is a directory, all files in that directory that are matched by
  2267. `org-agenda-file-regexp' will be part of the file list.
  2268. If the value of the variable is not a list but a single file name, then
  2269. the list of agenda files is actually stored and maintained in that file, one
  2270. agenda file per line."
  2271. :group 'org-agenda
  2272. :type '(choice
  2273. (repeat :tag "List of files and directories" file)
  2274. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2275. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2276. "Regular expression to match files for `org-agenda-files'.
  2277. If any element in the list in that variable contains a directory instead
  2278. of a normal file, all files in that directory that are matched by this
  2279. regular expression will be included."
  2280. :group 'org-agenda
  2281. :type 'regexp)
  2282. (defcustom org-agenda-text-search-extra-files nil
  2283. "List of extra files to be searched by text search commands.
  2284. These files will be search in addition to the agenda files by the
  2285. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2286. Note that these files will only be searched for text search commands,
  2287. not for the other agenda views like todo lists, tag searches or the weekly
  2288. agenda. This variable is intended to list notes and possibly archive files
  2289. that should also be searched by these two commands.
  2290. In fact, if the first element in the list is the symbol `agenda-archives',
  2291. than all archive files of all agenda files will be added to the search
  2292. scope."
  2293. :group 'org-agenda
  2294. :type '(set :greedy t
  2295. (const :tag "Agenda Archives" agenda-archives)
  2296. (repeat :inline t (file))))
  2297. (if (fboundp 'defvaralias)
  2298. (defvaralias 'org-agenda-multi-occur-extra-files
  2299. 'org-agenda-text-search-extra-files))
  2300. (defcustom org-agenda-skip-unavailable-files nil
  2301. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2302. A nil value means to remove them, after a query, from the list."
  2303. :group 'org-agenda
  2304. :type 'boolean)
  2305. (defcustom org-calendar-to-agenda-key [?c]
  2306. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2307. The command `org-calendar-goto-agenda' will be bound to this key. The
  2308. default is the character `c' because then `c' can be used to switch back and
  2309. forth between agenda and calendar."
  2310. :group 'org-agenda
  2311. :type 'sexp)
  2312. (defcustom org-calendar-agenda-action-key [?k]
  2313. "The key to be installed in `calendar-mode-map' for agenda-action.
  2314. The command `org-agenda-action' will be bound to this key. The
  2315. default is the character `k' because we use the same key in the agenda."
  2316. :group 'org-agenda
  2317. :type 'sexp)
  2318. (eval-after-load "calendar"
  2319. '(progn
  2320. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2321. 'org-calendar-goto-agenda)
  2322. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2323. 'org-agenda-action)))
  2324. (defgroup org-latex nil
  2325. "Options for embedding LaTeX code into Org-mode."
  2326. :tag "Org LaTeX"
  2327. :group 'org)
  2328. (defcustom org-format-latex-options
  2329. '(:foreground default :background default :scale 1.0
  2330. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2331. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2332. "Options for creating images from LaTeX fragments.
  2333. This is a property list with the following properties:
  2334. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2335. `default' means use the foreground of the default face.
  2336. :background the background color, or \"Transparent\".
  2337. `default' means use the background of the default face.
  2338. :scale a scaling factor for the size of the images.
  2339. :html-foreground, :html-background, :html-scale
  2340. the same numbers for HTML export.
  2341. :matchers a list indicating which matchers should be used to
  2342. find LaTeX fragments. Valid members of this list are:
  2343. \"begin\" find environments
  2344. \"$1\" find single characters surrounded by $.$
  2345. \"$\" find math expressions surrounded by $...$
  2346. \"$$\" find math expressions surrounded by $$....$$
  2347. \"\\(\" find math expressions surrounded by \\(...\\)
  2348. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2349. :group 'org-latex
  2350. :type 'plist)
  2351. (defcustom org-format-latex-header "\\documentclass{article}
  2352. \\usepackage{amssymb}
  2353. \\usepackage[usenames]{color}
  2354. \\usepackage{amsmath}
  2355. \\usepackage{latexsym}
  2356. \\usepackage[mathscr]{eucal}
  2357. \\pagestyle{empty} % do not remove
  2358. % The settings below are copied from fullpage.sty
  2359. \\setlength{\\textwidth}{\\paperwidth}
  2360. \\addtolength{\\textwidth}{-3cm}
  2361. \\setlength{\\oddsidemargin}{1.5cm}
  2362. \\addtolength{\\oddsidemargin}{-2.54cm}
  2363. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2364. \\setlength{\\textheight}{\\paperheight}
  2365. \\addtolength{\\textheight}{-\\headheight}
  2366. \\addtolength{\\textheight}{-\\headsep}
  2367. \\addtolength{\\textheight}{-\\footskip}
  2368. \\addtolength{\\textheight}{-3cm}
  2369. \\setlength{\\topmargin}{1.5cm}
  2370. \\addtolength{\\topmargin}{-2.54cm}"
  2371. "The document header used for processing LaTeX fragments.
  2372. It is imperative that this header make sure that no page number
  2373. appears on the page."
  2374. :group 'org-latex
  2375. :type 'string)
  2376. (defgroup org-font-lock nil
  2377. "Font-lock settings for highlighting in Org-mode."
  2378. :tag "Org Font Lock"
  2379. :group 'org)
  2380. (defcustom org-level-color-stars-only nil
  2381. "Non-nil means fontify only the stars in each headline.
  2382. When nil, the entire headline is fontified.
  2383. Changing it requires restart of `font-lock-mode' to become effective
  2384. also in regions already fontified."
  2385. :group 'org-font-lock
  2386. :type 'boolean)
  2387. (defcustom org-hide-leading-stars nil
  2388. "Non-nil means, hide the first N-1 stars in a headline.
  2389. This works by using the face `org-hide' for these stars. This
  2390. face is white for a light background, and black for a dark
  2391. background. You may have to customize the face `org-hide' to
  2392. make this work.
  2393. Changing it requires restart of `font-lock-mode' to become effective
  2394. also in regions already fontified.
  2395. You may also set this on a per-file basis by adding one of the following
  2396. lines to the buffer:
  2397. #+STARTUP: hidestars
  2398. #+STARTUP: showstars"
  2399. :group 'org-font-lock
  2400. :type 'boolean)
  2401. (defcustom org-fontify-done-headline nil
  2402. "Non-nil means, change the face of a headline if it is marked DONE.
  2403. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2404. When this is non-nil, the headline after the keyword is set to the
  2405. `org-headline-done' as an additional indication."
  2406. :group 'org-font-lock
  2407. :type 'boolean)
  2408. (defcustom org-fontify-emphasized-text t
  2409. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2410. Changing this variable requires a restart of Emacs to take effect."
  2411. :group 'org-font-lock
  2412. :type 'boolean)
  2413. (defcustom org-fontify-whole-heading-line nil
  2414. "Non-nil means fontify the whole line for headings.
  2415. This is useful when setting a background color for the
  2416. org-leve-* faces."
  2417. :group 'org-font-lock
  2418. :type 'boolean)
  2419. (defcustom org-highlight-latex-fragments-and-specials nil
  2420. "Non-nil means, fontify what is treated specially by the exporters."
  2421. :group 'org-font-lock
  2422. :type 'boolean)
  2423. (defcustom org-hide-emphasis-markers nil
  2424. "Non-nil mean font-lock should hide the emphasis marker characters."
  2425. :group 'org-font-lock
  2426. :type 'boolean)
  2427. (defvar org-emph-re nil
  2428. "Regular expression for matching emphasis.")
  2429. (defvar org-verbatim-re nil
  2430. "Regular expression for matching verbatim text.")
  2431. (defvar org-emphasis-regexp-components) ; defined just below
  2432. (defvar org-emphasis-alist) ; defined just below
  2433. (defun org-set-emph-re (var val)
  2434. "Set variable and compute the emphasis regular expression."
  2435. (set var val)
  2436. (when (and (boundp 'org-emphasis-alist)
  2437. (boundp 'org-emphasis-regexp-components)
  2438. org-emphasis-alist org-emphasis-regexp-components)
  2439. (let* ((e org-emphasis-regexp-components)
  2440. (pre (car e))
  2441. (post (nth 1 e))
  2442. (border (nth 2 e))
  2443. (body (nth 3 e))
  2444. (nl (nth 4 e))
  2445. (body1 (concat body "*?"))
  2446. (markers (mapconcat 'car org-emphasis-alist ""))
  2447. (vmarkers (mapconcat
  2448. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2449. org-emphasis-alist "")))
  2450. ;; make sure special characters appear at the right position in the class
  2451. (if (string-match "\\^" markers)
  2452. (setq markers (concat (replace-match "" t t markers) "^")))
  2453. (if (string-match "-" markers)
  2454. (setq markers (concat (replace-match "" t t markers) "-")))
  2455. (if (string-match "\\^" vmarkers)
  2456. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2457. (if (string-match "-" vmarkers)
  2458. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2459. (if (> nl 0)
  2460. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2461. (int-to-string nl) "\\}")))
  2462. ;; Make the regexp
  2463. (setq org-emph-re
  2464. (concat "\\([" pre "]\\|^\\)"
  2465. "\\("
  2466. "\\([" markers "]\\)"
  2467. "\\("
  2468. "[^" border "]\\|"
  2469. "[^" border "]"
  2470. body1
  2471. "[^" border "]"
  2472. "\\)"
  2473. "\\3\\)"
  2474. "\\([" post "]\\|$\\)"))
  2475. (setq org-verbatim-re
  2476. (concat "\\([" pre "]\\|^\\)"
  2477. "\\("
  2478. "\\([" vmarkers "]\\)"
  2479. "\\("
  2480. "[^" border "]\\|"
  2481. "[^" border "]"
  2482. body1
  2483. "[^" border "]"
  2484. "\\)"
  2485. "\\3\\)"
  2486. "\\([" post "]\\|$\\)")))))
  2487. (defcustom org-emphasis-regexp-components
  2488. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2489. "Components used to build the regular expression for emphasis.
  2490. This is a list with 6 entries. Terminology: In an emphasis string
  2491. like \" *strong word* \", we call the initial space PREMATCH, the final
  2492. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2493. and \"trong wor\" is the body. The different components in this variable
  2494. specify what is allowed/forbidden in each part:
  2495. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2496. post Chars allowed as postmatch. End of line will be allowed too.
  2497. border The chars *forbidden* as border characters.
  2498. body-regexp A regexp like \".\" to match a body character. Don't use
  2499. non-shy groups here, and don't allow newline here.
  2500. newline The maximum number of newlines allowed in an emphasis exp.
  2501. Use customize to modify this, or restart Emacs after changing it."
  2502. :group 'org-font-lock
  2503. :set 'org-set-emph-re
  2504. :type '(list
  2505. (sexp :tag "Allowed chars in pre ")
  2506. (sexp :tag "Allowed chars in post ")
  2507. (sexp :tag "Forbidden chars in border ")
  2508. (sexp :tag "Regexp for body ")
  2509. (integer :tag "number of newlines allowed")
  2510. (option (boolean :tag "Please ignore this button"))))
  2511. (defcustom org-emphasis-alist
  2512. `(("*" bold "<b>" "</b>")
  2513. ("/" italic "<i>" "</i>")
  2514. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2515. ("=" org-code "<code>" "</code>" verbatim)
  2516. ("~" org-verbatim "<code>" "</code>" verbatim)
  2517. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2518. "<del>" "</del>")
  2519. )
  2520. "Special syntax for emphasized text.
  2521. Text starting and ending with a special character will be emphasized, for
  2522. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2523. characters, the face to be used by font-lock for highlighting in Org-mode
  2524. Emacs buffers, and the HTML tags to be used for this.
  2525. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2526. Use customize to modify this, or restart Emacs after changing it."
  2527. :group 'org-font-lock
  2528. :set 'org-set-emph-re
  2529. :type '(repeat
  2530. (list
  2531. (string :tag "Marker character")
  2532. (choice
  2533. (face :tag "Font-lock-face")
  2534. (plist :tag "Face property list"))
  2535. (string :tag "HTML start tag")
  2536. (string :tag "HTML end tag")
  2537. (option (const verbatim)))))
  2538. (defvar org-protecting-blocks
  2539. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2540. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2541. This is needed for font-lock setup.")
  2542. ;;; Miscellaneous options
  2543. (defgroup org-completion nil
  2544. "Completion in Org-mode."
  2545. :tag "Org Completion"
  2546. :group 'org)
  2547. (defcustom org-completion-use-ido nil
  2548. "Non-nil means, use ido completion wherever possible.
  2549. Note that `ido-mode' must be active for this variable to be relevant.
  2550. If you decide to turn this variable on, you might well want to turn off
  2551. `org-outline-path-complete-in-steps'.
  2552. See also `org-completion-use-iswitchb'."
  2553. :group 'org-completion
  2554. :type 'boolean)
  2555. (defcustom org-completion-use-iswitchb nil
  2556. "Non-nil means, use iswitchb completion wherever possible.
  2557. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2558. If you decide to turn this variable on, you might well want to turn off
  2559. `org-outline-path-complete-in-steps'.
  2560. Note that thi variable has only an effect if `org-completion-use-ido' is nil."
  2561. :group 'org-completion
  2562. :type 'boolean)
  2563. (defcustom org-completion-fallback-command 'hippie-expand
  2564. "The expansion command called by \\[org-complete] in normal context.
  2565. Normal means, no org-mode-specific context."
  2566. :group 'org-completion
  2567. :type 'function)
  2568. ;;; Functions and variables from ther packages
  2569. ;; Declared here to avoid compiler warnings
  2570. ;; XEmacs only
  2571. (defvar outline-mode-menu-heading)
  2572. (defvar outline-mode-menu-show)
  2573. (defvar outline-mode-menu-hide)
  2574. (defvar zmacs-regions) ; XEmacs regions
  2575. ;; Emacs only
  2576. (defvar mark-active)
  2577. ;; Various packages
  2578. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2579. (declare-function calendar-forward-day "cal-move" (arg))
  2580. (declare-function calendar-goto-date "cal-move" (date))
  2581. (declare-function calendar-goto-today "cal-move" ())
  2582. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2583. (defvar calc-embedded-close-formula)
  2584. (defvar calc-embedded-open-formula)
  2585. (declare-function cdlatex-tab "ext:cdlatex" ())
  2586. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2587. (defvar font-lock-unfontify-region-function)
  2588. (declare-function iswitchb-read-buffer "iswitchb"
  2589. (prompt &optional default require-match start matches-set))
  2590. (defvar iswitchb-temp-buflist)
  2591. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2592. (defvar org-agenda-tags-todo-honor-ignore-options)
  2593. (declare-function org-agenda-skip "org-agenda" ())
  2594. (declare-function org-format-agenda-item "org-agenda"
  2595. (extra txt &optional category tags dotime noprefix remove-re))
  2596. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2597. (declare-function org-agenda-change-all-lines "org-agenda"
  2598. (newhead hdmarker &optional fixface just-this))
  2599. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2600. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2601. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2602. (beg end))
  2603. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2604. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2605. "org-agenda" (&optional end))
  2606. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2607. (declare-function org-indent-mode "org-indent" (&optional arg))
  2608. (declare-function parse-time-string "parse-time" (string))
  2609. (defvar remember-data-file)
  2610. (defvar texmathp-why)
  2611. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2612. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2613. (defvar w3m-current-url)
  2614. (defvar w3m-current-title)
  2615. (defvar org-latex-regexps)
  2616. ;;; Autoload and prepare some org modules
  2617. ;; Some table stuff that needs to be defined here, because it is used
  2618. ;; by the functions setting up org-mode or checking for table context.
  2619. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2620. "Detects an org-type or table-type table.")
  2621. (defconst org-table-line-regexp "^[ \t]*|"
  2622. "Detects an org-type table line.")
  2623. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2624. "Detects an org-type table line.")
  2625. (defconst org-table-hline-regexp "^[ \t]*|-"
  2626. "Detects an org-type table hline.")
  2627. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2628. "Detects a table-type table hline.")
  2629. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2630. "Searching from within a table (any type) this finds the first line
  2631. outside the table.")
  2632. ;; Autoload the functions in org-table.el that are needed by functions here.
  2633. (eval-and-compile
  2634. (org-autoload "org-table"
  2635. '(org-table-align org-table-begin org-table-blank-field
  2636. org-table-convert org-table-convert-region org-table-copy-down
  2637. org-table-copy-region org-table-create
  2638. org-table-create-or-convert-from-region
  2639. org-table-create-with-table.el org-table-current-dline
  2640. org-table-cut-region org-table-delete-column org-table-edit-field
  2641. org-table-edit-formulas org-table-end org-table-eval-formula
  2642. org-table-export org-table-field-info
  2643. org-table-get-stored-formulas org-table-goto-column
  2644. org-table-hline-and-move org-table-import org-table-insert-column
  2645. org-table-insert-hline org-table-insert-row org-table-iterate
  2646. org-table-justify-field-maybe org-table-kill-row
  2647. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2648. org-table-move-column org-table-move-column-left
  2649. org-table-move-column-right org-table-move-row
  2650. org-table-move-row-down org-table-move-row-up
  2651. org-table-next-field org-table-next-row org-table-paste-rectangle
  2652. org-table-previous-field org-table-recalculate
  2653. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2654. org-table-toggle-coordinate-overlays
  2655. org-table-toggle-formula-debugger org-table-wrap-region
  2656. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2657. (defun org-at-table-p (&optional table-type)
  2658. "Return t if the cursor is inside an org-type table.
  2659. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2660. (if org-enable-table-editor
  2661. (save-excursion
  2662. (beginning-of-line 1)
  2663. (looking-at (if table-type org-table-any-line-regexp
  2664. org-table-line-regexp)))
  2665. nil))
  2666. (defsubst org-table-p () (org-at-table-p))
  2667. (defun org-at-table.el-p ()
  2668. "Return t if and only if we are at a table.el table."
  2669. (and (org-at-table-p 'any)
  2670. (save-excursion
  2671. (goto-char (org-table-begin 'any))
  2672. (looking-at org-table1-hline-regexp))))
  2673. (defun org-table-recognize-table.el ()
  2674. "If there is a table.el table nearby, recognize it and move into it."
  2675. (if org-table-tab-recognizes-table.el
  2676. (if (org-at-table.el-p)
  2677. (progn
  2678. (beginning-of-line 1)
  2679. (if (looking-at org-table-dataline-regexp)
  2680. nil
  2681. (if (looking-at org-table1-hline-regexp)
  2682. (progn
  2683. (beginning-of-line 2)
  2684. (if (looking-at org-table-any-border-regexp)
  2685. (beginning-of-line -1)))))
  2686. (if (re-search-forward "|" (org-table-end t) t)
  2687. (progn
  2688. (require 'table)
  2689. (if (table--at-cell-p (point))
  2690. t
  2691. (message "recognizing table.el table...")
  2692. (table-recognize-table)
  2693. (message "recognizing table.el table...done")))
  2694. (error "This should not happen..."))
  2695. t)
  2696. nil)
  2697. nil))
  2698. (defun org-at-table-hline-p ()
  2699. "Return t if the cursor is inside a hline in a table."
  2700. (if org-enable-table-editor
  2701. (save-excursion
  2702. (beginning-of-line 1)
  2703. (looking-at org-table-hline-regexp))
  2704. nil))
  2705. (defvar org-table-clean-did-remove-column nil)
  2706. (defun org-table-map-tables (function)
  2707. "Apply FUNCTION to the start of all tables in the buffer."
  2708. (save-excursion
  2709. (save-restriction
  2710. (widen)
  2711. (goto-char (point-min))
  2712. (while (re-search-forward org-table-any-line-regexp nil t)
  2713. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2714. (beginning-of-line 1)
  2715. (when (looking-at org-table-line-regexp)
  2716. (save-excursion (funcall function))
  2717. (or (looking-at org-table-line-regexp)
  2718. (forward-char 1)))
  2719. (re-search-forward org-table-any-border-regexp nil 1))))
  2720. (message "Mapping tables: done"))
  2721. ;; Declare and autoload functions from org-exp.el & Co
  2722. (declare-function org-default-export-plist "org-exp")
  2723. (declare-function org-infile-export-plist "org-exp")
  2724. (declare-function org-get-current-options "org-exp")
  2725. (eval-and-compile
  2726. (org-autoload "org-exp"
  2727. '(org-export org-export-visible
  2728. org-insert-export-options-template
  2729. org-table-clean-before-export))
  2730. (org-autoload "org-ascii"
  2731. '(org-export-as-ascii org-export-ascii-preprocess
  2732. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2733. org-export-region-as-ascii))
  2734. (org-autoload "org-html"
  2735. '(org-export-as-html-and-open
  2736. org-export-as-html-batch org-export-as-html-to-buffer
  2737. org-replace-region-by-html org-export-region-as-html
  2738. org-export-as-html))
  2739. (org-autoload "org-icalendar"
  2740. '(org-export-icalendar-this-file
  2741. org-export-icalendar-all-agenda-files
  2742. org-export-icalendar-combine-agenda-files))
  2743. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2744. ;; Declare and autoload functions from org-agenda.el
  2745. (eval-and-compile
  2746. (org-autoload "org-agenda"
  2747. '(org-agenda org-agenda-list org-search-view
  2748. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2749. org-diary org-agenda-to-appt
  2750. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2751. ;; Autoload org-remember
  2752. (eval-and-compile
  2753. (org-autoload "org-remember"
  2754. '(org-remember-insinuate org-remember-annotation
  2755. org-remember-apply-template org-remember org-remember-handler)))
  2756. ;; Autoload org-clock.el
  2757. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2758. (beg end))
  2759. (declare-function org-clock-update-mode-line "org-clock" ())
  2760. (declare-function org-resolve-clocks "org-clock"
  2761. (&optional also-non-dangling-p prompt last-valid))
  2762. (defvar org-clock-start-time)
  2763. (defvar org-clock-marker (make-marker)
  2764. "Marker recording the last clock-in.")
  2765. (defvar org-clock-hd-marker (make-marker)
  2766. "Marker recording the last clock-in, but the headline position.")
  2767. (defun org-clock-is-active ()
  2768. "Return non-nil if clock is currently running.
  2769. The return value is actually the clock marker."
  2770. (marker-buffer org-clock-marker))
  2771. (eval-and-compile
  2772. (org-autoload
  2773. "org-clock"
  2774. '(org-clock-in org-clock-out org-clock-cancel
  2775. org-clock-goto org-clock-sum org-clock-display
  2776. org-clock-remove-overlays org-clock-report
  2777. org-clocktable-shift org-dblock-write:clocktable
  2778. org-get-clocktable org-resolve-clocks)))
  2779. (defun org-clock-update-time-maybe ()
  2780. "If this is a CLOCK line, update it and return t.
  2781. Otherwise, return nil."
  2782. (interactive)
  2783. (save-excursion
  2784. (beginning-of-line 1)
  2785. (skip-chars-forward " \t")
  2786. (when (looking-at org-clock-string)
  2787. (let ((re (concat "[ \t]*" org-clock-string
  2788. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2789. "\\([ \t]*=>.*\\)?\\)?"))
  2790. ts te h m s neg)
  2791. (cond
  2792. ((not (looking-at re))
  2793. nil)
  2794. ((not (match-end 2))
  2795. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2796. (> org-clock-marker (point))
  2797. (<= org-clock-marker (point-at-eol)))
  2798. ;; The clock is running here
  2799. (setq org-clock-start-time
  2800. (apply 'encode-time
  2801. (org-parse-time-string (match-string 1))))
  2802. (org-clock-update-mode-line)))
  2803. (t
  2804. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2805. (end-of-line 1)
  2806. (setq ts (match-string 1)
  2807. te (match-string 3))
  2808. (setq s (- (org-float-time
  2809. (apply 'encode-time (org-parse-time-string te)))
  2810. (org-float-time
  2811. (apply 'encode-time (org-parse-time-string ts))))
  2812. neg (< s 0)
  2813. s (abs s)
  2814. h (floor (/ s 3600))
  2815. s (- s (* 3600 h))
  2816. m (floor (/ s 60))
  2817. s (- s (* 60 s)))
  2818. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2819. t))))))
  2820. (defun org-check-running-clock ()
  2821. "Check if the current buffer contains the running clock.
  2822. If yes, offer to stop it and to save the buffer with the changes."
  2823. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2824. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2825. (buffer-name))))
  2826. (org-clock-out)
  2827. (when (y-or-n-p "Save changed buffer?")
  2828. (save-buffer))))
  2829. (defun org-clocktable-try-shift (dir n)
  2830. "Check if this line starts a clock table, if yes, shift the time block."
  2831. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2832. (org-clocktable-shift dir n)))
  2833. ;; Autoload org-timer.el
  2834. (eval-and-compile
  2835. (org-autoload
  2836. "org-timer"
  2837. '(org-timer-start org-timer org-timer-item
  2838. org-timer-change-times-in-region
  2839. org-timer-set-timer
  2840. org-timer-reset-timers
  2841. org-timer-show-remaining-time)))
  2842. ;; Autoload org-feed.el
  2843. (eval-and-compile
  2844. (org-autoload
  2845. "org-feed"
  2846. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2847. ;; Autoload org-indent.el
  2848. (eval-and-compile
  2849. (org-autoload
  2850. "org-indent"
  2851. '(org-indent-mode)))
  2852. ;; Autoload org-mobile.el
  2853. (eval-and-compile
  2854. (org-autoload
  2855. "org-mobile"
  2856. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  2857. ;; Autoload archiving code
  2858. ;; The stuff that is needed for cycling and tags has to be defined here.
  2859. (defgroup org-archive nil
  2860. "Options concerning archiving in Org-mode."
  2861. :tag "Org Archive"
  2862. :group 'org-structure)
  2863. (defcustom org-archive-location "%s_archive::"
  2864. "The location where subtrees should be archived.
  2865. The value of this variable is a string, consisting of two parts,
  2866. separated by a double-colon. The first part is a filename and
  2867. the second part is a headline.
  2868. When the filename is omitted, archiving happens in the same file.
  2869. %s in the filename will be replaced by the current file
  2870. name (without the directory part). Archiving to a different file
  2871. is useful to keep archived entries from contributing to the
  2872. Org-mode Agenda.
  2873. The archived entries will be filed as subtrees of the specified
  2874. headline. When the headline is omitted, the subtrees are simply
  2875. filed away at the end of the file, as top-level entries. Also in
  2876. the heading you can use %s to represent the file name, this can be
  2877. useful when using the same archive for a number of different files.
  2878. Here are a few examples:
  2879. \"%s_archive::\"
  2880. If the current file is Projects.org, archive in file
  2881. Projects.org_archive, as top-level trees. This is the default.
  2882. \"::* Archived Tasks\"
  2883. Archive in the current file, under the top-level headline
  2884. \"* Archived Tasks\".
  2885. \"~/org/archive.org::\"
  2886. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2887. \"~/org/archive.org::From %s\"
  2888. Archive in file ~/org/archive.org (absolute path), und headlines
  2889. \"From FILENAME\" where file name is the current file name.
  2890. \"basement::** Finished Tasks\"
  2891. Archive in file ./basement (relative path), as level 3 trees
  2892. below the level 2 heading \"** Finished Tasks\".
  2893. You may set this option on a per-file basis by adding to the buffer a
  2894. line like
  2895. #+ARCHIVE: basement::** Finished Tasks
  2896. You may also define it locally for a subtree by setting an ARCHIVE property
  2897. in the entry. If such a property is found in an entry, or anywhere up
  2898. the hierarchy, it will be used."
  2899. :group 'org-archive
  2900. :type 'string)
  2901. (defcustom org-archive-tag "ARCHIVE"
  2902. "The tag that marks a subtree as archived.
  2903. An archived subtree does not open during visibility cycling, and does
  2904. not contribute to the agenda listings.
  2905. After changing this, font-lock must be restarted in the relevant buffers to
  2906. get the proper fontification."
  2907. :group 'org-archive
  2908. :group 'org-keywords
  2909. :type 'string)
  2910. (defcustom org-agenda-skip-archived-trees t
  2911. "Non-nil means, the agenda will skip any items located in archived trees.
  2912. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2913. variable is no longer recommended, you should leave it at the value t.
  2914. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2915. :group 'org-archive
  2916. :group 'org-agenda-skip
  2917. :type 'boolean)
  2918. (defcustom org-columns-skip-arrchived-trees t
  2919. "Non-nil means, irgnore archived trees when creating column view."
  2920. :group 'org-archive
  2921. :group 'org-properties
  2922. :type 'boolean)
  2923. (defcustom org-cycle-open-archived-trees nil
  2924. "Non-nil means, `org-cycle' will open archived trees.
  2925. An archived tree is a tree marked with the tag ARCHIVE.
  2926. When nil, archived trees will stay folded. You can still open them with
  2927. normal outline commands like `show-all', but not with the cycling commands."
  2928. :group 'org-archive
  2929. :group 'org-cycle
  2930. :type 'boolean)
  2931. (defcustom org-sparse-tree-open-archived-trees nil
  2932. "Non-nil means sparse tree construction shows matches in archived trees.
  2933. When nil, matches in these trees are highlighted, but the trees are kept in
  2934. collapsed state."
  2935. :group 'org-archive
  2936. :group 'org-sparse-trees
  2937. :type 'boolean)
  2938. (defun org-cycle-hide-archived-subtrees (state)
  2939. "Re-hide all archived subtrees after a visibility state change."
  2940. (when (and (not org-cycle-open-archived-trees)
  2941. (not (memq state '(overview folded))))
  2942. (save-excursion
  2943. (let* ((globalp (memq state '(contents all)))
  2944. (beg (if globalp (point-min) (point)))
  2945. (end (if globalp (point-max) (org-end-of-subtree t))))
  2946. (org-hide-archived-subtrees beg end)
  2947. (goto-char beg)
  2948. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2949. (message "%s" (substitute-command-keys
  2950. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2951. (defun org-force-cycle-archived ()
  2952. "Cycle subtree even if it is archived."
  2953. (interactive)
  2954. (setq this-command 'org-cycle)
  2955. (let ((org-cycle-open-archived-trees t))
  2956. (call-interactively 'org-cycle)))
  2957. (defun org-hide-archived-subtrees (beg end)
  2958. "Re-hide all archived subtrees after a visibility state change."
  2959. (save-excursion
  2960. (let* ((re (concat ":" org-archive-tag ":")))
  2961. (goto-char beg)
  2962. (while (re-search-forward re end t)
  2963. (and (org-on-heading-p) (hide-subtree))
  2964. (org-end-of-subtree t)))))
  2965. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2966. (eval-and-compile
  2967. (org-autoload "org-archive"
  2968. '(org-add-archive-files org-archive-subtree
  2969. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2970. ;; Autoload Column View Code
  2971. (declare-function org-columns-number-to-string "org-colview")
  2972. (declare-function org-columns-get-format-and-top-level "org-colview")
  2973. (declare-function org-columns-compute "org-colview")
  2974. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2975. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2976. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2977. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2978. ;; Autoload ID code
  2979. (declare-function org-id-store-link "org-id")
  2980. (declare-function org-id-locations-load "org-id")
  2981. (declare-function org-id-locations-save "org-id")
  2982. (defvar org-id-track-globally)
  2983. (org-autoload "org-id"
  2984. '(org-id-get-create org-id-new org-id-copy org-id-get
  2985. org-id-get-with-outline-path-completion
  2986. org-id-get-with-outline-drilling
  2987. org-id-goto org-id-find org-id-store-link))
  2988. ;; Autoload Plotting Code
  2989. (org-autoload "org-plot"
  2990. '(org-plot/gnuplot))
  2991. ;;; Variables for pre-computed regular expressions, all buffer local
  2992. (defvar org-drawer-regexp nil
  2993. "Matches first line of a hidden block.")
  2994. (make-variable-buffer-local 'org-drawer-regexp)
  2995. (defvar org-todo-regexp nil
  2996. "Matches any of the TODO state keywords.")
  2997. (make-variable-buffer-local 'org-todo-regexp)
  2998. (defvar org-not-done-regexp nil
  2999. "Matches any of the TODO state keywords except the last one.")
  3000. (make-variable-buffer-local 'org-not-done-regexp)
  3001. (defvar org-not-done-heading-regexp nil
  3002. "Matches a TODO headline that is not done.")
  3003. (make-variable-buffer-local 'org-not-done-regexp)
  3004. (defvar org-todo-line-regexp nil
  3005. "Matches a headline and puts TODO state into group 2 if present.")
  3006. (make-variable-buffer-local 'org-todo-line-regexp)
  3007. (defvar org-complex-heading-regexp nil
  3008. "Matches a headline and puts everything into groups:
  3009. group 1: the stars
  3010. group 2: The todo keyword, maybe
  3011. group 3: Priority cookie
  3012. group 4: True headline
  3013. group 5: Tags")
  3014. (make-variable-buffer-local 'org-complex-heading-regexp)
  3015. (defvar org-complex-heading-regexp-format nil)
  3016. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3017. (defvar org-todo-line-tags-regexp nil
  3018. "Matches a headline and puts TODO state into group 2 if present.
  3019. Also put tags into group 4 if tags are present.")
  3020. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3021. (defvar org-nl-done-regexp nil
  3022. "Matches newline followed by a headline with the DONE keyword.")
  3023. (make-variable-buffer-local 'org-nl-done-regexp)
  3024. (defvar org-looking-at-done-regexp nil
  3025. "Matches the DONE keyword a point.")
  3026. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3027. (defvar org-ds-keyword-length 12
  3028. "Maximum length of the Deadline and SCHEDULED keywords.")
  3029. (make-variable-buffer-local 'org-ds-keyword-length)
  3030. (defvar org-deadline-regexp nil
  3031. "Matches the DEADLINE keyword.")
  3032. (make-variable-buffer-local 'org-deadline-regexp)
  3033. (defvar org-deadline-time-regexp nil
  3034. "Matches the DEADLINE keyword together with a time stamp.")
  3035. (make-variable-buffer-local 'org-deadline-time-regexp)
  3036. (defvar org-deadline-line-regexp nil
  3037. "Matches the DEADLINE keyword and the rest of the line.")
  3038. (make-variable-buffer-local 'org-deadline-line-regexp)
  3039. (defvar org-scheduled-regexp nil
  3040. "Matches the SCHEDULED keyword.")
  3041. (make-variable-buffer-local 'org-scheduled-regexp)
  3042. (defvar org-scheduled-time-regexp nil
  3043. "Matches the SCHEDULED keyword together with a time stamp.")
  3044. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3045. (defvar org-closed-time-regexp nil
  3046. "Matches the CLOSED keyword together with a time stamp.")
  3047. (make-variable-buffer-local 'org-closed-time-regexp)
  3048. (defvar org-keyword-time-regexp nil
  3049. "Matches any of the 4 keywords, together with the time stamp.")
  3050. (make-variable-buffer-local 'org-keyword-time-regexp)
  3051. (defvar org-keyword-time-not-clock-regexp nil
  3052. "Matches any of the 3 keywords, together with the time stamp.")
  3053. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3054. (defvar org-maybe-keyword-time-regexp nil
  3055. "Matches a timestamp, possibly preceeded by a keyword.")
  3056. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3057. (defvar org-planning-or-clock-line-re nil
  3058. "Matches a line with planning or clock info.")
  3059. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3060. (defconst org-plain-time-of-day-regexp
  3061. (concat
  3062. "\\(\\<[012]?[0-9]"
  3063. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3064. "\\(--?"
  3065. "\\(\\<[012]?[0-9]"
  3066. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3067. "\\)?")
  3068. "Regular expression to match a plain time or time range.
  3069. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3070. groups carry important information:
  3071. 0 the full match
  3072. 1 the first time, range or not
  3073. 8 the second time, if it is a range.")
  3074. (defconst org-plain-time-extension-regexp
  3075. (concat
  3076. "\\(\\<[012]?[0-9]"
  3077. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3078. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3079. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3080. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3081. groups carry important information:
  3082. 0 the full match
  3083. 7 hours of duration
  3084. 9 minutes of duration")
  3085. (defconst org-stamp-time-of-day-regexp
  3086. (concat
  3087. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3088. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3089. "\\(--?"
  3090. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3091. "Regular expression to match a timestamp time or time range.
  3092. After a match, the following groups carry important information:
  3093. 0 the full match
  3094. 1 date plus weekday, for backreferencing to make sure both times on same day
  3095. 2 the first time, range or not
  3096. 4 the second time, if it is a range.")
  3097. (defconst org-startup-options
  3098. '(("fold" org-startup-folded t)
  3099. ("overview" org-startup-folded t)
  3100. ("nofold" org-startup-folded nil)
  3101. ("showall" org-startup-folded nil)
  3102. ("showeverything" org-startup-folded showeverything)
  3103. ("content" org-startup-folded content)
  3104. ("indent" org-startup-indented t)
  3105. ("noindent" org-startup-indented nil)
  3106. ("hidestars" org-hide-leading-stars t)
  3107. ("showstars" org-hide-leading-stars nil)
  3108. ("odd" org-odd-levels-only t)
  3109. ("oddeven" org-odd-levels-only nil)
  3110. ("align" org-startup-align-all-tables t)
  3111. ("noalign" org-startup-align-all-tables nil)
  3112. ("customtime" org-display-custom-times t)
  3113. ("logdone" org-log-done time)
  3114. ("lognotedone" org-log-done note)
  3115. ("nologdone" org-log-done nil)
  3116. ("lognoteclock-out" org-log-note-clock-out t)
  3117. ("nolognoteclock-out" org-log-note-clock-out nil)
  3118. ("logrepeat" org-log-repeat state)
  3119. ("lognoterepeat" org-log-repeat note)
  3120. ("nologrepeat" org-log-repeat nil)
  3121. ("fninline" org-footnote-define-inline t)
  3122. ("nofninline" org-footnote-define-inline nil)
  3123. ("fnlocal" org-footnote-section nil)
  3124. ("fnauto" org-footnote-auto-label t)
  3125. ("fnprompt" org-footnote-auto-label nil)
  3126. ("fnconfirm" org-footnote-auto-label confirm)
  3127. ("fnplain" org-footnote-auto-label plain)
  3128. ("fnadjust" org-footnote-auto-adjust t)
  3129. ("nofnadjust" org-footnote-auto-adjust nil)
  3130. ("constcgs" constants-unit-system cgs)
  3131. ("constSI" constants-unit-system SI)
  3132. ("noptag" org-tag-persistent-alist nil)
  3133. ("hideblocks" org-hide-block-startup t)
  3134. ("nohideblocks" org-hide-block-startup nil))
  3135. "Variable associated with STARTUP options for org-mode.
  3136. Each element is a list of three items: The startup options as written
  3137. in the #+STARTUP line, the corresponding variable, and the value to
  3138. set this variable to if the option is found. An optional forth element PUSH
  3139. means to push this value onto the list in the variable.")
  3140. (defun org-set-regexps-and-options ()
  3141. "Precompute regular expressions for current buffer."
  3142. (when (org-mode-p)
  3143. (org-set-local 'org-todo-kwd-alist nil)
  3144. (org-set-local 'org-todo-key-alist nil)
  3145. (org-set-local 'org-todo-key-trigger nil)
  3146. (org-set-local 'org-todo-keywords-1 nil)
  3147. (org-set-local 'org-done-keywords nil)
  3148. (org-set-local 'org-todo-heads nil)
  3149. (org-set-local 'org-todo-sets nil)
  3150. (org-set-local 'org-todo-log-states nil)
  3151. (org-set-local 'org-file-properties nil)
  3152. (org-set-local 'org-file-tags nil)
  3153. (let ((re (org-make-options-regexp
  3154. '("CATEGORY" "TODO" "COLUMNS"
  3155. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3156. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  3157. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3158. (splitre "[ \t]+")
  3159. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3160. tail sep kws1 prio props ftags drawers
  3161. ext-setup-or-nil setup-contents (start 0))
  3162. (save-excursion
  3163. (save-restriction
  3164. (widen)
  3165. (goto-char (point-min))
  3166. (while (or (and ext-setup-or-nil
  3167. (string-match re ext-setup-or-nil start)
  3168. (setq start (match-end 0)))
  3169. (and (setq ext-setup-or-nil nil start 0)
  3170. (re-search-forward re nil t)))
  3171. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3172. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3173. (cond
  3174. ((equal key "CATEGORY")
  3175. (if (string-match "[ \t]+$" value)
  3176. (setq value (replace-match "" t t value)))
  3177. (setq cat value))
  3178. ((member key '("SEQ_TODO" "TODO"))
  3179. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3180. ((equal key "TYP_TODO")
  3181. (push (cons 'type (org-split-string value splitre)) kwds))
  3182. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3183. ;; general TODO-like setup
  3184. (push (cons (intern (downcase (match-string 1 key)))
  3185. (org-split-string value splitre)) kwds))
  3186. ((equal key "TAGS")
  3187. (setq tags (append tags (if tags '("\\n") nil)
  3188. (org-split-string value splitre))))
  3189. ((equal key "COLUMNS")
  3190. (org-set-local 'org-columns-default-format value))
  3191. ((equal key "LINK")
  3192. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3193. (push (cons (match-string 1 value)
  3194. (org-trim (match-string 2 value)))
  3195. links)))
  3196. ((equal key "PRIORITIES")
  3197. (setq prio (org-split-string value " +")))
  3198. ((equal key "PROPERTY")
  3199. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3200. (push (cons (match-string 1 value) (match-string 2 value))
  3201. props)))
  3202. ((equal key "FILETAGS")
  3203. (when (string-match "\\S-" value)
  3204. (setq ftags
  3205. (append
  3206. ftags
  3207. (apply 'append
  3208. (mapcar (lambda (x) (org-split-string x ":"))
  3209. (org-split-string value)))))))
  3210. ((equal key "DRAWERS")
  3211. (setq drawers (org-split-string value splitre)))
  3212. ((equal key "CONSTANTS")
  3213. (setq const (append const (org-split-string value splitre))))
  3214. ((equal key "STARTUP")
  3215. (let ((opts (org-split-string value splitre))
  3216. l var val)
  3217. (while (setq l (pop opts))
  3218. (when (setq l (assoc l org-startup-options))
  3219. (setq var (nth 1 l) val (nth 2 l))
  3220. (if (not (nth 3 l))
  3221. (set (make-local-variable var) val)
  3222. (if (not (listp (symbol-value var)))
  3223. (set (make-local-variable var) nil))
  3224. (set (make-local-variable var) (symbol-value var))
  3225. (add-to-list var val))))))
  3226. ((equal key "ARCHIVE")
  3227. (string-match " *$" value)
  3228. (setq arch (replace-match "" t t value))
  3229. (remove-text-properties 0 (length arch)
  3230. '(face t fontified t) arch))
  3231. ((equal key "SETUPFILE")
  3232. (setq setup-contents (org-file-contents
  3233. (expand-file-name
  3234. (org-remove-double-quotes value))
  3235. 'noerror))
  3236. (if (not ext-setup-or-nil)
  3237. (setq ext-setup-or-nil setup-contents start 0)
  3238. (setq ext-setup-or-nil
  3239. (concat (substring ext-setup-or-nil 0 start)
  3240. "\n" setup-contents "\n"
  3241. (substring ext-setup-or-nil start)))))
  3242. ))))
  3243. (when cat
  3244. (org-set-local 'org-category (intern cat))
  3245. (push (cons "CATEGORY" cat) props))
  3246. (when prio
  3247. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3248. (setq prio (mapcar 'string-to-char prio))
  3249. (org-set-local 'org-highest-priority (nth 0 prio))
  3250. (org-set-local 'org-lowest-priority (nth 1 prio))
  3251. (org-set-local 'org-default-priority (nth 2 prio)))
  3252. (and props (org-set-local 'org-file-properties (nreverse props)))
  3253. (and ftags (org-set-local 'org-file-tags
  3254. (mapcar 'org-add-prop-inherited ftags)))
  3255. (and drawers (org-set-local 'org-drawers drawers))
  3256. (and arch (org-set-local 'org-archive-location arch))
  3257. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3258. ;; Process the TODO keywords
  3259. (unless kwds
  3260. ;; Use the global values as if they had been given locally.
  3261. (setq kwds (default-value 'org-todo-keywords))
  3262. (if (stringp (car kwds))
  3263. (setq kwds (list (cons org-todo-interpretation
  3264. (default-value 'org-todo-keywords)))))
  3265. (setq kwds (reverse kwds)))
  3266. (setq kwds (nreverse kwds))
  3267. (let (inter kws kw)
  3268. (while (setq kws (pop kwds))
  3269. (let ((kws (or
  3270. (run-hook-with-args-until-success
  3271. 'org-todo-setup-filter-hook kws)
  3272. kws)))
  3273. (setq inter (pop kws) sep (member "|" kws)
  3274. kws0 (delete "|" (copy-sequence kws))
  3275. kwsa nil
  3276. kws1 (mapcar
  3277. (lambda (x)
  3278. ;; 1 2
  3279. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3280. (progn
  3281. (setq kw (match-string 1 x)
  3282. key (and (match-end 2) (match-string 2 x))
  3283. log (org-extract-log-state-settings x))
  3284. (push (cons kw (and key (string-to-char key))) kwsa)
  3285. (and log (push log org-todo-log-states))
  3286. kw)
  3287. (error "Invalid TODO keyword %s" x)))
  3288. kws0)
  3289. kwsa (if kwsa (append '((:startgroup))
  3290. (nreverse kwsa)
  3291. '((:endgroup))))
  3292. hw (car kws1)
  3293. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3294. tail (list inter hw (car dws) (org-last dws))))
  3295. (add-to-list 'org-todo-heads hw 'append)
  3296. (push kws1 org-todo-sets)
  3297. (setq org-done-keywords (append org-done-keywords dws nil))
  3298. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3299. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3300. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3301. (setq org-todo-sets (nreverse org-todo-sets)
  3302. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3303. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3304. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3305. ;; Process the constants
  3306. (when const
  3307. (let (e cst)
  3308. (while (setq e (pop const))
  3309. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3310. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3311. (setq org-table-formula-constants-local cst)))
  3312. ;; Process the tags.
  3313. (when tags
  3314. (let (e tgs)
  3315. (while (setq e (pop tags))
  3316. (cond
  3317. ((equal e "{") (push '(:startgroup) tgs))
  3318. ((equal e "}") (push '(:endgroup) tgs))
  3319. ((equal e "\\n") (push '(:newline) tgs))
  3320. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3321. (push (cons (match-string 1 e)
  3322. (string-to-char (match-string 2 e)))
  3323. tgs))
  3324. (t (push (list e) tgs))))
  3325. (org-set-local 'org-tag-alist nil)
  3326. (while (setq e (pop tgs))
  3327. (or (and (stringp (car e))
  3328. (assoc (car e) org-tag-alist))
  3329. (push e org-tag-alist)))))
  3330. ;; Compute the regular expressions and other local variables
  3331. (if (not org-done-keywords)
  3332. (setq org-done-keywords (and org-todo-keywords-1
  3333. (list (org-last org-todo-keywords-1)))))
  3334. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3335. (length org-scheduled-string)
  3336. (length org-clock-string)
  3337. (length org-closed-string)))
  3338. org-drawer-regexp
  3339. (concat "^[ \t]*:\\("
  3340. (mapconcat 'regexp-quote org-drawers "\\|")
  3341. "\\):[ \t]*$")
  3342. org-not-done-keywords
  3343. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3344. org-todo-regexp
  3345. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3346. "\\|") "\\)\\>")
  3347. org-not-done-regexp
  3348. (concat "\\<\\("
  3349. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3350. "\\)\\>")
  3351. org-not-done-heading-regexp
  3352. (concat "^\\(\\*+\\)[ \t]+\\("
  3353. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3354. "\\)\\>")
  3355. org-todo-line-regexp
  3356. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3357. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3358. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3359. org-complex-heading-regexp
  3360. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3361. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3362. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3363. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3364. org-complex-heading-regexp-format
  3365. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3366. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3367. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
  3368. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3369. org-nl-done-regexp
  3370. (concat "\n\\*+[ \t]+"
  3371. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3372. "\\)" "\\>")
  3373. org-todo-line-tags-regexp
  3374. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3375. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3376. (org-re
  3377. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3378. org-looking-at-done-regexp
  3379. (concat "^" "\\(?:"
  3380. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3381. "\\>")
  3382. org-deadline-regexp (concat "\\<" org-deadline-string)
  3383. org-deadline-time-regexp
  3384. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3385. org-deadline-line-regexp
  3386. (concat "\\<\\(" org-deadline-string "\\).*")
  3387. org-scheduled-regexp
  3388. (concat "\\<" org-scheduled-string)
  3389. org-scheduled-time-regexp
  3390. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3391. org-closed-time-regexp
  3392. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3393. org-keyword-time-regexp
  3394. (concat "\\<\\(" org-scheduled-string
  3395. "\\|" org-deadline-string
  3396. "\\|" org-closed-string
  3397. "\\|" org-clock-string "\\)"
  3398. " *[[<]\\([^]>]+\\)[]>]")
  3399. org-keyword-time-not-clock-regexp
  3400. (concat "\\<\\(" org-scheduled-string
  3401. "\\|" org-deadline-string
  3402. "\\|" org-closed-string
  3403. "\\)"
  3404. " *[[<]\\([^]>]+\\)[]>]")
  3405. org-maybe-keyword-time-regexp
  3406. (concat "\\(\\<\\(" org-scheduled-string
  3407. "\\|" org-deadline-string
  3408. "\\|" org-closed-string
  3409. "\\|" org-clock-string "\\)\\)?"
  3410. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3411. org-planning-or-clock-line-re
  3412. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3413. "\\|" org-deadline-string
  3414. "\\|" org-closed-string "\\|" org-clock-string
  3415. "\\)\\>\\)")
  3416. )
  3417. (org-compute-latex-and-specials-regexp)
  3418. (org-set-font-lock-defaults))))
  3419. (defun org-file-contents (file &optional noerror)
  3420. "Return the contents of FILE, as a string."
  3421. (if (or (not file)
  3422. (not (file-readable-p file)))
  3423. (if noerror
  3424. (progn
  3425. (message "Cannot read file %s" file)
  3426. (ding) (sit-for 2)
  3427. "")
  3428. (error "Cannot read file %s" file))
  3429. (with-temp-buffer
  3430. (insert-file-contents file)
  3431. (buffer-string))))
  3432. (defun org-extract-log-state-settings (x)
  3433. "Extract the log state setting from a TODO keyword string.
  3434. This will extract info from a string like \"WAIT(w@/!)\"."
  3435. (let (kw key log1 log2)
  3436. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3437. (setq kw (match-string 1 x)
  3438. key (and (match-end 2) (match-string 2 x))
  3439. log1 (and (match-end 3) (match-string 3 x))
  3440. log2 (and (match-end 4) (match-string 4 x)))
  3441. (and (or log1 log2)
  3442. (list kw
  3443. (and log1 (if (equal log1 "!") 'time 'note))
  3444. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3445. (defun org-remove-keyword-keys (list)
  3446. "Remove a pair of parenthesis at the end of each string in LIST."
  3447. (mapcar (lambda (x)
  3448. (if (string-match "(.*)$" x)
  3449. (substring x 0 (match-beginning 0))
  3450. x))
  3451. list))
  3452. ;; FIXME: this could be done much better, using second characters etc.
  3453. (defun org-assign-fast-keys (alist)
  3454. "Assign fast keys to a keyword-key alist.
  3455. Respect keys that are already there."
  3456. (let (new e k c c1 c2 (char ?a))
  3457. (while (setq e (pop alist))
  3458. (cond
  3459. ((equal e '(:startgroup)) (push e new))
  3460. ((equal e '(:endgroup)) (push e new))
  3461. ((equal e '(:newline)) (push e new))
  3462. (t
  3463. (setq k (car e) c2 nil)
  3464. (if (cdr e)
  3465. (setq c (cdr e))
  3466. ;; automatically assign a character.
  3467. (setq c1 (string-to-char
  3468. (downcase (substring
  3469. k (if (= (string-to-char k) ?@) 1 0)))))
  3470. (if (or (rassoc c1 new) (rassoc c1 alist))
  3471. (while (or (rassoc char new) (rassoc char alist))
  3472. (setq char (1+ char)))
  3473. (setq c2 c1))
  3474. (setq c (or c2 char)))
  3475. (push (cons k c) new))))
  3476. (nreverse new)))
  3477. ;;; Some variables used in various places
  3478. (defvar org-window-configuration nil
  3479. "Used in various places to store a window configuration.")
  3480. (defvar org-selected-window nil
  3481. "Used in various places to store a window configuration.")
  3482. (defvar org-finish-function nil
  3483. "Function to be called when `C-c C-c' is used.
  3484. This is for getting out of special buffers like remember.")
  3485. ;; FIXME: Occasionally check by commenting these, to make sure
  3486. ;; no other functions uses these, forgetting to let-bind them.
  3487. (defvar entry)
  3488. (defvar last-state)
  3489. (defvar date)
  3490. ;; Defined somewhere in this file, but used before definition.
  3491. (defvar org-html-entities)
  3492. (defvar org-struct-menu)
  3493. (defvar org-org-menu)
  3494. (defvar org-tbl-menu)
  3495. (defvar org-agenda-keymap)
  3496. ;;;; Define the Org-mode
  3497. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3498. (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."))
  3499. ;; We use a before-change function to check if a table might need
  3500. ;; an update.
  3501. (defvar org-table-may-need-update t
  3502. "Indicates that a table might need an update.
  3503. This variable is set by `org-before-change-function'.
  3504. `org-table-align' sets it back to nil.")
  3505. (defun org-before-change-function (beg end)
  3506. "Every change indicates that a table might need an update."
  3507. (setq org-table-may-need-update t))
  3508. (defvar org-mode-map)
  3509. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3510. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3511. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3512. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3513. (defvar org-table-buffer-is-an nil)
  3514. (defconst org-outline-regexp "\\*+ ")
  3515. ;;;###autoload
  3516. (define-derived-mode org-mode outline-mode "Org"
  3517. "Outline-based notes management and organizer, alias
  3518. \"Carsten's outline-mode for keeping track of everything.\"
  3519. Org-mode develops organizational tasks around a NOTES file which
  3520. contains information about projects as plain text. Org-mode is
  3521. implemented on top of outline-mode, which is ideal to keep the content
  3522. of large files well structured. It supports ToDo items, deadlines and
  3523. time stamps, which magically appear in the diary listing of the Emacs
  3524. calendar. Tables are easily created with a built-in table editor.
  3525. Plain text URL-like links connect to websites, emails (VM), Usenet
  3526. messages (Gnus), BBDB entries, and any files related to the project.
  3527. For printing and sharing of notes, an Org-mode file (or a part of it)
  3528. can be exported as a structured ASCII or HTML file.
  3529. The following commands are available:
  3530. \\{org-mode-map}"
  3531. ;; Get rid of Outline menus, they are not needed
  3532. ;; Need to do this here because define-derived-mode sets up
  3533. ;; the keymap so late. Still, it is a waste to call this each time
  3534. ;; we switch another buffer into org-mode.
  3535. (if (featurep 'xemacs)
  3536. (when (boundp 'outline-mode-menu-heading)
  3537. ;; Assume this is Greg's port, it used easymenu
  3538. (easy-menu-remove outline-mode-menu-heading)
  3539. (easy-menu-remove outline-mode-menu-show)
  3540. (easy-menu-remove outline-mode-menu-hide))
  3541. (define-key org-mode-map [menu-bar headings] 'undefined)
  3542. (define-key org-mode-map [menu-bar hide] 'undefined)
  3543. (define-key org-mode-map [menu-bar show] 'undefined))
  3544. (org-load-modules-maybe)
  3545. (easy-menu-add org-org-menu)
  3546. (easy-menu-add org-tbl-menu)
  3547. (org-install-agenda-files-menu)
  3548. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3549. (org-add-to-invisibility-spec '(org-cwidth))
  3550. (org-add-to-invisibility-spec '(org-hide-block . t))
  3551. (when (featurep 'xemacs)
  3552. (org-set-local 'line-move-ignore-invisible t))
  3553. (org-set-local 'outline-regexp org-outline-regexp)
  3554. (org-set-local 'outline-level 'org-outline-level)
  3555. (when (and org-ellipsis
  3556. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3557. (fboundp 'make-glyph-code))
  3558. (unless org-display-table
  3559. (setq org-display-table (make-display-table)))
  3560. (set-display-table-slot
  3561. org-display-table 4
  3562. (vconcat (mapcar
  3563. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3564. org-ellipsis)))
  3565. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3566. (setq buffer-display-table org-display-table))
  3567. (org-set-regexps-and-options)
  3568. (when (and org-tag-faces (not org-tags-special-faces-re))
  3569. ;; tag faces set outside customize.... force initialization.
  3570. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3571. ;; Calc embedded
  3572. (org-set-local 'calc-embedded-open-mode "# ")
  3573. (modify-syntax-entry ?# "<")
  3574. (modify-syntax-entry ?@ "w")
  3575. (if org-startup-truncated (setq truncate-lines t))
  3576. (org-set-local 'font-lock-unfontify-region-function
  3577. 'org-unfontify-region)
  3578. ;; Activate before-change-function
  3579. (org-set-local 'org-table-may-need-update t)
  3580. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3581. 'local)
  3582. ;; Check for running clock before killing a buffer
  3583. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3584. ;; Paragraphs and auto-filling
  3585. (org-set-autofill-regexps)
  3586. (setq indent-line-function 'org-indent-line-function)
  3587. (org-update-radio-target-regexp)
  3588. ;; Make sure dependence stuff works reliably, even for users who set it
  3589. ;; too late :-(
  3590. (if org-enforce-todo-dependencies
  3591. (add-hook 'org-blocker-hook
  3592. 'org-block-todo-from-children-or-siblings-or-parent)
  3593. (remove-hook 'org-blocker-hook
  3594. 'org-block-todo-from-children-or-siblings-or-parent))
  3595. (if org-enforce-todo-checkbox-dependencies
  3596. (add-hook 'org-blocker-hook
  3597. 'org-block-todo-from-checkboxes)
  3598. (remove-hook 'org-blocker-hook
  3599. 'org-block-todo-from-checkboxes))
  3600. ;; Comment characters
  3601. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3602. (org-set-local 'comment-padding " ")
  3603. ;; Align options lines
  3604. (org-set-local
  3605. 'align-mode-rules-list
  3606. '((org-in-buffer-settings
  3607. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3608. (modes . '(org-mode)))))
  3609. ;; Imenu
  3610. (org-set-local 'imenu-create-index-function
  3611. 'org-imenu-get-tree)
  3612. ;; Make isearch reveal context
  3613. (if (or (featurep 'xemacs)
  3614. (not (boundp 'outline-isearch-open-invisible-function)))
  3615. ;; Emacs 21 and XEmacs make use of the hook
  3616. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3617. ;; Emacs 22 deals with this through a special variable
  3618. (org-set-local 'outline-isearch-open-invisible-function
  3619. (lambda (&rest ignore) (org-show-context 'isearch))))
  3620. ;; If empty file that did not turn on org-mode automatically, make it to.
  3621. (if (and org-insert-mode-line-in-empty-file
  3622. (interactive-p)
  3623. (= (point-min) (point-max)))
  3624. (insert "# -*- mode: org -*-\n\n"))
  3625. (unless org-inhibit-startup
  3626. (when org-startup-align-all-tables
  3627. (let ((bmp (buffer-modified-p)))
  3628. (org-table-map-tables 'org-table-align)
  3629. (set-buffer-modified-p bmp)))
  3630. (when org-startup-indented
  3631. (require 'org-indent)
  3632. (org-indent-mode 1))
  3633. (org-set-startup-visibility)))
  3634. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3635. (defun org-current-time ()
  3636. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3637. (if (> (car org-time-stamp-rounding-minutes) 1)
  3638. (let ((r (car org-time-stamp-rounding-minutes))
  3639. (time (decode-time)))
  3640. (apply 'encode-time
  3641. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3642. (nthcdr 2 time))))
  3643. (current-time)))
  3644. ;;;; Font-Lock stuff, including the activators
  3645. (defvar org-mouse-map (make-sparse-keymap))
  3646. (org-defkey org-mouse-map
  3647. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3648. (org-defkey org-mouse-map
  3649. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3650. (when org-mouse-1-follows-link
  3651. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3652. (when org-tab-follows-link
  3653. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3654. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3655. (require 'font-lock)
  3656. (defconst org-non-link-chars "]\t\n\r<>")
  3657. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3658. "shell" "elisp"))
  3659. (defvar org-link-types-re nil
  3660. "Matches a link that has a url-like prefix like \"http:\"")
  3661. (defvar org-link-re-with-space nil
  3662. "Matches a link with spaces, optional angular brackets around it.")
  3663. (defvar org-link-re-with-space2 nil
  3664. "Matches a link with spaces, optional angular brackets around it.")
  3665. (defvar org-link-re-with-space3 nil
  3666. "Matches a link with spaces, only for internal part in bracket links.")
  3667. (defvar org-angle-link-re nil
  3668. "Matches link with angular brackets, spaces are allowed.")
  3669. (defvar org-plain-link-re nil
  3670. "Matches plain link, without spaces.")
  3671. (defvar org-bracket-link-regexp nil
  3672. "Matches a link in double brackets.")
  3673. (defvar org-bracket-link-analytic-regexp nil
  3674. "Regular expression used to analyze links.
  3675. Here is what the match groups contain after a match:
  3676. 1: http:
  3677. 2: http
  3678. 3: path
  3679. 4: [desc]
  3680. 5: desc")
  3681. (defvar org-bracket-link-analytic-regexp++ nil
  3682. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3683. (defvar org-any-link-re nil
  3684. "Regular expression matching any link.")
  3685. (defun org-make-link-regexps ()
  3686. "Update the link regular expressions.
  3687. This should be called after the variable `org-link-types' has changed."
  3688. (setq org-link-types-re
  3689. (concat
  3690. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3691. org-link-re-with-space
  3692. (concat
  3693. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3694. "\\([^" org-non-link-chars " ]"
  3695. "[^" org-non-link-chars "]*"
  3696. "[^" org-non-link-chars " ]\\)>?")
  3697. org-link-re-with-space2
  3698. (concat
  3699. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3700. "\\([^" org-non-link-chars " ]"
  3701. "[^\t\n\r]*"
  3702. "[^" org-non-link-chars " ]\\)>?")
  3703. org-link-re-with-space3
  3704. (concat
  3705. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3706. "\\([^" org-non-link-chars " ]"
  3707. "[^\t\n\r]*\\)")
  3708. org-angle-link-re
  3709. (concat
  3710. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3711. "\\([^" org-non-link-chars " ]"
  3712. "[^" org-non-link-chars "]*"
  3713. "\\)>")
  3714. org-plain-link-re
  3715. (concat
  3716. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3717. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3718. org-bracket-link-regexp
  3719. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3720. org-bracket-link-analytic-regexp
  3721. (concat
  3722. "\\[\\["
  3723. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3724. "\\([^]]+\\)"
  3725. "\\]"
  3726. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3727. "\\]")
  3728. org-bracket-link-analytic-regexp++
  3729. (concat
  3730. "\\[\\["
  3731. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3732. "\\([^]]+\\)"
  3733. "\\]"
  3734. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3735. "\\]")
  3736. org-any-link-re
  3737. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3738. org-angle-link-re "\\)\\|\\("
  3739. org-plain-link-re "\\)")))
  3740. (org-make-link-regexps)
  3741. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3742. "Regular expression for fast time stamp matching.")
  3743. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3744. "Regular expression for fast time stamp matching.")
  3745. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3746. "Regular expression matching time strings for analysis.
  3747. This one does not require the space after the date, so it can be used
  3748. on a string that terminates immediately after the date.")
  3749. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3750. "Regular expression matching time strings for analysis.")
  3751. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3752. "Regular expression matching time stamps, with groups.")
  3753. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3754. "Regular expression matching time stamps (also [..]), with groups.")
  3755. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3756. "Regular expression matching a time stamp range.")
  3757. (defconst org-tr-regexp-both
  3758. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3759. "Regular expression matching a time stamp range.")
  3760. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3761. org-ts-regexp "\\)?")
  3762. "Regular expression matching a time stamp or time stamp range.")
  3763. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3764. org-ts-regexp-both "\\)?")
  3765. "Regular expression matching a time stamp or time stamp range.
  3766. The time stamps may be either active or inactive.")
  3767. (defvar org-emph-face nil)
  3768. (defun org-do-emphasis-faces (limit)
  3769. "Run through the buffer and add overlays to links."
  3770. (let (rtn a)
  3771. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3772. (if (not (= (char-after (match-beginning 3))
  3773. (char-after (match-beginning 4))))
  3774. (progn
  3775. (setq rtn t)
  3776. (setq a (assoc (match-string 3) org-emphasis-alist))
  3777. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3778. 'face
  3779. (nth 1 a))
  3780. (and (nth 4 a)
  3781. (org-remove-flyspell-overlays-in
  3782. (match-beginning 0) (match-end 0)))
  3783. (add-text-properties (match-beginning 2) (match-end 2)
  3784. '(font-lock-multiline t))
  3785. (when org-hide-emphasis-markers
  3786. (add-text-properties (match-end 4) (match-beginning 5)
  3787. '(invisible org-link))
  3788. (add-text-properties (match-beginning 3) (match-end 3)
  3789. '(invisible org-link)))))
  3790. (backward-char 1))
  3791. rtn))
  3792. (defun org-emphasize (&optional char)
  3793. "Insert or change an emphasis, i.e. a font like bold or italic.
  3794. If there is an active region, change that region to a new emphasis.
  3795. If there is no region, just insert the marker characters and position
  3796. the cursor between them.
  3797. CHAR should be either the marker character, or the first character of the
  3798. HTML tag associated with that emphasis. If CHAR is a space, the means
  3799. to remove the emphasis of the selected region.
  3800. If char is not given (for example in an interactive call) it
  3801. will be prompted for."
  3802. (interactive)
  3803. (let ((eal org-emphasis-alist) e det
  3804. (erc org-emphasis-regexp-components)
  3805. (prompt "")
  3806. (string "") beg end move tag c s)
  3807. (if (org-region-active-p)
  3808. (setq beg (region-beginning) end (region-end)
  3809. string (buffer-substring beg end))
  3810. (setq move t))
  3811. (while (setq e (pop eal))
  3812. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3813. c (aref tag 0))
  3814. (push (cons c (string-to-char (car e))) det)
  3815. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3816. (substring tag 1)))))
  3817. (setq det (nreverse det))
  3818. (unless char
  3819. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3820. (setq char (read-char-exclusive)))
  3821. (setq char (or (cdr (assoc char det)) char))
  3822. (if (equal char ?\ )
  3823. (setq s "" move nil)
  3824. (unless (assoc (char-to-string char) org-emphasis-alist)
  3825. (error "No such emphasis marker: \"%c\"" char))
  3826. (setq s (char-to-string char)))
  3827. (while (and (> (length string) 1)
  3828. (equal (substring string 0 1) (substring string -1))
  3829. (assoc (substring string 0 1) org-emphasis-alist))
  3830. (setq string (substring string 1 -1)))
  3831. (setq string (concat s string s))
  3832. (if beg (delete-region beg end))
  3833. (unless (or (bolp)
  3834. (string-match (concat "[" (nth 0 erc) "\n]")
  3835. (char-to-string (char-before (point)))))
  3836. (insert " "))
  3837. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3838. (char-to-string (char-after (point))))
  3839. (insert " ") (backward-char 1))
  3840. (insert string)
  3841. (and move (backward-char 1))))
  3842. (defconst org-nonsticky-props
  3843. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3844. (defsubst org-rear-nonsticky-at (pos)
  3845. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3846. (defun org-activate-plain-links (limit)
  3847. "Run through the buffer and add overlays to links."
  3848. (catch 'exit
  3849. (let (f)
  3850. (if (re-search-forward org-plain-link-re limit t)
  3851. (progn
  3852. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3853. (setq f (get-text-property (match-beginning 0) 'face))
  3854. (if (or (eq f 'org-tag)
  3855. (and (listp f) (memq 'org-tag f)))
  3856. nil
  3857. (add-text-properties (match-beginning 0) (match-end 0)
  3858. (list 'mouse-face 'highlight
  3859. 'keymap org-mouse-map))
  3860. (org-rear-nonsticky-at (match-end 0)))
  3861. t)))))
  3862. (defun org-activate-code (limit)
  3863. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3864. (progn
  3865. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3866. (remove-text-properties (match-beginning 0) (match-end 0)
  3867. '(display t invisible t intangible t))
  3868. t)))
  3869. (defun org-fontify-meta-lines-and-blocks (limit)
  3870. "Fontify #+ lines and blocks, in the correct ways."
  3871. (let ((case-fold-search t))
  3872. (if (re-search-forward
  3873. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  3874. limit t)
  3875. (let ((beg (match-beginning 0))
  3876. (beg1 (line-beginning-position 2))
  3877. (dc1 (downcase (match-string 2)))
  3878. (dc3 (downcase (match-string 3)))
  3879. end end1 quoting)
  3880. (cond
  3881. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  3882. ;; a single line of backend-specific content
  3883. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3884. (remove-text-properties (match-beginning 0) (match-end 0)
  3885. '(display t invisible t intangible t))
  3886. (add-text-properties (match-beginning 1) (match-end 3)
  3887. '(font-lock-fontified t face org-meta-line))
  3888. (add-text-properties (match-beginning 6) (match-end 6)
  3889. '(font-lock-fontified t face org-block))
  3890. t)
  3891. ((and (match-end 4) (equal dc3 "begin"))
  3892. ;; Truely a block
  3893. (setq quoting (member (downcase (match-string 5))
  3894. org-protecting-blocks))
  3895. (when (re-search-forward
  3896. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  3897. nil t) ;; on purpose, we look further than LIMIT
  3898. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  3899. (when quoting
  3900. (remove-text-properties beg end
  3901. '(display t invisible t intangible t)))
  3902. (add-text-properties
  3903. beg end
  3904. '(font-lock-fontified t font-lock-multiline t))
  3905. (add-text-properties beg beg1 '(face org-meta-line))
  3906. (add-text-properties end1 end '(face org-meta-line))
  3907. (when quoting
  3908. (add-text-properties beg1 end1 '(face org-block)))
  3909. t))
  3910. ((not (member (char-after beg) '(?\ ?\t)))
  3911. ;; just any other in-buffer setting, but not indented
  3912. (add-text-properties
  3913. beg (match-end 0)
  3914. '(font-lock-fontified t face org-meta-line))
  3915. t)
  3916. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  3917. "orgtbl:" "tblfm:" "tblname:"))
  3918. (and (match-end 4) (equal dc3 "attr")))
  3919. (add-text-properties
  3920. beg (match-end 0)
  3921. '(font-lock-fontified t face org-meta-line))
  3922. t)
  3923. ((member dc3 '(" " ""))
  3924. (add-text-properties
  3925. beg (match-end 0)
  3926. '(font-lock-fontified t face font-lock-comment-face)))
  3927. (t nil))))))
  3928. (defun org-activate-angle-links (limit)
  3929. "Run through the buffer and add overlays to links."
  3930. (if (re-search-forward org-angle-link-re limit t)
  3931. (progn
  3932. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3933. (add-text-properties (match-beginning 0) (match-end 0)
  3934. (list 'mouse-face 'highlight
  3935. 'keymap org-mouse-map))
  3936. (org-rear-nonsticky-at (match-end 0))
  3937. t)))
  3938. (defun org-activate-footnote-links (limit)
  3939. "Run through the buffer and add overlays to links."
  3940. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3941. limit t)
  3942. (progn
  3943. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3944. (add-text-properties (match-beginning 2) (match-end 2)
  3945. (list 'mouse-face 'highlight
  3946. 'keymap org-mouse-map
  3947. 'help-echo
  3948. (if (= (point-at-bol) (match-beginning 2))
  3949. "Footnote definition"
  3950. "Footnote reference")
  3951. ))
  3952. (org-rear-nonsticky-at (match-end 2))
  3953. t)))
  3954. (defun org-activate-bracket-links (limit)
  3955. "Run through the buffer and add overlays to bracketed links."
  3956. (if (re-search-forward org-bracket-link-regexp limit t)
  3957. (let* ((help (concat "LINK: "
  3958. (org-match-string-no-properties 1)))
  3959. ;; FIXME: above we should remove the escapes.
  3960. ;; but that requires another match, protecting match data,
  3961. ;; a lot of overhead for font-lock.
  3962. (ip (org-maybe-intangible
  3963. (list 'invisible 'org-link
  3964. 'keymap org-mouse-map 'mouse-face 'highlight
  3965. 'font-lock-multiline t 'help-echo help)))
  3966. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  3967. 'font-lock-multiline t 'help-echo help)))
  3968. ;; We need to remove the invisible property here. Table narrowing
  3969. ;; may have made some of this invisible.
  3970. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3971. (remove-text-properties (match-beginning 0) (match-end 0)
  3972. '(invisible nil))
  3973. (if (match-end 3)
  3974. (progn
  3975. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3976. (org-rear-nonsticky-at (match-beginning 3))
  3977. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3978. (org-rear-nonsticky-at (match-end 3))
  3979. (add-text-properties (match-end 3) (match-end 0) ip)
  3980. (org-rear-nonsticky-at (match-end 0)))
  3981. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3982. (org-rear-nonsticky-at (match-beginning 1))
  3983. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3984. (org-rear-nonsticky-at (match-end 1))
  3985. (add-text-properties (match-end 1) (match-end 0) ip)
  3986. (org-rear-nonsticky-at (match-end 0)))
  3987. t)))
  3988. (defun org-activate-dates (limit)
  3989. "Run through the buffer and add overlays to dates."
  3990. (if (re-search-forward org-tsr-regexp-both limit t)
  3991. (progn
  3992. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3993. (add-text-properties (match-beginning 0) (match-end 0)
  3994. (list 'mouse-face 'highlight
  3995. 'keymap org-mouse-map))
  3996. (org-rear-nonsticky-at (match-end 0))
  3997. (when org-display-custom-times
  3998. (if (match-end 3)
  3999. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4000. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4001. t)))
  4002. (defvar org-target-link-regexp nil
  4003. "Regular expression matching radio targets in plain text.")
  4004. (make-variable-buffer-local 'org-target-link-regexp)
  4005. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4006. "Regular expression matching a link target.")
  4007. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4008. "Regular expression matching a radio target.")
  4009. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4010. "Regular expression matching any target.")
  4011. (defun org-activate-target-links (limit)
  4012. "Run through the buffer and add overlays to target matches."
  4013. (when org-target-link-regexp
  4014. (let ((case-fold-search t))
  4015. (if (re-search-forward org-target-link-regexp limit t)
  4016. (progn
  4017. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4018. (add-text-properties (match-beginning 0) (match-end 0)
  4019. (list 'mouse-face 'highlight
  4020. 'keymap org-mouse-map
  4021. 'help-echo "Radio target link"
  4022. 'org-linked-text t))
  4023. (org-rear-nonsticky-at (match-end 0))
  4024. t)))))
  4025. (defun org-update-radio-target-regexp ()
  4026. "Find all radio targets in this file and update the regular expression."
  4027. (interactive)
  4028. (when (memq 'radio org-activate-links)
  4029. (setq org-target-link-regexp
  4030. (org-make-target-link-regexp (org-all-targets 'radio)))
  4031. (org-restart-font-lock)))
  4032. (defun org-hide-wide-columns (limit)
  4033. (let (s e)
  4034. (setq s (text-property-any (point) (or limit (point-max))
  4035. 'org-cwidth t))
  4036. (when s
  4037. (setq e (next-single-property-change s 'org-cwidth))
  4038. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4039. (goto-char e)
  4040. t)))
  4041. (defvar org-latex-and-specials-regexp nil
  4042. "Regular expression for highlighting export special stuff.")
  4043. (defvar org-match-substring-regexp)
  4044. (defvar org-match-substring-with-braces-regexp)
  4045. ;; This should be with the exporter code, but we also use if for font-locking
  4046. (defconst org-export-html-special-string-regexps
  4047. '(("\\\\-" . "&shy;")
  4048. ("---\\([^-]\\)" . "&mdash;\\1")
  4049. ("--\\([^-]\\)" . "&ndash;\\1")
  4050. ("\\.\\.\\." . "&hellip;"))
  4051. "Regular expressions for special string conversion.")
  4052. (defun org-compute-latex-and-specials-regexp ()
  4053. "Compute regular expression for stuff treated specially by exporters."
  4054. (if (not org-highlight-latex-fragments-and-specials)
  4055. (org-set-local 'org-latex-and-specials-regexp nil)
  4056. (require 'org-exp)
  4057. (let*
  4058. ((matchers (plist-get org-format-latex-options :matchers))
  4059. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4060. org-latex-regexps)))
  4061. (options (org-combine-plists (org-default-export-plist)
  4062. (org-infile-export-plist)))
  4063. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4064. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4065. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4066. (org-export-html-expand (plist-get options :expand-quoted-html))
  4067. (org-export-with-special-strings (plist-get options :special-strings))
  4068. (re-sub
  4069. (cond
  4070. ((equal org-export-with-sub-superscripts '{})
  4071. (list org-match-substring-with-braces-regexp))
  4072. (org-export-with-sub-superscripts
  4073. (list org-match-substring-regexp))
  4074. (t nil)))
  4075. (re-latex
  4076. (if org-export-with-LaTeX-fragments
  4077. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4078. (re-macros
  4079. (if org-export-with-TeX-macros
  4080. (list (concat "\\\\"
  4081. (regexp-opt
  4082. (append (mapcar 'car org-html-entities)
  4083. (if (boundp 'org-latex-entities)
  4084. (mapcar (lambda (x)
  4085. (or (car-safe x) x))
  4086. org-latex-entities)
  4087. nil))
  4088. 'words))) ; FIXME
  4089. ))
  4090. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4091. (re-special (if org-export-with-special-strings
  4092. (mapcar (lambda (x) (car x))
  4093. org-export-html-special-string-regexps)))
  4094. (re-rest
  4095. (delq nil
  4096. (list
  4097. (if org-export-html-expand "@<[^>\n]+>")
  4098. ))))
  4099. (org-set-local
  4100. 'org-latex-and-specials-regexp
  4101. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4102. re-rest) "\\|")))))
  4103. (defun org-do-latex-and-special-faces (limit)
  4104. "Run through the buffer and add overlays to links."
  4105. (when org-latex-and-specials-regexp
  4106. (let (rtn d)
  4107. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4108. limit t))
  4109. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4110. 'face))
  4111. '(org-code org-verbatim underline)))
  4112. (progn
  4113. (setq rtn t
  4114. d (cond ((member (char-after (1+ (match-beginning 0)))
  4115. '(?_ ?^)) 1)
  4116. (t 0)))
  4117. (font-lock-prepend-text-property
  4118. (+ d (match-beginning 0)) (match-end 0)
  4119. 'face 'org-latex-and-export-specials)
  4120. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4121. '(font-lock-multiline t)))))
  4122. rtn)))
  4123. (defun org-restart-font-lock ()
  4124. "Restart font-lock-mode, to force refontification."
  4125. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4126. (font-lock-mode -1)
  4127. (font-lock-mode 1)))
  4128. (defun org-all-targets (&optional radio)
  4129. "Return a list of all targets in this file.
  4130. With optional argument RADIO, only find radio targets."
  4131. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4132. rtn)
  4133. (save-excursion
  4134. (goto-char (point-min))
  4135. (while (re-search-forward re nil t)
  4136. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4137. rtn)))
  4138. (defun org-make-target-link-regexp (targets)
  4139. "Make regular expression matching all strings in TARGETS.
  4140. The regular expression finds the targets also if there is a line break
  4141. between words."
  4142. (and targets
  4143. (concat
  4144. "\\<\\("
  4145. (mapconcat
  4146. (lambda (x)
  4147. (while (string-match " +" x)
  4148. (setq x (replace-match "\\s-+" t t x)))
  4149. x)
  4150. targets
  4151. "\\|")
  4152. "\\)\\>")))
  4153. (defun org-activate-tags (limit)
  4154. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4155. (progn
  4156. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4157. (add-text-properties (match-beginning 1) (match-end 1)
  4158. (list 'mouse-face 'highlight
  4159. 'keymap org-mouse-map))
  4160. (org-rear-nonsticky-at (match-end 1))
  4161. t)))
  4162. (defun org-outline-level ()
  4163. "Compute the outline level of the heading at point.
  4164. This function assumes that the cursor is at the beginning of a line matched
  4165. by outline-regexp. Otherwise it returns garbage.
  4166. If this is called at a normal headline, the level is the number of stars.
  4167. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4168. For plain list items, if they are matched by `outline-regexp', this returns
  4169. 1000 plus the line indentation."
  4170. (save-excursion
  4171. (looking-at outline-regexp)
  4172. (if (match-beginning 1)
  4173. (+ (org-get-string-indentation (match-string 1)) 1000)
  4174. (1- (- (match-end 0) (match-beginning 0))))))
  4175. (defvar org-font-lock-keywords nil)
  4176. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4177. "Regular expression matching a property line.")
  4178. (defvar org-font-lock-hook nil
  4179. "Functions to be called for special font lock stuff.")
  4180. (defun org-font-lock-hook (limit)
  4181. (run-hook-with-args 'org-font-lock-hook limit))
  4182. (defun org-set-font-lock-defaults ()
  4183. (let* ((em org-fontify-emphasized-text)
  4184. (lk org-activate-links)
  4185. (org-font-lock-extra-keywords
  4186. (list
  4187. ;; Call the hook
  4188. '(org-font-lock-hook)
  4189. ;; Headlines
  4190. `(,(if org-fontify-whole-heading-line
  4191. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4192. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4193. (1 (org-get-level-face 1))
  4194. (2 (org-get-level-face 2))
  4195. (3 (org-get-level-face 3)))
  4196. ;; Table lines
  4197. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4198. (1 'org-table t))
  4199. ;; Table internals
  4200. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4201. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4202. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4203. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4204. ;; Drawers
  4205. (list org-drawer-regexp '(0 'org-special-keyword t))
  4206. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4207. ;; Properties
  4208. (list org-property-re
  4209. '(1 'org-special-keyword t)
  4210. '(3 'org-property-value t))
  4211. ;; Links
  4212. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4213. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4214. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4215. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4216. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4217. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4218. (if (memq 'footnote lk) '(org-activate-footnote-links
  4219. (2 'org-footnote t)))
  4220. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4221. '(org-hide-wide-columns (0 nil append))
  4222. ;; TODO lines
  4223. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4224. '(1 (org-get-todo-face 1) t))
  4225. ;; DONE
  4226. (if org-fontify-done-headline
  4227. (list (concat "^[*]+ +\\<\\("
  4228. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4229. "\\)\\(.*\\)")
  4230. '(2 'org-headline-done t))
  4231. nil)
  4232. ;; Priorities
  4233. '(org-font-lock-add-priority-faces)
  4234. ;; Tags
  4235. '(org-font-lock-add-tag-faces)
  4236. ;; Special keywords
  4237. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4238. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4239. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4240. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4241. ;; Emphasis
  4242. (if em
  4243. (if (featurep 'xemacs)
  4244. '(org-do-emphasis-faces (0 nil append))
  4245. '(org-do-emphasis-faces)))
  4246. ;; Checkboxes
  4247. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4248. 2 'org-checkbox prepend)
  4249. (if org-provide-checkbox-statistics
  4250. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4251. (0 (org-get-checkbox-statistics-face) t)))
  4252. ;; Description list items
  4253. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4254. 2 'bold prepend)
  4255. ;; ARCHIVEd headings
  4256. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4257. '(1 'org-archived prepend))
  4258. ;; Specials
  4259. '(org-do-latex-and-special-faces)
  4260. ;; Code
  4261. '(org-activate-code (1 'org-code t))
  4262. ;; COMMENT
  4263. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4264. "\\|" org-quote-string "\\)\\>")
  4265. '(1 'org-special-keyword t))
  4266. '("^#.*" (0 'font-lock-comment-face t))
  4267. ;; Blocks and meta lines
  4268. '(org-fontify-meta-lines-and-blocks)
  4269. )))
  4270. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4271. ;; Now set the full font-lock-keywords
  4272. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4273. (org-set-local 'font-lock-defaults
  4274. '(org-font-lock-keywords t nil nil backward-paragraph))
  4275. (kill-local-variable 'font-lock-keywords) nil))
  4276. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4277. "Fontify string S like in Org-mode"
  4278. (with-temp-buffer
  4279. (insert s)
  4280. (let ((org-odd-levels-only odd-levels))
  4281. (org-mode)
  4282. (font-lock-fontify-buffer)
  4283. (buffer-string))))
  4284. (defvar org-m nil)
  4285. (defvar org-l nil)
  4286. (defvar org-f nil)
  4287. (defun org-get-level-face (n)
  4288. "Get the right face for match N in font-lock matching of headlines."
  4289. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4290. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4291. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4292. (cond
  4293. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4294. ((eq n 2) org-f)
  4295. (t (if org-level-color-stars-only nil org-f))))
  4296. (defun org-get-todo-face (kwd)
  4297. "Get the right face for a TODO keyword KWD.
  4298. If KWD is a number, get the corresponding match group."
  4299. (if (numberp kwd) (setq kwd (match-string kwd)))
  4300. (or (cdr (assoc kwd org-todo-keyword-faces))
  4301. (and (member kwd org-done-keywords) 'org-done)
  4302. 'org-todo))
  4303. (defun org-font-lock-add-tag-faces (limit)
  4304. "Add the special tag faces."
  4305. (when (and org-tag-faces org-tags-special-faces-re)
  4306. (while (re-search-forward org-tags-special-faces-re limit t)
  4307. (add-text-properties (match-beginning 1) (match-end 1)
  4308. (list 'face (org-get-tag-face 1)
  4309. 'font-lock-fontified t))
  4310. (backward-char 1))))
  4311. (defun org-font-lock-add-priority-faces (limit)
  4312. "Add the special priority faces."
  4313. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4314. (add-text-properties
  4315. (match-beginning 0) (match-end 0)
  4316. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4317. org-priority-faces))
  4318. 'org-special-keyword)
  4319. 'font-lock-fontified t))))
  4320. (defun org-get-tag-face (kwd)
  4321. "Get the right face for a TODO keyword KWD.
  4322. If KWD is a number, get the corresponding match group."
  4323. (if (numberp kwd) (setq kwd (match-string kwd)))
  4324. (or (cdr (assoc kwd org-tag-faces))
  4325. 'org-tag))
  4326. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4327. "Remove fontification and activation overlays from links."
  4328. (font-lock-default-unfontify-region beg end)
  4329. (let* ((buffer-undo-list t)
  4330. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4331. (inhibit-modification-hooks t)
  4332. deactivate-mark buffer-file-name buffer-file-truename)
  4333. (remove-text-properties beg end
  4334. '(mouse-face t keymap t org-linked-text t
  4335. invisible t intangible t
  4336. line-prefix t wrap-prefix t
  4337. org-no-flyspell t))))
  4338. ;;;; Visibility cycling, including org-goto and indirect buffer
  4339. ;;; Cycling
  4340. (defvar org-cycle-global-status nil)
  4341. (make-variable-buffer-local 'org-cycle-global-status)
  4342. (defvar org-cycle-subtree-status nil)
  4343. (make-variable-buffer-local 'org-cycle-subtree-status)
  4344. ;;;###autoload
  4345. (defvar org-inlinetask-min-level)
  4346. (defun org-cycle (&optional arg)
  4347. "TAB-action and visibility cycling for Org-mode.
  4348. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4349. is outine visibility cycling, but it also invokes other actions
  4350. in special contexts.
  4351. - When this function is called with a prefix argument, rotate the entire
  4352. buffer through 3 states (global cycling)
  4353. 1. OVERVIEW: Show only top-level headlines.
  4354. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4355. 3. SHOW ALL: Show everything.
  4356. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4357. determined by the variable `org-startup-folded', and by any VISIBILITY
  4358. properties in the buffer.
  4359. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4360. including any drawers.
  4361. - When inside a table, re-align the table and move to the next field.
  4362. - When point is at the beginning of a headline, rotate the subtree started
  4363. by this line through 3 different states (local cycling)
  4364. 1. FOLDED: Only the main headline is shown.
  4365. 2. CHILDREN: The main headline and the direct children are shown.
  4366. From this state, you can move to one of the children
  4367. and zoom in further.
  4368. 3. SUBTREE: Show the entire subtree, including body text.
  4369. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4370. - When there is a numeric prefix, go up to a heading with level ARG, do
  4371. a `show-subtree' and return to the previous cursor position. If ARG
  4372. is negative, go up that many levels.
  4373. - When point is not at the beginning of a headline, execute the global
  4374. binding for TAB, which is re-indenting the line. See the option
  4375. `org-cycle-emulate-tab' for details.
  4376. - Special case: if point is at the beginning of the buffer and there is
  4377. no headline in line 1, this function will act as if called with prefix arg.
  4378. But only if also the variable `org-cycle-global-at-bob' is t."
  4379. (interactive "P")
  4380. (org-load-modules-maybe)
  4381. (unless (run-hook-with-args-until-success 'org-tab-first-hook)
  4382. (let* ((limit-level
  4383. (or org-cycle-max-level
  4384. (and (boundp 'org-inlinetask-min-level)
  4385. org-inlinetask-min-level
  4386. (1- org-inlinetask-min-level))))
  4387. (nstars (and limit-level
  4388. (if org-odd-levels-only
  4389. (and limit-level (1- (* limit-level 2)))
  4390. limit-level)))
  4391. (outline-regexp
  4392. (cond
  4393. ((not (org-mode-p)) outline-regexp)
  4394. ((or (eq org-cycle-include-plain-lists 'integrate)
  4395. (and org-cycle-include-plain-lists (org-at-item-p)))
  4396. (concat "\\(?:\\*"
  4397. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4398. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4399. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4400. (bob-special (and org-cycle-global-at-bob (bobp)
  4401. (not (looking-at outline-regexp))))
  4402. (org-cycle-hook
  4403. (if bob-special
  4404. (delq 'org-optimize-window-after-visibility-change
  4405. (copy-sequence org-cycle-hook))
  4406. org-cycle-hook))
  4407. (pos (point)))
  4408. (if (or bob-special (equal arg '(4)))
  4409. ;; special case: use global cycling
  4410. (setq arg t))
  4411. (cond
  4412. ((equal arg '(16))
  4413. (org-set-startup-visibility)
  4414. (message "Startup visibility, plus VISIBILITY properties"))
  4415. ((equal arg '(64))
  4416. (show-all)
  4417. (message "Entire buffer visible, including drawers"))
  4418. ((org-at-table-p 'any)
  4419. ;; Enter the table or move to the next field in the table
  4420. (or (org-table-recognize-table.el)
  4421. (progn
  4422. (if arg (org-table-edit-field t)
  4423. (org-table-justify-field-maybe)
  4424. (call-interactively 'org-table-next-field)))))
  4425. ((run-hook-with-args-until-success
  4426. 'org-tab-after-check-for-table-hook))
  4427. ((eq arg t) ;; Global cycling
  4428. (org-cycle-internal-global))
  4429. ((and org-drawers org-drawer-regexp
  4430. (save-excursion
  4431. (beginning-of-line 1)
  4432. (looking-at org-drawer-regexp)))
  4433. ;; Toggle block visibility
  4434. (org-flag-drawer
  4435. (not (get-char-property (match-end 0) 'invisible))))
  4436. ((integerp arg)
  4437. ;; Show-subtree, ARG levels up from here.
  4438. (save-excursion
  4439. (org-back-to-heading)
  4440. (outline-up-heading (if (< arg 0) (- arg)
  4441. (- (funcall outline-level) arg)))
  4442. (org-show-subtree)))
  4443. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4444. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4445. (org-cycle-internal-local))
  4446. ;; TAB emulation and template completion
  4447. (buffer-read-only (org-back-to-heading))
  4448. ((run-hook-with-args-until-success
  4449. 'org-tab-after-check-for-cycling-hook))
  4450. ((org-try-structure-completion))
  4451. ((org-try-cdlatex-tab))
  4452. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4453. (or (not (bolp))
  4454. (not (looking-at outline-regexp))))
  4455. (call-interactively (global-key-binding "\t")))
  4456. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4457. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4458. (or (and (eq org-cycle-emulate-tab 'white)
  4459. (= (match-end 0) (point-at-eol)))
  4460. (and (eq org-cycle-emulate-tab 'whitestart)
  4461. (>= (match-end 0) pos))))
  4462. t
  4463. (eq org-cycle-emulate-tab t))
  4464. (call-interactively (global-key-binding "\t")))
  4465. (t (save-excursion
  4466. (org-back-to-heading)
  4467. (org-cycle)))))))
  4468. (defun org-cycle-internal-global ()
  4469. "Do the global cycling action."
  4470. (cond
  4471. ((and (eq last-command this-command)
  4472. (eq org-cycle-global-status 'overview))
  4473. ;; We just created the overview - now do table of contents
  4474. ;; This can be slow in very large buffers, so indicate action
  4475. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4476. (message "CONTENTS...")
  4477. (org-content)
  4478. (message "CONTENTS...done")
  4479. (setq org-cycle-global-status 'contents)
  4480. (run-hook-with-args 'org-cycle-hook 'contents))
  4481. ((and (eq last-command this-command)
  4482. (eq org-cycle-global-status 'contents))
  4483. ;; We just showed the table of contents - now show everything
  4484. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4485. (show-all)
  4486. (message "SHOW ALL")
  4487. (setq org-cycle-global-status 'all)
  4488. (run-hook-with-args 'org-cycle-hook 'all))
  4489. (t
  4490. ;; Default action: go to overview
  4491. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4492. (org-overview)
  4493. (message "OVERVIEW")
  4494. (setq org-cycle-global-status 'overview)
  4495. (run-hook-with-args 'org-cycle-hook 'overview))))
  4496. (defun org-cycle-internal-local ()
  4497. "Do the local cycling action."
  4498. (org-back-to-heading)
  4499. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4500. ;; First, some boundaries
  4501. (save-excursion
  4502. (org-back-to-heading)
  4503. (setq level (funcall outline-level))
  4504. (save-excursion
  4505. (beginning-of-line 2)
  4506. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4507. ; XEmacs does not have `next-single-char-property-change'
  4508. ; I'm not sure about Emacs 21.
  4509. (while (and (not (eobp)) ;; this is like `next-line'
  4510. (get-char-property (1- (point)) 'invisible))
  4511. (beginning-of-line 2))
  4512. (while (and (not (eobp)) ;; this is like `next-line'
  4513. (get-char-property (1- (point)) 'invisible))
  4514. (goto-char (next-single-char-property-change (point) 'invisible))
  4515. ;;;??? (or (bolp) (beginning-of-line 2))))
  4516. (and (eolp) (beginning-of-line 2))))
  4517. (setq eol (point)))
  4518. (outline-end-of-heading) (setq eoh (point))
  4519. (save-excursion
  4520. (outline-next-heading)
  4521. (setq has-children (and (org-at-heading-p t)
  4522. (> (funcall outline-level) level))))
  4523. (org-end-of-subtree t)
  4524. (unless (eobp)
  4525. (skip-chars-forward " \t\n")
  4526. (beginning-of-line 1) ; in case this is an item
  4527. )
  4528. (setq eos (if (eobp) (point) (1- (point)))))
  4529. ;; Find out what to do next and set `this-command'
  4530. (cond
  4531. ((= eos eoh)
  4532. ;; Nothing is hidden behind this heading
  4533. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4534. (message "EMPTY ENTRY")
  4535. (setq org-cycle-subtree-status nil)
  4536. (save-excursion
  4537. (goto-char eos)
  4538. (outline-next-heading)
  4539. (if (org-invisible-p) (org-flag-heading nil))))
  4540. ((and (or (>= eol eos)
  4541. (not (string-match "\\S-" (buffer-substring eol eos))))
  4542. (or has-children
  4543. (not (setq children-skipped
  4544. org-cycle-skip-children-state-if-no-children))))
  4545. ;; Entire subtree is hidden in one line: children view
  4546. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4547. (org-show-entry)
  4548. (show-children)
  4549. (message "CHILDREN")
  4550. (save-excursion
  4551. (goto-char eos)
  4552. (outline-next-heading)
  4553. (if (org-invisible-p) (org-flag-heading nil)))
  4554. (setq org-cycle-subtree-status 'children)
  4555. (run-hook-with-args 'org-cycle-hook 'children))
  4556. ((or children-skipped
  4557. (and (eq last-command this-command)
  4558. (eq org-cycle-subtree-status 'children)))
  4559. ;; We just showed the children, or no children are there,
  4560. ;; now show everything.
  4561. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4562. (org-show-subtree)
  4563. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4564. (setq org-cycle-subtree-status 'subtree)
  4565. (run-hook-with-args 'org-cycle-hook 'subtree))
  4566. (t
  4567. ;; Default action: hide the subtree.
  4568. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4569. (hide-subtree)
  4570. (message "FOLDED")
  4571. (setq org-cycle-subtree-status 'folded)
  4572. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4573. ;;;###autoload
  4574. (defun org-global-cycle (&optional arg)
  4575. "Cycle the global visibility. For details see `org-cycle'.
  4576. With C-u prefix arg, switch to startup visibility.
  4577. With a numeric prefix, show all headlines up to that level."
  4578. (interactive "P")
  4579. (let ((org-cycle-include-plain-lists
  4580. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4581. (cond
  4582. ((integerp arg)
  4583. (show-all)
  4584. (hide-sublevels arg)
  4585. (setq org-cycle-global-status 'contents))
  4586. ((equal arg '(4))
  4587. (org-set-startup-visibility)
  4588. (message "Startup visibility, plus VISIBILITY properties."))
  4589. (t
  4590. (org-cycle '(4))))))
  4591. (defun org-set-startup-visibility ()
  4592. "Set the visibility required by startup options and properties."
  4593. (cond
  4594. ((eq org-startup-folded t)
  4595. (org-cycle '(4)))
  4596. ((eq org-startup-folded 'content)
  4597. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4598. (org-cycle '(4)) (org-cycle '(4)))))
  4599. (unless (eq org-startup-folded 'showeverything)
  4600. (if org-hide-block-startup (org-hide-block-all))
  4601. (org-set-visibility-according-to-property 'no-cleanup)
  4602. (org-cycle-hide-archived-subtrees 'all)
  4603. (org-cycle-hide-drawers 'all)
  4604. (org-cycle-show-empty-lines 'all)))
  4605. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4606. "Switch subtree visibilities according to :VISIBILITY: property."
  4607. (interactive)
  4608. (let (org-show-entry-below state)
  4609. (save-excursion
  4610. (goto-char (point-min))
  4611. (while (re-search-forward
  4612. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4613. nil t)
  4614. (setq state (match-string 1))
  4615. (save-excursion
  4616. (org-back-to-heading t)
  4617. (hide-subtree)
  4618. (org-reveal)
  4619. (cond
  4620. ((equal state '("fold" "folded"))
  4621. (hide-subtree))
  4622. ((equal state "children")
  4623. (org-show-hidden-entry)
  4624. (show-children))
  4625. ((equal state "content")
  4626. (save-excursion
  4627. (save-restriction
  4628. (org-narrow-to-subtree)
  4629. (org-content))))
  4630. ((member state '("all" "showall"))
  4631. (show-subtree)))))
  4632. (unless no-cleanup
  4633. (org-cycle-hide-archived-subtrees 'all)
  4634. (org-cycle-hide-drawers 'all)
  4635. (org-cycle-show-empty-lines 'all)))))
  4636. (defun org-overview ()
  4637. "Switch to overview mode, showing only top-level headlines.
  4638. Really, this shows all headlines with level equal or greater than the level
  4639. of the first headline in the buffer. This is important, because if the
  4640. first headline is not level one, then (hide-sublevels 1) gives confusing
  4641. results."
  4642. (interactive)
  4643. (let ((level (save-excursion
  4644. (goto-char (point-min))
  4645. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4646. (progn
  4647. (goto-char (match-beginning 0))
  4648. (funcall outline-level))))))
  4649. (and level (hide-sublevels level))))
  4650. (defun org-content (&optional arg)
  4651. "Show all headlines in the buffer, like a table of contents.
  4652. With numerical argument N, show content up to level N."
  4653. (interactive "P")
  4654. (save-excursion
  4655. ;; Visit all headings and show their offspring
  4656. (and (integerp arg) (org-overview))
  4657. (goto-char (point-max))
  4658. (catch 'exit
  4659. (while (and (progn (condition-case nil
  4660. (outline-previous-visible-heading 1)
  4661. (error (goto-char (point-min))))
  4662. t)
  4663. (looking-at outline-regexp))
  4664. (if (integerp arg)
  4665. (show-children (1- arg))
  4666. (show-branches))
  4667. (if (bobp) (throw 'exit nil))))))
  4668. (defun org-optimize-window-after-visibility-change (state)
  4669. "Adjust the window after a change in outline visibility.
  4670. This function is the default value of the hook `org-cycle-hook'."
  4671. (when (get-buffer-window (current-buffer))
  4672. (cond
  4673. ((eq state 'content) nil)
  4674. ((eq state 'all) nil)
  4675. ((eq state 'folded) nil)
  4676. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4677. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4678. ;; FIXME: no longer in use
  4679. (defun org-compact-display-after-subtree-move ()
  4680. "Show a compacter version of the tree of the entry's parent."
  4681. (save-excursion
  4682. (if (org-up-heading-safe)
  4683. (progn
  4684. (hide-subtree)
  4685. (show-entry)
  4686. (show-children)
  4687. (org-cycle-show-empty-lines 'children)
  4688. (org-cycle-hide-drawers 'children))
  4689. (org-overview))))
  4690. (defun org-remove-empty-overlays-at (pos)
  4691. "Remove outline overlays that do not contain non-white stuff."
  4692. (mapc
  4693. (lambda (o)
  4694. (and (eq 'outline (org-overlay-get o 'invisible))
  4695. (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
  4696. (org-overlay-end o))))
  4697. (org-delete-overlay o)))
  4698. (org-overlays-at pos)))
  4699. (defun org-clean-visibility-after-subtree-move ()
  4700. "Fix visibility issues after moving a subtree."
  4701. ;; First, find a reasonable region to look at:
  4702. ;; Start two siblings above, end three below
  4703. (let* ((beg (save-excursion
  4704. (and (org-get-last-sibling)
  4705. (org-get-last-sibling))
  4706. (point)))
  4707. (end (save-excursion
  4708. (and (org-get-next-sibling)
  4709. (org-get-next-sibling)
  4710. (org-get-next-sibling))
  4711. (if (org-at-heading-p)
  4712. (point-at-eol)
  4713. (point))))
  4714. (level (looking-at "\\*+"))
  4715. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  4716. (save-excursion
  4717. (save-restriction
  4718. (narrow-to-region beg end)
  4719. (when re
  4720. ;; Properly fold already folded siblings
  4721. (goto-char (point-min))
  4722. (while (re-search-forward re nil t)
  4723. (if (save-excursion (goto-char (point-at-eol)) (org-invisible-p))
  4724. (hide-entry))))
  4725. (org-cycle-show-empty-lines 'overview)
  4726. (org-cycle-hide-drawers 'overview)))))
  4727. (defun org-cycle-show-empty-lines (state)
  4728. "Show empty lines above all visible headlines.
  4729. The region to be covered depends on STATE when called through
  4730. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4731. entire buffer covered. Note that an empty line is only shown if there
  4732. are at least `org-cycle-separator-lines' empty lines before the headline."
  4733. (when (not (= org-cycle-separator-lines 0))
  4734. (save-excursion
  4735. (let* ((n (abs org-cycle-separator-lines))
  4736. (re (cond
  4737. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4738. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4739. (t (let ((ns (number-to-string (- n 2))))
  4740. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4741. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4742. beg end b e)
  4743. (cond
  4744. ((memq state '(overview contents t))
  4745. (setq beg (point-min) end (point-max)))
  4746. ((memq state '(children folded))
  4747. (setq beg (point) end (progn (org-end-of-subtree t t)
  4748. (beginning-of-line 2)
  4749. (point)))))
  4750. (when beg
  4751. (goto-char beg)
  4752. (while (re-search-forward re end t)
  4753. (unless (get-char-property (match-end 1) 'invisible)
  4754. (setq e (match-end 1))
  4755. (if (< org-cycle-separator-lines 0)
  4756. (setq b (save-excursion
  4757. (goto-char (match-beginning 0))
  4758. (org-back-over-empty-lines)
  4759. (if (save-excursion
  4760. (goto-char (max (point-min) (1- (point))))
  4761. (org-on-heading-p))
  4762. (1- (point))
  4763. (point))))
  4764. (setq b (match-beginning 1)))
  4765. (outline-flag-region b e nil)))))))
  4766. ;; Never hide empty lines at the end of the file.
  4767. (save-excursion
  4768. (goto-char (point-max))
  4769. (outline-previous-heading)
  4770. (outline-end-of-heading)
  4771. (if (and (looking-at "[ \t\n]+")
  4772. (= (match-end 0) (point-max)))
  4773. (outline-flag-region (point) (match-end 0) nil))))
  4774. (defun org-show-empty-lines-in-parent ()
  4775. "Move to the parent and re-show empty lines before visible headlines."
  4776. (save-excursion
  4777. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4778. (org-cycle-show-empty-lines context))))
  4779. (defun org-files-list ()
  4780. "Return `org-agenda-files' list, plus all open org-mode files.
  4781. This is useful for operations that need to scan all of a user's
  4782. open and agenda-wise Org files."
  4783. (let ((files (mapcar 'expand-file-name org-agenda-files)))
  4784. (dolist (buf (buffer-list))
  4785. (with-current-buffer buf
  4786. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  4787. (let ((file (expand-file-name (buffer-file-name))))
  4788. (unless (member file files)
  4789. (push file files))))))
  4790. files))
  4791. (defsubst org-entry-beginning-position ()
  4792. "Return the beginning position of the current entry."
  4793. (save-excursion (outline-back-to-heading t) (point)))
  4794. (defsubst org-entry-end-position ()
  4795. "Return the end position of the current entry."
  4796. (save-excursion (outline-next-heading) (point)))
  4797. (defun org-cycle-hide-drawers (state)
  4798. "Re-hide all drawers after a visibility state change."
  4799. (when (and (org-mode-p)
  4800. (not (memq state '(overview folded contents))))
  4801. (save-excursion
  4802. (let* ((globalp (memq state '(contents all)))
  4803. (beg (if globalp (point-min) (point)))
  4804. (end (if globalp (point-max)
  4805. (if (eq state 'children)
  4806. (save-excursion (outline-next-heading) (point))
  4807. (org-end-of-subtree t)))))
  4808. (goto-char beg)
  4809. (while (re-search-forward org-drawer-regexp end t)
  4810. (org-flag-drawer t))))))
  4811. (defun org-flag-drawer (flag)
  4812. (save-excursion
  4813. (beginning-of-line 1)
  4814. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4815. (let ((b (match-end 0))
  4816. (outline-regexp org-outline-regexp))
  4817. (if (re-search-forward
  4818. "^[ \t]*:END:"
  4819. (save-excursion (outline-next-heading) (point)) t)
  4820. (outline-flag-region b (point-at-eol) flag)
  4821. (error ":END: line missing at position %s" b))))))
  4822. (defun org-subtree-end-visible-p ()
  4823. "Is the end of the current subtree visible?"
  4824. (pos-visible-in-window-p
  4825. (save-excursion (org-end-of-subtree t) (point))))
  4826. (defun org-first-headline-recenter (&optional N)
  4827. "Move cursor to the first headline and recenter the headline.
  4828. Optional argument N means, put the headline into the Nth line of the window."
  4829. (goto-char (point-min))
  4830. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4831. (beginning-of-line)
  4832. (recenter (prefix-numeric-value N))))
  4833. ;;; Folding of blocks
  4834. (defconst org-block-regexp
  4835. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  4836. "Regular expression for hiding blocks.")
  4837. (defvar org-hide-block-overlays nil
  4838. "Overays hiding blocks.")
  4839. (make-variable-buffer-local 'org-hide-block-overlays)
  4840. (defun org-block-map (function &optional start end)
  4841. "Call func at the head of all source blocks in the current
  4842. buffer. Optional arguments START and END can be used to limit
  4843. the range."
  4844. (let ((start (or start (point-min)))
  4845. (end (or end (point-max))))
  4846. (save-excursion
  4847. (goto-char start)
  4848. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  4849. (save-excursion
  4850. (save-match-data
  4851. (goto-char (match-beginning 0))
  4852. (funcall function)))))))
  4853. (defun org-hide-block-toggle-all ()
  4854. "Toggle the visibility of all blocks in the current buffer."
  4855. (org-block-map #'org-hide-block-toggle))
  4856. (defun org-hide-block-all ()
  4857. "Fold all blocks in the current buffer."
  4858. (interactive)
  4859. (org-show-block-all)
  4860. (org-block-map #'org-hide-block-toggle-maybe))
  4861. (defun org-show-block-all ()
  4862. "Unfold all blocks in the current buffer."
  4863. (mapc 'org-delete-overlay org-hide-block-overlays)
  4864. (setq org-hide-block-overlays nil))
  4865. (defun org-hide-block-toggle-maybe ()
  4866. "Toggle visibility of block at point."
  4867. (interactive)
  4868. (let ((case-fold-search t))
  4869. (if (save-excursion
  4870. (beginning-of-line 1)
  4871. (looking-at org-block-regexp))
  4872. (progn (org-hide-block-toggle)
  4873. t) ;; to signal that we took action
  4874. nil))) ;; to signal that we did not
  4875. (defun org-hide-block-toggle (&optional force)
  4876. "Toggle the visibility of the current block."
  4877. (interactive)
  4878. (save-excursion
  4879. (beginning-of-line)
  4880. (if (re-search-forward org-block-regexp nil t)
  4881. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  4882. (end (match-end 0)) ;; end of entire body
  4883. ov)
  4884. (if (memq t (mapcar (lambda (overlay)
  4885. (eq (org-overlay-get overlay 'invisible)
  4886. 'org-hide-block))
  4887. (org-overlays-at start)))
  4888. (if (or (not force) (eq force 'off))
  4889. (mapc (lambda (ov)
  4890. (when (member ov org-hide-block-overlays)
  4891. (setq org-hide-block-overlays
  4892. (delq ov org-hide-block-overlays)))
  4893. (when (eq (org-overlay-get ov 'invisible)
  4894. 'org-hide-block)
  4895. (org-delete-overlay ov)))
  4896. (org-overlays-at start)))
  4897. (setq ov (org-make-overlay start end))
  4898. (org-overlay-put ov 'invisible 'org-hide-block)
  4899. ;; make the block accessible to isearch
  4900. (org-overlay-put
  4901. ov 'isearch-open-invisible
  4902. (lambda (ov)
  4903. (when (member ov org-hide-block-overlays)
  4904. (setq org-hide-block-overlays
  4905. (delq ov org-hide-block-overlays)))
  4906. (when (eq (org-overlay-get ov 'invisible)
  4907. 'org-hide-block)
  4908. (org-delete-overlay ov))))
  4909. (push ov org-hide-block-overlays)))
  4910. (error "Not looking at a source block"))))
  4911. ;; org-tab-after-check-for-cycling-hook
  4912. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  4913. ;; Remove overlays when changing major mode
  4914. (add-hook 'org-mode-hook
  4915. (lambda () (org-add-hook 'change-major-mode-hook
  4916. 'org-show-block-all 'append 'local)))
  4917. ;;; Org-goto
  4918. (defvar org-goto-window-configuration nil)
  4919. (defvar org-goto-marker nil)
  4920. (defvar org-goto-map
  4921. (let ((map (make-sparse-keymap)))
  4922. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4923. (while (setq cmd (pop cmds))
  4924. (substitute-key-definition cmd cmd map global-map)))
  4925. (suppress-keymap map)
  4926. (org-defkey map "\C-m" 'org-goto-ret)
  4927. (org-defkey map [(return)] 'org-goto-ret)
  4928. (org-defkey map [(left)] 'org-goto-left)
  4929. (org-defkey map [(right)] 'org-goto-right)
  4930. (org-defkey map [(control ?g)] 'org-goto-quit)
  4931. (org-defkey map "\C-i" 'org-cycle)
  4932. (org-defkey map [(tab)] 'org-cycle)
  4933. (org-defkey map [(down)] 'outline-next-visible-heading)
  4934. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4935. (if org-goto-auto-isearch
  4936. (if (fboundp 'define-key-after)
  4937. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4938. nil)
  4939. (org-defkey map "q" 'org-goto-quit)
  4940. (org-defkey map "n" 'outline-next-visible-heading)
  4941. (org-defkey map "p" 'outline-previous-visible-heading)
  4942. (org-defkey map "f" 'outline-forward-same-level)
  4943. (org-defkey map "b" 'outline-backward-same-level)
  4944. (org-defkey map "u" 'outline-up-heading))
  4945. (org-defkey map "/" 'org-occur)
  4946. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4947. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4948. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4949. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4950. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4951. map))
  4952. (defconst org-goto-help
  4953. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4954. RET=jump to location [Q]uit and return to previous location
  4955. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4956. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4957. ;; FIXME: Docstring doe not mention both interfaces
  4958. (defun org-goto (&optional alternative-interface)
  4959. "Look up a different location in the current file, keeping current visibility.
  4960. When you want look-up or go to a different location in a document, the
  4961. fastest way is often to fold the entire buffer and then dive into the tree.
  4962. This method has the disadvantage, that the previous location will be folded,
  4963. which may not be what you want.
  4964. This command works around this by showing a copy of the current buffer
  4965. in an indirect buffer, in overview mode. You can dive into the tree in
  4966. that copy, use org-occur and incremental search to find a location.
  4967. When pressing RET or `Q', the command returns to the original buffer in
  4968. which the visibility is still unchanged. After RET is will also jump to
  4969. the location selected in the indirect buffer and expose the
  4970. the headline hierarchy above."
  4971. (interactive "P")
  4972. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4973. (org-refile-use-outline-path t)
  4974. (org-refile-target-verify-function nil)
  4975. (interface
  4976. (if (not alternative-interface)
  4977. org-goto-interface
  4978. (if (eq org-goto-interface 'outline)
  4979. 'outline-path-completion
  4980. 'outline)))
  4981. (org-goto-start-pos (point))
  4982. (selected-point
  4983. (if (eq interface 'outline)
  4984. (car (org-get-location (current-buffer) org-goto-help))
  4985. (nth 3 (org-refile-get-location "Goto: ")))))
  4986. (if selected-point
  4987. (progn
  4988. (org-mark-ring-push org-goto-start-pos)
  4989. (goto-char selected-point)
  4990. (if (or (org-invisible-p) (org-invisible-p2))
  4991. (org-show-context 'org-goto)))
  4992. (message "Quit"))))
  4993. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4994. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4995. (defvar org-goto-local-auto-isearch-map) ; defined below
  4996. (defun org-get-location (buf help)
  4997. "Let the user select a location in the Org-mode buffer BUF.
  4998. This function uses a recursive edit. It returns the selected position
  4999. or nil."
  5000. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5001. (isearch-hide-immediately nil)
  5002. (isearch-search-fun-function
  5003. (lambda () 'org-goto-local-search-headings))
  5004. (org-goto-selected-point org-goto-exit-command))
  5005. (save-excursion
  5006. (save-window-excursion
  5007. (delete-other-windows)
  5008. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5009. (switch-to-buffer
  5010. (condition-case nil
  5011. (make-indirect-buffer (current-buffer) "*org-goto*")
  5012. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5013. (with-output-to-temp-buffer "*Help*"
  5014. (princ help))
  5015. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5016. (setq buffer-read-only nil)
  5017. (let ((org-startup-truncated t)
  5018. (org-startup-folded nil)
  5019. (org-startup-align-all-tables nil))
  5020. (org-mode)
  5021. (org-overview))
  5022. (setq buffer-read-only t)
  5023. (if (and (boundp 'org-goto-start-pos)
  5024. (integer-or-marker-p org-goto-start-pos))
  5025. (let ((org-show-hierarchy-above t)
  5026. (org-show-siblings t)
  5027. (org-show-following-heading t))
  5028. (goto-char org-goto-start-pos)
  5029. (and (org-invisible-p) (org-show-context)))
  5030. (goto-char (point-min)))
  5031. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5032. (message "Select location and press RET")
  5033. (use-local-map org-goto-map)
  5034. (recursive-edit)
  5035. ))
  5036. (kill-buffer "*org-goto*")
  5037. (cons org-goto-selected-point org-goto-exit-command)))
  5038. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5039. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5040. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5041. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5042. (defun org-goto-local-search-headings (string bound noerror)
  5043. "Search and make sure that any matches are in headlines."
  5044. (catch 'return
  5045. (while (if isearch-forward
  5046. (search-forward string bound noerror)
  5047. (search-backward string bound noerror))
  5048. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5049. (and (member :headline context)
  5050. (not (member :tags context))))
  5051. (throw 'return (point))))))
  5052. (defun org-goto-local-auto-isearch ()
  5053. "Start isearch."
  5054. (interactive)
  5055. (goto-char (point-min))
  5056. (let ((keys (this-command-keys)))
  5057. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5058. (isearch-mode t)
  5059. (isearch-process-search-char (string-to-char keys)))))
  5060. (defun org-goto-ret (&optional arg)
  5061. "Finish `org-goto' by going to the new location."
  5062. (interactive "P")
  5063. (setq org-goto-selected-point (point)
  5064. org-goto-exit-command 'return)
  5065. (throw 'exit nil))
  5066. (defun org-goto-left ()
  5067. "Finish `org-goto' by going to the new location."
  5068. (interactive)
  5069. (if (org-on-heading-p)
  5070. (progn
  5071. (beginning-of-line 1)
  5072. (setq org-goto-selected-point (point)
  5073. org-goto-exit-command 'left)
  5074. (throw 'exit nil))
  5075. (error "Not on a heading")))
  5076. (defun org-goto-right ()
  5077. "Finish `org-goto' by going to the new location."
  5078. (interactive)
  5079. (if (org-on-heading-p)
  5080. (progn
  5081. (setq org-goto-selected-point (point)
  5082. org-goto-exit-command 'right)
  5083. (throw 'exit nil))
  5084. (error "Not on a heading")))
  5085. (defun org-goto-quit ()
  5086. "Finish `org-goto' without cursor motion."
  5087. (interactive)
  5088. (setq org-goto-selected-point nil)
  5089. (setq org-goto-exit-command 'quit)
  5090. (throw 'exit nil))
  5091. ;;; Indirect buffer display of subtrees
  5092. (defvar org-indirect-dedicated-frame nil
  5093. "This is the frame being used for indirect tree display.")
  5094. (defvar org-last-indirect-buffer nil)
  5095. (defun org-tree-to-indirect-buffer (&optional arg)
  5096. "Create indirect buffer and narrow it to current subtree.
  5097. With numerical prefix ARG, go up to this level and then take that tree.
  5098. If ARG is negative, go up that many levels.
  5099. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5100. indirect buffer previously made with this command, to avoid proliferation of
  5101. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5102. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5103. is kept so that you can work with several indirect buffers at the same time.
  5104. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5105. requests that a new frame be made for the new buffer, so that the dedicated
  5106. frame is not changed."
  5107. (interactive "P")
  5108. (let ((cbuf (current-buffer))
  5109. (cwin (selected-window))
  5110. (pos (point))
  5111. beg end level heading ibuf)
  5112. (save-excursion
  5113. (org-back-to-heading t)
  5114. (when (numberp arg)
  5115. (setq level (org-outline-level))
  5116. (if (< arg 0) (setq arg (+ level arg)))
  5117. (while (> (setq level (org-outline-level)) arg)
  5118. (outline-up-heading 1 t)))
  5119. (setq beg (point)
  5120. heading (org-get-heading))
  5121. (org-end-of-subtree t t) (setq end (point)))
  5122. (if (and (buffer-live-p org-last-indirect-buffer)
  5123. (not (eq org-indirect-buffer-display 'new-frame))
  5124. (not arg))
  5125. (kill-buffer org-last-indirect-buffer))
  5126. (setq ibuf (org-get-indirect-buffer cbuf)
  5127. org-last-indirect-buffer ibuf)
  5128. (cond
  5129. ((or (eq org-indirect-buffer-display 'new-frame)
  5130. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5131. (select-frame (make-frame))
  5132. (delete-other-windows)
  5133. (switch-to-buffer ibuf)
  5134. (org-set-frame-title heading))
  5135. ((eq org-indirect-buffer-display 'dedicated-frame)
  5136. (raise-frame
  5137. (select-frame (or (and org-indirect-dedicated-frame
  5138. (frame-live-p org-indirect-dedicated-frame)
  5139. org-indirect-dedicated-frame)
  5140. (setq org-indirect-dedicated-frame (make-frame)))))
  5141. (delete-other-windows)
  5142. (switch-to-buffer ibuf)
  5143. (org-set-frame-title (concat "Indirect: " heading)))
  5144. ((eq org-indirect-buffer-display 'current-window)
  5145. (switch-to-buffer ibuf))
  5146. ((eq org-indirect-buffer-display 'other-window)
  5147. (pop-to-buffer ibuf))
  5148. (t (error "Invalid value")))
  5149. (if (featurep 'xemacs)
  5150. (save-excursion (org-mode) (turn-on-font-lock)))
  5151. (narrow-to-region beg end)
  5152. (show-all)
  5153. (goto-char pos)
  5154. (and (window-live-p cwin) (select-window cwin))))
  5155. (defun org-get-indirect-buffer (&optional buffer)
  5156. (setq buffer (or buffer (current-buffer)))
  5157. (let ((n 1) (base (buffer-name buffer)) bname)
  5158. (while (buffer-live-p
  5159. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5160. (setq n (1+ n)))
  5161. (condition-case nil
  5162. (make-indirect-buffer buffer bname 'clone)
  5163. (error (make-indirect-buffer buffer bname)))))
  5164. (defun org-set-frame-title (title)
  5165. "Set the title of the current frame to the string TITLE."
  5166. ;; FIXME: how to name a single frame in XEmacs???
  5167. (unless (featurep 'xemacs)
  5168. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5169. ;;;; Structure editing
  5170. ;;; Inserting headlines
  5171. (defun org-previous-line-empty-p ()
  5172. (save-excursion
  5173. (and (not (bobp))
  5174. (or (beginning-of-line 0) t)
  5175. (save-match-data
  5176. (looking-at "[ \t]*$")))))
  5177. (defun org-insert-heading (&optional force-heading)
  5178. "Insert a new heading or item with same depth at point.
  5179. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5180. If point is at the beginning of a headline, insert a sibling before the
  5181. current headline. If point is not at the beginning, do not split the line,
  5182. but create the new headline after the current line."
  5183. (interactive "P")
  5184. (if (= (buffer-size) 0)
  5185. (insert "\n* ")
  5186. (when (or force-heading (not (org-insert-item)))
  5187. (let* ((empty-line-p nil)
  5188. (head (save-excursion
  5189. (condition-case nil
  5190. (progn
  5191. (org-back-to-heading)
  5192. (setq empty-line-p (org-previous-line-empty-p))
  5193. (match-string 0))
  5194. (error "*"))))
  5195. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5196. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5197. pos hide-previous previous-pos)
  5198. (cond
  5199. ((and (org-on-heading-p) (bolp)
  5200. (or (bobp)
  5201. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5202. ;; insert before the current line
  5203. (open-line (if blank 2 1)))
  5204. ((and (bolp)
  5205. (not org-insert-heading-respect-content)
  5206. (or (bobp)
  5207. (save-excursion
  5208. (backward-char 1) (not (org-invisible-p)))))
  5209. ;; insert right here
  5210. nil)
  5211. (t
  5212. ;; somewhere in the line
  5213. (save-excursion
  5214. (setq previous-pos (point-at-bol))
  5215. (end-of-line)
  5216. (setq hide-previous (org-invisible-p)))
  5217. (and org-insert-heading-respect-content (org-show-subtree))
  5218. (let ((split
  5219. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5220. (save-excursion
  5221. (let ((p (point)))
  5222. (goto-char (point-at-bol))
  5223. (and (looking-at org-complex-heading-regexp)
  5224. (> p (match-beginning 4)))))))
  5225. tags pos)
  5226. (cond
  5227. (org-insert-heading-respect-content
  5228. (org-end-of-subtree nil t)
  5229. (or (bolp) (newline))
  5230. (or (org-previous-line-empty-p)
  5231. (and blank (newline)))
  5232. (open-line 1))
  5233. ((org-on-heading-p)
  5234. (when hide-previous
  5235. (show-children)
  5236. (org-show-entry))
  5237. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5238. (setq tags (and (match-end 2) (match-string 2)))
  5239. (and (match-end 1)
  5240. (delete-region (match-beginning 1) (match-end 1)))
  5241. (setq pos (point-at-bol))
  5242. (or split (end-of-line 1))
  5243. (delete-horizontal-space)
  5244. (newline (if blank 2 1))
  5245. (when tags
  5246. (save-excursion
  5247. (goto-char pos)
  5248. (end-of-line 1)
  5249. (insert " " tags)
  5250. (org-set-tags nil 'align))))
  5251. (t
  5252. (or split (end-of-line 1))
  5253. (newline (if blank 2 1)))))))
  5254. (insert head) (just-one-space)
  5255. (setq pos (point))
  5256. (end-of-line 1)
  5257. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5258. (when (and org-insert-heading-respect-content hide-previous)
  5259. (save-excursion
  5260. (goto-char previous-pos)
  5261. (hide-subtree)))
  5262. (run-hooks 'org-insert-heading-hook)))))
  5263. (defun org-get-heading (&optional no-tags)
  5264. "Return the heading of the current entry, without the stars."
  5265. (save-excursion
  5266. (org-back-to-heading t)
  5267. (if (looking-at
  5268. (if no-tags
  5269. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5270. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5271. (match-string 1) "")))
  5272. (defun org-heading-components ()
  5273. "Return the components of the current heading.
  5274. This is a list with the following elements:
  5275. - the level as an integer
  5276. - the reduced level, different if `org-odd-levels-only' is set.
  5277. - the TODO keyword, or nil
  5278. - the priority character, like ?A, or nil if no priority is given
  5279. - the headline text itself, or the tags string if no headline text
  5280. - the tags string, or nil."
  5281. (save-excursion
  5282. (org-back-to-heading t)
  5283. (if (looking-at org-complex-heading-regexp)
  5284. (list (length (match-string 1))
  5285. (org-reduced-level (length (match-string 1)))
  5286. (org-match-string-no-properties 2)
  5287. (and (match-end 3) (aref (match-string 3) 2))
  5288. (org-match-string-no-properties 4)
  5289. (org-match-string-no-properties 5)))))
  5290. (defun org-get-entry ()
  5291. "Get the entry text, after heading, entire subtree."
  5292. (save-excursion
  5293. (org-back-to-heading t)
  5294. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5295. (defun org-insert-heading-after-current ()
  5296. "Insert a new heading with same level as current, after current subtree."
  5297. (interactive)
  5298. (org-back-to-heading)
  5299. (org-insert-heading)
  5300. (org-move-subtree-down)
  5301. (end-of-line 1))
  5302. (defun org-insert-heading-respect-content ()
  5303. (interactive)
  5304. (let ((org-insert-heading-respect-content t))
  5305. (org-insert-heading t)))
  5306. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5307. (interactive "P")
  5308. (let ((org-insert-heading-respect-content t))
  5309. (org-insert-todo-heading force-state t)))
  5310. (defun org-insert-todo-heading (arg &optional force-heading)
  5311. "Insert a new heading with the same level and TODO state as current heading.
  5312. If the heading has no TODO state, or if the state is DONE, use the first
  5313. state (TODO by default). Also with prefix arg, force first state."
  5314. (interactive "P")
  5315. (when (or force-heading (not (org-insert-item 'checkbox)))
  5316. (org-insert-heading force-heading)
  5317. (save-excursion
  5318. (org-back-to-heading)
  5319. (outline-previous-heading)
  5320. (looking-at org-todo-line-regexp))
  5321. (let*
  5322. ((new-mark-x
  5323. (if (or arg
  5324. (not (match-beginning 2))
  5325. (member (match-string 2) org-done-keywords))
  5326. (car org-todo-keywords-1)
  5327. (match-string 2)))
  5328. (new-mark
  5329. (or
  5330. (run-hook-with-args-until-success
  5331. 'org-todo-get-default-hook new-mark-x nil)
  5332. new-mark-x)))
  5333. (beginning-of-line 1)
  5334. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5335. (if org-treat-insert-todo-heading-as-state-change
  5336. (org-todo new-mark)
  5337. (insert new-mark " "))))
  5338. (when org-provide-todo-statistics
  5339. (org-update-parent-todo-statistics))))
  5340. (defun org-insert-subheading (arg)
  5341. "Insert a new subheading and demote it.
  5342. Works for outline headings and for plain lists alike."
  5343. (interactive "P")
  5344. (org-insert-heading arg)
  5345. (cond
  5346. ((org-on-heading-p) (org-do-demote))
  5347. ((org-at-item-p) (org-indent-item 1))))
  5348. (defun org-insert-todo-subheading (arg)
  5349. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5350. Works for outline headings and for plain lists alike."
  5351. (interactive "P")
  5352. (org-insert-todo-heading arg)
  5353. (cond
  5354. ((org-on-heading-p) (org-do-demote))
  5355. ((org-at-item-p) (org-indent-item 1))))
  5356. ;;; Promotion and Demotion
  5357. (defvar org-after-demote-entry-hook nil
  5358. "Hook run after an entry has been demoted.
  5359. The cursor will be at the beginning of the entry.
  5360. When a subtree is being demoted, the hook will be called for each node.")
  5361. (defvar org-after-promote-entry-hook nil
  5362. "Hook run after an entry has been promoted.
  5363. The cursor will be at the beginning of the entry.
  5364. When a subtree is being promoted, the hook will be called for each node.")
  5365. (defun org-promote-subtree ()
  5366. "Promote the entire subtree.
  5367. See also `org-promote'."
  5368. (interactive)
  5369. (save-excursion
  5370. (org-map-tree 'org-promote))
  5371. (org-fix-position-after-promote))
  5372. (defun org-demote-subtree ()
  5373. "Demote the entire subtree. See `org-demote'.
  5374. See also `org-promote'."
  5375. (interactive)
  5376. (save-excursion
  5377. (org-map-tree 'org-demote))
  5378. (org-fix-position-after-promote))
  5379. (defun org-do-promote ()
  5380. "Promote the current heading higher up the tree.
  5381. If the region is active in `transient-mark-mode', promote all headings
  5382. in the region."
  5383. (interactive)
  5384. (save-excursion
  5385. (if (org-region-active-p)
  5386. (org-map-region 'org-promote (region-beginning) (region-end))
  5387. (org-promote)))
  5388. (org-fix-position-after-promote))
  5389. (defun org-do-demote ()
  5390. "Demote the current heading lower down the tree.
  5391. If the region is active in `transient-mark-mode', demote all headings
  5392. in the region."
  5393. (interactive)
  5394. (save-excursion
  5395. (if (org-region-active-p)
  5396. (org-map-region 'org-demote (region-beginning) (region-end))
  5397. (org-demote)))
  5398. (org-fix-position-after-promote))
  5399. (defun org-fix-position-after-promote ()
  5400. "Make sure that after pro/demotion cursor position is right."
  5401. (let ((pos (point)))
  5402. (when (save-excursion
  5403. (beginning-of-line 1)
  5404. (looking-at org-todo-line-regexp)
  5405. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5406. (cond ((eobp) (insert " "))
  5407. ((eolp) (insert " "))
  5408. ((equal (char-after) ?\ ) (forward-char 1))))))
  5409. (defun org-reduced-level (l)
  5410. "Compute the effective level of a heading.
  5411. This takes into account the setting of `org-odd-levels-only'."
  5412. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5413. (defun org-get-valid-level (level &optional change)
  5414. "Rectify a level change under the influence of `org-odd-levels-only'
  5415. LEVEL is a current level, CHANGE is by how much the level should be
  5416. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5417. even level numbers will become the next higher odd number."
  5418. (if org-odd-levels-only
  5419. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5420. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5421. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5422. (max 1 (+ level (or change 0)))))
  5423. (if (boundp 'define-obsolete-function-alias)
  5424. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5425. (define-obsolete-function-alias 'org-get-legal-level
  5426. 'org-get-valid-level)
  5427. (define-obsolete-function-alias 'org-get-legal-level
  5428. 'org-get-valid-level "23.1")))
  5429. (defun org-promote ()
  5430. "Promote the current heading higher up the tree.
  5431. If the region is active in `transient-mark-mode', promote all headings
  5432. in the region."
  5433. (org-back-to-heading t)
  5434. (let* ((level (save-match-data (funcall outline-level)))
  5435. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5436. (diff (abs (- level (length up-head) -1))))
  5437. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5438. (replace-match up-head nil t)
  5439. ;; Fixup tag positioning
  5440. (and org-auto-align-tags (org-set-tags nil t))
  5441. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5442. (run-hooks 'org-after-promote-entry-hook)))
  5443. (defun org-demote ()
  5444. "Demote the current heading lower down the tree.
  5445. If the region is active in `transient-mark-mode', demote all headings
  5446. in the region."
  5447. (org-back-to-heading t)
  5448. (let* ((level (save-match-data (funcall outline-level)))
  5449. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5450. (diff (abs (- level (length down-head) -1))))
  5451. (replace-match down-head nil t)
  5452. ;; Fixup tag positioning
  5453. (and org-auto-align-tags (org-set-tags nil t))
  5454. (if org-adapt-indentation (org-fixup-indentation diff))
  5455. (run-hooks 'org-after-demote-entry-hook)))
  5456. (defun org-map-tree (fun)
  5457. "Call FUN for every heading underneath the current one."
  5458. (org-back-to-heading)
  5459. (let ((level (funcall outline-level)))
  5460. (save-excursion
  5461. (funcall fun)
  5462. (while (and (progn
  5463. (outline-next-heading)
  5464. (> (funcall outline-level) level))
  5465. (not (eobp)))
  5466. (funcall fun)))))
  5467. (defun org-map-region (fun beg end)
  5468. "Call FUN for every heading between BEG and END."
  5469. (let ((org-ignore-region t))
  5470. (save-excursion
  5471. (setq end (copy-marker end))
  5472. (goto-char beg)
  5473. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5474. (< (point) end))
  5475. (funcall fun))
  5476. (while (and (progn
  5477. (outline-next-heading)
  5478. (< (point) end))
  5479. (not (eobp)))
  5480. (funcall fun)))))
  5481. (defun org-fixup-indentation (diff)
  5482. "Change the indentation in the current entry by DIFF
  5483. However, if any line in the current entry has no indentation, or if it
  5484. would end up with no indentation after the change, nothing at all is done."
  5485. (save-excursion
  5486. (let ((end (save-excursion (outline-next-heading)
  5487. (point-marker)))
  5488. (prohibit (if (> diff 0)
  5489. "^\\S-"
  5490. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5491. col)
  5492. (unless (save-excursion (end-of-line 1)
  5493. (re-search-forward prohibit end t))
  5494. (while (and (< (point) end)
  5495. (re-search-forward "^[ \t]+" end t))
  5496. (goto-char (match-end 0))
  5497. (setq col (current-column))
  5498. (if (< diff 0) (replace-match ""))
  5499. (org-indent-to-column (+ diff col))))
  5500. (move-marker end nil))))
  5501. (defun org-convert-to-odd-levels ()
  5502. "Convert an org-mode file with all levels allowed to one with odd levels.
  5503. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5504. level 5 etc."
  5505. (interactive)
  5506. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5507. (let ((outline-regexp org-outline-regexp)
  5508. (outline-level 'org-outline-level)
  5509. (org-odd-levels-only nil) n)
  5510. (save-excursion
  5511. (goto-char (point-min))
  5512. (while (re-search-forward "^\\*\\*+ " nil t)
  5513. (setq n (- (length (match-string 0)) 2))
  5514. (while (>= (setq n (1- n)) 0)
  5515. (org-demote))
  5516. (end-of-line 1))))))
  5517. (defun org-convert-to-oddeven-levels ()
  5518. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5519. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5520. section with an even level, conversion would destroy the structure of the file. An error
  5521. is signaled in this case."
  5522. (interactive)
  5523. (goto-char (point-min))
  5524. ;; First check if there are no even levels
  5525. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5526. (org-show-context t)
  5527. (error "Not all levels are odd in this file. Conversion not possible"))
  5528. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5529. (let ((outline-regexp org-outline-regexp)
  5530. (outline-level 'org-outline-level)
  5531. (org-odd-levels-only nil) n)
  5532. (save-excursion
  5533. (goto-char (point-min))
  5534. (while (re-search-forward "^\\*\\*+ " nil t)
  5535. (setq n (/ (1- (length (match-string 0))) 2))
  5536. (while (>= (setq n (1- n)) 0)
  5537. (org-promote))
  5538. (end-of-line 1))))))
  5539. (defun org-tr-level (n)
  5540. "Make N odd if required."
  5541. (if org-odd-levels-only (1+ (/ n 2)) n))
  5542. ;;; Vertical tree motion, cutting and pasting of subtrees
  5543. (defun org-move-subtree-up (&optional arg)
  5544. "Move the current subtree up past ARG headlines of the same level."
  5545. (interactive "p")
  5546. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5547. (defun org-move-subtree-down (&optional arg)
  5548. "Move the current subtree down past ARG headlines of the same level."
  5549. (interactive "p")
  5550. (setq arg (prefix-numeric-value arg))
  5551. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  5552. 'org-get-last-sibling))
  5553. (ins-point (make-marker))
  5554. (cnt (abs arg))
  5555. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5556. ;; Select the tree
  5557. (org-back-to-heading)
  5558. (setq beg0 (point))
  5559. (save-excursion
  5560. (setq ne-beg (org-back-over-empty-lines))
  5561. (setq beg (point)))
  5562. (save-match-data
  5563. (save-excursion (outline-end-of-heading)
  5564. (setq folded (org-invisible-p)))
  5565. (outline-end-of-subtree))
  5566. (outline-next-heading)
  5567. (setq ne-end (org-back-over-empty-lines))
  5568. (setq end (point))
  5569. (goto-char beg0)
  5570. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5571. ;; include less whitespace
  5572. (save-excursion
  5573. (goto-char beg)
  5574. (forward-line (- ne-beg ne-end))
  5575. (setq beg (point))))
  5576. ;; Find insertion point, with error handling
  5577. (while (> cnt 0)
  5578. (or (and (funcall movfunc) (looking-at outline-regexp))
  5579. (progn (goto-char beg0)
  5580. (error "Cannot move past superior level or buffer limit")))
  5581. (setq cnt (1- cnt)))
  5582. (if (> arg 0)
  5583. ;; Moving forward - still need to move over subtree
  5584. (progn (org-end-of-subtree t t)
  5585. (save-excursion
  5586. (org-back-over-empty-lines)
  5587. (or (bolp) (newline)))))
  5588. (setq ne-ins (org-back-over-empty-lines))
  5589. (move-marker ins-point (point))
  5590. (setq txt (buffer-substring beg end))
  5591. (org-save-markers-in-region beg end)
  5592. (delete-region beg end)
  5593. (org-remove-empty-overlays-at beg)
  5594. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5595. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5596. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5597. (let ((bbb (point)))
  5598. (insert-before-markers txt)
  5599. (org-reinstall-markers-in-region bbb)
  5600. (move-marker ins-point bbb))
  5601. (or (bolp) (insert "\n"))
  5602. (setq ins-end (point))
  5603. (goto-char ins-point)
  5604. (org-skip-whitespace)
  5605. (when (and (< arg 0)
  5606. (org-first-sibling-p)
  5607. (> ne-ins ne-beg))
  5608. ;; Move whitespace back to beginning
  5609. (save-excursion
  5610. (goto-char ins-end)
  5611. (let ((kill-whole-line t))
  5612. (kill-line (- ne-ins ne-beg)) (point)))
  5613. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5614. (move-marker ins-point nil)
  5615. (if folded
  5616. (hide-subtree)
  5617. (org-show-entry)
  5618. (show-children)
  5619. (org-cycle-hide-drawers 'children))
  5620. (org-clean-visibility-after-subtree-move)))
  5621. (defvar org-subtree-clip ""
  5622. "Clipboard for cut and paste of subtrees.
  5623. This is actually only a copy of the kill, because we use the normal kill
  5624. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5625. (defvar org-subtree-clip-folded nil
  5626. "Was the last copied subtree folded?
  5627. This is used to fold the tree back after pasting.")
  5628. (defun org-cut-subtree (&optional n)
  5629. "Cut the current subtree into the clipboard.
  5630. With prefix arg N, cut this many sequential subtrees.
  5631. This is a short-hand for marking the subtree and then cutting it."
  5632. (interactive "p")
  5633. (org-copy-subtree n 'cut))
  5634. (defun org-copy-subtree (&optional n cut force-store-markers)
  5635. "Cut the current subtree into the clipboard.
  5636. With prefix arg N, cut this many sequential subtrees.
  5637. This is a short-hand for marking the subtree and then copying it.
  5638. If CUT is non-nil, actually cut the subtree.
  5639. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5640. of some markers in the region, even if CUT is non-nil. This is
  5641. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5642. (interactive "p")
  5643. (let (beg end folded (beg0 (point)))
  5644. (if (interactive-p)
  5645. (org-back-to-heading nil) ; take what looks like a subtree
  5646. (org-back-to-heading t)) ; take what is really there
  5647. (org-back-over-empty-lines)
  5648. (setq beg (point))
  5649. (skip-chars-forward " \t\r\n")
  5650. (save-match-data
  5651. (save-excursion (outline-end-of-heading)
  5652. (setq folded (org-invisible-p)))
  5653. (condition-case nil
  5654. (org-forward-same-level (1- n) t)
  5655. (error nil))
  5656. (org-end-of-subtree t t))
  5657. (org-back-over-empty-lines)
  5658. (setq end (point))
  5659. (goto-char beg0)
  5660. (when (> end beg)
  5661. (setq org-subtree-clip-folded folded)
  5662. (when (or cut force-store-markers)
  5663. (org-save-markers-in-region beg end))
  5664. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5665. (setq org-subtree-clip (current-kill 0))
  5666. (message "%s: Subtree(s) with %d characters"
  5667. (if cut "Cut" "Copied")
  5668. (length org-subtree-clip)))))
  5669. (defun org-paste-subtree (&optional level tree for-yank)
  5670. "Paste the clipboard as a subtree, with modification of headline level.
  5671. The entire subtree is promoted or demoted in order to match a new headline
  5672. level.
  5673. If the cursor is at the beginning of a headline, the same level as
  5674. that headline is used to paste the tree
  5675. If not, the new level is derived from the *visible* headings
  5676. before and after the insertion point, and taken to be the inferior headline
  5677. level of the two. So if the previous visible heading is level 3 and the
  5678. next is level 4 (or vice versa), level 4 will be used for insertion.
  5679. This makes sure that the subtree remains an independent subtree and does
  5680. not swallow low level entries.
  5681. You can also force a different level, either by using a numeric prefix
  5682. argument, or by inserting the heading marker by hand. For example, if the
  5683. cursor is after \"*****\", then the tree will be shifted to level 5.
  5684. If optional TREE is given, use this text instead of the kill ring.
  5685. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5686. move back over whitespace before inserting, and move point to the end of
  5687. the inserted text when done."
  5688. (interactive "P")
  5689. (setq tree (or tree (and kill-ring (current-kill 0))))
  5690. (unless (org-kill-is-subtree-p tree)
  5691. (error "%s"
  5692. (substitute-command-keys
  5693. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5694. (let* ((visp (not (org-invisible-p)))
  5695. (txt tree)
  5696. (^re (concat "^\\(" outline-regexp "\\)"))
  5697. (re (concat "\\(" outline-regexp "\\)"))
  5698. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5699. (old-level (if (string-match ^re txt)
  5700. (- (match-end 0) (match-beginning 0) 1)
  5701. -1))
  5702. (force-level (cond (level (prefix-numeric-value level))
  5703. ((and (looking-at "[ \t]*$")
  5704. (string-match
  5705. ^re_ (buffer-substring
  5706. (point-at-bol) (point))))
  5707. (- (match-end 1) (match-beginning 1)))
  5708. ((and (bolp)
  5709. (looking-at org-outline-regexp))
  5710. (- (match-end 0) (point) 1))
  5711. (t nil)))
  5712. (previous-level (save-excursion
  5713. (condition-case nil
  5714. (progn
  5715. (outline-previous-visible-heading 1)
  5716. (if (looking-at re)
  5717. (- (match-end 0) (match-beginning 0) 1)
  5718. 1))
  5719. (error 1))))
  5720. (next-level (save-excursion
  5721. (condition-case nil
  5722. (progn
  5723. (or (looking-at outline-regexp)
  5724. (outline-next-visible-heading 1))
  5725. (if (looking-at re)
  5726. (- (match-end 0) (match-beginning 0) 1)
  5727. 1))
  5728. (error 1))))
  5729. (new-level (or force-level (max previous-level next-level)))
  5730. (shift (if (or (= old-level -1)
  5731. (= new-level -1)
  5732. (= old-level new-level))
  5733. 0
  5734. (- new-level old-level)))
  5735. (delta (if (> shift 0) -1 1))
  5736. (func (if (> shift 0) 'org-demote 'org-promote))
  5737. (org-odd-levels-only nil)
  5738. beg end newend)
  5739. ;; Remove the forced level indicator
  5740. (if force-level
  5741. (delete-region (point-at-bol) (point)))
  5742. ;; Paste
  5743. (beginning-of-line 1)
  5744. (unless for-yank (org-back-over-empty-lines))
  5745. (setq beg (point))
  5746. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5747. (insert-before-markers txt)
  5748. (unless (string-match "\n\\'" txt) (insert "\n"))
  5749. (setq newend (point))
  5750. (org-reinstall-markers-in-region beg)
  5751. (setq end (point))
  5752. (goto-char beg)
  5753. (skip-chars-forward " \t\n\r")
  5754. (setq beg (point))
  5755. (if (and (org-invisible-p) visp)
  5756. (save-excursion (outline-show-heading)))
  5757. ;; Shift if necessary
  5758. (unless (= shift 0)
  5759. (save-restriction
  5760. (narrow-to-region beg end)
  5761. (while (not (= shift 0))
  5762. (org-map-region func (point-min) (point-max))
  5763. (setq shift (+ delta shift)))
  5764. (goto-char (point-min))
  5765. (setq newend (point-max))))
  5766. (when (or (interactive-p) for-yank)
  5767. (message "Clipboard pasted as level %d subtree" new-level))
  5768. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5769. kill-ring
  5770. (eq org-subtree-clip (current-kill 0))
  5771. org-subtree-clip-folded)
  5772. ;; The tree was folded before it was killed/copied
  5773. (hide-subtree))
  5774. (and for-yank (goto-char newend))))
  5775. (defun org-kill-is-subtree-p (&optional txt)
  5776. "Check if the current kill is an outline subtree, or a set of trees.
  5777. Returns nil if kill does not start with a headline, or if the first
  5778. headline level is not the largest headline level in the tree.
  5779. So this will actually accept several entries of equal levels as well,
  5780. which is OK for `org-paste-subtree'.
  5781. If optional TXT is given, check this string instead of the current kill."
  5782. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5783. (start-level (and kill
  5784. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5785. org-outline-regexp "\\)")
  5786. kill)
  5787. (- (match-end 2) (match-beginning 2) 1)))
  5788. (re (concat "^" org-outline-regexp))
  5789. (start (1+ (or (match-beginning 2) -1))))
  5790. (if (not start-level)
  5791. (progn
  5792. nil) ;; does not even start with a heading
  5793. (catch 'exit
  5794. (while (setq start (string-match re kill (1+ start)))
  5795. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5796. (throw 'exit nil)))
  5797. t))))
  5798. (defvar org-markers-to-move nil
  5799. "Markers that should be moved with a cut-and-paste operation.
  5800. Those markers are stored together with their positions relative to
  5801. the start of the region.")
  5802. (defun org-save-markers-in-region (beg end)
  5803. "Check markers in region.
  5804. If these markers are between BEG and END, record their position relative
  5805. to BEG, so that after moving the block of text, we can put the markers back
  5806. into place.
  5807. This function gets called just before an entry or tree gets cut from the
  5808. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5809. called immediately, to move the markers with the entries."
  5810. (setq org-markers-to-move nil)
  5811. (when (featurep 'org-clock)
  5812. (org-clock-save-markers-for-cut-and-paste beg end))
  5813. (when (featurep 'org-agenda)
  5814. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5815. (defun org-check-and-save-marker (marker beg end)
  5816. "Check if MARKER is between BEG and END.
  5817. If yes, remember the marker and the distance to BEG."
  5818. (when (and (marker-buffer marker)
  5819. (equal (marker-buffer marker) (current-buffer)))
  5820. (if (and (>= marker beg) (< marker end))
  5821. (push (cons marker (- marker beg)) org-markers-to-move))))
  5822. (defun org-reinstall-markers-in-region (beg)
  5823. "Move all remembered markers to their position relative to BEG."
  5824. (mapc (lambda (x)
  5825. (move-marker (car x) (+ beg (cdr x))))
  5826. org-markers-to-move)
  5827. (setq org-markers-to-move nil))
  5828. (defun org-narrow-to-subtree ()
  5829. "Narrow buffer to the current subtree."
  5830. (interactive)
  5831. (save-excursion
  5832. (save-match-data
  5833. (narrow-to-region
  5834. (progn (org-back-to-heading t) (point))
  5835. (progn (org-end-of-subtree t t) (point))))))
  5836. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5837. "Clone the task (subtree) at point N times.
  5838. The clones will be inserted as siblings.
  5839. In interactive use, the user will be prompted for the number of clones
  5840. to be produced, and for a time SHIFT, which may be a repeater as used
  5841. in time stamps, for example `+3d'.
  5842. When a valid repeater is given and the entry contains any time stamps,
  5843. the clones will become a sequence in time, with time stamps in the
  5844. subtree shifted for each clone produced. If SHIFT is nil or the
  5845. empty string, time stamps will be left alone.
  5846. If the original subtree did contain time stamps with a repeater,
  5847. the following will happen:
  5848. - the repeater will be removed in each clone
  5849. - an additional clone will be produced, with the current, unshifted
  5850. date(s) in the entry.
  5851. - the original entry will be placed *after* all the clones, with
  5852. repeater intact.
  5853. - the start days in the repeater in the original entry will be shifted
  5854. to past the last clone.
  5855. I this way you can spell out a number of instances of a repeating task,
  5856. and still retain the repeater to cover future instances of the task."
  5857. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5858. (let (beg end template task
  5859. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5860. (if (not (and (integerp n) (> n 0)))
  5861. (error "Invalid number of replications %s" n))
  5862. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5863. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5864. shift)))
  5865. (error "Invalid shift specification %s" shift))
  5866. (when doshift
  5867. (setq shift-n (string-to-number (match-string 1 shift))
  5868. shift-what (cdr (assoc (match-string 2 shift)
  5869. '(("d" . day) ("w" . week)
  5870. ("m" . month) ("y" . year))))))
  5871. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5872. (setq nmin 1 nmax n)
  5873. (org-back-to-heading t)
  5874. (setq beg (point))
  5875. (org-end-of-subtree t t)
  5876. (setq end (point))
  5877. (setq template (buffer-substring beg end))
  5878. (when (and doshift
  5879. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5880. (delete-region beg end)
  5881. (setq end beg)
  5882. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5883. (goto-char end)
  5884. (loop for n from nmin to nmax do
  5885. (if (not doshift)
  5886. (setq task template)
  5887. (with-temp-buffer
  5888. (insert template)
  5889. (org-mode)
  5890. (goto-char (point-min))
  5891. (while (re-search-forward org-ts-regexp-both nil t)
  5892. (org-timestamp-change (* n shift-n) shift-what))
  5893. (unless (= n n-no-remove)
  5894. (goto-char (point-min))
  5895. (while (re-search-forward org-ts-regexp nil t)
  5896. (save-excursion
  5897. (goto-char (match-beginning 0))
  5898. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5899. (delete-region (match-beginning 1) (match-end 1))))))
  5900. (setq task (buffer-string))))
  5901. (insert task))
  5902. (goto-char beg)))
  5903. ;;; Outline Sorting
  5904. (defun org-sort (with-case)
  5905. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5906. Optional argument WITH-CASE means sort case-sensitively.
  5907. With a double prefix argument, also remove duplicate entries."
  5908. (interactive "P")
  5909. (if (org-at-table-p)
  5910. (org-call-with-arg 'org-table-sort-lines with-case)
  5911. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5912. (defun org-sort-remove-invisible (s)
  5913. (remove-text-properties 0 (length s) org-rm-props s)
  5914. (while (string-match org-bracket-link-regexp s)
  5915. (setq s (replace-match (if (match-end 2)
  5916. (match-string 3 s)
  5917. (match-string 1 s)) t t s)))
  5918. s)
  5919. (defvar org-priority-regexp) ; defined later in the file
  5920. (defvar org-after-sorting-entries-or-items-hook nil
  5921. "Hook that is run after a bunch of entries or items have been sorted.
  5922. When children are sorted, the cursor is in the parent line when this
  5923. hook gets called. When a region or a plain list is sorted, the cursor
  5924. will be in the first entry of the sorted region/list.")
  5925. (defun org-sort-entries-or-items
  5926. (&optional with-case sorting-type getkey-func compare-func property)
  5927. "Sort entries on a certain level of an outline tree, or plain list items.
  5928. If there is an active region, the entries in the region are sorted.
  5929. Else, if the cursor is before the first entry, sort the top-level items.
  5930. Else, the children of the entry at point are sorted.
  5931. If the cursor is at the first item in a plain list, the list items will be
  5932. sorted.
  5933. Sorting can be alphabetically, numerically, by date/time as given by
  5934. a time stamp, by a property or by priority.
  5935. The command prompts for the sorting type unless it has been given to the
  5936. function through the SORTING-TYPE argument, which needs to a character,
  5937. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  5938. precise meaning of each character:
  5939. n Numerically, by converting the beginning of the entry/item to a number.
  5940. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  5941. t By date/time, either the first active time stamp in the entry, or, if
  5942. none exist, by the first inactive one.
  5943. In items, only the first line will be chekced.
  5944. s By the scheduled date/time.
  5945. d By deadline date/time.
  5946. c By creation time, which is assumed to be the first inactive time stamp
  5947. at the beginning of a line.
  5948. p By priority according to the cookie.
  5949. r By the value of a property.
  5950. Capital letters will reverse the sort order.
  5951. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5952. called with point at the beginning of the record. It must return either
  5953. a string or a number that should serve as the sorting key for that record.
  5954. Comparing entries ignores case by default. However, with an optional argument
  5955. WITH-CASE, the sorting considers case as well."
  5956. (interactive "P")
  5957. (let ((case-func (if with-case 'identity 'downcase))
  5958. start beg end stars re re2
  5959. txt what tmp plain-list-p)
  5960. ;; Find beginning and end of region to sort
  5961. (cond
  5962. ((org-region-active-p)
  5963. ;; we will sort the region
  5964. (setq end (region-end)
  5965. what "region")
  5966. (goto-char (region-beginning))
  5967. (if (not (org-on-heading-p)) (outline-next-heading))
  5968. (setq start (point)))
  5969. ((org-at-item-p)
  5970. ;; we will sort this plain list
  5971. (org-beginning-of-item-list) (setq start (point))
  5972. (org-end-of-item-list) (setq end (point))
  5973. (goto-char start)
  5974. (setq plain-list-p t
  5975. what "plain list"))
  5976. ((or (org-on-heading-p)
  5977. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5978. ;; we will sort the children of the current headline
  5979. (org-back-to-heading)
  5980. (setq start (point)
  5981. end (progn (org-end-of-subtree t t)
  5982. (org-back-over-empty-lines)
  5983. (point))
  5984. what "children")
  5985. (goto-char start)
  5986. (show-subtree)
  5987. (outline-next-heading))
  5988. (t
  5989. ;; we will sort the top-level entries in this file
  5990. (goto-char (point-min))
  5991. (or (org-on-heading-p) (outline-next-heading))
  5992. (setq start (point) end (point-max) what "top-level")
  5993. (goto-char start)
  5994. (show-all)))
  5995. (setq beg (point))
  5996. (if (>= beg end) (error "Nothing to sort"))
  5997. (unless plain-list-p
  5998. (looking-at "\\(\\*+\\)")
  5999. (setq stars (match-string 1)
  6000. re (concat "^" (regexp-quote stars) " +")
  6001. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6002. txt (buffer-substring beg end))
  6003. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6004. (if (and (not (equal stars "*")) (string-match re2 txt))
  6005. (error "Region to sort contains a level above the first entry")))
  6006. (unless sorting-type
  6007. (message
  6008. (if plain-list-p
  6009. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6010. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6011. [t]ime [s]cheduled [d]eadline [c]reated
  6012. A/N/T/S/D/C/P/O/F means reversed:")
  6013. what)
  6014. (setq sorting-type (read-char-exclusive))
  6015. (and (= (downcase sorting-type) ?f)
  6016. (setq getkey-func
  6017. (org-icompleting-read "Sort using function: "
  6018. obarray 'fboundp t nil nil))
  6019. (setq getkey-func (intern getkey-func)))
  6020. (and (= (downcase sorting-type) ?r)
  6021. (setq property
  6022. (org-icompleting-read "Property: "
  6023. (mapcar 'list (org-buffer-property-keys t))
  6024. nil t))))
  6025. (message "Sorting entries...")
  6026. (save-restriction
  6027. (narrow-to-region start end)
  6028. (let ((dcst (downcase sorting-type))
  6029. (case-fold-search nil)
  6030. (now (current-time)))
  6031. (sort-subr
  6032. (/= dcst sorting-type)
  6033. ;; This function moves to the beginning character of the "record" to
  6034. ;; be sorted.
  6035. (if plain-list-p
  6036. (lambda nil
  6037. (if (org-at-item-p) t (goto-char (point-max))))
  6038. (lambda nil
  6039. (if (re-search-forward re nil t)
  6040. (goto-char (match-beginning 0))
  6041. (goto-char (point-max)))))
  6042. ;; This function moves to the last character of the "record" being
  6043. ;; sorted.
  6044. (if plain-list-p
  6045. 'org-end-of-item
  6046. (lambda nil
  6047. (save-match-data
  6048. (condition-case nil
  6049. (outline-forward-same-level 1)
  6050. (error
  6051. (goto-char (point-max)))))))
  6052. ;; This function returns the value that gets sorted against.
  6053. (if plain-list-p
  6054. (lambda nil
  6055. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6056. (cond
  6057. ((= dcst ?n)
  6058. (string-to-number (buffer-substring (match-end 0)
  6059. (point-at-eol))))
  6060. ((= dcst ?a)
  6061. (buffer-substring (match-end 0) (point-at-eol)))
  6062. ((= dcst ?t)
  6063. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  6064. (re-search-forward org-ts-regexp-both
  6065. (point-at-eol) t))
  6066. (org-time-string-to-seconds (match-string 0))
  6067. (org-float-time now)))
  6068. ((= dcst ?f)
  6069. (if getkey-func
  6070. (progn
  6071. (setq tmp (funcall getkey-func))
  6072. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6073. tmp)
  6074. (error "Invalid key function `%s'" getkey-func)))
  6075. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6076. (lambda nil
  6077. (cond
  6078. ((= dcst ?n)
  6079. (if (looking-at org-complex-heading-regexp)
  6080. (string-to-number (match-string 4))
  6081. nil))
  6082. ((= dcst ?a)
  6083. (if (looking-at org-complex-heading-regexp)
  6084. (funcall case-func (match-string 4))
  6085. nil))
  6086. ((= dcst ?t)
  6087. (let ((end (save-excursion (outline-next-heading) (point))))
  6088. (if (or (re-search-forward org-ts-regexp end t)
  6089. (re-search-forward org-ts-regexp-both end t))
  6090. (org-time-string-to-seconds (match-string 0))
  6091. (org-float-time now))))
  6092. ((= dcst ?c)
  6093. (let ((end (save-excursion (outline-next-heading) (point))))
  6094. (if (re-search-forward
  6095. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6096. end t)
  6097. (org-time-string-to-seconds (match-string 0))
  6098. (org-float-time now))))
  6099. ((= dcst ?s)
  6100. (let ((end (save-excursion (outline-next-heading) (point))))
  6101. (if (re-search-forward org-scheduled-time-regexp end t)
  6102. (org-time-string-to-seconds (match-string 1))
  6103. (org-float-time now))))
  6104. ((= dcst ?d)
  6105. (let ((end (save-excursion (outline-next-heading) (point))))
  6106. (if (re-search-forward org-deadline-time-regexp end t)
  6107. (org-time-string-to-seconds (match-string 1))
  6108. (org-float-time now))))
  6109. ((= dcst ?p)
  6110. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6111. (string-to-char (match-string 2))
  6112. org-default-priority))
  6113. ((= dcst ?r)
  6114. (or (org-entry-get nil property) ""))
  6115. ((= dcst ?o)
  6116. (if (looking-at org-complex-heading-regexp)
  6117. (- 9999 (length (member (match-string 2)
  6118. org-todo-keywords-1)))))
  6119. ((= dcst ?f)
  6120. (if getkey-func
  6121. (progn
  6122. (setq tmp (funcall getkey-func))
  6123. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6124. tmp)
  6125. (error "Invalid key function `%s'" getkey-func)))
  6126. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6127. nil
  6128. (cond
  6129. ((= dcst ?a) 'string<)
  6130. ((= dcst ?f) compare-func)
  6131. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6132. (t nil)))))
  6133. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6134. (message "Sorting entries...done")))
  6135. (defun org-do-sort (table what &optional with-case sorting-type)
  6136. "Sort TABLE of WHAT according to SORTING-TYPE.
  6137. The user will be prompted for the SORTING-TYPE if the call to this
  6138. function does not specify it. WHAT is only for the prompt, to indicate
  6139. what is being sorted. The sorting key will be extracted from
  6140. the car of the elements of the table.
  6141. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6142. (unless sorting-type
  6143. (message
  6144. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6145. what)
  6146. (setq sorting-type (read-char-exclusive)))
  6147. (let ((dcst (downcase sorting-type))
  6148. extractfun comparefun)
  6149. ;; Define the appropriate functions
  6150. (cond
  6151. ((= dcst ?n)
  6152. (setq extractfun 'string-to-number
  6153. comparefun (if (= dcst sorting-type) '< '>)))
  6154. ((= dcst ?a)
  6155. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6156. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6157. comparefun (if (= dcst sorting-type)
  6158. 'string<
  6159. (lambda (a b) (and (not (string< a b))
  6160. (not (string= a b)))))))
  6161. ((= dcst ?t)
  6162. (setq extractfun
  6163. (lambda (x)
  6164. (if (or (string-match org-ts-regexp x)
  6165. (string-match org-ts-regexp-both x))
  6166. (org-float-time
  6167. (org-time-string-to-time (match-string 0 x)))
  6168. 0))
  6169. comparefun (if (= dcst sorting-type) '< '>)))
  6170. (t (error "Invalid sorting type `%c'" sorting-type)))
  6171. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6172. table)
  6173. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6174. ;;; The orgstruct minor mode
  6175. ;; Define a minor mode which can be used in other modes in order to
  6176. ;; integrate the org-mode structure editing commands.
  6177. ;; This is really a hack, because the org-mode structure commands use
  6178. ;; keys which normally belong to the major mode. Here is how it
  6179. ;; works: The minor mode defines all the keys necessary to operate the
  6180. ;; structure commands, but wraps the commands into a function which
  6181. ;; tests if the cursor is currently at a headline or a plain list
  6182. ;; item. If that is the case, the structure command is used,
  6183. ;; temporarily setting many Org-mode variables like regular
  6184. ;; expressions for filling etc. However, when any of those keys is
  6185. ;; used at a different location, function uses `key-binding' to look
  6186. ;; up if the key has an associated command in another currently active
  6187. ;; keymap (minor modes, major mode, global), and executes that
  6188. ;; command. There might be problems if any of the keys is otherwise
  6189. ;; used as a prefix key.
  6190. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6191. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6192. ;; addresses this by checking explicitly for both bindings.
  6193. (defvar orgstruct-mode-map (make-sparse-keymap)
  6194. "Keymap for the minor `orgstruct-mode'.")
  6195. (defvar org-local-vars nil
  6196. "List of local variables, for use by `orgstruct-mode'")
  6197. ;;;###autoload
  6198. (define-minor-mode orgstruct-mode
  6199. "Toggle the minor more `orgstruct-mode'.
  6200. This mode is for using Org-mode structure commands in other modes.
  6201. The following key behave as if Org-mode was active, if the cursor
  6202. is on a headline, or on a plain list item (both in the definition
  6203. of Org-mode).
  6204. M-up Move entry/item up
  6205. M-down Move entry/item down
  6206. M-left Promote
  6207. M-right Demote
  6208. M-S-up Move entry/item up
  6209. M-S-down Move entry/item down
  6210. M-S-left Promote subtree
  6211. M-S-right Demote subtree
  6212. M-q Fill paragraph and items like in Org-mode
  6213. C-c ^ Sort entries
  6214. C-c - Cycle list bullet
  6215. TAB Cycle item visibility
  6216. M-RET Insert new heading/item
  6217. S-M-RET Insert new TODO heading / Checkbox item
  6218. C-c C-c Set tags / toggle checkbox"
  6219. nil " OrgStruct" nil
  6220. (org-load-modules-maybe)
  6221. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6222. ;;;###autoload
  6223. (defun turn-on-orgstruct ()
  6224. "Unconditionally turn on `orgstruct-mode'."
  6225. (orgstruct-mode 1))
  6226. (defun orgstruct++-mode (&optional arg)
  6227. "Toggle `orgstruct-mode', the enhanced version of it.
  6228. In addition to setting orgstruct-mode, this also exports all indentation
  6229. and autofilling variables from org-mode into the buffer. It will also
  6230. recognize item context in multiline items.
  6231. Note that turning off orgstruct-mode will *not* remove the
  6232. indentation/paragraph settings. This can only be done by refreshing the
  6233. major mode, for example with \\[normal-mode]."
  6234. (interactive "P")
  6235. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6236. (if (< arg 1)
  6237. (orgstruct-mode -1)
  6238. (orgstruct-mode 1)
  6239. (let (var val)
  6240. (mapc
  6241. (lambda (x)
  6242. (when (string-match
  6243. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6244. (symbol-name (car x)))
  6245. (setq var (car x) val (nth 1 x))
  6246. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6247. org-local-vars)
  6248. (org-set-local 'orgstruct-is-++ t))))
  6249. (defvar orgstruct-is-++ nil
  6250. "Is orgstruct-mode in ++ version in the current-buffer?")
  6251. (make-variable-buffer-local 'orgstruct-is-++)
  6252. ;;;###autoload
  6253. (defun turn-on-orgstruct++ ()
  6254. "Unconditionally turn on `orgstruct++-mode'."
  6255. (orgstruct++-mode 1))
  6256. (defun orgstruct-error ()
  6257. "Error when there is no default binding for a structure key."
  6258. (interactive)
  6259. (error "This key has no function outside structure elements"))
  6260. (defun orgstruct-setup ()
  6261. "Setup orgstruct keymaps."
  6262. (let ((nfunc 0)
  6263. (bindings
  6264. (list
  6265. '([(meta up)] org-metaup)
  6266. '([(meta down)] org-metadown)
  6267. '([(meta left)] org-metaleft)
  6268. '([(meta right)] org-metaright)
  6269. '([(meta shift up)] org-shiftmetaup)
  6270. '([(meta shift down)] org-shiftmetadown)
  6271. '([(meta shift left)] org-shiftmetaleft)
  6272. '([(meta shift right)] org-shiftmetaright)
  6273. '([?\e (up)] org-metaup)
  6274. '([?\e (down)] org-metadown)
  6275. '([?\e (left)] org-metaleft)
  6276. '([?\e (right)] org-metaright)
  6277. '([?\e (shift up)] org-shiftmetaup)
  6278. '([?\e (shift down)] org-shiftmetadown)
  6279. '([?\e (shift left)] org-shiftmetaleft)
  6280. '([?\e (shift right)] org-shiftmetaright)
  6281. '([(shift up)] org-shiftup)
  6282. '([(shift down)] org-shiftdown)
  6283. '([(shift left)] org-shiftleft)
  6284. '([(shift right)] org-shiftright)
  6285. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6286. '("\M-q" fill-paragraph)
  6287. '("\C-c^" org-sort)
  6288. '("\C-c-" org-cycle-list-bullet)))
  6289. elt key fun cmd)
  6290. (while (setq elt (pop bindings))
  6291. (setq nfunc (1+ nfunc))
  6292. (setq key (org-key (car elt))
  6293. fun (nth 1 elt)
  6294. cmd (orgstruct-make-binding fun nfunc key))
  6295. (org-defkey orgstruct-mode-map key cmd))
  6296. ;; Special treatment needed for TAB and RET
  6297. (org-defkey orgstruct-mode-map [(tab)]
  6298. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6299. (org-defkey orgstruct-mode-map "\C-i"
  6300. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6301. (org-defkey orgstruct-mode-map "\M-\C-m"
  6302. (orgstruct-make-binding 'org-insert-heading 105
  6303. "\M-\C-m" [(meta return)]))
  6304. (org-defkey orgstruct-mode-map [(meta return)]
  6305. (orgstruct-make-binding 'org-insert-heading 106
  6306. [(meta return)] "\M-\C-m"))
  6307. (org-defkey orgstruct-mode-map [(shift meta return)]
  6308. (orgstruct-make-binding 'org-insert-todo-heading 107
  6309. [(meta return)] "\M-\C-m"))
  6310. (org-defkey orgstruct-mode-map "\e\C-m"
  6311. (orgstruct-make-binding 'org-insert-heading 108
  6312. "\e\C-m" [?\e (return)]))
  6313. (org-defkey orgstruct-mode-map [?\e (return)]
  6314. (orgstruct-make-binding 'org-insert-heading 109
  6315. [?\e (return)] "\e\C-m"))
  6316. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6317. (orgstruct-make-binding 'org-insert-todo-heading 110
  6318. [?\e (return)] "\e\C-m"))
  6319. (unless org-local-vars
  6320. (setq org-local-vars (org-get-local-variables)))
  6321. t))
  6322. (defun orgstruct-make-binding (fun n &rest keys)
  6323. "Create a function for binding in the structure minor mode.
  6324. FUN is the command to call inside a table. N is used to create a unique
  6325. command name. KEYS are keys that should be checked in for a command
  6326. to execute outside of tables."
  6327. (eval
  6328. (list 'defun
  6329. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6330. '(arg)
  6331. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6332. "Outside of structure, run the binding of `"
  6333. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6334. "'.")
  6335. '(interactive "p")
  6336. (list 'if
  6337. `(org-context-p 'headline 'item
  6338. (and orgstruct-is-++
  6339. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6340. 'item-body))
  6341. (list 'org-run-like-in-org-mode (list 'quote fun))
  6342. (list 'let '(orgstruct-mode)
  6343. (list 'call-interactively
  6344. (append '(or)
  6345. (mapcar (lambda (k)
  6346. (list 'key-binding k))
  6347. keys)
  6348. '('orgstruct-error))))))))
  6349. (defun org-context-p (&rest contexts)
  6350. "Check if local context is any of CONTEXTS.
  6351. Possible values in the list of contexts are `table', `headline', and `item'."
  6352. (let ((pos (point)))
  6353. (goto-char (point-at-bol))
  6354. (prog1 (or (and (memq 'table contexts)
  6355. (looking-at "[ \t]*|"))
  6356. (and (memq 'headline contexts)
  6357. ;;????????? (looking-at "\\*+"))
  6358. (looking-at outline-regexp))
  6359. (and (memq 'item contexts)
  6360. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6361. (and (memq 'item-body contexts)
  6362. (org-in-item-p)))
  6363. (goto-char pos))))
  6364. (defun org-get-local-variables ()
  6365. "Return a list of all local variables in an org-mode buffer."
  6366. (let (varlist)
  6367. (with-current-buffer (get-buffer-create "*Org tmp*")
  6368. (erase-buffer)
  6369. (org-mode)
  6370. (setq varlist (buffer-local-variables)))
  6371. (kill-buffer "*Org tmp*")
  6372. (delq nil
  6373. (mapcar
  6374. (lambda (x)
  6375. (setq x
  6376. (if (symbolp x)
  6377. (list x)
  6378. (list (car x) (list 'quote (cdr x)))))
  6379. (if (string-match
  6380. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6381. (symbol-name (car x)))
  6382. x nil))
  6383. varlist))))
  6384. ;;;###autoload
  6385. (defun org-run-like-in-org-mode (cmd)
  6386. "Run a command, pretending that the current buffer is in Org-mode.
  6387. This will temporarily bind local variables that are typically bound in
  6388. Org-mode to the values they have in Org-mode, and then interactively
  6389. call CMD."
  6390. (org-load-modules-maybe)
  6391. (unless org-local-vars
  6392. (setq org-local-vars (org-get-local-variables)))
  6393. (eval (list 'let org-local-vars
  6394. (list 'call-interactively (list 'quote cmd)))))
  6395. ;;;; Archiving
  6396. (defun org-get-category (&optional pos)
  6397. "Get the category applying to position POS."
  6398. (get-text-property (or pos (point)) 'org-category))
  6399. (defun org-refresh-category-properties ()
  6400. "Refresh category text properties in the buffer."
  6401. (let ((def-cat (cond
  6402. ((null org-category)
  6403. (if buffer-file-name
  6404. (file-name-sans-extension
  6405. (file-name-nondirectory buffer-file-name))
  6406. "???"))
  6407. ((symbolp org-category) (symbol-name org-category))
  6408. (t org-category)))
  6409. beg end cat pos optionp)
  6410. (org-unmodified
  6411. (save-excursion
  6412. (save-restriction
  6413. (widen)
  6414. (goto-char (point-min))
  6415. (put-text-property (point) (point-max) 'org-category def-cat)
  6416. (while (re-search-forward
  6417. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6418. (setq pos (match-end 0)
  6419. optionp (equal (char-after (match-beginning 0)) ?#)
  6420. cat (org-trim (match-string 2)))
  6421. (if optionp
  6422. (setq beg (point-at-bol) end (point-max))
  6423. (org-back-to-heading t)
  6424. (setq beg (point) end (org-end-of-subtree t t)))
  6425. (put-text-property beg end 'org-category cat)
  6426. (goto-char pos)))))))
  6427. ;;;; Link Stuff
  6428. ;;; Link abbreviations
  6429. (defun org-link-expand-abbrev (link)
  6430. "Apply replacements as defined in `org-link-abbrev-alist."
  6431. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6432. (let* ((key (match-string 1 link))
  6433. (as (or (assoc key org-link-abbrev-alist-local)
  6434. (assoc key org-link-abbrev-alist)))
  6435. (tag (and (match-end 2) (match-string 3 link)))
  6436. rpl)
  6437. (if (not as)
  6438. link
  6439. (setq rpl (cdr as))
  6440. (cond
  6441. ((symbolp rpl) (funcall rpl tag))
  6442. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6443. ((string-match "%h" rpl)
  6444. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6445. (t (concat rpl tag)))))
  6446. link))
  6447. ;;; Storing and inserting links
  6448. (defvar org-insert-link-history nil
  6449. "Minibuffer history for links inserted with `org-insert-link'.")
  6450. (defvar org-stored-links nil
  6451. "Contains the links stored with `org-store-link'.")
  6452. (defvar org-store-link-plist nil
  6453. "Plist with info about the most recently link created with `org-store-link'.")
  6454. (defvar org-link-protocols nil
  6455. "Link protocols added to Org-mode using `org-add-link-type'.")
  6456. (defvar org-store-link-functions nil
  6457. "List of functions that are called to create and store a link.
  6458. Each function will be called in turn until one returns a non-nil
  6459. value. Each function should check if it is responsible for creating
  6460. this link (for example by looking at the major mode).
  6461. If not, it must exit and return nil.
  6462. If yes, it should return a non-nil value after a calling
  6463. `org-store-link-props' with a list of properties and values.
  6464. Special properties are:
  6465. :type The link prefix. like \"http\". This must be given.
  6466. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6467. This is obligatory as well.
  6468. :description Optional default description for the second pair
  6469. of brackets in an Org-mode link. The user can still change
  6470. this when inserting this link into an Org-mode buffer.
  6471. In addition to these, any additional properties can be specified
  6472. and then used in remember templates.")
  6473. (defun org-add-link-type (type &optional follow export)
  6474. "Add TYPE to the list of `org-link-types'.
  6475. Re-compute all regular expressions depending on `org-link-types'
  6476. FOLLOW and EXPORT are two functions.
  6477. FOLLOW should take the link path as the single argument and do whatever
  6478. is necessary to follow the link, for example find a file or display
  6479. a mail message.
  6480. EXPORT should format the link path for export to one of the export formats.
  6481. It should be a function accepting three arguments:
  6482. path the path of the link, the text after the prefix (like \"http:\")
  6483. desc the description of the link, if any, nil if there was no description
  6484. format the export format, a symbol like `html' or `latex'.
  6485. The function may use the FORMAT information to return different values
  6486. depending on the format. The return value will be put literally into
  6487. the exported file.
  6488. Org-mode has a built-in default for exporting links. If you are happy with
  6489. this default, there is no need to define an export function for the link
  6490. type. For a simple example of an export function, see `org-bbdb.el'."
  6491. (add-to-list 'org-link-types type t)
  6492. (org-make-link-regexps)
  6493. (if (assoc type org-link-protocols)
  6494. (setcdr (assoc type org-link-protocols) (list follow export))
  6495. (push (list type follow export) org-link-protocols)))
  6496. (defvar org-agenda-buffer-name)
  6497. ;;;###autoload
  6498. (defun org-store-link (arg)
  6499. "\\<org-mode-map>Store an org-link to the current location.
  6500. This link is added to `org-stored-links' and can later be inserted
  6501. into an org-buffer with \\[org-insert-link].
  6502. For some link types, a prefix arg is interpreted:
  6503. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6504. For file links, arg negates `org-context-in-file-links'."
  6505. (interactive "P")
  6506. (org-load-modules-maybe)
  6507. (setq org-store-link-plist nil) ; reset
  6508. (let ((outline-regexp (org-get-limited-outline-regexp))
  6509. link cpltxt desc description search txt custom-id)
  6510. (cond
  6511. ((run-hook-with-args-until-success 'org-store-link-functions)
  6512. (setq link (plist-get org-store-link-plist :link)
  6513. desc (or (plist-get org-store-link-plist :description) link)))
  6514. ((equal (buffer-name) "*Org Edit Src Example*")
  6515. (let (label gc)
  6516. (while (or (not label)
  6517. (save-excursion
  6518. (save-restriction
  6519. (widen)
  6520. (goto-char (point-min))
  6521. (re-search-forward
  6522. (regexp-quote (format org-coderef-label-format label))
  6523. nil t))))
  6524. (when label (message "Label exists already") (sit-for 2))
  6525. (setq label (read-string "Code line label: " label)))
  6526. (end-of-line 1)
  6527. (setq link (format org-coderef-label-format label))
  6528. (setq gc (- 79 (length link)))
  6529. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6530. (insert link)
  6531. (setq link (concat "(" label ")") desc nil)))
  6532. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  6533. ;; We are in the agenda, link to referenced location
  6534. (let ((m (or (get-text-property (point) 'org-hd-marker)
  6535. (get-text-property (point) 'org-marker))))
  6536. (when m
  6537. (org-with-point-at m
  6538. (call-interactively 'org-store-link)))))
  6539. ((eq major-mode 'calendar-mode)
  6540. (let ((cd (calendar-cursor-to-date)))
  6541. (setq link
  6542. (format-time-string
  6543. (car org-time-stamp-formats)
  6544. (apply 'encode-time
  6545. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6546. nil nil nil))))
  6547. (org-store-link-props :type "calendar" :date cd)))
  6548. ((eq major-mode 'w3-mode)
  6549. (setq cpltxt (if (and (buffer-name)
  6550. (not (string-match "Untitled" (buffer-name))))
  6551. (buffer-name)
  6552. (url-view-url t))
  6553. link (org-make-link (url-view-url t)))
  6554. (org-store-link-props :type "w3" :url (url-view-url t)))
  6555. ((eq major-mode 'w3m-mode)
  6556. (setq cpltxt (or w3m-current-title w3m-current-url)
  6557. link (org-make-link w3m-current-url))
  6558. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6559. ((setq search (run-hook-with-args-until-success
  6560. 'org-create-file-search-functions))
  6561. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6562. "::" search))
  6563. (setq cpltxt (or description link)))
  6564. ((eq major-mode 'image-mode)
  6565. (setq cpltxt (concat "file:"
  6566. (abbreviate-file-name buffer-file-name))
  6567. link (org-make-link cpltxt))
  6568. (org-store-link-props :type "image" :file buffer-file-name))
  6569. ((eq major-mode 'dired-mode)
  6570. ;; link to the file in the current line
  6571. (setq cpltxt (concat "file:"
  6572. (abbreviate-file-name
  6573. (expand-file-name
  6574. (dired-get-filename nil t))))
  6575. link (org-make-link cpltxt)))
  6576. ((and buffer-file-name (org-mode-p))
  6577. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6578. (cond
  6579. ((org-in-regexp "<<\\(.*?\\)>>")
  6580. (setq cpltxt
  6581. (concat "file:"
  6582. (abbreviate-file-name buffer-file-name)
  6583. "::" (match-string 1))
  6584. link (org-make-link cpltxt)))
  6585. ((and (featurep 'org-id)
  6586. (or (eq org-link-to-org-use-id t)
  6587. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6588. (interactive-p))
  6589. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6590. (interactive-p)
  6591. (not custom-id))
  6592. (and org-link-to-org-use-id
  6593. (condition-case nil
  6594. (org-entry-get nil "ID")
  6595. (error nil)))))
  6596. ;; We can make a link using the ID.
  6597. (setq link (condition-case nil
  6598. (prog1 (org-id-store-link)
  6599. (setq desc (plist-get org-store-link-plist
  6600. :description)))
  6601. (error
  6602. ;; probably before first headline, link to file only
  6603. (concat "file:"
  6604. (abbreviate-file-name buffer-file-name))))))
  6605. (t
  6606. ;; Just link to current headline
  6607. (setq cpltxt (concat "file:"
  6608. (abbreviate-file-name buffer-file-name)))
  6609. ;; Add a context search string
  6610. (when (org-xor org-context-in-file-links arg)
  6611. (setq txt (cond
  6612. ((org-on-heading-p) nil)
  6613. ((org-region-active-p)
  6614. (buffer-substring (region-beginning) (region-end)))
  6615. (t nil)))
  6616. (when (or (null txt) (string-match "\\S-" txt))
  6617. (setq cpltxt
  6618. (concat cpltxt "::"
  6619. (condition-case nil
  6620. (org-make-org-heading-search-string txt)
  6621. (error "")))
  6622. desc (or (nth 4 (ignore-errors
  6623. (org-heading-components))) "NONE"))))
  6624. (if (string-match "::\\'" cpltxt)
  6625. (setq cpltxt (substring cpltxt 0 -2)))
  6626. (setq link (org-make-link cpltxt)))))
  6627. ((buffer-file-name (buffer-base-buffer))
  6628. ;; Just link to this file here.
  6629. (setq cpltxt (concat "file:"
  6630. (abbreviate-file-name
  6631. (buffer-file-name (buffer-base-buffer)))))
  6632. ;; Add a context string
  6633. (when (org-xor org-context-in-file-links arg)
  6634. (setq txt (if (org-region-active-p)
  6635. (buffer-substring (region-beginning) (region-end))
  6636. (buffer-substring (point-at-bol) (point-at-eol))))
  6637. ;; Only use search option if there is some text.
  6638. (when (string-match "\\S-" txt)
  6639. (setq cpltxt
  6640. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6641. desc "NONE")))
  6642. (setq link (org-make-link cpltxt)))
  6643. ((interactive-p)
  6644. (error "Cannot link to a buffer which is not visiting a file"))
  6645. (t (setq link nil)))
  6646. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6647. (setq link (or link cpltxt)
  6648. desc (or desc cpltxt))
  6649. (if (equal desc "NONE") (setq desc nil))
  6650. (if (and (or (interactive-p) executing-kbd-macro) link)
  6651. (progn
  6652. (setq org-stored-links
  6653. (cons (list link desc) org-stored-links))
  6654. (message "Stored: %s" (or desc link))
  6655. (when custom-id
  6656. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6657. "::#" custom-id))
  6658. (setq org-stored-links
  6659. (cons (list link desc) org-stored-links))))
  6660. (and link (org-make-link-string link desc)))))
  6661. (defun org-store-link-props (&rest plist)
  6662. "Store link properties, extract names and addresses."
  6663. (let (x adr)
  6664. (when (setq x (plist-get plist :from))
  6665. (setq adr (mail-extract-address-components x))
  6666. (setq plist (plist-put plist :fromname (car adr)))
  6667. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6668. (when (setq x (plist-get plist :to))
  6669. (setq adr (mail-extract-address-components x))
  6670. (setq plist (plist-put plist :toname (car adr)))
  6671. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6672. (let ((from (plist-get plist :from))
  6673. (to (plist-get plist :to)))
  6674. (when (and from to org-from-is-user-regexp)
  6675. (setq plist
  6676. (plist-put plist :fromto
  6677. (if (string-match org-from-is-user-regexp from)
  6678. (concat "to %t")
  6679. (concat "from %f"))))))
  6680. (setq org-store-link-plist plist))
  6681. (defun org-add-link-props (&rest plist)
  6682. "Add these properties to the link property list."
  6683. (let (key value)
  6684. (while plist
  6685. (setq key (pop plist) value (pop plist))
  6686. (setq org-store-link-plist
  6687. (plist-put org-store-link-plist key value)))))
  6688. (defun org-email-link-description (&optional fmt)
  6689. "Return the description part of an email link.
  6690. This takes information from `org-store-link-plist' and formats it
  6691. according to FMT (default from `org-email-link-description-format')."
  6692. (setq fmt (or fmt org-email-link-description-format))
  6693. (let* ((p org-store-link-plist)
  6694. (to (plist-get p :toaddress))
  6695. (from (plist-get p :fromaddress))
  6696. (table
  6697. (list
  6698. (cons "%c" (plist-get p :fromto))
  6699. (cons "%F" (plist-get p :from))
  6700. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6701. (cons "%T" (plist-get p :to))
  6702. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6703. (cons "%s" (plist-get p :subject))
  6704. (cons "%m" (plist-get p :message-id)))))
  6705. (when (string-match "%c" fmt)
  6706. ;; Check if the user wrote this message
  6707. (if (and org-from-is-user-regexp from to
  6708. (save-match-data (string-match org-from-is-user-regexp from)))
  6709. (setq fmt (replace-match "to %t" t t fmt))
  6710. (setq fmt (replace-match "from %f" t t fmt))))
  6711. (org-replace-escapes fmt table)))
  6712. (defun org-make-org-heading-search-string (&optional string heading)
  6713. "Make search string for STRING or current headline."
  6714. (interactive)
  6715. (let ((s (or string (org-get-heading))))
  6716. (unless (and string (not heading))
  6717. ;; We are using a headline, clean up garbage in there.
  6718. (if (string-match org-todo-regexp s)
  6719. (setq s (replace-match "" t t s)))
  6720. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6721. (setq s (replace-match "" t t s)))
  6722. (setq s (org-trim s))
  6723. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6724. org-comment-string "\\)") s)
  6725. (setq s (replace-match "" t t s)))
  6726. (while (string-match org-ts-regexp s)
  6727. (setq s (replace-match "" t t s))))
  6728. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6729. (setq s (replace-match " " t t s)))
  6730. (or string (setq s (concat "*" s))) ; Add * for headlines
  6731. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6732. (defun org-make-link (&rest strings)
  6733. "Concatenate STRINGS."
  6734. (apply 'concat strings))
  6735. (defun org-make-link-string (link &optional description)
  6736. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6737. (unless (string-match "\\S-" link)
  6738. (error "Empty link"))
  6739. (when (stringp description)
  6740. ;; Remove brackets from the description, they are fatal.
  6741. (while (string-match "\\[" description)
  6742. (setq description (replace-match "{" t t description)))
  6743. (while (string-match "\\]" description)
  6744. (setq description (replace-match "}" t t description))))
  6745. (when (equal (org-link-escape link) description)
  6746. ;; No description needed, it is identical
  6747. (setq description nil))
  6748. (when (and (not description)
  6749. (not (equal link (org-link-escape link))))
  6750. (setq description (org-extract-attributes link)))
  6751. (concat "[[" (org-link-escape link) "]"
  6752. (if description (concat "[" description "]") "")
  6753. "]"))
  6754. (defconst org-link-escape-chars
  6755. '((?\ . "%20")
  6756. (?\[ . "%5B")
  6757. (?\] . "%5D")
  6758. (?\340 . "%E0") ; `a
  6759. (?\342 . "%E2") ; ^a
  6760. (?\347 . "%E7") ; ,c
  6761. (?\350 . "%E8") ; `e
  6762. (?\351 . "%E9") ; 'e
  6763. (?\352 . "%EA") ; ^e
  6764. (?\356 . "%EE") ; ^i
  6765. (?\364 . "%F4") ; ^o
  6766. (?\371 . "%F9") ; `u
  6767. (?\373 . "%FB") ; ^u
  6768. (?\; . "%3B")
  6769. (?? . "%3F")
  6770. (?= . "%3D")
  6771. (?+ . "%2B")
  6772. )
  6773. "Association list of escapes for some characters problematic in links.
  6774. This is the list that is used for internal purposes.")
  6775. (defvar org-url-encoding-use-url-hexify nil)
  6776. (defconst org-link-escape-chars-browser
  6777. '((?\ . "%20")) ; 32 for the SPC char
  6778. "Association list of escapes for some characters problematic in links.
  6779. This is the list that is used before handing over to the browser.")
  6780. (defun org-link-escape (text &optional table)
  6781. "Escape characters in TEXT that are problematic for links."
  6782. (if org-url-encoding-use-url-hexify
  6783. (url-hexify-string text)
  6784. (setq table (or table org-link-escape-chars))
  6785. (when text
  6786. (let ((re (mapconcat (lambda (x) (regexp-quote
  6787. (char-to-string (car x))))
  6788. table "\\|")))
  6789. (while (string-match re text)
  6790. (setq text
  6791. (replace-match
  6792. (cdr (assoc (string-to-char (match-string 0 text))
  6793. table))
  6794. t t text)))
  6795. text))))
  6796. (defun org-link-unescape (text &optional table)
  6797. "Reverse the action of `org-link-escape'."
  6798. (if org-url-encoding-use-url-hexify
  6799. (url-unhex-string text)
  6800. (setq table (or table org-link-escape-chars))
  6801. (when text
  6802. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6803. table "\\|")))
  6804. (while (string-match re text)
  6805. (setq text
  6806. (replace-match
  6807. (char-to-string (car (rassoc (match-string 0 text) table)))
  6808. t t text)))
  6809. text))))
  6810. (defun org-xor (a b)
  6811. "Exclusive or."
  6812. (if a (not b) b))
  6813. (defun org-fixup-message-id-for-http (s)
  6814. "Replace special characters in a message id, so it can be used in an http query."
  6815. (while (string-match "<" s)
  6816. (setq s (replace-match "%3C" t t s)))
  6817. (while (string-match ">" s)
  6818. (setq s (replace-match "%3E" t t s)))
  6819. (while (string-match "@" s)
  6820. (setq s (replace-match "%40" t t s)))
  6821. s)
  6822. ;;;###autoload
  6823. (defun org-insert-link-global ()
  6824. "Insert a link like Org-mode does.
  6825. This command can be called in any mode to insert a link in Org-mode syntax."
  6826. (interactive)
  6827. (org-load-modules-maybe)
  6828. (org-run-like-in-org-mode 'org-insert-link))
  6829. (defun org-insert-link (&optional complete-file link-location)
  6830. "Insert a link. At the prompt, enter the link.
  6831. Completion can be used to insert any of the link protocol prefixes like
  6832. http or ftp in use.
  6833. The history can be used to select a link previously stored with
  6834. `org-store-link'. When the empty string is entered (i.e. if you just
  6835. press RET at the prompt), the link defaults to the most recently
  6836. stored link. As SPC triggers completion in the minibuffer, you need to
  6837. use M-SPC or C-q SPC to force the insertion of a space character.
  6838. You will also be prompted for a description, and if one is given, it will
  6839. be displayed in the buffer instead of the link.
  6840. If there is already a link at point, this command will allow you to edit link
  6841. and description parts.
  6842. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6843. be selected using completion. The path to the file will be relative to the
  6844. current directory if the file is in the current directory or a subdirectory.
  6845. Otherwise, the link will be the absolute path as completed in the minibuffer
  6846. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6847. option `org-link-file-path-type'.
  6848. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6849. the current directory or below.
  6850. With three \\[universal-argument] prefixes, negate the meaning of
  6851. `org-keep-stored-link-after-insertion'.
  6852. If `org-make-link-description-function' is non-nil, this function will be
  6853. called with the link target, and the result will be the default
  6854. link description.
  6855. If the LINK-LOCATION parameter is non-nil, this value will be
  6856. used as the link location instead of reading one interactively."
  6857. (interactive "P")
  6858. (let* ((wcf (current-window-configuration))
  6859. (region (if (org-region-active-p)
  6860. (buffer-substring (region-beginning) (region-end))))
  6861. (remove (and region (list (region-beginning) (region-end))))
  6862. (desc region)
  6863. tmphist ; byte-compile incorrectly complains about this
  6864. (link link-location)
  6865. entry file all-prefixes)
  6866. (cond
  6867. (link-location) ; specified by arg, just use it.
  6868. ((org-in-regexp org-bracket-link-regexp 1)
  6869. ;; We do have a link at point, and we are going to edit it.
  6870. (setq remove (list (match-beginning 0) (match-end 0)))
  6871. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6872. (setq link (read-string "Link: "
  6873. (org-link-unescape
  6874. (org-match-string-no-properties 1)))))
  6875. ((or (org-in-regexp org-angle-link-re)
  6876. (org-in-regexp org-plain-link-re))
  6877. ;; Convert to bracket link
  6878. (setq remove (list (match-beginning 0) (match-end 0))
  6879. link (read-string "Link: "
  6880. (org-remove-angle-brackets (match-string 0)))))
  6881. ((member complete-file '((4) (16)))
  6882. ;; Completing read for file names.
  6883. (setq link (org-file-complete-link complete-file)))
  6884. (t
  6885. ;; Read link, with completion for stored links.
  6886. (with-output-to-temp-buffer "*Org Links*"
  6887. (princ "Insert a link.
  6888. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  6889. (when org-stored-links
  6890. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6891. (princ (mapconcat
  6892. (lambda (x)
  6893. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6894. (reverse org-stored-links) "\n"))))
  6895. (let ((cw (selected-window)))
  6896. (select-window (get-buffer-window "*Org Links*"))
  6897. (setq truncate-lines t)
  6898. (unless (pos-visible-in-window-p (point-max))
  6899. (org-fit-window-to-buffer))
  6900. (and (window-live-p cw) (select-window cw)))
  6901. ;; Fake a link history, containing the stored links.
  6902. (setq tmphist (append (mapcar 'car org-stored-links)
  6903. org-insert-link-history))
  6904. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  6905. (mapcar 'car org-link-abbrev-alist)
  6906. org-link-types))
  6907. (unwind-protect
  6908. (progn
  6909. (setq link
  6910. (let ((org-completion-use-ido nil)
  6911. (org-completion-use-iswitchb nil))
  6912. (org-completing-read
  6913. "Link: "
  6914. (append
  6915. (mapcar (lambda (x) (list (concat x ":")))
  6916. all-prefixes)
  6917. (mapcar 'car org-stored-links))
  6918. nil nil nil
  6919. 'tmphist
  6920. (car (car org-stored-links)))))
  6921. (if (or (member link all-prefixes)
  6922. (and (equal ":" (substring link -1))
  6923. (member (substring link 0 -1) all-prefixes)
  6924. (setq link (substring link 0 -1))))
  6925. (setq link (org-link-try-special-completion link))))
  6926. (set-window-configuration wcf)
  6927. (kill-buffer "*Org Links*"))
  6928. (setq entry (assoc link org-stored-links))
  6929. (or entry (push link org-insert-link-history))
  6930. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6931. (not org-keep-stored-link-after-insertion))
  6932. (setq org-stored-links (delq (assoc link org-stored-links)
  6933. org-stored-links)))
  6934. (setq desc (or desc (nth 1 entry)))))
  6935. (if (string-match org-plain-link-re link)
  6936. ;; URL-like link, normalize the use of angular brackets.
  6937. (setq link (org-make-link (org-remove-angle-brackets link))))
  6938. ;; Check if we are linking to the current file with a search option
  6939. ;; If yes, simplify the link by using only the search option.
  6940. (when (and buffer-file-name
  6941. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6942. (let* ((path (match-string 1 link))
  6943. (case-fold-search nil)
  6944. (search (match-string 2 link)))
  6945. (save-match-data
  6946. (if (equal (file-truename buffer-file-name) (file-truename path))
  6947. ;; We are linking to this same file, with a search option
  6948. (setq link search)))))
  6949. ;; Check if we can/should use a relative path. If yes, simplify the link
  6950. (when (string-match "^file:\\(.*\\)" link)
  6951. (let* ((path (match-string 1 link))
  6952. (origpath path)
  6953. (case-fold-search nil))
  6954. (cond
  6955. ((or (eq org-link-file-path-type 'absolute)
  6956. (equal complete-file '(16)))
  6957. (setq path (abbreviate-file-name (expand-file-name path))))
  6958. ((eq org-link-file-path-type 'noabbrev)
  6959. (setq path (expand-file-name path)))
  6960. ((eq org-link-file-path-type 'relative)
  6961. (setq path (file-relative-name path)))
  6962. (t
  6963. (save-match-data
  6964. (if (string-match (concat "^" (regexp-quote
  6965. (file-name-as-directory
  6966. (expand-file-name "."))))
  6967. (expand-file-name path))
  6968. ;; We are linking a file with relative path name.
  6969. (setq path (substring (expand-file-name path)
  6970. (match-end 0)))
  6971. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6972. (setq link (concat "file:" path))
  6973. (if (equal desc origpath)
  6974. (setq desc path))))
  6975. (if org-make-link-description-function
  6976. (setq desc (funcall org-make-link-description-function link desc)))
  6977. (setq desc (read-string "Description: " desc))
  6978. (unless (string-match "\\S-" desc) (setq desc nil))
  6979. (if remove (apply 'delete-region remove))
  6980. (insert (org-make-link-string link desc))))
  6981. (defun org-link-try-special-completion (type)
  6982. "If there is completion support for link type TYPE, offer it."
  6983. (let ((fun (intern (concat "org-" type "-complete-link"))))
  6984. (if (functionp fun)
  6985. (funcall fun)
  6986. (read-string "Link (no completion support): " (concat type ":")))))
  6987. (defun org-file-complete-link (&optional arg)
  6988. "Create a file link using completion."
  6989. (let (file link)
  6990. (setq file (read-file-name "File: "))
  6991. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6992. (pwd1 (file-name-as-directory (abbreviate-file-name
  6993. (expand-file-name ".")))))
  6994. (cond
  6995. ((equal arg '(16))
  6996. (setq link (org-make-link
  6997. "file:"
  6998. (abbreviate-file-name (expand-file-name file)))))
  6999. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7000. (setq link (org-make-link "file:" (match-string 1 file))))
  7001. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7002. (expand-file-name file))
  7003. (setq link (org-make-link
  7004. "file:" (match-string 1 (expand-file-name file)))))
  7005. (t (setq link (org-make-link "file:" file)))))
  7006. link))
  7007. (defun org-completing-read (&rest args)
  7008. "Completing-read with SPACE being a normal character."
  7009. (let ((minibuffer-local-completion-map
  7010. (copy-keymap minibuffer-local-completion-map)))
  7011. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7012. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7013. (apply 'org-icompleting-read args)))
  7014. (defun org-completing-read-no-i (&rest args)
  7015. (let (org-completion-use-ido org-completion-use-iswitchb)
  7016. (apply 'org-completing-read args)))
  7017. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7018. "Use iswitch as a completing-read replacement to choose from choices.
  7019. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7020. from."
  7021. (let* ((iswitchb-use-virtual-buffers nil)
  7022. (iswitchb-make-buflist-hook
  7023. (lambda ()
  7024. (setq iswitchb-temp-buflist choices))))
  7025. (iswitchb-read-buffer prompt)))
  7026. (defun org-icompleting-read (&rest args)
  7027. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7028. (org-without-partial-completion
  7029. (if (and org-completion-use-ido
  7030. (fboundp 'ido-completing-read)
  7031. (boundp 'ido-mode) ido-mode
  7032. (listp (second args)))
  7033. (let ((ido-enter-matching-directory nil))
  7034. (apply 'ido-completing-read (concat (car args))
  7035. (if (consp (car (nth 1 args)))
  7036. (mapcar (lambda (x) (car x)) (nth 1 args))
  7037. (nth 1 args))
  7038. (cddr args)))
  7039. (if (and org-completion-use-iswitchb
  7040. (boundp 'iswitchb-mode) iswitchb-mode
  7041. (listp (second args)))
  7042. (apply 'org-iswitchb-completing-read (concat (car args))
  7043. (if (consp (car (nth 1 args)))
  7044. (mapcar (lambda (x) (car x)) (nth 1 args))
  7045. (nth 1 args))
  7046. (cddr args))
  7047. (apply 'completing-read args)))))
  7048. (defun org-extract-attributes (s)
  7049. "Extract the attributes cookie from a string and set as text property."
  7050. (let (a attr (start 0) key value)
  7051. (save-match-data
  7052. (when (string-match "{{\\([^}]+\\)}}$" s)
  7053. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7054. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7055. (setq key (match-string 1 a) value (match-string 2 a)
  7056. start (match-end 0)
  7057. attr (plist-put attr (intern key) value))))
  7058. (org-add-props s nil 'org-attr attr))
  7059. s))
  7060. (defun org-extract-attributes-from-string (tag)
  7061. (let (key value attr)
  7062. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7063. (setq key (match-string 1 tag) value (match-string 2 tag)
  7064. tag (replace-match "" t t tag)
  7065. attr (plist-put attr (intern key) value)))
  7066. (cons tag attr)))
  7067. (defun org-attributes-to-string (plist)
  7068. "Format a property list into an HTML attribute list."
  7069. (let ((s "") key value)
  7070. (while plist
  7071. (setq key (pop plist) value (pop plist))
  7072. (and value
  7073. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7074. s))
  7075. ;;; Opening/following a link
  7076. (defvar org-link-search-failed nil)
  7077. (defun org-next-link ()
  7078. "Move forward to the next link.
  7079. If the link is in hidden text, expose it."
  7080. (interactive)
  7081. (when (and org-link-search-failed (eq this-command last-command))
  7082. (goto-char (point-min))
  7083. (message "Link search wrapped back to beginning of buffer"))
  7084. (setq org-link-search-failed nil)
  7085. (let* ((pos (point))
  7086. (ct (org-context))
  7087. (a (assoc :link ct)))
  7088. (if a (goto-char (nth 2 a)))
  7089. (if (re-search-forward org-any-link-re nil t)
  7090. (progn
  7091. (goto-char (match-beginning 0))
  7092. (if (org-invisible-p) (org-show-context)))
  7093. (goto-char pos)
  7094. (setq org-link-search-failed t)
  7095. (error "No further link found"))))
  7096. (defun org-previous-link ()
  7097. "Move backward to the previous link.
  7098. If the link is in hidden text, expose it."
  7099. (interactive)
  7100. (when (and org-link-search-failed (eq this-command last-command))
  7101. (goto-char (point-max))
  7102. (message "Link search wrapped back to end of buffer"))
  7103. (setq org-link-search-failed nil)
  7104. (let* ((pos (point))
  7105. (ct (org-context))
  7106. (a (assoc :link ct)))
  7107. (if a (goto-char (nth 1 a)))
  7108. (if (re-search-backward org-any-link-re nil t)
  7109. (progn
  7110. (goto-char (match-beginning 0))
  7111. (if (org-invisible-p) (org-show-context)))
  7112. (goto-char pos)
  7113. (setq org-link-search-failed t)
  7114. (error "No further link found"))))
  7115. (defun org-translate-link (s)
  7116. "Translate a link string if a translation function has been defined."
  7117. (if (and org-link-translation-function
  7118. (fboundp org-link-translation-function)
  7119. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7120. (progn
  7121. (setq s (funcall org-link-translation-function
  7122. (match-string 1) (match-string 2)))
  7123. (concat (car s) ":" (cdr s)))
  7124. s))
  7125. (defun org-translate-link-from-planner (type path)
  7126. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7127. This is still an experimental function, your mileage may vary."
  7128. (cond
  7129. ((member type '("http" "https" "news" "ftp"))
  7130. ;; standard Internet links are the same.
  7131. nil)
  7132. ((and (equal type "irc") (string-match "^//" path))
  7133. ;; Planner has two / at the beginning of an irc link, we have 1.
  7134. ;; We should have zero, actually....
  7135. (setq path (substring path 1)))
  7136. ((and (equal type "lisp") (string-match "^/" path))
  7137. ;; Planner has a slash, we do not.
  7138. (setq type "elisp" path (substring path 1)))
  7139. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7140. ;; A typical message link. Planner has the id after the fina slash,
  7141. ;; we separate it with a hash mark
  7142. (setq path (concat (match-string 1 path) "#"
  7143. (org-remove-angle-brackets (match-string 2 path)))))
  7144. )
  7145. (cons type path))
  7146. (defun org-find-file-at-mouse (ev)
  7147. "Open file link or URL at mouse."
  7148. (interactive "e")
  7149. (mouse-set-point ev)
  7150. (org-open-at-point 'in-emacs))
  7151. (defun org-open-at-mouse (ev)
  7152. "Open file link or URL at mouse."
  7153. (interactive "e")
  7154. (mouse-set-point ev)
  7155. (if (eq major-mode 'org-agenda-mode)
  7156. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7157. (org-open-at-point))
  7158. (defvar org-window-config-before-follow-link nil
  7159. "The window configuration before following a link.
  7160. This is saved in case the need arises to restore it.")
  7161. (defvar org-open-link-marker (make-marker)
  7162. "Marker pointing to the location where `org-open-at-point; was called.")
  7163. ;;;###autoload
  7164. (defun org-open-at-point-global ()
  7165. "Follow a link like Org-mode does.
  7166. This command can be called in any mode to follow a link that has
  7167. Org-mode syntax."
  7168. (interactive)
  7169. (org-run-like-in-org-mode 'org-open-at-point))
  7170. ;;;###autoload
  7171. (defun org-open-link-from-string (s &optional arg reference-buffer)
  7172. "Open a link in the string S, as if it was in Org-mode."
  7173. (interactive "sLink: \nP")
  7174. (let ((reference-buffer (or reference-buffer (current-buffer))))
  7175. (with-temp-buffer
  7176. (let ((org-inhibit-startup t))
  7177. (org-mode)
  7178. (insert s)
  7179. (goto-char (point-min))
  7180. (org-open-at-point arg reference-buffer)))))
  7181. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7182. "Open link at or after point.
  7183. If there is no link at point, this function will search forward up to
  7184. the end of the current line.
  7185. Normally, files will be opened by an appropriate application. If the
  7186. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7187. With a double prefix argument, try to open outside of Emacs, in the
  7188. application the system uses for this file type."
  7189. (interactive "P")
  7190. (org-load-modules-maybe)
  7191. (move-marker org-open-link-marker (point))
  7192. (setq org-window-config-before-follow-link (current-window-configuration))
  7193. (org-remove-occur-highlights nil nil t)
  7194. (cond
  7195. ((and (org-on-heading-p)
  7196. (not (org-in-regexp
  7197. (concat org-plain-link-re "\\|"
  7198. org-bracket-link-regexp "\\|"
  7199. org-angle-link-re "\\|"
  7200. "[ \t]:[^ \t\n]+:[ \t]*$"))))
  7201. (org-offer-links-in-entry in-emacs))
  7202. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7203. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7204. (org-footnote-action))
  7205. (t
  7206. (let (type path link line search (pos (point)))
  7207. (catch 'match
  7208. (save-excursion
  7209. (skip-chars-forward "^]\n\r")
  7210. (when (org-in-regexp org-bracket-link-regexp)
  7211. (setq link (org-extract-attributes
  7212. (org-link-unescape (org-match-string-no-properties 1))))
  7213. (while (string-match " *\n *" link)
  7214. (setq link (replace-match " " t t link)))
  7215. (setq link (org-link-expand-abbrev link))
  7216. (cond
  7217. ((or (file-name-absolute-p link)
  7218. (string-match "^\\.\\.?/" link))
  7219. (setq type "file" path link))
  7220. ((string-match org-link-re-with-space3 link)
  7221. (setq type (match-string 1 link) path (match-string 2 link)))
  7222. (t (setq type "thisfile" path link)))
  7223. (throw 'match t)))
  7224. (when (get-text-property (point) 'org-linked-text)
  7225. (setq type "thisfile"
  7226. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7227. (1+ (point)) (point))
  7228. path (buffer-substring
  7229. (previous-single-property-change pos 'org-linked-text)
  7230. (next-single-property-change pos 'org-linked-text)))
  7231. (throw 'match t))
  7232. (save-excursion
  7233. (when (or (org-in-regexp org-angle-link-re)
  7234. (org-in-regexp org-plain-link-re))
  7235. (setq type (match-string 1) path (match-string 2))
  7236. (throw 'match t)))
  7237. (save-excursion
  7238. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7239. (setq type "tags"
  7240. path (match-string 1))
  7241. (while (string-match ":" path)
  7242. (setq path (replace-match "+" t t path)))
  7243. (throw 'match t)))
  7244. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7245. (setq type "tree-match"
  7246. path (match-string 1))
  7247. (throw 'match t)))
  7248. (unless path
  7249. (error "No link found"))
  7250. ;; switch back to reference buffer
  7251. ;; needed when if called in a temporary buffer through
  7252. ;; org-open-link-from-string
  7253. (with-current-buffer (or reference-buffer (current-buffer))
  7254. ;; Remove any trailing spaces in path
  7255. (if (string-match " +\\'" path)
  7256. (setq path (replace-match "" t t path)))
  7257. (if (and org-link-translation-function
  7258. (fboundp org-link-translation-function))
  7259. ;; Check if we need to translate the link
  7260. (let ((tmp (funcall org-link-translation-function type path)))
  7261. (setq type (car tmp) path (cdr tmp))))
  7262. (cond
  7263. ((assoc type org-link-protocols)
  7264. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7265. ((equal type "mailto")
  7266. (let ((cmd (car org-link-mailto-program))
  7267. (args (cdr org-link-mailto-program)) args1
  7268. (address path) (subject "") a)
  7269. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7270. (setq address (match-string 1 path)
  7271. subject (org-link-escape (match-string 2 path))))
  7272. (while args
  7273. (cond
  7274. ((not (stringp (car args))) (push (pop args) args1))
  7275. (t (setq a (pop args))
  7276. (if (string-match "%a" a)
  7277. (setq a (replace-match address t t a)))
  7278. (if (string-match "%s" a)
  7279. (setq a (replace-match subject t t a)))
  7280. (push a args1))))
  7281. (apply cmd (nreverse args1))))
  7282. ((member type '("http" "https" "ftp" "news"))
  7283. (browse-url (concat type ":" (org-link-escape
  7284. path org-link-escape-chars-browser))))
  7285. ((member type '("message"))
  7286. (browse-url (concat type ":" path)))
  7287. ((string= type "tags")
  7288. (org-tags-view in-emacs path))
  7289. ((string= type "thisfile")
  7290. (if in-emacs
  7291. (switch-to-buffer-other-window
  7292. (org-get-buffer-for-internal-link (current-buffer)))
  7293. (org-mark-ring-push))
  7294. (let ((cmd `(org-link-search
  7295. ,path
  7296. ,(cond ((equal in-emacs '(4)) 'occur)
  7297. ((equal in-emacs '(16)) 'org-occur)
  7298. (t nil))
  7299. ,pos)))
  7300. (condition-case nil (eval cmd)
  7301. (error (progn (widen) (eval cmd))))))
  7302. ((string= type "tree-match")
  7303. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7304. ((string= type "file")
  7305. (if (string-match "::\\([0-9]+\\)\\'" path)
  7306. (setq line (string-to-number (match-string 1 path))
  7307. path (substring path 0 (match-beginning 0)))
  7308. (if (string-match "::\\(.+\\)\\'" path)
  7309. (setq search (match-string 1 path)
  7310. path (substring path 0 (match-beginning 0)))))
  7311. (if (string-match "[*?{]" (file-name-nondirectory path))
  7312. (dired path)
  7313. (org-open-file path in-emacs line search)))
  7314. ((string= type "news")
  7315. (require 'org-gnus)
  7316. (org-gnus-follow-link path))
  7317. ((string= type "shell")
  7318. (let ((cmd path))
  7319. (if (or (not org-confirm-shell-link-function)
  7320. (funcall org-confirm-shell-link-function
  7321. (format "Execute \"%s\" in shell? "
  7322. (org-add-props cmd nil
  7323. 'face 'org-warning))))
  7324. (progn
  7325. (message "Executing %s" cmd)
  7326. (shell-command cmd))
  7327. (error "Abort"))))
  7328. ((string= type "elisp")
  7329. (let ((cmd path))
  7330. (if (or (not org-confirm-elisp-link-function)
  7331. (funcall org-confirm-elisp-link-function
  7332. (format "Execute \"%s\" as elisp? "
  7333. (org-add-props cmd nil
  7334. 'face 'org-warning))))
  7335. (message "%s => %s" cmd
  7336. (if (equal (string-to-char cmd) ?\()
  7337. (eval (read cmd))
  7338. (call-interactively (read cmd))))
  7339. (error "Abort"))))
  7340. (t
  7341. (browse-url-at-point)))))))
  7342. (move-marker org-open-link-marker nil)
  7343. (run-hook-with-args 'org-follow-link-hook))
  7344. (defun org-offer-links-in-entry (&optional nth zero)
  7345. "Offer links in the curren entry and follow the selected link.
  7346. If there is only one link, follow it immediately as well.
  7347. If NTH is an integer, immediately pick the NTH link found.
  7348. If ZERO is a string, check also this string for a link, and if
  7349. there is one, offer it as link number zero."
  7350. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  7351. "\\(" org-angle-link-re "\\)\\|"
  7352. "\\(" org-plain-link-re "\\)"))
  7353. (cnt ?0)
  7354. (in-emacs (if (integerp nth) nil nth))
  7355. have-zero end links link c)
  7356. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  7357. (push (match-string 0 zero) links)
  7358. (setq cnt (1- cnt) have-zero t))
  7359. (save-excursion
  7360. (org-back-to-heading t)
  7361. (setq end (save-excursion (outline-next-heading) (point)))
  7362. (while (re-search-forward re end t)
  7363. (push (match-string 0) links))
  7364. (setq links (org-uniquify (reverse links))))
  7365. (cond
  7366. ((null links) (error "No links"))
  7367. ((equal (length links) 1)
  7368. (setq link (car links)))
  7369. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  7370. (setq link (nth (if have-zero nth (1- nth)) links)))
  7371. (t ; we have to select a link
  7372. (save-excursion
  7373. (save-window-excursion
  7374. (delete-other-windows)
  7375. (with-output-to-temp-buffer "*Select Link*"
  7376. (mapc (lambda (l)
  7377. (if (not (string-match org-bracket-link-regexp l))
  7378. (princ (format "[%c] %s\n" (incf cnt)
  7379. (org-remove-angle-brackets l)))
  7380. (if (match-end 3)
  7381. (princ (format "[%c] %s (%s)\n" (incf cnt)
  7382. (match-string 3 l) (match-string 1 l)))
  7383. (princ (format "[%c] %s\n" (incf cnt)
  7384. (match-string 1 l))))))
  7385. links))
  7386. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  7387. (message "Select link to open:")
  7388. (setq c (read-char-exclusive))
  7389. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  7390. (when (equal c ?q) (error "Abort"))
  7391. (setq nth (- c ?0))
  7392. (if have-zero (setq nth (1+ nth)))
  7393. (unless (and (integerp nth) (>= (length links) nth))
  7394. (error "Invalid link selection"))
  7395. (setq link (nth (1- nth) links))))
  7396. (org-open-link-from-string link in-emacs (current-buffer))))
  7397. ;;;; Time estimates
  7398. (defun org-get-effort (&optional pom)
  7399. "Get the effort estimate for the current entry."
  7400. (org-entry-get pom org-effort-property))
  7401. ;;; File search
  7402. (defvar org-create-file-search-functions nil
  7403. "List of functions to construct the right search string for a file link.
  7404. These functions are called in turn with point at the location to
  7405. which the link should point.
  7406. A function in the hook should first test if it would like to
  7407. handle this file type, for example by checking the major-mode or
  7408. the file extension. If it decides not to handle this file, it
  7409. should just return nil to give other functions a chance. If it
  7410. does handle the file, it must return the search string to be used
  7411. when following the link. The search string will be part of the
  7412. file link, given after a double colon, and `org-open-at-point'
  7413. will automatically search for it. If special measures must be
  7414. taken to make the search successful, another function should be
  7415. added to the companion hook `org-execute-file-search-functions',
  7416. which see.
  7417. A function in this hook may also use `setq' to set the variable
  7418. `description' to provide a suggestion for the descriptive text to
  7419. be used for this link when it gets inserted into an Org-mode
  7420. buffer with \\[org-insert-link].")
  7421. (defvar org-execute-file-search-functions nil
  7422. "List of functions to execute a file search triggered by a link.
  7423. Functions added to this hook must accept a single argument, the
  7424. search string that was part of the file link, the part after the
  7425. double colon. The function must first check if it would like to
  7426. handle this search, for example by checking the major-mode or the
  7427. file extension. If it decides not to handle this search, it
  7428. should just return nil to give other functions a chance. If it
  7429. does handle the search, it must return a non-nil value to keep
  7430. other functions from trying.
  7431. Each function can access the current prefix argument through the
  7432. variable `current-prefix-argument'. Note that a single prefix is
  7433. used to force opening a link in Emacs, so it may be good to only
  7434. use a numeric or double prefix to guide the search function.
  7435. In case this is needed, a function in this hook can also restore
  7436. the window configuration before `org-open-at-point' was called using:
  7437. (set-window-configuration org-window-config-before-follow-link)")
  7438. (defun org-link-search (s &optional type avoid-pos)
  7439. "Search for a link search option.
  7440. If S is surrounded by forward slashes, it is interpreted as a
  7441. regular expression. In org-mode files, this will create an `org-occur'
  7442. sparse tree. In ordinary files, `occur' will be used to list matches.
  7443. If the current buffer is in `dired-mode', grep will be used to search
  7444. in all files. If AVOID-POS is given, ignore matches near that position."
  7445. (let ((case-fold-search t)
  7446. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7447. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7448. (append '(("") (" ") ("\t") ("\n"))
  7449. org-emphasis-alist)
  7450. "\\|") "\\)"))
  7451. (pos (point))
  7452. (pre nil) (post nil)
  7453. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7454. (cond
  7455. ;; First check if there are any special
  7456. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7457. ;; Now try the builtin stuff
  7458. ((and (equal (string-to-char s0) ?#)
  7459. (> (length s0) 1)
  7460. (save-excursion
  7461. (goto-char (point-min))
  7462. (and
  7463. (re-search-forward
  7464. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7465. (setq type 'dedicated
  7466. pos (match-beginning 0))))
  7467. ;; There is an exact target for this
  7468. (goto-char pos)
  7469. (org-back-to-heading t)))
  7470. ((save-excursion
  7471. (goto-char (point-min))
  7472. (and
  7473. (re-search-forward
  7474. (concat "<<" (regexp-quote s0) ">>") nil t)
  7475. (setq type 'dedicated
  7476. pos (match-beginning 0))))
  7477. ;; There is an exact target for this
  7478. (goto-char pos))
  7479. ((and (string-match "^(\\(.*\\))$" s0)
  7480. (save-excursion
  7481. (goto-char (point-min))
  7482. (and
  7483. (re-search-forward
  7484. (concat "[^[]" (regexp-quote
  7485. (format org-coderef-label-format
  7486. (match-string 1 s0))))
  7487. nil t)
  7488. (setq type 'dedicated
  7489. pos (1+ (match-beginning 0))))))
  7490. ;; There is a coderef target for this
  7491. (goto-char pos))
  7492. ((string-match "^/\\(.*\\)/$" s)
  7493. ;; A regular expression
  7494. (cond
  7495. ((org-mode-p)
  7496. (org-occur (match-string 1 s)))
  7497. ;;((eq major-mode 'dired-mode)
  7498. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7499. (t (org-do-occur (match-string 1 s)))))
  7500. (t
  7501. ;; A normal search strings
  7502. (when (equal (string-to-char s) ?*)
  7503. ;; Anchor on headlines, post may include tags.
  7504. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7505. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7506. s (substring s 1)))
  7507. (remove-text-properties
  7508. 0 (length s)
  7509. '(face nil mouse-face nil keymap nil fontified nil) s)
  7510. ;; Make a series of regular expressions to find a match
  7511. (setq words (org-split-string s "[ \n\r\t]+")
  7512. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7513. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7514. "\\)" markers)
  7515. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7516. re2a (concat "[ \t\r\n]" re2a_)
  7517. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7518. re4 (concat "[^a-zA-Z_]" re4_)
  7519. re1 (concat pre re2 post)
  7520. re3 (concat pre (if pre re4_ re4) post)
  7521. re5 (concat pre ".*" re4)
  7522. re2 (concat pre re2)
  7523. re2a (concat pre (if pre re2a_ re2a))
  7524. re4 (concat pre (if pre re4_ re4))
  7525. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7526. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7527. re5 "\\)"
  7528. ))
  7529. (cond
  7530. ((eq type 'org-occur) (org-occur reall))
  7531. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7532. (t (goto-char (point-min))
  7533. (setq type 'fuzzy)
  7534. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7535. (org-search-not-self 1 re1 nil t)
  7536. (org-search-not-self 1 re2 nil t)
  7537. (org-search-not-self 1 re2a nil t)
  7538. (org-search-not-self 1 re3 nil t)
  7539. (org-search-not-self 1 re4 nil t)
  7540. (org-search-not-self 1 re5 nil t)
  7541. )
  7542. (goto-char (match-beginning 1))
  7543. (goto-char pos)
  7544. (error "No match")))))
  7545. (t
  7546. ;; Normal string-search
  7547. (goto-char (point-min))
  7548. (if (search-forward s nil t)
  7549. (goto-char (match-beginning 0))
  7550. (error "No match"))))
  7551. (and (org-mode-p) (org-show-context 'link-search))
  7552. type))
  7553. (defun org-search-not-self (group &rest args)
  7554. "Execute `re-search-forward', but only accept matches that do not
  7555. enclose the position of `org-open-link-marker'."
  7556. (let ((m org-open-link-marker))
  7557. (catch 'exit
  7558. (while (apply 're-search-forward args)
  7559. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7560. (goto-char (match-end group))
  7561. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7562. (> (match-beginning 0) (marker-position m))
  7563. (< (match-end 0) (marker-position m)))
  7564. (save-match-data
  7565. (or (not (org-in-regexp
  7566. org-bracket-link-analytic-regexp 1))
  7567. (not (match-end 4)) ; no description
  7568. (and (<= (match-beginning 4) (point))
  7569. (>= (match-end 4) (point))))))
  7570. (throw 'exit (point))))))))
  7571. (defun org-get-buffer-for-internal-link (buffer)
  7572. "Return a buffer to be used for displaying the link target of internal links."
  7573. (cond
  7574. ((not org-display-internal-link-with-indirect-buffer)
  7575. buffer)
  7576. ((string-match "(Clone)$" (buffer-name buffer))
  7577. (message "Buffer is already a clone, not making another one")
  7578. ;; we also do not modify visibility in this case
  7579. buffer)
  7580. (t ; make a new indirect buffer for displaying the link
  7581. (let* ((bn (buffer-name buffer))
  7582. (ibn (concat bn "(Clone)"))
  7583. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7584. (with-current-buffer ib (org-overview))
  7585. ib))))
  7586. (defun org-do-occur (regexp &optional cleanup)
  7587. "Call the Emacs command `occur'.
  7588. If CLEANUP is non-nil, remove the printout of the regular expression
  7589. in the *Occur* buffer. This is useful if the regex is long and not useful
  7590. to read."
  7591. (occur regexp)
  7592. (when cleanup
  7593. (let ((cwin (selected-window)) win beg end)
  7594. (when (setq win (get-buffer-window "*Occur*"))
  7595. (select-window win))
  7596. (goto-char (point-min))
  7597. (when (re-search-forward "match[a-z]+" nil t)
  7598. (setq beg (match-end 0))
  7599. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7600. (setq end (1- (match-beginning 0)))))
  7601. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7602. (goto-char (point-min))
  7603. (select-window cwin))))
  7604. ;;; The mark ring for links jumps
  7605. (defvar org-mark-ring nil
  7606. "Mark ring for positions before jumps in Org-mode.")
  7607. (defvar org-mark-ring-last-goto nil
  7608. "Last position in the mark ring used to go back.")
  7609. ;; Fill and close the ring
  7610. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7611. (loop for i from 1 to org-mark-ring-length do
  7612. (push (make-marker) org-mark-ring))
  7613. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7614. org-mark-ring)
  7615. (defun org-mark-ring-push (&optional pos buffer)
  7616. "Put the current position or POS into the mark ring and rotate it."
  7617. (interactive)
  7618. (setq pos (or pos (point)))
  7619. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7620. (move-marker (car org-mark-ring)
  7621. (or pos (point))
  7622. (or buffer (current-buffer)))
  7623. (message "%s"
  7624. (substitute-command-keys
  7625. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7626. (defun org-mark-ring-goto (&optional n)
  7627. "Jump to the previous position in the mark ring.
  7628. With prefix arg N, jump back that many stored positions. When
  7629. called several times in succession, walk through the entire ring.
  7630. Org-mode commands jumping to a different position in the current file,
  7631. or to another Org-mode file, automatically push the old position
  7632. onto the ring."
  7633. (interactive "p")
  7634. (let (p m)
  7635. (if (eq last-command this-command)
  7636. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7637. (setq p org-mark-ring))
  7638. (setq org-mark-ring-last-goto p)
  7639. (setq m (car p))
  7640. (switch-to-buffer (marker-buffer m))
  7641. (goto-char m)
  7642. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7643. (defun org-remove-angle-brackets (s)
  7644. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7645. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7646. s)
  7647. (defun org-add-angle-brackets (s)
  7648. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7649. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7650. s)
  7651. (defun org-remove-double-quotes (s)
  7652. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7653. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7654. s)
  7655. ;;; Following specific links
  7656. (defun org-follow-timestamp-link ()
  7657. (cond
  7658. ((org-at-date-range-p t)
  7659. (let ((org-agenda-start-on-weekday)
  7660. (t1 (match-string 1))
  7661. (t2 (match-string 2)))
  7662. (setq t1 (time-to-days (org-time-string-to-time t1))
  7663. t2 (time-to-days (org-time-string-to-time t2)))
  7664. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7665. ((org-at-timestamp-p t)
  7666. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7667. (substring (match-string 1) 0 10)))
  7668. 1))
  7669. (t (error "This should not happen"))))
  7670. ;;; Following file links
  7671. (defvar org-wait nil)
  7672. (defun org-open-file (path &optional in-emacs line search)
  7673. "Open the file at PATH.
  7674. First, this expands any special file name abbreviations. Then the
  7675. configuration variable `org-file-apps' is checked if it contains an
  7676. entry for this file type, and if yes, the corresponding command is launched.
  7677. If no application is found, Emacs simply visits the file.
  7678. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7679. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7680. and o use an external application to visit the file.
  7681. Optional LINE specifies a line to go to, optional SEARCH a string to
  7682. search for. If LINE or SEARCH is given, the file will always be
  7683. opened in Emacs.
  7684. If the file does not exist, an error is thrown."
  7685. (setq in-emacs (or in-emacs line search))
  7686. (let* ((file (if (equal path "")
  7687. buffer-file-name
  7688. (substitute-in-file-name (expand-file-name path))))
  7689. (apps (append org-file-apps (org-default-apps)))
  7690. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7691. (dirp (if remp nil (file-directory-p file)))
  7692. (file (if (and dirp org-open-directory-means-index-dot-org)
  7693. (concat (file-name-as-directory file) "index.org")
  7694. file))
  7695. (a-m-a-p (assq 'auto-mode apps))
  7696. (dfile (downcase file))
  7697. (old-buffer (current-buffer))
  7698. (old-pos (point))
  7699. (old-mode major-mode)
  7700. ext cmd)
  7701. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7702. (setq ext (match-string 1 dfile))
  7703. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7704. (setq ext (match-string 1 dfile))))
  7705. (cond
  7706. ((equal in-emacs '(16))
  7707. (setq cmd (cdr (assoc 'system apps))))
  7708. (in-emacs (setq cmd 'emacs))
  7709. (t
  7710. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7711. (and dirp (cdr (assoc 'directory apps)))
  7712. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7713. 'string-match)
  7714. (cdr (assoc ext apps))
  7715. (cdr (assoc t apps))))))
  7716. (when (eq cmd 'system)
  7717. (setq cmd (cdr (assoc 'system apps))))
  7718. (when (eq cmd 'default)
  7719. (setq cmd (cdr (assoc t apps))))
  7720. (when (eq cmd 'mailcap)
  7721. (require 'mailcap)
  7722. (mailcap-parse-mailcaps)
  7723. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7724. (command (mailcap-mime-info mime-type)))
  7725. (if (stringp command)
  7726. (setq cmd command)
  7727. (setq cmd 'emacs))))
  7728. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7729. (not (file-exists-p file))
  7730. (not org-open-non-existing-files))
  7731. (error "No such file: %s" file))
  7732. (cond
  7733. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7734. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7735. (while (string-match "['\"]%s['\"]" cmd)
  7736. (setq cmd (replace-match "%s" t t cmd)))
  7737. (while (string-match "%s" cmd)
  7738. (setq cmd (replace-match
  7739. (save-match-data
  7740. (shell-quote-argument
  7741. (convert-standard-filename file)))
  7742. t t cmd)))
  7743. (save-window-excursion
  7744. (start-process-shell-command cmd nil cmd)
  7745. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7746. ))
  7747. ((or (stringp cmd)
  7748. (eq cmd 'emacs))
  7749. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7750. (widen)
  7751. (if line (org-goto-line line)
  7752. (if search (org-link-search search))))
  7753. ((consp cmd)
  7754. (let ((file (convert-standard-filename file)))
  7755. (eval cmd)))
  7756. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7757. (and (org-mode-p) (eq old-mode 'org-mode)
  7758. (or (not (equal old-buffer (current-buffer)))
  7759. (not (equal old-pos (point))))
  7760. (org-mark-ring-push old-pos old-buffer))))
  7761. (defun org-default-apps ()
  7762. "Return the default applications for this operating system."
  7763. (cond
  7764. ((eq system-type 'darwin)
  7765. org-file-apps-defaults-macosx)
  7766. ((eq system-type 'windows-nt)
  7767. org-file-apps-defaults-windowsnt)
  7768. (t org-file-apps-defaults-gnu)))
  7769. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7770. "Convert extensions to regular expressions in the cars of LIST.
  7771. Also, weed out any non-string entries, because the return value is used
  7772. only for regexp matching.
  7773. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7774. point to the symbol `emacs', indicating that the file should
  7775. be opened in Emacs."
  7776. (append
  7777. (delq nil
  7778. (mapcar (lambda (x)
  7779. (if (not (stringp (car x)))
  7780. nil
  7781. (if (string-match "\\W" (car x))
  7782. x
  7783. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7784. list))
  7785. (if add-auto-mode
  7786. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7787. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7788. (defun org-file-remote-p (file)
  7789. "Test whether FILE specifies a location on a remote system.
  7790. Return non-nil if the location is indeed remote.
  7791. For example, the filename \"/user@host:/foo\" specifies a location
  7792. on the system \"/user@host:\"."
  7793. (cond ((fboundp 'file-remote-p)
  7794. (file-remote-p file))
  7795. ((fboundp 'tramp-handle-file-remote-p)
  7796. (tramp-handle-file-remote-p file))
  7797. ((and (boundp 'ange-ftp-name-format)
  7798. (string-match (car ange-ftp-name-format) file))
  7799. t)
  7800. (t nil)))
  7801. ;;;; Refiling
  7802. (defun org-get-org-file ()
  7803. "Read a filename, with default directory `org-directory'."
  7804. (let ((default (or org-default-notes-file remember-data-file)))
  7805. (read-file-name (format "File name [%s]: " default)
  7806. (file-name-as-directory org-directory)
  7807. default)))
  7808. (defun org-notes-order-reversed-p ()
  7809. "Check if the current file should receive notes in reversed order."
  7810. (cond
  7811. ((not org-reverse-note-order) nil)
  7812. ((eq t org-reverse-note-order) t)
  7813. ((not (listp org-reverse-note-order)) nil)
  7814. (t (catch 'exit
  7815. (let ((all org-reverse-note-order)
  7816. entry)
  7817. (while (setq entry (pop all))
  7818. (if (string-match (car entry) buffer-file-name)
  7819. (throw 'exit (cdr entry))))
  7820. nil)))))
  7821. (defvar org-refile-target-table nil
  7822. "The list of refile targets, created by `org-refile'.")
  7823. (defvar org-agenda-new-buffers nil
  7824. "Buffers created to visit agenda files.")
  7825. (defun org-get-refile-targets (&optional default-buffer)
  7826. "Produce a table with refile targets."
  7827. (let ((case-fold-search nil)
  7828. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  7829. (entries (or org-refile-targets '((nil . (:level . 1)))))
  7830. targets txt re files f desc descre fast-path-p level pos0)
  7831. (message "Getting targets...")
  7832. (with-current-buffer (or default-buffer (current-buffer))
  7833. (while (setq entry (pop entries))
  7834. (setq files (car entry) desc (cdr entry))
  7835. (setq fast-path-p nil)
  7836. (cond
  7837. ((null files) (setq files (list (current-buffer))))
  7838. ((eq files 'org-agenda-files)
  7839. (setq files (org-agenda-files 'unrestricted)))
  7840. ((and (symbolp files) (fboundp files))
  7841. (setq files (funcall files)))
  7842. ((and (symbolp files) (boundp files))
  7843. (setq files (symbol-value files))))
  7844. (if (stringp files) (setq files (list files)))
  7845. (cond
  7846. ((eq (car desc) :tag)
  7847. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7848. ((eq (car desc) :todo)
  7849. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7850. ((eq (car desc) :regexp)
  7851. (setq descre (cdr desc)))
  7852. ((eq (car desc) :level)
  7853. (setq descre (concat "^\\*\\{" (number-to-string
  7854. (if org-odd-levels-only
  7855. (1- (* 2 (cdr desc)))
  7856. (cdr desc)))
  7857. "\\}[ \t]")))
  7858. ((eq (car desc) :maxlevel)
  7859. (setq fast-path-p t)
  7860. (setq descre (concat "^\\*\\{1," (number-to-string
  7861. (if org-odd-levels-only
  7862. (1- (* 2 (cdr desc)))
  7863. (cdr desc)))
  7864. "\\}[ \t]")))
  7865. (t (error "Bad refiling target description %s" desc)))
  7866. (while (setq f (pop files))
  7867. (save-excursion
  7868. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7869. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7870. (setq f (expand-file-name f))
  7871. (if (eq org-refile-use-outline-path 'file)
  7872. (push (list (file-name-nondirectory f) f nil nil) targets))
  7873. (save-excursion
  7874. (save-restriction
  7875. (widen)
  7876. (goto-char (point-min))
  7877. (while (re-search-forward descre nil t)
  7878. (goto-char (setq pos0 (point-at-bol)))
  7879. (catch 'next
  7880. (when org-refile-target-verify-function
  7881. (save-match-data
  7882. (or (funcall org-refile-target-verify-function)
  7883. (throw 'next t))))
  7884. (when (looking-at org-complex-heading-regexp)
  7885. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7886. txt (org-link-display-format (match-string 4))
  7887. re (concat "^" (regexp-quote
  7888. (buffer-substring (match-beginning 1)
  7889. (match-end 4)))))
  7890. (if (match-end 5) (setq re (concat re "[ \t]+"
  7891. (regexp-quote
  7892. (match-string 5)))))
  7893. (setq re (concat re "[ \t]*$"))
  7894. (when org-refile-use-outline-path
  7895. (setq txt (mapconcat 'org-protect-slash
  7896. (append
  7897. (if (eq org-refile-use-outline-path 'file)
  7898. (list (file-name-nondirectory
  7899. (buffer-file-name (buffer-base-buffer))))
  7900. (if (eq org-refile-use-outline-path 'full-file-path)
  7901. (list (buffer-file-name (buffer-base-buffer)))))
  7902. (org-get-outline-path fast-path-p level txt)
  7903. (list txt))
  7904. "/")))
  7905. (push (list txt f re (point)) targets)))
  7906. (when (= (point) pos0)
  7907. ;; verification function has not moved point
  7908. (goto-char (point-at-eol))))))))))
  7909. (message "Getting targets...done")
  7910. (nreverse targets)))
  7911. (defun org-protect-slash (s)
  7912. (while (string-match "/" s)
  7913. (setq s (replace-match "\\" t t s)))
  7914. s)
  7915. (defvar org-olpa (make-vector 20 nil))
  7916. (defun org-get-outline-path (&optional fastp level heading)
  7917. "Return the outline path to the current entry, as a list."
  7918. (if fastp
  7919. (progn
  7920. (if (> level 19)
  7921. (error "Outline path failure, more than 19 levels."))
  7922. (loop for i from level upto 19 do
  7923. (aset org-olpa i nil))
  7924. (prog1
  7925. (delq nil (append org-olpa nil))
  7926. (aset org-olpa level heading)))
  7927. (let (rtn)
  7928. (save-excursion
  7929. (while (org-up-heading-safe)
  7930. (when (looking-at org-complex-heading-regexp)
  7931. (push (org-match-string-no-properties 4) rtn)))
  7932. rtn))))
  7933. (defvar org-refile-history nil
  7934. "History for refiling operations.")
  7935. (defvar org-after-refile-insert-hook nil
  7936. "Hook run after `org-refile' has inserted its stuff at the new location.
  7937. Note that this is still *before* the stuff will be removed from
  7938. the *old* location.")
  7939. (defun org-refile (&optional goto default-buffer rfloc)
  7940. "Move the entry at point to another heading.
  7941. The list of target headings is compiled using the information in
  7942. `org-refile-targets', which see. This list is created before each use
  7943. and will therefore always be up-to-date.
  7944. At the target location, the entry is filed as a subitem of the target heading.
  7945. Depending on `org-reverse-note-order', the new subitem will either be the
  7946. first or the last subitem.
  7947. If there is an active region, all entries in that region will be moved.
  7948. However, the region must fulfil the requirement that the first heading
  7949. is the first one sets the top-level of the moved text - at most siblings
  7950. below it are allowed.
  7951. With prefix arg GOTO, the command will only visit the target location,
  7952. not actually move anything.
  7953. With a double prefix `C-u C-u', go to the location where the last refiling
  7954. operation has put the subtree.
  7955. RFLOC can be a refile location obtained in a different way.
  7956. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7957. (interactive "P")
  7958. (let* ((cbuf (current-buffer))
  7959. (regionp (org-region-active-p))
  7960. (region-start (and regionp (region-beginning)))
  7961. (region-end (and regionp (region-end)))
  7962. (region-length (and regionp (- region-end region-start)))
  7963. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7964. pos it nbuf file re level reversed)
  7965. (when regionp
  7966. (goto-char region-start)
  7967. (or (bolp) (goto-char (point-at-bol)))
  7968. (setq region-start (point))
  7969. (unless (org-kill-is-subtree-p
  7970. (buffer-substring region-start region-end))
  7971. (error "The region is not a (sequence of) subtree(s)")))
  7972. (if (equal goto '(16))
  7973. (org-refile-goto-last-stored)
  7974. (when (setq it (or rfloc
  7975. (save-excursion
  7976. (org-refile-get-location
  7977. (if goto "Goto: " "Refile to: ") default-buffer
  7978. org-refile-allow-creating-parent-nodes))))
  7979. (setq file (nth 1 it)
  7980. re (nth 2 it)
  7981. pos (nth 3 it))
  7982. (if (and (not goto)
  7983. pos
  7984. (equal (buffer-file-name) file)
  7985. (if regionp
  7986. (and (>= pos region-start)
  7987. (<= pos region-end))
  7988. (and (>= pos (point))
  7989. (< pos (save-excursion
  7990. (org-end-of-subtree t t))))))
  7991. (error "Cannot refile to position inside the tree or region"))
  7992. (setq nbuf (or (find-buffer-visiting file)
  7993. (find-file-noselect file)))
  7994. (if goto
  7995. (progn
  7996. (switch-to-buffer nbuf)
  7997. (goto-char pos)
  7998. (org-show-context 'org-goto))
  7999. (if regionp
  8000. (progn
  8001. (org-kill-new (buffer-substring region-start region-end))
  8002. (org-save-markers-in-region region-start region-end))
  8003. (org-copy-subtree 1 nil t))
  8004. (save-excursion
  8005. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  8006. (find-file-noselect file))))
  8007. (setq reversed (org-notes-order-reversed-p))
  8008. (save-excursion
  8009. (save-restriction
  8010. (widen)
  8011. (if pos
  8012. (progn
  8013. (goto-char pos)
  8014. (looking-at outline-regexp)
  8015. (setq level (org-get-valid-level (funcall outline-level) 1))
  8016. (goto-char
  8017. (if reversed
  8018. (or (outline-next-heading) (point-max))
  8019. (or (save-excursion (org-get-next-sibling))
  8020. (org-end-of-subtree t t)
  8021. (point-max)))))
  8022. (setq level 1)
  8023. (if (not reversed)
  8024. (goto-char (point-max))
  8025. (goto-char (point-min))
  8026. (or (outline-next-heading) (goto-char (point-max)))))
  8027. (if (not (bolp)) (newline))
  8028. (bookmark-set "org-refile-last-stored")
  8029. (org-paste-subtree level)
  8030. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8031. (run-hooks 'org-after-refile-insert-hook))))
  8032. (if regionp
  8033. (delete-region (point) (+ (point) region-length))
  8034. (org-cut-subtree))
  8035. (when (featurep 'org-inlinetask)
  8036. (org-inlinetask-remove-END-maybe))
  8037. (setq org-markers-to-move nil)
  8038. (message "Refiled to \"%s\"" (car it))))))
  8039. (org-reveal))
  8040. (defun org-refile-goto-last-stored ()
  8041. "Go to the location where the last refile was stored."
  8042. (interactive)
  8043. (bookmark-jump "org-refile-last-stored")
  8044. (message "This is the location of the last refile"))
  8045. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  8046. "Prompt the user for a refile location, using PROMPT."
  8047. (let ((org-refile-targets org-refile-targets)
  8048. (org-refile-use-outline-path org-refile-use-outline-path))
  8049. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  8050. (unless org-refile-target-table
  8051. (error "No refile targets"))
  8052. (let* ((cbuf (current-buffer))
  8053. (partial-completion-mode nil)
  8054. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  8055. (cfunc (if (and org-refile-use-outline-path
  8056. org-outline-path-complete-in-steps)
  8057. 'org-olpath-completing-read
  8058. 'org-icompleting-read))
  8059. (extra (if org-refile-use-outline-path "/" ""))
  8060. (filename (and cfn (expand-file-name cfn)))
  8061. (tbl (mapcar
  8062. (lambda (x)
  8063. (if (and (not (member org-refile-use-outline-path
  8064. '(file full-file-path)))
  8065. (not (equal filename (nth 1 x))))
  8066. (cons (concat (car x) extra " ("
  8067. (file-name-nondirectory (nth 1 x)) ")")
  8068. (cdr x))
  8069. (cons (concat (car x) extra) (cdr x))))
  8070. org-refile-target-table))
  8071. (completion-ignore-case t)
  8072. pa answ parent-target child parent old-hist)
  8073. (setq old-hist org-refile-history)
  8074. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  8075. nil 'org-refile-history))
  8076. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  8077. (if pa
  8078. (progn
  8079. (when (or (not org-refile-history)
  8080. (not (eq old-hist org-refile-history))
  8081. (not (equal (car pa) (car org-refile-history))))
  8082. (setq org-refile-history
  8083. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  8084. org-refile-history
  8085. (cdr org-refile-history))))
  8086. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  8087. (pop org-refile-history)))
  8088. pa)
  8089. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8090. (setq parent (match-string 1 answ)
  8091. child (match-string 2 answ))
  8092. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  8093. (when (and parent-target
  8094. (or (eq new-nodes t)
  8095. (and (eq new-nodes 'confirm)
  8096. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  8097. (org-refile-new-child parent-target child))))))
  8098. (defun org-refile-new-child (parent-target child)
  8099. "Use refile target PARENT-TARGET to add new CHILD below it."
  8100. (unless parent-target
  8101. (error "Cannot find parent for new node"))
  8102. (let ((file (nth 1 parent-target))
  8103. (pos (nth 3 parent-target))
  8104. level)
  8105. (with-current-buffer (or (find-buffer-visiting file)
  8106. (find-file-noselect file))
  8107. (save-excursion
  8108. (save-restriction
  8109. (widen)
  8110. (if pos
  8111. (goto-char pos)
  8112. (goto-char (point-max))
  8113. (if (not (bolp)) (newline)))
  8114. (when (looking-at outline-regexp)
  8115. (setq level (funcall outline-level))
  8116. (org-end-of-subtree t t))
  8117. (org-back-over-empty-lines)
  8118. (insert "\n" (make-string
  8119. (if pos (org-get-valid-level level 1) 1) ?*)
  8120. " " child "\n")
  8121. (beginning-of-line 0)
  8122. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8123. (defun org-olpath-completing-read (prompt collection &rest args)
  8124. "Read an outline path like a file name."
  8125. (let ((thetable collection)
  8126. (org-completion-use-ido nil) ; does not work with ido.
  8127. (org-completion-use-iswitchb nil)) ; or iswitchb
  8128. (apply
  8129. 'org-icompleting-read prompt
  8130. (lambda (string predicate &optional flag)
  8131. (let (rtn r f (l (length string)))
  8132. (cond
  8133. ((eq flag nil)
  8134. ;; try completion
  8135. (try-completion string thetable))
  8136. ((eq flag t)
  8137. ;; all-completions
  8138. (setq rtn (all-completions string thetable predicate))
  8139. (mapcar
  8140. (lambda (x)
  8141. (setq r (substring x l))
  8142. (if (string-match " ([^)]*)$" x)
  8143. (setq f (match-string 0 x))
  8144. (setq f ""))
  8145. (if (string-match "/" r)
  8146. (concat string (substring r 0 (match-end 0)) f)
  8147. x))
  8148. rtn))
  8149. ((eq flag 'lambda)
  8150. ;; exact match?
  8151. (assoc string thetable)))
  8152. ))
  8153. args)))
  8154. ;;;; Dynamic blocks
  8155. (defun org-find-dblock (name)
  8156. "Find the first dynamic block with name NAME in the buffer.
  8157. If not found, stay at current position and return nil."
  8158. (let (pos)
  8159. (save-excursion
  8160. (goto-char (point-min))
  8161. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8162. nil t)
  8163. (match-beginning 0))))
  8164. (if pos (goto-char pos))
  8165. pos))
  8166. (defconst org-dblock-start-re
  8167. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8168. "Matches the startline of a dynamic block, with parameters.")
  8169. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  8170. "Matches the end of a dynamic block.")
  8171. (defun org-create-dblock (plist)
  8172. "Create a dynamic block section, with parameters taken from PLIST.
  8173. PLIST must contain a :name entry which is used as name of the block."
  8174. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  8175. (end-of-line 1)
  8176. (newline))
  8177. (let ((col (current-column))
  8178. (name (plist-get plist :name)))
  8179. (insert "#+BEGIN: " name)
  8180. (while plist
  8181. (if (eq (car plist) :name)
  8182. (setq plist (cddr plist))
  8183. (insert " " (prin1-to-string (pop plist)))))
  8184. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  8185. (beginning-of-line -2)))
  8186. (defun org-prepare-dblock ()
  8187. "Prepare dynamic block for refresh.
  8188. This empties the block, puts the cursor at the insert position and returns
  8189. the property list including an extra property :name with the block name."
  8190. (unless (looking-at org-dblock-start-re)
  8191. (error "Not at a dynamic block"))
  8192. (let* ((begdel (1+ (match-end 0)))
  8193. (name (org-no-properties (match-string 1)))
  8194. (params (append (list :name name)
  8195. (read (concat "(" (match-string 3) ")")))))
  8196. (save-excursion
  8197. (beginning-of-line 1)
  8198. (skip-chars-forward " \t")
  8199. (setq params (plist-put params :indentation-column (current-column))))
  8200. (unless (re-search-forward org-dblock-end-re nil t)
  8201. (error "Dynamic block not terminated"))
  8202. (setq params
  8203. (append params
  8204. (list :content (buffer-substring
  8205. begdel (match-beginning 0)))))
  8206. (delete-region begdel (match-beginning 0))
  8207. (goto-char begdel)
  8208. (open-line 1)
  8209. params))
  8210. (defun org-map-dblocks (&optional command)
  8211. "Apply COMMAND to all dynamic blocks in the current buffer.
  8212. If COMMAND is not given, use `org-update-dblock'."
  8213. (let ((cmd (or command 'org-update-dblock))
  8214. pos)
  8215. (save-excursion
  8216. (goto-char (point-min))
  8217. (while (re-search-forward org-dblock-start-re nil t)
  8218. (goto-char (setq pos (match-beginning 0)))
  8219. (condition-case nil
  8220. (funcall cmd)
  8221. (error (message "Error during update of dynamic block")))
  8222. (goto-char pos)
  8223. (unless (re-search-forward org-dblock-end-re nil t)
  8224. (error "Dynamic block not terminated"))))))
  8225. (defun org-dblock-update (&optional arg)
  8226. "User command for updating dynamic blocks.
  8227. Update the dynamic block at point. With prefix ARG, update all dynamic
  8228. blocks in the buffer."
  8229. (interactive "P")
  8230. (if arg
  8231. (org-update-all-dblocks)
  8232. (or (looking-at org-dblock-start-re)
  8233. (org-beginning-of-dblock))
  8234. (org-update-dblock)))
  8235. (defun org-update-dblock ()
  8236. "Update the dynamic block at point
  8237. This means to empty the block, parse for parameters and then call
  8238. the correct writing function."
  8239. (save-window-excursion
  8240. (let* ((pos (point))
  8241. (line (org-current-line))
  8242. (params (org-prepare-dblock))
  8243. (name (plist-get params :name))
  8244. (indent (plist-get params :indentation-column))
  8245. (cmd (intern (concat "org-dblock-write:" name))))
  8246. (message "Updating dynamic block `%s' at line %d..." name line)
  8247. (funcall cmd params)
  8248. (message "Updating dynamic block `%s' at line %d...done" name line)
  8249. (goto-char pos)
  8250. (when (and indent (> indent 0))
  8251. (setq indent (make-string indent ?\ ))
  8252. (save-excursion
  8253. (org-beginning-of-dblock)
  8254. (forward-line 1)
  8255. (while (not (looking-at org-dblock-end-re))
  8256. (insert indent)
  8257. (beginning-of-line 2))
  8258. (when (looking-at org-dblock-end-re)
  8259. (and (looking-at "[ \t]+")
  8260. (replace-match ""))
  8261. (insert indent)))))))
  8262. (defun org-beginning-of-dblock ()
  8263. "Find the beginning of the dynamic block at point.
  8264. Error if there is no such block at point."
  8265. (let ((pos (point))
  8266. beg)
  8267. (end-of-line 1)
  8268. (if (and (re-search-backward org-dblock-start-re nil t)
  8269. (setq beg (match-beginning 0))
  8270. (re-search-forward org-dblock-end-re nil t)
  8271. (> (match-end 0) pos))
  8272. (goto-char beg)
  8273. (goto-char pos)
  8274. (error "Not in a dynamic block"))))
  8275. (defun org-update-all-dblocks ()
  8276. "Update all dynamic blocks in the buffer.
  8277. This function can be used in a hook."
  8278. (when (org-mode-p)
  8279. (org-map-dblocks 'org-update-dblock)))
  8280. ;;;; Completion
  8281. (defconst org-additional-option-like-keywords
  8282. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8283. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8284. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:" "LATEX_CLASS:" "ATTR_LaTeX"
  8285. "BEGIN:" "END:"
  8286. "ORGTBL" "TBLFM:" "TBLNAME:"
  8287. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8288. "BEGIN_QUOTE" "END_QUOTE"
  8289. "BEGIN_VERSE" "END_VERSE"
  8290. "BEGIN_CENTER" "END_CENTER"
  8291. "BEGIN_SRC" "END_SRC"
  8292. "CATEGORY" "COLUMNS"
  8293. "CAPTION" "LABEL"
  8294. "BIND"
  8295. "MACRO"))
  8296. (defcustom org-structure-template-alist
  8297. '(
  8298. ("s" "#+begin_src ?\n\n#+end_src"
  8299. "<src lang=\"?\">\n\n</src>")
  8300. ("e" "#+begin_example\n?\n#+end_example"
  8301. "<example>\n?\n</example>")
  8302. ("q" "#+begin_quote\n?\n#+end_quote"
  8303. "<quote>\n?\n</quote>")
  8304. ("v" "#+begin_verse\n?\n#+end_verse"
  8305. "<verse>\n?\n/verse>")
  8306. ("c" "#+begin_center\n?\n#+end_center"
  8307. "<center>\n?\n/center>")
  8308. ("l" "#+begin_latex\n?\n#+end_latex"
  8309. "<literal style=\"latex\">\n?\n</literal>")
  8310. ("L" "#+latex: "
  8311. "<literal style=\"latex\">?</literal>")
  8312. ("h" "#+begin_html\n?\n#+end_html"
  8313. "<literal style=\"html\">\n?\n</literal>")
  8314. ("H" "#+html: "
  8315. "<literal style=\"html\">?</literal>")
  8316. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8317. ("A" "#+ascii: ")
  8318. ("i" "#+include %file ?"
  8319. "<include file=%file markup=\"?\">")
  8320. )
  8321. "Structure completion elements.
  8322. This is a list of abbreviation keys and values. The value gets inserted
  8323. it you type @samp{.} followed by the key and then the completion key,
  8324. usually `M-TAB'. %file will be replaced by a file name after prompting
  8325. for the file using completion.
  8326. There are two templates for each key, the first uses the original Org syntax,
  8327. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8328. the default when the /org-mtags.el/ module has been loaded. See also the
  8329. variable `org-mtags-prefer-muse-templates'.
  8330. This is an experimental feature, it is undecided if it is going to stay in."
  8331. :group 'org-completion
  8332. :type '(repeat
  8333. (string :tag "Key")
  8334. (string :tag "Template")
  8335. (string :tag "Muse Template")))
  8336. (defun org-try-structure-completion ()
  8337. "Try to complete a structure template before point.
  8338. This looks for strings like \"<e\" on an otherwise empty line and
  8339. expands them."
  8340. (let ((l (buffer-substring (point-at-bol) (point)))
  8341. a)
  8342. (when (and (looking-at "[ \t]*$")
  8343. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8344. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8345. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8346. (match-beginning 1)) a)
  8347. t)))
  8348. (defun org-complete-expand-structure-template (start cell)
  8349. "Expand a structure template."
  8350. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8351. (rpl (nth (if musep 2 1) cell))
  8352. (ind ""))
  8353. (delete-region start (point))
  8354. (when (string-match "\\`#\\+" rpl)
  8355. (cond
  8356. ((bolp))
  8357. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8358. (setq ind (buffer-substring (point-at-bol) (point))))
  8359. (t (newline))))
  8360. (setq start (point))
  8361. (if (string-match "%file" rpl)
  8362. (setq rpl (replace-match
  8363. (concat
  8364. "\""
  8365. (save-match-data
  8366. (abbreviate-file-name (read-file-name "Include file: ")))
  8367. "\"")
  8368. t t rpl)))
  8369. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  8370. (concat "\n" ind)))
  8371. (insert rpl)
  8372. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8373. (defun org-complete (&optional arg)
  8374. "Perform completion on word at point.
  8375. At the beginning of a headline, this completes TODO keywords as given in
  8376. `org-todo-keywords'.
  8377. If the current word is preceded by a backslash, completes the TeX symbols
  8378. that are supported for HTML support.
  8379. If the current word is preceded by \"#+\", completes special words for
  8380. setting file options.
  8381. In the line after \"#+STARTUP:, complete valid keywords.\"
  8382. At all other locations, this simply calls the value of
  8383. `org-completion-fallback-command'."
  8384. (interactive "P")
  8385. (org-without-partial-completion
  8386. (catch 'exit
  8387. (let* ((a nil)
  8388. (end (point))
  8389. (beg1 (save-excursion
  8390. (skip-chars-backward (org-re "[:alnum:]_@"))
  8391. (point)))
  8392. (beg (save-excursion
  8393. (skip-chars-backward "a-zA-Z0-9_:$")
  8394. (point)))
  8395. (confirm (lambda (x) (stringp (car x))))
  8396. (searchhead (equal (char-before beg) ?*))
  8397. (struct
  8398. (when (and (member (char-before beg1) '(?. ?<))
  8399. (setq a (assoc (buffer-substring beg1 (point))
  8400. org-structure-template-alist)))
  8401. (org-complete-expand-structure-template (1- beg1) a)
  8402. (throw 'exit t)))
  8403. (tag (and (equal (char-before beg1) ?:)
  8404. (equal (char-after (point-at-bol)) ?*)))
  8405. (prop (and (equal (char-before beg1) ?:)
  8406. (not (equal (char-after (point-at-bol)) ?*))))
  8407. (texp (equal (char-before beg) ?\\))
  8408. (link (equal (char-before beg) ?\[))
  8409. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8410. beg)
  8411. "#+"))
  8412. (startup (string-match "^#\\+STARTUP:.*"
  8413. (buffer-substring (point-at-bol) (point))))
  8414. (completion-ignore-case opt)
  8415. (type nil)
  8416. (tbl nil)
  8417. (table (cond
  8418. (opt
  8419. (setq type :opt)
  8420. (require 'org-exp)
  8421. (append
  8422. (delq nil
  8423. (mapcar
  8424. (lambda (x)
  8425. (if (string-match
  8426. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8427. (cons (match-string 2 x)
  8428. (match-string 1 x))))
  8429. (org-split-string (org-get-current-options) "\n")))
  8430. (mapcar 'list org-additional-option-like-keywords)))
  8431. (startup
  8432. (setq type :startup)
  8433. org-startup-options)
  8434. (link (append org-link-abbrev-alist-local
  8435. org-link-abbrev-alist))
  8436. (texp
  8437. (setq type :tex)
  8438. org-html-entities)
  8439. ((string-match "\\`\\*+[ \t]+\\'"
  8440. (buffer-substring (point-at-bol) beg))
  8441. (setq type :todo)
  8442. (mapcar 'list org-todo-keywords-1))
  8443. (searchhead
  8444. (setq type :searchhead)
  8445. (save-excursion
  8446. (goto-char (point-min))
  8447. (while (re-search-forward org-todo-line-regexp nil t)
  8448. (push (list
  8449. (org-make-org-heading-search-string
  8450. (match-string 3) t))
  8451. tbl)))
  8452. tbl)
  8453. (tag (setq type :tag beg beg1)
  8454. (or org-tag-alist (org-get-buffer-tags)))
  8455. (prop (setq type :prop beg beg1)
  8456. (mapcar 'list (org-buffer-property-keys nil t t)))
  8457. (t (progn
  8458. (call-interactively org-completion-fallback-command)
  8459. (throw 'exit nil)))))
  8460. (pattern (buffer-substring-no-properties beg end))
  8461. (completion (try-completion pattern table confirm)))
  8462. (cond ((eq completion t)
  8463. (if (not (assoc (upcase pattern) table))
  8464. (message "Already complete")
  8465. (if (and (equal type :opt)
  8466. (not (member (car (assoc (upcase pattern) table))
  8467. org-additional-option-like-keywords)))
  8468. (insert (substring (cdr (assoc (upcase pattern) table))
  8469. (length pattern)))
  8470. (if (memq type '(:tag :prop)) (insert ":")))))
  8471. ((null completion)
  8472. (message "Can't find completion for \"%s\"" pattern)
  8473. (ding))
  8474. ((not (string= pattern completion))
  8475. (delete-region beg end)
  8476. (if (string-match " +$" completion)
  8477. (setq completion (replace-match "" t t completion)))
  8478. (insert completion)
  8479. (if (get-buffer-window "*Completions*")
  8480. (delete-window (get-buffer-window "*Completions*")))
  8481. (if (assoc completion table)
  8482. (if (eq type :todo) (insert " ")
  8483. (if (memq type '(:tag :prop)) (insert ":"))))
  8484. (if (and (equal type :opt) (assoc completion table))
  8485. (message "%s" (substitute-command-keys
  8486. "Press \\[org-complete] again to insert example settings"))))
  8487. (t
  8488. (message "Making completion list...")
  8489. (let ((list (sort (all-completions pattern table confirm)
  8490. 'string<)))
  8491. (with-output-to-temp-buffer "*Completions*"
  8492. (condition-case nil
  8493. ;; Protection needed for XEmacs and emacs 21
  8494. (display-completion-list list pattern)
  8495. (error (display-completion-list list)))))
  8496. (message "Making completion list...%s" "done")))))))
  8497. ;;;; TODO, DEADLINE, Comments
  8498. (defun org-toggle-comment ()
  8499. "Change the COMMENT state of an entry."
  8500. (interactive)
  8501. (save-excursion
  8502. (org-back-to-heading)
  8503. (let (case-fold-search)
  8504. (if (looking-at (concat outline-regexp
  8505. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8506. (replace-match "" t t nil 1)
  8507. (if (looking-at outline-regexp)
  8508. (progn
  8509. (goto-char (match-end 0))
  8510. (insert org-comment-string " ")))))))
  8511. (defvar org-last-todo-state-is-todo nil
  8512. "This is non-nil when the last TODO state change led to a TODO state.
  8513. If the last change removed the TODO tag or switched to DONE, then
  8514. this is nil.")
  8515. (defvar org-setting-tags nil) ; dynamically skipped
  8516. (defun org-parse-local-options (string var)
  8517. "Parse STRING for startup setting relevant for variable VAR."
  8518. (let ((rtn (symbol-value var))
  8519. e opts)
  8520. (save-match-data
  8521. (if (or (not string) (not (string-match "\\S-" string)))
  8522. rtn
  8523. (setq opts (delq nil (mapcar (lambda (x)
  8524. (setq e (assoc x org-startup-options))
  8525. (if (eq (nth 1 e) var) e nil))
  8526. (org-split-string string "[ \t]+"))))
  8527. (if (not opts)
  8528. rtn
  8529. (setq rtn nil)
  8530. (while (setq e (pop opts))
  8531. (if (not (nth 3 e))
  8532. (setq rtn (nth 2 e))
  8533. (if (not (listp rtn)) (setq rtn nil))
  8534. (push (nth 2 e) rtn)))
  8535. rtn)))))
  8536. (defvar org-todo-setup-filter-hook nil
  8537. "Hook for functions that pre-filter todo specs.
  8538. Each function takes a todo spec and returns either `nil' or the spec
  8539. transformed into canonical form." )
  8540. (defvar org-todo-get-default-hook nil
  8541. "Hook for functions that get a default item for todo.
  8542. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8543. `nil' or a string to be used for the todo mark." )
  8544. (defvar org-agenda-headline-snapshot-before-repeat)
  8545. (defun org-todo (&optional arg)
  8546. "Change the TODO state of an item.
  8547. The state of an item is given by a keyword at the start of the heading,
  8548. like
  8549. *** TODO Write paper
  8550. *** DONE Call mom
  8551. The different keywords are specified in the variable `org-todo-keywords'.
  8552. By default the available states are \"TODO\" and \"DONE\".
  8553. So for this example: when the item starts with TODO, it is changed to DONE.
  8554. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8555. DONE are present, add TODO at the beginning of the heading.
  8556. With C-u prefix arg, use completion to determine the new state.
  8557. With numeric prefix arg, switch to that state.
  8558. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8559. With a tripple C-u prefix, circumvent any state blocking.
  8560. For calling through lisp, arg is also interpreted in the following way:
  8561. 'none -> empty state
  8562. \"\"(empty string) -> switch to empty state
  8563. 'done -> switch to DONE
  8564. 'nextset -> switch to the next set of keywords
  8565. 'previousset -> switch to the previous set of keywords
  8566. \"WAITING\" -> switch to the specified keyword, but only if it
  8567. really is a member of `org-todo-keywords'."
  8568. (interactive "P")
  8569. (if (equal arg '(16)) (setq arg 'nextset))
  8570. (let ((org-blocker-hook org-blocker-hook)
  8571. (case-fold-search nil))
  8572. (when (equal arg '(64))
  8573. (setq arg nil org-blocker-hook nil))
  8574. (when (and org-blocker-hook
  8575. (or org-inhibit-blocking
  8576. (org-entry-get nil "NOBLOCKING")))
  8577. (setq org-blocker-hook nil))
  8578. (save-excursion
  8579. (catch 'exit
  8580. (org-back-to-heading t)
  8581. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8582. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8583. (looking-at " *"))
  8584. (let* ((match-data (match-data))
  8585. (startpos (point-at-bol))
  8586. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8587. (org-log-done org-log-done)
  8588. (org-log-repeat org-log-repeat)
  8589. (org-todo-log-states org-todo-log-states)
  8590. (this (match-string 1))
  8591. (hl-pos (match-beginning 0))
  8592. (head (org-get-todo-sequence-head this))
  8593. (ass (assoc head org-todo-kwd-alist))
  8594. (interpret (nth 1 ass))
  8595. (done-word (nth 3 ass))
  8596. (final-done-word (nth 4 ass))
  8597. (last-state (or this ""))
  8598. (completion-ignore-case t)
  8599. (member (member this org-todo-keywords-1))
  8600. (tail (cdr member))
  8601. (state (cond
  8602. ((and org-todo-key-trigger
  8603. (or (and (equal arg '(4))
  8604. (eq org-use-fast-todo-selection 'prefix))
  8605. (and (not arg) org-use-fast-todo-selection
  8606. (not (eq org-use-fast-todo-selection
  8607. 'prefix)))))
  8608. ;; Use fast selection
  8609. (org-fast-todo-selection))
  8610. ((and (equal arg '(4))
  8611. (or (not org-use-fast-todo-selection)
  8612. (not org-todo-key-trigger)))
  8613. ;; Read a state with completion
  8614. (org-icompleting-read
  8615. "State: " (mapcar (lambda(x) (list x))
  8616. org-todo-keywords-1)
  8617. nil t))
  8618. ((eq arg 'right)
  8619. (if this
  8620. (if tail (car tail) nil)
  8621. (car org-todo-keywords-1)))
  8622. ((eq arg 'left)
  8623. (if (equal member org-todo-keywords-1)
  8624. nil
  8625. (if this
  8626. (nth (- (length org-todo-keywords-1)
  8627. (length tail) 2)
  8628. org-todo-keywords-1)
  8629. (org-last org-todo-keywords-1))))
  8630. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8631. (setq arg nil))) ; hack to fall back to cycling
  8632. (arg
  8633. ;; user or caller requests a specific state
  8634. (cond
  8635. ((equal arg "") nil)
  8636. ((eq arg 'none) nil)
  8637. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8638. ((eq arg 'nextset)
  8639. (or (car (cdr (member head org-todo-heads)))
  8640. (car org-todo-heads)))
  8641. ((eq arg 'previousset)
  8642. (let ((org-todo-heads (reverse org-todo-heads)))
  8643. (or (car (cdr (member head org-todo-heads)))
  8644. (car org-todo-heads))))
  8645. ((car (member arg org-todo-keywords-1)))
  8646. ((stringp arg)
  8647. (error "State `%s' not valid in this file" arg))
  8648. ((nth (1- (prefix-numeric-value arg))
  8649. org-todo-keywords-1))))
  8650. ((null member) (or head (car org-todo-keywords-1)))
  8651. ((equal this final-done-word) nil) ;; -> make empty
  8652. ((null tail) nil) ;; -> first entry
  8653. ((memq interpret '(type priority))
  8654. (if (eq this-command last-command)
  8655. (car tail)
  8656. (if (> (length tail) 0)
  8657. (or done-word (car org-done-keywords))
  8658. nil)))
  8659. (t
  8660. (car tail))))
  8661. (state (or
  8662. (run-hook-with-args-until-success
  8663. 'org-todo-get-default-hook state last-state)
  8664. state))
  8665. (next (if state (concat " " state " ") " "))
  8666. (change-plist (list :type 'todo-state-change :from this :to state
  8667. :position startpos))
  8668. dolog now-done-p)
  8669. (when org-blocker-hook
  8670. (setq org-last-todo-state-is-todo
  8671. (not (member this org-done-keywords)))
  8672. (unless (save-excursion
  8673. (save-match-data
  8674. (run-hook-with-args-until-failure
  8675. 'org-blocker-hook change-plist)))
  8676. (if (interactive-p)
  8677. (error "TODO state change from %s to %s blocked" this state)
  8678. ;; fail silently
  8679. (message "TODO state change from %s to %s blocked" this state)
  8680. (throw 'exit nil))))
  8681. (store-match-data match-data)
  8682. (replace-match next t t)
  8683. (unless (pos-visible-in-window-p hl-pos)
  8684. (message "TODO state changed to %s" (org-trim next)))
  8685. (unless head
  8686. (setq head (org-get-todo-sequence-head state)
  8687. ass (assoc head org-todo-kwd-alist)
  8688. interpret (nth 1 ass)
  8689. done-word (nth 3 ass)
  8690. final-done-word (nth 4 ass)))
  8691. (when (memq arg '(nextset previousset))
  8692. (message "Keyword-Set %d/%d: %s"
  8693. (- (length org-todo-sets) -1
  8694. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8695. (length org-todo-sets)
  8696. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8697. (setq org-last-todo-state-is-todo
  8698. (not (member state org-done-keywords)))
  8699. (setq now-done-p (and (member state org-done-keywords)
  8700. (not (member this org-done-keywords))))
  8701. (and logging (org-local-logging logging))
  8702. (when (and (or org-todo-log-states org-log-done)
  8703. (not (eq org-inhibit-logging t))
  8704. (not (memq arg '(nextset previousset))))
  8705. ;; we need to look at recording a time and note
  8706. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8707. (nth 2 (assoc this org-todo-log-states))))
  8708. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  8709. (setq dolog 'time))
  8710. (when (and state
  8711. (member state org-not-done-keywords)
  8712. (not (member this org-not-done-keywords)))
  8713. ;; This is now a todo state and was not one before
  8714. ;; If there was a CLOSED time stamp, get rid of it.
  8715. (org-add-planning-info nil nil 'closed))
  8716. (when (and now-done-p org-log-done)
  8717. ;; It is now done, and it was not done before
  8718. (org-add-planning-info 'closed (org-current-time))
  8719. (if (and (not dolog) (eq 'note org-log-done))
  8720. (org-add-log-setup 'done state this 'findpos 'note)))
  8721. (when (and state dolog)
  8722. ;; This is a non-nil state, and we need to log it
  8723. (org-add-log-setup 'state state this 'findpos dolog)))
  8724. ;; Fixup tag positioning
  8725. (org-todo-trigger-tag-changes state)
  8726. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8727. (when org-provide-todo-statistics
  8728. (org-update-parent-todo-statistics))
  8729. (run-hooks 'org-after-todo-state-change-hook)
  8730. (if (and arg (not (member state org-done-keywords)))
  8731. (setq head (org-get-todo-sequence-head state)))
  8732. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8733. ;; Do we need to trigger a repeat?
  8734. (when now-done-p
  8735. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8736. ;; This is for the agenda, take a snapshot of the headline.
  8737. (save-match-data
  8738. (setq org-agenda-headline-snapshot-before-repeat
  8739. (org-get-heading))))
  8740. (org-auto-repeat-maybe state))
  8741. ;; Fixup cursor location if close to the keyword
  8742. (if (and (outline-on-heading-p)
  8743. (not (bolp))
  8744. (save-excursion (beginning-of-line 1)
  8745. (looking-at org-todo-line-regexp))
  8746. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8747. (progn
  8748. (goto-char (or (match-end 2) (match-end 1)))
  8749. (and (looking-at " ") (just-one-space))))
  8750. (when org-trigger-hook
  8751. (save-excursion
  8752. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8753. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8754. "Block turning an entry into a TODO, using the hierarchy.
  8755. This checks whether the current task should be blocked from state
  8756. changes. Such blocking occurs when:
  8757. 1. The task has children which are not all in a completed state.
  8758. 2. A task has a parent with the property :ORDERED:, and there
  8759. are siblings prior to the current task with incomplete
  8760. status.
  8761. 3. The parent of the task is blocked because it has siblings that should
  8762. be done first, or is child of a block grandparent TODO entry."
  8763. (catch 'dont-block
  8764. ;; If this is not a todo state change, or if this entry is already DONE,
  8765. ;; do not block
  8766. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8767. (member (plist-get change-plist :from)
  8768. (cons 'done org-done-keywords))
  8769. (member (plist-get change-plist :to)
  8770. (cons 'todo org-not-done-keywords)))
  8771. (throw 'dont-block t))
  8772. ;; If this task has children, and any are undone, it's blocked
  8773. (save-excursion
  8774. (org-back-to-heading t)
  8775. (let ((this-level (funcall outline-level)))
  8776. (outline-next-heading)
  8777. (let ((child-level (funcall outline-level)))
  8778. (while (and (not (eobp))
  8779. (> child-level this-level))
  8780. ;; this todo has children, check whether they are all
  8781. ;; completed
  8782. (if (and (not (org-entry-is-done-p))
  8783. (org-entry-is-todo-p))
  8784. (throw 'dont-block nil))
  8785. (outline-next-heading)
  8786. (setq child-level (funcall outline-level))))))
  8787. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8788. ;; any previous siblings are undone, it's blocked
  8789. (save-excursion
  8790. (org-back-to-heading t)
  8791. (let* ((pos (point))
  8792. (parent-pos (and (org-up-heading-safe) (point))))
  8793. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8794. (when (and (org-entry-get (point) "ORDERED")
  8795. (forward-line 1)
  8796. (re-search-forward org-not-done-heading-regexp pos t))
  8797. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8798. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8799. (while t
  8800. (goto-char parent-pos)
  8801. (if (not (looking-at org-not-done-heading-regexp))
  8802. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8803. (setq pos (point))
  8804. (setq parent-pos (and (org-up-heading-safe) (point)))
  8805. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8806. (when (and (org-entry-get (point) "ORDERED")
  8807. (forward-line 1)
  8808. (re-search-forward org-not-done-heading-regexp pos t))
  8809. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8810. (defcustom org-track-ordered-property-with-tag nil
  8811. "Should the ORDERED property also be shown as a tag?
  8812. The ORDERED property decides if an entry should require subtasks to be
  8813. completed in sequence. Since a property is not very visible, setting
  8814. this option means that toggling the ORDERED property with the command
  8815. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8816. not relevant for the behavior, but it makes things more visible.
  8817. Note that toggling the tag with tags commands will not change the property
  8818. and therefore not influence behavior!
  8819. This can be t, meaning the tag ORDERED should be used, It can also be a
  8820. string to select a different tag for this task."
  8821. :group 'org-todo
  8822. :type '(choice
  8823. (const :tag "No tracking" nil)
  8824. (const :tag "Track with ORDERED tag" t)
  8825. (string :tag "Use other tag")))
  8826. (defun org-toggle-ordered-property ()
  8827. "Toggle the ORDERED property of the current entry.
  8828. For better visibility, you can track the value of this property with a tag.
  8829. See variable `org-track-ordered-property-with-tag'."
  8830. (interactive)
  8831. (let* ((t1 org-track-ordered-property-with-tag)
  8832. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8833. (save-excursion
  8834. (org-back-to-heading)
  8835. (if (org-entry-get nil "ORDERED")
  8836. (progn
  8837. (org-delete-property "ORDERED")
  8838. (and tag (org-toggle-tag tag 'off))
  8839. (message "Subtasks can be completed in arbitrary order"))
  8840. (org-entry-put nil "ORDERED" "t")
  8841. (and tag (org-toggle-tag tag 'on))
  8842. (message "Subtasks must be completed in sequence")))))
  8843. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8844. (defun org-block-todo-from-checkboxes (change-plist)
  8845. "Block turning an entry into a TODO, using checkboxes.
  8846. This checks whether the current task should be blocked from state
  8847. changes because there are uncheckd boxes in this entry."
  8848. (catch 'dont-block
  8849. ;; If this is not a todo state change, or if this entry is already DONE,
  8850. ;; do not block
  8851. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8852. (member (plist-get change-plist :from)
  8853. (cons 'done org-done-keywords))
  8854. (member (plist-get change-plist :to)
  8855. (cons 'todo org-not-done-keywords)))
  8856. (throw 'dont-block t))
  8857. ;; If this task has checkboxes that are not checked, it's blocked
  8858. (save-excursion
  8859. (org-back-to-heading t)
  8860. (let ((beg (point)) end)
  8861. (outline-next-heading)
  8862. (setq end (point))
  8863. (goto-char beg)
  8864. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8865. end t)
  8866. (progn
  8867. (if (boundp 'org-blocked-by-checkboxes)
  8868. (setq org-blocked-by-checkboxes t))
  8869. (throw 'dont-block nil)))))
  8870. t)) ; do not block
  8871. (defun org-update-statistics-cookies (all)
  8872. "Update the statistics cookie, either from TODO or from checkboxes.
  8873. This should be called with the cursor in a line with a statistics cookie."
  8874. (interactive "P")
  8875. (if all
  8876. (progn
  8877. (org-update-checkbox-count 'all)
  8878. (org-map-entries 'org-update-parent-todo-statistics))
  8879. (if (not (org-on-heading-p))
  8880. (org-update-checkbox-count)
  8881. (let ((pos (move-marker (make-marker) (point)))
  8882. end l1 l2)
  8883. (ignore-errors (org-back-to-heading t))
  8884. (if (not (org-on-heading-p))
  8885. (org-update-checkbox-count)
  8886. (setq l1 (org-outline-level))
  8887. (setq end (save-excursion
  8888. (outline-next-heading)
  8889. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  8890. (point)))
  8891. (if (and (save-excursion (re-search-forward
  8892. "^[ \t]*[-+*] \\[[- X]\\]" end t))
  8893. (not (save-excursion (re-search-forward
  8894. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  8895. (org-update-checkbox-count)
  8896. (if (and l2 (> l2 l1))
  8897. (progn
  8898. (goto-char end)
  8899. (org-update-parent-todo-statistics))
  8900. (error "No data for statistics cookie"))))
  8901. (goto-char pos)
  8902. (move-marker pos nil)))))
  8903. (defvar org-entry-property-inherited-from) ;; defined below
  8904. (defun org-update-parent-todo-statistics ()
  8905. "Update any statistics cookie in the parent of the current headline.
  8906. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  8907. the entire subtree and this will travel up the hierarchy and update
  8908. statistics everywhere."
  8909. (interactive)
  8910. (let* ((lim 0) prop
  8911. (recursive (or (not org-hierarchical-todo-statistics)
  8912. (string-match
  8913. "\\<recursive\\>"
  8914. (or (setq prop (org-entry-get
  8915. nil "COOKIE_DATA" 'inherit)) ""))))
  8916. (lim (or (and prop (marker-position
  8917. org-entry-property-inherited-from))
  8918. lim))
  8919. (first t)
  8920. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8921. level ltoggle l1 new ndel
  8922. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8923. (catch 'exit
  8924. (save-excursion
  8925. (beginning-of-line 1)
  8926. (if (org-at-heading-p)
  8927. (setq ltoggle (funcall outline-level))
  8928. (error "This should not happen"))
  8929. (while (and (setq level (org-up-heading-safe))
  8930. (or recursive first)
  8931. (>= (point) lim))
  8932. (setq first nil)
  8933. (unless (and level
  8934. (not (string-match
  8935. "\\<checkbox\\>"
  8936. (downcase
  8937. (or (org-entry-get
  8938. nil "COOKIE_DATA")
  8939. "")))))
  8940. (throw 'exit nil))
  8941. (while (re-search-forward box-re (point-at-eol) t)
  8942. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8943. (setq is-percent (match-end 2))
  8944. (save-match-data
  8945. (unless (outline-next-heading) (throw 'exit nil))
  8946. (while (and (looking-at org-complex-heading-regexp)
  8947. (> (setq l1 (length (match-string 1))) level))
  8948. (setq kwd (and (or recursive (= l1 ltoggle))
  8949. (match-string 2)))
  8950. (if (or (eq org-provide-todo-statistics 'all-headlines)
  8951. (and (listp org-provide-todo-statistics)
  8952. (or (member kwd org-provide-todo-statistics)
  8953. (member kwd org-done-keywords))))
  8954. (setq cnt-all (1+ cnt-all))
  8955. (if (eq org-provide-todo-statistics t)
  8956. (and kwd (setq cnt-all (1+ cnt-all)))))
  8957. (and (member kwd org-done-keywords)
  8958. (setq cnt-done (1+ cnt-done)))
  8959. (outline-next-heading)))
  8960. (setq new
  8961. (if is-percent
  8962. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8963. (format "[%d/%d]" cnt-done cnt-all))
  8964. ndel (- (match-end 0) (match-beginning 0)))
  8965. (goto-char (match-beginning 0))
  8966. (insert new)
  8967. (delete-region (point) (+ (point) ndel))))
  8968. (when cookie-present
  8969. (run-hook-with-args 'org-after-todo-statistics-hook
  8970. cnt-done (- cnt-all cnt-done)))))
  8971. (run-hooks 'org-todo-statistics-hook)))
  8972. (defvar org-after-todo-statistics-hook nil
  8973. "Hook that is called after a TODO statistics cookie has been updated.
  8974. Each function is called with two arguments: the number of not-done entries
  8975. and the number of done entries.
  8976. For example, the following function, when added to this hook, will switch
  8977. an entry to DONE when all children are done, and back to TODO when new
  8978. entries are set to a TODO status. Note that this hook is only called
  8979. when there is a statistics cookie in the headline!
  8980. (defun org-summary-todo (n-done n-not-done)
  8981. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8982. (let (org-log-done org-log-states) ; turn off logging
  8983. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8984. ")
  8985. (defvar org-todo-statistics-hook nil
  8986. "Hook that is run whenever Org thinks TODO statistics should be updated.
  8987. This hook runs even if there is no statisics cookie present, in which case
  8988. `org-after-todo-statistics-hook' would not run.")
  8989. (defun org-todo-trigger-tag-changes (state)
  8990. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8991. (let ((l org-todo-state-tags-triggers)
  8992. changes)
  8993. (when (or (not state) (equal state ""))
  8994. (setq changes (append changes (cdr (assoc "" l)))))
  8995. (when (and (stringp state) (> (length state) 0))
  8996. (setq changes (append changes (cdr (assoc state l)))))
  8997. (when (member state org-not-done-keywords)
  8998. (setq changes (append changes (cdr (assoc 'todo l)))))
  8999. (when (member state org-done-keywords)
  9000. (setq changes (append changes (cdr (assoc 'done l)))))
  9001. (dolist (c changes)
  9002. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  9003. (defun org-local-logging (value)
  9004. "Get logging settings from a property VALUE."
  9005. (let* (words w a)
  9006. ;; directly set the variables, they are already local.
  9007. (setq org-log-done nil
  9008. org-log-repeat nil
  9009. org-todo-log-states nil)
  9010. (setq words (org-split-string value))
  9011. (while (setq w (pop words))
  9012. (cond
  9013. ((setq a (assoc w org-startup-options))
  9014. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  9015. (set (nth 1 a) (nth 2 a))))
  9016. ((setq a (org-extract-log-state-settings w))
  9017. (and (member (car a) org-todo-keywords-1)
  9018. (push a org-todo-log-states)))))))
  9019. (defun org-get-todo-sequence-head (kwd)
  9020. "Return the head of the TODO sequence to which KWD belongs.
  9021. If KWD is not set, check if there is a text property remembering the
  9022. right sequence."
  9023. (let (p)
  9024. (cond
  9025. ((not kwd)
  9026. (or (get-text-property (point-at-bol) 'org-todo-head)
  9027. (progn
  9028. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  9029. nil (point-at-eol)))
  9030. (get-text-property p 'org-todo-head))))
  9031. ((not (member kwd org-todo-keywords-1))
  9032. (car org-todo-keywords-1))
  9033. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  9034. (defun org-fast-todo-selection ()
  9035. "Fast TODO keyword selection with single keys.
  9036. Returns the new TODO keyword, or nil if no state change should occur."
  9037. (let* ((fulltable org-todo-key-alist)
  9038. (done-keywords org-done-keywords) ;; needed for the faces.
  9039. (maxlen (apply 'max (mapcar
  9040. (lambda (x)
  9041. (if (stringp (car x)) (string-width (car x)) 0))
  9042. fulltable)))
  9043. (expert nil)
  9044. (fwidth (+ maxlen 3 1 3))
  9045. (ncol (/ (- (window-width) 4) fwidth))
  9046. tg cnt e c tbl
  9047. groups ingroup)
  9048. (save-excursion
  9049. (save-window-excursion
  9050. (if expert
  9051. (set-buffer (get-buffer-create " *Org todo*"))
  9052. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  9053. (erase-buffer)
  9054. (org-set-local 'org-done-keywords done-keywords)
  9055. (setq tbl fulltable cnt 0)
  9056. (while (setq e (pop tbl))
  9057. (cond
  9058. ((equal e '(:startgroup))
  9059. (push '() groups) (setq ingroup t)
  9060. (when (not (= cnt 0))
  9061. (setq cnt 0)
  9062. (insert "\n"))
  9063. (insert "{ "))
  9064. ((equal e '(:endgroup))
  9065. (setq ingroup nil cnt 0)
  9066. (insert "}\n"))
  9067. ((equal e '(:newline))
  9068. (when (not (= cnt 0))
  9069. (setq cnt 0)
  9070. (insert "\n")
  9071. (setq e (car tbl))
  9072. (while (equal (car tbl) '(:newline))
  9073. (insert "\n")
  9074. (setq tbl (cdr tbl)))))
  9075. (t
  9076. (setq tg (car e) c (cdr e))
  9077. (if ingroup (push tg (car groups)))
  9078. (setq tg (org-add-props tg nil 'face
  9079. (org-get-todo-face tg)))
  9080. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9081. (insert "[" c "] " tg (make-string
  9082. (- fwidth 4 (length tg)) ?\ ))
  9083. (when (= (setq cnt (1+ cnt)) ncol)
  9084. (insert "\n")
  9085. (if ingroup (insert " "))
  9086. (setq cnt 0)))))
  9087. (insert "\n")
  9088. (goto-char (point-min))
  9089. (if (not expert) (org-fit-window-to-buffer))
  9090. (message "[a-z..]:Set [SPC]:clear")
  9091. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9092. (cond
  9093. ((or (= c ?\C-g)
  9094. (and (= c ?q) (not (rassoc c fulltable))))
  9095. (setq quit-flag t))
  9096. ((= c ?\ ) nil)
  9097. ((setq e (rassoc c fulltable) tg (car e))
  9098. tg)
  9099. (t (setq quit-flag t)))))))
  9100. (defun org-entry-is-todo-p ()
  9101. (member (org-get-todo-state) org-not-done-keywords))
  9102. (defun org-entry-is-done-p ()
  9103. (member (org-get-todo-state) org-done-keywords))
  9104. (defun org-get-todo-state ()
  9105. (save-excursion
  9106. (org-back-to-heading t)
  9107. (and (looking-at org-todo-line-regexp)
  9108. (match-end 2)
  9109. (match-string 2))))
  9110. (defun org-at-date-range-p (&optional inactive-ok)
  9111. "Is the cursor inside a date range?"
  9112. (interactive)
  9113. (save-excursion
  9114. (catch 'exit
  9115. (let ((pos (point)))
  9116. (skip-chars-backward "^[<\r\n")
  9117. (skip-chars-backward "<[")
  9118. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9119. (>= (match-end 0) pos)
  9120. (throw 'exit t))
  9121. (skip-chars-backward "^<[\r\n")
  9122. (skip-chars-backward "<[")
  9123. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9124. (>= (match-end 0) pos)
  9125. (throw 'exit t)))
  9126. nil)))
  9127. (defun org-get-repeat (&optional tagline)
  9128. "Check if there is a deadline/schedule with repeater in this entry."
  9129. (save-match-data
  9130. (save-excursion
  9131. (org-back-to-heading t)
  9132. (and (re-search-forward (if tagline
  9133. (concat tagline "\\s-*" org-repeat-re)
  9134. org-repeat-re)
  9135. (org-entry-end-position) t)
  9136. (match-string-no-properties 1)))))
  9137. (defvar org-last-changed-timestamp)
  9138. (defvar org-last-inserted-timestamp)
  9139. (defvar org-log-post-message)
  9140. (defvar org-log-note-purpose)
  9141. (defvar org-log-note-how)
  9142. (defvar org-log-note-extra)
  9143. (defun org-auto-repeat-maybe (done-word)
  9144. "Check if the current headline contains a repeated deadline/schedule.
  9145. If yes, set TODO state back to what it was and change the base date
  9146. of repeating deadline/scheduled time stamps to new date.
  9147. This function is run automatically after each state change to a DONE state."
  9148. ;; last-state is dynamically scoped into this function
  9149. (let* ((repeat (org-get-repeat))
  9150. (aa (assoc last-state org-todo-kwd-alist))
  9151. (interpret (nth 1 aa))
  9152. (head (nth 2 aa))
  9153. (whata '(("d" . day) ("m" . month) ("y" . year)))
  9154. (msg "Entry repeats: ")
  9155. (org-log-done nil)
  9156. (org-todo-log-states nil)
  9157. (nshiftmax 10) (nshift 0)
  9158. re type n what ts time)
  9159. (when repeat
  9160. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  9161. (org-todo (if (eq interpret 'type) last-state head))
  9162. (org-entry-put nil "LAST_REPEAT" (format-time-string
  9163. (org-time-stamp-format t t)))
  9164. (when org-log-repeat
  9165. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9166. (memq 'org-add-log-note post-command-hook))
  9167. ;; OK, we are already setup for some record
  9168. (if (eq org-log-repeat 'note)
  9169. ;; make sure we take a note, not only a time stamp
  9170. (setq org-log-note-how 'note))
  9171. ;; Set up for taking a record
  9172. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  9173. last-state
  9174. 'findpos org-log-repeat)))
  9175. (org-back-to-heading t)
  9176. (org-add-planning-info nil nil 'closed)
  9177. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  9178. org-deadline-time-regexp "\\)\\|\\("
  9179. org-ts-regexp "\\)"))
  9180. (while (re-search-forward
  9181. re (save-excursion (outline-next-heading) (point)) t)
  9182. (setq type (if (match-end 1) org-scheduled-string
  9183. (if (match-end 3) org-deadline-string "Plain:"))
  9184. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9185. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9186. (setq n (string-to-number (match-string 2 ts))
  9187. what (match-string 3 ts))
  9188. (if (equal what "w") (setq n (* n 7) what "d"))
  9189. ;; Preparation, see if we need to modify the start date for the change
  9190. (when (match-end 1)
  9191. (setq time (save-match-data (org-time-string-to-time ts)))
  9192. (cond
  9193. ((equal (match-string 1 ts) ".")
  9194. ;; Shift starting date to today
  9195. (org-timestamp-change
  9196. (- (time-to-days (current-time)) (time-to-days time))
  9197. 'day))
  9198. ((equal (match-string 1 ts) "+")
  9199. (while (or (= nshift 0)
  9200. (<= (time-to-days time) (time-to-days (current-time))))
  9201. (when (= (incf nshift) nshiftmax)
  9202. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9203. (error "Abort")))
  9204. (org-timestamp-change n (cdr (assoc what whata)))
  9205. (org-at-timestamp-p t)
  9206. (setq ts (match-string 1))
  9207. (setq time (save-match-data (org-time-string-to-time ts))))
  9208. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9209. ;; rematch, so that we have everything in place for the real shift
  9210. (org-at-timestamp-p t)
  9211. (setq ts (match-string 1))
  9212. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9213. (org-timestamp-change n (cdr (assoc what whata)))
  9214. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9215. (setq org-log-post-message msg)
  9216. (message "%s" msg))))
  9217. (defun org-show-todo-tree (arg)
  9218. "Make a compact tree which shows all headlines marked with TODO.
  9219. The tree will show the lines where the regexp matches, and all higher
  9220. headlines above the match.
  9221. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9222. With a numeric prefix N, construct a sparse tree for the Nth element
  9223. of `org-todo-keywords-1'."
  9224. (interactive "P")
  9225. (let ((case-fold-search nil)
  9226. (kwd-re
  9227. (cond ((null arg) org-not-done-regexp)
  9228. ((equal arg '(4))
  9229. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  9230. (mapcar 'list org-todo-keywords-1))))
  9231. (concat "\\("
  9232. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9233. "\\)\\>")))
  9234. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9235. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9236. org-todo-keywords-1)))
  9237. (t (error "Invalid prefix argument: %s" arg)))))
  9238. (message "%d TODO entries found"
  9239. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9240. (defun org-deadline (&optional remove time)
  9241. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9242. With argument REMOVE, remove any deadline from the item.
  9243. When TIME is set, it should be an internal time specification, and the
  9244. scheduling will use the corresponding date."
  9245. (interactive "P")
  9246. (if remove
  9247. (progn
  9248. (org-remove-timestamp-with-keyword org-deadline-string)
  9249. (message "Item no longer has a deadline."))
  9250. (if (org-get-repeat)
  9251. (error "Cannot change deadline on task with repeater, please do that by hand")
  9252. (org-add-planning-info 'deadline time 'closed)
  9253. (message "Deadline on %s" org-last-inserted-timestamp))))
  9254. (defun org-schedule (&optional remove time)
  9255. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9256. With argument REMOVE, remove any scheduling date from the item.
  9257. When TIME is set, it should be an internal time specification, and the
  9258. scheduling will use the corresponding date."
  9259. (interactive "P")
  9260. (if remove
  9261. (progn
  9262. (org-remove-timestamp-with-keyword org-scheduled-string)
  9263. (message "Item is no longer scheduled."))
  9264. (if (org-get-repeat)
  9265. (error "Cannot reschedule task with repeater, please do that by hand")
  9266. (org-add-planning-info 'scheduled time 'closed)
  9267. (message "Scheduled to %s" org-last-inserted-timestamp))))
  9268. (defun org-get-scheduled-time (pom &optional inherit)
  9269. "Get the scheduled time as a time tuple, of a format suitable
  9270. for calling org-schedule with, or if there is no scheduling,
  9271. returns nil."
  9272. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  9273. (when time
  9274. (apply 'encode-time (org-parse-time-string time)))))
  9275. (defun org-get-deadline-time (pom &optional inherit)
  9276. "Get the deadine as a time tuple, of a format suitable for
  9277. calling org-deadlin with, or if there is no scheduling, returns
  9278. nil."
  9279. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  9280. (when time
  9281. (apply 'encode-time (org-parse-time-string time)))))
  9282. (defun org-remove-timestamp-with-keyword (keyword)
  9283. "Remove all time stamps with KEYWORD in the current entry."
  9284. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  9285. beg)
  9286. (save-excursion
  9287. (org-back-to-heading t)
  9288. (setq beg (point))
  9289. (outline-next-heading)
  9290. (while (re-search-backward re beg t)
  9291. (replace-match "")
  9292. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  9293. (equal (char-before) ?\ ))
  9294. (backward-delete-char 1)
  9295. (if (string-match "^[ \t]*$" (buffer-substring
  9296. (point-at-bol) (point-at-eol)))
  9297. (delete-region (point-at-bol)
  9298. (min (point-max) (1+ (point-at-eol))))))))))
  9299. (defun org-add-planning-info (what &optional time &rest remove)
  9300. "Insert new timestamp with keyword in the line directly after the headline.
  9301. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  9302. If non is given, the user is prompted for a date.
  9303. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  9304. be removed."
  9305. (interactive)
  9306. (let (org-time-was-given org-end-time-was-given ts
  9307. end default-time default-input)
  9308. (catch 'exit
  9309. (when (and (not time) (memq what '(scheduled deadline)))
  9310. ;; Try to get a default date/time from existing timestamp
  9311. (save-excursion
  9312. (org-back-to-heading t)
  9313. (setq end (save-excursion (outline-next-heading) (point)))
  9314. (when (re-search-forward (if (eq what 'scheduled)
  9315. org-scheduled-time-regexp
  9316. org-deadline-time-regexp)
  9317. end t)
  9318. (setq ts (match-string 1)
  9319. default-time
  9320. (apply 'encode-time (org-parse-time-string ts))
  9321. default-input (and ts (org-get-compact-tod ts))))))
  9322. (when what
  9323. ;; If necessary, get the time from the user
  9324. (setq time (or time (org-read-date nil 'to-time nil nil
  9325. default-time default-input))))
  9326. (when (and org-insert-labeled-timestamps-at-point
  9327. (member what '(scheduled deadline)))
  9328. (insert
  9329. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  9330. (org-insert-time-stamp time org-time-was-given
  9331. nil nil nil (list org-end-time-was-given))
  9332. (setq what nil))
  9333. (save-excursion
  9334. (save-restriction
  9335. (let (col list elt ts buffer-invisibility-spec)
  9336. (org-back-to-heading t)
  9337. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  9338. (goto-char (match-end 1))
  9339. (setq col (current-column))
  9340. (goto-char (match-end 0))
  9341. (if (eobp) (insert "\n") (forward-char 1))
  9342. (when (and (not what)
  9343. (not (looking-at
  9344. (concat "[ \t]*"
  9345. org-keyword-time-not-clock-regexp))))
  9346. ;; Nothing to add, nothing to remove...... :-)
  9347. (throw 'exit nil))
  9348. (if (and (not (looking-at outline-regexp))
  9349. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  9350. "[^\r\n]*"))
  9351. (not (equal (match-string 1) org-clock-string)))
  9352. (narrow-to-region (match-beginning 0) (match-end 0))
  9353. (insert-before-markers "\n")
  9354. (backward-char 1)
  9355. (narrow-to-region (point) (point))
  9356. (and org-adapt-indentation (org-indent-to-column col)))
  9357. ;; Check if we have to remove something.
  9358. (setq list (cons what remove))
  9359. (while list
  9360. (setq elt (pop list))
  9361. (goto-char (point-min))
  9362. (when (or (and (eq elt 'scheduled)
  9363. (re-search-forward org-scheduled-time-regexp nil t))
  9364. (and (eq elt 'deadline)
  9365. (re-search-forward org-deadline-time-regexp nil t))
  9366. (and (eq elt 'closed)
  9367. (re-search-forward org-closed-time-regexp nil t)))
  9368. (replace-match "")
  9369. (if (looking-at "--+<[^>]+>") (replace-match ""))
  9370. (skip-chars-backward " ")
  9371. (if (looking-at " +") (replace-match ""))))
  9372. (goto-char (point-max))
  9373. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  9374. (when what
  9375. (insert
  9376. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  9377. (cond ((eq what 'scheduled) org-scheduled-string)
  9378. ((eq what 'deadline) org-deadline-string)
  9379. ((eq what 'closed) org-closed-string))
  9380. " ")
  9381. (setq ts (org-insert-time-stamp
  9382. time
  9383. (or org-time-was-given
  9384. (and (eq what 'closed) org-log-done-with-time))
  9385. (eq what 'closed)
  9386. nil nil (list org-end-time-was-given)))
  9387. (end-of-line 1))
  9388. (goto-char (point-min))
  9389. (widen)
  9390. (if (and (looking-at "[ \t]+\n")
  9391. (equal (char-before) ?\n))
  9392. (delete-region (1- (point)) (point-at-eol)))
  9393. ts))))))
  9394. (defvar org-log-note-marker (make-marker))
  9395. (defvar org-log-note-purpose nil)
  9396. (defvar org-log-note-state nil)
  9397. (defvar org-log-note-previous-state nil)
  9398. (defvar org-log-note-how nil)
  9399. (defvar org-log-note-extra nil)
  9400. (defvar org-log-note-window-configuration nil)
  9401. (defvar org-log-note-return-to (make-marker))
  9402. (defvar org-log-post-message nil
  9403. "Message to be displayed after a log note has been stored.
  9404. The auto-repeater uses this.")
  9405. (defun org-add-note ()
  9406. "Add a note to the current entry.
  9407. This is done in the same way as adding a state change note."
  9408. (interactive)
  9409. (org-add-log-setup 'note nil nil 'findpos nil))
  9410. (defvar org-property-end-re)
  9411. (defun org-add-log-setup (&optional purpose state prev-state
  9412. findpos how &optional extra)
  9413. "Set up the post command hook to take a note.
  9414. If this is about to TODO state change, the new state is expected in STATE.
  9415. When FINDPOS is non-nil, find the correct position for the note in
  9416. the current entry. If not, assume that it can be inserted at point.
  9417. HOW is an indicator what kind of note should be created.
  9418. EXTRA is additional text that will be inserted into the notes buffer."
  9419. (let* ((org-log-into-drawer (org-log-into-drawer))
  9420. (drawer (cond ((stringp org-log-into-drawer)
  9421. org-log-into-drawer)
  9422. (org-log-into-drawer "LOGBOOK")
  9423. (t nil))))
  9424. (save-restriction
  9425. (save-excursion
  9426. (when findpos
  9427. (org-back-to-heading t)
  9428. (narrow-to-region (point) (save-excursion
  9429. (outline-next-heading) (point)))
  9430. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  9431. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  9432. "[^\r\n]*\\)?"))
  9433. (goto-char (match-end 0))
  9434. (cond
  9435. (drawer
  9436. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  9437. nil t)
  9438. (progn
  9439. (goto-char (match-end 0))
  9440. (or org-log-states-order-reversed
  9441. (and (re-search-forward org-property-end-re nil t)
  9442. (goto-char (1- (match-beginning 0))))))
  9443. (insert "\n:" drawer ":\n:END:")
  9444. (beginning-of-line 0)
  9445. (org-indent-line-function)
  9446. (beginning-of-line 2)
  9447. (org-indent-line-function)
  9448. (end-of-line 0)))
  9449. ((and org-log-state-notes-insert-after-drawers
  9450. (save-excursion
  9451. (forward-line) (looking-at org-drawer-regexp)))
  9452. (forward-line)
  9453. (while (looking-at org-drawer-regexp)
  9454. (goto-char (match-end 0))
  9455. (re-search-forward org-property-end-re (point-max) t)
  9456. (forward-line))
  9457. (forward-line -1)))
  9458. (unless org-log-states-order-reversed
  9459. (and (= (char-after) ?\n) (forward-char 1))
  9460. (org-skip-over-state-notes)
  9461. (skip-chars-backward " \t\n\r")))
  9462. (move-marker org-log-note-marker (point))
  9463. (setq org-log-note-purpose purpose
  9464. org-log-note-state state
  9465. org-log-note-previous-state prev-state
  9466. org-log-note-how how
  9467. org-log-note-extra extra)
  9468. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  9469. (defun org-skip-over-state-notes ()
  9470. "Skip past the list of State notes in an entry."
  9471. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  9472. (while (looking-at "[ \t]*- State")
  9473. (condition-case nil
  9474. (org-next-item)
  9475. (error (org-end-of-item)))))
  9476. (defun org-add-log-note (&optional purpose)
  9477. "Pop up a window for taking a note, and add this note later at point."
  9478. (remove-hook 'post-command-hook 'org-add-log-note)
  9479. (setq org-log-note-window-configuration (current-window-configuration))
  9480. (delete-other-windows)
  9481. (move-marker org-log-note-return-to (point))
  9482. (switch-to-buffer (marker-buffer org-log-note-marker))
  9483. (goto-char org-log-note-marker)
  9484. (org-switch-to-buffer-other-window "*Org Note*")
  9485. (erase-buffer)
  9486. (if (memq org-log-note-how '(time state))
  9487. (let (current-prefix-arg) (org-store-log-note))
  9488. (let ((org-inhibit-startup t)) (org-mode))
  9489. (insert (format "# Insert note for %s.
  9490. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  9491. (cond
  9492. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9493. ((eq org-log-note-purpose 'done) "closed todo item")
  9494. ((eq org-log-note-purpose 'state)
  9495. (format "state change from \"%s\" to \"%s\""
  9496. (or org-log-note-previous-state "")
  9497. (or org-log-note-state "")))
  9498. ((eq org-log-note-purpose 'note)
  9499. "this entry")
  9500. (t (error "This should not happen")))))
  9501. (if org-log-note-extra (insert org-log-note-extra))
  9502. (org-set-local 'org-finish-function 'org-store-log-note)))
  9503. (defvar org-note-abort nil) ; dynamically scoped
  9504. (defun org-store-log-note ()
  9505. "Finish taking a log note, and insert it to where it belongs."
  9506. (let ((txt (buffer-string))
  9507. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9508. lines ind)
  9509. (kill-buffer (current-buffer))
  9510. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9511. (setq txt (replace-match "" t t txt)))
  9512. (if (string-match "\\s-+\\'" txt)
  9513. (setq txt (replace-match "" t t txt)))
  9514. (setq lines (org-split-string txt "\n"))
  9515. (when (and note (string-match "\\S-" note))
  9516. (setq note
  9517. (org-replace-escapes
  9518. note
  9519. (list (cons "%u" (user-login-name))
  9520. (cons "%U" user-full-name)
  9521. (cons "%t" (format-time-string
  9522. (org-time-stamp-format 'long 'inactive)
  9523. (current-time)))
  9524. (cons "%s" (if org-log-note-state
  9525. (concat "\"" org-log-note-state "\"")
  9526. ""))
  9527. (cons "%S" (if org-log-note-previous-state
  9528. (concat "\"" org-log-note-previous-state "\"")
  9529. "\"\"")))))
  9530. (if lines (setq note (concat note " \\\\")))
  9531. (push note lines))
  9532. (when (or current-prefix-arg org-note-abort)
  9533. (when org-log-into-drawer
  9534. (org-remove-empty-drawer-at
  9535. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9536. org-log-note-marker))
  9537. (setq lines nil))
  9538. (when lines
  9539. (save-excursion
  9540. (set-buffer (marker-buffer org-log-note-marker))
  9541. (save-excursion
  9542. (goto-char org-log-note-marker)
  9543. (move-marker org-log-note-marker nil)
  9544. (end-of-line 1)
  9545. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9546. (insert "- " (pop lines))
  9547. (org-indent-line-function)
  9548. (beginning-of-line 1)
  9549. (looking-at "[ \t]*")
  9550. (setq ind (concat (match-string 0) " "))
  9551. (end-of-line 1)
  9552. (while lines (insert "\n" ind (pop lines)))
  9553. (message "Note stored")
  9554. (org-back-to-heading t)
  9555. (org-cycle-hide-drawers 'children)))))
  9556. (set-window-configuration org-log-note-window-configuration)
  9557. (with-current-buffer (marker-buffer org-log-note-return-to)
  9558. (goto-char org-log-note-return-to))
  9559. (move-marker org-log-note-return-to nil)
  9560. (and org-log-post-message (message "%s" org-log-post-message)))
  9561. (defun org-remove-empty-drawer-at (drawer pos)
  9562. "Remove an emptyr DARWER drawer at position POS.
  9563. POS may also be a marker."
  9564. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9565. (save-excursion
  9566. (save-restriction
  9567. (widen)
  9568. (goto-char pos)
  9569. (if (org-in-regexp
  9570. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9571. (replace-match ""))))))
  9572. (defun org-sparse-tree (&optional arg)
  9573. "Create a sparse tree, prompt for the details.
  9574. This command can create sparse trees. You first need to select the type
  9575. of match used to create the tree:
  9576. t Show entries with a specific TODO keyword.
  9577. m Show entries selected by a tags/property match.
  9578. p Enter a property name and its value (both with completion on existing
  9579. names/values) and show entries with that property.
  9580. r Show entries matching a regular expression.
  9581. d Show deadlines due within `org-deadline-warning-days'.
  9582. b Show deadlines and scheduled items before a date.
  9583. a Show deadlines and scheduled items after a date."
  9584. (interactive "P")
  9585. (let (ans kwd value)
  9586. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9587. (setq ans (read-char-exclusive))
  9588. (cond
  9589. ((equal ans ?d)
  9590. (call-interactively 'org-check-deadlines))
  9591. ((equal ans ?b)
  9592. (call-interactively 'org-check-before-date))
  9593. ((equal ans ?a)
  9594. (call-interactively 'org-check-after-date))
  9595. ((equal ans ?t)
  9596. (org-show-todo-tree '(4)))
  9597. ((member ans '(?T ?m))
  9598. (call-interactively 'org-match-sparse-tree))
  9599. ((member ans '(?p ?P))
  9600. (setq kwd (org-icompleting-read "Property: "
  9601. (mapcar 'list (org-buffer-property-keys))))
  9602. (setq value (org-icompleting-read "Value: "
  9603. (mapcar 'list (org-property-values kwd))))
  9604. (unless (string-match "\\`{.*}\\'" value)
  9605. (setq value (concat "\"" value "\"")))
  9606. (org-match-sparse-tree arg (concat kwd "=" value)))
  9607. ((member ans '(?r ?R ?/))
  9608. (call-interactively 'org-occur))
  9609. (t (error "No such sparse tree command \"%c\"" ans)))))
  9610. (defvar org-occur-highlights nil
  9611. "List of overlays used for occur matches.")
  9612. (make-variable-buffer-local 'org-occur-highlights)
  9613. (defvar org-occur-parameters nil
  9614. "Parameters of the active org-occur calls.
  9615. This is a list, each call to org-occur pushes as cons cell,
  9616. containing the regular expression and the callback, onto the list.
  9617. The list can contain several entries if `org-occur' has been called
  9618. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9619. will only contain one set of parameters. When the highlights are
  9620. removed (for example with `C-c C-c', or with the next edit (depending
  9621. on `org-remove-highlights-with-change'), this variable is emptied
  9622. as well.")
  9623. (make-variable-buffer-local 'org-occur-parameters)
  9624. (defun org-occur (regexp &optional keep-previous callback)
  9625. "Make a compact tree which shows all matches of REGEXP.
  9626. The tree will show the lines where the regexp matches, and all higher
  9627. headlines above the match. It will also show the heading after the match,
  9628. to make sure editing the matching entry is easy.
  9629. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9630. call to `org-occur' will be kept, to allow stacking of calls to this
  9631. command.
  9632. If CALLBACK is non-nil, it is a function which is called to confirm
  9633. that the match should indeed be shown."
  9634. (interactive "sRegexp: \nP")
  9635. (when (equal regexp "")
  9636. (error "Regexp cannot be empty"))
  9637. (unless keep-previous
  9638. (org-remove-occur-highlights nil nil t))
  9639. (push (cons regexp callback) org-occur-parameters)
  9640. (let ((cnt 0))
  9641. (save-excursion
  9642. (goto-char (point-min))
  9643. (if (or (not keep-previous) ; do not want to keep
  9644. (not org-occur-highlights)) ; no previous matches
  9645. ;; hide everything
  9646. (org-overview))
  9647. (while (re-search-forward regexp nil t)
  9648. (when (or (not callback)
  9649. (save-match-data (funcall callback)))
  9650. (setq cnt (1+ cnt))
  9651. (when org-highlight-sparse-tree-matches
  9652. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9653. (org-show-context 'occur-tree))))
  9654. (when org-remove-highlights-with-change
  9655. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9656. nil 'local))
  9657. (unless org-sparse-tree-open-archived-trees
  9658. (org-hide-archived-subtrees (point-min) (point-max)))
  9659. (run-hooks 'org-occur-hook)
  9660. (if (interactive-p)
  9661. (message "%d match(es) for regexp %s" cnt regexp))
  9662. cnt))
  9663. (defun org-show-context (&optional key)
  9664. "Make sure point and context and visible.
  9665. How much context is shown depends upon the variables
  9666. `org-show-hierarchy-above', `org-show-following-heading'. and
  9667. `org-show-siblings'."
  9668. (let ((heading-p (org-on-heading-p t))
  9669. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9670. (following-p (org-get-alist-option org-show-following-heading key))
  9671. (entry-p (org-get-alist-option org-show-entry-below key))
  9672. (siblings-p (org-get-alist-option org-show-siblings key)))
  9673. (catch 'exit
  9674. ;; Show heading or entry text
  9675. (if (and heading-p (not entry-p))
  9676. (org-flag-heading nil) ; only show the heading
  9677. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9678. (org-show-hidden-entry))) ; show entire entry
  9679. (when following-p
  9680. ;; Show next sibling, or heading below text
  9681. (save-excursion
  9682. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9683. (org-flag-heading nil))))
  9684. (when siblings-p (org-show-siblings))
  9685. (when hierarchy-p
  9686. ;; show all higher headings, possibly with siblings
  9687. (save-excursion
  9688. (while (and (condition-case nil
  9689. (progn (org-up-heading-all 1) t)
  9690. (error nil))
  9691. (not (bobp)))
  9692. (org-flag-heading nil)
  9693. (when siblings-p (org-show-siblings))))))))
  9694. (defun org-reveal (&optional siblings)
  9695. "Show current entry, hierarchy above it, and the following headline.
  9696. This can be used to show a consistent set of context around locations
  9697. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9698. not t for the search context.
  9699. With optional argument SIBLINGS, on each level of the hierarchy all
  9700. siblings are shown. This repairs the tree structure to what it would
  9701. look like when opened with hierarchical calls to `org-cycle'."
  9702. (interactive "P")
  9703. (let ((org-show-hierarchy-above t)
  9704. (org-show-following-heading t)
  9705. (org-show-siblings (if siblings t org-show-siblings)))
  9706. (org-show-context nil)))
  9707. (defun org-highlight-new-match (beg end)
  9708. "Highlight from BEG to END and mark the highlight is an occur headline."
  9709. (let ((ov (org-make-overlay beg end)))
  9710. (org-overlay-put ov 'face 'secondary-selection)
  9711. (push ov org-occur-highlights)))
  9712. (defun org-remove-occur-highlights (&optional beg end noremove)
  9713. "Remove the occur highlights from the buffer.
  9714. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9715. from the `before-change-functions' in the current buffer."
  9716. (interactive)
  9717. (unless org-inhibit-highlight-removal
  9718. (mapc 'org-delete-overlay org-occur-highlights)
  9719. (setq org-occur-highlights nil)
  9720. (setq org-occur-parameters nil)
  9721. (unless noremove
  9722. (remove-hook 'before-change-functions
  9723. 'org-remove-occur-highlights 'local))))
  9724. ;;;; Priorities
  9725. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9726. "Regular expression matching the priority indicator.")
  9727. (defvar org-remove-priority-next-time nil)
  9728. (defun org-priority-up ()
  9729. "Increase the priority of the current item."
  9730. (interactive)
  9731. (org-priority 'up))
  9732. (defun org-priority-down ()
  9733. "Decrease the priority of the current item."
  9734. (interactive)
  9735. (org-priority 'down))
  9736. (defun org-priority (&optional action)
  9737. "Change the priority of an item by ARG.
  9738. ACTION can be `set', `up', `down', or a character."
  9739. (interactive)
  9740. (unless org-enable-priority-commands
  9741. (error "Priority commands are disabled"))
  9742. (setq action (or action 'set))
  9743. (let (current new news have remove)
  9744. (save-excursion
  9745. (org-back-to-heading t)
  9746. (if (looking-at org-priority-regexp)
  9747. (setq current (string-to-char (match-string 2))
  9748. have t)
  9749. (setq current org-default-priority))
  9750. (cond
  9751. ((or (eq action 'set)
  9752. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9753. (if (not (eq action 'set))
  9754. (setq new action)
  9755. (message "Priority %c-%c, SPC to remove: "
  9756. org-highest-priority org-lowest-priority)
  9757. (setq new (read-char-exclusive)))
  9758. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9759. (= (upcase org-lowest-priority) org-lowest-priority))
  9760. (setq new (upcase new)))
  9761. (cond ((equal new ?\ ) (setq remove t))
  9762. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9763. (error "Priority must be between `%c' and `%c'"
  9764. org-highest-priority org-lowest-priority))))
  9765. ((eq action 'up)
  9766. (if (and (not have) (eq last-command this-command))
  9767. (setq new org-lowest-priority)
  9768. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9769. org-default-priority (1- current)))))
  9770. ((eq action 'down)
  9771. (if (and (not have) (eq last-command this-command))
  9772. (setq new org-highest-priority)
  9773. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9774. org-default-priority (1+ current)))))
  9775. (t (error "Invalid action")))
  9776. (if (or (< (upcase new) org-highest-priority)
  9777. (> (upcase new) org-lowest-priority))
  9778. (setq remove t))
  9779. (setq news (format "%c" new))
  9780. (if have
  9781. (if remove
  9782. (replace-match "" t t nil 1)
  9783. (replace-match news t t nil 2))
  9784. (if remove
  9785. (error "No priority cookie found in line")
  9786. (let ((case-fold-search nil))
  9787. (looking-at org-todo-line-regexp))
  9788. (if (match-end 2)
  9789. (progn
  9790. (goto-char (match-end 2))
  9791. (insert " [#" news "]"))
  9792. (goto-char (match-beginning 3))
  9793. (insert "[#" news "] "))))
  9794. (org-preserve-lc (org-set-tags nil 'align)))
  9795. (if remove
  9796. (message "Priority removed")
  9797. (message "Priority of current item set to %s" news))))
  9798. (defun org-get-priority (s)
  9799. "Find priority cookie and return priority."
  9800. (save-match-data
  9801. (if (not (string-match org-priority-regexp s))
  9802. (* 1000 (- org-lowest-priority org-default-priority))
  9803. (* 1000 (- org-lowest-priority
  9804. (string-to-char (match-string 2 s)))))))
  9805. ;;;; Tags
  9806. (defvar org-agenda-archives-mode)
  9807. (defvar org-map-continue-from nil
  9808. "Position from where mapping should continue.
  9809. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9810. (defvar org-scanner-tags nil
  9811. "The current tag list while the tags scanner is running.")
  9812. (defvar org-trust-scanner-tags nil
  9813. "Should `org-get-tags-at' use the tags fro the scanner.
  9814. This is for internal dynamical scoping only.
  9815. When this is non-nil, the function `org-get-tags-at' will return the value
  9816. of `org-scanner-tags' instead of building the list by itself. This
  9817. can lead to large speed-ups when the tags scanner is used in a file with
  9818. many entries, and when the list of tags is retrieved, for example to
  9819. obtain a list of properties. Building the tags list for each entry in such
  9820. a file becomes an N^2 operation - but with this variable set, it scales
  9821. as N.")
  9822. (defun org-scan-tags (action matcher &optional todo-only)
  9823. "Scan headline tags with inheritance and produce output ACTION.
  9824. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9825. or `agenda' to produce an entry list for an agenda view. It can also be
  9826. a Lisp form or a function that should be called at each matched headline, in
  9827. this case the return value is a list of all return values from these calls.
  9828. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9829. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9830. only lines with a TODO keyword are included in the output."
  9831. (require 'org-agenda)
  9832. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9833. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9834. (org-re
  9835. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9836. (props (list 'face 'default
  9837. 'done-face 'org-agenda-done
  9838. 'undone-face 'default
  9839. 'mouse-face 'highlight
  9840. 'org-not-done-regexp org-not-done-regexp
  9841. 'org-todo-regexp org-todo-regexp
  9842. 'keymap org-agenda-keymap
  9843. 'help-echo
  9844. (format "mouse-2 or RET jump to org file %s"
  9845. (abbreviate-file-name
  9846. (or (buffer-file-name (buffer-base-buffer))
  9847. (buffer-name (buffer-base-buffer)))))))
  9848. (case-fold-search nil)
  9849. (org-map-continue-from nil)
  9850. lspos tags tags-list
  9851. (tags-alist (list (cons 0 org-file-tags)))
  9852. (llast 0) rtn rtn1 level category i txt
  9853. todo marker entry priority)
  9854. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9855. (setq action (list 'lambda nil action)))
  9856. (save-excursion
  9857. (goto-char (point-min))
  9858. (when (eq action 'sparse-tree)
  9859. (org-overview)
  9860. (org-remove-occur-highlights))
  9861. (while (re-search-forward re nil t)
  9862. (catch :skip
  9863. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9864. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9865. (goto-char (setq lspos (match-beginning 0)))
  9866. (setq level (org-reduced-level (funcall outline-level))
  9867. category (org-get-category))
  9868. (setq i llast llast level)
  9869. ;; remove tag lists from same and sublevels
  9870. (while (>= i level)
  9871. (when (setq entry (assoc i tags-alist))
  9872. (setq tags-alist (delete entry tags-alist)))
  9873. (setq i (1- i)))
  9874. ;; add the next tags
  9875. (when tags
  9876. (setq tags (org-split-string tags ":")
  9877. tags-alist
  9878. (cons (cons level tags) tags-alist)))
  9879. ;; compile tags for current headline
  9880. (setq tags-list
  9881. (if org-use-tag-inheritance
  9882. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9883. tags)
  9884. org-scanner-tags tags-list)
  9885. (when org-use-tag-inheritance
  9886. (setcdr (car tags-alist)
  9887. (mapcar (lambda (x)
  9888. (setq x (copy-sequence x))
  9889. (org-add-prop-inherited x))
  9890. (cdar tags-alist))))
  9891. (when (and tags org-use-tag-inheritance
  9892. (or (not (eq t org-use-tag-inheritance))
  9893. org-tags-exclude-from-inheritance))
  9894. ;; selective inheritance, remove uninherited ones
  9895. (setcdr (car tags-alist)
  9896. (org-remove-uniherited-tags (cdar tags-alist))))
  9897. (when (and (or (not todo-only)
  9898. (and (member todo org-not-done-keywords)
  9899. (or (not org-agenda-tags-todo-honor-ignore-options)
  9900. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9901. (let ((case-fold-search t)) (eval matcher))
  9902. (or
  9903. (not (member org-archive-tag tags-list))
  9904. ;; we have an archive tag, should we use this anyway?
  9905. (or (not org-agenda-skip-archived-trees)
  9906. (and (eq action 'agenda) org-agenda-archives-mode))))
  9907. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9908. ;; select this headline
  9909. (cond
  9910. ((eq action 'sparse-tree)
  9911. (and org-highlight-sparse-tree-matches
  9912. (org-get-heading) (match-end 0)
  9913. (org-highlight-new-match
  9914. (match-beginning 0) (match-beginning 1)))
  9915. (org-show-context 'tags-tree))
  9916. ((eq action 'agenda)
  9917. (setq txt (org-format-agenda-item
  9918. ""
  9919. (concat
  9920. (if (eq org-tags-match-list-sublevels 'indented)
  9921. (make-string (1- level) ?.) "")
  9922. (org-get-heading))
  9923. category
  9924. tags-list
  9925. )
  9926. priority (org-get-priority txt))
  9927. (goto-char lspos)
  9928. (setq marker (org-agenda-new-marker))
  9929. (org-add-props txt props
  9930. 'org-marker marker 'org-hd-marker marker 'org-category category
  9931. 'todo-state todo
  9932. 'priority priority 'type "tagsmatch")
  9933. (push txt rtn))
  9934. ((functionp action)
  9935. (setq org-map-continue-from nil)
  9936. (save-excursion
  9937. (setq rtn1 (funcall action))
  9938. (push rtn1 rtn)))
  9939. (t (error "Invalid action")))
  9940. ;; if we are to skip sublevels, jump to end of subtree
  9941. (unless org-tags-match-list-sublevels
  9942. (org-end-of-subtree t)
  9943. (backward-char 1))))
  9944. ;; Get the correct position from where to continue
  9945. (if org-map-continue-from
  9946. (goto-char org-map-continue-from)
  9947. (and (= (point) lspos) (end-of-line 1)))))
  9948. (when (and (eq action 'sparse-tree)
  9949. (not org-sparse-tree-open-archived-trees))
  9950. (org-hide-archived-subtrees (point-min) (point-max)))
  9951. (nreverse rtn)))
  9952. (defun org-remove-uniherited-tags (tags)
  9953. "Remove all tags that are not inherited from the list TAGS."
  9954. (cond
  9955. ((eq org-use-tag-inheritance t)
  9956. (if org-tags-exclude-from-inheritance
  9957. (org-delete-all org-tags-exclude-from-inheritance tags)
  9958. tags))
  9959. ((not org-use-tag-inheritance) nil)
  9960. ((stringp org-use-tag-inheritance)
  9961. (delq nil (mapcar
  9962. (lambda (x)
  9963. (if (and (string-match org-use-tag-inheritance x)
  9964. (not (member x org-tags-exclude-from-inheritance)))
  9965. x nil))
  9966. tags)))
  9967. ((listp org-use-tag-inheritance)
  9968. (delq nil (mapcar
  9969. (lambda (x)
  9970. (if (member x org-use-tag-inheritance) x nil))
  9971. tags)))))
  9972. (defvar todo-only) ;; dynamically scoped
  9973. (defun org-match-sparse-tree (&optional todo-only match)
  9974. "Create a sparse tree according to tags string MATCH.
  9975. MATCH can contain positive and negative selection of tags, like
  9976. \"+WORK+URGENT-WITHBOSS\".
  9977. If optional argument TODO-ONLY is non-nil, only select lines that are
  9978. also TODO lines."
  9979. (interactive "P")
  9980. (org-prepare-agenda-buffers (list (current-buffer)))
  9981. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9982. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9983. (defvar org-cached-props nil)
  9984. (defun org-cached-entry-get (pom property)
  9985. (if (or (eq t org-use-property-inheritance)
  9986. (and (stringp org-use-property-inheritance)
  9987. (string-match org-use-property-inheritance property))
  9988. (and (listp org-use-property-inheritance)
  9989. (member property org-use-property-inheritance)))
  9990. ;; Caching is not possible, check it directly
  9991. (org-entry-get pom property 'inherit)
  9992. ;; Get all properties, so that we can do complicated checks easily
  9993. (cdr (assoc property (or org-cached-props
  9994. (setq org-cached-props
  9995. (org-entry-properties pom)))))))
  9996. (defun org-global-tags-completion-table (&optional files)
  9997. "Return the list of all tags in all agenda buffer/files."
  9998. (save-excursion
  9999. (org-uniquify
  10000. (delq nil
  10001. (apply 'append
  10002. (mapcar
  10003. (lambda (file)
  10004. (set-buffer (find-file-noselect file))
  10005. (append (org-get-buffer-tags)
  10006. (mapcar (lambda (x) (if (stringp (car-safe x))
  10007. (list (car-safe x)) nil))
  10008. org-tag-alist)))
  10009. (if (and files (car files))
  10010. files
  10011. (org-agenda-files))))))))
  10012. (defun org-make-tags-matcher (match)
  10013. "Create the TAGS//TODO matcher form for the selection string MATCH."
  10014. ;; todo-only is scoped dynamically into this function, and the function
  10015. ;; may change it if the matcher asks for it.
  10016. (unless match
  10017. ;; Get a new match request, with completion
  10018. (let ((org-last-tags-completion-table
  10019. (org-global-tags-completion-table)))
  10020. (setq match (org-completing-read-no-i
  10021. "Match: " 'org-tags-completion-function nil nil nil
  10022. 'org-tags-history))))
  10023. ;; Parse the string and create a lisp form
  10024. (let ((match0 match)
  10025. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  10026. minus tag mm
  10027. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  10028. orterms term orlist re-p str-p level-p level-op time-p
  10029. prop-p pn pv po cat-p gv rest)
  10030. (if (string-match "/+" match)
  10031. ;; match contains also a todo-matching request
  10032. (progn
  10033. (setq tagsmatch (substring match 0 (match-beginning 0))
  10034. todomatch (substring match (match-end 0)))
  10035. (if (string-match "^!" todomatch)
  10036. (setq todo-only t todomatch (substring todomatch 1)))
  10037. (if (string-match "^\\s-*$" todomatch)
  10038. (setq todomatch nil)))
  10039. ;; only matching tags
  10040. (setq tagsmatch match todomatch nil))
  10041. ;; Make the tags matcher
  10042. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  10043. (setq tagsmatcher t)
  10044. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  10045. (while (setq term (pop orterms))
  10046. (while (and (equal (substring term -1) "\\") orterms)
  10047. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  10048. (while (string-match re term)
  10049. (setq rest (substring term (match-end 0))
  10050. minus (and (match-end 1)
  10051. (equal (match-string 1 term) "-"))
  10052. tag (match-string 2 term)
  10053. re-p (equal (string-to-char tag) ?{)
  10054. level-p (match-end 4)
  10055. prop-p (match-end 5)
  10056. mm (cond
  10057. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  10058. (level-p
  10059. (setq level-op (org-op-to-function (match-string 3 term)))
  10060. `(,level-op level ,(string-to-number
  10061. (match-string 4 term))))
  10062. (prop-p
  10063. (setq pn (match-string 5 term)
  10064. po (match-string 6 term)
  10065. pv (match-string 7 term)
  10066. cat-p (equal pn "CATEGORY")
  10067. re-p (equal (string-to-char pv) ?{)
  10068. str-p (equal (string-to-char pv) ?\")
  10069. time-p (save-match-data
  10070. (string-match "^\"[[<].*[]>]\"$" pv))
  10071. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  10072. (if time-p (setq pv (org-matcher-time pv)))
  10073. (setq po (org-op-to-function po (if time-p 'time str-p)))
  10074. (cond
  10075. ((equal pn "CATEGORY")
  10076. (setq gv '(get-text-property (point) 'org-category)))
  10077. ((equal pn "TODO")
  10078. (setq gv 'todo))
  10079. (t
  10080. (setq gv `(org-cached-entry-get nil ,pn))))
  10081. (if re-p
  10082. (if (eq po 'org<>)
  10083. `(not (string-match ,pv (or ,gv "")))
  10084. `(string-match ,pv (or ,gv "")))
  10085. (if str-p
  10086. `(,po (or ,gv "") ,pv)
  10087. `(,po (string-to-number (or ,gv ""))
  10088. ,(string-to-number pv) ))))
  10089. (t `(member ,tag tags-list)))
  10090. mm (if minus (list 'not mm) mm)
  10091. term rest)
  10092. (push mm tagsmatcher))
  10093. (push (if (> (length tagsmatcher) 1)
  10094. (cons 'and tagsmatcher)
  10095. (car tagsmatcher))
  10096. orlist)
  10097. (setq tagsmatcher nil))
  10098. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  10099. (setq tagsmatcher
  10100. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  10101. ;; Make the todo matcher
  10102. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  10103. (setq todomatcher t)
  10104. (setq orterms (org-split-string todomatch "|") orlist nil)
  10105. (while (setq term (pop orterms))
  10106. (while (string-match re term)
  10107. (setq minus (and (match-end 1)
  10108. (equal (match-string 1 term) "-"))
  10109. kwd (match-string 2 term)
  10110. re-p (equal (string-to-char kwd) ?{)
  10111. term (substring term (match-end 0))
  10112. mm (if re-p
  10113. `(string-match ,(substring kwd 1 -1) todo)
  10114. (list 'equal 'todo kwd))
  10115. mm (if minus (list 'not mm) mm))
  10116. (push mm todomatcher))
  10117. (push (if (> (length todomatcher) 1)
  10118. (cons 'and todomatcher)
  10119. (car todomatcher))
  10120. orlist)
  10121. (setq todomatcher nil))
  10122. (setq todomatcher (if (> (length orlist) 1)
  10123. (cons 'or orlist) (car orlist))))
  10124. ;; Return the string and lisp forms of the matcher
  10125. (setq matcher (if todomatcher
  10126. (list 'and tagsmatcher todomatcher)
  10127. tagsmatcher))
  10128. (cons match0 matcher)))
  10129. (defun org-op-to-function (op &optional stringp)
  10130. "Turn an operator into the appropriate function."
  10131. (setq op
  10132. (cond
  10133. ((equal op "<" ) '(< string< org-time<))
  10134. ((equal op ">" ) '(> org-string> org-time>))
  10135. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  10136. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  10137. ((member op '("=" "==")) '(= string= org-time=))
  10138. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  10139. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  10140. (defun org<> (a b) (not (= a b)))
  10141. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  10142. (defun org-string>= (a b) (not (string< a b)))
  10143. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  10144. (defun org-string<> (a b) (not (string= a b)))
  10145. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  10146. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  10147. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  10148. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  10149. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  10150. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  10151. (defun org-2ft (s)
  10152. "Convert S to a floating point time.
  10153. If S is already a number, just return it. If it is a string, parse
  10154. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  10155. (cond
  10156. ((numberp s) s)
  10157. ((stringp s)
  10158. (condition-case nil
  10159. (float-time (apply 'encode-time (org-parse-time-string s)))
  10160. (error 0.)))
  10161. (t 0.)))
  10162. (defun org-time-today ()
  10163. "Time in seconds today at 0:00.
  10164. Returns the float number of seconds since the beginning of the
  10165. epoch to the beginning of today (00:00)."
  10166. (float-time (apply 'encode-time
  10167. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  10168. (defun org-matcher-time (s)
  10169. "Interpret a time comparison value."
  10170. (save-match-data
  10171. (cond
  10172. ((string= s "<now>") (float-time))
  10173. ((string= s "<today>") (org-time-today))
  10174. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  10175. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  10176. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  10177. (+ (org-time-today)
  10178. (* (string-to-number (match-string 1 s))
  10179. (cdr (assoc (match-string 2 s)
  10180. '(("d" . 86400.0) ("w" . 604800.0)
  10181. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10182. (t (org-2ft s)))))
  10183. (defun org-match-any-p (re list)
  10184. "Does re match any element of list?"
  10185. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10186. (delq nil list))
  10187. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10188. (defvar org-tags-overlay (org-make-overlay 1 1))
  10189. (org-detach-overlay org-tags-overlay)
  10190. (defun org-get-local-tags-at (&optional pos)
  10191. "Get a list of tags defined in the current headline."
  10192. (org-get-tags-at pos 'local))
  10193. (defun org-get-local-tags ()
  10194. "Get a list of tags defined in the current headline."
  10195. (org-get-tags-at nil 'local))
  10196. (defun org-get-tags-at (&optional pos local)
  10197. "Get a list of all headline tags applicable at POS.
  10198. POS defaults to point. If tags are inherited, the list contains
  10199. the targets in the same sequence as the headlines appear, i.e.
  10200. the tags of the current headline come last.
  10201. When LOCAL is non-nil, only return tags from the current headline,
  10202. ignore inherited ones."
  10203. (interactive)
  10204. (if (and org-trust-scanner-tags
  10205. (or (not pos) (equal pos (point)))
  10206. (not local))
  10207. org-scanner-tags
  10208. (let (tags ltags lastpos parent)
  10209. (save-excursion
  10210. (save-restriction
  10211. (widen)
  10212. (goto-char (or pos (point)))
  10213. (save-match-data
  10214. (catch 'done
  10215. (condition-case nil
  10216. (progn
  10217. (org-back-to-heading t)
  10218. (while (not (equal lastpos (point)))
  10219. (setq lastpos (point))
  10220. (when (looking-at
  10221. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  10222. (setq ltags (org-split-string
  10223. (org-match-string-no-properties 1) ":"))
  10224. (when parent
  10225. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  10226. (setq tags (append
  10227. (if parent
  10228. (org-remove-uniherited-tags ltags)
  10229. ltags)
  10230. tags)))
  10231. (or org-use-tag-inheritance (throw 'done t))
  10232. (if local (throw 'done t))
  10233. (or (org-up-heading-safe) (error nil))
  10234. (setq parent t)))
  10235. (error nil)))))
  10236. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  10237. (defun org-add-prop-inherited (s)
  10238. (add-text-properties 0 (length s) '(inherited t) s)
  10239. s)
  10240. (defun org-toggle-tag (tag &optional onoff)
  10241. "Toggle the tag TAG for the current line.
  10242. If ONOFF is `on' or `off', don't toggle but set to this state."
  10243. (let (res current)
  10244. (save-excursion
  10245. (org-back-to-heading t)
  10246. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  10247. (point-at-eol) t)
  10248. (progn
  10249. (setq current (match-string 1))
  10250. (replace-match ""))
  10251. (setq current ""))
  10252. (setq current (nreverse (org-split-string current ":")))
  10253. (cond
  10254. ((eq onoff 'on)
  10255. (setq res t)
  10256. (or (member tag current) (push tag current)))
  10257. ((eq onoff 'off)
  10258. (or (not (member tag current)) (setq current (delete tag current))))
  10259. (t (if (member tag current)
  10260. (setq current (delete tag current))
  10261. (setq res t)
  10262. (push tag current))))
  10263. (end-of-line 1)
  10264. (if current
  10265. (progn
  10266. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  10267. (org-set-tags nil t))
  10268. (delete-horizontal-space))
  10269. (run-hooks 'org-after-tags-change-hook))
  10270. res))
  10271. (defun org-align-tags-here (to-col)
  10272. ;; Assumes that this is a headline
  10273. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  10274. (beginning-of-line 1)
  10275. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10276. (< pos (match-beginning 2)))
  10277. (progn
  10278. (setq tags-l (- (match-end 2) (match-beginning 2)))
  10279. (goto-char (match-beginning 1))
  10280. (insert " ")
  10281. (delete-region (point) (1+ (match-beginning 2)))
  10282. (setq ncol (max (1+ (current-column))
  10283. (1+ col)
  10284. (if (> to-col 0)
  10285. to-col
  10286. (- (abs to-col) tags-l))))
  10287. (setq p (point))
  10288. (insert (make-string (- ncol (current-column)) ?\ ))
  10289. (setq ncol (current-column))
  10290. (when indent-tabs-mode (tabify p (point-at-eol)))
  10291. (org-move-to-column (min ncol col) t))
  10292. (goto-char pos))))
  10293. (defun org-set-tags-command (&optional arg just-align)
  10294. "Call the set-tags command for the current entry."
  10295. (interactive "P")
  10296. (if (org-on-heading-p)
  10297. (org-set-tags arg just-align)
  10298. (save-excursion
  10299. (org-back-to-heading t)
  10300. (org-set-tags arg just-align))))
  10301. (defun org-set-tags-to (data)
  10302. "Set the tags of the current entry to DATA, replacing the current tags.
  10303. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  10304. If DATA is nil or the empty string, any tags will be removed."
  10305. (interactive "sTags: ")
  10306. (setq data
  10307. (cond
  10308. ((eq data nil) "")
  10309. ((equal data "") "")
  10310. ((stringp data)
  10311. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  10312. ":"))
  10313. ((listp data)
  10314. (concat ":" (mapconcat 'identity data ":") ":"))
  10315. (t nil)))
  10316. (when data
  10317. (save-excursion
  10318. (org-back-to-heading t)
  10319. (when (looking-at org-complex-heading-regexp)
  10320. (if (match-end 5)
  10321. (progn
  10322. (goto-char (match-beginning 5))
  10323. (insert data)
  10324. (delete-region (point) (point-at-eol))
  10325. (org-set-tags nil 'align))
  10326. (goto-char (point-at-eol))
  10327. (insert " " data)
  10328. (org-set-tags nil 'align)))
  10329. (beginning-of-line 1)
  10330. (if (looking-at ".*?\\([ \t]+\\)$")
  10331. (delete-region (match-beginning 1) (match-end 1))))))
  10332. (defun org-set-tags (&optional arg just-align)
  10333. "Set the tags for the current headline.
  10334. With prefix ARG, realign all tags in headings in the current buffer."
  10335. (interactive "P")
  10336. (let* ((re (concat "^" outline-regexp))
  10337. (current (org-get-tags-string))
  10338. (col (current-column))
  10339. (org-setting-tags t)
  10340. table current-tags inherited-tags ; computed below when needed
  10341. tags p0 c0 c1 rpl)
  10342. (if arg
  10343. (save-excursion
  10344. (goto-char (point-min))
  10345. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  10346. (while (re-search-forward re nil t)
  10347. (org-set-tags nil t)
  10348. (end-of-line 1)))
  10349. (message "All tags realigned to column %d" org-tags-column))
  10350. (if just-align
  10351. (setq tags current)
  10352. ;; Get a new set of tags from the user
  10353. (save-excursion
  10354. (setq table (append org-tag-persistent-alist
  10355. (or org-tag-alist (org-get-buffer-tags)))
  10356. org-last-tags-completion-table table
  10357. current-tags (org-split-string current ":")
  10358. inherited-tags (nreverse
  10359. (nthcdr (length current-tags)
  10360. (nreverse (org-get-tags-at))))
  10361. tags
  10362. (if (or (eq t org-use-fast-tag-selection)
  10363. (and org-use-fast-tag-selection
  10364. (delq nil (mapcar 'cdr table))))
  10365. (org-fast-tag-selection
  10366. current-tags inherited-tags table
  10367. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  10368. (let ((org-add-colon-after-tag-completion t))
  10369. (org-trim
  10370. (org-without-partial-completion
  10371. (org-icompleting-read "Tags: " 'org-tags-completion-function
  10372. nil nil current 'org-tags-history)))))))
  10373. (while (string-match "[-+&]+" tags)
  10374. ;; No boolean logic, just a list
  10375. (setq tags (replace-match ":" t t tags))))
  10376. (if org-tags-sort-function
  10377. (setq tags (mapconcat 'identity
  10378. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  10379. org-tags-sort-function) ":")))
  10380. (if (string-match "\\`[\t ]*\\'" tags)
  10381. (setq tags "")
  10382. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  10383. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  10384. ;; Insert new tags at the correct column
  10385. (beginning-of-line 1)
  10386. (cond
  10387. ((and (equal current "") (equal tags "")))
  10388. ((re-search-forward
  10389. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  10390. (point-at-eol) t)
  10391. (if (equal tags "")
  10392. (setq rpl "")
  10393. (goto-char (match-beginning 0))
  10394. (setq c0 (current-column) p0 (point)
  10395. c1 (max (1+ c0) (if (> org-tags-column 0)
  10396. org-tags-column
  10397. (- (- org-tags-column) (length tags))))
  10398. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  10399. (replace-match rpl t t)
  10400. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  10401. tags)
  10402. (t (error "Tags alignment failed")))
  10403. (org-move-to-column col)
  10404. (unless just-align
  10405. (run-hooks 'org-after-tags-change-hook)))))
  10406. (defun org-change-tag-in-region (beg end tag off)
  10407. "Add or remove TAG for each entry in the region.
  10408. This works in the agenda, and also in an org-mode buffer."
  10409. (interactive
  10410. (list (region-beginning) (region-end)
  10411. (let ((org-last-tags-completion-table
  10412. (if (org-mode-p)
  10413. (org-get-buffer-tags)
  10414. (org-global-tags-completion-table))))
  10415. (org-icompleting-read
  10416. "Tag: " 'org-tags-completion-function nil nil nil
  10417. 'org-tags-history))
  10418. (progn
  10419. (message "[s]et or [r]emove? ")
  10420. (equal (read-char-exclusive) ?r))))
  10421. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10422. (let ((agendap (equal major-mode 'org-agenda-mode))
  10423. l1 l2 m buf pos newhead (cnt 0))
  10424. (goto-char end)
  10425. (setq l2 (1- (org-current-line)))
  10426. (goto-char beg)
  10427. (setq l1 (org-current-line))
  10428. (loop for l from l1 to l2 do
  10429. (org-goto-line l)
  10430. (setq m (get-text-property (point) 'org-hd-marker))
  10431. (when (or (and (org-mode-p) (org-on-heading-p))
  10432. (and agendap m))
  10433. (setq buf (if agendap (marker-buffer m) (current-buffer))
  10434. pos (if agendap m (point)))
  10435. (with-current-buffer buf
  10436. (save-excursion
  10437. (save-restriction
  10438. (goto-char pos)
  10439. (setq cnt (1+ cnt))
  10440. (org-toggle-tag tag (if off 'off 'on))
  10441. (setq newhead (org-get-heading)))))
  10442. (and agendap (org-agenda-change-all-lines newhead m))))
  10443. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  10444. (defun org-tags-completion-function (string predicate &optional flag)
  10445. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  10446. (confirm (lambda (x) (stringp (car x)))))
  10447. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  10448. (setq s1 (match-string 1 string)
  10449. s2 (match-string 2 string))
  10450. (setq s1 "" s2 string))
  10451. (cond
  10452. ((eq flag nil)
  10453. ;; try completion
  10454. (setq rtn (try-completion s2 ctable confirm))
  10455. (if (stringp rtn)
  10456. (setq rtn
  10457. (concat s1 s2 (substring rtn (length s2))
  10458. (if (and org-add-colon-after-tag-completion
  10459. (assoc rtn ctable))
  10460. ":" ""))))
  10461. rtn)
  10462. ((eq flag t)
  10463. ;; all-completions
  10464. (all-completions s2 ctable confirm)
  10465. )
  10466. ((eq flag 'lambda)
  10467. ;; exact match?
  10468. (assoc s2 ctable)))
  10469. ))
  10470. (defun org-fast-tag-insert (kwd tags face &optional end)
  10471. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  10472. (insert (format "%-12s" (concat kwd ":"))
  10473. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  10474. (or end "")))
  10475. (defun org-fast-tag-show-exit (flag)
  10476. (save-excursion
  10477. (org-goto-line 3)
  10478. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10479. (replace-match ""))
  10480. (when flag
  10481. (end-of-line 1)
  10482. (org-move-to-column (- (window-width) 19) t)
  10483. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10484. (defun org-set-current-tags-overlay (current prefix)
  10485. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10486. (if (featurep 'xemacs)
  10487. (org-overlay-display org-tags-overlay (concat prefix s)
  10488. 'secondary-selection)
  10489. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10490. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10491. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  10492. "Fast tag selection with single keys.
  10493. CURRENT is the current list of tags in the headline, INHERITED is the
  10494. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10495. possibly with grouping information. TODO-TABLE is a similar table with
  10496. TODO keywords, should these have keys assigned to them.
  10497. If the keys are nil, a-z are automatically assigned.
  10498. Returns the new tags string, or nil to not change the current settings."
  10499. (let* ((fulltable (append table todo-table))
  10500. (maxlen (apply 'max (mapcar
  10501. (lambda (x)
  10502. (if (stringp (car x)) (string-width (car x)) 0))
  10503. fulltable)))
  10504. (buf (current-buffer))
  10505. (expert (eq org-fast-tag-selection-single-key 'expert))
  10506. (buffer-tags nil)
  10507. (fwidth (+ maxlen 3 1 3))
  10508. (ncol (/ (- (window-width) 4) fwidth))
  10509. (i-face 'org-done)
  10510. (c-face 'org-todo)
  10511. tg cnt e c char c1 c2 ntable tbl rtn
  10512. ov-start ov-end ov-prefix
  10513. (exit-after-next org-fast-tag-selection-single-key)
  10514. (done-keywords org-done-keywords)
  10515. groups ingroup)
  10516. (save-excursion
  10517. (beginning-of-line 1)
  10518. (if (looking-at
  10519. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10520. (setq ov-start (match-beginning 1)
  10521. ov-end (match-end 1)
  10522. ov-prefix "")
  10523. (setq ov-start (1- (point-at-eol))
  10524. ov-end (1+ ov-start))
  10525. (skip-chars-forward "^\n\r")
  10526. (setq ov-prefix
  10527. (concat
  10528. (buffer-substring (1- (point)) (point))
  10529. (if (> (current-column) org-tags-column)
  10530. " "
  10531. (make-string (- org-tags-column (current-column)) ?\ ))))))
  10532. (org-move-overlay org-tags-overlay ov-start ov-end)
  10533. (save-window-excursion
  10534. (if expert
  10535. (set-buffer (get-buffer-create " *Org tags*"))
  10536. (delete-other-windows)
  10537. (split-window-vertically)
  10538. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10539. (erase-buffer)
  10540. (org-set-local 'org-done-keywords done-keywords)
  10541. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10542. (org-fast-tag-insert "Current" current c-face "\n\n")
  10543. (org-fast-tag-show-exit exit-after-next)
  10544. (org-set-current-tags-overlay current ov-prefix)
  10545. (setq tbl fulltable char ?a cnt 0)
  10546. (while (setq e (pop tbl))
  10547. (cond
  10548. ((equal (car e) :startgroup)
  10549. (push '() groups) (setq ingroup t)
  10550. (when (not (= cnt 0))
  10551. (setq cnt 0)
  10552. (insert "\n"))
  10553. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  10554. ((equal (car e) :endgroup)
  10555. (setq ingroup nil cnt 0)
  10556. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  10557. ((equal e '(:newline))
  10558. (when (not (= cnt 0))
  10559. (setq cnt 0)
  10560. (insert "\n")
  10561. (setq e (car tbl))
  10562. (while (equal (car tbl) '(:newline))
  10563. (insert "\n")
  10564. (setq tbl (cdr tbl)))))
  10565. (t
  10566. (setq tg (copy-sequence (car e)) c2 nil)
  10567. (if (cdr e)
  10568. (setq c (cdr e))
  10569. ;; automatically assign a character.
  10570. (setq c1 (string-to-char
  10571. (downcase (substring
  10572. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10573. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10574. (while (or (rassoc char ntable) (rassoc char table))
  10575. (setq char (1+ char)))
  10576. (setq c2 c1))
  10577. (setq c (or c2 char)))
  10578. (if ingroup (push tg (car groups)))
  10579. (setq tg (org-add-props tg nil 'face
  10580. (cond
  10581. ((not (assoc tg table))
  10582. (org-get-todo-face tg))
  10583. ((member tg current) c-face)
  10584. ((member tg inherited) i-face)
  10585. (t nil))))
  10586. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10587. (insert "[" c "] " tg (make-string
  10588. (- fwidth 4 (length tg)) ?\ ))
  10589. (push (cons tg c) ntable)
  10590. (when (= (setq cnt (1+ cnt)) ncol)
  10591. (insert "\n")
  10592. (if ingroup (insert " "))
  10593. (setq cnt 0)))))
  10594. (setq ntable (nreverse ntable))
  10595. (insert "\n")
  10596. (goto-char (point-min))
  10597. (if (not expert) (org-fit-window-to-buffer))
  10598. (setq rtn
  10599. (catch 'exit
  10600. (while t
  10601. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  10602. (if (not groups) "no " "")
  10603. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10604. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10605. (cond
  10606. ((= c ?\r) (throw 'exit t))
  10607. ((= c ?!)
  10608. (setq groups (not groups))
  10609. (goto-char (point-min))
  10610. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10611. ((= c ?\C-c)
  10612. (if (not expert)
  10613. (org-fast-tag-show-exit
  10614. (setq exit-after-next (not exit-after-next)))
  10615. (setq expert nil)
  10616. (delete-other-windows)
  10617. (split-window-vertically)
  10618. (org-switch-to-buffer-other-window " *Org tags*")
  10619. (org-fit-window-to-buffer)))
  10620. ((or (= c ?\C-g)
  10621. (and (= c ?q) (not (rassoc c ntable))))
  10622. (org-detach-overlay org-tags-overlay)
  10623. (setq quit-flag t))
  10624. ((= c ?\ )
  10625. (setq current nil)
  10626. (if exit-after-next (setq exit-after-next 'now)))
  10627. ((= c ?\t)
  10628. (condition-case nil
  10629. (setq tg (org-icompleting-read
  10630. "Tag: "
  10631. (or buffer-tags
  10632. (with-current-buffer buf
  10633. (org-get-buffer-tags)))))
  10634. (quit (setq tg "")))
  10635. (when (string-match "\\S-" tg)
  10636. (add-to-list 'buffer-tags (list tg))
  10637. (if (member tg current)
  10638. (setq current (delete tg current))
  10639. (push tg current)))
  10640. (if exit-after-next (setq exit-after-next 'now)))
  10641. ((setq e (rassoc c todo-table) tg (car e))
  10642. (with-current-buffer buf
  10643. (save-excursion (org-todo tg)))
  10644. (if exit-after-next (setq exit-after-next 'now)))
  10645. ((setq e (rassoc c ntable) tg (car e))
  10646. (if (member tg current)
  10647. (setq current (delete tg current))
  10648. (loop for g in groups do
  10649. (if (member tg g)
  10650. (mapc (lambda (x)
  10651. (setq current (delete x current)))
  10652. g)))
  10653. (push tg current))
  10654. (if exit-after-next (setq exit-after-next 'now))))
  10655. ;; Create a sorted list
  10656. (setq current
  10657. (sort current
  10658. (lambda (a b)
  10659. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10660. (if (eq exit-after-next 'now) (throw 'exit t))
  10661. (goto-char (point-min))
  10662. (beginning-of-line 2)
  10663. (delete-region (point) (point-at-eol))
  10664. (org-fast-tag-insert "Current" current c-face)
  10665. (org-set-current-tags-overlay current ov-prefix)
  10666. (while (re-search-forward
  10667. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10668. (setq tg (match-string 1))
  10669. (add-text-properties
  10670. (match-beginning 1) (match-end 1)
  10671. (list 'face
  10672. (cond
  10673. ((member tg current) c-face)
  10674. ((member tg inherited) i-face)
  10675. (t (get-text-property (match-beginning 1) 'face))))))
  10676. (goto-char (point-min)))))
  10677. (org-detach-overlay org-tags-overlay)
  10678. (if rtn
  10679. (mapconcat 'identity current ":")
  10680. nil))))
  10681. (defun org-get-tags-string ()
  10682. "Get the TAGS string in the current headline."
  10683. (unless (org-on-heading-p t)
  10684. (error "Not on a heading"))
  10685. (save-excursion
  10686. (beginning-of-line 1)
  10687. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10688. (org-match-string-no-properties 1)
  10689. "")))
  10690. (defun org-get-tags ()
  10691. "Get the list of tags specified in the current headline."
  10692. (org-split-string (org-get-tags-string) ":"))
  10693. (defun org-get-buffer-tags ()
  10694. "Get a table of all tags used in the buffer, for completion."
  10695. (let (tags)
  10696. (save-excursion
  10697. (goto-char (point-min))
  10698. (while (re-search-forward
  10699. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10700. (when (equal (char-after (point-at-bol 0)) ?*)
  10701. (mapc (lambda (x) (add-to-list 'tags x))
  10702. (org-split-string (org-match-string-no-properties 1) ":")))))
  10703. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  10704. (mapcar 'list tags)))
  10705. ;;;; The mapping API
  10706. ;;;###autoload
  10707. (defun org-map-entries (func &optional match scope &rest skip)
  10708. "Call FUNC at each headline selected by MATCH in SCOPE.
  10709. FUNC is a function or a lisp form. The function will be called without
  10710. arguments, with the cursor positioned at the beginning of the headline.
  10711. The return values of all calls to the function will be collected and
  10712. returned as a list.
  10713. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10714. does not need to preserve point. After evaluation, the cursor will be
  10715. moved to the end of the line (presumably of the headline of the
  10716. processed entry) and search continues from there. Under some
  10717. circumstances, this may not produce the wanted results. For example,
  10718. if you have removed (e.g. archived) the current (sub)tree it could
  10719. mean that the next entry will be skipped entirely. In such cases, you
  10720. can specify the position from where search should continue by making
  10721. FUNC set the variable `org-map-continue-from' to the desired buffer
  10722. position.
  10723. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10724. Only headlines that are matched by this query will be considered during
  10725. the iteration. When MATCH is nil or t, all headlines will be
  10726. visited by the iteration.
  10727. SCOPE determines the scope of this command. It can be any of:
  10728. nil The current buffer, respecting the restriction if any
  10729. tree The subtree started with the entry at point
  10730. file The current buffer, without restriction
  10731. file-with-archives
  10732. The current buffer, and any archives associated with it
  10733. agenda All agenda files
  10734. agenda-with-archives
  10735. All agenda files with any archive files associated with them
  10736. \(file1 file2 ...)
  10737. If this is a list, all files in the list will be scanned
  10738. The remaining args are treated as settings for the skipping facilities of
  10739. the scanner. The following items can be given here:
  10740. archive skip trees with the archive tag.
  10741. comment skip trees with the COMMENT keyword
  10742. function or Emacs Lisp form:
  10743. will be used as value for `org-agenda-skip-function', so whenever
  10744. the function returns t, FUNC will not be called for that
  10745. entry and search will continue from the point where the
  10746. function leaves it.
  10747. If your function needs to retrieve the tags including inherited tags
  10748. at the *current* entry, you can use the value of the variable
  10749. `org-scanner-tags' which will be much faster than getting the value
  10750. with `org-get-tags-at'. If your function gets properties with
  10751. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10752. to t around the call to `org-entry-properties' to get the same speedup.
  10753. Note that if your function moves around to retrieve tags and properties at
  10754. a *different* entry, you cannot use these techniques."
  10755. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10756. (org-agenda-skip-archived-trees (memq 'archive skip))
  10757. (org-agenda-skip-comment-trees (memq 'comment skip))
  10758. (org-agenda-skip-function
  10759. (car (org-delete-all '(comment archive) skip)))
  10760. (org-tags-match-list-sublevels t)
  10761. matcher file res
  10762. org-todo-keywords-for-agenda
  10763. org-done-keywords-for-agenda
  10764. org-todo-keyword-alist-for-agenda
  10765. org-drawers-for-agenda
  10766. org-tag-alist-for-agenda)
  10767. (cond
  10768. ((eq match t) (setq matcher t))
  10769. ((eq match nil) (setq matcher t))
  10770. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10771. (save-excursion
  10772. (save-restriction
  10773. (when (eq scope 'tree)
  10774. (org-back-to-heading t)
  10775. (org-narrow-to-subtree)
  10776. (setq scope nil))
  10777. (if (not scope)
  10778. (progn
  10779. (org-prepare-agenda-buffers
  10780. (list (buffer-file-name (current-buffer))))
  10781. (setq res (org-scan-tags func matcher)))
  10782. ;; Get the right scope
  10783. (cond
  10784. ((and scope (listp scope) (symbolp (car scope)))
  10785. (setq scope (eval scope)))
  10786. ((eq scope 'agenda)
  10787. (setq scope (org-agenda-files t)))
  10788. ((eq scope 'agenda-with-archives)
  10789. (setq scope (org-agenda-files t))
  10790. (setq scope (org-add-archive-files scope)))
  10791. ((eq scope 'file)
  10792. (setq scope (list (buffer-file-name))))
  10793. ((eq scope 'file-with-archives)
  10794. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10795. (org-prepare-agenda-buffers scope)
  10796. (while (setq file (pop scope))
  10797. (with-current-buffer (org-find-base-buffer-visiting file)
  10798. (save-excursion
  10799. (save-restriction
  10800. (widen)
  10801. (goto-char (point-min))
  10802. (setq res (append res (org-scan-tags func matcher))))))))))
  10803. res))
  10804. ;;;; Properties
  10805. ;;; Setting and retrieving properties
  10806. (defconst org-special-properties
  10807. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10808. "TIMESTAMP" "TIMESTAMP_IA")
  10809. "The special properties valid in Org-mode.
  10810. These are properties that are not defined in the property drawer,
  10811. but in some other way.")
  10812. (defconst org-default-properties
  10813. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10814. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10815. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10816. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10817. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  10818. "CLOCK_MODELINE_TOTAL")
  10819. "Some properties that are used by Org-mode for various purposes.
  10820. Being in this list makes sure that they are offered for completion.")
  10821. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10822. "Regular expression matching the first line of a property drawer.")
  10823. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10824. "Regular expression matching the first line of a property drawer.")
  10825. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10826. "Regular expression matching the first line of a property drawer.")
  10827. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10828. "Regular expression matching the first line of a property drawer.")
  10829. (defconst org-property-drawer-re
  10830. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10831. org-property-end-re "\\)\n?")
  10832. "Matches an entire property drawer.")
  10833. (defconst org-clock-drawer-re
  10834. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10835. org-property-end-re "\\)\n?")
  10836. "Matches an entire clock drawer.")
  10837. (defun org-property-action ()
  10838. "Do an action on properties."
  10839. (interactive)
  10840. (let (c)
  10841. (org-at-property-p)
  10842. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10843. (setq c (read-char-exclusive))
  10844. (cond
  10845. ((equal c ?s)
  10846. (call-interactively 'org-set-property))
  10847. ((equal c ?d)
  10848. (call-interactively 'org-delete-property))
  10849. ((equal c ?D)
  10850. (call-interactively 'org-delete-property-globally))
  10851. ((equal c ?c)
  10852. (call-interactively 'org-compute-property-at-point))
  10853. (t (error "No such property action %c" c)))))
  10854. (defun org-set-effort (&optional value)
  10855. "Set the effort property of the current entry.
  10856. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  10857. allowed value."
  10858. (interactive "P")
  10859. (if (equal value 0) (setq value 10))
  10860. (let* ((completion-ignore-case t)
  10861. (prop org-effort-property)
  10862. (cur (org-entry-get nil prop))
  10863. (allowed (org-property-get-allowed-values nil prop 'table))
  10864. (existing (mapcar 'list (org-property-values prop)))
  10865. (val (cond
  10866. ((stringp value) value)
  10867. ((and allowed (integerp value))
  10868. (or (car (nth (1- value) allowed))
  10869. (car (org-last allowed))))
  10870. (allowed
  10871. (org-completing-read "Value: " allowed nil 'req-match))
  10872. (t
  10873. (let (org-completion-use-ido org-completion-use-iswitchb)
  10874. (org-completing-read
  10875. (concat "Value " (if (and cur (string-match "\\S-" cur))
  10876. (concat "[" cur "]") "")
  10877. ": ")
  10878. existing nil nil "" nil cur))))))
  10879. (unless (equal (org-entry-get nil prop) val)
  10880. (org-entry-put nil prop val))
  10881. (message "%s is now %s" prop val)))
  10882. (defun org-at-property-p ()
  10883. "Is the cursor in a property line?"
  10884. ;; FIXME: Does not check if we are actually in the drawer.
  10885. ;; FIXME: also returns true on any drawers.....
  10886. ;; This is used by C-c C-c for property action.
  10887. (save-excursion
  10888. (beginning-of-line 1)
  10889. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10890. (defun org-get-property-block (&optional beg end force)
  10891. "Return the (beg . end) range of the body of the property drawer.
  10892. BEG and END can be beginning and end of subtree, if not given
  10893. they will be found.
  10894. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10895. (catch 'exit
  10896. (save-excursion
  10897. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10898. (end (or end (progn (outline-next-heading) (point)))))
  10899. (goto-char beg)
  10900. (if (re-search-forward org-property-start-re end t)
  10901. (setq beg (1+ (match-end 0)))
  10902. (if force
  10903. (save-excursion
  10904. (org-insert-property-drawer)
  10905. (setq end (progn (outline-next-heading) (point))))
  10906. (throw 'exit nil))
  10907. (goto-char beg)
  10908. (if (re-search-forward org-property-start-re end t)
  10909. (setq beg (1+ (match-end 0)))))
  10910. (if (re-search-forward org-property-end-re end t)
  10911. (setq end (match-beginning 0))
  10912. (or force (throw 'exit nil))
  10913. (goto-char beg)
  10914. (setq end beg)
  10915. (org-indent-line-function)
  10916. (insert ":END:\n"))
  10917. (cons beg end)))))
  10918. (defun org-entry-properties (&optional pom which)
  10919. "Get all properties of the entry at point-or-marker POM.
  10920. This includes the TODO keyword, the tags, time strings for deadline,
  10921. scheduled, and clocking, and any additional properties defined in the
  10922. entry. The return value is an alist, keys may occur multiple times
  10923. if the property key was used several times.
  10924. POM may also be nil, in which case the current entry is used.
  10925. If WHICH is nil or `all', get all properties. If WHICH is
  10926. `special' or `standard', only get that subclass."
  10927. (setq which (or which 'all))
  10928. (org-with-point-at pom
  10929. (let ((clockstr (substring org-clock-string 0 -1))
  10930. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10931. beg end range props sum-props key value string clocksum)
  10932. (save-excursion
  10933. (when (condition-case nil
  10934. (and (org-mode-p) (org-back-to-heading t))
  10935. (error nil))
  10936. (setq beg (point))
  10937. (setq sum-props (get-text-property (point) 'org-summaries))
  10938. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10939. (outline-next-heading)
  10940. (setq end (point))
  10941. (when (memq which '(all special))
  10942. ;; Get the special properties, like TODO and tags
  10943. (goto-char beg)
  10944. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10945. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10946. (when (looking-at org-priority-regexp)
  10947. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10948. (when (and (setq value (org-get-tags-string))
  10949. (string-match "\\S-" value))
  10950. (push (cons "TAGS" value) props))
  10951. (when (setq value (org-get-tags-at))
  10952. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10953. props))
  10954. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10955. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10956. string (if (equal key clockstr)
  10957. (org-no-properties
  10958. (org-trim
  10959. (buffer-substring
  10960. (match-beginning 3) (goto-char (point-at-eol)))))
  10961. (substring (org-match-string-no-properties 3) 1 -1)))
  10962. (unless key
  10963. (if (= (char-after (match-beginning 3)) ?\[)
  10964. (setq key "TIMESTAMP_IA")
  10965. (setq key "TIMESTAMP")))
  10966. (when (or (equal key clockstr) (not (assoc key props)))
  10967. (push (cons key string) props)))
  10968. )
  10969. (when (memq which '(all standard))
  10970. ;; Get the standard properties, like :PROP: ...
  10971. (setq range (org-get-property-block beg end))
  10972. (when range
  10973. (goto-char (car range))
  10974. (while (re-search-forward
  10975. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10976. (cdr range) t)
  10977. (setq key (org-match-string-no-properties 1)
  10978. value (org-trim (or (org-match-string-no-properties 2) "")))
  10979. (unless (member key excluded)
  10980. (push (cons key (or value "")) props)))))
  10981. (if clocksum
  10982. (push (cons "CLOCKSUM"
  10983. (org-columns-number-to-string (/ (float clocksum) 60.)
  10984. 'add_times))
  10985. props))
  10986. (unless (assoc "CATEGORY" props)
  10987. (setq value (or (org-get-category)
  10988. (progn (org-refresh-category-properties)
  10989. (org-get-category))))
  10990. (push (cons "CATEGORY" value) props))
  10991. (append sum-props (nreverse props)))))))
  10992. (defun org-entry-get (pom property &optional inherit)
  10993. "Get value of PROPERTY for entry at point-or-marker POM.
  10994. If INHERIT is non-nil and the entry does not have the property,
  10995. then also check higher levels of the hierarchy.
  10996. If INHERIT is the symbol `selective', use inheritance only if the setting
  10997. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10998. If the property is present but empty, the return value is the empty string.
  10999. If the property is not present at all, nil is returned."
  11000. (org-with-point-at pom
  11001. (if (and inherit (if (eq inherit 'selective)
  11002. (org-property-inherit-p property)
  11003. t))
  11004. (org-entry-get-with-inheritance property)
  11005. (if (member property org-special-properties)
  11006. ;; We need a special property. Use brute force, get all properties.
  11007. (cdr (assoc property (org-entry-properties nil 'special)))
  11008. (let ((range (org-get-property-block)))
  11009. (if (and range
  11010. (goto-char (car range))
  11011. (re-search-forward
  11012. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  11013. (cdr range) t))
  11014. ;; Found the property, return it.
  11015. (if (match-end 1)
  11016. (org-match-string-no-properties 1)
  11017. "")))))))
  11018. (defun org-property-or-variable-value (var &optional inherit)
  11019. "Check if there is a property fixing the value of VAR.
  11020. If yes, return this value. If not, return the current value of the variable."
  11021. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  11022. (if (and prop (stringp prop) (string-match "\\S-" prop))
  11023. (read prop)
  11024. (symbol-value var))))
  11025. (defun org-entry-delete (pom property)
  11026. "Delete the property PROPERTY from entry at point-or-marker POM."
  11027. (org-with-point-at pom
  11028. (if (member property org-special-properties)
  11029. nil ; cannot delete these properties.
  11030. (let ((range (org-get-property-block)))
  11031. (if (and range
  11032. (goto-char (car range))
  11033. (re-search-forward
  11034. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  11035. (cdr range) t))
  11036. (progn
  11037. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  11038. t)
  11039. nil)))))
  11040. ;; Multi-values properties are properties that contain multiple values
  11041. ;; These values are assumed to be single words, separated by whitespace.
  11042. (defun org-entry-add-to-multivalued-property (pom property value)
  11043. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  11044. (let* ((old (org-entry-get pom property))
  11045. (values (and old (org-split-string old "[ \t]"))))
  11046. (setq value (org-entry-protect-space value))
  11047. (unless (member value values)
  11048. (setq values (cons value values))
  11049. (org-entry-put pom property
  11050. (mapconcat 'identity values " ")))))
  11051. (defun org-entry-remove-from-multivalued-property (pom property value)
  11052. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  11053. (let* ((old (org-entry-get pom property))
  11054. (values (and old (org-split-string old "[ \t]"))))
  11055. (setq value (org-entry-protect-space value))
  11056. (when (member value values)
  11057. (setq values (delete value values))
  11058. (org-entry-put pom property
  11059. (mapconcat 'identity values " ")))))
  11060. (defun org-entry-member-in-multivalued-property (pom property value)
  11061. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  11062. (let* ((old (org-entry-get pom property))
  11063. (values (and old (org-split-string old "[ \t]"))))
  11064. (setq value (org-entry-protect-space value))
  11065. (member value values)))
  11066. (defun org-entry-get-multivalued-property (pom property)
  11067. "Return a list of values in a multivalued property."
  11068. (let* ((value (org-entry-get pom property))
  11069. (values (and value (org-split-string value "[ \t]"))))
  11070. (mapcar 'org-entry-restore-space values)))
  11071. (defun org-entry-put-multivalued-property (pom property &rest values)
  11072. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  11073. VALUES should be a list of strings. Spaces will be protected."
  11074. (org-entry-put pom property
  11075. (mapconcat 'org-entry-protect-space values " "))
  11076. (let* ((value (org-entry-get pom property))
  11077. (values (and value (org-split-string value "[ \t]"))))
  11078. (mapcar 'org-entry-restore-space values)))
  11079. (defun org-entry-protect-space (s)
  11080. "Protect spaces and newline in string S."
  11081. (while (string-match " " s)
  11082. (setq s (replace-match "%20" t t s)))
  11083. (while (string-match "\n" s)
  11084. (setq s (replace-match "%0A" t t s)))
  11085. s)
  11086. (defun org-entry-restore-space (s)
  11087. "Restore spaces and newline in string S."
  11088. (while (string-match "%20" s)
  11089. (setq s (replace-match " " t t s)))
  11090. (while (string-match "%0A" s)
  11091. (setq s (replace-match "\n" t t s)))
  11092. s)
  11093. (defvar org-entry-property-inherited-from (make-marker)
  11094. "Marker pointing to the entry from where a property was inherited.
  11095. Each call to `org-entry-get-with-inheritance' will set this marker to the
  11096. location of the entry where the inheritance search matched. If there was
  11097. no match, the marker will point nowhere.
  11098. Note that also `org-entry-get' calls this function, if the INHERIT flag
  11099. is set.")
  11100. (defun org-entry-get-with-inheritance (property)
  11101. "Get entry property, and search higher levels if not present."
  11102. (move-marker org-entry-property-inherited-from nil)
  11103. (let (tmp)
  11104. (save-excursion
  11105. (save-restriction
  11106. (widen)
  11107. (catch 'ex
  11108. (while t
  11109. (when (setq tmp (org-entry-get nil property))
  11110. (org-back-to-heading t)
  11111. (move-marker org-entry-property-inherited-from (point))
  11112. (throw 'ex tmp))
  11113. (or (org-up-heading-safe) (throw 'ex nil)))))
  11114. (or tmp
  11115. (cdr (assoc property org-file-properties))
  11116. (cdr (assoc property org-global-properties))
  11117. (cdr (assoc property org-global-properties-fixed))))))
  11118. (defun org-entry-put (pom property value)
  11119. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  11120. (org-with-point-at pom
  11121. (org-back-to-heading t)
  11122. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  11123. range)
  11124. (cond
  11125. ((equal property "TODO")
  11126. (when (and (stringp value) (string-match "\\S-" value)
  11127. (not (member value org-todo-keywords-1)))
  11128. (error "\"%s\" is not a valid TODO state" value))
  11129. (if (or (not value)
  11130. (not (string-match "\\S-" value)))
  11131. (setq value 'none))
  11132. (org-todo value)
  11133. (org-set-tags nil 'align))
  11134. ((equal property "PRIORITY")
  11135. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  11136. (string-to-char value) ?\ ))
  11137. (org-set-tags nil 'align))
  11138. ((equal property "SCHEDULED")
  11139. (if (re-search-forward org-scheduled-time-regexp end t)
  11140. (cond
  11141. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11142. ((eq value 'later) (org-timestamp-change 1 'day))
  11143. (t (call-interactively 'org-schedule)))
  11144. (call-interactively 'org-schedule)))
  11145. ((equal property "DEADLINE")
  11146. (if (re-search-forward org-deadline-time-regexp end t)
  11147. (cond
  11148. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11149. ((eq value 'later) (org-timestamp-change 1 'day))
  11150. (t (call-interactively 'org-deadline)))
  11151. (call-interactively 'org-deadline)))
  11152. ((member property org-special-properties)
  11153. (error "The %s property can not yet be set with `org-entry-put'"
  11154. property))
  11155. (t ; a non-special property
  11156. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  11157. (setq range (org-get-property-block beg end 'force))
  11158. (goto-char (car range))
  11159. (if (re-search-forward
  11160. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  11161. (progn
  11162. (delete-region (match-beginning 1) (match-end 1))
  11163. (goto-char (match-beginning 1)))
  11164. (goto-char (cdr range))
  11165. (insert "\n")
  11166. (backward-char 1)
  11167. (org-indent-line-function)
  11168. (insert ":" property ":"))
  11169. (and value (insert " " value))
  11170. (org-indent-line-function)))))))
  11171. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  11172. "Get all property keys in the current buffer.
  11173. With INCLUDE-SPECIALS, also list the special properties that reflect things
  11174. like tags and TODO state.
  11175. With INCLUDE-DEFAULTS, also include properties that has special meaning
  11176. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  11177. With INCLUDE-COLUMNS, also include property names given in COLUMN
  11178. formats in the current buffer."
  11179. (let (rtn range cfmt s p)
  11180. (save-excursion
  11181. (save-restriction
  11182. (widen)
  11183. (goto-char (point-min))
  11184. (while (re-search-forward org-property-start-re nil t)
  11185. (setq range (org-get-property-block))
  11186. (goto-char (car range))
  11187. (while (re-search-forward
  11188. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  11189. (cdr range) t)
  11190. (add-to-list 'rtn (org-match-string-no-properties 1)))
  11191. (outline-next-heading))))
  11192. (when include-specials
  11193. (setq rtn (append org-special-properties rtn)))
  11194. (when include-defaults
  11195. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  11196. (add-to-list 'rtn org-effort-property))
  11197. (when include-columns
  11198. (save-excursion
  11199. (save-restriction
  11200. (widen)
  11201. (goto-char (point-min))
  11202. (while (re-search-forward
  11203. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  11204. nil t)
  11205. (setq cfmt (match-string 2) s 0)
  11206. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  11207. cfmt s)
  11208. (setq s (match-end 0)
  11209. p (match-string 1 cfmt))
  11210. (unless (or (equal p "ITEM")
  11211. (member p org-special-properties))
  11212. (add-to-list 'rtn (match-string 1 cfmt))))))))
  11213. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  11214. (defun org-property-values (key)
  11215. "Return a list of all values of property KEY."
  11216. (save-excursion
  11217. (save-restriction
  11218. (widen)
  11219. (goto-char (point-min))
  11220. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  11221. values)
  11222. (while (re-search-forward re nil t)
  11223. (add-to-list 'values (org-trim (match-string 1))))
  11224. (delete "" values)))))
  11225. (defun org-insert-property-drawer ()
  11226. "Insert a property drawer into the current entry."
  11227. (interactive)
  11228. (org-back-to-heading t)
  11229. (looking-at outline-regexp)
  11230. (let ((indent (if org-adapt-indentation
  11231. (- (match-end 0)(match-beginning 0))
  11232. 0))
  11233. (beg (point))
  11234. (re (concat "^[ \t]*" org-keyword-time-regexp))
  11235. end hiddenp)
  11236. (outline-next-heading)
  11237. (setq end (point))
  11238. (goto-char beg)
  11239. (while (re-search-forward re end t))
  11240. (setq hiddenp (org-invisible-p))
  11241. (end-of-line 1)
  11242. (and (equal (char-after) ?\n) (forward-char 1))
  11243. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  11244. (if (member (match-string 1) '("CLOCK:" ":END:"))
  11245. ;; just skip this line
  11246. (beginning-of-line 2)
  11247. ;; Drawer start, find the end
  11248. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  11249. (beginning-of-line 1)))
  11250. (org-skip-over-state-notes)
  11251. (skip-chars-backward " \t\n\r")
  11252. (if (eq (char-before) ?*) (forward-char 1))
  11253. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  11254. (beginning-of-line 0)
  11255. (org-indent-to-column indent)
  11256. (beginning-of-line 2)
  11257. (org-indent-to-column indent)
  11258. (beginning-of-line 0)
  11259. (if hiddenp
  11260. (save-excursion
  11261. (org-back-to-heading t)
  11262. (hide-entry))
  11263. (org-flag-drawer t))))
  11264. (defun org-set-property (property value)
  11265. "In the current entry, set PROPERTY to VALUE.
  11266. When called interactively, this will prompt for a property name, offering
  11267. completion on existing and default properties. And then it will prompt
  11268. for a value, offering completion either on allowed values (via an inherited
  11269. xxx_ALL property) or on existing values in other instances of this property
  11270. in the current file."
  11271. (interactive
  11272. (let* ((completion-ignore-case t)
  11273. (keys (org-buffer-property-keys nil t t))
  11274. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  11275. (prop (if (member prop0 keys)
  11276. prop0
  11277. (or (cdr (assoc (downcase prop0)
  11278. (mapcar (lambda (x) (cons (downcase x) x))
  11279. keys)))
  11280. prop0)))
  11281. (cur (org-entry-get nil prop))
  11282. (allowed (org-property-get-allowed-values nil prop 'table))
  11283. (existing (mapcar 'list (org-property-values prop)))
  11284. (val (if allowed
  11285. (org-completing-read "Value: " allowed nil 'req-match)
  11286. (let (org-completion-use-ido org-completion-use-iswitchb)
  11287. (org-completing-read
  11288. (concat "Value " (if (and cur (string-match "\\S-" cur))
  11289. (concat "[" cur "]") "")
  11290. ": ")
  11291. existing nil nil "" nil cur)))))
  11292. (list prop (if (equal val "") cur val))))
  11293. (unless (equal (org-entry-get nil property) value)
  11294. (org-entry-put nil property value)))
  11295. (defun org-delete-property (property)
  11296. "In the current entry, delete PROPERTY."
  11297. (interactive
  11298. (let* ((completion-ignore-case t)
  11299. (prop (org-icompleting-read
  11300. "Property: " (org-entry-properties nil 'standard))))
  11301. (list prop)))
  11302. (message "Property %s %s" property
  11303. (if (org-entry-delete nil property)
  11304. "deleted"
  11305. "was not present in the entry")))
  11306. (defun org-delete-property-globally (property)
  11307. "Remove PROPERTY globally, from all entries."
  11308. (interactive
  11309. (let* ((completion-ignore-case t)
  11310. (prop (org-icompleting-read
  11311. "Globally remove property: "
  11312. (mapcar 'list (org-buffer-property-keys)))))
  11313. (list prop)))
  11314. (save-excursion
  11315. (save-restriction
  11316. (widen)
  11317. (goto-char (point-min))
  11318. (let ((cnt 0))
  11319. (while (re-search-forward
  11320. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  11321. nil t)
  11322. (setq cnt (1+ cnt))
  11323. (replace-match ""))
  11324. (message "Property \"%s\" removed from %d entries" property cnt)))))
  11325. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  11326. (defun org-compute-property-at-point ()
  11327. "Compute the property at point.
  11328. This looks for an enclosing column format, extracts the operator and
  11329. then applies it to the property in the column format's scope."
  11330. (interactive)
  11331. (unless (org-at-property-p)
  11332. (error "Not at a property"))
  11333. (let ((prop (org-match-string-no-properties 2)))
  11334. (org-columns-get-format-and-top-level)
  11335. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  11336. (error "No operator defined for property %s" prop))
  11337. (org-columns-compute prop)))
  11338. (defun org-property-get-allowed-values (pom property &optional table)
  11339. "Get allowed values for the property PROPERTY.
  11340. When TABLE is non-nil, return an alist that can directly be used for
  11341. completion."
  11342. (let (vals)
  11343. (cond
  11344. ((equal property "TODO")
  11345. (setq vals (org-with-point-at pom
  11346. (append org-todo-keywords-1 '("")))))
  11347. ((equal property "PRIORITY")
  11348. (let ((n org-lowest-priority))
  11349. (while (>= n org-highest-priority)
  11350. (push (char-to-string n) vals)
  11351. (setq n (1- n)))))
  11352. ((member property org-special-properties))
  11353. (t
  11354. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  11355. (when (and vals (string-match "\\S-" vals))
  11356. (setq vals (car (read-from-string (concat "(" vals ")"))))
  11357. (setq vals (mapcar (lambda (x)
  11358. (cond ((stringp x) x)
  11359. ((numberp x) (number-to-string x))
  11360. ((symbolp x) (symbol-name x))
  11361. (t "???")))
  11362. vals)))))
  11363. (if table (mapcar 'list vals) vals)))
  11364. (defun org-property-previous-allowed-value (&optional previous)
  11365. "Switch to the next allowed value for this property."
  11366. (interactive)
  11367. (org-property-next-allowed-value t))
  11368. (defun org-property-next-allowed-value (&optional previous)
  11369. "Switch to the next allowed value for this property."
  11370. (interactive)
  11371. (unless (org-at-property-p)
  11372. (error "Not at a property"))
  11373. (let* ((key (match-string 2))
  11374. (value (match-string 3))
  11375. (allowed (or (org-property-get-allowed-values (point) key)
  11376. (and (member value '("[ ]" "[-]" "[X]"))
  11377. '("[ ]" "[X]"))))
  11378. nval)
  11379. (unless allowed
  11380. (error "Allowed values for this property have not been defined"))
  11381. (if previous (setq allowed (reverse allowed)))
  11382. (if (member value allowed)
  11383. (setq nval (car (cdr (member value allowed)))))
  11384. (setq nval (or nval (car allowed)))
  11385. (if (equal nval value)
  11386. (error "Only one allowed value for this property"))
  11387. (org-at-property-p)
  11388. (replace-match (concat " :" key ": " nval) t t)
  11389. (org-indent-line-function)
  11390. (beginning-of-line 1)
  11391. (skip-chars-forward " \t")))
  11392. (defun org-find-entry-with-id (ident)
  11393. "Locate the entry that contains the ID property with exact value IDENT.
  11394. IDENT can be a string, a symbol or a number, this function will search for
  11395. the string representation of it.
  11396. Return the position where this entry starts, or nil if there is no such entry."
  11397. (interactive "sID: ")
  11398. (let ((id (cond
  11399. ((stringp ident) ident)
  11400. ((symbol-name ident) (symbol-name ident))
  11401. ((numberp ident) (number-to-string ident))
  11402. (t (error "IDENT %s must be a string, symbol or number" ident))))
  11403. (case-fold-search nil))
  11404. (save-excursion
  11405. (save-restriction
  11406. (widen)
  11407. (goto-char (point-min))
  11408. (when (re-search-forward
  11409. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  11410. nil t)
  11411. (org-back-to-heading t)
  11412. (point))))))
  11413. ;;;; Timestamps
  11414. (defvar org-last-changed-timestamp nil)
  11415. (defvar org-last-inserted-timestamp nil
  11416. "The last time stamp inserted with `org-insert-time-stamp'.")
  11417. (defvar org-time-was-given) ; dynamically scoped parameter
  11418. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11419. (defvar org-ts-what) ; dynamically scoped parameter
  11420. (defun org-time-stamp (arg &optional inactive)
  11421. "Prompt for a date/time and insert a time stamp.
  11422. If the user specifies a time like HH:MM, or if this command is called
  11423. with a prefix argument, the time stamp will contain date and time.
  11424. Otherwise, only the date will be included. All parts of a date not
  11425. specified by the user will be filled in from the current date/time.
  11426. So if you press just return without typing anything, the time stamp
  11427. will represent the current date/time. If there is already a timestamp
  11428. at the cursor, it will be modified."
  11429. (interactive "P")
  11430. (let* ((ts nil)
  11431. (default-time
  11432. ;; Default time is either today, or, when entering a range,
  11433. ;; the range start.
  11434. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  11435. (save-excursion
  11436. (re-search-backward
  11437. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  11438. (- (point) 20) t)))
  11439. (apply 'encode-time (org-parse-time-string (match-string 1)))
  11440. (current-time)))
  11441. (default-input (and ts (org-get-compact-tod ts)))
  11442. org-time-was-given org-end-time-was-given time)
  11443. (cond
  11444. ((and (org-at-timestamp-p t)
  11445. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  11446. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  11447. (insert "--")
  11448. (setq time (let ((this-command this-command))
  11449. (org-read-date arg 'totime nil nil
  11450. default-time default-input)))
  11451. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  11452. ((org-at-timestamp-p t)
  11453. (setq time (let ((this-command this-command))
  11454. (org-read-date arg 'totime nil nil default-time default-input)))
  11455. (when (org-at-timestamp-p t) ; just to get the match data
  11456. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  11457. (replace-match "")
  11458. (setq org-last-changed-timestamp
  11459. (org-insert-time-stamp
  11460. time (or org-time-was-given arg)
  11461. inactive nil nil (list org-end-time-was-given))))
  11462. (message "Timestamp updated"))
  11463. (t
  11464. (setq time (let ((this-command this-command))
  11465. (org-read-date arg 'totime nil nil default-time default-input)))
  11466. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  11467. nil nil (list org-end-time-was-given))))))
  11468. ;; FIXME: can we use this for something else, like computing time differences?
  11469. (defun org-get-compact-tod (s)
  11470. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  11471. (let* ((t1 (match-string 1 s))
  11472. (h1 (string-to-number (match-string 2 s)))
  11473. (m1 (string-to-number (match-string 3 s)))
  11474. (t2 (and (match-end 4) (match-string 5 s)))
  11475. (h2 (and t2 (string-to-number (match-string 6 s))))
  11476. (m2 (and t2 (string-to-number (match-string 7 s))))
  11477. dh dm)
  11478. (if (not t2)
  11479. t1
  11480. (setq dh (- h2 h1) dm (- m2 m1))
  11481. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  11482. (concat t1 "+" (number-to-string dh)
  11483. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  11484. (defun org-time-stamp-inactive (&optional arg)
  11485. "Insert an inactive time stamp.
  11486. An inactive time stamp is enclosed in square brackets instead of angle
  11487. brackets. It is inactive in the sense that it does not trigger agenda entries,
  11488. does not link to the calendar and cannot be changed with the S-cursor keys.
  11489. So these are more for recording a certain time/date."
  11490. (interactive "P")
  11491. (org-time-stamp arg 'inactive))
  11492. (defvar org-date-ovl (org-make-overlay 1 1))
  11493. (org-overlay-put org-date-ovl 'face 'org-warning)
  11494. (org-detach-overlay org-date-ovl)
  11495. (defvar org-ans1) ; dynamically scoped parameter
  11496. (defvar org-ans2) ; dynamically scoped parameter
  11497. (defvar org-plain-time-of-day-regexp) ; defined below
  11498. (defvar org-overriding-default-time nil) ; dynamically scoped
  11499. (defvar org-read-date-overlay nil)
  11500. (defvar org-dcst nil) ; dynamically scoped
  11501. (defvar org-read-date-history nil)
  11502. (defvar org-read-date-final-answer nil)
  11503. (defun org-read-date (&optional with-time to-time from-string prompt
  11504. default-time default-input)
  11505. "Read a date, possibly a time, and make things smooth for the user.
  11506. The prompt will suggest to enter an ISO date, but you can also enter anything
  11507. which will at least partially be understood by `parse-time-string'.
  11508. Unrecognized parts of the date will default to the current day, month, year,
  11509. hour and minute. If this command is called to replace a timestamp at point,
  11510. of to enter the second timestamp of a range, the default time is taken from the
  11511. existing stamp. For example,
  11512. 3-2-5 --> 2003-02-05
  11513. feb 15 --> currentyear-02-15
  11514. sep 12 9 --> 2009-09-12
  11515. 12:45 --> today 12:45
  11516. 22 sept 0:34 --> currentyear-09-22 0:34
  11517. 12 --> currentyear-currentmonth-12
  11518. Fri --> nearest Friday (today or later)
  11519. etc.
  11520. Furthermore you can specify a relative date by giving, as the *first* thing
  11521. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  11522. change in days weeks, months, years.
  11523. With a single plus or minus, the date is relative to today. With a double
  11524. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  11525. +4d --> four days from today
  11526. +4 --> same as above
  11527. +2w --> two weeks from today
  11528. ++5 --> five days from default date
  11529. The function understands only English month and weekday abbreviations,
  11530. but this can be configured with the variables `parse-time-months' and
  11531. `parse-time-weekdays'.
  11532. While prompting, a calendar is popped up - you can also select the
  11533. date with the mouse (button 1). The calendar shows a period of three
  11534. months. To scroll it to other months, use the keys `>' and `<'.
  11535. If you don't like the calendar, turn it off with
  11536. \(setq org-read-date-popup-calendar nil)
  11537. With optional argument TO-TIME, the date will immediately be converted
  11538. to an internal time.
  11539. With an optional argument WITH-TIME, the prompt will suggest to also
  11540. insert a time. Note that when WITH-TIME is not set, you can still
  11541. enter a time, and this function will inform the calling routine about
  11542. this change. The calling routine may then choose to change the format
  11543. used to insert the time stamp into the buffer to include the time.
  11544. With optional argument FROM-STRING, read from this string instead from
  11545. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  11546. the time/date that is used for everything that is not specified by the
  11547. user."
  11548. (require 'parse-time)
  11549. (let* ((org-time-stamp-rounding-minutes
  11550. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  11551. (org-dcst org-display-custom-times)
  11552. (ct (org-current-time))
  11553. (def (or org-overriding-default-time default-time ct))
  11554. (defdecode (decode-time def))
  11555. (dummy (progn
  11556. (when (< (nth 2 defdecode) org-extend-today-until)
  11557. (setcar (nthcdr 2 defdecode) -1)
  11558. (setcar (nthcdr 1 defdecode) 59)
  11559. (setq def (apply 'encode-time defdecode)
  11560. defdecode (decode-time def)))))
  11561. (calendar-frame-setup nil)
  11562. (calendar-move-hook nil)
  11563. (calendar-view-diary-initially-flag nil)
  11564. (view-diary-entries-initially nil)
  11565. (calendar-view-holidays-initially-flag nil)
  11566. (view-calendar-holidays-initially nil)
  11567. (timestr (format-time-string
  11568. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  11569. (prompt (concat (if prompt (concat prompt " ") "")
  11570. (format "Date+time [%s]: " timestr)))
  11571. ans (org-ans0 "") org-ans1 org-ans2 final)
  11572. (cond
  11573. (from-string (setq ans from-string))
  11574. (org-read-date-popup-calendar
  11575. (save-excursion
  11576. (save-window-excursion
  11577. (calendar)
  11578. (calendar-forward-day (- (time-to-days def)
  11579. (calendar-absolute-from-gregorian
  11580. (calendar-current-date))))
  11581. (org-eval-in-calendar nil t)
  11582. (let* ((old-map (current-local-map))
  11583. (map (copy-keymap calendar-mode-map))
  11584. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11585. (org-defkey map (kbd "RET") 'org-calendar-select)
  11586. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11587. 'org-calendar-select-mouse)
  11588. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11589. 'org-calendar-select-mouse)
  11590. (org-defkey minibuffer-local-map [(meta shift left)]
  11591. (lambda () (interactive)
  11592. (org-eval-in-calendar '(calendar-backward-month 1))))
  11593. (org-defkey minibuffer-local-map [(meta shift right)]
  11594. (lambda () (interactive)
  11595. (org-eval-in-calendar '(calendar-forward-month 1))))
  11596. (org-defkey minibuffer-local-map [(meta shift up)]
  11597. (lambda () (interactive)
  11598. (org-eval-in-calendar '(calendar-backward-year 1))))
  11599. (org-defkey minibuffer-local-map [(meta shift down)]
  11600. (lambda () (interactive)
  11601. (org-eval-in-calendar '(calendar-forward-year 1))))
  11602. (org-defkey minibuffer-local-map [?\e (shift left)]
  11603. (lambda () (interactive)
  11604. (org-eval-in-calendar '(calendar-backward-month 1))))
  11605. (org-defkey minibuffer-local-map [?\e (shift right)]
  11606. (lambda () (interactive)
  11607. (org-eval-in-calendar '(calendar-forward-month 1))))
  11608. (org-defkey minibuffer-local-map [?\e (shift up)]
  11609. (lambda () (interactive)
  11610. (org-eval-in-calendar '(calendar-backward-year 1))))
  11611. (org-defkey minibuffer-local-map [?\e (shift down)]
  11612. (lambda () (interactive)
  11613. (org-eval-in-calendar '(calendar-forward-year 1))))
  11614. (org-defkey minibuffer-local-map [(shift up)]
  11615. (lambda () (interactive)
  11616. (org-eval-in-calendar '(calendar-backward-week 1))))
  11617. (org-defkey minibuffer-local-map [(shift down)]
  11618. (lambda () (interactive)
  11619. (org-eval-in-calendar '(calendar-forward-week 1))))
  11620. (org-defkey minibuffer-local-map [(shift left)]
  11621. (lambda () (interactive)
  11622. (org-eval-in-calendar '(calendar-backward-day 1))))
  11623. (org-defkey minibuffer-local-map [(shift right)]
  11624. (lambda () (interactive)
  11625. (org-eval-in-calendar '(calendar-forward-day 1))))
  11626. (org-defkey minibuffer-local-map ">"
  11627. (lambda () (interactive)
  11628. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11629. (org-defkey minibuffer-local-map "<"
  11630. (lambda () (interactive)
  11631. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11632. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11633. (unwind-protect
  11634. (progn
  11635. (use-local-map map)
  11636. (add-hook 'post-command-hook 'org-read-date-display)
  11637. (setq org-ans0 (read-string prompt default-input
  11638. 'org-read-date-history nil))
  11639. ;; org-ans0: from prompt
  11640. ;; org-ans1: from mouse click
  11641. ;; org-ans2: from calendar motion
  11642. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11643. (remove-hook 'post-command-hook 'org-read-date-display)
  11644. (use-local-map old-map)
  11645. (when org-read-date-overlay
  11646. (org-delete-overlay org-read-date-overlay)
  11647. (setq org-read-date-overlay nil)))))))
  11648. (t ; Naked prompt only
  11649. (unwind-protect
  11650. (setq ans (read-string prompt default-input
  11651. 'org-read-date-history timestr))
  11652. (when org-read-date-overlay
  11653. (org-delete-overlay org-read-date-overlay)
  11654. (setq org-read-date-overlay nil)))))
  11655. (setq final (org-read-date-analyze ans def defdecode))
  11656. (setq org-read-date-final-answer ans)
  11657. (if to-time
  11658. (apply 'encode-time final)
  11659. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11660. (format "%04d-%02d-%02d %02d:%02d"
  11661. (nth 5 final) (nth 4 final) (nth 3 final)
  11662. (nth 2 final) (nth 1 final))
  11663. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11664. (defvar def)
  11665. (defvar defdecode)
  11666. (defvar with-time)
  11667. (defvar org-read-date-analyze-futurep nil)
  11668. (defun org-read-date-display ()
  11669. "Display the current date prompt interpretation in the minibuffer."
  11670. (when org-read-date-display-live
  11671. (when org-read-date-overlay
  11672. (org-delete-overlay org-read-date-overlay))
  11673. (let ((p (point)))
  11674. (end-of-line 1)
  11675. (while (not (equal (buffer-substring
  11676. (max (point-min) (- (point) 4)) (point))
  11677. " "))
  11678. (insert " "))
  11679. (goto-char p))
  11680. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11681. " " (or org-ans1 org-ans2)))
  11682. (org-end-time-was-given nil)
  11683. (f (org-read-date-analyze ans def defdecode))
  11684. (fmts (if org-dcst
  11685. org-time-stamp-custom-formats
  11686. org-time-stamp-formats))
  11687. (fmt (if (or with-time
  11688. (and (boundp 'org-time-was-given) org-time-was-given))
  11689. (cdr fmts)
  11690. (car fmts)))
  11691. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11692. (when (and org-end-time-was-given
  11693. (string-match org-plain-time-of-day-regexp txt))
  11694. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11695. org-end-time-was-given
  11696. (substring txt (match-end 0)))))
  11697. (when org-read-date-analyze-futurep
  11698. (setq txt (concat txt " (=>F)")))
  11699. (setq org-read-date-overlay
  11700. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11701. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11702. (defun org-read-date-analyze (ans def defdecode)
  11703. "Analyse the combined answer of the date prompt."
  11704. ;; FIXME: cleanup and comment
  11705. (let (delta deltan deltaw deltadef year month day
  11706. hour minute second wday pm h2 m2 tl wday1
  11707. iso-year iso-weekday iso-week iso-year iso-date futurep)
  11708. (setq org-read-date-analyze-futurep nil)
  11709. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11710. (setq ans "+0"))
  11711. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11712. (setq ans (replace-match "" t t ans)
  11713. deltan (car delta)
  11714. deltaw (nth 1 delta)
  11715. deltadef (nth 2 delta)))
  11716. ;; Check if there is an iso week date in there
  11717. ;; If yes, sore the info and postpone interpreting it until the rest
  11718. ;; of the parsing is done
  11719. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11720. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11721. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11722. iso-week (string-to-number (match-string 2 ans)))
  11723. (setq ans (replace-match "" t t ans)))
  11724. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11725. (when (string-match
  11726. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11727. (setq year (if (match-end 2)
  11728. (string-to-number (match-string 2 ans))
  11729. (string-to-number (format-time-string "%Y")))
  11730. month (string-to-number (match-string 3 ans))
  11731. day (string-to-number (match-string 4 ans)))
  11732. (if (< year 100) (setq year (+ 2000 year)))
  11733. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11734. t nil ans)))
  11735. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11736. ;; If there is a time with am/pm, and *no* time without it, we convert
  11737. ;; so that matching will be successful.
  11738. (loop for i from 1 to 2 do ; twice, for end time as well
  11739. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11740. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11741. (setq hour (string-to-number (match-string 1 ans))
  11742. minute (if (match-end 3)
  11743. (string-to-number (match-string 3 ans))
  11744. 0)
  11745. pm (equal ?p
  11746. (string-to-char (downcase (match-string 4 ans)))))
  11747. (if (and (= hour 12) (not pm))
  11748. (setq hour 0)
  11749. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11750. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11751. t t ans))))
  11752. ;; Check if a time range is given as a duration
  11753. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11754. (setq hour (string-to-number (match-string 1 ans))
  11755. h2 (+ hour (string-to-number (match-string 3 ans)))
  11756. minute (string-to-number (match-string 2 ans))
  11757. m2 (+ minute (if (match-end 5) (string-to-number
  11758. (match-string 5 ans))0)))
  11759. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11760. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11761. t t ans)))
  11762. ;; Check if there is a time range
  11763. (when (boundp 'org-end-time-was-given)
  11764. (setq org-time-was-given nil)
  11765. (when (and (string-match org-plain-time-of-day-regexp ans)
  11766. (match-end 8))
  11767. (setq org-end-time-was-given (match-string 8 ans))
  11768. (setq ans (concat (substring ans 0 (match-beginning 7))
  11769. (substring ans (match-end 7))))))
  11770. (setq tl (parse-time-string ans)
  11771. day (or (nth 3 tl) (nth 3 defdecode))
  11772. month (or (nth 4 tl)
  11773. (if (and org-read-date-prefer-future
  11774. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11775. (prog1 (1+ (nth 4 defdecode)) (setq futurep t))
  11776. (nth 4 defdecode)))
  11777. year (or (nth 5 tl)
  11778. (if (and org-read-date-prefer-future
  11779. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11780. (prog1 (1+ (nth 5 defdecode)) (setq futurep t))
  11781. (nth 5 defdecode)))
  11782. hour (or (nth 2 tl) (nth 2 defdecode))
  11783. minute (or (nth 1 tl) (nth 1 defdecode))
  11784. second (or (nth 0 tl) 0)
  11785. wday (nth 6 tl))
  11786. (when (and (eq org-read-date-prefer-future 'time)
  11787. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  11788. (equal day (nth 3 defdecode))
  11789. (equal month (nth 4 defdecode))
  11790. (equal year (nth 5 defdecode))
  11791. (nth 2 tl)
  11792. (or (< (nth 2 tl) (nth 2 defdecode))
  11793. (and (= (nth 2 tl) (nth 2 defdecode))
  11794. (nth 1 tl)
  11795. (< (nth 1 tl) (nth 1 defdecode)))))
  11796. (setq day (1+ day)
  11797. futurep t))
  11798. ;; Special date definitions below
  11799. (cond
  11800. (iso-week
  11801. ;; There was an iso week
  11802. (setq futurep nil)
  11803. (setq year (or iso-year year)
  11804. day (or iso-weekday wday 1)
  11805. wday nil ; to make sure that the trigger below does not match
  11806. iso-date (calendar-gregorian-from-absolute
  11807. (calendar-absolute-from-iso
  11808. (list iso-week day year))))
  11809. ; FIXME: Should we also push ISO weeks into the future?
  11810. ; (when (and org-read-date-prefer-future
  11811. ; (not iso-year)
  11812. ; (< (calendar-absolute-from-gregorian iso-date)
  11813. ; (time-to-days (current-time))))
  11814. ; (setq year (1+ year)
  11815. ; iso-date (calendar-gregorian-from-absolute
  11816. ; (calendar-absolute-from-iso
  11817. ; (list iso-week day year)))))
  11818. (setq month (car iso-date)
  11819. year (nth 2 iso-date)
  11820. day (nth 1 iso-date)))
  11821. (deltan
  11822. (setq futurep nil)
  11823. (unless deltadef
  11824. (let ((now (decode-time (current-time))))
  11825. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11826. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11827. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11828. ((equal deltaw "m") (setq month (+ month deltan)))
  11829. ((equal deltaw "y") (setq year (+ year deltan)))))
  11830. ((and wday (not (nth 3 tl)))
  11831. (setq futurep nil)
  11832. ;; Weekday was given, but no day, so pick that day in the week
  11833. ;; on or after the derived date.
  11834. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11835. (unless (equal wday wday1)
  11836. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11837. (if (and (boundp 'org-time-was-given)
  11838. (nth 2 tl))
  11839. (setq org-time-was-given t))
  11840. (if (< year 100) (setq year (+ 2000 year)))
  11841. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11842. (setq org-read-date-analyze-futurep futurep)
  11843. (list second minute hour day month year)))
  11844. (defvar parse-time-weekdays)
  11845. (defun org-read-date-get-relative (s today default)
  11846. "Check string S for special relative date string.
  11847. TODAY and DEFAULT are internal times, for today and for a default.
  11848. Return shift list (N what def-flag)
  11849. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11850. N is the number of WHATs to shift.
  11851. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11852. the DEFAULT date rather than TODAY."
  11853. (when (and
  11854. (string-match
  11855. (concat
  11856. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11857. "\\([0-9]+\\)?"
  11858. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11859. "\\([ \t]\\|$\\)") s)
  11860. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11861. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11862. (string-to-char (substring (match-string 1 s) -1))
  11863. ?+))
  11864. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11865. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11866. (what (if (match-end 3) (match-string 3 s) "d"))
  11867. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11868. (date (if rel default today))
  11869. (wday (nth 6 (decode-time date)))
  11870. delta)
  11871. (if wday1
  11872. (progn
  11873. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11874. (if (= dir ?-) (setq delta (- delta 7)))
  11875. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11876. (list delta "d" rel))
  11877. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11878. (defun org-eval-in-calendar (form &optional keepdate)
  11879. "Eval FORM in the calendar window and return to current window.
  11880. Also, store the cursor date in variable org-ans2."
  11881. (let ((sf (selected-frame))
  11882. (sw (selected-window)))
  11883. (select-window (get-buffer-window "*Calendar*" t))
  11884. (eval form)
  11885. (when (and (not keepdate) (calendar-cursor-to-date))
  11886. (let* ((date (calendar-cursor-to-date))
  11887. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11888. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11889. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11890. (select-window sw)
  11891. (org-select-frame-set-input-focus sf)))
  11892. (defun org-calendar-select ()
  11893. "Return to `org-read-date' with the date currently selected.
  11894. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11895. (interactive)
  11896. (when (calendar-cursor-to-date)
  11897. (let* ((date (calendar-cursor-to-date))
  11898. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11899. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11900. (if (active-minibuffer-window) (exit-minibuffer))))
  11901. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11902. "Insert a date stamp for the date given by the internal TIME.
  11903. WITH-HM means, use the stamp format that includes the time of the day.
  11904. INACTIVE means use square brackets instead of angular ones, so that the
  11905. stamp will not contribute to the agenda.
  11906. PRE and POST are optional strings to be inserted before and after the
  11907. stamp.
  11908. The command returns the inserted time stamp."
  11909. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11910. stamp)
  11911. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11912. (insert-before-markers (or pre ""))
  11913. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11914. (when (listp extra)
  11915. (setq extra (car extra))
  11916. (if (and (stringp extra)
  11917. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11918. (setq extra (format "-%02d:%02d"
  11919. (string-to-number (match-string 1 extra))
  11920. (string-to-number (match-string 2 extra))))
  11921. (setq extra nil)))
  11922. (when extra
  11923. (backward-char 1)
  11924. (insert-before-markers extra)
  11925. (forward-char 1))
  11926. (insert-before-markers (or post ""))
  11927. (setq org-last-inserted-timestamp stamp)))
  11928. (defun org-toggle-time-stamp-overlays ()
  11929. "Toggle the use of custom time stamp formats."
  11930. (interactive)
  11931. (setq org-display-custom-times (not org-display-custom-times))
  11932. (unless org-display-custom-times
  11933. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11934. (while (setq p (next-single-property-change p 'display))
  11935. (if (and (get-text-property p 'display)
  11936. (eq (get-text-property p 'face) 'org-date))
  11937. (remove-text-properties
  11938. p (setq p (next-single-property-change p 'display))
  11939. '(display t))))
  11940. (set-buffer-modified-p bmp)))
  11941. (if (featurep 'xemacs)
  11942. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11943. (org-restart-font-lock)
  11944. (setq org-table-may-need-update t)
  11945. (if org-display-custom-times
  11946. (message "Time stamps are overlayed with custom format")
  11947. (message "Time stamp overlays removed")))
  11948. (defun org-display-custom-time (beg end)
  11949. "Overlay modified time stamp format over timestamp between BEG and END."
  11950. (let* ((ts (buffer-substring beg end))
  11951. t1 w1 with-hm tf time str w2 (off 0))
  11952. (save-match-data
  11953. (setq t1 (org-parse-time-string ts t))
  11954. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  11955. (setq off (- (match-end 0) (match-beginning 0)))))
  11956. (setq end (- end off))
  11957. (setq w1 (- end beg)
  11958. with-hm (and (nth 1 t1) (nth 2 t1))
  11959. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11960. time (org-fix-decoded-time t1)
  11961. str (org-add-props
  11962. (format-time-string
  11963. (substring tf 1 -1) (apply 'encode-time time))
  11964. nil 'mouse-face 'highlight)
  11965. w2 (length str))
  11966. (if (not (= w2 w1))
  11967. (add-text-properties (1+ beg) (+ 2 beg)
  11968. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11969. (if (featurep 'xemacs)
  11970. (progn
  11971. (put-text-property beg end 'invisible t)
  11972. (put-text-property beg end 'end-glyph (make-glyph str)))
  11973. (put-text-property beg end 'display str))))
  11974. (defun org-translate-time (string)
  11975. "Translate all timestamps in STRING to custom format.
  11976. But do this only if the variable `org-display-custom-times' is set."
  11977. (when org-display-custom-times
  11978. (save-match-data
  11979. (let* ((start 0)
  11980. (re org-ts-regexp-both)
  11981. t1 with-hm inactive tf time str beg end)
  11982. (while (setq start (string-match re string start))
  11983. (setq beg (match-beginning 0)
  11984. end (match-end 0)
  11985. t1 (save-match-data
  11986. (org-parse-time-string (substring string beg end) t))
  11987. with-hm (and (nth 1 t1) (nth 2 t1))
  11988. inactive (equal (substring string beg (1+ beg)) "[")
  11989. tf (funcall (if with-hm 'cdr 'car)
  11990. org-time-stamp-custom-formats)
  11991. time (org-fix-decoded-time t1)
  11992. str (format-time-string
  11993. (concat
  11994. (if inactive "[" "<") (substring tf 1 -1)
  11995. (if inactive "]" ">"))
  11996. (apply 'encode-time time))
  11997. string (replace-match str t t string)
  11998. start (+ start (length str)))))))
  11999. string)
  12000. (defun org-fix-decoded-time (time)
  12001. "Set 0 instead of nil for the first 6 elements of time.
  12002. Don't touch the rest."
  12003. (let ((n 0))
  12004. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  12005. (defun org-days-to-time (timestamp-string)
  12006. "Difference between TIMESTAMP-STRING and now in days."
  12007. (- (time-to-days (org-time-string-to-time timestamp-string))
  12008. (time-to-days (current-time))))
  12009. (defun org-deadline-close (timestamp-string &optional ndays)
  12010. "Is the time in TIMESTAMP-STRING close to the current date?"
  12011. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  12012. (and (< (org-days-to-time timestamp-string) ndays)
  12013. (not (org-entry-is-done-p))))
  12014. (defun org-get-wdays (ts)
  12015. "Get the deadline lead time appropriate for timestring TS."
  12016. (cond
  12017. ((<= org-deadline-warning-days 0)
  12018. ;; 0 or negative, enforce this value no matter what
  12019. (- org-deadline-warning-days))
  12020. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  12021. ;; lead time is specified.
  12022. (floor (* (string-to-number (match-string 1 ts))
  12023. (cdr (assoc (match-string 2 ts)
  12024. '(("d" . 1) ("w" . 7)
  12025. ("m" . 30.4) ("y" . 365.25)))))))
  12026. ;; go for the default.
  12027. (t org-deadline-warning-days)))
  12028. (defun org-calendar-select-mouse (ev)
  12029. "Return to `org-read-date' with the date currently selected.
  12030. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12031. (interactive "e")
  12032. (mouse-set-point ev)
  12033. (when (calendar-cursor-to-date)
  12034. (let* ((date (calendar-cursor-to-date))
  12035. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12036. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12037. (if (active-minibuffer-window) (exit-minibuffer))))
  12038. (defun org-check-deadlines (ndays)
  12039. "Check if there are any deadlines due or past due.
  12040. A deadline is considered due if it happens within `org-deadline-warning-days'
  12041. days from today's date. If the deadline appears in an entry marked DONE,
  12042. it is not shown. The prefix arg NDAYS can be used to test that many
  12043. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  12044. (interactive "P")
  12045. (let* ((org-warn-days
  12046. (cond
  12047. ((equal ndays '(4)) 100000)
  12048. (ndays (prefix-numeric-value ndays))
  12049. (t (abs org-deadline-warning-days))))
  12050. (case-fold-search nil)
  12051. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  12052. (callback
  12053. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  12054. (message "%d deadlines past-due or due within %d days"
  12055. (org-occur regexp nil callback)
  12056. org-warn-days)))
  12057. (defun org-check-before-date (date)
  12058. "Check if there are deadlines or scheduled entries before DATE."
  12059. (interactive (list (org-read-date)))
  12060. (let ((case-fold-search nil)
  12061. (regexp (concat "\\<\\(" org-deadline-string
  12062. "\\|" org-scheduled-string
  12063. "\\) *<\\([^>]+\\)>"))
  12064. (callback
  12065. (lambda () (time-less-p
  12066. (org-time-string-to-time (match-string 2))
  12067. (org-time-string-to-time date)))))
  12068. (message "%d entries before %s"
  12069. (org-occur regexp nil callback) date)))
  12070. (defun org-check-after-date (date)
  12071. "Check if there are deadlines or scheduled entries after DATE."
  12072. (interactive (list (org-read-date)))
  12073. (let ((case-fold-search nil)
  12074. (regexp (concat "\\<\\(" org-deadline-string
  12075. "\\|" org-scheduled-string
  12076. "\\) *<\\([^>]+\\)>"))
  12077. (callback
  12078. (lambda () (not
  12079. (time-less-p
  12080. (org-time-string-to-time (match-string 2))
  12081. (org-time-string-to-time date))))))
  12082. (message "%d entries after %s"
  12083. (org-occur regexp nil callback) date)))
  12084. (defun org-evaluate-time-range (&optional to-buffer)
  12085. "Evaluate a time range by computing the difference between start and end.
  12086. Normally the result is just printed in the echo area, but with prefix arg
  12087. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  12088. If the time range is actually in a table, the result is inserted into the
  12089. next column.
  12090. For time difference computation, a year is assumed to be exactly 365
  12091. days in order to avoid rounding problems."
  12092. (interactive "P")
  12093. (or
  12094. (org-clock-update-time-maybe)
  12095. (save-excursion
  12096. (unless (org-at-date-range-p t)
  12097. (goto-char (point-at-bol))
  12098. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  12099. (if (not (org-at-date-range-p t))
  12100. (error "Not at a time-stamp range, and none found in current line")))
  12101. (let* ((ts1 (match-string 1))
  12102. (ts2 (match-string 2))
  12103. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  12104. (match-end (match-end 0))
  12105. (time1 (org-time-string-to-time ts1))
  12106. (time2 (org-time-string-to-time ts2))
  12107. (t1 (org-float-time time1))
  12108. (t2 (org-float-time time2))
  12109. (diff (abs (- t2 t1)))
  12110. (negative (< (- t2 t1) 0))
  12111. ;; (ys (floor (* 365 24 60 60)))
  12112. (ds (* 24 60 60))
  12113. (hs (* 60 60))
  12114. (fy "%dy %dd %02d:%02d")
  12115. (fy1 "%dy %dd")
  12116. (fd "%dd %02d:%02d")
  12117. (fd1 "%dd")
  12118. (fh "%02d:%02d")
  12119. y d h m align)
  12120. (if havetime
  12121. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12122. y 0
  12123. d (floor (/ diff ds)) diff (mod diff ds)
  12124. h (floor (/ diff hs)) diff (mod diff hs)
  12125. m (floor (/ diff 60)))
  12126. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12127. y 0
  12128. d (floor (+ (/ diff ds) 0.5))
  12129. h 0 m 0))
  12130. (if (not to-buffer)
  12131. (message "%s" (org-make-tdiff-string y d h m))
  12132. (if (org-at-table-p)
  12133. (progn
  12134. (goto-char match-end)
  12135. (setq align t)
  12136. (and (looking-at " *|") (goto-char (match-end 0))))
  12137. (goto-char match-end))
  12138. (if (looking-at
  12139. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  12140. (replace-match ""))
  12141. (if negative (insert " -"))
  12142. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  12143. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  12144. (insert " " (format fh h m))))
  12145. (if align (org-table-align))
  12146. (message "Time difference inserted")))))
  12147. (defun org-make-tdiff-string (y d h m)
  12148. (let ((fmt "")
  12149. (l nil))
  12150. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  12151. l (push y l)))
  12152. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  12153. l (push d l)))
  12154. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  12155. l (push h l)))
  12156. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  12157. l (push m l)))
  12158. (apply 'format fmt (nreverse l))))
  12159. (defun org-time-string-to-time (s)
  12160. (apply 'encode-time (org-parse-time-string s)))
  12161. (defun org-time-string-to-seconds (s)
  12162. (org-float-time (org-time-string-to-time s)))
  12163. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  12164. "Convert a time stamp to an absolute day number.
  12165. If there is a specifyer for a cyclic time stamp, get the closest date to
  12166. DAYNR.
  12167. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  12168. the variable date is bound by the calendar when this is called."
  12169. (cond
  12170. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  12171. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  12172. daynr
  12173. (+ daynr 1000)))
  12174. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  12175. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  12176. (time-to-days (current-time))) (match-string 0 s)
  12177. prefer show-all))
  12178. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  12179. (defun org-days-to-iso-week (days)
  12180. "Return the iso week number."
  12181. (require 'cal-iso)
  12182. (car (calendar-iso-from-absolute days)))
  12183. (defun org-small-year-to-year (year)
  12184. "Convert 2-digit years into 4-digit years.
  12185. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  12186. The year 2000 cannot be abbreviated. Any year larger than 99
  12187. is returned unchanged."
  12188. (if (< year 38)
  12189. (setq year (+ 2000 year))
  12190. (if (< year 100)
  12191. (setq year (+ 1900 year))))
  12192. year)
  12193. (defun org-time-from-absolute (d)
  12194. "Return the time corresponding to date D.
  12195. D may be an absolute day number, or a calendar-type list (month day year)."
  12196. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  12197. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  12198. (defun org-calendar-holiday ()
  12199. "List of holidays, for Diary display in Org-mode."
  12200. (require 'holidays)
  12201. (let ((hl (funcall
  12202. (if (fboundp 'calendar-check-holidays)
  12203. 'calendar-check-holidays 'check-calendar-holidays) date)))
  12204. (if hl (mapconcat 'identity hl "; "))))
  12205. (defun org-diary-sexp-entry (sexp entry date)
  12206. "Process a SEXP diary ENTRY for DATE."
  12207. (require 'diary-lib)
  12208. (let ((result (if calendar-debug-sexp
  12209. (let ((stack-trace-on-error t))
  12210. (eval (car (read-from-string sexp))))
  12211. (condition-case nil
  12212. (eval (car (read-from-string sexp)))
  12213. (error
  12214. (beep)
  12215. (message "Bad sexp at line %d in %s: %s"
  12216. (org-current-line)
  12217. (buffer-file-name) sexp)
  12218. (sleep-for 2))))))
  12219. (cond ((stringp result) result)
  12220. ((and (consp result)
  12221. (stringp (cdr result))) (cdr result))
  12222. (result entry)
  12223. (t nil))))
  12224. (defun org-diary-to-ical-string (frombuf)
  12225. "Get iCalendar entries from diary entries in buffer FROMBUF.
  12226. This uses the icalendar.el library."
  12227. (let* ((tmpdir (if (featurep 'xemacs)
  12228. (temp-directory)
  12229. temporary-file-directory))
  12230. (tmpfile (make-temp-name
  12231. (expand-file-name "orgics" tmpdir)))
  12232. buf rtn b e)
  12233. (save-excursion
  12234. (set-buffer frombuf)
  12235. (icalendar-export-region (point-min) (point-max) tmpfile)
  12236. (setq buf (find-buffer-visiting tmpfile))
  12237. (set-buffer buf)
  12238. (goto-char (point-min))
  12239. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  12240. (setq b (match-beginning 0)))
  12241. (goto-char (point-max))
  12242. (if (re-search-backward "^END:VEVENT" nil t)
  12243. (setq e (match-end 0)))
  12244. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  12245. (kill-buffer buf)
  12246. (delete-file tmpfile)
  12247. rtn))
  12248. (defun org-closest-date (start current change prefer show-all)
  12249. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  12250. When PREFER is `past' return a date that is either CURRENT or past.
  12251. When PREFER is `future', return a date that is either CURRENT or future.
  12252. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  12253. ;; Make the proper lists from the dates
  12254. (catch 'exit
  12255. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  12256. dn dw sday cday n1 n2 n0
  12257. d m y y1 y2 date1 date2 nmonths nm ny m2)
  12258. (setq start (org-date-to-gregorian start)
  12259. current (org-date-to-gregorian
  12260. (if show-all
  12261. current
  12262. (time-to-days (current-time))))
  12263. sday (calendar-absolute-from-gregorian start)
  12264. cday (calendar-absolute-from-gregorian current))
  12265. (if (<= cday sday) (throw 'exit sday))
  12266. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  12267. (setq dn (string-to-number (match-string 1 change))
  12268. dw (cdr (assoc (match-string 2 change) a1)))
  12269. (error "Invalid change specifyer: %s" change))
  12270. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  12271. (cond
  12272. ((eq dw 'day)
  12273. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  12274. n2 (+ n1 dn)))
  12275. ((eq dw 'year)
  12276. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  12277. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  12278. (setq date1 (list m d y1)
  12279. n1 (calendar-absolute-from-gregorian date1)
  12280. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  12281. n2 (calendar-absolute-from-gregorian date2)))
  12282. ((eq dw 'month)
  12283. ;; approx number of month between the two dates
  12284. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  12285. ;; How often does dn fit in there?
  12286. (setq d (nth 1 start) m (car start) y (nth 2 start)
  12287. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  12288. m (+ m nm)
  12289. ny (floor (/ m 12))
  12290. y (+ y ny)
  12291. m (- m (* ny 12)))
  12292. (while (> m 12) (setq m (- m 12) y (1+ y)))
  12293. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  12294. (setq m2 (+ m dn) y2 y)
  12295. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12296. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  12297. (while (<= n2 cday)
  12298. (setq n1 n2 m m2 y y2)
  12299. (setq m2 (+ m dn) y2 y)
  12300. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12301. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  12302. ;; Make sure n1 is the earlier date
  12303. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  12304. (if show-all
  12305. (cond
  12306. ((eq prefer 'past) (if (= cday n2) n2 n1))
  12307. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12308. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  12309. (cond
  12310. ((eq prefer 'past) (if (= cday n2) n2 n1))
  12311. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12312. (t (if (= cday n1) n1 n2)))))))
  12313. (defun org-date-to-gregorian (date)
  12314. "Turn any specification of DATE into a gregorian date for the calendar."
  12315. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  12316. ((and (listp date) (= (length date) 3)) date)
  12317. ((stringp date)
  12318. (setq date (org-parse-time-string date))
  12319. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  12320. ((listp date)
  12321. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  12322. (defun org-parse-time-string (s &optional nodefault)
  12323. "Parse the standard Org-mode time string.
  12324. This should be a lot faster than the normal `parse-time-string'.
  12325. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  12326. hour and minute fields will be nil if not given."
  12327. (if (string-match org-ts-regexp0 s)
  12328. (list 0
  12329. (if (or (match-beginning 8) (not nodefault))
  12330. (string-to-number (or (match-string 8 s) "0")))
  12331. (if (or (match-beginning 7) (not nodefault))
  12332. (string-to-number (or (match-string 7 s) "0")))
  12333. (string-to-number (match-string 4 s))
  12334. (string-to-number (match-string 3 s))
  12335. (string-to-number (match-string 2 s))
  12336. nil nil nil)
  12337. (error "Not a standard Org-mode time string: %s" s)))
  12338. (defun org-timestamp-up (&optional arg)
  12339. "Increase the date item at the cursor by one.
  12340. If the cursor is on the year, change the year. If it is on the month or
  12341. the day, change that.
  12342. With prefix ARG, change by that many units."
  12343. (interactive "p")
  12344. (org-timestamp-change (prefix-numeric-value arg)))
  12345. (defun org-timestamp-down (&optional arg)
  12346. "Decrease the date item at the cursor by one.
  12347. If the cursor is on the year, change the year. If it is on the month or
  12348. the day, change that.
  12349. With prefix ARG, change by that many units."
  12350. (interactive "p")
  12351. (org-timestamp-change (- (prefix-numeric-value arg))))
  12352. (defun org-timestamp-up-day (&optional arg)
  12353. "Increase the date in the time stamp by one day.
  12354. With prefix ARG, change that many days."
  12355. (interactive "p")
  12356. (if (and (not (org-at-timestamp-p t))
  12357. (org-on-heading-p))
  12358. (org-todo 'up)
  12359. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  12360. (defun org-timestamp-down-day (&optional arg)
  12361. "Decrease the date in the time stamp by one day.
  12362. With prefix ARG, change that many days."
  12363. (interactive "p")
  12364. (if (and (not (org-at-timestamp-p t))
  12365. (org-on-heading-p))
  12366. (org-todo 'down)
  12367. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  12368. (defun org-at-timestamp-p (&optional inactive-ok)
  12369. "Determine if the cursor is in or at a timestamp."
  12370. (interactive)
  12371. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  12372. (pos (point))
  12373. (ans (or (looking-at tsr)
  12374. (save-excursion
  12375. (skip-chars-backward "^[<\n\r\t")
  12376. (if (> (point) (point-min)) (backward-char 1))
  12377. (and (looking-at tsr)
  12378. (> (- (match-end 0) pos) -1))))))
  12379. (and ans
  12380. (boundp 'org-ts-what)
  12381. (setq org-ts-what
  12382. (cond
  12383. ((= pos (match-beginning 0)) 'bracket)
  12384. ((= pos (1- (match-end 0))) 'bracket)
  12385. ((org-pos-in-match-range pos 2) 'year)
  12386. ((org-pos-in-match-range pos 3) 'month)
  12387. ((org-pos-in-match-range pos 7) 'hour)
  12388. ((org-pos-in-match-range pos 8) 'minute)
  12389. ((or (org-pos-in-match-range pos 4)
  12390. (org-pos-in-match-range pos 5)) 'day)
  12391. ((and (> pos (or (match-end 8) (match-end 5)))
  12392. (< pos (match-end 0)))
  12393. (- pos (or (match-end 8) (match-end 5))))
  12394. (t 'day))))
  12395. ans))
  12396. (defun org-toggle-timestamp-type ()
  12397. "Toggle the type (<active> or [inactive]) of a time stamp."
  12398. (interactive)
  12399. (when (org-at-timestamp-p t)
  12400. (let ((beg (match-beginning 0)) (end (match-end 0))
  12401. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  12402. (save-excursion
  12403. (goto-char beg)
  12404. (while (re-search-forward "[][<>]" end t)
  12405. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  12406. t t)))
  12407. (message "Timestamp is now %sactive"
  12408. (if (equal (char-after beg) ?<) "" "in")))))
  12409. (defun org-timestamp-change (n &optional what)
  12410. "Change the date in the time stamp at point.
  12411. The date will be changed by N times WHAT. WHAT can be `day', `month',
  12412. `year', `minute', `second'. If WHAT is not given, the cursor position
  12413. in the timestamp determines what will be changed."
  12414. (let ((pos (point))
  12415. with-hm inactive
  12416. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  12417. org-ts-what
  12418. extra rem
  12419. ts time time0)
  12420. (if (not (org-at-timestamp-p t))
  12421. (error "Not at a timestamp"))
  12422. (if (and (not what) (eq org-ts-what 'bracket))
  12423. (org-toggle-timestamp-type)
  12424. (if (and (not what) (not (eq org-ts-what 'day))
  12425. org-display-custom-times
  12426. (get-text-property (point) 'display)
  12427. (not (get-text-property (1- (point)) 'display)))
  12428. (setq org-ts-what 'day))
  12429. (setq org-ts-what (or what org-ts-what)
  12430. inactive (= (char-after (match-beginning 0)) ?\[)
  12431. ts (match-string 0))
  12432. (replace-match "")
  12433. (if (string-match
  12434. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  12435. ts)
  12436. (setq extra (match-string 1 ts)))
  12437. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  12438. (setq with-hm t))
  12439. (setq time0 (org-parse-time-string ts))
  12440. (when (and (eq org-ts-what 'minute)
  12441. (eq current-prefix-arg nil))
  12442. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  12443. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  12444. (setcar (cdr time0) (+ (nth 1 time0)
  12445. (if (> n 0) (- rem) (- dm rem))))))
  12446. (setq time
  12447. (encode-time (or (car time0) 0)
  12448. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  12449. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  12450. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  12451. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  12452. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  12453. (nthcdr 6 time0)))
  12454. (when (and (member org-ts-what '(hour minute))
  12455. extra
  12456. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  12457. (setq extra (org-modify-ts-extra
  12458. extra
  12459. (if (eq org-ts-what 'hour) 2 5)
  12460. n dm)))
  12461. (when (integerp org-ts-what)
  12462. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  12463. (if (eq what 'calendar)
  12464. (let ((cal-date (org-get-date-from-calendar)))
  12465. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  12466. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  12467. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  12468. (setcar time0 (or (car time0) 0))
  12469. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  12470. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  12471. (setq time (apply 'encode-time time0))))
  12472. (setq org-last-changed-timestamp
  12473. (org-insert-time-stamp time with-hm inactive nil nil extra))
  12474. (org-clock-update-time-maybe)
  12475. (goto-char pos)
  12476. ;; Try to recenter the calendar window, if any
  12477. (if (and org-calendar-follow-timestamp-change
  12478. (get-buffer-window "*Calendar*" t)
  12479. (memq org-ts-what '(day month year)))
  12480. (org-recenter-calendar (time-to-days time))))))
  12481. (defun org-modify-ts-extra (s pos n dm)
  12482. "Change the different parts of the lead-time and repeat fields in timestamp."
  12483. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  12484. ng h m new rem)
  12485. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  12486. (cond
  12487. ((or (org-pos-in-match-range pos 2)
  12488. (org-pos-in-match-range pos 3))
  12489. (setq m (string-to-number (match-string 3 s))
  12490. h (string-to-number (match-string 2 s)))
  12491. (if (org-pos-in-match-range pos 2)
  12492. (setq h (+ h n))
  12493. (setq n (* dm (org-no-warnings (signum n))))
  12494. (when (not (= 0 (setq rem (% m dm))))
  12495. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  12496. (setq m (+ m n)))
  12497. (if (< m 0) (setq m (+ m 60) h (1- h)))
  12498. (if (> m 59) (setq m (- m 60) h (1+ h)))
  12499. (setq h (min 24 (max 0 h)))
  12500. (setq ng 1 new (format "-%02d:%02d" h m)))
  12501. ((org-pos-in-match-range pos 6)
  12502. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  12503. ((org-pos-in-match-range pos 5)
  12504. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  12505. ((org-pos-in-match-range pos 9)
  12506. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  12507. ((org-pos-in-match-range pos 8)
  12508. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  12509. (when ng
  12510. (setq s (concat
  12511. (substring s 0 (match-beginning ng))
  12512. new
  12513. (substring s (match-end ng))))))
  12514. s))
  12515. (defun org-recenter-calendar (date)
  12516. "If the calendar is visible, recenter it to DATE."
  12517. (let* ((win (selected-window))
  12518. (cwin (get-buffer-window "*Calendar*" t))
  12519. (calendar-move-hook nil))
  12520. (when cwin
  12521. (select-window cwin)
  12522. (calendar-goto-date (if (listp date) date
  12523. (calendar-gregorian-from-absolute date)))
  12524. (select-window win))))
  12525. (defun org-goto-calendar (&optional arg)
  12526. "Go to the Emacs calendar at the current date.
  12527. If there is a time stamp in the current line, go to that date.
  12528. A prefix ARG can be used to force the current date."
  12529. (interactive "P")
  12530. (let ((tsr org-ts-regexp) diff
  12531. (calendar-move-hook nil)
  12532. (calendar-view-holidays-initially-flag nil)
  12533. (view-calendar-holidays-initially nil)
  12534. (calendar-view-diary-initially-flag nil)
  12535. (view-diary-entries-initially nil))
  12536. (if (or (org-at-timestamp-p)
  12537. (save-excursion
  12538. (beginning-of-line 1)
  12539. (looking-at (concat ".*" tsr))))
  12540. (let ((d1 (time-to-days (current-time)))
  12541. (d2 (time-to-days
  12542. (org-time-string-to-time (match-string 1)))))
  12543. (setq diff (- d2 d1))))
  12544. (calendar)
  12545. (calendar-goto-today)
  12546. (if (and diff (not arg)) (calendar-forward-day diff))))
  12547. (defun org-get-date-from-calendar ()
  12548. "Return a list (month day year) of date at point in calendar."
  12549. (with-current-buffer "*Calendar*"
  12550. (save-match-data
  12551. (calendar-cursor-to-date))))
  12552. (defun org-date-from-calendar ()
  12553. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  12554. If there is already a time stamp at the cursor position, update it."
  12555. (interactive)
  12556. (if (org-at-timestamp-p t)
  12557. (org-timestamp-change 0 'calendar)
  12558. (let ((cal-date (org-get-date-from-calendar)))
  12559. (org-insert-time-stamp
  12560. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  12561. (defun org-minutes-to-hh:mm-string (m)
  12562. "Compute H:MM from a number of minutes."
  12563. (let ((h (/ m 60)))
  12564. (setq m (- m (* 60 h)))
  12565. (format org-time-clocksum-format h m)))
  12566. (defun org-hh:mm-string-to-minutes (s)
  12567. "Convert a string H:MM to a number of minutes.
  12568. If the string is just a number, interprete it as minutes.
  12569. In fact, the first hh:mm or number in the string will be taken,
  12570. there can be extra stuff in the string.
  12571. If no number is found, the return value is 0."
  12572. (cond
  12573. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  12574. (+ (* (string-to-number (match-string 1 s)) 60)
  12575. (string-to-number (match-string 2 s))))
  12576. ((string-match "\\([0-9]+\\)" s)
  12577. (string-to-number (match-string 1 s)))
  12578. (t 0)))
  12579. ;;;; Files
  12580. (defun org-save-all-org-buffers ()
  12581. "Save all Org-mode buffers without user confirmation."
  12582. (interactive)
  12583. (message "Saving all Org-mode buffers...")
  12584. (save-some-buffers t 'org-mode-p)
  12585. (when (featurep 'org-id) (org-id-locations-save))
  12586. (message "Saving all Org-mode buffers... done"))
  12587. (defun org-revert-all-org-buffers ()
  12588. "Revert all Org-mode buffers.
  12589. Prompt for confirmation when there are unsaved changes.
  12590. Be sure you know what you are doing before letting this function
  12591. overwrite your changes.
  12592. This function is useful in a setup where one tracks org files
  12593. with a version control system, to revert on one machine after pulling
  12594. changes from another. I believe the procedure must be like this:
  12595. 1. M-x org-save-all-org-buffers
  12596. 2. Pull changes from the other machine, resolve conflicts
  12597. 3. M-x org-revert-all-org-buffers"
  12598. (interactive)
  12599. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  12600. (error "Abort"))
  12601. (save-excursion
  12602. (save-window-excursion
  12603. (mapc
  12604. (lambda (b)
  12605. (when (and (with-current-buffer b (org-mode-p))
  12606. (with-current-buffer b buffer-file-name))
  12607. (switch-to-buffer b)
  12608. (revert-buffer t 'no-confirm)))
  12609. (buffer-list))
  12610. (when (and (featurep 'org-id) org-id-track-globally)
  12611. (org-id-locations-load)))))
  12612. ;;;; Agenda files
  12613. ;;;###autoload
  12614. (defun org-iswitchb (&optional arg)
  12615. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  12616. With a prefix argument, restrict available to files.
  12617. With two prefix arguments, restrict available buffers to agenda files."
  12618. (interactive "P")
  12619. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12620. ((equal arg '(16)) (org-buffer-list 'agenda))
  12621. (t (org-buffer-list)))))
  12622. (switch-to-buffer
  12623. (org-icompleting-read "Org buffer: "
  12624. (mapcar 'list (mapcar 'buffer-name blist))
  12625. nil t))))
  12626. ;;;###autoload
  12627. (defalias 'org-ido-switchb 'org-iswitchb)
  12628. (defun org-buffer-list (&optional predicate exclude-tmp)
  12629. "Return a list of Org buffers.
  12630. PREDICATE can be `export', `files' or `agenda'.
  12631. export restrict the list to Export buffers.
  12632. files restrict the list to buffers visiting Org files.
  12633. agenda restrict the list to buffers visiting agenda files.
  12634. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12635. (let* ((bfn nil)
  12636. (agenda-files (and (eq predicate 'agenda)
  12637. (mapcar 'file-truename (org-agenda-files t))))
  12638. (filter
  12639. (cond
  12640. ((eq predicate 'files)
  12641. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12642. ((eq predicate 'export)
  12643. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12644. ((eq predicate 'agenda)
  12645. (lambda (b)
  12646. (with-current-buffer b
  12647. (and (eq major-mode 'org-mode)
  12648. (setq bfn (buffer-file-name b))
  12649. (member (file-truename bfn) agenda-files)))))
  12650. (t (lambda (b) (with-current-buffer b
  12651. (or (eq major-mode 'org-mode)
  12652. (string-match "\*Org .*Export"
  12653. (buffer-name b)))))))))
  12654. (delq nil
  12655. (mapcar
  12656. (lambda(b)
  12657. (if (and (funcall filter b)
  12658. (or (not exclude-tmp)
  12659. (not (string-match "tmp" (buffer-name b)))))
  12660. b
  12661. nil))
  12662. (buffer-list)))))
  12663. (defun org-agenda-files (&optional unrestricted archives)
  12664. "Get the list of agenda files.
  12665. Optional UNRESTRICTED means return the full list even if a restriction
  12666. is currently in place.
  12667. When ARCHIVES is t, include all archive files hat are really being
  12668. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12669. `org-agenda-archives-mode' is t."
  12670. (let ((files
  12671. (cond
  12672. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12673. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12674. ((listp org-agenda-files) org-agenda-files)
  12675. (t (error "Invalid value of `org-agenda-files'")))))
  12676. (setq files (apply 'append
  12677. (mapcar (lambda (f)
  12678. (if (file-directory-p f)
  12679. (directory-files
  12680. f t org-agenda-file-regexp)
  12681. (list f)))
  12682. files)))
  12683. (when org-agenda-skip-unavailable-files
  12684. (setq files (delq nil
  12685. (mapcar (function
  12686. (lambda (file)
  12687. (and (file-readable-p file) file)))
  12688. files))))
  12689. (when (or (eq archives t)
  12690. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12691. (setq files (org-add-archive-files files)))
  12692. files))
  12693. (defun org-edit-agenda-file-list ()
  12694. "Edit the list of agenda files.
  12695. Depending on setup, this either uses customize to edit the variable
  12696. `org-agenda-files', or it visits the file that is holding the list. In the
  12697. latter case, the buffer is set up in a way that saving it automatically kills
  12698. the buffer and restores the previous window configuration."
  12699. (interactive)
  12700. (if (stringp org-agenda-files)
  12701. (let ((cw (current-window-configuration)))
  12702. (find-file org-agenda-files)
  12703. (org-set-local 'org-window-configuration cw)
  12704. (org-add-hook 'after-save-hook
  12705. (lambda ()
  12706. (set-window-configuration
  12707. (prog1 org-window-configuration
  12708. (kill-buffer (current-buffer))))
  12709. (org-install-agenda-files-menu)
  12710. (message "New agenda file list installed"))
  12711. nil 'local)
  12712. (message "%s" (substitute-command-keys
  12713. "Edit list and finish with \\[save-buffer]")))
  12714. (customize-variable 'org-agenda-files)))
  12715. (defun org-store-new-agenda-file-list (list)
  12716. "Set new value for the agenda file list and save it correctly."
  12717. (if (stringp org-agenda-files)
  12718. (let ((f org-agenda-files) b)
  12719. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12720. (with-temp-file f
  12721. (insert (mapconcat 'identity list "\n") "\n")))
  12722. (let ((org-mode-hook nil) (org-inhibit-startup t)
  12723. (org-insert-mode-line-in-empty-file nil))
  12724. (setq org-agenda-files list)
  12725. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12726. (defun org-read-agenda-file-list ()
  12727. "Read the list of agenda files from a file."
  12728. (when (file-directory-p org-agenda-files)
  12729. (error "`org-agenda-files' cannot be a single directory"))
  12730. (when (stringp org-agenda-files)
  12731. (with-temp-buffer
  12732. (insert-file-contents org-agenda-files)
  12733. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12734. ;;;###autoload
  12735. (defun org-cycle-agenda-files ()
  12736. "Cycle through the files in `org-agenda-files'.
  12737. If the current buffer visits an agenda file, find the next one in the list.
  12738. If the current buffer does not, find the first agenda file."
  12739. (interactive)
  12740. (let* ((fs (org-agenda-files t))
  12741. (files (append fs (list (car fs))))
  12742. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12743. file)
  12744. (unless files (error "No agenda files"))
  12745. (catch 'exit
  12746. (while (setq file (pop files))
  12747. (if (equal (file-truename file) tcf)
  12748. (when (car files)
  12749. (find-file (car files))
  12750. (throw 'exit t))))
  12751. (find-file (car fs)))
  12752. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12753. (defun org-agenda-file-to-front (&optional to-end)
  12754. "Move/add the current file to the top of the agenda file list.
  12755. If the file is not present in the list, it is added to the front. If it is
  12756. present, it is moved there. With optional argument TO-END, add/move to the
  12757. end of the list."
  12758. (interactive "P")
  12759. (let ((org-agenda-skip-unavailable-files nil)
  12760. (file-alist (mapcar (lambda (x)
  12761. (cons (file-truename x) x))
  12762. (org-agenda-files t)))
  12763. (ctf (file-truename buffer-file-name))
  12764. x had)
  12765. (setq x (assoc ctf file-alist) had x)
  12766. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12767. (if to-end
  12768. (setq file-alist (append (delq x file-alist) (list x)))
  12769. (setq file-alist (cons x (delq x file-alist))))
  12770. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12771. (org-install-agenda-files-menu)
  12772. (message "File %s to %s of agenda file list"
  12773. (if had "moved" "added") (if to-end "end" "front"))))
  12774. (defun org-remove-file (&optional file)
  12775. "Remove current file from the list of files in variable `org-agenda-files'.
  12776. These are the files which are being checked for agenda entries.
  12777. Optional argument FILE means, use this file instead of the current."
  12778. (interactive)
  12779. (let* ((org-agenda-skip-unavailable-files nil)
  12780. (file (or file buffer-file-name))
  12781. (true-file (file-truename file))
  12782. (afile (abbreviate-file-name file))
  12783. (files (delq nil (mapcar
  12784. (lambda (x)
  12785. (if (equal true-file
  12786. (file-truename x))
  12787. nil x))
  12788. (org-agenda-files t)))))
  12789. (if (not (= (length files) (length (org-agenda-files t))))
  12790. (progn
  12791. (org-store-new-agenda-file-list files)
  12792. (org-install-agenda-files-menu)
  12793. (message "Removed file: %s" afile))
  12794. (message "File was not in list: %s (not removed)" afile))))
  12795. (defun org-file-menu-entry (file)
  12796. (vector file (list 'find-file file) t))
  12797. (defun org-check-agenda-file (file)
  12798. "Make sure FILE exists. If not, ask user what to do."
  12799. (when (not (file-exists-p file))
  12800. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  12801. (abbreviate-file-name file))
  12802. (let ((r (downcase (read-char-exclusive))))
  12803. (cond
  12804. ((equal r ?r)
  12805. (org-remove-file file)
  12806. (throw 'nextfile t))
  12807. (t (error "Abort"))))))
  12808. (defun org-get-agenda-file-buffer (file)
  12809. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12810. it to the list of buffers which might be released later."
  12811. (let ((buf (org-find-base-buffer-visiting file)))
  12812. (if buf
  12813. buf ; just return it
  12814. ;; Make a new buffer and remember it
  12815. (setq buf (find-file-noselect file))
  12816. (if buf (push buf org-agenda-new-buffers))
  12817. buf)))
  12818. (defun org-release-buffers (blist)
  12819. "Release all buffers in list, asking the user for confirmation when needed.
  12820. When a buffer is unmodified, it is just killed. When modified, it is saved
  12821. \(if the user agrees) and then killed."
  12822. (let (buf file)
  12823. (while (setq buf (pop blist))
  12824. (setq file (buffer-file-name buf))
  12825. (when (and (buffer-modified-p buf)
  12826. file
  12827. (y-or-n-p (format "Save file %s? " file)))
  12828. (with-current-buffer buf (save-buffer)))
  12829. (kill-buffer buf))))
  12830. (defun org-prepare-agenda-buffers (files)
  12831. "Create buffers for all agenda files, protect archived trees and comments."
  12832. (interactive)
  12833. (let ((pa '(:org-archived t))
  12834. (pc '(:org-comment t))
  12835. (pall '(:org-archived t :org-comment t))
  12836. (inhibit-read-only t)
  12837. (rea (concat ":" org-archive-tag ":"))
  12838. bmp file re)
  12839. (save-excursion
  12840. (save-restriction
  12841. (while (setq file (pop files))
  12842. (catch 'nextfile
  12843. (if (bufferp file)
  12844. (set-buffer file)
  12845. (org-check-agenda-file file)
  12846. (set-buffer (org-get-agenda-file-buffer file)))
  12847. (widen)
  12848. (setq bmp (buffer-modified-p))
  12849. (org-refresh-category-properties)
  12850. (setq org-todo-keywords-for-agenda
  12851. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12852. (setq org-done-keywords-for-agenda
  12853. (append org-done-keywords-for-agenda org-done-keywords))
  12854. (setq org-todo-keyword-alist-for-agenda
  12855. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12856. (setq org-drawers-for-agenda
  12857. (append org-drawers-for-agenda org-drawers))
  12858. (setq org-tag-alist-for-agenda
  12859. (append org-tag-alist-for-agenda org-tag-alist))
  12860. (save-excursion
  12861. (remove-text-properties (point-min) (point-max) pall)
  12862. (when org-agenda-skip-archived-trees
  12863. (goto-char (point-min))
  12864. (while (re-search-forward rea nil t)
  12865. (if (org-on-heading-p t)
  12866. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12867. (goto-char (point-min))
  12868. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12869. (while (re-search-forward re nil t)
  12870. (add-text-properties
  12871. (match-beginning 0) (org-end-of-subtree t) pc)))
  12872. (set-buffer-modified-p bmp)))))
  12873. (setq org-todo-keyword-alist-for-agenda
  12874. (org-uniquify org-todo-keyword-alist-for-agenda)
  12875. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12876. ;;;; Embedded LaTeX
  12877. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12878. "Keymap for the minor `org-cdlatex-mode'.")
  12879. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12880. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12881. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12882. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12883. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12884. (defvar org-cdlatex-texmathp-advice-is-done nil
  12885. "Flag remembering if we have applied the advice to texmathp already.")
  12886. (define-minor-mode org-cdlatex-mode
  12887. "Toggle the minor `org-cdlatex-mode'.
  12888. This mode supports entering LaTeX environment and math in LaTeX fragments
  12889. in Org-mode.
  12890. \\{org-cdlatex-mode-map}"
  12891. nil " OCDL" nil
  12892. (when org-cdlatex-mode (require 'cdlatex))
  12893. (unless org-cdlatex-texmathp-advice-is-done
  12894. (setq org-cdlatex-texmathp-advice-is-done t)
  12895. (defadvice texmathp (around org-math-always-on activate)
  12896. "Always return t in org-mode buffers.
  12897. This is because we want to insert math symbols without dollars even outside
  12898. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12899. an embedded LaTeX fragment, let texmathp do its job.
  12900. \\[org-cdlatex-mode-map]"
  12901. (interactive)
  12902. (let (p)
  12903. (cond
  12904. ((not (org-mode-p)) ad-do-it)
  12905. ((eq this-command 'cdlatex-math-symbol)
  12906. (setq ad-return-value t
  12907. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12908. (t
  12909. (let ((p (org-inside-LaTeX-fragment-p)))
  12910. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12911. (setq ad-return-value t
  12912. texmathp-why '("Org-mode embedded math" . 0))
  12913. (if p ad-do-it)))))))))
  12914. (defun turn-on-org-cdlatex ()
  12915. "Unconditionally turn on `org-cdlatex-mode'."
  12916. (org-cdlatex-mode 1))
  12917. (defun org-inside-LaTeX-fragment-p ()
  12918. "Test if point is inside a LaTeX fragment.
  12919. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12920. sequence appearing also before point.
  12921. Even though the matchers for math are configurable, this function assumes
  12922. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12923. delimiters are skipped when they have been removed by customization.
  12924. The return value is nil, or a cons cell with the delimiter and
  12925. and the position of this delimiter.
  12926. This function does a reasonably good job, but can locally be fooled by
  12927. for example currency specifications. For example it will assume being in
  12928. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12929. fragments that are properly closed, but during editing, we have to live
  12930. with the uncertainty caused by missing closing delimiters. This function
  12931. looks only before point, not after."
  12932. (catch 'exit
  12933. (let ((pos (point))
  12934. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12935. (lim (progn
  12936. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12937. (point)))
  12938. dd-on str (start 0) m re)
  12939. (goto-char pos)
  12940. (when dodollar
  12941. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12942. re (nth 1 (assoc "$" org-latex-regexps)))
  12943. (while (string-match re str start)
  12944. (cond
  12945. ((= (match-end 0) (length str))
  12946. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12947. ((= (match-end 0) (- (length str) 5))
  12948. (throw 'exit nil))
  12949. (t (setq start (match-end 0))))))
  12950. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12951. (goto-char pos)
  12952. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12953. (and (match-beginning 2) (throw 'exit nil))
  12954. ;; count $$
  12955. (while (re-search-backward "\\$\\$" lim t)
  12956. (setq dd-on (not dd-on)))
  12957. (goto-char pos)
  12958. (if dd-on (cons "$$" m))))))
  12959. (defun org-try-cdlatex-tab ()
  12960. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12961. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12962. - inside a LaTeX fragment, or
  12963. - after the first word in a line, where an abbreviation expansion could
  12964. insert a LaTeX environment."
  12965. (when org-cdlatex-mode
  12966. (cond
  12967. ((save-excursion
  12968. (skip-chars-backward "a-zA-Z0-9*")
  12969. (skip-chars-backward " \t")
  12970. (bolp))
  12971. (cdlatex-tab) t)
  12972. ((org-inside-LaTeX-fragment-p)
  12973. (cdlatex-tab) t)
  12974. (t nil))))
  12975. (defun org-cdlatex-underscore-caret (&optional arg)
  12976. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12977. Revert to the normal definition outside of these fragments."
  12978. (interactive "P")
  12979. (if (org-inside-LaTeX-fragment-p)
  12980. (call-interactively 'cdlatex-sub-superscript)
  12981. (let (org-cdlatex-mode)
  12982. (call-interactively (key-binding (vector last-input-event))))))
  12983. (defun org-cdlatex-math-modify (&optional arg)
  12984. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12985. Revert to the normal definition outside of these fragments."
  12986. (interactive "P")
  12987. (if (org-inside-LaTeX-fragment-p)
  12988. (call-interactively 'cdlatex-math-modify)
  12989. (let (org-cdlatex-mode)
  12990. (call-interactively (key-binding (vector last-input-event))))))
  12991. (defvar org-latex-fragment-image-overlays nil
  12992. "List of overlays carrying the images of latex fragments.")
  12993. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12994. (defun org-remove-latex-fragment-image-overlays ()
  12995. "Remove all overlays with LaTeX fragment images in current buffer."
  12996. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12997. (setq org-latex-fragment-image-overlays nil))
  12998. (defun org-preview-latex-fragment (&optional subtree)
  12999. "Preview the LaTeX fragment at point, or all locally or globally.
  13000. If the cursor is in a LaTeX fragment, create the image and overlay
  13001. it over the source code. If there is no fragment at point, display
  13002. all fragments in the current text, from one headline to the next. With
  13003. prefix SUBTREE, display all fragments in the current subtree. With a
  13004. double prefix `C-u C-u', or when the cursor is before the first headline,
  13005. display all fragments in the buffer.
  13006. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  13007. (interactive "P")
  13008. (org-remove-latex-fragment-image-overlays)
  13009. (save-excursion
  13010. (save-restriction
  13011. (let (beg end at msg)
  13012. (cond
  13013. ((or (equal subtree '(16))
  13014. (not (save-excursion
  13015. (re-search-backward (concat "^" outline-regexp) nil t))))
  13016. (setq beg (point-min) end (point-max)
  13017. msg "Creating images for buffer...%s"))
  13018. ((equal subtree '(4))
  13019. (org-back-to-heading)
  13020. (setq beg (point) end (org-end-of-subtree t)
  13021. msg "Creating images for subtree...%s"))
  13022. (t
  13023. (if (setq at (org-inside-LaTeX-fragment-p))
  13024. (goto-char (max (point-min) (- (cdr at) 2)))
  13025. (org-back-to-heading))
  13026. (setq beg (point) end (progn (outline-next-heading) (point))
  13027. msg (if at "Creating image...%s"
  13028. "Creating images for entry...%s"))))
  13029. (message msg "")
  13030. (narrow-to-region beg end)
  13031. (goto-char beg)
  13032. (org-format-latex
  13033. (concat "ltxpng/" (file-name-sans-extension
  13034. (file-name-nondirectory
  13035. buffer-file-name)))
  13036. default-directory 'overlays msg at 'forbuffer)
  13037. (message msg "done. Use `C-c C-c' to remove images.")))))
  13038. (defvar org-latex-regexps
  13039. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  13040. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  13041. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  13042. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13043. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13044. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  13045. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  13046. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  13047. "Regular expressions for matching embedded LaTeX.")
  13048. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  13049. "Replace LaTeX fragments with links to an image, and produce images.
  13050. Some of the options can be changed using the variable
  13051. `org-format-latex-options'."
  13052. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  13053. (let* ((prefixnodir (file-name-nondirectory prefix))
  13054. (absprefix (expand-file-name prefix dir))
  13055. (todir (file-name-directory absprefix))
  13056. (opt org-format-latex-options)
  13057. (matchers (plist-get opt :matchers))
  13058. (re-list org-latex-regexps)
  13059. (cnt 0) txt link beg end re e checkdir
  13060. executables-checked
  13061. m n block linkfile movefile ov)
  13062. ;; Check if there are old images files with this prefix, and remove them
  13063. (when (file-directory-p todir)
  13064. (mapc 'delete-file
  13065. (directory-files
  13066. todir 'full
  13067. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  13068. ;; Check the different regular expressions
  13069. (while (setq e (pop re-list))
  13070. (setq m (car e) re (nth 1 e) n (nth 2 e)
  13071. block (if (nth 3 e) "\n\n" ""))
  13072. (when (member m matchers)
  13073. (goto-char (point-min))
  13074. (while (re-search-forward re nil t)
  13075. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  13076. (not (get-text-property (match-beginning n)
  13077. 'org-protected))
  13078. (or (not overlays)
  13079. (not (eq (get-char-property (match-beginning n)
  13080. 'org-overlay-type)
  13081. 'org-latex-overlay))))
  13082. (setq txt (match-string n)
  13083. beg (match-beginning n) end (match-end n)
  13084. cnt (1+ cnt)
  13085. linkfile (format "%s_%04d.png" prefix cnt)
  13086. movefile (format "%s_%04d.png" absprefix cnt)
  13087. link (concat block "[[file:" linkfile "]]" block))
  13088. (if msg (message msg cnt))
  13089. (goto-char beg)
  13090. (unless checkdir ; make sure the directory exists
  13091. (setq checkdir t)
  13092. (or (file-directory-p todir) (make-directory todir)))
  13093. (unless executables-checked
  13094. (org-check-external-command
  13095. "latex" "needed to convert LaTeX fragments to images")
  13096. (org-check-external-command
  13097. "dvipng" "needed to convert LaTeX fragments to images")
  13098. (setq executables-checked t))
  13099. (org-create-formula-image
  13100. txt movefile opt forbuffer)
  13101. (if overlays
  13102. (progn
  13103. (mapc (lambda (o)
  13104. (if (eq (org-overlay-get o 'org-overlay-type)
  13105. 'org-latex-overlay)
  13106. (org-delete-overlay o)))
  13107. (org-overlays-in beg end))
  13108. (setq ov (org-make-overlay beg end))
  13109. (org-overlay-put ov 'org-overlay-type 'org-latex-overlay)
  13110. (if (featurep 'xemacs)
  13111. (progn
  13112. (org-overlay-put ov 'invisible t)
  13113. (org-overlay-put
  13114. ov 'end-glyph
  13115. (make-glyph (vector 'png :file movefile))))
  13116. (org-overlay-put
  13117. ov 'display
  13118. (list 'image :type 'png :file movefile :ascent 'center)))
  13119. (push ov org-latex-fragment-image-overlays)
  13120. (goto-char end))
  13121. (delete-region beg end)
  13122. (insert link))))))))
  13123. (defvar org-export-latex-packages-alist) ;; defined in org-latex.el
  13124. ;; This function borrows from Ganesh Swami's latex2png.el
  13125. (defun org-create-formula-image (string tofile options buffer)
  13126. "This calls dvipng."
  13127. (require 'org-latex)
  13128. (let* ((tmpdir (if (featurep 'xemacs)
  13129. (temp-directory)
  13130. temporary-file-directory))
  13131. (texfilebase (make-temp-name
  13132. (expand-file-name "orgtex" tmpdir)))
  13133. (texfile (concat texfilebase ".tex"))
  13134. (dvifile (concat texfilebase ".dvi"))
  13135. (pngfile (concat texfilebase ".png"))
  13136. (fnh (if (featurep 'xemacs)
  13137. (font-height (get-face-font 'default))
  13138. (face-attribute 'default :height nil)))
  13139. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  13140. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  13141. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  13142. "Black"))
  13143. (bg (or (plist-get options (if buffer :background :html-background))
  13144. "Transparent")))
  13145. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  13146. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  13147. (with-temp-file texfile
  13148. (insert org-format-latex-header
  13149. (if org-export-latex-packages-alist
  13150. (concat "\n"
  13151. (mapconcat (lambda(p)
  13152. (if (equal "" (car p))
  13153. (format "\\usepackage{%s}" (cadr p))
  13154. (format "\\usepackage[%s]{%s}"
  13155. (car p) (cadr p))))
  13156. org-export-latex-packages-alist "\n"))
  13157. "")
  13158. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  13159. (let ((dir default-directory))
  13160. (condition-case nil
  13161. (progn
  13162. (cd tmpdir)
  13163. (call-process "latex" nil nil nil texfile))
  13164. (error nil))
  13165. (cd dir))
  13166. (if (not (file-exists-p dvifile))
  13167. (progn (message "Failed to create dvi file from %s" texfile) nil)
  13168. (condition-case nil
  13169. (call-process "dvipng" nil nil nil
  13170. "-fg" fg "-bg" bg
  13171. "-D" dpi
  13172. ;;"-x" scale "-y" scale
  13173. "-T" "tight"
  13174. "-o" pngfile
  13175. dvifile)
  13176. (error nil))
  13177. (if (not (file-exists-p pngfile))
  13178. (progn (message "Failed to create png file from %s" texfile) nil)
  13179. ;; Use the requested file name and clean up
  13180. (copy-file pngfile tofile 'replace)
  13181. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  13182. (delete-file (concat texfilebase e)))
  13183. pngfile))))
  13184. (defun org-dvipng-color (attr)
  13185. "Return an rgb color specification for dvipng."
  13186. (apply 'format "rgb %s %s %s"
  13187. (mapcar 'org-normalize-color
  13188. (color-values (face-attribute 'default attr nil)))))
  13189. (defun org-normalize-color (value)
  13190. "Return string to be used as color value for an RGB component."
  13191. (format "%g" (/ value 65535.0)))
  13192. ;;;; Key bindings
  13193. ;; Make `C-c C-x' a prefix key
  13194. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  13195. ;; TAB key with modifiers
  13196. (org-defkey org-mode-map "\C-i" 'org-cycle)
  13197. (org-defkey org-mode-map [(tab)] 'org-cycle)
  13198. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  13199. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  13200. (org-defkey org-mode-map "\M-\t" 'org-complete)
  13201. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  13202. ;; The following line is necessary under Suse GNU/Linux
  13203. (unless (featurep 'xemacs)
  13204. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  13205. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  13206. (define-key org-mode-map [backtab] 'org-shifttab)
  13207. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  13208. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  13209. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  13210. ;; Cursor keys with modifiers
  13211. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  13212. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  13213. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  13214. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  13215. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  13216. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  13217. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  13218. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  13219. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  13220. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  13221. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  13222. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  13223. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  13224. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  13225. ;;; Extra keys for tty access.
  13226. ;; We only set them when really needed because otherwise the
  13227. ;; menus don't show the simple keys
  13228. (when (or org-use-extra-keys
  13229. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  13230. (not window-system))
  13231. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  13232. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  13233. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  13234. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  13235. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  13236. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  13237. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  13238. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  13239. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  13240. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  13241. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  13242. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  13243. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  13244. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  13245. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  13246. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  13247. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  13248. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  13249. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  13250. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  13251. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  13252. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  13253. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  13254. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  13255. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  13256. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  13257. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  13258. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  13259. ;; All the other keys
  13260. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  13261. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  13262. (if (boundp 'narrow-map)
  13263. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  13264. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  13265. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  13266. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  13267. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  13268. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  13269. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  13270. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  13271. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  13272. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  13273. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  13274. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  13275. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  13276. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  13277. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  13278. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  13279. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  13280. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  13281. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  13282. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  13283. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  13284. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  13285. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  13286. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  13287. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  13288. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  13289. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  13290. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  13291. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  13292. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  13293. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  13294. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  13295. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  13296. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  13297. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  13298. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  13299. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  13300. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  13301. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  13302. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  13303. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  13304. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  13305. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  13306. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13307. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  13308. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  13309. (org-defkey org-mode-map "\C-c^" 'org-sort)
  13310. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  13311. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  13312. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  13313. (org-defkey org-mode-map "\C-m" 'org-return)
  13314. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  13315. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  13316. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  13317. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  13318. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  13319. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  13320. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  13321. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  13322. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  13323. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  13324. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  13325. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  13326. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  13327. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  13328. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  13329. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  13330. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  13331. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  13332. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  13333. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  13334. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  13335. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  13336. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  13337. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  13338. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  13339. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  13340. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  13341. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  13342. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  13343. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  13344. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  13345. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  13346. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  13347. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  13348. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  13349. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  13350. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  13351. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  13352. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  13353. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  13354. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  13355. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  13356. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  13357. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  13358. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  13359. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  13360. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  13361. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  13362. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  13363. (when (featurep 'xemacs)
  13364. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  13365. (defvar org-self-insert-command-undo-counter 0)
  13366. (defvar org-table-auto-blank-field) ; defined in org-table.el
  13367. (defun org-self-insert-command (N)
  13368. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  13369. If the cursor is in a table looking at whitespace, the whitespace is
  13370. overwritten, and the table is not marked as requiring realignment."
  13371. (interactive "p")
  13372. (if (and
  13373. (org-table-p)
  13374. (progn
  13375. ;; check if we blank the field, and if that triggers align
  13376. (and (featurep 'org-table) org-table-auto-blank-field
  13377. (member last-command
  13378. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  13379. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  13380. ;; got extra space, this field does not determine column width
  13381. (let (org-table-may-need-update) (org-table-blank-field))
  13382. ;; no extra space, this field may determine column width
  13383. (org-table-blank-field)))
  13384. t)
  13385. (eq N 1)
  13386. (looking-at "[^|\n]* |"))
  13387. (let (org-table-may-need-update)
  13388. (goto-char (1- (match-end 0)))
  13389. (delete-backward-char 1)
  13390. (goto-char (match-beginning 0))
  13391. (self-insert-command N))
  13392. (setq org-table-may-need-update t)
  13393. (self-insert-command N)
  13394. (org-fix-tags-on-the-fly)
  13395. (if org-self-insert-cluster-for-undo
  13396. (if (not (eq last-command 'org-self-insert-command))
  13397. (setq org-self-insert-command-undo-counter 1)
  13398. (if (>= org-self-insert-command-undo-counter 20)
  13399. (setq org-self-insert-command-undo-counter 1)
  13400. (and (> org-self-insert-command-undo-counter 0)
  13401. buffer-undo-list
  13402. (not (cadr buffer-undo-list)) ; remove nil entry
  13403. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  13404. (setq org-self-insert-command-undo-counter
  13405. (1+ org-self-insert-command-undo-counter)))))))
  13406. (defun org-fix-tags-on-the-fly ()
  13407. (when (and (equal (char-after (point-at-bol)) ?*)
  13408. (org-on-heading-p))
  13409. (org-align-tags-here org-tags-column)))
  13410. (defun org-delete-backward-char (N)
  13411. "Like `delete-backward-char', insert whitespace at field end in tables.
  13412. When deleting backwards, in tables this function will insert whitespace in
  13413. front of the next \"|\" separator, to keep the table aligned. The table will
  13414. still be marked for re-alignment if the field did fill the entire column,
  13415. because, in this case the deletion might narrow the column."
  13416. (interactive "p")
  13417. (if (and (org-table-p)
  13418. (eq N 1)
  13419. (string-match "|" (buffer-substring (point-at-bol) (point)))
  13420. (looking-at ".*?|"))
  13421. (let ((pos (point))
  13422. (noalign (looking-at "[^|\n\r]* |"))
  13423. (c org-table-may-need-update))
  13424. (backward-delete-char N)
  13425. (skip-chars-forward "^|")
  13426. (insert " ")
  13427. (goto-char (1- pos))
  13428. ;; noalign: if there were two spaces at the end, this field
  13429. ;; does not determine the width of the column.
  13430. (if noalign (setq org-table-may-need-update c)))
  13431. (backward-delete-char N)
  13432. (org-fix-tags-on-the-fly)))
  13433. (defun org-delete-char (N)
  13434. "Like `delete-char', but insert whitespace at field end in tables.
  13435. When deleting characters, in tables this function will insert whitespace in
  13436. front of the next \"|\" separator, to keep the table aligned. The table will
  13437. still be marked for re-alignment if the field did fill the entire column,
  13438. because, in this case the deletion might narrow the column."
  13439. (interactive "p")
  13440. (if (and (org-table-p)
  13441. (not (bolp))
  13442. (not (= (char-after) ?|))
  13443. (eq N 1))
  13444. (if (looking-at ".*?|")
  13445. (let ((pos (point))
  13446. (noalign (looking-at "[^|\n\r]* |"))
  13447. (c org-table-may-need-update))
  13448. (replace-match (concat
  13449. (substring (match-string 0) 1 -1)
  13450. " |"))
  13451. (goto-char pos)
  13452. ;; noalign: if there were two spaces at the end, this field
  13453. ;; does not determine the width of the column.
  13454. (if noalign (setq org-table-may-need-update c)))
  13455. (delete-char N))
  13456. (delete-char N)
  13457. (org-fix-tags-on-the-fly)))
  13458. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  13459. (put 'org-self-insert-command 'delete-selection t)
  13460. (put 'orgtbl-self-insert-command 'delete-selection t)
  13461. (put 'org-delete-char 'delete-selection 'supersede)
  13462. (put 'org-delete-backward-char 'delete-selection 'supersede)
  13463. (put 'org-yank 'delete-selection 'yank)
  13464. ;; Make `flyspell-mode' delay after some commands
  13465. (put 'org-self-insert-command 'flyspell-delayed t)
  13466. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  13467. (put 'org-delete-char 'flyspell-delayed t)
  13468. (put 'org-delete-backward-char 'flyspell-delayed t)
  13469. ;; Make pabbrev-mode expand after org-mode commands
  13470. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  13471. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  13472. ;; How to do this: Measure non-white length of current string
  13473. ;; If equal to column width, we should realign.
  13474. (defun org-remap (map &rest commands)
  13475. "In MAP, remap the functions given in COMMANDS.
  13476. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  13477. (let (new old)
  13478. (while commands
  13479. (setq old (pop commands) new (pop commands))
  13480. (if (fboundp 'command-remapping)
  13481. (org-defkey map (vector 'remap old) new)
  13482. (substitute-key-definition old new map global-map)))))
  13483. (when (eq org-enable-table-editor 'optimized)
  13484. ;; If the user wants maximum table support, we need to hijack
  13485. ;; some standard editing functions
  13486. (org-remap org-mode-map
  13487. 'self-insert-command 'org-self-insert-command
  13488. 'delete-char 'org-delete-char
  13489. 'delete-backward-char 'org-delete-backward-char)
  13490. (org-defkey org-mode-map "|" 'org-force-self-insert))
  13491. (defvar org-ctrl-c-ctrl-c-hook nil
  13492. "Hook for functions attaching themselves to `C-c C-c'.
  13493. This can be used to add additional functionality to the C-c C-c key which
  13494. executes context-dependent commands.
  13495. Each function will be called with no arguments. The function must check
  13496. if the context is appropriate for it to act. If yes, it should do its
  13497. thing and then return a non-nil value. If the context is wrong,
  13498. just do nothing and return nil.")
  13499. (defvar org-tab-first-hook nil
  13500. "Hook for functions to attach themselves to TAB.
  13501. See `org-ctrl-c-ctrl-c-hook' for more information.
  13502. This hook runs as the first action when TAB is pressed, even before
  13503. `org-cycle' messes around with the `outline-regexp' to cater for
  13504. inline tasks and plain list item folding.
  13505. If any function in this hook returns t, not other actions like table
  13506. field motion visibility cycling will be done.")
  13507. (defvar org-tab-after-check-for-table-hook nil
  13508. "Hook for functions to attach themselves to TAB.
  13509. See `org-ctrl-c-ctrl-c-hook' for more information.
  13510. This hook runs after it has been established that the cursor is not in a
  13511. table, but before checking if the cursor is in a headline or if global cycling
  13512. should be done.
  13513. If any function in this hook returns t, not other actions like visibility
  13514. cycling will be done.")
  13515. (defvar org-tab-after-check-for-cycling-hook nil
  13516. "Hook for functions to attach themselves to TAB.
  13517. See `org-ctrl-c-ctrl-c-hook' for more information.
  13518. This hook runs after it has been established that not table field motion and
  13519. not visibility should be done because of current context. This is probably
  13520. the place where a package like yasnippets can hook in.")
  13521. (defvar org-metaleft-hook nil
  13522. "Hook for functions attaching themselves to `M-left'.
  13523. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13524. (defvar org-metaright-hook nil
  13525. "Hook for functions attaching themselves to `M-right'.
  13526. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13527. (defvar org-metaup-hook nil
  13528. "Hook for functions attaching themselves to `M-up'.
  13529. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13530. (defvar org-metadown-hook nil
  13531. "Hook for functions attaching themselves to `M-down'.
  13532. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13533. (defvar org-shiftmetaleft-hook nil
  13534. "Hook for functions attaching themselves to `M-S-left'.
  13535. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13536. (defvar org-shiftmetaright-hook nil
  13537. "Hook for functions attaching themselves to `M-S-right'.
  13538. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13539. (defvar org-shiftmetaup-hook nil
  13540. "Hook for functions attaching themselves to `M-S-up'.
  13541. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13542. (defvar org-shiftmetadown-hook nil
  13543. "Hook for functions attaching themselves to `M-S-down'.
  13544. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13545. (defvar org-metareturn-hook nil
  13546. "Hook for functions attaching themselves to `M-RET'.
  13547. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13548. (defun org-modifier-cursor-error ()
  13549. "Throw an error, a modified cursor command was applied in wrong context."
  13550. (error "This command is active in special context like tables, headlines or items"))
  13551. (defun org-shiftselect-error ()
  13552. "Throw an error because Shift-Cursor command was applied in wrong context."
  13553. (if (and (boundp 'shift-select-mode) shift-select-mode)
  13554. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  13555. (error "This command works only in special context like headlines or timestamps")))
  13556. (defun org-call-for-shift-select (cmd)
  13557. (let ((this-command-keys-shift-translated t))
  13558. (call-interactively cmd)))
  13559. (defun org-shifttab (&optional arg)
  13560. "Global visibility cycling or move to previous table field.
  13561. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  13562. on context.
  13563. See the individual commands for more information."
  13564. (interactive "P")
  13565. (cond
  13566. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  13567. ((integerp arg)
  13568. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  13569. (message "Content view to level: %d" arg)
  13570. (org-content (prefix-numeric-value arg2))
  13571. (setq org-cycle-global-status 'overview)))
  13572. (t (call-interactively 'org-global-cycle))))
  13573. (defun org-shiftmetaleft ()
  13574. "Promote subtree or delete table column.
  13575. Calls `org-promote-subtree', `org-outdent-item',
  13576. or `org-table-delete-column', depending on context.
  13577. See the individual commands for more information."
  13578. (interactive)
  13579. (cond
  13580. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  13581. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  13582. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  13583. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13584. (t (org-modifier-cursor-error))))
  13585. (defun org-shiftmetaright ()
  13586. "Demote subtree or insert table column.
  13587. Calls `org-demote-subtree', `org-indent-item',
  13588. or `org-table-insert-column', depending on context.
  13589. See the individual commands for more information."
  13590. (interactive)
  13591. (cond
  13592. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  13593. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  13594. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  13595. ((org-at-item-p) (call-interactively 'org-indent-item))
  13596. (t (org-modifier-cursor-error))))
  13597. (defun org-shiftmetaup (&optional arg)
  13598. "Move subtree up or kill table row.
  13599. Calls `org-move-subtree-up' or `org-table-kill-row' or
  13600. `org-move-item-up' depending on context. See the individual commands
  13601. for more information."
  13602. (interactive "P")
  13603. (cond
  13604. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  13605. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13606. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13607. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13608. (t (org-modifier-cursor-error))))
  13609. (defun org-shiftmetadown (&optional arg)
  13610. "Move subtree down or insert table row.
  13611. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13612. `org-move-item-down', depending on context. See the individual
  13613. commands for more information."
  13614. (interactive "P")
  13615. (cond
  13616. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  13617. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13618. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13619. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13620. (t (org-modifier-cursor-error))))
  13621. (defun org-metaleft (&optional arg)
  13622. "Promote heading or move table column to left.
  13623. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13624. With no specific context, calls the Emacs default `backward-word'.
  13625. See the individual commands for more information."
  13626. (interactive "P")
  13627. (cond
  13628. ((run-hook-with-args-until-success 'org-metaleft-hook))
  13629. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13630. ((or (org-on-heading-p)
  13631. (and (org-region-active-p)
  13632. (save-excursion
  13633. (goto-char (region-beginning))
  13634. (org-on-heading-p))))
  13635. (call-interactively 'org-do-promote))
  13636. ((or (org-at-item-p)
  13637. (and (org-region-active-p)
  13638. (save-excursion
  13639. (goto-char (region-beginning))
  13640. (org-at-item-p))))
  13641. (call-interactively 'org-outdent-item))
  13642. (t (call-interactively 'backward-word))))
  13643. (defun org-metaright (&optional arg)
  13644. "Demote subtree or move table column to right.
  13645. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13646. With no specific context, calls the Emacs default `forward-word'.
  13647. See the individual commands for more information."
  13648. (interactive "P")
  13649. (cond
  13650. ((run-hook-with-args-until-success 'org-metaright-hook))
  13651. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13652. ((or (org-on-heading-p)
  13653. (and (org-region-active-p)
  13654. (save-excursion
  13655. (goto-char (region-beginning))
  13656. (org-on-heading-p))))
  13657. (call-interactively 'org-do-demote))
  13658. ((or (org-at-item-p)
  13659. (and (org-region-active-p)
  13660. (save-excursion
  13661. (goto-char (region-beginning))
  13662. (org-at-item-p))))
  13663. (call-interactively 'org-indent-item))
  13664. (t (call-interactively 'forward-word))))
  13665. (defun org-metaup (&optional arg)
  13666. "Move subtree up or move table row up.
  13667. Calls `org-move-subtree-up' or `org-table-move-row' or
  13668. `org-move-item-up', depending on context. See the individual commands
  13669. for more information."
  13670. (interactive "P")
  13671. (cond
  13672. ((run-hook-with-args-until-success 'org-metaup-hook))
  13673. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13674. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13675. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13676. (t (transpose-lines 1) (beginning-of-line -1))))
  13677. (defun org-metadown (&optional arg)
  13678. "Move subtree down or move table row down.
  13679. Calls `org-move-subtree-down' or `org-table-move-row' or
  13680. `org-move-item-down', depending on context. See the individual
  13681. commands for more information."
  13682. (interactive "P")
  13683. (cond
  13684. ((run-hook-with-args-until-success 'org-metadown-hook))
  13685. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13686. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13687. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13688. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13689. (defun org-shiftup (&optional arg)
  13690. "Increase item in timestamp or increase priority of current headline.
  13691. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13692. depending on context. See the individual commands for more information."
  13693. (interactive "P")
  13694. (cond
  13695. ((and org-support-shift-select (org-region-active-p))
  13696. (org-call-for-shift-select 'previous-line))
  13697. ((org-at-timestamp-p t)
  13698. (call-interactively (if org-edit-timestamp-down-means-later
  13699. 'org-timestamp-down 'org-timestamp-up)))
  13700. ((and (not (eq org-support-shift-select 'always))
  13701. org-enable-priority-commands
  13702. (org-on-heading-p))
  13703. (call-interactively 'org-priority-up))
  13704. ((and (not org-support-shift-select) (org-at-item-p))
  13705. (call-interactively 'org-previous-item))
  13706. ((org-clocktable-try-shift 'up arg))
  13707. (org-support-shift-select
  13708. (org-call-for-shift-select 'previous-line))
  13709. (t (org-shiftselect-error))))
  13710. (defun org-shiftdown (&optional arg)
  13711. "Decrease item in timestamp or decrease priority of current headline.
  13712. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13713. depending on context. See the individual commands for more information."
  13714. (interactive "P")
  13715. (cond
  13716. ((and org-support-shift-select (org-region-active-p))
  13717. (org-call-for-shift-select 'next-line))
  13718. ((org-at-timestamp-p t)
  13719. (call-interactively (if org-edit-timestamp-down-means-later
  13720. 'org-timestamp-up 'org-timestamp-down)))
  13721. ((and (not (eq org-support-shift-select 'always))
  13722. org-enable-priority-commands
  13723. (org-on-heading-p))
  13724. (call-interactively 'org-priority-down))
  13725. ((and (not org-support-shift-select) (org-at-item-p))
  13726. (call-interactively 'org-next-item))
  13727. ((org-clocktable-try-shift 'down arg))
  13728. (org-support-shift-select
  13729. (org-call-for-shift-select 'next-line))
  13730. (t (org-shiftselect-error))))
  13731. (defun org-shiftright (&optional arg)
  13732. "Cycle the thing at point or in the current line, depending on context.
  13733. Depending on context, this does one of the following:
  13734. - switch a timestamp at point one day into the future
  13735. - on a headline, switch to the next TODO keyword.
  13736. - on an item, switch entire list to the next bullet type
  13737. - on a property line, switch to the next allowed value
  13738. - on a clocktable definition line, move time block into the future"
  13739. (interactive "P")
  13740. (cond
  13741. ((and org-support-shift-select (org-region-active-p))
  13742. (org-call-for-shift-select 'forward-char))
  13743. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13744. ((and (not (eq org-support-shift-select 'always))
  13745. (org-on-heading-p))
  13746. (let ((org-inhibit-logging
  13747. (not org-treat-S-cursor-todo-selection-as-state-change))
  13748. (org-inhibit-blocking
  13749. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13750. (org-call-with-arg 'org-todo 'right)))
  13751. ((or (and org-support-shift-select
  13752. (not (eq org-support-shift-select 'always))
  13753. (org-at-item-bullet-p))
  13754. (and (not org-support-shift-select) (org-at-item-p)))
  13755. (org-call-with-arg 'org-cycle-list-bullet nil))
  13756. ((and (not (eq org-support-shift-select 'always))
  13757. (org-at-property-p))
  13758. (call-interactively 'org-property-next-allowed-value))
  13759. ((org-clocktable-try-shift 'right arg))
  13760. (org-support-shift-select
  13761. (org-call-for-shift-select 'forward-char))
  13762. (t (org-shiftselect-error))))
  13763. (defun org-shiftleft (&optional arg)
  13764. "Cycle the thing at point or in the current line, depending on context.
  13765. Depending on context, this does one of the following:
  13766. - switch a timestamp at point one day into the past
  13767. - on a headline, switch to the previous TODO keyword.
  13768. - on an item, switch entire list to the previous bullet type
  13769. - on a property line, switch to the previous allowed value
  13770. - on a clocktable definition line, move time block into the past"
  13771. (interactive "P")
  13772. (cond
  13773. ((and org-support-shift-select (org-region-active-p))
  13774. (org-call-for-shift-select 'backward-char))
  13775. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13776. ((and (not (eq org-support-shift-select 'always))
  13777. (org-on-heading-p))
  13778. (let ((org-inhibit-logging
  13779. (not org-treat-S-cursor-todo-selection-as-state-change))
  13780. (org-inhibit-blocking
  13781. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13782. (org-call-with-arg 'org-todo 'left)))
  13783. ((or (and org-support-shift-select
  13784. (not (eq org-support-shift-select 'always))
  13785. (org-at-item-bullet-p))
  13786. (and (not org-support-shift-select) (org-at-item-p)))
  13787. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13788. ((and (not (eq org-support-shift-select 'always))
  13789. (org-at-property-p))
  13790. (call-interactively 'org-property-previous-allowed-value))
  13791. ((org-clocktable-try-shift 'left arg))
  13792. (org-support-shift-select
  13793. (org-call-for-shift-select 'backward-char))
  13794. (t (org-shiftselect-error))))
  13795. (defun org-shiftcontrolright ()
  13796. "Switch to next TODO set."
  13797. (interactive)
  13798. (cond
  13799. ((and org-support-shift-select (org-region-active-p))
  13800. (org-call-for-shift-select 'forward-word))
  13801. ((and (not (eq org-support-shift-select 'always))
  13802. (org-on-heading-p))
  13803. (org-call-with-arg 'org-todo 'nextset))
  13804. (org-support-shift-select
  13805. (org-call-for-shift-select 'forward-word))
  13806. (t (org-shiftselect-error))))
  13807. (defun org-shiftcontrolleft ()
  13808. "Switch to previous TODO set."
  13809. (interactive)
  13810. (cond
  13811. ((and org-support-shift-select (org-region-active-p))
  13812. (org-call-for-shift-select 'backward-word))
  13813. ((and (not (eq org-support-shift-select 'always))
  13814. (org-on-heading-p))
  13815. (org-call-with-arg 'org-todo 'previousset))
  13816. (org-support-shift-select
  13817. (org-call-for-shift-select 'backward-word))
  13818. (t (org-shiftselect-error))))
  13819. (defun org-ctrl-c-ret ()
  13820. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13821. (interactive)
  13822. (cond
  13823. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13824. (t (call-interactively 'org-insert-heading))))
  13825. (defun org-copy-special ()
  13826. "Copy region in table or copy current subtree.
  13827. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13828. See the individual commands for more information."
  13829. (interactive)
  13830. (call-interactively
  13831. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13832. (defun org-cut-special ()
  13833. "Cut region in table or cut current subtree.
  13834. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13835. See the individual commands for more information."
  13836. (interactive)
  13837. (call-interactively
  13838. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13839. (defun org-paste-special (arg)
  13840. "Paste rectangular region into table, or past subtree relative to level.
  13841. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13842. See the individual commands for more information."
  13843. (interactive "P")
  13844. (if (org-at-table-p)
  13845. (org-table-paste-rectangle)
  13846. (org-paste-subtree arg)))
  13847. (defun org-edit-special ()
  13848. "Call a special editor for the stuff at point.
  13849. When at a table, call the formula editor with `org-table-edit-formulas'.
  13850. When at the first line of an src example, call `org-edit-src-code'.
  13851. When in an #+include line, visit the include file. Otherwise call
  13852. `ffap' to visit the file at point."
  13853. (interactive)
  13854. (cond
  13855. ((org-at-table-p)
  13856. (call-interactively 'org-table-edit-formulas))
  13857. ((save-excursion
  13858. (beginning-of-line 1)
  13859. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13860. (find-file (org-trim (match-string 1))))
  13861. ((org-edit-src-code))
  13862. ((org-edit-fixed-width-region))
  13863. (t (call-interactively 'ffap))))
  13864. (defun org-ctrl-c-ctrl-c (&optional arg)
  13865. "Set tags in headline, or update according to changed information at point.
  13866. This command does many different things, depending on context:
  13867. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13868. this is what we do.
  13869. - If the cursor is on a statistics cookie, update it.
  13870. - If the cursor is in a headline, prompt for tags and insert them
  13871. into the current line, aligned to `org-tags-column'. When called
  13872. with prefix arg, realign all tags in the current buffer.
  13873. - If the cursor is in one of the special #+KEYWORD lines, this
  13874. triggers scanning the buffer for these lines and updating the
  13875. information.
  13876. - If the cursor is inside a table, realign the table. This command
  13877. works even if the automatic table editor has been turned off.
  13878. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13879. the entire table.
  13880. - If the cursor is at a footnote reference or definition, jump to
  13881. the corresponding definition or references, respectively.
  13882. - If the cursor is a the beginning of a dynamic block, update it.
  13883. - If the cursor is inside a table created by the table.el package,
  13884. activate that table.
  13885. - If the current buffer is a remember buffer, close note and file
  13886. it. A prefix argument of 1 files to the default location
  13887. without further interaction. A prefix argument of 2 files to
  13888. the currently clocking task.
  13889. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13890. links in this buffer.
  13891. - If the cursor is on a numbered item in a plain list, renumber the
  13892. ordered list.
  13893. - If the cursor is on a checkbox, toggle it."
  13894. (interactive "P")
  13895. (let ((org-enable-table-editor t))
  13896. (cond
  13897. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13898. org-occur-highlights
  13899. org-latex-fragment-image-overlays)
  13900. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13901. (org-remove-occur-highlights)
  13902. (org-remove-latex-fragment-image-overlays)
  13903. (message "Temporary highlights/overlays removed from current buffer"))
  13904. ((and (local-variable-p 'org-finish-function (current-buffer))
  13905. (fboundp org-finish-function))
  13906. (funcall org-finish-function))
  13907. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13908. ((org-at-property-p)
  13909. (call-interactively 'org-property-action))
  13910. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13911. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  13912. (or (org-on-heading-p) (org-at-item-p)))
  13913. (call-interactively 'org-update-statistics-cookies))
  13914. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13915. ((org-at-table.el-p)
  13916. (require 'table)
  13917. (beginning-of-line 1)
  13918. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13919. (call-interactively 'table-recognize-table))
  13920. ((org-at-table-p)
  13921. (org-table-maybe-eval-formula)
  13922. (if arg
  13923. (call-interactively 'org-table-recalculate)
  13924. (org-table-maybe-recalculate-line))
  13925. (call-interactively 'org-table-align))
  13926. ((or (org-footnote-at-reference-p)
  13927. (org-footnote-at-definition-p))
  13928. (call-interactively 'org-footnote-action))
  13929. ((org-at-item-checkbox-p)
  13930. (call-interactively 'org-toggle-checkbox))
  13931. ((org-at-item-p)
  13932. (if arg
  13933. (call-interactively 'org-toggle-checkbox)
  13934. (call-interactively 'org-maybe-renumber-ordered-list)))
  13935. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  13936. ;; Dynamic block
  13937. (beginning-of-line 1)
  13938. (save-excursion (org-update-dblock)))
  13939. ((save-excursion
  13940. (beginning-of-line 1)
  13941. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  13942. (cond
  13943. ((equal (match-string 1) "TBLFM")
  13944. ;; Recalculate the table before this line
  13945. (save-excursion
  13946. (beginning-of-line 1)
  13947. (skip-chars-backward " \r\n\t")
  13948. (if (org-at-table-p)
  13949. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  13950. (t
  13951. ; (org-set-regexps-and-options)
  13952. ; (org-restart-font-lock)
  13953. (let ((org-inhibit-startup t)) (org-mode-restart))
  13954. (message "Local setup has been refreshed"))))
  13955. ((org-clock-update-time-maybe))
  13956. (t (error "C-c C-c can do nothing useful at this location")))))
  13957. (defun org-mode-restart ()
  13958. "Restart Org-mode, to scan again for special lines.
  13959. Also updates the keyword regular expressions."
  13960. (interactive)
  13961. (org-mode)
  13962. (message "Org-mode restarted"))
  13963. (defun org-kill-note-or-show-branches ()
  13964. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13965. (interactive)
  13966. (if (not org-finish-function)
  13967. (call-interactively 'show-branches)
  13968. (let ((org-note-abort t))
  13969. (funcall org-finish-function))))
  13970. (defun org-return (&optional indent)
  13971. "Goto next table row or insert a newline.
  13972. Calls `org-table-next-row' or `newline', depending on context.
  13973. See the individual commands for more information."
  13974. (interactive)
  13975. (cond
  13976. ((bobp) (if indent (newline-and-indent) (newline)))
  13977. ((org-at-table-p)
  13978. (org-table-justify-field-maybe)
  13979. (call-interactively 'org-table-next-row))
  13980. ((and org-return-follows-link
  13981. (eq (get-text-property (point) 'face) 'org-link))
  13982. (call-interactively 'org-open-at-point))
  13983. ((and (org-at-heading-p)
  13984. (looking-at
  13985. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13986. (org-show-entry)
  13987. (end-of-line 1)
  13988. (newline))
  13989. (t (if indent (newline-and-indent) (newline)))))
  13990. (defun org-return-indent ()
  13991. "Goto next table row or insert a newline and indent.
  13992. Calls `org-table-next-row' or `newline-and-indent', depending on
  13993. context. See the individual commands for more information."
  13994. (interactive)
  13995. (org-return t))
  13996. (defun org-ctrl-c-star ()
  13997. "Compute table, or change heading status of lines.
  13998. Calls `org-table-recalculate' or `org-toggle-heading',
  13999. depending on context."
  14000. (interactive)
  14001. (cond
  14002. ((org-at-table-p)
  14003. (call-interactively 'org-table-recalculate))
  14004. (t
  14005. ;; Convert all lines in region to list items
  14006. (call-interactively 'org-toggle-heading))))
  14007. (defun org-ctrl-c-minus ()
  14008. "Insert separator line in table or modify bullet status of line.
  14009. Also turns a plain line or a region of lines into list items.
  14010. Calls `org-table-insert-hline', `org-toggle-item', or
  14011. `org-cycle-list-bullet', depending on context."
  14012. (interactive)
  14013. (cond
  14014. ((org-at-table-p)
  14015. (call-interactively 'org-table-insert-hline))
  14016. ((org-region-active-p)
  14017. (call-interactively 'org-toggle-item))
  14018. ((org-in-item-p)
  14019. (call-interactively 'org-cycle-list-bullet))
  14020. (t
  14021. (call-interactively 'org-toggle-item))))
  14022. (defun org-toggle-item ()
  14023. "Convert headings or normal lines to items, items to normal lines.
  14024. If there is no active region, only the current line is considered.
  14025. If the first line in the region is a headline, convert all headlines to items.
  14026. If the first line in the region is an item, convert all items to normal lines.
  14027. If the first line is normal text, add an item bullet to each line."
  14028. (interactive)
  14029. (let (l2 l beg end)
  14030. (if (org-region-active-p)
  14031. (setq beg (region-beginning) end (region-end))
  14032. (setq beg (point-at-bol)
  14033. end (min (1+ (point-at-eol)) (point-max))))
  14034. (save-excursion
  14035. (goto-char end)
  14036. (setq l2 (org-current-line))
  14037. (goto-char beg)
  14038. (beginning-of-line 1)
  14039. (setq l (1- (org-current-line)))
  14040. (if (org-at-item-p)
  14041. ;; We already have items, de-itemize
  14042. (while (< (setq l (1+ l)) l2)
  14043. (when (org-at-item-p)
  14044. (goto-char (match-beginning 2))
  14045. (delete-region (match-beginning 2) (match-end 2))
  14046. (and (looking-at "[ \t]+") (replace-match "")))
  14047. (beginning-of-line 2))
  14048. (if (org-on-heading-p)
  14049. ;; Headings, convert to items
  14050. (while (< (setq l (1+ l)) l2)
  14051. (if (looking-at org-outline-regexp)
  14052. (replace-match "- " t t))
  14053. (beginning-of-line 2))
  14054. ;; normal lines, turn them into items
  14055. (while (< (setq l (1+ l)) l2)
  14056. (unless (org-at-item-p)
  14057. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  14058. (replace-match "\\1- \\2")))
  14059. (beginning-of-line 2)))))))
  14060. (defun org-toggle-heading (&optional nstars)
  14061. "Convert headings to normal text, or items or text to headings.
  14062. If there is no active region, only the current line is considered.
  14063. If the first line is a heading, remove the stars from all headlines
  14064. in the region.
  14065. If the first line is a plain list item, turn all plain list items
  14066. into headings.
  14067. If the first line is a normal line, turn each and every line in the
  14068. region into a heading.
  14069. When converting a line into a heading, the number of stars is chosen
  14070. such that the lines become children of the current entry. However,
  14071. when a prefix argument is given, its value determines the number of
  14072. stars to add."
  14073. (interactive "P")
  14074. (let (l2 l itemp beg end)
  14075. (if (org-region-active-p)
  14076. (setq beg (region-beginning) end (region-end))
  14077. (setq beg (point-at-bol)
  14078. end (min (1+ (point-at-eol)) (point-max))))
  14079. (save-excursion
  14080. (goto-char end)
  14081. (setq l2 (org-current-line))
  14082. (goto-char beg)
  14083. (beginning-of-line 1)
  14084. (setq l (1- (org-current-line)))
  14085. (if (org-on-heading-p)
  14086. ;; We already have headlines, de-star them
  14087. (while (< (setq l (1+ l)) l2)
  14088. (when (org-on-heading-p t)
  14089. (and (looking-at outline-regexp) (replace-match "")))
  14090. (beginning-of-line 2))
  14091. (setq itemp (org-at-item-p))
  14092. (let* ((stars
  14093. (if nstars
  14094. (make-string (prefix-numeric-value current-prefix-arg)
  14095. ?*)
  14096. (save-excursion
  14097. (if (re-search-backward org-complex-heading-regexp nil t)
  14098. (match-string 1) ""))))
  14099. (add-stars (cond (nstars "")
  14100. ((equal stars "") "*")
  14101. (org-odd-levels-only "**")
  14102. (t "*")))
  14103. (rpl (concat stars add-stars " ")))
  14104. (while (< (setq l (1+ l)) l2)
  14105. (if itemp
  14106. (and (org-at-item-p) (replace-match rpl t t))
  14107. (unless (org-on-heading-p)
  14108. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  14109. (replace-match (concat rpl (match-string 2))))))
  14110. (beginning-of-line 2)))))))
  14111. (defun org-meta-return (&optional arg)
  14112. "Insert a new heading or wrap a region in a table.
  14113. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  14114. See the individual commands for more information."
  14115. (interactive "P")
  14116. (cond
  14117. ((run-hook-with-args-until-success 'org-metareturn-hook))
  14118. ((org-at-table-p)
  14119. (call-interactively 'org-table-wrap-region))
  14120. (t (call-interactively 'org-insert-heading))))
  14121. ;;; Menu entries
  14122. ;; Define the Org-mode menus
  14123. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  14124. '("Tbl"
  14125. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  14126. ["Next Field" org-cycle (org-at-table-p)]
  14127. ["Previous Field" org-shifttab (org-at-table-p)]
  14128. ["Next Row" org-return (org-at-table-p)]
  14129. "--"
  14130. ["Blank Field" org-table-blank-field (org-at-table-p)]
  14131. ["Edit Field" org-table-edit-field (org-at-table-p)]
  14132. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  14133. "--"
  14134. ("Column"
  14135. ["Move Column Left" org-metaleft (org-at-table-p)]
  14136. ["Move Column Right" org-metaright (org-at-table-p)]
  14137. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  14138. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  14139. ("Row"
  14140. ["Move Row Up" org-metaup (org-at-table-p)]
  14141. ["Move Row Down" org-metadown (org-at-table-p)]
  14142. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  14143. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  14144. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  14145. "--"
  14146. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  14147. ("Rectangle"
  14148. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  14149. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  14150. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  14151. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  14152. "--"
  14153. ("Calculate"
  14154. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  14155. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  14156. ["Edit Formulas" org-edit-special (org-at-table-p)]
  14157. "--"
  14158. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  14159. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  14160. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  14161. "--"
  14162. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  14163. "--"
  14164. ["Sum Column/Rectangle" org-table-sum
  14165. (or (org-at-table-p) (org-region-active-p))]
  14166. ["Which Column?" org-table-current-column (org-at-table-p)])
  14167. ["Debug Formulas"
  14168. org-table-toggle-formula-debugger
  14169. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  14170. ["Show Col/Row Numbers"
  14171. org-table-toggle-coordinate-overlays
  14172. :style toggle
  14173. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  14174. "--"
  14175. ["Create" org-table-create (and (not (org-at-table-p))
  14176. org-enable-table-editor)]
  14177. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  14178. ["Import from File" org-table-import (not (org-at-table-p))]
  14179. ["Export to File" org-table-export (org-at-table-p)]
  14180. "--"
  14181. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  14182. (easy-menu-define org-org-menu org-mode-map "Org menu"
  14183. '("Org"
  14184. ("Show/Hide"
  14185. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  14186. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  14187. ["Sparse Tree..." org-sparse-tree t]
  14188. ["Reveal Context" org-reveal t]
  14189. ["Show All" show-all t]
  14190. "--"
  14191. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  14192. "--"
  14193. ["New Heading" org-insert-heading t]
  14194. ("Navigate Headings"
  14195. ["Up" outline-up-heading t]
  14196. ["Next" outline-next-visible-heading t]
  14197. ["Previous" outline-previous-visible-heading t]
  14198. ["Next Same Level" outline-forward-same-level t]
  14199. ["Previous Same Level" outline-backward-same-level t]
  14200. "--"
  14201. ["Jump" org-goto t])
  14202. ("Edit Structure"
  14203. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  14204. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  14205. "--"
  14206. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  14207. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  14208. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  14209. "--"
  14210. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  14211. "--"
  14212. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  14213. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  14214. ["Demote Heading" org-metaright (not (org-at-table-p))]
  14215. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  14216. "--"
  14217. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  14218. "--"
  14219. ["Convert to odd levels" org-convert-to-odd-levels t]
  14220. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  14221. ("Editing"
  14222. ["Emphasis..." org-emphasize t]
  14223. ["Edit Source Example" org-edit-special t]
  14224. "--"
  14225. ["Footnote new/jump" org-footnote-action t]
  14226. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  14227. ("Archive"
  14228. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  14229. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  14230. ; :active t :keys "C-u C-c C-x C-a"]
  14231. ["Sparse trees open ARCHIVE trees"
  14232. (setq org-sparse-tree-open-archived-trees
  14233. (not org-sparse-tree-open-archived-trees))
  14234. :style toggle :selected org-sparse-tree-open-archived-trees]
  14235. ["Cycling opens ARCHIVE trees"
  14236. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  14237. :style toggle :selected org-cycle-open-archived-trees]
  14238. "--"
  14239. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  14240. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  14241. ; ["Check and Move Children" (org-archive-subtree '(4))
  14242. ; :active t :keys "C-u C-c C-x C-s"]
  14243. )
  14244. "--"
  14245. ("Hyperlinks"
  14246. ["Store Link (Global)" org-store-link t]
  14247. ["Find existing link to here" org-occur-link-in-agenda-files t]
  14248. ["Insert Link" org-insert-link t]
  14249. ["Follow Link" org-open-at-point t]
  14250. "--"
  14251. ["Next link" org-next-link t]
  14252. ["Previous link" org-previous-link t]
  14253. "--"
  14254. ["Descriptive Links"
  14255. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  14256. :style radio
  14257. :selected (member '(org-link) buffer-invisibility-spec)]
  14258. ["Literal Links"
  14259. (progn
  14260. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  14261. :style radio
  14262. :selected (not (member '(org-link) buffer-invisibility-spec))])
  14263. "--"
  14264. ("TODO Lists"
  14265. ["TODO/DONE/-" org-todo t]
  14266. ("Select keyword"
  14267. ["Next keyword" org-shiftright (org-on-heading-p)]
  14268. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  14269. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  14270. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  14271. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  14272. ["Show TODO Tree" org-show-todo-tree t]
  14273. ["Global TODO list" org-todo-list t]
  14274. "--"
  14275. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  14276. :selected org-enforce-todo-dependencies :style toggle :active t]
  14277. "Settings for tree at point"
  14278. ["Do Children sequentially" org-toggle-ordered-property :style radio
  14279. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  14280. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14281. ["Do Children parallel" org-toggle-ordered-property :style radio
  14282. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  14283. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14284. "--"
  14285. ["Set Priority" org-priority t]
  14286. ["Priority Up" org-shiftup t]
  14287. ["Priority Down" org-shiftdown t]
  14288. "--"
  14289. ["Get news from all feeds" org-feed-update-all t]
  14290. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  14291. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  14292. ("TAGS and Properties"
  14293. ["Set Tags" org-set-tags-command t]
  14294. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  14295. "--"
  14296. ["Set property" org-set-property t]
  14297. ["Column view of properties" org-columns t]
  14298. ["Insert Column View DBlock" org-insert-columns-dblock t])
  14299. ("Dates and Scheduling"
  14300. ["Timestamp" org-time-stamp t]
  14301. ["Timestamp (inactive)" org-time-stamp-inactive t]
  14302. ("Change Date"
  14303. ["1 Day Later" org-shiftright t]
  14304. ["1 Day Earlier" org-shiftleft t]
  14305. ["1 ... Later" org-shiftup t]
  14306. ["1 ... Earlier" org-shiftdown t])
  14307. ["Compute Time Range" org-evaluate-time-range t]
  14308. ["Schedule Item" org-schedule t]
  14309. ["Deadline" org-deadline t]
  14310. "--"
  14311. ["Custom time format" org-toggle-time-stamp-overlays
  14312. :style radio :selected org-display-custom-times]
  14313. "--"
  14314. ["Goto Calendar" org-goto-calendar t]
  14315. ["Date from Calendar" org-date-from-calendar t]
  14316. "--"
  14317. ["Start/Restart Timer" org-timer-start t]
  14318. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  14319. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  14320. ["Insert Timer String" org-timer t]
  14321. ["Insert Timer Item" org-timer-item t])
  14322. ("Logging work"
  14323. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  14324. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  14325. ["Clock out" org-clock-out t]
  14326. ["Clock cancel" org-clock-cancel t]
  14327. "--"
  14328. ["Mark as default task" org-clock-mark-default-task t]
  14329. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  14330. ["Goto running clock" org-clock-goto t]
  14331. "--"
  14332. ["Display times" org-clock-display t]
  14333. ["Create clock table" org-clock-report t]
  14334. "--"
  14335. ["Record DONE time"
  14336. (progn (setq org-log-done (not org-log-done))
  14337. (message "Switching to %s will %s record a timestamp"
  14338. (car org-done-keywords)
  14339. (if org-log-done "automatically" "not")))
  14340. :style toggle :selected org-log-done])
  14341. "--"
  14342. ["Agenda Command..." org-agenda t]
  14343. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  14344. ("File List for Agenda")
  14345. ("Special views current file"
  14346. ["TODO Tree" org-show-todo-tree t]
  14347. ["Check Deadlines" org-check-deadlines t]
  14348. ["Timeline" org-timeline t]
  14349. ["Tags/Property tree" org-match-sparse-tree t])
  14350. "--"
  14351. ["Export/Publish..." org-export t]
  14352. ("LaTeX"
  14353. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  14354. :selected org-cdlatex-mode]
  14355. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  14356. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  14357. ["Modify math symbol" org-cdlatex-math-modify
  14358. (org-inside-LaTeX-fragment-p)]
  14359. ["Insert citation" org-reftex-citation t]
  14360. "--"
  14361. ["Export LaTeX fragments as images"
  14362. (if (featurep 'org-exp)
  14363. (setq org-export-with-LaTeX-fragments
  14364. (not org-export-with-LaTeX-fragments))
  14365. (require 'org-exp))
  14366. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  14367. org-export-with-LaTeX-fragments)])
  14368. "--"
  14369. ("MobileOrg"
  14370. ["Push Files and Views" org-mobile-push t]
  14371. ["Get Captured and Flagged" org-mobile-pull t]
  14372. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  14373. "--"
  14374. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  14375. "--"
  14376. ("Documentation"
  14377. ["Show Version" org-version t]
  14378. ["Info Documentation" org-info t])
  14379. ("Customize"
  14380. ["Browse Org Group" org-customize t]
  14381. "--"
  14382. ["Expand This Menu" org-create-customize-menu
  14383. (fboundp 'customize-menu-create)])
  14384. ["Send bug report" org-submit-bug-report t]
  14385. "--"
  14386. ("Refresh/Reload"
  14387. ["Refresh setup current buffer" org-mode-restart t]
  14388. ["Reload Org (after update)" org-reload t]
  14389. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  14390. ))
  14391. (defun org-info (&optional node)
  14392. "Read documentation for Org-mode in the info system.
  14393. With optional NODE, go directly to that node."
  14394. (interactive)
  14395. (info (format "(org)%s" (or node ""))))
  14396. ;;;###autoload
  14397. (defun org-submit-bug-report ()
  14398. "Submit a bug report on Org-mode via mail.
  14399. Don't hesitate to report any problems or inaccurate documentation.
  14400. If you don't have setup sending mail from (X)Emacs, please copy the
  14401. output buffer into your mail program, as it gives us important
  14402. information about your Org-mode version and configuration."
  14403. (interactive)
  14404. (require 'reporter)
  14405. (org-load-modules-maybe)
  14406. (org-require-autoloaded-modules)
  14407. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  14408. (reporter-submit-bug-report
  14409. "emacs-orgmode@gnu.org"
  14410. (org-version)
  14411. (let (list)
  14412. (save-window-excursion
  14413. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  14414. (delete-other-windows)
  14415. (erase-buffer)
  14416. (insert "You are about to submit a bug report to the Org-mode mailing list.
  14417. We would like to add your full Org-mode and Outline configuration to the
  14418. bug report. This greatly simplifies the work of the maintainer and
  14419. other experts on the mailing list.
  14420. HOWEVER, some variables you have customized may contain private
  14421. information. The names of customers, colleagues, or friends, might
  14422. appear in the form of file names, tags, todo states, or search strings.
  14423. If you answer yes to the prompt, you might want to check and remove
  14424. such private information before sending the email.")
  14425. (add-text-properties (point-min) (point-max) '(face org-warning))
  14426. (when (yes-or-no-p "Include your Org-mode configuration ")
  14427. (mapatoms
  14428. (lambda (v)
  14429. (and (boundp v)
  14430. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  14431. (or (and (symbol-value v)
  14432. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  14433. (and
  14434. (get v 'custom-type) (get v 'standard-value)
  14435. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  14436. (push v list)))))
  14437. (kill-buffer (get-buffer "*Warn about privacy*"))
  14438. list))
  14439. nil nil
  14440. "Remember to cover the basics, that is, what you expected to happen and
  14441. what in fact did happen. You don't know how to make a good report? See
  14442. http://orgmode.org/manual/Feedback.html#Feedback
  14443. Your bug report will be posted to the Org-mode mailing list.
  14444. ------------------------------------------------------------------------")))
  14445. (defun org-install-agenda-files-menu ()
  14446. (let ((bl (buffer-list)))
  14447. (save-excursion
  14448. (while bl
  14449. (set-buffer (pop bl))
  14450. (if (org-mode-p) (setq bl nil)))
  14451. (when (org-mode-p)
  14452. (easy-menu-change
  14453. '("Org") "File List for Agenda"
  14454. (append
  14455. (list
  14456. ["Edit File List" (org-edit-agenda-file-list) t]
  14457. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  14458. ["Remove Current File from List" org-remove-file t]
  14459. ["Cycle through agenda files" org-cycle-agenda-files t]
  14460. ["Occur in all agenda files" org-occur-in-agenda-files t]
  14461. "--")
  14462. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  14463. ;;;; Documentation
  14464. ;;;###autoload
  14465. (defun org-require-autoloaded-modules ()
  14466. (interactive)
  14467. (mapc 'require
  14468. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  14469. org-docbook org-exp org-html org-icalendar
  14470. org-id org-latex
  14471. org-publish org-remember org-table
  14472. org-timer org-xoxo)))
  14473. ;;;###autoload
  14474. (defun org-reload (&optional uncompiled)
  14475. "Reload all org lisp files.
  14476. With prefix arg UNCOMPILED, load the uncompiled versions."
  14477. (interactive "P")
  14478. (require 'find-func)
  14479. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  14480. (dir-org (file-name-directory (org-find-library-name "org")))
  14481. (dir-org-contrib (ignore-errors
  14482. (file-name-directory
  14483. (org-find-library-name "org-contribdir"))))
  14484. (files
  14485. (append (directory-files dir-org t file-re)
  14486. (and dir-org-contrib
  14487. (directory-files dir-org-contrib t file-re))))
  14488. (remove-re (concat (if (featurep 'xemacs)
  14489. "org-colview" "org-colview-xemacs")
  14490. "\\'")))
  14491. (setq files (mapcar 'file-name-sans-extension files))
  14492. (setq files (mapcar
  14493. (lambda (x) (if (string-match remove-re x) nil x))
  14494. files))
  14495. (setq files (delq nil files))
  14496. (mapc
  14497. (lambda (f)
  14498. (when (featurep (intern (file-name-nondirectory f)))
  14499. (if (and (not uncompiled)
  14500. (file-exists-p (concat f ".elc")))
  14501. (load (concat f ".elc") nil nil t)
  14502. (load (concat f ".el") nil nil t))))
  14503. files))
  14504. (org-version))
  14505. ;;;###autoload
  14506. (defun org-customize ()
  14507. "Call the customize function with org as argument."
  14508. (interactive)
  14509. (org-load-modules-maybe)
  14510. (org-require-autoloaded-modules)
  14511. (customize-browse 'org))
  14512. (defun org-create-customize-menu ()
  14513. "Create a full customization menu for Org-mode, insert it into the menu."
  14514. (interactive)
  14515. (org-load-modules-maybe)
  14516. (org-require-autoloaded-modules)
  14517. (if (fboundp 'customize-menu-create)
  14518. (progn
  14519. (easy-menu-change
  14520. '("Org") "Customize"
  14521. `(["Browse Org group" org-customize t]
  14522. "--"
  14523. ,(customize-menu-create 'org)
  14524. ["Set" Custom-set t]
  14525. ["Save" Custom-save t]
  14526. ["Reset to Current" Custom-reset-current t]
  14527. ["Reset to Saved" Custom-reset-saved t]
  14528. ["Reset to Standard Settings" Custom-reset-standard t]))
  14529. (message "\"Org\"-menu now contains full customization menu"))
  14530. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  14531. ;;;; Miscellaneous stuff
  14532. ;;; Generally useful functions
  14533. (defun org-get-at-bol (property)
  14534. "Get text property PROPERTY at beginning of line."
  14535. (get-text-property (point-at-bol) property))
  14536. (defun org-find-text-property-in-string (prop s)
  14537. "Return the first non-nil value of property PROP in string S."
  14538. (or (get-text-property 0 prop s)
  14539. (get-text-property (or (next-single-property-change 0 prop s) 0)
  14540. prop s)))
  14541. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  14542. "Display the given MESSAGE as a warning."
  14543. (if (fboundp 'display-warning)
  14544. (display-warning 'org message
  14545. (if (featurep 'xemacs)
  14546. 'warning
  14547. :warning))
  14548. (let ((buf (get-buffer-create "*Org warnings*")))
  14549. (with-current-buffer buf
  14550. (goto-char (point-max))
  14551. (insert "Warning (Org): " message)
  14552. (unless (bolp)
  14553. (newline)))
  14554. (display-buffer buf)
  14555. (sit-for 0))))
  14556. (defun org-in-commented-line ()
  14557. "Is point in a line starting with `#'?"
  14558. (equal (char-after (point-at-bol)) ?#))
  14559. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  14560. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  14561. (if (and marker (marker-buffer marker)
  14562. (buffer-live-p (marker-buffer marker)))
  14563. (progn
  14564. (switch-to-buffer (marker-buffer marker))
  14565. (if (or (> marker (point-max)) (< marker (point-min)))
  14566. (widen))
  14567. (goto-char marker)
  14568. (org-show-context 'org-goto))
  14569. (if bookmark
  14570. (bookmark-jump bookmark)
  14571. (error "Cannot find location"))))
  14572. (defun org-quote-csv-field (s)
  14573. "Quote field for inclusion in CSV material."
  14574. (if (string-match "[\",]" s)
  14575. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  14576. s))
  14577. (defun org-plist-delete (plist property)
  14578. "Delete PROPERTY from PLIST.
  14579. This is in contrast to merely setting it to 0."
  14580. (let (p)
  14581. (while plist
  14582. (if (not (eq property (car plist)))
  14583. (setq p (plist-put p (car plist) (nth 1 plist))))
  14584. (setq plist (cddr plist)))
  14585. p))
  14586. (defun org-force-self-insert (N)
  14587. "Needed to enforce self-insert under remapping."
  14588. (interactive "p")
  14589. (self-insert-command N))
  14590. (defun org-string-width (s)
  14591. "Compute width of string, ignoring invisible characters.
  14592. This ignores character with invisibility property `org-link', and also
  14593. characters with property `org-cwidth', because these will become invisible
  14594. upon the next fontification round."
  14595. (let (b l)
  14596. (when (or (eq t buffer-invisibility-spec)
  14597. (assq 'org-link buffer-invisibility-spec))
  14598. (while (setq b (text-property-any 0 (length s)
  14599. 'invisible 'org-link s))
  14600. (setq s (concat (substring s 0 b)
  14601. (substring s (or (next-single-property-change
  14602. b 'invisible s) (length s)))))))
  14603. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  14604. (setq s (concat (substring s 0 b)
  14605. (substring s (or (next-single-property-change
  14606. b 'org-cwidth s) (length s))))))
  14607. (setq l (string-width s) b -1)
  14608. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  14609. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  14610. l))
  14611. (defun org-get-indentation (&optional line)
  14612. "Get the indentation of the current line, interpreting tabs.
  14613. When LINE is given, assume it represents a line and compute its indentation."
  14614. (if line
  14615. (if (string-match "^ *" (org-remove-tabs line))
  14616. (match-end 0))
  14617. (save-excursion
  14618. (beginning-of-line 1)
  14619. (skip-chars-forward " \t")
  14620. (current-column))))
  14621. (defun org-remove-tabs (s &optional width)
  14622. "Replace tabulators in S with spaces.
  14623. Assumes that s is a single line, starting in column 0."
  14624. (setq width (or width tab-width))
  14625. (while (string-match "\t" s)
  14626. (setq s (replace-match
  14627. (make-string
  14628. (- (* width (/ (+ (match-beginning 0) width) width))
  14629. (match-beginning 0)) ?\ )
  14630. t t s)))
  14631. s)
  14632. (defun org-fix-indentation (line ind)
  14633. "Fix indentation in LINE.
  14634. IND is a cons cell with target and minimum indentation.
  14635. If the current indentation in LINE is smaller than the minimum,
  14636. leave it alone. If it is larger than ind, set it to the target."
  14637. (let* ((l (org-remove-tabs line))
  14638. (i (org-get-indentation l))
  14639. (i1 (car ind)) (i2 (cdr ind)))
  14640. (if (>= i i2) (setq l (substring line i2)))
  14641. (if (> i1 0)
  14642. (concat (make-string i1 ?\ ) l)
  14643. l)))
  14644. (defun org-remove-indentation (code &optional n)
  14645. "Remove the maximum common indentation from the lines in CODE.
  14646. N may optionally be the number of spaces to remove."
  14647. (with-temp-buffer
  14648. (insert code)
  14649. (org-do-remove-indentation n)
  14650. (buffer-string)))
  14651. (defun org-do-remove-indentation (&optional n)
  14652. "Remove the maximum common indentation from the buffer."
  14653. (untabify (point-min) (point-max))
  14654. (let ((min 10000) re)
  14655. (if n
  14656. (setq min n)
  14657. (goto-char (point-min))
  14658. (while (re-search-forward "^ *[^ \n]" nil t)
  14659. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  14660. (unless (or (= min 0) (= min 10000))
  14661. (setq re (format "^ \\{%d\\}" min))
  14662. (goto-char (point-min))
  14663. (while (re-search-forward re nil t)
  14664. (replace-match "")
  14665. (end-of-line 1))
  14666. min)))
  14667. (defun org-fill-template (template alist)
  14668. "Find each %key of ALIST in TEMPLATE and replace it."
  14669. (let (entry key value)
  14670. (setq alist (sort (copy-sequence alist)
  14671. (lambda (a b) (< (length (car a)) (length (car b))))))
  14672. (while (setq entry (pop alist))
  14673. (setq template
  14674. (replace-regexp-in-string
  14675. (concat "%" (regexp-quote (car entry)))
  14676. (cdr entry) template t t)))
  14677. template))
  14678. (defun org-base-buffer (buffer)
  14679. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  14680. (if (not buffer)
  14681. buffer
  14682. (or (buffer-base-buffer buffer)
  14683. buffer)))
  14684. (defun org-trim (s)
  14685. "Remove whitespace at beginning and end of string."
  14686. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  14687. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  14688. s)
  14689. (defun org-wrap (string &optional width lines)
  14690. "Wrap string to either a number of lines, or a width in characters.
  14691. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  14692. that costs. If there is a word longer than WIDTH, the text is actually
  14693. wrapped to the length of that word.
  14694. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  14695. many lines, whatever width that takes.
  14696. The return value is a list of lines, without newlines at the end."
  14697. (let* ((words (org-split-string string "[ \t\n]+"))
  14698. (maxword (apply 'max (mapcar 'org-string-width words)))
  14699. w ll)
  14700. (cond (width
  14701. (org-do-wrap words (max maxword width)))
  14702. (lines
  14703. (setq w maxword)
  14704. (setq ll (org-do-wrap words maxword))
  14705. (if (<= (length ll) lines)
  14706. ll
  14707. (setq ll words)
  14708. (while (> (length ll) lines)
  14709. (setq w (1+ w))
  14710. (setq ll (org-do-wrap words w)))
  14711. ll))
  14712. (t (error "Cannot wrap this")))))
  14713. (defun org-do-wrap (words width)
  14714. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  14715. (let (lines line)
  14716. (while words
  14717. (setq line (pop words))
  14718. (while (and words (< (+ (length line) (length (car words))) width))
  14719. (setq line (concat line " " (pop words))))
  14720. (setq lines (push line lines)))
  14721. (nreverse lines)))
  14722. (defun org-split-string (string &optional separators)
  14723. "Splits STRING into substrings at SEPARATORS.
  14724. No empty strings are returned if there are matches at the beginning
  14725. and end of string."
  14726. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  14727. (start 0)
  14728. notfirst
  14729. (list nil))
  14730. (while (and (string-match rexp string
  14731. (if (and notfirst
  14732. (= start (match-beginning 0))
  14733. (< start (length string)))
  14734. (1+ start) start))
  14735. (< (match-beginning 0) (length string)))
  14736. (setq notfirst t)
  14737. (or (eq (match-beginning 0) 0)
  14738. (and (eq (match-beginning 0) (match-end 0))
  14739. (eq (match-beginning 0) start))
  14740. (setq list
  14741. (cons (substring string start (match-beginning 0))
  14742. list)))
  14743. (setq start (match-end 0)))
  14744. (or (eq start (length string))
  14745. (setq list
  14746. (cons (substring string start)
  14747. list)))
  14748. (nreverse list)))
  14749. (defun org-quote-vert (s)
  14750. "Replace \"|\" with \"\\vert\"."
  14751. (while (string-match "|" s)
  14752. (setq s (replace-match "\\vert" t t s)))
  14753. s)
  14754. (defun org-uuidgen-p (s)
  14755. "Is S an ID created by UUIDGEN?"
  14756. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  14757. (defun org-context ()
  14758. "Return a list of contexts of the current cursor position.
  14759. If several contexts apply, all are returned.
  14760. Each context entry is a list with a symbol naming the context, and
  14761. two positions indicating start and end of the context. Possible
  14762. contexts are:
  14763. :headline anywhere in a headline
  14764. :headline-stars on the leading stars in a headline
  14765. :todo-keyword on a TODO keyword (including DONE) in a headline
  14766. :tags on the TAGS in a headline
  14767. :priority on the priority cookie in a headline
  14768. :item on the first line of a plain list item
  14769. :item-bullet on the bullet/number of a plain list item
  14770. :checkbox on the checkbox in a plain list item
  14771. :table in an org-mode table
  14772. :table-special on a special filed in a table
  14773. :table-table in a table.el table
  14774. :link on a hyperlink
  14775. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  14776. :target on a <<target>>
  14777. :radio-target on a <<<radio-target>>>
  14778. :latex-fragment on a LaTeX fragment
  14779. :latex-preview on a LaTeX fragment with overlayed preview image
  14780. This function expects the position to be visible because it uses font-lock
  14781. faces as a help to recognize the following contexts: :table-special, :link,
  14782. and :keyword."
  14783. (let* ((f (get-text-property (point) 'face))
  14784. (faces (if (listp f) f (list f)))
  14785. (p (point)) clist o)
  14786. ;; First the large context
  14787. (cond
  14788. ((org-on-heading-p t)
  14789. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  14790. (when (progn
  14791. (beginning-of-line 1)
  14792. (looking-at org-todo-line-tags-regexp))
  14793. (push (org-point-in-group p 1 :headline-stars) clist)
  14794. (push (org-point-in-group p 2 :todo-keyword) clist)
  14795. (push (org-point-in-group p 4 :tags) clist))
  14796. (goto-char p)
  14797. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  14798. (if (looking-at "\\[#[A-Z0-9]\\]")
  14799. (push (org-point-in-group p 0 :priority) clist)))
  14800. ((org-at-item-p)
  14801. (push (org-point-in-group p 2 :item-bullet) clist)
  14802. (push (list :item (point-at-bol)
  14803. (save-excursion (org-end-of-item) (point)))
  14804. clist)
  14805. (and (org-at-item-checkbox-p)
  14806. (push (org-point-in-group p 0 :checkbox) clist)))
  14807. ((org-at-table-p)
  14808. (push (list :table (org-table-begin) (org-table-end)) clist)
  14809. (if (memq 'org-formula faces)
  14810. (push (list :table-special
  14811. (previous-single-property-change p 'face)
  14812. (next-single-property-change p 'face)) clist)))
  14813. ((org-at-table-p 'any)
  14814. (push (list :table-table) clist)))
  14815. (goto-char p)
  14816. ;; Now the small context
  14817. (cond
  14818. ((org-at-timestamp-p)
  14819. (push (org-point-in-group p 0 :timestamp) clist))
  14820. ((memq 'org-link faces)
  14821. (push (list :link
  14822. (previous-single-property-change p 'face)
  14823. (next-single-property-change p 'face)) clist))
  14824. ((memq 'org-special-keyword faces)
  14825. (push (list :keyword
  14826. (previous-single-property-change p 'face)
  14827. (next-single-property-change p 'face)) clist))
  14828. ((org-on-target-p)
  14829. (push (org-point-in-group p 0 :target) clist)
  14830. (goto-char (1- (match-beginning 0)))
  14831. (if (looking-at org-radio-target-regexp)
  14832. (push (org-point-in-group p 0 :radio-target) clist))
  14833. (goto-char p))
  14834. ((setq o (car (delq nil
  14835. (mapcar
  14836. (lambda (x)
  14837. (if (memq x org-latex-fragment-image-overlays) x))
  14838. (org-overlays-at (point))))))
  14839. (push (list :latex-fragment
  14840. (org-overlay-start o) (org-overlay-end o)) clist)
  14841. (push (list :latex-preview
  14842. (org-overlay-start o) (org-overlay-end o)) clist))
  14843. ((org-inside-LaTeX-fragment-p)
  14844. ;; FIXME: positions wrong.
  14845. (push (list :latex-fragment (point) (point)) clist)))
  14846. (setq clist (nreverse (delq nil clist)))
  14847. clist))
  14848. ;; FIXME: Compare with at-regexp-p Do we need both?
  14849. (defun org-in-regexp (re &optional nlines visually)
  14850. "Check if point is inside a match of regexp.
  14851. Normally only the current line is checked, but you can include NLINES extra
  14852. lines both before and after point into the search.
  14853. If VISUALLY is set, require that the cursor is not after the match but
  14854. really on, so that the block visually is on the match."
  14855. (catch 'exit
  14856. (let ((pos (point))
  14857. (eol (point-at-eol (+ 1 (or nlines 0))))
  14858. (inc (if visually 1 0)))
  14859. (save-excursion
  14860. (beginning-of-line (- 1 (or nlines 0)))
  14861. (while (re-search-forward re eol t)
  14862. (if (and (<= (match-beginning 0) pos)
  14863. (>= (+ inc (match-end 0)) pos))
  14864. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  14865. (defun org-at-regexp-p (regexp)
  14866. "Is point inside a match of REGEXP in the current line?"
  14867. (catch 'exit
  14868. (save-excursion
  14869. (let ((pos (point)) (end (point-at-eol)))
  14870. (beginning-of-line 1)
  14871. (while (re-search-forward regexp end t)
  14872. (if (and (<= (match-beginning 0) pos)
  14873. (>= (match-end 0) pos))
  14874. (throw 'exit t)))
  14875. nil))))
  14876. (defun org-occur-in-agenda-files (regexp &optional nlines)
  14877. "Call `multi-occur' with buffers for all agenda files."
  14878. (interactive "sOrg-files matching: \np")
  14879. (let* ((files (org-agenda-files))
  14880. (tnames (mapcar 'file-truename files))
  14881. (extra org-agenda-text-search-extra-files)
  14882. f)
  14883. (when (eq (car extra) 'agenda-archives)
  14884. (setq extra (cdr extra))
  14885. (setq files (org-add-archive-files files)))
  14886. (while (setq f (pop extra))
  14887. (unless (member (file-truename f) tnames)
  14888. (add-to-list 'files f 'append)
  14889. (add-to-list 'tnames (file-truename f) 'append)))
  14890. (multi-occur
  14891. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  14892. regexp)))
  14893. (if (boundp 'occur-mode-find-occurrence-hook)
  14894. ;; Emacs 23
  14895. (add-hook 'occur-mode-find-occurrence-hook
  14896. (lambda ()
  14897. (when (org-mode-p)
  14898. (org-reveal))))
  14899. ;; Emacs 22
  14900. (defadvice occur-mode-goto-occurrence
  14901. (after org-occur-reveal activate)
  14902. (and (org-mode-p) (org-reveal)))
  14903. (defadvice occur-mode-goto-occurrence-other-window
  14904. (after org-occur-reveal activate)
  14905. (and (org-mode-p) (org-reveal)))
  14906. (defadvice occur-mode-display-occurrence
  14907. (after org-occur-reveal activate)
  14908. (when (org-mode-p)
  14909. (let ((pos (occur-mode-find-occurrence)))
  14910. (with-current-buffer (marker-buffer pos)
  14911. (save-excursion
  14912. (goto-char pos)
  14913. (org-reveal)))))))
  14914. (defun org-occur-link-in-agenda-files ()
  14915. "Create a link and search for it in the agendas.
  14916. The link is not stored in `org-stored-links', it is just created
  14917. for the search purpose."
  14918. (interactive)
  14919. (let ((link (condition-case nil
  14920. (org-store-link nil)
  14921. (error "Unable to create a link to here"))))
  14922. (org-occur-in-agenda-files (regexp-quote link))))
  14923. (defun org-uniquify (list)
  14924. "Remove duplicate elements from LIST."
  14925. (let (res)
  14926. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  14927. res))
  14928. (defun org-delete-all (elts list)
  14929. "Remove all elements in ELTS from LIST."
  14930. (while elts
  14931. (setq list (delete (pop elts) list)))
  14932. list)
  14933. (defun org-back-over-empty-lines ()
  14934. "Move backwards over whitespace, to the beginning of the first empty line.
  14935. Returns the number of empty lines passed."
  14936. (let ((pos (point)))
  14937. (skip-chars-backward " \t\n\r")
  14938. (beginning-of-line 2)
  14939. (goto-char (min (point) pos))
  14940. (count-lines (point) pos)))
  14941. (defun org-skip-whitespace ()
  14942. (skip-chars-forward " \t\n\r"))
  14943. (defun org-point-in-group (point group &optional context)
  14944. "Check if POINT is in match-group GROUP.
  14945. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  14946. match. If the match group does ot exist or point is not inside it,
  14947. return nil."
  14948. (and (match-beginning group)
  14949. (>= point (match-beginning group))
  14950. (<= point (match-end group))
  14951. (if context
  14952. (list context (match-beginning group) (match-end group))
  14953. t)))
  14954. (defun org-switch-to-buffer-other-window (&rest args)
  14955. "Switch to buffer in a second window on the current frame.
  14956. In particular, do not allow pop-up frames."
  14957. (let (pop-up-frames special-display-buffer-names special-display-regexps
  14958. special-display-function)
  14959. (apply 'switch-to-buffer-other-window args)))
  14960. (defun org-combine-plists (&rest plists)
  14961. "Create a single property list from all plists in PLISTS.
  14962. The process starts by copying the first list, and then setting properties
  14963. from the other lists. Settings in the last list are the most significant
  14964. ones and overrule settings in the other lists."
  14965. (let ((rtn (copy-sequence (pop plists)))
  14966. p v ls)
  14967. (while plists
  14968. (setq ls (pop plists))
  14969. (while ls
  14970. (setq p (pop ls) v (pop ls))
  14971. (setq rtn (plist-put rtn p v))))
  14972. rtn))
  14973. (defun org-move-line-down (arg)
  14974. "Move the current line down. With prefix argument, move it past ARG lines."
  14975. (interactive "p")
  14976. (let ((col (current-column))
  14977. beg end pos)
  14978. (beginning-of-line 1) (setq beg (point))
  14979. (beginning-of-line 2) (setq end (point))
  14980. (beginning-of-line (+ 1 arg))
  14981. (setq pos (move-marker (make-marker) (point)))
  14982. (insert (delete-and-extract-region beg end))
  14983. (goto-char pos)
  14984. (org-move-to-column col)))
  14985. (defun org-move-line-up (arg)
  14986. "Move the current line up. With prefix argument, move it past ARG lines."
  14987. (interactive "p")
  14988. (let ((col (current-column))
  14989. beg end pos)
  14990. (beginning-of-line 1) (setq beg (point))
  14991. (beginning-of-line 2) (setq end (point))
  14992. (beginning-of-line (- arg))
  14993. (setq pos (move-marker (make-marker) (point)))
  14994. (insert (delete-and-extract-region beg end))
  14995. (goto-char pos)
  14996. (org-move-to-column col)))
  14997. (defun org-replace-escapes (string table)
  14998. "Replace %-escapes in STRING with values in TABLE.
  14999. TABLE is an association list with keys like \"%a\" and string values.
  15000. The sequences in STRING may contain normal field width and padding information,
  15001. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  15002. so values can contain further %-escapes if they are define later in TABLE."
  15003. (let ((case-fold-search nil)
  15004. e re rpl)
  15005. (while (setq e (pop table))
  15006. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  15007. (while (string-match re string)
  15008. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  15009. (cdr e)))
  15010. (setq string (replace-match rpl t t string))))
  15011. string))
  15012. (defun org-sublist (list start end)
  15013. "Return a section of LIST, from START to END.
  15014. Counting starts at 1."
  15015. (let (rtn (c start))
  15016. (setq list (nthcdr (1- start) list))
  15017. (while (and list (<= c end))
  15018. (push (pop list) rtn)
  15019. (setq c (1+ c)))
  15020. (nreverse rtn)))
  15021. (defun org-find-base-buffer-visiting (file)
  15022. "Like `find-buffer-visiting' but always return the base buffer and
  15023. not an indirect buffer."
  15024. (let ((buf (or (get-file-buffer file)
  15025. (find-buffer-visiting file))))
  15026. (if buf
  15027. (or (buffer-base-buffer buf) buf)
  15028. nil)))
  15029. (defun org-image-file-name-regexp (&optional extensions)
  15030. "Return regexp matching the file names of images.
  15031. If EXTENSIONS is given, only match these."
  15032. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  15033. (image-file-name-regexp)
  15034. (let ((image-file-name-extensions
  15035. (or extensions
  15036. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  15037. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  15038. (concat "\\."
  15039. (regexp-opt (nconc (mapcar 'upcase
  15040. image-file-name-extensions)
  15041. image-file-name-extensions)
  15042. t)
  15043. "\\'"))))
  15044. (defun org-file-image-p (file &optional extensions)
  15045. "Return non-nil if FILE is an image."
  15046. (save-match-data
  15047. (string-match (org-image-file-name-regexp extensions) file)))
  15048. (defun org-get-cursor-date ()
  15049. "Return the date at cursor in as a time.
  15050. This works in the calendar and in the agenda, anywhere else it just
  15051. returns the current time."
  15052. (let (date day defd)
  15053. (cond
  15054. ((eq major-mode 'calendar-mode)
  15055. (setq date (calendar-cursor-to-date)
  15056. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15057. ((eq major-mode 'org-agenda-mode)
  15058. (setq day (get-text-property (point) 'day))
  15059. (if day
  15060. (setq date (calendar-gregorian-from-absolute day)
  15061. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  15062. (nth 2 date))))))
  15063. (or defd (current-time))))
  15064. (defvar org-agenda-action-marker (make-marker)
  15065. "Marker pointing to the entry for the next agenda action.")
  15066. (defun org-mark-entry-for-agenda-action ()
  15067. "Mark the current entry as target of an agenda action.
  15068. Agenda actions are actions executed from the agenda with the key `k',
  15069. which make use of the date at the cursor."
  15070. (interactive)
  15071. (move-marker org-agenda-action-marker
  15072. (save-excursion (org-back-to-heading t) (point))
  15073. (current-buffer))
  15074. (message
  15075. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  15076. ;;; Paragraph filling stuff.
  15077. ;; We want this to be just right, so use the full arsenal.
  15078. (defun org-indent-line-function ()
  15079. "Indent line like previous, but further if previous was headline or item."
  15080. (interactive)
  15081. (let* ((pos (point))
  15082. (itemp (org-at-item-p))
  15083. (case-fold-search t)
  15084. (org-drawer-regexp (or org-drawer-regexp "\000"))
  15085. column bpos bcol tpos tcol bullet btype bullet-type)
  15086. ;; Find the previous relevant line
  15087. (beginning-of-line 1)
  15088. (cond
  15089. ((looking-at "#") (setq column 0))
  15090. ((looking-at "\\*+ ") (setq column 0))
  15091. ((and (looking-at "[ \t]*:END:")
  15092. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  15093. (save-excursion
  15094. (goto-char (1- (match-beginning 1)))
  15095. (setq column (current-column))))
  15096. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  15097. (save-excursion
  15098. (re-search-backward
  15099. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  15100. (setq column (org-get-indentation (match-string 0))))
  15101. (t
  15102. (beginning-of-line 0)
  15103. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  15104. (not (looking-at "[ \t]*:END:"))
  15105. (not (looking-at org-drawer-regexp)))
  15106. (beginning-of-line 0))
  15107. (cond
  15108. ((looking-at "\\*+[ \t]+")
  15109. (if (not org-adapt-indentation)
  15110. (setq column 0)
  15111. (goto-char (match-end 0))
  15112. (setq column (current-column))))
  15113. ((looking-at org-drawer-regexp)
  15114. (goto-char (1- (match-beginning 1)))
  15115. (setq column (current-column)))
  15116. ((looking-at "\\([ \t]*\\):END:")
  15117. (goto-char (match-end 1))
  15118. (setq column (current-column)))
  15119. ((org-in-item-p)
  15120. (org-beginning-of-item)
  15121. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  15122. (setq bpos (match-beginning 1) tpos (match-end 0)
  15123. bcol (progn (goto-char bpos) (current-column))
  15124. tcol (progn (goto-char tpos) (current-column))
  15125. bullet (match-string 1)
  15126. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  15127. (if (> tcol (+ bcol org-description-max-indent))
  15128. (setq tcol (+ bcol 5)))
  15129. (if (not itemp)
  15130. (setq column tcol)
  15131. (goto-char pos)
  15132. (beginning-of-line 1)
  15133. (if (looking-at "\\S-")
  15134. (progn
  15135. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  15136. (setq bullet (match-string 1)
  15137. btype (if (string-match "[0-9]" bullet) "n" bullet))
  15138. (setq column (if (equal btype bullet-type) bcol tcol)))
  15139. (setq column (org-get-indentation)))))
  15140. (t (setq column (org-get-indentation))))))
  15141. (goto-char pos)
  15142. (if (<= (current-column) (current-indentation))
  15143. (org-indent-line-to column)
  15144. (save-excursion (org-indent-line-to column)))
  15145. (setq column (current-column))
  15146. (beginning-of-line 1)
  15147. (if (looking-at
  15148. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  15149. (replace-match (concat (match-string 1)
  15150. (format org-property-format
  15151. (match-string 2) (match-string 3)))
  15152. t t))
  15153. (org-move-to-column column)))
  15154. (defun org-set-autofill-regexps ()
  15155. (interactive)
  15156. ;; In the paragraph separator we include headlines, because filling
  15157. ;; text in a line directly attached to a headline would otherwise
  15158. ;; fill the headline as well.
  15159. (org-set-local 'comment-start-skip "^#+[ \t]*")
  15160. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  15161. ;; The paragraph starter includes hand-formatted lists.
  15162. (org-set-local
  15163. 'paragraph-start
  15164. (concat
  15165. "\f" "\\|"
  15166. "[ ]*$" "\\|"
  15167. "\\*+ " "\\|"
  15168. "[ \t]*#" "\\|"
  15169. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  15170. "[ \t]*[:|]" "\\|"
  15171. "\\$\\$" "\\|"
  15172. "\\\\\\(begin\\|end\\|[][]\\)"))
  15173. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  15174. ;; But only if the user has not turned off tables or fixed-width regions
  15175. (org-set-local
  15176. 'auto-fill-inhibit-regexp
  15177. (concat "\\*+ \\|#\\+"
  15178. "\\|[ \t]*" org-keyword-time-regexp
  15179. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  15180. (concat
  15181. "\\|[ \t]*["
  15182. (if org-enable-table-editor "|" "")
  15183. (if org-enable-fixed-width-editor ":" "")
  15184. "]"))))
  15185. ;; We use our own fill-paragraph function, to make sure that tables
  15186. ;; and fixed-width regions are not wrapped. That function will pass
  15187. ;; through to `fill-paragraph' when appropriate.
  15188. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  15189. ; Adaptive filling: To get full control, first make sure that
  15190. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  15191. (org-set-local 'adaptive-fill-regexp "\000")
  15192. (org-set-local 'adaptive-fill-function
  15193. 'org-adaptive-fill-function)
  15194. (org-set-local
  15195. 'align-mode-rules-list
  15196. '((org-in-buffer-settings
  15197. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  15198. (modes . '(org-mode))))))
  15199. (defun org-fill-paragraph (&optional justify)
  15200. "Re-align a table, pass through to fill-paragraph if no table."
  15201. (let ((table-p (org-at-table-p))
  15202. (table.el-p (org-at-table.el-p)))
  15203. (cond ((and (equal (char-after (point-at-bol)) ?*)
  15204. (save-excursion (goto-char (point-at-bol))
  15205. (looking-at outline-regexp)))
  15206. t) ; skip headlines
  15207. (table.el-p t) ; skip table.el tables
  15208. (table-p (org-table-align) t) ; align org-mode tables
  15209. (t nil)))) ; call paragraph-fill
  15210. ;; For reference, this is the default value of adaptive-fill-regexp
  15211. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  15212. (defun org-adaptive-fill-function ()
  15213. "Return a fill prefix for org-mode files.
  15214. In particular, this makes sure hanging paragraphs for hand-formatted lists
  15215. work correctly."
  15216. (cond ((looking-at "#[ \t]+")
  15217. (match-string 0))
  15218. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  15219. (save-excursion
  15220. (if (> (match-end 1) (+ (match-beginning 1)
  15221. org-description-max-indent))
  15222. (goto-char (+ (match-beginning 1) 5))
  15223. (goto-char (match-end 0)))
  15224. (make-string (current-column) ?\ )))
  15225. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  15226. (save-excursion
  15227. (goto-char (match-end 0))
  15228. (make-string (current-column) ?\ )))
  15229. (t nil)))
  15230. ;;; Other stuff.
  15231. (defun org-toggle-fixed-width-section (arg)
  15232. "Toggle the fixed-width export.
  15233. If there is no active region, the QUOTE keyword at the current headline is
  15234. inserted or removed. When present, it causes the text between this headline
  15235. and the next to be exported as fixed-width text, and unmodified.
  15236. If there is an active region, this command adds or removes a colon as the
  15237. first character of this line. If the first character of a line is a colon,
  15238. this line is also exported in fixed-width font."
  15239. (interactive "P")
  15240. (let* ((cc 0)
  15241. (regionp (org-region-active-p))
  15242. (beg (if regionp (region-beginning) (point)))
  15243. (end (if regionp (region-end)))
  15244. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  15245. (case-fold-search nil)
  15246. (re "[ \t]*\\(: \\)")
  15247. off)
  15248. (if regionp
  15249. (save-excursion
  15250. (goto-char beg)
  15251. (setq cc (current-column))
  15252. (beginning-of-line 1)
  15253. (setq off (looking-at re))
  15254. (while (> nlines 0)
  15255. (setq nlines (1- nlines))
  15256. (beginning-of-line 1)
  15257. (cond
  15258. (arg
  15259. (org-move-to-column cc t)
  15260. (insert ": \n")
  15261. (forward-line -1))
  15262. ((and off (looking-at re))
  15263. (replace-match "" t t nil 1))
  15264. ((not off) (org-move-to-column cc t) (insert ": ")))
  15265. (forward-line 1)))
  15266. (save-excursion
  15267. (org-back-to-heading)
  15268. (if (looking-at (concat outline-regexp
  15269. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  15270. (replace-match "" t t nil 1)
  15271. (if (looking-at outline-regexp)
  15272. (progn
  15273. (goto-char (match-end 0))
  15274. (insert org-quote-string " "))))))))
  15275. (defun org-reftex-citation ()
  15276. "Use reftex-citation to insert a citation into the buffer.
  15277. This looks for a line like
  15278. #+BIBLIOGRAPHY: foo plain option:-d
  15279. and derives from it that foo.bib is the bbliography file relevant
  15280. for this document. It then installs the necessary environment for RefTeX
  15281. to work in this buffer and calls `reftex-citation' to insert a citation
  15282. into the buffer.
  15283. Export of such citations to both LaTeX and HTML is handled by the contributed
  15284. package org-exp-bibtex by Taru Karttunen."
  15285. (interactive)
  15286. (let ((reftex-docstruct-symbol 'rds)
  15287. (reftex-cite-format "\\cite{%l}")
  15288. rds bib)
  15289. (save-excursion
  15290. (save-restriction
  15291. (widen)
  15292. (let ((case-fold-search t)
  15293. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  15294. (if (not (save-excursion
  15295. (or (re-search-forward re nil t)
  15296. (re-search-backward re nil t))))
  15297. (error "No bibliography defined in file")
  15298. (setq bib (concat (match-string 1) ".bib")
  15299. rds (list (list 'bib bib)))))))
  15300. (call-interactively 'reftex-citation)))
  15301. ;;;; Functions extending outline functionality
  15302. (defun org-beginning-of-line (&optional arg)
  15303. "Go to the beginning of the current line. If that is invisible, continue
  15304. to a visible line beginning. This makes the function of C-a more intuitive.
  15305. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15306. first attempt, and only move to after the tags when the cursor is already
  15307. beyond the end of the headline."
  15308. (interactive "P")
  15309. (let ((pos (point))
  15310. (special (if (consp org-special-ctrl-a/e)
  15311. (car org-special-ctrl-a/e)
  15312. org-special-ctrl-a/e))
  15313. refpos)
  15314. (if (org-bound-and-true-p line-move-visual)
  15315. (beginning-of-visual-line 1)
  15316. (beginning-of-line 1))
  15317. (if (and arg (fboundp 'move-beginning-of-line))
  15318. (call-interactively 'move-beginning-of-line)
  15319. (if (bobp)
  15320. nil
  15321. (backward-char 1)
  15322. (if (org-invisible-p)
  15323. (while (and (not (bobp)) (org-invisible-p))
  15324. (backward-char 1)
  15325. (beginning-of-line 1))
  15326. (forward-char 1))))
  15327. (when special
  15328. (cond
  15329. ((and (looking-at org-complex-heading-regexp)
  15330. (= (char-after (match-end 1)) ?\ ))
  15331. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  15332. (point-at-eol)))
  15333. (goto-char
  15334. (if (eq special t)
  15335. (cond ((> pos refpos) refpos)
  15336. ((= pos (point)) refpos)
  15337. (t (point)))
  15338. (cond ((> pos (point)) (point))
  15339. ((not (eq last-command this-command)) (point))
  15340. (t refpos)))))
  15341. ((org-at-item-p)
  15342. (goto-char
  15343. (if (eq special t)
  15344. (cond ((> pos (match-end 4)) (match-end 4))
  15345. ((= pos (point)) (match-end 4))
  15346. (t (point)))
  15347. (cond ((> pos (point)) (point))
  15348. ((not (eq last-command this-command)) (point))
  15349. (t (match-end 4))))))))
  15350. (org-no-warnings
  15351. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15352. (defun org-end-of-line (&optional arg)
  15353. "Go to the end of the line.
  15354. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15355. first attempt, and only move to after the tags when the cursor is already
  15356. beyond the end of the headline."
  15357. (interactive "P")
  15358. (let ((special (if (consp org-special-ctrl-a/e)
  15359. (cdr org-special-ctrl-a/e)
  15360. org-special-ctrl-a/e)))
  15361. (if (or (not special)
  15362. (not (org-on-heading-p))
  15363. arg)
  15364. (call-interactively
  15365. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  15366. ((fboundp 'move-end-of-line) 'move-end-of-line)
  15367. (t 'end-of-line)))
  15368. (let ((pos (point)))
  15369. (beginning-of-line 1)
  15370. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
  15371. (if (eq special t)
  15372. (if (or (< pos (match-beginning 1))
  15373. (= pos (match-end 0)))
  15374. (goto-char (match-beginning 1))
  15375. (goto-char (match-end 0)))
  15376. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  15377. (goto-char (match-end 0))
  15378. (goto-char (match-beginning 1))))
  15379. (call-interactively (if (fboundp 'move-end-of-line)
  15380. 'move-end-of-line
  15381. 'end-of-line)))))
  15382. (org-no-warnings
  15383. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15384. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  15385. (define-key org-mode-map "\C-e" 'org-end-of-line)
  15386. (define-key org-mode-map [home] 'org-beginning-of-line)
  15387. (define-key org-mode-map [end] 'org-end-of-line)
  15388. (defun org-backward-sentence (&optional arg)
  15389. "Go to beginning of sentence, or beginning of table field.
  15390. This will call `backward-sentence' or `org-table-beginning-of-field',
  15391. depending on context."
  15392. (interactive "P")
  15393. (cond
  15394. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  15395. (t (call-interactively 'backward-sentence))))
  15396. (defun org-forward-sentence (&optional arg)
  15397. "Go to end of sentence, or end of table field.
  15398. This will call `forward-sentence' or `org-table-end-of-field',
  15399. depending on context."
  15400. (interactive "P")
  15401. (cond
  15402. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  15403. (t (call-interactively 'forward-sentence))))
  15404. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  15405. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  15406. (defun org-kill-line (&optional arg)
  15407. "Kill line, to tags or end of line."
  15408. (interactive "P")
  15409. (cond
  15410. ((or (not org-special-ctrl-k)
  15411. (bolp)
  15412. (not (org-on-heading-p)))
  15413. (call-interactively 'kill-line))
  15414. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  15415. (kill-region (point) (match-beginning 1))
  15416. (org-set-tags nil t))
  15417. (t (kill-region (point) (point-at-eol)))))
  15418. (define-key org-mode-map "\C-k" 'org-kill-line)
  15419. (defun org-yank (&optional arg)
  15420. "Yank. If the kill is a subtree, treat it specially.
  15421. This command will look at the current kill and check if is a single
  15422. subtree, or a series of subtrees[1]. If it passes the test, and if the
  15423. cursor is at the beginning of a line or after the stars of a currently
  15424. empty headline, then the yank is handled specially. How exactly depends
  15425. on the value of the following variables, both set by default.
  15426. org-yank-folded-subtrees
  15427. When set, the subtree(s) will be folded after insertion, but only
  15428. if doing so would now swallow text after the yanked text.
  15429. org-yank-adjusted-subtrees
  15430. When set, the subtree will be promoted or demoted in order to
  15431. fit into the local outline tree structure, which means that the level
  15432. will be adjusted so that it becomes the smaller one of the two
  15433. *visible* surrounding headings.
  15434. Any prefix to this command will cause `yank' to be called directly with
  15435. no special treatment. In particular, a simple `C-u' prefix will just
  15436. plainly yank the text as it is.
  15437. \[1] The test checks if the first non-white line is a heading
  15438. and if there are no other headings with fewer stars."
  15439. (interactive "P")
  15440. (org-yank-generic 'yank arg))
  15441. (defun org-yank-generic (command arg)
  15442. "Perform some yank-like command.
  15443. This function implements the behavior described in the `org-yank'
  15444. documentation. However, it has been generalized to work for any
  15445. interactive command with similar behavior."
  15446. ;; pretend to be command COMMAND
  15447. (setq this-command command)
  15448. (if arg
  15449. (call-interactively command)
  15450. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  15451. (and (org-kill-is-subtree-p)
  15452. (or (bolp)
  15453. (and (looking-at "[ \t]*$")
  15454. (string-match
  15455. "\\`\\*+\\'"
  15456. (buffer-substring (point-at-bol) (point)))))))
  15457. swallowp)
  15458. (cond
  15459. ((and subtreep org-yank-folded-subtrees)
  15460. (let ((beg (point))
  15461. end)
  15462. (if (and subtreep org-yank-adjusted-subtrees)
  15463. (org-paste-subtree nil nil 'for-yank)
  15464. (call-interactively command))
  15465. (setq end (point))
  15466. (goto-char beg)
  15467. (when (and (bolp) subtreep
  15468. (not (setq swallowp
  15469. (org-yank-folding-would-swallow-text beg end))))
  15470. (or (looking-at outline-regexp)
  15471. (re-search-forward (concat "^" outline-regexp) end t))
  15472. (while (and (< (point) end) (looking-at outline-regexp))
  15473. (hide-subtree)
  15474. (org-cycle-show-empty-lines 'folded)
  15475. (condition-case nil
  15476. (outline-forward-same-level 1)
  15477. (error (goto-char end)))))
  15478. (when swallowp
  15479. (message
  15480. "Inserted text not folded because that would swallow text"))
  15481. (goto-char end)
  15482. (skip-chars-forward " \t\n\r")
  15483. (beginning-of-line 1)
  15484. (push-mark beg 'nomsg)))
  15485. ((and subtreep org-yank-adjusted-subtrees)
  15486. (let ((beg (point-at-bol)))
  15487. (org-paste-subtree nil nil 'for-yank)
  15488. (push-mark beg 'nomsg)))
  15489. (t
  15490. (call-interactively command))))))
  15491. (defun org-yank-folding-would-swallow-text (beg end)
  15492. "Would hide-subtree at BEG swallow any text after END?"
  15493. (let (level)
  15494. (save-excursion
  15495. (goto-char beg)
  15496. (when (or (looking-at outline-regexp)
  15497. (re-search-forward (concat "^" outline-regexp) end t))
  15498. (setq level (org-outline-level)))
  15499. (goto-char end)
  15500. (skip-chars-forward " \t\r\n\v\f")
  15501. (if (or (eobp)
  15502. (and (bolp) (looking-at org-outline-regexp)
  15503. (<= (org-outline-level) level)))
  15504. nil ; Nothing would be swallowed
  15505. t)))) ; something would swallow
  15506. (define-key org-mode-map "\C-y" 'org-yank)
  15507. (defun org-invisible-p ()
  15508. "Check if point is at a character currently not visible."
  15509. ;; Early versions of noutline don't have `outline-invisible-p'.
  15510. (if (fboundp 'outline-invisible-p)
  15511. (outline-invisible-p)
  15512. (get-char-property (point) 'invisible)))
  15513. (defun org-invisible-p2 ()
  15514. "Check if point is at a character currently not visible."
  15515. (save-excursion
  15516. (if (and (eolp) (not (bobp))) (backward-char 1))
  15517. ;; Early versions of noutline don't have `outline-invisible-p'.
  15518. (if (fboundp 'outline-invisible-p)
  15519. (outline-invisible-p)
  15520. (get-char-property (point) 'invisible))))
  15521. (defun org-back-to-heading (&optional invisible-ok)
  15522. "Call `outline-back-to-heading', but provide a better error message."
  15523. (condition-case nil
  15524. (outline-back-to-heading invisible-ok)
  15525. (error (error "Before first headline at position %d in buffer %s"
  15526. (point) (current-buffer)))))
  15527. (defun org-before-first-heading-p ()
  15528. "Before first heading?"
  15529. (save-excursion
  15530. (null (re-search-backward "^\\*+ " nil t))))
  15531. (defun org-on-heading-p (&optional ignored)
  15532. (outline-on-heading-p t))
  15533. (defun org-at-heading-p (&optional ignored)
  15534. (outline-on-heading-p t))
  15535. (defun org-at-heading-or-item-p ()
  15536. (or (org-on-heading-p) (org-at-item-p)))
  15537. (defun org-on-target-p ()
  15538. (or (org-in-regexp org-radio-target-regexp)
  15539. (org-in-regexp org-target-regexp)))
  15540. (defun org-up-heading-all (arg)
  15541. "Move to the heading line of which the present line is a subheading.
  15542. This function considers both visible and invisible heading lines.
  15543. With argument, move up ARG levels."
  15544. (if (fboundp 'outline-up-heading-all)
  15545. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  15546. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  15547. (defun org-up-heading-safe ()
  15548. "Move to the heading line of which the present line is a subheading.
  15549. This version will not throw an error. It will return the level of the
  15550. headline found, or nil if no higher level is found.
  15551. Also, this function will be a lot faster than `outline-up-heading',
  15552. because it relies on stars being the outline starters. This can really
  15553. make a significant difference in outlines with very many siblings."
  15554. (let (start-level re)
  15555. (org-back-to-heading t)
  15556. (setq start-level (funcall outline-level))
  15557. (if (equal start-level 1)
  15558. nil
  15559. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  15560. (if (re-search-backward re nil t)
  15561. (funcall outline-level)))))
  15562. (defun org-first-sibling-p ()
  15563. "Is this heading the first child of its parents?"
  15564. (interactive)
  15565. (let ((re (concat "^" outline-regexp))
  15566. level l)
  15567. (unless (org-at-heading-p t)
  15568. (error "Not at a heading"))
  15569. (setq level (funcall outline-level))
  15570. (save-excursion
  15571. (if (not (re-search-backward re nil t))
  15572. t
  15573. (setq l (funcall outline-level))
  15574. (< l level)))))
  15575. (defun org-goto-sibling (&optional previous)
  15576. "Goto the next sibling, even if it is invisible.
  15577. When PREVIOUS is set, go to the previous sibling instead. Returns t
  15578. when a sibling was found. When none is found, return nil and don't
  15579. move point."
  15580. (let ((fun (if previous 're-search-backward 're-search-forward))
  15581. (pos (point))
  15582. (re (concat "^" outline-regexp))
  15583. level l)
  15584. (when (condition-case nil (org-back-to-heading t) (error nil))
  15585. (setq level (funcall outline-level))
  15586. (catch 'exit
  15587. (or previous (forward-char 1))
  15588. (while (funcall fun re nil t)
  15589. (setq l (funcall outline-level))
  15590. (when (< l level) (goto-char pos) (throw 'exit nil))
  15591. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  15592. (goto-char pos)
  15593. nil))))
  15594. (defun org-show-siblings ()
  15595. "Show all siblings of the current headline."
  15596. (save-excursion
  15597. (while (org-goto-sibling) (org-flag-heading nil)))
  15598. (save-excursion
  15599. (while (org-goto-sibling 'previous)
  15600. (org-flag-heading nil))))
  15601. (defun org-show-hidden-entry ()
  15602. "Show an entry where even the heading is hidden."
  15603. (save-excursion
  15604. (org-show-entry)))
  15605. (defun org-flag-heading (flag &optional entry)
  15606. "Flag the current heading. FLAG non-nil means make invisible.
  15607. When ENTRY is non-nil, show the entire entry."
  15608. (save-excursion
  15609. (org-back-to-heading t)
  15610. ;; Check if we should show the entire entry
  15611. (if entry
  15612. (progn
  15613. (org-show-entry)
  15614. (save-excursion
  15615. (and (outline-next-heading)
  15616. (org-flag-heading nil))))
  15617. (outline-flag-region (max (point-min) (1- (point)))
  15618. (save-excursion (outline-end-of-heading) (point))
  15619. flag))))
  15620. (defun org-get-next-sibling ()
  15621. "Move to next heading of the same level, and return point.
  15622. If there is no such heading, return nil.
  15623. This is like outline-next-sibling, but invisible headings are ok."
  15624. (let ((level (funcall outline-level)))
  15625. (outline-next-heading)
  15626. (while (and (not (eobp)) (> (funcall outline-level) level))
  15627. (outline-next-heading))
  15628. (if (or (eobp) (< (funcall outline-level) level))
  15629. nil
  15630. (point))))
  15631. (defun org-get-last-sibling ()
  15632. "Move to previous heading of the same level, and return point.
  15633. If there is no such heading, return nil."
  15634. (let ((opoint (point))
  15635. (level (funcall outline-level)))
  15636. (outline-previous-heading)
  15637. (when (and (/= (point) opoint) (outline-on-heading-p t))
  15638. (while (and (> (funcall outline-level) level)
  15639. (not (bobp)))
  15640. (outline-previous-heading))
  15641. (if (< (funcall outline-level) level)
  15642. nil
  15643. (point)))))
  15644. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  15645. ;; This contains an exact copy of the original function, but it uses
  15646. ;; `org-back-to-heading', to make it work also in invisible
  15647. ;; trees. And is uses an invisible-OK argument.
  15648. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  15649. ;; Furthermore, when used inside Org, finding the end of a large subtree
  15650. ;; with many children and grandchildren etc, this can be much faster
  15651. ;; than the outline version.
  15652. (org-back-to-heading invisible-OK)
  15653. (let ((first t)
  15654. (level (funcall outline-level)))
  15655. (if (and (org-mode-p) (< level 1000))
  15656. ;; A true heading (not a plain list item), in Org-mode
  15657. ;; This means we can easily find the end by looking
  15658. ;; only for the right number of stars. Using a regexp to do
  15659. ;; this is so much faster than using a Lisp loop.
  15660. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  15661. (forward-char 1)
  15662. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  15663. ;; something else, do it the slow way
  15664. (while (and (not (eobp))
  15665. (or first (> (funcall outline-level) level)))
  15666. (setq first nil)
  15667. (outline-next-heading)))
  15668. (unless to-heading
  15669. (if (memq (preceding-char) '(?\n ?\^M))
  15670. (progn
  15671. ;; Go to end of line before heading
  15672. (forward-char -1)
  15673. (if (memq (preceding-char) '(?\n ?\^M))
  15674. ;; leave blank line before heading
  15675. (forward-char -1))))))
  15676. (point))
  15677. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  15678. "Use Org version in org-mode, for dramatic speed-up."
  15679. (if (eq major-mode 'org-mode)
  15680. (progn
  15681. (org-end-of-subtree nil t)
  15682. (unless (eobp) (backward-char 1)))
  15683. ad-do-it))
  15684. (defun org-forward-same-level (arg &optional invisible-ok)
  15685. "Move forward to the arg'th subheading at same level as this one.
  15686. Stop at the first and last subheadings of a superior heading."
  15687. (interactive "p")
  15688. (org-back-to-heading invisible-ok)
  15689. (org-on-heading-p)
  15690. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15691. (re (format "^\\*\\{1,%d\\} " level))
  15692. l)
  15693. (forward-char 1)
  15694. (while (> arg 0)
  15695. (while (and (re-search-forward re nil 'move)
  15696. (setq l (- (match-end 0) (match-beginning 0) 1))
  15697. (= l level)
  15698. (not invisible-ok)
  15699. (org-invisible-p))
  15700. (if (< l level) (setq arg 1)))
  15701. (setq arg (1- arg)))
  15702. (beginning-of-line 1)))
  15703. (defun org-backward-same-level (arg &optional invisible-ok)
  15704. "Move backward to the arg'th subheading at same level as this one.
  15705. Stop at the first and last subheadings of a superior heading."
  15706. (interactive "p")
  15707. (org-back-to-heading)
  15708. (org-on-heading-p)
  15709. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15710. (re (format "^\\*\\{1,%d\\} " level))
  15711. l)
  15712. (while (> arg 0)
  15713. (while (and (re-search-backward re nil 'move)
  15714. (setq l (- (match-end 0) (match-beginning 0) 1))
  15715. (= l level)
  15716. (not invisible-ok)
  15717. (org-invisible-p))
  15718. (if (< l level) (setq arg 1)))
  15719. (setq arg (1- arg)))))
  15720. (defun org-show-subtree ()
  15721. "Show everything after this heading at deeper levels."
  15722. (outline-flag-region
  15723. (point)
  15724. (save-excursion
  15725. (org-end-of-subtree t t))
  15726. nil))
  15727. (defun org-show-entry ()
  15728. "Show the body directly following this heading.
  15729. Show the heading too, if it is currently invisible."
  15730. (interactive)
  15731. (save-excursion
  15732. (condition-case nil
  15733. (progn
  15734. (org-back-to-heading t)
  15735. (outline-flag-region
  15736. (max (point-min) (1- (point)))
  15737. (save-excursion
  15738. (if (re-search-forward
  15739. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  15740. (match-beginning 1)
  15741. (point-max)))
  15742. nil)
  15743. (org-cycle-hide-drawers 'children))
  15744. (error nil))))
  15745. (defun org-make-options-regexp (kwds &optional extra)
  15746. "Make a regular expression for keyword lines."
  15747. (concat
  15748. "^"
  15749. "#?[ \t]*\\+\\("
  15750. (mapconcat 'regexp-quote kwds "\\|")
  15751. (if extra (concat "\\|" extra))
  15752. "\\):[ \t]*"
  15753. "\\(.*\\)"))
  15754. ;; Make isearch reveal the necessary context
  15755. (defun org-isearch-end ()
  15756. "Reveal context after isearch exits."
  15757. (when isearch-success ; only if search was successful
  15758. (if (featurep 'xemacs)
  15759. ;; Under XEmacs, the hook is run in the correct place,
  15760. ;; we directly show the context.
  15761. (org-show-context 'isearch)
  15762. ;; In Emacs the hook runs *before* restoring the overlays.
  15763. ;; So we have to use a one-time post-command-hook to do this.
  15764. ;; (Emacs 22 has a special variable, see function `org-mode')
  15765. (unless (and (boundp 'isearch-mode-end-hook-quit)
  15766. isearch-mode-end-hook-quit)
  15767. ;; Only when the isearch was not quitted.
  15768. (org-add-hook 'post-command-hook 'org-isearch-post-command
  15769. 'append 'local)))))
  15770. (defun org-isearch-post-command ()
  15771. "Remove self from hook, and show context."
  15772. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  15773. (org-show-context 'isearch))
  15774. ;;;; Integration with and fixes for other packages
  15775. ;;; Imenu support
  15776. (defvar org-imenu-markers nil
  15777. "All markers currently used by Imenu.")
  15778. (make-variable-buffer-local 'org-imenu-markers)
  15779. (defun org-imenu-new-marker (&optional pos)
  15780. "Return a new marker for use by Imenu, and remember the marker."
  15781. (let ((m (make-marker)))
  15782. (move-marker m (or pos (point)))
  15783. (push m org-imenu-markers)
  15784. m))
  15785. (defun org-imenu-get-tree ()
  15786. "Produce the index for Imenu."
  15787. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  15788. (setq org-imenu-markers nil)
  15789. (let* ((n org-imenu-depth)
  15790. (re (concat "^" outline-regexp))
  15791. (subs (make-vector (1+ n) nil))
  15792. (last-level 0)
  15793. m level head)
  15794. (save-excursion
  15795. (save-restriction
  15796. (widen)
  15797. (goto-char (point-max))
  15798. (while (re-search-backward re nil t)
  15799. (setq level (org-reduced-level (funcall outline-level)))
  15800. (when (<= level n)
  15801. (looking-at org-complex-heading-regexp)
  15802. (setq head (org-link-display-format
  15803. (org-match-string-no-properties 4))
  15804. m (org-imenu-new-marker))
  15805. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  15806. (if (>= level last-level)
  15807. (push (cons head m) (aref subs level))
  15808. (push (cons head (aref subs (1+ level))) (aref subs level))
  15809. (loop for i from (1+ level) to n do (aset subs i nil)))
  15810. (setq last-level level)))))
  15811. (aref subs 1)))
  15812. (eval-after-load "imenu"
  15813. '(progn
  15814. (add-hook 'imenu-after-jump-hook
  15815. (lambda ()
  15816. (if (eq major-mode 'org-mode)
  15817. (org-show-context 'org-goto))))))
  15818. (defun org-link-display-format (link)
  15819. "Replace a link with either the description, or the link target
  15820. if no description is present"
  15821. (save-match-data
  15822. (if (string-match org-bracket-link-analytic-regexp link)
  15823. (replace-match (or (match-string 5 link)
  15824. (concat (match-string 1 link)
  15825. (match-string 3 link)))
  15826. nil nil link)
  15827. link)))
  15828. ;; Speedbar support
  15829. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  15830. "Overlay marking the agenda restriction line in speedbar.")
  15831. (org-overlay-put org-speedbar-restriction-lock-overlay
  15832. 'face 'org-agenda-restriction-lock)
  15833. (org-overlay-put org-speedbar-restriction-lock-overlay
  15834. 'help-echo "Agendas are currently limited to this item.")
  15835. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  15836. (defun org-speedbar-set-agenda-restriction ()
  15837. "Restrict future agenda commands to the location at point in speedbar.
  15838. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  15839. (interactive)
  15840. (require 'org-agenda)
  15841. (let (p m tp np dir txt)
  15842. (cond
  15843. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15844. 'org-imenu t))
  15845. (setq m (get-text-property p 'org-imenu-marker))
  15846. (save-excursion
  15847. (save-restriction
  15848. (set-buffer (marker-buffer m))
  15849. (goto-char m)
  15850. (org-agenda-set-restriction-lock 'subtree))))
  15851. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15852. 'speedbar-function 'speedbar-find-file))
  15853. (setq tp (previous-single-property-change
  15854. (1+ p) 'speedbar-function)
  15855. np (next-single-property-change
  15856. tp 'speedbar-function)
  15857. dir (speedbar-line-directory)
  15858. txt (buffer-substring-no-properties (or tp (point-min))
  15859. (or np (point-max))))
  15860. (save-excursion
  15861. (save-restriction
  15862. (set-buffer (find-file-noselect
  15863. (let ((default-directory dir))
  15864. (expand-file-name txt))))
  15865. (unless (org-mode-p)
  15866. (error "Cannot restrict to non-Org-mode file"))
  15867. (org-agenda-set-restriction-lock 'file))))
  15868. (t (error "Don't know how to restrict Org-mode's agenda")))
  15869. (org-move-overlay org-speedbar-restriction-lock-overlay
  15870. (point-at-bol) (point-at-eol))
  15871. (setq current-prefix-arg nil)
  15872. (org-agenda-maybe-redo)))
  15873. (eval-after-load "speedbar"
  15874. '(progn
  15875. (speedbar-add-supported-extension ".org")
  15876. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  15877. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  15878. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  15879. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15880. (add-hook 'speedbar-visiting-tag-hook
  15881. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  15882. ;;; Fixes and Hacks for problems with other packages
  15883. ;; Make flyspell not check words in links, to not mess up our keymap
  15884. (defun org-mode-flyspell-verify ()
  15885. "Don't let flyspell put overlays at active buttons."
  15886. (and (not (get-text-property (point) 'keymap))
  15887. (not (get-text-property (point) 'org-no-flyspell))))
  15888. (defun org-remove-flyspell-overlays-in (beg end)
  15889. "Remove flyspell overlays in region."
  15890. (and (org-bound-and-true-p flyspell-mode)
  15891. (fboundp 'flyspell-delete-region-overlays)
  15892. (flyspell-delete-region-overlays beg end))
  15893. (add-text-properties beg end '(org-no-flyspell t)))
  15894. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  15895. (eval-after-load "bookmark"
  15896. '(if (boundp 'bookmark-after-jump-hook)
  15897. ;; We can use the hook
  15898. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15899. ;; Hook not available, use advice
  15900. (defadvice bookmark-jump (after org-make-visible activate)
  15901. "Make the position visible."
  15902. (org-bookmark-jump-unhide))))
  15903. ;; Make sure saveplace shows the location if it was hidden
  15904. (eval-after-load "saveplace"
  15905. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  15906. "Make the position visible."
  15907. (org-bookmark-jump-unhide)))
  15908. ;; Make sure ecb shows the location if it was hidden
  15909. (eval-after-load "ecb"
  15910. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  15911. "Make hierarchy visible when jumping into location from ECB tree buffer."
  15912. (if (eq major-mode 'org-mode)
  15913. (org-show-context))))
  15914. (defun org-bookmark-jump-unhide ()
  15915. "Unhide the current position, to show the bookmark location."
  15916. (and (org-mode-p)
  15917. (or (org-invisible-p)
  15918. (save-excursion (goto-char (max (point-min) (1- (point))))
  15919. (org-invisible-p)))
  15920. (org-show-context 'bookmark-jump)))
  15921. ;; Make session.el ignore our circular variable
  15922. (eval-after-load "session"
  15923. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  15924. ;;;; Experimental code
  15925. (defun org-closed-in-range ()
  15926. "Sparse tree of items closed in a certain time range.
  15927. Still experimental, may disappear in the future."
  15928. (interactive)
  15929. ;; Get the time interval from the user.
  15930. (let* ((time1 (org-float-time
  15931. (org-read-date nil 'to-time nil "Starting date: ")))
  15932. (time2 (org-float-time
  15933. (org-read-date nil 'to-time nil "End date:")))
  15934. ;; callback function
  15935. (callback (lambda ()
  15936. (let ((time
  15937. (org-float-time
  15938. (apply 'encode-time
  15939. (org-parse-time-string
  15940. (match-string 1))))))
  15941. ;; check if time in interval
  15942. (and (>= time time1) (<= time time2))))))
  15943. ;; make tree, check each match with the callback
  15944. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  15945. ;;;; Finish up
  15946. (provide 'org)
  15947. (run-hooks 'org-load-hook)
  15948. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  15949. ;;; org.el ends here