org.el 686 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386
  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."
  2406. :group 'org-agenda
  2407. :type '(choice
  2408. (repeat :tag "List of files and directories" file)
  2409. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2410. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2411. "Regular expression to match files for `org-agenda-files'.
  2412. If any element in the list in that variable contains a directory instead
  2413. of a normal file, all files in that directory that are matched by this
  2414. regular expression will be included."
  2415. :group 'org-agenda
  2416. :type 'regexp)
  2417. (defcustom org-agenda-text-search-extra-files nil
  2418. "List of extra files to be searched by text search commands.
  2419. These files will be search in addition to the agenda files by the
  2420. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2421. Note that these files will only be searched for text search commands,
  2422. not for the other agenda views like todo lists, tag searches or the weekly
  2423. agenda. This variable is intended to list notes and possibly archive files
  2424. that should also be searched by these two commands.
  2425. In fact, if the first element in the list is the symbol `agenda-archives',
  2426. than all archive files of all agenda files will be added to the search
  2427. scope."
  2428. :group 'org-agenda
  2429. :type '(set :greedy t
  2430. (const :tag "Agenda Archives" agenda-archives)
  2431. (repeat :inline t (file))))
  2432. (if (fboundp 'defvaralias)
  2433. (defvaralias 'org-agenda-multi-occur-extra-files
  2434. 'org-agenda-text-search-extra-files))
  2435. (defcustom org-agenda-skip-unavailable-files nil
  2436. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2437. A nil value means to remove them, after a query, from the list."
  2438. :group 'org-agenda
  2439. :type 'boolean)
  2440. (defcustom org-calendar-to-agenda-key [?c]
  2441. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2442. The command `org-calendar-goto-agenda' will be bound to this key. The
  2443. default is the character `c' because then `c' can be used to switch back and
  2444. forth between agenda and calendar."
  2445. :group 'org-agenda
  2446. :type 'sexp)
  2447. (defcustom org-calendar-agenda-action-key [?k]
  2448. "The key to be installed in `calendar-mode-map' for agenda-action.
  2449. The command `org-agenda-action' will be bound to this key. The
  2450. default is the character `k' because we use the same key in the agenda."
  2451. :group 'org-agenda
  2452. :type 'sexp)
  2453. (defcustom org-calendar-insert-diary-entry-key [?i]
  2454. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2455. This option is irrelevant until `org-agenda-diary-file' has been configured
  2456. to point to an Org-mode file. When that is the case, the command
  2457. `org-agenda-diary-entry' will be bound to the key given here, by default
  2458. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2459. if you want to continue doing this, you need to change this to a different
  2460. key."
  2461. :group 'org-agenda
  2462. :type 'sexp)
  2463. (defcustom org-agenda-diary-file 'diary-file
  2464. "File to which to add new entries with the `i' key in agenda and calendar.
  2465. When this is the symbol `diary-file', the functionality in the Emacs
  2466. calendar will be used to add entries to the `diary-file'. But when this
  2467. points to a file, `org-agenda-diary-entry' will be used instead."
  2468. :group 'org-agenda
  2469. :type '(choice
  2470. (const :tag "The standard Emacs diary file" diary-file)
  2471. (file :tag "Special Org file diary entries")))
  2472. (eval-after-load "calendar"
  2473. '(progn
  2474. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2475. 'org-calendar-goto-agenda)
  2476. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2477. 'org-agenda-action)
  2478. (add-hook 'calendar-mode-hook
  2479. (lambda ()
  2480. (unless (eq org-agenda-diary-file 'diary-file)
  2481. (define-key calendar-mode-map
  2482. org-calendar-insert-diary-entry-key
  2483. 'org-agenda-diary-entry))))))
  2484. (defgroup org-latex nil
  2485. "Options for embedding LaTeX code into Org-mode."
  2486. :tag "Org LaTeX"
  2487. :group 'org)
  2488. (defcustom org-format-latex-options
  2489. '(:foreground default :background default :scale 1.0
  2490. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2491. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2492. "Options for creating images from LaTeX fragments.
  2493. This is a property list with the following properties:
  2494. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2495. `default' means use the foreground of the default face.
  2496. :background the background color, or \"Transparent\".
  2497. `default' means use the background of the default face.
  2498. :scale a scaling factor for the size of the images.
  2499. :html-foreground, :html-background, :html-scale
  2500. the same numbers for HTML export.
  2501. :matchers a list indicating which matchers should be used to
  2502. find LaTeX fragments. Valid members of this list are:
  2503. \"begin\" find environments
  2504. \"$1\" find single characters surrounded by $.$
  2505. \"$\" find math expressions surrounded by $...$
  2506. \"$$\" find math expressions surrounded by $$....$$
  2507. \"\\(\" find math expressions surrounded by \\(...\\)
  2508. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2509. :group 'org-latex
  2510. :type 'plist)
  2511. (defcustom org-format-latex-signal-error t
  2512. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2513. When nil, just push out a message."
  2514. :group 'org-latex
  2515. :type 'boolean)
  2516. (defcustom org-format-latex-header "\\documentclass{article}
  2517. \\usepackage{amssymb}
  2518. \\usepackage[usenames]{color}
  2519. \\usepackage{amsmath}
  2520. \\usepackage{latexsym}
  2521. \\usepackage[mathscr]{eucal}
  2522. \\pagestyle{empty} % do not remove
  2523. % The settings below are copied from fullpage.sty
  2524. \\setlength{\\textwidth}{\\paperwidth}
  2525. \\addtolength{\\textwidth}{-3cm}
  2526. \\setlength{\\oddsidemargin}{1.5cm}
  2527. \\addtolength{\\oddsidemargin}{-2.54cm}
  2528. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2529. \\setlength{\\textheight}{\\paperheight}
  2530. \\addtolength{\\textheight}{-\\headheight}
  2531. \\addtolength{\\textheight}{-\\headsep}
  2532. \\addtolength{\\textheight}{-\\footskip}
  2533. \\addtolength{\\textheight}{-3cm}
  2534. \\setlength{\\topmargin}{1.5cm}
  2535. \\addtolength{\\topmargin}{-2.54cm}"
  2536. "The document header used for processing LaTeX fragments.
  2537. It is imperative that this header make sure that no page number
  2538. appears on the page."
  2539. :group 'org-latex
  2540. :type 'string)
  2541. (defvar org-format-latex-header-extra nil)
  2542. ;; The following variable is defined here because is it also used
  2543. ;; when formatting latex fragments. Originally it was part of the
  2544. ;; LaTeX exporter, which is why the name includes "export".
  2545. (defcustom org-export-latex-packages-alist nil
  2546. "Alist of packages to be inserted in the header.
  2547. Each cell is of the format \( \"option\" . \"package\" \)."
  2548. :group 'org-export-latex
  2549. :type '(repeat
  2550. (list
  2551. (string :tag "option")
  2552. (string :tag "package"))))
  2553. (defgroup org-font-lock nil
  2554. "Font-lock settings for highlighting in Org-mode."
  2555. :tag "Org Font Lock"
  2556. :group 'org)
  2557. (defcustom org-level-color-stars-only nil
  2558. "Non-nil means fontify only the stars in each headline.
  2559. When nil, the entire headline is fontified.
  2560. Changing it requires restart of `font-lock-mode' to become effective
  2561. also in regions already fontified."
  2562. :group 'org-font-lock
  2563. :type 'boolean)
  2564. (defcustom org-hide-leading-stars nil
  2565. "Non-nil means hide the first N-1 stars in a headline.
  2566. This works by using the face `org-hide' for these stars. This
  2567. face is white for a light background, and black for a dark
  2568. background. You may have to customize the face `org-hide' to
  2569. make this work.
  2570. Changing it requires restart of `font-lock-mode' to become effective
  2571. also in regions already fontified.
  2572. You may also set this on a per-file basis by adding one of the following
  2573. lines to the buffer:
  2574. #+STARTUP: hidestars
  2575. #+STARTUP: showstars"
  2576. :group 'org-font-lock
  2577. :type 'boolean)
  2578. (defcustom org-fontify-done-headline nil
  2579. "Non-nil means change the face of a headline if it is marked DONE.
  2580. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2581. When this is non-nil, the headline after the keyword is set to the
  2582. `org-headline-done' as an additional indication."
  2583. :group 'org-font-lock
  2584. :type 'boolean)
  2585. (defcustom org-fontify-emphasized-text t
  2586. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2587. Changing this variable requires a restart of Emacs to take effect."
  2588. :group 'org-font-lock
  2589. :type 'boolean)
  2590. (defcustom org-fontify-whole-heading-line nil
  2591. "Non-nil means fontify the whole line for headings.
  2592. This is useful when setting a background color for the
  2593. org-level-* faces."
  2594. :group 'org-font-lock
  2595. :type 'boolean)
  2596. (defcustom org-highlight-latex-fragments-and-specials nil
  2597. "Non-nil means fontify what is treated specially by the exporters."
  2598. :group 'org-font-lock
  2599. :type 'boolean)
  2600. (defcustom org-hide-emphasis-markers nil
  2601. "Non-nil mean font-lock should hide the emphasis marker characters."
  2602. :group 'org-font-lock
  2603. :type 'boolean)
  2604. (defvar org-emph-re nil
  2605. "Regular expression for matching emphasis.")
  2606. (defvar org-verbatim-re nil
  2607. "Regular expression for matching verbatim text.")
  2608. (defvar org-emphasis-regexp-components) ; defined just below
  2609. (defvar org-emphasis-alist) ; defined just below
  2610. (defun org-set-emph-re (var val)
  2611. "Set variable and compute the emphasis regular expression."
  2612. (set var val)
  2613. (when (and (boundp 'org-emphasis-alist)
  2614. (boundp 'org-emphasis-regexp-components)
  2615. org-emphasis-alist org-emphasis-regexp-components)
  2616. (let* ((e org-emphasis-regexp-components)
  2617. (pre (car e))
  2618. (post (nth 1 e))
  2619. (border (nth 2 e))
  2620. (body (nth 3 e))
  2621. (nl (nth 4 e))
  2622. (body1 (concat body "*?"))
  2623. (markers (mapconcat 'car org-emphasis-alist ""))
  2624. (vmarkers (mapconcat
  2625. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2626. org-emphasis-alist "")))
  2627. ;; make sure special characters appear at the right position in the class
  2628. (if (string-match "\\^" markers)
  2629. (setq markers (concat (replace-match "" t t markers) "^")))
  2630. (if (string-match "-" markers)
  2631. (setq markers (concat (replace-match "" t t markers) "-")))
  2632. (if (string-match "\\^" vmarkers)
  2633. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2634. (if (string-match "-" vmarkers)
  2635. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2636. (if (> nl 0)
  2637. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2638. (int-to-string nl) "\\}")))
  2639. ;; Make the regexp
  2640. (setq org-emph-re
  2641. (concat "\\([" pre "]\\|^\\)"
  2642. "\\("
  2643. "\\([" markers "]\\)"
  2644. "\\("
  2645. "[^" border "]\\|"
  2646. "[^" border "]"
  2647. body1
  2648. "[^" border "]"
  2649. "\\)"
  2650. "\\3\\)"
  2651. "\\([" post "]\\|$\\)"))
  2652. (setq org-verbatim-re
  2653. (concat "\\([" pre "]\\|^\\)"
  2654. "\\("
  2655. "\\([" vmarkers "]\\)"
  2656. "\\("
  2657. "[^" border "]\\|"
  2658. "[^" border "]"
  2659. body1
  2660. "[^" border "]"
  2661. "\\)"
  2662. "\\3\\)"
  2663. "\\([" post "]\\|$\\)")))))
  2664. (defcustom org-emphasis-regexp-components
  2665. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2666. "Components used to build the regular expression for emphasis.
  2667. This is a list with 6 entries. Terminology: In an emphasis string
  2668. like \" *strong word* \", we call the initial space PREMATCH, the final
  2669. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2670. and \"trong wor\" is the body. The different components in this variable
  2671. specify what is allowed/forbidden in each part:
  2672. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2673. post Chars allowed as postmatch. End of line will be allowed too.
  2674. border The chars *forbidden* as border characters.
  2675. body-regexp A regexp like \".\" to match a body character. Don't use
  2676. non-shy groups here, and don't allow newline here.
  2677. newline The maximum number of newlines allowed in an emphasis exp.
  2678. Use customize to modify this, or restart Emacs after changing it."
  2679. :group 'org-font-lock
  2680. :set 'org-set-emph-re
  2681. :type '(list
  2682. (sexp :tag "Allowed chars in pre ")
  2683. (sexp :tag "Allowed chars in post ")
  2684. (sexp :tag "Forbidden chars in border ")
  2685. (sexp :tag "Regexp for body ")
  2686. (integer :tag "number of newlines allowed")
  2687. (option (boolean :tag "Please ignore this button"))))
  2688. (defcustom org-emphasis-alist
  2689. `(("*" bold "<b>" "</b>")
  2690. ("/" italic "<i>" "</i>")
  2691. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2692. ("=" org-code "<code>" "</code>" verbatim)
  2693. ("~" org-verbatim "<code>" "</code>" verbatim)
  2694. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2695. "<del>" "</del>")
  2696. )
  2697. "Special syntax for emphasized text.
  2698. Text starting and ending with a special character will be emphasized, for
  2699. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2700. characters, the face to be used by font-lock for highlighting in Org-mode
  2701. Emacs buffers, and the HTML tags to be used for this.
  2702. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2703. Use customize to modify this, or restart Emacs after changing it."
  2704. :group 'org-font-lock
  2705. :set 'org-set-emph-re
  2706. :type '(repeat
  2707. (list
  2708. (string :tag "Marker character")
  2709. (choice
  2710. (face :tag "Font-lock-face")
  2711. (plist :tag "Face property list"))
  2712. (string :tag "HTML start tag")
  2713. (string :tag "HTML end tag")
  2714. (option (const verbatim)))))
  2715. (defvar org-protecting-blocks
  2716. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2717. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2718. This is needed for font-lock setup.")
  2719. ;;; Miscellaneous options
  2720. (defgroup org-completion nil
  2721. "Completion in Org-mode."
  2722. :tag "Org Completion"
  2723. :group 'org)
  2724. (defcustom org-completion-use-ido nil
  2725. "Non-nil means use ido completion wherever possible.
  2726. Note that `ido-mode' must be active for this variable to be relevant.
  2727. If you decide to turn this variable on, you might well want to turn off
  2728. `org-outline-path-complete-in-steps'.
  2729. See also `org-completion-use-iswitchb'."
  2730. :group 'org-completion
  2731. :type 'boolean)
  2732. (defcustom org-completion-use-iswitchb nil
  2733. "Non-nil means use iswitchb completion wherever possible.
  2734. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2735. If you decide to turn this variable on, you might well want to turn off
  2736. `org-outline-path-complete-in-steps'.
  2737. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  2738. :group 'org-completion
  2739. :type 'boolean)
  2740. (defcustom org-completion-fallback-command 'hippie-expand
  2741. "The expansion command called by \\[org-complete] in normal context.
  2742. Normal means no org-mode-specific context."
  2743. :group 'org-completion
  2744. :type 'function)
  2745. ;;; Functions and variables from their packages
  2746. ;; Declared here to avoid compiler warnings
  2747. ;; XEmacs only
  2748. (defvar outline-mode-menu-heading)
  2749. (defvar outline-mode-menu-show)
  2750. (defvar outline-mode-menu-hide)
  2751. (defvar zmacs-regions) ; XEmacs regions
  2752. ;; Emacs only
  2753. (defvar mark-active)
  2754. ;; Various packages
  2755. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2756. (declare-function calendar-forward-day "cal-move" (arg))
  2757. (declare-function calendar-goto-date "cal-move" (date))
  2758. (declare-function calendar-goto-today "cal-move" ())
  2759. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2760. (defvar calc-embedded-close-formula)
  2761. (defvar calc-embedded-open-formula)
  2762. (declare-function cdlatex-tab "ext:cdlatex" ())
  2763. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2764. (defvar font-lock-unfontify-region-function)
  2765. (declare-function iswitchb-read-buffer "iswitchb"
  2766. (prompt &optional default require-match start matches-set))
  2767. (defvar iswitchb-temp-buflist)
  2768. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2769. (defvar org-agenda-tags-todo-honor-ignore-options)
  2770. (declare-function org-agenda-skip "org-agenda" ())
  2771. (declare-function
  2772. org-format-agenda-item "org-agenda"
  2773. (extra txt &optional category tags dotime noprefix remove-re habitp))
  2774. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2775. (declare-function org-agenda-change-all-lines "org-agenda"
  2776. (newhead hdmarker &optional fixface just-this))
  2777. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2778. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2779. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2780. (beg end))
  2781. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2782. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2783. "org-agenda" (&optional end))
  2784. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2785. (declare-function org-indent-mode "org-indent" (&optional arg))
  2786. (declare-function parse-time-string "parse-time" (string))
  2787. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  2788. (defvar remember-data-file)
  2789. (defvar texmathp-why)
  2790. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2791. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2792. (defvar w3m-current-url)
  2793. (defvar w3m-current-title)
  2794. (defvar org-latex-regexps)
  2795. ;;; Autoload and prepare some org modules
  2796. ;; Some table stuff that needs to be defined here, because it is used
  2797. ;; by the functions setting up org-mode or checking for table context.
  2798. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2799. "Detects an org-type or table-type table.")
  2800. (defconst org-table-line-regexp "^[ \t]*|"
  2801. "Detects an org-type table line.")
  2802. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2803. "Detects an org-type table line.")
  2804. (defconst org-table-hline-regexp "^[ \t]*|-"
  2805. "Detects an org-type table hline.")
  2806. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2807. "Detects a table-type table hline.")
  2808. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2809. "Searching from within a table (any type) this finds the first line
  2810. outside the table.")
  2811. ;; Autoload the functions in org-table.el that are needed by functions here.
  2812. (eval-and-compile
  2813. (org-autoload "org-table"
  2814. '(org-table-align org-table-begin org-table-blank-field
  2815. org-table-convert org-table-convert-region org-table-copy-down
  2816. org-table-copy-region org-table-create
  2817. org-table-create-or-convert-from-region
  2818. org-table-create-with-table.el org-table-current-dline
  2819. org-table-cut-region org-table-delete-column org-table-edit-field
  2820. org-table-edit-formulas org-table-end org-table-eval-formula
  2821. org-table-export org-table-field-info
  2822. org-table-get-stored-formulas org-table-goto-column
  2823. org-table-hline-and-move org-table-import org-table-insert-column
  2824. org-table-insert-hline org-table-insert-row org-table-iterate
  2825. org-table-justify-field-maybe org-table-kill-row
  2826. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2827. org-table-move-column org-table-move-column-left
  2828. org-table-move-column-right org-table-move-row
  2829. org-table-move-row-down org-table-move-row-up
  2830. org-table-next-field org-table-next-row org-table-paste-rectangle
  2831. org-table-previous-field org-table-recalculate
  2832. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2833. org-table-toggle-coordinate-overlays
  2834. org-table-toggle-formula-debugger org-table-wrap-region
  2835. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2836. (defun org-at-table-p (&optional table-type)
  2837. "Return t if the cursor is inside an org-type table.
  2838. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2839. (if org-enable-table-editor
  2840. (save-excursion
  2841. (beginning-of-line 1)
  2842. (looking-at (if table-type org-table-any-line-regexp
  2843. org-table-line-regexp)))
  2844. nil))
  2845. (defsubst org-table-p () (org-at-table-p))
  2846. (defun org-at-table.el-p ()
  2847. "Return t if and only if we are at a table.el table."
  2848. (and (org-at-table-p 'any)
  2849. (save-excursion
  2850. (goto-char (org-table-begin 'any))
  2851. (looking-at org-table1-hline-regexp))))
  2852. (defun org-table-recognize-table.el ()
  2853. "If there is a table.el table nearby, recognize it and move into it."
  2854. (if org-table-tab-recognizes-table.el
  2855. (if (org-at-table.el-p)
  2856. (progn
  2857. (beginning-of-line 1)
  2858. (if (looking-at org-table-dataline-regexp)
  2859. nil
  2860. (if (looking-at org-table1-hline-regexp)
  2861. (progn
  2862. (beginning-of-line 2)
  2863. (if (looking-at org-table-any-border-regexp)
  2864. (beginning-of-line -1)))))
  2865. (if (re-search-forward "|" (org-table-end t) t)
  2866. (progn
  2867. (require 'table)
  2868. (if (table--at-cell-p (point))
  2869. t
  2870. (message "recognizing table.el table...")
  2871. (table-recognize-table)
  2872. (message "recognizing table.el table...done")))
  2873. (error "This should not happen..."))
  2874. t)
  2875. nil)
  2876. nil))
  2877. (defun org-at-table-hline-p ()
  2878. "Return t if the cursor is inside a hline in a table."
  2879. (if org-enable-table-editor
  2880. (save-excursion
  2881. (beginning-of-line 1)
  2882. (looking-at org-table-hline-regexp))
  2883. nil))
  2884. (defvar org-table-clean-did-remove-column nil)
  2885. (defun org-table-map-tables (function)
  2886. "Apply FUNCTION to the start of all tables in the buffer."
  2887. (save-excursion
  2888. (save-restriction
  2889. (widen)
  2890. (goto-char (point-min))
  2891. (while (re-search-forward org-table-any-line-regexp nil t)
  2892. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2893. (beginning-of-line 1)
  2894. (when (looking-at org-table-line-regexp)
  2895. (save-excursion (funcall function))
  2896. (or (looking-at org-table-line-regexp)
  2897. (forward-char 1)))
  2898. (re-search-forward org-table-any-border-regexp nil 1))))
  2899. (message "Mapping tables: done"))
  2900. ;; Declare and autoload functions from org-exp.el & Co
  2901. (declare-function org-default-export-plist "org-exp")
  2902. (declare-function org-infile-export-plist "org-exp")
  2903. (declare-function org-get-current-options "org-exp")
  2904. (eval-and-compile
  2905. (org-autoload "org-exp"
  2906. '(org-export org-export-visible
  2907. org-insert-export-options-template
  2908. org-table-clean-before-export))
  2909. (org-autoload "org-ascii"
  2910. '(org-export-as-ascii org-export-ascii-preprocess
  2911. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2912. org-export-region-as-ascii))
  2913. (org-autoload "org-latex"
  2914. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  2915. org-replace-region-by-latex org-export-region-as-latex
  2916. org-export-as-latex org-export-as-pdf
  2917. org-export-as-pdf-and-open))
  2918. (org-autoload "org-html"
  2919. '(org-export-as-html-and-open
  2920. org-export-as-html-batch org-export-as-html-to-buffer
  2921. org-replace-region-by-html org-export-region-as-html
  2922. org-export-as-html))
  2923. (org-autoload "org-docbook"
  2924. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  2925. org-replace-region-by-docbook org-export-region-as-docbook
  2926. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  2927. org-export-as-docbook))
  2928. (org-autoload "org-icalendar"
  2929. '(org-export-icalendar-this-file
  2930. org-export-icalendar-all-agenda-files
  2931. org-export-icalendar-combine-agenda-files))
  2932. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  2933. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  2934. ;; Declare and autoload functions from org-agenda.el
  2935. (eval-and-compile
  2936. (org-autoload "org-agenda"
  2937. '(org-agenda org-agenda-list org-search-view
  2938. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2939. org-diary org-agenda-to-appt
  2940. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2941. ;; Autoload org-remember
  2942. (eval-and-compile
  2943. (org-autoload "org-remember"
  2944. '(org-remember-insinuate org-remember-annotation
  2945. org-remember-apply-template org-remember org-remember-handler)))
  2946. ;; Autoload org-clock.el
  2947. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2948. (beg end))
  2949. (declare-function org-clock-update-mode-line "org-clock" ())
  2950. (declare-function org-resolve-clocks "org-clock"
  2951. (&optional also-non-dangling-p prompt last-valid))
  2952. (defvar org-clock-start-time)
  2953. (defvar org-clock-marker (make-marker)
  2954. "Marker recording the last clock-in.")
  2955. (defvar org-clock-hd-marker (make-marker)
  2956. "Marker recording the last clock-in, but the headline position.")
  2957. (defvar org-clock-heading ""
  2958. "The heading of the current clock entry.")
  2959. (defun org-clock-is-active ()
  2960. "Return non-nil if clock is currently running.
  2961. The return value is actually the clock marker."
  2962. (marker-buffer org-clock-marker))
  2963. (eval-and-compile
  2964. (org-autoload
  2965. "org-clock"
  2966. '(org-clock-in org-clock-out org-clock-cancel
  2967. org-clock-goto org-clock-sum org-clock-display
  2968. org-clock-remove-overlays org-clock-report
  2969. org-clocktable-shift org-dblock-write:clocktable
  2970. org-get-clocktable org-resolve-clocks)))
  2971. (defun org-clock-update-time-maybe ()
  2972. "If this is a CLOCK line, update it and return t.
  2973. Otherwise, return nil."
  2974. (interactive)
  2975. (save-excursion
  2976. (beginning-of-line 1)
  2977. (skip-chars-forward " \t")
  2978. (when (looking-at org-clock-string)
  2979. (let ((re (concat "[ \t]*" org-clock-string
  2980. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2981. "\\([ \t]*=>.*\\)?\\)?"))
  2982. ts te h m s neg)
  2983. (cond
  2984. ((not (looking-at re))
  2985. nil)
  2986. ((not (match-end 2))
  2987. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2988. (> org-clock-marker (point))
  2989. (<= org-clock-marker (point-at-eol)))
  2990. ;; The clock is running here
  2991. (setq org-clock-start-time
  2992. (apply 'encode-time
  2993. (org-parse-time-string (match-string 1))))
  2994. (org-clock-update-mode-line)))
  2995. (t
  2996. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2997. (end-of-line 1)
  2998. (setq ts (match-string 1)
  2999. te (match-string 3))
  3000. (setq s (- (org-float-time
  3001. (apply 'encode-time (org-parse-time-string te)))
  3002. (org-float-time
  3003. (apply 'encode-time (org-parse-time-string ts))))
  3004. neg (< s 0)
  3005. s (abs s)
  3006. h (floor (/ s 3600))
  3007. s (- s (* 3600 h))
  3008. m (floor (/ s 60))
  3009. s (- s (* 60 s)))
  3010. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3011. t))))))
  3012. (defun org-check-running-clock ()
  3013. "Check if the current buffer contains the running clock.
  3014. If yes, offer to stop it and to save the buffer with the changes."
  3015. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3016. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3017. (buffer-name))))
  3018. (org-clock-out)
  3019. (when (y-or-n-p "Save changed buffer?")
  3020. (save-buffer))))
  3021. (defun org-clocktable-try-shift (dir n)
  3022. "Check if this line starts a clock table, if yes, shift the time block."
  3023. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  3024. (org-clocktable-shift dir n)))
  3025. ;; Autoload org-timer.el
  3026. (eval-and-compile
  3027. (org-autoload
  3028. "org-timer"
  3029. '(org-timer-start org-timer org-timer-item
  3030. org-timer-change-times-in-region
  3031. org-timer-set-timer
  3032. org-timer-reset-timers
  3033. org-timer-show-remaining-time)))
  3034. ;; Autoload org-feed.el
  3035. (eval-and-compile
  3036. (org-autoload
  3037. "org-feed"
  3038. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3039. ;; Autoload org-indent.el
  3040. ;; Define the variable already here, to make sure we have it.
  3041. (defvar org-indent-mode nil
  3042. "Non-nil if Org-Indent mode is enabled.
  3043. Use the command `org-indent-mode' to change this variable.")
  3044. (eval-and-compile
  3045. (org-autoload
  3046. "org-indent"
  3047. '(org-indent-mode)))
  3048. ;; Autoload org-mobile.el
  3049. (eval-and-compile
  3050. (org-autoload
  3051. "org-mobile"
  3052. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3053. ;; Autoload archiving code
  3054. ;; The stuff that is needed for cycling and tags has to be defined here.
  3055. (defgroup org-archive nil
  3056. "Options concerning archiving in Org-mode."
  3057. :tag "Org Archive"
  3058. :group 'org-structure)
  3059. (defcustom org-archive-location "%s_archive::"
  3060. "The location where subtrees should be archived.
  3061. The value of this variable is a string, consisting of two parts,
  3062. separated by a double-colon. The first part is a filename and
  3063. the second part is a headline.
  3064. When the filename is omitted, archiving happens in the same file.
  3065. %s in the filename will be replaced by the current file
  3066. name (without the directory part). Archiving to a different file
  3067. is useful to keep archived entries from contributing to the
  3068. Org-mode Agenda.
  3069. The archived entries will be filed as subtrees of the specified
  3070. headline. When the headline is omitted, the subtrees are simply
  3071. filed away at the end of the file, as top-level entries. Also in
  3072. the heading you can use %s to represent the file name, this can be
  3073. useful when using the same archive for a number of different files.
  3074. Here are a few examples:
  3075. \"%s_archive::\"
  3076. If the current file is Projects.org, archive in file
  3077. Projects.org_archive, as top-level trees. This is the default.
  3078. \"::* Archived Tasks\"
  3079. Archive in the current file, under the top-level headline
  3080. \"* Archived Tasks\".
  3081. \"~/org/archive.org::\"
  3082. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3083. \"~/org/archive.org::From %s\"
  3084. Archive in file ~/org/archive.org (absolute path), under headlines
  3085. \"From FILENAME\" where file name is the current file name.
  3086. \"basement::** Finished Tasks\"
  3087. Archive in file ./basement (relative path), as level 3 trees
  3088. below the level 2 heading \"** Finished Tasks\".
  3089. You may set this option on a per-file basis by adding to the buffer a
  3090. line like
  3091. #+ARCHIVE: basement::** Finished Tasks
  3092. You may also define it locally for a subtree by setting an ARCHIVE property
  3093. in the entry. If such a property is found in an entry, or anywhere up
  3094. the hierarchy, it will be used."
  3095. :group 'org-archive
  3096. :type 'string)
  3097. (defcustom org-archive-tag "ARCHIVE"
  3098. "The tag that marks a subtree as archived.
  3099. An archived subtree does not open during visibility cycling, and does
  3100. not contribute to the agenda listings.
  3101. After changing this, font-lock must be restarted in the relevant buffers to
  3102. get the proper fontification."
  3103. :group 'org-archive
  3104. :group 'org-keywords
  3105. :type 'string)
  3106. (defcustom org-agenda-skip-archived-trees t
  3107. "Non-nil means the agenda will skip any items located in archived trees.
  3108. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3109. variable is no longer recommended, you should leave it at the value t.
  3110. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3111. :group 'org-archive
  3112. :group 'org-agenda-skip
  3113. :type 'boolean)
  3114. (defcustom org-columns-skip-archived-trees t
  3115. "Non-nil means ignore archived trees when creating column view."
  3116. :group 'org-archive
  3117. :group 'org-properties
  3118. :type 'boolean)
  3119. (defcustom org-cycle-open-archived-trees nil
  3120. "Non-nil means `org-cycle' will open archived trees.
  3121. An archived tree is a tree marked with the tag ARCHIVE.
  3122. When nil, archived trees will stay folded. You can still open them with
  3123. normal outline commands like `show-all', but not with the cycling commands."
  3124. :group 'org-archive
  3125. :group 'org-cycle
  3126. :type 'boolean)
  3127. (defcustom org-sparse-tree-open-archived-trees nil
  3128. "Non-nil means sparse tree construction shows matches in archived trees.
  3129. When nil, matches in these trees are highlighted, but the trees are kept in
  3130. collapsed state."
  3131. :group 'org-archive
  3132. :group 'org-sparse-trees
  3133. :type 'boolean)
  3134. (defun org-cycle-hide-archived-subtrees (state)
  3135. "Re-hide all archived subtrees after a visibility state change."
  3136. (when (and (not org-cycle-open-archived-trees)
  3137. (not (memq state '(overview folded))))
  3138. (save-excursion
  3139. (let* ((globalp (memq state '(contents all)))
  3140. (beg (if globalp (point-min) (point)))
  3141. (end (if globalp (point-max) (org-end-of-subtree t))))
  3142. (org-hide-archived-subtrees beg end)
  3143. (goto-char beg)
  3144. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3145. (message "%s" (substitute-command-keys
  3146. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3147. (defun org-force-cycle-archived ()
  3148. "Cycle subtree even if it is archived."
  3149. (interactive)
  3150. (setq this-command 'org-cycle)
  3151. (let ((org-cycle-open-archived-trees t))
  3152. (call-interactively 'org-cycle)))
  3153. (defun org-hide-archived-subtrees (beg end)
  3154. "Re-hide all archived subtrees after a visibility state change."
  3155. (save-excursion
  3156. (let* ((re (concat ":" org-archive-tag ":")))
  3157. (goto-char beg)
  3158. (while (re-search-forward re end t)
  3159. (when (org-on-heading-p)
  3160. (org-flag-subtree t)
  3161. (org-end-of-subtree t))))))
  3162. (defun org-flag-subtree (flag)
  3163. (save-excursion
  3164. (org-back-to-heading t)
  3165. (outline-end-of-heading)
  3166. (outline-flag-region (point)
  3167. (progn (org-end-of-subtree t) (point))
  3168. flag)))
  3169. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3170. (eval-and-compile
  3171. (org-autoload "org-archive"
  3172. '(org-add-archive-files org-archive-subtree
  3173. org-archive-to-archive-sibling org-toggle-archive-tag
  3174. org-archive-subtree-default
  3175. org-archive-subtree-default-with-confirmation)))
  3176. ;; Autoload Column View Code
  3177. (declare-function org-columns-number-to-string "org-colview")
  3178. (declare-function org-columns-get-format-and-top-level "org-colview")
  3179. (declare-function org-columns-compute "org-colview")
  3180. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3181. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3182. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3183. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3184. ;; Autoload ID code
  3185. (declare-function org-id-store-link "org-id")
  3186. (declare-function org-id-locations-load "org-id")
  3187. (declare-function org-id-locations-save "org-id")
  3188. (defvar org-id-track-globally)
  3189. (org-autoload "org-id"
  3190. '(org-id-get-create org-id-new org-id-copy org-id-get
  3191. org-id-get-with-outline-path-completion
  3192. org-id-get-with-outline-drilling
  3193. org-id-goto org-id-find org-id-store-link))
  3194. ;; Autoload Plotting Code
  3195. (org-autoload "org-plot"
  3196. '(org-plot/gnuplot))
  3197. ;;; Variables for pre-computed regular expressions, all buffer local
  3198. (defvar org-drawer-regexp nil
  3199. "Matches first line of a hidden block.")
  3200. (make-variable-buffer-local 'org-drawer-regexp)
  3201. (defvar org-todo-regexp nil
  3202. "Matches any of the TODO state keywords.")
  3203. (make-variable-buffer-local 'org-todo-regexp)
  3204. (defvar org-not-done-regexp nil
  3205. "Matches any of the TODO state keywords except the last one.")
  3206. (make-variable-buffer-local 'org-not-done-regexp)
  3207. (defvar org-not-done-heading-regexp nil
  3208. "Matches a TODO headline that is not done.")
  3209. (make-variable-buffer-local 'org-not-done-regexp)
  3210. (defvar org-todo-line-regexp nil
  3211. "Matches a headline and puts TODO state into group 2 if present.")
  3212. (make-variable-buffer-local 'org-todo-line-regexp)
  3213. (defvar org-complex-heading-regexp nil
  3214. "Matches a headline and puts everything into groups:
  3215. group 1: the stars
  3216. group 2: The todo keyword, maybe
  3217. group 3: Priority cookie
  3218. group 4: True headline
  3219. group 5: Tags")
  3220. (make-variable-buffer-local 'org-complex-heading-regexp)
  3221. (defvar org-complex-heading-regexp-format nil)
  3222. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3223. (defvar org-todo-line-tags-regexp nil
  3224. "Matches a headline and puts TODO state into group 2 if present.
  3225. Also put tags into group 4 if tags are present.")
  3226. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3227. (defvar org-nl-done-regexp nil
  3228. "Matches newline followed by a headline with the DONE keyword.")
  3229. (make-variable-buffer-local 'org-nl-done-regexp)
  3230. (defvar org-looking-at-done-regexp nil
  3231. "Matches the DONE keyword a point.")
  3232. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3233. (defvar org-ds-keyword-length 12
  3234. "Maximum length of the Deadline and SCHEDULED keywords.")
  3235. (make-variable-buffer-local 'org-ds-keyword-length)
  3236. (defvar org-deadline-regexp nil
  3237. "Matches the DEADLINE keyword.")
  3238. (make-variable-buffer-local 'org-deadline-regexp)
  3239. (defvar org-deadline-time-regexp nil
  3240. "Matches the DEADLINE keyword together with a time stamp.")
  3241. (make-variable-buffer-local 'org-deadline-time-regexp)
  3242. (defvar org-deadline-line-regexp nil
  3243. "Matches the DEADLINE keyword and the rest of the line.")
  3244. (make-variable-buffer-local 'org-deadline-line-regexp)
  3245. (defvar org-scheduled-regexp nil
  3246. "Matches the SCHEDULED keyword.")
  3247. (make-variable-buffer-local 'org-scheduled-regexp)
  3248. (defvar org-scheduled-time-regexp nil
  3249. "Matches the SCHEDULED keyword together with a time stamp.")
  3250. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3251. (defvar org-closed-time-regexp nil
  3252. "Matches the CLOSED keyword together with a time stamp.")
  3253. (make-variable-buffer-local 'org-closed-time-regexp)
  3254. (defvar org-keyword-time-regexp nil
  3255. "Matches any of the 4 keywords, together with the time stamp.")
  3256. (make-variable-buffer-local 'org-keyword-time-regexp)
  3257. (defvar org-keyword-time-not-clock-regexp nil
  3258. "Matches any of the 3 keywords, together with the time stamp.")
  3259. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3260. (defvar org-maybe-keyword-time-regexp nil
  3261. "Matches a timestamp, possibly preceeded by a keyword.")
  3262. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3263. (defvar org-planning-or-clock-line-re nil
  3264. "Matches a line with planning or clock info.")
  3265. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3266. (defvar org-all-time-keywords nil
  3267. "List of time keywords.")
  3268. (make-variable-buffer-local 'org-all-time-keywords)
  3269. (defconst org-plain-time-of-day-regexp
  3270. (concat
  3271. "\\(\\<[012]?[0-9]"
  3272. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3273. "\\(--?"
  3274. "\\(\\<[012]?[0-9]"
  3275. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3276. "\\)?")
  3277. "Regular expression to match a plain time or time range.
  3278. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3279. groups carry important information:
  3280. 0 the full match
  3281. 1 the first time, range or not
  3282. 8 the second time, if it is a range.")
  3283. (defconst org-plain-time-extension-regexp
  3284. (concat
  3285. "\\(\\<[012]?[0-9]"
  3286. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3287. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3288. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3289. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3290. groups carry important information:
  3291. 0 the full match
  3292. 7 hours of duration
  3293. 9 minutes of duration")
  3294. (defconst org-stamp-time-of-day-regexp
  3295. (concat
  3296. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3297. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3298. "\\(--?"
  3299. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3300. "Regular expression to match a timestamp time or time range.
  3301. After a match, the following groups carry important information:
  3302. 0 the full match
  3303. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3304. 2 the first time, range or not
  3305. 4 the second time, if it is a range.")
  3306. (defconst org-startup-options
  3307. '(("fold" org-startup-folded t)
  3308. ("overview" org-startup-folded t)
  3309. ("nofold" org-startup-folded nil)
  3310. ("showall" org-startup-folded nil)
  3311. ("showeverything" org-startup-folded showeverything)
  3312. ("content" org-startup-folded content)
  3313. ("indent" org-startup-indented t)
  3314. ("noindent" org-startup-indented nil)
  3315. ("hidestars" org-hide-leading-stars t)
  3316. ("showstars" org-hide-leading-stars nil)
  3317. ("odd" org-odd-levels-only t)
  3318. ("oddeven" org-odd-levels-only nil)
  3319. ("align" org-startup-align-all-tables t)
  3320. ("noalign" org-startup-align-all-tables nil)
  3321. ("customtime" org-display-custom-times t)
  3322. ("logdone" org-log-done time)
  3323. ("lognotedone" org-log-done note)
  3324. ("nologdone" org-log-done nil)
  3325. ("lognoteclock-out" org-log-note-clock-out t)
  3326. ("nolognoteclock-out" org-log-note-clock-out nil)
  3327. ("logrepeat" org-log-repeat state)
  3328. ("lognoterepeat" org-log-repeat note)
  3329. ("nologrepeat" org-log-repeat nil)
  3330. ("logreschedule" org-log-reschedule time)
  3331. ("lognotereschedule" org-log-reschedule note)
  3332. ("nologreschedule" org-log-reschedule nil)
  3333. ("logredeadline" org-log-redeadline time)
  3334. ("lognoteredeadline" org-log-redeadline note)
  3335. ("nologredeadline" org-log-redeadline nil)
  3336. ("logrefile" org-log-refile time)
  3337. ("lognoterefile" org-log-refile note)
  3338. ("nologrefile" org-log-refile nil)
  3339. ("fninline" org-footnote-define-inline t)
  3340. ("nofninline" org-footnote-define-inline nil)
  3341. ("fnlocal" org-footnote-section nil)
  3342. ("fnauto" org-footnote-auto-label t)
  3343. ("fnprompt" org-footnote-auto-label nil)
  3344. ("fnconfirm" org-footnote-auto-label confirm)
  3345. ("fnplain" org-footnote-auto-label plain)
  3346. ("fnadjust" org-footnote-auto-adjust t)
  3347. ("nofnadjust" org-footnote-auto-adjust nil)
  3348. ("constcgs" constants-unit-system cgs)
  3349. ("constSI" constants-unit-system SI)
  3350. ("noptag" org-tag-persistent-alist nil)
  3351. ("hideblocks" org-hide-block-startup t)
  3352. ("nohideblocks" org-hide-block-startup nil)
  3353. ("beamer" org-startup-with-beamer-mode t))
  3354. "Variable associated with STARTUP options for org-mode.
  3355. Each element is a list of three items: The startup options as written
  3356. in the #+STARTUP line, the corresponding variable, and the value to
  3357. set this variable to if the option is found. An optional forth element PUSH
  3358. means to push this value onto the list in the variable.")
  3359. (defun org-set-regexps-and-options ()
  3360. "Precompute regular expressions for current buffer."
  3361. (when (org-mode-p)
  3362. (org-set-local 'org-todo-kwd-alist nil)
  3363. (org-set-local 'org-todo-key-alist nil)
  3364. (org-set-local 'org-todo-key-trigger nil)
  3365. (org-set-local 'org-todo-keywords-1 nil)
  3366. (org-set-local 'org-done-keywords nil)
  3367. (org-set-local 'org-todo-heads nil)
  3368. (org-set-local 'org-todo-sets nil)
  3369. (org-set-local 'org-todo-log-states nil)
  3370. (org-set-local 'org-file-properties nil)
  3371. (org-set-local 'org-file-tags nil)
  3372. (let ((re (org-make-options-regexp
  3373. '("CATEGORY" "TODO" "COLUMNS"
  3374. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3375. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS")
  3376. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3377. (splitre "[ \t]+")
  3378. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3379. tail sep kws1 prio props ftags drawers beamer-p
  3380. ext-setup-or-nil setup-contents (start 0))
  3381. (save-excursion
  3382. (save-restriction
  3383. (widen)
  3384. (goto-char (point-min))
  3385. (while (or (and ext-setup-or-nil
  3386. (string-match re ext-setup-or-nil start)
  3387. (setq start (match-end 0)))
  3388. (and (setq ext-setup-or-nil nil start 0)
  3389. (re-search-forward re nil t)))
  3390. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3391. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3392. (cond
  3393. ((equal key "CATEGORY")
  3394. (if (string-match "[ \t]+$" value)
  3395. (setq value (replace-match "" t t value)))
  3396. (setq cat value))
  3397. ((member key '("SEQ_TODO" "TODO"))
  3398. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3399. ((equal key "TYP_TODO")
  3400. (push (cons 'type (org-split-string value splitre)) kwds))
  3401. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3402. ;; general TODO-like setup
  3403. (push (cons (intern (downcase (match-string 1 key)))
  3404. (org-split-string value splitre)) kwds))
  3405. ((equal key "TAGS")
  3406. (setq tags (append tags (if tags '("\\n") nil)
  3407. (org-split-string value splitre))))
  3408. ((equal key "COLUMNS")
  3409. (org-set-local 'org-columns-default-format value))
  3410. ((equal key "LINK")
  3411. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3412. (push (cons (match-string 1 value)
  3413. (org-trim (match-string 2 value)))
  3414. links)))
  3415. ((equal key "PRIORITIES")
  3416. (setq prio (org-split-string value " +")))
  3417. ((equal key "PROPERTY")
  3418. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3419. (push (cons (match-string 1 value) (match-string 2 value))
  3420. props)))
  3421. ((equal key "FILETAGS")
  3422. (when (string-match "\\S-" value)
  3423. (setq ftags
  3424. (append
  3425. ftags
  3426. (apply 'append
  3427. (mapcar (lambda (x) (org-split-string x ":"))
  3428. (org-split-string value)))))))
  3429. ((equal key "DRAWERS")
  3430. (setq drawers (org-split-string value splitre)))
  3431. ((equal key "CONSTANTS")
  3432. (setq const (append const (org-split-string value splitre))))
  3433. ((equal key "STARTUP")
  3434. (let ((opts (org-split-string value splitre))
  3435. l var val)
  3436. (while (setq l (pop opts))
  3437. (when (setq l (assoc l org-startup-options))
  3438. (setq var (nth 1 l) val (nth 2 l))
  3439. (if (not (nth 3 l))
  3440. (set (make-local-variable var) val)
  3441. (if (not (listp (symbol-value var)))
  3442. (set (make-local-variable var) nil))
  3443. (set (make-local-variable var) (symbol-value var))
  3444. (add-to-list var val))))))
  3445. ((equal key "ARCHIVE")
  3446. (string-match " *$" value)
  3447. (setq arch (replace-match "" t t value))
  3448. (remove-text-properties 0 (length arch)
  3449. '(face t fontified t) arch))
  3450. ((equal key "LATEX_CLASS")
  3451. (setq beamer-p (equal value "beamer")))
  3452. ((equal key "SETUPFILE")
  3453. (setq setup-contents (org-file-contents
  3454. (expand-file-name
  3455. (org-remove-double-quotes value))
  3456. 'noerror))
  3457. (if (not ext-setup-or-nil)
  3458. (setq ext-setup-or-nil setup-contents start 0)
  3459. (setq ext-setup-or-nil
  3460. (concat (substring ext-setup-or-nil 0 start)
  3461. "\n" setup-contents "\n"
  3462. (substring ext-setup-or-nil start)))))
  3463. ))))
  3464. (when cat
  3465. (org-set-local 'org-category (intern cat))
  3466. (push (cons "CATEGORY" cat) props))
  3467. (when prio
  3468. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3469. (setq prio (mapcar 'string-to-char prio))
  3470. (org-set-local 'org-highest-priority (nth 0 prio))
  3471. (org-set-local 'org-lowest-priority (nth 1 prio))
  3472. (org-set-local 'org-default-priority (nth 2 prio)))
  3473. (and props (org-set-local 'org-file-properties (nreverse props)))
  3474. (and ftags (org-set-local 'org-file-tags
  3475. (mapcar 'org-add-prop-inherited ftags)))
  3476. (and drawers (org-set-local 'org-drawers drawers))
  3477. (and arch (org-set-local 'org-archive-location arch))
  3478. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3479. ;; Process the TODO keywords
  3480. (unless kwds
  3481. ;; Use the global values as if they had been given locally.
  3482. (setq kwds (default-value 'org-todo-keywords))
  3483. (if (stringp (car kwds))
  3484. (setq kwds (list (cons org-todo-interpretation
  3485. (default-value 'org-todo-keywords)))))
  3486. (setq kwds (reverse kwds)))
  3487. (setq kwds (nreverse kwds))
  3488. (let (inter kws kw)
  3489. (while (setq kws (pop kwds))
  3490. (let ((kws (or
  3491. (run-hook-with-args-until-success
  3492. 'org-todo-setup-filter-hook kws)
  3493. kws)))
  3494. (setq inter (pop kws) sep (member "|" kws)
  3495. kws0 (delete "|" (copy-sequence kws))
  3496. kwsa nil
  3497. kws1 (mapcar
  3498. (lambda (x)
  3499. ;; 1 2
  3500. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3501. (progn
  3502. (setq kw (match-string 1 x)
  3503. key (and (match-end 2) (match-string 2 x))
  3504. log (org-extract-log-state-settings x))
  3505. (push (cons kw (and key (string-to-char key))) kwsa)
  3506. (and log (push log org-todo-log-states))
  3507. kw)
  3508. (error "Invalid TODO keyword %s" x)))
  3509. kws0)
  3510. kwsa (if kwsa (append '((:startgroup))
  3511. (nreverse kwsa)
  3512. '((:endgroup))))
  3513. hw (car kws1)
  3514. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3515. tail (list inter hw (car dws) (org-last dws))))
  3516. (add-to-list 'org-todo-heads hw 'append)
  3517. (push kws1 org-todo-sets)
  3518. (setq org-done-keywords (append org-done-keywords dws nil))
  3519. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3520. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3521. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3522. (setq org-todo-sets (nreverse org-todo-sets)
  3523. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3524. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3525. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3526. ;; Process the constants
  3527. (when const
  3528. (let (e cst)
  3529. (while (setq e (pop const))
  3530. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3531. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3532. (setq org-table-formula-constants-local cst)))
  3533. ;; Process the tags.
  3534. (when tags
  3535. (let (e tgs)
  3536. (while (setq e (pop tags))
  3537. (cond
  3538. ((equal e "{") (push '(:startgroup) tgs))
  3539. ((equal e "}") (push '(:endgroup) tgs))
  3540. ((equal e "\\n") (push '(:newline) tgs))
  3541. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3542. (push (cons (match-string 1 e)
  3543. (string-to-char (match-string 2 e)))
  3544. tgs))
  3545. (t (push (list e) tgs))))
  3546. (org-set-local 'org-tag-alist nil)
  3547. (while (setq e (pop tgs))
  3548. (or (and (stringp (car e))
  3549. (assoc (car e) org-tag-alist))
  3550. (push e org-tag-alist)))))
  3551. ;; Compute the regular expressions and other local variables
  3552. (if (not org-done-keywords)
  3553. (setq org-done-keywords (and org-todo-keywords-1
  3554. (list (org-last org-todo-keywords-1)))))
  3555. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3556. (length org-scheduled-string)
  3557. (length org-clock-string)
  3558. (length org-closed-string)))
  3559. org-drawer-regexp
  3560. (concat "^[ \t]*:\\("
  3561. (mapconcat 'regexp-quote org-drawers "\\|")
  3562. "\\):[ \t]*$")
  3563. org-not-done-keywords
  3564. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3565. org-todo-regexp
  3566. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3567. "\\|") "\\)\\>")
  3568. org-not-done-regexp
  3569. (concat "\\<\\("
  3570. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3571. "\\)\\>")
  3572. org-not-done-heading-regexp
  3573. (concat "^\\(\\*+\\)[ \t]+\\("
  3574. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3575. "\\)\\>")
  3576. org-todo-line-regexp
  3577. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3578. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3579. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3580. org-complex-heading-regexp
  3581. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3582. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3583. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3584. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3585. org-complex-heading-regexp-format
  3586. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3587. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3588. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
  3589. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3590. org-nl-done-regexp
  3591. (concat "\n\\*+[ \t]+"
  3592. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3593. "\\)" "\\>")
  3594. org-todo-line-tags-regexp
  3595. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3596. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3597. (org-re
  3598. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3599. org-looking-at-done-regexp
  3600. (concat "^" "\\(?:"
  3601. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3602. "\\>")
  3603. org-deadline-regexp (concat "\\<" org-deadline-string)
  3604. org-deadline-time-regexp
  3605. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3606. org-deadline-line-regexp
  3607. (concat "\\<\\(" org-deadline-string "\\).*")
  3608. org-scheduled-regexp
  3609. (concat "\\<" org-scheduled-string)
  3610. org-scheduled-time-regexp
  3611. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3612. org-closed-time-regexp
  3613. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3614. org-keyword-time-regexp
  3615. (concat "\\<\\(" org-scheduled-string
  3616. "\\|" org-deadline-string
  3617. "\\|" org-closed-string
  3618. "\\|" org-clock-string "\\)"
  3619. " *[[<]\\([^]>]+\\)[]>]")
  3620. org-keyword-time-not-clock-regexp
  3621. (concat "\\<\\(" org-scheduled-string
  3622. "\\|" org-deadline-string
  3623. "\\|" org-closed-string
  3624. "\\)"
  3625. " *[[<]\\([^]>]+\\)[]>]")
  3626. org-maybe-keyword-time-regexp
  3627. (concat "\\(\\<\\(" org-scheduled-string
  3628. "\\|" org-deadline-string
  3629. "\\|" org-closed-string
  3630. "\\|" org-clock-string "\\)\\)?"
  3631. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3632. org-planning-or-clock-line-re
  3633. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3634. "\\|" org-deadline-string
  3635. "\\|" org-closed-string "\\|" org-clock-string
  3636. "\\)\\>\\)")
  3637. org-all-time-keywords
  3638. (mapcar (lambda (w) (substring w 0 -1))
  3639. (list org-scheduled-string org-deadline-string
  3640. org-clock-string org-closed-string))
  3641. )
  3642. (org-compute-latex-and-specials-regexp)
  3643. (org-set-font-lock-defaults))))
  3644. (defun org-file-contents (file &optional noerror)
  3645. "Return the contents of FILE, as a string."
  3646. (if (or (not file)
  3647. (not (file-readable-p file)))
  3648. (if noerror
  3649. (progn
  3650. (message "Cannot read file %s" file)
  3651. (ding) (sit-for 2)
  3652. "")
  3653. (error "Cannot read file %s" file))
  3654. (with-temp-buffer
  3655. (insert-file-contents file)
  3656. (buffer-string))))
  3657. (defun org-extract-log-state-settings (x)
  3658. "Extract the log state setting from a TODO keyword string.
  3659. This will extract info from a string like \"WAIT(w@/!)\"."
  3660. (let (kw key log1 log2)
  3661. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3662. (setq kw (match-string 1 x)
  3663. key (and (match-end 2) (match-string 2 x))
  3664. log1 (and (match-end 3) (match-string 3 x))
  3665. log2 (and (match-end 4) (match-string 4 x)))
  3666. (and (or log1 log2)
  3667. (list kw
  3668. (and log1 (if (equal log1 "!") 'time 'note))
  3669. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3670. (defun org-remove-keyword-keys (list)
  3671. "Remove a pair of parenthesis at the end of each string in LIST."
  3672. (mapcar (lambda (x)
  3673. (if (string-match "(.*)$" x)
  3674. (substring x 0 (match-beginning 0))
  3675. x))
  3676. list))
  3677. (defun org-assign-fast-keys (alist)
  3678. "Assign fast keys to a keyword-key alist.
  3679. Respect keys that are already there."
  3680. (let (new e (alt ?0))
  3681. (while (setq e (pop alist))
  3682. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  3683. (cdr e)) ;; Key already assigned.
  3684. (push e new)
  3685. (let ((clist (string-to-list (downcase (car e))))
  3686. (used (append new alist)))
  3687. (when (= (car clist) ?@)
  3688. (pop clist))
  3689. (while (and clist (rassoc (car clist) used))
  3690. (pop clist))
  3691. (unless clist
  3692. (while (rassoc alt used)
  3693. (incf alt)))
  3694. (push (cons (car e) (or (car clist) alt)) new))))
  3695. (nreverse new)))
  3696. ;;; Some variables used in various places
  3697. (defvar org-window-configuration nil
  3698. "Used in various places to store a window configuration.")
  3699. (defvar org-selected-window nil
  3700. "Used in various places to store a window configuration.")
  3701. (defvar org-finish-function nil
  3702. "Function to be called when `C-c C-c' is used.
  3703. This is for getting out of special buffers like remember.")
  3704. ;; FIXME: Occasionally check by commenting these, to make sure
  3705. ;; no other functions uses these, forgetting to let-bind them.
  3706. (defvar entry)
  3707. (defvar last-state)
  3708. (defvar date)
  3709. ;; Defined somewhere in this file, but used before definition.
  3710. (defvar org-html-entities)
  3711. (defvar org-struct-menu)
  3712. (defvar org-org-menu)
  3713. (defvar org-tbl-menu)
  3714. ;;;; Define the Org-mode
  3715. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3716. (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."))
  3717. ;; We use a before-change function to check if a table might need
  3718. ;; an update.
  3719. (defvar org-table-may-need-update t
  3720. "Indicates that a table might need an update.
  3721. This variable is set by `org-before-change-function'.
  3722. `org-table-align' sets it back to nil.")
  3723. (defun org-before-change-function (beg end)
  3724. "Every change indicates that a table might need an update."
  3725. (setq org-table-may-need-update t))
  3726. (defvar org-mode-map)
  3727. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3728. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  3729. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3730. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3731. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3732. (defvar org-table-buffer-is-an nil)
  3733. (defconst org-outline-regexp "\\*+ ")
  3734. ;;;###autoload
  3735. (define-derived-mode org-mode outline-mode "Org"
  3736. "Outline-based notes management and organizer, alias
  3737. \"Carsten's outline-mode for keeping track of everything.\"
  3738. Org-mode develops organizational tasks around a NOTES file which
  3739. contains information about projects as plain text. Org-mode is
  3740. implemented on top of outline-mode, which is ideal to keep the content
  3741. of large files well structured. It supports ToDo items, deadlines and
  3742. time stamps, which magically appear in the diary listing of the Emacs
  3743. calendar. Tables are easily created with a built-in table editor.
  3744. Plain text URL-like links connect to websites, emails (VM), Usenet
  3745. messages (Gnus), BBDB entries, and any files related to the project.
  3746. For printing and sharing of notes, an Org-mode file (or a part of it)
  3747. can be exported as a structured ASCII or HTML file.
  3748. The following commands are available:
  3749. \\{org-mode-map}"
  3750. ;; Get rid of Outline menus, they are not needed
  3751. ;; Need to do this here because define-derived-mode sets up
  3752. ;; the keymap so late. Still, it is a waste to call this each time
  3753. ;; we switch another buffer into org-mode.
  3754. (if (featurep 'xemacs)
  3755. (when (boundp 'outline-mode-menu-heading)
  3756. ;; Assume this is Greg's port, it used easymenu
  3757. (easy-menu-remove outline-mode-menu-heading)
  3758. (easy-menu-remove outline-mode-menu-show)
  3759. (easy-menu-remove outline-mode-menu-hide))
  3760. (define-key org-mode-map [menu-bar headings] 'undefined)
  3761. (define-key org-mode-map [menu-bar hide] 'undefined)
  3762. (define-key org-mode-map [menu-bar show] 'undefined))
  3763. (org-load-modules-maybe)
  3764. (easy-menu-add org-org-menu)
  3765. (easy-menu-add org-tbl-menu)
  3766. (org-install-agenda-files-menu)
  3767. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3768. (org-add-to-invisibility-spec '(org-cwidth))
  3769. (org-add-to-invisibility-spec '(org-hide-block . t))
  3770. (when (featurep 'xemacs)
  3771. (org-set-local 'line-move-ignore-invisible t))
  3772. (org-set-local 'outline-regexp org-outline-regexp)
  3773. (org-set-local 'outline-level 'org-outline-level)
  3774. (when (and org-ellipsis
  3775. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3776. (fboundp 'make-glyph-code))
  3777. (unless org-display-table
  3778. (setq org-display-table (make-display-table)))
  3779. (set-display-table-slot
  3780. org-display-table 4
  3781. (vconcat (mapcar
  3782. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3783. org-ellipsis)))
  3784. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3785. (setq buffer-display-table org-display-table))
  3786. (org-set-regexps-and-options)
  3787. (when (and org-tag-faces (not org-tags-special-faces-re))
  3788. ;; tag faces set outside customize.... force initialization.
  3789. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3790. ;; Calc embedded
  3791. (org-set-local 'calc-embedded-open-mode "# ")
  3792. (modify-syntax-entry ?# "<")
  3793. (modify-syntax-entry ?@ "w")
  3794. (if org-startup-truncated (setq truncate-lines t))
  3795. (org-set-local 'font-lock-unfontify-region-function
  3796. 'org-unfontify-region)
  3797. ;; Activate before-change-function
  3798. (org-set-local 'org-table-may-need-update t)
  3799. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3800. 'local)
  3801. ;; Check for running clock before killing a buffer
  3802. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3803. ;; Paragraphs and auto-filling
  3804. (org-set-autofill-regexps)
  3805. (setq indent-line-function 'org-indent-line-function)
  3806. (org-update-radio-target-regexp)
  3807. ;; Make sure dependence stuff works reliably, even for users who set it
  3808. ;; too late :-(
  3809. (if org-enforce-todo-dependencies
  3810. (add-hook 'org-blocker-hook
  3811. 'org-block-todo-from-children-or-siblings-or-parent)
  3812. (remove-hook 'org-blocker-hook
  3813. 'org-block-todo-from-children-or-siblings-or-parent))
  3814. (if org-enforce-todo-checkbox-dependencies
  3815. (add-hook 'org-blocker-hook
  3816. 'org-block-todo-from-checkboxes)
  3817. (remove-hook 'org-blocker-hook
  3818. 'org-block-todo-from-checkboxes))
  3819. ;; Comment characters
  3820. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3821. (org-set-local 'comment-padding " ")
  3822. ;; Align options lines
  3823. (org-set-local
  3824. 'align-mode-rules-list
  3825. '((org-in-buffer-settings
  3826. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3827. (modes . '(org-mode)))))
  3828. ;; Imenu
  3829. (org-set-local 'imenu-create-index-function
  3830. 'org-imenu-get-tree)
  3831. ;; Make isearch reveal context
  3832. (if (or (featurep 'xemacs)
  3833. (not (boundp 'outline-isearch-open-invisible-function)))
  3834. ;; Emacs 21 and XEmacs make use of the hook
  3835. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3836. ;; Emacs 22 deals with this through a special variable
  3837. (org-set-local 'outline-isearch-open-invisible-function
  3838. (lambda (&rest ignore) (org-show-context 'isearch))))
  3839. ;; Turn on org-beamer-mode?
  3840. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  3841. ;; If empty file that did not turn on org-mode automatically, make it to.
  3842. (if (and org-insert-mode-line-in-empty-file
  3843. (interactive-p)
  3844. (= (point-min) (point-max)))
  3845. (insert "# -*- mode: org -*-\n\n"))
  3846. (unless org-inhibit-startup
  3847. (when org-startup-align-all-tables
  3848. (let ((bmp (buffer-modified-p)))
  3849. (org-table-map-tables 'org-table-align)
  3850. (set-buffer-modified-p bmp)))
  3851. (when org-startup-indented
  3852. (require 'org-indent)
  3853. (org-indent-mode 1))
  3854. (unless org-inhibit-startup-visibility-stuff
  3855. (org-set-startup-visibility))))
  3856. (when (fboundp 'abbrev-table-put)
  3857. (abbrev-table-put org-mode-abbrev-table
  3858. :parents (list text-mode-abbrev-table)))
  3859. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3860. (defun org-current-time ()
  3861. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3862. (if (> (car org-time-stamp-rounding-minutes) 1)
  3863. (let ((r (car org-time-stamp-rounding-minutes))
  3864. (time (decode-time)))
  3865. (apply 'encode-time
  3866. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3867. (nthcdr 2 time))))
  3868. (current-time)))
  3869. ;;;; Font-Lock stuff, including the activators
  3870. (defvar org-mouse-map (make-sparse-keymap))
  3871. (org-defkey org-mouse-map
  3872. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3873. (org-defkey org-mouse-map
  3874. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3875. (when org-mouse-1-follows-link
  3876. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3877. (when org-tab-follows-link
  3878. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3879. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3880. (require 'font-lock)
  3881. (defconst org-non-link-chars "]\t\n\r<>")
  3882. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3883. "shell" "elisp"))
  3884. (defvar org-link-types-re nil
  3885. "Matches a link that has a url-like prefix like \"http:\"")
  3886. (defvar org-link-re-with-space nil
  3887. "Matches a link with spaces, optional angular brackets around it.")
  3888. (defvar org-link-re-with-space2 nil
  3889. "Matches a link with spaces, optional angular brackets around it.")
  3890. (defvar org-link-re-with-space3 nil
  3891. "Matches a link with spaces, only for internal part in bracket links.")
  3892. (defvar org-angle-link-re nil
  3893. "Matches link with angular brackets, spaces are allowed.")
  3894. (defvar org-plain-link-re nil
  3895. "Matches plain link, without spaces.")
  3896. (defvar org-bracket-link-regexp nil
  3897. "Matches a link in double brackets.")
  3898. (defvar org-bracket-link-analytic-regexp nil
  3899. "Regular expression used to analyze links.
  3900. Here is what the match groups contain after a match:
  3901. 1: http:
  3902. 2: http
  3903. 3: path
  3904. 4: [desc]
  3905. 5: desc")
  3906. (defvar org-bracket-link-analytic-regexp++ nil
  3907. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3908. (defvar org-any-link-re nil
  3909. "Regular expression matching any link.")
  3910. (defun org-make-link-regexps ()
  3911. "Update the link regular expressions.
  3912. This should be called after the variable `org-link-types' has changed."
  3913. (setq org-link-types-re
  3914. (concat
  3915. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  3916. org-link-re-with-space
  3917. (concat
  3918. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3919. "\\([^" org-non-link-chars " ]"
  3920. "[^" org-non-link-chars "]*"
  3921. "[^" org-non-link-chars " ]\\)>?")
  3922. org-link-re-with-space2
  3923. (concat
  3924. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3925. "\\([^" org-non-link-chars " ]"
  3926. "[^\t\n\r]*"
  3927. "[^" org-non-link-chars " ]\\)>?")
  3928. org-link-re-with-space3
  3929. (concat
  3930. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3931. "\\([^" org-non-link-chars " ]"
  3932. "[^\t\n\r]*\\)")
  3933. org-angle-link-re
  3934. (concat
  3935. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3936. "\\([^" org-non-link-chars " ]"
  3937. "[^" org-non-link-chars "]*"
  3938. "\\)>")
  3939. org-plain-link-re
  3940. (concat
  3941. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3942. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  3943. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3944. org-bracket-link-regexp
  3945. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3946. org-bracket-link-analytic-regexp
  3947. (concat
  3948. "\\[\\["
  3949. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  3950. "\\([^]]+\\)"
  3951. "\\]"
  3952. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3953. "\\]")
  3954. org-bracket-link-analytic-regexp++
  3955. (concat
  3956. "\\[\\["
  3957. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3958. "\\([^]]+\\)"
  3959. "\\]"
  3960. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3961. "\\]")
  3962. org-any-link-re
  3963. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3964. org-angle-link-re "\\)\\|\\("
  3965. org-plain-link-re "\\)")))
  3966. (org-make-link-regexps)
  3967. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3968. "Regular expression for fast time stamp matching.")
  3969. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3970. "Regular expression for fast time stamp matching.")
  3971. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3972. "Regular expression matching time strings for analysis.
  3973. This one does not require the space after the date, so it can be used
  3974. on a string that terminates immediately after the date.")
  3975. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3976. "Regular expression matching time strings for analysis.")
  3977. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3978. "Regular expression matching time stamps, with groups.")
  3979. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3980. "Regular expression matching time stamps (also [..]), with groups.")
  3981. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3982. "Regular expression matching a time stamp range.")
  3983. (defconst org-tr-regexp-both
  3984. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3985. "Regular expression matching a time stamp range.")
  3986. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3987. org-ts-regexp "\\)?")
  3988. "Regular expression matching a time stamp or time stamp range.")
  3989. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3990. org-ts-regexp-both "\\)?")
  3991. "Regular expression matching a time stamp or time stamp range.
  3992. The time stamps may be either active or inactive.")
  3993. (defvar org-emph-face nil)
  3994. (defun org-do-emphasis-faces (limit)
  3995. "Run through the buffer and add overlays to links."
  3996. (let (rtn a)
  3997. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3998. (if (not (= (char-after (match-beginning 3))
  3999. (char-after (match-beginning 4))))
  4000. (progn
  4001. (setq rtn t)
  4002. (setq a (assoc (match-string 3) org-emphasis-alist))
  4003. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4004. 'face
  4005. (nth 1 a))
  4006. (and (nth 4 a)
  4007. (org-remove-flyspell-overlays-in
  4008. (match-beginning 0) (match-end 0)))
  4009. (add-text-properties (match-beginning 2) (match-end 2)
  4010. '(font-lock-multiline t))
  4011. (when org-hide-emphasis-markers
  4012. (add-text-properties (match-end 4) (match-beginning 5)
  4013. '(invisible org-link))
  4014. (add-text-properties (match-beginning 3) (match-end 3)
  4015. '(invisible org-link)))))
  4016. (backward-char 1))
  4017. rtn))
  4018. (defun org-emphasize (&optional char)
  4019. "Insert or change an emphasis, i.e. a font like bold or italic.
  4020. If there is an active region, change that region to a new emphasis.
  4021. If there is no region, just insert the marker characters and position
  4022. the cursor between them.
  4023. CHAR should be either the marker character, or the first character of the
  4024. HTML tag associated with that emphasis. If CHAR is a space, the means
  4025. to remove the emphasis of the selected region.
  4026. If char is not given (for example in an interactive call) it
  4027. will be prompted for."
  4028. (interactive)
  4029. (let ((eal org-emphasis-alist) e det
  4030. (erc org-emphasis-regexp-components)
  4031. (prompt "")
  4032. (string "") beg end move tag c s)
  4033. (if (org-region-active-p)
  4034. (setq beg (region-beginning) end (region-end)
  4035. string (buffer-substring beg end))
  4036. (setq move t))
  4037. (while (setq e (pop eal))
  4038. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4039. c (aref tag 0))
  4040. (push (cons c (string-to-char (car e))) det)
  4041. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4042. (substring tag 1)))))
  4043. (setq det (nreverse det))
  4044. (unless char
  4045. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4046. (setq char (read-char-exclusive)))
  4047. (setq char (or (cdr (assoc char det)) char))
  4048. (if (equal char ?\ )
  4049. (setq s "" move nil)
  4050. (unless (assoc (char-to-string char) org-emphasis-alist)
  4051. (error "No such emphasis marker: \"%c\"" char))
  4052. (setq s (char-to-string char)))
  4053. (while (and (> (length string) 1)
  4054. (equal (substring string 0 1) (substring string -1))
  4055. (assoc (substring string 0 1) org-emphasis-alist))
  4056. (setq string (substring string 1 -1)))
  4057. (setq string (concat s string s))
  4058. (if beg (delete-region beg end))
  4059. (unless (or (bolp)
  4060. (string-match (concat "[" (nth 0 erc) "\n]")
  4061. (char-to-string (char-before (point)))))
  4062. (insert " "))
  4063. (unless (or (eobp)
  4064. (string-match (concat "[" (nth 1 erc) "\n]")
  4065. (char-to-string (char-after (point)))))
  4066. (insert " ") (backward-char 1))
  4067. (insert string)
  4068. (and move (backward-char 1))))
  4069. (defconst org-nonsticky-props
  4070. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4071. (defsubst org-rear-nonsticky-at (pos)
  4072. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4073. (defun org-activate-plain-links (limit)
  4074. "Run through the buffer and add overlays to links."
  4075. (catch 'exit
  4076. (let (f)
  4077. (if (re-search-forward org-plain-link-re limit t)
  4078. (progn
  4079. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4080. (setq f (get-text-property (match-beginning 0) 'face))
  4081. (if (or (eq f 'org-tag)
  4082. (and (listp f) (memq 'org-tag f)))
  4083. nil
  4084. (add-text-properties (match-beginning 0) (match-end 0)
  4085. (list 'mouse-face 'highlight
  4086. 'face 'org-link
  4087. 'keymap org-mouse-map))
  4088. (org-rear-nonsticky-at (match-end 0)))
  4089. t)))))
  4090. (defun org-activate-code (limit)
  4091. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4092. (progn
  4093. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4094. (remove-text-properties (match-beginning 0) (match-end 0)
  4095. '(display t invisible t intangible t))
  4096. t)))
  4097. (defun org-fontify-meta-lines-and-blocks (limit)
  4098. "Fontify #+ lines and blocks, in the correct ways."
  4099. (let ((case-fold-search t))
  4100. (if (re-search-forward
  4101. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  4102. limit t)
  4103. (let ((beg (match-beginning 0))
  4104. (beg1 (line-beginning-position 2))
  4105. (dc1 (downcase (match-string 2)))
  4106. (dc3 (downcase (match-string 3)))
  4107. end end1 quoting block-type)
  4108. (cond
  4109. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4110. ;; a single line of backend-specific content
  4111. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4112. (remove-text-properties (match-beginning 0) (match-end 0)
  4113. '(display t invisible t intangible t))
  4114. (add-text-properties (match-beginning 1) (match-end 3)
  4115. '(font-lock-fontified t face org-meta-line))
  4116. (add-text-properties (match-beginning 6) (match-end 6)
  4117. '(font-lock-fontified t face org-block))
  4118. t)
  4119. ((and (match-end 4) (equal dc3 "begin"))
  4120. ;; Truely a block
  4121. (setq block-type (downcase (match-string 5))
  4122. quoting (member block-type org-protecting-blocks))
  4123. (when (re-search-forward
  4124. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4125. nil t) ;; on purpose, we look further than LIMIT
  4126. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4127. (when quoting
  4128. (remove-text-properties beg end
  4129. '(display t invisible t intangible t)))
  4130. (add-text-properties
  4131. beg end
  4132. '(font-lock-fontified t font-lock-multiline t))
  4133. (add-text-properties beg beg1 '(face org-meta-line))
  4134. (add-text-properties end1 end '(face org-meta-line))
  4135. (cond
  4136. (quoting
  4137. (add-text-properties beg1 end1 '(face org-block)))
  4138. ((not org-fontify-quote-and-verse-blocks))
  4139. ((string= block-type "quote")
  4140. (add-text-properties beg1 end1 '(face org-quote)))
  4141. ((string= block-type "verse")
  4142. (add-text-properties beg1 end1 '(face org-verse))))
  4143. t))
  4144. ((not (member (char-after beg) '(?\ ?\t)))
  4145. ;; just any other in-buffer setting, but not indented
  4146. (add-text-properties
  4147. beg (match-end 0)
  4148. '(font-lock-fontified t face org-meta-line))
  4149. t)
  4150. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4151. "orgtbl:" "tblfm:" "tblname:"))
  4152. (and (match-end 4) (equal dc3 "attr")))
  4153. (add-text-properties
  4154. beg (match-end 0)
  4155. '(font-lock-fontified t face org-meta-line))
  4156. t)
  4157. ((member dc3 '(" " ""))
  4158. (add-text-properties
  4159. beg (match-end 0)
  4160. '(font-lock-fontified t face font-lock-comment-face)))
  4161. (t nil))))))
  4162. (defun org-activate-angle-links (limit)
  4163. "Run through the buffer and add overlays to links."
  4164. (if (re-search-forward org-angle-link-re limit t)
  4165. (progn
  4166. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4167. (add-text-properties (match-beginning 0) (match-end 0)
  4168. (list 'mouse-face 'highlight
  4169. 'keymap org-mouse-map))
  4170. (org-rear-nonsticky-at (match-end 0))
  4171. t)))
  4172. (defun org-activate-footnote-links (limit)
  4173. "Run through the buffer and add overlays to links."
  4174. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4175. limit t)
  4176. (progn
  4177. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4178. (add-text-properties (match-beginning 2) (match-end 2)
  4179. (list 'mouse-face 'highlight
  4180. 'keymap org-mouse-map
  4181. 'help-echo
  4182. (if (= (point-at-bol) (match-beginning 2))
  4183. "Footnote definition"
  4184. "Footnote reference")
  4185. ))
  4186. (org-rear-nonsticky-at (match-end 2))
  4187. t)))
  4188. (defun org-activate-bracket-links (limit)
  4189. "Run through the buffer and add overlays to bracketed links."
  4190. (if (re-search-forward org-bracket-link-regexp limit t)
  4191. (let* ((help (concat "LINK: "
  4192. (org-match-string-no-properties 1)))
  4193. ;; FIXME: above we should remove the escapes.
  4194. ;; but that requires another match, protecting match data,
  4195. ;; a lot of overhead for font-lock.
  4196. (ip (org-maybe-intangible
  4197. (list 'invisible 'org-link
  4198. 'keymap org-mouse-map 'mouse-face 'highlight
  4199. 'font-lock-multiline t 'help-echo help)))
  4200. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4201. 'font-lock-multiline t 'help-echo help)))
  4202. ;; We need to remove the invisible property here. Table narrowing
  4203. ;; may have made some of this invisible.
  4204. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4205. (remove-text-properties (match-beginning 0) (match-end 0)
  4206. '(invisible nil))
  4207. (if (match-end 3)
  4208. (progn
  4209. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4210. (org-rear-nonsticky-at (match-beginning 3))
  4211. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4212. (org-rear-nonsticky-at (match-end 3))
  4213. (add-text-properties (match-end 3) (match-end 0) ip)
  4214. (org-rear-nonsticky-at (match-end 0)))
  4215. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4216. (org-rear-nonsticky-at (match-beginning 1))
  4217. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4218. (org-rear-nonsticky-at (match-end 1))
  4219. (add-text-properties (match-end 1) (match-end 0) ip)
  4220. (org-rear-nonsticky-at (match-end 0)))
  4221. t)))
  4222. (defun org-activate-dates (limit)
  4223. "Run through the buffer and add overlays to dates."
  4224. (if (re-search-forward org-tsr-regexp-both limit t)
  4225. (progn
  4226. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4227. (add-text-properties (match-beginning 0) (match-end 0)
  4228. (list 'mouse-face 'highlight
  4229. 'keymap org-mouse-map))
  4230. (org-rear-nonsticky-at (match-end 0))
  4231. (when org-display-custom-times
  4232. (if (match-end 3)
  4233. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4234. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4235. t)))
  4236. (defvar org-target-link-regexp nil
  4237. "Regular expression matching radio targets in plain text.")
  4238. (make-variable-buffer-local 'org-target-link-regexp)
  4239. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4240. "Regular expression matching a link target.")
  4241. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4242. "Regular expression matching a radio target.")
  4243. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4244. "Regular expression matching any target.")
  4245. (defun org-activate-target-links (limit)
  4246. "Run through the buffer and add overlays to target matches."
  4247. (when org-target-link-regexp
  4248. (let ((case-fold-search t))
  4249. (if (re-search-forward org-target-link-regexp limit t)
  4250. (progn
  4251. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4252. (add-text-properties (match-beginning 0) (match-end 0)
  4253. (list 'mouse-face 'highlight
  4254. 'keymap org-mouse-map
  4255. 'help-echo "Radio target link"
  4256. 'org-linked-text t))
  4257. (org-rear-nonsticky-at (match-end 0))
  4258. t)))))
  4259. (defun org-update-radio-target-regexp ()
  4260. "Find all radio targets in this file and update the regular expression."
  4261. (interactive)
  4262. (when (memq 'radio org-activate-links)
  4263. (setq org-target-link-regexp
  4264. (org-make-target-link-regexp (org-all-targets 'radio)))
  4265. (org-restart-font-lock)))
  4266. (defun org-hide-wide-columns (limit)
  4267. (let (s e)
  4268. (setq s (text-property-any (point) (or limit (point-max))
  4269. 'org-cwidth t))
  4270. (when s
  4271. (setq e (next-single-property-change s 'org-cwidth))
  4272. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4273. (goto-char e)
  4274. t)))
  4275. (defvar org-latex-and-specials-regexp nil
  4276. "Regular expression for highlighting export special stuff.")
  4277. (defvar org-match-substring-regexp)
  4278. (defvar org-match-substring-with-braces-regexp)
  4279. ;; This should be with the exporter code, but we also use if for font-locking
  4280. (defconst org-export-html-special-string-regexps
  4281. '(("\\\\-" . "&shy;")
  4282. ("---\\([^-]\\)" . "&mdash;\\1")
  4283. ("--\\([^-]\\)" . "&ndash;\\1")
  4284. ("\\.\\.\\." . "&hellip;"))
  4285. "Regular expressions for special string conversion.")
  4286. (defun org-compute-latex-and-specials-regexp ()
  4287. "Compute regular expression for stuff treated specially by exporters."
  4288. (if (not org-highlight-latex-fragments-and-specials)
  4289. (org-set-local 'org-latex-and-specials-regexp nil)
  4290. (require 'org-exp)
  4291. (let*
  4292. ((matchers (plist-get org-format-latex-options :matchers))
  4293. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4294. org-latex-regexps)))
  4295. (org-export-allow-BIND nil)
  4296. (options (org-combine-plists (org-default-export-plist)
  4297. (org-infile-export-plist)))
  4298. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4299. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4300. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4301. (org-export-html-expand (plist-get options :expand-quoted-html))
  4302. (org-export-with-special-strings (plist-get options :special-strings))
  4303. (re-sub
  4304. (cond
  4305. ((equal org-export-with-sub-superscripts '{})
  4306. (list org-match-substring-with-braces-regexp))
  4307. (org-export-with-sub-superscripts
  4308. (list org-match-substring-regexp))
  4309. (t nil)))
  4310. (re-latex
  4311. (if org-export-with-LaTeX-fragments
  4312. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4313. (re-macros
  4314. (if org-export-with-TeX-macros
  4315. (list (concat "\\\\"
  4316. (regexp-opt
  4317. (append (mapcar 'car org-html-entities)
  4318. (if (boundp 'org-latex-entities)
  4319. (mapcar (lambda (x)
  4320. (or (car-safe x) x))
  4321. org-latex-entities)
  4322. nil))
  4323. 'words))) ; FIXME
  4324. ))
  4325. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4326. (re-special (if org-export-with-special-strings
  4327. (mapcar (lambda (x) (car x))
  4328. org-export-html-special-string-regexps)))
  4329. (re-rest
  4330. (delq nil
  4331. (list
  4332. (if org-export-html-expand "@<[^>\n]+>")
  4333. ))))
  4334. (org-set-local
  4335. 'org-latex-and-specials-regexp
  4336. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4337. re-rest) "\\|")))))
  4338. (defun org-do-latex-and-special-faces (limit)
  4339. "Run through the buffer and add overlays to links."
  4340. (when org-latex-and-specials-regexp
  4341. (let (rtn d)
  4342. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4343. limit t))
  4344. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4345. 'face))
  4346. '(org-code org-verbatim underline)))
  4347. (progn
  4348. (setq rtn t
  4349. d (cond ((member (char-after (1+ (match-beginning 0)))
  4350. '(?_ ?^)) 1)
  4351. (t 0)))
  4352. (font-lock-prepend-text-property
  4353. (+ d (match-beginning 0)) (match-end 0)
  4354. 'face 'org-latex-and-export-specials)
  4355. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4356. '(font-lock-multiline t)))))
  4357. rtn)))
  4358. (defun org-restart-font-lock ()
  4359. "Restart font-lock-mode, to force refontification."
  4360. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4361. (font-lock-mode -1)
  4362. (font-lock-mode 1)))
  4363. (defun org-all-targets (&optional radio)
  4364. "Return a list of all targets in this file.
  4365. With optional argument RADIO, only find radio targets."
  4366. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4367. rtn)
  4368. (save-excursion
  4369. (goto-char (point-min))
  4370. (while (re-search-forward re nil t)
  4371. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4372. rtn)))
  4373. (defun org-make-target-link-regexp (targets)
  4374. "Make regular expression matching all strings in TARGETS.
  4375. The regular expression finds the targets also if there is a line break
  4376. between words."
  4377. (and targets
  4378. (concat
  4379. "\\<\\("
  4380. (mapconcat
  4381. (lambda (x)
  4382. (while (string-match " +" x)
  4383. (setq x (replace-match "\\s-+" t t x)))
  4384. x)
  4385. targets
  4386. "\\|")
  4387. "\\)\\>")))
  4388. (defun org-activate-tags (limit)
  4389. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4390. (progn
  4391. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4392. (add-text-properties (match-beginning 1) (match-end 1)
  4393. (list 'mouse-face 'highlight
  4394. 'keymap org-mouse-map))
  4395. (org-rear-nonsticky-at (match-end 1))
  4396. t)))
  4397. (defun org-outline-level ()
  4398. "Compute the outline level of the heading at point.
  4399. This function assumes that the cursor is at the beginning of a line matched
  4400. by outline-regexp. Otherwise it returns garbage.
  4401. If this is called at a normal headline, the level is the number of stars.
  4402. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4403. For plain list items, if they are matched by `outline-regexp', this returns
  4404. 1000 plus the line indentation."
  4405. (save-excursion
  4406. (looking-at outline-regexp)
  4407. (if (match-beginning 1)
  4408. (+ (org-get-string-indentation (match-string 1)) 1000)
  4409. (1- (- (match-end 0) (match-beginning 0))))))
  4410. (defvar org-font-lock-keywords nil)
  4411. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4412. "Regular expression matching a property line.")
  4413. (defvar org-font-lock-hook nil
  4414. "Functions to be called for special font lock stuff.")
  4415. (defun org-font-lock-hook (limit)
  4416. (run-hook-with-args 'org-font-lock-hook limit))
  4417. (defun org-set-font-lock-defaults ()
  4418. (let* ((em org-fontify-emphasized-text)
  4419. (lk org-activate-links)
  4420. (org-font-lock-extra-keywords
  4421. (list
  4422. ;; Call the hook
  4423. '(org-font-lock-hook)
  4424. ;; Headlines
  4425. `(,(if org-fontify-whole-heading-line
  4426. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4427. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4428. (1 (org-get-level-face 1))
  4429. (2 (org-get-level-face 2))
  4430. (3 (org-get-level-face 3)))
  4431. ;; Table lines
  4432. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4433. (1 'org-table t))
  4434. ;; Table internals
  4435. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4436. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4437. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4438. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4439. ;; Drawers
  4440. (list org-drawer-regexp '(0 'org-special-keyword t))
  4441. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4442. ;; Properties
  4443. (list org-property-re
  4444. '(1 'org-special-keyword t)
  4445. '(3 'org-property-value t))
  4446. ;; Links
  4447. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4448. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4449. (if (memq 'plain lk) '(org-activate-plain-links))
  4450. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4451. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4452. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4453. (if (memq 'footnote lk) '(org-activate-footnote-links
  4454. (2 'org-footnote t)))
  4455. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4456. '(org-hide-wide-columns (0 nil append))
  4457. ;; TODO lines
  4458. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4459. '(1 (org-get-todo-face 1) t))
  4460. ;; DONE
  4461. (if org-fontify-done-headline
  4462. (list (concat "^[*]+ +\\<\\("
  4463. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4464. "\\)\\(.*\\)")
  4465. '(2 'org-headline-done t))
  4466. nil)
  4467. ;; Priorities
  4468. '(org-font-lock-add-priority-faces)
  4469. ;; Tags
  4470. '(org-font-lock-add-tag-faces)
  4471. ;; Special keywords
  4472. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4473. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4474. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4475. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4476. ;; Emphasis
  4477. (if em
  4478. (if (featurep 'xemacs)
  4479. '(org-do-emphasis-faces (0 nil append))
  4480. '(org-do-emphasis-faces)))
  4481. ;; Checkboxes
  4482. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4483. 2 'org-checkbox prepend)
  4484. (if org-provide-checkbox-statistics
  4485. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4486. (0 (org-get-checkbox-statistics-face) t)))
  4487. ;; Description list items
  4488. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4489. 2 'bold prepend)
  4490. ;; ARCHIVEd headings
  4491. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4492. '(1 'org-archived prepend))
  4493. ;; Specials
  4494. '(org-do-latex-and-special-faces)
  4495. ;; Code
  4496. '(org-activate-code (1 'org-code t))
  4497. ;; COMMENT
  4498. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4499. "\\|" org-quote-string "\\)\\>")
  4500. '(1 'org-special-keyword t))
  4501. '("^#.*" (0 'font-lock-comment-face t))
  4502. ;; Blocks and meta lines
  4503. '(org-fontify-meta-lines-and-blocks)
  4504. )))
  4505. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4506. ;; Now set the full font-lock-keywords
  4507. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4508. (org-set-local 'font-lock-defaults
  4509. '(org-font-lock-keywords t nil nil backward-paragraph))
  4510. (kill-local-variable 'font-lock-keywords) nil))
  4511. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4512. "Fontify string S like in Org-mode"
  4513. (with-temp-buffer
  4514. (insert s)
  4515. (let ((org-odd-levels-only odd-levels))
  4516. (org-mode)
  4517. (font-lock-fontify-buffer)
  4518. (buffer-string))))
  4519. (defvar org-m nil)
  4520. (defvar org-l nil)
  4521. (defvar org-f nil)
  4522. (defun org-get-level-face (n)
  4523. "Get the right face for match N in font-lock matching of headlines."
  4524. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4525. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4526. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4527. (cond
  4528. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4529. ((eq n 2) org-f)
  4530. (t (if org-level-color-stars-only nil org-f))))
  4531. (defun org-get-todo-face (kwd)
  4532. "Get the right face for a TODO keyword KWD.
  4533. If KWD is a number, get the corresponding match group."
  4534. (if (numberp kwd) (setq kwd (match-string kwd)))
  4535. (or (org-face-from-face-or-color
  4536. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  4537. (and (member kwd org-done-keywords) 'org-done)
  4538. 'org-todo))
  4539. (defun org-face-from-face-or-color (context inherit face-or-color)
  4540. "Create a face list that inherits INHERIT, but sets the foreground color.
  4541. When FACE-OR-COLOR is not a string, just return it."
  4542. (if (stringp face-or-color)
  4543. (list :inherit inherit
  4544. (cdr (assoc context org-faces-easy-properties))
  4545. face-or-color)
  4546. face-or-color))
  4547. (defun org-font-lock-add-tag-faces (limit)
  4548. "Add the special tag faces."
  4549. (when (and org-tag-faces org-tags-special-faces-re)
  4550. (while (re-search-forward org-tags-special-faces-re limit t)
  4551. (add-text-properties (match-beginning 1) (match-end 1)
  4552. (list 'face (org-get-tag-face 1)
  4553. 'font-lock-fontified t))
  4554. (backward-char 1))))
  4555. (defun org-font-lock-add-priority-faces (limit)
  4556. "Add the special priority faces."
  4557. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4558. (add-text-properties
  4559. (match-beginning 0) (match-end 0)
  4560. (list 'face (or (org-face-from-face-or-color
  4561. 'priority 'org-special-keyword
  4562. (cdr (assoc (char-after (match-beginning 1))
  4563. org-priority-faces)))
  4564. 'org-special-keyword)
  4565. 'font-lock-fontified t))))
  4566. (defun org-get-tag-face (kwd)
  4567. "Get the right face for a TODO keyword KWD.
  4568. If KWD is a number, get the corresponding match group."
  4569. (if (numberp kwd) (setq kwd (match-string kwd)))
  4570. (or (org-face-from-face-or-color
  4571. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  4572. 'org-tag))
  4573. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4574. "Remove fontification and activation overlays from links."
  4575. (font-lock-default-unfontify-region beg end)
  4576. (let* ((buffer-undo-list t)
  4577. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4578. (inhibit-modification-hooks t)
  4579. deactivate-mark buffer-file-name buffer-file-truename)
  4580. (remove-text-properties
  4581. beg end
  4582. (if org-indent-mode
  4583. ;; also remove line-prefix and wrap-prefix properties
  4584. '(mouse-face t keymap t org-linked-text t
  4585. invisible t intangible t
  4586. line-prefix t wrap-prefix t
  4587. org-no-flyspell t)
  4588. '(mouse-face t keymap t org-linked-text t
  4589. invisible t intangible t
  4590. org-no-flyspell t)))))
  4591. ;;;; Visibility cycling, including org-goto and indirect buffer
  4592. ;;; Cycling
  4593. (defvar org-cycle-global-status nil)
  4594. (make-variable-buffer-local 'org-cycle-global-status)
  4595. (defvar org-cycle-subtree-status nil)
  4596. (make-variable-buffer-local 'org-cycle-subtree-status)
  4597. ;;;###autoload
  4598. (defvar org-inlinetask-min-level)
  4599. (defun org-cycle (&optional arg)
  4600. "TAB-action and visibility cycling for Org-mode.
  4601. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4602. is outline visibility cycling, but it also invokes other actions
  4603. in special contexts.
  4604. - When this function is called with a prefix argument, rotate the entire
  4605. buffer through 3 states (global cycling)
  4606. 1. OVERVIEW: Show only top-level headlines.
  4607. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4608. 3. SHOW ALL: Show everything.
  4609. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4610. determined by the variable `org-startup-folded', and by any VISIBILITY
  4611. properties in the buffer.
  4612. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4613. including any drawers.
  4614. - When inside a table, re-align the table and move to the next field.
  4615. - When point is at the beginning of a headline, rotate the subtree started
  4616. by this line through 3 different states (local cycling)
  4617. 1. FOLDED: Only the main headline is shown.
  4618. 2. CHILDREN: The main headline and the direct children are shown.
  4619. From this state, you can move to one of the children
  4620. and zoom in further.
  4621. 3. SUBTREE: Show the entire subtree, including body text.
  4622. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4623. - When point is at the beginning of an empty headline and the variable
  4624. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  4625. of the headline by demoting and promoting it to likely levels. This
  4626. speeds up creation document structure by presing TAB once or several
  4627. times right after creating a new headline.
  4628. - When there is a numeric prefix, go up to a heading with level ARG, do
  4629. a `show-subtree' and return to the previous cursor position. If ARG
  4630. is negative, go up that many levels.
  4631. - When point is not at the beginning of a headline, execute the global
  4632. binding for TAB, which is re-indenting the line. See the option
  4633. `org-cycle-emulate-tab' for details.
  4634. - Special case: if point is at the beginning of the buffer and there is
  4635. no headline in line 1, this function will act as if called with prefix arg.
  4636. But only if also the variable `org-cycle-global-at-bob' is t."
  4637. (interactive "P")
  4638. (org-load-modules-maybe)
  4639. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  4640. (and org-cycle-level-after-item/entry-creation
  4641. (or (org-cycle-level)
  4642. (org-cycle-item-indentation))))
  4643. (let* ((limit-level
  4644. (or org-cycle-max-level
  4645. (and (boundp 'org-inlinetask-min-level)
  4646. org-inlinetask-min-level
  4647. (1- org-inlinetask-min-level))))
  4648. (nstars (and limit-level
  4649. (if org-odd-levels-only
  4650. (and limit-level (1- (* limit-level 2)))
  4651. limit-level)))
  4652. (outline-regexp
  4653. (cond
  4654. ((not (org-mode-p)) outline-regexp)
  4655. ((or (eq org-cycle-include-plain-lists 'integrate)
  4656. (and org-cycle-include-plain-lists (org-at-item-p)))
  4657. (concat "\\(?:\\*"
  4658. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4659. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4660. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4661. (bob-special (and org-cycle-global-at-bob (bobp)
  4662. (not (looking-at outline-regexp))))
  4663. (org-cycle-hook
  4664. (if bob-special
  4665. (delq 'org-optimize-window-after-visibility-change
  4666. (copy-sequence org-cycle-hook))
  4667. org-cycle-hook))
  4668. (pos (point)))
  4669. (if (or bob-special (equal arg '(4)))
  4670. ;; special case: use global cycling
  4671. (setq arg t))
  4672. (cond
  4673. ((equal arg '(16))
  4674. (org-set-startup-visibility)
  4675. (message "Startup visibility, plus VISIBILITY properties"))
  4676. ((equal arg '(64))
  4677. (show-all)
  4678. (message "Entire buffer visible, including drawers"))
  4679. ((org-at-table-p 'any)
  4680. ;; Enter the table or move to the next field in the table
  4681. (if (org-at-table.el-p)
  4682. (message "Use C-c ' to edit table.el tables")
  4683. (if arg (org-table-edit-field t)
  4684. (org-table-justify-field-maybe)
  4685. (call-interactively 'org-table-next-field))))
  4686. ((run-hook-with-args-until-success
  4687. 'org-tab-after-check-for-table-hook))
  4688. ((eq arg t) ;; Global cycling
  4689. (org-cycle-internal-global))
  4690. ((and org-drawers org-drawer-regexp
  4691. (save-excursion
  4692. (beginning-of-line 1)
  4693. (looking-at org-drawer-regexp)))
  4694. ;; Toggle block visibility
  4695. (org-flag-drawer
  4696. (not (get-char-property (match-end 0) 'invisible))))
  4697. ((integerp arg)
  4698. ;; Show-subtree, ARG levels up from here.
  4699. (save-excursion
  4700. (org-back-to-heading)
  4701. (outline-up-heading (if (< arg 0) (- arg)
  4702. (- (funcall outline-level) arg)))
  4703. (org-show-subtree)))
  4704. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4705. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4706. (org-cycle-internal-local))
  4707. ;; TAB emulation and template completion
  4708. (buffer-read-only (org-back-to-heading))
  4709. ((run-hook-with-args-until-success
  4710. 'org-tab-after-check-for-cycling-hook))
  4711. ((org-try-structure-completion))
  4712. ((org-try-cdlatex-tab))
  4713. ((run-hook-with-args-until-success
  4714. 'org-tab-before-tab-emulation-hook))
  4715. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4716. (or (not (bolp))
  4717. (not (looking-at outline-regexp))))
  4718. (call-interactively (global-key-binding "\t")))
  4719. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4720. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4721. (or (and (eq org-cycle-emulate-tab 'white)
  4722. (= (match-end 0) (point-at-eol)))
  4723. (and (eq org-cycle-emulate-tab 'whitestart)
  4724. (>= (match-end 0) pos))))
  4725. t
  4726. (eq org-cycle-emulate-tab t))
  4727. (call-interactively (global-key-binding "\t")))
  4728. (t (save-excursion
  4729. (org-back-to-heading)
  4730. (org-cycle)))))))
  4731. (defun org-cycle-internal-global ()
  4732. "Do the global cycling action."
  4733. (cond
  4734. ((and (eq last-command this-command)
  4735. (eq org-cycle-global-status 'overview))
  4736. ;; We just created the overview - now do table of contents
  4737. ;; This can be slow in very large buffers, so indicate action
  4738. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4739. (message "CONTENTS...")
  4740. (org-content)
  4741. (message "CONTENTS...done")
  4742. (setq org-cycle-global-status 'contents)
  4743. (run-hook-with-args 'org-cycle-hook 'contents))
  4744. ((and (eq last-command this-command)
  4745. (eq org-cycle-global-status 'contents))
  4746. ;; We just showed the table of contents - now show everything
  4747. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4748. (show-all)
  4749. (message "SHOW ALL")
  4750. (setq org-cycle-global-status 'all)
  4751. (run-hook-with-args 'org-cycle-hook 'all))
  4752. (t
  4753. ;; Default action: go to overview
  4754. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4755. (org-overview)
  4756. (message "OVERVIEW")
  4757. (setq org-cycle-global-status 'overview)
  4758. (run-hook-with-args 'org-cycle-hook 'overview))))
  4759. (defun org-cycle-internal-local ()
  4760. "Do the local cycling action."
  4761. (org-back-to-heading)
  4762. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4763. ;; First, some boundaries
  4764. (save-excursion
  4765. (org-back-to-heading)
  4766. (setq level (funcall outline-level))
  4767. (save-excursion
  4768. (beginning-of-line 2)
  4769. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4770. ; XEmacs does not have `next-single-char-property-change'
  4771. ; I'm not sure about Emacs 21.
  4772. (while (and (not (eobp)) ;; this is like `next-line'
  4773. (get-char-property (1- (point)) 'invisible))
  4774. (beginning-of-line 2))
  4775. (while (and (not (eobp)) ;; this is like `next-line'
  4776. (get-char-property (1- (point)) 'invisible))
  4777. (goto-char (next-single-char-property-change (point) 'invisible))
  4778. ;;;??? (or (bolp) (beginning-of-line 2))))
  4779. (and (eolp) (beginning-of-line 2))))
  4780. (setq eol (point)))
  4781. (outline-end-of-heading) (setq eoh (point))
  4782. (save-excursion
  4783. (outline-next-heading)
  4784. (setq has-children (and (org-at-heading-p t)
  4785. (> (funcall outline-level) level))))
  4786. (org-end-of-subtree t)
  4787. (unless (eobp)
  4788. (skip-chars-forward " \t\n")
  4789. (beginning-of-line 1) ; in case this is an item
  4790. )
  4791. (setq eos (if (eobp) (point) (1- (point)))))
  4792. ;; Find out what to do next and set `this-command'
  4793. (cond
  4794. ((= eos eoh)
  4795. ;; Nothing is hidden behind this heading
  4796. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4797. (message "EMPTY ENTRY")
  4798. (setq org-cycle-subtree-status nil)
  4799. (save-excursion
  4800. (goto-char eos)
  4801. (outline-next-heading)
  4802. (if (org-invisible-p) (org-flag-heading nil))))
  4803. ((and (or (>= eol eos)
  4804. (not (string-match "\\S-" (buffer-substring eol eos))))
  4805. (or has-children
  4806. (not (setq children-skipped
  4807. org-cycle-skip-children-state-if-no-children))))
  4808. ;; Entire subtree is hidden in one line: children view
  4809. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4810. (org-show-entry)
  4811. (show-children)
  4812. (message "CHILDREN")
  4813. (save-excursion
  4814. (goto-char eos)
  4815. (outline-next-heading)
  4816. (if (org-invisible-p) (org-flag-heading nil)))
  4817. (setq org-cycle-subtree-status 'children)
  4818. (run-hook-with-args 'org-cycle-hook 'children))
  4819. ((or children-skipped
  4820. (and (eq last-command this-command)
  4821. (eq org-cycle-subtree-status 'children)))
  4822. ;; We just showed the children, or no children are there,
  4823. ;; now show everything.
  4824. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4825. (org-show-subtree)
  4826. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4827. (setq org-cycle-subtree-status 'subtree)
  4828. (run-hook-with-args 'org-cycle-hook 'subtree))
  4829. (t
  4830. ;; Default action: hide the subtree.
  4831. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4832. (hide-subtree)
  4833. (message "FOLDED")
  4834. (setq org-cycle-subtree-status 'folded)
  4835. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4836. ;;;###autoload
  4837. (defun org-global-cycle (&optional arg)
  4838. "Cycle the global visibility. For details see `org-cycle'.
  4839. With C-u prefix arg, switch to startup visibility.
  4840. With a numeric prefix, show all headlines up to that level."
  4841. (interactive "P")
  4842. (let ((org-cycle-include-plain-lists
  4843. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4844. (cond
  4845. ((integerp arg)
  4846. (show-all)
  4847. (hide-sublevels arg)
  4848. (setq org-cycle-global-status 'contents))
  4849. ((equal arg '(4))
  4850. (org-set-startup-visibility)
  4851. (message "Startup visibility, plus VISIBILITY properties."))
  4852. (t
  4853. (org-cycle '(4))))))
  4854. (defun org-set-startup-visibility ()
  4855. "Set the visibility required by startup options and properties."
  4856. (cond
  4857. ((eq org-startup-folded t)
  4858. (org-cycle '(4)))
  4859. ((eq org-startup-folded 'content)
  4860. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4861. (org-cycle '(4)) (org-cycle '(4)))))
  4862. (unless (eq org-startup-folded 'showeverything)
  4863. (if org-hide-block-startup (org-hide-block-all))
  4864. (org-set-visibility-according-to-property 'no-cleanup)
  4865. (org-cycle-hide-archived-subtrees 'all)
  4866. (org-cycle-hide-drawers 'all)
  4867. (org-cycle-show-empty-lines 'all)))
  4868. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4869. "Switch subtree visibilities according to :VISIBILITY: property."
  4870. (interactive)
  4871. (let (org-show-entry-below state)
  4872. (save-excursion
  4873. (goto-char (point-min))
  4874. (while (re-search-forward
  4875. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4876. nil t)
  4877. (setq state (match-string 1))
  4878. (save-excursion
  4879. (org-back-to-heading t)
  4880. (hide-subtree)
  4881. (org-reveal)
  4882. (cond
  4883. ((equal state '("fold" "folded"))
  4884. (hide-subtree))
  4885. ((equal state "children")
  4886. (org-show-hidden-entry)
  4887. (show-children))
  4888. ((equal state "content")
  4889. (save-excursion
  4890. (save-restriction
  4891. (org-narrow-to-subtree)
  4892. (org-content))))
  4893. ((member state '("all" "showall"))
  4894. (show-subtree)))))
  4895. (unless no-cleanup
  4896. (org-cycle-hide-archived-subtrees 'all)
  4897. (org-cycle-hide-drawers 'all)
  4898. (org-cycle-show-empty-lines 'all)))))
  4899. (defun org-overview ()
  4900. "Switch to overview mode, showing only top-level headlines.
  4901. Really, this shows all headlines with level equal or greater than the level
  4902. of the first headline in the buffer. This is important, because if the
  4903. first headline is not level one, then (hide-sublevels 1) gives confusing
  4904. results."
  4905. (interactive)
  4906. (let ((level (save-excursion
  4907. (goto-char (point-min))
  4908. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4909. (progn
  4910. (goto-char (match-beginning 0))
  4911. (funcall outline-level))))))
  4912. (and level (hide-sublevels level))))
  4913. (defun org-content (&optional arg)
  4914. "Show all headlines in the buffer, like a table of contents.
  4915. With numerical argument N, show content up to level N."
  4916. (interactive "P")
  4917. (save-excursion
  4918. ;; Visit all headings and show their offspring
  4919. (and (integerp arg) (org-overview))
  4920. (goto-char (point-max))
  4921. (catch 'exit
  4922. (while (and (progn (condition-case nil
  4923. (outline-previous-visible-heading 1)
  4924. (error (goto-char (point-min))))
  4925. t)
  4926. (looking-at outline-regexp))
  4927. (if (integerp arg)
  4928. (show-children (1- arg))
  4929. (show-branches))
  4930. (if (bobp) (throw 'exit nil))))))
  4931. (defun org-optimize-window-after-visibility-change (state)
  4932. "Adjust the window after a change in outline visibility.
  4933. This function is the default value of the hook `org-cycle-hook'."
  4934. (when (get-buffer-window (current-buffer))
  4935. (cond
  4936. ((eq state 'content) nil)
  4937. ((eq state 'all) nil)
  4938. ((eq state 'folded) nil)
  4939. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4940. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4941. (defun org-remove-empty-overlays-at (pos)
  4942. "Remove outline overlays that do not contain non-white stuff."
  4943. (mapc
  4944. (lambda (o)
  4945. (and (eq 'outline (org-overlay-get o 'invisible))
  4946. (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
  4947. (org-overlay-end o))))
  4948. (org-delete-overlay o)))
  4949. (org-overlays-at pos)))
  4950. (defun org-clean-visibility-after-subtree-move ()
  4951. "Fix visibility issues after moving a subtree."
  4952. ;; First, find a reasonable region to look at:
  4953. ;; Start two siblings above, end three below
  4954. (let* ((beg (save-excursion
  4955. (and (org-get-last-sibling)
  4956. (org-get-last-sibling))
  4957. (point)))
  4958. (end (save-excursion
  4959. (and (org-get-next-sibling)
  4960. (org-get-next-sibling)
  4961. (org-get-next-sibling))
  4962. (if (org-at-heading-p)
  4963. (point-at-eol)
  4964. (point))))
  4965. (level (looking-at "\\*+"))
  4966. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  4967. (save-excursion
  4968. (save-restriction
  4969. (narrow-to-region beg end)
  4970. (when re
  4971. ;; Properly fold already folded siblings
  4972. (goto-char (point-min))
  4973. (while (re-search-forward re nil t)
  4974. (if (and (not (org-invisible-p))
  4975. (save-excursion
  4976. (goto-char (point-at-eol)) (org-invisible-p)))
  4977. (hide-entry))))
  4978. (org-cycle-show-empty-lines 'overview)
  4979. (org-cycle-hide-drawers 'overview)))))
  4980. (defun org-cycle-show-empty-lines (state)
  4981. "Show empty lines above all visible headlines.
  4982. The region to be covered depends on STATE when called through
  4983. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4984. entire buffer covered. Note that an empty line is only shown if there
  4985. are at least `org-cycle-separator-lines' empty lines before the headline."
  4986. (when (not (= org-cycle-separator-lines 0))
  4987. (save-excursion
  4988. (let* ((n (abs org-cycle-separator-lines))
  4989. (re (cond
  4990. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4991. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4992. (t (let ((ns (number-to-string (- n 2))))
  4993. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4994. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4995. beg end b e)
  4996. (cond
  4997. ((memq state '(overview contents t))
  4998. (setq beg (point-min) end (point-max)))
  4999. ((memq state '(children folded))
  5000. (setq beg (point) end (progn (org-end-of-subtree t t)
  5001. (beginning-of-line 2)
  5002. (point)))))
  5003. (when beg
  5004. (goto-char beg)
  5005. (while (re-search-forward re end t)
  5006. (unless (get-char-property (match-end 1) 'invisible)
  5007. (setq e (match-end 1))
  5008. (if (< org-cycle-separator-lines 0)
  5009. (setq b (save-excursion
  5010. (goto-char (match-beginning 0))
  5011. (org-back-over-empty-lines)
  5012. (if (save-excursion
  5013. (goto-char (max (point-min) (1- (point))))
  5014. (org-on-heading-p))
  5015. (1- (point))
  5016. (point))))
  5017. (setq b (match-beginning 1)))
  5018. (outline-flag-region b e nil)))))))
  5019. ;; Never hide empty lines at the end of the file.
  5020. (save-excursion
  5021. (goto-char (point-max))
  5022. (outline-previous-heading)
  5023. (outline-end-of-heading)
  5024. (if (and (looking-at "[ \t\n]+")
  5025. (= (match-end 0) (point-max)))
  5026. (outline-flag-region (point) (match-end 0) nil))))
  5027. (defun org-show-empty-lines-in-parent ()
  5028. "Move to the parent and re-show empty lines before visible headlines."
  5029. (save-excursion
  5030. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5031. (org-cycle-show-empty-lines context))))
  5032. (defun org-files-list ()
  5033. "Return `org-agenda-files' list, plus all open org-mode files.
  5034. This is useful for operations that need to scan all of a user's
  5035. open and agenda-wise Org files."
  5036. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5037. (dolist (buf (buffer-list))
  5038. (with-current-buffer buf
  5039. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5040. (let ((file (expand-file-name (buffer-file-name))))
  5041. (unless (member file files)
  5042. (push file files))))))
  5043. files))
  5044. (defsubst org-entry-beginning-position ()
  5045. "Return the beginning position of the current entry."
  5046. (save-excursion (outline-back-to-heading t) (point)))
  5047. (defsubst org-entry-end-position ()
  5048. "Return the end position of the current entry."
  5049. (save-excursion (outline-next-heading) (point)))
  5050. (defun org-cycle-hide-drawers (state)
  5051. "Re-hide all drawers after a visibility state change."
  5052. (when (and (org-mode-p)
  5053. (not (memq state '(overview folded contents))))
  5054. (save-excursion
  5055. (let* ((globalp (memq state '(contents all)))
  5056. (beg (if globalp (point-min) (point)))
  5057. (end (if globalp (point-max)
  5058. (if (eq state 'children)
  5059. (save-excursion (outline-next-heading) (point))
  5060. (org-end-of-subtree t)))))
  5061. (goto-char beg)
  5062. (while (re-search-forward org-drawer-regexp end t)
  5063. (org-flag-drawer t))))))
  5064. (defun org-flag-drawer (flag)
  5065. (save-excursion
  5066. (beginning-of-line 1)
  5067. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5068. (let ((b (match-end 0))
  5069. (outline-regexp org-outline-regexp))
  5070. (if (re-search-forward
  5071. "^[ \t]*:END:"
  5072. (save-excursion (outline-next-heading) (point)) t)
  5073. (outline-flag-region b (point-at-eol) flag)
  5074. (error ":END: line missing at position %s" b))))))
  5075. (defun org-subtree-end-visible-p ()
  5076. "Is the end of the current subtree visible?"
  5077. (pos-visible-in-window-p
  5078. (save-excursion (org-end-of-subtree t) (point))))
  5079. (defun org-first-headline-recenter (&optional N)
  5080. "Move cursor to the first headline and recenter the headline.
  5081. Optional argument N means put the headline into the Nth line of the window."
  5082. (goto-char (point-min))
  5083. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5084. (beginning-of-line)
  5085. (recenter (prefix-numeric-value N))))
  5086. ;;; Saving and restoring visibility
  5087. (defun org-outline-overlay-data (&optional use-markers)
  5088. "Return a list of the locations of all outline overlays.
  5089. The are overlays with the `invisible' property value `outline'.
  5090. The return valus is a list of cons cells, with start and stop
  5091. positions for each overlay.
  5092. If USE-MARKERS is set, return the positions as markers."
  5093. (let (beg end)
  5094. (save-excursion
  5095. (save-restriction
  5096. (widen)
  5097. (delq nil
  5098. (mapcar (lambda (o)
  5099. (when (eq (org-overlay-get o 'invisible) 'outline)
  5100. (setq beg (org-overlay-start o)
  5101. end (org-overlay-end o))
  5102. (and beg end (> end beg)
  5103. (if use-markers
  5104. (cons (move-marker (make-marker) beg)
  5105. (move-marker (make-marker) end))
  5106. (cons beg end)))))
  5107. (org-overlays-in (point-min) (point-max))))))))
  5108. (defun org-set-outline-overlay-data (data)
  5109. "Create visibility overlays for all positions in DATA.
  5110. DATA should have been made by `org-outline-overlay-data'."
  5111. (let (o)
  5112. (save-excursion
  5113. (save-restriction
  5114. (widen)
  5115. (show-all)
  5116. (mapc (lambda (c)
  5117. (setq o (org-make-overlay (car c) (cdr c)))
  5118. (org-overlay-put o 'invisible 'outline))
  5119. data)))))
  5120. (defmacro org-save-outline-visibility (use-markers &rest body)
  5121. "Save and restore outline visibility around BODY.
  5122. If USE-MARKERS is non-nil, use markers for the positions.
  5123. This means that the buffer may change while running BODY,
  5124. but it also means that the buffer should stay alive
  5125. during the operation, because otherwise all these markers will
  5126. point nowhere."
  5127. `(let ((data (org-outline-overlay-data ,use-markers)))
  5128. (unwind-protect
  5129. (progn
  5130. ,@body
  5131. (org-set-outline-overlay-data data))
  5132. (when ,use-markers
  5133. (mapc (lambda (c)
  5134. (and (markerp (car c)) (move-marker (car c) nil))
  5135. (and (markerp (cdr c)) (move-marker (cdr c) nil)))
  5136. data)))))
  5137. ;;; Folding of blocks
  5138. (defconst org-block-regexp
  5139. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  5140. "Regular expression for hiding blocks.")
  5141. (defvar org-hide-block-overlays nil
  5142. "Overlays hiding blocks.")
  5143. (make-variable-buffer-local 'org-hide-block-overlays)
  5144. (defun org-block-map (function &optional start end)
  5145. "Call func at the head of all source blocks in the current
  5146. buffer. Optional arguments START and END can be used to limit
  5147. the range."
  5148. (let ((start (or start (point-min)))
  5149. (end (or end (point-max))))
  5150. (save-excursion
  5151. (goto-char start)
  5152. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5153. (save-excursion
  5154. (save-match-data
  5155. (goto-char (match-beginning 0))
  5156. (funcall function)))))))
  5157. (defun org-hide-block-toggle-all ()
  5158. "Toggle the visibility of all blocks in the current buffer."
  5159. (org-block-map #'org-hide-block-toggle))
  5160. (defun org-hide-block-all ()
  5161. "Fold all blocks in the current buffer."
  5162. (interactive)
  5163. (org-show-block-all)
  5164. (org-block-map #'org-hide-block-toggle-maybe))
  5165. (defun org-show-block-all ()
  5166. "Unfold all blocks in the current buffer."
  5167. (mapc 'org-delete-overlay org-hide-block-overlays)
  5168. (setq org-hide-block-overlays nil))
  5169. (defun org-hide-block-toggle-maybe ()
  5170. "Toggle visibility of block at point."
  5171. (interactive)
  5172. (let ((case-fold-search t))
  5173. (if (save-excursion
  5174. (beginning-of-line 1)
  5175. (looking-at org-block-regexp))
  5176. (progn (org-hide-block-toggle)
  5177. t) ;; to signal that we took action
  5178. nil))) ;; to signal that we did not
  5179. (defun org-hide-block-toggle (&optional force)
  5180. "Toggle the visibility of the current block."
  5181. (interactive)
  5182. (save-excursion
  5183. (beginning-of-line)
  5184. (if (re-search-forward org-block-regexp nil t)
  5185. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5186. (end (match-end 0)) ;; end of entire body
  5187. ov)
  5188. (if (memq t (mapcar (lambda (overlay)
  5189. (eq (org-overlay-get overlay 'invisible)
  5190. 'org-hide-block))
  5191. (org-overlays-at start)))
  5192. (if (or (not force) (eq force 'off))
  5193. (mapc (lambda (ov)
  5194. (when (member ov org-hide-block-overlays)
  5195. (setq org-hide-block-overlays
  5196. (delq ov org-hide-block-overlays)))
  5197. (when (eq (org-overlay-get ov 'invisible)
  5198. 'org-hide-block)
  5199. (org-delete-overlay ov)))
  5200. (org-overlays-at start)))
  5201. (setq ov (org-make-overlay start end))
  5202. (org-overlay-put ov 'invisible 'org-hide-block)
  5203. ;; make the block accessible to isearch
  5204. (org-overlay-put
  5205. ov 'isearch-open-invisible
  5206. (lambda (ov)
  5207. (when (member ov org-hide-block-overlays)
  5208. (setq org-hide-block-overlays
  5209. (delq ov org-hide-block-overlays)))
  5210. (when (eq (org-overlay-get ov 'invisible)
  5211. 'org-hide-block)
  5212. (org-delete-overlay ov))))
  5213. (push ov org-hide-block-overlays)))
  5214. (error "Not looking at a source block"))))
  5215. ;; org-tab-after-check-for-cycling-hook
  5216. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5217. ;; Remove overlays when changing major mode
  5218. (add-hook 'org-mode-hook
  5219. (lambda () (org-add-hook 'change-major-mode-hook
  5220. 'org-show-block-all 'append 'local)))
  5221. ;;; Org-goto
  5222. (defvar org-goto-window-configuration nil)
  5223. (defvar org-goto-marker nil)
  5224. (defvar org-goto-map
  5225. (let ((map (make-sparse-keymap)))
  5226. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5227. (while (setq cmd (pop cmds))
  5228. (substitute-key-definition cmd cmd map global-map)))
  5229. (suppress-keymap map)
  5230. (org-defkey map "\C-m" 'org-goto-ret)
  5231. (org-defkey map [(return)] 'org-goto-ret)
  5232. (org-defkey map [(left)] 'org-goto-left)
  5233. (org-defkey map [(right)] 'org-goto-right)
  5234. (org-defkey map [(control ?g)] 'org-goto-quit)
  5235. (org-defkey map "\C-i" 'org-cycle)
  5236. (org-defkey map [(tab)] 'org-cycle)
  5237. (org-defkey map [(down)] 'outline-next-visible-heading)
  5238. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5239. (if org-goto-auto-isearch
  5240. (if (fboundp 'define-key-after)
  5241. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5242. nil)
  5243. (org-defkey map "q" 'org-goto-quit)
  5244. (org-defkey map "n" 'outline-next-visible-heading)
  5245. (org-defkey map "p" 'outline-previous-visible-heading)
  5246. (org-defkey map "f" 'outline-forward-same-level)
  5247. (org-defkey map "b" 'outline-backward-same-level)
  5248. (org-defkey map "u" 'outline-up-heading))
  5249. (org-defkey map "/" 'org-occur)
  5250. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5251. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5252. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5253. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5254. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5255. map))
  5256. (defconst org-goto-help
  5257. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5258. RET=jump to location [Q]uit and return to previous location
  5259. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5260. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5261. ;; FIXME: Docstring does not mention both interfaces
  5262. (defun org-goto (&optional alternative-interface)
  5263. "Look up a different location in the current file, keeping current visibility.
  5264. When you want look-up or go to a different location in a document, the
  5265. fastest way is often to fold the entire buffer and then dive into the tree.
  5266. This method has the disadvantage, that the previous location will be folded,
  5267. which may not be what you want.
  5268. This command works around this by showing a copy of the current buffer
  5269. in an indirect buffer, in overview mode. You can dive into the tree in
  5270. that copy, use org-occur and incremental search to find a location.
  5271. When pressing RET or `Q', the command returns to the original buffer in
  5272. which the visibility is still unchanged. After RET is will also jump to
  5273. the location selected in the indirect buffer and expose the
  5274. the headline hierarchy above."
  5275. (interactive "P")
  5276. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5277. (org-refile-use-outline-path t)
  5278. (org-refile-target-verify-function nil)
  5279. (interface
  5280. (if (not alternative-interface)
  5281. org-goto-interface
  5282. (if (eq org-goto-interface 'outline)
  5283. 'outline-path-completion
  5284. 'outline)))
  5285. (org-goto-start-pos (point))
  5286. (selected-point
  5287. (if (eq interface 'outline)
  5288. (car (org-get-location (current-buffer) org-goto-help))
  5289. (nth 3 (org-refile-get-location "Goto: ")))))
  5290. (if selected-point
  5291. (progn
  5292. (org-mark-ring-push org-goto-start-pos)
  5293. (goto-char selected-point)
  5294. (if (or (org-invisible-p) (org-invisible-p2))
  5295. (org-show-context 'org-goto)))
  5296. (message "Quit"))))
  5297. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5298. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5299. (defvar org-goto-local-auto-isearch-map) ; defined below
  5300. (defun org-get-location (buf help)
  5301. "Let the user select a location in the Org-mode buffer BUF.
  5302. This function uses a recursive edit. It returns the selected position
  5303. or nil."
  5304. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5305. (isearch-hide-immediately nil)
  5306. (isearch-search-fun-function
  5307. (lambda () 'org-goto-local-search-headings))
  5308. (org-goto-selected-point org-goto-exit-command)
  5309. (pop-up-frames nil)
  5310. (special-display-buffer-names nil)
  5311. (special-display-regexps nil)
  5312. (special-display-function nil))
  5313. (save-excursion
  5314. (save-window-excursion
  5315. (delete-other-windows)
  5316. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5317. (switch-to-buffer
  5318. (condition-case nil
  5319. (make-indirect-buffer (current-buffer) "*org-goto*")
  5320. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5321. (with-output-to-temp-buffer "*Help*"
  5322. (princ help))
  5323. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5324. (setq buffer-read-only nil)
  5325. (let ((org-startup-truncated t)
  5326. (org-startup-folded nil)
  5327. (org-startup-align-all-tables nil))
  5328. (org-mode)
  5329. (org-overview))
  5330. (setq buffer-read-only t)
  5331. (if (and (boundp 'org-goto-start-pos)
  5332. (integer-or-marker-p org-goto-start-pos))
  5333. (let ((org-show-hierarchy-above t)
  5334. (org-show-siblings t)
  5335. (org-show-following-heading t))
  5336. (goto-char org-goto-start-pos)
  5337. (and (org-invisible-p) (org-show-context)))
  5338. (goto-char (point-min)))
  5339. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5340. (message "Select location and press RET")
  5341. (use-local-map org-goto-map)
  5342. (recursive-edit)
  5343. ))
  5344. (kill-buffer "*org-goto*")
  5345. (cons org-goto-selected-point org-goto-exit-command)))
  5346. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5347. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5348. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5349. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5350. (defun org-goto-local-search-headings (string bound noerror)
  5351. "Search and make sure that any matches are in headlines."
  5352. (catch 'return
  5353. (while (if isearch-forward
  5354. (search-forward string bound noerror)
  5355. (search-backward string bound noerror))
  5356. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5357. (and (member :headline context)
  5358. (not (member :tags context))))
  5359. (throw 'return (point))))))
  5360. (defun org-goto-local-auto-isearch ()
  5361. "Start isearch."
  5362. (interactive)
  5363. (goto-char (point-min))
  5364. (let ((keys (this-command-keys)))
  5365. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5366. (isearch-mode t)
  5367. (isearch-process-search-char (string-to-char keys)))))
  5368. (defun org-goto-ret (&optional arg)
  5369. "Finish `org-goto' by going to the new location."
  5370. (interactive "P")
  5371. (setq org-goto-selected-point (point)
  5372. org-goto-exit-command 'return)
  5373. (throw 'exit nil))
  5374. (defun org-goto-left ()
  5375. "Finish `org-goto' by going to the new location."
  5376. (interactive)
  5377. (if (org-on-heading-p)
  5378. (progn
  5379. (beginning-of-line 1)
  5380. (setq org-goto-selected-point (point)
  5381. org-goto-exit-command 'left)
  5382. (throw 'exit nil))
  5383. (error "Not on a heading")))
  5384. (defun org-goto-right ()
  5385. "Finish `org-goto' by going to the new location."
  5386. (interactive)
  5387. (if (org-on-heading-p)
  5388. (progn
  5389. (setq org-goto-selected-point (point)
  5390. org-goto-exit-command 'right)
  5391. (throw 'exit nil))
  5392. (error "Not on a heading")))
  5393. (defun org-goto-quit ()
  5394. "Finish `org-goto' without cursor motion."
  5395. (interactive)
  5396. (setq org-goto-selected-point nil)
  5397. (setq org-goto-exit-command 'quit)
  5398. (throw 'exit nil))
  5399. ;;; Indirect buffer display of subtrees
  5400. (defvar org-indirect-dedicated-frame nil
  5401. "This is the frame being used for indirect tree display.")
  5402. (defvar org-last-indirect-buffer nil)
  5403. (defun org-tree-to-indirect-buffer (&optional arg)
  5404. "Create indirect buffer and narrow it to current subtree.
  5405. With numerical prefix ARG, go up to this level and then take that tree.
  5406. If ARG is negative, go up that many levels.
  5407. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5408. indirect buffer previously made with this command, to avoid proliferation of
  5409. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5410. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5411. is kept so that you can work with several indirect buffers at the same time.
  5412. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5413. requests that a new frame be made for the new buffer, so that the dedicated
  5414. frame is not changed."
  5415. (interactive "P")
  5416. (let ((cbuf (current-buffer))
  5417. (cwin (selected-window))
  5418. (pos (point))
  5419. beg end level heading ibuf)
  5420. (save-excursion
  5421. (org-back-to-heading t)
  5422. (when (numberp arg)
  5423. (setq level (org-outline-level))
  5424. (if (< arg 0) (setq arg (+ level arg)))
  5425. (while (> (setq level (org-outline-level)) arg)
  5426. (outline-up-heading 1 t)))
  5427. (setq beg (point)
  5428. heading (org-get-heading))
  5429. (org-end-of-subtree t t)
  5430. (if (org-on-heading-p) (backward-char 1))
  5431. (setq end (point)))
  5432. (if (and (buffer-live-p org-last-indirect-buffer)
  5433. (not (eq org-indirect-buffer-display 'new-frame))
  5434. (not arg))
  5435. (kill-buffer org-last-indirect-buffer))
  5436. (setq ibuf (org-get-indirect-buffer cbuf)
  5437. org-last-indirect-buffer ibuf)
  5438. (cond
  5439. ((or (eq org-indirect-buffer-display 'new-frame)
  5440. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5441. (select-frame (make-frame))
  5442. (delete-other-windows)
  5443. (switch-to-buffer ibuf)
  5444. (org-set-frame-title heading))
  5445. ((eq org-indirect-buffer-display 'dedicated-frame)
  5446. (raise-frame
  5447. (select-frame (or (and org-indirect-dedicated-frame
  5448. (frame-live-p org-indirect-dedicated-frame)
  5449. org-indirect-dedicated-frame)
  5450. (setq org-indirect-dedicated-frame (make-frame)))))
  5451. (delete-other-windows)
  5452. (switch-to-buffer ibuf)
  5453. (org-set-frame-title (concat "Indirect: " heading)))
  5454. ((eq org-indirect-buffer-display 'current-window)
  5455. (switch-to-buffer ibuf))
  5456. ((eq org-indirect-buffer-display 'other-window)
  5457. (pop-to-buffer ibuf))
  5458. (t (error "Invalid value")))
  5459. (if (featurep 'xemacs)
  5460. (save-excursion (org-mode) (turn-on-font-lock)))
  5461. (narrow-to-region beg end)
  5462. (show-all)
  5463. (goto-char pos)
  5464. (and (window-live-p cwin) (select-window cwin))))
  5465. (defun org-get-indirect-buffer (&optional buffer)
  5466. (setq buffer (or buffer (current-buffer)))
  5467. (let ((n 1) (base (buffer-name buffer)) bname)
  5468. (while (buffer-live-p
  5469. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5470. (setq n (1+ n)))
  5471. (condition-case nil
  5472. (make-indirect-buffer buffer bname 'clone)
  5473. (error (make-indirect-buffer buffer bname)))))
  5474. (defun org-set-frame-title (title)
  5475. "Set the title of the current frame to the string TITLE."
  5476. ;; FIXME: how to name a single frame in XEmacs???
  5477. (unless (featurep 'xemacs)
  5478. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5479. ;;;; Structure editing
  5480. ;;; Inserting headlines
  5481. (defun org-previous-line-empty-p ()
  5482. (save-excursion
  5483. (and (not (bobp))
  5484. (or (beginning-of-line 0) t)
  5485. (save-match-data
  5486. (looking-at "[ \t]*$")))))
  5487. (defun org-insert-heading (&optional force-heading invisible-ok)
  5488. "Insert a new heading or item with same depth at point.
  5489. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5490. If point is at the beginning of a headline, insert a sibling before the
  5491. current headline. If point is not at the beginning, do not split the line,
  5492. but create the new headline after the current line.
  5493. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  5494. This is important for non-interactive uses of the command."
  5495. (interactive "P")
  5496. (if (or (= (buffer-size) 0)
  5497. (and (not (save-excursion (and (ignore-errors (org-back-to-heading invisible-ok))
  5498. (org-on-heading-p))))
  5499. (not (org-in-item-p))))
  5500. (insert "\n* ")
  5501. (when (or force-heading (not (org-insert-item)))
  5502. (let* ((empty-line-p nil)
  5503. (head (save-excursion
  5504. (condition-case nil
  5505. (progn
  5506. (org-back-to-heading invisible-ok)
  5507. (setq empty-line-p (org-previous-line-empty-p))
  5508. (match-string 0))
  5509. (error "*"))))
  5510. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5511. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5512. pos hide-previous previous-pos)
  5513. (cond
  5514. ((and (org-on-heading-p) (bolp)
  5515. (or (bobp)
  5516. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5517. ;; insert before the current line
  5518. (open-line (if blank 2 1)))
  5519. ((and (bolp)
  5520. (not org-insert-heading-respect-content)
  5521. (or (bobp)
  5522. (save-excursion
  5523. (backward-char 1) (not (org-invisible-p)))))
  5524. ;; insert right here
  5525. nil)
  5526. (t
  5527. ;; somewhere in the line
  5528. (save-excursion
  5529. (setq previous-pos (point-at-bol))
  5530. (end-of-line)
  5531. (setq hide-previous (org-invisible-p)))
  5532. (and org-insert-heading-respect-content (org-show-subtree))
  5533. (let ((split
  5534. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5535. (save-excursion
  5536. (let ((p (point)))
  5537. (goto-char (point-at-bol))
  5538. (and (looking-at org-complex-heading-regexp)
  5539. (> p (match-beginning 4)))))))
  5540. tags pos)
  5541. (cond
  5542. (org-insert-heading-respect-content
  5543. (org-end-of-subtree nil t)
  5544. (or (bolp) (newline))
  5545. (or (org-previous-line-empty-p)
  5546. (and blank (newline)))
  5547. (open-line 1))
  5548. ((org-on-heading-p)
  5549. (when hide-previous
  5550. (show-children)
  5551. (org-show-entry))
  5552. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5553. (setq tags (and (match-end 2) (match-string 2)))
  5554. (and (match-end 1)
  5555. (delete-region (match-beginning 1) (match-end 1)))
  5556. (setq pos (point-at-bol))
  5557. (or split (end-of-line 1))
  5558. (delete-horizontal-space)
  5559. (if (string-match "\\`\\*+\\'"
  5560. (buffer-substring (point-at-bol) (point)))
  5561. (insert " "))
  5562. (newline (if blank 2 1))
  5563. (when tags
  5564. (save-excursion
  5565. (goto-char pos)
  5566. (end-of-line 1)
  5567. (insert " " tags)
  5568. (org-set-tags nil 'align))))
  5569. (t
  5570. (or split (end-of-line 1))
  5571. (newline (if blank 2 1)))))))
  5572. (insert head) (just-one-space)
  5573. (setq pos (point))
  5574. (end-of-line 1)
  5575. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5576. (when (and org-insert-heading-respect-content hide-previous)
  5577. (save-excursion
  5578. (goto-char previous-pos)
  5579. (hide-subtree)))
  5580. (run-hooks 'org-insert-heading-hook)))))
  5581. (defun org-get-heading (&optional no-tags)
  5582. "Return the heading of the current entry, without the stars."
  5583. (save-excursion
  5584. (org-back-to-heading t)
  5585. (if (looking-at
  5586. (if no-tags
  5587. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5588. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5589. (match-string 1) "")))
  5590. (defun org-heading-components ()
  5591. "Return the components of the current heading.
  5592. This is a list with the following elements:
  5593. - the level as an integer
  5594. - the reduced level, different if `org-odd-levels-only' is set.
  5595. - the TODO keyword, or nil
  5596. - the priority character, like ?A, or nil if no priority is given
  5597. - the headline text itself, or the tags string if no headline text
  5598. - the tags string, or nil."
  5599. (save-excursion
  5600. (org-back-to-heading t)
  5601. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  5602. (list (length (match-string 1))
  5603. (org-reduced-level (length (match-string 1)))
  5604. (org-match-string-no-properties 2)
  5605. (and (match-end 3) (aref (match-string 3) 2))
  5606. (org-match-string-no-properties 4)
  5607. (org-match-string-no-properties 5)))))
  5608. (defun org-get-entry ()
  5609. "Get the entry text, after heading, entire subtree."
  5610. (save-excursion
  5611. (org-back-to-heading t)
  5612. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5613. (defun org-insert-heading-after-current ()
  5614. "Insert a new heading with same level as current, after current subtree."
  5615. (interactive)
  5616. (org-back-to-heading)
  5617. (org-insert-heading)
  5618. (org-move-subtree-down)
  5619. (end-of-line 1))
  5620. (defun org-insert-heading-respect-content ()
  5621. (interactive)
  5622. (let ((org-insert-heading-respect-content t))
  5623. (org-insert-heading t)))
  5624. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5625. (interactive "P")
  5626. (let ((org-insert-heading-respect-content t))
  5627. (org-insert-todo-heading force-state t)))
  5628. (defun org-insert-todo-heading (arg &optional force-heading)
  5629. "Insert a new heading with the same level and TODO state as current heading.
  5630. If the heading has no TODO state, or if the state is DONE, use the first
  5631. state (TODO by default). Also with prefix arg, force first state."
  5632. (interactive "P")
  5633. (when (or force-heading (not (org-insert-item 'checkbox)))
  5634. (org-insert-heading force-heading)
  5635. (save-excursion
  5636. (org-back-to-heading)
  5637. (outline-previous-heading)
  5638. (looking-at org-todo-line-regexp))
  5639. (let*
  5640. ((new-mark-x
  5641. (if (or arg
  5642. (not (match-beginning 2))
  5643. (member (match-string 2) org-done-keywords))
  5644. (car org-todo-keywords-1)
  5645. (match-string 2)))
  5646. (new-mark
  5647. (or
  5648. (run-hook-with-args-until-success
  5649. 'org-todo-get-default-hook new-mark-x nil)
  5650. new-mark-x)))
  5651. (beginning-of-line 1)
  5652. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5653. (if org-treat-insert-todo-heading-as-state-change
  5654. (org-todo new-mark)
  5655. (insert new-mark " "))))
  5656. (when org-provide-todo-statistics
  5657. (org-update-parent-todo-statistics))))
  5658. (defun org-insert-subheading (arg)
  5659. "Insert a new subheading and demote it.
  5660. Works for outline headings and for plain lists alike."
  5661. (interactive "P")
  5662. (org-insert-heading arg)
  5663. (cond
  5664. ((org-on-heading-p) (org-do-demote))
  5665. ((org-at-item-p) (org-indent-item 1))))
  5666. (defun org-insert-todo-subheading (arg)
  5667. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5668. Works for outline headings and for plain lists alike."
  5669. (interactive "P")
  5670. (org-insert-todo-heading arg)
  5671. (cond
  5672. ((org-on-heading-p) (org-do-demote))
  5673. ((org-at-item-p) (org-indent-item 1))))
  5674. ;;; Promotion and Demotion
  5675. (defvar org-after-demote-entry-hook nil
  5676. "Hook run after an entry has been demoted.
  5677. The cursor will be at the beginning of the entry.
  5678. When a subtree is being demoted, the hook will be called for each node.")
  5679. (defvar org-after-promote-entry-hook nil
  5680. "Hook run after an entry has been promoted.
  5681. The cursor will be at the beginning of the entry.
  5682. When a subtree is being promoted, the hook will be called for each node.")
  5683. (defun org-promote-subtree ()
  5684. "Promote the entire subtree.
  5685. See also `org-promote'."
  5686. (interactive)
  5687. (save-excursion
  5688. (org-map-tree 'org-promote))
  5689. (org-fix-position-after-promote))
  5690. (defun org-demote-subtree ()
  5691. "Demote the entire subtree. See `org-demote'.
  5692. See also `org-promote'."
  5693. (interactive)
  5694. (save-excursion
  5695. (org-map-tree 'org-demote))
  5696. (org-fix-position-after-promote))
  5697. (defun org-do-promote ()
  5698. "Promote the current heading higher up the tree.
  5699. If the region is active in `transient-mark-mode', promote all headings
  5700. in the region."
  5701. (interactive)
  5702. (save-excursion
  5703. (if (org-region-active-p)
  5704. (org-map-region 'org-promote (region-beginning) (region-end))
  5705. (org-promote)))
  5706. (org-fix-position-after-promote))
  5707. (defun org-do-demote ()
  5708. "Demote the current heading lower down the tree.
  5709. If the region is active in `transient-mark-mode', demote all headings
  5710. in the region."
  5711. (interactive)
  5712. (save-excursion
  5713. (if (org-region-active-p)
  5714. (org-map-region 'org-demote (region-beginning) (region-end))
  5715. (org-demote)))
  5716. (org-fix-position-after-promote))
  5717. (defun org-fix-position-after-promote ()
  5718. "Make sure that after pro/demotion cursor position is right."
  5719. (let ((pos (point)))
  5720. (when (save-excursion
  5721. (beginning-of-line 1)
  5722. (looking-at org-todo-line-regexp)
  5723. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5724. (cond ((eobp) (insert " "))
  5725. ((eolp) (insert " "))
  5726. ((equal (char-after) ?\ ) (forward-char 1))))))
  5727. (defun org-current-level ()
  5728. "Return the level of the current entry, or nil if before the first headline.
  5729. The level is the number of stars at the beginning of the headline."
  5730. (save-excursion
  5731. (condition-case nil
  5732. (progn
  5733. (org-back-to-heading t)
  5734. (funcall outline-level))
  5735. (error nil))))
  5736. (defun org-get-previous-line-level ()
  5737. "Return the outline depth of the last headline before the current line.
  5738. Returns 0 for the first headline in the buffer, and nil if before the
  5739. first headline."
  5740. (let ((current-level (org-current-level))
  5741. (prev-level (when (> (line-number-at-pos) 1)
  5742. (save-excursion
  5743. (beginning-of-line 0)
  5744. (org-current-level)))))
  5745. (cond ((null current-level) nil) ; Before first headline
  5746. ((null prev-level) 0) ; At first headline
  5747. (prev-level))))
  5748. (defun org-reduced-level (l)
  5749. "Compute the effective level of a heading.
  5750. This takes into account the setting of `org-odd-levels-only'."
  5751. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5752. (defun org-level-increment ()
  5753. "Return the number of stars that will be added or removed at a
  5754. time to headlines when structure editing, based on the value of
  5755. `org-odd-levels-only'."
  5756. (if org-odd-levels-only 2 1))
  5757. (defun org-get-valid-level (level &optional change)
  5758. "Rectify a level change under the influence of `org-odd-levels-only'
  5759. LEVEL is a current level, CHANGE is by how much the level should be
  5760. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5761. even level numbers will become the next higher odd number."
  5762. (if org-odd-levels-only
  5763. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5764. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5765. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5766. (max 1 (+ level (or change 0)))))
  5767. (if (boundp 'define-obsolete-function-alias)
  5768. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5769. (define-obsolete-function-alias 'org-get-legal-level
  5770. 'org-get-valid-level)
  5771. (define-obsolete-function-alias 'org-get-legal-level
  5772. 'org-get-valid-level "23.1")))
  5773. (defun org-promote ()
  5774. "Promote the current heading higher up the tree.
  5775. If the region is active in `transient-mark-mode', promote all headings
  5776. in the region."
  5777. (org-back-to-heading t)
  5778. (let* ((level (save-match-data (funcall outline-level)))
  5779. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5780. (diff (abs (- level (length up-head) -1))))
  5781. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5782. (replace-match up-head nil t)
  5783. ;; Fixup tag positioning
  5784. (and org-auto-align-tags (org-set-tags nil t))
  5785. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5786. (run-hooks 'org-after-promote-entry-hook)))
  5787. (defun org-demote ()
  5788. "Demote the current heading lower down the tree.
  5789. If the region is active in `transient-mark-mode', demote all headings
  5790. in the region."
  5791. (org-back-to-heading t)
  5792. (let* ((level (save-match-data (funcall outline-level)))
  5793. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5794. (diff (abs (- level (length down-head) -1))))
  5795. (replace-match down-head nil t)
  5796. ;; Fixup tag positioning
  5797. (and org-auto-align-tags (org-set-tags nil t))
  5798. (if org-adapt-indentation (org-fixup-indentation diff))
  5799. (run-hooks 'org-after-demote-entry-hook)))
  5800. (defun org-cycle-level ()
  5801. "Cycle the level of an empty headline through possible states.
  5802. This goes first to child, then to parent, level, then up the hierarchy.
  5803. After top level, it switches back to sibling level."
  5804. (interactive)
  5805. (let ((org-adapt-indentation nil))
  5806. (when (org-point-at-end-of-empty-headline)
  5807. (setq this-command 'org-cycle-level) ; Only needed for caching
  5808. (let ((cur-level (org-current-level))
  5809. (prev-level (org-get-previous-line-level)))
  5810. (cond
  5811. ;; If first headline in file, promote to top-level.
  5812. ((= prev-level 0)
  5813. (loop repeat (/ (- cur-level 1) (org-level-increment))
  5814. do (org-do-promote)))
  5815. ;; If same level as prev, demote one.
  5816. ((= prev-level cur-level)
  5817. (org-do-demote))
  5818. ;; If parent is top-level, promote to top level if not already.
  5819. ((= prev-level 1)
  5820. (loop repeat (/ (- cur-level 1) (org-level-increment))
  5821. do (org-do-promote)))
  5822. ;; If top-level, return to prev-level.
  5823. ((= cur-level 1)
  5824. (loop repeat (/ (- prev-level 1) (org-level-increment))
  5825. do (org-do-demote)))
  5826. ;; If less than prev-level, promote one.
  5827. ((< cur-level prev-level)
  5828. (org-do-promote))
  5829. ;; If deeper than prev-level, promote until higher than
  5830. ;; prev-level.
  5831. ((> cur-level prev-level)
  5832. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  5833. do (org-do-promote))))
  5834. t))))
  5835. (defun org-map-tree (fun)
  5836. "Call FUN for every heading underneath the current one."
  5837. (org-back-to-heading)
  5838. (let ((level (funcall outline-level)))
  5839. (save-excursion
  5840. (funcall fun)
  5841. (while (and (progn
  5842. (outline-next-heading)
  5843. (> (funcall outline-level) level))
  5844. (not (eobp)))
  5845. (funcall fun)))))
  5846. (defun org-map-region (fun beg end)
  5847. "Call FUN for every heading between BEG and END."
  5848. (let ((org-ignore-region t))
  5849. (save-excursion
  5850. (setq end (copy-marker end))
  5851. (goto-char beg)
  5852. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5853. (< (point) end))
  5854. (funcall fun))
  5855. (while (and (progn
  5856. (outline-next-heading)
  5857. (< (point) end))
  5858. (not (eobp)))
  5859. (funcall fun)))))
  5860. (defun org-fixup-indentation (diff)
  5861. "Change the indentation in the current entry by DIFF
  5862. However, if any line in the current entry has no indentation, or if it
  5863. would end up with no indentation after the change, nothing at all is done."
  5864. (save-excursion
  5865. (let ((end (save-excursion (outline-next-heading)
  5866. (point-marker)))
  5867. (prohibit (if (> diff 0)
  5868. "^\\S-"
  5869. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5870. col)
  5871. (unless (save-excursion (end-of-line 1)
  5872. (re-search-forward prohibit end t))
  5873. (while (and (< (point) end)
  5874. (re-search-forward "^[ \t]+" end t))
  5875. (goto-char (match-end 0))
  5876. (setq col (current-column))
  5877. (if (< diff 0) (replace-match ""))
  5878. (org-indent-to-column (+ diff col))))
  5879. (move-marker end nil))))
  5880. (defun org-convert-to-odd-levels ()
  5881. "Convert an org-mode file with all levels allowed to one with odd levels.
  5882. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5883. level 5 etc."
  5884. (interactive)
  5885. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5886. (let ((outline-regexp org-outline-regexp)
  5887. (outline-level 'org-outline-level)
  5888. (org-odd-levels-only nil) n)
  5889. (save-excursion
  5890. (goto-char (point-min))
  5891. (while (re-search-forward "^\\*\\*+ " nil t)
  5892. (setq n (- (length (match-string 0)) 2))
  5893. (while (>= (setq n (1- n)) 0)
  5894. (org-demote))
  5895. (end-of-line 1))))))
  5896. (defun org-convert-to-oddeven-levels ()
  5897. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5898. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5899. section with an even level, conversion would destroy the structure of the file. An error
  5900. is signaled in this case."
  5901. (interactive)
  5902. (goto-char (point-min))
  5903. ;; First check if there are no even levels
  5904. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5905. (org-show-context t)
  5906. (error "Not all levels are odd in this file. Conversion not possible"))
  5907. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5908. (let ((outline-regexp org-outline-regexp)
  5909. (outline-level 'org-outline-level)
  5910. (org-odd-levels-only nil) n)
  5911. (save-excursion
  5912. (goto-char (point-min))
  5913. (while (re-search-forward "^\\*\\*+ " nil t)
  5914. (setq n (/ (1- (length (match-string 0))) 2))
  5915. (while (>= (setq n (1- n)) 0)
  5916. (org-promote))
  5917. (end-of-line 1))))))
  5918. (defun org-tr-level (n)
  5919. "Make N odd if required."
  5920. (if org-odd-levels-only (1+ (/ n 2)) n))
  5921. ;;; Vertical tree motion, cutting and pasting of subtrees
  5922. (defun org-move-subtree-up (&optional arg)
  5923. "Move the current subtree up past ARG headlines of the same level."
  5924. (interactive "p")
  5925. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5926. (defun org-move-subtree-down (&optional arg)
  5927. "Move the current subtree down past ARG headlines of the same level."
  5928. (interactive "p")
  5929. (setq arg (prefix-numeric-value arg))
  5930. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  5931. 'org-get-last-sibling))
  5932. (ins-point (make-marker))
  5933. (cnt (abs arg))
  5934. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5935. ;; Select the tree
  5936. (org-back-to-heading)
  5937. (setq beg0 (point))
  5938. (save-excursion
  5939. (setq ne-beg (org-back-over-empty-lines))
  5940. (setq beg (point)))
  5941. (save-match-data
  5942. (save-excursion (outline-end-of-heading)
  5943. (setq folded (org-invisible-p)))
  5944. (outline-end-of-subtree))
  5945. (outline-next-heading)
  5946. (setq ne-end (org-back-over-empty-lines))
  5947. (setq end (point))
  5948. (goto-char beg0)
  5949. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5950. ;; include less whitespace
  5951. (save-excursion
  5952. (goto-char beg)
  5953. (forward-line (- ne-beg ne-end))
  5954. (setq beg (point))))
  5955. ;; Find insertion point, with error handling
  5956. (while (> cnt 0)
  5957. (or (and (funcall movfunc) (looking-at outline-regexp))
  5958. (progn (goto-char beg0)
  5959. (error "Cannot move past superior level or buffer limit")))
  5960. (setq cnt (1- cnt)))
  5961. (if (> arg 0)
  5962. ;; Moving forward - still need to move over subtree
  5963. (progn (org-end-of-subtree t t)
  5964. (save-excursion
  5965. (org-back-over-empty-lines)
  5966. (or (bolp) (newline)))))
  5967. (setq ne-ins (org-back-over-empty-lines))
  5968. (move-marker ins-point (point))
  5969. (setq txt (buffer-substring beg end))
  5970. (org-save-markers-in-region beg end)
  5971. (delete-region beg end)
  5972. (org-remove-empty-overlays-at beg)
  5973. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5974. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5975. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5976. (let ((bbb (point)))
  5977. (insert-before-markers txt)
  5978. (org-reinstall-markers-in-region bbb)
  5979. (move-marker ins-point bbb))
  5980. (or (bolp) (insert "\n"))
  5981. (setq ins-end (point))
  5982. (goto-char ins-point)
  5983. (org-skip-whitespace)
  5984. (when (and (< arg 0)
  5985. (org-first-sibling-p)
  5986. (> ne-ins ne-beg))
  5987. ;; Move whitespace back to beginning
  5988. (save-excursion
  5989. (goto-char ins-end)
  5990. (let ((kill-whole-line t))
  5991. (kill-line (- ne-ins ne-beg)) (point)))
  5992. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5993. (move-marker ins-point nil)
  5994. (if folded
  5995. (hide-subtree)
  5996. (org-show-entry)
  5997. (show-children)
  5998. (org-cycle-hide-drawers 'children))
  5999. (org-clean-visibility-after-subtree-move)))
  6000. (defvar org-subtree-clip ""
  6001. "Clipboard for cut and paste of subtrees.
  6002. This is actually only a copy of the kill, because we use the normal kill
  6003. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6004. (defvar org-subtree-clip-folded nil
  6005. "Was the last copied subtree folded?
  6006. This is used to fold the tree back after pasting.")
  6007. (defun org-cut-subtree (&optional n)
  6008. "Cut the current subtree into the clipboard.
  6009. With prefix arg N, cut this many sequential subtrees.
  6010. This is a short-hand for marking the subtree and then cutting it."
  6011. (interactive "p")
  6012. (org-copy-subtree n 'cut))
  6013. (defun org-copy-subtree (&optional n cut force-store-markers)
  6014. "Cut the current subtree into the clipboard.
  6015. With prefix arg N, cut this many sequential subtrees.
  6016. This is a short-hand for marking the subtree and then copying it.
  6017. If CUT is non-nil, actually cut the subtree.
  6018. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6019. of some markers in the region, even if CUT is non-nil. This is
  6020. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6021. (interactive "p")
  6022. (let (beg end folded (beg0 (point)))
  6023. (if (interactive-p)
  6024. (org-back-to-heading nil) ; take what looks like a subtree
  6025. (org-back-to-heading t)) ; take what is really there
  6026. (org-back-over-empty-lines)
  6027. (setq beg (point))
  6028. (skip-chars-forward " \t\r\n")
  6029. (save-match-data
  6030. (save-excursion (outline-end-of-heading)
  6031. (setq folded (org-invisible-p)))
  6032. (condition-case nil
  6033. (org-forward-same-level (1- n) t)
  6034. (error nil))
  6035. (org-end-of-subtree t t))
  6036. (org-back-over-empty-lines)
  6037. (setq end (point))
  6038. (goto-char beg0)
  6039. (when (> end beg)
  6040. (setq org-subtree-clip-folded folded)
  6041. (when (or cut force-store-markers)
  6042. (org-save-markers-in-region beg end))
  6043. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6044. (setq org-subtree-clip (current-kill 0))
  6045. (message "%s: Subtree(s) with %d characters"
  6046. (if cut "Cut" "Copied")
  6047. (length org-subtree-clip)))))
  6048. (defun org-paste-subtree (&optional level tree for-yank)
  6049. "Paste the clipboard as a subtree, with modification of headline level.
  6050. The entire subtree is promoted or demoted in order to match a new headline
  6051. level.
  6052. If the cursor is at the beginning of a headline, the same level as
  6053. that headline is used to paste the tree
  6054. If not, the new level is derived from the *visible* headings
  6055. before and after the insertion point, and taken to be the inferior headline
  6056. level of the two. So if the previous visible heading is level 3 and the
  6057. next is level 4 (or vice versa), level 4 will be used for insertion.
  6058. This makes sure that the subtree remains an independent subtree and does
  6059. not swallow low level entries.
  6060. You can also force a different level, either by using a numeric prefix
  6061. argument, or by inserting the heading marker by hand. For example, if the
  6062. cursor is after \"*****\", then the tree will be shifted to level 5.
  6063. If optional TREE is given, use this text instead of the kill ring.
  6064. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6065. move back over whitespace before inserting, and move point to the end of
  6066. the inserted text when done."
  6067. (interactive "P")
  6068. (setq tree (or tree (and kill-ring (current-kill 0))))
  6069. (unless (org-kill-is-subtree-p tree)
  6070. (error "%s"
  6071. (substitute-command-keys
  6072. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6073. (let* ((visp (not (org-invisible-p)))
  6074. (txt tree)
  6075. (^re (concat "^\\(" outline-regexp "\\)"))
  6076. (re (concat "\\(" outline-regexp "\\)"))
  6077. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6078. (old-level (if (string-match ^re txt)
  6079. (- (match-end 0) (match-beginning 0) 1)
  6080. -1))
  6081. (force-level (cond (level (prefix-numeric-value level))
  6082. ((and (looking-at "[ \t]*$")
  6083. (string-match
  6084. ^re_ (buffer-substring
  6085. (point-at-bol) (point))))
  6086. (- (match-end 1) (match-beginning 1)))
  6087. ((and (bolp)
  6088. (looking-at org-outline-regexp))
  6089. (- (match-end 0) (point) 1))
  6090. (t nil)))
  6091. (previous-level (save-excursion
  6092. (condition-case nil
  6093. (progn
  6094. (outline-previous-visible-heading 1)
  6095. (if (looking-at re)
  6096. (- (match-end 0) (match-beginning 0) 1)
  6097. 1))
  6098. (error 1))))
  6099. (next-level (save-excursion
  6100. (condition-case nil
  6101. (progn
  6102. (or (looking-at outline-regexp)
  6103. (outline-next-visible-heading 1))
  6104. (if (looking-at re)
  6105. (- (match-end 0) (match-beginning 0) 1)
  6106. 1))
  6107. (error 1))))
  6108. (new-level (or force-level (max previous-level next-level)))
  6109. (shift (if (or (= old-level -1)
  6110. (= new-level -1)
  6111. (= old-level new-level))
  6112. 0
  6113. (- new-level old-level)))
  6114. (delta (if (> shift 0) -1 1))
  6115. (func (if (> shift 0) 'org-demote 'org-promote))
  6116. (org-odd-levels-only nil)
  6117. beg end newend)
  6118. ;; Remove the forced level indicator
  6119. (if force-level
  6120. (delete-region (point-at-bol) (point)))
  6121. ;; Paste
  6122. (beginning-of-line 1)
  6123. (unless for-yank (org-back-over-empty-lines))
  6124. (setq beg (point))
  6125. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6126. (insert-before-markers txt)
  6127. (unless (string-match "\n\\'" txt) (insert "\n"))
  6128. (setq newend (point))
  6129. (org-reinstall-markers-in-region beg)
  6130. (setq end (point))
  6131. (goto-char beg)
  6132. (skip-chars-forward " \t\n\r")
  6133. (setq beg (point))
  6134. (if (and (org-invisible-p) visp)
  6135. (save-excursion (outline-show-heading)))
  6136. ;; Shift if necessary
  6137. (unless (= shift 0)
  6138. (save-restriction
  6139. (narrow-to-region beg end)
  6140. (while (not (= shift 0))
  6141. (org-map-region func (point-min) (point-max))
  6142. (setq shift (+ delta shift)))
  6143. (goto-char (point-min))
  6144. (setq newend (point-max))))
  6145. (when (or (interactive-p) for-yank)
  6146. (message "Clipboard pasted as level %d subtree" new-level))
  6147. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6148. kill-ring
  6149. (eq org-subtree-clip (current-kill 0))
  6150. org-subtree-clip-folded)
  6151. ;; The tree was folded before it was killed/copied
  6152. (hide-subtree))
  6153. (and for-yank (goto-char newend))))
  6154. (defun org-kill-is-subtree-p (&optional txt)
  6155. "Check if the current kill is an outline subtree, or a set of trees.
  6156. Returns nil if kill does not start with a headline, or if the first
  6157. headline level is not the largest headline level in the tree.
  6158. So this will actually accept several entries of equal levels as well,
  6159. which is OK for `org-paste-subtree'.
  6160. If optional TXT is given, check this string instead of the current kill."
  6161. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6162. (start-level (and kill
  6163. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6164. org-outline-regexp "\\)")
  6165. kill)
  6166. (- (match-end 2) (match-beginning 2) 1)))
  6167. (re (concat "^" org-outline-regexp))
  6168. (start (1+ (or (match-beginning 2) -1))))
  6169. (if (not start-level)
  6170. (progn
  6171. nil) ;; does not even start with a heading
  6172. (catch 'exit
  6173. (while (setq start (string-match re kill (1+ start)))
  6174. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6175. (throw 'exit nil)))
  6176. t))))
  6177. (defvar org-markers-to-move nil
  6178. "Markers that should be moved with a cut-and-paste operation.
  6179. Those markers are stored together with their positions relative to
  6180. the start of the region.")
  6181. (defun org-save-markers-in-region (beg end)
  6182. "Check markers in region.
  6183. If these markers are between BEG and END, record their position relative
  6184. to BEG, so that after moving the block of text, we can put the markers back
  6185. into place.
  6186. This function gets called just before an entry or tree gets cut from the
  6187. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6188. called immediately, to move the markers with the entries."
  6189. (setq org-markers-to-move nil)
  6190. (when (featurep 'org-clock)
  6191. (org-clock-save-markers-for-cut-and-paste beg end))
  6192. (when (featurep 'org-agenda)
  6193. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6194. (defun org-check-and-save-marker (marker beg end)
  6195. "Check if MARKER is between BEG and END.
  6196. If yes, remember the marker and the distance to BEG."
  6197. (when (and (marker-buffer marker)
  6198. (equal (marker-buffer marker) (current-buffer)))
  6199. (if (and (>= marker beg) (< marker end))
  6200. (push (cons marker (- marker beg)) org-markers-to-move))))
  6201. (defun org-reinstall-markers-in-region (beg)
  6202. "Move all remembered markers to their position relative to BEG."
  6203. (mapc (lambda (x)
  6204. (move-marker (car x) (+ beg (cdr x))))
  6205. org-markers-to-move)
  6206. (setq org-markers-to-move nil))
  6207. (defun org-narrow-to-subtree ()
  6208. "Narrow buffer to the current subtree."
  6209. (interactive)
  6210. (save-excursion
  6211. (save-match-data
  6212. (narrow-to-region
  6213. (progn (org-back-to-heading t) (point))
  6214. (progn (org-end-of-subtree t t)
  6215. (if (org-on-heading-p) (backward-char 1))
  6216. (point))))))
  6217. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6218. "Clone the task (subtree) at point N times.
  6219. The clones will be inserted as siblings.
  6220. In interactive use, the user will be prompted for the number of clones
  6221. to be produced, and for a time SHIFT, which may be a repeater as used
  6222. in time stamps, for example `+3d'.
  6223. When a valid repeater is given and the entry contains any time stamps,
  6224. the clones will become a sequence in time, with time stamps in the
  6225. subtree shifted for each clone produced. If SHIFT is nil or the
  6226. empty string, time stamps will be left alone.
  6227. If the original subtree did contain time stamps with a repeater,
  6228. the following will happen:
  6229. - the repeater will be removed in each clone
  6230. - an additional clone will be produced, with the current, unshifted
  6231. date(s) in the entry.
  6232. - the original entry will be placed *after* all the clones, with
  6233. repeater intact.
  6234. - the start days in the repeater in the original entry will be shifted
  6235. to past the last clone.
  6236. I this way you can spell out a number of instances of a repeating task,
  6237. and still retain the repeater to cover future instances of the task."
  6238. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6239. (let (beg end template task
  6240. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6241. (if (not (and (integerp n) (> n 0)))
  6242. (error "Invalid number of replications %s" n))
  6243. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6244. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6245. shift)))
  6246. (error "Invalid shift specification %s" shift))
  6247. (when doshift
  6248. (setq shift-n (string-to-number (match-string 1 shift))
  6249. shift-what (cdr (assoc (match-string 2 shift)
  6250. '(("d" . day) ("w" . week)
  6251. ("m" . month) ("y" . year))))))
  6252. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6253. (setq nmin 1 nmax n)
  6254. (org-back-to-heading t)
  6255. (setq beg (point))
  6256. (org-end-of-subtree t t)
  6257. (or (bolp) (insert "\n"))
  6258. (setq end (point))
  6259. (setq template (buffer-substring beg end))
  6260. (when (and doshift
  6261. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6262. (delete-region beg end)
  6263. (setq end beg)
  6264. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6265. (goto-char end)
  6266. (loop for n from nmin to nmax do
  6267. (if (not doshift)
  6268. (setq task template)
  6269. (with-temp-buffer
  6270. (insert template)
  6271. (org-mode)
  6272. (goto-char (point-min))
  6273. (while (re-search-forward org-ts-regexp-both nil t)
  6274. (org-timestamp-change (* n shift-n) shift-what))
  6275. (unless (= n n-no-remove)
  6276. (goto-char (point-min))
  6277. (while (re-search-forward org-ts-regexp nil t)
  6278. (save-excursion
  6279. (goto-char (match-beginning 0))
  6280. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6281. (delete-region (match-beginning 1) (match-end 1))))))
  6282. (setq task (buffer-string))))
  6283. (insert task))
  6284. (goto-char beg)))
  6285. ;;; Outline Sorting
  6286. (defun org-sort (with-case)
  6287. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6288. Optional argument WITH-CASE means sort case-sensitively.
  6289. With a double prefix argument, also remove duplicate entries."
  6290. (interactive "P")
  6291. (if (org-at-table-p)
  6292. (org-call-with-arg 'org-table-sort-lines with-case)
  6293. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6294. (defun org-sort-remove-invisible (s)
  6295. (remove-text-properties 0 (length s) org-rm-props s)
  6296. (while (string-match org-bracket-link-regexp s)
  6297. (setq s (replace-match (if (match-end 2)
  6298. (match-string 3 s)
  6299. (match-string 1 s)) t t s)))
  6300. s)
  6301. (defvar org-priority-regexp) ; defined later in the file
  6302. (defvar org-after-sorting-entries-or-items-hook nil
  6303. "Hook that is run after a bunch of entries or items have been sorted.
  6304. When children are sorted, the cursor is in the parent line when this
  6305. hook gets called. When a region or a plain list is sorted, the cursor
  6306. will be in the first entry of the sorted region/list.")
  6307. (defun org-sort-entries-or-items
  6308. (&optional with-case sorting-type getkey-func compare-func property)
  6309. "Sort entries on a certain level of an outline tree, or plain list items.
  6310. If there is an active region, the entries in the region are sorted.
  6311. Else, if the cursor is before the first entry, sort the top-level items.
  6312. Else, the children of the entry at point are sorted.
  6313. If the cursor is at the first item in a plain list, the list items will be
  6314. sorted.
  6315. Sorting can be alphabetically, numerically, by date/time as given by
  6316. a time stamp, by a property or by priority.
  6317. The command prompts for the sorting type unless it has been given to the
  6318. function through the SORTING-TYPE argument, which needs to a character,
  6319. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6320. precise meaning of each character:
  6321. n Numerically, by converting the beginning of the entry/item to a number.
  6322. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6323. t By date/time, either the first active time stamp in the entry, or, if
  6324. none exist, by the first inactive one.
  6325. In items, only the first line will be checked.
  6326. s By the scheduled date/time.
  6327. d By deadline date/time.
  6328. c By creation time, which is assumed to be the first inactive time stamp
  6329. at the beginning of a line.
  6330. p By priority according to the cookie.
  6331. r By the value of a property.
  6332. Capital letters will reverse the sort order.
  6333. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6334. called with point at the beginning of the record. It must return either
  6335. a string or a number that should serve as the sorting key for that record.
  6336. Comparing entries ignores case by default. However, with an optional argument
  6337. WITH-CASE, the sorting considers case as well."
  6338. (interactive "P")
  6339. (let ((case-func (if with-case 'identity 'downcase))
  6340. start beg end stars re re2
  6341. txt what tmp plain-list-p)
  6342. ;; Find beginning and end of region to sort
  6343. (cond
  6344. ((org-region-active-p)
  6345. ;; we will sort the region
  6346. (setq end (region-end)
  6347. what "region")
  6348. (goto-char (region-beginning))
  6349. (if (not (org-on-heading-p)) (outline-next-heading))
  6350. (setq start (point)))
  6351. ((org-at-item-p)
  6352. ;; we will sort this plain list
  6353. (org-beginning-of-item-list) (setq start (point))
  6354. (org-end-of-item-list)
  6355. (or (bolp) (insert "\n"))
  6356. (setq end (point))
  6357. (goto-char start)
  6358. (setq plain-list-p t
  6359. what "plain list"))
  6360. ((or (org-on-heading-p)
  6361. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6362. ;; we will sort the children of the current headline
  6363. (org-back-to-heading)
  6364. (setq start (point)
  6365. end (progn (org-end-of-subtree t t)
  6366. (or (bolp) (insert "\n"))
  6367. (org-back-over-empty-lines)
  6368. (point))
  6369. what "children")
  6370. (goto-char start)
  6371. (show-subtree)
  6372. (outline-next-heading))
  6373. (t
  6374. ;; we will sort the top-level entries in this file
  6375. (goto-char (point-min))
  6376. (or (org-on-heading-p) (outline-next-heading))
  6377. (setq start (point))
  6378. (goto-char (point-max))
  6379. (beginning-of-line 1)
  6380. (when (looking-at ".*?\\S-")
  6381. ;; File ends in a non-white line
  6382. (end-of-line 1)
  6383. (insert "\n"))
  6384. (setq end (point-max))
  6385. (setq what "top-level")
  6386. (goto-char start)
  6387. (show-all)))
  6388. (setq beg (point))
  6389. (if (>= beg end) (error "Nothing to sort"))
  6390. (unless plain-list-p
  6391. (looking-at "\\(\\*+\\)")
  6392. (setq stars (match-string 1)
  6393. re (concat "^" (regexp-quote stars) " +")
  6394. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6395. txt (buffer-substring beg end))
  6396. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6397. (if (and (not (equal stars "*")) (string-match re2 txt))
  6398. (error "Region to sort contains a level above the first entry")))
  6399. (unless sorting-type
  6400. (message
  6401. (if plain-list-p
  6402. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6403. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6404. [t]ime [s]cheduled [d]eadline [c]reated
  6405. A/N/T/S/D/C/P/O/F means reversed:")
  6406. what)
  6407. (setq sorting-type (read-char-exclusive))
  6408. (and (= (downcase sorting-type) ?f)
  6409. (setq getkey-func
  6410. (org-icompleting-read "Sort using function: "
  6411. obarray 'fboundp t nil nil))
  6412. (setq getkey-func (intern getkey-func)))
  6413. (and (= (downcase sorting-type) ?r)
  6414. (setq property
  6415. (org-icompleting-read "Property: "
  6416. (mapcar 'list (org-buffer-property-keys t))
  6417. nil t))))
  6418. (message "Sorting entries...")
  6419. (save-restriction
  6420. (narrow-to-region start end)
  6421. (let ((dcst (downcase sorting-type))
  6422. (case-fold-search nil)
  6423. (now (current-time)))
  6424. (sort-subr
  6425. (/= dcst sorting-type)
  6426. ;; This function moves to the beginning character of the "record" to
  6427. ;; be sorted.
  6428. (if plain-list-p
  6429. (lambda nil
  6430. (if (org-at-item-p) t (goto-char (point-max))))
  6431. (lambda nil
  6432. (if (re-search-forward re nil t)
  6433. (goto-char (match-beginning 0))
  6434. (goto-char (point-max)))))
  6435. ;; This function moves to the last character of the "record" being
  6436. ;; sorted.
  6437. (if plain-list-p
  6438. 'org-end-of-item
  6439. (lambda nil
  6440. (save-match-data
  6441. (condition-case nil
  6442. (outline-forward-same-level 1)
  6443. (error
  6444. (goto-char (point-max)))))))
  6445. ;; This function returns the value that gets sorted against.
  6446. (if plain-list-p
  6447. (lambda nil
  6448. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6449. (cond
  6450. ((= dcst ?n)
  6451. (string-to-number (buffer-substring (match-end 0)
  6452. (point-at-eol))))
  6453. ((= dcst ?a)
  6454. (buffer-substring (match-end 0) (point-at-eol)))
  6455. ((= dcst ?t)
  6456. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  6457. (re-search-forward org-ts-regexp-both
  6458. (point-at-eol) t))
  6459. (org-time-string-to-seconds (match-string 0))
  6460. (org-float-time now)))
  6461. ((= dcst ?f)
  6462. (if getkey-func
  6463. (progn
  6464. (setq tmp (funcall getkey-func))
  6465. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6466. tmp)
  6467. (error "Invalid key function `%s'" getkey-func)))
  6468. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6469. (lambda nil
  6470. (cond
  6471. ((= dcst ?n)
  6472. (if (looking-at org-complex-heading-regexp)
  6473. (string-to-number (match-string 4))
  6474. nil))
  6475. ((= dcst ?a)
  6476. (if (looking-at org-complex-heading-regexp)
  6477. (funcall case-func (match-string 4))
  6478. nil))
  6479. ((= dcst ?t)
  6480. (let ((end (save-excursion (outline-next-heading) (point))))
  6481. (if (or (re-search-forward org-ts-regexp end t)
  6482. (re-search-forward org-ts-regexp-both end t))
  6483. (org-time-string-to-seconds (match-string 0))
  6484. (org-float-time now))))
  6485. ((= dcst ?c)
  6486. (let ((end (save-excursion (outline-next-heading) (point))))
  6487. (if (re-search-forward
  6488. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6489. end t)
  6490. (org-time-string-to-seconds (match-string 0))
  6491. (org-float-time now))))
  6492. ((= dcst ?s)
  6493. (let ((end (save-excursion (outline-next-heading) (point))))
  6494. (if (re-search-forward org-scheduled-time-regexp end t)
  6495. (org-time-string-to-seconds (match-string 1))
  6496. (org-float-time now))))
  6497. ((= dcst ?d)
  6498. (let ((end (save-excursion (outline-next-heading) (point))))
  6499. (if (re-search-forward org-deadline-time-regexp end t)
  6500. (org-time-string-to-seconds (match-string 1))
  6501. (org-float-time now))))
  6502. ((= dcst ?p)
  6503. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6504. (string-to-char (match-string 2))
  6505. org-default-priority))
  6506. ((= dcst ?r)
  6507. (or (org-entry-get nil property) ""))
  6508. ((= dcst ?o)
  6509. (if (looking-at org-complex-heading-regexp)
  6510. (- 9999 (length (member (match-string 2)
  6511. org-todo-keywords-1)))))
  6512. ((= dcst ?f)
  6513. (if getkey-func
  6514. (progn
  6515. (setq tmp (funcall getkey-func))
  6516. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6517. tmp)
  6518. (error "Invalid key function `%s'" getkey-func)))
  6519. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6520. nil
  6521. (cond
  6522. ((= dcst ?a) 'string<)
  6523. ((= dcst ?f) compare-func)
  6524. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6525. (t nil)))))
  6526. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6527. (message "Sorting entries...done")))
  6528. (defun org-do-sort (table what &optional with-case sorting-type)
  6529. "Sort TABLE of WHAT according to SORTING-TYPE.
  6530. The user will be prompted for the SORTING-TYPE if the call to this
  6531. function does not specify it. WHAT is only for the prompt, to indicate
  6532. what is being sorted. The sorting key will be extracted from
  6533. the car of the elements of the table.
  6534. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6535. (unless sorting-type
  6536. (message
  6537. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6538. what)
  6539. (setq sorting-type (read-char-exclusive)))
  6540. (let ((dcst (downcase sorting-type))
  6541. extractfun comparefun)
  6542. ;; Define the appropriate functions
  6543. (cond
  6544. ((= dcst ?n)
  6545. (setq extractfun 'string-to-number
  6546. comparefun (if (= dcst sorting-type) '< '>)))
  6547. ((= dcst ?a)
  6548. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6549. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6550. comparefun (if (= dcst sorting-type)
  6551. 'string<
  6552. (lambda (a b) (and (not (string< a b))
  6553. (not (string= a b)))))))
  6554. ((= dcst ?t)
  6555. (setq extractfun
  6556. (lambda (x)
  6557. (if (or (string-match org-ts-regexp x)
  6558. (string-match org-ts-regexp-both x))
  6559. (org-float-time
  6560. (org-time-string-to-time (match-string 0 x)))
  6561. 0))
  6562. comparefun (if (= dcst sorting-type) '< '>)))
  6563. (t (error "Invalid sorting type `%c'" sorting-type)))
  6564. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6565. table)
  6566. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6567. ;;; The orgstruct minor mode
  6568. ;; Define a minor mode which can be used in other modes in order to
  6569. ;; integrate the org-mode structure editing commands.
  6570. ;; This is really a hack, because the org-mode structure commands use
  6571. ;; keys which normally belong to the major mode. Here is how it
  6572. ;; works: The minor mode defines all the keys necessary to operate the
  6573. ;; structure commands, but wraps the commands into a function which
  6574. ;; tests if the cursor is currently at a headline or a plain list
  6575. ;; item. If that is the case, the structure command is used,
  6576. ;; temporarily setting many Org-mode variables like regular
  6577. ;; expressions for filling etc. However, when any of those keys is
  6578. ;; used at a different location, function uses `key-binding' to look
  6579. ;; up if the key has an associated command in another currently active
  6580. ;; keymap (minor modes, major mode, global), and executes that
  6581. ;; command. There might be problems if any of the keys is otherwise
  6582. ;; used as a prefix key.
  6583. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6584. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6585. ;; addresses this by checking explicitly for both bindings.
  6586. (defvar orgstruct-mode-map (make-sparse-keymap)
  6587. "Keymap for the minor `orgstruct-mode'.")
  6588. (defvar org-local-vars nil
  6589. "List of local variables, for use by `orgstruct-mode'")
  6590. ;;;###autoload
  6591. (define-minor-mode orgstruct-mode
  6592. "Toggle the minor more `orgstruct-mode'.
  6593. This mode is for using Org-mode structure commands in other modes.
  6594. The following key behave as if Org-mode was active, if the cursor
  6595. is on a headline, or on a plain list item (both in the definition
  6596. of Org-mode).
  6597. M-up Move entry/item up
  6598. M-down Move entry/item down
  6599. M-left Promote
  6600. M-right Demote
  6601. M-S-up Move entry/item up
  6602. M-S-down Move entry/item down
  6603. M-S-left Promote subtree
  6604. M-S-right Demote subtree
  6605. M-q Fill paragraph and items like in Org-mode
  6606. C-c ^ Sort entries
  6607. C-c - Cycle list bullet
  6608. TAB Cycle item visibility
  6609. M-RET Insert new heading/item
  6610. S-M-RET Insert new TODO heading / Checkbox item
  6611. C-c C-c Set tags / toggle checkbox"
  6612. nil " OrgStruct" nil
  6613. (org-load-modules-maybe)
  6614. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6615. ;;;###autoload
  6616. (defun turn-on-orgstruct ()
  6617. "Unconditionally turn on `orgstruct-mode'."
  6618. (orgstruct-mode 1))
  6619. (defun orgstruct++-mode (&optional arg)
  6620. "Toggle `orgstruct-mode', the enhanced version of it.
  6621. In addition to setting orgstruct-mode, this also exports all indentation
  6622. and autofilling variables from org-mode into the buffer. It will also
  6623. recognize item context in multiline items.
  6624. Note that turning off orgstruct-mode will *not* remove the
  6625. indentation/paragraph settings. This can only be done by refreshing the
  6626. major mode, for example with \\[normal-mode]."
  6627. (interactive "P")
  6628. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6629. (if (< arg 1)
  6630. (orgstruct-mode -1)
  6631. (orgstruct-mode 1)
  6632. (let (var val)
  6633. (mapc
  6634. (lambda (x)
  6635. (when (string-match
  6636. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6637. (symbol-name (car x)))
  6638. (setq var (car x) val (nth 1 x))
  6639. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6640. org-local-vars)
  6641. (org-set-local 'orgstruct-is-++ t))))
  6642. (defvar orgstruct-is-++ nil
  6643. "Is orgstruct-mode in ++ version in the current-buffer?")
  6644. (make-variable-buffer-local 'orgstruct-is-++)
  6645. ;;;###autoload
  6646. (defun turn-on-orgstruct++ ()
  6647. "Unconditionally turn on `orgstruct++-mode'."
  6648. (orgstruct++-mode 1))
  6649. (defun orgstruct-error ()
  6650. "Error when there is no default binding for a structure key."
  6651. (interactive)
  6652. (error "This key has no function outside structure elements"))
  6653. (defun orgstruct-setup ()
  6654. "Setup orgstruct keymaps."
  6655. (let ((nfunc 0)
  6656. (bindings
  6657. (list
  6658. '([(meta up)] org-metaup)
  6659. '([(meta down)] org-metadown)
  6660. '([(meta left)] org-metaleft)
  6661. '([(meta right)] org-metaright)
  6662. '([(meta shift up)] org-shiftmetaup)
  6663. '([(meta shift down)] org-shiftmetadown)
  6664. '([(meta shift left)] org-shiftmetaleft)
  6665. '([(meta shift right)] org-shiftmetaright)
  6666. '([?\e (up)] org-metaup)
  6667. '([?\e (down)] org-metadown)
  6668. '([?\e (left)] org-metaleft)
  6669. '([?\e (right)] org-metaright)
  6670. '([?\e (shift up)] org-shiftmetaup)
  6671. '([?\e (shift down)] org-shiftmetadown)
  6672. '([?\e (shift left)] org-shiftmetaleft)
  6673. '([?\e (shift right)] org-shiftmetaright)
  6674. '([(shift up)] org-shiftup)
  6675. '([(shift down)] org-shiftdown)
  6676. '([(shift left)] org-shiftleft)
  6677. '([(shift right)] org-shiftright)
  6678. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6679. '("\M-q" fill-paragraph)
  6680. '("\C-c^" org-sort)
  6681. '("\C-c-" org-cycle-list-bullet)))
  6682. elt key fun cmd)
  6683. (while (setq elt (pop bindings))
  6684. (setq nfunc (1+ nfunc))
  6685. (setq key (org-key (car elt))
  6686. fun (nth 1 elt)
  6687. cmd (orgstruct-make-binding fun nfunc key))
  6688. (org-defkey orgstruct-mode-map key cmd))
  6689. ;; Special treatment needed for TAB and RET
  6690. (org-defkey orgstruct-mode-map [(tab)]
  6691. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6692. (org-defkey orgstruct-mode-map "\C-i"
  6693. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6694. (org-defkey orgstruct-mode-map "\M-\C-m"
  6695. (orgstruct-make-binding 'org-insert-heading 105
  6696. "\M-\C-m" [(meta return)]))
  6697. (org-defkey orgstruct-mode-map [(meta return)]
  6698. (orgstruct-make-binding 'org-insert-heading 106
  6699. [(meta return)] "\M-\C-m"))
  6700. (org-defkey orgstruct-mode-map [(shift meta return)]
  6701. (orgstruct-make-binding 'org-insert-todo-heading 107
  6702. [(meta return)] "\M-\C-m"))
  6703. (org-defkey orgstruct-mode-map "\e\C-m"
  6704. (orgstruct-make-binding 'org-insert-heading 108
  6705. "\e\C-m" [?\e (return)]))
  6706. (org-defkey orgstruct-mode-map [?\e (return)]
  6707. (orgstruct-make-binding 'org-insert-heading 109
  6708. [?\e (return)] "\e\C-m"))
  6709. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6710. (orgstruct-make-binding 'org-insert-todo-heading 110
  6711. [?\e (return)] "\e\C-m"))
  6712. (unless org-local-vars
  6713. (setq org-local-vars (org-get-local-variables)))
  6714. t))
  6715. (defun orgstruct-make-binding (fun n &rest keys)
  6716. "Create a function for binding in the structure minor mode.
  6717. FUN is the command to call inside a table. N is used to create a unique
  6718. command name. KEYS are keys that should be checked in for a command
  6719. to execute outside of tables."
  6720. (eval
  6721. (list 'defun
  6722. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6723. '(arg)
  6724. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6725. "Outside of structure, run the binding of `"
  6726. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6727. "'.")
  6728. '(interactive "p")
  6729. (list 'if
  6730. `(org-context-p 'headline 'item
  6731. (and orgstruct-is-++
  6732. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6733. 'item-body))
  6734. (list 'org-run-like-in-org-mode (list 'quote fun))
  6735. (list 'let '(orgstruct-mode)
  6736. (list 'call-interactively
  6737. (append '(or)
  6738. (mapcar (lambda (k)
  6739. (list 'key-binding k))
  6740. keys)
  6741. '('orgstruct-error))))))))
  6742. (defun org-context-p (&rest contexts)
  6743. "Check if local context is any of CONTEXTS.
  6744. Possible values in the list of contexts are `table', `headline', and `item'."
  6745. (let ((pos (point)))
  6746. (goto-char (point-at-bol))
  6747. (prog1 (or (and (memq 'table contexts)
  6748. (looking-at "[ \t]*|"))
  6749. (and (memq 'headline contexts)
  6750. ;;????????? (looking-at "\\*+"))
  6751. (looking-at outline-regexp))
  6752. (and (memq 'item contexts)
  6753. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6754. (and (memq 'item-body contexts)
  6755. (org-in-item-p)))
  6756. (goto-char pos))))
  6757. (defun org-get-local-variables ()
  6758. "Return a list of all local variables in an org-mode buffer."
  6759. (let (varlist)
  6760. (with-current-buffer (get-buffer-create "*Org tmp*")
  6761. (erase-buffer)
  6762. (org-mode)
  6763. (setq varlist (buffer-local-variables)))
  6764. (kill-buffer "*Org tmp*")
  6765. (delq nil
  6766. (mapcar
  6767. (lambda (x)
  6768. (setq x
  6769. (if (symbolp x)
  6770. (list x)
  6771. (list (car x) (list 'quote (cdr x)))))
  6772. (if (string-match
  6773. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6774. (symbol-name (car x)))
  6775. x nil))
  6776. varlist))))
  6777. ;;;###autoload
  6778. (defun org-run-like-in-org-mode (cmd)
  6779. "Run a command, pretending that the current buffer is in Org-mode.
  6780. This will temporarily bind local variables that are typically bound in
  6781. Org-mode to the values they have in Org-mode, and then interactively
  6782. call CMD."
  6783. (org-load-modules-maybe)
  6784. (unless org-local-vars
  6785. (setq org-local-vars (org-get-local-variables)))
  6786. (eval (list 'let org-local-vars
  6787. (list 'call-interactively (list 'quote cmd)))))
  6788. ;;;; Archiving
  6789. (defun org-get-category (&optional pos)
  6790. "Get the category applying to position POS."
  6791. (get-text-property (or pos (point)) 'org-category))
  6792. (defun org-refresh-category-properties ()
  6793. "Refresh category text properties in the buffer."
  6794. (let ((def-cat (cond
  6795. ((null org-category)
  6796. (if buffer-file-name
  6797. (file-name-sans-extension
  6798. (file-name-nondirectory buffer-file-name))
  6799. "???"))
  6800. ((symbolp org-category) (symbol-name org-category))
  6801. (t org-category)))
  6802. beg end cat pos optionp)
  6803. (org-unmodified
  6804. (save-excursion
  6805. (save-restriction
  6806. (widen)
  6807. (goto-char (point-min))
  6808. (put-text-property (point) (point-max) 'org-category def-cat)
  6809. (while (re-search-forward
  6810. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6811. (setq pos (match-end 0)
  6812. optionp (equal (char-after (match-beginning 0)) ?#)
  6813. cat (org-trim (match-string 2)))
  6814. (if optionp
  6815. (setq beg (point-at-bol) end (point-max))
  6816. (org-back-to-heading t)
  6817. (setq beg (point) end (org-end-of-subtree t t)))
  6818. (put-text-property beg end 'org-category cat)
  6819. (goto-char pos)))))))
  6820. ;;;; Link Stuff
  6821. ;;; Link abbreviations
  6822. (defun org-link-expand-abbrev (link)
  6823. "Apply replacements as defined in `org-link-abbrev-alist."
  6824. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6825. (let* ((key (match-string 1 link))
  6826. (as (or (assoc key org-link-abbrev-alist-local)
  6827. (assoc key org-link-abbrev-alist)))
  6828. (tag (and (match-end 2) (match-string 3 link)))
  6829. rpl)
  6830. (if (not as)
  6831. link
  6832. (setq rpl (cdr as))
  6833. (cond
  6834. ((symbolp rpl) (funcall rpl tag))
  6835. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6836. ((string-match "%h" rpl)
  6837. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6838. (t (concat rpl tag)))))
  6839. link))
  6840. ;;; Storing and inserting links
  6841. (defvar org-insert-link-history nil
  6842. "Minibuffer history for links inserted with `org-insert-link'.")
  6843. (defvar org-stored-links nil
  6844. "Contains the links stored with `org-store-link'.")
  6845. (defvar org-store-link-plist nil
  6846. "Plist with info about the most recently link created with `org-store-link'.")
  6847. (defvar org-link-protocols nil
  6848. "Link protocols added to Org-mode using `org-add-link-type'.")
  6849. (defvar org-store-link-functions nil
  6850. "List of functions that are called to create and store a link.
  6851. Each function will be called in turn until one returns a non-nil
  6852. value. Each function should check if it is responsible for creating
  6853. this link (for example by looking at the major mode).
  6854. If not, it must exit and return nil.
  6855. If yes, it should return a non-nil value after a calling
  6856. `org-store-link-props' with a list of properties and values.
  6857. Special properties are:
  6858. :type The link prefix. like \"http\". This must be given.
  6859. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6860. This is obligatory as well.
  6861. :description Optional default description for the second pair
  6862. of brackets in an Org-mode link. The user can still change
  6863. this when inserting this link into an Org-mode buffer.
  6864. In addition to these, any additional properties can be specified
  6865. and then used in remember templates.")
  6866. (defun org-add-link-type (type &optional follow export)
  6867. "Add TYPE to the list of `org-link-types'.
  6868. Re-compute all regular expressions depending on `org-link-types'
  6869. FOLLOW and EXPORT are two functions.
  6870. FOLLOW should take the link path as the single argument and do whatever
  6871. is necessary to follow the link, for example find a file or display
  6872. a mail message.
  6873. EXPORT should format the link path for export to one of the export formats.
  6874. It should be a function accepting three arguments:
  6875. path the path of the link, the text after the prefix (like \"http:\")
  6876. desc the description of the link, if any, nil if there was no description
  6877. format the export format, a symbol like `html' or `latex'.
  6878. The function may use the FORMAT information to return different values
  6879. depending on the format. The return value will be put literally into
  6880. the exported file.
  6881. Org-mode has a built-in default for exporting links. If you are happy with
  6882. this default, there is no need to define an export function for the link
  6883. type. For a simple example of an export function, see `org-bbdb.el'."
  6884. (add-to-list 'org-link-types type t)
  6885. (org-make-link-regexps)
  6886. (if (assoc type org-link-protocols)
  6887. (setcdr (assoc type org-link-protocols) (list follow export))
  6888. (push (list type follow export) org-link-protocols)))
  6889. (defvar org-agenda-buffer-name)
  6890. ;;;###autoload
  6891. (defun org-store-link (arg)
  6892. "\\<org-mode-map>Store an org-link to the current location.
  6893. This link is added to `org-stored-links' and can later be inserted
  6894. into an org-buffer with \\[org-insert-link].
  6895. For some link types, a prefix arg is interpreted:
  6896. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6897. For file links, arg negates `org-context-in-file-links'."
  6898. (interactive "P")
  6899. (org-load-modules-maybe)
  6900. (setq org-store-link-plist nil) ; reset
  6901. (let ((outline-regexp (org-get-limited-outline-regexp))
  6902. link cpltxt desc description search txt custom-id)
  6903. (cond
  6904. ((run-hook-with-args-until-success 'org-store-link-functions)
  6905. (setq link (plist-get org-store-link-plist :link)
  6906. desc (or (plist-get org-store-link-plist :description) link)))
  6907. ((equal (buffer-name) "*Org Edit Src Example*")
  6908. (let (label gc)
  6909. (while (or (not label)
  6910. (save-excursion
  6911. (save-restriction
  6912. (widen)
  6913. (goto-char (point-min))
  6914. (re-search-forward
  6915. (regexp-quote (format org-coderef-label-format label))
  6916. nil t))))
  6917. (when label (message "Label exists already") (sit-for 2))
  6918. (setq label (read-string "Code line label: " label)))
  6919. (end-of-line 1)
  6920. (setq link (format org-coderef-label-format label))
  6921. (setq gc (- 79 (length link)))
  6922. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6923. (insert link)
  6924. (setq link (concat "(" label ")") desc nil)))
  6925. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  6926. ;; We are in the agenda, link to referenced location
  6927. (let ((m (or (get-text-property (point) 'org-hd-marker)
  6928. (get-text-property (point) 'org-marker))))
  6929. (when m
  6930. (org-with-point-at m
  6931. (call-interactively 'org-store-link)))))
  6932. ((eq major-mode 'calendar-mode)
  6933. (let ((cd (calendar-cursor-to-date)))
  6934. (setq link
  6935. (format-time-string
  6936. (car org-time-stamp-formats)
  6937. (apply 'encode-time
  6938. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6939. nil nil nil))))
  6940. (org-store-link-props :type "calendar" :date cd)))
  6941. ((eq major-mode 'w3-mode)
  6942. (setq cpltxt (if (and (buffer-name)
  6943. (not (string-match "Untitled" (buffer-name))))
  6944. (buffer-name)
  6945. (url-view-url t))
  6946. link (org-make-link (url-view-url t)))
  6947. (org-store-link-props :type "w3" :url (url-view-url t)))
  6948. ((eq major-mode 'w3m-mode)
  6949. (setq cpltxt (or w3m-current-title w3m-current-url)
  6950. link (org-make-link w3m-current-url))
  6951. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6952. ((setq search (run-hook-with-args-until-success
  6953. 'org-create-file-search-functions))
  6954. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6955. "::" search))
  6956. (setq cpltxt (or description link)))
  6957. ((eq major-mode 'image-mode)
  6958. (setq cpltxt (concat "file:"
  6959. (abbreviate-file-name buffer-file-name))
  6960. link (org-make-link cpltxt))
  6961. (org-store-link-props :type "image" :file buffer-file-name))
  6962. ((eq major-mode 'dired-mode)
  6963. ;; link to the file in the current line
  6964. (let ((file (dired-get-filename nil t)))
  6965. (setq file (if file
  6966. (abbreviate-file-name
  6967. (expand-file-name (dired-get-filename nil t)))
  6968. ;; otherwise, no file so use current directory.
  6969. default-directory))
  6970. (setq cpltxt (concat "file:" file)
  6971. link (org-make-link cpltxt))))
  6972. ((and buffer-file-name (org-mode-p))
  6973. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6974. (cond
  6975. ((org-in-regexp "<<\\(.*?\\)>>")
  6976. (setq cpltxt
  6977. (concat "file:"
  6978. (abbreviate-file-name buffer-file-name)
  6979. "::" (match-string 1))
  6980. link (org-make-link cpltxt)))
  6981. ((and (featurep 'org-id)
  6982. (or (eq org-link-to-org-use-id t)
  6983. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6984. (interactive-p))
  6985. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6986. (interactive-p)
  6987. (not custom-id))
  6988. (and org-link-to-org-use-id
  6989. (condition-case nil
  6990. (org-entry-get nil "ID")
  6991. (error nil)))))
  6992. ;; We can make a link using the ID.
  6993. (setq link (condition-case nil
  6994. (prog1 (org-id-store-link)
  6995. (setq desc (plist-get org-store-link-plist
  6996. :description)))
  6997. (error
  6998. ;; probably before first headline, link to file only
  6999. (concat "file:"
  7000. (abbreviate-file-name buffer-file-name))))))
  7001. (t
  7002. ;; Just link to current headline
  7003. (setq cpltxt (concat "file:"
  7004. (abbreviate-file-name buffer-file-name)))
  7005. ;; Add a context search string
  7006. (when (org-xor org-context-in-file-links arg)
  7007. (setq txt (cond
  7008. ((org-on-heading-p) nil)
  7009. ((org-region-active-p)
  7010. (buffer-substring (region-beginning) (region-end)))
  7011. (t nil)))
  7012. (when (or (null txt) (string-match "\\S-" txt))
  7013. (setq cpltxt
  7014. (concat cpltxt "::"
  7015. (condition-case nil
  7016. (org-make-org-heading-search-string txt)
  7017. (error "")))
  7018. desc (or (nth 4 (ignore-errors
  7019. (org-heading-components))) "NONE"))))
  7020. (if (string-match "::\\'" cpltxt)
  7021. (setq cpltxt (substring cpltxt 0 -2)))
  7022. (setq link (org-make-link cpltxt)))))
  7023. ((buffer-file-name (buffer-base-buffer))
  7024. ;; Just link to this file here.
  7025. (setq cpltxt (concat "file:"
  7026. (abbreviate-file-name
  7027. (buffer-file-name (buffer-base-buffer)))))
  7028. ;; Add a context string
  7029. (when (org-xor org-context-in-file-links arg)
  7030. (setq txt (if (org-region-active-p)
  7031. (buffer-substring (region-beginning) (region-end))
  7032. (buffer-substring (point-at-bol) (point-at-eol))))
  7033. ;; Only use search option if there is some text.
  7034. (when (string-match "\\S-" txt)
  7035. (setq cpltxt
  7036. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7037. desc "NONE")))
  7038. (setq link (org-make-link cpltxt)))
  7039. ((interactive-p)
  7040. (error "Cannot link to a buffer which is not visiting a file"))
  7041. (t (setq link nil)))
  7042. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7043. (setq link (or link cpltxt)
  7044. desc (or desc cpltxt))
  7045. (if (equal desc "NONE") (setq desc nil))
  7046. (if (and (or (interactive-p) executing-kbd-macro) link)
  7047. (progn
  7048. (setq org-stored-links
  7049. (cons (list link desc) org-stored-links))
  7050. (message "Stored: %s" (or desc link))
  7051. (when custom-id
  7052. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7053. "::#" custom-id))
  7054. (setq org-stored-links
  7055. (cons (list link desc) org-stored-links))))
  7056. (and link (org-make-link-string link desc)))))
  7057. (defun org-store-link-props (&rest plist)
  7058. "Store link properties, extract names and addresses."
  7059. (let (x adr)
  7060. (when (setq x (plist-get plist :from))
  7061. (setq adr (mail-extract-address-components x))
  7062. (setq plist (plist-put plist :fromname (car adr)))
  7063. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7064. (when (setq x (plist-get plist :to))
  7065. (setq adr (mail-extract-address-components x))
  7066. (setq plist (plist-put plist :toname (car adr)))
  7067. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7068. (let ((from (plist-get plist :from))
  7069. (to (plist-get plist :to)))
  7070. (when (and from to org-from-is-user-regexp)
  7071. (setq plist
  7072. (plist-put plist :fromto
  7073. (if (string-match org-from-is-user-regexp from)
  7074. (concat "to %t")
  7075. (concat "from %f"))))))
  7076. (setq org-store-link-plist plist))
  7077. (defun org-add-link-props (&rest plist)
  7078. "Add these properties to the link property list."
  7079. (let (key value)
  7080. (while plist
  7081. (setq key (pop plist) value (pop plist))
  7082. (setq org-store-link-plist
  7083. (plist-put org-store-link-plist key value)))))
  7084. (defun org-email-link-description (&optional fmt)
  7085. "Return the description part of an email link.
  7086. This takes information from `org-store-link-plist' and formats it
  7087. according to FMT (default from `org-email-link-description-format')."
  7088. (setq fmt (or fmt org-email-link-description-format))
  7089. (let* ((p org-store-link-plist)
  7090. (to (plist-get p :toaddress))
  7091. (from (plist-get p :fromaddress))
  7092. (table
  7093. (list
  7094. (cons "%c" (plist-get p :fromto))
  7095. (cons "%F" (plist-get p :from))
  7096. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7097. (cons "%T" (plist-get p :to))
  7098. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7099. (cons "%s" (plist-get p :subject))
  7100. (cons "%m" (plist-get p :message-id)))))
  7101. (when (string-match "%c" fmt)
  7102. ;; Check if the user wrote this message
  7103. (if (and org-from-is-user-regexp from to
  7104. (save-match-data (string-match org-from-is-user-regexp from)))
  7105. (setq fmt (replace-match "to %t" t t fmt))
  7106. (setq fmt (replace-match "from %f" t t fmt))))
  7107. (org-replace-escapes fmt table)))
  7108. (defun org-make-org-heading-search-string (&optional string heading)
  7109. "Make search string for STRING or current headline."
  7110. (interactive)
  7111. (let ((s (or string (org-get-heading))))
  7112. (unless (and string (not heading))
  7113. ;; We are using a headline, clean up garbage in there.
  7114. (if (string-match org-todo-regexp s)
  7115. (setq s (replace-match "" t t s)))
  7116. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  7117. (setq s (replace-match "" t t s)))
  7118. (setq s (org-trim s))
  7119. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7120. org-comment-string "\\)") s)
  7121. (setq s (replace-match "" t t s)))
  7122. (while (string-match org-ts-regexp s)
  7123. (setq s (replace-match "" t t s))))
  7124. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  7125. (setq s (replace-match " " t t s)))
  7126. (or string (setq s (concat "*" s))) ; Add * for headlines
  7127. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7128. (defun org-make-link (&rest strings)
  7129. "Concatenate STRINGS."
  7130. (apply 'concat strings))
  7131. (defun org-make-link-string (link &optional description)
  7132. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7133. (unless (string-match "\\S-" link)
  7134. (error "Empty link"))
  7135. (when (and description
  7136. (stringp description)
  7137. (not (string-match "\\S-" description)))
  7138. (setq description nil))
  7139. (when (stringp description)
  7140. ;; Remove brackets from the description, they are fatal.
  7141. (while (string-match "\\[" description)
  7142. (setq description (replace-match "{" t t description)))
  7143. (while (string-match "\\]" description)
  7144. (setq description (replace-match "}" t t description))))
  7145. (when (equal (org-link-escape link) description)
  7146. ;; No description needed, it is identical
  7147. (setq description nil))
  7148. (when (and (not description)
  7149. (not (equal link (org-link-escape link))))
  7150. (setq description (org-extract-attributes link)))
  7151. (concat "[[" (org-link-escape link) "]"
  7152. (if description (concat "[" description "]") "")
  7153. "]"))
  7154. (defconst org-link-escape-chars
  7155. '((?\ . "%20")
  7156. (?\[ . "%5B")
  7157. (?\] . "%5D")
  7158. (?\340 . "%E0") ; `a
  7159. (?\342 . "%E2") ; ^a
  7160. (?\347 . "%E7") ; ,c
  7161. (?\350 . "%E8") ; `e
  7162. (?\351 . "%E9") ; 'e
  7163. (?\352 . "%EA") ; ^e
  7164. (?\356 . "%EE") ; ^i
  7165. (?\364 . "%F4") ; ^o
  7166. (?\371 . "%F9") ; `u
  7167. (?\373 . "%FB") ; ^u
  7168. (?\; . "%3B")
  7169. ;; (?? . "%3F")
  7170. (?= . "%3D")
  7171. (?+ . "%2B")
  7172. )
  7173. "Association list of escapes for some characters problematic in links.
  7174. This is the list that is used for internal purposes.")
  7175. (defvar org-url-encoding-use-url-hexify nil)
  7176. (defconst org-link-escape-chars-browser
  7177. '((?\ . "%20")) ; 32 for the SPC char
  7178. "Association list of escapes for some characters problematic in links.
  7179. This is the list that is used before handing over to the browser.")
  7180. (defun org-link-escape (text &optional table)
  7181. "Escape characters in TEXT that are problematic for links."
  7182. (if (and org-url-encoding-use-url-hexify (not table))
  7183. (url-hexify-string text)
  7184. (setq table (or table org-link-escape-chars))
  7185. (when text
  7186. (let ((re (mapconcat (lambda (x) (regexp-quote
  7187. (char-to-string (car x))))
  7188. table "\\|")))
  7189. (while (string-match re text)
  7190. (setq text
  7191. (replace-match
  7192. (cdr (assoc (string-to-char (match-string 0 text))
  7193. table))
  7194. t t text)))
  7195. text))))
  7196. (defun org-link-unescape (text &optional table)
  7197. "Reverse the action of `org-link-escape'."
  7198. (if (and org-url-encoding-use-url-hexify (not table))
  7199. (url-unhex-string text)
  7200. (setq table (or table org-link-escape-chars))
  7201. (when text
  7202. (let ((case-fold-search t)
  7203. (re (mapconcat (lambda (x) (regexp-quote (downcase (cdr x))))
  7204. table "\\|")))
  7205. (while (string-match re text)
  7206. (setq text
  7207. (replace-match
  7208. (char-to-string (car (rassoc (upcase (match-string 0 text))
  7209. table)))
  7210. t t text)))
  7211. text))))
  7212. (defun org-xor (a b)
  7213. "Exclusive or."
  7214. (if a (not b) b))
  7215. (defun org-fixup-message-id-for-http (s)
  7216. "Replace special characters in a message id, so it can be used in an http query."
  7217. (while (string-match "<" s)
  7218. (setq s (replace-match "%3C" t t s)))
  7219. (while (string-match ">" s)
  7220. (setq s (replace-match "%3E" t t s)))
  7221. (while (string-match "@" s)
  7222. (setq s (replace-match "%40" t t s)))
  7223. s)
  7224. ;;;###autoload
  7225. (defun org-insert-link-global ()
  7226. "Insert a link like Org-mode does.
  7227. This command can be called in any mode to insert a link in Org-mode syntax."
  7228. (interactive)
  7229. (org-load-modules-maybe)
  7230. (org-run-like-in-org-mode 'org-insert-link))
  7231. (defun org-insert-link (&optional complete-file link-location)
  7232. "Insert a link. At the prompt, enter the link.
  7233. Completion can be used to insert any of the link protocol prefixes like
  7234. http or ftp in use.
  7235. The history can be used to select a link previously stored with
  7236. `org-store-link'. When the empty string is entered (i.e. if you just
  7237. press RET at the prompt), the link defaults to the most recently
  7238. stored link. As SPC triggers completion in the minibuffer, you need to
  7239. use M-SPC or C-q SPC to force the insertion of a space character.
  7240. You will also be prompted for a description, and if one is given, it will
  7241. be displayed in the buffer instead of the link.
  7242. If there is already a link at point, this command will allow you to edit link
  7243. and description parts.
  7244. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7245. be selected using completion. The path to the file will be relative to the
  7246. current directory if the file is in the current directory or a subdirectory.
  7247. Otherwise, the link will be the absolute path as completed in the minibuffer
  7248. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7249. option `org-link-file-path-type'.
  7250. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7251. the current directory or below.
  7252. With three \\[universal-argument] prefixes, negate the meaning of
  7253. `org-keep-stored-link-after-insertion'.
  7254. If `org-make-link-description-function' is non-nil, this function will be
  7255. called with the link target, and the result will be the default
  7256. link description.
  7257. If the LINK-LOCATION parameter is non-nil, this value will be
  7258. used as the link location instead of reading one interactively."
  7259. (interactive "P")
  7260. (let* ((wcf (current-window-configuration))
  7261. (region (if (org-region-active-p)
  7262. (buffer-substring (region-beginning) (region-end))))
  7263. (remove (and region (list (region-beginning) (region-end))))
  7264. (desc region)
  7265. tmphist ; byte-compile incorrectly complains about this
  7266. (link link-location)
  7267. entry file all-prefixes)
  7268. (cond
  7269. (link-location) ; specified by arg, just use it.
  7270. ((org-in-regexp org-bracket-link-regexp 1)
  7271. ;; We do have a link at point, and we are going to edit it.
  7272. (setq remove (list (match-beginning 0) (match-end 0)))
  7273. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7274. (setq link (read-string "Link: "
  7275. (org-link-unescape
  7276. (org-match-string-no-properties 1)))))
  7277. ((or (org-in-regexp org-angle-link-re)
  7278. (org-in-regexp org-plain-link-re))
  7279. ;; Convert to bracket link
  7280. (setq remove (list (match-beginning 0) (match-end 0))
  7281. link (read-string "Link: "
  7282. (org-remove-angle-brackets (match-string 0)))))
  7283. ((member complete-file '((4) (16)))
  7284. ;; Completing read for file names.
  7285. (setq link (org-file-complete-link complete-file)))
  7286. (t
  7287. ;; Read link, with completion for stored links.
  7288. (with-output-to-temp-buffer "*Org Links*"
  7289. (princ "Insert a link.
  7290. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7291. (when org-stored-links
  7292. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7293. (princ (mapconcat
  7294. (lambda (x)
  7295. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7296. (reverse org-stored-links) "\n"))))
  7297. (let ((cw (selected-window)))
  7298. (select-window (get-buffer-window "*Org Links*"))
  7299. (setq truncate-lines t)
  7300. (unless (pos-visible-in-window-p (point-max))
  7301. (org-fit-window-to-buffer))
  7302. (and (window-live-p cw) (select-window cw)))
  7303. ;; Fake a link history, containing the stored links.
  7304. (setq tmphist (append (mapcar 'car org-stored-links)
  7305. org-insert-link-history))
  7306. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7307. (mapcar 'car org-link-abbrev-alist)
  7308. org-link-types))
  7309. (unwind-protect
  7310. (progn
  7311. (setq link
  7312. (let ((org-completion-use-ido nil)
  7313. (org-completion-use-iswitchb nil))
  7314. (org-completing-read
  7315. "Link: "
  7316. (append
  7317. (mapcar (lambda (x) (list (concat x ":")))
  7318. all-prefixes)
  7319. (mapcar 'car org-stored-links))
  7320. nil nil nil
  7321. 'tmphist
  7322. (car (car org-stored-links)))))
  7323. (if (not (string-match "\\S-" link))
  7324. (error "No link selected"))
  7325. (if (or (member link all-prefixes)
  7326. (and (equal ":" (substring link -1))
  7327. (member (substring link 0 -1) all-prefixes)
  7328. (setq link (substring link 0 -1))))
  7329. (setq link (org-link-try-special-completion link))))
  7330. (set-window-configuration wcf)
  7331. (kill-buffer "*Org Links*"))
  7332. (setq entry (assoc link org-stored-links))
  7333. (or entry (push link org-insert-link-history))
  7334. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7335. (not org-keep-stored-link-after-insertion))
  7336. (setq org-stored-links (delq (assoc link org-stored-links)
  7337. org-stored-links)))
  7338. (setq desc (or desc (nth 1 entry)))))
  7339. (if (string-match org-plain-link-re link)
  7340. ;; URL-like link, normalize the use of angular brackets.
  7341. (setq link (org-make-link (org-remove-angle-brackets link))))
  7342. ;; Check if we are linking to the current file with a search option
  7343. ;; If yes, simplify the link by using only the search option.
  7344. (when (and buffer-file-name
  7345. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7346. (let* ((path (match-string 1 link))
  7347. (case-fold-search nil)
  7348. (search (match-string 2 link)))
  7349. (save-match-data
  7350. (if (equal (file-truename buffer-file-name) (file-truename path))
  7351. ;; We are linking to this same file, with a search option
  7352. (setq link search)))))
  7353. ;; Check if we can/should use a relative path. If yes, simplify the link
  7354. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  7355. (let* ((type (match-string 1 link))
  7356. (path (match-string 2 link))
  7357. (origpath path)
  7358. (case-fold-search nil))
  7359. (cond
  7360. ((or (eq org-link-file-path-type 'absolute)
  7361. (equal complete-file '(16)))
  7362. (setq path (abbreviate-file-name (expand-file-name path))))
  7363. ((eq org-link-file-path-type 'noabbrev)
  7364. (setq path (expand-file-name path)))
  7365. ((eq org-link-file-path-type 'relative)
  7366. (setq path (file-relative-name path)))
  7367. (t
  7368. (save-match-data
  7369. (if (string-match (concat "^" (regexp-quote
  7370. (file-name-as-directory
  7371. (expand-file-name "."))))
  7372. (expand-file-name path))
  7373. ;; We are linking a file with relative path name.
  7374. (setq path (substring (expand-file-name path)
  7375. (match-end 0)))
  7376. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7377. (setq link (concat type path))
  7378. (if (equal desc origpath)
  7379. (setq desc path))))
  7380. (if org-make-link-description-function
  7381. (setq desc (funcall org-make-link-description-function link desc)))
  7382. (setq desc (read-string "Description: " desc))
  7383. (unless (string-match "\\S-" desc) (setq desc nil))
  7384. (if remove (apply 'delete-region remove))
  7385. (insert (org-make-link-string link desc))))
  7386. (defun org-link-try-special-completion (type)
  7387. "If there is completion support for link type TYPE, offer it."
  7388. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7389. (if (functionp fun)
  7390. (funcall fun)
  7391. (read-string "Link (no completion support): " (concat type ":")))))
  7392. (defun org-file-complete-link (&optional arg)
  7393. "Create a file link using completion."
  7394. (let (file link)
  7395. (setq file (read-file-name "File: "))
  7396. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7397. (pwd1 (file-name-as-directory (abbreviate-file-name
  7398. (expand-file-name ".")))))
  7399. (cond
  7400. ((equal arg '(16))
  7401. (setq link (org-make-link
  7402. "file:"
  7403. (abbreviate-file-name (expand-file-name file)))))
  7404. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7405. (setq link (org-make-link "file:" (match-string 1 file))))
  7406. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7407. (expand-file-name file))
  7408. (setq link (org-make-link
  7409. "file:" (match-string 1 (expand-file-name file)))))
  7410. (t (setq link (org-make-link "file:" file)))))
  7411. link))
  7412. (defun org-completing-read (&rest args)
  7413. "Completing-read with SPACE being a normal character."
  7414. (let ((minibuffer-local-completion-map
  7415. (copy-keymap minibuffer-local-completion-map)))
  7416. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7417. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7418. (apply 'org-icompleting-read args)))
  7419. (defun org-completing-read-no-i (&rest args)
  7420. (let (org-completion-use-ido org-completion-use-iswitchb)
  7421. (apply 'org-completing-read args)))
  7422. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7423. "Use iswitch as a completing-read replacement to choose from choices.
  7424. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7425. from."
  7426. (let* ((iswitchb-use-virtual-buffers nil)
  7427. (iswitchb-make-buflist-hook
  7428. (lambda ()
  7429. (setq iswitchb-temp-buflist choices))))
  7430. (iswitchb-read-buffer prompt)))
  7431. (defun org-icompleting-read (&rest args)
  7432. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7433. (org-without-partial-completion
  7434. (if (and org-completion-use-ido
  7435. (fboundp 'ido-completing-read)
  7436. (boundp 'ido-mode) ido-mode
  7437. (listp (second args)))
  7438. (let ((ido-enter-matching-directory nil))
  7439. (apply 'ido-completing-read (concat (car args))
  7440. (if (consp (car (nth 1 args)))
  7441. (mapcar (lambda (x) (car x)) (nth 1 args))
  7442. (nth 1 args))
  7443. (cddr args)))
  7444. (if (and org-completion-use-iswitchb
  7445. (boundp 'iswitchb-mode) iswitchb-mode
  7446. (listp (second args)))
  7447. (apply 'org-iswitchb-completing-read (concat (car args))
  7448. (if (consp (car (nth 1 args)))
  7449. (mapcar (lambda (x) (car x)) (nth 1 args))
  7450. (nth 1 args))
  7451. (cddr args))
  7452. (apply 'completing-read args)))))
  7453. (defun org-extract-attributes (s)
  7454. "Extract the attributes cookie from a string and set as text property."
  7455. (let (a attr (start 0) key value)
  7456. (save-match-data
  7457. (when (string-match "{{\\([^}]+\\)}}$" s)
  7458. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7459. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7460. (setq key (match-string 1 a) value (match-string 2 a)
  7461. start (match-end 0)
  7462. attr (plist-put attr (intern key) value))))
  7463. (org-add-props s nil 'org-attr attr))
  7464. s))
  7465. (defun org-extract-attributes-from-string (tag)
  7466. (let (key value attr)
  7467. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7468. (setq key (match-string 1 tag) value (match-string 2 tag)
  7469. tag (replace-match "" t t tag)
  7470. attr (plist-put attr (intern key) value)))
  7471. (cons tag attr)))
  7472. (defun org-attributes-to-string (plist)
  7473. "Format a property list into an HTML attribute list."
  7474. (let ((s "") key value)
  7475. (while plist
  7476. (setq key (pop plist) value (pop plist))
  7477. (and value
  7478. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7479. s))
  7480. ;;; Opening/following a link
  7481. (defvar org-link-search-failed nil)
  7482. (defvar org-open-link-functions nil
  7483. "Hook for functions finding a plain text link.
  7484. These functions must take a single argument, the link content.
  7485. They will be called for links that look like [[link text][description]]
  7486. when LINK TEXT does not have a protocol like \"http:\" and does not look
  7487. like a filename (e.g. \"./blue.png\").
  7488. These functions will be called *before* Org attempts to resolve the
  7489. link by doing text searches in the current buffer - so if you want a
  7490. link \"[[target]]\" to still find \"<<target>>\", your function should
  7491. handle this as a special case.
  7492. When the function does handle the link, it must return a non-nil value.
  7493. If it decides that it is not responsible for this link, it must return
  7494. nil to indicate that that Org-mode can continue with other options
  7495. like exact and fuzzy text search.")
  7496. (defun org-next-link ()
  7497. "Move forward to the next link.
  7498. If the link is in hidden text, expose it."
  7499. (interactive)
  7500. (when (and org-link-search-failed (eq this-command last-command))
  7501. (goto-char (point-min))
  7502. (message "Link search wrapped back to beginning of buffer"))
  7503. (setq org-link-search-failed nil)
  7504. (let* ((pos (point))
  7505. (ct (org-context))
  7506. (a (assoc :link ct)))
  7507. (if a (goto-char (nth 2 a)))
  7508. (if (re-search-forward org-any-link-re nil t)
  7509. (progn
  7510. (goto-char (match-beginning 0))
  7511. (if (org-invisible-p) (org-show-context)))
  7512. (goto-char pos)
  7513. (setq org-link-search-failed t)
  7514. (error "No further link found"))))
  7515. (defun org-previous-link ()
  7516. "Move backward to the previous link.
  7517. If the link is in hidden text, expose it."
  7518. (interactive)
  7519. (when (and org-link-search-failed (eq this-command last-command))
  7520. (goto-char (point-max))
  7521. (message "Link search wrapped back to end of buffer"))
  7522. (setq org-link-search-failed nil)
  7523. (let* ((pos (point))
  7524. (ct (org-context))
  7525. (a (assoc :link ct)))
  7526. (if a (goto-char (nth 1 a)))
  7527. (if (re-search-backward org-any-link-re nil t)
  7528. (progn
  7529. (goto-char (match-beginning 0))
  7530. (if (org-invisible-p) (org-show-context)))
  7531. (goto-char pos)
  7532. (setq org-link-search-failed t)
  7533. (error "No further link found"))))
  7534. (defun org-translate-link (s)
  7535. "Translate a link string if a translation function has been defined."
  7536. (if (and org-link-translation-function
  7537. (fboundp org-link-translation-function)
  7538. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7539. (progn
  7540. (setq s (funcall org-link-translation-function
  7541. (match-string 1) (match-string 2)))
  7542. (concat (car s) ":" (cdr s)))
  7543. s))
  7544. (defun org-translate-link-from-planner (type path)
  7545. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7546. This is still an experimental function, your mileage may vary."
  7547. (cond
  7548. ((member type '("http" "https" "news" "ftp"))
  7549. ;; standard Internet links are the same.
  7550. nil)
  7551. ((and (equal type "irc") (string-match "^//" path))
  7552. ;; Planner has two / at the beginning of an irc link, we have 1.
  7553. ;; We should have zero, actually....
  7554. (setq path (substring path 1)))
  7555. ((and (equal type "lisp") (string-match "^/" path))
  7556. ;; Planner has a slash, we do not.
  7557. (setq type "elisp" path (substring path 1)))
  7558. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7559. ;; A typical message link. Planner has the id after the final slash,
  7560. ;; we separate it with a hash mark
  7561. (setq path (concat (match-string 1 path) "#"
  7562. (org-remove-angle-brackets (match-string 2 path)))))
  7563. )
  7564. (cons type path))
  7565. (defun org-find-file-at-mouse (ev)
  7566. "Open file link or URL at mouse."
  7567. (interactive "e")
  7568. (mouse-set-point ev)
  7569. (org-open-at-point 'in-emacs))
  7570. (defun org-open-at-mouse (ev)
  7571. "Open file link or URL at mouse."
  7572. (interactive "e")
  7573. (mouse-set-point ev)
  7574. (if (eq major-mode 'org-agenda-mode)
  7575. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7576. (org-open-at-point))
  7577. (defvar org-window-config-before-follow-link nil
  7578. "The window configuration before following a link.
  7579. This is saved in case the need arises to restore it.")
  7580. (defvar org-open-link-marker (make-marker)
  7581. "Marker pointing to the location where `org-open-at-point; was called.")
  7582. ;;;###autoload
  7583. (defun org-open-at-point-global ()
  7584. "Follow a link like Org-mode does.
  7585. This command can be called in any mode to follow a link that has
  7586. Org-mode syntax."
  7587. (interactive)
  7588. (org-run-like-in-org-mode 'org-open-at-point))
  7589. ;;;###autoload
  7590. (defun org-open-link-from-string (s &optional arg reference-buffer)
  7591. "Open a link in the string S, as if it was in Org-mode."
  7592. (interactive "sLink: \nP")
  7593. (let ((reference-buffer (or reference-buffer (current-buffer))))
  7594. (with-temp-buffer
  7595. (let ((org-inhibit-startup t))
  7596. (org-mode)
  7597. (insert s)
  7598. (goto-char (point-min))
  7599. (when reference-buffer
  7600. (setq org-link-abbrev-alist-local
  7601. (with-current-buffer reference-buffer
  7602. org-link-abbrev-alist-local)))
  7603. (org-open-at-point arg reference-buffer)))))
  7604. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7605. "Open link at or after point.
  7606. If there is no link at point, this function will search forward up to
  7607. the end of the current line.
  7608. Normally, files will be opened by an appropriate application. If the
  7609. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7610. With a double prefix argument, try to open outside of Emacs, in the
  7611. application the system uses for this file type."
  7612. (interactive "P")
  7613. (org-load-modules-maybe)
  7614. (move-marker org-open-link-marker (point))
  7615. (setq org-window-config-before-follow-link (current-window-configuration))
  7616. (org-remove-occur-highlights nil nil t)
  7617. (cond
  7618. ((and (org-on-heading-p)
  7619. (not (org-in-regexp
  7620. (concat org-plain-link-re "\\|"
  7621. org-bracket-link-regexp "\\|"
  7622. org-angle-link-re "\\|"
  7623. "[ \t]:[^ \t\n]+:[ \t]*$")))
  7624. (not (get-text-property (point) 'org-linked-text)))
  7625. (or (org-offer-links-in-entry in-emacs)
  7626. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  7627. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7628. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7629. (org-footnote-action))
  7630. (t
  7631. (let (type path link line search (pos (point)))
  7632. (catch 'match
  7633. (save-excursion
  7634. (skip-chars-forward "^]\n\r")
  7635. (when (org-in-regexp org-bracket-link-regexp 1)
  7636. (setq link (org-extract-attributes
  7637. (org-link-unescape (org-match-string-no-properties 1))))
  7638. (while (string-match " *\n *" link)
  7639. (setq link (replace-match " " t t link)))
  7640. (setq link (org-link-expand-abbrev link))
  7641. (cond
  7642. ((or (file-name-absolute-p link)
  7643. (string-match "^\\.\\.?/" link))
  7644. (setq type "file" path link))
  7645. ((string-match org-link-re-with-space3 link)
  7646. (setq type (match-string 1 link) path (match-string 2 link)))
  7647. (t (setq type "thisfile" path link)))
  7648. (throw 'match t)))
  7649. (when (get-text-property (point) 'org-linked-text)
  7650. (setq type "thisfile"
  7651. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7652. (1+ (point)) (point))
  7653. path (buffer-substring
  7654. (previous-single-property-change pos 'org-linked-text)
  7655. (next-single-property-change pos 'org-linked-text)))
  7656. (throw 'match t))
  7657. (save-excursion
  7658. (when (or (org-in-regexp org-angle-link-re)
  7659. (org-in-regexp org-plain-link-re))
  7660. (setq type (match-string 1) path (match-string 2))
  7661. (throw 'match t)))
  7662. (save-excursion
  7663. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7664. (setq type "tags"
  7665. path (match-string 1))
  7666. (while (string-match ":" path)
  7667. (setq path (replace-match "+" t t path)))
  7668. (throw 'match t)))
  7669. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7670. (setq type "tree-match"
  7671. path (match-string 1))
  7672. (throw 'match t)))
  7673. (unless path
  7674. (error "No link found"))
  7675. ;; switch back to reference buffer
  7676. ;; needed when if called in a temporary buffer through
  7677. ;; org-open-link-from-string
  7678. (with-current-buffer (or reference-buffer (current-buffer))
  7679. ;; Remove any trailing spaces in path
  7680. (if (string-match " +\\'" path)
  7681. (setq path (replace-match "" t t path)))
  7682. (if (and org-link-translation-function
  7683. (fboundp org-link-translation-function))
  7684. ;; Check if we need to translate the link
  7685. (let ((tmp (funcall org-link-translation-function type path)))
  7686. (setq type (car tmp) path (cdr tmp))))
  7687. (cond
  7688. ((assoc type org-link-protocols)
  7689. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7690. ((equal type "mailto")
  7691. (let ((cmd (car org-link-mailto-program))
  7692. (args (cdr org-link-mailto-program)) args1
  7693. (address path) (subject "") a)
  7694. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7695. (setq address (match-string 1 path)
  7696. subject (org-link-escape (match-string 2 path))))
  7697. (while args
  7698. (cond
  7699. ((not (stringp (car args))) (push (pop args) args1))
  7700. (t (setq a (pop args))
  7701. (if (string-match "%a" a)
  7702. (setq a (replace-match address t t a)))
  7703. (if (string-match "%s" a)
  7704. (setq a (replace-match subject t t a)))
  7705. (push a args1))))
  7706. (apply cmd (nreverse args1))))
  7707. ((member type '("http" "https" "ftp" "news"))
  7708. (browse-url (concat type ":" (org-link-escape
  7709. path org-link-escape-chars-browser))))
  7710. ((member type '("message"))
  7711. (browse-url (concat type ":" path)))
  7712. ((string= type "tags")
  7713. (org-tags-view in-emacs path))
  7714. ((string= type "tree-match")
  7715. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7716. ((string= type "file")
  7717. (if (string-match "::\\([0-9]+\\)\\'" path)
  7718. (setq line (string-to-number (match-string 1 path))
  7719. path (substring path 0 (match-beginning 0)))
  7720. (if (string-match "::\\(.+\\)\\'" path)
  7721. (setq search (match-string 1 path)
  7722. path (substring path 0 (match-beginning 0)))))
  7723. (if (string-match "[*?{]" (file-name-nondirectory path))
  7724. (dired path)
  7725. (org-open-file path in-emacs line search)))
  7726. ((string= type "news")
  7727. (require 'org-gnus)
  7728. (org-gnus-follow-link path))
  7729. ((string= type "shell")
  7730. (let ((cmd path))
  7731. (if (or (not org-confirm-shell-link-function)
  7732. (funcall org-confirm-shell-link-function
  7733. (format "Execute \"%s\" in shell? "
  7734. (org-add-props cmd nil
  7735. 'face 'org-warning))))
  7736. (progn
  7737. (message "Executing %s" cmd)
  7738. (shell-command cmd))
  7739. (error "Abort"))))
  7740. ((string= type "elisp")
  7741. (let ((cmd path))
  7742. (if (or (not org-confirm-elisp-link-function)
  7743. (funcall org-confirm-elisp-link-function
  7744. (format "Execute \"%s\" as elisp? "
  7745. (org-add-props cmd nil
  7746. 'face 'org-warning))))
  7747. (message "%s => %s" cmd
  7748. (if (equal (string-to-char cmd) ?\()
  7749. (eval (read cmd))
  7750. (call-interactively (read cmd))))
  7751. (error "Abort"))))
  7752. ((and (string= type "thisfile")
  7753. (run-hook-with-args-until-success
  7754. 'org-open-link-functions path)))
  7755. ((string= type "thisfile")
  7756. (if in-emacs
  7757. (switch-to-buffer-other-window
  7758. (org-get-buffer-for-internal-link (current-buffer)))
  7759. (org-mark-ring-push))
  7760. (let ((cmd `(org-link-search
  7761. ,path
  7762. ,(cond ((equal in-emacs '(4)) 'occur)
  7763. ((equal in-emacs '(16)) 'org-occur)
  7764. (t nil))
  7765. ,pos)))
  7766. (condition-case nil (eval cmd)
  7767. (error (progn (widen) (eval cmd))))))
  7768. (t
  7769. (browse-url-at-point)))))))
  7770. (move-marker org-open-link-marker nil)
  7771. (run-hook-with-args 'org-follow-link-hook))
  7772. (defun org-offer-links-in-entry (&optional nth zero)
  7773. "Offer links in the current entry and follow the selected link.
  7774. If there is only one link, follow it immediately as well.
  7775. If NTH is an integer, immediately pick the NTH link found.
  7776. If ZERO is a string, check also this string for a link, and if
  7777. there is one, offer it as link number zero."
  7778. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  7779. "\\(" org-angle-link-re "\\)\\|"
  7780. "\\(" org-plain-link-re "\\)"))
  7781. (cnt ?0)
  7782. (in-emacs (if (integerp nth) nil nth))
  7783. have-zero end links link c)
  7784. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  7785. (push (match-string 0 zero) links)
  7786. (setq cnt (1- cnt) have-zero t))
  7787. (save-excursion
  7788. (org-back-to-heading t)
  7789. (setq end (save-excursion (outline-next-heading) (point)))
  7790. (while (re-search-forward re end t)
  7791. (push (match-string 0) links))
  7792. (setq links (org-uniquify (reverse links))))
  7793. (cond
  7794. ((null links)
  7795. (message "No links"))
  7796. ((equal (length links) 1)
  7797. (setq link (list (car links))))
  7798. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  7799. (setq link (nth (if have-zero nth (1- nth)) links)))
  7800. (t ; we have to select a link
  7801. (save-excursion
  7802. (save-window-excursion
  7803. (delete-other-windows)
  7804. (with-output-to-temp-buffer "*Select Link*"
  7805. (mapc (lambda (l)
  7806. (if (not (string-match org-bracket-link-regexp l))
  7807. (princ (format "[%c] %s\n" (incf cnt)
  7808. (org-remove-angle-brackets l)))
  7809. (if (match-end 3)
  7810. (princ (format "[%c] %s (%s)\n" (incf cnt)
  7811. (match-string 3 l) (match-string 1 l)))
  7812. (princ (format "[%c] %s\n" (incf cnt)
  7813. (match-string 1 l))))))
  7814. links))
  7815. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  7816. (message "Select link to open, RET to open all:")
  7817. (setq c (read-char-exclusive))
  7818. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  7819. (when (equal c ?q) (error "Abort"))
  7820. (if (equal c ?\C-m)
  7821. (setq link links)
  7822. (setq nth (- c ?0))
  7823. (if have-zero (setq nth (1+ nth)))
  7824. (unless (and (integerp nth) (>= (length links) nth))
  7825. (error "Invalid link selection"))
  7826. (setq link (list (nth (1- nth) links))))))
  7827. (if link
  7828. (let ((buf (current-buffer)))
  7829. (dolist (l link)
  7830. (org-open-link-from-string l in-emacs buf))
  7831. t)
  7832. nil)))
  7833. ;; Add special file links that specify the way of opening
  7834. (org-add-link-type "file+sys" 'org-open-file-with-system)
  7835. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  7836. (defun org-open-file-with-system (path)
  7837. "Open file at PATH using the system way of opeing it."
  7838. (org-open-file path 'system))
  7839. (defun org-open-file-with-emacs (path)
  7840. "Open file at PATH in emacs."
  7841. (org-open-file path 'emacs))
  7842. (defun org-remove-file-link-modifiers ()
  7843. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  7844. (goto-char (point-min))
  7845. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  7846. (org-if-unprotected
  7847. (replace-match "file:" t t))))
  7848. (eval-after-load "org-exp"
  7849. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  7850. 'org-remove-file-link-modifiers))
  7851. ;;;; Time estimates
  7852. (defun org-get-effort (&optional pom)
  7853. "Get the effort estimate for the current entry."
  7854. (org-entry-get pom org-effort-property))
  7855. ;;; File search
  7856. (defvar org-create-file-search-functions nil
  7857. "List of functions to construct the right search string for a file link.
  7858. These functions are called in turn with point at the location to
  7859. which the link should point.
  7860. A function in the hook should first test if it would like to
  7861. handle this file type, for example by checking the major-mode or
  7862. the file extension. If it decides not to handle this file, it
  7863. should just return nil to give other functions a chance. If it
  7864. does handle the file, it must return the search string to be used
  7865. when following the link. The search string will be part of the
  7866. file link, given after a double colon, and `org-open-at-point'
  7867. will automatically search for it. If special measures must be
  7868. taken to make the search successful, another function should be
  7869. added to the companion hook `org-execute-file-search-functions',
  7870. which see.
  7871. A function in this hook may also use `setq' to set the variable
  7872. `description' to provide a suggestion for the descriptive text to
  7873. be used for this link when it gets inserted into an Org-mode
  7874. buffer with \\[org-insert-link].")
  7875. (defvar org-execute-file-search-functions nil
  7876. "List of functions to execute a file search triggered by a link.
  7877. Functions added to this hook must accept a single argument, the
  7878. search string that was part of the file link, the part after the
  7879. double colon. The function must first check if it would like to
  7880. handle this search, for example by checking the major-mode or the
  7881. file extension. If it decides not to handle this search, it
  7882. should just return nil to give other functions a chance. If it
  7883. does handle the search, it must return a non-nil value to keep
  7884. other functions from trying.
  7885. Each function can access the current prefix argument through the
  7886. variable `current-prefix-argument'. Note that a single prefix is
  7887. used to force opening a link in Emacs, so it may be good to only
  7888. use a numeric or double prefix to guide the search function.
  7889. In case this is needed, a function in this hook can also restore
  7890. the window configuration before `org-open-at-point' was called using:
  7891. (set-window-configuration org-window-config-before-follow-link)")
  7892. (defun org-link-search (s &optional type avoid-pos)
  7893. "Search for a link search option.
  7894. If S is surrounded by forward slashes, it is interpreted as a
  7895. regular expression. In org-mode files, this will create an `org-occur'
  7896. sparse tree. In ordinary files, `occur' will be used to list matches.
  7897. If the current buffer is in `dired-mode', grep will be used to search
  7898. in all files. If AVOID-POS is given, ignore matches near that position."
  7899. (let ((case-fold-search t)
  7900. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7901. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7902. (append '(("") (" ") ("\t") ("\n"))
  7903. org-emphasis-alist)
  7904. "\\|") "\\)"))
  7905. (pos (point))
  7906. (pre nil) (post nil)
  7907. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7908. (cond
  7909. ;; First check if there are any special
  7910. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7911. ;; Now try the builtin stuff
  7912. ((and (equal (string-to-char s0) ?#)
  7913. (> (length s0) 1)
  7914. (save-excursion
  7915. (goto-char (point-min))
  7916. (and
  7917. (re-search-forward
  7918. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7919. (setq type 'dedicated
  7920. pos (match-beginning 0))))
  7921. ;; There is an exact target for this
  7922. (goto-char pos)
  7923. (org-back-to-heading t)))
  7924. ((save-excursion
  7925. (goto-char (point-min))
  7926. (and
  7927. (re-search-forward
  7928. (concat "<<" (regexp-quote s0) ">>") nil t)
  7929. (setq type 'dedicated
  7930. pos (match-beginning 0))))
  7931. ;; There is an exact target for this
  7932. (goto-char pos))
  7933. ((and (string-match "^(\\(.*\\))$" s0)
  7934. (save-excursion
  7935. (goto-char (point-min))
  7936. (and
  7937. (re-search-forward
  7938. (concat "[^[]" (regexp-quote
  7939. (format org-coderef-label-format
  7940. (match-string 1 s0))))
  7941. nil t)
  7942. (setq type 'dedicated
  7943. pos (1+ (match-beginning 0))))))
  7944. ;; There is a coderef target for this
  7945. (goto-char pos))
  7946. ((string-match "^/\\(.*\\)/$" s)
  7947. ;; A regular expression
  7948. (cond
  7949. ((org-mode-p)
  7950. (org-occur (match-string 1 s)))
  7951. ;;((eq major-mode 'dired-mode)
  7952. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7953. (t (org-do-occur (match-string 1 s)))))
  7954. (t
  7955. ;; A normal search strings
  7956. (when (equal (string-to-char s) ?*)
  7957. ;; Anchor on headlines, post may include tags.
  7958. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7959. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7960. s (substring s 1)))
  7961. (remove-text-properties
  7962. 0 (length s)
  7963. '(face nil mouse-face nil keymap nil fontified nil) s)
  7964. ;; Make a series of regular expressions to find a match
  7965. (setq words (org-split-string s "[ \n\r\t]+")
  7966. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7967. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7968. "\\)" markers)
  7969. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7970. re2a (concat "[ \t\r\n]" re2a_)
  7971. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7972. re4 (concat "[^a-zA-Z_]" re4_)
  7973. re1 (concat pre re2 post)
  7974. re3 (concat pre (if pre re4_ re4) post)
  7975. re5 (concat pre ".*" re4)
  7976. re2 (concat pre re2)
  7977. re2a (concat pre (if pre re2a_ re2a))
  7978. re4 (concat pre (if pre re4_ re4))
  7979. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7980. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7981. re5 "\\)"
  7982. ))
  7983. (cond
  7984. ((eq type 'org-occur) (org-occur reall))
  7985. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7986. (t (goto-char (point-min))
  7987. (setq type 'fuzzy)
  7988. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7989. (org-search-not-self 1 re1 nil t)
  7990. (org-search-not-self 1 re2 nil t)
  7991. (org-search-not-self 1 re2a nil t)
  7992. (org-search-not-self 1 re3 nil t)
  7993. (org-search-not-self 1 re4 nil t)
  7994. (org-search-not-self 1 re5 nil t)
  7995. )
  7996. (goto-char (match-beginning 1))
  7997. (goto-char pos)
  7998. (error "No match")))))
  7999. (t
  8000. ;; Normal string-search
  8001. (goto-char (point-min))
  8002. (if (search-forward s nil t)
  8003. (goto-char (match-beginning 0))
  8004. (error "No match"))))
  8005. (and (org-mode-p) (org-show-context 'link-search))
  8006. type))
  8007. (defun org-search-not-self (group &rest args)
  8008. "Execute `re-search-forward', but only accept matches that do not
  8009. enclose the position of `org-open-link-marker'."
  8010. (let ((m org-open-link-marker))
  8011. (catch 'exit
  8012. (while (apply 're-search-forward args)
  8013. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8014. (goto-char (match-end group))
  8015. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8016. (> (match-beginning 0) (marker-position m))
  8017. (< (match-end 0) (marker-position m)))
  8018. (save-match-data
  8019. (or (not (org-in-regexp
  8020. org-bracket-link-analytic-regexp 1))
  8021. (not (match-end 4)) ; no description
  8022. (and (<= (match-beginning 4) (point))
  8023. (>= (match-end 4) (point))))))
  8024. (throw 'exit (point))))))))
  8025. (defun org-get-buffer-for-internal-link (buffer)
  8026. "Return a buffer to be used for displaying the link target of internal links."
  8027. (cond
  8028. ((not org-display-internal-link-with-indirect-buffer)
  8029. buffer)
  8030. ((string-match "(Clone)$" (buffer-name buffer))
  8031. (message "Buffer is already a clone, not making another one")
  8032. ;; we also do not modify visibility in this case
  8033. buffer)
  8034. (t ; make a new indirect buffer for displaying the link
  8035. (let* ((bn (buffer-name buffer))
  8036. (ibn (concat bn "(Clone)"))
  8037. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8038. (with-current-buffer ib (org-overview))
  8039. ib))))
  8040. (defun org-do-occur (regexp &optional cleanup)
  8041. "Call the Emacs command `occur'.
  8042. If CLEANUP is non-nil, remove the printout of the regular expression
  8043. in the *Occur* buffer. This is useful if the regex is long and not useful
  8044. to read."
  8045. (occur regexp)
  8046. (when cleanup
  8047. (let ((cwin (selected-window)) win beg end)
  8048. (when (setq win (get-buffer-window "*Occur*"))
  8049. (select-window win))
  8050. (goto-char (point-min))
  8051. (when (re-search-forward "match[a-z]+" nil t)
  8052. (setq beg (match-end 0))
  8053. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8054. (setq end (1- (match-beginning 0)))))
  8055. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8056. (goto-char (point-min))
  8057. (select-window cwin))))
  8058. ;;; The mark ring for links jumps
  8059. (defvar org-mark-ring nil
  8060. "Mark ring for positions before jumps in Org-mode.")
  8061. (defvar org-mark-ring-last-goto nil
  8062. "Last position in the mark ring used to go back.")
  8063. ;; Fill and close the ring
  8064. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8065. (loop for i from 1 to org-mark-ring-length do
  8066. (push (make-marker) org-mark-ring))
  8067. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8068. org-mark-ring)
  8069. (defun org-mark-ring-push (&optional pos buffer)
  8070. "Put the current position or POS into the mark ring and rotate it."
  8071. (interactive)
  8072. (setq pos (or pos (point)))
  8073. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8074. (move-marker (car org-mark-ring)
  8075. (or pos (point))
  8076. (or buffer (current-buffer)))
  8077. (message "%s"
  8078. (substitute-command-keys
  8079. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8080. (defun org-mark-ring-goto (&optional n)
  8081. "Jump to the previous position in the mark ring.
  8082. With prefix arg N, jump back that many stored positions. When
  8083. called several times in succession, walk through the entire ring.
  8084. Org-mode commands jumping to a different position in the current file,
  8085. or to another Org-mode file, automatically push the old position
  8086. onto the ring."
  8087. (interactive "p")
  8088. (let (p m)
  8089. (if (eq last-command this-command)
  8090. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8091. (setq p org-mark-ring))
  8092. (setq org-mark-ring-last-goto p)
  8093. (setq m (car p))
  8094. (switch-to-buffer (marker-buffer m))
  8095. (goto-char m)
  8096. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8097. (defun org-remove-angle-brackets (s)
  8098. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8099. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8100. s)
  8101. (defun org-add-angle-brackets (s)
  8102. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8103. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8104. s)
  8105. (defun org-remove-double-quotes (s)
  8106. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8107. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8108. s)
  8109. ;;; Following specific links
  8110. (defun org-follow-timestamp-link ()
  8111. (cond
  8112. ((org-at-date-range-p t)
  8113. (let ((org-agenda-start-on-weekday)
  8114. (t1 (match-string 1))
  8115. (t2 (match-string 2)))
  8116. (setq t1 (time-to-days (org-time-string-to-time t1))
  8117. t2 (time-to-days (org-time-string-to-time t2)))
  8118. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8119. ((org-at-timestamp-p t)
  8120. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8121. (substring (match-string 1) 0 10)))
  8122. 1))
  8123. (t (error "This should not happen"))))
  8124. ;;; Following file links
  8125. (defvar org-wait nil)
  8126. (defun org-open-file (path &optional in-emacs line search)
  8127. "Open the file at PATH.
  8128. First, this expands any special file name abbreviations. Then the
  8129. configuration variable `org-file-apps' is checked if it contains an
  8130. entry for this file type, and if yes, the corresponding command is launched.
  8131. If no application is found, Emacs simply visits the file.
  8132. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8133. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  8134. and to use an external application to visit the file.
  8135. Optional LINE specifies a line to go to, optional SEARCH a string to
  8136. search for. If LINE or SEARCH is given, but IN-EMACS is nil, it will
  8137. be assumed that org-open-file was called to open a file: link, and the
  8138. original link to match against org-file-apps will be reconstructed
  8139. from PATH and whichever of LINE or SEARCH is given.
  8140. If the file does not exist, an error is thrown."
  8141. (let* ((file (if (equal path "")
  8142. buffer-file-name
  8143. (substitute-in-file-name (expand-file-name path))))
  8144. (apps (append org-file-apps (org-default-apps)))
  8145. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8146. (dirp (if remp nil (file-directory-p file)))
  8147. (file (if (and dirp org-open-directory-means-index-dot-org)
  8148. (concat (file-name-as-directory file) "index.org")
  8149. file))
  8150. (a-m-a-p (assq 'auto-mode apps))
  8151. (dfile (downcase file))
  8152. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  8153. (link (cond ((and (eq line nil)
  8154. (eq search nil))
  8155. file)
  8156. (line
  8157. (concat file "::" (number-to-string line)))
  8158. (search
  8159. (concat file "::" search))))
  8160. (dlink (downcase link))
  8161. (old-buffer (current-buffer))
  8162. (old-pos (point))
  8163. (old-mode major-mode)
  8164. ext cmd link-match-data)
  8165. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8166. (setq ext (match-string 1 dfile))
  8167. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8168. (setq ext (match-string 1 dfile))))
  8169. (cond
  8170. ((member in-emacs '((16) system))
  8171. (setq cmd (cdr (assoc 'system apps))))
  8172. (in-emacs (setq cmd 'emacs))
  8173. (t
  8174. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8175. (and dirp (cdr (assoc 'directory apps)))
  8176. ;; if we find a match in org-file-apps, store the match
  8177. ;; data for later
  8178. (let ((match (assoc-default dlink (org-apps-regexp-alist
  8179. apps a-m-a-p)
  8180. 'string-match)))
  8181. (if match
  8182. (progn (setq link-match-data (match-data))
  8183. match)
  8184. nil))
  8185. (cdr (assoc ext apps))
  8186. (cdr (assoc t apps))))))
  8187. (when (eq cmd 'system)
  8188. (setq cmd (cdr (assoc 'system apps))))
  8189. (when (eq cmd 'default)
  8190. (setq cmd (cdr (assoc t apps))))
  8191. (when (eq cmd 'mailcap)
  8192. (require 'mailcap)
  8193. (mailcap-parse-mailcaps)
  8194. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8195. (command (mailcap-mime-info mime-type)))
  8196. (if (stringp command)
  8197. (setq cmd command)
  8198. (setq cmd 'emacs))))
  8199. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8200. (not (file-exists-p file))
  8201. (not org-open-non-existing-files))
  8202. (error "No such file: %s" file))
  8203. (cond
  8204. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8205. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8206. (while (string-match "['\"]%s['\"]" cmd)
  8207. (setq cmd (replace-match "%s" t t cmd)))
  8208. (while (string-match "%s" cmd)
  8209. (setq cmd (replace-match
  8210. (save-match-data
  8211. (shell-quote-argument
  8212. (convert-standard-filename file)))
  8213. t t cmd)))
  8214. ;; Replace "%1", "%2" etc. in command with group matches from regex
  8215. (save-match-data
  8216. (let ((match-index 1)
  8217. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  8218. (set-match-data link-match-data)
  8219. (while (<= match-index number-of-groups)
  8220. (let ((regex (concat "%" (number-to-string match-index)))
  8221. (replace-with (match-string match-index dlink)))
  8222. (while (string-match regex cmd)
  8223. (setq cmd (replace-match replace-with t t cmd))))
  8224. (setq match-index (+ match-index 1)))))
  8225. (save-window-excursion
  8226. (start-process-shell-command cmd nil cmd)
  8227. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8228. ))
  8229. ((or (stringp cmd)
  8230. (eq cmd 'emacs))
  8231. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8232. (widen)
  8233. (if line (org-goto-line line)
  8234. (if search (org-link-search search))))
  8235. ((consp cmd)
  8236. (let ((file (convert-standard-filename file)))
  8237. (save-match-data
  8238. (set-match-data link-match-data)
  8239. (eval cmd))))
  8240. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8241. (and (org-mode-p) (eq old-mode 'org-mode)
  8242. (or (not (equal old-buffer (current-buffer)))
  8243. (not (equal old-pos (point))))
  8244. (org-mark-ring-push old-pos old-buffer))))
  8245. (defun org-default-apps ()
  8246. "Return the default applications for this operating system."
  8247. (cond
  8248. ((eq system-type 'darwin)
  8249. org-file-apps-defaults-macosx)
  8250. ((eq system-type 'windows-nt)
  8251. org-file-apps-defaults-windowsnt)
  8252. (t org-file-apps-defaults-gnu)))
  8253. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  8254. "Convert extensions to regular expressions in the cars of LIST.
  8255. Also, weed out any non-string entries, because the return value is used
  8256. only for regexp matching.
  8257. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  8258. point to the symbol `emacs', indicating that the file should
  8259. be opened in Emacs."
  8260. (append
  8261. (delq nil
  8262. (mapcar (lambda (x)
  8263. (if (not (stringp (car x)))
  8264. nil
  8265. (if (string-match "\\W" (car x))
  8266. x
  8267. (cons (concat "\\." (car x) "\\(::.*\\)?\\'")
  8268. (cdr x)))))
  8269. list))
  8270. (if add-auto-mode
  8271. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  8272. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  8273. (defun org-file-remote-p (file)
  8274. "Test whether FILE specifies a location on a remote system.
  8275. Return non-nil if the location is indeed remote.
  8276. For example, the filename \"/user@host:/foo\" specifies a location
  8277. on the system \"/user@host:\"."
  8278. (cond ((fboundp 'file-remote-p)
  8279. (file-remote-p file))
  8280. ((fboundp 'tramp-handle-file-remote-p)
  8281. (tramp-handle-file-remote-p file))
  8282. ((and (boundp 'ange-ftp-name-format)
  8283. (string-match (car ange-ftp-name-format) file))
  8284. t)
  8285. (t nil)))
  8286. ;;;; Refiling
  8287. (defun org-get-org-file ()
  8288. "Read a filename, with default directory `org-directory'."
  8289. (let ((default (or org-default-notes-file remember-data-file)))
  8290. (read-file-name (format "File name [%s]: " default)
  8291. (file-name-as-directory org-directory)
  8292. default)))
  8293. (defun org-notes-order-reversed-p ()
  8294. "Check if the current file should receive notes in reversed order."
  8295. (cond
  8296. ((not org-reverse-note-order) nil)
  8297. ((eq t org-reverse-note-order) t)
  8298. ((not (listp org-reverse-note-order)) nil)
  8299. (t (catch 'exit
  8300. (let ((all org-reverse-note-order)
  8301. entry)
  8302. (while (setq entry (pop all))
  8303. (if (string-match (car entry) buffer-file-name)
  8304. (throw 'exit (cdr entry))))
  8305. nil)))))
  8306. (defvar org-refile-target-table nil
  8307. "The list of refile targets, created by `org-refile'.")
  8308. (defvar org-agenda-new-buffers nil
  8309. "Buffers created to visit agenda files.")
  8310. (defun org-get-refile-targets (&optional default-buffer)
  8311. "Produce a table with refile targets."
  8312. (let ((case-fold-search nil)
  8313. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  8314. (entries (or org-refile-targets '((nil . (:level . 1)))))
  8315. targets txt re files f desc descre fast-path-p level pos0)
  8316. (message "Getting targets...")
  8317. (with-current-buffer (or default-buffer (current-buffer))
  8318. (while (setq entry (pop entries))
  8319. (setq files (car entry) desc (cdr entry))
  8320. (setq fast-path-p nil)
  8321. (cond
  8322. ((null files) (setq files (list (current-buffer))))
  8323. ((eq files 'org-agenda-files)
  8324. (setq files (org-agenda-files 'unrestricted)))
  8325. ((and (symbolp files) (fboundp files))
  8326. (setq files (funcall files)))
  8327. ((and (symbolp files) (boundp files))
  8328. (setq files (symbol-value files))))
  8329. (if (stringp files) (setq files (list files)))
  8330. (cond
  8331. ((eq (car desc) :tag)
  8332. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  8333. ((eq (car desc) :todo)
  8334. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  8335. ((eq (car desc) :regexp)
  8336. (setq descre (cdr desc)))
  8337. ((eq (car desc) :level)
  8338. (setq descre (concat "^\\*\\{" (number-to-string
  8339. (if org-odd-levels-only
  8340. (1- (* 2 (cdr desc)))
  8341. (cdr desc)))
  8342. "\\}[ \t]")))
  8343. ((eq (car desc) :maxlevel)
  8344. (setq fast-path-p t)
  8345. (setq descre (concat "^\\*\\{1," (number-to-string
  8346. (if org-odd-levels-only
  8347. (1- (* 2 (cdr desc)))
  8348. (cdr desc)))
  8349. "\\}[ \t]")))
  8350. (t (error "Bad refiling target description %s" desc)))
  8351. (while (setq f (pop files))
  8352. (with-current-buffer
  8353. (if (bufferp f) f (org-get-agenda-file-buffer f))
  8354. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  8355. (setq f (and f (expand-file-name f)))
  8356. (if (eq org-refile-use-outline-path 'file)
  8357. (push (list (file-name-nondirectory f) f nil nil) targets))
  8358. (save-excursion
  8359. (save-restriction
  8360. (widen)
  8361. (goto-char (point-min))
  8362. (while (re-search-forward descre nil t)
  8363. (goto-char (setq pos0 (point-at-bol)))
  8364. (catch 'next
  8365. (when org-refile-target-verify-function
  8366. (save-match-data
  8367. (or (funcall org-refile-target-verify-function)
  8368. (throw 'next t))))
  8369. (when (looking-at org-complex-heading-regexp)
  8370. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  8371. txt (org-link-display-format (match-string 4))
  8372. re (concat "^" (regexp-quote
  8373. (buffer-substring (match-beginning 1)
  8374. (match-end 4)))))
  8375. (if (match-end 5) (setq re (concat re "[ \t]+"
  8376. (regexp-quote
  8377. (match-string 5)))))
  8378. (setq re (concat re "[ \t]*$"))
  8379. (when org-refile-use-outline-path
  8380. (setq txt (mapconcat 'org-protect-slash
  8381. (append
  8382. (if (eq org-refile-use-outline-path 'file)
  8383. (list (file-name-nondirectory
  8384. (buffer-file-name (buffer-base-buffer))))
  8385. (if (eq org-refile-use-outline-path 'full-file-path)
  8386. (list (buffer-file-name (buffer-base-buffer)))))
  8387. (org-get-outline-path fast-path-p level txt)
  8388. (list txt))
  8389. "/")))
  8390. (push (list txt f re (point)) targets)))
  8391. (when (= (point) pos0)
  8392. ;; verification function has not moved point
  8393. (goto-char (point-at-eol))))))))))
  8394. (message "Getting targets...done")
  8395. (nreverse targets)))
  8396. (defun org-protect-slash (s)
  8397. (while (string-match "/" s)
  8398. (setq s (replace-match "\\" t t s)))
  8399. s)
  8400. (defvar org-olpa (make-vector 20 nil))
  8401. (defun org-get-outline-path (&optional fastp level heading)
  8402. "Return the outline path to the current entry, as a list.
  8403. The parameters FASTP, LEVEL, and HEADING are for use be a scanner
  8404. routine which makes outline path derivations for an entire file,
  8405. avoiding backtracing."
  8406. (if fastp
  8407. (progn
  8408. (if (> level 19)
  8409. (error "Outline path failure, more than 19 levels."))
  8410. (loop for i from level upto 19 do
  8411. (aset org-olpa i nil))
  8412. (prog1
  8413. (delq nil (append org-olpa nil))
  8414. (aset org-olpa level heading)))
  8415. (let (rtn case-fold-search)
  8416. (save-excursion
  8417. (save-restriction
  8418. (widen)
  8419. (while (org-up-heading-safe)
  8420. (when (looking-at org-complex-heading-regexp)
  8421. (push (org-match-string-no-properties 4) rtn)))
  8422. rtn)))))
  8423. (defun org-format-outline-path (path &optional width prefix)
  8424. "Format the outlie path PATH for display.
  8425. Width is the maximum number of characters that is available.
  8426. Prefix is a prefix to be included in the returned string,
  8427. such as the file name."
  8428. (setq width (or width 79))
  8429. (if prefix (setq width (- width (length prefix))))
  8430. (if (not path)
  8431. (or prefix "")
  8432. (let* ((nsteps (length path))
  8433. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  8434. (maxwidth (if (<= total-width width)
  8435. 10000 ;; everything fits
  8436. ;; we need to shorten the level headings
  8437. (/ (- width nsteps) nsteps)))
  8438. (org-odd-levels-only nil)
  8439. (n 0)
  8440. (total (1+ (length prefix))))
  8441. (setq maxwidth (max maxwidth 10))
  8442. (concat prefix
  8443. (mapconcat
  8444. (lambda (h)
  8445. (setq n (1+ n))
  8446. (if (and (= n nsteps) (< maxwidth 10000))
  8447. (setq maxwidth (- total-width total)))
  8448. (if (< (length h) maxwidth)
  8449. (progn (setq total (+ total (length h) 1)) h)
  8450. (setq h (substring h 0 (- maxwidth 2))
  8451. total (+ total maxwidth 1))
  8452. (if (string-match "[ \t]+\\'" h)
  8453. (setq h (substring h 0 (match-beginning 0))))
  8454. (setq h (concat h "..")))
  8455. (org-add-props h nil 'face
  8456. (nth (% (1- n) org-n-level-faces)
  8457. org-level-faces))
  8458. h)
  8459. path "/")))))
  8460. (defun org-display-outline-path (&optional file current)
  8461. "Display the current outline path in the echo area."
  8462. (interactive "P")
  8463. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  8464. (case-fold-search nil)
  8465. (path (and (org-mode-p) (org-get-outline-path))))
  8466. (if current (setq path (append path
  8467. (save-excursion
  8468. (org-back-to-heading t)
  8469. (if (looking-at org-complex-heading-regexp)
  8470. (list (match-string 4)))))))
  8471. (message "%s"
  8472. (org-format-outline-path
  8473. path
  8474. (1- (frame-width))
  8475. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  8476. (defvar org-refile-history nil
  8477. "History for refiling operations.")
  8478. (defvar org-after-refile-insert-hook nil
  8479. "Hook run after `org-refile' has inserted its stuff at the new location.
  8480. Note that this is still *before* the stuff will be removed from
  8481. the *old* location.")
  8482. (defun org-refile (&optional goto default-buffer rfloc)
  8483. "Move the entry at point to another heading.
  8484. The list of target headings is compiled using the information in
  8485. `org-refile-targets', which see. This list is created before each use
  8486. and will therefore always be up-to-date.
  8487. At the target location, the entry is filed as a subitem of the target heading.
  8488. Depending on `org-reverse-note-order', the new subitem will either be the
  8489. first or the last subitem.
  8490. If there is an active region, all entries in that region will be moved.
  8491. However, the region must fulfil the requirement that the first heading
  8492. is the first one sets the top-level of the moved text - at most siblings
  8493. below it are allowed.
  8494. With prefix arg GOTO, the command will only visit the target location,
  8495. not actually move anything.
  8496. With a double prefix `C-u C-u', go to the location where the last refiling
  8497. operation has put the subtree.
  8498. With a prefix argument of `2', refile to the running clock.
  8499. RFLOC can be a refile location obtained in a different way.
  8500. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  8501. (interactive "P")
  8502. (let* ((cbuf (current-buffer))
  8503. (regionp (org-region-active-p))
  8504. (region-start (and regionp (region-beginning)))
  8505. (region-end (and regionp (region-end)))
  8506. (region-length (and regionp (- region-end region-start)))
  8507. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  8508. pos it nbuf file re level reversed)
  8509. (setq last-command nil)
  8510. (when regionp
  8511. (goto-char region-start)
  8512. (or (bolp) (goto-char (point-at-bol)))
  8513. (setq region-start (point))
  8514. (unless (org-kill-is-subtree-p
  8515. (buffer-substring region-start region-end))
  8516. (error "The region is not a (sequence of) subtree(s)")))
  8517. (if (equal goto '(16))
  8518. (org-refile-goto-last-stored)
  8519. (when (or
  8520. (and (equal goto 2)
  8521. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  8522. (prog1
  8523. (setq it (list (or org-clock-heading "running clock")
  8524. (buffer-file-name
  8525. (marker-buffer org-clock-hd-marker))
  8526. ""
  8527. (marker-position org-clock-hd-marker)))
  8528. (setq goto nil)))
  8529. (setq it (or rfloc
  8530. (save-excursion
  8531. (org-refile-get-location
  8532. (if goto "Goto: " "Refile to: ") default-buffer
  8533. org-refile-allow-creating-parent-nodes)))))
  8534. (setq file (nth 1 it)
  8535. re (nth 2 it)
  8536. pos (nth 3 it))
  8537. (if (and (not goto)
  8538. pos
  8539. (equal (buffer-file-name) file)
  8540. (if regionp
  8541. (and (>= pos region-start)
  8542. (<= pos region-end))
  8543. (and (>= pos (point))
  8544. (< pos (save-excursion
  8545. (org-end-of-subtree t t))))))
  8546. (error "Cannot refile to position inside the tree or region"))
  8547. (setq nbuf (or (find-buffer-visiting file)
  8548. (find-file-noselect file)))
  8549. (if goto
  8550. (progn
  8551. (switch-to-buffer nbuf)
  8552. (goto-char pos)
  8553. (org-show-context 'org-goto))
  8554. (if regionp
  8555. (progn
  8556. (org-kill-new (buffer-substring region-start region-end))
  8557. (org-save-markers-in-region region-start region-end))
  8558. (org-copy-subtree 1 nil t))
  8559. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  8560. (find-file-noselect file)))
  8561. (setq reversed (org-notes-order-reversed-p))
  8562. (save-excursion
  8563. (save-restriction
  8564. (widen)
  8565. (if pos
  8566. (progn
  8567. (goto-char pos)
  8568. (looking-at outline-regexp)
  8569. (setq level (org-get-valid-level (funcall outline-level) 1))
  8570. (goto-char
  8571. (if reversed
  8572. (or (outline-next-heading) (point-max))
  8573. (or (save-excursion (org-get-next-sibling))
  8574. (org-end-of-subtree t t)
  8575. (point-max)))))
  8576. (setq level 1)
  8577. (if (not reversed)
  8578. (goto-char (point-max))
  8579. (goto-char (point-min))
  8580. (or (outline-next-heading) (goto-char (point-max)))))
  8581. (if (not (bolp)) (newline))
  8582. (org-paste-subtree level)
  8583. (when org-log-refile
  8584. (org-add-log-setup 'refile nil nil 'findpos
  8585. org-log-refile)
  8586. (unless (eq org-log-refile 'note)
  8587. (save-excursion (org-add-log-note))))
  8588. (and org-auto-align-tags (org-set-tags nil t))
  8589. (bookmark-set "org-refile-last-stored")
  8590. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8591. (run-hooks 'org-after-refile-insert-hook))))
  8592. (if regionp
  8593. (delete-region (point) (+ (point) region-length))
  8594. (org-cut-subtree))
  8595. (when (featurep 'org-inlinetask)
  8596. (org-inlinetask-remove-END-maybe))
  8597. (setq org-markers-to-move nil)
  8598. (message "Refiled to \"%s\"" (car it))))))
  8599. (org-reveal))
  8600. (defun org-refile-goto-last-stored ()
  8601. "Go to the location where the last refile was stored."
  8602. (interactive)
  8603. (bookmark-jump "org-refile-last-stored")
  8604. (message "This is the location of the last refile"))
  8605. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  8606. "Prompt the user for a refile location, using PROMPT."
  8607. (let ((org-refile-targets org-refile-targets)
  8608. (org-refile-use-outline-path org-refile-use-outline-path))
  8609. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  8610. (unless org-refile-target-table
  8611. (error "No refile targets"))
  8612. (let* ((cbuf (current-buffer))
  8613. (partial-completion-mode nil)
  8614. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  8615. (cfunc (if (and org-refile-use-outline-path
  8616. org-outline-path-complete-in-steps)
  8617. 'org-olpath-completing-read
  8618. 'org-icompleting-read))
  8619. (extra (if org-refile-use-outline-path "/" ""))
  8620. (filename (and cfn (expand-file-name cfn)))
  8621. (tbl (mapcar
  8622. (lambda (x)
  8623. (if (and (not (member org-refile-use-outline-path
  8624. '(file full-file-path)))
  8625. (not (equal filename (nth 1 x))))
  8626. (cons (concat (car x) extra " ("
  8627. (file-name-nondirectory (nth 1 x)) ")")
  8628. (cdr x))
  8629. (cons (concat (car x) extra) (cdr x))))
  8630. org-refile-target-table))
  8631. (completion-ignore-case t)
  8632. pa answ parent-target child parent old-hist)
  8633. (setq old-hist org-refile-history)
  8634. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  8635. nil 'org-refile-history))
  8636. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  8637. (if pa
  8638. (progn
  8639. (when (or (not org-refile-history)
  8640. (not (eq old-hist org-refile-history))
  8641. (not (equal (car pa) (car org-refile-history))))
  8642. (setq org-refile-history
  8643. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  8644. org-refile-history
  8645. (cdr org-refile-history))))
  8646. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  8647. (pop org-refile-history)))
  8648. pa)
  8649. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8650. (progn
  8651. (setq parent (match-string 1 answ)
  8652. child (match-string 2 answ))
  8653. (setq parent-target (or (assoc parent tbl)
  8654. (assoc (concat parent "/") tbl)))
  8655. (when (and parent-target
  8656. (or (eq new-nodes t)
  8657. (and (eq new-nodes 'confirm)
  8658. (y-or-n-p (format "Create new node \"%s\"? "
  8659. child)))))
  8660. (org-refile-new-child parent-target child)))
  8661. (error "Invalid target location")))))
  8662. (defun org-refile-new-child (parent-target child)
  8663. "Use refile target PARENT-TARGET to add new CHILD below it."
  8664. (unless parent-target
  8665. (error "Cannot find parent for new node"))
  8666. (let ((file (nth 1 parent-target))
  8667. (pos (nth 3 parent-target))
  8668. level)
  8669. (with-current-buffer (or (find-buffer-visiting file)
  8670. (find-file-noselect file))
  8671. (save-excursion
  8672. (save-restriction
  8673. (widen)
  8674. (if pos
  8675. (goto-char pos)
  8676. (goto-char (point-max))
  8677. (if (not (bolp)) (newline)))
  8678. (when (looking-at outline-regexp)
  8679. (setq level (funcall outline-level))
  8680. (org-end-of-subtree t t))
  8681. (org-back-over-empty-lines)
  8682. (insert "\n" (make-string
  8683. (if pos (org-get-valid-level level 1) 1) ?*)
  8684. " " child "\n")
  8685. (beginning-of-line 0)
  8686. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8687. (defun org-olpath-completing-read (prompt collection &rest args)
  8688. "Read an outline path like a file name."
  8689. (let ((thetable collection)
  8690. (org-completion-use-ido nil) ; does not work with ido.
  8691. (org-completion-use-iswitchb nil)) ; or iswitchb
  8692. (apply
  8693. 'org-icompleting-read prompt
  8694. (lambda (string predicate &optional flag)
  8695. (let (rtn r f (l (length string)))
  8696. (cond
  8697. ((eq flag nil)
  8698. ;; try completion
  8699. (try-completion string thetable))
  8700. ((eq flag t)
  8701. ;; all-completions
  8702. (setq rtn (all-completions string thetable predicate))
  8703. (mapcar
  8704. (lambda (x)
  8705. (setq r (substring x l))
  8706. (if (string-match " ([^)]*)$" x)
  8707. (setq f (match-string 0 x))
  8708. (setq f ""))
  8709. (if (string-match "/" r)
  8710. (concat string (substring r 0 (match-end 0)) f)
  8711. x))
  8712. rtn))
  8713. ((eq flag 'lambda)
  8714. ;; exact match?
  8715. (assoc string thetable)))
  8716. ))
  8717. args)))
  8718. ;;;; Dynamic blocks
  8719. (defun org-find-dblock (name)
  8720. "Find the first dynamic block with name NAME in the buffer.
  8721. If not found, stay at current position and return nil."
  8722. (let (pos)
  8723. (save-excursion
  8724. (goto-char (point-min))
  8725. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8726. nil t)
  8727. (match-beginning 0))))
  8728. (if pos (goto-char pos))
  8729. pos))
  8730. (defconst org-dblock-start-re
  8731. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8732. "Matches the start line of a dynamic block, with parameters.")
  8733. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  8734. "Matches the end of a dynamic block.")
  8735. (defun org-create-dblock (plist)
  8736. "Create a dynamic block section, with parameters taken from PLIST.
  8737. PLIST must contain a :name entry which is used as name of the block."
  8738. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  8739. (end-of-line 1)
  8740. (newline))
  8741. (let ((col (current-column))
  8742. (name (plist-get plist :name)))
  8743. (insert "#+BEGIN: " name)
  8744. (while plist
  8745. (if (eq (car plist) :name)
  8746. (setq plist (cddr plist))
  8747. (insert " " (prin1-to-string (pop plist)))))
  8748. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  8749. (beginning-of-line -2)))
  8750. (defun org-prepare-dblock ()
  8751. "Prepare dynamic block for refresh.
  8752. This empties the block, puts the cursor at the insert position and returns
  8753. the property list including an extra property :name with the block name."
  8754. (unless (looking-at org-dblock-start-re)
  8755. (error "Not at a dynamic block"))
  8756. (let* ((begdel (1+ (match-end 0)))
  8757. (name (org-no-properties (match-string 1)))
  8758. (params (append (list :name name)
  8759. (read (concat "(" (match-string 3) ")")))))
  8760. (save-excursion
  8761. (beginning-of-line 1)
  8762. (skip-chars-forward " \t")
  8763. (setq params (plist-put params :indentation-column (current-column))))
  8764. (unless (re-search-forward org-dblock-end-re nil t)
  8765. (error "Dynamic block not terminated"))
  8766. (setq params
  8767. (append params
  8768. (list :content (buffer-substring
  8769. begdel (match-beginning 0)))))
  8770. (delete-region begdel (match-beginning 0))
  8771. (goto-char begdel)
  8772. (open-line 1)
  8773. params))
  8774. (defun org-map-dblocks (&optional command)
  8775. "Apply COMMAND to all dynamic blocks in the current buffer.
  8776. If COMMAND is not given, use `org-update-dblock'."
  8777. (let ((cmd (or command 'org-update-dblock)))
  8778. (save-excursion
  8779. (goto-char (point-min))
  8780. (while (re-search-forward org-dblock-start-re nil t)
  8781. (goto-char (match-beginning 0))
  8782. (save-excursion
  8783. (condition-case nil
  8784. (funcall cmd)
  8785. (error (message "Error during update of dynamic block"))))
  8786. (unless (re-search-forward org-dblock-end-re nil t)
  8787. (error "Dynamic block not terminated"))))))
  8788. (defun org-dblock-update (&optional arg)
  8789. "User command for updating dynamic blocks.
  8790. Update the dynamic block at point. With prefix ARG, update all dynamic
  8791. blocks in the buffer."
  8792. (interactive "P")
  8793. (if arg
  8794. (org-update-all-dblocks)
  8795. (or (looking-at org-dblock-start-re)
  8796. (org-beginning-of-dblock))
  8797. (org-update-dblock)))
  8798. (defun org-update-dblock ()
  8799. "Update the dynamic block at point
  8800. This means to empty the block, parse for parameters and then call
  8801. the correct writing function."
  8802. (save-window-excursion
  8803. (let* ((pos (point))
  8804. (line (org-current-line))
  8805. (params (org-prepare-dblock))
  8806. (name (plist-get params :name))
  8807. (indent (plist-get params :indentation-column))
  8808. (cmd (intern (concat "org-dblock-write:" name))))
  8809. (message "Updating dynamic block `%s' at line %d..." name line)
  8810. (funcall cmd params)
  8811. (message "Updating dynamic block `%s' at line %d...done" name line)
  8812. (goto-char pos)
  8813. (when (and indent (> indent 0))
  8814. (setq indent (make-string indent ?\ ))
  8815. (save-excursion
  8816. (org-beginning-of-dblock)
  8817. (forward-line 1)
  8818. (while (not (looking-at org-dblock-end-re))
  8819. (insert indent)
  8820. (beginning-of-line 2))
  8821. (when (looking-at org-dblock-end-re)
  8822. (and (looking-at "[ \t]+")
  8823. (replace-match ""))
  8824. (insert indent)))))))
  8825. (defun org-beginning-of-dblock ()
  8826. "Find the beginning of the dynamic block at point.
  8827. Error if there is no such block at point."
  8828. (let ((pos (point))
  8829. beg)
  8830. (end-of-line 1)
  8831. (if (and (re-search-backward org-dblock-start-re nil t)
  8832. (setq beg (match-beginning 0))
  8833. (re-search-forward org-dblock-end-re nil t)
  8834. (> (match-end 0) pos))
  8835. (goto-char beg)
  8836. (goto-char pos)
  8837. (error "Not in a dynamic block"))))
  8838. (defun org-update-all-dblocks ()
  8839. "Update all dynamic blocks in the buffer.
  8840. This function can be used in a hook."
  8841. (when (org-mode-p)
  8842. (org-map-dblocks 'org-update-dblock)))
  8843. ;;;; Completion
  8844. (defconst org-additional-option-like-keywords
  8845. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8846. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8847. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  8848. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX"
  8849. "BEGIN:" "END:"
  8850. "ORGTBL" "TBLFM:" "TBLNAME:"
  8851. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8852. "BEGIN_QUOTE" "END_QUOTE"
  8853. "BEGIN_VERSE" "END_VERSE"
  8854. "BEGIN_CENTER" "END_CENTER"
  8855. "BEGIN_SRC" "END_SRC"
  8856. "CATEGORY" "COLUMNS"
  8857. "CAPTION" "LABEL"
  8858. "SETUPFILE"
  8859. "BIND"
  8860. "MACRO"))
  8861. (defcustom org-structure-template-alist
  8862. '(
  8863. ("s" "#+begin_src ?\n\n#+end_src"
  8864. "<src lang=\"?\">\n\n</src>")
  8865. ("e" "#+begin_example\n?\n#+end_example"
  8866. "<example>\n?\n</example>")
  8867. ("q" "#+begin_quote\n?\n#+end_quote"
  8868. "<quote>\n?\n</quote>")
  8869. ("v" "#+begin_verse\n?\n#+end_verse"
  8870. "<verse>\n?\n/verse>")
  8871. ("c" "#+begin_center\n?\n#+end_center"
  8872. "<center>\n?\n/center>")
  8873. ("l" "#+begin_latex\n?\n#+end_latex"
  8874. "<literal style=\"latex\">\n?\n</literal>")
  8875. ("L" "#+latex: "
  8876. "<literal style=\"latex\">?</literal>")
  8877. ("h" "#+begin_html\n?\n#+end_html"
  8878. "<literal style=\"html\">\n?\n</literal>")
  8879. ("H" "#+html: "
  8880. "<literal style=\"html\">?</literal>")
  8881. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8882. ("A" "#+ascii: ")
  8883. ("i" "#+include %file ?"
  8884. "<include file=%file markup=\"?\">")
  8885. )
  8886. "Structure completion elements.
  8887. This is a list of abbreviation keys and values. The value gets inserted
  8888. it you type @samp{.} followed by the key and then the completion key,
  8889. usually `M-TAB'. %file will be replaced by a file name after prompting
  8890. for the file using completion.
  8891. There are two templates for each key, the first uses the original Org syntax,
  8892. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8893. the default when the /org-mtags.el/ module has been loaded. See also the
  8894. variable `org-mtags-prefer-muse-templates'.
  8895. This is an experimental feature, it is undecided if it is going to stay in."
  8896. :group 'org-completion
  8897. :type '(repeat
  8898. (string :tag "Key")
  8899. (string :tag "Template")
  8900. (string :tag "Muse Template")))
  8901. (defun org-try-structure-completion ()
  8902. "Try to complete a structure template before point.
  8903. This looks for strings like \"<e\" on an otherwise empty line and
  8904. expands them."
  8905. (let ((l (buffer-substring (point-at-bol) (point)))
  8906. a)
  8907. (when (and (looking-at "[ \t]*$")
  8908. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8909. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8910. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8911. (match-beginning 1)) a)
  8912. t)))
  8913. (defun org-complete-expand-structure-template (start cell)
  8914. "Expand a structure template."
  8915. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8916. (rpl (nth (if musep 2 1) cell))
  8917. (ind ""))
  8918. (delete-region start (point))
  8919. (when (string-match "\\`#\\+" rpl)
  8920. (cond
  8921. ((bolp))
  8922. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8923. (setq ind (buffer-substring (point-at-bol) (point))))
  8924. (t (newline))))
  8925. (setq start (point))
  8926. (if (string-match "%file" rpl)
  8927. (setq rpl (replace-match
  8928. (concat
  8929. "\""
  8930. (save-match-data
  8931. (abbreviate-file-name (read-file-name "Include file: ")))
  8932. "\"")
  8933. t t rpl)))
  8934. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  8935. (concat "\n" ind)))
  8936. (insert rpl)
  8937. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8938. (defun org-complete (&optional arg)
  8939. "Perform completion on word at point.
  8940. At the beginning of a headline, this completes TODO keywords as given in
  8941. `org-todo-keywords'.
  8942. If the current word is preceded by a backslash, completes the TeX symbols
  8943. that are supported for HTML support.
  8944. If the current word is preceded by \"#+\", completes special words for
  8945. setting file options.
  8946. In the line after \"#+STARTUP:, complete valid keywords.\"
  8947. At all other locations, this simply calls the value of
  8948. `org-completion-fallback-command'."
  8949. (interactive "P")
  8950. (org-without-partial-completion
  8951. (catch 'exit
  8952. (let* ((a nil)
  8953. (end (point))
  8954. (beg1 (save-excursion
  8955. (skip-chars-backward (org-re "[:alnum:]_@"))
  8956. (point)))
  8957. (beg (save-excursion
  8958. (skip-chars-backward "a-zA-Z0-9_:$")
  8959. (point)))
  8960. (confirm (lambda (x) (stringp (car x))))
  8961. (searchhead (equal (char-before beg) ?*))
  8962. (struct
  8963. (when (and (member (char-before beg1) '(?. ?<))
  8964. (setq a (assoc (buffer-substring beg1 (point))
  8965. org-structure-template-alist)))
  8966. (org-complete-expand-structure-template (1- beg1) a)
  8967. (throw 'exit t)))
  8968. (tag (and (equal (char-before beg1) ?:)
  8969. (equal (char-after (point-at-bol)) ?*)))
  8970. (prop (and (equal (char-before beg1) ?:)
  8971. (not (equal (char-after (point-at-bol)) ?*))))
  8972. (texp (equal (char-before beg) ?\\))
  8973. (link (equal (char-before beg) ?\[))
  8974. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8975. beg)
  8976. "#+"))
  8977. (startup (string-match "^#\\+STARTUP:.*"
  8978. (buffer-substring (point-at-bol) (point))))
  8979. (completion-ignore-case opt)
  8980. (type nil)
  8981. (tbl nil)
  8982. (table (cond
  8983. (opt
  8984. (setq type :opt)
  8985. (require 'org-exp)
  8986. (append
  8987. (delq nil
  8988. (mapcar
  8989. (lambda (x)
  8990. (if (string-match
  8991. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8992. (cons (match-string 2 x)
  8993. (match-string 1 x))))
  8994. (org-split-string (org-get-current-options) "\n")))
  8995. (mapcar 'list org-additional-option-like-keywords)))
  8996. (startup
  8997. (setq type :startup)
  8998. org-startup-options)
  8999. (link (append org-link-abbrev-alist-local
  9000. org-link-abbrev-alist))
  9001. (texp
  9002. (setq type :tex)
  9003. org-html-entities)
  9004. ((string-match "\\`\\*+[ \t]+\\'"
  9005. (buffer-substring (point-at-bol) beg))
  9006. (setq type :todo)
  9007. (mapcar 'list org-todo-keywords-1))
  9008. (searchhead
  9009. (setq type :searchhead)
  9010. (save-excursion
  9011. (goto-char (point-min))
  9012. (while (re-search-forward org-todo-line-regexp nil t)
  9013. (push (list
  9014. (org-make-org-heading-search-string
  9015. (match-string 3) t))
  9016. tbl)))
  9017. tbl)
  9018. (tag (setq type :tag beg beg1)
  9019. (or org-tag-alist (org-get-buffer-tags)))
  9020. (prop (setq type :prop beg beg1)
  9021. (mapcar 'list (org-buffer-property-keys nil t t)))
  9022. (t (progn
  9023. (call-interactively org-completion-fallback-command)
  9024. (throw 'exit nil)))))
  9025. (pattern (buffer-substring-no-properties beg end))
  9026. (completion (try-completion pattern table confirm)))
  9027. (cond ((eq completion t)
  9028. (if (not (assoc (upcase pattern) table))
  9029. (message "Already complete")
  9030. (if (and (equal type :opt)
  9031. (not (member (car (assoc (upcase pattern) table))
  9032. org-additional-option-like-keywords)))
  9033. (insert (substring (cdr (assoc (upcase pattern) table))
  9034. (length pattern)))
  9035. (if (memq type '(:tag :prop)) (insert ":")))))
  9036. ((null completion)
  9037. (message "Can't find completion for \"%s\"" pattern)
  9038. (ding))
  9039. ((not (string= pattern completion))
  9040. (delete-region beg end)
  9041. (if (string-match " +$" completion)
  9042. (setq completion (replace-match "" t t completion)))
  9043. (insert completion)
  9044. (if (get-buffer-window "*Completions*")
  9045. (delete-window (get-buffer-window "*Completions*")))
  9046. (if (assoc completion table)
  9047. (if (eq type :todo) (insert " ")
  9048. (if (memq type '(:tag :prop)) (insert ":"))))
  9049. (if (and (equal type :opt) (assoc completion table))
  9050. (message "%s" (substitute-command-keys
  9051. "Press \\[org-complete] again to insert example settings"))))
  9052. (t
  9053. (message "Making completion list...")
  9054. (let ((list (sort (all-completions pattern table confirm)
  9055. 'string<)))
  9056. (with-output-to-temp-buffer "*Completions*"
  9057. (condition-case nil
  9058. ;; Protection needed for XEmacs and emacs 21
  9059. (display-completion-list list pattern)
  9060. (error (display-completion-list list)))))
  9061. (message "Making completion list...%s" "done")))))))
  9062. ;;;; TODO, DEADLINE, Comments
  9063. (defun org-toggle-comment ()
  9064. "Change the COMMENT state of an entry."
  9065. (interactive)
  9066. (save-excursion
  9067. (org-back-to-heading)
  9068. (let (case-fold-search)
  9069. (if (looking-at (concat outline-regexp
  9070. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9071. (replace-match "" t t nil 1)
  9072. (if (looking-at outline-regexp)
  9073. (progn
  9074. (goto-char (match-end 0))
  9075. (insert org-comment-string " ")))))))
  9076. (defvar org-last-todo-state-is-todo nil
  9077. "This is non-nil when the last TODO state change led to a TODO state.
  9078. If the last change removed the TODO tag or switched to DONE, then
  9079. this is nil.")
  9080. (defvar org-setting-tags nil) ; dynamically skipped
  9081. (defun org-parse-local-options (string var)
  9082. "Parse STRING for startup setting relevant for variable VAR."
  9083. (let ((rtn (symbol-value var))
  9084. e opts)
  9085. (save-match-data
  9086. (if (or (not string) (not (string-match "\\S-" string)))
  9087. rtn
  9088. (setq opts (delq nil (mapcar (lambda (x)
  9089. (setq e (assoc x org-startup-options))
  9090. (if (eq (nth 1 e) var) e nil))
  9091. (org-split-string string "[ \t]+"))))
  9092. (if (not opts)
  9093. rtn
  9094. (setq rtn nil)
  9095. (while (setq e (pop opts))
  9096. (if (not (nth 3 e))
  9097. (setq rtn (nth 2 e))
  9098. (if (not (listp rtn)) (setq rtn nil))
  9099. (push (nth 2 e) rtn)))
  9100. rtn)))))
  9101. (defvar org-todo-setup-filter-hook nil
  9102. "Hook for functions that pre-filter todo specs.
  9103. Each function takes a todo spec and returns either `nil' or the spec
  9104. transformed into canonical form." )
  9105. (defvar org-todo-get-default-hook nil
  9106. "Hook for functions that get a default item for todo.
  9107. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9108. `nil' or a string to be used for the todo mark." )
  9109. (defvar org-agenda-headline-snapshot-before-repeat)
  9110. (defun org-todo (&optional arg)
  9111. "Change the TODO state of an item.
  9112. The state of an item is given by a keyword at the start of the heading,
  9113. like
  9114. *** TODO Write paper
  9115. *** DONE Call mom
  9116. The different keywords are specified in the variable `org-todo-keywords'.
  9117. By default the available states are \"TODO\" and \"DONE\".
  9118. So for this example: when the item starts with TODO, it is changed to DONE.
  9119. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9120. DONE are present, add TODO at the beginning of the heading.
  9121. With C-u prefix arg, use completion to determine the new state.
  9122. With numeric prefix arg, switch to that state.
  9123. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  9124. With a triple C-u prefix, circumvent any state blocking.
  9125. For calling through lisp, arg is also interpreted in the following way:
  9126. 'none -> empty state
  9127. \"\"(empty string) -> switch to empty state
  9128. 'done -> switch to DONE
  9129. 'nextset -> switch to the next set of keywords
  9130. 'previousset -> switch to the previous set of keywords
  9131. \"WAITING\" -> switch to the specified keyword, but only if it
  9132. really is a member of `org-todo-keywords'."
  9133. (interactive "P")
  9134. (if (equal arg '(16)) (setq arg 'nextset))
  9135. (let ((org-blocker-hook org-blocker-hook)
  9136. (case-fold-search nil))
  9137. (when (equal arg '(64))
  9138. (setq arg nil org-blocker-hook nil))
  9139. (when (and org-blocker-hook
  9140. (or org-inhibit-blocking
  9141. (org-entry-get nil "NOBLOCKING")))
  9142. (setq org-blocker-hook nil))
  9143. (save-excursion
  9144. (catch 'exit
  9145. (org-back-to-heading t)
  9146. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9147. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9148. (looking-at " *"))
  9149. (let* ((match-data (match-data))
  9150. (startpos (point-at-bol))
  9151. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  9152. (org-log-done org-log-done)
  9153. (org-log-repeat org-log-repeat)
  9154. (org-todo-log-states org-todo-log-states)
  9155. (this (match-string 1))
  9156. (hl-pos (match-beginning 0))
  9157. (head (org-get-todo-sequence-head this))
  9158. (ass (assoc head org-todo-kwd-alist))
  9159. (interpret (nth 1 ass))
  9160. (done-word (nth 3 ass))
  9161. (final-done-word (nth 4 ass))
  9162. (last-state (or this ""))
  9163. (completion-ignore-case t)
  9164. (member (member this org-todo-keywords-1))
  9165. (tail (cdr member))
  9166. (state (cond
  9167. ((and org-todo-key-trigger
  9168. (or (and (equal arg '(4))
  9169. (eq org-use-fast-todo-selection 'prefix))
  9170. (and (not arg) org-use-fast-todo-selection
  9171. (not (eq org-use-fast-todo-selection
  9172. 'prefix)))))
  9173. ;; Use fast selection
  9174. (org-fast-todo-selection))
  9175. ((and (equal arg '(4))
  9176. (or (not org-use-fast-todo-selection)
  9177. (not org-todo-key-trigger)))
  9178. ;; Read a state with completion
  9179. (org-icompleting-read
  9180. "State: " (mapcar (lambda(x) (list x))
  9181. org-todo-keywords-1)
  9182. nil t))
  9183. ((eq arg 'right)
  9184. (if this
  9185. (if tail (car tail) nil)
  9186. (car org-todo-keywords-1)))
  9187. ((eq arg 'left)
  9188. (if (equal member org-todo-keywords-1)
  9189. nil
  9190. (if this
  9191. (nth (- (length org-todo-keywords-1)
  9192. (length tail) 2)
  9193. org-todo-keywords-1)
  9194. (org-last org-todo-keywords-1))))
  9195. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9196. (setq arg nil))) ; hack to fall back to cycling
  9197. (arg
  9198. ;; user or caller requests a specific state
  9199. (cond
  9200. ((equal arg "") nil)
  9201. ((eq arg 'none) nil)
  9202. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9203. ((eq arg 'nextset)
  9204. (or (car (cdr (member head org-todo-heads)))
  9205. (car org-todo-heads)))
  9206. ((eq arg 'previousset)
  9207. (let ((org-todo-heads (reverse org-todo-heads)))
  9208. (or (car (cdr (member head org-todo-heads)))
  9209. (car org-todo-heads))))
  9210. ((car (member arg org-todo-keywords-1)))
  9211. ((stringp arg)
  9212. (error "State `%s' not valid in this file" arg))
  9213. ((nth (1- (prefix-numeric-value arg))
  9214. org-todo-keywords-1))))
  9215. ((null member) (or head (car org-todo-keywords-1)))
  9216. ((equal this final-done-word) nil) ;; -> make empty
  9217. ((null tail) nil) ;; -> first entry
  9218. ((memq interpret '(type priority))
  9219. (if (eq this-command last-command)
  9220. (car tail)
  9221. (if (> (length tail) 0)
  9222. (or done-word (car org-done-keywords))
  9223. nil)))
  9224. (t
  9225. (car tail))))
  9226. (state (or
  9227. (run-hook-with-args-until-success
  9228. 'org-todo-get-default-hook state last-state)
  9229. state))
  9230. (next (if state (concat " " state " ") " "))
  9231. (change-plist (list :type 'todo-state-change :from this :to state
  9232. :position startpos))
  9233. dolog now-done-p)
  9234. (when org-blocker-hook
  9235. (setq org-last-todo-state-is-todo
  9236. (not (member this org-done-keywords)))
  9237. (unless (save-excursion
  9238. (save-match-data
  9239. (run-hook-with-args-until-failure
  9240. 'org-blocker-hook change-plist)))
  9241. (if (interactive-p)
  9242. (error "TODO state change from %s to %s blocked" this state)
  9243. ;; fail silently
  9244. (message "TODO state change from %s to %s blocked" this state)
  9245. (throw 'exit nil))))
  9246. (store-match-data match-data)
  9247. (replace-match next t t)
  9248. (unless (pos-visible-in-window-p hl-pos)
  9249. (message "TODO state changed to %s" (org-trim next)))
  9250. (unless head
  9251. (setq head (org-get-todo-sequence-head state)
  9252. ass (assoc head org-todo-kwd-alist)
  9253. interpret (nth 1 ass)
  9254. done-word (nth 3 ass)
  9255. final-done-word (nth 4 ass)))
  9256. (when (memq arg '(nextset previousset))
  9257. (message "Keyword-Set %d/%d: %s"
  9258. (- (length org-todo-sets) -1
  9259. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9260. (length org-todo-sets)
  9261. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9262. (setq org-last-todo-state-is-todo
  9263. (not (member state org-done-keywords)))
  9264. (setq now-done-p (and (member state org-done-keywords)
  9265. (not (member this org-done-keywords))))
  9266. (and logging (org-local-logging logging))
  9267. (when (and (or org-todo-log-states org-log-done)
  9268. (not (eq org-inhibit-logging t))
  9269. (not (memq arg '(nextset previousset))))
  9270. ;; we need to look at recording a time and note
  9271. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  9272. (nth 2 (assoc this org-todo-log-states))))
  9273. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  9274. (setq dolog 'time))
  9275. (when (and state
  9276. (member state org-not-done-keywords)
  9277. (not (member this org-not-done-keywords)))
  9278. ;; This is now a todo state and was not one before
  9279. ;; If there was a CLOSED time stamp, get rid of it.
  9280. (org-add-planning-info nil nil 'closed))
  9281. (when (and now-done-p org-log-done)
  9282. ;; It is now done, and it was not done before
  9283. (org-add-planning-info 'closed (org-current-time))
  9284. (if (and (not dolog) (eq 'note org-log-done))
  9285. (org-add-log-setup 'done state this 'findpos 'note)))
  9286. (when (and state dolog)
  9287. ;; This is a non-nil state, and we need to log it
  9288. (org-add-log-setup 'state state this 'findpos dolog)))
  9289. ;; Fixup tag positioning
  9290. (org-todo-trigger-tag-changes state)
  9291. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  9292. (when org-provide-todo-statistics
  9293. (org-update-parent-todo-statistics))
  9294. (run-hooks 'org-after-todo-state-change-hook)
  9295. (if (and arg (not (member state org-done-keywords)))
  9296. (setq head (org-get-todo-sequence-head state)))
  9297. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  9298. ;; Do we need to trigger a repeat?
  9299. (when now-done-p
  9300. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  9301. ;; This is for the agenda, take a snapshot of the headline.
  9302. (save-match-data
  9303. (setq org-agenda-headline-snapshot-before-repeat
  9304. (org-get-heading))))
  9305. (org-auto-repeat-maybe state))
  9306. ;; Fixup cursor location if close to the keyword
  9307. (if (and (outline-on-heading-p)
  9308. (not (bolp))
  9309. (save-excursion (beginning-of-line 1)
  9310. (looking-at org-todo-line-regexp))
  9311. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  9312. (progn
  9313. (goto-char (or (match-end 2) (match-end 1)))
  9314. (and (looking-at " ") (just-one-space))))
  9315. (when org-trigger-hook
  9316. (save-excursion
  9317. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  9318. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  9319. "Block turning an entry into a TODO, using the hierarchy.
  9320. This checks whether the current task should be blocked from state
  9321. changes. Such blocking occurs when:
  9322. 1. The task has children which are not all in a completed state.
  9323. 2. A task has a parent with the property :ORDERED:, and there
  9324. are siblings prior to the current task with incomplete
  9325. status.
  9326. 3. The parent of the task is blocked because it has siblings that should
  9327. be done first, or is child of a block grandparent TODO entry."
  9328. (if (not org-enforce-todo-dependencies)
  9329. t ; if locally turned off don't block
  9330. (catch 'dont-block
  9331. ;; If this is not a todo state change, or if this entry is already DONE,
  9332. ;; do not block
  9333. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9334. (member (plist-get change-plist :from)
  9335. (cons 'done org-done-keywords))
  9336. (member (plist-get change-plist :to)
  9337. (cons 'todo org-not-done-keywords))
  9338. (not (plist-get change-plist :to)))
  9339. (throw 'dont-block t))
  9340. ;; If this task has children, and any are undone, it's blocked
  9341. (save-excursion
  9342. (org-back-to-heading t)
  9343. (let ((this-level (funcall outline-level)))
  9344. (outline-next-heading)
  9345. (let ((child-level (funcall outline-level)))
  9346. (while (and (not (eobp))
  9347. (> child-level this-level))
  9348. ;; this todo has children, check whether they are all
  9349. ;; completed
  9350. (if (and (not (org-entry-is-done-p))
  9351. (org-entry-is-todo-p))
  9352. (throw 'dont-block nil))
  9353. (outline-next-heading)
  9354. (setq child-level (funcall outline-level))))))
  9355. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  9356. ;; any previous siblings are undone, it's blocked
  9357. (save-excursion
  9358. (org-back-to-heading t)
  9359. (let* ((pos (point))
  9360. (parent-pos (and (org-up-heading-safe) (point))))
  9361. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9362. (when (and (org-entry-get (point) "ORDERED")
  9363. (forward-line 1)
  9364. (re-search-forward org-not-done-heading-regexp pos t))
  9365. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  9366. ;; Search further up the hierarchy, to see if an anchestor is blocked
  9367. (while t
  9368. (goto-char parent-pos)
  9369. (if (not (looking-at org-not-done-heading-regexp))
  9370. (throw 'dont-block t)) ; do not block, parent is not a TODO
  9371. (setq pos (point))
  9372. (setq parent-pos (and (org-up-heading-safe) (point)))
  9373. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9374. (when (and (org-entry-get (point) "ORDERED")
  9375. (forward-line 1)
  9376. (re-search-forward org-not-done-heading-regexp pos t))
  9377. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  9378. (defcustom org-track-ordered-property-with-tag nil
  9379. "Should the ORDERED property also be shown as a tag?
  9380. The ORDERED property decides if an entry should require subtasks to be
  9381. completed in sequence. Since a property is not very visible, setting
  9382. this option means that toggling the ORDERED property with the command
  9383. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  9384. not relevant for the behavior, but it makes things more visible.
  9385. Note that toggling the tag with tags commands will not change the property
  9386. and therefore not influence behavior!
  9387. This can be t, meaning the tag ORDERED should be used, It can also be a
  9388. string to select a different tag for this task."
  9389. :group 'org-todo
  9390. :type '(choice
  9391. (const :tag "No tracking" nil)
  9392. (const :tag "Track with ORDERED tag" t)
  9393. (string :tag "Use other tag")))
  9394. (defun org-toggle-ordered-property ()
  9395. "Toggle the ORDERED property of the current entry.
  9396. For better visibility, you can track the value of this property with a tag.
  9397. See variable `org-track-ordered-property-with-tag'."
  9398. (interactive)
  9399. (let* ((t1 org-track-ordered-property-with-tag)
  9400. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  9401. (save-excursion
  9402. (org-back-to-heading)
  9403. (if (org-entry-get nil "ORDERED")
  9404. (progn
  9405. (org-delete-property "ORDERED")
  9406. (and tag (org-toggle-tag tag 'off))
  9407. (message "Subtasks can be completed in arbitrary order"))
  9408. (org-entry-put nil "ORDERED" "t")
  9409. (and tag (org-toggle-tag tag 'on))
  9410. (message "Subtasks must be completed in sequence")))))
  9411. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  9412. (defun org-block-todo-from-checkboxes (change-plist)
  9413. "Block turning an entry into a TODO, using checkboxes.
  9414. This checks whether the current task should be blocked from state
  9415. changes because there are unchecked boxes in this entry."
  9416. (if (not org-enforce-todo-checkbox-dependencies)
  9417. t ; if locally turned off don't block
  9418. (catch 'dont-block
  9419. ;; If this is not a todo state change, or if this entry is already DONE,
  9420. ;; do not block
  9421. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9422. (member (plist-get change-plist :from)
  9423. (cons 'done org-done-keywords))
  9424. (member (plist-get change-plist :to)
  9425. (cons 'todo org-not-done-keywords))
  9426. (not (plist-get change-plist :to)))
  9427. (throw 'dont-block t))
  9428. ;; If this task has checkboxes that are not checked, it's blocked
  9429. (save-excursion
  9430. (org-back-to-heading t)
  9431. (let ((beg (point)) end)
  9432. (outline-next-heading)
  9433. (setq end (point))
  9434. (goto-char beg)
  9435. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  9436. end t)
  9437. (progn
  9438. (if (boundp 'org-blocked-by-checkboxes)
  9439. (setq org-blocked-by-checkboxes t))
  9440. (throw 'dont-block nil)))))
  9441. t))) ; do not block
  9442. (defun org-entry-blocked-p ()
  9443. "Is the current entry blocked?"
  9444. (if (org-entry-get nil "NOBLOCKING")
  9445. nil ;; Never block this entry
  9446. (not
  9447. (run-hook-with-args-until-failure
  9448. 'org-blocker-hook
  9449. (list :type 'todo-state-change
  9450. :position (point)
  9451. :from 'todo
  9452. :to 'done)))))
  9453. (defun org-update-statistics-cookies (all)
  9454. "Update the statistics cookie, either from TODO or from checkboxes.
  9455. This should be called with the cursor in a line with a statistics cookie."
  9456. (interactive "P")
  9457. (if all
  9458. (progn
  9459. (org-update-checkbox-count 'all)
  9460. (org-map-entries 'org-update-parent-todo-statistics))
  9461. (if (not (org-on-heading-p))
  9462. (org-update-checkbox-count)
  9463. (let ((pos (move-marker (make-marker) (point)))
  9464. end l1 l2)
  9465. (ignore-errors (org-back-to-heading t))
  9466. (if (not (org-on-heading-p))
  9467. (org-update-checkbox-count)
  9468. (setq l1 (org-outline-level))
  9469. (setq end (save-excursion
  9470. (outline-next-heading)
  9471. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  9472. (point)))
  9473. (if (and (save-excursion
  9474. (re-search-forward
  9475. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  9476. (not (save-excursion (re-search-forward
  9477. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  9478. (org-update-checkbox-count)
  9479. (if (and l2 (> l2 l1))
  9480. (progn
  9481. (goto-char end)
  9482. (org-update-parent-todo-statistics))
  9483. (goto-char pos)
  9484. (beginning-of-line 1)
  9485. (while (re-search-forward
  9486. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  9487. (point-at-eol) t)
  9488. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  9489. (goto-char pos)
  9490. (move-marker pos nil)))))
  9491. (defvar org-entry-property-inherited-from) ;; defined below
  9492. (defun org-update-parent-todo-statistics ()
  9493. "Update any statistics cookie in the parent of the current headline.
  9494. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  9495. the entire subtree and this will travel up the hierarchy and update
  9496. statistics everywhere."
  9497. (interactive)
  9498. (let* ((lim 0) prop
  9499. (recursive (or (not org-hierarchical-todo-statistics)
  9500. (string-match
  9501. "\\<recursive\\>"
  9502. (or (setq prop (org-entry-get
  9503. nil "COOKIE_DATA" 'inherit)) ""))))
  9504. (lim (or (and prop (marker-position
  9505. org-entry-property-inherited-from))
  9506. lim))
  9507. (first t)
  9508. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  9509. level ltoggle l1 new ndel
  9510. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  9511. (catch 'exit
  9512. (save-excursion
  9513. (beginning-of-line 1)
  9514. (if (org-at-heading-p)
  9515. (setq ltoggle (funcall outline-level))
  9516. (error "This should not happen"))
  9517. (while (and (setq level (org-up-heading-safe))
  9518. (or recursive first)
  9519. (>= (point) lim))
  9520. (setq first nil cookie-present nil)
  9521. (unless (and level
  9522. (not (string-match
  9523. "\\<checkbox\\>"
  9524. (downcase
  9525. (or (org-entry-get
  9526. nil "COOKIE_DATA")
  9527. "")))))
  9528. (throw 'exit nil))
  9529. (while (re-search-forward box-re (point-at-eol) t)
  9530. (setq cnt-all 0 cnt-done 0 cookie-present t)
  9531. (setq is-percent (match-end 2))
  9532. (save-match-data
  9533. (unless (outline-next-heading) (throw 'exit nil))
  9534. (while (and (looking-at org-complex-heading-regexp)
  9535. (> (setq l1 (length (match-string 1))) level))
  9536. (setq kwd (and (or recursive (= l1 ltoggle))
  9537. (match-string 2)))
  9538. (if (or (eq org-provide-todo-statistics 'all-headlines)
  9539. (and (listp org-provide-todo-statistics)
  9540. (or (member kwd org-provide-todo-statistics)
  9541. (member kwd org-done-keywords))))
  9542. (setq cnt-all (1+ cnt-all))
  9543. (if (eq org-provide-todo-statistics t)
  9544. (and kwd (setq cnt-all (1+ cnt-all)))))
  9545. (and (member kwd org-done-keywords)
  9546. (setq cnt-done (1+ cnt-done)))
  9547. (outline-next-heading)))
  9548. (setq new
  9549. (if is-percent
  9550. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  9551. (format "[%d/%d]" cnt-done cnt-all))
  9552. ndel (- (match-end 0) (match-beginning 0)))
  9553. (goto-char (match-beginning 0))
  9554. (insert new)
  9555. (delete-region (point) (+ (point) ndel)))
  9556. (when cookie-present
  9557. (run-hook-with-args 'org-after-todo-statistics-hook
  9558. cnt-done (- cnt-all cnt-done))))))
  9559. (run-hooks 'org-todo-statistics-hook)))
  9560. (defvar org-after-todo-statistics-hook nil
  9561. "Hook that is called after a TODO statistics cookie has been updated.
  9562. Each function is called with two arguments: the number of not-done entries
  9563. and the number of done entries.
  9564. For example, the following function, when added to this hook, will switch
  9565. an entry to DONE when all children are done, and back to TODO when new
  9566. entries are set to a TODO status. Note that this hook is only called
  9567. when there is a statistics cookie in the headline!
  9568. (defun org-summary-todo (n-done n-not-done)
  9569. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  9570. (let (org-log-done org-log-states) ; turn off logging
  9571. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  9572. ")
  9573. (defvar org-todo-statistics-hook nil
  9574. "Hook that is run whenever Org thinks TODO statistics should be updated.
  9575. This hook runs even if there is no statistics cookie present, in which case
  9576. `org-after-todo-statistics-hook' would not run.")
  9577. (defun org-todo-trigger-tag-changes (state)
  9578. "Apply the changes defined in `org-todo-state-tags-triggers'."
  9579. (let ((l org-todo-state-tags-triggers)
  9580. changes)
  9581. (when (or (not state) (equal state ""))
  9582. (setq changes (append changes (cdr (assoc "" l)))))
  9583. (when (and (stringp state) (> (length state) 0))
  9584. (setq changes (append changes (cdr (assoc state l)))))
  9585. (when (member state org-not-done-keywords)
  9586. (setq changes (append changes (cdr (assoc 'todo l)))))
  9587. (when (member state org-done-keywords)
  9588. (setq changes (append changes (cdr (assoc 'done l)))))
  9589. (dolist (c changes)
  9590. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  9591. (defun org-local-logging (value)
  9592. "Get logging settings from a property VALUE."
  9593. (let* (words w a)
  9594. ;; directly set the variables, they are already local.
  9595. (setq org-log-done nil
  9596. org-log-repeat nil
  9597. org-todo-log-states nil)
  9598. (setq words (org-split-string value))
  9599. (while (setq w (pop words))
  9600. (cond
  9601. ((setq a (assoc w org-startup-options))
  9602. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  9603. (set (nth 1 a) (nth 2 a))))
  9604. ((setq a (org-extract-log-state-settings w))
  9605. (and (member (car a) org-todo-keywords-1)
  9606. (push a org-todo-log-states)))))))
  9607. (defun org-get-todo-sequence-head (kwd)
  9608. "Return the head of the TODO sequence to which KWD belongs.
  9609. If KWD is not set, check if there is a text property remembering the
  9610. right sequence."
  9611. (let (p)
  9612. (cond
  9613. ((not kwd)
  9614. (or (get-text-property (point-at-bol) 'org-todo-head)
  9615. (progn
  9616. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  9617. nil (point-at-eol)))
  9618. (get-text-property p 'org-todo-head))))
  9619. ((not (member kwd org-todo-keywords-1))
  9620. (car org-todo-keywords-1))
  9621. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  9622. (defun org-fast-todo-selection ()
  9623. "Fast TODO keyword selection with single keys.
  9624. Returns the new TODO keyword, or nil if no state change should occur."
  9625. (let* ((fulltable org-todo-key-alist)
  9626. (done-keywords org-done-keywords) ;; needed for the faces.
  9627. (maxlen (apply 'max (mapcar
  9628. (lambda (x)
  9629. (if (stringp (car x)) (string-width (car x)) 0))
  9630. fulltable)))
  9631. (expert nil)
  9632. (fwidth (+ maxlen 3 1 3))
  9633. (ncol (/ (- (window-width) 4) fwidth))
  9634. tg cnt e c tbl
  9635. groups ingroup)
  9636. (save-excursion
  9637. (save-window-excursion
  9638. (if expert
  9639. (set-buffer (get-buffer-create " *Org todo*"))
  9640. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  9641. (erase-buffer)
  9642. (org-set-local 'org-done-keywords done-keywords)
  9643. (setq tbl fulltable cnt 0)
  9644. (while (setq e (pop tbl))
  9645. (cond
  9646. ((equal e '(:startgroup))
  9647. (push '() groups) (setq ingroup t)
  9648. (when (not (= cnt 0))
  9649. (setq cnt 0)
  9650. (insert "\n"))
  9651. (insert "{ "))
  9652. ((equal e '(:endgroup))
  9653. (setq ingroup nil cnt 0)
  9654. (insert "}\n"))
  9655. ((equal e '(:newline))
  9656. (when (not (= cnt 0))
  9657. (setq cnt 0)
  9658. (insert "\n")
  9659. (setq e (car tbl))
  9660. (while (equal (car tbl) '(:newline))
  9661. (insert "\n")
  9662. (setq tbl (cdr tbl)))))
  9663. (t
  9664. (setq tg (car e) c (cdr e))
  9665. (if ingroup (push tg (car groups)))
  9666. (setq tg (org-add-props tg nil 'face
  9667. (org-get-todo-face tg)))
  9668. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9669. (insert "[" c "] " tg (make-string
  9670. (- fwidth 4 (length tg)) ?\ ))
  9671. (when (= (setq cnt (1+ cnt)) ncol)
  9672. (insert "\n")
  9673. (if ingroup (insert " "))
  9674. (setq cnt 0)))))
  9675. (insert "\n")
  9676. (goto-char (point-min))
  9677. (if (not expert) (org-fit-window-to-buffer))
  9678. (message "[a-z..]:Set [SPC]:clear")
  9679. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9680. (cond
  9681. ((or (= c ?\C-g)
  9682. (and (= c ?q) (not (rassoc c fulltable))))
  9683. (setq quit-flag t))
  9684. ((= c ?\ ) nil)
  9685. ((setq e (rassoc c fulltable) tg (car e))
  9686. tg)
  9687. (t (setq quit-flag t)))))))
  9688. (defun org-entry-is-todo-p ()
  9689. (member (org-get-todo-state) org-not-done-keywords))
  9690. (defun org-entry-is-done-p ()
  9691. (member (org-get-todo-state) org-done-keywords))
  9692. (defun org-get-todo-state ()
  9693. (save-excursion
  9694. (org-back-to-heading t)
  9695. (and (looking-at org-todo-line-regexp)
  9696. (match-end 2)
  9697. (match-string 2))))
  9698. (defun org-at-date-range-p (&optional inactive-ok)
  9699. "Is the cursor inside a date range?"
  9700. (interactive)
  9701. (save-excursion
  9702. (catch 'exit
  9703. (let ((pos (point)))
  9704. (skip-chars-backward "^[<\r\n")
  9705. (skip-chars-backward "<[")
  9706. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9707. (>= (match-end 0) pos)
  9708. (throw 'exit t))
  9709. (skip-chars-backward "^<[\r\n")
  9710. (skip-chars-backward "<[")
  9711. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9712. (>= (match-end 0) pos)
  9713. (throw 'exit t)))
  9714. nil)))
  9715. (defun org-get-repeat (&optional tagline)
  9716. "Check if there is a deadline/schedule with repeater in this entry."
  9717. (save-match-data
  9718. (save-excursion
  9719. (org-back-to-heading t)
  9720. (and (re-search-forward (if tagline
  9721. (concat tagline "\\s-*" org-repeat-re)
  9722. org-repeat-re)
  9723. (org-entry-end-position) t)
  9724. (match-string-no-properties 1)))))
  9725. (defvar org-last-changed-timestamp)
  9726. (defvar org-last-inserted-timestamp)
  9727. (defvar org-log-post-message)
  9728. (defvar org-log-note-purpose)
  9729. (defvar org-log-note-how)
  9730. (defvar org-log-note-extra)
  9731. (defun org-auto-repeat-maybe (done-word)
  9732. "Check if the current headline contains a repeated deadline/schedule.
  9733. If yes, set TODO state back to what it was and change the base date
  9734. of repeating deadline/scheduled time stamps to new date.
  9735. This function is run automatically after each state change to a DONE state."
  9736. ;; last-state is dynamically scoped into this function
  9737. (let* ((repeat (org-get-repeat))
  9738. (aa (assoc last-state org-todo-kwd-alist))
  9739. (interpret (nth 1 aa))
  9740. (head (nth 2 aa))
  9741. (whata '(("d" . day) ("m" . month) ("y" . year)))
  9742. (msg "Entry repeats: ")
  9743. (org-log-done nil)
  9744. (org-todo-log-states nil)
  9745. (nshiftmax 10) (nshift 0)
  9746. re type n what ts time)
  9747. (when repeat
  9748. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  9749. (org-todo (if (eq interpret 'type) last-state head))
  9750. (org-entry-put nil "LAST_REPEAT" (format-time-string
  9751. (org-time-stamp-format t t)))
  9752. (when org-log-repeat
  9753. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9754. (memq 'org-add-log-note post-command-hook))
  9755. ;; OK, we are already setup for some record
  9756. (if (eq org-log-repeat 'note)
  9757. ;; make sure we take a note, not only a time stamp
  9758. (setq org-log-note-how 'note))
  9759. ;; Set up for taking a record
  9760. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  9761. last-state
  9762. 'findpos org-log-repeat)))
  9763. (org-back-to-heading t)
  9764. (org-add-planning-info nil nil 'closed)
  9765. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  9766. org-deadline-time-regexp "\\)\\|\\("
  9767. org-ts-regexp "\\)"))
  9768. (while (re-search-forward
  9769. re (save-excursion (outline-next-heading) (point)) t)
  9770. (setq type (if (match-end 1) org-scheduled-string
  9771. (if (match-end 3) org-deadline-string "Plain:"))
  9772. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9773. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9774. (setq n (string-to-number (match-string 2 ts))
  9775. what (match-string 3 ts))
  9776. (if (equal what "w") (setq n (* n 7) what "d"))
  9777. ;; Preparation, see if we need to modify the start date for the change
  9778. (when (match-end 1)
  9779. (setq time (save-match-data (org-time-string-to-time ts)))
  9780. (cond
  9781. ((equal (match-string 1 ts) ".")
  9782. ;; Shift starting date to today
  9783. (org-timestamp-change
  9784. (- (time-to-days (current-time)) (time-to-days time))
  9785. 'day))
  9786. ((equal (match-string 1 ts) "+")
  9787. (while (or (= nshift 0)
  9788. (<= (time-to-days time) (time-to-days (current-time))))
  9789. (when (= (incf nshift) nshiftmax)
  9790. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9791. (error "Abort")))
  9792. (org-timestamp-change n (cdr (assoc what whata)))
  9793. (org-at-timestamp-p t)
  9794. (setq ts (match-string 1))
  9795. (setq time (save-match-data (org-time-string-to-time ts))))
  9796. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9797. ;; rematch, so that we have everything in place for the real shift
  9798. (org-at-timestamp-p t)
  9799. (setq ts (match-string 1))
  9800. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9801. (org-timestamp-change n (cdr (assoc what whata)))
  9802. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9803. (setq org-log-post-message msg)
  9804. (message "%s" msg))))
  9805. (defun org-show-todo-tree (arg)
  9806. "Make a compact tree which shows all headlines marked with TODO.
  9807. The tree will show the lines where the regexp matches, and all higher
  9808. headlines above the match.
  9809. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9810. With a numeric prefix N, construct a sparse tree for the Nth element
  9811. of `org-todo-keywords-1'."
  9812. (interactive "P")
  9813. (let ((case-fold-search nil)
  9814. (kwd-re
  9815. (cond ((null arg) org-not-done-regexp)
  9816. ((equal arg '(4))
  9817. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  9818. (mapcar 'list org-todo-keywords-1))))
  9819. (concat "\\("
  9820. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9821. "\\)\\>")))
  9822. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9823. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9824. org-todo-keywords-1)))
  9825. (t (error "Invalid prefix argument: %s" arg)))))
  9826. (message "%d TODO entries found"
  9827. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9828. (defun org-deadline (&optional remove time)
  9829. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9830. With argument REMOVE, remove any deadline from the item.
  9831. When TIME is set, it should be an internal time specification, and the
  9832. scheduling will use the corresponding date."
  9833. (interactive "P")
  9834. (let* ((old-date (org-entry-get nil "DEADLINE"))
  9835. (repeater (and old-date
  9836. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  9837. (match-string 1 old-date))))
  9838. (if remove
  9839. (progn
  9840. (when (and old-date org-log-redeadline)
  9841. (org-add-log-setup 'deldeadline nil old-date 'findpos
  9842. org-log-redeadline))
  9843. (org-remove-timestamp-with-keyword org-deadline-string)
  9844. (message "Item no longer has a deadline."))
  9845. (org-add-planning-info 'deadline time 'closed)
  9846. (when (and old-date org-log-redeadline
  9847. (not (equal old-date
  9848. (substring org-last-inserted-timestamp 1 -1))))
  9849. (org-add-log-setup 'redeadline nil old-date 'findpos
  9850. org-log-redeadline))
  9851. (when repeater
  9852. (save-excursion
  9853. (org-back-to-heading t)
  9854. (when (re-search-forward (concat org-deadline-string " "
  9855. org-last-inserted-timestamp)
  9856. (save-excursion
  9857. (outline-next-heading) (point)) t)
  9858. (goto-char (1- (match-end 0)))
  9859. (insert " " repeater)
  9860. (setq org-last-inserted-timestamp
  9861. (concat (substring org-last-inserted-timestamp 0 -1)
  9862. " " repeater
  9863. (substring org-last-inserted-timestamp -1))))))
  9864. (message "Deadline on %s" org-last-inserted-timestamp))))
  9865. (defun org-schedule (&optional remove time)
  9866. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9867. With argument REMOVE, remove any scheduling date from the item.
  9868. When TIME is set, it should be an internal time specification, and the
  9869. scheduling will use the corresponding date."
  9870. (interactive "P")
  9871. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  9872. (repeater (and old-date
  9873. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  9874. (match-string 1 old-date))))
  9875. (if remove
  9876. (progn
  9877. (when (and old-date org-log-reschedule)
  9878. (org-add-log-setup 'delschedule nil old-date 'findpos
  9879. org-log-reschedule))
  9880. (org-remove-timestamp-with-keyword org-scheduled-string)
  9881. (message "Item is no longer scheduled."))
  9882. (org-add-planning-info 'scheduled time 'closed)
  9883. (when (and old-date org-log-reschedule
  9884. (not (equal old-date
  9885. (substring org-last-inserted-timestamp 1 -1))))
  9886. (org-add-log-setup 'reschedule nil old-date 'findpos
  9887. org-log-reschedule))
  9888. (when repeater
  9889. (save-excursion
  9890. (org-back-to-heading t)
  9891. (when (re-search-forward (concat org-scheduled-string " "
  9892. org-last-inserted-timestamp)
  9893. (save-excursion
  9894. (outline-next-heading) (point)) t)
  9895. (goto-char (1- (match-end 0)))
  9896. (insert " " repeater)
  9897. (setq org-last-inserted-timestamp
  9898. (concat (substring org-last-inserted-timestamp 0 -1)
  9899. " " repeater
  9900. (substring org-last-inserted-timestamp -1))))))
  9901. (message "Scheduled to %s" org-last-inserted-timestamp))))
  9902. (defun org-get-scheduled-time (pom &optional inherit)
  9903. "Get the scheduled time as a time tuple, of a format suitable
  9904. for calling org-schedule with, or if there is no scheduling,
  9905. returns nil."
  9906. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  9907. (when time
  9908. (apply 'encode-time (org-parse-time-string time)))))
  9909. (defun org-get-deadline-time (pom &optional inherit)
  9910. "Get the deadine as a time tuple, of a format suitable for
  9911. calling org-deadline with, or if there is no scheduling, returns
  9912. nil."
  9913. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  9914. (when time
  9915. (apply 'encode-time (org-parse-time-string time)))))
  9916. (defun org-remove-timestamp-with-keyword (keyword)
  9917. "Remove all time stamps with KEYWORD in the current entry."
  9918. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  9919. beg)
  9920. (save-excursion
  9921. (org-back-to-heading t)
  9922. (setq beg (point))
  9923. (outline-next-heading)
  9924. (while (re-search-backward re beg t)
  9925. (replace-match "")
  9926. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  9927. (equal (char-before) ?\ ))
  9928. (backward-delete-char 1)
  9929. (if (string-match "^[ \t]*$" (buffer-substring
  9930. (point-at-bol) (point-at-eol)))
  9931. (delete-region (point-at-bol)
  9932. (min (point-max) (1+ (point-at-eol))))))))))
  9933. (defun org-add-planning-info (what &optional time &rest remove)
  9934. "Insert new timestamp with keyword in the line directly after the headline.
  9935. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  9936. If non is given, the user is prompted for a date.
  9937. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  9938. be removed."
  9939. (interactive)
  9940. (let (org-time-was-given org-end-time-was-given ts
  9941. end default-time default-input)
  9942. (catch 'exit
  9943. (when (and (not time) (memq what '(scheduled deadline)))
  9944. ;; Try to get a default date/time from existing timestamp
  9945. (save-excursion
  9946. (org-back-to-heading t)
  9947. (setq end (save-excursion (outline-next-heading) (point)))
  9948. (when (re-search-forward (if (eq what 'scheduled)
  9949. org-scheduled-time-regexp
  9950. org-deadline-time-regexp)
  9951. end t)
  9952. (setq ts (match-string 1)
  9953. default-time
  9954. (apply 'encode-time (org-parse-time-string ts))
  9955. default-input (and ts (org-get-compact-tod ts))))))
  9956. (when what
  9957. ;; If necessary, get the time from the user
  9958. (setq time (or time (org-read-date nil 'to-time nil nil
  9959. default-time default-input))))
  9960. (when (and org-insert-labeled-timestamps-at-point
  9961. (member what '(scheduled deadline)))
  9962. (insert
  9963. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  9964. (org-insert-time-stamp time org-time-was-given
  9965. nil nil nil (list org-end-time-was-given))
  9966. (setq what nil))
  9967. (save-excursion
  9968. (save-restriction
  9969. (let (col list elt ts buffer-invisibility-spec)
  9970. (org-back-to-heading t)
  9971. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  9972. (goto-char (match-end 1))
  9973. (setq col (current-column))
  9974. (goto-char (match-end 0))
  9975. (if (eobp) (insert "\n") (forward-char 1))
  9976. (when (and (not what)
  9977. (not (looking-at
  9978. (concat "[ \t]*"
  9979. org-keyword-time-not-clock-regexp))))
  9980. ;; Nothing to add, nothing to remove...... :-)
  9981. (throw 'exit nil))
  9982. (if (and (not (looking-at outline-regexp))
  9983. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  9984. "[^\r\n]*"))
  9985. (not (equal (match-string 1) org-clock-string)))
  9986. (narrow-to-region (match-beginning 0) (match-end 0))
  9987. (insert-before-markers "\n")
  9988. (backward-char 1)
  9989. (narrow-to-region (point) (point))
  9990. (and org-adapt-indentation (org-indent-to-column col)))
  9991. ;; Check if we have to remove something.
  9992. (setq list (cons what remove))
  9993. (while list
  9994. (setq elt (pop list))
  9995. (goto-char (point-min))
  9996. (when (or (and (eq elt 'scheduled)
  9997. (re-search-forward org-scheduled-time-regexp nil t))
  9998. (and (eq elt 'deadline)
  9999. (re-search-forward org-deadline-time-regexp nil t))
  10000. (and (eq elt 'closed)
  10001. (re-search-forward org-closed-time-regexp nil t)))
  10002. (replace-match "")
  10003. (if (looking-at "--+<[^>]+>") (replace-match ""))
  10004. (skip-chars-backward " ")
  10005. (if (looking-at " +") (replace-match ""))))
  10006. (goto-char (point-max))
  10007. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10008. (when what
  10009. (insert
  10010. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10011. (cond ((eq what 'scheduled) org-scheduled-string)
  10012. ((eq what 'deadline) org-deadline-string)
  10013. ((eq what 'closed) org-closed-string))
  10014. " ")
  10015. (setq ts (org-insert-time-stamp
  10016. time
  10017. (or org-time-was-given
  10018. (and (eq what 'closed) org-log-done-with-time))
  10019. (eq what 'closed)
  10020. nil nil (list org-end-time-was-given)))
  10021. (end-of-line 1))
  10022. (goto-char (point-min))
  10023. (widen)
  10024. (if (and (looking-at "[ \t]+\n")
  10025. (equal (char-before) ?\n))
  10026. (delete-region (1- (point)) (point-at-eol)))
  10027. ts))))))
  10028. (defvar org-log-note-marker (make-marker))
  10029. (defvar org-log-note-purpose nil)
  10030. (defvar org-log-note-state nil)
  10031. (defvar org-log-note-previous-state nil)
  10032. (defvar org-log-note-how nil)
  10033. (defvar org-log-note-extra nil)
  10034. (defvar org-log-note-window-configuration nil)
  10035. (defvar org-log-note-return-to (make-marker))
  10036. (defvar org-log-post-message nil
  10037. "Message to be displayed after a log note has been stored.
  10038. The auto-repeater uses this.")
  10039. (defun org-add-note ()
  10040. "Add a note to the current entry.
  10041. This is done in the same way as adding a state change note."
  10042. (interactive)
  10043. (org-add-log-setup 'note nil nil 'findpos nil))
  10044. (defvar org-property-end-re)
  10045. (defun org-add-log-setup (&optional purpose state prev-state
  10046. findpos how &optional extra)
  10047. "Set up the post command hook to take a note.
  10048. If this is about to TODO state change, the new state is expected in STATE.
  10049. When FINDPOS is non-nil, find the correct position for the note in
  10050. the current entry. If not, assume that it can be inserted at point.
  10051. HOW is an indicator what kind of note should be created.
  10052. EXTRA is additional text that will be inserted into the notes buffer."
  10053. (let* ((org-log-into-drawer (org-log-into-drawer))
  10054. (drawer (cond ((stringp org-log-into-drawer)
  10055. org-log-into-drawer)
  10056. (org-log-into-drawer "LOGBOOK")
  10057. (t nil))))
  10058. (save-restriction
  10059. (save-excursion
  10060. (when findpos
  10061. (org-back-to-heading t)
  10062. (narrow-to-region (point) (save-excursion
  10063. (outline-next-heading) (point)))
  10064. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  10065. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10066. "[^\r\n]*\\)?"))
  10067. (goto-char (match-end 0))
  10068. (cond
  10069. (drawer
  10070. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10071. nil t)
  10072. (progn
  10073. (goto-char (match-end 0))
  10074. (or org-log-states-order-reversed
  10075. (and (re-search-forward org-property-end-re nil t)
  10076. (goto-char (1- (match-beginning 0))))))
  10077. (insert "\n:" drawer ":\n:END:")
  10078. (beginning-of-line 0)
  10079. (org-indent-line-function)
  10080. (beginning-of-line 2)
  10081. (org-indent-line-function)
  10082. (end-of-line 0)))
  10083. ((and org-log-state-notes-insert-after-drawers
  10084. (save-excursion
  10085. (forward-line) (looking-at org-drawer-regexp)))
  10086. (forward-line)
  10087. (while (looking-at org-drawer-regexp)
  10088. (goto-char (match-end 0))
  10089. (re-search-forward org-property-end-re (point-max) t)
  10090. (forward-line))
  10091. (forward-line -1)))
  10092. (unless org-log-states-order-reversed
  10093. (and (= (char-after) ?\n) (forward-char 1))
  10094. (org-skip-over-state-notes)
  10095. (skip-chars-backward " \t\n\r")))
  10096. (move-marker org-log-note-marker (point))
  10097. (setq org-log-note-purpose purpose
  10098. org-log-note-state state
  10099. org-log-note-previous-state prev-state
  10100. org-log-note-how how
  10101. org-log-note-extra extra)
  10102. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10103. (defun org-skip-over-state-notes ()
  10104. "Skip past the list of State notes in an entry."
  10105. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10106. (while (looking-at "[ \t]*- State")
  10107. (condition-case nil
  10108. (org-next-item)
  10109. (error (org-end-of-item)))))
  10110. (defun org-add-log-note (&optional purpose)
  10111. "Pop up a window for taking a note, and add this note later at point."
  10112. (remove-hook 'post-command-hook 'org-add-log-note)
  10113. (setq org-log-note-window-configuration (current-window-configuration))
  10114. (delete-other-windows)
  10115. (move-marker org-log-note-return-to (point))
  10116. (switch-to-buffer (marker-buffer org-log-note-marker))
  10117. (goto-char org-log-note-marker)
  10118. (org-switch-to-buffer-other-window "*Org Note*")
  10119. (erase-buffer)
  10120. (if (memq org-log-note-how '(time state))
  10121. (let (current-prefix-arg) (org-store-log-note))
  10122. (let ((org-inhibit-startup t)) (org-mode))
  10123. (insert (format "# Insert note for %s.
  10124. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  10125. (cond
  10126. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  10127. ((eq org-log-note-purpose 'done) "closed todo item")
  10128. ((eq org-log-note-purpose 'state)
  10129. (format "state change from \"%s\" to \"%s\""
  10130. (or org-log-note-previous-state "")
  10131. (or org-log-note-state "")))
  10132. ((eq org-log-note-purpose 'reschedule)
  10133. "rescheduling")
  10134. ((eq org-log-note-purpose 'delschedule)
  10135. "no longer scheduled")
  10136. ((eq org-log-note-purpose 'redeadline)
  10137. "changing deadline")
  10138. ((eq org-log-note-purpose 'deldeadline)
  10139. "removing deadline")
  10140. ((eq org-log-note-purpose 'refile)
  10141. "refiling")
  10142. ((eq org-log-note-purpose 'note)
  10143. "this entry")
  10144. (t (error "This should not happen")))))
  10145. (if org-log-note-extra (insert org-log-note-extra))
  10146. (org-set-local 'org-finish-function 'org-store-log-note)))
  10147. (defvar org-note-abort nil) ; dynamically scoped
  10148. (defun org-store-log-note ()
  10149. "Finish taking a log note, and insert it to where it belongs."
  10150. (let ((txt (buffer-string))
  10151. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  10152. lines ind)
  10153. (kill-buffer (current-buffer))
  10154. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  10155. (setq txt (replace-match "" t t txt)))
  10156. (if (string-match "\\s-+\\'" txt)
  10157. (setq txt (replace-match "" t t txt)))
  10158. (setq lines (org-split-string txt "\n"))
  10159. (when (and note (string-match "\\S-" note))
  10160. (setq note
  10161. (org-replace-escapes
  10162. note
  10163. (list (cons "%u" (user-login-name))
  10164. (cons "%U" user-full-name)
  10165. (cons "%t" (format-time-string
  10166. (org-time-stamp-format 'long 'inactive)
  10167. (current-time)))
  10168. (cons "%s" (if org-log-note-state
  10169. (concat "\"" org-log-note-state "\"")
  10170. ""))
  10171. (cons "%S" (if org-log-note-previous-state
  10172. (concat "\"" org-log-note-previous-state "\"")
  10173. "\"\"")))))
  10174. (if lines (setq note (concat note " \\\\")))
  10175. (push note lines))
  10176. (when (or current-prefix-arg org-note-abort)
  10177. (when org-log-into-drawer
  10178. (org-remove-empty-drawer-at
  10179. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10180. org-log-note-marker))
  10181. (setq lines nil))
  10182. (when lines
  10183. (with-current-buffer (marker-buffer org-log-note-marker)
  10184. (save-excursion
  10185. (goto-char org-log-note-marker)
  10186. (move-marker org-log-note-marker nil)
  10187. (end-of-line 1)
  10188. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10189. (insert "- " (pop lines))
  10190. (org-indent-line-function)
  10191. (beginning-of-line 1)
  10192. (looking-at "[ \t]*")
  10193. (setq ind (concat (match-string 0) " "))
  10194. (end-of-line 1)
  10195. (while lines (insert "\n" ind (pop lines)))
  10196. (message "Note stored")
  10197. (org-back-to-heading t)
  10198. (org-cycle-hide-drawers 'children)))))
  10199. (set-window-configuration org-log-note-window-configuration)
  10200. (with-current-buffer (marker-buffer org-log-note-return-to)
  10201. (goto-char org-log-note-return-to))
  10202. (move-marker org-log-note-return-to nil)
  10203. (and org-log-post-message (message "%s" org-log-post-message)))
  10204. (defun org-remove-empty-drawer-at (drawer pos)
  10205. "Remove an empty drawer DRAWER at position POS.
  10206. POS may also be a marker."
  10207. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10208. (save-excursion
  10209. (save-restriction
  10210. (widen)
  10211. (goto-char pos)
  10212. (if (org-in-regexp
  10213. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10214. (replace-match ""))))))
  10215. (defun org-sparse-tree (&optional arg)
  10216. "Create a sparse tree, prompt for the details.
  10217. This command can create sparse trees. You first need to select the type
  10218. of match used to create the tree:
  10219. t Show entries with a specific TODO keyword.
  10220. m Show entries selected by a tags/property match.
  10221. p Enter a property name and its value (both with completion on existing
  10222. names/values) and show entries with that property.
  10223. / Show entries matching a regular expression (`r' can be used as well)
  10224. d Show deadlines due within `org-deadline-warning-days'.
  10225. b Show deadlines and scheduled items before a date.
  10226. a Show deadlines and scheduled items after a date."
  10227. (interactive "P")
  10228. (let (ans kwd value)
  10229. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  10230. (setq ans (read-char-exclusive))
  10231. (cond
  10232. ((equal ans ?d)
  10233. (call-interactively 'org-check-deadlines))
  10234. ((equal ans ?b)
  10235. (call-interactively 'org-check-before-date))
  10236. ((equal ans ?a)
  10237. (call-interactively 'org-check-after-date))
  10238. ((equal ans ?t)
  10239. (org-show-todo-tree '(4)))
  10240. ((member ans '(?T ?m))
  10241. (call-interactively 'org-match-sparse-tree))
  10242. ((member ans '(?p ?P))
  10243. (setq kwd (org-icompleting-read "Property: "
  10244. (mapcar 'list (org-buffer-property-keys))))
  10245. (setq value (org-icompleting-read "Value: "
  10246. (mapcar 'list (org-property-values kwd))))
  10247. (unless (string-match "\\`{.*}\\'" value)
  10248. (setq value (concat "\"" value "\"")))
  10249. (org-match-sparse-tree arg (concat kwd "=" value)))
  10250. ((member ans '(?r ?R ?/))
  10251. (call-interactively 'org-occur))
  10252. (t (error "No such sparse tree command \"%c\"" ans)))))
  10253. (defvar org-occur-highlights nil
  10254. "List of overlays used for occur matches.")
  10255. (make-variable-buffer-local 'org-occur-highlights)
  10256. (defvar org-occur-parameters nil
  10257. "Parameters of the active org-occur calls.
  10258. This is a list, each call to org-occur pushes as cons cell,
  10259. containing the regular expression and the callback, onto the list.
  10260. The list can contain several entries if `org-occur' has been called
  10261. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  10262. will only contain one set of parameters. When the highlights are
  10263. removed (for example with `C-c C-c', or with the next edit (depending
  10264. on `org-remove-highlights-with-change'), this variable is emptied
  10265. as well.")
  10266. (make-variable-buffer-local 'org-occur-parameters)
  10267. (defun org-occur (regexp &optional keep-previous callback)
  10268. "Make a compact tree which shows all matches of REGEXP.
  10269. The tree will show the lines where the regexp matches, and all higher
  10270. headlines above the match. It will also show the heading after the match,
  10271. to make sure editing the matching entry is easy.
  10272. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  10273. call to `org-occur' will be kept, to allow stacking of calls to this
  10274. command.
  10275. If CALLBACK is non-nil, it is a function which is called to confirm
  10276. that the match should indeed be shown."
  10277. (interactive "sRegexp: \nP")
  10278. (when (equal regexp "")
  10279. (error "Regexp cannot be empty"))
  10280. (unless keep-previous
  10281. (org-remove-occur-highlights nil nil t))
  10282. (push (cons regexp callback) org-occur-parameters)
  10283. (let ((cnt 0))
  10284. (save-excursion
  10285. (goto-char (point-min))
  10286. (if (or (not keep-previous) ; do not want to keep
  10287. (not org-occur-highlights)) ; no previous matches
  10288. ;; hide everything
  10289. (org-overview))
  10290. (while (re-search-forward regexp nil t)
  10291. (when (or (not callback)
  10292. (save-match-data (funcall callback)))
  10293. (setq cnt (1+ cnt))
  10294. (when org-highlight-sparse-tree-matches
  10295. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  10296. (org-show-context 'occur-tree))))
  10297. (when org-remove-highlights-with-change
  10298. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  10299. nil 'local))
  10300. (unless org-sparse-tree-open-archived-trees
  10301. (org-hide-archived-subtrees (point-min) (point-max)))
  10302. (run-hooks 'org-occur-hook)
  10303. (if (interactive-p)
  10304. (message "%d match(es) for regexp %s" cnt regexp))
  10305. cnt))
  10306. (defun org-show-context (&optional key)
  10307. "Make sure point and context and visible.
  10308. How much context is shown depends upon the variables
  10309. `org-show-hierarchy-above', `org-show-following-heading'. and
  10310. `org-show-siblings'."
  10311. (let ((heading-p (org-on-heading-p t))
  10312. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  10313. (following-p (org-get-alist-option org-show-following-heading key))
  10314. (entry-p (org-get-alist-option org-show-entry-below key))
  10315. (siblings-p (org-get-alist-option org-show-siblings key)))
  10316. (catch 'exit
  10317. ;; Show heading or entry text
  10318. (if (and heading-p (not entry-p))
  10319. (org-flag-heading nil) ; only show the heading
  10320. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  10321. (org-show-hidden-entry))) ; show entire entry
  10322. (when following-p
  10323. ;; Show next sibling, or heading below text
  10324. (save-excursion
  10325. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  10326. (org-flag-heading nil))))
  10327. (when siblings-p (org-show-siblings))
  10328. (when hierarchy-p
  10329. ;; show all higher headings, possibly with siblings
  10330. (save-excursion
  10331. (while (and (condition-case nil
  10332. (progn (org-up-heading-all 1) t)
  10333. (error nil))
  10334. (not (bobp)))
  10335. (org-flag-heading nil)
  10336. (when siblings-p (org-show-siblings))))))))
  10337. (defvar org-reveal-start-hook nil
  10338. "Hook run before revealing a location.")
  10339. (defun org-reveal (&optional siblings)
  10340. "Show current entry, hierarchy above it, and the following headline.
  10341. This can be used to show a consistent set of context around locations
  10342. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  10343. not t for the search context.
  10344. With optional argument SIBLINGS, on each level of the hierarchy all
  10345. siblings are shown. This repairs the tree structure to what it would
  10346. look like when opened with hierarchical calls to `org-cycle'.
  10347. With double optional argument `C-u C-u', go to the parent and show the
  10348. entire tree."
  10349. (interactive "P")
  10350. (run-hooks 'org-reveal-start-hook)
  10351. (let ((org-show-hierarchy-above t)
  10352. (org-show-following-heading t)
  10353. (org-show-siblings (if siblings t org-show-siblings)))
  10354. (org-show-context nil))
  10355. (when (equal siblings '(16))
  10356. (save-excursion
  10357. (when (org-up-heading-safe)
  10358. (org-show-subtree)
  10359. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  10360. (defun org-highlight-new-match (beg end)
  10361. "Highlight from BEG to END and mark the highlight is an occur headline."
  10362. (let ((ov (org-make-overlay beg end)))
  10363. (org-overlay-put ov 'face 'secondary-selection)
  10364. (push ov org-occur-highlights)))
  10365. (defun org-remove-occur-highlights (&optional beg end noremove)
  10366. "Remove the occur highlights from the buffer.
  10367. BEG and END are ignored. If NOREMOVE is nil, remove this function
  10368. from the `before-change-functions' in the current buffer."
  10369. (interactive)
  10370. (unless org-inhibit-highlight-removal
  10371. (mapc 'org-delete-overlay org-occur-highlights)
  10372. (setq org-occur-highlights nil)
  10373. (setq org-occur-parameters nil)
  10374. (unless noremove
  10375. (remove-hook 'before-change-functions
  10376. 'org-remove-occur-highlights 'local))))
  10377. ;;;; Priorities
  10378. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  10379. "Regular expression matching the priority indicator.")
  10380. (defvar org-remove-priority-next-time nil)
  10381. (defun org-priority-up ()
  10382. "Increase the priority of the current item."
  10383. (interactive)
  10384. (org-priority 'up))
  10385. (defun org-priority-down ()
  10386. "Decrease the priority of the current item."
  10387. (interactive)
  10388. (org-priority 'down))
  10389. (defun org-priority (&optional action)
  10390. "Change the priority of an item by ARG.
  10391. ACTION can be `set', `up', `down', or a character."
  10392. (interactive)
  10393. (unless org-enable-priority-commands
  10394. (error "Priority commands are disabled"))
  10395. (setq action (or action 'set))
  10396. (let (current new news have remove)
  10397. (save-excursion
  10398. (org-back-to-heading t)
  10399. (if (looking-at org-priority-regexp)
  10400. (setq current (string-to-char (match-string 2))
  10401. have t)
  10402. (setq current org-default-priority))
  10403. (cond
  10404. ((eq action 'remove)
  10405. (setq remove t new ?\ ))
  10406. ((or (eq action 'set)
  10407. (if (featurep 'xemacs) (characterp action) (integerp action)))
  10408. (if (not (eq action 'set))
  10409. (setq new action)
  10410. (message "Priority %c-%c, SPC to remove: "
  10411. org-highest-priority org-lowest-priority)
  10412. (setq new (read-char-exclusive)))
  10413. (if (and (= (upcase org-highest-priority) org-highest-priority)
  10414. (= (upcase org-lowest-priority) org-lowest-priority))
  10415. (setq new (upcase new)))
  10416. (cond ((equal new ?\ ) (setq remove t))
  10417. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  10418. (error "Priority must be between `%c' and `%c'"
  10419. org-highest-priority org-lowest-priority))))
  10420. ((eq action 'up)
  10421. (if (and (not have) (eq last-command this-command))
  10422. (setq new org-lowest-priority)
  10423. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10424. org-default-priority (1- current)))))
  10425. ((eq action 'down)
  10426. (if (and (not have) (eq last-command this-command))
  10427. (setq new org-highest-priority)
  10428. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10429. org-default-priority (1+ current)))))
  10430. (t (error "Invalid action")))
  10431. (if (or (< (upcase new) org-highest-priority)
  10432. (> (upcase new) org-lowest-priority))
  10433. (setq remove t))
  10434. (setq news (format "%c" new))
  10435. (if have
  10436. (if remove
  10437. (replace-match "" t t nil 1)
  10438. (replace-match news t t nil 2))
  10439. (if remove
  10440. (error "No priority cookie found in line")
  10441. (let ((case-fold-search nil))
  10442. (looking-at org-todo-line-regexp))
  10443. (if (match-end 2)
  10444. (progn
  10445. (goto-char (match-end 2))
  10446. (insert " [#" news "]"))
  10447. (goto-char (match-beginning 3))
  10448. (insert "[#" news "] "))))
  10449. (org-preserve-lc (org-set-tags nil 'align)))
  10450. (if remove
  10451. (message "Priority removed")
  10452. (message "Priority of current item set to %s" news))))
  10453. (defun org-get-priority (s)
  10454. "Find priority cookie and return priority."
  10455. (save-match-data
  10456. (if (not (string-match org-priority-regexp s))
  10457. (* 1000 (- org-lowest-priority org-default-priority))
  10458. (* 1000 (- org-lowest-priority
  10459. (string-to-char (match-string 2 s)))))))
  10460. ;;;; Tags
  10461. (defvar org-agenda-archives-mode)
  10462. (defvar org-map-continue-from nil
  10463. "Position from where mapping should continue.
  10464. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  10465. (defvar org-scanner-tags nil
  10466. "The current tag list while the tags scanner is running.")
  10467. (defvar org-trust-scanner-tags nil
  10468. "Should `org-get-tags-at' use the tags fro the scanner.
  10469. This is for internal dynamical scoping only.
  10470. When this is non-nil, the function `org-get-tags-at' will return the value
  10471. of `org-scanner-tags' instead of building the list by itself. This
  10472. can lead to large speed-ups when the tags scanner is used in a file with
  10473. many entries, and when the list of tags is retrieved, for example to
  10474. obtain a list of properties. Building the tags list for each entry in such
  10475. a file becomes an N^2 operation - but with this variable set, it scales
  10476. as N.")
  10477. (defun org-scan-tags (action matcher &optional todo-only)
  10478. "Scan headline tags with inheritance and produce output ACTION.
  10479. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  10480. or `agenda' to produce an entry list for an agenda view. It can also be
  10481. a Lisp form or a function that should be called at each matched headline, in
  10482. this case the return value is a list of all return values from these calls.
  10483. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  10484. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  10485. only lines with a TODO keyword are included in the output."
  10486. (require 'org-agenda)
  10487. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  10488. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  10489. (org-re
  10490. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  10491. (props (list 'face 'default
  10492. 'done-face 'org-agenda-done
  10493. 'undone-face 'default
  10494. 'mouse-face 'highlight
  10495. 'org-not-done-regexp org-not-done-regexp
  10496. 'org-todo-regexp org-todo-regexp
  10497. 'help-echo
  10498. (format "mouse-2 or RET jump to org file %s"
  10499. (abbreviate-file-name
  10500. (or (buffer-file-name (buffer-base-buffer))
  10501. (buffer-name (buffer-base-buffer)))))))
  10502. (case-fold-search nil)
  10503. (org-map-continue-from nil)
  10504. lspos tags tags-list
  10505. (tags-alist (list (cons 0 org-file-tags)))
  10506. (llast 0) rtn rtn1 level category i txt
  10507. todo marker entry priority)
  10508. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  10509. (setq action (list 'lambda nil action)))
  10510. (save-excursion
  10511. (goto-char (point-min))
  10512. (when (eq action 'sparse-tree)
  10513. (org-overview)
  10514. (org-remove-occur-highlights))
  10515. (while (re-search-forward re nil t)
  10516. (catch :skip
  10517. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  10518. tags (if (match-end 4) (org-match-string-no-properties 4)))
  10519. (goto-char (setq lspos (match-beginning 0)))
  10520. (setq level (org-reduced-level (funcall outline-level))
  10521. category (org-get-category))
  10522. (setq i llast llast level)
  10523. ;; remove tag lists from same and sublevels
  10524. (while (>= i level)
  10525. (when (setq entry (assoc i tags-alist))
  10526. (setq tags-alist (delete entry tags-alist)))
  10527. (setq i (1- i)))
  10528. ;; add the next tags
  10529. (when tags
  10530. (setq tags (org-split-string tags ":")
  10531. tags-alist
  10532. (cons (cons level tags) tags-alist)))
  10533. ;; compile tags for current headline
  10534. (setq tags-list
  10535. (if org-use-tag-inheritance
  10536. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  10537. tags)
  10538. org-scanner-tags tags-list)
  10539. (when org-use-tag-inheritance
  10540. (setcdr (car tags-alist)
  10541. (mapcar (lambda (x)
  10542. (setq x (copy-sequence x))
  10543. (org-add-prop-inherited x))
  10544. (cdar tags-alist))))
  10545. (when (and tags org-use-tag-inheritance
  10546. (or (not (eq t org-use-tag-inheritance))
  10547. org-tags-exclude-from-inheritance))
  10548. ;; selective inheritance, remove uninherited ones
  10549. (setcdr (car tags-alist)
  10550. (org-remove-uniherited-tags (cdar tags-alist))))
  10551. (when (and (or (not todo-only)
  10552. (and (member todo org-not-done-keywords)
  10553. (or (not org-agenda-tags-todo-honor-ignore-options)
  10554. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  10555. (let ((case-fold-search t)) (eval matcher))
  10556. (or
  10557. (not (member org-archive-tag tags-list))
  10558. ;; we have an archive tag, should we use this anyway?
  10559. (or (not org-agenda-skip-archived-trees)
  10560. (and (eq action 'agenda) org-agenda-archives-mode))))
  10561. (unless (eq action 'sparse-tree) (org-agenda-skip))
  10562. ;; select this headline
  10563. (cond
  10564. ((eq action 'sparse-tree)
  10565. (and org-highlight-sparse-tree-matches
  10566. (org-get-heading) (match-end 0)
  10567. (org-highlight-new-match
  10568. (match-beginning 0) (match-beginning 1)))
  10569. (org-show-context 'tags-tree))
  10570. ((eq action 'agenda)
  10571. (setq txt (org-format-agenda-item
  10572. ""
  10573. (concat
  10574. (if (eq org-tags-match-list-sublevels 'indented)
  10575. (make-string (1- level) ?.) "")
  10576. (org-get-heading))
  10577. category
  10578. tags-list
  10579. )
  10580. priority (org-get-priority txt))
  10581. (goto-char lspos)
  10582. (setq marker (org-agenda-new-marker))
  10583. (org-add-props txt props
  10584. 'org-marker marker 'org-hd-marker marker 'org-category category
  10585. 'todo-state todo
  10586. 'priority priority 'type "tagsmatch")
  10587. (push txt rtn))
  10588. ((functionp action)
  10589. (setq org-map-continue-from nil)
  10590. (save-excursion
  10591. (setq rtn1 (funcall action))
  10592. (push rtn1 rtn)))
  10593. (t (error "Invalid action")))
  10594. ;; if we are to skip sublevels, jump to end of subtree
  10595. (unless org-tags-match-list-sublevels
  10596. (org-end-of-subtree t)
  10597. (backward-char 1))))
  10598. ;; Get the correct position from where to continue
  10599. (if org-map-continue-from
  10600. (goto-char org-map-continue-from)
  10601. (and (= (point) lspos) (end-of-line 1)))))
  10602. (when (and (eq action 'sparse-tree)
  10603. (not org-sparse-tree-open-archived-trees))
  10604. (org-hide-archived-subtrees (point-min) (point-max)))
  10605. (nreverse rtn)))
  10606. (defun org-remove-uniherited-tags (tags)
  10607. "Remove all tags that are not inherited from the list TAGS."
  10608. (cond
  10609. ((eq org-use-tag-inheritance t)
  10610. (if org-tags-exclude-from-inheritance
  10611. (org-delete-all org-tags-exclude-from-inheritance tags)
  10612. tags))
  10613. ((not org-use-tag-inheritance) nil)
  10614. ((stringp org-use-tag-inheritance)
  10615. (delq nil (mapcar
  10616. (lambda (x)
  10617. (if (and (string-match org-use-tag-inheritance x)
  10618. (not (member x org-tags-exclude-from-inheritance)))
  10619. x nil))
  10620. tags)))
  10621. ((listp org-use-tag-inheritance)
  10622. (delq nil (mapcar
  10623. (lambda (x)
  10624. (if (member x org-use-tag-inheritance) x nil))
  10625. tags)))))
  10626. (defvar todo-only) ;; dynamically scoped
  10627. (defun org-match-sparse-tree (&optional todo-only match)
  10628. "Create a sparse tree according to tags string MATCH.
  10629. MATCH can contain positive and negative selection of tags, like
  10630. \"+WORK+URGENT-WITHBOSS\".
  10631. If optional argument TODO-ONLY is non-nil, only select lines that are
  10632. also TODO lines."
  10633. (interactive "P")
  10634. (org-prepare-agenda-buffers (list (current-buffer)))
  10635. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  10636. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  10637. (defvar org-cached-props nil)
  10638. (defun org-cached-entry-get (pom property)
  10639. (if (or (eq t org-use-property-inheritance)
  10640. (and (stringp org-use-property-inheritance)
  10641. (string-match org-use-property-inheritance property))
  10642. (and (listp org-use-property-inheritance)
  10643. (member property org-use-property-inheritance)))
  10644. ;; Caching is not possible, check it directly
  10645. (org-entry-get pom property 'inherit)
  10646. ;; Get all properties, so that we can do complicated checks easily
  10647. (cdr (assoc property (or org-cached-props
  10648. (setq org-cached-props
  10649. (org-entry-properties pom)))))))
  10650. (defun org-global-tags-completion-table (&optional files)
  10651. "Return the list of all tags in all agenda buffer/files."
  10652. (save-excursion
  10653. (org-uniquify
  10654. (delq nil
  10655. (apply 'append
  10656. (mapcar
  10657. (lambda (file)
  10658. (set-buffer (find-file-noselect file))
  10659. (append (org-get-buffer-tags)
  10660. (mapcar (lambda (x) (if (stringp (car-safe x))
  10661. (list (car-safe x)) nil))
  10662. org-tag-alist)))
  10663. (if (and files (car files))
  10664. files
  10665. (org-agenda-files))))))))
  10666. (defun org-make-tags-matcher (match)
  10667. "Create the TAGS//TODO matcher form for the selection string MATCH."
  10668. ;; todo-only is scoped dynamically into this function, and the function
  10669. ;; may change it if the matcher asks for it.
  10670. (unless match
  10671. ;; Get a new match request, with completion
  10672. (let ((org-last-tags-completion-table
  10673. (org-global-tags-completion-table)))
  10674. (setq match (org-completing-read-no-i
  10675. "Match: " 'org-tags-completion-function nil nil nil
  10676. 'org-tags-history))))
  10677. ;; Parse the string and create a lisp form
  10678. (let ((match0 match)
  10679. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  10680. minus tag mm
  10681. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  10682. orterms term orlist re-p str-p level-p level-op time-p
  10683. prop-p pn pv po cat-p gv rest)
  10684. (if (string-match "/+" match)
  10685. ;; match contains also a todo-matching request
  10686. (progn
  10687. (setq tagsmatch (substring match 0 (match-beginning 0))
  10688. todomatch (substring match (match-end 0)))
  10689. (if (string-match "^!" todomatch)
  10690. (setq todo-only t todomatch (substring todomatch 1)))
  10691. (if (string-match "^\\s-*$" todomatch)
  10692. (setq todomatch nil)))
  10693. ;; only matching tags
  10694. (setq tagsmatch match todomatch nil))
  10695. ;; Make the tags matcher
  10696. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  10697. (setq tagsmatcher t)
  10698. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  10699. (while (setq term (pop orterms))
  10700. (while (and (equal (substring term -1) "\\") orterms)
  10701. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  10702. (while (string-match re term)
  10703. (setq rest (substring term (match-end 0))
  10704. minus (and (match-end 1)
  10705. (equal (match-string 1 term) "-"))
  10706. tag (match-string 2 term)
  10707. re-p (equal (string-to-char tag) ?{)
  10708. level-p (match-end 4)
  10709. prop-p (match-end 5)
  10710. mm (cond
  10711. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  10712. (level-p
  10713. (setq level-op (org-op-to-function (match-string 3 term)))
  10714. `(,level-op level ,(string-to-number
  10715. (match-string 4 term))))
  10716. (prop-p
  10717. (setq pn (match-string 5 term)
  10718. po (match-string 6 term)
  10719. pv (match-string 7 term)
  10720. cat-p (equal pn "CATEGORY")
  10721. re-p (equal (string-to-char pv) ?{)
  10722. str-p (equal (string-to-char pv) ?\")
  10723. time-p (save-match-data
  10724. (string-match "^\"[[<].*[]>]\"$" pv))
  10725. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  10726. (if time-p (setq pv (org-matcher-time pv)))
  10727. (setq po (org-op-to-function po (if time-p 'time str-p)))
  10728. (cond
  10729. ((equal pn "CATEGORY")
  10730. (setq gv '(get-text-property (point) 'org-category)))
  10731. ((equal pn "TODO")
  10732. (setq gv 'todo))
  10733. (t
  10734. (setq gv `(org-cached-entry-get nil ,pn))))
  10735. (if re-p
  10736. (if (eq po 'org<>)
  10737. `(not (string-match ,pv (or ,gv "")))
  10738. `(string-match ,pv (or ,gv "")))
  10739. (if str-p
  10740. `(,po (or ,gv "") ,pv)
  10741. `(,po (string-to-number (or ,gv ""))
  10742. ,(string-to-number pv) ))))
  10743. (t `(member ,tag tags-list)))
  10744. mm (if minus (list 'not mm) mm)
  10745. term rest)
  10746. (push mm tagsmatcher))
  10747. (push (if (> (length tagsmatcher) 1)
  10748. (cons 'and tagsmatcher)
  10749. (car tagsmatcher))
  10750. orlist)
  10751. (setq tagsmatcher nil))
  10752. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  10753. (setq tagsmatcher
  10754. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  10755. ;; Make the todo matcher
  10756. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  10757. (setq todomatcher t)
  10758. (setq orterms (org-split-string todomatch "|") orlist nil)
  10759. (while (setq term (pop orterms))
  10760. (while (string-match re term)
  10761. (setq minus (and (match-end 1)
  10762. (equal (match-string 1 term) "-"))
  10763. kwd (match-string 2 term)
  10764. re-p (equal (string-to-char kwd) ?{)
  10765. term (substring term (match-end 0))
  10766. mm (if re-p
  10767. `(string-match ,(substring kwd 1 -1) todo)
  10768. (list 'equal 'todo kwd))
  10769. mm (if minus (list 'not mm) mm))
  10770. (push mm todomatcher))
  10771. (push (if (> (length todomatcher) 1)
  10772. (cons 'and todomatcher)
  10773. (car todomatcher))
  10774. orlist)
  10775. (setq todomatcher nil))
  10776. (setq todomatcher (if (> (length orlist) 1)
  10777. (cons 'or orlist) (car orlist))))
  10778. ;; Return the string and lisp forms of the matcher
  10779. (setq matcher (if todomatcher
  10780. (list 'and tagsmatcher todomatcher)
  10781. tagsmatcher))
  10782. (cons match0 matcher)))
  10783. (defun org-op-to-function (op &optional stringp)
  10784. "Turn an operator into the appropriate function."
  10785. (setq op
  10786. (cond
  10787. ((equal op "<" ) '(< string< org-time<))
  10788. ((equal op ">" ) '(> org-string> org-time>))
  10789. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  10790. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  10791. ((member op '("=" "==")) '(= string= org-time=))
  10792. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  10793. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  10794. (defun org<> (a b) (not (= a b)))
  10795. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  10796. (defun org-string>= (a b) (not (string< a b)))
  10797. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  10798. (defun org-string<> (a b) (not (string= a b)))
  10799. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  10800. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  10801. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  10802. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  10803. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  10804. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  10805. (defun org-2ft (s)
  10806. "Convert S to a floating point time.
  10807. If S is already a number, just return it. If it is a string, parse
  10808. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  10809. (cond
  10810. ((numberp s) s)
  10811. ((stringp s)
  10812. (condition-case nil
  10813. (float-time (apply 'encode-time (org-parse-time-string s)))
  10814. (error 0.)))
  10815. (t 0.)))
  10816. (defun org-time-today ()
  10817. "Time in seconds today at 0:00.
  10818. Returns the float number of seconds since the beginning of the
  10819. epoch to the beginning of today (00:00)."
  10820. (float-time (apply 'encode-time
  10821. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  10822. (defun org-matcher-time (s)
  10823. "Interpret a time comparison value."
  10824. (save-match-data
  10825. (cond
  10826. ((string= s "<now>") (float-time))
  10827. ((string= s "<today>") (org-time-today))
  10828. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  10829. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  10830. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  10831. (+ (org-time-today)
  10832. (* (string-to-number (match-string 1 s))
  10833. (cdr (assoc (match-string 2 s)
  10834. '(("d" . 86400.0) ("w" . 604800.0)
  10835. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10836. (t (org-2ft s)))))
  10837. (defun org-match-any-p (re list)
  10838. "Does re match any element of list?"
  10839. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10840. (delq nil list))
  10841. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10842. (defvar org-tags-overlay (org-make-overlay 1 1))
  10843. (org-detach-overlay org-tags-overlay)
  10844. (defun org-get-local-tags-at (&optional pos)
  10845. "Get a list of tags defined in the current headline."
  10846. (org-get-tags-at pos 'local))
  10847. (defun org-get-local-tags ()
  10848. "Get a list of tags defined in the current headline."
  10849. (org-get-tags-at nil 'local))
  10850. (defun org-get-tags-at (&optional pos local)
  10851. "Get a list of all headline tags applicable at POS.
  10852. POS defaults to point. If tags are inherited, the list contains
  10853. the targets in the same sequence as the headlines appear, i.e.
  10854. the tags of the current headline come last.
  10855. When LOCAL is non-nil, only return tags from the current headline,
  10856. ignore inherited ones."
  10857. (interactive)
  10858. (if (and org-trust-scanner-tags
  10859. (or (not pos) (equal pos (point)))
  10860. (not local))
  10861. org-scanner-tags
  10862. (let (tags ltags lastpos parent)
  10863. (save-excursion
  10864. (save-restriction
  10865. (widen)
  10866. (goto-char (or pos (point)))
  10867. (save-match-data
  10868. (catch 'done
  10869. (condition-case nil
  10870. (progn
  10871. (org-back-to-heading t)
  10872. (while (not (equal lastpos (point)))
  10873. (setq lastpos (point))
  10874. (when (looking-at
  10875. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  10876. (setq ltags (org-split-string
  10877. (org-match-string-no-properties 1) ":"))
  10878. (when parent
  10879. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  10880. (setq tags (append
  10881. (if parent
  10882. (org-remove-uniherited-tags ltags)
  10883. ltags)
  10884. tags)))
  10885. (or org-use-tag-inheritance (throw 'done t))
  10886. (if local (throw 'done t))
  10887. (or (org-up-heading-safe) (error nil))
  10888. (setq parent t)))
  10889. (error nil)))))
  10890. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  10891. (defun org-add-prop-inherited (s)
  10892. (add-text-properties 0 (length s) '(inherited t) s)
  10893. s)
  10894. (defun org-toggle-tag (tag &optional onoff)
  10895. "Toggle the tag TAG for the current line.
  10896. If ONOFF is `on' or `off', don't toggle but set to this state."
  10897. (let (res current)
  10898. (save-excursion
  10899. (org-back-to-heading t)
  10900. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  10901. (point-at-eol) t)
  10902. (progn
  10903. (setq current (match-string 1))
  10904. (replace-match ""))
  10905. (setq current ""))
  10906. (setq current (nreverse (org-split-string current ":")))
  10907. (cond
  10908. ((eq onoff 'on)
  10909. (setq res t)
  10910. (or (member tag current) (push tag current)))
  10911. ((eq onoff 'off)
  10912. (or (not (member tag current)) (setq current (delete tag current))))
  10913. (t (if (member tag current)
  10914. (setq current (delete tag current))
  10915. (setq res t)
  10916. (push tag current))))
  10917. (end-of-line 1)
  10918. (if current
  10919. (progn
  10920. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  10921. (org-set-tags nil t))
  10922. (delete-horizontal-space))
  10923. (run-hooks 'org-after-tags-change-hook))
  10924. res))
  10925. (defun org-align-tags-here (to-col)
  10926. ;; Assumes that this is a headline
  10927. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  10928. (beginning-of-line 1)
  10929. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10930. (< pos (match-beginning 2)))
  10931. (progn
  10932. (setq tags-l (- (match-end 2) (match-beginning 2)))
  10933. (goto-char (match-beginning 1))
  10934. (insert " ")
  10935. (delete-region (point) (1+ (match-beginning 2)))
  10936. (setq ncol (max (1+ (current-column))
  10937. (1+ col)
  10938. (if (> to-col 0)
  10939. to-col
  10940. (- (abs to-col) tags-l))))
  10941. (setq p (point))
  10942. (insert (make-string (- ncol (current-column)) ?\ ))
  10943. (setq ncol (current-column))
  10944. (when indent-tabs-mode (tabify p (point-at-eol)))
  10945. (org-move-to-column (min ncol col) t))
  10946. (goto-char pos))))
  10947. (defun org-set-tags-command (&optional arg just-align)
  10948. "Call the set-tags command for the current entry."
  10949. (interactive "P")
  10950. (if (org-on-heading-p)
  10951. (org-set-tags arg just-align)
  10952. (save-excursion
  10953. (org-back-to-heading t)
  10954. (org-set-tags arg just-align))))
  10955. (defun org-set-tags-to (data)
  10956. "Set the tags of the current entry to DATA, replacing the current tags.
  10957. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  10958. If DATA is nil or the empty string, any tags will be removed."
  10959. (interactive "sTags: ")
  10960. (setq data
  10961. (cond
  10962. ((eq data nil) "")
  10963. ((equal data "") "")
  10964. ((stringp data)
  10965. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  10966. ":"))
  10967. ((listp data)
  10968. (concat ":" (mapconcat 'identity data ":") ":"))
  10969. (t nil)))
  10970. (when data
  10971. (save-excursion
  10972. (org-back-to-heading t)
  10973. (when (looking-at org-complex-heading-regexp)
  10974. (if (match-end 5)
  10975. (progn
  10976. (goto-char (match-beginning 5))
  10977. (insert data)
  10978. (delete-region (point) (point-at-eol))
  10979. (org-set-tags nil 'align))
  10980. (goto-char (point-at-eol))
  10981. (insert " " data)
  10982. (org-set-tags nil 'align)))
  10983. (beginning-of-line 1)
  10984. (if (looking-at ".*?\\([ \t]+\\)$")
  10985. (delete-region (match-beginning 1) (match-end 1))))))
  10986. (defun org-set-tags (&optional arg just-align)
  10987. "Set the tags for the current headline.
  10988. With prefix ARG, realign all tags in headings in the current buffer."
  10989. (interactive "P")
  10990. (let* ((re (concat "^" outline-regexp))
  10991. (current (org-get-tags-string))
  10992. (col (current-column))
  10993. (org-setting-tags t)
  10994. table current-tags inherited-tags ; computed below when needed
  10995. tags p0 c0 c1 rpl)
  10996. (if arg
  10997. (save-excursion
  10998. (goto-char (point-min))
  10999. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11000. (while (re-search-forward re nil t)
  11001. (org-set-tags nil t)
  11002. (end-of-line 1)))
  11003. (message "All tags realigned to column %d" org-tags-column))
  11004. (if just-align
  11005. (setq tags current)
  11006. ;; Get a new set of tags from the user
  11007. (save-excursion
  11008. (setq table (append org-tag-persistent-alist
  11009. (or org-tag-alist (org-get-buffer-tags))
  11010. (and org-complete-tags-always-offer-all-agenda-tags
  11011. (org-global-tags-completion-table (org-agenda-files))))
  11012. org-last-tags-completion-table table
  11013. current-tags (org-split-string current ":")
  11014. inherited-tags (nreverse
  11015. (nthcdr (length current-tags)
  11016. (nreverse (org-get-tags-at))))
  11017. tags
  11018. (if (or (eq t org-use-fast-tag-selection)
  11019. (and org-use-fast-tag-selection
  11020. (delq nil (mapcar 'cdr table))))
  11021. (org-fast-tag-selection
  11022. current-tags inherited-tags table
  11023. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  11024. (let ((org-add-colon-after-tag-completion t))
  11025. (org-trim
  11026. (org-without-partial-completion
  11027. (org-icompleting-read "Tags: " 'org-tags-completion-function
  11028. nil nil current 'org-tags-history)))))))
  11029. (while (string-match "[-+&]+" tags)
  11030. ;; No boolean logic, just a list
  11031. (setq tags (replace-match ":" t t tags))))
  11032. (if org-tags-sort-function
  11033. (setq tags (mapconcat 'identity
  11034. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  11035. org-tags-sort-function) ":")))
  11036. (if (string-match "\\`[\t ]*\\'" tags)
  11037. (setq tags "")
  11038. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11039. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11040. ;; Insert new tags at the correct column
  11041. (beginning-of-line 1)
  11042. (cond
  11043. ((and (equal current "") (equal tags "")))
  11044. ((re-search-forward
  11045. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11046. (point-at-eol) t)
  11047. (if (equal tags "")
  11048. (setq rpl "")
  11049. (goto-char (match-beginning 0))
  11050. (setq c0 (current-column) p0 (if (equal (char-before) ?*)
  11051. (1+ (point)) (point))
  11052. c1 (max (1+ c0) (if (> org-tags-column 0)
  11053. org-tags-column
  11054. (- (- org-tags-column) (length tags))))
  11055. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11056. (replace-match rpl t t)
  11057. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  11058. tags)
  11059. (t (error "Tags alignment failed")))
  11060. (org-move-to-column col)
  11061. (unless just-align
  11062. (run-hooks 'org-after-tags-change-hook)))))
  11063. (defun org-change-tag-in-region (beg end tag off)
  11064. "Add or remove TAG for each entry in the region.
  11065. This works in the agenda, and also in an org-mode buffer."
  11066. (interactive
  11067. (list (region-beginning) (region-end)
  11068. (let ((org-last-tags-completion-table
  11069. (if (org-mode-p)
  11070. (org-get-buffer-tags)
  11071. (org-global-tags-completion-table))))
  11072. (org-icompleting-read
  11073. "Tag: " 'org-tags-completion-function nil nil nil
  11074. 'org-tags-history))
  11075. (progn
  11076. (message "[s]et or [r]emove? ")
  11077. (equal (read-char-exclusive) ?r))))
  11078. (if (fboundp 'deactivate-mark) (deactivate-mark))
  11079. (let ((agendap (equal major-mode 'org-agenda-mode))
  11080. l1 l2 m buf pos newhead (cnt 0))
  11081. (goto-char end)
  11082. (setq l2 (1- (org-current-line)))
  11083. (goto-char beg)
  11084. (setq l1 (org-current-line))
  11085. (loop for l from l1 to l2 do
  11086. (org-goto-line l)
  11087. (setq m (get-text-property (point) 'org-hd-marker))
  11088. (when (or (and (org-mode-p) (org-on-heading-p))
  11089. (and agendap m))
  11090. (setq buf (if agendap (marker-buffer m) (current-buffer))
  11091. pos (if agendap m (point)))
  11092. (with-current-buffer buf
  11093. (save-excursion
  11094. (save-restriction
  11095. (goto-char pos)
  11096. (setq cnt (1+ cnt))
  11097. (org-toggle-tag tag (if off 'off 'on))
  11098. (setq newhead (org-get-heading)))))
  11099. (and agendap (org-agenda-change-all-lines newhead m))))
  11100. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  11101. (defun org-tags-completion-function (string predicate &optional flag)
  11102. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11103. (confirm (lambda (x) (stringp (car x)))))
  11104. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  11105. (setq s1 (match-string 1 string)
  11106. s2 (match-string 2 string))
  11107. (setq s1 "" s2 string))
  11108. (cond
  11109. ((eq flag nil)
  11110. ;; try completion
  11111. (setq rtn (try-completion s2 ctable confirm))
  11112. (if (stringp rtn)
  11113. (setq rtn
  11114. (concat s1 s2 (substring rtn (length s2))
  11115. (if (and org-add-colon-after-tag-completion
  11116. (assoc rtn ctable))
  11117. ":" ""))))
  11118. rtn)
  11119. ((eq flag t)
  11120. ;; all-completions
  11121. (all-completions s2 ctable confirm)
  11122. )
  11123. ((eq flag 'lambda)
  11124. ;; exact match?
  11125. (assoc s2 ctable)))
  11126. ))
  11127. (defun org-fast-tag-insert (kwd tags face &optional end)
  11128. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  11129. (insert (format "%-12s" (concat kwd ":"))
  11130. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  11131. (or end "")))
  11132. (defun org-fast-tag-show-exit (flag)
  11133. (save-excursion
  11134. (org-goto-line 3)
  11135. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  11136. (replace-match ""))
  11137. (when flag
  11138. (end-of-line 1)
  11139. (org-move-to-column (- (window-width) 19) t)
  11140. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  11141. (defun org-set-current-tags-overlay (current prefix)
  11142. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  11143. (if (featurep 'xemacs)
  11144. (org-overlay-display org-tags-overlay (concat prefix s)
  11145. 'secondary-selection)
  11146. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  11147. (org-overlay-display org-tags-overlay (concat prefix s)))))
  11148. (defvar org-last-tag-selection-key nil)
  11149. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  11150. "Fast tag selection with single keys.
  11151. CURRENT is the current list of tags in the headline, INHERITED is the
  11152. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  11153. possibly with grouping information. TODO-TABLE is a similar table with
  11154. TODO keywords, should these have keys assigned to them.
  11155. If the keys are nil, a-z are automatically assigned.
  11156. Returns the new tags string, or nil to not change the current settings."
  11157. (let* ((fulltable (append table todo-table))
  11158. (maxlen (apply 'max (mapcar
  11159. (lambda (x)
  11160. (if (stringp (car x)) (string-width (car x)) 0))
  11161. fulltable)))
  11162. (buf (current-buffer))
  11163. (expert (eq org-fast-tag-selection-single-key 'expert))
  11164. (buffer-tags nil)
  11165. (fwidth (+ maxlen 3 1 3))
  11166. (ncol (/ (- (window-width) 4) fwidth))
  11167. (i-face 'org-done)
  11168. (c-face 'org-todo)
  11169. tg cnt e c char c1 c2 ntable tbl rtn
  11170. ov-start ov-end ov-prefix
  11171. (exit-after-next org-fast-tag-selection-single-key)
  11172. (done-keywords org-done-keywords)
  11173. groups ingroup)
  11174. (save-excursion
  11175. (beginning-of-line 1)
  11176. (if (looking-at
  11177. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11178. (setq ov-start (match-beginning 1)
  11179. ov-end (match-end 1)
  11180. ov-prefix "")
  11181. (setq ov-start (1- (point-at-eol))
  11182. ov-end (1+ ov-start))
  11183. (skip-chars-forward "^\n\r")
  11184. (setq ov-prefix
  11185. (concat
  11186. (buffer-substring (1- (point)) (point))
  11187. (if (> (current-column) org-tags-column)
  11188. " "
  11189. (make-string (- org-tags-column (current-column)) ?\ ))))))
  11190. (org-move-overlay org-tags-overlay ov-start ov-end)
  11191. (save-window-excursion
  11192. (if expert
  11193. (set-buffer (get-buffer-create " *Org tags*"))
  11194. (delete-other-windows)
  11195. (split-window-vertically)
  11196. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  11197. (erase-buffer)
  11198. (org-set-local 'org-done-keywords done-keywords)
  11199. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  11200. (org-fast-tag-insert "Current" current c-face "\n\n")
  11201. (org-fast-tag-show-exit exit-after-next)
  11202. (org-set-current-tags-overlay current ov-prefix)
  11203. (setq tbl fulltable char ?a cnt 0)
  11204. (while (setq e (pop tbl))
  11205. (cond
  11206. ((equal (car e) :startgroup)
  11207. (push '() groups) (setq ingroup t)
  11208. (when (not (= cnt 0))
  11209. (setq cnt 0)
  11210. (insert "\n"))
  11211. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  11212. ((equal (car e) :endgroup)
  11213. (setq ingroup nil cnt 0)
  11214. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  11215. ((equal e '(:newline))
  11216. (when (not (= cnt 0))
  11217. (setq cnt 0)
  11218. (insert "\n")
  11219. (setq e (car tbl))
  11220. (while (equal (car tbl) '(:newline))
  11221. (insert "\n")
  11222. (setq tbl (cdr tbl)))))
  11223. (t
  11224. (setq tg (copy-sequence (car e)) c2 nil)
  11225. (if (cdr e)
  11226. (setq c (cdr e))
  11227. ;; automatically assign a character.
  11228. (setq c1 (string-to-char
  11229. (downcase (substring
  11230. tg (if (= (string-to-char tg) ?@) 1 0)))))
  11231. (if (or (rassoc c1 ntable) (rassoc c1 table))
  11232. (while (or (rassoc char ntable) (rassoc char table))
  11233. (setq char (1+ char)))
  11234. (setq c2 c1))
  11235. (setq c (or c2 char)))
  11236. (if ingroup (push tg (car groups)))
  11237. (setq tg (org-add-props tg nil 'face
  11238. (cond
  11239. ((not (assoc tg table))
  11240. (org-get-todo-face tg))
  11241. ((member tg current) c-face)
  11242. ((member tg inherited) i-face)
  11243. (t nil))))
  11244. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11245. (insert "[" c "] " tg (make-string
  11246. (- fwidth 4 (length tg)) ?\ ))
  11247. (push (cons tg c) ntable)
  11248. (when (= (setq cnt (1+ cnt)) ncol)
  11249. (insert "\n")
  11250. (if ingroup (insert " "))
  11251. (setq cnt 0)))))
  11252. (setq ntable (nreverse ntable))
  11253. (insert "\n")
  11254. (goto-char (point-min))
  11255. (if (not expert) (org-fit-window-to-buffer))
  11256. (setq rtn
  11257. (catch 'exit
  11258. (while t
  11259. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  11260. (if (not groups) "no " "")
  11261. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  11262. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11263. (setq org-last-tag-selection-key c)
  11264. (cond
  11265. ((= c ?\r) (throw 'exit t))
  11266. ((= c ?!)
  11267. (setq groups (not groups))
  11268. (goto-char (point-min))
  11269. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  11270. ((= c ?\C-c)
  11271. (if (not expert)
  11272. (org-fast-tag-show-exit
  11273. (setq exit-after-next (not exit-after-next)))
  11274. (setq expert nil)
  11275. (delete-other-windows)
  11276. (split-window-vertically)
  11277. (org-switch-to-buffer-other-window " *Org tags*")
  11278. (org-fit-window-to-buffer)))
  11279. ((or (= c ?\C-g)
  11280. (and (= c ?q) (not (rassoc c ntable))))
  11281. (org-detach-overlay org-tags-overlay)
  11282. (setq quit-flag t))
  11283. ((= c ?\ )
  11284. (setq current nil)
  11285. (if exit-after-next (setq exit-after-next 'now)))
  11286. ((= c ?\t)
  11287. (condition-case nil
  11288. (setq tg (org-icompleting-read
  11289. "Tag: "
  11290. (or buffer-tags
  11291. (with-current-buffer buf
  11292. (org-get-buffer-tags)))))
  11293. (quit (setq tg "")))
  11294. (when (string-match "\\S-" tg)
  11295. (add-to-list 'buffer-tags (list tg))
  11296. (if (member tg current)
  11297. (setq current (delete tg current))
  11298. (push tg current)))
  11299. (if exit-after-next (setq exit-after-next 'now)))
  11300. ((setq e (rassoc c todo-table) tg (car e))
  11301. (with-current-buffer buf
  11302. (save-excursion (org-todo tg)))
  11303. (if exit-after-next (setq exit-after-next 'now)))
  11304. ((setq e (rassoc c ntable) tg (car e))
  11305. (if (member tg current)
  11306. (setq current (delete tg current))
  11307. (loop for g in groups do
  11308. (if (member tg g)
  11309. (mapc (lambda (x)
  11310. (setq current (delete x current)))
  11311. g)))
  11312. (push tg current))
  11313. (if exit-after-next (setq exit-after-next 'now))))
  11314. ;; Create a sorted list
  11315. (setq current
  11316. (sort current
  11317. (lambda (a b)
  11318. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  11319. (if (eq exit-after-next 'now) (throw 'exit t))
  11320. (goto-char (point-min))
  11321. (beginning-of-line 2)
  11322. (delete-region (point) (point-at-eol))
  11323. (org-fast-tag-insert "Current" current c-face)
  11324. (org-set-current-tags-overlay current ov-prefix)
  11325. (while (re-search-forward
  11326. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  11327. (setq tg (match-string 1))
  11328. (add-text-properties
  11329. (match-beginning 1) (match-end 1)
  11330. (list 'face
  11331. (cond
  11332. ((member tg current) c-face)
  11333. ((member tg inherited) i-face)
  11334. (t (get-text-property (match-beginning 1) 'face))))))
  11335. (goto-char (point-min)))))
  11336. (org-detach-overlay org-tags-overlay)
  11337. (if rtn
  11338. (mapconcat 'identity current ":")
  11339. nil))))
  11340. (defun org-get-tags-string ()
  11341. "Get the TAGS string in the current headline."
  11342. (unless (org-on-heading-p t)
  11343. (error "Not on a heading"))
  11344. (save-excursion
  11345. (beginning-of-line 1)
  11346. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11347. (org-match-string-no-properties 1)
  11348. "")))
  11349. (defun org-get-tags ()
  11350. "Get the list of tags specified in the current headline."
  11351. (org-split-string (org-get-tags-string) ":"))
  11352. (defun org-get-buffer-tags ()
  11353. "Get a table of all tags used in the buffer, for completion."
  11354. (let (tags)
  11355. (save-excursion
  11356. (goto-char (point-min))
  11357. (while (re-search-forward
  11358. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  11359. (when (equal (char-after (point-at-bol 0)) ?*)
  11360. (mapc (lambda (x) (add-to-list 'tags x))
  11361. (org-split-string (org-match-string-no-properties 1) ":")))))
  11362. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  11363. (mapcar 'list tags)))
  11364. ;;;; The mapping API
  11365. ;;;###autoload
  11366. (defun org-map-entries (func &optional match scope &rest skip)
  11367. "Call FUNC at each headline selected by MATCH in SCOPE.
  11368. FUNC is a function or a lisp form. The function will be called without
  11369. arguments, with the cursor positioned at the beginning of the headline.
  11370. The return values of all calls to the function will be collected and
  11371. returned as a list.
  11372. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  11373. does not need to preserve point. After evaluation, the cursor will be
  11374. moved to the end of the line (presumably of the headline of the
  11375. processed entry) and search continues from there. Under some
  11376. circumstances, this may not produce the wanted results. For example,
  11377. if you have removed (e.g. archived) the current (sub)tree it could
  11378. mean that the next entry will be skipped entirely. In such cases, you
  11379. can specify the position from where search should continue by making
  11380. FUNC set the variable `org-map-continue-from' to the desired buffer
  11381. position.
  11382. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  11383. Only headlines that are matched by this query will be considered during
  11384. the iteration. When MATCH is nil or t, all headlines will be
  11385. visited by the iteration.
  11386. SCOPE determines the scope of this command. It can be any of:
  11387. nil The current buffer, respecting the restriction if any
  11388. tree The subtree started with the entry at point
  11389. file The current buffer, without restriction
  11390. file-with-archives
  11391. The current buffer, and any archives associated with it
  11392. agenda All agenda files
  11393. agenda-with-archives
  11394. All agenda files with any archive files associated with them
  11395. \(file1 file2 ...)
  11396. If this is a list, all files in the list will be scanned
  11397. The remaining args are treated as settings for the skipping facilities of
  11398. the scanner. The following items can be given here:
  11399. archive skip trees with the archive tag.
  11400. comment skip trees with the COMMENT keyword
  11401. function or Emacs Lisp form:
  11402. will be used as value for `org-agenda-skip-function', so whenever
  11403. the function returns t, FUNC will not be called for that
  11404. entry and search will continue from the point where the
  11405. function leaves it.
  11406. If your function needs to retrieve the tags including inherited tags
  11407. at the *current* entry, you can use the value of the variable
  11408. `org-scanner-tags' which will be much faster than getting the value
  11409. with `org-get-tags-at'. If your function gets properties with
  11410. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  11411. to t around the call to `org-entry-properties' to get the same speedup.
  11412. Note that if your function moves around to retrieve tags and properties at
  11413. a *different* entry, you cannot use these techniques."
  11414. (let* ((org-agenda-archives-mode nil) ; just to make sure
  11415. (org-agenda-skip-archived-trees (memq 'archive skip))
  11416. (org-agenda-skip-comment-trees (memq 'comment skip))
  11417. (org-agenda-skip-function
  11418. (car (org-delete-all '(comment archive) skip)))
  11419. (org-tags-match-list-sublevels t)
  11420. matcher file res
  11421. org-todo-keywords-for-agenda
  11422. org-done-keywords-for-agenda
  11423. org-todo-keyword-alist-for-agenda
  11424. org-drawers-for-agenda
  11425. org-tag-alist-for-agenda)
  11426. (cond
  11427. ((eq match t) (setq matcher t))
  11428. ((eq match nil) (setq matcher t))
  11429. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  11430. (save-excursion
  11431. (save-restriction
  11432. (when (eq scope 'tree)
  11433. (org-back-to-heading t)
  11434. (org-narrow-to-subtree)
  11435. (setq scope nil))
  11436. (if (not scope)
  11437. (progn
  11438. (org-prepare-agenda-buffers
  11439. (list (buffer-file-name (current-buffer))))
  11440. (setq res (org-scan-tags func matcher)))
  11441. ;; Get the right scope
  11442. (cond
  11443. ((and scope (listp scope) (symbolp (car scope)))
  11444. (setq scope (eval scope)))
  11445. ((eq scope 'agenda)
  11446. (setq scope (org-agenda-files t)))
  11447. ((eq scope 'agenda-with-archives)
  11448. (setq scope (org-agenda-files t))
  11449. (setq scope (org-add-archive-files scope)))
  11450. ((eq scope 'file)
  11451. (setq scope (list (buffer-file-name))))
  11452. ((eq scope 'file-with-archives)
  11453. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  11454. (org-prepare-agenda-buffers scope)
  11455. (while (setq file (pop scope))
  11456. (with-current-buffer (org-find-base-buffer-visiting file)
  11457. (save-excursion
  11458. (save-restriction
  11459. (widen)
  11460. (goto-char (point-min))
  11461. (setq res (append res (org-scan-tags func matcher))))))))))
  11462. res))
  11463. ;;;; Properties
  11464. ;;; Setting and retrieving properties
  11465. (defconst org-special-properties
  11466. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  11467. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED")
  11468. "The special properties valid in Org-mode.
  11469. These are properties that are not defined in the property drawer,
  11470. but in some other way.")
  11471. (defconst org-default-properties
  11472. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  11473. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  11474. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  11475. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  11476. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  11477. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  11478. "Some properties that are used by Org-mode for various purposes.
  11479. Being in this list makes sure that they are offered for completion.")
  11480. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  11481. "Regular expression matching the first line of a property drawer.")
  11482. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  11483. "Regular expression matching the last line of a property drawer.")
  11484. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  11485. "Regular expression matching the first line of a property drawer.")
  11486. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  11487. "Regular expression matching the first line of a property drawer.")
  11488. (defconst org-property-drawer-re
  11489. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  11490. org-property-end-re "\\)\n?")
  11491. "Matches an entire property drawer.")
  11492. (defconst org-clock-drawer-re
  11493. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  11494. org-property-end-re "\\)\n?")
  11495. "Matches an entire clock drawer.")
  11496. (defun org-property-action ()
  11497. "Do an action on properties."
  11498. (interactive)
  11499. (let (c)
  11500. (org-at-property-p)
  11501. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  11502. (setq c (read-char-exclusive))
  11503. (cond
  11504. ((equal c ?s)
  11505. (call-interactively 'org-set-property))
  11506. ((equal c ?d)
  11507. (call-interactively 'org-delete-property))
  11508. ((equal c ?D)
  11509. (call-interactively 'org-delete-property-globally))
  11510. ((equal c ?c)
  11511. (call-interactively 'org-compute-property-at-point))
  11512. (t (error "No such property action %c" c)))))
  11513. (defun org-set-effort (&optional value)
  11514. "Set the effort property of the current entry.
  11515. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  11516. allowed value."
  11517. (interactive "P")
  11518. (if (equal value 0) (setq value 10))
  11519. (let* ((completion-ignore-case t)
  11520. (prop org-effort-property)
  11521. (cur (org-entry-get nil prop))
  11522. (allowed (org-property-get-allowed-values nil prop 'table))
  11523. (existing (mapcar 'list (org-property-values prop)))
  11524. rpl
  11525. (val (cond
  11526. ((stringp value) value)
  11527. ((and allowed (integerp value))
  11528. (or (car (nth (1- value) allowed))
  11529. (car (org-last allowed))))
  11530. (allowed
  11531. (message "Select 1-9,0, [RET%s]: %s"
  11532. (if cur (concat "=" cur) "")
  11533. (mapconcat 'car allowed " "))
  11534. (setq rpl (read-char-exclusive))
  11535. (if (equal rpl ?\r)
  11536. cur
  11537. (setq rpl (- rpl ?0))
  11538. (if (equal rpl 0) (setq rpl 10))
  11539. (if (and (> rpl 0) (<= rpl (length allowed)))
  11540. (car (nth (1- rpl) allowed))
  11541. (org-completing-read "Effort: " allowed nil))))
  11542. (t
  11543. (let (org-completion-use-ido org-completion-use-iswitchb)
  11544. (org-completing-read
  11545. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  11546. (concat "[" cur "]") "")
  11547. ": ")
  11548. existing nil nil "" nil cur))))))
  11549. (unless (equal (org-entry-get nil prop) val)
  11550. (org-entry-put nil prop val))
  11551. (message "%s is now %s" prop val)))
  11552. (defun org-at-property-p ()
  11553. "Is cursor inside a property drawer?"
  11554. (save-excursion
  11555. (beginning-of-line 1)
  11556. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  11557. (let ((match (match-data)) ;; Keep match-data for use by calling
  11558. (p (point)) ;; procedures.
  11559. (range (unless (org-before-first-heading-p)
  11560. (org-get-property-block))))
  11561. (prog1 (and range (<= (car range) p) (< p (cdr range)))
  11562. (set-match-data match))))))
  11563. (defun org-get-property-block (&optional beg end force)
  11564. "Return the (beg . end) range of the body of the property drawer.
  11565. BEG and END can be beginning and end of subtree, if not given
  11566. they will be found.
  11567. If the drawer does not exist and FORCE is non-nil, create the drawer."
  11568. (catch 'exit
  11569. (save-excursion
  11570. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  11571. (end (or end (progn (outline-next-heading) (point)))))
  11572. (goto-char beg)
  11573. (if (re-search-forward org-property-start-re end t)
  11574. (setq beg (1+ (match-end 0)))
  11575. (if force
  11576. (save-excursion
  11577. (org-insert-property-drawer)
  11578. (setq end (progn (outline-next-heading) (point))))
  11579. (throw 'exit nil))
  11580. (goto-char beg)
  11581. (if (re-search-forward org-property-start-re end t)
  11582. (setq beg (1+ (match-end 0)))))
  11583. (if (re-search-forward org-property-end-re end t)
  11584. (setq end (match-beginning 0))
  11585. (or force (throw 'exit nil))
  11586. (goto-char beg)
  11587. (setq end beg)
  11588. (org-indent-line-function)
  11589. (insert ":END:\n"))
  11590. (cons beg end)))))
  11591. (defun org-entry-properties (&optional pom which specific)
  11592. "Get all properties of the entry at point-or-marker POM.
  11593. This includes the TODO keyword, the tags, time strings for deadline,
  11594. scheduled, and clocking, and any additional properties defined in the
  11595. entry. The return value is an alist, keys may occur multiple times
  11596. if the property key was used several times.
  11597. POM may also be nil, in which case the current entry is used.
  11598. If WHICH is nil or `all', get all properties. If WHICH is
  11599. `special' or `standard', only get that subclass. If WHICH
  11600. is a string only get exactly this property. Specific can be a string, the
  11601. specific property we are interested in. Specifying it can speed
  11602. things up because then unnecessary parsing is avoided."
  11603. (setq which (or which 'all))
  11604. (org-with-point-at pom
  11605. (let ((clockstr (substring org-clock-string 0 -1))
  11606. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  11607. (case-fold-search nil)
  11608. beg end range props sum-props key value string clocksum)
  11609. (save-excursion
  11610. (when (condition-case nil
  11611. (and (org-mode-p) (org-back-to-heading t))
  11612. (error nil))
  11613. (setq beg (point))
  11614. (setq sum-props (get-text-property (point) 'org-summaries))
  11615. (setq clocksum (get-text-property (point) :org-clock-minutes))
  11616. (outline-next-heading)
  11617. (setq end (point))
  11618. (when (memq which '(all special))
  11619. ;; Get the special properties, like TODO and tags
  11620. (goto-char beg)
  11621. (when (and (or (not specific) (string= specific "TODO"))
  11622. (looking-at org-todo-line-regexp) (match-end 2))
  11623. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  11624. (when (and (or (not specific) (string= specific "PRIORITY"))
  11625. (looking-at org-priority-regexp))
  11626. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  11627. (when (and (or (not specific) (string= specific "TAGS"))
  11628. (setq value (org-get-tags-string))
  11629. (string-match "\\S-" value))
  11630. (push (cons "TAGS" value) props))
  11631. (when (and (or (not specific) (string= specific "ALLTAGS"))
  11632. (setq value (org-get-tags-at)))
  11633. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  11634. ":"))
  11635. props))
  11636. (when (or (not specific) (string= specific "BLOCKED"))
  11637. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  11638. (when (or (not specific)
  11639. (member specific org-all-time-keywords)
  11640. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  11641. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  11642. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  11643. string (if (equal key clockstr)
  11644. (org-no-properties
  11645. (org-trim
  11646. (buffer-substring
  11647. (match-beginning 3) (goto-char (point-at-eol)))))
  11648. (substring (org-match-string-no-properties 3) 1 -1)))
  11649. (unless key
  11650. (if (= (char-after (match-beginning 3)) ?\[)
  11651. (setq key "TIMESTAMP_IA")
  11652. (setq key "TIMESTAMP")))
  11653. (when (or (equal key clockstr) (not (assoc key props)))
  11654. (push (cons key string) props))))
  11655. )
  11656. (when (memq which '(all standard))
  11657. ;; Get the standard properties, like :PROP: ...
  11658. (setq range (org-get-property-block beg end))
  11659. (when range
  11660. (goto-char (car range))
  11661. (while (re-search-forward
  11662. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  11663. (cdr range) t)
  11664. (setq key (org-match-string-no-properties 1)
  11665. value (org-trim (or (org-match-string-no-properties 2) "")))
  11666. (unless (member key excluded)
  11667. (push (cons key (or value "")) props)))))
  11668. (if clocksum
  11669. (push (cons "CLOCKSUM"
  11670. (org-columns-number-to-string (/ (float clocksum) 60.)
  11671. 'add_times))
  11672. props))
  11673. (unless (assoc "CATEGORY" props)
  11674. (setq value (or (org-get-category)
  11675. (progn (org-refresh-category-properties)
  11676. (org-get-category))))
  11677. (push (cons "CATEGORY" value) props))
  11678. (append sum-props (nreverse props)))))))
  11679. (defun org-entry-get (pom property &optional inherit)
  11680. "Get value of PROPERTY for entry at point-or-marker POM.
  11681. If INHERIT is non-nil and the entry does not have the property,
  11682. then also check higher levels of the hierarchy.
  11683. If INHERIT is the symbol `selective', use inheritance only if the setting
  11684. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  11685. If the property is present but empty, the return value is the empty string.
  11686. If the property is not present at all, nil is returned."
  11687. (org-with-point-at pom
  11688. (if (and inherit (if (eq inherit 'selective)
  11689. (org-property-inherit-p property)
  11690. t))
  11691. (org-entry-get-with-inheritance property)
  11692. (if (member property org-special-properties)
  11693. ;; We need a special property. Use `org-entry-properties' to
  11694. ;; retrieve it, but specify the wanted property
  11695. (cdr (assoc property (org-entry-properties nil 'special property)))
  11696. (let ((range (org-get-property-block)))
  11697. (if (and range
  11698. (goto-char (car range))
  11699. (re-search-forward
  11700. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  11701. (cdr range) t))
  11702. ;; Found the property, return it.
  11703. (if (match-end 1)
  11704. (org-match-string-no-properties 1)
  11705. "")))))))
  11706. (defun org-property-or-variable-value (var &optional inherit)
  11707. "Check if there is a property fixing the value of VAR.
  11708. If yes, return this value. If not, return the current value of the variable."
  11709. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  11710. (if (and prop (stringp prop) (string-match "\\S-" prop))
  11711. (read prop)
  11712. (symbol-value var))))
  11713. (defun org-entry-delete (pom property)
  11714. "Delete the property PROPERTY from entry at point-or-marker POM."
  11715. (org-with-point-at pom
  11716. (if (member property org-special-properties)
  11717. nil ; cannot delete these properties.
  11718. (let ((range (org-get-property-block)))
  11719. (if (and range
  11720. (goto-char (car range))
  11721. (re-search-forward
  11722. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  11723. (cdr range) t))
  11724. (progn
  11725. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  11726. t)
  11727. nil)))))
  11728. ;; Multi-values properties are properties that contain multiple values
  11729. ;; These values are assumed to be single words, separated by whitespace.
  11730. (defun org-entry-add-to-multivalued-property (pom property value)
  11731. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  11732. (let* ((old (org-entry-get pom property))
  11733. (values (and old (org-split-string old "[ \t]"))))
  11734. (setq value (org-entry-protect-space value))
  11735. (unless (member value values)
  11736. (setq values (cons value values))
  11737. (org-entry-put pom property
  11738. (mapconcat 'identity values " ")))))
  11739. (defun org-entry-remove-from-multivalued-property (pom property value)
  11740. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  11741. (let* ((old (org-entry-get pom property))
  11742. (values (and old (org-split-string old "[ \t]"))))
  11743. (setq value (org-entry-protect-space value))
  11744. (when (member value values)
  11745. (setq values (delete value values))
  11746. (org-entry-put pom property
  11747. (mapconcat 'identity values " ")))))
  11748. (defun org-entry-member-in-multivalued-property (pom property value)
  11749. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  11750. (let* ((old (org-entry-get pom property))
  11751. (values (and old (org-split-string old "[ \t]"))))
  11752. (setq value (org-entry-protect-space value))
  11753. (member value values)))
  11754. (defun org-entry-get-multivalued-property (pom property)
  11755. "Return a list of values in a multivalued property."
  11756. (let* ((value (org-entry-get pom property))
  11757. (values (and value (org-split-string value "[ \t]"))))
  11758. (mapcar 'org-entry-restore-space values)))
  11759. (defun org-entry-put-multivalued-property (pom property &rest values)
  11760. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  11761. VALUES should be a list of strings. Spaces will be protected."
  11762. (org-entry-put pom property
  11763. (mapconcat 'org-entry-protect-space values " "))
  11764. (let* ((value (org-entry-get pom property))
  11765. (values (and value (org-split-string value "[ \t]"))))
  11766. (mapcar 'org-entry-restore-space values)))
  11767. (defun org-entry-protect-space (s)
  11768. "Protect spaces and newline in string S."
  11769. (while (string-match " " s)
  11770. (setq s (replace-match "%20" t t s)))
  11771. (while (string-match "\n" s)
  11772. (setq s (replace-match "%0A" t t s)))
  11773. s)
  11774. (defun org-entry-restore-space (s)
  11775. "Restore spaces and newline in string S."
  11776. (while (string-match "%20" s)
  11777. (setq s (replace-match " " t t s)))
  11778. (while (string-match "%0A" s)
  11779. (setq s (replace-match "\n" t t s)))
  11780. s)
  11781. (defvar org-entry-property-inherited-from (make-marker)
  11782. "Marker pointing to the entry from where a property was inherited.
  11783. Each call to `org-entry-get-with-inheritance' will set this marker to the
  11784. location of the entry where the inheritance search matched. If there was
  11785. no match, the marker will point nowhere.
  11786. Note that also `org-entry-get' calls this function, if the INHERIT flag
  11787. is set.")
  11788. (defun org-entry-get-with-inheritance (property)
  11789. "Get entry property, and search higher levels if not present."
  11790. (move-marker org-entry-property-inherited-from nil)
  11791. (let (tmp)
  11792. (save-excursion
  11793. (save-restriction
  11794. (widen)
  11795. (catch 'ex
  11796. (while t
  11797. (when (setq tmp (org-entry-get nil property))
  11798. (org-back-to-heading t)
  11799. (move-marker org-entry-property-inherited-from (point))
  11800. (throw 'ex tmp))
  11801. (or (org-up-heading-safe) (throw 'ex nil)))))
  11802. (or tmp
  11803. (cdr (assoc property org-file-properties))
  11804. (cdr (assoc property org-global-properties))
  11805. (cdr (assoc property org-global-properties-fixed))))))
  11806. (defvar org-property-changed-functions nil
  11807. "Hook called when the value of a property has changed.
  11808. Each hook function should accept two arguments, the name of the property
  11809. and the new value.")
  11810. (defun org-entry-put (pom property value)
  11811. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  11812. (org-with-point-at pom
  11813. (org-back-to-heading t)
  11814. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  11815. range)
  11816. (cond
  11817. ((equal property "TODO")
  11818. (when (and (stringp value) (string-match "\\S-" value)
  11819. (not (member value org-todo-keywords-1)))
  11820. (error "\"%s\" is not a valid TODO state" value))
  11821. (if (or (not value)
  11822. (not (string-match "\\S-" value)))
  11823. (setq value 'none))
  11824. (org-todo value)
  11825. (org-set-tags nil 'align))
  11826. ((equal property "PRIORITY")
  11827. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  11828. (string-to-char value) ?\ ))
  11829. (org-set-tags nil 'align))
  11830. ((equal property "SCHEDULED")
  11831. (if (re-search-forward org-scheduled-time-regexp end t)
  11832. (cond
  11833. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11834. ((eq value 'later) (org-timestamp-change 1 'day))
  11835. (t (call-interactively 'org-schedule)))
  11836. (call-interactively 'org-schedule)))
  11837. ((equal property "DEADLINE")
  11838. (if (re-search-forward org-deadline-time-regexp end t)
  11839. (cond
  11840. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11841. ((eq value 'later) (org-timestamp-change 1 'day))
  11842. (t (call-interactively 'org-deadline)))
  11843. (call-interactively 'org-deadline)))
  11844. ((member property org-special-properties)
  11845. (error "The %s property can not yet be set with `org-entry-put'"
  11846. property))
  11847. (t ; a non-special property
  11848. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  11849. (setq range (org-get-property-block beg end 'force))
  11850. (goto-char (car range))
  11851. (if (re-search-forward
  11852. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  11853. (progn
  11854. (delete-region (match-beginning 1) (match-end 1))
  11855. (goto-char (match-beginning 1)))
  11856. (goto-char (cdr range))
  11857. (insert "\n")
  11858. (backward-char 1)
  11859. (org-indent-line-function)
  11860. (insert ":" property ":"))
  11861. (and value (insert " " value))
  11862. (org-indent-line-function)))))
  11863. (run-hook-with-args 'org-property-changed-functions property value)))
  11864. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  11865. "Get all property keys in the current buffer.
  11866. With INCLUDE-SPECIALS, also list the special properties that reflect things
  11867. like tags and TODO state.
  11868. With INCLUDE-DEFAULTS, also include properties that has special meaning
  11869. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  11870. With INCLUDE-COLUMNS, also include property names given in COLUMN
  11871. formats in the current buffer."
  11872. (let (rtn range cfmt s p)
  11873. (save-excursion
  11874. (save-restriction
  11875. (widen)
  11876. (goto-char (point-min))
  11877. (while (re-search-forward org-property-start-re nil t)
  11878. (setq range (org-get-property-block))
  11879. (goto-char (car range))
  11880. (while (re-search-forward
  11881. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  11882. (cdr range) t)
  11883. (add-to-list 'rtn (org-match-string-no-properties 1)))
  11884. (outline-next-heading))))
  11885. (when include-specials
  11886. (setq rtn (append org-special-properties rtn)))
  11887. (when include-defaults
  11888. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  11889. (add-to-list 'rtn org-effort-property))
  11890. (when include-columns
  11891. (save-excursion
  11892. (save-restriction
  11893. (widen)
  11894. (goto-char (point-min))
  11895. (while (re-search-forward
  11896. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  11897. nil t)
  11898. (setq cfmt (match-string 2) s 0)
  11899. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  11900. cfmt s)
  11901. (setq s (match-end 0)
  11902. p (match-string 1 cfmt))
  11903. (unless (or (equal p "ITEM")
  11904. (member p org-special-properties))
  11905. (add-to-list 'rtn (match-string 1 cfmt))))))))
  11906. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  11907. (defun org-property-values (key)
  11908. "Return a list of all values of property KEY."
  11909. (save-excursion
  11910. (save-restriction
  11911. (widen)
  11912. (goto-char (point-min))
  11913. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  11914. values)
  11915. (while (re-search-forward re nil t)
  11916. (add-to-list 'values (org-trim (match-string 1))))
  11917. (delete "" values)))))
  11918. (defun org-insert-property-drawer ()
  11919. "Insert a property drawer into the current entry."
  11920. (interactive)
  11921. (org-back-to-heading t)
  11922. (looking-at outline-regexp)
  11923. (let ((indent (if org-adapt-indentation
  11924. (- (match-end 0)(match-beginning 0))
  11925. 0))
  11926. (beg (point))
  11927. (re (concat "^[ \t]*" org-keyword-time-regexp))
  11928. end hiddenp)
  11929. (outline-next-heading)
  11930. (setq end (point))
  11931. (goto-char beg)
  11932. (while (re-search-forward re end t))
  11933. (setq hiddenp (org-invisible-p))
  11934. (end-of-line 1)
  11935. (and (equal (char-after) ?\n) (forward-char 1))
  11936. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  11937. (if (member (match-string 1) '("CLOCK:" ":END:"))
  11938. ;; just skip this line
  11939. (beginning-of-line 2)
  11940. ;; Drawer start, find the end
  11941. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  11942. (beginning-of-line 1)))
  11943. (org-skip-over-state-notes)
  11944. (skip-chars-backward " \t\n\r")
  11945. (if (eq (char-before) ?*) (forward-char 1))
  11946. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  11947. (beginning-of-line 0)
  11948. (org-indent-to-column indent)
  11949. (beginning-of-line 2)
  11950. (org-indent-to-column indent)
  11951. (beginning-of-line 0)
  11952. (if hiddenp
  11953. (save-excursion
  11954. (org-back-to-heading t)
  11955. (hide-entry))
  11956. (org-flag-drawer t))))
  11957. (defun org-set-property (property value)
  11958. "In the current entry, set PROPERTY to VALUE.
  11959. When called interactively, this will prompt for a property name, offering
  11960. completion on existing and default properties. And then it will prompt
  11961. for a value, offering completion either on allowed values (via an inherited
  11962. xxx_ALL property) or on existing values in other instances of this property
  11963. in the current file."
  11964. (interactive
  11965. (let* ((completion-ignore-case t)
  11966. (keys (org-buffer-property-keys nil t t))
  11967. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  11968. (prop (if (member prop0 keys)
  11969. prop0
  11970. (or (cdr (assoc (downcase prop0)
  11971. (mapcar (lambda (x) (cons (downcase x) x))
  11972. keys)))
  11973. prop0)))
  11974. (cur (org-entry-get nil prop))
  11975. (prompt (concat prop " value"
  11976. (if (and cur (string-match "\\S-" cur))
  11977. (concat " [" cur "]") "") ": "))
  11978. (allowed (org-property-get-allowed-values nil prop 'table))
  11979. (existing (mapcar 'list (org-property-values prop)))
  11980. (val (if allowed
  11981. (org-completing-read prompt allowed nil
  11982. (not (get-text-property 0 'org-unrestricted
  11983. (caar allowed))))
  11984. (let (org-completion-use-ido org-completion-use-iswitchb)
  11985. (org-completing-read prompt existing nil nil "" nil cur)))))
  11986. (list prop (if (equal val "") cur val))))
  11987. (unless (equal (org-entry-get nil property) value)
  11988. (org-entry-put nil property value)))
  11989. (defun org-delete-property (property)
  11990. "In the current entry, delete PROPERTY."
  11991. (interactive
  11992. (let* ((completion-ignore-case t)
  11993. (prop (org-icompleting-read "Property: " (org-entry-properties nil 'standard))))
  11994. (list prop)))
  11995. (message "Property %s %s" property
  11996. (if (org-entry-delete nil property)
  11997. "deleted"
  11998. "was not present in the entry")))
  11999. (defun org-delete-property-globally (property)
  12000. "Remove PROPERTY globally, from all entries."
  12001. (interactive
  12002. (let* ((completion-ignore-case t)
  12003. (prop (org-icompleting-read
  12004. "Globally remove property: "
  12005. (mapcar 'list (org-buffer-property-keys)))))
  12006. (list prop)))
  12007. (save-excursion
  12008. (save-restriction
  12009. (widen)
  12010. (goto-char (point-min))
  12011. (let ((cnt 0))
  12012. (while (re-search-forward
  12013. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  12014. nil t)
  12015. (setq cnt (1+ cnt))
  12016. (replace-match ""))
  12017. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12018. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  12019. (defun org-compute-property-at-point ()
  12020. "Compute the property at point.
  12021. This looks for an enclosing column format, extracts the operator and
  12022. then applies it to the property in the column format's scope."
  12023. (interactive)
  12024. (unless (org-at-property-p)
  12025. (error "Not at a property"))
  12026. (let ((prop (org-match-string-no-properties 2)))
  12027. (org-columns-get-format-and-top-level)
  12028. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12029. (error "No operator defined for property %s" prop))
  12030. (org-columns-compute prop)))
  12031. (defvar org-property-allowed-value-functions nil
  12032. "Hook for functions supplying allowed values for a specific property.
  12033. The functions must take a single argument, the name of the property, and
  12034. return a flat list of allowed values. If \":ETC\" is one of
  12035. the values, this means that these values are intended as defaults for
  12036. completion, but that other values should be allowed too.
  12037. The functions must return nil if they are not responsible for this
  12038. property.")
  12039. (defun org-property-get-allowed-values (pom property &optional table)
  12040. "Get allowed values for the property PROPERTY.
  12041. When TABLE is non-nil, return an alist that can directly be used for
  12042. completion."
  12043. (let (vals)
  12044. (cond
  12045. ((equal property "TODO")
  12046. (setq vals (org-with-point-at pom
  12047. (append org-todo-keywords-1 '("")))))
  12048. ((equal property "PRIORITY")
  12049. (let ((n org-lowest-priority))
  12050. (while (>= n org-highest-priority)
  12051. (push (char-to-string n) vals)
  12052. (setq n (1- n)))))
  12053. ((member property org-special-properties))
  12054. ((setq vals (run-hook-with-args-until-success
  12055. 'org-property-allowed-value-functions property)))
  12056. (t
  12057. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12058. (when (and vals (string-match "\\S-" vals))
  12059. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12060. (setq vals (mapcar (lambda (x)
  12061. (cond ((stringp x) x)
  12062. ((numberp x) (number-to-string x))
  12063. ((symbolp x) (symbol-name x))
  12064. (t "???")))
  12065. vals)))))
  12066. (when (member ":ETC" vals)
  12067. (setq vals (remove ":ETC" vals))
  12068. (org-add-props (car vals) '(org-unrestricted t)))
  12069. (if table (mapcar 'list vals) vals)))
  12070. (defun org-property-previous-allowed-value (&optional previous)
  12071. "Switch to the next allowed value for this property."
  12072. (interactive)
  12073. (org-property-next-allowed-value t))
  12074. (defun org-property-next-allowed-value (&optional previous)
  12075. "Switch to the next allowed value for this property."
  12076. (interactive)
  12077. (unless (org-at-property-p)
  12078. (error "Not at a property"))
  12079. (let* ((key (match-string 2))
  12080. (value (match-string 3))
  12081. (allowed (or (org-property-get-allowed-values (point) key)
  12082. (and (member value '("[ ]" "[-]" "[X]"))
  12083. '("[ ]" "[X]"))))
  12084. nval)
  12085. (unless allowed
  12086. (error "Allowed values for this property have not been defined"))
  12087. (if previous (setq allowed (reverse allowed)))
  12088. (if (member value allowed)
  12089. (setq nval (car (cdr (member value allowed)))))
  12090. (setq nval (or nval (car allowed)))
  12091. (if (equal nval value)
  12092. (error "Only one allowed value for this property"))
  12093. (org-at-property-p)
  12094. (replace-match (concat " :" key ": " nval) t t)
  12095. (org-indent-line-function)
  12096. (beginning-of-line 1)
  12097. (skip-chars-forward " \t")
  12098. (run-hook-with-args 'org-property-changed-functions key nval)))
  12099. (defun org-find-entry-with-id (ident)
  12100. "Locate the entry that contains the ID property with exact value IDENT.
  12101. IDENT can be a string, a symbol or a number, this function will search for
  12102. the string representation of it.
  12103. Return the position where this entry starts, or nil if there is no such entry."
  12104. (interactive "sID: ")
  12105. (let ((id (cond
  12106. ((stringp ident) ident)
  12107. ((symbol-name ident) (symbol-name ident))
  12108. ((numberp ident) (number-to-string ident))
  12109. (t (error "IDENT %s must be a string, symbol or number" ident))))
  12110. (case-fold-search nil))
  12111. (save-excursion
  12112. (save-restriction
  12113. (widen)
  12114. (goto-char (point-min))
  12115. (when (re-search-forward
  12116. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  12117. nil t)
  12118. (org-back-to-heading t)
  12119. (point))))))
  12120. ;;;; Timestamps
  12121. (defvar org-last-changed-timestamp nil)
  12122. (defvar org-last-inserted-timestamp nil
  12123. "The last time stamp inserted with `org-insert-time-stamp'.")
  12124. (defvar org-time-was-given) ; dynamically scoped parameter
  12125. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12126. (defvar org-ts-what) ; dynamically scoped parameter
  12127. (defun org-time-stamp (arg &optional inactive)
  12128. "Prompt for a date/time and insert a time stamp.
  12129. If the user specifies a time like HH:MM, or if this command is called
  12130. with a prefix argument, the time stamp will contain date and time.
  12131. Otherwise, only the date will be included. All parts of a date not
  12132. specified by the user will be filled in from the current date/time.
  12133. So if you press just return without typing anything, the time stamp
  12134. will represent the current date/time. If there is already a timestamp
  12135. at the cursor, it will be modified."
  12136. (interactive "P")
  12137. (let* ((ts nil)
  12138. (default-time
  12139. ;; Default time is either today, or, when entering a range,
  12140. ;; the range start.
  12141. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  12142. (save-excursion
  12143. (re-search-backward
  12144. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  12145. (- (point) 20) t)))
  12146. (apply 'encode-time (org-parse-time-string (match-string 1)))
  12147. (current-time)))
  12148. (default-input (and ts (org-get-compact-tod ts)))
  12149. org-time-was-given org-end-time-was-given time)
  12150. (cond
  12151. ((and (org-at-timestamp-p t)
  12152. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  12153. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  12154. (insert "--")
  12155. (setq time (let ((this-command this-command))
  12156. (org-read-date arg 'totime nil nil
  12157. default-time default-input)))
  12158. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  12159. ((org-at-timestamp-p t)
  12160. (setq time (let ((this-command this-command))
  12161. (org-read-date arg 'totime nil nil default-time default-input)))
  12162. (when (org-at-timestamp-p t) ; just to get the match data
  12163. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  12164. (replace-match "")
  12165. (setq org-last-changed-timestamp
  12166. (org-insert-time-stamp
  12167. time (or org-time-was-given arg)
  12168. inactive nil nil (list org-end-time-was-given))))
  12169. (message "Timestamp updated"))
  12170. (t
  12171. (setq time (let ((this-command this-command))
  12172. (org-read-date arg 'totime nil nil default-time default-input)))
  12173. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  12174. nil nil (list org-end-time-was-given))))))
  12175. ;; FIXME: can we use this for something else, like computing time differences?
  12176. (defun org-get-compact-tod (s)
  12177. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  12178. (let* ((t1 (match-string 1 s))
  12179. (h1 (string-to-number (match-string 2 s)))
  12180. (m1 (string-to-number (match-string 3 s)))
  12181. (t2 (and (match-end 4) (match-string 5 s)))
  12182. (h2 (and t2 (string-to-number (match-string 6 s))))
  12183. (m2 (and t2 (string-to-number (match-string 7 s))))
  12184. dh dm)
  12185. (if (not t2)
  12186. t1
  12187. (setq dh (- h2 h1) dm (- m2 m1))
  12188. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  12189. (concat t1 "+" (number-to-string dh)
  12190. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  12191. (defun org-time-stamp-inactive (&optional arg)
  12192. "Insert an inactive time stamp.
  12193. An inactive time stamp is enclosed in square brackets instead of angle
  12194. brackets. It is inactive in the sense that it does not trigger agenda entries,
  12195. does not link to the calendar and cannot be changed with the S-cursor keys.
  12196. So these are more for recording a certain time/date."
  12197. (interactive "P")
  12198. (org-time-stamp arg 'inactive))
  12199. (defvar org-date-ovl (org-make-overlay 1 1))
  12200. (org-overlay-put org-date-ovl 'face 'org-warning)
  12201. (org-detach-overlay org-date-ovl)
  12202. (defvar org-ans1) ; dynamically scoped parameter
  12203. (defvar org-ans2) ; dynamically scoped parameter
  12204. (defvar org-plain-time-of-day-regexp) ; defined below
  12205. (defvar org-overriding-default-time nil) ; dynamically scoped
  12206. (defvar org-read-date-overlay nil)
  12207. (defvar org-dcst nil) ; dynamically scoped
  12208. (defvar org-read-date-history nil)
  12209. (defvar org-read-date-final-answer nil)
  12210. (defun org-read-date (&optional with-time to-time from-string prompt
  12211. default-time default-input)
  12212. "Read a date, possibly a time, and make things smooth for the user.
  12213. The prompt will suggest to enter an ISO date, but you can also enter anything
  12214. which will at least partially be understood by `parse-time-string'.
  12215. Unrecognized parts of the date will default to the current day, month, year,
  12216. hour and minute. If this command is called to replace a timestamp at point,
  12217. of to enter the second timestamp of a range, the default time is taken from the
  12218. existing stamp. For example,
  12219. 3-2-5 --> 2003-02-05
  12220. feb 15 --> currentyear-02-15
  12221. sep 12 9 --> 2009-09-12
  12222. 12:45 --> today 12:45
  12223. 22 sept 0:34 --> currentyear-09-22 0:34
  12224. 12 --> currentyear-currentmonth-12
  12225. Fri --> nearest Friday (today or later)
  12226. etc.
  12227. Furthermore you can specify a relative date by giving, as the *first* thing
  12228. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  12229. change in days weeks, months, years.
  12230. With a single plus or minus, the date is relative to today. With a double
  12231. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  12232. +4d --> four days from today
  12233. +4 --> same as above
  12234. +2w --> two weeks from today
  12235. ++5 --> five days from default date
  12236. The function understands only English month and weekday abbreviations,
  12237. but this can be configured with the variables `parse-time-months' and
  12238. `parse-time-weekdays'.
  12239. While prompting, a calendar is popped up - you can also select the
  12240. date with the mouse (button 1). The calendar shows a period of three
  12241. months. To scroll it to other months, use the keys `>' and `<'.
  12242. If you don't like the calendar, turn it off with
  12243. \(setq org-read-date-popup-calendar nil)
  12244. With optional argument TO-TIME, the date will immediately be converted
  12245. to an internal time.
  12246. With an optional argument WITH-TIME, the prompt will suggest to also
  12247. insert a time. Note that when WITH-TIME is not set, you can still
  12248. enter a time, and this function will inform the calling routine about
  12249. this change. The calling routine may then choose to change the format
  12250. used to insert the time stamp into the buffer to include the time.
  12251. With optional argument FROM-STRING, read from this string instead from
  12252. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  12253. the time/date that is used for everything that is not specified by the
  12254. user."
  12255. (require 'parse-time)
  12256. (let* ((org-time-stamp-rounding-minutes
  12257. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  12258. (org-dcst org-display-custom-times)
  12259. (ct (org-current-time))
  12260. (def (or org-overriding-default-time default-time ct))
  12261. (defdecode (decode-time def))
  12262. (dummy (progn
  12263. (when (< (nth 2 defdecode) org-extend-today-until)
  12264. (setcar (nthcdr 2 defdecode) -1)
  12265. (setcar (nthcdr 1 defdecode) 59)
  12266. (setq def (apply 'encode-time defdecode)
  12267. defdecode (decode-time def)))))
  12268. (calendar-frame-setup nil)
  12269. (calendar-move-hook nil)
  12270. (calendar-view-diary-initially-flag nil)
  12271. (view-diary-entries-initially nil)
  12272. (calendar-view-holidays-initially-flag nil)
  12273. (view-calendar-holidays-initially nil)
  12274. (timestr (format-time-string
  12275. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  12276. (prompt (concat (if prompt (concat prompt " ") "")
  12277. (format "Date+time [%s]: " timestr)))
  12278. ans (org-ans0 "") org-ans1 org-ans2 final)
  12279. (cond
  12280. (from-string (setq ans from-string))
  12281. (org-read-date-popup-calendar
  12282. (save-excursion
  12283. (save-window-excursion
  12284. (calendar)
  12285. (calendar-forward-day (- (time-to-days def)
  12286. (calendar-absolute-from-gregorian
  12287. (calendar-current-date))))
  12288. (org-eval-in-calendar nil t)
  12289. (let* ((old-map (current-local-map))
  12290. (map (copy-keymap calendar-mode-map))
  12291. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  12292. (org-defkey map (kbd "RET") 'org-calendar-select)
  12293. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  12294. 'org-calendar-select-mouse)
  12295. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  12296. 'org-calendar-select-mouse)
  12297. (org-defkey minibuffer-local-map [(meta shift left)]
  12298. (lambda () (interactive)
  12299. (org-eval-in-calendar '(calendar-backward-month 1))))
  12300. (org-defkey minibuffer-local-map [(meta shift right)]
  12301. (lambda () (interactive)
  12302. (org-eval-in-calendar '(calendar-forward-month 1))))
  12303. (org-defkey minibuffer-local-map [(meta shift up)]
  12304. (lambda () (interactive)
  12305. (org-eval-in-calendar '(calendar-backward-year 1))))
  12306. (org-defkey minibuffer-local-map [(meta shift down)]
  12307. (lambda () (interactive)
  12308. (org-eval-in-calendar '(calendar-forward-year 1))))
  12309. (org-defkey minibuffer-local-map [?\e (shift left)]
  12310. (lambda () (interactive)
  12311. (org-eval-in-calendar '(calendar-backward-month 1))))
  12312. (org-defkey minibuffer-local-map [?\e (shift right)]
  12313. (lambda () (interactive)
  12314. (org-eval-in-calendar '(calendar-forward-month 1))))
  12315. (org-defkey minibuffer-local-map [?\e (shift up)]
  12316. (lambda () (interactive)
  12317. (org-eval-in-calendar '(calendar-backward-year 1))))
  12318. (org-defkey minibuffer-local-map [?\e (shift down)]
  12319. (lambda () (interactive)
  12320. (org-eval-in-calendar '(calendar-forward-year 1))))
  12321. (org-defkey minibuffer-local-map [(shift up)]
  12322. (lambda () (interactive)
  12323. (org-eval-in-calendar '(calendar-backward-week 1))))
  12324. (org-defkey minibuffer-local-map [(shift down)]
  12325. (lambda () (interactive)
  12326. (org-eval-in-calendar '(calendar-forward-week 1))))
  12327. (org-defkey minibuffer-local-map [(shift left)]
  12328. (lambda () (interactive)
  12329. (org-eval-in-calendar '(calendar-backward-day 1))))
  12330. (org-defkey minibuffer-local-map [(shift right)]
  12331. (lambda () (interactive)
  12332. (org-eval-in-calendar '(calendar-forward-day 1))))
  12333. (org-defkey minibuffer-local-map ">"
  12334. (lambda () (interactive)
  12335. (org-eval-in-calendar '(scroll-calendar-left 1))))
  12336. (org-defkey minibuffer-local-map "<"
  12337. (lambda () (interactive)
  12338. (org-eval-in-calendar '(scroll-calendar-right 1))))
  12339. (run-hooks 'org-read-date-minibuffer-setup-hook)
  12340. (unwind-protect
  12341. (progn
  12342. (use-local-map map)
  12343. (add-hook 'post-command-hook 'org-read-date-display)
  12344. (setq org-ans0 (read-string prompt default-input
  12345. 'org-read-date-history nil))
  12346. ;; org-ans0: from prompt
  12347. ;; org-ans1: from mouse click
  12348. ;; org-ans2: from calendar motion
  12349. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  12350. (remove-hook 'post-command-hook 'org-read-date-display)
  12351. (use-local-map old-map)
  12352. (when org-read-date-overlay
  12353. (org-delete-overlay org-read-date-overlay)
  12354. (setq org-read-date-overlay nil)))))))
  12355. (t ; Naked prompt only
  12356. (unwind-protect
  12357. (setq ans (read-string prompt default-input
  12358. 'org-read-date-history timestr))
  12359. (when org-read-date-overlay
  12360. (org-delete-overlay org-read-date-overlay)
  12361. (setq org-read-date-overlay nil)))))
  12362. (setq final (org-read-date-analyze ans def defdecode))
  12363. (setq org-read-date-final-answer ans)
  12364. (if to-time
  12365. (apply 'encode-time final)
  12366. (if (and (boundp 'org-time-was-given) org-time-was-given)
  12367. (format "%04d-%02d-%02d %02d:%02d"
  12368. (nth 5 final) (nth 4 final) (nth 3 final)
  12369. (nth 2 final) (nth 1 final))
  12370. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  12371. (defvar def)
  12372. (defvar defdecode)
  12373. (defvar with-time)
  12374. (defvar org-read-date-analyze-futurep nil)
  12375. (defun org-read-date-display ()
  12376. "Display the current date prompt interpretation in the minibuffer."
  12377. (when org-read-date-display-live
  12378. (when org-read-date-overlay
  12379. (org-delete-overlay org-read-date-overlay))
  12380. (let ((p (point)))
  12381. (end-of-line 1)
  12382. (while (not (equal (buffer-substring
  12383. (max (point-min) (- (point) 4)) (point))
  12384. " "))
  12385. (insert " "))
  12386. (goto-char p))
  12387. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  12388. " " (or org-ans1 org-ans2)))
  12389. (org-end-time-was-given nil)
  12390. (f (org-read-date-analyze ans def defdecode))
  12391. (fmts (if org-dcst
  12392. org-time-stamp-custom-formats
  12393. org-time-stamp-formats))
  12394. (fmt (if (or with-time
  12395. (and (boundp 'org-time-was-given) org-time-was-given))
  12396. (cdr fmts)
  12397. (car fmts)))
  12398. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  12399. (when (and org-end-time-was-given
  12400. (string-match org-plain-time-of-day-regexp txt))
  12401. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  12402. org-end-time-was-given
  12403. (substring txt (match-end 0)))))
  12404. (when org-read-date-analyze-futurep
  12405. (setq txt (concat txt " (=>F)")))
  12406. (setq org-read-date-overlay
  12407. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  12408. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  12409. (defun org-read-date-analyze (ans def defdecode)
  12410. "Analyse the combined answer of the date prompt."
  12411. ;; FIXME: cleanup and comment
  12412. (let ((nowdecode (decode-time (current-time)))
  12413. delta deltan deltaw deltadef year month day
  12414. hour minute second wday pm h2 m2 tl wday1
  12415. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  12416. (setq org-read-date-analyze-futurep nil)
  12417. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  12418. (setq ans "+0"))
  12419. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  12420. (setq ans (replace-match "" t t ans)
  12421. deltan (car delta)
  12422. deltaw (nth 1 delta)
  12423. deltadef (nth 2 delta)))
  12424. ;; Check if there is an iso week date in there
  12425. ;; If yes, store the info and postpone interpreting it until the rest
  12426. ;; of the parsing is done
  12427. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  12428. (setq iso-year (if (match-end 1)
  12429. (org-small-year-to-year
  12430. (string-to-number (match-string 1 ans))))
  12431. iso-weekday (if (match-end 3)
  12432. (string-to-number (match-string 3 ans)))
  12433. iso-week (string-to-number (match-string 2 ans)))
  12434. (setq ans (replace-match "" t t ans)))
  12435. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  12436. (when (string-match
  12437. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  12438. (setq year (if (match-end 2)
  12439. (string-to-number (match-string 2 ans))
  12440. (progn (setq kill-year t)
  12441. (string-to-number (format-time-string "%Y"))))
  12442. month (string-to-number (match-string 3 ans))
  12443. day (string-to-number (match-string 4 ans)))
  12444. (if (< year 100) (setq year (+ 2000 year)))
  12445. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12446. t nil ans)))
  12447. ;; Help matching american dates, like 5/30 or 5/30/7
  12448. (when (string-match
  12449. "^ *\\([0-3]?[0-9]\\)/\\([0-1]?[0-9]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  12450. (setq year (if (match-end 4)
  12451. (string-to-number (match-string 4 ans))
  12452. (progn (setq kill-year t)
  12453. (string-to-number (format-time-string "%Y"))))
  12454. month (string-to-number (match-string 1 ans))
  12455. day (string-to-number (match-string 2 ans)))
  12456. (if (< year 100) (setq year (+ 2000 year)))
  12457. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12458. t nil ans)))
  12459. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  12460. ;; If there is a time with am/pm, and *no* time without it, we convert
  12461. ;; so that matching will be successful.
  12462. (loop for i from 1 to 2 do ; twice, for end time as well
  12463. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  12464. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  12465. (setq hour (string-to-number (match-string 1 ans))
  12466. minute (if (match-end 3)
  12467. (string-to-number (match-string 3 ans))
  12468. 0)
  12469. pm (equal ?p
  12470. (string-to-char (downcase (match-string 4 ans)))))
  12471. (if (and (= hour 12) (not pm))
  12472. (setq hour 0)
  12473. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  12474. (setq ans (replace-match (format "%02d:%02d" hour minute)
  12475. t t ans))))
  12476. ;; Check if a time range is given as a duration
  12477. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  12478. (setq hour (string-to-number (match-string 1 ans))
  12479. h2 (+ hour (string-to-number (match-string 3 ans)))
  12480. minute (string-to-number (match-string 2 ans))
  12481. m2 (+ minute (if (match-end 5) (string-to-number
  12482. (match-string 5 ans))0)))
  12483. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  12484. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  12485. t t ans)))
  12486. ;; Check if there is a time range
  12487. (when (boundp 'org-end-time-was-given)
  12488. (setq org-time-was-given nil)
  12489. (when (and (string-match org-plain-time-of-day-regexp ans)
  12490. (match-end 8))
  12491. (setq org-end-time-was-given (match-string 8 ans))
  12492. (setq ans (concat (substring ans 0 (match-beginning 7))
  12493. (substring ans (match-end 7))))))
  12494. (setq tl (parse-time-string ans)
  12495. day (or (nth 3 tl) (nth 3 defdecode))
  12496. month (or (nth 4 tl)
  12497. (if (and org-read-date-prefer-future
  12498. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  12499. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  12500. (nth 4 defdecode)))
  12501. year (or (and (not kill-year) (nth 5 tl))
  12502. (if (and org-read-date-prefer-future
  12503. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  12504. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  12505. (nth 5 defdecode)))
  12506. hour (or (nth 2 tl) (nth 2 defdecode))
  12507. minute (or (nth 1 tl) (nth 1 defdecode))
  12508. second (or (nth 0 tl) 0)
  12509. wday (nth 6 tl))
  12510. (when (and (eq org-read-date-prefer-future 'time)
  12511. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  12512. (equal day (nth 3 nowdecode))
  12513. (equal month (nth 4 nowdecode))
  12514. (equal year (nth 5 nowdecode))
  12515. (nth 2 tl)
  12516. (or (< (nth 2 tl) (nth 2 nowdecode))
  12517. (and (= (nth 2 tl) (nth 2 nowdecode))
  12518. (nth 1 tl)
  12519. (< (nth 1 tl) (nth 1 nowdecode)))))
  12520. (setq day (1+ day)
  12521. futurep t))
  12522. ;; Special date definitions below
  12523. (cond
  12524. (iso-week
  12525. ;; There was an iso week
  12526. (require 'cal-iso)
  12527. (setq futurep nil)
  12528. (setq year (or iso-year year)
  12529. day (or iso-weekday wday 1)
  12530. wday nil ; to make sure that the trigger below does not match
  12531. iso-date (calendar-gregorian-from-absolute
  12532. (calendar-absolute-from-iso
  12533. (list iso-week day year))))
  12534. ; FIXME: Should we also push ISO weeks into the future?
  12535. ; (when (and org-read-date-prefer-future
  12536. ; (not iso-year)
  12537. ; (< (calendar-absolute-from-gregorian iso-date)
  12538. ; (time-to-days (current-time))))
  12539. ; (setq year (1+ year)
  12540. ; iso-date (calendar-gregorian-from-absolute
  12541. ; (calendar-absolute-from-iso
  12542. ; (list iso-week day year)))))
  12543. (setq month (car iso-date)
  12544. year (nth 2 iso-date)
  12545. day (nth 1 iso-date)))
  12546. (deltan
  12547. (setq futurep nil)
  12548. (unless deltadef
  12549. (let ((now (decode-time (current-time))))
  12550. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  12551. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  12552. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  12553. ((equal deltaw "m") (setq month (+ month deltan)))
  12554. ((equal deltaw "y") (setq year (+ year deltan)))))
  12555. ((and wday (not (nth 3 tl)))
  12556. (setq futurep nil)
  12557. ;; Weekday was given, but no day, so pick that day in the week
  12558. ;; on or after the derived date.
  12559. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  12560. (unless (equal wday wday1)
  12561. (setq day (+ day (% (- wday wday1 -7) 7))))))
  12562. (if (and (boundp 'org-time-was-given)
  12563. (nth 2 tl))
  12564. (setq org-time-was-given t))
  12565. (if (< year 100) (setq year (+ 2000 year)))
  12566. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  12567. (setq org-read-date-analyze-futurep futurep)
  12568. (list second minute hour day month year)))
  12569. (defvar parse-time-weekdays)
  12570. (defun org-read-date-get-relative (s today default)
  12571. "Check string S for special relative date string.
  12572. TODAY and DEFAULT are internal times, for today and for a default.
  12573. Return shift list (N what def-flag)
  12574. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  12575. N is the number of WHATs to shift.
  12576. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  12577. the DEFAULT date rather than TODAY."
  12578. (when (and
  12579. (string-match
  12580. (concat
  12581. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  12582. "\\([0-9]+\\)?"
  12583. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  12584. "\\([ \t]\\|$\\)") s)
  12585. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  12586. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  12587. (string-to-char (substring (match-string 1 s) -1))
  12588. ?+))
  12589. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  12590. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  12591. (what (if (match-end 3) (match-string 3 s) "d"))
  12592. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  12593. (date (if rel default today))
  12594. (wday (nth 6 (decode-time date)))
  12595. delta)
  12596. (if wday1
  12597. (progn
  12598. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  12599. (if (= dir ?-) (setq delta (- delta 7)))
  12600. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  12601. (list delta "d" rel))
  12602. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  12603. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  12604. "Turn a user-specified date into the internal representation.
  12605. The internal representation needed by the calendar is (month day year).
  12606. This is a wrapper to handle the brain-dead convention in calendar that
  12607. user function argument order change dependent on argument order."
  12608. (if (boundp 'calendar-date-style)
  12609. (cond
  12610. ((eq calendar-date-style 'american)
  12611. (list arg1 arg2 arg3))
  12612. ((eq calendar-date-style 'european)
  12613. (list arg2 arg1 arg3))
  12614. ((eq calendar-date-style 'iso)
  12615. (list arg2 arg3 arg1)))
  12616. (if (org-bound-and-true-p european-calendar-style)
  12617. (list arg2 arg1 arg3)
  12618. (list arg1 arg2 arg3))))
  12619. (defun org-eval-in-calendar (form &optional keepdate)
  12620. "Eval FORM in the calendar window and return to current window.
  12621. Also, store the cursor date in variable org-ans2."
  12622. (let ((sf (selected-frame))
  12623. (sw (selected-window)))
  12624. (select-window (get-buffer-window "*Calendar*" t))
  12625. (eval form)
  12626. (when (and (not keepdate) (calendar-cursor-to-date))
  12627. (let* ((date (calendar-cursor-to-date))
  12628. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12629. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  12630. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  12631. (select-window sw)
  12632. (org-select-frame-set-input-focus sf)))
  12633. (defun org-calendar-select ()
  12634. "Return to `org-read-date' with the date currently selected.
  12635. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12636. (interactive)
  12637. (when (calendar-cursor-to-date)
  12638. (let* ((date (calendar-cursor-to-date))
  12639. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12640. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12641. (if (active-minibuffer-window) (exit-minibuffer))))
  12642. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  12643. "Insert a date stamp for the date given by the internal TIME.
  12644. WITH-HM means use the stamp format that includes the time of the day.
  12645. INACTIVE means use square brackets instead of angular ones, so that the
  12646. stamp will not contribute to the agenda.
  12647. PRE and POST are optional strings to be inserted before and after the
  12648. stamp.
  12649. The command returns the inserted time stamp."
  12650. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  12651. stamp)
  12652. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  12653. (insert-before-markers (or pre ""))
  12654. (insert-before-markers (setq stamp (format-time-string fmt time)))
  12655. (when (listp extra)
  12656. (setq extra (car extra))
  12657. (if (and (stringp extra)
  12658. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  12659. (setq extra (format "-%02d:%02d"
  12660. (string-to-number (match-string 1 extra))
  12661. (string-to-number (match-string 2 extra))))
  12662. (setq extra nil)))
  12663. (when extra
  12664. (backward-char 1)
  12665. (insert-before-markers extra)
  12666. (forward-char 1))
  12667. (insert-before-markers (or post ""))
  12668. (setq org-last-inserted-timestamp stamp)))
  12669. (defun org-toggle-time-stamp-overlays ()
  12670. "Toggle the use of custom time stamp formats."
  12671. (interactive)
  12672. (setq org-display-custom-times (not org-display-custom-times))
  12673. (unless org-display-custom-times
  12674. (let ((p (point-min)) (bmp (buffer-modified-p)))
  12675. (while (setq p (next-single-property-change p 'display))
  12676. (if (and (get-text-property p 'display)
  12677. (eq (get-text-property p 'face) 'org-date))
  12678. (remove-text-properties
  12679. p (setq p (next-single-property-change p 'display))
  12680. '(display t))))
  12681. (set-buffer-modified-p bmp)))
  12682. (if (featurep 'xemacs)
  12683. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  12684. (org-restart-font-lock)
  12685. (setq org-table-may-need-update t)
  12686. (if org-display-custom-times
  12687. (message "Time stamps are overlayed with custom format")
  12688. (message "Time stamp overlays removed")))
  12689. (defun org-display-custom-time (beg end)
  12690. "Overlay modified time stamp format over timestamp between BEG and END."
  12691. (let* ((ts (buffer-substring beg end))
  12692. t1 w1 with-hm tf time str w2 (off 0))
  12693. (save-match-data
  12694. (setq t1 (org-parse-time-string ts t))
  12695. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  12696. (setq off (- (match-end 0) (match-beginning 0)))))
  12697. (setq end (- end off))
  12698. (setq w1 (- end beg)
  12699. with-hm (and (nth 1 t1) (nth 2 t1))
  12700. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  12701. time (org-fix-decoded-time t1)
  12702. str (org-add-props
  12703. (format-time-string
  12704. (substring tf 1 -1) (apply 'encode-time time))
  12705. nil 'mouse-face 'highlight)
  12706. w2 (length str))
  12707. (if (not (= w2 w1))
  12708. (add-text-properties (1+ beg) (+ 2 beg)
  12709. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  12710. (if (featurep 'xemacs)
  12711. (progn
  12712. (put-text-property beg end 'invisible t)
  12713. (put-text-property beg end 'end-glyph (make-glyph str)))
  12714. (put-text-property beg end 'display str))))
  12715. (defun org-translate-time (string)
  12716. "Translate all timestamps in STRING to custom format.
  12717. But do this only if the variable `org-display-custom-times' is set."
  12718. (when org-display-custom-times
  12719. (save-match-data
  12720. (let* ((start 0)
  12721. (re org-ts-regexp-both)
  12722. t1 with-hm inactive tf time str beg end)
  12723. (while (setq start (string-match re string start))
  12724. (setq beg (match-beginning 0)
  12725. end (match-end 0)
  12726. t1 (save-match-data
  12727. (org-parse-time-string (substring string beg end) t))
  12728. with-hm (and (nth 1 t1) (nth 2 t1))
  12729. inactive (equal (substring string beg (1+ beg)) "[")
  12730. tf (funcall (if with-hm 'cdr 'car)
  12731. org-time-stamp-custom-formats)
  12732. time (org-fix-decoded-time t1)
  12733. str (format-time-string
  12734. (concat
  12735. (if inactive "[" "<") (substring tf 1 -1)
  12736. (if inactive "]" ">"))
  12737. (apply 'encode-time time))
  12738. string (replace-match str t t string)
  12739. start (+ start (length str)))))))
  12740. string)
  12741. (defun org-fix-decoded-time (time)
  12742. "Set 0 instead of nil for the first 6 elements of time.
  12743. Don't touch the rest."
  12744. (let ((n 0))
  12745. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  12746. (defun org-days-to-time (timestamp-string)
  12747. "Difference between TIMESTAMP-STRING and now in days."
  12748. (- (time-to-days (org-time-string-to-time timestamp-string))
  12749. (time-to-days (current-time))))
  12750. (defun org-deadline-close (timestamp-string &optional ndays)
  12751. "Is the time in TIMESTAMP-STRING close to the current date?"
  12752. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  12753. (and (< (org-days-to-time timestamp-string) ndays)
  12754. (not (org-entry-is-done-p))))
  12755. (defun org-get-wdays (ts)
  12756. "Get the deadline lead time appropriate for timestring TS."
  12757. (cond
  12758. ((<= org-deadline-warning-days 0)
  12759. ;; 0 or negative, enforce this value no matter what
  12760. (- org-deadline-warning-days))
  12761. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  12762. ;; lead time is specified.
  12763. (floor (* (string-to-number (match-string 1 ts))
  12764. (cdr (assoc (match-string 2 ts)
  12765. '(("d" . 1) ("w" . 7)
  12766. ("m" . 30.4) ("y" . 365.25)))))))
  12767. ;; go for the default.
  12768. (t org-deadline-warning-days)))
  12769. (defun org-calendar-select-mouse (ev)
  12770. "Return to `org-read-date' with the date currently selected.
  12771. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12772. (interactive "e")
  12773. (mouse-set-point ev)
  12774. (when (calendar-cursor-to-date)
  12775. (let* ((date (calendar-cursor-to-date))
  12776. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12777. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12778. (if (active-minibuffer-window) (exit-minibuffer))))
  12779. (defun org-check-deadlines (ndays)
  12780. "Check if there are any deadlines due or past due.
  12781. A deadline is considered due if it happens within `org-deadline-warning-days'
  12782. days from today's date. If the deadline appears in an entry marked DONE,
  12783. it is not shown. The prefix arg NDAYS can be used to test that many
  12784. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  12785. (interactive "P")
  12786. (let* ((org-warn-days
  12787. (cond
  12788. ((equal ndays '(4)) 100000)
  12789. (ndays (prefix-numeric-value ndays))
  12790. (t (abs org-deadline-warning-days))))
  12791. (case-fold-search nil)
  12792. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  12793. (callback
  12794. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  12795. (message "%d deadlines past-due or due within %d days"
  12796. (org-occur regexp nil callback)
  12797. org-warn-days)))
  12798. (defun org-check-before-date (date)
  12799. "Check if there are deadlines or scheduled entries before DATE."
  12800. (interactive (list (org-read-date)))
  12801. (let ((case-fold-search nil)
  12802. (regexp (concat "\\<\\(" org-deadline-string
  12803. "\\|" org-scheduled-string
  12804. "\\) *<\\([^>]+\\)>"))
  12805. (callback
  12806. (lambda () (time-less-p
  12807. (org-time-string-to-time (match-string 2))
  12808. (org-time-string-to-time date)))))
  12809. (message "%d entries before %s"
  12810. (org-occur regexp nil callback) date)))
  12811. (defun org-check-after-date (date)
  12812. "Check if there are deadlines or scheduled entries after DATE."
  12813. (interactive (list (org-read-date)))
  12814. (let ((case-fold-search nil)
  12815. (regexp (concat "\\<\\(" org-deadline-string
  12816. "\\|" org-scheduled-string
  12817. "\\) *<\\([^>]+\\)>"))
  12818. (callback
  12819. (lambda () (not
  12820. (time-less-p
  12821. (org-time-string-to-time (match-string 2))
  12822. (org-time-string-to-time date))))))
  12823. (message "%d entries after %s"
  12824. (org-occur regexp nil callback) date)))
  12825. (defun org-evaluate-time-range (&optional to-buffer)
  12826. "Evaluate a time range by computing the difference between start and end.
  12827. Normally the result is just printed in the echo area, but with prefix arg
  12828. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  12829. If the time range is actually in a table, the result is inserted into the
  12830. next column.
  12831. For time difference computation, a year is assumed to be exactly 365
  12832. days in order to avoid rounding problems."
  12833. (interactive "P")
  12834. (or
  12835. (org-clock-update-time-maybe)
  12836. (save-excursion
  12837. (unless (org-at-date-range-p t)
  12838. (goto-char (point-at-bol))
  12839. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  12840. (if (not (org-at-date-range-p t))
  12841. (error "Not at a time-stamp range, and none found in current line")))
  12842. (let* ((ts1 (match-string 1))
  12843. (ts2 (match-string 2))
  12844. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  12845. (match-end (match-end 0))
  12846. (time1 (org-time-string-to-time ts1))
  12847. (time2 (org-time-string-to-time ts2))
  12848. (t1 (org-float-time time1))
  12849. (t2 (org-float-time time2))
  12850. (diff (abs (- t2 t1)))
  12851. (negative (< (- t2 t1) 0))
  12852. ;; (ys (floor (* 365 24 60 60)))
  12853. (ds (* 24 60 60))
  12854. (hs (* 60 60))
  12855. (fy "%dy %dd %02d:%02d")
  12856. (fy1 "%dy %dd")
  12857. (fd "%dd %02d:%02d")
  12858. (fd1 "%dd")
  12859. (fh "%02d:%02d")
  12860. y d h m align)
  12861. (if havetime
  12862. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12863. y 0
  12864. d (floor (/ diff ds)) diff (mod diff ds)
  12865. h (floor (/ diff hs)) diff (mod diff hs)
  12866. m (floor (/ diff 60)))
  12867. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12868. y 0
  12869. d (floor (+ (/ diff ds) 0.5))
  12870. h 0 m 0))
  12871. (if (not to-buffer)
  12872. (message "%s" (org-make-tdiff-string y d h m))
  12873. (if (org-at-table-p)
  12874. (progn
  12875. (goto-char match-end)
  12876. (setq align t)
  12877. (and (looking-at " *|") (goto-char (match-end 0))))
  12878. (goto-char match-end))
  12879. (if (looking-at
  12880. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  12881. (replace-match ""))
  12882. (if negative (insert " -"))
  12883. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  12884. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  12885. (insert " " (format fh h m))))
  12886. (if align (org-table-align))
  12887. (message "Time difference inserted")))))
  12888. (defun org-make-tdiff-string (y d h m)
  12889. (let ((fmt "")
  12890. (l nil))
  12891. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  12892. l (push y l)))
  12893. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  12894. l (push d l)))
  12895. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  12896. l (push h l)))
  12897. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  12898. l (push m l)))
  12899. (apply 'format fmt (nreverse l))))
  12900. (defun org-time-string-to-time (s)
  12901. (apply 'encode-time (org-parse-time-string s)))
  12902. (defun org-time-string-to-seconds (s)
  12903. (org-float-time (org-time-string-to-time s)))
  12904. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  12905. "Convert a time stamp to an absolute day number.
  12906. If there is a specifyer for a cyclic time stamp, get the closest date to
  12907. DAYNR.
  12908. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  12909. the variable date is bound by the calendar when this is called."
  12910. (cond
  12911. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  12912. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  12913. daynr
  12914. (+ daynr 1000)))
  12915. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  12916. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  12917. (time-to-days (current-time))) (match-string 0 s)
  12918. prefer show-all))
  12919. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  12920. (defun org-days-to-iso-week (days)
  12921. "Return the iso week number."
  12922. (require 'cal-iso)
  12923. (car (calendar-iso-from-absolute days)))
  12924. (defun org-small-year-to-year (year)
  12925. "Convert 2-digit years into 4-digit years.
  12926. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  12927. The year 2000 cannot be abbreviated. Any year larger than 99
  12928. is returned unchanged."
  12929. (if (< year 38)
  12930. (setq year (+ 2000 year))
  12931. (if (< year 100)
  12932. (setq year (+ 1900 year))))
  12933. year)
  12934. (defun org-time-from-absolute (d)
  12935. "Return the time corresponding to date D.
  12936. D may be an absolute day number, or a calendar-type list (month day year)."
  12937. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  12938. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  12939. (defun org-calendar-holiday ()
  12940. "List of holidays, for Diary display in Org-mode."
  12941. (require 'holidays)
  12942. (let ((hl (funcall
  12943. (if (fboundp 'calendar-check-holidays)
  12944. 'calendar-check-holidays 'check-calendar-holidays) date)))
  12945. (if hl (mapconcat 'identity hl "; "))))
  12946. (defun org-diary-sexp-entry (sexp entry date)
  12947. "Process a SEXP diary ENTRY for DATE."
  12948. (require 'diary-lib)
  12949. (let ((result (if calendar-debug-sexp
  12950. (let ((stack-trace-on-error t))
  12951. (eval (car (read-from-string sexp))))
  12952. (condition-case nil
  12953. (eval (car (read-from-string sexp)))
  12954. (error
  12955. (beep)
  12956. (message "Bad sexp at line %d in %s: %s"
  12957. (org-current-line)
  12958. (buffer-file-name) sexp)
  12959. (sleep-for 2))))))
  12960. (cond ((stringp result) result)
  12961. ((and (consp result)
  12962. (stringp (cdr result))) (cdr result))
  12963. (result entry)
  12964. (t nil))))
  12965. (defun org-diary-to-ical-string (frombuf)
  12966. "Get iCalendar entries from diary entries in buffer FROMBUF.
  12967. This uses the icalendar.el library."
  12968. (let* ((tmpdir (if (featurep 'xemacs)
  12969. (temp-directory)
  12970. temporary-file-directory))
  12971. (tmpfile (make-temp-name
  12972. (expand-file-name "orgics" tmpdir)))
  12973. buf rtn b e)
  12974. (with-current-buffer frombuf
  12975. (icalendar-export-region (point-min) (point-max) tmpfile)
  12976. (setq buf (find-buffer-visiting tmpfile))
  12977. (set-buffer buf)
  12978. (goto-char (point-min))
  12979. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  12980. (setq b (match-beginning 0)))
  12981. (goto-char (point-max))
  12982. (if (re-search-backward "^END:VEVENT" nil t)
  12983. (setq e (match-end 0)))
  12984. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  12985. (kill-buffer buf)
  12986. (delete-file tmpfile)
  12987. rtn))
  12988. (defun org-closest-date (start current change prefer show-all)
  12989. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  12990. When PREFER is `past' return a date that is either CURRENT or past.
  12991. When PREFER is `future', return a date that is either CURRENT or future.
  12992. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  12993. ;; Make the proper lists from the dates
  12994. (catch 'exit
  12995. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  12996. dn dw sday cday n1 n2 n0
  12997. d m y y1 y2 date1 date2 nmonths nm ny m2)
  12998. (setq start (org-date-to-gregorian start)
  12999. current (org-date-to-gregorian
  13000. (if show-all
  13001. current
  13002. (time-to-days (current-time))))
  13003. sday (calendar-absolute-from-gregorian start)
  13004. cday (calendar-absolute-from-gregorian current))
  13005. (if (<= cday sday) (throw 'exit sday))
  13006. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  13007. (setq dn (string-to-number (match-string 1 change))
  13008. dw (cdr (assoc (match-string 2 change) a1)))
  13009. (error "Invalid change specifyer: %s" change))
  13010. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  13011. (cond
  13012. ((eq dw 'day)
  13013. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  13014. n2 (+ n1 dn)))
  13015. ((eq dw 'year)
  13016. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  13017. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  13018. (setq date1 (list m d y1)
  13019. n1 (calendar-absolute-from-gregorian date1)
  13020. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  13021. n2 (calendar-absolute-from-gregorian date2)))
  13022. ((eq dw 'month)
  13023. ;; approx number of month between the two dates
  13024. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  13025. ;; How often does dn fit in there?
  13026. (setq d (nth 1 start) m (car start) y (nth 2 start)
  13027. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  13028. m (+ m nm)
  13029. ny (floor (/ m 12))
  13030. y (+ y ny)
  13031. m (- m (* ny 12)))
  13032. (while (> m 12) (setq m (- m 12) y (1+ y)))
  13033. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  13034. (setq m2 (+ m dn) y2 y)
  13035. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13036. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  13037. (while (<= n2 cday)
  13038. (setq n1 n2 m m2 y y2)
  13039. (setq m2 (+ m dn) y2 y)
  13040. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13041. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  13042. ;; Make sure n1 is the earlier date
  13043. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  13044. (if show-all
  13045. (cond
  13046. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13047. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13048. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  13049. (cond
  13050. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13051. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13052. (t (if (= cday n1) n1 n2)))))))
  13053. (defun org-date-to-gregorian (date)
  13054. "Turn any specification of DATE into a gregorian date for the calendar."
  13055. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  13056. ((and (listp date) (= (length date) 3)) date)
  13057. ((stringp date)
  13058. (setq date (org-parse-time-string date))
  13059. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  13060. ((listp date)
  13061. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  13062. (defun org-parse-time-string (s &optional nodefault)
  13063. "Parse the standard Org-mode time string.
  13064. This should be a lot faster than the normal `parse-time-string'.
  13065. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  13066. hour and minute fields will be nil if not given."
  13067. (if (string-match org-ts-regexp0 s)
  13068. (list 0
  13069. (if (or (match-beginning 8) (not nodefault))
  13070. (string-to-number (or (match-string 8 s) "0")))
  13071. (if (or (match-beginning 7) (not nodefault))
  13072. (string-to-number (or (match-string 7 s) "0")))
  13073. (string-to-number (match-string 4 s))
  13074. (string-to-number (match-string 3 s))
  13075. (string-to-number (match-string 2 s))
  13076. nil nil nil)
  13077. (error "Not a standard Org-mode time string: %s" s)))
  13078. (defun org-timestamp-up (&optional arg)
  13079. "Increase the date item at the cursor by one.
  13080. If the cursor is on the year, change the year. If it is on the month or
  13081. the day, change that.
  13082. With prefix ARG, change by that many units."
  13083. (interactive "p")
  13084. (org-timestamp-change (prefix-numeric-value arg)))
  13085. (defun org-timestamp-down (&optional arg)
  13086. "Decrease the date item at the cursor by one.
  13087. If the cursor is on the year, change the year. If it is on the month or
  13088. the day, change that.
  13089. With prefix ARG, change by that many units."
  13090. (interactive "p")
  13091. (org-timestamp-change (- (prefix-numeric-value arg))))
  13092. (defun org-timestamp-up-day (&optional arg)
  13093. "Increase the date in the time stamp by one day.
  13094. With prefix ARG, change that many days."
  13095. (interactive "p")
  13096. (if (and (not (org-at-timestamp-p t))
  13097. (org-on-heading-p))
  13098. (org-todo 'up)
  13099. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  13100. (defun org-timestamp-down-day (&optional arg)
  13101. "Decrease the date in the time stamp by one day.
  13102. With prefix ARG, change that many days."
  13103. (interactive "p")
  13104. (if (and (not (org-at-timestamp-p t))
  13105. (org-on-heading-p))
  13106. (org-todo 'down)
  13107. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  13108. (defun org-at-timestamp-p (&optional inactive-ok)
  13109. "Determine if the cursor is in or at a timestamp."
  13110. (interactive)
  13111. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  13112. (pos (point))
  13113. (ans (or (looking-at tsr)
  13114. (save-excursion
  13115. (skip-chars-backward "^[<\n\r\t")
  13116. (if (> (point) (point-min)) (backward-char 1))
  13117. (and (looking-at tsr)
  13118. (> (- (match-end 0) pos) -1))))))
  13119. (and ans
  13120. (boundp 'org-ts-what)
  13121. (setq org-ts-what
  13122. (cond
  13123. ((= pos (match-beginning 0)) 'bracket)
  13124. ((= pos (1- (match-end 0))) 'bracket)
  13125. ((org-pos-in-match-range pos 2) 'year)
  13126. ((org-pos-in-match-range pos 3) 'month)
  13127. ((org-pos-in-match-range pos 7) 'hour)
  13128. ((org-pos-in-match-range pos 8) 'minute)
  13129. ((or (org-pos-in-match-range pos 4)
  13130. (org-pos-in-match-range pos 5)) 'day)
  13131. ((and (> pos (or (match-end 8) (match-end 5)))
  13132. (< pos (match-end 0)))
  13133. (- pos (or (match-end 8) (match-end 5))))
  13134. (t 'day))))
  13135. ans))
  13136. (defun org-toggle-timestamp-type ()
  13137. "Toggle the type (<active> or [inactive]) of a time stamp."
  13138. (interactive)
  13139. (when (org-at-timestamp-p t)
  13140. (let ((beg (match-beginning 0)) (end (match-end 0))
  13141. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  13142. (save-excursion
  13143. (goto-char beg)
  13144. (while (re-search-forward "[][<>]" end t)
  13145. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  13146. t t)))
  13147. (message "Timestamp is now %sactive"
  13148. (if (equal (char-after beg) ?<) "" "in")))))
  13149. (defun org-timestamp-change (n &optional what)
  13150. "Change the date in the time stamp at point.
  13151. The date will be changed by N times WHAT. WHAT can be `day', `month',
  13152. `year', `minute', `second'. If WHAT is not given, the cursor position
  13153. in the timestamp determines what will be changed."
  13154. (let ((pos (point))
  13155. with-hm inactive
  13156. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  13157. org-ts-what
  13158. extra rem
  13159. ts time time0)
  13160. (if (not (org-at-timestamp-p t))
  13161. (error "Not at a timestamp"))
  13162. (if (and (not what) (eq org-ts-what 'bracket))
  13163. (org-toggle-timestamp-type)
  13164. (if (and (not what) (not (eq org-ts-what 'day))
  13165. org-display-custom-times
  13166. (get-text-property (point) 'display)
  13167. (not (get-text-property (1- (point)) 'display)))
  13168. (setq org-ts-what 'day))
  13169. (setq org-ts-what (or what org-ts-what)
  13170. inactive (= (char-after (match-beginning 0)) ?\[)
  13171. ts (match-string 0))
  13172. (replace-match "")
  13173. (if (string-match
  13174. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  13175. ts)
  13176. (setq extra (match-string 1 ts)))
  13177. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  13178. (setq with-hm t))
  13179. (setq time0 (org-parse-time-string ts))
  13180. (when (and (eq org-ts-what 'minute)
  13181. (eq current-prefix-arg nil))
  13182. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  13183. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  13184. (setcar (cdr time0) (+ (nth 1 time0)
  13185. (if (> n 0) (- rem) (- dm rem))))))
  13186. (setq time
  13187. (encode-time (or (car time0) 0)
  13188. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  13189. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  13190. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  13191. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  13192. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  13193. (nthcdr 6 time0)))
  13194. (when (and (member org-ts-what '(hour minute))
  13195. extra
  13196. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  13197. (setq extra (org-modify-ts-extra
  13198. extra
  13199. (if (eq org-ts-what 'hour) 2 5)
  13200. n dm)))
  13201. (when (integerp org-ts-what)
  13202. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  13203. (if (eq what 'calendar)
  13204. (let ((cal-date (org-get-date-from-calendar)))
  13205. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  13206. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  13207. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  13208. (setcar time0 (or (car time0) 0))
  13209. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  13210. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  13211. (setq time (apply 'encode-time time0))))
  13212. (setq org-last-changed-timestamp
  13213. (org-insert-time-stamp time with-hm inactive nil nil extra))
  13214. (org-clock-update-time-maybe)
  13215. (goto-char pos)
  13216. ;; Try to recenter the calendar window, if any
  13217. (if (and org-calendar-follow-timestamp-change
  13218. (get-buffer-window "*Calendar*" t)
  13219. (memq org-ts-what '(day month year)))
  13220. (org-recenter-calendar (time-to-days time))))))
  13221. (defun org-modify-ts-extra (s pos n dm)
  13222. "Change the different parts of the lead-time and repeat fields in timestamp."
  13223. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  13224. ng h m new rem)
  13225. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  13226. (cond
  13227. ((or (org-pos-in-match-range pos 2)
  13228. (org-pos-in-match-range pos 3))
  13229. (setq m (string-to-number (match-string 3 s))
  13230. h (string-to-number (match-string 2 s)))
  13231. (if (org-pos-in-match-range pos 2)
  13232. (setq h (+ h n))
  13233. (setq n (* dm (org-no-warnings (signum n))))
  13234. (when (not (= 0 (setq rem (% m dm))))
  13235. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  13236. (setq m (+ m n)))
  13237. (if (< m 0) (setq m (+ m 60) h (1- h)))
  13238. (if (> m 59) (setq m (- m 60) h (1+ h)))
  13239. (setq h (min 24 (max 0 h)))
  13240. (setq ng 1 new (format "-%02d:%02d" h m)))
  13241. ((org-pos-in-match-range pos 6)
  13242. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  13243. ((org-pos-in-match-range pos 5)
  13244. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  13245. ((org-pos-in-match-range pos 9)
  13246. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  13247. ((org-pos-in-match-range pos 8)
  13248. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  13249. (when ng
  13250. (setq s (concat
  13251. (substring s 0 (match-beginning ng))
  13252. new
  13253. (substring s (match-end ng))))))
  13254. s))
  13255. (defun org-recenter-calendar (date)
  13256. "If the calendar is visible, recenter it to DATE."
  13257. (let* ((win (selected-window))
  13258. (cwin (get-buffer-window "*Calendar*" t))
  13259. (calendar-move-hook nil))
  13260. (when cwin
  13261. (select-window cwin)
  13262. (calendar-goto-date (if (listp date) date
  13263. (calendar-gregorian-from-absolute date)))
  13264. (select-window win))))
  13265. (defun org-goto-calendar (&optional arg)
  13266. "Go to the Emacs calendar at the current date.
  13267. If there is a time stamp in the current line, go to that date.
  13268. A prefix ARG can be used to force the current date."
  13269. (interactive "P")
  13270. (let ((tsr org-ts-regexp) diff
  13271. (calendar-move-hook nil)
  13272. (calendar-view-holidays-initially-flag nil)
  13273. (view-calendar-holidays-initially nil)
  13274. (calendar-view-diary-initially-flag nil)
  13275. (view-diary-entries-initially nil))
  13276. (if (or (org-at-timestamp-p)
  13277. (save-excursion
  13278. (beginning-of-line 1)
  13279. (looking-at (concat ".*" tsr))))
  13280. (let ((d1 (time-to-days (current-time)))
  13281. (d2 (time-to-days
  13282. (org-time-string-to-time (match-string 1)))))
  13283. (setq diff (- d2 d1))))
  13284. (calendar)
  13285. (calendar-goto-today)
  13286. (if (and diff (not arg)) (calendar-forward-day diff))))
  13287. (defun org-get-date-from-calendar ()
  13288. "Return a list (month day year) of date at point in calendar."
  13289. (with-current-buffer "*Calendar*"
  13290. (save-match-data
  13291. (calendar-cursor-to-date))))
  13292. (defun org-date-from-calendar ()
  13293. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  13294. If there is already a time stamp at the cursor position, update it."
  13295. (interactive)
  13296. (if (org-at-timestamp-p t)
  13297. (org-timestamp-change 0 'calendar)
  13298. (let ((cal-date (org-get-date-from-calendar)))
  13299. (org-insert-time-stamp
  13300. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  13301. (defun org-minutes-to-hh:mm-string (m)
  13302. "Compute H:MM from a number of minutes."
  13303. (let ((h (/ m 60)))
  13304. (setq m (- m (* 60 h)))
  13305. (format org-time-clocksum-format h m)))
  13306. (defun org-hh:mm-string-to-minutes (s)
  13307. "Convert a string H:MM to a number of minutes.
  13308. If the string is just a number, interpret it as minutes.
  13309. In fact, the first hh:mm or number in the string will be taken,
  13310. there can be extra stuff in the string.
  13311. If no number is found, the return value is 0."
  13312. (cond
  13313. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  13314. (+ (* (string-to-number (match-string 1 s)) 60)
  13315. (string-to-number (match-string 2 s))))
  13316. ((string-match "\\([0-9]+\\)" s)
  13317. (string-to-number (match-string 1 s)))
  13318. (t 0)))
  13319. ;;;; Files
  13320. (defun org-save-all-org-buffers ()
  13321. "Save all Org-mode buffers without user confirmation."
  13322. (interactive)
  13323. (message "Saving all Org-mode buffers...")
  13324. (save-some-buffers t 'org-mode-p)
  13325. (when (featurep 'org-id) (org-id-locations-save))
  13326. (message "Saving all Org-mode buffers... done"))
  13327. (defun org-revert-all-org-buffers ()
  13328. "Revert all Org-mode buffers.
  13329. Prompt for confirmation when there are unsaved changes.
  13330. Be sure you know what you are doing before letting this function
  13331. overwrite your changes.
  13332. This function is useful in a setup where one tracks org files
  13333. with a version control system, to revert on one machine after pulling
  13334. changes from another. I believe the procedure must be like this:
  13335. 1. M-x org-save-all-org-buffers
  13336. 2. Pull changes from the other machine, resolve conflicts
  13337. 3. M-x org-revert-all-org-buffers"
  13338. (interactive)
  13339. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  13340. (error "Abort"))
  13341. (save-excursion
  13342. (save-window-excursion
  13343. (mapc
  13344. (lambda (b)
  13345. (when (and (with-current-buffer b (org-mode-p))
  13346. (with-current-buffer b buffer-file-name))
  13347. (switch-to-buffer b)
  13348. (revert-buffer t 'no-confirm)))
  13349. (buffer-list))
  13350. (when (and (featurep 'org-id) org-id-track-globally)
  13351. (org-id-locations-load)))))
  13352. ;;;; Agenda files
  13353. ;;;###autoload
  13354. (defun org-iswitchb (&optional arg)
  13355. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  13356. With a prefix argument, restrict available to files.
  13357. With two prefix arguments, restrict available buffers to agenda files."
  13358. (interactive "P")
  13359. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  13360. ((equal arg '(16)) (org-buffer-list 'agenda))
  13361. (t (org-buffer-list)))))
  13362. (switch-to-buffer
  13363. (org-icompleting-read "Org buffer: "
  13364. (mapcar 'list (mapcar 'buffer-name blist))
  13365. nil t))))
  13366. ;;;###autoload
  13367. (defalias 'org-ido-switchb 'org-iswitchb)
  13368. (defun org-buffer-list (&optional predicate exclude-tmp)
  13369. "Return a list of Org buffers.
  13370. PREDICATE can be `export', `files' or `agenda'.
  13371. export restrict the list to Export buffers.
  13372. files restrict the list to buffers visiting Org files.
  13373. agenda restrict the list to buffers visiting agenda files.
  13374. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  13375. (let* ((bfn nil)
  13376. (agenda-files (and (eq predicate 'agenda)
  13377. (mapcar 'file-truename (org-agenda-files t))))
  13378. (filter
  13379. (cond
  13380. ((eq predicate 'files)
  13381. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  13382. ((eq predicate 'export)
  13383. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  13384. ((eq predicate 'agenda)
  13385. (lambda (b)
  13386. (with-current-buffer b
  13387. (and (eq major-mode 'org-mode)
  13388. (setq bfn (buffer-file-name b))
  13389. (member (file-truename bfn) agenda-files)))))
  13390. (t (lambda (b) (with-current-buffer b
  13391. (or (eq major-mode 'org-mode)
  13392. (string-match "\*Org .*Export"
  13393. (buffer-name b)))))))))
  13394. (delq nil
  13395. (mapcar
  13396. (lambda(b)
  13397. (if (and (funcall filter b)
  13398. (or (not exclude-tmp)
  13399. (not (string-match "tmp" (buffer-name b)))))
  13400. b
  13401. nil))
  13402. (buffer-list)))))
  13403. (defun org-agenda-files (&optional unrestricted archives)
  13404. "Get the list of agenda files.
  13405. Optional UNRESTRICTED means return the full list even if a restriction
  13406. is currently in place.
  13407. When ARCHIVES is t, include all archive files hat are really being
  13408. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  13409. `org-agenda-archives-mode' is t."
  13410. (let ((files
  13411. (cond
  13412. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  13413. ((stringp org-agenda-files) (org-read-agenda-file-list))
  13414. ((listp org-agenda-files) org-agenda-files)
  13415. (t (error "Invalid value of `org-agenda-files'")))))
  13416. (setq files (apply 'append
  13417. (mapcar (lambda (f)
  13418. (if (file-directory-p f)
  13419. (directory-files
  13420. f t org-agenda-file-regexp)
  13421. (list f)))
  13422. files)))
  13423. (when org-agenda-skip-unavailable-files
  13424. (setq files (delq nil
  13425. (mapcar (function
  13426. (lambda (file)
  13427. (and (file-readable-p file) file)))
  13428. files))))
  13429. (when (or (eq archives t)
  13430. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  13431. (setq files (org-add-archive-files files)))
  13432. files))
  13433. (defun org-edit-agenda-file-list ()
  13434. "Edit the list of agenda files.
  13435. Depending on setup, this either uses customize to edit the variable
  13436. `org-agenda-files', or it visits the file that is holding the list. In the
  13437. latter case, the buffer is set up in a way that saving it automatically kills
  13438. the buffer and restores the previous window configuration."
  13439. (interactive)
  13440. (if (stringp org-agenda-files)
  13441. (let ((cw (current-window-configuration)))
  13442. (find-file org-agenda-files)
  13443. (org-set-local 'org-window-configuration cw)
  13444. (org-add-hook 'after-save-hook
  13445. (lambda ()
  13446. (set-window-configuration
  13447. (prog1 org-window-configuration
  13448. (kill-buffer (current-buffer))))
  13449. (org-install-agenda-files-menu)
  13450. (message "New agenda file list installed"))
  13451. nil 'local)
  13452. (message "%s" (substitute-command-keys
  13453. "Edit list and finish with \\[save-buffer]")))
  13454. (customize-variable 'org-agenda-files)))
  13455. (defun org-store-new-agenda-file-list (list)
  13456. "Set new value for the agenda file list and save it correctly."
  13457. (if (stringp org-agenda-files)
  13458. (let ((f org-agenda-files) b)
  13459. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  13460. (with-temp-file f
  13461. (insert (mapconcat 'identity list "\n") "\n")))
  13462. (let ((org-mode-hook nil) (org-inhibit-startup t)
  13463. (org-insert-mode-line-in-empty-file nil))
  13464. (setq org-agenda-files list)
  13465. (customize-save-variable 'org-agenda-files org-agenda-files))))
  13466. (defun org-read-agenda-file-list ()
  13467. "Read the list of agenda files from a file."
  13468. (when (file-directory-p org-agenda-files)
  13469. (error "`org-agenda-files' cannot be a single directory"))
  13470. (when (stringp org-agenda-files)
  13471. (with-temp-buffer
  13472. (insert-file-contents org-agenda-files)
  13473. (mapcar
  13474. (lambda (f)
  13475. (expand-file-name (substitute-in-file-name f)
  13476. (file-name-directory org-agenda-files)))
  13477. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  13478. ;;;###autoload
  13479. (defun org-cycle-agenda-files ()
  13480. "Cycle through the files in `org-agenda-files'.
  13481. If the current buffer visits an agenda file, find the next one in the list.
  13482. If the current buffer does not, find the first agenda file."
  13483. (interactive)
  13484. (let* ((fs (org-agenda-files t))
  13485. (files (append fs (list (car fs))))
  13486. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  13487. file)
  13488. (unless files (error "No agenda files"))
  13489. (catch 'exit
  13490. (while (setq file (pop files))
  13491. (if (equal (file-truename file) tcf)
  13492. (when (car files)
  13493. (find-file (car files))
  13494. (throw 'exit t))))
  13495. (find-file (car fs)))
  13496. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  13497. (defun org-agenda-file-to-front (&optional to-end)
  13498. "Move/add the current file to the top of the agenda file list.
  13499. If the file is not present in the list, it is added to the front. If it is
  13500. present, it is moved there. With optional argument TO-END, add/move to the
  13501. end of the list."
  13502. (interactive "P")
  13503. (let ((org-agenda-skip-unavailable-files nil)
  13504. (file-alist (mapcar (lambda (x)
  13505. (cons (file-truename x) x))
  13506. (org-agenda-files t)))
  13507. (ctf (file-truename buffer-file-name))
  13508. x had)
  13509. (setq x (assoc ctf file-alist) had x)
  13510. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  13511. (if to-end
  13512. (setq file-alist (append (delq x file-alist) (list x)))
  13513. (setq file-alist (cons x (delq x file-alist))))
  13514. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  13515. (org-install-agenda-files-menu)
  13516. (message "File %s to %s of agenda file list"
  13517. (if had "moved" "added") (if to-end "end" "front"))))
  13518. (defun org-remove-file (&optional file)
  13519. "Remove current file from the list of files in variable `org-agenda-files'.
  13520. These are the files which are being checked for agenda entries.
  13521. Optional argument FILE means use this file instead of the current."
  13522. (interactive)
  13523. (let* ((org-agenda-skip-unavailable-files nil)
  13524. (file (or file buffer-file-name))
  13525. (true-file (file-truename file))
  13526. (afile (abbreviate-file-name file))
  13527. (files (delq nil (mapcar
  13528. (lambda (x)
  13529. (if (equal true-file
  13530. (file-truename x))
  13531. nil x))
  13532. (org-agenda-files t)))))
  13533. (if (not (= (length files) (length (org-agenda-files t))))
  13534. (progn
  13535. (org-store-new-agenda-file-list files)
  13536. (org-install-agenda-files-menu)
  13537. (message "Removed file: %s" afile))
  13538. (message "File was not in list: %s (not removed)" afile))))
  13539. (defun org-file-menu-entry (file)
  13540. (vector file (list 'find-file file) t))
  13541. (defun org-check-agenda-file (file)
  13542. "Make sure FILE exists. If not, ask user what to do."
  13543. (when (not (file-exists-p file))
  13544. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  13545. (abbreviate-file-name file))
  13546. (let ((r (downcase (read-char-exclusive))))
  13547. (cond
  13548. ((equal r ?r)
  13549. (org-remove-file file)
  13550. (throw 'nextfile t))
  13551. (t (error "Abort"))))))
  13552. (defun org-get-agenda-file-buffer (file)
  13553. "Get a buffer visiting FILE. If the buffer needs to be created, add
  13554. it to the list of buffers which might be released later."
  13555. (let ((buf (org-find-base-buffer-visiting file)))
  13556. (if buf
  13557. buf ; just return it
  13558. ;; Make a new buffer and remember it
  13559. (setq buf (find-file-noselect file))
  13560. (if buf (push buf org-agenda-new-buffers))
  13561. buf)))
  13562. (defun org-release-buffers (blist)
  13563. "Release all buffers in list, asking the user for confirmation when needed.
  13564. When a buffer is unmodified, it is just killed. When modified, it is saved
  13565. \(if the user agrees) and then killed."
  13566. (let (buf file)
  13567. (while (setq buf (pop blist))
  13568. (setq file (buffer-file-name buf))
  13569. (when (and (buffer-modified-p buf)
  13570. file
  13571. (y-or-n-p (format "Save file %s? " file)))
  13572. (with-current-buffer buf (save-buffer)))
  13573. (kill-buffer buf))))
  13574. (defun org-prepare-agenda-buffers (files)
  13575. "Create buffers for all agenda files, protect archived trees and comments."
  13576. (interactive)
  13577. (let ((pa '(:org-archived t))
  13578. (pc '(:org-comment t))
  13579. (pall '(:org-archived t :org-comment t))
  13580. (inhibit-read-only t)
  13581. (rea (concat ":" org-archive-tag ":"))
  13582. bmp file re)
  13583. (save-excursion
  13584. (save-restriction
  13585. (while (setq file (pop files))
  13586. (catch 'nextfile
  13587. (if (bufferp file)
  13588. (set-buffer file)
  13589. (org-check-agenda-file file)
  13590. (set-buffer (org-get-agenda-file-buffer file)))
  13591. (widen)
  13592. (setq bmp (buffer-modified-p))
  13593. (org-refresh-category-properties)
  13594. (setq org-todo-keywords-for-agenda
  13595. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  13596. (setq org-done-keywords-for-agenda
  13597. (append org-done-keywords-for-agenda org-done-keywords))
  13598. (setq org-todo-keyword-alist-for-agenda
  13599. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  13600. (setq org-drawers-for-agenda
  13601. (append org-drawers-for-agenda org-drawers))
  13602. (setq org-tag-alist-for-agenda
  13603. (append org-tag-alist-for-agenda org-tag-alist))
  13604. (save-excursion
  13605. (remove-text-properties (point-min) (point-max) pall)
  13606. (when org-agenda-skip-archived-trees
  13607. (goto-char (point-min))
  13608. (while (re-search-forward rea nil t)
  13609. (if (org-on-heading-p t)
  13610. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  13611. (goto-char (point-min))
  13612. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  13613. (while (re-search-forward re nil t)
  13614. (add-text-properties
  13615. (match-beginning 0) (org-end-of-subtree t) pc)))
  13616. (set-buffer-modified-p bmp)))))
  13617. (setq org-todo-keyword-alist-for-agenda
  13618. (org-uniquify org-todo-keyword-alist-for-agenda)
  13619. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  13620. ;;;; Embedded LaTeX
  13621. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  13622. "Keymap for the minor `org-cdlatex-mode'.")
  13623. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  13624. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  13625. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  13626. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  13627. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  13628. (defvar org-cdlatex-texmathp-advice-is-done nil
  13629. "Flag remembering if we have applied the advice to texmathp already.")
  13630. (define-minor-mode org-cdlatex-mode
  13631. "Toggle the minor `org-cdlatex-mode'.
  13632. This mode supports entering LaTeX environment and math in LaTeX fragments
  13633. in Org-mode.
  13634. \\{org-cdlatex-mode-map}"
  13635. nil " OCDL" nil
  13636. (when org-cdlatex-mode (require 'cdlatex))
  13637. (unless org-cdlatex-texmathp-advice-is-done
  13638. (setq org-cdlatex-texmathp-advice-is-done t)
  13639. (defadvice texmathp (around org-math-always-on activate)
  13640. "Always return t in org-mode buffers.
  13641. This is because we want to insert math symbols without dollars even outside
  13642. the LaTeX math segments. If Orgmode thinks that point is actually inside
  13643. an embedded LaTeX fragment, let texmathp do its job.
  13644. \\[org-cdlatex-mode-map]"
  13645. (interactive)
  13646. (let (p)
  13647. (cond
  13648. ((not (org-mode-p)) ad-do-it)
  13649. ((eq this-command 'cdlatex-math-symbol)
  13650. (setq ad-return-value t
  13651. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  13652. (t
  13653. (let ((p (org-inside-LaTeX-fragment-p)))
  13654. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  13655. (setq ad-return-value t
  13656. texmathp-why '("Org-mode embedded math" . 0))
  13657. (if p ad-do-it)))))))))
  13658. (defun turn-on-org-cdlatex ()
  13659. "Unconditionally turn on `org-cdlatex-mode'."
  13660. (org-cdlatex-mode 1))
  13661. (defun org-inside-LaTeX-fragment-p ()
  13662. "Test if point is inside a LaTeX fragment.
  13663. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  13664. sequence appearing also before point.
  13665. Even though the matchers for math are configurable, this function assumes
  13666. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  13667. delimiters are skipped when they have been removed by customization.
  13668. The return value is nil, or a cons cell with the delimiter and
  13669. and the position of this delimiter.
  13670. This function does a reasonably good job, but can locally be fooled by
  13671. for example currency specifications. For example it will assume being in
  13672. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  13673. fragments that are properly closed, but during editing, we have to live
  13674. with the uncertainty caused by missing closing delimiters. This function
  13675. looks only before point, not after."
  13676. (catch 'exit
  13677. (let ((pos (point))
  13678. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  13679. (lim (progn
  13680. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  13681. (point)))
  13682. dd-on str (start 0) m re)
  13683. (goto-char pos)
  13684. (when dodollar
  13685. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  13686. re (nth 1 (assoc "$" org-latex-regexps)))
  13687. (while (string-match re str start)
  13688. (cond
  13689. ((= (match-end 0) (length str))
  13690. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  13691. ((= (match-end 0) (- (length str) 5))
  13692. (throw 'exit nil))
  13693. (t (setq start (match-end 0))))))
  13694. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  13695. (goto-char pos)
  13696. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  13697. (and (match-beginning 2) (throw 'exit nil))
  13698. ;; count $$
  13699. (while (re-search-backward "\\$\\$" lim t)
  13700. (setq dd-on (not dd-on)))
  13701. (goto-char pos)
  13702. (if dd-on (cons "$$" m))))))
  13703. (defun org-inside-latex-macro-p ()
  13704. "Is point inside a LaTeX macro or its arguments?"
  13705. (save-match-data
  13706. (org-in-regexp
  13707. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  13708. (defun test ()
  13709. (interactive)
  13710. (message "%s" (org-inside-latex-macro-p)))
  13711. (defun org-try-cdlatex-tab ()
  13712. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  13713. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  13714. - inside a LaTeX fragment, or
  13715. - after the first word in a line, where an abbreviation expansion could
  13716. insert a LaTeX environment."
  13717. (when org-cdlatex-mode
  13718. (cond
  13719. ((save-excursion
  13720. (skip-chars-backward "a-zA-Z0-9*")
  13721. (skip-chars-backward " \t")
  13722. (bolp))
  13723. (cdlatex-tab) t)
  13724. ((org-inside-LaTeX-fragment-p)
  13725. (cdlatex-tab) t)
  13726. (t nil))))
  13727. (defun org-cdlatex-underscore-caret (&optional arg)
  13728. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  13729. Revert to the normal definition outside of these fragments."
  13730. (interactive "P")
  13731. (if (org-inside-LaTeX-fragment-p)
  13732. (call-interactively 'cdlatex-sub-superscript)
  13733. (let (org-cdlatex-mode)
  13734. (call-interactively (key-binding (vector last-input-event))))))
  13735. (defun org-cdlatex-math-modify (&optional arg)
  13736. "Execute `cdlatex-math-modify' in LaTeX fragments.
  13737. Revert to the normal definition outside of these fragments."
  13738. (interactive "P")
  13739. (if (org-inside-LaTeX-fragment-p)
  13740. (call-interactively 'cdlatex-math-modify)
  13741. (let (org-cdlatex-mode)
  13742. (call-interactively (key-binding (vector last-input-event))))))
  13743. (defvar org-latex-fragment-image-overlays nil
  13744. "List of overlays carrying the images of latex fragments.")
  13745. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  13746. (defun org-remove-latex-fragment-image-overlays ()
  13747. "Remove all overlays with LaTeX fragment images in current buffer."
  13748. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  13749. (setq org-latex-fragment-image-overlays nil))
  13750. (defun org-preview-latex-fragment (&optional subtree)
  13751. "Preview the LaTeX fragment at point, or all locally or globally.
  13752. If the cursor is in a LaTeX fragment, create the image and overlay
  13753. it over the source code. If there is no fragment at point, display
  13754. all fragments in the current text, from one headline to the next. With
  13755. prefix SUBTREE, display all fragments in the current subtree. With a
  13756. double prefix `C-u C-u', or when the cursor is before the first headline,
  13757. display all fragments in the buffer.
  13758. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  13759. (interactive "P")
  13760. (org-remove-latex-fragment-image-overlays)
  13761. (save-excursion
  13762. (save-restriction
  13763. (let (beg end at msg)
  13764. (cond
  13765. ((or (equal subtree '(16))
  13766. (not (save-excursion
  13767. (re-search-backward (concat "^" outline-regexp) nil t))))
  13768. (setq beg (point-min) end (point-max)
  13769. msg "Creating images for buffer...%s"))
  13770. ((equal subtree '(4))
  13771. (org-back-to-heading)
  13772. (setq beg (point) end (org-end-of-subtree t)
  13773. msg "Creating images for subtree...%s"))
  13774. (t
  13775. (if (setq at (org-inside-LaTeX-fragment-p))
  13776. (goto-char (max (point-min) (- (cdr at) 2)))
  13777. (org-back-to-heading))
  13778. (setq beg (point) end (progn (outline-next-heading) (point))
  13779. msg (if at "Creating image...%s"
  13780. "Creating images for entry...%s"))))
  13781. (message msg "")
  13782. (narrow-to-region beg end)
  13783. (goto-char beg)
  13784. (org-format-latex
  13785. (concat "ltxpng/" (file-name-sans-extension
  13786. (file-name-nondirectory
  13787. buffer-file-name)))
  13788. default-directory 'overlays msg at 'forbuffer)
  13789. (message msg "done. Use `C-c C-c' to remove images.")))))
  13790. (defvar org-latex-regexps
  13791. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  13792. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  13793. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  13794. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13795. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13796. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  13797. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  13798. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  13799. "Regular expressions for matching embedded LaTeX.")
  13800. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  13801. "Replace LaTeX fragments with links to an image, and produce images.
  13802. Some of the options can be changed using the variable
  13803. `org-format-latex-options'."
  13804. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  13805. (let* ((prefixnodir (file-name-nondirectory prefix))
  13806. (absprefix (expand-file-name prefix dir))
  13807. (todir (file-name-directory absprefix))
  13808. (opt org-format-latex-options)
  13809. (matchers (plist-get opt :matchers))
  13810. (re-list org-latex-regexps)
  13811. (org-format-latex-header-extra
  13812. (plist-get (org-infile-export-plist) :latex-header-extra))
  13813. (cnt 0) txt hash link beg end re e checkdir
  13814. executables-checked
  13815. m n block linkfile movefile ov)
  13816. ;; Check the different regular expressions
  13817. (while (setq e (pop re-list))
  13818. (setq m (car e) re (nth 1 e) n (nth 2 e)
  13819. block (if (nth 3 e) "\n\n" ""))
  13820. (when (member m matchers)
  13821. (goto-char (point-min))
  13822. (while (re-search-forward re nil t)
  13823. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  13824. (not (get-text-property (match-beginning n)
  13825. 'org-protected))
  13826. (or (not overlays)
  13827. (not (eq (get-char-property (match-beginning n)
  13828. 'org-overlay-type)
  13829. 'org-latex-overlay))))
  13830. (setq txt (match-string n)
  13831. beg (match-beginning n) end (match-end n)
  13832. cnt (1+ cnt))
  13833. (let (print-length print-level) ; make sure full list is printed
  13834. (setq hash (sha1 (prin1-to-string
  13835. (list org-format-latex-header
  13836. org-format-latex-header-extra
  13837. org-export-latex-packages-alist
  13838. org-format-latex-options
  13839. forbuffer txt)))
  13840. linkfile (format "%s_%s.png" prefix hash)
  13841. movefile (format "%s_%s.png" absprefix hash)))
  13842. (setq link (concat block "[[file:" linkfile "]]" block))
  13843. (if msg (message msg cnt))
  13844. (goto-char beg)
  13845. (unless checkdir ; make sure the directory exists
  13846. (setq checkdir t)
  13847. (or (file-directory-p todir) (make-directory todir)))
  13848. (unless executables-checked
  13849. (org-check-external-command
  13850. "latex" "needed to convert LaTeX fragments to images")
  13851. (org-check-external-command
  13852. "dvipng" "needed to convert LaTeX fragments to images")
  13853. (setq executables-checked t))
  13854. (unless (file-exists-p movefile)
  13855. (org-create-formula-image
  13856. txt movefile opt forbuffer))
  13857. (if overlays
  13858. (progn
  13859. (mapc (lambda (o)
  13860. (if (eq (org-overlay-get o 'org-overlay-type)
  13861. 'org-latex-overlay)
  13862. (org-delete-overlay o)))
  13863. (org-overlays-in beg end))
  13864. (setq ov (org-make-overlay beg end))
  13865. (org-overlay-put ov 'org-overlay-type 'org-latex-overlay)
  13866. (if (featurep 'xemacs)
  13867. (progn
  13868. (org-overlay-put ov 'invisible t)
  13869. (org-overlay-put
  13870. ov 'end-glyph
  13871. (make-glyph (vector 'png :file movefile))))
  13872. (org-overlay-put
  13873. ov 'display
  13874. (list 'image :type 'png :file movefile :ascent 'center)))
  13875. (push ov org-latex-fragment-image-overlays)
  13876. (goto-char end))
  13877. (delete-region beg end)
  13878. (insert link))))))))
  13879. ;; This function borrows from Ganesh Swami's latex2png.el
  13880. (defun org-create-formula-image (string tofile options buffer)
  13881. "This calls dvipng."
  13882. (require 'org-latex)
  13883. (let* ((tmpdir (if (featurep 'xemacs)
  13884. (temp-directory)
  13885. temporary-file-directory))
  13886. (texfilebase (make-temp-name
  13887. (expand-file-name "orgtex" tmpdir)))
  13888. (texfile (concat texfilebase ".tex"))
  13889. (dvifile (concat texfilebase ".dvi"))
  13890. (pngfile (concat texfilebase ".png"))
  13891. (fnh (if (featurep 'xemacs)
  13892. (font-height (get-face-font 'default))
  13893. (face-attribute 'default :height nil)))
  13894. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  13895. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  13896. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  13897. "Black"))
  13898. (bg (or (plist-get options (if buffer :background :html-background))
  13899. "Transparent")))
  13900. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  13901. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  13902. (with-temp-file texfile
  13903. (insert org-format-latex-header
  13904. (if org-export-latex-packages-alist
  13905. (concat "\n"
  13906. (mapconcat (lambda(p)
  13907. (if (equal "" (car p))
  13908. (format "\\usepackage{%s}" (cadr p))
  13909. (format "\\usepackage[%s]{%s}"
  13910. (car p) (cadr p))))
  13911. org-export-latex-packages-alist "\n"))
  13912. "")
  13913. (if org-format-latex-header-extra
  13914. (concat "\n" org-format-latex-header-extra)
  13915. "")
  13916. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  13917. (let ((dir default-directory))
  13918. (condition-case nil
  13919. (progn
  13920. (cd tmpdir)
  13921. (call-process "latex" nil nil nil texfile))
  13922. (error nil))
  13923. (cd dir))
  13924. (if (not (file-exists-p dvifile))
  13925. (progn (message "Failed to create dvi file from %s" texfile) nil)
  13926. (condition-case nil
  13927. (call-process "dvipng" nil nil nil
  13928. "-fg" fg "-bg" bg
  13929. "-D" dpi
  13930. ;;"-x" scale "-y" scale
  13931. "-T" "tight"
  13932. "-o" pngfile
  13933. dvifile)
  13934. (error nil))
  13935. (if (not (file-exists-p pngfile))
  13936. (if org-format-latex-signal-error
  13937. (error "Failed to create png file from %s" texfile)
  13938. (message "Failed to create png file from %s" texfile)
  13939. nil)
  13940. ;; Use the requested file name and clean up
  13941. (copy-file pngfile tofile 'replace)
  13942. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  13943. (delete-file (concat texfilebase e)))
  13944. pngfile))))
  13945. (defun org-dvipng-color (attr)
  13946. "Return an rgb color specification for dvipng."
  13947. (apply 'format "rgb %s %s %s"
  13948. (mapcar 'org-normalize-color
  13949. (color-values (face-attribute 'default attr nil)))))
  13950. (defun org-normalize-color (value)
  13951. "Return string to be used as color value for an RGB component."
  13952. (format "%g" (/ value 65535.0)))
  13953. ;;;; Key bindings
  13954. ;; Make `C-c C-x' a prefix key
  13955. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  13956. ;; TAB key with modifiers
  13957. (org-defkey org-mode-map "\C-i" 'org-cycle)
  13958. (org-defkey org-mode-map [(tab)] 'org-cycle)
  13959. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  13960. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  13961. (org-defkey org-mode-map "\M-\t" 'org-complete)
  13962. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  13963. ;; The following line is necessary under Suse GNU/Linux
  13964. (unless (featurep 'xemacs)
  13965. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  13966. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  13967. (define-key org-mode-map [backtab] 'org-shifttab)
  13968. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  13969. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  13970. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  13971. ;; Cursor keys with modifiers
  13972. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  13973. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  13974. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  13975. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  13976. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  13977. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  13978. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  13979. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  13980. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  13981. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  13982. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  13983. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  13984. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  13985. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  13986. ;;; Extra keys for tty access.
  13987. ;; We only set them when really needed because otherwise the
  13988. ;; menus don't show the simple keys
  13989. (when (or org-use-extra-keys
  13990. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  13991. (not window-system))
  13992. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  13993. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  13994. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  13995. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  13996. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  13997. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  13998. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  13999. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  14000. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  14001. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  14002. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  14003. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  14004. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  14005. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  14006. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  14007. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  14008. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  14009. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  14010. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  14011. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  14012. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  14013. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  14014. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  14015. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  14016. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  14017. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  14018. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  14019. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  14020. ;; All the other keys
  14021. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  14022. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  14023. (if (boundp 'narrow-map)
  14024. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  14025. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  14026. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  14027. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  14028. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  14029. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  14030. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  14031. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  14032. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  14033. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  14034. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  14035. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  14036. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  14037. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  14038. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  14039. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  14040. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  14041. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  14042. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  14043. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  14044. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  14045. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  14046. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  14047. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  14048. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  14049. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  14050. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  14051. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  14052. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  14053. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  14054. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  14055. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  14056. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  14057. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  14058. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  14059. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  14060. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  14061. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  14062. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  14063. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  14064. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  14065. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  14066. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  14067. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14068. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  14069. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  14070. (org-defkey org-mode-map "\C-c^" 'org-sort)
  14071. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  14072. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  14073. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  14074. (org-defkey org-mode-map "\C-m" 'org-return)
  14075. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  14076. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  14077. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  14078. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  14079. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  14080. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  14081. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  14082. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  14083. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  14084. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  14085. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  14086. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  14087. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  14088. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  14089. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  14090. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  14091. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  14092. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  14093. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  14094. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  14095. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  14096. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  14097. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  14098. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  14099. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  14100. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  14101. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  14102. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  14103. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  14104. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  14105. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  14106. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  14107. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  14108. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  14109. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  14110. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  14111. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  14112. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  14113. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  14114. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  14115. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  14116. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  14117. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  14118. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  14119. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  14120. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  14121. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  14122. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  14123. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  14124. (when (featurep 'xemacs)
  14125. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  14126. (defconst org-speed-commands-default
  14127. '(
  14128. ("Outline Navigation")
  14129. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  14130. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  14131. ("f" . (org-speed-move-safe 'org-forward-same-level))
  14132. ("b" . (org-speed-move-safe 'org-backward-same-level))
  14133. ("u" . (org-speed-move-safe 'outline-up-heading))
  14134. ("j" . org-goto)
  14135. ("g" . (org-refile t))
  14136. ("Outline Visibility")
  14137. ("c" . org-cycle)
  14138. ("C" . org-shifttab)
  14139. (" " . org-display-outline-path)
  14140. ("Outline Structure Editing")
  14141. ("U" . org-shiftmetaup)
  14142. ("D" . org-shiftmetadown)
  14143. ("r" . org-metaright)
  14144. ("l" . org-metaleft)
  14145. ("R" . org-shiftmetaright)
  14146. ("L" . org-shiftmetaleft)
  14147. ("i" . (progn (forward-char 1) (call-interactively
  14148. 'org-insert-heading-respect-content)))
  14149. ("^" . org-sort)
  14150. ("w" . org-refile)
  14151. ("a" . org-archive-subtree-default-with-confirmation)
  14152. ("." . outline-mark-subtree)
  14153. ("Clock Commands")
  14154. ("I" . org-clock-in)
  14155. ("O" . org-clock-out)
  14156. ("Meta Data Editing")
  14157. ("t" . org-todo)
  14158. ("0" . (org-priority ?\ ))
  14159. ("1" . (org-priority ?A))
  14160. ("2" . (org-priority ?B))
  14161. ("3" . (org-priority ?C))
  14162. (";" . org-set-tags-command)
  14163. ("e" . org-set-effort)
  14164. ("Agenda Views etc")
  14165. ("v" . org-agenda)
  14166. ("/" . org-sparse-tree)
  14167. ("Misc")
  14168. ("o" . org-open-at-point)
  14169. ("?" . org-speed-command-help)
  14170. )
  14171. "The default speed commands.")
  14172. (defun org-print-speed-command (e)
  14173. (if (> (length (car e)) 1)
  14174. (progn
  14175. (princ "\n")
  14176. (princ (car e))
  14177. (princ "\n")
  14178. (princ (make-string (length (car e)) ?-))
  14179. (princ "\n"))
  14180. (princ (car e))
  14181. (princ " ")
  14182. (if (symbolp (cdr e))
  14183. (princ (symbol-name (cdr e)))
  14184. (prin1 (cdr e)))
  14185. (princ "\n")))
  14186. (defun org-speed-command-help ()
  14187. "Show the available speed commands."
  14188. (interactive)
  14189. (if (not org-use-speed-commands)
  14190. (error "Speed commands are not activated, customize `org-use-speed-commands'.")
  14191. (with-output-to-temp-buffer "*Help*"
  14192. (princ "User-defined Speed commands\n===========================\n")
  14193. (mapc 'org-print-speed-command org-speed-commands-user)
  14194. (princ "\n")
  14195. (princ "Built-in Speed commands\n=======================\n")
  14196. (mapc 'org-print-speed-command org-speed-commands-default))
  14197. (with-current-buffer "*Help*"
  14198. (setq truncate-lines t))))
  14199. (defun org-speed-move-safe (cmd)
  14200. "Execute CMD, but make sure that the cursor always ends up in a headline.
  14201. If not, return to the original position and throw an error."
  14202. (interactive)
  14203. (let ((pos (point)))
  14204. (call-interactively cmd)
  14205. (unless (and (bolp) (org-on-heading-p))
  14206. (goto-char pos)
  14207. (error "Boundary reached while executing %s" cmd))))
  14208. (defvar org-self-insert-command-undo-counter 0)
  14209. (defvar org-table-auto-blank-field) ; defined in org-table.el
  14210. (defvar org-speed-command nil)
  14211. (defun org-self-insert-command (N)
  14212. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  14213. If the cursor is in a table looking at whitespace, the whitespace is
  14214. overwritten, and the table is not marked as requiring realignment."
  14215. (interactive "p")
  14216. (cond
  14217. ((and org-use-speed-commands
  14218. (or (and (bolp) (looking-at outline-regexp))
  14219. (and (functionp org-use-speed-commands)
  14220. (funcall org-use-speed-commands)))
  14221. (setq
  14222. org-speed-command
  14223. (or (cdr (assoc (this-command-keys) org-speed-commands-user))
  14224. (cdr (assoc (this-command-keys) org-speed-commands-default)))))
  14225. (cond
  14226. ((commandp org-speed-command)
  14227. (setq this-command org-speed-command)
  14228. (call-interactively org-speed-command))
  14229. ((functionp org-speed-command)
  14230. (funcall org-speed-command))
  14231. ((and org-speed-command (listp org-speed-command))
  14232. (eval org-speed-command))
  14233. (t (let (org-use-speed-commands)
  14234. (call-interactively 'org-self-insert-command)))))
  14235. ((and
  14236. (org-table-p)
  14237. (progn
  14238. ;; check if we blank the field, and if that triggers align
  14239. (and (featurep 'org-table) org-table-auto-blank-field
  14240. (member last-command
  14241. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  14242. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  14243. ;; got extra space, this field does not determine column width
  14244. (let (org-table-may-need-update) (org-table-blank-field))
  14245. ;; no extra space, this field may determine column width
  14246. (org-table-blank-field)))
  14247. t)
  14248. (eq N 1)
  14249. (looking-at "[^|\n]* |"))
  14250. (let (org-table-may-need-update)
  14251. (goto-char (1- (match-end 0)))
  14252. (delete-backward-char 1)
  14253. (goto-char (match-beginning 0))
  14254. (self-insert-command N)))
  14255. (t
  14256. (setq org-table-may-need-update t)
  14257. (self-insert-command N)
  14258. (org-fix-tags-on-the-fly)
  14259. (if org-self-insert-cluster-for-undo
  14260. (if (not (eq last-command 'org-self-insert-command))
  14261. (setq org-self-insert-command-undo-counter 1)
  14262. (if (>= org-self-insert-command-undo-counter 20)
  14263. (setq org-self-insert-command-undo-counter 1)
  14264. (and (> org-self-insert-command-undo-counter 0)
  14265. buffer-undo-list
  14266. (not (cadr buffer-undo-list)) ; remove nil entry
  14267. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  14268. (setq org-self-insert-command-undo-counter
  14269. (1+ org-self-insert-command-undo-counter))))))))
  14270. (defun org-fix-tags-on-the-fly ()
  14271. (when (and (equal (char-after (point-at-bol)) ?*)
  14272. (org-on-heading-p))
  14273. (org-align-tags-here org-tags-column)))
  14274. (defun org-delete-backward-char (N)
  14275. "Like `delete-backward-char', insert whitespace at field end in tables.
  14276. When deleting backwards, in tables this function will insert whitespace in
  14277. front of the next \"|\" separator, to keep the table aligned. The table will
  14278. still be marked for re-alignment if the field did fill the entire column,
  14279. because, in this case the deletion might narrow the column."
  14280. (interactive "p")
  14281. (if (and (org-table-p)
  14282. (eq N 1)
  14283. (string-match "|" (buffer-substring (point-at-bol) (point)))
  14284. (looking-at ".*?|"))
  14285. (let ((pos (point))
  14286. (noalign (looking-at "[^|\n\r]* |"))
  14287. (c org-table-may-need-update))
  14288. (backward-delete-char N)
  14289. (skip-chars-forward "^|")
  14290. (insert " ")
  14291. (goto-char (1- pos))
  14292. ;; noalign: if there were two spaces at the end, this field
  14293. ;; does not determine the width of the column.
  14294. (if noalign (setq org-table-may-need-update c)))
  14295. (backward-delete-char N)
  14296. (org-fix-tags-on-the-fly)))
  14297. (defun org-delete-char (N)
  14298. "Like `delete-char', but insert whitespace at field end in tables.
  14299. When deleting characters, in tables this function will insert whitespace in
  14300. front of the next \"|\" separator, to keep the table aligned. The table will
  14301. still be marked for re-alignment if the field did fill the entire column,
  14302. because, in this case the deletion might narrow the column."
  14303. (interactive "p")
  14304. (if (and (org-table-p)
  14305. (not (bolp))
  14306. (not (= (char-after) ?|))
  14307. (eq N 1))
  14308. (if (looking-at ".*?|")
  14309. (let ((pos (point))
  14310. (noalign (looking-at "[^|\n\r]* |"))
  14311. (c org-table-may-need-update))
  14312. (replace-match (concat
  14313. (substring (match-string 0) 1 -1)
  14314. " |"))
  14315. (goto-char pos)
  14316. ;; noalign: if there were two spaces at the end, this field
  14317. ;; does not determine the width of the column.
  14318. (if noalign (setq org-table-may-need-update c)))
  14319. (delete-char N))
  14320. (delete-char N)
  14321. (org-fix-tags-on-the-fly)))
  14322. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  14323. (put 'org-self-insert-command 'delete-selection t)
  14324. (put 'orgtbl-self-insert-command 'delete-selection t)
  14325. (put 'org-delete-char 'delete-selection 'supersede)
  14326. (put 'org-delete-backward-char 'delete-selection 'supersede)
  14327. (put 'org-yank 'delete-selection 'yank)
  14328. ;; Make `flyspell-mode' delay after some commands
  14329. (put 'org-self-insert-command 'flyspell-delayed t)
  14330. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  14331. (put 'org-delete-char 'flyspell-delayed t)
  14332. (put 'org-delete-backward-char 'flyspell-delayed t)
  14333. ;; Make pabbrev-mode expand after org-mode commands
  14334. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  14335. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  14336. ;; How to do this: Measure non-white length of current string
  14337. ;; If equal to column width, we should realign.
  14338. (defun org-remap (map &rest commands)
  14339. "In MAP, remap the functions given in COMMANDS.
  14340. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  14341. (let (new old)
  14342. (while commands
  14343. (setq old (pop commands) new (pop commands))
  14344. (if (fboundp 'command-remapping)
  14345. (org-defkey map (vector 'remap old) new)
  14346. (substitute-key-definition old new map global-map)))))
  14347. (when (eq org-enable-table-editor 'optimized)
  14348. ;; If the user wants maximum table support, we need to hijack
  14349. ;; some standard editing functions
  14350. (org-remap org-mode-map
  14351. 'self-insert-command 'org-self-insert-command
  14352. 'delete-char 'org-delete-char
  14353. 'delete-backward-char 'org-delete-backward-char)
  14354. (org-defkey org-mode-map "|" 'org-force-self-insert))
  14355. (defvar org-ctrl-c-ctrl-c-hook nil
  14356. "Hook for functions attaching themselves to `C-c C-c'.
  14357. This can be used to add additional functionality to the C-c C-c key which
  14358. executes context-dependent commands.
  14359. Each function will be called with no arguments. The function must check
  14360. if the context is appropriate for it to act. If yes, it should do its
  14361. thing and then return a non-nil value. If the context is wrong,
  14362. just do nothing and return nil.")
  14363. (defvar org-tab-first-hook nil
  14364. "Hook for functions to attach themselves to TAB.
  14365. See `org-ctrl-c-ctrl-c-hook' for more information.
  14366. This hook runs as the first action when TAB is pressed, even before
  14367. `org-cycle' messes around with the `outline-regexp' to cater for
  14368. inline tasks and plain list item folding.
  14369. If any function in this hook returns t, not other actions like table
  14370. field motion visibility cycling will be done.")
  14371. (defvar org-tab-after-check-for-table-hook nil
  14372. "Hook for functions to attach themselves to TAB.
  14373. See `org-ctrl-c-ctrl-c-hook' for more information.
  14374. This hook runs after it has been established that the cursor is not in a
  14375. table, but before checking if the cursor is in a headline or if global cycling
  14376. should be done.
  14377. If any function in this hook returns t, not other actions like visibility
  14378. cycling will be done.")
  14379. (defvar org-tab-after-check-for-cycling-hook nil
  14380. "Hook for functions to attach themselves to TAB.
  14381. See `org-ctrl-c-ctrl-c-hook' for more information.
  14382. This hook runs after it has been established that not table field motion and
  14383. not visibility should be done because of current context. This is probably
  14384. the place where a package like yasnippets can hook in.")
  14385. (defvar org-tab-before-tab-emulation-hook nil
  14386. "Hook for functions to attach themselves to TAB.
  14387. See `org-ctrl-c-ctrl-c-hook' for more information.
  14388. This hook runs after every other options for TAB have been exhausted, but
  14389. before indentation and \t insertion takes place.")
  14390. (defvar org-metaleft-hook nil
  14391. "Hook for functions attaching themselves to `M-left'.
  14392. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14393. (defvar org-metaright-hook nil
  14394. "Hook for functions attaching themselves to `M-right'.
  14395. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14396. (defvar org-metaup-hook nil
  14397. "Hook for functions attaching themselves to `M-up'.
  14398. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14399. (defvar org-metadown-hook nil
  14400. "Hook for functions attaching themselves to `M-down'.
  14401. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14402. (defvar org-shiftmetaleft-hook nil
  14403. "Hook for functions attaching themselves to `M-S-left'.
  14404. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14405. (defvar org-shiftmetaright-hook nil
  14406. "Hook for functions attaching themselves to `M-S-right'.
  14407. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14408. (defvar org-shiftmetaup-hook nil
  14409. "Hook for functions attaching themselves to `M-S-up'.
  14410. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14411. (defvar org-shiftmetadown-hook nil
  14412. "Hook for functions attaching themselves to `M-S-down'.
  14413. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14414. (defvar org-metareturn-hook nil
  14415. "Hook for functions attaching themselves to `M-RET'.
  14416. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14417. (defun org-modifier-cursor-error ()
  14418. "Throw an error, a modified cursor command was applied in wrong context."
  14419. (error "This command is active in special context like tables, headlines or items"))
  14420. (defun org-shiftselect-error ()
  14421. "Throw an error because Shift-Cursor command was applied in wrong context."
  14422. (if (and (boundp 'shift-select-mode) shift-select-mode)
  14423. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  14424. (error "This command works only in special context like headlines or timestamps")))
  14425. (defun org-call-for-shift-select (cmd)
  14426. (let ((this-command-keys-shift-translated t))
  14427. (call-interactively cmd)))
  14428. (defun org-shifttab (&optional arg)
  14429. "Global visibility cycling or move to previous table field.
  14430. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  14431. on context.
  14432. See the individual commands for more information."
  14433. (interactive "P")
  14434. (cond
  14435. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  14436. ((integerp arg)
  14437. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  14438. (message "Content view to level: %d" arg)
  14439. (org-content (prefix-numeric-value arg2))
  14440. (setq org-cycle-global-status 'overview)))
  14441. (t (call-interactively 'org-global-cycle))))
  14442. (defun org-shiftmetaleft ()
  14443. "Promote subtree or delete table column.
  14444. Calls `org-promote-subtree', `org-outdent-item',
  14445. or `org-table-delete-column', depending on context.
  14446. See the individual commands for more information."
  14447. (interactive)
  14448. (cond
  14449. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  14450. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  14451. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  14452. ((org-at-item-p) (call-interactively 'org-outdent-item))
  14453. (t (org-modifier-cursor-error))))
  14454. (defun org-shiftmetaright ()
  14455. "Demote subtree or insert table column.
  14456. Calls `org-demote-subtree', `org-indent-item',
  14457. or `org-table-insert-column', depending on context.
  14458. See the individual commands for more information."
  14459. (interactive)
  14460. (cond
  14461. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  14462. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  14463. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  14464. ((org-at-item-p) (call-interactively 'org-indent-item))
  14465. (t (org-modifier-cursor-error))))
  14466. (defun org-shiftmetaup (&optional arg)
  14467. "Move subtree up or kill table row.
  14468. Calls `org-move-subtree-up' or `org-table-kill-row' or
  14469. `org-move-item-up' depending on context. See the individual commands
  14470. for more information."
  14471. (interactive "P")
  14472. (cond
  14473. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  14474. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  14475. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14476. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14477. (t (org-modifier-cursor-error))))
  14478. (defun org-shiftmetadown (&optional arg)
  14479. "Move subtree down or insert table row.
  14480. Calls `org-move-subtree-down' or `org-table-insert-row' or
  14481. `org-move-item-down', depending on context. See the individual
  14482. commands for more information."
  14483. (interactive "P")
  14484. (cond
  14485. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  14486. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  14487. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14488. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14489. (t (org-modifier-cursor-error))))
  14490. (defun org-metaleft (&optional arg)
  14491. "Promote heading or move table column to left.
  14492. Calls `org-do-promote' or `org-table-move-column', depending on context.
  14493. With no specific context, calls the Emacs default `backward-word'.
  14494. See the individual commands for more information."
  14495. (interactive "P")
  14496. (cond
  14497. ((run-hook-with-args-until-success 'org-metaleft-hook))
  14498. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  14499. ((or (org-on-heading-p)
  14500. (and (org-region-active-p)
  14501. (save-excursion
  14502. (goto-char (region-beginning))
  14503. (org-on-heading-p))))
  14504. (call-interactively 'org-do-promote))
  14505. ((or (org-at-item-p)
  14506. (and (org-region-active-p)
  14507. (save-excursion
  14508. (goto-char (region-beginning))
  14509. (org-at-item-p))))
  14510. (call-interactively 'org-outdent-item))
  14511. (t (call-interactively 'backward-word))))
  14512. (defun org-metaright (&optional arg)
  14513. "Demote subtree or move table column to right.
  14514. Calls `org-do-demote' or `org-table-move-column', depending on context.
  14515. With no specific context, calls the Emacs default `forward-word'.
  14516. See the individual commands for more information."
  14517. (interactive "P")
  14518. (cond
  14519. ((run-hook-with-args-until-success 'org-metaright-hook))
  14520. ((org-at-table-p) (call-interactively 'org-table-move-column))
  14521. ((or (org-on-heading-p)
  14522. (and (org-region-active-p)
  14523. (save-excursion
  14524. (goto-char (region-beginning))
  14525. (org-on-heading-p))))
  14526. (call-interactively 'org-do-demote))
  14527. ((or (org-at-item-p)
  14528. (and (org-region-active-p)
  14529. (save-excursion
  14530. (goto-char (region-beginning))
  14531. (org-at-item-p))))
  14532. (call-interactively 'org-indent-item))
  14533. (t (call-interactively 'forward-word))))
  14534. (defun org-metaup (&optional arg)
  14535. "Move subtree up or move table row up.
  14536. Calls `org-move-subtree-up' or `org-table-move-row' or
  14537. `org-move-item-up', depending on context. See the individual commands
  14538. for more information."
  14539. (interactive "P")
  14540. (cond
  14541. ((run-hook-with-args-until-success 'org-metaup-hook))
  14542. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  14543. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14544. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14545. (t (transpose-lines 1) (beginning-of-line -1))))
  14546. (defun org-metadown (&optional arg)
  14547. "Move subtree down or move table row down.
  14548. Calls `org-move-subtree-down' or `org-table-move-row' or
  14549. `org-move-item-down', depending on context. See the individual
  14550. commands for more information."
  14551. (interactive "P")
  14552. (cond
  14553. ((run-hook-with-args-until-success 'org-metadown-hook))
  14554. ((org-at-table-p) (call-interactively 'org-table-move-row))
  14555. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14556. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14557. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  14558. (defun org-shiftup (&optional arg)
  14559. "Increase item in timestamp or increase priority of current headline.
  14560. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  14561. depending on context. See the individual commands for more information."
  14562. (interactive "P")
  14563. (cond
  14564. ((and org-support-shift-select (org-region-active-p))
  14565. (org-call-for-shift-select 'previous-line))
  14566. ((org-at-timestamp-p t)
  14567. (call-interactively (if org-edit-timestamp-down-means-later
  14568. 'org-timestamp-down 'org-timestamp-up)))
  14569. ((and (not (eq org-support-shift-select 'always))
  14570. org-enable-priority-commands
  14571. (org-on-heading-p))
  14572. (call-interactively 'org-priority-up))
  14573. ((and (not org-support-shift-select) (org-at-item-p))
  14574. (call-interactively 'org-previous-item))
  14575. ((org-clocktable-try-shift 'up arg))
  14576. (org-support-shift-select
  14577. (org-call-for-shift-select 'previous-line))
  14578. (t (org-shiftselect-error))))
  14579. (defun org-shiftdown (&optional arg)
  14580. "Decrease item in timestamp or decrease priority of current headline.
  14581. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  14582. depending on context. See the individual commands for more information."
  14583. (interactive "P")
  14584. (cond
  14585. ((and org-support-shift-select (org-region-active-p))
  14586. (org-call-for-shift-select 'next-line))
  14587. ((org-at-timestamp-p t)
  14588. (call-interactively (if org-edit-timestamp-down-means-later
  14589. 'org-timestamp-up 'org-timestamp-down)))
  14590. ((and (not (eq org-support-shift-select 'always))
  14591. org-enable-priority-commands
  14592. (org-on-heading-p))
  14593. (call-interactively 'org-priority-down))
  14594. ((and (not org-support-shift-select) (org-at-item-p))
  14595. (call-interactively 'org-next-item))
  14596. ((org-clocktable-try-shift 'down arg))
  14597. (org-support-shift-select
  14598. (org-call-for-shift-select 'next-line))
  14599. (t (org-shiftselect-error))))
  14600. (defun org-shiftright (&optional arg)
  14601. "Cycle the thing at point or in the current line, depending on context.
  14602. Depending on context, this does one of the following:
  14603. - switch a timestamp at point one day into the future
  14604. - on a headline, switch to the next TODO keyword.
  14605. - on an item, switch entire list to the next bullet type
  14606. - on a property line, switch to the next allowed value
  14607. - on a clocktable definition line, move time block into the future"
  14608. (interactive "P")
  14609. (cond
  14610. ((and org-support-shift-select (org-region-active-p))
  14611. (org-call-for-shift-select 'forward-char))
  14612. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  14613. ((and (not (eq org-support-shift-select 'always))
  14614. (org-on-heading-p))
  14615. (let ((org-inhibit-logging
  14616. (not org-treat-S-cursor-todo-selection-as-state-change))
  14617. (org-inhibit-blocking
  14618. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14619. (org-call-with-arg 'org-todo 'right)))
  14620. ((or (and org-support-shift-select
  14621. (not (eq org-support-shift-select 'always))
  14622. (org-at-item-bullet-p))
  14623. (and (not org-support-shift-select) (org-at-item-p)))
  14624. (org-call-with-arg 'org-cycle-list-bullet nil))
  14625. ((and (not (eq org-support-shift-select 'always))
  14626. (org-at-property-p))
  14627. (call-interactively 'org-property-next-allowed-value))
  14628. ((org-clocktable-try-shift 'right arg))
  14629. (org-support-shift-select
  14630. (org-call-for-shift-select 'forward-char))
  14631. (t (org-shiftselect-error))))
  14632. (defun org-shiftleft (&optional arg)
  14633. "Cycle the thing at point or in the current line, depending on context.
  14634. Depending on context, this does one of the following:
  14635. - switch a timestamp at point one day into the past
  14636. - on a headline, switch to the previous TODO keyword.
  14637. - on an item, switch entire list to the previous bullet type
  14638. - on a property line, switch to the previous allowed value
  14639. - on a clocktable definition line, move time block into the past"
  14640. (interactive "P")
  14641. (cond
  14642. ((and org-support-shift-select (org-region-active-p))
  14643. (org-call-for-shift-select 'backward-char))
  14644. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  14645. ((and (not (eq org-support-shift-select 'always))
  14646. (org-on-heading-p))
  14647. (let ((org-inhibit-logging
  14648. (not org-treat-S-cursor-todo-selection-as-state-change))
  14649. (org-inhibit-blocking
  14650. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14651. (org-call-with-arg 'org-todo 'left)))
  14652. ((or (and org-support-shift-select
  14653. (not (eq org-support-shift-select 'always))
  14654. (org-at-item-bullet-p))
  14655. (and (not org-support-shift-select) (org-at-item-p)))
  14656. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  14657. ((and (not (eq org-support-shift-select 'always))
  14658. (org-at-property-p))
  14659. (call-interactively 'org-property-previous-allowed-value))
  14660. ((org-clocktable-try-shift 'left arg))
  14661. (org-support-shift-select
  14662. (org-call-for-shift-select 'backward-char))
  14663. (t (org-shiftselect-error))))
  14664. (defun org-shiftcontrolright ()
  14665. "Switch to next TODO set."
  14666. (interactive)
  14667. (cond
  14668. ((and org-support-shift-select (org-region-active-p))
  14669. (org-call-for-shift-select 'forward-word))
  14670. ((and (not (eq org-support-shift-select 'always))
  14671. (org-on-heading-p))
  14672. (org-call-with-arg 'org-todo 'nextset))
  14673. (org-support-shift-select
  14674. (org-call-for-shift-select 'forward-word))
  14675. (t (org-shiftselect-error))))
  14676. (defun org-shiftcontrolleft ()
  14677. "Switch to previous TODO set."
  14678. (interactive)
  14679. (cond
  14680. ((and org-support-shift-select (org-region-active-p))
  14681. (org-call-for-shift-select 'backward-word))
  14682. ((and (not (eq org-support-shift-select 'always))
  14683. (org-on-heading-p))
  14684. (org-call-with-arg 'org-todo 'previousset))
  14685. (org-support-shift-select
  14686. (org-call-for-shift-select 'backward-word))
  14687. (t (org-shiftselect-error))))
  14688. (defun org-ctrl-c-ret ()
  14689. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  14690. (interactive)
  14691. (cond
  14692. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  14693. (t (call-interactively 'org-insert-heading))))
  14694. (defun org-copy-special ()
  14695. "Copy region in table or copy current subtree.
  14696. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  14697. See the individual commands for more information."
  14698. (interactive)
  14699. (call-interactively
  14700. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  14701. (defun org-cut-special ()
  14702. "Cut region in table or cut current subtree.
  14703. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  14704. See the individual commands for more information."
  14705. (interactive)
  14706. (call-interactively
  14707. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  14708. (defun org-paste-special (arg)
  14709. "Paste rectangular region into table, or past subtree relative to level.
  14710. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  14711. See the individual commands for more information."
  14712. (interactive "P")
  14713. (if (org-at-table-p)
  14714. (org-table-paste-rectangle)
  14715. (org-paste-subtree arg)))
  14716. (defun org-edit-special ()
  14717. "Call a special editor for the stuff at point.
  14718. When at a table, call the formula editor with `org-table-edit-formulas'.
  14719. When at the first line of an src example, call `org-edit-src-code'.
  14720. When in an #+include line, visit the include file. Otherwise call
  14721. `ffap' to visit the file at point."
  14722. (interactive)
  14723. (cond
  14724. ((org-at-table.el-p)
  14725. (org-edit-src-code))
  14726. ((org-at-table-p)
  14727. (call-interactively 'org-table-edit-formulas))
  14728. ((save-excursion
  14729. (beginning-of-line 1)
  14730. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  14731. (find-file (org-trim (match-string 1))))
  14732. ((org-edit-src-code))
  14733. ((org-edit-fixed-width-region))
  14734. (t (call-interactively 'ffap))))
  14735. (defun org-ctrl-c-ctrl-c (&optional arg)
  14736. "Set tags in headline, or update according to changed information at point.
  14737. This command does many different things, depending on context:
  14738. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  14739. this is what we do.
  14740. - If the cursor is on a statistics cookie, update it.
  14741. - If the cursor is in a headline, prompt for tags and insert them
  14742. into the current line, aligned to `org-tags-column'. When called
  14743. with prefix arg, realign all tags in the current buffer.
  14744. - If the cursor is in one of the special #+KEYWORD lines, this
  14745. triggers scanning the buffer for these lines and updating the
  14746. information.
  14747. - If the cursor is inside a table, realign the table. This command
  14748. works even if the automatic table editor has been turned off.
  14749. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  14750. the entire table.
  14751. - If the cursor is at a footnote reference or definition, jump to
  14752. the corresponding definition or references, respectively.
  14753. - If the cursor is a the beginning of a dynamic block, update it.
  14754. - If the current buffer is a remember buffer, close note and file
  14755. it. A prefix argument of 1 files to the default location
  14756. without further interaction. A prefix argument of 2 files to
  14757. the currently clocking task.
  14758. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  14759. links in this buffer.
  14760. - If the cursor is on a numbered item in a plain list, renumber the
  14761. ordered list.
  14762. - If the cursor is on a checkbox, toggle it."
  14763. (interactive "P")
  14764. (let ((org-enable-table-editor t))
  14765. (cond
  14766. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  14767. org-occur-highlights
  14768. org-latex-fragment-image-overlays)
  14769. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  14770. (org-remove-occur-highlights)
  14771. (org-remove-latex-fragment-image-overlays)
  14772. (message "Temporary highlights/overlays removed from current buffer"))
  14773. ((and (local-variable-p 'org-finish-function (current-buffer))
  14774. (fboundp org-finish-function))
  14775. (funcall org-finish-function))
  14776. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  14777. ((or (looking-at (org-re org-property-start-re))
  14778. (org-at-property-p))
  14779. (call-interactively 'org-property-action))
  14780. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  14781. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  14782. (or (org-on-heading-p) (org-at-item-p)))
  14783. (call-interactively 'org-update-statistics-cookies))
  14784. ((org-on-heading-p) (call-interactively 'org-set-tags))
  14785. ((org-at-table.el-p)
  14786. (message "Use C-c ' to edit table.el tables"))
  14787. ((org-at-table-p)
  14788. (org-table-maybe-eval-formula)
  14789. (if arg
  14790. (call-interactively 'org-table-recalculate)
  14791. (org-table-maybe-recalculate-line))
  14792. (call-interactively 'org-table-align))
  14793. ((or (org-footnote-at-reference-p)
  14794. (org-footnote-at-definition-p))
  14795. (call-interactively 'org-footnote-action))
  14796. ((org-at-item-checkbox-p)
  14797. (call-interactively 'org-toggle-checkbox))
  14798. ((org-at-item-p)
  14799. (if arg
  14800. (call-interactively 'org-toggle-checkbox)
  14801. (call-interactively 'org-maybe-renumber-ordered-list)))
  14802. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  14803. ;; Dynamic block
  14804. (beginning-of-line 1)
  14805. (save-excursion (org-update-dblock)))
  14806. ((save-excursion
  14807. (beginning-of-line 1)
  14808. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  14809. (cond
  14810. ((equal (match-string 1) "TBLFM")
  14811. ;; Recalculate the table before this line
  14812. (save-excursion
  14813. (beginning-of-line 1)
  14814. (skip-chars-backward " \r\n\t")
  14815. (if (org-at-table-p)
  14816. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  14817. (t
  14818. (let ((org-inhibit-startup-visibility-stuff t)
  14819. (org-startup-align-all-tables nil))
  14820. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  14821. (message "Local setup has been refreshed"))))
  14822. ((org-clock-update-time-maybe))
  14823. (t (error "C-c C-c can do nothing useful at this location")))))
  14824. (defun org-mode-restart ()
  14825. "Restart Org-mode, to scan again for special lines.
  14826. Also updates the keyword regular expressions."
  14827. (interactive)
  14828. (org-mode)
  14829. (message "Org-mode restarted"))
  14830. (defun org-kill-note-or-show-branches ()
  14831. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  14832. (interactive)
  14833. (if (not org-finish-function)
  14834. (call-interactively 'show-branches)
  14835. (let ((org-note-abort t))
  14836. (funcall org-finish-function))))
  14837. (defun org-return (&optional indent)
  14838. "Goto next table row or insert a newline.
  14839. Calls `org-table-next-row' or `newline', depending on context.
  14840. See the individual commands for more information."
  14841. (interactive)
  14842. (cond
  14843. ((bobp) (if indent (newline-and-indent) (newline)))
  14844. ((org-at-table-p)
  14845. (org-table-justify-field-maybe)
  14846. (call-interactively 'org-table-next-row))
  14847. ((and org-return-follows-link
  14848. (eq (get-text-property (point) 'face) 'org-link))
  14849. (call-interactively 'org-open-at-point))
  14850. ((and (org-at-heading-p)
  14851. (looking-at
  14852. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  14853. (org-show-entry)
  14854. (end-of-line 1)
  14855. (newline))
  14856. (t (if indent (newline-and-indent) (newline)))))
  14857. (defun org-return-indent ()
  14858. "Goto next table row or insert a newline and indent.
  14859. Calls `org-table-next-row' or `newline-and-indent', depending on
  14860. context. See the individual commands for more information."
  14861. (interactive)
  14862. (org-return t))
  14863. (defun org-ctrl-c-star ()
  14864. "Compute table, or change heading status of lines.
  14865. Calls `org-table-recalculate' or `org-toggle-heading',
  14866. depending on context."
  14867. (interactive)
  14868. (cond
  14869. ((org-at-table-p)
  14870. (call-interactively 'org-table-recalculate))
  14871. (t
  14872. ;; Convert all lines in region to list items
  14873. (call-interactively 'org-toggle-heading))))
  14874. (defun org-ctrl-c-minus ()
  14875. "Insert separator line in table or modify bullet status of line.
  14876. Also turns a plain line or a region of lines into list items.
  14877. Calls `org-table-insert-hline', `org-toggle-item', or
  14878. `org-cycle-list-bullet', depending on context."
  14879. (interactive)
  14880. (cond
  14881. ((org-at-table-p)
  14882. (call-interactively 'org-table-insert-hline))
  14883. ((org-region-active-p)
  14884. (call-interactively 'org-toggle-item))
  14885. ((org-in-item-p)
  14886. (call-interactively 'org-cycle-list-bullet))
  14887. (t
  14888. (call-interactively 'org-toggle-item))))
  14889. (defun org-toggle-item ()
  14890. "Convert headings or normal lines to items, items to normal lines.
  14891. If there is no active region, only the current line is considered.
  14892. If the first line in the region is a headline, convert all headlines to items.
  14893. If the first line in the region is an item, convert all items to normal lines.
  14894. If the first line is normal text, add an item bullet to each line."
  14895. (interactive)
  14896. (let (l2 l beg end)
  14897. (if (org-region-active-p)
  14898. (setq beg (region-beginning) end (region-end))
  14899. (setq beg (point-at-bol)
  14900. end (min (1+ (point-at-eol)) (point-max))))
  14901. (save-excursion
  14902. (goto-char end)
  14903. (setq l2 (org-current-line))
  14904. (goto-char beg)
  14905. (beginning-of-line 1)
  14906. (setq l (1- (org-current-line)))
  14907. (if (org-at-item-p)
  14908. ;; We already have items, de-itemize
  14909. (while (< (setq l (1+ l)) l2)
  14910. (when (org-at-item-p)
  14911. (goto-char (match-beginning 2))
  14912. (delete-region (match-beginning 2) (match-end 2))
  14913. (and (looking-at "[ \t]+") (replace-match "")))
  14914. (beginning-of-line 2))
  14915. (if (org-on-heading-p)
  14916. ;; Headings, convert to items
  14917. (while (< (setq l (1+ l)) l2)
  14918. (if (looking-at org-outline-regexp)
  14919. (replace-match "- " t t))
  14920. (beginning-of-line 2))
  14921. ;; normal lines, turn them into items
  14922. (while (< (setq l (1+ l)) l2)
  14923. (unless (org-at-item-p)
  14924. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  14925. (replace-match "\\1- \\2")))
  14926. (beginning-of-line 2)))))))
  14927. (defun org-toggle-heading (&optional nstars)
  14928. "Convert headings to normal text, or items or text to headings.
  14929. If there is no active region, only the current line is considered.
  14930. If the first line is a heading, remove the stars from all headlines
  14931. in the region.
  14932. If the first line is a plain list item, turn all plain list items
  14933. into headings.
  14934. If the first line is a normal line, turn each and every line in the
  14935. region into a heading.
  14936. When converting a line into a heading, the number of stars is chosen
  14937. such that the lines become children of the current entry. However,
  14938. when a prefix argument is given, its value determines the number of
  14939. stars to add."
  14940. (interactive "P")
  14941. (let (l2 l itemp beg end)
  14942. (if (org-region-active-p)
  14943. (setq beg (region-beginning) end (region-end))
  14944. (setq beg (point-at-bol)
  14945. end (min (1+ (point-at-eol)) (point-max))))
  14946. (save-excursion
  14947. (goto-char end)
  14948. (setq l2 (org-current-line))
  14949. (goto-char beg)
  14950. (beginning-of-line 1)
  14951. (setq l (1- (org-current-line)))
  14952. (if (org-on-heading-p)
  14953. ;; We already have headlines, de-star them
  14954. (while (< (setq l (1+ l)) l2)
  14955. (when (org-on-heading-p t)
  14956. (and (looking-at outline-regexp) (replace-match "")))
  14957. (beginning-of-line 2))
  14958. (setq itemp (org-at-item-p))
  14959. (let* ((stars
  14960. (if nstars
  14961. (make-string (prefix-numeric-value current-prefix-arg)
  14962. ?*)
  14963. (save-excursion
  14964. (if (re-search-backward org-complex-heading-regexp nil t)
  14965. (match-string 1) ""))))
  14966. (add-stars (cond (nstars "")
  14967. ((equal stars "") "*")
  14968. (org-odd-levels-only "**")
  14969. (t "*")))
  14970. (rpl (concat stars add-stars " ")))
  14971. (while (< (setq l (1+ l)) l2)
  14972. (if itemp
  14973. (and (org-at-item-p) (replace-match rpl t t))
  14974. (unless (org-on-heading-p)
  14975. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  14976. (replace-match (concat rpl (match-string 2))))))
  14977. (beginning-of-line 2)))))))
  14978. (defun org-meta-return (&optional arg)
  14979. "Insert a new heading or wrap a region in a table.
  14980. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  14981. See the individual commands for more information."
  14982. (interactive "P")
  14983. (cond
  14984. ((run-hook-with-args-until-success 'org-metareturn-hook))
  14985. ((org-at-table-p)
  14986. (call-interactively 'org-table-wrap-region))
  14987. (t (call-interactively 'org-insert-heading))))
  14988. ;;; Menu entries
  14989. ;; Define the Org-mode menus
  14990. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  14991. '("Tbl"
  14992. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  14993. ["Next Field" org-cycle (org-at-table-p)]
  14994. ["Previous Field" org-shifttab (org-at-table-p)]
  14995. ["Next Row" org-return (org-at-table-p)]
  14996. "--"
  14997. ["Blank Field" org-table-blank-field (org-at-table-p)]
  14998. ["Edit Field" org-table-edit-field (org-at-table-p)]
  14999. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  15000. "--"
  15001. ("Column"
  15002. ["Move Column Left" org-metaleft (org-at-table-p)]
  15003. ["Move Column Right" org-metaright (org-at-table-p)]
  15004. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  15005. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  15006. ("Row"
  15007. ["Move Row Up" org-metaup (org-at-table-p)]
  15008. ["Move Row Down" org-metadown (org-at-table-p)]
  15009. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  15010. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  15011. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  15012. "--"
  15013. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  15014. ("Rectangle"
  15015. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  15016. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  15017. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  15018. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  15019. "--"
  15020. ("Calculate"
  15021. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  15022. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  15023. ["Edit Formulas" org-edit-special (org-at-table-p)]
  15024. "--"
  15025. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  15026. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  15027. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  15028. "--"
  15029. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  15030. "--"
  15031. ["Sum Column/Rectangle" org-table-sum
  15032. (or (org-at-table-p) (org-region-active-p))]
  15033. ["Which Column?" org-table-current-column (org-at-table-p)])
  15034. ["Debug Formulas"
  15035. org-table-toggle-formula-debugger
  15036. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  15037. ["Show Col/Row Numbers"
  15038. org-table-toggle-coordinate-overlays
  15039. :style toggle
  15040. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  15041. "--"
  15042. ["Create" org-table-create (and (not (org-at-table-p))
  15043. org-enable-table-editor)]
  15044. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  15045. ["Import from File" org-table-import (not (org-at-table-p))]
  15046. ["Export to File" org-table-export (org-at-table-p)]
  15047. "--"
  15048. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  15049. (easy-menu-define org-org-menu org-mode-map "Org menu"
  15050. '("Org"
  15051. ("Show/Hide"
  15052. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  15053. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  15054. ["Sparse Tree..." org-sparse-tree t]
  15055. ["Reveal Context" org-reveal t]
  15056. ["Show All" show-all t]
  15057. "--"
  15058. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  15059. "--"
  15060. ["New Heading" org-insert-heading t]
  15061. ("Navigate Headings"
  15062. ["Up" outline-up-heading t]
  15063. ["Next" outline-next-visible-heading t]
  15064. ["Previous" outline-previous-visible-heading t]
  15065. ["Next Same Level" outline-forward-same-level t]
  15066. ["Previous Same Level" outline-backward-same-level t]
  15067. "--"
  15068. ["Jump" org-goto t])
  15069. ("Edit Structure"
  15070. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  15071. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  15072. "--"
  15073. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  15074. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  15075. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  15076. "--"
  15077. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  15078. "--"
  15079. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  15080. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  15081. ["Demote Heading" org-metaright (not (org-at-table-p))]
  15082. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  15083. "--"
  15084. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  15085. "--"
  15086. ["Convert to odd levels" org-convert-to-odd-levels t]
  15087. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  15088. ("Editing"
  15089. ["Emphasis..." org-emphasize t]
  15090. ["Edit Source Example" org-edit-special t]
  15091. "--"
  15092. ["Footnote new/jump" org-footnote-action t]
  15093. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  15094. ("Archive"
  15095. ["Archive (default method)" org-archive-subtree-default t]
  15096. "--"
  15097. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  15098. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  15099. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  15100. )
  15101. "--"
  15102. ("Hyperlinks"
  15103. ["Store Link (Global)" org-store-link t]
  15104. ["Find existing link to here" org-occur-link-in-agenda-files t]
  15105. ["Insert Link" org-insert-link t]
  15106. ["Follow Link" org-open-at-point t]
  15107. "--"
  15108. ["Next link" org-next-link t]
  15109. ["Previous link" org-previous-link t]
  15110. "--"
  15111. ["Descriptive Links"
  15112. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  15113. :style radio
  15114. :selected (member '(org-link) buffer-invisibility-spec)]
  15115. ["Literal Links"
  15116. (progn
  15117. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  15118. :style radio
  15119. :selected (not (member '(org-link) buffer-invisibility-spec))])
  15120. "--"
  15121. ("TODO Lists"
  15122. ["TODO/DONE/-" org-todo t]
  15123. ("Select keyword"
  15124. ["Next keyword" org-shiftright (org-on-heading-p)]
  15125. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  15126. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  15127. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  15128. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  15129. ["Show TODO Tree" org-show-todo-tree t]
  15130. ["Global TODO list" org-todo-list t]
  15131. "--"
  15132. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  15133. :selected org-enforce-todo-dependencies :style toggle :active t]
  15134. "Settings for tree at point"
  15135. ["Do Children sequentially" org-toggle-ordered-property :style radio
  15136. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  15137. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  15138. ["Do Children parallel" org-toggle-ordered-property :style radio
  15139. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  15140. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  15141. "--"
  15142. ["Set Priority" org-priority t]
  15143. ["Priority Up" org-shiftup t]
  15144. ["Priority Down" org-shiftdown t]
  15145. "--"
  15146. ["Get news from all feeds" org-feed-update-all t]
  15147. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  15148. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  15149. ("TAGS and Properties"
  15150. ["Set Tags" org-set-tags-command t]
  15151. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  15152. "--"
  15153. ["Set property" org-set-property t]
  15154. ["Column view of properties" org-columns t]
  15155. ["Insert Column View DBlock" org-insert-columns-dblock t])
  15156. ("Dates and Scheduling"
  15157. ["Timestamp" org-time-stamp t]
  15158. ["Timestamp (inactive)" org-time-stamp-inactive t]
  15159. ("Change Date"
  15160. ["1 Day Later" org-shiftright t]
  15161. ["1 Day Earlier" org-shiftleft t]
  15162. ["1 ... Later" org-shiftup t]
  15163. ["1 ... Earlier" org-shiftdown t])
  15164. ["Compute Time Range" org-evaluate-time-range t]
  15165. ["Schedule Item" org-schedule t]
  15166. ["Deadline" org-deadline t]
  15167. "--"
  15168. ["Custom time format" org-toggle-time-stamp-overlays
  15169. :style radio :selected org-display-custom-times]
  15170. "--"
  15171. ["Goto Calendar" org-goto-calendar t]
  15172. ["Date from Calendar" org-date-from-calendar t]
  15173. "--"
  15174. ["Start/Restart Timer" org-timer-start t]
  15175. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  15176. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  15177. ["Insert Timer String" org-timer t]
  15178. ["Insert Timer Item" org-timer-item t])
  15179. ("Logging work"
  15180. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  15181. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  15182. ["Clock out" org-clock-out t]
  15183. ["Clock cancel" org-clock-cancel t]
  15184. "--"
  15185. ["Mark as default task" org-clock-mark-default-task t]
  15186. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  15187. ["Goto running clock" org-clock-goto t]
  15188. "--"
  15189. ["Display times" org-clock-display t]
  15190. ["Create clock table" org-clock-report t]
  15191. "--"
  15192. ["Record DONE time"
  15193. (progn (setq org-log-done (not org-log-done))
  15194. (message "Switching to %s will %s record a timestamp"
  15195. (car org-done-keywords)
  15196. (if org-log-done "automatically" "not")))
  15197. :style toggle :selected org-log-done])
  15198. "--"
  15199. ["Agenda Command..." org-agenda t]
  15200. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  15201. ("File List for Agenda")
  15202. ("Special views current file"
  15203. ["TODO Tree" org-show-todo-tree t]
  15204. ["Check Deadlines" org-check-deadlines t]
  15205. ["Timeline" org-timeline t]
  15206. ["Tags/Property tree" org-match-sparse-tree t])
  15207. "--"
  15208. ["Export/Publish..." org-export t]
  15209. ("LaTeX"
  15210. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  15211. :selected org-cdlatex-mode]
  15212. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  15213. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  15214. ["Modify math symbol" org-cdlatex-math-modify
  15215. (org-inside-LaTeX-fragment-p)]
  15216. ["Insert citation" org-reftex-citation t]
  15217. "--"
  15218. ["Export LaTeX fragments as images"
  15219. (if (featurep 'org-exp)
  15220. (setq org-export-with-LaTeX-fragments
  15221. (not org-export-with-LaTeX-fragments))
  15222. (require 'org-exp))
  15223. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  15224. org-export-with-LaTeX-fragments)]
  15225. "--"
  15226. ["Template for BEAMER" org-beamer-settings-template t])
  15227. "--"
  15228. ("MobileOrg"
  15229. ["Push Files and Views" org-mobile-push t]
  15230. ["Get Captured and Flagged" org-mobile-pull t]
  15231. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  15232. "--"
  15233. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  15234. "--"
  15235. ("Documentation"
  15236. ["Show Version" org-version t]
  15237. ["Info Documentation" org-info t])
  15238. ("Customize"
  15239. ["Browse Org Group" org-customize t]
  15240. "--"
  15241. ["Expand This Menu" org-create-customize-menu
  15242. (fboundp 'customize-menu-create)])
  15243. ["Send bug report" org-submit-bug-report t]
  15244. "--"
  15245. ("Refresh/Reload"
  15246. ["Refresh setup current buffer" org-mode-restart t]
  15247. ["Reload Org (after update)" org-reload t]
  15248. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  15249. ))
  15250. (defun org-info (&optional node)
  15251. "Read documentation for Org-mode in the info system.
  15252. With optional NODE, go directly to that node."
  15253. (interactive)
  15254. (info (format "(org)%s" (or node ""))))
  15255. ;;;###autoload
  15256. (defun org-submit-bug-report ()
  15257. "Submit a bug report on Org-mode via mail.
  15258. Don't hesitate to report any problems or inaccurate documentation.
  15259. If you don't have setup sending mail from (X)Emacs, please copy the
  15260. output buffer into your mail program, as it gives us important
  15261. information about your Org-mode version and configuration."
  15262. (interactive)
  15263. (require 'reporter)
  15264. (org-load-modules-maybe)
  15265. (org-require-autoloaded-modules)
  15266. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  15267. (reporter-submit-bug-report
  15268. "emacs-orgmode@gnu.org"
  15269. (org-version)
  15270. (let (list)
  15271. (save-window-excursion
  15272. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  15273. (delete-other-windows)
  15274. (erase-buffer)
  15275. (insert "You are about to submit a bug report to the Org-mode mailing list.
  15276. We would like to add your full Org-mode and Outline configuration to the
  15277. bug report. This greatly simplifies the work of the maintainer and
  15278. other experts on the mailing list.
  15279. HOWEVER, some variables you have customized may contain private
  15280. information. The names of customers, colleagues, or friends, might
  15281. appear in the form of file names, tags, todo states, or search strings.
  15282. If you answer yes to the prompt, you might want to check and remove
  15283. such private information before sending the email.")
  15284. (add-text-properties (point-min) (point-max) '(face org-warning))
  15285. (when (yes-or-no-p "Include your Org-mode configuration ")
  15286. (mapatoms
  15287. (lambda (v)
  15288. (and (boundp v)
  15289. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  15290. (or (and (symbol-value v)
  15291. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  15292. (and
  15293. (get v 'custom-type) (get v 'standard-value)
  15294. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  15295. (push v list)))))
  15296. (kill-buffer (get-buffer "*Warn about privacy*"))
  15297. list))
  15298. nil nil
  15299. "Remember to cover the basics, that is, what you expected to happen and
  15300. what in fact did happen. You don't know how to make a good report? See
  15301. http://orgmode.org/manual/Feedback.html#Feedback
  15302. Your bug report will be posted to the Org-mode mailing list.
  15303. ------------------------------------------------------------------------")
  15304. (save-excursion
  15305. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  15306. (replace-match "\\1Bug: \\3 [\\2]")))))
  15307. (defun org-install-agenda-files-menu ()
  15308. (let ((bl (buffer-list)))
  15309. (save-excursion
  15310. (while bl
  15311. (set-buffer (pop bl))
  15312. (if (org-mode-p) (setq bl nil)))
  15313. (when (org-mode-p)
  15314. (easy-menu-change
  15315. '("Org") "File List for Agenda"
  15316. (append
  15317. (list
  15318. ["Edit File List" (org-edit-agenda-file-list) t]
  15319. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  15320. ["Remove Current File from List" org-remove-file t]
  15321. ["Cycle through agenda files" org-cycle-agenda-files t]
  15322. ["Occur in all agenda files" org-occur-in-agenda-files t]
  15323. "--")
  15324. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  15325. ;;;; Documentation
  15326. ;;;###autoload
  15327. (defun org-require-autoloaded-modules ()
  15328. (interactive)
  15329. (mapc 'require
  15330. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  15331. org-docbook org-exp org-html org-icalendar
  15332. org-id org-latex
  15333. org-publish org-remember org-table
  15334. org-timer org-xoxo)))
  15335. ;;;###autoload
  15336. (defun org-reload (&optional uncompiled)
  15337. "Reload all org lisp files.
  15338. With prefix arg UNCOMPILED, load the uncompiled versions."
  15339. (interactive "P")
  15340. (require 'find-func)
  15341. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  15342. (dir-org (file-name-directory (org-find-library-name "org")))
  15343. (dir-org-contrib (ignore-errors
  15344. (file-name-directory
  15345. (org-find-library-name "org-contribdir"))))
  15346. (files
  15347. (append (directory-files dir-org t file-re)
  15348. (and dir-org-contrib
  15349. (directory-files dir-org-contrib t file-re))))
  15350. (remove-re (concat (if (featurep 'xemacs)
  15351. "org-colview" "org-colview-xemacs")
  15352. "\\'")))
  15353. (setq files (mapcar 'file-name-sans-extension files))
  15354. (setq files (mapcar
  15355. (lambda (x) (if (string-match remove-re x) nil x))
  15356. files))
  15357. (setq files (delq nil files))
  15358. (mapc
  15359. (lambda (f)
  15360. (when (featurep (intern (file-name-nondirectory f)))
  15361. (if (and (not uncompiled)
  15362. (file-exists-p (concat f ".elc")))
  15363. (load (concat f ".elc") nil nil t)
  15364. (load (concat f ".el") nil nil t))))
  15365. files))
  15366. (org-version))
  15367. ;;;###autoload
  15368. (defun org-customize ()
  15369. "Call the customize function with org as argument."
  15370. (interactive)
  15371. (org-load-modules-maybe)
  15372. (org-require-autoloaded-modules)
  15373. (customize-browse 'org))
  15374. (defun org-create-customize-menu ()
  15375. "Create a full customization menu for Org-mode, insert it into the menu."
  15376. (interactive)
  15377. (org-load-modules-maybe)
  15378. (org-require-autoloaded-modules)
  15379. (if (fboundp 'customize-menu-create)
  15380. (progn
  15381. (easy-menu-change
  15382. '("Org") "Customize"
  15383. `(["Browse Org group" org-customize t]
  15384. "--"
  15385. ,(customize-menu-create 'org)
  15386. ["Set" Custom-set t]
  15387. ["Save" Custom-save t]
  15388. ["Reset to Current" Custom-reset-current t]
  15389. ["Reset to Saved" Custom-reset-saved t]
  15390. ["Reset to Standard Settings" Custom-reset-standard t]))
  15391. (message "\"Org\"-menu now contains full customization menu"))
  15392. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  15393. ;;;; Miscellaneous stuff
  15394. ;;; Generally useful functions
  15395. (defun org-get-at-bol (property)
  15396. "Get text property PROPERTY at beginning of line."
  15397. (get-text-property (point-at-bol) property))
  15398. (defun org-find-text-property-in-string (prop s)
  15399. "Return the first non-nil value of property PROP in string S."
  15400. (or (get-text-property 0 prop s)
  15401. (get-text-property (or (next-single-property-change 0 prop s) 0)
  15402. prop s)))
  15403. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  15404. "Display the given MESSAGE as a warning."
  15405. (if (fboundp 'display-warning)
  15406. (display-warning 'org message
  15407. (if (featurep 'xemacs)
  15408. 'warning
  15409. :warning))
  15410. (let ((buf (get-buffer-create "*Org warnings*")))
  15411. (with-current-buffer buf
  15412. (goto-char (point-max))
  15413. (insert "Warning (Org): " message)
  15414. (unless (bolp)
  15415. (newline)))
  15416. (display-buffer buf)
  15417. (sit-for 0))))
  15418. (defun org-in-commented-line ()
  15419. "Is point in a line starting with `#'?"
  15420. (equal (char-after (point-at-bol)) ?#))
  15421. (defun org-in-verbatim-emphasis ()
  15422. (save-match-data
  15423. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  15424. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  15425. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  15426. (if (and marker (marker-buffer marker)
  15427. (buffer-live-p (marker-buffer marker)))
  15428. (progn
  15429. (switch-to-buffer (marker-buffer marker))
  15430. (if (or (> marker (point-max)) (< marker (point-min)))
  15431. (widen))
  15432. (goto-char marker)
  15433. (org-show-context 'org-goto))
  15434. (if bookmark
  15435. (bookmark-jump bookmark)
  15436. (error "Cannot find location"))))
  15437. (defun org-quote-csv-field (s)
  15438. "Quote field for inclusion in CSV material."
  15439. (if (string-match "[\",]" s)
  15440. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  15441. s))
  15442. (defun org-plist-delete (plist property)
  15443. "Delete PROPERTY from PLIST.
  15444. This is in contrast to merely setting it to 0."
  15445. (let (p)
  15446. (while plist
  15447. (if (not (eq property (car plist)))
  15448. (setq p (plist-put p (car plist) (nth 1 plist))))
  15449. (setq plist (cddr plist)))
  15450. p))
  15451. (defun org-force-self-insert (N)
  15452. "Needed to enforce self-insert under remapping."
  15453. (interactive "p")
  15454. (self-insert-command N))
  15455. (defun org-string-width (s)
  15456. "Compute width of string, ignoring invisible characters.
  15457. This ignores character with invisibility property `org-link', and also
  15458. characters with property `org-cwidth', because these will become invisible
  15459. upon the next fontification round."
  15460. (let (b l)
  15461. (when (or (eq t buffer-invisibility-spec)
  15462. (assq 'org-link buffer-invisibility-spec))
  15463. (while (setq b (text-property-any 0 (length s)
  15464. 'invisible 'org-link s))
  15465. (setq s (concat (substring s 0 b)
  15466. (substring s (or (next-single-property-change
  15467. b 'invisible s) (length s)))))))
  15468. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  15469. (setq s (concat (substring s 0 b)
  15470. (substring s (or (next-single-property-change
  15471. b 'org-cwidth s) (length s))))))
  15472. (setq l (string-width s) b -1)
  15473. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  15474. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  15475. l))
  15476. (defun org-get-indentation (&optional line)
  15477. "Get the indentation of the current line, interpreting tabs.
  15478. When LINE is given, assume it represents a line and compute its indentation."
  15479. (if line
  15480. (if (string-match "^ *" (org-remove-tabs line))
  15481. (match-end 0))
  15482. (save-excursion
  15483. (beginning-of-line 1)
  15484. (skip-chars-forward " \t")
  15485. (current-column))))
  15486. (defun org-remove-tabs (s &optional width)
  15487. "Replace tabulators in S with spaces.
  15488. Assumes that s is a single line, starting in column 0."
  15489. (setq width (or width tab-width))
  15490. (while (string-match "\t" s)
  15491. (setq s (replace-match
  15492. (make-string
  15493. (- (* width (/ (+ (match-beginning 0) width) width))
  15494. (match-beginning 0)) ?\ )
  15495. t t s)))
  15496. s)
  15497. (defun org-fix-indentation (line ind)
  15498. "Fix indentation in LINE.
  15499. IND is a cons cell with target and minimum indentation.
  15500. If the current indentation in LINE is smaller than the minimum,
  15501. leave it alone. If it is larger than ind, set it to the target."
  15502. (let* ((l (org-remove-tabs line))
  15503. (i (org-get-indentation l))
  15504. (i1 (car ind)) (i2 (cdr ind)))
  15505. (if (>= i i2) (setq l (substring line i2)))
  15506. (if (> i1 0)
  15507. (concat (make-string i1 ?\ ) l)
  15508. l)))
  15509. (defun org-remove-indentation (code &optional n)
  15510. "Remove the maximum common indentation from the lines in CODE.
  15511. N may optionally be the number of spaces to remove."
  15512. (with-temp-buffer
  15513. (insert code)
  15514. (org-do-remove-indentation n)
  15515. (buffer-string)))
  15516. (defun org-do-remove-indentation (&optional n)
  15517. "Remove the maximum common indentation from the buffer."
  15518. (untabify (point-min) (point-max))
  15519. (let ((min 10000) re)
  15520. (if n
  15521. (setq min n)
  15522. (goto-char (point-min))
  15523. (while (re-search-forward "^ *[^ \n]" nil t)
  15524. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  15525. (unless (or (= min 0) (= min 10000))
  15526. (setq re (format "^ \\{%d\\}" min))
  15527. (goto-char (point-min))
  15528. (while (re-search-forward re nil t)
  15529. (replace-match "")
  15530. (end-of-line 1))
  15531. min)))
  15532. (defun org-fill-template (template alist)
  15533. "Find each %key of ALIST in TEMPLATE and replace it."
  15534. (let ((case-fold-search nil)
  15535. entry key value)
  15536. (setq alist (sort (copy-sequence alist)
  15537. (lambda (a b) (< (length (car a)) (length (car b))))))
  15538. (while (setq entry (pop alist))
  15539. (setq template
  15540. (replace-regexp-in-string
  15541. (concat "%" (regexp-quote (car entry)))
  15542. (cdr entry) template t t)))
  15543. template))
  15544. (defun org-base-buffer (buffer)
  15545. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  15546. (if (not buffer)
  15547. buffer
  15548. (or (buffer-base-buffer buffer)
  15549. buffer)))
  15550. (defun org-trim (s)
  15551. "Remove whitespace at beginning and end of string."
  15552. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  15553. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  15554. s)
  15555. (defun org-wrap (string &optional width lines)
  15556. "Wrap string to either a number of lines, or a width in characters.
  15557. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  15558. that costs. If there is a word longer than WIDTH, the text is actually
  15559. wrapped to the length of that word.
  15560. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  15561. many lines, whatever width that takes.
  15562. The return value is a list of lines, without newlines at the end."
  15563. (let* ((words (org-split-string string "[ \t\n]+"))
  15564. (maxword (apply 'max (mapcar 'org-string-width words)))
  15565. w ll)
  15566. (cond (width
  15567. (org-do-wrap words (max maxword width)))
  15568. (lines
  15569. (setq w maxword)
  15570. (setq ll (org-do-wrap words maxword))
  15571. (if (<= (length ll) lines)
  15572. ll
  15573. (setq ll words)
  15574. (while (> (length ll) lines)
  15575. (setq w (1+ w))
  15576. (setq ll (org-do-wrap words w)))
  15577. ll))
  15578. (t (error "Cannot wrap this")))))
  15579. (defun org-do-wrap (words width)
  15580. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  15581. (let (lines line)
  15582. (while words
  15583. (setq line (pop words))
  15584. (while (and words (< (+ (length line) (length (car words))) width))
  15585. (setq line (concat line " " (pop words))))
  15586. (setq lines (push line lines)))
  15587. (nreverse lines)))
  15588. (defun org-split-string (string &optional separators)
  15589. "Splits STRING into substrings at SEPARATORS.
  15590. No empty strings are returned if there are matches at the beginning
  15591. and end of string."
  15592. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  15593. (start 0)
  15594. notfirst
  15595. (list nil))
  15596. (while (and (string-match rexp string
  15597. (if (and notfirst
  15598. (= start (match-beginning 0))
  15599. (< start (length string)))
  15600. (1+ start) start))
  15601. (< (match-beginning 0) (length string)))
  15602. (setq notfirst t)
  15603. (or (eq (match-beginning 0) 0)
  15604. (and (eq (match-beginning 0) (match-end 0))
  15605. (eq (match-beginning 0) start))
  15606. (setq list
  15607. (cons (substring string start (match-beginning 0))
  15608. list)))
  15609. (setq start (match-end 0)))
  15610. (or (eq start (length string))
  15611. (setq list
  15612. (cons (substring string start)
  15613. list)))
  15614. (nreverse list)))
  15615. (defun org-quote-vert (s)
  15616. "Replace \"|\" with \"\\vert\"."
  15617. (while (string-match "|" s)
  15618. (setq s (replace-match "\\vert" t t s)))
  15619. s)
  15620. (defun org-uuidgen-p (s)
  15621. "Is S an ID created by UUIDGEN?"
  15622. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  15623. (defun org-context ()
  15624. "Return a list of contexts of the current cursor position.
  15625. If several contexts apply, all are returned.
  15626. Each context entry is a list with a symbol naming the context, and
  15627. two positions indicating start and end of the context. Possible
  15628. contexts are:
  15629. :headline anywhere in a headline
  15630. :headline-stars on the leading stars in a headline
  15631. :todo-keyword on a TODO keyword (including DONE) in a headline
  15632. :tags on the TAGS in a headline
  15633. :priority on the priority cookie in a headline
  15634. :item on the first line of a plain list item
  15635. :item-bullet on the bullet/number of a plain list item
  15636. :checkbox on the checkbox in a plain list item
  15637. :table in an org-mode table
  15638. :table-special on a special filed in a table
  15639. :table-table in a table.el table
  15640. :link on a hyperlink
  15641. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  15642. :target on a <<target>>
  15643. :radio-target on a <<<radio-target>>>
  15644. :latex-fragment on a LaTeX fragment
  15645. :latex-preview on a LaTeX fragment with overlayed preview image
  15646. This function expects the position to be visible because it uses font-lock
  15647. faces as a help to recognize the following contexts: :table-special, :link,
  15648. and :keyword."
  15649. (let* ((f (get-text-property (point) 'face))
  15650. (faces (if (listp f) f (list f)))
  15651. (p (point)) clist o)
  15652. ;; First the large context
  15653. (cond
  15654. ((org-on-heading-p t)
  15655. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  15656. (when (progn
  15657. (beginning-of-line 1)
  15658. (looking-at org-todo-line-tags-regexp))
  15659. (push (org-point-in-group p 1 :headline-stars) clist)
  15660. (push (org-point-in-group p 2 :todo-keyword) clist)
  15661. (push (org-point-in-group p 4 :tags) clist))
  15662. (goto-char p)
  15663. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  15664. (if (looking-at "\\[#[A-Z0-9]\\]")
  15665. (push (org-point-in-group p 0 :priority) clist)))
  15666. ((org-at-item-p)
  15667. (push (org-point-in-group p 2 :item-bullet) clist)
  15668. (push (list :item (point-at-bol)
  15669. (save-excursion (org-end-of-item) (point)))
  15670. clist)
  15671. (and (org-at-item-checkbox-p)
  15672. (push (org-point-in-group p 0 :checkbox) clist)))
  15673. ((org-at-table-p)
  15674. (push (list :table (org-table-begin) (org-table-end)) clist)
  15675. (if (memq 'org-formula faces)
  15676. (push (list :table-special
  15677. (previous-single-property-change p 'face)
  15678. (next-single-property-change p 'face)) clist)))
  15679. ((org-at-table-p 'any)
  15680. (push (list :table-table) clist)))
  15681. (goto-char p)
  15682. ;; Now the small context
  15683. (cond
  15684. ((org-at-timestamp-p)
  15685. (push (org-point-in-group p 0 :timestamp) clist))
  15686. ((memq 'org-link faces)
  15687. (push (list :link
  15688. (previous-single-property-change p 'face)
  15689. (next-single-property-change p 'face)) clist))
  15690. ((memq 'org-special-keyword faces)
  15691. (push (list :keyword
  15692. (previous-single-property-change p 'face)
  15693. (next-single-property-change p 'face)) clist))
  15694. ((org-on-target-p)
  15695. (push (org-point-in-group p 0 :target) clist)
  15696. (goto-char (1- (match-beginning 0)))
  15697. (if (looking-at org-radio-target-regexp)
  15698. (push (org-point-in-group p 0 :radio-target) clist))
  15699. (goto-char p))
  15700. ((setq o (car (delq nil
  15701. (mapcar
  15702. (lambda (x)
  15703. (if (memq x org-latex-fragment-image-overlays) x))
  15704. (org-overlays-at (point))))))
  15705. (push (list :latex-fragment
  15706. (org-overlay-start o) (org-overlay-end o)) clist)
  15707. (push (list :latex-preview
  15708. (org-overlay-start o) (org-overlay-end o)) clist))
  15709. ((org-inside-LaTeX-fragment-p)
  15710. ;; FIXME: positions wrong.
  15711. (push (list :latex-fragment (point) (point)) clist)))
  15712. (setq clist (nreverse (delq nil clist)))
  15713. clist))
  15714. ;; FIXME: Compare with at-regexp-p Do we need both?
  15715. (defun org-in-regexp (re &optional nlines visually)
  15716. "Check if point is inside a match of regexp.
  15717. Normally only the current line is checked, but you can include NLINES extra
  15718. lines both before and after point into the search.
  15719. If VISUALLY is set, require that the cursor is not after the match but
  15720. really on, so that the block visually is on the match."
  15721. (catch 'exit
  15722. (let ((pos (point))
  15723. (eol (point-at-eol (+ 1 (or nlines 0))))
  15724. (inc (if visually 1 0)))
  15725. (save-excursion
  15726. (beginning-of-line (- 1 (or nlines 0)))
  15727. (while (re-search-forward re eol t)
  15728. (if (and (<= (match-beginning 0) pos)
  15729. (>= (+ inc (match-end 0)) pos))
  15730. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  15731. (defun org-at-regexp-p (regexp)
  15732. "Is point inside a match of REGEXP in the current line?"
  15733. (catch 'exit
  15734. (save-excursion
  15735. (let ((pos (point)) (end (point-at-eol)))
  15736. (beginning-of-line 1)
  15737. (while (re-search-forward regexp end t)
  15738. (if (and (<= (match-beginning 0) pos)
  15739. (>= (match-end 0) pos))
  15740. (throw 'exit t)))
  15741. nil))))
  15742. (defun org-in-regexps-block-p (start-re end-re)
  15743. "Returns t if the current point is between matches of START-RE and END-RE.
  15744. This will also return to if point is on one of the two matches."
  15745. (interactive)
  15746. (let ((p (point)))
  15747. (save-excursion
  15748. (and (or (org-at-regexp-p start-re)
  15749. (re-search-backward start-re nil t))
  15750. (re-search-forward end-re nil t)
  15751. (>= (point) p)))))
  15752. (defun org-occur-in-agenda-files (regexp &optional nlines)
  15753. "Call `multi-occur' with buffers for all agenda files."
  15754. (interactive "sOrg-files matching: \np")
  15755. (let* ((files (org-agenda-files))
  15756. (tnames (mapcar 'file-truename files))
  15757. (extra org-agenda-text-search-extra-files)
  15758. f)
  15759. (when (eq (car extra) 'agenda-archives)
  15760. (setq extra (cdr extra))
  15761. (setq files (org-add-archive-files files)))
  15762. (while (setq f (pop extra))
  15763. (unless (member (file-truename f) tnames)
  15764. (add-to-list 'files f 'append)
  15765. (add-to-list 'tnames (file-truename f) 'append)))
  15766. (multi-occur
  15767. (mapcar (lambda (x)
  15768. (with-current-buffer
  15769. (or (get-file-buffer x) (find-file-noselect x))
  15770. (widen)
  15771. (current-buffer)))
  15772. files)
  15773. regexp)))
  15774. (if (boundp 'occur-mode-find-occurrence-hook)
  15775. ;; Emacs 23
  15776. (add-hook 'occur-mode-find-occurrence-hook
  15777. (lambda ()
  15778. (when (org-mode-p)
  15779. (org-reveal))))
  15780. ;; Emacs 22
  15781. (defadvice occur-mode-goto-occurrence
  15782. (after org-occur-reveal activate)
  15783. (and (org-mode-p) (org-reveal)))
  15784. (defadvice occur-mode-goto-occurrence-other-window
  15785. (after org-occur-reveal activate)
  15786. (and (org-mode-p) (org-reveal)))
  15787. (defadvice occur-mode-display-occurrence
  15788. (after org-occur-reveal activate)
  15789. (when (org-mode-p)
  15790. (let ((pos (occur-mode-find-occurrence)))
  15791. (with-current-buffer (marker-buffer pos)
  15792. (save-excursion
  15793. (goto-char pos)
  15794. (org-reveal)))))))
  15795. (defun org-occur-link-in-agenda-files ()
  15796. "Create a link and search for it in the agendas.
  15797. The link is not stored in `org-stored-links', it is just created
  15798. for the search purpose."
  15799. (interactive)
  15800. (let ((link (condition-case nil
  15801. (org-store-link nil)
  15802. (error "Unable to create a link to here"))))
  15803. (org-occur-in-agenda-files (regexp-quote link))))
  15804. (defun org-uniquify (list)
  15805. "Remove duplicate elements from LIST."
  15806. (let (res)
  15807. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  15808. res))
  15809. (defun org-delete-all (elts list)
  15810. "Remove all elements in ELTS from LIST."
  15811. (while elts
  15812. (setq list (delete (pop elts) list)))
  15813. list)
  15814. (defun org-back-over-empty-lines ()
  15815. "Move backwards over whitespace, to the beginning of the first empty line.
  15816. Returns the number of empty lines passed."
  15817. (let ((pos (point)))
  15818. (skip-chars-backward " \t\n\r")
  15819. (beginning-of-line 2)
  15820. (goto-char (min (point) pos))
  15821. (count-lines (point) pos)))
  15822. (defun org-skip-whitespace ()
  15823. (skip-chars-forward " \t\n\r"))
  15824. (defun org-point-in-group (point group &optional context)
  15825. "Check if POINT is in match-group GROUP.
  15826. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  15827. match. If the match group does ot exist or point is not inside it,
  15828. return nil."
  15829. (and (match-beginning group)
  15830. (>= point (match-beginning group))
  15831. (<= point (match-end group))
  15832. (if context
  15833. (list context (match-beginning group) (match-end group))
  15834. t)))
  15835. (defun org-switch-to-buffer-other-window (&rest args)
  15836. "Switch to buffer in a second window on the current frame.
  15837. In particular, do not allow pop-up frames."
  15838. (let (pop-up-frames special-display-buffer-names special-display-regexps
  15839. special-display-function)
  15840. (apply 'switch-to-buffer-other-window args)))
  15841. (defun org-combine-plists (&rest plists)
  15842. "Create a single property list from all plists in PLISTS.
  15843. The process starts by copying the first list, and then setting properties
  15844. from the other lists. Settings in the last list are the most significant
  15845. ones and overrule settings in the other lists."
  15846. (let ((rtn (copy-sequence (pop plists)))
  15847. p v ls)
  15848. (while plists
  15849. (setq ls (pop plists))
  15850. (while ls
  15851. (setq p (pop ls) v (pop ls))
  15852. (setq rtn (plist-put rtn p v))))
  15853. rtn))
  15854. (defun org-move-line-down (arg)
  15855. "Move the current line down. With prefix argument, move it past ARG lines."
  15856. (interactive "p")
  15857. (let ((col (current-column))
  15858. beg end pos)
  15859. (beginning-of-line 1) (setq beg (point))
  15860. (beginning-of-line 2) (setq end (point))
  15861. (beginning-of-line (+ 1 arg))
  15862. (setq pos (move-marker (make-marker) (point)))
  15863. (insert (delete-and-extract-region beg end))
  15864. (goto-char pos)
  15865. (org-move-to-column col)))
  15866. (defun org-move-line-up (arg)
  15867. "Move the current line up. With prefix argument, move it past ARG lines."
  15868. (interactive "p")
  15869. (let ((col (current-column))
  15870. beg end pos)
  15871. (beginning-of-line 1) (setq beg (point))
  15872. (beginning-of-line 2) (setq end (point))
  15873. (beginning-of-line (- arg))
  15874. (setq pos (move-marker (make-marker) (point)))
  15875. (insert (delete-and-extract-region beg end))
  15876. (goto-char pos)
  15877. (org-move-to-column col)))
  15878. (defun org-replace-escapes (string table)
  15879. "Replace %-escapes in STRING with values in TABLE.
  15880. TABLE is an association list with keys like \"%a\" and string values.
  15881. The sequences in STRING may contain normal field width and padding information,
  15882. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  15883. so values can contain further %-escapes if they are define later in TABLE."
  15884. (let ((case-fold-search nil)
  15885. e re rpl)
  15886. (while (setq e (pop table))
  15887. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  15888. (while (string-match re string)
  15889. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  15890. (cdr e)))
  15891. (setq string (replace-match rpl t t string))))
  15892. string))
  15893. (defun org-sublist (list start end)
  15894. "Return a section of LIST, from START to END.
  15895. Counting starts at 1."
  15896. (let (rtn (c start))
  15897. (setq list (nthcdr (1- start) list))
  15898. (while (and list (<= c end))
  15899. (push (pop list) rtn)
  15900. (setq c (1+ c)))
  15901. (nreverse rtn)))
  15902. (defun org-find-base-buffer-visiting (file)
  15903. "Like `find-buffer-visiting' but always return the base buffer and
  15904. not an indirect buffer."
  15905. (let ((buf (or (get-file-buffer file)
  15906. (find-buffer-visiting file))))
  15907. (if buf
  15908. (or (buffer-base-buffer buf) buf)
  15909. nil)))
  15910. (defun org-image-file-name-regexp (&optional extensions)
  15911. "Return regexp matching the file names of images.
  15912. If EXTENSIONS is given, only match these."
  15913. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  15914. (image-file-name-regexp)
  15915. (let ((image-file-name-extensions
  15916. (or extensions
  15917. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  15918. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  15919. (concat "\\."
  15920. (regexp-opt (nconc (mapcar 'upcase
  15921. image-file-name-extensions)
  15922. image-file-name-extensions)
  15923. t)
  15924. "\\'"))))
  15925. (defun org-file-image-p (file &optional extensions)
  15926. "Return non-nil if FILE is an image."
  15927. (save-match-data
  15928. (string-match (org-image-file-name-regexp extensions) file)))
  15929. (defun org-get-cursor-date ()
  15930. "Return the date at cursor in as a time.
  15931. This works in the calendar and in the agenda, anywhere else it just
  15932. returns the current time."
  15933. (let (date day defd)
  15934. (cond
  15935. ((eq major-mode 'calendar-mode)
  15936. (setq date (calendar-cursor-to-date)
  15937. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15938. ((eq major-mode 'org-agenda-mode)
  15939. (setq day (get-text-property (point) 'day))
  15940. (if day
  15941. (setq date (calendar-gregorian-from-absolute day)
  15942. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  15943. (nth 2 date))))))
  15944. (or defd (current-time))))
  15945. (defvar org-agenda-action-marker (make-marker)
  15946. "Marker pointing to the entry for the next agenda action.")
  15947. (defun org-mark-entry-for-agenda-action ()
  15948. "Mark the current entry as target of an agenda action.
  15949. Agenda actions are actions executed from the agenda with the key `k',
  15950. which make use of the date at the cursor."
  15951. (interactive)
  15952. (move-marker org-agenda-action-marker
  15953. (save-excursion (org-back-to-heading t) (point))
  15954. (current-buffer))
  15955. (message
  15956. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  15957. ;;; Paragraph filling stuff.
  15958. ;; We want this to be just right, so use the full arsenal.
  15959. (defun org-indent-line-function ()
  15960. "Indent line like previous, but further if previous was headline or item."
  15961. (interactive)
  15962. (let* ((pos (point))
  15963. (itemp (org-at-item-p))
  15964. (case-fold-search t)
  15965. (org-drawer-regexp (or org-drawer-regexp "\000"))
  15966. column bpos bcol tpos tcol bullet btype bullet-type)
  15967. ;; Find the previous relevant line
  15968. (beginning-of-line 1)
  15969. (cond
  15970. ((looking-at "#") (setq column 0))
  15971. ((looking-at "\\*+ ") (setq column 0))
  15972. ((and (looking-at "[ \t]*:END:")
  15973. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  15974. (save-excursion
  15975. (goto-char (1- (match-beginning 1)))
  15976. (setq column (current-column))))
  15977. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  15978. (save-excursion
  15979. (re-search-backward
  15980. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  15981. (setq column (org-get-indentation (match-string 0))))
  15982. (t
  15983. (beginning-of-line 0)
  15984. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  15985. (not (looking-at "[ \t]*:END:"))
  15986. (not (looking-at org-drawer-regexp)))
  15987. (beginning-of-line 0))
  15988. (cond
  15989. ((looking-at "\\*+[ \t]+")
  15990. (if (not org-adapt-indentation)
  15991. (setq column 0)
  15992. (goto-char (match-end 0))
  15993. (setq column (current-column))))
  15994. ((looking-at org-drawer-regexp)
  15995. (goto-char (1- (match-beginning 1)))
  15996. (setq column (current-column)))
  15997. ((looking-at "\\([ \t]*\\):END:")
  15998. (goto-char (match-end 1))
  15999. (setq column (current-column)))
  16000. ((org-in-item-p)
  16001. (org-beginning-of-item)
  16002. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  16003. (setq bpos (match-beginning 1) tpos (match-end 0)
  16004. bcol (progn (goto-char bpos) (current-column))
  16005. tcol (progn (goto-char tpos) (current-column))
  16006. bullet (match-string 1)
  16007. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  16008. (if (> tcol (+ bcol org-description-max-indent))
  16009. (setq tcol (+ bcol 5)))
  16010. (if (not itemp)
  16011. (setq column tcol)
  16012. (goto-char pos)
  16013. (beginning-of-line 1)
  16014. (if (looking-at "\\S-")
  16015. (progn
  16016. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  16017. (setq bullet (match-string 1)
  16018. btype (if (string-match "[0-9]" bullet) "n" bullet))
  16019. (setq column (if (equal btype bullet-type) bcol tcol)))
  16020. (setq column (org-get-indentation)))))
  16021. (t (setq column (org-get-indentation))))))
  16022. (goto-char pos)
  16023. (if (<= (current-column) (current-indentation))
  16024. (org-indent-line-to column)
  16025. (save-excursion (org-indent-line-to column)))
  16026. (setq column (current-column))
  16027. (beginning-of-line 1)
  16028. (if (looking-at
  16029. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  16030. (replace-match (concat (match-string 1)
  16031. (format org-property-format
  16032. (match-string 2) (match-string 3)))
  16033. t t))
  16034. (org-move-to-column column)))
  16035. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  16036. "Variable to store copy of `adaptive-fill-regexp'.
  16037. Since `adaptive-fill-regexp' is set to never match, we need to
  16038. store a backup of its value before entering `org-mode' so that
  16039. the functionality can be provided as a fall-back.")
  16040. (defun org-set-autofill-regexps ()
  16041. (interactive)
  16042. ;; In the paragraph separator we include headlines, because filling
  16043. ;; text in a line directly attached to a headline would otherwise
  16044. ;; fill the headline as well.
  16045. (org-set-local 'comment-start-skip "^#+[ \t]*")
  16046. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  16047. ;; The paragraph starter includes hand-formatted lists.
  16048. (org-set-local
  16049. 'paragraph-start
  16050. (concat
  16051. "\f" "\\|"
  16052. "[ ]*$" "\\|"
  16053. "\\*+ " "\\|"
  16054. "[ \t]*#" "\\|"
  16055. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  16056. "[ \t]*[:|]" "\\|"
  16057. "\\$\\$" "\\|"
  16058. "\\\\\\(begin\\|end\\|[][]\\)"))
  16059. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  16060. ;; But only if the user has not turned off tables or fixed-width regions
  16061. (org-set-local
  16062. 'auto-fill-inhibit-regexp
  16063. (concat "\\*+ \\|#\\+"
  16064. "\\|[ \t]*" org-keyword-time-regexp
  16065. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  16066. (concat
  16067. "\\|[ \t]*["
  16068. (if org-enable-table-editor "|" "")
  16069. (if org-enable-fixed-width-editor ":" "")
  16070. "]"))))
  16071. ;; We use our own fill-paragraph function, to make sure that tables
  16072. ;; and fixed-width regions are not wrapped. That function will pass
  16073. ;; through to `fill-paragraph' when appropriate.
  16074. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  16075. ;; Adaptive filling: To get full control, first make sure that
  16076. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  16077. (unless (local-variable-p 'adaptive-fill-regexp)
  16078. (org-set-local 'org-adaptive-fill-regexp-backup
  16079. adaptive-fill-regexp))
  16080. (org-set-local 'adaptive-fill-regexp "\000")
  16081. (org-set-local 'adaptive-fill-function
  16082. 'org-adaptive-fill-function)
  16083. (org-set-local
  16084. 'align-mode-rules-list
  16085. '((org-in-buffer-settings
  16086. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  16087. (modes . '(org-mode))))))
  16088. (defun org-fill-paragraph (&optional justify)
  16089. "Re-align a table, pass through to fill-paragraph if no table."
  16090. (let ((table-p (org-at-table-p))
  16091. (table.el-p (org-at-table.el-p)))
  16092. (cond ((and (equal (char-after (point-at-bol)) ?*)
  16093. (save-excursion (goto-char (point-at-bol))
  16094. (looking-at outline-regexp)))
  16095. t) ; skip headlines
  16096. (table.el-p t) ; skip table.el tables
  16097. (table-p (org-table-align) t) ; align org-mode tables
  16098. (t nil)))) ; call paragraph-fill
  16099. ;; For reference, this is the default value of adaptive-fill-regexp
  16100. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  16101. (defun org-adaptive-fill-function ()
  16102. "Return a fill prefix for org-mode files.
  16103. In particular, this makes sure hanging paragraphs for hand-formatted lists
  16104. work correctly."
  16105. (cond
  16106. ;; Comment line
  16107. ((looking-at "#[ \t]+")
  16108. (match-string-no-properties 0))
  16109. ;; Description list
  16110. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  16111. (save-excursion
  16112. (if (> (match-end 1) (+ (match-beginning 1)
  16113. org-description-max-indent))
  16114. (goto-char (+ (match-beginning 1) 5))
  16115. (goto-char (match-end 0)))
  16116. (make-string (current-column) ?\ )))
  16117. ;; Ordered or unordered list
  16118. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)")
  16119. (save-excursion
  16120. (goto-char (match-end 0))
  16121. (make-string (current-column) ?\ )))
  16122. ;; Other text
  16123. ((looking-at org-adaptive-fill-regexp-backup)
  16124. (match-string-no-properties 0))))
  16125. ;;; Other stuff.
  16126. (defun org-toggle-fixed-width-section (arg)
  16127. "Toggle the fixed-width export.
  16128. If there is no active region, the QUOTE keyword at the current headline is
  16129. inserted or removed. When present, it causes the text between this headline
  16130. and the next to be exported as fixed-width text, and unmodified.
  16131. If there is an active region, this command adds or removes a colon as the
  16132. first character of this line. If the first character of a line is a colon,
  16133. this line is also exported in fixed-width font."
  16134. (interactive "P")
  16135. (let* ((cc 0)
  16136. (regionp (org-region-active-p))
  16137. (beg (if regionp (region-beginning) (point)))
  16138. (end (if regionp (region-end)))
  16139. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  16140. (case-fold-search nil)
  16141. (re "[ \t]*\\(: \\)")
  16142. off)
  16143. (if regionp
  16144. (save-excursion
  16145. (goto-char beg)
  16146. (setq cc (current-column))
  16147. (beginning-of-line 1)
  16148. (setq off (looking-at re))
  16149. (while (> nlines 0)
  16150. (setq nlines (1- nlines))
  16151. (beginning-of-line 1)
  16152. (cond
  16153. (arg
  16154. (org-move-to-column cc t)
  16155. (insert ": \n")
  16156. (forward-line -1))
  16157. ((and off (looking-at re))
  16158. (replace-match "" t t nil 1))
  16159. ((not off) (org-move-to-column cc t) (insert ": ")))
  16160. (forward-line 1)))
  16161. (save-excursion
  16162. (org-back-to-heading)
  16163. (if (looking-at (concat outline-regexp
  16164. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  16165. (replace-match "" t t nil 1)
  16166. (if (looking-at outline-regexp)
  16167. (progn
  16168. (goto-char (match-end 0))
  16169. (insert org-quote-string " "))))))))
  16170. (defun org-reftex-citation ()
  16171. "Use reftex-citation to insert a citation into the buffer.
  16172. This looks for a line like
  16173. #+BIBLIOGRAPHY: foo plain option:-d
  16174. and derives from it that foo.bib is the bibliography file relevant
  16175. for this document. It then installs the necessary environment for RefTeX
  16176. to work in this buffer and calls `reftex-citation' to insert a citation
  16177. into the buffer.
  16178. Export of such citations to both LaTeX and HTML is handled by the contributed
  16179. package org-exp-bibtex by Taru Karttunen."
  16180. (interactive)
  16181. (let ((reftex-docstruct-symbol 'rds)
  16182. (reftex-cite-format "\\cite{%l}")
  16183. rds bib)
  16184. (save-excursion
  16185. (save-restriction
  16186. (widen)
  16187. (let ((case-fold-search t)
  16188. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  16189. (if (not (save-excursion
  16190. (or (re-search-forward re nil t)
  16191. (re-search-backward re nil t))))
  16192. (error "No bibliography defined in file")
  16193. (setq bib (concat (match-string 1) ".bib")
  16194. rds (list (list 'bib bib)))))))
  16195. (call-interactively 'reftex-citation)))
  16196. ;;;; Functions extending outline functionality
  16197. (defun org-beginning-of-line (&optional arg)
  16198. "Go to the beginning of the current line. If that is invisible, continue
  16199. to a visible line beginning. This makes the function of C-a more intuitive.
  16200. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  16201. first attempt, and only move to after the tags when the cursor is already
  16202. beyond the end of the headline."
  16203. (interactive "P")
  16204. (let ((pos (point))
  16205. (special (if (consp org-special-ctrl-a/e)
  16206. (car org-special-ctrl-a/e)
  16207. org-special-ctrl-a/e))
  16208. refpos)
  16209. (if (org-bound-and-true-p line-move-visual)
  16210. (beginning-of-visual-line 1)
  16211. (beginning-of-line 1))
  16212. (if (and arg (fboundp 'move-beginning-of-line))
  16213. (call-interactively 'move-beginning-of-line)
  16214. (if (bobp)
  16215. nil
  16216. (backward-char 1)
  16217. (if (org-invisible-p)
  16218. (while (and (not (bobp)) (org-invisible-p))
  16219. (backward-char 1)
  16220. (beginning-of-line 1))
  16221. (forward-char 1))))
  16222. (when special
  16223. (cond
  16224. ((and (looking-at org-complex-heading-regexp)
  16225. (= (char-after (match-end 1)) ?\ ))
  16226. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  16227. (point-at-eol)))
  16228. (goto-char
  16229. (if (eq special t)
  16230. (cond ((> pos refpos) refpos)
  16231. ((= pos (point)) refpos)
  16232. (t (point)))
  16233. (cond ((> pos (point)) (point))
  16234. ((not (eq last-command this-command)) (point))
  16235. (t refpos)))))
  16236. ((org-at-item-p)
  16237. (goto-char
  16238. (if (eq special t)
  16239. (cond ((> pos (match-end 4)) (match-end 4))
  16240. ((= pos (point)) (match-end 4))
  16241. (t (point)))
  16242. (cond ((> pos (point)) (point))
  16243. ((not (eq last-command this-command)) (point))
  16244. (t (match-end 4))))))))
  16245. (org-no-warnings
  16246. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  16247. (defun org-end-of-line (&optional arg)
  16248. "Go to the end of the line.
  16249. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  16250. first attempt, and only move to after the tags when the cursor is already
  16251. beyond the end of the headline."
  16252. (interactive "P")
  16253. (let ((special (if (consp org-special-ctrl-a/e)
  16254. (cdr org-special-ctrl-a/e)
  16255. org-special-ctrl-a/e)))
  16256. (if (or (not special)
  16257. (not (org-on-heading-p))
  16258. arg)
  16259. (call-interactively
  16260. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  16261. ((fboundp 'move-end-of-line) 'move-end-of-line)
  16262. (t 'end-of-line)))
  16263. (let ((pos (point)))
  16264. (beginning-of-line 1)
  16265. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
  16266. (if (eq special t)
  16267. (if (or (< pos (match-beginning 1))
  16268. (= pos (match-end 0)))
  16269. (goto-char (match-beginning 1))
  16270. (goto-char (match-end 0)))
  16271. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  16272. (goto-char (match-end 0))
  16273. (goto-char (match-beginning 1))))
  16274. (call-interactively (if (fboundp 'move-end-of-line)
  16275. 'move-end-of-line
  16276. 'end-of-line)))))
  16277. (org-no-warnings
  16278. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  16279. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  16280. (define-key org-mode-map "\C-e" 'org-end-of-line)
  16281. (define-key org-mode-map [home] 'org-beginning-of-line)
  16282. (define-key org-mode-map [end] 'org-end-of-line)
  16283. (defun org-backward-sentence (&optional arg)
  16284. "Go to beginning of sentence, or beginning of table field.
  16285. This will call `backward-sentence' or `org-table-beginning-of-field',
  16286. depending on context."
  16287. (interactive "P")
  16288. (cond
  16289. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  16290. (t (call-interactively 'backward-sentence))))
  16291. (defun org-forward-sentence (&optional arg)
  16292. "Go to end of sentence, or end of table field.
  16293. This will call `forward-sentence' or `org-table-end-of-field',
  16294. depending on context."
  16295. (interactive "P")
  16296. (cond
  16297. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  16298. (t (call-interactively 'forward-sentence))))
  16299. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  16300. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  16301. (defun org-kill-line (&optional arg)
  16302. "Kill line, to tags or end of line."
  16303. (interactive "P")
  16304. (cond
  16305. ((or (not org-special-ctrl-k)
  16306. (bolp)
  16307. (not (org-on-heading-p)))
  16308. (call-interactively 'kill-line))
  16309. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  16310. (kill-region (point) (match-beginning 1))
  16311. (org-set-tags nil t))
  16312. (t (kill-region (point) (point-at-eol)))))
  16313. (define-key org-mode-map "\C-k" 'org-kill-line)
  16314. (defun org-yank (&optional arg)
  16315. "Yank. If the kill is a subtree, treat it specially.
  16316. This command will look at the current kill and check if is a single
  16317. subtree, or a series of subtrees[1]. If it passes the test, and if the
  16318. cursor is at the beginning of a line or after the stars of a currently
  16319. empty headline, then the yank is handled specially. How exactly depends
  16320. on the value of the following variables, both set by default.
  16321. org-yank-folded-subtrees
  16322. When set, the subtree(s) will be folded after insertion, but only
  16323. if doing so would now swallow text after the yanked text.
  16324. org-yank-adjusted-subtrees
  16325. When set, the subtree will be promoted or demoted in order to
  16326. fit into the local outline tree structure, which means that the level
  16327. will be adjusted so that it becomes the smaller one of the two
  16328. *visible* surrounding headings.
  16329. Any prefix to this command will cause `yank' to be called directly with
  16330. no special treatment. In particular, a simple `C-u' prefix will just
  16331. plainly yank the text as it is.
  16332. \[1] The test checks if the first non-white line is a heading
  16333. and if there are no other headings with fewer stars."
  16334. (interactive "P")
  16335. (org-yank-generic 'yank arg))
  16336. (defun org-yank-generic (command arg)
  16337. "Perform some yank-like command.
  16338. This function implements the behavior described in the `org-yank'
  16339. documentation. However, it has been generalized to work for any
  16340. interactive command with similar behavior."
  16341. ;; pretend to be command COMMAND
  16342. (setq this-command command)
  16343. (if arg
  16344. (call-interactively command)
  16345. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  16346. (and (org-kill-is-subtree-p)
  16347. (or (bolp)
  16348. (and (looking-at "[ \t]*$")
  16349. (string-match
  16350. "\\`\\*+\\'"
  16351. (buffer-substring (point-at-bol) (point)))))))
  16352. swallowp)
  16353. (cond
  16354. ((and subtreep org-yank-folded-subtrees)
  16355. (let ((beg (point))
  16356. end)
  16357. (if (and subtreep org-yank-adjusted-subtrees)
  16358. (org-paste-subtree nil nil 'for-yank)
  16359. (call-interactively command))
  16360. (setq end (point))
  16361. (goto-char beg)
  16362. (when (and (bolp) subtreep
  16363. (not (setq swallowp
  16364. (org-yank-folding-would-swallow-text beg end))))
  16365. (or (looking-at outline-regexp)
  16366. (re-search-forward (concat "^" outline-regexp) end t))
  16367. (while (and (< (point) end) (looking-at outline-regexp))
  16368. (hide-subtree)
  16369. (org-cycle-show-empty-lines 'folded)
  16370. (condition-case nil
  16371. (outline-forward-same-level 1)
  16372. (error (goto-char end)))))
  16373. (when swallowp
  16374. (message
  16375. "Inserted text not folded because that would swallow text"))
  16376. (goto-char end)
  16377. (skip-chars-forward " \t\n\r")
  16378. (beginning-of-line 1)
  16379. (push-mark beg 'nomsg)))
  16380. ((and subtreep org-yank-adjusted-subtrees)
  16381. (let ((beg (point-at-bol)))
  16382. (org-paste-subtree nil nil 'for-yank)
  16383. (push-mark beg 'nomsg)))
  16384. (t
  16385. (call-interactively command))))))
  16386. (defun org-yank-folding-would-swallow-text (beg end)
  16387. "Would hide-subtree at BEG swallow any text after END?"
  16388. (let (level)
  16389. (save-excursion
  16390. (goto-char beg)
  16391. (when (or (looking-at outline-regexp)
  16392. (re-search-forward (concat "^" outline-regexp) end t))
  16393. (setq level (org-outline-level)))
  16394. (goto-char end)
  16395. (skip-chars-forward " \t\r\n\v\f")
  16396. (if (or (eobp)
  16397. (and (bolp) (looking-at org-outline-regexp)
  16398. (<= (org-outline-level) level)))
  16399. nil ; Nothing would be swallowed
  16400. t)))) ; something would swallow
  16401. (define-key org-mode-map "\C-y" 'org-yank)
  16402. (defun org-invisible-p ()
  16403. "Check if point is at a character currently not visible."
  16404. ;; Early versions of noutline don't have `outline-invisible-p'.
  16405. (if (fboundp 'outline-invisible-p)
  16406. (outline-invisible-p)
  16407. (get-char-property (point) 'invisible)))
  16408. (defun org-invisible-p2 ()
  16409. "Check if point is at a character currently not visible."
  16410. (save-excursion
  16411. (if (and (eolp) (not (bobp))) (backward-char 1))
  16412. ;; Early versions of noutline don't have `outline-invisible-p'.
  16413. (if (fboundp 'outline-invisible-p)
  16414. (outline-invisible-p)
  16415. (get-char-property (point) 'invisible))))
  16416. (defun org-back-to-heading (&optional invisible-ok)
  16417. "Call `outline-back-to-heading', but provide a better error message."
  16418. (condition-case nil
  16419. (outline-back-to-heading invisible-ok)
  16420. (error (error "Before first headline at position %d in buffer %s"
  16421. (point) (current-buffer)))))
  16422. (defun org-before-first-heading-p ()
  16423. "Before first heading?"
  16424. (save-excursion
  16425. (null (re-search-backward "^\\*+ " nil t))))
  16426. (defun org-on-heading-p (&optional ignored)
  16427. (outline-on-heading-p t))
  16428. (defun org-at-heading-p (&optional ignored)
  16429. (outline-on-heading-p t))
  16430. (defun org-point-at-end-of-empty-headline ()
  16431. "If point is at the end of an empty headline, return t, else nil.
  16432. If the heading only contains a TODO keyword, it is still still considered
  16433. empty."
  16434. (and (looking-at "[ \t]*$")
  16435. (save-excursion
  16436. (beginning-of-line 1)
  16437. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  16438. "\\)?[ \t]*$")))))
  16439. (defun org-at-heading-or-item-p ()
  16440. (or (org-on-heading-p) (org-at-item-p)))
  16441. (defun org-on-target-p ()
  16442. (or (org-in-regexp org-radio-target-regexp)
  16443. (org-in-regexp org-target-regexp)))
  16444. (defun org-up-heading-all (arg)
  16445. "Move to the heading line of which the present line is a subheading.
  16446. This function considers both visible and invisible heading lines.
  16447. With argument, move up ARG levels."
  16448. (if (fboundp 'outline-up-heading-all)
  16449. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  16450. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  16451. (defun org-up-heading-safe ()
  16452. "Move to the heading line of which the present line is a subheading.
  16453. This version will not throw an error. It will return the level of the
  16454. headline found, or nil if no higher level is found.
  16455. Also, this function will be a lot faster than `outline-up-heading',
  16456. because it relies on stars being the outline starters. This can really
  16457. make a significant difference in outlines with very many siblings."
  16458. (let (start-level re)
  16459. (org-back-to-heading t)
  16460. (setq start-level (funcall outline-level))
  16461. (if (equal start-level 1)
  16462. nil
  16463. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  16464. (if (re-search-backward re nil t)
  16465. (funcall outline-level)))))
  16466. (defun org-first-sibling-p ()
  16467. "Is this heading the first child of its parents?"
  16468. (interactive)
  16469. (let ((re (concat "^" outline-regexp))
  16470. level l)
  16471. (unless (org-at-heading-p t)
  16472. (error "Not at a heading"))
  16473. (setq level (funcall outline-level))
  16474. (save-excursion
  16475. (if (not (re-search-backward re nil t))
  16476. t
  16477. (setq l (funcall outline-level))
  16478. (< l level)))))
  16479. (defun org-goto-sibling (&optional previous)
  16480. "Goto the next sibling, even if it is invisible.
  16481. When PREVIOUS is set, go to the previous sibling instead. Returns t
  16482. when a sibling was found. When none is found, return nil and don't
  16483. move point."
  16484. (let ((fun (if previous 're-search-backward 're-search-forward))
  16485. (pos (point))
  16486. (re (concat "^" outline-regexp))
  16487. level l)
  16488. (when (condition-case nil (org-back-to-heading t) (error nil))
  16489. (setq level (funcall outline-level))
  16490. (catch 'exit
  16491. (or previous (forward-char 1))
  16492. (while (funcall fun re nil t)
  16493. (setq l (funcall outline-level))
  16494. (when (< l level) (goto-char pos) (throw 'exit nil))
  16495. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  16496. (goto-char pos)
  16497. nil))))
  16498. (defun org-show-siblings ()
  16499. "Show all siblings of the current headline."
  16500. (save-excursion
  16501. (while (org-goto-sibling) (org-flag-heading nil)))
  16502. (save-excursion
  16503. (while (org-goto-sibling 'previous)
  16504. (org-flag-heading nil))))
  16505. (defun org-show-hidden-entry ()
  16506. "Show an entry where even the heading is hidden."
  16507. (save-excursion
  16508. (org-show-entry)))
  16509. (defun org-flag-heading (flag &optional entry)
  16510. "Flag the current heading. FLAG non-nil means make invisible.
  16511. When ENTRY is non-nil, show the entire entry."
  16512. (save-excursion
  16513. (org-back-to-heading t)
  16514. ;; Check if we should show the entire entry
  16515. (if entry
  16516. (progn
  16517. (org-show-entry)
  16518. (save-excursion
  16519. (and (outline-next-heading)
  16520. (org-flag-heading nil))))
  16521. (outline-flag-region (max (point-min) (1- (point)))
  16522. (save-excursion (outline-end-of-heading) (point))
  16523. flag))))
  16524. (defun org-get-next-sibling ()
  16525. "Move to next heading of the same level, and return point.
  16526. If there is no such heading, return nil.
  16527. This is like outline-next-sibling, but invisible headings are ok."
  16528. (let ((level (funcall outline-level)))
  16529. (outline-next-heading)
  16530. (while (and (not (eobp)) (> (funcall outline-level) level))
  16531. (outline-next-heading))
  16532. (if (or (eobp) (< (funcall outline-level) level))
  16533. nil
  16534. (point))))
  16535. (defun org-get-last-sibling ()
  16536. "Move to previous heading of the same level, and return point.
  16537. If there is no such heading, return nil."
  16538. (let ((opoint (point))
  16539. (level (funcall outline-level)))
  16540. (outline-previous-heading)
  16541. (when (and (/= (point) opoint) (outline-on-heading-p t))
  16542. (while (and (> (funcall outline-level) level)
  16543. (not (bobp)))
  16544. (outline-previous-heading))
  16545. (if (< (funcall outline-level) level)
  16546. nil
  16547. (point)))))
  16548. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  16549. ;; This contains an exact copy of the original function, but it uses
  16550. ;; `org-back-to-heading', to make it work also in invisible
  16551. ;; trees. And is uses an invisible-OK argument.
  16552. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  16553. ;; Furthermore, when used inside Org, finding the end of a large subtree
  16554. ;; with many children and grandchildren etc, this can be much faster
  16555. ;; than the outline version.
  16556. (org-back-to-heading invisible-OK)
  16557. (let ((first t)
  16558. (level (funcall outline-level)))
  16559. (if (and (org-mode-p) (< level 1000))
  16560. ;; A true heading (not a plain list item), in Org-mode
  16561. ;; This means we can easily find the end by looking
  16562. ;; only for the right number of stars. Using a regexp to do
  16563. ;; this is so much faster than using a Lisp loop.
  16564. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  16565. (forward-char 1)
  16566. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  16567. ;; something else, do it the slow way
  16568. (while (and (not (eobp))
  16569. (or first (> (funcall outline-level) level)))
  16570. (setq first nil)
  16571. (outline-next-heading)))
  16572. (unless to-heading
  16573. (if (memq (preceding-char) '(?\n ?\^M))
  16574. (progn
  16575. ;; Go to end of line before heading
  16576. (forward-char -1)
  16577. (if (memq (preceding-char) '(?\n ?\^M))
  16578. ;; leave blank line before heading
  16579. (forward-char -1))))))
  16580. (point))
  16581. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  16582. "Use Org version in org-mode, for dramatic speed-up."
  16583. (if (eq major-mode 'org-mode)
  16584. (progn
  16585. (org-end-of-subtree nil t)
  16586. (unless (eobp) (backward-char 1)))
  16587. ad-do-it))
  16588. (defun org-forward-same-level (arg &optional invisible-ok)
  16589. "Move forward to the arg'th subheading at same level as this one.
  16590. Stop at the first and last subheadings of a superior heading."
  16591. (interactive "p")
  16592. (org-back-to-heading invisible-ok)
  16593. (org-on-heading-p)
  16594. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  16595. (re (format "^\\*\\{1,%d\\} " level))
  16596. l)
  16597. (forward-char 1)
  16598. (while (> arg 0)
  16599. (while (and (re-search-forward re nil 'move)
  16600. (setq l (- (match-end 0) (match-beginning 0) 1))
  16601. (= l level)
  16602. (not invisible-ok)
  16603. (progn (backward-char 1) (org-invisible-p)))
  16604. (if (< l level) (setq arg 1)))
  16605. (setq arg (1- arg)))
  16606. (beginning-of-line 1)))
  16607. (defun org-backward-same-level (arg &optional invisible-ok)
  16608. "Move backward to the arg'th subheading at same level as this one.
  16609. Stop at the first and last subheadings of a superior heading."
  16610. (interactive "p")
  16611. (org-back-to-heading)
  16612. (org-on-heading-p)
  16613. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  16614. (re (format "^\\*\\{1,%d\\} " level))
  16615. l)
  16616. (while (> arg 0)
  16617. (while (and (re-search-backward re nil 'move)
  16618. (setq l (- (match-end 0) (match-beginning 0) 1))
  16619. (= l level)
  16620. (not invisible-ok)
  16621. (org-invisible-p))
  16622. (if (< l level) (setq arg 1)))
  16623. (setq arg (1- arg)))))
  16624. (defun org-show-subtree ()
  16625. "Show everything after this heading at deeper levels."
  16626. (outline-flag-region
  16627. (point)
  16628. (save-excursion
  16629. (org-end-of-subtree t t))
  16630. nil))
  16631. (defun org-show-entry ()
  16632. "Show the body directly following this heading.
  16633. Show the heading too, if it is currently invisible."
  16634. (interactive)
  16635. (save-excursion
  16636. (condition-case nil
  16637. (progn
  16638. (org-back-to-heading t)
  16639. (outline-flag-region
  16640. (max (point-min) (1- (point)))
  16641. (save-excursion
  16642. (if (re-search-forward
  16643. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  16644. (match-beginning 1)
  16645. (point-max)))
  16646. nil)
  16647. (org-cycle-hide-drawers 'children))
  16648. (error nil))))
  16649. (defun org-make-options-regexp (kwds &optional extra)
  16650. "Make a regular expression for keyword lines."
  16651. (concat
  16652. "^"
  16653. "#?[ \t]*\\+\\("
  16654. (mapconcat 'regexp-quote kwds "\\|")
  16655. (if extra (concat "\\|" extra))
  16656. "\\):[ \t]*"
  16657. "\\(.*\\)"))
  16658. ;; Make isearch reveal the necessary context
  16659. (defun org-isearch-end ()
  16660. "Reveal context after isearch exits."
  16661. (when isearch-success ; only if search was successful
  16662. (if (featurep 'xemacs)
  16663. ;; Under XEmacs, the hook is run in the correct place,
  16664. ;; we directly show the context.
  16665. (org-show-context 'isearch)
  16666. ;; In Emacs the hook runs *before* restoring the overlays.
  16667. ;; So we have to use a one-time post-command-hook to do this.
  16668. ;; (Emacs 22 has a special variable, see function `org-mode')
  16669. (unless (and (boundp 'isearch-mode-end-hook-quit)
  16670. isearch-mode-end-hook-quit)
  16671. ;; Only when the isearch was not quitted.
  16672. (org-add-hook 'post-command-hook 'org-isearch-post-command
  16673. 'append 'local)))))
  16674. (defun org-isearch-post-command ()
  16675. "Remove self from hook, and show context."
  16676. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  16677. (org-show-context 'isearch))
  16678. ;;;; Integration with and fixes for other packages
  16679. ;;; Imenu support
  16680. (defvar org-imenu-markers nil
  16681. "All markers currently used by Imenu.")
  16682. (make-variable-buffer-local 'org-imenu-markers)
  16683. (defun org-imenu-new-marker (&optional pos)
  16684. "Return a new marker for use by Imenu, and remember the marker."
  16685. (let ((m (make-marker)))
  16686. (move-marker m (or pos (point)))
  16687. (push m org-imenu-markers)
  16688. m))
  16689. (defun org-imenu-get-tree ()
  16690. "Produce the index for Imenu."
  16691. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  16692. (setq org-imenu-markers nil)
  16693. (let* ((n org-imenu-depth)
  16694. (re (concat "^" outline-regexp))
  16695. (subs (make-vector (1+ n) nil))
  16696. (last-level 0)
  16697. m level head)
  16698. (save-excursion
  16699. (save-restriction
  16700. (widen)
  16701. (goto-char (point-max))
  16702. (while (re-search-backward re nil t)
  16703. (setq level (org-reduced-level (funcall outline-level)))
  16704. (when (<= level n)
  16705. (looking-at org-complex-heading-regexp)
  16706. (setq head (org-link-display-format
  16707. (org-match-string-no-properties 4))
  16708. m (org-imenu-new-marker))
  16709. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  16710. (if (>= level last-level)
  16711. (push (cons head m) (aref subs level))
  16712. (push (cons head (aref subs (1+ level))) (aref subs level))
  16713. (loop for i from (1+ level) to n do (aset subs i nil)))
  16714. (setq last-level level)))))
  16715. (aref subs 1)))
  16716. (eval-after-load "imenu"
  16717. '(progn
  16718. (add-hook 'imenu-after-jump-hook
  16719. (lambda ()
  16720. (if (eq major-mode 'org-mode)
  16721. (org-show-context 'org-goto))))))
  16722. (defun org-link-display-format (link)
  16723. "Replace a link with either the description, or the link target
  16724. if no description is present"
  16725. (save-match-data
  16726. (if (string-match org-bracket-link-analytic-regexp link)
  16727. (replace-match (if (match-end 5)
  16728. (match-string 5 link)
  16729. (concat (match-string 1 link)
  16730. (match-string 3 link)))
  16731. nil t link)
  16732. link)))
  16733. ;; Speedbar support
  16734. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  16735. "Overlay marking the agenda restriction line in speedbar.")
  16736. (org-overlay-put org-speedbar-restriction-lock-overlay
  16737. 'face 'org-agenda-restriction-lock)
  16738. (org-overlay-put org-speedbar-restriction-lock-overlay
  16739. 'help-echo "Agendas are currently limited to this item.")
  16740. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  16741. (defun org-speedbar-set-agenda-restriction ()
  16742. "Restrict future agenda commands to the location at point in speedbar.
  16743. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  16744. (interactive)
  16745. (require 'org-agenda)
  16746. (let (p m tp np dir txt)
  16747. (cond
  16748. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16749. 'org-imenu t))
  16750. (setq m (get-text-property p 'org-imenu-marker))
  16751. (with-current-buffer (marker-buffer m)
  16752. (goto-char m)
  16753. (org-agenda-set-restriction-lock 'subtree)))
  16754. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16755. 'speedbar-function 'speedbar-find-file))
  16756. (setq tp (previous-single-property-change
  16757. (1+ p) 'speedbar-function)
  16758. np (next-single-property-change
  16759. tp 'speedbar-function)
  16760. dir (speedbar-line-directory)
  16761. txt (buffer-substring-no-properties (or tp (point-min))
  16762. (or np (point-max))))
  16763. (with-current-buffer (find-file-noselect
  16764. (let ((default-directory dir))
  16765. (expand-file-name txt)))
  16766. (unless (org-mode-p)
  16767. (error "Cannot restrict to non-Org-mode file"))
  16768. (org-agenda-set-restriction-lock 'file)))
  16769. (t (error "Don't know how to restrict Org-mode's agenda")))
  16770. (org-move-overlay org-speedbar-restriction-lock-overlay
  16771. (point-at-bol) (point-at-eol))
  16772. (setq current-prefix-arg nil)
  16773. (org-agenda-maybe-redo)))
  16774. (eval-after-load "speedbar"
  16775. '(progn
  16776. (speedbar-add-supported-extension ".org")
  16777. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  16778. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  16779. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  16780. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16781. (add-hook 'speedbar-visiting-tag-hook
  16782. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  16783. ;;; Fixes and Hacks for problems with other packages
  16784. ;; Make flyspell not check words in links, to not mess up our keymap
  16785. (defun org-mode-flyspell-verify ()
  16786. "Don't let flyspell put overlays at active buttons."
  16787. (and (not (get-text-property (point) 'keymap))
  16788. (not (get-text-property (point) 'org-no-flyspell))))
  16789. (defun org-remove-flyspell-overlays-in (beg end)
  16790. "Remove flyspell overlays in region."
  16791. (and (org-bound-and-true-p flyspell-mode)
  16792. (fboundp 'flyspell-delete-region-overlays)
  16793. (flyspell-delete-region-overlays beg end))
  16794. (add-text-properties beg end '(org-no-flyspell t)))
  16795. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  16796. (eval-after-load "bookmark"
  16797. '(if (boundp 'bookmark-after-jump-hook)
  16798. ;; We can use the hook
  16799. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  16800. ;; Hook not available, use advice
  16801. (defadvice bookmark-jump (after org-make-visible activate)
  16802. "Make the position visible."
  16803. (org-bookmark-jump-unhide))))
  16804. ;; Make sure saveplace shows the location if it was hidden
  16805. (eval-after-load "saveplace"
  16806. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  16807. "Make the position visible."
  16808. (org-bookmark-jump-unhide)))
  16809. ;; Make sure ecb shows the location if it was hidden
  16810. (eval-after-load "ecb"
  16811. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  16812. "Make hierarchy visible when jumping into location from ECB tree buffer."
  16813. (if (eq major-mode 'org-mode)
  16814. (org-show-context))))
  16815. (defun org-bookmark-jump-unhide ()
  16816. "Unhide the current position, to show the bookmark location."
  16817. (and (org-mode-p)
  16818. (or (org-invisible-p)
  16819. (save-excursion (goto-char (max (point-min) (1- (point))))
  16820. (org-invisible-p)))
  16821. (org-show-context 'bookmark-jump)))
  16822. ;; Make session.el ignore our circular variable
  16823. (eval-after-load "session"
  16824. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  16825. ;;;; Experimental code
  16826. (defun org-closed-in-range ()
  16827. "Sparse tree of items closed in a certain time range.
  16828. Still experimental, may disappear in the future."
  16829. (interactive)
  16830. ;; Get the time interval from the user.
  16831. (let* ((time1 (org-float-time
  16832. (org-read-date nil 'to-time nil "Starting date: ")))
  16833. (time2 (org-float-time
  16834. (org-read-date nil 'to-time nil "End date:")))
  16835. ;; callback function
  16836. (callback (lambda ()
  16837. (let ((time
  16838. (org-float-time
  16839. (apply 'encode-time
  16840. (org-parse-time-string
  16841. (match-string 1))))))
  16842. ;; check if time in interval
  16843. (and (>= time time1) (<= time time2))))))
  16844. ;; make tree, check each match with the callback
  16845. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  16846. ;;;; Finish up
  16847. (provide 'org)
  16848. (run-hooks 'org-load-hook)
  16849. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  16850. ;;; org.el ends here