org.el 687 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424
  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.34trans
  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.34trans"
  90. "The version number of the file org.el.")
  91. (defun org-version (&optional here)
  92. "Show the org-mode version in the echo area.
  93. With prefix arg HERE, insert it at point."
  94. (interactive "P")
  95. (let* ((origin default-directory)
  96. (version org-version)
  97. (git-version)
  98. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  99. (when (and (file-exists-p (expand-file-name ".git" dir))
  100. (executable-find "git"))
  101. (unwind-protect
  102. (progn
  103. (cd dir)
  104. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  105. (with-current-buffer "*Shell Command Output*"
  106. (goto-char (point-min))
  107. (setq git-version (buffer-substring (point) (point-at-eol))))
  108. (subst-char-in-string ?- ?. git-version t)
  109. (when (string-match "\\S-"
  110. (shell-command-to-string
  111. "git diff-index --name-only HEAD --"))
  112. (setq git-version (concat git-version ".dirty")))
  113. (setq version (concat version " (" git-version ")"))))
  114. (cd origin)))
  115. (setq version (format "Org-mode version %s" version))
  116. (if here (insert version))
  117. (message version)))
  118. ;;; Compatibility constants
  119. ;;; The custom variables
  120. (defgroup org nil
  121. "Outline-based notes management and organizer."
  122. :tag "Org"
  123. :group 'outlines
  124. :group '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-docview 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 " ctags: Access to Emacs tags with links" org-ctags)
  170. (const :tag " docview: Links to doc-view buffers" org-docview)
  171. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  172. (const :tag " id: Global IDs for identifying entries" org-id)
  173. (const :tag " info: Links to Info nodes" org-info)
  174. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  175. (const :tag " habit: Track your consistency with habits" org-habit)
  176. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  177. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  178. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  179. (const :tag " mew Links to Mew folders/messages" org-mew)
  180. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  181. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  182. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  183. (const :tag " vm: Links to VM folders/messages" org-vm)
  184. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  185. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  186. (const :tag " mouse: Additional mouse support" org-mouse)
  187. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  188. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  189. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  190. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  191. (const :tag "C collector: Collect properties into tables" org-collector)
  192. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  193. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  194. (const :tag "C eval: Include command output as text" org-eval)
  195. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  196. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  197. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  198. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  199. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  200. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  201. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  202. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  203. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  204. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  205. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  206. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  207. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  208. (const :tag "C R: Computation using the R language" org-R)
  209. (const :tag "C registry: A registry for Org-mode links" org-registry)
  210. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  211. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  212. (const :tag "C secretary: Team management with org-mode" org-secretary)
  213. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  214. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  215. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  216. (const :tag "C track: Keep up with Org-mode development" org-track)
  217. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  218. (defcustom org-support-shift-select nil
  219. "Non-nil means make shift-cursor commands select text when possible.
  220. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  221. selecting a region, or enlarge thusly regions started in this way.
  222. In Org-mode, in special contexts, these same keys are used for other
  223. purposes, important enough to compete with shift selection. Org tries
  224. to balance these needs by supporting `shift-select-mode' outside these
  225. special contexts, under control of this variable.
  226. The default of this variable is nil, to avoid confusing behavior. Shifted
  227. cursor keys will then execute Org commands in the following contexts:
  228. - on a headline, changing TODO state (left/right) and priority (up/down)
  229. - on a time stamp, changing the time
  230. - in a plain list item, changing the bullet type
  231. - in a property definition line, switching between allowed values
  232. - in the BEGIN line of a clock table (changing the time block).
  233. Outside these contexts, the commands will throw an error.
  234. When this variable is t and the cursor is not in a special context,
  235. Org-mode will support shift-selection for making and enlarging regions.
  236. To make this more effective, the bullet cycling will no longer happen
  237. anywhere in an item line, but only if the cursor is exactly on the bullet.
  238. If you set this variable to the symbol `always', then the keys
  239. will not be special in headlines, property lines, and item lines, to make
  240. shift selection work there as well. If this is what you want, you can
  241. use the following alternative commands: `C-c C-t' and `C-c ,' to
  242. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  243. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  244. edited by hand or in column view.
  245. However, when the cursor is on a timestamp, shift-cursor commands
  246. will still edit the time stamp - this is just too good to give up.
  247. XEmacs user should have this variable set to nil, because shift-select-mode
  248. is Emacs 23 only."
  249. :group 'org
  250. :type '(choice
  251. (const :tag "Never" nil)
  252. (const :tag "When outside special context" t)
  253. (const :tag "Everywhere except timestamps" always)))
  254. (defgroup org-startup nil
  255. "Options concerning startup of Org-mode."
  256. :tag "Org Startup"
  257. :group 'org)
  258. (defcustom org-startup-folded t
  259. "Non-nil means entering Org-mode will switch to OVERVIEW.
  260. This can also be configured on a per-file basis by adding one of
  261. the following lines anywhere in the buffer:
  262. #+STARTUP: fold (or `overview', this is equivalent)
  263. #+STARTUP: nofold (or `showall', this is equivalent)
  264. #+STARTUP: content
  265. #+STARTUP: showeverything"
  266. :group 'org-startup
  267. :type '(choice
  268. (const :tag "nofold: show all" nil)
  269. (const :tag "fold: overview" t)
  270. (const :tag "content: all headlines" content)
  271. (const :tag "show everything, even drawers" showeverything)))
  272. (defcustom org-startup-truncated t
  273. "Non-nil means entering Org-mode will set `truncate-lines'.
  274. This is useful since some lines containing links can be very long and
  275. uninteresting. Also tables look terrible when wrapped."
  276. :group 'org-startup
  277. :type 'boolean)
  278. (defcustom org-startup-indented nil
  279. "Non-nil means turn on `org-indent-mode' on startup.
  280. This can also be configured on a per-file basis by adding one of
  281. the following lines anywhere in the buffer:
  282. #+STARTUP: indent
  283. #+STARTUP: noindent"
  284. :group 'org-structure
  285. :type '(choice
  286. (const :tag "Not" nil)
  287. (const :tag "Globally (slow on startup in large files)" t)))
  288. (defcustom org-startup-with-beamer-mode nil
  289. "Non-nil means turn on `org-beamer-mode' on startup.
  290. This can also be configured on a per-file basis by adding one of
  291. the following lines anywhere in the buffer:
  292. #+STARTUP: beamer"
  293. :group 'org-startup
  294. :type 'boolean)
  295. (defcustom org-startup-align-all-tables nil
  296. "Non-nil means align all tables when visiting a file.
  297. This is useful when the column width in tables is forced with <N> cookies
  298. in table fields. Such tables will look correct only after the first re-align.
  299. This can also be configured on a per-file basis by adding one of
  300. the following lines anywhere in the buffer:
  301. #+STARTUP: align
  302. #+STARTUP: noalign"
  303. :group 'org-startup
  304. :type 'boolean)
  305. (defcustom org-insert-mode-line-in-empty-file nil
  306. "Non-nil means insert the first line setting Org-mode in empty files.
  307. When the function `org-mode' is called interactively in an empty file, this
  308. normally means that the file name does not automatically trigger Org-mode.
  309. To ensure that the file will always be in Org-mode in the future, a
  310. line enforcing Org-mode will be inserted into the buffer, if this option
  311. has been set."
  312. :group 'org-startup
  313. :type 'boolean)
  314. (defcustom org-replace-disputed-keys nil
  315. "Non-nil means use alternative key bindings for some keys.
  316. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  317. These keys are also used by other packages like shift-selection-mode'
  318. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  319. If you want to use Org-mode together with one of these other modes,
  320. or more generally if you would like to move some Org-mode commands to
  321. other keys, set this variable and configure the keys with the variable
  322. `org-disputed-keys'.
  323. This option is only relevant at load-time of Org-mode, and must be set
  324. *before* org.el is loaded. Changing it requires a restart of Emacs to
  325. become effective."
  326. :group 'org-startup
  327. :type 'boolean)
  328. (defcustom org-use-extra-keys nil
  329. "Non-nil means use extra key sequence definitions for certain
  330. commands. This happens automatically if you run XEmacs or if
  331. window-system is nil. This variable lets you do the same
  332. manually. You must set it before loading org.
  333. Example: on Carbon Emacs 22 running graphically, with an external
  334. keyboard on a Powerbook, the default way of setting M-left might
  335. not work for either Alt or ESC. Setting this variable will make
  336. it work for ESC."
  337. :group 'org-startup
  338. :type 'boolean)
  339. (if (fboundp 'defvaralias)
  340. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  341. (defcustom org-disputed-keys
  342. '(([(shift up)] . [(meta p)])
  343. ([(shift down)] . [(meta n)])
  344. ([(shift left)] . [(meta -)])
  345. ([(shift right)] . [(meta +)])
  346. ([(control shift right)] . [(meta shift +)])
  347. ([(control shift left)] . [(meta shift -)]))
  348. "Keys for which Org-mode and other modes compete.
  349. This is an alist, cars are the default keys, second element specifies
  350. the alternative to use when `org-replace-disputed-keys' is t.
  351. Keys can be specified in any syntax supported by `define-key'.
  352. The value of this option takes effect only at Org-mode's startup,
  353. therefore you'll have to restart Emacs to apply it after changing."
  354. :group 'org-startup
  355. :type 'alist)
  356. (defun org-key (key)
  357. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  358. Or return the original if not disputed."
  359. (if org-replace-disputed-keys
  360. (let* ((nkey (key-description key))
  361. (x (org-find-if (lambda (x)
  362. (equal (key-description (car x)) nkey))
  363. org-disputed-keys)))
  364. (if x (cdr x) key))
  365. key))
  366. (defun org-find-if (predicate seq)
  367. (catch 'exit
  368. (while seq
  369. (if (funcall predicate (car seq))
  370. (throw 'exit (car seq))
  371. (pop seq)))))
  372. (defun org-defkey (keymap key def)
  373. "Define a key, possibly translated, as returned by `org-key'."
  374. (define-key keymap (org-key key) def))
  375. (defcustom org-ellipsis nil
  376. "The ellipsis to use in the Org-mode outline.
  377. When nil, just use the standard three dots. When a string, use that instead,
  378. When a face, use the standard 3 dots, but with the specified face.
  379. The change affects only Org-mode (which will then use its own display table).
  380. Changing this requires executing `M-x org-mode' in a buffer to become
  381. effective."
  382. :group 'org-startup
  383. :type '(choice (const :tag "Default" nil)
  384. (face :tag "Face" :value org-warning)
  385. (string :tag "String" :value "...#")))
  386. (defvar org-display-table nil
  387. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  388. (defgroup org-keywords nil
  389. "Keywords in Org-mode."
  390. :tag "Org Keywords"
  391. :group 'org)
  392. (defcustom org-deadline-string "DEADLINE:"
  393. "String to mark deadline entries.
  394. A deadline is this string, followed by a time stamp. Should be a word,
  395. terminated by a colon. You can insert a schedule keyword and
  396. a timestamp with \\[org-deadline].
  397. Changes become only effective after restarting Emacs."
  398. :group 'org-keywords
  399. :type 'string)
  400. (defcustom org-scheduled-string "SCHEDULED:"
  401. "String to mark scheduled TODO entries.
  402. A schedule is this string, followed by a time stamp. Should be a word,
  403. terminated by a colon. You can insert a schedule keyword and
  404. a timestamp with \\[org-schedule].
  405. Changes become only effective after restarting Emacs."
  406. :group 'org-keywords
  407. :type 'string)
  408. (defcustom org-closed-string "CLOSED:"
  409. "String used as the prefix for timestamps logging closing a TODO entry."
  410. :group 'org-keywords
  411. :type 'string)
  412. (defcustom org-clock-string "CLOCK:"
  413. "String used as prefix for timestamps clocking work hours on an item."
  414. :group 'org-keywords
  415. :type 'string)
  416. (defcustom org-comment-string "COMMENT"
  417. "Entries starting with this keyword will never be exported.
  418. An entry can be toggled between COMMENT and normal with
  419. \\[org-toggle-comment].
  420. Changes become only effective after restarting Emacs."
  421. :group 'org-keywords
  422. :type 'string)
  423. (defcustom org-quote-string "QUOTE"
  424. "Entries starting with this keyword will be exported in fixed-width font.
  425. Quoting applies only to the text in the entry following the headline, and does
  426. not extend beyond the next headline, even if that is lower level.
  427. An entry can be toggled between QUOTE and normal with
  428. \\[org-toggle-fixed-width-section]."
  429. :group 'org-keywords
  430. :type 'string)
  431. (defconst org-repeat-re
  432. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  433. "Regular expression for specifying repeated events.
  434. After a match, group 1 contains the repeat expression.")
  435. (defgroup org-structure nil
  436. "Options concerning the general structure of Org-mode files."
  437. :tag "Org Structure"
  438. :group 'org)
  439. (defgroup org-reveal-location nil
  440. "Options about how to make context of a location visible."
  441. :tag "Org Reveal Location"
  442. :group 'org-structure)
  443. (defconst org-context-choice
  444. '(choice
  445. (const :tag "Always" t)
  446. (const :tag "Never" nil)
  447. (repeat :greedy t :tag "Individual contexts"
  448. (cons
  449. (choice :tag "Context"
  450. (const agenda)
  451. (const org-goto)
  452. (const occur-tree)
  453. (const tags-tree)
  454. (const link-search)
  455. (const mark-goto)
  456. (const bookmark-jump)
  457. (const isearch)
  458. (const default))
  459. (boolean))))
  460. "Contexts for the reveal options.")
  461. (defcustom org-show-hierarchy-above '((default . t))
  462. "Non-nil means show full hierarchy when revealing a location.
  463. Org-mode often shows locations in an org-mode file which might have
  464. been invisible before. When this is set, the hierarchy of headings
  465. above the exposed location is shown.
  466. Turning this off for example for sparse trees makes them very compact.
  467. Instead of t, this can also be an alist specifying this option for different
  468. contexts. Valid contexts are
  469. agenda when exposing an entry from the agenda
  470. org-goto when using the command `org-goto' on key C-c C-j
  471. occur-tree when using the command `org-occur' on key C-c /
  472. tags-tree when constructing a sparse tree based on tags matches
  473. link-search when exposing search matches associated with a link
  474. mark-goto when exposing the jump goal of a mark
  475. bookmark-jump when exposing a bookmark location
  476. isearch when exiting from an incremental search
  477. default default for all contexts not set explicitly"
  478. :group 'org-reveal-location
  479. :type org-context-choice)
  480. (defcustom org-show-following-heading '((default . nil))
  481. "Non-nil means show following heading when revealing a location.
  482. Org-mode often shows locations in an org-mode file which might have
  483. been invisible before. When this is set, the heading following the
  484. match is shown.
  485. Turning this off for example for sparse trees makes them very compact,
  486. but makes it harder to edit the location of the match. In such a case,
  487. use the command \\[org-reveal] to show more context.
  488. Instead of t, this can also be an alist specifying this option for different
  489. contexts. See `org-show-hierarchy-above' for valid contexts."
  490. :group 'org-reveal-location
  491. :type org-context-choice)
  492. (defcustom org-show-siblings '((default . nil) (isearch t))
  493. "Non-nil means show all sibling heading when revealing a location.
  494. Org-mode often shows locations in an org-mode file which might have
  495. been invisible before. When this is set, the sibling of the current entry
  496. heading are all made visible. If `org-show-hierarchy-above' is t,
  497. the same happens on each level of the hierarchy above the current entry.
  498. By default this is on for the isearch context, off for all other contexts.
  499. Turning this off for example for sparse trees makes them very compact,
  500. but makes it harder to edit the location of the match. In such a case,
  501. use the command \\[org-reveal] to show more context.
  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-show-entry-below '((default . nil))
  507. "Non-nil means show the entry below a headline when revealing a location.
  508. Org-mode often shows locations in an org-mode file which might have
  509. been invisible before. When this is set, the text below the headline that is
  510. exposed is also shown.
  511. By default this is off for all contexts.
  512. Instead of t, this can also be an alist specifying this option for different
  513. contexts. See `org-show-hierarchy-above' for valid contexts."
  514. :group 'org-reveal-location
  515. :type org-context-choice)
  516. (defcustom org-indirect-buffer-display 'other-window
  517. "How should indirect tree buffers be displayed?
  518. This applies to indirect buffers created with the commands
  519. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  520. Valid values are:
  521. current-window Display in the current window
  522. other-window Just display in another window.
  523. dedicated-frame Create one new frame, and re-use it each time.
  524. new-frame Make a new frame each time. Note that in this case
  525. previously-made indirect buffers are kept, and you need to
  526. kill these buffers yourself."
  527. :group 'org-structure
  528. :group 'org-agenda-windows
  529. :type '(choice
  530. (const :tag "In current window" current-window)
  531. (const :tag "In current frame, other window" other-window)
  532. (const :tag "Each time a new frame" new-frame)
  533. (const :tag "One dedicated frame" dedicated-frame)))
  534. (defcustom org-use-speed-commands nil
  535. "Non-nil means activate single letter commands at beginning of a headline.
  536. This may also be a function to test for appropriate locations where speed
  537. commands should be active."
  538. :group 'org-structure
  539. :type '(choice
  540. (const :tag "Never" nil)
  541. (const :tag "At beginning of headline stars" t)
  542. (function)))
  543. (defcustom org-speed-commands-user nil
  544. "Alist of additional speed commands.
  545. This list will be checked before `org-speed-commands-default'
  546. when the variable `org-use-speed-commands' is non-nil
  547. and when the cursor is at the beginning of a headline.
  548. The car if each entry is a string with a single letter, which must
  549. be assigned to `self-insert-command' in the global map.
  550. The cdr is either a command to be called interactively, a function
  551. to be called, or a form to be evaluated.
  552. An entry that is just a list with a single string will be interpreted
  553. as a descriptive headline that will be added when listing the speed
  554. copmmands in the Help buffer using the `?' speed command."
  555. :group 'org-structure
  556. :type '(repeat :value ("k" . ignore)
  557. (choice :value ("k" . ignore)
  558. (list :tag "Descriptive Headline" (string :tag "Headline"))
  559. (cons :tag "Letter and Command"
  560. (string :tag "Command letter")
  561. (choice
  562. (function)
  563. (sexp))))))
  564. (defgroup org-cycle nil
  565. "Options concerning visibility cycling in Org-mode."
  566. :tag "Org Cycle"
  567. :group 'org-structure)
  568. (defcustom org-cycle-skip-children-state-if-no-children t
  569. "Non-nil means skip CHILDREN state in entries that don't have any."
  570. :group 'org-cycle
  571. :type 'boolean)
  572. (defcustom org-cycle-max-level nil
  573. "Maximum level which should still be subject to visibility cycling.
  574. Levels higher than this will, for cycling, be treated as text, not a headline.
  575. When `org-odd-levels-only' is set, a value of N in this variable actually
  576. means 2N-1 stars as the limiting headline.
  577. When nil, cycle all levels.
  578. Note that the limiting level of cycling is also influenced by
  579. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  580. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  581. than its value."
  582. :group 'org-cycle
  583. :type '(choice
  584. (const :tag "No limit" nil)
  585. (integer :tag "Maximum level")))
  586. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  587. "Names of drawers. Drawers are not opened by cycling on the headline above.
  588. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  589. this:
  590. :DRAWERNAME:
  591. .....
  592. :END:
  593. The drawer \"PROPERTIES\" is special for capturing properties through
  594. the property API.
  595. Drawers can be defined on the per-file basis with a line like:
  596. #+DRAWERS: HIDDEN STATE PROPERTIES"
  597. :group 'org-structure
  598. :group 'org-cycle
  599. :type '(repeat (string :tag "Drawer Name")))
  600. (defcustom org-hide-block-startup nil
  601. "Non-nil means entering Org-mode will fold all blocks.
  602. This can also be set in on a per-file basis with
  603. #+STARTUP: hideblocks
  604. #+STARTUP: showblocks"
  605. :group 'org-startup
  606. :group 'org-cycle
  607. :type 'boolean)
  608. (defcustom org-cycle-global-at-bob nil
  609. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  610. This makes it possible to do global cycling without having to use S-TAB or
  611. C-u TAB. For this special case to work, the first line of the buffer
  612. must not be a headline - it may be empty or some other text. When used in
  613. this way, `org-cycle-hook' is disables temporarily, to make sure the
  614. cursor stays at the beginning of the buffer.
  615. When this option is nil, don't do anything special at the beginning
  616. of the buffer."
  617. :group 'org-cycle
  618. :type 'boolean)
  619. (defcustom org-cycle-level-after-item/entry-creation t
  620. "Non-nil means cycle entry level or item indentation in new empty entries.
  621. When the cursor is at the end of an empty headline, i.e with only stars
  622. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  623. and then all possible anchestor states, before returning to the original state.
  624. This makes data entry extremely fast: M-RET to create a new headline,
  625. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  626. When the cursor is at the end of an empty plain list item, one TAB will
  627. make it a subitem, two or more tabs will back up to make this an item
  628. higher up in the item hierarchy."
  629. :group 'org-cycle
  630. :type 'boolean)
  631. (defcustom org-cycle-emulate-tab t
  632. "Where should `org-cycle' emulate TAB.
  633. nil Never
  634. white Only in completely white lines
  635. whitestart Only at the beginning of lines, before the first non-white char
  636. t Everywhere except in headlines
  637. exc-hl-bol Everywhere except at the start of a headline
  638. If TAB is used in a place where it does not emulate TAB, the current subtree
  639. visibility is cycled."
  640. :group 'org-cycle
  641. :type '(choice (const :tag "Never" nil)
  642. (const :tag "Only in completely white lines" white)
  643. (const :tag "Before first char in a line" whitestart)
  644. (const :tag "Everywhere except in headlines" t)
  645. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  646. ))
  647. (defcustom org-cycle-separator-lines 2
  648. "Number of empty lines needed to keep an empty line between collapsed trees.
  649. If you leave an empty line between the end of a subtree and the following
  650. headline, this empty line is hidden when the subtree is folded.
  651. Org-mode will leave (exactly) one empty line visible if the number of
  652. empty lines is equal or larger to the number given in this variable.
  653. So the default 2 means at least 2 empty lines after the end of a subtree
  654. are needed to produce free space between a collapsed subtree and the
  655. following headline.
  656. If the number is negative, and the number of empty lines is at least -N,
  657. all empty lines are shown.
  658. Special case: when 0, never leave empty lines in collapsed view."
  659. :group 'org-cycle
  660. :type 'integer)
  661. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  662. (defcustom org-pre-cycle-hook nil
  663. "Hook that is run before visibility cycling is happening.
  664. The function(s) in this hook must accept a single argument which indicates
  665. the new state that will be set right after running this hook. The
  666. argument is a symbol. Before a global state change, it can have the values
  667. `overview', `content', or `all'. Before a local state change, it can have
  668. the values `folded', `children', or `subtree'."
  669. :group 'org-cycle
  670. :type 'hook)
  671. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  672. org-cycle-hide-drawers
  673. org-cycle-show-empty-lines
  674. org-optimize-window-after-visibility-change)
  675. "Hook that is run after `org-cycle' has changed the buffer visibility.
  676. The function(s) in this hook must accept a single argument which indicates
  677. the new state that was set by the most recent `org-cycle' command. The
  678. argument is a symbol. After a global state change, it can have the values
  679. `overview', `content', or `all'. After a local state change, it can have
  680. the values `folded', `children', or `subtree'."
  681. :group 'org-cycle
  682. :type 'hook)
  683. (defgroup org-edit-structure nil
  684. "Options concerning structure editing in Org-mode."
  685. :tag "Org Edit Structure"
  686. :group 'org-structure)
  687. (defcustom org-odd-levels-only nil
  688. "Non-nil means skip even levels and only use odd levels for the outline.
  689. This has the effect that two stars are being added/taken away in
  690. promotion/demotion commands. It also influences how levels are
  691. handled by the exporters.
  692. Changing it requires restart of `font-lock-mode' to become effective
  693. for fontification also in regions already fontified.
  694. You may also set this on a per-file basis by adding one of the following
  695. lines to the buffer:
  696. #+STARTUP: odd
  697. #+STARTUP: oddeven"
  698. :group 'org-edit-structure
  699. :group 'org-font-lock
  700. :type 'boolean)
  701. (defcustom org-adapt-indentation t
  702. "Non-nil means adapt indentation to outline node level.
  703. When this variable is set, Org assumes that you write outlines by
  704. indenting text in each node to align with the headline (after the stars).
  705. The following issues are influenced by this variable:
  706. - When this is set and the *entire* text in an entry is indented, the
  707. indentation is increased by one space in a demotion command, and
  708. decreased by one in a promotion command. If any line in the entry
  709. body starts with text at column 0, indentation is not changed at all.
  710. - Property drawers and planning information is inserted indented when
  711. this variable s set. When nil, they will not be indented.
  712. - TAB indents a line relative to context. The lines below a headline
  713. will be indented when this variable is set.
  714. Note that this is all about true indentation, by adding and removing
  715. space characters. See also `org-indent.el' which does level-dependent
  716. indentation in a virtual way, i.e. at display time in Emacs."
  717. :group 'org-edit-structure
  718. :type 'boolean)
  719. (defcustom org-special-ctrl-a/e nil
  720. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  721. When t, `C-a' will bring back the cursor to the beginning of the
  722. headline text, i.e. after the stars and after a possible TODO keyword.
  723. In an item, this will be the position after the bullet.
  724. When the cursor is already at that position, another `C-a' will bring
  725. it to the beginning of the line.
  726. `C-e' will jump to the end of the headline, ignoring the presence of tags
  727. in the headline. A second `C-e' will then jump to the true end of the
  728. line, after any tags. This also means that, when this variable is
  729. non-nil, `C-e' also will never jump beyond the end of the heading of a
  730. folded section, i.e. not after the ellipses.
  731. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  732. going to the true line boundary first. Only a directly following, identical
  733. keypress will bring the cursor to the special positions.
  734. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  735. set separately."
  736. :group 'org-edit-structure
  737. :type '(choice
  738. (const :tag "off" nil)
  739. (const :tag "on: after stars/bullet and before tags first" t)
  740. (const :tag "reversed: true line boundary first" reversed)
  741. (cons :tag "Set C-a and C-e separately"
  742. (choice :tag "Special C-a"
  743. (const :tag "off" nil)
  744. (const :tag "on: after stars/bullet first" t)
  745. (const :tag "reversed: before stars/bullet first" reversed))
  746. (choice :tag "Special C-e"
  747. (const :tag "off" nil)
  748. (const :tag "on: before tags first" t)
  749. (const :tag "reversed: after tags first" reversed)))))
  750. (if (fboundp 'defvaralias)
  751. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  752. (defcustom org-special-ctrl-k nil
  753. "Non-nil means `C-k' will behave specially in headlines.
  754. When nil, `C-k' will call the default `kill-line' command.
  755. When t, the following will happen while the cursor is in the headline:
  756. - When the cursor is at the beginning of a headline, kill the entire
  757. line and possible the folded subtree below the line.
  758. - When in the middle of the headline text, kill the headline up to the tags.
  759. - When after the headline text, kill the tags."
  760. :group 'org-edit-structure
  761. :type 'boolean)
  762. (defcustom org-yank-folded-subtrees t
  763. "Non-nil means when yanking subtrees, fold them.
  764. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  765. it starts with a heading and all other headings in it are either children
  766. or siblings, then fold all the subtrees. However, do this only if no
  767. text after the yank would be swallowed into a folded tree by this action."
  768. :group 'org-edit-structure
  769. :type 'boolean)
  770. (defcustom org-yank-adjusted-subtrees nil
  771. "Non-nil means when yanking subtrees, adjust the level.
  772. With this setting, `org-paste-subtree' is used to insert the subtree, see
  773. this function for details."
  774. :group 'org-edit-structure
  775. :type 'boolean)
  776. (defcustom org-M-RET-may-split-line '((default . t))
  777. "Non-nil means M-RET will split the line at the cursor position.
  778. When nil, it will go to the end of the line before making a
  779. new line.
  780. You may also set this option in a different way for different
  781. contexts. Valid contexts are:
  782. headline when creating a new headline
  783. item when creating a new item
  784. table in a table field
  785. default the value to be used for all contexts not explicitly
  786. customized"
  787. :group 'org-structure
  788. :group 'org-table
  789. :type '(choice
  790. (const :tag "Always" t)
  791. (const :tag "Never" nil)
  792. (repeat :greedy t :tag "Individual contexts"
  793. (cons
  794. (choice :tag "Context"
  795. (const headline)
  796. (const item)
  797. (const table)
  798. (const default))
  799. (boolean)))))
  800. (defcustom org-insert-heading-respect-content nil
  801. "Non-nil means insert new headings after the current subtree.
  802. When nil, the new heading is created directly after the current line.
  803. The commands \\[org-insert-heading-respect-content] and
  804. \\[org-insert-todo-heading-respect-content] turn this variable on
  805. for the duration of the command."
  806. :group 'org-structure
  807. :type 'boolean)
  808. (defcustom org-blank-before-new-entry '((heading . auto)
  809. (plain-list-item . auto))
  810. "Should `org-insert-heading' leave a blank line before new heading/item?
  811. The value is an alist, with `heading' and `plain-list-item' as car,
  812. and a boolean flag as cdr. For plain lists, if the variable
  813. `org-empty-line-terminates-plain-lists' is set, the setting here
  814. is ignored and no empty line is inserted, to keep the list in tact."
  815. :group 'org-edit-structure
  816. :type '(list
  817. (cons (const heading)
  818. (choice (const :tag "Never" nil)
  819. (const :tag "Always" t)
  820. (const :tag "Auto" auto)))
  821. (cons (const plain-list-item)
  822. (choice (const :tag "Never" nil)
  823. (const :tag "Always" t)
  824. (const :tag "Auto" auto)))))
  825. (defcustom org-insert-heading-hook nil
  826. "Hook being run after inserting a new heading."
  827. :group 'org-edit-structure
  828. :type 'hook)
  829. (defcustom org-enable-fixed-width-editor t
  830. "Non-nil means lines starting with \":\" are treated as fixed-width.
  831. This currently only means they are never auto-wrapped.
  832. When nil, such lines will be treated like ordinary lines.
  833. See also the QUOTE keyword."
  834. :group 'org-edit-structure
  835. :type 'boolean)
  836. (defcustom org-goto-auto-isearch t
  837. "Non-nil means typing characters in org-goto starts incremental search."
  838. :group 'org-edit-structure
  839. :type 'boolean)
  840. (defgroup org-sparse-trees nil
  841. "Options concerning sparse trees in Org-mode."
  842. :tag "Org Sparse Trees"
  843. :group 'org-structure)
  844. (defcustom org-highlight-sparse-tree-matches t
  845. "Non-nil means highlight all matches that define a sparse tree.
  846. The highlights will automatically disappear the next time the buffer is
  847. changed by an edit command."
  848. :group 'org-sparse-trees
  849. :type 'boolean)
  850. (defcustom org-remove-highlights-with-change t
  851. "Non-nil means any change to the buffer will remove temporary highlights.
  852. Such highlights are created by `org-occur' and `org-clock-display'.
  853. When nil, `C-c C-c needs to be used to get rid of the highlights.
  854. The highlights created by `org-preview-latex-fragment' always need
  855. `C-c C-c' to be removed."
  856. :group 'org-sparse-trees
  857. :group 'org-time
  858. :type 'boolean)
  859. (defcustom org-occur-hook '(org-first-headline-recenter)
  860. "Hook that is run after `org-occur' has constructed a sparse tree.
  861. This can be used to recenter the window to show as much of the structure
  862. as possible."
  863. :group 'org-sparse-trees
  864. :type 'hook)
  865. (defgroup org-imenu-and-speedbar nil
  866. "Options concerning imenu and speedbar in Org-mode."
  867. :tag "Org Imenu and Speedbar"
  868. :group 'org-structure)
  869. (defcustom org-imenu-depth 2
  870. "The maximum level for Imenu access to Org-mode headlines.
  871. This also applied for speedbar access."
  872. :group 'org-imenu-and-speedbar
  873. :type 'integer)
  874. (defgroup org-table nil
  875. "Options concerning tables in Org-mode."
  876. :tag "Org Table"
  877. :group 'org)
  878. (defcustom org-enable-table-editor 'optimized
  879. "Non-nil means lines starting with \"|\" are handled by the table editor.
  880. When nil, such lines will be treated like ordinary lines.
  881. When equal to the symbol `optimized', the table editor will be optimized to
  882. do the following:
  883. - Automatic overwrite mode in front of whitespace in table fields.
  884. This makes the structure of the table stay in tact as long as the edited
  885. field does not exceed the column width.
  886. - Minimize the number of realigns. Normally, the table is aligned each time
  887. TAB or RET are pressed to move to another field. With optimization this
  888. happens only if changes to a field might have changed the column width.
  889. Optimization requires replacing the functions `self-insert-command',
  890. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  891. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  892. very good at guessing when a re-align will be necessary, but you can always
  893. force one with \\[org-ctrl-c-ctrl-c].
  894. If you would like to use the optimized version in Org-mode, but the
  895. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  896. This variable can be used to turn on and off the table editor during a session,
  897. but in order to toggle optimization, a restart is required.
  898. See also the variable `org-table-auto-blank-field'."
  899. :group 'org-table
  900. :type '(choice
  901. (const :tag "off" nil)
  902. (const :tag "on" t)
  903. (const :tag "on, optimized" optimized)))
  904. (defcustom org-self-insert-cluster-for-undo t
  905. "Non-nil means cluster self-insert commands for undo when possible.
  906. If this is set, then, like in the Emacs command loop, 20 consecutive
  907. characters will be undone together.
  908. This is configurable, because there is some impact on typing performance."
  909. :group 'org-table
  910. :type 'boolean)
  911. (defcustom org-table-tab-recognizes-table.el t
  912. "Non-nil means TAB will automatically notice a table.el table.
  913. When it sees such a table, it moves point into it and - if necessary -
  914. calls `table-recognize-table'."
  915. :group 'org-table-editing
  916. :type 'boolean)
  917. (defgroup org-link nil
  918. "Options concerning links in Org-mode."
  919. :tag "Org Link"
  920. :group 'org)
  921. (defvar org-link-abbrev-alist-local nil
  922. "Buffer-local version of `org-link-abbrev-alist', which see.
  923. The value of this is taken from the #+LINK lines.")
  924. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  925. (defcustom org-link-abbrev-alist nil
  926. "Alist of link abbreviations.
  927. The car of each element is a string, to be replaced at the start of a link.
  928. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  929. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  930. [[linkkey:tag][description]]
  931. The 'linkkey' must be a word word, starting with a letter, followed
  932. by letters, numbers, '-' or '_'.
  933. If REPLACE is a string, the tag will simply be appended to create the link.
  934. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  935. the placeholder \"%h\" will cause a url-encoded version of the tag to
  936. be inserted at that point (see the function `url-hexify-string').
  937. REPLACE may also be a function that will be called with the tag as the
  938. only argument to create the link, which should be returned as a string.
  939. See the manual for examples."
  940. :group 'org-link
  941. :type '(repeat
  942. (cons
  943. (string :tag "Protocol")
  944. (choice
  945. (string :tag "Format")
  946. (function)))))
  947. (defcustom org-descriptive-links t
  948. "Non-nil means hide link part and only show description of bracket links.
  949. Bracket links are like [[link][description]]. This variable sets the initial
  950. state in new org-mode buffers. The setting can then be toggled on a
  951. per-buffer basis from the Org->Hyperlinks menu."
  952. :group 'org-link
  953. :type 'boolean)
  954. (defcustom org-link-file-path-type 'adaptive
  955. "How the path name in file links should be stored.
  956. Valid values are:
  957. relative Relative to the current directory, i.e. the directory of the file
  958. into which the link is being inserted.
  959. absolute Absolute path, if possible with ~ for home directory.
  960. noabbrev Absolute path, no abbreviation of home directory.
  961. adaptive Use relative path for files in the current directory and sub-
  962. directories of it. For other files, use an absolute path."
  963. :group 'org-link
  964. :type '(choice
  965. (const relative)
  966. (const absolute)
  967. (const noabbrev)
  968. (const adaptive)))
  969. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  970. "Types of links that should be activated in Org-mode files.
  971. This is a list of symbols, each leading to the activation of a certain link
  972. type. In principle, it does not hurt to turn on most link types - there may
  973. be a small gain when turning off unused link types. The types are:
  974. bracket The recommended [[link][description]] or [[link]] links with hiding.
  975. angular Links in angular brackets that may contain whitespace like
  976. <bbdb:Carsten Dominik>.
  977. plain Plain links in normal text, no whitespace, like http://google.com.
  978. radio Text that is matched by a radio target, see manual for details.
  979. tag Tag settings in a headline (link to tag search).
  980. date Time stamps (link to calendar).
  981. footnote Footnote labels.
  982. Changing this variable requires a restart of Emacs to become effective."
  983. :group 'org-link
  984. :type '(set :greedy t
  985. (const :tag "Double bracket links (new style)" bracket)
  986. (const :tag "Angular bracket links (old style)" angular)
  987. (const :tag "Plain text links" plain)
  988. (const :tag "Radio target matches" radio)
  989. (const :tag "Tags" tag)
  990. (const :tag "Timestamps" date)
  991. (const :tag "Footnotes" footnote)))
  992. (defcustom org-make-link-description-function nil
  993. "Function to use to generate link descriptions from links. If
  994. nil the link location will be used. This function must take two
  995. parameters; the first is the link and the second the description
  996. org-insert-link has generated, and should return the description
  997. to use."
  998. :group 'org-link
  999. :type 'function)
  1000. (defgroup org-link-store nil
  1001. "Options concerning storing links in Org-mode."
  1002. :tag "Org Store Link"
  1003. :group 'org-link)
  1004. (defcustom org-email-link-description-format "Email %c: %.30s"
  1005. "Format of the description part of a link to an email or usenet message.
  1006. The following %-escapes will be replaced by corresponding information:
  1007. %F full \"From\" field
  1008. %f name, taken from \"From\" field, address if no name
  1009. %T full \"To\" field
  1010. %t first name in \"To\" field, address if no name
  1011. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1012. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1013. %s subject
  1014. %m message-id.
  1015. You may use normal field width specification between the % and the letter.
  1016. This is for example useful to limit the length of the subject.
  1017. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1018. :group 'org-link-store
  1019. :type 'string)
  1020. (defcustom org-from-is-user-regexp
  1021. (let (r1 r2)
  1022. (when (and user-mail-address (not (string= user-mail-address "")))
  1023. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1024. (when (and user-full-name (not (string= user-full-name "")))
  1025. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1026. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1027. "Regexp matched against the \"From:\" header of an email or usenet message.
  1028. It should match if the message is from the user him/herself."
  1029. :group 'org-link-store
  1030. :type 'regexp)
  1031. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1032. "Non-nil means storing a link to an Org file will use entry IDs.
  1033. Note that before this variable is even considered, org-id must be loaded,
  1034. so please customize `org-modules' and turn it on.
  1035. The variable can have the following values:
  1036. t Create an ID if needed to make a link to the current entry.
  1037. create-if-interactive
  1038. If `org-store-link' is called directly (interactively, as a user
  1039. command), do create an ID to support the link. But when doing the
  1040. job for remember, only use the ID if it already exists. The
  1041. purpose of this setting is to avoid proliferation of unwanted
  1042. IDs, just because you happen to be in an Org file when you
  1043. call `org-remember' that automatically and preemptively
  1044. creates a link. If you do want to get an ID link in a remember
  1045. template to an entry not having an ID, create it first by
  1046. explicitly creating a link to it, using `C-c C-l' first.
  1047. create-if-interactive-and-no-custom-id
  1048. Like create-if-interactive, but do not create an ID if there is
  1049. a CUSTOM_ID property defined in the entry. This is the default.
  1050. use-existing
  1051. Use existing ID, do not create one.
  1052. nil Never use an ID to make a link, instead link using a text search for
  1053. the headline text."
  1054. :group 'org-link-store
  1055. :type '(choice
  1056. (const :tag "Create ID to make link" t)
  1057. (const :tag "Create if storing link interactively"
  1058. create-if-interactive)
  1059. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1060. create-if-interactive-and-no-custom-id)
  1061. (const :tag "Only use existing" use-existing)
  1062. (const :tag "Do not use ID to create link" nil)))
  1063. (defcustom org-context-in-file-links t
  1064. "Non-nil means file links from `org-store-link' contain context.
  1065. A search string will be added to the file name with :: as separator and
  1066. used to find the context when the link is activated by the command
  1067. `org-open-at-point'.
  1068. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1069. negates this setting for the duration of the command."
  1070. :group 'org-link-store
  1071. :type 'boolean)
  1072. (defcustom org-keep-stored-link-after-insertion nil
  1073. "Non-nil means keep link in list for entire session.
  1074. The command `org-store-link' adds a link pointing to the current
  1075. location to an internal list. These links accumulate during a session.
  1076. The command `org-insert-link' can be used to insert links into any
  1077. Org-mode file (offering completion for all stored links). When this
  1078. option is nil, every link which has been inserted once using \\[org-insert-link]
  1079. will be removed from the list, to make completing the unused links
  1080. more efficient."
  1081. :group 'org-link-store
  1082. :type 'boolean)
  1083. (defgroup org-link-follow nil
  1084. "Options concerning following links in Org-mode."
  1085. :tag "Org Follow Link"
  1086. :group 'org-link)
  1087. (defcustom org-link-translation-function nil
  1088. "Function to translate links with different syntax to Org syntax.
  1089. This can be used to translate links created for example by the Planner
  1090. or emacs-wiki packages to Org syntax.
  1091. The function must accept two parameters, a TYPE containing the link
  1092. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1093. which is everything after the link protocol. It should return a cons
  1094. with possibly modified values of type and path.
  1095. Org contains a function for this, so if you set this variable to
  1096. `org-translate-link-from-planner', you should be able follow many
  1097. links created by planner."
  1098. :group 'org-link-follow
  1099. :type 'function)
  1100. (defcustom org-follow-link-hook nil
  1101. "Hook that is run after a link has been followed."
  1102. :group 'org-link-follow
  1103. :type 'hook)
  1104. (defcustom org-tab-follows-link nil
  1105. "Non-nil means on links TAB will follow the link.
  1106. Needs to be set before org.el is loaded.
  1107. This really should not be used, it does not make sense, and the
  1108. implementation is bad."
  1109. :group 'org-link-follow
  1110. :type 'boolean)
  1111. (defcustom org-return-follows-link nil
  1112. "Non-nil means on links RET will follow the link.
  1113. Needs to be set before org.el is loaded."
  1114. :group 'org-link-follow
  1115. :type 'boolean)
  1116. (defcustom org-mouse-1-follows-link
  1117. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1118. "Non-nil means mouse-1 on a link will follow the link.
  1119. A longer mouse click will still set point. Does not work on XEmacs.
  1120. Needs to be set before org.el is loaded."
  1121. :group 'org-link-follow
  1122. :type 'boolean)
  1123. (defcustom org-mark-ring-length 4
  1124. "Number of different positions to be recorded in the ring
  1125. Changing this requires a restart of Emacs to work correctly."
  1126. :group 'org-link-follow
  1127. :type 'integer)
  1128. (defcustom org-link-frame-setup
  1129. '((vm . vm-visit-folder-other-frame)
  1130. (gnus . gnus-other-frame)
  1131. (file . find-file-other-window))
  1132. "Setup the frame configuration for following links.
  1133. When following a link with Emacs, it may often be useful to display
  1134. this link in another window or frame. This variable can be used to
  1135. set this up for the different types of links.
  1136. For VM, use any of
  1137. `vm-visit-folder'
  1138. `vm-visit-folder-other-frame'
  1139. For Gnus, use any of
  1140. `gnus'
  1141. `gnus-other-frame'
  1142. `org-gnus-no-new-news'
  1143. For FILE, use any of
  1144. `find-file'
  1145. `find-file-other-window'
  1146. `find-file-other-frame'
  1147. For the calendar, use the variable `calendar-setup'.
  1148. For BBDB, it is currently only possible to display the matches in
  1149. another window."
  1150. :group 'org-link-follow
  1151. :type '(list
  1152. (cons (const vm)
  1153. (choice
  1154. (const vm-visit-folder)
  1155. (const vm-visit-folder-other-window)
  1156. (const vm-visit-folder-other-frame)))
  1157. (cons (const gnus)
  1158. (choice
  1159. (const gnus)
  1160. (const gnus-other-frame)
  1161. (const org-gnus-no-new-news)))
  1162. (cons (const file)
  1163. (choice
  1164. (const find-file)
  1165. (const find-file-other-window)
  1166. (const find-file-other-frame)))))
  1167. (defcustom org-display-internal-link-with-indirect-buffer nil
  1168. "Non-nil means use indirect buffer to display infile links.
  1169. Activating internal links (from one location in a file to another location
  1170. in the same file) normally just jumps to the location. When the link is
  1171. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1172. another window. When this option is set, the other window actually displays
  1173. an indirect buffer clone of the current buffer, to avoid any visibility
  1174. changes to the current buffer."
  1175. :group 'org-link-follow
  1176. :type 'boolean)
  1177. (defcustom org-open-non-existing-files nil
  1178. "Non-nil means `org-open-file' will open non-existing files.
  1179. When nil, an error will be generated.
  1180. This variable applies only to external applications because they
  1181. might choke on non-existing files. If the link is to a file that
  1182. will be opened in Emacs, the variable is ignored."
  1183. :group 'org-link-follow
  1184. :type 'boolean)
  1185. (defcustom org-open-directory-means-index-dot-org nil
  1186. "Non-nil means a link to a directory really means to index.org.
  1187. When nil, following a directory link will run dired or open a finder/explorer
  1188. window on that directory."
  1189. :group 'org-link-follow
  1190. :type 'boolean)
  1191. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1192. "Function and arguments to call for following mailto links.
  1193. This is a list with the first element being a lisp function, and the
  1194. remaining elements being arguments to the function. In string arguments,
  1195. %a will be replaced by the address, and %s will be replaced by the subject
  1196. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1197. :group 'org-link-follow
  1198. :type '(choice
  1199. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1200. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1201. (const :tag "message-mail" (message-mail "%a" "%s"))
  1202. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1203. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1204. "Non-nil means ask for confirmation before executing shell links.
  1205. Shell links can be dangerous: just think about a link
  1206. [[shell:rm -rf ~/*][Google Search]]
  1207. This link would show up in your Org-mode document as \"Google Search\",
  1208. but really it would remove your entire home directory.
  1209. Therefore we advise against setting this variable to nil.
  1210. Just change it to `y-or-n-p' if you want to confirm with a
  1211. single keystroke rather than having to type \"yes\"."
  1212. :group 'org-link-follow
  1213. :type '(choice
  1214. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1215. (const :tag "with y-or-n (faster)" y-or-n-p)
  1216. (const :tag "no confirmation (dangerous)" nil)))
  1217. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1218. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1219. Elisp links can be dangerous: just think about a link
  1220. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1221. This link would show up in your Org-mode document as \"Google Search\",
  1222. but really it would remove your entire home directory.
  1223. Therefore we advise against setting this variable to nil.
  1224. Just change it to `y-or-n-p' if you want to confirm with a
  1225. single keystroke rather than having to type \"yes\"."
  1226. :group 'org-link-follow
  1227. :type '(choice
  1228. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1229. (const :tag "with y-or-n (faster)" y-or-n-p)
  1230. (const :tag "no confirmation (dangerous)" nil)))
  1231. (defconst org-file-apps-defaults-gnu
  1232. '((remote . emacs)
  1233. (system . mailcap)
  1234. (t . mailcap))
  1235. "Default file applications on a UNIX or GNU/Linux system.
  1236. See `org-file-apps'.")
  1237. (defconst org-file-apps-defaults-macosx
  1238. '((remote . emacs)
  1239. (t . "open %s")
  1240. (system . "open %s")
  1241. ("ps.gz" . "gv %s")
  1242. ("eps.gz" . "gv %s")
  1243. ("dvi" . "xdvi %s")
  1244. ("fig" . "xfig %s"))
  1245. "Default file applications on a MacOS X system.
  1246. The system \"open\" is known as a default, but we use X11 applications
  1247. for some files for which the OS does not have a good default.
  1248. See `org-file-apps'.")
  1249. (defconst org-file-apps-defaults-windowsnt
  1250. (list
  1251. '(remote . emacs)
  1252. (cons t
  1253. (list (if (featurep 'xemacs)
  1254. 'mswindows-shell-execute
  1255. 'w32-shell-execute)
  1256. "open" 'file))
  1257. (cons 'system
  1258. (list (if (featurep 'xemacs)
  1259. 'mswindows-shell-execute
  1260. 'w32-shell-execute)
  1261. "open" 'file)))
  1262. "Default file applications on a Windows NT system.
  1263. The system \"open\" is used for most files.
  1264. See `org-file-apps'.")
  1265. (defcustom org-file-apps
  1266. '(
  1267. (auto-mode . emacs)
  1268. ("\\.mm\\'" . default)
  1269. ("\\.x?html?\\'" . default)
  1270. ("\\.pdf\\'" . default)
  1271. )
  1272. "External applications for opening `file:path' items in a document.
  1273. Org-mode uses system defaults for different file types, but
  1274. you can use this variable to set the application for a given file
  1275. extension. The entries in this list are cons cells where the car identifies
  1276. files and the cdr the corresponding command. Possible values for the
  1277. file identifier are
  1278. \"regex\" Regular expression matched against the file: link. For
  1279. backward compatibility, this can also be a string with only
  1280. alphanumeric characters, which is then interpreted as an
  1281. extension.
  1282. `directory' Matches a directory
  1283. `remote' Matches a remote file, accessible through tramp or efs.
  1284. Remote files most likely should be visited through Emacs
  1285. because external applications cannot handle such paths.
  1286. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1287. so all files Emacs knows how to handle. Using this with
  1288. command `emacs' will open most files in Emacs. Beware that this
  1289. will also open html files inside Emacs, unless you add
  1290. (\"html\" . default) to the list as well.
  1291. t Default for files not matched by any of the other options.
  1292. `system' The system command to open files, like `open' on Windows
  1293. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1294. that will be selected if you call `C-c C-o' with a double
  1295. `C-u C-u' prefix.
  1296. Possible values for the command are:
  1297. `emacs' The file will be visited by the current Emacs process.
  1298. `default' Use the default application for this file type, which is the
  1299. association for t in the list, most likely in the system-specific
  1300. part.
  1301. This can be used to overrule an unwanted setting in the
  1302. system-specific variable.
  1303. `system' Use the system command for opening files, like \"open\".
  1304. This command is specified by the entry whose car is `system'.
  1305. Most likely, the system-specific version of this variable
  1306. does define this command, but you can overrule/replace it
  1307. here.
  1308. string A command to be executed by a shell; %s will be replaced
  1309. by the path to the file. If the file identifier is a regex,
  1310. %n will be replaced by the match of the nth match group.
  1311. sexp A Lisp form which will be evaluated. The file path will
  1312. be available in the Lisp variable `file', the link itself
  1313. in the Lisp variable `link'. If the file identifier is a regex,
  1314. the original match data will be restored, so subexpression
  1315. matches are accessible using (match-string n link).
  1316. For more examples, see the system specific constants
  1317. `org-file-apps-defaults-macosx'
  1318. `org-file-apps-defaults-windowsnt'
  1319. `org-file-apps-defaults-gnu'."
  1320. :group 'org-link-follow
  1321. :type '(repeat
  1322. (cons (choice :value ""
  1323. (string :tag "Extension")
  1324. (const :tag "System command to open files" system)
  1325. (const :tag "Default for unrecognized files" t)
  1326. (const :tag "Remote file" remote)
  1327. (const :tag "Links to a directory" directory)
  1328. (const :tag "Any files that have Emacs modes"
  1329. auto-mode))
  1330. (choice :value ""
  1331. (const :tag "Visit with Emacs" emacs)
  1332. (const :tag "Use default" default)
  1333. (const :tag "Use the system command" system)
  1334. (string :tag "Command")
  1335. (sexp :tag "Lisp form")))))
  1336. (defgroup org-refile nil
  1337. "Options concerning refiling entries in Org-mode."
  1338. :tag "Org Refile"
  1339. :group 'org)
  1340. (defcustom org-directory "~/org"
  1341. "Directory with org files.
  1342. This is just a default location to look for Org files. There is no need
  1343. at all to put your files into this directory. It is only used in the
  1344. following situations:
  1345. 1. When a remember template specifies a target file that is not an
  1346. absolute path. The path will then be interpreted relative to
  1347. `org-directory'
  1348. 2. When a remember note is filed away in an interactive way (when exiting the
  1349. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1350. with `org-directory' as the default path."
  1351. :group 'org-refile
  1352. :group 'org-remember
  1353. :type 'directory)
  1354. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1355. "Default target for storing notes.
  1356. Used by the hooks for remember.el. This can be a string, or nil to mean
  1357. the value of `remember-data-file'.
  1358. You can set this on a per-template basis with the variable
  1359. `org-remember-templates'."
  1360. :group 'org-refile
  1361. :group 'org-remember
  1362. :type '(choice
  1363. (const :tag "Default from remember-data-file" nil)
  1364. file))
  1365. (defcustom org-goto-interface 'outline
  1366. "The default interface to be used for `org-goto'.
  1367. Allowed values are:
  1368. outline The interface shows an outline of the relevant file
  1369. and the correct heading is found by moving through
  1370. the outline or by searching with incremental search.
  1371. outline-path-completion Headlines in the current buffer are offered via
  1372. completion. This is the interface also used by
  1373. the refile command."
  1374. :group 'org-refile
  1375. :type '(choice
  1376. (const :tag "Outline" outline)
  1377. (const :tag "Outline-path-completion" outline-path-completion)))
  1378. (defcustom org-goto-max-level 5
  1379. "Maximum level to be considered when running org-goto with refile interface."
  1380. :group 'org-refile
  1381. :type 'integer)
  1382. (defcustom org-reverse-note-order nil
  1383. "Non-nil means store new notes at the beginning of a file or entry.
  1384. When nil, new notes will be filed to the end of a file or entry.
  1385. This can also be a list with cons cells of regular expressions that
  1386. are matched against file names, and values."
  1387. :group 'org-remember
  1388. :group 'org-refile
  1389. :type '(choice
  1390. (const :tag "Reverse always" t)
  1391. (const :tag "Reverse never" nil)
  1392. (repeat :tag "By file name regexp"
  1393. (cons regexp boolean))))
  1394. (defcustom org-log-refile nil
  1395. "Information to record when a task is refiled.
  1396. Possible values are:
  1397. nil Don't add anything
  1398. time Add a time stamp to the task
  1399. note Prompt for a note and add it with template `org-log-note-headings'
  1400. This option can also be set with on a per-file-basis with
  1401. #+STARTUP: nologrefile
  1402. #+STARTUP: logrefile
  1403. #+STARTUP: lognoterefile
  1404. You can have local logging settings for a subtree by setting the LOGGING
  1405. property to one or more of these keywords.
  1406. When bulk-refiling from the agenda, the value `note' is forbidden and
  1407. will temporarily be changed to `time'."
  1408. :group 'org-refile
  1409. :group 'org-progress
  1410. :type '(choice
  1411. (const :tag "No logging" nil)
  1412. (const :tag "Record timestamp" time)
  1413. (const :tag "Record timestamp with note." note)))
  1414. (defcustom org-refile-targets nil
  1415. "Targets for refiling entries with \\[org-refile].
  1416. This is list of cons cells. Each cell contains:
  1417. - a specification of the files to be considered, either a list of files,
  1418. or a symbol whose function or variable value will be used to retrieve
  1419. a file name or a list of file names. If you use `org-agenda-files' for
  1420. that, all agenda files will be scanned for targets. Nil means consider
  1421. headings in the current buffer.
  1422. - A specification of how to find candidate refile targets. This may be
  1423. any of:
  1424. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1425. This tag has to be present in all target headlines, inheritance will
  1426. not be considered.
  1427. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1428. todo keyword.
  1429. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1430. headlines that are refiling targets.
  1431. - a cons cell (:level . N). Any headline of level N is considered a target.
  1432. Note that, when `org-odd-levels-only' is set, level corresponds to
  1433. order in hierarchy, not to the number of stars.
  1434. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1435. Note that, when `org-odd-levels-only' is set, level corresponds to
  1436. order in hierarchy, not to the number of stars.
  1437. You can set the variable `org-refile-target-verify-function' to a function
  1438. to verify each headline found by the simple critery above.
  1439. When this variable is nil, all top-level headlines in the current buffer
  1440. are used, equivalent to the value `((nil . (:level . 1))'."
  1441. :group 'org-refile
  1442. :type '(repeat
  1443. (cons
  1444. (choice :value org-agenda-files
  1445. (const :tag "All agenda files" org-agenda-files)
  1446. (const :tag "Current buffer" nil)
  1447. (function) (variable) (file))
  1448. (choice :tag "Identify target headline by"
  1449. (cons :tag "Specific tag" (const :value :tag) (string))
  1450. (cons :tag "TODO keyword" (const :value :todo) (string))
  1451. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1452. (cons :tag "Level number" (const :value :level) (integer))
  1453. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1454. (defcustom org-refile-target-verify-function nil
  1455. "Function to verify if the headline at point should be a refile target.
  1456. The function will be called without arguments, with point at the
  1457. beginning of the headline. It should return t and leave point
  1458. where it is if the headline is a valid target for refiling.
  1459. If the target should not be selected, the function must return nil.
  1460. In addition to this, it may move point to a place from where the search
  1461. should be continued. For example, the function may decide that the entire
  1462. subtree of the current entry should be excluded and move point to the end
  1463. of the subtree."
  1464. :group 'org-refile
  1465. :type 'function)
  1466. (defcustom org-refile-use-outline-path nil
  1467. "Non-nil means provide refile targets as paths.
  1468. So a level 3 headline will be available as level1/level2/level3.
  1469. When the value is `file', also include the file name (without directory)
  1470. into the path. In this case, you can also stop the completion after
  1471. the file name, to get entries inserted as top level in the file.
  1472. When `full-file-path', include the full file path."
  1473. :group 'org-refile
  1474. :type '(choice
  1475. (const :tag "Not" nil)
  1476. (const :tag "Yes" t)
  1477. (const :tag "Start with file name" file)
  1478. (const :tag "Start with full file path" full-file-path)))
  1479. (defcustom org-outline-path-complete-in-steps t
  1480. "Non-nil means complete the outline path in hierarchical steps.
  1481. When Org-mode uses the refile interface to select an outline path
  1482. \(see variable `org-refile-use-outline-path'), the completion of
  1483. the path can be done is a single go, or if can be done in steps down
  1484. the headline hierarchy. Going in steps is probably the best if you
  1485. do not use a special completion package like `ido' or `icicles'.
  1486. However, when using these packages, going in one step can be very
  1487. fast, while still showing the whole path to the entry."
  1488. :group 'org-refile
  1489. :type 'boolean)
  1490. (defcustom org-refile-allow-creating-parent-nodes nil
  1491. "Non-nil means allow to create new nodes as refile targets.
  1492. New nodes are then created by adding \"/new node name\" to the completion
  1493. of an existing node. When the value of this variable is `confirm',
  1494. new node creation must be confirmed by the user (recommended)
  1495. When nil, the completion must match an existing entry.
  1496. Note that, if the new heading is not seen by the criteria
  1497. listed in `org-refile-targets', multiple instances of the same
  1498. heading would be created by trying again to file under the new
  1499. heading."
  1500. :group 'org-refile
  1501. :type '(choice
  1502. (const :tag "Never" nil)
  1503. (const :tag "Always" t)
  1504. (const :tag "Prompt for confirmation" confirm)))
  1505. (defgroup org-todo nil
  1506. "Options concerning TODO items in Org-mode."
  1507. :tag "Org TODO"
  1508. :group 'org)
  1509. (defgroup org-progress nil
  1510. "Options concerning Progress logging in Org-mode."
  1511. :tag "Org Progress"
  1512. :group 'org-time)
  1513. (defvar org-todo-interpretation-widgets
  1514. '(
  1515. (:tag "Sequence (cycling hits every state)" sequence)
  1516. (:tag "Type (cycling directly to DONE)" type))
  1517. "The available interpretation symbols for customizing
  1518. `org-todo-keywords'.
  1519. Interested libraries should add to this list.")
  1520. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1521. "List of TODO entry keyword sequences and their interpretation.
  1522. \\<org-mode-map>This is a list of sequences.
  1523. Each sequence starts with a symbol, either `sequence' or `type',
  1524. indicating if the keywords should be interpreted as a sequence of
  1525. action steps, or as different types of TODO items. The first
  1526. keywords are states requiring action - these states will select a headline
  1527. for inclusion into the global TODO list Org-mode produces. If one of
  1528. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1529. signify that no further action is necessary. If \"|\" is not found,
  1530. the last keyword is treated as the only DONE state of the sequence.
  1531. The command \\[org-todo] cycles an entry through these states, and one
  1532. additional state where no keyword is present. For details about this
  1533. cycling, see the manual.
  1534. TODO keywords and interpretation can also be set on a per-file basis with
  1535. the special #+SEQ_TODO and #+TYP_TODO lines.
  1536. Each keyword can optionally specify a character for fast state selection
  1537. \(in combination with the variable `org-use-fast-todo-selection')
  1538. and specifiers for state change logging, using the same syntax
  1539. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1540. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1541. indicates to record a time stamp each time this state is selected.
  1542. Each keyword may also specify if a timestamp or a note should be
  1543. recorded when entering or leaving the state, by adding additional
  1544. characters in the parenthesis after the keyword. This looks like this:
  1545. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1546. record only the time of the state change. With X and Y being either
  1547. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1548. Y when leaving the state if and only if the *target* state does not
  1549. define X. You may omit any of the fast-selection key or X or /Y,
  1550. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1551. For backward compatibility, this variable may also be just a list
  1552. of keywords - in this case the interpretation (sequence or type) will be
  1553. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1554. :group 'org-todo
  1555. :group 'org-keywords
  1556. :type '(choice
  1557. (repeat :tag "Old syntax, just keywords"
  1558. (string :tag "Keyword"))
  1559. (repeat :tag "New syntax"
  1560. (cons
  1561. (choice
  1562. :tag "Interpretation"
  1563. ;;Quick and dirty way to see
  1564. ;;`org-todo-interpretations'. This takes the
  1565. ;;place of item arguments
  1566. :convert-widget
  1567. (lambda (widget)
  1568. (widget-put widget
  1569. :args (mapcar
  1570. #'(lambda (x)
  1571. (widget-convert
  1572. (cons 'const x)))
  1573. org-todo-interpretation-widgets))
  1574. widget))
  1575. (repeat
  1576. (string :tag "Keyword"))))))
  1577. (defvar org-todo-keywords-1 nil
  1578. "All TODO and DONE keywords active in a buffer.")
  1579. (make-variable-buffer-local 'org-todo-keywords-1)
  1580. (defvar org-todo-keywords-for-agenda nil)
  1581. (defvar org-done-keywords-for-agenda nil)
  1582. (defvar org-drawers-for-agenda nil)
  1583. (defvar org-todo-keyword-alist-for-agenda nil)
  1584. (defvar org-tag-alist-for-agenda nil)
  1585. (defvar org-agenda-contributing-files nil)
  1586. (defvar org-not-done-keywords nil)
  1587. (make-variable-buffer-local 'org-not-done-keywords)
  1588. (defvar org-done-keywords nil)
  1589. (make-variable-buffer-local 'org-done-keywords)
  1590. (defvar org-todo-heads nil)
  1591. (make-variable-buffer-local 'org-todo-heads)
  1592. (defvar org-todo-sets nil)
  1593. (make-variable-buffer-local 'org-todo-sets)
  1594. (defvar org-todo-log-states nil)
  1595. (make-variable-buffer-local 'org-todo-log-states)
  1596. (defvar org-todo-kwd-alist nil)
  1597. (make-variable-buffer-local 'org-todo-kwd-alist)
  1598. (defvar org-todo-key-alist nil)
  1599. (make-variable-buffer-local 'org-todo-key-alist)
  1600. (defvar org-todo-key-trigger nil)
  1601. (make-variable-buffer-local 'org-todo-key-trigger)
  1602. (defcustom org-todo-interpretation 'sequence
  1603. "Controls how TODO keywords are interpreted.
  1604. This variable is in principle obsolete and is only used for
  1605. backward compatibility, if the interpretation of todo keywords is
  1606. not given already in `org-todo-keywords'. See that variable for
  1607. more information."
  1608. :group 'org-todo
  1609. :group 'org-keywords
  1610. :type '(choice (const sequence)
  1611. (const type)))
  1612. (defcustom org-use-fast-todo-selection t
  1613. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1614. This variable describes if and under what circumstances the cycling
  1615. mechanism for TODO keywords will be replaced by a single-key, direct
  1616. selection scheme.
  1617. When nil, fast selection is never used.
  1618. When the symbol `prefix', it will be used when `org-todo' is called with
  1619. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1620. in an agenda buffer.
  1621. When t, fast selection is used by default. In this case, the prefix
  1622. argument forces cycling instead.
  1623. In all cases, the special interface is only used if access keys have actually
  1624. been assigned by the user, i.e. if keywords in the configuration are followed
  1625. by a letter in parenthesis, like TODO(t)."
  1626. :group 'org-todo
  1627. :type '(choice
  1628. (const :tag "Never" nil)
  1629. (const :tag "By default" t)
  1630. (const :tag "Only with C-u C-c C-t" prefix)))
  1631. (defcustom org-provide-todo-statistics t
  1632. "Non-nil means update todo statistics after insert and toggle.
  1633. ALL-HEADLINES means update todo statistics by including headlines
  1634. with no TODO keyword as well, counting them as not done.
  1635. A list of TODO keywords means the same, but skip keywords that are
  1636. not in this list.
  1637. When this is set, todo statistics is updated in the parent of the
  1638. current entry each time a todo state is changed."
  1639. :group 'org-todo
  1640. :type '(choice
  1641. (const :tag "Yes, only for TODO entries" t)
  1642. (const :tag "Yes, including all entries" 'all-headlines)
  1643. (repeat :tag "Yes, for TODOs in this list"
  1644. (string :tag "TODO keyword"))
  1645. (other :tag "No TODO statistics" nil)))
  1646. (defcustom org-hierarchical-todo-statistics t
  1647. "Non-nil means TODO statistics covers just direct children.
  1648. When nil, all entries in the subtree are considered.
  1649. This has only an effect if `org-provide-todo-statistics' is set.
  1650. To set this to nil for only a single subtree, use a COOKIE_DATA
  1651. property and include the word \"recursive\" into the value."
  1652. :group 'org-todo
  1653. :type 'boolean)
  1654. (defcustom org-after-todo-state-change-hook nil
  1655. "Hook which is run after the state of a TODO item was changed.
  1656. The new state (a string with a TODO keyword, or nil) is available in the
  1657. Lisp variable `state'."
  1658. :group 'org-todo
  1659. :type 'hook)
  1660. (defvar org-blocker-hook nil
  1661. "Hook for functions that are allowed to block a state change.
  1662. Each function gets as its single argument a property list, see
  1663. `org-trigger-hook' for more information about this list.
  1664. If any of the functions in this hook returns nil, the state change
  1665. is blocked.")
  1666. (defvar org-trigger-hook nil
  1667. "Hook for functions that are triggered by a state change.
  1668. Each function gets as its single argument a property list with at least
  1669. the following elements:
  1670. (:type type-of-change :position pos-at-entry-start
  1671. :from old-state :to new-state)
  1672. Depending on the type, more properties may be present.
  1673. This mechanism is currently implemented for:
  1674. TODO state changes
  1675. ------------------
  1676. :type todo-state-change
  1677. :from previous state (keyword as a string), or nil, or a symbol
  1678. 'todo' or 'done', to indicate the general type of state.
  1679. :to new state, like in :from")
  1680. (defcustom org-enforce-todo-dependencies nil
  1681. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1682. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1683. be blocked if any prior sibling is not yet done.
  1684. Finally, if the parent is blocked because of ordered siblings of its own,
  1685. the child will also be blocked.
  1686. This variable needs to be set before org.el is loaded, and you need to
  1687. restart Emacs after a change to make the change effective. The only way
  1688. to change is while Emacs is running is through the customize interface."
  1689. :set (lambda (var val)
  1690. (set var val)
  1691. (if val
  1692. (add-hook 'org-blocker-hook
  1693. 'org-block-todo-from-children-or-siblings-or-parent)
  1694. (remove-hook 'org-blocker-hook
  1695. 'org-block-todo-from-children-or-siblings-or-parent)))
  1696. :group 'org-todo
  1697. :type 'boolean)
  1698. (defcustom org-enforce-todo-checkbox-dependencies nil
  1699. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1700. When this is nil, checkboxes have no influence on switching TODO states.
  1701. When non-nil, you first need to check off all check boxes before the TODO
  1702. entry can be switched to DONE.
  1703. This variable needs to be set before org.el is loaded, and you need to
  1704. restart Emacs after a change to make the change effective. The only way
  1705. to change is while Emacs is running is through the customize interface."
  1706. :set (lambda (var val)
  1707. (set var val)
  1708. (if val
  1709. (add-hook 'org-blocker-hook
  1710. 'org-block-todo-from-checkboxes)
  1711. (remove-hook 'org-blocker-hook
  1712. 'org-block-todo-from-checkboxes)))
  1713. :group 'org-todo
  1714. :type 'boolean)
  1715. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1716. "Non-nil means inserting a TODO heading is treated as state change.
  1717. So when the command \\[org-insert-todo-heading] is used, state change
  1718. logging will apply if appropriate. When nil, the new TODO item will
  1719. be inserted directly, and no logging will take place."
  1720. :group 'org-todo
  1721. :type 'boolean)
  1722. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1723. "Non-nil means switching TODO states with S-cursor counts as state change.
  1724. This is the default behavior. However, setting this to nil allows a
  1725. convenient way to select a TODO state and bypass any logging associated
  1726. with that."
  1727. :group 'org-todo
  1728. :type 'boolean)
  1729. (defcustom org-todo-state-tags-triggers nil
  1730. "Tag changes that should be triggered by TODO state changes.
  1731. This is a list. Each entry is
  1732. (state-change (tag . flag) .......)
  1733. State-change can be a string with a state, and empty string to indicate the
  1734. state that has no TODO keyword, or it can be one of the symbols `todo'
  1735. or `done', meaning any not-done or done state, respectively."
  1736. :group 'org-todo
  1737. :group 'org-tags
  1738. :type '(repeat
  1739. (cons (choice :tag "When changing to"
  1740. (const :tag "Not-done state" todo)
  1741. (const :tag "Done state" done)
  1742. (string :tag "State"))
  1743. (repeat
  1744. (cons :tag "Tag action"
  1745. (string :tag "Tag")
  1746. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1747. (defcustom org-log-done nil
  1748. "Information to record when a task moves to the DONE state.
  1749. Possible values are:
  1750. nil Don't add anything, just change the keyword
  1751. time Add a time stamp to the task
  1752. note Prompt for a note and add it with template `org-log-note-headings'
  1753. This option can also be set with on a per-file-basis with
  1754. #+STARTUP: nologdone
  1755. #+STARTUP: logdone
  1756. #+STARTUP: lognotedone
  1757. You can have local logging settings for a subtree by setting the LOGGING
  1758. property to one or more of these keywords."
  1759. :group 'org-todo
  1760. :group 'org-progress
  1761. :type '(choice
  1762. (const :tag "No logging" nil)
  1763. (const :tag "Record CLOSED timestamp" time)
  1764. (const :tag "Record CLOSED timestamp with note." note)))
  1765. ;; Normalize old uses of org-log-done.
  1766. (cond
  1767. ((eq org-log-done t) (setq org-log-done 'time))
  1768. ((and (listp org-log-done) (memq 'done org-log-done))
  1769. (setq org-log-done 'note)))
  1770. (defcustom org-log-reschedule nil
  1771. "Information to record when the scheduling date of a tasks is modified.
  1772. Possible values are:
  1773. nil Don't add anything, just change the date
  1774. time Add a time stamp to the task
  1775. note Prompt for a note and add it with template `org-log-note-headings'
  1776. This option can also be set with on a per-file-basis with
  1777. #+STARTUP: nologreschedule
  1778. #+STARTUP: logreschedule
  1779. #+STARTUP: lognotereschedule"
  1780. :group 'org-todo
  1781. :group 'org-progress
  1782. :type '(choice
  1783. (const :tag "No logging" nil)
  1784. (const :tag "Record timestamp" time)
  1785. (const :tag "Record timestamp with note." note)))
  1786. (defcustom org-log-redeadline nil
  1787. "Information to record when the deadline date of a tasks is modified.
  1788. Possible values are:
  1789. nil Don't add anything, just change the date
  1790. time Add a time stamp to the task
  1791. note Prompt for a note and add it with template `org-log-note-headings'
  1792. This option can also be set with on a per-file-basis with
  1793. #+STARTUP: nologredeadline
  1794. #+STARTUP: logredeadline
  1795. #+STARTUP: lognoteredeadline
  1796. You can have local logging settings for a subtree by setting the LOGGING
  1797. property to one or more of these keywords."
  1798. :group 'org-todo
  1799. :group 'org-progress
  1800. :type '(choice
  1801. (const :tag "No logging" nil)
  1802. (const :tag "Record timestamp" time)
  1803. (const :tag "Record timestamp with note." note)))
  1804. (defcustom org-log-note-clock-out nil
  1805. "Non-nil means record a note when clocking out of an item.
  1806. This can also be configured on a per-file basis by adding one of
  1807. the following lines anywhere in the buffer:
  1808. #+STARTUP: lognoteclock-out
  1809. #+STARTUP: nolognoteclock-out"
  1810. :group 'org-todo
  1811. :group 'org-progress
  1812. :type 'boolean)
  1813. (defcustom org-log-done-with-time t
  1814. "Non-nil means the CLOSED time stamp will contain date and time.
  1815. When nil, only the date will be recorded."
  1816. :group 'org-progress
  1817. :type 'boolean)
  1818. (defcustom org-log-note-headings
  1819. '((done . "CLOSING NOTE %t")
  1820. (state . "State %-12s from %-12S %t")
  1821. (note . "Note taken on %t")
  1822. (reschedule . "Rescheduled from %S on %t")
  1823. (delschedule . "Not scheduled, was %S on %t")
  1824. (redeadline . "New deadline from %S on %t")
  1825. (deldeadline . "Removed deadline, was %S on %t")
  1826. (refile . "Refiled on %t")
  1827. (clock-out . ""))
  1828. "Headings for notes added to entries.
  1829. The value is an alist, with the car being a symbol indicating the note
  1830. context, and the cdr is the heading to be used. The heading may also be the
  1831. empty string.
  1832. %t in the heading will be replaced by a time stamp.
  1833. %s will be replaced by the new TODO state, in double quotes.
  1834. %S will be replaced by the old TODO state, in double quotes.
  1835. %u will be replaced by the user name.
  1836. %U will be replaced by the full user name.
  1837. In fact, it is not a good idea to change the `state' entry, because
  1838. agenda log mode depends on the format of these entries."
  1839. :group 'org-todo
  1840. :group 'org-progress
  1841. :type '(list :greedy t
  1842. (cons (const :tag "Heading when closing an item" done) string)
  1843. (cons (const :tag
  1844. "Heading when changing todo state (todo sequence only)"
  1845. state) string)
  1846. (cons (const :tag "Heading when just taking a note" note) string)
  1847. (cons (const :tag "Heading when clocking out" clock-out) string)
  1848. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  1849. (cons (const :tag "Heading when rescheduling" reschedule) string)
  1850. (cons (const :tag "Heading when changing deadline" redeadline) string)
  1851. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  1852. (cons (const :tag "Heading when refiling" refile) string)))
  1853. (unless (assq 'note org-log-note-headings)
  1854. (push '(note . "%t") org-log-note-headings))
  1855. (defcustom org-log-into-drawer nil
  1856. "Non-nil means insert state change notes and time stamps into a drawer.
  1857. When nil, state changes notes will be inserted after the headline and
  1858. any scheduling and clock lines, but not inside a drawer.
  1859. The value of this variable should be the name of the drawer to use.
  1860. LOGBOOK is proposed at the default drawer for this purpose, you can
  1861. also set this to a string to define the drawer of your choice.
  1862. A value of t is also allowed, representing \"LOGBOOK\".
  1863. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1864. will be ignored.
  1865. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1866. a subtree."
  1867. :group 'org-todo
  1868. :group 'org-progress
  1869. :type '(choice
  1870. (const :tag "Not into a drawer" nil)
  1871. (const :tag "LOGBOOK" t)
  1872. (string :tag "Other")))
  1873. (if (fboundp 'defvaralias)
  1874. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1875. (defun org-log-into-drawer ()
  1876. "Return the value of `org-log-into-drawer', but let properties overrule.
  1877. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1878. used instead of the default value."
  1879. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1880. (cond
  1881. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1882. ((equal p "t") "LOGBOOK")
  1883. (t p))))
  1884. (defcustom org-log-state-notes-insert-after-drawers nil
  1885. "Non-nil means insert state change notes after any drawers in entry.
  1886. Only the drawers that *immediately* follow the headline and the
  1887. deadline/scheduled line are skipped.
  1888. When nil, insert notes right after the heading and perhaps the line
  1889. with deadline/scheduling if present.
  1890. This variable will have no effect if `org-log-into-drawer' is
  1891. set."
  1892. :group 'org-todo
  1893. :group 'org-progress
  1894. :type 'boolean)
  1895. (defcustom org-log-states-order-reversed t
  1896. "Non-nil means the latest state note will be directly after heading.
  1897. When nil, the state change notes will be ordered according to time."
  1898. :group 'org-todo
  1899. :group 'org-progress
  1900. :type 'boolean)
  1901. (defcustom org-log-repeat 'time
  1902. "Non-nil means record moving through the DONE state when triggering repeat.
  1903. An auto-repeating task is immediately switched back to TODO when
  1904. marked DONE. If you are not logging state changes (by adding \"@\"
  1905. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  1906. record a closing note, there will be no record of the task moving
  1907. through DONE. This variable forces taking a note anyway.
  1908. nil Don't force a record
  1909. time Record a time stamp
  1910. note Record a note
  1911. This option can also be set with on a per-file-basis with
  1912. #+STARTUP: logrepeat
  1913. #+STARTUP: lognoterepeat
  1914. #+STARTUP: nologrepeat
  1915. You can have local logging settings for a subtree by setting the LOGGING
  1916. property to one or more of these keywords."
  1917. :group 'org-todo
  1918. :group 'org-progress
  1919. :type '(choice
  1920. (const :tag "Don't force a record" nil)
  1921. (const :tag "Force recording the DONE state" time)
  1922. (const :tag "Force recording a note with the DONE state" note)))
  1923. (defgroup org-priorities nil
  1924. "Priorities in Org-mode."
  1925. :tag "Org Priorities"
  1926. :group 'org-todo)
  1927. (defcustom org-enable-priority-commands t
  1928. "Non-nil means priority commands are active.
  1929. When nil, these commands will be disabled, so that you never accidentally
  1930. set a priority."
  1931. :group 'org-priorities
  1932. :type 'boolean)
  1933. (defcustom org-highest-priority ?A
  1934. "The highest priority of TODO items. A character like ?A, ?B etc.
  1935. Must have a smaller ASCII number than `org-lowest-priority'."
  1936. :group 'org-priorities
  1937. :type 'character)
  1938. (defcustom org-lowest-priority ?C
  1939. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1940. Must have a larger ASCII number than `org-highest-priority'."
  1941. :group 'org-priorities
  1942. :type 'character)
  1943. (defcustom org-default-priority ?B
  1944. "The default priority of TODO items.
  1945. This is the priority an item get if no explicit priority is given."
  1946. :group 'org-priorities
  1947. :type 'character)
  1948. (defcustom org-priority-start-cycle-with-default t
  1949. "Non-nil means start with default priority when starting to cycle.
  1950. When this is nil, the first step in the cycle will be (depending on the
  1951. command used) one higher or lower that the default priority."
  1952. :group 'org-priorities
  1953. :type 'boolean)
  1954. (defgroup org-time nil
  1955. "Options concerning time stamps and deadlines in Org-mode."
  1956. :tag "Org Time"
  1957. :group 'org)
  1958. (defcustom org-insert-labeled-timestamps-at-point nil
  1959. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  1960. When nil, these labeled time stamps are forces into the second line of an
  1961. entry, just after the headline. When scheduling from the global TODO list,
  1962. the time stamp will always be forced into the second line."
  1963. :group 'org-time
  1964. :type 'boolean)
  1965. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1966. "Formats for `format-time-string' which are used for time stamps.
  1967. It is not recommended to change this constant.")
  1968. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1969. "Number of minutes to round time stamps to.
  1970. These are two values, the first applies when first creating a time stamp.
  1971. The second applies when changing it with the commands `S-up' and `S-down'.
  1972. When changing the time stamp, this means that it will change in steps
  1973. of N minutes, as given by the second value.
  1974. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1975. numbers should be factors of 60, so for example 5, 10, 15.
  1976. When this is larger than 1, you can still force an exact time-stamp by using
  1977. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1978. and by using a prefix arg to `S-up/down' to specify the exact number
  1979. of minutes to shift."
  1980. :group 'org-time
  1981. :get '(lambda (var) ; Make sure all entries have 5 elements
  1982. (if (integerp (default-value var))
  1983. (list (default-value var) 5)
  1984. (default-value var)))
  1985. :type '(list
  1986. (integer :tag "when inserting times")
  1987. (integer :tag "when modifying times")))
  1988. ;; Normalize old customizations of this variable.
  1989. (when (integerp org-time-stamp-rounding-minutes)
  1990. (setq org-time-stamp-rounding-minutes
  1991. (list org-time-stamp-rounding-minutes
  1992. org-time-stamp-rounding-minutes)))
  1993. (defcustom org-display-custom-times nil
  1994. "Non-nil means overlay custom formats over all time stamps.
  1995. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1996. To turn this on on a per-file basis, insert anywhere in the file:
  1997. #+STARTUP: customtime"
  1998. :group 'org-time
  1999. :set 'set-default
  2000. :type 'sexp)
  2001. (make-variable-buffer-local 'org-display-custom-times)
  2002. (defcustom org-time-stamp-custom-formats
  2003. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2004. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2005. These are overlayed over the default ISO format if the variable
  2006. `org-display-custom-times' is set. Time like %H:%M should be at the
  2007. end of the second format. The custom formats are also honored by export
  2008. commands, if custom time display is turned on at the time of export."
  2009. :group 'org-time
  2010. :type 'sexp)
  2011. (defun org-time-stamp-format (&optional long inactive)
  2012. "Get the right format for a time string."
  2013. (let ((f (if long (cdr org-time-stamp-formats)
  2014. (car org-time-stamp-formats))))
  2015. (if inactive
  2016. (concat "[" (substring f 1 -1) "]")
  2017. f)))
  2018. (defcustom org-time-clocksum-format "%d:%02d"
  2019. "The format string used when creating CLOCKSUM lines, or when
  2020. org-mode generates a time duration."
  2021. :group 'org-time
  2022. :type 'string)
  2023. (defcustom org-time-clocksum-use-fractional nil
  2024. "If non-nil, \\[org-clock-display] uses fractional times.
  2025. org-mode generates a time duration."
  2026. :group 'org-time
  2027. :type 'boolean)
  2028. (defcustom org-time-clocksum-fractional-format "%.2f"
  2029. "The format string used when creating CLOCKSUM lines, or when
  2030. org-mode generates a time duration."
  2031. :group 'org-time
  2032. :type 'string)
  2033. (defcustom org-deadline-warning-days 14
  2034. "No. of days before expiration during which a deadline becomes active.
  2035. This variable governs the display in sparse trees and in the agenda.
  2036. When 0 or negative, it means use this number (the absolute value of it)
  2037. even if a deadline has a different individual lead time specified.
  2038. Custom commands can set this variable in the options section."
  2039. :group 'org-time
  2040. :group 'org-agenda-daily/weekly
  2041. :type 'integer)
  2042. (defcustom org-read-date-prefer-future t
  2043. "Non-nil means assume future for incomplete date input from user.
  2044. This affects the following situations:
  2045. 1. The user gives a month but not a year.
  2046. For example, if it is april and you enter \"feb 2\", this will be read
  2047. as feb 2, *next* year. \"May 5\", however, will be this year.
  2048. 2. The user gives a day, but no month.
  2049. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2050. read this as the third of *next* month. However, if you enter \"17\",
  2051. it will be considered as *this* month.
  2052. If you set this variable to the symbol `time', then also the following
  2053. will work:
  2054. 3. If the user gives a time, but no day. If the time is before now,
  2055. to will be interpreted as tomorrow.
  2056. Currently none of this works for ISO week specifications.
  2057. When this option is nil, the current day, month and year will always be
  2058. used as defaults."
  2059. :group 'org-time
  2060. :type '(choice
  2061. (const :tag "Never" nil)
  2062. (const :tag "Check month and day" t)
  2063. (const :tag "Check month, day, and time" time)))
  2064. (defcustom org-read-date-display-live t
  2065. "Non-nil means display current interpretation of date prompt live.
  2066. This display will be in an overlay, in the minibuffer."
  2067. :group 'org-time
  2068. :type 'boolean)
  2069. (defcustom org-read-date-popup-calendar t
  2070. "Non-nil means pop up a calendar when prompting for a date.
  2071. In the calendar, the date can be selected with mouse-1. However, the
  2072. minibuffer will also be active, and you can simply enter the date as well.
  2073. When nil, only the minibuffer will be available."
  2074. :group 'org-time
  2075. :type 'boolean)
  2076. (if (fboundp 'defvaralias)
  2077. (defvaralias 'org-popup-calendar-for-date-prompt
  2078. 'org-read-date-popup-calendar))
  2079. (defcustom org-read-date-minibuffer-setup-hook nil
  2080. "Hook to be used to set up keys for the date/time interface.
  2081. Add key definitions to `minibuffer-local-map', which will be a temporary
  2082. copy."
  2083. :group 'org-time
  2084. :type 'hook)
  2085. (defcustom org-extend-today-until 0
  2086. "The hour when your day really ends. Must be an integer.
  2087. This has influence for the following applications:
  2088. - When switching the agenda to \"today\". It it is still earlier than
  2089. the time given here, the day recognized as TODAY is actually yesterday.
  2090. - When a date is read from the user and it is still before the time given
  2091. here, the current date and time will be assumed to be yesterday, 23:59.
  2092. Also, timestamps inserted in remember templates follow this rule.
  2093. IMPORTANT: This is a feature whose implementation is and likely will
  2094. remain incomplete. Really, it is only here because past midnight seems to
  2095. be the favorite working time of John Wiegley :-)"
  2096. :group 'org-time
  2097. :type 'integer)
  2098. (defcustom org-edit-timestamp-down-means-later nil
  2099. "Non-nil means S-down will increase the time in a time stamp.
  2100. When nil, S-up will increase."
  2101. :group 'org-time
  2102. :type 'boolean)
  2103. (defcustom org-calendar-follow-timestamp-change t
  2104. "Non-nil means make the calendar window follow timestamp changes.
  2105. When a timestamp is modified and the calendar window is visible, it will be
  2106. moved to the new date."
  2107. :group 'org-time
  2108. :type 'boolean)
  2109. (defgroup org-tags nil
  2110. "Options concerning tags in Org-mode."
  2111. :tag "Org Tags"
  2112. :group 'org)
  2113. (defcustom org-tag-alist nil
  2114. "List of tags allowed in Org-mode files.
  2115. When this list is nil, Org-mode will base TAG input on what is already in the
  2116. buffer.
  2117. The value of this variable is an alist, the car of each entry must be a
  2118. keyword as a string, the cdr may be a character that is used to select
  2119. that tag through the fast-tag-selection interface.
  2120. See the manual for details."
  2121. :group 'org-tags
  2122. :type '(repeat
  2123. (choice
  2124. (cons (string :tag "Tag name")
  2125. (character :tag "Access char"))
  2126. (list :tag "Start radio group"
  2127. (const :startgroup)
  2128. (option (string :tag "Group description")))
  2129. (list :tag "End radio group"
  2130. (const :endgroup)
  2131. (option (string :tag "Group description")))
  2132. (const :tag "New line" (:newline)))))
  2133. (defcustom org-tag-persistent-alist nil
  2134. "List of tags that will always appear in all Org-mode files.
  2135. This is in addition to any in buffer settings or customizations
  2136. of `org-tag-alist'.
  2137. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2138. The value of this variable is an alist, the car of each entry must be a
  2139. keyword as a string, the cdr may be a character that is used to select
  2140. that tag through the fast-tag-selection interface.
  2141. See the manual for details.
  2142. To disable these tags on a per-file basis, insert anywhere in the file:
  2143. #+STARTUP: noptag"
  2144. :group 'org-tags
  2145. :type '(repeat
  2146. (choice
  2147. (cons (string :tag "Tag name")
  2148. (character :tag "Access char"))
  2149. (const :tag "Start radio group" (:startgroup))
  2150. (const :tag "End radio group" (:endgroup))
  2151. (const :tag "New line" (:newline)))))
  2152. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2153. "If non-nil, always offer completion for all tags of all agenda files.
  2154. Instead of customizing this variable directly, you might want to
  2155. set it locally for remember buffers, because there no list of
  2156. tags in that file can be created dynamically (there are none).
  2157. (add-hook 'org-remember-mode-hook
  2158. (lambda ()
  2159. (set (make-local-variable
  2160. 'org-complete-tags-always-offer-all-agenda-tags)
  2161. t)))"
  2162. :group 'org-tags
  2163. :type 'boolean)
  2164. (defvar org-file-tags nil
  2165. "List of tags that can be inherited by all entries in the file.
  2166. The tags will be inherited if the variable `org-use-tag-inheritance'
  2167. says they should be.
  2168. This variable is populated from #+FILETAGS lines.")
  2169. (defcustom org-use-fast-tag-selection 'auto
  2170. "Non-nil means use fast tag selection scheme.
  2171. This is a special interface to select and deselect tags with single keys.
  2172. When nil, fast selection is never used.
  2173. When the symbol `auto', fast selection is used if and only if selection
  2174. characters for tags have been configured, either through the variable
  2175. `org-tag-alist' or through a #+TAGS line in the buffer.
  2176. When t, fast selection is always used and selection keys are assigned
  2177. automatically if necessary."
  2178. :group 'org-tags
  2179. :type '(choice
  2180. (const :tag "Always" t)
  2181. (const :tag "Never" nil)
  2182. (const :tag "When selection characters are configured" 'auto)))
  2183. (defcustom org-fast-tag-selection-single-key nil
  2184. "Non-nil means fast tag selection exits after first change.
  2185. When nil, you have to press RET to exit it.
  2186. During fast tag selection, you can toggle this flag with `C-c'.
  2187. This variable can also have the value `expert'. In this case, the window
  2188. displaying the tags menu is not even shown, until you press C-c again."
  2189. :group 'org-tags
  2190. :type '(choice
  2191. (const :tag "No" nil)
  2192. (const :tag "Yes" t)
  2193. (const :tag "Expert" expert)))
  2194. (defvar org-fast-tag-selection-include-todo nil
  2195. "Non-nil means fast tags selection interface will also offer TODO states.
  2196. This is an undocumented feature, you should not rely on it.")
  2197. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2198. "The column to which tags should be indented in a headline.
  2199. If this number is positive, it specifies the column. If it is negative,
  2200. it means that the tags should be flushright to that column. For example,
  2201. -80 works well for a normal 80 character screen."
  2202. :group 'org-tags
  2203. :type 'integer)
  2204. (defcustom org-auto-align-tags t
  2205. "Non-nil means realign tags after pro/demotion of TODO state change.
  2206. These operations change the length of a headline and therefore shift
  2207. the tags around. With this options turned on, after each such operation
  2208. the tags are again aligned to `org-tags-column'."
  2209. :group 'org-tags
  2210. :type 'boolean)
  2211. (defcustom org-use-tag-inheritance t
  2212. "Non-nil means tags in levels apply also for sublevels.
  2213. When nil, only the tags directly given in a specific line apply there.
  2214. This may also be a list of tags that should be inherited, or a regexp that
  2215. matches tags that should be inherited. Additional control is possible
  2216. with the variable `org-tags-exclude-from-inheritance' which gives an
  2217. explicit list of tags to be excluded from inheritance., even if the value of
  2218. `org-use-tag-inheritance' would select it for inheritance.
  2219. If this option is t, a match early-on in a tree can lead to a large
  2220. number of matches in the subtree when constructing the agenda or creating
  2221. a sparse tree. If you only want to see the first match in a tree during
  2222. a search, check out the variable `org-tags-match-list-sublevels'."
  2223. :group 'org-tags
  2224. :type '(choice
  2225. (const :tag "Not" nil)
  2226. (const :tag "Always" t)
  2227. (repeat :tag "Specific tags" (string :tag "Tag"))
  2228. (regexp :tag "Tags matched by regexp")))
  2229. (defcustom org-tags-exclude-from-inheritance nil
  2230. "List of tags that should never be inherited.
  2231. This is a way to exclude a few tags from inheritance. For way to do
  2232. the opposite, to actively allow inheritance for selected tags,
  2233. see the variable `org-use-tag-inheritance'."
  2234. :group 'org-tags
  2235. :type '(repeat (string :tag "Tag")))
  2236. (defun org-tag-inherit-p (tag)
  2237. "Check if TAG is one that should be inherited."
  2238. (cond
  2239. ((member tag org-tags-exclude-from-inheritance) nil)
  2240. ((eq org-use-tag-inheritance t) t)
  2241. ((not org-use-tag-inheritance) nil)
  2242. ((stringp org-use-tag-inheritance)
  2243. (string-match org-use-tag-inheritance tag))
  2244. ((listp org-use-tag-inheritance)
  2245. (member tag org-use-tag-inheritance))
  2246. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2247. (defcustom org-tags-match-list-sublevels t
  2248. "Non-nil means list also sublevels of headlines matching a search.
  2249. This variable applies to tags/property searches, and also to stuck
  2250. projects because this search is based on a tags match as well.
  2251. When set to the symbol `indented', sublevels are indented with
  2252. leading dots.
  2253. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2254. the sublevels of a headline matching a tag search often also match
  2255. the same search. Listing all of them can create very long lists.
  2256. Setting this variable to nil causes subtrees of a match to be skipped.
  2257. This variable is semi-obsolete and probably should always be true. It
  2258. is better to limit inheritance to certain tags using the variables
  2259. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2260. :group 'org-tags
  2261. :type '(choice
  2262. (const :tag "No, don't list them" nil)
  2263. (const :tag "Yes, do list them" t)
  2264. (const :tag "List them, indented with leading dots" indented)))
  2265. (defcustom org-tags-sort-function nil
  2266. "When set, tags are sorted using this function as a comparator"
  2267. :group 'org-tags
  2268. :type '(choice
  2269. (const :tag "No sorting" nil)
  2270. (const :tag "Alphabetical" string<)
  2271. (const :tag "Reverse alphabetical" string>)
  2272. (function :tag "Custom function" nil)))
  2273. (defvar org-tags-history nil
  2274. "History of minibuffer reads for tags.")
  2275. (defvar org-last-tags-completion-table nil
  2276. "The last used completion table for tags.")
  2277. (defvar org-after-tags-change-hook nil
  2278. "Hook that is run after the tags in a line have changed.")
  2279. (defgroup org-properties nil
  2280. "Options concerning properties in Org-mode."
  2281. :tag "Org Properties"
  2282. :group 'org)
  2283. (defcustom org-property-format "%-10s %s"
  2284. "How property key/value pairs should be formatted by `indent-line'.
  2285. When `indent-line' hits a property definition, it will format the line
  2286. according to this format, mainly to make sure that the values are
  2287. lined-up with respect to each other."
  2288. :group 'org-properties
  2289. :type 'string)
  2290. (defcustom org-use-property-inheritance nil
  2291. "Non-nil means properties apply also for sublevels.
  2292. This setting is chiefly used during property searches. Turning it on can
  2293. cause significant overhead when doing a search, which is why it is not
  2294. on by default.
  2295. When nil, only the properties directly given in the current entry count.
  2296. When t, every property is inherited. The value may also be a list of
  2297. properties that should have inheritance, or a regular expression matching
  2298. properties that should be inherited.
  2299. However, note that some special properties use inheritance under special
  2300. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2301. and the properties ending in \"_ALL\" when they are used as descriptor
  2302. for valid values of a property.
  2303. Note for programmers:
  2304. When querying an entry with `org-entry-get', you can control if inheritance
  2305. should be used. By default, `org-entry-get' looks only at the local
  2306. properties. You can request inheritance by setting the inherit argument
  2307. to t (to force inheritance) or to `selective' (to respect the setting
  2308. in this variable)."
  2309. :group 'org-properties
  2310. :type '(choice
  2311. (const :tag "Not" nil)
  2312. (const :tag "Always" t)
  2313. (repeat :tag "Specific properties" (string :tag "Property"))
  2314. (regexp :tag "Properties matched by regexp")))
  2315. (defun org-property-inherit-p (property)
  2316. "Check if PROPERTY is one that should be inherited."
  2317. (cond
  2318. ((eq org-use-property-inheritance t) t)
  2319. ((not org-use-property-inheritance) nil)
  2320. ((stringp org-use-property-inheritance)
  2321. (string-match org-use-property-inheritance property))
  2322. ((listp org-use-property-inheritance)
  2323. (member property org-use-property-inheritance))
  2324. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2325. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2326. "The default column format, if no other format has been defined.
  2327. This variable can be set on the per-file basis by inserting a line
  2328. #+COLUMNS: %25ITEM ....."
  2329. :group 'org-properties
  2330. :type 'string)
  2331. (defcustom org-columns-ellipses ".."
  2332. "The ellipses to be used when a field in column view is truncated.
  2333. When this is the empty string, as many characters as possible are shown,
  2334. but then there will be no visual indication that the field has been truncated.
  2335. When this is a string of length N, the last N characters of a truncated
  2336. field are replaced by this string. If the column is narrower than the
  2337. ellipses string, only part of the ellipses string will be shown."
  2338. :group 'org-properties
  2339. :type 'string)
  2340. (defcustom org-columns-modify-value-for-display-function nil
  2341. "Function that modifies values for display in column view.
  2342. For example, it can be used to cut out a certain part from a time stamp.
  2343. The function must take 2 arguments:
  2344. column-title The title of the column (*not* the property name)
  2345. value The value that should be modified.
  2346. The function should return the value that should be displayed,
  2347. or nil if the normal value should be used."
  2348. :group 'org-properties
  2349. :type 'function)
  2350. (defcustom org-effort-property "Effort"
  2351. "The property that is being used to keep track of effort estimates.
  2352. Effort estimates given in this property need to have the format H:MM."
  2353. :group 'org-properties
  2354. :group 'org-progress
  2355. :type '(string :tag "Property"))
  2356. (defconst org-global-properties-fixed
  2357. '(("VISIBILITY_ALL" . "folded children content all")
  2358. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2359. "List of property/value pairs that can be inherited by any entry.
  2360. These are fixed values, for the preset properties. The user variable
  2361. that can be used to add to this list is `org-global-properties'.
  2362. The entries in this list are cons cells where the car is a property
  2363. name and cdr is a string with the value. If the value represents
  2364. multiple items like an \"_ALL\" property, separate the items by
  2365. spaces.")
  2366. (defcustom org-global-properties nil
  2367. "List of property/value pairs that can be inherited by any entry.
  2368. This list will be combined with the constant `org-global-properties-fixed'.
  2369. The entries in this list are cons cells where the car is a property
  2370. name and cdr is a string with the value.
  2371. You can set buffer-local values for the same purpose in the variable
  2372. `org-file-properties' this by adding lines like
  2373. #+PROPERTY: NAME VALUE"
  2374. :group 'org-properties
  2375. :type '(repeat
  2376. (cons (string :tag "Property")
  2377. (string :tag "Value"))))
  2378. (defvar org-file-properties nil
  2379. "List of property/value pairs that can be inherited by any entry.
  2380. Valid for the current buffer.
  2381. This variable is populated from #+PROPERTY lines.")
  2382. (make-variable-buffer-local 'org-file-properties)
  2383. (defgroup org-agenda nil
  2384. "Options concerning agenda views in Org-mode."
  2385. :tag "Org Agenda"
  2386. :group 'org)
  2387. (defvar org-category nil
  2388. "Variable used by org files to set a category for agenda display.
  2389. Such files should use a file variable to set it, for example
  2390. # -*- mode: org; org-category: \"ELisp\"
  2391. or contain a special line
  2392. #+CATEGORY: ELisp
  2393. If the file does not specify a category, then file's base name
  2394. is used instead.")
  2395. (make-variable-buffer-local 'org-category)
  2396. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2397. (defcustom org-agenda-files nil
  2398. "The files to be used for agenda display.
  2399. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2400. \\[org-remove-file]. You can also use customize to edit the list.
  2401. If an entry is a directory, all files in that directory that are matched by
  2402. `org-agenda-file-regexp' will be part of the file list.
  2403. If the value of the variable is not a list but a single file name, then
  2404. the list of agenda files is actually stored and maintained in that file, one
  2405. agenda file per line. In this file paths can be given relative to
  2406. `org-directory'. Tilde expansion and environment variable substitution
  2407. are also made."
  2408. :group 'org-agenda
  2409. :type '(choice
  2410. (repeat :tag "List of files and directories" file)
  2411. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2412. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2413. "Regular expression to match files for `org-agenda-files'.
  2414. If any element in the list in that variable contains a directory instead
  2415. of a normal file, all files in that directory that are matched by this
  2416. regular expression will be included."
  2417. :group 'org-agenda
  2418. :type 'regexp)
  2419. (defcustom org-agenda-text-search-extra-files nil
  2420. "List of extra files to be searched by text search commands.
  2421. These files will be search in addition to the agenda files by the
  2422. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2423. Note that these files will only be searched for text search commands,
  2424. not for the other agenda views like todo lists, tag searches or the weekly
  2425. agenda. This variable is intended to list notes and possibly archive files
  2426. that should also be searched by these two commands.
  2427. In fact, if the first element in the list is the symbol `agenda-archives',
  2428. than all archive files of all agenda files will be added to the search
  2429. scope."
  2430. :group 'org-agenda
  2431. :type '(set :greedy t
  2432. (const :tag "Agenda Archives" agenda-archives)
  2433. (repeat :inline t (file))))
  2434. (if (fboundp 'defvaralias)
  2435. (defvaralias 'org-agenda-multi-occur-extra-files
  2436. 'org-agenda-text-search-extra-files))
  2437. (defcustom org-agenda-skip-unavailable-files nil
  2438. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2439. A nil value means to remove them, after a query, from the list."
  2440. :group 'org-agenda
  2441. :type 'boolean)
  2442. (defcustom org-calendar-to-agenda-key [?c]
  2443. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2444. The command `org-calendar-goto-agenda' will be bound to this key. The
  2445. default is the character `c' because then `c' can be used to switch back and
  2446. forth between agenda and calendar."
  2447. :group 'org-agenda
  2448. :type 'sexp)
  2449. (defcustom org-calendar-agenda-action-key [?k]
  2450. "The key to be installed in `calendar-mode-map' for agenda-action.
  2451. The command `org-agenda-action' will be bound to this key. The
  2452. default is the character `k' because we use the same key in the agenda."
  2453. :group 'org-agenda
  2454. :type 'sexp)
  2455. (defcustom org-calendar-insert-diary-entry-key [?i]
  2456. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2457. This option is irrelevant until `org-agenda-diary-file' has been configured
  2458. to point to an Org-mode file. When that is the case, the command
  2459. `org-agenda-diary-entry' will be bound to the key given here, by default
  2460. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2461. if you want to continue doing this, you need to change this to a different
  2462. key."
  2463. :group 'org-agenda
  2464. :type 'sexp)
  2465. (defcustom org-agenda-diary-file 'diary-file
  2466. "File to which to add new entries with the `i' key in agenda and calendar.
  2467. When this is the symbol `diary-file', the functionality in the Emacs
  2468. calendar will be used to add entries to the `diary-file'. But when this
  2469. points to a file, `org-agenda-diary-entry' will be used instead."
  2470. :group 'org-agenda
  2471. :type '(choice
  2472. (const :tag "The standard Emacs diary file" diary-file)
  2473. (file :tag "Special Org file diary entries")))
  2474. (eval-after-load "calendar"
  2475. '(progn
  2476. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2477. 'org-calendar-goto-agenda)
  2478. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2479. 'org-agenda-action)
  2480. (add-hook 'calendar-mode-hook
  2481. (lambda ()
  2482. (unless (eq org-agenda-diary-file 'diary-file)
  2483. (define-key calendar-mode-map
  2484. org-calendar-insert-diary-entry-key
  2485. 'org-agenda-diary-entry))))))
  2486. (defgroup org-latex nil
  2487. "Options for embedding LaTeX code into Org-mode."
  2488. :tag "Org LaTeX"
  2489. :group 'org)
  2490. (defcustom org-format-latex-options
  2491. '(:foreground default :background default :scale 1.0
  2492. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2493. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2494. "Options for creating images from LaTeX fragments.
  2495. This is a property list with the following properties:
  2496. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2497. `default' means use the foreground of the default face.
  2498. :background the background color, or \"Transparent\".
  2499. `default' means use the background of the default face.
  2500. :scale a scaling factor for the size of the images.
  2501. :html-foreground, :html-background, :html-scale
  2502. the same numbers for HTML export.
  2503. :matchers a list indicating which matchers should be used to
  2504. find LaTeX fragments. Valid members of this list are:
  2505. \"begin\" find environments
  2506. \"$1\" find single characters surrounded by $.$
  2507. \"$\" find math expressions surrounded by $...$
  2508. \"$$\" find math expressions surrounded by $$....$$
  2509. \"\\(\" find math expressions surrounded by \\(...\\)
  2510. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2511. :group 'org-latex
  2512. :type 'plist)
  2513. (defcustom org-format-latex-signal-error t
  2514. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2515. When nil, just push out a message."
  2516. :group 'org-latex
  2517. :type 'boolean)
  2518. (defcustom org-format-latex-header "\\documentclass{article}
  2519. \\usepackage{amssymb}
  2520. \\usepackage[usenames]{color}
  2521. \\usepackage{amsmath}
  2522. \\usepackage{latexsym}
  2523. \\usepackage[mathscr]{eucal}
  2524. \\pagestyle{empty} % do not remove
  2525. % The settings below are copied from fullpage.sty
  2526. \\setlength{\\textwidth}{\\paperwidth}
  2527. \\addtolength{\\textwidth}{-3cm}
  2528. \\setlength{\\oddsidemargin}{1.5cm}
  2529. \\addtolength{\\oddsidemargin}{-2.54cm}
  2530. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2531. \\setlength{\\textheight}{\\paperheight}
  2532. \\addtolength{\\textheight}{-\\headheight}
  2533. \\addtolength{\\textheight}{-\\headsep}
  2534. \\addtolength{\\textheight}{-\\footskip}
  2535. \\addtolength{\\textheight}{-3cm}
  2536. \\setlength{\\topmargin}{1.5cm}
  2537. \\addtolength{\\topmargin}{-2.54cm}"
  2538. "The document header used for processing LaTeX fragments.
  2539. It is imperative that this header make sure that no page number
  2540. appears on the page."
  2541. :group 'org-latex
  2542. :type 'string)
  2543. (defvar org-format-latex-header-extra nil)
  2544. ;; The following variable is defined here because is it also used
  2545. ;; when formatting latex fragments. Originally it was part of the
  2546. ;; LaTeX exporter, which is why the name includes "export".
  2547. (defcustom org-export-latex-packages-alist nil
  2548. "Alist of packages to be inserted in the header.
  2549. Each cell is of the format \( \"option\" . \"package\" \)."
  2550. :group 'org-export-latex
  2551. :type '(repeat
  2552. (list
  2553. (string :tag "option")
  2554. (string :tag "package"))))
  2555. (defgroup org-font-lock nil
  2556. "Font-lock settings for highlighting in Org-mode."
  2557. :tag "Org Font Lock"
  2558. :group 'org)
  2559. (defcustom org-level-color-stars-only nil
  2560. "Non-nil means fontify only the stars in each headline.
  2561. When nil, the entire headline is fontified.
  2562. Changing it requires restart of `font-lock-mode' to become effective
  2563. also in regions already fontified."
  2564. :group 'org-font-lock
  2565. :type 'boolean)
  2566. (defcustom org-hide-leading-stars nil
  2567. "Non-nil means hide the first N-1 stars in a headline.
  2568. This works by using the face `org-hide' for these stars. This
  2569. face is white for a light background, and black for a dark
  2570. background. You may have to customize the face `org-hide' to
  2571. make this work.
  2572. Changing it requires restart of `font-lock-mode' to become effective
  2573. also in regions already fontified.
  2574. You may also set this on a per-file basis by adding one of the following
  2575. lines to the buffer:
  2576. #+STARTUP: hidestars
  2577. #+STARTUP: showstars"
  2578. :group 'org-font-lock
  2579. :type 'boolean)
  2580. (defcustom org-hidden-keywords nil
  2581. "List of keywords that should be hidden when typed in the org buffer.
  2582. For example, add #+TITLE to this list in order to make the
  2583. document title appear in the buffer without the initial #+TITLE:
  2584. keyword."
  2585. :group 'org-font-lock
  2586. :type '(set (const :tag "#+AUTHOR" author)
  2587. (const :tag "#+DATE" date)
  2588. (const :tag "#+EMAIL" email)
  2589. (const :tag "#+TITLE" title)))
  2590. (defcustom org-fontify-done-headline nil
  2591. "Non-nil means change the face of a headline if it is marked DONE.
  2592. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2593. When this is non-nil, the headline after the keyword is set to the
  2594. `org-headline-done' as an additional indication."
  2595. :group 'org-font-lock
  2596. :type 'boolean)
  2597. (defcustom org-fontify-emphasized-text t
  2598. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2599. Changing this variable requires a restart of Emacs to take effect."
  2600. :group 'org-font-lock
  2601. :type 'boolean)
  2602. (defcustom org-fontify-whole-heading-line nil
  2603. "Non-nil means fontify the whole line for headings.
  2604. This is useful when setting a background color for the
  2605. org-level-* faces."
  2606. :group 'org-font-lock
  2607. :type 'boolean)
  2608. (defcustom org-highlight-latex-fragments-and-specials nil
  2609. "Non-nil means fontify what is treated specially by the exporters."
  2610. :group 'org-font-lock
  2611. :type 'boolean)
  2612. (defcustom org-hide-emphasis-markers nil
  2613. "Non-nil mean font-lock should hide the emphasis marker characters."
  2614. :group 'org-font-lock
  2615. :type 'boolean)
  2616. (defvar org-emph-re nil
  2617. "Regular expression for matching emphasis.")
  2618. (defvar org-verbatim-re nil
  2619. "Regular expression for matching verbatim text.")
  2620. (defvar org-emphasis-regexp-components) ; defined just below
  2621. (defvar org-emphasis-alist) ; defined just below
  2622. (defun org-set-emph-re (var val)
  2623. "Set variable and compute the emphasis regular expression."
  2624. (set var val)
  2625. (when (and (boundp 'org-emphasis-alist)
  2626. (boundp 'org-emphasis-regexp-components)
  2627. org-emphasis-alist org-emphasis-regexp-components)
  2628. (let* ((e org-emphasis-regexp-components)
  2629. (pre (car e))
  2630. (post (nth 1 e))
  2631. (border (nth 2 e))
  2632. (body (nth 3 e))
  2633. (nl (nth 4 e))
  2634. (body1 (concat body "*?"))
  2635. (markers (mapconcat 'car org-emphasis-alist ""))
  2636. (vmarkers (mapconcat
  2637. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2638. org-emphasis-alist "")))
  2639. ;; make sure special characters appear at the right position in the class
  2640. (if (string-match "\\^" markers)
  2641. (setq markers (concat (replace-match "" t t markers) "^")))
  2642. (if (string-match "-" markers)
  2643. (setq markers (concat (replace-match "" t t markers) "-")))
  2644. (if (string-match "\\^" vmarkers)
  2645. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2646. (if (string-match "-" vmarkers)
  2647. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2648. (if (> nl 0)
  2649. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2650. (int-to-string nl) "\\}")))
  2651. ;; Make the regexp
  2652. (setq org-emph-re
  2653. (concat "\\([" pre "]\\|^\\)"
  2654. "\\("
  2655. "\\([" markers "]\\)"
  2656. "\\("
  2657. "[^" border "]\\|"
  2658. "[^" border "]"
  2659. body1
  2660. "[^" border "]"
  2661. "\\)"
  2662. "\\3\\)"
  2663. "\\([" post "]\\|$\\)"))
  2664. (setq org-verbatim-re
  2665. (concat "\\([" pre "]\\|^\\)"
  2666. "\\("
  2667. "\\([" vmarkers "]\\)"
  2668. "\\("
  2669. "[^" border "]\\|"
  2670. "[^" border "]"
  2671. body1
  2672. "[^" border "]"
  2673. "\\)"
  2674. "\\3\\)"
  2675. "\\([" post "]\\|$\\)")))))
  2676. (defcustom org-emphasis-regexp-components
  2677. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2678. "Components used to build the regular expression for emphasis.
  2679. This is a list with 6 entries. Terminology: In an emphasis string
  2680. like \" *strong word* \", we call the initial space PREMATCH, the final
  2681. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2682. and \"trong wor\" is the body. The different components in this variable
  2683. specify what is allowed/forbidden in each part:
  2684. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2685. post Chars allowed as postmatch. End of line will be allowed too.
  2686. border The chars *forbidden* as border characters.
  2687. body-regexp A regexp like \".\" to match a body character. Don't use
  2688. non-shy groups here, and don't allow newline here.
  2689. newline The maximum number of newlines allowed in an emphasis exp.
  2690. Use customize to modify this, or restart Emacs after changing it."
  2691. :group 'org-font-lock
  2692. :set 'org-set-emph-re
  2693. :type '(list
  2694. (sexp :tag "Allowed chars in pre ")
  2695. (sexp :tag "Allowed chars in post ")
  2696. (sexp :tag "Forbidden chars in border ")
  2697. (sexp :tag "Regexp for body ")
  2698. (integer :tag "number of newlines allowed")
  2699. (option (boolean :tag "Please ignore this button"))))
  2700. (defcustom org-emphasis-alist
  2701. `(("*" bold "<b>" "</b>")
  2702. ("/" italic "<i>" "</i>")
  2703. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2704. ("=" org-code "<code>" "</code>" verbatim)
  2705. ("~" org-verbatim "<code>" "</code>" verbatim)
  2706. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2707. "<del>" "</del>")
  2708. )
  2709. "Special syntax for emphasized text.
  2710. Text starting and ending with a special character will be emphasized, for
  2711. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2712. characters, the face to be used by font-lock for highlighting in Org-mode
  2713. Emacs buffers, and the HTML tags to be used for this.
  2714. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2715. Use customize to modify this, or restart Emacs after changing it."
  2716. :group 'org-font-lock
  2717. :set 'org-set-emph-re
  2718. :type '(repeat
  2719. (list
  2720. (string :tag "Marker character")
  2721. (choice
  2722. (face :tag "Font-lock-face")
  2723. (plist :tag "Face property list"))
  2724. (string :tag "HTML start tag")
  2725. (string :tag "HTML end tag")
  2726. (option (const verbatim)))))
  2727. (defvar org-protecting-blocks
  2728. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2729. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2730. This is needed for font-lock setup.")
  2731. ;;; Miscellaneous options
  2732. (defgroup org-completion nil
  2733. "Completion in Org-mode."
  2734. :tag "Org Completion"
  2735. :group 'org)
  2736. (defcustom org-completion-use-ido nil
  2737. "Non-nil means use ido completion wherever possible.
  2738. Note that `ido-mode' must be active for this variable to be relevant.
  2739. If you decide to turn this variable on, you might well want to turn off
  2740. `org-outline-path-complete-in-steps'.
  2741. See also `org-completion-use-iswitchb'."
  2742. :group 'org-completion
  2743. :type 'boolean)
  2744. (defcustom org-completion-use-iswitchb nil
  2745. "Non-nil means use iswitchb completion wherever possible.
  2746. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2747. If you decide to turn this variable on, you might well want to turn off
  2748. `org-outline-path-complete-in-steps'.
  2749. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  2750. :group 'org-completion
  2751. :type 'boolean)
  2752. (defcustom org-completion-fallback-command 'hippie-expand
  2753. "The expansion command called by \\[org-complete] in normal context.
  2754. Normal means no org-mode-specific context."
  2755. :group 'org-completion
  2756. :type 'function)
  2757. ;;; Functions and variables from their packages
  2758. ;; Declared here to avoid compiler warnings
  2759. ;; XEmacs only
  2760. (defvar outline-mode-menu-heading)
  2761. (defvar outline-mode-menu-show)
  2762. (defvar outline-mode-menu-hide)
  2763. (defvar zmacs-regions) ; XEmacs regions
  2764. ;; Emacs only
  2765. (defvar mark-active)
  2766. ;; Various packages
  2767. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2768. (declare-function calendar-forward-day "cal-move" (arg))
  2769. (declare-function calendar-goto-date "cal-move" (date))
  2770. (declare-function calendar-goto-today "cal-move" ())
  2771. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2772. (defvar calc-embedded-close-formula)
  2773. (defvar calc-embedded-open-formula)
  2774. (declare-function cdlatex-tab "ext:cdlatex" ())
  2775. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2776. (defvar font-lock-unfontify-region-function)
  2777. (declare-function iswitchb-read-buffer "iswitchb"
  2778. (prompt &optional default require-match start matches-set))
  2779. (defvar iswitchb-temp-buflist)
  2780. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2781. (defvar org-agenda-tags-todo-honor-ignore-options)
  2782. (declare-function org-agenda-skip "org-agenda" ())
  2783. (declare-function
  2784. org-format-agenda-item "org-agenda"
  2785. (extra txt &optional category tags dotime noprefix remove-re habitp))
  2786. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2787. (declare-function org-agenda-change-all-lines "org-agenda"
  2788. (newhead hdmarker &optional fixface just-this))
  2789. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2790. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2791. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2792. (beg end))
  2793. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2794. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2795. "org-agenda" (&optional end))
  2796. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2797. (declare-function org-indent-mode "org-indent" (&optional arg))
  2798. (declare-function parse-time-string "parse-time" (string))
  2799. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  2800. (defvar remember-data-file)
  2801. (defvar texmathp-why)
  2802. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2803. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2804. (defvar w3m-current-url)
  2805. (defvar w3m-current-title)
  2806. (defvar org-latex-regexps)
  2807. ;;; Autoload and prepare some org modules
  2808. ;; Some table stuff that needs to be defined here, because it is used
  2809. ;; by the functions setting up org-mode or checking for table context.
  2810. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2811. "Detects an org-type or table-type table.")
  2812. (defconst org-table-line-regexp "^[ \t]*|"
  2813. "Detects an org-type table line.")
  2814. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2815. "Detects an org-type table line.")
  2816. (defconst org-table-hline-regexp "^[ \t]*|-"
  2817. "Detects an org-type table hline.")
  2818. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2819. "Detects a table-type table hline.")
  2820. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2821. "Searching from within a table (any type) this finds the first line
  2822. outside the table.")
  2823. ;; Autoload the functions in org-table.el that are needed by functions here.
  2824. (eval-and-compile
  2825. (org-autoload "org-table"
  2826. '(org-table-align org-table-begin org-table-blank-field
  2827. org-table-convert org-table-convert-region org-table-copy-down
  2828. org-table-copy-region org-table-create
  2829. org-table-create-or-convert-from-region
  2830. org-table-create-with-table.el org-table-current-dline
  2831. org-table-cut-region org-table-delete-column org-table-edit-field
  2832. org-table-edit-formulas org-table-end org-table-eval-formula
  2833. org-table-export org-table-field-info
  2834. org-table-get-stored-formulas org-table-goto-column
  2835. org-table-hline-and-move org-table-import org-table-insert-column
  2836. org-table-insert-hline org-table-insert-row org-table-iterate
  2837. org-table-justify-field-maybe org-table-kill-row
  2838. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2839. org-table-move-column org-table-move-column-left
  2840. org-table-move-column-right org-table-move-row
  2841. org-table-move-row-down org-table-move-row-up
  2842. org-table-next-field org-table-next-row org-table-paste-rectangle
  2843. org-table-previous-field org-table-recalculate
  2844. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2845. org-table-toggle-coordinate-overlays
  2846. org-table-toggle-formula-debugger org-table-wrap-region
  2847. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2848. (defun org-at-table-p (&optional table-type)
  2849. "Return t if the cursor is inside an org-type table.
  2850. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2851. (if org-enable-table-editor
  2852. (save-excursion
  2853. (beginning-of-line 1)
  2854. (looking-at (if table-type org-table-any-line-regexp
  2855. org-table-line-regexp)))
  2856. nil))
  2857. (defsubst org-table-p () (org-at-table-p))
  2858. (defun org-at-table.el-p ()
  2859. "Return t if and only if we are at a table.el table."
  2860. (and (org-at-table-p 'any)
  2861. (save-excursion
  2862. (goto-char (org-table-begin 'any))
  2863. (looking-at org-table1-hline-regexp))))
  2864. (defun org-table-recognize-table.el ()
  2865. "If there is a table.el table nearby, recognize it and move into it."
  2866. (if org-table-tab-recognizes-table.el
  2867. (if (org-at-table.el-p)
  2868. (progn
  2869. (beginning-of-line 1)
  2870. (if (looking-at org-table-dataline-regexp)
  2871. nil
  2872. (if (looking-at org-table1-hline-regexp)
  2873. (progn
  2874. (beginning-of-line 2)
  2875. (if (looking-at org-table-any-border-regexp)
  2876. (beginning-of-line -1)))))
  2877. (if (re-search-forward "|" (org-table-end t) t)
  2878. (progn
  2879. (require 'table)
  2880. (if (table--at-cell-p (point))
  2881. t
  2882. (message "recognizing table.el table...")
  2883. (table-recognize-table)
  2884. (message "recognizing table.el table...done")))
  2885. (error "This should not happen..."))
  2886. t)
  2887. nil)
  2888. nil))
  2889. (defun org-at-table-hline-p ()
  2890. "Return t if the cursor is inside a hline in a table."
  2891. (if org-enable-table-editor
  2892. (save-excursion
  2893. (beginning-of-line 1)
  2894. (looking-at org-table-hline-regexp))
  2895. nil))
  2896. (defvar org-table-clean-did-remove-column nil)
  2897. (defun org-table-map-tables (function)
  2898. "Apply FUNCTION to the start of all tables in the buffer."
  2899. (save-excursion
  2900. (save-restriction
  2901. (widen)
  2902. (goto-char (point-min))
  2903. (while (re-search-forward org-table-any-line-regexp nil t)
  2904. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2905. (beginning-of-line 1)
  2906. (when (looking-at org-table-line-regexp)
  2907. (save-excursion (funcall function))
  2908. (or (looking-at org-table-line-regexp)
  2909. (forward-char 1)))
  2910. (re-search-forward org-table-any-border-regexp nil 1))))
  2911. (message "Mapping tables: done"))
  2912. ;; Declare and autoload functions from org-exp.el & Co
  2913. (declare-function org-default-export-plist "org-exp")
  2914. (declare-function org-infile-export-plist "org-exp")
  2915. (declare-function org-get-current-options "org-exp")
  2916. (eval-and-compile
  2917. (org-autoload "org-exp"
  2918. '(org-export org-export-visible
  2919. org-insert-export-options-template
  2920. org-table-clean-before-export))
  2921. (org-autoload "org-ascii"
  2922. '(org-export-as-ascii org-export-ascii-preprocess
  2923. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2924. org-export-region-as-ascii))
  2925. (org-autoload "org-latex"
  2926. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  2927. org-replace-region-by-latex org-export-region-as-latex
  2928. org-export-as-latex org-export-as-pdf
  2929. org-export-as-pdf-and-open))
  2930. (org-autoload "org-html"
  2931. '(org-export-as-html-and-open
  2932. org-export-as-html-batch org-export-as-html-to-buffer
  2933. org-replace-region-by-html org-export-region-as-html
  2934. org-export-as-html))
  2935. (org-autoload "org-docbook"
  2936. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  2937. org-replace-region-by-docbook org-export-region-as-docbook
  2938. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  2939. org-export-as-docbook))
  2940. (org-autoload "org-icalendar"
  2941. '(org-export-icalendar-this-file
  2942. org-export-icalendar-all-agenda-files
  2943. org-export-icalendar-combine-agenda-files))
  2944. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  2945. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  2946. ;; Declare and autoload functions from org-agenda.el
  2947. (eval-and-compile
  2948. (org-autoload "org-agenda"
  2949. '(org-agenda org-agenda-list org-search-view
  2950. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2951. org-diary org-agenda-to-appt
  2952. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2953. ;; Autoload org-remember
  2954. (eval-and-compile
  2955. (org-autoload "org-remember"
  2956. '(org-remember-insinuate org-remember-annotation
  2957. org-remember-apply-template org-remember org-remember-handler)))
  2958. ;; Autoload org-clock.el
  2959. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2960. (beg end))
  2961. (declare-function org-clock-update-mode-line "org-clock" ())
  2962. (declare-function org-resolve-clocks "org-clock"
  2963. (&optional also-non-dangling-p prompt last-valid))
  2964. (defvar org-clock-start-time)
  2965. (defvar org-clock-marker (make-marker)
  2966. "Marker recording the last clock-in.")
  2967. (defvar org-clock-hd-marker (make-marker)
  2968. "Marker recording the last clock-in, but the headline position.")
  2969. (defvar org-clock-heading ""
  2970. "The heading of the current clock entry.")
  2971. (defun org-clock-is-active ()
  2972. "Return non-nil if clock is currently running.
  2973. The return value is actually the clock marker."
  2974. (marker-buffer org-clock-marker))
  2975. (eval-and-compile
  2976. (org-autoload
  2977. "org-clock"
  2978. '(org-clock-in org-clock-out org-clock-cancel
  2979. org-clock-goto org-clock-sum org-clock-display
  2980. org-clock-remove-overlays org-clock-report
  2981. org-clocktable-shift org-dblock-write:clocktable
  2982. org-get-clocktable org-resolve-clocks)))
  2983. (defun org-clock-update-time-maybe ()
  2984. "If this is a CLOCK line, update it and return t.
  2985. Otherwise, return nil."
  2986. (interactive)
  2987. (save-excursion
  2988. (beginning-of-line 1)
  2989. (skip-chars-forward " \t")
  2990. (when (looking-at org-clock-string)
  2991. (let ((re (concat "[ \t]*" org-clock-string
  2992. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2993. "\\([ \t]*=>.*\\)?\\)?"))
  2994. ts te h m s neg)
  2995. (cond
  2996. ((not (looking-at re))
  2997. nil)
  2998. ((not (match-end 2))
  2999. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3000. (> org-clock-marker (point))
  3001. (<= org-clock-marker (point-at-eol)))
  3002. ;; The clock is running here
  3003. (setq org-clock-start-time
  3004. (apply 'encode-time
  3005. (org-parse-time-string (match-string 1))))
  3006. (org-clock-update-mode-line)))
  3007. (t
  3008. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3009. (end-of-line 1)
  3010. (setq ts (match-string 1)
  3011. te (match-string 3))
  3012. (setq s (- (org-float-time
  3013. (apply 'encode-time (org-parse-time-string te)))
  3014. (org-float-time
  3015. (apply 'encode-time (org-parse-time-string ts))))
  3016. neg (< s 0)
  3017. s (abs s)
  3018. h (floor (/ s 3600))
  3019. s (- s (* 3600 h))
  3020. m (floor (/ s 60))
  3021. s (- s (* 60 s)))
  3022. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3023. t))))))
  3024. (defun org-check-running-clock ()
  3025. "Check if the current buffer contains the running clock.
  3026. If yes, offer to stop it and to save the buffer with the changes."
  3027. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3028. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3029. (buffer-name))))
  3030. (org-clock-out)
  3031. (when (y-or-n-p "Save changed buffer?")
  3032. (save-buffer))))
  3033. (defun org-clocktable-try-shift (dir n)
  3034. "Check if this line starts a clock table, if yes, shift the time block."
  3035. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  3036. (org-clocktable-shift dir n)))
  3037. ;; Autoload org-timer.el
  3038. (eval-and-compile
  3039. (org-autoload
  3040. "org-timer"
  3041. '(org-timer-start org-timer org-timer-item
  3042. org-timer-change-times-in-region
  3043. org-timer-set-timer
  3044. org-timer-reset-timers
  3045. org-timer-show-remaining-time)))
  3046. ;; Autoload org-feed.el
  3047. (eval-and-compile
  3048. (org-autoload
  3049. "org-feed"
  3050. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3051. ;; Autoload org-indent.el
  3052. ;; Define the variable already here, to make sure we have it.
  3053. (defvar org-indent-mode nil
  3054. "Non-nil if Org-Indent mode is enabled.
  3055. Use the command `org-indent-mode' to change this variable.")
  3056. (eval-and-compile
  3057. (org-autoload
  3058. "org-indent"
  3059. '(org-indent-mode)))
  3060. ;; Autoload org-mobile.el
  3061. (eval-and-compile
  3062. (org-autoload
  3063. "org-mobile"
  3064. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3065. ;; Autoload archiving code
  3066. ;; The stuff that is needed for cycling and tags has to be defined here.
  3067. (defgroup org-archive nil
  3068. "Options concerning archiving in Org-mode."
  3069. :tag "Org Archive"
  3070. :group 'org-structure)
  3071. (defcustom org-archive-location "%s_archive::"
  3072. "The location where subtrees should be archived.
  3073. The value of this variable is a string, consisting of two parts,
  3074. separated by a double-colon. The first part is a filename and
  3075. the second part is a headline.
  3076. When the filename is omitted, archiving happens in the same file.
  3077. %s in the filename will be replaced by the current file
  3078. name (without the directory part). Archiving to a different file
  3079. is useful to keep archived entries from contributing to the
  3080. Org-mode Agenda.
  3081. The archived entries will be filed as subtrees of the specified
  3082. headline. When the headline is omitted, the subtrees are simply
  3083. filed away at the end of the file, as top-level entries. Also in
  3084. the heading you can use %s to represent the file name, this can be
  3085. useful when using the same archive for a number of different files.
  3086. Here are a few examples:
  3087. \"%s_archive::\"
  3088. If the current file is Projects.org, archive in file
  3089. Projects.org_archive, as top-level trees. This is the default.
  3090. \"::* Archived Tasks\"
  3091. Archive in the current file, under the top-level headline
  3092. \"* Archived Tasks\".
  3093. \"~/org/archive.org::\"
  3094. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3095. \"~/org/archive.org::From %s\"
  3096. Archive in file ~/org/archive.org (absolute path), under headlines
  3097. \"From FILENAME\" where file name is the current file name.
  3098. \"basement::** Finished Tasks\"
  3099. Archive in file ./basement (relative path), as level 3 trees
  3100. below the level 2 heading \"** Finished Tasks\".
  3101. You may set this option on a per-file basis by adding to the buffer a
  3102. line like
  3103. #+ARCHIVE: basement::** Finished Tasks
  3104. You may also define it locally for a subtree by setting an ARCHIVE property
  3105. in the entry. If such a property is found in an entry, or anywhere up
  3106. the hierarchy, it will be used."
  3107. :group 'org-archive
  3108. :type 'string)
  3109. (defcustom org-archive-tag "ARCHIVE"
  3110. "The tag that marks a subtree as archived.
  3111. An archived subtree does not open during visibility cycling, and does
  3112. not contribute to the agenda listings.
  3113. After changing this, font-lock must be restarted in the relevant buffers to
  3114. get the proper fontification."
  3115. :group 'org-archive
  3116. :group 'org-keywords
  3117. :type 'string)
  3118. (defcustom org-agenda-skip-archived-trees t
  3119. "Non-nil means the agenda will skip any items located in archived trees.
  3120. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3121. variable is no longer recommended, you should leave it at the value t.
  3122. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3123. :group 'org-archive
  3124. :group 'org-agenda-skip
  3125. :type 'boolean)
  3126. (defcustom org-columns-skip-archived-trees t
  3127. "Non-nil means ignore archived trees when creating column view."
  3128. :group 'org-archive
  3129. :group 'org-properties
  3130. :type 'boolean)
  3131. (defcustom org-cycle-open-archived-trees nil
  3132. "Non-nil means `org-cycle' will open archived trees.
  3133. An archived tree is a tree marked with the tag ARCHIVE.
  3134. When nil, archived trees will stay folded. You can still open them with
  3135. normal outline commands like `show-all', but not with the cycling commands."
  3136. :group 'org-archive
  3137. :group 'org-cycle
  3138. :type 'boolean)
  3139. (defcustom org-sparse-tree-open-archived-trees nil
  3140. "Non-nil means sparse tree construction shows matches in archived trees.
  3141. When nil, matches in these trees are highlighted, but the trees are kept in
  3142. collapsed state."
  3143. :group 'org-archive
  3144. :group 'org-sparse-trees
  3145. :type 'boolean)
  3146. (defun org-cycle-hide-archived-subtrees (state)
  3147. "Re-hide all archived subtrees after a visibility state change."
  3148. (when (and (not org-cycle-open-archived-trees)
  3149. (not (memq state '(overview folded))))
  3150. (save-excursion
  3151. (let* ((globalp (memq state '(contents all)))
  3152. (beg (if globalp (point-min) (point)))
  3153. (end (if globalp (point-max) (org-end-of-subtree t))))
  3154. (org-hide-archived-subtrees beg end)
  3155. (goto-char beg)
  3156. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3157. (message "%s" (substitute-command-keys
  3158. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3159. (defun org-force-cycle-archived ()
  3160. "Cycle subtree even if it is archived."
  3161. (interactive)
  3162. (setq this-command 'org-cycle)
  3163. (let ((org-cycle-open-archived-trees t))
  3164. (call-interactively 'org-cycle)))
  3165. (defun org-hide-archived-subtrees (beg end)
  3166. "Re-hide all archived subtrees after a visibility state change."
  3167. (save-excursion
  3168. (let* ((re (concat ":" org-archive-tag ":")))
  3169. (goto-char beg)
  3170. (while (re-search-forward re end t)
  3171. (when (org-on-heading-p)
  3172. (org-flag-subtree t)
  3173. (org-end-of-subtree t))))))
  3174. (defun org-flag-subtree (flag)
  3175. (save-excursion
  3176. (org-back-to-heading t)
  3177. (outline-end-of-heading)
  3178. (outline-flag-region (point)
  3179. (progn (org-end-of-subtree t) (point))
  3180. flag)))
  3181. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3182. (eval-and-compile
  3183. (org-autoload "org-archive"
  3184. '(org-add-archive-files org-archive-subtree
  3185. org-archive-to-archive-sibling org-toggle-archive-tag
  3186. org-archive-subtree-default
  3187. org-archive-subtree-default-with-confirmation)))
  3188. ;; Autoload Column View Code
  3189. (declare-function org-columns-number-to-string "org-colview")
  3190. (declare-function org-columns-get-format-and-top-level "org-colview")
  3191. (declare-function org-columns-compute "org-colview")
  3192. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3193. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3194. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3195. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3196. ;; Autoload ID code
  3197. (declare-function org-id-store-link "org-id")
  3198. (declare-function org-id-locations-load "org-id")
  3199. (declare-function org-id-locations-save "org-id")
  3200. (defvar org-id-track-globally)
  3201. (org-autoload "org-id"
  3202. '(org-id-get-create org-id-new org-id-copy org-id-get
  3203. org-id-get-with-outline-path-completion
  3204. org-id-get-with-outline-drilling
  3205. org-id-goto org-id-find org-id-store-link))
  3206. ;; Autoload Plotting Code
  3207. (org-autoload "org-plot"
  3208. '(org-plot/gnuplot))
  3209. ;;; Variables for pre-computed regular expressions, all buffer local
  3210. (defvar org-drawer-regexp nil
  3211. "Matches first line of a hidden block.")
  3212. (make-variable-buffer-local 'org-drawer-regexp)
  3213. (defvar org-todo-regexp nil
  3214. "Matches any of the TODO state keywords.")
  3215. (make-variable-buffer-local 'org-todo-regexp)
  3216. (defvar org-not-done-regexp nil
  3217. "Matches any of the TODO state keywords except the last one.")
  3218. (make-variable-buffer-local 'org-not-done-regexp)
  3219. (defvar org-not-done-heading-regexp nil
  3220. "Matches a TODO headline that is not done.")
  3221. (make-variable-buffer-local 'org-not-done-regexp)
  3222. (defvar org-todo-line-regexp nil
  3223. "Matches a headline and puts TODO state into group 2 if present.")
  3224. (make-variable-buffer-local 'org-todo-line-regexp)
  3225. (defvar org-complex-heading-regexp nil
  3226. "Matches a headline and puts everything into groups:
  3227. group 1: the stars
  3228. group 2: The todo keyword, maybe
  3229. group 3: Priority cookie
  3230. group 4: True headline
  3231. group 5: Tags")
  3232. (make-variable-buffer-local 'org-complex-heading-regexp)
  3233. (defvar org-complex-heading-regexp-format nil)
  3234. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3235. (defvar org-todo-line-tags-regexp nil
  3236. "Matches a headline and puts TODO state into group 2 if present.
  3237. Also put tags into group 4 if tags are present.")
  3238. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3239. (defvar org-nl-done-regexp nil
  3240. "Matches newline followed by a headline with the DONE keyword.")
  3241. (make-variable-buffer-local 'org-nl-done-regexp)
  3242. (defvar org-looking-at-done-regexp nil
  3243. "Matches the DONE keyword a point.")
  3244. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3245. (defvar org-ds-keyword-length 12
  3246. "Maximum length of the Deadline and SCHEDULED keywords.")
  3247. (make-variable-buffer-local 'org-ds-keyword-length)
  3248. (defvar org-deadline-regexp nil
  3249. "Matches the DEADLINE keyword.")
  3250. (make-variable-buffer-local 'org-deadline-regexp)
  3251. (defvar org-deadline-time-regexp nil
  3252. "Matches the DEADLINE keyword together with a time stamp.")
  3253. (make-variable-buffer-local 'org-deadline-time-regexp)
  3254. (defvar org-deadline-line-regexp nil
  3255. "Matches the DEADLINE keyword and the rest of the line.")
  3256. (make-variable-buffer-local 'org-deadline-line-regexp)
  3257. (defvar org-scheduled-regexp nil
  3258. "Matches the SCHEDULED keyword.")
  3259. (make-variable-buffer-local 'org-scheduled-regexp)
  3260. (defvar org-scheduled-time-regexp nil
  3261. "Matches the SCHEDULED keyword together with a time stamp.")
  3262. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3263. (defvar org-closed-time-regexp nil
  3264. "Matches the CLOSED keyword together with a time stamp.")
  3265. (make-variable-buffer-local 'org-closed-time-regexp)
  3266. (defvar org-keyword-time-regexp nil
  3267. "Matches any of the 4 keywords, together with the time stamp.")
  3268. (make-variable-buffer-local 'org-keyword-time-regexp)
  3269. (defvar org-keyword-time-not-clock-regexp nil
  3270. "Matches any of the 3 keywords, together with the time stamp.")
  3271. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3272. (defvar org-maybe-keyword-time-regexp nil
  3273. "Matches a timestamp, possibly preceeded by a keyword.")
  3274. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3275. (defvar org-planning-or-clock-line-re nil
  3276. "Matches a line with planning or clock info.")
  3277. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3278. (defvar org-all-time-keywords nil
  3279. "List of time keywords.")
  3280. (make-variable-buffer-local 'org-all-time-keywords)
  3281. (defconst org-plain-time-of-day-regexp
  3282. (concat
  3283. "\\(\\<[012]?[0-9]"
  3284. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3285. "\\(--?"
  3286. "\\(\\<[012]?[0-9]"
  3287. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3288. "\\)?")
  3289. "Regular expression to match a plain time or time range.
  3290. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3291. groups carry important information:
  3292. 0 the full match
  3293. 1 the first time, range or not
  3294. 8 the second time, if it is a range.")
  3295. (defconst org-plain-time-extension-regexp
  3296. (concat
  3297. "\\(\\<[012]?[0-9]"
  3298. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3299. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3300. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3301. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3302. groups carry important information:
  3303. 0 the full match
  3304. 7 hours of duration
  3305. 9 minutes of duration")
  3306. (defconst org-stamp-time-of-day-regexp
  3307. (concat
  3308. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3309. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3310. "\\(--?"
  3311. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3312. "Regular expression to match a timestamp time or time range.
  3313. After a match, the following groups carry important information:
  3314. 0 the full match
  3315. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3316. 2 the first time, range or not
  3317. 4 the second time, if it is a range.")
  3318. (defconst org-startup-options
  3319. '(("fold" org-startup-folded t)
  3320. ("overview" org-startup-folded t)
  3321. ("nofold" org-startup-folded nil)
  3322. ("showall" org-startup-folded nil)
  3323. ("showeverything" org-startup-folded showeverything)
  3324. ("content" org-startup-folded content)
  3325. ("indent" org-startup-indented t)
  3326. ("noindent" org-startup-indented nil)
  3327. ("hidestars" org-hide-leading-stars t)
  3328. ("showstars" org-hide-leading-stars nil)
  3329. ("odd" org-odd-levels-only t)
  3330. ("oddeven" org-odd-levels-only nil)
  3331. ("align" org-startup-align-all-tables t)
  3332. ("noalign" org-startup-align-all-tables nil)
  3333. ("customtime" org-display-custom-times t)
  3334. ("logdone" org-log-done time)
  3335. ("lognotedone" org-log-done note)
  3336. ("nologdone" org-log-done nil)
  3337. ("lognoteclock-out" org-log-note-clock-out t)
  3338. ("nolognoteclock-out" org-log-note-clock-out nil)
  3339. ("logrepeat" org-log-repeat state)
  3340. ("lognoterepeat" org-log-repeat note)
  3341. ("nologrepeat" org-log-repeat nil)
  3342. ("logreschedule" org-log-reschedule time)
  3343. ("lognotereschedule" org-log-reschedule note)
  3344. ("nologreschedule" org-log-reschedule nil)
  3345. ("logredeadline" org-log-redeadline time)
  3346. ("lognoteredeadline" org-log-redeadline note)
  3347. ("nologredeadline" org-log-redeadline nil)
  3348. ("logrefile" org-log-refile time)
  3349. ("lognoterefile" org-log-refile note)
  3350. ("nologrefile" org-log-refile nil)
  3351. ("fninline" org-footnote-define-inline t)
  3352. ("nofninline" org-footnote-define-inline nil)
  3353. ("fnlocal" org-footnote-section nil)
  3354. ("fnauto" org-footnote-auto-label t)
  3355. ("fnprompt" org-footnote-auto-label nil)
  3356. ("fnconfirm" org-footnote-auto-label confirm)
  3357. ("fnplain" org-footnote-auto-label plain)
  3358. ("fnadjust" org-footnote-auto-adjust t)
  3359. ("nofnadjust" org-footnote-auto-adjust nil)
  3360. ("constcgs" constants-unit-system cgs)
  3361. ("constSI" constants-unit-system SI)
  3362. ("noptag" org-tag-persistent-alist nil)
  3363. ("hideblocks" org-hide-block-startup t)
  3364. ("nohideblocks" org-hide-block-startup nil)
  3365. ("beamer" org-startup-with-beamer-mode t))
  3366. "Variable associated with STARTUP options for org-mode.
  3367. Each element is a list of three items: The startup options as written
  3368. in the #+STARTUP line, the corresponding variable, and the value to
  3369. set this variable to if the option is found. An optional forth element PUSH
  3370. means to push this value onto the list in the variable.")
  3371. (defun org-set-regexps-and-options ()
  3372. "Precompute regular expressions for current buffer."
  3373. (when (org-mode-p)
  3374. (org-set-local 'org-todo-kwd-alist nil)
  3375. (org-set-local 'org-todo-key-alist nil)
  3376. (org-set-local 'org-todo-key-trigger nil)
  3377. (org-set-local 'org-todo-keywords-1 nil)
  3378. (org-set-local 'org-done-keywords nil)
  3379. (org-set-local 'org-todo-heads nil)
  3380. (org-set-local 'org-todo-sets nil)
  3381. (org-set-local 'org-todo-log-states nil)
  3382. (org-set-local 'org-file-properties nil)
  3383. (org-set-local 'org-file-tags nil)
  3384. (let ((re (org-make-options-regexp
  3385. '("CATEGORY" "TODO" "COLUMNS"
  3386. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3387. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS")
  3388. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3389. (splitre "[ \t]+")
  3390. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3391. tail sep kws1 prio props ftags drawers beamer-p
  3392. ext-setup-or-nil setup-contents (start 0))
  3393. (save-excursion
  3394. (save-restriction
  3395. (widen)
  3396. (goto-char (point-min))
  3397. (while (or (and ext-setup-or-nil
  3398. (string-match re ext-setup-or-nil start)
  3399. (setq start (match-end 0)))
  3400. (and (setq ext-setup-or-nil nil start 0)
  3401. (re-search-forward re nil t)))
  3402. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3403. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3404. (cond
  3405. ((equal key "CATEGORY")
  3406. (if (string-match "[ \t]+$" value)
  3407. (setq value (replace-match "" t t value)))
  3408. (setq cat value))
  3409. ((member key '("SEQ_TODO" "TODO"))
  3410. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3411. ((equal key "TYP_TODO")
  3412. (push (cons 'type (org-split-string value splitre)) kwds))
  3413. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3414. ;; general TODO-like setup
  3415. (push (cons (intern (downcase (match-string 1 key)))
  3416. (org-split-string value splitre)) kwds))
  3417. ((equal key "TAGS")
  3418. (setq tags (append tags (if tags '("\\n") nil)
  3419. (org-split-string value splitre))))
  3420. ((equal key "COLUMNS")
  3421. (org-set-local 'org-columns-default-format value))
  3422. ((equal key "LINK")
  3423. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3424. (push (cons (match-string 1 value)
  3425. (org-trim (match-string 2 value)))
  3426. links)))
  3427. ((equal key "PRIORITIES")
  3428. (setq prio (org-split-string value " +")))
  3429. ((equal key "PROPERTY")
  3430. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3431. (push (cons (match-string 1 value) (match-string 2 value))
  3432. props)))
  3433. ((equal key "FILETAGS")
  3434. (when (string-match "\\S-" value)
  3435. (setq ftags
  3436. (append
  3437. ftags
  3438. (apply 'append
  3439. (mapcar (lambda (x) (org-split-string x ":"))
  3440. (org-split-string value)))))))
  3441. ((equal key "DRAWERS")
  3442. (setq drawers (org-split-string value splitre)))
  3443. ((equal key "CONSTANTS")
  3444. (setq const (append const (org-split-string value splitre))))
  3445. ((equal key "STARTUP")
  3446. (let ((opts (org-split-string value splitre))
  3447. l var val)
  3448. (while (setq l (pop opts))
  3449. (when (setq l (assoc l org-startup-options))
  3450. (setq var (nth 1 l) val (nth 2 l))
  3451. (if (not (nth 3 l))
  3452. (set (make-local-variable var) val)
  3453. (if (not (listp (symbol-value var)))
  3454. (set (make-local-variable var) nil))
  3455. (set (make-local-variable var) (symbol-value var))
  3456. (add-to-list var val))))))
  3457. ((equal key "ARCHIVE")
  3458. (string-match " *$" value)
  3459. (setq arch (replace-match "" t t value))
  3460. (remove-text-properties 0 (length arch)
  3461. '(face t fontified t) arch))
  3462. ((equal key "LATEX_CLASS")
  3463. (setq beamer-p (equal value "beamer")))
  3464. ((equal key "SETUPFILE")
  3465. (setq setup-contents (org-file-contents
  3466. (expand-file-name
  3467. (org-remove-double-quotes value))
  3468. 'noerror))
  3469. (if (not ext-setup-or-nil)
  3470. (setq ext-setup-or-nil setup-contents start 0)
  3471. (setq ext-setup-or-nil
  3472. (concat (substring ext-setup-or-nil 0 start)
  3473. "\n" setup-contents "\n"
  3474. (substring ext-setup-or-nil start)))))
  3475. ))))
  3476. (when cat
  3477. (org-set-local 'org-category (intern cat))
  3478. (push (cons "CATEGORY" cat) props))
  3479. (when prio
  3480. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3481. (setq prio (mapcar 'string-to-char prio))
  3482. (org-set-local 'org-highest-priority (nth 0 prio))
  3483. (org-set-local 'org-lowest-priority (nth 1 prio))
  3484. (org-set-local 'org-default-priority (nth 2 prio)))
  3485. (and props (org-set-local 'org-file-properties (nreverse props)))
  3486. (and ftags (org-set-local 'org-file-tags
  3487. (mapcar 'org-add-prop-inherited ftags)))
  3488. (and drawers (org-set-local 'org-drawers drawers))
  3489. (and arch (org-set-local 'org-archive-location arch))
  3490. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3491. ;; Process the TODO keywords
  3492. (unless kwds
  3493. ;; Use the global values as if they had been given locally.
  3494. (setq kwds (default-value 'org-todo-keywords))
  3495. (if (stringp (car kwds))
  3496. (setq kwds (list (cons org-todo-interpretation
  3497. (default-value 'org-todo-keywords)))))
  3498. (setq kwds (reverse kwds)))
  3499. (setq kwds (nreverse kwds))
  3500. (let (inter kws kw)
  3501. (while (setq kws (pop kwds))
  3502. (let ((kws (or
  3503. (run-hook-with-args-until-success
  3504. 'org-todo-setup-filter-hook kws)
  3505. kws)))
  3506. (setq inter (pop kws) sep (member "|" kws)
  3507. kws0 (delete "|" (copy-sequence kws))
  3508. kwsa nil
  3509. kws1 (mapcar
  3510. (lambda (x)
  3511. ;; 1 2
  3512. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3513. (progn
  3514. (setq kw (match-string 1 x)
  3515. key (and (match-end 2) (match-string 2 x))
  3516. log (org-extract-log-state-settings x))
  3517. (push (cons kw (and key (string-to-char key))) kwsa)
  3518. (and log (push log org-todo-log-states))
  3519. kw)
  3520. (error "Invalid TODO keyword %s" x)))
  3521. kws0)
  3522. kwsa (if kwsa (append '((:startgroup))
  3523. (nreverse kwsa)
  3524. '((:endgroup))))
  3525. hw (car kws1)
  3526. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3527. tail (list inter hw (car dws) (org-last dws))))
  3528. (add-to-list 'org-todo-heads hw 'append)
  3529. (push kws1 org-todo-sets)
  3530. (setq org-done-keywords (append org-done-keywords dws nil))
  3531. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3532. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3533. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3534. (setq org-todo-sets (nreverse org-todo-sets)
  3535. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3536. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3537. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3538. ;; Process the constants
  3539. (when const
  3540. (let (e cst)
  3541. (while (setq e (pop const))
  3542. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3543. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3544. (setq org-table-formula-constants-local cst)))
  3545. ;; Process the tags.
  3546. (when tags
  3547. (let (e tgs)
  3548. (while (setq e (pop tags))
  3549. (cond
  3550. ((equal e "{") (push '(:startgroup) tgs))
  3551. ((equal e "}") (push '(:endgroup) tgs))
  3552. ((equal e "\\n") (push '(:newline) tgs))
  3553. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3554. (push (cons (match-string 1 e)
  3555. (string-to-char (match-string 2 e)))
  3556. tgs))
  3557. (t (push (list e) tgs))))
  3558. (org-set-local 'org-tag-alist nil)
  3559. (while (setq e (pop tgs))
  3560. (or (and (stringp (car e))
  3561. (assoc (car e) org-tag-alist))
  3562. (push e org-tag-alist)))))
  3563. ;; Compute the regular expressions and other local variables
  3564. (if (not org-done-keywords)
  3565. (setq org-done-keywords (and org-todo-keywords-1
  3566. (list (org-last org-todo-keywords-1)))))
  3567. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3568. (length org-scheduled-string)
  3569. (length org-clock-string)
  3570. (length org-closed-string)))
  3571. org-drawer-regexp
  3572. (concat "^[ \t]*:\\("
  3573. (mapconcat 'regexp-quote org-drawers "\\|")
  3574. "\\):[ \t]*$")
  3575. org-not-done-keywords
  3576. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3577. org-todo-regexp
  3578. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3579. "\\|") "\\)\\>")
  3580. org-not-done-regexp
  3581. (concat "\\<\\("
  3582. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3583. "\\)\\>")
  3584. org-not-done-heading-regexp
  3585. (concat "^\\(\\*+\\)[ \t]+\\("
  3586. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3587. "\\)\\>")
  3588. org-todo-line-regexp
  3589. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3590. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3591. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3592. org-complex-heading-regexp
  3593. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3594. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3595. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3596. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3597. org-complex-heading-regexp-format
  3598. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3599. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3600. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
  3601. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3602. org-nl-done-regexp
  3603. (concat "\n\\*+[ \t]+"
  3604. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3605. "\\)" "\\>")
  3606. org-todo-line-tags-regexp
  3607. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3608. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3609. (org-re
  3610. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3611. org-looking-at-done-regexp
  3612. (concat "^" "\\(?:"
  3613. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3614. "\\>")
  3615. org-deadline-regexp (concat "\\<" org-deadline-string)
  3616. org-deadline-time-regexp
  3617. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3618. org-deadline-line-regexp
  3619. (concat "\\<\\(" org-deadline-string "\\).*")
  3620. org-scheduled-regexp
  3621. (concat "\\<" org-scheduled-string)
  3622. org-scheduled-time-regexp
  3623. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3624. org-closed-time-regexp
  3625. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3626. org-keyword-time-regexp
  3627. (concat "\\<\\(" org-scheduled-string
  3628. "\\|" org-deadline-string
  3629. "\\|" org-closed-string
  3630. "\\|" org-clock-string "\\)"
  3631. " *[[<]\\([^]>]+\\)[]>]")
  3632. org-keyword-time-not-clock-regexp
  3633. (concat "\\<\\(" org-scheduled-string
  3634. "\\|" org-deadline-string
  3635. "\\|" org-closed-string
  3636. "\\)"
  3637. " *[[<]\\([^]>]+\\)[]>]")
  3638. org-maybe-keyword-time-regexp
  3639. (concat "\\(\\<\\(" org-scheduled-string
  3640. "\\|" org-deadline-string
  3641. "\\|" org-closed-string
  3642. "\\|" org-clock-string "\\)\\)?"
  3643. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3644. org-planning-or-clock-line-re
  3645. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3646. "\\|" org-deadline-string
  3647. "\\|" org-closed-string "\\|" org-clock-string
  3648. "\\)\\>\\)")
  3649. org-all-time-keywords
  3650. (mapcar (lambda (w) (substring w 0 -1))
  3651. (list org-scheduled-string org-deadline-string
  3652. org-clock-string org-closed-string))
  3653. )
  3654. (org-compute-latex-and-specials-regexp)
  3655. (org-set-font-lock-defaults))))
  3656. (defun org-file-contents (file &optional noerror)
  3657. "Return the contents of FILE, as a string."
  3658. (if (or (not file)
  3659. (not (file-readable-p file)))
  3660. (if noerror
  3661. (progn
  3662. (message "Cannot read file %s" file)
  3663. (ding) (sit-for 2)
  3664. "")
  3665. (error "Cannot read file %s" file))
  3666. (with-temp-buffer
  3667. (insert-file-contents file)
  3668. (buffer-string))))
  3669. (defun org-extract-log-state-settings (x)
  3670. "Extract the log state setting from a TODO keyword string.
  3671. This will extract info from a string like \"WAIT(w@/!)\"."
  3672. (let (kw key log1 log2)
  3673. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3674. (setq kw (match-string 1 x)
  3675. key (and (match-end 2) (match-string 2 x))
  3676. log1 (and (match-end 3) (match-string 3 x))
  3677. log2 (and (match-end 4) (match-string 4 x)))
  3678. (and (or log1 log2)
  3679. (list kw
  3680. (and log1 (if (equal log1 "!") 'time 'note))
  3681. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3682. (defun org-remove-keyword-keys (list)
  3683. "Remove a pair of parenthesis at the end of each string in LIST."
  3684. (mapcar (lambda (x)
  3685. (if (string-match "(.*)$" x)
  3686. (substring x 0 (match-beginning 0))
  3687. x))
  3688. list))
  3689. (defun org-assign-fast-keys (alist)
  3690. "Assign fast keys to a keyword-key alist.
  3691. Respect keys that are already there."
  3692. (let (new e (alt ?0))
  3693. (while (setq e (pop alist))
  3694. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  3695. (cdr e)) ;; Key already assigned.
  3696. (push e new)
  3697. (let ((clist (string-to-list (downcase (car e))))
  3698. (used (append new alist)))
  3699. (when (= (car clist) ?@)
  3700. (pop clist))
  3701. (while (and clist (rassoc (car clist) used))
  3702. (pop clist))
  3703. (unless clist
  3704. (while (rassoc alt used)
  3705. (incf alt)))
  3706. (push (cons (car e) (or (car clist) alt)) new))))
  3707. (nreverse new)))
  3708. ;;; Some variables used in various places
  3709. (defvar org-window-configuration nil
  3710. "Used in various places to store a window configuration.")
  3711. (defvar org-selected-window nil
  3712. "Used in various places to store a window configuration.")
  3713. (defvar org-finish-function nil
  3714. "Function to be called when `C-c C-c' is used.
  3715. This is for getting out of special buffers like remember.")
  3716. ;; FIXME: Occasionally check by commenting these, to make sure
  3717. ;; no other functions uses these, forgetting to let-bind them.
  3718. (defvar entry)
  3719. (defvar last-state)
  3720. (defvar date)
  3721. ;; Defined somewhere in this file, but used before definition.
  3722. (defvar org-html-entities)
  3723. (defvar org-struct-menu)
  3724. (defvar org-org-menu)
  3725. (defvar org-tbl-menu)
  3726. ;;;; Define the Org-mode
  3727. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3728. (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."))
  3729. ;; We use a before-change function to check if a table might need
  3730. ;; an update.
  3731. (defvar org-table-may-need-update t
  3732. "Indicates that a table might need an update.
  3733. This variable is set by `org-before-change-function'.
  3734. `org-table-align' sets it back to nil.")
  3735. (defun org-before-change-function (beg end)
  3736. "Every change indicates that a table might need an update."
  3737. (setq org-table-may-need-update t))
  3738. (defvar org-mode-map)
  3739. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3740. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  3741. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3742. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3743. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3744. (defvar org-table-buffer-is-an nil)
  3745. (defconst org-outline-regexp "\\*+ ")
  3746. ;;;###autoload
  3747. (define-derived-mode org-mode outline-mode "Org"
  3748. "Outline-based notes management and organizer, alias
  3749. \"Carsten's outline-mode for keeping track of everything.\"
  3750. Org-mode develops organizational tasks around a NOTES file which
  3751. contains information about projects as plain text. Org-mode is
  3752. implemented on top of outline-mode, which is ideal to keep the content
  3753. of large files well structured. It supports ToDo items, deadlines and
  3754. time stamps, which magically appear in the diary listing of the Emacs
  3755. calendar. Tables are easily created with a built-in table editor.
  3756. Plain text URL-like links connect to websites, emails (VM), Usenet
  3757. messages (Gnus), BBDB entries, and any files related to the project.
  3758. For printing and sharing of notes, an Org-mode file (or a part of it)
  3759. can be exported as a structured ASCII or HTML file.
  3760. The following commands are available:
  3761. \\{org-mode-map}"
  3762. ;; Get rid of Outline menus, they are not needed
  3763. ;; Need to do this here because define-derived-mode sets up
  3764. ;; the keymap so late. Still, it is a waste to call this each time
  3765. ;; we switch another buffer into org-mode.
  3766. (if (featurep 'xemacs)
  3767. (when (boundp 'outline-mode-menu-heading)
  3768. ;; Assume this is Greg's port, it used easymenu
  3769. (easy-menu-remove outline-mode-menu-heading)
  3770. (easy-menu-remove outline-mode-menu-show)
  3771. (easy-menu-remove outline-mode-menu-hide))
  3772. (define-key org-mode-map [menu-bar headings] 'undefined)
  3773. (define-key org-mode-map [menu-bar hide] 'undefined)
  3774. (define-key org-mode-map [menu-bar show] 'undefined))
  3775. (org-load-modules-maybe)
  3776. (easy-menu-add org-org-menu)
  3777. (easy-menu-add org-tbl-menu)
  3778. (org-install-agenda-files-menu)
  3779. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3780. (org-add-to-invisibility-spec '(org-cwidth))
  3781. (org-add-to-invisibility-spec '(org-hide-block . t))
  3782. (when (featurep 'xemacs)
  3783. (org-set-local 'line-move-ignore-invisible t))
  3784. (org-set-local 'outline-regexp org-outline-regexp)
  3785. (org-set-local 'outline-level 'org-outline-level)
  3786. (when (and org-ellipsis
  3787. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3788. (fboundp 'make-glyph-code))
  3789. (unless org-display-table
  3790. (setq org-display-table (make-display-table)))
  3791. (set-display-table-slot
  3792. org-display-table 4
  3793. (vconcat (mapcar
  3794. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3795. org-ellipsis)))
  3796. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3797. (setq buffer-display-table org-display-table))
  3798. (org-set-regexps-and-options)
  3799. (when (and org-tag-faces (not org-tags-special-faces-re))
  3800. ;; tag faces set outside customize.... force initialization.
  3801. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3802. ;; Calc embedded
  3803. (org-set-local 'calc-embedded-open-mode "# ")
  3804. (modify-syntax-entry ?# "<")
  3805. (modify-syntax-entry ?@ "w")
  3806. (if org-startup-truncated (setq truncate-lines t))
  3807. (org-set-local 'font-lock-unfontify-region-function
  3808. 'org-unfontify-region)
  3809. ;; Activate before-change-function
  3810. (org-set-local 'org-table-may-need-update t)
  3811. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3812. 'local)
  3813. ;; Check for running clock before killing a buffer
  3814. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3815. ;; Paragraphs and auto-filling
  3816. (org-set-autofill-regexps)
  3817. (setq indent-line-function 'org-indent-line-function)
  3818. (org-update-radio-target-regexp)
  3819. ;; Make sure dependence stuff works reliably, even for users who set it
  3820. ;; too late :-(
  3821. (if org-enforce-todo-dependencies
  3822. (add-hook 'org-blocker-hook
  3823. 'org-block-todo-from-children-or-siblings-or-parent)
  3824. (remove-hook 'org-blocker-hook
  3825. 'org-block-todo-from-children-or-siblings-or-parent))
  3826. (if org-enforce-todo-checkbox-dependencies
  3827. (add-hook 'org-blocker-hook
  3828. 'org-block-todo-from-checkboxes)
  3829. (remove-hook 'org-blocker-hook
  3830. 'org-block-todo-from-checkboxes))
  3831. ;; Comment characters
  3832. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3833. (org-set-local 'comment-padding " ")
  3834. ;; Align options lines
  3835. (org-set-local
  3836. 'align-mode-rules-list
  3837. '((org-in-buffer-settings
  3838. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3839. (modes . '(org-mode)))))
  3840. ;; Imenu
  3841. (org-set-local 'imenu-create-index-function
  3842. 'org-imenu-get-tree)
  3843. ;; Make isearch reveal context
  3844. (if (or (featurep 'xemacs)
  3845. (not (boundp 'outline-isearch-open-invisible-function)))
  3846. ;; Emacs 21 and XEmacs make use of the hook
  3847. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3848. ;; Emacs 22 deals with this through a special variable
  3849. (org-set-local 'outline-isearch-open-invisible-function
  3850. (lambda (&rest ignore) (org-show-context 'isearch))))
  3851. ;; Turn on org-beamer-mode?
  3852. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  3853. ;; If empty file that did not turn on org-mode automatically, make it to.
  3854. (if (and org-insert-mode-line-in-empty-file
  3855. (interactive-p)
  3856. (= (point-min) (point-max)))
  3857. (insert "# -*- mode: org -*-\n\n"))
  3858. (unless org-inhibit-startup
  3859. (when org-startup-align-all-tables
  3860. (let ((bmp (buffer-modified-p)))
  3861. (org-table-map-tables 'org-table-align)
  3862. (set-buffer-modified-p bmp)))
  3863. (when org-startup-indented
  3864. (require 'org-indent)
  3865. (org-indent-mode 1))
  3866. (unless org-inhibit-startup-visibility-stuff
  3867. (org-set-startup-visibility))))
  3868. (when (fboundp 'abbrev-table-put)
  3869. (abbrev-table-put org-mode-abbrev-table
  3870. :parents (list text-mode-abbrev-table)))
  3871. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3872. (defun org-current-time ()
  3873. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3874. (if (> (car org-time-stamp-rounding-minutes) 1)
  3875. (let ((r (car org-time-stamp-rounding-minutes))
  3876. (time (decode-time)))
  3877. (apply 'encode-time
  3878. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3879. (nthcdr 2 time))))
  3880. (current-time)))
  3881. ;;;; Font-Lock stuff, including the activators
  3882. (defvar org-mouse-map (make-sparse-keymap))
  3883. (org-defkey org-mouse-map
  3884. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3885. (org-defkey org-mouse-map
  3886. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3887. (when org-mouse-1-follows-link
  3888. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3889. (when org-tab-follows-link
  3890. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3891. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3892. (require 'font-lock)
  3893. (defconst org-non-link-chars "]\t\n\r<>")
  3894. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3895. "shell" "elisp"))
  3896. (defvar org-link-types-re nil
  3897. "Matches a link that has a url-like prefix like \"http:\"")
  3898. (defvar org-link-re-with-space nil
  3899. "Matches a link with spaces, optional angular brackets around it.")
  3900. (defvar org-link-re-with-space2 nil
  3901. "Matches a link with spaces, optional angular brackets around it.")
  3902. (defvar org-link-re-with-space3 nil
  3903. "Matches a link with spaces, only for internal part in bracket links.")
  3904. (defvar org-angle-link-re nil
  3905. "Matches link with angular brackets, spaces are allowed.")
  3906. (defvar org-plain-link-re nil
  3907. "Matches plain link, without spaces.")
  3908. (defvar org-bracket-link-regexp nil
  3909. "Matches a link in double brackets.")
  3910. (defvar org-bracket-link-analytic-regexp nil
  3911. "Regular expression used to analyze links.
  3912. Here is what the match groups contain after a match:
  3913. 1: http:
  3914. 2: http
  3915. 3: path
  3916. 4: [desc]
  3917. 5: desc")
  3918. (defvar org-bracket-link-analytic-regexp++ nil
  3919. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3920. (defvar org-any-link-re nil
  3921. "Regular expression matching any link.")
  3922. (defun org-make-link-regexps ()
  3923. "Update the link regular expressions.
  3924. This should be called after the variable `org-link-types' has changed."
  3925. (setq org-link-types-re
  3926. (concat
  3927. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  3928. org-link-re-with-space
  3929. (concat
  3930. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3931. "\\([^" org-non-link-chars " ]"
  3932. "[^" org-non-link-chars "]*"
  3933. "[^" org-non-link-chars " ]\\)>?")
  3934. org-link-re-with-space2
  3935. (concat
  3936. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3937. "\\([^" org-non-link-chars " ]"
  3938. "[^\t\n\r]*"
  3939. "[^" org-non-link-chars " ]\\)>?")
  3940. org-link-re-with-space3
  3941. (concat
  3942. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3943. "\\([^" org-non-link-chars " ]"
  3944. "[^\t\n\r]*\\)")
  3945. org-angle-link-re
  3946. (concat
  3947. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3948. "\\([^" org-non-link-chars " ]"
  3949. "[^" org-non-link-chars "]*"
  3950. "\\)>")
  3951. org-plain-link-re
  3952. (concat
  3953. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3954. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  3955. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3956. org-bracket-link-regexp
  3957. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3958. org-bracket-link-analytic-regexp
  3959. (concat
  3960. "\\[\\["
  3961. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  3962. "\\([^]]+\\)"
  3963. "\\]"
  3964. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3965. "\\]")
  3966. org-bracket-link-analytic-regexp++
  3967. (concat
  3968. "\\[\\["
  3969. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3970. "\\([^]]+\\)"
  3971. "\\]"
  3972. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3973. "\\]")
  3974. org-any-link-re
  3975. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3976. org-angle-link-re "\\)\\|\\("
  3977. org-plain-link-re "\\)")))
  3978. (org-make-link-regexps)
  3979. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3980. "Regular expression for fast time stamp matching.")
  3981. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3982. "Regular expression for fast time stamp matching.")
  3983. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3984. "Regular expression matching time strings for analysis.
  3985. This one does not require the space after the date, so it can be used
  3986. on a string that terminates immediately after the date.")
  3987. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3988. "Regular expression matching time strings for analysis.")
  3989. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3990. "Regular expression matching time stamps, with groups.")
  3991. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3992. "Regular expression matching time stamps (also [..]), with groups.")
  3993. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3994. "Regular expression matching a time stamp range.")
  3995. (defconst org-tr-regexp-both
  3996. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3997. "Regular expression matching a time stamp range.")
  3998. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3999. org-ts-regexp "\\)?")
  4000. "Regular expression matching a time stamp or time stamp range.")
  4001. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4002. org-ts-regexp-both "\\)?")
  4003. "Regular expression matching a time stamp or time stamp range.
  4004. The time stamps may be either active or inactive.")
  4005. (defvar org-emph-face nil)
  4006. (defun org-do-emphasis-faces (limit)
  4007. "Run through the buffer and add overlays to links."
  4008. (let (rtn a)
  4009. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4010. (if (not (= (char-after (match-beginning 3))
  4011. (char-after (match-beginning 4))))
  4012. (progn
  4013. (setq rtn t)
  4014. (setq a (assoc (match-string 3) org-emphasis-alist))
  4015. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4016. 'face
  4017. (nth 1 a))
  4018. (and (nth 4 a)
  4019. (org-remove-flyspell-overlays-in
  4020. (match-beginning 0) (match-end 0)))
  4021. (add-text-properties (match-beginning 2) (match-end 2)
  4022. '(font-lock-multiline t))
  4023. (when org-hide-emphasis-markers
  4024. (add-text-properties (match-end 4) (match-beginning 5)
  4025. '(invisible org-link))
  4026. (add-text-properties (match-beginning 3) (match-end 3)
  4027. '(invisible org-link)))))
  4028. (backward-char 1))
  4029. rtn))
  4030. (defun org-emphasize (&optional char)
  4031. "Insert or change an emphasis, i.e. a font like bold or italic.
  4032. If there is an active region, change that region to a new emphasis.
  4033. If there is no region, just insert the marker characters and position
  4034. the cursor between them.
  4035. CHAR should be either the marker character, or the first character of the
  4036. HTML tag associated with that emphasis. If CHAR is a space, the means
  4037. to remove the emphasis of the selected region.
  4038. If char is not given (for example in an interactive call) it
  4039. will be prompted for."
  4040. (interactive)
  4041. (let ((eal org-emphasis-alist) e det
  4042. (erc org-emphasis-regexp-components)
  4043. (prompt "")
  4044. (string "") beg end move tag c s)
  4045. (if (org-region-active-p)
  4046. (setq beg (region-beginning) end (region-end)
  4047. string (buffer-substring beg end))
  4048. (setq move t))
  4049. (while (setq e (pop eal))
  4050. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4051. c (aref tag 0))
  4052. (push (cons c (string-to-char (car e))) det)
  4053. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4054. (substring tag 1)))))
  4055. (setq det (nreverse det))
  4056. (unless char
  4057. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4058. (setq char (read-char-exclusive)))
  4059. (setq char (or (cdr (assoc char det)) char))
  4060. (if (equal char ?\ )
  4061. (setq s "" move nil)
  4062. (unless (assoc (char-to-string char) org-emphasis-alist)
  4063. (error "No such emphasis marker: \"%c\"" char))
  4064. (setq s (char-to-string char)))
  4065. (while (and (> (length string) 1)
  4066. (equal (substring string 0 1) (substring string -1))
  4067. (assoc (substring string 0 1) org-emphasis-alist))
  4068. (setq string (substring string 1 -1)))
  4069. (setq string (concat s string s))
  4070. (if beg (delete-region beg end))
  4071. (unless (or (bolp)
  4072. (string-match (concat "[" (nth 0 erc) "\n]")
  4073. (char-to-string (char-before (point)))))
  4074. (insert " "))
  4075. (unless (or (eobp)
  4076. (string-match (concat "[" (nth 1 erc) "\n]")
  4077. (char-to-string (char-after (point)))))
  4078. (insert " ") (backward-char 1))
  4079. (insert string)
  4080. (and move (backward-char 1))))
  4081. (defconst org-nonsticky-props
  4082. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4083. (defsubst org-rear-nonsticky-at (pos)
  4084. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4085. (defun org-activate-plain-links (limit)
  4086. "Run through the buffer and add overlays to links."
  4087. (catch 'exit
  4088. (let (f)
  4089. (if (re-search-forward org-plain-link-re limit t)
  4090. (progn
  4091. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4092. (setq f (get-text-property (match-beginning 0) 'face))
  4093. (if (or (eq f 'org-tag)
  4094. (and (listp f) (memq 'org-tag f)))
  4095. nil
  4096. (add-text-properties (match-beginning 0) (match-end 0)
  4097. (list 'mouse-face 'highlight
  4098. 'face 'org-link
  4099. 'keymap org-mouse-map))
  4100. (org-rear-nonsticky-at (match-end 0)))
  4101. t)))))
  4102. (defun org-activate-code (limit)
  4103. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4104. (progn
  4105. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4106. (remove-text-properties (match-beginning 0) (match-end 0)
  4107. '(display t invisible t intangible t))
  4108. t)))
  4109. (defun org-fontify-meta-lines-and-blocks (limit)
  4110. "Fontify #+ lines and blocks, in the correct ways."
  4111. (let ((case-fold-search t))
  4112. (if (re-search-forward
  4113. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  4114. limit t)
  4115. (let ((beg (match-beginning 0))
  4116. (beg1 (line-beginning-position 2))
  4117. (dc1 (downcase (match-string 2)))
  4118. (dc3 (downcase (match-string 3)))
  4119. end end1 quoting block-type)
  4120. (cond
  4121. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4122. ;; a single line of backend-specific content
  4123. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4124. (remove-text-properties (match-beginning 0) (match-end 0)
  4125. '(display t invisible t intangible t))
  4126. (add-text-properties (match-beginning 1) (match-end 3)
  4127. '(font-lock-fontified t face org-meta-line))
  4128. (add-text-properties (match-beginning 6) (match-end 6)
  4129. '(font-lock-fontified t face org-block))
  4130. t)
  4131. ((and (match-end 4) (equal dc3 "begin"))
  4132. ;; Truely a block
  4133. (setq block-type (downcase (match-string 5))
  4134. quoting (member block-type org-protecting-blocks))
  4135. (when (re-search-forward
  4136. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4137. nil t) ;; on purpose, we look further than LIMIT
  4138. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4139. (when quoting
  4140. (remove-text-properties beg end
  4141. '(display t invisible t intangible t)))
  4142. (add-text-properties
  4143. beg end
  4144. '(font-lock-fontified t font-lock-multiline t))
  4145. (add-text-properties beg beg1 '(face org-meta-line))
  4146. (add-text-properties end1 end '(face org-meta-line))
  4147. (cond
  4148. (quoting
  4149. (add-text-properties beg1 end1 '(face org-block)))
  4150. ((not org-fontify-quote-and-verse-blocks))
  4151. ((string= block-type "quote")
  4152. (add-text-properties beg1 end1 '(face org-quote)))
  4153. ((string= block-type "verse")
  4154. (add-text-properties beg1 end1 '(face org-verse))))
  4155. t))
  4156. ((member dc1 '("title:" "author:" "email:" "date:"))
  4157. (add-text-properties
  4158. beg (match-end 3)
  4159. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4160. '(font-lock-fontified t invisible t)
  4161. '(font-lock-fontified t face org-document-info-keyword)))
  4162. (add-text-properties
  4163. (match-beginning 6) (match-end 6)
  4164. (if (string-equal dc1 "title:")
  4165. '(font-lock-fontified t face org-document-title)
  4166. '(font-lock-fontified t face org-document-info))))
  4167. ((not (member (char-after beg) '(?\ ?\t)))
  4168. ;; just any other in-buffer setting, but not indented
  4169. (add-text-properties
  4170. beg (match-end 0)
  4171. '(font-lock-fontified t face org-meta-line))
  4172. t)
  4173. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4174. "orgtbl:" "tblfm:" "tblname:"))
  4175. (and (match-end 4) (equal dc3 "attr")))
  4176. (add-text-properties
  4177. beg (match-end 0)
  4178. '(font-lock-fontified t face org-meta-line))
  4179. t)
  4180. ((member dc3 '(" " ""))
  4181. (add-text-properties
  4182. beg (match-end 0)
  4183. '(font-lock-fontified t face font-lock-comment-face)))
  4184. (t nil))))))
  4185. (defun org-activate-angle-links (limit)
  4186. "Run through the buffer and add overlays to links."
  4187. (if (re-search-forward org-angle-link-re limit t)
  4188. (progn
  4189. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4190. (add-text-properties (match-beginning 0) (match-end 0)
  4191. (list 'mouse-face 'highlight
  4192. 'keymap org-mouse-map))
  4193. (org-rear-nonsticky-at (match-end 0))
  4194. t)))
  4195. (defun org-activate-footnote-links (limit)
  4196. "Run through the buffer and add overlays to links."
  4197. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4198. limit t)
  4199. (progn
  4200. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4201. (add-text-properties (match-beginning 2) (match-end 2)
  4202. (list 'mouse-face 'highlight
  4203. 'keymap org-mouse-map
  4204. 'help-echo
  4205. (if (= (point-at-bol) (match-beginning 2))
  4206. "Footnote definition"
  4207. "Footnote reference")
  4208. ))
  4209. (org-rear-nonsticky-at (match-end 2))
  4210. t)))
  4211. (defun org-activate-bracket-links (limit)
  4212. "Run through the buffer and add overlays to bracketed links."
  4213. (if (re-search-forward org-bracket-link-regexp limit t)
  4214. (let* ((help (concat "LINK: "
  4215. (org-match-string-no-properties 1)))
  4216. ;; FIXME: above we should remove the escapes.
  4217. ;; but that requires another match, protecting match data,
  4218. ;; a lot of overhead for font-lock.
  4219. (ip (org-maybe-intangible
  4220. (list 'invisible 'org-link
  4221. 'keymap org-mouse-map 'mouse-face 'highlight
  4222. 'font-lock-multiline t 'help-echo help)))
  4223. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4224. 'font-lock-multiline t 'help-echo help)))
  4225. ;; We need to remove the invisible property here. Table narrowing
  4226. ;; may have made some of this invisible.
  4227. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4228. (remove-text-properties (match-beginning 0) (match-end 0)
  4229. '(invisible nil))
  4230. (if (match-end 3)
  4231. (progn
  4232. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4233. (org-rear-nonsticky-at (match-beginning 3))
  4234. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4235. (org-rear-nonsticky-at (match-end 3))
  4236. (add-text-properties (match-end 3) (match-end 0) ip)
  4237. (org-rear-nonsticky-at (match-end 0)))
  4238. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4239. (org-rear-nonsticky-at (match-beginning 1))
  4240. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4241. (org-rear-nonsticky-at (match-end 1))
  4242. (add-text-properties (match-end 1) (match-end 0) ip)
  4243. (org-rear-nonsticky-at (match-end 0)))
  4244. t)))
  4245. (defun org-activate-dates (limit)
  4246. "Run through the buffer and add overlays to dates."
  4247. (if (re-search-forward org-tsr-regexp-both limit t)
  4248. (progn
  4249. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4250. (add-text-properties (match-beginning 0) (match-end 0)
  4251. (list 'mouse-face 'highlight
  4252. 'keymap org-mouse-map))
  4253. (org-rear-nonsticky-at (match-end 0))
  4254. (when org-display-custom-times
  4255. (if (match-end 3)
  4256. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4257. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4258. t)))
  4259. (defvar org-target-link-regexp nil
  4260. "Regular expression matching radio targets in plain text.")
  4261. (make-variable-buffer-local 'org-target-link-regexp)
  4262. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4263. "Regular expression matching a link target.")
  4264. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4265. "Regular expression matching a radio target.")
  4266. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4267. "Regular expression matching any target.")
  4268. (defun org-activate-target-links (limit)
  4269. "Run through the buffer and add overlays to target matches."
  4270. (when org-target-link-regexp
  4271. (let ((case-fold-search t))
  4272. (if (re-search-forward org-target-link-regexp limit t)
  4273. (progn
  4274. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4275. (add-text-properties (match-beginning 0) (match-end 0)
  4276. (list 'mouse-face 'highlight
  4277. 'keymap org-mouse-map
  4278. 'help-echo "Radio target link"
  4279. 'org-linked-text t))
  4280. (org-rear-nonsticky-at (match-end 0))
  4281. t)))))
  4282. (defun org-update-radio-target-regexp ()
  4283. "Find all radio targets in this file and update the regular expression."
  4284. (interactive)
  4285. (when (memq 'radio org-activate-links)
  4286. (setq org-target-link-regexp
  4287. (org-make-target-link-regexp (org-all-targets 'radio)))
  4288. (org-restart-font-lock)))
  4289. (defun org-hide-wide-columns (limit)
  4290. (let (s e)
  4291. (setq s (text-property-any (point) (or limit (point-max))
  4292. 'org-cwidth t))
  4293. (when s
  4294. (setq e (next-single-property-change s 'org-cwidth))
  4295. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4296. (goto-char e)
  4297. t)))
  4298. (defvar org-latex-and-specials-regexp nil
  4299. "Regular expression for highlighting export special stuff.")
  4300. (defvar org-match-substring-regexp)
  4301. (defvar org-match-substring-with-braces-regexp)
  4302. ;; This should be with the exporter code, but we also use if for font-locking
  4303. (defconst org-export-html-special-string-regexps
  4304. '(("\\\\-" . "&shy;")
  4305. ("---\\([^-]\\)" . "&mdash;\\1")
  4306. ("--\\([^-]\\)" . "&ndash;\\1")
  4307. ("\\.\\.\\." . "&hellip;"))
  4308. "Regular expressions for special string conversion.")
  4309. (defun org-compute-latex-and-specials-regexp ()
  4310. "Compute regular expression for stuff treated specially by exporters."
  4311. (if (not org-highlight-latex-fragments-and-specials)
  4312. (org-set-local 'org-latex-and-specials-regexp nil)
  4313. (require 'org-exp)
  4314. (let*
  4315. ((matchers (plist-get org-format-latex-options :matchers))
  4316. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4317. org-latex-regexps)))
  4318. (org-export-allow-BIND nil)
  4319. (options (org-combine-plists (org-default-export-plist)
  4320. (org-infile-export-plist)))
  4321. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4322. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4323. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4324. (org-export-html-expand (plist-get options :expand-quoted-html))
  4325. (org-export-with-special-strings (plist-get options :special-strings))
  4326. (re-sub
  4327. (cond
  4328. ((equal org-export-with-sub-superscripts '{})
  4329. (list org-match-substring-with-braces-regexp))
  4330. (org-export-with-sub-superscripts
  4331. (list org-match-substring-regexp))
  4332. (t nil)))
  4333. (re-latex
  4334. (if org-export-with-LaTeX-fragments
  4335. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4336. (re-macros
  4337. (if org-export-with-TeX-macros
  4338. (list (concat "\\\\"
  4339. (regexp-opt
  4340. (append (mapcar 'car org-html-entities)
  4341. (if (boundp 'org-latex-entities)
  4342. (mapcar (lambda (x)
  4343. (or (car-safe x) x))
  4344. org-latex-entities)
  4345. nil))
  4346. 'words))) ; FIXME
  4347. ))
  4348. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4349. (re-special (if org-export-with-special-strings
  4350. (mapcar (lambda (x) (car x))
  4351. org-export-html-special-string-regexps)))
  4352. (re-rest
  4353. (delq nil
  4354. (list
  4355. (if org-export-html-expand "@<[^>\n]+>")
  4356. ))))
  4357. (org-set-local
  4358. 'org-latex-and-specials-regexp
  4359. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4360. re-rest) "\\|")))))
  4361. (defun org-do-latex-and-special-faces (limit)
  4362. "Run through the buffer and add overlays to links."
  4363. (when org-latex-and-specials-regexp
  4364. (let (rtn d)
  4365. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4366. limit t))
  4367. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4368. 'face))
  4369. '(org-code org-verbatim underline)))
  4370. (progn
  4371. (setq rtn t
  4372. d (cond ((member (char-after (1+ (match-beginning 0)))
  4373. '(?_ ?^)) 1)
  4374. (t 0)))
  4375. (font-lock-prepend-text-property
  4376. (+ d (match-beginning 0)) (match-end 0)
  4377. 'face 'org-latex-and-export-specials)
  4378. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4379. '(font-lock-multiline t)))))
  4380. rtn)))
  4381. (defun org-restart-font-lock ()
  4382. "Restart font-lock-mode, to force refontification."
  4383. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4384. (font-lock-mode -1)
  4385. (font-lock-mode 1)))
  4386. (defun org-all-targets (&optional radio)
  4387. "Return a list of all targets in this file.
  4388. With optional argument RADIO, only find radio targets."
  4389. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4390. rtn)
  4391. (save-excursion
  4392. (goto-char (point-min))
  4393. (while (re-search-forward re nil t)
  4394. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4395. rtn)))
  4396. (defun org-make-target-link-regexp (targets)
  4397. "Make regular expression matching all strings in TARGETS.
  4398. The regular expression finds the targets also if there is a line break
  4399. between words."
  4400. (and targets
  4401. (concat
  4402. "\\<\\("
  4403. (mapconcat
  4404. (lambda (x)
  4405. (while (string-match " +" x)
  4406. (setq x (replace-match "\\s-+" t t x)))
  4407. x)
  4408. targets
  4409. "\\|")
  4410. "\\)\\>")))
  4411. (defun org-activate-tags (limit)
  4412. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4413. (progn
  4414. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4415. (add-text-properties (match-beginning 1) (match-end 1)
  4416. (list 'mouse-face 'highlight
  4417. 'keymap org-mouse-map))
  4418. (org-rear-nonsticky-at (match-end 1))
  4419. t)))
  4420. (defun org-outline-level ()
  4421. "Compute the outline level of the heading at point.
  4422. This function assumes that the cursor is at the beginning of a line matched
  4423. by outline-regexp. Otherwise it returns garbage.
  4424. If this is called at a normal headline, the level is the number of stars.
  4425. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4426. For plain list items, if they are matched by `outline-regexp', this returns
  4427. 1000 plus the line indentation."
  4428. (save-excursion
  4429. (looking-at outline-regexp)
  4430. (if (match-beginning 1)
  4431. (+ (org-get-string-indentation (match-string 1)) 1000)
  4432. (1- (- (match-end 0) (match-beginning 0))))))
  4433. (defvar org-font-lock-keywords nil)
  4434. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4435. "Regular expression matching a property line.")
  4436. (defvar org-font-lock-hook nil
  4437. "Functions to be called for special font lock stuff.")
  4438. (defun org-font-lock-hook (limit)
  4439. (run-hook-with-args 'org-font-lock-hook limit))
  4440. (defun org-set-font-lock-defaults ()
  4441. (let* ((em org-fontify-emphasized-text)
  4442. (lk org-activate-links)
  4443. (org-font-lock-extra-keywords
  4444. (list
  4445. ;; Call the hook
  4446. '(org-font-lock-hook)
  4447. ;; Headlines
  4448. `(,(if org-fontify-whole-heading-line
  4449. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4450. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4451. (1 (org-get-level-face 1))
  4452. (2 (org-get-level-face 2))
  4453. (3 (org-get-level-face 3)))
  4454. ;; Table lines
  4455. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4456. (1 'org-table t))
  4457. ;; Table internals
  4458. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4459. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4460. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4461. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4462. ;; Drawers
  4463. (list org-drawer-regexp '(0 'org-special-keyword t))
  4464. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4465. ;; Properties
  4466. (list org-property-re
  4467. '(1 'org-special-keyword t)
  4468. '(3 'org-property-value t))
  4469. ;; Links
  4470. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4471. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4472. (if (memq 'plain lk) '(org-activate-plain-links))
  4473. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4474. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4475. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4476. (if (memq 'footnote lk) '(org-activate-footnote-links
  4477. (2 'org-footnote t)))
  4478. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4479. '(org-hide-wide-columns (0 nil append))
  4480. ;; TODO lines
  4481. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4482. '(1 (org-get-todo-face 1) t))
  4483. ;; DONE
  4484. (if org-fontify-done-headline
  4485. (list (concat "^[*]+ +\\<\\("
  4486. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4487. "\\)\\(.*\\)")
  4488. '(2 'org-headline-done t))
  4489. nil)
  4490. ;; Priorities
  4491. '(org-font-lock-add-priority-faces)
  4492. ;; Tags
  4493. '(org-font-lock-add-tag-faces)
  4494. ;; Special keywords
  4495. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4496. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4497. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4498. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4499. ;; Emphasis
  4500. (if em
  4501. (if (featurep 'xemacs)
  4502. '(org-do-emphasis-faces (0 nil append))
  4503. '(org-do-emphasis-faces)))
  4504. ;; Checkboxes
  4505. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4506. 2 'org-checkbox prepend)
  4507. (if org-provide-checkbox-statistics
  4508. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4509. (0 (org-get-checkbox-statistics-face) t)))
  4510. ;; Description list items
  4511. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4512. 2 'bold prepend)
  4513. ;; ARCHIVEd headings
  4514. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4515. '(1 'org-archived prepend))
  4516. ;; Specials
  4517. '(org-do-latex-and-special-faces)
  4518. ;; Code
  4519. '(org-activate-code (1 'org-code t))
  4520. ;; COMMENT
  4521. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4522. "\\|" org-quote-string "\\)\\>")
  4523. '(1 'org-special-keyword t))
  4524. '("^#.*" (0 'font-lock-comment-face t))
  4525. ;; Blocks and meta lines
  4526. '(org-fontify-meta-lines-and-blocks)
  4527. )))
  4528. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4529. ;; Now set the full font-lock-keywords
  4530. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4531. (org-set-local 'font-lock-defaults
  4532. '(org-font-lock-keywords t nil nil backward-paragraph))
  4533. (kill-local-variable 'font-lock-keywords) nil))
  4534. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4535. "Fontify string S like in Org-mode"
  4536. (with-temp-buffer
  4537. (insert s)
  4538. (let ((org-odd-levels-only odd-levels))
  4539. (org-mode)
  4540. (font-lock-fontify-buffer)
  4541. (buffer-string))))
  4542. (defvar org-m nil)
  4543. (defvar org-l nil)
  4544. (defvar org-f nil)
  4545. (defun org-get-level-face (n)
  4546. "Get the right face for match N in font-lock matching of headlines."
  4547. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4548. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4549. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4550. (cond
  4551. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4552. ((eq n 2) org-f)
  4553. (t (if org-level-color-stars-only nil org-f))))
  4554. (defun org-get-todo-face (kwd)
  4555. "Get the right face for a TODO keyword KWD.
  4556. If KWD is a number, get the corresponding match group."
  4557. (if (numberp kwd) (setq kwd (match-string kwd)))
  4558. (or (org-face-from-face-or-color
  4559. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  4560. (and (member kwd org-done-keywords) 'org-done)
  4561. 'org-todo))
  4562. (defun org-face-from-face-or-color (context inherit face-or-color)
  4563. "Create a face list that inherits INHERIT, but sets the foreground color.
  4564. When FACE-OR-COLOR is not a string, just return it."
  4565. (if (stringp face-or-color)
  4566. (list :inherit inherit
  4567. (cdr (assoc context org-faces-easy-properties))
  4568. face-or-color)
  4569. face-or-color))
  4570. (defun org-font-lock-add-tag-faces (limit)
  4571. "Add the special tag faces."
  4572. (when (and org-tag-faces org-tags-special-faces-re)
  4573. (while (re-search-forward org-tags-special-faces-re limit t)
  4574. (add-text-properties (match-beginning 1) (match-end 1)
  4575. (list 'face (org-get-tag-face 1)
  4576. 'font-lock-fontified t))
  4577. (backward-char 1))))
  4578. (defun org-font-lock-add-priority-faces (limit)
  4579. "Add the special priority faces."
  4580. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4581. (add-text-properties
  4582. (match-beginning 0) (match-end 0)
  4583. (list 'face (or (org-face-from-face-or-color
  4584. 'priority 'org-special-keyword
  4585. (cdr (assoc (char-after (match-beginning 1))
  4586. org-priority-faces)))
  4587. 'org-special-keyword)
  4588. 'font-lock-fontified t))))
  4589. (defun org-get-tag-face (kwd)
  4590. "Get the right face for a TODO keyword KWD.
  4591. If KWD is a number, get the corresponding match group."
  4592. (if (numberp kwd) (setq kwd (match-string kwd)))
  4593. (or (org-face-from-face-or-color
  4594. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  4595. 'org-tag))
  4596. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4597. "Remove fontification and activation overlays from links."
  4598. (font-lock-default-unfontify-region beg end)
  4599. (let* ((buffer-undo-list t)
  4600. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4601. (inhibit-modification-hooks t)
  4602. deactivate-mark buffer-file-name buffer-file-truename)
  4603. (remove-text-properties
  4604. beg end
  4605. (if org-indent-mode
  4606. ;; also remove line-prefix and wrap-prefix properties
  4607. '(mouse-face t keymap t org-linked-text t
  4608. invisible t intangible t
  4609. line-prefix t wrap-prefix t
  4610. org-no-flyspell t)
  4611. '(mouse-face t keymap t org-linked-text t
  4612. invisible t intangible t
  4613. org-no-flyspell t)))))
  4614. ;;;; Visibility cycling, including org-goto and indirect buffer
  4615. ;;; Cycling
  4616. (defvar org-cycle-global-status nil)
  4617. (make-variable-buffer-local 'org-cycle-global-status)
  4618. (defvar org-cycle-subtree-status nil)
  4619. (make-variable-buffer-local 'org-cycle-subtree-status)
  4620. ;;;###autoload
  4621. (defvar org-inlinetask-min-level)
  4622. (defun org-cycle (&optional arg)
  4623. "TAB-action and visibility cycling for Org-mode.
  4624. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4625. is outline visibility cycling, but it also invokes other actions
  4626. in special contexts.
  4627. - When this function is called with a prefix argument, rotate the entire
  4628. buffer through 3 states (global cycling)
  4629. 1. OVERVIEW: Show only top-level headlines.
  4630. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4631. 3. SHOW ALL: Show everything.
  4632. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4633. determined by the variable `org-startup-folded', and by any VISIBILITY
  4634. properties in the buffer.
  4635. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4636. including any drawers.
  4637. - When inside a table, re-align the table and move to the next field.
  4638. - When point is at the beginning of a headline, rotate the subtree started
  4639. by this line through 3 different states (local cycling)
  4640. 1. FOLDED: Only the main headline is shown.
  4641. 2. CHILDREN: The main headline and the direct children are shown.
  4642. From this state, you can move to one of the children
  4643. and zoom in further.
  4644. 3. SUBTREE: Show the entire subtree, including body text.
  4645. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4646. - When point is at the beginning of an empty headline and the variable
  4647. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  4648. of the headline by demoting and promoting it to likely levels. This
  4649. speeds up creation document structure by presing TAB once or several
  4650. times right after creating a new headline.
  4651. - When there is a numeric prefix, go up to a heading with level ARG, do
  4652. a `show-subtree' and return to the previous cursor position. If ARG
  4653. is negative, go up that many levels.
  4654. - When point is not at the beginning of a headline, execute the global
  4655. binding for TAB, which is re-indenting the line. See the option
  4656. `org-cycle-emulate-tab' for details.
  4657. - Special case: if point is at the beginning of the buffer and there is
  4658. no headline in line 1, this function will act as if called with prefix arg.
  4659. But only if also the variable `org-cycle-global-at-bob' is t."
  4660. (interactive "P")
  4661. (org-load-modules-maybe)
  4662. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  4663. (and org-cycle-level-after-item/entry-creation
  4664. (or (org-cycle-level)
  4665. (org-cycle-item-indentation))))
  4666. (let* ((limit-level
  4667. (or org-cycle-max-level
  4668. (and (boundp 'org-inlinetask-min-level)
  4669. org-inlinetask-min-level
  4670. (1- org-inlinetask-min-level))))
  4671. (nstars (and limit-level
  4672. (if org-odd-levels-only
  4673. (and limit-level (1- (* limit-level 2)))
  4674. limit-level)))
  4675. (outline-regexp
  4676. (cond
  4677. ((not (org-mode-p)) outline-regexp)
  4678. ((or (eq org-cycle-include-plain-lists 'integrate)
  4679. (and org-cycle-include-plain-lists (org-at-item-p)))
  4680. (concat "\\(?:\\*"
  4681. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4682. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4683. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4684. (bob-special (and org-cycle-global-at-bob (bobp)
  4685. (not (looking-at outline-regexp))))
  4686. (org-cycle-hook
  4687. (if bob-special
  4688. (delq 'org-optimize-window-after-visibility-change
  4689. (copy-sequence org-cycle-hook))
  4690. org-cycle-hook))
  4691. (pos (point)))
  4692. (if (or bob-special (equal arg '(4)))
  4693. ;; special case: use global cycling
  4694. (setq arg t))
  4695. (cond
  4696. ((equal arg '(16))
  4697. (org-set-startup-visibility)
  4698. (message "Startup visibility, plus VISIBILITY properties"))
  4699. ((equal arg '(64))
  4700. (show-all)
  4701. (message "Entire buffer visible, including drawers"))
  4702. ((org-at-table-p 'any)
  4703. ;; Enter the table or move to the next field in the table
  4704. (if (org-at-table.el-p)
  4705. (message "Use C-c ' to edit table.el tables")
  4706. (if arg (org-table-edit-field t)
  4707. (org-table-justify-field-maybe)
  4708. (call-interactively 'org-table-next-field))))
  4709. ((run-hook-with-args-until-success
  4710. 'org-tab-after-check-for-table-hook))
  4711. ((eq arg t) ;; Global cycling
  4712. (org-cycle-internal-global))
  4713. ((and org-drawers org-drawer-regexp
  4714. (save-excursion
  4715. (beginning-of-line 1)
  4716. (looking-at org-drawer-regexp)))
  4717. ;; Toggle block visibility
  4718. (org-flag-drawer
  4719. (not (get-char-property (match-end 0) 'invisible))))
  4720. ((integerp arg)
  4721. ;; Show-subtree, ARG levels up from here.
  4722. (save-excursion
  4723. (org-back-to-heading)
  4724. (outline-up-heading (if (< arg 0) (- arg)
  4725. (- (funcall outline-level) arg)))
  4726. (org-show-subtree)))
  4727. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4728. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4729. (org-cycle-internal-local))
  4730. ;; TAB emulation and template completion
  4731. (buffer-read-only (org-back-to-heading))
  4732. ((run-hook-with-args-until-success
  4733. 'org-tab-after-check-for-cycling-hook))
  4734. ((org-try-structure-completion))
  4735. ((org-try-cdlatex-tab))
  4736. ((run-hook-with-args-until-success
  4737. 'org-tab-before-tab-emulation-hook))
  4738. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4739. (or (not (bolp))
  4740. (not (looking-at outline-regexp))))
  4741. (call-interactively (global-key-binding "\t")))
  4742. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4743. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4744. (or (and (eq org-cycle-emulate-tab 'white)
  4745. (= (match-end 0) (point-at-eol)))
  4746. (and (eq org-cycle-emulate-tab 'whitestart)
  4747. (>= (match-end 0) pos))))
  4748. t
  4749. (eq org-cycle-emulate-tab t))
  4750. (call-interactively (global-key-binding "\t")))
  4751. (t (save-excursion
  4752. (org-back-to-heading)
  4753. (org-cycle)))))))
  4754. (defun org-cycle-internal-global ()
  4755. "Do the global cycling action."
  4756. (cond
  4757. ((and (eq last-command this-command)
  4758. (eq org-cycle-global-status 'overview))
  4759. ;; We just created the overview - now do table of contents
  4760. ;; This can be slow in very large buffers, so indicate action
  4761. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4762. (message "CONTENTS...")
  4763. (org-content)
  4764. (message "CONTENTS...done")
  4765. (setq org-cycle-global-status 'contents)
  4766. (run-hook-with-args 'org-cycle-hook 'contents))
  4767. ((and (eq last-command this-command)
  4768. (eq org-cycle-global-status 'contents))
  4769. ;; We just showed the table of contents - now show everything
  4770. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4771. (show-all)
  4772. (message "SHOW ALL")
  4773. (setq org-cycle-global-status 'all)
  4774. (run-hook-with-args 'org-cycle-hook 'all))
  4775. (t
  4776. ;; Default action: go to overview
  4777. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4778. (org-overview)
  4779. (message "OVERVIEW")
  4780. (setq org-cycle-global-status 'overview)
  4781. (run-hook-with-args 'org-cycle-hook 'overview))))
  4782. (defun org-cycle-internal-local ()
  4783. "Do the local cycling action."
  4784. (org-back-to-heading)
  4785. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4786. ;; First, some boundaries
  4787. (save-excursion
  4788. (org-back-to-heading)
  4789. (setq level (funcall outline-level))
  4790. (save-excursion
  4791. (beginning-of-line 2)
  4792. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4793. ; XEmacs does not have `next-single-char-property-change'
  4794. ; I'm not sure about Emacs 21.
  4795. (while (and (not (eobp)) ;; this is like `next-line'
  4796. (get-char-property (1- (point)) 'invisible))
  4797. (beginning-of-line 2))
  4798. (while (and (not (eobp)) ;; this is like `next-line'
  4799. (get-char-property (1- (point)) 'invisible))
  4800. (goto-char (next-single-char-property-change (point) 'invisible))
  4801. ;;;??? (or (bolp) (beginning-of-line 2))))
  4802. (and (eolp) (beginning-of-line 2))))
  4803. (setq eol (point)))
  4804. (outline-end-of-heading) (setq eoh (point))
  4805. (save-excursion
  4806. (outline-next-heading)
  4807. (setq has-children (and (org-at-heading-p t)
  4808. (> (funcall outline-level) level))))
  4809. (org-end-of-subtree t)
  4810. (unless (eobp)
  4811. (skip-chars-forward " \t\n")
  4812. (beginning-of-line 1) ; in case this is an item
  4813. )
  4814. (setq eos (if (eobp) (point) (1- (point)))))
  4815. ;; Find out what to do next and set `this-command'
  4816. (cond
  4817. ((= eos eoh)
  4818. ;; Nothing is hidden behind this heading
  4819. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4820. (message "EMPTY ENTRY")
  4821. (setq org-cycle-subtree-status nil)
  4822. (save-excursion
  4823. (goto-char eos)
  4824. (outline-next-heading)
  4825. (if (org-invisible-p) (org-flag-heading nil))))
  4826. ((and (or (>= eol eos)
  4827. (not (string-match "\\S-" (buffer-substring eol eos))))
  4828. (or has-children
  4829. (not (setq children-skipped
  4830. org-cycle-skip-children-state-if-no-children))))
  4831. ;; Entire subtree is hidden in one line: children view
  4832. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4833. (org-show-entry)
  4834. (show-children)
  4835. (message "CHILDREN")
  4836. (save-excursion
  4837. (goto-char eos)
  4838. (outline-next-heading)
  4839. (if (org-invisible-p) (org-flag-heading nil)))
  4840. (setq org-cycle-subtree-status 'children)
  4841. (run-hook-with-args 'org-cycle-hook 'children))
  4842. ((or children-skipped
  4843. (and (eq last-command this-command)
  4844. (eq org-cycle-subtree-status 'children)))
  4845. ;; We just showed the children, or no children are there,
  4846. ;; now show everything.
  4847. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4848. (org-show-subtree)
  4849. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4850. (setq org-cycle-subtree-status 'subtree)
  4851. (run-hook-with-args 'org-cycle-hook 'subtree))
  4852. (t
  4853. ;; Default action: hide the subtree.
  4854. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4855. (hide-subtree)
  4856. (message "FOLDED")
  4857. (setq org-cycle-subtree-status 'folded)
  4858. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4859. ;;;###autoload
  4860. (defun org-global-cycle (&optional arg)
  4861. "Cycle the global visibility. For details see `org-cycle'.
  4862. With C-u prefix arg, switch to startup visibility.
  4863. With a numeric prefix, show all headlines up to that level."
  4864. (interactive "P")
  4865. (let ((org-cycle-include-plain-lists
  4866. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4867. (cond
  4868. ((integerp arg)
  4869. (show-all)
  4870. (hide-sublevels arg)
  4871. (setq org-cycle-global-status 'contents))
  4872. ((equal arg '(4))
  4873. (org-set-startup-visibility)
  4874. (message "Startup visibility, plus VISIBILITY properties."))
  4875. (t
  4876. (org-cycle '(4))))))
  4877. (defun org-set-startup-visibility ()
  4878. "Set the visibility required by startup options and properties."
  4879. (cond
  4880. ((eq org-startup-folded t)
  4881. (org-cycle '(4)))
  4882. ((eq org-startup-folded 'content)
  4883. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4884. (org-cycle '(4)) (org-cycle '(4)))))
  4885. (unless (eq org-startup-folded 'showeverything)
  4886. (if org-hide-block-startup (org-hide-block-all))
  4887. (org-set-visibility-according-to-property 'no-cleanup)
  4888. (org-cycle-hide-archived-subtrees 'all)
  4889. (org-cycle-hide-drawers 'all)
  4890. (org-cycle-show-empty-lines 'all)))
  4891. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4892. "Switch subtree visibilities according to :VISIBILITY: property."
  4893. (interactive)
  4894. (let (org-show-entry-below state)
  4895. (save-excursion
  4896. (goto-char (point-min))
  4897. (while (re-search-forward
  4898. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4899. nil t)
  4900. (setq state (match-string 1))
  4901. (save-excursion
  4902. (org-back-to-heading t)
  4903. (hide-subtree)
  4904. (org-reveal)
  4905. (cond
  4906. ((equal state '("fold" "folded"))
  4907. (hide-subtree))
  4908. ((equal state "children")
  4909. (org-show-hidden-entry)
  4910. (show-children))
  4911. ((equal state "content")
  4912. (save-excursion
  4913. (save-restriction
  4914. (org-narrow-to-subtree)
  4915. (org-content))))
  4916. ((member state '("all" "showall"))
  4917. (show-subtree)))))
  4918. (unless no-cleanup
  4919. (org-cycle-hide-archived-subtrees 'all)
  4920. (org-cycle-hide-drawers 'all)
  4921. (org-cycle-show-empty-lines 'all)))))
  4922. (defun org-overview ()
  4923. "Switch to overview mode, showing only top-level headlines.
  4924. Really, this shows all headlines with level equal or greater than the level
  4925. of the first headline in the buffer. This is important, because if the
  4926. first headline is not level one, then (hide-sublevels 1) gives confusing
  4927. results."
  4928. (interactive)
  4929. (let ((level (save-excursion
  4930. (goto-char (point-min))
  4931. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4932. (progn
  4933. (goto-char (match-beginning 0))
  4934. (funcall outline-level))))))
  4935. (and level (hide-sublevels level))))
  4936. (defun org-content (&optional arg)
  4937. "Show all headlines in the buffer, like a table of contents.
  4938. With numerical argument N, show content up to level N."
  4939. (interactive "P")
  4940. (save-excursion
  4941. ;; Visit all headings and show their offspring
  4942. (and (integerp arg) (org-overview))
  4943. (goto-char (point-max))
  4944. (catch 'exit
  4945. (while (and (progn (condition-case nil
  4946. (outline-previous-visible-heading 1)
  4947. (error (goto-char (point-min))))
  4948. t)
  4949. (looking-at outline-regexp))
  4950. (if (integerp arg)
  4951. (show-children (1- arg))
  4952. (show-branches))
  4953. (if (bobp) (throw 'exit nil))))))
  4954. (defun org-optimize-window-after-visibility-change (state)
  4955. "Adjust the window after a change in outline visibility.
  4956. This function is the default value of the hook `org-cycle-hook'."
  4957. (when (get-buffer-window (current-buffer))
  4958. (cond
  4959. ((eq state 'content) nil)
  4960. ((eq state 'all) nil)
  4961. ((eq state 'folded) nil)
  4962. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4963. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4964. (defun org-remove-empty-overlays-at (pos)
  4965. "Remove outline overlays that do not contain non-white stuff."
  4966. (mapc
  4967. (lambda (o)
  4968. (and (eq 'outline (org-overlay-get o 'invisible))
  4969. (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
  4970. (org-overlay-end o))))
  4971. (org-delete-overlay o)))
  4972. (org-overlays-at pos)))
  4973. (defun org-clean-visibility-after-subtree-move ()
  4974. "Fix visibility issues after moving a subtree."
  4975. ;; First, find a reasonable region to look at:
  4976. ;; Start two siblings above, end three below
  4977. (let* ((beg (save-excursion
  4978. (and (org-get-last-sibling)
  4979. (org-get-last-sibling))
  4980. (point)))
  4981. (end (save-excursion
  4982. (and (org-get-next-sibling)
  4983. (org-get-next-sibling)
  4984. (org-get-next-sibling))
  4985. (if (org-at-heading-p)
  4986. (point-at-eol)
  4987. (point))))
  4988. (level (looking-at "\\*+"))
  4989. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  4990. (save-excursion
  4991. (save-restriction
  4992. (narrow-to-region beg end)
  4993. (when re
  4994. ;; Properly fold already folded siblings
  4995. (goto-char (point-min))
  4996. (while (re-search-forward re nil t)
  4997. (if (and (not (org-invisible-p))
  4998. (save-excursion
  4999. (goto-char (point-at-eol)) (org-invisible-p)))
  5000. (hide-entry))))
  5001. (org-cycle-show-empty-lines 'overview)
  5002. (org-cycle-hide-drawers 'overview)))))
  5003. (defun org-cycle-show-empty-lines (state)
  5004. "Show empty lines above all visible headlines.
  5005. The region to be covered depends on STATE when called through
  5006. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5007. entire buffer covered. Note that an empty line is only shown if there
  5008. are at least `org-cycle-separator-lines' empty lines before the headline."
  5009. (when (not (= org-cycle-separator-lines 0))
  5010. (save-excursion
  5011. (let* ((n (abs org-cycle-separator-lines))
  5012. (re (cond
  5013. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5014. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5015. (t (let ((ns (number-to-string (- n 2))))
  5016. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5017. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5018. beg end b e)
  5019. (cond
  5020. ((memq state '(overview contents t))
  5021. (setq beg (point-min) end (point-max)))
  5022. ((memq state '(children folded))
  5023. (setq beg (point) end (progn (org-end-of-subtree t t)
  5024. (beginning-of-line 2)
  5025. (point)))))
  5026. (when beg
  5027. (goto-char beg)
  5028. (while (re-search-forward re end t)
  5029. (unless (get-char-property (match-end 1) 'invisible)
  5030. (setq e (match-end 1))
  5031. (if (< org-cycle-separator-lines 0)
  5032. (setq b (save-excursion
  5033. (goto-char (match-beginning 0))
  5034. (org-back-over-empty-lines)
  5035. (if (save-excursion
  5036. (goto-char (max (point-min) (1- (point))))
  5037. (org-on-heading-p))
  5038. (1- (point))
  5039. (point))))
  5040. (setq b (match-beginning 1)))
  5041. (outline-flag-region b e nil)))))))
  5042. ;; Never hide empty lines at the end of the file.
  5043. (save-excursion
  5044. (goto-char (point-max))
  5045. (outline-previous-heading)
  5046. (outline-end-of-heading)
  5047. (if (and (looking-at "[ \t\n]+")
  5048. (= (match-end 0) (point-max)))
  5049. (outline-flag-region (point) (match-end 0) nil))))
  5050. (defun org-show-empty-lines-in-parent ()
  5051. "Move to the parent and re-show empty lines before visible headlines."
  5052. (save-excursion
  5053. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5054. (org-cycle-show-empty-lines context))))
  5055. (defun org-files-list ()
  5056. "Return `org-agenda-files' list, plus all open org-mode files.
  5057. This is useful for operations that need to scan all of a user's
  5058. open and agenda-wise Org files."
  5059. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5060. (dolist (buf (buffer-list))
  5061. (with-current-buffer buf
  5062. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5063. (let ((file (expand-file-name (buffer-file-name))))
  5064. (unless (member file files)
  5065. (push file files))))))
  5066. files))
  5067. (defsubst org-entry-beginning-position ()
  5068. "Return the beginning position of the current entry."
  5069. (save-excursion (outline-back-to-heading t) (point)))
  5070. (defsubst org-entry-end-position ()
  5071. "Return the end position of the current entry."
  5072. (save-excursion (outline-next-heading) (point)))
  5073. (defun org-cycle-hide-drawers (state)
  5074. "Re-hide all drawers after a visibility state change."
  5075. (when (and (org-mode-p)
  5076. (not (memq state '(overview folded contents))))
  5077. (save-excursion
  5078. (let* ((globalp (memq state '(contents all)))
  5079. (beg (if globalp (point-min) (point)))
  5080. (end (if globalp (point-max)
  5081. (if (eq state 'children)
  5082. (save-excursion (outline-next-heading) (point))
  5083. (org-end-of-subtree t)))))
  5084. (goto-char beg)
  5085. (while (re-search-forward org-drawer-regexp end t)
  5086. (org-flag-drawer t))))))
  5087. (defun org-flag-drawer (flag)
  5088. (save-excursion
  5089. (beginning-of-line 1)
  5090. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5091. (let ((b (match-end 0))
  5092. (outline-regexp org-outline-regexp))
  5093. (if (re-search-forward
  5094. "^[ \t]*:END:"
  5095. (save-excursion (outline-next-heading) (point)) t)
  5096. (outline-flag-region b (point-at-eol) flag)
  5097. (error ":END: line missing at position %s" b))))))
  5098. (defun org-subtree-end-visible-p ()
  5099. "Is the end of the current subtree visible?"
  5100. (pos-visible-in-window-p
  5101. (save-excursion (org-end-of-subtree t) (point))))
  5102. (defun org-first-headline-recenter (&optional N)
  5103. "Move cursor to the first headline and recenter the headline.
  5104. Optional argument N means put the headline into the Nth line of the window."
  5105. (goto-char (point-min))
  5106. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5107. (beginning-of-line)
  5108. (recenter (prefix-numeric-value N))))
  5109. ;;; Saving and restoring visibility
  5110. (defun org-outline-overlay-data (&optional use-markers)
  5111. "Return a list of the locations of all outline overlays.
  5112. The are overlays with the `invisible' property value `outline'.
  5113. The return valus is a list of cons cells, with start and stop
  5114. positions for each overlay.
  5115. If USE-MARKERS is set, return the positions as markers."
  5116. (let (beg end)
  5117. (save-excursion
  5118. (save-restriction
  5119. (widen)
  5120. (delq nil
  5121. (mapcar (lambda (o)
  5122. (when (eq (org-overlay-get o 'invisible) 'outline)
  5123. (setq beg (org-overlay-start o)
  5124. end (org-overlay-end o))
  5125. (and beg end (> end beg)
  5126. (if use-markers
  5127. (cons (move-marker (make-marker) beg)
  5128. (move-marker (make-marker) end))
  5129. (cons beg end)))))
  5130. (org-overlays-in (point-min) (point-max))))))))
  5131. (defun org-set-outline-overlay-data (data)
  5132. "Create visibility overlays for all positions in DATA.
  5133. DATA should have been made by `org-outline-overlay-data'."
  5134. (let (o)
  5135. (save-excursion
  5136. (save-restriction
  5137. (widen)
  5138. (show-all)
  5139. (mapc (lambda (c)
  5140. (setq o (org-make-overlay (car c) (cdr c)))
  5141. (org-overlay-put o 'invisible 'outline))
  5142. data)))))
  5143. (defmacro org-save-outline-visibility (use-markers &rest body)
  5144. "Save and restore outline visibility around BODY.
  5145. If USE-MARKERS is non-nil, use markers for the positions.
  5146. This means that the buffer may change while running BODY,
  5147. but it also means that the buffer should stay alive
  5148. during the operation, because otherwise all these markers will
  5149. point nowhere."
  5150. `(let ((data (org-outline-overlay-data ,use-markers)))
  5151. (unwind-protect
  5152. (progn
  5153. ,@body
  5154. (org-set-outline-overlay-data data))
  5155. (when ,use-markers
  5156. (mapc (lambda (c)
  5157. (and (markerp (car c)) (move-marker (car c) nil))
  5158. (and (markerp (cdr c)) (move-marker (cdr c) nil)))
  5159. data)))))
  5160. ;;; Folding of blocks
  5161. (defconst org-block-regexp
  5162. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  5163. "Regular expression for hiding blocks.")
  5164. (defvar org-hide-block-overlays nil
  5165. "Overlays hiding blocks.")
  5166. (make-variable-buffer-local 'org-hide-block-overlays)
  5167. (defun org-block-map (function &optional start end)
  5168. "Call func at the head of all source blocks in the current
  5169. buffer. Optional arguments START and END can be used to limit
  5170. the range."
  5171. (let ((start (or start (point-min)))
  5172. (end (or end (point-max))))
  5173. (save-excursion
  5174. (goto-char start)
  5175. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5176. (save-excursion
  5177. (save-match-data
  5178. (goto-char (match-beginning 0))
  5179. (funcall function)))))))
  5180. (defun org-hide-block-toggle-all ()
  5181. "Toggle the visibility of all blocks in the current buffer."
  5182. (org-block-map #'org-hide-block-toggle))
  5183. (defun org-hide-block-all ()
  5184. "Fold all blocks in the current buffer."
  5185. (interactive)
  5186. (org-show-block-all)
  5187. (org-block-map #'org-hide-block-toggle-maybe))
  5188. (defun org-show-block-all ()
  5189. "Unfold all blocks in the current buffer."
  5190. (mapc 'org-delete-overlay org-hide-block-overlays)
  5191. (setq org-hide-block-overlays nil))
  5192. (defun org-hide-block-toggle-maybe ()
  5193. "Toggle visibility of block at point."
  5194. (interactive)
  5195. (let ((case-fold-search t))
  5196. (if (save-excursion
  5197. (beginning-of-line 1)
  5198. (looking-at org-block-regexp))
  5199. (progn (org-hide-block-toggle)
  5200. t) ;; to signal that we took action
  5201. nil))) ;; to signal that we did not
  5202. (defun org-hide-block-toggle (&optional force)
  5203. "Toggle the visibility of the current block."
  5204. (interactive)
  5205. (save-excursion
  5206. (beginning-of-line)
  5207. (if (re-search-forward org-block-regexp nil t)
  5208. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5209. (end (match-end 0)) ;; end of entire body
  5210. ov)
  5211. (if (memq t (mapcar (lambda (overlay)
  5212. (eq (org-overlay-get overlay 'invisible)
  5213. 'org-hide-block))
  5214. (org-overlays-at start)))
  5215. (if (or (not force) (eq force 'off))
  5216. (mapc (lambda (ov)
  5217. (when (member ov org-hide-block-overlays)
  5218. (setq org-hide-block-overlays
  5219. (delq ov org-hide-block-overlays)))
  5220. (when (eq (org-overlay-get ov 'invisible)
  5221. 'org-hide-block)
  5222. (org-delete-overlay ov)))
  5223. (org-overlays-at start)))
  5224. (setq ov (org-make-overlay start end))
  5225. (org-overlay-put ov 'invisible 'org-hide-block)
  5226. ;; make the block accessible to isearch
  5227. (org-overlay-put
  5228. ov 'isearch-open-invisible
  5229. (lambda (ov)
  5230. (when (member ov org-hide-block-overlays)
  5231. (setq org-hide-block-overlays
  5232. (delq ov org-hide-block-overlays)))
  5233. (when (eq (org-overlay-get ov 'invisible)
  5234. 'org-hide-block)
  5235. (org-delete-overlay ov))))
  5236. (push ov org-hide-block-overlays)))
  5237. (error "Not looking at a source block"))))
  5238. ;; org-tab-after-check-for-cycling-hook
  5239. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5240. ;; Remove overlays when changing major mode
  5241. (add-hook 'org-mode-hook
  5242. (lambda () (org-add-hook 'change-major-mode-hook
  5243. 'org-show-block-all 'append 'local)))
  5244. ;;; Org-goto
  5245. (defvar org-goto-window-configuration nil)
  5246. (defvar org-goto-marker nil)
  5247. (defvar org-goto-map
  5248. (let ((map (make-sparse-keymap)))
  5249. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5250. (while (setq cmd (pop cmds))
  5251. (substitute-key-definition cmd cmd map global-map)))
  5252. (suppress-keymap map)
  5253. (org-defkey map "\C-m" 'org-goto-ret)
  5254. (org-defkey map [(return)] 'org-goto-ret)
  5255. (org-defkey map [(left)] 'org-goto-left)
  5256. (org-defkey map [(right)] 'org-goto-right)
  5257. (org-defkey map [(control ?g)] 'org-goto-quit)
  5258. (org-defkey map "\C-i" 'org-cycle)
  5259. (org-defkey map [(tab)] 'org-cycle)
  5260. (org-defkey map [(down)] 'outline-next-visible-heading)
  5261. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5262. (if org-goto-auto-isearch
  5263. (if (fboundp 'define-key-after)
  5264. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5265. nil)
  5266. (org-defkey map "q" 'org-goto-quit)
  5267. (org-defkey map "n" 'outline-next-visible-heading)
  5268. (org-defkey map "p" 'outline-previous-visible-heading)
  5269. (org-defkey map "f" 'outline-forward-same-level)
  5270. (org-defkey map "b" 'outline-backward-same-level)
  5271. (org-defkey map "u" 'outline-up-heading))
  5272. (org-defkey map "/" 'org-occur)
  5273. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5274. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5275. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5276. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5277. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5278. map))
  5279. (defconst org-goto-help
  5280. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5281. RET=jump to location [Q]uit and return to previous location
  5282. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5283. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5284. ;; FIXME: Docstring does not mention both interfaces
  5285. (defun org-goto (&optional alternative-interface)
  5286. "Look up a different location in the current file, keeping current visibility.
  5287. When you want look-up or go to a different location in a document, the
  5288. fastest way is often to fold the entire buffer and then dive into the tree.
  5289. This method has the disadvantage, that the previous location will be folded,
  5290. which may not be what you want.
  5291. This command works around this by showing a copy of the current buffer
  5292. in an indirect buffer, in overview mode. You can dive into the tree in
  5293. that copy, use org-occur and incremental search to find a location.
  5294. When pressing RET or `Q', the command returns to the original buffer in
  5295. which the visibility is still unchanged. After RET is will also jump to
  5296. the location selected in the indirect buffer and expose the
  5297. the headline hierarchy above."
  5298. (interactive "P")
  5299. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5300. (org-refile-use-outline-path t)
  5301. (org-refile-target-verify-function nil)
  5302. (interface
  5303. (if (not alternative-interface)
  5304. org-goto-interface
  5305. (if (eq org-goto-interface 'outline)
  5306. 'outline-path-completion
  5307. 'outline)))
  5308. (org-goto-start-pos (point))
  5309. (selected-point
  5310. (if (eq interface 'outline)
  5311. (car (org-get-location (current-buffer) org-goto-help))
  5312. (nth 3 (org-refile-get-location "Goto: ")))))
  5313. (if selected-point
  5314. (progn
  5315. (org-mark-ring-push org-goto-start-pos)
  5316. (goto-char selected-point)
  5317. (if (or (org-invisible-p) (org-invisible-p2))
  5318. (org-show-context 'org-goto)))
  5319. (message "Quit"))))
  5320. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5321. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5322. (defvar org-goto-local-auto-isearch-map) ; defined below
  5323. (defun org-get-location (buf help)
  5324. "Let the user select a location in the Org-mode buffer BUF.
  5325. This function uses a recursive edit. It returns the selected position
  5326. or nil."
  5327. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5328. (isearch-hide-immediately nil)
  5329. (isearch-search-fun-function
  5330. (lambda () 'org-goto-local-search-headings))
  5331. (org-goto-selected-point org-goto-exit-command)
  5332. (pop-up-frames nil)
  5333. (special-display-buffer-names nil)
  5334. (special-display-regexps nil)
  5335. (special-display-function nil))
  5336. (save-excursion
  5337. (save-window-excursion
  5338. (delete-other-windows)
  5339. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5340. (switch-to-buffer
  5341. (condition-case nil
  5342. (make-indirect-buffer (current-buffer) "*org-goto*")
  5343. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5344. (with-output-to-temp-buffer "*Help*"
  5345. (princ help))
  5346. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5347. (setq buffer-read-only nil)
  5348. (let ((org-startup-truncated t)
  5349. (org-startup-folded nil)
  5350. (org-startup-align-all-tables nil))
  5351. (org-mode)
  5352. (org-overview))
  5353. (setq buffer-read-only t)
  5354. (if (and (boundp 'org-goto-start-pos)
  5355. (integer-or-marker-p org-goto-start-pos))
  5356. (let ((org-show-hierarchy-above t)
  5357. (org-show-siblings t)
  5358. (org-show-following-heading t))
  5359. (goto-char org-goto-start-pos)
  5360. (and (org-invisible-p) (org-show-context)))
  5361. (goto-char (point-min)))
  5362. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5363. (message "Select location and press RET")
  5364. (use-local-map org-goto-map)
  5365. (recursive-edit)
  5366. ))
  5367. (kill-buffer "*org-goto*")
  5368. (cons org-goto-selected-point org-goto-exit-command)))
  5369. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5370. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5371. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5372. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5373. (defun org-goto-local-search-headings (string bound noerror)
  5374. "Search and make sure that any matches are in headlines."
  5375. (catch 'return
  5376. (while (if isearch-forward
  5377. (search-forward string bound noerror)
  5378. (search-backward string bound noerror))
  5379. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5380. (and (member :headline context)
  5381. (not (member :tags context))))
  5382. (throw 'return (point))))))
  5383. (defun org-goto-local-auto-isearch ()
  5384. "Start isearch."
  5385. (interactive)
  5386. (goto-char (point-min))
  5387. (let ((keys (this-command-keys)))
  5388. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5389. (isearch-mode t)
  5390. (isearch-process-search-char (string-to-char keys)))))
  5391. (defun org-goto-ret (&optional arg)
  5392. "Finish `org-goto' by going to the new location."
  5393. (interactive "P")
  5394. (setq org-goto-selected-point (point)
  5395. org-goto-exit-command 'return)
  5396. (throw 'exit nil))
  5397. (defun org-goto-left ()
  5398. "Finish `org-goto' by going to the new location."
  5399. (interactive)
  5400. (if (org-on-heading-p)
  5401. (progn
  5402. (beginning-of-line 1)
  5403. (setq org-goto-selected-point (point)
  5404. org-goto-exit-command 'left)
  5405. (throw 'exit nil))
  5406. (error "Not on a heading")))
  5407. (defun org-goto-right ()
  5408. "Finish `org-goto' by going to the new location."
  5409. (interactive)
  5410. (if (org-on-heading-p)
  5411. (progn
  5412. (setq org-goto-selected-point (point)
  5413. org-goto-exit-command 'right)
  5414. (throw 'exit nil))
  5415. (error "Not on a heading")))
  5416. (defun org-goto-quit ()
  5417. "Finish `org-goto' without cursor motion."
  5418. (interactive)
  5419. (setq org-goto-selected-point nil)
  5420. (setq org-goto-exit-command 'quit)
  5421. (throw 'exit nil))
  5422. ;;; Indirect buffer display of subtrees
  5423. (defvar org-indirect-dedicated-frame nil
  5424. "This is the frame being used for indirect tree display.")
  5425. (defvar org-last-indirect-buffer nil)
  5426. (defun org-tree-to-indirect-buffer (&optional arg)
  5427. "Create indirect buffer and narrow it to current subtree.
  5428. With numerical prefix ARG, go up to this level and then take that tree.
  5429. If ARG is negative, go up that many levels.
  5430. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5431. indirect buffer previously made with this command, to avoid proliferation of
  5432. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5433. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5434. is kept so that you can work with several indirect buffers at the same time.
  5435. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5436. requests that a new frame be made for the new buffer, so that the dedicated
  5437. frame is not changed."
  5438. (interactive "P")
  5439. (let ((cbuf (current-buffer))
  5440. (cwin (selected-window))
  5441. (pos (point))
  5442. beg end level heading ibuf)
  5443. (save-excursion
  5444. (org-back-to-heading t)
  5445. (when (numberp arg)
  5446. (setq level (org-outline-level))
  5447. (if (< arg 0) (setq arg (+ level arg)))
  5448. (while (> (setq level (org-outline-level)) arg)
  5449. (outline-up-heading 1 t)))
  5450. (setq beg (point)
  5451. heading (org-get-heading))
  5452. (org-end-of-subtree t t)
  5453. (if (org-on-heading-p) (backward-char 1))
  5454. (setq end (point)))
  5455. (if (and (buffer-live-p org-last-indirect-buffer)
  5456. (not (eq org-indirect-buffer-display 'new-frame))
  5457. (not arg))
  5458. (kill-buffer org-last-indirect-buffer))
  5459. (setq ibuf (org-get-indirect-buffer cbuf)
  5460. org-last-indirect-buffer ibuf)
  5461. (cond
  5462. ((or (eq org-indirect-buffer-display 'new-frame)
  5463. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5464. (select-frame (make-frame))
  5465. (delete-other-windows)
  5466. (switch-to-buffer ibuf)
  5467. (org-set-frame-title heading))
  5468. ((eq org-indirect-buffer-display 'dedicated-frame)
  5469. (raise-frame
  5470. (select-frame (or (and org-indirect-dedicated-frame
  5471. (frame-live-p org-indirect-dedicated-frame)
  5472. org-indirect-dedicated-frame)
  5473. (setq org-indirect-dedicated-frame (make-frame)))))
  5474. (delete-other-windows)
  5475. (switch-to-buffer ibuf)
  5476. (org-set-frame-title (concat "Indirect: " heading)))
  5477. ((eq org-indirect-buffer-display 'current-window)
  5478. (switch-to-buffer ibuf))
  5479. ((eq org-indirect-buffer-display 'other-window)
  5480. (pop-to-buffer ibuf))
  5481. (t (error "Invalid value")))
  5482. (if (featurep 'xemacs)
  5483. (save-excursion (org-mode) (turn-on-font-lock)))
  5484. (narrow-to-region beg end)
  5485. (show-all)
  5486. (goto-char pos)
  5487. (and (window-live-p cwin) (select-window cwin))))
  5488. (defun org-get-indirect-buffer (&optional buffer)
  5489. (setq buffer (or buffer (current-buffer)))
  5490. (let ((n 1) (base (buffer-name buffer)) bname)
  5491. (while (buffer-live-p
  5492. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5493. (setq n (1+ n)))
  5494. (condition-case nil
  5495. (make-indirect-buffer buffer bname 'clone)
  5496. (error (make-indirect-buffer buffer bname)))))
  5497. (defun org-set-frame-title (title)
  5498. "Set the title of the current frame to the string TITLE."
  5499. ;; FIXME: how to name a single frame in XEmacs???
  5500. (unless (featurep 'xemacs)
  5501. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5502. ;;;; Structure editing
  5503. ;;; Inserting headlines
  5504. (defun org-previous-line-empty-p ()
  5505. (save-excursion
  5506. (and (not (bobp))
  5507. (or (beginning-of-line 0) t)
  5508. (save-match-data
  5509. (looking-at "[ \t]*$")))))
  5510. (defun org-insert-heading (&optional force-heading invisible-ok)
  5511. "Insert a new heading or item with same depth at point.
  5512. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5513. If point is at the beginning of a headline, insert a sibling before the
  5514. current headline. If point is not at the beginning, do not split the line,
  5515. but create the new headline after the current line.
  5516. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  5517. This is important for non-interactive uses of the command."
  5518. (interactive "P")
  5519. (if (or (= (buffer-size) 0)
  5520. (and (not (save-excursion (and (ignore-errors (org-back-to-heading invisible-ok))
  5521. (org-on-heading-p))))
  5522. (not (org-in-item-p))))
  5523. (insert "\n* ")
  5524. (when (or force-heading (not (org-insert-item)))
  5525. (let* ((empty-line-p nil)
  5526. (head (save-excursion
  5527. (condition-case nil
  5528. (progn
  5529. (org-back-to-heading invisible-ok)
  5530. (setq empty-line-p (org-previous-line-empty-p))
  5531. (match-string 0))
  5532. (error "*"))))
  5533. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5534. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5535. pos hide-previous previous-pos)
  5536. (cond
  5537. ((and (org-on-heading-p) (bolp)
  5538. (or (bobp)
  5539. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5540. ;; insert before the current line
  5541. (open-line (if blank 2 1)))
  5542. ((and (bolp)
  5543. (not org-insert-heading-respect-content)
  5544. (or (bobp)
  5545. (save-excursion
  5546. (backward-char 1) (not (org-invisible-p)))))
  5547. ;; insert right here
  5548. nil)
  5549. (t
  5550. ;; somewhere in the line
  5551. (save-excursion
  5552. (setq previous-pos (point-at-bol))
  5553. (end-of-line)
  5554. (setq hide-previous (org-invisible-p)))
  5555. (and org-insert-heading-respect-content (org-show-subtree))
  5556. (let ((split
  5557. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5558. (save-excursion
  5559. (let ((p (point)))
  5560. (goto-char (point-at-bol))
  5561. (and (looking-at org-complex-heading-regexp)
  5562. (> p (match-beginning 4)))))))
  5563. tags pos)
  5564. (cond
  5565. (org-insert-heading-respect-content
  5566. (org-end-of-subtree nil t)
  5567. (or (bolp) (newline))
  5568. (or (org-previous-line-empty-p)
  5569. (and blank (newline)))
  5570. (open-line 1))
  5571. ((org-on-heading-p)
  5572. (when hide-previous
  5573. (show-children)
  5574. (org-show-entry))
  5575. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5576. (setq tags (and (match-end 2) (match-string 2)))
  5577. (and (match-end 1)
  5578. (delete-region (match-beginning 1) (match-end 1)))
  5579. (setq pos (point-at-bol))
  5580. (or split (end-of-line 1))
  5581. (delete-horizontal-space)
  5582. (if (string-match "\\`\\*+\\'"
  5583. (buffer-substring (point-at-bol) (point)))
  5584. (insert " "))
  5585. (newline (if blank 2 1))
  5586. (when tags
  5587. (save-excursion
  5588. (goto-char pos)
  5589. (end-of-line 1)
  5590. (insert " " tags)
  5591. (org-set-tags nil 'align))))
  5592. (t
  5593. (or split (end-of-line 1))
  5594. (newline (if blank 2 1)))))))
  5595. (insert head) (just-one-space)
  5596. (setq pos (point))
  5597. (end-of-line 1)
  5598. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5599. (when (and org-insert-heading-respect-content hide-previous)
  5600. (save-excursion
  5601. (goto-char previous-pos)
  5602. (hide-subtree)))
  5603. (run-hooks 'org-insert-heading-hook)))))
  5604. (defun org-get-heading (&optional no-tags)
  5605. "Return the heading of the current entry, without the stars."
  5606. (save-excursion
  5607. (org-back-to-heading t)
  5608. (if (looking-at
  5609. (if no-tags
  5610. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5611. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5612. (match-string 1) "")))
  5613. (defun org-heading-components ()
  5614. "Return the components of the current heading.
  5615. This is a list with the following elements:
  5616. - the level as an integer
  5617. - the reduced level, different if `org-odd-levels-only' is set.
  5618. - the TODO keyword, or nil
  5619. - the priority character, like ?A, or nil if no priority is given
  5620. - the headline text itself, or the tags string if no headline text
  5621. - the tags string, or nil."
  5622. (save-excursion
  5623. (org-back-to-heading t)
  5624. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  5625. (list (length (match-string 1))
  5626. (org-reduced-level (length (match-string 1)))
  5627. (org-match-string-no-properties 2)
  5628. (and (match-end 3) (aref (match-string 3) 2))
  5629. (org-match-string-no-properties 4)
  5630. (org-match-string-no-properties 5)))))
  5631. (defun org-get-entry ()
  5632. "Get the entry text, after heading, entire subtree."
  5633. (save-excursion
  5634. (org-back-to-heading t)
  5635. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5636. (defun org-insert-heading-after-current ()
  5637. "Insert a new heading with same level as current, after current subtree."
  5638. (interactive)
  5639. (org-back-to-heading)
  5640. (org-insert-heading)
  5641. (org-move-subtree-down)
  5642. (end-of-line 1))
  5643. (defun org-insert-heading-respect-content ()
  5644. (interactive)
  5645. (let ((org-insert-heading-respect-content t))
  5646. (org-insert-heading t)))
  5647. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5648. (interactive "P")
  5649. (let ((org-insert-heading-respect-content t))
  5650. (org-insert-todo-heading force-state t)))
  5651. (defun org-insert-todo-heading (arg &optional force-heading)
  5652. "Insert a new heading with the same level and TODO state as current heading.
  5653. If the heading has no TODO state, or if the state is DONE, use the first
  5654. state (TODO by default). Also with prefix arg, force first state."
  5655. (interactive "P")
  5656. (when (or force-heading (not (org-insert-item 'checkbox)))
  5657. (org-insert-heading force-heading)
  5658. (save-excursion
  5659. (org-back-to-heading)
  5660. (outline-previous-heading)
  5661. (looking-at org-todo-line-regexp))
  5662. (let*
  5663. ((new-mark-x
  5664. (if (or arg
  5665. (not (match-beginning 2))
  5666. (member (match-string 2) org-done-keywords))
  5667. (car org-todo-keywords-1)
  5668. (match-string 2)))
  5669. (new-mark
  5670. (or
  5671. (run-hook-with-args-until-success
  5672. 'org-todo-get-default-hook new-mark-x nil)
  5673. new-mark-x)))
  5674. (beginning-of-line 1)
  5675. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5676. (if org-treat-insert-todo-heading-as-state-change
  5677. (org-todo new-mark)
  5678. (insert new-mark " "))))
  5679. (when org-provide-todo-statistics
  5680. (org-update-parent-todo-statistics))))
  5681. (defun org-insert-subheading (arg)
  5682. "Insert a new subheading and demote it.
  5683. Works for outline headings and for plain lists alike."
  5684. (interactive "P")
  5685. (org-insert-heading arg)
  5686. (cond
  5687. ((org-on-heading-p) (org-do-demote))
  5688. ((org-at-item-p) (org-indent-item 1))))
  5689. (defun org-insert-todo-subheading (arg)
  5690. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5691. Works for outline headings and for plain lists alike."
  5692. (interactive "P")
  5693. (org-insert-todo-heading arg)
  5694. (cond
  5695. ((org-on-heading-p) (org-do-demote))
  5696. ((org-at-item-p) (org-indent-item 1))))
  5697. ;;; Promotion and Demotion
  5698. (defvar org-after-demote-entry-hook nil
  5699. "Hook run after an entry has been demoted.
  5700. The cursor will be at the beginning of the entry.
  5701. When a subtree is being demoted, the hook will be called for each node.")
  5702. (defvar org-after-promote-entry-hook nil
  5703. "Hook run after an entry has been promoted.
  5704. The cursor will be at the beginning of the entry.
  5705. When a subtree is being promoted, the hook will be called for each node.")
  5706. (defun org-promote-subtree ()
  5707. "Promote the entire subtree.
  5708. See also `org-promote'."
  5709. (interactive)
  5710. (save-excursion
  5711. (org-map-tree 'org-promote))
  5712. (org-fix-position-after-promote))
  5713. (defun org-demote-subtree ()
  5714. "Demote the entire subtree. See `org-demote'.
  5715. See also `org-promote'."
  5716. (interactive)
  5717. (save-excursion
  5718. (org-map-tree 'org-demote))
  5719. (org-fix-position-after-promote))
  5720. (defun org-do-promote ()
  5721. "Promote the current heading higher up the tree.
  5722. If the region is active in `transient-mark-mode', promote all headings
  5723. in the region."
  5724. (interactive)
  5725. (save-excursion
  5726. (if (org-region-active-p)
  5727. (org-map-region 'org-promote (region-beginning) (region-end))
  5728. (org-promote)))
  5729. (org-fix-position-after-promote))
  5730. (defun org-do-demote ()
  5731. "Demote the current heading lower down the tree.
  5732. If the region is active in `transient-mark-mode', demote all headings
  5733. in the region."
  5734. (interactive)
  5735. (save-excursion
  5736. (if (org-region-active-p)
  5737. (org-map-region 'org-demote (region-beginning) (region-end))
  5738. (org-demote)))
  5739. (org-fix-position-after-promote))
  5740. (defun org-fix-position-after-promote ()
  5741. "Make sure that after pro/demotion cursor position is right."
  5742. (let ((pos (point)))
  5743. (when (save-excursion
  5744. (beginning-of-line 1)
  5745. (looking-at org-todo-line-regexp)
  5746. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5747. (cond ((eobp) (insert " "))
  5748. ((eolp) (insert " "))
  5749. ((equal (char-after) ?\ ) (forward-char 1))))))
  5750. (defun org-current-level ()
  5751. "Return the level of the current entry, or nil if before the first headline.
  5752. The level is the number of stars at the beginning of the headline."
  5753. (save-excursion
  5754. (condition-case nil
  5755. (progn
  5756. (org-back-to-heading t)
  5757. (funcall outline-level))
  5758. (error nil))))
  5759. (defun org-get-previous-line-level ()
  5760. "Return the outline depth of the last headline before the current line.
  5761. Returns 0 for the first headline in the buffer, and nil if before the
  5762. first headline."
  5763. (let ((current-level (org-current-level))
  5764. (prev-level (when (> (line-number-at-pos) 1)
  5765. (save-excursion
  5766. (beginning-of-line 0)
  5767. (org-current-level)))))
  5768. (cond ((null current-level) nil) ; Before first headline
  5769. ((null prev-level) 0) ; At first headline
  5770. (prev-level))))
  5771. (defun org-reduced-level (l)
  5772. "Compute the effective level of a heading.
  5773. This takes into account the setting of `org-odd-levels-only'."
  5774. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5775. (defun org-level-increment ()
  5776. "Return the number of stars that will be added or removed at a
  5777. time to headlines when structure editing, based on the value of
  5778. `org-odd-levels-only'."
  5779. (if org-odd-levels-only 2 1))
  5780. (defun org-get-valid-level (level &optional change)
  5781. "Rectify a level change under the influence of `org-odd-levels-only'
  5782. LEVEL is a current level, CHANGE is by how much the level should be
  5783. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5784. even level numbers will become the next higher odd number."
  5785. (if org-odd-levels-only
  5786. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5787. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5788. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5789. (max 1 (+ level (or change 0)))))
  5790. (if (boundp 'define-obsolete-function-alias)
  5791. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5792. (define-obsolete-function-alias 'org-get-legal-level
  5793. 'org-get-valid-level)
  5794. (define-obsolete-function-alias 'org-get-legal-level
  5795. 'org-get-valid-level "23.1")))
  5796. (defun org-promote ()
  5797. "Promote the current heading higher up the tree.
  5798. If the region is active in `transient-mark-mode', promote all headings
  5799. in the region."
  5800. (org-back-to-heading t)
  5801. (let* ((level (save-match-data (funcall outline-level)))
  5802. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5803. (diff (abs (- level (length up-head) -1))))
  5804. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5805. (replace-match up-head nil t)
  5806. ;; Fixup tag positioning
  5807. (and org-auto-align-tags (org-set-tags nil t))
  5808. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5809. (run-hooks 'org-after-promote-entry-hook)))
  5810. (defun org-demote ()
  5811. "Demote the current heading lower down the tree.
  5812. If the region is active in `transient-mark-mode', demote all headings
  5813. in the region."
  5814. (org-back-to-heading t)
  5815. (let* ((level (save-match-data (funcall outline-level)))
  5816. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5817. (diff (abs (- level (length down-head) -1))))
  5818. (replace-match down-head nil t)
  5819. ;; Fixup tag positioning
  5820. (and org-auto-align-tags (org-set-tags nil t))
  5821. (if org-adapt-indentation (org-fixup-indentation diff))
  5822. (run-hooks 'org-after-demote-entry-hook)))
  5823. (defun org-cycle-level ()
  5824. "Cycle the level of an empty headline through possible states.
  5825. This goes first to child, then to parent, level, then up the hierarchy.
  5826. After top level, it switches back to sibling level."
  5827. (interactive)
  5828. (let ((org-adapt-indentation nil))
  5829. (when (org-point-at-end-of-empty-headline)
  5830. (setq this-command 'org-cycle-level) ; Only needed for caching
  5831. (let ((cur-level (org-current-level))
  5832. (prev-level (org-get-previous-line-level)))
  5833. (cond
  5834. ;; If first headline in file, promote to top-level.
  5835. ((= prev-level 0)
  5836. (loop repeat (/ (- cur-level 1) (org-level-increment))
  5837. do (org-do-promote)))
  5838. ;; If same level as prev, demote one.
  5839. ((= prev-level cur-level)
  5840. (org-do-demote))
  5841. ;; If parent is top-level, promote to top level if not already.
  5842. ((= prev-level 1)
  5843. (loop repeat (/ (- cur-level 1) (org-level-increment))
  5844. do (org-do-promote)))
  5845. ;; If top-level, return to prev-level.
  5846. ((= cur-level 1)
  5847. (loop repeat (/ (- prev-level 1) (org-level-increment))
  5848. do (org-do-demote)))
  5849. ;; If less than prev-level, promote one.
  5850. ((< cur-level prev-level)
  5851. (org-do-promote))
  5852. ;; If deeper than prev-level, promote until higher than
  5853. ;; prev-level.
  5854. ((> cur-level prev-level)
  5855. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  5856. do (org-do-promote))))
  5857. t))))
  5858. (defun org-map-tree (fun)
  5859. "Call FUN for every heading underneath the current one."
  5860. (org-back-to-heading)
  5861. (let ((level (funcall outline-level)))
  5862. (save-excursion
  5863. (funcall fun)
  5864. (while (and (progn
  5865. (outline-next-heading)
  5866. (> (funcall outline-level) level))
  5867. (not (eobp)))
  5868. (funcall fun)))))
  5869. (defun org-map-region (fun beg end)
  5870. "Call FUN for every heading between BEG and END."
  5871. (let ((org-ignore-region t))
  5872. (save-excursion
  5873. (setq end (copy-marker end))
  5874. (goto-char beg)
  5875. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5876. (< (point) end))
  5877. (funcall fun))
  5878. (while (and (progn
  5879. (outline-next-heading)
  5880. (< (point) end))
  5881. (not (eobp)))
  5882. (funcall fun)))))
  5883. (defun org-fixup-indentation (diff)
  5884. "Change the indentation in the current entry by DIFF
  5885. However, if any line in the current entry has no indentation, or if it
  5886. would end up with no indentation after the change, nothing at all is done."
  5887. (save-excursion
  5888. (let ((end (save-excursion (outline-next-heading)
  5889. (point-marker)))
  5890. (prohibit (if (> diff 0)
  5891. "^\\S-"
  5892. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5893. col)
  5894. (unless (save-excursion (end-of-line 1)
  5895. (re-search-forward prohibit end t))
  5896. (while (and (< (point) end)
  5897. (re-search-forward "^[ \t]+" end t))
  5898. (goto-char (match-end 0))
  5899. (setq col (current-column))
  5900. (if (< diff 0) (replace-match ""))
  5901. (org-indent-to-column (+ diff col))))
  5902. (move-marker end nil))))
  5903. (defun org-convert-to-odd-levels ()
  5904. "Convert an org-mode file with all levels allowed to one with odd levels.
  5905. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5906. level 5 etc."
  5907. (interactive)
  5908. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5909. (let ((outline-regexp org-outline-regexp)
  5910. (outline-level 'org-outline-level)
  5911. (org-odd-levels-only nil) n)
  5912. (save-excursion
  5913. (goto-char (point-min))
  5914. (while (re-search-forward "^\\*\\*+ " nil t)
  5915. (setq n (- (length (match-string 0)) 2))
  5916. (while (>= (setq n (1- n)) 0)
  5917. (org-demote))
  5918. (end-of-line 1))))))
  5919. (defun org-convert-to-oddeven-levels ()
  5920. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5921. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5922. section with an even level, conversion would destroy the structure of the file. An error
  5923. is signaled in this case."
  5924. (interactive)
  5925. (goto-char (point-min))
  5926. ;; First check if there are no even levels
  5927. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5928. (org-show-context t)
  5929. (error "Not all levels are odd in this file. Conversion not possible"))
  5930. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5931. (let ((outline-regexp org-outline-regexp)
  5932. (outline-level 'org-outline-level)
  5933. (org-odd-levels-only nil) n)
  5934. (save-excursion
  5935. (goto-char (point-min))
  5936. (while (re-search-forward "^\\*\\*+ " nil t)
  5937. (setq n (/ (1- (length (match-string 0))) 2))
  5938. (while (>= (setq n (1- n)) 0)
  5939. (org-promote))
  5940. (end-of-line 1))))))
  5941. (defun org-tr-level (n)
  5942. "Make N odd if required."
  5943. (if org-odd-levels-only (1+ (/ n 2)) n))
  5944. ;;; Vertical tree motion, cutting and pasting of subtrees
  5945. (defun org-move-subtree-up (&optional arg)
  5946. "Move the current subtree up past ARG headlines of the same level."
  5947. (interactive "p")
  5948. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5949. (defun org-move-subtree-down (&optional arg)
  5950. "Move the current subtree down past ARG headlines of the same level."
  5951. (interactive "p")
  5952. (setq arg (prefix-numeric-value arg))
  5953. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  5954. 'org-get-last-sibling))
  5955. (ins-point (make-marker))
  5956. (cnt (abs arg))
  5957. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5958. ;; Select the tree
  5959. (org-back-to-heading)
  5960. (setq beg0 (point))
  5961. (save-excursion
  5962. (setq ne-beg (org-back-over-empty-lines))
  5963. (setq beg (point)))
  5964. (save-match-data
  5965. (save-excursion (outline-end-of-heading)
  5966. (setq folded (org-invisible-p)))
  5967. (outline-end-of-subtree))
  5968. (outline-next-heading)
  5969. (setq ne-end (org-back-over-empty-lines))
  5970. (setq end (point))
  5971. (goto-char beg0)
  5972. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5973. ;; include less whitespace
  5974. (save-excursion
  5975. (goto-char beg)
  5976. (forward-line (- ne-beg ne-end))
  5977. (setq beg (point))))
  5978. ;; Find insertion point, with error handling
  5979. (while (> cnt 0)
  5980. (or (and (funcall movfunc) (looking-at outline-regexp))
  5981. (progn (goto-char beg0)
  5982. (error "Cannot move past superior level or buffer limit")))
  5983. (setq cnt (1- cnt)))
  5984. (if (> arg 0)
  5985. ;; Moving forward - still need to move over subtree
  5986. (progn (org-end-of-subtree t t)
  5987. (save-excursion
  5988. (org-back-over-empty-lines)
  5989. (or (bolp) (newline)))))
  5990. (setq ne-ins (org-back-over-empty-lines))
  5991. (move-marker ins-point (point))
  5992. (setq txt (buffer-substring beg end))
  5993. (org-save-markers-in-region beg end)
  5994. (delete-region beg end)
  5995. (org-remove-empty-overlays-at beg)
  5996. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5997. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5998. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5999. (let ((bbb (point)))
  6000. (insert-before-markers txt)
  6001. (org-reinstall-markers-in-region bbb)
  6002. (move-marker ins-point bbb))
  6003. (or (bolp) (insert "\n"))
  6004. (setq ins-end (point))
  6005. (goto-char ins-point)
  6006. (org-skip-whitespace)
  6007. (when (and (< arg 0)
  6008. (org-first-sibling-p)
  6009. (> ne-ins ne-beg))
  6010. ;; Move whitespace back to beginning
  6011. (save-excursion
  6012. (goto-char ins-end)
  6013. (let ((kill-whole-line t))
  6014. (kill-line (- ne-ins ne-beg)) (point)))
  6015. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6016. (move-marker ins-point nil)
  6017. (if folded
  6018. (hide-subtree)
  6019. (org-show-entry)
  6020. (show-children)
  6021. (org-cycle-hide-drawers 'children))
  6022. (org-clean-visibility-after-subtree-move)))
  6023. (defvar org-subtree-clip ""
  6024. "Clipboard for cut and paste of subtrees.
  6025. This is actually only a copy of the kill, because we use the normal kill
  6026. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6027. (defvar org-subtree-clip-folded nil
  6028. "Was the last copied subtree folded?
  6029. This is used to fold the tree back after pasting.")
  6030. (defun org-cut-subtree (&optional n)
  6031. "Cut the current subtree into the clipboard.
  6032. With prefix arg N, cut this many sequential subtrees.
  6033. This is a short-hand for marking the subtree and then cutting it."
  6034. (interactive "p")
  6035. (org-copy-subtree n 'cut))
  6036. (defun org-copy-subtree (&optional n cut force-store-markers)
  6037. "Cut the current subtree into the clipboard.
  6038. With prefix arg N, cut this many sequential subtrees.
  6039. This is a short-hand for marking the subtree and then copying it.
  6040. If CUT is non-nil, actually cut the subtree.
  6041. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6042. of some markers in the region, even if CUT is non-nil. This is
  6043. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6044. (interactive "p")
  6045. (let (beg end folded (beg0 (point)))
  6046. (if (interactive-p)
  6047. (org-back-to-heading nil) ; take what looks like a subtree
  6048. (org-back-to-heading t)) ; take what is really there
  6049. (org-back-over-empty-lines)
  6050. (setq beg (point))
  6051. (skip-chars-forward " \t\r\n")
  6052. (save-match-data
  6053. (save-excursion (outline-end-of-heading)
  6054. (setq folded (org-invisible-p)))
  6055. (condition-case nil
  6056. (org-forward-same-level (1- n) t)
  6057. (error nil))
  6058. (org-end-of-subtree t t))
  6059. (org-back-over-empty-lines)
  6060. (setq end (point))
  6061. (goto-char beg0)
  6062. (when (> end beg)
  6063. (setq org-subtree-clip-folded folded)
  6064. (when (or cut force-store-markers)
  6065. (org-save-markers-in-region beg end))
  6066. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6067. (setq org-subtree-clip (current-kill 0))
  6068. (message "%s: Subtree(s) with %d characters"
  6069. (if cut "Cut" "Copied")
  6070. (length org-subtree-clip)))))
  6071. (defun org-paste-subtree (&optional level tree for-yank)
  6072. "Paste the clipboard as a subtree, with modification of headline level.
  6073. The entire subtree is promoted or demoted in order to match a new headline
  6074. level.
  6075. If the cursor is at the beginning of a headline, the same level as
  6076. that headline is used to paste the tree
  6077. If not, the new level is derived from the *visible* headings
  6078. before and after the insertion point, and taken to be the inferior headline
  6079. level of the two. So if the previous visible heading is level 3 and the
  6080. next is level 4 (or vice versa), level 4 will be used for insertion.
  6081. This makes sure that the subtree remains an independent subtree and does
  6082. not swallow low level entries.
  6083. You can also force a different level, either by using a numeric prefix
  6084. argument, or by inserting the heading marker by hand. For example, if the
  6085. cursor is after \"*****\", then the tree will be shifted to level 5.
  6086. If optional TREE is given, use this text instead of the kill ring.
  6087. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6088. move back over whitespace before inserting, and move point to the end of
  6089. the inserted text when done."
  6090. (interactive "P")
  6091. (setq tree (or tree (and kill-ring (current-kill 0))))
  6092. (unless (org-kill-is-subtree-p tree)
  6093. (error "%s"
  6094. (substitute-command-keys
  6095. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6096. (let* ((visp (not (org-invisible-p)))
  6097. (txt tree)
  6098. (^re (concat "^\\(" outline-regexp "\\)"))
  6099. (re (concat "\\(" outline-regexp "\\)"))
  6100. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6101. (old-level (if (string-match ^re txt)
  6102. (- (match-end 0) (match-beginning 0) 1)
  6103. -1))
  6104. (force-level (cond (level (prefix-numeric-value level))
  6105. ((and (looking-at "[ \t]*$")
  6106. (string-match
  6107. ^re_ (buffer-substring
  6108. (point-at-bol) (point))))
  6109. (- (match-end 1) (match-beginning 1)))
  6110. ((and (bolp)
  6111. (looking-at org-outline-regexp))
  6112. (- (match-end 0) (point) 1))
  6113. (t nil)))
  6114. (previous-level (save-excursion
  6115. (condition-case nil
  6116. (progn
  6117. (outline-previous-visible-heading 1)
  6118. (if (looking-at re)
  6119. (- (match-end 0) (match-beginning 0) 1)
  6120. 1))
  6121. (error 1))))
  6122. (next-level (save-excursion
  6123. (condition-case nil
  6124. (progn
  6125. (or (looking-at outline-regexp)
  6126. (outline-next-visible-heading 1))
  6127. (if (looking-at re)
  6128. (- (match-end 0) (match-beginning 0) 1)
  6129. 1))
  6130. (error 1))))
  6131. (new-level (or force-level (max previous-level next-level)))
  6132. (shift (if (or (= old-level -1)
  6133. (= new-level -1)
  6134. (= old-level new-level))
  6135. 0
  6136. (- new-level old-level)))
  6137. (delta (if (> shift 0) -1 1))
  6138. (func (if (> shift 0) 'org-demote 'org-promote))
  6139. (org-odd-levels-only nil)
  6140. beg end newend)
  6141. ;; Remove the forced level indicator
  6142. (if force-level
  6143. (delete-region (point-at-bol) (point)))
  6144. ;; Paste
  6145. (beginning-of-line 1)
  6146. (unless for-yank (org-back-over-empty-lines))
  6147. (setq beg (point))
  6148. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6149. (insert-before-markers txt)
  6150. (unless (string-match "\n\\'" txt) (insert "\n"))
  6151. (setq newend (point))
  6152. (org-reinstall-markers-in-region beg)
  6153. (setq end (point))
  6154. (goto-char beg)
  6155. (skip-chars-forward " \t\n\r")
  6156. (setq beg (point))
  6157. (if (and (org-invisible-p) visp)
  6158. (save-excursion (outline-show-heading)))
  6159. ;; Shift if necessary
  6160. (unless (= shift 0)
  6161. (save-restriction
  6162. (narrow-to-region beg end)
  6163. (while (not (= shift 0))
  6164. (org-map-region func (point-min) (point-max))
  6165. (setq shift (+ delta shift)))
  6166. (goto-char (point-min))
  6167. (setq newend (point-max))))
  6168. (when (or (interactive-p) for-yank)
  6169. (message "Clipboard pasted as level %d subtree" new-level))
  6170. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6171. kill-ring
  6172. (eq org-subtree-clip (current-kill 0))
  6173. org-subtree-clip-folded)
  6174. ;; The tree was folded before it was killed/copied
  6175. (hide-subtree))
  6176. (and for-yank (goto-char newend))))
  6177. (defun org-kill-is-subtree-p (&optional txt)
  6178. "Check if the current kill is an outline subtree, or a set of trees.
  6179. Returns nil if kill does not start with a headline, or if the first
  6180. headline level is not the largest headline level in the tree.
  6181. So this will actually accept several entries of equal levels as well,
  6182. which is OK for `org-paste-subtree'.
  6183. If optional TXT is given, check this string instead of the current kill."
  6184. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6185. (start-level (and kill
  6186. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6187. org-outline-regexp "\\)")
  6188. kill)
  6189. (- (match-end 2) (match-beginning 2) 1)))
  6190. (re (concat "^" org-outline-regexp))
  6191. (start (1+ (or (match-beginning 2) -1))))
  6192. (if (not start-level)
  6193. (progn
  6194. nil) ;; does not even start with a heading
  6195. (catch 'exit
  6196. (while (setq start (string-match re kill (1+ start)))
  6197. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6198. (throw 'exit nil)))
  6199. t))))
  6200. (defvar org-markers-to-move nil
  6201. "Markers that should be moved with a cut-and-paste operation.
  6202. Those markers are stored together with their positions relative to
  6203. the start of the region.")
  6204. (defun org-save-markers-in-region (beg end)
  6205. "Check markers in region.
  6206. If these markers are between BEG and END, record their position relative
  6207. to BEG, so that after moving the block of text, we can put the markers back
  6208. into place.
  6209. This function gets called just before an entry or tree gets cut from the
  6210. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6211. called immediately, to move the markers with the entries."
  6212. (setq org-markers-to-move nil)
  6213. (when (featurep 'org-clock)
  6214. (org-clock-save-markers-for-cut-and-paste beg end))
  6215. (when (featurep 'org-agenda)
  6216. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6217. (defun org-check-and-save-marker (marker beg end)
  6218. "Check if MARKER is between BEG and END.
  6219. If yes, remember the marker and the distance to BEG."
  6220. (when (and (marker-buffer marker)
  6221. (equal (marker-buffer marker) (current-buffer)))
  6222. (if (and (>= marker beg) (< marker end))
  6223. (push (cons marker (- marker beg)) org-markers-to-move))))
  6224. (defun org-reinstall-markers-in-region (beg)
  6225. "Move all remembered markers to their position relative to BEG."
  6226. (mapc (lambda (x)
  6227. (move-marker (car x) (+ beg (cdr x))))
  6228. org-markers-to-move)
  6229. (setq org-markers-to-move nil))
  6230. (defun org-narrow-to-subtree ()
  6231. "Narrow buffer to the current subtree."
  6232. (interactive)
  6233. (save-excursion
  6234. (save-match-data
  6235. (narrow-to-region
  6236. (progn (org-back-to-heading t) (point))
  6237. (progn (org-end-of-subtree t t)
  6238. (if (org-on-heading-p) (backward-char 1))
  6239. (point))))))
  6240. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6241. "Clone the task (subtree) at point N times.
  6242. The clones will be inserted as siblings.
  6243. In interactive use, the user will be prompted for the number of clones
  6244. to be produced, and for a time SHIFT, which may be a repeater as used
  6245. in time stamps, for example `+3d'.
  6246. When a valid repeater is given and the entry contains any time stamps,
  6247. the clones will become a sequence in time, with time stamps in the
  6248. subtree shifted for each clone produced. If SHIFT is nil or the
  6249. empty string, time stamps will be left alone.
  6250. If the original subtree did contain time stamps with a repeater,
  6251. the following will happen:
  6252. - the repeater will be removed in each clone
  6253. - an additional clone will be produced, with the current, unshifted
  6254. date(s) in the entry.
  6255. - the original entry will be placed *after* all the clones, with
  6256. repeater intact.
  6257. - the start days in the repeater in the original entry will be shifted
  6258. to past the last clone.
  6259. I this way you can spell out a number of instances of a repeating task,
  6260. and still retain the repeater to cover future instances of the task."
  6261. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6262. (let (beg end template task
  6263. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6264. (if (not (and (integerp n) (> n 0)))
  6265. (error "Invalid number of replications %s" n))
  6266. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6267. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6268. shift)))
  6269. (error "Invalid shift specification %s" shift))
  6270. (when doshift
  6271. (setq shift-n (string-to-number (match-string 1 shift))
  6272. shift-what (cdr (assoc (match-string 2 shift)
  6273. '(("d" . day) ("w" . week)
  6274. ("m" . month) ("y" . year))))))
  6275. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6276. (setq nmin 1 nmax n)
  6277. (org-back-to-heading t)
  6278. (setq beg (point))
  6279. (org-end-of-subtree t t)
  6280. (or (bolp) (insert "\n"))
  6281. (setq end (point))
  6282. (setq template (buffer-substring beg end))
  6283. (when (and doshift
  6284. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6285. (delete-region beg end)
  6286. (setq end beg)
  6287. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6288. (goto-char end)
  6289. (loop for n from nmin to nmax do
  6290. (if (not doshift)
  6291. (setq task template)
  6292. (with-temp-buffer
  6293. (insert template)
  6294. (org-mode)
  6295. (goto-char (point-min))
  6296. (while (re-search-forward org-ts-regexp-both nil t)
  6297. (org-timestamp-change (* n shift-n) shift-what))
  6298. (unless (= n n-no-remove)
  6299. (goto-char (point-min))
  6300. (while (re-search-forward org-ts-regexp nil t)
  6301. (save-excursion
  6302. (goto-char (match-beginning 0))
  6303. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6304. (delete-region (match-beginning 1) (match-end 1))))))
  6305. (setq task (buffer-string))))
  6306. (insert task))
  6307. (goto-char beg)))
  6308. ;;; Outline Sorting
  6309. (defun org-sort (with-case)
  6310. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6311. Optional argument WITH-CASE means sort case-sensitively.
  6312. With a double prefix argument, also remove duplicate entries."
  6313. (interactive "P")
  6314. (if (org-at-table-p)
  6315. (org-call-with-arg 'org-table-sort-lines with-case)
  6316. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6317. (defun org-sort-remove-invisible (s)
  6318. (remove-text-properties 0 (length s) org-rm-props s)
  6319. (while (string-match org-bracket-link-regexp s)
  6320. (setq s (replace-match (if (match-end 2)
  6321. (match-string 3 s)
  6322. (match-string 1 s)) t t s)))
  6323. s)
  6324. (defvar org-priority-regexp) ; defined later in the file
  6325. (defvar org-after-sorting-entries-or-items-hook nil
  6326. "Hook that is run after a bunch of entries or items have been sorted.
  6327. When children are sorted, the cursor is in the parent line when this
  6328. hook gets called. When a region or a plain list is sorted, the cursor
  6329. will be in the first entry of the sorted region/list.")
  6330. (defun org-sort-entries-or-items
  6331. (&optional with-case sorting-type getkey-func compare-func property)
  6332. "Sort entries on a certain level of an outline tree, or plain list items.
  6333. If there is an active region, the entries in the region are sorted.
  6334. Else, if the cursor is before the first entry, sort the top-level items.
  6335. Else, the children of the entry at point are sorted.
  6336. If the cursor is at the first item in a plain list, the list items will be
  6337. sorted.
  6338. Sorting can be alphabetically, numerically, by date/time as given by
  6339. a time stamp, by a property or by priority.
  6340. The command prompts for the sorting type unless it has been given to the
  6341. function through the SORTING-TYPE argument, which needs to a character,
  6342. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6343. precise meaning of each character:
  6344. n Numerically, by converting the beginning of the entry/item to a number.
  6345. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6346. t By date/time, either the first active time stamp in the entry, or, if
  6347. none exist, by the first inactive one.
  6348. In items, only the first line will be checked.
  6349. s By the scheduled date/time.
  6350. d By deadline date/time.
  6351. c By creation time, which is assumed to be the first inactive time stamp
  6352. at the beginning of a line.
  6353. p By priority according to the cookie.
  6354. r By the value of a property.
  6355. Capital letters will reverse the sort order.
  6356. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6357. called with point at the beginning of the record. It must return either
  6358. a string or a number that should serve as the sorting key for that record.
  6359. Comparing entries ignores case by default. However, with an optional argument
  6360. WITH-CASE, the sorting considers case as well."
  6361. (interactive "P")
  6362. (let ((case-func (if with-case 'identity 'downcase))
  6363. start beg end stars re re2
  6364. txt what tmp plain-list-p)
  6365. ;; Find beginning and end of region to sort
  6366. (cond
  6367. ((org-region-active-p)
  6368. ;; we will sort the region
  6369. (setq end (region-end)
  6370. what "region")
  6371. (goto-char (region-beginning))
  6372. (if (not (org-on-heading-p)) (outline-next-heading))
  6373. (setq start (point)))
  6374. ((org-at-item-p)
  6375. ;; we will sort this plain list
  6376. (org-beginning-of-item-list) (setq start (point))
  6377. (org-end-of-item-list)
  6378. (or (bolp) (insert "\n"))
  6379. (setq end (point))
  6380. (goto-char start)
  6381. (setq plain-list-p t
  6382. what "plain list"))
  6383. ((or (org-on-heading-p)
  6384. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6385. ;; we will sort the children of the current headline
  6386. (org-back-to-heading)
  6387. (setq start (point)
  6388. end (progn (org-end-of-subtree t t)
  6389. (or (bolp) (insert "\n"))
  6390. (org-back-over-empty-lines)
  6391. (point))
  6392. what "children")
  6393. (goto-char start)
  6394. (show-subtree)
  6395. (outline-next-heading))
  6396. (t
  6397. ;; we will sort the top-level entries in this file
  6398. (goto-char (point-min))
  6399. (or (org-on-heading-p) (outline-next-heading))
  6400. (setq start (point))
  6401. (goto-char (point-max))
  6402. (beginning-of-line 1)
  6403. (when (looking-at ".*?\\S-")
  6404. ;; File ends in a non-white line
  6405. (end-of-line 1)
  6406. (insert "\n"))
  6407. (setq end (point-max))
  6408. (setq what "top-level")
  6409. (goto-char start)
  6410. (show-all)))
  6411. (setq beg (point))
  6412. (if (>= beg end) (error "Nothing to sort"))
  6413. (unless plain-list-p
  6414. (looking-at "\\(\\*+\\)")
  6415. (setq stars (match-string 1)
  6416. re (concat "^" (regexp-quote stars) " +")
  6417. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6418. txt (buffer-substring beg end))
  6419. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6420. (if (and (not (equal stars "*")) (string-match re2 txt))
  6421. (error "Region to sort contains a level above the first entry")))
  6422. (unless sorting-type
  6423. (message
  6424. (if plain-list-p
  6425. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6426. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6427. [t]ime [s]cheduled [d]eadline [c]reated
  6428. A/N/T/S/D/C/P/O/F means reversed:")
  6429. what)
  6430. (setq sorting-type (read-char-exclusive))
  6431. (and (= (downcase sorting-type) ?f)
  6432. (setq getkey-func
  6433. (org-icompleting-read "Sort using function: "
  6434. obarray 'fboundp t nil nil))
  6435. (setq getkey-func (intern getkey-func)))
  6436. (and (= (downcase sorting-type) ?r)
  6437. (setq property
  6438. (org-icompleting-read "Property: "
  6439. (mapcar 'list (org-buffer-property-keys t))
  6440. nil t))))
  6441. (message "Sorting entries...")
  6442. (save-restriction
  6443. (narrow-to-region start end)
  6444. (let ((dcst (downcase sorting-type))
  6445. (case-fold-search nil)
  6446. (now (current-time)))
  6447. (sort-subr
  6448. (/= dcst sorting-type)
  6449. ;; This function moves to the beginning character of the "record" to
  6450. ;; be sorted.
  6451. (if plain-list-p
  6452. (lambda nil
  6453. (if (org-at-item-p) t (goto-char (point-max))))
  6454. (lambda nil
  6455. (if (re-search-forward re nil t)
  6456. (goto-char (match-beginning 0))
  6457. (goto-char (point-max)))))
  6458. ;; This function moves to the last character of the "record" being
  6459. ;; sorted.
  6460. (if plain-list-p
  6461. 'org-end-of-item
  6462. (lambda nil
  6463. (save-match-data
  6464. (condition-case nil
  6465. (outline-forward-same-level 1)
  6466. (error
  6467. (goto-char (point-max)))))))
  6468. ;; This function returns the value that gets sorted against.
  6469. (if plain-list-p
  6470. (lambda nil
  6471. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6472. (cond
  6473. ((= dcst ?n)
  6474. (string-to-number (buffer-substring (match-end 0)
  6475. (point-at-eol))))
  6476. ((= dcst ?a)
  6477. (buffer-substring (match-end 0) (point-at-eol)))
  6478. ((= dcst ?t)
  6479. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  6480. (re-search-forward org-ts-regexp-both
  6481. (point-at-eol) t))
  6482. (org-time-string-to-seconds (match-string 0))
  6483. (org-float-time now)))
  6484. ((= dcst ?f)
  6485. (if getkey-func
  6486. (progn
  6487. (setq tmp (funcall getkey-func))
  6488. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6489. tmp)
  6490. (error "Invalid key function `%s'" getkey-func)))
  6491. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6492. (lambda nil
  6493. (cond
  6494. ((= dcst ?n)
  6495. (if (looking-at org-complex-heading-regexp)
  6496. (string-to-number (match-string 4))
  6497. nil))
  6498. ((= dcst ?a)
  6499. (if (looking-at org-complex-heading-regexp)
  6500. (funcall case-func (match-string 4))
  6501. nil))
  6502. ((= dcst ?t)
  6503. (let ((end (save-excursion (outline-next-heading) (point))))
  6504. (if (or (re-search-forward org-ts-regexp end t)
  6505. (re-search-forward org-ts-regexp-both end t))
  6506. (org-time-string-to-seconds (match-string 0))
  6507. (org-float-time now))))
  6508. ((= dcst ?c)
  6509. (let ((end (save-excursion (outline-next-heading) (point))))
  6510. (if (re-search-forward
  6511. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6512. end t)
  6513. (org-time-string-to-seconds (match-string 0))
  6514. (org-float-time now))))
  6515. ((= dcst ?s)
  6516. (let ((end (save-excursion (outline-next-heading) (point))))
  6517. (if (re-search-forward org-scheduled-time-regexp end t)
  6518. (org-time-string-to-seconds (match-string 1))
  6519. (org-float-time now))))
  6520. ((= dcst ?d)
  6521. (let ((end (save-excursion (outline-next-heading) (point))))
  6522. (if (re-search-forward org-deadline-time-regexp end t)
  6523. (org-time-string-to-seconds (match-string 1))
  6524. (org-float-time now))))
  6525. ((= dcst ?p)
  6526. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6527. (string-to-char (match-string 2))
  6528. org-default-priority))
  6529. ((= dcst ?r)
  6530. (or (org-entry-get nil property) ""))
  6531. ((= dcst ?o)
  6532. (if (looking-at org-complex-heading-regexp)
  6533. (- 9999 (length (member (match-string 2)
  6534. org-todo-keywords-1)))))
  6535. ((= dcst ?f)
  6536. (if getkey-func
  6537. (progn
  6538. (setq tmp (funcall getkey-func))
  6539. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6540. tmp)
  6541. (error "Invalid key function `%s'" getkey-func)))
  6542. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6543. nil
  6544. (cond
  6545. ((= dcst ?a) 'string<)
  6546. ((= dcst ?f) compare-func)
  6547. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6548. (t nil)))))
  6549. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6550. (message "Sorting entries...done")))
  6551. (defun org-do-sort (table what &optional with-case sorting-type)
  6552. "Sort TABLE of WHAT according to SORTING-TYPE.
  6553. The user will be prompted for the SORTING-TYPE if the call to this
  6554. function does not specify it. WHAT is only for the prompt, to indicate
  6555. what is being sorted. The sorting key will be extracted from
  6556. the car of the elements of the table.
  6557. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6558. (unless sorting-type
  6559. (message
  6560. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6561. what)
  6562. (setq sorting-type (read-char-exclusive)))
  6563. (let ((dcst (downcase sorting-type))
  6564. extractfun comparefun)
  6565. ;; Define the appropriate functions
  6566. (cond
  6567. ((= dcst ?n)
  6568. (setq extractfun 'string-to-number
  6569. comparefun (if (= dcst sorting-type) '< '>)))
  6570. ((= dcst ?a)
  6571. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6572. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6573. comparefun (if (= dcst sorting-type)
  6574. 'string<
  6575. (lambda (a b) (and (not (string< a b))
  6576. (not (string= a b)))))))
  6577. ((= dcst ?t)
  6578. (setq extractfun
  6579. (lambda (x)
  6580. (if (or (string-match org-ts-regexp x)
  6581. (string-match org-ts-regexp-both x))
  6582. (org-float-time
  6583. (org-time-string-to-time (match-string 0 x)))
  6584. 0))
  6585. comparefun (if (= dcst sorting-type) '< '>)))
  6586. (t (error "Invalid sorting type `%c'" sorting-type)))
  6587. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6588. table)
  6589. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6590. ;;; The orgstruct minor mode
  6591. ;; Define a minor mode which can be used in other modes in order to
  6592. ;; integrate the org-mode structure editing commands.
  6593. ;; This is really a hack, because the org-mode structure commands use
  6594. ;; keys which normally belong to the major mode. Here is how it
  6595. ;; works: The minor mode defines all the keys necessary to operate the
  6596. ;; structure commands, but wraps the commands into a function which
  6597. ;; tests if the cursor is currently at a headline or a plain list
  6598. ;; item. If that is the case, the structure command is used,
  6599. ;; temporarily setting many Org-mode variables like regular
  6600. ;; expressions for filling etc. However, when any of those keys is
  6601. ;; used at a different location, function uses `key-binding' to look
  6602. ;; up if the key has an associated command in another currently active
  6603. ;; keymap (minor modes, major mode, global), and executes that
  6604. ;; command. There might be problems if any of the keys is otherwise
  6605. ;; used as a prefix key.
  6606. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6607. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6608. ;; addresses this by checking explicitly for both bindings.
  6609. (defvar orgstruct-mode-map (make-sparse-keymap)
  6610. "Keymap for the minor `orgstruct-mode'.")
  6611. (defvar org-local-vars nil
  6612. "List of local variables, for use by `orgstruct-mode'")
  6613. ;;;###autoload
  6614. (define-minor-mode orgstruct-mode
  6615. "Toggle the minor more `orgstruct-mode'.
  6616. This mode is for using Org-mode structure commands in other modes.
  6617. The following key behave as if Org-mode was active, if the cursor
  6618. is on a headline, or on a plain list item (both in the definition
  6619. of Org-mode).
  6620. M-up Move entry/item up
  6621. M-down Move entry/item down
  6622. M-left Promote
  6623. M-right Demote
  6624. M-S-up Move entry/item up
  6625. M-S-down Move entry/item down
  6626. M-S-left Promote subtree
  6627. M-S-right Demote subtree
  6628. M-q Fill paragraph and items like in Org-mode
  6629. C-c ^ Sort entries
  6630. C-c - Cycle list bullet
  6631. TAB Cycle item visibility
  6632. M-RET Insert new heading/item
  6633. S-M-RET Insert new TODO heading / Checkbox item
  6634. C-c C-c Set tags / toggle checkbox"
  6635. nil " OrgStruct" nil
  6636. (org-load-modules-maybe)
  6637. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6638. ;;;###autoload
  6639. (defun turn-on-orgstruct ()
  6640. "Unconditionally turn on `orgstruct-mode'."
  6641. (orgstruct-mode 1))
  6642. (defun orgstruct++-mode (&optional arg)
  6643. "Toggle `orgstruct-mode', the enhanced version of it.
  6644. In addition to setting orgstruct-mode, this also exports all indentation
  6645. and autofilling variables from org-mode into the buffer. It will also
  6646. recognize item context in multiline items.
  6647. Note that turning off orgstruct-mode will *not* remove the
  6648. indentation/paragraph settings. This can only be done by refreshing the
  6649. major mode, for example with \\[normal-mode]."
  6650. (interactive "P")
  6651. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6652. (if (< arg 1)
  6653. (orgstruct-mode -1)
  6654. (orgstruct-mode 1)
  6655. (let (var val)
  6656. (mapc
  6657. (lambda (x)
  6658. (when (string-match
  6659. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6660. (symbol-name (car x)))
  6661. (setq var (car x) val (nth 1 x))
  6662. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6663. org-local-vars)
  6664. (org-set-local 'orgstruct-is-++ t))))
  6665. (defvar orgstruct-is-++ nil
  6666. "Is orgstruct-mode in ++ version in the current-buffer?")
  6667. (make-variable-buffer-local 'orgstruct-is-++)
  6668. ;;;###autoload
  6669. (defun turn-on-orgstruct++ ()
  6670. "Unconditionally turn on `orgstruct++-mode'."
  6671. (orgstruct++-mode 1))
  6672. (defun orgstruct-error ()
  6673. "Error when there is no default binding for a structure key."
  6674. (interactive)
  6675. (error "This key has no function outside structure elements"))
  6676. (defun orgstruct-setup ()
  6677. "Setup orgstruct keymaps."
  6678. (let ((nfunc 0)
  6679. (bindings
  6680. (list
  6681. '([(meta up)] org-metaup)
  6682. '([(meta down)] org-metadown)
  6683. '([(meta left)] org-metaleft)
  6684. '([(meta right)] org-metaright)
  6685. '([(meta shift up)] org-shiftmetaup)
  6686. '([(meta shift down)] org-shiftmetadown)
  6687. '([(meta shift left)] org-shiftmetaleft)
  6688. '([(meta shift right)] org-shiftmetaright)
  6689. '([?\e (up)] org-metaup)
  6690. '([?\e (down)] org-metadown)
  6691. '([?\e (left)] org-metaleft)
  6692. '([?\e (right)] org-metaright)
  6693. '([?\e (shift up)] org-shiftmetaup)
  6694. '([?\e (shift down)] org-shiftmetadown)
  6695. '([?\e (shift left)] org-shiftmetaleft)
  6696. '([?\e (shift right)] org-shiftmetaright)
  6697. '([(shift up)] org-shiftup)
  6698. '([(shift down)] org-shiftdown)
  6699. '([(shift left)] org-shiftleft)
  6700. '([(shift right)] org-shiftright)
  6701. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6702. '("\M-q" fill-paragraph)
  6703. '("\C-c^" org-sort)
  6704. '("\C-c-" org-cycle-list-bullet)))
  6705. elt key fun cmd)
  6706. (while (setq elt (pop bindings))
  6707. (setq nfunc (1+ nfunc))
  6708. (setq key (org-key (car elt))
  6709. fun (nth 1 elt)
  6710. cmd (orgstruct-make-binding fun nfunc key))
  6711. (org-defkey orgstruct-mode-map key cmd))
  6712. ;; Special treatment needed for TAB and RET
  6713. (org-defkey orgstruct-mode-map [(tab)]
  6714. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6715. (org-defkey orgstruct-mode-map "\C-i"
  6716. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6717. (org-defkey orgstruct-mode-map "\M-\C-m"
  6718. (orgstruct-make-binding 'org-insert-heading 105
  6719. "\M-\C-m" [(meta return)]))
  6720. (org-defkey orgstruct-mode-map [(meta return)]
  6721. (orgstruct-make-binding 'org-insert-heading 106
  6722. [(meta return)] "\M-\C-m"))
  6723. (org-defkey orgstruct-mode-map [(shift meta return)]
  6724. (orgstruct-make-binding 'org-insert-todo-heading 107
  6725. [(meta return)] "\M-\C-m"))
  6726. (org-defkey orgstruct-mode-map "\e\C-m"
  6727. (orgstruct-make-binding 'org-insert-heading 108
  6728. "\e\C-m" [?\e (return)]))
  6729. (org-defkey orgstruct-mode-map [?\e (return)]
  6730. (orgstruct-make-binding 'org-insert-heading 109
  6731. [?\e (return)] "\e\C-m"))
  6732. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6733. (orgstruct-make-binding 'org-insert-todo-heading 110
  6734. [?\e (return)] "\e\C-m"))
  6735. (unless org-local-vars
  6736. (setq org-local-vars (org-get-local-variables)))
  6737. t))
  6738. (defun orgstruct-make-binding (fun n &rest keys)
  6739. "Create a function for binding in the structure minor mode.
  6740. FUN is the command to call inside a table. N is used to create a unique
  6741. command name. KEYS are keys that should be checked in for a command
  6742. to execute outside of tables."
  6743. (eval
  6744. (list 'defun
  6745. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6746. '(arg)
  6747. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6748. "Outside of structure, run the binding of `"
  6749. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6750. "'.")
  6751. '(interactive "p")
  6752. (list 'if
  6753. `(org-context-p 'headline 'item
  6754. (and orgstruct-is-++
  6755. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6756. 'item-body))
  6757. (list 'org-run-like-in-org-mode (list 'quote fun))
  6758. (list 'let '(orgstruct-mode)
  6759. (list 'call-interactively
  6760. (append '(or)
  6761. (mapcar (lambda (k)
  6762. (list 'key-binding k))
  6763. keys)
  6764. '('orgstruct-error))))))))
  6765. (defun org-context-p (&rest contexts)
  6766. "Check if local context is any of CONTEXTS.
  6767. Possible values in the list of contexts are `table', `headline', and `item'."
  6768. (let ((pos (point)))
  6769. (goto-char (point-at-bol))
  6770. (prog1 (or (and (memq 'table contexts)
  6771. (looking-at "[ \t]*|"))
  6772. (and (memq 'headline contexts)
  6773. ;;????????? (looking-at "\\*+"))
  6774. (looking-at outline-regexp))
  6775. (and (memq 'item contexts)
  6776. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6777. (and (memq 'item-body contexts)
  6778. (org-in-item-p)))
  6779. (goto-char pos))))
  6780. (defun org-get-local-variables ()
  6781. "Return a list of all local variables in an org-mode buffer."
  6782. (let (varlist)
  6783. (with-current-buffer (get-buffer-create "*Org tmp*")
  6784. (erase-buffer)
  6785. (org-mode)
  6786. (setq varlist (buffer-local-variables)))
  6787. (kill-buffer "*Org tmp*")
  6788. (delq nil
  6789. (mapcar
  6790. (lambda (x)
  6791. (setq x
  6792. (if (symbolp x)
  6793. (list x)
  6794. (list (car x) (list 'quote (cdr x)))))
  6795. (if (string-match
  6796. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6797. (symbol-name (car x)))
  6798. x nil))
  6799. varlist))))
  6800. ;;;###autoload
  6801. (defun org-run-like-in-org-mode (cmd)
  6802. "Run a command, pretending that the current buffer is in Org-mode.
  6803. This will temporarily bind local variables that are typically bound in
  6804. Org-mode to the values they have in Org-mode, and then interactively
  6805. call CMD."
  6806. (org-load-modules-maybe)
  6807. (unless org-local-vars
  6808. (setq org-local-vars (org-get-local-variables)))
  6809. (eval (list 'let org-local-vars
  6810. (list 'call-interactively (list 'quote cmd)))))
  6811. ;;;; Archiving
  6812. (defun org-get-category (&optional pos)
  6813. "Get the category applying to position POS."
  6814. (get-text-property (or pos (point)) 'org-category))
  6815. (defun org-refresh-category-properties ()
  6816. "Refresh category text properties in the buffer."
  6817. (let ((def-cat (cond
  6818. ((null org-category)
  6819. (if buffer-file-name
  6820. (file-name-sans-extension
  6821. (file-name-nondirectory buffer-file-name))
  6822. "???"))
  6823. ((symbolp org-category) (symbol-name org-category))
  6824. (t org-category)))
  6825. beg end cat pos optionp)
  6826. (org-unmodified
  6827. (save-excursion
  6828. (save-restriction
  6829. (widen)
  6830. (goto-char (point-min))
  6831. (put-text-property (point) (point-max) 'org-category def-cat)
  6832. (while (re-search-forward
  6833. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6834. (setq pos (match-end 0)
  6835. optionp (equal (char-after (match-beginning 0)) ?#)
  6836. cat (org-trim (match-string 2)))
  6837. (if optionp
  6838. (setq beg (point-at-bol) end (point-max))
  6839. (org-back-to-heading t)
  6840. (setq beg (point) end (org-end-of-subtree t t)))
  6841. (put-text-property beg end 'org-category cat)
  6842. (goto-char pos)))))))
  6843. ;;;; Link Stuff
  6844. ;;; Link abbreviations
  6845. (defun org-link-expand-abbrev (link)
  6846. "Apply replacements as defined in `org-link-abbrev-alist."
  6847. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6848. (let* ((key (match-string 1 link))
  6849. (as (or (assoc key org-link-abbrev-alist-local)
  6850. (assoc key org-link-abbrev-alist)))
  6851. (tag (and (match-end 2) (match-string 3 link)))
  6852. rpl)
  6853. (if (not as)
  6854. link
  6855. (setq rpl (cdr as))
  6856. (cond
  6857. ((symbolp rpl) (funcall rpl tag))
  6858. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6859. ((string-match "%h" rpl)
  6860. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6861. (t (concat rpl tag)))))
  6862. link))
  6863. ;;; Storing and inserting links
  6864. (defvar org-insert-link-history nil
  6865. "Minibuffer history for links inserted with `org-insert-link'.")
  6866. (defvar org-stored-links nil
  6867. "Contains the links stored with `org-store-link'.")
  6868. (defvar org-store-link-plist nil
  6869. "Plist with info about the most recently link created with `org-store-link'.")
  6870. (defvar org-link-protocols nil
  6871. "Link protocols added to Org-mode using `org-add-link-type'.")
  6872. (defvar org-store-link-functions nil
  6873. "List of functions that are called to create and store a link.
  6874. Each function will be called in turn until one returns a non-nil
  6875. value. Each function should check if it is responsible for creating
  6876. this link (for example by looking at the major mode).
  6877. If not, it must exit and return nil.
  6878. If yes, it should return a non-nil value after a calling
  6879. `org-store-link-props' with a list of properties and values.
  6880. Special properties are:
  6881. :type The link prefix. like \"http\". This must be given.
  6882. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6883. This is obligatory as well.
  6884. :description Optional default description for the second pair
  6885. of brackets in an Org-mode link. The user can still change
  6886. this when inserting this link into an Org-mode buffer.
  6887. In addition to these, any additional properties can be specified
  6888. and then used in remember templates.")
  6889. (defun org-add-link-type (type &optional follow export)
  6890. "Add TYPE to the list of `org-link-types'.
  6891. Re-compute all regular expressions depending on `org-link-types'
  6892. FOLLOW and EXPORT are two functions.
  6893. FOLLOW should take the link path as the single argument and do whatever
  6894. is necessary to follow the link, for example find a file or display
  6895. a mail message.
  6896. EXPORT should format the link path for export to one of the export formats.
  6897. It should be a function accepting three arguments:
  6898. path the path of the link, the text after the prefix (like \"http:\")
  6899. desc the description of the link, if any, nil if there was no description
  6900. format the export format, a symbol like `html' or `latex'.
  6901. The function may use the FORMAT information to return different values
  6902. depending on the format. The return value will be put literally into
  6903. the exported file.
  6904. Org-mode has a built-in default for exporting links. If you are happy with
  6905. this default, there is no need to define an export function for the link
  6906. type. For a simple example of an export function, see `org-bbdb.el'."
  6907. (add-to-list 'org-link-types type t)
  6908. (org-make-link-regexps)
  6909. (if (assoc type org-link-protocols)
  6910. (setcdr (assoc type org-link-protocols) (list follow export))
  6911. (push (list type follow export) org-link-protocols)))
  6912. (defvar org-agenda-buffer-name)
  6913. ;;;###autoload
  6914. (defun org-store-link (arg)
  6915. "\\<org-mode-map>Store an org-link to the current location.
  6916. This link is added to `org-stored-links' and can later be inserted
  6917. into an org-buffer with \\[org-insert-link].
  6918. For some link types, a prefix arg is interpreted:
  6919. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6920. For file links, arg negates `org-context-in-file-links'."
  6921. (interactive "P")
  6922. (org-load-modules-maybe)
  6923. (setq org-store-link-plist nil) ; reset
  6924. (let ((outline-regexp (org-get-limited-outline-regexp))
  6925. link cpltxt desc description search txt custom-id)
  6926. (cond
  6927. ((run-hook-with-args-until-success 'org-store-link-functions)
  6928. (setq link (plist-get org-store-link-plist :link)
  6929. desc (or (plist-get org-store-link-plist :description) link)))
  6930. ((equal (buffer-name) "*Org Edit Src Example*")
  6931. (let (label gc)
  6932. (while (or (not label)
  6933. (save-excursion
  6934. (save-restriction
  6935. (widen)
  6936. (goto-char (point-min))
  6937. (re-search-forward
  6938. (regexp-quote (format org-coderef-label-format label))
  6939. nil t))))
  6940. (when label (message "Label exists already") (sit-for 2))
  6941. (setq label (read-string "Code line label: " label)))
  6942. (end-of-line 1)
  6943. (setq link (format org-coderef-label-format label))
  6944. (setq gc (- 79 (length link)))
  6945. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6946. (insert link)
  6947. (setq link (concat "(" label ")") desc nil)))
  6948. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  6949. ;; We are in the agenda, link to referenced location
  6950. (let ((m (or (get-text-property (point) 'org-hd-marker)
  6951. (get-text-property (point) 'org-marker))))
  6952. (when m
  6953. (org-with-point-at m
  6954. (call-interactively 'org-store-link)))))
  6955. ((eq major-mode 'calendar-mode)
  6956. (let ((cd (calendar-cursor-to-date)))
  6957. (setq link
  6958. (format-time-string
  6959. (car org-time-stamp-formats)
  6960. (apply 'encode-time
  6961. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6962. nil nil nil))))
  6963. (org-store-link-props :type "calendar" :date cd)))
  6964. ((eq major-mode 'w3-mode)
  6965. (setq cpltxt (if (and (buffer-name)
  6966. (not (string-match "Untitled" (buffer-name))))
  6967. (buffer-name)
  6968. (url-view-url t))
  6969. link (org-make-link (url-view-url t)))
  6970. (org-store-link-props :type "w3" :url (url-view-url t)))
  6971. ((eq major-mode 'w3m-mode)
  6972. (setq cpltxt (or w3m-current-title w3m-current-url)
  6973. link (org-make-link w3m-current-url))
  6974. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6975. ((setq search (run-hook-with-args-until-success
  6976. 'org-create-file-search-functions))
  6977. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6978. "::" search))
  6979. (setq cpltxt (or description link)))
  6980. ((eq major-mode 'image-mode)
  6981. (setq cpltxt (concat "file:"
  6982. (abbreviate-file-name buffer-file-name))
  6983. link (org-make-link cpltxt))
  6984. (org-store-link-props :type "image" :file buffer-file-name))
  6985. ((eq major-mode 'dired-mode)
  6986. ;; link to the file in the current line
  6987. (let ((file (dired-get-filename nil t)))
  6988. (setq file (if file
  6989. (abbreviate-file-name
  6990. (expand-file-name (dired-get-filename nil t)))
  6991. ;; otherwise, no file so use current directory.
  6992. default-directory))
  6993. (setq cpltxt (concat "file:" file)
  6994. link (org-make-link cpltxt))))
  6995. ((and buffer-file-name (org-mode-p))
  6996. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6997. (cond
  6998. ((org-in-regexp "<<\\(.*?\\)>>")
  6999. (setq cpltxt
  7000. (concat "file:"
  7001. (abbreviate-file-name buffer-file-name)
  7002. "::" (match-string 1))
  7003. link (org-make-link cpltxt)))
  7004. ((and (featurep 'org-id)
  7005. (or (eq org-link-to-org-use-id t)
  7006. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7007. (interactive-p))
  7008. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  7009. (interactive-p)
  7010. (not custom-id))
  7011. (and org-link-to-org-use-id
  7012. (condition-case nil
  7013. (org-entry-get nil "ID")
  7014. (error nil)))))
  7015. ;; We can make a link using the ID.
  7016. (setq link (condition-case nil
  7017. (prog1 (org-id-store-link)
  7018. (setq desc (plist-get org-store-link-plist
  7019. :description)))
  7020. (error
  7021. ;; probably before first headline, link to file only
  7022. (concat "file:"
  7023. (abbreviate-file-name buffer-file-name))))))
  7024. (t
  7025. ;; Just link to current headline
  7026. (setq cpltxt (concat "file:"
  7027. (abbreviate-file-name buffer-file-name)))
  7028. ;; Add a context search string
  7029. (when (org-xor org-context-in-file-links arg)
  7030. (setq txt (cond
  7031. ((org-on-heading-p) nil)
  7032. ((org-region-active-p)
  7033. (buffer-substring (region-beginning) (region-end)))
  7034. (t nil)))
  7035. (when (or (null txt) (string-match "\\S-" txt))
  7036. (setq cpltxt
  7037. (concat cpltxt "::"
  7038. (condition-case nil
  7039. (org-make-org-heading-search-string txt)
  7040. (error "")))
  7041. desc (or (nth 4 (ignore-errors
  7042. (org-heading-components))) "NONE"))))
  7043. (if (string-match "::\\'" cpltxt)
  7044. (setq cpltxt (substring cpltxt 0 -2)))
  7045. (setq link (org-make-link cpltxt)))))
  7046. ((buffer-file-name (buffer-base-buffer))
  7047. ;; Just link to this file here.
  7048. (setq cpltxt (concat "file:"
  7049. (abbreviate-file-name
  7050. (buffer-file-name (buffer-base-buffer)))))
  7051. ;; Add a context string
  7052. (when (org-xor org-context-in-file-links arg)
  7053. (setq txt (if (org-region-active-p)
  7054. (buffer-substring (region-beginning) (region-end))
  7055. (buffer-substring (point-at-bol) (point-at-eol))))
  7056. ;; Only use search option if there is some text.
  7057. (when (string-match "\\S-" txt)
  7058. (setq cpltxt
  7059. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7060. desc "NONE")))
  7061. (setq link (org-make-link cpltxt)))
  7062. ((interactive-p)
  7063. (error "Cannot link to a buffer which is not visiting a file"))
  7064. (t (setq link nil)))
  7065. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7066. (setq link (or link cpltxt)
  7067. desc (or desc cpltxt))
  7068. (if (equal desc "NONE") (setq desc nil))
  7069. (if (and (or (interactive-p) executing-kbd-macro) link)
  7070. (progn
  7071. (setq org-stored-links
  7072. (cons (list link desc) org-stored-links))
  7073. (message "Stored: %s" (or desc link))
  7074. (when custom-id
  7075. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7076. "::#" custom-id))
  7077. (setq org-stored-links
  7078. (cons (list link desc) org-stored-links))))
  7079. (and link (org-make-link-string link desc)))))
  7080. (defun org-store-link-props (&rest plist)
  7081. "Store link properties, extract names and addresses."
  7082. (let (x adr)
  7083. (when (setq x (plist-get plist :from))
  7084. (setq adr (mail-extract-address-components x))
  7085. (setq plist (plist-put plist :fromname (car adr)))
  7086. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7087. (when (setq x (plist-get plist :to))
  7088. (setq adr (mail-extract-address-components x))
  7089. (setq plist (plist-put plist :toname (car adr)))
  7090. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7091. (let ((from (plist-get plist :from))
  7092. (to (plist-get plist :to)))
  7093. (when (and from to org-from-is-user-regexp)
  7094. (setq plist
  7095. (plist-put plist :fromto
  7096. (if (string-match org-from-is-user-regexp from)
  7097. (concat "to %t")
  7098. (concat "from %f"))))))
  7099. (setq org-store-link-plist plist))
  7100. (defun org-add-link-props (&rest plist)
  7101. "Add these properties to the link property list."
  7102. (let (key value)
  7103. (while plist
  7104. (setq key (pop plist) value (pop plist))
  7105. (setq org-store-link-plist
  7106. (plist-put org-store-link-plist key value)))))
  7107. (defun org-email-link-description (&optional fmt)
  7108. "Return the description part of an email link.
  7109. This takes information from `org-store-link-plist' and formats it
  7110. according to FMT (default from `org-email-link-description-format')."
  7111. (setq fmt (or fmt org-email-link-description-format))
  7112. (let* ((p org-store-link-plist)
  7113. (to (plist-get p :toaddress))
  7114. (from (plist-get p :fromaddress))
  7115. (table
  7116. (list
  7117. (cons "%c" (plist-get p :fromto))
  7118. (cons "%F" (plist-get p :from))
  7119. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7120. (cons "%T" (plist-get p :to))
  7121. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7122. (cons "%s" (plist-get p :subject))
  7123. (cons "%m" (plist-get p :message-id)))))
  7124. (when (string-match "%c" fmt)
  7125. ;; Check if the user wrote this message
  7126. (if (and org-from-is-user-regexp from to
  7127. (save-match-data (string-match org-from-is-user-regexp from)))
  7128. (setq fmt (replace-match "to %t" t t fmt))
  7129. (setq fmt (replace-match "from %f" t t fmt))))
  7130. (org-replace-escapes fmt table)))
  7131. (defun org-make-org-heading-search-string (&optional string heading)
  7132. "Make search string for STRING or current headline."
  7133. (interactive)
  7134. (let ((s (or string (org-get-heading))))
  7135. (unless (and string (not heading))
  7136. ;; We are using a headline, clean up garbage in there.
  7137. (if (string-match org-todo-regexp s)
  7138. (setq s (replace-match "" t t s)))
  7139. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  7140. (setq s (replace-match "" t t s)))
  7141. (setq s (org-trim s))
  7142. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7143. org-comment-string "\\)") s)
  7144. (setq s (replace-match "" t t s)))
  7145. (while (string-match org-ts-regexp s)
  7146. (setq s (replace-match "" t t s))))
  7147. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  7148. (setq s (replace-match " " t t s)))
  7149. (or string (setq s (concat "*" s))) ; Add * for headlines
  7150. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7151. (defun org-make-link (&rest strings)
  7152. "Concatenate STRINGS."
  7153. (apply 'concat strings))
  7154. (defun org-make-link-string (link &optional description)
  7155. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7156. (unless (string-match "\\S-" link)
  7157. (error "Empty link"))
  7158. (when (and description
  7159. (stringp description)
  7160. (not (string-match "\\S-" description)))
  7161. (setq description nil))
  7162. (when (stringp description)
  7163. ;; Remove brackets from the description, they are fatal.
  7164. (while (string-match "\\[" description)
  7165. (setq description (replace-match "{" t t description)))
  7166. (while (string-match "\\]" description)
  7167. (setq description (replace-match "}" t t description))))
  7168. (when (equal (org-link-escape link) description)
  7169. ;; No description needed, it is identical
  7170. (setq description nil))
  7171. (when (and (not description)
  7172. (not (equal link (org-link-escape link))))
  7173. (setq description (org-extract-attributes link)))
  7174. (concat "[[" (org-link-escape link) "]"
  7175. (if description (concat "[" description "]") "")
  7176. "]"))
  7177. (defconst org-link-escape-chars
  7178. '((?\ . "%20")
  7179. (?\[ . "%5B")
  7180. (?\] . "%5D")
  7181. (?\340 . "%E0") ; `a
  7182. (?\342 . "%E2") ; ^a
  7183. (?\347 . "%E7") ; ,c
  7184. (?\350 . "%E8") ; `e
  7185. (?\351 . "%E9") ; 'e
  7186. (?\352 . "%EA") ; ^e
  7187. (?\356 . "%EE") ; ^i
  7188. (?\364 . "%F4") ; ^o
  7189. (?\371 . "%F9") ; `u
  7190. (?\373 . "%FB") ; ^u
  7191. (?\; . "%3B")
  7192. ;; (?? . "%3F")
  7193. (?= . "%3D")
  7194. (?+ . "%2B")
  7195. )
  7196. "Association list of escapes for some characters problematic in links.
  7197. This is the list that is used for internal purposes.")
  7198. (defvar org-url-encoding-use-url-hexify nil)
  7199. (defconst org-link-escape-chars-browser
  7200. '((?\ . "%20")) ; 32 for the SPC char
  7201. "Association list of escapes for some characters problematic in links.
  7202. This is the list that is used before handing over to the browser.")
  7203. (defun org-link-escape (text &optional table)
  7204. "Escape characters in TEXT that are problematic for links."
  7205. (if (and org-url-encoding-use-url-hexify (not table))
  7206. (url-hexify-string text)
  7207. (setq table (or table org-link-escape-chars))
  7208. (when text
  7209. (let ((re (mapconcat (lambda (x) (regexp-quote
  7210. (char-to-string (car x))))
  7211. table "\\|")))
  7212. (while (string-match re text)
  7213. (setq text
  7214. (replace-match
  7215. (cdr (assoc (string-to-char (match-string 0 text))
  7216. table))
  7217. t t text)))
  7218. text))))
  7219. (defun org-link-unescape (text &optional table)
  7220. "Reverse the action of `org-link-escape'."
  7221. (if (and org-url-encoding-use-url-hexify (not table))
  7222. (url-unhex-string text)
  7223. (setq table (or table org-link-escape-chars))
  7224. (when text
  7225. (let ((case-fold-search t)
  7226. (re (mapconcat (lambda (x) (regexp-quote (downcase (cdr x))))
  7227. table "\\|")))
  7228. (while (string-match re text)
  7229. (setq text
  7230. (replace-match
  7231. (char-to-string (car (rassoc (upcase (match-string 0 text))
  7232. table)))
  7233. t t text)))
  7234. text))))
  7235. (defun org-xor (a b)
  7236. "Exclusive or."
  7237. (if a (not b) b))
  7238. (defun org-fixup-message-id-for-http (s)
  7239. "Replace special characters in a message id, so it can be used in an http query."
  7240. (while (string-match "<" s)
  7241. (setq s (replace-match "%3C" t t s)))
  7242. (while (string-match ">" s)
  7243. (setq s (replace-match "%3E" t t s)))
  7244. (while (string-match "@" s)
  7245. (setq s (replace-match "%40" t t s)))
  7246. s)
  7247. ;;;###autoload
  7248. (defun org-insert-link-global ()
  7249. "Insert a link like Org-mode does.
  7250. This command can be called in any mode to insert a link in Org-mode syntax."
  7251. (interactive)
  7252. (org-load-modules-maybe)
  7253. (org-run-like-in-org-mode 'org-insert-link))
  7254. (defun org-insert-link (&optional complete-file link-location)
  7255. "Insert a link. At the prompt, enter the link.
  7256. Completion can be used to insert any of the link protocol prefixes like
  7257. http or ftp in use.
  7258. The history can be used to select a link previously stored with
  7259. `org-store-link'. When the empty string is entered (i.e. if you just
  7260. press RET at the prompt), the link defaults to the most recently
  7261. stored link. As SPC triggers completion in the minibuffer, you need to
  7262. use M-SPC or C-q SPC to force the insertion of a space character.
  7263. You will also be prompted for a description, and if one is given, it will
  7264. be displayed in the buffer instead of the link.
  7265. If there is already a link at point, this command will allow you to edit link
  7266. and description parts.
  7267. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7268. be selected using completion. The path to the file will be relative to the
  7269. current directory if the file is in the current directory or a subdirectory.
  7270. Otherwise, the link will be the absolute path as completed in the minibuffer
  7271. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7272. option `org-link-file-path-type'.
  7273. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7274. the current directory or below.
  7275. With three \\[universal-argument] prefixes, negate the meaning of
  7276. `org-keep-stored-link-after-insertion'.
  7277. If `org-make-link-description-function' is non-nil, this function will be
  7278. called with the link target, and the result will be the default
  7279. link description.
  7280. If the LINK-LOCATION parameter is non-nil, this value will be
  7281. used as the link location instead of reading one interactively."
  7282. (interactive "P")
  7283. (let* ((wcf (current-window-configuration))
  7284. (region (if (org-region-active-p)
  7285. (buffer-substring (region-beginning) (region-end))))
  7286. (remove (and region (list (region-beginning) (region-end))))
  7287. (desc region)
  7288. tmphist ; byte-compile incorrectly complains about this
  7289. (link link-location)
  7290. entry file all-prefixes)
  7291. (cond
  7292. (link-location) ; specified by arg, just use it.
  7293. ((org-in-regexp org-bracket-link-regexp 1)
  7294. ;; We do have a link at point, and we are going to edit it.
  7295. (setq remove (list (match-beginning 0) (match-end 0)))
  7296. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7297. (setq link (read-string "Link: "
  7298. (org-link-unescape
  7299. (org-match-string-no-properties 1)))))
  7300. ((or (org-in-regexp org-angle-link-re)
  7301. (org-in-regexp org-plain-link-re))
  7302. ;; Convert to bracket link
  7303. (setq remove (list (match-beginning 0) (match-end 0))
  7304. link (read-string "Link: "
  7305. (org-remove-angle-brackets (match-string 0)))))
  7306. ((member complete-file '((4) (16)))
  7307. ;; Completing read for file names.
  7308. (setq link (org-file-complete-link complete-file)))
  7309. (t
  7310. ;; Read link, with completion for stored links.
  7311. (with-output-to-temp-buffer "*Org Links*"
  7312. (princ "Insert a link.
  7313. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7314. (when org-stored-links
  7315. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7316. (princ (mapconcat
  7317. (lambda (x)
  7318. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7319. (reverse org-stored-links) "\n"))))
  7320. (let ((cw (selected-window)))
  7321. (select-window (get-buffer-window "*Org Links*"))
  7322. (setq truncate-lines t)
  7323. (unless (pos-visible-in-window-p (point-max))
  7324. (org-fit-window-to-buffer))
  7325. (and (window-live-p cw) (select-window cw)))
  7326. ;; Fake a link history, containing the stored links.
  7327. (setq tmphist (append (mapcar 'car org-stored-links)
  7328. org-insert-link-history))
  7329. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7330. (mapcar 'car org-link-abbrev-alist)
  7331. org-link-types))
  7332. (unwind-protect
  7333. (progn
  7334. (setq link
  7335. (let ((org-completion-use-ido nil)
  7336. (org-completion-use-iswitchb nil))
  7337. (org-completing-read
  7338. "Link: "
  7339. (append
  7340. (mapcar (lambda (x) (list (concat x ":")))
  7341. all-prefixes)
  7342. (mapcar 'car org-stored-links))
  7343. nil nil nil
  7344. 'tmphist
  7345. (car (car org-stored-links)))))
  7346. (if (not (string-match "\\S-" link))
  7347. (error "No link selected"))
  7348. (if (or (member link all-prefixes)
  7349. (and (equal ":" (substring link -1))
  7350. (member (substring link 0 -1) all-prefixes)
  7351. (setq link (substring link 0 -1))))
  7352. (setq link (org-link-try-special-completion link))))
  7353. (set-window-configuration wcf)
  7354. (kill-buffer "*Org Links*"))
  7355. (setq entry (assoc link org-stored-links))
  7356. (or entry (push link org-insert-link-history))
  7357. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7358. (not org-keep-stored-link-after-insertion))
  7359. (setq org-stored-links (delq (assoc link org-stored-links)
  7360. org-stored-links)))
  7361. (setq desc (or desc (nth 1 entry)))))
  7362. (if (string-match org-plain-link-re link)
  7363. ;; URL-like link, normalize the use of angular brackets.
  7364. (setq link (org-make-link (org-remove-angle-brackets link))))
  7365. ;; Check if we are linking to the current file with a search option
  7366. ;; If yes, simplify the link by using only the search option.
  7367. (when (and buffer-file-name
  7368. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7369. (let* ((path (match-string 1 link))
  7370. (case-fold-search nil)
  7371. (search (match-string 2 link)))
  7372. (save-match-data
  7373. (if (equal (file-truename buffer-file-name) (file-truename path))
  7374. ;; We are linking to this same file, with a search option
  7375. (setq link search)))))
  7376. ;; Check if we can/should use a relative path. If yes, simplify the link
  7377. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  7378. (let* ((type (match-string 1 link))
  7379. (path (match-string 2 link))
  7380. (origpath path)
  7381. (case-fold-search nil))
  7382. (cond
  7383. ((or (eq org-link-file-path-type 'absolute)
  7384. (equal complete-file '(16)))
  7385. (setq path (abbreviate-file-name (expand-file-name path))))
  7386. ((eq org-link-file-path-type 'noabbrev)
  7387. (setq path (expand-file-name path)))
  7388. ((eq org-link-file-path-type 'relative)
  7389. (setq path (file-relative-name path)))
  7390. (t
  7391. (save-match-data
  7392. (if (string-match (concat "^" (regexp-quote
  7393. (file-name-as-directory
  7394. (expand-file-name "."))))
  7395. (expand-file-name path))
  7396. ;; We are linking a file with relative path name.
  7397. (setq path (substring (expand-file-name path)
  7398. (match-end 0)))
  7399. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7400. (setq link (concat type path))
  7401. (if (equal desc origpath)
  7402. (setq desc path))))
  7403. (if org-make-link-description-function
  7404. (setq desc (funcall org-make-link-description-function link desc)))
  7405. (setq desc (read-string "Description: " desc))
  7406. (unless (string-match "\\S-" desc) (setq desc nil))
  7407. (if remove (apply 'delete-region remove))
  7408. (insert (org-make-link-string link desc))))
  7409. (defun org-link-try-special-completion (type)
  7410. "If there is completion support for link type TYPE, offer it."
  7411. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7412. (if (functionp fun)
  7413. (funcall fun)
  7414. (read-string "Link (no completion support): " (concat type ":")))))
  7415. (defun org-file-complete-link (&optional arg)
  7416. "Create a file link using completion."
  7417. (let (file link)
  7418. (setq file (read-file-name "File: "))
  7419. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7420. (pwd1 (file-name-as-directory (abbreviate-file-name
  7421. (expand-file-name ".")))))
  7422. (cond
  7423. ((equal arg '(16))
  7424. (setq link (org-make-link
  7425. "file:"
  7426. (abbreviate-file-name (expand-file-name file)))))
  7427. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7428. (setq link (org-make-link "file:" (match-string 1 file))))
  7429. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7430. (expand-file-name file))
  7431. (setq link (org-make-link
  7432. "file:" (match-string 1 (expand-file-name file)))))
  7433. (t (setq link (org-make-link "file:" file)))))
  7434. link))
  7435. (defun org-completing-read (&rest args)
  7436. "Completing-read with SPACE being a normal character."
  7437. (let ((minibuffer-local-completion-map
  7438. (copy-keymap minibuffer-local-completion-map)))
  7439. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7440. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7441. (apply 'org-icompleting-read args)))
  7442. (defun org-completing-read-no-i (&rest args)
  7443. (let (org-completion-use-ido org-completion-use-iswitchb)
  7444. (apply 'org-completing-read args)))
  7445. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7446. "Use iswitch as a completing-read replacement to choose from choices.
  7447. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7448. from."
  7449. (let* ((iswitchb-use-virtual-buffers nil)
  7450. (iswitchb-make-buflist-hook
  7451. (lambda ()
  7452. (setq iswitchb-temp-buflist choices))))
  7453. (iswitchb-read-buffer prompt)))
  7454. (defun org-icompleting-read (&rest args)
  7455. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7456. (org-without-partial-completion
  7457. (if (and org-completion-use-ido
  7458. (fboundp 'ido-completing-read)
  7459. (boundp 'ido-mode) ido-mode
  7460. (listp (second args)))
  7461. (let ((ido-enter-matching-directory nil))
  7462. (apply 'ido-completing-read (concat (car args))
  7463. (if (consp (car (nth 1 args)))
  7464. (mapcar (lambda (x) (car x)) (nth 1 args))
  7465. (nth 1 args))
  7466. (cddr args)))
  7467. (if (and org-completion-use-iswitchb
  7468. (boundp 'iswitchb-mode) iswitchb-mode
  7469. (listp (second args)))
  7470. (apply 'org-iswitchb-completing-read (concat (car args))
  7471. (if (consp (car (nth 1 args)))
  7472. (mapcar (lambda (x) (car x)) (nth 1 args))
  7473. (nth 1 args))
  7474. (cddr args))
  7475. (apply 'completing-read args)))))
  7476. (defun org-extract-attributes (s)
  7477. "Extract the attributes cookie from a string and set as text property."
  7478. (let (a attr (start 0) key value)
  7479. (save-match-data
  7480. (when (string-match "{{\\([^}]+\\)}}$" s)
  7481. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7482. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7483. (setq key (match-string 1 a) value (match-string 2 a)
  7484. start (match-end 0)
  7485. attr (plist-put attr (intern key) value))))
  7486. (org-add-props s nil 'org-attr attr))
  7487. s))
  7488. (defun org-extract-attributes-from-string (tag)
  7489. (let (key value attr)
  7490. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7491. (setq key (match-string 1 tag) value (match-string 2 tag)
  7492. tag (replace-match "" t t tag)
  7493. attr (plist-put attr (intern key) value)))
  7494. (cons tag attr)))
  7495. (defun org-attributes-to-string (plist)
  7496. "Format a property list into an HTML attribute list."
  7497. (let ((s "") key value)
  7498. (while plist
  7499. (setq key (pop plist) value (pop plist))
  7500. (and value
  7501. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7502. s))
  7503. ;;; Opening/following a link
  7504. (defvar org-link-search-failed nil)
  7505. (defvar org-open-link-functions nil
  7506. "Hook for functions finding a plain text link.
  7507. These functions must take a single argument, the link content.
  7508. They will be called for links that look like [[link text][description]]
  7509. when LINK TEXT does not have a protocol like \"http:\" and does not look
  7510. like a filename (e.g. \"./blue.png\").
  7511. These functions will be called *before* Org attempts to resolve the
  7512. link by doing text searches in the current buffer - so if you want a
  7513. link \"[[target]]\" to still find \"<<target>>\", your function should
  7514. handle this as a special case.
  7515. When the function does handle the link, it must return a non-nil value.
  7516. If it decides that it is not responsible for this link, it must return
  7517. nil to indicate that that Org-mode can continue with other options
  7518. like exact and fuzzy text search.")
  7519. (defun org-next-link ()
  7520. "Move forward to the next link.
  7521. If the link is in hidden text, expose it."
  7522. (interactive)
  7523. (when (and org-link-search-failed (eq this-command last-command))
  7524. (goto-char (point-min))
  7525. (message "Link search wrapped back to beginning of buffer"))
  7526. (setq org-link-search-failed nil)
  7527. (let* ((pos (point))
  7528. (ct (org-context))
  7529. (a (assoc :link ct)))
  7530. (if a (goto-char (nth 2 a)))
  7531. (if (re-search-forward org-any-link-re nil t)
  7532. (progn
  7533. (goto-char (match-beginning 0))
  7534. (if (org-invisible-p) (org-show-context)))
  7535. (goto-char pos)
  7536. (setq org-link-search-failed t)
  7537. (error "No further link found"))))
  7538. (defun org-previous-link ()
  7539. "Move backward to the previous link.
  7540. If the link is in hidden text, expose it."
  7541. (interactive)
  7542. (when (and org-link-search-failed (eq this-command last-command))
  7543. (goto-char (point-max))
  7544. (message "Link search wrapped back to end of buffer"))
  7545. (setq org-link-search-failed nil)
  7546. (let* ((pos (point))
  7547. (ct (org-context))
  7548. (a (assoc :link ct)))
  7549. (if a (goto-char (nth 1 a)))
  7550. (if (re-search-backward org-any-link-re nil t)
  7551. (progn
  7552. (goto-char (match-beginning 0))
  7553. (if (org-invisible-p) (org-show-context)))
  7554. (goto-char pos)
  7555. (setq org-link-search-failed t)
  7556. (error "No further link found"))))
  7557. (defun org-translate-link (s)
  7558. "Translate a link string if a translation function has been defined."
  7559. (if (and org-link-translation-function
  7560. (fboundp org-link-translation-function)
  7561. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7562. (progn
  7563. (setq s (funcall org-link-translation-function
  7564. (match-string 1) (match-string 2)))
  7565. (concat (car s) ":" (cdr s)))
  7566. s))
  7567. (defun org-translate-link-from-planner (type path)
  7568. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7569. This is still an experimental function, your mileage may vary."
  7570. (cond
  7571. ((member type '("http" "https" "news" "ftp"))
  7572. ;; standard Internet links are the same.
  7573. nil)
  7574. ((and (equal type "irc") (string-match "^//" path))
  7575. ;; Planner has two / at the beginning of an irc link, we have 1.
  7576. ;; We should have zero, actually....
  7577. (setq path (substring path 1)))
  7578. ((and (equal type "lisp") (string-match "^/" path))
  7579. ;; Planner has a slash, we do not.
  7580. (setq type "elisp" path (substring path 1)))
  7581. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7582. ;; A typical message link. Planner has the id after the final slash,
  7583. ;; we separate it with a hash mark
  7584. (setq path (concat (match-string 1 path) "#"
  7585. (org-remove-angle-brackets (match-string 2 path)))))
  7586. )
  7587. (cons type path))
  7588. (defun org-find-file-at-mouse (ev)
  7589. "Open file link or URL at mouse."
  7590. (interactive "e")
  7591. (mouse-set-point ev)
  7592. (org-open-at-point 'in-emacs))
  7593. (defun org-open-at-mouse (ev)
  7594. "Open file link or URL at mouse."
  7595. (interactive "e")
  7596. (mouse-set-point ev)
  7597. (if (eq major-mode 'org-agenda-mode)
  7598. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7599. (org-open-at-point))
  7600. (defvar org-window-config-before-follow-link nil
  7601. "The window configuration before following a link.
  7602. This is saved in case the need arises to restore it.")
  7603. (defvar org-open-link-marker (make-marker)
  7604. "Marker pointing to the location where `org-open-at-point; was called.")
  7605. ;;;###autoload
  7606. (defun org-open-at-point-global ()
  7607. "Follow a link like Org-mode does.
  7608. This command can be called in any mode to follow a link that has
  7609. Org-mode syntax."
  7610. (interactive)
  7611. (org-run-like-in-org-mode 'org-open-at-point))
  7612. ;;;###autoload
  7613. (defun org-open-link-from-string (s &optional arg reference-buffer)
  7614. "Open a link in the string S, as if it was in Org-mode."
  7615. (interactive "sLink: \nP")
  7616. (let ((reference-buffer (or reference-buffer (current-buffer))))
  7617. (with-temp-buffer
  7618. (let ((org-inhibit-startup t))
  7619. (org-mode)
  7620. (insert s)
  7621. (goto-char (point-min))
  7622. (when reference-buffer
  7623. (setq org-link-abbrev-alist-local
  7624. (with-current-buffer reference-buffer
  7625. org-link-abbrev-alist-local)))
  7626. (org-open-at-point arg reference-buffer)))))
  7627. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7628. "Open link at or after point.
  7629. If there is no link at point, this function will search forward up to
  7630. the end of the current line.
  7631. Normally, files will be opened by an appropriate application. If the
  7632. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7633. With a double prefix argument, try to open outside of Emacs, in the
  7634. application the system uses for this file type."
  7635. (interactive "P")
  7636. (org-load-modules-maybe)
  7637. (move-marker org-open-link-marker (point))
  7638. (setq org-window-config-before-follow-link (current-window-configuration))
  7639. (org-remove-occur-highlights nil nil t)
  7640. (cond
  7641. ((and (org-on-heading-p)
  7642. (not (org-in-regexp
  7643. (concat org-plain-link-re "\\|"
  7644. org-bracket-link-regexp "\\|"
  7645. org-angle-link-re "\\|"
  7646. "[ \t]:[^ \t\n]+:[ \t]*$")))
  7647. (not (get-text-property (point) 'org-linked-text)))
  7648. (or (org-offer-links-in-entry in-emacs)
  7649. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  7650. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7651. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7652. (org-footnote-action))
  7653. (t
  7654. (let (type path link line search (pos (point)))
  7655. (catch 'match
  7656. (save-excursion
  7657. (skip-chars-forward "^]\n\r")
  7658. (when (org-in-regexp org-bracket-link-regexp 1)
  7659. (setq link (org-extract-attributes
  7660. (org-link-unescape (org-match-string-no-properties 1))))
  7661. (while (string-match " *\n *" link)
  7662. (setq link (replace-match " " t t link)))
  7663. (setq link (org-link-expand-abbrev link))
  7664. (cond
  7665. ((or (file-name-absolute-p link)
  7666. (string-match "^\\.\\.?/" link))
  7667. (setq type "file" path link))
  7668. ((string-match org-link-re-with-space3 link)
  7669. (setq type (match-string 1 link) path (match-string 2 link)))
  7670. (t (setq type "thisfile" path link)))
  7671. (throw 'match t)))
  7672. (when (get-text-property (point) 'org-linked-text)
  7673. (setq type "thisfile"
  7674. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7675. (1+ (point)) (point))
  7676. path (buffer-substring
  7677. (previous-single-property-change pos 'org-linked-text)
  7678. (next-single-property-change pos 'org-linked-text)))
  7679. (throw 'match t))
  7680. (save-excursion
  7681. (when (or (org-in-regexp org-angle-link-re)
  7682. (org-in-regexp org-plain-link-re))
  7683. (setq type (match-string 1) path (match-string 2))
  7684. (throw 'match t)))
  7685. (save-excursion
  7686. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7687. (setq type "tags"
  7688. path (match-string 1))
  7689. (while (string-match ":" path)
  7690. (setq path (replace-match "+" t t path)))
  7691. (throw 'match t)))
  7692. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7693. (setq type "tree-match"
  7694. path (match-string 1))
  7695. (throw 'match t)))
  7696. (unless path
  7697. (error "No link found"))
  7698. ;; switch back to reference buffer
  7699. ;; needed when if called in a temporary buffer through
  7700. ;; org-open-link-from-string
  7701. (with-current-buffer (or reference-buffer (current-buffer))
  7702. ;; Remove any trailing spaces in path
  7703. (if (string-match " +\\'" path)
  7704. (setq path (replace-match "" t t path)))
  7705. (if (and org-link-translation-function
  7706. (fboundp org-link-translation-function))
  7707. ;; Check if we need to translate the link
  7708. (let ((tmp (funcall org-link-translation-function type path)))
  7709. (setq type (car tmp) path (cdr tmp))))
  7710. (cond
  7711. ((assoc type org-link-protocols)
  7712. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7713. ((equal type "mailto")
  7714. (let ((cmd (car org-link-mailto-program))
  7715. (args (cdr org-link-mailto-program)) args1
  7716. (address path) (subject "") a)
  7717. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7718. (setq address (match-string 1 path)
  7719. subject (org-link-escape (match-string 2 path))))
  7720. (while args
  7721. (cond
  7722. ((not (stringp (car args))) (push (pop args) args1))
  7723. (t (setq a (pop args))
  7724. (if (string-match "%a" a)
  7725. (setq a (replace-match address t t a)))
  7726. (if (string-match "%s" a)
  7727. (setq a (replace-match subject t t a)))
  7728. (push a args1))))
  7729. (apply cmd (nreverse args1))))
  7730. ((member type '("http" "https" "ftp" "news"))
  7731. (browse-url (concat type ":" (org-link-escape
  7732. path org-link-escape-chars-browser))))
  7733. ((member type '("message"))
  7734. (browse-url (concat type ":" path)))
  7735. ((string= type "tags")
  7736. (org-tags-view in-emacs path))
  7737. ((string= type "tree-match")
  7738. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7739. ((string= type "file")
  7740. (if (string-match "::\\([0-9]+\\)\\'" path)
  7741. (setq line (string-to-number (match-string 1 path))
  7742. path (substring path 0 (match-beginning 0)))
  7743. (if (string-match "::\\(.+\\)\\'" path)
  7744. (setq search (match-string 1 path)
  7745. path (substring path 0 (match-beginning 0)))))
  7746. (if (string-match "[*?{]" (file-name-nondirectory path))
  7747. (dired path)
  7748. (org-open-file path in-emacs line search)))
  7749. ((string= type "news")
  7750. (require 'org-gnus)
  7751. (org-gnus-follow-link path))
  7752. ((string= type "shell")
  7753. (let ((cmd path))
  7754. (if (or (not org-confirm-shell-link-function)
  7755. (funcall org-confirm-shell-link-function
  7756. (format "Execute \"%s\" in shell? "
  7757. (org-add-props cmd nil
  7758. 'face 'org-warning))))
  7759. (progn
  7760. (message "Executing %s" cmd)
  7761. (shell-command cmd))
  7762. (error "Abort"))))
  7763. ((string= type "elisp")
  7764. (let ((cmd path))
  7765. (if (or (not org-confirm-elisp-link-function)
  7766. (funcall org-confirm-elisp-link-function
  7767. (format "Execute \"%s\" as elisp? "
  7768. (org-add-props cmd nil
  7769. 'face 'org-warning))))
  7770. (message "%s => %s" cmd
  7771. (if (equal (string-to-char cmd) ?\()
  7772. (eval (read cmd))
  7773. (call-interactively (read cmd))))
  7774. (error "Abort"))))
  7775. ((and (string= type "thisfile")
  7776. (run-hook-with-args-until-success
  7777. 'org-open-link-functions path)))
  7778. ((string= type "thisfile")
  7779. (if in-emacs
  7780. (switch-to-buffer-other-window
  7781. (org-get-buffer-for-internal-link (current-buffer)))
  7782. (org-mark-ring-push))
  7783. (let ((cmd `(org-link-search
  7784. ,path
  7785. ,(cond ((equal in-emacs '(4)) 'occur)
  7786. ((equal in-emacs '(16)) 'org-occur)
  7787. (t nil))
  7788. ,pos)))
  7789. (condition-case nil (eval cmd)
  7790. (error (progn (widen) (eval cmd))))))
  7791. (t
  7792. (browse-url-at-point)))))))
  7793. (move-marker org-open-link-marker nil)
  7794. (run-hook-with-args 'org-follow-link-hook))
  7795. (defun org-offer-links-in-entry (&optional nth zero)
  7796. "Offer links in the current entry and follow the selected link.
  7797. If there is only one link, follow it immediately as well.
  7798. If NTH is an integer, immediately pick the NTH link found.
  7799. If ZERO is a string, check also this string for a link, and if
  7800. there is one, offer it as link number zero."
  7801. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  7802. "\\(" org-angle-link-re "\\)\\|"
  7803. "\\(" org-plain-link-re "\\)"))
  7804. (cnt ?0)
  7805. (in-emacs (if (integerp nth) nil nth))
  7806. have-zero end links link c)
  7807. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  7808. (push (match-string 0 zero) links)
  7809. (setq cnt (1- cnt) have-zero t))
  7810. (save-excursion
  7811. (org-back-to-heading t)
  7812. (setq end (save-excursion (outline-next-heading) (point)))
  7813. (while (re-search-forward re end t)
  7814. (push (match-string 0) links))
  7815. (setq links (org-uniquify (reverse links))))
  7816. (cond
  7817. ((null links)
  7818. (message "No links"))
  7819. ((equal (length links) 1)
  7820. (setq link (list (car links))))
  7821. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  7822. (setq link (nth (if have-zero nth (1- nth)) links)))
  7823. (t ; we have to select a link
  7824. (save-excursion
  7825. (save-window-excursion
  7826. (delete-other-windows)
  7827. (with-output-to-temp-buffer "*Select Link*"
  7828. (mapc (lambda (l)
  7829. (if (not (string-match org-bracket-link-regexp l))
  7830. (princ (format "[%c] %s\n" (incf cnt)
  7831. (org-remove-angle-brackets l)))
  7832. (if (match-end 3)
  7833. (princ (format "[%c] %s (%s)\n" (incf cnt)
  7834. (match-string 3 l) (match-string 1 l)))
  7835. (princ (format "[%c] %s\n" (incf cnt)
  7836. (match-string 1 l))))))
  7837. links))
  7838. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  7839. (message "Select link to open, RET to open all:")
  7840. (setq c (read-char-exclusive))
  7841. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  7842. (when (equal c ?q) (error "Abort"))
  7843. (if (equal c ?\C-m)
  7844. (setq link links)
  7845. (setq nth (- c ?0))
  7846. (if have-zero (setq nth (1+ nth)))
  7847. (unless (and (integerp nth) (>= (length links) nth))
  7848. (error "Invalid link selection"))
  7849. (setq link (list (nth (1- nth) links))))))
  7850. (if link
  7851. (let ((buf (current-buffer)))
  7852. (dolist (l link)
  7853. (org-open-link-from-string l in-emacs buf))
  7854. t)
  7855. nil)))
  7856. ;; Add special file links that specify the way of opening
  7857. (org-add-link-type "file+sys" 'org-open-file-with-system)
  7858. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  7859. (defun org-open-file-with-system (path)
  7860. "Open file at PATH using the system way of opeing it."
  7861. (org-open-file path 'system))
  7862. (defun org-open-file-with-emacs (path)
  7863. "Open file at PATH in emacs."
  7864. (org-open-file path 'emacs))
  7865. (defun org-remove-file-link-modifiers ()
  7866. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  7867. (goto-char (point-min))
  7868. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  7869. (org-if-unprotected
  7870. (replace-match "file:" t t))))
  7871. (eval-after-load "org-exp"
  7872. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  7873. 'org-remove-file-link-modifiers))
  7874. ;;;; Time estimates
  7875. (defun org-get-effort (&optional pom)
  7876. "Get the effort estimate for the current entry."
  7877. (org-entry-get pom org-effort-property))
  7878. ;;; File search
  7879. (defvar org-create-file-search-functions nil
  7880. "List of functions to construct the right search string for a file link.
  7881. These functions are called in turn with point at the location to
  7882. which the link should point.
  7883. A function in the hook should first test if it would like to
  7884. handle this file type, for example by checking the major-mode or
  7885. the file extension. If it decides not to handle this file, it
  7886. should just return nil to give other functions a chance. If it
  7887. does handle the file, it must return the search string to be used
  7888. when following the link. The search string will be part of the
  7889. file link, given after a double colon, and `org-open-at-point'
  7890. will automatically search for it. If special measures must be
  7891. taken to make the search successful, another function should be
  7892. added to the companion hook `org-execute-file-search-functions',
  7893. which see.
  7894. A function in this hook may also use `setq' to set the variable
  7895. `description' to provide a suggestion for the descriptive text to
  7896. be used for this link when it gets inserted into an Org-mode
  7897. buffer with \\[org-insert-link].")
  7898. (defvar org-execute-file-search-functions nil
  7899. "List of functions to execute a file search triggered by a link.
  7900. Functions added to this hook must accept a single argument, the
  7901. search string that was part of the file link, the part after the
  7902. double colon. The function must first check if it would like to
  7903. handle this search, for example by checking the major-mode or the
  7904. file extension. If it decides not to handle this search, it
  7905. should just return nil to give other functions a chance. If it
  7906. does handle the search, it must return a non-nil value to keep
  7907. other functions from trying.
  7908. Each function can access the current prefix argument through the
  7909. variable `current-prefix-argument'. Note that a single prefix is
  7910. used to force opening a link in Emacs, so it may be good to only
  7911. use a numeric or double prefix to guide the search function.
  7912. In case this is needed, a function in this hook can also restore
  7913. the window configuration before `org-open-at-point' was called using:
  7914. (set-window-configuration org-window-config-before-follow-link)")
  7915. (defun org-link-search (s &optional type avoid-pos)
  7916. "Search for a link search option.
  7917. If S is surrounded by forward slashes, it is interpreted as a
  7918. regular expression. In org-mode files, this will create an `org-occur'
  7919. sparse tree. In ordinary files, `occur' will be used to list matches.
  7920. If the current buffer is in `dired-mode', grep will be used to search
  7921. in all files. If AVOID-POS is given, ignore matches near that position."
  7922. (let ((case-fold-search t)
  7923. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7924. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7925. (append '(("") (" ") ("\t") ("\n"))
  7926. org-emphasis-alist)
  7927. "\\|") "\\)"))
  7928. (pos (point))
  7929. (pre nil) (post nil)
  7930. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7931. (cond
  7932. ;; First check if there are any special
  7933. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7934. ;; Now try the builtin stuff
  7935. ((and (equal (string-to-char s0) ?#)
  7936. (> (length s0) 1)
  7937. (save-excursion
  7938. (goto-char (point-min))
  7939. (and
  7940. (re-search-forward
  7941. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7942. (setq type 'dedicated
  7943. pos (match-beginning 0))))
  7944. ;; There is an exact target for this
  7945. (goto-char pos)
  7946. (org-back-to-heading t)))
  7947. ((save-excursion
  7948. (goto-char (point-min))
  7949. (and
  7950. (re-search-forward
  7951. (concat "<<" (regexp-quote s0) ">>") nil t)
  7952. (setq type 'dedicated
  7953. pos (match-beginning 0))))
  7954. ;; There is an exact target for this
  7955. (goto-char pos))
  7956. ((and (string-match "^(\\(.*\\))$" s0)
  7957. (save-excursion
  7958. (goto-char (point-min))
  7959. (and
  7960. (re-search-forward
  7961. (concat "[^[]" (regexp-quote
  7962. (format org-coderef-label-format
  7963. (match-string 1 s0))))
  7964. nil t)
  7965. (setq type 'dedicated
  7966. pos (1+ (match-beginning 0))))))
  7967. ;; There is a coderef target for this
  7968. (goto-char pos))
  7969. ((string-match "^/\\(.*\\)/$" s)
  7970. ;; A regular expression
  7971. (cond
  7972. ((org-mode-p)
  7973. (org-occur (match-string 1 s)))
  7974. ;;((eq major-mode 'dired-mode)
  7975. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7976. (t (org-do-occur (match-string 1 s)))))
  7977. (t
  7978. ;; A normal search strings
  7979. (when (equal (string-to-char s) ?*)
  7980. ;; Anchor on headlines, post may include tags.
  7981. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7982. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7983. s (substring s 1)))
  7984. (remove-text-properties
  7985. 0 (length s)
  7986. '(face nil mouse-face nil keymap nil fontified nil) s)
  7987. ;; Make a series of regular expressions to find a match
  7988. (setq words (org-split-string s "[ \n\r\t]+")
  7989. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7990. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7991. "\\)" markers)
  7992. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7993. re2a (concat "[ \t\r\n]" re2a_)
  7994. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7995. re4 (concat "[^a-zA-Z_]" re4_)
  7996. re1 (concat pre re2 post)
  7997. re3 (concat pre (if pre re4_ re4) post)
  7998. re5 (concat pre ".*" re4)
  7999. re2 (concat pre re2)
  8000. re2a (concat pre (if pre re2a_ re2a))
  8001. re4 (concat pre (if pre re4_ re4))
  8002. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8003. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8004. re5 "\\)"
  8005. ))
  8006. (cond
  8007. ((eq type 'org-occur) (org-occur reall))
  8008. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8009. (t (goto-char (point-min))
  8010. (setq type 'fuzzy)
  8011. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8012. (org-search-not-self 1 re1 nil t)
  8013. (org-search-not-self 1 re2 nil t)
  8014. (org-search-not-self 1 re2a nil t)
  8015. (org-search-not-self 1 re3 nil t)
  8016. (org-search-not-self 1 re4 nil t)
  8017. (org-search-not-self 1 re5 nil t)
  8018. )
  8019. (goto-char (match-beginning 1))
  8020. (goto-char pos)
  8021. (error "No match")))))
  8022. (t
  8023. ;; Normal string-search
  8024. (goto-char (point-min))
  8025. (if (search-forward s nil t)
  8026. (goto-char (match-beginning 0))
  8027. (error "No match"))))
  8028. (and (org-mode-p) (org-show-context 'link-search))
  8029. type))
  8030. (defun org-search-not-self (group &rest args)
  8031. "Execute `re-search-forward', but only accept matches that do not
  8032. enclose the position of `org-open-link-marker'."
  8033. (let ((m org-open-link-marker))
  8034. (catch 'exit
  8035. (while (apply 're-search-forward args)
  8036. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8037. (goto-char (match-end group))
  8038. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8039. (> (match-beginning 0) (marker-position m))
  8040. (< (match-end 0) (marker-position m)))
  8041. (save-match-data
  8042. (or (not (org-in-regexp
  8043. org-bracket-link-analytic-regexp 1))
  8044. (not (match-end 4)) ; no description
  8045. (and (<= (match-beginning 4) (point))
  8046. (>= (match-end 4) (point))))))
  8047. (throw 'exit (point))))))))
  8048. (defun org-get-buffer-for-internal-link (buffer)
  8049. "Return a buffer to be used for displaying the link target of internal links."
  8050. (cond
  8051. ((not org-display-internal-link-with-indirect-buffer)
  8052. buffer)
  8053. ((string-match "(Clone)$" (buffer-name buffer))
  8054. (message "Buffer is already a clone, not making another one")
  8055. ;; we also do not modify visibility in this case
  8056. buffer)
  8057. (t ; make a new indirect buffer for displaying the link
  8058. (let* ((bn (buffer-name buffer))
  8059. (ibn (concat bn "(Clone)"))
  8060. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8061. (with-current-buffer ib (org-overview))
  8062. ib))))
  8063. (defun org-do-occur (regexp &optional cleanup)
  8064. "Call the Emacs command `occur'.
  8065. If CLEANUP is non-nil, remove the printout of the regular expression
  8066. in the *Occur* buffer. This is useful if the regex is long and not useful
  8067. to read."
  8068. (occur regexp)
  8069. (when cleanup
  8070. (let ((cwin (selected-window)) win beg end)
  8071. (when (setq win (get-buffer-window "*Occur*"))
  8072. (select-window win))
  8073. (goto-char (point-min))
  8074. (when (re-search-forward "match[a-z]+" nil t)
  8075. (setq beg (match-end 0))
  8076. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8077. (setq end (1- (match-beginning 0)))))
  8078. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8079. (goto-char (point-min))
  8080. (select-window cwin))))
  8081. ;;; The mark ring for links jumps
  8082. (defvar org-mark-ring nil
  8083. "Mark ring for positions before jumps in Org-mode.")
  8084. (defvar org-mark-ring-last-goto nil
  8085. "Last position in the mark ring used to go back.")
  8086. ;; Fill and close the ring
  8087. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8088. (loop for i from 1 to org-mark-ring-length do
  8089. (push (make-marker) org-mark-ring))
  8090. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8091. org-mark-ring)
  8092. (defun org-mark-ring-push (&optional pos buffer)
  8093. "Put the current position or POS into the mark ring and rotate it."
  8094. (interactive)
  8095. (setq pos (or pos (point)))
  8096. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8097. (move-marker (car org-mark-ring)
  8098. (or pos (point))
  8099. (or buffer (current-buffer)))
  8100. (message "%s"
  8101. (substitute-command-keys
  8102. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8103. (defun org-mark-ring-goto (&optional n)
  8104. "Jump to the previous position in the mark ring.
  8105. With prefix arg N, jump back that many stored positions. When
  8106. called several times in succession, walk through the entire ring.
  8107. Org-mode commands jumping to a different position in the current file,
  8108. or to another Org-mode file, automatically push the old position
  8109. onto the ring."
  8110. (interactive "p")
  8111. (let (p m)
  8112. (if (eq last-command this-command)
  8113. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8114. (setq p org-mark-ring))
  8115. (setq org-mark-ring-last-goto p)
  8116. (setq m (car p))
  8117. (switch-to-buffer (marker-buffer m))
  8118. (goto-char m)
  8119. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8120. (defun org-remove-angle-brackets (s)
  8121. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8122. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8123. s)
  8124. (defun org-add-angle-brackets (s)
  8125. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8126. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8127. s)
  8128. (defun org-remove-double-quotes (s)
  8129. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8130. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8131. s)
  8132. ;;; Following specific links
  8133. (defun org-follow-timestamp-link ()
  8134. (cond
  8135. ((org-at-date-range-p t)
  8136. (let ((org-agenda-start-on-weekday)
  8137. (t1 (match-string 1))
  8138. (t2 (match-string 2)))
  8139. (setq t1 (time-to-days (org-time-string-to-time t1))
  8140. t2 (time-to-days (org-time-string-to-time t2)))
  8141. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8142. ((org-at-timestamp-p t)
  8143. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8144. (substring (match-string 1) 0 10)))
  8145. 1))
  8146. (t (error "This should not happen"))))
  8147. ;;; Following file links
  8148. (defvar org-wait nil)
  8149. (defun org-open-file (path &optional in-emacs line search)
  8150. "Open the file at PATH.
  8151. First, this expands any special file name abbreviations. Then the
  8152. configuration variable `org-file-apps' is checked if it contains an
  8153. entry for this file type, and if yes, the corresponding command is launched.
  8154. If no application is found, Emacs simply visits the file.
  8155. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8156. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  8157. and to use an external application to visit the file.
  8158. Optional LINE specifies a line to go to, optional SEARCH a string to
  8159. search for. If LINE or SEARCH is given, but IN-EMACS is nil, it will
  8160. be assumed that org-open-file was called to open a file: link, and the
  8161. original link to match against org-file-apps will be reconstructed
  8162. from PATH and whichever of LINE or SEARCH is given.
  8163. If the file does not exist, an error is thrown."
  8164. (let* ((file (if (equal path "")
  8165. buffer-file-name
  8166. (substitute-in-file-name (expand-file-name path))))
  8167. (apps (append org-file-apps (org-default-apps)))
  8168. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8169. (dirp (if remp nil (file-directory-p file)))
  8170. (file (if (and dirp org-open-directory-means-index-dot-org)
  8171. (concat (file-name-as-directory file) "index.org")
  8172. file))
  8173. (a-m-a-p (assq 'auto-mode apps))
  8174. (dfile (downcase file))
  8175. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  8176. (link (cond ((and (eq line nil)
  8177. (eq search nil))
  8178. file)
  8179. (line
  8180. (concat file "::" (number-to-string line)))
  8181. (search
  8182. (concat file "::" search))))
  8183. (dlink (downcase link))
  8184. (old-buffer (current-buffer))
  8185. (old-pos (point))
  8186. (old-mode major-mode)
  8187. ext cmd link-match-data)
  8188. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8189. (setq ext (match-string 1 dfile))
  8190. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8191. (setq ext (match-string 1 dfile))))
  8192. (cond
  8193. ((member in-emacs '((16) system))
  8194. (setq cmd (cdr (assoc 'system apps))))
  8195. (in-emacs (setq cmd 'emacs))
  8196. (t
  8197. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8198. (and dirp (cdr (assoc 'directory apps)))
  8199. ;; if we find a match in org-file-apps, store the match
  8200. ;; data for later
  8201. (let ((match (assoc-default dlink (org-apps-regexp-alist
  8202. apps a-m-a-p)
  8203. 'string-match)))
  8204. (if match
  8205. (progn (setq link-match-data (match-data))
  8206. match)
  8207. nil))
  8208. (cdr (assoc ext apps))
  8209. (cdr (assoc t apps))))))
  8210. (when (eq cmd 'system)
  8211. (setq cmd (cdr (assoc 'system apps))))
  8212. (when (eq cmd 'default)
  8213. (setq cmd (cdr (assoc t apps))))
  8214. (when (eq cmd 'mailcap)
  8215. (require 'mailcap)
  8216. (mailcap-parse-mailcaps)
  8217. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8218. (command (mailcap-mime-info mime-type)))
  8219. (if (stringp command)
  8220. (setq cmd command)
  8221. (setq cmd 'emacs))))
  8222. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8223. (not (file-exists-p file))
  8224. (not org-open-non-existing-files))
  8225. (error "No such file: %s" file))
  8226. (cond
  8227. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8228. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8229. (while (string-match "['\"]%s['\"]" cmd)
  8230. (setq cmd (replace-match "%s" t t cmd)))
  8231. (while (string-match "%s" cmd)
  8232. (setq cmd (replace-match
  8233. (save-match-data
  8234. (shell-quote-argument
  8235. (convert-standard-filename file)))
  8236. t t cmd)))
  8237. ;; Replace "%1", "%2" etc. in command with group matches from regex
  8238. (save-match-data
  8239. (let ((match-index 1)
  8240. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  8241. (set-match-data link-match-data)
  8242. (while (<= match-index number-of-groups)
  8243. (let ((regex (concat "%" (number-to-string match-index)))
  8244. (replace-with (match-string match-index dlink)))
  8245. (while (string-match regex cmd)
  8246. (setq cmd (replace-match replace-with t t cmd))))
  8247. (setq match-index (+ match-index 1)))))
  8248. (save-window-excursion
  8249. (start-process-shell-command cmd nil cmd)
  8250. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8251. ))
  8252. ((or (stringp cmd)
  8253. (eq cmd 'emacs))
  8254. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8255. (widen)
  8256. (if line (org-goto-line line)
  8257. (if search (org-link-search search))))
  8258. ((consp cmd)
  8259. (let ((file (convert-standard-filename file)))
  8260. (save-match-data
  8261. (set-match-data link-match-data)
  8262. (eval cmd))))
  8263. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8264. (and (org-mode-p) (eq old-mode 'org-mode)
  8265. (or (not (equal old-buffer (current-buffer)))
  8266. (not (equal old-pos (point))))
  8267. (org-mark-ring-push old-pos old-buffer))))
  8268. (defun org-default-apps ()
  8269. "Return the default applications for this operating system."
  8270. (cond
  8271. ((eq system-type 'darwin)
  8272. org-file-apps-defaults-macosx)
  8273. ((eq system-type 'windows-nt)
  8274. org-file-apps-defaults-windowsnt)
  8275. (t org-file-apps-defaults-gnu)))
  8276. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  8277. "Convert extensions to regular expressions in the cars of LIST.
  8278. Also, weed out any non-string entries, because the return value is used
  8279. only for regexp matching.
  8280. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  8281. point to the symbol `emacs', indicating that the file should
  8282. be opened in Emacs."
  8283. (append
  8284. (delq nil
  8285. (mapcar (lambda (x)
  8286. (if (not (stringp (car x)))
  8287. nil
  8288. (if (string-match "\\W" (car x))
  8289. x
  8290. (cons (concat "\\." (car x) "\\(::.*\\)?\\'")
  8291. (cdr x)))))
  8292. list))
  8293. (if add-auto-mode
  8294. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  8295. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  8296. (defun org-file-remote-p (file)
  8297. "Test whether FILE specifies a location on a remote system.
  8298. Return non-nil if the location is indeed remote.
  8299. For example, the filename \"/user@host:/foo\" specifies a location
  8300. on the system \"/user@host:\"."
  8301. (cond ((fboundp 'file-remote-p)
  8302. (file-remote-p file))
  8303. ((fboundp 'tramp-handle-file-remote-p)
  8304. (tramp-handle-file-remote-p file))
  8305. ((and (boundp 'ange-ftp-name-format)
  8306. (string-match (car ange-ftp-name-format) file))
  8307. t)
  8308. (t nil)))
  8309. ;;;; Refiling
  8310. (defun org-get-org-file ()
  8311. "Read a filename, with default directory `org-directory'."
  8312. (let ((default (or org-default-notes-file remember-data-file)))
  8313. (read-file-name (format "File name [%s]: " default)
  8314. (file-name-as-directory org-directory)
  8315. default)))
  8316. (defun org-notes-order-reversed-p ()
  8317. "Check if the current file should receive notes in reversed order."
  8318. (cond
  8319. ((not org-reverse-note-order) nil)
  8320. ((eq t org-reverse-note-order) t)
  8321. ((not (listp org-reverse-note-order)) nil)
  8322. (t (catch 'exit
  8323. (let ((all org-reverse-note-order)
  8324. entry)
  8325. (while (setq entry (pop all))
  8326. (if (string-match (car entry) buffer-file-name)
  8327. (throw 'exit (cdr entry))))
  8328. nil)))))
  8329. (defvar org-refile-target-table nil
  8330. "The list of refile targets, created by `org-refile'.")
  8331. (defvar org-agenda-new-buffers nil
  8332. "Buffers created to visit agenda files.")
  8333. (defun org-get-refile-targets (&optional default-buffer)
  8334. "Produce a table with refile targets."
  8335. (let ((case-fold-search nil)
  8336. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  8337. (entries (or org-refile-targets '((nil . (:level . 1)))))
  8338. targets txt re files f desc descre fast-path-p level pos0)
  8339. (message "Getting targets...")
  8340. (with-current-buffer (or default-buffer (current-buffer))
  8341. (while (setq entry (pop entries))
  8342. (setq files (car entry) desc (cdr entry))
  8343. (setq fast-path-p nil)
  8344. (cond
  8345. ((null files) (setq files (list (current-buffer))))
  8346. ((eq files 'org-agenda-files)
  8347. (setq files (org-agenda-files 'unrestricted)))
  8348. ((and (symbolp files) (fboundp files))
  8349. (setq files (funcall files)))
  8350. ((and (symbolp files) (boundp files))
  8351. (setq files (symbol-value files))))
  8352. (if (stringp files) (setq files (list files)))
  8353. (cond
  8354. ((eq (car desc) :tag)
  8355. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  8356. ((eq (car desc) :todo)
  8357. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  8358. ((eq (car desc) :regexp)
  8359. (setq descre (cdr desc)))
  8360. ((eq (car desc) :level)
  8361. (setq descre (concat "^\\*\\{" (number-to-string
  8362. (if org-odd-levels-only
  8363. (1- (* 2 (cdr desc)))
  8364. (cdr desc)))
  8365. "\\}[ \t]")))
  8366. ((eq (car desc) :maxlevel)
  8367. (setq fast-path-p t)
  8368. (setq descre (concat "^\\*\\{1," (number-to-string
  8369. (if org-odd-levels-only
  8370. (1- (* 2 (cdr desc)))
  8371. (cdr desc)))
  8372. "\\}[ \t]")))
  8373. (t (error "Bad refiling target description %s" desc)))
  8374. (while (setq f (pop files))
  8375. (with-current-buffer
  8376. (if (bufferp f) f (org-get-agenda-file-buffer f))
  8377. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  8378. (setq f (and f (expand-file-name f)))
  8379. (if (eq org-refile-use-outline-path 'file)
  8380. (push (list (file-name-nondirectory f) f nil nil) targets))
  8381. (save-excursion
  8382. (save-restriction
  8383. (widen)
  8384. (goto-char (point-min))
  8385. (while (re-search-forward descre nil t)
  8386. (goto-char (setq pos0 (point-at-bol)))
  8387. (catch 'next
  8388. (when org-refile-target-verify-function
  8389. (save-match-data
  8390. (or (funcall org-refile-target-verify-function)
  8391. (throw 'next t))))
  8392. (when (looking-at org-complex-heading-regexp)
  8393. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  8394. txt (org-link-display-format (match-string 4))
  8395. re (concat "^" (regexp-quote
  8396. (buffer-substring (match-beginning 1)
  8397. (match-end 4)))))
  8398. (if (match-end 5) (setq re (concat re "[ \t]+"
  8399. (regexp-quote
  8400. (match-string 5)))))
  8401. (setq re (concat re "[ \t]*$"))
  8402. (when org-refile-use-outline-path
  8403. (setq txt (mapconcat 'org-protect-slash
  8404. (append
  8405. (if (eq org-refile-use-outline-path 'file)
  8406. (list (file-name-nondirectory
  8407. (buffer-file-name (buffer-base-buffer))))
  8408. (if (eq org-refile-use-outline-path 'full-file-path)
  8409. (list (buffer-file-name (buffer-base-buffer)))))
  8410. (org-get-outline-path fast-path-p level txt)
  8411. (list txt))
  8412. "/")))
  8413. (push (list txt f re (point)) targets)))
  8414. (when (= (point) pos0)
  8415. ;; verification function has not moved point
  8416. (goto-char (point-at-eol))))))))))
  8417. (message "Getting targets...done")
  8418. (nreverse targets)))
  8419. (defun org-protect-slash (s)
  8420. (while (string-match "/" s)
  8421. (setq s (replace-match "\\" t t s)))
  8422. s)
  8423. (defvar org-olpa (make-vector 20 nil))
  8424. (defun org-get-outline-path (&optional fastp level heading)
  8425. "Return the outline path to the current entry, as a list.
  8426. The parameters FASTP, LEVEL, and HEADING are for use be a scanner
  8427. routine which makes outline path derivations for an entire file,
  8428. avoiding backtracing."
  8429. (if fastp
  8430. (progn
  8431. (if (> level 19)
  8432. (error "Outline path failure, more than 19 levels."))
  8433. (loop for i from level upto 19 do
  8434. (aset org-olpa i nil))
  8435. (prog1
  8436. (delq nil (append org-olpa nil))
  8437. (aset org-olpa level heading)))
  8438. (let (rtn case-fold-search)
  8439. (save-excursion
  8440. (save-restriction
  8441. (widen)
  8442. (while (org-up-heading-safe)
  8443. (when (looking-at org-complex-heading-regexp)
  8444. (push (org-match-string-no-properties 4) rtn)))
  8445. rtn)))))
  8446. (defun org-format-outline-path (path &optional width prefix)
  8447. "Format the outlie path PATH for display.
  8448. Width is the maximum number of characters that is available.
  8449. Prefix is a prefix to be included in the returned string,
  8450. such as the file name."
  8451. (setq width (or width 79))
  8452. (if prefix (setq width (- width (length prefix))))
  8453. (if (not path)
  8454. (or prefix "")
  8455. (let* ((nsteps (length path))
  8456. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  8457. (maxwidth (if (<= total-width width)
  8458. 10000 ;; everything fits
  8459. ;; we need to shorten the level headings
  8460. (/ (- width nsteps) nsteps)))
  8461. (org-odd-levels-only nil)
  8462. (n 0)
  8463. (total (1+ (length prefix))))
  8464. (setq maxwidth (max maxwidth 10))
  8465. (concat prefix
  8466. (mapconcat
  8467. (lambda (h)
  8468. (setq n (1+ n))
  8469. (if (and (= n nsteps) (< maxwidth 10000))
  8470. (setq maxwidth (- total-width total)))
  8471. (if (< (length h) maxwidth)
  8472. (progn (setq total (+ total (length h) 1)) h)
  8473. (setq h (substring h 0 (- maxwidth 2))
  8474. total (+ total maxwidth 1))
  8475. (if (string-match "[ \t]+\\'" h)
  8476. (setq h (substring h 0 (match-beginning 0))))
  8477. (setq h (concat h "..")))
  8478. (org-add-props h nil 'face
  8479. (nth (% (1- n) org-n-level-faces)
  8480. org-level-faces))
  8481. h)
  8482. path "/")))))
  8483. (defun org-display-outline-path (&optional file current)
  8484. "Display the current outline path in the echo area."
  8485. (interactive "P")
  8486. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  8487. (case-fold-search nil)
  8488. (path (and (org-mode-p) (org-get-outline-path))))
  8489. (if current (setq path (append path
  8490. (save-excursion
  8491. (org-back-to-heading t)
  8492. (if (looking-at org-complex-heading-regexp)
  8493. (list (match-string 4)))))))
  8494. (message "%s"
  8495. (org-format-outline-path
  8496. path
  8497. (1- (frame-width))
  8498. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  8499. (defvar org-refile-history nil
  8500. "History for refiling operations.")
  8501. (defvar org-after-refile-insert-hook nil
  8502. "Hook run after `org-refile' has inserted its stuff at the new location.
  8503. Note that this is still *before* the stuff will be removed from
  8504. the *old* location.")
  8505. (defun org-refile (&optional goto default-buffer rfloc)
  8506. "Move the entry at point to another heading.
  8507. The list of target headings is compiled using the information in
  8508. `org-refile-targets', which see. This list is created before each use
  8509. and will therefore always be up-to-date.
  8510. At the target location, the entry is filed as a subitem of the target heading.
  8511. Depending on `org-reverse-note-order', the new subitem will either be the
  8512. first or the last subitem.
  8513. If there is an active region, all entries in that region will be moved.
  8514. However, the region must fulfil the requirement that the first heading
  8515. is the first one sets the top-level of the moved text - at most siblings
  8516. below it are allowed.
  8517. With prefix arg GOTO, the command will only visit the target location,
  8518. not actually move anything.
  8519. With a double prefix `C-u C-u', go to the location where the last refiling
  8520. operation has put the subtree.
  8521. With a prefix argument of `2', refile to the running clock.
  8522. RFLOC can be a refile location obtained in a different way.
  8523. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  8524. (interactive "P")
  8525. (let* ((cbuf (current-buffer))
  8526. (regionp (org-region-active-p))
  8527. (region-start (and regionp (region-beginning)))
  8528. (region-end (and regionp (region-end)))
  8529. (region-length (and regionp (- region-end region-start)))
  8530. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  8531. pos it nbuf file re level reversed)
  8532. (setq last-command nil)
  8533. (when regionp
  8534. (goto-char region-start)
  8535. (or (bolp) (goto-char (point-at-bol)))
  8536. (setq region-start (point))
  8537. (unless (org-kill-is-subtree-p
  8538. (buffer-substring region-start region-end))
  8539. (error "The region is not a (sequence of) subtree(s)")))
  8540. (if (equal goto '(16))
  8541. (org-refile-goto-last-stored)
  8542. (when (or
  8543. (and (equal goto 2)
  8544. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  8545. (prog1
  8546. (setq it (list (or org-clock-heading "running clock")
  8547. (buffer-file-name
  8548. (marker-buffer org-clock-hd-marker))
  8549. ""
  8550. (marker-position org-clock-hd-marker)))
  8551. (setq goto nil)))
  8552. (setq it (or rfloc
  8553. (save-excursion
  8554. (org-refile-get-location
  8555. (if goto "Goto: " "Refile to: ") default-buffer
  8556. org-refile-allow-creating-parent-nodes)))))
  8557. (setq file (nth 1 it)
  8558. re (nth 2 it)
  8559. pos (nth 3 it))
  8560. (if (and (not goto)
  8561. pos
  8562. (equal (buffer-file-name) file)
  8563. (if regionp
  8564. (and (>= pos region-start)
  8565. (<= pos region-end))
  8566. (and (>= pos (point))
  8567. (< pos (save-excursion
  8568. (org-end-of-subtree t t))))))
  8569. (error "Cannot refile to position inside the tree or region"))
  8570. (setq nbuf (or (find-buffer-visiting file)
  8571. (find-file-noselect file)))
  8572. (if goto
  8573. (progn
  8574. (switch-to-buffer nbuf)
  8575. (goto-char pos)
  8576. (org-show-context 'org-goto))
  8577. (if regionp
  8578. (progn
  8579. (org-kill-new (buffer-substring region-start region-end))
  8580. (org-save-markers-in-region region-start region-end))
  8581. (org-copy-subtree 1 nil t))
  8582. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  8583. (find-file-noselect file)))
  8584. (setq reversed (org-notes-order-reversed-p))
  8585. (save-excursion
  8586. (save-restriction
  8587. (widen)
  8588. (if pos
  8589. (progn
  8590. (goto-char pos)
  8591. (looking-at outline-regexp)
  8592. (setq level (org-get-valid-level (funcall outline-level) 1))
  8593. (goto-char
  8594. (if reversed
  8595. (or (outline-next-heading) (point-max))
  8596. (or (save-excursion (org-get-next-sibling))
  8597. (org-end-of-subtree t t)
  8598. (point-max)))))
  8599. (setq level 1)
  8600. (if (not reversed)
  8601. (goto-char (point-max))
  8602. (goto-char (point-min))
  8603. (or (outline-next-heading) (goto-char (point-max)))))
  8604. (if (not (bolp)) (newline))
  8605. (org-paste-subtree level)
  8606. (when org-log-refile
  8607. (org-add-log-setup 'refile nil nil 'findpos
  8608. org-log-refile)
  8609. (unless (eq org-log-refile 'note)
  8610. (save-excursion (org-add-log-note))))
  8611. (and org-auto-align-tags (org-set-tags nil t))
  8612. (bookmark-set "org-refile-last-stored")
  8613. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8614. (run-hooks 'org-after-refile-insert-hook))))
  8615. (if regionp
  8616. (delete-region (point) (+ (point) region-length))
  8617. (org-cut-subtree))
  8618. (when (featurep 'org-inlinetask)
  8619. (org-inlinetask-remove-END-maybe))
  8620. (setq org-markers-to-move nil)
  8621. (message "Refiled to \"%s\"" (car it))))))
  8622. (org-reveal))
  8623. (defun org-refile-goto-last-stored ()
  8624. "Go to the location where the last refile was stored."
  8625. (interactive)
  8626. (bookmark-jump "org-refile-last-stored")
  8627. (message "This is the location of the last refile"))
  8628. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  8629. "Prompt the user for a refile location, using PROMPT."
  8630. (let ((org-refile-targets org-refile-targets)
  8631. (org-refile-use-outline-path org-refile-use-outline-path))
  8632. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  8633. (unless org-refile-target-table
  8634. (error "No refile targets"))
  8635. (let* ((cbuf (current-buffer))
  8636. (partial-completion-mode nil)
  8637. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  8638. (cfunc (if (and org-refile-use-outline-path
  8639. org-outline-path-complete-in-steps)
  8640. 'org-olpath-completing-read
  8641. 'org-icompleting-read))
  8642. (extra (if org-refile-use-outline-path "/" ""))
  8643. (filename (and cfn (expand-file-name cfn)))
  8644. (tbl (mapcar
  8645. (lambda (x)
  8646. (if (and (not (member org-refile-use-outline-path
  8647. '(file full-file-path)))
  8648. (not (equal filename (nth 1 x))))
  8649. (cons (concat (car x) extra " ("
  8650. (file-name-nondirectory (nth 1 x)) ")")
  8651. (cdr x))
  8652. (cons (concat (car x) extra) (cdr x))))
  8653. org-refile-target-table))
  8654. (completion-ignore-case t)
  8655. pa answ parent-target child parent old-hist)
  8656. (setq old-hist org-refile-history)
  8657. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  8658. nil 'org-refile-history))
  8659. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  8660. (if pa
  8661. (progn
  8662. (when (or (not org-refile-history)
  8663. (not (eq old-hist org-refile-history))
  8664. (not (equal (car pa) (car org-refile-history))))
  8665. (setq org-refile-history
  8666. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  8667. org-refile-history
  8668. (cdr org-refile-history))))
  8669. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  8670. (pop org-refile-history)))
  8671. pa)
  8672. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8673. (progn
  8674. (setq parent (match-string 1 answ)
  8675. child (match-string 2 answ))
  8676. (setq parent-target (or (assoc parent tbl)
  8677. (assoc (concat parent "/") tbl)))
  8678. (when (and parent-target
  8679. (or (eq new-nodes t)
  8680. (and (eq new-nodes 'confirm)
  8681. (y-or-n-p (format "Create new node \"%s\"? "
  8682. child)))))
  8683. (org-refile-new-child parent-target child)))
  8684. (error "Invalid target location")))))
  8685. (defun org-refile-new-child (parent-target child)
  8686. "Use refile target PARENT-TARGET to add new CHILD below it."
  8687. (unless parent-target
  8688. (error "Cannot find parent for new node"))
  8689. (let ((file (nth 1 parent-target))
  8690. (pos (nth 3 parent-target))
  8691. level)
  8692. (with-current-buffer (or (find-buffer-visiting file)
  8693. (find-file-noselect file))
  8694. (save-excursion
  8695. (save-restriction
  8696. (widen)
  8697. (if pos
  8698. (goto-char pos)
  8699. (goto-char (point-max))
  8700. (if (not (bolp)) (newline)))
  8701. (when (looking-at outline-regexp)
  8702. (setq level (funcall outline-level))
  8703. (org-end-of-subtree t t))
  8704. (org-back-over-empty-lines)
  8705. (insert "\n" (make-string
  8706. (if pos (org-get-valid-level level 1) 1) ?*)
  8707. " " child "\n")
  8708. (beginning-of-line 0)
  8709. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8710. (defun org-olpath-completing-read (prompt collection &rest args)
  8711. "Read an outline path like a file name."
  8712. (let ((thetable collection)
  8713. (org-completion-use-ido nil) ; does not work with ido.
  8714. (org-completion-use-iswitchb nil)) ; or iswitchb
  8715. (apply
  8716. 'org-icompleting-read prompt
  8717. (lambda (string predicate &optional flag)
  8718. (let (rtn r f (l (length string)))
  8719. (cond
  8720. ((eq flag nil)
  8721. ;; try completion
  8722. (try-completion string thetable))
  8723. ((eq flag t)
  8724. ;; all-completions
  8725. (setq rtn (all-completions string thetable predicate))
  8726. (mapcar
  8727. (lambda (x)
  8728. (setq r (substring x l))
  8729. (if (string-match " ([^)]*)$" x)
  8730. (setq f (match-string 0 x))
  8731. (setq f ""))
  8732. (if (string-match "/" r)
  8733. (concat string (substring r 0 (match-end 0)) f)
  8734. x))
  8735. rtn))
  8736. ((eq flag 'lambda)
  8737. ;; exact match?
  8738. (assoc string thetable)))
  8739. ))
  8740. args)))
  8741. ;;;; Dynamic blocks
  8742. (defun org-find-dblock (name)
  8743. "Find the first dynamic block with name NAME in the buffer.
  8744. If not found, stay at current position and return nil."
  8745. (let (pos)
  8746. (save-excursion
  8747. (goto-char (point-min))
  8748. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8749. nil t)
  8750. (match-beginning 0))))
  8751. (if pos (goto-char pos))
  8752. pos))
  8753. (defconst org-dblock-start-re
  8754. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8755. "Matches the start line of a dynamic block, with parameters.")
  8756. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  8757. "Matches the end of a dynamic block.")
  8758. (defun org-create-dblock (plist)
  8759. "Create a dynamic block section, with parameters taken from PLIST.
  8760. PLIST must contain a :name entry which is used as name of the block."
  8761. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  8762. (end-of-line 1)
  8763. (newline))
  8764. (let ((col (current-column))
  8765. (name (plist-get plist :name)))
  8766. (insert "#+BEGIN: " name)
  8767. (while plist
  8768. (if (eq (car plist) :name)
  8769. (setq plist (cddr plist))
  8770. (insert " " (prin1-to-string (pop plist)))))
  8771. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  8772. (beginning-of-line -2)))
  8773. (defun org-prepare-dblock ()
  8774. "Prepare dynamic block for refresh.
  8775. This empties the block, puts the cursor at the insert position and returns
  8776. the property list including an extra property :name with the block name."
  8777. (unless (looking-at org-dblock-start-re)
  8778. (error "Not at a dynamic block"))
  8779. (let* ((begdel (1+ (match-end 0)))
  8780. (name (org-no-properties (match-string 1)))
  8781. (params (append (list :name name)
  8782. (read (concat "(" (match-string 3) ")")))))
  8783. (save-excursion
  8784. (beginning-of-line 1)
  8785. (skip-chars-forward " \t")
  8786. (setq params (plist-put params :indentation-column (current-column))))
  8787. (unless (re-search-forward org-dblock-end-re nil t)
  8788. (error "Dynamic block not terminated"))
  8789. (setq params
  8790. (append params
  8791. (list :content (buffer-substring
  8792. begdel (match-beginning 0)))))
  8793. (delete-region begdel (match-beginning 0))
  8794. (goto-char begdel)
  8795. (open-line 1)
  8796. params))
  8797. (defun org-map-dblocks (&optional command)
  8798. "Apply COMMAND to all dynamic blocks in the current buffer.
  8799. If COMMAND is not given, use `org-update-dblock'."
  8800. (let ((cmd (or command 'org-update-dblock)))
  8801. (save-excursion
  8802. (goto-char (point-min))
  8803. (while (re-search-forward org-dblock-start-re nil t)
  8804. (goto-char (match-beginning 0))
  8805. (save-excursion
  8806. (condition-case nil
  8807. (funcall cmd)
  8808. (error (message "Error during update of dynamic block"))))
  8809. (unless (re-search-forward org-dblock-end-re nil t)
  8810. (error "Dynamic block not terminated"))))))
  8811. (defun org-dblock-update (&optional arg)
  8812. "User command for updating dynamic blocks.
  8813. Update the dynamic block at point. With prefix ARG, update all dynamic
  8814. blocks in the buffer."
  8815. (interactive "P")
  8816. (if arg
  8817. (org-update-all-dblocks)
  8818. (or (looking-at org-dblock-start-re)
  8819. (org-beginning-of-dblock))
  8820. (org-update-dblock)))
  8821. (defun org-update-dblock ()
  8822. "Update the dynamic block at point
  8823. This means to empty the block, parse for parameters and then call
  8824. the correct writing function."
  8825. (save-window-excursion
  8826. (let* ((pos (point))
  8827. (line (org-current-line))
  8828. (params (org-prepare-dblock))
  8829. (name (plist-get params :name))
  8830. (indent (plist-get params :indentation-column))
  8831. (cmd (intern (concat "org-dblock-write:" name))))
  8832. (message "Updating dynamic block `%s' at line %d..." name line)
  8833. (funcall cmd params)
  8834. (message "Updating dynamic block `%s' at line %d...done" name line)
  8835. (goto-char pos)
  8836. (when (and indent (> indent 0))
  8837. (setq indent (make-string indent ?\ ))
  8838. (save-excursion
  8839. (org-beginning-of-dblock)
  8840. (forward-line 1)
  8841. (while (not (looking-at org-dblock-end-re))
  8842. (insert indent)
  8843. (beginning-of-line 2))
  8844. (when (looking-at org-dblock-end-re)
  8845. (and (looking-at "[ \t]+")
  8846. (replace-match ""))
  8847. (insert indent)))))))
  8848. (defun org-beginning-of-dblock ()
  8849. "Find the beginning of the dynamic block at point.
  8850. Error if there is no such block at point."
  8851. (let ((pos (point))
  8852. beg)
  8853. (end-of-line 1)
  8854. (if (and (re-search-backward org-dblock-start-re nil t)
  8855. (setq beg (match-beginning 0))
  8856. (re-search-forward org-dblock-end-re nil t)
  8857. (> (match-end 0) pos))
  8858. (goto-char beg)
  8859. (goto-char pos)
  8860. (error "Not in a dynamic block"))))
  8861. (defun org-update-all-dblocks ()
  8862. "Update all dynamic blocks in the buffer.
  8863. This function can be used in a hook."
  8864. (when (org-mode-p)
  8865. (org-map-dblocks 'org-update-dblock)))
  8866. ;;;; Completion
  8867. (defconst org-additional-option-like-keywords
  8868. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8869. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8870. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  8871. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX"
  8872. "BEGIN:" "END:"
  8873. "ORGTBL" "TBLFM:" "TBLNAME:"
  8874. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8875. "BEGIN_QUOTE" "END_QUOTE"
  8876. "BEGIN_VERSE" "END_VERSE"
  8877. "BEGIN_CENTER" "END_CENTER"
  8878. "BEGIN_SRC" "END_SRC"
  8879. "CATEGORY" "COLUMNS"
  8880. "CAPTION" "LABEL"
  8881. "SETUPFILE"
  8882. "BIND"
  8883. "MACRO"))
  8884. (defcustom org-structure-template-alist
  8885. '(
  8886. ("s" "#+begin_src ?\n\n#+end_src"
  8887. "<src lang=\"?\">\n\n</src>")
  8888. ("e" "#+begin_example\n?\n#+end_example"
  8889. "<example>\n?\n</example>")
  8890. ("q" "#+begin_quote\n?\n#+end_quote"
  8891. "<quote>\n?\n</quote>")
  8892. ("v" "#+begin_verse\n?\n#+end_verse"
  8893. "<verse>\n?\n/verse>")
  8894. ("c" "#+begin_center\n?\n#+end_center"
  8895. "<center>\n?\n/center>")
  8896. ("l" "#+begin_latex\n?\n#+end_latex"
  8897. "<literal style=\"latex\">\n?\n</literal>")
  8898. ("L" "#+latex: "
  8899. "<literal style=\"latex\">?</literal>")
  8900. ("h" "#+begin_html\n?\n#+end_html"
  8901. "<literal style=\"html\">\n?\n</literal>")
  8902. ("H" "#+html: "
  8903. "<literal style=\"html\">?</literal>")
  8904. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8905. ("A" "#+ascii: ")
  8906. ("i" "#+include %file ?"
  8907. "<include file=%file markup=\"?\">")
  8908. )
  8909. "Structure completion elements.
  8910. This is a list of abbreviation keys and values. The value gets inserted
  8911. it you type @samp{.} followed by the key and then the completion key,
  8912. usually `M-TAB'. %file will be replaced by a file name after prompting
  8913. for the file using completion.
  8914. There are two templates for each key, the first uses the original Org syntax,
  8915. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8916. the default when the /org-mtags.el/ module has been loaded. See also the
  8917. variable `org-mtags-prefer-muse-templates'.
  8918. This is an experimental feature, it is undecided if it is going to stay in."
  8919. :group 'org-completion
  8920. :type '(repeat
  8921. (string :tag "Key")
  8922. (string :tag "Template")
  8923. (string :tag "Muse Template")))
  8924. (defun org-try-structure-completion ()
  8925. "Try to complete a structure template before point.
  8926. This looks for strings like \"<e\" on an otherwise empty line and
  8927. expands them."
  8928. (let ((l (buffer-substring (point-at-bol) (point)))
  8929. a)
  8930. (when (and (looking-at "[ \t]*$")
  8931. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8932. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8933. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8934. (match-beginning 1)) a)
  8935. t)))
  8936. (defun org-complete-expand-structure-template (start cell)
  8937. "Expand a structure template."
  8938. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8939. (rpl (nth (if musep 2 1) cell))
  8940. (ind ""))
  8941. (delete-region start (point))
  8942. (when (string-match "\\`#\\+" rpl)
  8943. (cond
  8944. ((bolp))
  8945. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8946. (setq ind (buffer-substring (point-at-bol) (point))))
  8947. (t (newline))))
  8948. (setq start (point))
  8949. (if (string-match "%file" rpl)
  8950. (setq rpl (replace-match
  8951. (concat
  8952. "\""
  8953. (save-match-data
  8954. (abbreviate-file-name (read-file-name "Include file: ")))
  8955. "\"")
  8956. t t rpl)))
  8957. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  8958. (concat "\n" ind)))
  8959. (insert rpl)
  8960. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8961. (defun org-complete (&optional arg)
  8962. "Perform completion on word at point.
  8963. At the beginning of a headline, this completes TODO keywords as given in
  8964. `org-todo-keywords'.
  8965. If the current word is preceded by a backslash, completes the TeX symbols
  8966. that are supported for HTML support.
  8967. If the current word is preceded by \"#+\", completes special words for
  8968. setting file options.
  8969. In the line after \"#+STARTUP:, complete valid keywords.\"
  8970. At all other locations, this simply calls the value of
  8971. `org-completion-fallback-command'."
  8972. (interactive "P")
  8973. (org-without-partial-completion
  8974. (catch 'exit
  8975. (let* ((a nil)
  8976. (end (point))
  8977. (beg1 (save-excursion
  8978. (skip-chars-backward (org-re "[:alnum:]_@"))
  8979. (point)))
  8980. (beg (save-excursion
  8981. (skip-chars-backward "a-zA-Z0-9_:$")
  8982. (point)))
  8983. (confirm (lambda (x) (stringp (car x))))
  8984. (searchhead (equal (char-before beg) ?*))
  8985. (struct
  8986. (when (and (member (char-before beg1) '(?. ?<))
  8987. (setq a (assoc (buffer-substring beg1 (point))
  8988. org-structure-template-alist)))
  8989. (org-complete-expand-structure-template (1- beg1) a)
  8990. (throw 'exit t)))
  8991. (tag (and (equal (char-before beg1) ?:)
  8992. (equal (char-after (point-at-bol)) ?*)))
  8993. (prop (and (equal (char-before beg1) ?:)
  8994. (not (equal (char-after (point-at-bol)) ?*))))
  8995. (texp (equal (char-before beg) ?\\))
  8996. (link (equal (char-before beg) ?\[))
  8997. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8998. beg)
  8999. "#+"))
  9000. (startup (string-match "^#\\+STARTUP:.*"
  9001. (buffer-substring (point-at-bol) (point))))
  9002. (completion-ignore-case opt)
  9003. (type nil)
  9004. (tbl nil)
  9005. (table (cond
  9006. (opt
  9007. (setq type :opt)
  9008. (require 'org-exp)
  9009. (append
  9010. (delq nil
  9011. (mapcar
  9012. (lambda (x)
  9013. (if (string-match
  9014. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  9015. (cons (match-string 2 x)
  9016. (match-string 1 x))))
  9017. (org-split-string (org-get-current-options) "\n")))
  9018. (mapcar 'list org-additional-option-like-keywords)))
  9019. (startup
  9020. (setq type :startup)
  9021. org-startup-options)
  9022. (link (append org-link-abbrev-alist-local
  9023. org-link-abbrev-alist))
  9024. (texp
  9025. (setq type :tex)
  9026. org-html-entities)
  9027. ((string-match "\\`\\*+[ \t]+\\'"
  9028. (buffer-substring (point-at-bol) beg))
  9029. (setq type :todo)
  9030. (mapcar 'list org-todo-keywords-1))
  9031. (searchhead
  9032. (setq type :searchhead)
  9033. (save-excursion
  9034. (goto-char (point-min))
  9035. (while (re-search-forward org-todo-line-regexp nil t)
  9036. (push (list
  9037. (org-make-org-heading-search-string
  9038. (match-string 3) t))
  9039. tbl)))
  9040. tbl)
  9041. (tag (setq type :tag beg beg1)
  9042. (or org-tag-alist (org-get-buffer-tags)))
  9043. (prop (setq type :prop beg beg1)
  9044. (mapcar 'list (org-buffer-property-keys nil t t)))
  9045. (t (progn
  9046. (call-interactively org-completion-fallback-command)
  9047. (throw 'exit nil)))))
  9048. (pattern (buffer-substring-no-properties beg end))
  9049. (completion (try-completion pattern table confirm)))
  9050. (cond ((eq completion t)
  9051. (if (not (assoc (upcase pattern) table))
  9052. (message "Already complete")
  9053. (if (and (equal type :opt)
  9054. (not (member (car (assoc (upcase pattern) table))
  9055. org-additional-option-like-keywords)))
  9056. (insert (substring (cdr (assoc (upcase pattern) table))
  9057. (length pattern)))
  9058. (if (memq type '(:tag :prop)) (insert ":")))))
  9059. ((null completion)
  9060. (message "Can't find completion for \"%s\"" pattern)
  9061. (ding))
  9062. ((not (string= pattern completion))
  9063. (delete-region beg end)
  9064. (if (string-match " +$" completion)
  9065. (setq completion (replace-match "" t t completion)))
  9066. (insert completion)
  9067. (if (get-buffer-window "*Completions*")
  9068. (delete-window (get-buffer-window "*Completions*")))
  9069. (if (assoc completion table)
  9070. (if (eq type :todo) (insert " ")
  9071. (if (memq type '(:tag :prop)) (insert ":"))))
  9072. (if (and (equal type :opt) (assoc completion table))
  9073. (message "%s" (substitute-command-keys
  9074. "Press \\[org-complete] again to insert example settings"))))
  9075. (t
  9076. (message "Making completion list...")
  9077. (let ((list (sort (all-completions pattern table confirm)
  9078. 'string<)))
  9079. (with-output-to-temp-buffer "*Completions*"
  9080. (condition-case nil
  9081. ;; Protection needed for XEmacs and emacs 21
  9082. (display-completion-list list pattern)
  9083. (error (display-completion-list list)))))
  9084. (message "Making completion list...%s" "done")))))))
  9085. ;;;; TODO, DEADLINE, Comments
  9086. (defun org-toggle-comment ()
  9087. "Change the COMMENT state of an entry."
  9088. (interactive)
  9089. (save-excursion
  9090. (org-back-to-heading)
  9091. (let (case-fold-search)
  9092. (if (looking-at (concat outline-regexp
  9093. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9094. (replace-match "" t t nil 1)
  9095. (if (looking-at outline-regexp)
  9096. (progn
  9097. (goto-char (match-end 0))
  9098. (insert org-comment-string " ")))))))
  9099. (defvar org-last-todo-state-is-todo nil
  9100. "This is non-nil when the last TODO state change led to a TODO state.
  9101. If the last change removed the TODO tag or switched to DONE, then
  9102. this is nil.")
  9103. (defvar org-setting-tags nil) ; dynamically skipped
  9104. (defun org-parse-local-options (string var)
  9105. "Parse STRING for startup setting relevant for variable VAR."
  9106. (let ((rtn (symbol-value var))
  9107. e opts)
  9108. (save-match-data
  9109. (if (or (not string) (not (string-match "\\S-" string)))
  9110. rtn
  9111. (setq opts (delq nil (mapcar (lambda (x)
  9112. (setq e (assoc x org-startup-options))
  9113. (if (eq (nth 1 e) var) e nil))
  9114. (org-split-string string "[ \t]+"))))
  9115. (if (not opts)
  9116. rtn
  9117. (setq rtn nil)
  9118. (while (setq e (pop opts))
  9119. (if (not (nth 3 e))
  9120. (setq rtn (nth 2 e))
  9121. (if (not (listp rtn)) (setq rtn nil))
  9122. (push (nth 2 e) rtn)))
  9123. rtn)))))
  9124. (defvar org-todo-setup-filter-hook nil
  9125. "Hook for functions that pre-filter todo specs.
  9126. Each function takes a todo spec and returns either `nil' or the spec
  9127. transformed into canonical form." )
  9128. (defvar org-todo-get-default-hook nil
  9129. "Hook for functions that get a default item for todo.
  9130. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9131. `nil' or a string to be used for the todo mark." )
  9132. (defvar org-agenda-headline-snapshot-before-repeat)
  9133. (defun org-todo (&optional arg)
  9134. "Change the TODO state of an item.
  9135. The state of an item is given by a keyword at the start of the heading,
  9136. like
  9137. *** TODO Write paper
  9138. *** DONE Call mom
  9139. The different keywords are specified in the variable `org-todo-keywords'.
  9140. By default the available states are \"TODO\" and \"DONE\".
  9141. So for this example: when the item starts with TODO, it is changed to DONE.
  9142. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9143. DONE are present, add TODO at the beginning of the heading.
  9144. With C-u prefix arg, use completion to determine the new state.
  9145. With numeric prefix arg, switch to that state.
  9146. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  9147. With a triple C-u prefix, circumvent any state blocking.
  9148. For calling through lisp, arg is also interpreted in the following way:
  9149. 'none -> empty state
  9150. \"\"(empty string) -> switch to empty state
  9151. 'done -> switch to DONE
  9152. 'nextset -> switch to the next set of keywords
  9153. 'previousset -> switch to the previous set of keywords
  9154. \"WAITING\" -> switch to the specified keyword, but only if it
  9155. really is a member of `org-todo-keywords'."
  9156. (interactive "P")
  9157. (if (equal arg '(16)) (setq arg 'nextset))
  9158. (let ((org-blocker-hook org-blocker-hook)
  9159. (case-fold-search nil))
  9160. (when (equal arg '(64))
  9161. (setq arg nil org-blocker-hook nil))
  9162. (when (and org-blocker-hook
  9163. (or org-inhibit-blocking
  9164. (org-entry-get nil "NOBLOCKING")))
  9165. (setq org-blocker-hook nil))
  9166. (save-excursion
  9167. (catch 'exit
  9168. (org-back-to-heading t)
  9169. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9170. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9171. (looking-at " *"))
  9172. (let* ((match-data (match-data))
  9173. (startpos (point-at-bol))
  9174. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  9175. (org-log-done org-log-done)
  9176. (org-log-repeat org-log-repeat)
  9177. (org-todo-log-states org-todo-log-states)
  9178. (this (match-string 1))
  9179. (hl-pos (match-beginning 0))
  9180. (head (org-get-todo-sequence-head this))
  9181. (ass (assoc head org-todo-kwd-alist))
  9182. (interpret (nth 1 ass))
  9183. (done-word (nth 3 ass))
  9184. (final-done-word (nth 4 ass))
  9185. (last-state (or this ""))
  9186. (completion-ignore-case t)
  9187. (member (member this org-todo-keywords-1))
  9188. (tail (cdr member))
  9189. (state (cond
  9190. ((and org-todo-key-trigger
  9191. (or (and (equal arg '(4))
  9192. (eq org-use-fast-todo-selection 'prefix))
  9193. (and (not arg) org-use-fast-todo-selection
  9194. (not (eq org-use-fast-todo-selection
  9195. 'prefix)))))
  9196. ;; Use fast selection
  9197. (org-fast-todo-selection))
  9198. ((and (equal arg '(4))
  9199. (or (not org-use-fast-todo-selection)
  9200. (not org-todo-key-trigger)))
  9201. ;; Read a state with completion
  9202. (org-icompleting-read
  9203. "State: " (mapcar (lambda(x) (list x))
  9204. org-todo-keywords-1)
  9205. nil t))
  9206. ((eq arg 'right)
  9207. (if this
  9208. (if tail (car tail) nil)
  9209. (car org-todo-keywords-1)))
  9210. ((eq arg 'left)
  9211. (if (equal member org-todo-keywords-1)
  9212. nil
  9213. (if this
  9214. (nth (- (length org-todo-keywords-1)
  9215. (length tail) 2)
  9216. org-todo-keywords-1)
  9217. (org-last org-todo-keywords-1))))
  9218. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9219. (setq arg nil))) ; hack to fall back to cycling
  9220. (arg
  9221. ;; user or caller requests a specific state
  9222. (cond
  9223. ((equal arg "") nil)
  9224. ((eq arg 'none) nil)
  9225. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9226. ((eq arg 'nextset)
  9227. (or (car (cdr (member head org-todo-heads)))
  9228. (car org-todo-heads)))
  9229. ((eq arg 'previousset)
  9230. (let ((org-todo-heads (reverse org-todo-heads)))
  9231. (or (car (cdr (member head org-todo-heads)))
  9232. (car org-todo-heads))))
  9233. ((car (member arg org-todo-keywords-1)))
  9234. ((stringp arg)
  9235. (error "State `%s' not valid in this file" arg))
  9236. ((nth (1- (prefix-numeric-value arg))
  9237. org-todo-keywords-1))))
  9238. ((null member) (or head (car org-todo-keywords-1)))
  9239. ((equal this final-done-word) nil) ;; -> make empty
  9240. ((null tail) nil) ;; -> first entry
  9241. ((memq interpret '(type priority))
  9242. (if (eq this-command last-command)
  9243. (car tail)
  9244. (if (> (length tail) 0)
  9245. (or done-word (car org-done-keywords))
  9246. nil)))
  9247. (t
  9248. (car tail))))
  9249. (state (or
  9250. (run-hook-with-args-until-success
  9251. 'org-todo-get-default-hook state last-state)
  9252. state))
  9253. (next (if state (concat " " state " ") " "))
  9254. (change-plist (list :type 'todo-state-change :from this :to state
  9255. :position startpos))
  9256. dolog now-done-p)
  9257. (when org-blocker-hook
  9258. (setq org-last-todo-state-is-todo
  9259. (not (member this org-done-keywords)))
  9260. (unless (save-excursion
  9261. (save-match-data
  9262. (run-hook-with-args-until-failure
  9263. 'org-blocker-hook change-plist)))
  9264. (if (interactive-p)
  9265. (error "TODO state change from %s to %s blocked" this state)
  9266. ;; fail silently
  9267. (message "TODO state change from %s to %s blocked" this state)
  9268. (throw 'exit nil))))
  9269. (store-match-data match-data)
  9270. (replace-match next t t)
  9271. (unless (pos-visible-in-window-p hl-pos)
  9272. (message "TODO state changed to %s" (org-trim next)))
  9273. (unless head
  9274. (setq head (org-get-todo-sequence-head state)
  9275. ass (assoc head org-todo-kwd-alist)
  9276. interpret (nth 1 ass)
  9277. done-word (nth 3 ass)
  9278. final-done-word (nth 4 ass)))
  9279. (when (memq arg '(nextset previousset))
  9280. (message "Keyword-Set %d/%d: %s"
  9281. (- (length org-todo-sets) -1
  9282. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9283. (length org-todo-sets)
  9284. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9285. (setq org-last-todo-state-is-todo
  9286. (not (member state org-done-keywords)))
  9287. (setq now-done-p (and (member state org-done-keywords)
  9288. (not (member this org-done-keywords))))
  9289. (and logging (org-local-logging logging))
  9290. (when (and (or org-todo-log-states org-log-done)
  9291. (not (eq org-inhibit-logging t))
  9292. (not (memq arg '(nextset previousset))))
  9293. ;; we need to look at recording a time and note
  9294. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  9295. (nth 2 (assoc this org-todo-log-states))))
  9296. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  9297. (setq dolog 'time))
  9298. (when (and state
  9299. (member state org-not-done-keywords)
  9300. (not (member this org-not-done-keywords)))
  9301. ;; This is now a todo state and was not one before
  9302. ;; If there was a CLOSED time stamp, get rid of it.
  9303. (org-add-planning-info nil nil 'closed))
  9304. (when (and now-done-p org-log-done)
  9305. ;; It is now done, and it was not done before
  9306. (org-add-planning-info 'closed (org-current-time))
  9307. (if (and (not dolog) (eq 'note org-log-done))
  9308. (org-add-log-setup 'done state this 'findpos 'note)))
  9309. (when (and state dolog)
  9310. ;; This is a non-nil state, and we need to log it
  9311. (org-add-log-setup 'state state this 'findpos dolog)))
  9312. ;; Fixup tag positioning
  9313. (org-todo-trigger-tag-changes state)
  9314. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  9315. (when org-provide-todo-statistics
  9316. (org-update-parent-todo-statistics))
  9317. (run-hooks 'org-after-todo-state-change-hook)
  9318. (if (and arg (not (member state org-done-keywords)))
  9319. (setq head (org-get-todo-sequence-head state)))
  9320. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  9321. ;; Do we need to trigger a repeat?
  9322. (when now-done-p
  9323. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  9324. ;; This is for the agenda, take a snapshot of the headline.
  9325. (save-match-data
  9326. (setq org-agenda-headline-snapshot-before-repeat
  9327. (org-get-heading))))
  9328. (org-auto-repeat-maybe state))
  9329. ;; Fixup cursor location if close to the keyword
  9330. (if (and (outline-on-heading-p)
  9331. (not (bolp))
  9332. (save-excursion (beginning-of-line 1)
  9333. (looking-at org-todo-line-regexp))
  9334. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  9335. (progn
  9336. (goto-char (or (match-end 2) (match-end 1)))
  9337. (and (looking-at " ") (just-one-space))))
  9338. (when org-trigger-hook
  9339. (save-excursion
  9340. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  9341. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  9342. "Block turning an entry into a TODO, using the hierarchy.
  9343. This checks whether the current task should be blocked from state
  9344. changes. Such blocking occurs when:
  9345. 1. The task has children which are not all in a completed state.
  9346. 2. A task has a parent with the property :ORDERED:, and there
  9347. are siblings prior to the current task with incomplete
  9348. status.
  9349. 3. The parent of the task is blocked because it has siblings that should
  9350. be done first, or is child of a block grandparent TODO entry."
  9351. (if (not org-enforce-todo-dependencies)
  9352. t ; if locally turned off don't block
  9353. (catch 'dont-block
  9354. ;; If this is not a todo state change, or if this entry is already DONE,
  9355. ;; do not block
  9356. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9357. (member (plist-get change-plist :from)
  9358. (cons 'done org-done-keywords))
  9359. (member (plist-get change-plist :to)
  9360. (cons 'todo org-not-done-keywords))
  9361. (not (plist-get change-plist :to)))
  9362. (throw 'dont-block t))
  9363. ;; If this task has children, and any are undone, it's blocked
  9364. (save-excursion
  9365. (org-back-to-heading t)
  9366. (let ((this-level (funcall outline-level)))
  9367. (outline-next-heading)
  9368. (let ((child-level (funcall outline-level)))
  9369. (while (and (not (eobp))
  9370. (> child-level this-level))
  9371. ;; this todo has children, check whether they are all
  9372. ;; completed
  9373. (if (and (not (org-entry-is-done-p))
  9374. (org-entry-is-todo-p))
  9375. (throw 'dont-block nil))
  9376. (outline-next-heading)
  9377. (setq child-level (funcall outline-level))))))
  9378. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  9379. ;; any previous siblings are undone, it's blocked
  9380. (save-excursion
  9381. (org-back-to-heading t)
  9382. (let* ((pos (point))
  9383. (parent-pos (and (org-up-heading-safe) (point))))
  9384. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9385. (when (and (org-entry-get (point) "ORDERED")
  9386. (forward-line 1)
  9387. (re-search-forward org-not-done-heading-regexp pos t))
  9388. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  9389. ;; Search further up the hierarchy, to see if an anchestor is blocked
  9390. (while t
  9391. (goto-char parent-pos)
  9392. (if (not (looking-at org-not-done-heading-regexp))
  9393. (throw 'dont-block t)) ; do not block, parent is not a TODO
  9394. (setq pos (point))
  9395. (setq parent-pos (and (org-up-heading-safe) (point)))
  9396. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9397. (when (and (org-entry-get (point) "ORDERED")
  9398. (forward-line 1)
  9399. (re-search-forward org-not-done-heading-regexp pos t))
  9400. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  9401. (defcustom org-track-ordered-property-with-tag nil
  9402. "Should the ORDERED property also be shown as a tag?
  9403. The ORDERED property decides if an entry should require subtasks to be
  9404. completed in sequence. Since a property is not very visible, setting
  9405. this option means that toggling the ORDERED property with the command
  9406. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  9407. not relevant for the behavior, but it makes things more visible.
  9408. Note that toggling the tag with tags commands will not change the property
  9409. and therefore not influence behavior!
  9410. This can be t, meaning the tag ORDERED should be used, It can also be a
  9411. string to select a different tag for this task."
  9412. :group 'org-todo
  9413. :type '(choice
  9414. (const :tag "No tracking" nil)
  9415. (const :tag "Track with ORDERED tag" t)
  9416. (string :tag "Use other tag")))
  9417. (defun org-toggle-ordered-property ()
  9418. "Toggle the ORDERED property of the current entry.
  9419. For better visibility, you can track the value of this property with a tag.
  9420. See variable `org-track-ordered-property-with-tag'."
  9421. (interactive)
  9422. (let* ((t1 org-track-ordered-property-with-tag)
  9423. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  9424. (save-excursion
  9425. (org-back-to-heading)
  9426. (if (org-entry-get nil "ORDERED")
  9427. (progn
  9428. (org-delete-property "ORDERED")
  9429. (and tag (org-toggle-tag tag 'off))
  9430. (message "Subtasks can be completed in arbitrary order"))
  9431. (org-entry-put nil "ORDERED" "t")
  9432. (and tag (org-toggle-tag tag 'on))
  9433. (message "Subtasks must be completed in sequence")))))
  9434. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  9435. (defun org-block-todo-from-checkboxes (change-plist)
  9436. "Block turning an entry into a TODO, using checkboxes.
  9437. This checks whether the current task should be blocked from state
  9438. changes because there are unchecked boxes in this entry."
  9439. (if (not org-enforce-todo-checkbox-dependencies)
  9440. t ; if locally turned off don't block
  9441. (catch 'dont-block
  9442. ;; If this is not a todo state change, or if this entry is already DONE,
  9443. ;; do not block
  9444. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9445. (member (plist-get change-plist :from)
  9446. (cons 'done org-done-keywords))
  9447. (member (plist-get change-plist :to)
  9448. (cons 'todo org-not-done-keywords))
  9449. (not (plist-get change-plist :to)))
  9450. (throw 'dont-block t))
  9451. ;; If this task has checkboxes that are not checked, it's blocked
  9452. (save-excursion
  9453. (org-back-to-heading t)
  9454. (let ((beg (point)) end)
  9455. (outline-next-heading)
  9456. (setq end (point))
  9457. (goto-char beg)
  9458. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  9459. end t)
  9460. (progn
  9461. (if (boundp 'org-blocked-by-checkboxes)
  9462. (setq org-blocked-by-checkboxes t))
  9463. (throw 'dont-block nil)))))
  9464. t))) ; do not block
  9465. (defun org-entry-blocked-p ()
  9466. "Is the current entry blocked?"
  9467. (if (org-entry-get nil "NOBLOCKING")
  9468. nil ;; Never block this entry
  9469. (not
  9470. (run-hook-with-args-until-failure
  9471. 'org-blocker-hook
  9472. (list :type 'todo-state-change
  9473. :position (point)
  9474. :from 'todo
  9475. :to 'done)))))
  9476. (defun org-update-statistics-cookies (all)
  9477. "Update the statistics cookie, either from TODO or from checkboxes.
  9478. This should be called with the cursor in a line with a statistics cookie."
  9479. (interactive "P")
  9480. (if all
  9481. (progn
  9482. (org-update-checkbox-count 'all)
  9483. (org-map-entries 'org-update-parent-todo-statistics))
  9484. (if (not (org-on-heading-p))
  9485. (org-update-checkbox-count)
  9486. (let ((pos (move-marker (make-marker) (point)))
  9487. end l1 l2)
  9488. (ignore-errors (org-back-to-heading t))
  9489. (if (not (org-on-heading-p))
  9490. (org-update-checkbox-count)
  9491. (setq l1 (org-outline-level))
  9492. (setq end (save-excursion
  9493. (outline-next-heading)
  9494. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  9495. (point)))
  9496. (if (and (save-excursion
  9497. (re-search-forward
  9498. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  9499. (not (save-excursion (re-search-forward
  9500. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  9501. (org-update-checkbox-count)
  9502. (if (and l2 (> l2 l1))
  9503. (progn
  9504. (goto-char end)
  9505. (org-update-parent-todo-statistics))
  9506. (goto-char pos)
  9507. (beginning-of-line 1)
  9508. (while (re-search-forward
  9509. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  9510. (point-at-eol) t)
  9511. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  9512. (goto-char pos)
  9513. (move-marker pos nil)))))
  9514. (defvar org-entry-property-inherited-from) ;; defined below
  9515. (defun org-update-parent-todo-statistics ()
  9516. "Update any statistics cookie in the parent of the current headline.
  9517. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  9518. the entire subtree and this will travel up the hierarchy and update
  9519. statistics everywhere."
  9520. (interactive)
  9521. (let* ((lim 0) prop
  9522. (recursive (or (not org-hierarchical-todo-statistics)
  9523. (string-match
  9524. "\\<recursive\\>"
  9525. (or (setq prop (org-entry-get
  9526. nil "COOKIE_DATA" 'inherit)) ""))))
  9527. (lim (or (and prop (marker-position
  9528. org-entry-property-inherited-from))
  9529. lim))
  9530. (first t)
  9531. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  9532. level ltoggle l1 new ndel
  9533. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  9534. (catch 'exit
  9535. (save-excursion
  9536. (beginning-of-line 1)
  9537. (if (org-at-heading-p)
  9538. (setq ltoggle (funcall outline-level))
  9539. (error "This should not happen"))
  9540. (while (and (setq level (org-up-heading-safe))
  9541. (or recursive first)
  9542. (>= (point) lim))
  9543. (setq first nil cookie-present nil)
  9544. (unless (and level
  9545. (not (string-match
  9546. "\\<checkbox\\>"
  9547. (downcase
  9548. (or (org-entry-get
  9549. nil "COOKIE_DATA")
  9550. "")))))
  9551. (throw 'exit nil))
  9552. (while (re-search-forward box-re (point-at-eol) t)
  9553. (setq cnt-all 0 cnt-done 0 cookie-present t)
  9554. (setq is-percent (match-end 2))
  9555. (save-match-data
  9556. (unless (outline-next-heading) (throw 'exit nil))
  9557. (while (and (looking-at org-complex-heading-regexp)
  9558. (> (setq l1 (length (match-string 1))) level))
  9559. (setq kwd (and (or recursive (= l1 ltoggle))
  9560. (match-string 2)))
  9561. (if (or (eq org-provide-todo-statistics 'all-headlines)
  9562. (and (listp org-provide-todo-statistics)
  9563. (or (member kwd org-provide-todo-statistics)
  9564. (member kwd org-done-keywords))))
  9565. (setq cnt-all (1+ cnt-all))
  9566. (if (eq org-provide-todo-statistics t)
  9567. (and kwd (setq cnt-all (1+ cnt-all)))))
  9568. (and (member kwd org-done-keywords)
  9569. (setq cnt-done (1+ cnt-done)))
  9570. (outline-next-heading)))
  9571. (setq new
  9572. (if is-percent
  9573. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  9574. (format "[%d/%d]" cnt-done cnt-all))
  9575. ndel (- (match-end 0) (match-beginning 0)))
  9576. (goto-char (match-beginning 0))
  9577. (insert new)
  9578. (delete-region (point) (+ (point) ndel)))
  9579. (when cookie-present
  9580. (run-hook-with-args 'org-after-todo-statistics-hook
  9581. cnt-done (- cnt-all cnt-done))))))
  9582. (run-hooks 'org-todo-statistics-hook)))
  9583. (defvar org-after-todo-statistics-hook nil
  9584. "Hook that is called after a TODO statistics cookie has been updated.
  9585. Each function is called with two arguments: the number of not-done entries
  9586. and the number of done entries.
  9587. For example, the following function, when added to this hook, will switch
  9588. an entry to DONE when all children are done, and back to TODO when new
  9589. entries are set to a TODO status. Note that this hook is only called
  9590. when there is a statistics cookie in the headline!
  9591. (defun org-summary-todo (n-done n-not-done)
  9592. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  9593. (let (org-log-done org-log-states) ; turn off logging
  9594. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  9595. ")
  9596. (defvar org-todo-statistics-hook nil
  9597. "Hook that is run whenever Org thinks TODO statistics should be updated.
  9598. This hook runs even if there is no statistics cookie present, in which case
  9599. `org-after-todo-statistics-hook' would not run.")
  9600. (defun org-todo-trigger-tag-changes (state)
  9601. "Apply the changes defined in `org-todo-state-tags-triggers'."
  9602. (let ((l org-todo-state-tags-triggers)
  9603. changes)
  9604. (when (or (not state) (equal state ""))
  9605. (setq changes (append changes (cdr (assoc "" l)))))
  9606. (when (and (stringp state) (> (length state) 0))
  9607. (setq changes (append changes (cdr (assoc state l)))))
  9608. (when (member state org-not-done-keywords)
  9609. (setq changes (append changes (cdr (assoc 'todo l)))))
  9610. (when (member state org-done-keywords)
  9611. (setq changes (append changes (cdr (assoc 'done l)))))
  9612. (dolist (c changes)
  9613. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  9614. (defun org-local-logging (value)
  9615. "Get logging settings from a property VALUE."
  9616. (let* (words w a)
  9617. ;; directly set the variables, they are already local.
  9618. (setq org-log-done nil
  9619. org-log-repeat nil
  9620. org-todo-log-states nil)
  9621. (setq words (org-split-string value))
  9622. (while (setq w (pop words))
  9623. (cond
  9624. ((setq a (assoc w org-startup-options))
  9625. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  9626. (set (nth 1 a) (nth 2 a))))
  9627. ((setq a (org-extract-log-state-settings w))
  9628. (and (member (car a) org-todo-keywords-1)
  9629. (push a org-todo-log-states)))))))
  9630. (defun org-get-todo-sequence-head (kwd)
  9631. "Return the head of the TODO sequence to which KWD belongs.
  9632. If KWD is not set, check if there is a text property remembering the
  9633. right sequence."
  9634. (let (p)
  9635. (cond
  9636. ((not kwd)
  9637. (or (get-text-property (point-at-bol) 'org-todo-head)
  9638. (progn
  9639. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  9640. nil (point-at-eol)))
  9641. (get-text-property p 'org-todo-head))))
  9642. ((not (member kwd org-todo-keywords-1))
  9643. (car org-todo-keywords-1))
  9644. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  9645. (defun org-fast-todo-selection ()
  9646. "Fast TODO keyword selection with single keys.
  9647. Returns the new TODO keyword, or nil if no state change should occur."
  9648. (let* ((fulltable org-todo-key-alist)
  9649. (done-keywords org-done-keywords) ;; needed for the faces.
  9650. (maxlen (apply 'max (mapcar
  9651. (lambda (x)
  9652. (if (stringp (car x)) (string-width (car x)) 0))
  9653. fulltable)))
  9654. (expert nil)
  9655. (fwidth (+ maxlen 3 1 3))
  9656. (ncol (/ (- (window-width) 4) fwidth))
  9657. tg cnt e c tbl
  9658. groups ingroup)
  9659. (save-excursion
  9660. (save-window-excursion
  9661. (if expert
  9662. (set-buffer (get-buffer-create " *Org todo*"))
  9663. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  9664. (erase-buffer)
  9665. (org-set-local 'org-done-keywords done-keywords)
  9666. (setq tbl fulltable cnt 0)
  9667. (while (setq e (pop tbl))
  9668. (cond
  9669. ((equal e '(:startgroup))
  9670. (push '() groups) (setq ingroup t)
  9671. (when (not (= cnt 0))
  9672. (setq cnt 0)
  9673. (insert "\n"))
  9674. (insert "{ "))
  9675. ((equal e '(:endgroup))
  9676. (setq ingroup nil cnt 0)
  9677. (insert "}\n"))
  9678. ((equal e '(:newline))
  9679. (when (not (= cnt 0))
  9680. (setq cnt 0)
  9681. (insert "\n")
  9682. (setq e (car tbl))
  9683. (while (equal (car tbl) '(:newline))
  9684. (insert "\n")
  9685. (setq tbl (cdr tbl)))))
  9686. (t
  9687. (setq tg (car e) c (cdr e))
  9688. (if ingroup (push tg (car groups)))
  9689. (setq tg (org-add-props tg nil 'face
  9690. (org-get-todo-face tg)))
  9691. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9692. (insert "[" c "] " tg (make-string
  9693. (- fwidth 4 (length tg)) ?\ ))
  9694. (when (= (setq cnt (1+ cnt)) ncol)
  9695. (insert "\n")
  9696. (if ingroup (insert " "))
  9697. (setq cnt 0)))))
  9698. (insert "\n")
  9699. (goto-char (point-min))
  9700. (if (not expert) (org-fit-window-to-buffer))
  9701. (message "[a-z..]:Set [SPC]:clear")
  9702. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9703. (cond
  9704. ((or (= c ?\C-g)
  9705. (and (= c ?q) (not (rassoc c fulltable))))
  9706. (setq quit-flag t))
  9707. ((= c ?\ ) nil)
  9708. ((setq e (rassoc c fulltable) tg (car e))
  9709. tg)
  9710. (t (setq quit-flag t)))))))
  9711. (defun org-entry-is-todo-p ()
  9712. (member (org-get-todo-state) org-not-done-keywords))
  9713. (defun org-entry-is-done-p ()
  9714. (member (org-get-todo-state) org-done-keywords))
  9715. (defun org-get-todo-state ()
  9716. (save-excursion
  9717. (org-back-to-heading t)
  9718. (and (looking-at org-todo-line-regexp)
  9719. (match-end 2)
  9720. (match-string 2))))
  9721. (defun org-at-date-range-p (&optional inactive-ok)
  9722. "Is the cursor inside a date range?"
  9723. (interactive)
  9724. (save-excursion
  9725. (catch 'exit
  9726. (let ((pos (point)))
  9727. (skip-chars-backward "^[<\r\n")
  9728. (skip-chars-backward "<[")
  9729. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9730. (>= (match-end 0) pos)
  9731. (throw 'exit t))
  9732. (skip-chars-backward "^<[\r\n")
  9733. (skip-chars-backward "<[")
  9734. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9735. (>= (match-end 0) pos)
  9736. (throw 'exit t)))
  9737. nil)))
  9738. (defun org-get-repeat (&optional tagline)
  9739. "Check if there is a deadline/schedule with repeater in this entry."
  9740. (save-match-data
  9741. (save-excursion
  9742. (org-back-to-heading t)
  9743. (and (re-search-forward (if tagline
  9744. (concat tagline "\\s-*" org-repeat-re)
  9745. org-repeat-re)
  9746. (org-entry-end-position) t)
  9747. (match-string-no-properties 1)))))
  9748. (defvar org-last-changed-timestamp)
  9749. (defvar org-last-inserted-timestamp)
  9750. (defvar org-log-post-message)
  9751. (defvar org-log-note-purpose)
  9752. (defvar org-log-note-how)
  9753. (defvar org-log-note-extra)
  9754. (defun org-auto-repeat-maybe (done-word)
  9755. "Check if the current headline contains a repeated deadline/schedule.
  9756. If yes, set TODO state back to what it was and change the base date
  9757. of repeating deadline/scheduled time stamps to new date.
  9758. This function is run automatically after each state change to a DONE state."
  9759. ;; last-state is dynamically scoped into this function
  9760. (let* ((repeat (org-get-repeat))
  9761. (aa (assoc last-state org-todo-kwd-alist))
  9762. (interpret (nth 1 aa))
  9763. (head (nth 2 aa))
  9764. (whata '(("d" . day) ("m" . month) ("y" . year)))
  9765. (msg "Entry repeats: ")
  9766. (org-log-done nil)
  9767. (org-todo-log-states nil)
  9768. (nshiftmax 10) (nshift 0)
  9769. re type n what ts time)
  9770. (when repeat
  9771. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  9772. (org-todo (if (eq interpret 'type) last-state head))
  9773. (org-entry-put nil "LAST_REPEAT" (format-time-string
  9774. (org-time-stamp-format t t)))
  9775. (when org-log-repeat
  9776. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9777. (memq 'org-add-log-note post-command-hook))
  9778. ;; OK, we are already setup for some record
  9779. (if (eq org-log-repeat 'note)
  9780. ;; make sure we take a note, not only a time stamp
  9781. (setq org-log-note-how 'note))
  9782. ;; Set up for taking a record
  9783. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  9784. last-state
  9785. 'findpos org-log-repeat)))
  9786. (org-back-to-heading t)
  9787. (org-add-planning-info nil nil 'closed)
  9788. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  9789. org-deadline-time-regexp "\\)\\|\\("
  9790. org-ts-regexp "\\)"))
  9791. (while (re-search-forward
  9792. re (save-excursion (outline-next-heading) (point)) t)
  9793. (setq type (if (match-end 1) org-scheduled-string
  9794. (if (match-end 3) org-deadline-string "Plain:"))
  9795. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9796. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9797. (setq n (string-to-number (match-string 2 ts))
  9798. what (match-string 3 ts))
  9799. (if (equal what "w") (setq n (* n 7) what "d"))
  9800. ;; Preparation, see if we need to modify the start date for the change
  9801. (when (match-end 1)
  9802. (setq time (save-match-data (org-time-string-to-time ts)))
  9803. (cond
  9804. ((equal (match-string 1 ts) ".")
  9805. ;; Shift starting date to today
  9806. (org-timestamp-change
  9807. (- (time-to-days (current-time)) (time-to-days time))
  9808. 'day))
  9809. ((equal (match-string 1 ts) "+")
  9810. (while (or (= nshift 0)
  9811. (<= (time-to-days time) (time-to-days (current-time))))
  9812. (when (= (incf nshift) nshiftmax)
  9813. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9814. (error "Abort")))
  9815. (org-timestamp-change n (cdr (assoc what whata)))
  9816. (org-at-timestamp-p t)
  9817. (setq ts (match-string 1))
  9818. (setq time (save-match-data (org-time-string-to-time ts))))
  9819. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9820. ;; rematch, so that we have everything in place for the real shift
  9821. (org-at-timestamp-p t)
  9822. (setq ts (match-string 1))
  9823. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9824. (org-timestamp-change n (cdr (assoc what whata)))
  9825. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9826. (setq org-log-post-message msg)
  9827. (message "%s" msg))))
  9828. (defun org-show-todo-tree (arg)
  9829. "Make a compact tree which shows all headlines marked with TODO.
  9830. The tree will show the lines where the regexp matches, and all higher
  9831. headlines above the match.
  9832. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9833. With a numeric prefix N, construct a sparse tree for the Nth element
  9834. of `org-todo-keywords-1'."
  9835. (interactive "P")
  9836. (let ((case-fold-search nil)
  9837. (kwd-re
  9838. (cond ((null arg) org-not-done-regexp)
  9839. ((equal arg '(4))
  9840. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  9841. (mapcar 'list org-todo-keywords-1))))
  9842. (concat "\\("
  9843. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9844. "\\)\\>")))
  9845. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9846. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9847. org-todo-keywords-1)))
  9848. (t (error "Invalid prefix argument: %s" arg)))))
  9849. (message "%d TODO entries found"
  9850. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9851. (defun org-deadline (&optional remove time)
  9852. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9853. With argument REMOVE, remove any deadline from the item.
  9854. When TIME is set, it should be an internal time specification, and the
  9855. scheduling will use the corresponding date."
  9856. (interactive "P")
  9857. (let* ((old-date (org-entry-get nil "DEADLINE"))
  9858. (repeater (and old-date
  9859. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  9860. (match-string 1 old-date))))
  9861. (if remove
  9862. (progn
  9863. (when (and old-date org-log-redeadline)
  9864. (org-add-log-setup 'deldeadline nil old-date 'findpos
  9865. org-log-redeadline))
  9866. (org-remove-timestamp-with-keyword org-deadline-string)
  9867. (message "Item no longer has a deadline."))
  9868. (org-add-planning-info 'deadline time 'closed)
  9869. (when (and old-date org-log-redeadline
  9870. (not (equal old-date
  9871. (substring org-last-inserted-timestamp 1 -1))))
  9872. (org-add-log-setup 'redeadline nil old-date 'findpos
  9873. org-log-redeadline))
  9874. (when repeater
  9875. (save-excursion
  9876. (org-back-to-heading t)
  9877. (when (re-search-forward (concat org-deadline-string " "
  9878. org-last-inserted-timestamp)
  9879. (save-excursion
  9880. (outline-next-heading) (point)) t)
  9881. (goto-char (1- (match-end 0)))
  9882. (insert " " repeater)
  9883. (setq org-last-inserted-timestamp
  9884. (concat (substring org-last-inserted-timestamp 0 -1)
  9885. " " repeater
  9886. (substring org-last-inserted-timestamp -1))))))
  9887. (message "Deadline on %s" org-last-inserted-timestamp))))
  9888. (defun org-schedule (&optional remove time)
  9889. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9890. With argument REMOVE, remove any scheduling date from the item.
  9891. When TIME is set, it should be an internal time specification, and the
  9892. scheduling will use the corresponding date."
  9893. (interactive "P")
  9894. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  9895. (repeater (and old-date
  9896. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  9897. (match-string 1 old-date))))
  9898. (if remove
  9899. (progn
  9900. (when (and old-date org-log-reschedule)
  9901. (org-add-log-setup 'delschedule nil old-date 'findpos
  9902. org-log-reschedule))
  9903. (org-remove-timestamp-with-keyword org-scheduled-string)
  9904. (message "Item is no longer scheduled."))
  9905. (org-add-planning-info 'scheduled time 'closed)
  9906. (when (and old-date org-log-reschedule
  9907. (not (equal old-date
  9908. (substring org-last-inserted-timestamp 1 -1))))
  9909. (org-add-log-setup 'reschedule nil old-date 'findpos
  9910. org-log-reschedule))
  9911. (when repeater
  9912. (save-excursion
  9913. (org-back-to-heading t)
  9914. (when (re-search-forward (concat org-scheduled-string " "
  9915. org-last-inserted-timestamp)
  9916. (save-excursion
  9917. (outline-next-heading) (point)) t)
  9918. (goto-char (1- (match-end 0)))
  9919. (insert " " repeater)
  9920. (setq org-last-inserted-timestamp
  9921. (concat (substring org-last-inserted-timestamp 0 -1)
  9922. " " repeater
  9923. (substring org-last-inserted-timestamp -1))))))
  9924. (message "Scheduled to %s" org-last-inserted-timestamp))))
  9925. (defun org-get-scheduled-time (pom &optional inherit)
  9926. "Get the scheduled time as a time tuple, of a format suitable
  9927. for calling org-schedule with, or if there is no scheduling,
  9928. returns nil."
  9929. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  9930. (when time
  9931. (apply 'encode-time (org-parse-time-string time)))))
  9932. (defun org-get-deadline-time (pom &optional inherit)
  9933. "Get the deadine as a time tuple, of a format suitable for
  9934. calling org-deadline with, or if there is no scheduling, returns
  9935. nil."
  9936. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  9937. (when time
  9938. (apply 'encode-time (org-parse-time-string time)))))
  9939. (defun org-remove-timestamp-with-keyword (keyword)
  9940. "Remove all time stamps with KEYWORD in the current entry."
  9941. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  9942. beg)
  9943. (save-excursion
  9944. (org-back-to-heading t)
  9945. (setq beg (point))
  9946. (outline-next-heading)
  9947. (while (re-search-backward re beg t)
  9948. (replace-match "")
  9949. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  9950. (equal (char-before) ?\ ))
  9951. (backward-delete-char 1)
  9952. (if (string-match "^[ \t]*$" (buffer-substring
  9953. (point-at-bol) (point-at-eol)))
  9954. (delete-region (point-at-bol)
  9955. (min (point-max) (1+ (point-at-eol))))))))))
  9956. (defun org-add-planning-info (what &optional time &rest remove)
  9957. "Insert new timestamp with keyword in the line directly after the headline.
  9958. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  9959. If non is given, the user is prompted for a date.
  9960. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  9961. be removed."
  9962. (interactive)
  9963. (let (org-time-was-given org-end-time-was-given ts
  9964. end default-time default-input)
  9965. (catch 'exit
  9966. (when (and (not time) (memq what '(scheduled deadline)))
  9967. ;; Try to get a default date/time from existing timestamp
  9968. (save-excursion
  9969. (org-back-to-heading t)
  9970. (setq end (save-excursion (outline-next-heading) (point)))
  9971. (when (re-search-forward (if (eq what 'scheduled)
  9972. org-scheduled-time-regexp
  9973. org-deadline-time-regexp)
  9974. end t)
  9975. (setq ts (match-string 1)
  9976. default-time
  9977. (apply 'encode-time (org-parse-time-string ts))
  9978. default-input (and ts (org-get-compact-tod ts))))))
  9979. (when what
  9980. ;; If necessary, get the time from the user
  9981. (setq time (or time (org-read-date nil 'to-time nil nil
  9982. default-time default-input))))
  9983. (when (and org-insert-labeled-timestamps-at-point
  9984. (member what '(scheduled deadline)))
  9985. (insert
  9986. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  9987. (org-insert-time-stamp time org-time-was-given
  9988. nil nil nil (list org-end-time-was-given))
  9989. (setq what nil))
  9990. (save-excursion
  9991. (save-restriction
  9992. (let (col list elt ts buffer-invisibility-spec)
  9993. (org-back-to-heading t)
  9994. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  9995. (goto-char (match-end 1))
  9996. (setq col (current-column))
  9997. (goto-char (match-end 0))
  9998. (if (eobp) (insert "\n") (forward-char 1))
  9999. (when (and (not what)
  10000. (not (looking-at
  10001. (concat "[ \t]*"
  10002. org-keyword-time-not-clock-regexp))))
  10003. ;; Nothing to add, nothing to remove...... :-)
  10004. (throw 'exit nil))
  10005. (if (and (not (looking-at outline-regexp))
  10006. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10007. "[^\r\n]*"))
  10008. (not (equal (match-string 1) org-clock-string)))
  10009. (narrow-to-region (match-beginning 0) (match-end 0))
  10010. (insert-before-markers "\n")
  10011. (backward-char 1)
  10012. (narrow-to-region (point) (point))
  10013. (and org-adapt-indentation (org-indent-to-column col)))
  10014. ;; Check if we have to remove something.
  10015. (setq list (cons what remove))
  10016. (while list
  10017. (setq elt (pop list))
  10018. (goto-char (point-min))
  10019. (when (or (and (eq elt 'scheduled)
  10020. (re-search-forward org-scheduled-time-regexp nil t))
  10021. (and (eq elt 'deadline)
  10022. (re-search-forward org-deadline-time-regexp nil t))
  10023. (and (eq elt 'closed)
  10024. (re-search-forward org-closed-time-regexp nil t)))
  10025. (replace-match "")
  10026. (if (looking-at "--+<[^>]+>") (replace-match ""))
  10027. (skip-chars-backward " ")
  10028. (if (looking-at " +") (replace-match ""))))
  10029. (goto-char (point-max))
  10030. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10031. (when what
  10032. (insert
  10033. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10034. (cond ((eq what 'scheduled) org-scheduled-string)
  10035. ((eq what 'deadline) org-deadline-string)
  10036. ((eq what 'closed) org-closed-string))
  10037. " ")
  10038. (setq ts (org-insert-time-stamp
  10039. time
  10040. (or org-time-was-given
  10041. (and (eq what 'closed) org-log-done-with-time))
  10042. (eq what 'closed)
  10043. nil nil (list org-end-time-was-given)))
  10044. (end-of-line 1))
  10045. (goto-char (point-min))
  10046. (widen)
  10047. (if (and (looking-at "[ \t]+\n")
  10048. (equal (char-before) ?\n))
  10049. (delete-region (1- (point)) (point-at-eol)))
  10050. ts))))))
  10051. (defvar org-log-note-marker (make-marker))
  10052. (defvar org-log-note-purpose nil)
  10053. (defvar org-log-note-state nil)
  10054. (defvar org-log-note-previous-state nil)
  10055. (defvar org-log-note-how nil)
  10056. (defvar org-log-note-extra nil)
  10057. (defvar org-log-note-window-configuration nil)
  10058. (defvar org-log-note-return-to (make-marker))
  10059. (defvar org-log-post-message nil
  10060. "Message to be displayed after a log note has been stored.
  10061. The auto-repeater uses this.")
  10062. (defun org-add-note ()
  10063. "Add a note to the current entry.
  10064. This is done in the same way as adding a state change note."
  10065. (interactive)
  10066. (org-add-log-setup 'note nil nil 'findpos nil))
  10067. (defvar org-property-end-re)
  10068. (defun org-add-log-setup (&optional purpose state prev-state
  10069. findpos how &optional extra)
  10070. "Set up the post command hook to take a note.
  10071. If this is about to TODO state change, the new state is expected in STATE.
  10072. When FINDPOS is non-nil, find the correct position for the note in
  10073. the current entry. If not, assume that it can be inserted at point.
  10074. HOW is an indicator what kind of note should be created.
  10075. EXTRA is additional text that will be inserted into the notes buffer."
  10076. (let* ((org-log-into-drawer (org-log-into-drawer))
  10077. (drawer (cond ((stringp org-log-into-drawer)
  10078. org-log-into-drawer)
  10079. (org-log-into-drawer "LOGBOOK")
  10080. (t nil))))
  10081. (save-restriction
  10082. (save-excursion
  10083. (when findpos
  10084. (org-back-to-heading t)
  10085. (narrow-to-region (point) (save-excursion
  10086. (outline-next-heading) (point)))
  10087. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  10088. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10089. "[^\r\n]*\\)?"))
  10090. (goto-char (match-end 0))
  10091. (cond
  10092. (drawer
  10093. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10094. nil t)
  10095. (progn
  10096. (goto-char (match-end 0))
  10097. (or org-log-states-order-reversed
  10098. (and (re-search-forward org-property-end-re nil t)
  10099. (goto-char (1- (match-beginning 0))))))
  10100. (insert "\n:" drawer ":\n:END:")
  10101. (beginning-of-line 0)
  10102. (org-indent-line-function)
  10103. (beginning-of-line 2)
  10104. (org-indent-line-function)
  10105. (end-of-line 0)))
  10106. ((and org-log-state-notes-insert-after-drawers
  10107. (save-excursion
  10108. (forward-line) (looking-at org-drawer-regexp)))
  10109. (forward-line)
  10110. (while (looking-at org-drawer-regexp)
  10111. (goto-char (match-end 0))
  10112. (re-search-forward org-property-end-re (point-max) t)
  10113. (forward-line))
  10114. (forward-line -1)))
  10115. (unless org-log-states-order-reversed
  10116. (and (= (char-after) ?\n) (forward-char 1))
  10117. (org-skip-over-state-notes)
  10118. (skip-chars-backward " \t\n\r")))
  10119. (move-marker org-log-note-marker (point))
  10120. (setq org-log-note-purpose purpose
  10121. org-log-note-state state
  10122. org-log-note-previous-state prev-state
  10123. org-log-note-how how
  10124. org-log-note-extra extra)
  10125. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10126. (defun org-skip-over-state-notes ()
  10127. "Skip past the list of State notes in an entry."
  10128. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10129. (while (looking-at "[ \t]*- State")
  10130. (condition-case nil
  10131. (org-next-item)
  10132. (error (org-end-of-item)))))
  10133. (defun org-add-log-note (&optional purpose)
  10134. "Pop up a window for taking a note, and add this note later at point."
  10135. (remove-hook 'post-command-hook 'org-add-log-note)
  10136. (setq org-log-note-window-configuration (current-window-configuration))
  10137. (delete-other-windows)
  10138. (move-marker org-log-note-return-to (point))
  10139. (switch-to-buffer (marker-buffer org-log-note-marker))
  10140. (goto-char org-log-note-marker)
  10141. (org-switch-to-buffer-other-window "*Org Note*")
  10142. (erase-buffer)
  10143. (if (memq org-log-note-how '(time state))
  10144. (let (current-prefix-arg) (org-store-log-note))
  10145. (let ((org-inhibit-startup t)) (org-mode))
  10146. (insert (format "# Insert note for %s.
  10147. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  10148. (cond
  10149. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  10150. ((eq org-log-note-purpose 'done) "closed todo item")
  10151. ((eq org-log-note-purpose 'state)
  10152. (format "state change from \"%s\" to \"%s\""
  10153. (or org-log-note-previous-state "")
  10154. (or org-log-note-state "")))
  10155. ((eq org-log-note-purpose 'reschedule)
  10156. "rescheduling")
  10157. ((eq org-log-note-purpose 'delschedule)
  10158. "no longer scheduled")
  10159. ((eq org-log-note-purpose 'redeadline)
  10160. "changing deadline")
  10161. ((eq org-log-note-purpose 'deldeadline)
  10162. "removing deadline")
  10163. ((eq org-log-note-purpose 'refile)
  10164. "refiling")
  10165. ((eq org-log-note-purpose 'note)
  10166. "this entry")
  10167. (t (error "This should not happen")))))
  10168. (if org-log-note-extra (insert org-log-note-extra))
  10169. (org-set-local 'org-finish-function 'org-store-log-note)))
  10170. (defvar org-note-abort nil) ; dynamically scoped
  10171. (defun org-store-log-note ()
  10172. "Finish taking a log note, and insert it to where it belongs."
  10173. (let ((txt (buffer-string))
  10174. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  10175. lines ind)
  10176. (kill-buffer (current-buffer))
  10177. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  10178. (setq txt (replace-match "" t t txt)))
  10179. (if (string-match "\\s-+\\'" txt)
  10180. (setq txt (replace-match "" t t txt)))
  10181. (setq lines (org-split-string txt "\n"))
  10182. (when (and note (string-match "\\S-" note))
  10183. (setq note
  10184. (org-replace-escapes
  10185. note
  10186. (list (cons "%u" (user-login-name))
  10187. (cons "%U" user-full-name)
  10188. (cons "%t" (format-time-string
  10189. (org-time-stamp-format 'long 'inactive)
  10190. (current-time)))
  10191. (cons "%s" (if org-log-note-state
  10192. (concat "\"" org-log-note-state "\"")
  10193. ""))
  10194. (cons "%S" (if org-log-note-previous-state
  10195. (concat "\"" org-log-note-previous-state "\"")
  10196. "\"\"")))))
  10197. (if lines (setq note (concat note " \\\\")))
  10198. (push note lines))
  10199. (when (or current-prefix-arg org-note-abort)
  10200. (when org-log-into-drawer
  10201. (org-remove-empty-drawer-at
  10202. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10203. org-log-note-marker))
  10204. (setq lines nil))
  10205. (when lines
  10206. (with-current-buffer (marker-buffer org-log-note-marker)
  10207. (save-excursion
  10208. (goto-char org-log-note-marker)
  10209. (move-marker org-log-note-marker nil)
  10210. (end-of-line 1)
  10211. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10212. (insert "- " (pop lines))
  10213. (org-indent-line-function)
  10214. (beginning-of-line 1)
  10215. (looking-at "[ \t]*")
  10216. (setq ind (concat (match-string 0) " "))
  10217. (end-of-line 1)
  10218. (while lines (insert "\n" ind (pop lines)))
  10219. (message "Note stored")
  10220. (org-back-to-heading t)
  10221. (org-cycle-hide-drawers 'children)))))
  10222. (set-window-configuration org-log-note-window-configuration)
  10223. (with-current-buffer (marker-buffer org-log-note-return-to)
  10224. (goto-char org-log-note-return-to))
  10225. (move-marker org-log-note-return-to nil)
  10226. (and org-log-post-message (message "%s" org-log-post-message)))
  10227. (defun org-remove-empty-drawer-at (drawer pos)
  10228. "Remove an empty drawer DRAWER at position POS.
  10229. POS may also be a marker."
  10230. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10231. (save-excursion
  10232. (save-restriction
  10233. (widen)
  10234. (goto-char pos)
  10235. (if (org-in-regexp
  10236. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10237. (replace-match ""))))))
  10238. (defun org-sparse-tree (&optional arg)
  10239. "Create a sparse tree, prompt for the details.
  10240. This command can create sparse trees. You first need to select the type
  10241. of match used to create the tree:
  10242. t Show entries with a specific TODO keyword.
  10243. m Show entries selected by a tags/property match.
  10244. p Enter a property name and its value (both with completion on existing
  10245. names/values) and show entries with that property.
  10246. / Show entries matching a regular expression (`r' can be used as well)
  10247. d Show deadlines due within `org-deadline-warning-days'.
  10248. b Show deadlines and scheduled items before a date.
  10249. a Show deadlines and scheduled items after a date."
  10250. (interactive "P")
  10251. (let (ans kwd value)
  10252. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  10253. (setq ans (read-char-exclusive))
  10254. (cond
  10255. ((equal ans ?d)
  10256. (call-interactively 'org-check-deadlines))
  10257. ((equal ans ?b)
  10258. (call-interactively 'org-check-before-date))
  10259. ((equal ans ?a)
  10260. (call-interactively 'org-check-after-date))
  10261. ((equal ans ?t)
  10262. (org-show-todo-tree '(4)))
  10263. ((member ans '(?T ?m))
  10264. (call-interactively 'org-match-sparse-tree))
  10265. ((member ans '(?p ?P))
  10266. (setq kwd (org-icompleting-read "Property: "
  10267. (mapcar 'list (org-buffer-property-keys))))
  10268. (setq value (org-icompleting-read "Value: "
  10269. (mapcar 'list (org-property-values kwd))))
  10270. (unless (string-match "\\`{.*}\\'" value)
  10271. (setq value (concat "\"" value "\"")))
  10272. (org-match-sparse-tree arg (concat kwd "=" value)))
  10273. ((member ans '(?r ?R ?/))
  10274. (call-interactively 'org-occur))
  10275. (t (error "No such sparse tree command \"%c\"" ans)))))
  10276. (defvar org-occur-highlights nil
  10277. "List of overlays used for occur matches.")
  10278. (make-variable-buffer-local 'org-occur-highlights)
  10279. (defvar org-occur-parameters nil
  10280. "Parameters of the active org-occur calls.
  10281. This is a list, each call to org-occur pushes as cons cell,
  10282. containing the regular expression and the callback, onto the list.
  10283. The list can contain several entries if `org-occur' has been called
  10284. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  10285. will only contain one set of parameters. When the highlights are
  10286. removed (for example with `C-c C-c', or with the next edit (depending
  10287. on `org-remove-highlights-with-change'), this variable is emptied
  10288. as well.")
  10289. (make-variable-buffer-local 'org-occur-parameters)
  10290. (defun org-occur (regexp &optional keep-previous callback)
  10291. "Make a compact tree which shows all matches of REGEXP.
  10292. The tree will show the lines where the regexp matches, and all higher
  10293. headlines above the match. It will also show the heading after the match,
  10294. to make sure editing the matching entry is easy.
  10295. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  10296. call to `org-occur' will be kept, to allow stacking of calls to this
  10297. command.
  10298. If CALLBACK is non-nil, it is a function which is called to confirm
  10299. that the match should indeed be shown."
  10300. (interactive "sRegexp: \nP")
  10301. (when (equal regexp "")
  10302. (error "Regexp cannot be empty"))
  10303. (unless keep-previous
  10304. (org-remove-occur-highlights nil nil t))
  10305. (push (cons regexp callback) org-occur-parameters)
  10306. (let ((cnt 0))
  10307. (save-excursion
  10308. (goto-char (point-min))
  10309. (if (or (not keep-previous) ; do not want to keep
  10310. (not org-occur-highlights)) ; no previous matches
  10311. ;; hide everything
  10312. (org-overview))
  10313. (while (re-search-forward regexp nil t)
  10314. (when (or (not callback)
  10315. (save-match-data (funcall callback)))
  10316. (setq cnt (1+ cnt))
  10317. (when org-highlight-sparse-tree-matches
  10318. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  10319. (org-show-context 'occur-tree))))
  10320. (when org-remove-highlights-with-change
  10321. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  10322. nil 'local))
  10323. (unless org-sparse-tree-open-archived-trees
  10324. (org-hide-archived-subtrees (point-min) (point-max)))
  10325. (run-hooks 'org-occur-hook)
  10326. (if (interactive-p)
  10327. (message "%d match(es) for regexp %s" cnt regexp))
  10328. cnt))
  10329. (defun org-show-context (&optional key)
  10330. "Make sure point and context and visible.
  10331. How much context is shown depends upon the variables
  10332. `org-show-hierarchy-above', `org-show-following-heading'. and
  10333. `org-show-siblings'."
  10334. (let ((heading-p (org-on-heading-p t))
  10335. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  10336. (following-p (org-get-alist-option org-show-following-heading key))
  10337. (entry-p (org-get-alist-option org-show-entry-below key))
  10338. (siblings-p (org-get-alist-option org-show-siblings key)))
  10339. (catch 'exit
  10340. ;; Show heading or entry text
  10341. (if (and heading-p (not entry-p))
  10342. (org-flag-heading nil) ; only show the heading
  10343. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  10344. (org-show-hidden-entry))) ; show entire entry
  10345. (when following-p
  10346. ;; Show next sibling, or heading below text
  10347. (save-excursion
  10348. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  10349. (org-flag-heading nil))))
  10350. (when siblings-p (org-show-siblings))
  10351. (when hierarchy-p
  10352. ;; show all higher headings, possibly with siblings
  10353. (save-excursion
  10354. (while (and (condition-case nil
  10355. (progn (org-up-heading-all 1) t)
  10356. (error nil))
  10357. (not (bobp)))
  10358. (org-flag-heading nil)
  10359. (when siblings-p (org-show-siblings))))))))
  10360. (defvar org-reveal-start-hook nil
  10361. "Hook run before revealing a location.")
  10362. (defun org-reveal (&optional siblings)
  10363. "Show current entry, hierarchy above it, and the following headline.
  10364. This can be used to show a consistent set of context around locations
  10365. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  10366. not t for the search context.
  10367. With optional argument SIBLINGS, on each level of the hierarchy all
  10368. siblings are shown. This repairs the tree structure to what it would
  10369. look like when opened with hierarchical calls to `org-cycle'.
  10370. With double optional argument `C-u C-u', go to the parent and show the
  10371. entire tree."
  10372. (interactive "P")
  10373. (run-hooks 'org-reveal-start-hook)
  10374. (let ((org-show-hierarchy-above t)
  10375. (org-show-following-heading t)
  10376. (org-show-siblings (if siblings t org-show-siblings)))
  10377. (org-show-context nil))
  10378. (when (equal siblings '(16))
  10379. (save-excursion
  10380. (when (org-up-heading-safe)
  10381. (org-show-subtree)
  10382. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  10383. (defun org-highlight-new-match (beg end)
  10384. "Highlight from BEG to END and mark the highlight is an occur headline."
  10385. (let ((ov (org-make-overlay beg end)))
  10386. (org-overlay-put ov 'face 'secondary-selection)
  10387. (push ov org-occur-highlights)))
  10388. (defun org-remove-occur-highlights (&optional beg end noremove)
  10389. "Remove the occur highlights from the buffer.
  10390. BEG and END are ignored. If NOREMOVE is nil, remove this function
  10391. from the `before-change-functions' in the current buffer."
  10392. (interactive)
  10393. (unless org-inhibit-highlight-removal
  10394. (mapc 'org-delete-overlay org-occur-highlights)
  10395. (setq org-occur-highlights nil)
  10396. (setq org-occur-parameters nil)
  10397. (unless noremove
  10398. (remove-hook 'before-change-functions
  10399. 'org-remove-occur-highlights 'local))))
  10400. ;;;; Priorities
  10401. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  10402. "Regular expression matching the priority indicator.")
  10403. (defvar org-remove-priority-next-time nil)
  10404. (defun org-priority-up ()
  10405. "Increase the priority of the current item."
  10406. (interactive)
  10407. (org-priority 'up))
  10408. (defun org-priority-down ()
  10409. "Decrease the priority of the current item."
  10410. (interactive)
  10411. (org-priority 'down))
  10412. (defun org-priority (&optional action)
  10413. "Change the priority of an item by ARG.
  10414. ACTION can be `set', `up', `down', or a character."
  10415. (interactive)
  10416. (unless org-enable-priority-commands
  10417. (error "Priority commands are disabled"))
  10418. (setq action (or action 'set))
  10419. (let (current new news have remove)
  10420. (save-excursion
  10421. (org-back-to-heading t)
  10422. (if (looking-at org-priority-regexp)
  10423. (setq current (string-to-char (match-string 2))
  10424. have t)
  10425. (setq current org-default-priority))
  10426. (cond
  10427. ((eq action 'remove)
  10428. (setq remove t new ?\ ))
  10429. ((or (eq action 'set)
  10430. (if (featurep 'xemacs) (characterp action) (integerp action)))
  10431. (if (not (eq action 'set))
  10432. (setq new action)
  10433. (message "Priority %c-%c, SPC to remove: "
  10434. org-highest-priority org-lowest-priority)
  10435. (setq new (read-char-exclusive)))
  10436. (if (and (= (upcase org-highest-priority) org-highest-priority)
  10437. (= (upcase org-lowest-priority) org-lowest-priority))
  10438. (setq new (upcase new)))
  10439. (cond ((equal new ?\ ) (setq remove t))
  10440. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  10441. (error "Priority must be between `%c' and `%c'"
  10442. org-highest-priority org-lowest-priority))))
  10443. ((eq action 'up)
  10444. (if (and (not have) (eq last-command this-command))
  10445. (setq new org-lowest-priority)
  10446. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10447. org-default-priority (1- current)))))
  10448. ((eq action 'down)
  10449. (if (and (not have) (eq last-command this-command))
  10450. (setq new org-highest-priority)
  10451. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10452. org-default-priority (1+ current)))))
  10453. (t (error "Invalid action")))
  10454. (if (or (< (upcase new) org-highest-priority)
  10455. (> (upcase new) org-lowest-priority))
  10456. (setq remove t))
  10457. (setq news (format "%c" new))
  10458. (if have
  10459. (if remove
  10460. (replace-match "" t t nil 1)
  10461. (replace-match news t t nil 2))
  10462. (if remove
  10463. (error "No priority cookie found in line")
  10464. (let ((case-fold-search nil))
  10465. (looking-at org-todo-line-regexp))
  10466. (if (match-end 2)
  10467. (progn
  10468. (goto-char (match-end 2))
  10469. (insert " [#" news "]"))
  10470. (goto-char (match-beginning 3))
  10471. (insert "[#" news "] "))))
  10472. (org-preserve-lc (org-set-tags nil 'align)))
  10473. (if remove
  10474. (message "Priority removed")
  10475. (message "Priority of current item set to %s" news))))
  10476. (defun org-get-priority (s)
  10477. "Find priority cookie and return priority."
  10478. (save-match-data
  10479. (if (not (string-match org-priority-regexp s))
  10480. (* 1000 (- org-lowest-priority org-default-priority))
  10481. (* 1000 (- org-lowest-priority
  10482. (string-to-char (match-string 2 s)))))))
  10483. ;;;; Tags
  10484. (defvar org-agenda-archives-mode)
  10485. (defvar org-map-continue-from nil
  10486. "Position from where mapping should continue.
  10487. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  10488. (defvar org-scanner-tags nil
  10489. "The current tag list while the tags scanner is running.")
  10490. (defvar org-trust-scanner-tags nil
  10491. "Should `org-get-tags-at' use the tags fro the scanner.
  10492. This is for internal dynamical scoping only.
  10493. When this is non-nil, the function `org-get-tags-at' will return the value
  10494. of `org-scanner-tags' instead of building the list by itself. This
  10495. can lead to large speed-ups when the tags scanner is used in a file with
  10496. many entries, and when the list of tags is retrieved, for example to
  10497. obtain a list of properties. Building the tags list for each entry in such
  10498. a file becomes an N^2 operation - but with this variable set, it scales
  10499. as N.")
  10500. (defun org-scan-tags (action matcher &optional todo-only)
  10501. "Scan headline tags with inheritance and produce output ACTION.
  10502. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  10503. or `agenda' to produce an entry list for an agenda view. It can also be
  10504. a Lisp form or a function that should be called at each matched headline, in
  10505. this case the return value is a list of all return values from these calls.
  10506. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  10507. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  10508. only lines with a TODO keyword are included in the output."
  10509. (require 'org-agenda)
  10510. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  10511. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  10512. (org-re
  10513. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  10514. (props (list 'face 'default
  10515. 'done-face 'org-agenda-done
  10516. 'undone-face 'default
  10517. 'mouse-face 'highlight
  10518. 'org-not-done-regexp org-not-done-regexp
  10519. 'org-todo-regexp org-todo-regexp
  10520. 'help-echo
  10521. (format "mouse-2 or RET jump to org file %s"
  10522. (abbreviate-file-name
  10523. (or (buffer-file-name (buffer-base-buffer))
  10524. (buffer-name (buffer-base-buffer)))))))
  10525. (case-fold-search nil)
  10526. (org-map-continue-from nil)
  10527. lspos tags tags-list
  10528. (tags-alist (list (cons 0 org-file-tags)))
  10529. (llast 0) rtn rtn1 level category i txt
  10530. todo marker entry priority)
  10531. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  10532. (setq action (list 'lambda nil action)))
  10533. (save-excursion
  10534. (goto-char (point-min))
  10535. (when (eq action 'sparse-tree)
  10536. (org-overview)
  10537. (org-remove-occur-highlights))
  10538. (while (re-search-forward re nil t)
  10539. (catch :skip
  10540. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  10541. tags (if (match-end 4) (org-match-string-no-properties 4)))
  10542. (goto-char (setq lspos (match-beginning 0)))
  10543. (setq level (org-reduced-level (funcall outline-level))
  10544. category (org-get-category))
  10545. (setq i llast llast level)
  10546. ;; remove tag lists from same and sublevels
  10547. (while (>= i level)
  10548. (when (setq entry (assoc i tags-alist))
  10549. (setq tags-alist (delete entry tags-alist)))
  10550. (setq i (1- i)))
  10551. ;; add the next tags
  10552. (when tags
  10553. (setq tags (org-split-string tags ":")
  10554. tags-alist
  10555. (cons (cons level tags) tags-alist)))
  10556. ;; compile tags for current headline
  10557. (setq tags-list
  10558. (if org-use-tag-inheritance
  10559. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  10560. tags)
  10561. org-scanner-tags tags-list)
  10562. (when org-use-tag-inheritance
  10563. (setcdr (car tags-alist)
  10564. (mapcar (lambda (x)
  10565. (setq x (copy-sequence x))
  10566. (org-add-prop-inherited x))
  10567. (cdar tags-alist))))
  10568. (when (and tags org-use-tag-inheritance
  10569. (or (not (eq t org-use-tag-inheritance))
  10570. org-tags-exclude-from-inheritance))
  10571. ;; selective inheritance, remove uninherited ones
  10572. (setcdr (car tags-alist)
  10573. (org-remove-uniherited-tags (cdar tags-alist))))
  10574. (when (and (or (not todo-only)
  10575. (and (member todo org-not-done-keywords)
  10576. (or (not org-agenda-tags-todo-honor-ignore-options)
  10577. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  10578. (let ((case-fold-search t)) (eval matcher))
  10579. (or
  10580. (not (member org-archive-tag tags-list))
  10581. ;; we have an archive tag, should we use this anyway?
  10582. (or (not org-agenda-skip-archived-trees)
  10583. (and (eq action 'agenda) org-agenda-archives-mode))))
  10584. (unless (eq action 'sparse-tree) (org-agenda-skip))
  10585. ;; select this headline
  10586. (cond
  10587. ((eq action 'sparse-tree)
  10588. (and org-highlight-sparse-tree-matches
  10589. (org-get-heading) (match-end 0)
  10590. (org-highlight-new-match
  10591. (match-beginning 0) (match-beginning 1)))
  10592. (org-show-context 'tags-tree))
  10593. ((eq action 'agenda)
  10594. (setq txt (org-format-agenda-item
  10595. ""
  10596. (concat
  10597. (if (eq org-tags-match-list-sublevels 'indented)
  10598. (make-string (1- level) ?.) "")
  10599. (org-get-heading))
  10600. category
  10601. tags-list
  10602. )
  10603. priority (org-get-priority txt))
  10604. (goto-char lspos)
  10605. (setq marker (org-agenda-new-marker))
  10606. (org-add-props txt props
  10607. 'org-marker marker 'org-hd-marker marker 'org-category category
  10608. 'todo-state todo
  10609. 'priority priority 'type "tagsmatch")
  10610. (push txt rtn))
  10611. ((functionp action)
  10612. (setq org-map-continue-from nil)
  10613. (save-excursion
  10614. (setq rtn1 (funcall action))
  10615. (push rtn1 rtn)))
  10616. (t (error "Invalid action")))
  10617. ;; if we are to skip sublevels, jump to end of subtree
  10618. (unless org-tags-match-list-sublevels
  10619. (org-end-of-subtree t)
  10620. (backward-char 1))))
  10621. ;; Get the correct position from where to continue
  10622. (if org-map-continue-from
  10623. (goto-char org-map-continue-from)
  10624. (and (= (point) lspos) (end-of-line 1)))))
  10625. (when (and (eq action 'sparse-tree)
  10626. (not org-sparse-tree-open-archived-trees))
  10627. (org-hide-archived-subtrees (point-min) (point-max)))
  10628. (nreverse rtn)))
  10629. (defun org-remove-uniherited-tags (tags)
  10630. "Remove all tags that are not inherited from the list TAGS."
  10631. (cond
  10632. ((eq org-use-tag-inheritance t)
  10633. (if org-tags-exclude-from-inheritance
  10634. (org-delete-all org-tags-exclude-from-inheritance tags)
  10635. tags))
  10636. ((not org-use-tag-inheritance) nil)
  10637. ((stringp org-use-tag-inheritance)
  10638. (delq nil (mapcar
  10639. (lambda (x)
  10640. (if (and (string-match org-use-tag-inheritance x)
  10641. (not (member x org-tags-exclude-from-inheritance)))
  10642. x nil))
  10643. tags)))
  10644. ((listp org-use-tag-inheritance)
  10645. (delq nil (mapcar
  10646. (lambda (x)
  10647. (if (member x org-use-tag-inheritance) x nil))
  10648. tags)))))
  10649. (defvar todo-only) ;; dynamically scoped
  10650. (defun org-match-sparse-tree (&optional todo-only match)
  10651. "Create a sparse tree according to tags string MATCH.
  10652. MATCH can contain positive and negative selection of tags, like
  10653. \"+WORK+URGENT-WITHBOSS\".
  10654. If optional argument TODO-ONLY is non-nil, only select lines that are
  10655. also TODO lines."
  10656. (interactive "P")
  10657. (org-prepare-agenda-buffers (list (current-buffer)))
  10658. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  10659. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  10660. (defvar org-cached-props nil)
  10661. (defun org-cached-entry-get (pom property)
  10662. (if (or (eq t org-use-property-inheritance)
  10663. (and (stringp org-use-property-inheritance)
  10664. (string-match org-use-property-inheritance property))
  10665. (and (listp org-use-property-inheritance)
  10666. (member property org-use-property-inheritance)))
  10667. ;; Caching is not possible, check it directly
  10668. (org-entry-get pom property 'inherit)
  10669. ;; Get all properties, so that we can do complicated checks easily
  10670. (cdr (assoc property (or org-cached-props
  10671. (setq org-cached-props
  10672. (org-entry-properties pom)))))))
  10673. (defun org-global-tags-completion-table (&optional files)
  10674. "Return the list of all tags in all agenda buffer/files."
  10675. (save-excursion
  10676. (org-uniquify
  10677. (delq nil
  10678. (apply 'append
  10679. (mapcar
  10680. (lambda (file)
  10681. (set-buffer (find-file-noselect file))
  10682. (append (org-get-buffer-tags)
  10683. (mapcar (lambda (x) (if (stringp (car-safe x))
  10684. (list (car-safe x)) nil))
  10685. org-tag-alist)))
  10686. (if (and files (car files))
  10687. files
  10688. (org-agenda-files))))))))
  10689. (defun org-make-tags-matcher (match)
  10690. "Create the TAGS//TODO matcher form for the selection string MATCH."
  10691. ;; todo-only is scoped dynamically into this function, and the function
  10692. ;; may change it if the matcher asks for it.
  10693. (unless match
  10694. ;; Get a new match request, with completion
  10695. (let ((org-last-tags-completion-table
  10696. (org-global-tags-completion-table)))
  10697. (setq match (org-completing-read-no-i
  10698. "Match: " 'org-tags-completion-function nil nil nil
  10699. 'org-tags-history))))
  10700. ;; Parse the string and create a lisp form
  10701. (let ((match0 match)
  10702. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  10703. minus tag mm
  10704. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  10705. orterms term orlist re-p str-p level-p level-op time-p
  10706. prop-p pn pv po cat-p gv rest)
  10707. (if (string-match "/+" match)
  10708. ;; match contains also a todo-matching request
  10709. (progn
  10710. (setq tagsmatch (substring match 0 (match-beginning 0))
  10711. todomatch (substring match (match-end 0)))
  10712. (if (string-match "^!" todomatch)
  10713. (setq todo-only t todomatch (substring todomatch 1)))
  10714. (if (string-match "^\\s-*$" todomatch)
  10715. (setq todomatch nil)))
  10716. ;; only matching tags
  10717. (setq tagsmatch match todomatch nil))
  10718. ;; Make the tags matcher
  10719. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  10720. (setq tagsmatcher t)
  10721. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  10722. (while (setq term (pop orterms))
  10723. (while (and (equal (substring term -1) "\\") orterms)
  10724. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  10725. (while (string-match re term)
  10726. (setq rest (substring term (match-end 0))
  10727. minus (and (match-end 1)
  10728. (equal (match-string 1 term) "-"))
  10729. tag (match-string 2 term)
  10730. re-p (equal (string-to-char tag) ?{)
  10731. level-p (match-end 4)
  10732. prop-p (match-end 5)
  10733. mm (cond
  10734. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  10735. (level-p
  10736. (setq level-op (org-op-to-function (match-string 3 term)))
  10737. `(,level-op level ,(string-to-number
  10738. (match-string 4 term))))
  10739. (prop-p
  10740. (setq pn (match-string 5 term)
  10741. po (match-string 6 term)
  10742. pv (match-string 7 term)
  10743. cat-p (equal pn "CATEGORY")
  10744. re-p (equal (string-to-char pv) ?{)
  10745. str-p (equal (string-to-char pv) ?\")
  10746. time-p (save-match-data
  10747. (string-match "^\"[[<].*[]>]\"$" pv))
  10748. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  10749. (if time-p (setq pv (org-matcher-time pv)))
  10750. (setq po (org-op-to-function po (if time-p 'time str-p)))
  10751. (cond
  10752. ((equal pn "CATEGORY")
  10753. (setq gv '(get-text-property (point) 'org-category)))
  10754. ((equal pn "TODO")
  10755. (setq gv 'todo))
  10756. (t
  10757. (setq gv `(org-cached-entry-get nil ,pn))))
  10758. (if re-p
  10759. (if (eq po 'org<>)
  10760. `(not (string-match ,pv (or ,gv "")))
  10761. `(string-match ,pv (or ,gv "")))
  10762. (if str-p
  10763. `(,po (or ,gv "") ,pv)
  10764. `(,po (string-to-number (or ,gv ""))
  10765. ,(string-to-number pv) ))))
  10766. (t `(member ,tag tags-list)))
  10767. mm (if minus (list 'not mm) mm)
  10768. term rest)
  10769. (push mm tagsmatcher))
  10770. (push (if (> (length tagsmatcher) 1)
  10771. (cons 'and tagsmatcher)
  10772. (car tagsmatcher))
  10773. orlist)
  10774. (setq tagsmatcher nil))
  10775. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  10776. (setq tagsmatcher
  10777. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  10778. ;; Make the todo matcher
  10779. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  10780. (setq todomatcher t)
  10781. (setq orterms (org-split-string todomatch "|") orlist nil)
  10782. (while (setq term (pop orterms))
  10783. (while (string-match re term)
  10784. (setq minus (and (match-end 1)
  10785. (equal (match-string 1 term) "-"))
  10786. kwd (match-string 2 term)
  10787. re-p (equal (string-to-char kwd) ?{)
  10788. term (substring term (match-end 0))
  10789. mm (if re-p
  10790. `(string-match ,(substring kwd 1 -1) todo)
  10791. (list 'equal 'todo kwd))
  10792. mm (if minus (list 'not mm) mm))
  10793. (push mm todomatcher))
  10794. (push (if (> (length todomatcher) 1)
  10795. (cons 'and todomatcher)
  10796. (car todomatcher))
  10797. orlist)
  10798. (setq todomatcher nil))
  10799. (setq todomatcher (if (> (length orlist) 1)
  10800. (cons 'or orlist) (car orlist))))
  10801. ;; Return the string and lisp forms of the matcher
  10802. (setq matcher (if todomatcher
  10803. (list 'and tagsmatcher todomatcher)
  10804. tagsmatcher))
  10805. (cons match0 matcher)))
  10806. (defun org-op-to-function (op &optional stringp)
  10807. "Turn an operator into the appropriate function."
  10808. (setq op
  10809. (cond
  10810. ((equal op "<" ) '(< string< org-time<))
  10811. ((equal op ">" ) '(> org-string> org-time>))
  10812. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  10813. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  10814. ((member op '("=" "==")) '(= string= org-time=))
  10815. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  10816. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  10817. (defun org<> (a b) (not (= a b)))
  10818. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  10819. (defun org-string>= (a b) (not (string< a b)))
  10820. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  10821. (defun org-string<> (a b) (not (string= a b)))
  10822. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  10823. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  10824. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  10825. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  10826. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  10827. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  10828. (defun org-2ft (s)
  10829. "Convert S to a floating point time.
  10830. If S is already a number, just return it. If it is a string, parse
  10831. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  10832. (cond
  10833. ((numberp s) s)
  10834. ((stringp s)
  10835. (condition-case nil
  10836. (float-time (apply 'encode-time (org-parse-time-string s)))
  10837. (error 0.)))
  10838. (t 0.)))
  10839. (defun org-time-today ()
  10840. "Time in seconds today at 0:00.
  10841. Returns the float number of seconds since the beginning of the
  10842. epoch to the beginning of today (00:00)."
  10843. (float-time (apply 'encode-time
  10844. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  10845. (defun org-matcher-time (s)
  10846. "Interpret a time comparison value."
  10847. (save-match-data
  10848. (cond
  10849. ((string= s "<now>") (float-time))
  10850. ((string= s "<today>") (org-time-today))
  10851. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  10852. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  10853. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  10854. (+ (org-time-today)
  10855. (* (string-to-number (match-string 1 s))
  10856. (cdr (assoc (match-string 2 s)
  10857. '(("d" . 86400.0) ("w" . 604800.0)
  10858. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10859. (t (org-2ft s)))))
  10860. (defun org-match-any-p (re list)
  10861. "Does re match any element of list?"
  10862. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10863. (delq nil list))
  10864. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10865. (defvar org-tags-overlay (org-make-overlay 1 1))
  10866. (org-detach-overlay org-tags-overlay)
  10867. (defun org-get-local-tags-at (&optional pos)
  10868. "Get a list of tags defined in the current headline."
  10869. (org-get-tags-at pos 'local))
  10870. (defun org-get-local-tags ()
  10871. "Get a list of tags defined in the current headline."
  10872. (org-get-tags-at nil 'local))
  10873. (defun org-get-tags-at (&optional pos local)
  10874. "Get a list of all headline tags applicable at POS.
  10875. POS defaults to point. If tags are inherited, the list contains
  10876. the targets in the same sequence as the headlines appear, i.e.
  10877. the tags of the current headline come last.
  10878. When LOCAL is non-nil, only return tags from the current headline,
  10879. ignore inherited ones."
  10880. (interactive)
  10881. (if (and org-trust-scanner-tags
  10882. (or (not pos) (equal pos (point)))
  10883. (not local))
  10884. org-scanner-tags
  10885. (let (tags ltags lastpos parent)
  10886. (save-excursion
  10887. (save-restriction
  10888. (widen)
  10889. (goto-char (or pos (point)))
  10890. (save-match-data
  10891. (catch 'done
  10892. (condition-case nil
  10893. (progn
  10894. (org-back-to-heading t)
  10895. (while (not (equal lastpos (point)))
  10896. (setq lastpos (point))
  10897. (when (looking-at
  10898. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  10899. (setq ltags (org-split-string
  10900. (org-match-string-no-properties 1) ":"))
  10901. (when parent
  10902. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  10903. (setq tags (append
  10904. (if parent
  10905. (org-remove-uniherited-tags ltags)
  10906. ltags)
  10907. tags)))
  10908. (or org-use-tag-inheritance (throw 'done t))
  10909. (if local (throw 'done t))
  10910. (or (org-up-heading-safe) (error nil))
  10911. (setq parent t)))
  10912. (error nil)))))
  10913. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  10914. (defun org-add-prop-inherited (s)
  10915. (add-text-properties 0 (length s) '(inherited t) s)
  10916. s)
  10917. (defun org-toggle-tag (tag &optional onoff)
  10918. "Toggle the tag TAG for the current line.
  10919. If ONOFF is `on' or `off', don't toggle but set to this state."
  10920. (let (res current)
  10921. (save-excursion
  10922. (org-back-to-heading t)
  10923. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  10924. (point-at-eol) t)
  10925. (progn
  10926. (setq current (match-string 1))
  10927. (replace-match ""))
  10928. (setq current ""))
  10929. (setq current (nreverse (org-split-string current ":")))
  10930. (cond
  10931. ((eq onoff 'on)
  10932. (setq res t)
  10933. (or (member tag current) (push tag current)))
  10934. ((eq onoff 'off)
  10935. (or (not (member tag current)) (setq current (delete tag current))))
  10936. (t (if (member tag current)
  10937. (setq current (delete tag current))
  10938. (setq res t)
  10939. (push tag current))))
  10940. (end-of-line 1)
  10941. (if current
  10942. (progn
  10943. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  10944. (org-set-tags nil t))
  10945. (delete-horizontal-space))
  10946. (run-hooks 'org-after-tags-change-hook))
  10947. res))
  10948. (defun org-align-tags-here (to-col)
  10949. ;; Assumes that this is a headline
  10950. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  10951. (beginning-of-line 1)
  10952. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10953. (< pos (match-beginning 2)))
  10954. (progn
  10955. (setq tags-l (- (match-end 2) (match-beginning 2)))
  10956. (goto-char (match-beginning 1))
  10957. (insert " ")
  10958. (delete-region (point) (1+ (match-beginning 2)))
  10959. (setq ncol (max (1+ (current-column))
  10960. (1+ col)
  10961. (if (> to-col 0)
  10962. to-col
  10963. (- (abs to-col) tags-l))))
  10964. (setq p (point))
  10965. (insert (make-string (- ncol (current-column)) ?\ ))
  10966. (setq ncol (current-column))
  10967. (when indent-tabs-mode (tabify p (point-at-eol)))
  10968. (org-move-to-column (min ncol col) t))
  10969. (goto-char pos))))
  10970. (defun org-set-tags-command (&optional arg just-align)
  10971. "Call the set-tags command for the current entry."
  10972. (interactive "P")
  10973. (if (org-on-heading-p)
  10974. (org-set-tags arg just-align)
  10975. (save-excursion
  10976. (org-back-to-heading t)
  10977. (org-set-tags arg just-align))))
  10978. (defun org-set-tags-to (data)
  10979. "Set the tags of the current entry to DATA, replacing the current tags.
  10980. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  10981. If DATA is nil or the empty string, any tags will be removed."
  10982. (interactive "sTags: ")
  10983. (setq data
  10984. (cond
  10985. ((eq data nil) "")
  10986. ((equal data "") "")
  10987. ((stringp data)
  10988. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  10989. ":"))
  10990. ((listp data)
  10991. (concat ":" (mapconcat 'identity data ":") ":"))
  10992. (t nil)))
  10993. (when data
  10994. (save-excursion
  10995. (org-back-to-heading t)
  10996. (when (looking-at org-complex-heading-regexp)
  10997. (if (match-end 5)
  10998. (progn
  10999. (goto-char (match-beginning 5))
  11000. (insert data)
  11001. (delete-region (point) (point-at-eol))
  11002. (org-set-tags nil 'align))
  11003. (goto-char (point-at-eol))
  11004. (insert " " data)
  11005. (org-set-tags nil 'align)))
  11006. (beginning-of-line 1)
  11007. (if (looking-at ".*?\\([ \t]+\\)$")
  11008. (delete-region (match-beginning 1) (match-end 1))))))
  11009. (defun org-set-tags (&optional arg just-align)
  11010. "Set the tags for the current headline.
  11011. With prefix ARG, realign all tags in headings in the current buffer."
  11012. (interactive "P")
  11013. (let* ((re (concat "^" outline-regexp))
  11014. (current (org-get-tags-string))
  11015. (col (current-column))
  11016. (org-setting-tags t)
  11017. table current-tags inherited-tags ; computed below when needed
  11018. tags p0 c0 c1 rpl)
  11019. (if arg
  11020. (save-excursion
  11021. (goto-char (point-min))
  11022. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11023. (while (re-search-forward re nil t)
  11024. (org-set-tags nil t)
  11025. (end-of-line 1)))
  11026. (message "All tags realigned to column %d" org-tags-column))
  11027. (if just-align
  11028. (setq tags current)
  11029. ;; Get a new set of tags from the user
  11030. (save-excursion
  11031. (setq table (append org-tag-persistent-alist
  11032. (or org-tag-alist (org-get-buffer-tags))
  11033. (and org-complete-tags-always-offer-all-agenda-tags
  11034. (org-global-tags-completion-table (org-agenda-files))))
  11035. org-last-tags-completion-table table
  11036. current-tags (org-split-string current ":")
  11037. inherited-tags (nreverse
  11038. (nthcdr (length current-tags)
  11039. (nreverse (org-get-tags-at))))
  11040. tags
  11041. (if (or (eq t org-use-fast-tag-selection)
  11042. (and org-use-fast-tag-selection
  11043. (delq nil (mapcar 'cdr table))))
  11044. (org-fast-tag-selection
  11045. current-tags inherited-tags table
  11046. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  11047. (let ((org-add-colon-after-tag-completion t))
  11048. (org-trim
  11049. (org-without-partial-completion
  11050. (org-icompleting-read "Tags: " 'org-tags-completion-function
  11051. nil nil current 'org-tags-history)))))))
  11052. (while (string-match "[-+&]+" tags)
  11053. ;; No boolean logic, just a list
  11054. (setq tags (replace-match ":" t t tags))))
  11055. (if org-tags-sort-function
  11056. (setq tags (mapconcat 'identity
  11057. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  11058. org-tags-sort-function) ":")))
  11059. (if (string-match "\\`[\t ]*\\'" tags)
  11060. (setq tags "")
  11061. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11062. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11063. ;; Insert new tags at the correct column
  11064. (beginning-of-line 1)
  11065. (cond
  11066. ((and (equal current "") (equal tags "")))
  11067. ((re-search-forward
  11068. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11069. (point-at-eol) t)
  11070. (if (equal tags "")
  11071. (setq rpl "")
  11072. (goto-char (match-beginning 0))
  11073. (setq c0 (current-column) p0 (if (equal (char-before) ?*)
  11074. (1+ (point)) (point))
  11075. c1 (max (1+ c0) (if (> org-tags-column 0)
  11076. org-tags-column
  11077. (- (- org-tags-column) (length tags))))
  11078. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11079. (replace-match rpl t t)
  11080. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  11081. tags)
  11082. (t (error "Tags alignment failed")))
  11083. (org-move-to-column col)
  11084. (unless just-align
  11085. (run-hooks 'org-after-tags-change-hook)))))
  11086. (defun org-change-tag-in-region (beg end tag off)
  11087. "Add or remove TAG for each entry in the region.
  11088. This works in the agenda, and also in an org-mode buffer."
  11089. (interactive
  11090. (list (region-beginning) (region-end)
  11091. (let ((org-last-tags-completion-table
  11092. (if (org-mode-p)
  11093. (org-get-buffer-tags)
  11094. (org-global-tags-completion-table))))
  11095. (org-icompleting-read
  11096. "Tag: " 'org-tags-completion-function nil nil nil
  11097. 'org-tags-history))
  11098. (progn
  11099. (message "[s]et or [r]emove? ")
  11100. (equal (read-char-exclusive) ?r))))
  11101. (if (fboundp 'deactivate-mark) (deactivate-mark))
  11102. (let ((agendap (equal major-mode 'org-agenda-mode))
  11103. l1 l2 m buf pos newhead (cnt 0))
  11104. (goto-char end)
  11105. (setq l2 (1- (org-current-line)))
  11106. (goto-char beg)
  11107. (setq l1 (org-current-line))
  11108. (loop for l from l1 to l2 do
  11109. (org-goto-line l)
  11110. (setq m (get-text-property (point) 'org-hd-marker))
  11111. (when (or (and (org-mode-p) (org-on-heading-p))
  11112. (and agendap m))
  11113. (setq buf (if agendap (marker-buffer m) (current-buffer))
  11114. pos (if agendap m (point)))
  11115. (with-current-buffer buf
  11116. (save-excursion
  11117. (save-restriction
  11118. (goto-char pos)
  11119. (setq cnt (1+ cnt))
  11120. (org-toggle-tag tag (if off 'off 'on))
  11121. (setq newhead (org-get-heading)))))
  11122. (and agendap (org-agenda-change-all-lines newhead m))))
  11123. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  11124. (defun org-tags-completion-function (string predicate &optional flag)
  11125. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11126. (confirm (lambda (x) (stringp (car x)))))
  11127. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  11128. (setq s1 (match-string 1 string)
  11129. s2 (match-string 2 string))
  11130. (setq s1 "" s2 string))
  11131. (cond
  11132. ((eq flag nil)
  11133. ;; try completion
  11134. (setq rtn (try-completion s2 ctable confirm))
  11135. (if (stringp rtn)
  11136. (setq rtn
  11137. (concat s1 s2 (substring rtn (length s2))
  11138. (if (and org-add-colon-after-tag-completion
  11139. (assoc rtn ctable))
  11140. ":" ""))))
  11141. rtn)
  11142. ((eq flag t)
  11143. ;; all-completions
  11144. (all-completions s2 ctable confirm)
  11145. )
  11146. ((eq flag 'lambda)
  11147. ;; exact match?
  11148. (assoc s2 ctable)))
  11149. ))
  11150. (defun org-fast-tag-insert (kwd tags face &optional end)
  11151. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  11152. (insert (format "%-12s" (concat kwd ":"))
  11153. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  11154. (or end "")))
  11155. (defun org-fast-tag-show-exit (flag)
  11156. (save-excursion
  11157. (org-goto-line 3)
  11158. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  11159. (replace-match ""))
  11160. (when flag
  11161. (end-of-line 1)
  11162. (org-move-to-column (- (window-width) 19) t)
  11163. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  11164. (defun org-set-current-tags-overlay (current prefix)
  11165. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  11166. (if (featurep 'xemacs)
  11167. (org-overlay-display org-tags-overlay (concat prefix s)
  11168. 'secondary-selection)
  11169. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  11170. (org-overlay-display org-tags-overlay (concat prefix s)))))
  11171. (defvar org-last-tag-selection-key nil)
  11172. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  11173. "Fast tag selection with single keys.
  11174. CURRENT is the current list of tags in the headline, INHERITED is the
  11175. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  11176. possibly with grouping information. TODO-TABLE is a similar table with
  11177. TODO keywords, should these have keys assigned to them.
  11178. If the keys are nil, a-z are automatically assigned.
  11179. Returns the new tags string, or nil to not change the current settings."
  11180. (let* ((fulltable (append table todo-table))
  11181. (maxlen (apply 'max (mapcar
  11182. (lambda (x)
  11183. (if (stringp (car x)) (string-width (car x)) 0))
  11184. fulltable)))
  11185. (buf (current-buffer))
  11186. (expert (eq org-fast-tag-selection-single-key 'expert))
  11187. (buffer-tags nil)
  11188. (fwidth (+ maxlen 3 1 3))
  11189. (ncol (/ (- (window-width) 4) fwidth))
  11190. (i-face 'org-done)
  11191. (c-face 'org-todo)
  11192. tg cnt e c char c1 c2 ntable tbl rtn
  11193. ov-start ov-end ov-prefix
  11194. (exit-after-next org-fast-tag-selection-single-key)
  11195. (done-keywords org-done-keywords)
  11196. groups ingroup)
  11197. (save-excursion
  11198. (beginning-of-line 1)
  11199. (if (looking-at
  11200. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11201. (setq ov-start (match-beginning 1)
  11202. ov-end (match-end 1)
  11203. ov-prefix "")
  11204. (setq ov-start (1- (point-at-eol))
  11205. ov-end (1+ ov-start))
  11206. (skip-chars-forward "^\n\r")
  11207. (setq ov-prefix
  11208. (concat
  11209. (buffer-substring (1- (point)) (point))
  11210. (if (> (current-column) org-tags-column)
  11211. " "
  11212. (make-string (- org-tags-column (current-column)) ?\ ))))))
  11213. (org-move-overlay org-tags-overlay ov-start ov-end)
  11214. (save-window-excursion
  11215. (if expert
  11216. (set-buffer (get-buffer-create " *Org tags*"))
  11217. (delete-other-windows)
  11218. (split-window-vertically)
  11219. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  11220. (erase-buffer)
  11221. (org-set-local 'org-done-keywords done-keywords)
  11222. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  11223. (org-fast-tag-insert "Current" current c-face "\n\n")
  11224. (org-fast-tag-show-exit exit-after-next)
  11225. (org-set-current-tags-overlay current ov-prefix)
  11226. (setq tbl fulltable char ?a cnt 0)
  11227. (while (setq e (pop tbl))
  11228. (cond
  11229. ((equal (car e) :startgroup)
  11230. (push '() groups) (setq ingroup t)
  11231. (when (not (= cnt 0))
  11232. (setq cnt 0)
  11233. (insert "\n"))
  11234. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  11235. ((equal (car e) :endgroup)
  11236. (setq ingroup nil cnt 0)
  11237. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  11238. ((equal e '(:newline))
  11239. (when (not (= cnt 0))
  11240. (setq cnt 0)
  11241. (insert "\n")
  11242. (setq e (car tbl))
  11243. (while (equal (car tbl) '(:newline))
  11244. (insert "\n")
  11245. (setq tbl (cdr tbl)))))
  11246. (t
  11247. (setq tg (copy-sequence (car e)) c2 nil)
  11248. (if (cdr e)
  11249. (setq c (cdr e))
  11250. ;; automatically assign a character.
  11251. (setq c1 (string-to-char
  11252. (downcase (substring
  11253. tg (if (= (string-to-char tg) ?@) 1 0)))))
  11254. (if (or (rassoc c1 ntable) (rassoc c1 table))
  11255. (while (or (rassoc char ntable) (rassoc char table))
  11256. (setq char (1+ char)))
  11257. (setq c2 c1))
  11258. (setq c (or c2 char)))
  11259. (if ingroup (push tg (car groups)))
  11260. (setq tg (org-add-props tg nil 'face
  11261. (cond
  11262. ((not (assoc tg table))
  11263. (org-get-todo-face tg))
  11264. ((member tg current) c-face)
  11265. ((member tg inherited) i-face)
  11266. (t nil))))
  11267. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11268. (insert "[" c "] " tg (make-string
  11269. (- fwidth 4 (length tg)) ?\ ))
  11270. (push (cons tg c) ntable)
  11271. (when (= (setq cnt (1+ cnt)) ncol)
  11272. (insert "\n")
  11273. (if ingroup (insert " "))
  11274. (setq cnt 0)))))
  11275. (setq ntable (nreverse ntable))
  11276. (insert "\n")
  11277. (goto-char (point-min))
  11278. (if (not expert) (org-fit-window-to-buffer))
  11279. (setq rtn
  11280. (catch 'exit
  11281. (while t
  11282. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  11283. (if (not groups) "no " "")
  11284. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  11285. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11286. (setq org-last-tag-selection-key c)
  11287. (cond
  11288. ((= c ?\r) (throw 'exit t))
  11289. ((= c ?!)
  11290. (setq groups (not groups))
  11291. (goto-char (point-min))
  11292. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  11293. ((= c ?\C-c)
  11294. (if (not expert)
  11295. (org-fast-tag-show-exit
  11296. (setq exit-after-next (not exit-after-next)))
  11297. (setq expert nil)
  11298. (delete-other-windows)
  11299. (split-window-vertically)
  11300. (org-switch-to-buffer-other-window " *Org tags*")
  11301. (org-fit-window-to-buffer)))
  11302. ((or (= c ?\C-g)
  11303. (and (= c ?q) (not (rassoc c ntable))))
  11304. (org-detach-overlay org-tags-overlay)
  11305. (setq quit-flag t))
  11306. ((= c ?\ )
  11307. (setq current nil)
  11308. (if exit-after-next (setq exit-after-next 'now)))
  11309. ((= c ?\t)
  11310. (condition-case nil
  11311. (setq tg (org-icompleting-read
  11312. "Tag: "
  11313. (or buffer-tags
  11314. (with-current-buffer buf
  11315. (org-get-buffer-tags)))))
  11316. (quit (setq tg "")))
  11317. (when (string-match "\\S-" tg)
  11318. (add-to-list 'buffer-tags (list tg))
  11319. (if (member tg current)
  11320. (setq current (delete tg current))
  11321. (push tg current)))
  11322. (if exit-after-next (setq exit-after-next 'now)))
  11323. ((setq e (rassoc c todo-table) tg (car e))
  11324. (with-current-buffer buf
  11325. (save-excursion (org-todo tg)))
  11326. (if exit-after-next (setq exit-after-next 'now)))
  11327. ((setq e (rassoc c ntable) tg (car e))
  11328. (if (member tg current)
  11329. (setq current (delete tg current))
  11330. (loop for g in groups do
  11331. (if (member tg g)
  11332. (mapc (lambda (x)
  11333. (setq current (delete x current)))
  11334. g)))
  11335. (push tg current))
  11336. (if exit-after-next (setq exit-after-next 'now))))
  11337. ;; Create a sorted list
  11338. (setq current
  11339. (sort current
  11340. (lambda (a b)
  11341. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  11342. (if (eq exit-after-next 'now) (throw 'exit t))
  11343. (goto-char (point-min))
  11344. (beginning-of-line 2)
  11345. (delete-region (point) (point-at-eol))
  11346. (org-fast-tag-insert "Current" current c-face)
  11347. (org-set-current-tags-overlay current ov-prefix)
  11348. (while (re-search-forward
  11349. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  11350. (setq tg (match-string 1))
  11351. (add-text-properties
  11352. (match-beginning 1) (match-end 1)
  11353. (list 'face
  11354. (cond
  11355. ((member tg current) c-face)
  11356. ((member tg inherited) i-face)
  11357. (t (get-text-property (match-beginning 1) 'face))))))
  11358. (goto-char (point-min)))))
  11359. (org-detach-overlay org-tags-overlay)
  11360. (if rtn
  11361. (mapconcat 'identity current ":")
  11362. nil))))
  11363. (defun org-get-tags-string ()
  11364. "Get the TAGS string in the current headline."
  11365. (unless (org-on-heading-p t)
  11366. (error "Not on a heading"))
  11367. (save-excursion
  11368. (beginning-of-line 1)
  11369. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11370. (org-match-string-no-properties 1)
  11371. "")))
  11372. (defun org-get-tags ()
  11373. "Get the list of tags specified in the current headline."
  11374. (org-split-string (org-get-tags-string) ":"))
  11375. (defun org-get-buffer-tags ()
  11376. "Get a table of all tags used in the buffer, for completion."
  11377. (let (tags)
  11378. (save-excursion
  11379. (goto-char (point-min))
  11380. (while (re-search-forward
  11381. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  11382. (when (equal (char-after (point-at-bol 0)) ?*)
  11383. (mapc (lambda (x) (add-to-list 'tags x))
  11384. (org-split-string (org-match-string-no-properties 1) ":")))))
  11385. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  11386. (mapcar 'list tags)))
  11387. ;;;; The mapping API
  11388. ;;;###autoload
  11389. (defun org-map-entries (func &optional match scope &rest skip)
  11390. "Call FUNC at each headline selected by MATCH in SCOPE.
  11391. FUNC is a function or a lisp form. The function will be called without
  11392. arguments, with the cursor positioned at the beginning of the headline.
  11393. The return values of all calls to the function will be collected and
  11394. returned as a list.
  11395. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  11396. does not need to preserve point. After evaluation, the cursor will be
  11397. moved to the end of the line (presumably of the headline of the
  11398. processed entry) and search continues from there. Under some
  11399. circumstances, this may not produce the wanted results. For example,
  11400. if you have removed (e.g. archived) the current (sub)tree it could
  11401. mean that the next entry will be skipped entirely. In such cases, you
  11402. can specify the position from where search should continue by making
  11403. FUNC set the variable `org-map-continue-from' to the desired buffer
  11404. position.
  11405. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  11406. Only headlines that are matched by this query will be considered during
  11407. the iteration. When MATCH is nil or t, all headlines will be
  11408. visited by the iteration.
  11409. SCOPE determines the scope of this command. It can be any of:
  11410. nil The current buffer, respecting the restriction if any
  11411. tree The subtree started with the entry at point
  11412. file The current buffer, without restriction
  11413. file-with-archives
  11414. The current buffer, and any archives associated with it
  11415. agenda All agenda files
  11416. agenda-with-archives
  11417. All agenda files with any archive files associated with them
  11418. \(file1 file2 ...)
  11419. If this is a list, all files in the list will be scanned
  11420. The remaining args are treated as settings for the skipping facilities of
  11421. the scanner. The following items can be given here:
  11422. archive skip trees with the archive tag.
  11423. comment skip trees with the COMMENT keyword
  11424. function or Emacs Lisp form:
  11425. will be used as value for `org-agenda-skip-function', so whenever
  11426. the function returns t, FUNC will not be called for that
  11427. entry and search will continue from the point where the
  11428. function leaves it.
  11429. If your function needs to retrieve the tags including inherited tags
  11430. at the *current* entry, you can use the value of the variable
  11431. `org-scanner-tags' which will be much faster than getting the value
  11432. with `org-get-tags-at'. If your function gets properties with
  11433. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  11434. to t around the call to `org-entry-properties' to get the same speedup.
  11435. Note that if your function moves around to retrieve tags and properties at
  11436. a *different* entry, you cannot use these techniques."
  11437. (let* ((org-agenda-archives-mode nil) ; just to make sure
  11438. (org-agenda-skip-archived-trees (memq 'archive skip))
  11439. (org-agenda-skip-comment-trees (memq 'comment skip))
  11440. (org-agenda-skip-function
  11441. (car (org-delete-all '(comment archive) skip)))
  11442. (org-tags-match-list-sublevels t)
  11443. matcher file res
  11444. org-todo-keywords-for-agenda
  11445. org-done-keywords-for-agenda
  11446. org-todo-keyword-alist-for-agenda
  11447. org-drawers-for-agenda
  11448. org-tag-alist-for-agenda)
  11449. (cond
  11450. ((eq match t) (setq matcher t))
  11451. ((eq match nil) (setq matcher t))
  11452. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  11453. (save-excursion
  11454. (save-restriction
  11455. (when (eq scope 'tree)
  11456. (org-back-to-heading t)
  11457. (org-narrow-to-subtree)
  11458. (setq scope nil))
  11459. (if (not scope)
  11460. (progn
  11461. (org-prepare-agenda-buffers
  11462. (list (buffer-file-name (current-buffer))))
  11463. (setq res (org-scan-tags func matcher)))
  11464. ;; Get the right scope
  11465. (cond
  11466. ((and scope (listp scope) (symbolp (car scope)))
  11467. (setq scope (eval scope)))
  11468. ((eq scope 'agenda)
  11469. (setq scope (org-agenda-files t)))
  11470. ((eq scope 'agenda-with-archives)
  11471. (setq scope (org-agenda-files t))
  11472. (setq scope (org-add-archive-files scope)))
  11473. ((eq scope 'file)
  11474. (setq scope (list (buffer-file-name))))
  11475. ((eq scope 'file-with-archives)
  11476. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  11477. (org-prepare-agenda-buffers scope)
  11478. (while (setq file (pop scope))
  11479. (with-current-buffer (org-find-base-buffer-visiting file)
  11480. (save-excursion
  11481. (save-restriction
  11482. (widen)
  11483. (goto-char (point-min))
  11484. (setq res (append res (org-scan-tags func matcher))))))))))
  11485. res))
  11486. ;;;; Properties
  11487. ;;; Setting and retrieving properties
  11488. (defconst org-special-properties
  11489. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  11490. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED")
  11491. "The special properties valid in Org-mode.
  11492. These are properties that are not defined in the property drawer,
  11493. but in some other way.")
  11494. (defconst org-default-properties
  11495. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  11496. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  11497. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  11498. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  11499. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  11500. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  11501. "Some properties that are used by Org-mode for various purposes.
  11502. Being in this list makes sure that they are offered for completion.")
  11503. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  11504. "Regular expression matching the first line of a property drawer.")
  11505. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  11506. "Regular expression matching the last line of a property drawer.")
  11507. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  11508. "Regular expression matching the first line of a property drawer.")
  11509. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  11510. "Regular expression matching the first line of a property drawer.")
  11511. (defconst org-property-drawer-re
  11512. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  11513. org-property-end-re "\\)\n?")
  11514. "Matches an entire property drawer.")
  11515. (defconst org-clock-drawer-re
  11516. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  11517. org-property-end-re "\\)\n?")
  11518. "Matches an entire clock drawer.")
  11519. (defun org-property-action ()
  11520. "Do an action on properties."
  11521. (interactive)
  11522. (let (c)
  11523. (org-at-property-p)
  11524. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  11525. (setq c (read-char-exclusive))
  11526. (cond
  11527. ((equal c ?s)
  11528. (call-interactively 'org-set-property))
  11529. ((equal c ?d)
  11530. (call-interactively 'org-delete-property))
  11531. ((equal c ?D)
  11532. (call-interactively 'org-delete-property-globally))
  11533. ((equal c ?c)
  11534. (call-interactively 'org-compute-property-at-point))
  11535. (t (error "No such property action %c" c)))))
  11536. (defun org-set-effort (&optional value)
  11537. "Set the effort property of the current entry.
  11538. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  11539. allowed value."
  11540. (interactive "P")
  11541. (if (equal value 0) (setq value 10))
  11542. (let* ((completion-ignore-case t)
  11543. (prop org-effort-property)
  11544. (cur (org-entry-get nil prop))
  11545. (allowed (org-property-get-allowed-values nil prop 'table))
  11546. (existing (mapcar 'list (org-property-values prop)))
  11547. rpl
  11548. (val (cond
  11549. ((stringp value) value)
  11550. ((and allowed (integerp value))
  11551. (or (car (nth (1- value) allowed))
  11552. (car (org-last allowed))))
  11553. (allowed
  11554. (message "Select 1-9,0, [RET%s]: %s"
  11555. (if cur (concat "=" cur) "")
  11556. (mapconcat 'car allowed " "))
  11557. (setq rpl (read-char-exclusive))
  11558. (if (equal rpl ?\r)
  11559. cur
  11560. (setq rpl (- rpl ?0))
  11561. (if (equal rpl 0) (setq rpl 10))
  11562. (if (and (> rpl 0) (<= rpl (length allowed)))
  11563. (car (nth (1- rpl) allowed))
  11564. (org-completing-read "Effort: " allowed nil))))
  11565. (t
  11566. (let (org-completion-use-ido org-completion-use-iswitchb)
  11567. (org-completing-read
  11568. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  11569. (concat "[" cur "]") "")
  11570. ": ")
  11571. existing nil nil "" nil cur))))))
  11572. (unless (equal (org-entry-get nil prop) val)
  11573. (org-entry-put nil prop val))
  11574. (message "%s is now %s" prop val)))
  11575. (defun org-at-property-p ()
  11576. "Is cursor inside a property drawer?"
  11577. (save-excursion
  11578. (beginning-of-line 1)
  11579. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  11580. (let ((match (match-data)) ;; Keep match-data for use by calling
  11581. (p (point)) ;; procedures.
  11582. (range (unless (org-before-first-heading-p)
  11583. (org-get-property-block))))
  11584. (prog1 (and range (<= (car range) p) (< p (cdr range)))
  11585. (set-match-data match))))))
  11586. (defun org-get-property-block (&optional beg end force)
  11587. "Return the (beg . end) range of the body of the property drawer.
  11588. BEG and END can be beginning and end of subtree, if not given
  11589. they will be found.
  11590. If the drawer does not exist and FORCE is non-nil, create the drawer."
  11591. (catch 'exit
  11592. (save-excursion
  11593. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  11594. (end (or end (progn (outline-next-heading) (point)))))
  11595. (goto-char beg)
  11596. (if (re-search-forward org-property-start-re end t)
  11597. (setq beg (1+ (match-end 0)))
  11598. (if force
  11599. (save-excursion
  11600. (org-insert-property-drawer)
  11601. (setq end (progn (outline-next-heading) (point))))
  11602. (throw 'exit nil))
  11603. (goto-char beg)
  11604. (if (re-search-forward org-property-start-re end t)
  11605. (setq beg (1+ (match-end 0)))))
  11606. (if (re-search-forward org-property-end-re end t)
  11607. (setq end (match-beginning 0))
  11608. (or force (throw 'exit nil))
  11609. (goto-char beg)
  11610. (setq end beg)
  11611. (org-indent-line-function)
  11612. (insert ":END:\n"))
  11613. (cons beg end)))))
  11614. (defun org-entry-properties (&optional pom which specific)
  11615. "Get all properties of the entry at point-or-marker POM.
  11616. This includes the TODO keyword, the tags, time strings for deadline,
  11617. scheduled, and clocking, and any additional properties defined in the
  11618. entry. The return value is an alist, keys may occur multiple times
  11619. if the property key was used several times.
  11620. POM may also be nil, in which case the current entry is used.
  11621. If WHICH is nil or `all', get all properties. If WHICH is
  11622. `special' or `standard', only get that subclass. If WHICH
  11623. is a string only get exactly this property. Specific can be a string, the
  11624. specific property we are interested in. Specifying it can speed
  11625. things up because then unnecessary parsing is avoided."
  11626. (setq which (or which 'all))
  11627. (org-with-point-at pom
  11628. (let ((clockstr (substring org-clock-string 0 -1))
  11629. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  11630. (case-fold-search nil)
  11631. beg end range props sum-props key value string clocksum)
  11632. (save-excursion
  11633. (when (condition-case nil
  11634. (and (org-mode-p) (org-back-to-heading t))
  11635. (error nil))
  11636. (setq beg (point))
  11637. (setq sum-props (get-text-property (point) 'org-summaries))
  11638. (setq clocksum (get-text-property (point) :org-clock-minutes))
  11639. (outline-next-heading)
  11640. (setq end (point))
  11641. (when (memq which '(all special))
  11642. ;; Get the special properties, like TODO and tags
  11643. (goto-char beg)
  11644. (when (and (or (not specific) (string= specific "TODO"))
  11645. (looking-at org-todo-line-regexp) (match-end 2))
  11646. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  11647. (when (and (or (not specific) (string= specific "PRIORITY"))
  11648. (looking-at org-priority-regexp))
  11649. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  11650. (when (and (or (not specific) (string= specific "TAGS"))
  11651. (setq value (org-get-tags-string))
  11652. (string-match "\\S-" value))
  11653. (push (cons "TAGS" value) props))
  11654. (when (and (or (not specific) (string= specific "ALLTAGS"))
  11655. (setq value (org-get-tags-at)))
  11656. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  11657. ":"))
  11658. props))
  11659. (when (or (not specific) (string= specific "BLOCKED"))
  11660. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  11661. (when (or (not specific)
  11662. (member specific org-all-time-keywords)
  11663. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  11664. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  11665. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  11666. string (if (equal key clockstr)
  11667. (org-no-properties
  11668. (org-trim
  11669. (buffer-substring
  11670. (match-beginning 3) (goto-char (point-at-eol)))))
  11671. (substring (org-match-string-no-properties 3) 1 -1)))
  11672. (unless key
  11673. (if (= (char-after (match-beginning 3)) ?\[)
  11674. (setq key "TIMESTAMP_IA")
  11675. (setq key "TIMESTAMP")))
  11676. (when (or (equal key clockstr) (not (assoc key props)))
  11677. (push (cons key string) props))))
  11678. )
  11679. (when (memq which '(all standard))
  11680. ;; Get the standard properties, like :PROP: ...
  11681. (setq range (org-get-property-block beg end))
  11682. (when range
  11683. (goto-char (car range))
  11684. (while (re-search-forward
  11685. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  11686. (cdr range) t)
  11687. (setq key (org-match-string-no-properties 1)
  11688. value (org-trim (or (org-match-string-no-properties 2) "")))
  11689. (unless (member key excluded)
  11690. (push (cons key (or value "")) props)))))
  11691. (if clocksum
  11692. (push (cons "CLOCKSUM"
  11693. (org-columns-number-to-string (/ (float clocksum) 60.)
  11694. 'add_times))
  11695. props))
  11696. (unless (assoc "CATEGORY" props)
  11697. (setq value (or (org-get-category)
  11698. (progn (org-refresh-category-properties)
  11699. (org-get-category))))
  11700. (push (cons "CATEGORY" value) props))
  11701. (append sum-props (nreverse props)))))))
  11702. (defun org-entry-get (pom property &optional inherit)
  11703. "Get value of PROPERTY for entry at point-or-marker POM.
  11704. If INHERIT is non-nil and the entry does not have the property,
  11705. then also check higher levels of the hierarchy.
  11706. If INHERIT is the symbol `selective', use inheritance only if the setting
  11707. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  11708. If the property is present but empty, the return value is the empty string.
  11709. If the property is not present at all, nil is returned."
  11710. (org-with-point-at pom
  11711. (if (and inherit (if (eq inherit 'selective)
  11712. (org-property-inherit-p property)
  11713. t))
  11714. (org-entry-get-with-inheritance property)
  11715. (if (member property org-special-properties)
  11716. ;; We need a special property. Use `org-entry-properties' to
  11717. ;; retrieve it, but specify the wanted property
  11718. (cdr (assoc property (org-entry-properties nil 'special property)))
  11719. (let ((range (org-get-property-block)))
  11720. (if (and range
  11721. (goto-char (car range))
  11722. (re-search-forward
  11723. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  11724. (cdr range) t))
  11725. ;; Found the property, return it.
  11726. (if (match-end 1)
  11727. (org-match-string-no-properties 1)
  11728. "")))))))
  11729. (defun org-property-or-variable-value (var &optional inherit)
  11730. "Check if there is a property fixing the value of VAR.
  11731. If yes, return this value. If not, return the current value of the variable."
  11732. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  11733. (if (and prop (stringp prop) (string-match "\\S-" prop))
  11734. (read prop)
  11735. (symbol-value var))))
  11736. (defun org-entry-delete (pom property)
  11737. "Delete the property PROPERTY from entry at point-or-marker POM."
  11738. (org-with-point-at pom
  11739. (if (member property org-special-properties)
  11740. nil ; cannot delete these properties.
  11741. (let ((range (org-get-property-block)))
  11742. (if (and range
  11743. (goto-char (car range))
  11744. (re-search-forward
  11745. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  11746. (cdr range) t))
  11747. (progn
  11748. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  11749. t)
  11750. nil)))))
  11751. ;; Multi-values properties are properties that contain multiple values
  11752. ;; These values are assumed to be single words, separated by whitespace.
  11753. (defun org-entry-add-to-multivalued-property (pom property value)
  11754. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  11755. (let* ((old (org-entry-get pom property))
  11756. (values (and old (org-split-string old "[ \t]"))))
  11757. (setq value (org-entry-protect-space value))
  11758. (unless (member value values)
  11759. (setq values (cons value values))
  11760. (org-entry-put pom property
  11761. (mapconcat 'identity values " ")))))
  11762. (defun org-entry-remove-from-multivalued-property (pom property value)
  11763. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  11764. (let* ((old (org-entry-get pom property))
  11765. (values (and old (org-split-string old "[ \t]"))))
  11766. (setq value (org-entry-protect-space value))
  11767. (when (member value values)
  11768. (setq values (delete value values))
  11769. (org-entry-put pom property
  11770. (mapconcat 'identity values " ")))))
  11771. (defun org-entry-member-in-multivalued-property (pom property value)
  11772. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  11773. (let* ((old (org-entry-get pom property))
  11774. (values (and old (org-split-string old "[ \t]"))))
  11775. (setq value (org-entry-protect-space value))
  11776. (member value values)))
  11777. (defun org-entry-get-multivalued-property (pom property)
  11778. "Return a list of values in a multivalued property."
  11779. (let* ((value (org-entry-get pom property))
  11780. (values (and value (org-split-string value "[ \t]"))))
  11781. (mapcar 'org-entry-restore-space values)))
  11782. (defun org-entry-put-multivalued-property (pom property &rest values)
  11783. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  11784. VALUES should be a list of strings. Spaces will be protected."
  11785. (org-entry-put pom property
  11786. (mapconcat 'org-entry-protect-space values " "))
  11787. (let* ((value (org-entry-get pom property))
  11788. (values (and value (org-split-string value "[ \t]"))))
  11789. (mapcar 'org-entry-restore-space values)))
  11790. (defun org-entry-protect-space (s)
  11791. "Protect spaces and newline in string S."
  11792. (while (string-match " " s)
  11793. (setq s (replace-match "%20" t t s)))
  11794. (while (string-match "\n" s)
  11795. (setq s (replace-match "%0A" t t s)))
  11796. s)
  11797. (defun org-entry-restore-space (s)
  11798. "Restore spaces and newline in string S."
  11799. (while (string-match "%20" s)
  11800. (setq s (replace-match " " t t s)))
  11801. (while (string-match "%0A" s)
  11802. (setq s (replace-match "\n" t t s)))
  11803. s)
  11804. (defvar org-entry-property-inherited-from (make-marker)
  11805. "Marker pointing to the entry from where a property was inherited.
  11806. Each call to `org-entry-get-with-inheritance' will set this marker to the
  11807. location of the entry where the inheritance search matched. If there was
  11808. no match, the marker will point nowhere.
  11809. Note that also `org-entry-get' calls this function, if the INHERIT flag
  11810. is set.")
  11811. (defun org-entry-get-with-inheritance (property)
  11812. "Get entry property, and search higher levels if not present."
  11813. (move-marker org-entry-property-inherited-from nil)
  11814. (let (tmp)
  11815. (save-excursion
  11816. (save-restriction
  11817. (widen)
  11818. (catch 'ex
  11819. (while t
  11820. (when (setq tmp (org-entry-get nil property))
  11821. (org-back-to-heading t)
  11822. (move-marker org-entry-property-inherited-from (point))
  11823. (throw 'ex tmp))
  11824. (or (org-up-heading-safe) (throw 'ex nil)))))
  11825. (or tmp
  11826. (cdr (assoc property org-file-properties))
  11827. (cdr (assoc property org-global-properties))
  11828. (cdr (assoc property org-global-properties-fixed))))))
  11829. (defvar org-property-changed-functions nil
  11830. "Hook called when the value of a property has changed.
  11831. Each hook function should accept two arguments, the name of the property
  11832. and the new value.")
  11833. (defun org-entry-put (pom property value)
  11834. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  11835. (org-with-point-at pom
  11836. (org-back-to-heading t)
  11837. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  11838. range)
  11839. (cond
  11840. ((equal property "TODO")
  11841. (when (and (stringp value) (string-match "\\S-" value)
  11842. (not (member value org-todo-keywords-1)))
  11843. (error "\"%s\" is not a valid TODO state" value))
  11844. (if (or (not value)
  11845. (not (string-match "\\S-" value)))
  11846. (setq value 'none))
  11847. (org-todo value)
  11848. (org-set-tags nil 'align))
  11849. ((equal property "PRIORITY")
  11850. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  11851. (string-to-char value) ?\ ))
  11852. (org-set-tags nil 'align))
  11853. ((equal property "SCHEDULED")
  11854. (if (re-search-forward org-scheduled-time-regexp end t)
  11855. (cond
  11856. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11857. ((eq value 'later) (org-timestamp-change 1 'day))
  11858. (t (call-interactively 'org-schedule)))
  11859. (call-interactively 'org-schedule)))
  11860. ((equal property "DEADLINE")
  11861. (if (re-search-forward org-deadline-time-regexp end t)
  11862. (cond
  11863. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11864. ((eq value 'later) (org-timestamp-change 1 'day))
  11865. (t (call-interactively 'org-deadline)))
  11866. (call-interactively 'org-deadline)))
  11867. ((member property org-special-properties)
  11868. (error "The %s property can not yet be set with `org-entry-put'"
  11869. property))
  11870. (t ; a non-special property
  11871. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  11872. (setq range (org-get-property-block beg end 'force))
  11873. (goto-char (car range))
  11874. (if (re-search-forward
  11875. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  11876. (progn
  11877. (delete-region (match-beginning 1) (match-end 1))
  11878. (goto-char (match-beginning 1)))
  11879. (goto-char (cdr range))
  11880. (insert "\n")
  11881. (backward-char 1)
  11882. (org-indent-line-function)
  11883. (insert ":" property ":"))
  11884. (and value (insert " " value))
  11885. (org-indent-line-function)))))
  11886. (run-hook-with-args 'org-property-changed-functions property value)))
  11887. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  11888. "Get all property keys in the current buffer.
  11889. With INCLUDE-SPECIALS, also list the special properties that reflect things
  11890. like tags and TODO state.
  11891. With INCLUDE-DEFAULTS, also include properties that has special meaning
  11892. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  11893. With INCLUDE-COLUMNS, also include property names given in COLUMN
  11894. formats in the current buffer."
  11895. (let (rtn range cfmt s p)
  11896. (save-excursion
  11897. (save-restriction
  11898. (widen)
  11899. (goto-char (point-min))
  11900. (while (re-search-forward org-property-start-re nil t)
  11901. (setq range (org-get-property-block))
  11902. (goto-char (car range))
  11903. (while (re-search-forward
  11904. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  11905. (cdr range) t)
  11906. (add-to-list 'rtn (org-match-string-no-properties 1)))
  11907. (outline-next-heading))))
  11908. (when include-specials
  11909. (setq rtn (append org-special-properties rtn)))
  11910. (when include-defaults
  11911. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  11912. (add-to-list 'rtn org-effort-property))
  11913. (when include-columns
  11914. (save-excursion
  11915. (save-restriction
  11916. (widen)
  11917. (goto-char (point-min))
  11918. (while (re-search-forward
  11919. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  11920. nil t)
  11921. (setq cfmt (match-string 2) s 0)
  11922. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  11923. cfmt s)
  11924. (setq s (match-end 0)
  11925. p (match-string 1 cfmt))
  11926. (unless (or (equal p "ITEM")
  11927. (member p org-special-properties))
  11928. (add-to-list 'rtn (match-string 1 cfmt))))))))
  11929. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  11930. (defun org-property-values (key)
  11931. "Return a list of all values of property KEY."
  11932. (save-excursion
  11933. (save-restriction
  11934. (widen)
  11935. (goto-char (point-min))
  11936. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  11937. values)
  11938. (while (re-search-forward re nil t)
  11939. (add-to-list 'values (org-trim (match-string 1))))
  11940. (delete "" values)))))
  11941. (defun org-insert-property-drawer ()
  11942. "Insert a property drawer into the current entry."
  11943. (interactive)
  11944. (org-back-to-heading t)
  11945. (looking-at outline-regexp)
  11946. (let ((indent (if org-adapt-indentation
  11947. (- (match-end 0)(match-beginning 0))
  11948. 0))
  11949. (beg (point))
  11950. (re (concat "^[ \t]*" org-keyword-time-regexp))
  11951. end hiddenp)
  11952. (outline-next-heading)
  11953. (setq end (point))
  11954. (goto-char beg)
  11955. (while (re-search-forward re end t))
  11956. (setq hiddenp (org-invisible-p))
  11957. (end-of-line 1)
  11958. (and (equal (char-after) ?\n) (forward-char 1))
  11959. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  11960. (if (member (match-string 1) '("CLOCK:" ":END:"))
  11961. ;; just skip this line
  11962. (beginning-of-line 2)
  11963. ;; Drawer start, find the end
  11964. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  11965. (beginning-of-line 1)))
  11966. (org-skip-over-state-notes)
  11967. (skip-chars-backward " \t\n\r")
  11968. (if (eq (char-before) ?*) (forward-char 1))
  11969. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  11970. (beginning-of-line 0)
  11971. (org-indent-to-column indent)
  11972. (beginning-of-line 2)
  11973. (org-indent-to-column indent)
  11974. (beginning-of-line 0)
  11975. (if hiddenp
  11976. (save-excursion
  11977. (org-back-to-heading t)
  11978. (hide-entry))
  11979. (org-flag-drawer t))))
  11980. (defun org-set-property (property value)
  11981. "In the current entry, set PROPERTY to VALUE.
  11982. When called interactively, this will prompt for a property name, offering
  11983. completion on existing and default properties. And then it will prompt
  11984. for a value, offering completion either on allowed values (via an inherited
  11985. xxx_ALL property) or on existing values in other instances of this property
  11986. in the current file."
  11987. (interactive
  11988. (let* ((completion-ignore-case t)
  11989. (keys (org-buffer-property-keys nil t t))
  11990. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  11991. (prop (if (member prop0 keys)
  11992. prop0
  11993. (or (cdr (assoc (downcase prop0)
  11994. (mapcar (lambda (x) (cons (downcase x) x))
  11995. keys)))
  11996. prop0)))
  11997. (cur (org-entry-get nil prop))
  11998. (prompt (concat prop " value"
  11999. (if (and cur (string-match "\\S-" cur))
  12000. (concat " [" cur "]") "") ": "))
  12001. (allowed (org-property-get-allowed-values nil prop 'table))
  12002. (existing (mapcar 'list (org-property-values prop)))
  12003. (val (if allowed
  12004. (org-completing-read prompt allowed nil
  12005. (not (get-text-property 0 'org-unrestricted
  12006. (caar allowed))))
  12007. (let (org-completion-use-ido org-completion-use-iswitchb)
  12008. (org-completing-read prompt existing nil nil "" nil cur)))))
  12009. (list prop (if (equal val "") cur val))))
  12010. (unless (equal (org-entry-get nil property) value)
  12011. (org-entry-put nil property value)))
  12012. (defun org-delete-property (property)
  12013. "In the current entry, delete PROPERTY."
  12014. (interactive
  12015. (let* ((completion-ignore-case t)
  12016. (prop (org-icompleting-read "Property: " (org-entry-properties nil 'standard))))
  12017. (list prop)))
  12018. (message "Property %s %s" property
  12019. (if (org-entry-delete nil property)
  12020. "deleted"
  12021. "was not present in the entry")))
  12022. (defun org-delete-property-globally (property)
  12023. "Remove PROPERTY globally, from all entries."
  12024. (interactive
  12025. (let* ((completion-ignore-case t)
  12026. (prop (org-icompleting-read
  12027. "Globally remove property: "
  12028. (mapcar 'list (org-buffer-property-keys)))))
  12029. (list prop)))
  12030. (save-excursion
  12031. (save-restriction
  12032. (widen)
  12033. (goto-char (point-min))
  12034. (let ((cnt 0))
  12035. (while (re-search-forward
  12036. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  12037. nil t)
  12038. (setq cnt (1+ cnt))
  12039. (replace-match ""))
  12040. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12041. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  12042. (defun org-compute-property-at-point ()
  12043. "Compute the property at point.
  12044. This looks for an enclosing column format, extracts the operator and
  12045. then applies it to the property in the column format's scope."
  12046. (interactive)
  12047. (unless (org-at-property-p)
  12048. (error "Not at a property"))
  12049. (let ((prop (org-match-string-no-properties 2)))
  12050. (org-columns-get-format-and-top-level)
  12051. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12052. (error "No operator defined for property %s" prop))
  12053. (org-columns-compute prop)))
  12054. (defvar org-property-allowed-value-functions nil
  12055. "Hook for functions supplying allowed values for a specific property.
  12056. The functions must take a single argument, the name of the property, and
  12057. return a flat list of allowed values. If \":ETC\" is one of
  12058. the values, this means that these values are intended as defaults for
  12059. completion, but that other values should be allowed too.
  12060. The functions must return nil if they are not responsible for this
  12061. property.")
  12062. (defun org-property-get-allowed-values (pom property &optional table)
  12063. "Get allowed values for the property PROPERTY.
  12064. When TABLE is non-nil, return an alist that can directly be used for
  12065. completion."
  12066. (let (vals)
  12067. (cond
  12068. ((equal property "TODO")
  12069. (setq vals (org-with-point-at pom
  12070. (append org-todo-keywords-1 '("")))))
  12071. ((equal property "PRIORITY")
  12072. (let ((n org-lowest-priority))
  12073. (while (>= n org-highest-priority)
  12074. (push (char-to-string n) vals)
  12075. (setq n (1- n)))))
  12076. ((member property org-special-properties))
  12077. ((setq vals (run-hook-with-args-until-success
  12078. 'org-property-allowed-value-functions property)))
  12079. (t
  12080. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12081. (when (and vals (string-match "\\S-" vals))
  12082. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12083. (setq vals (mapcar (lambda (x)
  12084. (cond ((stringp x) x)
  12085. ((numberp x) (number-to-string x))
  12086. ((symbolp x) (symbol-name x))
  12087. (t "???")))
  12088. vals)))))
  12089. (when (member ":ETC" vals)
  12090. (setq vals (remove ":ETC" vals))
  12091. (org-add-props (car vals) '(org-unrestricted t)))
  12092. (if table (mapcar 'list vals) vals)))
  12093. (defun org-property-previous-allowed-value (&optional previous)
  12094. "Switch to the next allowed value for this property."
  12095. (interactive)
  12096. (org-property-next-allowed-value t))
  12097. (defun org-property-next-allowed-value (&optional previous)
  12098. "Switch to the next allowed value for this property."
  12099. (interactive)
  12100. (unless (org-at-property-p)
  12101. (error "Not at a property"))
  12102. (let* ((key (match-string 2))
  12103. (value (match-string 3))
  12104. (allowed (or (org-property-get-allowed-values (point) key)
  12105. (and (member value '("[ ]" "[-]" "[X]"))
  12106. '("[ ]" "[X]"))))
  12107. nval)
  12108. (unless allowed
  12109. (error "Allowed values for this property have not been defined"))
  12110. (if previous (setq allowed (reverse allowed)))
  12111. (if (member value allowed)
  12112. (setq nval (car (cdr (member value allowed)))))
  12113. (setq nval (or nval (car allowed)))
  12114. (if (equal nval value)
  12115. (error "Only one allowed value for this property"))
  12116. (org-at-property-p)
  12117. (replace-match (concat " :" key ": " nval) t t)
  12118. (org-indent-line-function)
  12119. (beginning-of-line 1)
  12120. (skip-chars-forward " \t")
  12121. (run-hook-with-args 'org-property-changed-functions key nval)))
  12122. (defun org-find-entry-with-id (ident)
  12123. "Locate the entry that contains the ID property with exact value IDENT.
  12124. IDENT can be a string, a symbol or a number, this function will search for
  12125. the string representation of it.
  12126. Return the position where this entry starts, or nil if there is no such entry."
  12127. (interactive "sID: ")
  12128. (let ((id (cond
  12129. ((stringp ident) ident)
  12130. ((symbol-name ident) (symbol-name ident))
  12131. ((numberp ident) (number-to-string ident))
  12132. (t (error "IDENT %s must be a string, symbol or number" ident))))
  12133. (case-fold-search nil))
  12134. (save-excursion
  12135. (save-restriction
  12136. (widen)
  12137. (goto-char (point-min))
  12138. (when (re-search-forward
  12139. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  12140. nil t)
  12141. (org-back-to-heading t)
  12142. (point))))))
  12143. ;;;; Timestamps
  12144. (defvar org-last-changed-timestamp nil)
  12145. (defvar org-last-inserted-timestamp nil
  12146. "The last time stamp inserted with `org-insert-time-stamp'.")
  12147. (defvar org-time-was-given) ; dynamically scoped parameter
  12148. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12149. (defvar org-ts-what) ; dynamically scoped parameter
  12150. (defun org-time-stamp (arg &optional inactive)
  12151. "Prompt for a date/time and insert a time stamp.
  12152. If the user specifies a time like HH:MM, or if this command is called
  12153. with a prefix argument, the time stamp will contain date and time.
  12154. Otherwise, only the date will be included. All parts of a date not
  12155. specified by the user will be filled in from the current date/time.
  12156. So if you press just return without typing anything, the time stamp
  12157. will represent the current date/time. If there is already a timestamp
  12158. at the cursor, it will be modified."
  12159. (interactive "P")
  12160. (let* ((ts nil)
  12161. (default-time
  12162. ;; Default time is either today, or, when entering a range,
  12163. ;; the range start.
  12164. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  12165. (save-excursion
  12166. (re-search-backward
  12167. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  12168. (- (point) 20) t)))
  12169. (apply 'encode-time (org-parse-time-string (match-string 1)))
  12170. (current-time)))
  12171. (default-input (and ts (org-get-compact-tod ts)))
  12172. org-time-was-given org-end-time-was-given time)
  12173. (cond
  12174. ((and (org-at-timestamp-p t)
  12175. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  12176. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  12177. (insert "--")
  12178. (setq time (let ((this-command this-command))
  12179. (org-read-date arg 'totime nil nil
  12180. default-time default-input)))
  12181. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  12182. ((org-at-timestamp-p t)
  12183. (setq time (let ((this-command this-command))
  12184. (org-read-date arg 'totime nil nil default-time default-input)))
  12185. (when (org-at-timestamp-p t) ; just to get the match data
  12186. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  12187. (replace-match "")
  12188. (setq org-last-changed-timestamp
  12189. (org-insert-time-stamp
  12190. time (or org-time-was-given arg)
  12191. inactive nil nil (list org-end-time-was-given))))
  12192. (message "Timestamp updated"))
  12193. (t
  12194. (setq time (let ((this-command this-command))
  12195. (org-read-date arg 'totime nil nil default-time default-input)))
  12196. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  12197. nil nil (list org-end-time-was-given))))))
  12198. ;; FIXME: can we use this for something else, like computing time differences?
  12199. (defun org-get-compact-tod (s)
  12200. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  12201. (let* ((t1 (match-string 1 s))
  12202. (h1 (string-to-number (match-string 2 s)))
  12203. (m1 (string-to-number (match-string 3 s)))
  12204. (t2 (and (match-end 4) (match-string 5 s)))
  12205. (h2 (and t2 (string-to-number (match-string 6 s))))
  12206. (m2 (and t2 (string-to-number (match-string 7 s))))
  12207. dh dm)
  12208. (if (not t2)
  12209. t1
  12210. (setq dh (- h2 h1) dm (- m2 m1))
  12211. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  12212. (concat t1 "+" (number-to-string dh)
  12213. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  12214. (defun org-time-stamp-inactive (&optional arg)
  12215. "Insert an inactive time stamp.
  12216. An inactive time stamp is enclosed in square brackets instead of angle
  12217. brackets. It is inactive in the sense that it does not trigger agenda entries,
  12218. does not link to the calendar and cannot be changed with the S-cursor keys.
  12219. So these are more for recording a certain time/date."
  12220. (interactive "P")
  12221. (org-time-stamp arg 'inactive))
  12222. (defvar org-date-ovl (org-make-overlay 1 1))
  12223. (org-overlay-put org-date-ovl 'face 'org-warning)
  12224. (org-detach-overlay org-date-ovl)
  12225. (defvar org-ans1) ; dynamically scoped parameter
  12226. (defvar org-ans2) ; dynamically scoped parameter
  12227. (defvar org-plain-time-of-day-regexp) ; defined below
  12228. (defvar org-overriding-default-time nil) ; dynamically scoped
  12229. (defvar org-read-date-overlay nil)
  12230. (defvar org-dcst nil) ; dynamically scoped
  12231. (defvar org-read-date-history nil)
  12232. (defvar org-read-date-final-answer nil)
  12233. (defun org-read-date (&optional with-time to-time from-string prompt
  12234. default-time default-input)
  12235. "Read a date, possibly a time, and make things smooth for the user.
  12236. The prompt will suggest to enter an ISO date, but you can also enter anything
  12237. which will at least partially be understood by `parse-time-string'.
  12238. Unrecognized parts of the date will default to the current day, month, year,
  12239. hour and minute. If this command is called to replace a timestamp at point,
  12240. of to enter the second timestamp of a range, the default time is taken from the
  12241. existing stamp. For example,
  12242. 3-2-5 --> 2003-02-05
  12243. feb 15 --> currentyear-02-15
  12244. sep 12 9 --> 2009-09-12
  12245. 12:45 --> today 12:45
  12246. 22 sept 0:34 --> currentyear-09-22 0:34
  12247. 12 --> currentyear-currentmonth-12
  12248. Fri --> nearest Friday (today or later)
  12249. etc.
  12250. Furthermore you can specify a relative date by giving, as the *first* thing
  12251. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  12252. change in days weeks, months, years.
  12253. With a single plus or minus, the date is relative to today. With a double
  12254. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  12255. +4d --> four days from today
  12256. +4 --> same as above
  12257. +2w --> two weeks from today
  12258. ++5 --> five days from default date
  12259. The function understands only English month and weekday abbreviations,
  12260. but this can be configured with the variables `parse-time-months' and
  12261. `parse-time-weekdays'.
  12262. While prompting, a calendar is popped up - you can also select the
  12263. date with the mouse (button 1). The calendar shows a period of three
  12264. months. To scroll it to other months, use the keys `>' and `<'.
  12265. If you don't like the calendar, turn it off with
  12266. \(setq org-read-date-popup-calendar nil)
  12267. With optional argument TO-TIME, the date will immediately be converted
  12268. to an internal time.
  12269. With an optional argument WITH-TIME, the prompt will suggest to also
  12270. insert a time. Note that when WITH-TIME is not set, you can still
  12271. enter a time, and this function will inform the calling routine about
  12272. this change. The calling routine may then choose to change the format
  12273. used to insert the time stamp into the buffer to include the time.
  12274. With optional argument FROM-STRING, read from this string instead from
  12275. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  12276. the time/date that is used for everything that is not specified by the
  12277. user."
  12278. (require 'parse-time)
  12279. (let* ((org-time-stamp-rounding-minutes
  12280. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  12281. (org-dcst org-display-custom-times)
  12282. (ct (org-current-time))
  12283. (def (or org-overriding-default-time default-time ct))
  12284. (defdecode (decode-time def))
  12285. (dummy (progn
  12286. (when (< (nth 2 defdecode) org-extend-today-until)
  12287. (setcar (nthcdr 2 defdecode) -1)
  12288. (setcar (nthcdr 1 defdecode) 59)
  12289. (setq def (apply 'encode-time defdecode)
  12290. defdecode (decode-time def)))))
  12291. (calendar-frame-setup nil)
  12292. (calendar-move-hook nil)
  12293. (calendar-view-diary-initially-flag nil)
  12294. (view-diary-entries-initially nil)
  12295. (calendar-view-holidays-initially-flag nil)
  12296. (view-calendar-holidays-initially nil)
  12297. (timestr (format-time-string
  12298. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  12299. (prompt (concat (if prompt (concat prompt " ") "")
  12300. (format "Date+time [%s]: " timestr)))
  12301. ans (org-ans0 "") org-ans1 org-ans2 final)
  12302. (cond
  12303. (from-string (setq ans from-string))
  12304. (org-read-date-popup-calendar
  12305. (save-excursion
  12306. (save-window-excursion
  12307. (calendar)
  12308. (calendar-forward-day (- (time-to-days def)
  12309. (calendar-absolute-from-gregorian
  12310. (calendar-current-date))))
  12311. (org-eval-in-calendar nil t)
  12312. (let* ((old-map (current-local-map))
  12313. (map (copy-keymap calendar-mode-map))
  12314. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  12315. (org-defkey map (kbd "RET") 'org-calendar-select)
  12316. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  12317. 'org-calendar-select-mouse)
  12318. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  12319. 'org-calendar-select-mouse)
  12320. (org-defkey minibuffer-local-map [(meta shift left)]
  12321. (lambda () (interactive)
  12322. (org-eval-in-calendar '(calendar-backward-month 1))))
  12323. (org-defkey minibuffer-local-map [(meta shift right)]
  12324. (lambda () (interactive)
  12325. (org-eval-in-calendar '(calendar-forward-month 1))))
  12326. (org-defkey minibuffer-local-map [(meta shift up)]
  12327. (lambda () (interactive)
  12328. (org-eval-in-calendar '(calendar-backward-year 1))))
  12329. (org-defkey minibuffer-local-map [(meta shift down)]
  12330. (lambda () (interactive)
  12331. (org-eval-in-calendar '(calendar-forward-year 1))))
  12332. (org-defkey minibuffer-local-map [?\e (shift left)]
  12333. (lambda () (interactive)
  12334. (org-eval-in-calendar '(calendar-backward-month 1))))
  12335. (org-defkey minibuffer-local-map [?\e (shift right)]
  12336. (lambda () (interactive)
  12337. (org-eval-in-calendar '(calendar-forward-month 1))))
  12338. (org-defkey minibuffer-local-map [?\e (shift up)]
  12339. (lambda () (interactive)
  12340. (org-eval-in-calendar '(calendar-backward-year 1))))
  12341. (org-defkey minibuffer-local-map [?\e (shift down)]
  12342. (lambda () (interactive)
  12343. (org-eval-in-calendar '(calendar-forward-year 1))))
  12344. (org-defkey minibuffer-local-map [(shift up)]
  12345. (lambda () (interactive)
  12346. (org-eval-in-calendar '(calendar-backward-week 1))))
  12347. (org-defkey minibuffer-local-map [(shift down)]
  12348. (lambda () (interactive)
  12349. (org-eval-in-calendar '(calendar-forward-week 1))))
  12350. (org-defkey minibuffer-local-map [(shift left)]
  12351. (lambda () (interactive)
  12352. (org-eval-in-calendar '(calendar-backward-day 1))))
  12353. (org-defkey minibuffer-local-map [(shift right)]
  12354. (lambda () (interactive)
  12355. (org-eval-in-calendar '(calendar-forward-day 1))))
  12356. (org-defkey minibuffer-local-map ">"
  12357. (lambda () (interactive)
  12358. (org-eval-in-calendar '(scroll-calendar-left 1))))
  12359. (org-defkey minibuffer-local-map "<"
  12360. (lambda () (interactive)
  12361. (org-eval-in-calendar '(scroll-calendar-right 1))))
  12362. (run-hooks 'org-read-date-minibuffer-setup-hook)
  12363. (unwind-protect
  12364. (progn
  12365. (use-local-map map)
  12366. (add-hook 'post-command-hook 'org-read-date-display)
  12367. (setq org-ans0 (read-string prompt default-input
  12368. 'org-read-date-history nil))
  12369. ;; org-ans0: from prompt
  12370. ;; org-ans1: from mouse click
  12371. ;; org-ans2: from calendar motion
  12372. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  12373. (remove-hook 'post-command-hook 'org-read-date-display)
  12374. (use-local-map old-map)
  12375. (when org-read-date-overlay
  12376. (org-delete-overlay org-read-date-overlay)
  12377. (setq org-read-date-overlay nil)))))))
  12378. (t ; Naked prompt only
  12379. (unwind-protect
  12380. (setq ans (read-string prompt default-input
  12381. 'org-read-date-history timestr))
  12382. (when org-read-date-overlay
  12383. (org-delete-overlay org-read-date-overlay)
  12384. (setq org-read-date-overlay nil)))))
  12385. (setq final (org-read-date-analyze ans def defdecode))
  12386. (setq org-read-date-final-answer ans)
  12387. (if to-time
  12388. (apply 'encode-time final)
  12389. (if (and (boundp 'org-time-was-given) org-time-was-given)
  12390. (format "%04d-%02d-%02d %02d:%02d"
  12391. (nth 5 final) (nth 4 final) (nth 3 final)
  12392. (nth 2 final) (nth 1 final))
  12393. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  12394. (defvar def)
  12395. (defvar defdecode)
  12396. (defvar with-time)
  12397. (defvar org-read-date-analyze-futurep nil)
  12398. (defun org-read-date-display ()
  12399. "Display the current date prompt interpretation in the minibuffer."
  12400. (when org-read-date-display-live
  12401. (when org-read-date-overlay
  12402. (org-delete-overlay org-read-date-overlay))
  12403. (let ((p (point)))
  12404. (end-of-line 1)
  12405. (while (not (equal (buffer-substring
  12406. (max (point-min) (- (point) 4)) (point))
  12407. " "))
  12408. (insert " "))
  12409. (goto-char p))
  12410. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  12411. " " (or org-ans1 org-ans2)))
  12412. (org-end-time-was-given nil)
  12413. (f (org-read-date-analyze ans def defdecode))
  12414. (fmts (if org-dcst
  12415. org-time-stamp-custom-formats
  12416. org-time-stamp-formats))
  12417. (fmt (if (or with-time
  12418. (and (boundp 'org-time-was-given) org-time-was-given))
  12419. (cdr fmts)
  12420. (car fmts)))
  12421. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  12422. (when (and org-end-time-was-given
  12423. (string-match org-plain-time-of-day-regexp txt))
  12424. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  12425. org-end-time-was-given
  12426. (substring txt (match-end 0)))))
  12427. (when org-read-date-analyze-futurep
  12428. (setq txt (concat txt " (=>F)")))
  12429. (setq org-read-date-overlay
  12430. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  12431. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  12432. (defun org-read-date-analyze (ans def defdecode)
  12433. "Analyse the combined answer of the date prompt."
  12434. ;; FIXME: cleanup and comment
  12435. (let ((nowdecode (decode-time (current-time)))
  12436. delta deltan deltaw deltadef year month day
  12437. hour minute second wday pm h2 m2 tl wday1
  12438. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  12439. (setq org-read-date-analyze-futurep nil)
  12440. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  12441. (setq ans "+0"))
  12442. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  12443. (setq ans (replace-match "" t t ans)
  12444. deltan (car delta)
  12445. deltaw (nth 1 delta)
  12446. deltadef (nth 2 delta)))
  12447. ;; Check if there is an iso week date in there
  12448. ;; If yes, store the info and postpone interpreting it until the rest
  12449. ;; of the parsing is done
  12450. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  12451. (setq iso-year (if (match-end 1)
  12452. (org-small-year-to-year
  12453. (string-to-number (match-string 1 ans))))
  12454. iso-weekday (if (match-end 3)
  12455. (string-to-number (match-string 3 ans)))
  12456. iso-week (string-to-number (match-string 2 ans)))
  12457. (setq ans (replace-match "" t t ans)))
  12458. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  12459. (when (string-match
  12460. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  12461. (setq year (if (match-end 2)
  12462. (string-to-number (match-string 2 ans))
  12463. (progn (setq kill-year t)
  12464. (string-to-number (format-time-string "%Y"))))
  12465. month (string-to-number (match-string 3 ans))
  12466. day (string-to-number (match-string 4 ans)))
  12467. (if (< year 100) (setq year (+ 2000 year)))
  12468. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12469. t nil ans)))
  12470. ;; Help matching american dates, like 5/30 or 5/30/7
  12471. (when (string-match
  12472. "^ *\\([0-3]?[0-9]\\)/\\([0-1]?[0-9]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  12473. (setq year (if (match-end 4)
  12474. (string-to-number (match-string 4 ans))
  12475. (progn (setq kill-year t)
  12476. (string-to-number (format-time-string "%Y"))))
  12477. month (string-to-number (match-string 1 ans))
  12478. day (string-to-number (match-string 2 ans)))
  12479. (if (< year 100) (setq year (+ 2000 year)))
  12480. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12481. t nil ans)))
  12482. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  12483. ;; If there is a time with am/pm, and *no* time without it, we convert
  12484. ;; so that matching will be successful.
  12485. (loop for i from 1 to 2 do ; twice, for end time as well
  12486. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  12487. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  12488. (setq hour (string-to-number (match-string 1 ans))
  12489. minute (if (match-end 3)
  12490. (string-to-number (match-string 3 ans))
  12491. 0)
  12492. pm (equal ?p
  12493. (string-to-char (downcase (match-string 4 ans)))))
  12494. (if (and (= hour 12) (not pm))
  12495. (setq hour 0)
  12496. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  12497. (setq ans (replace-match (format "%02d:%02d" hour minute)
  12498. t t ans))))
  12499. ;; Check if a time range is given as a duration
  12500. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  12501. (setq hour (string-to-number (match-string 1 ans))
  12502. h2 (+ hour (string-to-number (match-string 3 ans)))
  12503. minute (string-to-number (match-string 2 ans))
  12504. m2 (+ minute (if (match-end 5) (string-to-number
  12505. (match-string 5 ans))0)))
  12506. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  12507. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  12508. t t ans)))
  12509. ;; Check if there is a time range
  12510. (when (boundp 'org-end-time-was-given)
  12511. (setq org-time-was-given nil)
  12512. (when (and (string-match org-plain-time-of-day-regexp ans)
  12513. (match-end 8))
  12514. (setq org-end-time-was-given (match-string 8 ans))
  12515. (setq ans (concat (substring ans 0 (match-beginning 7))
  12516. (substring ans (match-end 7))))))
  12517. (setq tl (parse-time-string ans)
  12518. day (or (nth 3 tl) (nth 3 defdecode))
  12519. month (or (nth 4 tl)
  12520. (if (and org-read-date-prefer-future
  12521. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  12522. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  12523. (nth 4 defdecode)))
  12524. year (or (and (not kill-year) (nth 5 tl))
  12525. (if (and org-read-date-prefer-future
  12526. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  12527. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  12528. (nth 5 defdecode)))
  12529. hour (or (nth 2 tl) (nth 2 defdecode))
  12530. minute (or (nth 1 tl) (nth 1 defdecode))
  12531. second (or (nth 0 tl) 0)
  12532. wday (nth 6 tl))
  12533. (when (and (eq org-read-date-prefer-future 'time)
  12534. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  12535. (equal day (nth 3 nowdecode))
  12536. (equal month (nth 4 nowdecode))
  12537. (equal year (nth 5 nowdecode))
  12538. (nth 2 tl)
  12539. (or (< (nth 2 tl) (nth 2 nowdecode))
  12540. (and (= (nth 2 tl) (nth 2 nowdecode))
  12541. (nth 1 tl)
  12542. (< (nth 1 tl) (nth 1 nowdecode)))))
  12543. (setq day (1+ day)
  12544. futurep t))
  12545. ;; Special date definitions below
  12546. (cond
  12547. (iso-week
  12548. ;; There was an iso week
  12549. (require 'cal-iso)
  12550. (setq futurep nil)
  12551. (setq year (or iso-year year)
  12552. day (or iso-weekday wday 1)
  12553. wday nil ; to make sure that the trigger below does not match
  12554. iso-date (calendar-gregorian-from-absolute
  12555. (calendar-absolute-from-iso
  12556. (list iso-week day year))))
  12557. ; FIXME: Should we also push ISO weeks into the future?
  12558. ; (when (and org-read-date-prefer-future
  12559. ; (not iso-year)
  12560. ; (< (calendar-absolute-from-gregorian iso-date)
  12561. ; (time-to-days (current-time))))
  12562. ; (setq year (1+ year)
  12563. ; iso-date (calendar-gregorian-from-absolute
  12564. ; (calendar-absolute-from-iso
  12565. ; (list iso-week day year)))))
  12566. (setq month (car iso-date)
  12567. year (nth 2 iso-date)
  12568. day (nth 1 iso-date)))
  12569. (deltan
  12570. (setq futurep nil)
  12571. (unless deltadef
  12572. (let ((now (decode-time (current-time))))
  12573. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  12574. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  12575. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  12576. ((equal deltaw "m") (setq month (+ month deltan)))
  12577. ((equal deltaw "y") (setq year (+ year deltan)))))
  12578. ((and wday (not (nth 3 tl)))
  12579. (setq futurep nil)
  12580. ;; Weekday was given, but no day, so pick that day in the week
  12581. ;; on or after the derived date.
  12582. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  12583. (unless (equal wday wday1)
  12584. (setq day (+ day (% (- wday wday1 -7) 7))))))
  12585. (if (and (boundp 'org-time-was-given)
  12586. (nth 2 tl))
  12587. (setq org-time-was-given t))
  12588. (if (< year 100) (setq year (+ 2000 year)))
  12589. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  12590. (setq org-read-date-analyze-futurep futurep)
  12591. (list second minute hour day month year)))
  12592. (defvar parse-time-weekdays)
  12593. (defun org-read-date-get-relative (s today default)
  12594. "Check string S for special relative date string.
  12595. TODAY and DEFAULT are internal times, for today and for a default.
  12596. Return shift list (N what def-flag)
  12597. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  12598. N is the number of WHATs to shift.
  12599. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  12600. the DEFAULT date rather than TODAY."
  12601. (when (and
  12602. (string-match
  12603. (concat
  12604. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  12605. "\\([0-9]+\\)?"
  12606. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  12607. "\\([ \t]\\|$\\)") s)
  12608. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  12609. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  12610. (string-to-char (substring (match-string 1 s) -1))
  12611. ?+))
  12612. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  12613. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  12614. (what (if (match-end 3) (match-string 3 s) "d"))
  12615. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  12616. (date (if rel default today))
  12617. (wday (nth 6 (decode-time date)))
  12618. delta)
  12619. (if wday1
  12620. (progn
  12621. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  12622. (if (= dir ?-) (setq delta (- delta 7)))
  12623. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  12624. (list delta "d" rel))
  12625. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  12626. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  12627. "Turn a user-specified date into the internal representation.
  12628. The internal representation needed by the calendar is (month day year).
  12629. This is a wrapper to handle the brain-dead convention in calendar that
  12630. user function argument order change dependent on argument order."
  12631. (if (boundp 'calendar-date-style)
  12632. (cond
  12633. ((eq calendar-date-style 'american)
  12634. (list arg1 arg2 arg3))
  12635. ((eq calendar-date-style 'european)
  12636. (list arg2 arg1 arg3))
  12637. ((eq calendar-date-style 'iso)
  12638. (list arg2 arg3 arg1)))
  12639. (if (org-bound-and-true-p european-calendar-style)
  12640. (list arg2 arg1 arg3)
  12641. (list arg1 arg2 arg3))))
  12642. (defun org-eval-in-calendar (form &optional keepdate)
  12643. "Eval FORM in the calendar window and return to current window.
  12644. Also, store the cursor date in variable org-ans2."
  12645. (let ((sf (selected-frame))
  12646. (sw (selected-window)))
  12647. (select-window (get-buffer-window "*Calendar*" t))
  12648. (eval form)
  12649. (when (and (not keepdate) (calendar-cursor-to-date))
  12650. (let* ((date (calendar-cursor-to-date))
  12651. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12652. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  12653. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  12654. (select-window sw)
  12655. (org-select-frame-set-input-focus sf)))
  12656. (defun org-calendar-select ()
  12657. "Return to `org-read-date' with the date currently selected.
  12658. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12659. (interactive)
  12660. (when (calendar-cursor-to-date)
  12661. (let* ((date (calendar-cursor-to-date))
  12662. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12663. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12664. (if (active-minibuffer-window) (exit-minibuffer))))
  12665. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  12666. "Insert a date stamp for the date given by the internal TIME.
  12667. WITH-HM means use the stamp format that includes the time of the day.
  12668. INACTIVE means use square brackets instead of angular ones, so that the
  12669. stamp will not contribute to the agenda.
  12670. PRE and POST are optional strings to be inserted before and after the
  12671. stamp.
  12672. The command returns the inserted time stamp."
  12673. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  12674. stamp)
  12675. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  12676. (insert-before-markers (or pre ""))
  12677. (insert-before-markers (setq stamp (format-time-string fmt time)))
  12678. (when (listp extra)
  12679. (setq extra (car extra))
  12680. (if (and (stringp extra)
  12681. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  12682. (setq extra (format "-%02d:%02d"
  12683. (string-to-number (match-string 1 extra))
  12684. (string-to-number (match-string 2 extra))))
  12685. (setq extra nil)))
  12686. (when extra
  12687. (backward-char 1)
  12688. (insert-before-markers extra)
  12689. (forward-char 1))
  12690. (insert-before-markers (or post ""))
  12691. (setq org-last-inserted-timestamp stamp)))
  12692. (defun org-toggle-time-stamp-overlays ()
  12693. "Toggle the use of custom time stamp formats."
  12694. (interactive)
  12695. (setq org-display-custom-times (not org-display-custom-times))
  12696. (unless org-display-custom-times
  12697. (let ((p (point-min)) (bmp (buffer-modified-p)))
  12698. (while (setq p (next-single-property-change p 'display))
  12699. (if (and (get-text-property p 'display)
  12700. (eq (get-text-property p 'face) 'org-date))
  12701. (remove-text-properties
  12702. p (setq p (next-single-property-change p 'display))
  12703. '(display t))))
  12704. (set-buffer-modified-p bmp)))
  12705. (if (featurep 'xemacs)
  12706. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  12707. (org-restart-font-lock)
  12708. (setq org-table-may-need-update t)
  12709. (if org-display-custom-times
  12710. (message "Time stamps are overlayed with custom format")
  12711. (message "Time stamp overlays removed")))
  12712. (defun org-display-custom-time (beg end)
  12713. "Overlay modified time stamp format over timestamp between BEG and END."
  12714. (let* ((ts (buffer-substring beg end))
  12715. t1 w1 with-hm tf time str w2 (off 0))
  12716. (save-match-data
  12717. (setq t1 (org-parse-time-string ts t))
  12718. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  12719. (setq off (- (match-end 0) (match-beginning 0)))))
  12720. (setq end (- end off))
  12721. (setq w1 (- end beg)
  12722. with-hm (and (nth 1 t1) (nth 2 t1))
  12723. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  12724. time (org-fix-decoded-time t1)
  12725. str (org-add-props
  12726. (format-time-string
  12727. (substring tf 1 -1) (apply 'encode-time time))
  12728. nil 'mouse-face 'highlight)
  12729. w2 (length str))
  12730. (if (not (= w2 w1))
  12731. (add-text-properties (1+ beg) (+ 2 beg)
  12732. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  12733. (if (featurep 'xemacs)
  12734. (progn
  12735. (put-text-property beg end 'invisible t)
  12736. (put-text-property beg end 'end-glyph (make-glyph str)))
  12737. (put-text-property beg end 'display str))))
  12738. (defun org-translate-time (string)
  12739. "Translate all timestamps in STRING to custom format.
  12740. But do this only if the variable `org-display-custom-times' is set."
  12741. (when org-display-custom-times
  12742. (save-match-data
  12743. (let* ((start 0)
  12744. (re org-ts-regexp-both)
  12745. t1 with-hm inactive tf time str beg end)
  12746. (while (setq start (string-match re string start))
  12747. (setq beg (match-beginning 0)
  12748. end (match-end 0)
  12749. t1 (save-match-data
  12750. (org-parse-time-string (substring string beg end) t))
  12751. with-hm (and (nth 1 t1) (nth 2 t1))
  12752. inactive (equal (substring string beg (1+ beg)) "[")
  12753. tf (funcall (if with-hm 'cdr 'car)
  12754. org-time-stamp-custom-formats)
  12755. time (org-fix-decoded-time t1)
  12756. str (format-time-string
  12757. (concat
  12758. (if inactive "[" "<") (substring tf 1 -1)
  12759. (if inactive "]" ">"))
  12760. (apply 'encode-time time))
  12761. string (replace-match str t t string)
  12762. start (+ start (length str)))))))
  12763. string)
  12764. (defun org-fix-decoded-time (time)
  12765. "Set 0 instead of nil for the first 6 elements of time.
  12766. Don't touch the rest."
  12767. (let ((n 0))
  12768. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  12769. (defun org-days-to-time (timestamp-string)
  12770. "Difference between TIMESTAMP-STRING and now in days."
  12771. (- (time-to-days (org-time-string-to-time timestamp-string))
  12772. (time-to-days (current-time))))
  12773. (defun org-deadline-close (timestamp-string &optional ndays)
  12774. "Is the time in TIMESTAMP-STRING close to the current date?"
  12775. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  12776. (and (< (org-days-to-time timestamp-string) ndays)
  12777. (not (org-entry-is-done-p))))
  12778. (defun org-get-wdays (ts)
  12779. "Get the deadline lead time appropriate for timestring TS."
  12780. (cond
  12781. ((<= org-deadline-warning-days 0)
  12782. ;; 0 or negative, enforce this value no matter what
  12783. (- org-deadline-warning-days))
  12784. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  12785. ;; lead time is specified.
  12786. (floor (* (string-to-number (match-string 1 ts))
  12787. (cdr (assoc (match-string 2 ts)
  12788. '(("d" . 1) ("w" . 7)
  12789. ("m" . 30.4) ("y" . 365.25)))))))
  12790. ;; go for the default.
  12791. (t org-deadline-warning-days)))
  12792. (defun org-calendar-select-mouse (ev)
  12793. "Return to `org-read-date' with the date currently selected.
  12794. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12795. (interactive "e")
  12796. (mouse-set-point ev)
  12797. (when (calendar-cursor-to-date)
  12798. (let* ((date (calendar-cursor-to-date))
  12799. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12800. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12801. (if (active-minibuffer-window) (exit-minibuffer))))
  12802. (defun org-check-deadlines (ndays)
  12803. "Check if there are any deadlines due or past due.
  12804. A deadline is considered due if it happens within `org-deadline-warning-days'
  12805. days from today's date. If the deadline appears in an entry marked DONE,
  12806. it is not shown. The prefix arg NDAYS can be used to test that many
  12807. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  12808. (interactive "P")
  12809. (let* ((org-warn-days
  12810. (cond
  12811. ((equal ndays '(4)) 100000)
  12812. (ndays (prefix-numeric-value ndays))
  12813. (t (abs org-deadline-warning-days))))
  12814. (case-fold-search nil)
  12815. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  12816. (callback
  12817. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  12818. (message "%d deadlines past-due or due within %d days"
  12819. (org-occur regexp nil callback)
  12820. org-warn-days)))
  12821. (defun org-check-before-date (date)
  12822. "Check if there are deadlines or scheduled entries before DATE."
  12823. (interactive (list (org-read-date)))
  12824. (let ((case-fold-search nil)
  12825. (regexp (concat "\\<\\(" org-deadline-string
  12826. "\\|" org-scheduled-string
  12827. "\\) *<\\([^>]+\\)>"))
  12828. (callback
  12829. (lambda () (time-less-p
  12830. (org-time-string-to-time (match-string 2))
  12831. (org-time-string-to-time date)))))
  12832. (message "%d entries before %s"
  12833. (org-occur regexp nil callback) date)))
  12834. (defun org-check-after-date (date)
  12835. "Check if there are deadlines or scheduled entries after DATE."
  12836. (interactive (list (org-read-date)))
  12837. (let ((case-fold-search nil)
  12838. (regexp (concat "\\<\\(" org-deadline-string
  12839. "\\|" org-scheduled-string
  12840. "\\) *<\\([^>]+\\)>"))
  12841. (callback
  12842. (lambda () (not
  12843. (time-less-p
  12844. (org-time-string-to-time (match-string 2))
  12845. (org-time-string-to-time date))))))
  12846. (message "%d entries after %s"
  12847. (org-occur regexp nil callback) date)))
  12848. (defun org-evaluate-time-range (&optional to-buffer)
  12849. "Evaluate a time range by computing the difference between start and end.
  12850. Normally the result is just printed in the echo area, but with prefix arg
  12851. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  12852. If the time range is actually in a table, the result is inserted into the
  12853. next column.
  12854. For time difference computation, a year is assumed to be exactly 365
  12855. days in order to avoid rounding problems."
  12856. (interactive "P")
  12857. (or
  12858. (org-clock-update-time-maybe)
  12859. (save-excursion
  12860. (unless (org-at-date-range-p t)
  12861. (goto-char (point-at-bol))
  12862. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  12863. (if (not (org-at-date-range-p t))
  12864. (error "Not at a time-stamp range, and none found in current line")))
  12865. (let* ((ts1 (match-string 1))
  12866. (ts2 (match-string 2))
  12867. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  12868. (match-end (match-end 0))
  12869. (time1 (org-time-string-to-time ts1))
  12870. (time2 (org-time-string-to-time ts2))
  12871. (t1 (org-float-time time1))
  12872. (t2 (org-float-time time2))
  12873. (diff (abs (- t2 t1)))
  12874. (negative (< (- t2 t1) 0))
  12875. ;; (ys (floor (* 365 24 60 60)))
  12876. (ds (* 24 60 60))
  12877. (hs (* 60 60))
  12878. (fy "%dy %dd %02d:%02d")
  12879. (fy1 "%dy %dd")
  12880. (fd "%dd %02d:%02d")
  12881. (fd1 "%dd")
  12882. (fh "%02d:%02d")
  12883. y d h m align)
  12884. (if havetime
  12885. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12886. y 0
  12887. d (floor (/ diff ds)) diff (mod diff ds)
  12888. h (floor (/ diff hs)) diff (mod diff hs)
  12889. m (floor (/ diff 60)))
  12890. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12891. y 0
  12892. d (floor (+ (/ diff ds) 0.5))
  12893. h 0 m 0))
  12894. (if (not to-buffer)
  12895. (message "%s" (org-make-tdiff-string y d h m))
  12896. (if (org-at-table-p)
  12897. (progn
  12898. (goto-char match-end)
  12899. (setq align t)
  12900. (and (looking-at " *|") (goto-char (match-end 0))))
  12901. (goto-char match-end))
  12902. (if (looking-at
  12903. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  12904. (replace-match ""))
  12905. (if negative (insert " -"))
  12906. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  12907. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  12908. (insert " " (format fh h m))))
  12909. (if align (org-table-align))
  12910. (message "Time difference inserted")))))
  12911. (defun org-make-tdiff-string (y d h m)
  12912. (let ((fmt "")
  12913. (l nil))
  12914. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  12915. l (push y l)))
  12916. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  12917. l (push d l)))
  12918. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  12919. l (push h l)))
  12920. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  12921. l (push m l)))
  12922. (apply 'format fmt (nreverse l))))
  12923. (defun org-time-string-to-time (s)
  12924. (apply 'encode-time (org-parse-time-string s)))
  12925. (defun org-time-string-to-seconds (s)
  12926. (org-float-time (org-time-string-to-time s)))
  12927. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  12928. "Convert a time stamp to an absolute day number.
  12929. If there is a specifyer for a cyclic time stamp, get the closest date to
  12930. DAYNR.
  12931. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  12932. the variable date is bound by the calendar when this is called."
  12933. (cond
  12934. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  12935. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  12936. daynr
  12937. (+ daynr 1000)))
  12938. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  12939. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  12940. (time-to-days (current-time))) (match-string 0 s)
  12941. prefer show-all))
  12942. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  12943. (defun org-days-to-iso-week (days)
  12944. "Return the iso week number."
  12945. (require 'cal-iso)
  12946. (car (calendar-iso-from-absolute days)))
  12947. (defun org-small-year-to-year (year)
  12948. "Convert 2-digit years into 4-digit years.
  12949. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  12950. The year 2000 cannot be abbreviated. Any year larger than 99
  12951. is returned unchanged."
  12952. (if (< year 38)
  12953. (setq year (+ 2000 year))
  12954. (if (< year 100)
  12955. (setq year (+ 1900 year))))
  12956. year)
  12957. (defun org-time-from-absolute (d)
  12958. "Return the time corresponding to date D.
  12959. D may be an absolute day number, or a calendar-type list (month day year)."
  12960. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  12961. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  12962. (defun org-calendar-holiday ()
  12963. "List of holidays, for Diary display in Org-mode."
  12964. (require 'holidays)
  12965. (let ((hl (funcall
  12966. (if (fboundp 'calendar-check-holidays)
  12967. 'calendar-check-holidays 'check-calendar-holidays) date)))
  12968. (if hl (mapconcat 'identity hl "; "))))
  12969. (defun org-diary-sexp-entry (sexp entry date)
  12970. "Process a SEXP diary ENTRY for DATE."
  12971. (require 'diary-lib)
  12972. (let ((result (if calendar-debug-sexp
  12973. (let ((stack-trace-on-error t))
  12974. (eval (car (read-from-string sexp))))
  12975. (condition-case nil
  12976. (eval (car (read-from-string sexp)))
  12977. (error
  12978. (beep)
  12979. (message "Bad sexp at line %d in %s: %s"
  12980. (org-current-line)
  12981. (buffer-file-name) sexp)
  12982. (sleep-for 2))))))
  12983. (cond ((stringp result) result)
  12984. ((and (consp result)
  12985. (stringp (cdr result))) (cdr result))
  12986. (result entry)
  12987. (t nil))))
  12988. (defun org-diary-to-ical-string (frombuf)
  12989. "Get iCalendar entries from diary entries in buffer FROMBUF.
  12990. This uses the icalendar.el library."
  12991. (let* ((tmpdir (if (featurep 'xemacs)
  12992. (temp-directory)
  12993. temporary-file-directory))
  12994. (tmpfile (make-temp-name
  12995. (expand-file-name "orgics" tmpdir)))
  12996. buf rtn b e)
  12997. (with-current-buffer frombuf
  12998. (icalendar-export-region (point-min) (point-max) tmpfile)
  12999. (setq buf (find-buffer-visiting tmpfile))
  13000. (set-buffer buf)
  13001. (goto-char (point-min))
  13002. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  13003. (setq b (match-beginning 0)))
  13004. (goto-char (point-max))
  13005. (if (re-search-backward "^END:VEVENT" nil t)
  13006. (setq e (match-end 0)))
  13007. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  13008. (kill-buffer buf)
  13009. (delete-file tmpfile)
  13010. rtn))
  13011. (defun org-closest-date (start current change prefer show-all)
  13012. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  13013. When PREFER is `past' return a date that is either CURRENT or past.
  13014. When PREFER is `future', return a date that is either CURRENT or future.
  13015. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  13016. ;; Make the proper lists from the dates
  13017. (catch 'exit
  13018. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  13019. dn dw sday cday n1 n2 n0
  13020. d m y y1 y2 date1 date2 nmonths nm ny m2)
  13021. (setq start (org-date-to-gregorian start)
  13022. current (org-date-to-gregorian
  13023. (if show-all
  13024. current
  13025. (time-to-days (current-time))))
  13026. sday (calendar-absolute-from-gregorian start)
  13027. cday (calendar-absolute-from-gregorian current))
  13028. (if (<= cday sday) (throw 'exit sday))
  13029. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  13030. (setq dn (string-to-number (match-string 1 change))
  13031. dw (cdr (assoc (match-string 2 change) a1)))
  13032. (error "Invalid change specifyer: %s" change))
  13033. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  13034. (cond
  13035. ((eq dw 'day)
  13036. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  13037. n2 (+ n1 dn)))
  13038. ((eq dw 'year)
  13039. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  13040. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  13041. (setq date1 (list m d y1)
  13042. n1 (calendar-absolute-from-gregorian date1)
  13043. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  13044. n2 (calendar-absolute-from-gregorian date2)))
  13045. ((eq dw 'month)
  13046. ;; approx number of month between the two dates
  13047. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  13048. ;; How often does dn fit in there?
  13049. (setq d (nth 1 start) m (car start) y (nth 2 start)
  13050. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  13051. m (+ m nm)
  13052. ny (floor (/ m 12))
  13053. y (+ y ny)
  13054. m (- m (* ny 12)))
  13055. (while (> m 12) (setq m (- m 12) y (1+ y)))
  13056. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  13057. (setq m2 (+ m dn) y2 y)
  13058. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13059. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  13060. (while (<= n2 cday)
  13061. (setq n1 n2 m m2 y y2)
  13062. (setq m2 (+ m dn) y2 y)
  13063. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13064. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  13065. ;; Make sure n1 is the earlier date
  13066. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  13067. (if show-all
  13068. (cond
  13069. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13070. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13071. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  13072. (cond
  13073. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13074. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13075. (t (if (= cday n1) n1 n2)))))))
  13076. (defun org-date-to-gregorian (date)
  13077. "Turn any specification of DATE into a gregorian date for the calendar."
  13078. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  13079. ((and (listp date) (= (length date) 3)) date)
  13080. ((stringp date)
  13081. (setq date (org-parse-time-string date))
  13082. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  13083. ((listp date)
  13084. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  13085. (defun org-parse-time-string (s &optional nodefault)
  13086. "Parse the standard Org-mode time string.
  13087. This should be a lot faster than the normal `parse-time-string'.
  13088. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  13089. hour and minute fields will be nil if not given."
  13090. (if (string-match org-ts-regexp0 s)
  13091. (list 0
  13092. (if (or (match-beginning 8) (not nodefault))
  13093. (string-to-number (or (match-string 8 s) "0")))
  13094. (if (or (match-beginning 7) (not nodefault))
  13095. (string-to-number (or (match-string 7 s) "0")))
  13096. (string-to-number (match-string 4 s))
  13097. (string-to-number (match-string 3 s))
  13098. (string-to-number (match-string 2 s))
  13099. nil nil nil)
  13100. (error "Not a standard Org-mode time string: %s" s)))
  13101. (defun org-timestamp-up (&optional arg)
  13102. "Increase the date item at the cursor by one.
  13103. If the cursor is on the year, change the year. If it is on the month or
  13104. the day, change that.
  13105. With prefix ARG, change by that many units."
  13106. (interactive "p")
  13107. (org-timestamp-change (prefix-numeric-value arg)))
  13108. (defun org-timestamp-down (&optional arg)
  13109. "Decrease the date item at the cursor by one.
  13110. If the cursor is on the year, change the year. If it is on the month or
  13111. the day, change that.
  13112. With prefix ARG, change by that many units."
  13113. (interactive "p")
  13114. (org-timestamp-change (- (prefix-numeric-value arg))))
  13115. (defun org-timestamp-up-day (&optional arg)
  13116. "Increase the date in the time stamp by one day.
  13117. With prefix ARG, change that many days."
  13118. (interactive "p")
  13119. (if (and (not (org-at-timestamp-p t))
  13120. (org-on-heading-p))
  13121. (org-todo 'up)
  13122. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  13123. (defun org-timestamp-down-day (&optional arg)
  13124. "Decrease the date in the time stamp by one day.
  13125. With prefix ARG, change that many days."
  13126. (interactive "p")
  13127. (if (and (not (org-at-timestamp-p t))
  13128. (org-on-heading-p))
  13129. (org-todo 'down)
  13130. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  13131. (defun org-at-timestamp-p (&optional inactive-ok)
  13132. "Determine if the cursor is in or at a timestamp."
  13133. (interactive)
  13134. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  13135. (pos (point))
  13136. (ans (or (looking-at tsr)
  13137. (save-excursion
  13138. (skip-chars-backward "^[<\n\r\t")
  13139. (if (> (point) (point-min)) (backward-char 1))
  13140. (and (looking-at tsr)
  13141. (> (- (match-end 0) pos) -1))))))
  13142. (and ans
  13143. (boundp 'org-ts-what)
  13144. (setq org-ts-what
  13145. (cond
  13146. ((= pos (match-beginning 0)) 'bracket)
  13147. ((= pos (1- (match-end 0))) 'bracket)
  13148. ((org-pos-in-match-range pos 2) 'year)
  13149. ((org-pos-in-match-range pos 3) 'month)
  13150. ((org-pos-in-match-range pos 7) 'hour)
  13151. ((org-pos-in-match-range pos 8) 'minute)
  13152. ((or (org-pos-in-match-range pos 4)
  13153. (org-pos-in-match-range pos 5)) 'day)
  13154. ((and (> pos (or (match-end 8) (match-end 5)))
  13155. (< pos (match-end 0)))
  13156. (- pos (or (match-end 8) (match-end 5))))
  13157. (t 'day))))
  13158. ans))
  13159. (defun org-toggle-timestamp-type ()
  13160. "Toggle the type (<active> or [inactive]) of a time stamp."
  13161. (interactive)
  13162. (when (org-at-timestamp-p t)
  13163. (let ((beg (match-beginning 0)) (end (match-end 0))
  13164. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  13165. (save-excursion
  13166. (goto-char beg)
  13167. (while (re-search-forward "[][<>]" end t)
  13168. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  13169. t t)))
  13170. (message "Timestamp is now %sactive"
  13171. (if (equal (char-after beg) ?<) "" "in")))))
  13172. (defun org-timestamp-change (n &optional what)
  13173. "Change the date in the time stamp at point.
  13174. The date will be changed by N times WHAT. WHAT can be `day', `month',
  13175. `year', `minute', `second'. If WHAT is not given, the cursor position
  13176. in the timestamp determines what will be changed."
  13177. (let ((pos (point))
  13178. with-hm inactive
  13179. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  13180. org-ts-what
  13181. extra rem
  13182. ts time time0)
  13183. (if (not (org-at-timestamp-p t))
  13184. (error "Not at a timestamp"))
  13185. (if (and (not what) (eq org-ts-what 'bracket))
  13186. (org-toggle-timestamp-type)
  13187. (if (and (not what) (not (eq org-ts-what 'day))
  13188. org-display-custom-times
  13189. (get-text-property (point) 'display)
  13190. (not (get-text-property (1- (point)) 'display)))
  13191. (setq org-ts-what 'day))
  13192. (setq org-ts-what (or what org-ts-what)
  13193. inactive (= (char-after (match-beginning 0)) ?\[)
  13194. ts (match-string 0))
  13195. (replace-match "")
  13196. (if (string-match
  13197. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  13198. ts)
  13199. (setq extra (match-string 1 ts)))
  13200. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  13201. (setq with-hm t))
  13202. (setq time0 (org-parse-time-string ts))
  13203. (when (and (eq org-ts-what 'minute)
  13204. (eq current-prefix-arg nil))
  13205. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  13206. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  13207. (setcar (cdr time0) (+ (nth 1 time0)
  13208. (if (> n 0) (- rem) (- dm rem))))))
  13209. (setq time
  13210. (encode-time (or (car time0) 0)
  13211. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  13212. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  13213. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  13214. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  13215. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  13216. (nthcdr 6 time0)))
  13217. (when (and (member org-ts-what '(hour minute))
  13218. extra
  13219. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  13220. (setq extra (org-modify-ts-extra
  13221. extra
  13222. (if (eq org-ts-what 'hour) 2 5)
  13223. n dm)))
  13224. (when (integerp org-ts-what)
  13225. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  13226. (if (eq what 'calendar)
  13227. (let ((cal-date (org-get-date-from-calendar)))
  13228. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  13229. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  13230. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  13231. (setcar time0 (or (car time0) 0))
  13232. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  13233. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  13234. (setq time (apply 'encode-time time0))))
  13235. (setq org-last-changed-timestamp
  13236. (org-insert-time-stamp time with-hm inactive nil nil extra))
  13237. (org-clock-update-time-maybe)
  13238. (goto-char pos)
  13239. ;; Try to recenter the calendar window, if any
  13240. (if (and org-calendar-follow-timestamp-change
  13241. (get-buffer-window "*Calendar*" t)
  13242. (memq org-ts-what '(day month year)))
  13243. (org-recenter-calendar (time-to-days time))))))
  13244. (defun org-modify-ts-extra (s pos n dm)
  13245. "Change the different parts of the lead-time and repeat fields in timestamp."
  13246. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  13247. ng h m new rem)
  13248. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  13249. (cond
  13250. ((or (org-pos-in-match-range pos 2)
  13251. (org-pos-in-match-range pos 3))
  13252. (setq m (string-to-number (match-string 3 s))
  13253. h (string-to-number (match-string 2 s)))
  13254. (if (org-pos-in-match-range pos 2)
  13255. (setq h (+ h n))
  13256. (setq n (* dm (org-no-warnings (signum n))))
  13257. (when (not (= 0 (setq rem (% m dm))))
  13258. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  13259. (setq m (+ m n)))
  13260. (if (< m 0) (setq m (+ m 60) h (1- h)))
  13261. (if (> m 59) (setq m (- m 60) h (1+ h)))
  13262. (setq h (min 24 (max 0 h)))
  13263. (setq ng 1 new (format "-%02d:%02d" h m)))
  13264. ((org-pos-in-match-range pos 6)
  13265. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  13266. ((org-pos-in-match-range pos 5)
  13267. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  13268. ((org-pos-in-match-range pos 9)
  13269. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  13270. ((org-pos-in-match-range pos 8)
  13271. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  13272. (when ng
  13273. (setq s (concat
  13274. (substring s 0 (match-beginning ng))
  13275. new
  13276. (substring s (match-end ng))))))
  13277. s))
  13278. (defun org-recenter-calendar (date)
  13279. "If the calendar is visible, recenter it to DATE."
  13280. (let* ((win (selected-window))
  13281. (cwin (get-buffer-window "*Calendar*" t))
  13282. (calendar-move-hook nil))
  13283. (when cwin
  13284. (select-window cwin)
  13285. (calendar-goto-date (if (listp date) date
  13286. (calendar-gregorian-from-absolute date)))
  13287. (select-window win))))
  13288. (defun org-goto-calendar (&optional arg)
  13289. "Go to the Emacs calendar at the current date.
  13290. If there is a time stamp in the current line, go to that date.
  13291. A prefix ARG can be used to force the current date."
  13292. (interactive "P")
  13293. (let ((tsr org-ts-regexp) diff
  13294. (calendar-move-hook nil)
  13295. (calendar-view-holidays-initially-flag nil)
  13296. (view-calendar-holidays-initially nil)
  13297. (calendar-view-diary-initially-flag nil)
  13298. (view-diary-entries-initially nil))
  13299. (if (or (org-at-timestamp-p)
  13300. (save-excursion
  13301. (beginning-of-line 1)
  13302. (looking-at (concat ".*" tsr))))
  13303. (let ((d1 (time-to-days (current-time)))
  13304. (d2 (time-to-days
  13305. (org-time-string-to-time (match-string 1)))))
  13306. (setq diff (- d2 d1))))
  13307. (calendar)
  13308. (calendar-goto-today)
  13309. (if (and diff (not arg)) (calendar-forward-day diff))))
  13310. (defun org-get-date-from-calendar ()
  13311. "Return a list (month day year) of date at point in calendar."
  13312. (with-current-buffer "*Calendar*"
  13313. (save-match-data
  13314. (calendar-cursor-to-date))))
  13315. (defun org-date-from-calendar ()
  13316. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  13317. If there is already a time stamp at the cursor position, update it."
  13318. (interactive)
  13319. (if (org-at-timestamp-p t)
  13320. (org-timestamp-change 0 'calendar)
  13321. (let ((cal-date (org-get-date-from-calendar)))
  13322. (org-insert-time-stamp
  13323. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  13324. (defun org-minutes-to-hh:mm-string (m)
  13325. "Compute H:MM from a number of minutes."
  13326. (let ((h (/ m 60)))
  13327. (setq m (- m (* 60 h)))
  13328. (format org-time-clocksum-format h m)))
  13329. (defun org-hh:mm-string-to-minutes (s)
  13330. "Convert a string H:MM to a number of minutes.
  13331. If the string is just a number, interpret it as minutes.
  13332. In fact, the first hh:mm or number in the string will be taken,
  13333. there can be extra stuff in the string.
  13334. If no number is found, the return value is 0."
  13335. (cond
  13336. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  13337. (+ (* (string-to-number (match-string 1 s)) 60)
  13338. (string-to-number (match-string 2 s))))
  13339. ((string-match "\\([0-9]+\\)" s)
  13340. (string-to-number (match-string 1 s)))
  13341. (t 0)))
  13342. ;;;; Files
  13343. (defun org-save-all-org-buffers ()
  13344. "Save all Org-mode buffers without user confirmation."
  13345. (interactive)
  13346. (message "Saving all Org-mode buffers...")
  13347. (save-some-buffers t 'org-mode-p)
  13348. (when (featurep 'org-id) (org-id-locations-save))
  13349. (message "Saving all Org-mode buffers... done"))
  13350. (defun org-revert-all-org-buffers ()
  13351. "Revert all Org-mode buffers.
  13352. Prompt for confirmation when there are unsaved changes.
  13353. Be sure you know what you are doing before letting this function
  13354. overwrite your changes.
  13355. This function is useful in a setup where one tracks org files
  13356. with a version control system, to revert on one machine after pulling
  13357. changes from another. I believe the procedure must be like this:
  13358. 1. M-x org-save-all-org-buffers
  13359. 2. Pull changes from the other machine, resolve conflicts
  13360. 3. M-x org-revert-all-org-buffers"
  13361. (interactive)
  13362. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  13363. (error "Abort"))
  13364. (save-excursion
  13365. (save-window-excursion
  13366. (mapc
  13367. (lambda (b)
  13368. (when (and (with-current-buffer b (org-mode-p))
  13369. (with-current-buffer b buffer-file-name))
  13370. (switch-to-buffer b)
  13371. (revert-buffer t 'no-confirm)))
  13372. (buffer-list))
  13373. (when (and (featurep 'org-id) org-id-track-globally)
  13374. (org-id-locations-load)))))
  13375. ;;;; Agenda files
  13376. ;;;###autoload
  13377. (defun org-iswitchb (&optional arg)
  13378. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  13379. With a prefix argument, restrict available to files.
  13380. With two prefix arguments, restrict available buffers to agenda files."
  13381. (interactive "P")
  13382. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  13383. ((equal arg '(16)) (org-buffer-list 'agenda))
  13384. (t (org-buffer-list)))))
  13385. (switch-to-buffer
  13386. (org-icompleting-read "Org buffer: "
  13387. (mapcar 'list (mapcar 'buffer-name blist))
  13388. nil t))))
  13389. ;;;###autoload
  13390. (defalias 'org-ido-switchb 'org-iswitchb)
  13391. (defun org-buffer-list (&optional predicate exclude-tmp)
  13392. "Return a list of Org buffers.
  13393. PREDICATE can be `export', `files' or `agenda'.
  13394. export restrict the list to Export buffers.
  13395. files restrict the list to buffers visiting Org files.
  13396. agenda restrict the list to buffers visiting agenda files.
  13397. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  13398. (let* ((bfn nil)
  13399. (agenda-files (and (eq predicate 'agenda)
  13400. (mapcar 'file-truename (org-agenda-files t))))
  13401. (filter
  13402. (cond
  13403. ((eq predicate 'files)
  13404. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  13405. ((eq predicate 'export)
  13406. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  13407. ((eq predicate 'agenda)
  13408. (lambda (b)
  13409. (with-current-buffer b
  13410. (and (eq major-mode 'org-mode)
  13411. (setq bfn (buffer-file-name b))
  13412. (member (file-truename bfn) agenda-files)))))
  13413. (t (lambda (b) (with-current-buffer b
  13414. (or (eq major-mode 'org-mode)
  13415. (string-match "\*Org .*Export"
  13416. (buffer-name b)))))))))
  13417. (delq nil
  13418. (mapcar
  13419. (lambda(b)
  13420. (if (and (funcall filter b)
  13421. (or (not exclude-tmp)
  13422. (not (string-match "tmp" (buffer-name b)))))
  13423. b
  13424. nil))
  13425. (buffer-list)))))
  13426. (defun org-agenda-files (&optional unrestricted archives)
  13427. "Get the list of agenda files.
  13428. Optional UNRESTRICTED means return the full list even if a restriction
  13429. is currently in place.
  13430. When ARCHIVES is t, include all archive files that are really being
  13431. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  13432. `org-agenda-archives-mode' is t."
  13433. (let ((files
  13434. (cond
  13435. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  13436. ((stringp org-agenda-files) (org-read-agenda-file-list))
  13437. ((listp org-agenda-files) org-agenda-files)
  13438. (t (error "Invalid value of `org-agenda-files'")))))
  13439. (setq files (apply 'append
  13440. (mapcar (lambda (f)
  13441. (if (file-directory-p f)
  13442. (directory-files
  13443. f t org-agenda-file-regexp)
  13444. (list f)))
  13445. files)))
  13446. (when org-agenda-skip-unavailable-files
  13447. (setq files (delq nil
  13448. (mapcar (function
  13449. (lambda (file)
  13450. (and (file-readable-p file) file)))
  13451. files))))
  13452. (when (or (eq archives t)
  13453. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  13454. (setq files (org-add-archive-files files)))
  13455. files))
  13456. (defun org-edit-agenda-file-list ()
  13457. "Edit the list of agenda files.
  13458. Depending on setup, this either uses customize to edit the variable
  13459. `org-agenda-files', or it visits the file that is holding the list. In the
  13460. latter case, the buffer is set up in a way that saving it automatically kills
  13461. the buffer and restores the previous window configuration."
  13462. (interactive)
  13463. (if (stringp org-agenda-files)
  13464. (let ((cw (current-window-configuration)))
  13465. (find-file org-agenda-files)
  13466. (org-set-local 'org-window-configuration cw)
  13467. (org-add-hook 'after-save-hook
  13468. (lambda ()
  13469. (set-window-configuration
  13470. (prog1 org-window-configuration
  13471. (kill-buffer (current-buffer))))
  13472. (org-install-agenda-files-menu)
  13473. (message "New agenda file list installed"))
  13474. nil 'local)
  13475. (message "%s" (substitute-command-keys
  13476. "Edit list and finish with \\[save-buffer]")))
  13477. (customize-variable 'org-agenda-files)))
  13478. (defun org-store-new-agenda-file-list (list)
  13479. "Set new value for the agenda file list and save it correctly."
  13480. (if (stringp org-agenda-files)
  13481. (let ((fe (org-read-agenda-file-list t)) b u)
  13482. (while (setq b (find-buffer-visiting org-agenda-files))
  13483. (kill-buffer b))
  13484. (with-temp-file org-agenda-files
  13485. (insert
  13486. (mapconcat
  13487. (lambda (f) ;; Keep un-expanded entries.
  13488. (if (setq u (assoc f fe))
  13489. (cdr u)
  13490. f))
  13491. list "\n")
  13492. "\n")))
  13493. (let ((org-mode-hook nil) (org-inhibit-startup t)
  13494. (org-insert-mode-line-in-empty-file nil))
  13495. (setq org-agenda-files list)
  13496. (customize-save-variable 'org-agenda-files org-agenda-files))))
  13497. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  13498. "Read the list of agenda files from a file.
  13499. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  13500. filenames, used by `org-store-new-agenda-file-list' to write back
  13501. un-expanded file names."
  13502. (when (file-directory-p org-agenda-files)
  13503. (error "`org-agenda-files' cannot be a single directory"))
  13504. (when (stringp org-agenda-files)
  13505. (with-temp-buffer
  13506. (insert-file-contents org-agenda-files)
  13507. (mapcar
  13508. (lambda (f)
  13509. (let ((e (expand-file-name (substitute-in-file-name f)
  13510. org-directory)))
  13511. (if pair-with-expansion
  13512. (cons e f)
  13513. e)))
  13514. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  13515. ;;;###autoload
  13516. (defun org-cycle-agenda-files ()
  13517. "Cycle through the files in `org-agenda-files'.
  13518. If the current buffer visits an agenda file, find the next one in the list.
  13519. If the current buffer does not, find the first agenda file."
  13520. (interactive)
  13521. (let* ((fs (org-agenda-files t))
  13522. (files (append fs (list (car fs))))
  13523. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  13524. file)
  13525. (unless files (error "No agenda files"))
  13526. (catch 'exit
  13527. (while (setq file (pop files))
  13528. (if (equal (file-truename file) tcf)
  13529. (when (car files)
  13530. (find-file (car files))
  13531. (throw 'exit t))))
  13532. (find-file (car fs)))
  13533. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  13534. (defun org-agenda-file-to-front (&optional to-end)
  13535. "Move/add the current file to the top of the agenda file list.
  13536. If the file is not present in the list, it is added to the front. If it is
  13537. present, it is moved there. With optional argument TO-END, add/move to the
  13538. end of the list."
  13539. (interactive "P")
  13540. (let ((org-agenda-skip-unavailable-files nil)
  13541. (file-alist (mapcar (lambda (x)
  13542. (cons (file-truename x) x))
  13543. (org-agenda-files t)))
  13544. (ctf (file-truename buffer-file-name))
  13545. x had)
  13546. (setq x (assoc ctf file-alist) had x)
  13547. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  13548. (if to-end
  13549. (setq file-alist (append (delq x file-alist) (list x)))
  13550. (setq file-alist (cons x (delq x file-alist))))
  13551. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  13552. (org-install-agenda-files-menu)
  13553. (message "File %s to %s of agenda file list"
  13554. (if had "moved" "added") (if to-end "end" "front"))))
  13555. (defun org-remove-file (&optional file)
  13556. "Remove current file from the list of files in variable `org-agenda-files'.
  13557. These are the files which are being checked for agenda entries.
  13558. Optional argument FILE means use this file instead of the current."
  13559. (interactive)
  13560. (let* ((org-agenda-skip-unavailable-files nil)
  13561. (file (or file buffer-file-name))
  13562. (true-file (file-truename file))
  13563. (afile (abbreviate-file-name file))
  13564. (files (delq nil (mapcar
  13565. (lambda (x)
  13566. (if (equal true-file
  13567. (file-truename x))
  13568. nil x))
  13569. (org-agenda-files t)))))
  13570. (if (not (= (length files) (length (org-agenda-files t))))
  13571. (progn
  13572. (org-store-new-agenda-file-list files)
  13573. (org-install-agenda-files-menu)
  13574. (message "Removed file: %s" afile))
  13575. (message "File was not in list: %s (not removed)" afile))))
  13576. (defun org-file-menu-entry (file)
  13577. (vector file (list 'find-file file) t))
  13578. (defun org-check-agenda-file (file)
  13579. "Make sure FILE exists. If not, ask user what to do."
  13580. (when (not (file-exists-p file))
  13581. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  13582. (abbreviate-file-name file))
  13583. (let ((r (downcase (read-char-exclusive))))
  13584. (cond
  13585. ((equal r ?r)
  13586. (org-remove-file file)
  13587. (throw 'nextfile t))
  13588. (t (error "Abort"))))))
  13589. (defun org-get-agenda-file-buffer (file)
  13590. "Get a buffer visiting FILE. If the buffer needs to be created, add
  13591. it to the list of buffers which might be released later."
  13592. (let ((buf (org-find-base-buffer-visiting file)))
  13593. (if buf
  13594. buf ; just return it
  13595. ;; Make a new buffer and remember it
  13596. (setq buf (find-file-noselect file))
  13597. (if buf (push buf org-agenda-new-buffers))
  13598. buf)))
  13599. (defun org-release-buffers (blist)
  13600. "Release all buffers in list, asking the user for confirmation when needed.
  13601. When a buffer is unmodified, it is just killed. When modified, it is saved
  13602. \(if the user agrees) and then killed."
  13603. (let (buf file)
  13604. (while (setq buf (pop blist))
  13605. (setq file (buffer-file-name buf))
  13606. (when (and (buffer-modified-p buf)
  13607. file
  13608. (y-or-n-p (format "Save file %s? " file)))
  13609. (with-current-buffer buf (save-buffer)))
  13610. (kill-buffer buf))))
  13611. (defun org-prepare-agenda-buffers (files)
  13612. "Create buffers for all agenda files, protect archived trees and comments."
  13613. (interactive)
  13614. (let ((pa '(:org-archived t))
  13615. (pc '(:org-comment t))
  13616. (pall '(:org-archived t :org-comment t))
  13617. (inhibit-read-only t)
  13618. (rea (concat ":" org-archive-tag ":"))
  13619. bmp file re)
  13620. (save-excursion
  13621. (save-restriction
  13622. (while (setq file (pop files))
  13623. (catch 'nextfile
  13624. (if (bufferp file)
  13625. (set-buffer file)
  13626. (org-check-agenda-file file)
  13627. (set-buffer (org-get-agenda-file-buffer file)))
  13628. (widen)
  13629. (setq bmp (buffer-modified-p))
  13630. (org-refresh-category-properties)
  13631. (setq org-todo-keywords-for-agenda
  13632. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  13633. (setq org-done-keywords-for-agenda
  13634. (append org-done-keywords-for-agenda org-done-keywords))
  13635. (setq org-todo-keyword-alist-for-agenda
  13636. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  13637. (setq org-drawers-for-agenda
  13638. (append org-drawers-for-agenda org-drawers))
  13639. (setq org-tag-alist-for-agenda
  13640. (append org-tag-alist-for-agenda org-tag-alist))
  13641. (save-excursion
  13642. (remove-text-properties (point-min) (point-max) pall)
  13643. (when org-agenda-skip-archived-trees
  13644. (goto-char (point-min))
  13645. (while (re-search-forward rea nil t)
  13646. (if (org-on-heading-p t)
  13647. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  13648. (goto-char (point-min))
  13649. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  13650. (while (re-search-forward re nil t)
  13651. (add-text-properties
  13652. (match-beginning 0) (org-end-of-subtree t) pc)))
  13653. (set-buffer-modified-p bmp)))))
  13654. (setq org-todo-keyword-alist-for-agenda
  13655. (org-uniquify org-todo-keyword-alist-for-agenda)
  13656. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  13657. ;;;; Embedded LaTeX
  13658. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  13659. "Keymap for the minor `org-cdlatex-mode'.")
  13660. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  13661. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  13662. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  13663. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  13664. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  13665. (defvar org-cdlatex-texmathp-advice-is-done nil
  13666. "Flag remembering if we have applied the advice to texmathp already.")
  13667. (define-minor-mode org-cdlatex-mode
  13668. "Toggle the minor `org-cdlatex-mode'.
  13669. This mode supports entering LaTeX environment and math in LaTeX fragments
  13670. in Org-mode.
  13671. \\{org-cdlatex-mode-map}"
  13672. nil " OCDL" nil
  13673. (when org-cdlatex-mode (require 'cdlatex))
  13674. (unless org-cdlatex-texmathp-advice-is-done
  13675. (setq org-cdlatex-texmathp-advice-is-done t)
  13676. (defadvice texmathp (around org-math-always-on activate)
  13677. "Always return t in org-mode buffers.
  13678. This is because we want to insert math symbols without dollars even outside
  13679. the LaTeX math segments. If Orgmode thinks that point is actually inside
  13680. an embedded LaTeX fragment, let texmathp do its job.
  13681. \\[org-cdlatex-mode-map]"
  13682. (interactive)
  13683. (let (p)
  13684. (cond
  13685. ((not (org-mode-p)) ad-do-it)
  13686. ((eq this-command 'cdlatex-math-symbol)
  13687. (setq ad-return-value t
  13688. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  13689. (t
  13690. (let ((p (org-inside-LaTeX-fragment-p)))
  13691. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  13692. (setq ad-return-value t
  13693. texmathp-why '("Org-mode embedded math" . 0))
  13694. (if p ad-do-it)))))))))
  13695. (defun turn-on-org-cdlatex ()
  13696. "Unconditionally turn on `org-cdlatex-mode'."
  13697. (org-cdlatex-mode 1))
  13698. (defun org-inside-LaTeX-fragment-p ()
  13699. "Test if point is inside a LaTeX fragment.
  13700. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  13701. sequence appearing also before point.
  13702. Even though the matchers for math are configurable, this function assumes
  13703. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  13704. delimiters are skipped when they have been removed by customization.
  13705. The return value is nil, or a cons cell with the delimiter and
  13706. and the position of this delimiter.
  13707. This function does a reasonably good job, but can locally be fooled by
  13708. for example currency specifications. For example it will assume being in
  13709. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  13710. fragments that are properly closed, but during editing, we have to live
  13711. with the uncertainty caused by missing closing delimiters. This function
  13712. looks only before point, not after."
  13713. (catch 'exit
  13714. (let ((pos (point))
  13715. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  13716. (lim (progn
  13717. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  13718. (point)))
  13719. dd-on str (start 0) m re)
  13720. (goto-char pos)
  13721. (when dodollar
  13722. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  13723. re (nth 1 (assoc "$" org-latex-regexps)))
  13724. (while (string-match re str start)
  13725. (cond
  13726. ((= (match-end 0) (length str))
  13727. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  13728. ((= (match-end 0) (- (length str) 5))
  13729. (throw 'exit nil))
  13730. (t (setq start (match-end 0))))))
  13731. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  13732. (goto-char pos)
  13733. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  13734. (and (match-beginning 2) (throw 'exit nil))
  13735. ;; count $$
  13736. (while (re-search-backward "\\$\\$" lim t)
  13737. (setq dd-on (not dd-on)))
  13738. (goto-char pos)
  13739. (if dd-on (cons "$$" m))))))
  13740. (defun org-inside-latex-macro-p ()
  13741. "Is point inside a LaTeX macro or its arguments?"
  13742. (save-match-data
  13743. (org-in-regexp
  13744. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  13745. (defun test ()
  13746. (interactive)
  13747. (message "%s" (org-inside-latex-macro-p)))
  13748. (defun org-try-cdlatex-tab ()
  13749. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  13750. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  13751. - inside a LaTeX fragment, or
  13752. - after the first word in a line, where an abbreviation expansion could
  13753. insert a LaTeX environment."
  13754. (when org-cdlatex-mode
  13755. (cond
  13756. ((save-excursion
  13757. (skip-chars-backward "a-zA-Z0-9*")
  13758. (skip-chars-backward " \t")
  13759. (bolp))
  13760. (cdlatex-tab) t)
  13761. ((org-inside-LaTeX-fragment-p)
  13762. (cdlatex-tab) t)
  13763. (t nil))))
  13764. (defun org-cdlatex-underscore-caret (&optional arg)
  13765. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  13766. Revert to the normal definition outside of these fragments."
  13767. (interactive "P")
  13768. (if (org-inside-LaTeX-fragment-p)
  13769. (call-interactively 'cdlatex-sub-superscript)
  13770. (let (org-cdlatex-mode)
  13771. (call-interactively (key-binding (vector last-input-event))))))
  13772. (defun org-cdlatex-math-modify (&optional arg)
  13773. "Execute `cdlatex-math-modify' in LaTeX fragments.
  13774. Revert to the normal definition outside of these fragments."
  13775. (interactive "P")
  13776. (if (org-inside-LaTeX-fragment-p)
  13777. (call-interactively 'cdlatex-math-modify)
  13778. (let (org-cdlatex-mode)
  13779. (call-interactively (key-binding (vector last-input-event))))))
  13780. (defvar org-latex-fragment-image-overlays nil
  13781. "List of overlays carrying the images of latex fragments.")
  13782. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  13783. (defun org-remove-latex-fragment-image-overlays ()
  13784. "Remove all overlays with LaTeX fragment images in current buffer."
  13785. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  13786. (setq org-latex-fragment-image-overlays nil))
  13787. (defun org-preview-latex-fragment (&optional subtree)
  13788. "Preview the LaTeX fragment at point, or all locally or globally.
  13789. If the cursor is in a LaTeX fragment, create the image and overlay
  13790. it over the source code. If there is no fragment at point, display
  13791. all fragments in the current text, from one headline to the next. With
  13792. prefix SUBTREE, display all fragments in the current subtree. With a
  13793. double prefix `C-u C-u', or when the cursor is before the first headline,
  13794. display all fragments in the buffer.
  13795. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  13796. (interactive "P")
  13797. (org-remove-latex-fragment-image-overlays)
  13798. (save-excursion
  13799. (save-restriction
  13800. (let (beg end at msg)
  13801. (cond
  13802. ((or (equal subtree '(16))
  13803. (not (save-excursion
  13804. (re-search-backward (concat "^" outline-regexp) nil t))))
  13805. (setq beg (point-min) end (point-max)
  13806. msg "Creating images for buffer...%s"))
  13807. ((equal subtree '(4))
  13808. (org-back-to-heading)
  13809. (setq beg (point) end (org-end-of-subtree t)
  13810. msg "Creating images for subtree...%s"))
  13811. (t
  13812. (if (setq at (org-inside-LaTeX-fragment-p))
  13813. (goto-char (max (point-min) (- (cdr at) 2)))
  13814. (org-back-to-heading))
  13815. (setq beg (point) end (progn (outline-next-heading) (point))
  13816. msg (if at "Creating image...%s"
  13817. "Creating images for entry...%s"))))
  13818. (message msg "")
  13819. (narrow-to-region beg end)
  13820. (goto-char beg)
  13821. (org-format-latex
  13822. (concat "ltxpng/" (file-name-sans-extension
  13823. (file-name-nondirectory
  13824. buffer-file-name)))
  13825. default-directory 'overlays msg at 'forbuffer)
  13826. (message msg "done. Use `C-c C-c' to remove images.")))))
  13827. (defvar org-latex-regexps
  13828. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  13829. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  13830. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  13831. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13832. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13833. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  13834. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  13835. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  13836. "Regular expressions for matching embedded LaTeX.")
  13837. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  13838. "Replace LaTeX fragments with links to an image, and produce images.
  13839. Some of the options can be changed using the variable
  13840. `org-format-latex-options'."
  13841. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  13842. (let* ((prefixnodir (file-name-nondirectory prefix))
  13843. (absprefix (expand-file-name prefix dir))
  13844. (todir (file-name-directory absprefix))
  13845. (opt org-format-latex-options)
  13846. (matchers (plist-get opt :matchers))
  13847. (re-list org-latex-regexps)
  13848. (org-format-latex-header-extra
  13849. (plist-get (org-infile-export-plist) :latex-header-extra))
  13850. (cnt 0) txt hash link beg end re e checkdir
  13851. executables-checked
  13852. m n block linkfile movefile ov)
  13853. ;; Check the different regular expressions
  13854. (while (setq e (pop re-list))
  13855. (setq m (car e) re (nth 1 e) n (nth 2 e)
  13856. block (if (nth 3 e) "\n\n" ""))
  13857. (when (member m matchers)
  13858. (goto-char (point-min))
  13859. (while (re-search-forward re nil t)
  13860. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  13861. (not (get-text-property (match-beginning n)
  13862. 'org-protected))
  13863. (or (not overlays)
  13864. (not (eq (get-char-property (match-beginning n)
  13865. 'org-overlay-type)
  13866. 'org-latex-overlay))))
  13867. (setq txt (match-string n)
  13868. beg (match-beginning n) end (match-end n)
  13869. cnt (1+ cnt))
  13870. (let (print-length print-level) ; make sure full list is printed
  13871. (setq hash (sha1 (prin1-to-string
  13872. (list org-format-latex-header
  13873. org-format-latex-header-extra
  13874. org-export-latex-packages-alist
  13875. org-format-latex-options
  13876. forbuffer txt)))
  13877. linkfile (format "%s_%s.png" prefix hash)
  13878. movefile (format "%s_%s.png" absprefix hash)))
  13879. (setq link (concat block "[[file:" linkfile "]]" block))
  13880. (if msg (message msg cnt))
  13881. (goto-char beg)
  13882. (unless checkdir ; make sure the directory exists
  13883. (setq checkdir t)
  13884. (or (file-directory-p todir) (make-directory todir)))
  13885. (unless executables-checked
  13886. (org-check-external-command
  13887. "latex" "needed to convert LaTeX fragments to images")
  13888. (org-check-external-command
  13889. "dvipng" "needed to convert LaTeX fragments to images")
  13890. (setq executables-checked t))
  13891. (unless (file-exists-p movefile)
  13892. (org-create-formula-image
  13893. txt movefile opt forbuffer))
  13894. (if overlays
  13895. (progn
  13896. (mapc (lambda (o)
  13897. (if (eq (org-overlay-get o 'org-overlay-type)
  13898. 'org-latex-overlay)
  13899. (org-delete-overlay o)))
  13900. (org-overlays-in beg end))
  13901. (setq ov (org-make-overlay beg end))
  13902. (org-overlay-put ov 'org-overlay-type 'org-latex-overlay)
  13903. (if (featurep 'xemacs)
  13904. (progn
  13905. (org-overlay-put ov 'invisible t)
  13906. (org-overlay-put
  13907. ov 'end-glyph
  13908. (make-glyph (vector 'png :file movefile))))
  13909. (org-overlay-put
  13910. ov 'display
  13911. (list 'image :type 'png :file movefile :ascent 'center)))
  13912. (push ov org-latex-fragment-image-overlays)
  13913. (goto-char end))
  13914. (delete-region beg end)
  13915. (insert link))))))))
  13916. ;; This function borrows from Ganesh Swami's latex2png.el
  13917. (defun org-create-formula-image (string tofile options buffer)
  13918. "This calls dvipng."
  13919. (require 'org-latex)
  13920. (let* ((tmpdir (if (featurep 'xemacs)
  13921. (temp-directory)
  13922. temporary-file-directory))
  13923. (texfilebase (make-temp-name
  13924. (expand-file-name "orgtex" tmpdir)))
  13925. (texfile (concat texfilebase ".tex"))
  13926. (dvifile (concat texfilebase ".dvi"))
  13927. (pngfile (concat texfilebase ".png"))
  13928. (fnh (if (featurep 'xemacs)
  13929. (font-height (get-face-font 'default))
  13930. (face-attribute 'default :height nil)))
  13931. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  13932. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  13933. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  13934. "Black"))
  13935. (bg (or (plist-get options (if buffer :background :html-background))
  13936. "Transparent")))
  13937. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  13938. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  13939. (with-temp-file texfile
  13940. (insert org-format-latex-header
  13941. (if org-export-latex-packages-alist
  13942. (concat "\n"
  13943. (mapconcat (lambda(p)
  13944. (if (equal "" (car p))
  13945. (format "\\usepackage{%s}" (cadr p))
  13946. (format "\\usepackage[%s]{%s}"
  13947. (car p) (cadr p))))
  13948. org-export-latex-packages-alist "\n"))
  13949. "")
  13950. (if org-format-latex-header-extra
  13951. (concat "\n" org-format-latex-header-extra)
  13952. "")
  13953. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  13954. (let ((dir default-directory))
  13955. (condition-case nil
  13956. (progn
  13957. (cd tmpdir)
  13958. (call-process "latex" nil nil nil texfile))
  13959. (error nil))
  13960. (cd dir))
  13961. (if (not (file-exists-p dvifile))
  13962. (progn (message "Failed to create dvi file from %s" texfile) nil)
  13963. (condition-case nil
  13964. (call-process "dvipng" nil nil nil
  13965. "-fg" fg "-bg" bg
  13966. "-D" dpi
  13967. ;;"-x" scale "-y" scale
  13968. "-T" "tight"
  13969. "-o" pngfile
  13970. dvifile)
  13971. (error nil))
  13972. (if (not (file-exists-p pngfile))
  13973. (if org-format-latex-signal-error
  13974. (error "Failed to create png file from %s" texfile)
  13975. (message "Failed to create png file from %s" texfile)
  13976. nil)
  13977. ;; Use the requested file name and clean up
  13978. (copy-file pngfile tofile 'replace)
  13979. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  13980. (delete-file (concat texfilebase e)))
  13981. pngfile))))
  13982. (defun org-dvipng-color (attr)
  13983. "Return an rgb color specification for dvipng."
  13984. (apply 'format "rgb %s %s %s"
  13985. (mapcar 'org-normalize-color
  13986. (color-values (face-attribute 'default attr nil)))))
  13987. (defun org-normalize-color (value)
  13988. "Return string to be used as color value for an RGB component."
  13989. (format "%g" (/ value 65535.0)))
  13990. ;;;; Key bindings
  13991. ;; Make `C-c C-x' a prefix key
  13992. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  13993. ;; TAB key with modifiers
  13994. (org-defkey org-mode-map "\C-i" 'org-cycle)
  13995. (org-defkey org-mode-map [(tab)] 'org-cycle)
  13996. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  13997. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  13998. (org-defkey org-mode-map "\M-\t" 'org-complete)
  13999. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  14000. ;; The following line is necessary under Suse GNU/Linux
  14001. (unless (featurep 'xemacs)
  14002. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  14003. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  14004. (define-key org-mode-map [backtab] 'org-shifttab)
  14005. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  14006. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  14007. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  14008. ;; Cursor keys with modifiers
  14009. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  14010. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  14011. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  14012. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  14013. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  14014. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  14015. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  14016. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  14017. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  14018. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  14019. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  14020. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  14021. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  14022. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  14023. ;;; Extra keys for tty access.
  14024. ;; We only set them when really needed because otherwise the
  14025. ;; menus don't show the simple keys
  14026. (when (or org-use-extra-keys
  14027. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  14028. (not window-system))
  14029. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  14030. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  14031. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  14032. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  14033. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  14034. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  14035. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  14036. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  14037. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  14038. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  14039. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  14040. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  14041. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  14042. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  14043. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  14044. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  14045. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  14046. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  14047. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  14048. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  14049. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  14050. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  14051. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  14052. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  14053. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  14054. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  14055. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  14056. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  14057. ;; All the other keys
  14058. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  14059. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  14060. (if (boundp 'narrow-map)
  14061. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  14062. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  14063. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  14064. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  14065. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  14066. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  14067. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  14068. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  14069. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  14070. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  14071. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  14072. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  14073. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  14074. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  14075. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  14076. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  14077. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  14078. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  14079. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  14080. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  14081. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  14082. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  14083. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  14084. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  14085. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  14086. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  14087. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  14088. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  14089. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  14090. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  14091. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  14092. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  14093. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  14094. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  14095. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  14096. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  14097. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  14098. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  14099. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  14100. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  14101. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  14102. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  14103. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  14104. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14105. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  14106. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  14107. (org-defkey org-mode-map "\C-c^" 'org-sort)
  14108. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  14109. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  14110. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  14111. (org-defkey org-mode-map "\C-m" 'org-return)
  14112. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  14113. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  14114. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  14115. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  14116. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  14117. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  14118. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  14119. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  14120. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  14121. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  14122. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  14123. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  14124. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  14125. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  14126. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  14127. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  14128. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  14129. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  14130. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  14131. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  14132. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  14133. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  14134. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  14135. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  14136. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  14137. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  14138. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  14139. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  14140. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  14141. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  14142. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  14143. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  14144. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  14145. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  14146. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  14147. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  14148. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  14149. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  14150. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  14151. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  14152. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  14153. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  14154. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  14155. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  14156. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  14157. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  14158. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  14159. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  14160. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  14161. (when (featurep 'xemacs)
  14162. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  14163. (defconst org-speed-commands-default
  14164. '(
  14165. ("Outline Navigation")
  14166. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  14167. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  14168. ("f" . (org-speed-move-safe 'org-forward-same-level))
  14169. ("b" . (org-speed-move-safe 'org-backward-same-level))
  14170. ("u" . (org-speed-move-safe 'outline-up-heading))
  14171. ("j" . org-goto)
  14172. ("g" . (org-refile t))
  14173. ("Outline Visibility")
  14174. ("c" . org-cycle)
  14175. ("C" . org-shifttab)
  14176. (" " . org-display-outline-path)
  14177. ("Outline Structure Editing")
  14178. ("U" . org-shiftmetaup)
  14179. ("D" . org-shiftmetadown)
  14180. ("r" . org-metaright)
  14181. ("l" . org-metaleft)
  14182. ("R" . org-shiftmetaright)
  14183. ("L" . org-shiftmetaleft)
  14184. ("i" . (progn (forward-char 1) (call-interactively
  14185. 'org-insert-heading-respect-content)))
  14186. ("^" . org-sort)
  14187. ("w" . org-refile)
  14188. ("a" . org-archive-subtree-default-with-confirmation)
  14189. ("." . outline-mark-subtree)
  14190. ("Clock Commands")
  14191. ("I" . org-clock-in)
  14192. ("O" . org-clock-out)
  14193. ("Meta Data Editing")
  14194. ("t" . org-todo)
  14195. ("0" . (org-priority ?\ ))
  14196. ("1" . (org-priority ?A))
  14197. ("2" . (org-priority ?B))
  14198. ("3" . (org-priority ?C))
  14199. (";" . org-set-tags-command)
  14200. ("e" . org-set-effort)
  14201. ("Agenda Views etc")
  14202. ("v" . org-agenda)
  14203. ("/" . org-sparse-tree)
  14204. ("Misc")
  14205. ("o" . org-open-at-point)
  14206. ("?" . org-speed-command-help)
  14207. )
  14208. "The default speed commands.")
  14209. (defun org-print-speed-command (e)
  14210. (if (> (length (car e)) 1)
  14211. (progn
  14212. (princ "\n")
  14213. (princ (car e))
  14214. (princ "\n")
  14215. (princ (make-string (length (car e)) ?-))
  14216. (princ "\n"))
  14217. (princ (car e))
  14218. (princ " ")
  14219. (if (symbolp (cdr e))
  14220. (princ (symbol-name (cdr e)))
  14221. (prin1 (cdr e)))
  14222. (princ "\n")))
  14223. (defun org-speed-command-help ()
  14224. "Show the available speed commands."
  14225. (interactive)
  14226. (if (not org-use-speed-commands)
  14227. (error "Speed commands are not activated, customize `org-use-speed-commands'.")
  14228. (with-output-to-temp-buffer "*Help*"
  14229. (princ "User-defined Speed commands\n===========================\n")
  14230. (mapc 'org-print-speed-command org-speed-commands-user)
  14231. (princ "\n")
  14232. (princ "Built-in Speed commands\n=======================\n")
  14233. (mapc 'org-print-speed-command org-speed-commands-default))
  14234. (with-current-buffer "*Help*"
  14235. (setq truncate-lines t))))
  14236. (defun org-speed-move-safe (cmd)
  14237. "Execute CMD, but make sure that the cursor always ends up in a headline.
  14238. If not, return to the original position and throw an error."
  14239. (interactive)
  14240. (let ((pos (point)))
  14241. (call-interactively cmd)
  14242. (unless (and (bolp) (org-on-heading-p))
  14243. (goto-char pos)
  14244. (error "Boundary reached while executing %s" cmd))))
  14245. (defvar org-self-insert-command-undo-counter 0)
  14246. (defvar org-table-auto-blank-field) ; defined in org-table.el
  14247. (defvar org-speed-command nil)
  14248. (defun org-self-insert-command (N)
  14249. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  14250. If the cursor is in a table looking at whitespace, the whitespace is
  14251. overwritten, and the table is not marked as requiring realignment."
  14252. (interactive "p")
  14253. (cond
  14254. ((and org-use-speed-commands
  14255. (or (and (bolp) (looking-at outline-regexp))
  14256. (and (functionp org-use-speed-commands)
  14257. (funcall org-use-speed-commands)))
  14258. (setq
  14259. org-speed-command
  14260. (or (cdr (assoc (this-command-keys) org-speed-commands-user))
  14261. (cdr (assoc (this-command-keys) org-speed-commands-default)))))
  14262. (cond
  14263. ((commandp org-speed-command)
  14264. (setq this-command org-speed-command)
  14265. (call-interactively org-speed-command))
  14266. ((functionp org-speed-command)
  14267. (funcall org-speed-command))
  14268. ((and org-speed-command (listp org-speed-command))
  14269. (eval org-speed-command))
  14270. (t (let (org-use-speed-commands)
  14271. (call-interactively 'org-self-insert-command)))))
  14272. ((and
  14273. (org-table-p)
  14274. (progn
  14275. ;; check if we blank the field, and if that triggers align
  14276. (and (featurep 'org-table) org-table-auto-blank-field
  14277. (member last-command
  14278. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  14279. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  14280. ;; got extra space, this field does not determine column width
  14281. (let (org-table-may-need-update) (org-table-blank-field))
  14282. ;; no extra space, this field may determine column width
  14283. (org-table-blank-field)))
  14284. t)
  14285. (eq N 1)
  14286. (looking-at "[^|\n]* |"))
  14287. (let (org-table-may-need-update)
  14288. (goto-char (1- (match-end 0)))
  14289. (delete-backward-char 1)
  14290. (goto-char (match-beginning 0))
  14291. (self-insert-command N)))
  14292. (t
  14293. (setq org-table-may-need-update t)
  14294. (self-insert-command N)
  14295. (org-fix-tags-on-the-fly)
  14296. (if org-self-insert-cluster-for-undo
  14297. (if (not (eq last-command 'org-self-insert-command))
  14298. (setq org-self-insert-command-undo-counter 1)
  14299. (if (>= org-self-insert-command-undo-counter 20)
  14300. (setq org-self-insert-command-undo-counter 1)
  14301. (and (> org-self-insert-command-undo-counter 0)
  14302. buffer-undo-list
  14303. (not (cadr buffer-undo-list)) ; remove nil entry
  14304. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  14305. (setq org-self-insert-command-undo-counter
  14306. (1+ org-self-insert-command-undo-counter))))))))
  14307. (defun org-fix-tags-on-the-fly ()
  14308. (when (and (equal (char-after (point-at-bol)) ?*)
  14309. (org-on-heading-p))
  14310. (org-align-tags-here org-tags-column)))
  14311. (defun org-delete-backward-char (N)
  14312. "Like `delete-backward-char', insert whitespace at field end in tables.
  14313. When deleting backwards, in tables this function will insert whitespace in
  14314. front of the next \"|\" separator, to keep the table aligned. The table will
  14315. still be marked for re-alignment if the field did fill the entire column,
  14316. because, in this case the deletion might narrow the column."
  14317. (interactive "p")
  14318. (if (and (org-table-p)
  14319. (eq N 1)
  14320. (string-match "|" (buffer-substring (point-at-bol) (point)))
  14321. (looking-at ".*?|"))
  14322. (let ((pos (point))
  14323. (noalign (looking-at "[^|\n\r]* |"))
  14324. (c org-table-may-need-update))
  14325. (backward-delete-char N)
  14326. (skip-chars-forward "^|")
  14327. (insert " ")
  14328. (goto-char (1- pos))
  14329. ;; noalign: if there were two spaces at the end, this field
  14330. ;; does not determine the width of the column.
  14331. (if noalign (setq org-table-may-need-update c)))
  14332. (backward-delete-char N)
  14333. (org-fix-tags-on-the-fly)))
  14334. (defun org-delete-char (N)
  14335. "Like `delete-char', but insert whitespace at field end in tables.
  14336. When deleting characters, in tables this function will insert whitespace in
  14337. front of the next \"|\" separator, to keep the table aligned. The table will
  14338. still be marked for re-alignment if the field did fill the entire column,
  14339. because, in this case the deletion might narrow the column."
  14340. (interactive "p")
  14341. (if (and (org-table-p)
  14342. (not (bolp))
  14343. (not (= (char-after) ?|))
  14344. (eq N 1))
  14345. (if (looking-at ".*?|")
  14346. (let ((pos (point))
  14347. (noalign (looking-at "[^|\n\r]* |"))
  14348. (c org-table-may-need-update))
  14349. (replace-match (concat
  14350. (substring (match-string 0) 1 -1)
  14351. " |"))
  14352. (goto-char pos)
  14353. ;; noalign: if there were two spaces at the end, this field
  14354. ;; does not determine the width of the column.
  14355. (if noalign (setq org-table-may-need-update c)))
  14356. (delete-char N))
  14357. (delete-char N)
  14358. (org-fix-tags-on-the-fly)))
  14359. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  14360. (put 'org-self-insert-command 'delete-selection t)
  14361. (put 'orgtbl-self-insert-command 'delete-selection t)
  14362. (put 'org-delete-char 'delete-selection 'supersede)
  14363. (put 'org-delete-backward-char 'delete-selection 'supersede)
  14364. (put 'org-yank 'delete-selection 'yank)
  14365. ;; Make `flyspell-mode' delay after some commands
  14366. (put 'org-self-insert-command 'flyspell-delayed t)
  14367. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  14368. (put 'org-delete-char 'flyspell-delayed t)
  14369. (put 'org-delete-backward-char 'flyspell-delayed t)
  14370. ;; Make pabbrev-mode expand after org-mode commands
  14371. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  14372. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  14373. ;; How to do this: Measure non-white length of current string
  14374. ;; If equal to column width, we should realign.
  14375. (defun org-remap (map &rest commands)
  14376. "In MAP, remap the functions given in COMMANDS.
  14377. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  14378. (let (new old)
  14379. (while commands
  14380. (setq old (pop commands) new (pop commands))
  14381. (if (fboundp 'command-remapping)
  14382. (org-defkey map (vector 'remap old) new)
  14383. (substitute-key-definition old new map global-map)))))
  14384. (when (eq org-enable-table-editor 'optimized)
  14385. ;; If the user wants maximum table support, we need to hijack
  14386. ;; some standard editing functions
  14387. (org-remap org-mode-map
  14388. 'self-insert-command 'org-self-insert-command
  14389. 'delete-char 'org-delete-char
  14390. 'delete-backward-char 'org-delete-backward-char)
  14391. (org-defkey org-mode-map "|" 'org-force-self-insert))
  14392. (defvar org-ctrl-c-ctrl-c-hook nil
  14393. "Hook for functions attaching themselves to `C-c C-c'.
  14394. This can be used to add additional functionality to the C-c C-c key which
  14395. executes context-dependent commands.
  14396. Each function will be called with no arguments. The function must check
  14397. if the context is appropriate for it to act. If yes, it should do its
  14398. thing and then return a non-nil value. If the context is wrong,
  14399. just do nothing and return nil.")
  14400. (defvar org-tab-first-hook nil
  14401. "Hook for functions to attach themselves to TAB.
  14402. See `org-ctrl-c-ctrl-c-hook' for more information.
  14403. This hook runs as the first action when TAB is pressed, even before
  14404. `org-cycle' messes around with the `outline-regexp' to cater for
  14405. inline tasks and plain list item folding.
  14406. If any function in this hook returns t, not other actions like table
  14407. field motion visibility cycling will be done.")
  14408. (defvar org-tab-after-check-for-table-hook nil
  14409. "Hook for functions to attach themselves to TAB.
  14410. See `org-ctrl-c-ctrl-c-hook' for more information.
  14411. This hook runs after it has been established that the cursor is not in a
  14412. table, but before checking if the cursor is in a headline or if global cycling
  14413. should be done.
  14414. If any function in this hook returns t, not other actions like visibility
  14415. cycling will be done.")
  14416. (defvar org-tab-after-check-for-cycling-hook nil
  14417. "Hook for functions to attach themselves to TAB.
  14418. See `org-ctrl-c-ctrl-c-hook' for more information.
  14419. This hook runs after it has been established that not table field motion and
  14420. not visibility should be done because of current context. This is probably
  14421. the place where a package like yasnippets can hook in.")
  14422. (defvar org-tab-before-tab-emulation-hook nil
  14423. "Hook for functions to attach themselves to TAB.
  14424. See `org-ctrl-c-ctrl-c-hook' for more information.
  14425. This hook runs after every other options for TAB have been exhausted, but
  14426. before indentation and \t insertion takes place.")
  14427. (defvar org-metaleft-hook nil
  14428. "Hook for functions attaching themselves to `M-left'.
  14429. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14430. (defvar org-metaright-hook nil
  14431. "Hook for functions attaching themselves to `M-right'.
  14432. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14433. (defvar org-metaup-hook nil
  14434. "Hook for functions attaching themselves to `M-up'.
  14435. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14436. (defvar org-metadown-hook nil
  14437. "Hook for functions attaching themselves to `M-down'.
  14438. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14439. (defvar org-shiftmetaleft-hook nil
  14440. "Hook for functions attaching themselves to `M-S-left'.
  14441. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14442. (defvar org-shiftmetaright-hook nil
  14443. "Hook for functions attaching themselves to `M-S-right'.
  14444. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14445. (defvar org-shiftmetaup-hook nil
  14446. "Hook for functions attaching themselves to `M-S-up'.
  14447. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14448. (defvar org-shiftmetadown-hook nil
  14449. "Hook for functions attaching themselves to `M-S-down'.
  14450. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14451. (defvar org-metareturn-hook nil
  14452. "Hook for functions attaching themselves to `M-RET'.
  14453. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14454. (defun org-modifier-cursor-error ()
  14455. "Throw an error, a modified cursor command was applied in wrong context."
  14456. (error "This command is active in special context like tables, headlines or items"))
  14457. (defun org-shiftselect-error ()
  14458. "Throw an error because Shift-Cursor command was applied in wrong context."
  14459. (if (and (boundp 'shift-select-mode) shift-select-mode)
  14460. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  14461. (error "This command works only in special context like headlines or timestamps")))
  14462. (defun org-call-for-shift-select (cmd)
  14463. (let ((this-command-keys-shift-translated t))
  14464. (call-interactively cmd)))
  14465. (defun org-shifttab (&optional arg)
  14466. "Global visibility cycling or move to previous table field.
  14467. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  14468. on context.
  14469. See the individual commands for more information."
  14470. (interactive "P")
  14471. (cond
  14472. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  14473. ((integerp arg)
  14474. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  14475. (message "Content view to level: %d" arg)
  14476. (org-content (prefix-numeric-value arg2))
  14477. (setq org-cycle-global-status 'overview)))
  14478. (t (call-interactively 'org-global-cycle))))
  14479. (defun org-shiftmetaleft ()
  14480. "Promote subtree or delete table column.
  14481. Calls `org-promote-subtree', `org-outdent-item',
  14482. or `org-table-delete-column', depending on context.
  14483. See the individual commands for more information."
  14484. (interactive)
  14485. (cond
  14486. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  14487. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  14488. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  14489. ((org-at-item-p) (call-interactively 'org-outdent-item))
  14490. (t (org-modifier-cursor-error))))
  14491. (defun org-shiftmetaright ()
  14492. "Demote subtree or insert table column.
  14493. Calls `org-demote-subtree', `org-indent-item',
  14494. or `org-table-insert-column', depending on context.
  14495. See the individual commands for more information."
  14496. (interactive)
  14497. (cond
  14498. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  14499. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  14500. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  14501. ((org-at-item-p) (call-interactively 'org-indent-item))
  14502. (t (org-modifier-cursor-error))))
  14503. (defun org-shiftmetaup (&optional arg)
  14504. "Move subtree up or kill table row.
  14505. Calls `org-move-subtree-up' or `org-table-kill-row' or
  14506. `org-move-item-up' depending on context. See the individual commands
  14507. for more information."
  14508. (interactive "P")
  14509. (cond
  14510. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  14511. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  14512. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14513. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14514. (t (org-modifier-cursor-error))))
  14515. (defun org-shiftmetadown (&optional arg)
  14516. "Move subtree down or insert table row.
  14517. Calls `org-move-subtree-down' or `org-table-insert-row' or
  14518. `org-move-item-down', depending on context. See the individual
  14519. commands for more information."
  14520. (interactive "P")
  14521. (cond
  14522. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  14523. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  14524. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14525. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14526. (t (org-modifier-cursor-error))))
  14527. (defun org-metaleft (&optional arg)
  14528. "Promote heading or move table column to left.
  14529. Calls `org-do-promote' or `org-table-move-column', depending on context.
  14530. With no specific context, calls the Emacs default `backward-word'.
  14531. See the individual commands for more information."
  14532. (interactive "P")
  14533. (cond
  14534. ((run-hook-with-args-until-success 'org-metaleft-hook))
  14535. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  14536. ((or (org-on-heading-p)
  14537. (and (org-region-active-p)
  14538. (save-excursion
  14539. (goto-char (region-beginning))
  14540. (org-on-heading-p))))
  14541. (call-interactively 'org-do-promote))
  14542. ((or (org-at-item-p)
  14543. (and (org-region-active-p)
  14544. (save-excursion
  14545. (goto-char (region-beginning))
  14546. (org-at-item-p))))
  14547. (call-interactively 'org-outdent-item))
  14548. (t (call-interactively 'backward-word))))
  14549. (defun org-metaright (&optional arg)
  14550. "Demote subtree or move table column to right.
  14551. Calls `org-do-demote' or `org-table-move-column', depending on context.
  14552. With no specific context, calls the Emacs default `forward-word'.
  14553. See the individual commands for more information."
  14554. (interactive "P")
  14555. (cond
  14556. ((run-hook-with-args-until-success 'org-metaright-hook))
  14557. ((org-at-table-p) (call-interactively 'org-table-move-column))
  14558. ((or (org-on-heading-p)
  14559. (and (org-region-active-p)
  14560. (save-excursion
  14561. (goto-char (region-beginning))
  14562. (org-on-heading-p))))
  14563. (call-interactively 'org-do-demote))
  14564. ((or (org-at-item-p)
  14565. (and (org-region-active-p)
  14566. (save-excursion
  14567. (goto-char (region-beginning))
  14568. (org-at-item-p))))
  14569. (call-interactively 'org-indent-item))
  14570. (t (call-interactively 'forward-word))))
  14571. (defun org-metaup (&optional arg)
  14572. "Move subtree up or move table row up.
  14573. Calls `org-move-subtree-up' or `org-table-move-row' or
  14574. `org-move-item-up', depending on context. See the individual commands
  14575. for more information."
  14576. (interactive "P")
  14577. (cond
  14578. ((run-hook-with-args-until-success 'org-metaup-hook))
  14579. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  14580. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14581. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14582. (t (transpose-lines 1) (beginning-of-line -1))))
  14583. (defun org-metadown (&optional arg)
  14584. "Move subtree down or move table row down.
  14585. Calls `org-move-subtree-down' or `org-table-move-row' or
  14586. `org-move-item-down', depending on context. See the individual
  14587. commands for more information."
  14588. (interactive "P")
  14589. (cond
  14590. ((run-hook-with-args-until-success 'org-metadown-hook))
  14591. ((org-at-table-p) (call-interactively 'org-table-move-row))
  14592. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14593. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14594. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  14595. (defun org-shiftup (&optional arg)
  14596. "Increase item in timestamp or increase priority of current headline.
  14597. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  14598. depending on context. See the individual commands for more information."
  14599. (interactive "P")
  14600. (cond
  14601. ((and org-support-shift-select (org-region-active-p))
  14602. (org-call-for-shift-select 'previous-line))
  14603. ((org-at-timestamp-p t)
  14604. (call-interactively (if org-edit-timestamp-down-means-later
  14605. 'org-timestamp-down 'org-timestamp-up)))
  14606. ((and (not (eq org-support-shift-select 'always))
  14607. org-enable-priority-commands
  14608. (org-on-heading-p))
  14609. (call-interactively 'org-priority-up))
  14610. ((and (not org-support-shift-select) (org-at-item-p))
  14611. (call-interactively 'org-previous-item))
  14612. ((org-clocktable-try-shift 'up arg))
  14613. (org-support-shift-select
  14614. (org-call-for-shift-select 'previous-line))
  14615. (t (org-shiftselect-error))))
  14616. (defun org-shiftdown (&optional arg)
  14617. "Decrease item in timestamp or decrease priority of current headline.
  14618. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  14619. depending on context. See the individual commands for more information."
  14620. (interactive "P")
  14621. (cond
  14622. ((and org-support-shift-select (org-region-active-p))
  14623. (org-call-for-shift-select 'next-line))
  14624. ((org-at-timestamp-p t)
  14625. (call-interactively (if org-edit-timestamp-down-means-later
  14626. 'org-timestamp-up 'org-timestamp-down)))
  14627. ((and (not (eq org-support-shift-select 'always))
  14628. org-enable-priority-commands
  14629. (org-on-heading-p))
  14630. (call-interactively 'org-priority-down))
  14631. ((and (not org-support-shift-select) (org-at-item-p))
  14632. (call-interactively 'org-next-item))
  14633. ((org-clocktable-try-shift 'down arg))
  14634. (org-support-shift-select
  14635. (org-call-for-shift-select 'next-line))
  14636. (t (org-shiftselect-error))))
  14637. (defun org-shiftright (&optional arg)
  14638. "Cycle the thing at point or in the current line, depending on context.
  14639. Depending on context, this does one of the following:
  14640. - switch a timestamp at point one day into the future
  14641. - on a headline, switch to the next TODO keyword.
  14642. - on an item, switch entire list to the next bullet type
  14643. - on a property line, switch to the next allowed value
  14644. - on a clocktable definition line, move time block into the future"
  14645. (interactive "P")
  14646. (cond
  14647. ((and org-support-shift-select (org-region-active-p))
  14648. (org-call-for-shift-select 'forward-char))
  14649. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  14650. ((and (not (eq org-support-shift-select 'always))
  14651. (org-on-heading-p))
  14652. (let ((org-inhibit-logging
  14653. (not org-treat-S-cursor-todo-selection-as-state-change))
  14654. (org-inhibit-blocking
  14655. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14656. (org-call-with-arg 'org-todo 'right)))
  14657. ((or (and org-support-shift-select
  14658. (not (eq org-support-shift-select 'always))
  14659. (org-at-item-bullet-p))
  14660. (and (not org-support-shift-select) (org-at-item-p)))
  14661. (org-call-with-arg 'org-cycle-list-bullet nil))
  14662. ((and (not (eq org-support-shift-select 'always))
  14663. (org-at-property-p))
  14664. (call-interactively 'org-property-next-allowed-value))
  14665. ((org-clocktable-try-shift 'right arg))
  14666. (org-support-shift-select
  14667. (org-call-for-shift-select 'forward-char))
  14668. (t (org-shiftselect-error))))
  14669. (defun org-shiftleft (&optional arg)
  14670. "Cycle the thing at point or in the current line, depending on context.
  14671. Depending on context, this does one of the following:
  14672. - switch a timestamp at point one day into the past
  14673. - on a headline, switch to the previous TODO keyword.
  14674. - on an item, switch entire list to the previous bullet type
  14675. - on a property line, switch to the previous allowed value
  14676. - on a clocktable definition line, move time block into the past"
  14677. (interactive "P")
  14678. (cond
  14679. ((and org-support-shift-select (org-region-active-p))
  14680. (org-call-for-shift-select 'backward-char))
  14681. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  14682. ((and (not (eq org-support-shift-select 'always))
  14683. (org-on-heading-p))
  14684. (let ((org-inhibit-logging
  14685. (not org-treat-S-cursor-todo-selection-as-state-change))
  14686. (org-inhibit-blocking
  14687. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14688. (org-call-with-arg 'org-todo 'left)))
  14689. ((or (and org-support-shift-select
  14690. (not (eq org-support-shift-select 'always))
  14691. (org-at-item-bullet-p))
  14692. (and (not org-support-shift-select) (org-at-item-p)))
  14693. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  14694. ((and (not (eq org-support-shift-select 'always))
  14695. (org-at-property-p))
  14696. (call-interactively 'org-property-previous-allowed-value))
  14697. ((org-clocktable-try-shift 'left arg))
  14698. (org-support-shift-select
  14699. (org-call-for-shift-select 'backward-char))
  14700. (t (org-shiftselect-error))))
  14701. (defun org-shiftcontrolright ()
  14702. "Switch to next TODO set."
  14703. (interactive)
  14704. (cond
  14705. ((and org-support-shift-select (org-region-active-p))
  14706. (org-call-for-shift-select 'forward-word))
  14707. ((and (not (eq org-support-shift-select 'always))
  14708. (org-on-heading-p))
  14709. (org-call-with-arg 'org-todo 'nextset))
  14710. (org-support-shift-select
  14711. (org-call-for-shift-select 'forward-word))
  14712. (t (org-shiftselect-error))))
  14713. (defun org-shiftcontrolleft ()
  14714. "Switch to previous TODO set."
  14715. (interactive)
  14716. (cond
  14717. ((and org-support-shift-select (org-region-active-p))
  14718. (org-call-for-shift-select 'backward-word))
  14719. ((and (not (eq org-support-shift-select 'always))
  14720. (org-on-heading-p))
  14721. (org-call-with-arg 'org-todo 'previousset))
  14722. (org-support-shift-select
  14723. (org-call-for-shift-select 'backward-word))
  14724. (t (org-shiftselect-error))))
  14725. (defun org-ctrl-c-ret ()
  14726. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  14727. (interactive)
  14728. (cond
  14729. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  14730. (t (call-interactively 'org-insert-heading))))
  14731. (defun org-copy-special ()
  14732. "Copy region in table or copy current subtree.
  14733. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  14734. See the individual commands for more information."
  14735. (interactive)
  14736. (call-interactively
  14737. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  14738. (defun org-cut-special ()
  14739. "Cut region in table or cut current subtree.
  14740. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  14741. See the individual commands for more information."
  14742. (interactive)
  14743. (call-interactively
  14744. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  14745. (defun org-paste-special (arg)
  14746. "Paste rectangular region into table, or past subtree relative to level.
  14747. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  14748. See the individual commands for more information."
  14749. (interactive "P")
  14750. (if (org-at-table-p)
  14751. (org-table-paste-rectangle)
  14752. (org-paste-subtree arg)))
  14753. (defun org-edit-special ()
  14754. "Call a special editor for the stuff at point.
  14755. When at a table, call the formula editor with `org-table-edit-formulas'.
  14756. When at the first line of an src example, call `org-edit-src-code'.
  14757. When in an #+include line, visit the include file. Otherwise call
  14758. `ffap' to visit the file at point."
  14759. (interactive)
  14760. (cond
  14761. ((org-at-table.el-p)
  14762. (org-edit-src-code))
  14763. ((org-at-table-p)
  14764. (call-interactively 'org-table-edit-formulas))
  14765. ((save-excursion
  14766. (beginning-of-line 1)
  14767. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  14768. (find-file (org-trim (match-string 1))))
  14769. ((org-edit-src-code))
  14770. ((org-edit-fixed-width-region))
  14771. (t (call-interactively 'ffap))))
  14772. (defun org-ctrl-c-ctrl-c (&optional arg)
  14773. "Set tags in headline, or update according to changed information at point.
  14774. This command does many different things, depending on context:
  14775. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  14776. this is what we do.
  14777. - If the cursor is on a statistics cookie, update it.
  14778. - If the cursor is in a headline, prompt for tags and insert them
  14779. into the current line, aligned to `org-tags-column'. When called
  14780. with prefix arg, realign all tags in the current buffer.
  14781. - If the cursor is in one of the special #+KEYWORD lines, this
  14782. triggers scanning the buffer for these lines and updating the
  14783. information.
  14784. - If the cursor is inside a table, realign the table. This command
  14785. works even if the automatic table editor has been turned off.
  14786. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  14787. the entire table.
  14788. - If the cursor is at a footnote reference or definition, jump to
  14789. the corresponding definition or references, respectively.
  14790. - If the cursor is a the beginning of a dynamic block, update it.
  14791. - If the current buffer is a remember buffer, close note and file
  14792. it. A prefix argument of 1 files to the default location
  14793. without further interaction. A prefix argument of 2 files to
  14794. the currently clocking task.
  14795. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  14796. links in this buffer.
  14797. - If the cursor is on a numbered item in a plain list, renumber the
  14798. ordered list.
  14799. - If the cursor is on a checkbox, toggle it."
  14800. (interactive "P")
  14801. (let ((org-enable-table-editor t))
  14802. (cond
  14803. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  14804. org-occur-highlights
  14805. org-latex-fragment-image-overlays)
  14806. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  14807. (org-remove-occur-highlights)
  14808. (org-remove-latex-fragment-image-overlays)
  14809. (message "Temporary highlights/overlays removed from current buffer"))
  14810. ((and (local-variable-p 'org-finish-function (current-buffer))
  14811. (fboundp org-finish-function))
  14812. (funcall org-finish-function))
  14813. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  14814. ((or (looking-at (org-re org-property-start-re))
  14815. (org-at-property-p))
  14816. (call-interactively 'org-property-action))
  14817. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  14818. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  14819. (or (org-on-heading-p) (org-at-item-p)))
  14820. (call-interactively 'org-update-statistics-cookies))
  14821. ((org-on-heading-p) (call-interactively 'org-set-tags))
  14822. ((org-at-table.el-p)
  14823. (message "Use C-c ' to edit table.el tables"))
  14824. ((org-at-table-p)
  14825. (org-table-maybe-eval-formula)
  14826. (if arg
  14827. (call-interactively 'org-table-recalculate)
  14828. (org-table-maybe-recalculate-line))
  14829. (call-interactively 'org-table-align))
  14830. ((or (org-footnote-at-reference-p)
  14831. (org-footnote-at-definition-p))
  14832. (call-interactively 'org-footnote-action))
  14833. ((org-at-item-checkbox-p)
  14834. (call-interactively 'org-toggle-checkbox))
  14835. ((org-at-item-p)
  14836. (if arg
  14837. (call-interactively 'org-toggle-checkbox)
  14838. (call-interactively 'org-maybe-renumber-ordered-list)))
  14839. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  14840. ;; Dynamic block
  14841. (beginning-of-line 1)
  14842. (save-excursion (org-update-dblock)))
  14843. ((save-excursion
  14844. (beginning-of-line 1)
  14845. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  14846. (cond
  14847. ((equal (match-string 1) "TBLFM")
  14848. ;; Recalculate the table before this line
  14849. (save-excursion
  14850. (beginning-of-line 1)
  14851. (skip-chars-backward " \r\n\t")
  14852. (if (org-at-table-p)
  14853. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  14854. (t
  14855. (let ((org-inhibit-startup-visibility-stuff t)
  14856. (org-startup-align-all-tables nil))
  14857. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  14858. (message "Local setup has been refreshed"))))
  14859. ((org-clock-update-time-maybe))
  14860. (t (error "C-c C-c can do nothing useful at this location")))))
  14861. (defun org-mode-restart ()
  14862. "Restart Org-mode, to scan again for special lines.
  14863. Also updates the keyword regular expressions."
  14864. (interactive)
  14865. (org-mode)
  14866. (message "Org-mode restarted"))
  14867. (defun org-kill-note-or-show-branches ()
  14868. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  14869. (interactive)
  14870. (if (not org-finish-function)
  14871. (call-interactively 'show-branches)
  14872. (let ((org-note-abort t))
  14873. (funcall org-finish-function))))
  14874. (defun org-return (&optional indent)
  14875. "Goto next table row or insert a newline.
  14876. Calls `org-table-next-row' or `newline', depending on context.
  14877. See the individual commands for more information."
  14878. (interactive)
  14879. (cond
  14880. ((bobp) (if indent (newline-and-indent) (newline)))
  14881. ((org-at-table-p)
  14882. (org-table-justify-field-maybe)
  14883. (call-interactively 'org-table-next-row))
  14884. ((and org-return-follows-link
  14885. (eq (get-text-property (point) 'face) 'org-link))
  14886. (call-interactively 'org-open-at-point))
  14887. ((and (org-at-heading-p)
  14888. (looking-at
  14889. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  14890. (org-show-entry)
  14891. (end-of-line 1)
  14892. (newline))
  14893. (t (if indent (newline-and-indent) (newline)))))
  14894. (defun org-return-indent ()
  14895. "Goto next table row or insert a newline and indent.
  14896. Calls `org-table-next-row' or `newline-and-indent', depending on
  14897. context. See the individual commands for more information."
  14898. (interactive)
  14899. (org-return t))
  14900. (defun org-ctrl-c-star ()
  14901. "Compute table, or change heading status of lines.
  14902. Calls `org-table-recalculate' or `org-toggle-heading',
  14903. depending on context."
  14904. (interactive)
  14905. (cond
  14906. ((org-at-table-p)
  14907. (call-interactively 'org-table-recalculate))
  14908. (t
  14909. ;; Convert all lines in region to list items
  14910. (call-interactively 'org-toggle-heading))))
  14911. (defun org-ctrl-c-minus ()
  14912. "Insert separator line in table or modify bullet status of line.
  14913. Also turns a plain line or a region of lines into list items.
  14914. Calls `org-table-insert-hline', `org-toggle-item', or
  14915. `org-cycle-list-bullet', depending on context."
  14916. (interactive)
  14917. (cond
  14918. ((org-at-table-p)
  14919. (call-interactively 'org-table-insert-hline))
  14920. ((org-region-active-p)
  14921. (call-interactively 'org-toggle-item))
  14922. ((org-in-item-p)
  14923. (call-interactively 'org-cycle-list-bullet))
  14924. (t
  14925. (call-interactively 'org-toggle-item))))
  14926. (defun org-toggle-item ()
  14927. "Convert headings or normal lines to items, items to normal lines.
  14928. If there is no active region, only the current line is considered.
  14929. If the first line in the region is a headline, convert all headlines to items.
  14930. If the first line in the region is an item, convert all items to normal lines.
  14931. If the first line is normal text, add an item bullet to each line."
  14932. (interactive)
  14933. (let (l2 l beg end)
  14934. (if (org-region-active-p)
  14935. (setq beg (region-beginning) end (region-end))
  14936. (setq beg (point-at-bol)
  14937. end (min (1+ (point-at-eol)) (point-max))))
  14938. (save-excursion
  14939. (goto-char end)
  14940. (setq l2 (org-current-line))
  14941. (goto-char beg)
  14942. (beginning-of-line 1)
  14943. (setq l (1- (org-current-line)))
  14944. (if (org-at-item-p)
  14945. ;; We already have items, de-itemize
  14946. (while (< (setq l (1+ l)) l2)
  14947. (when (org-at-item-p)
  14948. (goto-char (match-beginning 2))
  14949. (delete-region (match-beginning 2) (match-end 2))
  14950. (and (looking-at "[ \t]+") (replace-match "")))
  14951. (beginning-of-line 2))
  14952. (if (org-on-heading-p)
  14953. ;; Headings, convert to items
  14954. (while (< (setq l (1+ l)) l2)
  14955. (if (looking-at org-outline-regexp)
  14956. (replace-match "- " t t))
  14957. (beginning-of-line 2))
  14958. ;; normal lines, turn them into items
  14959. (while (< (setq l (1+ l)) l2)
  14960. (unless (org-at-item-p)
  14961. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  14962. (replace-match "\\1- \\2")))
  14963. (beginning-of-line 2)))))))
  14964. (defun org-toggle-heading (&optional nstars)
  14965. "Convert headings to normal text, or items or text to headings.
  14966. If there is no active region, only the current line is considered.
  14967. If the first line is a heading, remove the stars from all headlines
  14968. in the region.
  14969. If the first line is a plain list item, turn all plain list items
  14970. into headings.
  14971. If the first line is a normal line, turn each and every line in the
  14972. region into a heading.
  14973. When converting a line into a heading, the number of stars is chosen
  14974. such that the lines become children of the current entry. However,
  14975. when a prefix argument is given, its value determines the number of
  14976. stars to add."
  14977. (interactive "P")
  14978. (let (l2 l itemp beg end)
  14979. (if (org-region-active-p)
  14980. (setq beg (region-beginning) end (region-end))
  14981. (setq beg (point-at-bol)
  14982. end (min (1+ (point-at-eol)) (point-max))))
  14983. (save-excursion
  14984. (goto-char end)
  14985. (setq l2 (org-current-line))
  14986. (goto-char beg)
  14987. (beginning-of-line 1)
  14988. (setq l (1- (org-current-line)))
  14989. (if (org-on-heading-p)
  14990. ;; We already have headlines, de-star them
  14991. (while (< (setq l (1+ l)) l2)
  14992. (when (org-on-heading-p t)
  14993. (and (looking-at outline-regexp) (replace-match "")))
  14994. (beginning-of-line 2))
  14995. (setq itemp (org-at-item-p))
  14996. (let* ((stars
  14997. (if nstars
  14998. (make-string (prefix-numeric-value current-prefix-arg)
  14999. ?*)
  15000. (save-excursion
  15001. (if (re-search-backward org-complex-heading-regexp nil t)
  15002. (match-string 1) ""))))
  15003. (add-stars (cond (nstars "")
  15004. ((equal stars "") "*")
  15005. (org-odd-levels-only "**")
  15006. (t "*")))
  15007. (rpl (concat stars add-stars " ")))
  15008. (while (< (setq l (1+ l)) l2)
  15009. (if itemp
  15010. (and (org-at-item-p) (replace-match rpl t t))
  15011. (unless (org-on-heading-p)
  15012. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15013. (replace-match (concat rpl (match-string 2))))))
  15014. (beginning-of-line 2)))))))
  15015. (defun org-meta-return (&optional arg)
  15016. "Insert a new heading or wrap a region in a table.
  15017. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  15018. See the individual commands for more information."
  15019. (interactive "P")
  15020. (cond
  15021. ((run-hook-with-args-until-success 'org-metareturn-hook))
  15022. ((org-at-table-p)
  15023. (call-interactively 'org-table-wrap-region))
  15024. (t (call-interactively 'org-insert-heading))))
  15025. ;;; Menu entries
  15026. ;; Define the Org-mode menus
  15027. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  15028. '("Tbl"
  15029. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  15030. ["Next Field" org-cycle (org-at-table-p)]
  15031. ["Previous Field" org-shifttab (org-at-table-p)]
  15032. ["Next Row" org-return (org-at-table-p)]
  15033. "--"
  15034. ["Blank Field" org-table-blank-field (org-at-table-p)]
  15035. ["Edit Field" org-table-edit-field (org-at-table-p)]
  15036. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  15037. "--"
  15038. ("Column"
  15039. ["Move Column Left" org-metaleft (org-at-table-p)]
  15040. ["Move Column Right" org-metaright (org-at-table-p)]
  15041. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  15042. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  15043. ("Row"
  15044. ["Move Row Up" org-metaup (org-at-table-p)]
  15045. ["Move Row Down" org-metadown (org-at-table-p)]
  15046. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  15047. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  15048. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  15049. "--"
  15050. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  15051. ("Rectangle"
  15052. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  15053. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  15054. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  15055. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  15056. "--"
  15057. ("Calculate"
  15058. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  15059. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  15060. ["Edit Formulas" org-edit-special (org-at-table-p)]
  15061. "--"
  15062. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  15063. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  15064. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  15065. "--"
  15066. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  15067. "--"
  15068. ["Sum Column/Rectangle" org-table-sum
  15069. (or (org-at-table-p) (org-region-active-p))]
  15070. ["Which Column?" org-table-current-column (org-at-table-p)])
  15071. ["Debug Formulas"
  15072. org-table-toggle-formula-debugger
  15073. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  15074. ["Show Col/Row Numbers"
  15075. org-table-toggle-coordinate-overlays
  15076. :style toggle
  15077. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  15078. "--"
  15079. ["Create" org-table-create (and (not (org-at-table-p))
  15080. org-enable-table-editor)]
  15081. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  15082. ["Import from File" org-table-import (not (org-at-table-p))]
  15083. ["Export to File" org-table-export (org-at-table-p)]
  15084. "--"
  15085. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  15086. (easy-menu-define org-org-menu org-mode-map "Org menu"
  15087. '("Org"
  15088. ("Show/Hide"
  15089. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  15090. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  15091. ["Sparse Tree..." org-sparse-tree t]
  15092. ["Reveal Context" org-reveal t]
  15093. ["Show All" show-all t]
  15094. "--"
  15095. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  15096. "--"
  15097. ["New Heading" org-insert-heading t]
  15098. ("Navigate Headings"
  15099. ["Up" outline-up-heading t]
  15100. ["Next" outline-next-visible-heading t]
  15101. ["Previous" outline-previous-visible-heading t]
  15102. ["Next Same Level" outline-forward-same-level t]
  15103. ["Previous Same Level" outline-backward-same-level t]
  15104. "--"
  15105. ["Jump" org-goto t])
  15106. ("Edit Structure"
  15107. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  15108. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  15109. "--"
  15110. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  15111. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  15112. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  15113. "--"
  15114. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  15115. "--"
  15116. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  15117. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  15118. ["Demote Heading" org-metaright (not (org-at-table-p))]
  15119. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  15120. "--"
  15121. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  15122. "--"
  15123. ["Convert to odd levels" org-convert-to-odd-levels t]
  15124. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  15125. ("Editing"
  15126. ["Emphasis..." org-emphasize t]
  15127. ["Edit Source Example" org-edit-special t]
  15128. "--"
  15129. ["Footnote new/jump" org-footnote-action t]
  15130. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  15131. ("Archive"
  15132. ["Archive (default method)" org-archive-subtree-default t]
  15133. "--"
  15134. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  15135. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  15136. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  15137. )
  15138. "--"
  15139. ("Hyperlinks"
  15140. ["Store Link (Global)" org-store-link t]
  15141. ["Find existing link to here" org-occur-link-in-agenda-files t]
  15142. ["Insert Link" org-insert-link t]
  15143. ["Follow Link" org-open-at-point t]
  15144. "--"
  15145. ["Next link" org-next-link t]
  15146. ["Previous link" org-previous-link t]
  15147. "--"
  15148. ["Descriptive Links"
  15149. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  15150. :style radio
  15151. :selected (member '(org-link) buffer-invisibility-spec)]
  15152. ["Literal Links"
  15153. (progn
  15154. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  15155. :style radio
  15156. :selected (not (member '(org-link) buffer-invisibility-spec))])
  15157. "--"
  15158. ("TODO Lists"
  15159. ["TODO/DONE/-" org-todo t]
  15160. ("Select keyword"
  15161. ["Next keyword" org-shiftright (org-on-heading-p)]
  15162. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  15163. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  15164. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  15165. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  15166. ["Show TODO Tree" org-show-todo-tree t]
  15167. ["Global TODO list" org-todo-list t]
  15168. "--"
  15169. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  15170. :selected org-enforce-todo-dependencies :style toggle :active t]
  15171. "Settings for tree at point"
  15172. ["Do Children sequentially" org-toggle-ordered-property :style radio
  15173. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  15174. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  15175. ["Do Children parallel" org-toggle-ordered-property :style radio
  15176. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  15177. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  15178. "--"
  15179. ["Set Priority" org-priority t]
  15180. ["Priority Up" org-shiftup t]
  15181. ["Priority Down" org-shiftdown t]
  15182. "--"
  15183. ["Get news from all feeds" org-feed-update-all t]
  15184. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  15185. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  15186. ("TAGS and Properties"
  15187. ["Set Tags" org-set-tags-command t]
  15188. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  15189. "--"
  15190. ["Set property" org-set-property t]
  15191. ["Column view of properties" org-columns t]
  15192. ["Insert Column View DBlock" org-insert-columns-dblock t])
  15193. ("Dates and Scheduling"
  15194. ["Timestamp" org-time-stamp t]
  15195. ["Timestamp (inactive)" org-time-stamp-inactive t]
  15196. ("Change Date"
  15197. ["1 Day Later" org-shiftright t]
  15198. ["1 Day Earlier" org-shiftleft t]
  15199. ["1 ... Later" org-shiftup t]
  15200. ["1 ... Earlier" org-shiftdown t])
  15201. ["Compute Time Range" org-evaluate-time-range t]
  15202. ["Schedule Item" org-schedule t]
  15203. ["Deadline" org-deadline t]
  15204. "--"
  15205. ["Custom time format" org-toggle-time-stamp-overlays
  15206. :style radio :selected org-display-custom-times]
  15207. "--"
  15208. ["Goto Calendar" org-goto-calendar t]
  15209. ["Date from Calendar" org-date-from-calendar t]
  15210. "--"
  15211. ["Start/Restart Timer" org-timer-start t]
  15212. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  15213. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  15214. ["Insert Timer String" org-timer t]
  15215. ["Insert Timer Item" org-timer-item t])
  15216. ("Logging work"
  15217. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  15218. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  15219. ["Clock out" org-clock-out t]
  15220. ["Clock cancel" org-clock-cancel t]
  15221. "--"
  15222. ["Mark as default task" org-clock-mark-default-task t]
  15223. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  15224. ["Goto running clock" org-clock-goto t]
  15225. "--"
  15226. ["Display times" org-clock-display t]
  15227. ["Create clock table" org-clock-report t]
  15228. "--"
  15229. ["Record DONE time"
  15230. (progn (setq org-log-done (not org-log-done))
  15231. (message "Switching to %s will %s record a timestamp"
  15232. (car org-done-keywords)
  15233. (if org-log-done "automatically" "not")))
  15234. :style toggle :selected org-log-done])
  15235. "--"
  15236. ["Agenda Command..." org-agenda t]
  15237. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  15238. ("File List for Agenda")
  15239. ("Special views current file"
  15240. ["TODO Tree" org-show-todo-tree t]
  15241. ["Check Deadlines" org-check-deadlines t]
  15242. ["Timeline" org-timeline t]
  15243. ["Tags/Property tree" org-match-sparse-tree t])
  15244. "--"
  15245. ["Export/Publish..." org-export t]
  15246. ("LaTeX"
  15247. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  15248. :selected org-cdlatex-mode]
  15249. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  15250. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  15251. ["Modify math symbol" org-cdlatex-math-modify
  15252. (org-inside-LaTeX-fragment-p)]
  15253. ["Insert citation" org-reftex-citation t]
  15254. "--"
  15255. ["Export LaTeX fragments as images"
  15256. (if (featurep 'org-exp)
  15257. (setq org-export-with-LaTeX-fragments
  15258. (not org-export-with-LaTeX-fragments))
  15259. (require 'org-exp))
  15260. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  15261. org-export-with-LaTeX-fragments)]
  15262. "--"
  15263. ["Template for BEAMER" org-beamer-settings-template t])
  15264. "--"
  15265. ("MobileOrg"
  15266. ["Push Files and Views" org-mobile-push t]
  15267. ["Get Captured and Flagged" org-mobile-pull t]
  15268. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  15269. "--"
  15270. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  15271. "--"
  15272. ("Documentation"
  15273. ["Show Version" org-version t]
  15274. ["Info Documentation" org-info t])
  15275. ("Customize"
  15276. ["Browse Org Group" org-customize t]
  15277. "--"
  15278. ["Expand This Menu" org-create-customize-menu
  15279. (fboundp 'customize-menu-create)])
  15280. ["Send bug report" org-submit-bug-report t]
  15281. "--"
  15282. ("Refresh/Reload"
  15283. ["Refresh setup current buffer" org-mode-restart t]
  15284. ["Reload Org (after update)" org-reload t]
  15285. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  15286. ))
  15287. (defun org-info (&optional node)
  15288. "Read documentation for Org-mode in the info system.
  15289. With optional NODE, go directly to that node."
  15290. (interactive)
  15291. (info (format "(org)%s" (or node ""))))
  15292. ;;;###autoload
  15293. (defun org-submit-bug-report ()
  15294. "Submit a bug report on Org-mode via mail.
  15295. Don't hesitate to report any problems or inaccurate documentation.
  15296. If you don't have setup sending mail from (X)Emacs, please copy the
  15297. output buffer into your mail program, as it gives us important
  15298. information about your Org-mode version and configuration."
  15299. (interactive)
  15300. (require 'reporter)
  15301. (org-load-modules-maybe)
  15302. (org-require-autoloaded-modules)
  15303. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  15304. (reporter-submit-bug-report
  15305. "emacs-orgmode@gnu.org"
  15306. (org-version)
  15307. (let (list)
  15308. (save-window-excursion
  15309. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  15310. (delete-other-windows)
  15311. (erase-buffer)
  15312. (insert "You are about to submit a bug report to the Org-mode mailing list.
  15313. We would like to add your full Org-mode and Outline configuration to the
  15314. bug report. This greatly simplifies the work of the maintainer and
  15315. other experts on the mailing list.
  15316. HOWEVER, some variables you have customized may contain private
  15317. information. The names of customers, colleagues, or friends, might
  15318. appear in the form of file names, tags, todo states, or search strings.
  15319. If you answer yes to the prompt, you might want to check and remove
  15320. such private information before sending the email.")
  15321. (add-text-properties (point-min) (point-max) '(face org-warning))
  15322. (when (yes-or-no-p "Include your Org-mode configuration ")
  15323. (mapatoms
  15324. (lambda (v)
  15325. (and (boundp v)
  15326. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  15327. (or (and (symbol-value v)
  15328. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  15329. (and
  15330. (get v 'custom-type) (get v 'standard-value)
  15331. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  15332. (push v list)))))
  15333. (kill-buffer (get-buffer "*Warn about privacy*"))
  15334. list))
  15335. nil nil
  15336. "Remember to cover the basics, that is, what you expected to happen and
  15337. what in fact did happen. You don't know how to make a good report? See
  15338. http://orgmode.org/manual/Feedback.html#Feedback
  15339. Your bug report will be posted to the Org-mode mailing list.
  15340. ------------------------------------------------------------------------")
  15341. (save-excursion
  15342. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  15343. (replace-match "\\1Bug: \\3 [\\2]")))))
  15344. (defun org-install-agenda-files-menu ()
  15345. (let ((bl (buffer-list)))
  15346. (save-excursion
  15347. (while bl
  15348. (set-buffer (pop bl))
  15349. (if (org-mode-p) (setq bl nil)))
  15350. (when (org-mode-p)
  15351. (easy-menu-change
  15352. '("Org") "File List for Agenda"
  15353. (append
  15354. (list
  15355. ["Edit File List" (org-edit-agenda-file-list) t]
  15356. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  15357. ["Remove Current File from List" org-remove-file t]
  15358. ["Cycle through agenda files" org-cycle-agenda-files t]
  15359. ["Occur in all agenda files" org-occur-in-agenda-files t]
  15360. "--")
  15361. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  15362. ;;;; Documentation
  15363. ;;;###autoload
  15364. (defun org-require-autoloaded-modules ()
  15365. (interactive)
  15366. (mapc 'require
  15367. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  15368. org-docbook org-exp org-html org-icalendar
  15369. org-id org-latex
  15370. org-publish org-remember org-table
  15371. org-timer org-xoxo)))
  15372. ;;;###autoload
  15373. (defun org-reload (&optional uncompiled)
  15374. "Reload all org lisp files.
  15375. With prefix arg UNCOMPILED, load the uncompiled versions."
  15376. (interactive "P")
  15377. (require 'find-func)
  15378. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  15379. (dir-org (file-name-directory (org-find-library-name "org")))
  15380. (dir-org-contrib (ignore-errors
  15381. (file-name-directory
  15382. (org-find-library-name "org-contribdir"))))
  15383. (files
  15384. (append (directory-files dir-org t file-re)
  15385. (and dir-org-contrib
  15386. (directory-files dir-org-contrib t file-re))))
  15387. (remove-re (concat (if (featurep 'xemacs)
  15388. "org-colview" "org-colview-xemacs")
  15389. "\\'")))
  15390. (setq files (mapcar 'file-name-sans-extension files))
  15391. (setq files (mapcar
  15392. (lambda (x) (if (string-match remove-re x) nil x))
  15393. files))
  15394. (setq files (delq nil files))
  15395. (mapc
  15396. (lambda (f)
  15397. (when (featurep (intern (file-name-nondirectory f)))
  15398. (if (and (not uncompiled)
  15399. (file-exists-p (concat f ".elc")))
  15400. (load (concat f ".elc") nil nil t)
  15401. (load (concat f ".el") nil nil t))))
  15402. files))
  15403. (org-version))
  15404. ;;;###autoload
  15405. (defun org-customize ()
  15406. "Call the customize function with org as argument."
  15407. (interactive)
  15408. (org-load-modules-maybe)
  15409. (org-require-autoloaded-modules)
  15410. (customize-browse 'org))
  15411. (defun org-create-customize-menu ()
  15412. "Create a full customization menu for Org-mode, insert it into the menu."
  15413. (interactive)
  15414. (org-load-modules-maybe)
  15415. (org-require-autoloaded-modules)
  15416. (if (fboundp 'customize-menu-create)
  15417. (progn
  15418. (easy-menu-change
  15419. '("Org") "Customize"
  15420. `(["Browse Org group" org-customize t]
  15421. "--"
  15422. ,(customize-menu-create 'org)
  15423. ["Set" Custom-set t]
  15424. ["Save" Custom-save t]
  15425. ["Reset to Current" Custom-reset-current t]
  15426. ["Reset to Saved" Custom-reset-saved t]
  15427. ["Reset to Standard Settings" Custom-reset-standard t]))
  15428. (message "\"Org\"-menu now contains full customization menu"))
  15429. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  15430. ;;;; Miscellaneous stuff
  15431. ;;; Generally useful functions
  15432. (defun org-get-at-bol (property)
  15433. "Get text property PROPERTY at beginning of line."
  15434. (get-text-property (point-at-bol) property))
  15435. (defun org-find-text-property-in-string (prop s)
  15436. "Return the first non-nil value of property PROP in string S."
  15437. (or (get-text-property 0 prop s)
  15438. (get-text-property (or (next-single-property-change 0 prop s) 0)
  15439. prop s)))
  15440. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  15441. "Display the given MESSAGE as a warning."
  15442. (if (fboundp 'display-warning)
  15443. (display-warning 'org message
  15444. (if (featurep 'xemacs)
  15445. 'warning
  15446. :warning))
  15447. (let ((buf (get-buffer-create "*Org warnings*")))
  15448. (with-current-buffer buf
  15449. (goto-char (point-max))
  15450. (insert "Warning (Org): " message)
  15451. (unless (bolp)
  15452. (newline)))
  15453. (display-buffer buf)
  15454. (sit-for 0))))
  15455. (defun org-in-commented-line ()
  15456. "Is point in a line starting with `#'?"
  15457. (equal (char-after (point-at-bol)) ?#))
  15458. (defun org-in-verbatim-emphasis ()
  15459. (save-match-data
  15460. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  15461. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  15462. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  15463. (if (and marker (marker-buffer marker)
  15464. (buffer-live-p (marker-buffer marker)))
  15465. (progn
  15466. (switch-to-buffer (marker-buffer marker))
  15467. (if (or (> marker (point-max)) (< marker (point-min)))
  15468. (widen))
  15469. (goto-char marker)
  15470. (org-show-context 'org-goto))
  15471. (if bookmark
  15472. (bookmark-jump bookmark)
  15473. (error "Cannot find location"))))
  15474. (defun org-quote-csv-field (s)
  15475. "Quote field for inclusion in CSV material."
  15476. (if (string-match "[\",]" s)
  15477. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  15478. s))
  15479. (defun org-plist-delete (plist property)
  15480. "Delete PROPERTY from PLIST.
  15481. This is in contrast to merely setting it to 0."
  15482. (let (p)
  15483. (while plist
  15484. (if (not (eq property (car plist)))
  15485. (setq p (plist-put p (car plist) (nth 1 plist))))
  15486. (setq plist (cddr plist)))
  15487. p))
  15488. (defun org-force-self-insert (N)
  15489. "Needed to enforce self-insert under remapping."
  15490. (interactive "p")
  15491. (self-insert-command N))
  15492. (defun org-string-width (s)
  15493. "Compute width of string, ignoring invisible characters.
  15494. This ignores character with invisibility property `org-link', and also
  15495. characters with property `org-cwidth', because these will become invisible
  15496. upon the next fontification round."
  15497. (let (b l)
  15498. (when (or (eq t buffer-invisibility-spec)
  15499. (assq 'org-link buffer-invisibility-spec))
  15500. (while (setq b (text-property-any 0 (length s)
  15501. 'invisible 'org-link s))
  15502. (setq s (concat (substring s 0 b)
  15503. (substring s (or (next-single-property-change
  15504. b 'invisible s) (length s)))))))
  15505. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  15506. (setq s (concat (substring s 0 b)
  15507. (substring s (or (next-single-property-change
  15508. b 'org-cwidth s) (length s))))))
  15509. (setq l (string-width s) b -1)
  15510. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  15511. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  15512. l))
  15513. (defun org-get-indentation (&optional line)
  15514. "Get the indentation of the current line, interpreting tabs.
  15515. When LINE is given, assume it represents a line and compute its indentation."
  15516. (if line
  15517. (if (string-match "^ *" (org-remove-tabs line))
  15518. (match-end 0))
  15519. (save-excursion
  15520. (beginning-of-line 1)
  15521. (skip-chars-forward " \t")
  15522. (current-column))))
  15523. (defun org-remove-tabs (s &optional width)
  15524. "Replace tabulators in S with spaces.
  15525. Assumes that s is a single line, starting in column 0."
  15526. (setq width (or width tab-width))
  15527. (while (string-match "\t" s)
  15528. (setq s (replace-match
  15529. (make-string
  15530. (- (* width (/ (+ (match-beginning 0) width) width))
  15531. (match-beginning 0)) ?\ )
  15532. t t s)))
  15533. s)
  15534. (defun org-fix-indentation (line ind)
  15535. "Fix indentation in LINE.
  15536. IND is a cons cell with target and minimum indentation.
  15537. If the current indentation in LINE is smaller than the minimum,
  15538. leave it alone. If it is larger than ind, set it to the target."
  15539. (let* ((l (org-remove-tabs line))
  15540. (i (org-get-indentation l))
  15541. (i1 (car ind)) (i2 (cdr ind)))
  15542. (if (>= i i2) (setq l (substring line i2)))
  15543. (if (> i1 0)
  15544. (concat (make-string i1 ?\ ) l)
  15545. l)))
  15546. (defun org-remove-indentation (code &optional n)
  15547. "Remove the maximum common indentation from the lines in CODE.
  15548. N may optionally be the number of spaces to remove."
  15549. (with-temp-buffer
  15550. (insert code)
  15551. (org-do-remove-indentation n)
  15552. (buffer-string)))
  15553. (defun org-do-remove-indentation (&optional n)
  15554. "Remove the maximum common indentation from the buffer."
  15555. (untabify (point-min) (point-max))
  15556. (let ((min 10000) re)
  15557. (if n
  15558. (setq min n)
  15559. (goto-char (point-min))
  15560. (while (re-search-forward "^ *[^ \n]" nil t)
  15561. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  15562. (unless (or (= min 0) (= min 10000))
  15563. (setq re (format "^ \\{%d\\}" min))
  15564. (goto-char (point-min))
  15565. (while (re-search-forward re nil t)
  15566. (replace-match "")
  15567. (end-of-line 1))
  15568. min)))
  15569. (defun org-fill-template (template alist)
  15570. "Find each %key of ALIST in TEMPLATE and replace it."
  15571. (let ((case-fold-search nil)
  15572. entry key value)
  15573. (setq alist (sort (copy-sequence alist)
  15574. (lambda (a b) (< (length (car a)) (length (car b))))))
  15575. (while (setq entry (pop alist))
  15576. (setq template
  15577. (replace-regexp-in-string
  15578. (concat "%" (regexp-quote (car entry)))
  15579. (cdr entry) template t t)))
  15580. template))
  15581. (defun org-base-buffer (buffer)
  15582. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  15583. (if (not buffer)
  15584. buffer
  15585. (or (buffer-base-buffer buffer)
  15586. buffer)))
  15587. (defun org-trim (s)
  15588. "Remove whitespace at beginning and end of string."
  15589. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  15590. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  15591. s)
  15592. (defun org-wrap (string &optional width lines)
  15593. "Wrap string to either a number of lines, or a width in characters.
  15594. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  15595. that costs. If there is a word longer than WIDTH, the text is actually
  15596. wrapped to the length of that word.
  15597. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  15598. many lines, whatever width that takes.
  15599. The return value is a list of lines, without newlines at the end."
  15600. (let* ((words (org-split-string string "[ \t\n]+"))
  15601. (maxword (apply 'max (mapcar 'org-string-width words)))
  15602. w ll)
  15603. (cond (width
  15604. (org-do-wrap words (max maxword width)))
  15605. (lines
  15606. (setq w maxword)
  15607. (setq ll (org-do-wrap words maxword))
  15608. (if (<= (length ll) lines)
  15609. ll
  15610. (setq ll words)
  15611. (while (> (length ll) lines)
  15612. (setq w (1+ w))
  15613. (setq ll (org-do-wrap words w)))
  15614. ll))
  15615. (t (error "Cannot wrap this")))))
  15616. (defun org-do-wrap (words width)
  15617. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  15618. (let (lines line)
  15619. (while words
  15620. (setq line (pop words))
  15621. (while (and words (< (+ (length line) (length (car words))) width))
  15622. (setq line (concat line " " (pop words))))
  15623. (setq lines (push line lines)))
  15624. (nreverse lines)))
  15625. (defun org-split-string (string &optional separators)
  15626. "Splits STRING into substrings at SEPARATORS.
  15627. No empty strings are returned if there are matches at the beginning
  15628. and end of string."
  15629. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  15630. (start 0)
  15631. notfirst
  15632. (list nil))
  15633. (while (and (string-match rexp string
  15634. (if (and notfirst
  15635. (= start (match-beginning 0))
  15636. (< start (length string)))
  15637. (1+ start) start))
  15638. (< (match-beginning 0) (length string)))
  15639. (setq notfirst t)
  15640. (or (eq (match-beginning 0) 0)
  15641. (and (eq (match-beginning 0) (match-end 0))
  15642. (eq (match-beginning 0) start))
  15643. (setq list
  15644. (cons (substring string start (match-beginning 0))
  15645. list)))
  15646. (setq start (match-end 0)))
  15647. (or (eq start (length string))
  15648. (setq list
  15649. (cons (substring string start)
  15650. list)))
  15651. (nreverse list)))
  15652. (defun org-quote-vert (s)
  15653. "Replace \"|\" with \"\\vert\"."
  15654. (while (string-match "|" s)
  15655. (setq s (replace-match "\\vert" t t s)))
  15656. s)
  15657. (defun org-uuidgen-p (s)
  15658. "Is S an ID created by UUIDGEN?"
  15659. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  15660. (defun org-context ()
  15661. "Return a list of contexts of the current cursor position.
  15662. If several contexts apply, all are returned.
  15663. Each context entry is a list with a symbol naming the context, and
  15664. two positions indicating start and end of the context. Possible
  15665. contexts are:
  15666. :headline anywhere in a headline
  15667. :headline-stars on the leading stars in a headline
  15668. :todo-keyword on a TODO keyword (including DONE) in a headline
  15669. :tags on the TAGS in a headline
  15670. :priority on the priority cookie in a headline
  15671. :item on the first line of a plain list item
  15672. :item-bullet on the bullet/number of a plain list item
  15673. :checkbox on the checkbox in a plain list item
  15674. :table in an org-mode table
  15675. :table-special on a special filed in a table
  15676. :table-table in a table.el table
  15677. :link on a hyperlink
  15678. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  15679. :target on a <<target>>
  15680. :radio-target on a <<<radio-target>>>
  15681. :latex-fragment on a LaTeX fragment
  15682. :latex-preview on a LaTeX fragment with overlayed preview image
  15683. This function expects the position to be visible because it uses font-lock
  15684. faces as a help to recognize the following contexts: :table-special, :link,
  15685. and :keyword."
  15686. (let* ((f (get-text-property (point) 'face))
  15687. (faces (if (listp f) f (list f)))
  15688. (p (point)) clist o)
  15689. ;; First the large context
  15690. (cond
  15691. ((org-on-heading-p t)
  15692. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  15693. (when (progn
  15694. (beginning-of-line 1)
  15695. (looking-at org-todo-line-tags-regexp))
  15696. (push (org-point-in-group p 1 :headline-stars) clist)
  15697. (push (org-point-in-group p 2 :todo-keyword) clist)
  15698. (push (org-point-in-group p 4 :tags) clist))
  15699. (goto-char p)
  15700. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  15701. (if (looking-at "\\[#[A-Z0-9]\\]")
  15702. (push (org-point-in-group p 0 :priority) clist)))
  15703. ((org-at-item-p)
  15704. (push (org-point-in-group p 2 :item-bullet) clist)
  15705. (push (list :item (point-at-bol)
  15706. (save-excursion (org-end-of-item) (point)))
  15707. clist)
  15708. (and (org-at-item-checkbox-p)
  15709. (push (org-point-in-group p 0 :checkbox) clist)))
  15710. ((org-at-table-p)
  15711. (push (list :table (org-table-begin) (org-table-end)) clist)
  15712. (if (memq 'org-formula faces)
  15713. (push (list :table-special
  15714. (previous-single-property-change p 'face)
  15715. (next-single-property-change p 'face)) clist)))
  15716. ((org-at-table-p 'any)
  15717. (push (list :table-table) clist)))
  15718. (goto-char p)
  15719. ;; Now the small context
  15720. (cond
  15721. ((org-at-timestamp-p)
  15722. (push (org-point-in-group p 0 :timestamp) clist))
  15723. ((memq 'org-link faces)
  15724. (push (list :link
  15725. (previous-single-property-change p 'face)
  15726. (next-single-property-change p 'face)) clist))
  15727. ((memq 'org-special-keyword faces)
  15728. (push (list :keyword
  15729. (previous-single-property-change p 'face)
  15730. (next-single-property-change p 'face)) clist))
  15731. ((org-on-target-p)
  15732. (push (org-point-in-group p 0 :target) clist)
  15733. (goto-char (1- (match-beginning 0)))
  15734. (if (looking-at org-radio-target-regexp)
  15735. (push (org-point-in-group p 0 :radio-target) clist))
  15736. (goto-char p))
  15737. ((setq o (car (delq nil
  15738. (mapcar
  15739. (lambda (x)
  15740. (if (memq x org-latex-fragment-image-overlays) x))
  15741. (org-overlays-at (point))))))
  15742. (push (list :latex-fragment
  15743. (org-overlay-start o) (org-overlay-end o)) clist)
  15744. (push (list :latex-preview
  15745. (org-overlay-start o) (org-overlay-end o)) clist))
  15746. ((org-inside-LaTeX-fragment-p)
  15747. ;; FIXME: positions wrong.
  15748. (push (list :latex-fragment (point) (point)) clist)))
  15749. (setq clist (nreverse (delq nil clist)))
  15750. clist))
  15751. ;; FIXME: Compare with at-regexp-p Do we need both?
  15752. (defun org-in-regexp (re &optional nlines visually)
  15753. "Check if point is inside a match of regexp.
  15754. Normally only the current line is checked, but you can include NLINES extra
  15755. lines both before and after point into the search.
  15756. If VISUALLY is set, require that the cursor is not after the match but
  15757. really on, so that the block visually is on the match."
  15758. (catch 'exit
  15759. (let ((pos (point))
  15760. (eol (point-at-eol (+ 1 (or nlines 0))))
  15761. (inc (if visually 1 0)))
  15762. (save-excursion
  15763. (beginning-of-line (- 1 (or nlines 0)))
  15764. (while (re-search-forward re eol t)
  15765. (if (and (<= (match-beginning 0) pos)
  15766. (>= (+ inc (match-end 0)) pos))
  15767. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  15768. (defun org-at-regexp-p (regexp)
  15769. "Is point inside a match of REGEXP in the current line?"
  15770. (catch 'exit
  15771. (save-excursion
  15772. (let ((pos (point)) (end (point-at-eol)))
  15773. (beginning-of-line 1)
  15774. (while (re-search-forward regexp end t)
  15775. (if (and (<= (match-beginning 0) pos)
  15776. (>= (match-end 0) pos))
  15777. (throw 'exit t)))
  15778. nil))))
  15779. (defun org-in-regexps-block-p (start-re end-re)
  15780. "Returns t if the current point is between matches of START-RE and END-RE.
  15781. This will also return to if point is on one of the two matches."
  15782. (interactive)
  15783. (let ((p (point)))
  15784. (save-excursion
  15785. (and (or (org-at-regexp-p start-re)
  15786. (re-search-backward start-re nil t))
  15787. (re-search-forward end-re nil t)
  15788. (>= (point) p)))))
  15789. (defun org-occur-in-agenda-files (regexp &optional nlines)
  15790. "Call `multi-occur' with buffers for all agenda files."
  15791. (interactive "sOrg-files matching: \np")
  15792. (let* ((files (org-agenda-files))
  15793. (tnames (mapcar 'file-truename files))
  15794. (extra org-agenda-text-search-extra-files)
  15795. f)
  15796. (when (eq (car extra) 'agenda-archives)
  15797. (setq extra (cdr extra))
  15798. (setq files (org-add-archive-files files)))
  15799. (while (setq f (pop extra))
  15800. (unless (member (file-truename f) tnames)
  15801. (add-to-list 'files f 'append)
  15802. (add-to-list 'tnames (file-truename f) 'append)))
  15803. (multi-occur
  15804. (mapcar (lambda (x)
  15805. (with-current-buffer
  15806. (or (get-file-buffer x) (find-file-noselect x))
  15807. (widen)
  15808. (current-buffer)))
  15809. files)
  15810. regexp)))
  15811. (if (boundp 'occur-mode-find-occurrence-hook)
  15812. ;; Emacs 23
  15813. (add-hook 'occur-mode-find-occurrence-hook
  15814. (lambda ()
  15815. (when (org-mode-p)
  15816. (org-reveal))))
  15817. ;; Emacs 22
  15818. (defadvice occur-mode-goto-occurrence
  15819. (after org-occur-reveal activate)
  15820. (and (org-mode-p) (org-reveal)))
  15821. (defadvice occur-mode-goto-occurrence-other-window
  15822. (after org-occur-reveal activate)
  15823. (and (org-mode-p) (org-reveal)))
  15824. (defadvice occur-mode-display-occurrence
  15825. (after org-occur-reveal activate)
  15826. (when (org-mode-p)
  15827. (let ((pos (occur-mode-find-occurrence)))
  15828. (with-current-buffer (marker-buffer pos)
  15829. (save-excursion
  15830. (goto-char pos)
  15831. (org-reveal)))))))
  15832. (defun org-occur-link-in-agenda-files ()
  15833. "Create a link and search for it in the agendas.
  15834. The link is not stored in `org-stored-links', it is just created
  15835. for the search purpose."
  15836. (interactive)
  15837. (let ((link (condition-case nil
  15838. (org-store-link nil)
  15839. (error "Unable to create a link to here"))))
  15840. (org-occur-in-agenda-files (regexp-quote link))))
  15841. (defun org-uniquify (list)
  15842. "Remove duplicate elements from LIST."
  15843. (let (res)
  15844. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  15845. res))
  15846. (defun org-delete-all (elts list)
  15847. "Remove all elements in ELTS from LIST."
  15848. (while elts
  15849. (setq list (delete (pop elts) list)))
  15850. list)
  15851. (defun org-back-over-empty-lines ()
  15852. "Move backwards over whitespace, to the beginning of the first empty line.
  15853. Returns the number of empty lines passed."
  15854. (let ((pos (point)))
  15855. (skip-chars-backward " \t\n\r")
  15856. (beginning-of-line 2)
  15857. (goto-char (min (point) pos))
  15858. (count-lines (point) pos)))
  15859. (defun org-skip-whitespace ()
  15860. (skip-chars-forward " \t\n\r"))
  15861. (defun org-point-in-group (point group &optional context)
  15862. "Check if POINT is in match-group GROUP.
  15863. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  15864. match. If the match group does ot exist or point is not inside it,
  15865. return nil."
  15866. (and (match-beginning group)
  15867. (>= point (match-beginning group))
  15868. (<= point (match-end group))
  15869. (if context
  15870. (list context (match-beginning group) (match-end group))
  15871. t)))
  15872. (defun org-switch-to-buffer-other-window (&rest args)
  15873. "Switch to buffer in a second window on the current frame.
  15874. In particular, do not allow pop-up frames."
  15875. (let (pop-up-frames special-display-buffer-names special-display-regexps
  15876. special-display-function)
  15877. (apply 'switch-to-buffer-other-window args)))
  15878. (defun org-combine-plists (&rest plists)
  15879. "Create a single property list from all plists in PLISTS.
  15880. The process starts by copying the first list, and then setting properties
  15881. from the other lists. Settings in the last list are the most significant
  15882. ones and overrule settings in the other lists."
  15883. (let ((rtn (copy-sequence (pop plists)))
  15884. p v ls)
  15885. (while plists
  15886. (setq ls (pop plists))
  15887. (while ls
  15888. (setq p (pop ls) v (pop ls))
  15889. (setq rtn (plist-put rtn p v))))
  15890. rtn))
  15891. (defun org-move-line-down (arg)
  15892. "Move the current line down. With prefix argument, move it past ARG lines."
  15893. (interactive "p")
  15894. (let ((col (current-column))
  15895. beg end pos)
  15896. (beginning-of-line 1) (setq beg (point))
  15897. (beginning-of-line 2) (setq end (point))
  15898. (beginning-of-line (+ 1 arg))
  15899. (setq pos (move-marker (make-marker) (point)))
  15900. (insert (delete-and-extract-region beg end))
  15901. (goto-char pos)
  15902. (org-move-to-column col)))
  15903. (defun org-move-line-up (arg)
  15904. "Move the current line up. With prefix argument, move it past ARG lines."
  15905. (interactive "p")
  15906. (let ((col (current-column))
  15907. beg end pos)
  15908. (beginning-of-line 1) (setq beg (point))
  15909. (beginning-of-line 2) (setq end (point))
  15910. (beginning-of-line (- arg))
  15911. (setq pos (move-marker (make-marker) (point)))
  15912. (insert (delete-and-extract-region beg end))
  15913. (goto-char pos)
  15914. (org-move-to-column col)))
  15915. (defun org-replace-escapes (string table)
  15916. "Replace %-escapes in STRING with values in TABLE.
  15917. TABLE is an association list with keys like \"%a\" and string values.
  15918. The sequences in STRING may contain normal field width and padding information,
  15919. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  15920. so values can contain further %-escapes if they are define later in TABLE."
  15921. (let ((case-fold-search nil)
  15922. e re rpl)
  15923. (while (setq e (pop table))
  15924. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  15925. (while (string-match re string)
  15926. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  15927. (cdr e)))
  15928. (setq string (replace-match rpl t t string))))
  15929. string))
  15930. (defun org-sublist (list start end)
  15931. "Return a section of LIST, from START to END.
  15932. Counting starts at 1."
  15933. (let (rtn (c start))
  15934. (setq list (nthcdr (1- start) list))
  15935. (while (and list (<= c end))
  15936. (push (pop list) rtn)
  15937. (setq c (1+ c)))
  15938. (nreverse rtn)))
  15939. (defun org-find-base-buffer-visiting (file)
  15940. "Like `find-buffer-visiting' but always return the base buffer and
  15941. not an indirect buffer."
  15942. (let ((buf (or (get-file-buffer file)
  15943. (find-buffer-visiting file))))
  15944. (if buf
  15945. (or (buffer-base-buffer buf) buf)
  15946. nil)))
  15947. (defun org-image-file-name-regexp (&optional extensions)
  15948. "Return regexp matching the file names of images.
  15949. If EXTENSIONS is given, only match these."
  15950. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  15951. (image-file-name-regexp)
  15952. (let ((image-file-name-extensions
  15953. (or extensions
  15954. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  15955. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  15956. (concat "\\."
  15957. (regexp-opt (nconc (mapcar 'upcase
  15958. image-file-name-extensions)
  15959. image-file-name-extensions)
  15960. t)
  15961. "\\'"))))
  15962. (defun org-file-image-p (file &optional extensions)
  15963. "Return non-nil if FILE is an image."
  15964. (save-match-data
  15965. (string-match (org-image-file-name-regexp extensions) file)))
  15966. (defun org-get-cursor-date ()
  15967. "Return the date at cursor in as a time.
  15968. This works in the calendar and in the agenda, anywhere else it just
  15969. returns the current time."
  15970. (let (date day defd)
  15971. (cond
  15972. ((eq major-mode 'calendar-mode)
  15973. (setq date (calendar-cursor-to-date)
  15974. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15975. ((eq major-mode 'org-agenda-mode)
  15976. (setq day (get-text-property (point) 'day))
  15977. (if day
  15978. (setq date (calendar-gregorian-from-absolute day)
  15979. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  15980. (nth 2 date))))))
  15981. (or defd (current-time))))
  15982. (defvar org-agenda-action-marker (make-marker)
  15983. "Marker pointing to the entry for the next agenda action.")
  15984. (defun org-mark-entry-for-agenda-action ()
  15985. "Mark the current entry as target of an agenda action.
  15986. Agenda actions are actions executed from the agenda with the key `k',
  15987. which make use of the date at the cursor."
  15988. (interactive)
  15989. (move-marker org-agenda-action-marker
  15990. (save-excursion (org-back-to-heading t) (point))
  15991. (current-buffer))
  15992. (message
  15993. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  15994. ;;; Paragraph filling stuff.
  15995. ;; We want this to be just right, so use the full arsenal.
  15996. (defun org-indent-line-function ()
  15997. "Indent line like previous, but further if previous was headline or item."
  15998. (interactive)
  15999. (let* ((pos (point))
  16000. (itemp (org-at-item-p))
  16001. (case-fold-search t)
  16002. (org-drawer-regexp (or org-drawer-regexp "\000"))
  16003. column bpos bcol tpos tcol bullet btype bullet-type)
  16004. ;; Find the previous relevant line
  16005. (beginning-of-line 1)
  16006. (cond
  16007. ((looking-at "#") (setq column 0))
  16008. ((looking-at "\\*+ ") (setq column 0))
  16009. ((and (looking-at "[ \t]*:END:")
  16010. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  16011. (save-excursion
  16012. (goto-char (1- (match-beginning 1)))
  16013. (setq column (current-column))))
  16014. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  16015. (save-excursion
  16016. (re-search-backward
  16017. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  16018. (setq column (org-get-indentation (match-string 0))))
  16019. (t
  16020. (beginning-of-line 0)
  16021. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  16022. (not (looking-at "[ \t]*:END:"))
  16023. (not (looking-at org-drawer-regexp)))
  16024. (beginning-of-line 0))
  16025. (cond
  16026. ((looking-at "\\*+[ \t]+")
  16027. (if (not org-adapt-indentation)
  16028. (setq column 0)
  16029. (goto-char (match-end 0))
  16030. (setq column (current-column))))
  16031. ((looking-at org-drawer-regexp)
  16032. (goto-char (1- (match-beginning 1)))
  16033. (setq column (current-column)))
  16034. ((looking-at "\\([ \t]*\\):END:")
  16035. (goto-char (match-end 1))
  16036. (setq column (current-column)))
  16037. ((org-in-item-p)
  16038. (org-beginning-of-item)
  16039. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  16040. (setq bpos (match-beginning 1) tpos (match-end 0)
  16041. bcol (progn (goto-char bpos) (current-column))
  16042. tcol (progn (goto-char tpos) (current-column))
  16043. bullet (match-string 1)
  16044. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  16045. (if (> tcol (+ bcol org-description-max-indent))
  16046. (setq tcol (+ bcol 5)))
  16047. (if (not itemp)
  16048. (setq column tcol)
  16049. (goto-char pos)
  16050. (beginning-of-line 1)
  16051. (if (looking-at "\\S-")
  16052. (progn
  16053. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  16054. (setq bullet (match-string 1)
  16055. btype (if (string-match "[0-9]" bullet) "n" bullet))
  16056. (setq column (if (equal btype bullet-type) bcol tcol)))
  16057. (setq column (org-get-indentation)))))
  16058. (t (setq column (org-get-indentation))))))
  16059. (goto-char pos)
  16060. (if (<= (current-column) (current-indentation))
  16061. (org-indent-line-to column)
  16062. (save-excursion (org-indent-line-to column)))
  16063. (setq column (current-column))
  16064. (beginning-of-line 1)
  16065. (if (looking-at
  16066. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  16067. (replace-match (concat (match-string 1)
  16068. (format org-property-format
  16069. (match-string 2) (match-string 3)))
  16070. t t))
  16071. (org-move-to-column column)))
  16072. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  16073. "Variable to store copy of `adaptive-fill-regexp'.
  16074. Since `adaptive-fill-regexp' is set to never match, we need to
  16075. store a backup of its value before entering `org-mode' so that
  16076. the functionality can be provided as a fall-back.")
  16077. (defun org-set-autofill-regexps ()
  16078. (interactive)
  16079. ;; In the paragraph separator we include headlines, because filling
  16080. ;; text in a line directly attached to a headline would otherwise
  16081. ;; fill the headline as well.
  16082. (org-set-local 'comment-start-skip "^#+[ \t]*")
  16083. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  16084. ;; The paragraph starter includes hand-formatted lists.
  16085. (org-set-local
  16086. 'paragraph-start
  16087. (concat
  16088. "\f" "\\|"
  16089. "[ ]*$" "\\|"
  16090. "\\*+ " "\\|"
  16091. "[ \t]*#" "\\|"
  16092. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  16093. "[ \t]*[:|]" "\\|"
  16094. "\\$\\$" "\\|"
  16095. "\\\\\\(begin\\|end\\|[][]\\)"))
  16096. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  16097. ;; But only if the user has not turned off tables or fixed-width regions
  16098. (org-set-local
  16099. 'auto-fill-inhibit-regexp
  16100. (concat "\\*+ \\|#\\+"
  16101. "\\|[ \t]*" org-keyword-time-regexp
  16102. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  16103. (concat
  16104. "\\|[ \t]*["
  16105. (if org-enable-table-editor "|" "")
  16106. (if org-enable-fixed-width-editor ":" "")
  16107. "]"))))
  16108. ;; We use our own fill-paragraph function, to make sure that tables
  16109. ;; and fixed-width regions are not wrapped. That function will pass
  16110. ;; through to `fill-paragraph' when appropriate.
  16111. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  16112. ;; Adaptive filling: To get full control, first make sure that
  16113. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  16114. (unless (local-variable-p 'adaptive-fill-regexp)
  16115. (org-set-local 'org-adaptive-fill-regexp-backup
  16116. adaptive-fill-regexp))
  16117. (org-set-local 'adaptive-fill-regexp "\000")
  16118. (org-set-local 'adaptive-fill-function
  16119. 'org-adaptive-fill-function)
  16120. (org-set-local
  16121. 'align-mode-rules-list
  16122. '((org-in-buffer-settings
  16123. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  16124. (modes . '(org-mode))))))
  16125. (defun org-fill-paragraph (&optional justify)
  16126. "Re-align a table, pass through to fill-paragraph if no table."
  16127. (let ((table-p (org-at-table-p))
  16128. (table.el-p (org-at-table.el-p)))
  16129. (cond ((and (equal (char-after (point-at-bol)) ?*)
  16130. (save-excursion (goto-char (point-at-bol))
  16131. (looking-at outline-regexp)))
  16132. t) ; skip headlines
  16133. (table.el-p t) ; skip table.el tables
  16134. (table-p (org-table-align) t) ; align org-mode tables
  16135. (t nil)))) ; call paragraph-fill
  16136. ;; For reference, this is the default value of adaptive-fill-regexp
  16137. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  16138. (defun org-adaptive-fill-function ()
  16139. "Return a fill prefix for org-mode files.
  16140. In particular, this makes sure hanging paragraphs for hand-formatted lists
  16141. work correctly."
  16142. (cond
  16143. ;; Comment line
  16144. ((looking-at "#[ \t]+")
  16145. (match-string-no-properties 0))
  16146. ;; Description list
  16147. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  16148. (save-excursion
  16149. (if (> (match-end 1) (+ (match-beginning 1)
  16150. org-description-max-indent))
  16151. (goto-char (+ (match-beginning 1) 5))
  16152. (goto-char (match-end 0)))
  16153. (make-string (current-column) ?\ )))
  16154. ;; Ordered or unordered list
  16155. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)")
  16156. (save-excursion
  16157. (goto-char (match-end 0))
  16158. (make-string (current-column) ?\ )))
  16159. ;; Other text
  16160. ((looking-at org-adaptive-fill-regexp-backup)
  16161. (match-string-no-properties 0))))
  16162. ;;; Other stuff.
  16163. (defun org-toggle-fixed-width-section (arg)
  16164. "Toggle the fixed-width export.
  16165. If there is no active region, the QUOTE keyword at the current headline is
  16166. inserted or removed. When present, it causes the text between this headline
  16167. and the next to be exported as fixed-width text, and unmodified.
  16168. If there is an active region, this command adds or removes a colon as the
  16169. first character of this line. If the first character of a line is a colon,
  16170. this line is also exported in fixed-width font."
  16171. (interactive "P")
  16172. (let* ((cc 0)
  16173. (regionp (org-region-active-p))
  16174. (beg (if regionp (region-beginning) (point)))
  16175. (end (if regionp (region-end)))
  16176. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  16177. (case-fold-search nil)
  16178. (re "[ \t]*\\(: \\)")
  16179. off)
  16180. (if regionp
  16181. (save-excursion
  16182. (goto-char beg)
  16183. (setq cc (current-column))
  16184. (beginning-of-line 1)
  16185. (setq off (looking-at re))
  16186. (while (> nlines 0)
  16187. (setq nlines (1- nlines))
  16188. (beginning-of-line 1)
  16189. (cond
  16190. (arg
  16191. (org-move-to-column cc t)
  16192. (insert ": \n")
  16193. (forward-line -1))
  16194. ((and off (looking-at re))
  16195. (replace-match "" t t nil 1))
  16196. ((not off) (org-move-to-column cc t) (insert ": ")))
  16197. (forward-line 1)))
  16198. (save-excursion
  16199. (org-back-to-heading)
  16200. (if (looking-at (concat outline-regexp
  16201. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  16202. (replace-match "" t t nil 1)
  16203. (if (looking-at outline-regexp)
  16204. (progn
  16205. (goto-char (match-end 0))
  16206. (insert org-quote-string " "))))))))
  16207. (defun org-reftex-citation ()
  16208. "Use reftex-citation to insert a citation into the buffer.
  16209. This looks for a line like
  16210. #+BIBLIOGRAPHY: foo plain option:-d
  16211. and derives from it that foo.bib is the bibliography file relevant
  16212. for this document. It then installs the necessary environment for RefTeX
  16213. to work in this buffer and calls `reftex-citation' to insert a citation
  16214. into the buffer.
  16215. Export of such citations to both LaTeX and HTML is handled by the contributed
  16216. package org-exp-bibtex by Taru Karttunen."
  16217. (interactive)
  16218. (let ((reftex-docstruct-symbol 'rds)
  16219. (reftex-cite-format "\\cite{%l}")
  16220. rds bib)
  16221. (save-excursion
  16222. (save-restriction
  16223. (widen)
  16224. (let ((case-fold-search t)
  16225. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  16226. (if (not (save-excursion
  16227. (or (re-search-forward re nil t)
  16228. (re-search-backward re nil t))))
  16229. (error "No bibliography defined in file")
  16230. (setq bib (concat (match-string 1) ".bib")
  16231. rds (list (list 'bib bib)))))))
  16232. (call-interactively 'reftex-citation)))
  16233. ;;;; Functions extending outline functionality
  16234. (defun org-beginning-of-line (&optional arg)
  16235. "Go to the beginning of the current line. If that is invisible, continue
  16236. to a visible line beginning. This makes the function of C-a more intuitive.
  16237. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  16238. first attempt, and only move to after the tags when the cursor is already
  16239. beyond the end of the headline."
  16240. (interactive "P")
  16241. (let ((pos (point))
  16242. (special (if (consp org-special-ctrl-a/e)
  16243. (car org-special-ctrl-a/e)
  16244. org-special-ctrl-a/e))
  16245. refpos)
  16246. (if (org-bound-and-true-p line-move-visual)
  16247. (beginning-of-visual-line 1)
  16248. (beginning-of-line 1))
  16249. (if (and arg (fboundp 'move-beginning-of-line))
  16250. (call-interactively 'move-beginning-of-line)
  16251. (if (bobp)
  16252. nil
  16253. (backward-char 1)
  16254. (if (org-invisible-p)
  16255. (while (and (not (bobp)) (org-invisible-p))
  16256. (backward-char 1)
  16257. (beginning-of-line 1))
  16258. (forward-char 1))))
  16259. (when special
  16260. (cond
  16261. ((and (looking-at org-complex-heading-regexp)
  16262. (= (char-after (match-end 1)) ?\ ))
  16263. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  16264. (point-at-eol)))
  16265. (goto-char
  16266. (if (eq special t)
  16267. (cond ((> pos refpos) refpos)
  16268. ((= pos (point)) refpos)
  16269. (t (point)))
  16270. (cond ((> pos (point)) (point))
  16271. ((not (eq last-command this-command)) (point))
  16272. (t refpos)))))
  16273. ((org-at-item-p)
  16274. (goto-char
  16275. (if (eq special t)
  16276. (cond ((> pos (match-end 4)) (match-end 4))
  16277. ((= pos (point)) (match-end 4))
  16278. (t (point)))
  16279. (cond ((> pos (point)) (point))
  16280. ((not (eq last-command this-command)) (point))
  16281. (t (match-end 4))))))))
  16282. (org-no-warnings
  16283. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  16284. (defun org-end-of-line (&optional arg)
  16285. "Go to the end of the line.
  16286. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  16287. first attempt, and only move to after the tags when the cursor is already
  16288. beyond the end of the headline."
  16289. (interactive "P")
  16290. (let ((special (if (consp org-special-ctrl-a/e)
  16291. (cdr org-special-ctrl-a/e)
  16292. org-special-ctrl-a/e)))
  16293. (if (or (not special)
  16294. (not (org-on-heading-p))
  16295. arg)
  16296. (call-interactively
  16297. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  16298. ((fboundp 'move-end-of-line) 'move-end-of-line)
  16299. (t 'end-of-line)))
  16300. (let ((pos (point)))
  16301. (beginning-of-line 1)
  16302. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
  16303. (if (eq special t)
  16304. (if (or (< pos (match-beginning 1))
  16305. (= pos (match-end 0)))
  16306. (goto-char (match-beginning 1))
  16307. (goto-char (match-end 0)))
  16308. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  16309. (goto-char (match-end 0))
  16310. (goto-char (match-beginning 1))))
  16311. (call-interactively (if (fboundp 'move-end-of-line)
  16312. 'move-end-of-line
  16313. 'end-of-line)))))
  16314. (org-no-warnings
  16315. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  16316. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  16317. (define-key org-mode-map "\C-e" 'org-end-of-line)
  16318. (define-key org-mode-map [home] 'org-beginning-of-line)
  16319. (define-key org-mode-map [end] 'org-end-of-line)
  16320. (defun org-backward-sentence (&optional arg)
  16321. "Go to beginning of sentence, or beginning of table field.
  16322. This will call `backward-sentence' or `org-table-beginning-of-field',
  16323. depending on context."
  16324. (interactive "P")
  16325. (cond
  16326. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  16327. (t (call-interactively 'backward-sentence))))
  16328. (defun org-forward-sentence (&optional arg)
  16329. "Go to end of sentence, or end of table field.
  16330. This will call `forward-sentence' or `org-table-end-of-field',
  16331. depending on context."
  16332. (interactive "P")
  16333. (cond
  16334. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  16335. (t (call-interactively 'forward-sentence))))
  16336. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  16337. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  16338. (defun org-kill-line (&optional arg)
  16339. "Kill line, to tags or end of line."
  16340. (interactive "P")
  16341. (cond
  16342. ((or (not org-special-ctrl-k)
  16343. (bolp)
  16344. (not (org-on-heading-p)))
  16345. (call-interactively 'kill-line))
  16346. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  16347. (kill-region (point) (match-beginning 1))
  16348. (org-set-tags nil t))
  16349. (t (kill-region (point) (point-at-eol)))))
  16350. (define-key org-mode-map "\C-k" 'org-kill-line)
  16351. (defun org-yank (&optional arg)
  16352. "Yank. If the kill is a subtree, treat it specially.
  16353. This command will look at the current kill and check if is a single
  16354. subtree, or a series of subtrees[1]. If it passes the test, and if the
  16355. cursor is at the beginning of a line or after the stars of a currently
  16356. empty headline, then the yank is handled specially. How exactly depends
  16357. on the value of the following variables, both set by default.
  16358. org-yank-folded-subtrees
  16359. When set, the subtree(s) will be folded after insertion, but only
  16360. if doing so would now swallow text after the yanked text.
  16361. org-yank-adjusted-subtrees
  16362. When set, the subtree will be promoted or demoted in order to
  16363. fit into the local outline tree structure, which means that the level
  16364. will be adjusted so that it becomes the smaller one of the two
  16365. *visible* surrounding headings.
  16366. Any prefix to this command will cause `yank' to be called directly with
  16367. no special treatment. In particular, a simple `C-u' prefix will just
  16368. plainly yank the text as it is.
  16369. \[1] The test checks if the first non-white line is a heading
  16370. and if there are no other headings with fewer stars."
  16371. (interactive "P")
  16372. (org-yank-generic 'yank arg))
  16373. (defun org-yank-generic (command arg)
  16374. "Perform some yank-like command.
  16375. This function implements the behavior described in the `org-yank'
  16376. documentation. However, it has been generalized to work for any
  16377. interactive command with similar behavior."
  16378. ;; pretend to be command COMMAND
  16379. (setq this-command command)
  16380. (if arg
  16381. (call-interactively command)
  16382. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  16383. (and (org-kill-is-subtree-p)
  16384. (or (bolp)
  16385. (and (looking-at "[ \t]*$")
  16386. (string-match
  16387. "\\`\\*+\\'"
  16388. (buffer-substring (point-at-bol) (point)))))))
  16389. swallowp)
  16390. (cond
  16391. ((and subtreep org-yank-folded-subtrees)
  16392. (let ((beg (point))
  16393. end)
  16394. (if (and subtreep org-yank-adjusted-subtrees)
  16395. (org-paste-subtree nil nil 'for-yank)
  16396. (call-interactively command))
  16397. (setq end (point))
  16398. (goto-char beg)
  16399. (when (and (bolp) subtreep
  16400. (not (setq swallowp
  16401. (org-yank-folding-would-swallow-text beg end))))
  16402. (or (looking-at outline-regexp)
  16403. (re-search-forward (concat "^" outline-regexp) end t))
  16404. (while (and (< (point) end) (looking-at outline-regexp))
  16405. (hide-subtree)
  16406. (org-cycle-show-empty-lines 'folded)
  16407. (condition-case nil
  16408. (outline-forward-same-level 1)
  16409. (error (goto-char end)))))
  16410. (when swallowp
  16411. (message
  16412. "Inserted text not folded because that would swallow text"))
  16413. (goto-char end)
  16414. (skip-chars-forward " \t\n\r")
  16415. (beginning-of-line 1)
  16416. (push-mark beg 'nomsg)))
  16417. ((and subtreep org-yank-adjusted-subtrees)
  16418. (let ((beg (point-at-bol)))
  16419. (org-paste-subtree nil nil 'for-yank)
  16420. (push-mark beg 'nomsg)))
  16421. (t
  16422. (call-interactively command))))))
  16423. (defun org-yank-folding-would-swallow-text (beg end)
  16424. "Would hide-subtree at BEG swallow any text after END?"
  16425. (let (level)
  16426. (save-excursion
  16427. (goto-char beg)
  16428. (when (or (looking-at outline-regexp)
  16429. (re-search-forward (concat "^" outline-regexp) end t))
  16430. (setq level (org-outline-level)))
  16431. (goto-char end)
  16432. (skip-chars-forward " \t\r\n\v\f")
  16433. (if (or (eobp)
  16434. (and (bolp) (looking-at org-outline-regexp)
  16435. (<= (org-outline-level) level)))
  16436. nil ; Nothing would be swallowed
  16437. t)))) ; something would swallow
  16438. (define-key org-mode-map "\C-y" 'org-yank)
  16439. (defun org-invisible-p ()
  16440. "Check if point is at a character currently not visible."
  16441. ;; Early versions of noutline don't have `outline-invisible-p'.
  16442. (if (fboundp 'outline-invisible-p)
  16443. (outline-invisible-p)
  16444. (get-char-property (point) 'invisible)))
  16445. (defun org-invisible-p2 ()
  16446. "Check if point is at a character currently not visible."
  16447. (save-excursion
  16448. (if (and (eolp) (not (bobp))) (backward-char 1))
  16449. ;; Early versions of noutline don't have `outline-invisible-p'.
  16450. (if (fboundp 'outline-invisible-p)
  16451. (outline-invisible-p)
  16452. (get-char-property (point) 'invisible))))
  16453. (defun org-back-to-heading (&optional invisible-ok)
  16454. "Call `outline-back-to-heading', but provide a better error message."
  16455. (condition-case nil
  16456. (outline-back-to-heading invisible-ok)
  16457. (error (error "Before first headline at position %d in buffer %s"
  16458. (point) (current-buffer)))))
  16459. (defun org-before-first-heading-p ()
  16460. "Before first heading?"
  16461. (save-excursion
  16462. (null (re-search-backward "^\\*+ " nil t))))
  16463. (defun org-on-heading-p (&optional ignored)
  16464. (outline-on-heading-p t))
  16465. (defun org-at-heading-p (&optional ignored)
  16466. (outline-on-heading-p t))
  16467. (defun org-point-at-end-of-empty-headline ()
  16468. "If point is at the end of an empty headline, return t, else nil.
  16469. If the heading only contains a TODO keyword, it is still still considered
  16470. empty."
  16471. (and (looking-at "[ \t]*$")
  16472. (save-excursion
  16473. (beginning-of-line 1)
  16474. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  16475. "\\)?[ \t]*$")))))
  16476. (defun org-at-heading-or-item-p ()
  16477. (or (org-on-heading-p) (org-at-item-p)))
  16478. (defun org-on-target-p ()
  16479. (or (org-in-regexp org-radio-target-regexp)
  16480. (org-in-regexp org-target-regexp)))
  16481. (defun org-up-heading-all (arg)
  16482. "Move to the heading line of which the present line is a subheading.
  16483. This function considers both visible and invisible heading lines.
  16484. With argument, move up ARG levels."
  16485. (if (fboundp 'outline-up-heading-all)
  16486. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  16487. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  16488. (defun org-up-heading-safe ()
  16489. "Move to the heading line of which the present line is a subheading.
  16490. This version will not throw an error. It will return the level of the
  16491. headline found, or nil if no higher level is found.
  16492. Also, this function will be a lot faster than `outline-up-heading',
  16493. because it relies on stars being the outline starters. This can really
  16494. make a significant difference in outlines with very many siblings."
  16495. (let (start-level re)
  16496. (org-back-to-heading t)
  16497. (setq start-level (funcall outline-level))
  16498. (if (equal start-level 1)
  16499. nil
  16500. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  16501. (if (re-search-backward re nil t)
  16502. (funcall outline-level)))))
  16503. (defun org-first-sibling-p ()
  16504. "Is this heading the first child of its parents?"
  16505. (interactive)
  16506. (let ((re (concat "^" outline-regexp))
  16507. level l)
  16508. (unless (org-at-heading-p t)
  16509. (error "Not at a heading"))
  16510. (setq level (funcall outline-level))
  16511. (save-excursion
  16512. (if (not (re-search-backward re nil t))
  16513. t
  16514. (setq l (funcall outline-level))
  16515. (< l level)))))
  16516. (defun org-goto-sibling (&optional previous)
  16517. "Goto the next sibling, even if it is invisible.
  16518. When PREVIOUS is set, go to the previous sibling instead. Returns t
  16519. when a sibling was found. When none is found, return nil and don't
  16520. move point."
  16521. (let ((fun (if previous 're-search-backward 're-search-forward))
  16522. (pos (point))
  16523. (re (concat "^" outline-regexp))
  16524. level l)
  16525. (when (condition-case nil (org-back-to-heading t) (error nil))
  16526. (setq level (funcall outline-level))
  16527. (catch 'exit
  16528. (or previous (forward-char 1))
  16529. (while (funcall fun re nil t)
  16530. (setq l (funcall outline-level))
  16531. (when (< l level) (goto-char pos) (throw 'exit nil))
  16532. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  16533. (goto-char pos)
  16534. nil))))
  16535. (defun org-show-siblings ()
  16536. "Show all siblings of the current headline."
  16537. (save-excursion
  16538. (while (org-goto-sibling) (org-flag-heading nil)))
  16539. (save-excursion
  16540. (while (org-goto-sibling 'previous)
  16541. (org-flag-heading nil))))
  16542. (defun org-show-hidden-entry ()
  16543. "Show an entry where even the heading is hidden."
  16544. (save-excursion
  16545. (org-show-entry)))
  16546. (defun org-flag-heading (flag &optional entry)
  16547. "Flag the current heading. FLAG non-nil means make invisible.
  16548. When ENTRY is non-nil, show the entire entry."
  16549. (save-excursion
  16550. (org-back-to-heading t)
  16551. ;; Check if we should show the entire entry
  16552. (if entry
  16553. (progn
  16554. (org-show-entry)
  16555. (save-excursion
  16556. (and (outline-next-heading)
  16557. (org-flag-heading nil))))
  16558. (outline-flag-region (max (point-min) (1- (point)))
  16559. (save-excursion (outline-end-of-heading) (point))
  16560. flag))))
  16561. (defun org-get-next-sibling ()
  16562. "Move to next heading of the same level, and return point.
  16563. If there is no such heading, return nil.
  16564. This is like outline-next-sibling, but invisible headings are ok."
  16565. (let ((level (funcall outline-level)))
  16566. (outline-next-heading)
  16567. (while (and (not (eobp)) (> (funcall outline-level) level))
  16568. (outline-next-heading))
  16569. (if (or (eobp) (< (funcall outline-level) level))
  16570. nil
  16571. (point))))
  16572. (defun org-get-last-sibling ()
  16573. "Move to previous heading of the same level, and return point.
  16574. If there is no such heading, return nil."
  16575. (let ((opoint (point))
  16576. (level (funcall outline-level)))
  16577. (outline-previous-heading)
  16578. (when (and (/= (point) opoint) (outline-on-heading-p t))
  16579. (while (and (> (funcall outline-level) level)
  16580. (not (bobp)))
  16581. (outline-previous-heading))
  16582. (if (< (funcall outline-level) level)
  16583. nil
  16584. (point)))))
  16585. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  16586. ;; This contains an exact copy of the original function, but it uses
  16587. ;; `org-back-to-heading', to make it work also in invisible
  16588. ;; trees. And is uses an invisible-OK argument.
  16589. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  16590. ;; Furthermore, when used inside Org, finding the end of a large subtree
  16591. ;; with many children and grandchildren etc, this can be much faster
  16592. ;; than the outline version.
  16593. (org-back-to-heading invisible-OK)
  16594. (let ((first t)
  16595. (level (funcall outline-level)))
  16596. (if (and (org-mode-p) (< level 1000))
  16597. ;; A true heading (not a plain list item), in Org-mode
  16598. ;; This means we can easily find the end by looking
  16599. ;; only for the right number of stars. Using a regexp to do
  16600. ;; this is so much faster than using a Lisp loop.
  16601. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  16602. (forward-char 1)
  16603. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  16604. ;; something else, do it the slow way
  16605. (while (and (not (eobp))
  16606. (or first (> (funcall outline-level) level)))
  16607. (setq first nil)
  16608. (outline-next-heading)))
  16609. (unless to-heading
  16610. (if (memq (preceding-char) '(?\n ?\^M))
  16611. (progn
  16612. ;; Go to end of line before heading
  16613. (forward-char -1)
  16614. (if (memq (preceding-char) '(?\n ?\^M))
  16615. ;; leave blank line before heading
  16616. (forward-char -1))))))
  16617. (point))
  16618. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  16619. "Use Org version in org-mode, for dramatic speed-up."
  16620. (if (eq major-mode 'org-mode)
  16621. (progn
  16622. (org-end-of-subtree nil t)
  16623. (unless (eobp) (backward-char 1)))
  16624. ad-do-it))
  16625. (defun org-forward-same-level (arg &optional invisible-ok)
  16626. "Move forward to the arg'th subheading at same level as this one.
  16627. Stop at the first and last subheadings of a superior heading."
  16628. (interactive "p")
  16629. (org-back-to-heading invisible-ok)
  16630. (org-on-heading-p)
  16631. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  16632. (re (format "^\\*\\{1,%d\\} " level))
  16633. l)
  16634. (forward-char 1)
  16635. (while (> arg 0)
  16636. (while (and (re-search-forward re nil 'move)
  16637. (setq l (- (match-end 0) (match-beginning 0) 1))
  16638. (= l level)
  16639. (not invisible-ok)
  16640. (progn (backward-char 1) (org-invisible-p)))
  16641. (if (< l level) (setq arg 1)))
  16642. (setq arg (1- arg)))
  16643. (beginning-of-line 1)))
  16644. (defun org-backward-same-level (arg &optional invisible-ok)
  16645. "Move backward to the arg'th subheading at same level as this one.
  16646. Stop at the first and last subheadings of a superior heading."
  16647. (interactive "p")
  16648. (org-back-to-heading)
  16649. (org-on-heading-p)
  16650. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  16651. (re (format "^\\*\\{1,%d\\} " level))
  16652. l)
  16653. (while (> arg 0)
  16654. (while (and (re-search-backward re nil 'move)
  16655. (setq l (- (match-end 0) (match-beginning 0) 1))
  16656. (= l level)
  16657. (not invisible-ok)
  16658. (org-invisible-p))
  16659. (if (< l level) (setq arg 1)))
  16660. (setq arg (1- arg)))))
  16661. (defun org-show-subtree ()
  16662. "Show everything after this heading at deeper levels."
  16663. (outline-flag-region
  16664. (point)
  16665. (save-excursion
  16666. (org-end-of-subtree t t))
  16667. nil))
  16668. (defun org-show-entry ()
  16669. "Show the body directly following this heading.
  16670. Show the heading too, if it is currently invisible."
  16671. (interactive)
  16672. (save-excursion
  16673. (condition-case nil
  16674. (progn
  16675. (org-back-to-heading t)
  16676. (outline-flag-region
  16677. (max (point-min) (1- (point)))
  16678. (save-excursion
  16679. (if (re-search-forward
  16680. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  16681. (match-beginning 1)
  16682. (point-max)))
  16683. nil)
  16684. (org-cycle-hide-drawers 'children))
  16685. (error nil))))
  16686. (defun org-make-options-regexp (kwds &optional extra)
  16687. "Make a regular expression for keyword lines."
  16688. (concat
  16689. "^"
  16690. "#?[ \t]*\\+\\("
  16691. (mapconcat 'regexp-quote kwds "\\|")
  16692. (if extra (concat "\\|" extra))
  16693. "\\):[ \t]*"
  16694. "\\(.*\\)"))
  16695. ;; Make isearch reveal the necessary context
  16696. (defun org-isearch-end ()
  16697. "Reveal context after isearch exits."
  16698. (when isearch-success ; only if search was successful
  16699. (if (featurep 'xemacs)
  16700. ;; Under XEmacs, the hook is run in the correct place,
  16701. ;; we directly show the context.
  16702. (org-show-context 'isearch)
  16703. ;; In Emacs the hook runs *before* restoring the overlays.
  16704. ;; So we have to use a one-time post-command-hook to do this.
  16705. ;; (Emacs 22 has a special variable, see function `org-mode')
  16706. (unless (and (boundp 'isearch-mode-end-hook-quit)
  16707. isearch-mode-end-hook-quit)
  16708. ;; Only when the isearch was not quitted.
  16709. (org-add-hook 'post-command-hook 'org-isearch-post-command
  16710. 'append 'local)))))
  16711. (defun org-isearch-post-command ()
  16712. "Remove self from hook, and show context."
  16713. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  16714. (org-show-context 'isearch))
  16715. ;;;; Integration with and fixes for other packages
  16716. ;;; Imenu support
  16717. (defvar org-imenu-markers nil
  16718. "All markers currently used by Imenu.")
  16719. (make-variable-buffer-local 'org-imenu-markers)
  16720. (defun org-imenu-new-marker (&optional pos)
  16721. "Return a new marker for use by Imenu, and remember the marker."
  16722. (let ((m (make-marker)))
  16723. (move-marker m (or pos (point)))
  16724. (push m org-imenu-markers)
  16725. m))
  16726. (defun org-imenu-get-tree ()
  16727. "Produce the index for Imenu."
  16728. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  16729. (setq org-imenu-markers nil)
  16730. (let* ((n org-imenu-depth)
  16731. (re (concat "^" outline-regexp))
  16732. (subs (make-vector (1+ n) nil))
  16733. (last-level 0)
  16734. m level head)
  16735. (save-excursion
  16736. (save-restriction
  16737. (widen)
  16738. (goto-char (point-max))
  16739. (while (re-search-backward re nil t)
  16740. (setq level (org-reduced-level (funcall outline-level)))
  16741. (when (<= level n)
  16742. (looking-at org-complex-heading-regexp)
  16743. (setq head (org-link-display-format
  16744. (org-match-string-no-properties 4))
  16745. m (org-imenu-new-marker))
  16746. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  16747. (if (>= level last-level)
  16748. (push (cons head m) (aref subs level))
  16749. (push (cons head (aref subs (1+ level))) (aref subs level))
  16750. (loop for i from (1+ level) to n do (aset subs i nil)))
  16751. (setq last-level level)))))
  16752. (aref subs 1)))
  16753. (eval-after-load "imenu"
  16754. '(progn
  16755. (add-hook 'imenu-after-jump-hook
  16756. (lambda ()
  16757. (if (eq major-mode 'org-mode)
  16758. (org-show-context 'org-goto))))))
  16759. (defun org-link-display-format (link)
  16760. "Replace a link with either the description, or the link target
  16761. if no description is present"
  16762. (save-match-data
  16763. (if (string-match org-bracket-link-analytic-regexp link)
  16764. (replace-match (if (match-end 5)
  16765. (match-string 5 link)
  16766. (concat (match-string 1 link)
  16767. (match-string 3 link)))
  16768. nil t link)
  16769. link)))
  16770. ;; Speedbar support
  16771. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  16772. "Overlay marking the agenda restriction line in speedbar.")
  16773. (org-overlay-put org-speedbar-restriction-lock-overlay
  16774. 'face 'org-agenda-restriction-lock)
  16775. (org-overlay-put org-speedbar-restriction-lock-overlay
  16776. 'help-echo "Agendas are currently limited to this item.")
  16777. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  16778. (defun org-speedbar-set-agenda-restriction ()
  16779. "Restrict future agenda commands to the location at point in speedbar.
  16780. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  16781. (interactive)
  16782. (require 'org-agenda)
  16783. (let (p m tp np dir txt)
  16784. (cond
  16785. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16786. 'org-imenu t))
  16787. (setq m (get-text-property p 'org-imenu-marker))
  16788. (with-current-buffer (marker-buffer m)
  16789. (goto-char m)
  16790. (org-agenda-set-restriction-lock 'subtree)))
  16791. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16792. 'speedbar-function 'speedbar-find-file))
  16793. (setq tp (previous-single-property-change
  16794. (1+ p) 'speedbar-function)
  16795. np (next-single-property-change
  16796. tp 'speedbar-function)
  16797. dir (speedbar-line-directory)
  16798. txt (buffer-substring-no-properties (or tp (point-min))
  16799. (or np (point-max))))
  16800. (with-current-buffer (find-file-noselect
  16801. (let ((default-directory dir))
  16802. (expand-file-name txt)))
  16803. (unless (org-mode-p)
  16804. (error "Cannot restrict to non-Org-mode file"))
  16805. (org-agenda-set-restriction-lock 'file)))
  16806. (t (error "Don't know how to restrict Org-mode's agenda")))
  16807. (org-move-overlay org-speedbar-restriction-lock-overlay
  16808. (point-at-bol) (point-at-eol))
  16809. (setq current-prefix-arg nil)
  16810. (org-agenda-maybe-redo)))
  16811. (eval-after-load "speedbar"
  16812. '(progn
  16813. (speedbar-add-supported-extension ".org")
  16814. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  16815. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  16816. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  16817. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16818. (add-hook 'speedbar-visiting-tag-hook
  16819. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  16820. ;;; Fixes and Hacks for problems with other packages
  16821. ;; Make flyspell not check words in links, to not mess up our keymap
  16822. (defun org-mode-flyspell-verify ()
  16823. "Don't let flyspell put overlays at active buttons."
  16824. (and (not (get-text-property (point) 'keymap))
  16825. (not (get-text-property (point) 'org-no-flyspell))))
  16826. (defun org-remove-flyspell-overlays-in (beg end)
  16827. "Remove flyspell overlays in region."
  16828. (and (org-bound-and-true-p flyspell-mode)
  16829. (fboundp 'flyspell-delete-region-overlays)
  16830. (flyspell-delete-region-overlays beg end))
  16831. (add-text-properties beg end '(org-no-flyspell t)))
  16832. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  16833. (eval-after-load "bookmark"
  16834. '(if (boundp 'bookmark-after-jump-hook)
  16835. ;; We can use the hook
  16836. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  16837. ;; Hook not available, use advice
  16838. (defadvice bookmark-jump (after org-make-visible activate)
  16839. "Make the position visible."
  16840. (org-bookmark-jump-unhide))))
  16841. ;; Make sure saveplace shows the location if it was hidden
  16842. (eval-after-load "saveplace"
  16843. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  16844. "Make the position visible."
  16845. (org-bookmark-jump-unhide)))
  16846. ;; Make sure ecb shows the location if it was hidden
  16847. (eval-after-load "ecb"
  16848. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  16849. "Make hierarchy visible when jumping into location from ECB tree buffer."
  16850. (if (eq major-mode 'org-mode)
  16851. (org-show-context))))
  16852. (defun org-bookmark-jump-unhide ()
  16853. "Unhide the current position, to show the bookmark location."
  16854. (and (org-mode-p)
  16855. (or (org-invisible-p)
  16856. (save-excursion (goto-char (max (point-min) (1- (point))))
  16857. (org-invisible-p)))
  16858. (org-show-context 'bookmark-jump)))
  16859. ;; Make session.el ignore our circular variable
  16860. (eval-after-load "session"
  16861. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  16862. ;;;; Experimental code
  16863. (defun org-closed-in-range ()
  16864. "Sparse tree of items closed in a certain time range.
  16865. Still experimental, may disappear in the future."
  16866. (interactive)
  16867. ;; Get the time interval from the user.
  16868. (let* ((time1 (org-float-time
  16869. (org-read-date nil 'to-time nil "Starting date: ")))
  16870. (time2 (org-float-time
  16871. (org-read-date nil 'to-time nil "End date:")))
  16872. ;; callback function
  16873. (callback (lambda ()
  16874. (let ((time
  16875. (org-float-time
  16876. (apply 'encode-time
  16877. (org-parse-time-string
  16878. (match-string 1))))))
  16879. ;; check if time in interval
  16880. (and (>= time time1) (<= time time2))))))
  16881. ;; make tree, check each match with the callback
  16882. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  16883. ;;;; Finish up
  16884. (provide 'org)
  16885. (run-hooks 'org-load-hook)
  16886. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  16887. ;;; org.el ends here