org.el 660 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720
  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.32trans
  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.32trans"
  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. (with-current-buffer "*Shell Command Output*"
  104. (goto-char (point-min))
  105. (re-search-forward "[^\n]+")
  106. (setq git-version (match-string 0))
  107. (subst-char-in-string ?- ?. git-version t)
  108. (shell-command "git diff-index --name-only HEAD --")
  109. (unless (eql 1 (point-max))
  110. (setq git-version (concat git-version ".dirty")))
  111. (setq version (concat version " (" git-version ")")))
  112. (cd pwd))))
  113. (setq version (format "Org-mode version %s" version))
  114. (if here (insert version))
  115. (message version)
  116. version))
  117. ;;; Compatibility constants
  118. ;;; The custom variables
  119. (defgroup org nil
  120. "Outline-based notes management and organizer."
  121. :tag "Org"
  122. :group 'outlines
  123. :group 'hypermedia
  124. :group 'calendar)
  125. (defcustom org-mode-hook nil
  126. "Mode hook for Org-mode, run after the mode was turned on."
  127. :group 'org
  128. :type 'hook)
  129. (defcustom org-load-hook nil
  130. "Hook that is run after org.el has been loaded."
  131. :group 'org
  132. :type 'hook)
  133. (defvar org-modules) ; defined below
  134. (defvar org-modules-loaded nil
  135. "Have the modules been loaded already?")
  136. (defun org-load-modules-maybe (&optional force)
  137. "Load all extensions listed in `org-modules'."
  138. (when (or force (not org-modules-loaded))
  139. (mapc (lambda (ext)
  140. (condition-case nil (require ext)
  141. (error (message "Problems while trying to load feature `%s'" ext))))
  142. org-modules)
  143. (setq org-modules-loaded t)))
  144. (defun org-set-modules (var value)
  145. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  146. (set var value)
  147. (when (featurep 'org)
  148. (org-load-modules-maybe 'force)))
  149. (when (org-bound-and-true-p org-modules)
  150. (let ((a (member 'org-infojs org-modules)))
  151. (and a (setcar a 'org-jsinfo))))
  152. (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)
  153. "Modules that should always be loaded together with org.el.
  154. If a description starts with <C>, the file is not part of Emacs
  155. and loading it will require that you have downloaded and properly installed
  156. the org-mode distribution.
  157. You can also use this system to load external packages (i.e. neither Org
  158. core modules, nor modules from the CONTRIB directory). Just add symbols
  159. to the end of the list. If the package is called org-xyz.el, then you need
  160. to add the symbol `xyz', and the package must have a call to
  161. (provide 'org-xyz)"
  162. :group 'org
  163. :set 'org-set-modules
  164. :type
  165. '(set :greedy t
  166. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  167. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  168. (const :tag " crypt: Encryption of subtrees" org-crypt)
  169. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  170. (const :tag " id: Global IDs for identifying entries" org-id)
  171. (const :tag " info: Links to Info nodes" org-info)
  172. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  173. (const :tag " habit: Track your consistency with habits" org-habit)
  174. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  175. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  176. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  177. (const :tag " mew Links to Mew folders/messages" org-mew)
  178. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  179. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  180. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  181. (const :tag " vm: Links to VM folders/messages" org-vm)
  182. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  183. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  184. (const :tag " mouse: Additional mouse support" org-mouse)
  185. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  186. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  187. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  188. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  189. (const :tag "C collector: Collect properties into tables" org-collector)
  190. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  191. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  192. (const :tag "C eval: Include command output as text" org-eval)
  193. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  194. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  195. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  196. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  197. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  198. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  199. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  200. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  201. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  202. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  203. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  204. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  205. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  206. (const :tag "C R: Computation using the R language" org-R)
  207. (const :tag "C registry: A registry for Org-mode links" org-registry)
  208. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  209. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  210. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  211. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  212. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  213. (const :tag "C track: Keep up with Org-mode development" org-track)
  214. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  215. (defcustom org-support-shift-select nil
  216. "Non-nil means, make shift-cursor commands select text when possible.
  217. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  218. selecting a region, or enlarge thusly regions started in this way.
  219. In Org-mode, in special contexts, these same keys are used for other
  220. purposes, important enough to compete with shift selection. Org tries
  221. to balance these needs by supporting `shift-select-mode' outside these
  222. special contexts, under control of this variable.
  223. The default of this variable is nil, to avoid confusing behavior. Shifted
  224. cursor keys will then execute Org commands in the following contexts:
  225. - on a headline, changing TODO state (left/right) and priority (up/down)
  226. - on a time stamp, changing the time
  227. - in a plain list item, changing the bullet type
  228. - in a property definition line, switching between allowed values
  229. - in the BEGIN line of a clock table (changing the time block).
  230. Outside these contexts, the commands will throw an error.
  231. When this variable is t and the cursor is not in a special context,
  232. Org-mode will support shift-selection for making and enlarging regions.
  233. To make this more effective, the bullet cycling will no longer happen
  234. anywhere in an item line, but only if the cursor is exactly on the bullet.
  235. If you set this variable to the symbol `always', then the keys
  236. will not be special in headlines, property lines, and item lines, to make
  237. shift selection work there as well. If this is what you want, you can
  238. use the following alternative commands: `C-c C-t' and `C-c ,' to
  239. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  240. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  241. edited by hand or in column view.
  242. However, when the cursor is on a timestamp, shift-cursor commands
  243. will still edit the time stamp - this is just too good to give up.
  244. XEmacs user should have this variable set to nil, because shift-select-mode
  245. is Emacs 23 only."
  246. :group 'org
  247. :type '(choice
  248. (const :tag "Never" nil)
  249. (const :tag "When outside special context" t)
  250. (const :tag "Everywhere except timestamps" always)))
  251. (defgroup org-startup nil
  252. "Options concerning startup of Org-mode."
  253. :tag "Org Startup"
  254. :group 'org)
  255. (defcustom org-startup-folded t
  256. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  257. This can also be configured on a per-file basis by adding one of
  258. the following lines anywhere in the buffer:
  259. #+STARTUP: fold (or `overview', this is equivalent)
  260. #+STARTUP: nofold (or `showall', this is equivalent)
  261. #+STARTUP: content
  262. #+STARTUP: showeverything"
  263. :group 'org-startup
  264. :type '(choice
  265. (const :tag "nofold: show all" nil)
  266. (const :tag "fold: overview" t)
  267. (const :tag "content: all headlines" content)
  268. (const :tag "show everything, even drawers" showeverything)))
  269. (defcustom org-startup-truncated t
  270. "Non-nil means, entering Org-mode will set `truncate-lines'.
  271. This is useful since some lines containing links can be very long and
  272. uninteresting. Also tables look terrible when wrapped."
  273. :group 'org-startup
  274. :type 'boolean)
  275. (defcustom org-startup-indented nil
  276. "Non-nil means, turn on `org-indent-mode' on startup.
  277. This can also be configured on a per-file basis by adding one of
  278. the following lines anywhere in the buffer:
  279. #+STARTUP: indent
  280. #+STARTUP: noindent"
  281. :group 'org-structure
  282. :type '(choice
  283. (const :tag "Not" nil)
  284. (const :tag "Globally (slow on startup in large files)" t)))
  285. (defcustom org-startup-align-all-tables nil
  286. "Non-nil means, align all tables when visiting a file.
  287. This is useful when the column width in tables is forced with <N> cookies
  288. in table fields. Such tables will look correct only after the first re-align.
  289. This can also be configured on a per-file basis by adding one of
  290. the following lines anywhere in the buffer:
  291. #+STARTUP: align
  292. #+STARTUP: noalign"
  293. :group 'org-startup
  294. :type 'boolean)
  295. (defcustom org-insert-mode-line-in-empty-file nil
  296. "Non-nil means insert the first line setting Org-mode in empty files.
  297. When the function `org-mode' is called interactively in an empty file, this
  298. normally means that the file name does not automatically trigger Org-mode.
  299. To ensure that the file will always be in Org-mode in the future, a
  300. line enforcing Org-mode will be inserted into the buffer, if this option
  301. has been set."
  302. :group 'org-startup
  303. :type 'boolean)
  304. (defcustom org-replace-disputed-keys nil
  305. "Non-nil means use alternative key bindings for some keys.
  306. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  307. These keys are also used by other packages like shift-selection-mode'
  308. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  309. If you want to use Org-mode together with one of these other modes,
  310. or more generally if you would like to move some Org-mode commands to
  311. other keys, set this variable and configure the keys with the variable
  312. `org-disputed-keys'.
  313. This option is only relevant at load-time of Org-mode, and must be set
  314. *before* org.el is loaded. Changing it requires a restart of Emacs to
  315. become effective."
  316. :group 'org-startup
  317. :type 'boolean)
  318. (defcustom org-use-extra-keys nil
  319. "Non-nil means use extra key sequence definitions for certain
  320. commands. This happens automatically if you run XEmacs or if
  321. window-system is nil. This variable lets you do the same
  322. manually. You must set it before loading org.
  323. Example: on Carbon Emacs 22 running graphically, with an external
  324. keyboard on a Powerbook, the default way of setting M-left might
  325. not work for either Alt or ESC. Setting this variable will make
  326. it work for ESC."
  327. :group 'org-startup
  328. :type 'boolean)
  329. (if (fboundp 'defvaralias)
  330. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  331. (defcustom org-disputed-keys
  332. '(([(shift up)] . [(meta p)])
  333. ([(shift down)] . [(meta n)])
  334. ([(shift left)] . [(meta -)])
  335. ([(shift right)] . [(meta +)])
  336. ([(control shift right)] . [(meta shift +)])
  337. ([(control shift left)] . [(meta shift -)]))
  338. "Keys for which Org-mode and other modes compete.
  339. This is an alist, cars are the default keys, second element specifies
  340. the alternative to use when `org-replace-disputed-keys' is t.
  341. Keys can be specified in any syntax supported by `define-key'.
  342. The value of this option takes effect only at Org-mode's startup,
  343. therefore you'll have to restart Emacs to apply it after changing."
  344. :group 'org-startup
  345. :type 'alist)
  346. (defun org-key (key)
  347. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  348. Or return the original if not disputed."
  349. (if org-replace-disputed-keys
  350. (let* ((nkey (key-description key))
  351. (x (org-find-if (lambda (x)
  352. (equal (key-description (car x)) nkey))
  353. org-disputed-keys)))
  354. (if x (cdr x) key))
  355. key))
  356. (defun org-find-if (predicate seq)
  357. (catch 'exit
  358. (while seq
  359. (if (funcall predicate (car seq))
  360. (throw 'exit (car seq))
  361. (pop seq)))))
  362. (defun org-defkey (keymap key def)
  363. "Define a key, possibly translated, as returned by `org-key'."
  364. (define-key keymap (org-key key) def))
  365. (defcustom org-ellipsis nil
  366. "The ellipsis to use in the Org-mode outline.
  367. When nil, just use the standard three dots. When a string, use that instead,
  368. When a face, use the standard 3 dots, but with the specified face.
  369. The change affects only Org-mode (which will then use its own display table).
  370. Changing this requires executing `M-x org-mode' in a buffer to become
  371. effective."
  372. :group 'org-startup
  373. :type '(choice (const :tag "Default" nil)
  374. (face :tag "Face" :value org-warning)
  375. (string :tag "String" :value "...#")))
  376. (defvar org-display-table nil
  377. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  378. (defgroup org-keywords nil
  379. "Keywords in Org-mode."
  380. :tag "Org Keywords"
  381. :group 'org)
  382. (defcustom org-deadline-string "DEADLINE:"
  383. "String to mark deadline entries.
  384. A deadline is this string, followed by a time stamp. Should be a word,
  385. terminated by a colon. You can insert a schedule keyword and
  386. a timestamp with \\[org-deadline].
  387. Changes become only effective after restarting Emacs."
  388. :group 'org-keywords
  389. :type 'string)
  390. (defcustom org-scheduled-string "SCHEDULED:"
  391. "String to mark scheduled TODO entries.
  392. A schedule is this string, followed by a time stamp. Should be a word,
  393. terminated by a colon. You can insert a schedule keyword and
  394. a timestamp with \\[org-schedule].
  395. Changes become only effective after restarting Emacs."
  396. :group 'org-keywords
  397. :type 'string)
  398. (defcustom org-closed-string "CLOSED:"
  399. "String used as the prefix for timestamps logging closing a TODO entry."
  400. :group 'org-keywords
  401. :type 'string)
  402. (defcustom org-clock-string "CLOCK:"
  403. "String used as prefix for timestamps clocking work hours on an item."
  404. :group 'org-keywords
  405. :type 'string)
  406. (defcustom org-comment-string "COMMENT"
  407. "Entries starting with this keyword will never be exported.
  408. An entry can be toggled between COMMENT and normal with
  409. \\[org-toggle-comment].
  410. Changes become only effective after restarting Emacs."
  411. :group 'org-keywords
  412. :type 'string)
  413. (defcustom org-quote-string "QUOTE"
  414. "Entries starting with this keyword will be exported in fixed-width font.
  415. Quoting applies only to the text in the entry following the headline, and does
  416. not extend beyond the next headline, even if that is lower level.
  417. An entry can be toggled between QUOTE and normal with
  418. \\[org-toggle-fixed-width-section]."
  419. :group 'org-keywords
  420. :type 'string)
  421. (defconst org-repeat-re
  422. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  423. "Regular expression for specifying repeated events.
  424. After a match, group 1 contains the repeat expression.")
  425. (defgroup org-structure nil
  426. "Options concerning the general structure of Org-mode files."
  427. :tag "Org Structure"
  428. :group 'org)
  429. (defgroup org-reveal-location nil
  430. "Options about how to make context of a location visible."
  431. :tag "Org Reveal Location"
  432. :group 'org-structure)
  433. (defconst org-context-choice
  434. '(choice
  435. (const :tag "Always" t)
  436. (const :tag "Never" nil)
  437. (repeat :greedy t :tag "Individual contexts"
  438. (cons
  439. (choice :tag "Context"
  440. (const agenda)
  441. (const org-goto)
  442. (const occur-tree)
  443. (const tags-tree)
  444. (const link-search)
  445. (const mark-goto)
  446. (const bookmark-jump)
  447. (const isearch)
  448. (const default))
  449. (boolean))))
  450. "Contexts for the reveal options.")
  451. (defcustom org-show-hierarchy-above '((default . t))
  452. "Non-nil means, show full hierarchy when revealing a location.
  453. Org-mode often shows locations in an org-mode file which might have
  454. been invisible before. When this is set, the hierarchy of headings
  455. above the exposed location is shown.
  456. Turning this off for example for sparse trees makes them very compact.
  457. Instead of t, this can also be an alist specifying this option for different
  458. contexts. Valid contexts are
  459. agenda when exposing an entry from the agenda
  460. org-goto when using the command `org-goto' on key C-c C-j
  461. occur-tree when using the command `org-occur' on key C-c /
  462. tags-tree when constructing a sparse tree based on tags matches
  463. link-search when exposing search matches associated with a link
  464. mark-goto when exposing the jump goal of a mark
  465. bookmark-jump when exposing a bookmark location
  466. isearch when exiting from an incremental search
  467. default default for all contexts not set explicitly"
  468. :group 'org-reveal-location
  469. :type org-context-choice)
  470. (defcustom org-show-following-heading '((default . nil))
  471. "Non-nil means, show following heading when revealing a location.
  472. Org-mode often shows locations in an org-mode file which might have
  473. been invisible before. When this is set, the heading following the
  474. match is shown.
  475. Turning this off for example for sparse trees makes them very compact,
  476. but makes it harder to edit the location of the match. In such a case,
  477. use the command \\[org-reveal] to show more context.
  478. Instead of t, this can also be an alist specifying this option for different
  479. contexts. See `org-show-hierarchy-above' for valid contexts."
  480. :group 'org-reveal-location
  481. :type org-context-choice)
  482. (defcustom org-show-siblings '((default . nil) (isearch t))
  483. "Non-nil means, show all sibling heading when revealing a location.
  484. Org-mode often shows locations in an org-mode file which might have
  485. been invisible before. When this is set, the sibling of the current entry
  486. heading are all made visible. If `org-show-hierarchy-above' is t,
  487. the same happens on each level of the hierarchy above the current entry.
  488. By default this is on for the isearch context, off for all other contexts.
  489. Turning this off for example for sparse trees makes them very compact,
  490. but makes it harder to edit the location of the match. In such a case,
  491. use the command \\[org-reveal] to show more context.
  492. Instead of t, this can also be an alist specifying this option for different
  493. contexts. See `org-show-hierarchy-above' for valid contexts."
  494. :group 'org-reveal-location
  495. :type org-context-choice)
  496. (defcustom org-show-entry-below '((default . nil))
  497. "Non-nil means, show the entry below a headline when revealing a location.
  498. Org-mode often shows locations in an org-mode file which might have
  499. been invisible before. When this is set, the text below the headline that is
  500. exposed is also shown.
  501. By default this is off for all contexts.
  502. Instead of t, this can also be an alist specifying this option for different
  503. contexts. See `org-show-hierarchy-above' for valid contexts."
  504. :group 'org-reveal-location
  505. :type org-context-choice)
  506. (defcustom org-indirect-buffer-display 'other-window
  507. "How should indirect tree buffers be displayed?
  508. This applies to indirect buffers created with the commands
  509. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  510. Valid values are:
  511. current-window Display in the current window
  512. other-window Just display in another window.
  513. dedicated-frame Create one new frame, and re-use it each time.
  514. new-frame Make a new frame each time. Note that in this case
  515. previously-made indirect buffers are kept, and you need to
  516. kill these buffers yourself."
  517. :group 'org-structure
  518. :group 'org-agenda-windows
  519. :type '(choice
  520. (const :tag "In current window" current-window)
  521. (const :tag "In current frame, other window" other-window)
  522. (const :tag "Each time a new frame" new-frame)
  523. (const :tag "One dedicated frame" dedicated-frame)))
  524. (defcustom org-use-speed-commands nil
  525. "Non-nil means, activate single letter commands at beginning of a headline."
  526. :group 'org-structure
  527. :type 'boolean)
  528. (defcustom org-speed-commands-user nil
  529. "Alist of additional speed commands.
  530. This list will be checked before `org-speed-commands-default'
  531. when the variable `org-use-speed-commands' is non-nil
  532. and when the cursor is at the beginning of a headline.
  533. The car if each entry is a string with a single letter, which must
  534. be assigned to `self-insert-command' in the global map.
  535. The cdr is either a command to be called interactively, a function
  536. to be called, or a form to be evaluated."
  537. :group 'org-structure
  538. :type '(repeat
  539. (cons
  540. (string "Command letter")
  541. (choice
  542. (function)
  543. (sexp)))))
  544. (defgroup org-cycle nil
  545. "Options concerning visibility cycling in Org-mode."
  546. :tag "Org Cycle"
  547. :group 'org-structure)
  548. (defcustom org-cycle-skip-children-state-if-no-children t
  549. "Non-nil means, skip CHILDREN state in entries that don't have any."
  550. :group 'org-cycle
  551. :type 'boolean)
  552. (defcustom org-cycle-max-level nil
  553. "Maximum level which should still be subject to visibility cycling.
  554. Levels higher than this will, for cycling, be treated as text, not a headline.
  555. When `org-odd-levels-only' is set, a value of N in this variable actually
  556. means 2N-1 stars as the limiting headline.
  557. When nil, cycle all levels.
  558. Note that the limiting level of cycling is also influenced by
  559. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  560. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  561. than its value."
  562. :group 'org-cycle
  563. :type '(choice
  564. (const :tag "No limit" nil)
  565. (integer :tag "Maximum level")))
  566. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  567. "Names of drawers. Drawers are not opened by cycling on the headline above.
  568. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  569. this:
  570. :DRAWERNAME:
  571. .....
  572. :END:
  573. The drawer \"PROPERTIES\" is special for capturing properties through
  574. the property API.
  575. Drawers can be defined on the per-file basis with a line like:
  576. #+DRAWERS: HIDDEN STATE PROPERTIES"
  577. :group 'org-structure
  578. :group 'org-cycle
  579. :type '(repeat (string :tag "Drawer Name")))
  580. (defcustom org-hide-block-startup nil
  581. "Non-nil means, , entering Org-mode will fold all blocks.
  582. This can also be set in on a per-file basis with
  583. #+STARTUP: hideblocks
  584. #+STARTUP: showblocks"
  585. :group 'org-startup
  586. :group 'org-cycle
  587. :type 'boolean)
  588. (defcustom org-cycle-global-at-bob nil
  589. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  590. This makes it possible to do global cycling without having to use S-TAB or
  591. C-u TAB. For this special case to work, the first line of the buffer
  592. must not be a headline - it may be empty or some other text. When used in
  593. this way, `org-cycle-hook' is disables temporarily, to make sure the
  594. cursor stays at the beginning of the buffer.
  595. When this option is nil, don't do anything special at the beginning
  596. of the buffer."
  597. :group 'org-cycle
  598. :type 'boolean)
  599. (defcustom org-cycle-level-after-item/entry-creation t
  600. "Non-nil means, cycle entry level or item indentation in new empty entries.
  601. When the cursor is at the end of an empty headline, i.e with only stars
  602. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  603. and then all possible anchestor states, before returning to the original state.
  604. This makes data entry extremely fast: M-RET to create a new hedline,
  605. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  606. When the cursor is at the end of an empty plain list item, one TAB will
  607. make it a subitem, two or more tabs will back up to make this an item
  608. higher up in the item hierarchy."
  609. :group 'org-cycle
  610. :type 'boolean)
  611. (defcustom org-cycle-emulate-tab t
  612. "Where should `org-cycle' emulate TAB.
  613. nil Never
  614. white Only in completely white lines
  615. whitestart Only at the beginning of lines, before the first non-white char
  616. t Everywhere except in headlines
  617. exc-hl-bol Everywhere except at the start of a headline
  618. If TAB is used in a place where it does not emulate TAB, the current subtree
  619. visibility is cycled."
  620. :group 'org-cycle
  621. :type '(choice (const :tag "Never" nil)
  622. (const :tag "Only in completely white lines" white)
  623. (const :tag "Before first char in a line" whitestart)
  624. (const :tag "Everywhere except in headlines" t)
  625. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  626. ))
  627. (defcustom org-cycle-separator-lines 2
  628. "Number of empty lines needed to keep an empty line between collapsed trees.
  629. If you leave an empty line between the end of a subtree and the following
  630. headline, this empty line is hidden when the subtree is folded.
  631. Org-mode will leave (exactly) one empty line visible if the number of
  632. empty lines is equal or larger to the number given in this variable.
  633. So the default 2 means, at least 2 empty lines after the end of a subtree
  634. are needed to produce free space between a collapsed subtree and the
  635. following headline.
  636. If the number is negative, and the number of empty lines is at least -N,
  637. all empty lines are shown.
  638. Special case: when 0, never leave empty lines in collapsed view."
  639. :group 'org-cycle
  640. :type 'integer)
  641. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  642. (defcustom org-pre-cycle-hook nil
  643. "Hook that is run before visibility cycling is happening.
  644. The function(s) in this hook must accept a single argument which indicates
  645. the new state that will be set right after running this hook. The
  646. argument is a symbol. Before a global state change, it can have the values
  647. `overview', `content', or `all'. Before a local state change, it can have
  648. the values `folded', `children', or `subtree'."
  649. :group 'org-cycle
  650. :type 'hook)
  651. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  652. org-cycle-hide-drawers
  653. org-cycle-show-empty-lines
  654. org-optimize-window-after-visibility-change)
  655. "Hook that is run after `org-cycle' has changed the buffer visibility.
  656. The function(s) in this hook must accept a single argument which indicates
  657. the new state that was set by the most recent `org-cycle' command. The
  658. argument is a symbol. After a global state change, it can have the values
  659. `overview', `content', or `all'. After a local state change, it can have
  660. the values `folded', `children', or `subtree'."
  661. :group 'org-cycle
  662. :type 'hook)
  663. (defgroup org-edit-structure nil
  664. "Options concerning structure editing in Org-mode."
  665. :tag "Org Edit Structure"
  666. :group 'org-structure)
  667. (defcustom org-odd-levels-only nil
  668. "Non-nil means, skip even levels and only use odd levels for the outline.
  669. This has the effect that two stars are being added/taken away in
  670. promotion/demotion commands. It also influences how levels are
  671. handled by the exporters.
  672. Changing it requires restart of `font-lock-mode' to become effective
  673. for fontification also in regions already fontified.
  674. You may also set this on a per-file basis by adding one of the following
  675. lines to the buffer:
  676. #+STARTUP: odd
  677. #+STARTUP: oddeven"
  678. :group 'org-edit-structure
  679. :group 'org-font-lock
  680. :type 'boolean)
  681. (defcustom org-adapt-indentation t
  682. "Non-nil means, adapt indentation to outline node level.
  683. When this variable is set, Org assumes that you write outlines by
  684. indenting text in each node to align with the headline (after the stars).
  685. The following issues are influenced by this variable:
  686. - When this is set and the *entire* text in an entry is indented, the
  687. indentation is increased by one space in a demotion command, and
  688. decreased by one in a promotion command. If any line in the entry
  689. body starts with text at column 0, indentation is not changed at all.
  690. - Property drawers and planning information is inserted indented when
  691. this variable s set. When nil, they will not be indented.
  692. - TAB indents a line relative to context. The lines below a headline
  693. will be indented when this variable is set.
  694. Note that this is all about true indentation, by adding and removing
  695. space characters. See also `org-indent.el' which does level-dependent
  696. indentation in a virtual way, i.e. at display time in Emacs."
  697. :group 'org-edit-structure
  698. :type 'boolean)
  699. (defcustom org-special-ctrl-a/e nil
  700. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  701. When t, `C-a' will bring back the cursor to the beginning of the
  702. headline text, i.e. after the stars and after a possible TODO keyword.
  703. In an item, this will be the position after the bullet.
  704. When the cursor is already at that position, another `C-a' will bring
  705. it to the beginning of the line.
  706. `C-e' will jump to the end of the headline, ignoring the presence of tags
  707. in the headline. A second `C-e' will then jump to the true end of the
  708. line, after any tags. This also means that, when this variable is
  709. non-nil, `C-e' also will never jump beyond the end of the heading of a
  710. folded section, i.e. not after the ellipses.
  711. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  712. going to the true line boundary first. Only a directly following, identical
  713. keypress will bring the cursor to the special positions.
  714. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  715. set separately."
  716. :group 'org-edit-structure
  717. :type '(choice
  718. (const :tag "off" nil)
  719. (const :tag "on: after stars/bullet and before tags first" t)
  720. (const :tag "reversed: true line boundary first" reversed)
  721. (cons :tag "Set C-a and C-e separately"
  722. (choice :tag "Special C-a"
  723. (const :tag "off" nil)
  724. (const :tag "on: after stars/bullet first" t)
  725. (const :tag "reversed: before stars/bullet first" reversed))
  726. (choice :tag "Special C-e"
  727. (const :tag "off" nil)
  728. (const :tag "on: before tags first" t)
  729. (const :tag "reversed: after tags first" reversed)))))
  730. (if (fboundp 'defvaralias)
  731. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  732. (defcustom org-special-ctrl-k nil
  733. "Non-nil means `C-k' will behave specially in headlines.
  734. When nil, `C-k' will call the default `kill-line' command.
  735. When t, the following will happen while the cursor is in the headline:
  736. - When the cursor is at the beginning of a headline, kill the entire
  737. line and possible the folded subtree below the line.
  738. - When in the middle of the headline text, kill the headline up to the tags.
  739. - When after the headline text, kill the tags."
  740. :group 'org-edit-structure
  741. :type 'boolean)
  742. (defcustom org-yank-folded-subtrees t
  743. "Non-nil means, when yanking subtrees, fold them.
  744. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  745. it starts with a heading and all other headings in it are either children
  746. or siblings, then fold all the subtrees. However, do this only if no
  747. text after the yank would be swallowed into a folded tree by this action."
  748. :group 'org-edit-structure
  749. :type 'boolean)
  750. (defcustom org-yank-adjusted-subtrees nil
  751. "Non-nil means, when yanking subtrees, adjust the level.
  752. With this setting, `org-paste-subtree' is used to insert the subtree, see
  753. this function for details."
  754. :group 'org-edit-structure
  755. :type 'boolean)
  756. (defcustom org-M-RET-may-split-line '((default . t))
  757. "Non-nil means, M-RET will split the line at the cursor position.
  758. When nil, it will go to the end of the line before making a
  759. new line.
  760. You may also set this option in a different way for different
  761. contexts. Valid contexts are:
  762. headline when creating a new headline
  763. item when creating a new item
  764. table in a table field
  765. default the value to be used for all contexts not explicitly
  766. customized"
  767. :group 'org-structure
  768. :group 'org-table
  769. :type '(choice
  770. (const :tag "Always" t)
  771. (const :tag "Never" nil)
  772. (repeat :greedy t :tag "Individual contexts"
  773. (cons
  774. (choice :tag "Context"
  775. (const headline)
  776. (const item)
  777. (const table)
  778. (const default))
  779. (boolean)))))
  780. (defcustom org-insert-heading-respect-content nil
  781. "Non-nil means, insert new headings after the current subtree.
  782. When nil, the new heading is created directly after the current line.
  783. The commands \\[org-insert-heading-respect-content] and
  784. \\[org-insert-todo-heading-respect-content] turn this variable on
  785. for the duration of the command."
  786. :group 'org-structure
  787. :type 'boolean)
  788. (defcustom org-blank-before-new-entry '((heading . auto)
  789. (plain-list-item . auto))
  790. "Should `org-insert-heading' leave a blank line before new heading/item?
  791. The value is an alist, with `heading' and `plain-list-item' as car,
  792. and a boolean flag as cdr. For plain lists, if the variable
  793. `org-empty-line-terminates-plain-lists' is set, the setting here
  794. is ignored and no empty line is inserted, to keep the list in tact."
  795. :group 'org-edit-structure
  796. :type '(list
  797. (cons (const heading)
  798. (choice (const :tag "Never" nil)
  799. (const :tag "Always" t)
  800. (const :tag "Auto" auto)))
  801. (cons (const plain-list-item)
  802. (choice (const :tag "Never" nil)
  803. (const :tag "Always" t)
  804. (const :tag "Auto" auto)))))
  805. (defcustom org-insert-heading-hook nil
  806. "Hook being run after inserting a new heading."
  807. :group 'org-edit-structure
  808. :type 'hook)
  809. (defcustom org-enable-fixed-width-editor t
  810. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  811. This currently only means, they are never auto-wrapped.
  812. When nil, such lines will be treated like ordinary lines.
  813. See also the QUOTE keyword."
  814. :group 'org-edit-structure
  815. :type 'boolean)
  816. (defcustom org-goto-auto-isearch t
  817. "Non-nil means, typing characters in org-goto starts incremental search."
  818. :group 'org-edit-structure
  819. :type 'boolean)
  820. (defgroup org-sparse-trees nil
  821. "Options concerning sparse trees in Org-mode."
  822. :tag "Org Sparse Trees"
  823. :group 'org-structure)
  824. (defcustom org-highlight-sparse-tree-matches t
  825. "Non-nil means, highlight all matches that define a sparse tree.
  826. The highlights will automatically disappear the next time the buffer is
  827. changed by an edit command."
  828. :group 'org-sparse-trees
  829. :type 'boolean)
  830. (defcustom org-remove-highlights-with-change t
  831. "Non-nil means, any change to the buffer will remove temporary highlights.
  832. Such highlights are created by `org-occur' and `org-clock-display'.
  833. When nil, `C-c C-c needs to be used to get rid of the highlights.
  834. The highlights created by `org-preview-latex-fragment' always need
  835. `C-c C-c' to be removed."
  836. :group 'org-sparse-trees
  837. :group 'org-time
  838. :type 'boolean)
  839. (defcustom org-occur-hook '(org-first-headline-recenter)
  840. "Hook that is run after `org-occur' has constructed a sparse tree.
  841. This can be used to recenter the window to show as much of the structure
  842. as possible."
  843. :group 'org-sparse-trees
  844. :type 'hook)
  845. (defgroup org-imenu-and-speedbar nil
  846. "Options concerning imenu and speedbar in Org-mode."
  847. :tag "Org Imenu and Speedbar"
  848. :group 'org-structure)
  849. (defcustom org-imenu-depth 2
  850. "The maximum level for Imenu access to Org-mode headlines.
  851. This also applied for speedbar access."
  852. :group 'org-imenu-and-speedbar
  853. :type 'integer)
  854. (defgroup org-table nil
  855. "Options concerning tables in Org-mode."
  856. :tag "Org Table"
  857. :group 'org)
  858. (defcustom org-enable-table-editor 'optimized
  859. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  860. When nil, such lines will be treated like ordinary lines.
  861. When equal to the symbol `optimized', the table editor will be optimized to
  862. do the following:
  863. - Automatic overwrite mode in front of whitespace in table fields.
  864. This makes the structure of the table stay in tact as long as the edited
  865. field does not exceed the column width.
  866. - Minimize the number of realigns. Normally, the table is aligned each time
  867. TAB or RET are pressed to move to another field. With optimization this
  868. happens only if changes to a field might have changed the column width.
  869. Optimization requires replacing the functions `self-insert-command',
  870. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  871. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  872. very good at guessing when a re-align will be necessary, but you can always
  873. force one with \\[org-ctrl-c-ctrl-c].
  874. If you would like to use the optimized version in Org-mode, but the
  875. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  876. This variable can be used to turn on and off the table editor during a session,
  877. but in order to toggle optimization, a restart is required.
  878. See also the variable `org-table-auto-blank-field'."
  879. :group 'org-table
  880. :type '(choice
  881. (const :tag "off" nil)
  882. (const :tag "on" t)
  883. (const :tag "on, optimized" optimized)))
  884. (defcustom org-self-insert-cluster-for-undo t
  885. "Non-nil means cluster self-insert commands for undo when possible.
  886. If this is set, then, like in the Emacs command loop, 20 consequtive
  887. characters will be undone together.
  888. This is configurable, because there is some impact on typing performance."
  889. :group 'org-table
  890. :type 'boolean)
  891. (defcustom org-table-tab-recognizes-table.el t
  892. "Non-nil means, TAB will automatically notice a table.el table.
  893. When it sees such a table, it moves point into it and - if necessary -
  894. calls `table-recognize-table'."
  895. :group 'org-table-editing
  896. :type 'boolean)
  897. (defgroup org-link nil
  898. "Options concerning links in Org-mode."
  899. :tag "Org Link"
  900. :group 'org)
  901. (defvar org-link-abbrev-alist-local nil
  902. "Buffer-local version of `org-link-abbrev-alist', which see.
  903. The value of this is taken from the #+LINK lines.")
  904. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  905. (defcustom org-link-abbrev-alist nil
  906. "Alist of link abbreviations.
  907. The car of each element is a string, to be replaced at the start of a link.
  908. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  909. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  910. [[linkkey:tag][description]]
  911. The 'linkkey' must be a word word, starting with a letter, followed
  912. by letters, numbers, '-' or '_'.
  913. If REPLACE is a string, the tag will simply be appended to create the link.
  914. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  915. the placeholder \"%h\" will cause a url-encoded version of the tag to
  916. be inserted at that point (see the function `url-hexify-string').
  917. REPLACE may also be a function that will be called with the tag as the
  918. only argument to create the link, which should be returned as a string.
  919. See the manual for examples."
  920. :group 'org-link
  921. :type '(repeat
  922. (cons
  923. (string :tag "Protocol")
  924. (choice
  925. (string :tag "Format")
  926. (function)))))
  927. (defcustom org-descriptive-links t
  928. "Non-nil means, hide link part and only show description of bracket links.
  929. Bracket links are like [[link][description]]. This variable sets the initial
  930. state in new org-mode buffers. The setting can then be toggled on a
  931. per-buffer basis from the Org->Hyperlinks menu."
  932. :group 'org-link
  933. :type 'boolean)
  934. (defcustom org-link-file-path-type 'adaptive
  935. "How the path name in file links should be stored.
  936. Valid values are:
  937. relative Relative to the current directory, i.e. the directory of the file
  938. into which the link is being inserted.
  939. absolute Absolute path, if possible with ~ for home directory.
  940. noabbrev Absolute path, no abbreviation of home directory.
  941. adaptive Use relative path for files in the current directory and sub-
  942. directories of it. For other files, use an absolute path."
  943. :group 'org-link
  944. :type '(choice
  945. (const relative)
  946. (const absolute)
  947. (const noabbrev)
  948. (const adaptive)))
  949. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  950. "Types of links that should be activated in Org-mode files.
  951. This is a list of symbols, each leading to the activation of a certain link
  952. type. In principle, it does not hurt to turn on most link types - there may
  953. be a small gain when turning off unused link types. The types are:
  954. bracket The recommended [[link][description]] or [[link]] links with hiding.
  955. angular Links in angular brackets that may contain whitespace like
  956. <bbdb:Carsten Dominik>.
  957. plain Plain links in normal text, no whitespace, like http://google.com.
  958. radio Text that is matched by a radio target, see manual for details.
  959. tag Tag settings in a headline (link to tag search).
  960. date Time stamps (link to calendar).
  961. footnote Footnote labels.
  962. Changing this variable requires a restart of Emacs to become effective."
  963. :group 'org-link
  964. :type '(set :greedy t
  965. (const :tag "Double bracket links (new style)" bracket)
  966. (const :tag "Angular bracket links (old style)" angular)
  967. (const :tag "Plain text links" plain)
  968. (const :tag "Radio target matches" radio)
  969. (const :tag "Tags" tag)
  970. (const :tag "Timestamps" date)
  971. (const :tag "Footnotes" footnote)))
  972. (defcustom org-make-link-description-function nil
  973. "Function to use to generate link descriptions from links. If
  974. nil the link location will be used. This function must take two
  975. parameters; the first is the link and the second the description
  976. org-insert-link has generated, and should return the description
  977. to use."
  978. :group 'org-link
  979. :type 'function)
  980. (defgroup org-link-store nil
  981. "Options concerning storing links in Org-mode."
  982. :tag "Org Store Link"
  983. :group 'org-link)
  984. (defcustom org-email-link-description-format "Email %c: %.30s"
  985. "Format of the description part of a link to an email or usenet message.
  986. The following %-escapes will be replaced by corresponding information:
  987. %F full \"From\" field
  988. %f name, taken from \"From\" field, address if no name
  989. %T full \"To\" field
  990. %t first name in \"To\" field, address if no name
  991. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  992. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  993. %s subject
  994. %m message-id.
  995. You may use normal field width specification between the % and the letter.
  996. This is for example useful to limit the length of the subject.
  997. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  998. :group 'org-link-store
  999. :type 'string)
  1000. (defcustom org-from-is-user-regexp
  1001. (let (r1 r2)
  1002. (when (and user-mail-address (not (string= user-mail-address "")))
  1003. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1004. (when (and user-full-name (not (string= user-full-name "")))
  1005. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1006. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1007. "Regexp matched against the \"From:\" header of an email or usenet message.
  1008. It should match if the message is from the user him/herself."
  1009. :group 'org-link-store
  1010. :type 'regexp)
  1011. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1012. "Non-nil means, storing a link to an Org file will use entry IDs.
  1013. Note that before this variable is even considered, org-id must be loaded,
  1014. so please customize `org-modules' and turn it on.
  1015. The variable can have the following values:
  1016. t Create an ID if needed to make a link to the current entry.
  1017. create-if-interactive
  1018. If `org-store-link' is called directly (interactively, as a user
  1019. command), do create an ID to support the link. But when doing the
  1020. job for remember, only use the ID if it already exists. The
  1021. purpose of this setting is to avoid proliferation of unwanted
  1022. IDs, just because you happen to be in an Org file when you
  1023. call `org-remember' that automatically and preemptively
  1024. creates a link. If you do want to get an ID link in a remember
  1025. template to an entry not having an ID, create it first by
  1026. explicitly creating a link to it, using `C-c C-l' first.
  1027. create-if-interactive-and-no-custom-id
  1028. Like create-if-interactive, but do not create an ID if there is
  1029. a CUSTOM_ID property defined in the entry. This is the default.
  1030. use-existing
  1031. Use existing ID, do not create one.
  1032. nil Never use an ID to make a link, instead link using a text search for
  1033. the headline text."
  1034. :group 'org-link-store
  1035. :type '(choice
  1036. (const :tag "Create ID to make link" t)
  1037. (const :tag "Create if storing link interactively"
  1038. create-if-interactive)
  1039. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1040. create-if-interactive-and-no-custom-id)
  1041. (const :tag "Only use existing" use-existing)
  1042. (const :tag "Do not use ID to create link" nil)))
  1043. (defcustom org-context-in-file-links t
  1044. "Non-nil means, file links from `org-store-link' contain context.
  1045. A search string will be added to the file name with :: as separator and
  1046. used to find the context when the link is activated by the command
  1047. `org-open-at-point'.
  1048. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1049. negates this setting for the duration of the command."
  1050. :group 'org-link-store
  1051. :type 'boolean)
  1052. (defcustom org-keep-stored-link-after-insertion nil
  1053. "Non-nil means, keep link in list for entire session.
  1054. The command `org-store-link' adds a link pointing to the current
  1055. location to an internal list. These links accumulate during a session.
  1056. The command `org-insert-link' can be used to insert links into any
  1057. Org-mode file (offering completion for all stored links). When this
  1058. option is nil, every link which has been inserted once using \\[org-insert-link]
  1059. will be removed from the list, to make completing the unused links
  1060. more efficient."
  1061. :group 'org-link-store
  1062. :type 'boolean)
  1063. (defgroup org-link-follow nil
  1064. "Options concerning following links in Org-mode."
  1065. :tag "Org Follow Link"
  1066. :group 'org-link)
  1067. (defcustom org-link-translation-function nil
  1068. "Function to translate links with different syntax to Org syntax.
  1069. This can be used to translate links created for example by the Planner
  1070. or emacs-wiki packages to Org syntax.
  1071. The function must accept two parameters, a TYPE containing the link
  1072. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1073. which is everything after the link protocol. It should return a cons
  1074. with possibly modified values of type and path.
  1075. Org contains a function for this, so if you set this variable to
  1076. `org-translate-link-from-planner', you should be able follow many
  1077. links created by planner."
  1078. :group 'org-link-follow
  1079. :type 'function)
  1080. (defcustom org-follow-link-hook nil
  1081. "Hook that is run after a link has been followed."
  1082. :group 'org-link-follow
  1083. :type 'hook)
  1084. (defcustom org-tab-follows-link nil
  1085. "Non-nil means, on links TAB will follow the link.
  1086. Needs to be set before org.el is loaded.
  1087. This really should not be used, it does not make sense, and the
  1088. implementation is bad."
  1089. :group 'org-link-follow
  1090. :type 'boolean)
  1091. (defcustom org-return-follows-link nil
  1092. "Non-nil means, on links RET will follow the link.
  1093. Needs to be set before org.el is loaded."
  1094. :group 'org-link-follow
  1095. :type 'boolean)
  1096. (defcustom org-mouse-1-follows-link
  1097. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1098. "Non-nil means, mouse-1 on a link will follow the link.
  1099. A longer mouse click will still set point. Does not work on XEmacs.
  1100. Needs to be set before org.el is loaded."
  1101. :group 'org-link-follow
  1102. :type 'boolean)
  1103. (defcustom org-mark-ring-length 4
  1104. "Number of different positions to be recorded in the ring
  1105. Changing this requires a restart of Emacs to work correctly."
  1106. :group 'org-link-follow
  1107. :type 'integer)
  1108. (defcustom org-link-frame-setup
  1109. '((vm . vm-visit-folder-other-frame)
  1110. (gnus . gnus-other-frame)
  1111. (file . find-file-other-window))
  1112. "Setup the frame configuration for following links.
  1113. When following a link with Emacs, it may often be useful to display
  1114. this link in another window or frame. This variable can be used to
  1115. set this up for the different types of links.
  1116. For VM, use any of
  1117. `vm-visit-folder'
  1118. `vm-visit-folder-other-frame'
  1119. For Gnus, use any of
  1120. `gnus'
  1121. `gnus-other-frame'
  1122. `org-gnus-no-new-news'
  1123. For FILE, use any of
  1124. `find-file'
  1125. `find-file-other-window'
  1126. `find-file-other-frame'
  1127. For the calendar, use the variable `calendar-setup'.
  1128. For BBDB, it is currently only possible to display the matches in
  1129. another window."
  1130. :group 'org-link-follow
  1131. :type '(list
  1132. (cons (const vm)
  1133. (choice
  1134. (const vm-visit-folder)
  1135. (const vm-visit-folder-other-window)
  1136. (const vm-visit-folder-other-frame)))
  1137. (cons (const gnus)
  1138. (choice
  1139. (const gnus)
  1140. (const gnus-other-frame)
  1141. (const org-gnus-no-new-news)))
  1142. (cons (const file)
  1143. (choice
  1144. (const find-file)
  1145. (const find-file-other-window)
  1146. (const find-file-other-frame)))))
  1147. (defcustom org-display-internal-link-with-indirect-buffer nil
  1148. "Non-nil means, use indirect buffer to display infile links.
  1149. Activating internal links (from one location in a file to another location
  1150. in the same file) normally just jumps to the location. When the link is
  1151. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1152. another window. When this option is set, the other window actually displays
  1153. an indirect buffer clone of the current buffer, to avoid any visibility
  1154. changes to the current buffer."
  1155. :group 'org-link-follow
  1156. :type 'boolean)
  1157. (defcustom org-open-non-existing-files nil
  1158. "Non-nil means, `org-open-file' will open non-existing files.
  1159. When nil, an error will be generated.
  1160. This variable applies only to external applications because they
  1161. might choke on non-existing files. If the link is to a file that
  1162. will be openend in Emacs, the variable is ignored."
  1163. :group 'org-link-follow
  1164. :type 'boolean)
  1165. (defcustom org-open-directory-means-index-dot-org nil
  1166. "Non-nil means, a link to a directory really means to index.org.
  1167. When nil, following a directory link will run dired or open a finder/explorer
  1168. window on that directory."
  1169. :group 'org-link-follow
  1170. :type 'boolean)
  1171. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1172. "Function and arguments to call for following mailto links.
  1173. This is a list with the first element being a lisp function, and the
  1174. remaining elements being arguments to the function. In string arguments,
  1175. %a will be replaced by the address, and %s will be replaced by the subject
  1176. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1177. :group 'org-link-follow
  1178. :type '(choice
  1179. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1180. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1181. (const :tag "message-mail" (message-mail "%a" "%s"))
  1182. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1183. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1184. "Non-nil means, ask for confirmation before executing shell links.
  1185. Shell links can be dangerous: just think about a link
  1186. [[shell:rm -rf ~/*][Google Search]]
  1187. This link would show up in your Org-mode document as \"Google Search\",
  1188. but really it would remove your entire home directory.
  1189. Therefore we advise against setting this variable to nil.
  1190. Just change it to `y-or-n-p' if you want to confirm with a
  1191. single keystroke rather than having to type \"yes\"."
  1192. :group 'org-link-follow
  1193. :type '(choice
  1194. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1195. (const :tag "with y-or-n (faster)" y-or-n-p)
  1196. (const :tag "no confirmation (dangerous)" nil)))
  1197. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1198. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1199. Elisp links can be dangerous: just think about a link
  1200. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1201. This link would show up in your Org-mode document as \"Google Search\",
  1202. but really it would remove your entire home directory.
  1203. Therefore we advise against setting this variable to nil.
  1204. Just change it to `y-or-n-p' if you want to confirm with a
  1205. single keystroke rather than having to type \"yes\"."
  1206. :group 'org-link-follow
  1207. :type '(choice
  1208. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1209. (const :tag "with y-or-n (faster)" y-or-n-p)
  1210. (const :tag "no confirmation (dangerous)" nil)))
  1211. (defconst org-file-apps-defaults-gnu
  1212. '((remote . emacs)
  1213. (system . mailcap)
  1214. (t . mailcap))
  1215. "Default file applications on a UNIX or GNU/Linux system.
  1216. See `org-file-apps'.")
  1217. (defconst org-file-apps-defaults-macosx
  1218. '((remote . emacs)
  1219. (t . "open %s")
  1220. (system . "open %s")
  1221. ("ps.gz" . "gv %s")
  1222. ("eps.gz" . "gv %s")
  1223. ("dvi" . "xdvi %s")
  1224. ("fig" . "xfig %s"))
  1225. "Default file applications on a MacOS X system.
  1226. The system \"open\" is known as a default, but we use X11 applications
  1227. for some files for which the OS does not have a good default.
  1228. See `org-file-apps'.")
  1229. (defconst org-file-apps-defaults-windowsnt
  1230. (list
  1231. '(remote . emacs)
  1232. (cons t
  1233. (list (if (featurep 'xemacs)
  1234. 'mswindows-shell-execute
  1235. 'w32-shell-execute)
  1236. "open" 'file))
  1237. (cons 'system
  1238. (list (if (featurep 'xemacs)
  1239. 'mswindows-shell-execute
  1240. 'w32-shell-execute)
  1241. "open" 'file)))
  1242. "Default file applications on a Windows NT system.
  1243. The system \"open\" is used for most files.
  1244. See `org-file-apps'.")
  1245. (defcustom org-file-apps
  1246. '(
  1247. (auto-mode . emacs)
  1248. ("\\.mm\\'" . default)
  1249. ("\\.x?html?\\'" . default)
  1250. ("\\.pdf\\'" . default)
  1251. )
  1252. "External applications for opening `file:path' items in a document.
  1253. Org-mode uses system defaults for different file types, but
  1254. you can use this variable to set the application for a given file
  1255. extension. The entries in this list are cons cells where the car identifies
  1256. files and the cdr the corresponding command. Possible values for the
  1257. file identifier are
  1258. \"regex\" Regular expression matched against the file name. For backward
  1259. compatibility, this can also be a string with only alphanumeric
  1260. characters, which is then interpreted as an extension.
  1261. `directory' Matches a directory
  1262. `remote' Matches a remote file, accessible through tramp or efs.
  1263. Remote files most likely should be visited through Emacs
  1264. because external applications cannot handle such paths.
  1265. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1266. so all files Emacs knows how to handle. Using this with
  1267. command `emacs' will open most files in Emacs. Beware that this
  1268. will also open html files inside Emacs, unless you add
  1269. (\"html\" . default) to the list as well.
  1270. t Default for files not matched by any of the other options.
  1271. `system' The system command to open files, like `open' on Windows
  1272. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1273. that will be selected if you call `C-c C-o' with a double
  1274. `C-u C-u' prefix.
  1275. Possible values for the command are:
  1276. `emacs' The file will be visited by the current Emacs process.
  1277. `default' Use the default application for this file type, which is the
  1278. association for t in the list, most likely in the system-specific
  1279. part.
  1280. This can be used to overrule an unwanted setting in the
  1281. system-specific variable.
  1282. `system' Use the system command for opening files, like \"open\".
  1283. This command is specified by the entry whose car is `system'.
  1284. Most likely, the system-specific version of this variable
  1285. does define this command, but you can overrule/replace it
  1286. here.
  1287. string A command to be executed by a shell; %s will be replaced
  1288. by the path to the file.
  1289. sexp A Lisp form which will be evaluated. The file path will
  1290. be available in the Lisp variable `file'.
  1291. For more examples, see the system specific constants
  1292. `org-file-apps-defaults-macosx'
  1293. `org-file-apps-defaults-windowsnt'
  1294. `org-file-apps-defaults-gnu'."
  1295. :group 'org-link-follow
  1296. :type '(repeat
  1297. (cons (choice :value ""
  1298. (string :tag "Extension")
  1299. (const :tag "System command to open files" system)
  1300. (const :tag "Default for unrecognized files" t)
  1301. (const :tag "Remote file" remote)
  1302. (const :tag "Links to a directory" directory)
  1303. (const :tag "Any files that have Emacs modes"
  1304. auto-mode))
  1305. (choice :value ""
  1306. (const :tag "Visit with Emacs" emacs)
  1307. (const :tag "Use default" default)
  1308. (const :tag "Use the system command" system)
  1309. (string :tag "Command")
  1310. (sexp :tag "Lisp form")))))
  1311. (defgroup org-refile nil
  1312. "Options concerning refiling entries in Org-mode."
  1313. :tag "Org Refile"
  1314. :group 'org)
  1315. (defcustom org-directory "~/org"
  1316. "Directory with org files.
  1317. This is just a default location to look for Org files. There is no need
  1318. at all to put your files into this directory. It is only used in the
  1319. following situations:
  1320. 1. When a remember template specifies a target file that is not an
  1321. absolute path. The path will then be interpreted relative to
  1322. `org-directory'
  1323. 2. When a remember note is filed away in an interactive way (when exiting the
  1324. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1325. with `org-directory' as the default path."
  1326. :group 'org-refile
  1327. :group 'org-remember
  1328. :type 'directory)
  1329. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1330. "Default target for storing notes.
  1331. Used by the hooks for remember.el. This can be a string, or nil to mean
  1332. the value of `remember-data-file'.
  1333. You can set this on a per-template basis with the variable
  1334. `org-remember-templates'."
  1335. :group 'org-refile
  1336. :group 'org-remember
  1337. :type '(choice
  1338. (const :tag "Default from remember-data-file" nil)
  1339. file))
  1340. (defcustom org-goto-interface 'outline
  1341. "The default interface to be used for `org-goto'.
  1342. Allowed values are:
  1343. outline The interface shows an outline of the relevant file
  1344. and the correct heading is found by moving through
  1345. the outline or by searching with incremental search.
  1346. outline-path-completion Headlines in the current buffer are offered via
  1347. completion. This is the interface also used by
  1348. the refile command."
  1349. :group 'org-refile
  1350. :type '(choice
  1351. (const :tag "Outline" outline)
  1352. (const :tag "Outline-path-completion" outline-path-completion)))
  1353. (defcustom org-goto-max-level 5
  1354. "Maximum level to be considered when running org-goto with refile interface."
  1355. :group 'org-refile
  1356. :type 'integer)
  1357. (defcustom org-reverse-note-order nil
  1358. "Non-nil means, store new notes at the beginning of a file or entry.
  1359. When nil, new notes will be filed to the end of a file or entry.
  1360. This can also be a list with cons cells of regular expressions that
  1361. are matched against file names, and values."
  1362. :group 'org-remember
  1363. :group 'org-refile
  1364. :type '(choice
  1365. (const :tag "Reverse always" t)
  1366. (const :tag "Reverse never" nil)
  1367. (repeat :tag "By file name regexp"
  1368. (cons regexp boolean))))
  1369. (defcustom org-refile-targets nil
  1370. "Targets for refiling entries with \\[org-refile].
  1371. This is list of cons cells. Each cell contains:
  1372. - a specification of the files to be considered, either a list of files,
  1373. or a symbol whose function or variable value will be used to retrieve
  1374. a file name or a list of file names. If you use `org-agenda-files' for
  1375. that, all agenda files will be scanned for targets. Nil means, consider
  1376. headings in the current buffer.
  1377. - A specification of how to find candidate refile targets. This may be
  1378. any of:
  1379. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1380. This tag has to be present in all target headlines, inheritance will
  1381. not be considered.
  1382. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1383. todo keyword.
  1384. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1385. headlines that are refiling targets.
  1386. - a cons cell (:level . N). Any headline of level N is considered a target.
  1387. Note that, when `org-odd-levels-only' is set, level corresponds to
  1388. order in hierarchy, not to the number of stars.
  1389. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1390. Note that, when `org-odd-levels-only' is set, level corresponds to
  1391. order in hierarchy, not to the number of stars.
  1392. You can set the variable `org-refile-target-verify-function' to a function
  1393. to verify each headline found by the simple critery above.
  1394. When this variable is nil, all top-level headlines in the current buffer
  1395. are used, equivalent to the value `((nil . (:level . 1))'."
  1396. :group 'org-refile
  1397. :type '(repeat
  1398. (cons
  1399. (choice :value org-agenda-files
  1400. (const :tag "All agenda files" org-agenda-files)
  1401. (const :tag "Current buffer" nil)
  1402. (function) (variable) (file))
  1403. (choice :tag "Identify target headline by"
  1404. (cons :tag "Specific tag" (const :value :tag) (string))
  1405. (cons :tag "TODO keyword" (const :value :todo) (string))
  1406. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1407. (cons :tag "Level number" (const :value :level) (integer))
  1408. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1409. (defcustom org-refile-target-verify-function nil
  1410. "Function to verify if the headline at point should be a refile target.
  1411. The function will be called without arguments, with point at the
  1412. beginning of the headline. It should return t and leave point
  1413. where it is if the headline is a valid target for refiling.
  1414. If the target should not be selected, the function must return nil.
  1415. In addition to this, it may move point to a place from where the search
  1416. should be continued. For example, the function may decide that the entire
  1417. subtree of the current entry should be excluded and move point to the end
  1418. of the subtree."
  1419. :group 'org-refile
  1420. :type 'function)
  1421. (defcustom org-refile-use-outline-path nil
  1422. "Non-nil means, provide refile targets as paths.
  1423. So a level 3 headline will be available as level1/level2/level3.
  1424. When the value is `file', also include the file name (without directory)
  1425. into the path. In this case, you can also stop the completion after
  1426. the file name, to get entries inserted as top level in the file.
  1427. When `full-file-path', include the full file path."
  1428. :group 'org-refile
  1429. :type '(choice
  1430. (const :tag "Not" nil)
  1431. (const :tag "Yes" t)
  1432. (const :tag "Start with file name" file)
  1433. (const :tag "Start with full file path" full-file-path)))
  1434. (defcustom org-outline-path-complete-in-steps t
  1435. "Non-nil means, complete the outline path in hierarchical steps.
  1436. When Org-mode uses the refile interface to select an outline path
  1437. \(see variable `org-refile-use-outline-path'), the completion of
  1438. the path can be done is a single go, or if can be done in steps down
  1439. the headline hierarchy. Going in steps is probably the best if you
  1440. do not use a special completion package like `ido' or `icicles'.
  1441. However, when using these packages, going in one step can be very
  1442. fast, while still showing the whole path to the entry."
  1443. :group 'org-refile
  1444. :type 'boolean)
  1445. (defcustom org-refile-allow-creating-parent-nodes nil
  1446. "Non-nil means, allow to create new nodes as refile targets.
  1447. New nodes are then created by adding \"/new node name\" to the completion
  1448. of an existing node. When the value of this variable is `confirm',
  1449. new node creation must be confirmed by the user (recommended)
  1450. When nil, the completion must match an existing entry.
  1451. Note that, if the new heading is not seen by the criteria
  1452. listed in `org-refile-targets', multiple instances of the same
  1453. heading would be created by trying again to file under the new
  1454. heading."
  1455. :group 'org-refile
  1456. :type '(choice
  1457. (const :tag "Never" nil)
  1458. (const :tag "Always" t)
  1459. (const :tag "Prompt for confirmation" confirm)))
  1460. (defgroup org-todo nil
  1461. "Options concerning TODO items in Org-mode."
  1462. :tag "Org TODO"
  1463. :group 'org)
  1464. (defgroup org-progress nil
  1465. "Options concerning Progress logging in Org-mode."
  1466. :tag "Org Progress"
  1467. :group 'org-time)
  1468. (defvar org-todo-interpretation-widgets
  1469. '(
  1470. (:tag "Sequence (cycling hits every state)" sequence)
  1471. (:tag "Type (cycling directly to DONE)" type))
  1472. "The available interpretation symbols for customizing
  1473. `org-todo-keywords'.
  1474. Interested libraries should add to this list.")
  1475. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1476. "List of TODO entry keyword sequences and their interpretation.
  1477. \\<org-mode-map>This is a list of sequences.
  1478. Each sequence starts with a symbol, either `sequence' or `type',
  1479. indicating if the keywords should be interpreted as a sequence of
  1480. action steps, or as different types of TODO items. The first
  1481. keywords are states requiring action - these states will select a headline
  1482. for inclusion into the global TODO list Org-mode produces. If one of
  1483. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1484. signify that no further action is necessary. If \"|\" is not found,
  1485. the last keyword is treated as the only DONE state of the sequence.
  1486. The command \\[org-todo] cycles an entry through these states, and one
  1487. additional state where no keyword is present. For details about this
  1488. cycling, see the manual.
  1489. TODO keywords and interpretation can also be set on a per-file basis with
  1490. the special #+SEQ_TODO and #+TYP_TODO lines.
  1491. Each keyword can optionally specify a character for fast state selection
  1492. \(in combination with the variable `org-use-fast-todo-selection')
  1493. and specifiers for state change logging, using the same syntax
  1494. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1495. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1496. indicates to record a time stamp each time this state is selected.
  1497. Each keyword may also specify if a timestamp or a note should be
  1498. recorded when entering or leaving the state, by adding additional
  1499. characters in the parenthesis after the keyword. This looks like this:
  1500. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1501. record only the time of the state change. With X and Y being either
  1502. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1503. Y when leaving the state if and only if the *target* state does not
  1504. define X. You may omit any of the fast-selection key or X or /Y,
  1505. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1506. For backward compatibility, this variable may also be just a list
  1507. of keywords - in this case the interpretation (sequence or type) will be
  1508. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1509. :group 'org-todo
  1510. :group 'org-keywords
  1511. :type '(choice
  1512. (repeat :tag "Old syntax, just keywords"
  1513. (string :tag "Keyword"))
  1514. (repeat :tag "New syntax"
  1515. (cons
  1516. (choice
  1517. :tag "Interpretation"
  1518. ;;Quick and dirty way to see
  1519. ;;`org-todo-interpretations'. This takes the
  1520. ;;place of item arguments
  1521. :convert-widget
  1522. (lambda (widget)
  1523. (widget-put widget
  1524. :args (mapcar
  1525. #'(lambda (x)
  1526. (widget-convert
  1527. (cons 'const x)))
  1528. org-todo-interpretation-widgets))
  1529. widget))
  1530. (repeat
  1531. (string :tag "Keyword"))))))
  1532. (defvar org-todo-keywords-1 nil
  1533. "All TODO and DONE keywords active in a buffer.")
  1534. (make-variable-buffer-local 'org-todo-keywords-1)
  1535. (defvar org-todo-keywords-for-agenda nil)
  1536. (defvar org-done-keywords-for-agenda nil)
  1537. (defvar org-drawers-for-agenda nil)
  1538. (defvar org-todo-keyword-alist-for-agenda nil)
  1539. (defvar org-tag-alist-for-agenda nil)
  1540. (defvar org-agenda-contributing-files nil)
  1541. (defvar org-not-done-keywords nil)
  1542. (make-variable-buffer-local 'org-not-done-keywords)
  1543. (defvar org-done-keywords nil)
  1544. (make-variable-buffer-local 'org-done-keywords)
  1545. (defvar org-todo-heads nil)
  1546. (make-variable-buffer-local 'org-todo-heads)
  1547. (defvar org-todo-sets nil)
  1548. (make-variable-buffer-local 'org-todo-sets)
  1549. (defvar org-todo-log-states nil)
  1550. (make-variable-buffer-local 'org-todo-log-states)
  1551. (defvar org-todo-kwd-alist nil)
  1552. (make-variable-buffer-local 'org-todo-kwd-alist)
  1553. (defvar org-todo-key-alist nil)
  1554. (make-variable-buffer-local 'org-todo-key-alist)
  1555. (defvar org-todo-key-trigger nil)
  1556. (make-variable-buffer-local 'org-todo-key-trigger)
  1557. (defcustom org-todo-interpretation 'sequence
  1558. "Controls how TODO keywords are interpreted.
  1559. This variable is in principle obsolete and is only used for
  1560. backward compatibility, if the interpretation of todo keywords is
  1561. not given already in `org-todo-keywords'. See that variable for
  1562. more information."
  1563. :group 'org-todo
  1564. :group 'org-keywords
  1565. :type '(choice (const sequence)
  1566. (const type)))
  1567. (defcustom org-use-fast-todo-selection t
  1568. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1569. This variable describes if and under what circumstances the cycling
  1570. mechanism for TODO keywords will be replaced by a single-key, direct
  1571. selection scheme.
  1572. When nil, fast selection is never used.
  1573. When the symbol `prefix', it will be used when `org-todo' is called with
  1574. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1575. in an agenda buffer.
  1576. When t, fast selection is used by default. In this case, the prefix
  1577. argument forces cycling instead.
  1578. In all cases, the special interface is only used if access keys have actually
  1579. been assigned by the user, i.e. if keywords in the configuration are followed
  1580. by a letter in parenthesis, like TODO(t)."
  1581. :group 'org-todo
  1582. :type '(choice
  1583. (const :tag "Never" nil)
  1584. (const :tag "By default" t)
  1585. (const :tag "Only with C-u C-c C-t" prefix)))
  1586. (defcustom org-provide-todo-statistics t
  1587. "Non-nil means, update todo statistics after insert and toggle.
  1588. ALL-HEADLINES means update todo statistics by including headlines
  1589. with no TODO keyword as well, counting them as not done.
  1590. A list of TODO keywords means the same, but skip keywords that are
  1591. not in this list.
  1592. When this is set, todo statistics is updated in the parent of the
  1593. current entry each time a todo state is changed."
  1594. :group 'org-todo
  1595. :type '(choice
  1596. (const :tag "Yes, only for TODO entries" t)
  1597. (const :tag "Yes, including all entries" 'all-headlines)
  1598. (repeat :tag "Yes, for TODOs in this list"
  1599. (string :tag "TODO keyword"))
  1600. (other :tag "No TODO statistics" nil)))
  1601. (defcustom org-hierarchical-todo-statistics t
  1602. "Non-nil means, TODO statistics covers just direct children.
  1603. When nil, all entries in the subtree are considered.
  1604. This has only an effect if `org-provide-todo-statistics' is set.
  1605. To set this to nil for only a single subtree, use a COOKIE_DATA
  1606. property and include the word \"recursive\" into the value."
  1607. :group 'org-todo
  1608. :type 'boolean)
  1609. (defcustom org-after-todo-state-change-hook nil
  1610. "Hook which is run after the state of a TODO item was changed.
  1611. The new state (a string with a TODO keyword, or nil) is available in the
  1612. Lisp variable `state'."
  1613. :group 'org-todo
  1614. :type 'hook)
  1615. (defvar org-blocker-hook nil
  1616. "Hook for functions that are allowed to block a state change.
  1617. Each function gets as its single argument a property list, see
  1618. `org-trigger-hook' for more information about this list.
  1619. If any of the functions in this hook returns nil, the state change
  1620. is blocked.")
  1621. (defvar org-trigger-hook nil
  1622. "Hook for functions that are triggered by a state change.
  1623. Each function gets as its single argument a property list with at least
  1624. the following elements:
  1625. (:type type-of-change :position pos-at-entry-start
  1626. :from old-state :to new-state)
  1627. Depending on the type, more properties may be present.
  1628. This mechanism is currently implemented for:
  1629. TODO state changes
  1630. ------------------
  1631. :type todo-state-change
  1632. :from previous state (keyword as a string), or nil, or a symbol
  1633. 'todo' or 'done', to indicate the general type of state.
  1634. :to new state, like in :from")
  1635. (defcustom org-enforce-todo-dependencies nil
  1636. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1637. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1638. be blocked if any prior sibling is not yet done.
  1639. Finally, if the parent is blocked because of ordered siblings of its own,
  1640. the child will also be blocked.
  1641. This variable needs to be set before org.el is loaded, and you need to
  1642. restart Emacs after a change to make the change effective. The only way
  1643. to change is while Emacs is running is through the customize interface."
  1644. :set (lambda (var val)
  1645. (set var val)
  1646. (if val
  1647. (add-hook 'org-blocker-hook
  1648. 'org-block-todo-from-children-or-siblings-or-parent)
  1649. (remove-hook 'org-blocker-hook
  1650. 'org-block-todo-from-children-or-siblings-or-parent)))
  1651. :group 'org-todo
  1652. :type 'boolean)
  1653. (defcustom org-enforce-todo-checkbox-dependencies nil
  1654. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1655. When this is nil, checkboxes have no influence on switching TODO states.
  1656. When non-nil, you first need to check off all check boxes before the TODO
  1657. entry can be switched to DONE.
  1658. This variable needs to be set before org.el is loaded, and you need to
  1659. restart Emacs after a change to make the change effective. The only way
  1660. to change is while Emacs is running is through the customize interface."
  1661. :set (lambda (var val)
  1662. (set var val)
  1663. (if val
  1664. (add-hook 'org-blocker-hook
  1665. 'org-block-todo-from-checkboxes)
  1666. (remove-hook 'org-blocker-hook
  1667. 'org-block-todo-from-checkboxes)))
  1668. :group 'org-todo
  1669. :type 'boolean)
  1670. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1671. "Non-nil means, inserting a TODO heading is treated as state change.
  1672. So when the command \\[org-insert-todo-heading] is used, state change
  1673. logging will apply if appropriate. When nil, the new TODO item will
  1674. be inserted directly, and no logging will take place."
  1675. :group 'org-todo
  1676. :type 'boolean)
  1677. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1678. "Non-nil means, switching TODO states with S-cursor counts as state change.
  1679. This is the default behavior. However, setting this to nil allows a
  1680. convenient way to select a TODO state and bypass any logging associated
  1681. with that."
  1682. :group 'org-todo
  1683. :type 'boolean)
  1684. (defcustom org-todo-state-tags-triggers nil
  1685. "Tag changes that should be triggered by TODO state changes.
  1686. This is a list. Each entry is
  1687. (state-change (tag . flag) .......)
  1688. State-change can be a string with a state, and empty string to indicate the
  1689. state that has no TODO keyword, or it can be one of the symbols `todo'
  1690. or `done', meaning any not-done or done state, respectively."
  1691. :group 'org-todo
  1692. :group 'org-tags
  1693. :type '(repeat
  1694. (cons (choice :tag "When changing to"
  1695. (const :tag "Not-done state" todo)
  1696. (const :tag "Done state" done)
  1697. (string :tag "State"))
  1698. (repeat
  1699. (cons :tag "Tag action"
  1700. (string :tag "Tag")
  1701. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1702. (defcustom org-log-done nil
  1703. "Information to record when a task moves to the DONE state.
  1704. Possible values are:
  1705. nil Don't add anything, just change the keyword
  1706. time Add a time stamp to the task
  1707. note Prompt for a note and add it with template `org-log-note-headings'
  1708. This option can also be set with on a per-file-basis with
  1709. #+STARTUP: nologdone
  1710. #+STARTUP: logdone
  1711. #+STARTUP: lognotedone
  1712. You can have local logging settings for a subtree by setting the LOGGING
  1713. property to one or more of these keywords."
  1714. :group 'org-todo
  1715. :group 'org-progress
  1716. :type '(choice
  1717. (const :tag "No logging" nil)
  1718. (const :tag "Record CLOSED timestamp" time)
  1719. (const :tag "Record CLOSED timestamp with note." note)))
  1720. ;; Normalize old uses of org-log-done.
  1721. (cond
  1722. ((eq org-log-done t) (setq org-log-done 'time))
  1723. ((and (listp org-log-done) (memq 'done org-log-done))
  1724. (setq org-log-done 'note)))
  1725. (defcustom org-log-reschedule nil
  1726. "Information to record when the scheduling date of a tasks is modified.
  1727. Possible values are:
  1728. nil Don't add anything, just change the date
  1729. time Add a time stamp to the task
  1730. note Prompt for a note and add it with template `org-log-note-headings'
  1731. This option can also be set with on a per-file-basis with
  1732. #+STARTUP: nologreschedule
  1733. #+STARTUP: logreschedule
  1734. #+STARTUP: lognotereschedule"
  1735. :group 'org-todo
  1736. :group 'org-progress
  1737. :type '(choice
  1738. (const :tag "No logging" nil)
  1739. (const :tag "Record timestamp" time)
  1740. (const :tag "Record timestamp with note." note)))
  1741. (defcustom org-log-redeadline nil
  1742. "Information to record when the deadline date of a tasks is modified.
  1743. Possible values are:
  1744. nil Don't add anything, just change the date
  1745. time Add a time stamp to the task
  1746. note Prompt for a note and add it with template `org-log-note-headings'
  1747. This option can also be set with on a per-file-basis with
  1748. #+STARTUP: nologredeadline
  1749. #+STARTUP: logredeadline
  1750. #+STARTUP: lognoteredeadline
  1751. You can have local logging settings for a subtree by setting the LOGGING
  1752. property to one or more of these keywords."
  1753. :group 'org-todo
  1754. :group 'org-progress
  1755. :type '(choice
  1756. (const :tag "No logging" nil)
  1757. (const :tag "Record timestamp" time)
  1758. (const :tag "Record timestamp with note." note)))
  1759. (defcustom org-log-note-clock-out nil
  1760. "Non-nil means, record a note when clocking out of an item.
  1761. This can also be configured on a per-file basis by adding one of
  1762. the following lines anywhere in the buffer:
  1763. #+STARTUP: lognoteclock-out
  1764. #+STARTUP: nolognoteclock-out"
  1765. :group 'org-todo
  1766. :group 'org-progress
  1767. :type 'boolean)
  1768. (defcustom org-log-done-with-time t
  1769. "Non-nil means, the CLOSED time stamp will contain date and time.
  1770. When nil, only the date will be recorded."
  1771. :group 'org-progress
  1772. :type 'boolean)
  1773. (defcustom org-log-note-headings
  1774. '((done . "CLOSING NOTE %t")
  1775. (state . "State %-12s from %-12S %t")
  1776. (note . "Note taken on %t")
  1777. (reschedule . "Rescheduled from %S on %t")
  1778. (redeadline . "New deadline from %S on %t")
  1779. (clock-out . ""))
  1780. "Headings for notes added to entries.
  1781. The value is an alist, with the car being a symbol indicating the note
  1782. context, and the cdr is the heading to be used. The heading may also be the
  1783. empty string.
  1784. %t in the heading will be replaced by a time stamp.
  1785. %s will be replaced by the new TODO state, in double quotes.
  1786. %S will be replaced by the old TODO state, in double quotes.
  1787. %u will be replaced by the user name.
  1788. %U will be replaced by the full user name."
  1789. :group 'org-todo
  1790. :group 'org-progress
  1791. :type '(list :greedy t
  1792. (cons (const :tag "Heading when closing an item" done) string)
  1793. (cons (const :tag
  1794. "Heading when changing todo state (todo sequence only)"
  1795. state) string)
  1796. (cons (const :tag "Heading when just taking a note" note) string)
  1797. (cons (const :tag "Heading when clocking out" clock-out) string)
  1798. (cons (const :tag "Heading when rescheduling" reschedule) string)
  1799. (cons (const :tag "Heading when changing deadline" redeadline) string)))
  1800. (unless (assq 'note org-log-note-headings)
  1801. (push '(note . "%t") org-log-note-headings))
  1802. (defcustom org-log-into-drawer nil
  1803. "Non-nil means, insert state change notes and time stamps into a drawer.
  1804. When nil, state changes notes will be inserted after the headline and
  1805. any scheduling and clock lines, but not inside a drawer.
  1806. The value of this variable should be the name of the drawer to use.
  1807. LOGBOOK is proposed at the default drawer for this purpose, you can
  1808. also set this to a string to define the drawer of your choice.
  1809. A value of t is also allowed, representing \"LOGBOOK\".
  1810. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1811. will be ignored.
  1812. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1813. a subtree."
  1814. :group 'org-todo
  1815. :group 'org-progress
  1816. :type '(choice
  1817. (const :tag "Not into a drawer" nil)
  1818. (const :tag "LOGBOOK" t)
  1819. (string :tag "Other")))
  1820. (if (fboundp 'defvaralias)
  1821. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1822. (defun org-log-into-drawer ()
  1823. "Return the value of `org-log-into-drawer', but let properties overrule.
  1824. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1825. used instead of the default value."
  1826. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1827. (cond
  1828. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1829. ((equal p "t") "LOGBOOK")
  1830. (t p))))
  1831. (defcustom org-log-state-notes-insert-after-drawers nil
  1832. "Non-nil means, insert state change notes after any drawers in entry.
  1833. Only the drawers that *immediately* follow the headline and the
  1834. deadline/scheduled line are skipped.
  1835. When nil, insert notes right after the heading and perhaps the line
  1836. with deadline/scheduling if present.
  1837. This variable will have no effect if `org-log-into-drawer' is
  1838. set."
  1839. :group 'org-todo
  1840. :group 'org-progress
  1841. :type 'boolean)
  1842. (defcustom org-log-states-order-reversed t
  1843. "Non-nil means, the latest state change note will be directly after heading.
  1844. When nil, the notes will be orderer according to time."
  1845. :group 'org-todo
  1846. :group 'org-progress
  1847. :type 'boolean)
  1848. (defcustom org-log-repeat 'time
  1849. "Non-nil means, record moving through the DONE state when triggering repeat.
  1850. An auto-repeating task is immediately switched back to TODO when
  1851. marked DONE. If you are not logging state changes (by adding \"@\"
  1852. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  1853. record a closing note, there will be no record of the task moving
  1854. through DONE. This variable forces taking a note anyway.
  1855. nil Don't force a record
  1856. time Record a time stamp
  1857. note Record a note
  1858. This option can also be set with on a per-file-basis with
  1859. #+STARTUP: logrepeat
  1860. #+STARTUP: lognoterepeat
  1861. #+STARTUP: nologrepeat
  1862. You can have local logging settings for a subtree by setting the LOGGING
  1863. property to one or more of these keywords."
  1864. :group 'org-todo
  1865. :group 'org-progress
  1866. :type '(choice
  1867. (const :tag "Don't force a record" nil)
  1868. (const :tag "Force recording the DONE state" time)
  1869. (const :tag "Force recording a note with the DONE state" note)))
  1870. (defgroup org-priorities nil
  1871. "Priorities in Org-mode."
  1872. :tag "Org Priorities"
  1873. :group 'org-todo)
  1874. (defcustom org-enable-priority-commands t
  1875. "Non-nil means, priority commands are active.
  1876. When nil, these commands will be disabled, so that you never accidentally
  1877. set a priority."
  1878. :group 'org-priorities
  1879. :type 'boolean)
  1880. (defcustom org-highest-priority ?A
  1881. "The highest priority of TODO items. A character like ?A, ?B etc.
  1882. Must have a smaller ASCII number than `org-lowest-priority'."
  1883. :group 'org-priorities
  1884. :type 'character)
  1885. (defcustom org-lowest-priority ?C
  1886. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1887. Must have a larger ASCII number than `org-highest-priority'."
  1888. :group 'org-priorities
  1889. :type 'character)
  1890. (defcustom org-default-priority ?B
  1891. "The default priority of TODO items.
  1892. This is the priority an item get if no explicit priority is given."
  1893. :group 'org-priorities
  1894. :type 'character)
  1895. (defcustom org-priority-start-cycle-with-default t
  1896. "Non-nil means, start with default priority when starting to cycle.
  1897. When this is nil, the first step in the cycle will be (depending on the
  1898. command used) one higher or lower that the default priority."
  1899. :group 'org-priorities
  1900. :type 'boolean)
  1901. (defgroup org-time nil
  1902. "Options concerning time stamps and deadlines in Org-mode."
  1903. :tag "Org Time"
  1904. :group 'org)
  1905. (defcustom org-insert-labeled-timestamps-at-point nil
  1906. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1907. When nil, these labeled time stamps are forces into the second line of an
  1908. entry, just after the headline. When scheduling from the global TODO list,
  1909. the time stamp will always be forced into the second line."
  1910. :group 'org-time
  1911. :type 'boolean)
  1912. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1913. "Formats for `format-time-string' which are used for time stamps.
  1914. It is not recommended to change this constant.")
  1915. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1916. "Number of minutes to round time stamps to.
  1917. These are two values, the first applies when first creating a time stamp.
  1918. The second applies when changing it with the commands `S-up' and `S-down'.
  1919. When changing the time stamp, this means that it will change in steps
  1920. of N minutes, as given by the second value.
  1921. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1922. numbers should be factors of 60, so for example 5, 10, 15.
  1923. When this is larger than 1, you can still force an exact time-stamp by using
  1924. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1925. and by using a prefix arg to `S-up/down' to specify the exact number
  1926. of minutes to shift."
  1927. :group 'org-time
  1928. :get '(lambda (var) ; Make sure all entries have 5 elements
  1929. (if (integerp (default-value var))
  1930. (list (default-value var) 5)
  1931. (default-value var)))
  1932. :type '(list
  1933. (integer :tag "when inserting times")
  1934. (integer :tag "when modifying times")))
  1935. ;; Normalize old customizations of this variable.
  1936. (when (integerp org-time-stamp-rounding-minutes)
  1937. (setq org-time-stamp-rounding-minutes
  1938. (list org-time-stamp-rounding-minutes
  1939. org-time-stamp-rounding-minutes)))
  1940. (defcustom org-display-custom-times nil
  1941. "Non-nil means, overlay custom formats over all time stamps.
  1942. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1943. To turn this on on a per-file basis, insert anywhere in the file:
  1944. #+STARTUP: customtime"
  1945. :group 'org-time
  1946. :set 'set-default
  1947. :type 'sexp)
  1948. (make-variable-buffer-local 'org-display-custom-times)
  1949. (defcustom org-time-stamp-custom-formats
  1950. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1951. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1952. These are overlayed over the default ISO format if the variable
  1953. `org-display-custom-times' is set. Time like %H:%M should be at the
  1954. end of the second format. The custom formats are also honored by export
  1955. commands, if custom time display is turned on at the time of export."
  1956. :group 'org-time
  1957. :type 'sexp)
  1958. (defun org-time-stamp-format (&optional long inactive)
  1959. "Get the right format for a time string."
  1960. (let ((f (if long (cdr org-time-stamp-formats)
  1961. (car org-time-stamp-formats))))
  1962. (if inactive
  1963. (concat "[" (substring f 1 -1) "]")
  1964. f)))
  1965. (defcustom org-time-clocksum-format "%d:%02d"
  1966. "The format string used when creating CLOCKSUM lines, or when
  1967. org-mode generates a time duration."
  1968. :group 'org-time
  1969. :type 'string)
  1970. (defcustom org-time-clocksum-use-fractional nil
  1971. "If non-nil, \\[org-clock-display] uses fractional times.
  1972. org-mode generates a time duration."
  1973. :group 'org-time
  1974. :type 'boolean)
  1975. (defcustom org-time-clocksum-fractional-format "%.2f"
  1976. "The format string used when creating CLOCKSUM lines, or when
  1977. org-mode generates a time duration."
  1978. :group 'org-time
  1979. :type 'string)
  1980. (defcustom org-deadline-warning-days 14
  1981. "No. of days before expiration during which a deadline becomes active.
  1982. This variable governs the display in sparse trees and in the agenda.
  1983. When 0 or negative, it means use this number (the absolute value of it)
  1984. even if a deadline has a different individual lead time specified.
  1985. Custom commands can set this variable in the options section."
  1986. :group 'org-time
  1987. :group 'org-agenda-daily/weekly
  1988. :type 'integer)
  1989. (defcustom org-read-date-prefer-future t
  1990. "Non-nil means, assume future for incomplete date input from user.
  1991. This affects the following situations:
  1992. 1. The user gives a month but not a year.
  1993. For example, if it is april and you enter \"feb 2\", this will be read
  1994. as feb 2, *next* year. \"May 5\", however, will be this year.
  1995. 2. The user gives a day, but no month.
  1996. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1997. read this as the third of *next* month. However, if you enter \"17\",
  1998. it will be considered as *this* month.
  1999. If you set this variable to the symbol `time', then also the following
  2000. will work:
  2001. 3. If the user gives a time, but no day. If the time is before now,
  2002. to will be interpreted as tomorrow.
  2003. Currently none of this works for ISO week specifications.
  2004. When this option is nil, the current day, month and year will always be
  2005. used as defaults."
  2006. :group 'org-time
  2007. :type '(choice
  2008. (const :tag "Never" nil)
  2009. (const :tag "Check month and day" t)
  2010. (const :tag "Check month, day, and time" time)))
  2011. (defcustom org-read-date-display-live t
  2012. "Non-nil means, display current interpretation of date prompt live.
  2013. This display will be in an overlay, in the minibuffer."
  2014. :group 'org-time
  2015. :type 'boolean)
  2016. (defcustom org-read-date-popup-calendar t
  2017. "Non-nil means, pop up a calendar when prompting for a date.
  2018. In the calendar, the date can be selected with mouse-1. However, the
  2019. minibuffer will also be active, and you can simply enter the date as well.
  2020. When nil, only the minibuffer will be available."
  2021. :group 'org-time
  2022. :type 'boolean)
  2023. (if (fboundp 'defvaralias)
  2024. (defvaralias 'org-popup-calendar-for-date-prompt
  2025. 'org-read-date-popup-calendar))
  2026. (defcustom org-read-date-minibuffer-setup-hook nil
  2027. "Hook to be used to set up keys for the date/time interface.
  2028. Add key definitions to `minibuffer-local-map', which will be a temporary
  2029. copy."
  2030. :group 'org-time
  2031. :type 'hook)
  2032. (defcustom org-extend-today-until 0
  2033. "The hour when your day really ends. Must be an integer.
  2034. This has influence for the following applications:
  2035. - When switching the agenda to \"today\". It it is still earlier than
  2036. the time given here, the day recognized as TODAY is actually yesterday.
  2037. - When a date is read from the user and it is still before the time given
  2038. here, the current date and time will be assumed to be yesterday, 23:59.
  2039. Also, timestamps inserted in remember templates follow this rule.
  2040. IMPORTANT: This is a feature whose implementation is and likely will
  2041. remain incomplete. Really, it is only here because past midnight seems to
  2042. be the favorite working time of John Wiegley :-)"
  2043. :group 'org-time
  2044. :type 'integer)
  2045. (defcustom org-edit-timestamp-down-means-later nil
  2046. "Non-nil means, S-down will increase the time in a time stamp.
  2047. When nil, S-up will increase."
  2048. :group 'org-time
  2049. :type 'boolean)
  2050. (defcustom org-calendar-follow-timestamp-change t
  2051. "Non-nil means, make the calendar window follow timestamp changes.
  2052. When a timestamp is modified and the calendar window is visible, it will be
  2053. moved to the new date."
  2054. :group 'org-time
  2055. :type 'boolean)
  2056. (defgroup org-tags nil
  2057. "Options concerning tags in Org-mode."
  2058. :tag "Org Tags"
  2059. :group 'org)
  2060. (defcustom org-tag-alist nil
  2061. "List of tags allowed in Org-mode files.
  2062. When this list is nil, Org-mode will base TAG input on what is already in the
  2063. buffer.
  2064. The value of this variable is an alist, the car of each entry must be a
  2065. keyword as a string, the cdr may be a character that is used to select
  2066. that tag through the fast-tag-selection interface.
  2067. See the manual for details."
  2068. :group 'org-tags
  2069. :type '(repeat
  2070. (choice
  2071. (cons (string :tag "Tag name")
  2072. (character :tag "Access char"))
  2073. (list :tag "Start radio group"
  2074. (const :startgroup)
  2075. (option (string :tag "Group description")))
  2076. (list :tag "End radio group"
  2077. (const :endgroup)
  2078. (option (string :tag "Group description")))
  2079. (const :tag "New line" (:newline)))))
  2080. (defcustom org-tag-persistent-alist nil
  2081. "List of tags that will always appear in all Org-mode files.
  2082. This is in addition to any in buffer settings or customizations
  2083. of `org-tag-alist'.
  2084. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2085. The value of this variable is an alist, the car of each entry must be a
  2086. keyword as a string, the cdr may be a character that is used to select
  2087. that tag through the fast-tag-selection interface.
  2088. See the manual for details.
  2089. To disable these tags on a per-file basis, insert anywhere in the file:
  2090. #+STARTUP: noptag"
  2091. :group 'org-tags
  2092. :type '(repeat
  2093. (choice
  2094. (cons (string :tag "Tag name")
  2095. (character :tag "Access char"))
  2096. (const :tag "Start radio group" (:startgroup))
  2097. (const :tag "End radio group" (:endgroup))
  2098. (const :tag "New line" (:newline)))))
  2099. (defvar org-file-tags nil
  2100. "List of tags that can be inherited by all entries in the file.
  2101. The tags will be inherited if the variable `org-use-tag-inheritance'
  2102. says they should be.
  2103. This variable is populated from #+FILETAGS lines.")
  2104. (defcustom org-use-fast-tag-selection 'auto
  2105. "Non-nil means, use fast tag selection scheme.
  2106. This is a special interface to select and deselect tags with single keys.
  2107. When nil, fast selection is never used.
  2108. When the symbol `auto', fast selection is used if and only if selection
  2109. characters for tags have been configured, either through the variable
  2110. `org-tag-alist' or through a #+TAGS line in the buffer.
  2111. When t, fast selection is always used and selection keys are assigned
  2112. automatically if necessary."
  2113. :group 'org-tags
  2114. :type '(choice
  2115. (const :tag "Always" t)
  2116. (const :tag "Never" nil)
  2117. (const :tag "When selection characters are configured" 'auto)))
  2118. (defcustom org-fast-tag-selection-single-key nil
  2119. "Non-nil means, fast tag selection exits after first change.
  2120. When nil, you have to press RET to exit it.
  2121. During fast tag selection, you can toggle this flag with `C-c'.
  2122. This variable can also have the value `expert'. In this case, the window
  2123. displaying the tags menu is not even shown, until you press C-c again."
  2124. :group 'org-tags
  2125. :type '(choice
  2126. (const :tag "No" nil)
  2127. (const :tag "Yes" t)
  2128. (const :tag "Expert" expert)))
  2129. (defvar org-fast-tag-selection-include-todo nil
  2130. "Non-nil means, fast tags selection interface will also offer TODO states.
  2131. This is an undocumented feature, you should not rely on it.")
  2132. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2133. "The column to which tags should be indented in a headline.
  2134. If this number is positive, it specifies the column. If it is negative,
  2135. it means that the tags should be flushright to that column. For example,
  2136. -80 works well for a normal 80 character screen."
  2137. :group 'org-tags
  2138. :type 'integer)
  2139. (defcustom org-auto-align-tags t
  2140. "Non-nil means, realign tags after pro/demotion of TODO state change.
  2141. These operations change the length of a headline and therefore shift
  2142. the tags around. With this options turned on, after each such operation
  2143. the tags are again aligned to `org-tags-column'."
  2144. :group 'org-tags
  2145. :type 'boolean)
  2146. (defcustom org-use-tag-inheritance t
  2147. "Non-nil means, tags in levels apply also for sublevels.
  2148. When nil, only the tags directly given in a specific line apply there.
  2149. This may also be a list of tags that should be inherited, or a regexp that
  2150. matches tags that should be inherited. Additional control is possible
  2151. with the variable `org-tags-exclude-from-inheritance' which gives an
  2152. explicit list of tags to be excluded from inheritance., even if the value of
  2153. `org-use-tag-inheritance' would select it for inheritance.
  2154. If this option is t, a match early-on in a tree can lead to a large
  2155. number of matches in the subtree when constructing the agenda or creating
  2156. a sparse tree. If you only want to see the first match in a tree during
  2157. a search, check out the variable `org-tags-match-list-sublevels'."
  2158. :group 'org-tags
  2159. :type '(choice
  2160. (const :tag "Not" nil)
  2161. (const :tag "Always" t)
  2162. (repeat :tag "Specific tags" (string :tag "Tag"))
  2163. (regexp :tag "Tags matched by regexp")))
  2164. (defcustom org-tags-exclude-from-inheritance nil
  2165. "List of tags that should never be inherited.
  2166. This is a way to exclude a few tags from inheritance. For way to do
  2167. the opposite, to actively allow inheritance for selected tags,
  2168. see the variable `org-use-tag-inheritance'."
  2169. :group 'org-tags
  2170. :type '(repeat (string :tag "Tag")))
  2171. (defun org-tag-inherit-p (tag)
  2172. "Check if TAG is one that should be inherited."
  2173. (cond
  2174. ((member tag org-tags-exclude-from-inheritance) nil)
  2175. ((eq org-use-tag-inheritance t) t)
  2176. ((not org-use-tag-inheritance) nil)
  2177. ((stringp org-use-tag-inheritance)
  2178. (string-match org-use-tag-inheritance tag))
  2179. ((listp org-use-tag-inheritance)
  2180. (member tag org-use-tag-inheritance))
  2181. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2182. (defcustom org-tags-match-list-sublevels t
  2183. "Non-nil means list also sublevels of headlines matching a search.
  2184. This variable applies to tags/property searches, and also to stuck
  2185. projects because this search is based on a tags match as well.
  2186. When set to the symbol `indented', sublevels are indented with
  2187. leading dots.
  2188. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2189. the sublevels of a headline matching a tag search often also match
  2190. the same search. Listing all of them can create very long lists.
  2191. Setting this variable to nil causes subtrees of a match to be skipped.
  2192. This variable is semi-obsolete and probably should always be true. It
  2193. is better to limit inheritance to certain tags using the variables
  2194. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2195. :group 'org-tags
  2196. :type '(choice
  2197. (const :tag "No, don't list them" nil)
  2198. (const :tag "Yes, do list them" t)
  2199. (const :tag "List them, indented with leading dots" indented)))
  2200. (defcustom org-tags-sort-function nil
  2201. "When set, tags are sorted using this function as a comparator"
  2202. :group 'org-tags
  2203. :type '(choice
  2204. (const :tag "No sorting" nil)
  2205. (const :tag "Alphabetical" string<)
  2206. (const :tag "Reverse alphabetical" string>)
  2207. (function :tag "Custom function" nil)))
  2208. (defvar org-tags-history nil
  2209. "History of minibuffer reads for tags.")
  2210. (defvar org-last-tags-completion-table nil
  2211. "The last used completion table for tags.")
  2212. (defvar org-after-tags-change-hook nil
  2213. "Hook that is run after the tags in a line have changed.")
  2214. (defgroup org-properties nil
  2215. "Options concerning properties in Org-mode."
  2216. :tag "Org Properties"
  2217. :group 'org)
  2218. (defcustom org-property-format "%-10s %s"
  2219. "How property key/value pairs should be formatted by `indent-line'.
  2220. When `indent-line' hits a property definition, it will format the line
  2221. according to this format, mainly to make sure that the values are
  2222. lined-up with respect to each other."
  2223. :group 'org-properties
  2224. :type 'string)
  2225. (defcustom org-use-property-inheritance nil
  2226. "Non-nil means, properties apply also for sublevels.
  2227. This setting is chiefly used during property searches. Turning it on can
  2228. cause significant overhead when doing a search, which is why it is not
  2229. on by default.
  2230. When nil, only the properties directly given in the current entry count.
  2231. When t, every property is inherited. The value may also be a list of
  2232. properties that should have inheritance, or a regular expression matching
  2233. properties that should be inherited.
  2234. However, note that some special properties use inheritance under special
  2235. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2236. and the properties ending in \"_ALL\" when they are used as descriptor
  2237. for valid values of a property.
  2238. Note for programmers:
  2239. When querying an entry with `org-entry-get', you can control if inheritance
  2240. should be used. By default, `org-entry-get' looks only at the local
  2241. properties. You can request inheritance by setting the inherit argument
  2242. to t (to force inheritance) or to `selective' (to respect the setting
  2243. in this variable)."
  2244. :group 'org-properties
  2245. :type '(choice
  2246. (const :tag "Not" nil)
  2247. (const :tag "Always" t)
  2248. (repeat :tag "Specific properties" (string :tag "Property"))
  2249. (regexp :tag "Properties matched by regexp")))
  2250. (defun org-property-inherit-p (property)
  2251. "Check if PROPERTY is one that should be inherited."
  2252. (cond
  2253. ((eq org-use-property-inheritance t) t)
  2254. ((not org-use-property-inheritance) nil)
  2255. ((stringp org-use-property-inheritance)
  2256. (string-match org-use-property-inheritance property))
  2257. ((listp org-use-property-inheritance)
  2258. (member property org-use-property-inheritance))
  2259. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2260. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2261. "The default column format, if no other format has been defined.
  2262. This variable can be set on the per-file basis by inserting a line
  2263. #+COLUMNS: %25ITEM ....."
  2264. :group 'org-properties
  2265. :type 'string)
  2266. (defcustom org-columns-ellipses ".."
  2267. "The ellipses to be used when a field in column view is truncated.
  2268. When this is the empty string, as many characters as possible are shown,
  2269. but then there will be no visual indication that the field has been truncated.
  2270. When this is a string of length N, the last N characters of a truncated
  2271. field are replaced by this string. If the column is narrower than the
  2272. ellipses string, only part of the ellipses string will be shown."
  2273. :group 'org-properties
  2274. :type 'string)
  2275. (defcustom org-columns-modify-value-for-display-function nil
  2276. "Function that modifies values for display in column view.
  2277. For example, it can be used to cut out a certain part from a time stamp.
  2278. The function must take 2 arguments:
  2279. column-title The title of the column (*not* the property name)
  2280. value The value that should be modified.
  2281. The function should return the value that should be displayed,
  2282. or nil if the normal value should be used."
  2283. :group 'org-properties
  2284. :type 'function)
  2285. (defcustom org-effort-property "Effort"
  2286. "The property that is being used to keep track of effort estimates.
  2287. Effort estimates given in this property need to have the format H:MM."
  2288. :group 'org-properties
  2289. :group 'org-progress
  2290. :type '(string :tag "Property"))
  2291. (defconst org-global-properties-fixed
  2292. '(("VISIBILITY_ALL" . "folded children content all")
  2293. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2294. "List of property/value pairs that can be inherited by any entry.
  2295. These are fixed values, for the preset properties. The user variable
  2296. that can be used to add to this list is `org-global-properties'.
  2297. The entries in this list are cons cells where the car is a property
  2298. name and cdr is a string with the value. If the value represents
  2299. multiple items like an \"_ALL\" property, separate the items by
  2300. spaces.")
  2301. (defcustom org-global-properties nil
  2302. "List of property/value pairs that can be inherited by any entry.
  2303. This list will be combined with the constant `org-global-properties-fixed'.
  2304. The entries in this list are cons cells where the car is a property
  2305. name and cdr is a string with the value.
  2306. You can set buffer-local values for the same purpose in the variable
  2307. `org-file-properties' this by adding lines like
  2308. #+PROPERTY: NAME VALUE"
  2309. :group 'org-properties
  2310. :type '(repeat
  2311. (cons (string :tag "Property")
  2312. (string :tag "Value"))))
  2313. (defvar org-file-properties nil
  2314. "List of property/value pairs that can be inherited by any entry.
  2315. Valid for the current buffer.
  2316. This variable is populated from #+PROPERTY lines.")
  2317. (make-variable-buffer-local 'org-file-properties)
  2318. (defgroup org-agenda nil
  2319. "Options concerning agenda views in Org-mode."
  2320. :tag "Org Agenda"
  2321. :group 'org)
  2322. (defvar org-category nil
  2323. "Variable used by org files to set a category for agenda display.
  2324. Such files should use a file variable to set it, for example
  2325. # -*- mode: org; org-category: \"ELisp\"
  2326. or contain a special line
  2327. #+CATEGORY: ELisp
  2328. If the file does not specify a category, then file's base name
  2329. is used instead.")
  2330. (make-variable-buffer-local 'org-category)
  2331. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2332. (defcustom org-agenda-files nil
  2333. "The files to be used for agenda display.
  2334. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2335. \\[org-remove-file]. You can also use customize to edit the list.
  2336. If an entry is a directory, all files in that directory that are matched by
  2337. `org-agenda-file-regexp' will be part of the file list.
  2338. If the value of the variable is not a list but a single file name, then
  2339. the list of agenda files is actually stored and maintained in that file, one
  2340. agenda file per line."
  2341. :group 'org-agenda
  2342. :type '(choice
  2343. (repeat :tag "List of files and directories" file)
  2344. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2345. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2346. "Regular expression to match files for `org-agenda-files'.
  2347. If any element in the list in that variable contains a directory instead
  2348. of a normal file, all files in that directory that are matched by this
  2349. regular expression will be included."
  2350. :group 'org-agenda
  2351. :type 'regexp)
  2352. (defcustom org-agenda-text-search-extra-files nil
  2353. "List of extra files to be searched by text search commands.
  2354. These files will be search in addition to the agenda files by the
  2355. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2356. Note that these files will only be searched for text search commands,
  2357. not for the other agenda views like todo lists, tag searches or the weekly
  2358. agenda. This variable is intended to list notes and possibly archive files
  2359. that should also be searched by these two commands.
  2360. In fact, if the first element in the list is the symbol `agenda-archives',
  2361. than all archive files of all agenda files will be added to the search
  2362. scope."
  2363. :group 'org-agenda
  2364. :type '(set :greedy t
  2365. (const :tag "Agenda Archives" agenda-archives)
  2366. (repeat :inline t (file))))
  2367. (if (fboundp 'defvaralias)
  2368. (defvaralias 'org-agenda-multi-occur-extra-files
  2369. 'org-agenda-text-search-extra-files))
  2370. (defcustom org-agenda-skip-unavailable-files nil
  2371. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2372. A nil value means to remove them, after a query, from the list."
  2373. :group 'org-agenda
  2374. :type 'boolean)
  2375. (defcustom org-calendar-to-agenda-key [?c]
  2376. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2377. The command `org-calendar-goto-agenda' will be bound to this key. The
  2378. default is the character `c' because then `c' can be used to switch back and
  2379. forth between agenda and calendar."
  2380. :group 'org-agenda
  2381. :type 'sexp)
  2382. (defcustom org-calendar-agenda-action-key [?k]
  2383. "The key to be installed in `calendar-mode-map' for agenda-action.
  2384. The command `org-agenda-action' will be bound to this key. The
  2385. default is the character `k' because we use the same key in the agenda."
  2386. :group 'org-agenda
  2387. :type 'sexp)
  2388. (defcustom org-calendar-insert-diary-entry-key [?i]
  2389. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2390. This option is irrelevant until `org-agenda-diary-file' has been configured
  2391. to point to an Org-mode file. When that is the case, the command
  2392. `org-agenda-diary-entry' will be bound to the key given here, by default
  2393. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2394. if you want to continue doing this, you need to change this to a defferent
  2395. key."
  2396. :group 'org-agenda
  2397. :type 'sexp)
  2398. (defcustom org-agenda-diary-file 'diary-file
  2399. "File to which to add new entries with the `i' key in agenda and calendar.
  2400. When this is the symbol `diary-file', the functionality in the Emacs
  2401. calendar will be used to add entries to the `diary-file'. But when this
  2402. points to a file, `org-agenda-diary-entry' will be used instead."
  2403. :group 'org-agenda
  2404. :type '(choice
  2405. (const :tag "The standard Emacs diary file" diary-file)
  2406. (file :tag "Special Org file diary entries")))
  2407. (eval-after-load "calendar"
  2408. '(progn
  2409. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2410. 'org-calendar-goto-agenda)
  2411. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2412. 'org-agenda-action)
  2413. (add-hook 'calendar-mode-hook
  2414. (lambda ()
  2415. (unless (eq org-agenda-diary-file 'diary-file)
  2416. (define-key calendar-mode-map
  2417. org-calendar-insert-diary-entry-key
  2418. 'org-agenda-diary-entry))))))
  2419. (defgroup org-latex nil
  2420. "Options for embedding LaTeX code into Org-mode."
  2421. :tag "Org LaTeX"
  2422. :group 'org)
  2423. (defcustom org-format-latex-options
  2424. '(:foreground default :background default :scale 1.0
  2425. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2426. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2427. "Options for creating images from LaTeX fragments.
  2428. This is a property list with the following properties:
  2429. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2430. `default' means use the foreground of the default face.
  2431. :background the background color, or \"Transparent\".
  2432. `default' means use the background of the default face.
  2433. :scale a scaling factor for the size of the images.
  2434. :html-foreground, :html-background, :html-scale
  2435. the same numbers for HTML export.
  2436. :matchers a list indicating which matchers should be used to
  2437. find LaTeX fragments. Valid members of this list are:
  2438. \"begin\" find environments
  2439. \"$1\" find single characters surrounded by $.$
  2440. \"$\" find math expressions surrounded by $...$
  2441. \"$$\" find math expressions surrounded by $$....$$
  2442. \"\\(\" find math expressions surrounded by \\(...\\)
  2443. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2444. :group 'org-latex
  2445. :type 'plist)
  2446. (defcustom org-format-latex-header "\\documentclass{article}
  2447. \\usepackage{amssymb}
  2448. \\usepackage[usenames]{color}
  2449. \\usepackage{amsmath}
  2450. \\usepackage{latexsym}
  2451. \\usepackage[mathscr]{eucal}
  2452. \\pagestyle{empty} % do not remove
  2453. % The settings below are copied from fullpage.sty
  2454. \\setlength{\\textwidth}{\\paperwidth}
  2455. \\addtolength{\\textwidth}{-3cm}
  2456. \\setlength{\\oddsidemargin}{1.5cm}
  2457. \\addtolength{\\oddsidemargin}{-2.54cm}
  2458. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2459. \\setlength{\\textheight}{\\paperheight}
  2460. \\addtolength{\\textheight}{-\\headheight}
  2461. \\addtolength{\\textheight}{-\\headsep}
  2462. \\addtolength{\\textheight}{-\\footskip}
  2463. \\addtolength{\\textheight}{-3cm}
  2464. \\setlength{\\topmargin}{1.5cm}
  2465. \\addtolength{\\topmargin}{-2.54cm}"
  2466. "The document header used for processing LaTeX fragments.
  2467. It is imperative that this header make sure that no page number
  2468. appears on the page."
  2469. :group 'org-latex
  2470. :type 'string)
  2471. (defgroup org-font-lock nil
  2472. "Font-lock settings for highlighting in Org-mode."
  2473. :tag "Org Font Lock"
  2474. :group 'org)
  2475. (defcustom org-level-color-stars-only nil
  2476. "Non-nil means fontify only the stars in each headline.
  2477. When nil, the entire headline is fontified.
  2478. Changing it requires restart of `font-lock-mode' to become effective
  2479. also in regions already fontified."
  2480. :group 'org-font-lock
  2481. :type 'boolean)
  2482. (defcustom org-hide-leading-stars nil
  2483. "Non-nil means, hide the first N-1 stars in a headline.
  2484. This works by using the face `org-hide' for these stars. This
  2485. face is white for a light background, and black for a dark
  2486. background. You may have to customize the face `org-hide' to
  2487. make this work.
  2488. Changing it requires restart of `font-lock-mode' to become effective
  2489. also in regions already fontified.
  2490. You may also set this on a per-file basis by adding one of the following
  2491. lines to the buffer:
  2492. #+STARTUP: hidestars
  2493. #+STARTUP: showstars"
  2494. :group 'org-font-lock
  2495. :type 'boolean)
  2496. (defcustom org-fontify-done-headline nil
  2497. "Non-nil means, change the face of a headline if it is marked DONE.
  2498. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2499. When this is non-nil, the headline after the keyword is set to the
  2500. `org-headline-done' as an additional indication."
  2501. :group 'org-font-lock
  2502. :type 'boolean)
  2503. (defcustom org-fontify-emphasized-text t
  2504. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2505. Changing this variable requires a restart of Emacs to take effect."
  2506. :group 'org-font-lock
  2507. :type 'boolean)
  2508. (defcustom org-fontify-whole-heading-line nil
  2509. "Non-nil means fontify the whole line for headings.
  2510. This is useful when setting a background color for the
  2511. org-leve-* faces."
  2512. :group 'org-font-lock
  2513. :type 'boolean)
  2514. (defcustom org-highlight-latex-fragments-and-specials nil
  2515. "Non-nil means, fontify what is treated specially by the exporters."
  2516. :group 'org-font-lock
  2517. :type 'boolean)
  2518. (defcustom org-hide-emphasis-markers nil
  2519. "Non-nil mean font-lock should hide the emphasis marker characters."
  2520. :group 'org-font-lock
  2521. :type 'boolean)
  2522. (defvar org-emph-re nil
  2523. "Regular expression for matching emphasis.")
  2524. (defvar org-verbatim-re nil
  2525. "Regular expression for matching verbatim text.")
  2526. (defvar org-emphasis-regexp-components) ; defined just below
  2527. (defvar org-emphasis-alist) ; defined just below
  2528. (defun org-set-emph-re (var val)
  2529. "Set variable and compute the emphasis regular expression."
  2530. (set var val)
  2531. (when (and (boundp 'org-emphasis-alist)
  2532. (boundp 'org-emphasis-regexp-components)
  2533. org-emphasis-alist org-emphasis-regexp-components)
  2534. (let* ((e org-emphasis-regexp-components)
  2535. (pre (car e))
  2536. (post (nth 1 e))
  2537. (border (nth 2 e))
  2538. (body (nth 3 e))
  2539. (nl (nth 4 e))
  2540. (body1 (concat body "*?"))
  2541. (markers (mapconcat 'car org-emphasis-alist ""))
  2542. (vmarkers (mapconcat
  2543. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2544. org-emphasis-alist "")))
  2545. ;; make sure special characters appear at the right position in the class
  2546. (if (string-match "\\^" markers)
  2547. (setq markers (concat (replace-match "" t t markers) "^")))
  2548. (if (string-match "-" markers)
  2549. (setq markers (concat (replace-match "" t t markers) "-")))
  2550. (if (string-match "\\^" vmarkers)
  2551. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2552. (if (string-match "-" vmarkers)
  2553. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2554. (if (> nl 0)
  2555. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2556. (int-to-string nl) "\\}")))
  2557. ;; Make the regexp
  2558. (setq org-emph-re
  2559. (concat "\\([" pre "]\\|^\\)"
  2560. "\\("
  2561. "\\([" markers "]\\)"
  2562. "\\("
  2563. "[^" border "]\\|"
  2564. "[^" border "]"
  2565. body1
  2566. "[^" border "]"
  2567. "\\)"
  2568. "\\3\\)"
  2569. "\\([" post "]\\|$\\)"))
  2570. (setq org-verbatim-re
  2571. (concat "\\([" pre "]\\|^\\)"
  2572. "\\("
  2573. "\\([" vmarkers "]\\)"
  2574. "\\("
  2575. "[^" border "]\\|"
  2576. "[^" border "]"
  2577. body1
  2578. "[^" border "]"
  2579. "\\)"
  2580. "\\3\\)"
  2581. "\\([" post "]\\|$\\)")))))
  2582. (defcustom org-emphasis-regexp-components
  2583. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2584. "Components used to build the regular expression for emphasis.
  2585. This is a list with 6 entries. Terminology: In an emphasis string
  2586. like \" *strong word* \", we call the initial space PREMATCH, the final
  2587. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2588. and \"trong wor\" is the body. The different components in this variable
  2589. specify what is allowed/forbidden in each part:
  2590. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2591. post Chars allowed as postmatch. End of line will be allowed too.
  2592. border The chars *forbidden* as border characters.
  2593. body-regexp A regexp like \".\" to match a body character. Don't use
  2594. non-shy groups here, and don't allow newline here.
  2595. newline The maximum number of newlines allowed in an emphasis exp.
  2596. Use customize to modify this, or restart Emacs after changing it."
  2597. :group 'org-font-lock
  2598. :set 'org-set-emph-re
  2599. :type '(list
  2600. (sexp :tag "Allowed chars in pre ")
  2601. (sexp :tag "Allowed chars in post ")
  2602. (sexp :tag "Forbidden chars in border ")
  2603. (sexp :tag "Regexp for body ")
  2604. (integer :tag "number of newlines allowed")
  2605. (option (boolean :tag "Please ignore this button"))))
  2606. (defcustom org-emphasis-alist
  2607. `(("*" bold "<b>" "</b>")
  2608. ("/" italic "<i>" "</i>")
  2609. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2610. ("=" org-code "<code>" "</code>" verbatim)
  2611. ("~" org-verbatim "<code>" "</code>" verbatim)
  2612. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2613. "<del>" "</del>")
  2614. )
  2615. "Special syntax for emphasized text.
  2616. Text starting and ending with a special character will be emphasized, for
  2617. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2618. characters, the face to be used by font-lock for highlighting in Org-mode
  2619. Emacs buffers, and the HTML tags to be used for this.
  2620. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2621. Use customize to modify this, or restart Emacs after changing it."
  2622. :group 'org-font-lock
  2623. :set 'org-set-emph-re
  2624. :type '(repeat
  2625. (list
  2626. (string :tag "Marker character")
  2627. (choice
  2628. (face :tag "Font-lock-face")
  2629. (plist :tag "Face property list"))
  2630. (string :tag "HTML start tag")
  2631. (string :tag "HTML end tag")
  2632. (option (const verbatim)))))
  2633. (defvar org-protecting-blocks
  2634. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2635. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2636. This is needed for font-lock setup.")
  2637. ;;; Miscellaneous options
  2638. (defgroup org-completion nil
  2639. "Completion in Org-mode."
  2640. :tag "Org Completion"
  2641. :group 'org)
  2642. (defcustom org-completion-use-ido nil
  2643. "Non-nil means, use ido completion wherever possible.
  2644. Note that `ido-mode' must be active for this variable to be relevant.
  2645. If you decide to turn this variable on, you might well want to turn off
  2646. `org-outline-path-complete-in-steps'.
  2647. See also `org-completion-use-iswitchb'."
  2648. :group 'org-completion
  2649. :type 'boolean)
  2650. (defcustom org-completion-use-iswitchb nil
  2651. "Non-nil means, use iswitchb completion wherever possible.
  2652. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2653. If you decide to turn this variable on, you might well want to turn off
  2654. `org-outline-path-complete-in-steps'.
  2655. Note that thi variable has only an effect if `org-completion-use-ido' is nil."
  2656. :group 'org-completion
  2657. :type 'boolean)
  2658. (defcustom org-completion-fallback-command 'hippie-expand
  2659. "The expansion command called by \\[org-complete] in normal context.
  2660. Normal means, no org-mode-specific context."
  2661. :group 'org-completion
  2662. :type 'function)
  2663. ;;; Functions and variables from ther packages
  2664. ;; Declared here to avoid compiler warnings
  2665. ;; XEmacs only
  2666. (defvar outline-mode-menu-heading)
  2667. (defvar outline-mode-menu-show)
  2668. (defvar outline-mode-menu-hide)
  2669. (defvar zmacs-regions) ; XEmacs regions
  2670. ;; Emacs only
  2671. (defvar mark-active)
  2672. ;; Various packages
  2673. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2674. (declare-function calendar-forward-day "cal-move" (arg))
  2675. (declare-function calendar-goto-date "cal-move" (date))
  2676. (declare-function calendar-goto-today "cal-move" ())
  2677. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2678. (defvar calc-embedded-close-formula)
  2679. (defvar calc-embedded-open-formula)
  2680. (declare-function cdlatex-tab "ext:cdlatex" ())
  2681. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2682. (defvar font-lock-unfontify-region-function)
  2683. (declare-function iswitchb-read-buffer "iswitchb"
  2684. (prompt &optional default require-match start matches-set))
  2685. (defvar iswitchb-temp-buflist)
  2686. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2687. (defvar org-agenda-tags-todo-honor-ignore-options)
  2688. (declare-function org-agenda-skip "org-agenda" ())
  2689. (declare-function org-format-agenda-item "org-agenda"
  2690. (extra txt &optional category tags dotime noprefix remove-re))
  2691. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2692. (declare-function org-agenda-change-all-lines "org-agenda"
  2693. (newhead hdmarker &optional fixface just-this))
  2694. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2695. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2696. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2697. (beg end))
  2698. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2699. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2700. "org-agenda" (&optional end))
  2701. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2702. (declare-function org-indent-mode "org-indent" (&optional arg))
  2703. (declare-function parse-time-string "parse-time" (string))
  2704. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  2705. (defvar remember-data-file)
  2706. (defvar texmathp-why)
  2707. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2708. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2709. (defvar w3m-current-url)
  2710. (defvar w3m-current-title)
  2711. (defvar org-latex-regexps)
  2712. ;;; Autoload and prepare some org modules
  2713. ;; Some table stuff that needs to be defined here, because it is used
  2714. ;; by the functions setting up org-mode or checking for table context.
  2715. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2716. "Detects an org-type or table-type table.")
  2717. (defconst org-table-line-regexp "^[ \t]*|"
  2718. "Detects an org-type table line.")
  2719. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2720. "Detects an org-type table line.")
  2721. (defconst org-table-hline-regexp "^[ \t]*|-"
  2722. "Detects an org-type table hline.")
  2723. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2724. "Detects a table-type table hline.")
  2725. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2726. "Searching from within a table (any type) this finds the first line
  2727. outside the table.")
  2728. ;; Autoload the functions in org-table.el that are needed by functions here.
  2729. (eval-and-compile
  2730. (org-autoload "org-table"
  2731. '(org-table-align org-table-begin org-table-blank-field
  2732. org-table-convert org-table-convert-region org-table-copy-down
  2733. org-table-copy-region org-table-create
  2734. org-table-create-or-convert-from-region
  2735. org-table-create-with-table.el org-table-current-dline
  2736. org-table-cut-region org-table-delete-column org-table-edit-field
  2737. org-table-edit-formulas org-table-end org-table-eval-formula
  2738. org-table-export org-table-field-info
  2739. org-table-get-stored-formulas org-table-goto-column
  2740. org-table-hline-and-move org-table-import org-table-insert-column
  2741. org-table-insert-hline org-table-insert-row org-table-iterate
  2742. org-table-justify-field-maybe org-table-kill-row
  2743. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2744. org-table-move-column org-table-move-column-left
  2745. org-table-move-column-right org-table-move-row
  2746. org-table-move-row-down org-table-move-row-up
  2747. org-table-next-field org-table-next-row org-table-paste-rectangle
  2748. org-table-previous-field org-table-recalculate
  2749. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2750. org-table-toggle-coordinate-overlays
  2751. org-table-toggle-formula-debugger org-table-wrap-region
  2752. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2753. (defun org-at-table-p (&optional table-type)
  2754. "Return t if the cursor is inside an org-type table.
  2755. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2756. (if org-enable-table-editor
  2757. (save-excursion
  2758. (beginning-of-line 1)
  2759. (looking-at (if table-type org-table-any-line-regexp
  2760. org-table-line-regexp)))
  2761. nil))
  2762. (defsubst org-table-p () (org-at-table-p))
  2763. (defun org-at-table.el-p ()
  2764. "Return t if and only if we are at a table.el table."
  2765. (and (org-at-table-p 'any)
  2766. (save-excursion
  2767. (goto-char (org-table-begin 'any))
  2768. (looking-at org-table1-hline-regexp))))
  2769. (defun org-table-recognize-table.el ()
  2770. "If there is a table.el table nearby, recognize it and move into it."
  2771. (if org-table-tab-recognizes-table.el
  2772. (if (org-at-table.el-p)
  2773. (progn
  2774. (beginning-of-line 1)
  2775. (if (looking-at org-table-dataline-regexp)
  2776. nil
  2777. (if (looking-at org-table1-hline-regexp)
  2778. (progn
  2779. (beginning-of-line 2)
  2780. (if (looking-at org-table-any-border-regexp)
  2781. (beginning-of-line -1)))))
  2782. (if (re-search-forward "|" (org-table-end t) t)
  2783. (progn
  2784. (require 'table)
  2785. (if (table--at-cell-p (point))
  2786. t
  2787. (message "recognizing table.el table...")
  2788. (table-recognize-table)
  2789. (message "recognizing table.el table...done")))
  2790. (error "This should not happen..."))
  2791. t)
  2792. nil)
  2793. nil))
  2794. (defun org-at-table-hline-p ()
  2795. "Return t if the cursor is inside a hline in a table."
  2796. (if org-enable-table-editor
  2797. (save-excursion
  2798. (beginning-of-line 1)
  2799. (looking-at org-table-hline-regexp))
  2800. nil))
  2801. (defvar org-table-clean-did-remove-column nil)
  2802. (defun org-table-map-tables (function)
  2803. "Apply FUNCTION to the start of all tables in the buffer."
  2804. (save-excursion
  2805. (save-restriction
  2806. (widen)
  2807. (goto-char (point-min))
  2808. (while (re-search-forward org-table-any-line-regexp nil t)
  2809. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2810. (beginning-of-line 1)
  2811. (when (looking-at org-table-line-regexp)
  2812. (save-excursion (funcall function))
  2813. (or (looking-at org-table-line-regexp)
  2814. (forward-char 1)))
  2815. (re-search-forward org-table-any-border-regexp nil 1))))
  2816. (message "Mapping tables: done"))
  2817. ;; Declare and autoload functions from org-exp.el & Co
  2818. (declare-function org-default-export-plist "org-exp")
  2819. (declare-function org-infile-export-plist "org-exp")
  2820. (declare-function org-get-current-options "org-exp")
  2821. (eval-and-compile
  2822. (org-autoload "org-exp"
  2823. '(org-export org-export-visible
  2824. org-insert-export-options-template
  2825. org-table-clean-before-export))
  2826. (org-autoload "org-ascii"
  2827. '(org-export-as-ascii org-export-ascii-preprocess
  2828. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2829. org-export-region-as-ascii))
  2830. (org-autoload "org-html"
  2831. '(org-export-as-html-and-open
  2832. org-export-as-html-batch org-export-as-html-to-buffer
  2833. org-replace-region-by-html org-export-region-as-html
  2834. org-export-as-html))
  2835. (org-autoload "org-icalendar"
  2836. '(org-export-icalendar-this-file
  2837. org-export-icalendar-all-agenda-files
  2838. org-export-icalendar-combine-agenda-files))
  2839. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2840. ;; Declare and autoload functions from org-agenda.el
  2841. (eval-and-compile
  2842. (org-autoload "org-agenda"
  2843. '(org-agenda org-agenda-list org-search-view
  2844. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2845. org-diary org-agenda-to-appt
  2846. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2847. ;; Autoload org-remember
  2848. (eval-and-compile
  2849. (org-autoload "org-remember"
  2850. '(org-remember-insinuate org-remember-annotation
  2851. org-remember-apply-template org-remember org-remember-handler)))
  2852. ;; Autoload org-clock.el
  2853. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2854. (beg end))
  2855. (declare-function org-clock-update-mode-line "org-clock" ())
  2856. (declare-function org-resolve-clocks "org-clock"
  2857. (&optional also-non-dangling-p prompt last-valid))
  2858. (defvar org-clock-start-time)
  2859. (defvar org-clock-marker (make-marker)
  2860. "Marker recording the last clock-in.")
  2861. (defvar org-clock-hd-marker (make-marker)
  2862. "Marker recording the last clock-in, but the headline position.")
  2863. (defvar org-clock-heading ""
  2864. "The heading of the current clock entry.")
  2865. (defun org-clock-is-active ()
  2866. "Return non-nil if clock is currently running.
  2867. The return value is actually the clock marker."
  2868. (marker-buffer org-clock-marker))
  2869. (eval-and-compile
  2870. (org-autoload
  2871. "org-clock"
  2872. '(org-clock-in org-clock-out org-clock-cancel
  2873. org-clock-goto org-clock-sum org-clock-display
  2874. org-clock-remove-overlays org-clock-report
  2875. org-clocktable-shift org-dblock-write:clocktable
  2876. org-get-clocktable org-resolve-clocks)))
  2877. (defun org-clock-update-time-maybe ()
  2878. "If this is a CLOCK line, update it and return t.
  2879. Otherwise, return nil."
  2880. (interactive)
  2881. (save-excursion
  2882. (beginning-of-line 1)
  2883. (skip-chars-forward " \t")
  2884. (when (looking-at org-clock-string)
  2885. (let ((re (concat "[ \t]*" org-clock-string
  2886. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2887. "\\([ \t]*=>.*\\)?\\)?"))
  2888. ts te h m s neg)
  2889. (cond
  2890. ((not (looking-at re))
  2891. nil)
  2892. ((not (match-end 2))
  2893. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2894. (> org-clock-marker (point))
  2895. (<= org-clock-marker (point-at-eol)))
  2896. ;; The clock is running here
  2897. (setq org-clock-start-time
  2898. (apply 'encode-time
  2899. (org-parse-time-string (match-string 1))))
  2900. (org-clock-update-mode-line)))
  2901. (t
  2902. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2903. (end-of-line 1)
  2904. (setq ts (match-string 1)
  2905. te (match-string 3))
  2906. (setq s (- (org-float-time
  2907. (apply 'encode-time (org-parse-time-string te)))
  2908. (org-float-time
  2909. (apply 'encode-time (org-parse-time-string ts))))
  2910. neg (< s 0)
  2911. s (abs s)
  2912. h (floor (/ s 3600))
  2913. s (- s (* 3600 h))
  2914. m (floor (/ s 60))
  2915. s (- s (* 60 s)))
  2916. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2917. t))))))
  2918. (defun org-check-running-clock ()
  2919. "Check if the current buffer contains the running clock.
  2920. If yes, offer to stop it and to save the buffer with the changes."
  2921. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2922. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2923. (buffer-name))))
  2924. (org-clock-out)
  2925. (when (y-or-n-p "Save changed buffer?")
  2926. (save-buffer))))
  2927. (defun org-clocktable-try-shift (dir n)
  2928. "Check if this line starts a clock table, if yes, shift the time block."
  2929. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2930. (org-clocktable-shift dir n)))
  2931. ;; Autoload org-timer.el
  2932. (eval-and-compile
  2933. (org-autoload
  2934. "org-timer"
  2935. '(org-timer-start org-timer org-timer-item
  2936. org-timer-change-times-in-region
  2937. org-timer-set-timer
  2938. org-timer-reset-timers
  2939. org-timer-show-remaining-time)))
  2940. ;; Autoload org-feed.el
  2941. (eval-and-compile
  2942. (org-autoload
  2943. "org-feed"
  2944. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2945. ;; Autoload org-indent.el
  2946. ;; Define the variable already here, to make sure we have it.
  2947. (defvar org-indent-mode nil
  2948. "Non-nil if Org-Indent mode is enabled.
  2949. Use the command `org-indent-mode' to change this variable.")
  2950. (eval-and-compile
  2951. (org-autoload
  2952. "org-indent"
  2953. '(org-indent-mode)))
  2954. ;; Autoload org-mobile.el
  2955. (eval-and-compile
  2956. (org-autoload
  2957. "org-mobile"
  2958. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  2959. ;; Autoload archiving code
  2960. ;; The stuff that is needed for cycling and tags has to be defined here.
  2961. (defgroup org-archive nil
  2962. "Options concerning archiving in Org-mode."
  2963. :tag "Org Archive"
  2964. :group 'org-structure)
  2965. (defcustom org-archive-location "%s_archive::"
  2966. "The location where subtrees should be archived.
  2967. The value of this variable is a string, consisting of two parts,
  2968. separated by a double-colon. The first part is a filename and
  2969. the second part is a headline.
  2970. When the filename is omitted, archiving happens in the same file.
  2971. %s in the filename will be replaced by the current file
  2972. name (without the directory part). Archiving to a different file
  2973. is useful to keep archived entries from contributing to the
  2974. Org-mode Agenda.
  2975. The archived entries will be filed as subtrees of the specified
  2976. headline. When the headline is omitted, the subtrees are simply
  2977. filed away at the end of the file, as top-level entries. Also in
  2978. the heading you can use %s to represent the file name, this can be
  2979. useful when using the same archive for a number of different files.
  2980. Here are a few examples:
  2981. \"%s_archive::\"
  2982. If the current file is Projects.org, archive in file
  2983. Projects.org_archive, as top-level trees. This is the default.
  2984. \"::* Archived Tasks\"
  2985. Archive in the current file, under the top-level headline
  2986. \"* Archived Tasks\".
  2987. \"~/org/archive.org::\"
  2988. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2989. \"~/org/archive.org::From %s\"
  2990. Archive in file ~/org/archive.org (absolute path), und headlines
  2991. \"From FILENAME\" where file name is the current file name.
  2992. \"basement::** Finished Tasks\"
  2993. Archive in file ./basement (relative path), as level 3 trees
  2994. below the level 2 heading \"** Finished Tasks\".
  2995. You may set this option on a per-file basis by adding to the buffer a
  2996. line like
  2997. #+ARCHIVE: basement::** Finished Tasks
  2998. You may also define it locally for a subtree by setting an ARCHIVE property
  2999. in the entry. If such a property is found in an entry, or anywhere up
  3000. the hierarchy, it will be used."
  3001. :group 'org-archive
  3002. :type 'string)
  3003. (defcustom org-archive-tag "ARCHIVE"
  3004. "The tag that marks a subtree as archived.
  3005. An archived subtree does not open during visibility cycling, and does
  3006. not contribute to the agenda listings.
  3007. After changing this, font-lock must be restarted in the relevant buffers to
  3008. get the proper fontification."
  3009. :group 'org-archive
  3010. :group 'org-keywords
  3011. :type 'string)
  3012. (defcustom org-agenda-skip-archived-trees t
  3013. "Non-nil means, the agenda will skip any items located in archived trees.
  3014. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3015. variable is no longer recommended, you should leave it at the value t.
  3016. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3017. :group 'org-archive
  3018. :group 'org-agenda-skip
  3019. :type 'boolean)
  3020. (defcustom org-columns-skip-archived-trees t
  3021. "Non-nil means, ignore archived trees when creating column view."
  3022. :group 'org-archive
  3023. :group 'org-properties
  3024. :type 'boolean)
  3025. (defcustom org-cycle-open-archived-trees nil
  3026. "Non-nil means, `org-cycle' will open archived trees.
  3027. An archived tree is a tree marked with the tag ARCHIVE.
  3028. When nil, archived trees will stay folded. You can still open them with
  3029. normal outline commands like `show-all', but not with the cycling commands."
  3030. :group 'org-archive
  3031. :group 'org-cycle
  3032. :type 'boolean)
  3033. (defcustom org-sparse-tree-open-archived-trees nil
  3034. "Non-nil means sparse tree construction shows matches in archived trees.
  3035. When nil, matches in these trees are highlighted, but the trees are kept in
  3036. collapsed state."
  3037. :group 'org-archive
  3038. :group 'org-sparse-trees
  3039. :type 'boolean)
  3040. (defun org-cycle-hide-archived-subtrees (state)
  3041. "Re-hide all archived subtrees after a visibility state change."
  3042. (when (and (not org-cycle-open-archived-trees)
  3043. (not (memq state '(overview folded))))
  3044. (save-excursion
  3045. (let* ((globalp (memq state '(contents all)))
  3046. (beg (if globalp (point-min) (point)))
  3047. (end (if globalp (point-max) (org-end-of-subtree t))))
  3048. (org-hide-archived-subtrees beg end)
  3049. (goto-char beg)
  3050. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3051. (message "%s" (substitute-command-keys
  3052. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3053. (defun org-force-cycle-archived ()
  3054. "Cycle subtree even if it is archived."
  3055. (interactive)
  3056. (setq this-command 'org-cycle)
  3057. (let ((org-cycle-open-archived-trees t))
  3058. (call-interactively 'org-cycle)))
  3059. (defun org-hide-archived-subtrees (beg end)
  3060. "Re-hide all archived subtrees after a visibility state change."
  3061. (save-excursion
  3062. (let* ((re (concat ":" org-archive-tag ":")))
  3063. (goto-char beg)
  3064. (while (re-search-forward re end t)
  3065. (and (org-on-heading-p) (org-flag-subtree t))
  3066. (org-end-of-subtree t)))))
  3067. (defun org-flag-subtree (flag)
  3068. (save-excursion
  3069. (org-back-to-heading t)
  3070. (outline-end-of-heading)
  3071. (outline-flag-region (point)
  3072. (progn (org-end-of-subtree t) (point))
  3073. flag)))
  3074. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3075. (eval-and-compile
  3076. (org-autoload "org-archive"
  3077. '(org-add-archive-files org-archive-subtree
  3078. org-archive-to-archive-sibling org-toggle-archive-tag)))
  3079. ;; Autoload Column View Code
  3080. (declare-function org-columns-number-to-string "org-colview")
  3081. (declare-function org-columns-get-format-and-top-level "org-colview")
  3082. (declare-function org-columns-compute "org-colview")
  3083. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3084. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3085. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3086. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3087. ;; Autoload ID code
  3088. (declare-function org-id-store-link "org-id")
  3089. (declare-function org-id-locations-load "org-id")
  3090. (declare-function org-id-locations-save "org-id")
  3091. (defvar org-id-track-globally)
  3092. (org-autoload "org-id"
  3093. '(org-id-get-create org-id-new org-id-copy org-id-get
  3094. org-id-get-with-outline-path-completion
  3095. org-id-get-with-outline-drilling
  3096. org-id-goto org-id-find org-id-store-link))
  3097. ;; Autoload Plotting Code
  3098. (org-autoload "org-plot"
  3099. '(org-plot/gnuplot))
  3100. ;;; Variables for pre-computed regular expressions, all buffer local
  3101. (defvar org-drawer-regexp nil
  3102. "Matches first line of a hidden block.")
  3103. (make-variable-buffer-local 'org-drawer-regexp)
  3104. (defvar org-todo-regexp nil
  3105. "Matches any of the TODO state keywords.")
  3106. (make-variable-buffer-local 'org-todo-regexp)
  3107. (defvar org-not-done-regexp nil
  3108. "Matches any of the TODO state keywords except the last one.")
  3109. (make-variable-buffer-local 'org-not-done-regexp)
  3110. (defvar org-not-done-heading-regexp nil
  3111. "Matches a TODO headline that is not done.")
  3112. (make-variable-buffer-local 'org-not-done-regexp)
  3113. (defvar org-todo-line-regexp nil
  3114. "Matches a headline and puts TODO state into group 2 if present.")
  3115. (make-variable-buffer-local 'org-todo-line-regexp)
  3116. (defvar org-complex-heading-regexp nil
  3117. "Matches a headline and puts everything into groups:
  3118. group 1: the stars
  3119. group 2: The todo keyword, maybe
  3120. group 3: Priority cookie
  3121. group 4: True headline
  3122. group 5: Tags")
  3123. (make-variable-buffer-local 'org-complex-heading-regexp)
  3124. (defvar org-complex-heading-regexp-format nil)
  3125. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3126. (defvar org-todo-line-tags-regexp nil
  3127. "Matches a headline and puts TODO state into group 2 if present.
  3128. Also put tags into group 4 if tags are present.")
  3129. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3130. (defvar org-nl-done-regexp nil
  3131. "Matches newline followed by a headline with the DONE keyword.")
  3132. (make-variable-buffer-local 'org-nl-done-regexp)
  3133. (defvar org-looking-at-done-regexp nil
  3134. "Matches the DONE keyword a point.")
  3135. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3136. (defvar org-ds-keyword-length 12
  3137. "Maximum length of the Deadline and SCHEDULED keywords.")
  3138. (make-variable-buffer-local 'org-ds-keyword-length)
  3139. (defvar org-deadline-regexp nil
  3140. "Matches the DEADLINE keyword.")
  3141. (make-variable-buffer-local 'org-deadline-regexp)
  3142. (defvar org-deadline-time-regexp nil
  3143. "Matches the DEADLINE keyword together with a time stamp.")
  3144. (make-variable-buffer-local 'org-deadline-time-regexp)
  3145. (defvar org-deadline-line-regexp nil
  3146. "Matches the DEADLINE keyword and the rest of the line.")
  3147. (make-variable-buffer-local 'org-deadline-line-regexp)
  3148. (defvar org-scheduled-regexp nil
  3149. "Matches the SCHEDULED keyword.")
  3150. (make-variable-buffer-local 'org-scheduled-regexp)
  3151. (defvar org-scheduled-time-regexp nil
  3152. "Matches the SCHEDULED keyword together with a time stamp.")
  3153. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3154. (defvar org-closed-time-regexp nil
  3155. "Matches the CLOSED keyword together with a time stamp.")
  3156. (make-variable-buffer-local 'org-closed-time-regexp)
  3157. (defvar org-keyword-time-regexp nil
  3158. "Matches any of the 4 keywords, together with the time stamp.")
  3159. (make-variable-buffer-local 'org-keyword-time-regexp)
  3160. (defvar org-keyword-time-not-clock-regexp nil
  3161. "Matches any of the 3 keywords, together with the time stamp.")
  3162. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3163. (defvar org-maybe-keyword-time-regexp nil
  3164. "Matches a timestamp, possibly preceeded by a keyword.")
  3165. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3166. (defvar org-planning-or-clock-line-re nil
  3167. "Matches a line with planning or clock info.")
  3168. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3169. (defconst org-plain-time-of-day-regexp
  3170. (concat
  3171. "\\(\\<[012]?[0-9]"
  3172. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3173. "\\(--?"
  3174. "\\(\\<[012]?[0-9]"
  3175. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3176. "\\)?")
  3177. "Regular expression to match a plain time or time range.
  3178. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3179. groups carry important information:
  3180. 0 the full match
  3181. 1 the first time, range or not
  3182. 8 the second time, if it is a range.")
  3183. (defconst org-plain-time-extension-regexp
  3184. (concat
  3185. "\\(\\<[012]?[0-9]"
  3186. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3187. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3188. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3189. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3190. groups carry important information:
  3191. 0 the full match
  3192. 7 hours of duration
  3193. 9 minutes of duration")
  3194. (defconst org-stamp-time-of-day-regexp
  3195. (concat
  3196. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3197. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3198. "\\(--?"
  3199. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3200. "Regular expression to match a timestamp time or time range.
  3201. After a match, the following groups carry important information:
  3202. 0 the full match
  3203. 1 date plus weekday, for backreferencing to make sure both times on same day
  3204. 2 the first time, range or not
  3205. 4 the second time, if it is a range.")
  3206. (defconst org-startup-options
  3207. '(("fold" org-startup-folded t)
  3208. ("overview" org-startup-folded t)
  3209. ("nofold" org-startup-folded nil)
  3210. ("showall" org-startup-folded nil)
  3211. ("showeverything" org-startup-folded showeverything)
  3212. ("content" org-startup-folded content)
  3213. ("indent" org-startup-indented t)
  3214. ("noindent" org-startup-indented nil)
  3215. ("hidestars" org-hide-leading-stars t)
  3216. ("showstars" org-hide-leading-stars nil)
  3217. ("odd" org-odd-levels-only t)
  3218. ("oddeven" org-odd-levels-only nil)
  3219. ("align" org-startup-align-all-tables t)
  3220. ("noalign" org-startup-align-all-tables nil)
  3221. ("customtime" org-display-custom-times t)
  3222. ("logdone" org-log-done time)
  3223. ("lognotedone" org-log-done note)
  3224. ("nologdone" org-log-done nil)
  3225. ("lognoteclock-out" org-log-note-clock-out t)
  3226. ("nolognoteclock-out" org-log-note-clock-out nil)
  3227. ("logrepeat" org-log-repeat state)
  3228. ("lognoterepeat" org-log-repeat note)
  3229. ("nologrepeat" org-log-repeat nil)
  3230. ("logreschedule" org-log-reschedule time)
  3231. ("lognotereschedule" org-log-reschedule note)
  3232. ("nologreschedule" org-log-reschedule nil)
  3233. ("logredeadline" org-log-redeadline time)
  3234. ("lognoteredeadline" org-log-redeadline note)
  3235. ("nologredeadline" org-log-redeadline nil)
  3236. ("fninline" org-footnote-define-inline t)
  3237. ("nofninline" org-footnote-define-inline nil)
  3238. ("fnlocal" org-footnote-section nil)
  3239. ("fnauto" org-footnote-auto-label t)
  3240. ("fnprompt" org-footnote-auto-label nil)
  3241. ("fnconfirm" org-footnote-auto-label confirm)
  3242. ("fnplain" org-footnote-auto-label plain)
  3243. ("fnadjust" org-footnote-auto-adjust t)
  3244. ("nofnadjust" org-footnote-auto-adjust nil)
  3245. ("constcgs" constants-unit-system cgs)
  3246. ("constSI" constants-unit-system SI)
  3247. ("noptag" org-tag-persistent-alist nil)
  3248. ("hideblocks" org-hide-block-startup t)
  3249. ("nohideblocks" org-hide-block-startup nil))
  3250. "Variable associated with STARTUP options for org-mode.
  3251. Each element is a list of three items: The startup options as written
  3252. in the #+STARTUP line, the corresponding variable, and the value to
  3253. set this variable to if the option is found. An optional forth element PUSH
  3254. means to push this value onto the list in the variable.")
  3255. (defun org-set-regexps-and-options ()
  3256. "Precompute regular expressions for current buffer."
  3257. (when (org-mode-p)
  3258. (org-set-local 'org-todo-kwd-alist nil)
  3259. (org-set-local 'org-todo-key-alist nil)
  3260. (org-set-local 'org-todo-key-trigger nil)
  3261. (org-set-local 'org-todo-keywords-1 nil)
  3262. (org-set-local 'org-done-keywords nil)
  3263. (org-set-local 'org-todo-heads nil)
  3264. (org-set-local 'org-todo-sets nil)
  3265. (org-set-local 'org-todo-log-states nil)
  3266. (org-set-local 'org-file-properties nil)
  3267. (org-set-local 'org-file-tags nil)
  3268. (let ((re (org-make-options-regexp
  3269. '("CATEGORY" "TODO" "COLUMNS"
  3270. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3271. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  3272. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3273. (splitre "[ \t]+")
  3274. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3275. tail sep kws1 prio props ftags drawers
  3276. ext-setup-or-nil setup-contents (start 0))
  3277. (save-excursion
  3278. (save-restriction
  3279. (widen)
  3280. (goto-char (point-min))
  3281. (while (or (and ext-setup-or-nil
  3282. (string-match re ext-setup-or-nil start)
  3283. (setq start (match-end 0)))
  3284. (and (setq ext-setup-or-nil nil start 0)
  3285. (re-search-forward re nil t)))
  3286. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3287. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3288. (cond
  3289. ((equal key "CATEGORY")
  3290. (if (string-match "[ \t]+$" value)
  3291. (setq value (replace-match "" t t value)))
  3292. (setq cat value))
  3293. ((member key '("SEQ_TODO" "TODO"))
  3294. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3295. ((equal key "TYP_TODO")
  3296. (push (cons 'type (org-split-string value splitre)) kwds))
  3297. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3298. ;; general TODO-like setup
  3299. (push (cons (intern (downcase (match-string 1 key)))
  3300. (org-split-string value splitre)) kwds))
  3301. ((equal key "TAGS")
  3302. (setq tags (append tags (if tags '("\\n") nil)
  3303. (org-split-string value splitre))))
  3304. ((equal key "COLUMNS")
  3305. (org-set-local 'org-columns-default-format value))
  3306. ((equal key "LINK")
  3307. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3308. (push (cons (match-string 1 value)
  3309. (org-trim (match-string 2 value)))
  3310. links)))
  3311. ((equal key "PRIORITIES")
  3312. (setq prio (org-split-string value " +")))
  3313. ((equal key "PROPERTY")
  3314. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3315. (push (cons (match-string 1 value) (match-string 2 value))
  3316. props)))
  3317. ((equal key "FILETAGS")
  3318. (when (string-match "\\S-" value)
  3319. (setq ftags
  3320. (append
  3321. ftags
  3322. (apply 'append
  3323. (mapcar (lambda (x) (org-split-string x ":"))
  3324. (org-split-string value)))))))
  3325. ((equal key "DRAWERS")
  3326. (setq drawers (org-split-string value splitre)))
  3327. ((equal key "CONSTANTS")
  3328. (setq const (append const (org-split-string value splitre))))
  3329. ((equal key "STARTUP")
  3330. (let ((opts (org-split-string value splitre))
  3331. l var val)
  3332. (while (setq l (pop opts))
  3333. (when (setq l (assoc l org-startup-options))
  3334. (setq var (nth 1 l) val (nth 2 l))
  3335. (if (not (nth 3 l))
  3336. (set (make-local-variable var) val)
  3337. (if (not (listp (symbol-value var)))
  3338. (set (make-local-variable var) nil))
  3339. (set (make-local-variable var) (symbol-value var))
  3340. (add-to-list var val))))))
  3341. ((equal key "ARCHIVE")
  3342. (string-match " *$" value)
  3343. (setq arch (replace-match "" t t value))
  3344. (remove-text-properties 0 (length arch)
  3345. '(face t fontified t) arch))
  3346. ((equal key "SETUPFILE")
  3347. (setq setup-contents (org-file-contents
  3348. (expand-file-name
  3349. (org-remove-double-quotes value))
  3350. 'noerror))
  3351. (if (not ext-setup-or-nil)
  3352. (setq ext-setup-or-nil setup-contents start 0)
  3353. (setq ext-setup-or-nil
  3354. (concat (substring ext-setup-or-nil 0 start)
  3355. "\n" setup-contents "\n"
  3356. (substring ext-setup-or-nil start)))))
  3357. ))))
  3358. (when cat
  3359. (org-set-local 'org-category (intern cat))
  3360. (push (cons "CATEGORY" cat) props))
  3361. (when prio
  3362. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3363. (setq prio (mapcar 'string-to-char prio))
  3364. (org-set-local 'org-highest-priority (nth 0 prio))
  3365. (org-set-local 'org-lowest-priority (nth 1 prio))
  3366. (org-set-local 'org-default-priority (nth 2 prio)))
  3367. (and props (org-set-local 'org-file-properties (nreverse props)))
  3368. (and ftags (org-set-local 'org-file-tags
  3369. (mapcar 'org-add-prop-inherited ftags)))
  3370. (and drawers (org-set-local 'org-drawers drawers))
  3371. (and arch (org-set-local 'org-archive-location arch))
  3372. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3373. ;; Process the TODO keywords
  3374. (unless kwds
  3375. ;; Use the global values as if they had been given locally.
  3376. (setq kwds (default-value 'org-todo-keywords))
  3377. (if (stringp (car kwds))
  3378. (setq kwds (list (cons org-todo-interpretation
  3379. (default-value 'org-todo-keywords)))))
  3380. (setq kwds (reverse kwds)))
  3381. (setq kwds (nreverse kwds))
  3382. (let (inter kws kw)
  3383. (while (setq kws (pop kwds))
  3384. (let ((kws (or
  3385. (run-hook-with-args-until-success
  3386. 'org-todo-setup-filter-hook kws)
  3387. kws)))
  3388. (setq inter (pop kws) sep (member "|" kws)
  3389. kws0 (delete "|" (copy-sequence kws))
  3390. kwsa nil
  3391. kws1 (mapcar
  3392. (lambda (x)
  3393. ;; 1 2
  3394. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3395. (progn
  3396. (setq kw (match-string 1 x)
  3397. key (and (match-end 2) (match-string 2 x))
  3398. log (org-extract-log-state-settings x))
  3399. (push (cons kw (and key (string-to-char key))) kwsa)
  3400. (and log (push log org-todo-log-states))
  3401. kw)
  3402. (error "Invalid TODO keyword %s" x)))
  3403. kws0)
  3404. kwsa (if kwsa (append '((:startgroup))
  3405. (nreverse kwsa)
  3406. '((:endgroup))))
  3407. hw (car kws1)
  3408. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3409. tail (list inter hw (car dws) (org-last dws))))
  3410. (add-to-list 'org-todo-heads hw 'append)
  3411. (push kws1 org-todo-sets)
  3412. (setq org-done-keywords (append org-done-keywords dws nil))
  3413. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3414. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3415. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3416. (setq org-todo-sets (nreverse org-todo-sets)
  3417. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3418. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3419. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3420. ;; Process the constants
  3421. (when const
  3422. (let (e cst)
  3423. (while (setq e (pop const))
  3424. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3425. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3426. (setq org-table-formula-constants-local cst)))
  3427. ;; Process the tags.
  3428. (when tags
  3429. (let (e tgs)
  3430. (while (setq e (pop tags))
  3431. (cond
  3432. ((equal e "{") (push '(:startgroup) tgs))
  3433. ((equal e "}") (push '(:endgroup) tgs))
  3434. ((equal e "\\n") (push '(:newline) tgs))
  3435. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3436. (push (cons (match-string 1 e)
  3437. (string-to-char (match-string 2 e)))
  3438. tgs))
  3439. (t (push (list e) tgs))))
  3440. (org-set-local 'org-tag-alist nil)
  3441. (while (setq e (pop tgs))
  3442. (or (and (stringp (car e))
  3443. (assoc (car e) org-tag-alist))
  3444. (push e org-tag-alist)))))
  3445. ;; Compute the regular expressions and other local variables
  3446. (if (not org-done-keywords)
  3447. (setq org-done-keywords (and org-todo-keywords-1
  3448. (list (org-last org-todo-keywords-1)))))
  3449. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3450. (length org-scheduled-string)
  3451. (length org-clock-string)
  3452. (length org-closed-string)))
  3453. org-drawer-regexp
  3454. (concat "^[ \t]*:\\("
  3455. (mapconcat 'regexp-quote org-drawers "\\|")
  3456. "\\):[ \t]*$")
  3457. org-not-done-keywords
  3458. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3459. org-todo-regexp
  3460. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3461. "\\|") "\\)\\>")
  3462. org-not-done-regexp
  3463. (concat "\\<\\("
  3464. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3465. "\\)\\>")
  3466. org-not-done-heading-regexp
  3467. (concat "^\\(\\*+\\)[ \t]+\\("
  3468. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3469. "\\)\\>")
  3470. org-todo-line-regexp
  3471. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3472. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3473. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3474. org-complex-heading-regexp
  3475. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3476. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3477. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3478. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3479. org-complex-heading-regexp-format
  3480. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3481. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3482. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
  3483. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3484. org-nl-done-regexp
  3485. (concat "\n\\*+[ \t]+"
  3486. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3487. "\\)" "\\>")
  3488. org-todo-line-tags-regexp
  3489. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3490. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3491. (org-re
  3492. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3493. org-looking-at-done-regexp
  3494. (concat "^" "\\(?:"
  3495. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3496. "\\>")
  3497. org-deadline-regexp (concat "\\<" org-deadline-string)
  3498. org-deadline-time-regexp
  3499. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3500. org-deadline-line-regexp
  3501. (concat "\\<\\(" org-deadline-string "\\).*")
  3502. org-scheduled-regexp
  3503. (concat "\\<" org-scheduled-string)
  3504. org-scheduled-time-regexp
  3505. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3506. org-closed-time-regexp
  3507. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3508. org-keyword-time-regexp
  3509. (concat "\\<\\(" org-scheduled-string
  3510. "\\|" org-deadline-string
  3511. "\\|" org-closed-string
  3512. "\\|" org-clock-string "\\)"
  3513. " *[[<]\\([^]>]+\\)[]>]")
  3514. org-keyword-time-not-clock-regexp
  3515. (concat "\\<\\(" org-scheduled-string
  3516. "\\|" org-deadline-string
  3517. "\\|" org-closed-string
  3518. "\\)"
  3519. " *[[<]\\([^]>]+\\)[]>]")
  3520. org-maybe-keyword-time-regexp
  3521. (concat "\\(\\<\\(" org-scheduled-string
  3522. "\\|" org-deadline-string
  3523. "\\|" org-closed-string
  3524. "\\|" org-clock-string "\\)\\)?"
  3525. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3526. org-planning-or-clock-line-re
  3527. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3528. "\\|" org-deadline-string
  3529. "\\|" org-closed-string "\\|" org-clock-string
  3530. "\\)\\>\\)")
  3531. )
  3532. (org-compute-latex-and-specials-regexp)
  3533. (org-set-font-lock-defaults))))
  3534. (defun org-file-contents (file &optional noerror)
  3535. "Return the contents of FILE, as a string."
  3536. (if (or (not file)
  3537. (not (file-readable-p file)))
  3538. (if noerror
  3539. (progn
  3540. (message "Cannot read file %s" file)
  3541. (ding) (sit-for 2)
  3542. "")
  3543. (error "Cannot read file %s" file))
  3544. (with-temp-buffer
  3545. (insert-file-contents file)
  3546. (buffer-string))))
  3547. (defun org-extract-log-state-settings (x)
  3548. "Extract the log state setting from a TODO keyword string.
  3549. This will extract info from a string like \"WAIT(w@/!)\"."
  3550. (let (kw key log1 log2)
  3551. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3552. (setq kw (match-string 1 x)
  3553. key (and (match-end 2) (match-string 2 x))
  3554. log1 (and (match-end 3) (match-string 3 x))
  3555. log2 (and (match-end 4) (match-string 4 x)))
  3556. (and (or log1 log2)
  3557. (list kw
  3558. (and log1 (if (equal log1 "!") 'time 'note))
  3559. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3560. (defun org-remove-keyword-keys (list)
  3561. "Remove a pair of parenthesis at the end of each string in LIST."
  3562. (mapcar (lambda (x)
  3563. (if (string-match "(.*)$" x)
  3564. (substring x 0 (match-beginning 0))
  3565. x))
  3566. list))
  3567. ;; FIXME: this could be done much better, using second characters etc.
  3568. (defun org-assign-fast-keys (alist)
  3569. "Assign fast keys to a keyword-key alist.
  3570. Respect keys that are already there."
  3571. (let (new e k c c1 c2 (char ?a))
  3572. (while (setq e (pop alist))
  3573. (cond
  3574. ((equal e '(:startgroup)) (push e new))
  3575. ((equal e '(:endgroup)) (push e new))
  3576. ((equal e '(:newline)) (push e new))
  3577. (t
  3578. (setq k (car e) c2 nil)
  3579. (if (cdr e)
  3580. (setq c (cdr e))
  3581. ;; automatically assign a character.
  3582. (setq c1 (string-to-char
  3583. (downcase (substring
  3584. k (if (= (string-to-char k) ?@) 1 0)))))
  3585. (if (or (rassoc c1 new) (rassoc c1 alist))
  3586. (while (or (rassoc char new) (rassoc char alist))
  3587. (setq char (1+ char)))
  3588. (setq c2 c1))
  3589. (setq c (or c2 char)))
  3590. (push (cons k c) new))))
  3591. (nreverse new)))
  3592. ;;; Some variables used in various places
  3593. (defvar org-window-configuration nil
  3594. "Used in various places to store a window configuration.")
  3595. (defvar org-selected-window nil
  3596. "Used in various places to store a window configuration.")
  3597. (defvar org-finish-function nil
  3598. "Function to be called when `C-c C-c' is used.
  3599. This is for getting out of special buffers like remember.")
  3600. ;; FIXME: Occasionally check by commenting these, to make sure
  3601. ;; no other functions uses these, forgetting to let-bind them.
  3602. (defvar entry)
  3603. (defvar last-state)
  3604. (defvar date)
  3605. ;; Defined somewhere in this file, but used before definition.
  3606. (defvar org-html-entities)
  3607. (defvar org-struct-menu)
  3608. (defvar org-org-menu)
  3609. (defvar org-tbl-menu)
  3610. ;;;; Define the Org-mode
  3611. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3612. (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."))
  3613. ;; We use a before-change function to check if a table might need
  3614. ;; an update.
  3615. (defvar org-table-may-need-update t
  3616. "Indicates that a table might need an update.
  3617. This variable is set by `org-before-change-function'.
  3618. `org-table-align' sets it back to nil.")
  3619. (defun org-before-change-function (beg end)
  3620. "Every change indicates that a table might need an update."
  3621. (setq org-table-may-need-update t))
  3622. (defvar org-mode-map)
  3623. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3624. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3625. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3626. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3627. (defvar org-table-buffer-is-an nil)
  3628. (defconst org-outline-regexp "\\*+ ")
  3629. ;;;###autoload
  3630. (define-derived-mode org-mode outline-mode "Org"
  3631. "Outline-based notes management and organizer, alias
  3632. \"Carsten's outline-mode for keeping track of everything.\"
  3633. Org-mode develops organizational tasks around a NOTES file which
  3634. contains information about projects as plain text. Org-mode is
  3635. implemented on top of outline-mode, which is ideal to keep the content
  3636. of large files well structured. It supports ToDo items, deadlines and
  3637. time stamps, which magically appear in the diary listing of the Emacs
  3638. calendar. Tables are easily created with a built-in table editor.
  3639. Plain text URL-like links connect to websites, emails (VM), Usenet
  3640. messages (Gnus), BBDB entries, and any files related to the project.
  3641. For printing and sharing of notes, an Org-mode file (or a part of it)
  3642. can be exported as a structured ASCII or HTML file.
  3643. The following commands are available:
  3644. \\{org-mode-map}"
  3645. ;; Get rid of Outline menus, they are not needed
  3646. ;; Need to do this here because define-derived-mode sets up
  3647. ;; the keymap so late. Still, it is a waste to call this each time
  3648. ;; we switch another buffer into org-mode.
  3649. (if (featurep 'xemacs)
  3650. (when (boundp 'outline-mode-menu-heading)
  3651. ;; Assume this is Greg's port, it used easymenu
  3652. (easy-menu-remove outline-mode-menu-heading)
  3653. (easy-menu-remove outline-mode-menu-show)
  3654. (easy-menu-remove outline-mode-menu-hide))
  3655. (define-key org-mode-map [menu-bar headings] 'undefined)
  3656. (define-key org-mode-map [menu-bar hide] 'undefined)
  3657. (define-key org-mode-map [menu-bar show] 'undefined))
  3658. (org-load-modules-maybe)
  3659. (easy-menu-add org-org-menu)
  3660. (easy-menu-add org-tbl-menu)
  3661. (org-install-agenda-files-menu)
  3662. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3663. (org-add-to-invisibility-spec '(org-cwidth))
  3664. (org-add-to-invisibility-spec '(org-hide-block . t))
  3665. (when (featurep 'xemacs)
  3666. (org-set-local 'line-move-ignore-invisible t))
  3667. (org-set-local 'outline-regexp org-outline-regexp)
  3668. (org-set-local 'outline-level 'org-outline-level)
  3669. (when (and org-ellipsis
  3670. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3671. (fboundp 'make-glyph-code))
  3672. (unless org-display-table
  3673. (setq org-display-table (make-display-table)))
  3674. (set-display-table-slot
  3675. org-display-table 4
  3676. (vconcat (mapcar
  3677. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3678. org-ellipsis)))
  3679. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3680. (setq buffer-display-table org-display-table))
  3681. (org-set-regexps-and-options)
  3682. (when (and org-tag-faces (not org-tags-special-faces-re))
  3683. ;; tag faces set outside customize.... force initialization.
  3684. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3685. ;; Calc embedded
  3686. (org-set-local 'calc-embedded-open-mode "# ")
  3687. (modify-syntax-entry ?# "<")
  3688. (modify-syntax-entry ?@ "w")
  3689. (if org-startup-truncated (setq truncate-lines t))
  3690. (org-set-local 'font-lock-unfontify-region-function
  3691. 'org-unfontify-region)
  3692. ;; Activate before-change-function
  3693. (org-set-local 'org-table-may-need-update t)
  3694. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3695. 'local)
  3696. ;; Check for running clock before killing a buffer
  3697. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3698. ;; Paragraphs and auto-filling
  3699. (org-set-autofill-regexps)
  3700. (setq indent-line-function 'org-indent-line-function)
  3701. (org-update-radio-target-regexp)
  3702. ;; Make sure dependence stuff works reliably, even for users who set it
  3703. ;; too late :-(
  3704. (if org-enforce-todo-dependencies
  3705. (add-hook 'org-blocker-hook
  3706. 'org-block-todo-from-children-or-siblings-or-parent)
  3707. (remove-hook 'org-blocker-hook
  3708. 'org-block-todo-from-children-or-siblings-or-parent))
  3709. (if org-enforce-todo-checkbox-dependencies
  3710. (add-hook 'org-blocker-hook
  3711. 'org-block-todo-from-checkboxes)
  3712. (remove-hook 'org-blocker-hook
  3713. 'org-block-todo-from-checkboxes))
  3714. ;; Comment characters
  3715. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3716. (org-set-local 'comment-padding " ")
  3717. ;; Align options lines
  3718. (org-set-local
  3719. 'align-mode-rules-list
  3720. '((org-in-buffer-settings
  3721. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3722. (modes . '(org-mode)))))
  3723. ;; Imenu
  3724. (org-set-local 'imenu-create-index-function
  3725. 'org-imenu-get-tree)
  3726. ;; Make isearch reveal context
  3727. (if (or (featurep 'xemacs)
  3728. (not (boundp 'outline-isearch-open-invisible-function)))
  3729. ;; Emacs 21 and XEmacs make use of the hook
  3730. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3731. ;; Emacs 22 deals with this through a special variable
  3732. (org-set-local 'outline-isearch-open-invisible-function
  3733. (lambda (&rest ignore) (org-show-context 'isearch))))
  3734. ;; If empty file that did not turn on org-mode automatically, make it to.
  3735. (if (and org-insert-mode-line-in-empty-file
  3736. (interactive-p)
  3737. (= (point-min) (point-max)))
  3738. (insert "# -*- mode: org -*-\n\n"))
  3739. (unless org-inhibit-startup
  3740. (when org-startup-align-all-tables
  3741. (let ((bmp (buffer-modified-p)))
  3742. (org-table-map-tables 'org-table-align)
  3743. (set-buffer-modified-p bmp)))
  3744. (when org-startup-indented
  3745. (require 'org-indent)
  3746. (org-indent-mode 1))
  3747. (org-set-startup-visibility)))
  3748. (when (fboundp 'abbrev-table-put)
  3749. (abbrev-table-put org-mode-abbrev-table
  3750. :parents (list text-mode-abbrev-table)))
  3751. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3752. (defun org-current-time ()
  3753. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3754. (if (> (car org-time-stamp-rounding-minutes) 1)
  3755. (let ((r (car org-time-stamp-rounding-minutes))
  3756. (time (decode-time)))
  3757. (apply 'encode-time
  3758. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3759. (nthcdr 2 time))))
  3760. (current-time)))
  3761. ;;;; Font-Lock stuff, including the activators
  3762. (defvar org-mouse-map (make-sparse-keymap))
  3763. (org-defkey org-mouse-map
  3764. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3765. (org-defkey org-mouse-map
  3766. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3767. (when org-mouse-1-follows-link
  3768. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3769. (when org-tab-follows-link
  3770. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3771. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3772. (require 'font-lock)
  3773. (defconst org-non-link-chars "]\t\n\r<>")
  3774. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3775. "shell" "elisp"))
  3776. (defvar org-link-types-re nil
  3777. "Matches a link that has a url-like prefix like \"http:\"")
  3778. (defvar org-link-re-with-space nil
  3779. "Matches a link with spaces, optional angular brackets around it.")
  3780. (defvar org-link-re-with-space2 nil
  3781. "Matches a link with spaces, optional angular brackets around it.")
  3782. (defvar org-link-re-with-space3 nil
  3783. "Matches a link with spaces, only for internal part in bracket links.")
  3784. (defvar org-angle-link-re nil
  3785. "Matches link with angular brackets, spaces are allowed.")
  3786. (defvar org-plain-link-re nil
  3787. "Matches plain link, without spaces.")
  3788. (defvar org-bracket-link-regexp nil
  3789. "Matches a link in double brackets.")
  3790. (defvar org-bracket-link-analytic-regexp nil
  3791. "Regular expression used to analyze links.
  3792. Here is what the match groups contain after a match:
  3793. 1: http:
  3794. 2: http
  3795. 3: path
  3796. 4: [desc]
  3797. 5: desc")
  3798. (defvar org-bracket-link-analytic-regexp++ nil
  3799. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3800. (defvar org-any-link-re nil
  3801. "Regular expression matching any link.")
  3802. (defun org-make-link-regexps ()
  3803. "Update the link regular expressions.
  3804. This should be called after the variable `org-link-types' has changed."
  3805. (setq org-link-types-re
  3806. (concat
  3807. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3808. org-link-re-with-space
  3809. (concat
  3810. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3811. "\\([^" org-non-link-chars " ]"
  3812. "[^" org-non-link-chars "]*"
  3813. "[^" org-non-link-chars " ]\\)>?")
  3814. org-link-re-with-space2
  3815. (concat
  3816. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3817. "\\([^" org-non-link-chars " ]"
  3818. "[^\t\n\r]*"
  3819. "[^" org-non-link-chars " ]\\)>?")
  3820. org-link-re-with-space3
  3821. (concat
  3822. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3823. "\\([^" org-non-link-chars " ]"
  3824. "[^\t\n\r]*\\)")
  3825. org-angle-link-re
  3826. (concat
  3827. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3828. "\\([^" org-non-link-chars " ]"
  3829. "[^" org-non-link-chars "]*"
  3830. "\\)>")
  3831. org-plain-link-re
  3832. (concat
  3833. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3834. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3835. org-bracket-link-regexp
  3836. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3837. org-bracket-link-analytic-regexp
  3838. (concat
  3839. "\\[\\["
  3840. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3841. "\\([^]]+\\)"
  3842. "\\]"
  3843. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3844. "\\]")
  3845. org-bracket-link-analytic-regexp++
  3846. (concat
  3847. "\\[\\["
  3848. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3849. "\\([^]]+\\)"
  3850. "\\]"
  3851. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3852. "\\]")
  3853. org-any-link-re
  3854. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3855. org-angle-link-re "\\)\\|\\("
  3856. org-plain-link-re "\\)")))
  3857. (org-make-link-regexps)
  3858. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3859. "Regular expression for fast time stamp matching.")
  3860. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3861. "Regular expression for fast time stamp matching.")
  3862. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3863. "Regular expression matching time strings for analysis.
  3864. This one does not require the space after the date, so it can be used
  3865. on a string that terminates immediately after the date.")
  3866. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3867. "Regular expression matching time strings for analysis.")
  3868. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3869. "Regular expression matching time stamps, with groups.")
  3870. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3871. "Regular expression matching time stamps (also [..]), with groups.")
  3872. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3873. "Regular expression matching a time stamp range.")
  3874. (defconst org-tr-regexp-both
  3875. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3876. "Regular expression matching a time stamp range.")
  3877. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3878. org-ts-regexp "\\)?")
  3879. "Regular expression matching a time stamp or time stamp range.")
  3880. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3881. org-ts-regexp-both "\\)?")
  3882. "Regular expression matching a time stamp or time stamp range.
  3883. The time stamps may be either active or inactive.")
  3884. (defvar org-emph-face nil)
  3885. (defun org-do-emphasis-faces (limit)
  3886. "Run through the buffer and add overlays to links."
  3887. (let (rtn a)
  3888. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3889. (if (not (= (char-after (match-beginning 3))
  3890. (char-after (match-beginning 4))))
  3891. (progn
  3892. (setq rtn t)
  3893. (setq a (assoc (match-string 3) org-emphasis-alist))
  3894. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3895. 'face
  3896. (nth 1 a))
  3897. (and (nth 4 a)
  3898. (org-remove-flyspell-overlays-in
  3899. (match-beginning 0) (match-end 0)))
  3900. (add-text-properties (match-beginning 2) (match-end 2)
  3901. '(font-lock-multiline t))
  3902. (when org-hide-emphasis-markers
  3903. (add-text-properties (match-end 4) (match-beginning 5)
  3904. '(invisible org-link))
  3905. (add-text-properties (match-beginning 3) (match-end 3)
  3906. '(invisible org-link)))))
  3907. (backward-char 1))
  3908. rtn))
  3909. (defun org-emphasize (&optional char)
  3910. "Insert or change an emphasis, i.e. a font like bold or italic.
  3911. If there is an active region, change that region to a new emphasis.
  3912. If there is no region, just insert the marker characters and position
  3913. the cursor between them.
  3914. CHAR should be either the marker character, or the first character of the
  3915. HTML tag associated with that emphasis. If CHAR is a space, the means
  3916. to remove the emphasis of the selected region.
  3917. If char is not given (for example in an interactive call) it
  3918. will be prompted for."
  3919. (interactive)
  3920. (let ((eal org-emphasis-alist) e det
  3921. (erc org-emphasis-regexp-components)
  3922. (prompt "")
  3923. (string "") beg end move tag c s)
  3924. (if (org-region-active-p)
  3925. (setq beg (region-beginning) end (region-end)
  3926. string (buffer-substring beg end))
  3927. (setq move t))
  3928. (while (setq e (pop eal))
  3929. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3930. c (aref tag 0))
  3931. (push (cons c (string-to-char (car e))) det)
  3932. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3933. (substring tag 1)))))
  3934. (setq det (nreverse det))
  3935. (unless char
  3936. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3937. (setq char (read-char-exclusive)))
  3938. (setq char (or (cdr (assoc char det)) char))
  3939. (if (equal char ?\ )
  3940. (setq s "" move nil)
  3941. (unless (assoc (char-to-string char) org-emphasis-alist)
  3942. (error "No such emphasis marker: \"%c\"" char))
  3943. (setq s (char-to-string char)))
  3944. (while (and (> (length string) 1)
  3945. (equal (substring string 0 1) (substring string -1))
  3946. (assoc (substring string 0 1) org-emphasis-alist))
  3947. (setq string (substring string 1 -1)))
  3948. (setq string (concat s string s))
  3949. (if beg (delete-region beg end))
  3950. (unless (or (bolp)
  3951. (string-match (concat "[" (nth 0 erc) "\n]")
  3952. (char-to-string (char-before (point)))))
  3953. (insert " "))
  3954. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3955. (char-to-string (char-after (point))))
  3956. (insert " ") (backward-char 1))
  3957. (insert string)
  3958. (and move (backward-char 1))))
  3959. (defconst org-nonsticky-props
  3960. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3961. (defsubst org-rear-nonsticky-at (pos)
  3962. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3963. (defun org-activate-plain-links (limit)
  3964. "Run through the buffer and add overlays to links."
  3965. (catch 'exit
  3966. (let (f)
  3967. (if (re-search-forward org-plain-link-re limit t)
  3968. (progn
  3969. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3970. (setq f (get-text-property (match-beginning 0) 'face))
  3971. (if (or (eq f 'org-tag)
  3972. (and (listp f) (memq 'org-tag f)))
  3973. nil
  3974. (add-text-properties (match-beginning 0) (match-end 0)
  3975. (list 'mouse-face 'highlight
  3976. 'keymap org-mouse-map))
  3977. (org-rear-nonsticky-at (match-end 0)))
  3978. t)))))
  3979. (defun org-activate-code (limit)
  3980. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3981. (progn
  3982. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3983. (remove-text-properties (match-beginning 0) (match-end 0)
  3984. '(display t invisible t intangible t))
  3985. t)))
  3986. (defun org-fontify-meta-lines-and-blocks (limit)
  3987. "Fontify #+ lines and blocks, in the correct ways."
  3988. (let ((case-fold-search t))
  3989. (if (re-search-forward
  3990. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  3991. limit t)
  3992. (let ((beg (match-beginning 0))
  3993. (beg1 (line-beginning-position 2))
  3994. (dc1 (downcase (match-string 2)))
  3995. (dc3 (downcase (match-string 3)))
  3996. end end1 quoting)
  3997. (cond
  3998. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  3999. ;; a single line of backend-specific content
  4000. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4001. (remove-text-properties (match-beginning 0) (match-end 0)
  4002. '(display t invisible t intangible t))
  4003. (add-text-properties (match-beginning 1) (match-end 3)
  4004. '(font-lock-fontified t face org-meta-line))
  4005. (add-text-properties (match-beginning 6) (match-end 6)
  4006. '(font-lock-fontified t face org-block))
  4007. t)
  4008. ((and (match-end 4) (equal dc3 "begin"))
  4009. ;; Truely a block
  4010. (setq quoting (member (downcase (match-string 5))
  4011. org-protecting-blocks))
  4012. (when (re-search-forward
  4013. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4014. nil t) ;; on purpose, we look further than LIMIT
  4015. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4016. (when quoting
  4017. (remove-text-properties beg end
  4018. '(display t invisible t intangible t)))
  4019. (add-text-properties
  4020. beg end
  4021. '(font-lock-fontified t font-lock-multiline t))
  4022. (add-text-properties beg beg1 '(face org-meta-line))
  4023. (add-text-properties end1 end '(face org-meta-line))
  4024. (when quoting
  4025. (add-text-properties beg1 end1 '(face org-block)))
  4026. t))
  4027. ((not (member (char-after beg) '(?\ ?\t)))
  4028. ;; just any other in-buffer setting, but not indented
  4029. (add-text-properties
  4030. beg (match-end 0)
  4031. '(font-lock-fontified t face org-meta-line))
  4032. t)
  4033. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4034. "orgtbl:" "tblfm:" "tblname:"))
  4035. (and (match-end 4) (equal dc3 "attr")))
  4036. (add-text-properties
  4037. beg (match-end 0)
  4038. '(font-lock-fontified t face org-meta-line))
  4039. t)
  4040. ((member dc3 '(" " ""))
  4041. (add-text-properties
  4042. beg (match-end 0)
  4043. '(font-lock-fontified t face font-lock-comment-face)))
  4044. (t nil))))))
  4045. (defun org-activate-angle-links (limit)
  4046. "Run through the buffer and add overlays to links."
  4047. (if (re-search-forward org-angle-link-re limit t)
  4048. (progn
  4049. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4050. (add-text-properties (match-beginning 0) (match-end 0)
  4051. (list 'mouse-face 'highlight
  4052. 'keymap org-mouse-map))
  4053. (org-rear-nonsticky-at (match-end 0))
  4054. t)))
  4055. (defun org-activate-footnote-links (limit)
  4056. "Run through the buffer and add overlays to links."
  4057. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4058. limit t)
  4059. (progn
  4060. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4061. (add-text-properties (match-beginning 2) (match-end 2)
  4062. (list 'mouse-face 'highlight
  4063. 'keymap org-mouse-map
  4064. 'help-echo
  4065. (if (= (point-at-bol) (match-beginning 2))
  4066. "Footnote definition"
  4067. "Footnote reference")
  4068. ))
  4069. (org-rear-nonsticky-at (match-end 2))
  4070. t)))
  4071. (defun org-activate-bracket-links (limit)
  4072. "Run through the buffer and add overlays to bracketed links."
  4073. (if (re-search-forward org-bracket-link-regexp limit t)
  4074. (let* ((help (concat "LINK: "
  4075. (org-match-string-no-properties 1)))
  4076. ;; FIXME: above we should remove the escapes.
  4077. ;; but that requires another match, protecting match data,
  4078. ;; a lot of overhead for font-lock.
  4079. (ip (org-maybe-intangible
  4080. (list 'invisible 'org-link
  4081. 'keymap org-mouse-map 'mouse-face 'highlight
  4082. 'font-lock-multiline t 'help-echo help)))
  4083. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4084. 'font-lock-multiline t 'help-echo help)))
  4085. ;; We need to remove the invisible property here. Table narrowing
  4086. ;; may have made some of this invisible.
  4087. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4088. (remove-text-properties (match-beginning 0) (match-end 0)
  4089. '(invisible nil))
  4090. (if (match-end 3)
  4091. (progn
  4092. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4093. (org-rear-nonsticky-at (match-beginning 3))
  4094. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4095. (org-rear-nonsticky-at (match-end 3))
  4096. (add-text-properties (match-end 3) (match-end 0) ip)
  4097. (org-rear-nonsticky-at (match-end 0)))
  4098. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4099. (org-rear-nonsticky-at (match-beginning 1))
  4100. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4101. (org-rear-nonsticky-at (match-end 1))
  4102. (add-text-properties (match-end 1) (match-end 0) ip)
  4103. (org-rear-nonsticky-at (match-end 0)))
  4104. t)))
  4105. (defun org-activate-dates (limit)
  4106. "Run through the buffer and add overlays to dates."
  4107. (if (re-search-forward org-tsr-regexp-both limit t)
  4108. (progn
  4109. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4110. (add-text-properties (match-beginning 0) (match-end 0)
  4111. (list 'mouse-face 'highlight
  4112. 'keymap org-mouse-map))
  4113. (org-rear-nonsticky-at (match-end 0))
  4114. (when org-display-custom-times
  4115. (if (match-end 3)
  4116. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4117. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4118. t)))
  4119. (defvar org-target-link-regexp nil
  4120. "Regular expression matching radio targets in plain text.")
  4121. (make-variable-buffer-local 'org-target-link-regexp)
  4122. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4123. "Regular expression matching a link target.")
  4124. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4125. "Regular expression matching a radio target.")
  4126. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4127. "Regular expression matching any target.")
  4128. (defun org-activate-target-links (limit)
  4129. "Run through the buffer and add overlays to target matches."
  4130. (when org-target-link-regexp
  4131. (let ((case-fold-search t))
  4132. (if (re-search-forward org-target-link-regexp limit t)
  4133. (progn
  4134. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4135. (add-text-properties (match-beginning 0) (match-end 0)
  4136. (list 'mouse-face 'highlight
  4137. 'keymap org-mouse-map
  4138. 'help-echo "Radio target link"
  4139. 'org-linked-text t))
  4140. (org-rear-nonsticky-at (match-end 0))
  4141. t)))))
  4142. (defun org-update-radio-target-regexp ()
  4143. "Find all radio targets in this file and update the regular expression."
  4144. (interactive)
  4145. (when (memq 'radio org-activate-links)
  4146. (setq org-target-link-regexp
  4147. (org-make-target-link-regexp (org-all-targets 'radio)))
  4148. (org-restart-font-lock)))
  4149. (defun org-hide-wide-columns (limit)
  4150. (let (s e)
  4151. (setq s (text-property-any (point) (or limit (point-max))
  4152. 'org-cwidth t))
  4153. (when s
  4154. (setq e (next-single-property-change s 'org-cwidth))
  4155. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4156. (goto-char e)
  4157. t)))
  4158. (defvar org-latex-and-specials-regexp nil
  4159. "Regular expression for highlighting export special stuff.")
  4160. (defvar org-match-substring-regexp)
  4161. (defvar org-match-substring-with-braces-regexp)
  4162. ;; This should be with the exporter code, but we also use if for font-locking
  4163. (defconst org-export-html-special-string-regexps
  4164. '(("\\\\-" . "&shy;")
  4165. ("---\\([^-]\\)" . "&mdash;\\1")
  4166. ("--\\([^-]\\)" . "&ndash;\\1")
  4167. ("\\.\\.\\." . "&hellip;"))
  4168. "Regular expressions for special string conversion.")
  4169. (defun org-compute-latex-and-specials-regexp ()
  4170. "Compute regular expression for stuff treated specially by exporters."
  4171. (if (not org-highlight-latex-fragments-and-specials)
  4172. (org-set-local 'org-latex-and-specials-regexp nil)
  4173. (require 'org-exp)
  4174. (let*
  4175. ((matchers (plist-get org-format-latex-options :matchers))
  4176. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4177. org-latex-regexps)))
  4178. (options (org-combine-plists (org-default-export-plist)
  4179. (org-infile-export-plist)))
  4180. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4181. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4182. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4183. (org-export-html-expand (plist-get options :expand-quoted-html))
  4184. (org-export-with-special-strings (plist-get options :special-strings))
  4185. (re-sub
  4186. (cond
  4187. ((equal org-export-with-sub-superscripts '{})
  4188. (list org-match-substring-with-braces-regexp))
  4189. (org-export-with-sub-superscripts
  4190. (list org-match-substring-regexp))
  4191. (t nil)))
  4192. (re-latex
  4193. (if org-export-with-LaTeX-fragments
  4194. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4195. (re-macros
  4196. (if org-export-with-TeX-macros
  4197. (list (concat "\\\\"
  4198. (regexp-opt
  4199. (append (mapcar 'car org-html-entities)
  4200. (if (boundp 'org-latex-entities)
  4201. (mapcar (lambda (x)
  4202. (or (car-safe x) x))
  4203. org-latex-entities)
  4204. nil))
  4205. 'words))) ; FIXME
  4206. ))
  4207. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4208. (re-special (if org-export-with-special-strings
  4209. (mapcar (lambda (x) (car x))
  4210. org-export-html-special-string-regexps)))
  4211. (re-rest
  4212. (delq nil
  4213. (list
  4214. (if org-export-html-expand "@<[^>\n]+>")
  4215. ))))
  4216. (org-set-local
  4217. 'org-latex-and-specials-regexp
  4218. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4219. re-rest) "\\|")))))
  4220. (defun org-do-latex-and-special-faces (limit)
  4221. "Run through the buffer and add overlays to links."
  4222. (when org-latex-and-specials-regexp
  4223. (let (rtn d)
  4224. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4225. limit t))
  4226. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4227. 'face))
  4228. '(org-code org-verbatim underline)))
  4229. (progn
  4230. (setq rtn t
  4231. d (cond ((member (char-after (1+ (match-beginning 0)))
  4232. '(?_ ?^)) 1)
  4233. (t 0)))
  4234. (font-lock-prepend-text-property
  4235. (+ d (match-beginning 0)) (match-end 0)
  4236. 'face 'org-latex-and-export-specials)
  4237. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4238. '(font-lock-multiline t)))))
  4239. rtn)))
  4240. (defun org-restart-font-lock ()
  4241. "Restart font-lock-mode, to force refontification."
  4242. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4243. (font-lock-mode -1)
  4244. (font-lock-mode 1)))
  4245. (defun org-all-targets (&optional radio)
  4246. "Return a list of all targets in this file.
  4247. With optional argument RADIO, only find radio targets."
  4248. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4249. rtn)
  4250. (save-excursion
  4251. (goto-char (point-min))
  4252. (while (re-search-forward re nil t)
  4253. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4254. rtn)))
  4255. (defun org-make-target-link-regexp (targets)
  4256. "Make regular expression matching all strings in TARGETS.
  4257. The regular expression finds the targets also if there is a line break
  4258. between words."
  4259. (and targets
  4260. (concat
  4261. "\\<\\("
  4262. (mapconcat
  4263. (lambda (x)
  4264. (while (string-match " +" x)
  4265. (setq x (replace-match "\\s-+" t t x)))
  4266. x)
  4267. targets
  4268. "\\|")
  4269. "\\)\\>")))
  4270. (defun org-activate-tags (limit)
  4271. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4272. (progn
  4273. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4274. (add-text-properties (match-beginning 1) (match-end 1)
  4275. (list 'mouse-face 'highlight
  4276. 'keymap org-mouse-map))
  4277. (org-rear-nonsticky-at (match-end 1))
  4278. t)))
  4279. (defun org-outline-level ()
  4280. "Compute the outline level of the heading at point.
  4281. This function assumes that the cursor is at the beginning of a line matched
  4282. by outline-regexp. Otherwise it returns garbage.
  4283. If this is called at a normal headline, the level is the number of stars.
  4284. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4285. For plain list items, if they are matched by `outline-regexp', this returns
  4286. 1000 plus the line indentation."
  4287. (save-excursion
  4288. (looking-at outline-regexp)
  4289. (if (match-beginning 1)
  4290. (+ (org-get-string-indentation (match-string 1)) 1000)
  4291. (1- (- (match-end 0) (match-beginning 0))))))
  4292. (defvar org-font-lock-keywords nil)
  4293. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4294. "Regular expression matching a property line.")
  4295. (defvar org-font-lock-hook nil
  4296. "Functions to be called for special font lock stuff.")
  4297. (defun org-font-lock-hook (limit)
  4298. (run-hook-with-args 'org-font-lock-hook limit))
  4299. (defun org-set-font-lock-defaults ()
  4300. (let* ((em org-fontify-emphasized-text)
  4301. (lk org-activate-links)
  4302. (org-font-lock-extra-keywords
  4303. (list
  4304. ;; Call the hook
  4305. '(org-font-lock-hook)
  4306. ;; Headlines
  4307. `(,(if org-fontify-whole-heading-line
  4308. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4309. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4310. (1 (org-get-level-face 1))
  4311. (2 (org-get-level-face 2))
  4312. (3 (org-get-level-face 3)))
  4313. ;; Table lines
  4314. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4315. (1 'org-table t))
  4316. ;; Table internals
  4317. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4318. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4319. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4320. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4321. ;; Drawers
  4322. (list org-drawer-regexp '(0 'org-special-keyword t))
  4323. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4324. ;; Properties
  4325. (list org-property-re
  4326. '(1 'org-special-keyword t)
  4327. '(3 'org-property-value t))
  4328. ;; Links
  4329. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4330. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4331. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4332. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4333. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4334. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4335. (if (memq 'footnote lk) '(org-activate-footnote-links
  4336. (2 'org-footnote t)))
  4337. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4338. '(org-hide-wide-columns (0 nil append))
  4339. ;; TODO lines
  4340. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4341. '(1 (org-get-todo-face 1) t))
  4342. ;; DONE
  4343. (if org-fontify-done-headline
  4344. (list (concat "^[*]+ +\\<\\("
  4345. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4346. "\\)\\(.*\\)")
  4347. '(2 'org-headline-done t))
  4348. nil)
  4349. ;; Priorities
  4350. '(org-font-lock-add-priority-faces)
  4351. ;; Tags
  4352. '(org-font-lock-add-tag-faces)
  4353. ;; Special keywords
  4354. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4355. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4356. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4357. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4358. ;; Emphasis
  4359. (if em
  4360. (if (featurep 'xemacs)
  4361. '(org-do-emphasis-faces (0 nil append))
  4362. '(org-do-emphasis-faces)))
  4363. ;; Checkboxes
  4364. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4365. 2 'org-checkbox prepend)
  4366. (if org-provide-checkbox-statistics
  4367. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4368. (0 (org-get-checkbox-statistics-face) t)))
  4369. ;; Description list items
  4370. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4371. 2 'bold prepend)
  4372. ;; ARCHIVEd headings
  4373. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4374. '(1 'org-archived prepend))
  4375. ;; Specials
  4376. '(org-do-latex-and-special-faces)
  4377. ;; Code
  4378. '(org-activate-code (1 'org-code t))
  4379. ;; COMMENT
  4380. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4381. "\\|" org-quote-string "\\)\\>")
  4382. '(1 'org-special-keyword t))
  4383. '("^#.*" (0 'font-lock-comment-face t))
  4384. ;; Blocks and meta lines
  4385. '(org-fontify-meta-lines-and-blocks)
  4386. )))
  4387. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4388. ;; Now set the full font-lock-keywords
  4389. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4390. (org-set-local 'font-lock-defaults
  4391. '(org-font-lock-keywords t nil nil backward-paragraph))
  4392. (kill-local-variable 'font-lock-keywords) nil))
  4393. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4394. "Fontify string S like in Org-mode"
  4395. (with-temp-buffer
  4396. (insert s)
  4397. (let ((org-odd-levels-only odd-levels))
  4398. (org-mode)
  4399. (font-lock-fontify-buffer)
  4400. (buffer-string))))
  4401. (defvar org-m nil)
  4402. (defvar org-l nil)
  4403. (defvar org-f nil)
  4404. (defun org-get-level-face (n)
  4405. "Get the right face for match N in font-lock matching of headlines."
  4406. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4407. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4408. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4409. (cond
  4410. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4411. ((eq n 2) org-f)
  4412. (t (if org-level-color-stars-only nil org-f))))
  4413. (defun org-get-todo-face (kwd)
  4414. "Get the right face for a TODO keyword KWD.
  4415. If KWD is a number, get the corresponding match group."
  4416. (if (numberp kwd) (setq kwd (match-string kwd)))
  4417. (or (cdr (assoc kwd org-todo-keyword-faces))
  4418. (and (member kwd org-done-keywords) 'org-done)
  4419. 'org-todo))
  4420. (defun org-font-lock-add-tag-faces (limit)
  4421. "Add the special tag faces."
  4422. (when (and org-tag-faces org-tags-special-faces-re)
  4423. (while (re-search-forward org-tags-special-faces-re limit t)
  4424. (add-text-properties (match-beginning 1) (match-end 1)
  4425. (list 'face (org-get-tag-face 1)
  4426. 'font-lock-fontified t))
  4427. (backward-char 1))))
  4428. (defun org-font-lock-add-priority-faces (limit)
  4429. "Add the special priority faces."
  4430. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4431. (add-text-properties
  4432. (match-beginning 0) (match-end 0)
  4433. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4434. org-priority-faces))
  4435. 'org-special-keyword)
  4436. 'font-lock-fontified t))))
  4437. (defun org-get-tag-face (kwd)
  4438. "Get the right face for a TODO keyword KWD.
  4439. If KWD is a number, get the corresponding match group."
  4440. (if (numberp kwd) (setq kwd (match-string kwd)))
  4441. (or (cdr (assoc kwd org-tag-faces))
  4442. 'org-tag))
  4443. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4444. "Remove fontification and activation overlays from links."
  4445. (font-lock-default-unfontify-region beg end)
  4446. (let* ((buffer-undo-list t)
  4447. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4448. (inhibit-modification-hooks t)
  4449. deactivate-mark buffer-file-name buffer-file-truename)
  4450. (remove-text-properties
  4451. beg end
  4452. (if org-indent-mode
  4453. ;; also remove line-prefix and wrap-prefix properties
  4454. '(mouse-face t keymap t org-linked-text t
  4455. invisible t intangible t
  4456. line-prefix t wrap-prefix t
  4457. org-no-flyspell t)
  4458. '(mouse-face t keymap t org-linked-text t
  4459. invisible t intangible t
  4460. org-no-flyspell t)))))
  4461. ;;;; Visibility cycling, including org-goto and indirect buffer
  4462. ;;; Cycling
  4463. (defvar org-cycle-global-status nil)
  4464. (make-variable-buffer-local 'org-cycle-global-status)
  4465. (defvar org-cycle-subtree-status nil)
  4466. (make-variable-buffer-local 'org-cycle-subtree-status)
  4467. ;;;###autoload
  4468. (defvar org-inlinetask-min-level)
  4469. (defun org-cycle (&optional arg)
  4470. "TAB-action and visibility cycling for Org-mode.
  4471. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4472. is outine visibility cycling, but it also invokes other actions
  4473. in special contexts.
  4474. - When this function is called with a prefix argument, rotate the entire
  4475. buffer through 3 states (global cycling)
  4476. 1. OVERVIEW: Show only top-level headlines.
  4477. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4478. 3. SHOW ALL: Show everything.
  4479. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4480. determined by the variable `org-startup-folded', and by any VISIBILITY
  4481. properties in the buffer.
  4482. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4483. including any drawers.
  4484. - When inside a table, re-align the table and move to the next field.
  4485. - When point is at the beginning of a headline, rotate the subtree started
  4486. by this line through 3 different states (local cycling)
  4487. 1. FOLDED: Only the main headline is shown.
  4488. 2. CHILDREN: The main headline and the direct children are shown.
  4489. From this state, you can move to one of the children
  4490. and zoom in further.
  4491. 3. SUBTREE: Show the entire subtree, including body text.
  4492. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4493. - When there is a numeric prefix, go up to a heading with level ARG, do
  4494. a `show-subtree' and return to the previous cursor position. If ARG
  4495. is negative, go up that many levels.
  4496. - When point is not at the beginning of a headline, execute the global
  4497. binding for TAB, which is re-indenting the line. See the option
  4498. `org-cycle-emulate-tab' for details.
  4499. - Special case: if point is at the beginning of the buffer and there is
  4500. no headline in line 1, this function will act as if called with prefix arg.
  4501. But only if also the variable `org-cycle-global-at-bob' is t."
  4502. (interactive "P")
  4503. (org-load-modules-maybe)
  4504. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  4505. (and org-cycle-level-after-item/entry-creation
  4506. (or (org-cycle-level)
  4507. (org-cycle-item-indentation))))
  4508. (let* ((limit-level
  4509. (or org-cycle-max-level
  4510. (and (boundp 'org-inlinetask-min-level)
  4511. org-inlinetask-min-level
  4512. (1- org-inlinetask-min-level))))
  4513. (nstars (and limit-level
  4514. (if org-odd-levels-only
  4515. (and limit-level (1- (* limit-level 2)))
  4516. limit-level)))
  4517. (outline-regexp
  4518. (cond
  4519. ((not (org-mode-p)) outline-regexp)
  4520. ((or (eq org-cycle-include-plain-lists 'integrate)
  4521. (and org-cycle-include-plain-lists (org-at-item-p)))
  4522. (concat "\\(?:\\*"
  4523. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4524. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4525. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4526. (bob-special (and org-cycle-global-at-bob (bobp)
  4527. (not (looking-at outline-regexp))))
  4528. (org-cycle-hook
  4529. (if bob-special
  4530. (delq 'org-optimize-window-after-visibility-change
  4531. (copy-sequence org-cycle-hook))
  4532. org-cycle-hook))
  4533. (pos (point)))
  4534. (if (or bob-special (equal arg '(4)))
  4535. ;; special case: use global cycling
  4536. (setq arg t))
  4537. (cond
  4538. ((equal arg '(16))
  4539. (org-set-startup-visibility)
  4540. (message "Startup visibility, plus VISIBILITY properties"))
  4541. ((equal arg '(64))
  4542. (show-all)
  4543. (message "Entire buffer visible, including drawers"))
  4544. ((org-at-table-p 'any)
  4545. ;; Enter the table or move to the next field in the table
  4546. (or (org-table-recognize-table.el)
  4547. (progn
  4548. (if arg (org-table-edit-field t)
  4549. (org-table-justify-field-maybe)
  4550. (call-interactively 'org-table-next-field)))))
  4551. ((run-hook-with-args-until-success
  4552. 'org-tab-after-check-for-table-hook))
  4553. ((eq arg t) ;; Global cycling
  4554. (org-cycle-internal-global))
  4555. ((and org-drawers org-drawer-regexp
  4556. (save-excursion
  4557. (beginning-of-line 1)
  4558. (looking-at org-drawer-regexp)))
  4559. ;; Toggle block visibility
  4560. (org-flag-drawer
  4561. (not (get-char-property (match-end 0) 'invisible))))
  4562. ((integerp arg)
  4563. ;; Show-subtree, ARG levels up from here.
  4564. (save-excursion
  4565. (org-back-to-heading)
  4566. (outline-up-heading (if (< arg 0) (- arg)
  4567. (- (funcall outline-level) arg)))
  4568. (org-show-subtree)))
  4569. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4570. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4571. (org-cycle-internal-local))
  4572. ;; TAB emulation and template completion
  4573. (buffer-read-only (org-back-to-heading))
  4574. ((run-hook-with-args-until-success
  4575. 'org-tab-after-check-for-cycling-hook))
  4576. ((org-try-structure-completion))
  4577. ((org-try-cdlatex-tab))
  4578. ((run-hook-with-args-until-success
  4579. 'org-tab-before-tab-emulation-hook))
  4580. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4581. (or (not (bolp))
  4582. (not (looking-at outline-regexp))))
  4583. (call-interactively (global-key-binding "\t")))
  4584. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4585. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4586. (or (and (eq org-cycle-emulate-tab 'white)
  4587. (= (match-end 0) (point-at-eol)))
  4588. (and (eq org-cycle-emulate-tab 'whitestart)
  4589. (>= (match-end 0) pos))))
  4590. t
  4591. (eq org-cycle-emulate-tab t))
  4592. (call-interactively (global-key-binding "\t")))
  4593. (t (save-excursion
  4594. (org-back-to-heading)
  4595. (org-cycle)))))))
  4596. (defun org-cycle-internal-global ()
  4597. "Do the global cycling action."
  4598. (cond
  4599. ((and (eq last-command this-command)
  4600. (eq org-cycle-global-status 'overview))
  4601. ;; We just created the overview - now do table of contents
  4602. ;; This can be slow in very large buffers, so indicate action
  4603. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4604. (message "CONTENTS...")
  4605. (org-content)
  4606. (message "CONTENTS...done")
  4607. (setq org-cycle-global-status 'contents)
  4608. (run-hook-with-args 'org-cycle-hook 'contents))
  4609. ((and (eq last-command this-command)
  4610. (eq org-cycle-global-status 'contents))
  4611. ;; We just showed the table of contents - now show everything
  4612. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4613. (show-all)
  4614. (message "SHOW ALL")
  4615. (setq org-cycle-global-status 'all)
  4616. (run-hook-with-args 'org-cycle-hook 'all))
  4617. (t
  4618. ;; Default action: go to overview
  4619. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4620. (org-overview)
  4621. (message "OVERVIEW")
  4622. (setq org-cycle-global-status 'overview)
  4623. (run-hook-with-args 'org-cycle-hook 'overview))))
  4624. (defun org-cycle-internal-local ()
  4625. "Do the local cycling action."
  4626. (org-back-to-heading)
  4627. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4628. ;; First, some boundaries
  4629. (save-excursion
  4630. (org-back-to-heading)
  4631. (setq level (funcall outline-level))
  4632. (save-excursion
  4633. (beginning-of-line 2)
  4634. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4635. ; XEmacs does not have `next-single-char-property-change'
  4636. ; I'm not sure about Emacs 21.
  4637. (while (and (not (eobp)) ;; this is like `next-line'
  4638. (get-char-property (1- (point)) 'invisible))
  4639. (beginning-of-line 2))
  4640. (while (and (not (eobp)) ;; this is like `next-line'
  4641. (get-char-property (1- (point)) 'invisible))
  4642. (goto-char (next-single-char-property-change (point) 'invisible))
  4643. ;;;??? (or (bolp) (beginning-of-line 2))))
  4644. (and (eolp) (beginning-of-line 2))))
  4645. (setq eol (point)))
  4646. (outline-end-of-heading) (setq eoh (point))
  4647. (save-excursion
  4648. (outline-next-heading)
  4649. (setq has-children (and (org-at-heading-p t)
  4650. (> (funcall outline-level) level))))
  4651. (org-end-of-subtree t)
  4652. (unless (eobp)
  4653. (skip-chars-forward " \t\n")
  4654. (beginning-of-line 1) ; in case this is an item
  4655. )
  4656. (setq eos (if (eobp) (point) (1- (point)))))
  4657. ;; Find out what to do next and set `this-command'
  4658. (cond
  4659. ((= eos eoh)
  4660. ;; Nothing is hidden behind this heading
  4661. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4662. (message "EMPTY ENTRY")
  4663. (setq org-cycle-subtree-status nil)
  4664. (save-excursion
  4665. (goto-char eos)
  4666. (outline-next-heading)
  4667. (if (org-invisible-p) (org-flag-heading nil))))
  4668. ((and (or (>= eol eos)
  4669. (not (string-match "\\S-" (buffer-substring eol eos))))
  4670. (or has-children
  4671. (not (setq children-skipped
  4672. org-cycle-skip-children-state-if-no-children))))
  4673. ;; Entire subtree is hidden in one line: children view
  4674. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4675. (org-show-entry)
  4676. (show-children)
  4677. (message "CHILDREN")
  4678. (save-excursion
  4679. (goto-char eos)
  4680. (outline-next-heading)
  4681. (if (org-invisible-p) (org-flag-heading nil)))
  4682. (setq org-cycle-subtree-status 'children)
  4683. (run-hook-with-args 'org-cycle-hook 'children))
  4684. ((or children-skipped
  4685. (and (eq last-command this-command)
  4686. (eq org-cycle-subtree-status 'children)))
  4687. ;; We just showed the children, or no children are there,
  4688. ;; now show everything.
  4689. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4690. (org-show-subtree)
  4691. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4692. (setq org-cycle-subtree-status 'subtree)
  4693. (run-hook-with-args 'org-cycle-hook 'subtree))
  4694. (t
  4695. ;; Default action: hide the subtree.
  4696. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4697. (hide-subtree)
  4698. (message "FOLDED")
  4699. (setq org-cycle-subtree-status 'folded)
  4700. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4701. ;;;###autoload
  4702. (defun org-global-cycle (&optional arg)
  4703. "Cycle the global visibility. For details see `org-cycle'.
  4704. With C-u prefix arg, switch to startup visibility.
  4705. With a numeric prefix, show all headlines up to that level."
  4706. (interactive "P")
  4707. (let ((org-cycle-include-plain-lists
  4708. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4709. (cond
  4710. ((integerp arg)
  4711. (show-all)
  4712. (hide-sublevels arg)
  4713. (setq org-cycle-global-status 'contents))
  4714. ((equal arg '(4))
  4715. (org-set-startup-visibility)
  4716. (message "Startup visibility, plus VISIBILITY properties."))
  4717. (t
  4718. (org-cycle '(4))))))
  4719. (defun org-set-startup-visibility ()
  4720. "Set the visibility required by startup options and properties."
  4721. (cond
  4722. ((eq org-startup-folded t)
  4723. (org-cycle '(4)))
  4724. ((eq org-startup-folded 'content)
  4725. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4726. (org-cycle '(4)) (org-cycle '(4)))))
  4727. (unless (eq org-startup-folded 'showeverything)
  4728. (if org-hide-block-startup (org-hide-block-all))
  4729. (org-set-visibility-according-to-property 'no-cleanup)
  4730. (org-cycle-hide-archived-subtrees 'all)
  4731. (org-cycle-hide-drawers 'all)
  4732. (org-cycle-show-empty-lines 'all)))
  4733. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4734. "Switch subtree visibilities according to :VISIBILITY: property."
  4735. (interactive)
  4736. (let (org-show-entry-below state)
  4737. (save-excursion
  4738. (goto-char (point-min))
  4739. (while (re-search-forward
  4740. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4741. nil t)
  4742. (setq state (match-string 1))
  4743. (save-excursion
  4744. (org-back-to-heading t)
  4745. (hide-subtree)
  4746. (org-reveal)
  4747. (cond
  4748. ((equal state '("fold" "folded"))
  4749. (hide-subtree))
  4750. ((equal state "children")
  4751. (org-show-hidden-entry)
  4752. (show-children))
  4753. ((equal state "content")
  4754. (save-excursion
  4755. (save-restriction
  4756. (org-narrow-to-subtree)
  4757. (org-content))))
  4758. ((member state '("all" "showall"))
  4759. (show-subtree)))))
  4760. (unless no-cleanup
  4761. (org-cycle-hide-archived-subtrees 'all)
  4762. (org-cycle-hide-drawers 'all)
  4763. (org-cycle-show-empty-lines 'all)))))
  4764. (defun org-overview ()
  4765. "Switch to overview mode, showing only top-level headlines.
  4766. Really, this shows all headlines with level equal or greater than the level
  4767. of the first headline in the buffer. This is important, because if the
  4768. first headline is not level one, then (hide-sublevels 1) gives confusing
  4769. results."
  4770. (interactive)
  4771. (let ((level (save-excursion
  4772. (goto-char (point-min))
  4773. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4774. (progn
  4775. (goto-char (match-beginning 0))
  4776. (funcall outline-level))))))
  4777. (and level (hide-sublevels level))))
  4778. (defun org-content (&optional arg)
  4779. "Show all headlines in the buffer, like a table of contents.
  4780. With numerical argument N, show content up to level N."
  4781. (interactive "P")
  4782. (save-excursion
  4783. ;; Visit all headings and show their offspring
  4784. (and (integerp arg) (org-overview))
  4785. (goto-char (point-max))
  4786. (catch 'exit
  4787. (while (and (progn (condition-case nil
  4788. (outline-previous-visible-heading 1)
  4789. (error (goto-char (point-min))))
  4790. t)
  4791. (looking-at outline-regexp))
  4792. (if (integerp arg)
  4793. (show-children (1- arg))
  4794. (show-branches))
  4795. (if (bobp) (throw 'exit nil))))))
  4796. (defun org-optimize-window-after-visibility-change (state)
  4797. "Adjust the window after a change in outline visibility.
  4798. This function is the default value of the hook `org-cycle-hook'."
  4799. (when (get-buffer-window (current-buffer))
  4800. (cond
  4801. ((eq state 'content) nil)
  4802. ((eq state 'all) nil)
  4803. ((eq state 'folded) nil)
  4804. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4805. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4806. ;; FIXME: no longer in use
  4807. (defun org-compact-display-after-subtree-move ()
  4808. "Show a compacter version of the tree of the entry's parent."
  4809. (save-excursion
  4810. (if (org-up-heading-safe)
  4811. (progn
  4812. (hide-subtree)
  4813. (show-entry)
  4814. (show-children)
  4815. (org-cycle-show-empty-lines 'children)
  4816. (org-cycle-hide-drawers 'children))
  4817. (org-overview))))
  4818. (defun org-remove-empty-overlays-at (pos)
  4819. "Remove outline overlays that do not contain non-white stuff."
  4820. (mapc
  4821. (lambda (o)
  4822. (and (eq 'outline (org-overlay-get o 'invisible))
  4823. (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
  4824. (org-overlay-end o))))
  4825. (org-delete-overlay o)))
  4826. (org-overlays-at pos)))
  4827. (defun org-clean-visibility-after-subtree-move ()
  4828. "Fix visibility issues after moving a subtree."
  4829. ;; First, find a reasonable region to look at:
  4830. ;; Start two siblings above, end three below
  4831. (let* ((beg (save-excursion
  4832. (and (org-get-last-sibling)
  4833. (org-get-last-sibling))
  4834. (point)))
  4835. (end (save-excursion
  4836. (and (org-get-next-sibling)
  4837. (org-get-next-sibling)
  4838. (org-get-next-sibling))
  4839. (if (org-at-heading-p)
  4840. (point-at-eol)
  4841. (point))))
  4842. (level (looking-at "\\*+"))
  4843. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  4844. (save-excursion
  4845. (save-restriction
  4846. (narrow-to-region beg end)
  4847. (when re
  4848. ;; Properly fold already folded siblings
  4849. (goto-char (point-min))
  4850. (while (re-search-forward re nil t)
  4851. (if (save-excursion (goto-char (point-at-eol)) (org-invisible-p))
  4852. (hide-entry))))
  4853. (org-cycle-show-empty-lines 'overview)
  4854. (org-cycle-hide-drawers 'overview)))))
  4855. (defun org-cycle-show-empty-lines (state)
  4856. "Show empty lines above all visible headlines.
  4857. The region to be covered depends on STATE when called through
  4858. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4859. entire buffer covered. Note that an empty line is only shown if there
  4860. are at least `org-cycle-separator-lines' empty lines before the headline."
  4861. (when (not (= org-cycle-separator-lines 0))
  4862. (save-excursion
  4863. (let* ((n (abs org-cycle-separator-lines))
  4864. (re (cond
  4865. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4866. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4867. (t (let ((ns (number-to-string (- n 2))))
  4868. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4869. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4870. beg end b e)
  4871. (cond
  4872. ((memq state '(overview contents t))
  4873. (setq beg (point-min) end (point-max)))
  4874. ((memq state '(children folded))
  4875. (setq beg (point) end (progn (org-end-of-subtree t t)
  4876. (beginning-of-line 2)
  4877. (point)))))
  4878. (when beg
  4879. (goto-char beg)
  4880. (while (re-search-forward re end t)
  4881. (unless (get-char-property (match-end 1) 'invisible)
  4882. (setq e (match-end 1))
  4883. (if (< org-cycle-separator-lines 0)
  4884. (setq b (save-excursion
  4885. (goto-char (match-beginning 0))
  4886. (org-back-over-empty-lines)
  4887. (if (save-excursion
  4888. (goto-char (max (point-min) (1- (point))))
  4889. (org-on-heading-p))
  4890. (1- (point))
  4891. (point))))
  4892. (setq b (match-beginning 1)))
  4893. (outline-flag-region b e nil)))))))
  4894. ;; Never hide empty lines at the end of the file.
  4895. (save-excursion
  4896. (goto-char (point-max))
  4897. (outline-previous-heading)
  4898. (outline-end-of-heading)
  4899. (if (and (looking-at "[ \t\n]+")
  4900. (= (match-end 0) (point-max)))
  4901. (outline-flag-region (point) (match-end 0) nil))))
  4902. (defun org-show-empty-lines-in-parent ()
  4903. "Move to the parent and re-show empty lines before visible headlines."
  4904. (save-excursion
  4905. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4906. (org-cycle-show-empty-lines context))))
  4907. (defun org-files-list ()
  4908. "Return `org-agenda-files' list, plus all open org-mode files.
  4909. This is useful for operations that need to scan all of a user's
  4910. open and agenda-wise Org files."
  4911. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  4912. (dolist (buf (buffer-list))
  4913. (with-current-buffer buf
  4914. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  4915. (let ((file (expand-file-name (buffer-file-name))))
  4916. (unless (member file files)
  4917. (push file files))))))
  4918. files))
  4919. (defsubst org-entry-beginning-position ()
  4920. "Return the beginning position of the current entry."
  4921. (save-excursion (outline-back-to-heading t) (point)))
  4922. (defsubst org-entry-end-position ()
  4923. "Return the end position of the current entry."
  4924. (save-excursion (outline-next-heading) (point)))
  4925. (defun org-cycle-hide-drawers (state)
  4926. "Re-hide all drawers after a visibility state change."
  4927. (when (and (org-mode-p)
  4928. (not (memq state '(overview folded contents))))
  4929. (save-excursion
  4930. (let* ((globalp (memq state '(contents all)))
  4931. (beg (if globalp (point-min) (point)))
  4932. (end (if globalp (point-max)
  4933. (if (eq state 'children)
  4934. (save-excursion (outline-next-heading) (point))
  4935. (org-end-of-subtree t)))))
  4936. (goto-char beg)
  4937. (while (re-search-forward org-drawer-regexp end t)
  4938. (org-flag-drawer t))))))
  4939. (defun org-flag-drawer (flag)
  4940. (save-excursion
  4941. (beginning-of-line 1)
  4942. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4943. (let ((b (match-end 0))
  4944. (outline-regexp org-outline-regexp))
  4945. (if (re-search-forward
  4946. "^[ \t]*:END:"
  4947. (save-excursion (outline-next-heading) (point)) t)
  4948. (outline-flag-region b (point-at-eol) flag)
  4949. (error ":END: line missing at position %s" b))))))
  4950. (defun org-subtree-end-visible-p ()
  4951. "Is the end of the current subtree visible?"
  4952. (pos-visible-in-window-p
  4953. (save-excursion (org-end-of-subtree t) (point))))
  4954. (defun org-first-headline-recenter (&optional N)
  4955. "Move cursor to the first headline and recenter the headline.
  4956. Optional argument N means, put the headline into the Nth line of the window."
  4957. (goto-char (point-min))
  4958. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4959. (beginning-of-line)
  4960. (recenter (prefix-numeric-value N))))
  4961. ;;; Folding of blocks
  4962. (defconst org-block-regexp
  4963. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  4964. "Regular expression for hiding blocks.")
  4965. (defvar org-hide-block-overlays nil
  4966. "Overays hiding blocks.")
  4967. (make-variable-buffer-local 'org-hide-block-overlays)
  4968. (defun org-block-map (function &optional start end)
  4969. "Call func at the head of all source blocks in the current
  4970. buffer. Optional arguments START and END can be used to limit
  4971. the range."
  4972. (let ((start (or start (point-min)))
  4973. (end (or end (point-max))))
  4974. (save-excursion
  4975. (goto-char start)
  4976. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  4977. (save-excursion
  4978. (save-match-data
  4979. (goto-char (match-beginning 0))
  4980. (funcall function)))))))
  4981. (defun org-hide-block-toggle-all ()
  4982. "Toggle the visibility of all blocks in the current buffer."
  4983. (org-block-map #'org-hide-block-toggle))
  4984. (defun org-hide-block-all ()
  4985. "Fold all blocks in the current buffer."
  4986. (interactive)
  4987. (org-show-block-all)
  4988. (org-block-map #'org-hide-block-toggle-maybe))
  4989. (defun org-show-block-all ()
  4990. "Unfold all blocks in the current buffer."
  4991. (mapc 'org-delete-overlay org-hide-block-overlays)
  4992. (setq org-hide-block-overlays nil))
  4993. (defun org-hide-block-toggle-maybe ()
  4994. "Toggle visibility of block at point."
  4995. (interactive)
  4996. (let ((case-fold-search t))
  4997. (if (save-excursion
  4998. (beginning-of-line 1)
  4999. (looking-at org-block-regexp))
  5000. (progn (org-hide-block-toggle)
  5001. t) ;; to signal that we took action
  5002. nil))) ;; to signal that we did not
  5003. (defun org-hide-block-toggle (&optional force)
  5004. "Toggle the visibility of the current block."
  5005. (interactive)
  5006. (save-excursion
  5007. (beginning-of-line)
  5008. (if (re-search-forward org-block-regexp nil t)
  5009. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5010. (end (match-end 0)) ;; end of entire body
  5011. ov)
  5012. (if (memq t (mapcar (lambda (overlay)
  5013. (eq (org-overlay-get overlay 'invisible)
  5014. 'org-hide-block))
  5015. (org-overlays-at start)))
  5016. (if (or (not force) (eq force 'off))
  5017. (mapc (lambda (ov)
  5018. (when (member ov org-hide-block-overlays)
  5019. (setq org-hide-block-overlays
  5020. (delq ov org-hide-block-overlays)))
  5021. (when (eq (org-overlay-get ov 'invisible)
  5022. 'org-hide-block)
  5023. (org-delete-overlay ov)))
  5024. (org-overlays-at start)))
  5025. (setq ov (org-make-overlay start end))
  5026. (org-overlay-put ov 'invisible 'org-hide-block)
  5027. ;; make the block accessible to isearch
  5028. (org-overlay-put
  5029. ov 'isearch-open-invisible
  5030. (lambda (ov)
  5031. (when (member ov org-hide-block-overlays)
  5032. (setq org-hide-block-overlays
  5033. (delq ov org-hide-block-overlays)))
  5034. (when (eq (org-overlay-get ov 'invisible)
  5035. 'org-hide-block)
  5036. (org-delete-overlay ov))))
  5037. (push ov org-hide-block-overlays)))
  5038. (error "Not looking at a source block"))))
  5039. ;; org-tab-after-check-for-cycling-hook
  5040. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5041. ;; Remove overlays when changing major mode
  5042. (add-hook 'org-mode-hook
  5043. (lambda () (org-add-hook 'change-major-mode-hook
  5044. 'org-show-block-all 'append 'local)))
  5045. ;;; Org-goto
  5046. (defvar org-goto-window-configuration nil)
  5047. (defvar org-goto-marker nil)
  5048. (defvar org-goto-map
  5049. (let ((map (make-sparse-keymap)))
  5050. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5051. (while (setq cmd (pop cmds))
  5052. (substitute-key-definition cmd cmd map global-map)))
  5053. (suppress-keymap map)
  5054. (org-defkey map "\C-m" 'org-goto-ret)
  5055. (org-defkey map [(return)] 'org-goto-ret)
  5056. (org-defkey map [(left)] 'org-goto-left)
  5057. (org-defkey map [(right)] 'org-goto-right)
  5058. (org-defkey map [(control ?g)] 'org-goto-quit)
  5059. (org-defkey map "\C-i" 'org-cycle)
  5060. (org-defkey map [(tab)] 'org-cycle)
  5061. (org-defkey map [(down)] 'outline-next-visible-heading)
  5062. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5063. (if org-goto-auto-isearch
  5064. (if (fboundp 'define-key-after)
  5065. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5066. nil)
  5067. (org-defkey map "q" 'org-goto-quit)
  5068. (org-defkey map "n" 'outline-next-visible-heading)
  5069. (org-defkey map "p" 'outline-previous-visible-heading)
  5070. (org-defkey map "f" 'outline-forward-same-level)
  5071. (org-defkey map "b" 'outline-backward-same-level)
  5072. (org-defkey map "u" 'outline-up-heading))
  5073. (org-defkey map "/" 'org-occur)
  5074. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5075. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5076. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5077. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5078. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5079. map))
  5080. (defconst org-goto-help
  5081. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5082. RET=jump to location [Q]uit and return to previous location
  5083. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5084. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5085. ;; FIXME: Docstring doe not mention both interfaces
  5086. (defun org-goto (&optional alternative-interface)
  5087. "Look up a different location in the current file, keeping current visibility.
  5088. When you want look-up or go to a different location in a document, the
  5089. fastest way is often to fold the entire buffer and then dive into the tree.
  5090. This method has the disadvantage, that the previous location will be folded,
  5091. which may not be what you want.
  5092. This command works around this by showing a copy of the current buffer
  5093. in an indirect buffer, in overview mode. You can dive into the tree in
  5094. that copy, use org-occur and incremental search to find a location.
  5095. When pressing RET or `Q', the command returns to the original buffer in
  5096. which the visibility is still unchanged. After RET is will also jump to
  5097. the location selected in the indirect buffer and expose the
  5098. the headline hierarchy above."
  5099. (interactive "P")
  5100. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5101. (org-refile-use-outline-path t)
  5102. (org-refile-target-verify-function nil)
  5103. (interface
  5104. (if (not alternative-interface)
  5105. org-goto-interface
  5106. (if (eq org-goto-interface 'outline)
  5107. 'outline-path-completion
  5108. 'outline)))
  5109. (org-goto-start-pos (point))
  5110. (selected-point
  5111. (if (eq interface 'outline)
  5112. (car (org-get-location (current-buffer) org-goto-help))
  5113. (nth 3 (org-refile-get-location "Goto: ")))))
  5114. (if selected-point
  5115. (progn
  5116. (org-mark-ring-push org-goto-start-pos)
  5117. (goto-char selected-point)
  5118. (if (or (org-invisible-p) (org-invisible-p2))
  5119. (org-show-context 'org-goto)))
  5120. (message "Quit"))))
  5121. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5122. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5123. (defvar org-goto-local-auto-isearch-map) ; defined below
  5124. (defun org-get-location (buf help)
  5125. "Let the user select a location in the Org-mode buffer BUF.
  5126. This function uses a recursive edit. It returns the selected position
  5127. or nil."
  5128. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5129. (isearch-hide-immediately nil)
  5130. (isearch-search-fun-function
  5131. (lambda () 'org-goto-local-search-headings))
  5132. (org-goto-selected-point org-goto-exit-command))
  5133. (save-excursion
  5134. (save-window-excursion
  5135. (delete-other-windows)
  5136. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5137. (switch-to-buffer
  5138. (condition-case nil
  5139. (make-indirect-buffer (current-buffer) "*org-goto*")
  5140. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5141. (with-output-to-temp-buffer "*Help*"
  5142. (princ help))
  5143. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5144. (setq buffer-read-only nil)
  5145. (let ((org-startup-truncated t)
  5146. (org-startup-folded nil)
  5147. (org-startup-align-all-tables nil))
  5148. (org-mode)
  5149. (org-overview))
  5150. (setq buffer-read-only t)
  5151. (if (and (boundp 'org-goto-start-pos)
  5152. (integer-or-marker-p org-goto-start-pos))
  5153. (let ((org-show-hierarchy-above t)
  5154. (org-show-siblings t)
  5155. (org-show-following-heading t))
  5156. (goto-char org-goto-start-pos)
  5157. (and (org-invisible-p) (org-show-context)))
  5158. (goto-char (point-min)))
  5159. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5160. (message "Select location and press RET")
  5161. (use-local-map org-goto-map)
  5162. (recursive-edit)
  5163. ))
  5164. (kill-buffer "*org-goto*")
  5165. (cons org-goto-selected-point org-goto-exit-command)))
  5166. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5167. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5168. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5169. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5170. (defun org-goto-local-search-headings (string bound noerror)
  5171. "Search and make sure that any matches are in headlines."
  5172. (catch 'return
  5173. (while (if isearch-forward
  5174. (search-forward string bound noerror)
  5175. (search-backward string bound noerror))
  5176. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5177. (and (member :headline context)
  5178. (not (member :tags context))))
  5179. (throw 'return (point))))))
  5180. (defun org-goto-local-auto-isearch ()
  5181. "Start isearch."
  5182. (interactive)
  5183. (goto-char (point-min))
  5184. (let ((keys (this-command-keys)))
  5185. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5186. (isearch-mode t)
  5187. (isearch-process-search-char (string-to-char keys)))))
  5188. (defun org-goto-ret (&optional arg)
  5189. "Finish `org-goto' by going to the new location."
  5190. (interactive "P")
  5191. (setq org-goto-selected-point (point)
  5192. org-goto-exit-command 'return)
  5193. (throw 'exit nil))
  5194. (defun org-goto-left ()
  5195. "Finish `org-goto' by going to the new location."
  5196. (interactive)
  5197. (if (org-on-heading-p)
  5198. (progn
  5199. (beginning-of-line 1)
  5200. (setq org-goto-selected-point (point)
  5201. org-goto-exit-command 'left)
  5202. (throw 'exit nil))
  5203. (error "Not on a heading")))
  5204. (defun org-goto-right ()
  5205. "Finish `org-goto' by going to the new location."
  5206. (interactive)
  5207. (if (org-on-heading-p)
  5208. (progn
  5209. (setq org-goto-selected-point (point)
  5210. org-goto-exit-command 'right)
  5211. (throw 'exit nil))
  5212. (error "Not on a heading")))
  5213. (defun org-goto-quit ()
  5214. "Finish `org-goto' without cursor motion."
  5215. (interactive)
  5216. (setq org-goto-selected-point nil)
  5217. (setq org-goto-exit-command 'quit)
  5218. (throw 'exit nil))
  5219. ;;; Indirect buffer display of subtrees
  5220. (defvar org-indirect-dedicated-frame nil
  5221. "This is the frame being used for indirect tree display.")
  5222. (defvar org-last-indirect-buffer nil)
  5223. (defun org-tree-to-indirect-buffer (&optional arg)
  5224. "Create indirect buffer and narrow it to current subtree.
  5225. With numerical prefix ARG, go up to this level and then take that tree.
  5226. If ARG is negative, go up that many levels.
  5227. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5228. indirect buffer previously made with this command, to avoid proliferation of
  5229. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5230. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5231. is kept so that you can work with several indirect buffers at the same time.
  5232. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5233. requests that a new frame be made for the new buffer, so that the dedicated
  5234. frame is not changed."
  5235. (interactive "P")
  5236. (let ((cbuf (current-buffer))
  5237. (cwin (selected-window))
  5238. (pos (point))
  5239. beg end level heading ibuf)
  5240. (save-excursion
  5241. (org-back-to-heading t)
  5242. (when (numberp arg)
  5243. (setq level (org-outline-level))
  5244. (if (< arg 0) (setq arg (+ level arg)))
  5245. (while (> (setq level (org-outline-level)) arg)
  5246. (outline-up-heading 1 t)))
  5247. (setq beg (point)
  5248. heading (org-get-heading))
  5249. (org-end-of-subtree t t) (setq end (point)))
  5250. (if (and (buffer-live-p org-last-indirect-buffer)
  5251. (not (eq org-indirect-buffer-display 'new-frame))
  5252. (not arg))
  5253. (kill-buffer org-last-indirect-buffer))
  5254. (setq ibuf (org-get-indirect-buffer cbuf)
  5255. org-last-indirect-buffer ibuf)
  5256. (cond
  5257. ((or (eq org-indirect-buffer-display 'new-frame)
  5258. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5259. (select-frame (make-frame))
  5260. (delete-other-windows)
  5261. (switch-to-buffer ibuf)
  5262. (org-set-frame-title heading))
  5263. ((eq org-indirect-buffer-display 'dedicated-frame)
  5264. (raise-frame
  5265. (select-frame (or (and org-indirect-dedicated-frame
  5266. (frame-live-p org-indirect-dedicated-frame)
  5267. org-indirect-dedicated-frame)
  5268. (setq org-indirect-dedicated-frame (make-frame)))))
  5269. (delete-other-windows)
  5270. (switch-to-buffer ibuf)
  5271. (org-set-frame-title (concat "Indirect: " heading)))
  5272. ((eq org-indirect-buffer-display 'current-window)
  5273. (switch-to-buffer ibuf))
  5274. ((eq org-indirect-buffer-display 'other-window)
  5275. (pop-to-buffer ibuf))
  5276. (t (error "Invalid value")))
  5277. (if (featurep 'xemacs)
  5278. (save-excursion (org-mode) (turn-on-font-lock)))
  5279. (narrow-to-region beg end)
  5280. (show-all)
  5281. (goto-char pos)
  5282. (and (window-live-p cwin) (select-window cwin))))
  5283. (defun org-get-indirect-buffer (&optional buffer)
  5284. (setq buffer (or buffer (current-buffer)))
  5285. (let ((n 1) (base (buffer-name buffer)) bname)
  5286. (while (buffer-live-p
  5287. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5288. (setq n (1+ n)))
  5289. (condition-case nil
  5290. (make-indirect-buffer buffer bname 'clone)
  5291. (error (make-indirect-buffer buffer bname)))))
  5292. (defun org-set-frame-title (title)
  5293. "Set the title of the current frame to the string TITLE."
  5294. ;; FIXME: how to name a single frame in XEmacs???
  5295. (unless (featurep 'xemacs)
  5296. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5297. ;;;; Structure editing
  5298. ;;; Inserting headlines
  5299. (defun org-previous-line-empty-p ()
  5300. (save-excursion
  5301. (and (not (bobp))
  5302. (or (beginning-of-line 0) t)
  5303. (save-match-data
  5304. (looking-at "[ \t]*$")))))
  5305. (defun org-insert-heading (&optional force-heading)
  5306. "Insert a new heading or item with same depth at point.
  5307. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5308. If point is at the beginning of a headline, insert a sibling before the
  5309. current headline. If point is not at the beginning, do not split the line,
  5310. but create the new headline after the current line."
  5311. (interactive "P")
  5312. (if (= (buffer-size) 0)
  5313. (insert "\n* ")
  5314. (when (or force-heading (not (org-insert-item)))
  5315. (let* ((empty-line-p nil)
  5316. (head (save-excursion
  5317. (condition-case nil
  5318. (progn
  5319. (org-back-to-heading)
  5320. (setq empty-line-p (org-previous-line-empty-p))
  5321. (match-string 0))
  5322. (error "*"))))
  5323. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5324. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5325. pos hide-previous previous-pos)
  5326. (cond
  5327. ((and (org-on-heading-p) (bolp)
  5328. (or (bobp)
  5329. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5330. ;; insert before the current line
  5331. (open-line (if blank 2 1)))
  5332. ((and (bolp)
  5333. (not org-insert-heading-respect-content)
  5334. (or (bobp)
  5335. (save-excursion
  5336. (backward-char 1) (not (org-invisible-p)))))
  5337. ;; insert right here
  5338. nil)
  5339. (t
  5340. ;; somewhere in the line
  5341. (save-excursion
  5342. (setq previous-pos (point-at-bol))
  5343. (end-of-line)
  5344. (setq hide-previous (org-invisible-p)))
  5345. (and org-insert-heading-respect-content (org-show-subtree))
  5346. (let ((split
  5347. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5348. (save-excursion
  5349. (let ((p (point)))
  5350. (goto-char (point-at-bol))
  5351. (and (looking-at org-complex-heading-regexp)
  5352. (> p (match-beginning 4)))))))
  5353. tags pos)
  5354. (cond
  5355. (org-insert-heading-respect-content
  5356. (org-end-of-subtree nil t)
  5357. (or (bolp) (newline))
  5358. (or (org-previous-line-empty-p)
  5359. (and blank (newline)))
  5360. (open-line 1))
  5361. ((org-on-heading-p)
  5362. (when hide-previous
  5363. (show-children)
  5364. (org-show-entry))
  5365. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5366. (setq tags (and (match-end 2) (match-string 2)))
  5367. (and (match-end 1)
  5368. (delete-region (match-beginning 1) (match-end 1)))
  5369. (setq pos (point-at-bol))
  5370. (or split (end-of-line 1))
  5371. (delete-horizontal-space)
  5372. (newline (if blank 2 1))
  5373. (when tags
  5374. (save-excursion
  5375. (goto-char pos)
  5376. (end-of-line 1)
  5377. (insert " " tags)
  5378. (org-set-tags nil 'align))))
  5379. (t
  5380. (or split (end-of-line 1))
  5381. (newline (if blank 2 1)))))))
  5382. (insert head) (just-one-space)
  5383. (setq pos (point))
  5384. (end-of-line 1)
  5385. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5386. (when (and org-insert-heading-respect-content hide-previous)
  5387. (save-excursion
  5388. (goto-char previous-pos)
  5389. (hide-subtree)))
  5390. (run-hooks 'org-insert-heading-hook)))))
  5391. (defun org-get-heading (&optional no-tags)
  5392. "Return the heading of the current entry, without the stars."
  5393. (save-excursion
  5394. (org-back-to-heading t)
  5395. (if (looking-at
  5396. (if no-tags
  5397. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5398. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5399. (match-string 1) "")))
  5400. (defun org-heading-components ()
  5401. "Return the components of the current heading.
  5402. This is a list with the following elements:
  5403. - the level as an integer
  5404. - the reduced level, different if `org-odd-levels-only' is set.
  5405. - the TODO keyword, or nil
  5406. - the priority character, like ?A, or nil if no priority is given
  5407. - the headline text itself, or the tags string if no headline text
  5408. - the tags string, or nil."
  5409. (save-excursion
  5410. (org-back-to-heading t)
  5411. (if (looking-at org-complex-heading-regexp)
  5412. (list (length (match-string 1))
  5413. (org-reduced-level (length (match-string 1)))
  5414. (org-match-string-no-properties 2)
  5415. (and (match-end 3) (aref (match-string 3) 2))
  5416. (org-match-string-no-properties 4)
  5417. (org-match-string-no-properties 5)))))
  5418. (defun org-get-entry ()
  5419. "Get the entry text, after heading, entire subtree."
  5420. (save-excursion
  5421. (org-back-to-heading t)
  5422. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5423. (defun org-insert-heading-after-current ()
  5424. "Insert a new heading with same level as current, after current subtree."
  5425. (interactive)
  5426. (org-back-to-heading)
  5427. (org-insert-heading)
  5428. (org-move-subtree-down)
  5429. (end-of-line 1))
  5430. (defun org-insert-heading-respect-content ()
  5431. (interactive)
  5432. (let ((org-insert-heading-respect-content t))
  5433. (org-insert-heading t)))
  5434. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5435. (interactive "P")
  5436. (let ((org-insert-heading-respect-content t))
  5437. (org-insert-todo-heading force-state t)))
  5438. (defun org-insert-todo-heading (arg &optional force-heading)
  5439. "Insert a new heading with the same level and TODO state as current heading.
  5440. If the heading has no TODO state, or if the state is DONE, use the first
  5441. state (TODO by default). Also with prefix arg, force first state."
  5442. (interactive "P")
  5443. (when (or force-heading (not (org-insert-item 'checkbox)))
  5444. (org-insert-heading force-heading)
  5445. (save-excursion
  5446. (org-back-to-heading)
  5447. (outline-previous-heading)
  5448. (looking-at org-todo-line-regexp))
  5449. (let*
  5450. ((new-mark-x
  5451. (if (or arg
  5452. (not (match-beginning 2))
  5453. (member (match-string 2) org-done-keywords))
  5454. (car org-todo-keywords-1)
  5455. (match-string 2)))
  5456. (new-mark
  5457. (or
  5458. (run-hook-with-args-until-success
  5459. 'org-todo-get-default-hook new-mark-x nil)
  5460. new-mark-x)))
  5461. (beginning-of-line 1)
  5462. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5463. (if org-treat-insert-todo-heading-as-state-change
  5464. (org-todo new-mark)
  5465. (insert new-mark " "))))
  5466. (when org-provide-todo-statistics
  5467. (org-update-parent-todo-statistics))))
  5468. (defun org-insert-subheading (arg)
  5469. "Insert a new subheading and demote it.
  5470. Works for outline headings and for plain lists alike."
  5471. (interactive "P")
  5472. (org-insert-heading arg)
  5473. (cond
  5474. ((org-on-heading-p) (org-do-demote))
  5475. ((org-at-item-p) (org-indent-item 1))))
  5476. (defun org-insert-todo-subheading (arg)
  5477. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5478. Works for outline headings and for plain lists alike."
  5479. (interactive "P")
  5480. (org-insert-todo-heading arg)
  5481. (cond
  5482. ((org-on-heading-p) (org-do-demote))
  5483. ((org-at-item-p) (org-indent-item 1))))
  5484. ;;; Promotion and Demotion
  5485. (defvar org-after-demote-entry-hook nil
  5486. "Hook run after an entry has been demoted.
  5487. The cursor will be at the beginning of the entry.
  5488. When a subtree is being demoted, the hook will be called for each node.")
  5489. (defvar org-after-promote-entry-hook nil
  5490. "Hook run after an entry has been promoted.
  5491. The cursor will be at the beginning of the entry.
  5492. When a subtree is being promoted, the hook will be called for each node.")
  5493. (defun org-promote-subtree ()
  5494. "Promote the entire subtree.
  5495. See also `org-promote'."
  5496. (interactive)
  5497. (save-excursion
  5498. (org-map-tree 'org-promote))
  5499. (org-fix-position-after-promote))
  5500. (defun org-demote-subtree ()
  5501. "Demote the entire subtree. See `org-demote'.
  5502. See also `org-promote'."
  5503. (interactive)
  5504. (save-excursion
  5505. (org-map-tree 'org-demote))
  5506. (org-fix-position-after-promote))
  5507. (defun org-do-promote ()
  5508. "Promote the current heading higher up the tree.
  5509. If the region is active in `transient-mark-mode', promote all headings
  5510. in the region."
  5511. (interactive)
  5512. (save-excursion
  5513. (if (org-region-active-p)
  5514. (org-map-region 'org-promote (region-beginning) (region-end))
  5515. (org-promote)))
  5516. (org-fix-position-after-promote))
  5517. (defun org-do-demote ()
  5518. "Demote the current heading lower down the tree.
  5519. If the region is active in `transient-mark-mode', demote all headings
  5520. in the region."
  5521. (interactive)
  5522. (save-excursion
  5523. (if (org-region-active-p)
  5524. (org-map-region 'org-demote (region-beginning) (region-end))
  5525. (org-demote)))
  5526. (org-fix-position-after-promote))
  5527. (defun org-fix-position-after-promote ()
  5528. "Make sure that after pro/demotion cursor position is right."
  5529. (let ((pos (point)))
  5530. (when (save-excursion
  5531. (beginning-of-line 1)
  5532. (looking-at org-todo-line-regexp)
  5533. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5534. (cond ((eobp) (insert " "))
  5535. ((eolp) (insert " "))
  5536. ((equal (char-after) ?\ ) (forward-char 1))))))
  5537. (defun org-current-level ()
  5538. "Return the level of the current entry, or nil if before the first headline.
  5539. The level is the number of stars at the beginning of the headline."
  5540. (save-excursion
  5541. (condition-case nil
  5542. (progn
  5543. (org-back-to-heading t)
  5544. (funcall outline-level))
  5545. (error nil))))
  5546. (defun org-reduced-level (l)
  5547. "Compute the effective level of a heading.
  5548. This takes into account the setting of `org-odd-levels-only'."
  5549. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5550. (defun org-get-valid-level (level &optional change)
  5551. "Rectify a level change under the influence of `org-odd-levels-only'
  5552. LEVEL is a current level, CHANGE is by how much the level should be
  5553. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5554. even level numbers will become the next higher odd number."
  5555. (if org-odd-levels-only
  5556. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5557. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5558. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5559. (max 1 (+ level (or change 0)))))
  5560. (if (boundp 'define-obsolete-function-alias)
  5561. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5562. (define-obsolete-function-alias 'org-get-legal-level
  5563. 'org-get-valid-level)
  5564. (define-obsolete-function-alias 'org-get-legal-level
  5565. 'org-get-valid-level "23.1")))
  5566. (defun org-promote ()
  5567. "Promote the current heading higher up the tree.
  5568. If the region is active in `transient-mark-mode', promote all headings
  5569. in the region."
  5570. (org-back-to-heading t)
  5571. (let* ((level (save-match-data (funcall outline-level)))
  5572. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5573. (diff (abs (- level (length up-head) -1))))
  5574. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5575. (replace-match up-head nil t)
  5576. ;; Fixup tag positioning
  5577. (and org-auto-align-tags (org-set-tags nil t))
  5578. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5579. (run-hooks 'org-after-promote-entry-hook)))
  5580. (defun org-demote ()
  5581. "Demote the current heading lower down the tree.
  5582. If the region is active in `transient-mark-mode', demote all headings
  5583. in the region."
  5584. (org-back-to-heading t)
  5585. (let* ((level (save-match-data (funcall outline-level)))
  5586. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5587. (diff (abs (- level (length down-head) -1))))
  5588. (replace-match down-head nil t)
  5589. ;; Fixup tag positioning
  5590. (and org-auto-align-tags (org-set-tags nil t))
  5591. (if org-adapt-indentation (org-fixup-indentation diff))
  5592. (run-hooks 'org-after-demote-entry-hook)))
  5593. (defvar org-tab-ind-state nil)
  5594. (defun org-cycle-level ()
  5595. (let ((org-adapt-indentation nil))
  5596. (when (and (looking-at "[ \t]*$")
  5597. (looking-back
  5598. (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp "\\)?[ \t]*")))
  5599. (setq this-command 'org-cycle-level)
  5600. (if (eq last-command 'org-cycle-level)
  5601. (condition-case nil
  5602. (progn (org-do-promote)
  5603. (if (equal org-tab-ind-state (org-current-level))
  5604. (org-do-promote)))
  5605. (error
  5606. (progn
  5607. (save-excursion
  5608. (beginning-of-line 1)
  5609. (and (looking-at "\\*+")
  5610. (replace-match
  5611. (make-string org-tab-ind-state ?*))))
  5612. (setq this-command 'org-cycle))))
  5613. (setq org-tab-ind-state (- (match-end 1) (match-beginning 1)))
  5614. (org-do-demote))
  5615. t)))
  5616. (defun org-map-tree (fun)
  5617. "Call FUN for every heading underneath the current one."
  5618. (org-back-to-heading)
  5619. (let ((level (funcall outline-level)))
  5620. (save-excursion
  5621. (funcall fun)
  5622. (while (and (progn
  5623. (outline-next-heading)
  5624. (> (funcall outline-level) level))
  5625. (not (eobp)))
  5626. (funcall fun)))))
  5627. (defun org-map-region (fun beg end)
  5628. "Call FUN for every heading between BEG and END."
  5629. (let ((org-ignore-region t))
  5630. (save-excursion
  5631. (setq end (copy-marker end))
  5632. (goto-char beg)
  5633. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5634. (< (point) end))
  5635. (funcall fun))
  5636. (while (and (progn
  5637. (outline-next-heading)
  5638. (< (point) end))
  5639. (not (eobp)))
  5640. (funcall fun)))))
  5641. (defun org-fixup-indentation (diff)
  5642. "Change the indentation in the current entry by DIFF
  5643. However, if any line in the current entry has no indentation, or if it
  5644. would end up with no indentation after the change, nothing at all is done."
  5645. (save-excursion
  5646. (let ((end (save-excursion (outline-next-heading)
  5647. (point-marker)))
  5648. (prohibit (if (> diff 0)
  5649. "^\\S-"
  5650. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5651. col)
  5652. (unless (save-excursion (end-of-line 1)
  5653. (re-search-forward prohibit end t))
  5654. (while (and (< (point) end)
  5655. (re-search-forward "^[ \t]+" end t))
  5656. (goto-char (match-end 0))
  5657. (setq col (current-column))
  5658. (if (< diff 0) (replace-match ""))
  5659. (org-indent-to-column (+ diff col))))
  5660. (move-marker end nil))))
  5661. (defun org-convert-to-odd-levels ()
  5662. "Convert an org-mode file with all levels allowed to one with odd levels.
  5663. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5664. level 5 etc."
  5665. (interactive)
  5666. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5667. (let ((outline-regexp org-outline-regexp)
  5668. (outline-level 'org-outline-level)
  5669. (org-odd-levels-only nil) n)
  5670. (save-excursion
  5671. (goto-char (point-min))
  5672. (while (re-search-forward "^\\*\\*+ " nil t)
  5673. (setq n (- (length (match-string 0)) 2))
  5674. (while (>= (setq n (1- n)) 0)
  5675. (org-demote))
  5676. (end-of-line 1))))))
  5677. (defun org-convert-to-oddeven-levels ()
  5678. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5679. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5680. section with an even level, conversion would destroy the structure of the file. An error
  5681. is signaled in this case."
  5682. (interactive)
  5683. (goto-char (point-min))
  5684. ;; First check if there are no even levels
  5685. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5686. (org-show-context t)
  5687. (error "Not all levels are odd in this file. Conversion not possible"))
  5688. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5689. (let ((outline-regexp org-outline-regexp)
  5690. (outline-level 'org-outline-level)
  5691. (org-odd-levels-only nil) n)
  5692. (save-excursion
  5693. (goto-char (point-min))
  5694. (while (re-search-forward "^\\*\\*+ " nil t)
  5695. (setq n (/ (1- (length (match-string 0))) 2))
  5696. (while (>= (setq n (1- n)) 0)
  5697. (org-promote))
  5698. (end-of-line 1))))))
  5699. (defun org-tr-level (n)
  5700. "Make N odd if required."
  5701. (if org-odd-levels-only (1+ (/ n 2)) n))
  5702. ;;; Vertical tree motion, cutting and pasting of subtrees
  5703. (defun org-move-subtree-up (&optional arg)
  5704. "Move the current subtree up past ARG headlines of the same level."
  5705. (interactive "p")
  5706. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5707. (defun org-move-subtree-down (&optional arg)
  5708. "Move the current subtree down past ARG headlines of the same level."
  5709. (interactive "p")
  5710. (setq arg (prefix-numeric-value arg))
  5711. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  5712. 'org-get-last-sibling))
  5713. (ins-point (make-marker))
  5714. (cnt (abs arg))
  5715. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5716. ;; Select the tree
  5717. (org-back-to-heading)
  5718. (setq beg0 (point))
  5719. (save-excursion
  5720. (setq ne-beg (org-back-over-empty-lines))
  5721. (setq beg (point)))
  5722. (save-match-data
  5723. (save-excursion (outline-end-of-heading)
  5724. (setq folded (org-invisible-p)))
  5725. (outline-end-of-subtree))
  5726. (outline-next-heading)
  5727. (setq ne-end (org-back-over-empty-lines))
  5728. (setq end (point))
  5729. (goto-char beg0)
  5730. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5731. ;; include less whitespace
  5732. (save-excursion
  5733. (goto-char beg)
  5734. (forward-line (- ne-beg ne-end))
  5735. (setq beg (point))))
  5736. ;; Find insertion point, with error handling
  5737. (while (> cnt 0)
  5738. (or (and (funcall movfunc) (looking-at outline-regexp))
  5739. (progn (goto-char beg0)
  5740. (error "Cannot move past superior level or buffer limit")))
  5741. (setq cnt (1- cnt)))
  5742. (if (> arg 0)
  5743. ;; Moving forward - still need to move over subtree
  5744. (progn (org-end-of-subtree t t)
  5745. (save-excursion
  5746. (org-back-over-empty-lines)
  5747. (or (bolp) (newline)))))
  5748. (setq ne-ins (org-back-over-empty-lines))
  5749. (move-marker ins-point (point))
  5750. (setq txt (buffer-substring beg end))
  5751. (org-save-markers-in-region beg end)
  5752. (delete-region beg end)
  5753. (org-remove-empty-overlays-at beg)
  5754. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5755. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5756. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5757. (let ((bbb (point)))
  5758. (insert-before-markers txt)
  5759. (org-reinstall-markers-in-region bbb)
  5760. (move-marker ins-point bbb))
  5761. (or (bolp) (insert "\n"))
  5762. (setq ins-end (point))
  5763. (goto-char ins-point)
  5764. (org-skip-whitespace)
  5765. (when (and (< arg 0)
  5766. (org-first-sibling-p)
  5767. (> ne-ins ne-beg))
  5768. ;; Move whitespace back to beginning
  5769. (save-excursion
  5770. (goto-char ins-end)
  5771. (let ((kill-whole-line t))
  5772. (kill-line (- ne-ins ne-beg)) (point)))
  5773. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5774. (move-marker ins-point nil)
  5775. (if folded
  5776. (hide-subtree)
  5777. (org-show-entry)
  5778. (show-children)
  5779. (org-cycle-hide-drawers 'children))
  5780. (org-clean-visibility-after-subtree-move)))
  5781. (defvar org-subtree-clip ""
  5782. "Clipboard for cut and paste of subtrees.
  5783. This is actually only a copy of the kill, because we use the normal kill
  5784. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5785. (defvar org-subtree-clip-folded nil
  5786. "Was the last copied subtree folded?
  5787. This is used to fold the tree back after pasting.")
  5788. (defun org-cut-subtree (&optional n)
  5789. "Cut the current subtree into the clipboard.
  5790. With prefix arg N, cut this many sequential subtrees.
  5791. This is a short-hand for marking the subtree and then cutting it."
  5792. (interactive "p")
  5793. (org-copy-subtree n 'cut))
  5794. (defun org-copy-subtree (&optional n cut force-store-markers)
  5795. "Cut the current subtree into the clipboard.
  5796. With prefix arg N, cut this many sequential subtrees.
  5797. This is a short-hand for marking the subtree and then copying it.
  5798. If CUT is non-nil, actually cut the subtree.
  5799. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5800. of some markers in the region, even if CUT is non-nil. This is
  5801. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5802. (interactive "p")
  5803. (let (beg end folded (beg0 (point)))
  5804. (if (interactive-p)
  5805. (org-back-to-heading nil) ; take what looks like a subtree
  5806. (org-back-to-heading t)) ; take what is really there
  5807. (org-back-over-empty-lines)
  5808. (setq beg (point))
  5809. (skip-chars-forward " \t\r\n")
  5810. (save-match-data
  5811. (save-excursion (outline-end-of-heading)
  5812. (setq folded (org-invisible-p)))
  5813. (condition-case nil
  5814. (org-forward-same-level (1- n) t)
  5815. (error nil))
  5816. (org-end-of-subtree t t))
  5817. (org-back-over-empty-lines)
  5818. (setq end (point))
  5819. (goto-char beg0)
  5820. (when (> end beg)
  5821. (setq org-subtree-clip-folded folded)
  5822. (when (or cut force-store-markers)
  5823. (org-save-markers-in-region beg end))
  5824. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5825. (setq org-subtree-clip (current-kill 0))
  5826. (message "%s: Subtree(s) with %d characters"
  5827. (if cut "Cut" "Copied")
  5828. (length org-subtree-clip)))))
  5829. (defun org-paste-subtree (&optional level tree for-yank)
  5830. "Paste the clipboard as a subtree, with modification of headline level.
  5831. The entire subtree is promoted or demoted in order to match a new headline
  5832. level.
  5833. If the cursor is at the beginning of a headline, the same level as
  5834. that headline is used to paste the tree
  5835. If not, the new level is derived from the *visible* headings
  5836. before and after the insertion point, and taken to be the inferior headline
  5837. level of the two. So if the previous visible heading is level 3 and the
  5838. next is level 4 (or vice versa), level 4 will be used for insertion.
  5839. This makes sure that the subtree remains an independent subtree and does
  5840. not swallow low level entries.
  5841. You can also force a different level, either by using a numeric prefix
  5842. argument, or by inserting the heading marker by hand. For example, if the
  5843. cursor is after \"*****\", then the tree will be shifted to level 5.
  5844. If optional TREE is given, use this text instead of the kill ring.
  5845. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5846. move back over whitespace before inserting, and move point to the end of
  5847. the inserted text when done."
  5848. (interactive "P")
  5849. (setq tree (or tree (and kill-ring (current-kill 0))))
  5850. (unless (org-kill-is-subtree-p tree)
  5851. (error "%s"
  5852. (substitute-command-keys
  5853. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5854. (let* ((visp (not (org-invisible-p)))
  5855. (txt tree)
  5856. (^re (concat "^\\(" outline-regexp "\\)"))
  5857. (re (concat "\\(" outline-regexp "\\)"))
  5858. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5859. (old-level (if (string-match ^re txt)
  5860. (- (match-end 0) (match-beginning 0) 1)
  5861. -1))
  5862. (force-level (cond (level (prefix-numeric-value level))
  5863. ((and (looking-at "[ \t]*$")
  5864. (string-match
  5865. ^re_ (buffer-substring
  5866. (point-at-bol) (point))))
  5867. (- (match-end 1) (match-beginning 1)))
  5868. ((and (bolp)
  5869. (looking-at org-outline-regexp))
  5870. (- (match-end 0) (point) 1))
  5871. (t nil)))
  5872. (previous-level (save-excursion
  5873. (condition-case nil
  5874. (progn
  5875. (outline-previous-visible-heading 1)
  5876. (if (looking-at re)
  5877. (- (match-end 0) (match-beginning 0) 1)
  5878. 1))
  5879. (error 1))))
  5880. (next-level (save-excursion
  5881. (condition-case nil
  5882. (progn
  5883. (or (looking-at outline-regexp)
  5884. (outline-next-visible-heading 1))
  5885. (if (looking-at re)
  5886. (- (match-end 0) (match-beginning 0) 1)
  5887. 1))
  5888. (error 1))))
  5889. (new-level (or force-level (max previous-level next-level)))
  5890. (shift (if (or (= old-level -1)
  5891. (= new-level -1)
  5892. (= old-level new-level))
  5893. 0
  5894. (- new-level old-level)))
  5895. (delta (if (> shift 0) -1 1))
  5896. (func (if (> shift 0) 'org-demote 'org-promote))
  5897. (org-odd-levels-only nil)
  5898. beg end newend)
  5899. ;; Remove the forced level indicator
  5900. (if force-level
  5901. (delete-region (point-at-bol) (point)))
  5902. ;; Paste
  5903. (beginning-of-line 1)
  5904. (unless for-yank (org-back-over-empty-lines))
  5905. (setq beg (point))
  5906. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5907. (insert-before-markers txt)
  5908. (unless (string-match "\n\\'" txt) (insert "\n"))
  5909. (setq newend (point))
  5910. (org-reinstall-markers-in-region beg)
  5911. (setq end (point))
  5912. (goto-char beg)
  5913. (skip-chars-forward " \t\n\r")
  5914. (setq beg (point))
  5915. (if (and (org-invisible-p) visp)
  5916. (save-excursion (outline-show-heading)))
  5917. ;; Shift if necessary
  5918. (unless (= shift 0)
  5919. (save-restriction
  5920. (narrow-to-region beg end)
  5921. (while (not (= shift 0))
  5922. (org-map-region func (point-min) (point-max))
  5923. (setq shift (+ delta shift)))
  5924. (goto-char (point-min))
  5925. (setq newend (point-max))))
  5926. (when (or (interactive-p) for-yank)
  5927. (message "Clipboard pasted as level %d subtree" new-level))
  5928. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5929. kill-ring
  5930. (eq org-subtree-clip (current-kill 0))
  5931. org-subtree-clip-folded)
  5932. ;; The tree was folded before it was killed/copied
  5933. (hide-subtree))
  5934. (and for-yank (goto-char newend))))
  5935. (defun org-kill-is-subtree-p (&optional txt)
  5936. "Check if the current kill is an outline subtree, or a set of trees.
  5937. Returns nil if kill does not start with a headline, or if the first
  5938. headline level is not the largest headline level in the tree.
  5939. So this will actually accept several entries of equal levels as well,
  5940. which is OK for `org-paste-subtree'.
  5941. If optional TXT is given, check this string instead of the current kill."
  5942. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5943. (start-level (and kill
  5944. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5945. org-outline-regexp "\\)")
  5946. kill)
  5947. (- (match-end 2) (match-beginning 2) 1)))
  5948. (re (concat "^" org-outline-regexp))
  5949. (start (1+ (or (match-beginning 2) -1))))
  5950. (if (not start-level)
  5951. (progn
  5952. nil) ;; does not even start with a heading
  5953. (catch 'exit
  5954. (while (setq start (string-match re kill (1+ start)))
  5955. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5956. (throw 'exit nil)))
  5957. t))))
  5958. (defvar org-markers-to-move nil
  5959. "Markers that should be moved with a cut-and-paste operation.
  5960. Those markers are stored together with their positions relative to
  5961. the start of the region.")
  5962. (defun org-save-markers-in-region (beg end)
  5963. "Check markers in region.
  5964. If these markers are between BEG and END, record their position relative
  5965. to BEG, so that after moving the block of text, we can put the markers back
  5966. into place.
  5967. This function gets called just before an entry or tree gets cut from the
  5968. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5969. called immediately, to move the markers with the entries."
  5970. (setq org-markers-to-move nil)
  5971. (when (featurep 'org-clock)
  5972. (org-clock-save-markers-for-cut-and-paste beg end))
  5973. (when (featurep 'org-agenda)
  5974. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5975. (defun org-check-and-save-marker (marker beg end)
  5976. "Check if MARKER is between BEG and END.
  5977. If yes, remember the marker and the distance to BEG."
  5978. (when (and (marker-buffer marker)
  5979. (equal (marker-buffer marker) (current-buffer)))
  5980. (if (and (>= marker beg) (< marker end))
  5981. (push (cons marker (- marker beg)) org-markers-to-move))))
  5982. (defun org-reinstall-markers-in-region (beg)
  5983. "Move all remembered markers to their position relative to BEG."
  5984. (mapc (lambda (x)
  5985. (move-marker (car x) (+ beg (cdr x))))
  5986. org-markers-to-move)
  5987. (setq org-markers-to-move nil))
  5988. (defun org-narrow-to-subtree ()
  5989. "Narrow buffer to the current subtree."
  5990. (interactive)
  5991. (save-excursion
  5992. (save-match-data
  5993. (narrow-to-region
  5994. (progn (org-back-to-heading t) (point))
  5995. (progn (org-end-of-subtree t t) (point))))))
  5996. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5997. "Clone the task (subtree) at point N times.
  5998. The clones will be inserted as siblings.
  5999. In interactive use, the user will be prompted for the number of clones
  6000. to be produced, and for a time SHIFT, which may be a repeater as used
  6001. in time stamps, for example `+3d'.
  6002. When a valid repeater is given and the entry contains any time stamps,
  6003. the clones will become a sequence in time, with time stamps in the
  6004. subtree shifted for each clone produced. If SHIFT is nil or the
  6005. empty string, time stamps will be left alone.
  6006. If the original subtree did contain time stamps with a repeater,
  6007. the following will happen:
  6008. - the repeater will be removed in each clone
  6009. - an additional clone will be produced, with the current, unshifted
  6010. date(s) in the entry.
  6011. - the original entry will be placed *after* all the clones, with
  6012. repeater intact.
  6013. - the start days in the repeater in the original entry will be shifted
  6014. to past the last clone.
  6015. I this way you can spell out a number of instances of a repeating task,
  6016. and still retain the repeater to cover future instances of the task."
  6017. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6018. (let (beg end template task
  6019. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6020. (if (not (and (integerp n) (> n 0)))
  6021. (error "Invalid number of replications %s" n))
  6022. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6023. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6024. shift)))
  6025. (error "Invalid shift specification %s" shift))
  6026. (when doshift
  6027. (setq shift-n (string-to-number (match-string 1 shift))
  6028. shift-what (cdr (assoc (match-string 2 shift)
  6029. '(("d" . day) ("w" . week)
  6030. ("m" . month) ("y" . year))))))
  6031. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6032. (setq nmin 1 nmax n)
  6033. (org-back-to-heading t)
  6034. (setq beg (point))
  6035. (org-end-of-subtree t t)
  6036. (setq end (point))
  6037. (setq template (buffer-substring beg end))
  6038. (when (and doshift
  6039. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6040. (delete-region beg end)
  6041. (setq end beg)
  6042. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6043. (goto-char end)
  6044. (loop for n from nmin to nmax do
  6045. (if (not doshift)
  6046. (setq task template)
  6047. (with-temp-buffer
  6048. (insert template)
  6049. (org-mode)
  6050. (goto-char (point-min))
  6051. (while (re-search-forward org-ts-regexp-both nil t)
  6052. (org-timestamp-change (* n shift-n) shift-what))
  6053. (unless (= n n-no-remove)
  6054. (goto-char (point-min))
  6055. (while (re-search-forward org-ts-regexp nil t)
  6056. (save-excursion
  6057. (goto-char (match-beginning 0))
  6058. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6059. (delete-region (match-beginning 1) (match-end 1))))))
  6060. (setq task (buffer-string))))
  6061. (insert task))
  6062. (goto-char beg)))
  6063. ;;; Outline Sorting
  6064. (defun org-sort (with-case)
  6065. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6066. Optional argument WITH-CASE means sort case-sensitively.
  6067. With a double prefix argument, also remove duplicate entries."
  6068. (interactive "P")
  6069. (if (org-at-table-p)
  6070. (org-call-with-arg 'org-table-sort-lines with-case)
  6071. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6072. (defun org-sort-remove-invisible (s)
  6073. (remove-text-properties 0 (length s) org-rm-props s)
  6074. (while (string-match org-bracket-link-regexp s)
  6075. (setq s (replace-match (if (match-end 2)
  6076. (match-string 3 s)
  6077. (match-string 1 s)) t t s)))
  6078. s)
  6079. (defvar org-priority-regexp) ; defined later in the file
  6080. (defvar org-after-sorting-entries-or-items-hook nil
  6081. "Hook that is run after a bunch of entries or items have been sorted.
  6082. When children are sorted, the cursor is in the parent line when this
  6083. hook gets called. When a region or a plain list is sorted, the cursor
  6084. will be in the first entry of the sorted region/list.")
  6085. (defun org-sort-entries-or-items
  6086. (&optional with-case sorting-type getkey-func compare-func property)
  6087. "Sort entries on a certain level of an outline tree, or plain list items.
  6088. If there is an active region, the entries in the region are sorted.
  6089. Else, if the cursor is before the first entry, sort the top-level items.
  6090. Else, the children of the entry at point are sorted.
  6091. If the cursor is at the first item in a plain list, the list items will be
  6092. sorted.
  6093. Sorting can be alphabetically, numerically, by date/time as given by
  6094. a time stamp, by a property or by priority.
  6095. The command prompts for the sorting type unless it has been given to the
  6096. function through the SORTING-TYPE argument, which needs to a character,
  6097. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6098. precise meaning of each character:
  6099. n Numerically, by converting the beginning of the entry/item to a number.
  6100. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6101. t By date/time, either the first active time stamp in the entry, or, if
  6102. none exist, by the first inactive one.
  6103. In items, only the first line will be chekced.
  6104. s By the scheduled date/time.
  6105. d By deadline date/time.
  6106. c By creation time, which is assumed to be the first inactive time stamp
  6107. at the beginning of a line.
  6108. p By priority according to the cookie.
  6109. r By the value of a property.
  6110. Capital letters will reverse the sort order.
  6111. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6112. called with point at the beginning of the record. It must return either
  6113. a string or a number that should serve as the sorting key for that record.
  6114. Comparing entries ignores case by default. However, with an optional argument
  6115. WITH-CASE, the sorting considers case as well."
  6116. (interactive "P")
  6117. (let ((case-func (if with-case 'identity 'downcase))
  6118. start beg end stars re re2
  6119. txt what tmp plain-list-p)
  6120. ;; Find beginning and end of region to sort
  6121. (cond
  6122. ((org-region-active-p)
  6123. ;; we will sort the region
  6124. (setq end (region-end)
  6125. what "region")
  6126. (goto-char (region-beginning))
  6127. (if (not (org-on-heading-p)) (outline-next-heading))
  6128. (setq start (point)))
  6129. ((org-at-item-p)
  6130. ;; we will sort this plain list
  6131. (org-beginning-of-item-list) (setq start (point))
  6132. (org-end-of-item-list) (setq end (point))
  6133. (goto-char start)
  6134. (setq plain-list-p t
  6135. what "plain list"))
  6136. ((or (org-on-heading-p)
  6137. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6138. ;; we will sort the children of the current headline
  6139. (org-back-to-heading)
  6140. (setq start (point)
  6141. end (progn (org-end-of-subtree t t)
  6142. (org-back-over-empty-lines)
  6143. (point))
  6144. what "children")
  6145. (goto-char start)
  6146. (show-subtree)
  6147. (outline-next-heading))
  6148. (t
  6149. ;; we will sort the top-level entries in this file
  6150. (goto-char (point-min))
  6151. (or (org-on-heading-p) (outline-next-heading))
  6152. (setq start (point) end (point-max) what "top-level")
  6153. (goto-char start)
  6154. (show-all)))
  6155. (setq beg (point))
  6156. (if (>= beg end) (error "Nothing to sort"))
  6157. (unless plain-list-p
  6158. (looking-at "\\(\\*+\\)")
  6159. (setq stars (match-string 1)
  6160. re (concat "^" (regexp-quote stars) " +")
  6161. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6162. txt (buffer-substring beg end))
  6163. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6164. (if (and (not (equal stars "*")) (string-match re2 txt))
  6165. (error "Region to sort contains a level above the first entry")))
  6166. (unless sorting-type
  6167. (message
  6168. (if plain-list-p
  6169. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6170. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6171. [t]ime [s]cheduled [d]eadline [c]reated
  6172. A/N/T/S/D/C/P/O/F means reversed:")
  6173. what)
  6174. (setq sorting-type (read-char-exclusive))
  6175. (and (= (downcase sorting-type) ?f)
  6176. (setq getkey-func
  6177. (org-icompleting-read "Sort using function: "
  6178. obarray 'fboundp t nil nil))
  6179. (setq getkey-func (intern getkey-func)))
  6180. (and (= (downcase sorting-type) ?r)
  6181. (setq property
  6182. (org-icompleting-read "Property: "
  6183. (mapcar 'list (org-buffer-property-keys t))
  6184. nil t))))
  6185. (message "Sorting entries...")
  6186. (save-restriction
  6187. (narrow-to-region start end)
  6188. (let ((dcst (downcase sorting-type))
  6189. (case-fold-search nil)
  6190. (now (current-time)))
  6191. (sort-subr
  6192. (/= dcst sorting-type)
  6193. ;; This function moves to the beginning character of the "record" to
  6194. ;; be sorted.
  6195. (if plain-list-p
  6196. (lambda nil
  6197. (if (org-at-item-p) t (goto-char (point-max))))
  6198. (lambda nil
  6199. (if (re-search-forward re nil t)
  6200. (goto-char (match-beginning 0))
  6201. (goto-char (point-max)))))
  6202. ;; This function moves to the last character of the "record" being
  6203. ;; sorted.
  6204. (if plain-list-p
  6205. 'org-end-of-item
  6206. (lambda nil
  6207. (save-match-data
  6208. (condition-case nil
  6209. (outline-forward-same-level 1)
  6210. (error
  6211. (goto-char (point-max)))))))
  6212. ;; This function returns the value that gets sorted against.
  6213. (if plain-list-p
  6214. (lambda nil
  6215. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6216. (cond
  6217. ((= dcst ?n)
  6218. (string-to-number (buffer-substring (match-end 0)
  6219. (point-at-eol))))
  6220. ((= dcst ?a)
  6221. (buffer-substring (match-end 0) (point-at-eol)))
  6222. ((= dcst ?t)
  6223. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  6224. (re-search-forward org-ts-regexp-both
  6225. (point-at-eol) t))
  6226. (org-time-string-to-seconds (match-string 0))
  6227. (org-float-time now)))
  6228. ((= dcst ?f)
  6229. (if getkey-func
  6230. (progn
  6231. (setq tmp (funcall getkey-func))
  6232. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6233. tmp)
  6234. (error "Invalid key function `%s'" getkey-func)))
  6235. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6236. (lambda nil
  6237. (cond
  6238. ((= dcst ?n)
  6239. (if (looking-at org-complex-heading-regexp)
  6240. (string-to-number (match-string 4))
  6241. nil))
  6242. ((= dcst ?a)
  6243. (if (looking-at org-complex-heading-regexp)
  6244. (funcall case-func (match-string 4))
  6245. nil))
  6246. ((= dcst ?t)
  6247. (let ((end (save-excursion (outline-next-heading) (point))))
  6248. (if (or (re-search-forward org-ts-regexp end t)
  6249. (re-search-forward org-ts-regexp-both end t))
  6250. (org-time-string-to-seconds (match-string 0))
  6251. (org-float-time now))))
  6252. ((= dcst ?c)
  6253. (let ((end (save-excursion (outline-next-heading) (point))))
  6254. (if (re-search-forward
  6255. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6256. end t)
  6257. (org-time-string-to-seconds (match-string 0))
  6258. (org-float-time now))))
  6259. ((= dcst ?s)
  6260. (let ((end (save-excursion (outline-next-heading) (point))))
  6261. (if (re-search-forward org-scheduled-time-regexp end t)
  6262. (org-time-string-to-seconds (match-string 1))
  6263. (org-float-time now))))
  6264. ((= dcst ?d)
  6265. (let ((end (save-excursion (outline-next-heading) (point))))
  6266. (if (re-search-forward org-deadline-time-regexp end t)
  6267. (org-time-string-to-seconds (match-string 1))
  6268. (org-float-time now))))
  6269. ((= dcst ?p)
  6270. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6271. (string-to-char (match-string 2))
  6272. org-default-priority))
  6273. ((= dcst ?r)
  6274. (or (org-entry-get nil property) ""))
  6275. ((= dcst ?o)
  6276. (if (looking-at org-complex-heading-regexp)
  6277. (- 9999 (length (member (match-string 2)
  6278. org-todo-keywords-1)))))
  6279. ((= dcst ?f)
  6280. (if getkey-func
  6281. (progn
  6282. (setq tmp (funcall getkey-func))
  6283. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6284. tmp)
  6285. (error "Invalid key function `%s'" getkey-func)))
  6286. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6287. nil
  6288. (cond
  6289. ((= dcst ?a) 'string<)
  6290. ((= dcst ?f) compare-func)
  6291. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6292. (t nil)))))
  6293. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6294. (message "Sorting entries...done")))
  6295. (defun org-do-sort (table what &optional with-case sorting-type)
  6296. "Sort TABLE of WHAT according to SORTING-TYPE.
  6297. The user will be prompted for the SORTING-TYPE if the call to this
  6298. function does not specify it. WHAT is only for the prompt, to indicate
  6299. what is being sorted. The sorting key will be extracted from
  6300. the car of the elements of the table.
  6301. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6302. (unless sorting-type
  6303. (message
  6304. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6305. what)
  6306. (setq sorting-type (read-char-exclusive)))
  6307. (let ((dcst (downcase sorting-type))
  6308. extractfun comparefun)
  6309. ;; Define the appropriate functions
  6310. (cond
  6311. ((= dcst ?n)
  6312. (setq extractfun 'string-to-number
  6313. comparefun (if (= dcst sorting-type) '< '>)))
  6314. ((= dcst ?a)
  6315. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6316. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6317. comparefun (if (= dcst sorting-type)
  6318. 'string<
  6319. (lambda (a b) (and (not (string< a b))
  6320. (not (string= a b)))))))
  6321. ((= dcst ?t)
  6322. (setq extractfun
  6323. (lambda (x)
  6324. (if (or (string-match org-ts-regexp x)
  6325. (string-match org-ts-regexp-both x))
  6326. (org-float-time
  6327. (org-time-string-to-time (match-string 0 x)))
  6328. 0))
  6329. comparefun (if (= dcst sorting-type) '< '>)))
  6330. (t (error "Invalid sorting type `%c'" sorting-type)))
  6331. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6332. table)
  6333. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6334. ;;; The orgstruct minor mode
  6335. ;; Define a minor mode which can be used in other modes in order to
  6336. ;; integrate the org-mode structure editing commands.
  6337. ;; This is really a hack, because the org-mode structure commands use
  6338. ;; keys which normally belong to the major mode. Here is how it
  6339. ;; works: The minor mode defines all the keys necessary to operate the
  6340. ;; structure commands, but wraps the commands into a function which
  6341. ;; tests if the cursor is currently at a headline or a plain list
  6342. ;; item. If that is the case, the structure command is used,
  6343. ;; temporarily setting many Org-mode variables like regular
  6344. ;; expressions for filling etc. However, when any of those keys is
  6345. ;; used at a different location, function uses `key-binding' to look
  6346. ;; up if the key has an associated command in another currently active
  6347. ;; keymap (minor modes, major mode, global), and executes that
  6348. ;; command. There might be problems if any of the keys is otherwise
  6349. ;; used as a prefix key.
  6350. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6351. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6352. ;; addresses this by checking explicitly for both bindings.
  6353. (defvar orgstruct-mode-map (make-sparse-keymap)
  6354. "Keymap for the minor `orgstruct-mode'.")
  6355. (defvar org-local-vars nil
  6356. "List of local variables, for use by `orgstruct-mode'")
  6357. ;;;###autoload
  6358. (define-minor-mode orgstruct-mode
  6359. "Toggle the minor more `orgstruct-mode'.
  6360. This mode is for using Org-mode structure commands in other modes.
  6361. The following key behave as if Org-mode was active, if the cursor
  6362. is on a headline, or on a plain list item (both in the definition
  6363. of Org-mode).
  6364. M-up Move entry/item up
  6365. M-down Move entry/item down
  6366. M-left Promote
  6367. M-right Demote
  6368. M-S-up Move entry/item up
  6369. M-S-down Move entry/item down
  6370. M-S-left Promote subtree
  6371. M-S-right Demote subtree
  6372. M-q Fill paragraph and items like in Org-mode
  6373. C-c ^ Sort entries
  6374. C-c - Cycle list bullet
  6375. TAB Cycle item visibility
  6376. M-RET Insert new heading/item
  6377. S-M-RET Insert new TODO heading / Checkbox item
  6378. C-c C-c Set tags / toggle checkbox"
  6379. nil " OrgStruct" nil
  6380. (org-load-modules-maybe)
  6381. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6382. ;;;###autoload
  6383. (defun turn-on-orgstruct ()
  6384. "Unconditionally turn on `orgstruct-mode'."
  6385. (orgstruct-mode 1))
  6386. (defun orgstruct++-mode (&optional arg)
  6387. "Toggle `orgstruct-mode', the enhanced version of it.
  6388. In addition to setting orgstruct-mode, this also exports all indentation
  6389. and autofilling variables from org-mode into the buffer. It will also
  6390. recognize item context in multiline items.
  6391. Note that turning off orgstruct-mode will *not* remove the
  6392. indentation/paragraph settings. This can only be done by refreshing the
  6393. major mode, for example with \\[normal-mode]."
  6394. (interactive "P")
  6395. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6396. (if (< arg 1)
  6397. (orgstruct-mode -1)
  6398. (orgstruct-mode 1)
  6399. (let (var val)
  6400. (mapc
  6401. (lambda (x)
  6402. (when (string-match
  6403. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6404. (symbol-name (car x)))
  6405. (setq var (car x) val (nth 1 x))
  6406. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6407. org-local-vars)
  6408. (org-set-local 'orgstruct-is-++ t))))
  6409. (defvar orgstruct-is-++ nil
  6410. "Is orgstruct-mode in ++ version in the current-buffer?")
  6411. (make-variable-buffer-local 'orgstruct-is-++)
  6412. ;;;###autoload
  6413. (defun turn-on-orgstruct++ ()
  6414. "Unconditionally turn on `orgstruct++-mode'."
  6415. (orgstruct++-mode 1))
  6416. (defun orgstruct-error ()
  6417. "Error when there is no default binding for a structure key."
  6418. (interactive)
  6419. (error "This key has no function outside structure elements"))
  6420. (defun orgstruct-setup ()
  6421. "Setup orgstruct keymaps."
  6422. (let ((nfunc 0)
  6423. (bindings
  6424. (list
  6425. '([(meta up)] org-metaup)
  6426. '([(meta down)] org-metadown)
  6427. '([(meta left)] org-metaleft)
  6428. '([(meta right)] org-metaright)
  6429. '([(meta shift up)] org-shiftmetaup)
  6430. '([(meta shift down)] org-shiftmetadown)
  6431. '([(meta shift left)] org-shiftmetaleft)
  6432. '([(meta shift right)] org-shiftmetaright)
  6433. '([?\e (up)] org-metaup)
  6434. '([?\e (down)] org-metadown)
  6435. '([?\e (left)] org-metaleft)
  6436. '([?\e (right)] org-metaright)
  6437. '([?\e (shift up)] org-shiftmetaup)
  6438. '([?\e (shift down)] org-shiftmetadown)
  6439. '([?\e (shift left)] org-shiftmetaleft)
  6440. '([?\e (shift right)] org-shiftmetaright)
  6441. '([(shift up)] org-shiftup)
  6442. '([(shift down)] org-shiftdown)
  6443. '([(shift left)] org-shiftleft)
  6444. '([(shift right)] org-shiftright)
  6445. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6446. '("\M-q" fill-paragraph)
  6447. '("\C-c^" org-sort)
  6448. '("\C-c-" org-cycle-list-bullet)))
  6449. elt key fun cmd)
  6450. (while (setq elt (pop bindings))
  6451. (setq nfunc (1+ nfunc))
  6452. (setq key (org-key (car elt))
  6453. fun (nth 1 elt)
  6454. cmd (orgstruct-make-binding fun nfunc key))
  6455. (org-defkey orgstruct-mode-map key cmd))
  6456. ;; Special treatment needed for TAB and RET
  6457. (org-defkey orgstruct-mode-map [(tab)]
  6458. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6459. (org-defkey orgstruct-mode-map "\C-i"
  6460. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6461. (org-defkey orgstruct-mode-map "\M-\C-m"
  6462. (orgstruct-make-binding 'org-insert-heading 105
  6463. "\M-\C-m" [(meta return)]))
  6464. (org-defkey orgstruct-mode-map [(meta return)]
  6465. (orgstruct-make-binding 'org-insert-heading 106
  6466. [(meta return)] "\M-\C-m"))
  6467. (org-defkey orgstruct-mode-map [(shift meta return)]
  6468. (orgstruct-make-binding 'org-insert-todo-heading 107
  6469. [(meta return)] "\M-\C-m"))
  6470. (org-defkey orgstruct-mode-map "\e\C-m"
  6471. (orgstruct-make-binding 'org-insert-heading 108
  6472. "\e\C-m" [?\e (return)]))
  6473. (org-defkey orgstruct-mode-map [?\e (return)]
  6474. (orgstruct-make-binding 'org-insert-heading 109
  6475. [?\e (return)] "\e\C-m"))
  6476. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6477. (orgstruct-make-binding 'org-insert-todo-heading 110
  6478. [?\e (return)] "\e\C-m"))
  6479. (unless org-local-vars
  6480. (setq org-local-vars (org-get-local-variables)))
  6481. t))
  6482. (defun orgstruct-make-binding (fun n &rest keys)
  6483. "Create a function for binding in the structure minor mode.
  6484. FUN is the command to call inside a table. N is used to create a unique
  6485. command name. KEYS are keys that should be checked in for a command
  6486. to execute outside of tables."
  6487. (eval
  6488. (list 'defun
  6489. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6490. '(arg)
  6491. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6492. "Outside of structure, run the binding of `"
  6493. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6494. "'.")
  6495. '(interactive "p")
  6496. (list 'if
  6497. `(org-context-p 'headline 'item
  6498. (and orgstruct-is-++
  6499. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6500. 'item-body))
  6501. (list 'org-run-like-in-org-mode (list 'quote fun))
  6502. (list 'let '(orgstruct-mode)
  6503. (list 'call-interactively
  6504. (append '(or)
  6505. (mapcar (lambda (k)
  6506. (list 'key-binding k))
  6507. keys)
  6508. '('orgstruct-error))))))))
  6509. (defun org-context-p (&rest contexts)
  6510. "Check if local context is any of CONTEXTS.
  6511. Possible values in the list of contexts are `table', `headline', and `item'."
  6512. (let ((pos (point)))
  6513. (goto-char (point-at-bol))
  6514. (prog1 (or (and (memq 'table contexts)
  6515. (looking-at "[ \t]*|"))
  6516. (and (memq 'headline contexts)
  6517. ;;????????? (looking-at "\\*+"))
  6518. (looking-at outline-regexp))
  6519. (and (memq 'item contexts)
  6520. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6521. (and (memq 'item-body contexts)
  6522. (org-in-item-p)))
  6523. (goto-char pos))))
  6524. (defun org-get-local-variables ()
  6525. "Return a list of all local variables in an org-mode buffer."
  6526. (let (varlist)
  6527. (with-current-buffer (get-buffer-create "*Org tmp*")
  6528. (erase-buffer)
  6529. (org-mode)
  6530. (setq varlist (buffer-local-variables)))
  6531. (kill-buffer "*Org tmp*")
  6532. (delq nil
  6533. (mapcar
  6534. (lambda (x)
  6535. (setq x
  6536. (if (symbolp x)
  6537. (list x)
  6538. (list (car x) (list 'quote (cdr x)))))
  6539. (if (string-match
  6540. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6541. (symbol-name (car x)))
  6542. x nil))
  6543. varlist))))
  6544. ;;;###autoload
  6545. (defun org-run-like-in-org-mode (cmd)
  6546. "Run a command, pretending that the current buffer is in Org-mode.
  6547. This will temporarily bind local variables that are typically bound in
  6548. Org-mode to the values they have in Org-mode, and then interactively
  6549. call CMD."
  6550. (org-load-modules-maybe)
  6551. (unless org-local-vars
  6552. (setq org-local-vars (org-get-local-variables)))
  6553. (eval (list 'let org-local-vars
  6554. (list 'call-interactively (list 'quote cmd)))))
  6555. ;;;; Archiving
  6556. (defun org-get-category (&optional pos)
  6557. "Get the category applying to position POS."
  6558. (get-text-property (or pos (point)) 'org-category))
  6559. (defun org-refresh-category-properties ()
  6560. "Refresh category text properties in the buffer."
  6561. (let ((def-cat (cond
  6562. ((null org-category)
  6563. (if buffer-file-name
  6564. (file-name-sans-extension
  6565. (file-name-nondirectory buffer-file-name))
  6566. "???"))
  6567. ((symbolp org-category) (symbol-name org-category))
  6568. (t org-category)))
  6569. beg end cat pos optionp)
  6570. (org-unmodified
  6571. (save-excursion
  6572. (save-restriction
  6573. (widen)
  6574. (goto-char (point-min))
  6575. (put-text-property (point) (point-max) 'org-category def-cat)
  6576. (while (re-search-forward
  6577. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6578. (setq pos (match-end 0)
  6579. optionp (equal (char-after (match-beginning 0)) ?#)
  6580. cat (org-trim (match-string 2)))
  6581. (if optionp
  6582. (setq beg (point-at-bol) end (point-max))
  6583. (org-back-to-heading t)
  6584. (setq beg (point) end (org-end-of-subtree t t)))
  6585. (put-text-property beg end 'org-category cat)
  6586. (goto-char pos)))))))
  6587. ;;;; Link Stuff
  6588. ;;; Link abbreviations
  6589. (defun org-link-expand-abbrev (link)
  6590. "Apply replacements as defined in `org-link-abbrev-alist."
  6591. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6592. (let* ((key (match-string 1 link))
  6593. (as (or (assoc key org-link-abbrev-alist-local)
  6594. (assoc key org-link-abbrev-alist)))
  6595. (tag (and (match-end 2) (match-string 3 link)))
  6596. rpl)
  6597. (if (not as)
  6598. link
  6599. (setq rpl (cdr as))
  6600. (cond
  6601. ((symbolp rpl) (funcall rpl tag))
  6602. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6603. ((string-match "%h" rpl)
  6604. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6605. (t (concat rpl tag)))))
  6606. link))
  6607. ;;; Storing and inserting links
  6608. (defvar org-insert-link-history nil
  6609. "Minibuffer history for links inserted with `org-insert-link'.")
  6610. (defvar org-stored-links nil
  6611. "Contains the links stored with `org-store-link'.")
  6612. (defvar org-store-link-plist nil
  6613. "Plist with info about the most recently link created with `org-store-link'.")
  6614. (defvar org-link-protocols nil
  6615. "Link protocols added to Org-mode using `org-add-link-type'.")
  6616. (defvar org-store-link-functions nil
  6617. "List of functions that are called to create and store a link.
  6618. Each function will be called in turn until one returns a non-nil
  6619. value. Each function should check if it is responsible for creating
  6620. this link (for example by looking at the major mode).
  6621. If not, it must exit and return nil.
  6622. If yes, it should return a non-nil value after a calling
  6623. `org-store-link-props' with a list of properties and values.
  6624. Special properties are:
  6625. :type The link prefix. like \"http\". This must be given.
  6626. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6627. This is obligatory as well.
  6628. :description Optional default description for the second pair
  6629. of brackets in an Org-mode link. The user can still change
  6630. this when inserting this link into an Org-mode buffer.
  6631. In addition to these, any additional properties can be specified
  6632. and then used in remember templates.")
  6633. (defun org-add-link-type (type &optional follow export)
  6634. "Add TYPE to the list of `org-link-types'.
  6635. Re-compute all regular expressions depending on `org-link-types'
  6636. FOLLOW and EXPORT are two functions.
  6637. FOLLOW should take the link path as the single argument and do whatever
  6638. is necessary to follow the link, for example find a file or display
  6639. a mail message.
  6640. EXPORT should format the link path for export to one of the export formats.
  6641. It should be a function accepting three arguments:
  6642. path the path of the link, the text after the prefix (like \"http:\")
  6643. desc the description of the link, if any, nil if there was no description
  6644. format the export format, a symbol like `html' or `latex'.
  6645. The function may use the FORMAT information to return different values
  6646. depending on the format. The return value will be put literally into
  6647. the exported file.
  6648. Org-mode has a built-in default for exporting links. If you are happy with
  6649. this default, there is no need to define an export function for the link
  6650. type. For a simple example of an export function, see `org-bbdb.el'."
  6651. (add-to-list 'org-link-types type t)
  6652. (org-make-link-regexps)
  6653. (if (assoc type org-link-protocols)
  6654. (setcdr (assoc type org-link-protocols) (list follow export))
  6655. (push (list type follow export) org-link-protocols)))
  6656. (defvar org-agenda-buffer-name)
  6657. ;;;###autoload
  6658. (defun org-store-link (arg)
  6659. "\\<org-mode-map>Store an org-link to the current location.
  6660. This link is added to `org-stored-links' and can later be inserted
  6661. into an org-buffer with \\[org-insert-link].
  6662. For some link types, a prefix arg is interpreted:
  6663. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6664. For file links, arg negates `org-context-in-file-links'."
  6665. (interactive "P")
  6666. (org-load-modules-maybe)
  6667. (setq org-store-link-plist nil) ; reset
  6668. (let ((outline-regexp (org-get-limited-outline-regexp))
  6669. link cpltxt desc description search txt custom-id)
  6670. (cond
  6671. ((run-hook-with-args-until-success 'org-store-link-functions)
  6672. (setq link (plist-get org-store-link-plist :link)
  6673. desc (or (plist-get org-store-link-plist :description) link)))
  6674. ((equal (buffer-name) "*Org Edit Src Example*")
  6675. (let (label gc)
  6676. (while (or (not label)
  6677. (save-excursion
  6678. (save-restriction
  6679. (widen)
  6680. (goto-char (point-min))
  6681. (re-search-forward
  6682. (regexp-quote (format org-coderef-label-format label))
  6683. nil t))))
  6684. (when label (message "Label exists already") (sit-for 2))
  6685. (setq label (read-string "Code line label: " label)))
  6686. (end-of-line 1)
  6687. (setq link (format org-coderef-label-format label))
  6688. (setq gc (- 79 (length link)))
  6689. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6690. (insert link)
  6691. (setq link (concat "(" label ")") desc nil)))
  6692. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  6693. ;; We are in the agenda, link to referenced location
  6694. (let ((m (or (get-text-property (point) 'org-hd-marker)
  6695. (get-text-property (point) 'org-marker))))
  6696. (when m
  6697. (org-with-point-at m
  6698. (call-interactively 'org-store-link)))))
  6699. ((eq major-mode 'calendar-mode)
  6700. (let ((cd (calendar-cursor-to-date)))
  6701. (setq link
  6702. (format-time-string
  6703. (car org-time-stamp-formats)
  6704. (apply 'encode-time
  6705. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6706. nil nil nil))))
  6707. (org-store-link-props :type "calendar" :date cd)))
  6708. ((eq major-mode 'w3-mode)
  6709. (setq cpltxt (if (and (buffer-name)
  6710. (not (string-match "Untitled" (buffer-name))))
  6711. (buffer-name)
  6712. (url-view-url t))
  6713. link (org-make-link (url-view-url t)))
  6714. (org-store-link-props :type "w3" :url (url-view-url t)))
  6715. ((eq major-mode 'w3m-mode)
  6716. (setq cpltxt (or w3m-current-title w3m-current-url)
  6717. link (org-make-link w3m-current-url))
  6718. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6719. ((setq search (run-hook-with-args-until-success
  6720. 'org-create-file-search-functions))
  6721. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6722. "::" search))
  6723. (setq cpltxt (or description link)))
  6724. ((eq major-mode 'image-mode)
  6725. (setq cpltxt (concat "file:"
  6726. (abbreviate-file-name buffer-file-name))
  6727. link (org-make-link cpltxt))
  6728. (org-store-link-props :type "image" :file buffer-file-name))
  6729. ((eq major-mode 'dired-mode)
  6730. ;; link to the file in the current line
  6731. (setq cpltxt (concat "file:"
  6732. (abbreviate-file-name
  6733. (expand-file-name
  6734. (dired-get-filename nil t))))
  6735. link (org-make-link cpltxt)))
  6736. ((and buffer-file-name (org-mode-p))
  6737. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6738. (cond
  6739. ((org-in-regexp "<<\\(.*?\\)>>")
  6740. (setq cpltxt
  6741. (concat "file:"
  6742. (abbreviate-file-name buffer-file-name)
  6743. "::" (match-string 1))
  6744. link (org-make-link cpltxt)))
  6745. ((and (featurep 'org-id)
  6746. (or (eq org-link-to-org-use-id t)
  6747. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6748. (interactive-p))
  6749. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6750. (interactive-p)
  6751. (not custom-id))
  6752. (and org-link-to-org-use-id
  6753. (condition-case nil
  6754. (org-entry-get nil "ID")
  6755. (error nil)))))
  6756. ;; We can make a link using the ID.
  6757. (setq link (condition-case nil
  6758. (prog1 (org-id-store-link)
  6759. (setq desc (plist-get org-store-link-plist
  6760. :description)))
  6761. (error
  6762. ;; probably before first headline, link to file only
  6763. (concat "file:"
  6764. (abbreviate-file-name buffer-file-name))))))
  6765. (t
  6766. ;; Just link to current headline
  6767. (setq cpltxt (concat "file:"
  6768. (abbreviate-file-name buffer-file-name)))
  6769. ;; Add a context search string
  6770. (when (org-xor org-context-in-file-links arg)
  6771. (setq txt (cond
  6772. ((org-on-heading-p) nil)
  6773. ((org-region-active-p)
  6774. (buffer-substring (region-beginning) (region-end)))
  6775. (t nil)))
  6776. (when (or (null txt) (string-match "\\S-" txt))
  6777. (setq cpltxt
  6778. (concat cpltxt "::"
  6779. (condition-case nil
  6780. (org-make-org-heading-search-string txt)
  6781. (error "")))
  6782. desc (or (nth 4 (ignore-errors
  6783. (org-heading-components))) "NONE"))))
  6784. (if (string-match "::\\'" cpltxt)
  6785. (setq cpltxt (substring cpltxt 0 -2)))
  6786. (setq link (org-make-link cpltxt)))))
  6787. ((buffer-file-name (buffer-base-buffer))
  6788. ;; Just link to this file here.
  6789. (setq cpltxt (concat "file:"
  6790. (abbreviate-file-name
  6791. (buffer-file-name (buffer-base-buffer)))))
  6792. ;; Add a context string
  6793. (when (org-xor org-context-in-file-links arg)
  6794. (setq txt (if (org-region-active-p)
  6795. (buffer-substring (region-beginning) (region-end))
  6796. (buffer-substring (point-at-bol) (point-at-eol))))
  6797. ;; Only use search option if there is some text.
  6798. (when (string-match "\\S-" txt)
  6799. (setq cpltxt
  6800. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6801. desc "NONE")))
  6802. (setq link (org-make-link cpltxt)))
  6803. ((interactive-p)
  6804. (error "Cannot link to a buffer which is not visiting a file"))
  6805. (t (setq link nil)))
  6806. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6807. (setq link (or link cpltxt)
  6808. desc (or desc cpltxt))
  6809. (if (equal desc "NONE") (setq desc nil))
  6810. (if (and (or (interactive-p) executing-kbd-macro) link)
  6811. (progn
  6812. (setq org-stored-links
  6813. (cons (list link desc) org-stored-links))
  6814. (message "Stored: %s" (or desc link))
  6815. (when custom-id
  6816. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6817. "::#" custom-id))
  6818. (setq org-stored-links
  6819. (cons (list link desc) org-stored-links))))
  6820. (and link (org-make-link-string link desc)))))
  6821. (defun org-store-link-props (&rest plist)
  6822. "Store link properties, extract names and addresses."
  6823. (let (x adr)
  6824. (when (setq x (plist-get plist :from))
  6825. (setq adr (mail-extract-address-components x))
  6826. (setq plist (plist-put plist :fromname (car adr)))
  6827. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6828. (when (setq x (plist-get plist :to))
  6829. (setq adr (mail-extract-address-components x))
  6830. (setq plist (plist-put plist :toname (car adr)))
  6831. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6832. (let ((from (plist-get plist :from))
  6833. (to (plist-get plist :to)))
  6834. (when (and from to org-from-is-user-regexp)
  6835. (setq plist
  6836. (plist-put plist :fromto
  6837. (if (string-match org-from-is-user-regexp from)
  6838. (concat "to %t")
  6839. (concat "from %f"))))))
  6840. (setq org-store-link-plist plist))
  6841. (defun org-add-link-props (&rest plist)
  6842. "Add these properties to the link property list."
  6843. (let (key value)
  6844. (while plist
  6845. (setq key (pop plist) value (pop plist))
  6846. (setq org-store-link-plist
  6847. (plist-put org-store-link-plist key value)))))
  6848. (defun org-email-link-description (&optional fmt)
  6849. "Return the description part of an email link.
  6850. This takes information from `org-store-link-plist' and formats it
  6851. according to FMT (default from `org-email-link-description-format')."
  6852. (setq fmt (or fmt org-email-link-description-format))
  6853. (let* ((p org-store-link-plist)
  6854. (to (plist-get p :toaddress))
  6855. (from (plist-get p :fromaddress))
  6856. (table
  6857. (list
  6858. (cons "%c" (plist-get p :fromto))
  6859. (cons "%F" (plist-get p :from))
  6860. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6861. (cons "%T" (plist-get p :to))
  6862. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6863. (cons "%s" (plist-get p :subject))
  6864. (cons "%m" (plist-get p :message-id)))))
  6865. (when (string-match "%c" fmt)
  6866. ;; Check if the user wrote this message
  6867. (if (and org-from-is-user-regexp from to
  6868. (save-match-data (string-match org-from-is-user-regexp from)))
  6869. (setq fmt (replace-match "to %t" t t fmt))
  6870. (setq fmt (replace-match "from %f" t t fmt))))
  6871. (org-replace-escapes fmt table)))
  6872. (defun org-make-org-heading-search-string (&optional string heading)
  6873. "Make search string for STRING or current headline."
  6874. (interactive)
  6875. (let ((s (or string (org-get-heading))))
  6876. (unless (and string (not heading))
  6877. ;; We are using a headline, clean up garbage in there.
  6878. (if (string-match org-todo-regexp s)
  6879. (setq s (replace-match "" t t s)))
  6880. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6881. (setq s (replace-match "" t t s)))
  6882. (setq s (org-trim s))
  6883. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6884. org-comment-string "\\)") s)
  6885. (setq s (replace-match "" t t s)))
  6886. (while (string-match org-ts-regexp s)
  6887. (setq s (replace-match "" t t s))))
  6888. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6889. (setq s (replace-match " " t t s)))
  6890. (or string (setq s (concat "*" s))) ; Add * for headlines
  6891. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6892. (defun org-make-link (&rest strings)
  6893. "Concatenate STRINGS."
  6894. (apply 'concat strings))
  6895. (defun org-make-link-string (link &optional description)
  6896. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6897. (unless (string-match "\\S-" link)
  6898. (error "Empty link"))
  6899. (when (stringp description)
  6900. ;; Remove brackets from the description, they are fatal.
  6901. (while (string-match "\\[" description)
  6902. (setq description (replace-match "{" t t description)))
  6903. (while (string-match "\\]" description)
  6904. (setq description (replace-match "}" t t description))))
  6905. (when (equal (org-link-escape link) description)
  6906. ;; No description needed, it is identical
  6907. (setq description nil))
  6908. (when (and (not description)
  6909. (not (equal link (org-link-escape link))))
  6910. (setq description (org-extract-attributes link)))
  6911. (concat "[[" (org-link-escape link) "]"
  6912. (if description (concat "[" description "]") "")
  6913. "]"))
  6914. (defconst org-link-escape-chars
  6915. '((?\ . "%20")
  6916. (?\[ . "%5B")
  6917. (?\] . "%5D")
  6918. (?\340 . "%E0") ; `a
  6919. (?\342 . "%E2") ; ^a
  6920. (?\347 . "%E7") ; ,c
  6921. (?\350 . "%E8") ; `e
  6922. (?\351 . "%E9") ; 'e
  6923. (?\352 . "%EA") ; ^e
  6924. (?\356 . "%EE") ; ^i
  6925. (?\364 . "%F4") ; ^o
  6926. (?\371 . "%F9") ; `u
  6927. (?\373 . "%FB") ; ^u
  6928. (?\; . "%3B")
  6929. (?? . "%3F")
  6930. (?= . "%3D")
  6931. (?+ . "%2B")
  6932. )
  6933. "Association list of escapes for some characters problematic in links.
  6934. This is the list that is used for internal purposes.")
  6935. (defvar org-url-encoding-use-url-hexify nil)
  6936. (defconst org-link-escape-chars-browser
  6937. '((?\ . "%20")) ; 32 for the SPC char
  6938. "Association list of escapes for some characters problematic in links.
  6939. This is the list that is used before handing over to the browser.")
  6940. (defun org-link-escape (text &optional table)
  6941. "Escape characters in TEXT that are problematic for links."
  6942. (if org-url-encoding-use-url-hexify
  6943. (url-hexify-string text)
  6944. (setq table (or table org-link-escape-chars))
  6945. (when text
  6946. (let ((re (mapconcat (lambda (x) (regexp-quote
  6947. (char-to-string (car x))))
  6948. table "\\|")))
  6949. (while (string-match re text)
  6950. (setq text
  6951. (replace-match
  6952. (cdr (assoc (string-to-char (match-string 0 text))
  6953. table))
  6954. t t text)))
  6955. text))))
  6956. (defun org-link-unescape (text &optional table)
  6957. "Reverse the action of `org-link-escape'."
  6958. (if org-url-encoding-use-url-hexify
  6959. (url-unhex-string text)
  6960. (setq table (or table org-link-escape-chars))
  6961. (when text
  6962. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6963. table "\\|")))
  6964. (while (string-match re text)
  6965. (setq text
  6966. (replace-match
  6967. (char-to-string (car (rassoc (match-string 0 text) table)))
  6968. t t text)))
  6969. text))))
  6970. (defun org-xor (a b)
  6971. "Exclusive or."
  6972. (if a (not b) b))
  6973. (defun org-fixup-message-id-for-http (s)
  6974. "Replace special characters in a message id, so it can be used in an http query."
  6975. (while (string-match "<" s)
  6976. (setq s (replace-match "%3C" t t s)))
  6977. (while (string-match ">" s)
  6978. (setq s (replace-match "%3E" t t s)))
  6979. (while (string-match "@" s)
  6980. (setq s (replace-match "%40" t t s)))
  6981. s)
  6982. ;;;###autoload
  6983. (defun org-insert-link-global ()
  6984. "Insert a link like Org-mode does.
  6985. This command can be called in any mode to insert a link in Org-mode syntax."
  6986. (interactive)
  6987. (org-load-modules-maybe)
  6988. (org-run-like-in-org-mode 'org-insert-link))
  6989. (defun org-insert-link (&optional complete-file link-location)
  6990. "Insert a link. At the prompt, enter the link.
  6991. Completion can be used to insert any of the link protocol prefixes like
  6992. http or ftp in use.
  6993. The history can be used to select a link previously stored with
  6994. `org-store-link'. When the empty string is entered (i.e. if you just
  6995. press RET at the prompt), the link defaults to the most recently
  6996. stored link. As SPC triggers completion in the minibuffer, you need to
  6997. use M-SPC or C-q SPC to force the insertion of a space character.
  6998. You will also be prompted for a description, and if one is given, it will
  6999. be displayed in the buffer instead of the link.
  7000. If there is already a link at point, this command will allow you to edit link
  7001. and description parts.
  7002. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7003. be selected using completion. The path to the file will be relative to the
  7004. current directory if the file is in the current directory or a subdirectory.
  7005. Otherwise, the link will be the absolute path as completed in the minibuffer
  7006. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7007. option `org-link-file-path-type'.
  7008. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7009. the current directory or below.
  7010. With three \\[universal-argument] prefixes, negate the meaning of
  7011. `org-keep-stored-link-after-insertion'.
  7012. If `org-make-link-description-function' is non-nil, this function will be
  7013. called with the link target, and the result will be the default
  7014. link description.
  7015. If the LINK-LOCATION parameter is non-nil, this value will be
  7016. used as the link location instead of reading one interactively."
  7017. (interactive "P")
  7018. (let* ((wcf (current-window-configuration))
  7019. (region (if (org-region-active-p)
  7020. (buffer-substring (region-beginning) (region-end))))
  7021. (remove (and region (list (region-beginning) (region-end))))
  7022. (desc region)
  7023. tmphist ; byte-compile incorrectly complains about this
  7024. (link link-location)
  7025. entry file all-prefixes)
  7026. (cond
  7027. (link-location) ; specified by arg, just use it.
  7028. ((org-in-regexp org-bracket-link-regexp 1)
  7029. ;; We do have a link at point, and we are going to edit it.
  7030. (setq remove (list (match-beginning 0) (match-end 0)))
  7031. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7032. (setq link (read-string "Link: "
  7033. (org-link-unescape
  7034. (org-match-string-no-properties 1)))))
  7035. ((or (org-in-regexp org-angle-link-re)
  7036. (org-in-regexp org-plain-link-re))
  7037. ;; Convert to bracket link
  7038. (setq remove (list (match-beginning 0) (match-end 0))
  7039. link (read-string "Link: "
  7040. (org-remove-angle-brackets (match-string 0)))))
  7041. ((member complete-file '((4) (16)))
  7042. ;; Completing read for file names.
  7043. (setq link (org-file-complete-link complete-file)))
  7044. (t
  7045. ;; Read link, with completion for stored links.
  7046. (with-output-to-temp-buffer "*Org Links*"
  7047. (princ "Insert a link.
  7048. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7049. (when org-stored-links
  7050. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7051. (princ (mapconcat
  7052. (lambda (x)
  7053. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7054. (reverse org-stored-links) "\n"))))
  7055. (let ((cw (selected-window)))
  7056. (select-window (get-buffer-window "*Org Links*"))
  7057. (setq truncate-lines t)
  7058. (unless (pos-visible-in-window-p (point-max))
  7059. (org-fit-window-to-buffer))
  7060. (and (window-live-p cw) (select-window cw)))
  7061. ;; Fake a link history, containing the stored links.
  7062. (setq tmphist (append (mapcar 'car org-stored-links)
  7063. org-insert-link-history))
  7064. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7065. (mapcar 'car org-link-abbrev-alist)
  7066. org-link-types))
  7067. (unwind-protect
  7068. (progn
  7069. (setq link
  7070. (let ((org-completion-use-ido nil)
  7071. (org-completion-use-iswitchb nil))
  7072. (org-completing-read
  7073. "Link: "
  7074. (append
  7075. (mapcar (lambda (x) (list (concat x ":")))
  7076. all-prefixes)
  7077. (mapcar 'car org-stored-links))
  7078. nil nil nil
  7079. 'tmphist
  7080. (car (car org-stored-links)))))
  7081. (if (or (member link all-prefixes)
  7082. (and (equal ":" (substring link -1))
  7083. (member (substring link 0 -1) all-prefixes)
  7084. (setq link (substring link 0 -1))))
  7085. (setq link (org-link-try-special-completion link))))
  7086. (set-window-configuration wcf)
  7087. (kill-buffer "*Org Links*"))
  7088. (setq entry (assoc link org-stored-links))
  7089. (or entry (push link org-insert-link-history))
  7090. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7091. (not org-keep-stored-link-after-insertion))
  7092. (setq org-stored-links (delq (assoc link org-stored-links)
  7093. org-stored-links)))
  7094. (setq desc (or desc (nth 1 entry)))))
  7095. (if (string-match org-plain-link-re link)
  7096. ;; URL-like link, normalize the use of angular brackets.
  7097. (setq link (org-make-link (org-remove-angle-brackets link))))
  7098. ;; Check if we are linking to the current file with a search option
  7099. ;; If yes, simplify the link by using only the search option.
  7100. (when (and buffer-file-name
  7101. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7102. (let* ((path (match-string 1 link))
  7103. (case-fold-search nil)
  7104. (search (match-string 2 link)))
  7105. (save-match-data
  7106. (if (equal (file-truename buffer-file-name) (file-truename path))
  7107. ;; We are linking to this same file, with a search option
  7108. (setq link search)))))
  7109. ;; Check if we can/should use a relative path. If yes, simplify the link
  7110. (when (string-match "^file:\\(.*\\)" link)
  7111. (let* ((path (match-string 1 link))
  7112. (origpath path)
  7113. (case-fold-search nil))
  7114. (cond
  7115. ((or (eq org-link-file-path-type 'absolute)
  7116. (equal complete-file '(16)))
  7117. (setq path (abbreviate-file-name (expand-file-name path))))
  7118. ((eq org-link-file-path-type 'noabbrev)
  7119. (setq path (expand-file-name path)))
  7120. ((eq org-link-file-path-type 'relative)
  7121. (setq path (file-relative-name path)))
  7122. (t
  7123. (save-match-data
  7124. (if (string-match (concat "^" (regexp-quote
  7125. (file-name-as-directory
  7126. (expand-file-name "."))))
  7127. (expand-file-name path))
  7128. ;; We are linking a file with relative path name.
  7129. (setq path (substring (expand-file-name path)
  7130. (match-end 0)))
  7131. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7132. (setq link (concat "file:" path))
  7133. (if (equal desc origpath)
  7134. (setq desc path))))
  7135. (if org-make-link-description-function
  7136. (setq desc (funcall org-make-link-description-function link desc)))
  7137. (setq desc (read-string "Description: " desc))
  7138. (unless (string-match "\\S-" desc) (setq desc nil))
  7139. (if remove (apply 'delete-region remove))
  7140. (insert (org-make-link-string link desc))))
  7141. (defun org-link-try-special-completion (type)
  7142. "If there is completion support for link type TYPE, offer it."
  7143. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7144. (if (functionp fun)
  7145. (funcall fun)
  7146. (read-string "Link (no completion support): " (concat type ":")))))
  7147. (defun org-file-complete-link (&optional arg)
  7148. "Create a file link using completion."
  7149. (let (file link)
  7150. (setq file (read-file-name "File: "))
  7151. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7152. (pwd1 (file-name-as-directory (abbreviate-file-name
  7153. (expand-file-name ".")))))
  7154. (cond
  7155. ((equal arg '(16))
  7156. (setq link (org-make-link
  7157. "file:"
  7158. (abbreviate-file-name (expand-file-name file)))))
  7159. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7160. (setq link (org-make-link "file:" (match-string 1 file))))
  7161. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7162. (expand-file-name file))
  7163. (setq link (org-make-link
  7164. "file:" (match-string 1 (expand-file-name file)))))
  7165. (t (setq link (org-make-link "file:" file)))))
  7166. link))
  7167. (defun org-completing-read (&rest args)
  7168. "Completing-read with SPACE being a normal character."
  7169. (let ((minibuffer-local-completion-map
  7170. (copy-keymap minibuffer-local-completion-map)))
  7171. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7172. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7173. (apply 'org-icompleting-read args)))
  7174. (defun org-completing-read-no-i (&rest args)
  7175. (let (org-completion-use-ido org-completion-use-iswitchb)
  7176. (apply 'org-completing-read args)))
  7177. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7178. "Use iswitch as a completing-read replacement to choose from choices.
  7179. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7180. from."
  7181. (let* ((iswitchb-use-virtual-buffers nil)
  7182. (iswitchb-make-buflist-hook
  7183. (lambda ()
  7184. (setq iswitchb-temp-buflist choices))))
  7185. (iswitchb-read-buffer prompt)))
  7186. (defun org-icompleting-read (&rest args)
  7187. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7188. (org-without-partial-completion
  7189. (if (and org-completion-use-ido
  7190. (fboundp 'ido-completing-read)
  7191. (boundp 'ido-mode) ido-mode
  7192. (listp (second args)))
  7193. (let ((ido-enter-matching-directory nil))
  7194. (apply 'ido-completing-read (concat (car args))
  7195. (if (consp (car (nth 1 args)))
  7196. (mapcar (lambda (x) (car x)) (nth 1 args))
  7197. (nth 1 args))
  7198. (cddr args)))
  7199. (if (and org-completion-use-iswitchb
  7200. (boundp 'iswitchb-mode) iswitchb-mode
  7201. (listp (second args)))
  7202. (apply 'org-iswitchb-completing-read (concat (car args))
  7203. (if (consp (car (nth 1 args)))
  7204. (mapcar (lambda (x) (car x)) (nth 1 args))
  7205. (nth 1 args))
  7206. (cddr args))
  7207. (apply 'completing-read args)))))
  7208. (defun org-extract-attributes (s)
  7209. "Extract the attributes cookie from a string and set as text property."
  7210. (let (a attr (start 0) key value)
  7211. (save-match-data
  7212. (when (string-match "{{\\([^}]+\\)}}$" s)
  7213. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7214. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7215. (setq key (match-string 1 a) value (match-string 2 a)
  7216. start (match-end 0)
  7217. attr (plist-put attr (intern key) value))))
  7218. (org-add-props s nil 'org-attr attr))
  7219. s))
  7220. (defun org-extract-attributes-from-string (tag)
  7221. (let (key value attr)
  7222. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7223. (setq key (match-string 1 tag) value (match-string 2 tag)
  7224. tag (replace-match "" t t tag)
  7225. attr (plist-put attr (intern key) value)))
  7226. (cons tag attr)))
  7227. (defun org-attributes-to-string (plist)
  7228. "Format a property list into an HTML attribute list."
  7229. (let ((s "") key value)
  7230. (while plist
  7231. (setq key (pop plist) value (pop plist))
  7232. (and value
  7233. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7234. s))
  7235. ;;; Opening/following a link
  7236. (defvar org-link-search-failed nil)
  7237. (defun org-next-link ()
  7238. "Move forward to the next link.
  7239. If the link is in hidden text, expose it."
  7240. (interactive)
  7241. (when (and org-link-search-failed (eq this-command last-command))
  7242. (goto-char (point-min))
  7243. (message "Link search wrapped back to beginning of buffer"))
  7244. (setq org-link-search-failed nil)
  7245. (let* ((pos (point))
  7246. (ct (org-context))
  7247. (a (assoc :link ct)))
  7248. (if a (goto-char (nth 2 a)))
  7249. (if (re-search-forward org-any-link-re nil t)
  7250. (progn
  7251. (goto-char (match-beginning 0))
  7252. (if (org-invisible-p) (org-show-context)))
  7253. (goto-char pos)
  7254. (setq org-link-search-failed t)
  7255. (error "No further link found"))))
  7256. (defun org-previous-link ()
  7257. "Move backward to the previous link.
  7258. If the link is in hidden text, expose it."
  7259. (interactive)
  7260. (when (and org-link-search-failed (eq this-command last-command))
  7261. (goto-char (point-max))
  7262. (message "Link search wrapped back to end of buffer"))
  7263. (setq org-link-search-failed nil)
  7264. (let* ((pos (point))
  7265. (ct (org-context))
  7266. (a (assoc :link ct)))
  7267. (if a (goto-char (nth 1 a)))
  7268. (if (re-search-backward org-any-link-re nil t)
  7269. (progn
  7270. (goto-char (match-beginning 0))
  7271. (if (org-invisible-p) (org-show-context)))
  7272. (goto-char pos)
  7273. (setq org-link-search-failed t)
  7274. (error "No further link found"))))
  7275. (defun org-translate-link (s)
  7276. "Translate a link string if a translation function has been defined."
  7277. (if (and org-link-translation-function
  7278. (fboundp org-link-translation-function)
  7279. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7280. (progn
  7281. (setq s (funcall org-link-translation-function
  7282. (match-string 1) (match-string 2)))
  7283. (concat (car s) ":" (cdr s)))
  7284. s))
  7285. (defun org-translate-link-from-planner (type path)
  7286. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7287. This is still an experimental function, your mileage may vary."
  7288. (cond
  7289. ((member type '("http" "https" "news" "ftp"))
  7290. ;; standard Internet links are the same.
  7291. nil)
  7292. ((and (equal type "irc") (string-match "^//" path))
  7293. ;; Planner has two / at the beginning of an irc link, we have 1.
  7294. ;; We should have zero, actually....
  7295. (setq path (substring path 1)))
  7296. ((and (equal type "lisp") (string-match "^/" path))
  7297. ;; Planner has a slash, we do not.
  7298. (setq type "elisp" path (substring path 1)))
  7299. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7300. ;; A typical message link. Planner has the id after the fina slash,
  7301. ;; we separate it with a hash mark
  7302. (setq path (concat (match-string 1 path) "#"
  7303. (org-remove-angle-brackets (match-string 2 path)))))
  7304. )
  7305. (cons type path))
  7306. (defun org-find-file-at-mouse (ev)
  7307. "Open file link or URL at mouse."
  7308. (interactive "e")
  7309. (mouse-set-point ev)
  7310. (org-open-at-point 'in-emacs))
  7311. (defun org-open-at-mouse (ev)
  7312. "Open file link or URL at mouse."
  7313. (interactive "e")
  7314. (mouse-set-point ev)
  7315. (if (eq major-mode 'org-agenda-mode)
  7316. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7317. (org-open-at-point))
  7318. (defvar org-window-config-before-follow-link nil
  7319. "The window configuration before following a link.
  7320. This is saved in case the need arises to restore it.")
  7321. (defvar org-open-link-marker (make-marker)
  7322. "Marker pointing to the location where `org-open-at-point; was called.")
  7323. ;;;###autoload
  7324. (defun org-open-at-point-global ()
  7325. "Follow a link like Org-mode does.
  7326. This command can be called in any mode to follow a link that has
  7327. Org-mode syntax."
  7328. (interactive)
  7329. (org-run-like-in-org-mode 'org-open-at-point))
  7330. ;;;###autoload
  7331. (defun org-open-link-from-string (s &optional arg reference-buffer)
  7332. "Open a link in the string S, as if it was in Org-mode."
  7333. (interactive "sLink: \nP")
  7334. (let ((reference-buffer (or reference-buffer (current-buffer))))
  7335. (with-temp-buffer
  7336. (let ((org-inhibit-startup t))
  7337. (org-mode)
  7338. (insert s)
  7339. (goto-char (point-min))
  7340. (org-open-at-point arg reference-buffer)))))
  7341. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7342. "Open link at or after point.
  7343. If there is no link at point, this function will search forward up to
  7344. the end of the current line.
  7345. Normally, files will be opened by an appropriate application. If the
  7346. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7347. With a double prefix argument, try to open outside of Emacs, in the
  7348. application the system uses for this file type."
  7349. (interactive "P")
  7350. (org-load-modules-maybe)
  7351. (move-marker org-open-link-marker (point))
  7352. (setq org-window-config-before-follow-link (current-window-configuration))
  7353. (org-remove-occur-highlights nil nil t)
  7354. (cond
  7355. ((and (org-on-heading-p)
  7356. (not (org-in-regexp
  7357. (concat org-plain-link-re "\\|"
  7358. org-bracket-link-regexp "\\|"
  7359. org-angle-link-re "\\|"
  7360. "[ \t]:[^ \t\n]+:[ \t]*$"))))
  7361. (or (org-offer-links-in-entry in-emacs)
  7362. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  7363. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7364. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7365. (org-footnote-action))
  7366. (t
  7367. (let (type path link line search (pos (point)))
  7368. (catch 'match
  7369. (save-excursion
  7370. (skip-chars-forward "^]\n\r")
  7371. (when (org-in-regexp org-bracket-link-regexp)
  7372. (setq link (org-extract-attributes
  7373. (org-link-unescape (org-match-string-no-properties 1))))
  7374. (while (string-match " *\n *" link)
  7375. (setq link (replace-match " " t t link)))
  7376. (setq link (org-link-expand-abbrev link))
  7377. (cond
  7378. ((or (file-name-absolute-p link)
  7379. (string-match "^\\.\\.?/" link))
  7380. (setq type "file" path link))
  7381. ((string-match org-link-re-with-space3 link)
  7382. (setq type (match-string 1 link) path (match-string 2 link)))
  7383. (t (setq type "thisfile" path link)))
  7384. (throw 'match t)))
  7385. (when (get-text-property (point) 'org-linked-text)
  7386. (setq type "thisfile"
  7387. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7388. (1+ (point)) (point))
  7389. path (buffer-substring
  7390. (previous-single-property-change pos 'org-linked-text)
  7391. (next-single-property-change pos 'org-linked-text)))
  7392. (throw 'match t))
  7393. (save-excursion
  7394. (when (or (org-in-regexp org-angle-link-re)
  7395. (org-in-regexp org-plain-link-re))
  7396. (setq type (match-string 1) path (match-string 2))
  7397. (throw 'match t)))
  7398. (save-excursion
  7399. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7400. (setq type "tags"
  7401. path (match-string 1))
  7402. (while (string-match ":" path)
  7403. (setq path (replace-match "+" t t path)))
  7404. (throw 'match t)))
  7405. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7406. (setq type "tree-match"
  7407. path (match-string 1))
  7408. (throw 'match t)))
  7409. (unless path
  7410. (error "No link found"))
  7411. ;; switch back to reference buffer
  7412. ;; needed when if called in a temporary buffer through
  7413. ;; org-open-link-from-string
  7414. (with-current-buffer (or reference-buffer (current-buffer))
  7415. ;; Remove any trailing spaces in path
  7416. (if (string-match " +\\'" path)
  7417. (setq path (replace-match "" t t path)))
  7418. (if (and org-link-translation-function
  7419. (fboundp org-link-translation-function))
  7420. ;; Check if we need to translate the link
  7421. (let ((tmp (funcall org-link-translation-function type path)))
  7422. (setq type (car tmp) path (cdr tmp))))
  7423. (cond
  7424. ((assoc type org-link-protocols)
  7425. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7426. ((equal type "mailto")
  7427. (let ((cmd (car org-link-mailto-program))
  7428. (args (cdr org-link-mailto-program)) args1
  7429. (address path) (subject "") a)
  7430. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7431. (setq address (match-string 1 path)
  7432. subject (org-link-escape (match-string 2 path))))
  7433. (while args
  7434. (cond
  7435. ((not (stringp (car args))) (push (pop args) args1))
  7436. (t (setq a (pop args))
  7437. (if (string-match "%a" a)
  7438. (setq a (replace-match address t t a)))
  7439. (if (string-match "%s" a)
  7440. (setq a (replace-match subject t t a)))
  7441. (push a args1))))
  7442. (apply cmd (nreverse args1))))
  7443. ((member type '("http" "https" "ftp" "news"))
  7444. (browse-url (concat type ":" (org-link-escape
  7445. path org-link-escape-chars-browser))))
  7446. ((member type '("message"))
  7447. (browse-url (concat type ":" path)))
  7448. ((string= type "tags")
  7449. (org-tags-view in-emacs path))
  7450. ((string= type "thisfile")
  7451. (if in-emacs
  7452. (switch-to-buffer-other-window
  7453. (org-get-buffer-for-internal-link (current-buffer)))
  7454. (org-mark-ring-push))
  7455. (let ((cmd `(org-link-search
  7456. ,path
  7457. ,(cond ((equal in-emacs '(4)) 'occur)
  7458. ((equal in-emacs '(16)) 'org-occur)
  7459. (t nil))
  7460. ,pos)))
  7461. (condition-case nil (eval cmd)
  7462. (error (progn (widen) (eval cmd))))))
  7463. ((string= type "tree-match")
  7464. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7465. ((string= type "file")
  7466. (if (string-match "::\\([0-9]+\\)\\'" path)
  7467. (setq line (string-to-number (match-string 1 path))
  7468. path (substring path 0 (match-beginning 0)))
  7469. (if (string-match "::\\(.+\\)\\'" path)
  7470. (setq search (match-string 1 path)
  7471. path (substring path 0 (match-beginning 0)))))
  7472. (if (string-match "[*?{]" (file-name-nondirectory path))
  7473. (dired path)
  7474. (org-open-file path in-emacs line search)))
  7475. ((string= type "news")
  7476. (require 'org-gnus)
  7477. (org-gnus-follow-link path))
  7478. ((string= type "shell")
  7479. (let ((cmd path))
  7480. (if (or (not org-confirm-shell-link-function)
  7481. (funcall org-confirm-shell-link-function
  7482. (format "Execute \"%s\" in shell? "
  7483. (org-add-props cmd nil
  7484. 'face 'org-warning))))
  7485. (progn
  7486. (message "Executing %s" cmd)
  7487. (shell-command cmd))
  7488. (error "Abort"))))
  7489. ((string= type "elisp")
  7490. (let ((cmd path))
  7491. (if (or (not org-confirm-elisp-link-function)
  7492. (funcall org-confirm-elisp-link-function
  7493. (format "Execute \"%s\" as elisp? "
  7494. (org-add-props cmd nil
  7495. 'face 'org-warning))))
  7496. (message "%s => %s" cmd
  7497. (if (equal (string-to-char cmd) ?\()
  7498. (eval (read cmd))
  7499. (call-interactively (read cmd))))
  7500. (error "Abort"))))
  7501. (t
  7502. (browse-url-at-point)))))))
  7503. (move-marker org-open-link-marker nil)
  7504. (run-hook-with-args 'org-follow-link-hook))
  7505. (defun org-offer-links-in-entry (&optional nth zero)
  7506. "Offer links in the curren entry and follow the selected link.
  7507. If there is only one link, follow it immediately as well.
  7508. If NTH is an integer, immediately pick the NTH link found.
  7509. If ZERO is a string, check also this string for a link, and if
  7510. there is one, offer it as link number zero."
  7511. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  7512. "\\(" org-angle-link-re "\\)\\|"
  7513. "\\(" org-plain-link-re "\\)"))
  7514. (cnt ?0)
  7515. (in-emacs (if (integerp nth) nil nth))
  7516. have-zero end links link c)
  7517. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  7518. (push (match-string 0 zero) links)
  7519. (setq cnt (1- cnt) have-zero t))
  7520. (save-excursion
  7521. (org-back-to-heading t)
  7522. (setq end (save-excursion (outline-next-heading) (point)))
  7523. (while (re-search-forward re end t)
  7524. (push (match-string 0) links))
  7525. (setq links (org-uniquify (reverse links))))
  7526. (cond
  7527. ((null links)
  7528. (message "No links"))
  7529. ((equal (length links) 1)
  7530. (setq link (car links)))
  7531. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  7532. (setq link (nth (if have-zero nth (1- nth)) links)))
  7533. (t ; we have to select a link
  7534. (save-excursion
  7535. (save-window-excursion
  7536. (delete-other-windows)
  7537. (with-output-to-temp-buffer "*Select Link*"
  7538. (mapc (lambda (l)
  7539. (if (not (string-match org-bracket-link-regexp l))
  7540. (princ (format "[%c] %s\n" (incf cnt)
  7541. (org-remove-angle-brackets l)))
  7542. (if (match-end 3)
  7543. (princ (format "[%c] %s (%s)\n" (incf cnt)
  7544. (match-string 3 l) (match-string 1 l)))
  7545. (princ (format "[%c] %s\n" (incf cnt)
  7546. (match-string 1 l))))))
  7547. links))
  7548. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  7549. (message "Select link to open:")
  7550. (setq c (read-char-exclusive))
  7551. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  7552. (when (equal c ?q) (error "Abort"))
  7553. (setq nth (- c ?0))
  7554. (if have-zero (setq nth (1+ nth)))
  7555. (unless (and (integerp nth) (>= (length links) nth))
  7556. (error "Invalid link selection"))
  7557. (setq link (nth (1- nth) links))))
  7558. (if link
  7559. (progn (org-open-link-from-string link in-emacs (current-buffer)) t)
  7560. nil)))
  7561. ;;;; Time estimates
  7562. (defun org-get-effort (&optional pom)
  7563. "Get the effort estimate for the current entry."
  7564. (org-entry-get pom org-effort-property))
  7565. ;;; File search
  7566. (defvar org-create-file-search-functions nil
  7567. "List of functions to construct the right search string for a file link.
  7568. These functions are called in turn with point at the location to
  7569. which the link should point.
  7570. A function in the hook should first test if it would like to
  7571. handle this file type, for example by checking the major-mode or
  7572. the file extension. If it decides not to handle this file, it
  7573. should just return nil to give other functions a chance. If it
  7574. does handle the file, it must return the search string to be used
  7575. when following the link. The search string will be part of the
  7576. file link, given after a double colon, and `org-open-at-point'
  7577. will automatically search for it. If special measures must be
  7578. taken to make the search successful, another function should be
  7579. added to the companion hook `org-execute-file-search-functions',
  7580. which see.
  7581. A function in this hook may also use `setq' to set the variable
  7582. `description' to provide a suggestion for the descriptive text to
  7583. be used for this link when it gets inserted into an Org-mode
  7584. buffer with \\[org-insert-link].")
  7585. (defvar org-execute-file-search-functions nil
  7586. "List of functions to execute a file search triggered by a link.
  7587. Functions added to this hook must accept a single argument, the
  7588. search string that was part of the file link, the part after the
  7589. double colon. The function must first check if it would like to
  7590. handle this search, for example by checking the major-mode or the
  7591. file extension. If it decides not to handle this search, it
  7592. should just return nil to give other functions a chance. If it
  7593. does handle the search, it must return a non-nil value to keep
  7594. other functions from trying.
  7595. Each function can access the current prefix argument through the
  7596. variable `current-prefix-argument'. Note that a single prefix is
  7597. used to force opening a link in Emacs, so it may be good to only
  7598. use a numeric or double prefix to guide the search function.
  7599. In case this is needed, a function in this hook can also restore
  7600. the window configuration before `org-open-at-point' was called using:
  7601. (set-window-configuration org-window-config-before-follow-link)")
  7602. (defun org-link-search (s &optional type avoid-pos)
  7603. "Search for a link search option.
  7604. If S is surrounded by forward slashes, it is interpreted as a
  7605. regular expression. In org-mode files, this will create an `org-occur'
  7606. sparse tree. In ordinary files, `occur' will be used to list matches.
  7607. If the current buffer is in `dired-mode', grep will be used to search
  7608. in all files. If AVOID-POS is given, ignore matches near that position."
  7609. (let ((case-fold-search t)
  7610. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7611. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7612. (append '(("") (" ") ("\t") ("\n"))
  7613. org-emphasis-alist)
  7614. "\\|") "\\)"))
  7615. (pos (point))
  7616. (pre nil) (post nil)
  7617. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7618. (cond
  7619. ;; First check if there are any special
  7620. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7621. ;; Now try the builtin stuff
  7622. ((and (equal (string-to-char s0) ?#)
  7623. (> (length s0) 1)
  7624. (save-excursion
  7625. (goto-char (point-min))
  7626. (and
  7627. (re-search-forward
  7628. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7629. (setq type 'dedicated
  7630. pos (match-beginning 0))))
  7631. ;; There is an exact target for this
  7632. (goto-char pos)
  7633. (org-back-to-heading t)))
  7634. ((save-excursion
  7635. (goto-char (point-min))
  7636. (and
  7637. (re-search-forward
  7638. (concat "<<" (regexp-quote s0) ">>") nil t)
  7639. (setq type 'dedicated
  7640. pos (match-beginning 0))))
  7641. ;; There is an exact target for this
  7642. (goto-char pos))
  7643. ((and (string-match "^(\\(.*\\))$" s0)
  7644. (save-excursion
  7645. (goto-char (point-min))
  7646. (and
  7647. (re-search-forward
  7648. (concat "[^[]" (regexp-quote
  7649. (format org-coderef-label-format
  7650. (match-string 1 s0))))
  7651. nil t)
  7652. (setq type 'dedicated
  7653. pos (1+ (match-beginning 0))))))
  7654. ;; There is a coderef target for this
  7655. (goto-char pos))
  7656. ((string-match "^/\\(.*\\)/$" s)
  7657. ;; A regular expression
  7658. (cond
  7659. ((org-mode-p)
  7660. (org-occur (match-string 1 s)))
  7661. ;;((eq major-mode 'dired-mode)
  7662. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7663. (t (org-do-occur (match-string 1 s)))))
  7664. (t
  7665. ;; A normal search strings
  7666. (when (equal (string-to-char s) ?*)
  7667. ;; Anchor on headlines, post may include tags.
  7668. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7669. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7670. s (substring s 1)))
  7671. (remove-text-properties
  7672. 0 (length s)
  7673. '(face nil mouse-face nil keymap nil fontified nil) s)
  7674. ;; Make a series of regular expressions to find a match
  7675. (setq words (org-split-string s "[ \n\r\t]+")
  7676. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7677. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7678. "\\)" markers)
  7679. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7680. re2a (concat "[ \t\r\n]" re2a_)
  7681. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7682. re4 (concat "[^a-zA-Z_]" re4_)
  7683. re1 (concat pre re2 post)
  7684. re3 (concat pre (if pre re4_ re4) post)
  7685. re5 (concat pre ".*" re4)
  7686. re2 (concat pre re2)
  7687. re2a (concat pre (if pre re2a_ re2a))
  7688. re4 (concat pre (if pre re4_ re4))
  7689. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7690. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7691. re5 "\\)"
  7692. ))
  7693. (cond
  7694. ((eq type 'org-occur) (org-occur reall))
  7695. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7696. (t (goto-char (point-min))
  7697. (setq type 'fuzzy)
  7698. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7699. (org-search-not-self 1 re1 nil t)
  7700. (org-search-not-self 1 re2 nil t)
  7701. (org-search-not-self 1 re2a nil t)
  7702. (org-search-not-self 1 re3 nil t)
  7703. (org-search-not-self 1 re4 nil t)
  7704. (org-search-not-self 1 re5 nil t)
  7705. )
  7706. (goto-char (match-beginning 1))
  7707. (goto-char pos)
  7708. (error "No match")))))
  7709. (t
  7710. ;; Normal string-search
  7711. (goto-char (point-min))
  7712. (if (search-forward s nil t)
  7713. (goto-char (match-beginning 0))
  7714. (error "No match"))))
  7715. (and (org-mode-p) (org-show-context 'link-search))
  7716. type))
  7717. (defun org-search-not-self (group &rest args)
  7718. "Execute `re-search-forward', but only accept matches that do not
  7719. enclose the position of `org-open-link-marker'."
  7720. (let ((m org-open-link-marker))
  7721. (catch 'exit
  7722. (while (apply 're-search-forward args)
  7723. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7724. (goto-char (match-end group))
  7725. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7726. (> (match-beginning 0) (marker-position m))
  7727. (< (match-end 0) (marker-position m)))
  7728. (save-match-data
  7729. (or (not (org-in-regexp
  7730. org-bracket-link-analytic-regexp 1))
  7731. (not (match-end 4)) ; no description
  7732. (and (<= (match-beginning 4) (point))
  7733. (>= (match-end 4) (point))))))
  7734. (throw 'exit (point))))))))
  7735. (defun org-get-buffer-for-internal-link (buffer)
  7736. "Return a buffer to be used for displaying the link target of internal links."
  7737. (cond
  7738. ((not org-display-internal-link-with-indirect-buffer)
  7739. buffer)
  7740. ((string-match "(Clone)$" (buffer-name buffer))
  7741. (message "Buffer is already a clone, not making another one")
  7742. ;; we also do not modify visibility in this case
  7743. buffer)
  7744. (t ; make a new indirect buffer for displaying the link
  7745. (let* ((bn (buffer-name buffer))
  7746. (ibn (concat bn "(Clone)"))
  7747. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7748. (with-current-buffer ib (org-overview))
  7749. ib))))
  7750. (defun org-do-occur (regexp &optional cleanup)
  7751. "Call the Emacs command `occur'.
  7752. If CLEANUP is non-nil, remove the printout of the regular expression
  7753. in the *Occur* buffer. This is useful if the regex is long and not useful
  7754. to read."
  7755. (occur regexp)
  7756. (when cleanup
  7757. (let ((cwin (selected-window)) win beg end)
  7758. (when (setq win (get-buffer-window "*Occur*"))
  7759. (select-window win))
  7760. (goto-char (point-min))
  7761. (when (re-search-forward "match[a-z]+" nil t)
  7762. (setq beg (match-end 0))
  7763. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7764. (setq end (1- (match-beginning 0)))))
  7765. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7766. (goto-char (point-min))
  7767. (select-window cwin))))
  7768. ;;; The mark ring for links jumps
  7769. (defvar org-mark-ring nil
  7770. "Mark ring for positions before jumps in Org-mode.")
  7771. (defvar org-mark-ring-last-goto nil
  7772. "Last position in the mark ring used to go back.")
  7773. ;; Fill and close the ring
  7774. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7775. (loop for i from 1 to org-mark-ring-length do
  7776. (push (make-marker) org-mark-ring))
  7777. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7778. org-mark-ring)
  7779. (defun org-mark-ring-push (&optional pos buffer)
  7780. "Put the current position or POS into the mark ring and rotate it."
  7781. (interactive)
  7782. (setq pos (or pos (point)))
  7783. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7784. (move-marker (car org-mark-ring)
  7785. (or pos (point))
  7786. (or buffer (current-buffer)))
  7787. (message "%s"
  7788. (substitute-command-keys
  7789. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7790. (defun org-mark-ring-goto (&optional n)
  7791. "Jump to the previous position in the mark ring.
  7792. With prefix arg N, jump back that many stored positions. When
  7793. called several times in succession, walk through the entire ring.
  7794. Org-mode commands jumping to a different position in the current file,
  7795. or to another Org-mode file, automatically push the old position
  7796. onto the ring."
  7797. (interactive "p")
  7798. (let (p m)
  7799. (if (eq last-command this-command)
  7800. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7801. (setq p org-mark-ring))
  7802. (setq org-mark-ring-last-goto p)
  7803. (setq m (car p))
  7804. (switch-to-buffer (marker-buffer m))
  7805. (goto-char m)
  7806. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7807. (defun org-remove-angle-brackets (s)
  7808. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7809. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7810. s)
  7811. (defun org-add-angle-brackets (s)
  7812. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7813. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7814. s)
  7815. (defun org-remove-double-quotes (s)
  7816. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7817. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7818. s)
  7819. ;;; Following specific links
  7820. (defun org-follow-timestamp-link ()
  7821. (cond
  7822. ((org-at-date-range-p t)
  7823. (let ((org-agenda-start-on-weekday)
  7824. (t1 (match-string 1))
  7825. (t2 (match-string 2)))
  7826. (setq t1 (time-to-days (org-time-string-to-time t1))
  7827. t2 (time-to-days (org-time-string-to-time t2)))
  7828. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7829. ((org-at-timestamp-p t)
  7830. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7831. (substring (match-string 1) 0 10)))
  7832. 1))
  7833. (t (error "This should not happen"))))
  7834. ;;; Following file links
  7835. (defvar org-wait nil)
  7836. (defun org-open-file (path &optional in-emacs line search)
  7837. "Open the file at PATH.
  7838. First, this expands any special file name abbreviations. Then the
  7839. configuration variable `org-file-apps' is checked if it contains an
  7840. entry for this file type, and if yes, the corresponding command is launched.
  7841. If no application is found, Emacs simply visits the file.
  7842. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7843. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7844. and o use an external application to visit the file.
  7845. Optional LINE specifies a line to go to, optional SEARCH a string to
  7846. search for. If LINE or SEARCH is given, the file will always be
  7847. opened in Emacs.
  7848. If the file does not exist, an error is thrown."
  7849. (setq in-emacs (or in-emacs line search))
  7850. (let* ((file (if (equal path "")
  7851. buffer-file-name
  7852. (substitute-in-file-name (expand-file-name path))))
  7853. (apps (append org-file-apps (org-default-apps)))
  7854. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7855. (dirp (if remp nil (file-directory-p file)))
  7856. (file (if (and dirp org-open-directory-means-index-dot-org)
  7857. (concat (file-name-as-directory file) "index.org")
  7858. file))
  7859. (a-m-a-p (assq 'auto-mode apps))
  7860. (dfile (downcase file))
  7861. (old-buffer (current-buffer))
  7862. (old-pos (point))
  7863. (old-mode major-mode)
  7864. ext cmd)
  7865. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7866. (setq ext (match-string 1 dfile))
  7867. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7868. (setq ext (match-string 1 dfile))))
  7869. (cond
  7870. ((equal in-emacs '(16))
  7871. (setq cmd (cdr (assoc 'system apps))))
  7872. (in-emacs (setq cmd 'emacs))
  7873. (t
  7874. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7875. (and dirp (cdr (assoc 'directory apps)))
  7876. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7877. 'string-match)
  7878. (cdr (assoc ext apps))
  7879. (cdr (assoc t apps))))))
  7880. (when (eq cmd 'system)
  7881. (setq cmd (cdr (assoc 'system apps))))
  7882. (when (eq cmd 'default)
  7883. (setq cmd (cdr (assoc t apps))))
  7884. (when (eq cmd 'mailcap)
  7885. (require 'mailcap)
  7886. (mailcap-parse-mailcaps)
  7887. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7888. (command (mailcap-mime-info mime-type)))
  7889. (if (stringp command)
  7890. (setq cmd command)
  7891. (setq cmd 'emacs))))
  7892. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7893. (not (file-exists-p file))
  7894. (not org-open-non-existing-files))
  7895. (error "No such file: %s" file))
  7896. (cond
  7897. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7898. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7899. (while (string-match "['\"]%s['\"]" cmd)
  7900. (setq cmd (replace-match "%s" t t cmd)))
  7901. (while (string-match "%s" cmd)
  7902. (setq cmd (replace-match
  7903. (save-match-data
  7904. (shell-quote-argument
  7905. (convert-standard-filename file)))
  7906. t t cmd)))
  7907. (save-window-excursion
  7908. (start-process-shell-command cmd nil cmd)
  7909. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7910. ))
  7911. ((or (stringp cmd)
  7912. (eq cmd 'emacs))
  7913. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7914. (widen)
  7915. (if line (org-goto-line line)
  7916. (if search (org-link-search search))))
  7917. ((consp cmd)
  7918. (let ((file (convert-standard-filename file)))
  7919. (eval cmd)))
  7920. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7921. (and (org-mode-p) (eq old-mode 'org-mode)
  7922. (or (not (equal old-buffer (current-buffer)))
  7923. (not (equal old-pos (point))))
  7924. (org-mark-ring-push old-pos old-buffer))))
  7925. (defun org-default-apps ()
  7926. "Return the default applications for this operating system."
  7927. (cond
  7928. ((eq system-type 'darwin)
  7929. org-file-apps-defaults-macosx)
  7930. ((eq system-type 'windows-nt)
  7931. org-file-apps-defaults-windowsnt)
  7932. (t org-file-apps-defaults-gnu)))
  7933. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7934. "Convert extensions to regular expressions in the cars of LIST.
  7935. Also, weed out any non-string entries, because the return value is used
  7936. only for regexp matching.
  7937. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7938. point to the symbol `emacs', indicating that the file should
  7939. be opened in Emacs."
  7940. (append
  7941. (delq nil
  7942. (mapcar (lambda (x)
  7943. (if (not (stringp (car x)))
  7944. nil
  7945. (if (string-match "\\W" (car x))
  7946. x
  7947. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7948. list))
  7949. (if add-auto-mode
  7950. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7951. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7952. (defun org-file-remote-p (file)
  7953. "Test whether FILE specifies a location on a remote system.
  7954. Return non-nil if the location is indeed remote.
  7955. For example, the filename \"/user@host:/foo\" specifies a location
  7956. on the system \"/user@host:\"."
  7957. (cond ((fboundp 'file-remote-p)
  7958. (file-remote-p file))
  7959. ((fboundp 'tramp-handle-file-remote-p)
  7960. (tramp-handle-file-remote-p file))
  7961. ((and (boundp 'ange-ftp-name-format)
  7962. (string-match (car ange-ftp-name-format) file))
  7963. t)
  7964. (t nil)))
  7965. ;;;; Refiling
  7966. (defun org-get-org-file ()
  7967. "Read a filename, with default directory `org-directory'."
  7968. (let ((default (or org-default-notes-file remember-data-file)))
  7969. (read-file-name (format "File name [%s]: " default)
  7970. (file-name-as-directory org-directory)
  7971. default)))
  7972. (defun org-notes-order-reversed-p ()
  7973. "Check if the current file should receive notes in reversed order."
  7974. (cond
  7975. ((not org-reverse-note-order) nil)
  7976. ((eq t org-reverse-note-order) t)
  7977. ((not (listp org-reverse-note-order)) nil)
  7978. (t (catch 'exit
  7979. (let ((all org-reverse-note-order)
  7980. entry)
  7981. (while (setq entry (pop all))
  7982. (if (string-match (car entry) buffer-file-name)
  7983. (throw 'exit (cdr entry))))
  7984. nil)))))
  7985. (defvar org-refile-target-table nil
  7986. "The list of refile targets, created by `org-refile'.")
  7987. (defvar org-agenda-new-buffers nil
  7988. "Buffers created to visit agenda files.")
  7989. (defun org-get-refile-targets (&optional default-buffer)
  7990. "Produce a table with refile targets."
  7991. (let ((case-fold-search nil)
  7992. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  7993. (entries (or org-refile-targets '((nil . (:level . 1)))))
  7994. targets txt re files f desc descre fast-path-p level pos0)
  7995. (message "Getting targets...")
  7996. (with-current-buffer (or default-buffer (current-buffer))
  7997. (while (setq entry (pop entries))
  7998. (setq files (car entry) desc (cdr entry))
  7999. (setq fast-path-p nil)
  8000. (cond
  8001. ((null files) (setq files (list (current-buffer))))
  8002. ((eq files 'org-agenda-files)
  8003. (setq files (org-agenda-files 'unrestricted)))
  8004. ((and (symbolp files) (fboundp files))
  8005. (setq files (funcall files)))
  8006. ((and (symbolp files) (boundp files))
  8007. (setq files (symbol-value files))))
  8008. (if (stringp files) (setq files (list files)))
  8009. (cond
  8010. ((eq (car desc) :tag)
  8011. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  8012. ((eq (car desc) :todo)
  8013. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  8014. ((eq (car desc) :regexp)
  8015. (setq descre (cdr desc)))
  8016. ((eq (car desc) :level)
  8017. (setq descre (concat "^\\*\\{" (number-to-string
  8018. (if org-odd-levels-only
  8019. (1- (* 2 (cdr desc)))
  8020. (cdr desc)))
  8021. "\\}[ \t]")))
  8022. ((eq (car desc) :maxlevel)
  8023. (setq fast-path-p t)
  8024. (setq descre (concat "^\\*\\{1," (number-to-string
  8025. (if org-odd-levels-only
  8026. (1- (* 2 (cdr desc)))
  8027. (cdr desc)))
  8028. "\\}[ \t]")))
  8029. (t (error "Bad refiling target description %s" desc)))
  8030. (while (setq f (pop files))
  8031. (with-current-buffer
  8032. (if (bufferp f) f (org-get-agenda-file-buffer f))
  8033. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  8034. (setq f (expand-file-name f))
  8035. (if (eq org-refile-use-outline-path 'file)
  8036. (push (list (file-name-nondirectory f) f nil nil) targets))
  8037. (save-excursion
  8038. (save-restriction
  8039. (widen)
  8040. (goto-char (point-min))
  8041. (while (re-search-forward descre nil t)
  8042. (goto-char (setq pos0 (point-at-bol)))
  8043. (catch 'next
  8044. (when org-refile-target-verify-function
  8045. (save-match-data
  8046. (or (funcall org-refile-target-verify-function)
  8047. (throw 'next t))))
  8048. (when (looking-at org-complex-heading-regexp)
  8049. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  8050. txt (org-link-display-format (match-string 4))
  8051. re (concat "^" (regexp-quote
  8052. (buffer-substring (match-beginning 1)
  8053. (match-end 4)))))
  8054. (if (match-end 5) (setq re (concat re "[ \t]+"
  8055. (regexp-quote
  8056. (match-string 5)))))
  8057. (setq re (concat re "[ \t]*$"))
  8058. (when org-refile-use-outline-path
  8059. (setq txt (mapconcat 'org-protect-slash
  8060. (append
  8061. (if (eq org-refile-use-outline-path 'file)
  8062. (list (file-name-nondirectory
  8063. (buffer-file-name (buffer-base-buffer))))
  8064. (if (eq org-refile-use-outline-path 'full-file-path)
  8065. (list (buffer-file-name (buffer-base-buffer)))))
  8066. (org-get-outline-path fast-path-p level txt)
  8067. (list txt))
  8068. "/")))
  8069. (push (list txt f re (point)) targets)))
  8070. (when (= (point) pos0)
  8071. ;; verification function has not moved point
  8072. (goto-char (point-at-eol))))))))))
  8073. (message "Getting targets...done")
  8074. (nreverse targets)))
  8075. (defun org-protect-slash (s)
  8076. (while (string-match "/" s)
  8077. (setq s (replace-match "\\" t t s)))
  8078. s)
  8079. (defvar org-olpa (make-vector 20 nil))
  8080. (defun org-get-outline-path (&optional fastp level heading)
  8081. "Return the outline path to the current entry, as a list."
  8082. (if fastp
  8083. (progn
  8084. (if (> level 19)
  8085. (error "Outline path failure, more than 19 levels."))
  8086. (loop for i from level upto 19 do
  8087. (aset org-olpa i nil))
  8088. (prog1
  8089. (delq nil (append org-olpa nil))
  8090. (aset org-olpa level heading)))
  8091. (let (rtn)
  8092. (save-excursion
  8093. (while (org-up-heading-safe)
  8094. (when (looking-at org-complex-heading-regexp)
  8095. (push (org-match-string-no-properties 4) rtn)))
  8096. rtn))))
  8097. (defvar org-refile-history nil
  8098. "History for refiling operations.")
  8099. (defvar org-after-refile-insert-hook nil
  8100. "Hook run after `org-refile' has inserted its stuff at the new location.
  8101. Note that this is still *before* the stuff will be removed from
  8102. the *old* location.")
  8103. (defun org-refile (&optional goto default-buffer rfloc)
  8104. "Move the entry at point to another heading.
  8105. The list of target headings is compiled using the information in
  8106. `org-refile-targets', which see. This list is created before each use
  8107. and will therefore always be up-to-date.
  8108. At the target location, the entry is filed as a subitem of the target heading.
  8109. Depending on `org-reverse-note-order', the new subitem will either be the
  8110. first or the last subitem.
  8111. If there is an active region, all entries in that region will be moved.
  8112. However, the region must fulfil the requirement that the first heading
  8113. is the first one sets the top-level of the moved text - at most siblings
  8114. below it are allowed.
  8115. With prefix arg GOTO, the command will only visit the target location,
  8116. not actually move anything.
  8117. With a double prefix `C-u C-u', go to the location where the last refiling
  8118. operation has put the subtree.
  8119. With a prefix argument of `2', refile to the running clock.
  8120. RFLOC can be a refile location obtained in a different way.
  8121. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  8122. (interactive "P")
  8123. (let* ((cbuf (current-buffer))
  8124. (regionp (org-region-active-p))
  8125. (region-start (and regionp (region-beginning)))
  8126. (region-end (and regionp (region-end)))
  8127. (region-length (and regionp (- region-end region-start)))
  8128. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  8129. pos it nbuf file re level reversed)
  8130. (when regionp
  8131. (goto-char region-start)
  8132. (or (bolp) (goto-char (point-at-bol)))
  8133. (setq region-start (point))
  8134. (unless (org-kill-is-subtree-p
  8135. (buffer-substring region-start region-end))
  8136. (error "The region is not a (sequence of) subtree(s)")))
  8137. (if (equal goto '(16))
  8138. (org-refile-goto-last-stored)
  8139. (when (or
  8140. (and (equal goto 2)
  8141. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  8142. (prog1
  8143. (setq it (list (or org-clock-heading "running clock")
  8144. (buffer-file-name
  8145. (marker-buffer org-clock-hd-marker))
  8146. ""
  8147. (marker-position org-clock-hd-marker)))
  8148. (setq goto nil)))
  8149. (setq it (or rfloc
  8150. (save-excursion
  8151. (org-refile-get-location
  8152. (if goto "Goto: " "Refile to: ") default-buffer
  8153. org-refile-allow-creating-parent-nodes)))))
  8154. (setq file (nth 1 it)
  8155. re (nth 2 it)
  8156. pos (nth 3 it))
  8157. (if (and (not goto)
  8158. pos
  8159. (equal (buffer-file-name) file)
  8160. (if regionp
  8161. (and (>= pos region-start)
  8162. (<= pos region-end))
  8163. (and (>= pos (point))
  8164. (< pos (save-excursion
  8165. (org-end-of-subtree t t))))))
  8166. (error "Cannot refile to position inside the tree or region"))
  8167. (setq nbuf (or (find-buffer-visiting file)
  8168. (find-file-noselect file)))
  8169. (if goto
  8170. (progn
  8171. (switch-to-buffer nbuf)
  8172. (goto-char pos)
  8173. (org-show-context 'org-goto))
  8174. (if regionp
  8175. (progn
  8176. (org-kill-new (buffer-substring region-start region-end))
  8177. (org-save-markers-in-region region-start region-end))
  8178. (org-copy-subtree 1 nil t))
  8179. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  8180. (find-file-noselect file)))
  8181. (setq reversed (org-notes-order-reversed-p))
  8182. (save-excursion
  8183. (save-restriction
  8184. (widen)
  8185. (if pos
  8186. (progn
  8187. (goto-char pos)
  8188. (looking-at outline-regexp)
  8189. (setq level (org-get-valid-level (funcall outline-level) 1))
  8190. (goto-char
  8191. (if reversed
  8192. (or (outline-next-heading) (point-max))
  8193. (or (save-excursion (org-get-next-sibling))
  8194. (org-end-of-subtree t t)
  8195. (point-max)))))
  8196. (setq level 1)
  8197. (if (not reversed)
  8198. (goto-char (point-max))
  8199. (goto-char (point-min))
  8200. (or (outline-next-heading) (goto-char (point-max)))))
  8201. (if (not (bolp)) (newline))
  8202. (bookmark-set "org-refile-last-stored")
  8203. (org-paste-subtree level)
  8204. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8205. (run-hooks 'org-after-refile-insert-hook))))
  8206. (if regionp
  8207. (delete-region (point) (+ (point) region-length))
  8208. (org-cut-subtree))
  8209. (when (featurep 'org-inlinetask)
  8210. (org-inlinetask-remove-END-maybe))
  8211. (setq org-markers-to-move nil)
  8212. (message "Refiled to \"%s\"" (car it))))))
  8213. (org-reveal))
  8214. (defun org-refile-goto-last-stored ()
  8215. "Go to the location where the last refile was stored."
  8216. (interactive)
  8217. (bookmark-jump "org-refile-last-stored")
  8218. (message "This is the location of the last refile"))
  8219. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  8220. "Prompt the user for a refile location, using PROMPT."
  8221. (let ((org-refile-targets org-refile-targets)
  8222. (org-refile-use-outline-path org-refile-use-outline-path))
  8223. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  8224. (unless org-refile-target-table
  8225. (error "No refile targets"))
  8226. (let* ((cbuf (current-buffer))
  8227. (partial-completion-mode nil)
  8228. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  8229. (cfunc (if (and org-refile-use-outline-path
  8230. org-outline-path-complete-in-steps)
  8231. 'org-olpath-completing-read
  8232. 'org-icompleting-read))
  8233. (extra (if org-refile-use-outline-path "/" ""))
  8234. (filename (and cfn (expand-file-name cfn)))
  8235. (tbl (mapcar
  8236. (lambda (x)
  8237. (if (and (not (member org-refile-use-outline-path
  8238. '(file full-file-path)))
  8239. (not (equal filename (nth 1 x))))
  8240. (cons (concat (car x) extra " ("
  8241. (file-name-nondirectory (nth 1 x)) ")")
  8242. (cdr x))
  8243. (cons (concat (car x) extra) (cdr x))))
  8244. org-refile-target-table))
  8245. (completion-ignore-case t)
  8246. pa answ parent-target child parent old-hist)
  8247. (setq old-hist org-refile-history)
  8248. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  8249. nil 'org-refile-history))
  8250. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  8251. (if pa
  8252. (progn
  8253. (when (or (not org-refile-history)
  8254. (not (eq old-hist org-refile-history))
  8255. (not (equal (car pa) (car org-refile-history))))
  8256. (setq org-refile-history
  8257. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  8258. org-refile-history
  8259. (cdr org-refile-history))))
  8260. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  8261. (pop org-refile-history)))
  8262. pa)
  8263. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8264. (setq parent (match-string 1 answ)
  8265. child (match-string 2 answ))
  8266. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  8267. (when (and parent-target
  8268. (or (eq new-nodes t)
  8269. (and (eq new-nodes 'confirm)
  8270. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  8271. (org-refile-new-child parent-target child))))))
  8272. (defun org-refile-new-child (parent-target child)
  8273. "Use refile target PARENT-TARGET to add new CHILD below it."
  8274. (unless parent-target
  8275. (error "Cannot find parent for new node"))
  8276. (let ((file (nth 1 parent-target))
  8277. (pos (nth 3 parent-target))
  8278. level)
  8279. (with-current-buffer (or (find-buffer-visiting file)
  8280. (find-file-noselect file))
  8281. (save-excursion
  8282. (save-restriction
  8283. (widen)
  8284. (if pos
  8285. (goto-char pos)
  8286. (goto-char (point-max))
  8287. (if (not (bolp)) (newline)))
  8288. (when (looking-at outline-regexp)
  8289. (setq level (funcall outline-level))
  8290. (org-end-of-subtree t t))
  8291. (org-back-over-empty-lines)
  8292. (insert "\n" (make-string
  8293. (if pos (org-get-valid-level level 1) 1) ?*)
  8294. " " child "\n")
  8295. (beginning-of-line 0)
  8296. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8297. (defun org-olpath-completing-read (prompt collection &rest args)
  8298. "Read an outline path like a file name."
  8299. (let ((thetable collection)
  8300. (org-completion-use-ido nil) ; does not work with ido.
  8301. (org-completion-use-iswitchb nil)) ; or iswitchb
  8302. (apply
  8303. 'org-icompleting-read prompt
  8304. (lambda (string predicate &optional flag)
  8305. (let (rtn r f (l (length string)))
  8306. (cond
  8307. ((eq flag nil)
  8308. ;; try completion
  8309. (try-completion string thetable))
  8310. ((eq flag t)
  8311. ;; all-completions
  8312. (setq rtn (all-completions string thetable predicate))
  8313. (mapcar
  8314. (lambda (x)
  8315. (setq r (substring x l))
  8316. (if (string-match " ([^)]*)$" x)
  8317. (setq f (match-string 0 x))
  8318. (setq f ""))
  8319. (if (string-match "/" r)
  8320. (concat string (substring r 0 (match-end 0)) f)
  8321. x))
  8322. rtn))
  8323. ((eq flag 'lambda)
  8324. ;; exact match?
  8325. (assoc string thetable)))
  8326. ))
  8327. args)))
  8328. ;;;; Dynamic blocks
  8329. (defun org-find-dblock (name)
  8330. "Find the first dynamic block with name NAME in the buffer.
  8331. If not found, stay at current position and return nil."
  8332. (let (pos)
  8333. (save-excursion
  8334. (goto-char (point-min))
  8335. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8336. nil t)
  8337. (match-beginning 0))))
  8338. (if pos (goto-char pos))
  8339. pos))
  8340. (defconst org-dblock-start-re
  8341. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8342. "Matches the startline of a dynamic block, with parameters.")
  8343. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  8344. "Matches the end of a dynamic block.")
  8345. (defun org-create-dblock (plist)
  8346. "Create a dynamic block section, with parameters taken from PLIST.
  8347. PLIST must contain a :name entry which is used as name of the block."
  8348. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  8349. (end-of-line 1)
  8350. (newline))
  8351. (let ((col (current-column))
  8352. (name (plist-get plist :name)))
  8353. (insert "#+BEGIN: " name)
  8354. (while plist
  8355. (if (eq (car plist) :name)
  8356. (setq plist (cddr plist))
  8357. (insert " " (prin1-to-string (pop plist)))))
  8358. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  8359. (beginning-of-line -2)))
  8360. (defun org-prepare-dblock ()
  8361. "Prepare dynamic block for refresh.
  8362. This empties the block, puts the cursor at the insert position and returns
  8363. the property list including an extra property :name with the block name."
  8364. (unless (looking-at org-dblock-start-re)
  8365. (error "Not at a dynamic block"))
  8366. (let* ((begdel (1+ (match-end 0)))
  8367. (name (org-no-properties (match-string 1)))
  8368. (params (append (list :name name)
  8369. (read (concat "(" (match-string 3) ")")))))
  8370. (save-excursion
  8371. (beginning-of-line 1)
  8372. (skip-chars-forward " \t")
  8373. (setq params (plist-put params :indentation-column (current-column))))
  8374. (unless (re-search-forward org-dblock-end-re nil t)
  8375. (error "Dynamic block not terminated"))
  8376. (setq params
  8377. (append params
  8378. (list :content (buffer-substring
  8379. begdel (match-beginning 0)))))
  8380. (delete-region begdel (match-beginning 0))
  8381. (goto-char begdel)
  8382. (open-line 1)
  8383. params))
  8384. (defun org-map-dblocks (&optional command)
  8385. "Apply COMMAND to all dynamic blocks in the current buffer.
  8386. If COMMAND is not given, use `org-update-dblock'."
  8387. (let ((cmd (or command 'org-update-dblock))
  8388. pos)
  8389. (save-excursion
  8390. (goto-char (point-min))
  8391. (while (re-search-forward org-dblock-start-re nil t)
  8392. (goto-char (setq pos (match-beginning 0)))
  8393. (condition-case nil
  8394. (funcall cmd)
  8395. (error (message "Error during update of dynamic block")))
  8396. (goto-char pos)
  8397. (unless (re-search-forward org-dblock-end-re nil t)
  8398. (error "Dynamic block not terminated"))))))
  8399. (defun org-dblock-update (&optional arg)
  8400. "User command for updating dynamic blocks.
  8401. Update the dynamic block at point. With prefix ARG, update all dynamic
  8402. blocks in the buffer."
  8403. (interactive "P")
  8404. (if arg
  8405. (org-update-all-dblocks)
  8406. (or (looking-at org-dblock-start-re)
  8407. (org-beginning-of-dblock))
  8408. (org-update-dblock)))
  8409. (defun org-update-dblock ()
  8410. "Update the dynamic block at point
  8411. This means to empty the block, parse for parameters and then call
  8412. the correct writing function."
  8413. (save-window-excursion
  8414. (let* ((pos (point))
  8415. (line (org-current-line))
  8416. (params (org-prepare-dblock))
  8417. (name (plist-get params :name))
  8418. (indent (plist-get params :indentation-column))
  8419. (cmd (intern (concat "org-dblock-write:" name))))
  8420. (message "Updating dynamic block `%s' at line %d..." name line)
  8421. (funcall cmd params)
  8422. (message "Updating dynamic block `%s' at line %d...done" name line)
  8423. (goto-char pos)
  8424. (when (and indent (> indent 0))
  8425. (setq indent (make-string indent ?\ ))
  8426. (save-excursion
  8427. (org-beginning-of-dblock)
  8428. (forward-line 1)
  8429. (while (not (looking-at org-dblock-end-re))
  8430. (insert indent)
  8431. (beginning-of-line 2))
  8432. (when (looking-at org-dblock-end-re)
  8433. (and (looking-at "[ \t]+")
  8434. (replace-match ""))
  8435. (insert indent)))))))
  8436. (defun org-beginning-of-dblock ()
  8437. "Find the beginning of the dynamic block at point.
  8438. Error if there is no such block at point."
  8439. (let ((pos (point))
  8440. beg)
  8441. (end-of-line 1)
  8442. (if (and (re-search-backward org-dblock-start-re nil t)
  8443. (setq beg (match-beginning 0))
  8444. (re-search-forward org-dblock-end-re nil t)
  8445. (> (match-end 0) pos))
  8446. (goto-char beg)
  8447. (goto-char pos)
  8448. (error "Not in a dynamic block"))))
  8449. (defun org-update-all-dblocks ()
  8450. "Update all dynamic blocks in the buffer.
  8451. This function can be used in a hook."
  8452. (when (org-mode-p)
  8453. (org-map-dblocks 'org-update-dblock)))
  8454. ;;;; Completion
  8455. (defconst org-additional-option-like-keywords
  8456. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8457. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8458. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:" "LATEX_CLASS:" "ATTR_LaTeX"
  8459. "BEGIN:" "END:"
  8460. "ORGTBL" "TBLFM:" "TBLNAME:"
  8461. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8462. "BEGIN_QUOTE" "END_QUOTE"
  8463. "BEGIN_VERSE" "END_VERSE"
  8464. "BEGIN_CENTER" "END_CENTER"
  8465. "BEGIN_SRC" "END_SRC"
  8466. "CATEGORY" "COLUMNS"
  8467. "CAPTION" "LABEL"
  8468. "SETUPFILE"
  8469. "BIND"
  8470. "MACRO"))
  8471. (defcustom org-structure-template-alist
  8472. '(
  8473. ("s" "#+begin_src ?\n\n#+end_src"
  8474. "<src lang=\"?\">\n\n</src>")
  8475. ("e" "#+begin_example\n?\n#+end_example"
  8476. "<example>\n?\n</example>")
  8477. ("q" "#+begin_quote\n?\n#+end_quote"
  8478. "<quote>\n?\n</quote>")
  8479. ("v" "#+begin_verse\n?\n#+end_verse"
  8480. "<verse>\n?\n/verse>")
  8481. ("c" "#+begin_center\n?\n#+end_center"
  8482. "<center>\n?\n/center>")
  8483. ("l" "#+begin_latex\n?\n#+end_latex"
  8484. "<literal style=\"latex\">\n?\n</literal>")
  8485. ("L" "#+latex: "
  8486. "<literal style=\"latex\">?</literal>")
  8487. ("h" "#+begin_html\n?\n#+end_html"
  8488. "<literal style=\"html\">\n?\n</literal>")
  8489. ("H" "#+html: "
  8490. "<literal style=\"html\">?</literal>")
  8491. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8492. ("A" "#+ascii: ")
  8493. ("i" "#+include %file ?"
  8494. "<include file=%file markup=\"?\">")
  8495. )
  8496. "Structure completion elements.
  8497. This is a list of abbreviation keys and values. The value gets inserted
  8498. it you type @samp{.} followed by the key and then the completion key,
  8499. usually `M-TAB'. %file will be replaced by a file name after prompting
  8500. for the file using completion.
  8501. There are two templates for each key, the first uses the original Org syntax,
  8502. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8503. the default when the /org-mtags.el/ module has been loaded. See also the
  8504. variable `org-mtags-prefer-muse-templates'.
  8505. This is an experimental feature, it is undecided if it is going to stay in."
  8506. :group 'org-completion
  8507. :type '(repeat
  8508. (string :tag "Key")
  8509. (string :tag "Template")
  8510. (string :tag "Muse Template")))
  8511. (defun org-try-structure-completion ()
  8512. "Try to complete a structure template before point.
  8513. This looks for strings like \"<e\" on an otherwise empty line and
  8514. expands them."
  8515. (let ((l (buffer-substring (point-at-bol) (point)))
  8516. a)
  8517. (when (and (looking-at "[ \t]*$")
  8518. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8519. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8520. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8521. (match-beginning 1)) a)
  8522. t)))
  8523. (defun org-complete-expand-structure-template (start cell)
  8524. "Expand a structure template."
  8525. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8526. (rpl (nth (if musep 2 1) cell))
  8527. (ind ""))
  8528. (delete-region start (point))
  8529. (when (string-match "\\`#\\+" rpl)
  8530. (cond
  8531. ((bolp))
  8532. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8533. (setq ind (buffer-substring (point-at-bol) (point))))
  8534. (t (newline))))
  8535. (setq start (point))
  8536. (if (string-match "%file" rpl)
  8537. (setq rpl (replace-match
  8538. (concat
  8539. "\""
  8540. (save-match-data
  8541. (abbreviate-file-name (read-file-name "Include file: ")))
  8542. "\"")
  8543. t t rpl)))
  8544. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  8545. (concat "\n" ind)))
  8546. (insert rpl)
  8547. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8548. (defun org-complete (&optional arg)
  8549. "Perform completion on word at point.
  8550. At the beginning of a headline, this completes TODO keywords as given in
  8551. `org-todo-keywords'.
  8552. If the current word is preceded by a backslash, completes the TeX symbols
  8553. that are supported for HTML support.
  8554. If the current word is preceded by \"#+\", completes special words for
  8555. setting file options.
  8556. In the line after \"#+STARTUP:, complete valid keywords.\"
  8557. At all other locations, this simply calls the value of
  8558. `org-completion-fallback-command'."
  8559. (interactive "P")
  8560. (org-without-partial-completion
  8561. (catch 'exit
  8562. (let* ((a nil)
  8563. (end (point))
  8564. (beg1 (save-excursion
  8565. (skip-chars-backward (org-re "[:alnum:]_@"))
  8566. (point)))
  8567. (beg (save-excursion
  8568. (skip-chars-backward "a-zA-Z0-9_:$")
  8569. (point)))
  8570. (confirm (lambda (x) (stringp (car x))))
  8571. (searchhead (equal (char-before beg) ?*))
  8572. (struct
  8573. (when (and (member (char-before beg1) '(?. ?<))
  8574. (setq a (assoc (buffer-substring beg1 (point))
  8575. org-structure-template-alist)))
  8576. (org-complete-expand-structure-template (1- beg1) a)
  8577. (throw 'exit t)))
  8578. (tag (and (equal (char-before beg1) ?:)
  8579. (equal (char-after (point-at-bol)) ?*)))
  8580. (prop (and (equal (char-before beg1) ?:)
  8581. (not (equal (char-after (point-at-bol)) ?*))))
  8582. (texp (equal (char-before beg) ?\\))
  8583. (link (equal (char-before beg) ?\[))
  8584. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8585. beg)
  8586. "#+"))
  8587. (startup (string-match "^#\\+STARTUP:.*"
  8588. (buffer-substring (point-at-bol) (point))))
  8589. (completion-ignore-case opt)
  8590. (type nil)
  8591. (tbl nil)
  8592. (table (cond
  8593. (opt
  8594. (setq type :opt)
  8595. (require 'org-exp)
  8596. (append
  8597. (delq nil
  8598. (mapcar
  8599. (lambda (x)
  8600. (if (string-match
  8601. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8602. (cons (match-string 2 x)
  8603. (match-string 1 x))))
  8604. (org-split-string (org-get-current-options) "\n")))
  8605. (mapcar 'list org-additional-option-like-keywords)))
  8606. (startup
  8607. (setq type :startup)
  8608. org-startup-options)
  8609. (link (append org-link-abbrev-alist-local
  8610. org-link-abbrev-alist))
  8611. (texp
  8612. (setq type :tex)
  8613. org-html-entities)
  8614. ((string-match "\\`\\*+[ \t]+\\'"
  8615. (buffer-substring (point-at-bol) beg))
  8616. (setq type :todo)
  8617. (mapcar 'list org-todo-keywords-1))
  8618. (searchhead
  8619. (setq type :searchhead)
  8620. (save-excursion
  8621. (goto-char (point-min))
  8622. (while (re-search-forward org-todo-line-regexp nil t)
  8623. (push (list
  8624. (org-make-org-heading-search-string
  8625. (match-string 3) t))
  8626. tbl)))
  8627. tbl)
  8628. (tag (setq type :tag beg beg1)
  8629. (or org-tag-alist (org-get-buffer-tags)))
  8630. (prop (setq type :prop beg beg1)
  8631. (mapcar 'list (org-buffer-property-keys nil t t)))
  8632. (t (progn
  8633. (call-interactively org-completion-fallback-command)
  8634. (throw 'exit nil)))))
  8635. (pattern (buffer-substring-no-properties beg end))
  8636. (completion (try-completion pattern table confirm)))
  8637. (cond ((eq completion t)
  8638. (if (not (assoc (upcase pattern) table))
  8639. (message "Already complete")
  8640. (if (and (equal type :opt)
  8641. (not (member (car (assoc (upcase pattern) table))
  8642. org-additional-option-like-keywords)))
  8643. (insert (substring (cdr (assoc (upcase pattern) table))
  8644. (length pattern)))
  8645. (if (memq type '(:tag :prop)) (insert ":")))))
  8646. ((null completion)
  8647. (message "Can't find completion for \"%s\"" pattern)
  8648. (ding))
  8649. ((not (string= pattern completion))
  8650. (delete-region beg end)
  8651. (if (string-match " +$" completion)
  8652. (setq completion (replace-match "" t t completion)))
  8653. (insert completion)
  8654. (if (get-buffer-window "*Completions*")
  8655. (delete-window (get-buffer-window "*Completions*")))
  8656. (if (assoc completion table)
  8657. (if (eq type :todo) (insert " ")
  8658. (if (memq type '(:tag :prop)) (insert ":"))))
  8659. (if (and (equal type :opt) (assoc completion table))
  8660. (message "%s" (substitute-command-keys
  8661. "Press \\[org-complete] again to insert example settings"))))
  8662. (t
  8663. (message "Making completion list...")
  8664. (let ((list (sort (all-completions pattern table confirm)
  8665. 'string<)))
  8666. (with-output-to-temp-buffer "*Completions*"
  8667. (condition-case nil
  8668. ;; Protection needed for XEmacs and emacs 21
  8669. (display-completion-list list pattern)
  8670. (error (display-completion-list list)))))
  8671. (message "Making completion list...%s" "done")))))))
  8672. ;;;; TODO, DEADLINE, Comments
  8673. (defun org-toggle-comment ()
  8674. "Change the COMMENT state of an entry."
  8675. (interactive)
  8676. (save-excursion
  8677. (org-back-to-heading)
  8678. (let (case-fold-search)
  8679. (if (looking-at (concat outline-regexp
  8680. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8681. (replace-match "" t t nil 1)
  8682. (if (looking-at outline-regexp)
  8683. (progn
  8684. (goto-char (match-end 0))
  8685. (insert org-comment-string " ")))))))
  8686. (defvar org-last-todo-state-is-todo nil
  8687. "This is non-nil when the last TODO state change led to a TODO state.
  8688. If the last change removed the TODO tag or switched to DONE, then
  8689. this is nil.")
  8690. (defvar org-setting-tags nil) ; dynamically skipped
  8691. (defun org-parse-local-options (string var)
  8692. "Parse STRING for startup setting relevant for variable VAR."
  8693. (let ((rtn (symbol-value var))
  8694. e opts)
  8695. (save-match-data
  8696. (if (or (not string) (not (string-match "\\S-" string)))
  8697. rtn
  8698. (setq opts (delq nil (mapcar (lambda (x)
  8699. (setq e (assoc x org-startup-options))
  8700. (if (eq (nth 1 e) var) e nil))
  8701. (org-split-string string "[ \t]+"))))
  8702. (if (not opts)
  8703. rtn
  8704. (setq rtn nil)
  8705. (while (setq e (pop opts))
  8706. (if (not (nth 3 e))
  8707. (setq rtn (nth 2 e))
  8708. (if (not (listp rtn)) (setq rtn nil))
  8709. (push (nth 2 e) rtn)))
  8710. rtn)))))
  8711. (defvar org-todo-setup-filter-hook nil
  8712. "Hook for functions that pre-filter todo specs.
  8713. Each function takes a todo spec and returns either `nil' or the spec
  8714. transformed into canonical form." )
  8715. (defvar org-todo-get-default-hook nil
  8716. "Hook for functions that get a default item for todo.
  8717. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8718. `nil' or a string to be used for the todo mark." )
  8719. (defvar org-agenda-headline-snapshot-before-repeat)
  8720. (defun org-todo (&optional arg)
  8721. "Change the TODO state of an item.
  8722. The state of an item is given by a keyword at the start of the heading,
  8723. like
  8724. *** TODO Write paper
  8725. *** DONE Call mom
  8726. The different keywords are specified in the variable `org-todo-keywords'.
  8727. By default the available states are \"TODO\" and \"DONE\".
  8728. So for this example: when the item starts with TODO, it is changed to DONE.
  8729. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8730. DONE are present, add TODO at the beginning of the heading.
  8731. With C-u prefix arg, use completion to determine the new state.
  8732. With numeric prefix arg, switch to that state.
  8733. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8734. With a tripple C-u prefix, circumvent any state blocking.
  8735. For calling through lisp, arg is also interpreted in the following way:
  8736. 'none -> empty state
  8737. \"\"(empty string) -> switch to empty state
  8738. 'done -> switch to DONE
  8739. 'nextset -> switch to the next set of keywords
  8740. 'previousset -> switch to the previous set of keywords
  8741. \"WAITING\" -> switch to the specified keyword, but only if it
  8742. really is a member of `org-todo-keywords'."
  8743. (interactive "P")
  8744. (if (equal arg '(16)) (setq arg 'nextset))
  8745. (let ((org-blocker-hook org-blocker-hook)
  8746. (case-fold-search nil))
  8747. (when (equal arg '(64))
  8748. (setq arg nil org-blocker-hook nil))
  8749. (when (and org-blocker-hook
  8750. (or org-inhibit-blocking
  8751. (org-entry-get nil "NOBLOCKING")))
  8752. (setq org-blocker-hook nil))
  8753. (save-excursion
  8754. (catch 'exit
  8755. (org-back-to-heading t)
  8756. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8757. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8758. (looking-at " *"))
  8759. (let* ((match-data (match-data))
  8760. (startpos (point-at-bol))
  8761. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8762. (org-log-done org-log-done)
  8763. (org-log-repeat org-log-repeat)
  8764. (org-todo-log-states org-todo-log-states)
  8765. (this (match-string 1))
  8766. (hl-pos (match-beginning 0))
  8767. (head (org-get-todo-sequence-head this))
  8768. (ass (assoc head org-todo-kwd-alist))
  8769. (interpret (nth 1 ass))
  8770. (done-word (nth 3 ass))
  8771. (final-done-word (nth 4 ass))
  8772. (last-state (or this ""))
  8773. (completion-ignore-case t)
  8774. (member (member this org-todo-keywords-1))
  8775. (tail (cdr member))
  8776. (state (cond
  8777. ((and org-todo-key-trigger
  8778. (or (and (equal arg '(4))
  8779. (eq org-use-fast-todo-selection 'prefix))
  8780. (and (not arg) org-use-fast-todo-selection
  8781. (not (eq org-use-fast-todo-selection
  8782. 'prefix)))))
  8783. ;; Use fast selection
  8784. (org-fast-todo-selection))
  8785. ((and (equal arg '(4))
  8786. (or (not org-use-fast-todo-selection)
  8787. (not org-todo-key-trigger)))
  8788. ;; Read a state with completion
  8789. (org-icompleting-read
  8790. "State: " (mapcar (lambda(x) (list x))
  8791. org-todo-keywords-1)
  8792. nil t))
  8793. ((eq arg 'right)
  8794. (if this
  8795. (if tail (car tail) nil)
  8796. (car org-todo-keywords-1)))
  8797. ((eq arg 'left)
  8798. (if (equal member org-todo-keywords-1)
  8799. nil
  8800. (if this
  8801. (nth (- (length org-todo-keywords-1)
  8802. (length tail) 2)
  8803. org-todo-keywords-1)
  8804. (org-last org-todo-keywords-1))))
  8805. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8806. (setq arg nil))) ; hack to fall back to cycling
  8807. (arg
  8808. ;; user or caller requests a specific state
  8809. (cond
  8810. ((equal arg "") nil)
  8811. ((eq arg 'none) nil)
  8812. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8813. ((eq arg 'nextset)
  8814. (or (car (cdr (member head org-todo-heads)))
  8815. (car org-todo-heads)))
  8816. ((eq arg 'previousset)
  8817. (let ((org-todo-heads (reverse org-todo-heads)))
  8818. (or (car (cdr (member head org-todo-heads)))
  8819. (car org-todo-heads))))
  8820. ((car (member arg org-todo-keywords-1)))
  8821. ((stringp arg)
  8822. (error "State `%s' not valid in this file" arg))
  8823. ((nth (1- (prefix-numeric-value arg))
  8824. org-todo-keywords-1))))
  8825. ((null member) (or head (car org-todo-keywords-1)))
  8826. ((equal this final-done-word) nil) ;; -> make empty
  8827. ((null tail) nil) ;; -> first entry
  8828. ((memq interpret '(type priority))
  8829. (if (eq this-command last-command)
  8830. (car tail)
  8831. (if (> (length tail) 0)
  8832. (or done-word (car org-done-keywords))
  8833. nil)))
  8834. (t
  8835. (car tail))))
  8836. (state (or
  8837. (run-hook-with-args-until-success
  8838. 'org-todo-get-default-hook state last-state)
  8839. state))
  8840. (next (if state (concat " " state " ") " "))
  8841. (change-plist (list :type 'todo-state-change :from this :to state
  8842. :position startpos))
  8843. dolog now-done-p)
  8844. (when org-blocker-hook
  8845. (setq org-last-todo-state-is-todo
  8846. (not (member this org-done-keywords)))
  8847. (unless (save-excursion
  8848. (save-match-data
  8849. (run-hook-with-args-until-failure
  8850. 'org-blocker-hook change-plist)))
  8851. (if (interactive-p)
  8852. (error "TODO state change from %s to %s blocked" this state)
  8853. ;; fail silently
  8854. (message "TODO state change from %s to %s blocked" this state)
  8855. (throw 'exit nil))))
  8856. (store-match-data match-data)
  8857. (replace-match next t t)
  8858. (unless (pos-visible-in-window-p hl-pos)
  8859. (message "TODO state changed to %s" (org-trim next)))
  8860. (unless head
  8861. (setq head (org-get-todo-sequence-head state)
  8862. ass (assoc head org-todo-kwd-alist)
  8863. interpret (nth 1 ass)
  8864. done-word (nth 3 ass)
  8865. final-done-word (nth 4 ass)))
  8866. (when (memq arg '(nextset previousset))
  8867. (message "Keyword-Set %d/%d: %s"
  8868. (- (length org-todo-sets) -1
  8869. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8870. (length org-todo-sets)
  8871. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8872. (setq org-last-todo-state-is-todo
  8873. (not (member state org-done-keywords)))
  8874. (setq now-done-p (and (member state org-done-keywords)
  8875. (not (member this org-done-keywords))))
  8876. (and logging (org-local-logging logging))
  8877. (when (and (or org-todo-log-states org-log-done)
  8878. (not (eq org-inhibit-logging t))
  8879. (not (memq arg '(nextset previousset))))
  8880. ;; we need to look at recording a time and note
  8881. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8882. (nth 2 (assoc this org-todo-log-states))))
  8883. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  8884. (setq dolog 'time))
  8885. (when (and state
  8886. (member state org-not-done-keywords)
  8887. (not (member this org-not-done-keywords)))
  8888. ;; This is now a todo state and was not one before
  8889. ;; If there was a CLOSED time stamp, get rid of it.
  8890. (org-add-planning-info nil nil 'closed))
  8891. (when (and now-done-p org-log-done)
  8892. ;; It is now done, and it was not done before
  8893. (org-add-planning-info 'closed (org-current-time))
  8894. (if (and (not dolog) (eq 'note org-log-done))
  8895. (org-add-log-setup 'done state this 'findpos 'note)))
  8896. (when (and state dolog)
  8897. ;; This is a non-nil state, and we need to log it
  8898. (org-add-log-setup 'state state this 'findpos dolog)))
  8899. ;; Fixup tag positioning
  8900. (org-todo-trigger-tag-changes state)
  8901. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8902. (when org-provide-todo-statistics
  8903. (org-update-parent-todo-statistics))
  8904. (run-hooks 'org-after-todo-state-change-hook)
  8905. (if (and arg (not (member state org-done-keywords)))
  8906. (setq head (org-get-todo-sequence-head state)))
  8907. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8908. ;; Do we need to trigger a repeat?
  8909. (when now-done-p
  8910. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8911. ;; This is for the agenda, take a snapshot of the headline.
  8912. (save-match-data
  8913. (setq org-agenda-headline-snapshot-before-repeat
  8914. (org-get-heading))))
  8915. (org-auto-repeat-maybe state))
  8916. ;; Fixup cursor location if close to the keyword
  8917. (if (and (outline-on-heading-p)
  8918. (not (bolp))
  8919. (save-excursion (beginning-of-line 1)
  8920. (looking-at org-todo-line-regexp))
  8921. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8922. (progn
  8923. (goto-char (or (match-end 2) (match-end 1)))
  8924. (and (looking-at " ") (just-one-space))))
  8925. (when org-trigger-hook
  8926. (save-excursion
  8927. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8928. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8929. "Block turning an entry into a TODO, using the hierarchy.
  8930. This checks whether the current task should be blocked from state
  8931. changes. Such blocking occurs when:
  8932. 1. The task has children which are not all in a completed state.
  8933. 2. A task has a parent with the property :ORDERED:, and there
  8934. are siblings prior to the current task with incomplete
  8935. status.
  8936. 3. The parent of the task is blocked because it has siblings that should
  8937. be done first, or is child of a block grandparent TODO entry."
  8938. (catch 'dont-block
  8939. ;; If this is not a todo state change, or if this entry is already DONE,
  8940. ;; do not block
  8941. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8942. (member (plist-get change-plist :from)
  8943. (cons 'done org-done-keywords))
  8944. (member (plist-get change-plist :to)
  8945. (cons 'todo org-not-done-keywords)))
  8946. (throw 'dont-block t))
  8947. ;; If this task has children, and any are undone, it's blocked
  8948. (save-excursion
  8949. (org-back-to-heading t)
  8950. (let ((this-level (funcall outline-level)))
  8951. (outline-next-heading)
  8952. (let ((child-level (funcall outline-level)))
  8953. (while (and (not (eobp))
  8954. (> child-level this-level))
  8955. ;; this todo has children, check whether they are all
  8956. ;; completed
  8957. (if (and (not (org-entry-is-done-p))
  8958. (org-entry-is-todo-p))
  8959. (throw 'dont-block nil))
  8960. (outline-next-heading)
  8961. (setq child-level (funcall outline-level))))))
  8962. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8963. ;; any previous siblings are undone, it's blocked
  8964. (save-excursion
  8965. (org-back-to-heading t)
  8966. (let* ((pos (point))
  8967. (parent-pos (and (org-up-heading-safe) (point))))
  8968. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8969. (when (and (org-entry-get (point) "ORDERED")
  8970. (forward-line 1)
  8971. (re-search-forward org-not-done-heading-regexp pos t))
  8972. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8973. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8974. (while t
  8975. (goto-char parent-pos)
  8976. (if (not (looking-at org-not-done-heading-regexp))
  8977. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8978. (setq pos (point))
  8979. (setq parent-pos (and (org-up-heading-safe) (point)))
  8980. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8981. (when (and (org-entry-get (point) "ORDERED")
  8982. (forward-line 1)
  8983. (re-search-forward org-not-done-heading-regexp pos t))
  8984. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8985. (defcustom org-track-ordered-property-with-tag nil
  8986. "Should the ORDERED property also be shown as a tag?
  8987. The ORDERED property decides if an entry should require subtasks to be
  8988. completed in sequence. Since a property is not very visible, setting
  8989. this option means that toggling the ORDERED property with the command
  8990. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8991. not relevant for the behavior, but it makes things more visible.
  8992. Note that toggling the tag with tags commands will not change the property
  8993. and therefore not influence behavior!
  8994. This can be t, meaning the tag ORDERED should be used, It can also be a
  8995. string to select a different tag for this task."
  8996. :group 'org-todo
  8997. :type '(choice
  8998. (const :tag "No tracking" nil)
  8999. (const :tag "Track with ORDERED tag" t)
  9000. (string :tag "Use other tag")))
  9001. (defun org-toggle-ordered-property ()
  9002. "Toggle the ORDERED property of the current entry.
  9003. For better visibility, you can track the value of this property with a tag.
  9004. See variable `org-track-ordered-property-with-tag'."
  9005. (interactive)
  9006. (let* ((t1 org-track-ordered-property-with-tag)
  9007. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  9008. (save-excursion
  9009. (org-back-to-heading)
  9010. (if (org-entry-get nil "ORDERED")
  9011. (progn
  9012. (org-delete-property "ORDERED")
  9013. (and tag (org-toggle-tag tag 'off))
  9014. (message "Subtasks can be completed in arbitrary order"))
  9015. (org-entry-put nil "ORDERED" "t")
  9016. (and tag (org-toggle-tag tag 'on))
  9017. (message "Subtasks must be completed in sequence")))))
  9018. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  9019. (defun org-block-todo-from-checkboxes (change-plist)
  9020. "Block turning an entry into a TODO, using checkboxes.
  9021. This checks whether the current task should be blocked from state
  9022. changes because there are uncheckd boxes in this entry."
  9023. (catch 'dont-block
  9024. ;; If this is not a todo state change, or if this entry is already DONE,
  9025. ;; do not block
  9026. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9027. (member (plist-get change-plist :from)
  9028. (cons 'done org-done-keywords))
  9029. (member (plist-get change-plist :to)
  9030. (cons 'todo org-not-done-keywords)))
  9031. (throw 'dont-block t))
  9032. ;; If this task has checkboxes that are not checked, it's blocked
  9033. (save-excursion
  9034. (org-back-to-heading t)
  9035. (let ((beg (point)) end)
  9036. (outline-next-heading)
  9037. (setq end (point))
  9038. (goto-char beg)
  9039. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  9040. end t)
  9041. (progn
  9042. (if (boundp 'org-blocked-by-checkboxes)
  9043. (setq org-blocked-by-checkboxes t))
  9044. (throw 'dont-block nil)))))
  9045. t)) ; do not block
  9046. (defun org-update-statistics-cookies (all)
  9047. "Update the statistics cookie, either from TODO or from checkboxes.
  9048. This should be called with the cursor in a line with a statistics cookie."
  9049. (interactive "P")
  9050. (if all
  9051. (progn
  9052. (org-update-checkbox-count 'all)
  9053. (org-map-entries 'org-update-parent-todo-statistics))
  9054. (if (not (org-on-heading-p))
  9055. (org-update-checkbox-count)
  9056. (let ((pos (move-marker (make-marker) (point)))
  9057. end l1 l2)
  9058. (ignore-errors (org-back-to-heading t))
  9059. (if (not (org-on-heading-p))
  9060. (org-update-checkbox-count)
  9061. (setq l1 (org-outline-level))
  9062. (setq end (save-excursion
  9063. (outline-next-heading)
  9064. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  9065. (point)))
  9066. (if (and (save-excursion (re-search-forward
  9067. "^[ \t]*[-+*] \\[[- X]\\]" end t))
  9068. (not (save-excursion (re-search-forward
  9069. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  9070. (org-update-checkbox-count)
  9071. (if (and l2 (> l2 l1))
  9072. (progn
  9073. (goto-char end)
  9074. (org-update-parent-todo-statistics))
  9075. (error "No data for statistics cookie"))))
  9076. (goto-char pos)
  9077. (move-marker pos nil)))))
  9078. (defvar org-entry-property-inherited-from) ;; defined below
  9079. (defun org-update-parent-todo-statistics ()
  9080. "Update any statistics cookie in the parent of the current headline.
  9081. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  9082. the entire subtree and this will travel up the hierarchy and update
  9083. statistics everywhere."
  9084. (interactive)
  9085. (let* ((lim 0) prop
  9086. (recursive (or (not org-hierarchical-todo-statistics)
  9087. (string-match
  9088. "\\<recursive\\>"
  9089. (or (setq prop (org-entry-get
  9090. nil "COOKIE_DATA" 'inherit)) ""))))
  9091. (lim (or (and prop (marker-position
  9092. org-entry-property-inherited-from))
  9093. lim))
  9094. (first t)
  9095. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  9096. level ltoggle l1 new ndel
  9097. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  9098. (catch 'exit
  9099. (save-excursion
  9100. (beginning-of-line 1)
  9101. (if (org-at-heading-p)
  9102. (setq ltoggle (funcall outline-level))
  9103. (error "This should not happen"))
  9104. (while (and (setq level (org-up-heading-safe))
  9105. (or recursive first)
  9106. (>= (point) lim))
  9107. (setq first nil cookie-present nil)
  9108. (unless (and level
  9109. (not (string-match
  9110. "\\<checkbox\\>"
  9111. (downcase
  9112. (or (org-entry-get
  9113. nil "COOKIE_DATA")
  9114. "")))))
  9115. (throw 'exit nil))
  9116. (while (re-search-forward box-re (point-at-eol) t)
  9117. (setq cnt-all 0 cnt-done 0 cookie-present t)
  9118. (setq is-percent (match-end 2))
  9119. (save-match-data
  9120. (unless (outline-next-heading) (throw 'exit nil))
  9121. (while (and (looking-at org-complex-heading-regexp)
  9122. (> (setq l1 (length (match-string 1))) level))
  9123. (setq kwd (and (or recursive (= l1 ltoggle))
  9124. (match-string 2)))
  9125. (if (or (eq org-provide-todo-statistics 'all-headlines)
  9126. (and (listp org-provide-todo-statistics)
  9127. (or (member kwd org-provide-todo-statistics)
  9128. (member kwd org-done-keywords))))
  9129. (setq cnt-all (1+ cnt-all))
  9130. (if (eq org-provide-todo-statistics t)
  9131. (and kwd (setq cnt-all (1+ cnt-all)))))
  9132. (and (member kwd org-done-keywords)
  9133. (setq cnt-done (1+ cnt-done)))
  9134. (outline-next-heading)))
  9135. (setq new
  9136. (if is-percent
  9137. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  9138. (format "[%d/%d]" cnt-done cnt-all))
  9139. ndel (- (match-end 0) (match-beginning 0)))
  9140. (goto-char (match-beginning 0))
  9141. (insert new)
  9142. (delete-region (point) (+ (point) ndel)))
  9143. (when cookie-present
  9144. (run-hook-with-args 'org-after-todo-statistics-hook
  9145. cnt-done (- cnt-all cnt-done))))))
  9146. (run-hooks 'org-todo-statistics-hook)))
  9147. (defvar org-after-todo-statistics-hook nil
  9148. "Hook that is called after a TODO statistics cookie has been updated.
  9149. Each function is called with two arguments: the number of not-done entries
  9150. and the number of done entries.
  9151. For example, the following function, when added to this hook, will switch
  9152. an entry to DONE when all children are done, and back to TODO when new
  9153. entries are set to a TODO status. Note that this hook is only called
  9154. when there is a statistics cookie in the headline!
  9155. (defun org-summary-todo (n-done n-not-done)
  9156. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  9157. (let (org-log-done org-log-states) ; turn off logging
  9158. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  9159. ")
  9160. (defvar org-todo-statistics-hook nil
  9161. "Hook that is run whenever Org thinks TODO statistics should be updated.
  9162. This hook runs even if there is no statisics cookie present, in which case
  9163. `org-after-todo-statistics-hook' would not run.")
  9164. (defun org-todo-trigger-tag-changes (state)
  9165. "Apply the changes defined in `org-todo-state-tags-triggers'."
  9166. (let ((l org-todo-state-tags-triggers)
  9167. changes)
  9168. (when (or (not state) (equal state ""))
  9169. (setq changes (append changes (cdr (assoc "" l)))))
  9170. (when (and (stringp state) (> (length state) 0))
  9171. (setq changes (append changes (cdr (assoc state l)))))
  9172. (when (member state org-not-done-keywords)
  9173. (setq changes (append changes (cdr (assoc 'todo l)))))
  9174. (when (member state org-done-keywords)
  9175. (setq changes (append changes (cdr (assoc 'done l)))))
  9176. (dolist (c changes)
  9177. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  9178. (defun org-local-logging (value)
  9179. "Get logging settings from a property VALUE."
  9180. (let* (words w a)
  9181. ;; directly set the variables, they are already local.
  9182. (setq org-log-done nil
  9183. org-log-repeat nil
  9184. org-todo-log-states nil)
  9185. (setq words (org-split-string value))
  9186. (while (setq w (pop words))
  9187. (cond
  9188. ((setq a (assoc w org-startup-options))
  9189. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  9190. (set (nth 1 a) (nth 2 a))))
  9191. ((setq a (org-extract-log-state-settings w))
  9192. (and (member (car a) org-todo-keywords-1)
  9193. (push a org-todo-log-states)))))))
  9194. (defun org-get-todo-sequence-head (kwd)
  9195. "Return the head of the TODO sequence to which KWD belongs.
  9196. If KWD is not set, check if there is a text property remembering the
  9197. right sequence."
  9198. (let (p)
  9199. (cond
  9200. ((not kwd)
  9201. (or (get-text-property (point-at-bol) 'org-todo-head)
  9202. (progn
  9203. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  9204. nil (point-at-eol)))
  9205. (get-text-property p 'org-todo-head))))
  9206. ((not (member kwd org-todo-keywords-1))
  9207. (car org-todo-keywords-1))
  9208. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  9209. (defun org-fast-todo-selection ()
  9210. "Fast TODO keyword selection with single keys.
  9211. Returns the new TODO keyword, or nil if no state change should occur."
  9212. (let* ((fulltable org-todo-key-alist)
  9213. (done-keywords org-done-keywords) ;; needed for the faces.
  9214. (maxlen (apply 'max (mapcar
  9215. (lambda (x)
  9216. (if (stringp (car x)) (string-width (car x)) 0))
  9217. fulltable)))
  9218. (expert nil)
  9219. (fwidth (+ maxlen 3 1 3))
  9220. (ncol (/ (- (window-width) 4) fwidth))
  9221. tg cnt e c tbl
  9222. groups ingroup)
  9223. (save-excursion
  9224. (save-window-excursion
  9225. (if expert
  9226. (set-buffer (get-buffer-create " *Org todo*"))
  9227. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  9228. (erase-buffer)
  9229. (org-set-local 'org-done-keywords done-keywords)
  9230. (setq tbl fulltable cnt 0)
  9231. (while (setq e (pop tbl))
  9232. (cond
  9233. ((equal e '(:startgroup))
  9234. (push '() groups) (setq ingroup t)
  9235. (when (not (= cnt 0))
  9236. (setq cnt 0)
  9237. (insert "\n"))
  9238. (insert "{ "))
  9239. ((equal e '(:endgroup))
  9240. (setq ingroup nil cnt 0)
  9241. (insert "}\n"))
  9242. ((equal e '(:newline))
  9243. (when (not (= cnt 0))
  9244. (setq cnt 0)
  9245. (insert "\n")
  9246. (setq e (car tbl))
  9247. (while (equal (car tbl) '(:newline))
  9248. (insert "\n")
  9249. (setq tbl (cdr tbl)))))
  9250. (t
  9251. (setq tg (car e) c (cdr e))
  9252. (if ingroup (push tg (car groups)))
  9253. (setq tg (org-add-props tg nil 'face
  9254. (org-get-todo-face tg)))
  9255. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9256. (insert "[" c "] " tg (make-string
  9257. (- fwidth 4 (length tg)) ?\ ))
  9258. (when (= (setq cnt (1+ cnt)) ncol)
  9259. (insert "\n")
  9260. (if ingroup (insert " "))
  9261. (setq cnt 0)))))
  9262. (insert "\n")
  9263. (goto-char (point-min))
  9264. (if (not expert) (org-fit-window-to-buffer))
  9265. (message "[a-z..]:Set [SPC]:clear")
  9266. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9267. (cond
  9268. ((or (= c ?\C-g)
  9269. (and (= c ?q) (not (rassoc c fulltable))))
  9270. (setq quit-flag t))
  9271. ((= c ?\ ) nil)
  9272. ((setq e (rassoc c fulltable) tg (car e))
  9273. tg)
  9274. (t (setq quit-flag t)))))))
  9275. (defun org-entry-is-todo-p ()
  9276. (member (org-get-todo-state) org-not-done-keywords))
  9277. (defun org-entry-is-done-p ()
  9278. (member (org-get-todo-state) org-done-keywords))
  9279. (defun org-get-todo-state ()
  9280. (save-excursion
  9281. (org-back-to-heading t)
  9282. (and (looking-at org-todo-line-regexp)
  9283. (match-end 2)
  9284. (match-string 2))))
  9285. (defun org-at-date-range-p (&optional inactive-ok)
  9286. "Is the cursor inside a date range?"
  9287. (interactive)
  9288. (save-excursion
  9289. (catch 'exit
  9290. (let ((pos (point)))
  9291. (skip-chars-backward "^[<\r\n")
  9292. (skip-chars-backward "<[")
  9293. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9294. (>= (match-end 0) pos)
  9295. (throw 'exit t))
  9296. (skip-chars-backward "^<[\r\n")
  9297. (skip-chars-backward "<[")
  9298. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9299. (>= (match-end 0) pos)
  9300. (throw 'exit t)))
  9301. nil)))
  9302. (defun org-get-repeat (&optional tagline)
  9303. "Check if there is a deadline/schedule with repeater in this entry."
  9304. (save-match-data
  9305. (save-excursion
  9306. (org-back-to-heading t)
  9307. (and (re-search-forward (if tagline
  9308. (concat tagline "\\s-*" org-repeat-re)
  9309. org-repeat-re)
  9310. (org-entry-end-position) t)
  9311. (match-string-no-properties 1)))))
  9312. (defvar org-last-changed-timestamp)
  9313. (defvar org-last-inserted-timestamp)
  9314. (defvar org-log-post-message)
  9315. (defvar org-log-note-purpose)
  9316. (defvar org-log-note-how)
  9317. (defvar org-log-note-extra)
  9318. (defun org-auto-repeat-maybe (done-word)
  9319. "Check if the current headline contains a repeated deadline/schedule.
  9320. If yes, set TODO state back to what it was and change the base date
  9321. of repeating deadline/scheduled time stamps to new date.
  9322. This function is run automatically after each state change to a DONE state."
  9323. ;; last-state is dynamically scoped into this function
  9324. (let* ((repeat (org-get-repeat))
  9325. (aa (assoc last-state org-todo-kwd-alist))
  9326. (interpret (nth 1 aa))
  9327. (head (nth 2 aa))
  9328. (whata '(("d" . day) ("m" . month) ("y" . year)))
  9329. (msg "Entry repeats: ")
  9330. (org-log-done nil)
  9331. (org-todo-log-states nil)
  9332. (nshiftmax 10) (nshift 0)
  9333. re type n what ts time)
  9334. (when repeat
  9335. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  9336. (org-todo (if (eq interpret 'type) last-state head))
  9337. (org-entry-put nil "LAST_REPEAT" (format-time-string
  9338. (org-time-stamp-format t t)))
  9339. (when org-log-repeat
  9340. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9341. (memq 'org-add-log-note post-command-hook))
  9342. ;; OK, we are already setup for some record
  9343. (if (eq org-log-repeat 'note)
  9344. ;; make sure we take a note, not only a time stamp
  9345. (setq org-log-note-how 'note))
  9346. ;; Set up for taking a record
  9347. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  9348. last-state
  9349. 'findpos org-log-repeat)))
  9350. (org-back-to-heading t)
  9351. (org-add-planning-info nil nil 'closed)
  9352. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  9353. org-deadline-time-regexp "\\)\\|\\("
  9354. org-ts-regexp "\\)"))
  9355. (while (re-search-forward
  9356. re (save-excursion (outline-next-heading) (point)) t)
  9357. (setq type (if (match-end 1) org-scheduled-string
  9358. (if (match-end 3) org-deadline-string "Plain:"))
  9359. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9360. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9361. (setq n (string-to-number (match-string 2 ts))
  9362. what (match-string 3 ts))
  9363. (if (equal what "w") (setq n (* n 7) what "d"))
  9364. ;; Preparation, see if we need to modify the start date for the change
  9365. (when (match-end 1)
  9366. (setq time (save-match-data (org-time-string-to-time ts)))
  9367. (cond
  9368. ((equal (match-string 1 ts) ".")
  9369. ;; Shift starting date to today
  9370. (org-timestamp-change
  9371. (- (time-to-days (current-time)) (time-to-days time))
  9372. 'day))
  9373. ((equal (match-string 1 ts) "+")
  9374. (while (or (= nshift 0)
  9375. (<= (time-to-days time) (time-to-days (current-time))))
  9376. (when (= (incf nshift) nshiftmax)
  9377. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9378. (error "Abort")))
  9379. (org-timestamp-change n (cdr (assoc what whata)))
  9380. (org-at-timestamp-p t)
  9381. (setq ts (match-string 1))
  9382. (setq time (save-match-data (org-time-string-to-time ts))))
  9383. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9384. ;; rematch, so that we have everything in place for the real shift
  9385. (org-at-timestamp-p t)
  9386. (setq ts (match-string 1))
  9387. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9388. (org-timestamp-change n (cdr (assoc what whata)))
  9389. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9390. (setq org-log-post-message msg)
  9391. (message "%s" msg))))
  9392. (defun org-show-todo-tree (arg)
  9393. "Make a compact tree which shows all headlines marked with TODO.
  9394. The tree will show the lines where the regexp matches, and all higher
  9395. headlines above the match.
  9396. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9397. With a numeric prefix N, construct a sparse tree for the Nth element
  9398. of `org-todo-keywords-1'."
  9399. (interactive "P")
  9400. (let ((case-fold-search nil)
  9401. (kwd-re
  9402. (cond ((null arg) org-not-done-regexp)
  9403. ((equal arg '(4))
  9404. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  9405. (mapcar 'list org-todo-keywords-1))))
  9406. (concat "\\("
  9407. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9408. "\\)\\>")))
  9409. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9410. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9411. org-todo-keywords-1)))
  9412. (t (error "Invalid prefix argument: %s" arg)))))
  9413. (message "%d TODO entries found"
  9414. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9415. (defun org-deadline (&optional remove time)
  9416. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9417. With argument REMOVE, remove any deadline from the item.
  9418. When TIME is set, it should be an internal time specification, and the
  9419. scheduling will use the corresponding date."
  9420. (interactive "P")
  9421. (let ((old-date (org-entry-get nil "DEADLINE")))
  9422. (if remove
  9423. (progn
  9424. (org-remove-timestamp-with-keyword org-deadline-string)
  9425. (message "Item no longer has a deadline."))
  9426. (if (org-get-repeat)
  9427. (error "Cannot change deadline on task with repeater, please do that by hand")
  9428. (org-add-planning-info 'deadline time 'closed)
  9429. (when (and old-date org-log-redeadline
  9430. (not (equal old-date
  9431. (substring org-last-inserted-timestamp 1 -1))))
  9432. (org-add-log-setup 'redeadline nil old-date 'findpos
  9433. org-log-redeadline))
  9434. (message "Deadline on %s" org-last-inserted-timestamp)))))
  9435. (defun org-schedule (&optional remove time)
  9436. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9437. With argument REMOVE, remove any scheduling date from the item.
  9438. When TIME is set, it should be an internal time specification, and the
  9439. scheduling will use the corresponding date."
  9440. (interactive "P")
  9441. (let ((old-date (org-entry-get nil "SCHEDULED")))
  9442. (if remove
  9443. (progn
  9444. (org-remove-timestamp-with-keyword org-scheduled-string)
  9445. (message "Item is no longer scheduled."))
  9446. (if (org-get-repeat)
  9447. (error "Cannot reschedule task with repeater, please do that by hand")
  9448. (org-add-planning-info 'scheduled time 'closed)
  9449. (when (and old-date org-log-reschedule
  9450. (not (equal old-date
  9451. (substring org-last-inserted-timestamp 1 -1))))
  9452. (org-add-log-setup 'reschedule nil old-date 'findpos
  9453. org-log-reschedule))
  9454. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  9455. (defun org-get-scheduled-time (pom &optional inherit)
  9456. "Get the scheduled time as a time tuple, of a format suitable
  9457. for calling org-schedule with, or if there is no scheduling,
  9458. returns nil."
  9459. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  9460. (when time
  9461. (apply 'encode-time (org-parse-time-string time)))))
  9462. (defun org-get-deadline-time (pom &optional inherit)
  9463. "Get the deadine as a time tuple, of a format suitable for
  9464. calling org-deadlin with, or if there is no scheduling, returns
  9465. nil."
  9466. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  9467. (when time
  9468. (apply 'encode-time (org-parse-time-string time)))))
  9469. (defun org-remove-timestamp-with-keyword (keyword)
  9470. "Remove all time stamps with KEYWORD in the current entry."
  9471. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  9472. beg)
  9473. (save-excursion
  9474. (org-back-to-heading t)
  9475. (setq beg (point))
  9476. (outline-next-heading)
  9477. (while (re-search-backward re beg t)
  9478. (replace-match "")
  9479. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  9480. (equal (char-before) ?\ ))
  9481. (backward-delete-char 1)
  9482. (if (string-match "^[ \t]*$" (buffer-substring
  9483. (point-at-bol) (point-at-eol)))
  9484. (delete-region (point-at-bol)
  9485. (min (point-max) (1+ (point-at-eol))))))))))
  9486. (defun org-add-planning-info (what &optional time &rest remove)
  9487. "Insert new timestamp with keyword in the line directly after the headline.
  9488. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  9489. If non is given, the user is prompted for a date.
  9490. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  9491. be removed."
  9492. (interactive)
  9493. (let (org-time-was-given org-end-time-was-given ts
  9494. end default-time default-input)
  9495. (catch 'exit
  9496. (when (and (not time) (memq what '(scheduled deadline)))
  9497. ;; Try to get a default date/time from existing timestamp
  9498. (save-excursion
  9499. (org-back-to-heading t)
  9500. (setq end (save-excursion (outline-next-heading) (point)))
  9501. (when (re-search-forward (if (eq what 'scheduled)
  9502. org-scheduled-time-regexp
  9503. org-deadline-time-regexp)
  9504. end t)
  9505. (setq ts (match-string 1)
  9506. default-time
  9507. (apply 'encode-time (org-parse-time-string ts))
  9508. default-input (and ts (org-get-compact-tod ts))))))
  9509. (when what
  9510. ;; If necessary, get the time from the user
  9511. (setq time (or time (org-read-date nil 'to-time nil nil
  9512. default-time default-input))))
  9513. (when (and org-insert-labeled-timestamps-at-point
  9514. (member what '(scheduled deadline)))
  9515. (insert
  9516. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  9517. (org-insert-time-stamp time org-time-was-given
  9518. nil nil nil (list org-end-time-was-given))
  9519. (setq what nil))
  9520. (save-excursion
  9521. (save-restriction
  9522. (let (col list elt ts buffer-invisibility-spec)
  9523. (org-back-to-heading t)
  9524. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  9525. (goto-char (match-end 1))
  9526. (setq col (current-column))
  9527. (goto-char (match-end 0))
  9528. (if (eobp) (insert "\n") (forward-char 1))
  9529. (when (and (not what)
  9530. (not (looking-at
  9531. (concat "[ \t]*"
  9532. org-keyword-time-not-clock-regexp))))
  9533. ;; Nothing to add, nothing to remove...... :-)
  9534. (throw 'exit nil))
  9535. (if (and (not (looking-at outline-regexp))
  9536. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  9537. "[^\r\n]*"))
  9538. (not (equal (match-string 1) org-clock-string)))
  9539. (narrow-to-region (match-beginning 0) (match-end 0))
  9540. (insert-before-markers "\n")
  9541. (backward-char 1)
  9542. (narrow-to-region (point) (point))
  9543. (and org-adapt-indentation (org-indent-to-column col)))
  9544. ;; Check if we have to remove something.
  9545. (setq list (cons what remove))
  9546. (while list
  9547. (setq elt (pop list))
  9548. (goto-char (point-min))
  9549. (when (or (and (eq elt 'scheduled)
  9550. (re-search-forward org-scheduled-time-regexp nil t))
  9551. (and (eq elt 'deadline)
  9552. (re-search-forward org-deadline-time-regexp nil t))
  9553. (and (eq elt 'closed)
  9554. (re-search-forward org-closed-time-regexp nil t)))
  9555. (replace-match "")
  9556. (if (looking-at "--+<[^>]+>") (replace-match ""))
  9557. (skip-chars-backward " ")
  9558. (if (looking-at " +") (replace-match ""))))
  9559. (goto-char (point-max))
  9560. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  9561. (when what
  9562. (insert
  9563. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  9564. (cond ((eq what 'scheduled) org-scheduled-string)
  9565. ((eq what 'deadline) org-deadline-string)
  9566. ((eq what 'closed) org-closed-string))
  9567. " ")
  9568. (setq ts (org-insert-time-stamp
  9569. time
  9570. (or org-time-was-given
  9571. (and (eq what 'closed) org-log-done-with-time))
  9572. (eq what 'closed)
  9573. nil nil (list org-end-time-was-given)))
  9574. (end-of-line 1))
  9575. (goto-char (point-min))
  9576. (widen)
  9577. (if (and (looking-at "[ \t]+\n")
  9578. (equal (char-before) ?\n))
  9579. (delete-region (1- (point)) (point-at-eol)))
  9580. ts))))))
  9581. (defvar org-log-note-marker (make-marker))
  9582. (defvar org-log-note-purpose nil)
  9583. (defvar org-log-note-state nil)
  9584. (defvar org-log-note-previous-state nil)
  9585. (defvar org-log-note-how nil)
  9586. (defvar org-log-note-extra nil)
  9587. (defvar org-log-note-window-configuration nil)
  9588. (defvar org-log-note-return-to (make-marker))
  9589. (defvar org-log-post-message nil
  9590. "Message to be displayed after a log note has been stored.
  9591. The auto-repeater uses this.")
  9592. (defun org-add-note ()
  9593. "Add a note to the current entry.
  9594. This is done in the same way as adding a state change note."
  9595. (interactive)
  9596. (org-add-log-setup 'note nil nil 'findpos nil))
  9597. (defvar org-property-end-re)
  9598. (defun org-add-log-setup (&optional purpose state prev-state
  9599. findpos how &optional extra)
  9600. "Set up the post command hook to take a note.
  9601. If this is about to TODO state change, the new state is expected in STATE.
  9602. When FINDPOS is non-nil, find the correct position for the note in
  9603. the current entry. If not, assume that it can be inserted at point.
  9604. HOW is an indicator what kind of note should be created.
  9605. EXTRA is additional text that will be inserted into the notes buffer."
  9606. (let* ((org-log-into-drawer (org-log-into-drawer))
  9607. (drawer (cond ((stringp org-log-into-drawer)
  9608. org-log-into-drawer)
  9609. (org-log-into-drawer "LOGBOOK")
  9610. (t nil))))
  9611. (save-restriction
  9612. (save-excursion
  9613. (when findpos
  9614. (org-back-to-heading t)
  9615. (narrow-to-region (point) (save-excursion
  9616. (outline-next-heading) (point)))
  9617. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  9618. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  9619. "[^\r\n]*\\)?"))
  9620. (goto-char (match-end 0))
  9621. (cond
  9622. (drawer
  9623. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  9624. nil t)
  9625. (progn
  9626. (goto-char (match-end 0))
  9627. (or org-log-states-order-reversed
  9628. (and (re-search-forward org-property-end-re nil t)
  9629. (goto-char (1- (match-beginning 0))))))
  9630. (insert "\n:" drawer ":\n:END:")
  9631. (beginning-of-line 0)
  9632. (org-indent-line-function)
  9633. (beginning-of-line 2)
  9634. (org-indent-line-function)
  9635. (end-of-line 0)))
  9636. ((and org-log-state-notes-insert-after-drawers
  9637. (save-excursion
  9638. (forward-line) (looking-at org-drawer-regexp)))
  9639. (forward-line)
  9640. (while (looking-at org-drawer-regexp)
  9641. (goto-char (match-end 0))
  9642. (re-search-forward org-property-end-re (point-max) t)
  9643. (forward-line))
  9644. (forward-line -1)))
  9645. (unless org-log-states-order-reversed
  9646. (and (= (char-after) ?\n) (forward-char 1))
  9647. (org-skip-over-state-notes)
  9648. (skip-chars-backward " \t\n\r")))
  9649. (move-marker org-log-note-marker (point))
  9650. (setq org-log-note-purpose purpose
  9651. org-log-note-state state
  9652. org-log-note-previous-state prev-state
  9653. org-log-note-how how
  9654. org-log-note-extra extra)
  9655. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  9656. (defun org-skip-over-state-notes ()
  9657. "Skip past the list of State notes in an entry."
  9658. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  9659. (while (looking-at "[ \t]*- State")
  9660. (condition-case nil
  9661. (org-next-item)
  9662. (error (org-end-of-item)))))
  9663. (defun org-add-log-note (&optional purpose)
  9664. "Pop up a window for taking a note, and add this note later at point."
  9665. (remove-hook 'post-command-hook 'org-add-log-note)
  9666. (setq org-log-note-window-configuration (current-window-configuration))
  9667. (delete-other-windows)
  9668. (move-marker org-log-note-return-to (point))
  9669. (switch-to-buffer (marker-buffer org-log-note-marker))
  9670. (goto-char org-log-note-marker)
  9671. (org-switch-to-buffer-other-window "*Org Note*")
  9672. (erase-buffer)
  9673. (if (memq org-log-note-how '(time state))
  9674. (let (current-prefix-arg) (org-store-log-note))
  9675. (let ((org-inhibit-startup t)) (org-mode))
  9676. (insert (format "# Insert note for %s.
  9677. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  9678. (cond
  9679. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9680. ((eq org-log-note-purpose 'done) "closed todo item")
  9681. ((eq org-log-note-purpose 'state)
  9682. (format "state change from \"%s\" to \"%s\""
  9683. (or org-log-note-previous-state "")
  9684. (or org-log-note-state "")))
  9685. ((eq org-log-note-purpose 'reschedule)
  9686. "rescheduling")
  9687. ((eq org-log-note-purpose 'redeadline)
  9688. "changing deadline")
  9689. ((eq org-log-note-purpose 'note)
  9690. "this entry")
  9691. (t (error "This should not happen")))))
  9692. (if org-log-note-extra (insert org-log-note-extra))
  9693. (org-set-local 'org-finish-function 'org-store-log-note)))
  9694. (defvar org-note-abort nil) ; dynamically scoped
  9695. (defun org-store-log-note ()
  9696. "Finish taking a log note, and insert it to where it belongs."
  9697. (let ((txt (buffer-string))
  9698. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9699. lines ind)
  9700. (kill-buffer (current-buffer))
  9701. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9702. (setq txt (replace-match "" t t txt)))
  9703. (if (string-match "\\s-+\\'" txt)
  9704. (setq txt (replace-match "" t t txt)))
  9705. (setq lines (org-split-string txt "\n"))
  9706. (when (and note (string-match "\\S-" note))
  9707. (setq note
  9708. (org-replace-escapes
  9709. note
  9710. (list (cons "%u" (user-login-name))
  9711. (cons "%U" user-full-name)
  9712. (cons "%t" (format-time-string
  9713. (org-time-stamp-format 'long 'inactive)
  9714. (current-time)))
  9715. (cons "%s" (if org-log-note-state
  9716. (concat "\"" org-log-note-state "\"")
  9717. ""))
  9718. (cons "%S" (if org-log-note-previous-state
  9719. (concat "\"" org-log-note-previous-state "\"")
  9720. "\"\"")))))
  9721. (if lines (setq note (concat note " \\\\")))
  9722. (push note lines))
  9723. (when (or current-prefix-arg org-note-abort)
  9724. (when org-log-into-drawer
  9725. (org-remove-empty-drawer-at
  9726. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9727. org-log-note-marker))
  9728. (setq lines nil))
  9729. (when lines
  9730. (with-current-buffer (marker-buffer org-log-note-marker)
  9731. (save-excursion
  9732. (goto-char org-log-note-marker)
  9733. (move-marker org-log-note-marker nil)
  9734. (end-of-line 1)
  9735. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9736. (insert "- " (pop lines))
  9737. (org-indent-line-function)
  9738. (beginning-of-line 1)
  9739. (looking-at "[ \t]*")
  9740. (setq ind (concat (match-string 0) " "))
  9741. (end-of-line 1)
  9742. (while lines (insert "\n" ind (pop lines)))
  9743. (message "Note stored")
  9744. (org-back-to-heading t)
  9745. (org-cycle-hide-drawers 'children)))))
  9746. (set-window-configuration org-log-note-window-configuration)
  9747. (with-current-buffer (marker-buffer org-log-note-return-to)
  9748. (goto-char org-log-note-return-to))
  9749. (move-marker org-log-note-return-to nil)
  9750. (and org-log-post-message (message "%s" org-log-post-message)))
  9751. (defun org-remove-empty-drawer-at (drawer pos)
  9752. "Remove an emptyr DARWER drawer at position POS.
  9753. POS may also be a marker."
  9754. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9755. (save-excursion
  9756. (save-restriction
  9757. (widen)
  9758. (goto-char pos)
  9759. (if (org-in-regexp
  9760. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9761. (replace-match ""))))))
  9762. (defun org-sparse-tree (&optional arg)
  9763. "Create a sparse tree, prompt for the details.
  9764. This command can create sparse trees. You first need to select the type
  9765. of match used to create the tree:
  9766. t Show entries with a specific TODO keyword.
  9767. m Show entries selected by a tags/property match.
  9768. p Enter a property name and its value (both with completion on existing
  9769. names/values) and show entries with that property.
  9770. / Show entries matching a regular expression (`r' can be used as well)
  9771. d Show deadlines due within `org-deadline-warning-days'.
  9772. b Show deadlines and scheduled items before a date.
  9773. a Show deadlines and scheduled items after a date."
  9774. (interactive "P")
  9775. (let (ans kwd value)
  9776. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9777. (setq ans (read-char-exclusive))
  9778. (cond
  9779. ((equal ans ?d)
  9780. (call-interactively 'org-check-deadlines))
  9781. ((equal ans ?b)
  9782. (call-interactively 'org-check-before-date))
  9783. ((equal ans ?a)
  9784. (call-interactively 'org-check-after-date))
  9785. ((equal ans ?t)
  9786. (org-show-todo-tree '(4)))
  9787. ((member ans '(?T ?m))
  9788. (call-interactively 'org-match-sparse-tree))
  9789. ((member ans '(?p ?P))
  9790. (setq kwd (org-icompleting-read "Property: "
  9791. (mapcar 'list (org-buffer-property-keys))))
  9792. (setq value (org-icompleting-read "Value: "
  9793. (mapcar 'list (org-property-values kwd))))
  9794. (unless (string-match "\\`{.*}\\'" value)
  9795. (setq value (concat "\"" value "\"")))
  9796. (org-match-sparse-tree arg (concat kwd "=" value)))
  9797. ((member ans '(?r ?R ?/))
  9798. (call-interactively 'org-occur))
  9799. (t (error "No such sparse tree command \"%c\"" ans)))))
  9800. (defvar org-occur-highlights nil
  9801. "List of overlays used for occur matches.")
  9802. (make-variable-buffer-local 'org-occur-highlights)
  9803. (defvar org-occur-parameters nil
  9804. "Parameters of the active org-occur calls.
  9805. This is a list, each call to org-occur pushes as cons cell,
  9806. containing the regular expression and the callback, onto the list.
  9807. The list can contain several entries if `org-occur' has been called
  9808. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9809. will only contain one set of parameters. When the highlights are
  9810. removed (for example with `C-c C-c', or with the next edit (depending
  9811. on `org-remove-highlights-with-change'), this variable is emptied
  9812. as well.")
  9813. (make-variable-buffer-local 'org-occur-parameters)
  9814. (defun org-occur (regexp &optional keep-previous callback)
  9815. "Make a compact tree which shows all matches of REGEXP.
  9816. The tree will show the lines where the regexp matches, and all higher
  9817. headlines above the match. It will also show the heading after the match,
  9818. to make sure editing the matching entry is easy.
  9819. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9820. call to `org-occur' will be kept, to allow stacking of calls to this
  9821. command.
  9822. If CALLBACK is non-nil, it is a function which is called to confirm
  9823. that the match should indeed be shown."
  9824. (interactive "sRegexp: \nP")
  9825. (when (equal regexp "")
  9826. (error "Regexp cannot be empty"))
  9827. (unless keep-previous
  9828. (org-remove-occur-highlights nil nil t))
  9829. (push (cons regexp callback) org-occur-parameters)
  9830. (let ((cnt 0))
  9831. (save-excursion
  9832. (goto-char (point-min))
  9833. (if (or (not keep-previous) ; do not want to keep
  9834. (not org-occur-highlights)) ; no previous matches
  9835. ;; hide everything
  9836. (org-overview))
  9837. (while (re-search-forward regexp nil t)
  9838. (when (or (not callback)
  9839. (save-match-data (funcall callback)))
  9840. (setq cnt (1+ cnt))
  9841. (when org-highlight-sparse-tree-matches
  9842. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9843. (org-show-context 'occur-tree))))
  9844. (when org-remove-highlights-with-change
  9845. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9846. nil 'local))
  9847. (unless org-sparse-tree-open-archived-trees
  9848. (org-hide-archived-subtrees (point-min) (point-max)))
  9849. (run-hooks 'org-occur-hook)
  9850. (if (interactive-p)
  9851. (message "%d match(es) for regexp %s" cnt regexp))
  9852. cnt))
  9853. (defun org-show-context (&optional key)
  9854. "Make sure point and context and visible.
  9855. How much context is shown depends upon the variables
  9856. `org-show-hierarchy-above', `org-show-following-heading'. and
  9857. `org-show-siblings'."
  9858. (let ((heading-p (org-on-heading-p t))
  9859. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9860. (following-p (org-get-alist-option org-show-following-heading key))
  9861. (entry-p (org-get-alist-option org-show-entry-below key))
  9862. (siblings-p (org-get-alist-option org-show-siblings key)))
  9863. (catch 'exit
  9864. ;; Show heading or entry text
  9865. (if (and heading-p (not entry-p))
  9866. (org-flag-heading nil) ; only show the heading
  9867. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9868. (org-show-hidden-entry))) ; show entire entry
  9869. (when following-p
  9870. ;; Show next sibling, or heading below text
  9871. (save-excursion
  9872. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9873. (org-flag-heading nil))))
  9874. (when siblings-p (org-show-siblings))
  9875. (when hierarchy-p
  9876. ;; show all higher headings, possibly with siblings
  9877. (save-excursion
  9878. (while (and (condition-case nil
  9879. (progn (org-up-heading-all 1) t)
  9880. (error nil))
  9881. (not (bobp)))
  9882. (org-flag-heading nil)
  9883. (when siblings-p (org-show-siblings))))))))
  9884. (defun org-reveal (&optional siblings)
  9885. "Show current entry, hierarchy above it, and the following headline.
  9886. This can be used to show a consistent set of context around locations
  9887. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9888. not t for the search context.
  9889. With optional argument SIBLINGS, on each level of the hierarchy all
  9890. siblings are shown. This repairs the tree structure to what it would
  9891. look like when opened with hierarchical calls to `org-cycle'."
  9892. (interactive "P")
  9893. (let ((org-show-hierarchy-above t)
  9894. (org-show-following-heading t)
  9895. (org-show-siblings (if siblings t org-show-siblings)))
  9896. (org-show-context nil)))
  9897. (defun org-highlight-new-match (beg end)
  9898. "Highlight from BEG to END and mark the highlight is an occur headline."
  9899. (let ((ov (org-make-overlay beg end)))
  9900. (org-overlay-put ov 'face 'secondary-selection)
  9901. (push ov org-occur-highlights)))
  9902. (defun org-remove-occur-highlights (&optional beg end noremove)
  9903. "Remove the occur highlights from the buffer.
  9904. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9905. from the `before-change-functions' in the current buffer."
  9906. (interactive)
  9907. (unless org-inhibit-highlight-removal
  9908. (mapc 'org-delete-overlay org-occur-highlights)
  9909. (setq org-occur-highlights nil)
  9910. (setq org-occur-parameters nil)
  9911. (unless noremove
  9912. (remove-hook 'before-change-functions
  9913. 'org-remove-occur-highlights 'local))))
  9914. ;;;; Priorities
  9915. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9916. "Regular expression matching the priority indicator.")
  9917. (defvar org-remove-priority-next-time nil)
  9918. (defun org-priority-up ()
  9919. "Increase the priority of the current item."
  9920. (interactive)
  9921. (org-priority 'up))
  9922. (defun org-priority-down ()
  9923. "Decrease the priority of the current item."
  9924. (interactive)
  9925. (org-priority 'down))
  9926. (defun org-priority (&optional action)
  9927. "Change the priority of an item by ARG.
  9928. ACTION can be `set', `up', `down', or a character."
  9929. (interactive)
  9930. (unless org-enable-priority-commands
  9931. (error "Priority commands are disabled"))
  9932. (setq action (or action 'set))
  9933. (let (current new news have remove)
  9934. (save-excursion
  9935. (org-back-to-heading t)
  9936. (if (looking-at org-priority-regexp)
  9937. (setq current (string-to-char (match-string 2))
  9938. have t)
  9939. (setq current org-default-priority))
  9940. (cond
  9941. ((eq action 'remove)
  9942. (setq remove t new ?\ ))
  9943. ((or (eq action 'set)
  9944. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9945. (if (not (eq action 'set))
  9946. (setq new action)
  9947. (message "Priority %c-%c, SPC to remove: "
  9948. org-highest-priority org-lowest-priority)
  9949. (setq new (read-char-exclusive)))
  9950. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9951. (= (upcase org-lowest-priority) org-lowest-priority))
  9952. (setq new (upcase new)))
  9953. (cond ((equal new ?\ ) (setq remove t))
  9954. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9955. (error "Priority must be between `%c' and `%c'"
  9956. org-highest-priority org-lowest-priority))))
  9957. ((eq action 'up)
  9958. (if (and (not have) (eq last-command this-command))
  9959. (setq new org-lowest-priority)
  9960. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9961. org-default-priority (1- current)))))
  9962. ((eq action 'down)
  9963. (if (and (not have) (eq last-command this-command))
  9964. (setq new org-highest-priority)
  9965. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9966. org-default-priority (1+ current)))))
  9967. (t (error "Invalid action")))
  9968. (if (or (< (upcase new) org-highest-priority)
  9969. (> (upcase new) org-lowest-priority))
  9970. (setq remove t))
  9971. (setq news (format "%c" new))
  9972. (if have
  9973. (if remove
  9974. (replace-match "" t t nil 1)
  9975. (replace-match news t t nil 2))
  9976. (if remove
  9977. (error "No priority cookie found in line")
  9978. (let ((case-fold-search nil))
  9979. (looking-at org-todo-line-regexp))
  9980. (if (match-end 2)
  9981. (progn
  9982. (goto-char (match-end 2))
  9983. (insert " [#" news "]"))
  9984. (goto-char (match-beginning 3))
  9985. (insert "[#" news "] "))))
  9986. (org-preserve-lc (org-set-tags nil 'align)))
  9987. (if remove
  9988. (message "Priority removed")
  9989. (message "Priority of current item set to %s" news))))
  9990. (defun org-get-priority (s)
  9991. "Find priority cookie and return priority."
  9992. (save-match-data
  9993. (if (not (string-match org-priority-regexp s))
  9994. (* 1000 (- org-lowest-priority org-default-priority))
  9995. (* 1000 (- org-lowest-priority
  9996. (string-to-char (match-string 2 s)))))))
  9997. ;;;; Tags
  9998. (defvar org-agenda-archives-mode)
  9999. (defvar org-map-continue-from nil
  10000. "Position from where mapping should continue.
  10001. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  10002. (defvar org-scanner-tags nil
  10003. "The current tag list while the tags scanner is running.")
  10004. (defvar org-trust-scanner-tags nil
  10005. "Should `org-get-tags-at' use the tags fro the scanner.
  10006. This is for internal dynamical scoping only.
  10007. When this is non-nil, the function `org-get-tags-at' will return the value
  10008. of `org-scanner-tags' instead of building the list by itself. This
  10009. can lead to large speed-ups when the tags scanner is used in a file with
  10010. many entries, and when the list of tags is retrieved, for example to
  10011. obtain a list of properties. Building the tags list for each entry in such
  10012. a file becomes an N^2 operation - but with this variable set, it scales
  10013. as N.")
  10014. (defun org-scan-tags (action matcher &optional todo-only)
  10015. "Scan headline tags with inheritance and produce output ACTION.
  10016. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  10017. or `agenda' to produce an entry list for an agenda view. It can also be
  10018. a Lisp form or a function that should be called at each matched headline, in
  10019. this case the return value is a list of all return values from these calls.
  10020. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  10021. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  10022. only lines with a TODO keyword are included in the output."
  10023. (require 'org-agenda)
  10024. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  10025. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  10026. (org-re
  10027. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  10028. (props (list 'face 'default
  10029. 'done-face 'org-agenda-done
  10030. 'undone-face 'default
  10031. 'mouse-face 'highlight
  10032. 'org-not-done-regexp org-not-done-regexp
  10033. 'org-todo-regexp org-todo-regexp
  10034. 'help-echo
  10035. (format "mouse-2 or RET jump to org file %s"
  10036. (abbreviate-file-name
  10037. (or (buffer-file-name (buffer-base-buffer))
  10038. (buffer-name (buffer-base-buffer)))))))
  10039. (case-fold-search nil)
  10040. (org-map-continue-from nil)
  10041. lspos tags tags-list
  10042. (tags-alist (list (cons 0 org-file-tags)))
  10043. (llast 0) rtn rtn1 level category i txt
  10044. todo marker entry priority)
  10045. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  10046. (setq action (list 'lambda nil action)))
  10047. (save-excursion
  10048. (goto-char (point-min))
  10049. (when (eq action 'sparse-tree)
  10050. (org-overview)
  10051. (org-remove-occur-highlights))
  10052. (while (re-search-forward re nil t)
  10053. (catch :skip
  10054. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  10055. tags (if (match-end 4) (org-match-string-no-properties 4)))
  10056. (goto-char (setq lspos (match-beginning 0)))
  10057. (setq level (org-reduced-level (funcall outline-level))
  10058. category (org-get-category))
  10059. (setq i llast llast level)
  10060. ;; remove tag lists from same and sublevels
  10061. (while (>= i level)
  10062. (when (setq entry (assoc i tags-alist))
  10063. (setq tags-alist (delete entry tags-alist)))
  10064. (setq i (1- i)))
  10065. ;; add the next tags
  10066. (when tags
  10067. (setq tags (org-split-string tags ":")
  10068. tags-alist
  10069. (cons (cons level tags) tags-alist)))
  10070. ;; compile tags for current headline
  10071. (setq tags-list
  10072. (if org-use-tag-inheritance
  10073. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  10074. tags)
  10075. org-scanner-tags tags-list)
  10076. (when org-use-tag-inheritance
  10077. (setcdr (car tags-alist)
  10078. (mapcar (lambda (x)
  10079. (setq x (copy-sequence x))
  10080. (org-add-prop-inherited x))
  10081. (cdar tags-alist))))
  10082. (when (and tags org-use-tag-inheritance
  10083. (or (not (eq t org-use-tag-inheritance))
  10084. org-tags-exclude-from-inheritance))
  10085. ;; selective inheritance, remove uninherited ones
  10086. (setcdr (car tags-alist)
  10087. (org-remove-uniherited-tags (cdar tags-alist))))
  10088. (when (and (or (not todo-only)
  10089. (and (member todo org-not-done-keywords)
  10090. (or (not org-agenda-tags-todo-honor-ignore-options)
  10091. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  10092. (let ((case-fold-search t)) (eval matcher))
  10093. (or
  10094. (not (member org-archive-tag tags-list))
  10095. ;; we have an archive tag, should we use this anyway?
  10096. (or (not org-agenda-skip-archived-trees)
  10097. (and (eq action 'agenda) org-agenda-archives-mode))))
  10098. (unless (eq action 'sparse-tree) (org-agenda-skip))
  10099. ;; select this headline
  10100. (cond
  10101. ((eq action 'sparse-tree)
  10102. (and org-highlight-sparse-tree-matches
  10103. (org-get-heading) (match-end 0)
  10104. (org-highlight-new-match
  10105. (match-beginning 0) (match-beginning 1)))
  10106. (org-show-context 'tags-tree))
  10107. ((eq action 'agenda)
  10108. (setq txt (org-format-agenda-item
  10109. ""
  10110. (concat
  10111. (if (eq org-tags-match-list-sublevels 'indented)
  10112. (make-string (1- level) ?.) "")
  10113. (org-get-heading))
  10114. category
  10115. tags-list
  10116. )
  10117. priority (org-get-priority txt))
  10118. (goto-char lspos)
  10119. (setq marker (org-agenda-new-marker))
  10120. (org-add-props txt props
  10121. 'org-marker marker 'org-hd-marker marker 'org-category category
  10122. 'todo-state todo
  10123. 'priority priority 'type "tagsmatch")
  10124. (push txt rtn))
  10125. ((functionp action)
  10126. (setq org-map-continue-from nil)
  10127. (save-excursion
  10128. (setq rtn1 (funcall action))
  10129. (push rtn1 rtn)))
  10130. (t (error "Invalid action")))
  10131. ;; if we are to skip sublevels, jump to end of subtree
  10132. (unless org-tags-match-list-sublevels
  10133. (org-end-of-subtree t)
  10134. (backward-char 1))))
  10135. ;; Get the correct position from where to continue
  10136. (if org-map-continue-from
  10137. (goto-char org-map-continue-from)
  10138. (and (= (point) lspos) (end-of-line 1)))))
  10139. (when (and (eq action 'sparse-tree)
  10140. (not org-sparse-tree-open-archived-trees))
  10141. (org-hide-archived-subtrees (point-min) (point-max)))
  10142. (nreverse rtn)))
  10143. (defun org-remove-uniherited-tags (tags)
  10144. "Remove all tags that are not inherited from the list TAGS."
  10145. (cond
  10146. ((eq org-use-tag-inheritance t)
  10147. (if org-tags-exclude-from-inheritance
  10148. (org-delete-all org-tags-exclude-from-inheritance tags)
  10149. tags))
  10150. ((not org-use-tag-inheritance) nil)
  10151. ((stringp org-use-tag-inheritance)
  10152. (delq nil (mapcar
  10153. (lambda (x)
  10154. (if (and (string-match org-use-tag-inheritance x)
  10155. (not (member x org-tags-exclude-from-inheritance)))
  10156. x nil))
  10157. tags)))
  10158. ((listp org-use-tag-inheritance)
  10159. (delq nil (mapcar
  10160. (lambda (x)
  10161. (if (member x org-use-tag-inheritance) x nil))
  10162. tags)))))
  10163. (defvar todo-only) ;; dynamically scoped
  10164. (defun org-match-sparse-tree (&optional todo-only match)
  10165. "Create a sparse tree according to tags string MATCH.
  10166. MATCH can contain positive and negative selection of tags, like
  10167. \"+WORK+URGENT-WITHBOSS\".
  10168. If optional argument TODO-ONLY is non-nil, only select lines that are
  10169. also TODO lines."
  10170. (interactive "P")
  10171. (org-prepare-agenda-buffers (list (current-buffer)))
  10172. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  10173. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  10174. (defvar org-cached-props nil)
  10175. (defun org-cached-entry-get (pom property)
  10176. (if (or (eq t org-use-property-inheritance)
  10177. (and (stringp org-use-property-inheritance)
  10178. (string-match org-use-property-inheritance property))
  10179. (and (listp org-use-property-inheritance)
  10180. (member property org-use-property-inheritance)))
  10181. ;; Caching is not possible, check it directly
  10182. (org-entry-get pom property 'inherit)
  10183. ;; Get all properties, so that we can do complicated checks easily
  10184. (cdr (assoc property (or org-cached-props
  10185. (setq org-cached-props
  10186. (org-entry-properties pom)))))))
  10187. (defun org-global-tags-completion-table (&optional files)
  10188. "Return the list of all tags in all agenda buffer/files."
  10189. (save-excursion
  10190. (org-uniquify
  10191. (delq nil
  10192. (apply 'append
  10193. (mapcar
  10194. (lambda (file)
  10195. (set-buffer (find-file-noselect file))
  10196. (append (org-get-buffer-tags)
  10197. (mapcar (lambda (x) (if (stringp (car-safe x))
  10198. (list (car-safe x)) nil))
  10199. org-tag-alist)))
  10200. (if (and files (car files))
  10201. files
  10202. (org-agenda-files))))))))
  10203. (defun org-make-tags-matcher (match)
  10204. "Create the TAGS//TODO matcher form for the selection string MATCH."
  10205. ;; todo-only is scoped dynamically into this function, and the function
  10206. ;; may change it if the matcher asks for it.
  10207. (unless match
  10208. ;; Get a new match request, with completion
  10209. (let ((org-last-tags-completion-table
  10210. (org-global-tags-completion-table)))
  10211. (setq match (org-completing-read-no-i
  10212. "Match: " 'org-tags-completion-function nil nil nil
  10213. 'org-tags-history))))
  10214. ;; Parse the string and create a lisp form
  10215. (let ((match0 match)
  10216. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  10217. minus tag mm
  10218. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  10219. orterms term orlist re-p str-p level-p level-op time-p
  10220. prop-p pn pv po cat-p gv rest)
  10221. (if (string-match "/+" match)
  10222. ;; match contains also a todo-matching request
  10223. (progn
  10224. (setq tagsmatch (substring match 0 (match-beginning 0))
  10225. todomatch (substring match (match-end 0)))
  10226. (if (string-match "^!" todomatch)
  10227. (setq todo-only t todomatch (substring todomatch 1)))
  10228. (if (string-match "^\\s-*$" todomatch)
  10229. (setq todomatch nil)))
  10230. ;; only matching tags
  10231. (setq tagsmatch match todomatch nil))
  10232. ;; Make the tags matcher
  10233. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  10234. (setq tagsmatcher t)
  10235. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  10236. (while (setq term (pop orterms))
  10237. (while (and (equal (substring term -1) "\\") orterms)
  10238. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  10239. (while (string-match re term)
  10240. (setq rest (substring term (match-end 0))
  10241. minus (and (match-end 1)
  10242. (equal (match-string 1 term) "-"))
  10243. tag (match-string 2 term)
  10244. re-p (equal (string-to-char tag) ?{)
  10245. level-p (match-end 4)
  10246. prop-p (match-end 5)
  10247. mm (cond
  10248. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  10249. (level-p
  10250. (setq level-op (org-op-to-function (match-string 3 term)))
  10251. `(,level-op level ,(string-to-number
  10252. (match-string 4 term))))
  10253. (prop-p
  10254. (setq pn (match-string 5 term)
  10255. po (match-string 6 term)
  10256. pv (match-string 7 term)
  10257. cat-p (equal pn "CATEGORY")
  10258. re-p (equal (string-to-char pv) ?{)
  10259. str-p (equal (string-to-char pv) ?\")
  10260. time-p (save-match-data
  10261. (string-match "^\"[[<].*[]>]\"$" pv))
  10262. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  10263. (if time-p (setq pv (org-matcher-time pv)))
  10264. (setq po (org-op-to-function po (if time-p 'time str-p)))
  10265. (cond
  10266. ((equal pn "CATEGORY")
  10267. (setq gv '(get-text-property (point) 'org-category)))
  10268. ((equal pn "TODO")
  10269. (setq gv 'todo))
  10270. (t
  10271. (setq gv `(org-cached-entry-get nil ,pn))))
  10272. (if re-p
  10273. (if (eq po 'org<>)
  10274. `(not (string-match ,pv (or ,gv "")))
  10275. `(string-match ,pv (or ,gv "")))
  10276. (if str-p
  10277. `(,po (or ,gv "") ,pv)
  10278. `(,po (string-to-number (or ,gv ""))
  10279. ,(string-to-number pv) ))))
  10280. (t `(member ,tag tags-list)))
  10281. mm (if minus (list 'not mm) mm)
  10282. term rest)
  10283. (push mm tagsmatcher))
  10284. (push (if (> (length tagsmatcher) 1)
  10285. (cons 'and tagsmatcher)
  10286. (car tagsmatcher))
  10287. orlist)
  10288. (setq tagsmatcher nil))
  10289. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  10290. (setq tagsmatcher
  10291. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  10292. ;; Make the todo matcher
  10293. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  10294. (setq todomatcher t)
  10295. (setq orterms (org-split-string todomatch "|") orlist nil)
  10296. (while (setq term (pop orterms))
  10297. (while (string-match re term)
  10298. (setq minus (and (match-end 1)
  10299. (equal (match-string 1 term) "-"))
  10300. kwd (match-string 2 term)
  10301. re-p (equal (string-to-char kwd) ?{)
  10302. term (substring term (match-end 0))
  10303. mm (if re-p
  10304. `(string-match ,(substring kwd 1 -1) todo)
  10305. (list 'equal 'todo kwd))
  10306. mm (if minus (list 'not mm) mm))
  10307. (push mm todomatcher))
  10308. (push (if (> (length todomatcher) 1)
  10309. (cons 'and todomatcher)
  10310. (car todomatcher))
  10311. orlist)
  10312. (setq todomatcher nil))
  10313. (setq todomatcher (if (> (length orlist) 1)
  10314. (cons 'or orlist) (car orlist))))
  10315. ;; Return the string and lisp forms of the matcher
  10316. (setq matcher (if todomatcher
  10317. (list 'and tagsmatcher todomatcher)
  10318. tagsmatcher))
  10319. (cons match0 matcher)))
  10320. (defun org-op-to-function (op &optional stringp)
  10321. "Turn an operator into the appropriate function."
  10322. (setq op
  10323. (cond
  10324. ((equal op "<" ) '(< string< org-time<))
  10325. ((equal op ">" ) '(> org-string> org-time>))
  10326. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  10327. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  10328. ((member op '("=" "==")) '(= string= org-time=))
  10329. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  10330. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  10331. (defun org<> (a b) (not (= a b)))
  10332. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  10333. (defun org-string>= (a b) (not (string< a b)))
  10334. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  10335. (defun org-string<> (a b) (not (string= a b)))
  10336. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  10337. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  10338. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  10339. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  10340. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  10341. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  10342. (defun org-2ft (s)
  10343. "Convert S to a floating point time.
  10344. If S is already a number, just return it. If it is a string, parse
  10345. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  10346. (cond
  10347. ((numberp s) s)
  10348. ((stringp s)
  10349. (condition-case nil
  10350. (float-time (apply 'encode-time (org-parse-time-string s)))
  10351. (error 0.)))
  10352. (t 0.)))
  10353. (defun org-time-today ()
  10354. "Time in seconds today at 0:00.
  10355. Returns the float number of seconds since the beginning of the
  10356. epoch to the beginning of today (00:00)."
  10357. (float-time (apply 'encode-time
  10358. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  10359. (defun org-matcher-time (s)
  10360. "Interpret a time comparison value."
  10361. (save-match-data
  10362. (cond
  10363. ((string= s "<now>") (float-time))
  10364. ((string= s "<today>") (org-time-today))
  10365. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  10366. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  10367. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  10368. (+ (org-time-today)
  10369. (* (string-to-number (match-string 1 s))
  10370. (cdr (assoc (match-string 2 s)
  10371. '(("d" . 86400.0) ("w" . 604800.0)
  10372. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10373. (t (org-2ft s)))))
  10374. (defun org-match-any-p (re list)
  10375. "Does re match any element of list?"
  10376. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10377. (delq nil list))
  10378. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10379. (defvar org-tags-overlay (org-make-overlay 1 1))
  10380. (org-detach-overlay org-tags-overlay)
  10381. (defun org-get-local-tags-at (&optional pos)
  10382. "Get a list of tags defined in the current headline."
  10383. (org-get-tags-at pos 'local))
  10384. (defun org-get-local-tags ()
  10385. "Get a list of tags defined in the current headline."
  10386. (org-get-tags-at nil 'local))
  10387. (defun org-get-tags-at (&optional pos local)
  10388. "Get a list of all headline tags applicable at POS.
  10389. POS defaults to point. If tags are inherited, the list contains
  10390. the targets in the same sequence as the headlines appear, i.e.
  10391. the tags of the current headline come last.
  10392. When LOCAL is non-nil, only return tags from the current headline,
  10393. ignore inherited ones."
  10394. (interactive)
  10395. (if (and org-trust-scanner-tags
  10396. (or (not pos) (equal pos (point)))
  10397. (not local))
  10398. org-scanner-tags
  10399. (let (tags ltags lastpos parent)
  10400. (save-excursion
  10401. (save-restriction
  10402. (widen)
  10403. (goto-char (or pos (point)))
  10404. (save-match-data
  10405. (catch 'done
  10406. (condition-case nil
  10407. (progn
  10408. (org-back-to-heading t)
  10409. (while (not (equal lastpos (point)))
  10410. (setq lastpos (point))
  10411. (when (looking-at
  10412. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  10413. (setq ltags (org-split-string
  10414. (org-match-string-no-properties 1) ":"))
  10415. (when parent
  10416. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  10417. (setq tags (append
  10418. (if parent
  10419. (org-remove-uniherited-tags ltags)
  10420. ltags)
  10421. tags)))
  10422. (or org-use-tag-inheritance (throw 'done t))
  10423. (if local (throw 'done t))
  10424. (or (org-up-heading-safe) (error nil))
  10425. (setq parent t)))
  10426. (error nil)))))
  10427. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  10428. (defun org-add-prop-inherited (s)
  10429. (add-text-properties 0 (length s) '(inherited t) s)
  10430. s)
  10431. (defun org-toggle-tag (tag &optional onoff)
  10432. "Toggle the tag TAG for the current line.
  10433. If ONOFF is `on' or `off', don't toggle but set to this state."
  10434. (let (res current)
  10435. (save-excursion
  10436. (org-back-to-heading t)
  10437. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  10438. (point-at-eol) t)
  10439. (progn
  10440. (setq current (match-string 1))
  10441. (replace-match ""))
  10442. (setq current ""))
  10443. (setq current (nreverse (org-split-string current ":")))
  10444. (cond
  10445. ((eq onoff 'on)
  10446. (setq res t)
  10447. (or (member tag current) (push tag current)))
  10448. ((eq onoff 'off)
  10449. (or (not (member tag current)) (setq current (delete tag current))))
  10450. (t (if (member tag current)
  10451. (setq current (delete tag current))
  10452. (setq res t)
  10453. (push tag current))))
  10454. (end-of-line 1)
  10455. (if current
  10456. (progn
  10457. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  10458. (org-set-tags nil t))
  10459. (delete-horizontal-space))
  10460. (run-hooks 'org-after-tags-change-hook))
  10461. res))
  10462. (defun org-align-tags-here (to-col)
  10463. ;; Assumes that this is a headline
  10464. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  10465. (beginning-of-line 1)
  10466. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10467. (< pos (match-beginning 2)))
  10468. (progn
  10469. (setq tags-l (- (match-end 2) (match-beginning 2)))
  10470. (goto-char (match-beginning 1))
  10471. (insert " ")
  10472. (delete-region (point) (1+ (match-beginning 2)))
  10473. (setq ncol (max (1+ (current-column))
  10474. (1+ col)
  10475. (if (> to-col 0)
  10476. to-col
  10477. (- (abs to-col) tags-l))))
  10478. (setq p (point))
  10479. (insert (make-string (- ncol (current-column)) ?\ ))
  10480. (setq ncol (current-column))
  10481. (when indent-tabs-mode (tabify p (point-at-eol)))
  10482. (org-move-to-column (min ncol col) t))
  10483. (goto-char pos))))
  10484. (defun org-set-tags-command (&optional arg just-align)
  10485. "Call the set-tags command for the current entry."
  10486. (interactive "P")
  10487. (if (org-on-heading-p)
  10488. (org-set-tags arg just-align)
  10489. (save-excursion
  10490. (org-back-to-heading t)
  10491. (org-set-tags arg just-align))))
  10492. (defun org-set-tags-to (data)
  10493. "Set the tags of the current entry to DATA, replacing the current tags.
  10494. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  10495. If DATA is nil or the empty string, any tags will be removed."
  10496. (interactive "sTags: ")
  10497. (setq data
  10498. (cond
  10499. ((eq data nil) "")
  10500. ((equal data "") "")
  10501. ((stringp data)
  10502. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  10503. ":"))
  10504. ((listp data)
  10505. (concat ":" (mapconcat 'identity data ":") ":"))
  10506. (t nil)))
  10507. (when data
  10508. (save-excursion
  10509. (org-back-to-heading t)
  10510. (when (looking-at org-complex-heading-regexp)
  10511. (if (match-end 5)
  10512. (progn
  10513. (goto-char (match-beginning 5))
  10514. (insert data)
  10515. (delete-region (point) (point-at-eol))
  10516. (org-set-tags nil 'align))
  10517. (goto-char (point-at-eol))
  10518. (insert " " data)
  10519. (org-set-tags nil 'align)))
  10520. (beginning-of-line 1)
  10521. (if (looking-at ".*?\\([ \t]+\\)$")
  10522. (delete-region (match-beginning 1) (match-end 1))))))
  10523. (defun org-set-tags (&optional arg just-align)
  10524. "Set the tags for the current headline.
  10525. With prefix ARG, realign all tags in headings in the current buffer."
  10526. (interactive "P")
  10527. (let* ((re (concat "^" outline-regexp))
  10528. (current (org-get-tags-string))
  10529. (col (current-column))
  10530. (org-setting-tags t)
  10531. table current-tags inherited-tags ; computed below when needed
  10532. tags p0 c0 c1 rpl)
  10533. (if arg
  10534. (save-excursion
  10535. (goto-char (point-min))
  10536. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  10537. (while (re-search-forward re nil t)
  10538. (org-set-tags nil t)
  10539. (end-of-line 1)))
  10540. (message "All tags realigned to column %d" org-tags-column))
  10541. (if just-align
  10542. (setq tags current)
  10543. ;; Get a new set of tags from the user
  10544. (save-excursion
  10545. (setq table (append org-tag-persistent-alist
  10546. (or org-tag-alist (org-get-buffer-tags)))
  10547. org-last-tags-completion-table table
  10548. current-tags (org-split-string current ":")
  10549. inherited-tags (nreverse
  10550. (nthcdr (length current-tags)
  10551. (nreverse (org-get-tags-at))))
  10552. tags
  10553. (if (or (eq t org-use-fast-tag-selection)
  10554. (and org-use-fast-tag-selection
  10555. (delq nil (mapcar 'cdr table))))
  10556. (org-fast-tag-selection
  10557. current-tags inherited-tags table
  10558. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  10559. (let ((org-add-colon-after-tag-completion t))
  10560. (org-trim
  10561. (org-without-partial-completion
  10562. (org-icompleting-read "Tags: " 'org-tags-completion-function
  10563. nil nil current 'org-tags-history)))))))
  10564. (while (string-match "[-+&]+" tags)
  10565. ;; No boolean logic, just a list
  10566. (setq tags (replace-match ":" t t tags))))
  10567. (if org-tags-sort-function
  10568. (setq tags (mapconcat 'identity
  10569. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  10570. org-tags-sort-function) ":")))
  10571. (if (string-match "\\`[\t ]*\\'" tags)
  10572. (setq tags "")
  10573. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  10574. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  10575. ;; Insert new tags at the correct column
  10576. (beginning-of-line 1)
  10577. (cond
  10578. ((and (equal current "") (equal tags "")))
  10579. ((re-search-forward
  10580. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  10581. (point-at-eol) t)
  10582. (if (equal tags "")
  10583. (setq rpl "")
  10584. (goto-char (match-beginning 0))
  10585. (setq c0 (current-column) p0 (point)
  10586. c1 (max (1+ c0) (if (> org-tags-column 0)
  10587. org-tags-column
  10588. (- (- org-tags-column) (length tags))))
  10589. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  10590. (replace-match rpl t t)
  10591. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  10592. tags)
  10593. (t (error "Tags alignment failed")))
  10594. (org-move-to-column col)
  10595. (unless just-align
  10596. (run-hooks 'org-after-tags-change-hook)))))
  10597. (defun org-change-tag-in-region (beg end tag off)
  10598. "Add or remove TAG for each entry in the region.
  10599. This works in the agenda, and also in an org-mode buffer."
  10600. (interactive
  10601. (list (region-beginning) (region-end)
  10602. (let ((org-last-tags-completion-table
  10603. (if (org-mode-p)
  10604. (org-get-buffer-tags)
  10605. (org-global-tags-completion-table))))
  10606. (org-icompleting-read
  10607. "Tag: " 'org-tags-completion-function nil nil nil
  10608. 'org-tags-history))
  10609. (progn
  10610. (message "[s]et or [r]emove? ")
  10611. (equal (read-char-exclusive) ?r))))
  10612. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10613. (let ((agendap (equal major-mode 'org-agenda-mode))
  10614. l1 l2 m buf pos newhead (cnt 0))
  10615. (goto-char end)
  10616. (setq l2 (1- (org-current-line)))
  10617. (goto-char beg)
  10618. (setq l1 (org-current-line))
  10619. (loop for l from l1 to l2 do
  10620. (org-goto-line l)
  10621. (setq m (get-text-property (point) 'org-hd-marker))
  10622. (when (or (and (org-mode-p) (org-on-heading-p))
  10623. (and agendap m))
  10624. (setq buf (if agendap (marker-buffer m) (current-buffer))
  10625. pos (if agendap m (point)))
  10626. (with-current-buffer buf
  10627. (save-excursion
  10628. (save-restriction
  10629. (goto-char pos)
  10630. (setq cnt (1+ cnt))
  10631. (org-toggle-tag tag (if off 'off 'on))
  10632. (setq newhead (org-get-heading)))))
  10633. (and agendap (org-agenda-change-all-lines newhead m))))
  10634. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  10635. (defun org-tags-completion-function (string predicate &optional flag)
  10636. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  10637. (confirm (lambda (x) (stringp (car x)))))
  10638. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  10639. (setq s1 (match-string 1 string)
  10640. s2 (match-string 2 string))
  10641. (setq s1 "" s2 string))
  10642. (cond
  10643. ((eq flag nil)
  10644. ;; try completion
  10645. (setq rtn (try-completion s2 ctable confirm))
  10646. (if (stringp rtn)
  10647. (setq rtn
  10648. (concat s1 s2 (substring rtn (length s2))
  10649. (if (and org-add-colon-after-tag-completion
  10650. (assoc rtn ctable))
  10651. ":" ""))))
  10652. rtn)
  10653. ((eq flag t)
  10654. ;; all-completions
  10655. (all-completions s2 ctable confirm)
  10656. )
  10657. ((eq flag 'lambda)
  10658. ;; exact match?
  10659. (assoc s2 ctable)))
  10660. ))
  10661. (defun org-fast-tag-insert (kwd tags face &optional end)
  10662. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  10663. (insert (format "%-12s" (concat kwd ":"))
  10664. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  10665. (or end "")))
  10666. (defun org-fast-tag-show-exit (flag)
  10667. (save-excursion
  10668. (org-goto-line 3)
  10669. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10670. (replace-match ""))
  10671. (when flag
  10672. (end-of-line 1)
  10673. (org-move-to-column (- (window-width) 19) t)
  10674. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10675. (defun org-set-current-tags-overlay (current prefix)
  10676. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10677. (if (featurep 'xemacs)
  10678. (org-overlay-display org-tags-overlay (concat prefix s)
  10679. 'secondary-selection)
  10680. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10681. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10682. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  10683. "Fast tag selection with single keys.
  10684. CURRENT is the current list of tags in the headline, INHERITED is the
  10685. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10686. possibly with grouping information. TODO-TABLE is a similar table with
  10687. TODO keywords, should these have keys assigned to them.
  10688. If the keys are nil, a-z are automatically assigned.
  10689. Returns the new tags string, or nil to not change the current settings."
  10690. (let* ((fulltable (append table todo-table))
  10691. (maxlen (apply 'max (mapcar
  10692. (lambda (x)
  10693. (if (stringp (car x)) (string-width (car x)) 0))
  10694. fulltable)))
  10695. (buf (current-buffer))
  10696. (expert (eq org-fast-tag-selection-single-key 'expert))
  10697. (buffer-tags nil)
  10698. (fwidth (+ maxlen 3 1 3))
  10699. (ncol (/ (- (window-width) 4) fwidth))
  10700. (i-face 'org-done)
  10701. (c-face 'org-todo)
  10702. tg cnt e c char c1 c2 ntable tbl rtn
  10703. ov-start ov-end ov-prefix
  10704. (exit-after-next org-fast-tag-selection-single-key)
  10705. (done-keywords org-done-keywords)
  10706. groups ingroup)
  10707. (save-excursion
  10708. (beginning-of-line 1)
  10709. (if (looking-at
  10710. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10711. (setq ov-start (match-beginning 1)
  10712. ov-end (match-end 1)
  10713. ov-prefix "")
  10714. (setq ov-start (1- (point-at-eol))
  10715. ov-end (1+ ov-start))
  10716. (skip-chars-forward "^\n\r")
  10717. (setq ov-prefix
  10718. (concat
  10719. (buffer-substring (1- (point)) (point))
  10720. (if (> (current-column) org-tags-column)
  10721. " "
  10722. (make-string (- org-tags-column (current-column)) ?\ ))))))
  10723. (org-move-overlay org-tags-overlay ov-start ov-end)
  10724. (save-window-excursion
  10725. (if expert
  10726. (set-buffer (get-buffer-create " *Org tags*"))
  10727. (delete-other-windows)
  10728. (split-window-vertically)
  10729. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10730. (erase-buffer)
  10731. (org-set-local 'org-done-keywords done-keywords)
  10732. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10733. (org-fast-tag-insert "Current" current c-face "\n\n")
  10734. (org-fast-tag-show-exit exit-after-next)
  10735. (org-set-current-tags-overlay current ov-prefix)
  10736. (setq tbl fulltable char ?a cnt 0)
  10737. (while (setq e (pop tbl))
  10738. (cond
  10739. ((equal (car e) :startgroup)
  10740. (push '() groups) (setq ingroup t)
  10741. (when (not (= cnt 0))
  10742. (setq cnt 0)
  10743. (insert "\n"))
  10744. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  10745. ((equal (car e) :endgroup)
  10746. (setq ingroup nil cnt 0)
  10747. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  10748. ((equal e '(:newline))
  10749. (when (not (= cnt 0))
  10750. (setq cnt 0)
  10751. (insert "\n")
  10752. (setq e (car tbl))
  10753. (while (equal (car tbl) '(:newline))
  10754. (insert "\n")
  10755. (setq tbl (cdr tbl)))))
  10756. (t
  10757. (setq tg (copy-sequence (car e)) c2 nil)
  10758. (if (cdr e)
  10759. (setq c (cdr e))
  10760. ;; automatically assign a character.
  10761. (setq c1 (string-to-char
  10762. (downcase (substring
  10763. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10764. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10765. (while (or (rassoc char ntable) (rassoc char table))
  10766. (setq char (1+ char)))
  10767. (setq c2 c1))
  10768. (setq c (or c2 char)))
  10769. (if ingroup (push tg (car groups)))
  10770. (setq tg (org-add-props tg nil 'face
  10771. (cond
  10772. ((not (assoc tg table))
  10773. (org-get-todo-face tg))
  10774. ((member tg current) c-face)
  10775. ((member tg inherited) i-face)
  10776. (t nil))))
  10777. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10778. (insert "[" c "] " tg (make-string
  10779. (- fwidth 4 (length tg)) ?\ ))
  10780. (push (cons tg c) ntable)
  10781. (when (= (setq cnt (1+ cnt)) ncol)
  10782. (insert "\n")
  10783. (if ingroup (insert " "))
  10784. (setq cnt 0)))))
  10785. (setq ntable (nreverse ntable))
  10786. (insert "\n")
  10787. (goto-char (point-min))
  10788. (if (not expert) (org-fit-window-to-buffer))
  10789. (setq rtn
  10790. (catch 'exit
  10791. (while t
  10792. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  10793. (if (not groups) "no " "")
  10794. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10795. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10796. (cond
  10797. ((= c ?\r) (throw 'exit t))
  10798. ((= c ?!)
  10799. (setq groups (not groups))
  10800. (goto-char (point-min))
  10801. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10802. ((= c ?\C-c)
  10803. (if (not expert)
  10804. (org-fast-tag-show-exit
  10805. (setq exit-after-next (not exit-after-next)))
  10806. (setq expert nil)
  10807. (delete-other-windows)
  10808. (split-window-vertically)
  10809. (org-switch-to-buffer-other-window " *Org tags*")
  10810. (org-fit-window-to-buffer)))
  10811. ((or (= c ?\C-g)
  10812. (and (= c ?q) (not (rassoc c ntable))))
  10813. (org-detach-overlay org-tags-overlay)
  10814. (setq quit-flag t))
  10815. ((= c ?\ )
  10816. (setq current nil)
  10817. (if exit-after-next (setq exit-after-next 'now)))
  10818. ((= c ?\t)
  10819. (condition-case nil
  10820. (setq tg (org-icompleting-read
  10821. "Tag: "
  10822. (or buffer-tags
  10823. (with-current-buffer buf
  10824. (org-get-buffer-tags)))))
  10825. (quit (setq tg "")))
  10826. (when (string-match "\\S-" tg)
  10827. (add-to-list 'buffer-tags (list tg))
  10828. (if (member tg current)
  10829. (setq current (delete tg current))
  10830. (push tg current)))
  10831. (if exit-after-next (setq exit-after-next 'now)))
  10832. ((setq e (rassoc c todo-table) tg (car e))
  10833. (with-current-buffer buf
  10834. (save-excursion (org-todo tg)))
  10835. (if exit-after-next (setq exit-after-next 'now)))
  10836. ((setq e (rassoc c ntable) tg (car e))
  10837. (if (member tg current)
  10838. (setq current (delete tg current))
  10839. (loop for g in groups do
  10840. (if (member tg g)
  10841. (mapc (lambda (x)
  10842. (setq current (delete x current)))
  10843. g)))
  10844. (push tg current))
  10845. (if exit-after-next (setq exit-after-next 'now))))
  10846. ;; Create a sorted list
  10847. (setq current
  10848. (sort current
  10849. (lambda (a b)
  10850. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10851. (if (eq exit-after-next 'now) (throw 'exit t))
  10852. (goto-char (point-min))
  10853. (beginning-of-line 2)
  10854. (delete-region (point) (point-at-eol))
  10855. (org-fast-tag-insert "Current" current c-face)
  10856. (org-set-current-tags-overlay current ov-prefix)
  10857. (while (re-search-forward
  10858. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10859. (setq tg (match-string 1))
  10860. (add-text-properties
  10861. (match-beginning 1) (match-end 1)
  10862. (list 'face
  10863. (cond
  10864. ((member tg current) c-face)
  10865. ((member tg inherited) i-face)
  10866. (t (get-text-property (match-beginning 1) 'face))))))
  10867. (goto-char (point-min)))))
  10868. (org-detach-overlay org-tags-overlay)
  10869. (if rtn
  10870. (mapconcat 'identity current ":")
  10871. nil))))
  10872. (defun org-get-tags-string ()
  10873. "Get the TAGS string in the current headline."
  10874. (unless (org-on-heading-p t)
  10875. (error "Not on a heading"))
  10876. (save-excursion
  10877. (beginning-of-line 1)
  10878. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10879. (org-match-string-no-properties 1)
  10880. "")))
  10881. (defun org-get-tags ()
  10882. "Get the list of tags specified in the current headline."
  10883. (org-split-string (org-get-tags-string) ":"))
  10884. (defun org-get-buffer-tags ()
  10885. "Get a table of all tags used in the buffer, for completion."
  10886. (let (tags)
  10887. (save-excursion
  10888. (goto-char (point-min))
  10889. (while (re-search-forward
  10890. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10891. (when (equal (char-after (point-at-bol 0)) ?*)
  10892. (mapc (lambda (x) (add-to-list 'tags x))
  10893. (org-split-string (org-match-string-no-properties 1) ":")))))
  10894. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  10895. (mapcar 'list tags)))
  10896. ;;;; The mapping API
  10897. ;;;###autoload
  10898. (defun org-map-entries (func &optional match scope &rest skip)
  10899. "Call FUNC at each headline selected by MATCH in SCOPE.
  10900. FUNC is a function or a lisp form. The function will be called without
  10901. arguments, with the cursor positioned at the beginning of the headline.
  10902. The return values of all calls to the function will be collected and
  10903. returned as a list.
  10904. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10905. does not need to preserve point. After evaluation, the cursor will be
  10906. moved to the end of the line (presumably of the headline of the
  10907. processed entry) and search continues from there. Under some
  10908. circumstances, this may not produce the wanted results. For example,
  10909. if you have removed (e.g. archived) the current (sub)tree it could
  10910. mean that the next entry will be skipped entirely. In such cases, you
  10911. can specify the position from where search should continue by making
  10912. FUNC set the variable `org-map-continue-from' to the desired buffer
  10913. position.
  10914. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10915. Only headlines that are matched by this query will be considered during
  10916. the iteration. When MATCH is nil or t, all headlines will be
  10917. visited by the iteration.
  10918. SCOPE determines the scope of this command. It can be any of:
  10919. nil The current buffer, respecting the restriction if any
  10920. tree The subtree started with the entry at point
  10921. file The current buffer, without restriction
  10922. file-with-archives
  10923. The current buffer, and any archives associated with it
  10924. agenda All agenda files
  10925. agenda-with-archives
  10926. All agenda files with any archive files associated with them
  10927. \(file1 file2 ...)
  10928. If this is a list, all files in the list will be scanned
  10929. The remaining args are treated as settings for the skipping facilities of
  10930. the scanner. The following items can be given here:
  10931. archive skip trees with the archive tag.
  10932. comment skip trees with the COMMENT keyword
  10933. function or Emacs Lisp form:
  10934. will be used as value for `org-agenda-skip-function', so whenever
  10935. the function returns t, FUNC will not be called for that
  10936. entry and search will continue from the point where the
  10937. function leaves it.
  10938. If your function needs to retrieve the tags including inherited tags
  10939. at the *current* entry, you can use the value of the variable
  10940. `org-scanner-tags' which will be much faster than getting the value
  10941. with `org-get-tags-at'. If your function gets properties with
  10942. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10943. to t around the call to `org-entry-properties' to get the same speedup.
  10944. Note that if your function moves around to retrieve tags and properties at
  10945. a *different* entry, you cannot use these techniques."
  10946. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10947. (org-agenda-skip-archived-trees (memq 'archive skip))
  10948. (org-agenda-skip-comment-trees (memq 'comment skip))
  10949. (org-agenda-skip-function
  10950. (car (org-delete-all '(comment archive) skip)))
  10951. (org-tags-match-list-sublevels t)
  10952. matcher file res
  10953. org-todo-keywords-for-agenda
  10954. org-done-keywords-for-agenda
  10955. org-todo-keyword-alist-for-agenda
  10956. org-drawers-for-agenda
  10957. org-tag-alist-for-agenda)
  10958. (cond
  10959. ((eq match t) (setq matcher t))
  10960. ((eq match nil) (setq matcher t))
  10961. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10962. (save-excursion
  10963. (save-restriction
  10964. (when (eq scope 'tree)
  10965. (org-back-to-heading t)
  10966. (org-narrow-to-subtree)
  10967. (setq scope nil))
  10968. (if (not scope)
  10969. (progn
  10970. (org-prepare-agenda-buffers
  10971. (list (buffer-file-name (current-buffer))))
  10972. (setq res (org-scan-tags func matcher)))
  10973. ;; Get the right scope
  10974. (cond
  10975. ((and scope (listp scope) (symbolp (car scope)))
  10976. (setq scope (eval scope)))
  10977. ((eq scope 'agenda)
  10978. (setq scope (org-agenda-files t)))
  10979. ((eq scope 'agenda-with-archives)
  10980. (setq scope (org-agenda-files t))
  10981. (setq scope (org-add-archive-files scope)))
  10982. ((eq scope 'file)
  10983. (setq scope (list (buffer-file-name))))
  10984. ((eq scope 'file-with-archives)
  10985. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10986. (org-prepare-agenda-buffers scope)
  10987. (while (setq file (pop scope))
  10988. (with-current-buffer (org-find-base-buffer-visiting file)
  10989. (save-excursion
  10990. (save-restriction
  10991. (widen)
  10992. (goto-char (point-min))
  10993. (setq res (append res (org-scan-tags func matcher))))))))))
  10994. res))
  10995. ;;;; Properties
  10996. ;;; Setting and retrieving properties
  10997. (defconst org-special-properties
  10998. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10999. "TIMESTAMP" "TIMESTAMP_IA")
  11000. "The special properties valid in Org-mode.
  11001. These are properties that are not defined in the property drawer,
  11002. but in some other way.")
  11003. (defconst org-default-properties
  11004. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  11005. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  11006. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  11007. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  11008. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  11009. "CLOCK_MODELINE_TOTAL" "STYLE")
  11010. "Some properties that are used by Org-mode for various purposes.
  11011. Being in this list makes sure that they are offered for completion.")
  11012. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  11013. "Regular expression matching the first line of a property drawer.")
  11014. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  11015. "Regular expression matching the first line of a property drawer.")
  11016. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  11017. "Regular expression matching the first line of a property drawer.")
  11018. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  11019. "Regular expression matching the first line of a property drawer.")
  11020. (defconst org-property-drawer-re
  11021. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  11022. org-property-end-re "\\)\n?")
  11023. "Matches an entire property drawer.")
  11024. (defconst org-clock-drawer-re
  11025. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  11026. org-property-end-re "\\)\n?")
  11027. "Matches an entire clock drawer.")
  11028. (defun org-property-action ()
  11029. "Do an action on properties."
  11030. (interactive)
  11031. (let (c)
  11032. (org-at-property-p)
  11033. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  11034. (setq c (read-char-exclusive))
  11035. (cond
  11036. ((equal c ?s)
  11037. (call-interactively 'org-set-property))
  11038. ((equal c ?d)
  11039. (call-interactively 'org-delete-property))
  11040. ((equal c ?D)
  11041. (call-interactively 'org-delete-property-globally))
  11042. ((equal c ?c)
  11043. (call-interactively 'org-compute-property-at-point))
  11044. (t (error "No such property action %c" c)))))
  11045. (defun org-set-effort (&optional value)
  11046. "Set the effort property of the current entry.
  11047. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  11048. allowed value."
  11049. (interactive "P")
  11050. (if (equal value 0) (setq value 10))
  11051. (let* ((completion-ignore-case t)
  11052. (prop org-effort-property)
  11053. (cur (org-entry-get nil prop))
  11054. (allowed (org-property-get-allowed-values nil prop 'table))
  11055. (existing (mapcar 'list (org-property-values prop)))
  11056. rpl
  11057. (val (cond
  11058. ((stringp value) value)
  11059. ((and allowed (integerp value))
  11060. (or (car (nth (1- value) allowed))
  11061. (car (org-last allowed))))
  11062. (allowed
  11063. (message "Select 1-9,0, [RET%s]: %s"
  11064. (if cur (concat "=" cur) "")
  11065. (mapconcat 'car allowed " "))
  11066. (setq rpl (read-char-exclusive))
  11067. (if (equal rpl ?\r)
  11068. cur
  11069. (setq rpl (- rpl ?0))
  11070. (if (equal rpl 0) (setq rpl 10))
  11071. (if (and (> rpl 0) (<= rpl (length allowed)))
  11072. (car (nth (1- rpl) allowed))
  11073. (org-completing-read "Value: " allowed nil))))
  11074. (t
  11075. (let (org-completion-use-ido org-completion-use-iswitchb)
  11076. (org-completing-read
  11077. (concat "Value " (if (and cur (string-match "\\S-" cur))
  11078. (concat "[" cur "]") "")
  11079. ": ")
  11080. existing nil nil "" nil cur))))))
  11081. (unless (equal (org-entry-get nil prop) val)
  11082. (org-entry-put nil prop val))
  11083. (message "%s is now %s" prop val)))
  11084. (defun org-at-property-p ()
  11085. "Is the cursor in a property line?"
  11086. ;; FIXME: Does not check if we are actually in the drawer.
  11087. ;; FIXME: also returns true on any drawers.....
  11088. ;; This is used by C-c C-c for property action.
  11089. (save-excursion
  11090. (beginning-of-line 1)
  11091. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  11092. (defun org-get-property-block (&optional beg end force)
  11093. "Return the (beg . end) range of the body of the property drawer.
  11094. BEG and END can be beginning and end of subtree, if not given
  11095. they will be found.
  11096. If the drawer does not exist and FORCE is non-nil, create the drawer."
  11097. (catch 'exit
  11098. (save-excursion
  11099. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  11100. (end (or end (progn (outline-next-heading) (point)))))
  11101. (goto-char beg)
  11102. (if (re-search-forward org-property-start-re end t)
  11103. (setq beg (1+ (match-end 0)))
  11104. (if force
  11105. (save-excursion
  11106. (org-insert-property-drawer)
  11107. (setq end (progn (outline-next-heading) (point))))
  11108. (throw 'exit nil))
  11109. (goto-char beg)
  11110. (if (re-search-forward org-property-start-re end t)
  11111. (setq beg (1+ (match-end 0)))))
  11112. (if (re-search-forward org-property-end-re end t)
  11113. (setq end (match-beginning 0))
  11114. (or force (throw 'exit nil))
  11115. (goto-char beg)
  11116. (setq end beg)
  11117. (org-indent-line-function)
  11118. (insert ":END:\n"))
  11119. (cons beg end)))))
  11120. (defun org-entry-properties (&optional pom which)
  11121. "Get all properties of the entry at point-or-marker POM.
  11122. This includes the TODO keyword, the tags, time strings for deadline,
  11123. scheduled, and clocking, and any additional properties defined in the
  11124. entry. The return value is an alist, keys may occur multiple times
  11125. if the property key was used several times.
  11126. POM may also be nil, in which case the current entry is used.
  11127. If WHICH is nil or `all', get all properties. If WHICH is
  11128. `special' or `standard', only get that subclass."
  11129. (setq which (or which 'all))
  11130. (org-with-point-at pom
  11131. (let ((clockstr (substring org-clock-string 0 -1))
  11132. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  11133. beg end range props sum-props key value string clocksum)
  11134. (save-excursion
  11135. (when (condition-case nil
  11136. (and (org-mode-p) (org-back-to-heading t))
  11137. (error nil))
  11138. (setq beg (point))
  11139. (setq sum-props (get-text-property (point) 'org-summaries))
  11140. (setq clocksum (get-text-property (point) :org-clock-minutes))
  11141. (outline-next-heading)
  11142. (setq end (point))
  11143. (when (memq which '(all special))
  11144. ;; Get the special properties, like TODO and tags
  11145. (goto-char beg)
  11146. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  11147. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  11148. (when (looking-at org-priority-regexp)
  11149. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  11150. (when (and (setq value (org-get-tags-string))
  11151. (string-match "\\S-" value))
  11152. (push (cons "TAGS" value) props))
  11153. (when (setq value (org-get-tags-at))
  11154. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  11155. props))
  11156. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  11157. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  11158. string (if (equal key clockstr)
  11159. (org-no-properties
  11160. (org-trim
  11161. (buffer-substring
  11162. (match-beginning 3) (goto-char (point-at-eol)))))
  11163. (substring (org-match-string-no-properties 3) 1 -1)))
  11164. (unless key
  11165. (if (= (char-after (match-beginning 3)) ?\[)
  11166. (setq key "TIMESTAMP_IA")
  11167. (setq key "TIMESTAMP")))
  11168. (when (or (equal key clockstr) (not (assoc key props)))
  11169. (push (cons key string) props)))
  11170. )
  11171. (when (memq which '(all standard))
  11172. ;; Get the standard properties, like :PROP: ...
  11173. (setq range (org-get-property-block beg end))
  11174. (when range
  11175. (goto-char (car range))
  11176. (while (re-search-forward
  11177. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  11178. (cdr range) t)
  11179. (setq key (org-match-string-no-properties 1)
  11180. value (org-trim (or (org-match-string-no-properties 2) "")))
  11181. (unless (member key excluded)
  11182. (push (cons key (or value "")) props)))))
  11183. (if clocksum
  11184. (push (cons "CLOCKSUM"
  11185. (org-columns-number-to-string (/ (float clocksum) 60.)
  11186. 'add_times))
  11187. props))
  11188. (unless (assoc "CATEGORY" props)
  11189. (setq value (or (org-get-category)
  11190. (progn (org-refresh-category-properties)
  11191. (org-get-category))))
  11192. (push (cons "CATEGORY" value) props))
  11193. (append sum-props (nreverse props)))))))
  11194. (defun org-entry-get (pom property &optional inherit)
  11195. "Get value of PROPERTY for entry at point-or-marker POM.
  11196. If INHERIT is non-nil and the entry does not have the property,
  11197. then also check higher levels of the hierarchy.
  11198. If INHERIT is the symbol `selective', use inheritance only if the setting
  11199. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  11200. If the property is present but empty, the return value is the empty string.
  11201. If the property is not present at all, nil is returned."
  11202. (org-with-point-at pom
  11203. (if (and inherit (if (eq inherit 'selective)
  11204. (org-property-inherit-p property)
  11205. t))
  11206. (org-entry-get-with-inheritance property)
  11207. (if (member property org-special-properties)
  11208. ;; We need a special property. Use brute force, get all properties.
  11209. (cdr (assoc property (org-entry-properties nil 'special)))
  11210. (let ((range (org-get-property-block)))
  11211. (if (and range
  11212. (goto-char (car range))
  11213. (re-search-forward
  11214. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  11215. (cdr range) t))
  11216. ;; Found the property, return it.
  11217. (if (match-end 1)
  11218. (org-match-string-no-properties 1)
  11219. "")))))))
  11220. (defun org-property-or-variable-value (var &optional inherit)
  11221. "Check if there is a property fixing the value of VAR.
  11222. If yes, return this value. If not, return the current value of the variable."
  11223. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  11224. (if (and prop (stringp prop) (string-match "\\S-" prop))
  11225. (read prop)
  11226. (symbol-value var))))
  11227. (defun org-entry-delete (pom property)
  11228. "Delete the property PROPERTY from entry at point-or-marker POM."
  11229. (org-with-point-at pom
  11230. (if (member property org-special-properties)
  11231. nil ; cannot delete these properties.
  11232. (let ((range (org-get-property-block)))
  11233. (if (and range
  11234. (goto-char (car range))
  11235. (re-search-forward
  11236. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  11237. (cdr range) t))
  11238. (progn
  11239. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  11240. t)
  11241. nil)))))
  11242. ;; Multi-values properties are properties that contain multiple values
  11243. ;; These values are assumed to be single words, separated by whitespace.
  11244. (defun org-entry-add-to-multivalued-property (pom property value)
  11245. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  11246. (let* ((old (org-entry-get pom property))
  11247. (values (and old (org-split-string old "[ \t]"))))
  11248. (setq value (org-entry-protect-space value))
  11249. (unless (member value values)
  11250. (setq values (cons value values))
  11251. (org-entry-put pom property
  11252. (mapconcat 'identity values " ")))))
  11253. (defun org-entry-remove-from-multivalued-property (pom property value)
  11254. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  11255. (let* ((old (org-entry-get pom property))
  11256. (values (and old (org-split-string old "[ \t]"))))
  11257. (setq value (org-entry-protect-space value))
  11258. (when (member value values)
  11259. (setq values (delete value values))
  11260. (org-entry-put pom property
  11261. (mapconcat 'identity values " ")))))
  11262. (defun org-entry-member-in-multivalued-property (pom property value)
  11263. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  11264. (let* ((old (org-entry-get pom property))
  11265. (values (and old (org-split-string old "[ \t]"))))
  11266. (setq value (org-entry-protect-space value))
  11267. (member value values)))
  11268. (defun org-entry-get-multivalued-property (pom property)
  11269. "Return a list of values in a multivalued property."
  11270. (let* ((value (org-entry-get pom property))
  11271. (values (and value (org-split-string value "[ \t]"))))
  11272. (mapcar 'org-entry-restore-space values)))
  11273. (defun org-entry-put-multivalued-property (pom property &rest values)
  11274. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  11275. VALUES should be a list of strings. Spaces will be protected."
  11276. (org-entry-put pom property
  11277. (mapconcat 'org-entry-protect-space values " "))
  11278. (let* ((value (org-entry-get pom property))
  11279. (values (and value (org-split-string value "[ \t]"))))
  11280. (mapcar 'org-entry-restore-space values)))
  11281. (defun org-entry-protect-space (s)
  11282. "Protect spaces and newline in string S."
  11283. (while (string-match " " s)
  11284. (setq s (replace-match "%20" t t s)))
  11285. (while (string-match "\n" s)
  11286. (setq s (replace-match "%0A" t t s)))
  11287. s)
  11288. (defun org-entry-restore-space (s)
  11289. "Restore spaces and newline in string S."
  11290. (while (string-match "%20" s)
  11291. (setq s (replace-match " " t t s)))
  11292. (while (string-match "%0A" s)
  11293. (setq s (replace-match "\n" t t s)))
  11294. s)
  11295. (defvar org-entry-property-inherited-from (make-marker)
  11296. "Marker pointing to the entry from where a property was inherited.
  11297. Each call to `org-entry-get-with-inheritance' will set this marker to the
  11298. location of the entry where the inheritance search matched. If there was
  11299. no match, the marker will point nowhere.
  11300. Note that also `org-entry-get' calls this function, if the INHERIT flag
  11301. is set.")
  11302. (defun org-entry-get-with-inheritance (property)
  11303. "Get entry property, and search higher levels if not present."
  11304. (move-marker org-entry-property-inherited-from nil)
  11305. (let (tmp)
  11306. (save-excursion
  11307. (save-restriction
  11308. (widen)
  11309. (catch 'ex
  11310. (while t
  11311. (when (setq tmp (org-entry-get nil property))
  11312. (org-back-to-heading t)
  11313. (move-marker org-entry-property-inherited-from (point))
  11314. (throw 'ex tmp))
  11315. (or (org-up-heading-safe) (throw 'ex nil)))))
  11316. (or tmp
  11317. (cdr (assoc property org-file-properties))
  11318. (cdr (assoc property org-global-properties))
  11319. (cdr (assoc property org-global-properties-fixed))))))
  11320. (defun org-entry-put (pom property value)
  11321. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  11322. (org-with-point-at pom
  11323. (org-back-to-heading t)
  11324. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  11325. range)
  11326. (cond
  11327. ((equal property "TODO")
  11328. (when (and (stringp value) (string-match "\\S-" value)
  11329. (not (member value org-todo-keywords-1)))
  11330. (error "\"%s\" is not a valid TODO state" value))
  11331. (if (or (not value)
  11332. (not (string-match "\\S-" value)))
  11333. (setq value 'none))
  11334. (org-todo value)
  11335. (org-set-tags nil 'align))
  11336. ((equal property "PRIORITY")
  11337. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  11338. (string-to-char value) ?\ ))
  11339. (org-set-tags nil 'align))
  11340. ((equal property "SCHEDULED")
  11341. (if (re-search-forward org-scheduled-time-regexp end t)
  11342. (cond
  11343. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11344. ((eq value 'later) (org-timestamp-change 1 'day))
  11345. (t (call-interactively 'org-schedule)))
  11346. (call-interactively 'org-schedule)))
  11347. ((equal property "DEADLINE")
  11348. (if (re-search-forward org-deadline-time-regexp end t)
  11349. (cond
  11350. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11351. ((eq value 'later) (org-timestamp-change 1 'day))
  11352. (t (call-interactively 'org-deadline)))
  11353. (call-interactively 'org-deadline)))
  11354. ((member property org-special-properties)
  11355. (error "The %s property can not yet be set with `org-entry-put'"
  11356. property))
  11357. (t ; a non-special property
  11358. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  11359. (setq range (org-get-property-block beg end 'force))
  11360. (goto-char (car range))
  11361. (if (re-search-forward
  11362. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  11363. (progn
  11364. (delete-region (match-beginning 1) (match-end 1))
  11365. (goto-char (match-beginning 1)))
  11366. (goto-char (cdr range))
  11367. (insert "\n")
  11368. (backward-char 1)
  11369. (org-indent-line-function)
  11370. (insert ":" property ":"))
  11371. (and value (insert " " value))
  11372. (org-indent-line-function)))))))
  11373. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  11374. "Get all property keys in the current buffer.
  11375. With INCLUDE-SPECIALS, also list the special properties that reflect things
  11376. like tags and TODO state.
  11377. With INCLUDE-DEFAULTS, also include properties that has special meaning
  11378. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  11379. With INCLUDE-COLUMNS, also include property names given in COLUMN
  11380. formats in the current buffer."
  11381. (let (rtn range cfmt s p)
  11382. (save-excursion
  11383. (save-restriction
  11384. (widen)
  11385. (goto-char (point-min))
  11386. (while (re-search-forward org-property-start-re nil t)
  11387. (setq range (org-get-property-block))
  11388. (goto-char (car range))
  11389. (while (re-search-forward
  11390. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  11391. (cdr range) t)
  11392. (add-to-list 'rtn (org-match-string-no-properties 1)))
  11393. (outline-next-heading))))
  11394. (when include-specials
  11395. (setq rtn (append org-special-properties rtn)))
  11396. (when include-defaults
  11397. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  11398. (add-to-list 'rtn org-effort-property))
  11399. (when include-columns
  11400. (save-excursion
  11401. (save-restriction
  11402. (widen)
  11403. (goto-char (point-min))
  11404. (while (re-search-forward
  11405. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  11406. nil t)
  11407. (setq cfmt (match-string 2) s 0)
  11408. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  11409. cfmt s)
  11410. (setq s (match-end 0)
  11411. p (match-string 1 cfmt))
  11412. (unless (or (equal p "ITEM")
  11413. (member p org-special-properties))
  11414. (add-to-list 'rtn (match-string 1 cfmt))))))))
  11415. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  11416. (defun org-property-values (key)
  11417. "Return a list of all values of property KEY."
  11418. (save-excursion
  11419. (save-restriction
  11420. (widen)
  11421. (goto-char (point-min))
  11422. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  11423. values)
  11424. (while (re-search-forward re nil t)
  11425. (add-to-list 'values (org-trim (match-string 1))))
  11426. (delete "" values)))))
  11427. (defun org-insert-property-drawer ()
  11428. "Insert a property drawer into the current entry."
  11429. (interactive)
  11430. (org-back-to-heading t)
  11431. (looking-at outline-regexp)
  11432. (let ((indent (if org-adapt-indentation
  11433. (- (match-end 0)(match-beginning 0))
  11434. 0))
  11435. (beg (point))
  11436. (re (concat "^[ \t]*" org-keyword-time-regexp))
  11437. end hiddenp)
  11438. (outline-next-heading)
  11439. (setq end (point))
  11440. (goto-char beg)
  11441. (while (re-search-forward re end t))
  11442. (setq hiddenp (org-invisible-p))
  11443. (end-of-line 1)
  11444. (and (equal (char-after) ?\n) (forward-char 1))
  11445. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  11446. (if (member (match-string 1) '("CLOCK:" ":END:"))
  11447. ;; just skip this line
  11448. (beginning-of-line 2)
  11449. ;; Drawer start, find the end
  11450. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  11451. (beginning-of-line 1)))
  11452. (org-skip-over-state-notes)
  11453. (skip-chars-backward " \t\n\r")
  11454. (if (eq (char-before) ?*) (forward-char 1))
  11455. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  11456. (beginning-of-line 0)
  11457. (org-indent-to-column indent)
  11458. (beginning-of-line 2)
  11459. (org-indent-to-column indent)
  11460. (beginning-of-line 0)
  11461. (if hiddenp
  11462. (save-excursion
  11463. (org-back-to-heading t)
  11464. (hide-entry))
  11465. (org-flag-drawer t))))
  11466. (defun org-set-property (property value)
  11467. "In the current entry, set PROPERTY to VALUE.
  11468. When called interactively, this will prompt for a property name, offering
  11469. completion on existing and default properties. And then it will prompt
  11470. for a value, offering completion either on allowed values (via an inherited
  11471. xxx_ALL property) or on existing values in other instances of this property
  11472. in the current file."
  11473. (interactive
  11474. (let* ((completion-ignore-case t)
  11475. (keys (org-buffer-property-keys nil t t))
  11476. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  11477. (prop (if (member prop0 keys)
  11478. prop0
  11479. (or (cdr (assoc (downcase prop0)
  11480. (mapcar (lambda (x) (cons (downcase x) x))
  11481. keys)))
  11482. prop0)))
  11483. (cur (org-entry-get nil prop))
  11484. (allowed (org-property-get-allowed-values nil prop 'table))
  11485. (existing (mapcar 'list (org-property-values prop)))
  11486. (val (if allowed
  11487. (org-completing-read "Value: " allowed nil 'req-match)
  11488. (let (org-completion-use-ido org-completion-use-iswitchb)
  11489. (org-completing-read
  11490. (concat "Value " (if (and cur (string-match "\\S-" cur))
  11491. (concat "[" cur "]") "")
  11492. ": ")
  11493. existing nil nil "" nil cur)))))
  11494. (list prop (if (equal val "") cur val))))
  11495. (unless (equal (org-entry-get nil property) value)
  11496. (org-entry-put nil property value)))
  11497. (defun org-delete-property (property)
  11498. "In the current entry, delete PROPERTY."
  11499. (interactive
  11500. (let* ((completion-ignore-case t)
  11501. (prop (org-icompleting-read
  11502. "Property: " (org-entry-properties nil 'standard))))
  11503. (list prop)))
  11504. (message "Property %s %s" property
  11505. (if (org-entry-delete nil property)
  11506. "deleted"
  11507. "was not present in the entry")))
  11508. (defun org-delete-property-globally (property)
  11509. "Remove PROPERTY globally, from all entries."
  11510. (interactive
  11511. (let* ((completion-ignore-case t)
  11512. (prop (org-icompleting-read
  11513. "Globally remove property: "
  11514. (mapcar 'list (org-buffer-property-keys)))))
  11515. (list prop)))
  11516. (save-excursion
  11517. (save-restriction
  11518. (widen)
  11519. (goto-char (point-min))
  11520. (let ((cnt 0))
  11521. (while (re-search-forward
  11522. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  11523. nil t)
  11524. (setq cnt (1+ cnt))
  11525. (replace-match ""))
  11526. (message "Property \"%s\" removed from %d entries" property cnt)))))
  11527. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  11528. (defun org-compute-property-at-point ()
  11529. "Compute the property at point.
  11530. This looks for an enclosing column format, extracts the operator and
  11531. then applies it to the property in the column format's scope."
  11532. (interactive)
  11533. (unless (org-at-property-p)
  11534. (error "Not at a property"))
  11535. (let ((prop (org-match-string-no-properties 2)))
  11536. (org-columns-get-format-and-top-level)
  11537. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  11538. (error "No operator defined for property %s" prop))
  11539. (org-columns-compute prop)))
  11540. (defun org-property-get-allowed-values (pom property &optional table)
  11541. "Get allowed values for the property PROPERTY.
  11542. When TABLE is non-nil, return an alist that can directly be used for
  11543. completion."
  11544. (let (vals)
  11545. (cond
  11546. ((equal property "TODO")
  11547. (setq vals (org-with-point-at pom
  11548. (append org-todo-keywords-1 '("")))))
  11549. ((equal property "PRIORITY")
  11550. (let ((n org-lowest-priority))
  11551. (while (>= n org-highest-priority)
  11552. (push (char-to-string n) vals)
  11553. (setq n (1- n)))))
  11554. ((member property org-special-properties))
  11555. (t
  11556. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  11557. (when (and vals (string-match "\\S-" vals))
  11558. (setq vals (car (read-from-string (concat "(" vals ")"))))
  11559. (setq vals (mapcar (lambda (x)
  11560. (cond ((stringp x) x)
  11561. ((numberp x) (number-to-string x))
  11562. ((symbolp x) (symbol-name x))
  11563. (t "???")))
  11564. vals)))))
  11565. (if table (mapcar 'list vals) vals)))
  11566. (defun org-property-previous-allowed-value (&optional previous)
  11567. "Switch to the next allowed value for this property."
  11568. (interactive)
  11569. (org-property-next-allowed-value t))
  11570. (defun org-property-next-allowed-value (&optional previous)
  11571. "Switch to the next allowed value for this property."
  11572. (interactive)
  11573. (unless (org-at-property-p)
  11574. (error "Not at a property"))
  11575. (let* ((key (match-string 2))
  11576. (value (match-string 3))
  11577. (allowed (or (org-property-get-allowed-values (point) key)
  11578. (and (member value '("[ ]" "[-]" "[X]"))
  11579. '("[ ]" "[X]"))))
  11580. nval)
  11581. (unless allowed
  11582. (error "Allowed values for this property have not been defined"))
  11583. (if previous (setq allowed (reverse allowed)))
  11584. (if (member value allowed)
  11585. (setq nval (car (cdr (member value allowed)))))
  11586. (setq nval (or nval (car allowed)))
  11587. (if (equal nval value)
  11588. (error "Only one allowed value for this property"))
  11589. (org-at-property-p)
  11590. (replace-match (concat " :" key ": " nval) t t)
  11591. (org-indent-line-function)
  11592. (beginning-of-line 1)
  11593. (skip-chars-forward " \t")))
  11594. (defun org-find-entry-with-id (ident)
  11595. "Locate the entry that contains the ID property with exact value IDENT.
  11596. IDENT can be a string, a symbol or a number, this function will search for
  11597. the string representation of it.
  11598. Return the position where this entry starts, or nil if there is no such entry."
  11599. (interactive "sID: ")
  11600. (let ((id (cond
  11601. ((stringp ident) ident)
  11602. ((symbol-name ident) (symbol-name ident))
  11603. ((numberp ident) (number-to-string ident))
  11604. (t (error "IDENT %s must be a string, symbol or number" ident))))
  11605. (case-fold-search nil))
  11606. (save-excursion
  11607. (save-restriction
  11608. (widen)
  11609. (goto-char (point-min))
  11610. (when (re-search-forward
  11611. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  11612. nil t)
  11613. (org-back-to-heading t)
  11614. (point))))))
  11615. ;;;; Timestamps
  11616. (defvar org-last-changed-timestamp nil)
  11617. (defvar org-last-inserted-timestamp nil
  11618. "The last time stamp inserted with `org-insert-time-stamp'.")
  11619. (defvar org-time-was-given) ; dynamically scoped parameter
  11620. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11621. (defvar org-ts-what) ; dynamically scoped parameter
  11622. (defun org-time-stamp (arg &optional inactive)
  11623. "Prompt for a date/time and insert a time stamp.
  11624. If the user specifies a time like HH:MM, or if this command is called
  11625. with a prefix argument, the time stamp will contain date and time.
  11626. Otherwise, only the date will be included. All parts of a date not
  11627. specified by the user will be filled in from the current date/time.
  11628. So if you press just return without typing anything, the time stamp
  11629. will represent the current date/time. If there is already a timestamp
  11630. at the cursor, it will be modified."
  11631. (interactive "P")
  11632. (let* ((ts nil)
  11633. (default-time
  11634. ;; Default time is either today, or, when entering a range,
  11635. ;; the range start.
  11636. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  11637. (save-excursion
  11638. (re-search-backward
  11639. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  11640. (- (point) 20) t)))
  11641. (apply 'encode-time (org-parse-time-string (match-string 1)))
  11642. (current-time)))
  11643. (default-input (and ts (org-get-compact-tod ts)))
  11644. org-time-was-given org-end-time-was-given time)
  11645. (cond
  11646. ((and (org-at-timestamp-p t)
  11647. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  11648. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  11649. (insert "--")
  11650. (setq time (let ((this-command this-command))
  11651. (org-read-date arg 'totime nil nil
  11652. default-time default-input)))
  11653. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  11654. ((org-at-timestamp-p t)
  11655. (setq time (let ((this-command this-command))
  11656. (org-read-date arg 'totime nil nil default-time default-input)))
  11657. (when (org-at-timestamp-p t) ; just to get the match data
  11658. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  11659. (replace-match "")
  11660. (setq org-last-changed-timestamp
  11661. (org-insert-time-stamp
  11662. time (or org-time-was-given arg)
  11663. inactive nil nil (list org-end-time-was-given))))
  11664. (message "Timestamp updated"))
  11665. (t
  11666. (setq time (let ((this-command this-command))
  11667. (org-read-date arg 'totime nil nil default-time default-input)))
  11668. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  11669. nil nil (list org-end-time-was-given))))))
  11670. ;; FIXME: can we use this for something else, like computing time differences?
  11671. (defun org-get-compact-tod (s)
  11672. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  11673. (let* ((t1 (match-string 1 s))
  11674. (h1 (string-to-number (match-string 2 s)))
  11675. (m1 (string-to-number (match-string 3 s)))
  11676. (t2 (and (match-end 4) (match-string 5 s)))
  11677. (h2 (and t2 (string-to-number (match-string 6 s))))
  11678. (m2 (and t2 (string-to-number (match-string 7 s))))
  11679. dh dm)
  11680. (if (not t2)
  11681. t1
  11682. (setq dh (- h2 h1) dm (- m2 m1))
  11683. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  11684. (concat t1 "+" (number-to-string dh)
  11685. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  11686. (defun org-time-stamp-inactive (&optional arg)
  11687. "Insert an inactive time stamp.
  11688. An inactive time stamp is enclosed in square brackets instead of angle
  11689. brackets. It is inactive in the sense that it does not trigger agenda entries,
  11690. does not link to the calendar and cannot be changed with the S-cursor keys.
  11691. So these are more for recording a certain time/date."
  11692. (interactive "P")
  11693. (org-time-stamp arg 'inactive))
  11694. (defvar org-date-ovl (org-make-overlay 1 1))
  11695. (org-overlay-put org-date-ovl 'face 'org-warning)
  11696. (org-detach-overlay org-date-ovl)
  11697. (defvar org-ans1) ; dynamically scoped parameter
  11698. (defvar org-ans2) ; dynamically scoped parameter
  11699. (defvar org-plain-time-of-day-regexp) ; defined below
  11700. (defvar org-overriding-default-time nil) ; dynamically scoped
  11701. (defvar org-read-date-overlay nil)
  11702. (defvar org-dcst nil) ; dynamically scoped
  11703. (defvar org-read-date-history nil)
  11704. (defvar org-read-date-final-answer nil)
  11705. (defun org-read-date (&optional with-time to-time from-string prompt
  11706. default-time default-input)
  11707. "Read a date, possibly a time, and make things smooth for the user.
  11708. The prompt will suggest to enter an ISO date, but you can also enter anything
  11709. which will at least partially be understood by `parse-time-string'.
  11710. Unrecognized parts of the date will default to the current day, month, year,
  11711. hour and minute. If this command is called to replace a timestamp at point,
  11712. of to enter the second timestamp of a range, the default time is taken from the
  11713. existing stamp. For example,
  11714. 3-2-5 --> 2003-02-05
  11715. feb 15 --> currentyear-02-15
  11716. sep 12 9 --> 2009-09-12
  11717. 12:45 --> today 12:45
  11718. 22 sept 0:34 --> currentyear-09-22 0:34
  11719. 12 --> currentyear-currentmonth-12
  11720. Fri --> nearest Friday (today or later)
  11721. etc.
  11722. Furthermore you can specify a relative date by giving, as the *first* thing
  11723. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  11724. change in days weeks, months, years.
  11725. With a single plus or minus, the date is relative to today. With a double
  11726. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  11727. +4d --> four days from today
  11728. +4 --> same as above
  11729. +2w --> two weeks from today
  11730. ++5 --> five days from default date
  11731. The function understands only English month and weekday abbreviations,
  11732. but this can be configured with the variables `parse-time-months' and
  11733. `parse-time-weekdays'.
  11734. While prompting, a calendar is popped up - you can also select the
  11735. date with the mouse (button 1). The calendar shows a period of three
  11736. months. To scroll it to other months, use the keys `>' and `<'.
  11737. If you don't like the calendar, turn it off with
  11738. \(setq org-read-date-popup-calendar nil)
  11739. With optional argument TO-TIME, the date will immediately be converted
  11740. to an internal time.
  11741. With an optional argument WITH-TIME, the prompt will suggest to also
  11742. insert a time. Note that when WITH-TIME is not set, you can still
  11743. enter a time, and this function will inform the calling routine about
  11744. this change. The calling routine may then choose to change the format
  11745. used to insert the time stamp into the buffer to include the time.
  11746. With optional argument FROM-STRING, read from this string instead from
  11747. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  11748. the time/date that is used for everything that is not specified by the
  11749. user."
  11750. (require 'parse-time)
  11751. (let* ((org-time-stamp-rounding-minutes
  11752. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  11753. (org-dcst org-display-custom-times)
  11754. (ct (org-current-time))
  11755. (def (or org-overriding-default-time default-time ct))
  11756. (defdecode (decode-time def))
  11757. (dummy (progn
  11758. (when (< (nth 2 defdecode) org-extend-today-until)
  11759. (setcar (nthcdr 2 defdecode) -1)
  11760. (setcar (nthcdr 1 defdecode) 59)
  11761. (setq def (apply 'encode-time defdecode)
  11762. defdecode (decode-time def)))))
  11763. (calendar-frame-setup nil)
  11764. (calendar-move-hook nil)
  11765. (calendar-view-diary-initially-flag nil)
  11766. (view-diary-entries-initially nil)
  11767. (calendar-view-holidays-initially-flag nil)
  11768. (view-calendar-holidays-initially nil)
  11769. (timestr (format-time-string
  11770. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  11771. (prompt (concat (if prompt (concat prompt " ") "")
  11772. (format "Date+time [%s]: " timestr)))
  11773. ans (org-ans0 "") org-ans1 org-ans2 final)
  11774. (cond
  11775. (from-string (setq ans from-string))
  11776. (org-read-date-popup-calendar
  11777. (save-excursion
  11778. (save-window-excursion
  11779. (calendar)
  11780. (calendar-forward-day (- (time-to-days def)
  11781. (calendar-absolute-from-gregorian
  11782. (calendar-current-date))))
  11783. (org-eval-in-calendar nil t)
  11784. (let* ((old-map (current-local-map))
  11785. (map (copy-keymap calendar-mode-map))
  11786. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11787. (org-defkey map (kbd "RET") 'org-calendar-select)
  11788. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11789. 'org-calendar-select-mouse)
  11790. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11791. 'org-calendar-select-mouse)
  11792. (org-defkey minibuffer-local-map [(meta shift left)]
  11793. (lambda () (interactive)
  11794. (org-eval-in-calendar '(calendar-backward-month 1))))
  11795. (org-defkey minibuffer-local-map [(meta shift right)]
  11796. (lambda () (interactive)
  11797. (org-eval-in-calendar '(calendar-forward-month 1))))
  11798. (org-defkey minibuffer-local-map [(meta shift up)]
  11799. (lambda () (interactive)
  11800. (org-eval-in-calendar '(calendar-backward-year 1))))
  11801. (org-defkey minibuffer-local-map [(meta shift down)]
  11802. (lambda () (interactive)
  11803. (org-eval-in-calendar '(calendar-forward-year 1))))
  11804. (org-defkey minibuffer-local-map [?\e (shift left)]
  11805. (lambda () (interactive)
  11806. (org-eval-in-calendar '(calendar-backward-month 1))))
  11807. (org-defkey minibuffer-local-map [?\e (shift right)]
  11808. (lambda () (interactive)
  11809. (org-eval-in-calendar '(calendar-forward-month 1))))
  11810. (org-defkey minibuffer-local-map [?\e (shift up)]
  11811. (lambda () (interactive)
  11812. (org-eval-in-calendar '(calendar-backward-year 1))))
  11813. (org-defkey minibuffer-local-map [?\e (shift down)]
  11814. (lambda () (interactive)
  11815. (org-eval-in-calendar '(calendar-forward-year 1))))
  11816. (org-defkey minibuffer-local-map [(shift up)]
  11817. (lambda () (interactive)
  11818. (org-eval-in-calendar '(calendar-backward-week 1))))
  11819. (org-defkey minibuffer-local-map [(shift down)]
  11820. (lambda () (interactive)
  11821. (org-eval-in-calendar '(calendar-forward-week 1))))
  11822. (org-defkey minibuffer-local-map [(shift left)]
  11823. (lambda () (interactive)
  11824. (org-eval-in-calendar '(calendar-backward-day 1))))
  11825. (org-defkey minibuffer-local-map [(shift right)]
  11826. (lambda () (interactive)
  11827. (org-eval-in-calendar '(calendar-forward-day 1))))
  11828. (org-defkey minibuffer-local-map ">"
  11829. (lambda () (interactive)
  11830. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11831. (org-defkey minibuffer-local-map "<"
  11832. (lambda () (interactive)
  11833. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11834. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11835. (unwind-protect
  11836. (progn
  11837. (use-local-map map)
  11838. (add-hook 'post-command-hook 'org-read-date-display)
  11839. (setq org-ans0 (read-string prompt default-input
  11840. 'org-read-date-history nil))
  11841. ;; org-ans0: from prompt
  11842. ;; org-ans1: from mouse click
  11843. ;; org-ans2: from calendar motion
  11844. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11845. (remove-hook 'post-command-hook 'org-read-date-display)
  11846. (use-local-map old-map)
  11847. (when org-read-date-overlay
  11848. (org-delete-overlay org-read-date-overlay)
  11849. (setq org-read-date-overlay nil)))))))
  11850. (t ; Naked prompt only
  11851. (unwind-protect
  11852. (setq ans (read-string prompt default-input
  11853. 'org-read-date-history timestr))
  11854. (when org-read-date-overlay
  11855. (org-delete-overlay org-read-date-overlay)
  11856. (setq org-read-date-overlay nil)))))
  11857. (setq final (org-read-date-analyze ans def defdecode))
  11858. (setq org-read-date-final-answer ans)
  11859. (if to-time
  11860. (apply 'encode-time final)
  11861. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11862. (format "%04d-%02d-%02d %02d:%02d"
  11863. (nth 5 final) (nth 4 final) (nth 3 final)
  11864. (nth 2 final) (nth 1 final))
  11865. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11866. (defvar def)
  11867. (defvar defdecode)
  11868. (defvar with-time)
  11869. (defvar org-read-date-analyze-futurep nil)
  11870. (defun org-read-date-display ()
  11871. "Display the current date prompt interpretation in the minibuffer."
  11872. (when org-read-date-display-live
  11873. (when org-read-date-overlay
  11874. (org-delete-overlay org-read-date-overlay))
  11875. (let ((p (point)))
  11876. (end-of-line 1)
  11877. (while (not (equal (buffer-substring
  11878. (max (point-min) (- (point) 4)) (point))
  11879. " "))
  11880. (insert " "))
  11881. (goto-char p))
  11882. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11883. " " (or org-ans1 org-ans2)))
  11884. (org-end-time-was-given nil)
  11885. (f (org-read-date-analyze ans def defdecode))
  11886. (fmts (if org-dcst
  11887. org-time-stamp-custom-formats
  11888. org-time-stamp-formats))
  11889. (fmt (if (or with-time
  11890. (and (boundp 'org-time-was-given) org-time-was-given))
  11891. (cdr fmts)
  11892. (car fmts)))
  11893. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11894. (when (and org-end-time-was-given
  11895. (string-match org-plain-time-of-day-regexp txt))
  11896. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11897. org-end-time-was-given
  11898. (substring txt (match-end 0)))))
  11899. (when org-read-date-analyze-futurep
  11900. (setq txt (concat txt " (=>F)")))
  11901. (setq org-read-date-overlay
  11902. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11903. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11904. (defun org-read-date-analyze (ans def defdecode)
  11905. "Analyse the combined answer of the date prompt."
  11906. ;; FIXME: cleanup and comment
  11907. (let (delta deltan deltaw deltadef year month day
  11908. hour minute second wday pm h2 m2 tl wday1
  11909. iso-year iso-weekday iso-week iso-year iso-date futurep)
  11910. (setq org-read-date-analyze-futurep nil)
  11911. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11912. (setq ans "+0"))
  11913. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11914. (setq ans (replace-match "" t t ans)
  11915. deltan (car delta)
  11916. deltaw (nth 1 delta)
  11917. deltadef (nth 2 delta)))
  11918. ;; Check if there is an iso week date in there
  11919. ;; If yes, sore the info and postpone interpreting it until the rest
  11920. ;; of the parsing is done
  11921. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11922. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11923. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11924. iso-week (string-to-number (match-string 2 ans)))
  11925. (setq ans (replace-match "" t t ans)))
  11926. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11927. (when (string-match
  11928. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11929. (setq year (if (match-end 2)
  11930. (string-to-number (match-string 2 ans))
  11931. (string-to-number (format-time-string "%Y")))
  11932. month (string-to-number (match-string 3 ans))
  11933. day (string-to-number (match-string 4 ans)))
  11934. (if (< year 100) (setq year (+ 2000 year)))
  11935. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11936. t nil ans)))
  11937. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11938. ;; If there is a time with am/pm, and *no* time without it, we convert
  11939. ;; so that matching will be successful.
  11940. (loop for i from 1 to 2 do ; twice, for end time as well
  11941. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11942. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11943. (setq hour (string-to-number (match-string 1 ans))
  11944. minute (if (match-end 3)
  11945. (string-to-number (match-string 3 ans))
  11946. 0)
  11947. pm (equal ?p
  11948. (string-to-char (downcase (match-string 4 ans)))))
  11949. (if (and (= hour 12) (not pm))
  11950. (setq hour 0)
  11951. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11952. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11953. t t ans))))
  11954. ;; Check if a time range is given as a duration
  11955. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11956. (setq hour (string-to-number (match-string 1 ans))
  11957. h2 (+ hour (string-to-number (match-string 3 ans)))
  11958. minute (string-to-number (match-string 2 ans))
  11959. m2 (+ minute (if (match-end 5) (string-to-number
  11960. (match-string 5 ans))0)))
  11961. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11962. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11963. t t ans)))
  11964. ;; Check if there is a time range
  11965. (when (boundp 'org-end-time-was-given)
  11966. (setq org-time-was-given nil)
  11967. (when (and (string-match org-plain-time-of-day-regexp ans)
  11968. (match-end 8))
  11969. (setq org-end-time-was-given (match-string 8 ans))
  11970. (setq ans (concat (substring ans 0 (match-beginning 7))
  11971. (substring ans (match-end 7))))))
  11972. (setq tl (parse-time-string ans)
  11973. day (or (nth 3 tl) (nth 3 defdecode))
  11974. month (or (nth 4 tl)
  11975. (if (and org-read-date-prefer-future
  11976. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11977. (prog1 (1+ (nth 4 defdecode)) (setq futurep t))
  11978. (nth 4 defdecode)))
  11979. year (or (nth 5 tl)
  11980. (if (and org-read-date-prefer-future
  11981. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11982. (prog1 (1+ (nth 5 defdecode)) (setq futurep t))
  11983. (nth 5 defdecode)))
  11984. hour (or (nth 2 tl) (nth 2 defdecode))
  11985. minute (or (nth 1 tl) (nth 1 defdecode))
  11986. second (or (nth 0 tl) 0)
  11987. wday (nth 6 tl))
  11988. (when (and (eq org-read-date-prefer-future 'time)
  11989. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  11990. (equal day (nth 3 defdecode))
  11991. (equal month (nth 4 defdecode))
  11992. (equal year (nth 5 defdecode))
  11993. (nth 2 tl)
  11994. (or (< (nth 2 tl) (nth 2 defdecode))
  11995. (and (= (nth 2 tl) (nth 2 defdecode))
  11996. (nth 1 tl)
  11997. (< (nth 1 tl) (nth 1 defdecode)))))
  11998. (setq day (1+ day)
  11999. futurep t))
  12000. ;; Special date definitions below
  12001. (cond
  12002. (iso-week
  12003. ;; There was an iso week
  12004. (setq futurep nil)
  12005. (setq year (or iso-year year)
  12006. day (or iso-weekday wday 1)
  12007. wday nil ; to make sure that the trigger below does not match
  12008. iso-date (calendar-gregorian-from-absolute
  12009. (calendar-absolute-from-iso
  12010. (list iso-week day year))))
  12011. ; FIXME: Should we also push ISO weeks into the future?
  12012. ; (when (and org-read-date-prefer-future
  12013. ; (not iso-year)
  12014. ; (< (calendar-absolute-from-gregorian iso-date)
  12015. ; (time-to-days (current-time))))
  12016. ; (setq year (1+ year)
  12017. ; iso-date (calendar-gregorian-from-absolute
  12018. ; (calendar-absolute-from-iso
  12019. ; (list iso-week day year)))))
  12020. (setq month (car iso-date)
  12021. year (nth 2 iso-date)
  12022. day (nth 1 iso-date)))
  12023. (deltan
  12024. (setq futurep nil)
  12025. (unless deltadef
  12026. (let ((now (decode-time (current-time))))
  12027. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  12028. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  12029. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  12030. ((equal deltaw "m") (setq month (+ month deltan)))
  12031. ((equal deltaw "y") (setq year (+ year deltan)))))
  12032. ((and wday (not (nth 3 tl)))
  12033. (setq futurep nil)
  12034. ;; Weekday was given, but no day, so pick that day in the week
  12035. ;; on or after the derived date.
  12036. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  12037. (unless (equal wday wday1)
  12038. (setq day (+ day (% (- wday wday1 -7) 7))))))
  12039. (if (and (boundp 'org-time-was-given)
  12040. (nth 2 tl))
  12041. (setq org-time-was-given t))
  12042. (if (< year 100) (setq year (+ 2000 year)))
  12043. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  12044. (setq org-read-date-analyze-futurep futurep)
  12045. (list second minute hour day month year)))
  12046. (defvar parse-time-weekdays)
  12047. (defun org-read-date-get-relative (s today default)
  12048. "Check string S for special relative date string.
  12049. TODAY and DEFAULT are internal times, for today and for a default.
  12050. Return shift list (N what def-flag)
  12051. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  12052. N is the number of WHATs to shift.
  12053. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  12054. the DEFAULT date rather than TODAY."
  12055. (when (and
  12056. (string-match
  12057. (concat
  12058. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  12059. "\\([0-9]+\\)?"
  12060. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  12061. "\\([ \t]\\|$\\)") s)
  12062. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  12063. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  12064. (string-to-char (substring (match-string 1 s) -1))
  12065. ?+))
  12066. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  12067. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  12068. (what (if (match-end 3) (match-string 3 s) "d"))
  12069. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  12070. (date (if rel default today))
  12071. (wday (nth 6 (decode-time date)))
  12072. delta)
  12073. (if wday1
  12074. (progn
  12075. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  12076. (if (= dir ?-) (setq delta (- delta 7)))
  12077. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  12078. (list delta "d" rel))
  12079. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  12080. (defun org-eval-in-calendar (form &optional keepdate)
  12081. "Eval FORM in the calendar window and return to current window.
  12082. Also, store the cursor date in variable org-ans2."
  12083. (let ((sf (selected-frame))
  12084. (sw (selected-window)))
  12085. (select-window (get-buffer-window "*Calendar*" t))
  12086. (eval form)
  12087. (when (and (not keepdate) (calendar-cursor-to-date))
  12088. (let* ((date (calendar-cursor-to-date))
  12089. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12090. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  12091. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  12092. (select-window sw)
  12093. (org-select-frame-set-input-focus sf)))
  12094. (defun org-calendar-select ()
  12095. "Return to `org-read-date' with the date currently selected.
  12096. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12097. (interactive)
  12098. (when (calendar-cursor-to-date)
  12099. (let* ((date (calendar-cursor-to-date))
  12100. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12101. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12102. (if (active-minibuffer-window) (exit-minibuffer))))
  12103. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  12104. "Insert a date stamp for the date given by the internal TIME.
  12105. WITH-HM means, use the stamp format that includes the time of the day.
  12106. INACTIVE means use square brackets instead of angular ones, so that the
  12107. stamp will not contribute to the agenda.
  12108. PRE and POST are optional strings to be inserted before and after the
  12109. stamp.
  12110. The command returns the inserted time stamp."
  12111. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  12112. stamp)
  12113. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  12114. (insert-before-markers (or pre ""))
  12115. (insert-before-markers (setq stamp (format-time-string fmt time)))
  12116. (when (listp extra)
  12117. (setq extra (car extra))
  12118. (if (and (stringp extra)
  12119. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  12120. (setq extra (format "-%02d:%02d"
  12121. (string-to-number (match-string 1 extra))
  12122. (string-to-number (match-string 2 extra))))
  12123. (setq extra nil)))
  12124. (when extra
  12125. (backward-char 1)
  12126. (insert-before-markers extra)
  12127. (forward-char 1))
  12128. (insert-before-markers (or post ""))
  12129. (setq org-last-inserted-timestamp stamp)))
  12130. (defun org-toggle-time-stamp-overlays ()
  12131. "Toggle the use of custom time stamp formats."
  12132. (interactive)
  12133. (setq org-display-custom-times (not org-display-custom-times))
  12134. (unless org-display-custom-times
  12135. (let ((p (point-min)) (bmp (buffer-modified-p)))
  12136. (while (setq p (next-single-property-change p 'display))
  12137. (if (and (get-text-property p 'display)
  12138. (eq (get-text-property p 'face) 'org-date))
  12139. (remove-text-properties
  12140. p (setq p (next-single-property-change p 'display))
  12141. '(display t))))
  12142. (set-buffer-modified-p bmp)))
  12143. (if (featurep 'xemacs)
  12144. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  12145. (org-restart-font-lock)
  12146. (setq org-table-may-need-update t)
  12147. (if org-display-custom-times
  12148. (message "Time stamps are overlayed with custom format")
  12149. (message "Time stamp overlays removed")))
  12150. (defun org-display-custom-time (beg end)
  12151. "Overlay modified time stamp format over timestamp between BEG and END."
  12152. (let* ((ts (buffer-substring beg end))
  12153. t1 w1 with-hm tf time str w2 (off 0))
  12154. (save-match-data
  12155. (setq t1 (org-parse-time-string ts t))
  12156. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  12157. (setq off (- (match-end 0) (match-beginning 0)))))
  12158. (setq end (- end off))
  12159. (setq w1 (- end beg)
  12160. with-hm (and (nth 1 t1) (nth 2 t1))
  12161. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  12162. time (org-fix-decoded-time t1)
  12163. str (org-add-props
  12164. (format-time-string
  12165. (substring tf 1 -1) (apply 'encode-time time))
  12166. nil 'mouse-face 'highlight)
  12167. w2 (length str))
  12168. (if (not (= w2 w1))
  12169. (add-text-properties (1+ beg) (+ 2 beg)
  12170. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  12171. (if (featurep 'xemacs)
  12172. (progn
  12173. (put-text-property beg end 'invisible t)
  12174. (put-text-property beg end 'end-glyph (make-glyph str)))
  12175. (put-text-property beg end 'display str))))
  12176. (defun org-translate-time (string)
  12177. "Translate all timestamps in STRING to custom format.
  12178. But do this only if the variable `org-display-custom-times' is set."
  12179. (when org-display-custom-times
  12180. (save-match-data
  12181. (let* ((start 0)
  12182. (re org-ts-regexp-both)
  12183. t1 with-hm inactive tf time str beg end)
  12184. (while (setq start (string-match re string start))
  12185. (setq beg (match-beginning 0)
  12186. end (match-end 0)
  12187. t1 (save-match-data
  12188. (org-parse-time-string (substring string beg end) t))
  12189. with-hm (and (nth 1 t1) (nth 2 t1))
  12190. inactive (equal (substring string beg (1+ beg)) "[")
  12191. tf (funcall (if with-hm 'cdr 'car)
  12192. org-time-stamp-custom-formats)
  12193. time (org-fix-decoded-time t1)
  12194. str (format-time-string
  12195. (concat
  12196. (if inactive "[" "<") (substring tf 1 -1)
  12197. (if inactive "]" ">"))
  12198. (apply 'encode-time time))
  12199. string (replace-match str t t string)
  12200. start (+ start (length str)))))))
  12201. string)
  12202. (defun org-fix-decoded-time (time)
  12203. "Set 0 instead of nil for the first 6 elements of time.
  12204. Don't touch the rest."
  12205. (let ((n 0))
  12206. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  12207. (defun org-days-to-time (timestamp-string)
  12208. "Difference between TIMESTAMP-STRING and now in days."
  12209. (- (time-to-days (org-time-string-to-time timestamp-string))
  12210. (time-to-days (current-time))))
  12211. (defun org-deadline-close (timestamp-string &optional ndays)
  12212. "Is the time in TIMESTAMP-STRING close to the current date?"
  12213. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  12214. (and (< (org-days-to-time timestamp-string) ndays)
  12215. (not (org-entry-is-done-p))))
  12216. (defun org-get-wdays (ts)
  12217. "Get the deadline lead time appropriate for timestring TS."
  12218. (cond
  12219. ((<= org-deadline-warning-days 0)
  12220. ;; 0 or negative, enforce this value no matter what
  12221. (- org-deadline-warning-days))
  12222. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  12223. ;; lead time is specified.
  12224. (floor (* (string-to-number (match-string 1 ts))
  12225. (cdr (assoc (match-string 2 ts)
  12226. '(("d" . 1) ("w" . 7)
  12227. ("m" . 30.4) ("y" . 365.25)))))))
  12228. ;; go for the default.
  12229. (t org-deadline-warning-days)))
  12230. (defun org-calendar-select-mouse (ev)
  12231. "Return to `org-read-date' with the date currently selected.
  12232. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12233. (interactive "e")
  12234. (mouse-set-point ev)
  12235. (when (calendar-cursor-to-date)
  12236. (let* ((date (calendar-cursor-to-date))
  12237. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12238. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12239. (if (active-minibuffer-window) (exit-minibuffer))))
  12240. (defun org-check-deadlines (ndays)
  12241. "Check if there are any deadlines due or past due.
  12242. A deadline is considered due if it happens within `org-deadline-warning-days'
  12243. days from today's date. If the deadline appears in an entry marked DONE,
  12244. it is not shown. The prefix arg NDAYS can be used to test that many
  12245. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  12246. (interactive "P")
  12247. (let* ((org-warn-days
  12248. (cond
  12249. ((equal ndays '(4)) 100000)
  12250. (ndays (prefix-numeric-value ndays))
  12251. (t (abs org-deadline-warning-days))))
  12252. (case-fold-search nil)
  12253. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  12254. (callback
  12255. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  12256. (message "%d deadlines past-due or due within %d days"
  12257. (org-occur regexp nil callback)
  12258. org-warn-days)))
  12259. (defun org-check-before-date (date)
  12260. "Check if there are deadlines or scheduled entries before DATE."
  12261. (interactive (list (org-read-date)))
  12262. (let ((case-fold-search nil)
  12263. (regexp (concat "\\<\\(" org-deadline-string
  12264. "\\|" org-scheduled-string
  12265. "\\) *<\\([^>]+\\)>"))
  12266. (callback
  12267. (lambda () (time-less-p
  12268. (org-time-string-to-time (match-string 2))
  12269. (org-time-string-to-time date)))))
  12270. (message "%d entries before %s"
  12271. (org-occur regexp nil callback) date)))
  12272. (defun org-check-after-date (date)
  12273. "Check if there are deadlines or scheduled entries after DATE."
  12274. (interactive (list (org-read-date)))
  12275. (let ((case-fold-search nil)
  12276. (regexp (concat "\\<\\(" org-deadline-string
  12277. "\\|" org-scheduled-string
  12278. "\\) *<\\([^>]+\\)>"))
  12279. (callback
  12280. (lambda () (not
  12281. (time-less-p
  12282. (org-time-string-to-time (match-string 2))
  12283. (org-time-string-to-time date))))))
  12284. (message "%d entries after %s"
  12285. (org-occur regexp nil callback) date)))
  12286. (defun org-evaluate-time-range (&optional to-buffer)
  12287. "Evaluate a time range by computing the difference between start and end.
  12288. Normally the result is just printed in the echo area, but with prefix arg
  12289. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  12290. If the time range is actually in a table, the result is inserted into the
  12291. next column.
  12292. For time difference computation, a year is assumed to be exactly 365
  12293. days in order to avoid rounding problems."
  12294. (interactive "P")
  12295. (or
  12296. (org-clock-update-time-maybe)
  12297. (save-excursion
  12298. (unless (org-at-date-range-p t)
  12299. (goto-char (point-at-bol))
  12300. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  12301. (if (not (org-at-date-range-p t))
  12302. (error "Not at a time-stamp range, and none found in current line")))
  12303. (let* ((ts1 (match-string 1))
  12304. (ts2 (match-string 2))
  12305. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  12306. (match-end (match-end 0))
  12307. (time1 (org-time-string-to-time ts1))
  12308. (time2 (org-time-string-to-time ts2))
  12309. (t1 (org-float-time time1))
  12310. (t2 (org-float-time time2))
  12311. (diff (abs (- t2 t1)))
  12312. (negative (< (- t2 t1) 0))
  12313. ;; (ys (floor (* 365 24 60 60)))
  12314. (ds (* 24 60 60))
  12315. (hs (* 60 60))
  12316. (fy "%dy %dd %02d:%02d")
  12317. (fy1 "%dy %dd")
  12318. (fd "%dd %02d:%02d")
  12319. (fd1 "%dd")
  12320. (fh "%02d:%02d")
  12321. y d h m align)
  12322. (if havetime
  12323. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12324. y 0
  12325. d (floor (/ diff ds)) diff (mod diff ds)
  12326. h (floor (/ diff hs)) diff (mod diff hs)
  12327. m (floor (/ diff 60)))
  12328. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12329. y 0
  12330. d (floor (+ (/ diff ds) 0.5))
  12331. h 0 m 0))
  12332. (if (not to-buffer)
  12333. (message "%s" (org-make-tdiff-string y d h m))
  12334. (if (org-at-table-p)
  12335. (progn
  12336. (goto-char match-end)
  12337. (setq align t)
  12338. (and (looking-at " *|") (goto-char (match-end 0))))
  12339. (goto-char match-end))
  12340. (if (looking-at
  12341. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  12342. (replace-match ""))
  12343. (if negative (insert " -"))
  12344. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  12345. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  12346. (insert " " (format fh h m))))
  12347. (if align (org-table-align))
  12348. (message "Time difference inserted")))))
  12349. (defun org-make-tdiff-string (y d h m)
  12350. (let ((fmt "")
  12351. (l nil))
  12352. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  12353. l (push y l)))
  12354. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  12355. l (push d l)))
  12356. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  12357. l (push h l)))
  12358. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  12359. l (push m l)))
  12360. (apply 'format fmt (nreverse l))))
  12361. (defun org-time-string-to-time (s)
  12362. (apply 'encode-time (org-parse-time-string s)))
  12363. (defun org-time-string-to-seconds (s)
  12364. (org-float-time (org-time-string-to-time s)))
  12365. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  12366. "Convert a time stamp to an absolute day number.
  12367. If there is a specifyer for a cyclic time stamp, get the closest date to
  12368. DAYNR.
  12369. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  12370. the variable date is bound by the calendar when this is called."
  12371. (cond
  12372. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  12373. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  12374. daynr
  12375. (+ daynr 1000)))
  12376. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  12377. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  12378. (time-to-days (current-time))) (match-string 0 s)
  12379. prefer show-all))
  12380. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  12381. (defun org-days-to-iso-week (days)
  12382. "Return the iso week number."
  12383. (require 'cal-iso)
  12384. (car (calendar-iso-from-absolute days)))
  12385. (defun org-small-year-to-year (year)
  12386. "Convert 2-digit years into 4-digit years.
  12387. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  12388. The year 2000 cannot be abbreviated. Any year larger than 99
  12389. is returned unchanged."
  12390. (if (< year 38)
  12391. (setq year (+ 2000 year))
  12392. (if (< year 100)
  12393. (setq year (+ 1900 year))))
  12394. year)
  12395. (defun org-time-from-absolute (d)
  12396. "Return the time corresponding to date D.
  12397. D may be an absolute day number, or a calendar-type list (month day year)."
  12398. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  12399. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  12400. (defun org-calendar-holiday ()
  12401. "List of holidays, for Diary display in Org-mode."
  12402. (require 'holidays)
  12403. (let ((hl (funcall
  12404. (if (fboundp 'calendar-check-holidays)
  12405. 'calendar-check-holidays 'check-calendar-holidays) date)))
  12406. (if hl (mapconcat 'identity hl "; "))))
  12407. (defun org-diary-sexp-entry (sexp entry date)
  12408. "Process a SEXP diary ENTRY for DATE."
  12409. (require 'diary-lib)
  12410. (let ((result (if calendar-debug-sexp
  12411. (let ((stack-trace-on-error t))
  12412. (eval (car (read-from-string sexp))))
  12413. (condition-case nil
  12414. (eval (car (read-from-string sexp)))
  12415. (error
  12416. (beep)
  12417. (message "Bad sexp at line %d in %s: %s"
  12418. (org-current-line)
  12419. (buffer-file-name) sexp)
  12420. (sleep-for 2))))))
  12421. (cond ((stringp result) result)
  12422. ((and (consp result)
  12423. (stringp (cdr result))) (cdr result))
  12424. (result entry)
  12425. (t nil))))
  12426. (defun org-diary-to-ical-string (frombuf)
  12427. "Get iCalendar entries from diary entries in buffer FROMBUF.
  12428. This uses the icalendar.el library."
  12429. (let* ((tmpdir (if (featurep 'xemacs)
  12430. (temp-directory)
  12431. temporary-file-directory))
  12432. (tmpfile (make-temp-name
  12433. (expand-file-name "orgics" tmpdir)))
  12434. buf rtn b e)
  12435. (with-current-buffer frombuf
  12436. (icalendar-export-region (point-min) (point-max) tmpfile)
  12437. (setq buf (find-buffer-visiting tmpfile))
  12438. (set-buffer buf)
  12439. (goto-char (point-min))
  12440. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  12441. (setq b (match-beginning 0)))
  12442. (goto-char (point-max))
  12443. (if (re-search-backward "^END:VEVENT" nil t)
  12444. (setq e (match-end 0)))
  12445. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  12446. (kill-buffer buf)
  12447. (delete-file tmpfile)
  12448. rtn))
  12449. (defun org-closest-date (start current change prefer show-all)
  12450. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  12451. When PREFER is `past' return a date that is either CURRENT or past.
  12452. When PREFER is `future', return a date that is either CURRENT or future.
  12453. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  12454. ;; Make the proper lists from the dates
  12455. (catch 'exit
  12456. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  12457. dn dw sday cday n1 n2 n0
  12458. d m y y1 y2 date1 date2 nmonths nm ny m2)
  12459. (setq start (org-date-to-gregorian start)
  12460. current (org-date-to-gregorian
  12461. (if show-all
  12462. current
  12463. (time-to-days (current-time))))
  12464. sday (calendar-absolute-from-gregorian start)
  12465. cday (calendar-absolute-from-gregorian current))
  12466. (if (<= cday sday) (throw 'exit sday))
  12467. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  12468. (setq dn (string-to-number (match-string 1 change))
  12469. dw (cdr (assoc (match-string 2 change) a1)))
  12470. (error "Invalid change specifyer: %s" change))
  12471. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  12472. (cond
  12473. ((eq dw 'day)
  12474. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  12475. n2 (+ n1 dn)))
  12476. ((eq dw 'year)
  12477. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  12478. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  12479. (setq date1 (list m d y1)
  12480. n1 (calendar-absolute-from-gregorian date1)
  12481. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  12482. n2 (calendar-absolute-from-gregorian date2)))
  12483. ((eq dw 'month)
  12484. ;; approx number of month between the two dates
  12485. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  12486. ;; How often does dn fit in there?
  12487. (setq d (nth 1 start) m (car start) y (nth 2 start)
  12488. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  12489. m (+ m nm)
  12490. ny (floor (/ m 12))
  12491. y (+ y ny)
  12492. m (- m (* ny 12)))
  12493. (while (> m 12) (setq m (- m 12) y (1+ y)))
  12494. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  12495. (setq m2 (+ m dn) y2 y)
  12496. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12497. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  12498. (while (<= n2 cday)
  12499. (setq n1 n2 m m2 y y2)
  12500. (setq m2 (+ m dn) y2 y)
  12501. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12502. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  12503. ;; Make sure n1 is the earlier date
  12504. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  12505. (if show-all
  12506. (cond
  12507. ((eq prefer 'past) (if (= cday n2) n2 n1))
  12508. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12509. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  12510. (cond
  12511. ((eq prefer 'past) (if (= cday n2) n2 n1))
  12512. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12513. (t (if (= cday n1) n1 n2)))))))
  12514. (defun org-date-to-gregorian (date)
  12515. "Turn any specification of DATE into a gregorian date for the calendar."
  12516. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  12517. ((and (listp date) (= (length date) 3)) date)
  12518. ((stringp date)
  12519. (setq date (org-parse-time-string date))
  12520. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  12521. ((listp date)
  12522. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  12523. (defun org-parse-time-string (s &optional nodefault)
  12524. "Parse the standard Org-mode time string.
  12525. This should be a lot faster than the normal `parse-time-string'.
  12526. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  12527. hour and minute fields will be nil if not given."
  12528. (if (string-match org-ts-regexp0 s)
  12529. (list 0
  12530. (if (or (match-beginning 8) (not nodefault))
  12531. (string-to-number (or (match-string 8 s) "0")))
  12532. (if (or (match-beginning 7) (not nodefault))
  12533. (string-to-number (or (match-string 7 s) "0")))
  12534. (string-to-number (match-string 4 s))
  12535. (string-to-number (match-string 3 s))
  12536. (string-to-number (match-string 2 s))
  12537. nil nil nil)
  12538. (error "Not a standard Org-mode time string: %s" s)))
  12539. (defun org-timestamp-up (&optional arg)
  12540. "Increase the date item at the cursor by one.
  12541. If the cursor is on the year, change the year. If it is on the month or
  12542. the day, change that.
  12543. With prefix ARG, change by that many units."
  12544. (interactive "p")
  12545. (org-timestamp-change (prefix-numeric-value arg)))
  12546. (defun org-timestamp-down (&optional arg)
  12547. "Decrease the date item at the cursor by one.
  12548. If the cursor is on the year, change the year. If it is on the month or
  12549. the day, change that.
  12550. With prefix ARG, change by that many units."
  12551. (interactive "p")
  12552. (org-timestamp-change (- (prefix-numeric-value arg))))
  12553. (defun org-timestamp-up-day (&optional arg)
  12554. "Increase the date in the time stamp by one day.
  12555. With prefix ARG, change that many days."
  12556. (interactive "p")
  12557. (if (and (not (org-at-timestamp-p t))
  12558. (org-on-heading-p))
  12559. (org-todo 'up)
  12560. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  12561. (defun org-timestamp-down-day (&optional arg)
  12562. "Decrease the date in the time stamp by one day.
  12563. With prefix ARG, change that many days."
  12564. (interactive "p")
  12565. (if (and (not (org-at-timestamp-p t))
  12566. (org-on-heading-p))
  12567. (org-todo 'down)
  12568. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  12569. (defun org-at-timestamp-p (&optional inactive-ok)
  12570. "Determine if the cursor is in or at a timestamp."
  12571. (interactive)
  12572. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  12573. (pos (point))
  12574. (ans (or (looking-at tsr)
  12575. (save-excursion
  12576. (skip-chars-backward "^[<\n\r\t")
  12577. (if (> (point) (point-min)) (backward-char 1))
  12578. (and (looking-at tsr)
  12579. (> (- (match-end 0) pos) -1))))))
  12580. (and ans
  12581. (boundp 'org-ts-what)
  12582. (setq org-ts-what
  12583. (cond
  12584. ((= pos (match-beginning 0)) 'bracket)
  12585. ((= pos (1- (match-end 0))) 'bracket)
  12586. ((org-pos-in-match-range pos 2) 'year)
  12587. ((org-pos-in-match-range pos 3) 'month)
  12588. ((org-pos-in-match-range pos 7) 'hour)
  12589. ((org-pos-in-match-range pos 8) 'minute)
  12590. ((or (org-pos-in-match-range pos 4)
  12591. (org-pos-in-match-range pos 5)) 'day)
  12592. ((and (> pos (or (match-end 8) (match-end 5)))
  12593. (< pos (match-end 0)))
  12594. (- pos (or (match-end 8) (match-end 5))))
  12595. (t 'day))))
  12596. ans))
  12597. (defun org-toggle-timestamp-type ()
  12598. "Toggle the type (<active> or [inactive]) of a time stamp."
  12599. (interactive)
  12600. (when (org-at-timestamp-p t)
  12601. (let ((beg (match-beginning 0)) (end (match-end 0))
  12602. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  12603. (save-excursion
  12604. (goto-char beg)
  12605. (while (re-search-forward "[][<>]" end t)
  12606. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  12607. t t)))
  12608. (message "Timestamp is now %sactive"
  12609. (if (equal (char-after beg) ?<) "" "in")))))
  12610. (defun org-timestamp-change (n &optional what)
  12611. "Change the date in the time stamp at point.
  12612. The date will be changed by N times WHAT. WHAT can be `day', `month',
  12613. `year', `minute', `second'. If WHAT is not given, the cursor position
  12614. in the timestamp determines what will be changed."
  12615. (let ((pos (point))
  12616. with-hm inactive
  12617. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  12618. org-ts-what
  12619. extra rem
  12620. ts time time0)
  12621. (if (not (org-at-timestamp-p t))
  12622. (error "Not at a timestamp"))
  12623. (if (and (not what) (eq org-ts-what 'bracket))
  12624. (org-toggle-timestamp-type)
  12625. (if (and (not what) (not (eq org-ts-what 'day))
  12626. org-display-custom-times
  12627. (get-text-property (point) 'display)
  12628. (not (get-text-property (1- (point)) 'display)))
  12629. (setq org-ts-what 'day))
  12630. (setq org-ts-what (or what org-ts-what)
  12631. inactive (= (char-after (match-beginning 0)) ?\[)
  12632. ts (match-string 0))
  12633. (replace-match "")
  12634. (if (string-match
  12635. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  12636. ts)
  12637. (setq extra (match-string 1 ts)))
  12638. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  12639. (setq with-hm t))
  12640. (setq time0 (org-parse-time-string ts))
  12641. (when (and (eq org-ts-what 'minute)
  12642. (eq current-prefix-arg nil))
  12643. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  12644. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  12645. (setcar (cdr time0) (+ (nth 1 time0)
  12646. (if (> n 0) (- rem) (- dm rem))))))
  12647. (setq time
  12648. (encode-time (or (car time0) 0)
  12649. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  12650. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  12651. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  12652. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  12653. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  12654. (nthcdr 6 time0)))
  12655. (when (and (member org-ts-what '(hour minute))
  12656. extra
  12657. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  12658. (setq extra (org-modify-ts-extra
  12659. extra
  12660. (if (eq org-ts-what 'hour) 2 5)
  12661. n dm)))
  12662. (when (integerp org-ts-what)
  12663. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  12664. (if (eq what 'calendar)
  12665. (let ((cal-date (org-get-date-from-calendar)))
  12666. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  12667. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  12668. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  12669. (setcar time0 (or (car time0) 0))
  12670. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  12671. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  12672. (setq time (apply 'encode-time time0))))
  12673. (setq org-last-changed-timestamp
  12674. (org-insert-time-stamp time with-hm inactive nil nil extra))
  12675. (org-clock-update-time-maybe)
  12676. (goto-char pos)
  12677. ;; Try to recenter the calendar window, if any
  12678. (if (and org-calendar-follow-timestamp-change
  12679. (get-buffer-window "*Calendar*" t)
  12680. (memq org-ts-what '(day month year)))
  12681. (org-recenter-calendar (time-to-days time))))))
  12682. (defun org-modify-ts-extra (s pos n dm)
  12683. "Change the different parts of the lead-time and repeat fields in timestamp."
  12684. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  12685. ng h m new rem)
  12686. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  12687. (cond
  12688. ((or (org-pos-in-match-range pos 2)
  12689. (org-pos-in-match-range pos 3))
  12690. (setq m (string-to-number (match-string 3 s))
  12691. h (string-to-number (match-string 2 s)))
  12692. (if (org-pos-in-match-range pos 2)
  12693. (setq h (+ h n))
  12694. (setq n (* dm (org-no-warnings (signum n))))
  12695. (when (not (= 0 (setq rem (% m dm))))
  12696. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  12697. (setq m (+ m n)))
  12698. (if (< m 0) (setq m (+ m 60) h (1- h)))
  12699. (if (> m 59) (setq m (- m 60) h (1+ h)))
  12700. (setq h (min 24 (max 0 h)))
  12701. (setq ng 1 new (format "-%02d:%02d" h m)))
  12702. ((org-pos-in-match-range pos 6)
  12703. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  12704. ((org-pos-in-match-range pos 5)
  12705. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  12706. ((org-pos-in-match-range pos 9)
  12707. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  12708. ((org-pos-in-match-range pos 8)
  12709. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  12710. (when ng
  12711. (setq s (concat
  12712. (substring s 0 (match-beginning ng))
  12713. new
  12714. (substring s (match-end ng))))))
  12715. s))
  12716. (defun org-recenter-calendar (date)
  12717. "If the calendar is visible, recenter it to DATE."
  12718. (let* ((win (selected-window))
  12719. (cwin (get-buffer-window "*Calendar*" t))
  12720. (calendar-move-hook nil))
  12721. (when cwin
  12722. (select-window cwin)
  12723. (calendar-goto-date (if (listp date) date
  12724. (calendar-gregorian-from-absolute date)))
  12725. (select-window win))))
  12726. (defun org-goto-calendar (&optional arg)
  12727. "Go to the Emacs calendar at the current date.
  12728. If there is a time stamp in the current line, go to that date.
  12729. A prefix ARG can be used to force the current date."
  12730. (interactive "P")
  12731. (let ((tsr org-ts-regexp) diff
  12732. (calendar-move-hook nil)
  12733. (calendar-view-holidays-initially-flag nil)
  12734. (view-calendar-holidays-initially nil)
  12735. (calendar-view-diary-initially-flag nil)
  12736. (view-diary-entries-initially nil))
  12737. (if (or (org-at-timestamp-p)
  12738. (save-excursion
  12739. (beginning-of-line 1)
  12740. (looking-at (concat ".*" tsr))))
  12741. (let ((d1 (time-to-days (current-time)))
  12742. (d2 (time-to-days
  12743. (org-time-string-to-time (match-string 1)))))
  12744. (setq diff (- d2 d1))))
  12745. (calendar)
  12746. (calendar-goto-today)
  12747. (if (and diff (not arg)) (calendar-forward-day diff))))
  12748. (defun org-get-date-from-calendar ()
  12749. "Return a list (month day year) of date at point in calendar."
  12750. (with-current-buffer "*Calendar*"
  12751. (save-match-data
  12752. (calendar-cursor-to-date))))
  12753. (defun org-date-from-calendar ()
  12754. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  12755. If there is already a time stamp at the cursor position, update it."
  12756. (interactive)
  12757. (if (org-at-timestamp-p t)
  12758. (org-timestamp-change 0 'calendar)
  12759. (let ((cal-date (org-get-date-from-calendar)))
  12760. (org-insert-time-stamp
  12761. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  12762. (defun org-minutes-to-hh:mm-string (m)
  12763. "Compute H:MM from a number of minutes."
  12764. (let ((h (/ m 60)))
  12765. (setq m (- m (* 60 h)))
  12766. (format org-time-clocksum-format h m)))
  12767. (defun org-hh:mm-string-to-minutes (s)
  12768. "Convert a string H:MM to a number of minutes.
  12769. If the string is just a number, interprete it as minutes.
  12770. In fact, the first hh:mm or number in the string will be taken,
  12771. there can be extra stuff in the string.
  12772. If no number is found, the return value is 0."
  12773. (cond
  12774. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  12775. (+ (* (string-to-number (match-string 1 s)) 60)
  12776. (string-to-number (match-string 2 s))))
  12777. ((string-match "\\([0-9]+\\)" s)
  12778. (string-to-number (match-string 1 s)))
  12779. (t 0)))
  12780. ;;;; Files
  12781. (defun org-save-all-org-buffers ()
  12782. "Save all Org-mode buffers without user confirmation."
  12783. (interactive)
  12784. (message "Saving all Org-mode buffers...")
  12785. (save-some-buffers t 'org-mode-p)
  12786. (when (featurep 'org-id) (org-id-locations-save))
  12787. (message "Saving all Org-mode buffers... done"))
  12788. (defun org-revert-all-org-buffers ()
  12789. "Revert all Org-mode buffers.
  12790. Prompt for confirmation when there are unsaved changes.
  12791. Be sure you know what you are doing before letting this function
  12792. overwrite your changes.
  12793. This function is useful in a setup where one tracks org files
  12794. with a version control system, to revert on one machine after pulling
  12795. changes from another. I believe the procedure must be like this:
  12796. 1. M-x org-save-all-org-buffers
  12797. 2. Pull changes from the other machine, resolve conflicts
  12798. 3. M-x org-revert-all-org-buffers"
  12799. (interactive)
  12800. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  12801. (error "Abort"))
  12802. (save-excursion
  12803. (save-window-excursion
  12804. (mapc
  12805. (lambda (b)
  12806. (when (and (with-current-buffer b (org-mode-p))
  12807. (with-current-buffer b buffer-file-name))
  12808. (switch-to-buffer b)
  12809. (revert-buffer t 'no-confirm)))
  12810. (buffer-list))
  12811. (when (and (featurep 'org-id) org-id-track-globally)
  12812. (org-id-locations-load)))))
  12813. ;;;; Agenda files
  12814. ;;;###autoload
  12815. (defun org-iswitchb (&optional arg)
  12816. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  12817. With a prefix argument, restrict available to files.
  12818. With two prefix arguments, restrict available buffers to agenda files."
  12819. (interactive "P")
  12820. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12821. ((equal arg '(16)) (org-buffer-list 'agenda))
  12822. (t (org-buffer-list)))))
  12823. (switch-to-buffer
  12824. (org-icompleting-read "Org buffer: "
  12825. (mapcar 'list (mapcar 'buffer-name blist))
  12826. nil t))))
  12827. ;;;###autoload
  12828. (defalias 'org-ido-switchb 'org-iswitchb)
  12829. (defun org-buffer-list (&optional predicate exclude-tmp)
  12830. "Return a list of Org buffers.
  12831. PREDICATE can be `export', `files' or `agenda'.
  12832. export restrict the list to Export buffers.
  12833. files restrict the list to buffers visiting Org files.
  12834. agenda restrict the list to buffers visiting agenda files.
  12835. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12836. (let* ((bfn nil)
  12837. (agenda-files (and (eq predicate 'agenda)
  12838. (mapcar 'file-truename (org-agenda-files t))))
  12839. (filter
  12840. (cond
  12841. ((eq predicate 'files)
  12842. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12843. ((eq predicate 'export)
  12844. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12845. ((eq predicate 'agenda)
  12846. (lambda (b)
  12847. (with-current-buffer b
  12848. (and (eq major-mode 'org-mode)
  12849. (setq bfn (buffer-file-name b))
  12850. (member (file-truename bfn) agenda-files)))))
  12851. (t (lambda (b) (with-current-buffer b
  12852. (or (eq major-mode 'org-mode)
  12853. (string-match "\*Org .*Export"
  12854. (buffer-name b)))))))))
  12855. (delq nil
  12856. (mapcar
  12857. (lambda(b)
  12858. (if (and (funcall filter b)
  12859. (or (not exclude-tmp)
  12860. (not (string-match "tmp" (buffer-name b)))))
  12861. b
  12862. nil))
  12863. (buffer-list)))))
  12864. (defun org-agenda-files (&optional unrestricted archives)
  12865. "Get the list of agenda files.
  12866. Optional UNRESTRICTED means return the full list even if a restriction
  12867. is currently in place.
  12868. When ARCHIVES is t, include all archive files hat are really being
  12869. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12870. `org-agenda-archives-mode' is t."
  12871. (let ((files
  12872. (cond
  12873. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12874. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12875. ((listp org-agenda-files) org-agenda-files)
  12876. (t (error "Invalid value of `org-agenda-files'")))))
  12877. (setq files (apply 'append
  12878. (mapcar (lambda (f)
  12879. (if (file-directory-p f)
  12880. (directory-files
  12881. f t org-agenda-file-regexp)
  12882. (list f)))
  12883. files)))
  12884. (when org-agenda-skip-unavailable-files
  12885. (setq files (delq nil
  12886. (mapcar (function
  12887. (lambda (file)
  12888. (and (file-readable-p file) file)))
  12889. files))))
  12890. (when (or (eq archives t)
  12891. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12892. (setq files (org-add-archive-files files)))
  12893. files))
  12894. (defun org-edit-agenda-file-list ()
  12895. "Edit the list of agenda files.
  12896. Depending on setup, this either uses customize to edit the variable
  12897. `org-agenda-files', or it visits the file that is holding the list. In the
  12898. latter case, the buffer is set up in a way that saving it automatically kills
  12899. the buffer and restores the previous window configuration."
  12900. (interactive)
  12901. (if (stringp org-agenda-files)
  12902. (let ((cw (current-window-configuration)))
  12903. (find-file org-agenda-files)
  12904. (org-set-local 'org-window-configuration cw)
  12905. (org-add-hook 'after-save-hook
  12906. (lambda ()
  12907. (set-window-configuration
  12908. (prog1 org-window-configuration
  12909. (kill-buffer (current-buffer))))
  12910. (org-install-agenda-files-menu)
  12911. (message "New agenda file list installed"))
  12912. nil 'local)
  12913. (message "%s" (substitute-command-keys
  12914. "Edit list and finish with \\[save-buffer]")))
  12915. (customize-variable 'org-agenda-files)))
  12916. (defun org-store-new-agenda-file-list (list)
  12917. "Set new value for the agenda file list and save it correctly."
  12918. (if (stringp org-agenda-files)
  12919. (let ((f org-agenda-files) b)
  12920. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12921. (with-temp-file f
  12922. (insert (mapconcat 'identity list "\n") "\n")))
  12923. (let ((org-mode-hook nil) (org-inhibit-startup t)
  12924. (org-insert-mode-line-in-empty-file nil))
  12925. (setq org-agenda-files list)
  12926. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12927. (defun org-read-agenda-file-list ()
  12928. "Read the list of agenda files from a file."
  12929. (when (file-directory-p org-agenda-files)
  12930. (error "`org-agenda-files' cannot be a single directory"))
  12931. (when (stringp org-agenda-files)
  12932. (with-temp-buffer
  12933. (insert-file-contents org-agenda-files)
  12934. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12935. ;;;###autoload
  12936. (defun org-cycle-agenda-files ()
  12937. "Cycle through the files in `org-agenda-files'.
  12938. If the current buffer visits an agenda file, find the next one in the list.
  12939. If the current buffer does not, find the first agenda file."
  12940. (interactive)
  12941. (let* ((fs (org-agenda-files t))
  12942. (files (append fs (list (car fs))))
  12943. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12944. file)
  12945. (unless files (error "No agenda files"))
  12946. (catch 'exit
  12947. (while (setq file (pop files))
  12948. (if (equal (file-truename file) tcf)
  12949. (when (car files)
  12950. (find-file (car files))
  12951. (throw 'exit t))))
  12952. (find-file (car fs)))
  12953. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12954. (defun org-agenda-file-to-front (&optional to-end)
  12955. "Move/add the current file to the top of the agenda file list.
  12956. If the file is not present in the list, it is added to the front. If it is
  12957. present, it is moved there. With optional argument TO-END, add/move to the
  12958. end of the list."
  12959. (interactive "P")
  12960. (let ((org-agenda-skip-unavailable-files nil)
  12961. (file-alist (mapcar (lambda (x)
  12962. (cons (file-truename x) x))
  12963. (org-agenda-files t)))
  12964. (ctf (file-truename buffer-file-name))
  12965. x had)
  12966. (setq x (assoc ctf file-alist) had x)
  12967. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12968. (if to-end
  12969. (setq file-alist (append (delq x file-alist) (list x)))
  12970. (setq file-alist (cons x (delq x file-alist))))
  12971. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12972. (org-install-agenda-files-menu)
  12973. (message "File %s to %s of agenda file list"
  12974. (if had "moved" "added") (if to-end "end" "front"))))
  12975. (defun org-remove-file (&optional file)
  12976. "Remove current file from the list of files in variable `org-agenda-files'.
  12977. These are the files which are being checked for agenda entries.
  12978. Optional argument FILE means, use this file instead of the current."
  12979. (interactive)
  12980. (let* ((org-agenda-skip-unavailable-files nil)
  12981. (file (or file buffer-file-name))
  12982. (true-file (file-truename file))
  12983. (afile (abbreviate-file-name file))
  12984. (files (delq nil (mapcar
  12985. (lambda (x)
  12986. (if (equal true-file
  12987. (file-truename x))
  12988. nil x))
  12989. (org-agenda-files t)))))
  12990. (if (not (= (length files) (length (org-agenda-files t))))
  12991. (progn
  12992. (org-store-new-agenda-file-list files)
  12993. (org-install-agenda-files-menu)
  12994. (message "Removed file: %s" afile))
  12995. (message "File was not in list: %s (not removed)" afile))))
  12996. (defun org-file-menu-entry (file)
  12997. (vector file (list 'find-file file) t))
  12998. (defun org-check-agenda-file (file)
  12999. "Make sure FILE exists. If not, ask user what to do."
  13000. (when (not (file-exists-p file))
  13001. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  13002. (abbreviate-file-name file))
  13003. (let ((r (downcase (read-char-exclusive))))
  13004. (cond
  13005. ((equal r ?r)
  13006. (org-remove-file file)
  13007. (throw 'nextfile t))
  13008. (t (error "Abort"))))))
  13009. (defun org-get-agenda-file-buffer (file)
  13010. "Get a buffer visiting FILE. If the buffer needs to be created, add
  13011. it to the list of buffers which might be released later."
  13012. (let ((buf (org-find-base-buffer-visiting file)))
  13013. (if buf
  13014. buf ; just return it
  13015. ;; Make a new buffer and remember it
  13016. (setq buf (find-file-noselect file))
  13017. (if buf (push buf org-agenda-new-buffers))
  13018. buf)))
  13019. (defun org-release-buffers (blist)
  13020. "Release all buffers in list, asking the user for confirmation when needed.
  13021. When a buffer is unmodified, it is just killed. When modified, it is saved
  13022. \(if the user agrees) and then killed."
  13023. (let (buf file)
  13024. (while (setq buf (pop blist))
  13025. (setq file (buffer-file-name buf))
  13026. (when (and (buffer-modified-p buf)
  13027. file
  13028. (y-or-n-p (format "Save file %s? " file)))
  13029. (with-current-buffer buf (save-buffer)))
  13030. (kill-buffer buf))))
  13031. (defun org-prepare-agenda-buffers (files)
  13032. "Create buffers for all agenda files, protect archived trees and comments."
  13033. (interactive)
  13034. (let ((pa '(:org-archived t))
  13035. (pc '(:org-comment t))
  13036. (pall '(:org-archived t :org-comment t))
  13037. (inhibit-read-only t)
  13038. (rea (concat ":" org-archive-tag ":"))
  13039. bmp file re)
  13040. (save-excursion
  13041. (save-restriction
  13042. (while (setq file (pop files))
  13043. (catch 'nextfile
  13044. (if (bufferp file)
  13045. (set-buffer file)
  13046. (org-check-agenda-file file)
  13047. (set-buffer (org-get-agenda-file-buffer file)))
  13048. (widen)
  13049. (setq bmp (buffer-modified-p))
  13050. (org-refresh-category-properties)
  13051. (setq org-todo-keywords-for-agenda
  13052. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  13053. (setq org-done-keywords-for-agenda
  13054. (append org-done-keywords-for-agenda org-done-keywords))
  13055. (setq org-todo-keyword-alist-for-agenda
  13056. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  13057. (setq org-drawers-for-agenda
  13058. (append org-drawers-for-agenda org-drawers))
  13059. (setq org-tag-alist-for-agenda
  13060. (append org-tag-alist-for-agenda org-tag-alist))
  13061. (save-excursion
  13062. (remove-text-properties (point-min) (point-max) pall)
  13063. (when org-agenda-skip-archived-trees
  13064. (goto-char (point-min))
  13065. (while (re-search-forward rea nil t)
  13066. (if (org-on-heading-p t)
  13067. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  13068. (goto-char (point-min))
  13069. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  13070. (while (re-search-forward re nil t)
  13071. (add-text-properties
  13072. (match-beginning 0) (org-end-of-subtree t) pc)))
  13073. (set-buffer-modified-p bmp)))))
  13074. (setq org-todo-keyword-alist-for-agenda
  13075. (org-uniquify org-todo-keyword-alist-for-agenda)
  13076. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  13077. ;;;; Embedded LaTeX
  13078. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  13079. "Keymap for the minor `org-cdlatex-mode'.")
  13080. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  13081. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  13082. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  13083. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  13084. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  13085. (defvar org-cdlatex-texmathp-advice-is-done nil
  13086. "Flag remembering if we have applied the advice to texmathp already.")
  13087. (define-minor-mode org-cdlatex-mode
  13088. "Toggle the minor `org-cdlatex-mode'.
  13089. This mode supports entering LaTeX environment and math in LaTeX fragments
  13090. in Org-mode.
  13091. \\{org-cdlatex-mode-map}"
  13092. nil " OCDL" nil
  13093. (when org-cdlatex-mode (require 'cdlatex))
  13094. (unless org-cdlatex-texmathp-advice-is-done
  13095. (setq org-cdlatex-texmathp-advice-is-done t)
  13096. (defadvice texmathp (around org-math-always-on activate)
  13097. "Always return t in org-mode buffers.
  13098. This is because we want to insert math symbols without dollars even outside
  13099. the LaTeX math segments. If Orgmode thinks that point is actually inside
  13100. an embedded LaTeX fragment, let texmathp do its job.
  13101. \\[org-cdlatex-mode-map]"
  13102. (interactive)
  13103. (let (p)
  13104. (cond
  13105. ((not (org-mode-p)) ad-do-it)
  13106. ((eq this-command 'cdlatex-math-symbol)
  13107. (setq ad-return-value t
  13108. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  13109. (t
  13110. (let ((p (org-inside-LaTeX-fragment-p)))
  13111. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  13112. (setq ad-return-value t
  13113. texmathp-why '("Org-mode embedded math" . 0))
  13114. (if p ad-do-it)))))))))
  13115. (defun turn-on-org-cdlatex ()
  13116. "Unconditionally turn on `org-cdlatex-mode'."
  13117. (org-cdlatex-mode 1))
  13118. (defun org-inside-LaTeX-fragment-p ()
  13119. "Test if point is inside a LaTeX fragment.
  13120. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  13121. sequence appearing also before point.
  13122. Even though the matchers for math are configurable, this function assumes
  13123. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  13124. delimiters are skipped when they have been removed by customization.
  13125. The return value is nil, or a cons cell with the delimiter and
  13126. and the position of this delimiter.
  13127. This function does a reasonably good job, but can locally be fooled by
  13128. for example currency specifications. For example it will assume being in
  13129. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  13130. fragments that are properly closed, but during editing, we have to live
  13131. with the uncertainty caused by missing closing delimiters. This function
  13132. looks only before point, not after."
  13133. (catch 'exit
  13134. (let ((pos (point))
  13135. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  13136. (lim (progn
  13137. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  13138. (point)))
  13139. dd-on str (start 0) m re)
  13140. (goto-char pos)
  13141. (when dodollar
  13142. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  13143. re (nth 1 (assoc "$" org-latex-regexps)))
  13144. (while (string-match re str start)
  13145. (cond
  13146. ((= (match-end 0) (length str))
  13147. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  13148. ((= (match-end 0) (- (length str) 5))
  13149. (throw 'exit nil))
  13150. (t (setq start (match-end 0))))))
  13151. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  13152. (goto-char pos)
  13153. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  13154. (and (match-beginning 2) (throw 'exit nil))
  13155. ;; count $$
  13156. (while (re-search-backward "\\$\\$" lim t)
  13157. (setq dd-on (not dd-on)))
  13158. (goto-char pos)
  13159. (if dd-on (cons "$$" m))))))
  13160. (defun org-try-cdlatex-tab ()
  13161. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  13162. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  13163. - inside a LaTeX fragment, or
  13164. - after the first word in a line, where an abbreviation expansion could
  13165. insert a LaTeX environment."
  13166. (when org-cdlatex-mode
  13167. (cond
  13168. ((save-excursion
  13169. (skip-chars-backward "a-zA-Z0-9*")
  13170. (skip-chars-backward " \t")
  13171. (bolp))
  13172. (cdlatex-tab) t)
  13173. ((org-inside-LaTeX-fragment-p)
  13174. (cdlatex-tab) t)
  13175. (t nil))))
  13176. (defun org-cdlatex-underscore-caret (&optional arg)
  13177. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  13178. Revert to the normal definition outside of these fragments."
  13179. (interactive "P")
  13180. (if (org-inside-LaTeX-fragment-p)
  13181. (call-interactively 'cdlatex-sub-superscript)
  13182. (let (org-cdlatex-mode)
  13183. (call-interactively (key-binding (vector last-input-event))))))
  13184. (defun org-cdlatex-math-modify (&optional arg)
  13185. "Execute `cdlatex-math-modify' in LaTeX fragments.
  13186. Revert to the normal definition outside of these fragments."
  13187. (interactive "P")
  13188. (if (org-inside-LaTeX-fragment-p)
  13189. (call-interactively 'cdlatex-math-modify)
  13190. (let (org-cdlatex-mode)
  13191. (call-interactively (key-binding (vector last-input-event))))))
  13192. (defvar org-latex-fragment-image-overlays nil
  13193. "List of overlays carrying the images of latex fragments.")
  13194. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  13195. (defun org-remove-latex-fragment-image-overlays ()
  13196. "Remove all overlays with LaTeX fragment images in current buffer."
  13197. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  13198. (setq org-latex-fragment-image-overlays nil))
  13199. (defun org-preview-latex-fragment (&optional subtree)
  13200. "Preview the LaTeX fragment at point, or all locally or globally.
  13201. If the cursor is in a LaTeX fragment, create the image and overlay
  13202. it over the source code. If there is no fragment at point, display
  13203. all fragments in the current text, from one headline to the next. With
  13204. prefix SUBTREE, display all fragments in the current subtree. With a
  13205. double prefix `C-u C-u', or when the cursor is before the first headline,
  13206. display all fragments in the buffer.
  13207. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  13208. (interactive "P")
  13209. (org-remove-latex-fragment-image-overlays)
  13210. (save-excursion
  13211. (save-restriction
  13212. (let (beg end at msg)
  13213. (cond
  13214. ((or (equal subtree '(16))
  13215. (not (save-excursion
  13216. (re-search-backward (concat "^" outline-regexp) nil t))))
  13217. (setq beg (point-min) end (point-max)
  13218. msg "Creating images for buffer...%s"))
  13219. ((equal subtree '(4))
  13220. (org-back-to-heading)
  13221. (setq beg (point) end (org-end-of-subtree t)
  13222. msg "Creating images for subtree...%s"))
  13223. (t
  13224. (if (setq at (org-inside-LaTeX-fragment-p))
  13225. (goto-char (max (point-min) (- (cdr at) 2)))
  13226. (org-back-to-heading))
  13227. (setq beg (point) end (progn (outline-next-heading) (point))
  13228. msg (if at "Creating image...%s"
  13229. "Creating images for entry...%s"))))
  13230. (message msg "")
  13231. (narrow-to-region beg end)
  13232. (goto-char beg)
  13233. (org-format-latex
  13234. (concat "ltxpng/" (file-name-sans-extension
  13235. (file-name-nondirectory
  13236. buffer-file-name)))
  13237. default-directory 'overlays msg at 'forbuffer)
  13238. (message msg "done. Use `C-c C-c' to remove images.")))))
  13239. (defvar org-latex-regexps
  13240. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  13241. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  13242. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  13243. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13244. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13245. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  13246. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  13247. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  13248. "Regular expressions for matching embedded LaTeX.")
  13249. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  13250. "Replace LaTeX fragments with links to an image, and produce images.
  13251. Some of the options can be changed using the variable
  13252. `org-format-latex-options'."
  13253. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  13254. (let* ((prefixnodir (file-name-nondirectory prefix))
  13255. (absprefix (expand-file-name prefix dir))
  13256. (todir (file-name-directory absprefix))
  13257. (opt org-format-latex-options)
  13258. (matchers (plist-get opt :matchers))
  13259. (re-list org-latex-regexps)
  13260. (cnt 0) txt link beg end re e checkdir
  13261. executables-checked
  13262. m n block linkfile movefile ov)
  13263. ;; Check if there are old images files with this prefix, and remove them
  13264. (when (file-directory-p todir)
  13265. (mapc 'delete-file
  13266. (directory-files
  13267. todir 'full
  13268. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  13269. ;; Check the different regular expressions
  13270. (while (setq e (pop re-list))
  13271. (setq m (car e) re (nth 1 e) n (nth 2 e)
  13272. block (if (nth 3 e) "\n\n" ""))
  13273. (when (member m matchers)
  13274. (goto-char (point-min))
  13275. (while (re-search-forward re nil t)
  13276. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  13277. (not (get-text-property (match-beginning n)
  13278. 'org-protected))
  13279. (or (not overlays)
  13280. (not (eq (get-char-property (match-beginning n)
  13281. 'org-overlay-type)
  13282. 'org-latex-overlay))))
  13283. (setq txt (match-string n)
  13284. beg (match-beginning n) end (match-end n)
  13285. cnt (1+ cnt)
  13286. linkfile (format "%s_%04d.png" prefix cnt)
  13287. movefile (format "%s_%04d.png" absprefix cnt)
  13288. link (concat block "[[file:" linkfile "]]" block))
  13289. (if msg (message msg cnt))
  13290. (goto-char beg)
  13291. (unless checkdir ; make sure the directory exists
  13292. (setq checkdir t)
  13293. (or (file-directory-p todir) (make-directory todir)))
  13294. (unless executables-checked
  13295. (org-check-external-command
  13296. "latex" "needed to convert LaTeX fragments to images")
  13297. (org-check-external-command
  13298. "dvipng" "needed to convert LaTeX fragments to images")
  13299. (setq executables-checked t))
  13300. (org-create-formula-image
  13301. txt movefile opt forbuffer)
  13302. (if overlays
  13303. (progn
  13304. (mapc (lambda (o)
  13305. (if (eq (org-overlay-get o 'org-overlay-type)
  13306. 'org-latex-overlay)
  13307. (org-delete-overlay o)))
  13308. (org-overlays-in beg end))
  13309. (setq ov (org-make-overlay beg end))
  13310. (org-overlay-put ov 'org-overlay-type 'org-latex-overlay)
  13311. (if (featurep 'xemacs)
  13312. (progn
  13313. (org-overlay-put ov 'invisible t)
  13314. (org-overlay-put
  13315. ov 'end-glyph
  13316. (make-glyph (vector 'png :file movefile))))
  13317. (org-overlay-put
  13318. ov 'display
  13319. (list 'image :type 'png :file movefile :ascent 'center)))
  13320. (push ov org-latex-fragment-image-overlays)
  13321. (goto-char end))
  13322. (delete-region beg end)
  13323. (insert link))))))))
  13324. (defvar org-export-latex-packages-alist) ;; defined in org-latex.el
  13325. ;; This function borrows from Ganesh Swami's latex2png.el
  13326. (defun org-create-formula-image (string tofile options buffer)
  13327. "This calls dvipng."
  13328. (require 'org-latex)
  13329. (let* ((tmpdir (if (featurep 'xemacs)
  13330. (temp-directory)
  13331. temporary-file-directory))
  13332. (texfilebase (make-temp-name
  13333. (expand-file-name "orgtex" tmpdir)))
  13334. (texfile (concat texfilebase ".tex"))
  13335. (dvifile (concat texfilebase ".dvi"))
  13336. (pngfile (concat texfilebase ".png"))
  13337. (fnh (if (featurep 'xemacs)
  13338. (font-height (get-face-font 'default))
  13339. (face-attribute 'default :height nil)))
  13340. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  13341. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  13342. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  13343. "Black"))
  13344. (bg (or (plist-get options (if buffer :background :html-background))
  13345. "Transparent")))
  13346. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  13347. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  13348. (with-temp-file texfile
  13349. (insert org-format-latex-header
  13350. (if org-export-latex-packages-alist
  13351. (concat "\n"
  13352. (mapconcat (lambda(p)
  13353. (if (equal "" (car p))
  13354. (format "\\usepackage{%s}" (cadr p))
  13355. (format "\\usepackage[%s]{%s}"
  13356. (car p) (cadr p))))
  13357. org-export-latex-packages-alist "\n"))
  13358. "")
  13359. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  13360. (let ((dir default-directory))
  13361. (condition-case nil
  13362. (progn
  13363. (cd tmpdir)
  13364. (call-process "latex" nil nil nil texfile))
  13365. (error nil))
  13366. (cd dir))
  13367. (if (not (file-exists-p dvifile))
  13368. (progn (message "Failed to create dvi file from %s" texfile) nil)
  13369. (condition-case nil
  13370. (call-process "dvipng" nil nil nil
  13371. "-fg" fg "-bg" bg
  13372. "-D" dpi
  13373. ;;"-x" scale "-y" scale
  13374. "-T" "tight"
  13375. "-o" pngfile
  13376. dvifile)
  13377. (error nil))
  13378. (if (not (file-exists-p pngfile))
  13379. (progn (message "Failed to create png file from %s" texfile) nil)
  13380. ;; Use the requested file name and clean up
  13381. (copy-file pngfile tofile 'replace)
  13382. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  13383. (delete-file (concat texfilebase e)))
  13384. pngfile))))
  13385. (defun org-dvipng-color (attr)
  13386. "Return an rgb color specification for dvipng."
  13387. (apply 'format "rgb %s %s %s"
  13388. (mapcar 'org-normalize-color
  13389. (color-values (face-attribute 'default attr nil)))))
  13390. (defun org-normalize-color (value)
  13391. "Return string to be used as color value for an RGB component."
  13392. (format "%g" (/ value 65535.0)))
  13393. ;;;; Key bindings
  13394. ;; Make `C-c C-x' a prefix key
  13395. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  13396. ;; TAB key with modifiers
  13397. (org-defkey org-mode-map "\C-i" 'org-cycle)
  13398. (org-defkey org-mode-map [(tab)] 'org-cycle)
  13399. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  13400. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  13401. (org-defkey org-mode-map "\M-\t" 'org-complete)
  13402. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  13403. ;; The following line is necessary under Suse GNU/Linux
  13404. (unless (featurep 'xemacs)
  13405. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  13406. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  13407. (define-key org-mode-map [backtab] 'org-shifttab)
  13408. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  13409. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  13410. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  13411. ;; Cursor keys with modifiers
  13412. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  13413. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  13414. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  13415. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  13416. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  13417. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  13418. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  13419. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  13420. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  13421. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  13422. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  13423. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  13424. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  13425. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  13426. ;;; Extra keys for tty access.
  13427. ;; We only set them when really needed because otherwise the
  13428. ;; menus don't show the simple keys
  13429. (when (or org-use-extra-keys
  13430. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  13431. (not window-system))
  13432. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  13433. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  13434. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  13435. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  13436. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  13437. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  13438. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  13439. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  13440. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  13441. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  13442. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  13443. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  13444. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  13445. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  13446. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  13447. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  13448. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  13449. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  13450. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  13451. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  13452. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  13453. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  13454. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  13455. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  13456. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  13457. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  13458. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  13459. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  13460. ;; All the other keys
  13461. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  13462. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  13463. (if (boundp 'narrow-map)
  13464. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  13465. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  13466. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  13467. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  13468. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  13469. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  13470. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  13471. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  13472. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  13473. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  13474. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  13475. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  13476. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  13477. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  13478. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  13479. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  13480. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  13481. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  13482. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  13483. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  13484. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  13485. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  13486. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  13487. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  13488. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  13489. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  13490. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  13491. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  13492. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  13493. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  13494. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  13495. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  13496. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  13497. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  13498. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  13499. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  13500. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  13501. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  13502. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  13503. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  13504. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  13505. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  13506. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  13507. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13508. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  13509. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  13510. (org-defkey org-mode-map "\C-c^" 'org-sort)
  13511. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  13512. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  13513. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  13514. (org-defkey org-mode-map "\C-m" 'org-return)
  13515. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  13516. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  13517. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  13518. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  13519. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  13520. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  13521. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  13522. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  13523. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  13524. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  13525. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  13526. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  13527. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  13528. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  13529. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  13530. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  13531. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  13532. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  13533. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  13534. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  13535. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  13536. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  13537. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  13538. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  13539. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  13540. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  13541. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  13542. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  13543. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  13544. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  13545. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  13546. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  13547. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  13548. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  13549. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  13550. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  13551. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  13552. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  13553. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  13554. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  13555. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  13556. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  13557. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  13558. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  13559. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  13560. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  13561. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  13562. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  13563. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  13564. (when (featurep 'xemacs)
  13565. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  13566. (defconst org-speed-commands-default
  13567. '(
  13568. ("n" . outline-next-visible-heading)
  13569. ("p" . outline-previous-visible-heading)
  13570. ("f" . org-forward-same-level)
  13571. ("b" . org-backward-same-level)
  13572. ("u" . outline-up-heading)
  13573. ("c" . org-cycle)
  13574. ("C" . org-shifttab)
  13575. ("U" . org-shiftmetaup)
  13576. ("D" . org-shiftmetadown)
  13577. ("r" . org-metaright)
  13578. ("l" . org-metaleft)
  13579. ("R" . org-shiftmetaright)
  13580. ("L" . org-shiftmetaleft)
  13581. ("i" . (progn (forward-char 1) (call-interactively
  13582. 'org-insert-heading-respect-content)))
  13583. ("a" . org-agenda)
  13584. ("/" . org-sparse-tree)
  13585. (";" . org-set-tags-command)
  13586. ("I" . org-clock-in)
  13587. ("O" . org-clock-out)
  13588. ("o" . org-open-at-point)
  13589. ("t" . org-todo)
  13590. ("j" . org-goto)
  13591. ("g" . (org-refile t))
  13592. ("e" . org-set-effort)
  13593. ("0" . (org-priority ?\ ))
  13594. ("1" . (org-priority ?A))
  13595. ("2" . (org-priority ?B))
  13596. ("3" . (org-priority ?C))
  13597. ("." . outline-mark-subtree)
  13598. ("^" . org-sort)
  13599. ("w" . org-refile)
  13600. ("a" . org-archive-subtree-default-with-confirmation)
  13601. ("/" . org-sparse-tree)
  13602. ("?" . org-speed-command-help)
  13603. )
  13604. "The default speed commands.")
  13605. (defun org-print-speed-command (e)
  13606. (princ (car e))
  13607. (princ " ")
  13608. (if (symbolp (cdr e))
  13609. (princ (symbol-name (cdr e)))
  13610. (prin1 (cdr e)))
  13611. (princ "\n"))
  13612. (defun org-speed-command-help ()
  13613. "Show the available speed commands."
  13614. (interactive)
  13615. (if (not org-use-speed-commands)
  13616. (error "Speed commands are not activated, customize `org-use-speed-commands'.")
  13617. (with-output-to-temp-buffer "*Help*"
  13618. (princ "Speed commands\n==============\n")
  13619. (mapc 'org-print-speed-command org-speed-commands-user)
  13620. (princ "\n")
  13621. (mapc 'org-print-speed-command org-speed-commands-default))))
  13622. (defvar org-self-insert-command-undo-counter 0)
  13623. (defvar org-table-auto-blank-field) ; defined in org-table.el
  13624. (defvar org-speed-command nil)
  13625. (defun org-self-insert-command (N)
  13626. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  13627. If the cursor is in a table looking at whitespace, the whitespace is
  13628. overwritten, and the table is not marked as requiring realignment."
  13629. (interactive "p")
  13630. (cond
  13631. ((and org-use-speed-commands
  13632. (bolp)
  13633. (looking-at outline-regexp)
  13634. (setq
  13635. org-speed-command
  13636. (or (cdr (assoc (this-command-keys) org-speed-commands-user))
  13637. (cdr (assoc (this-command-keys) org-speed-commands-default)))))
  13638. (cond
  13639. ((commandp org-speed-command)
  13640. (setq this-command org-speed-command)
  13641. (call-interactively org-speed-command))
  13642. ((functionp org-speed-command)
  13643. (funcall org-speed-command))
  13644. ((and org-speed-command (listp org-speed-command))
  13645. (eval org-speed-command))
  13646. (t (let (org-use-speed-commands)
  13647. (call-interactively 'org-self-insert-command)))))
  13648. ((and
  13649. (org-table-p)
  13650. (progn
  13651. ;; check if we blank the field, and if that triggers align
  13652. (and (featurep 'org-table) org-table-auto-blank-field
  13653. (member last-command
  13654. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  13655. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  13656. ;; got extra space, this field does not determine column width
  13657. (let (org-table-may-need-update) (org-table-blank-field))
  13658. ;; no extra space, this field may determine column width
  13659. (org-table-blank-field)))
  13660. t)
  13661. (eq N 1)
  13662. (looking-at "[^|\n]* |"))
  13663. (let (org-table-may-need-update)
  13664. (goto-char (1- (match-end 0)))
  13665. (delete-backward-char 1)
  13666. (goto-char (match-beginning 0))
  13667. (self-insert-command N)))
  13668. (t
  13669. (setq org-table-may-need-update t)
  13670. (self-insert-command N)
  13671. (org-fix-tags-on-the-fly)
  13672. (if org-self-insert-cluster-for-undo
  13673. (if (not (eq last-command 'org-self-insert-command))
  13674. (setq org-self-insert-command-undo-counter 1)
  13675. (if (>= org-self-insert-command-undo-counter 20)
  13676. (setq org-self-insert-command-undo-counter 1)
  13677. (and (> org-self-insert-command-undo-counter 0)
  13678. buffer-undo-list
  13679. (not (cadr buffer-undo-list)) ; remove nil entry
  13680. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  13681. (setq org-self-insert-command-undo-counter
  13682. (1+ org-self-insert-command-undo-counter))))))))
  13683. (defun org-fix-tags-on-the-fly ()
  13684. (when (and (equal (char-after (point-at-bol)) ?*)
  13685. (org-on-heading-p))
  13686. (org-align-tags-here org-tags-column)))
  13687. (defun org-delete-backward-char (N)
  13688. "Like `delete-backward-char', insert whitespace at field end in tables.
  13689. When deleting backwards, in tables this function will insert whitespace in
  13690. front of the next \"|\" separator, to keep the table aligned. The table will
  13691. still be marked for re-alignment if the field did fill the entire column,
  13692. because, in this case the deletion might narrow the column."
  13693. (interactive "p")
  13694. (if (and (org-table-p)
  13695. (eq N 1)
  13696. (string-match "|" (buffer-substring (point-at-bol) (point)))
  13697. (looking-at ".*?|"))
  13698. (let ((pos (point))
  13699. (noalign (looking-at "[^|\n\r]* |"))
  13700. (c org-table-may-need-update))
  13701. (backward-delete-char N)
  13702. (skip-chars-forward "^|")
  13703. (insert " ")
  13704. (goto-char (1- pos))
  13705. ;; noalign: if there were two spaces at the end, this field
  13706. ;; does not determine the width of the column.
  13707. (if noalign (setq org-table-may-need-update c)))
  13708. (backward-delete-char N)
  13709. (org-fix-tags-on-the-fly)))
  13710. (defun org-delete-char (N)
  13711. "Like `delete-char', but insert whitespace at field end in tables.
  13712. When deleting characters, in tables this function will insert whitespace in
  13713. front of the next \"|\" separator, to keep the table aligned. The table will
  13714. still be marked for re-alignment if the field did fill the entire column,
  13715. because, in this case the deletion might narrow the column."
  13716. (interactive "p")
  13717. (if (and (org-table-p)
  13718. (not (bolp))
  13719. (not (= (char-after) ?|))
  13720. (eq N 1))
  13721. (if (looking-at ".*?|")
  13722. (let ((pos (point))
  13723. (noalign (looking-at "[^|\n\r]* |"))
  13724. (c org-table-may-need-update))
  13725. (replace-match (concat
  13726. (substring (match-string 0) 1 -1)
  13727. " |"))
  13728. (goto-char pos)
  13729. ;; noalign: if there were two spaces at the end, this field
  13730. ;; does not determine the width of the column.
  13731. (if noalign (setq org-table-may-need-update c)))
  13732. (delete-char N))
  13733. (delete-char N)
  13734. (org-fix-tags-on-the-fly)))
  13735. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  13736. (put 'org-self-insert-command 'delete-selection t)
  13737. (put 'orgtbl-self-insert-command 'delete-selection t)
  13738. (put 'org-delete-char 'delete-selection 'supersede)
  13739. (put 'org-delete-backward-char 'delete-selection 'supersede)
  13740. (put 'org-yank 'delete-selection 'yank)
  13741. ;; Make `flyspell-mode' delay after some commands
  13742. (put 'org-self-insert-command 'flyspell-delayed t)
  13743. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  13744. (put 'org-delete-char 'flyspell-delayed t)
  13745. (put 'org-delete-backward-char 'flyspell-delayed t)
  13746. ;; Make pabbrev-mode expand after org-mode commands
  13747. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  13748. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  13749. ;; How to do this: Measure non-white length of current string
  13750. ;; If equal to column width, we should realign.
  13751. (defun org-remap (map &rest commands)
  13752. "In MAP, remap the functions given in COMMANDS.
  13753. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  13754. (let (new old)
  13755. (while commands
  13756. (setq old (pop commands) new (pop commands))
  13757. (if (fboundp 'command-remapping)
  13758. (org-defkey map (vector 'remap old) new)
  13759. (substitute-key-definition old new map global-map)))))
  13760. (when (eq org-enable-table-editor 'optimized)
  13761. ;; If the user wants maximum table support, we need to hijack
  13762. ;; some standard editing functions
  13763. (org-remap org-mode-map
  13764. 'self-insert-command 'org-self-insert-command
  13765. 'delete-char 'org-delete-char
  13766. 'delete-backward-char 'org-delete-backward-char)
  13767. (org-defkey org-mode-map "|" 'org-force-self-insert))
  13768. (defvar org-ctrl-c-ctrl-c-hook nil
  13769. "Hook for functions attaching themselves to `C-c C-c'.
  13770. This can be used to add additional functionality to the C-c C-c key which
  13771. executes context-dependent commands.
  13772. Each function will be called with no arguments. The function must check
  13773. if the context is appropriate for it to act. If yes, it should do its
  13774. thing and then return a non-nil value. If the context is wrong,
  13775. just do nothing and return nil.")
  13776. (defvar org-tab-first-hook nil
  13777. "Hook for functions to attach themselves to TAB.
  13778. See `org-ctrl-c-ctrl-c-hook' for more information.
  13779. This hook runs as the first action when TAB is pressed, even before
  13780. `org-cycle' messes around with the `outline-regexp' to cater for
  13781. inline tasks and plain list item folding.
  13782. If any function in this hook returns t, not other actions like table
  13783. field motion visibility cycling will be done.")
  13784. (defvar org-tab-after-check-for-table-hook nil
  13785. "Hook for functions to attach themselves to TAB.
  13786. See `org-ctrl-c-ctrl-c-hook' for more information.
  13787. This hook runs after it has been established that the cursor is not in a
  13788. table, but before checking if the cursor is in a headline or if global cycling
  13789. should be done.
  13790. If any function in this hook returns t, not other actions like visibility
  13791. cycling will be done.")
  13792. (defvar org-tab-after-check-for-cycling-hook nil
  13793. "Hook for functions to attach themselves to TAB.
  13794. See `org-ctrl-c-ctrl-c-hook' for more information.
  13795. This hook runs after it has been established that not table field motion and
  13796. not visibility should be done because of current context. This is probably
  13797. the place where a package like yasnippets can hook in.")
  13798. (defvar org-tab-before-tab-emulation-hook nil
  13799. "Hook for functions to attach themselves to TAB.
  13800. See `org-ctrl-c-ctrl-c-hook' for more information.
  13801. This hook runs after every other options for TAB have been exhausted, but
  13802. before indentation and \t insertion takes place.")
  13803. (defvar org-metaleft-hook nil
  13804. "Hook for functions attaching themselves to `M-left'.
  13805. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13806. (defvar org-metaright-hook nil
  13807. "Hook for functions attaching themselves to `M-right'.
  13808. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13809. (defvar org-metaup-hook nil
  13810. "Hook for functions attaching themselves to `M-up'.
  13811. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13812. (defvar org-metadown-hook nil
  13813. "Hook for functions attaching themselves to `M-down'.
  13814. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13815. (defvar org-shiftmetaleft-hook nil
  13816. "Hook for functions attaching themselves to `M-S-left'.
  13817. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13818. (defvar org-shiftmetaright-hook nil
  13819. "Hook for functions attaching themselves to `M-S-right'.
  13820. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13821. (defvar org-shiftmetaup-hook nil
  13822. "Hook for functions attaching themselves to `M-S-up'.
  13823. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13824. (defvar org-shiftmetadown-hook nil
  13825. "Hook for functions attaching themselves to `M-S-down'.
  13826. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13827. (defvar org-metareturn-hook nil
  13828. "Hook for functions attaching themselves to `M-RET'.
  13829. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13830. (defun org-modifier-cursor-error ()
  13831. "Throw an error, a modified cursor command was applied in wrong context."
  13832. (error "This command is active in special context like tables, headlines or items"))
  13833. (defun org-shiftselect-error ()
  13834. "Throw an error because Shift-Cursor command was applied in wrong context."
  13835. (if (and (boundp 'shift-select-mode) shift-select-mode)
  13836. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  13837. (error "This command works only in special context like headlines or timestamps")))
  13838. (defun org-call-for-shift-select (cmd)
  13839. (let ((this-command-keys-shift-translated t))
  13840. (call-interactively cmd)))
  13841. (defun org-shifttab (&optional arg)
  13842. "Global visibility cycling or move to previous table field.
  13843. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  13844. on context.
  13845. See the individual commands for more information."
  13846. (interactive "P")
  13847. (cond
  13848. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  13849. ((integerp arg)
  13850. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  13851. (message "Content view to level: %d" arg)
  13852. (org-content (prefix-numeric-value arg2))
  13853. (setq org-cycle-global-status 'overview)))
  13854. (t (call-interactively 'org-global-cycle))))
  13855. (defun org-shiftmetaleft ()
  13856. "Promote subtree or delete table column.
  13857. Calls `org-promote-subtree', `org-outdent-item',
  13858. or `org-table-delete-column', depending on context.
  13859. See the individual commands for more information."
  13860. (interactive)
  13861. (cond
  13862. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  13863. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  13864. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  13865. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13866. (t (org-modifier-cursor-error))))
  13867. (defun org-shiftmetaright ()
  13868. "Demote subtree or insert table column.
  13869. Calls `org-demote-subtree', `org-indent-item',
  13870. or `org-table-insert-column', depending on context.
  13871. See the individual commands for more information."
  13872. (interactive)
  13873. (cond
  13874. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  13875. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  13876. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  13877. ((org-at-item-p) (call-interactively 'org-indent-item))
  13878. (t (org-modifier-cursor-error))))
  13879. (defun org-shiftmetaup (&optional arg)
  13880. "Move subtree up or kill table row.
  13881. Calls `org-move-subtree-up' or `org-table-kill-row' or
  13882. `org-move-item-up' depending on context. See the individual commands
  13883. for more information."
  13884. (interactive "P")
  13885. (cond
  13886. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  13887. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13888. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13889. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13890. (t (org-modifier-cursor-error))))
  13891. (defun org-shiftmetadown (&optional arg)
  13892. "Move subtree down or insert table row.
  13893. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13894. `org-move-item-down', depending on context. See the individual
  13895. commands for more information."
  13896. (interactive "P")
  13897. (cond
  13898. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  13899. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13900. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13901. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13902. (t (org-modifier-cursor-error))))
  13903. (defun org-metaleft (&optional arg)
  13904. "Promote heading or move table column to left.
  13905. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13906. With no specific context, calls the Emacs default `backward-word'.
  13907. See the individual commands for more information."
  13908. (interactive "P")
  13909. (cond
  13910. ((run-hook-with-args-until-success 'org-metaleft-hook))
  13911. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13912. ((or (org-on-heading-p)
  13913. (and (org-region-active-p)
  13914. (save-excursion
  13915. (goto-char (region-beginning))
  13916. (org-on-heading-p))))
  13917. (call-interactively 'org-do-promote))
  13918. ((or (org-at-item-p)
  13919. (and (org-region-active-p)
  13920. (save-excursion
  13921. (goto-char (region-beginning))
  13922. (org-at-item-p))))
  13923. (call-interactively 'org-outdent-item))
  13924. (t (call-interactively 'backward-word))))
  13925. (defun org-metaright (&optional arg)
  13926. "Demote subtree or move table column to right.
  13927. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13928. With no specific context, calls the Emacs default `forward-word'.
  13929. See the individual commands for more information."
  13930. (interactive "P")
  13931. (cond
  13932. ((run-hook-with-args-until-success 'org-metaright-hook))
  13933. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13934. ((or (org-on-heading-p)
  13935. (and (org-region-active-p)
  13936. (save-excursion
  13937. (goto-char (region-beginning))
  13938. (org-on-heading-p))))
  13939. (call-interactively 'org-do-demote))
  13940. ((or (org-at-item-p)
  13941. (and (org-region-active-p)
  13942. (save-excursion
  13943. (goto-char (region-beginning))
  13944. (org-at-item-p))))
  13945. (call-interactively 'org-indent-item))
  13946. (t (call-interactively 'forward-word))))
  13947. (defun org-metaup (&optional arg)
  13948. "Move subtree up or move table row up.
  13949. Calls `org-move-subtree-up' or `org-table-move-row' or
  13950. `org-move-item-up', depending on context. See the individual commands
  13951. for more information."
  13952. (interactive "P")
  13953. (cond
  13954. ((run-hook-with-args-until-success 'org-metaup-hook))
  13955. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13956. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13957. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13958. (t (transpose-lines 1) (beginning-of-line -1))))
  13959. (defun org-metadown (&optional arg)
  13960. "Move subtree down or move table row down.
  13961. Calls `org-move-subtree-down' or `org-table-move-row' or
  13962. `org-move-item-down', depending on context. See the individual
  13963. commands for more information."
  13964. (interactive "P")
  13965. (cond
  13966. ((run-hook-with-args-until-success 'org-metadown-hook))
  13967. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13968. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13969. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13970. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13971. (defun org-shiftup (&optional arg)
  13972. "Increase item in timestamp or increase priority of current headline.
  13973. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13974. depending on context. See the individual commands for more information."
  13975. (interactive "P")
  13976. (cond
  13977. ((and org-support-shift-select (org-region-active-p))
  13978. (org-call-for-shift-select 'previous-line))
  13979. ((org-at-timestamp-p t)
  13980. (call-interactively (if org-edit-timestamp-down-means-later
  13981. 'org-timestamp-down 'org-timestamp-up)))
  13982. ((and (not (eq org-support-shift-select 'always))
  13983. org-enable-priority-commands
  13984. (org-on-heading-p))
  13985. (call-interactively 'org-priority-up))
  13986. ((and (not org-support-shift-select) (org-at-item-p))
  13987. (call-interactively 'org-previous-item))
  13988. ((org-clocktable-try-shift 'up arg))
  13989. (org-support-shift-select
  13990. (org-call-for-shift-select 'previous-line))
  13991. (t (org-shiftselect-error))))
  13992. (defun org-shiftdown (&optional arg)
  13993. "Decrease item in timestamp or decrease priority of current headline.
  13994. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13995. depending on context. See the individual commands for more information."
  13996. (interactive "P")
  13997. (cond
  13998. ((and org-support-shift-select (org-region-active-p))
  13999. (org-call-for-shift-select 'next-line))
  14000. ((org-at-timestamp-p t)
  14001. (call-interactively (if org-edit-timestamp-down-means-later
  14002. 'org-timestamp-up 'org-timestamp-down)))
  14003. ((and (not (eq org-support-shift-select 'always))
  14004. org-enable-priority-commands
  14005. (org-on-heading-p))
  14006. (call-interactively 'org-priority-down))
  14007. ((and (not org-support-shift-select) (org-at-item-p))
  14008. (call-interactively 'org-next-item))
  14009. ((org-clocktable-try-shift 'down arg))
  14010. (org-support-shift-select
  14011. (org-call-for-shift-select 'next-line))
  14012. (t (org-shiftselect-error))))
  14013. (defun org-shiftright (&optional arg)
  14014. "Cycle the thing at point or in the current line, depending on context.
  14015. Depending on context, this does one of the following:
  14016. - switch a timestamp at point one day into the future
  14017. - on a headline, switch to the next TODO keyword.
  14018. - on an item, switch entire list to the next bullet type
  14019. - on a property line, switch to the next allowed value
  14020. - on a clocktable definition line, move time block into the future"
  14021. (interactive "P")
  14022. (cond
  14023. ((and org-support-shift-select (org-region-active-p))
  14024. (org-call-for-shift-select 'forward-char))
  14025. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  14026. ((and (not (eq org-support-shift-select 'always))
  14027. (org-on-heading-p))
  14028. (let ((org-inhibit-logging
  14029. (not org-treat-S-cursor-todo-selection-as-state-change))
  14030. (org-inhibit-blocking
  14031. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14032. (org-call-with-arg 'org-todo 'right)))
  14033. ((or (and org-support-shift-select
  14034. (not (eq org-support-shift-select 'always))
  14035. (org-at-item-bullet-p))
  14036. (and (not org-support-shift-select) (org-at-item-p)))
  14037. (org-call-with-arg 'org-cycle-list-bullet nil))
  14038. ((and (not (eq org-support-shift-select 'always))
  14039. (org-at-property-p))
  14040. (call-interactively 'org-property-next-allowed-value))
  14041. ((org-clocktable-try-shift 'right arg))
  14042. (org-support-shift-select
  14043. (org-call-for-shift-select 'forward-char))
  14044. (t (org-shiftselect-error))))
  14045. (defun org-shiftleft (&optional arg)
  14046. "Cycle the thing at point or in the current line, depending on context.
  14047. Depending on context, this does one of the following:
  14048. - switch a timestamp at point one day into the past
  14049. - on a headline, switch to the previous TODO keyword.
  14050. - on an item, switch entire list to the previous bullet type
  14051. - on a property line, switch to the previous allowed value
  14052. - on a clocktable definition line, move time block into the past"
  14053. (interactive "P")
  14054. (cond
  14055. ((and org-support-shift-select (org-region-active-p))
  14056. (org-call-for-shift-select 'backward-char))
  14057. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  14058. ((and (not (eq org-support-shift-select 'always))
  14059. (org-on-heading-p))
  14060. (let ((org-inhibit-logging
  14061. (not org-treat-S-cursor-todo-selection-as-state-change))
  14062. (org-inhibit-blocking
  14063. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14064. (org-call-with-arg 'org-todo 'left)))
  14065. ((or (and org-support-shift-select
  14066. (not (eq org-support-shift-select 'always))
  14067. (org-at-item-bullet-p))
  14068. (and (not org-support-shift-select) (org-at-item-p)))
  14069. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  14070. ((and (not (eq org-support-shift-select 'always))
  14071. (org-at-property-p))
  14072. (call-interactively 'org-property-previous-allowed-value))
  14073. ((org-clocktable-try-shift 'left arg))
  14074. (org-support-shift-select
  14075. (org-call-for-shift-select 'backward-char))
  14076. (t (org-shiftselect-error))))
  14077. (defun org-shiftcontrolright ()
  14078. "Switch to next TODO set."
  14079. (interactive)
  14080. (cond
  14081. ((and org-support-shift-select (org-region-active-p))
  14082. (org-call-for-shift-select 'forward-word))
  14083. ((and (not (eq org-support-shift-select 'always))
  14084. (org-on-heading-p))
  14085. (org-call-with-arg 'org-todo 'nextset))
  14086. (org-support-shift-select
  14087. (org-call-for-shift-select 'forward-word))
  14088. (t (org-shiftselect-error))))
  14089. (defun org-shiftcontrolleft ()
  14090. "Switch to previous TODO set."
  14091. (interactive)
  14092. (cond
  14093. ((and org-support-shift-select (org-region-active-p))
  14094. (org-call-for-shift-select 'backward-word))
  14095. ((and (not (eq org-support-shift-select 'always))
  14096. (org-on-heading-p))
  14097. (org-call-with-arg 'org-todo 'previousset))
  14098. (org-support-shift-select
  14099. (org-call-for-shift-select 'backward-word))
  14100. (t (org-shiftselect-error))))
  14101. (defun org-ctrl-c-ret ()
  14102. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  14103. (interactive)
  14104. (cond
  14105. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  14106. (t (call-interactively 'org-insert-heading))))
  14107. (defun org-copy-special ()
  14108. "Copy region in table or copy current subtree.
  14109. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  14110. See the individual commands for more information."
  14111. (interactive)
  14112. (call-interactively
  14113. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  14114. (defun org-cut-special ()
  14115. "Cut region in table or cut current subtree.
  14116. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  14117. See the individual commands for more information."
  14118. (interactive)
  14119. (call-interactively
  14120. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  14121. (defun org-paste-special (arg)
  14122. "Paste rectangular region into table, or past subtree relative to level.
  14123. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  14124. See the individual commands for more information."
  14125. (interactive "P")
  14126. (if (org-at-table-p)
  14127. (org-table-paste-rectangle)
  14128. (org-paste-subtree arg)))
  14129. (defun org-edit-special ()
  14130. "Call a special editor for the stuff at point.
  14131. When at a table, call the formula editor with `org-table-edit-formulas'.
  14132. When at the first line of an src example, call `org-edit-src-code'.
  14133. When in an #+include line, visit the include file. Otherwise call
  14134. `ffap' to visit the file at point."
  14135. (interactive)
  14136. (cond
  14137. ((org-at-table-p)
  14138. (call-interactively 'org-table-edit-formulas))
  14139. ((save-excursion
  14140. (beginning-of-line 1)
  14141. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  14142. (find-file (org-trim (match-string 1))))
  14143. ((org-edit-src-code))
  14144. ((org-edit-fixed-width-region))
  14145. (t (call-interactively 'ffap))))
  14146. (defun org-ctrl-c-ctrl-c (&optional arg)
  14147. "Set tags in headline, or update according to changed information at point.
  14148. This command does many different things, depending on context:
  14149. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  14150. this is what we do.
  14151. - If the cursor is on a statistics cookie, update it.
  14152. - If the cursor is in a headline, prompt for tags and insert them
  14153. into the current line, aligned to `org-tags-column'. When called
  14154. with prefix arg, realign all tags in the current buffer.
  14155. - If the cursor is in one of the special #+KEYWORD lines, this
  14156. triggers scanning the buffer for these lines and updating the
  14157. information.
  14158. - If the cursor is inside a table, realign the table. This command
  14159. works even if the automatic table editor has been turned off.
  14160. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  14161. the entire table.
  14162. - If the cursor is at a footnote reference or definition, jump to
  14163. the corresponding definition or references, respectively.
  14164. - If the cursor is a the beginning of a dynamic block, update it.
  14165. - If the cursor is inside a table created by the table.el package,
  14166. activate that table.
  14167. - If the current buffer is a remember buffer, close note and file
  14168. it. A prefix argument of 1 files to the default location
  14169. without further interaction. A prefix argument of 2 files to
  14170. the currently clocking task.
  14171. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  14172. links in this buffer.
  14173. - If the cursor is on a numbered item in a plain list, renumber the
  14174. ordered list.
  14175. - If the cursor is on a checkbox, toggle it."
  14176. (interactive "P")
  14177. (let ((org-enable-table-editor t))
  14178. (cond
  14179. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  14180. org-occur-highlights
  14181. org-latex-fragment-image-overlays)
  14182. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  14183. (org-remove-occur-highlights)
  14184. (org-remove-latex-fragment-image-overlays)
  14185. (message "Temporary highlights/overlays removed from current buffer"))
  14186. ((and (local-variable-p 'org-finish-function (current-buffer))
  14187. (fboundp org-finish-function))
  14188. (funcall org-finish-function))
  14189. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  14190. ((org-at-property-p)
  14191. (call-interactively 'org-property-action))
  14192. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  14193. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  14194. (or (org-on-heading-p) (org-at-item-p)))
  14195. (call-interactively 'org-update-statistics-cookies))
  14196. ((org-on-heading-p) (call-interactively 'org-set-tags))
  14197. ((org-at-table.el-p)
  14198. (require 'table)
  14199. (beginning-of-line 1)
  14200. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  14201. (call-interactively 'table-recognize-table))
  14202. ((org-at-table-p)
  14203. (org-table-maybe-eval-formula)
  14204. (if arg
  14205. (call-interactively 'org-table-recalculate)
  14206. (org-table-maybe-recalculate-line))
  14207. (call-interactively 'org-table-align))
  14208. ((or (org-footnote-at-reference-p)
  14209. (org-footnote-at-definition-p))
  14210. (call-interactively 'org-footnote-action))
  14211. ((org-at-item-checkbox-p)
  14212. (call-interactively 'org-toggle-checkbox))
  14213. ((org-at-item-p)
  14214. (if arg
  14215. (call-interactively 'org-toggle-checkbox)
  14216. (call-interactively 'org-maybe-renumber-ordered-list)))
  14217. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  14218. ;; Dynamic block
  14219. (beginning-of-line 1)
  14220. (save-excursion (org-update-dblock)))
  14221. ((save-excursion
  14222. (beginning-of-line 1)
  14223. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  14224. (cond
  14225. ((equal (match-string 1) "TBLFM")
  14226. ;; Recalculate the table before this line
  14227. (save-excursion
  14228. (beginning-of-line 1)
  14229. (skip-chars-backward " \r\n\t")
  14230. (if (org-at-table-p)
  14231. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  14232. (t
  14233. ; (org-set-regexps-and-options)
  14234. ; (org-restart-font-lock)
  14235. (let ((org-inhibit-startup t)) (org-mode-restart))
  14236. (message "Local setup has been refreshed"))))
  14237. ((org-clock-update-time-maybe))
  14238. (t (error "C-c C-c can do nothing useful at this location")))))
  14239. (defun org-mode-restart ()
  14240. "Restart Org-mode, to scan again for special lines.
  14241. Also updates the keyword regular expressions."
  14242. (interactive)
  14243. (org-mode)
  14244. (message "Org-mode restarted"))
  14245. (defun org-kill-note-or-show-branches ()
  14246. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  14247. (interactive)
  14248. (if (not org-finish-function)
  14249. (call-interactively 'show-branches)
  14250. (let ((org-note-abort t))
  14251. (funcall org-finish-function))))
  14252. (defun org-return (&optional indent)
  14253. "Goto next table row or insert a newline.
  14254. Calls `org-table-next-row' or `newline', depending on context.
  14255. See the individual commands for more information."
  14256. (interactive)
  14257. (cond
  14258. ((bobp) (if indent (newline-and-indent) (newline)))
  14259. ((org-at-table-p)
  14260. (org-table-justify-field-maybe)
  14261. (call-interactively 'org-table-next-row))
  14262. ((and org-return-follows-link
  14263. (eq (get-text-property (point) 'face) 'org-link))
  14264. (call-interactively 'org-open-at-point))
  14265. ((and (org-at-heading-p)
  14266. (looking-at
  14267. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  14268. (org-show-entry)
  14269. (end-of-line 1)
  14270. (newline))
  14271. (t (if indent (newline-and-indent) (newline)))))
  14272. (defun org-return-indent ()
  14273. "Goto next table row or insert a newline and indent.
  14274. Calls `org-table-next-row' or `newline-and-indent', depending on
  14275. context. See the individual commands for more information."
  14276. (interactive)
  14277. (org-return t))
  14278. (defun org-ctrl-c-star ()
  14279. "Compute table, or change heading status of lines.
  14280. Calls `org-table-recalculate' or `org-toggle-heading',
  14281. depending on context."
  14282. (interactive)
  14283. (cond
  14284. ((org-at-table-p)
  14285. (call-interactively 'org-table-recalculate))
  14286. (t
  14287. ;; Convert all lines in region to list items
  14288. (call-interactively 'org-toggle-heading))))
  14289. (defun org-ctrl-c-minus ()
  14290. "Insert separator line in table or modify bullet status of line.
  14291. Also turns a plain line or a region of lines into list items.
  14292. Calls `org-table-insert-hline', `org-toggle-item', or
  14293. `org-cycle-list-bullet', depending on context."
  14294. (interactive)
  14295. (cond
  14296. ((org-at-table-p)
  14297. (call-interactively 'org-table-insert-hline))
  14298. ((org-region-active-p)
  14299. (call-interactively 'org-toggle-item))
  14300. ((org-in-item-p)
  14301. (call-interactively 'org-cycle-list-bullet))
  14302. (t
  14303. (call-interactively 'org-toggle-item))))
  14304. (defun org-toggle-item ()
  14305. "Convert headings or normal lines to items, items to normal lines.
  14306. If there is no active region, only the current line is considered.
  14307. If the first line in the region is a headline, convert all headlines to items.
  14308. If the first line in the region is an item, convert all items to normal lines.
  14309. If the first line is normal text, add an item bullet to each line."
  14310. (interactive)
  14311. (let (l2 l beg end)
  14312. (if (org-region-active-p)
  14313. (setq beg (region-beginning) end (region-end))
  14314. (setq beg (point-at-bol)
  14315. end (min (1+ (point-at-eol)) (point-max))))
  14316. (save-excursion
  14317. (goto-char end)
  14318. (setq l2 (org-current-line))
  14319. (goto-char beg)
  14320. (beginning-of-line 1)
  14321. (setq l (1- (org-current-line)))
  14322. (if (org-at-item-p)
  14323. ;; We already have items, de-itemize
  14324. (while (< (setq l (1+ l)) l2)
  14325. (when (org-at-item-p)
  14326. (goto-char (match-beginning 2))
  14327. (delete-region (match-beginning 2) (match-end 2))
  14328. (and (looking-at "[ \t]+") (replace-match "")))
  14329. (beginning-of-line 2))
  14330. (if (org-on-heading-p)
  14331. ;; Headings, convert to items
  14332. (while (< (setq l (1+ l)) l2)
  14333. (if (looking-at org-outline-regexp)
  14334. (replace-match "- " t t))
  14335. (beginning-of-line 2))
  14336. ;; normal lines, turn them into items
  14337. (while (< (setq l (1+ l)) l2)
  14338. (unless (org-at-item-p)
  14339. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  14340. (replace-match "\\1- \\2")))
  14341. (beginning-of-line 2)))))))
  14342. (defun org-toggle-heading (&optional nstars)
  14343. "Convert headings to normal text, or items or text to headings.
  14344. If there is no active region, only the current line is considered.
  14345. If the first line is a heading, remove the stars from all headlines
  14346. in the region.
  14347. If the first line is a plain list item, turn all plain list items
  14348. into headings.
  14349. If the first line is a normal line, turn each and every line in the
  14350. region into a heading.
  14351. When converting a line into a heading, the number of stars is chosen
  14352. such that the lines become children of the current entry. However,
  14353. when a prefix argument is given, its value determines the number of
  14354. stars to add."
  14355. (interactive "P")
  14356. (let (l2 l itemp beg end)
  14357. (if (org-region-active-p)
  14358. (setq beg (region-beginning) end (region-end))
  14359. (setq beg (point-at-bol)
  14360. end (min (1+ (point-at-eol)) (point-max))))
  14361. (save-excursion
  14362. (goto-char end)
  14363. (setq l2 (org-current-line))
  14364. (goto-char beg)
  14365. (beginning-of-line 1)
  14366. (setq l (1- (org-current-line)))
  14367. (if (org-on-heading-p)
  14368. ;; We already have headlines, de-star them
  14369. (while (< (setq l (1+ l)) l2)
  14370. (when (org-on-heading-p t)
  14371. (and (looking-at outline-regexp) (replace-match "")))
  14372. (beginning-of-line 2))
  14373. (setq itemp (org-at-item-p))
  14374. (let* ((stars
  14375. (if nstars
  14376. (make-string (prefix-numeric-value current-prefix-arg)
  14377. ?*)
  14378. (save-excursion
  14379. (if (re-search-backward org-complex-heading-regexp nil t)
  14380. (match-string 1) ""))))
  14381. (add-stars (cond (nstars "")
  14382. ((equal stars "") "*")
  14383. (org-odd-levels-only "**")
  14384. (t "*")))
  14385. (rpl (concat stars add-stars " ")))
  14386. (while (< (setq l (1+ l)) l2)
  14387. (if itemp
  14388. (and (org-at-item-p) (replace-match rpl t t))
  14389. (unless (org-on-heading-p)
  14390. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  14391. (replace-match (concat rpl (match-string 2))))))
  14392. (beginning-of-line 2)))))))
  14393. (defun org-meta-return (&optional arg)
  14394. "Insert a new heading or wrap a region in a table.
  14395. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  14396. See the individual commands for more information."
  14397. (interactive "P")
  14398. (cond
  14399. ((run-hook-with-args-until-success 'org-metareturn-hook))
  14400. ((org-at-table-p)
  14401. (call-interactively 'org-table-wrap-region))
  14402. (t (call-interactively 'org-insert-heading))))
  14403. ;;; Menu entries
  14404. ;; Define the Org-mode menus
  14405. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  14406. '("Tbl"
  14407. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  14408. ["Next Field" org-cycle (org-at-table-p)]
  14409. ["Previous Field" org-shifttab (org-at-table-p)]
  14410. ["Next Row" org-return (org-at-table-p)]
  14411. "--"
  14412. ["Blank Field" org-table-blank-field (org-at-table-p)]
  14413. ["Edit Field" org-table-edit-field (org-at-table-p)]
  14414. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  14415. "--"
  14416. ("Column"
  14417. ["Move Column Left" org-metaleft (org-at-table-p)]
  14418. ["Move Column Right" org-metaright (org-at-table-p)]
  14419. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  14420. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  14421. ("Row"
  14422. ["Move Row Up" org-metaup (org-at-table-p)]
  14423. ["Move Row Down" org-metadown (org-at-table-p)]
  14424. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  14425. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  14426. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  14427. "--"
  14428. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  14429. ("Rectangle"
  14430. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  14431. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  14432. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  14433. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  14434. "--"
  14435. ("Calculate"
  14436. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  14437. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  14438. ["Edit Formulas" org-edit-special (org-at-table-p)]
  14439. "--"
  14440. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  14441. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  14442. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  14443. "--"
  14444. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  14445. "--"
  14446. ["Sum Column/Rectangle" org-table-sum
  14447. (or (org-at-table-p) (org-region-active-p))]
  14448. ["Which Column?" org-table-current-column (org-at-table-p)])
  14449. ["Debug Formulas"
  14450. org-table-toggle-formula-debugger
  14451. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  14452. ["Show Col/Row Numbers"
  14453. org-table-toggle-coordinate-overlays
  14454. :style toggle
  14455. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  14456. "--"
  14457. ["Create" org-table-create (and (not (org-at-table-p))
  14458. org-enable-table-editor)]
  14459. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  14460. ["Import from File" org-table-import (not (org-at-table-p))]
  14461. ["Export to File" org-table-export (org-at-table-p)]
  14462. "--"
  14463. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  14464. (easy-menu-define org-org-menu org-mode-map "Org menu"
  14465. '("Org"
  14466. ("Show/Hide"
  14467. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  14468. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  14469. ["Sparse Tree..." org-sparse-tree t]
  14470. ["Reveal Context" org-reveal t]
  14471. ["Show All" show-all t]
  14472. "--"
  14473. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  14474. "--"
  14475. ["New Heading" org-insert-heading t]
  14476. ("Navigate Headings"
  14477. ["Up" outline-up-heading t]
  14478. ["Next" outline-next-visible-heading t]
  14479. ["Previous" outline-previous-visible-heading t]
  14480. ["Next Same Level" outline-forward-same-level t]
  14481. ["Previous Same Level" outline-backward-same-level t]
  14482. "--"
  14483. ["Jump" org-goto t])
  14484. ("Edit Structure"
  14485. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  14486. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  14487. "--"
  14488. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  14489. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  14490. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  14491. "--"
  14492. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  14493. "--"
  14494. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  14495. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  14496. ["Demote Heading" org-metaright (not (org-at-table-p))]
  14497. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  14498. "--"
  14499. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  14500. "--"
  14501. ["Convert to odd levels" org-convert-to-odd-levels t]
  14502. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  14503. ("Editing"
  14504. ["Emphasis..." org-emphasize t]
  14505. ["Edit Source Example" org-edit-special t]
  14506. "--"
  14507. ["Footnote new/jump" org-footnote-action t]
  14508. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  14509. ("Archive"
  14510. ["Archive (default method)" org-archive-subtree-default t]
  14511. "--"
  14512. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  14513. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  14514. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  14515. )
  14516. "--"
  14517. ("Hyperlinks"
  14518. ["Store Link (Global)" org-store-link t]
  14519. ["Find existing link to here" org-occur-link-in-agenda-files t]
  14520. ["Insert Link" org-insert-link t]
  14521. ["Follow Link" org-open-at-point t]
  14522. "--"
  14523. ["Next link" org-next-link t]
  14524. ["Previous link" org-previous-link t]
  14525. "--"
  14526. ["Descriptive Links"
  14527. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  14528. :style radio
  14529. :selected (member '(org-link) buffer-invisibility-spec)]
  14530. ["Literal Links"
  14531. (progn
  14532. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  14533. :style radio
  14534. :selected (not (member '(org-link) buffer-invisibility-spec))])
  14535. "--"
  14536. ("TODO Lists"
  14537. ["TODO/DONE/-" org-todo t]
  14538. ("Select keyword"
  14539. ["Next keyword" org-shiftright (org-on-heading-p)]
  14540. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  14541. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  14542. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  14543. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  14544. ["Show TODO Tree" org-show-todo-tree t]
  14545. ["Global TODO list" org-todo-list t]
  14546. "--"
  14547. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  14548. :selected org-enforce-todo-dependencies :style toggle :active t]
  14549. "Settings for tree at point"
  14550. ["Do Children sequentially" org-toggle-ordered-property :style radio
  14551. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  14552. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14553. ["Do Children parallel" org-toggle-ordered-property :style radio
  14554. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  14555. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14556. "--"
  14557. ["Set Priority" org-priority t]
  14558. ["Priority Up" org-shiftup t]
  14559. ["Priority Down" org-shiftdown t]
  14560. "--"
  14561. ["Get news from all feeds" org-feed-update-all t]
  14562. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  14563. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  14564. ("TAGS and Properties"
  14565. ["Set Tags" org-set-tags-command t]
  14566. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  14567. "--"
  14568. ["Set property" org-set-property t]
  14569. ["Column view of properties" org-columns t]
  14570. ["Insert Column View DBlock" org-insert-columns-dblock t])
  14571. ("Dates and Scheduling"
  14572. ["Timestamp" org-time-stamp t]
  14573. ["Timestamp (inactive)" org-time-stamp-inactive t]
  14574. ("Change Date"
  14575. ["1 Day Later" org-shiftright t]
  14576. ["1 Day Earlier" org-shiftleft t]
  14577. ["1 ... Later" org-shiftup t]
  14578. ["1 ... Earlier" org-shiftdown t])
  14579. ["Compute Time Range" org-evaluate-time-range t]
  14580. ["Schedule Item" org-schedule t]
  14581. ["Deadline" org-deadline t]
  14582. "--"
  14583. ["Custom time format" org-toggle-time-stamp-overlays
  14584. :style radio :selected org-display-custom-times]
  14585. "--"
  14586. ["Goto Calendar" org-goto-calendar t]
  14587. ["Date from Calendar" org-date-from-calendar t]
  14588. "--"
  14589. ["Start/Restart Timer" org-timer-start t]
  14590. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  14591. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  14592. ["Insert Timer String" org-timer t]
  14593. ["Insert Timer Item" org-timer-item t])
  14594. ("Logging work"
  14595. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  14596. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  14597. ["Clock out" org-clock-out t]
  14598. ["Clock cancel" org-clock-cancel t]
  14599. "--"
  14600. ["Mark as default task" org-clock-mark-default-task t]
  14601. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  14602. ["Goto running clock" org-clock-goto t]
  14603. "--"
  14604. ["Display times" org-clock-display t]
  14605. ["Create clock table" org-clock-report t]
  14606. "--"
  14607. ["Record DONE time"
  14608. (progn (setq org-log-done (not org-log-done))
  14609. (message "Switching to %s will %s record a timestamp"
  14610. (car org-done-keywords)
  14611. (if org-log-done "automatically" "not")))
  14612. :style toggle :selected org-log-done])
  14613. "--"
  14614. ["Agenda Command..." org-agenda t]
  14615. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  14616. ("File List for Agenda")
  14617. ("Special views current file"
  14618. ["TODO Tree" org-show-todo-tree t]
  14619. ["Check Deadlines" org-check-deadlines t]
  14620. ["Timeline" org-timeline t]
  14621. ["Tags/Property tree" org-match-sparse-tree t])
  14622. "--"
  14623. ["Export/Publish..." org-export t]
  14624. ("LaTeX"
  14625. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  14626. :selected org-cdlatex-mode]
  14627. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  14628. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  14629. ["Modify math symbol" org-cdlatex-math-modify
  14630. (org-inside-LaTeX-fragment-p)]
  14631. ["Insert citation" org-reftex-citation t]
  14632. "--"
  14633. ["Export LaTeX fragments as images"
  14634. (if (featurep 'org-exp)
  14635. (setq org-export-with-LaTeX-fragments
  14636. (not org-export-with-LaTeX-fragments))
  14637. (require 'org-exp))
  14638. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  14639. org-export-with-LaTeX-fragments)])
  14640. "--"
  14641. ("MobileOrg"
  14642. ["Push Files and Views" org-mobile-push t]
  14643. ["Get Captured and Flagged" org-mobile-pull t]
  14644. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  14645. "--"
  14646. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  14647. "--"
  14648. ("Documentation"
  14649. ["Show Version" org-version t]
  14650. ["Info Documentation" org-info t])
  14651. ("Customize"
  14652. ["Browse Org Group" org-customize t]
  14653. "--"
  14654. ["Expand This Menu" org-create-customize-menu
  14655. (fboundp 'customize-menu-create)])
  14656. ["Send bug report" org-submit-bug-report t]
  14657. "--"
  14658. ("Refresh/Reload"
  14659. ["Refresh setup current buffer" org-mode-restart t]
  14660. ["Reload Org (after update)" org-reload t]
  14661. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  14662. ))
  14663. (defun org-info (&optional node)
  14664. "Read documentation for Org-mode in the info system.
  14665. With optional NODE, go directly to that node."
  14666. (interactive)
  14667. (info (format "(org)%s" (or node ""))))
  14668. ;;;###autoload
  14669. (defun org-submit-bug-report ()
  14670. "Submit a bug report on Org-mode via mail.
  14671. Don't hesitate to report any problems or inaccurate documentation.
  14672. If you don't have setup sending mail from (X)Emacs, please copy the
  14673. output buffer into your mail program, as it gives us important
  14674. information about your Org-mode version and configuration."
  14675. (interactive)
  14676. (require 'reporter)
  14677. (org-load-modules-maybe)
  14678. (org-require-autoloaded-modules)
  14679. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  14680. (reporter-submit-bug-report
  14681. "emacs-orgmode@gnu.org"
  14682. (org-version)
  14683. (let (list)
  14684. (save-window-excursion
  14685. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  14686. (delete-other-windows)
  14687. (erase-buffer)
  14688. (insert "You are about to submit a bug report to the Org-mode mailing list.
  14689. We would like to add your full Org-mode and Outline configuration to the
  14690. bug report. This greatly simplifies the work of the maintainer and
  14691. other experts on the mailing list.
  14692. HOWEVER, some variables you have customized may contain private
  14693. information. The names of customers, colleagues, or friends, might
  14694. appear in the form of file names, tags, todo states, or search strings.
  14695. If you answer yes to the prompt, you might want to check and remove
  14696. such private information before sending the email.")
  14697. (add-text-properties (point-min) (point-max) '(face org-warning))
  14698. (when (yes-or-no-p "Include your Org-mode configuration ")
  14699. (mapatoms
  14700. (lambda (v)
  14701. (and (boundp v)
  14702. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  14703. (or (and (symbol-value v)
  14704. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  14705. (and
  14706. (get v 'custom-type) (get v 'standard-value)
  14707. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  14708. (push v list)))))
  14709. (kill-buffer (get-buffer "*Warn about privacy*"))
  14710. list))
  14711. nil nil
  14712. "Remember to cover the basics, that is, what you expected to happen and
  14713. what in fact did happen. You don't know how to make a good report? See
  14714. http://orgmode.org/manual/Feedback.html#Feedback
  14715. Your bug report will be posted to the Org-mode mailing list.
  14716. ------------------------------------------------------------------------")))
  14717. (defun org-install-agenda-files-menu ()
  14718. (let ((bl (buffer-list)))
  14719. (save-excursion
  14720. (while bl
  14721. (set-buffer (pop bl))
  14722. (if (org-mode-p) (setq bl nil)))
  14723. (when (org-mode-p)
  14724. (easy-menu-change
  14725. '("Org") "File List for Agenda"
  14726. (append
  14727. (list
  14728. ["Edit File List" (org-edit-agenda-file-list) t]
  14729. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  14730. ["Remove Current File from List" org-remove-file t]
  14731. ["Cycle through agenda files" org-cycle-agenda-files t]
  14732. ["Occur in all agenda files" org-occur-in-agenda-files t]
  14733. "--")
  14734. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  14735. ;;;; Documentation
  14736. ;;;###autoload
  14737. (defun org-require-autoloaded-modules ()
  14738. (interactive)
  14739. (mapc 'require
  14740. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  14741. org-docbook org-exp org-html org-icalendar
  14742. org-id org-latex
  14743. org-publish org-remember org-table
  14744. org-timer org-xoxo)))
  14745. ;;;###autoload
  14746. (defun org-reload (&optional uncompiled)
  14747. "Reload all org lisp files.
  14748. With prefix arg UNCOMPILED, load the uncompiled versions."
  14749. (interactive "P")
  14750. (require 'find-func)
  14751. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  14752. (dir-org (file-name-directory (org-find-library-name "org")))
  14753. (dir-org-contrib (ignore-errors
  14754. (file-name-directory
  14755. (org-find-library-name "org-contribdir"))))
  14756. (files
  14757. (append (directory-files dir-org t file-re)
  14758. (and dir-org-contrib
  14759. (directory-files dir-org-contrib t file-re))))
  14760. (remove-re (concat (if (featurep 'xemacs)
  14761. "org-colview" "org-colview-xemacs")
  14762. "\\'")))
  14763. (setq files (mapcar 'file-name-sans-extension files))
  14764. (setq files (mapcar
  14765. (lambda (x) (if (string-match remove-re x) nil x))
  14766. files))
  14767. (setq files (delq nil files))
  14768. (mapc
  14769. (lambda (f)
  14770. (when (featurep (intern (file-name-nondirectory f)))
  14771. (if (and (not uncompiled)
  14772. (file-exists-p (concat f ".elc")))
  14773. (load (concat f ".elc") nil nil t)
  14774. (load (concat f ".el") nil nil t))))
  14775. files))
  14776. (org-version))
  14777. ;;;###autoload
  14778. (defun org-customize ()
  14779. "Call the customize function with org as argument."
  14780. (interactive)
  14781. (org-load-modules-maybe)
  14782. (org-require-autoloaded-modules)
  14783. (customize-browse 'org))
  14784. (defun org-create-customize-menu ()
  14785. "Create a full customization menu for Org-mode, insert it into the menu."
  14786. (interactive)
  14787. (org-load-modules-maybe)
  14788. (org-require-autoloaded-modules)
  14789. (if (fboundp 'customize-menu-create)
  14790. (progn
  14791. (easy-menu-change
  14792. '("Org") "Customize"
  14793. `(["Browse Org group" org-customize t]
  14794. "--"
  14795. ,(customize-menu-create 'org)
  14796. ["Set" Custom-set t]
  14797. ["Save" Custom-save t]
  14798. ["Reset to Current" Custom-reset-current t]
  14799. ["Reset to Saved" Custom-reset-saved t]
  14800. ["Reset to Standard Settings" Custom-reset-standard t]))
  14801. (message "\"Org\"-menu now contains full customization menu"))
  14802. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  14803. ;;;; Miscellaneous stuff
  14804. ;;; Generally useful functions
  14805. (defun org-get-at-bol (property)
  14806. "Get text property PROPERTY at beginning of line."
  14807. (get-text-property (point-at-bol) property))
  14808. (defun org-find-text-property-in-string (prop s)
  14809. "Return the first non-nil value of property PROP in string S."
  14810. (or (get-text-property 0 prop s)
  14811. (get-text-property (or (next-single-property-change 0 prop s) 0)
  14812. prop s)))
  14813. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  14814. "Display the given MESSAGE as a warning."
  14815. (if (fboundp 'display-warning)
  14816. (display-warning 'org message
  14817. (if (featurep 'xemacs)
  14818. 'warning
  14819. :warning))
  14820. (let ((buf (get-buffer-create "*Org warnings*")))
  14821. (with-current-buffer buf
  14822. (goto-char (point-max))
  14823. (insert "Warning (Org): " message)
  14824. (unless (bolp)
  14825. (newline)))
  14826. (display-buffer buf)
  14827. (sit-for 0))))
  14828. (defun org-in-commented-line ()
  14829. "Is point in a line starting with `#'?"
  14830. (equal (char-after (point-at-bol)) ?#))
  14831. (defun org-in-verbatim-emphasis ()
  14832. (save-match-data
  14833. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  14834. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  14835. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  14836. (if (and marker (marker-buffer marker)
  14837. (buffer-live-p (marker-buffer marker)))
  14838. (progn
  14839. (switch-to-buffer (marker-buffer marker))
  14840. (if (or (> marker (point-max)) (< marker (point-min)))
  14841. (widen))
  14842. (goto-char marker)
  14843. (org-show-context 'org-goto))
  14844. (if bookmark
  14845. (bookmark-jump bookmark)
  14846. (error "Cannot find location"))))
  14847. (defun org-quote-csv-field (s)
  14848. "Quote field for inclusion in CSV material."
  14849. (if (string-match "[\",]" s)
  14850. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  14851. s))
  14852. (defun org-plist-delete (plist property)
  14853. "Delete PROPERTY from PLIST.
  14854. This is in contrast to merely setting it to 0."
  14855. (let (p)
  14856. (while plist
  14857. (if (not (eq property (car plist)))
  14858. (setq p (plist-put p (car plist) (nth 1 plist))))
  14859. (setq plist (cddr plist)))
  14860. p))
  14861. (defun org-force-self-insert (N)
  14862. "Needed to enforce self-insert under remapping."
  14863. (interactive "p")
  14864. (self-insert-command N))
  14865. (defun org-string-width (s)
  14866. "Compute width of string, ignoring invisible characters.
  14867. This ignores character with invisibility property `org-link', and also
  14868. characters with property `org-cwidth', because these will become invisible
  14869. upon the next fontification round."
  14870. (let (b l)
  14871. (when (or (eq t buffer-invisibility-spec)
  14872. (assq 'org-link buffer-invisibility-spec))
  14873. (while (setq b (text-property-any 0 (length s)
  14874. 'invisible 'org-link s))
  14875. (setq s (concat (substring s 0 b)
  14876. (substring s (or (next-single-property-change
  14877. b 'invisible s) (length s)))))))
  14878. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  14879. (setq s (concat (substring s 0 b)
  14880. (substring s (or (next-single-property-change
  14881. b 'org-cwidth s) (length s))))))
  14882. (setq l (string-width s) b -1)
  14883. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  14884. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  14885. l))
  14886. (defun org-get-indentation (&optional line)
  14887. "Get the indentation of the current line, interpreting tabs.
  14888. When LINE is given, assume it represents a line and compute its indentation."
  14889. (if line
  14890. (if (string-match "^ *" (org-remove-tabs line))
  14891. (match-end 0))
  14892. (save-excursion
  14893. (beginning-of-line 1)
  14894. (skip-chars-forward " \t")
  14895. (current-column))))
  14896. (defun org-remove-tabs (s &optional width)
  14897. "Replace tabulators in S with spaces.
  14898. Assumes that s is a single line, starting in column 0."
  14899. (setq width (or width tab-width))
  14900. (while (string-match "\t" s)
  14901. (setq s (replace-match
  14902. (make-string
  14903. (- (* width (/ (+ (match-beginning 0) width) width))
  14904. (match-beginning 0)) ?\ )
  14905. t t s)))
  14906. s)
  14907. (defun org-fix-indentation (line ind)
  14908. "Fix indentation in LINE.
  14909. IND is a cons cell with target and minimum indentation.
  14910. If the current indentation in LINE is smaller than the minimum,
  14911. leave it alone. If it is larger than ind, set it to the target."
  14912. (let* ((l (org-remove-tabs line))
  14913. (i (org-get-indentation l))
  14914. (i1 (car ind)) (i2 (cdr ind)))
  14915. (if (>= i i2) (setq l (substring line i2)))
  14916. (if (> i1 0)
  14917. (concat (make-string i1 ?\ ) l)
  14918. l)))
  14919. (defun org-remove-indentation (code &optional n)
  14920. "Remove the maximum common indentation from the lines in CODE.
  14921. N may optionally be the number of spaces to remove."
  14922. (with-temp-buffer
  14923. (insert code)
  14924. (org-do-remove-indentation n)
  14925. (buffer-string)))
  14926. (defun org-do-remove-indentation (&optional n)
  14927. "Remove the maximum common indentation from the buffer."
  14928. (untabify (point-min) (point-max))
  14929. (let ((min 10000) re)
  14930. (if n
  14931. (setq min n)
  14932. (goto-char (point-min))
  14933. (while (re-search-forward "^ *[^ \n]" nil t)
  14934. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  14935. (unless (or (= min 0) (= min 10000))
  14936. (setq re (format "^ \\{%d\\}" min))
  14937. (goto-char (point-min))
  14938. (while (re-search-forward re nil t)
  14939. (replace-match "")
  14940. (end-of-line 1))
  14941. min)))
  14942. (defun org-fill-template (template alist)
  14943. "Find each %key of ALIST in TEMPLATE and replace it."
  14944. (let (entry key value)
  14945. (setq alist (sort (copy-sequence alist)
  14946. (lambda (a b) (< (length (car a)) (length (car b))))))
  14947. (while (setq entry (pop alist))
  14948. (setq template
  14949. (replace-regexp-in-string
  14950. (concat "%" (regexp-quote (car entry)))
  14951. (cdr entry) template t t)))
  14952. template))
  14953. (defun org-base-buffer (buffer)
  14954. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  14955. (if (not buffer)
  14956. buffer
  14957. (or (buffer-base-buffer buffer)
  14958. buffer)))
  14959. (defun org-trim (s)
  14960. "Remove whitespace at beginning and end of string."
  14961. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  14962. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  14963. s)
  14964. (defun org-wrap (string &optional width lines)
  14965. "Wrap string to either a number of lines, or a width in characters.
  14966. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  14967. that costs. If there is a word longer than WIDTH, the text is actually
  14968. wrapped to the length of that word.
  14969. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  14970. many lines, whatever width that takes.
  14971. The return value is a list of lines, without newlines at the end."
  14972. (let* ((words (org-split-string string "[ \t\n]+"))
  14973. (maxword (apply 'max (mapcar 'org-string-width words)))
  14974. w ll)
  14975. (cond (width
  14976. (org-do-wrap words (max maxword width)))
  14977. (lines
  14978. (setq w maxword)
  14979. (setq ll (org-do-wrap words maxword))
  14980. (if (<= (length ll) lines)
  14981. ll
  14982. (setq ll words)
  14983. (while (> (length ll) lines)
  14984. (setq w (1+ w))
  14985. (setq ll (org-do-wrap words w)))
  14986. ll))
  14987. (t (error "Cannot wrap this")))))
  14988. (defun org-do-wrap (words width)
  14989. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  14990. (let (lines line)
  14991. (while words
  14992. (setq line (pop words))
  14993. (while (and words (< (+ (length line) (length (car words))) width))
  14994. (setq line (concat line " " (pop words))))
  14995. (setq lines (push line lines)))
  14996. (nreverse lines)))
  14997. (defun org-split-string (string &optional separators)
  14998. "Splits STRING into substrings at SEPARATORS.
  14999. No empty strings are returned if there are matches at the beginning
  15000. and end of string."
  15001. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  15002. (start 0)
  15003. notfirst
  15004. (list nil))
  15005. (while (and (string-match rexp string
  15006. (if (and notfirst
  15007. (= start (match-beginning 0))
  15008. (< start (length string)))
  15009. (1+ start) start))
  15010. (< (match-beginning 0) (length string)))
  15011. (setq notfirst t)
  15012. (or (eq (match-beginning 0) 0)
  15013. (and (eq (match-beginning 0) (match-end 0))
  15014. (eq (match-beginning 0) start))
  15015. (setq list
  15016. (cons (substring string start (match-beginning 0))
  15017. list)))
  15018. (setq start (match-end 0)))
  15019. (or (eq start (length string))
  15020. (setq list
  15021. (cons (substring string start)
  15022. list)))
  15023. (nreverse list)))
  15024. (defun org-quote-vert (s)
  15025. "Replace \"|\" with \"\\vert\"."
  15026. (while (string-match "|" s)
  15027. (setq s (replace-match "\\vert" t t s)))
  15028. s)
  15029. (defun org-uuidgen-p (s)
  15030. "Is S an ID created by UUIDGEN?"
  15031. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  15032. (defun org-context ()
  15033. "Return a list of contexts of the current cursor position.
  15034. If several contexts apply, all are returned.
  15035. Each context entry is a list with a symbol naming the context, and
  15036. two positions indicating start and end of the context. Possible
  15037. contexts are:
  15038. :headline anywhere in a headline
  15039. :headline-stars on the leading stars in a headline
  15040. :todo-keyword on a TODO keyword (including DONE) in a headline
  15041. :tags on the TAGS in a headline
  15042. :priority on the priority cookie in a headline
  15043. :item on the first line of a plain list item
  15044. :item-bullet on the bullet/number of a plain list item
  15045. :checkbox on the checkbox in a plain list item
  15046. :table in an org-mode table
  15047. :table-special on a special filed in a table
  15048. :table-table in a table.el table
  15049. :link on a hyperlink
  15050. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  15051. :target on a <<target>>
  15052. :radio-target on a <<<radio-target>>>
  15053. :latex-fragment on a LaTeX fragment
  15054. :latex-preview on a LaTeX fragment with overlayed preview image
  15055. This function expects the position to be visible because it uses font-lock
  15056. faces as a help to recognize the following contexts: :table-special, :link,
  15057. and :keyword."
  15058. (let* ((f (get-text-property (point) 'face))
  15059. (faces (if (listp f) f (list f)))
  15060. (p (point)) clist o)
  15061. ;; First the large context
  15062. (cond
  15063. ((org-on-heading-p t)
  15064. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  15065. (when (progn
  15066. (beginning-of-line 1)
  15067. (looking-at org-todo-line-tags-regexp))
  15068. (push (org-point-in-group p 1 :headline-stars) clist)
  15069. (push (org-point-in-group p 2 :todo-keyword) clist)
  15070. (push (org-point-in-group p 4 :tags) clist))
  15071. (goto-char p)
  15072. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  15073. (if (looking-at "\\[#[A-Z0-9]\\]")
  15074. (push (org-point-in-group p 0 :priority) clist)))
  15075. ((org-at-item-p)
  15076. (push (org-point-in-group p 2 :item-bullet) clist)
  15077. (push (list :item (point-at-bol)
  15078. (save-excursion (org-end-of-item) (point)))
  15079. clist)
  15080. (and (org-at-item-checkbox-p)
  15081. (push (org-point-in-group p 0 :checkbox) clist)))
  15082. ((org-at-table-p)
  15083. (push (list :table (org-table-begin) (org-table-end)) clist)
  15084. (if (memq 'org-formula faces)
  15085. (push (list :table-special
  15086. (previous-single-property-change p 'face)
  15087. (next-single-property-change p 'face)) clist)))
  15088. ((org-at-table-p 'any)
  15089. (push (list :table-table) clist)))
  15090. (goto-char p)
  15091. ;; Now the small context
  15092. (cond
  15093. ((org-at-timestamp-p)
  15094. (push (org-point-in-group p 0 :timestamp) clist))
  15095. ((memq 'org-link faces)
  15096. (push (list :link
  15097. (previous-single-property-change p 'face)
  15098. (next-single-property-change p 'face)) clist))
  15099. ((memq 'org-special-keyword faces)
  15100. (push (list :keyword
  15101. (previous-single-property-change p 'face)
  15102. (next-single-property-change p 'face)) clist))
  15103. ((org-on-target-p)
  15104. (push (org-point-in-group p 0 :target) clist)
  15105. (goto-char (1- (match-beginning 0)))
  15106. (if (looking-at org-radio-target-regexp)
  15107. (push (org-point-in-group p 0 :radio-target) clist))
  15108. (goto-char p))
  15109. ((setq o (car (delq nil
  15110. (mapcar
  15111. (lambda (x)
  15112. (if (memq x org-latex-fragment-image-overlays) x))
  15113. (org-overlays-at (point))))))
  15114. (push (list :latex-fragment
  15115. (org-overlay-start o) (org-overlay-end o)) clist)
  15116. (push (list :latex-preview
  15117. (org-overlay-start o) (org-overlay-end o)) clist))
  15118. ((org-inside-LaTeX-fragment-p)
  15119. ;; FIXME: positions wrong.
  15120. (push (list :latex-fragment (point) (point)) clist)))
  15121. (setq clist (nreverse (delq nil clist)))
  15122. clist))
  15123. ;; FIXME: Compare with at-regexp-p Do we need both?
  15124. (defun org-in-regexp (re &optional nlines visually)
  15125. "Check if point is inside a match of regexp.
  15126. Normally only the current line is checked, but you can include NLINES extra
  15127. lines both before and after point into the search.
  15128. If VISUALLY is set, require that the cursor is not after the match but
  15129. really on, so that the block visually is on the match."
  15130. (catch 'exit
  15131. (let ((pos (point))
  15132. (eol (point-at-eol (+ 1 (or nlines 0))))
  15133. (inc (if visually 1 0)))
  15134. (save-excursion
  15135. (beginning-of-line (- 1 (or nlines 0)))
  15136. (while (re-search-forward re eol t)
  15137. (if (and (<= (match-beginning 0) pos)
  15138. (>= (+ inc (match-end 0)) pos))
  15139. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  15140. (defun org-at-regexp-p (regexp)
  15141. "Is point inside a match of REGEXP in the current line?"
  15142. (catch 'exit
  15143. (save-excursion
  15144. (let ((pos (point)) (end (point-at-eol)))
  15145. (beginning-of-line 1)
  15146. (while (re-search-forward regexp end t)
  15147. (if (and (<= (match-beginning 0) pos)
  15148. (>= (match-end 0) pos))
  15149. (throw 'exit t)))
  15150. nil))))
  15151. (defun org-occur-in-agenda-files (regexp &optional nlines)
  15152. "Call `multi-occur' with buffers for all agenda files."
  15153. (interactive "sOrg-files matching: \np")
  15154. (let* ((files (org-agenda-files))
  15155. (tnames (mapcar 'file-truename files))
  15156. (extra org-agenda-text-search-extra-files)
  15157. f)
  15158. (when (eq (car extra) 'agenda-archives)
  15159. (setq extra (cdr extra))
  15160. (setq files (org-add-archive-files files)))
  15161. (while (setq f (pop extra))
  15162. (unless (member (file-truename f) tnames)
  15163. (add-to-list 'files f 'append)
  15164. (add-to-list 'tnames (file-truename f) 'append)))
  15165. (multi-occur
  15166. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  15167. regexp)))
  15168. (if (boundp 'occur-mode-find-occurrence-hook)
  15169. ;; Emacs 23
  15170. (add-hook 'occur-mode-find-occurrence-hook
  15171. (lambda ()
  15172. (when (org-mode-p)
  15173. (org-reveal))))
  15174. ;; Emacs 22
  15175. (defadvice occur-mode-goto-occurrence
  15176. (after org-occur-reveal activate)
  15177. (and (org-mode-p) (org-reveal)))
  15178. (defadvice occur-mode-goto-occurrence-other-window
  15179. (after org-occur-reveal activate)
  15180. (and (org-mode-p) (org-reveal)))
  15181. (defadvice occur-mode-display-occurrence
  15182. (after org-occur-reveal activate)
  15183. (when (org-mode-p)
  15184. (let ((pos (occur-mode-find-occurrence)))
  15185. (with-current-buffer (marker-buffer pos)
  15186. (save-excursion
  15187. (goto-char pos)
  15188. (org-reveal)))))))
  15189. (defun org-occur-link-in-agenda-files ()
  15190. "Create a link and search for it in the agendas.
  15191. The link is not stored in `org-stored-links', it is just created
  15192. for the search purpose."
  15193. (interactive)
  15194. (let ((link (condition-case nil
  15195. (org-store-link nil)
  15196. (error "Unable to create a link to here"))))
  15197. (org-occur-in-agenda-files (regexp-quote link))))
  15198. (defun org-uniquify (list)
  15199. "Remove duplicate elements from LIST."
  15200. (let (res)
  15201. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  15202. res))
  15203. (defun org-delete-all (elts list)
  15204. "Remove all elements in ELTS from LIST."
  15205. (while elts
  15206. (setq list (delete (pop elts) list)))
  15207. list)
  15208. (defun org-back-over-empty-lines ()
  15209. "Move backwards over whitespace, to the beginning of the first empty line.
  15210. Returns the number of empty lines passed."
  15211. (let ((pos (point)))
  15212. (skip-chars-backward " \t\n\r")
  15213. (beginning-of-line 2)
  15214. (goto-char (min (point) pos))
  15215. (count-lines (point) pos)))
  15216. (defun org-skip-whitespace ()
  15217. (skip-chars-forward " \t\n\r"))
  15218. (defun org-point-in-group (point group &optional context)
  15219. "Check if POINT is in match-group GROUP.
  15220. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  15221. match. If the match group does ot exist or point is not inside it,
  15222. return nil."
  15223. (and (match-beginning group)
  15224. (>= point (match-beginning group))
  15225. (<= point (match-end group))
  15226. (if context
  15227. (list context (match-beginning group) (match-end group))
  15228. t)))
  15229. (defun org-switch-to-buffer-other-window (&rest args)
  15230. "Switch to buffer in a second window on the current frame.
  15231. In particular, do not allow pop-up frames."
  15232. (let (pop-up-frames special-display-buffer-names special-display-regexps
  15233. special-display-function)
  15234. (apply 'switch-to-buffer-other-window args)))
  15235. (defun org-combine-plists (&rest plists)
  15236. "Create a single property list from all plists in PLISTS.
  15237. The process starts by copying the first list, and then setting properties
  15238. from the other lists. Settings in the last list are the most significant
  15239. ones and overrule settings in the other lists."
  15240. (let ((rtn (copy-sequence (pop plists)))
  15241. p v ls)
  15242. (while plists
  15243. (setq ls (pop plists))
  15244. (while ls
  15245. (setq p (pop ls) v (pop ls))
  15246. (setq rtn (plist-put rtn p v))))
  15247. rtn))
  15248. (defun org-move-line-down (arg)
  15249. "Move the current line down. With prefix argument, move it past ARG lines."
  15250. (interactive "p")
  15251. (let ((col (current-column))
  15252. beg end pos)
  15253. (beginning-of-line 1) (setq beg (point))
  15254. (beginning-of-line 2) (setq end (point))
  15255. (beginning-of-line (+ 1 arg))
  15256. (setq pos (move-marker (make-marker) (point)))
  15257. (insert (delete-and-extract-region beg end))
  15258. (goto-char pos)
  15259. (org-move-to-column col)))
  15260. (defun org-move-line-up (arg)
  15261. "Move the current line up. With prefix argument, move it past ARG lines."
  15262. (interactive "p")
  15263. (let ((col (current-column))
  15264. beg end pos)
  15265. (beginning-of-line 1) (setq beg (point))
  15266. (beginning-of-line 2) (setq end (point))
  15267. (beginning-of-line (- arg))
  15268. (setq pos (move-marker (make-marker) (point)))
  15269. (insert (delete-and-extract-region beg end))
  15270. (goto-char pos)
  15271. (org-move-to-column col)))
  15272. (defun org-replace-escapes (string table)
  15273. "Replace %-escapes in STRING with values in TABLE.
  15274. TABLE is an association list with keys like \"%a\" and string values.
  15275. The sequences in STRING may contain normal field width and padding information,
  15276. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  15277. so values can contain further %-escapes if they are define later in TABLE."
  15278. (let ((case-fold-search nil)
  15279. e re rpl)
  15280. (while (setq e (pop table))
  15281. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  15282. (while (string-match re string)
  15283. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  15284. (cdr e)))
  15285. (setq string (replace-match rpl t t string))))
  15286. string))
  15287. (defun org-sublist (list start end)
  15288. "Return a section of LIST, from START to END.
  15289. Counting starts at 1."
  15290. (let (rtn (c start))
  15291. (setq list (nthcdr (1- start) list))
  15292. (while (and list (<= c end))
  15293. (push (pop list) rtn)
  15294. (setq c (1+ c)))
  15295. (nreverse rtn)))
  15296. (defun org-find-base-buffer-visiting (file)
  15297. "Like `find-buffer-visiting' but always return the base buffer and
  15298. not an indirect buffer."
  15299. (let ((buf (or (get-file-buffer file)
  15300. (find-buffer-visiting file))))
  15301. (if buf
  15302. (or (buffer-base-buffer buf) buf)
  15303. nil)))
  15304. (defun org-image-file-name-regexp (&optional extensions)
  15305. "Return regexp matching the file names of images.
  15306. If EXTENSIONS is given, only match these."
  15307. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  15308. (image-file-name-regexp)
  15309. (let ((image-file-name-extensions
  15310. (or extensions
  15311. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  15312. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  15313. (concat "\\."
  15314. (regexp-opt (nconc (mapcar 'upcase
  15315. image-file-name-extensions)
  15316. image-file-name-extensions)
  15317. t)
  15318. "\\'"))))
  15319. (defun org-file-image-p (file &optional extensions)
  15320. "Return non-nil if FILE is an image."
  15321. (save-match-data
  15322. (string-match (org-image-file-name-regexp extensions) file)))
  15323. (defun org-get-cursor-date ()
  15324. "Return the date at cursor in as a time.
  15325. This works in the calendar and in the agenda, anywhere else it just
  15326. returns the current time."
  15327. (let (date day defd)
  15328. (cond
  15329. ((eq major-mode 'calendar-mode)
  15330. (setq date (calendar-cursor-to-date)
  15331. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15332. ((eq major-mode 'org-agenda-mode)
  15333. (setq day (get-text-property (point) 'day))
  15334. (if day
  15335. (setq date (calendar-gregorian-from-absolute day)
  15336. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  15337. (nth 2 date))))))
  15338. (or defd (current-time))))
  15339. (defvar org-agenda-action-marker (make-marker)
  15340. "Marker pointing to the entry for the next agenda action.")
  15341. (defun org-mark-entry-for-agenda-action ()
  15342. "Mark the current entry as target of an agenda action.
  15343. Agenda actions are actions executed from the agenda with the key `k',
  15344. which make use of the date at the cursor."
  15345. (interactive)
  15346. (move-marker org-agenda-action-marker
  15347. (save-excursion (org-back-to-heading t) (point))
  15348. (current-buffer))
  15349. (message
  15350. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  15351. ;;; Paragraph filling stuff.
  15352. ;; We want this to be just right, so use the full arsenal.
  15353. (defun org-indent-line-function ()
  15354. "Indent line like previous, but further if previous was headline or item."
  15355. (interactive)
  15356. (let* ((pos (point))
  15357. (itemp (org-at-item-p))
  15358. (case-fold-search t)
  15359. (org-drawer-regexp (or org-drawer-regexp "\000"))
  15360. column bpos bcol tpos tcol bullet btype bullet-type)
  15361. ;; Find the previous relevant line
  15362. (beginning-of-line 1)
  15363. (cond
  15364. ((looking-at "#") (setq column 0))
  15365. ((looking-at "\\*+ ") (setq column 0))
  15366. ((and (looking-at "[ \t]*:END:")
  15367. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  15368. (save-excursion
  15369. (goto-char (1- (match-beginning 1)))
  15370. (setq column (current-column))))
  15371. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  15372. (save-excursion
  15373. (re-search-backward
  15374. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  15375. (setq column (org-get-indentation (match-string 0))))
  15376. (t
  15377. (beginning-of-line 0)
  15378. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  15379. (not (looking-at "[ \t]*:END:"))
  15380. (not (looking-at org-drawer-regexp)))
  15381. (beginning-of-line 0))
  15382. (cond
  15383. ((looking-at "\\*+[ \t]+")
  15384. (if (not org-adapt-indentation)
  15385. (setq column 0)
  15386. (goto-char (match-end 0))
  15387. (setq column (current-column))))
  15388. ((looking-at org-drawer-regexp)
  15389. (goto-char (1- (match-beginning 1)))
  15390. (setq column (current-column)))
  15391. ((looking-at "\\([ \t]*\\):END:")
  15392. (goto-char (match-end 1))
  15393. (setq column (current-column)))
  15394. ((org-in-item-p)
  15395. (org-beginning-of-item)
  15396. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  15397. (setq bpos (match-beginning 1) tpos (match-end 0)
  15398. bcol (progn (goto-char bpos) (current-column))
  15399. tcol (progn (goto-char tpos) (current-column))
  15400. bullet (match-string 1)
  15401. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  15402. (if (> tcol (+ bcol org-description-max-indent))
  15403. (setq tcol (+ bcol 5)))
  15404. (if (not itemp)
  15405. (setq column tcol)
  15406. (goto-char pos)
  15407. (beginning-of-line 1)
  15408. (if (looking-at "\\S-")
  15409. (progn
  15410. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  15411. (setq bullet (match-string 1)
  15412. btype (if (string-match "[0-9]" bullet) "n" bullet))
  15413. (setq column (if (equal btype bullet-type) bcol tcol)))
  15414. (setq column (org-get-indentation)))))
  15415. (t (setq column (org-get-indentation))))))
  15416. (goto-char pos)
  15417. (if (<= (current-column) (current-indentation))
  15418. (org-indent-line-to column)
  15419. (save-excursion (org-indent-line-to column)))
  15420. (setq column (current-column))
  15421. (beginning-of-line 1)
  15422. (if (looking-at
  15423. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  15424. (replace-match (concat (match-string 1)
  15425. (format org-property-format
  15426. (match-string 2) (match-string 3)))
  15427. t t))
  15428. (org-move-to-column column)))
  15429. (defun org-set-autofill-regexps ()
  15430. (interactive)
  15431. ;; In the paragraph separator we include headlines, because filling
  15432. ;; text in a line directly attached to a headline would otherwise
  15433. ;; fill the headline as well.
  15434. (org-set-local 'comment-start-skip "^#+[ \t]*")
  15435. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  15436. ;; The paragraph starter includes hand-formatted lists.
  15437. (org-set-local
  15438. 'paragraph-start
  15439. (concat
  15440. "\f" "\\|"
  15441. "[ ]*$" "\\|"
  15442. "\\*+ " "\\|"
  15443. "[ \t]*#" "\\|"
  15444. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  15445. "[ \t]*[:|]" "\\|"
  15446. "\\$\\$" "\\|"
  15447. "\\\\\\(begin\\|end\\|[][]\\)"))
  15448. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  15449. ;; But only if the user has not turned off tables or fixed-width regions
  15450. (org-set-local
  15451. 'auto-fill-inhibit-regexp
  15452. (concat "\\*+ \\|#\\+"
  15453. "\\|[ \t]*" org-keyword-time-regexp
  15454. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  15455. (concat
  15456. "\\|[ \t]*["
  15457. (if org-enable-table-editor "|" "")
  15458. (if org-enable-fixed-width-editor ":" "")
  15459. "]"))))
  15460. ;; We use our own fill-paragraph function, to make sure that tables
  15461. ;; and fixed-width regions are not wrapped. That function will pass
  15462. ;; through to `fill-paragraph' when appropriate.
  15463. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  15464. ; Adaptive filling: To get full control, first make sure that
  15465. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  15466. (org-set-local 'adaptive-fill-regexp "\000")
  15467. (org-set-local 'adaptive-fill-function
  15468. 'org-adaptive-fill-function)
  15469. (org-set-local
  15470. 'align-mode-rules-list
  15471. '((org-in-buffer-settings
  15472. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  15473. (modes . '(org-mode))))))
  15474. (defun org-fill-paragraph (&optional justify)
  15475. "Re-align a table, pass through to fill-paragraph if no table."
  15476. (let ((table-p (org-at-table-p))
  15477. (table.el-p (org-at-table.el-p)))
  15478. (cond ((and (equal (char-after (point-at-bol)) ?*)
  15479. (save-excursion (goto-char (point-at-bol))
  15480. (looking-at outline-regexp)))
  15481. t) ; skip headlines
  15482. (table.el-p t) ; skip table.el tables
  15483. (table-p (org-table-align) t) ; align org-mode tables
  15484. (t nil)))) ; call paragraph-fill
  15485. ;; For reference, this is the default value of adaptive-fill-regexp
  15486. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  15487. (defun org-adaptive-fill-function ()
  15488. "Return a fill prefix for org-mode files.
  15489. In particular, this makes sure hanging paragraphs for hand-formatted lists
  15490. work correctly."
  15491. (cond ((looking-at "#[ \t]+")
  15492. (match-string 0))
  15493. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  15494. (save-excursion
  15495. (if (> (match-end 1) (+ (match-beginning 1)
  15496. org-description-max-indent))
  15497. (goto-char (+ (match-beginning 1) 5))
  15498. (goto-char (match-end 0)))
  15499. (make-string (current-column) ?\ )))
  15500. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  15501. (save-excursion
  15502. (goto-char (match-end 0))
  15503. (make-string (current-column) ?\ )))
  15504. (t nil)))
  15505. ;;; Other stuff.
  15506. (defun org-toggle-fixed-width-section (arg)
  15507. "Toggle the fixed-width export.
  15508. If there is no active region, the QUOTE keyword at the current headline is
  15509. inserted or removed. When present, it causes the text between this headline
  15510. and the next to be exported as fixed-width text, and unmodified.
  15511. If there is an active region, this command adds or removes a colon as the
  15512. first character of this line. If the first character of a line is a colon,
  15513. this line is also exported in fixed-width font."
  15514. (interactive "P")
  15515. (let* ((cc 0)
  15516. (regionp (org-region-active-p))
  15517. (beg (if regionp (region-beginning) (point)))
  15518. (end (if regionp (region-end)))
  15519. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  15520. (case-fold-search nil)
  15521. (re "[ \t]*\\(: \\)")
  15522. off)
  15523. (if regionp
  15524. (save-excursion
  15525. (goto-char beg)
  15526. (setq cc (current-column))
  15527. (beginning-of-line 1)
  15528. (setq off (looking-at re))
  15529. (while (> nlines 0)
  15530. (setq nlines (1- nlines))
  15531. (beginning-of-line 1)
  15532. (cond
  15533. (arg
  15534. (org-move-to-column cc t)
  15535. (insert ": \n")
  15536. (forward-line -1))
  15537. ((and off (looking-at re))
  15538. (replace-match "" t t nil 1))
  15539. ((not off) (org-move-to-column cc t) (insert ": ")))
  15540. (forward-line 1)))
  15541. (save-excursion
  15542. (org-back-to-heading)
  15543. (if (looking-at (concat outline-regexp
  15544. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  15545. (replace-match "" t t nil 1)
  15546. (if (looking-at outline-regexp)
  15547. (progn
  15548. (goto-char (match-end 0))
  15549. (insert org-quote-string " "))))))))
  15550. (defun org-reftex-citation ()
  15551. "Use reftex-citation to insert a citation into the buffer.
  15552. This looks for a line like
  15553. #+BIBLIOGRAPHY: foo plain option:-d
  15554. and derives from it that foo.bib is the bbliography file relevant
  15555. for this document. It then installs the necessary environment for RefTeX
  15556. to work in this buffer and calls `reftex-citation' to insert a citation
  15557. into the buffer.
  15558. Export of such citations to both LaTeX and HTML is handled by the contributed
  15559. package org-exp-bibtex by Taru Karttunen."
  15560. (interactive)
  15561. (let ((reftex-docstruct-symbol 'rds)
  15562. (reftex-cite-format "\\cite{%l}")
  15563. rds bib)
  15564. (save-excursion
  15565. (save-restriction
  15566. (widen)
  15567. (let ((case-fold-search t)
  15568. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  15569. (if (not (save-excursion
  15570. (or (re-search-forward re nil t)
  15571. (re-search-backward re nil t))))
  15572. (error "No bibliography defined in file")
  15573. (setq bib (concat (match-string 1) ".bib")
  15574. rds (list (list 'bib bib)))))))
  15575. (call-interactively 'reftex-citation)))
  15576. ;;;; Functions extending outline functionality
  15577. (defun org-beginning-of-line (&optional arg)
  15578. "Go to the beginning of the current line. If that is invisible, continue
  15579. to a visible line beginning. This makes the function of C-a more intuitive.
  15580. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15581. first attempt, and only move to after the tags when the cursor is already
  15582. beyond the end of the headline."
  15583. (interactive "P")
  15584. (let ((pos (point))
  15585. (special (if (consp org-special-ctrl-a/e)
  15586. (car org-special-ctrl-a/e)
  15587. org-special-ctrl-a/e))
  15588. refpos)
  15589. (if (org-bound-and-true-p line-move-visual)
  15590. (beginning-of-visual-line 1)
  15591. (beginning-of-line 1))
  15592. (if (and arg (fboundp 'move-beginning-of-line))
  15593. (call-interactively 'move-beginning-of-line)
  15594. (if (bobp)
  15595. nil
  15596. (backward-char 1)
  15597. (if (org-invisible-p)
  15598. (while (and (not (bobp)) (org-invisible-p))
  15599. (backward-char 1)
  15600. (beginning-of-line 1))
  15601. (forward-char 1))))
  15602. (when special
  15603. (cond
  15604. ((and (looking-at org-complex-heading-regexp)
  15605. (= (char-after (match-end 1)) ?\ ))
  15606. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  15607. (point-at-eol)))
  15608. (goto-char
  15609. (if (eq special t)
  15610. (cond ((> pos refpos) refpos)
  15611. ((= pos (point)) refpos)
  15612. (t (point)))
  15613. (cond ((> pos (point)) (point))
  15614. ((not (eq last-command this-command)) (point))
  15615. (t refpos)))))
  15616. ((org-at-item-p)
  15617. (goto-char
  15618. (if (eq special t)
  15619. (cond ((> pos (match-end 4)) (match-end 4))
  15620. ((= pos (point)) (match-end 4))
  15621. (t (point)))
  15622. (cond ((> pos (point)) (point))
  15623. ((not (eq last-command this-command)) (point))
  15624. (t (match-end 4))))))))
  15625. (org-no-warnings
  15626. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15627. (defun org-end-of-line (&optional arg)
  15628. "Go to the end of the line.
  15629. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15630. first attempt, and only move to after the tags when the cursor is already
  15631. beyond the end of the headline."
  15632. (interactive "P")
  15633. (let ((special (if (consp org-special-ctrl-a/e)
  15634. (cdr org-special-ctrl-a/e)
  15635. org-special-ctrl-a/e)))
  15636. (if (or (not special)
  15637. (not (org-on-heading-p))
  15638. arg)
  15639. (call-interactively
  15640. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  15641. ((fboundp 'move-end-of-line) 'move-end-of-line)
  15642. (t 'end-of-line)))
  15643. (let ((pos (point)))
  15644. (beginning-of-line 1)
  15645. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
  15646. (if (eq special t)
  15647. (if (or (< pos (match-beginning 1))
  15648. (= pos (match-end 0)))
  15649. (goto-char (match-beginning 1))
  15650. (goto-char (match-end 0)))
  15651. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  15652. (goto-char (match-end 0))
  15653. (goto-char (match-beginning 1))))
  15654. (call-interactively (if (fboundp 'move-end-of-line)
  15655. 'move-end-of-line
  15656. 'end-of-line)))))
  15657. (org-no-warnings
  15658. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15659. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  15660. (define-key org-mode-map "\C-e" 'org-end-of-line)
  15661. (define-key org-mode-map [home] 'org-beginning-of-line)
  15662. (define-key org-mode-map [end] 'org-end-of-line)
  15663. (defun org-backward-sentence (&optional arg)
  15664. "Go to beginning of sentence, or beginning of table field.
  15665. This will call `backward-sentence' or `org-table-beginning-of-field',
  15666. depending on context."
  15667. (interactive "P")
  15668. (cond
  15669. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  15670. (t (call-interactively 'backward-sentence))))
  15671. (defun org-forward-sentence (&optional arg)
  15672. "Go to end of sentence, or end of table field.
  15673. This will call `forward-sentence' or `org-table-end-of-field',
  15674. depending on context."
  15675. (interactive "P")
  15676. (cond
  15677. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  15678. (t (call-interactively 'forward-sentence))))
  15679. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  15680. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  15681. (defun org-kill-line (&optional arg)
  15682. "Kill line, to tags or end of line."
  15683. (interactive "P")
  15684. (cond
  15685. ((or (not org-special-ctrl-k)
  15686. (bolp)
  15687. (not (org-on-heading-p)))
  15688. (call-interactively 'kill-line))
  15689. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  15690. (kill-region (point) (match-beginning 1))
  15691. (org-set-tags nil t))
  15692. (t (kill-region (point) (point-at-eol)))))
  15693. (define-key org-mode-map "\C-k" 'org-kill-line)
  15694. (defun org-yank (&optional arg)
  15695. "Yank. If the kill is a subtree, treat it specially.
  15696. This command will look at the current kill and check if is a single
  15697. subtree, or a series of subtrees[1]. If it passes the test, and if the
  15698. cursor is at the beginning of a line or after the stars of a currently
  15699. empty headline, then the yank is handled specially. How exactly depends
  15700. on the value of the following variables, both set by default.
  15701. org-yank-folded-subtrees
  15702. When set, the subtree(s) will be folded after insertion, but only
  15703. if doing so would now swallow text after the yanked text.
  15704. org-yank-adjusted-subtrees
  15705. When set, the subtree will be promoted or demoted in order to
  15706. fit into the local outline tree structure, which means that the level
  15707. will be adjusted so that it becomes the smaller one of the two
  15708. *visible* surrounding headings.
  15709. Any prefix to this command will cause `yank' to be called directly with
  15710. no special treatment. In particular, a simple `C-u' prefix will just
  15711. plainly yank the text as it is.
  15712. \[1] The test checks if the first non-white line is a heading
  15713. and if there are no other headings with fewer stars."
  15714. (interactive "P")
  15715. (org-yank-generic 'yank arg))
  15716. (defun org-yank-generic (command arg)
  15717. "Perform some yank-like command.
  15718. This function implements the behavior described in the `org-yank'
  15719. documentation. However, it has been generalized to work for any
  15720. interactive command with similar behavior."
  15721. ;; pretend to be command COMMAND
  15722. (setq this-command command)
  15723. (if arg
  15724. (call-interactively command)
  15725. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  15726. (and (org-kill-is-subtree-p)
  15727. (or (bolp)
  15728. (and (looking-at "[ \t]*$")
  15729. (string-match
  15730. "\\`\\*+\\'"
  15731. (buffer-substring (point-at-bol) (point)))))))
  15732. swallowp)
  15733. (cond
  15734. ((and subtreep org-yank-folded-subtrees)
  15735. (let ((beg (point))
  15736. end)
  15737. (if (and subtreep org-yank-adjusted-subtrees)
  15738. (org-paste-subtree nil nil 'for-yank)
  15739. (call-interactively command))
  15740. (setq end (point))
  15741. (goto-char beg)
  15742. (when (and (bolp) subtreep
  15743. (not (setq swallowp
  15744. (org-yank-folding-would-swallow-text beg end))))
  15745. (or (looking-at outline-regexp)
  15746. (re-search-forward (concat "^" outline-regexp) end t))
  15747. (while (and (< (point) end) (looking-at outline-regexp))
  15748. (hide-subtree)
  15749. (org-cycle-show-empty-lines 'folded)
  15750. (condition-case nil
  15751. (outline-forward-same-level 1)
  15752. (error (goto-char end)))))
  15753. (when swallowp
  15754. (message
  15755. "Inserted text not folded because that would swallow text"))
  15756. (goto-char end)
  15757. (skip-chars-forward " \t\n\r")
  15758. (beginning-of-line 1)
  15759. (push-mark beg 'nomsg)))
  15760. ((and subtreep org-yank-adjusted-subtrees)
  15761. (let ((beg (point-at-bol)))
  15762. (org-paste-subtree nil nil 'for-yank)
  15763. (push-mark beg 'nomsg)))
  15764. (t
  15765. (call-interactively command))))))
  15766. (defun org-yank-folding-would-swallow-text (beg end)
  15767. "Would hide-subtree at BEG swallow any text after END?"
  15768. (let (level)
  15769. (save-excursion
  15770. (goto-char beg)
  15771. (when (or (looking-at outline-regexp)
  15772. (re-search-forward (concat "^" outline-regexp) end t))
  15773. (setq level (org-outline-level)))
  15774. (goto-char end)
  15775. (skip-chars-forward " \t\r\n\v\f")
  15776. (if (or (eobp)
  15777. (and (bolp) (looking-at org-outline-regexp)
  15778. (<= (org-outline-level) level)))
  15779. nil ; Nothing would be swallowed
  15780. t)))) ; something would swallow
  15781. (define-key org-mode-map "\C-y" 'org-yank)
  15782. (defun org-invisible-p ()
  15783. "Check if point is at a character currently not visible."
  15784. ;; Early versions of noutline don't have `outline-invisible-p'.
  15785. (if (fboundp 'outline-invisible-p)
  15786. (outline-invisible-p)
  15787. (get-char-property (point) 'invisible)))
  15788. (defun org-invisible-p2 ()
  15789. "Check if point is at a character currently not visible."
  15790. (save-excursion
  15791. (if (and (eolp) (not (bobp))) (backward-char 1))
  15792. ;; Early versions of noutline don't have `outline-invisible-p'.
  15793. (if (fboundp 'outline-invisible-p)
  15794. (outline-invisible-p)
  15795. (get-char-property (point) 'invisible))))
  15796. (defun org-back-to-heading (&optional invisible-ok)
  15797. "Call `outline-back-to-heading', but provide a better error message."
  15798. (condition-case nil
  15799. (outline-back-to-heading invisible-ok)
  15800. (error (error "Before first headline at position %d in buffer %s"
  15801. (point) (current-buffer)))))
  15802. (defun org-before-first-heading-p ()
  15803. "Before first heading?"
  15804. (save-excursion
  15805. (null (re-search-backward "^\\*+ " nil t))))
  15806. (defun org-on-heading-p (&optional ignored)
  15807. (outline-on-heading-p t))
  15808. (defun org-at-heading-p (&optional ignored)
  15809. (outline-on-heading-p t))
  15810. (defun org-at-heading-or-item-p ()
  15811. (or (org-on-heading-p) (org-at-item-p)))
  15812. (defun org-on-target-p ()
  15813. (or (org-in-regexp org-radio-target-regexp)
  15814. (org-in-regexp org-target-regexp)))
  15815. (defun org-up-heading-all (arg)
  15816. "Move to the heading line of which the present line is a subheading.
  15817. This function considers both visible and invisible heading lines.
  15818. With argument, move up ARG levels."
  15819. (if (fboundp 'outline-up-heading-all)
  15820. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  15821. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  15822. (defun org-up-heading-safe ()
  15823. "Move to the heading line of which the present line is a subheading.
  15824. This version will not throw an error. It will return the level of the
  15825. headline found, or nil if no higher level is found.
  15826. Also, this function will be a lot faster than `outline-up-heading',
  15827. because it relies on stars being the outline starters. This can really
  15828. make a significant difference in outlines with very many siblings."
  15829. (let (start-level re)
  15830. (org-back-to-heading t)
  15831. (setq start-level (funcall outline-level))
  15832. (if (equal start-level 1)
  15833. nil
  15834. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  15835. (if (re-search-backward re nil t)
  15836. (funcall outline-level)))))
  15837. (defun org-first-sibling-p ()
  15838. "Is this heading the first child of its parents?"
  15839. (interactive)
  15840. (let ((re (concat "^" outline-regexp))
  15841. level l)
  15842. (unless (org-at-heading-p t)
  15843. (error "Not at a heading"))
  15844. (setq level (funcall outline-level))
  15845. (save-excursion
  15846. (if (not (re-search-backward re nil t))
  15847. t
  15848. (setq l (funcall outline-level))
  15849. (< l level)))))
  15850. (defun org-goto-sibling (&optional previous)
  15851. "Goto the next sibling, even if it is invisible.
  15852. When PREVIOUS is set, go to the previous sibling instead. Returns t
  15853. when a sibling was found. When none is found, return nil and don't
  15854. move point."
  15855. (let ((fun (if previous 're-search-backward 're-search-forward))
  15856. (pos (point))
  15857. (re (concat "^" outline-regexp))
  15858. level l)
  15859. (when (condition-case nil (org-back-to-heading t) (error nil))
  15860. (setq level (funcall outline-level))
  15861. (catch 'exit
  15862. (or previous (forward-char 1))
  15863. (while (funcall fun re nil t)
  15864. (setq l (funcall outline-level))
  15865. (when (< l level) (goto-char pos) (throw 'exit nil))
  15866. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  15867. (goto-char pos)
  15868. nil))))
  15869. (defun org-show-siblings ()
  15870. "Show all siblings of the current headline."
  15871. (save-excursion
  15872. (while (org-goto-sibling) (org-flag-heading nil)))
  15873. (save-excursion
  15874. (while (org-goto-sibling 'previous)
  15875. (org-flag-heading nil))))
  15876. (defun org-show-hidden-entry ()
  15877. "Show an entry where even the heading is hidden."
  15878. (save-excursion
  15879. (org-show-entry)))
  15880. (defun org-flag-heading (flag &optional entry)
  15881. "Flag the current heading. FLAG non-nil means make invisible.
  15882. When ENTRY is non-nil, show the entire entry."
  15883. (save-excursion
  15884. (org-back-to-heading t)
  15885. ;; Check if we should show the entire entry
  15886. (if entry
  15887. (progn
  15888. (org-show-entry)
  15889. (save-excursion
  15890. (and (outline-next-heading)
  15891. (org-flag-heading nil))))
  15892. (outline-flag-region (max (point-min) (1- (point)))
  15893. (save-excursion (outline-end-of-heading) (point))
  15894. flag))))
  15895. (defun org-get-next-sibling ()
  15896. "Move to next heading of the same level, and return point.
  15897. If there is no such heading, return nil.
  15898. This is like outline-next-sibling, but invisible headings are ok."
  15899. (let ((level (funcall outline-level)))
  15900. (outline-next-heading)
  15901. (while (and (not (eobp)) (> (funcall outline-level) level))
  15902. (outline-next-heading))
  15903. (if (or (eobp) (< (funcall outline-level) level))
  15904. nil
  15905. (point))))
  15906. (defun org-get-last-sibling ()
  15907. "Move to previous heading of the same level, and return point.
  15908. If there is no such heading, return nil."
  15909. (let ((opoint (point))
  15910. (level (funcall outline-level)))
  15911. (outline-previous-heading)
  15912. (when (and (/= (point) opoint) (outline-on-heading-p t))
  15913. (while (and (> (funcall outline-level) level)
  15914. (not (bobp)))
  15915. (outline-previous-heading))
  15916. (if (< (funcall outline-level) level)
  15917. nil
  15918. (point)))))
  15919. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  15920. ;; This contains an exact copy of the original function, but it uses
  15921. ;; `org-back-to-heading', to make it work also in invisible
  15922. ;; trees. And is uses an invisible-OK argument.
  15923. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  15924. ;; Furthermore, when used inside Org, finding the end of a large subtree
  15925. ;; with many children and grandchildren etc, this can be much faster
  15926. ;; than the outline version.
  15927. (org-back-to-heading invisible-OK)
  15928. (let ((first t)
  15929. (level (funcall outline-level)))
  15930. (if (and (org-mode-p) (< level 1000))
  15931. ;; A true heading (not a plain list item), in Org-mode
  15932. ;; This means we can easily find the end by looking
  15933. ;; only for the right number of stars. Using a regexp to do
  15934. ;; this is so much faster than using a Lisp loop.
  15935. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  15936. (forward-char 1)
  15937. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  15938. ;; something else, do it the slow way
  15939. (while (and (not (eobp))
  15940. (or first (> (funcall outline-level) level)))
  15941. (setq first nil)
  15942. (outline-next-heading)))
  15943. (unless to-heading
  15944. (if (memq (preceding-char) '(?\n ?\^M))
  15945. (progn
  15946. ;; Go to end of line before heading
  15947. (forward-char -1)
  15948. (if (memq (preceding-char) '(?\n ?\^M))
  15949. ;; leave blank line before heading
  15950. (forward-char -1))))))
  15951. (point))
  15952. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  15953. "Use Org version in org-mode, for dramatic speed-up."
  15954. (if (eq major-mode 'org-mode)
  15955. (progn
  15956. (org-end-of-subtree nil t)
  15957. (unless (eobp) (backward-char 1)))
  15958. ad-do-it))
  15959. (defun org-forward-same-level (arg &optional invisible-ok)
  15960. "Move forward to the arg'th subheading at same level as this one.
  15961. Stop at the first and last subheadings of a superior heading."
  15962. (interactive "p")
  15963. (org-back-to-heading invisible-ok)
  15964. (org-on-heading-p)
  15965. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15966. (re (format "^\\*\\{1,%d\\} " level))
  15967. l)
  15968. (forward-char 1)
  15969. (while (> arg 0)
  15970. (while (and (re-search-forward re nil 'move)
  15971. (setq l (- (match-end 0) (match-beginning 0) 1))
  15972. (= l level)
  15973. (not invisible-ok)
  15974. (org-invisible-p))
  15975. (if (< l level) (setq arg 1)))
  15976. (setq arg (1- arg)))
  15977. (beginning-of-line 1)))
  15978. (defun org-backward-same-level (arg &optional invisible-ok)
  15979. "Move backward to the arg'th subheading at same level as this one.
  15980. Stop at the first and last subheadings of a superior heading."
  15981. (interactive "p")
  15982. (org-back-to-heading)
  15983. (org-on-heading-p)
  15984. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15985. (re (format "^\\*\\{1,%d\\} " level))
  15986. l)
  15987. (while (> arg 0)
  15988. (while (and (re-search-backward re nil 'move)
  15989. (setq l (- (match-end 0) (match-beginning 0) 1))
  15990. (= l level)
  15991. (not invisible-ok)
  15992. (org-invisible-p))
  15993. (if (< l level) (setq arg 1)))
  15994. (setq arg (1- arg)))))
  15995. (defun org-show-subtree ()
  15996. "Show everything after this heading at deeper levels."
  15997. (outline-flag-region
  15998. (point)
  15999. (save-excursion
  16000. (org-end-of-subtree t t))
  16001. nil))
  16002. (defun org-show-entry ()
  16003. "Show the body directly following this heading.
  16004. Show the heading too, if it is currently invisible."
  16005. (interactive)
  16006. (save-excursion
  16007. (condition-case nil
  16008. (progn
  16009. (org-back-to-heading t)
  16010. (outline-flag-region
  16011. (max (point-min) (1- (point)))
  16012. (save-excursion
  16013. (if (re-search-forward
  16014. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  16015. (match-beginning 1)
  16016. (point-max)))
  16017. nil)
  16018. (org-cycle-hide-drawers 'children))
  16019. (error nil))))
  16020. (defun org-make-options-regexp (kwds &optional extra)
  16021. "Make a regular expression for keyword lines."
  16022. (concat
  16023. "^"
  16024. "#?[ \t]*\\+\\("
  16025. (mapconcat 'regexp-quote kwds "\\|")
  16026. (if extra (concat "\\|" extra))
  16027. "\\):[ \t]*"
  16028. "\\(.*\\)"))
  16029. ;; Make isearch reveal the necessary context
  16030. (defun org-isearch-end ()
  16031. "Reveal context after isearch exits."
  16032. (when isearch-success ; only if search was successful
  16033. (if (featurep 'xemacs)
  16034. ;; Under XEmacs, the hook is run in the correct place,
  16035. ;; we directly show the context.
  16036. (org-show-context 'isearch)
  16037. ;; In Emacs the hook runs *before* restoring the overlays.
  16038. ;; So we have to use a one-time post-command-hook to do this.
  16039. ;; (Emacs 22 has a special variable, see function `org-mode')
  16040. (unless (and (boundp 'isearch-mode-end-hook-quit)
  16041. isearch-mode-end-hook-quit)
  16042. ;; Only when the isearch was not quitted.
  16043. (org-add-hook 'post-command-hook 'org-isearch-post-command
  16044. 'append 'local)))))
  16045. (defun org-isearch-post-command ()
  16046. "Remove self from hook, and show context."
  16047. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  16048. (org-show-context 'isearch))
  16049. ;;;; Integration with and fixes for other packages
  16050. ;;; Imenu support
  16051. (defvar org-imenu-markers nil
  16052. "All markers currently used by Imenu.")
  16053. (make-variable-buffer-local 'org-imenu-markers)
  16054. (defun org-imenu-new-marker (&optional pos)
  16055. "Return a new marker for use by Imenu, and remember the marker."
  16056. (let ((m (make-marker)))
  16057. (move-marker m (or pos (point)))
  16058. (push m org-imenu-markers)
  16059. m))
  16060. (defun org-imenu-get-tree ()
  16061. "Produce the index for Imenu."
  16062. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  16063. (setq org-imenu-markers nil)
  16064. (let* ((n org-imenu-depth)
  16065. (re (concat "^" outline-regexp))
  16066. (subs (make-vector (1+ n) nil))
  16067. (last-level 0)
  16068. m level head)
  16069. (save-excursion
  16070. (save-restriction
  16071. (widen)
  16072. (goto-char (point-max))
  16073. (while (re-search-backward re nil t)
  16074. (setq level (org-reduced-level (funcall outline-level)))
  16075. (when (<= level n)
  16076. (looking-at org-complex-heading-regexp)
  16077. (setq head (org-link-display-format
  16078. (org-match-string-no-properties 4))
  16079. m (org-imenu-new-marker))
  16080. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  16081. (if (>= level last-level)
  16082. (push (cons head m) (aref subs level))
  16083. (push (cons head (aref subs (1+ level))) (aref subs level))
  16084. (loop for i from (1+ level) to n do (aset subs i nil)))
  16085. (setq last-level level)))))
  16086. (aref subs 1)))
  16087. (eval-after-load "imenu"
  16088. '(progn
  16089. (add-hook 'imenu-after-jump-hook
  16090. (lambda ()
  16091. (if (eq major-mode 'org-mode)
  16092. (org-show-context 'org-goto))))))
  16093. (defun org-link-display-format (link)
  16094. "Replace a link with either the description, or the link target
  16095. if no description is present"
  16096. (save-match-data
  16097. (if (string-match org-bracket-link-analytic-regexp link)
  16098. (replace-match (or (match-string 5 link)
  16099. (concat (match-string 1 link)
  16100. (match-string 3 link)))
  16101. nil nil link)
  16102. link)))
  16103. ;; Speedbar support
  16104. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  16105. "Overlay marking the agenda restriction line in speedbar.")
  16106. (org-overlay-put org-speedbar-restriction-lock-overlay
  16107. 'face 'org-agenda-restriction-lock)
  16108. (org-overlay-put org-speedbar-restriction-lock-overlay
  16109. 'help-echo "Agendas are currently limited to this item.")
  16110. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  16111. (defun org-speedbar-set-agenda-restriction ()
  16112. "Restrict future agenda commands to the location at point in speedbar.
  16113. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  16114. (interactive)
  16115. (require 'org-agenda)
  16116. (let (p m tp np dir txt)
  16117. (cond
  16118. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16119. 'org-imenu t))
  16120. (setq m (get-text-property p 'org-imenu-marker))
  16121. (with-current-buffer (marker-buffer m)
  16122. (goto-char m)
  16123. (org-agenda-set-restriction-lock 'subtree)))
  16124. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16125. 'speedbar-function 'speedbar-find-file))
  16126. (setq tp (previous-single-property-change
  16127. (1+ p) 'speedbar-function)
  16128. np (next-single-property-change
  16129. tp 'speedbar-function)
  16130. dir (speedbar-line-directory)
  16131. txt (buffer-substring-no-properties (or tp (point-min))
  16132. (or np (point-max))))
  16133. (with-current-buffer (find-file-noselect
  16134. (let ((default-directory dir))
  16135. (expand-file-name txt)))
  16136. (unless (org-mode-p)
  16137. (error "Cannot restrict to non-Org-mode file"))
  16138. (org-agenda-set-restriction-lock 'file)))
  16139. (t (error "Don't know how to restrict Org-mode's agenda")))
  16140. (org-move-overlay org-speedbar-restriction-lock-overlay
  16141. (point-at-bol) (point-at-eol))
  16142. (setq current-prefix-arg nil)
  16143. (org-agenda-maybe-redo)))
  16144. (eval-after-load "speedbar"
  16145. '(progn
  16146. (speedbar-add-supported-extension ".org")
  16147. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  16148. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  16149. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  16150. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16151. (add-hook 'speedbar-visiting-tag-hook
  16152. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  16153. ;;; Fixes and Hacks for problems with other packages
  16154. ;; Make flyspell not check words in links, to not mess up our keymap
  16155. (defun org-mode-flyspell-verify ()
  16156. "Don't let flyspell put overlays at active buttons."
  16157. (and (not (get-text-property (point) 'keymap))
  16158. (not (get-text-property (point) 'org-no-flyspell))))
  16159. (defun org-remove-flyspell-overlays-in (beg end)
  16160. "Remove flyspell overlays in region."
  16161. (and (org-bound-and-true-p flyspell-mode)
  16162. (fboundp 'flyspell-delete-region-overlays)
  16163. (flyspell-delete-region-overlays beg end))
  16164. (add-text-properties beg end '(org-no-flyspell t)))
  16165. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  16166. (eval-after-load "bookmark"
  16167. '(if (boundp 'bookmark-after-jump-hook)
  16168. ;; We can use the hook
  16169. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  16170. ;; Hook not available, use advice
  16171. (defadvice bookmark-jump (after org-make-visible activate)
  16172. "Make the position visible."
  16173. (org-bookmark-jump-unhide))))
  16174. ;; Make sure saveplace shows the location if it was hidden
  16175. (eval-after-load "saveplace"
  16176. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  16177. "Make the position visible."
  16178. (org-bookmark-jump-unhide)))
  16179. ;; Make sure ecb shows the location if it was hidden
  16180. (eval-after-load "ecb"
  16181. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  16182. "Make hierarchy visible when jumping into location from ECB tree buffer."
  16183. (if (eq major-mode 'org-mode)
  16184. (org-show-context))))
  16185. (defun org-bookmark-jump-unhide ()
  16186. "Unhide the current position, to show the bookmark location."
  16187. (and (org-mode-p)
  16188. (or (org-invisible-p)
  16189. (save-excursion (goto-char (max (point-min) (1- (point))))
  16190. (org-invisible-p)))
  16191. (org-show-context 'bookmark-jump)))
  16192. ;; Make session.el ignore our circular variable
  16193. (eval-after-load "session"
  16194. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  16195. ;;;; Experimental code
  16196. (defun org-closed-in-range ()
  16197. "Sparse tree of items closed in a certain time range.
  16198. Still experimental, may disappear in the future."
  16199. (interactive)
  16200. ;; Get the time interval from the user.
  16201. (let* ((time1 (org-float-time
  16202. (org-read-date nil 'to-time nil "Starting date: ")))
  16203. (time2 (org-float-time
  16204. (org-read-date nil 'to-time nil "End date:")))
  16205. ;; callback function
  16206. (callback (lambda ()
  16207. (let ((time
  16208. (org-float-time
  16209. (apply 'encode-time
  16210. (org-parse-time-string
  16211. (match-string 1))))))
  16212. ;; check if time in interval
  16213. (and (>= time time1) (<= time time2))))))
  16214. ;; make tree, check each match with the callback
  16215. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  16216. ;;;; Finish up
  16217. (provide 'org)
  16218. (run-hooks 'org-load-hook)
  16219. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  16220. ;;; org.el ends here