org.el 700 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766
  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, 2010
  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.35trans
  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. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  73. (unless (boundp 'calendar-view-holidays-initially-flag)
  74. (defvaralias 'calendar-view-holidays-initially-flag
  75. 'view-calendar-holidays-initially))
  76. (unless (boundp 'calendar-view-diary-initially-flag)
  77. (defvaralias 'calendar-view-diary-initially-flag
  78. 'view-diary-entries-initially))
  79. (unless (boundp 'diary-fancy-buffer)
  80. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  81. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  82. ;; the file noutline.el being loaded.
  83. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  84. ;; We require noutline, which might be provided in outline.el
  85. (require 'outline) (require 'noutline)
  86. ;; Other stuff we need.
  87. (require 'time-date)
  88. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  89. (require 'easymenu)
  90. (require 'overlay)
  91. (require 'org-macs)
  92. (require 'org-entities)
  93. (require 'org-compat)
  94. (require 'org-faces)
  95. (require 'org-list)
  96. (require 'org-src)
  97. (require 'org-footnote)
  98. ;;;; Customization variables
  99. ;;; Version
  100. (defconst org-version "6.35trans"
  101. "The version number of the file org.el.")
  102. (defun org-version (&optional here)
  103. "Show the org-mode version in the echo area.
  104. With prefix arg HERE, insert it at point."
  105. (interactive "P")
  106. (let* ((origin default-directory)
  107. (version org-version)
  108. (git-version)
  109. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  110. (when (and (file-exists-p (expand-file-name ".git" dir))
  111. (executable-find "git"))
  112. (unwind-protect
  113. (progn
  114. (cd dir)
  115. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  116. (with-current-buffer "*Shell Command Output*"
  117. (goto-char (point-min))
  118. (setq git-version (buffer-substring (point) (point-at-eol))))
  119. (subst-char-in-string ?- ?. git-version t)
  120. (when (string-match "\\S-"
  121. (shell-command-to-string
  122. "git diff-index --name-only HEAD --"))
  123. (setq git-version (concat git-version ".dirty")))
  124. (setq version (concat version " (" git-version ")"))))
  125. (cd origin)))
  126. (setq version (format "Org-mode version %s" version))
  127. (if here (insert version))
  128. (message version)))
  129. ;;; Compatibility constants
  130. ;;; The custom variables
  131. (defgroup org nil
  132. "Outline-based notes management and organizer."
  133. :tag "Org"
  134. :group 'outlines
  135. :group 'calendar)
  136. (defcustom org-mode-hook nil
  137. "Mode hook for Org-mode, run after the mode was turned on."
  138. :group 'org
  139. :type 'hook)
  140. (defcustom org-load-hook nil
  141. "Hook that is run after org.el has been loaded."
  142. :group 'org
  143. :type 'hook)
  144. (defvar org-modules) ; defined below
  145. (defvar org-modules-loaded nil
  146. "Have the modules been loaded already?")
  147. (defun org-load-modules-maybe (&optional force)
  148. "Load all extensions listed in `org-modules'."
  149. (when (or force (not org-modules-loaded))
  150. (mapc (lambda (ext)
  151. (condition-case nil (require ext)
  152. (error (message "Problems while trying to load feature `%s'" ext))))
  153. org-modules)
  154. (setq org-modules-loaded t)))
  155. (defun org-set-modules (var value)
  156. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  157. (set var value)
  158. (when (featurep 'org)
  159. (org-load-modules-maybe 'force)))
  160. (when (org-bound-and-true-p org-modules)
  161. (let ((a (member 'org-infojs org-modules)))
  162. (and a (setcar a 'org-jsinfo))))
  163. (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)
  164. "Modules that should always be loaded together with org.el.
  165. If a description starts with <C>, the file is not part of Emacs
  166. and loading it will require that you have downloaded and properly installed
  167. the org-mode distribution.
  168. You can also use this system to load external packages (i.e. neither Org
  169. core modules, nor modules from the CONTRIB directory). Just add symbols
  170. to the end of the list. If the package is called org-xyz.el, then you need
  171. to add the symbol `xyz', and the package must have a call to
  172. (provide 'org-xyz)"
  173. :group 'org
  174. :set 'org-set-modules
  175. :type
  176. '(set :greedy t
  177. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  178. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  179. (const :tag " crypt: Encryption of subtrees" org-crypt)
  180. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  181. (const :tag " docview: Links to doc-view buffers" org-docview)
  182. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  183. (const :tag " id: Global IDs for identifying entries" org-id)
  184. (const :tag " info: Links to Info nodes" org-info)
  185. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  186. (const :tag " habit: Track your consistency with habits" org-habit)
  187. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  188. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  189. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  190. (const :tag " mew Links to Mew folders/messages" org-mew)
  191. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  192. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  193. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  194. (const :tag " vm: Links to VM folders/messages" org-vm)
  195. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  196. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  197. (const :tag " mouse: Additional mouse support" org-mouse)
  198. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  199. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  200. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  201. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  202. (const :tag "C collector: Collect properties into tables" org-collector)
  203. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  204. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  205. (const :tag "C eval: Include command output as text" org-eval)
  206. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  207. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  208. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  209. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  210. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  211. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  212. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  213. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  214. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  215. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  216. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  217. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  218. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  219. (const :tag "C registry: A registry for Org-mode links" org-registry)
  220. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  221. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  222. (const :tag "C secretary: Team management with org-mode" org-secretary)
  223. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  224. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  225. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  226. (const :tag "C track: Keep up with Org-mode development" org-track)
  227. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  228. (defcustom org-support-shift-select nil
  229. "Non-nil means make shift-cursor commands select text when possible.
  230. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  231. selecting a region, or enlarge thusly regions started in this way.
  232. In Org-mode, in special contexts, these same keys are used for other
  233. purposes, important enough to compete with shift selection. Org tries
  234. to balance these needs by supporting `shift-select-mode' outside these
  235. special contexts, under control of this variable.
  236. The default of this variable is nil, to avoid confusing behavior. Shifted
  237. cursor keys will then execute Org commands in the following contexts:
  238. - on a headline, changing TODO state (left/right) and priority (up/down)
  239. - on a time stamp, changing the time
  240. - in a plain list item, changing the bullet type
  241. - in a property definition line, switching between allowed values
  242. - in the BEGIN line of a clock table (changing the time block).
  243. Outside these contexts, the commands will throw an error.
  244. When this variable is t and the cursor is not in a special context,
  245. Org-mode will support shift-selection for making and enlarging regions.
  246. To make this more effective, the bullet cycling will no longer happen
  247. anywhere in an item line, but only if the cursor is exactly on the bullet.
  248. If you set this variable to the symbol `always', then the keys
  249. will not be special in headlines, property lines, and item lines, to make
  250. shift selection work there as well. If this is what you want, you can
  251. use the following alternative commands: `C-c C-t' and `C-c ,' to
  252. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  253. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  254. edited by hand or in column view.
  255. However, when the cursor is on a timestamp, shift-cursor commands
  256. will still edit the time stamp - this is just too good to give up.
  257. XEmacs user should have this variable set to nil, because shift-select-mode
  258. is Emacs 23 only."
  259. :group 'org
  260. :type '(choice
  261. (const :tag "Never" nil)
  262. (const :tag "When outside special context" t)
  263. (const :tag "Everywhere except timestamps" always)))
  264. (defgroup org-startup nil
  265. "Options concerning startup of Org-mode."
  266. :tag "Org Startup"
  267. :group 'org)
  268. (defcustom org-startup-folded t
  269. "Non-nil means entering Org-mode will switch to OVERVIEW.
  270. This can also be configured on a per-file basis by adding one of
  271. the following lines anywhere in the buffer:
  272. #+STARTUP: fold (or `overview', this is equivalent)
  273. #+STARTUP: nofold (or `showall', this is equivalent)
  274. #+STARTUP: content
  275. #+STARTUP: showeverything"
  276. :group 'org-startup
  277. :type '(choice
  278. (const :tag "nofold: show all" nil)
  279. (const :tag "fold: overview" t)
  280. (const :tag "content: all headlines" content)
  281. (const :tag "show everything, even drawers" showeverything)))
  282. (defcustom org-startup-truncated t
  283. "Non-nil means entering Org-mode will set `truncate-lines'.
  284. This is useful since some lines containing links can be very long and
  285. uninteresting. Also tables look terrible when wrapped."
  286. :group 'org-startup
  287. :type 'boolean)
  288. (defcustom org-startup-indented nil
  289. "Non-nil means turn on `org-indent-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: indent
  293. #+STARTUP: noindent"
  294. :group 'org-structure
  295. :type '(choice
  296. (const :tag "Not" nil)
  297. (const :tag "Globally (slow on startup in large files)" t)))
  298. (defcustom org-startup-with-beamer-mode nil
  299. "Non-nil means turn on `org-beamer-mode' on startup.
  300. This can also be configured on a per-file basis by adding one of
  301. the following lines anywhere in the buffer:
  302. #+STARTUP: beamer"
  303. :group 'org-startup
  304. :type 'boolean)
  305. (defcustom org-startup-align-all-tables nil
  306. "Non-nil means align all tables when visiting a file.
  307. This is useful when the column width in tables is forced with <N> cookies
  308. in table fields. Such tables will look correct only after the first re-align.
  309. This can also be configured on a per-file basis by adding one of
  310. the following lines anywhere in the buffer:
  311. #+STARTUP: align
  312. #+STARTUP: noalign"
  313. :group 'org-startup
  314. :type 'boolean)
  315. (defcustom org-insert-mode-line-in-empty-file nil
  316. "Non-nil means insert the first line setting Org-mode in empty files.
  317. When the function `org-mode' is called interactively in an empty file, this
  318. normally means that the file name does not automatically trigger Org-mode.
  319. To ensure that the file will always be in Org-mode in the future, a
  320. line enforcing Org-mode will be inserted into the buffer, if this option
  321. has been set."
  322. :group 'org-startup
  323. :type 'boolean)
  324. (defcustom org-replace-disputed-keys nil
  325. "Non-nil means use alternative key bindings for some keys.
  326. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  327. These keys are also used by other packages like shift-selection-mode'
  328. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  329. If you want to use Org-mode together with one of these other modes,
  330. or more generally if you would like to move some Org-mode commands to
  331. other keys, set this variable and configure the keys with the variable
  332. `org-disputed-keys'.
  333. This option is only relevant at load-time of Org-mode, and must be set
  334. *before* org.el is loaded. Changing it requires a restart of Emacs to
  335. become effective."
  336. :group 'org-startup
  337. :type 'boolean)
  338. (defcustom org-use-extra-keys nil
  339. "Non-nil means use extra key sequence definitions for certain
  340. commands. This happens automatically if you run XEmacs or if
  341. window-system is nil. This variable lets you do the same
  342. manually. You must set it before loading org.
  343. Example: on Carbon Emacs 22 running graphically, with an external
  344. keyboard on a Powerbook, the default way of setting M-left might
  345. not work for either Alt or ESC. Setting this variable will make
  346. it work for ESC."
  347. :group 'org-startup
  348. :type 'boolean)
  349. (if (fboundp 'defvaralias)
  350. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  351. (defcustom org-disputed-keys
  352. '(([(shift up)] . [(meta p)])
  353. ([(shift down)] . [(meta n)])
  354. ([(shift left)] . [(meta -)])
  355. ([(shift right)] . [(meta +)])
  356. ([(control shift right)] . [(meta shift +)])
  357. ([(control shift left)] . [(meta shift -)]))
  358. "Keys for which Org-mode and other modes compete.
  359. This is an alist, cars are the default keys, second element specifies
  360. the alternative to use when `org-replace-disputed-keys' is t.
  361. Keys can be specified in any syntax supported by `define-key'.
  362. The value of this option takes effect only at Org-mode's startup,
  363. therefore you'll have to restart Emacs to apply it after changing."
  364. :group 'org-startup
  365. :type 'alist)
  366. (defun org-key (key)
  367. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  368. Or return the original if not disputed.
  369. Also apply the trnaslations defined in `org-xemacs-key-equivalents'."
  370. (when org-replace-disputed-keys
  371. (let* ((nkey (key-description key))
  372. (x (org-find-if (lambda (x)
  373. (equal (key-description (car x)) nkey))
  374. org-disputed-keys)))
  375. (setq key (if x (cdr x) key))))
  376. (when (featurep 'xemacs)
  377. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  378. key)
  379. (defun org-find-if (predicate seq)
  380. (catch 'exit
  381. (while seq
  382. (if (funcall predicate (car seq))
  383. (throw 'exit (car seq))
  384. (pop seq)))))
  385. (defun org-defkey (keymap key def)
  386. "Define a key, possibly translated, as returned by `org-key'."
  387. (define-key keymap (org-key key) def))
  388. (defcustom org-ellipsis nil
  389. "The ellipsis to use in the Org-mode outline.
  390. When nil, just use the standard three dots. When a string, use that instead,
  391. When a face, use the standard 3 dots, but with the specified face.
  392. The change affects only Org-mode (which will then use its own display table).
  393. Changing this requires executing `M-x org-mode' in a buffer to become
  394. effective."
  395. :group 'org-startup
  396. :type '(choice (const :tag "Default" nil)
  397. (face :tag "Face" :value org-warning)
  398. (string :tag "String" :value "...#")))
  399. (defvar org-display-table nil
  400. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  401. (defgroup org-keywords nil
  402. "Keywords in Org-mode."
  403. :tag "Org Keywords"
  404. :group 'org)
  405. (defcustom org-deadline-string "DEADLINE:"
  406. "String to mark deadline entries.
  407. A deadline is this string, followed by a time stamp. Should be a word,
  408. terminated by a colon. You can insert a schedule keyword and
  409. a timestamp with \\[org-deadline].
  410. Changes become only effective after restarting Emacs."
  411. :group 'org-keywords
  412. :type 'string)
  413. (defcustom org-scheduled-string "SCHEDULED:"
  414. "String to mark scheduled TODO entries.
  415. A schedule is this string, followed by a time stamp. Should be a word,
  416. terminated by a colon. You can insert a schedule keyword and
  417. a timestamp with \\[org-schedule].
  418. Changes become only effective after restarting Emacs."
  419. :group 'org-keywords
  420. :type 'string)
  421. (defcustom org-closed-string "CLOSED:"
  422. "String used as the prefix for timestamps logging closing a TODO entry."
  423. :group 'org-keywords
  424. :type 'string)
  425. (defcustom org-clock-string "CLOCK:"
  426. "String used as prefix for timestamps clocking work hours on an item."
  427. :group 'org-keywords
  428. :type 'string)
  429. (defcustom org-comment-string "COMMENT"
  430. "Entries starting with this keyword will never be exported.
  431. An entry can be toggled between COMMENT and normal with
  432. \\[org-toggle-comment].
  433. Changes become only effective after restarting Emacs."
  434. :group 'org-keywords
  435. :type 'string)
  436. (defcustom org-quote-string "QUOTE"
  437. "Entries starting with this keyword will be exported in fixed-width font.
  438. Quoting applies only to the text in the entry following the headline, and does
  439. not extend beyond the next headline, even if that is lower level.
  440. An entry can be toggled between QUOTE and normal with
  441. \\[org-toggle-fixed-width-section]."
  442. :group 'org-keywords
  443. :type 'string)
  444. (defconst org-repeat-re
  445. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  446. "Regular expression for specifying repeated events.
  447. After a match, group 1 contains the repeat expression.")
  448. (defgroup org-structure nil
  449. "Options concerning the general structure of Org-mode files."
  450. :tag "Org Structure"
  451. :group 'org)
  452. (defgroup org-reveal-location nil
  453. "Options about how to make context of a location visible."
  454. :tag "Org Reveal Location"
  455. :group 'org-structure)
  456. (defconst org-context-choice
  457. '(choice
  458. (const :tag "Always" t)
  459. (const :tag "Never" nil)
  460. (repeat :greedy t :tag "Individual contexts"
  461. (cons
  462. (choice :tag "Context"
  463. (const agenda)
  464. (const org-goto)
  465. (const occur-tree)
  466. (const tags-tree)
  467. (const link-search)
  468. (const mark-goto)
  469. (const bookmark-jump)
  470. (const isearch)
  471. (const default))
  472. (boolean))))
  473. "Contexts for the reveal options.")
  474. (defcustom org-show-hierarchy-above '((default . t))
  475. "Non-nil means show full hierarchy when revealing a location.
  476. Org-mode often shows locations in an org-mode file which might have
  477. been invisible before. When this is set, the hierarchy of headings
  478. above the exposed location is shown.
  479. Turning this off for example for sparse trees makes them very compact.
  480. Instead of t, this can also be an alist specifying this option for different
  481. contexts. Valid contexts are
  482. agenda when exposing an entry from the agenda
  483. org-goto when using the command `org-goto' on key C-c C-j
  484. occur-tree when using the command `org-occur' on key C-c /
  485. tags-tree when constructing a sparse tree based on tags matches
  486. link-search when exposing search matches associated with a link
  487. mark-goto when exposing the jump goal of a mark
  488. bookmark-jump when exposing a bookmark location
  489. isearch when exiting from an incremental search
  490. default default for all contexts not set explicitly"
  491. :group 'org-reveal-location
  492. :type org-context-choice)
  493. (defcustom org-show-following-heading '((default . nil))
  494. "Non-nil means show following heading when revealing a location.
  495. Org-mode often shows locations in an org-mode file which might have
  496. been invisible before. When this is set, the heading following the
  497. match is shown.
  498. Turning this off for example for sparse trees makes them very compact,
  499. but makes it harder to edit the location of the match. In such a case,
  500. use the command \\[org-reveal] to show more context.
  501. Instead of t, this can also be an alist specifying this option for different
  502. contexts. See `org-show-hierarchy-above' for valid contexts."
  503. :group 'org-reveal-location
  504. :type org-context-choice)
  505. (defcustom org-show-siblings '((default . nil) (isearch t))
  506. "Non-nil means show all sibling heading when revealing a location.
  507. Org-mode often shows locations in an org-mode file which might have
  508. been invisible before. When this is set, the sibling of the current entry
  509. heading are all made visible. If `org-show-hierarchy-above' is t,
  510. the same happens on each level of the hierarchy above the current entry.
  511. By default this is on for the isearch context, off for all other contexts.
  512. Turning this off for example for sparse trees makes them very compact,
  513. but makes it harder to edit the location of the match. In such a case,
  514. use the command \\[org-reveal] to show more context.
  515. Instead of t, this can also be an alist specifying this option for different
  516. contexts. See `org-show-hierarchy-above' for valid contexts."
  517. :group 'org-reveal-location
  518. :type org-context-choice)
  519. (defcustom org-show-entry-below '((default . nil))
  520. "Non-nil means show the entry below a headline when revealing a location.
  521. Org-mode often shows locations in an org-mode file which might have
  522. been invisible before. When this is set, the text below the headline that is
  523. exposed is also shown.
  524. By default this is off for all contexts.
  525. Instead of t, this can also be an alist specifying this option for different
  526. contexts. See `org-show-hierarchy-above' for valid contexts."
  527. :group 'org-reveal-location
  528. :type org-context-choice)
  529. (defcustom org-indirect-buffer-display 'other-window
  530. "How should indirect tree buffers be displayed?
  531. This applies to indirect buffers created with the commands
  532. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  533. Valid values are:
  534. current-window Display in the current window
  535. other-window Just display in another window.
  536. dedicated-frame Create one new frame, and re-use it each time.
  537. new-frame Make a new frame each time. Note that in this case
  538. previously-made indirect buffers are kept, and you need to
  539. kill these buffers yourself."
  540. :group 'org-structure
  541. :group 'org-agenda-windows
  542. :type '(choice
  543. (const :tag "In current window" current-window)
  544. (const :tag "In current frame, other window" other-window)
  545. (const :tag "Each time a new frame" new-frame)
  546. (const :tag "One dedicated frame" dedicated-frame)))
  547. (defcustom org-use-speed-commands nil
  548. "Non-nil means activate single letter commands at beginning of a headline.
  549. This may also be a function to test for appropriate locations where speed
  550. commands should be active."
  551. :group 'org-structure
  552. :type '(choice
  553. (const :tag "Never" nil)
  554. (const :tag "At beginning of headline stars" t)
  555. (function)))
  556. (defcustom org-speed-commands-user nil
  557. "Alist of additional speed commands.
  558. This list will be checked before `org-speed-commands-default'
  559. when the variable `org-use-speed-commands' is non-nil
  560. and when the cursor is at the beginning of a headline.
  561. The car if each entry is a string with a single letter, which must
  562. be assigned to `self-insert-command' in the global map.
  563. The cdr is either a command to be called interactively, a function
  564. to be called, or a form to be evaluated.
  565. An entry that is just a list with a single string will be interpreted
  566. as a descriptive headline that will be added when listing the speed
  567. copmmands in the Help buffer using the `?' speed command."
  568. :group 'org-structure
  569. :type '(repeat :value ("k" . ignore)
  570. (choice :value ("k" . ignore)
  571. (list :tag "Descriptive Headline" (string :tag "Headline"))
  572. (cons :tag "Letter and Command"
  573. (string :tag "Command letter")
  574. (choice
  575. (function)
  576. (sexp))))))
  577. (defgroup org-cycle nil
  578. "Options concerning visibility cycling in Org-mode."
  579. :tag "Org Cycle"
  580. :group 'org-structure)
  581. (defcustom org-cycle-skip-children-state-if-no-children t
  582. "Non-nil means skip CHILDREN state in entries that don't have any."
  583. :group 'org-cycle
  584. :type 'boolean)
  585. (defcustom org-cycle-max-level nil
  586. "Maximum level which should still be subject to visibility cycling.
  587. Levels higher than this will, for cycling, be treated as text, not a headline.
  588. When `org-odd-levels-only' is set, a value of N in this variable actually
  589. means 2N-1 stars as the limiting headline.
  590. When nil, cycle all levels.
  591. Note that the limiting level of cycling is also influenced by
  592. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  593. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  594. than its value."
  595. :group 'org-cycle
  596. :type '(choice
  597. (const :tag "No limit" nil)
  598. (integer :tag "Maximum level")))
  599. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  600. "Names of drawers. Drawers are not opened by cycling on the headline above.
  601. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  602. this:
  603. :DRAWERNAME:
  604. .....
  605. :END:
  606. The drawer \"PROPERTIES\" is special for capturing properties through
  607. the property API.
  608. Drawers can be defined on the per-file basis with a line like:
  609. #+DRAWERS: HIDDEN STATE PROPERTIES"
  610. :group 'org-structure
  611. :group 'org-cycle
  612. :type '(repeat (string :tag "Drawer Name")))
  613. (defcustom org-hide-block-startup nil
  614. "Non-nil means entering Org-mode will fold all blocks.
  615. This can also be set in on a per-file basis with
  616. #+STARTUP: hideblocks
  617. #+STARTUP: showblocks"
  618. :group 'org-startup
  619. :group 'org-cycle
  620. :type 'boolean)
  621. (defcustom org-cycle-global-at-bob nil
  622. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  623. This makes it possible to do global cycling without having to use S-TAB or
  624. C-u TAB. For this special case to work, the first line of the buffer
  625. must not be a headline - it may be empty or some other text. When used in
  626. this way, `org-cycle-hook' is disables temporarily, to make sure the
  627. cursor stays at the beginning of the buffer.
  628. When this option is nil, don't do anything special at the beginning
  629. of the buffer."
  630. :group 'org-cycle
  631. :type 'boolean)
  632. (defcustom org-cycle-level-after-item/entry-creation t
  633. "Non-nil means cycle entry level or item indentation in new empty entries.
  634. When the cursor is at the end of an empty headline, i.e with only stars
  635. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  636. and then all possible anchestor states, before returning to the original state.
  637. This makes data entry extremely fast: M-RET to create a new headline,
  638. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  639. When the cursor is at the end of an empty plain list item, one TAB will
  640. make it a subitem, two or more tabs will back up to make this an item
  641. higher up in the item hierarchy."
  642. :group 'org-cycle
  643. :type 'boolean)
  644. (defcustom org-cycle-emulate-tab t
  645. "Where should `org-cycle' emulate TAB.
  646. nil Never
  647. white Only in completely white lines
  648. whitestart Only at the beginning of lines, before the first non-white char
  649. t Everywhere except in headlines
  650. exc-hl-bol Everywhere except at the start of a headline
  651. If TAB is used in a place where it does not emulate TAB, the current subtree
  652. visibility is cycled."
  653. :group 'org-cycle
  654. :type '(choice (const :tag "Never" nil)
  655. (const :tag "Only in completely white lines" white)
  656. (const :tag "Before first char in a line" whitestart)
  657. (const :tag "Everywhere except in headlines" t)
  658. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  659. ))
  660. (defcustom org-cycle-separator-lines 2
  661. "Number of empty lines needed to keep an empty line between collapsed trees.
  662. If you leave an empty line between the end of a subtree and the following
  663. headline, this empty line is hidden when the subtree is folded.
  664. Org-mode will leave (exactly) one empty line visible if the number of
  665. empty lines is equal or larger to the number given in this variable.
  666. So the default 2 means at least 2 empty lines after the end of a subtree
  667. are needed to produce free space between a collapsed subtree and the
  668. following headline.
  669. If the number is negative, and the number of empty lines is at least -N,
  670. all empty lines are shown.
  671. Special case: when 0, never leave empty lines in collapsed view."
  672. :group 'org-cycle
  673. :type 'integer)
  674. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  675. (defcustom org-pre-cycle-hook nil
  676. "Hook that is run before visibility cycling is happening.
  677. The function(s) in this hook must accept a single argument which indicates
  678. the new state that will be set right after running this hook. The
  679. argument is a symbol. Before a global state change, it can have the values
  680. `overview', `content', or `all'. Before a local state change, it can have
  681. the values `folded', `children', or `subtree'."
  682. :group 'org-cycle
  683. :type 'hook)
  684. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  685. org-cycle-hide-drawers
  686. org-cycle-show-empty-lines
  687. org-optimize-window-after-visibility-change)
  688. "Hook that is run after `org-cycle' has changed the buffer visibility.
  689. The function(s) in this hook must accept a single argument which indicates
  690. the new state that was set by the most recent `org-cycle' command. The
  691. argument is a symbol. After a global state change, it can have the values
  692. `overview', `content', or `all'. After a local state change, it can have
  693. the values `folded', `children', or `subtree'."
  694. :group 'org-cycle
  695. :type 'hook)
  696. (defgroup org-edit-structure nil
  697. "Options concerning structure editing in Org-mode."
  698. :tag "Org Edit Structure"
  699. :group 'org-structure)
  700. (defcustom org-odd-levels-only nil
  701. "Non-nil means skip even levels and only use odd levels for the outline.
  702. This has the effect that two stars are being added/taken away in
  703. promotion/demotion commands. It also influences how levels are
  704. handled by the exporters.
  705. Changing it requires restart of `font-lock-mode' to become effective
  706. for fontification also in regions already fontified.
  707. You may also set this on a per-file basis by adding one of the following
  708. lines to the buffer:
  709. #+STARTUP: odd
  710. #+STARTUP: oddeven"
  711. :group 'org-edit-structure
  712. :group 'org-appearance
  713. :type 'boolean)
  714. (defcustom org-adapt-indentation t
  715. "Non-nil means adapt indentation to outline node level.
  716. When this variable is set, Org assumes that you write outlines by
  717. indenting text in each node to align with the headline (after the stars).
  718. The following issues are influenced by this variable:
  719. - When this is set and the *entire* text in an entry is indented, the
  720. indentation is increased by one space in a demotion command, and
  721. decreased by one in a promotion command. If any line in the entry
  722. body starts with text at column 0, indentation is not changed at all.
  723. - Property drawers and planning information is inserted indented when
  724. this variable s set. When nil, they will not be indented.
  725. - TAB indents a line relative to context. The lines below a headline
  726. will be indented when this variable is set.
  727. Note that this is all about true indentation, by adding and removing
  728. space characters. See also `org-indent.el' which does level-dependent
  729. indentation in a virtual way, i.e. at display time in Emacs."
  730. :group 'org-edit-structure
  731. :type 'boolean)
  732. (defcustom org-special-ctrl-a/e nil
  733. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  734. When t, `C-a' will bring back the cursor to the beginning of the
  735. headline text, i.e. after the stars and after a possible TODO keyword.
  736. In an item, this will be the position after the bullet.
  737. When the cursor is already at that position, another `C-a' will bring
  738. it to the beginning of the line.
  739. `C-e' will jump to the end of the headline, ignoring the presence of tags
  740. in the headline. A second `C-e' will then jump to the true end of the
  741. line, after any tags. This also means that, when this variable is
  742. non-nil, `C-e' also will never jump beyond the end of the heading of a
  743. folded section, i.e. not after the ellipses.
  744. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  745. going to the true line boundary first. Only a directly following, identical
  746. keypress will bring the cursor to the special positions.
  747. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  748. set separately."
  749. :group 'org-edit-structure
  750. :type '(choice
  751. (const :tag "off" nil)
  752. (const :tag "on: after stars/bullet and before tags first" t)
  753. (const :tag "reversed: true line boundary first" reversed)
  754. (cons :tag "Set C-a and C-e separately"
  755. (choice :tag "Special C-a"
  756. (const :tag "off" nil)
  757. (const :tag "on: after stars/bullet first" t)
  758. (const :tag "reversed: before stars/bullet first" reversed))
  759. (choice :tag "Special C-e"
  760. (const :tag "off" nil)
  761. (const :tag "on: before tags first" t)
  762. (const :tag "reversed: after tags first" reversed)))))
  763. (if (fboundp 'defvaralias)
  764. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  765. (defcustom org-special-ctrl-k nil
  766. "Non-nil means `C-k' will behave specially in headlines.
  767. When nil, `C-k' will call the default `kill-line' command.
  768. When t, the following will happen while the cursor is in the headline:
  769. - When the cursor is at the beginning of a headline, kill the entire
  770. line and possible the folded subtree below the line.
  771. - When in the middle of the headline text, kill the headline up to the tags.
  772. - When after the headline text, kill the tags."
  773. :group 'org-edit-structure
  774. :type 'boolean)
  775. (defcustom org-yank-folded-subtrees t
  776. "Non-nil means when yanking subtrees, fold them.
  777. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  778. it starts with a heading and all other headings in it are either children
  779. or siblings, then fold all the subtrees. However, do this only if no
  780. text after the yank would be swallowed into a folded tree by this action."
  781. :group 'org-edit-structure
  782. :type 'boolean)
  783. (defcustom org-yank-adjusted-subtrees nil
  784. "Non-nil means when yanking subtrees, adjust the level.
  785. With this setting, `org-paste-subtree' is used to insert the subtree, see
  786. this function for details."
  787. :group 'org-edit-structure
  788. :type 'boolean)
  789. (defcustom org-M-RET-may-split-line '((default . t))
  790. "Non-nil means M-RET will split the line at the cursor position.
  791. When nil, it will go to the end of the line before making a
  792. new line.
  793. You may also set this option in a different way for different
  794. contexts. Valid contexts are:
  795. headline when creating a new headline
  796. item when creating a new item
  797. table in a table field
  798. default the value to be used for all contexts not explicitly
  799. customized"
  800. :group 'org-structure
  801. :group 'org-table
  802. :type '(choice
  803. (const :tag "Always" t)
  804. (const :tag "Never" nil)
  805. (repeat :greedy t :tag "Individual contexts"
  806. (cons
  807. (choice :tag "Context"
  808. (const headline)
  809. (const item)
  810. (const table)
  811. (const default))
  812. (boolean)))))
  813. (defcustom org-insert-heading-respect-content nil
  814. "Non-nil means insert new headings after the current subtree.
  815. When nil, the new heading is created directly after the current line.
  816. The commands \\[org-insert-heading-respect-content] and
  817. \\[org-insert-todo-heading-respect-content] turn this variable on
  818. for the duration of the command."
  819. :group 'org-structure
  820. :type 'boolean)
  821. (defcustom org-blank-before-new-entry '((heading . auto)
  822. (plain-list-item . auto))
  823. "Should `org-insert-heading' leave a blank line before new heading/item?
  824. The value is an alist, with `heading' and `plain-list-item' as car,
  825. and a boolean flag as cdr. For plain lists, if the variable
  826. `org-empty-line-terminates-plain-lists' is set, the setting here
  827. is ignored and no empty line is inserted, to keep the list in tact."
  828. :group 'org-edit-structure
  829. :type '(list
  830. (cons (const heading)
  831. (choice (const :tag "Never" nil)
  832. (const :tag "Always" t)
  833. (const :tag "Auto" auto)))
  834. (cons (const plain-list-item)
  835. (choice (const :tag "Never" nil)
  836. (const :tag "Always" t)
  837. (const :tag "Auto" auto)))))
  838. (defcustom org-insert-heading-hook nil
  839. "Hook being run after inserting a new heading."
  840. :group 'org-edit-structure
  841. :type 'hook)
  842. (defcustom org-enable-fixed-width-editor t
  843. "Non-nil means lines starting with \":\" are treated as fixed-width.
  844. This currently only means they are never auto-wrapped.
  845. When nil, such lines will be treated like ordinary lines.
  846. See also the QUOTE keyword."
  847. :group 'org-edit-structure
  848. :type 'boolean)
  849. (defcustom org-goto-auto-isearch t
  850. "Non-nil means typing characters in org-goto starts incremental search."
  851. :group 'org-edit-structure
  852. :type 'boolean)
  853. (defgroup org-sparse-trees nil
  854. "Options concerning sparse trees in Org-mode."
  855. :tag "Org Sparse Trees"
  856. :group 'org-structure)
  857. (defcustom org-highlight-sparse-tree-matches t
  858. "Non-nil means highlight all matches that define a sparse tree.
  859. The highlights will automatically disappear the next time the buffer is
  860. changed by an edit command."
  861. :group 'org-sparse-trees
  862. :type 'boolean)
  863. (defcustom org-remove-highlights-with-change t
  864. "Non-nil means any change to the buffer will remove temporary highlights.
  865. Such highlights are created by `org-occur' and `org-clock-display'.
  866. When nil, `C-c C-c needs to be used to get rid of the highlights.
  867. The highlights created by `org-preview-latex-fragment' always need
  868. `C-c C-c' to be removed."
  869. :group 'org-sparse-trees
  870. :group 'org-time
  871. :type 'boolean)
  872. (defcustom org-occur-hook '(org-first-headline-recenter)
  873. "Hook that is run after `org-occur' has constructed a sparse tree.
  874. This can be used to recenter the window to show as much of the structure
  875. as possible."
  876. :group 'org-sparse-trees
  877. :type 'hook)
  878. (defgroup org-imenu-and-speedbar nil
  879. "Options concerning imenu and speedbar in Org-mode."
  880. :tag "Org Imenu and Speedbar"
  881. :group 'org-structure)
  882. (defcustom org-imenu-depth 2
  883. "The maximum level for Imenu access to Org-mode headlines.
  884. This also applied for speedbar access."
  885. :group 'org-imenu-and-speedbar
  886. :type 'integer)
  887. (defgroup org-table nil
  888. "Options concerning tables in Org-mode."
  889. :tag "Org Table"
  890. :group 'org)
  891. (defcustom org-enable-table-editor 'optimized
  892. "Non-nil means lines starting with \"|\" are handled by the table editor.
  893. When nil, such lines will be treated like ordinary lines.
  894. When equal to the symbol `optimized', the table editor will be optimized to
  895. do the following:
  896. - Automatic overwrite mode in front of whitespace in table fields.
  897. This makes the structure of the table stay in tact as long as the edited
  898. field does not exceed the column width.
  899. - Minimize the number of realigns. Normally, the table is aligned each time
  900. TAB or RET are pressed to move to another field. With optimization this
  901. happens only if changes to a field might have changed the column width.
  902. Optimization requires replacing the functions `self-insert-command',
  903. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  904. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  905. very good at guessing when a re-align will be necessary, but you can always
  906. force one with \\[org-ctrl-c-ctrl-c].
  907. If you would like to use the optimized version in Org-mode, but the
  908. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  909. This variable can be used to turn on and off the table editor during a session,
  910. but in order to toggle optimization, a restart is required.
  911. See also the variable `org-table-auto-blank-field'."
  912. :group 'org-table
  913. :type '(choice
  914. (const :tag "off" nil)
  915. (const :tag "on" t)
  916. (const :tag "on, optimized" optimized)))
  917. (defcustom org-self-insert-cluster-for-undo t
  918. "Non-nil means cluster self-insert commands for undo when possible.
  919. If this is set, then, like in the Emacs command loop, 20 consecutive
  920. characters will be undone together.
  921. This is configurable, because there is some impact on typing performance."
  922. :group 'org-table
  923. :type 'boolean)
  924. (defcustom org-table-tab-recognizes-table.el t
  925. "Non-nil means TAB will automatically notice a table.el table.
  926. When it sees such a table, it moves point into it and - if necessary -
  927. calls `table-recognize-table'."
  928. :group 'org-table-editing
  929. :type 'boolean)
  930. (defgroup org-link nil
  931. "Options concerning links in Org-mode."
  932. :tag "Org Link"
  933. :group 'org)
  934. (defvar org-link-abbrev-alist-local nil
  935. "Buffer-local version of `org-link-abbrev-alist', which see.
  936. The value of this is taken from the #+LINK lines.")
  937. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  938. (defcustom org-link-abbrev-alist nil
  939. "Alist of link abbreviations.
  940. The car of each element is a string, to be replaced at the start of a link.
  941. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  942. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  943. [[linkkey:tag][description]]
  944. The 'linkkey' must be a word word, starting with a letter, followed
  945. by letters, numbers, '-' or '_'.
  946. If REPLACE is a string, the tag will simply be appended to create the link.
  947. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  948. the placeholder \"%h\" will cause a url-encoded version of the tag to
  949. be inserted at that point (see the function `url-hexify-string').
  950. REPLACE may also be a function that will be called with the tag as the
  951. only argument to create the link, which should be returned as a string.
  952. See the manual for examples."
  953. :group 'org-link
  954. :type '(repeat
  955. (cons
  956. (string :tag "Protocol")
  957. (choice
  958. (string :tag "Format")
  959. (function)))))
  960. (defcustom org-descriptive-links t
  961. "Non-nil means hide link part and only show description of bracket links.
  962. Bracket links are like [[link][description]]. This variable sets the initial
  963. state in new org-mode buffers. The setting can then be toggled on a
  964. per-buffer basis from the Org->Hyperlinks menu."
  965. :group 'org-link
  966. :type 'boolean)
  967. (defcustom org-link-file-path-type 'adaptive
  968. "How the path name in file links should be stored.
  969. Valid values are:
  970. relative Relative to the current directory, i.e. the directory of the file
  971. into which the link is being inserted.
  972. absolute Absolute path, if possible with ~ for home directory.
  973. noabbrev Absolute path, no abbreviation of home directory.
  974. adaptive Use relative path for files in the current directory and sub-
  975. directories of it. For other files, use an absolute path."
  976. :group 'org-link
  977. :type '(choice
  978. (const relative)
  979. (const absolute)
  980. (const noabbrev)
  981. (const adaptive)))
  982. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  983. "Types of links that should be activated in Org-mode files.
  984. This is a list of symbols, each leading to the activation of a certain link
  985. type. In principle, it does not hurt to turn on most link types - there may
  986. be a small gain when turning off unused link types. The types are:
  987. bracket The recommended [[link][description]] or [[link]] links with hiding.
  988. angular Links in angular brackets that may contain whitespace like
  989. <bbdb:Carsten Dominik>.
  990. plain Plain links in normal text, no whitespace, like http://google.com.
  991. radio Text that is matched by a radio target, see manual for details.
  992. tag Tag settings in a headline (link to tag search).
  993. date Time stamps (link to calendar).
  994. footnote Footnote labels.
  995. Changing this variable requires a restart of Emacs to become effective."
  996. :group 'org-link
  997. :type '(set :greedy t
  998. (const :tag "Double bracket links (new style)" bracket)
  999. (const :tag "Angular bracket links (old style)" angular)
  1000. (const :tag "Plain text links" plain)
  1001. (const :tag "Radio target matches" radio)
  1002. (const :tag "Tags" tag)
  1003. (const :tag "Timestamps" date)
  1004. (const :tag "Footnotes" footnote)))
  1005. (defcustom org-make-link-description-function nil
  1006. "Function to use to generate link descriptions from links. If
  1007. nil the link location will be used. This function must take two
  1008. parameters; the first is the link and the second the description
  1009. org-insert-link has generated, and should return the description
  1010. to use."
  1011. :group 'org-link
  1012. :type 'function)
  1013. (defgroup org-link-store nil
  1014. "Options concerning storing links in Org-mode."
  1015. :tag "Org Store Link"
  1016. :group 'org-link)
  1017. (defcustom org-email-link-description-format "Email %c: %.30s"
  1018. "Format of the description part of a link to an email or usenet message.
  1019. The following %-escapes will be replaced by corresponding information:
  1020. %F full \"From\" field
  1021. %f name, taken from \"From\" field, address if no name
  1022. %T full \"To\" field
  1023. %t first name in \"To\" field, address if no name
  1024. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1025. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1026. %s subject
  1027. %m message-id.
  1028. You may use normal field width specification between the % and the letter.
  1029. This is for example useful to limit the length of the subject.
  1030. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1031. :group 'org-link-store
  1032. :type 'string)
  1033. (defcustom org-from-is-user-regexp
  1034. (let (r1 r2)
  1035. (when (and user-mail-address (not (string= user-mail-address "")))
  1036. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1037. (when (and user-full-name (not (string= user-full-name "")))
  1038. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1039. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1040. "Regexp matched against the \"From:\" header of an email or usenet message.
  1041. It should match if the message is from the user him/herself."
  1042. :group 'org-link-store
  1043. :type 'regexp)
  1044. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1045. "Non-nil means storing a link to an Org file will use entry IDs.
  1046. Note that before this variable is even considered, org-id must be loaded,
  1047. so please customize `org-modules' and turn it on.
  1048. The variable can have the following values:
  1049. t Create an ID if needed to make a link to the current entry.
  1050. create-if-interactive
  1051. If `org-store-link' is called directly (interactively, as a user
  1052. command), do create an ID to support the link. But when doing the
  1053. job for remember, only use the ID if it already exists. The
  1054. purpose of this setting is to avoid proliferation of unwanted
  1055. IDs, just because you happen to be in an Org file when you
  1056. call `org-remember' that automatically and preemptively
  1057. creates a link. If you do want to get an ID link in a remember
  1058. template to an entry not having an ID, create it first by
  1059. explicitly creating a link to it, using `C-c C-l' first.
  1060. create-if-interactive-and-no-custom-id
  1061. Like create-if-interactive, but do not create an ID if there is
  1062. a CUSTOM_ID property defined in the entry. This is the default.
  1063. use-existing
  1064. Use existing ID, do not create one.
  1065. nil Never use an ID to make a link, instead link using a text search for
  1066. the headline text."
  1067. :group 'org-link-store
  1068. :type '(choice
  1069. (const :tag "Create ID to make link" t)
  1070. (const :tag "Create if storing link interactively"
  1071. create-if-interactive)
  1072. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1073. create-if-interactive-and-no-custom-id)
  1074. (const :tag "Only use existing" use-existing)
  1075. (const :tag "Do not use ID to create link" nil)))
  1076. (defcustom org-context-in-file-links t
  1077. "Non-nil means file links from `org-store-link' contain context.
  1078. A search string will be added to the file name with :: as separator and
  1079. used to find the context when the link is activated by the command
  1080. `org-open-at-point'.
  1081. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1082. negates this setting for the duration of the command."
  1083. :group 'org-link-store
  1084. :type 'boolean)
  1085. (defcustom org-keep-stored-link-after-insertion nil
  1086. "Non-nil means keep link in list for entire session.
  1087. The command `org-store-link' adds a link pointing to the current
  1088. location to an internal list. These links accumulate during a session.
  1089. The command `org-insert-link' can be used to insert links into any
  1090. Org-mode file (offering completion for all stored links). When this
  1091. option is nil, every link which has been inserted once using \\[org-insert-link]
  1092. will be removed from the list, to make completing the unused links
  1093. more efficient."
  1094. :group 'org-link-store
  1095. :type 'boolean)
  1096. (defgroup org-link-follow nil
  1097. "Options concerning following links in Org-mode."
  1098. :tag "Org Follow Link"
  1099. :group 'org-link)
  1100. (defcustom org-link-translation-function nil
  1101. "Function to translate links with different syntax to Org syntax.
  1102. This can be used to translate links created for example by the Planner
  1103. or emacs-wiki packages to Org syntax.
  1104. The function must accept two parameters, a TYPE containing the link
  1105. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1106. which is everything after the link protocol. It should return a cons
  1107. with possibly modified values of type and path.
  1108. Org contains a function for this, so if you set this variable to
  1109. `org-translate-link-from-planner', you should be able follow many
  1110. links created by planner."
  1111. :group 'org-link-follow
  1112. :type 'function)
  1113. (defcustom org-follow-link-hook nil
  1114. "Hook that is run after a link has been followed."
  1115. :group 'org-link-follow
  1116. :type 'hook)
  1117. (defcustom org-tab-follows-link nil
  1118. "Non-nil means on links TAB will follow the link.
  1119. Needs to be set before org.el is loaded.
  1120. This really should not be used, it does not make sense, and the
  1121. implementation is bad."
  1122. :group 'org-link-follow
  1123. :type 'boolean)
  1124. (defcustom org-return-follows-link nil
  1125. "Non-nil means on links RET will follow the link."
  1126. :group 'org-link-follow
  1127. :type 'boolean)
  1128. (defcustom org-mouse-1-follows-link
  1129. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1130. "Non-nil means mouse-1 on a link will follow the link.
  1131. A longer mouse click will still set point. Does not work on XEmacs.
  1132. Needs to be set before org.el is loaded."
  1133. :group 'org-link-follow
  1134. :type 'boolean)
  1135. (defcustom org-mark-ring-length 4
  1136. "Number of different positions to be recorded in the ring
  1137. Changing this requires a restart of Emacs to work correctly."
  1138. :group 'org-link-follow
  1139. :type 'integer)
  1140. (defcustom org-link-frame-setup
  1141. '((vm . vm-visit-folder-other-frame)
  1142. (gnus . gnus-other-frame)
  1143. (file . find-file-other-window))
  1144. "Setup the frame configuration for following links.
  1145. When following a link with Emacs, it may often be useful to display
  1146. this link in another window or frame. This variable can be used to
  1147. set this up for the different types of links.
  1148. For VM, use any of
  1149. `vm-visit-folder'
  1150. `vm-visit-folder-other-frame'
  1151. For Gnus, use any of
  1152. `gnus'
  1153. `gnus-other-frame'
  1154. `org-gnus-no-new-news'
  1155. For FILE, use any of
  1156. `find-file'
  1157. `find-file-other-window'
  1158. `find-file-other-frame'
  1159. For the calendar, use the variable `calendar-setup'.
  1160. For BBDB, it is currently only possible to display the matches in
  1161. another window."
  1162. :group 'org-link-follow
  1163. :type '(list
  1164. (cons (const vm)
  1165. (choice
  1166. (const vm-visit-folder)
  1167. (const vm-visit-folder-other-window)
  1168. (const vm-visit-folder-other-frame)))
  1169. (cons (const gnus)
  1170. (choice
  1171. (const gnus)
  1172. (const gnus-other-frame)
  1173. (const org-gnus-no-new-news)))
  1174. (cons (const file)
  1175. (choice
  1176. (const find-file)
  1177. (const find-file-other-window)
  1178. (const find-file-other-frame)))))
  1179. (defcustom org-display-internal-link-with-indirect-buffer nil
  1180. "Non-nil means use indirect buffer to display infile links.
  1181. Activating internal links (from one location in a file to another location
  1182. in the same file) normally just jumps to the location. When the link is
  1183. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1184. another window. When this option is set, the other window actually displays
  1185. an indirect buffer clone of the current buffer, to avoid any visibility
  1186. changes to the current buffer."
  1187. :group 'org-link-follow
  1188. :type 'boolean)
  1189. (defcustom org-open-non-existing-files nil
  1190. "Non-nil means `org-open-file' will open non-existing files.
  1191. When nil, an error will be generated.
  1192. This variable applies only to external applications because they
  1193. might choke on non-existing files. If the link is to a file that
  1194. will be opened in Emacs, the variable is ignored."
  1195. :group 'org-link-follow
  1196. :type 'boolean)
  1197. (defcustom org-open-directory-means-index-dot-org nil
  1198. "Non-nil means a link to a directory really means to index.org.
  1199. When nil, following a directory link will run dired or open a finder/explorer
  1200. window on that directory."
  1201. :group 'org-link-follow
  1202. :type 'boolean)
  1203. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1204. "Function and arguments to call for following mailto links.
  1205. This is a list with the first element being a lisp function, and the
  1206. remaining elements being arguments to the function. In string arguments,
  1207. %a will be replaced by the address, and %s will be replaced by the subject
  1208. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1209. :group 'org-link-follow
  1210. :type '(choice
  1211. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1212. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1213. (const :tag "message-mail" (message-mail "%a" "%s"))
  1214. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1215. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1216. "Non-nil means ask for confirmation before executing shell links.
  1217. Shell links can be dangerous: just think about a link
  1218. [[shell:rm -rf ~/*][Google Search]]
  1219. This link would show up in your Org-mode document as \"Google Search\",
  1220. but really it would remove your entire home directory.
  1221. Therefore we advise against setting this variable to nil.
  1222. Just change it to `y-or-n-p' if you want to confirm with a
  1223. single keystroke rather than having to type \"yes\"."
  1224. :group 'org-link-follow
  1225. :type '(choice
  1226. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1227. (const :tag "with y-or-n (faster)" y-or-n-p)
  1228. (const :tag "no confirmation (dangerous)" nil)))
  1229. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1230. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1231. Elisp links can be dangerous: just think about a link
  1232. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1233. This link would show up in your Org-mode document as \"Google Search\",
  1234. but really it would remove your entire home directory.
  1235. Therefore we advise against setting this variable to nil.
  1236. Just change it to `y-or-n-p' if you want to confirm with a
  1237. single keystroke rather than having to type \"yes\"."
  1238. :group 'org-link-follow
  1239. :type '(choice
  1240. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1241. (const :tag "with y-or-n (faster)" y-or-n-p)
  1242. (const :tag "no confirmation (dangerous)" nil)))
  1243. (defconst org-file-apps-defaults-gnu
  1244. '((remote . emacs)
  1245. (system . mailcap)
  1246. (t . mailcap))
  1247. "Default file applications on a UNIX or GNU/Linux system.
  1248. See `org-file-apps'.")
  1249. (defconst org-file-apps-defaults-macosx
  1250. '((remote . emacs)
  1251. (t . "open %s")
  1252. (system . "open %s")
  1253. ("ps.gz" . "gv %s")
  1254. ("eps.gz" . "gv %s")
  1255. ("dvi" . "xdvi %s")
  1256. ("fig" . "xfig %s"))
  1257. "Default file applications on a MacOS X system.
  1258. The system \"open\" is known as a default, but we use X11 applications
  1259. for some files for which the OS does not have a good default.
  1260. See `org-file-apps'.")
  1261. (defconst org-file-apps-defaults-windowsnt
  1262. (list
  1263. '(remote . emacs)
  1264. (cons t
  1265. (list (if (featurep 'xemacs)
  1266. 'mswindows-shell-execute
  1267. 'w32-shell-execute)
  1268. "open" 'file))
  1269. (cons 'system
  1270. (list (if (featurep 'xemacs)
  1271. 'mswindows-shell-execute
  1272. 'w32-shell-execute)
  1273. "open" 'file)))
  1274. "Default file applications on a Windows NT system.
  1275. The system \"open\" is used for most files.
  1276. See `org-file-apps'.")
  1277. (defcustom org-file-apps
  1278. '(
  1279. (auto-mode . emacs)
  1280. ("\\.mm\\'" . default)
  1281. ("\\.x?html?\\'" . default)
  1282. ("\\.pdf\\'" . default)
  1283. )
  1284. "External applications for opening `file:path' items in a document.
  1285. Org-mode uses system defaults for different file types, but
  1286. you can use this variable to set the application for a given file
  1287. extension. The entries in this list are cons cells where the car identifies
  1288. files and the cdr the corresponding command. Possible values for the
  1289. file identifier are
  1290. \"regex\" Regular expression matched against the file name. For backward
  1291. compatibility, this can also be a string with only alphanumeric
  1292. characters, which is then interpreted as an extension.
  1293. `directory' Matches a directory
  1294. `remote' Matches a remote file, accessible through tramp or efs.
  1295. Remote files most likely should be visited through Emacs
  1296. because external applications cannot handle such paths.
  1297. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1298. so all files Emacs knows how to handle. Using this with
  1299. command `emacs' will open most files in Emacs. Beware that this
  1300. will also open html files inside Emacs, unless you add
  1301. (\"html\" . default) to the list as well.
  1302. t Default for files not matched by any of the other options.
  1303. `system' The system command to open files, like `open' on Windows
  1304. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1305. that will be selected if you call `C-c C-o' with a double
  1306. `C-u C-u' prefix.
  1307. Possible values for the command are:
  1308. `emacs' The file will be visited by the current Emacs process.
  1309. `default' Use the default application for this file type, which is the
  1310. association for t in the list, most likely in the system-specific
  1311. part.
  1312. This can be used to overrule an unwanted setting in the
  1313. system-specific variable.
  1314. `system' Use the system command for opening files, like \"open\".
  1315. This command is specified by the entry whose car is `system'.
  1316. Most likely, the system-specific version of this variable
  1317. does define this command, but you can overrule/replace it
  1318. here.
  1319. string A command to be executed by a shell; %s will be replaced
  1320. by the path to the file.
  1321. sexp A Lisp form which will be evaluated. The file path will
  1322. be available in the Lisp variable `file'.
  1323. For more examples, see the system specific constants
  1324. `org-file-apps-defaults-macosx'
  1325. `org-file-apps-defaults-windowsnt'
  1326. `org-file-apps-defaults-gnu'."
  1327. :group 'org-link-follow
  1328. :type '(repeat
  1329. (cons (choice :value ""
  1330. (string :tag "Extension")
  1331. (const :tag "System command to open files" system)
  1332. (const :tag "Default for unrecognized files" t)
  1333. (const :tag "Remote file" remote)
  1334. (const :tag "Links to a directory" directory)
  1335. (const :tag "Any files that have Emacs modes"
  1336. auto-mode))
  1337. (choice :value ""
  1338. (const :tag "Visit with Emacs" emacs)
  1339. (const :tag "Use default" default)
  1340. (const :tag "Use the system command" system)
  1341. (string :tag "Command")
  1342. (sexp :tag "Lisp form")))))
  1343. (defgroup org-refile nil
  1344. "Options concerning refiling entries in Org-mode."
  1345. :tag "Org Refile"
  1346. :group 'org)
  1347. (defcustom org-directory "~/org"
  1348. "Directory with org files.
  1349. This is just a default location to look for Org files. There is no need
  1350. at all to put your files into this directory. It is only used in the
  1351. following situations:
  1352. 1. When a remember template specifies a target file that is not an
  1353. absolute path. The path will then be interpreted relative to
  1354. `org-directory'
  1355. 2. When a remember note is filed away in an interactive way (when exiting the
  1356. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1357. with `org-directory' as the default path."
  1358. :group 'org-refile
  1359. :group 'org-remember
  1360. :type 'directory)
  1361. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1362. "Default target for storing notes.
  1363. Used by the hooks for remember.el. This can be a string, or nil to mean
  1364. the value of `remember-data-file'.
  1365. You can set this on a per-template basis with the variable
  1366. `org-remember-templates'."
  1367. :group 'org-refile
  1368. :group 'org-remember
  1369. :type '(choice
  1370. (const :tag "Default from remember-data-file" nil)
  1371. file))
  1372. (defcustom org-goto-interface 'outline
  1373. "The default interface to be used for `org-goto'.
  1374. Allowed values are:
  1375. outline The interface shows an outline of the relevant file
  1376. and the correct heading is found by moving through
  1377. the outline or by searching with incremental search.
  1378. outline-path-completion Headlines in the current buffer are offered via
  1379. completion. This is the interface also used by
  1380. the refile command."
  1381. :group 'org-refile
  1382. :type '(choice
  1383. (const :tag "Outline" outline)
  1384. (const :tag "Outline-path-completion" outline-path-completion)))
  1385. (defcustom org-goto-max-level 5
  1386. "Maximum level to be considered when running org-goto with refile interface."
  1387. :group 'org-refile
  1388. :type 'integer)
  1389. (defcustom org-reverse-note-order nil
  1390. "Non-nil means store new notes at the beginning of a file or entry.
  1391. When nil, new notes will be filed to the end of a file or entry.
  1392. This can also be a list with cons cells of regular expressions that
  1393. are matched against file names, and values."
  1394. :group 'org-remember
  1395. :group 'org-refile
  1396. :type '(choice
  1397. (const :tag "Reverse always" t)
  1398. (const :tag "Reverse never" nil)
  1399. (repeat :tag "By file name regexp"
  1400. (cons regexp boolean))))
  1401. (defcustom org-log-refile nil
  1402. "Information to record when a task is refiled.
  1403. Possible values are:
  1404. nil Don't add anything
  1405. time Add a time stamp to the task
  1406. note Prompt for a note and add it with template `org-log-note-headings'
  1407. This option can also be set with on a per-file-basis with
  1408. #+STARTUP: nologrefile
  1409. #+STARTUP: logrefile
  1410. #+STARTUP: lognoterefile
  1411. You can have local logging settings for a subtree by setting the LOGGING
  1412. property to one or more of these keywords.
  1413. When bulk-refiling from the agenda, the value `note' is forbidden and
  1414. will temporarily be changed to `time'."
  1415. :group 'org-refile
  1416. :group 'org-progress
  1417. :type '(choice
  1418. (const :tag "No logging" nil)
  1419. (const :tag "Record timestamp" time)
  1420. (const :tag "Record timestamp with note." note)))
  1421. (defcustom org-refile-targets nil
  1422. "Targets for refiling entries with \\[org-refile].
  1423. This is list of cons cells. Each cell contains:
  1424. - a specification of the files to be considered, either a list of files,
  1425. or a symbol whose function or variable value will be used to retrieve
  1426. a file name or a list of file names. If you use `org-agenda-files' for
  1427. that, all agenda files will be scanned for targets. Nil means consider
  1428. headings in the current buffer.
  1429. - A specification of how to find candidate refile targets. This may be
  1430. any of:
  1431. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1432. This tag has to be present in all target headlines, inheritance will
  1433. not be considered.
  1434. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1435. todo keyword.
  1436. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1437. headlines that are refiling targets.
  1438. - a cons cell (:level . N). Any headline of level N is considered a target.
  1439. Note that, when `org-odd-levels-only' is set, level corresponds to
  1440. order in hierarchy, not to the number of stars.
  1441. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1442. Note that, when `org-odd-levels-only' is set, level corresponds to
  1443. order in hierarchy, not to the number of stars.
  1444. You can set the variable `org-refile-target-verify-function' to a function
  1445. to verify each headline found by the simple critery above.
  1446. When this variable is nil, all top-level headlines in the current buffer
  1447. are used, equivalent to the value `((nil . (:level . 1))'."
  1448. :group 'org-refile
  1449. :type '(repeat
  1450. (cons
  1451. (choice :value org-agenda-files
  1452. (const :tag "All agenda files" org-agenda-files)
  1453. (const :tag "Current buffer" nil)
  1454. (function) (variable) (file))
  1455. (choice :tag "Identify target headline by"
  1456. (cons :tag "Specific tag" (const :value :tag) (string))
  1457. (cons :tag "TODO keyword" (const :value :todo) (string))
  1458. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1459. (cons :tag "Level number" (const :value :level) (integer))
  1460. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1461. (defcustom org-refile-target-verify-function nil
  1462. "Function to verify if the headline at point should be a refile target.
  1463. The function will be called without arguments, with point at the
  1464. beginning of the headline. It should return t and leave point
  1465. where it is if the headline is a valid target for refiling.
  1466. If the target should not be selected, the function must return nil.
  1467. In addition to this, it may move point to a place from where the search
  1468. should be continued. For example, the function may decide that the entire
  1469. subtree of the current entry should be excluded and move point to the end
  1470. of the subtree."
  1471. :group 'org-refile
  1472. :type 'function)
  1473. (defcustom org-refile-use-outline-path nil
  1474. "Non-nil means provide refile targets as paths.
  1475. So a level 3 headline will be available as level1/level2/level3.
  1476. When the value is `file', also include the file name (without directory)
  1477. into the path. In this case, you can also stop the completion after
  1478. the file name, to get entries inserted as top level in the file.
  1479. When `full-file-path', include the full file path."
  1480. :group 'org-refile
  1481. :type '(choice
  1482. (const :tag "Not" nil)
  1483. (const :tag "Yes" t)
  1484. (const :tag "Start with file name" file)
  1485. (const :tag "Start with full file path" full-file-path)))
  1486. (defcustom org-outline-path-complete-in-steps t
  1487. "Non-nil means complete the outline path in hierarchical steps.
  1488. When Org-mode uses the refile interface to select an outline path
  1489. \(see variable `org-refile-use-outline-path'), the completion of
  1490. the path can be done is a single go, or if can be done in steps down
  1491. the headline hierarchy. Going in steps is probably the best if you
  1492. do not use a special completion package like `ido' or `icicles'.
  1493. However, when using these packages, going in one step can be very
  1494. fast, while still showing the whole path to the entry."
  1495. :group 'org-refile
  1496. :type 'boolean)
  1497. (defcustom org-refile-allow-creating-parent-nodes nil
  1498. "Non-nil means allow to create new nodes as refile targets.
  1499. New nodes are then created by adding \"/new node name\" to the completion
  1500. of an existing node. When the value of this variable is `confirm',
  1501. new node creation must be confirmed by the user (recommended)
  1502. When nil, the completion must match an existing entry.
  1503. Note that, if the new heading is not seen by the criteria
  1504. listed in `org-refile-targets', multiple instances of the same
  1505. heading would be created by trying again to file under the new
  1506. heading."
  1507. :group 'org-refile
  1508. :type '(choice
  1509. (const :tag "Never" nil)
  1510. (const :tag "Always" t)
  1511. (const :tag "Prompt for confirmation" confirm)))
  1512. (defgroup org-todo nil
  1513. "Options concerning TODO items in Org-mode."
  1514. :tag "Org TODO"
  1515. :group 'org)
  1516. (defgroup org-progress nil
  1517. "Options concerning Progress logging in Org-mode."
  1518. :tag "Org Progress"
  1519. :group 'org-time)
  1520. (defvar org-todo-interpretation-widgets
  1521. '(
  1522. (:tag "Sequence (cycling hits every state)" sequence)
  1523. (:tag "Type (cycling directly to DONE)" type))
  1524. "The available interpretation symbols for customizing
  1525. `org-todo-keywords'.
  1526. Interested libraries should add to this list.")
  1527. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1528. "List of TODO entry keyword sequences and their interpretation.
  1529. \\<org-mode-map>This is a list of sequences.
  1530. Each sequence starts with a symbol, either `sequence' or `type',
  1531. indicating if the keywords should be interpreted as a sequence of
  1532. action steps, or as different types of TODO items. The first
  1533. keywords are states requiring action - these states will select a headline
  1534. for inclusion into the global TODO list Org-mode produces. If one of
  1535. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1536. signify that no further action is necessary. If \"|\" is not found,
  1537. the last keyword is treated as the only DONE state of the sequence.
  1538. The command \\[org-todo] cycles an entry through these states, and one
  1539. additional state where no keyword is present. For details about this
  1540. cycling, see the manual.
  1541. TODO keywords and interpretation can also be set on a per-file basis with
  1542. the special #+SEQ_TODO and #+TYP_TODO lines.
  1543. Each keyword can optionally specify a character for fast state selection
  1544. \(in combination with the variable `org-use-fast-todo-selection')
  1545. and specifiers for state change logging, using the same syntax
  1546. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1547. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1548. indicates to record a time stamp each time this state is selected.
  1549. Each keyword may also specify if a timestamp or a note should be
  1550. recorded when entering or leaving the state, by adding additional
  1551. characters in the parenthesis after the keyword. This looks like this:
  1552. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1553. record only the time of the state change. With X and Y being either
  1554. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1555. Y when leaving the state if and only if the *target* state does not
  1556. define X. You may omit any of the fast-selection key or X or /Y,
  1557. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1558. For backward compatibility, this variable may also be just a list
  1559. of keywords - in this case the interpretation (sequence or type) will be
  1560. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1561. :group 'org-todo
  1562. :group 'org-keywords
  1563. :type '(choice
  1564. (repeat :tag "Old syntax, just keywords"
  1565. (string :tag "Keyword"))
  1566. (repeat :tag "New syntax"
  1567. (cons
  1568. (choice
  1569. :tag "Interpretation"
  1570. ;;Quick and dirty way to see
  1571. ;;`org-todo-interpretations'. This takes the
  1572. ;;place of item arguments
  1573. :convert-widget
  1574. (lambda (widget)
  1575. (widget-put widget
  1576. :args (mapcar
  1577. #'(lambda (x)
  1578. (widget-convert
  1579. (cons 'const x)))
  1580. org-todo-interpretation-widgets))
  1581. widget))
  1582. (repeat
  1583. (string :tag "Keyword"))))))
  1584. (defvar org-todo-keywords-1 nil
  1585. "All TODO and DONE keywords active in a buffer.")
  1586. (make-variable-buffer-local 'org-todo-keywords-1)
  1587. (defvar org-todo-keywords-for-agenda nil)
  1588. (defvar org-done-keywords-for-agenda nil)
  1589. (defvar org-drawers-for-agenda nil)
  1590. (defvar org-todo-keyword-alist-for-agenda nil)
  1591. (defvar org-tag-alist-for-agenda nil)
  1592. (defvar org-agenda-contributing-files nil)
  1593. (defvar org-not-done-keywords nil)
  1594. (make-variable-buffer-local 'org-not-done-keywords)
  1595. (defvar org-done-keywords nil)
  1596. (make-variable-buffer-local 'org-done-keywords)
  1597. (defvar org-todo-heads nil)
  1598. (make-variable-buffer-local 'org-todo-heads)
  1599. (defvar org-todo-sets nil)
  1600. (make-variable-buffer-local 'org-todo-sets)
  1601. (defvar org-todo-log-states nil)
  1602. (make-variable-buffer-local 'org-todo-log-states)
  1603. (defvar org-todo-kwd-alist nil)
  1604. (make-variable-buffer-local 'org-todo-kwd-alist)
  1605. (defvar org-todo-key-alist nil)
  1606. (make-variable-buffer-local 'org-todo-key-alist)
  1607. (defvar org-todo-key-trigger nil)
  1608. (make-variable-buffer-local 'org-todo-key-trigger)
  1609. (defcustom org-todo-interpretation 'sequence
  1610. "Controls how TODO keywords are interpreted.
  1611. This variable is in principle obsolete and is only used for
  1612. backward compatibility, if the interpretation of todo keywords is
  1613. not given already in `org-todo-keywords'. See that variable for
  1614. more information."
  1615. :group 'org-todo
  1616. :group 'org-keywords
  1617. :type '(choice (const sequence)
  1618. (const type)))
  1619. (defcustom org-use-fast-todo-selection t
  1620. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1621. This variable describes if and under what circumstances the cycling
  1622. mechanism for TODO keywords will be replaced by a single-key, direct
  1623. selection scheme.
  1624. When nil, fast selection is never used.
  1625. When the symbol `prefix', it will be used when `org-todo' is called with
  1626. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1627. in an agenda buffer.
  1628. When t, fast selection is used by default. In this case, the prefix
  1629. argument forces cycling instead.
  1630. In all cases, the special interface is only used if access keys have actually
  1631. been assigned by the user, i.e. if keywords in the configuration are followed
  1632. by a letter in parenthesis, like TODO(t)."
  1633. :group 'org-todo
  1634. :type '(choice
  1635. (const :tag "Never" nil)
  1636. (const :tag "By default" t)
  1637. (const :tag "Only with C-u C-c C-t" prefix)))
  1638. (defcustom org-provide-todo-statistics t
  1639. "Non-nil means update todo statistics after insert and toggle.
  1640. ALL-HEADLINES means update todo statistics by including headlines
  1641. with no TODO keyword as well, counting them as not done.
  1642. A list of TODO keywords means the same, but skip keywords that are
  1643. not in this list.
  1644. When this is set, todo statistics is updated in the parent of the
  1645. current entry each time a todo state is changed."
  1646. :group 'org-todo
  1647. :type '(choice
  1648. (const :tag "Yes, only for TODO entries" t)
  1649. (const :tag "Yes, including all entries" 'all-headlines)
  1650. (repeat :tag "Yes, for TODOs in this list"
  1651. (string :tag "TODO keyword"))
  1652. (other :tag "No TODO statistics" nil)))
  1653. (defcustom org-hierarchical-todo-statistics t
  1654. "Non-nil means TODO statistics covers just direct children.
  1655. When nil, all entries in the subtree are considered.
  1656. This has only an effect if `org-provide-todo-statistics' is set.
  1657. To set this to nil for only a single subtree, use a COOKIE_DATA
  1658. property and include the word \"recursive\" into the value."
  1659. :group 'org-todo
  1660. :type 'boolean)
  1661. (defcustom org-after-todo-state-change-hook nil
  1662. "Hook which is run after the state of a TODO item was changed.
  1663. The new state (a string with a TODO keyword, or nil) is available in the
  1664. Lisp variable `state'."
  1665. :group 'org-todo
  1666. :type 'hook)
  1667. (defvar org-blocker-hook nil
  1668. "Hook for functions that are allowed to block a state change.
  1669. Each function gets as its single argument a property list, see
  1670. `org-trigger-hook' for more information about this list.
  1671. If any of the functions in this hook returns nil, the state change
  1672. is blocked.")
  1673. (defvar org-trigger-hook nil
  1674. "Hook for functions that are triggered by a state change.
  1675. Each function gets as its single argument a property list with at least
  1676. the following elements:
  1677. (:type type-of-change :position pos-at-entry-start
  1678. :from old-state :to new-state)
  1679. Depending on the type, more properties may be present.
  1680. This mechanism is currently implemented for:
  1681. TODO state changes
  1682. ------------------
  1683. :type todo-state-change
  1684. :from previous state (keyword as a string), or nil, or a symbol
  1685. 'todo' or 'done', to indicate the general type of state.
  1686. :to new state, like in :from")
  1687. (defcustom org-enforce-todo-dependencies nil
  1688. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1689. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1690. be blocked if any prior sibling is not yet done.
  1691. Finally, if the parent is blocked because of ordered siblings of its own,
  1692. the child will also be blocked.
  1693. This variable needs to be set before org.el is loaded, and you need to
  1694. restart Emacs after a change to make the change effective. The only way
  1695. to change is while Emacs is running is through the customize interface."
  1696. :set (lambda (var val)
  1697. (set var val)
  1698. (if val
  1699. (add-hook 'org-blocker-hook
  1700. 'org-block-todo-from-children-or-siblings-or-parent)
  1701. (remove-hook 'org-blocker-hook
  1702. 'org-block-todo-from-children-or-siblings-or-parent)))
  1703. :group 'org-todo
  1704. :type 'boolean)
  1705. (defcustom org-enforce-todo-checkbox-dependencies nil
  1706. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1707. When this is nil, checkboxes have no influence on switching TODO states.
  1708. When non-nil, you first need to check off all check boxes before the TODO
  1709. entry can be switched to DONE.
  1710. This variable needs to be set before org.el is loaded, and you need to
  1711. restart Emacs after a change to make the change effective. The only way
  1712. to change is while Emacs is running is through the customize interface."
  1713. :set (lambda (var val)
  1714. (set var val)
  1715. (if val
  1716. (add-hook 'org-blocker-hook
  1717. 'org-block-todo-from-checkboxes)
  1718. (remove-hook 'org-blocker-hook
  1719. 'org-block-todo-from-checkboxes)))
  1720. :group 'org-todo
  1721. :type 'boolean)
  1722. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1723. "Non-nil means inserting a TODO heading is treated as state change.
  1724. So when the command \\[org-insert-todo-heading] is used, state change
  1725. logging will apply if appropriate. When nil, the new TODO item will
  1726. be inserted directly, and no logging will take place."
  1727. :group 'org-todo
  1728. :type 'boolean)
  1729. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1730. "Non-nil means switching TODO states with S-cursor counts as state change.
  1731. This is the default behavior. However, setting this to nil allows a
  1732. convenient way to select a TODO state and bypass any logging associated
  1733. with that."
  1734. :group 'org-todo
  1735. :type 'boolean)
  1736. (defcustom org-todo-state-tags-triggers nil
  1737. "Tag changes that should be triggered by TODO state changes.
  1738. This is a list. Each entry is
  1739. (state-change (tag . flag) .......)
  1740. State-change can be a string with a state, and empty string to indicate the
  1741. state that has no TODO keyword, or it can be one of the symbols `todo'
  1742. or `done', meaning any not-done or done state, respectively."
  1743. :group 'org-todo
  1744. :group 'org-tags
  1745. :type '(repeat
  1746. (cons (choice :tag "When changing to"
  1747. (const :tag "Not-done state" todo)
  1748. (const :tag "Done state" done)
  1749. (string :tag "State"))
  1750. (repeat
  1751. (cons :tag "Tag action"
  1752. (string :tag "Tag")
  1753. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1754. (defcustom org-log-done nil
  1755. "Information to record when a task moves to the DONE state.
  1756. Possible values are:
  1757. nil Don't add anything, just change the keyword
  1758. time Add a time stamp to the task
  1759. note Prompt for a note and add it with template `org-log-note-headings'
  1760. This option can also be set with on a per-file-basis with
  1761. #+STARTUP: nologdone
  1762. #+STARTUP: logdone
  1763. #+STARTUP: lognotedone
  1764. You can have local logging settings for a subtree by setting the LOGGING
  1765. property to one or more of these keywords."
  1766. :group 'org-todo
  1767. :group 'org-progress
  1768. :type '(choice
  1769. (const :tag "No logging" nil)
  1770. (const :tag "Record CLOSED timestamp" time)
  1771. (const :tag "Record CLOSED timestamp with note." note)))
  1772. ;; Normalize old uses of org-log-done.
  1773. (cond
  1774. ((eq org-log-done t) (setq org-log-done 'time))
  1775. ((and (listp org-log-done) (memq 'done org-log-done))
  1776. (setq org-log-done 'note)))
  1777. (defcustom org-log-reschedule nil
  1778. "Information to record when the scheduling date of a tasks is modified.
  1779. Possible values are:
  1780. nil Don't add anything, just change the date
  1781. time Add a time stamp to the task
  1782. note Prompt for a note and add it with template `org-log-note-headings'
  1783. This option can also be set with on a per-file-basis with
  1784. #+STARTUP: nologreschedule
  1785. #+STARTUP: logreschedule
  1786. #+STARTUP: lognotereschedule"
  1787. :group 'org-todo
  1788. :group 'org-progress
  1789. :type '(choice
  1790. (const :tag "No logging" nil)
  1791. (const :tag "Record timestamp" time)
  1792. (const :tag "Record timestamp with note." note)))
  1793. (defcustom org-log-redeadline nil
  1794. "Information to record when the deadline date of a tasks is modified.
  1795. Possible values are:
  1796. nil Don't add anything, just change the date
  1797. time Add a time stamp to the task
  1798. note Prompt for a note and add it with template `org-log-note-headings'
  1799. This option can also be set with on a per-file-basis with
  1800. #+STARTUP: nologredeadline
  1801. #+STARTUP: logredeadline
  1802. #+STARTUP: lognoteredeadline
  1803. You can have local logging settings for a subtree by setting the LOGGING
  1804. property to one or more of these keywords."
  1805. :group 'org-todo
  1806. :group 'org-progress
  1807. :type '(choice
  1808. (const :tag "No logging" nil)
  1809. (const :tag "Record timestamp" time)
  1810. (const :tag "Record timestamp with note." note)))
  1811. (defcustom org-log-note-clock-out nil
  1812. "Non-nil means record a note when clocking out of an item.
  1813. This can also be configured on a per-file basis by adding one of
  1814. the following lines anywhere in the buffer:
  1815. #+STARTUP: lognoteclock-out
  1816. #+STARTUP: nolognoteclock-out"
  1817. :group 'org-todo
  1818. :group 'org-progress
  1819. :type 'boolean)
  1820. (defcustom org-log-done-with-time t
  1821. "Non-nil means the CLOSED time stamp will contain date and time.
  1822. When nil, only the date will be recorded."
  1823. :group 'org-progress
  1824. :type 'boolean)
  1825. (defcustom org-log-note-headings
  1826. '((done . "CLOSING NOTE %t")
  1827. (state . "State %-12s from %-12S %t")
  1828. (note . "Note taken on %t")
  1829. (reschedule . "Rescheduled from %S on %t")
  1830. (delschedule . "Not scheduled, was %S on %t")
  1831. (redeadline . "New deadline from %S on %t")
  1832. (deldeadline . "Removed deadline, was %S on %t")
  1833. (refile . "Refiled on %t")
  1834. (clock-out . ""))
  1835. "Headings for notes added to entries.
  1836. The value is an alist, with the car being a symbol indicating the note
  1837. context, and the cdr is the heading to be used. The heading may also be the
  1838. empty string.
  1839. %t in the heading will be replaced by a time stamp.
  1840. %s will be replaced by the new TODO state, in double quotes.
  1841. %S will be replaced by the old TODO state, in double quotes.
  1842. %u will be replaced by the user name.
  1843. %U will be replaced by the full user name.
  1844. In fact, it is not a good idea to change the `state' entry, because
  1845. agenda log mode depends on the format of these entries."
  1846. :group 'org-todo
  1847. :group 'org-progress
  1848. :type '(list :greedy t
  1849. (cons (const :tag "Heading when closing an item" done) string)
  1850. (cons (const :tag
  1851. "Heading when changing todo state (todo sequence only)"
  1852. state) string)
  1853. (cons (const :tag "Heading when just taking a note" note) string)
  1854. (cons (const :tag "Heading when clocking out" clock-out) string)
  1855. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  1856. (cons (const :tag "Heading when rescheduling" reschedule) string)
  1857. (cons (const :tag "Heading when changing deadline" redeadline) string)
  1858. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  1859. (cons (const :tag "Heading when refiling" refile) string)))
  1860. (unless (assq 'note org-log-note-headings)
  1861. (push '(note . "%t") org-log-note-headings))
  1862. (defcustom org-log-into-drawer nil
  1863. "Non-nil means insert state change notes and time stamps into a drawer.
  1864. When nil, state changes notes will be inserted after the headline and
  1865. any scheduling and clock lines, but not inside a drawer.
  1866. The value of this variable should be the name of the drawer to use.
  1867. LOGBOOK is proposed at the default drawer for this purpose, you can
  1868. also set this to a string to define the drawer of your choice.
  1869. A value of t is also allowed, representing \"LOGBOOK\".
  1870. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1871. will be ignored.
  1872. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1873. a subtree."
  1874. :group 'org-todo
  1875. :group 'org-progress
  1876. :type '(choice
  1877. (const :tag "Not into a drawer" nil)
  1878. (const :tag "LOGBOOK" t)
  1879. (string :tag "Other")))
  1880. (if (fboundp 'defvaralias)
  1881. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1882. (defun org-log-into-drawer ()
  1883. "Return the value of `org-log-into-drawer', but let properties overrule.
  1884. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1885. used instead of the default value."
  1886. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1887. (cond
  1888. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1889. ((equal p "t") "LOGBOOK")
  1890. (t p))))
  1891. (defcustom org-log-state-notes-insert-after-drawers nil
  1892. "Non-nil means insert state change notes after any drawers in entry.
  1893. Only the drawers that *immediately* follow the headline and the
  1894. deadline/scheduled line are skipped.
  1895. When nil, insert notes right after the heading and perhaps the line
  1896. with deadline/scheduling if present.
  1897. This variable will have no effect if `org-log-into-drawer' is
  1898. set."
  1899. :group 'org-todo
  1900. :group 'org-progress
  1901. :type 'boolean)
  1902. (defcustom org-log-states-order-reversed t
  1903. "Non-nil means the latest state note will be directly after heading.
  1904. When nil, the state change notes will be ordered according to time."
  1905. :group 'org-todo
  1906. :group 'org-progress
  1907. :type 'boolean)
  1908. (defcustom org-todo-repeat-to-state nil
  1909. "The TODO state to which a repeater should return the repeating task.
  1910. By default this is the first task in a TODO sequence, or the previous state
  1911. in a TODO_TYP set. But you can specify another task here.
  1912. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  1913. :group 'org-todo
  1914. :type '(choice (const :tag "Head of sequence" nil)
  1915. (string :tag "Specific state")))
  1916. (defcustom org-log-repeat 'time
  1917. "Non-nil means record moving through the DONE state when triggering repeat.
  1918. An auto-repeating task is immediately switched back to TODO when
  1919. marked DONE. If you are not logging state changes (by adding \"@\"
  1920. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  1921. record a closing note, there will be no record of the task moving
  1922. through DONE. This variable forces taking a note anyway.
  1923. nil Don't force a record
  1924. time Record a time stamp
  1925. note Record a note
  1926. This option can also be set with on a per-file-basis with
  1927. #+STARTUP: logrepeat
  1928. #+STARTUP: lognoterepeat
  1929. #+STARTUP: nologrepeat
  1930. You can have local logging settings for a subtree by setting the LOGGING
  1931. property to one or more of these keywords."
  1932. :group 'org-todo
  1933. :group 'org-progress
  1934. :type '(choice
  1935. (const :tag "Don't force a record" nil)
  1936. (const :tag "Force recording the DONE state" time)
  1937. (const :tag "Force recording a note with the DONE state" note)))
  1938. (defgroup org-priorities nil
  1939. "Priorities in Org-mode."
  1940. :tag "Org Priorities"
  1941. :group 'org-todo)
  1942. (defcustom org-enable-priority-commands t
  1943. "Non-nil means priority commands are active.
  1944. When nil, these commands will be disabled, so that you never accidentally
  1945. set a priority."
  1946. :group 'org-priorities
  1947. :type 'boolean)
  1948. (defcustom org-highest-priority ?A
  1949. "The highest priority of TODO items. A character like ?A, ?B etc.
  1950. Must have a smaller ASCII number than `org-lowest-priority'."
  1951. :group 'org-priorities
  1952. :type 'character)
  1953. (defcustom org-lowest-priority ?C
  1954. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1955. Must have a larger ASCII number than `org-highest-priority'."
  1956. :group 'org-priorities
  1957. :type 'character)
  1958. (defcustom org-default-priority ?B
  1959. "The default priority of TODO items.
  1960. This is the priority an item get if no explicit priority is given."
  1961. :group 'org-priorities
  1962. :type 'character)
  1963. (defcustom org-priority-start-cycle-with-default t
  1964. "Non-nil means start with default priority when starting to cycle.
  1965. When this is nil, the first step in the cycle will be (depending on the
  1966. command used) one higher or lower that the default priority."
  1967. :group 'org-priorities
  1968. :type 'boolean)
  1969. (defgroup org-time nil
  1970. "Options concerning time stamps and deadlines in Org-mode."
  1971. :tag "Org Time"
  1972. :group 'org)
  1973. (defcustom org-insert-labeled-timestamps-at-point nil
  1974. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  1975. When nil, these labeled time stamps are forces into the second line of an
  1976. entry, just after the headline. When scheduling from the global TODO list,
  1977. the time stamp will always be forced into the second line."
  1978. :group 'org-time
  1979. :type 'boolean)
  1980. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1981. "Formats for `format-time-string' which are used for time stamps.
  1982. It is not recommended to change this constant.")
  1983. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1984. "Number of minutes to round time stamps to.
  1985. These are two values, the first applies when first creating a time stamp.
  1986. The second applies when changing it with the commands `S-up' and `S-down'.
  1987. When changing the time stamp, this means that it will change in steps
  1988. of N minutes, as given by the second value.
  1989. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1990. numbers should be factors of 60, so for example 5, 10, 15.
  1991. When this is larger than 1, you can still force an exact time-stamp by using
  1992. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1993. and by using a prefix arg to `S-up/down' to specify the exact number
  1994. of minutes to shift."
  1995. :group 'org-time
  1996. :get '(lambda (var) ; Make sure both elements are there
  1997. (if (integerp (default-value var))
  1998. (list (default-value var) 5)
  1999. (default-value var)))
  2000. :type '(list
  2001. (integer :tag "when inserting times")
  2002. (integer :tag "when modifying times")))
  2003. ;; Normalize old customizations of this variable.
  2004. (when (integerp org-time-stamp-rounding-minutes)
  2005. (setq org-time-stamp-rounding-minutes
  2006. (list org-time-stamp-rounding-minutes
  2007. org-time-stamp-rounding-minutes)))
  2008. (defcustom org-display-custom-times nil
  2009. "Non-nil means overlay custom formats over all time stamps.
  2010. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2011. To turn this on on a per-file basis, insert anywhere in the file:
  2012. #+STARTUP: customtime"
  2013. :group 'org-time
  2014. :set 'set-default
  2015. :type 'sexp)
  2016. (make-variable-buffer-local 'org-display-custom-times)
  2017. (defcustom org-time-stamp-custom-formats
  2018. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2019. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2020. These are overlayed over the default ISO format if the variable
  2021. `org-display-custom-times' is set. Time like %H:%M should be at the
  2022. end of the second format. The custom formats are also honored by export
  2023. commands, if custom time display is turned on at the time of export."
  2024. :group 'org-time
  2025. :type 'sexp)
  2026. (defun org-time-stamp-format (&optional long inactive)
  2027. "Get the right format for a time string."
  2028. (let ((f (if long (cdr org-time-stamp-formats)
  2029. (car org-time-stamp-formats))))
  2030. (if inactive
  2031. (concat "[" (substring f 1 -1) "]")
  2032. f)))
  2033. (defcustom org-time-clocksum-format "%d:%02d"
  2034. "The format string used when creating CLOCKSUM lines, or when
  2035. org-mode generates a time duration."
  2036. :group 'org-time
  2037. :type 'string)
  2038. (defcustom org-time-clocksum-use-fractional nil
  2039. "If non-nil, \\[org-clock-display] uses fractional times.
  2040. org-mode generates a time duration."
  2041. :group 'org-time
  2042. :type 'boolean)
  2043. (defcustom org-time-clocksum-fractional-format "%.2f"
  2044. "The format string used when creating CLOCKSUM lines, or when
  2045. org-mode generates a time duration."
  2046. :group 'org-time
  2047. :type 'string)
  2048. (defcustom org-deadline-warning-days 14
  2049. "No. of days before expiration during which a deadline becomes active.
  2050. This variable governs the display in sparse trees and in the agenda.
  2051. When 0 or negative, it means use this number (the absolute value of it)
  2052. even if a deadline has a different individual lead time specified.
  2053. Custom commands can set this variable in the options section."
  2054. :group 'org-time
  2055. :group 'org-agenda-daily/weekly
  2056. :type 'integer)
  2057. (defcustom org-read-date-prefer-future t
  2058. "Non-nil means assume future for incomplete date input from user.
  2059. This affects the following situations:
  2060. 1. The user gives a month but not a year.
  2061. For example, if it is april and you enter \"feb 2\", this will be read
  2062. as feb 2, *next* year. \"May 5\", however, will be this year.
  2063. 2. The user gives a day, but no month.
  2064. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2065. read this as the third of *next* month. However, if you enter \"17\",
  2066. it will be considered as *this* month.
  2067. If you set this variable to the symbol `time', then also the following
  2068. will work:
  2069. 3. If the user gives a time, but no day. If the time is before now,
  2070. to will be interpreted as tomorrow.
  2071. Currently none of this works for ISO week specifications.
  2072. When this option is nil, the current day, month and year will always be
  2073. used as defaults."
  2074. :group 'org-time
  2075. :type '(choice
  2076. (const :tag "Never" nil)
  2077. (const :tag "Check month and day" t)
  2078. (const :tag "Check month, day, and time" time)))
  2079. (defcustom org-read-date-display-live t
  2080. "Non-nil means display current interpretation of date prompt live.
  2081. This display will be in an overlay, in the minibuffer."
  2082. :group 'org-time
  2083. :type 'boolean)
  2084. (defcustom org-read-date-popup-calendar t
  2085. "Non-nil means pop up a calendar when prompting for a date.
  2086. In the calendar, the date can be selected with mouse-1. However, the
  2087. minibuffer will also be active, and you can simply enter the date as well.
  2088. When nil, only the minibuffer will be available."
  2089. :group 'org-time
  2090. :type 'boolean)
  2091. (if (fboundp 'defvaralias)
  2092. (defvaralias 'org-popup-calendar-for-date-prompt
  2093. 'org-read-date-popup-calendar))
  2094. (defcustom org-read-date-minibuffer-setup-hook nil
  2095. "Hook to be used to set up keys for the date/time interface.
  2096. Add key definitions to `minibuffer-local-map', which will be a temporary
  2097. copy."
  2098. :group 'org-time
  2099. :type 'hook)
  2100. (defcustom org-extend-today-until 0
  2101. "The hour when your day really ends. Must be an integer.
  2102. This has influence for the following applications:
  2103. - When switching the agenda to \"today\". It it is still earlier than
  2104. the time given here, the day recognized as TODAY is actually yesterday.
  2105. - When a date is read from the user and it is still before the time given
  2106. here, the current date and time will be assumed to be yesterday, 23:59.
  2107. Also, timestamps inserted in remember templates follow this rule.
  2108. IMPORTANT: This is a feature whose implementation is and likely will
  2109. remain incomplete. Really, it is only here because past midnight seems to
  2110. be the favorite working time of John Wiegley :-)"
  2111. :group 'org-time
  2112. :type 'integer)
  2113. (defcustom org-edit-timestamp-down-means-later nil
  2114. "Non-nil means S-down will increase the time in a time stamp.
  2115. When nil, S-up will increase."
  2116. :group 'org-time
  2117. :type 'boolean)
  2118. (defcustom org-calendar-follow-timestamp-change t
  2119. "Non-nil means make the calendar window follow timestamp changes.
  2120. When a timestamp is modified and the calendar window is visible, it will be
  2121. moved to the new date."
  2122. :group 'org-time
  2123. :type 'boolean)
  2124. (defgroup org-tags nil
  2125. "Options concerning tags in Org-mode."
  2126. :tag "Org Tags"
  2127. :group 'org)
  2128. (defcustom org-tag-alist nil
  2129. "List of tags allowed in Org-mode files.
  2130. When this list is nil, Org-mode will base TAG input on what is already in the
  2131. buffer.
  2132. The value of this variable is an alist, the car of each entry must be a
  2133. keyword as a string, the cdr may be a character that is used to select
  2134. that tag through the fast-tag-selection interface.
  2135. See the manual for details."
  2136. :group 'org-tags
  2137. :type '(repeat
  2138. (choice
  2139. (cons (string :tag "Tag name")
  2140. (character :tag "Access char"))
  2141. (list :tag "Start radio group"
  2142. (const :startgroup)
  2143. (option (string :tag "Group description")))
  2144. (list :tag "End radio group"
  2145. (const :endgroup)
  2146. (option (string :tag "Group description")))
  2147. (const :tag "New line" (:newline)))))
  2148. (defcustom org-tag-persistent-alist nil
  2149. "List of tags that will always appear in all Org-mode files.
  2150. This is in addition to any in buffer settings or customizations
  2151. of `org-tag-alist'.
  2152. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2153. The value of this variable is an alist, the car of each entry must be a
  2154. keyword as a string, the cdr may be a character that is used to select
  2155. that tag through the fast-tag-selection interface.
  2156. See the manual for details.
  2157. To disable these tags on a per-file basis, insert anywhere in the file:
  2158. #+STARTUP: noptag"
  2159. :group 'org-tags
  2160. :type '(repeat
  2161. (choice
  2162. (cons (string :tag "Tag name")
  2163. (character :tag "Access char"))
  2164. (const :tag "Start radio group" (:startgroup))
  2165. (const :tag "End radio group" (:endgroup))
  2166. (const :tag "New line" (:newline)))))
  2167. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2168. "If non-nil, always offer completion for all tags of all agenda files.
  2169. Instead of customizing this variable directly, you might want to
  2170. set it locally for remember buffers, because there no list of
  2171. tags in that file can be created dynamically (there are none).
  2172. (add-hook 'org-remember-mode-hook
  2173. (lambda ()
  2174. (set (make-local-variable
  2175. 'org-complete-tags-always-offer-all-agenda-tags)
  2176. t)))"
  2177. :group 'org-tags
  2178. :type 'boolean)
  2179. (defvar org-file-tags nil
  2180. "List of tags that can be inherited by all entries in the file.
  2181. The tags will be inherited if the variable `org-use-tag-inheritance'
  2182. says they should be.
  2183. This variable is populated from #+FILETAGS lines.")
  2184. (defcustom org-use-fast-tag-selection 'auto
  2185. "Non-nil means use fast tag selection scheme.
  2186. This is a special interface to select and deselect tags with single keys.
  2187. When nil, fast selection is never used.
  2188. When the symbol `auto', fast selection is used if and only if selection
  2189. characters for tags have been configured, either through the variable
  2190. `org-tag-alist' or through a #+TAGS line in the buffer.
  2191. When t, fast selection is always used and selection keys are assigned
  2192. automatically if necessary."
  2193. :group 'org-tags
  2194. :type '(choice
  2195. (const :tag "Always" t)
  2196. (const :tag "Never" nil)
  2197. (const :tag "When selection characters are configured" 'auto)))
  2198. (defcustom org-fast-tag-selection-single-key nil
  2199. "Non-nil means fast tag selection exits after first change.
  2200. When nil, you have to press RET to exit it.
  2201. During fast tag selection, you can toggle this flag with `C-c'.
  2202. This variable can also have the value `expert'. In this case, the window
  2203. displaying the tags menu is not even shown, until you press C-c again."
  2204. :group 'org-tags
  2205. :type '(choice
  2206. (const :tag "No" nil)
  2207. (const :tag "Yes" t)
  2208. (const :tag "Expert" expert)))
  2209. (defvar org-fast-tag-selection-include-todo nil
  2210. "Non-nil means fast tags selection interface will also offer TODO states.
  2211. This is an undocumented feature, you should not rely on it.")
  2212. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2213. "The column to which tags should be indented in a headline.
  2214. If this number is positive, it specifies the column. If it is negative,
  2215. it means that the tags should be flushright to that column. For example,
  2216. -80 works well for a normal 80 character screen."
  2217. :group 'org-tags
  2218. :type 'integer)
  2219. (defcustom org-auto-align-tags t
  2220. "Non-nil means realign tags after pro/demotion of TODO state change.
  2221. These operations change the length of a headline and therefore shift
  2222. the tags around. With this options turned on, after each such operation
  2223. the tags are again aligned to `org-tags-column'."
  2224. :group 'org-tags
  2225. :type 'boolean)
  2226. (defcustom org-use-tag-inheritance t
  2227. "Non-nil means tags in levels apply also for sublevels.
  2228. When nil, only the tags directly given in a specific line apply there.
  2229. This may also be a list of tags that should be inherited, or a regexp that
  2230. matches tags that should be inherited. Additional control is possible
  2231. with the variable `org-tags-exclude-from-inheritance' which gives an
  2232. explicit list of tags to be excluded from inheritance., even if the value of
  2233. `org-use-tag-inheritance' would select it for inheritance.
  2234. If this option is t, a match early-on in a tree can lead to a large
  2235. number of matches in the subtree when constructing the agenda or creating
  2236. a sparse tree. If you only want to see the first match in a tree during
  2237. a search, check out the variable `org-tags-match-list-sublevels'."
  2238. :group 'org-tags
  2239. :type '(choice
  2240. (const :tag "Not" nil)
  2241. (const :tag "Always" t)
  2242. (repeat :tag "Specific tags" (string :tag "Tag"))
  2243. (regexp :tag "Tags matched by regexp")))
  2244. (defcustom org-tags-exclude-from-inheritance nil
  2245. "List of tags that should never be inherited.
  2246. This is a way to exclude a few tags from inheritance. For way to do
  2247. the opposite, to actively allow inheritance for selected tags,
  2248. see the variable `org-use-tag-inheritance'."
  2249. :group 'org-tags
  2250. :type '(repeat (string :tag "Tag")))
  2251. (defun org-tag-inherit-p (tag)
  2252. "Check if TAG is one that should be inherited."
  2253. (cond
  2254. ((member tag org-tags-exclude-from-inheritance) nil)
  2255. ((eq org-use-tag-inheritance t) t)
  2256. ((not org-use-tag-inheritance) nil)
  2257. ((stringp org-use-tag-inheritance)
  2258. (string-match org-use-tag-inheritance tag))
  2259. ((listp org-use-tag-inheritance)
  2260. (member tag org-use-tag-inheritance))
  2261. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2262. (defcustom org-tags-match-list-sublevels t
  2263. "Non-nil means list also sublevels of headlines matching a search.
  2264. This variable applies to tags/property searches, and also to stuck
  2265. projects because this search is based on a tags match as well.
  2266. When set to the symbol `indented', sublevels are indented with
  2267. leading dots.
  2268. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2269. the sublevels of a headline matching a tag search often also match
  2270. the same search. Listing all of them can create very long lists.
  2271. Setting this variable to nil causes subtrees of a match to be skipped.
  2272. This variable is semi-obsolete and probably should always be true. It
  2273. is better to limit inheritance to certain tags using the variables
  2274. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2275. :group 'org-tags
  2276. :type '(choice
  2277. (const :tag "No, don't list them" nil)
  2278. (const :tag "Yes, do list them" t)
  2279. (const :tag "List them, indented with leading dots" indented)))
  2280. (defcustom org-tags-sort-function nil
  2281. "When set, tags are sorted using this function as a comparator"
  2282. :group 'org-tags
  2283. :type '(choice
  2284. (const :tag "No sorting" nil)
  2285. (const :tag "Alphabetical" string<)
  2286. (const :tag "Reverse alphabetical" string>)
  2287. (function :tag "Custom function" nil)))
  2288. (defvar org-tags-history nil
  2289. "History of minibuffer reads for tags.")
  2290. (defvar org-last-tags-completion-table nil
  2291. "The last used completion table for tags.")
  2292. (defvar org-after-tags-change-hook nil
  2293. "Hook that is run after the tags in a line have changed.")
  2294. (defgroup org-properties nil
  2295. "Options concerning properties in Org-mode."
  2296. :tag "Org Properties"
  2297. :group 'org)
  2298. (defcustom org-property-format "%-10s %s"
  2299. "How property key/value pairs should be formatted by `indent-line'.
  2300. When `indent-line' hits a property definition, it will format the line
  2301. according to this format, mainly to make sure that the values are
  2302. lined-up with respect to each other."
  2303. :group 'org-properties
  2304. :type 'string)
  2305. (defcustom org-use-property-inheritance nil
  2306. "Non-nil means properties apply also for sublevels.
  2307. This setting is chiefly used during property searches. Turning it on can
  2308. cause significant overhead when doing a search, which is why it is not
  2309. on by default.
  2310. When nil, only the properties directly given in the current entry count.
  2311. When t, every property is inherited. The value may also be a list of
  2312. properties that should have inheritance, or a regular expression matching
  2313. properties that should be inherited.
  2314. However, note that some special properties use inheritance under special
  2315. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2316. and the properties ending in \"_ALL\" when they are used as descriptor
  2317. for valid values of a property.
  2318. Note for programmers:
  2319. When querying an entry with `org-entry-get', you can control if inheritance
  2320. should be used. By default, `org-entry-get' looks only at the local
  2321. properties. You can request inheritance by setting the inherit argument
  2322. to t (to force inheritance) or to `selective' (to respect the setting
  2323. in this variable)."
  2324. :group 'org-properties
  2325. :type '(choice
  2326. (const :tag "Not" nil)
  2327. (const :tag "Always" t)
  2328. (repeat :tag "Specific properties" (string :tag "Property"))
  2329. (regexp :tag "Properties matched by regexp")))
  2330. (defun org-property-inherit-p (property)
  2331. "Check if PROPERTY is one that should be inherited."
  2332. (cond
  2333. ((eq org-use-property-inheritance t) t)
  2334. ((not org-use-property-inheritance) nil)
  2335. ((stringp org-use-property-inheritance)
  2336. (string-match org-use-property-inheritance property))
  2337. ((listp org-use-property-inheritance)
  2338. (member property org-use-property-inheritance))
  2339. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2340. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2341. "The default column format, if no other format has been defined.
  2342. This variable can be set on the per-file basis by inserting a line
  2343. #+COLUMNS: %25ITEM ....."
  2344. :group 'org-properties
  2345. :type 'string)
  2346. (defcustom org-columns-ellipses ".."
  2347. "The ellipses to be used when a field in column view is truncated.
  2348. When this is the empty string, as many characters as possible are shown,
  2349. but then there will be no visual indication that the field has been truncated.
  2350. When this is a string of length N, the last N characters of a truncated
  2351. field are replaced by this string. If the column is narrower than the
  2352. ellipses string, only part of the ellipses string will be shown."
  2353. :group 'org-properties
  2354. :type 'string)
  2355. (defcustom org-columns-modify-value-for-display-function nil
  2356. "Function that modifies values for display in column view.
  2357. For example, it can be used to cut out a certain part from a time stamp.
  2358. The function must take 2 arguments:
  2359. column-title The title of the column (*not* the property name)
  2360. value The value that should be modified.
  2361. The function should return the value that should be displayed,
  2362. or nil if the normal value should be used."
  2363. :group 'org-properties
  2364. :type 'function)
  2365. (defcustom org-effort-property "Effort"
  2366. "The property that is being used to keep track of effort estimates.
  2367. Effort estimates given in this property need to have the format H:MM."
  2368. :group 'org-properties
  2369. :group 'org-progress
  2370. :type '(string :tag "Property"))
  2371. (defconst org-global-properties-fixed
  2372. '(("VISIBILITY_ALL" . "folded children content all")
  2373. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2374. "List of property/value pairs that can be inherited by any entry.
  2375. These are fixed values, for the preset properties. The user variable
  2376. that can be used to add to this list is `org-global-properties'.
  2377. The entries in this list are cons cells where the car is a property
  2378. name and cdr is a string with the value. If the value represents
  2379. multiple items like an \"_ALL\" property, separate the items by
  2380. spaces.")
  2381. (defcustom org-global-properties nil
  2382. "List of property/value pairs that can be inherited by any entry.
  2383. This list will be combined with the constant `org-global-properties-fixed'.
  2384. The entries in this list are cons cells where the car is a property
  2385. name and cdr is a string with the value.
  2386. You can set buffer-local values for the same purpose in the variable
  2387. `org-file-properties' this by adding lines like
  2388. #+PROPERTY: NAME VALUE"
  2389. :group 'org-properties
  2390. :type '(repeat
  2391. (cons (string :tag "Property")
  2392. (string :tag "Value"))))
  2393. (defvar org-file-properties nil
  2394. "List of property/value pairs that can be inherited by any entry.
  2395. Valid for the current buffer.
  2396. This variable is populated from #+PROPERTY lines.")
  2397. (make-variable-buffer-local 'org-file-properties)
  2398. (defgroup org-agenda nil
  2399. "Options concerning agenda views in Org-mode."
  2400. :tag "Org Agenda"
  2401. :group 'org)
  2402. (defvar org-category nil
  2403. "Variable used by org files to set a category for agenda display.
  2404. Such files should use a file variable to set it, for example
  2405. # -*- mode: org; org-category: \"ELisp\"
  2406. or contain a special line
  2407. #+CATEGORY: ELisp
  2408. If the file does not specify a category, then file's base name
  2409. is used instead.")
  2410. (make-variable-buffer-local 'org-category)
  2411. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2412. (defcustom org-agenda-files nil
  2413. "The files to be used for agenda display.
  2414. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2415. \\[org-remove-file]. You can also use customize to edit the list.
  2416. If an entry is a directory, all files in that directory that are matched by
  2417. `org-agenda-file-regexp' will be part of the file list.
  2418. If the value of the variable is not a list but a single file name, then
  2419. the list of agenda files is actually stored and maintained in that file, one
  2420. agenda file per line. In this file paths can be given relative to
  2421. `org-directory'. Tilde expansion and environment variable substitution
  2422. are also made."
  2423. :group 'org-agenda
  2424. :type '(choice
  2425. (repeat :tag "List of files and directories" file)
  2426. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2427. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2428. "Regular expression to match files for `org-agenda-files'.
  2429. If any element in the list in that variable contains a directory instead
  2430. of a normal file, all files in that directory that are matched by this
  2431. regular expression will be included."
  2432. :group 'org-agenda
  2433. :type 'regexp)
  2434. (defcustom org-agenda-text-search-extra-files nil
  2435. "List of extra files to be searched by text search commands.
  2436. These files will be search in addition to the agenda files by the
  2437. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2438. Note that these files will only be searched for text search commands,
  2439. not for the other agenda views like todo lists, tag searches or the weekly
  2440. agenda. This variable is intended to list notes and possibly archive files
  2441. that should also be searched by these two commands.
  2442. In fact, if the first element in the list is the symbol `agenda-archives',
  2443. than all archive files of all agenda files will be added to the search
  2444. scope."
  2445. :group 'org-agenda
  2446. :type '(set :greedy t
  2447. (const :tag "Agenda Archives" agenda-archives)
  2448. (repeat :inline t (file))))
  2449. (if (fboundp 'defvaralias)
  2450. (defvaralias 'org-agenda-multi-occur-extra-files
  2451. 'org-agenda-text-search-extra-files))
  2452. (defcustom org-agenda-skip-unavailable-files nil
  2453. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2454. A nil value means to remove them, after a query, from the list."
  2455. :group 'org-agenda
  2456. :type 'boolean)
  2457. (defcustom org-calendar-to-agenda-key [?c]
  2458. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2459. The command `org-calendar-goto-agenda' will be bound to this key. The
  2460. default is the character `c' because then `c' can be used to switch back and
  2461. forth between agenda and calendar."
  2462. :group 'org-agenda
  2463. :type 'sexp)
  2464. (defcustom org-calendar-agenda-action-key [?k]
  2465. "The key to be installed in `calendar-mode-map' for agenda-action.
  2466. The command `org-agenda-action' will be bound to this key. The
  2467. default is the character `k' because we use the same key in the agenda."
  2468. :group 'org-agenda
  2469. :type 'sexp)
  2470. (defcustom org-calendar-insert-diary-entry-key [?i]
  2471. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2472. This option is irrelevant until `org-agenda-diary-file' has been configured
  2473. to point to an Org-mode file. When that is the case, the command
  2474. `org-agenda-diary-entry' will be bound to the key given here, by default
  2475. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2476. if you want to continue doing this, you need to change this to a different
  2477. key."
  2478. :group 'org-agenda
  2479. :type 'sexp)
  2480. (defcustom org-agenda-diary-file 'diary-file
  2481. "File to which to add new entries with the `i' key in agenda and calendar.
  2482. When this is the symbol `diary-file', the functionality in the Emacs
  2483. calendar will be used to add entries to the `diary-file'. But when this
  2484. points to a file, `org-agenda-diary-entry' will be used instead."
  2485. :group 'org-agenda
  2486. :type '(choice
  2487. (const :tag "The standard Emacs diary file" diary-file)
  2488. (file :tag "Special Org file diary entries")))
  2489. (eval-after-load "calendar"
  2490. '(progn
  2491. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2492. 'org-calendar-goto-agenda)
  2493. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2494. 'org-agenda-action)
  2495. (add-hook 'calendar-mode-hook
  2496. (lambda ()
  2497. (unless (eq org-agenda-diary-file 'diary-file)
  2498. (define-key calendar-mode-map
  2499. org-calendar-insert-diary-entry-key
  2500. 'org-agenda-diary-entry))))))
  2501. (defgroup org-latex nil
  2502. "Options for embedding LaTeX code into Org-mode."
  2503. :tag "Org LaTeX"
  2504. :group 'org)
  2505. (defcustom org-format-latex-options
  2506. '(:foreground default :background default :scale 1.0
  2507. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2508. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2509. "Options for creating images from LaTeX fragments.
  2510. This is a property list with the following properties:
  2511. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2512. `default' means use the foreground of the default face.
  2513. :background the background color, or \"Transparent\".
  2514. `default' means use the background of the default face.
  2515. :scale a scaling factor for the size of the images.
  2516. :html-foreground, :html-background, :html-scale
  2517. the same numbers for HTML export.
  2518. :matchers a list indicating which matchers should be used to
  2519. find LaTeX fragments. Valid members of this list are:
  2520. \"begin\" find environments
  2521. \"$1\" find single characters surrounded by $.$
  2522. \"$\" find math expressions surrounded by $...$
  2523. \"$$\" find math expressions surrounded by $$....$$
  2524. \"\\(\" find math expressions surrounded by \\(...\\)
  2525. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2526. :group 'org-latex
  2527. :type 'plist)
  2528. (defcustom org-format-latex-signal-error t
  2529. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2530. When nil, just push out a message."
  2531. :group 'org-latex
  2532. :type 'boolean)
  2533. (defcustom org-format-latex-header "\\documentclass{article}
  2534. \\usepackage[usenames]{color}
  2535. \\usepackage{amsmath}
  2536. \\usepackage[mathscr]{eucal}
  2537. \\pagestyle{empty} % do not remove
  2538. \[PACKAGES]
  2539. \[DEFAULT-PACKAGES]
  2540. % The settings below are copied from fullpage.sty
  2541. \\setlength{\\textwidth}{\\paperwidth}
  2542. \\addtolength{\\textwidth}{-3cm}
  2543. \\setlength{\\oddsidemargin}{1.5cm}
  2544. \\addtolength{\\oddsidemargin}{-2.54cm}
  2545. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2546. \\setlength{\\textheight}{\\paperheight}
  2547. \\addtolength{\\textheight}{-\\headheight}
  2548. \\addtolength{\\textheight}{-\\headsep}
  2549. \\addtolength{\\textheight}{-\\footskip}
  2550. \\addtolength{\\textheight}{-3cm}
  2551. \\setlength{\\topmargin}{1.5cm}
  2552. \\addtolength{\\topmargin}{-2.54cm}"
  2553. "The document header used for processing LaTeX fragments.
  2554. It is imperative that this header make sure that no page number
  2555. appears on the page. The package defined in the variables
  2556. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2557. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2558. will be appended."
  2559. :group 'org-latex
  2560. :type 'string)
  2561. (defvar org-format-latex-header-extra nil)
  2562. (defun org-set-packages-alist (var val)
  2563. "Set the packages alist and make sure it has 3 elements per entry."
  2564. (set var (mapcar (lambda (x)
  2565. (if (and (consp x) (= (length x) 2))
  2566. (list (car x) (nth 1 x) t)
  2567. x))
  2568. val)))
  2569. (defun org-get-packages-alist (var)
  2570. "Get the packages alist and make sure it has 3 elements per entry."
  2571. (mapcar (lambda (x)
  2572. (if (and (consp x) (= (length x) 2))
  2573. (list (car x) (nth 1 x) t)
  2574. x))
  2575. (default-value var)))
  2576. ;; The following variables are defined here because is it also used
  2577. ;; when formatting latex fragments. Originally it was part of the
  2578. ;; LaTeX exporter, which is why the name includes "export".
  2579. (defcustom org-export-latex-default-packages-alist
  2580. '(("AUTO" "inputenc" t)
  2581. ("T1" "fontenc" t)
  2582. ("" "fixltx2e" nil)
  2583. ("" "graphicx" t)
  2584. ("" "longtable" nil)
  2585. ("" "float" nil)
  2586. ("" "wrapfig" nil)
  2587. ("" "soul" t)
  2588. ("" "t1enc" t)
  2589. ("" "textcomp" t)
  2590. ("" "marvosym" t)
  2591. ("" "wasysym" t)
  2592. ("" "latexsym" t)
  2593. ("" "amssymb" t)
  2594. ("" "hyperref" nil)
  2595. "\\tolerance=1000"
  2596. )
  2597. "Alist of default packages to be inserted in the header.
  2598. Change this only if one of the packages here causes an incompatibility
  2599. with another package you are using.
  2600. The packages in this list are needed by one part or another of Org-mode
  2601. to function properly.
  2602. - inputenc, fontenc, t1enc: for basic font and character selection
  2603. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2604. for interpreting the entities in `org-entities'. You can skip some of these
  2605. packages if you don't use any of the symbols in it.
  2606. - graphicx: for including images
  2607. - float, wrapfig: for figure placement
  2608. - longtable: for long tables
  2609. - hyperref: for cross references
  2610. Therefore you should not modify this variable unless you know what you
  2611. are doing. The one reason to change it anyway is that you might be loading
  2612. some other package that conflicts with one of the default packages.
  2613. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  2614. If SNIPPET-FLAG is t, the package also needs to be included when
  2615. compiling LaTeX snippets into images for inclusion into HTML."
  2616. :group 'org-export-latex
  2617. :set 'org-set-packages-alist
  2618. :get 'org-get-packages-alist
  2619. :type '(repeat
  2620. (choice
  2621. (list :tag "options/package pair"
  2622. (string :tag "options")
  2623. (string :tag "package")
  2624. (boolean :tag "Snippet"))
  2625. (string :tag "A line of LaTeX"))))
  2626. (defcustom org-export-latex-packages-alist nil
  2627. "Alist of packages to be inserted in every LaTeX header.
  2628. These will be inserted after `org-export-latex-default-packages-alist'.
  2629. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  2630. SNIPPET-FLAG, when t, indicates that this package is also needed when
  2631. turning LaTeX snippets into images for inclusion into HTML.
  2632. Make sure that you only list packages here which:
  2633. - you want in every file
  2634. - do not conflict with the default packages in
  2635. `org-export-latex-default-packages-alist'
  2636. - do not conflict with the setup in `org-format-latex-header'."
  2637. :group 'org-export-latex
  2638. :set 'org-set-packages-alist
  2639. :get 'org-get-packages-alist
  2640. :type '(repeat
  2641. (choice
  2642. (list :tag "options/package pair"
  2643. (string :tag "options")
  2644. (string :tag "package")
  2645. (boolean :tag "Snippet"))
  2646. (string :tag "A line of LaTeX"))))
  2647. (defgroup org-appearance nil
  2648. "Settings for Org-mode appearance."
  2649. :tag "Org Appearance"
  2650. :group 'org)
  2651. (defcustom org-level-color-stars-only nil
  2652. "Non-nil means fontify only the stars in each headline.
  2653. When nil, the entire headline is fontified.
  2654. Changing it requires restart of `font-lock-mode' to become effective
  2655. also in regions already fontified."
  2656. :group 'org-appearance
  2657. :type 'boolean)
  2658. (defcustom org-hide-leading-stars nil
  2659. "Non-nil means hide the first N-1 stars in a headline.
  2660. This works by using the face `org-hide' for these stars. This
  2661. face is white for a light background, and black for a dark
  2662. background. You may have to customize the face `org-hide' to
  2663. make this work.
  2664. Changing it requires restart of `font-lock-mode' to become effective
  2665. also in regions already fontified.
  2666. You may also set this on a per-file basis by adding one of the following
  2667. lines to the buffer:
  2668. #+STARTUP: hidestars
  2669. #+STARTUP: showstars"
  2670. :group 'org-appearance
  2671. :type 'boolean)
  2672. (defcustom org-hidden-keywords nil
  2673. "List of keywords that should be hidden when typed in the org buffer.
  2674. For example, add #+TITLE to this list in order to make the
  2675. document title appear in the buffer without the initial #+TITLE:
  2676. keyword."
  2677. :group 'org-appearance
  2678. :type '(set (const :tag "#+AUTHOR" author)
  2679. (const :tag "#+DATE" date)
  2680. (const :tag "#+EMAIL" email)
  2681. (const :tag "#+TITLE" title)))
  2682. (defcustom org-fontify-done-headline nil
  2683. "Non-nil means change the face of a headline if it is marked DONE.
  2684. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2685. When this is non-nil, the headline after the keyword is set to the
  2686. `org-headline-done' as an additional indication."
  2687. :group 'org-appearance
  2688. :type 'boolean)
  2689. (defcustom org-fontify-emphasized-text t
  2690. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2691. Changing this variable requires a restart of Emacs to take effect."
  2692. :group 'org-appearance
  2693. :type 'boolean)
  2694. (defcustom org-fontify-whole-heading-line nil
  2695. "Non-nil means fontify the whole line for headings.
  2696. This is useful when setting a background color for the
  2697. org-level-* faces."
  2698. :group 'org-appearance
  2699. :type 'boolean)
  2700. (defcustom org-highlight-latex-fragments-and-specials nil
  2701. "Non-nil means fontify what is treated specially by the exporters."
  2702. :group 'org-appearance
  2703. :type 'boolean)
  2704. (defcustom org-hide-emphasis-markers nil
  2705. "Non-nil mean font-lock should hide the emphasis marker characters."
  2706. :group 'org-appearance
  2707. :type 'boolean)
  2708. (defvar org-emph-re nil
  2709. "Regular expression for matching emphasis.")
  2710. (defvar org-verbatim-re nil
  2711. "Regular expression for matching verbatim text.")
  2712. (defvar org-emphasis-regexp-components) ; defined just below
  2713. (defvar org-emphasis-alist) ; defined just below
  2714. (defun org-set-emph-re (var val)
  2715. "Set variable and compute the emphasis regular expression."
  2716. (set var val)
  2717. (when (and (boundp 'org-emphasis-alist)
  2718. (boundp 'org-emphasis-regexp-components)
  2719. org-emphasis-alist org-emphasis-regexp-components)
  2720. (let* ((e org-emphasis-regexp-components)
  2721. (pre (car e))
  2722. (post (nth 1 e))
  2723. (border (nth 2 e))
  2724. (body (nth 3 e))
  2725. (nl (nth 4 e))
  2726. (body1 (concat body "*?"))
  2727. (markers (mapconcat 'car org-emphasis-alist ""))
  2728. (vmarkers (mapconcat
  2729. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2730. org-emphasis-alist "")))
  2731. ;; make sure special characters appear at the right position in the class
  2732. (if (string-match "\\^" markers)
  2733. (setq markers (concat (replace-match "" t t markers) "^")))
  2734. (if (string-match "-" markers)
  2735. (setq markers (concat (replace-match "" t t markers) "-")))
  2736. (if (string-match "\\^" vmarkers)
  2737. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2738. (if (string-match "-" vmarkers)
  2739. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2740. (if (> nl 0)
  2741. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2742. (int-to-string nl) "\\}")))
  2743. ;; Make the regexp
  2744. (setq org-emph-re
  2745. (concat "\\([" pre "]\\|^\\)"
  2746. "\\("
  2747. "\\([" markers "]\\)"
  2748. "\\("
  2749. "[^" border "]\\|"
  2750. "[^" border "]"
  2751. body1
  2752. "[^" border "]"
  2753. "\\)"
  2754. "\\3\\)"
  2755. "\\([" post "]\\|$\\)"))
  2756. (setq org-verbatim-re
  2757. (concat "\\([" pre "]\\|^\\)"
  2758. "\\("
  2759. "\\([" vmarkers "]\\)"
  2760. "\\("
  2761. "[^" border "]\\|"
  2762. "[^" border "]"
  2763. body1
  2764. "[^" border "]"
  2765. "\\)"
  2766. "\\3\\)"
  2767. "\\([" post "]\\|$\\)")))))
  2768. (defcustom org-emphasis-regexp-components
  2769. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2770. "Components used to build the regular expression for emphasis.
  2771. This is a list with 6 entries. Terminology: In an emphasis string
  2772. like \" *strong word* \", we call the initial space PREMATCH, the final
  2773. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2774. and \"trong wor\" is the body. The different components in this variable
  2775. specify what is allowed/forbidden in each part:
  2776. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2777. post Chars allowed as postmatch. End of line will be allowed too.
  2778. border The chars *forbidden* as border characters.
  2779. body-regexp A regexp like \".\" to match a body character. Don't use
  2780. non-shy groups here, and don't allow newline here.
  2781. newline The maximum number of newlines allowed in an emphasis exp.
  2782. Use customize to modify this, or restart Emacs after changing it."
  2783. :group 'org-appearance
  2784. :set 'org-set-emph-re
  2785. :type '(list
  2786. (sexp :tag "Allowed chars in pre ")
  2787. (sexp :tag "Allowed chars in post ")
  2788. (sexp :tag "Forbidden chars in border ")
  2789. (sexp :tag "Regexp for body ")
  2790. (integer :tag "number of newlines allowed")
  2791. (option (boolean :tag "Please ignore this button"))))
  2792. (defcustom org-emphasis-alist
  2793. `(("*" bold "<b>" "</b>")
  2794. ("/" italic "<i>" "</i>")
  2795. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2796. ("=" org-code "<code>" "</code>" verbatim)
  2797. ("~" org-verbatim "<code>" "</code>" verbatim)
  2798. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2799. "<del>" "</del>")
  2800. )
  2801. "Special syntax for emphasized text.
  2802. Text starting and ending with a special character will be emphasized, for
  2803. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2804. characters, the face to be used by font-lock for highlighting in Org-mode
  2805. Emacs buffers, and the HTML tags to be used for this.
  2806. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2807. Use customize to modify this, or restart Emacs after changing it."
  2808. :group 'org-appearance
  2809. :set 'org-set-emph-re
  2810. :type '(repeat
  2811. (list
  2812. (string :tag "Marker character")
  2813. (choice
  2814. (face :tag "Font-lock-face")
  2815. (plist :tag "Face property list"))
  2816. (string :tag "HTML start tag")
  2817. (string :tag "HTML end tag")
  2818. (option (const verbatim)))))
  2819. (defvar org-protecting-blocks
  2820. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2821. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2822. This is needed for font-lock setup.")
  2823. ;;; Miscellaneous options
  2824. (defgroup org-completion nil
  2825. "Completion in Org-mode."
  2826. :tag "Org Completion"
  2827. :group 'org)
  2828. (defcustom org-completion-use-ido nil
  2829. "Non-nil means use ido completion wherever possible.
  2830. Note that `ido-mode' must be active for this variable to be relevant.
  2831. If you decide to turn this variable on, you might well want to turn off
  2832. `org-outline-path-complete-in-steps'.
  2833. See also `org-completion-use-iswitchb'."
  2834. :group 'org-completion
  2835. :type 'boolean)
  2836. (defcustom org-completion-use-iswitchb nil
  2837. "Non-nil means use iswitchb completion wherever possible.
  2838. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2839. If you decide to turn this variable on, you might well want to turn off
  2840. `org-outline-path-complete-in-steps'.
  2841. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  2842. :group 'org-completion
  2843. :type 'boolean)
  2844. (defcustom org-completion-fallback-command 'hippie-expand
  2845. "The expansion command called by \\[org-complete] in normal context.
  2846. Normal means no org-mode-specific context."
  2847. :group 'org-completion
  2848. :type 'function)
  2849. ;;; Functions and variables from their packages
  2850. ;; Declared here to avoid compiler warnings
  2851. ;; XEmacs only
  2852. (defvar outline-mode-menu-heading)
  2853. (defvar outline-mode-menu-show)
  2854. (defvar outline-mode-menu-hide)
  2855. (defvar zmacs-regions) ; XEmacs regions
  2856. ;; Emacs only
  2857. (defvar mark-active)
  2858. ;; Various packages
  2859. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2860. (declare-function calendar-forward-day "cal-move" (arg))
  2861. (declare-function calendar-goto-date "cal-move" (date))
  2862. (declare-function calendar-goto-today "cal-move" ())
  2863. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2864. (defvar calc-embedded-close-formula)
  2865. (defvar calc-embedded-open-formula)
  2866. (declare-function cdlatex-tab "ext:cdlatex" ())
  2867. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2868. (defvar font-lock-unfontify-region-function)
  2869. (declare-function iswitchb-read-buffer "iswitchb"
  2870. (prompt &optional default require-match start matches-set))
  2871. (defvar iswitchb-temp-buflist)
  2872. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2873. (defvar org-agenda-tags-todo-honor-ignore-options)
  2874. (declare-function org-agenda-skip "org-agenda" ())
  2875. (declare-function
  2876. org-format-agenda-item "org-agenda"
  2877. (extra txt &optional category tags dotime noprefix remove-re habitp))
  2878. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2879. (declare-function org-agenda-change-all-lines "org-agenda"
  2880. (newhead hdmarker &optional fixface just-this))
  2881. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2882. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2883. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2884. (beg end))
  2885. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2886. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2887. "org-agenda" (&optional end))
  2888. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2889. (declare-function org-indent-mode "org-indent" (&optional arg))
  2890. (declare-function parse-time-string "parse-time" (string))
  2891. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  2892. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  2893. (defvar remember-data-file)
  2894. (defvar texmathp-why)
  2895. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2896. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2897. (defvar w3m-current-url)
  2898. (defvar w3m-current-title)
  2899. (defvar org-latex-regexps)
  2900. ;;; Autoload and prepare some org modules
  2901. ;; Some table stuff that needs to be defined here, because it is used
  2902. ;; by the functions setting up org-mode or checking for table context.
  2903. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2904. "Detects an org-type or table-type table.")
  2905. (defconst org-table-line-regexp "^[ \t]*|"
  2906. "Detects an org-type table line.")
  2907. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2908. "Detects an org-type table line.")
  2909. (defconst org-table-hline-regexp "^[ \t]*|-"
  2910. "Detects an org-type table hline.")
  2911. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2912. "Detects a table-type table hline.")
  2913. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2914. "Searching from within a table (any type) this finds the first line
  2915. outside the table.")
  2916. ;; Autoload the functions in org-table.el that are needed by functions here.
  2917. (eval-and-compile
  2918. (org-autoload "org-table"
  2919. '(org-table-align org-table-begin org-table-blank-field
  2920. org-table-convert org-table-convert-region org-table-copy-down
  2921. org-table-copy-region org-table-create
  2922. org-table-create-or-convert-from-region
  2923. org-table-create-with-table.el org-table-current-dline
  2924. org-table-cut-region org-table-delete-column org-table-edit-field
  2925. org-table-edit-formulas org-table-end org-table-eval-formula
  2926. org-table-export org-table-field-info
  2927. org-table-get-stored-formulas org-table-goto-column
  2928. org-table-hline-and-move org-table-import org-table-insert-column
  2929. org-table-insert-hline org-table-insert-row org-table-iterate
  2930. org-table-justify-field-maybe org-table-kill-row
  2931. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2932. org-table-move-column org-table-move-column-left
  2933. org-table-move-column-right org-table-move-row
  2934. org-table-move-row-down org-table-move-row-up
  2935. org-table-next-field org-table-next-row org-table-paste-rectangle
  2936. org-table-previous-field org-table-recalculate
  2937. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2938. org-table-toggle-coordinate-overlays
  2939. org-table-toggle-formula-debugger org-table-wrap-region
  2940. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2941. (defun org-at-table-p (&optional table-type)
  2942. "Return t if the cursor is inside an org-type table.
  2943. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2944. (if org-enable-table-editor
  2945. (save-excursion
  2946. (beginning-of-line 1)
  2947. (looking-at (if table-type org-table-any-line-regexp
  2948. org-table-line-regexp)))
  2949. nil))
  2950. (defsubst org-table-p () (org-at-table-p))
  2951. (defun org-at-table.el-p ()
  2952. "Return t if and only if we are at a table.el table."
  2953. (and (org-at-table-p 'any)
  2954. (save-excursion
  2955. (goto-char (org-table-begin 'any))
  2956. (looking-at org-table1-hline-regexp))))
  2957. (defun org-table-recognize-table.el ()
  2958. "If there is a table.el table nearby, recognize it and move into it."
  2959. (if org-table-tab-recognizes-table.el
  2960. (if (org-at-table.el-p)
  2961. (progn
  2962. (beginning-of-line 1)
  2963. (if (looking-at org-table-dataline-regexp)
  2964. nil
  2965. (if (looking-at org-table1-hline-regexp)
  2966. (progn
  2967. (beginning-of-line 2)
  2968. (if (looking-at org-table-any-border-regexp)
  2969. (beginning-of-line -1)))))
  2970. (if (re-search-forward "|" (org-table-end t) t)
  2971. (progn
  2972. (require 'table)
  2973. (if (table--at-cell-p (point))
  2974. t
  2975. (message "recognizing table.el table...")
  2976. (table-recognize-table)
  2977. (message "recognizing table.el table...done")))
  2978. (error "This should not happen..."))
  2979. t)
  2980. nil)
  2981. nil))
  2982. (defun org-at-table-hline-p ()
  2983. "Return t if the cursor is inside a hline in a table."
  2984. (if org-enable-table-editor
  2985. (save-excursion
  2986. (beginning-of-line 1)
  2987. (looking-at org-table-hline-regexp))
  2988. nil))
  2989. (defvar org-table-clean-did-remove-column nil)
  2990. (defun org-table-map-tables (function &optional quietly)
  2991. "Apply FUNCTION to the start of all tables in the buffer."
  2992. (save-excursion
  2993. (save-restriction
  2994. (widen)
  2995. (goto-char (point-min))
  2996. (while (re-search-forward org-table-any-line-regexp nil t)
  2997. (unless quietly
  2998. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  2999. (beginning-of-line 1)
  3000. (when (looking-at org-table-line-regexp)
  3001. (save-excursion (funcall function))
  3002. (or (looking-at org-table-line-regexp)
  3003. (forward-char 1)))
  3004. (re-search-forward org-table-any-border-regexp nil 1))))
  3005. (unless quietly (message "Mapping tables: done")))
  3006. ;; Declare and autoload functions from org-exp.el & Co
  3007. (declare-function org-default-export-plist "org-exp")
  3008. (declare-function org-infile-export-plist "org-exp")
  3009. (declare-function org-get-current-options "org-exp")
  3010. (eval-and-compile
  3011. (org-autoload "org-exp"
  3012. '(org-export org-export-visible
  3013. org-insert-export-options-template
  3014. org-table-clean-before-export))
  3015. (org-autoload "org-ascii"
  3016. '(org-export-as-ascii org-export-ascii-preprocess
  3017. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3018. org-export-region-as-ascii))
  3019. (org-autoload "org-latex"
  3020. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3021. org-replace-region-by-latex org-export-region-as-latex
  3022. org-export-as-latex org-export-as-pdf
  3023. org-export-as-pdf-and-open))
  3024. (org-autoload "org-html"
  3025. '(org-export-as-html-and-open
  3026. org-export-as-html-batch org-export-as-html-to-buffer
  3027. org-replace-region-by-html org-export-region-as-html
  3028. org-export-as-html))
  3029. (org-autoload "org-docbook"
  3030. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3031. org-replace-region-by-docbook org-export-region-as-docbook
  3032. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3033. org-export-as-docbook))
  3034. (org-autoload "org-icalendar"
  3035. '(org-export-icalendar-this-file
  3036. org-export-icalendar-all-agenda-files
  3037. org-export-icalendar-combine-agenda-files))
  3038. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3039. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3040. ;; Declare and autoload functions from org-agenda.el
  3041. (eval-and-compile
  3042. (org-autoload "org-agenda"
  3043. '(org-agenda org-agenda-list org-search-view
  3044. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3045. org-diary org-agenda-to-appt
  3046. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3047. ;; Autoload org-remember
  3048. (eval-and-compile
  3049. (org-autoload "org-remember"
  3050. '(org-remember-insinuate org-remember-annotation
  3051. org-remember-apply-template org-remember org-remember-handler)))
  3052. ;; Autoload org-clock.el
  3053. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3054. (beg end))
  3055. (declare-function org-clock-update-mode-line "org-clock" ())
  3056. (declare-function org-resolve-clocks "org-clock"
  3057. (&optional also-non-dangling-p prompt last-valid))
  3058. (defvar org-clock-start-time)
  3059. (defvar org-clock-marker (make-marker)
  3060. "Marker recording the last clock-in.")
  3061. (defvar org-clock-hd-marker (make-marker)
  3062. "Marker recording the last clock-in, but the headline position.")
  3063. (defvar org-clock-heading ""
  3064. "The heading of the current clock entry.")
  3065. (defun org-clock-is-active ()
  3066. "Return non-nil if clock is currently running.
  3067. The return value is actually the clock marker."
  3068. (marker-buffer org-clock-marker))
  3069. (eval-and-compile
  3070. (org-autoload
  3071. "org-clock"
  3072. '(org-clock-in org-clock-out org-clock-cancel
  3073. org-clock-goto org-clock-sum org-clock-display
  3074. org-clock-remove-overlays org-clock-report
  3075. org-clocktable-shift org-dblock-write:clocktable
  3076. org-get-clocktable org-resolve-clocks)))
  3077. (defun org-clock-update-time-maybe ()
  3078. "If this is a CLOCK line, update it and return t.
  3079. Otherwise, return nil."
  3080. (interactive)
  3081. (save-excursion
  3082. (beginning-of-line 1)
  3083. (skip-chars-forward " \t")
  3084. (when (looking-at org-clock-string)
  3085. (let ((re (concat "[ \t]*" org-clock-string
  3086. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3087. "\\([ \t]*=>.*\\)?\\)?"))
  3088. ts te h m s neg)
  3089. (cond
  3090. ((not (looking-at re))
  3091. nil)
  3092. ((not (match-end 2))
  3093. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3094. (> org-clock-marker (point))
  3095. (<= org-clock-marker (point-at-eol)))
  3096. ;; The clock is running here
  3097. (setq org-clock-start-time
  3098. (apply 'encode-time
  3099. (org-parse-time-string (match-string 1))))
  3100. (org-clock-update-mode-line)))
  3101. (t
  3102. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3103. (end-of-line 1)
  3104. (setq ts (match-string 1)
  3105. te (match-string 3))
  3106. (setq s (- (org-float-time
  3107. (apply 'encode-time (org-parse-time-string te)))
  3108. (org-float-time
  3109. (apply 'encode-time (org-parse-time-string ts))))
  3110. neg (< s 0)
  3111. s (abs s)
  3112. h (floor (/ s 3600))
  3113. s (- s (* 3600 h))
  3114. m (floor (/ s 60))
  3115. s (- s (* 60 s)))
  3116. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3117. t))))))
  3118. (defun org-check-running-clock ()
  3119. "Check if the current buffer contains the running clock.
  3120. If yes, offer to stop it and to save the buffer with the changes."
  3121. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3122. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3123. (buffer-name))))
  3124. (org-clock-out)
  3125. (when (y-or-n-p "Save changed buffer?")
  3126. (save-buffer))))
  3127. (defun org-clocktable-try-shift (dir n)
  3128. "Check if this line starts a clock table, if yes, shift the time block."
  3129. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  3130. (org-clocktable-shift dir n)))
  3131. ;; Autoload org-timer.el
  3132. (eval-and-compile
  3133. (org-autoload
  3134. "org-timer"
  3135. '(org-timer-start org-timer org-timer-item
  3136. org-timer-change-times-in-region
  3137. org-timer-set-timer
  3138. org-timer-reset-timers
  3139. org-timer-show-remaining-time)))
  3140. ;; Autoload org-feed.el
  3141. (eval-and-compile
  3142. (org-autoload
  3143. "org-feed"
  3144. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3145. ;; Autoload org-indent.el
  3146. ;; Define the variable already here, to make sure we have it.
  3147. (defvar org-indent-mode nil
  3148. "Non-nil if Org-Indent mode is enabled.
  3149. Use the command `org-indent-mode' to change this variable.")
  3150. (eval-and-compile
  3151. (org-autoload
  3152. "org-indent"
  3153. '(org-indent-mode)))
  3154. ;; Autoload org-mobile.el
  3155. (eval-and-compile
  3156. (org-autoload
  3157. "org-mobile"
  3158. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3159. ;; Autoload archiving code
  3160. ;; The stuff that is needed for cycling and tags has to be defined here.
  3161. (defgroup org-archive nil
  3162. "Options concerning archiving in Org-mode."
  3163. :tag "Org Archive"
  3164. :group 'org-structure)
  3165. (defcustom org-archive-location "%s_archive::"
  3166. "The location where subtrees should be archived.
  3167. The value of this variable is a string, consisting of two parts,
  3168. separated by a double-colon. The first part is a filename and
  3169. the second part is a headline.
  3170. When the filename is omitted, archiving happens in the same file.
  3171. %s in the filename will be replaced by the current file
  3172. name (without the directory part). Archiving to a different file
  3173. is useful to keep archived entries from contributing to the
  3174. Org-mode Agenda.
  3175. The archived entries will be filed as subtrees of the specified
  3176. headline. When the headline is omitted, the subtrees are simply
  3177. filed away at the end of the file, as top-level entries. Also in
  3178. the heading you can use %s to represent the file name, this can be
  3179. useful when using the same archive for a number of different files.
  3180. Here are a few examples:
  3181. \"%s_archive::\"
  3182. If the current file is Projects.org, archive in file
  3183. Projects.org_archive, as top-level trees. This is the default.
  3184. \"::* Archived Tasks\"
  3185. Archive in the current file, under the top-level headline
  3186. \"* Archived Tasks\".
  3187. \"~/org/archive.org::\"
  3188. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3189. \"~/org/archive.org::From %s\"
  3190. Archive in file ~/org/archive.org (absolute path), under headlines
  3191. \"From FILENAME\" where file name is the current file name.
  3192. \"basement::** Finished Tasks\"
  3193. Archive in file ./basement (relative path), as level 3 trees
  3194. below the level 2 heading \"** Finished Tasks\".
  3195. You may set this option on a per-file basis by adding to the buffer a
  3196. line like
  3197. #+ARCHIVE: basement::** Finished Tasks
  3198. You may also define it locally for a subtree by setting an ARCHIVE property
  3199. in the entry. If such a property is found in an entry, or anywhere up
  3200. the hierarchy, it will be used."
  3201. :group 'org-archive
  3202. :type 'string)
  3203. (defcustom org-archive-tag "ARCHIVE"
  3204. "The tag that marks a subtree as archived.
  3205. An archived subtree does not open during visibility cycling, and does
  3206. not contribute to the agenda listings.
  3207. After changing this, font-lock must be restarted in the relevant buffers to
  3208. get the proper fontification."
  3209. :group 'org-archive
  3210. :group 'org-keywords
  3211. :type 'string)
  3212. (defcustom org-agenda-skip-archived-trees t
  3213. "Non-nil means the agenda will skip any items located in archived trees.
  3214. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3215. variable is no longer recommended, you should leave it at the value t.
  3216. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3217. :group 'org-archive
  3218. :group 'org-agenda-skip
  3219. :type 'boolean)
  3220. (defcustom org-columns-skip-archived-trees t
  3221. "Non-nil means ignore archived trees when creating column view."
  3222. :group 'org-archive
  3223. :group 'org-properties
  3224. :type 'boolean)
  3225. (defcustom org-cycle-open-archived-trees nil
  3226. "Non-nil means `org-cycle' will open archived trees.
  3227. An archived tree is a tree marked with the tag ARCHIVE.
  3228. When nil, archived trees will stay folded. You can still open them with
  3229. normal outline commands like `show-all', but not with the cycling commands."
  3230. :group 'org-archive
  3231. :group 'org-cycle
  3232. :type 'boolean)
  3233. (defcustom org-sparse-tree-open-archived-trees nil
  3234. "Non-nil means sparse tree construction shows matches in archived trees.
  3235. When nil, matches in these trees are highlighted, but the trees are kept in
  3236. collapsed state."
  3237. :group 'org-archive
  3238. :group 'org-sparse-trees
  3239. :type 'boolean)
  3240. (defun org-cycle-hide-archived-subtrees (state)
  3241. "Re-hide all archived subtrees after a visibility state change."
  3242. (when (and (not org-cycle-open-archived-trees)
  3243. (not (memq state '(overview folded))))
  3244. (save-excursion
  3245. (let* ((globalp (memq state '(contents all)))
  3246. (beg (if globalp (point-min) (point)))
  3247. (end (if globalp (point-max) (org-end-of-subtree t))))
  3248. (org-hide-archived-subtrees beg end)
  3249. (goto-char beg)
  3250. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3251. (message "%s" (substitute-command-keys
  3252. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3253. (defun org-force-cycle-archived ()
  3254. "Cycle subtree even if it is archived."
  3255. (interactive)
  3256. (setq this-command 'org-cycle)
  3257. (let ((org-cycle-open-archived-trees t))
  3258. (call-interactively 'org-cycle)))
  3259. (defun org-hide-archived-subtrees (beg end)
  3260. "Re-hide all archived subtrees after a visibility state change."
  3261. (save-excursion
  3262. (let* ((re (concat ":" org-archive-tag ":")))
  3263. (goto-char beg)
  3264. (while (re-search-forward re end t)
  3265. (when (org-on-heading-p)
  3266. (org-flag-subtree t)
  3267. (org-end-of-subtree t))))))
  3268. (defun org-flag-subtree (flag)
  3269. (save-excursion
  3270. (org-back-to-heading t)
  3271. (outline-end-of-heading)
  3272. (outline-flag-region (point)
  3273. (progn (org-end-of-subtree t) (point))
  3274. flag)))
  3275. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3276. (eval-and-compile
  3277. (org-autoload "org-archive"
  3278. '(org-add-archive-files org-archive-subtree
  3279. org-archive-to-archive-sibling org-toggle-archive-tag
  3280. org-archive-subtree-default
  3281. org-archive-subtree-default-with-confirmation)))
  3282. ;; Autoload Column View Code
  3283. (declare-function org-columns-number-to-string "org-colview")
  3284. (declare-function org-columns-get-format-and-top-level "org-colview")
  3285. (declare-function org-columns-compute "org-colview")
  3286. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3287. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3288. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3289. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3290. ;; Autoload ID code
  3291. (declare-function org-id-store-link "org-id")
  3292. (declare-function org-id-locations-load "org-id")
  3293. (declare-function org-id-locations-save "org-id")
  3294. (defvar org-id-track-globally)
  3295. (org-autoload "org-id"
  3296. '(org-id-get-create org-id-new org-id-copy org-id-get
  3297. org-id-get-with-outline-path-completion
  3298. org-id-get-with-outline-drilling
  3299. org-id-goto org-id-find org-id-store-link))
  3300. ;; Autoload Plotting Code
  3301. (org-autoload "org-plot"
  3302. '(org-plot/gnuplot))
  3303. ;;; Variables for pre-computed regular expressions, all buffer local
  3304. (defvar org-drawer-regexp nil
  3305. "Matches first line of a hidden block.")
  3306. (make-variable-buffer-local 'org-drawer-regexp)
  3307. (defvar org-todo-regexp nil
  3308. "Matches any of the TODO state keywords.")
  3309. (make-variable-buffer-local 'org-todo-regexp)
  3310. (defvar org-not-done-regexp nil
  3311. "Matches any of the TODO state keywords except the last one.")
  3312. (make-variable-buffer-local 'org-not-done-regexp)
  3313. (defvar org-not-done-heading-regexp nil
  3314. "Matches a TODO headline that is not done.")
  3315. (make-variable-buffer-local 'org-not-done-regexp)
  3316. (defvar org-todo-line-regexp nil
  3317. "Matches a headline and puts TODO state into group 2 if present.")
  3318. (make-variable-buffer-local 'org-todo-line-regexp)
  3319. (defvar org-complex-heading-regexp nil
  3320. "Matches a headline and puts everything into groups:
  3321. group 1: the stars
  3322. group 2: The todo keyword, maybe
  3323. group 3: Priority cookie
  3324. group 4: True headline
  3325. group 5: Tags")
  3326. (make-variable-buffer-local 'org-complex-heading-regexp)
  3327. (defvar org-complex-heading-regexp-format nil)
  3328. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3329. (defvar org-todo-line-tags-regexp nil
  3330. "Matches a headline and puts TODO state into group 2 if present.
  3331. Also put tags into group 4 if tags are present.")
  3332. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3333. (defvar org-nl-done-regexp nil
  3334. "Matches newline followed by a headline with the DONE keyword.")
  3335. (make-variable-buffer-local 'org-nl-done-regexp)
  3336. (defvar org-looking-at-done-regexp nil
  3337. "Matches the DONE keyword a point.")
  3338. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3339. (defvar org-ds-keyword-length 12
  3340. "Maximum length of the Deadline and SCHEDULED keywords.")
  3341. (make-variable-buffer-local 'org-ds-keyword-length)
  3342. (defvar org-deadline-regexp nil
  3343. "Matches the DEADLINE keyword.")
  3344. (make-variable-buffer-local 'org-deadline-regexp)
  3345. (defvar org-deadline-time-regexp nil
  3346. "Matches the DEADLINE keyword together with a time stamp.")
  3347. (make-variable-buffer-local 'org-deadline-time-regexp)
  3348. (defvar org-deadline-line-regexp nil
  3349. "Matches the DEADLINE keyword and the rest of the line.")
  3350. (make-variable-buffer-local 'org-deadline-line-regexp)
  3351. (defvar org-scheduled-regexp nil
  3352. "Matches the SCHEDULED keyword.")
  3353. (make-variable-buffer-local 'org-scheduled-regexp)
  3354. (defvar org-scheduled-time-regexp nil
  3355. "Matches the SCHEDULED keyword together with a time stamp.")
  3356. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3357. (defvar org-closed-time-regexp nil
  3358. "Matches the CLOSED keyword together with a time stamp.")
  3359. (make-variable-buffer-local 'org-closed-time-regexp)
  3360. (defvar org-keyword-time-regexp nil
  3361. "Matches any of the 4 keywords, together with the time stamp.")
  3362. (make-variable-buffer-local 'org-keyword-time-regexp)
  3363. (defvar org-keyword-time-not-clock-regexp nil
  3364. "Matches any of the 3 keywords, together with the time stamp.")
  3365. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3366. (defvar org-maybe-keyword-time-regexp nil
  3367. "Matches a timestamp, possibly preceeded by a keyword.")
  3368. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3369. (defvar org-planning-or-clock-line-re nil
  3370. "Matches a line with planning or clock info.")
  3371. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3372. (defvar org-all-time-keywords nil
  3373. "List of time keywords.")
  3374. (make-variable-buffer-local 'org-all-time-keywords)
  3375. (defconst org-plain-time-of-day-regexp
  3376. (concat
  3377. "\\(\\<[012]?[0-9]"
  3378. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3379. "\\(--?"
  3380. "\\(\\<[012]?[0-9]"
  3381. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3382. "\\)?")
  3383. "Regular expression to match a plain time or time range.
  3384. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3385. groups carry important information:
  3386. 0 the full match
  3387. 1 the first time, range or not
  3388. 8 the second time, if it is a range.")
  3389. (defconst org-plain-time-extension-regexp
  3390. (concat
  3391. "\\(\\<[012]?[0-9]"
  3392. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3393. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3394. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3395. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3396. groups carry important information:
  3397. 0 the full match
  3398. 7 hours of duration
  3399. 9 minutes of duration")
  3400. (defconst org-stamp-time-of-day-regexp
  3401. (concat
  3402. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3403. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3404. "\\(--?"
  3405. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3406. "Regular expression to match a timestamp time or time range.
  3407. After a match, the following groups carry important information:
  3408. 0 the full match
  3409. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3410. 2 the first time, range or not
  3411. 4 the second time, if it is a range.")
  3412. (defconst org-startup-options
  3413. '(("fold" org-startup-folded t)
  3414. ("overview" org-startup-folded t)
  3415. ("nofold" org-startup-folded nil)
  3416. ("showall" org-startup-folded nil)
  3417. ("showeverything" org-startup-folded showeverything)
  3418. ("content" org-startup-folded content)
  3419. ("indent" org-startup-indented t)
  3420. ("noindent" org-startup-indented nil)
  3421. ("hidestars" org-hide-leading-stars t)
  3422. ("showstars" org-hide-leading-stars nil)
  3423. ("odd" org-odd-levels-only t)
  3424. ("oddeven" org-odd-levels-only nil)
  3425. ("align" org-startup-align-all-tables t)
  3426. ("noalign" org-startup-align-all-tables nil)
  3427. ("customtime" org-display-custom-times t)
  3428. ("logdone" org-log-done time)
  3429. ("lognotedone" org-log-done note)
  3430. ("nologdone" org-log-done nil)
  3431. ("lognoteclock-out" org-log-note-clock-out t)
  3432. ("nolognoteclock-out" org-log-note-clock-out nil)
  3433. ("logrepeat" org-log-repeat state)
  3434. ("lognoterepeat" org-log-repeat note)
  3435. ("nologrepeat" org-log-repeat nil)
  3436. ("logreschedule" org-log-reschedule time)
  3437. ("lognotereschedule" org-log-reschedule note)
  3438. ("nologreschedule" org-log-reschedule nil)
  3439. ("logredeadline" org-log-redeadline time)
  3440. ("lognoteredeadline" org-log-redeadline note)
  3441. ("nologredeadline" org-log-redeadline nil)
  3442. ("logrefile" org-log-refile time)
  3443. ("lognoterefile" org-log-refile note)
  3444. ("nologrefile" org-log-refile nil)
  3445. ("fninline" org-footnote-define-inline t)
  3446. ("nofninline" org-footnote-define-inline nil)
  3447. ("fnlocal" org-footnote-section nil)
  3448. ("fnauto" org-footnote-auto-label t)
  3449. ("fnprompt" org-footnote-auto-label nil)
  3450. ("fnconfirm" org-footnote-auto-label confirm)
  3451. ("fnplain" org-footnote-auto-label plain)
  3452. ("fnadjust" org-footnote-auto-adjust t)
  3453. ("nofnadjust" org-footnote-auto-adjust nil)
  3454. ("constcgs" constants-unit-system cgs)
  3455. ("constSI" constants-unit-system SI)
  3456. ("noptag" org-tag-persistent-alist nil)
  3457. ("hideblocks" org-hide-block-startup t)
  3458. ("nohideblocks" org-hide-block-startup nil)
  3459. ("beamer" org-startup-with-beamer-mode t))
  3460. "Variable associated with STARTUP options for org-mode.
  3461. Each element is a list of three items: The startup options as written
  3462. in the #+STARTUP line, the corresponding variable, and the value to
  3463. set this variable to if the option is found. An optional forth element PUSH
  3464. means to push this value onto the list in the variable.")
  3465. (defun org-set-regexps-and-options ()
  3466. "Precompute regular expressions for current buffer."
  3467. (when (org-mode-p)
  3468. (org-set-local 'org-todo-kwd-alist nil)
  3469. (org-set-local 'org-todo-key-alist nil)
  3470. (org-set-local 'org-todo-key-trigger nil)
  3471. (org-set-local 'org-todo-keywords-1 nil)
  3472. (org-set-local 'org-done-keywords nil)
  3473. (org-set-local 'org-todo-heads nil)
  3474. (org-set-local 'org-todo-sets nil)
  3475. (org-set-local 'org-todo-log-states nil)
  3476. (org-set-local 'org-file-properties nil)
  3477. (org-set-local 'org-file-tags nil)
  3478. (let ((re (org-make-options-regexp
  3479. '("CATEGORY" "TODO" "COLUMNS"
  3480. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3481. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS")
  3482. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3483. (splitre "[ \t]+")
  3484. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3485. tail sep kws1 prio props ftags drawers beamer-p
  3486. ext-setup-or-nil setup-contents (start 0))
  3487. (save-excursion
  3488. (save-restriction
  3489. (widen)
  3490. (goto-char (point-min))
  3491. (while (or (and ext-setup-or-nil
  3492. (string-match re ext-setup-or-nil start)
  3493. (setq start (match-end 0)))
  3494. (and (setq ext-setup-or-nil nil start 0)
  3495. (re-search-forward re nil t)))
  3496. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3497. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3498. (cond
  3499. ((equal key "CATEGORY")
  3500. (if (string-match "[ \t]+$" value)
  3501. (setq value (replace-match "" t t value)))
  3502. (setq cat value))
  3503. ((member key '("SEQ_TODO" "TODO"))
  3504. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3505. ((equal key "TYP_TODO")
  3506. (push (cons 'type (org-split-string value splitre)) kwds))
  3507. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3508. ;; general TODO-like setup
  3509. (push (cons (intern (downcase (match-string 1 key)))
  3510. (org-split-string value splitre)) kwds))
  3511. ((equal key "TAGS")
  3512. (setq tags (append tags (if tags '("\\n") nil)
  3513. (org-split-string value splitre))))
  3514. ((equal key "COLUMNS")
  3515. (org-set-local 'org-columns-default-format value))
  3516. ((equal key "LINK")
  3517. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3518. (push (cons (match-string 1 value)
  3519. (org-trim (match-string 2 value)))
  3520. links)))
  3521. ((equal key "PRIORITIES")
  3522. (setq prio (org-split-string value " +")))
  3523. ((equal key "PROPERTY")
  3524. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3525. (push (cons (match-string 1 value) (match-string 2 value))
  3526. props)))
  3527. ((equal key "FILETAGS")
  3528. (when (string-match "\\S-" value)
  3529. (setq ftags
  3530. (append
  3531. ftags
  3532. (apply 'append
  3533. (mapcar (lambda (x) (org-split-string x ":"))
  3534. (org-split-string value)))))))
  3535. ((equal key "DRAWERS")
  3536. (setq drawers (org-split-string value splitre)))
  3537. ((equal key "CONSTANTS")
  3538. (setq const (append const (org-split-string value splitre))))
  3539. ((equal key "STARTUP")
  3540. (let ((opts (org-split-string value splitre))
  3541. l var val)
  3542. (while (setq l (pop opts))
  3543. (when (setq l (assoc l org-startup-options))
  3544. (setq var (nth 1 l) val (nth 2 l))
  3545. (if (not (nth 3 l))
  3546. (set (make-local-variable var) val)
  3547. (if (not (listp (symbol-value var)))
  3548. (set (make-local-variable var) nil))
  3549. (set (make-local-variable var) (symbol-value var))
  3550. (add-to-list var val))))))
  3551. ((equal key "ARCHIVE")
  3552. (string-match " *$" value)
  3553. (setq arch (replace-match "" t t value))
  3554. (remove-text-properties 0 (length arch)
  3555. '(face t fontified t) arch))
  3556. ((equal key "LATEX_CLASS")
  3557. (setq beamer-p (equal value "beamer")))
  3558. ((equal key "SETUPFILE")
  3559. (setq setup-contents (org-file-contents
  3560. (expand-file-name
  3561. (org-remove-double-quotes value))
  3562. 'noerror))
  3563. (if (not ext-setup-or-nil)
  3564. (setq ext-setup-or-nil setup-contents start 0)
  3565. (setq ext-setup-or-nil
  3566. (concat (substring ext-setup-or-nil 0 start)
  3567. "\n" setup-contents "\n"
  3568. (substring ext-setup-or-nil start)))))
  3569. ))))
  3570. (when cat
  3571. (org-set-local 'org-category (intern cat))
  3572. (push (cons "CATEGORY" cat) props))
  3573. (when prio
  3574. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3575. (setq prio (mapcar 'string-to-char prio))
  3576. (org-set-local 'org-highest-priority (nth 0 prio))
  3577. (org-set-local 'org-lowest-priority (nth 1 prio))
  3578. (org-set-local 'org-default-priority (nth 2 prio)))
  3579. (and props (org-set-local 'org-file-properties (nreverse props)))
  3580. (and ftags (org-set-local 'org-file-tags
  3581. (mapcar 'org-add-prop-inherited ftags)))
  3582. (and drawers (org-set-local 'org-drawers drawers))
  3583. (and arch (org-set-local 'org-archive-location arch))
  3584. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3585. ;; Process the TODO keywords
  3586. (unless kwds
  3587. ;; Use the global values as if they had been given locally.
  3588. (setq kwds (default-value 'org-todo-keywords))
  3589. (if (stringp (car kwds))
  3590. (setq kwds (list (cons org-todo-interpretation
  3591. (default-value 'org-todo-keywords)))))
  3592. (setq kwds (reverse kwds)))
  3593. (setq kwds (nreverse kwds))
  3594. (let (inter kws kw)
  3595. (while (setq kws (pop kwds))
  3596. (let ((kws (or
  3597. (run-hook-with-args-until-success
  3598. 'org-todo-setup-filter-hook kws)
  3599. kws)))
  3600. (setq inter (pop kws) sep (member "|" kws)
  3601. kws0 (delete "|" (copy-sequence kws))
  3602. kwsa nil
  3603. kws1 (mapcar
  3604. (lambda (x)
  3605. ;; 1 2
  3606. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3607. (progn
  3608. (setq kw (match-string 1 x)
  3609. key (and (match-end 2) (match-string 2 x))
  3610. log (org-extract-log-state-settings x))
  3611. (push (cons kw (and key (string-to-char key))) kwsa)
  3612. (and log (push log org-todo-log-states))
  3613. kw)
  3614. (error "Invalid TODO keyword %s" x)))
  3615. kws0)
  3616. kwsa (if kwsa (append '((:startgroup))
  3617. (nreverse kwsa)
  3618. '((:endgroup))))
  3619. hw (car kws1)
  3620. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3621. tail (list inter hw (car dws) (org-last dws))))
  3622. (add-to-list 'org-todo-heads hw 'append)
  3623. (push kws1 org-todo-sets)
  3624. (setq org-done-keywords (append org-done-keywords dws nil))
  3625. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3626. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3627. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3628. (setq org-todo-sets (nreverse org-todo-sets)
  3629. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3630. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3631. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3632. ;; Process the constants
  3633. (when const
  3634. (let (e cst)
  3635. (while (setq e (pop const))
  3636. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3637. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3638. (setq org-table-formula-constants-local cst)))
  3639. ;; Process the tags.
  3640. (when tags
  3641. (let (e tgs)
  3642. (while (setq e (pop tags))
  3643. (cond
  3644. ((equal e "{") (push '(:startgroup) tgs))
  3645. ((equal e "}") (push '(:endgroup) tgs))
  3646. ((equal e "\\n") (push '(:newline) tgs))
  3647. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3648. (push (cons (match-string 1 e)
  3649. (string-to-char (match-string 2 e)))
  3650. tgs))
  3651. (t (push (list e) tgs))))
  3652. (org-set-local 'org-tag-alist nil)
  3653. (while (setq e (pop tgs))
  3654. (or (and (stringp (car e))
  3655. (assoc (car e) org-tag-alist))
  3656. (push e org-tag-alist)))))
  3657. ;; Compute the regular expressions and other local variables
  3658. (if (not org-done-keywords)
  3659. (setq org-done-keywords (and org-todo-keywords-1
  3660. (list (org-last org-todo-keywords-1)))))
  3661. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3662. (length org-scheduled-string)
  3663. (length org-clock-string)
  3664. (length org-closed-string)))
  3665. org-drawer-regexp
  3666. (concat "^[ \t]*:\\("
  3667. (mapconcat 'regexp-quote org-drawers "\\|")
  3668. "\\):[ \t]*$")
  3669. org-not-done-keywords
  3670. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3671. org-todo-regexp
  3672. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3673. "\\|") "\\)\\>")
  3674. org-not-done-regexp
  3675. (concat "\\<\\("
  3676. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3677. "\\)\\>")
  3678. org-not-done-heading-regexp
  3679. (concat "^\\(\\*+\\)[ \t]+\\("
  3680. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3681. "\\)\\>")
  3682. org-todo-line-regexp
  3683. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3684. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3685. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3686. org-complex-heading-regexp
  3687. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3688. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3689. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3690. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3691. org-complex-heading-regexp-format
  3692. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3693. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3694. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
  3695. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3696. org-nl-done-regexp
  3697. (concat "\n\\*+[ \t]+"
  3698. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3699. "\\)" "\\>")
  3700. org-todo-line-tags-regexp
  3701. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3702. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3703. (org-re
  3704. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3705. org-looking-at-done-regexp
  3706. (concat "^" "\\(?:"
  3707. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3708. "\\>")
  3709. org-deadline-regexp (concat "\\<" org-deadline-string)
  3710. org-deadline-time-regexp
  3711. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3712. org-deadline-line-regexp
  3713. (concat "\\<\\(" org-deadline-string "\\).*")
  3714. org-scheduled-regexp
  3715. (concat "\\<" org-scheduled-string)
  3716. org-scheduled-time-regexp
  3717. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3718. org-closed-time-regexp
  3719. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3720. org-keyword-time-regexp
  3721. (concat "\\<\\(" org-scheduled-string
  3722. "\\|" org-deadline-string
  3723. "\\|" org-closed-string
  3724. "\\|" org-clock-string "\\)"
  3725. " *[[<]\\([^]>]+\\)[]>]")
  3726. org-keyword-time-not-clock-regexp
  3727. (concat "\\<\\(" org-scheduled-string
  3728. "\\|" org-deadline-string
  3729. "\\|" org-closed-string
  3730. "\\)"
  3731. " *[[<]\\([^]>]+\\)[]>]")
  3732. org-maybe-keyword-time-regexp
  3733. (concat "\\(\\<\\(" org-scheduled-string
  3734. "\\|" org-deadline-string
  3735. "\\|" org-closed-string
  3736. "\\|" org-clock-string "\\)\\)?"
  3737. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3738. org-planning-or-clock-line-re
  3739. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3740. "\\|" org-deadline-string
  3741. "\\|" org-closed-string "\\|" org-clock-string
  3742. "\\)\\>\\)")
  3743. org-all-time-keywords
  3744. (mapcar (lambda (w) (substring w 0 -1))
  3745. (list org-scheduled-string org-deadline-string
  3746. org-clock-string org-closed-string))
  3747. )
  3748. (org-compute-latex-and-specials-regexp)
  3749. (org-set-font-lock-defaults))))
  3750. (defun org-file-contents (file &optional noerror)
  3751. "Return the contents of FILE, as a string."
  3752. (if (or (not file)
  3753. (not (file-readable-p file)))
  3754. (if noerror
  3755. (progn
  3756. (message "Cannot read file %s" file)
  3757. (ding) (sit-for 2)
  3758. "")
  3759. (error "Cannot read file %s" file))
  3760. (with-temp-buffer
  3761. (insert-file-contents file)
  3762. (buffer-string))))
  3763. (defun org-extract-log-state-settings (x)
  3764. "Extract the log state setting from a TODO keyword string.
  3765. This will extract info from a string like \"WAIT(w@/!)\"."
  3766. (let (kw key log1 log2)
  3767. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3768. (setq kw (match-string 1 x)
  3769. key (and (match-end 2) (match-string 2 x))
  3770. log1 (and (match-end 3) (match-string 3 x))
  3771. log2 (and (match-end 4) (match-string 4 x)))
  3772. (and (or log1 log2)
  3773. (list kw
  3774. (and log1 (if (equal log1 "!") 'time 'note))
  3775. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3776. (defun org-remove-keyword-keys (list)
  3777. "Remove a pair of parenthesis at the end of each string in LIST."
  3778. (mapcar (lambda (x)
  3779. (if (string-match "(.*)$" x)
  3780. (substring x 0 (match-beginning 0))
  3781. x))
  3782. list))
  3783. (defun org-assign-fast-keys (alist)
  3784. "Assign fast keys to a keyword-key alist.
  3785. Respect keys that are already there."
  3786. (let (new e (alt ?0))
  3787. (while (setq e (pop alist))
  3788. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  3789. (cdr e)) ;; Key already assigned.
  3790. (push e new)
  3791. (let ((clist (string-to-list (downcase (car e))))
  3792. (used (append new alist)))
  3793. (when (= (car clist) ?@)
  3794. (pop clist))
  3795. (while (and clist (rassoc (car clist) used))
  3796. (pop clist))
  3797. (unless clist
  3798. (while (rassoc alt used)
  3799. (incf alt)))
  3800. (push (cons (car e) (or (car clist) alt)) new))))
  3801. (nreverse new)))
  3802. ;;; Some variables used in various places
  3803. (defvar org-window-configuration nil
  3804. "Used in various places to store a window configuration.")
  3805. (defvar org-selected-window nil
  3806. "Used in various places to store a window configuration.")
  3807. (defvar org-finish-function nil
  3808. "Function to be called when `C-c C-c' is used.
  3809. This is for getting out of special buffers like remember.")
  3810. ;; FIXME: Occasionally check by commenting these, to make sure
  3811. ;; no other functions uses these, forgetting to let-bind them.
  3812. (defvar entry)
  3813. (defvar last-state)
  3814. (defvar date)
  3815. ;; Defined somewhere in this file, but used before definition.
  3816. (defvar org-entities) ;; defined in org-entities.el
  3817. (defvar org-struct-menu)
  3818. (defvar org-org-menu)
  3819. (defvar org-tbl-menu)
  3820. ;;;; Define the Org-mode
  3821. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3822. (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."))
  3823. ;; We use a before-change function to check if a table might need
  3824. ;; an update.
  3825. (defvar org-table-may-need-update t
  3826. "Indicates that a table might need an update.
  3827. This variable is set by `org-before-change-function'.
  3828. `org-table-align' sets it back to nil.")
  3829. (defun org-before-change-function (beg end)
  3830. "Every change indicates that a table might need an update."
  3831. (setq org-table-may-need-update t))
  3832. (defvar org-mode-map)
  3833. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3834. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  3835. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3836. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3837. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3838. (defvar org-table-buffer-is-an nil)
  3839. (defconst org-outline-regexp "\\*+ ")
  3840. ;;;###autoload
  3841. (define-derived-mode org-mode outline-mode "Org"
  3842. "Outline-based notes management and organizer, alias
  3843. \"Carsten's outline-mode for keeping track of everything.\"
  3844. Org-mode develops organizational tasks around a NOTES file which
  3845. contains information about projects as plain text. Org-mode is
  3846. implemented on top of outline-mode, which is ideal to keep the content
  3847. of large files well structured. It supports ToDo items, deadlines and
  3848. time stamps, which magically appear in the diary listing of the Emacs
  3849. calendar. Tables are easily created with a built-in table editor.
  3850. Plain text URL-like links connect to websites, emails (VM), Usenet
  3851. messages (Gnus), BBDB entries, and any files related to the project.
  3852. For printing and sharing of notes, an Org-mode file (or a part of it)
  3853. can be exported as a structured ASCII or HTML file.
  3854. The following commands are available:
  3855. \\{org-mode-map}"
  3856. ;; Get rid of Outline menus, they are not needed
  3857. ;; Need to do this here because define-derived-mode sets up
  3858. ;; the keymap so late. Still, it is a waste to call this each time
  3859. ;; we switch another buffer into org-mode.
  3860. (if (featurep 'xemacs)
  3861. (when (boundp 'outline-mode-menu-heading)
  3862. ;; Assume this is Greg's port, it uses easymenu
  3863. (easy-menu-remove outline-mode-menu-heading)
  3864. (easy-menu-remove outline-mode-menu-show)
  3865. (easy-menu-remove outline-mode-menu-hide))
  3866. (define-key org-mode-map [menu-bar headings] 'undefined)
  3867. (define-key org-mode-map [menu-bar hide] 'undefined)
  3868. (define-key org-mode-map [menu-bar show] 'undefined))
  3869. (org-load-modules-maybe)
  3870. (easy-menu-add org-org-menu)
  3871. (easy-menu-add org-tbl-menu)
  3872. (org-install-agenda-files-menu)
  3873. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  3874. (add-to-invisibility-spec '(org-cwidth))
  3875. (add-to-invisibility-spec '(org-hide-block . t))
  3876. (when (featurep 'xemacs)
  3877. (org-set-local 'line-move-ignore-invisible t))
  3878. (org-set-local 'outline-regexp org-outline-regexp)
  3879. (org-set-local 'outline-level 'org-outline-level)
  3880. (when (and org-ellipsis
  3881. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3882. (fboundp 'make-glyph-code))
  3883. (unless org-display-table
  3884. (setq org-display-table (make-display-table)))
  3885. (set-display-table-slot
  3886. org-display-table 4
  3887. (vconcat (mapcar
  3888. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3889. org-ellipsis)))
  3890. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3891. (setq buffer-display-table org-display-table))
  3892. (org-set-regexps-and-options)
  3893. (when (and org-tag-faces (not org-tags-special-faces-re))
  3894. ;; tag faces set outside customize.... force initialization.
  3895. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3896. ;; Calc embedded
  3897. (org-set-local 'calc-embedded-open-mode "# ")
  3898. (modify-syntax-entry ?# "<")
  3899. (modify-syntax-entry ?@ "w")
  3900. (if org-startup-truncated (setq truncate-lines t))
  3901. (org-set-local 'font-lock-unfontify-region-function
  3902. 'org-unfontify-region)
  3903. ;; Activate before-change-function
  3904. (org-set-local 'org-table-may-need-update t)
  3905. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3906. 'local)
  3907. ;; Check for running clock before killing a buffer
  3908. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3909. ;; Paragraphs and auto-filling
  3910. (org-set-autofill-regexps)
  3911. (setq indent-line-function 'org-indent-line-function)
  3912. (org-update-radio-target-regexp)
  3913. ;; Make sure dependence stuff works reliably, even for users who set it
  3914. ;; too late :-(
  3915. (if org-enforce-todo-dependencies
  3916. (add-hook 'org-blocker-hook
  3917. 'org-block-todo-from-children-or-siblings-or-parent)
  3918. (remove-hook 'org-blocker-hook
  3919. 'org-block-todo-from-children-or-siblings-or-parent))
  3920. (if org-enforce-todo-checkbox-dependencies
  3921. (add-hook 'org-blocker-hook
  3922. 'org-block-todo-from-checkboxes)
  3923. (remove-hook 'org-blocker-hook
  3924. 'org-block-todo-from-checkboxes))
  3925. ;; Comment characters
  3926. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3927. (org-set-local 'comment-padding " ")
  3928. ;; Align options lines
  3929. (org-set-local
  3930. 'align-mode-rules-list
  3931. '((org-in-buffer-settings
  3932. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3933. (modes . '(org-mode)))))
  3934. ;; Imenu
  3935. (org-set-local 'imenu-create-index-function
  3936. 'org-imenu-get-tree)
  3937. ;; Make isearch reveal context
  3938. (if (or (featurep 'xemacs)
  3939. (not (boundp 'outline-isearch-open-invisible-function)))
  3940. ;; Emacs 21 and XEmacs make use of the hook
  3941. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3942. ;; Emacs 22 deals with this through a special variable
  3943. (org-set-local 'outline-isearch-open-invisible-function
  3944. (lambda (&rest ignore) (org-show-context 'isearch))))
  3945. ;; Turn on org-beamer-mode?
  3946. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  3947. ;; If empty file that did not turn on org-mode automatically, make it to.
  3948. (if (and org-insert-mode-line-in-empty-file
  3949. (interactive-p)
  3950. (= (point-min) (point-max)))
  3951. (insert "# -*- mode: org -*-\n\n"))
  3952. (unless org-inhibit-startup
  3953. (when org-startup-align-all-tables
  3954. (let ((bmp (buffer-modified-p)))
  3955. (org-table-map-tables 'org-table-align 'quietly)
  3956. (set-buffer-modified-p bmp)))
  3957. (when org-startup-indented
  3958. (require 'org-indent)
  3959. (org-indent-mode 1))
  3960. (unless org-inhibit-startup-visibility-stuff
  3961. (org-set-startup-visibility))))
  3962. (when (fboundp 'abbrev-table-put)
  3963. (abbrev-table-put org-mode-abbrev-table
  3964. :parents (list text-mode-abbrev-table)))
  3965. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3966. (defun org-current-time ()
  3967. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3968. (if (> (car org-time-stamp-rounding-minutes) 1)
  3969. (let ((r (car org-time-stamp-rounding-minutes))
  3970. (time (decode-time)))
  3971. (apply 'encode-time
  3972. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3973. (nthcdr 2 time))))
  3974. (current-time)))
  3975. ;;;; Font-Lock stuff, including the activators
  3976. (defvar org-mouse-map (make-sparse-keymap))
  3977. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  3978. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  3979. (when org-mouse-1-follows-link
  3980. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3981. (when org-tab-follows-link
  3982. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3983. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3984. (require 'font-lock)
  3985. (defconst org-non-link-chars "]\t\n\r<>")
  3986. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3987. "shell" "elisp" "doi"))
  3988. (defvar org-link-types-re nil
  3989. "Matches a link that has a url-like prefix like \"http:\"")
  3990. (defvar org-link-re-with-space nil
  3991. "Matches a link with spaces, optional angular brackets around it.")
  3992. (defvar org-link-re-with-space2 nil
  3993. "Matches a link with spaces, optional angular brackets around it.")
  3994. (defvar org-link-re-with-space3 nil
  3995. "Matches a link with spaces, only for internal part in bracket links.")
  3996. (defvar org-angle-link-re nil
  3997. "Matches link with angular brackets, spaces are allowed.")
  3998. (defvar org-plain-link-re nil
  3999. "Matches plain link, without spaces.")
  4000. (defvar org-bracket-link-regexp nil
  4001. "Matches a link in double brackets.")
  4002. (defvar org-bracket-link-analytic-regexp nil
  4003. "Regular expression used to analyze links.
  4004. Here is what the match groups contain after a match:
  4005. 1: http:
  4006. 2: http
  4007. 3: path
  4008. 4: [desc]
  4009. 5: desc")
  4010. (defvar org-bracket-link-analytic-regexp++ nil
  4011. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  4012. (defvar org-any-link-re nil
  4013. "Regular expression matching any link.")
  4014. (defun org-make-link-regexps ()
  4015. "Update the link regular expressions.
  4016. This should be called after the variable `org-link-types' has changed."
  4017. (setq org-link-types-re
  4018. (concat
  4019. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4020. org-link-re-with-space
  4021. (concat
  4022. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4023. "\\([^" org-non-link-chars " ]"
  4024. "[^" org-non-link-chars "]*"
  4025. "[^" org-non-link-chars " ]\\)>?")
  4026. org-link-re-with-space2
  4027. (concat
  4028. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4029. "\\([^" org-non-link-chars " ]"
  4030. "[^\t\n\r]*"
  4031. "[^" org-non-link-chars " ]\\)>?")
  4032. org-link-re-with-space3
  4033. (concat
  4034. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4035. "\\([^" org-non-link-chars " ]"
  4036. "[^\t\n\r]*\\)")
  4037. org-angle-link-re
  4038. (concat
  4039. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4040. "\\([^" org-non-link-chars " ]"
  4041. "[^" org-non-link-chars "]*"
  4042. "\\)>")
  4043. org-plain-link-re
  4044. (concat
  4045. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4046. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4047. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4048. org-bracket-link-regexp
  4049. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4050. org-bracket-link-analytic-regexp
  4051. (concat
  4052. "\\[\\["
  4053. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4054. "\\([^]]+\\)"
  4055. "\\]"
  4056. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4057. "\\]")
  4058. org-bracket-link-analytic-regexp++
  4059. (concat
  4060. "\\[\\["
  4061. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4062. "\\([^]]+\\)"
  4063. "\\]"
  4064. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4065. "\\]")
  4066. org-any-link-re
  4067. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4068. org-angle-link-re "\\)\\|\\("
  4069. org-plain-link-re "\\)")))
  4070. (org-make-link-regexps)
  4071. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4072. "Regular expression for fast time stamp matching.")
  4073. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4074. "Regular expression for fast time stamp matching.")
  4075. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4076. "Regular expression matching time strings for analysis.
  4077. This one does not require the space after the date, so it can be used
  4078. on a string that terminates immediately after the date.")
  4079. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4080. "Regular expression matching time strings for analysis.")
  4081. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4082. "Regular expression matching time stamps, with groups.")
  4083. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4084. "Regular expression matching time stamps (also [..]), with groups.")
  4085. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4086. "Regular expression matching a time stamp range.")
  4087. (defconst org-tr-regexp-both
  4088. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4089. "Regular expression matching a time stamp range.")
  4090. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4091. org-ts-regexp "\\)?")
  4092. "Regular expression matching a time stamp or time stamp range.")
  4093. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4094. org-ts-regexp-both "\\)?")
  4095. "Regular expression matching a time stamp or time stamp range.
  4096. The time stamps may be either active or inactive.")
  4097. (defvar org-emph-face nil)
  4098. (defun org-do-emphasis-faces (limit)
  4099. "Run through the buffer and add overlays to links."
  4100. (let (rtn a)
  4101. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4102. (if (not (= (char-after (match-beginning 3))
  4103. (char-after (match-beginning 4))))
  4104. (progn
  4105. (setq rtn t)
  4106. (setq a (assoc (match-string 3) org-emphasis-alist))
  4107. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4108. 'face
  4109. (nth 1 a))
  4110. (and (nth 4 a)
  4111. (org-remove-flyspell-overlays-in
  4112. (match-beginning 0) (match-end 0)))
  4113. (add-text-properties (match-beginning 2) (match-end 2)
  4114. '(font-lock-multiline t))
  4115. (when org-hide-emphasis-markers
  4116. (add-text-properties (match-end 4) (match-beginning 5)
  4117. '(invisible org-link))
  4118. (add-text-properties (match-beginning 3) (match-end 3)
  4119. '(invisible org-link)))))
  4120. (backward-char 1))
  4121. rtn))
  4122. (defun org-emphasize (&optional char)
  4123. "Insert or change an emphasis, i.e. a font like bold or italic.
  4124. If there is an active region, change that region to a new emphasis.
  4125. If there is no region, just insert the marker characters and position
  4126. the cursor between them.
  4127. CHAR should be either the marker character, or the first character of the
  4128. HTML tag associated with that emphasis. If CHAR is a space, the means
  4129. to remove the emphasis of the selected region.
  4130. If char is not given (for example in an interactive call) it
  4131. will be prompted for."
  4132. (interactive)
  4133. (let ((eal org-emphasis-alist) e det
  4134. (erc org-emphasis-regexp-components)
  4135. (prompt "")
  4136. (string "") beg end move tag c s)
  4137. (if (org-region-active-p)
  4138. (setq beg (region-beginning) end (region-end)
  4139. string (buffer-substring beg end))
  4140. (setq move t))
  4141. (while (setq e (pop eal))
  4142. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4143. c (aref tag 0))
  4144. (push (cons c (string-to-char (car e))) det)
  4145. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4146. (substring tag 1)))))
  4147. (setq det (nreverse det))
  4148. (unless char
  4149. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4150. (setq char (read-char-exclusive)))
  4151. (setq char (or (cdr (assoc char det)) char))
  4152. (if (equal char ?\ )
  4153. (setq s "" move nil)
  4154. (unless (assoc (char-to-string char) org-emphasis-alist)
  4155. (error "No such emphasis marker: \"%c\"" char))
  4156. (setq s (char-to-string char)))
  4157. (while (and (> (length string) 1)
  4158. (equal (substring string 0 1) (substring string -1))
  4159. (assoc (substring string 0 1) org-emphasis-alist))
  4160. (setq string (substring string 1 -1)))
  4161. (setq string (concat s string s))
  4162. (if beg (delete-region beg end))
  4163. (unless (or (bolp)
  4164. (string-match (concat "[" (nth 0 erc) "\n]")
  4165. (char-to-string (char-before (point)))))
  4166. (insert " "))
  4167. (unless (or (eobp)
  4168. (string-match (concat "[" (nth 1 erc) "\n]")
  4169. (char-to-string (char-after (point)))))
  4170. (insert " ") (backward-char 1))
  4171. (insert string)
  4172. (and move (backward-char 1))))
  4173. (defconst org-nonsticky-props
  4174. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4175. (defsubst org-rear-nonsticky-at (pos)
  4176. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4177. (defun org-activate-plain-links (limit)
  4178. "Run through the buffer and add overlays to links."
  4179. (catch 'exit
  4180. (let (f)
  4181. (if (re-search-forward org-plain-link-re limit t)
  4182. (progn
  4183. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4184. (setq f (get-text-property (match-beginning 0) 'face))
  4185. (if (or (eq f 'org-tag)
  4186. (and (listp f) (memq 'org-tag f)))
  4187. nil
  4188. (add-text-properties (match-beginning 0) (match-end 0)
  4189. (list 'mouse-face 'highlight
  4190. 'face 'org-link
  4191. 'keymap org-mouse-map))
  4192. (org-rear-nonsticky-at (match-end 0)))
  4193. t)))))
  4194. (defun org-activate-code (limit)
  4195. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4196. (progn
  4197. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4198. (remove-text-properties (match-beginning 0) (match-end 0)
  4199. '(display t invisible t intangible t))
  4200. t)))
  4201. (defun org-fontify-meta-lines-and-blocks (limit)
  4202. "Fontify #+ lines and blocks, in the correct ways."
  4203. (let ((case-fold-search t))
  4204. (if (re-search-forward
  4205. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  4206. limit t)
  4207. (let ((beg (match-beginning 0))
  4208. (beg1 (line-beginning-position 2))
  4209. (dc1 (downcase (match-string 2)))
  4210. (dc3 (downcase (match-string 3)))
  4211. end end1 quoting block-type)
  4212. (cond
  4213. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4214. ;; a single line of backend-specific content
  4215. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4216. (remove-text-properties (match-beginning 0) (match-end 0)
  4217. '(display t invisible t intangible t))
  4218. (add-text-properties (match-beginning 1) (match-end 3)
  4219. '(font-lock-fontified t face org-meta-line))
  4220. (add-text-properties (match-beginning 6) (match-end 6)
  4221. '(font-lock-fontified t face org-block))
  4222. t)
  4223. ((and (match-end 4) (equal dc3 "begin"))
  4224. ;; Truly a block
  4225. (setq block-type (downcase (match-string 5))
  4226. quoting (member block-type org-protecting-blocks))
  4227. (when (re-search-forward
  4228. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4229. nil t) ;; on purpose, we look further than LIMIT
  4230. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4231. (when quoting
  4232. (remove-text-properties beg end
  4233. '(display t invisible t intangible t)))
  4234. (add-text-properties
  4235. beg end
  4236. '(font-lock-fontified t font-lock-multiline t))
  4237. (add-text-properties beg beg1 '(face org-meta-line))
  4238. (add-text-properties end1 end '(face org-meta-line))
  4239. (cond
  4240. (quoting
  4241. (add-text-properties beg1 end1 '(face org-block)))
  4242. ((not org-fontify-quote-and-verse-blocks))
  4243. ((string= block-type "quote")
  4244. (add-text-properties beg1 end1 '(face org-quote)))
  4245. ((string= block-type "verse")
  4246. (add-text-properties beg1 end1 '(face org-verse))))
  4247. t))
  4248. ((member dc1 '("title:" "author:" "email:" "date:"))
  4249. (add-text-properties
  4250. beg (match-end 3)
  4251. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4252. '(font-lock-fontified t invisible t)
  4253. '(font-lock-fontified t face org-document-info-keyword)))
  4254. (add-text-properties
  4255. (match-beginning 6) (match-end 6)
  4256. (if (string-equal dc1 "title:")
  4257. '(font-lock-fontified t face org-document-title)
  4258. '(font-lock-fontified t face org-document-info))))
  4259. ((not (member (char-after beg) '(?\ ?\t)))
  4260. ;; just any other in-buffer setting, but not indented
  4261. (add-text-properties
  4262. beg (match-end 0)
  4263. '(font-lock-fontified t face org-meta-line))
  4264. t)
  4265. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4266. "orgtbl:" "tblfm:" "tblname:"))
  4267. (and (match-end 4) (equal dc3 "attr")))
  4268. (add-text-properties
  4269. beg (match-end 0)
  4270. '(font-lock-fontified t face org-meta-line))
  4271. t)
  4272. ((member dc3 '(" " ""))
  4273. (add-text-properties
  4274. beg (match-end 0)
  4275. '(font-lock-fontified t face font-lock-comment-face)))
  4276. (t nil))))))
  4277. (defun org-activate-angle-links (limit)
  4278. "Run through the buffer and add overlays to links."
  4279. (if (re-search-forward org-angle-link-re limit t)
  4280. (progn
  4281. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4282. (add-text-properties (match-beginning 0) (match-end 0)
  4283. (list 'mouse-face 'highlight
  4284. 'keymap org-mouse-map))
  4285. (org-rear-nonsticky-at (match-end 0))
  4286. t)))
  4287. (defun org-activate-footnote-links (limit)
  4288. "Run through the buffer and add overlays to links."
  4289. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4290. limit t)
  4291. (progn
  4292. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4293. (add-text-properties (match-beginning 2) (match-end 2)
  4294. (list 'mouse-face 'highlight
  4295. 'keymap org-mouse-map
  4296. 'help-echo
  4297. (if (= (point-at-bol) (match-beginning 2))
  4298. "Footnote definition"
  4299. "Footnote reference")
  4300. ))
  4301. (org-rear-nonsticky-at (match-end 2))
  4302. t)))
  4303. (defun org-activate-bracket-links (limit)
  4304. "Run through the buffer and add overlays to bracketed links."
  4305. (if (re-search-forward org-bracket-link-regexp limit t)
  4306. (let* ((help (concat "LINK: "
  4307. (org-match-string-no-properties 1)))
  4308. ;; FIXME: above we should remove the escapes.
  4309. ;; but that requires another match, protecting match data,
  4310. ;; a lot of overhead for font-lock.
  4311. (ip (org-maybe-intangible
  4312. (list 'invisible 'org-link
  4313. 'keymap org-mouse-map 'mouse-face 'highlight
  4314. 'font-lock-multiline t 'help-echo help)))
  4315. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4316. 'font-lock-multiline t 'help-echo help)))
  4317. ;; We need to remove the invisible property here. Table narrowing
  4318. ;; may have made some of this invisible.
  4319. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4320. (remove-text-properties (match-beginning 0) (match-end 0)
  4321. '(invisible nil))
  4322. (if (match-end 3)
  4323. (progn
  4324. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4325. (org-rear-nonsticky-at (match-beginning 3))
  4326. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4327. (org-rear-nonsticky-at (match-end 3))
  4328. (add-text-properties (match-end 3) (match-end 0) ip)
  4329. (org-rear-nonsticky-at (match-end 0)))
  4330. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4331. (org-rear-nonsticky-at (match-beginning 1))
  4332. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4333. (org-rear-nonsticky-at (match-end 1))
  4334. (add-text-properties (match-end 1) (match-end 0) ip)
  4335. (org-rear-nonsticky-at (match-end 0)))
  4336. t)))
  4337. (defun org-activate-dates (limit)
  4338. "Run through the buffer and add overlays to dates."
  4339. (if (re-search-forward org-tsr-regexp-both limit t)
  4340. (progn
  4341. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4342. (add-text-properties (match-beginning 0) (match-end 0)
  4343. (list 'mouse-face 'highlight
  4344. 'keymap org-mouse-map))
  4345. (org-rear-nonsticky-at (match-end 0))
  4346. (when org-display-custom-times
  4347. (if (match-end 3)
  4348. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4349. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4350. t)))
  4351. (defvar org-target-link-regexp nil
  4352. "Regular expression matching radio targets in plain text.")
  4353. (make-variable-buffer-local 'org-target-link-regexp)
  4354. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4355. "Regular expression matching a link target.")
  4356. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4357. "Regular expression matching a radio target.")
  4358. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4359. "Regular expression matching any target.")
  4360. (defun org-activate-target-links (limit)
  4361. "Run through the buffer and add overlays to target matches."
  4362. (when org-target-link-regexp
  4363. (let ((case-fold-search t))
  4364. (if (re-search-forward org-target-link-regexp limit t)
  4365. (progn
  4366. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4367. (add-text-properties (match-beginning 0) (match-end 0)
  4368. (list 'mouse-face 'highlight
  4369. 'keymap org-mouse-map
  4370. 'help-echo "Radio target link"
  4371. 'org-linked-text t))
  4372. (org-rear-nonsticky-at (match-end 0))
  4373. t)))))
  4374. (defun org-update-radio-target-regexp ()
  4375. "Find all radio targets in this file and update the regular expression."
  4376. (interactive)
  4377. (when (memq 'radio org-activate-links)
  4378. (setq org-target-link-regexp
  4379. (org-make-target-link-regexp (org-all-targets 'radio)))
  4380. (org-restart-font-lock)))
  4381. (defun org-hide-wide-columns (limit)
  4382. (let (s e)
  4383. (setq s (text-property-any (point) (or limit (point-max))
  4384. 'org-cwidth t))
  4385. (when s
  4386. (setq e (next-single-property-change s 'org-cwidth))
  4387. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4388. (goto-char e)
  4389. t)))
  4390. (defvar org-latex-and-specials-regexp nil
  4391. "Regular expression for highlighting export special stuff.")
  4392. (defvar org-match-substring-regexp)
  4393. (defvar org-match-substring-with-braces-regexp)
  4394. ;; This should be with the exporter code, but we also use if for font-locking
  4395. (defconst org-export-html-special-string-regexps
  4396. '(("\\\\-" . "&shy;")
  4397. ("---\\([^-]\\)" . "&mdash;\\1")
  4398. ("--\\([^-]\\)" . "&ndash;\\1")
  4399. ("\\.\\.\\." . "&hellip;"))
  4400. "Regular expressions for special string conversion.")
  4401. (defun org-compute-latex-and-specials-regexp ()
  4402. "Compute regular expression for stuff treated specially by exporters."
  4403. (if (not org-highlight-latex-fragments-and-specials)
  4404. (org-set-local 'org-latex-and-specials-regexp nil)
  4405. (require 'org-exp)
  4406. (let*
  4407. ((matchers (plist-get org-format-latex-options :matchers))
  4408. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4409. org-latex-regexps)))
  4410. (org-export-allow-BIND nil)
  4411. (options (org-combine-plists (org-default-export-plist)
  4412. (org-infile-export-plist)))
  4413. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4414. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4415. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4416. (org-export-html-expand (plist-get options :expand-quoted-html))
  4417. (org-export-with-special-strings (plist-get options :special-strings))
  4418. (re-sub
  4419. (cond
  4420. ((equal org-export-with-sub-superscripts '{})
  4421. (list org-match-substring-with-braces-regexp))
  4422. (org-export-with-sub-superscripts
  4423. (list org-match-substring-regexp))
  4424. (t nil)))
  4425. (re-latex
  4426. (if org-export-with-LaTeX-fragments
  4427. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4428. (re-macros
  4429. (if org-export-with-TeX-macros
  4430. (list (concat "\\\\"
  4431. (regexp-opt
  4432. (append (mapcar 'car (append org-entities-user
  4433. org-entities))
  4434. (if (boundp 'org-latex-entities)
  4435. (mapcar (lambda (x)
  4436. (or (car-safe x) x))
  4437. org-latex-entities)
  4438. nil))
  4439. 'words))) ; FIXME
  4440. ))
  4441. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4442. (re-special (if org-export-with-special-strings
  4443. (mapcar (lambda (x) (car x))
  4444. org-export-html-special-string-regexps)))
  4445. (re-rest
  4446. (delq nil
  4447. (list
  4448. (if org-export-html-expand "@<[^>\n]+>")
  4449. ))))
  4450. (org-set-local
  4451. 'org-latex-and-specials-regexp
  4452. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4453. re-rest) "\\|")))))
  4454. (defun org-do-latex-and-special-faces (limit)
  4455. "Run through the buffer and add overlays to links."
  4456. (when org-latex-and-specials-regexp
  4457. (let (rtn d)
  4458. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4459. limit t))
  4460. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4461. 'face))
  4462. '(org-code org-verbatim underline)))
  4463. (progn
  4464. (setq rtn t
  4465. d (cond ((member (char-after (1+ (match-beginning 0)))
  4466. '(?_ ?^)) 1)
  4467. (t 0)))
  4468. (font-lock-prepend-text-property
  4469. (+ d (match-beginning 0)) (match-end 0)
  4470. 'face 'org-latex-and-export-specials)
  4471. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4472. '(font-lock-multiline t)))))
  4473. rtn)))
  4474. (defun org-restart-font-lock ()
  4475. "Restart font-lock-mode, to force refontification."
  4476. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4477. (font-lock-mode -1)
  4478. (font-lock-mode 1)))
  4479. (defun org-all-targets (&optional radio)
  4480. "Return a list of all targets in this file.
  4481. With optional argument RADIO, only find radio targets."
  4482. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4483. rtn)
  4484. (save-excursion
  4485. (goto-char (point-min))
  4486. (while (re-search-forward re nil t)
  4487. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4488. rtn)))
  4489. (defun org-make-target-link-regexp (targets)
  4490. "Make regular expression matching all strings in TARGETS.
  4491. The regular expression finds the targets also if there is a line break
  4492. between words."
  4493. (and targets
  4494. (concat
  4495. "\\<\\("
  4496. (mapconcat
  4497. (lambda (x)
  4498. (while (string-match " +" x)
  4499. (setq x (replace-match "\\s-+" t t x)))
  4500. x)
  4501. targets
  4502. "\\|")
  4503. "\\)\\>")))
  4504. (defun org-activate-tags (limit)
  4505. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4506. (progn
  4507. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4508. (add-text-properties (match-beginning 1) (match-end 1)
  4509. (list 'mouse-face 'highlight
  4510. 'keymap org-mouse-map))
  4511. (org-rear-nonsticky-at (match-end 1))
  4512. t)))
  4513. (defun org-outline-level ()
  4514. "Compute the outline level of the heading at point.
  4515. This function assumes that the cursor is at the beginning of a line matched
  4516. by outline-regexp. Otherwise it returns garbage.
  4517. If this is called at a normal headline, the level is the number of stars.
  4518. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4519. For plain list items, if they are matched by `outline-regexp', this returns
  4520. 1000 plus the line indentation."
  4521. (save-excursion
  4522. (looking-at outline-regexp)
  4523. (if (match-beginning 1)
  4524. (+ (org-get-string-indentation (match-string 1)) 1000)
  4525. (1- (- (match-end 0) (match-beginning 0))))))
  4526. (defvar org-font-lock-keywords nil)
  4527. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4528. "Regular expression matching a property line.")
  4529. (defvar org-font-lock-hook nil
  4530. "Functions to be called for special font lock stuff.")
  4531. (defun org-font-lock-hook (limit)
  4532. (run-hook-with-args 'org-font-lock-hook limit))
  4533. (defun org-set-font-lock-defaults ()
  4534. (let* ((em org-fontify-emphasized-text)
  4535. (lk org-activate-links)
  4536. (org-font-lock-extra-keywords
  4537. (list
  4538. ;; Call the hook
  4539. '(org-font-lock-hook)
  4540. ;; Headlines
  4541. `(,(if org-fontify-whole-heading-line
  4542. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4543. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4544. (1 (org-get-level-face 1))
  4545. (2 (org-get-level-face 2))
  4546. (3 (org-get-level-face 3)))
  4547. ;; Table lines
  4548. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4549. (1 'org-table t))
  4550. ;; Table internals
  4551. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4552. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4553. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4554. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4555. ;; Drawers
  4556. (list org-drawer-regexp '(0 'org-special-keyword t))
  4557. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4558. ;; Properties
  4559. (list org-property-re
  4560. '(1 'org-special-keyword t)
  4561. '(3 'org-property-value t))
  4562. ;; Links
  4563. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4564. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4565. (if (memq 'plain lk) '(org-activate-plain-links))
  4566. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4567. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4568. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4569. (if (memq 'footnote lk) '(org-activate-footnote-links
  4570. (2 'org-footnote t)))
  4571. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4572. '(org-hide-wide-columns (0 nil append))
  4573. ;; TODO lines
  4574. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4575. '(1 (org-get-todo-face 1) t))
  4576. ;; DONE
  4577. (if org-fontify-done-headline
  4578. (list (concat "^[*]+ +\\<\\("
  4579. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4580. "\\)\\(.*\\)")
  4581. '(2 'org-headline-done t))
  4582. nil)
  4583. ;; Priorities
  4584. '(org-font-lock-add-priority-faces)
  4585. ;; Tags
  4586. '(org-font-lock-add-tag-faces)
  4587. ;; Special keywords
  4588. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4589. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4590. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4591. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4592. ;; Emphasis
  4593. (if em
  4594. (if (featurep 'xemacs)
  4595. '(org-do-emphasis-faces (0 nil append))
  4596. '(org-do-emphasis-faces)))
  4597. ;; Checkboxes
  4598. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4599. 2 'org-checkbox prepend)
  4600. (if org-provide-checkbox-statistics
  4601. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4602. (0 (org-get-checkbox-statistics-face) t)))
  4603. ;; Description list items
  4604. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4605. 2 'bold prepend)
  4606. ;; ARCHIVEd headings
  4607. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4608. '(1 'org-archived prepend))
  4609. ;; Specials
  4610. '(org-do-latex-and-special-faces)
  4611. ;; Code
  4612. '(org-activate-code (1 'org-code t))
  4613. ;; COMMENT
  4614. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4615. "\\|" org-quote-string "\\)\\>")
  4616. '(1 'org-special-keyword t))
  4617. '("^#.*" (0 'font-lock-comment-face t))
  4618. ;; Blocks and meta lines
  4619. '(org-fontify-meta-lines-and-blocks)
  4620. )))
  4621. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4622. ;; Now set the full font-lock-keywords
  4623. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4624. (org-set-local 'font-lock-defaults
  4625. '(org-font-lock-keywords t nil nil backward-paragraph))
  4626. (kill-local-variable 'font-lock-keywords) nil))
  4627. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4628. "Fontify string S like in Org-mode"
  4629. (with-temp-buffer
  4630. (insert s)
  4631. (let ((org-odd-levels-only odd-levels))
  4632. (org-mode)
  4633. (font-lock-fontify-buffer)
  4634. (buffer-string))))
  4635. (defvar org-m nil)
  4636. (defvar org-l nil)
  4637. (defvar org-f nil)
  4638. (defun org-get-level-face (n)
  4639. "Get the right face for match N in font-lock matching of headlines."
  4640. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4641. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4642. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4643. (cond
  4644. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4645. ((eq n 2) org-f)
  4646. (t (if org-level-color-stars-only nil org-f))))
  4647. (defun org-get-todo-face (kwd)
  4648. "Get the right face for a TODO keyword KWD.
  4649. If KWD is a number, get the corresponding match group."
  4650. (if (numberp kwd) (setq kwd (match-string kwd)))
  4651. (or (org-face-from-face-or-color
  4652. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  4653. (and (member kwd org-done-keywords) 'org-done)
  4654. 'org-todo))
  4655. (defun org-face-from-face-or-color (context inherit face-or-color)
  4656. "Create a face list that inherits INHERIT, but sets the foreground color.
  4657. When FACE-OR-COLOR is not a string, just return it."
  4658. (if (stringp face-or-color)
  4659. (list :inherit inherit
  4660. (cdr (assoc context org-faces-easy-properties))
  4661. face-or-color)
  4662. face-or-color))
  4663. (defun org-font-lock-add-tag-faces (limit)
  4664. "Add the special tag faces."
  4665. (when (and org-tag-faces org-tags-special-faces-re)
  4666. (while (re-search-forward org-tags-special-faces-re limit t)
  4667. (add-text-properties (match-beginning 1) (match-end 1)
  4668. (list 'face (org-get-tag-face 1)
  4669. 'font-lock-fontified t))
  4670. (backward-char 1))))
  4671. (defun org-font-lock-add-priority-faces (limit)
  4672. "Add the special priority faces."
  4673. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4674. (add-text-properties
  4675. (match-beginning 0) (match-end 0)
  4676. (list 'face (or (org-face-from-face-or-color
  4677. 'priority 'org-special-keyword
  4678. (cdr (assoc (char-after (match-beginning 1))
  4679. org-priority-faces)))
  4680. 'org-special-keyword)
  4681. 'font-lock-fontified t))))
  4682. (defun org-get-tag-face (kwd)
  4683. "Get the right face for a TODO keyword KWD.
  4684. If KWD is a number, get the corresponding match group."
  4685. (if (numberp kwd) (setq kwd (match-string kwd)))
  4686. (or (org-face-from-face-or-color
  4687. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  4688. 'org-tag))
  4689. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4690. "Remove fontification and activation overlays from links."
  4691. (font-lock-default-unfontify-region beg end)
  4692. (let* ((buffer-undo-list t)
  4693. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4694. (inhibit-modification-hooks t)
  4695. deactivate-mark buffer-file-name buffer-file-truename)
  4696. (remove-text-properties
  4697. beg end
  4698. (if org-indent-mode
  4699. ;; also remove line-prefix and wrap-prefix properties
  4700. '(mouse-face t keymap t org-linked-text t
  4701. invisible t intangible t
  4702. line-prefix t wrap-prefix t
  4703. org-no-flyspell t)
  4704. '(mouse-face t keymap t org-linked-text t
  4705. invisible t intangible t
  4706. org-no-flyspell t)))))
  4707. ;;;; Visibility cycling, including org-goto and indirect buffer
  4708. ;;; Cycling
  4709. (defvar org-cycle-global-status nil)
  4710. (make-variable-buffer-local 'org-cycle-global-status)
  4711. (defvar org-cycle-subtree-status nil)
  4712. (make-variable-buffer-local 'org-cycle-subtree-status)
  4713. ;;;###autoload
  4714. (defvar org-inlinetask-min-level)
  4715. (defun org-cycle (&optional arg)
  4716. "TAB-action and visibility cycling for Org-mode.
  4717. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4718. is outline visibility cycling, but it also invokes other actions
  4719. in special contexts.
  4720. - When this function is called with a prefix argument, rotate the entire
  4721. buffer through 3 states (global cycling)
  4722. 1. OVERVIEW: Show only top-level headlines.
  4723. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4724. 3. SHOW ALL: Show everything.
  4725. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4726. determined by the variable `org-startup-folded', and by any VISIBILITY
  4727. properties in the buffer.
  4728. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4729. including any drawers.
  4730. - When inside a table, re-align the table and move to the next field.
  4731. - When point is at the beginning of a headline, rotate the subtree started
  4732. by this line through 3 different states (local cycling)
  4733. 1. FOLDED: Only the main headline is shown.
  4734. 2. CHILDREN: The main headline and the direct children are shown.
  4735. From this state, you can move to one of the children
  4736. and zoom in further.
  4737. 3. SUBTREE: Show the entire subtree, including body text.
  4738. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4739. - When point is at the beginning of an empty headline and the variable
  4740. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  4741. of the headline by demoting and promoting it to likely levels. This
  4742. speeds up creation document structure by presing TAB once or several
  4743. times right after creating a new headline.
  4744. - When there is a numeric prefix, go up to a heading with level ARG, do
  4745. a `show-subtree' and return to the previous cursor position. If ARG
  4746. is negative, go up that many levels.
  4747. - When point is not at the beginning of a headline, execute the global
  4748. binding for TAB, which is re-indenting the line. See the option
  4749. `org-cycle-emulate-tab' for details.
  4750. - Special case: if point is at the beginning of the buffer and there is
  4751. no headline in line 1, this function will act as if called with prefix arg.
  4752. But only if also the variable `org-cycle-global-at-bob' is t."
  4753. (interactive "P")
  4754. (org-load-modules-maybe)
  4755. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  4756. (and org-cycle-level-after-item/entry-creation
  4757. (or (org-cycle-level)
  4758. (org-cycle-item-indentation))))
  4759. (let* ((limit-level
  4760. (or org-cycle-max-level
  4761. (and (boundp 'org-inlinetask-min-level)
  4762. org-inlinetask-min-level
  4763. (1- org-inlinetask-min-level))))
  4764. (nstars (and limit-level
  4765. (if org-odd-levels-only
  4766. (and limit-level (1- (* limit-level 2)))
  4767. limit-level)))
  4768. (outline-regexp
  4769. (cond
  4770. ((not (org-mode-p)) outline-regexp)
  4771. ((or (eq org-cycle-include-plain-lists 'integrate)
  4772. (and org-cycle-include-plain-lists (org-at-item-p)))
  4773. (concat "\\(?:\\*"
  4774. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4775. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4776. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4777. (bob-special (and org-cycle-global-at-bob (bobp)
  4778. (not (looking-at outline-regexp))))
  4779. (org-cycle-hook
  4780. (if bob-special
  4781. (delq 'org-optimize-window-after-visibility-change
  4782. (copy-sequence org-cycle-hook))
  4783. org-cycle-hook))
  4784. (pos (point)))
  4785. (if (or bob-special (equal arg '(4)))
  4786. ;; special case: use global cycling
  4787. (setq arg t))
  4788. (cond
  4789. ((equal arg '(16))
  4790. (org-set-startup-visibility)
  4791. (message "Startup visibility, plus VISIBILITY properties"))
  4792. ((equal arg '(64))
  4793. (show-all)
  4794. (message "Entire buffer visible, including drawers"))
  4795. ((org-at-table-p 'any)
  4796. ;; Enter the table or move to the next field in the table
  4797. (if (org-at-table.el-p)
  4798. (message "Use C-c ' to edit table.el tables")
  4799. (if arg (org-table-edit-field t)
  4800. (org-table-justify-field-maybe)
  4801. (call-interactively 'org-table-next-field))))
  4802. ((run-hook-with-args-until-success
  4803. 'org-tab-after-check-for-table-hook))
  4804. ((eq arg t) ;; Global cycling
  4805. (org-cycle-internal-global))
  4806. ((and org-drawers org-drawer-regexp
  4807. (save-excursion
  4808. (beginning-of-line 1)
  4809. (looking-at org-drawer-regexp)))
  4810. ;; Toggle block visibility
  4811. (org-flag-drawer
  4812. (not (get-char-property (match-end 0) 'invisible))))
  4813. ((integerp arg)
  4814. ;; Show-subtree, ARG levels up from here.
  4815. (save-excursion
  4816. (org-back-to-heading)
  4817. (outline-up-heading (if (< arg 0) (- arg)
  4818. (- (funcall outline-level) arg)))
  4819. (org-show-subtree)))
  4820. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4821. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4822. (org-cycle-internal-local))
  4823. ;; TAB emulation and template completion
  4824. (buffer-read-only (org-back-to-heading))
  4825. ((run-hook-with-args-until-success
  4826. 'org-tab-after-check-for-cycling-hook))
  4827. ((org-try-structure-completion))
  4828. ((org-try-cdlatex-tab))
  4829. ((run-hook-with-args-until-success
  4830. 'org-tab-before-tab-emulation-hook))
  4831. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4832. (or (not (bolp))
  4833. (not (looking-at outline-regexp))))
  4834. (call-interactively (global-key-binding "\t")))
  4835. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4836. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4837. (or (and (eq org-cycle-emulate-tab 'white)
  4838. (= (match-end 0) (point-at-eol)))
  4839. (and (eq org-cycle-emulate-tab 'whitestart)
  4840. (>= (match-end 0) pos))))
  4841. t
  4842. (eq org-cycle-emulate-tab t))
  4843. (call-interactively (global-key-binding "\t")))
  4844. (t (save-excursion
  4845. (org-back-to-heading)
  4846. (org-cycle)))))))
  4847. (defun org-cycle-internal-global ()
  4848. "Do the global cycling action."
  4849. (cond
  4850. ((and (eq last-command this-command)
  4851. (eq org-cycle-global-status 'overview))
  4852. ;; We just created the overview - now do table of contents
  4853. ;; This can be slow in very large buffers, so indicate action
  4854. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4855. (message "CONTENTS...")
  4856. (org-content)
  4857. (message "CONTENTS...done")
  4858. (setq org-cycle-global-status 'contents)
  4859. (run-hook-with-args 'org-cycle-hook 'contents))
  4860. ((and (eq last-command this-command)
  4861. (eq org-cycle-global-status 'contents))
  4862. ;; We just showed the table of contents - now show everything
  4863. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4864. (show-all)
  4865. (message "SHOW ALL")
  4866. (setq org-cycle-global-status 'all)
  4867. (run-hook-with-args 'org-cycle-hook 'all))
  4868. (t
  4869. ;; Default action: go to overview
  4870. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4871. (org-overview)
  4872. (message "OVERVIEW")
  4873. (setq org-cycle-global-status 'overview)
  4874. (run-hook-with-args 'org-cycle-hook 'overview))))
  4875. (defun org-cycle-internal-local ()
  4876. "Do the local cycling action."
  4877. (org-back-to-heading)
  4878. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4879. ;; First, some boundaries
  4880. (save-excursion
  4881. (org-back-to-heading)
  4882. (setq level (funcall outline-level))
  4883. (save-excursion
  4884. (beginning-of-line 2)
  4885. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4886. ; XEmacs does not have `next-single-char-property-change'
  4887. ; I'm not sure about Emacs 21.
  4888. (while (and (not (eobp)) ;; this is like `next-line'
  4889. (get-char-property (1- (point)) 'invisible))
  4890. (beginning-of-line 2))
  4891. (while (and (not (eobp)) ;; this is like `next-line'
  4892. (get-char-property (1- (point)) 'invisible))
  4893. (goto-char (next-single-char-property-change (point) 'invisible))
  4894. (and (eolp) (beginning-of-line 2))))
  4895. (setq eol (point)))
  4896. (outline-end-of-heading) (setq eoh (point))
  4897. (save-excursion
  4898. (outline-next-heading)
  4899. (setq has-children (and (org-at-heading-p t)
  4900. (> (funcall outline-level) level))))
  4901. (org-end-of-subtree t)
  4902. (unless (eobp)
  4903. (skip-chars-forward " \t\n")
  4904. (beginning-of-line 1) ; in case this is an item
  4905. )
  4906. (setq eos (if (eobp) (point) (1- (point)))))
  4907. ;; Find out what to do next and set `this-command'
  4908. (cond
  4909. ((= eos eoh)
  4910. ;; Nothing is hidden behind this heading
  4911. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4912. (message "EMPTY ENTRY")
  4913. (setq org-cycle-subtree-status nil)
  4914. (save-excursion
  4915. (goto-char eos)
  4916. (outline-next-heading)
  4917. (if (org-invisible-p) (org-flag-heading nil))))
  4918. ((and (or (>= eol eos)
  4919. (not (string-match "\\S-" (buffer-substring eol eos))))
  4920. (or has-children
  4921. (not (setq children-skipped
  4922. org-cycle-skip-children-state-if-no-children))))
  4923. ;; Entire subtree is hidden in one line: children view
  4924. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4925. (org-show-entry)
  4926. (show-children)
  4927. (message "CHILDREN")
  4928. (save-excursion
  4929. (goto-char eos)
  4930. (outline-next-heading)
  4931. (if (org-invisible-p) (org-flag-heading nil)))
  4932. (setq org-cycle-subtree-status 'children)
  4933. (run-hook-with-args 'org-cycle-hook 'children))
  4934. ((or children-skipped
  4935. (and (eq last-command this-command)
  4936. (eq org-cycle-subtree-status 'children)))
  4937. ;; We just showed the children, or no children are there,
  4938. ;; now show everything.
  4939. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4940. (org-show-subtree)
  4941. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4942. (setq org-cycle-subtree-status 'subtree)
  4943. (run-hook-with-args 'org-cycle-hook 'subtree))
  4944. (t
  4945. ;; Default action: hide the subtree.
  4946. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4947. (hide-subtree)
  4948. (message "FOLDED")
  4949. (setq org-cycle-subtree-status 'folded)
  4950. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4951. ;;;###autoload
  4952. (defun org-global-cycle (&optional arg)
  4953. "Cycle the global visibility. For details see `org-cycle'.
  4954. With C-u prefix arg, switch to startup visibility.
  4955. With a numeric prefix, show all headlines up to that level."
  4956. (interactive "P")
  4957. (let ((org-cycle-include-plain-lists
  4958. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4959. (cond
  4960. ((integerp arg)
  4961. (show-all)
  4962. (hide-sublevels arg)
  4963. (setq org-cycle-global-status 'contents))
  4964. ((equal arg '(4))
  4965. (org-set-startup-visibility)
  4966. (message "Startup visibility, plus VISIBILITY properties."))
  4967. (t
  4968. (org-cycle '(4))))))
  4969. (defun org-set-startup-visibility ()
  4970. "Set the visibility required by startup options and properties."
  4971. (cond
  4972. ((eq org-startup-folded t)
  4973. (org-cycle '(4)))
  4974. ((eq org-startup-folded 'content)
  4975. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4976. (org-cycle '(4)) (org-cycle '(4)))))
  4977. (unless (eq org-startup-folded 'showeverything)
  4978. (if org-hide-block-startup (org-hide-block-all))
  4979. (org-set-visibility-according-to-property 'no-cleanup)
  4980. (org-cycle-hide-archived-subtrees 'all)
  4981. (org-cycle-hide-drawers 'all)
  4982. (org-cycle-show-empty-lines 'all)))
  4983. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4984. "Switch subtree visibilities according to :VISIBILITY: property."
  4985. (interactive)
  4986. (let (org-show-entry-below state)
  4987. (save-excursion
  4988. (goto-char (point-min))
  4989. (while (re-search-forward
  4990. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4991. nil t)
  4992. (setq state (match-string 1))
  4993. (save-excursion
  4994. (org-back-to-heading t)
  4995. (hide-subtree)
  4996. (org-reveal)
  4997. (cond
  4998. ((equal state '("fold" "folded"))
  4999. (hide-subtree))
  5000. ((equal state "children")
  5001. (org-show-hidden-entry)
  5002. (show-children))
  5003. ((equal state "content")
  5004. (save-excursion
  5005. (save-restriction
  5006. (org-narrow-to-subtree)
  5007. (org-content))))
  5008. ((member state '("all" "showall"))
  5009. (show-subtree)))))
  5010. (unless no-cleanup
  5011. (org-cycle-hide-archived-subtrees 'all)
  5012. (org-cycle-hide-drawers 'all)
  5013. (org-cycle-show-empty-lines 'all)))))
  5014. (defun org-overview ()
  5015. "Switch to overview mode, showing only top-level headlines.
  5016. Really, this shows all headlines with level equal or greater than the level
  5017. of the first headline in the buffer. This is important, because if the
  5018. first headline is not level one, then (hide-sublevels 1) gives confusing
  5019. results."
  5020. (interactive)
  5021. (let ((level (save-excursion
  5022. (goto-char (point-min))
  5023. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5024. (progn
  5025. (goto-char (match-beginning 0))
  5026. (funcall outline-level))))))
  5027. (and level (hide-sublevels level))))
  5028. (defun org-content (&optional arg)
  5029. "Show all headlines in the buffer, like a table of contents.
  5030. With numerical argument N, show content up to level N."
  5031. (interactive "P")
  5032. (save-excursion
  5033. ;; Visit all headings and show their offspring
  5034. (and (integerp arg) (org-overview))
  5035. (goto-char (point-max))
  5036. (catch 'exit
  5037. (while (and (progn (condition-case nil
  5038. (outline-previous-visible-heading 1)
  5039. (error (goto-char (point-min))))
  5040. t)
  5041. (looking-at outline-regexp))
  5042. (if (integerp arg)
  5043. (show-children (1- arg))
  5044. (show-branches))
  5045. (if (bobp) (throw 'exit nil))))))
  5046. (defun org-optimize-window-after-visibility-change (state)
  5047. "Adjust the window after a change in outline visibility.
  5048. This function is the default value of the hook `org-cycle-hook'."
  5049. (when (get-buffer-window (current-buffer))
  5050. (cond
  5051. ((eq state 'content) nil)
  5052. ((eq state 'all) nil)
  5053. ((eq state 'folded) nil)
  5054. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5055. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5056. (defun org-remove-empty-overlays-at (pos)
  5057. "Remove outline overlays that do not contain non-white stuff."
  5058. (mapc
  5059. (lambda (o)
  5060. (and (eq 'outline (overlay-get o 'invisible))
  5061. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5062. (overlay-end o))))
  5063. (delete-overlay o)))
  5064. (overlays-at pos)))
  5065. (defun org-clean-visibility-after-subtree-move ()
  5066. "Fix visibility issues after moving a subtree."
  5067. ;; First, find a reasonable region to look at:
  5068. ;; Start two siblings above, end three below
  5069. (let* ((beg (save-excursion
  5070. (and (org-get-last-sibling)
  5071. (org-get-last-sibling))
  5072. (point)))
  5073. (end (save-excursion
  5074. (and (org-get-next-sibling)
  5075. (org-get-next-sibling)
  5076. (org-get-next-sibling))
  5077. (if (org-at-heading-p)
  5078. (point-at-eol)
  5079. (point))))
  5080. (level (looking-at "\\*+"))
  5081. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5082. (save-excursion
  5083. (save-restriction
  5084. (narrow-to-region beg end)
  5085. (when re
  5086. ;; Properly fold already folded siblings
  5087. (goto-char (point-min))
  5088. (while (re-search-forward re nil t)
  5089. (if (and (not (org-invisible-p))
  5090. (save-excursion
  5091. (goto-char (point-at-eol)) (org-invisible-p)))
  5092. (hide-entry))))
  5093. (org-cycle-show-empty-lines 'overview)
  5094. (org-cycle-hide-drawers 'overview)))))
  5095. (defun org-cycle-show-empty-lines (state)
  5096. "Show empty lines above all visible headlines.
  5097. The region to be covered depends on STATE when called through
  5098. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5099. entire buffer covered. Note that an empty line is only shown if there
  5100. are at least `org-cycle-separator-lines' empty lines before the headline."
  5101. (when (not (= org-cycle-separator-lines 0))
  5102. (save-excursion
  5103. (let* ((n (abs org-cycle-separator-lines))
  5104. (re (cond
  5105. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5106. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5107. (t (let ((ns (number-to-string (- n 2))))
  5108. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5109. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5110. beg end b e)
  5111. (cond
  5112. ((memq state '(overview contents t))
  5113. (setq beg (point-min) end (point-max)))
  5114. ((memq state '(children folded))
  5115. (setq beg (point) end (progn (org-end-of-subtree t t)
  5116. (beginning-of-line 2)
  5117. (point)))))
  5118. (when beg
  5119. (goto-char beg)
  5120. (while (re-search-forward re end t)
  5121. (unless (get-char-property (match-end 1) 'invisible)
  5122. (setq e (match-end 1))
  5123. (if (< org-cycle-separator-lines 0)
  5124. (setq b (save-excursion
  5125. (goto-char (match-beginning 0))
  5126. (org-back-over-empty-lines)
  5127. (if (save-excursion
  5128. (goto-char (max (point-min) (1- (point))))
  5129. (org-on-heading-p))
  5130. (1- (point))
  5131. (point))))
  5132. (setq b (match-beginning 1)))
  5133. (outline-flag-region b e nil)))))))
  5134. ;; Never hide empty lines at the end of the file.
  5135. (save-excursion
  5136. (goto-char (point-max))
  5137. (outline-previous-heading)
  5138. (outline-end-of-heading)
  5139. (if (and (looking-at "[ \t\n]+")
  5140. (= (match-end 0) (point-max)))
  5141. (outline-flag-region (point) (match-end 0) nil))))
  5142. (defun org-show-empty-lines-in-parent ()
  5143. "Move to the parent and re-show empty lines before visible headlines."
  5144. (save-excursion
  5145. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5146. (org-cycle-show-empty-lines context))))
  5147. (defun org-files-list ()
  5148. "Return `org-agenda-files' list, plus all open org-mode files.
  5149. This is useful for operations that need to scan all of a user's
  5150. open and agenda-wise Org files."
  5151. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5152. (dolist (buf (buffer-list))
  5153. (with-current-buffer buf
  5154. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5155. (let ((file (expand-file-name (buffer-file-name))))
  5156. (unless (member file files)
  5157. (push file files))))))
  5158. files))
  5159. (defsubst org-entry-beginning-position ()
  5160. "Return the beginning position of the current entry."
  5161. (save-excursion (outline-back-to-heading t) (point)))
  5162. (defsubst org-entry-end-position ()
  5163. "Return the end position of the current entry."
  5164. (save-excursion (outline-next-heading) (point)))
  5165. (defun org-cycle-hide-drawers (state)
  5166. "Re-hide all drawers after a visibility state change."
  5167. (when (and (org-mode-p)
  5168. (not (memq state '(overview folded contents))))
  5169. (save-excursion
  5170. (let* ((globalp (memq state '(contents all)))
  5171. (beg (if globalp (point-min) (point)))
  5172. (end (if globalp (point-max)
  5173. (if (eq state 'children)
  5174. (save-excursion (outline-next-heading) (point))
  5175. (org-end-of-subtree t)))))
  5176. (goto-char beg)
  5177. (while (re-search-forward org-drawer-regexp end t)
  5178. (org-flag-drawer t))))))
  5179. (defun org-flag-drawer (flag)
  5180. (save-excursion
  5181. (beginning-of-line 1)
  5182. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5183. (let ((b (match-end 0))
  5184. (outline-regexp org-outline-regexp))
  5185. (if (re-search-forward
  5186. "^[ \t]*:END:"
  5187. (save-excursion (outline-next-heading) (point)) t)
  5188. (outline-flag-region b (point-at-eol) flag)
  5189. (error ":END: line missing at position %s" b))))))
  5190. (defun org-subtree-end-visible-p ()
  5191. "Is the end of the current subtree visible?"
  5192. (pos-visible-in-window-p
  5193. (save-excursion (org-end-of-subtree t) (point))))
  5194. (defun org-first-headline-recenter (&optional N)
  5195. "Move cursor to the first headline and recenter the headline.
  5196. Optional argument N means put the headline into the Nth line of the window."
  5197. (goto-char (point-min))
  5198. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5199. (beginning-of-line)
  5200. (recenter (prefix-numeric-value N))))
  5201. ;;; Saving and restoring visibility
  5202. (defun org-outline-overlay-data (&optional use-markers)
  5203. "Return a list of the locations of all outline overlays.
  5204. The are overlays with the `invisible' property value `outline'.
  5205. The return valus is a list of cons cells, with start and stop
  5206. positions for each overlay.
  5207. If USE-MARKERS is set, return the positions as markers."
  5208. (let (beg end)
  5209. (save-excursion
  5210. (save-restriction
  5211. (widen)
  5212. (delq nil
  5213. (mapcar (lambda (o)
  5214. (when (eq (overlay-get o 'invisible) 'outline)
  5215. (setq beg (overlay-start o)
  5216. end (overlay-end o))
  5217. (and beg end (> end beg)
  5218. (if use-markers
  5219. (cons (move-marker (make-marker) beg)
  5220. (move-marker (make-marker) end))
  5221. (cons beg end)))))
  5222. (overlays-in (point-min) (point-max))))))))
  5223. (defun org-set-outline-overlay-data (data)
  5224. "Create visibility overlays for all positions in DATA.
  5225. DATA should have been made by `org-outline-overlay-data'."
  5226. (let (o)
  5227. (save-excursion
  5228. (save-restriction
  5229. (widen)
  5230. (show-all)
  5231. (mapc (lambda (c)
  5232. (setq o (make-overlay (car c) (cdr c)))
  5233. (overlay-put o 'invisible 'outline))
  5234. data)))))
  5235. (defmacro org-save-outline-visibility (use-markers &rest body)
  5236. "Save and restore outline visibility around BODY.
  5237. If USE-MARKERS is non-nil, use markers for the positions.
  5238. This means that the buffer may change while running BODY,
  5239. but it also means that the buffer should stay alive
  5240. during the operation, because otherwise all these markers will
  5241. point nowhere."
  5242. `(let ((data (org-outline-overlay-data ,use-markers)))
  5243. (unwind-protect
  5244. (progn
  5245. ,@body
  5246. (org-set-outline-overlay-data data))
  5247. (when ,use-markers
  5248. (mapc (lambda (c)
  5249. (and (markerp (car c)) (move-marker (car c) nil))
  5250. (and (markerp (cdr c)) (move-marker (cdr c) nil)))
  5251. data)))))
  5252. ;;; Folding of blocks
  5253. (defconst org-block-regexp
  5254. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  5255. "Regular expression for hiding blocks.")
  5256. (defvar org-hide-block-overlays nil
  5257. "Overlays hiding blocks.")
  5258. (make-variable-buffer-local 'org-hide-block-overlays)
  5259. (defun org-block-map (function &optional start end)
  5260. "Call func at the head of all source blocks in the current
  5261. buffer. Optional arguments START and END can be used to limit
  5262. the range."
  5263. (let ((start (or start (point-min)))
  5264. (end (or end (point-max))))
  5265. (save-excursion
  5266. (goto-char start)
  5267. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5268. (save-excursion
  5269. (save-match-data
  5270. (goto-char (match-beginning 0))
  5271. (funcall function)))))))
  5272. (defun org-hide-block-toggle-all ()
  5273. "Toggle the visibility of all blocks in the current buffer."
  5274. (org-block-map #'org-hide-block-toggle))
  5275. (defun org-hide-block-all ()
  5276. "Fold all blocks in the current buffer."
  5277. (interactive)
  5278. (org-show-block-all)
  5279. (org-block-map #'org-hide-block-toggle-maybe))
  5280. (defun org-show-block-all ()
  5281. "Unfold all blocks in the current buffer."
  5282. (mapc 'delete-overlay org-hide-block-overlays)
  5283. (setq org-hide-block-overlays nil))
  5284. (defun org-hide-block-toggle-maybe ()
  5285. "Toggle visibility of block at point."
  5286. (interactive)
  5287. (let ((case-fold-search t))
  5288. (if (save-excursion
  5289. (beginning-of-line 1)
  5290. (looking-at org-block-regexp))
  5291. (progn (org-hide-block-toggle)
  5292. t) ;; to signal that we took action
  5293. nil))) ;; to signal that we did not
  5294. (defun org-hide-block-toggle (&optional force)
  5295. "Toggle the visibility of the current block."
  5296. (interactive)
  5297. (save-excursion
  5298. (beginning-of-line)
  5299. (if (re-search-forward org-block-regexp nil t)
  5300. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5301. (end (match-end 0)) ;; end of entire body
  5302. ov)
  5303. (if (memq t (mapcar (lambda (overlay)
  5304. (eq (overlay-get overlay 'invisible)
  5305. 'org-hide-block))
  5306. (overlays-at start)))
  5307. (if (or (not force) (eq force 'off))
  5308. (mapc (lambda (ov)
  5309. (when (member ov org-hide-block-overlays)
  5310. (setq org-hide-block-overlays
  5311. (delq ov org-hide-block-overlays)))
  5312. (when (eq (overlay-get ov 'invisible)
  5313. 'org-hide-block)
  5314. (delete-overlay ov)))
  5315. (overlays-at start)))
  5316. (setq ov (make-overlay start end))
  5317. (overlay-put ov 'invisible 'org-hide-block)
  5318. ;; make the block accessible to isearch
  5319. (overlay-put
  5320. ov 'isearch-open-invisible
  5321. (lambda (ov)
  5322. (when (member ov org-hide-block-overlays)
  5323. (setq org-hide-block-overlays
  5324. (delq ov org-hide-block-overlays)))
  5325. (when (eq (overlay-get ov 'invisible)
  5326. 'org-hide-block)
  5327. (delete-overlay ov))))
  5328. (push ov org-hide-block-overlays)))
  5329. (error "Not looking at a source block"))))
  5330. ;; org-tab-after-check-for-cycling-hook
  5331. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5332. ;; Remove overlays when changing major mode
  5333. (add-hook 'org-mode-hook
  5334. (lambda () (org-add-hook 'change-major-mode-hook
  5335. 'org-show-block-all 'append 'local)))
  5336. ;;; Org-goto
  5337. (defvar org-goto-window-configuration nil)
  5338. (defvar org-goto-marker nil)
  5339. (defvar org-goto-map
  5340. (let ((map (make-sparse-keymap)))
  5341. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5342. (while (setq cmd (pop cmds))
  5343. (substitute-key-definition cmd cmd map global-map)))
  5344. (suppress-keymap map)
  5345. (org-defkey map "\C-m" 'org-goto-ret)
  5346. (org-defkey map [(return)] 'org-goto-ret)
  5347. (org-defkey map [(left)] 'org-goto-left)
  5348. (org-defkey map [(right)] 'org-goto-right)
  5349. (org-defkey map [(control ?g)] 'org-goto-quit)
  5350. (org-defkey map "\C-i" 'org-cycle)
  5351. (org-defkey map [(tab)] 'org-cycle)
  5352. (org-defkey map [(down)] 'outline-next-visible-heading)
  5353. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5354. (if org-goto-auto-isearch
  5355. (if (fboundp 'define-key-after)
  5356. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5357. nil)
  5358. (org-defkey map "q" 'org-goto-quit)
  5359. (org-defkey map "n" 'outline-next-visible-heading)
  5360. (org-defkey map "p" 'outline-previous-visible-heading)
  5361. (org-defkey map "f" 'outline-forward-same-level)
  5362. (org-defkey map "b" 'outline-backward-same-level)
  5363. (org-defkey map "u" 'outline-up-heading))
  5364. (org-defkey map "/" 'org-occur)
  5365. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5366. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5367. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5368. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5369. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5370. map))
  5371. (defconst org-goto-help
  5372. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5373. RET=jump to location [Q]uit and return to previous location
  5374. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5375. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5376. ;; FIXME: Docstring does not mention both interfaces
  5377. (defun org-goto (&optional alternative-interface)
  5378. "Look up a different location in the current file, keeping current visibility.
  5379. When you want look-up or go to a different location in a document, the
  5380. fastest way is often to fold the entire buffer and then dive into the tree.
  5381. This method has the disadvantage, that the previous location will be folded,
  5382. which may not be what you want.
  5383. This command works around this by showing a copy of the current buffer
  5384. in an indirect buffer, in overview mode. You can dive into the tree in
  5385. that copy, use org-occur and incremental search to find a location.
  5386. When pressing RET or `Q', the command returns to the original buffer in
  5387. which the visibility is still unchanged. After RET is will also jump to
  5388. the location selected in the indirect buffer and expose the
  5389. the headline hierarchy above."
  5390. (interactive "P")
  5391. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5392. (org-refile-use-outline-path t)
  5393. (org-refile-target-verify-function nil)
  5394. (interface
  5395. (if (not alternative-interface)
  5396. org-goto-interface
  5397. (if (eq org-goto-interface 'outline)
  5398. 'outline-path-completion
  5399. 'outline)))
  5400. (org-goto-start-pos (point))
  5401. (selected-point
  5402. (if (eq interface 'outline)
  5403. (car (org-get-location (current-buffer) org-goto-help))
  5404. (nth 3 (org-refile-get-location "Goto: ")))))
  5405. (if selected-point
  5406. (progn
  5407. (org-mark-ring-push org-goto-start-pos)
  5408. (goto-char selected-point)
  5409. (if (or (org-invisible-p) (org-invisible-p2))
  5410. (org-show-context 'org-goto)))
  5411. (message "Quit"))))
  5412. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5413. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5414. (defvar org-goto-local-auto-isearch-map) ; defined below
  5415. (defun org-get-location (buf help)
  5416. "Let the user select a location in the Org-mode buffer BUF.
  5417. This function uses a recursive edit. It returns the selected position
  5418. or nil."
  5419. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5420. (isearch-hide-immediately nil)
  5421. (isearch-search-fun-function
  5422. (lambda () 'org-goto-local-search-headings))
  5423. (org-goto-selected-point org-goto-exit-command)
  5424. (pop-up-frames nil)
  5425. (special-display-buffer-names nil)
  5426. (special-display-regexps nil)
  5427. (special-display-function nil))
  5428. (save-excursion
  5429. (save-window-excursion
  5430. (delete-other-windows)
  5431. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5432. (switch-to-buffer
  5433. (condition-case nil
  5434. (make-indirect-buffer (current-buffer) "*org-goto*")
  5435. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5436. (with-output-to-temp-buffer "*Help*"
  5437. (princ help))
  5438. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5439. (setq buffer-read-only nil)
  5440. (let ((org-startup-truncated t)
  5441. (org-startup-folded nil)
  5442. (org-startup-align-all-tables nil))
  5443. (org-mode)
  5444. (org-overview))
  5445. (setq buffer-read-only t)
  5446. (if (and (boundp 'org-goto-start-pos)
  5447. (integer-or-marker-p org-goto-start-pos))
  5448. (let ((org-show-hierarchy-above t)
  5449. (org-show-siblings t)
  5450. (org-show-following-heading t))
  5451. (goto-char org-goto-start-pos)
  5452. (and (org-invisible-p) (org-show-context)))
  5453. (goto-char (point-min)))
  5454. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5455. (message "Select location and press RET")
  5456. (use-local-map org-goto-map)
  5457. (recursive-edit)
  5458. ))
  5459. (kill-buffer "*org-goto*")
  5460. (cons org-goto-selected-point org-goto-exit-command)))
  5461. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5462. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5463. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5464. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5465. (defun org-goto-local-search-headings (string bound noerror)
  5466. "Search and make sure that any matches are in headlines."
  5467. (catch 'return
  5468. (while (if isearch-forward
  5469. (search-forward string bound noerror)
  5470. (search-backward string bound noerror))
  5471. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5472. (and (member :headline context)
  5473. (not (member :tags context))))
  5474. (throw 'return (point))))))
  5475. (defun org-goto-local-auto-isearch ()
  5476. "Start isearch."
  5477. (interactive)
  5478. (goto-char (point-min))
  5479. (let ((keys (this-command-keys)))
  5480. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5481. (isearch-mode t)
  5482. (isearch-process-search-char (string-to-char keys)))))
  5483. (defun org-goto-ret (&optional arg)
  5484. "Finish `org-goto' by going to the new location."
  5485. (interactive "P")
  5486. (setq org-goto-selected-point (point)
  5487. org-goto-exit-command 'return)
  5488. (throw 'exit nil))
  5489. (defun org-goto-left ()
  5490. "Finish `org-goto' by going to the new location."
  5491. (interactive)
  5492. (if (org-on-heading-p)
  5493. (progn
  5494. (beginning-of-line 1)
  5495. (setq org-goto-selected-point (point)
  5496. org-goto-exit-command 'left)
  5497. (throw 'exit nil))
  5498. (error "Not on a heading")))
  5499. (defun org-goto-right ()
  5500. "Finish `org-goto' by going to the new location."
  5501. (interactive)
  5502. (if (org-on-heading-p)
  5503. (progn
  5504. (setq org-goto-selected-point (point)
  5505. org-goto-exit-command 'right)
  5506. (throw 'exit nil))
  5507. (error "Not on a heading")))
  5508. (defun org-goto-quit ()
  5509. "Finish `org-goto' without cursor motion."
  5510. (interactive)
  5511. (setq org-goto-selected-point nil)
  5512. (setq org-goto-exit-command 'quit)
  5513. (throw 'exit nil))
  5514. ;;; Indirect buffer display of subtrees
  5515. (defvar org-indirect-dedicated-frame nil
  5516. "This is the frame being used for indirect tree display.")
  5517. (defvar org-last-indirect-buffer nil)
  5518. (defun org-tree-to-indirect-buffer (&optional arg)
  5519. "Create indirect buffer and narrow it to current subtree.
  5520. With numerical prefix ARG, go up to this level and then take that tree.
  5521. If ARG is negative, go up that many levels.
  5522. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5523. indirect buffer previously made with this command, to avoid proliferation of
  5524. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5525. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5526. is kept so that you can work with several indirect buffers at the same time.
  5527. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5528. requests that a new frame be made for the new buffer, so that the dedicated
  5529. frame is not changed."
  5530. (interactive "P")
  5531. (let ((cbuf (current-buffer))
  5532. (cwin (selected-window))
  5533. (pos (point))
  5534. beg end level heading ibuf)
  5535. (save-excursion
  5536. (org-back-to-heading t)
  5537. (when (numberp arg)
  5538. (setq level (org-outline-level))
  5539. (if (< arg 0) (setq arg (+ level arg)))
  5540. (while (> (setq level (org-outline-level)) arg)
  5541. (outline-up-heading 1 t)))
  5542. (setq beg (point)
  5543. heading (org-get-heading))
  5544. (org-end-of-subtree t t)
  5545. (if (org-on-heading-p) (backward-char 1))
  5546. (setq end (point)))
  5547. (if (and (buffer-live-p org-last-indirect-buffer)
  5548. (not (eq org-indirect-buffer-display 'new-frame))
  5549. (not arg))
  5550. (kill-buffer org-last-indirect-buffer))
  5551. (setq ibuf (org-get-indirect-buffer cbuf)
  5552. org-last-indirect-buffer ibuf)
  5553. (cond
  5554. ((or (eq org-indirect-buffer-display 'new-frame)
  5555. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5556. (select-frame (make-frame))
  5557. (delete-other-windows)
  5558. (switch-to-buffer ibuf)
  5559. (org-set-frame-title heading))
  5560. ((eq org-indirect-buffer-display 'dedicated-frame)
  5561. (raise-frame
  5562. (select-frame (or (and org-indirect-dedicated-frame
  5563. (frame-live-p org-indirect-dedicated-frame)
  5564. org-indirect-dedicated-frame)
  5565. (setq org-indirect-dedicated-frame (make-frame)))))
  5566. (delete-other-windows)
  5567. (switch-to-buffer ibuf)
  5568. (org-set-frame-title (concat "Indirect: " heading)))
  5569. ((eq org-indirect-buffer-display 'current-window)
  5570. (switch-to-buffer ibuf))
  5571. ((eq org-indirect-buffer-display 'other-window)
  5572. (pop-to-buffer ibuf))
  5573. (t (error "Invalid value")))
  5574. (if (featurep 'xemacs)
  5575. (save-excursion (org-mode) (turn-on-font-lock)))
  5576. (narrow-to-region beg end)
  5577. (show-all)
  5578. (goto-char pos)
  5579. (and (window-live-p cwin) (select-window cwin))))
  5580. (defun org-get-indirect-buffer (&optional buffer)
  5581. (setq buffer (or buffer (current-buffer)))
  5582. (let ((n 1) (base (buffer-name buffer)) bname)
  5583. (while (buffer-live-p
  5584. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5585. (setq n (1+ n)))
  5586. (condition-case nil
  5587. (make-indirect-buffer buffer bname 'clone)
  5588. (error (make-indirect-buffer buffer bname)))))
  5589. (defun org-set-frame-title (title)
  5590. "Set the title of the current frame to the string TITLE."
  5591. ;; FIXME: how to name a single frame in XEmacs???
  5592. (unless (featurep 'xemacs)
  5593. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5594. ;;;; Structure editing
  5595. ;;; Inserting headlines
  5596. (defun org-previous-line-empty-p ()
  5597. (save-excursion
  5598. (and (not (bobp))
  5599. (or (beginning-of-line 0) t)
  5600. (save-match-data
  5601. (looking-at "[ \t]*$")))))
  5602. (defun org-insert-heading (&optional force-heading invisible-ok)
  5603. "Insert a new heading or item with same depth at point.
  5604. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5605. If point is at the beginning of a headline, insert a sibling before the
  5606. current headline. If point is not at the beginning, do not split the line,
  5607. but create the new headline after the current line.
  5608. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  5609. This is important for non-interactive uses of the command."
  5610. (interactive "P")
  5611. (if (or (= (buffer-size) 0)
  5612. (and (not (save-excursion (and (ignore-errors (org-back-to-heading invisible-ok))
  5613. (org-on-heading-p))))
  5614. (not (org-in-item-p))))
  5615. (insert "\n* ")
  5616. (when (or force-heading (not (org-insert-item)))
  5617. (let* ((empty-line-p nil)
  5618. (head (save-excursion
  5619. (condition-case nil
  5620. (progn
  5621. (org-back-to-heading invisible-ok)
  5622. (setq empty-line-p (org-previous-line-empty-p))
  5623. (match-string 0))
  5624. (error "*"))))
  5625. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5626. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5627. pos hide-previous previous-pos)
  5628. (cond
  5629. ((and (org-on-heading-p) (bolp)
  5630. (or (bobp)
  5631. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5632. ;; insert before the current line
  5633. (open-line (if blank 2 1)))
  5634. ((and (bolp)
  5635. (not org-insert-heading-respect-content)
  5636. (or (bobp)
  5637. (save-excursion
  5638. (backward-char 1) (not (org-invisible-p)))))
  5639. ;; insert right here
  5640. nil)
  5641. (t
  5642. ;; somewhere in the line
  5643. (save-excursion
  5644. (setq previous-pos (point-at-bol))
  5645. (end-of-line)
  5646. (setq hide-previous (org-invisible-p)))
  5647. (and org-insert-heading-respect-content (org-show-subtree))
  5648. (let ((split
  5649. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5650. (save-excursion
  5651. (let ((p (point)))
  5652. (goto-char (point-at-bol))
  5653. (and (looking-at org-complex-heading-regexp)
  5654. (> p (match-beginning 4)))))))
  5655. tags pos)
  5656. (cond
  5657. (org-insert-heading-respect-content
  5658. (org-end-of-subtree nil t)
  5659. (or (bolp) (newline))
  5660. (or (org-previous-line-empty-p)
  5661. (and blank (newline)))
  5662. (open-line 1))
  5663. ((org-on-heading-p)
  5664. (when hide-previous
  5665. (show-children)
  5666. (org-show-entry))
  5667. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5668. (setq tags (and (match-end 2) (match-string 2)))
  5669. (and (match-end 1)
  5670. (delete-region (match-beginning 1) (match-end 1)))
  5671. (setq pos (point-at-bol))
  5672. (or split (end-of-line 1))
  5673. (delete-horizontal-space)
  5674. (if (string-match "\\`\\*+\\'"
  5675. (buffer-substring (point-at-bol) (point)))
  5676. (insert " "))
  5677. (newline (if blank 2 1))
  5678. (when tags
  5679. (save-excursion
  5680. (goto-char pos)
  5681. (end-of-line 1)
  5682. (insert " " tags)
  5683. (org-set-tags nil 'align))))
  5684. (t
  5685. (or split (end-of-line 1))
  5686. (newline (if blank 2 1)))))))
  5687. (insert head) (just-one-space)
  5688. (setq pos (point))
  5689. (end-of-line 1)
  5690. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5691. (when (and org-insert-heading-respect-content hide-previous)
  5692. (save-excursion
  5693. (goto-char previous-pos)
  5694. (hide-subtree)))
  5695. (run-hooks 'org-insert-heading-hook)))))
  5696. (defun org-get-heading (&optional no-tags)
  5697. "Return the heading of the current entry, without the stars."
  5698. (save-excursion
  5699. (org-back-to-heading t)
  5700. (if (looking-at
  5701. (if no-tags
  5702. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5703. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5704. (match-string 1) "")))
  5705. (defun org-heading-components ()
  5706. "Return the components of the current heading.
  5707. This is a list with the following elements:
  5708. - the level as an integer
  5709. - the reduced level, different if `org-odd-levels-only' is set.
  5710. - the TODO keyword, or nil
  5711. - the priority character, like ?A, or nil if no priority is given
  5712. - the headline text itself, or the tags string if no headline text
  5713. - the tags string, or nil."
  5714. (save-excursion
  5715. (org-back-to-heading t)
  5716. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  5717. (list (length (match-string 1))
  5718. (org-reduced-level (length (match-string 1)))
  5719. (org-match-string-no-properties 2)
  5720. (and (match-end 3) (aref (match-string 3) 2))
  5721. (org-match-string-no-properties 4)
  5722. (org-match-string-no-properties 5)))))
  5723. (defun org-get-entry ()
  5724. "Get the entry text, after heading, entire subtree."
  5725. (save-excursion
  5726. (org-back-to-heading t)
  5727. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5728. (defun org-insert-heading-after-current ()
  5729. "Insert a new heading with same level as current, after current subtree."
  5730. (interactive)
  5731. (org-back-to-heading)
  5732. (org-insert-heading)
  5733. (org-move-subtree-down)
  5734. (end-of-line 1))
  5735. (defun org-insert-heading-respect-content ()
  5736. (interactive)
  5737. (let ((org-insert-heading-respect-content t))
  5738. (org-insert-heading t)))
  5739. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5740. (interactive "P")
  5741. (let ((org-insert-heading-respect-content t))
  5742. (org-insert-todo-heading force-state t)))
  5743. (defun org-insert-todo-heading (arg &optional force-heading)
  5744. "Insert a new heading with the same level and TODO state as current heading.
  5745. If the heading has no TODO state, or if the state is DONE, use the first
  5746. state (TODO by default). Also with prefix arg, force first state."
  5747. (interactive "P")
  5748. (when (or force-heading (not (org-insert-item 'checkbox)))
  5749. (org-insert-heading force-heading)
  5750. (save-excursion
  5751. (org-back-to-heading)
  5752. (outline-previous-heading)
  5753. (looking-at org-todo-line-regexp))
  5754. (let*
  5755. ((new-mark-x
  5756. (if (or arg
  5757. (not (match-beginning 2))
  5758. (member (match-string 2) org-done-keywords))
  5759. (car org-todo-keywords-1)
  5760. (match-string 2)))
  5761. (new-mark
  5762. (or
  5763. (run-hook-with-args-until-success
  5764. 'org-todo-get-default-hook new-mark-x nil)
  5765. new-mark-x)))
  5766. (beginning-of-line 1)
  5767. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5768. (if org-treat-insert-todo-heading-as-state-change
  5769. (org-todo new-mark)
  5770. (insert new-mark " "))))
  5771. (when org-provide-todo-statistics
  5772. (org-update-parent-todo-statistics))))
  5773. (defun org-insert-subheading (arg)
  5774. "Insert a new subheading and demote it.
  5775. Works for outline headings and for plain lists alike."
  5776. (interactive "P")
  5777. (org-insert-heading arg)
  5778. (cond
  5779. ((org-on-heading-p) (org-do-demote))
  5780. ((org-at-item-p) (org-indent-item 1))))
  5781. (defun org-insert-todo-subheading (arg)
  5782. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5783. Works for outline headings and for plain lists alike."
  5784. (interactive "P")
  5785. (org-insert-todo-heading arg)
  5786. (cond
  5787. ((org-on-heading-p) (org-do-demote))
  5788. ((org-at-item-p) (org-indent-item 1))))
  5789. ;;; Promotion and Demotion
  5790. (defvar org-after-demote-entry-hook nil
  5791. "Hook run after an entry has been demoted.
  5792. The cursor will be at the beginning of the entry.
  5793. When a subtree is being demoted, the hook will be called for each node.")
  5794. (defvar org-after-promote-entry-hook nil
  5795. "Hook run after an entry has been promoted.
  5796. The cursor will be at the beginning of the entry.
  5797. When a subtree is being promoted, the hook will be called for each node.")
  5798. (defun org-promote-subtree ()
  5799. "Promote the entire subtree.
  5800. See also `org-promote'."
  5801. (interactive)
  5802. (save-excursion
  5803. (org-map-tree 'org-promote))
  5804. (org-fix-position-after-promote))
  5805. (defun org-demote-subtree ()
  5806. "Demote the entire subtree. See `org-demote'.
  5807. See also `org-promote'."
  5808. (interactive)
  5809. (save-excursion
  5810. (org-map-tree 'org-demote))
  5811. (org-fix-position-after-promote))
  5812. (defun org-do-promote ()
  5813. "Promote the current heading higher up the tree.
  5814. If the region is active in `transient-mark-mode', promote all headings
  5815. in the region."
  5816. (interactive)
  5817. (save-excursion
  5818. (if (org-region-active-p)
  5819. (org-map-region 'org-promote (region-beginning) (region-end))
  5820. (org-promote)))
  5821. (org-fix-position-after-promote))
  5822. (defun org-do-demote ()
  5823. "Demote the current heading lower down the tree.
  5824. If the region is active in `transient-mark-mode', demote all headings
  5825. in the region."
  5826. (interactive)
  5827. (save-excursion
  5828. (if (org-region-active-p)
  5829. (org-map-region 'org-demote (region-beginning) (region-end))
  5830. (org-demote)))
  5831. (org-fix-position-after-promote))
  5832. (defun org-fix-position-after-promote ()
  5833. "Make sure that after pro/demotion cursor position is right."
  5834. (let ((pos (point)))
  5835. (when (save-excursion
  5836. (beginning-of-line 1)
  5837. (looking-at org-todo-line-regexp)
  5838. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5839. (cond ((eobp) (insert " "))
  5840. ((eolp) (insert " "))
  5841. ((equal (char-after) ?\ ) (forward-char 1))))))
  5842. (defun org-current-level ()
  5843. "Return the level of the current entry, or nil if before the first headline.
  5844. The level is the number of stars at the beginning of the headline."
  5845. (save-excursion
  5846. (condition-case nil
  5847. (progn
  5848. (org-back-to-heading t)
  5849. (funcall outline-level))
  5850. (error nil))))
  5851. (defun org-get-previous-line-level ()
  5852. "Return the outline depth of the last headline before the current line.
  5853. Returns 0 for the first headline in the buffer, and nil if before the
  5854. first headline."
  5855. (let ((current-level (org-current-level))
  5856. (prev-level (when (> (line-number-at-pos) 1)
  5857. (save-excursion
  5858. (beginning-of-line 0)
  5859. (org-current-level)))))
  5860. (cond ((null current-level) nil) ; Before first headline
  5861. ((null prev-level) 0) ; At first headline
  5862. (prev-level))))
  5863. (defun org-reduced-level (l)
  5864. "Compute the effective level of a heading.
  5865. This takes into account the setting of `org-odd-levels-only'."
  5866. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5867. (defun org-level-increment ()
  5868. "Return the number of stars that will be added or removed at a
  5869. time to headlines when structure editing, based on the value of
  5870. `org-odd-levels-only'."
  5871. (if org-odd-levels-only 2 1))
  5872. (defun org-get-valid-level (level &optional change)
  5873. "Rectify a level change under the influence of `org-odd-levels-only'
  5874. LEVEL is a current level, CHANGE is by how much the level should be
  5875. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5876. even level numbers will become the next higher odd number."
  5877. (if org-odd-levels-only
  5878. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5879. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5880. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5881. (max 1 (+ level (or change 0)))))
  5882. (if (boundp 'define-obsolete-function-alias)
  5883. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5884. (define-obsolete-function-alias 'org-get-legal-level
  5885. 'org-get-valid-level)
  5886. (define-obsolete-function-alias 'org-get-legal-level
  5887. 'org-get-valid-level "23.1")))
  5888. (defun org-promote ()
  5889. "Promote the current heading higher up the tree.
  5890. If the region is active in `transient-mark-mode', promote all headings
  5891. in the region."
  5892. (org-back-to-heading t)
  5893. (let* ((level (save-match-data (funcall outline-level)))
  5894. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5895. (diff (abs (- level (length up-head) -1))))
  5896. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5897. (replace-match up-head nil t)
  5898. ;; Fixup tag positioning
  5899. (and org-auto-align-tags (org-set-tags nil t))
  5900. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5901. (run-hooks 'org-after-promote-entry-hook)))
  5902. (defun org-demote ()
  5903. "Demote the current heading lower down the tree.
  5904. If the region is active in `transient-mark-mode', demote all headings
  5905. in the region."
  5906. (org-back-to-heading t)
  5907. (let* ((level (save-match-data (funcall outline-level)))
  5908. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5909. (diff (abs (- level (length down-head) -1))))
  5910. (replace-match down-head nil t)
  5911. ;; Fixup tag positioning
  5912. (and org-auto-align-tags (org-set-tags nil t))
  5913. (if org-adapt-indentation (org-fixup-indentation diff))
  5914. (run-hooks 'org-after-demote-entry-hook)))
  5915. (defun org-cycle-level ()
  5916. "Cycle the level of an empty headline through possible states.
  5917. This goes first to child, then to parent, level, then up the hierarchy.
  5918. After top level, it switches back to sibling level."
  5919. (interactive)
  5920. (let ((org-adapt-indentation nil))
  5921. (when (org-point-at-end-of-empty-headline)
  5922. (setq this-command 'org-cycle-level) ; Only needed for caching
  5923. (let ((cur-level (org-current-level))
  5924. (prev-level (org-get-previous-line-level)))
  5925. (cond
  5926. ;; If first headline in file, promote to top-level.
  5927. ((= prev-level 0)
  5928. (loop repeat (/ (- cur-level 1) (org-level-increment))
  5929. do (org-do-promote)))
  5930. ;; If same level as prev, demote one.
  5931. ((= prev-level cur-level)
  5932. (org-do-demote))
  5933. ;; If parent is top-level, promote to top level if not already.
  5934. ((= prev-level 1)
  5935. (loop repeat (/ (- cur-level 1) (org-level-increment))
  5936. do (org-do-promote)))
  5937. ;; If top-level, return to prev-level.
  5938. ((= cur-level 1)
  5939. (loop repeat (/ (- prev-level 1) (org-level-increment))
  5940. do (org-do-demote)))
  5941. ;; If less than prev-level, promote one.
  5942. ((< cur-level prev-level)
  5943. (org-do-promote))
  5944. ;; If deeper than prev-level, promote until higher than
  5945. ;; prev-level.
  5946. ((> cur-level prev-level)
  5947. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  5948. do (org-do-promote))))
  5949. t))))
  5950. (defun org-map-tree (fun)
  5951. "Call FUN for every heading underneath the current one."
  5952. (org-back-to-heading)
  5953. (let ((level (funcall outline-level)))
  5954. (save-excursion
  5955. (funcall fun)
  5956. (while (and (progn
  5957. (outline-next-heading)
  5958. (> (funcall outline-level) level))
  5959. (not (eobp)))
  5960. (funcall fun)))))
  5961. (defun org-map-region (fun beg end)
  5962. "Call FUN for every heading between BEG and END."
  5963. (let ((org-ignore-region t))
  5964. (save-excursion
  5965. (setq end (copy-marker end))
  5966. (goto-char beg)
  5967. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5968. (< (point) end))
  5969. (funcall fun))
  5970. (while (and (progn
  5971. (outline-next-heading)
  5972. (< (point) end))
  5973. (not (eobp)))
  5974. (funcall fun)))))
  5975. (defun org-fixup-indentation (diff)
  5976. "Change the indentation in the current entry by DIFF
  5977. However, if any line in the current entry has no indentation, or if it
  5978. would end up with no indentation after the change, nothing at all is done."
  5979. (save-excursion
  5980. (let ((end (save-excursion (outline-next-heading)
  5981. (point-marker)))
  5982. (prohibit (if (> diff 0)
  5983. "^\\S-"
  5984. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5985. col)
  5986. (unless (save-excursion (end-of-line 1)
  5987. (re-search-forward prohibit end t))
  5988. (while (and (< (point) end)
  5989. (re-search-forward "^[ \t]+" end t))
  5990. (goto-char (match-end 0))
  5991. (setq col (current-column))
  5992. (if (< diff 0) (replace-match ""))
  5993. (org-indent-to-column (+ diff col))))
  5994. (move-marker end nil))))
  5995. (defun org-convert-to-odd-levels ()
  5996. "Convert an org-mode file with all levels allowed to one with odd levels.
  5997. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5998. level 5 etc."
  5999. (interactive)
  6000. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6001. (let ((outline-regexp org-outline-regexp)
  6002. (outline-level 'org-outline-level)
  6003. (org-odd-levels-only nil) n)
  6004. (save-excursion
  6005. (goto-char (point-min))
  6006. (while (re-search-forward "^\\*\\*+ " nil t)
  6007. (setq n (- (length (match-string 0)) 2))
  6008. (while (>= (setq n (1- n)) 0)
  6009. (org-demote))
  6010. (end-of-line 1))))))
  6011. (defun org-convert-to-oddeven-levels ()
  6012. "Convert an org-mode file with only odd levels to one with odd and even levels.
  6013. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  6014. section with an even level, conversion would destroy the structure of the file. An error
  6015. is signaled in this case."
  6016. (interactive)
  6017. (goto-char (point-min))
  6018. ;; First check if there are no even levels
  6019. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6020. (org-show-context t)
  6021. (error "Not all levels are odd in this file. Conversion not possible"))
  6022. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6023. (let ((outline-regexp org-outline-regexp)
  6024. (outline-level 'org-outline-level)
  6025. (org-odd-levels-only nil) n)
  6026. (save-excursion
  6027. (goto-char (point-min))
  6028. (while (re-search-forward "^\\*\\*+ " nil t)
  6029. (setq n (/ (1- (length (match-string 0))) 2))
  6030. (while (>= (setq n (1- n)) 0)
  6031. (org-promote))
  6032. (end-of-line 1))))))
  6033. (defun org-tr-level (n)
  6034. "Make N odd if required."
  6035. (if org-odd-levels-only (1+ (/ n 2)) n))
  6036. ;;; Vertical tree motion, cutting and pasting of subtrees
  6037. (defun org-move-subtree-up (&optional arg)
  6038. "Move the current subtree up past ARG headlines of the same level."
  6039. (interactive "p")
  6040. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6041. (defun org-move-subtree-down (&optional arg)
  6042. "Move the current subtree down past ARG headlines of the same level."
  6043. (interactive "p")
  6044. (setq arg (prefix-numeric-value arg))
  6045. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6046. 'org-get-last-sibling))
  6047. (ins-point (make-marker))
  6048. (cnt (abs arg))
  6049. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6050. ;; Select the tree
  6051. (org-back-to-heading)
  6052. (setq beg0 (point))
  6053. (save-excursion
  6054. (setq ne-beg (org-back-over-empty-lines))
  6055. (setq beg (point)))
  6056. (save-match-data
  6057. (save-excursion (outline-end-of-heading)
  6058. (setq folded (org-invisible-p)))
  6059. (outline-end-of-subtree))
  6060. (outline-next-heading)
  6061. (setq ne-end (org-back-over-empty-lines))
  6062. (setq end (point))
  6063. (goto-char beg0)
  6064. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6065. ;; include less whitespace
  6066. (save-excursion
  6067. (goto-char beg)
  6068. (forward-line (- ne-beg ne-end))
  6069. (setq beg (point))))
  6070. ;; Find insertion point, with error handling
  6071. (while (> cnt 0)
  6072. (or (and (funcall movfunc) (looking-at outline-regexp))
  6073. (progn (goto-char beg0)
  6074. (error "Cannot move past superior level or buffer limit")))
  6075. (setq cnt (1- cnt)))
  6076. (if (> arg 0)
  6077. ;; Moving forward - still need to move over subtree
  6078. (progn (org-end-of-subtree t t)
  6079. (save-excursion
  6080. (org-back-over-empty-lines)
  6081. (or (bolp) (newline)))))
  6082. (setq ne-ins (org-back-over-empty-lines))
  6083. (move-marker ins-point (point))
  6084. (setq txt (buffer-substring beg end))
  6085. (org-save-markers-in-region beg end)
  6086. (delete-region beg end)
  6087. (org-remove-empty-overlays-at beg)
  6088. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6089. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6090. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6091. (let ((bbb (point)))
  6092. (insert-before-markers txt)
  6093. (org-reinstall-markers-in-region bbb)
  6094. (move-marker ins-point bbb))
  6095. (or (bolp) (insert "\n"))
  6096. (setq ins-end (point))
  6097. (goto-char ins-point)
  6098. (org-skip-whitespace)
  6099. (when (and (< arg 0)
  6100. (org-first-sibling-p)
  6101. (> ne-ins ne-beg))
  6102. ;; Move whitespace back to beginning
  6103. (save-excursion
  6104. (goto-char ins-end)
  6105. (let ((kill-whole-line t))
  6106. (kill-line (- ne-ins ne-beg)) (point)))
  6107. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6108. (move-marker ins-point nil)
  6109. (if folded
  6110. (hide-subtree)
  6111. (org-show-entry)
  6112. (show-children)
  6113. (org-cycle-hide-drawers 'children))
  6114. (org-clean-visibility-after-subtree-move)))
  6115. (defvar org-subtree-clip ""
  6116. "Clipboard for cut and paste of subtrees.
  6117. This is actually only a copy of the kill, because we use the normal kill
  6118. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6119. (defvar org-subtree-clip-folded nil
  6120. "Was the last copied subtree folded?
  6121. This is used to fold the tree back after pasting.")
  6122. (defun org-cut-subtree (&optional n)
  6123. "Cut the current subtree into the clipboard.
  6124. With prefix arg N, cut this many sequential subtrees.
  6125. This is a short-hand for marking the subtree and then cutting it."
  6126. (interactive "p")
  6127. (org-copy-subtree n 'cut))
  6128. (defun org-copy-subtree (&optional n cut force-store-markers)
  6129. "Cut the current subtree into the clipboard.
  6130. With prefix arg N, cut this many sequential subtrees.
  6131. This is a short-hand for marking the subtree and then copying it.
  6132. If CUT is non-nil, actually cut the subtree.
  6133. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6134. of some markers in the region, even if CUT is non-nil. This is
  6135. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6136. (interactive "p")
  6137. (let (beg end folded (beg0 (point)))
  6138. (if (interactive-p)
  6139. (org-back-to-heading nil) ; take what looks like a subtree
  6140. (org-back-to-heading t)) ; take what is really there
  6141. (org-back-over-empty-lines)
  6142. (setq beg (point))
  6143. (skip-chars-forward " \t\r\n")
  6144. (save-match-data
  6145. (save-excursion (outline-end-of-heading)
  6146. (setq folded (org-invisible-p)))
  6147. (condition-case nil
  6148. (org-forward-same-level (1- n) t)
  6149. (error nil))
  6150. (org-end-of-subtree t t))
  6151. (org-back-over-empty-lines)
  6152. (setq end (point))
  6153. (goto-char beg0)
  6154. (when (> end beg)
  6155. (setq org-subtree-clip-folded folded)
  6156. (when (or cut force-store-markers)
  6157. (org-save-markers-in-region beg end))
  6158. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6159. (setq org-subtree-clip (current-kill 0))
  6160. (message "%s: Subtree(s) with %d characters"
  6161. (if cut "Cut" "Copied")
  6162. (length org-subtree-clip)))))
  6163. (defun org-paste-subtree (&optional level tree for-yank)
  6164. "Paste the clipboard as a subtree, with modification of headline level.
  6165. The entire subtree is promoted or demoted in order to match a new headline
  6166. level.
  6167. If the cursor is at the beginning of a headline, the same level as
  6168. that headline is used to paste the tree
  6169. If not, the new level is derived from the *visible* headings
  6170. before and after the insertion point, and taken to be the inferior headline
  6171. level of the two. So if the previous visible heading is level 3 and the
  6172. next is level 4 (or vice versa), level 4 will be used for insertion.
  6173. This makes sure that the subtree remains an independent subtree and does
  6174. not swallow low level entries.
  6175. You can also force a different level, either by using a numeric prefix
  6176. argument, or by inserting the heading marker by hand. For example, if the
  6177. cursor is after \"*****\", then the tree will be shifted to level 5.
  6178. If optional TREE is given, use this text instead of the kill ring.
  6179. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6180. move back over whitespace before inserting, and move point to the end of
  6181. the inserted text when done."
  6182. (interactive "P")
  6183. (setq tree (or tree (and kill-ring (current-kill 0))))
  6184. (unless (org-kill-is-subtree-p tree)
  6185. (error "%s"
  6186. (substitute-command-keys
  6187. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6188. (let* ((visp (not (org-invisible-p)))
  6189. (txt tree)
  6190. (^re (concat "^\\(" outline-regexp "\\)"))
  6191. (re (concat "\\(" outline-regexp "\\)"))
  6192. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6193. (old-level (if (string-match ^re txt)
  6194. (- (match-end 0) (match-beginning 0) 1)
  6195. -1))
  6196. (force-level (cond (level (prefix-numeric-value level))
  6197. ((and (looking-at "[ \t]*$")
  6198. (string-match
  6199. ^re_ (buffer-substring
  6200. (point-at-bol) (point))))
  6201. (- (match-end 1) (match-beginning 1)))
  6202. ((and (bolp)
  6203. (looking-at org-outline-regexp))
  6204. (- (match-end 0) (point) 1))
  6205. (t nil)))
  6206. (previous-level (save-excursion
  6207. (condition-case nil
  6208. (progn
  6209. (outline-previous-visible-heading 1)
  6210. (if (looking-at re)
  6211. (- (match-end 0) (match-beginning 0) 1)
  6212. 1))
  6213. (error 1))))
  6214. (next-level (save-excursion
  6215. (condition-case nil
  6216. (progn
  6217. (or (looking-at outline-regexp)
  6218. (outline-next-visible-heading 1))
  6219. (if (looking-at re)
  6220. (- (match-end 0) (match-beginning 0) 1)
  6221. 1))
  6222. (error 1))))
  6223. (new-level (or force-level (max previous-level next-level)))
  6224. (shift (if (or (= old-level -1)
  6225. (= new-level -1)
  6226. (= old-level new-level))
  6227. 0
  6228. (- new-level old-level)))
  6229. (delta (if (> shift 0) -1 1))
  6230. (func (if (> shift 0) 'org-demote 'org-promote))
  6231. (org-odd-levels-only nil)
  6232. beg end newend)
  6233. ;; Remove the forced level indicator
  6234. (if force-level
  6235. (delete-region (point-at-bol) (point)))
  6236. ;; Paste
  6237. (beginning-of-line 1)
  6238. (unless for-yank (org-back-over-empty-lines))
  6239. (setq beg (point))
  6240. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6241. (insert-before-markers txt)
  6242. (unless (string-match "\n\\'" txt) (insert "\n"))
  6243. (setq newend (point))
  6244. (org-reinstall-markers-in-region beg)
  6245. (setq end (point))
  6246. (goto-char beg)
  6247. (skip-chars-forward " \t\n\r")
  6248. (setq beg (point))
  6249. (if (and (org-invisible-p) visp)
  6250. (save-excursion (outline-show-heading)))
  6251. ;; Shift if necessary
  6252. (unless (= shift 0)
  6253. (save-restriction
  6254. (narrow-to-region beg end)
  6255. (while (not (= shift 0))
  6256. (org-map-region func (point-min) (point-max))
  6257. (setq shift (+ delta shift)))
  6258. (goto-char (point-min))
  6259. (setq newend (point-max))))
  6260. (when (or (interactive-p) for-yank)
  6261. (message "Clipboard pasted as level %d subtree" new-level))
  6262. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6263. kill-ring
  6264. (eq org-subtree-clip (current-kill 0))
  6265. org-subtree-clip-folded)
  6266. ;; The tree was folded before it was killed/copied
  6267. (hide-subtree))
  6268. (and for-yank (goto-char newend))))
  6269. (defun org-kill-is-subtree-p (&optional txt)
  6270. "Check if the current kill is an outline subtree, or a set of trees.
  6271. Returns nil if kill does not start with a headline, or if the first
  6272. headline level is not the largest headline level in the tree.
  6273. So this will actually accept several entries of equal levels as well,
  6274. which is OK for `org-paste-subtree'.
  6275. If optional TXT is given, check this string instead of the current kill."
  6276. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6277. (start-level (and kill
  6278. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6279. org-outline-regexp "\\)")
  6280. kill)
  6281. (- (match-end 2) (match-beginning 2) 1)))
  6282. (re (concat "^" org-outline-regexp))
  6283. (start (1+ (or (match-beginning 2) -1))))
  6284. (if (not start-level)
  6285. (progn
  6286. nil) ;; does not even start with a heading
  6287. (catch 'exit
  6288. (while (setq start (string-match re kill (1+ start)))
  6289. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6290. (throw 'exit nil)))
  6291. t))))
  6292. (defvar org-markers-to-move nil
  6293. "Markers that should be moved with a cut-and-paste operation.
  6294. Those markers are stored together with their positions relative to
  6295. the start of the region.")
  6296. (defun org-save-markers-in-region (beg end)
  6297. "Check markers in region.
  6298. If these markers are between BEG and END, record their position relative
  6299. to BEG, so that after moving the block of text, we can put the markers back
  6300. into place.
  6301. This function gets called just before an entry or tree gets cut from the
  6302. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6303. called immediately, to move the markers with the entries."
  6304. (setq org-markers-to-move nil)
  6305. (when (featurep 'org-clock)
  6306. (org-clock-save-markers-for-cut-and-paste beg end))
  6307. (when (featurep 'org-agenda)
  6308. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6309. (defun org-check-and-save-marker (marker beg end)
  6310. "Check if MARKER is between BEG and END.
  6311. If yes, remember the marker and the distance to BEG."
  6312. (when (and (marker-buffer marker)
  6313. (equal (marker-buffer marker) (current-buffer)))
  6314. (if (and (>= marker beg) (< marker end))
  6315. (push (cons marker (- marker beg)) org-markers-to-move))))
  6316. (defun org-reinstall-markers-in-region (beg)
  6317. "Move all remembered markers to their position relative to BEG."
  6318. (mapc (lambda (x)
  6319. (move-marker (car x) (+ beg (cdr x))))
  6320. org-markers-to-move)
  6321. (setq org-markers-to-move nil))
  6322. (defun org-narrow-to-subtree ()
  6323. "Narrow buffer to the current subtree."
  6324. (interactive)
  6325. (save-excursion
  6326. (save-match-data
  6327. (narrow-to-region
  6328. (progn (org-back-to-heading t) (point))
  6329. (progn (org-end-of-subtree t t)
  6330. (if (org-on-heading-p) (backward-char 1))
  6331. (point))))))
  6332. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6333. "Clone the task (subtree) at point N times.
  6334. The clones will be inserted as siblings.
  6335. In interactive use, the user will be prompted for the number of clones
  6336. to be produced, and for a time SHIFT, which may be a repeater as used
  6337. in time stamps, for example `+3d'.
  6338. When a valid repeater is given and the entry contains any time stamps,
  6339. the clones will become a sequence in time, with time stamps in the
  6340. subtree shifted for each clone produced. If SHIFT is nil or the
  6341. empty string, time stamps will be left alone.
  6342. If the original subtree did contain time stamps with a repeater,
  6343. the following will happen:
  6344. - the repeater will be removed in each clone
  6345. - an additional clone will be produced, with the current, unshifted
  6346. date(s) in the entry.
  6347. - the original entry will be placed *after* all the clones, with
  6348. repeater intact.
  6349. - the start days in the repeater in the original entry will be shifted
  6350. to past the last clone.
  6351. I this way you can spell out a number of instances of a repeating task,
  6352. and still retain the repeater to cover future instances of the task."
  6353. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6354. (let (beg end template task
  6355. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6356. (if (not (and (integerp n) (> n 0)))
  6357. (error "Invalid number of replications %s" n))
  6358. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6359. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6360. shift)))
  6361. (error "Invalid shift specification %s" shift))
  6362. (when doshift
  6363. (setq shift-n (string-to-number (match-string 1 shift))
  6364. shift-what (cdr (assoc (match-string 2 shift)
  6365. '(("d" . day) ("w" . week)
  6366. ("m" . month) ("y" . year))))))
  6367. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6368. (setq nmin 1 nmax n)
  6369. (org-back-to-heading t)
  6370. (setq beg (point))
  6371. (org-end-of-subtree t t)
  6372. (or (bolp) (insert "\n"))
  6373. (setq end (point))
  6374. (setq template (buffer-substring beg end))
  6375. (when (and doshift
  6376. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6377. (delete-region beg end)
  6378. (setq end beg)
  6379. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6380. (goto-char end)
  6381. (loop for n from nmin to nmax do
  6382. (if (not doshift)
  6383. (setq task template)
  6384. (with-temp-buffer
  6385. (insert template)
  6386. (org-mode)
  6387. (goto-char (point-min))
  6388. (while (re-search-forward org-ts-regexp-both nil t)
  6389. (org-timestamp-change (* n shift-n) shift-what))
  6390. (unless (= n n-no-remove)
  6391. (goto-char (point-min))
  6392. (while (re-search-forward org-ts-regexp nil t)
  6393. (save-excursion
  6394. (goto-char (match-beginning 0))
  6395. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6396. (delete-region (match-beginning 1) (match-end 1))))))
  6397. (setq task (buffer-string))))
  6398. (insert task))
  6399. (goto-char beg)))
  6400. ;;; Outline Sorting
  6401. (defun org-sort (with-case)
  6402. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6403. Optional argument WITH-CASE means sort case-sensitively.
  6404. With a double prefix argument, also remove duplicate entries."
  6405. (interactive "P")
  6406. (if (org-at-table-p)
  6407. (org-call-with-arg 'org-table-sort-lines with-case)
  6408. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6409. (defun org-sort-remove-invisible (s)
  6410. (remove-text-properties 0 (length s) org-rm-props s)
  6411. (while (string-match org-bracket-link-regexp s)
  6412. (setq s (replace-match (if (match-end 2)
  6413. (match-string 3 s)
  6414. (match-string 1 s)) t t s)))
  6415. s)
  6416. (defvar org-priority-regexp) ; defined later in the file
  6417. (defvar org-after-sorting-entries-or-items-hook nil
  6418. "Hook that is run after a bunch of entries or items have been sorted.
  6419. When children are sorted, the cursor is in the parent line when this
  6420. hook gets called. When a region or a plain list is sorted, the cursor
  6421. will be in the first entry of the sorted region/list.")
  6422. (defun org-sort-entries-or-items
  6423. (&optional with-case sorting-type getkey-func compare-func property)
  6424. "Sort entries on a certain level of an outline tree, or plain list items.
  6425. If there is an active region, the entries in the region are sorted.
  6426. Else, if the cursor is before the first entry, sort the top-level items.
  6427. Else, the children of the entry at point are sorted.
  6428. If the cursor is at the first item in a plain list, the list items will be
  6429. sorted.
  6430. Sorting can be alphabetically, numerically, by date/time as given by
  6431. a time stamp, by a property or by priority.
  6432. The command prompts for the sorting type unless it has been given to the
  6433. function through the SORTING-TYPE argument, which needs to be a character,
  6434. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6435. precise meaning of each character:
  6436. n Numerically, by converting the beginning of the entry/item to a number.
  6437. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6438. t By date/time, either the first active time stamp in the entry, or, if
  6439. none exist, by the first inactive one.
  6440. In items, only the first line will be checked.
  6441. s By the scheduled date/time.
  6442. d By deadline date/time.
  6443. c By creation time, which is assumed to be the first inactive time stamp
  6444. at the beginning of a line.
  6445. p By priority according to the cookie.
  6446. r By the value of a property.
  6447. Capital letters will reverse the sort order.
  6448. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6449. called with point at the beginning of the record. It must return either
  6450. a string or a number that should serve as the sorting key for that record.
  6451. Comparing entries ignores case by default. However, with an optional argument
  6452. WITH-CASE, the sorting considers case as well."
  6453. (interactive "P")
  6454. (let ((case-func (if with-case 'identity 'downcase))
  6455. start beg end stars re re2
  6456. txt what tmp plain-list-p)
  6457. ;; Find beginning and end of region to sort
  6458. (cond
  6459. ((org-region-active-p)
  6460. ;; we will sort the region
  6461. (setq end (region-end)
  6462. what "region")
  6463. (goto-char (region-beginning))
  6464. (if (not (org-on-heading-p)) (outline-next-heading))
  6465. (setq start (point)))
  6466. ((org-at-item-p)
  6467. ;; we will sort this plain list
  6468. (org-beginning-of-item-list) (setq start (point))
  6469. (org-end-of-item-list)
  6470. (or (bolp) (insert "\n"))
  6471. (setq end (point))
  6472. (goto-char start)
  6473. (setq plain-list-p t
  6474. what "plain list"))
  6475. ((or (org-on-heading-p)
  6476. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6477. ;; we will sort the children of the current headline
  6478. (org-back-to-heading)
  6479. (setq start (point)
  6480. end (progn (org-end-of-subtree t t)
  6481. (or (bolp) (insert "\n"))
  6482. (org-back-over-empty-lines)
  6483. (point))
  6484. what "children")
  6485. (goto-char start)
  6486. (show-subtree)
  6487. (outline-next-heading))
  6488. (t
  6489. ;; we will sort the top-level entries in this file
  6490. (goto-char (point-min))
  6491. (or (org-on-heading-p) (outline-next-heading))
  6492. (setq start (point))
  6493. (goto-char (point-max))
  6494. (beginning-of-line 1)
  6495. (when (looking-at ".*?\\S-")
  6496. ;; File ends in a non-white line
  6497. (end-of-line 1)
  6498. (insert "\n"))
  6499. (setq end (point-max))
  6500. (setq what "top-level")
  6501. (goto-char start)
  6502. (show-all)))
  6503. (setq beg (point))
  6504. (if (>= beg end) (error "Nothing to sort"))
  6505. (unless plain-list-p
  6506. (looking-at "\\(\\*+\\)")
  6507. (setq stars (match-string 1)
  6508. re (concat "^" (regexp-quote stars) " +")
  6509. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6510. txt (buffer-substring beg end))
  6511. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6512. (if (and (not (equal stars "*")) (string-match re2 txt))
  6513. (error "Region to sort contains a level above the first entry")))
  6514. (unless sorting-type
  6515. (message
  6516. (if plain-list-p
  6517. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6518. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6519. [t]ime [s]cheduled [d]eadline [c]reated
  6520. A/N/T/S/D/C/P/O/F means reversed:")
  6521. what)
  6522. (setq sorting-type (read-char-exclusive))
  6523. (and (= (downcase sorting-type) ?f)
  6524. (setq getkey-func
  6525. (org-icompleting-read "Sort using function: "
  6526. obarray 'fboundp t nil nil))
  6527. (setq getkey-func (intern getkey-func)))
  6528. (and (= (downcase sorting-type) ?r)
  6529. (setq property
  6530. (org-icompleting-read "Property: "
  6531. (mapcar 'list (org-buffer-property-keys t))
  6532. nil t))))
  6533. (message "Sorting entries...")
  6534. (save-restriction
  6535. (narrow-to-region start end)
  6536. (let ((dcst (downcase sorting-type))
  6537. (case-fold-search nil)
  6538. (now (current-time)))
  6539. (sort-subr
  6540. (/= dcst sorting-type)
  6541. ;; This function moves to the beginning character of the "record" to
  6542. ;; be sorted.
  6543. (if plain-list-p
  6544. (lambda nil
  6545. (if (org-at-item-p) t (goto-char (point-max))))
  6546. (lambda nil
  6547. (if (re-search-forward re nil t)
  6548. (goto-char (match-beginning 0))
  6549. (goto-char (point-max)))))
  6550. ;; This function moves to the last character of the "record" being
  6551. ;; sorted.
  6552. (if plain-list-p
  6553. 'org-end-of-item
  6554. (lambda nil
  6555. (save-match-data
  6556. (condition-case nil
  6557. (outline-forward-same-level 1)
  6558. (error
  6559. (goto-char (point-max)))))))
  6560. ;; This function returns the value that gets sorted against.
  6561. (if plain-list-p
  6562. (lambda nil
  6563. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6564. (cond
  6565. ((= dcst ?n)
  6566. (string-to-number (buffer-substring (match-end 0)
  6567. (point-at-eol))))
  6568. ((= dcst ?a)
  6569. (buffer-substring (match-end 0) (point-at-eol)))
  6570. ((= dcst ?t)
  6571. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  6572. (re-search-forward org-ts-regexp-both
  6573. (point-at-eol) t))
  6574. (org-time-string-to-seconds (match-string 0))
  6575. (org-float-time now)))
  6576. ((= dcst ?f)
  6577. (if getkey-func
  6578. (progn
  6579. (setq tmp (funcall getkey-func))
  6580. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6581. tmp)
  6582. (error "Invalid key function `%s'" getkey-func)))
  6583. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6584. (lambda nil
  6585. (cond
  6586. ((= dcst ?n)
  6587. (if (looking-at org-complex-heading-regexp)
  6588. (string-to-number (match-string 4))
  6589. nil))
  6590. ((= dcst ?a)
  6591. (if (looking-at org-complex-heading-regexp)
  6592. (funcall case-func (match-string 4))
  6593. nil))
  6594. ((= dcst ?t)
  6595. (let ((end (save-excursion (outline-next-heading) (point))))
  6596. (if (or (re-search-forward org-ts-regexp end t)
  6597. (re-search-forward org-ts-regexp-both end t))
  6598. (org-time-string-to-seconds (match-string 0))
  6599. (org-float-time now))))
  6600. ((= dcst ?c)
  6601. (let ((end (save-excursion (outline-next-heading) (point))))
  6602. (if (re-search-forward
  6603. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6604. end t)
  6605. (org-time-string-to-seconds (match-string 0))
  6606. (org-float-time now))))
  6607. ((= dcst ?s)
  6608. (let ((end (save-excursion (outline-next-heading) (point))))
  6609. (if (re-search-forward org-scheduled-time-regexp end t)
  6610. (org-time-string-to-seconds (match-string 1))
  6611. (org-float-time now))))
  6612. ((= dcst ?d)
  6613. (let ((end (save-excursion (outline-next-heading) (point))))
  6614. (if (re-search-forward org-deadline-time-regexp end t)
  6615. (org-time-string-to-seconds (match-string 1))
  6616. (org-float-time now))))
  6617. ((= dcst ?p)
  6618. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6619. (string-to-char (match-string 2))
  6620. org-default-priority))
  6621. ((= dcst ?r)
  6622. (or (org-entry-get nil property) ""))
  6623. ((= dcst ?o)
  6624. (if (looking-at org-complex-heading-regexp)
  6625. (- 9999 (length (member (match-string 2)
  6626. org-todo-keywords-1)))))
  6627. ((= dcst ?f)
  6628. (if getkey-func
  6629. (progn
  6630. (setq tmp (funcall getkey-func))
  6631. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6632. tmp)
  6633. (error "Invalid key function `%s'" getkey-func)))
  6634. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6635. nil
  6636. (cond
  6637. ((= dcst ?a) 'string<)
  6638. ((= dcst ?f) compare-func)
  6639. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6640. (t nil)))))
  6641. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6642. (message "Sorting entries...done")))
  6643. (defun org-do-sort (table what &optional with-case sorting-type)
  6644. "Sort TABLE of WHAT according to SORTING-TYPE.
  6645. The user will be prompted for the SORTING-TYPE if the call to this
  6646. function does not specify it. WHAT is only for the prompt, to indicate
  6647. what is being sorted. The sorting key will be extracted from
  6648. the car of the elements of the table.
  6649. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6650. (unless sorting-type
  6651. (message
  6652. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6653. what)
  6654. (setq sorting-type (read-char-exclusive)))
  6655. (let ((dcst (downcase sorting-type))
  6656. extractfun comparefun)
  6657. ;; Define the appropriate functions
  6658. (cond
  6659. ((= dcst ?n)
  6660. (setq extractfun 'string-to-number
  6661. comparefun (if (= dcst sorting-type) '< '>)))
  6662. ((= dcst ?a)
  6663. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6664. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6665. comparefun (if (= dcst sorting-type)
  6666. 'string<
  6667. (lambda (a b) (and (not (string< a b))
  6668. (not (string= a b)))))))
  6669. ((= dcst ?t)
  6670. (setq extractfun
  6671. (lambda (x)
  6672. (if (or (string-match org-ts-regexp x)
  6673. (string-match org-ts-regexp-both x))
  6674. (org-float-time
  6675. (org-time-string-to-time (match-string 0 x)))
  6676. 0))
  6677. comparefun (if (= dcst sorting-type) '< '>)))
  6678. (t (error "Invalid sorting type `%c'" sorting-type)))
  6679. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6680. table)
  6681. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6682. ;;; The orgstruct minor mode
  6683. ;; Define a minor mode which can be used in other modes in order to
  6684. ;; integrate the org-mode structure editing commands.
  6685. ;; This is really a hack, because the org-mode structure commands use
  6686. ;; keys which normally belong to the major mode. Here is how it
  6687. ;; works: The minor mode defines all the keys necessary to operate the
  6688. ;; structure commands, but wraps the commands into a function which
  6689. ;; tests if the cursor is currently at a headline or a plain list
  6690. ;; item. If that is the case, the structure command is used,
  6691. ;; temporarily setting many Org-mode variables like regular
  6692. ;; expressions for filling etc. However, when any of those keys is
  6693. ;; used at a different location, function uses `key-binding' to look
  6694. ;; up if the key has an associated command in another currently active
  6695. ;; keymap (minor modes, major mode, global), and executes that
  6696. ;; command. There might be problems if any of the keys is otherwise
  6697. ;; used as a prefix key.
  6698. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6699. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6700. ;; addresses this by checking explicitly for both bindings.
  6701. (defvar orgstruct-mode-map (make-sparse-keymap)
  6702. "Keymap for the minor `orgstruct-mode'.")
  6703. (defvar org-local-vars nil
  6704. "List of local variables, for use by `orgstruct-mode'")
  6705. ;;;###autoload
  6706. (define-minor-mode orgstruct-mode
  6707. "Toggle the minor mode `orgstruct-mode'.
  6708. This mode is for using Org-mode structure commands in other
  6709. modes. The following keys behave as if Org-mode were active, if
  6710. the cursor is on a headline, or on a plain list item (both as
  6711. defined by Org-mode).
  6712. M-up Move entry/item up
  6713. M-down Move entry/item down
  6714. M-left Promote
  6715. M-right Demote
  6716. M-S-up Move entry/item up
  6717. M-S-down Move entry/item down
  6718. M-S-left Promote subtree
  6719. M-S-right Demote subtree
  6720. M-q Fill paragraph and items like in Org-mode
  6721. C-c ^ Sort entries
  6722. C-c - Cycle list bullet
  6723. TAB Cycle item visibility
  6724. M-RET Insert new heading/item
  6725. S-M-RET Insert new TODO heading / Checkbox item
  6726. C-c C-c Set tags / toggle checkbox"
  6727. nil " OrgStruct" nil
  6728. (org-load-modules-maybe)
  6729. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6730. ;;;###autoload
  6731. (defun turn-on-orgstruct ()
  6732. "Unconditionally turn on `orgstruct-mode'."
  6733. (orgstruct-mode 1))
  6734. (defun orgstruct++-mode (&optional arg)
  6735. "Toggle `orgstruct-mode', the enhanced version of it.
  6736. In addition to setting orgstruct-mode, this also exports all indentation
  6737. and autofilling variables from org-mode into the buffer. It will also
  6738. recognize item context in multiline items.
  6739. Note that turning off orgstruct-mode will *not* remove the
  6740. indentation/paragraph settings. This can only be done by refreshing the
  6741. major mode, for example with \\[normal-mode]."
  6742. (interactive "P")
  6743. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6744. (if (< arg 1)
  6745. (orgstruct-mode -1)
  6746. (orgstruct-mode 1)
  6747. (let (var val)
  6748. (mapc
  6749. (lambda (x)
  6750. (when (string-match
  6751. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6752. (symbol-name (car x)))
  6753. (setq var (car x) val (nth 1 x))
  6754. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6755. org-local-vars)
  6756. (org-set-local 'orgstruct-is-++ t))))
  6757. (defvar orgstruct-is-++ nil
  6758. "Is orgstruct-mode in ++ version in the current-buffer?")
  6759. (make-variable-buffer-local 'orgstruct-is-++)
  6760. ;;;###autoload
  6761. (defun turn-on-orgstruct++ ()
  6762. "Unconditionally turn on `orgstruct++-mode'."
  6763. (orgstruct++-mode 1))
  6764. (defun orgstruct-error ()
  6765. "Error when there is no default binding for a structure key."
  6766. (interactive)
  6767. (error "This key has no function outside structure elements"))
  6768. (defun orgstruct-setup ()
  6769. "Setup orgstruct keymaps."
  6770. (let ((nfunc 0)
  6771. (bindings
  6772. (list
  6773. '([(meta up)] org-metaup)
  6774. '([(meta down)] org-metadown)
  6775. '([(meta left)] org-metaleft)
  6776. '([(meta right)] org-metaright)
  6777. '([(meta shift up)] org-shiftmetaup)
  6778. '([(meta shift down)] org-shiftmetadown)
  6779. '([(meta shift left)] org-shiftmetaleft)
  6780. '([(meta shift right)] org-shiftmetaright)
  6781. '([?\e (up)] org-metaup)
  6782. '([?\e (down)] org-metadown)
  6783. '([?\e (left)] org-metaleft)
  6784. '([?\e (right)] org-metaright)
  6785. '([?\e (shift up)] org-shiftmetaup)
  6786. '([?\e (shift down)] org-shiftmetadown)
  6787. '([?\e (shift left)] org-shiftmetaleft)
  6788. '([?\e (shift right)] org-shiftmetaright)
  6789. '([(shift up)] org-shiftup)
  6790. '([(shift down)] org-shiftdown)
  6791. '([(shift left)] org-shiftleft)
  6792. '([(shift right)] org-shiftright)
  6793. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6794. '("\M-q" fill-paragraph)
  6795. '("\C-c^" org-sort)
  6796. '("\C-c-" org-cycle-list-bullet)))
  6797. elt key fun cmd)
  6798. (while (setq elt (pop bindings))
  6799. (setq nfunc (1+ nfunc))
  6800. (setq key (org-key (car elt))
  6801. fun (nth 1 elt)
  6802. cmd (orgstruct-make-binding fun nfunc key))
  6803. (org-defkey orgstruct-mode-map key cmd))
  6804. ;; Special treatment needed for TAB and RET
  6805. (org-defkey orgstruct-mode-map [(tab)]
  6806. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6807. (org-defkey orgstruct-mode-map "\C-i"
  6808. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6809. (org-defkey orgstruct-mode-map "\M-\C-m"
  6810. (orgstruct-make-binding 'org-insert-heading 105
  6811. "\M-\C-m" [(meta return)]))
  6812. (org-defkey orgstruct-mode-map [(meta return)]
  6813. (orgstruct-make-binding 'org-insert-heading 106
  6814. [(meta return)] "\M-\C-m"))
  6815. (org-defkey orgstruct-mode-map [(shift meta return)]
  6816. (orgstruct-make-binding 'org-insert-todo-heading 107
  6817. [(meta return)] "\M-\C-m"))
  6818. (org-defkey orgstruct-mode-map "\e\C-m"
  6819. (orgstruct-make-binding 'org-insert-heading 108
  6820. "\e\C-m" [?\e (return)]))
  6821. (org-defkey orgstruct-mode-map [?\e (return)]
  6822. (orgstruct-make-binding 'org-insert-heading 109
  6823. [?\e (return)] "\e\C-m"))
  6824. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6825. (orgstruct-make-binding 'org-insert-todo-heading 110
  6826. [?\e (return)] "\e\C-m"))
  6827. (unless org-local-vars
  6828. (setq org-local-vars (org-get-local-variables)))
  6829. t))
  6830. (defun orgstruct-make-binding (fun n &rest keys)
  6831. "Create a function for binding in the structure minor mode.
  6832. FUN is the command to call inside a table. N is used to create a unique
  6833. command name. KEYS are keys that should be checked in for a command
  6834. to execute outside of tables."
  6835. (eval
  6836. (list 'defun
  6837. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6838. '(arg)
  6839. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6840. "Outside of structure, run the binding of `"
  6841. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6842. "'.")
  6843. '(interactive "p")
  6844. (list 'if
  6845. `(org-context-p 'headline 'item
  6846. (and orgstruct-is-++
  6847. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6848. 'item-body))
  6849. (list 'org-run-like-in-org-mode (list 'quote fun))
  6850. (list 'let '(orgstruct-mode)
  6851. (list 'call-interactively
  6852. (append '(or)
  6853. (mapcar (lambda (k)
  6854. (list 'key-binding k))
  6855. keys)
  6856. '('orgstruct-error))))))))
  6857. (defun org-context-p (&rest contexts)
  6858. "Check if local context is any of CONTEXTS.
  6859. Possible values in the list of contexts are `table', `headline', and `item'."
  6860. (let ((pos (point)))
  6861. (goto-char (point-at-bol))
  6862. (prog1 (or (and (memq 'table contexts)
  6863. (looking-at "[ \t]*|"))
  6864. (and (memq 'headline contexts)
  6865. ;;????????? (looking-at "\\*+"))
  6866. (looking-at outline-regexp))
  6867. (and (memq 'item contexts)
  6868. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6869. (and (memq 'item-body contexts)
  6870. (org-in-item-p)))
  6871. (goto-char pos))))
  6872. (defun org-get-local-variables ()
  6873. "Return a list of all local variables in an org-mode buffer."
  6874. (let (varlist)
  6875. (with-current-buffer (get-buffer-create "*Org tmp*")
  6876. (erase-buffer)
  6877. (org-mode)
  6878. (setq varlist (buffer-local-variables)))
  6879. (kill-buffer "*Org tmp*")
  6880. (delq nil
  6881. (mapcar
  6882. (lambda (x)
  6883. (setq x
  6884. (if (symbolp x)
  6885. (list x)
  6886. (list (car x) (list 'quote (cdr x)))))
  6887. (if (string-match
  6888. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6889. (symbol-name (car x)))
  6890. x nil))
  6891. varlist))))
  6892. ;;;###autoload
  6893. (defun org-run-like-in-org-mode (cmd)
  6894. "Run a command, pretending that the current buffer is in Org-mode.
  6895. This will temporarily bind local variables that are typically bound in
  6896. Org-mode to the values they have in Org-mode, and then interactively
  6897. call CMD."
  6898. (org-load-modules-maybe)
  6899. (unless org-local-vars
  6900. (setq org-local-vars (org-get-local-variables)))
  6901. (eval (list 'let org-local-vars
  6902. (list 'call-interactively (list 'quote cmd)))))
  6903. ;;;; Archiving
  6904. (defun org-get-category (&optional pos)
  6905. "Get the category applying to position POS."
  6906. (get-text-property (or pos (point)) 'org-category))
  6907. (defun org-refresh-category-properties ()
  6908. "Refresh category text properties in the buffer."
  6909. (let ((def-cat (cond
  6910. ((null org-category)
  6911. (if buffer-file-name
  6912. (file-name-sans-extension
  6913. (file-name-nondirectory buffer-file-name))
  6914. "???"))
  6915. ((symbolp org-category) (symbol-name org-category))
  6916. (t org-category)))
  6917. beg end cat pos optionp)
  6918. (org-unmodified
  6919. (save-excursion
  6920. (save-restriction
  6921. (widen)
  6922. (goto-char (point-min))
  6923. (put-text-property (point) (point-max) 'org-category def-cat)
  6924. (while (re-search-forward
  6925. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6926. (setq pos (match-end 0)
  6927. optionp (equal (char-after (match-beginning 0)) ?#)
  6928. cat (org-trim (match-string 2)))
  6929. (if optionp
  6930. (setq beg (point-at-bol) end (point-max))
  6931. (org-back-to-heading t)
  6932. (setq beg (point) end (org-end-of-subtree t t)))
  6933. (put-text-property beg end 'org-category cat)
  6934. (goto-char pos)))))))
  6935. ;;;; Link Stuff
  6936. ;;; Link abbreviations
  6937. (defun org-link-expand-abbrev (link)
  6938. "Apply replacements as defined in `org-link-abbrev-alist."
  6939. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6940. (let* ((key (match-string 1 link))
  6941. (as (or (assoc key org-link-abbrev-alist-local)
  6942. (assoc key org-link-abbrev-alist)))
  6943. (tag (and (match-end 2) (match-string 3 link)))
  6944. rpl)
  6945. (if (not as)
  6946. link
  6947. (setq rpl (cdr as))
  6948. (cond
  6949. ((symbolp rpl) (funcall rpl tag))
  6950. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6951. ((string-match "%h" rpl)
  6952. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6953. (t (concat rpl tag)))))
  6954. link))
  6955. ;;; Storing and inserting links
  6956. (defvar org-insert-link-history nil
  6957. "Minibuffer history for links inserted with `org-insert-link'.")
  6958. (defvar org-stored-links nil
  6959. "Contains the links stored with `org-store-link'.")
  6960. (defvar org-store-link-plist nil
  6961. "Plist with info about the most recently link created with `org-store-link'.")
  6962. (defvar org-link-protocols nil
  6963. "Link protocols added to Org-mode using `org-add-link-type'.")
  6964. (defvar org-store-link-functions nil
  6965. "List of functions that are called to create and store a link.
  6966. Each function will be called in turn until one returns a non-nil
  6967. value. Each function should check if it is responsible for creating
  6968. this link (for example by looking at the major mode).
  6969. If not, it must exit and return nil.
  6970. If yes, it should return a non-nil value after a calling
  6971. `org-store-link-props' with a list of properties and values.
  6972. Special properties are:
  6973. :type The link prefix. like \"http\". This must be given.
  6974. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6975. This is obligatory as well.
  6976. :description Optional default description for the second pair
  6977. of brackets in an Org-mode link. The user can still change
  6978. this when inserting this link into an Org-mode buffer.
  6979. In addition to these, any additional properties can be specified
  6980. and then used in remember templates.")
  6981. (defun org-add-link-type (type &optional follow export)
  6982. "Add TYPE to the list of `org-link-types'.
  6983. Re-compute all regular expressions depending on `org-link-types'
  6984. FOLLOW and EXPORT are two functions.
  6985. FOLLOW should take the link path as the single argument and do whatever
  6986. is necessary to follow the link, for example find a file or display
  6987. a mail message.
  6988. EXPORT should format the link path for export to one of the export formats.
  6989. It should be a function accepting three arguments:
  6990. path the path of the link, the text after the prefix (like \"http:\")
  6991. desc the description of the link, if any, nil if there was no description
  6992. format the export format, a symbol like `html' or `latex'.
  6993. The function may use the FORMAT information to return different values
  6994. depending on the format. The return value will be put literally into
  6995. the exported file.
  6996. Org-mode has a built-in default for exporting links. If you are happy with
  6997. this default, there is no need to define an export function for the link
  6998. type. For a simple example of an export function, see `org-bbdb.el'."
  6999. (add-to-list 'org-link-types type t)
  7000. (org-make-link-regexps)
  7001. (if (assoc type org-link-protocols)
  7002. (setcdr (assoc type org-link-protocols) (list follow export))
  7003. (push (list type follow export) org-link-protocols)))
  7004. (defvar org-agenda-buffer-name)
  7005. ;;;###autoload
  7006. (defun org-store-link (arg)
  7007. "\\<org-mode-map>Store an org-link to the current location.
  7008. This link is added to `org-stored-links' and can later be inserted
  7009. into an org-buffer with \\[org-insert-link].
  7010. For some link types, a prefix arg is interpreted:
  7011. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7012. For file links, arg negates `org-context-in-file-links'."
  7013. (interactive "P")
  7014. (org-load-modules-maybe)
  7015. (setq org-store-link-plist nil) ; reset
  7016. (let ((outline-regexp (org-get-limited-outline-regexp))
  7017. link cpltxt desc description search txt custom-id)
  7018. (cond
  7019. ((run-hook-with-args-until-success 'org-store-link-functions)
  7020. (setq link (plist-get org-store-link-plist :link)
  7021. desc (or (plist-get org-store-link-plist :description) link)))
  7022. ((equal (buffer-name) "*Org Edit Src Example*")
  7023. (let (label gc)
  7024. (while (or (not label)
  7025. (save-excursion
  7026. (save-restriction
  7027. (widen)
  7028. (goto-char (point-min))
  7029. (re-search-forward
  7030. (regexp-quote (format org-coderef-label-format label))
  7031. nil t))))
  7032. (when label (message "Label exists already") (sit-for 2))
  7033. (setq label (read-string "Code line label: " label)))
  7034. (end-of-line 1)
  7035. (setq link (format org-coderef-label-format label))
  7036. (setq gc (- 79 (length link)))
  7037. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7038. (insert link)
  7039. (setq link (concat "(" label ")") desc nil)))
  7040. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7041. ;; We are in the agenda, link to referenced location
  7042. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7043. (get-text-property (point) 'org-marker))))
  7044. (when m
  7045. (org-with-point-at m
  7046. (call-interactively 'org-store-link)))))
  7047. ((eq major-mode 'calendar-mode)
  7048. (let ((cd (calendar-cursor-to-date)))
  7049. (setq link
  7050. (format-time-string
  7051. (car org-time-stamp-formats)
  7052. (apply 'encode-time
  7053. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7054. nil nil nil))))
  7055. (org-store-link-props :type "calendar" :date cd)))
  7056. ((eq major-mode 'w3-mode)
  7057. (setq cpltxt (if (and (buffer-name)
  7058. (not (string-match "Untitled" (buffer-name))))
  7059. (buffer-name)
  7060. (url-view-url t))
  7061. link (org-make-link (url-view-url t)))
  7062. (org-store-link-props :type "w3" :url (url-view-url t)))
  7063. ((eq major-mode 'w3m-mode)
  7064. (setq cpltxt (or w3m-current-title w3m-current-url)
  7065. link (org-make-link w3m-current-url))
  7066. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7067. ((setq search (run-hook-with-args-until-success
  7068. 'org-create-file-search-functions))
  7069. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7070. "::" search))
  7071. (setq cpltxt (or description link)))
  7072. ((eq major-mode 'image-mode)
  7073. (setq cpltxt (concat "file:"
  7074. (abbreviate-file-name buffer-file-name))
  7075. link (org-make-link cpltxt))
  7076. (org-store-link-props :type "image" :file buffer-file-name))
  7077. ((eq major-mode 'dired-mode)
  7078. ;; link to the file in the current line
  7079. (let ((file (dired-get-filename nil t)))
  7080. (setq file (if file
  7081. (abbreviate-file-name
  7082. (expand-file-name (dired-get-filename nil t)))
  7083. ;; otherwise, no file so use current directory.
  7084. default-directory))
  7085. (setq cpltxt (concat "file:" file)
  7086. link (org-make-link cpltxt))))
  7087. ((and buffer-file-name (org-mode-p))
  7088. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  7089. (cond
  7090. ((org-in-regexp "<<\\(.*?\\)>>")
  7091. (setq cpltxt
  7092. (concat "file:"
  7093. (abbreviate-file-name buffer-file-name)
  7094. "::" (match-string 1))
  7095. link (org-make-link cpltxt)))
  7096. ((and (featurep 'org-id)
  7097. (or (eq org-link-to-org-use-id t)
  7098. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7099. (interactive-p))
  7100. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  7101. (interactive-p)
  7102. (not custom-id))
  7103. (and org-link-to-org-use-id
  7104. (condition-case nil
  7105. (org-entry-get nil "ID")
  7106. (error nil)))))
  7107. ;; We can make a link using the ID.
  7108. (setq link (condition-case nil
  7109. (prog1 (org-id-store-link)
  7110. (setq desc (plist-get org-store-link-plist
  7111. :description)))
  7112. (error
  7113. ;; probably before first headline, link to file only
  7114. (concat "file:"
  7115. (abbreviate-file-name buffer-file-name))))))
  7116. (t
  7117. ;; Just link to current headline
  7118. (setq cpltxt (concat "file:"
  7119. (abbreviate-file-name buffer-file-name)))
  7120. ;; Add a context search string
  7121. (when (org-xor org-context-in-file-links arg)
  7122. (setq txt (cond
  7123. ((org-on-heading-p) nil)
  7124. ((org-region-active-p)
  7125. (buffer-substring (region-beginning) (region-end)))
  7126. (t nil)))
  7127. (when (or (null txt) (string-match "\\S-" txt))
  7128. (setq cpltxt
  7129. (concat cpltxt "::"
  7130. (condition-case nil
  7131. (org-make-org-heading-search-string txt)
  7132. (error "")))
  7133. desc (or (nth 4 (ignore-errors
  7134. (org-heading-components))) "NONE"))))
  7135. (if (string-match "::\\'" cpltxt)
  7136. (setq cpltxt (substring cpltxt 0 -2)))
  7137. (setq link (org-make-link cpltxt)))))
  7138. ((buffer-file-name (buffer-base-buffer))
  7139. ;; Just link to this file here.
  7140. (setq cpltxt (concat "file:"
  7141. (abbreviate-file-name
  7142. (buffer-file-name (buffer-base-buffer)))))
  7143. ;; Add a context string
  7144. (when (org-xor org-context-in-file-links arg)
  7145. (setq txt (if (org-region-active-p)
  7146. (buffer-substring (region-beginning) (region-end))
  7147. (buffer-substring (point-at-bol) (point-at-eol))))
  7148. ;; Only use search option if there is some text.
  7149. (when (string-match "\\S-" txt)
  7150. (setq cpltxt
  7151. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7152. desc "NONE")))
  7153. (setq link (org-make-link cpltxt)))
  7154. ((interactive-p)
  7155. (error "Cannot link to a buffer which is not visiting a file"))
  7156. (t (setq link nil)))
  7157. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7158. (setq link (or link cpltxt)
  7159. desc (or desc cpltxt))
  7160. (if (equal desc "NONE") (setq desc nil))
  7161. (if (and (or (interactive-p) executing-kbd-macro) link)
  7162. (progn
  7163. (setq org-stored-links
  7164. (cons (list link desc) org-stored-links))
  7165. (message "Stored: %s" (or desc link))
  7166. (when custom-id
  7167. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7168. "::#" custom-id))
  7169. (setq org-stored-links
  7170. (cons (list link desc) org-stored-links))))
  7171. (and link (org-make-link-string link desc)))))
  7172. (defun org-store-link-props (&rest plist)
  7173. "Store link properties, extract names and addresses."
  7174. (let (x adr)
  7175. (when (setq x (plist-get plist :from))
  7176. (setq adr (mail-extract-address-components x))
  7177. (setq plist (plist-put plist :fromname (car adr)))
  7178. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7179. (when (setq x (plist-get plist :to))
  7180. (setq adr (mail-extract-address-components x))
  7181. (setq plist (plist-put plist :toname (car adr)))
  7182. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7183. (let ((from (plist-get plist :from))
  7184. (to (plist-get plist :to)))
  7185. (when (and from to org-from-is-user-regexp)
  7186. (setq plist
  7187. (plist-put plist :fromto
  7188. (if (string-match org-from-is-user-regexp from)
  7189. (concat "to %t")
  7190. (concat "from %f"))))))
  7191. (setq org-store-link-plist plist))
  7192. (defun org-add-link-props (&rest plist)
  7193. "Add these properties to the link property list."
  7194. (let (key value)
  7195. (while plist
  7196. (setq key (pop plist) value (pop plist))
  7197. (setq org-store-link-plist
  7198. (plist-put org-store-link-plist key value)))))
  7199. (defun org-email-link-description (&optional fmt)
  7200. "Return the description part of an email link.
  7201. This takes information from `org-store-link-plist' and formats it
  7202. according to FMT (default from `org-email-link-description-format')."
  7203. (setq fmt (or fmt org-email-link-description-format))
  7204. (let* ((p org-store-link-plist)
  7205. (to (plist-get p :toaddress))
  7206. (from (plist-get p :fromaddress))
  7207. (table
  7208. (list
  7209. (cons "%c" (plist-get p :fromto))
  7210. (cons "%F" (plist-get p :from))
  7211. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7212. (cons "%T" (plist-get p :to))
  7213. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7214. (cons "%s" (plist-get p :subject))
  7215. (cons "%m" (plist-get p :message-id)))))
  7216. (when (string-match "%c" fmt)
  7217. ;; Check if the user wrote this message
  7218. (if (and org-from-is-user-regexp from to
  7219. (save-match-data (string-match org-from-is-user-regexp from)))
  7220. (setq fmt (replace-match "to %t" t t fmt))
  7221. (setq fmt (replace-match "from %f" t t fmt))))
  7222. (org-replace-escapes fmt table)))
  7223. (defun org-make-org-heading-search-string (&optional string heading)
  7224. "Make search string for STRING or current headline."
  7225. (interactive)
  7226. (let ((s (or string (org-get-heading))))
  7227. (unless (and string (not heading))
  7228. ;; We are using a headline, clean up garbage in there.
  7229. (if (string-match org-todo-regexp s)
  7230. (setq s (replace-match "" t t s)))
  7231. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  7232. (setq s (replace-match "" t t s)))
  7233. (setq s (org-trim s))
  7234. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7235. org-comment-string "\\)") s)
  7236. (setq s (replace-match "" t t s)))
  7237. (while (string-match org-ts-regexp s)
  7238. (setq s (replace-match "" t t s))))
  7239. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  7240. (setq s (replace-match " " t t s)))
  7241. (or string (setq s (concat "*" s))) ; Add * for headlines
  7242. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7243. (defun org-make-link (&rest strings)
  7244. "Concatenate STRINGS."
  7245. (apply 'concat strings))
  7246. (defun org-make-link-string (link &optional description)
  7247. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7248. (unless (string-match "\\S-" link)
  7249. (error "Empty link"))
  7250. (when (and description
  7251. (stringp description)
  7252. (not (string-match "\\S-" description)))
  7253. (setq description nil))
  7254. (when (stringp description)
  7255. ;; Remove brackets from the description, they are fatal.
  7256. (while (string-match "\\[" description)
  7257. (setq description (replace-match "{" t t description)))
  7258. (while (string-match "\\]" description)
  7259. (setq description (replace-match "}" t t description))))
  7260. (when (equal (org-link-escape link) description)
  7261. ;; No description needed, it is identical
  7262. (setq description nil))
  7263. (when (and (not description)
  7264. (not (equal link (org-link-escape link))))
  7265. (setq description (org-extract-attributes link)))
  7266. (concat "[[" (org-link-escape link) "]"
  7267. (if description (concat "[" description "]") "")
  7268. "]"))
  7269. (defconst org-link-escape-chars
  7270. '((?\ . "%20")
  7271. (?\[ . "%5B")
  7272. (?\] . "%5D")
  7273. (?\340 . "%E0") ; `a
  7274. (?\342 . "%E2") ; ^a
  7275. (?\347 . "%E7") ; ,c
  7276. (?\350 . "%E8") ; `e
  7277. (?\351 . "%E9") ; 'e
  7278. (?\352 . "%EA") ; ^e
  7279. (?\356 . "%EE") ; ^i
  7280. (?\364 . "%F4") ; ^o
  7281. (?\371 . "%F9") ; `u
  7282. (?\373 . "%FB") ; ^u
  7283. (?\; . "%3B")
  7284. ;; (?? . "%3F")
  7285. (?= . "%3D")
  7286. (?+ . "%2B")
  7287. )
  7288. "Association list of escapes for some characters problematic in links.
  7289. This is the list that is used for internal purposes.")
  7290. (defvar org-url-encoding-use-url-hexify nil)
  7291. (defconst org-link-escape-chars-browser
  7292. '((?\ . "%20")) ; 32 for the SPC char
  7293. "Association list of escapes for some characters problematic in links.
  7294. This is the list that is used before handing over to the browser.")
  7295. (defun org-link-escape (text &optional table)
  7296. "Escape characters in TEXT that are problematic for links."
  7297. (if (and org-url-encoding-use-url-hexify (not table))
  7298. (url-hexify-string text)
  7299. (setq table (or table org-link-escape-chars))
  7300. (when text
  7301. (let ((re (mapconcat (lambda (x) (regexp-quote
  7302. (char-to-string (car x))))
  7303. table "\\|")))
  7304. (while (string-match re text)
  7305. (setq text
  7306. (replace-match
  7307. (cdr (assoc (string-to-char (match-string 0 text))
  7308. table))
  7309. t t text)))
  7310. text))))
  7311. (defun org-link-unescape (text &optional table)
  7312. "Reverse the action of `org-link-escape'."
  7313. (if (and org-url-encoding-use-url-hexify (not table))
  7314. (url-unhex-string text)
  7315. (setq table (or table org-link-escape-chars))
  7316. (when text
  7317. (let ((case-fold-search t)
  7318. (re (mapconcat (lambda (x) (regexp-quote (downcase (cdr x))))
  7319. table "\\|")))
  7320. (while (string-match re text)
  7321. (setq text
  7322. (replace-match
  7323. (char-to-string (car (rassoc (upcase (match-string 0 text))
  7324. table)))
  7325. t t text)))
  7326. text))))
  7327. (defun org-xor (a b)
  7328. "Exclusive or."
  7329. (if a (not b) b))
  7330. (defun org-fixup-message-id-for-http (s)
  7331. "Replace special characters in a message id, so it can be used in an http query."
  7332. (while (string-match "<" s)
  7333. (setq s (replace-match "%3C" t t s)))
  7334. (while (string-match ">" s)
  7335. (setq s (replace-match "%3E" t t s)))
  7336. (while (string-match "@" s)
  7337. (setq s (replace-match "%40" t t s)))
  7338. s)
  7339. ;;;###autoload
  7340. (defun org-insert-link-global ()
  7341. "Insert a link like Org-mode does.
  7342. This command can be called in any mode to insert a link in Org-mode syntax."
  7343. (interactive)
  7344. (org-load-modules-maybe)
  7345. (org-run-like-in-org-mode 'org-insert-link))
  7346. (defun org-insert-link (&optional complete-file link-location)
  7347. "Insert a link. At the prompt, enter the link.
  7348. Completion can be used to insert any of the link protocol prefixes like
  7349. http or ftp in use.
  7350. The history can be used to select a link previously stored with
  7351. `org-store-link'. When the empty string is entered (i.e. if you just
  7352. press RET at the prompt), the link defaults to the most recently
  7353. stored link. As SPC triggers completion in the minibuffer, you need to
  7354. use M-SPC or C-q SPC to force the insertion of a space character.
  7355. You will also be prompted for a description, and if one is given, it will
  7356. be displayed in the buffer instead of the link.
  7357. If there is already a link at point, this command will allow you to edit link
  7358. and description parts.
  7359. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7360. be selected using completion. The path to the file will be relative to the
  7361. current directory if the file is in the current directory or a subdirectory.
  7362. Otherwise, the link will be the absolute path as completed in the minibuffer
  7363. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7364. option `org-link-file-path-type'.
  7365. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7366. the current directory or below.
  7367. With three \\[universal-argument] prefixes, negate the meaning of
  7368. `org-keep-stored-link-after-insertion'.
  7369. If `org-make-link-description-function' is non-nil, this function will be
  7370. called with the link target, and the result will be the default
  7371. link description.
  7372. If the LINK-LOCATION parameter is non-nil, this value will be
  7373. used as the link location instead of reading one interactively."
  7374. (interactive "P")
  7375. (let* ((wcf (current-window-configuration))
  7376. (region (if (org-region-active-p)
  7377. (buffer-substring (region-beginning) (region-end))))
  7378. (remove (and region (list (region-beginning) (region-end))))
  7379. (desc region)
  7380. tmphist ; byte-compile incorrectly complains about this
  7381. (link link-location)
  7382. entry file all-prefixes)
  7383. (cond
  7384. (link-location) ; specified by arg, just use it.
  7385. ((org-in-regexp org-bracket-link-regexp 1)
  7386. ;; We do have a link at point, and we are going to edit it.
  7387. (setq remove (list (match-beginning 0) (match-end 0)))
  7388. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7389. (setq link (read-string "Link: "
  7390. (org-link-unescape
  7391. (org-match-string-no-properties 1)))))
  7392. ((or (org-in-regexp org-angle-link-re)
  7393. (org-in-regexp org-plain-link-re))
  7394. ;; Convert to bracket link
  7395. (setq remove (list (match-beginning 0) (match-end 0))
  7396. link (read-string "Link: "
  7397. (org-remove-angle-brackets (match-string 0)))))
  7398. ((member complete-file '((4) (16)))
  7399. ;; Completing read for file names.
  7400. (setq link (org-file-complete-link complete-file)))
  7401. (t
  7402. ;; Read link, with completion for stored links.
  7403. (with-output-to-temp-buffer "*Org Links*"
  7404. (princ "Insert a link.
  7405. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7406. (when org-stored-links
  7407. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7408. (princ (mapconcat
  7409. (lambda (x)
  7410. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7411. (reverse org-stored-links) "\n"))))
  7412. (let ((cw (selected-window)))
  7413. (select-window (get-buffer-window "*Org Links*" 'visible))
  7414. (setq truncate-lines t)
  7415. (unless (pos-visible-in-window-p (point-max))
  7416. (org-fit-window-to-buffer))
  7417. (and (window-live-p cw) (select-window cw)))
  7418. ;; Fake a link history, containing the stored links.
  7419. (setq tmphist (append (mapcar 'car org-stored-links)
  7420. org-insert-link-history))
  7421. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7422. (mapcar 'car org-link-abbrev-alist)
  7423. org-link-types))
  7424. (unwind-protect
  7425. (progn
  7426. (setq link
  7427. (let ((org-completion-use-ido nil)
  7428. (org-completion-use-iswitchb nil))
  7429. (org-completing-read
  7430. "Link: "
  7431. (append
  7432. (mapcar (lambda (x) (list (concat x ":")))
  7433. all-prefixes)
  7434. (mapcar 'car org-stored-links))
  7435. nil nil nil
  7436. 'tmphist
  7437. (car (car org-stored-links)))))
  7438. (if (not (string-match "\\S-" link))
  7439. (error "No link selected"))
  7440. (if (or (member link all-prefixes)
  7441. (and (equal ":" (substring link -1))
  7442. (member (substring link 0 -1) all-prefixes)
  7443. (setq link (substring link 0 -1))))
  7444. (setq link (org-link-try-special-completion link))))
  7445. (set-window-configuration wcf)
  7446. (kill-buffer "*Org Links*"))
  7447. (setq entry (assoc link org-stored-links))
  7448. (or entry (push link org-insert-link-history))
  7449. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7450. (not org-keep-stored-link-after-insertion))
  7451. (setq org-stored-links (delq (assoc link org-stored-links)
  7452. org-stored-links)))
  7453. (setq desc (or desc (nth 1 entry)))))
  7454. (if (string-match org-plain-link-re link)
  7455. ;; URL-like link, normalize the use of angular brackets.
  7456. (setq link (org-make-link (org-remove-angle-brackets link))))
  7457. ;; Check if we are linking to the current file with a search option
  7458. ;; If yes, simplify the link by using only the search option.
  7459. (when (and buffer-file-name
  7460. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7461. (let* ((path (match-string 1 link))
  7462. (case-fold-search nil)
  7463. (search (match-string 2 link)))
  7464. (save-match-data
  7465. (if (equal (file-truename buffer-file-name) (file-truename path))
  7466. ;; We are linking to this same file, with a search option
  7467. (setq link search)))))
  7468. ;; Check if we can/should use a relative path. If yes, simplify the link
  7469. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  7470. (let* ((type (match-string 1 link))
  7471. (path (match-string 2 link))
  7472. (origpath path)
  7473. (case-fold-search nil))
  7474. (cond
  7475. ((or (eq org-link-file-path-type 'absolute)
  7476. (equal complete-file '(16)))
  7477. (setq path (abbreviate-file-name (expand-file-name path))))
  7478. ((eq org-link-file-path-type 'noabbrev)
  7479. (setq path (expand-file-name path)))
  7480. ((eq org-link-file-path-type 'relative)
  7481. (setq path (file-relative-name path)))
  7482. (t
  7483. (save-match-data
  7484. (if (string-match (concat "^" (regexp-quote
  7485. (file-name-as-directory
  7486. (expand-file-name "."))))
  7487. (expand-file-name path))
  7488. ;; We are linking a file with relative path name.
  7489. (setq path (substring (expand-file-name path)
  7490. (match-end 0)))
  7491. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7492. (setq link (concat type path))
  7493. (if (equal desc origpath)
  7494. (setq desc path))))
  7495. (if org-make-link-description-function
  7496. (setq desc (funcall org-make-link-description-function link desc)))
  7497. (setq desc (read-string "Description: " desc))
  7498. (unless (string-match "\\S-" desc) (setq desc nil))
  7499. (if remove (apply 'delete-region remove))
  7500. (insert (org-make-link-string link desc))))
  7501. (defun org-link-try-special-completion (type)
  7502. "If there is completion support for link type TYPE, offer it."
  7503. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7504. (if (functionp fun)
  7505. (funcall fun)
  7506. (read-string "Link (no completion support): " (concat type ":")))))
  7507. (defun org-file-complete-link (&optional arg)
  7508. "Create a file link using completion."
  7509. (let (file link)
  7510. (setq file (read-file-name "File: "))
  7511. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7512. (pwd1 (file-name-as-directory (abbreviate-file-name
  7513. (expand-file-name ".")))))
  7514. (cond
  7515. ((equal arg '(16))
  7516. (setq link (org-make-link
  7517. "file:"
  7518. (abbreviate-file-name (expand-file-name file)))))
  7519. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7520. (setq link (org-make-link "file:" (match-string 1 file))))
  7521. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7522. (expand-file-name file))
  7523. (setq link (org-make-link
  7524. "file:" (match-string 1 (expand-file-name file)))))
  7525. (t (setq link (org-make-link "file:" file)))))
  7526. link))
  7527. (defun org-completing-read (&rest args)
  7528. "Completing-read with SPACE being a normal character."
  7529. (let ((minibuffer-local-completion-map
  7530. (copy-keymap minibuffer-local-completion-map)))
  7531. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7532. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7533. (apply 'org-icompleting-read args)))
  7534. (defun org-completing-read-no-i (&rest args)
  7535. (let (org-completion-use-ido org-completion-use-iswitchb)
  7536. (apply 'org-completing-read args)))
  7537. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7538. "Use iswitch as a completing-read replacement to choose from choices.
  7539. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7540. from."
  7541. (let* ((iswitchb-use-virtual-buffers nil)
  7542. (iswitchb-make-buflist-hook
  7543. (lambda ()
  7544. (setq iswitchb-temp-buflist choices))))
  7545. (iswitchb-read-buffer prompt)))
  7546. (defun org-icompleting-read (&rest args)
  7547. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7548. (org-without-partial-completion
  7549. (if (and org-completion-use-ido
  7550. (fboundp 'ido-completing-read)
  7551. (boundp 'ido-mode) ido-mode
  7552. (listp (second args)))
  7553. (let ((ido-enter-matching-directory nil))
  7554. (apply 'ido-completing-read (concat (car args))
  7555. (if (consp (car (nth 1 args)))
  7556. (mapcar (lambda (x) (car x)) (nth 1 args))
  7557. (nth 1 args))
  7558. (cddr args)))
  7559. (if (and org-completion-use-iswitchb
  7560. (boundp 'iswitchb-mode) iswitchb-mode
  7561. (listp (second args)))
  7562. (apply 'org-iswitchb-completing-read (concat (car args))
  7563. (if (consp (car (nth 1 args)))
  7564. (mapcar (lambda (x) (car x)) (nth 1 args))
  7565. (nth 1 args))
  7566. (cddr args))
  7567. (apply 'completing-read args)))))
  7568. (defun org-extract-attributes (s)
  7569. "Extract the attributes cookie from a string and set as text property."
  7570. (let (a attr (start 0) key value)
  7571. (save-match-data
  7572. (when (string-match "{{\\([^}]+\\)}}$" s)
  7573. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7574. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7575. (setq key (match-string 1 a) value (match-string 2 a)
  7576. start (match-end 0)
  7577. attr (plist-put attr (intern key) value))))
  7578. (org-add-props s nil 'org-attr attr))
  7579. s))
  7580. (defun org-extract-attributes-from-string (tag)
  7581. (let (key value attr)
  7582. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7583. (setq key (match-string 1 tag) value (match-string 2 tag)
  7584. tag (replace-match "" t t tag)
  7585. attr (plist-put attr (intern key) value)))
  7586. (cons tag attr)))
  7587. (defun org-attributes-to-string (plist)
  7588. "Format a property list into an HTML attribute list."
  7589. (let ((s "") key value)
  7590. (while plist
  7591. (setq key (pop plist) value (pop plist))
  7592. (and value
  7593. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7594. s))
  7595. ;;; Opening/following a link
  7596. (defvar org-link-search-failed nil)
  7597. (defvar org-open-link-functions nil
  7598. "Hook for functions finding a plain text link.
  7599. These functions must take a single argument, the link content.
  7600. They will be called for links that look like [[link text][description]]
  7601. when LINK TEXT does not have a protocol like \"http:\" and does not look
  7602. like a filename (e.g. \"./blue.png\").
  7603. These functions will be called *before* Org attempts to resolve the
  7604. link by doing text searches in the current buffer - so if you want a
  7605. link \"[[target]]\" to still find \"<<target>>\", your function should
  7606. handle this as a special case.
  7607. When the function does handle the link, it must return a non-nil value.
  7608. If it decides that it is not responsible for this link, it must return
  7609. nil to indicate that that Org-mode can continue with other options
  7610. like exact and fuzzy text search.")
  7611. (defun org-next-link ()
  7612. "Move forward to the next link.
  7613. If the link is in hidden text, expose it."
  7614. (interactive)
  7615. (when (and org-link-search-failed (eq this-command last-command))
  7616. (goto-char (point-min))
  7617. (message "Link search wrapped back to beginning of buffer"))
  7618. (setq org-link-search-failed nil)
  7619. (let* ((pos (point))
  7620. (ct (org-context))
  7621. (a (assoc :link ct)))
  7622. (if a (goto-char (nth 2 a)))
  7623. (if (re-search-forward org-any-link-re nil t)
  7624. (progn
  7625. (goto-char (match-beginning 0))
  7626. (if (org-invisible-p) (org-show-context)))
  7627. (goto-char pos)
  7628. (setq org-link-search-failed t)
  7629. (error "No further link found"))))
  7630. (defun org-previous-link ()
  7631. "Move backward to the previous link.
  7632. If the link is in hidden text, expose it."
  7633. (interactive)
  7634. (when (and org-link-search-failed (eq this-command last-command))
  7635. (goto-char (point-max))
  7636. (message "Link search wrapped back to end of buffer"))
  7637. (setq org-link-search-failed nil)
  7638. (let* ((pos (point))
  7639. (ct (org-context))
  7640. (a (assoc :link ct)))
  7641. (if a (goto-char (nth 1 a)))
  7642. (if (re-search-backward org-any-link-re nil t)
  7643. (progn
  7644. (goto-char (match-beginning 0))
  7645. (if (org-invisible-p) (org-show-context)))
  7646. (goto-char pos)
  7647. (setq org-link-search-failed t)
  7648. (error "No further link found"))))
  7649. (defun org-translate-link (s)
  7650. "Translate a link string if a translation function has been defined."
  7651. (if (and org-link-translation-function
  7652. (fboundp org-link-translation-function)
  7653. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7654. (progn
  7655. (setq s (funcall org-link-translation-function
  7656. (match-string 1) (match-string 2)))
  7657. (concat (car s) ":" (cdr s)))
  7658. s))
  7659. (defun org-translate-link-from-planner (type path)
  7660. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7661. This is still an experimental function, your mileage may vary."
  7662. (cond
  7663. ((member type '("http" "https" "news" "ftp"))
  7664. ;; standard Internet links are the same.
  7665. nil)
  7666. ((and (equal type "irc") (string-match "^//" path))
  7667. ;; Planner has two / at the beginning of an irc link, we have 1.
  7668. ;; We should have zero, actually....
  7669. (setq path (substring path 1)))
  7670. ((and (equal type "lisp") (string-match "^/" path))
  7671. ;; Planner has a slash, we do not.
  7672. (setq type "elisp" path (substring path 1)))
  7673. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7674. ;; A typical message link. Planner has the id after the final slash,
  7675. ;; we separate it with a hash mark
  7676. (setq path (concat (match-string 1 path) "#"
  7677. (org-remove-angle-brackets (match-string 2 path)))))
  7678. )
  7679. (cons type path))
  7680. (defun org-find-file-at-mouse (ev)
  7681. "Open file link or URL at mouse."
  7682. (interactive "e")
  7683. (mouse-set-point ev)
  7684. (org-open-at-point 'in-emacs))
  7685. (defun org-open-at-mouse (ev)
  7686. "Open file link or URL at mouse."
  7687. (interactive "e")
  7688. (mouse-set-point ev)
  7689. (if (eq major-mode 'org-agenda-mode)
  7690. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7691. (org-open-at-point))
  7692. (defvar org-window-config-before-follow-link nil
  7693. "The window configuration before following a link.
  7694. This is saved in case the need arises to restore it.")
  7695. (defvar org-open-link-marker (make-marker)
  7696. "Marker pointing to the location where `org-open-at-point; was called.")
  7697. ;;;###autoload
  7698. (defun org-open-at-point-global ()
  7699. "Follow a link like Org-mode does.
  7700. This command can be called in any mode to follow a link that has
  7701. Org-mode syntax."
  7702. (interactive)
  7703. (org-run-like-in-org-mode 'org-open-at-point))
  7704. ;;;###autoload
  7705. (defun org-open-link-from-string (s &optional arg reference-buffer)
  7706. "Open a link in the string S, as if it was in Org-mode."
  7707. (interactive "sLink: \nP")
  7708. (let ((reference-buffer (or reference-buffer (current-buffer))))
  7709. (with-temp-buffer
  7710. (let ((org-inhibit-startup t))
  7711. (org-mode)
  7712. (insert s)
  7713. (goto-char (point-min))
  7714. (when reference-buffer
  7715. (setq org-link-abbrev-alist-local
  7716. (with-current-buffer reference-buffer
  7717. org-link-abbrev-alist-local)))
  7718. (org-open-at-point arg reference-buffer)))))
  7719. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7720. "Open link at or after point.
  7721. If there is no link at point, this function will search forward up to
  7722. the end of the current line.
  7723. Normally, files will be opened by an appropriate application. If the
  7724. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7725. With a double prefix argument, try to open outside of Emacs, in the
  7726. application the system uses for this file type."
  7727. (interactive "P")
  7728. (org-load-modules-maybe)
  7729. (move-marker org-open-link-marker (point))
  7730. (setq org-window-config-before-follow-link (current-window-configuration))
  7731. (org-remove-occur-highlights nil nil t)
  7732. (cond
  7733. ((and (org-on-heading-p)
  7734. (not (org-in-regexp
  7735. (concat org-plain-link-re "\\|"
  7736. org-bracket-link-regexp "\\|"
  7737. org-angle-link-re "\\|"
  7738. "[ \t]:[^ \t\n]+:[ \t]*$")))
  7739. (not (get-text-property (point) 'org-linked-text)))
  7740. (or (org-offer-links-in-entry in-emacs)
  7741. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  7742. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7743. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7744. (org-footnote-action))
  7745. (t
  7746. (let (type path link line search (pos (point)))
  7747. (catch 'match
  7748. (save-excursion
  7749. (skip-chars-forward "^]\n\r")
  7750. (when (org-in-regexp org-bracket-link-regexp 1)
  7751. (setq link (org-extract-attributes
  7752. (org-link-unescape (org-match-string-no-properties 1))))
  7753. (while (string-match " *\n *" link)
  7754. (setq link (replace-match " " t t link)))
  7755. (setq link (org-link-expand-abbrev link))
  7756. (cond
  7757. ((or (file-name-absolute-p link)
  7758. (string-match "^\\.\\.?/" link))
  7759. (setq type "file" path link))
  7760. ((string-match org-link-re-with-space3 link)
  7761. (setq type (match-string 1 link) path (match-string 2 link)))
  7762. (t (setq type "thisfile" path link)))
  7763. (throw 'match t)))
  7764. (when (get-text-property (point) 'org-linked-text)
  7765. (setq type "thisfile"
  7766. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7767. (1+ (point)) (point))
  7768. path (buffer-substring
  7769. (previous-single-property-change pos 'org-linked-text)
  7770. (next-single-property-change pos 'org-linked-text)))
  7771. (throw 'match t))
  7772. (save-excursion
  7773. (when (or (org-in-regexp org-angle-link-re)
  7774. (org-in-regexp org-plain-link-re))
  7775. (setq type (match-string 1) path (match-string 2))
  7776. (throw 'match t)))
  7777. (save-excursion
  7778. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7779. (setq type "tags"
  7780. path (match-string 1))
  7781. (while (string-match ":" path)
  7782. (setq path (replace-match "+" t t path)))
  7783. (throw 'match t)))
  7784. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7785. (setq type "tree-match"
  7786. path (match-string 1))
  7787. (throw 'match t)))
  7788. (unless path
  7789. (error "No link found"))
  7790. ;; switch back to reference buffer
  7791. ;; needed when if called in a temporary buffer through
  7792. ;; org-open-link-from-string
  7793. (with-current-buffer (or reference-buffer (current-buffer))
  7794. ;; Remove any trailing spaces in path
  7795. (if (string-match " +\\'" path)
  7796. (setq path (replace-match "" t t path)))
  7797. (if (and org-link-translation-function
  7798. (fboundp org-link-translation-function))
  7799. ;; Check if we need to translate the link
  7800. (let ((tmp (funcall org-link-translation-function type path)))
  7801. (setq type (car tmp) path (cdr tmp))))
  7802. (cond
  7803. ((assoc type org-link-protocols)
  7804. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7805. ((equal type "mailto")
  7806. (let ((cmd (car org-link-mailto-program))
  7807. (args (cdr org-link-mailto-program)) args1
  7808. (address path) (subject "") a)
  7809. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7810. (setq address (match-string 1 path)
  7811. subject (org-link-escape (match-string 2 path))))
  7812. (while args
  7813. (cond
  7814. ((not (stringp (car args))) (push (pop args) args1))
  7815. (t (setq a (pop args))
  7816. (if (string-match "%a" a)
  7817. (setq a (replace-match address t t a)))
  7818. (if (string-match "%s" a)
  7819. (setq a (replace-match subject t t a)))
  7820. (push a args1))))
  7821. (apply cmd (nreverse args1))))
  7822. ((member type '("http" "https" "ftp" "news"))
  7823. (browse-url (concat type ":" (org-link-escape
  7824. path org-link-escape-chars-browser))))
  7825. ((string= type "doi")
  7826. (browse-url (concat "http://dx.doi.org/"
  7827. (org-link-escape
  7828. path org-link-escape-chars-browser))))
  7829. ((member type '("message"))
  7830. (browse-url (concat type ":" path)))
  7831. ((string= type "tags")
  7832. (org-tags-view in-emacs path))
  7833. ((string= type "tree-match")
  7834. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7835. ((string= type "file")
  7836. (if (string-match "::\\([0-9]+\\)\\'" path)
  7837. (setq line (string-to-number (match-string 1 path))
  7838. path (substring path 0 (match-beginning 0)))
  7839. (if (string-match "::\\(.+\\)\\'" path)
  7840. (setq search (match-string 1 path)
  7841. path (substring path 0 (match-beginning 0)))))
  7842. (if (string-match "[*?{]" (file-name-nondirectory path))
  7843. (dired path)
  7844. (org-open-file path in-emacs line search)))
  7845. ((string= type "news")
  7846. (require 'org-gnus)
  7847. (org-gnus-follow-link path))
  7848. ((string= type "shell")
  7849. (let ((cmd path))
  7850. (if (or (not org-confirm-shell-link-function)
  7851. (funcall org-confirm-shell-link-function
  7852. (format "Execute \"%s\" in shell? "
  7853. (org-add-props cmd nil
  7854. 'face 'org-warning))))
  7855. (progn
  7856. (message "Executing %s" cmd)
  7857. (shell-command cmd))
  7858. (error "Abort"))))
  7859. ((string= type "elisp")
  7860. (let ((cmd path))
  7861. (if (or (not org-confirm-elisp-link-function)
  7862. (funcall org-confirm-elisp-link-function
  7863. (format "Execute \"%s\" as elisp? "
  7864. (org-add-props cmd nil
  7865. 'face 'org-warning))))
  7866. (message "%s => %s" cmd
  7867. (if (equal (string-to-char cmd) ?\()
  7868. (eval (read cmd))
  7869. (call-interactively (read cmd))))
  7870. (error "Abort"))))
  7871. ((and (string= type "thisfile")
  7872. (run-hook-with-args-until-success
  7873. 'org-open-link-functions path)))
  7874. ((string= type "thisfile")
  7875. (if in-emacs
  7876. (switch-to-buffer-other-window
  7877. (org-get-buffer-for-internal-link (current-buffer)))
  7878. (org-mark-ring-push))
  7879. (let ((cmd `(org-link-search
  7880. ,path
  7881. ,(cond ((equal in-emacs '(4)) 'occur)
  7882. ((equal in-emacs '(16)) 'org-occur)
  7883. (t nil))
  7884. ,pos)))
  7885. (condition-case nil (eval cmd)
  7886. (error (progn (widen) (eval cmd))))))
  7887. (t
  7888. (browse-url-at-point)))))))
  7889. (move-marker org-open-link-marker nil)
  7890. (run-hook-with-args 'org-follow-link-hook))
  7891. (defun org-offer-links-in-entry (&optional nth zero)
  7892. "Offer links in the current entry and follow the selected link.
  7893. If there is only one link, follow it immediately as well.
  7894. If NTH is an integer, immediately pick the NTH link found.
  7895. If ZERO is a string, check also this string for a link, and if
  7896. there is one, offer it as link number zero."
  7897. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  7898. "\\(" org-angle-link-re "\\)\\|"
  7899. "\\(" org-plain-link-re "\\)"))
  7900. (cnt ?0)
  7901. (in-emacs (if (integerp nth) nil nth))
  7902. have-zero end links link c)
  7903. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  7904. (push (match-string 0 zero) links)
  7905. (setq cnt (1- cnt) have-zero t))
  7906. (save-excursion
  7907. (org-back-to-heading t)
  7908. (setq end (save-excursion (outline-next-heading) (point)))
  7909. (while (re-search-forward re end t)
  7910. (push (match-string 0) links))
  7911. (setq links (org-uniquify (reverse links))))
  7912. (cond
  7913. ((null links)
  7914. (message "No links"))
  7915. ((equal (length links) 1)
  7916. (setq link (list (car links))))
  7917. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  7918. (setq link (nth (if have-zero nth (1- nth)) links)))
  7919. (t ; we have to select a link
  7920. (save-excursion
  7921. (save-window-excursion
  7922. (delete-other-windows)
  7923. (with-output-to-temp-buffer "*Select Link*"
  7924. (mapc (lambda (l)
  7925. (if (not (string-match org-bracket-link-regexp l))
  7926. (princ (format "[%c] %s\n" (incf cnt)
  7927. (org-remove-angle-brackets l)))
  7928. (if (match-end 3)
  7929. (princ (format "[%c] %s (%s)\n" (incf cnt)
  7930. (match-string 3 l) (match-string 1 l)))
  7931. (princ (format "[%c] %s\n" (incf cnt)
  7932. (match-string 1 l))))))
  7933. links))
  7934. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  7935. (message "Select link to open, RET to open all:")
  7936. (setq c (read-char-exclusive))
  7937. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  7938. (when (equal c ?q) (error "Abort"))
  7939. (if (equal c ?\C-m)
  7940. (setq link links)
  7941. (setq nth (- c ?0))
  7942. (if have-zero (setq nth (1+ nth)))
  7943. (unless (and (integerp nth) (>= (length links) nth))
  7944. (error "Invalid link selection"))
  7945. (setq link (list (nth (1- nth) links))))))
  7946. (if link
  7947. (let ((buf (current-buffer)))
  7948. (dolist (l link)
  7949. (org-open-link-from-string l in-emacs buf))
  7950. t)
  7951. nil)))
  7952. ;; Add special file links that specify the way of opening
  7953. (org-add-link-type "file+sys" 'org-open-file-with-system)
  7954. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  7955. (defun org-open-file-with-system (path)
  7956. "Open file at PATH using the system way of opeing it."
  7957. (org-open-file path 'system))
  7958. (defun org-open-file-with-emacs (path)
  7959. "Open file at PATH in emacs."
  7960. (org-open-file path 'emacs))
  7961. (defun org-remove-file-link-modifiers ()
  7962. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  7963. (goto-char (point-min))
  7964. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  7965. (org-if-unprotected
  7966. (replace-match "file:" t t))))
  7967. (eval-after-load "org-exp"
  7968. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  7969. 'org-remove-file-link-modifiers))
  7970. ;;;; Time estimates
  7971. (defun org-get-effort (&optional pom)
  7972. "Get the effort estimate for the current entry."
  7973. (org-entry-get pom org-effort-property))
  7974. ;;; File search
  7975. (defvar org-create-file-search-functions nil
  7976. "List of functions to construct the right search string for a file link.
  7977. These functions are called in turn with point at the location to
  7978. which the link should point.
  7979. A function in the hook should first test if it would like to
  7980. handle this file type, for example by checking the major-mode or
  7981. the file extension. If it decides not to handle this file, it
  7982. should just return nil to give other functions a chance. If it
  7983. does handle the file, it must return the search string to be used
  7984. when following the link. The search string will be part of the
  7985. file link, given after a double colon, and `org-open-at-point'
  7986. will automatically search for it. If special measures must be
  7987. taken to make the search successful, another function should be
  7988. added to the companion hook `org-execute-file-search-functions',
  7989. which see.
  7990. A function in this hook may also use `setq' to set the variable
  7991. `description' to provide a suggestion for the descriptive text to
  7992. be used for this link when it gets inserted into an Org-mode
  7993. buffer with \\[org-insert-link].")
  7994. (defvar org-execute-file-search-functions nil
  7995. "List of functions to execute a file search triggered by a link.
  7996. Functions added to this hook must accept a single argument, the
  7997. search string that was part of the file link, the part after the
  7998. double colon. The function must first check if it would like to
  7999. handle this search, for example by checking the major-mode or the
  8000. file extension. If it decides not to handle this search, it
  8001. should just return nil to give other functions a chance. If it
  8002. does handle the search, it must return a non-nil value to keep
  8003. other functions from trying.
  8004. Each function can access the current prefix argument through the
  8005. variable `current-prefix-argument'. Note that a single prefix is
  8006. used to force opening a link in Emacs, so it may be good to only
  8007. use a numeric or double prefix to guide the search function.
  8008. In case this is needed, a function in this hook can also restore
  8009. the window configuration before `org-open-at-point' was called using:
  8010. (set-window-configuration org-window-config-before-follow-link)")
  8011. (defun org-link-search (s &optional type avoid-pos)
  8012. "Search for a link search option.
  8013. If S is surrounded by forward slashes, it is interpreted as a
  8014. regular expression. In org-mode files, this will create an `org-occur'
  8015. sparse tree. In ordinary files, `occur' will be used to list matches.
  8016. If the current buffer is in `dired-mode', grep will be used to search
  8017. in all files. If AVOID-POS is given, ignore matches near that position."
  8018. (let ((case-fold-search t)
  8019. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8020. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8021. (append '(("") (" ") ("\t") ("\n"))
  8022. org-emphasis-alist)
  8023. "\\|") "\\)"))
  8024. (pos (point))
  8025. (pre nil) (post nil)
  8026. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8027. (cond
  8028. ;; First check if there are any special
  8029. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8030. ;; Now try the builtin stuff
  8031. ((and (equal (string-to-char s0) ?#)
  8032. (> (length s0) 1)
  8033. (save-excursion
  8034. (goto-char (point-min))
  8035. (and
  8036. (re-search-forward
  8037. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8038. (setq type 'dedicated
  8039. pos (match-beginning 0))))
  8040. ;; There is an exact target for this
  8041. (goto-char pos)
  8042. (org-back-to-heading t)))
  8043. ((save-excursion
  8044. (goto-char (point-min))
  8045. (and
  8046. (re-search-forward
  8047. (concat "<<" (regexp-quote s0) ">>") nil t)
  8048. (setq type 'dedicated
  8049. pos (match-beginning 0))))
  8050. ;; There is an exact target for this
  8051. (goto-char pos))
  8052. ((and (string-match "^(\\(.*\\))$" s0)
  8053. (save-excursion
  8054. (goto-char (point-min))
  8055. (and
  8056. (re-search-forward
  8057. (concat "[^[]" (regexp-quote
  8058. (format org-coderef-label-format
  8059. (match-string 1 s0))))
  8060. nil t)
  8061. (setq type 'dedicated
  8062. pos (1+ (match-beginning 0))))))
  8063. ;; There is a coderef target for this
  8064. (goto-char pos))
  8065. ((string-match "^/\\(.*\\)/$" s)
  8066. ;; A regular expression
  8067. (cond
  8068. ((org-mode-p)
  8069. (org-occur (match-string 1 s)))
  8070. ;;((eq major-mode 'dired-mode)
  8071. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8072. (t (org-do-occur (match-string 1 s)))))
  8073. (t
  8074. ;; A normal search strings
  8075. (when (equal (string-to-char s) ?*)
  8076. ;; Anchor on headlines, post may include tags.
  8077. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8078. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  8079. s (substring s 1)))
  8080. (remove-text-properties
  8081. 0 (length s)
  8082. '(face nil mouse-face nil keymap nil fontified nil) s)
  8083. ;; Make a series of regular expressions to find a match
  8084. (setq words (org-split-string s "[ \n\r\t]+")
  8085. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8086. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8087. "\\)" markers)
  8088. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8089. re2a (concat "[ \t\r\n]" re2a_)
  8090. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8091. re4 (concat "[^a-zA-Z_]" re4_)
  8092. re1 (concat pre re2 post)
  8093. re3 (concat pre (if pre re4_ re4) post)
  8094. re5 (concat pre ".*" re4)
  8095. re2 (concat pre re2)
  8096. re2a (concat pre (if pre re2a_ re2a))
  8097. re4 (concat pre (if pre re4_ re4))
  8098. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8099. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8100. re5 "\\)"
  8101. ))
  8102. (cond
  8103. ((eq type 'org-occur) (org-occur reall))
  8104. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8105. (t (goto-char (point-min))
  8106. (setq type 'fuzzy)
  8107. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8108. (org-search-not-self 1 re1 nil t)
  8109. (org-search-not-self 1 re2 nil t)
  8110. (org-search-not-self 1 re2a nil t)
  8111. (org-search-not-self 1 re3 nil t)
  8112. (org-search-not-self 1 re4 nil t)
  8113. (org-search-not-self 1 re5 nil t)
  8114. )
  8115. (goto-char (match-beginning 1))
  8116. (goto-char pos)
  8117. (error "No match")))))
  8118. (t
  8119. ;; Normal string-search
  8120. (goto-char (point-min))
  8121. (if (search-forward s nil t)
  8122. (goto-char (match-beginning 0))
  8123. (error "No match"))))
  8124. (and (org-mode-p) (org-show-context 'link-search))
  8125. type))
  8126. (defun org-search-not-self (group &rest args)
  8127. "Execute `re-search-forward', but only accept matches that do not
  8128. enclose the position of `org-open-link-marker'."
  8129. (let ((m org-open-link-marker))
  8130. (catch 'exit
  8131. (while (apply 're-search-forward args)
  8132. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8133. (goto-char (match-end group))
  8134. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8135. (> (match-beginning 0) (marker-position m))
  8136. (< (match-end 0) (marker-position m)))
  8137. (save-match-data
  8138. (or (not (org-in-regexp
  8139. org-bracket-link-analytic-regexp 1))
  8140. (not (match-end 4)) ; no description
  8141. (and (<= (match-beginning 4) (point))
  8142. (>= (match-end 4) (point))))))
  8143. (throw 'exit (point))))))))
  8144. (defun org-get-buffer-for-internal-link (buffer)
  8145. "Return a buffer to be used for displaying the link target of internal links."
  8146. (cond
  8147. ((not org-display-internal-link-with-indirect-buffer)
  8148. buffer)
  8149. ((string-match "(Clone)$" (buffer-name buffer))
  8150. (message "Buffer is already a clone, not making another one")
  8151. ;; we also do not modify visibility in this case
  8152. buffer)
  8153. (t ; make a new indirect buffer for displaying the link
  8154. (let* ((bn (buffer-name buffer))
  8155. (ibn (concat bn "(Clone)"))
  8156. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8157. (with-current-buffer ib (org-overview))
  8158. ib))))
  8159. (defun org-do-occur (regexp &optional cleanup)
  8160. "Call the Emacs command `occur'.
  8161. If CLEANUP is non-nil, remove the printout of the regular expression
  8162. in the *Occur* buffer. This is useful if the regex is long and not useful
  8163. to read."
  8164. (occur regexp)
  8165. (when cleanup
  8166. (let ((cwin (selected-window)) win beg end)
  8167. (when (setq win (get-buffer-window "*Occur*"))
  8168. (select-window win))
  8169. (goto-char (point-min))
  8170. (when (re-search-forward "match[a-z]+" nil t)
  8171. (setq beg (match-end 0))
  8172. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8173. (setq end (1- (match-beginning 0)))))
  8174. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8175. (goto-char (point-min))
  8176. (select-window cwin))))
  8177. ;;; The mark ring for links jumps
  8178. (defvar org-mark-ring nil
  8179. "Mark ring for positions before jumps in Org-mode.")
  8180. (defvar org-mark-ring-last-goto nil
  8181. "Last position in the mark ring used to go back.")
  8182. ;; Fill and close the ring
  8183. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8184. (loop for i from 1 to org-mark-ring-length do
  8185. (push (make-marker) org-mark-ring))
  8186. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8187. org-mark-ring)
  8188. (defun org-mark-ring-push (&optional pos buffer)
  8189. "Put the current position or POS into the mark ring and rotate it."
  8190. (interactive)
  8191. (setq pos (or pos (point)))
  8192. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8193. (move-marker (car org-mark-ring)
  8194. (or pos (point))
  8195. (or buffer (current-buffer)))
  8196. (message "%s"
  8197. (substitute-command-keys
  8198. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8199. (defun org-mark-ring-goto (&optional n)
  8200. "Jump to the previous position in the mark ring.
  8201. With prefix arg N, jump back that many stored positions. When
  8202. called several times in succession, walk through the entire ring.
  8203. Org-mode commands jumping to a different position in the current file,
  8204. or to another Org-mode file, automatically push the old position
  8205. onto the ring."
  8206. (interactive "p")
  8207. (let (p m)
  8208. (if (eq last-command this-command)
  8209. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8210. (setq p org-mark-ring))
  8211. (setq org-mark-ring-last-goto p)
  8212. (setq m (car p))
  8213. (switch-to-buffer (marker-buffer m))
  8214. (goto-char m)
  8215. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8216. (defun org-remove-angle-brackets (s)
  8217. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8218. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8219. s)
  8220. (defun org-add-angle-brackets (s)
  8221. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8222. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8223. s)
  8224. (defun org-remove-double-quotes (s)
  8225. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8226. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8227. s)
  8228. ;;; Following specific links
  8229. (defun org-follow-timestamp-link ()
  8230. (cond
  8231. ((org-at-date-range-p t)
  8232. (let ((org-agenda-start-on-weekday)
  8233. (t1 (match-string 1))
  8234. (t2 (match-string 2)))
  8235. (setq t1 (time-to-days (org-time-string-to-time t1))
  8236. t2 (time-to-days (org-time-string-to-time t2)))
  8237. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8238. ((org-at-timestamp-p t)
  8239. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8240. (substring (match-string 1) 0 10)))
  8241. 1))
  8242. (t (error "This should not happen"))))
  8243. ;;; Following file links
  8244. (defvar org-wait nil)
  8245. (defun org-open-file (path &optional in-emacs line search)
  8246. "Open the file at PATH.
  8247. First, this expands any special file name abbreviations. Then the
  8248. configuration variable `org-file-apps' is checked if it contains an
  8249. entry for this file type, and if yes, the corresponding command is launched.
  8250. If no application is found, Emacs simply visits the file.
  8251. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8252. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  8253. and to use an external application to visit the file.
  8254. Optional LINE specifies a line to go to, optional SEARCH a string
  8255. to search for. If LINE or SEARCH is given, the file will be
  8256. opened in Emacs, unless an entry from org-file-apps that makes
  8257. use of groups in a regexp matches.
  8258. If the file does not exist, an error is thrown."
  8259. (let* ((file (if (equal path "")
  8260. buffer-file-name
  8261. (substitute-in-file-name (expand-file-name path))))
  8262. (file-apps (append org-file-apps (org-default-apps)))
  8263. (apps (org-remove-if
  8264. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8265. (apps-dlink (org-remove-if-not
  8266. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8267. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8268. (dirp (if remp nil (file-directory-p file)))
  8269. (file (if (and dirp org-open-directory-means-index-dot-org)
  8270. (concat (file-name-as-directory file) "index.org")
  8271. file))
  8272. (a-m-a-p (assq 'auto-mode apps))
  8273. (dfile (downcase file))
  8274. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  8275. (link (cond ((and (eq line nil)
  8276. (eq search nil))
  8277. file)
  8278. (line
  8279. (concat file "::" (number-to-string line)))
  8280. (search
  8281. (concat file "::" search))))
  8282. (dlink (downcase link))
  8283. (old-buffer (current-buffer))
  8284. (old-pos (point))
  8285. (old-mode major-mode)
  8286. ext cmd link-match-data)
  8287. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8288. (setq ext (match-string 1 dfile))
  8289. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8290. (setq ext (match-string 1 dfile))))
  8291. (cond
  8292. ((member in-emacs '((16) system))
  8293. (setq cmd (cdr (assoc 'system apps))))
  8294. (in-emacs (setq cmd 'emacs))
  8295. (t
  8296. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8297. (and dirp (cdr (assoc 'directory apps)))
  8298. ; first, try matching against apps-dlink
  8299. ; if we get a match here, store the match data for later
  8300. (let ((match (assoc-default dlink apps-dlink
  8301. 'string-match)))
  8302. (if match
  8303. (progn (setq link-match-data (match-data))
  8304. match)
  8305. (progn (setq in-emacs (or in-emacs line search))
  8306. nil))) ; if we have no match in apps-dlink,
  8307. ; always open the file in emacs if line or search
  8308. ; is given (for backwards compatibility)
  8309. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  8310. 'string-match)
  8311. (cdr (assoc ext apps))
  8312. (cdr (assoc t apps))))))
  8313. (when (eq cmd 'system)
  8314. (setq cmd (cdr (assoc 'system apps))))
  8315. (when (eq cmd 'default)
  8316. (setq cmd (cdr (assoc t apps))))
  8317. (when (eq cmd 'mailcap)
  8318. (require 'mailcap)
  8319. (mailcap-parse-mailcaps)
  8320. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8321. (command (mailcap-mime-info mime-type)))
  8322. (if (stringp command)
  8323. (setq cmd command)
  8324. (setq cmd 'emacs))))
  8325. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8326. (not (file-exists-p file))
  8327. (not org-open-non-existing-files))
  8328. (error "No such file: %s" file))
  8329. (cond
  8330. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8331. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8332. (while (string-match "['\"]%s['\"]" cmd)
  8333. (setq cmd (replace-match "%s" t t cmd)))
  8334. (while (string-match "%s" cmd)
  8335. (setq cmd (replace-match
  8336. (save-match-data
  8337. (shell-quote-argument
  8338. (convert-standard-filename file)))
  8339. t t cmd)))
  8340. ;; Replace "%1", "%2" etc. in command with group matches from regex
  8341. (save-match-data
  8342. (let ((match-index 1)
  8343. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  8344. (set-match-data link-match-data)
  8345. (while (<= match-index number-of-groups)
  8346. (let ((regex (concat "%" (number-to-string match-index)))
  8347. (replace-with (match-string match-index dlink)))
  8348. (while (string-match regex cmd)
  8349. (setq cmd (replace-match replace-with t t cmd))))
  8350. (setq match-index (+ match-index 1)))))
  8351. (save-window-excursion
  8352. (start-process-shell-command cmd nil cmd)
  8353. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8354. ))
  8355. ((or (stringp cmd)
  8356. (eq cmd 'emacs))
  8357. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8358. (widen)
  8359. (if line (org-goto-line line)
  8360. (if search (org-link-search search))))
  8361. ((consp cmd)
  8362. (let ((file (convert-standard-filename file)))
  8363. (save-match-data
  8364. (set-match-data link-match-data)
  8365. (eval cmd))))
  8366. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8367. (and (org-mode-p) (eq old-mode 'org-mode)
  8368. (or (not (equal old-buffer (current-buffer)))
  8369. (not (equal old-pos (point))))
  8370. (org-mark-ring-push old-pos old-buffer))))
  8371. (defun org-file-apps-entry-match-against-dlink-p (entry)
  8372. "This function returns non-nil if `entry' uses a regular
  8373. expression which should be matched against the whole link by
  8374. org-open-file.
  8375. It assumes that is the case when the entry uses a regular
  8376. expression which has at least one grouping construct and the
  8377. action is either a lisp form or a command string containing
  8378. '%1', i.e. using at least one subexpression match as a
  8379. parameter."
  8380. (let ((selector (car entry))
  8381. (action (cdr entry)))
  8382. (if (stringp selector)
  8383. (and (> (regexp-opt-depth selector) 0)
  8384. (or (and (stringp action)
  8385. (string-match "%[0-9]" action))
  8386. (consp action)))
  8387. nil)))
  8388. (defun org-default-apps ()
  8389. "Return the default applications for this operating system."
  8390. (cond
  8391. ((eq system-type 'darwin)
  8392. org-file-apps-defaults-macosx)
  8393. ((eq system-type 'windows-nt)
  8394. org-file-apps-defaults-windowsnt)
  8395. (t org-file-apps-defaults-gnu)))
  8396. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  8397. "Convert extensions to regular expressions in the cars of LIST.
  8398. Also, weed out any non-string entries, because the return value is used
  8399. only for regexp matching.
  8400. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  8401. point to the symbol `emacs', indicating that the file should
  8402. be opened in Emacs."
  8403. (append
  8404. (delq nil
  8405. (mapcar (lambda (x)
  8406. (if (not (stringp (car x)))
  8407. nil
  8408. (if (string-match "\\W" (car x))
  8409. x
  8410. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  8411. list))
  8412. (if add-auto-mode
  8413. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  8414. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  8415. (defun org-file-remote-p (file)
  8416. "Test whether FILE specifies a location on a remote system.
  8417. Return non-nil if the location is indeed remote.
  8418. For example, the filename \"/user@host:/foo\" specifies a location
  8419. on the system \"/user@host:\"."
  8420. (cond ((fboundp 'file-remote-p)
  8421. (file-remote-p file))
  8422. ((fboundp 'tramp-handle-file-remote-p)
  8423. (tramp-handle-file-remote-p file))
  8424. ((and (boundp 'ange-ftp-name-format)
  8425. (string-match (car ange-ftp-name-format) file))
  8426. t)
  8427. (t nil)))
  8428. ;;;; Refiling
  8429. (defun org-get-org-file ()
  8430. "Read a filename, with default directory `org-directory'."
  8431. (let ((default (or org-default-notes-file remember-data-file)))
  8432. (read-file-name (format "File name [%s]: " default)
  8433. (file-name-as-directory org-directory)
  8434. default)))
  8435. (defun org-notes-order-reversed-p ()
  8436. "Check if the current file should receive notes in reversed order."
  8437. (cond
  8438. ((not org-reverse-note-order) nil)
  8439. ((eq t org-reverse-note-order) t)
  8440. ((not (listp org-reverse-note-order)) nil)
  8441. (t (catch 'exit
  8442. (let ((all org-reverse-note-order)
  8443. entry)
  8444. (while (setq entry (pop all))
  8445. (if (string-match (car entry) buffer-file-name)
  8446. (throw 'exit (cdr entry))))
  8447. nil)))))
  8448. (defvar org-refile-target-table nil
  8449. "The list of refile targets, created by `org-refile'.")
  8450. (defvar org-agenda-new-buffers nil
  8451. "Buffers created to visit agenda files.")
  8452. (defun org-get-refile-targets (&optional default-buffer)
  8453. "Produce a table with refile targets."
  8454. (let ((case-fold-search nil)
  8455. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  8456. (entries (or org-refile-targets '((nil . (:level . 1)))))
  8457. targets txt re files f desc descre fast-path-p level pos0)
  8458. (message "Getting targets...")
  8459. (with-current-buffer (or default-buffer (current-buffer))
  8460. (while (setq entry (pop entries))
  8461. (setq files (car entry) desc (cdr entry))
  8462. (setq fast-path-p nil)
  8463. (cond
  8464. ((null files) (setq files (list (current-buffer))))
  8465. ((eq files 'org-agenda-files)
  8466. (setq files (org-agenda-files 'unrestricted)))
  8467. ((and (symbolp files) (fboundp files))
  8468. (setq files (funcall files)))
  8469. ((and (symbolp files) (boundp files))
  8470. (setq files (symbol-value files))))
  8471. (if (stringp files) (setq files (list files)))
  8472. (cond
  8473. ((eq (car desc) :tag)
  8474. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  8475. ((eq (car desc) :todo)
  8476. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  8477. ((eq (car desc) :regexp)
  8478. (setq descre (cdr desc)))
  8479. ((eq (car desc) :level)
  8480. (setq descre (concat "^\\*\\{" (number-to-string
  8481. (if org-odd-levels-only
  8482. (1- (* 2 (cdr desc)))
  8483. (cdr desc)))
  8484. "\\}[ \t]")))
  8485. ((eq (car desc) :maxlevel)
  8486. (setq fast-path-p t)
  8487. (setq descre (concat "^\\*\\{1," (number-to-string
  8488. (if org-odd-levels-only
  8489. (1- (* 2 (cdr desc)))
  8490. (cdr desc)))
  8491. "\\}[ \t]")))
  8492. (t (error "Bad refiling target description %s" desc)))
  8493. (while (setq f (pop files))
  8494. (with-current-buffer
  8495. (if (bufferp f) f (org-get-agenda-file-buffer f))
  8496. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  8497. (setq f (and f (expand-file-name f)))
  8498. (if (eq org-refile-use-outline-path 'file)
  8499. (push (list (file-name-nondirectory f) f nil nil) targets))
  8500. (save-excursion
  8501. (save-restriction
  8502. (widen)
  8503. (goto-char (point-min))
  8504. (while (re-search-forward descre nil t)
  8505. (goto-char (setq pos0 (point-at-bol)))
  8506. (catch 'next
  8507. (when org-refile-target-verify-function
  8508. (save-match-data
  8509. (or (funcall org-refile-target-verify-function)
  8510. (throw 'next t))))
  8511. (when (looking-at org-complex-heading-regexp)
  8512. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  8513. txt (org-link-display-format (match-string 4))
  8514. re (concat "^" (regexp-quote
  8515. (buffer-substring (match-beginning 1)
  8516. (match-end 4)))))
  8517. (if (match-end 5) (setq re (concat re "[ \t]+"
  8518. (regexp-quote
  8519. (match-string 5)))))
  8520. (setq re (concat re "[ \t]*$"))
  8521. (when org-refile-use-outline-path
  8522. (setq txt (mapconcat 'org-protect-slash
  8523. (append
  8524. (if (eq org-refile-use-outline-path 'file)
  8525. (list (file-name-nondirectory
  8526. (buffer-file-name (buffer-base-buffer))))
  8527. (if (eq org-refile-use-outline-path 'full-file-path)
  8528. (list (buffer-file-name (buffer-base-buffer)))))
  8529. (org-get-outline-path fast-path-p level txt)
  8530. (list txt))
  8531. "/")))
  8532. (push (list txt f re (point)) targets)))
  8533. (when (= (point) pos0)
  8534. ;; verification function has not moved point
  8535. (goto-char (point-at-eol))))))))))
  8536. (message "Getting targets...done")
  8537. (nreverse targets)))
  8538. (defun org-protect-slash (s)
  8539. (while (string-match "/" s)
  8540. (setq s (replace-match "\\" t t s)))
  8541. s)
  8542. (defvar org-olpa (make-vector 20 nil))
  8543. (defun org-get-outline-path (&optional fastp level heading)
  8544. "Return the outline path to the current entry, as a list.
  8545. The parameters FASTP, LEVEL, and HEADING are for use be a scanner
  8546. routine which makes outline path derivations for an entire file,
  8547. avoiding backtracing."
  8548. (if fastp
  8549. (progn
  8550. (if (> level 19)
  8551. (error "Outline path failure, more than 19 levels."))
  8552. (loop for i from level upto 19 do
  8553. (aset org-olpa i nil))
  8554. (prog1
  8555. (delq nil (append org-olpa nil))
  8556. (aset org-olpa level heading)))
  8557. (let (rtn case-fold-search)
  8558. (save-excursion
  8559. (save-restriction
  8560. (widen)
  8561. (while (org-up-heading-safe)
  8562. (when (looking-at org-complex-heading-regexp)
  8563. (push (org-match-string-no-properties 4) rtn)))
  8564. rtn)))))
  8565. (defun org-format-outline-path (path &optional width prefix)
  8566. "Format the outlie path PATH for display.
  8567. Width is the maximum number of characters that is available.
  8568. Prefix is a prefix to be included in the returned string,
  8569. such as the file name."
  8570. (setq width (or width 79))
  8571. (if prefix (setq width (- width (length prefix))))
  8572. (if (not path)
  8573. (or prefix "")
  8574. (let* ((nsteps (length path))
  8575. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  8576. (maxwidth (if (<= total-width width)
  8577. 10000 ;; everything fits
  8578. ;; we need to shorten the level headings
  8579. (/ (- width nsteps) nsteps)))
  8580. (org-odd-levels-only nil)
  8581. (n 0)
  8582. (total (1+ (length prefix))))
  8583. (setq maxwidth (max maxwidth 10))
  8584. (concat prefix
  8585. (mapconcat
  8586. (lambda (h)
  8587. (setq n (1+ n))
  8588. (if (and (= n nsteps) (< maxwidth 10000))
  8589. (setq maxwidth (- total-width total)))
  8590. (if (< (length h) maxwidth)
  8591. (progn (setq total (+ total (length h) 1)) h)
  8592. (setq h (substring h 0 (- maxwidth 2))
  8593. total (+ total maxwidth 1))
  8594. (if (string-match "[ \t]+\\'" h)
  8595. (setq h (substring h 0 (match-beginning 0))))
  8596. (setq h (concat h "..")))
  8597. (org-add-props h nil 'face
  8598. (nth (% (1- n) org-n-level-faces)
  8599. org-level-faces))
  8600. h)
  8601. path "/")))))
  8602. (defun org-display-outline-path (&optional file current)
  8603. "Display the current outline path in the echo area."
  8604. (interactive "P")
  8605. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  8606. (case-fold-search nil)
  8607. (path (and (org-mode-p) (org-get-outline-path))))
  8608. (if current (setq path (append path
  8609. (save-excursion
  8610. (org-back-to-heading t)
  8611. (if (looking-at org-complex-heading-regexp)
  8612. (list (match-string 4)))))))
  8613. (message "%s"
  8614. (org-format-outline-path
  8615. path
  8616. (1- (frame-width))
  8617. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  8618. (defvar org-refile-history nil
  8619. "History for refiling operations.")
  8620. (defvar org-after-refile-insert-hook nil
  8621. "Hook run after `org-refile' has inserted its stuff at the new location.
  8622. Note that this is still *before* the stuff will be removed from
  8623. the *old* location.")
  8624. (defun org-refile (&optional goto default-buffer rfloc)
  8625. "Move the entry at point to another heading.
  8626. The list of target headings is compiled using the information in
  8627. `org-refile-targets', which see. This list is created before each use
  8628. and will therefore always be up-to-date.
  8629. At the target location, the entry is filed as a subitem of the target heading.
  8630. Depending on `org-reverse-note-order', the new subitem will either be the
  8631. first or the last subitem.
  8632. If there is an active region, all entries in that region will be moved.
  8633. However, the region must fulfil the requirement that the first heading
  8634. is the first one sets the top-level of the moved text - at most siblings
  8635. below it are allowed.
  8636. With prefix arg GOTO, the command will only visit the target location,
  8637. not actually move anything.
  8638. With a double prefix `C-u C-u', go to the location where the last refiling
  8639. operation has put the subtree.
  8640. With a prefix argument of `2', refile to the running clock.
  8641. RFLOC can be a refile location obtained in a different way.
  8642. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  8643. (interactive "P")
  8644. (let* ((cbuf (current-buffer))
  8645. (regionp (org-region-active-p))
  8646. (region-start (and regionp (region-beginning)))
  8647. (region-end (and regionp (region-end)))
  8648. (region-length (and regionp (- region-end region-start)))
  8649. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  8650. pos it nbuf file re level reversed)
  8651. (setq last-command nil)
  8652. (when regionp
  8653. (goto-char region-start)
  8654. (or (bolp) (goto-char (point-at-bol)))
  8655. (setq region-start (point))
  8656. (unless (org-kill-is-subtree-p
  8657. (buffer-substring region-start region-end))
  8658. (error "The region is not a (sequence of) subtree(s)")))
  8659. (if (equal goto '(16))
  8660. (org-refile-goto-last-stored)
  8661. (when (or
  8662. (and (equal goto 2)
  8663. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  8664. (prog1
  8665. (setq it (list (or org-clock-heading "running clock")
  8666. (buffer-file-name
  8667. (marker-buffer org-clock-hd-marker))
  8668. ""
  8669. (marker-position org-clock-hd-marker)))
  8670. (setq goto nil)))
  8671. (setq it (or rfloc
  8672. (save-excursion
  8673. (org-refile-get-location
  8674. (if goto "Goto: " "Refile to: ") default-buffer
  8675. org-refile-allow-creating-parent-nodes)))))
  8676. (setq file (nth 1 it)
  8677. re (nth 2 it)
  8678. pos (nth 3 it))
  8679. (if (and (not goto)
  8680. pos
  8681. (equal (buffer-file-name) file)
  8682. (if regionp
  8683. (and (>= pos region-start)
  8684. (<= pos region-end))
  8685. (and (>= pos (point))
  8686. (< pos (save-excursion
  8687. (org-end-of-subtree t t))))))
  8688. (error "Cannot refile to position inside the tree or region"))
  8689. (setq nbuf (or (find-buffer-visiting file)
  8690. (find-file-noselect file)))
  8691. (if goto
  8692. (progn
  8693. (switch-to-buffer nbuf)
  8694. (goto-char pos)
  8695. (org-show-context 'org-goto))
  8696. (if regionp
  8697. (progn
  8698. (org-kill-new (buffer-substring region-start region-end))
  8699. (org-save-markers-in-region region-start region-end))
  8700. (org-copy-subtree 1 nil t))
  8701. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  8702. (find-file-noselect file)))
  8703. (setq reversed (org-notes-order-reversed-p))
  8704. (save-excursion
  8705. (save-restriction
  8706. (widen)
  8707. (if pos
  8708. (progn
  8709. (goto-char pos)
  8710. (looking-at outline-regexp)
  8711. (setq level (org-get-valid-level (funcall outline-level) 1))
  8712. (goto-char
  8713. (if reversed
  8714. (or (outline-next-heading) (point-max))
  8715. (or (save-excursion (org-get-next-sibling))
  8716. (org-end-of-subtree t t)
  8717. (point-max)))))
  8718. (setq level 1)
  8719. (if (not reversed)
  8720. (goto-char (point-max))
  8721. (goto-char (point-min))
  8722. (or (outline-next-heading) (goto-char (point-max)))))
  8723. (if (not (bolp)) (newline))
  8724. (org-paste-subtree level)
  8725. (when org-log-refile
  8726. (org-add-log-setup 'refile nil nil 'findpos
  8727. org-log-refile)
  8728. (unless (eq org-log-refile 'note)
  8729. (save-excursion (org-add-log-note))))
  8730. (and org-auto-align-tags (org-set-tags nil t))
  8731. (bookmark-set "org-refile-last-stored")
  8732. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8733. (run-hooks 'org-after-refile-insert-hook))))
  8734. (if regionp
  8735. (delete-region (point) (+ (point) region-length))
  8736. (org-cut-subtree))
  8737. (when (featurep 'org-inlinetask)
  8738. (org-inlinetask-remove-END-maybe))
  8739. (setq org-markers-to-move nil)
  8740. (message "Refiled to \"%s\"" (car it)))))))
  8741. (defun org-refile-goto-last-stored ()
  8742. "Go to the location where the last refile was stored."
  8743. (interactive)
  8744. (bookmark-jump "org-refile-last-stored")
  8745. (message "This is the location of the last refile"))
  8746. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  8747. "Prompt the user for a refile location, using PROMPT."
  8748. (let ((org-refile-targets org-refile-targets)
  8749. (org-refile-use-outline-path org-refile-use-outline-path))
  8750. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  8751. (unless org-refile-target-table
  8752. (error "No refile targets"))
  8753. (let* ((cbuf (current-buffer))
  8754. (partial-completion-mode nil)
  8755. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  8756. (cfunc (if (and org-refile-use-outline-path
  8757. org-outline-path-complete-in-steps)
  8758. 'org-olpath-completing-read
  8759. 'org-icompleting-read))
  8760. (extra (if org-refile-use-outline-path "/" ""))
  8761. (filename (and cfn (expand-file-name cfn)))
  8762. (tbl (mapcar
  8763. (lambda (x)
  8764. (if (and (not (member org-refile-use-outline-path
  8765. '(file full-file-path)))
  8766. (not (equal filename (nth 1 x))))
  8767. (cons (concat (car x) extra " ("
  8768. (file-name-nondirectory (nth 1 x)) ")")
  8769. (cdr x))
  8770. (cons (concat (car x) extra) (cdr x))))
  8771. org-refile-target-table))
  8772. (completion-ignore-case t)
  8773. pa answ parent-target child parent old-hist)
  8774. (setq old-hist org-refile-history)
  8775. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  8776. nil 'org-refile-history))
  8777. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  8778. (if pa
  8779. (progn
  8780. (when (or (not org-refile-history)
  8781. (not (eq old-hist org-refile-history))
  8782. (not (equal (car pa) (car org-refile-history))))
  8783. (setq org-refile-history
  8784. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  8785. org-refile-history
  8786. (cdr org-refile-history))))
  8787. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  8788. (pop org-refile-history)))
  8789. pa)
  8790. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8791. (progn
  8792. (setq parent (match-string 1 answ)
  8793. child (match-string 2 answ))
  8794. (setq parent-target (or (assoc parent tbl)
  8795. (assoc (concat parent "/") tbl)))
  8796. (when (and parent-target
  8797. (or (eq new-nodes t)
  8798. (and (eq new-nodes 'confirm)
  8799. (y-or-n-p (format "Create new node \"%s\"? "
  8800. child)))))
  8801. (org-refile-new-child parent-target child)))
  8802. (error "Invalid target location")))))
  8803. (defun org-refile-new-child (parent-target child)
  8804. "Use refile target PARENT-TARGET to add new CHILD below it."
  8805. (unless parent-target
  8806. (error "Cannot find parent for new node"))
  8807. (let ((file (nth 1 parent-target))
  8808. (pos (nth 3 parent-target))
  8809. level)
  8810. (with-current-buffer (or (find-buffer-visiting file)
  8811. (find-file-noselect file))
  8812. (save-excursion
  8813. (save-restriction
  8814. (widen)
  8815. (if pos
  8816. (goto-char pos)
  8817. (goto-char (point-max))
  8818. (if (not (bolp)) (newline)))
  8819. (when (looking-at outline-regexp)
  8820. (setq level (funcall outline-level))
  8821. (org-end-of-subtree t t))
  8822. (org-back-over-empty-lines)
  8823. (insert "\n" (make-string
  8824. (if pos (org-get-valid-level level 1) 1) ?*)
  8825. " " child "\n")
  8826. (beginning-of-line 0)
  8827. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8828. (defun org-olpath-completing-read (prompt collection &rest args)
  8829. "Read an outline path like a file name."
  8830. (let ((thetable collection)
  8831. (org-completion-use-ido nil) ; does not work with ido.
  8832. (org-completion-use-iswitchb nil)) ; or iswitchb
  8833. (apply
  8834. 'org-icompleting-read prompt
  8835. (lambda (string predicate &optional flag)
  8836. (let (rtn r f (l (length string)))
  8837. (cond
  8838. ((eq flag nil)
  8839. ;; try completion
  8840. (try-completion string thetable))
  8841. ((eq flag t)
  8842. ;; all-completions
  8843. (setq rtn (all-completions string thetable predicate))
  8844. (mapcar
  8845. (lambda (x)
  8846. (setq r (substring x l))
  8847. (if (string-match " ([^)]*)$" x)
  8848. (setq f (match-string 0 x))
  8849. (setq f ""))
  8850. (if (string-match "/" r)
  8851. (concat string (substring r 0 (match-end 0)) f)
  8852. x))
  8853. rtn))
  8854. ((eq flag 'lambda)
  8855. ;; exact match?
  8856. (assoc string thetable)))
  8857. ))
  8858. args)))
  8859. ;;;; Dynamic blocks
  8860. (defun org-find-dblock (name)
  8861. "Find the first dynamic block with name NAME in the buffer.
  8862. If not found, stay at current position and return nil."
  8863. (let (pos)
  8864. (save-excursion
  8865. (goto-char (point-min))
  8866. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8867. nil t)
  8868. (match-beginning 0))))
  8869. (if pos (goto-char pos))
  8870. pos))
  8871. (defconst org-dblock-start-re
  8872. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8873. "Matches the start line of a dynamic block, with parameters.")
  8874. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  8875. "Matches the end of a dynamic block.")
  8876. (defun org-create-dblock (plist)
  8877. "Create a dynamic block section, with parameters taken from PLIST.
  8878. PLIST must contain a :name entry which is used as name of the block."
  8879. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  8880. (end-of-line 1)
  8881. (newline))
  8882. (let ((col (current-column))
  8883. (name (plist-get plist :name)))
  8884. (insert "#+BEGIN: " name)
  8885. (while plist
  8886. (if (eq (car plist) :name)
  8887. (setq plist (cddr plist))
  8888. (insert " " (prin1-to-string (pop plist)))))
  8889. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  8890. (beginning-of-line -2)))
  8891. (defun org-prepare-dblock ()
  8892. "Prepare dynamic block for refresh.
  8893. This empties the block, puts the cursor at the insert position and returns
  8894. the property list including an extra property :name with the block name."
  8895. (unless (looking-at org-dblock-start-re)
  8896. (error "Not at a dynamic block"))
  8897. (let* ((begdel (1+ (match-end 0)))
  8898. (name (org-no-properties (match-string 1)))
  8899. (params (append (list :name name)
  8900. (read (concat "(" (match-string 3) ")")))))
  8901. (save-excursion
  8902. (beginning-of-line 1)
  8903. (skip-chars-forward " \t")
  8904. (setq params (plist-put params :indentation-column (current-column))))
  8905. (unless (re-search-forward org-dblock-end-re nil t)
  8906. (error "Dynamic block not terminated"))
  8907. (setq params
  8908. (append params
  8909. (list :content (buffer-substring
  8910. begdel (match-beginning 0)))))
  8911. (delete-region begdel (match-beginning 0))
  8912. (goto-char begdel)
  8913. (open-line 1)
  8914. params))
  8915. (defun org-map-dblocks (&optional command)
  8916. "Apply COMMAND to all dynamic blocks in the current buffer.
  8917. If COMMAND is not given, use `org-update-dblock'."
  8918. (let ((cmd (or command 'org-update-dblock)))
  8919. (save-excursion
  8920. (goto-char (point-min))
  8921. (while (re-search-forward org-dblock-start-re nil t)
  8922. (goto-char (match-beginning 0))
  8923. (save-excursion
  8924. (condition-case nil
  8925. (funcall cmd)
  8926. (error (message "Error during update of dynamic block"))))
  8927. (unless (re-search-forward org-dblock-end-re nil t)
  8928. (error "Dynamic block not terminated"))))))
  8929. (defun org-dblock-update (&optional arg)
  8930. "User command for updating dynamic blocks.
  8931. Update the dynamic block at point. With prefix ARG, update all dynamic
  8932. blocks in the buffer."
  8933. (interactive "P")
  8934. (if arg
  8935. (org-update-all-dblocks)
  8936. (or (looking-at org-dblock-start-re)
  8937. (org-beginning-of-dblock))
  8938. (org-update-dblock)))
  8939. (defun org-update-dblock ()
  8940. "Update the dynamic block at point
  8941. This means to empty the block, parse for parameters and then call
  8942. the correct writing function."
  8943. (save-window-excursion
  8944. (let* ((pos (point))
  8945. (line (org-current-line))
  8946. (params (org-prepare-dblock))
  8947. (name (plist-get params :name))
  8948. (indent (plist-get params :indentation-column))
  8949. (cmd (intern (concat "org-dblock-write:" name))))
  8950. (message "Updating dynamic block `%s' at line %d..." name line)
  8951. (funcall cmd params)
  8952. (message "Updating dynamic block `%s' at line %d...done" name line)
  8953. (goto-char pos)
  8954. (when (and indent (> indent 0))
  8955. (setq indent (make-string indent ?\ ))
  8956. (save-excursion
  8957. (org-beginning-of-dblock)
  8958. (forward-line 1)
  8959. (while (not (looking-at org-dblock-end-re))
  8960. (insert indent)
  8961. (beginning-of-line 2))
  8962. (when (looking-at org-dblock-end-re)
  8963. (and (looking-at "[ \t]+")
  8964. (replace-match ""))
  8965. (insert indent)))))))
  8966. (defun org-beginning-of-dblock ()
  8967. "Find the beginning of the dynamic block at point.
  8968. Error if there is no such block at point."
  8969. (let ((pos (point))
  8970. beg)
  8971. (end-of-line 1)
  8972. (if (and (re-search-backward org-dblock-start-re nil t)
  8973. (setq beg (match-beginning 0))
  8974. (re-search-forward org-dblock-end-re nil t)
  8975. (> (match-end 0) pos))
  8976. (goto-char beg)
  8977. (goto-char pos)
  8978. (error "Not in a dynamic block"))))
  8979. (defun org-update-all-dblocks ()
  8980. "Update all dynamic blocks in the buffer.
  8981. This function can be used in a hook."
  8982. (when (org-mode-p)
  8983. (org-map-dblocks 'org-update-dblock)))
  8984. ;;;; Completion
  8985. (defconst org-additional-option-like-keywords
  8986. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8987. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8988. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  8989. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX"
  8990. "BEGIN:" "END:"
  8991. "ORGTBL" "TBLFM:" "TBLNAME:"
  8992. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8993. "BEGIN_QUOTE" "END_QUOTE"
  8994. "BEGIN_VERSE" "END_VERSE"
  8995. "BEGIN_CENTER" "END_CENTER"
  8996. "BEGIN_SRC" "END_SRC"
  8997. "CATEGORY" "COLUMNS"
  8998. "CAPTION" "LABEL"
  8999. "SETUPFILE"
  9000. "BIND"
  9001. "MACRO"))
  9002. (defcustom org-structure-template-alist
  9003. '(
  9004. ("s" "#+begin_src ?\n\n#+end_src"
  9005. "<src lang=\"?\">\n\n</src>")
  9006. ("e" "#+begin_example\n?\n#+end_example"
  9007. "<example>\n?\n</example>")
  9008. ("q" "#+begin_quote\n?\n#+end_quote"
  9009. "<quote>\n?\n</quote>")
  9010. ("v" "#+begin_verse\n?\n#+end_verse"
  9011. "<verse>\n?\n/verse>")
  9012. ("c" "#+begin_center\n?\n#+end_center"
  9013. "<center>\n?\n/center>")
  9014. ("l" "#+begin_latex\n?\n#+end_latex"
  9015. "<literal style=\"latex\">\n?\n</literal>")
  9016. ("L" "#+latex: "
  9017. "<literal style=\"latex\">?</literal>")
  9018. ("h" "#+begin_html\n?\n#+end_html"
  9019. "<literal style=\"html\">\n?\n</literal>")
  9020. ("H" "#+html: "
  9021. "<literal style=\"html\">?</literal>")
  9022. ("a" "#+begin_ascii\n?\n#+end_ascii")
  9023. ("A" "#+ascii: ")
  9024. ("i" "#+include %file ?"
  9025. "<include file=%file markup=\"?\">")
  9026. )
  9027. "Structure completion elements.
  9028. This is a list of abbreviation keys and values. The value gets inserted
  9029. if you type `<' followed by the key and then press the completion key,
  9030. usually `M-TAB'. %file will be replaced by a file name after prompting
  9031. for the file using completion.
  9032. There are two templates for each key, the first uses the original Org syntax,
  9033. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  9034. the default when the /org-mtags.el/ module has been loaded. See also the
  9035. variable `org-mtags-prefer-muse-templates'.
  9036. This is an experimental feature, it is undecided if it is going to stay in."
  9037. :group 'org-completion
  9038. :type '(repeat
  9039. (string :tag "Key")
  9040. (string :tag "Template")
  9041. (string :tag "Muse Template")))
  9042. (defun org-try-structure-completion ()
  9043. "Try to complete a structure template before point.
  9044. This looks for strings like \"<e\" on an otherwise empty line and
  9045. expands them."
  9046. (let ((l (buffer-substring (point-at-bol) (point)))
  9047. a)
  9048. (when (and (looking-at "[ \t]*$")
  9049. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  9050. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  9051. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  9052. (match-beginning 1)) a)
  9053. t)))
  9054. (defun org-complete-expand-structure-template (start cell)
  9055. "Expand a structure template."
  9056. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  9057. (rpl (nth (if musep 2 1) cell))
  9058. (ind ""))
  9059. (delete-region start (point))
  9060. (when (string-match "\\`#\\+" rpl)
  9061. (cond
  9062. ((bolp))
  9063. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  9064. (setq ind (buffer-substring (point-at-bol) (point))))
  9065. (t (newline))))
  9066. (setq start (point))
  9067. (if (string-match "%file" rpl)
  9068. (setq rpl (replace-match
  9069. (concat
  9070. "\""
  9071. (save-match-data
  9072. (abbreviate-file-name (read-file-name "Include file: ")))
  9073. "\"")
  9074. t t rpl)))
  9075. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  9076. (concat "\n" ind)))
  9077. (insert rpl)
  9078. (if (re-search-backward "\\?" start t) (delete-char 1))))
  9079. (defun org-complete (&optional arg)
  9080. "Perform completion on word at point.
  9081. At the beginning of a headline, this completes TODO keywords as given in
  9082. `org-todo-keywords'.
  9083. If the current word is preceded by a backslash, completes the TeX symbols
  9084. that are supported for HTML support.
  9085. If the current word is preceded by \"#+\", completes special words for
  9086. setting file options.
  9087. In the line after \"#+STARTUP:, complete valid keywords.\"
  9088. At all other locations, this simply calls the value of
  9089. `org-completion-fallback-command'."
  9090. (interactive "P")
  9091. (org-without-partial-completion
  9092. (catch 'exit
  9093. (let* ((a nil)
  9094. (end (point))
  9095. (beg1 (save-excursion
  9096. (skip-chars-backward (org-re "[:alnum:]_@"))
  9097. (point)))
  9098. (beg (save-excursion
  9099. (skip-chars-backward "a-zA-Z0-9_:$")
  9100. (point)))
  9101. (confirm (lambda (x) (stringp (car x))))
  9102. (searchhead (equal (char-before beg) ?*))
  9103. (struct
  9104. (when (and (member (char-before beg1) '(?. ?<))
  9105. (setq a (assoc (buffer-substring beg1 (point))
  9106. org-structure-template-alist)))
  9107. (org-complete-expand-structure-template (1- beg1) a)
  9108. (throw 'exit t)))
  9109. (tag (and (equal (char-before beg1) ?:)
  9110. (equal (char-after (point-at-bol)) ?*)))
  9111. (prop (and (equal (char-before beg1) ?:)
  9112. (not (equal (char-after (point-at-bol)) ?*))))
  9113. (texp (equal (char-before beg) ?\\))
  9114. (link (equal (char-before beg) ?\[))
  9115. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  9116. beg)
  9117. "#+"))
  9118. (startup (string-match "^#\\+STARTUP:.*"
  9119. (buffer-substring (point-at-bol) (point))))
  9120. (completion-ignore-case opt)
  9121. (type nil)
  9122. (tbl nil)
  9123. (table (cond
  9124. (opt
  9125. (setq type :opt)
  9126. (require 'org-exp)
  9127. (append
  9128. (delq nil
  9129. (mapcar
  9130. (lambda (x)
  9131. (if (string-match
  9132. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  9133. (cons (match-string 2 x)
  9134. (match-string 1 x))))
  9135. (org-split-string (org-get-current-options) "\n")))
  9136. (mapcar 'list org-additional-option-like-keywords)))
  9137. (startup
  9138. (setq type :startup)
  9139. org-startup-options)
  9140. (link (append org-link-abbrev-alist-local
  9141. org-link-abbrev-alist))
  9142. (texp
  9143. (setq type :tex)
  9144. (append org-entities-user org-entities))
  9145. ((string-match "\\`\\*+[ \t]+\\'"
  9146. (buffer-substring (point-at-bol) beg))
  9147. (setq type :todo)
  9148. (mapcar 'list org-todo-keywords-1))
  9149. (searchhead
  9150. (setq type :searchhead)
  9151. (save-excursion
  9152. (goto-char (point-min))
  9153. (while (re-search-forward org-todo-line-regexp nil t)
  9154. (push (list
  9155. (org-make-org-heading-search-string
  9156. (match-string 3) t))
  9157. tbl)))
  9158. tbl)
  9159. (tag (setq type :tag beg beg1)
  9160. (or org-tag-alist (org-get-buffer-tags)))
  9161. (prop (setq type :prop beg beg1)
  9162. (mapcar 'list (org-buffer-property-keys nil t t)))
  9163. (t (progn
  9164. (call-interactively org-completion-fallback-command)
  9165. (throw 'exit nil)))))
  9166. (pattern (buffer-substring-no-properties beg end))
  9167. (completion (try-completion pattern table confirm)))
  9168. (cond ((eq completion t)
  9169. (if (not (assoc (upcase pattern) table))
  9170. (message "Already complete")
  9171. (if (and (equal type :opt)
  9172. (not (member (car (assoc (upcase pattern) table))
  9173. org-additional-option-like-keywords)))
  9174. (insert (substring (cdr (assoc (upcase pattern) table))
  9175. (length pattern)))
  9176. (if (memq type '(:tag :prop)) (insert ":")))))
  9177. ((null completion)
  9178. (message "Can't find completion for \"%s\"" pattern)
  9179. (ding))
  9180. ((not (string= pattern completion))
  9181. (delete-region beg end)
  9182. (if (string-match " +$" completion)
  9183. (setq completion (replace-match "" t t completion)))
  9184. (insert completion)
  9185. (if (get-buffer-window "*Completions*")
  9186. (delete-window (get-buffer-window "*Completions*")))
  9187. (if (assoc completion table)
  9188. (if (eq type :todo) (insert " ")
  9189. (if (memq type '(:tag :prop)) (insert ":"))))
  9190. (if (and (equal type :opt) (assoc completion table))
  9191. (message "%s" (substitute-command-keys
  9192. "Press \\[org-complete] again to insert example settings"))))
  9193. (t
  9194. (message "Making completion list...")
  9195. (let ((list (sort (all-completions pattern table confirm)
  9196. 'string<)))
  9197. (with-output-to-temp-buffer "*Completions*"
  9198. (condition-case nil
  9199. ;; Protection needed for XEmacs and emacs 21
  9200. (display-completion-list list pattern)
  9201. (error (display-completion-list list)))))
  9202. (message "Making completion list...%s" "done")))))))
  9203. ;;;; TODO, DEADLINE, Comments
  9204. (defun org-toggle-comment ()
  9205. "Change the COMMENT state of an entry."
  9206. (interactive)
  9207. (save-excursion
  9208. (org-back-to-heading)
  9209. (let (case-fold-search)
  9210. (if (looking-at (concat outline-regexp
  9211. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9212. (replace-match "" t t nil 1)
  9213. (if (looking-at outline-regexp)
  9214. (progn
  9215. (goto-char (match-end 0))
  9216. (insert org-comment-string " ")))))))
  9217. (defvar org-last-todo-state-is-todo nil
  9218. "This is non-nil when the last TODO state change led to a TODO state.
  9219. If the last change removed the TODO tag or switched to DONE, then
  9220. this is nil.")
  9221. (defvar org-setting-tags nil) ; dynamically skipped
  9222. (defun org-parse-local-options (string var)
  9223. "Parse STRING for startup setting relevant for variable VAR."
  9224. (let ((rtn (symbol-value var))
  9225. e opts)
  9226. (save-match-data
  9227. (if (or (not string) (not (string-match "\\S-" string)))
  9228. rtn
  9229. (setq opts (delq nil (mapcar (lambda (x)
  9230. (setq e (assoc x org-startup-options))
  9231. (if (eq (nth 1 e) var) e nil))
  9232. (org-split-string string "[ \t]+"))))
  9233. (if (not opts)
  9234. rtn
  9235. (setq rtn nil)
  9236. (while (setq e (pop opts))
  9237. (if (not (nth 3 e))
  9238. (setq rtn (nth 2 e))
  9239. (if (not (listp rtn)) (setq rtn nil))
  9240. (push (nth 2 e) rtn)))
  9241. rtn)))))
  9242. (defvar org-todo-setup-filter-hook nil
  9243. "Hook for functions that pre-filter todo specs.
  9244. Each function takes a todo spec and returns either `nil' or the spec
  9245. transformed into canonical form." )
  9246. (defvar org-todo-get-default-hook nil
  9247. "Hook for functions that get a default item for todo.
  9248. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9249. `nil' or a string to be used for the todo mark." )
  9250. (defvar org-agenda-headline-snapshot-before-repeat)
  9251. (defun org-todo (&optional arg)
  9252. "Change the TODO state of an item.
  9253. The state of an item is given by a keyword at the start of the heading,
  9254. like
  9255. *** TODO Write paper
  9256. *** DONE Call mom
  9257. The different keywords are specified in the variable `org-todo-keywords'.
  9258. By default the available states are \"TODO\" and \"DONE\".
  9259. So for this example: when the item starts with TODO, it is changed to DONE.
  9260. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9261. DONE are present, add TODO at the beginning of the heading.
  9262. With C-u prefix arg, use completion to determine the new state.
  9263. With numeric prefix arg, switch to that state.
  9264. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  9265. With a triple C-u prefix, circumvent any state blocking.
  9266. For calling through lisp, arg is also interpreted in the following way:
  9267. 'none -> empty state
  9268. \"\"(empty string) -> switch to empty state
  9269. 'done -> switch to DONE
  9270. 'nextset -> switch to the next set of keywords
  9271. 'previousset -> switch to the previous set of keywords
  9272. \"WAITING\" -> switch to the specified keyword, but only if it
  9273. really is a member of `org-todo-keywords'."
  9274. (interactive "P")
  9275. (if (equal arg '(16)) (setq arg 'nextset))
  9276. (let ((org-blocker-hook org-blocker-hook)
  9277. (case-fold-search nil))
  9278. (when (equal arg '(64))
  9279. (setq arg nil org-blocker-hook nil))
  9280. (when (and org-blocker-hook
  9281. (or org-inhibit-blocking
  9282. (org-entry-get nil "NOBLOCKING")))
  9283. (setq org-blocker-hook nil))
  9284. (save-excursion
  9285. (catch 'exit
  9286. (org-back-to-heading t)
  9287. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9288. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9289. (looking-at " *"))
  9290. (let* ((match-data (match-data))
  9291. (startpos (point-at-bol))
  9292. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  9293. (org-log-done org-log-done)
  9294. (org-log-repeat org-log-repeat)
  9295. (org-todo-log-states org-todo-log-states)
  9296. (this (match-string 1))
  9297. (hl-pos (match-beginning 0))
  9298. (head (org-get-todo-sequence-head this))
  9299. (ass (assoc head org-todo-kwd-alist))
  9300. (interpret (nth 1 ass))
  9301. (done-word (nth 3 ass))
  9302. (final-done-word (nth 4 ass))
  9303. (last-state (or this ""))
  9304. (completion-ignore-case t)
  9305. (member (member this org-todo-keywords-1))
  9306. (tail (cdr member))
  9307. (state (cond
  9308. ((and org-todo-key-trigger
  9309. (or (and (equal arg '(4))
  9310. (eq org-use-fast-todo-selection 'prefix))
  9311. (and (not arg) org-use-fast-todo-selection
  9312. (not (eq org-use-fast-todo-selection
  9313. 'prefix)))))
  9314. ;; Use fast selection
  9315. (org-fast-todo-selection))
  9316. ((and (equal arg '(4))
  9317. (or (not org-use-fast-todo-selection)
  9318. (not org-todo-key-trigger)))
  9319. ;; Read a state with completion
  9320. (org-icompleting-read
  9321. "State: " (mapcar (lambda(x) (list x))
  9322. org-todo-keywords-1)
  9323. nil t))
  9324. ((eq arg 'right)
  9325. (if this
  9326. (if tail (car tail) nil)
  9327. (car org-todo-keywords-1)))
  9328. ((eq arg 'left)
  9329. (if (equal member org-todo-keywords-1)
  9330. nil
  9331. (if this
  9332. (nth (- (length org-todo-keywords-1)
  9333. (length tail) 2)
  9334. org-todo-keywords-1)
  9335. (org-last org-todo-keywords-1))))
  9336. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9337. (setq arg nil))) ; hack to fall back to cycling
  9338. (arg
  9339. ;; user or caller requests a specific state
  9340. (cond
  9341. ((equal arg "") nil)
  9342. ((eq arg 'none) nil)
  9343. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9344. ((eq arg 'nextset)
  9345. (or (car (cdr (member head org-todo-heads)))
  9346. (car org-todo-heads)))
  9347. ((eq arg 'previousset)
  9348. (let ((org-todo-heads (reverse org-todo-heads)))
  9349. (or (car (cdr (member head org-todo-heads)))
  9350. (car org-todo-heads))))
  9351. ((car (member arg org-todo-keywords-1)))
  9352. ((stringp arg)
  9353. (error "State `%s' not valid in this file" arg))
  9354. ((nth (1- (prefix-numeric-value arg))
  9355. org-todo-keywords-1))))
  9356. ((null member) (or head (car org-todo-keywords-1)))
  9357. ((equal this final-done-word) nil) ;; -> make empty
  9358. ((null tail) nil) ;; -> first entry
  9359. ((memq interpret '(type priority))
  9360. (if (eq this-command last-command)
  9361. (car tail)
  9362. (if (> (length tail) 0)
  9363. (or done-word (car org-done-keywords))
  9364. nil)))
  9365. (t
  9366. (car tail))))
  9367. (state (or
  9368. (run-hook-with-args-until-success
  9369. 'org-todo-get-default-hook state last-state)
  9370. state))
  9371. (next (if state (concat " " state " ") " "))
  9372. (change-plist (list :type 'todo-state-change :from this :to state
  9373. :position startpos))
  9374. dolog now-done-p)
  9375. (when org-blocker-hook
  9376. (setq org-last-todo-state-is-todo
  9377. (not (member this org-done-keywords)))
  9378. (unless (save-excursion
  9379. (save-match-data
  9380. (run-hook-with-args-until-failure
  9381. 'org-blocker-hook change-plist)))
  9382. (if (interactive-p)
  9383. (error "TODO state change from %s to %s blocked" this state)
  9384. ;; fail silently
  9385. (message "TODO state change from %s to %s blocked" this state)
  9386. (throw 'exit nil))))
  9387. (store-match-data match-data)
  9388. (replace-match next t t)
  9389. (unless (pos-visible-in-window-p hl-pos)
  9390. (message "TODO state changed to %s" (org-trim next)))
  9391. (unless head
  9392. (setq head (org-get-todo-sequence-head state)
  9393. ass (assoc head org-todo-kwd-alist)
  9394. interpret (nth 1 ass)
  9395. done-word (nth 3 ass)
  9396. final-done-word (nth 4 ass)))
  9397. (when (memq arg '(nextset previousset))
  9398. (message "Keyword-Set %d/%d: %s"
  9399. (- (length org-todo-sets) -1
  9400. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9401. (length org-todo-sets)
  9402. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9403. (setq org-last-todo-state-is-todo
  9404. (not (member state org-done-keywords)))
  9405. (setq now-done-p (and (member state org-done-keywords)
  9406. (not (member this org-done-keywords))))
  9407. (and logging (org-local-logging logging))
  9408. (when (and (or org-todo-log-states org-log-done)
  9409. (not (eq org-inhibit-logging t))
  9410. (not (memq arg '(nextset previousset))))
  9411. ;; we need to look at recording a time and note
  9412. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  9413. (nth 2 (assoc this org-todo-log-states))))
  9414. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  9415. (setq dolog 'time))
  9416. (when (and state
  9417. (member state org-not-done-keywords)
  9418. (not (member this org-not-done-keywords)))
  9419. ;; This is now a todo state and was not one before
  9420. ;; If there was a CLOSED time stamp, get rid of it.
  9421. (org-add-planning-info nil nil 'closed))
  9422. (when (and now-done-p org-log-done)
  9423. ;; It is now done, and it was not done before
  9424. (org-add-planning-info 'closed (org-current-time))
  9425. (if (and (not dolog) (eq 'note org-log-done))
  9426. (org-add-log-setup 'done state this 'findpos 'note)))
  9427. (when (and state dolog)
  9428. ;; This is a non-nil state, and we need to log it
  9429. (org-add-log-setup 'state state this 'findpos dolog)))
  9430. ;; Fixup tag positioning
  9431. (org-todo-trigger-tag-changes state)
  9432. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  9433. (when org-provide-todo-statistics
  9434. (org-update-parent-todo-statistics))
  9435. (run-hooks 'org-after-todo-state-change-hook)
  9436. (if (and arg (not (member state org-done-keywords)))
  9437. (setq head (org-get-todo-sequence-head state)))
  9438. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  9439. ;; Do we need to trigger a repeat?
  9440. (when now-done-p
  9441. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  9442. ;; This is for the agenda, take a snapshot of the headline.
  9443. (save-match-data
  9444. (setq org-agenda-headline-snapshot-before-repeat
  9445. (org-get-heading))))
  9446. (org-auto-repeat-maybe state))
  9447. ;; Fixup cursor location if close to the keyword
  9448. (if (and (outline-on-heading-p)
  9449. (not (bolp))
  9450. (save-excursion (beginning-of-line 1)
  9451. (looking-at org-todo-line-regexp))
  9452. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  9453. (progn
  9454. (goto-char (or (match-end 2) (match-end 1)))
  9455. (and (looking-at " ") (just-one-space))))
  9456. (when org-trigger-hook
  9457. (save-excursion
  9458. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  9459. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  9460. "Block turning an entry into a TODO, using the hierarchy.
  9461. This checks whether the current task should be blocked from state
  9462. changes. Such blocking occurs when:
  9463. 1. The task has children which are not all in a completed state.
  9464. 2. A task has a parent with the property :ORDERED:, and there
  9465. are siblings prior to the current task with incomplete
  9466. status.
  9467. 3. The parent of the task is blocked because it has siblings that should
  9468. be done first, or is child of a block grandparent TODO entry."
  9469. (if (not org-enforce-todo-dependencies)
  9470. t ; if locally turned off don't block
  9471. (catch 'dont-block
  9472. ;; If this is not a todo state change, or if this entry is already DONE,
  9473. ;; do not block
  9474. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9475. (member (plist-get change-plist :from)
  9476. (cons 'done org-done-keywords))
  9477. (member (plist-get change-plist :to)
  9478. (cons 'todo org-not-done-keywords))
  9479. (not (plist-get change-plist :to)))
  9480. (throw 'dont-block t))
  9481. ;; If this task has children, and any are undone, it's blocked
  9482. (save-excursion
  9483. (org-back-to-heading t)
  9484. (let ((this-level (funcall outline-level)))
  9485. (outline-next-heading)
  9486. (let ((child-level (funcall outline-level)))
  9487. (while (and (not (eobp))
  9488. (> child-level this-level))
  9489. ;; this todo has children, check whether they are all
  9490. ;; completed
  9491. (if (and (not (org-entry-is-done-p))
  9492. (org-entry-is-todo-p))
  9493. (throw 'dont-block nil))
  9494. (outline-next-heading)
  9495. (setq child-level (funcall outline-level))))))
  9496. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  9497. ;; any previous siblings are undone, it's blocked
  9498. (save-excursion
  9499. (org-back-to-heading t)
  9500. (let* ((pos (point))
  9501. (parent-pos (and (org-up-heading-safe) (point))))
  9502. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9503. (when (and (org-entry-get (point) "ORDERED")
  9504. (forward-line 1)
  9505. (re-search-forward org-not-done-heading-regexp pos t))
  9506. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  9507. ;; Search further up the hierarchy, to see if an anchestor is blocked
  9508. (while t
  9509. (goto-char parent-pos)
  9510. (if (not (looking-at org-not-done-heading-regexp))
  9511. (throw 'dont-block t)) ; do not block, parent is not a TODO
  9512. (setq pos (point))
  9513. (setq parent-pos (and (org-up-heading-safe) (point)))
  9514. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9515. (when (and (org-entry-get (point) "ORDERED")
  9516. (forward-line 1)
  9517. (re-search-forward org-not-done-heading-regexp pos t))
  9518. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  9519. (defcustom org-track-ordered-property-with-tag nil
  9520. "Should the ORDERED property also be shown as a tag?
  9521. The ORDERED property decides if an entry should require subtasks to be
  9522. completed in sequence. Since a property is not very visible, setting
  9523. this option means that toggling the ORDERED property with the command
  9524. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  9525. not relevant for the behavior, but it makes things more visible.
  9526. Note that toggling the tag with tags commands will not change the property
  9527. and therefore not influence behavior!
  9528. This can be t, meaning the tag ORDERED should be used, It can also be a
  9529. string to select a different tag for this task."
  9530. :group 'org-todo
  9531. :type '(choice
  9532. (const :tag "No tracking" nil)
  9533. (const :tag "Track with ORDERED tag" t)
  9534. (string :tag "Use other tag")))
  9535. (defun org-toggle-ordered-property ()
  9536. "Toggle the ORDERED property of the current entry.
  9537. For better visibility, you can track the value of this property with a tag.
  9538. See variable `org-track-ordered-property-with-tag'."
  9539. (interactive)
  9540. (let* ((t1 org-track-ordered-property-with-tag)
  9541. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  9542. (save-excursion
  9543. (org-back-to-heading)
  9544. (if (org-entry-get nil "ORDERED")
  9545. (progn
  9546. (org-delete-property "ORDERED")
  9547. (and tag (org-toggle-tag tag 'off))
  9548. (message "Subtasks can be completed in arbitrary order"))
  9549. (org-entry-put nil "ORDERED" "t")
  9550. (and tag (org-toggle-tag tag 'on))
  9551. (message "Subtasks must be completed in sequence")))))
  9552. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  9553. (defun org-block-todo-from-checkboxes (change-plist)
  9554. "Block turning an entry into a TODO, using checkboxes.
  9555. This checks whether the current task should be blocked from state
  9556. changes because there are unchecked boxes in this entry."
  9557. (if (not org-enforce-todo-checkbox-dependencies)
  9558. t ; if locally turned off don't block
  9559. (catch 'dont-block
  9560. ;; If this is not a todo state change, or if this entry is already DONE,
  9561. ;; do not block
  9562. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9563. (member (plist-get change-plist :from)
  9564. (cons 'done org-done-keywords))
  9565. (member (plist-get change-plist :to)
  9566. (cons 'todo org-not-done-keywords))
  9567. (not (plist-get change-plist :to)))
  9568. (throw 'dont-block t))
  9569. ;; If this task has checkboxes that are not checked, it's blocked
  9570. (save-excursion
  9571. (org-back-to-heading t)
  9572. (let ((beg (point)) end)
  9573. (outline-next-heading)
  9574. (setq end (point))
  9575. (goto-char beg)
  9576. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  9577. end t)
  9578. (progn
  9579. (if (boundp 'org-blocked-by-checkboxes)
  9580. (setq org-blocked-by-checkboxes t))
  9581. (throw 'dont-block nil)))))
  9582. t))) ; do not block
  9583. (defun org-entry-blocked-p ()
  9584. "Is the current entry blocked?"
  9585. (if (org-entry-get nil "NOBLOCKING")
  9586. nil ;; Never block this entry
  9587. (not
  9588. (run-hook-with-args-until-failure
  9589. 'org-blocker-hook
  9590. (list :type 'todo-state-change
  9591. :position (point)
  9592. :from 'todo
  9593. :to 'done)))))
  9594. (defun org-update-statistics-cookies (all)
  9595. "Update the statistics cookie, either from TODO or from checkboxes.
  9596. This should be called with the cursor in a line with a statistics cookie."
  9597. (interactive "P")
  9598. (if all
  9599. (progn
  9600. (org-update-checkbox-count 'all)
  9601. (org-map-entries 'org-update-parent-todo-statistics))
  9602. (if (not (org-on-heading-p))
  9603. (org-update-checkbox-count)
  9604. (let ((pos (move-marker (make-marker) (point)))
  9605. end l1 l2)
  9606. (ignore-errors (org-back-to-heading t))
  9607. (if (not (org-on-heading-p))
  9608. (org-update-checkbox-count)
  9609. (setq l1 (org-outline-level))
  9610. (setq end (save-excursion
  9611. (outline-next-heading)
  9612. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  9613. (point)))
  9614. (if (and (save-excursion
  9615. (re-search-forward
  9616. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  9617. (not (save-excursion (re-search-forward
  9618. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  9619. (org-update-checkbox-count)
  9620. (if (and l2 (> l2 l1))
  9621. (progn
  9622. (goto-char end)
  9623. (org-update-parent-todo-statistics))
  9624. (goto-char pos)
  9625. (beginning-of-line 1)
  9626. (while (re-search-forward
  9627. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  9628. (point-at-eol) t)
  9629. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  9630. (goto-char pos)
  9631. (move-marker pos nil)))))
  9632. (defvar org-entry-property-inherited-from) ;; defined below
  9633. (defun org-update-parent-todo-statistics ()
  9634. "Update any statistics cookie in the parent of the current headline.
  9635. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  9636. the entire subtree and this will travel up the hierarchy and update
  9637. statistics everywhere."
  9638. (interactive)
  9639. (let* ((lim 0) prop
  9640. (recursive (or (not org-hierarchical-todo-statistics)
  9641. (string-match
  9642. "\\<recursive\\>"
  9643. (or (setq prop (org-entry-get
  9644. nil "COOKIE_DATA" 'inherit)) ""))))
  9645. (lim (or (and prop (marker-position
  9646. org-entry-property-inherited-from))
  9647. lim))
  9648. (first t)
  9649. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  9650. level ltoggle l1 new ndel
  9651. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  9652. (catch 'exit
  9653. (save-excursion
  9654. (beginning-of-line 1)
  9655. (if (org-at-heading-p)
  9656. (setq ltoggle (funcall outline-level))
  9657. (error "This should not happen"))
  9658. (while (and (setq level (org-up-heading-safe))
  9659. (or recursive first)
  9660. (>= (point) lim))
  9661. (setq first nil cookie-present nil)
  9662. (unless (and level
  9663. (not (string-match
  9664. "\\<checkbox\\>"
  9665. (downcase
  9666. (or (org-entry-get
  9667. nil "COOKIE_DATA")
  9668. "")))))
  9669. (throw 'exit nil))
  9670. (while (re-search-forward box-re (point-at-eol) t)
  9671. (setq cnt-all 0 cnt-done 0 cookie-present t)
  9672. (setq is-percent (match-end 2))
  9673. (save-match-data
  9674. (unless (outline-next-heading) (throw 'exit nil))
  9675. (while (and (looking-at org-complex-heading-regexp)
  9676. (> (setq l1 (length (match-string 1))) level))
  9677. (setq kwd (and (or recursive (= l1 ltoggle))
  9678. (match-string 2)))
  9679. (if (or (eq org-provide-todo-statistics 'all-headlines)
  9680. (and (listp org-provide-todo-statistics)
  9681. (or (member kwd org-provide-todo-statistics)
  9682. (member kwd org-done-keywords))))
  9683. (setq cnt-all (1+ cnt-all))
  9684. (if (eq org-provide-todo-statistics t)
  9685. (and kwd (setq cnt-all (1+ cnt-all)))))
  9686. (and (member kwd org-done-keywords)
  9687. (setq cnt-done (1+ cnt-done)))
  9688. (outline-next-heading)))
  9689. (setq new
  9690. (if is-percent
  9691. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  9692. (format "[%d/%d]" cnt-done cnt-all))
  9693. ndel (- (match-end 0) (match-beginning 0)))
  9694. (goto-char (match-beginning 0))
  9695. (insert new)
  9696. (delete-region (point) (+ (point) ndel)))
  9697. (when cookie-present
  9698. (run-hook-with-args 'org-after-todo-statistics-hook
  9699. cnt-done (- cnt-all cnt-done))))))
  9700. (run-hooks 'org-todo-statistics-hook)))
  9701. (defvar org-after-todo-statistics-hook nil
  9702. "Hook that is called after a TODO statistics cookie has been updated.
  9703. Each function is called with two arguments: the number of not-done entries
  9704. and the number of done entries.
  9705. For example, the following function, when added to this hook, will switch
  9706. an entry to DONE when all children are done, and back to TODO when new
  9707. entries are set to a TODO status. Note that this hook is only called
  9708. when there is a statistics cookie in the headline!
  9709. (defun org-summary-todo (n-done n-not-done)
  9710. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  9711. (let (org-log-done org-log-states) ; turn off logging
  9712. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  9713. ")
  9714. (defvar org-todo-statistics-hook nil
  9715. "Hook that is run whenever Org thinks TODO statistics should be updated.
  9716. This hook runs even if there is no statistics cookie present, in which case
  9717. `org-after-todo-statistics-hook' would not run.")
  9718. (defun org-todo-trigger-tag-changes (state)
  9719. "Apply the changes defined in `org-todo-state-tags-triggers'."
  9720. (let ((l org-todo-state-tags-triggers)
  9721. changes)
  9722. (when (or (not state) (equal state ""))
  9723. (setq changes (append changes (cdr (assoc "" l)))))
  9724. (when (and (stringp state) (> (length state) 0))
  9725. (setq changes (append changes (cdr (assoc state l)))))
  9726. (when (member state org-not-done-keywords)
  9727. (setq changes (append changes (cdr (assoc 'todo l)))))
  9728. (when (member state org-done-keywords)
  9729. (setq changes (append changes (cdr (assoc 'done l)))))
  9730. (dolist (c changes)
  9731. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  9732. (defun org-local-logging (value)
  9733. "Get logging settings from a property VALUE."
  9734. (let* (words w a)
  9735. ;; directly set the variables, they are already local.
  9736. (setq org-log-done nil
  9737. org-log-repeat nil
  9738. org-todo-log-states nil)
  9739. (setq words (org-split-string value))
  9740. (while (setq w (pop words))
  9741. (cond
  9742. ((setq a (assoc w org-startup-options))
  9743. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  9744. (set (nth 1 a) (nth 2 a))))
  9745. ((setq a (org-extract-log-state-settings w))
  9746. (and (member (car a) org-todo-keywords-1)
  9747. (push a org-todo-log-states)))))))
  9748. (defun org-get-todo-sequence-head (kwd)
  9749. "Return the head of the TODO sequence to which KWD belongs.
  9750. If KWD is not set, check if there is a text property remembering the
  9751. right sequence."
  9752. (let (p)
  9753. (cond
  9754. ((not kwd)
  9755. (or (get-text-property (point-at-bol) 'org-todo-head)
  9756. (progn
  9757. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  9758. nil (point-at-eol)))
  9759. (get-text-property p 'org-todo-head))))
  9760. ((not (member kwd org-todo-keywords-1))
  9761. (car org-todo-keywords-1))
  9762. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  9763. (defun org-fast-todo-selection ()
  9764. "Fast TODO keyword selection with single keys.
  9765. Returns the new TODO keyword, or nil if no state change should occur."
  9766. (let* ((fulltable org-todo-key-alist)
  9767. (done-keywords org-done-keywords) ;; needed for the faces.
  9768. (maxlen (apply 'max (mapcar
  9769. (lambda (x)
  9770. (if (stringp (car x)) (string-width (car x)) 0))
  9771. fulltable)))
  9772. (expert nil)
  9773. (fwidth (+ maxlen 3 1 3))
  9774. (ncol (/ (- (window-width) 4) fwidth))
  9775. tg cnt e c tbl
  9776. groups ingroup)
  9777. (save-excursion
  9778. (save-window-excursion
  9779. (if expert
  9780. (set-buffer (get-buffer-create " *Org todo*"))
  9781. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  9782. (erase-buffer)
  9783. (org-set-local 'org-done-keywords done-keywords)
  9784. (setq tbl fulltable cnt 0)
  9785. (while (setq e (pop tbl))
  9786. (cond
  9787. ((equal e '(:startgroup))
  9788. (push '() groups) (setq ingroup t)
  9789. (when (not (= cnt 0))
  9790. (setq cnt 0)
  9791. (insert "\n"))
  9792. (insert "{ "))
  9793. ((equal e '(:endgroup))
  9794. (setq ingroup nil cnt 0)
  9795. (insert "}\n"))
  9796. ((equal e '(:newline))
  9797. (when (not (= cnt 0))
  9798. (setq cnt 0)
  9799. (insert "\n")
  9800. (setq e (car tbl))
  9801. (while (equal (car tbl) '(:newline))
  9802. (insert "\n")
  9803. (setq tbl (cdr tbl)))))
  9804. (t
  9805. (setq tg (car e) c (cdr e))
  9806. (if ingroup (push tg (car groups)))
  9807. (setq tg (org-add-props tg nil 'face
  9808. (org-get-todo-face tg)))
  9809. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9810. (insert "[" c "] " tg (make-string
  9811. (- fwidth 4 (length tg)) ?\ ))
  9812. (when (= (setq cnt (1+ cnt)) ncol)
  9813. (insert "\n")
  9814. (if ingroup (insert " "))
  9815. (setq cnt 0)))))
  9816. (insert "\n")
  9817. (goto-char (point-min))
  9818. (if (not expert) (org-fit-window-to-buffer))
  9819. (message "[a-z..]:Set [SPC]:clear")
  9820. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9821. (cond
  9822. ((or (= c ?\C-g)
  9823. (and (= c ?q) (not (rassoc c fulltable))))
  9824. (setq quit-flag t))
  9825. ((= c ?\ ) nil)
  9826. ((setq e (rassoc c fulltable) tg (car e))
  9827. tg)
  9828. (t (setq quit-flag t)))))))
  9829. (defun org-entry-is-todo-p ()
  9830. (member (org-get-todo-state) org-not-done-keywords))
  9831. (defun org-entry-is-done-p ()
  9832. (member (org-get-todo-state) org-done-keywords))
  9833. (defun org-get-todo-state ()
  9834. (save-excursion
  9835. (org-back-to-heading t)
  9836. (and (looking-at org-todo-line-regexp)
  9837. (match-end 2)
  9838. (match-string 2))))
  9839. (defun org-at-date-range-p (&optional inactive-ok)
  9840. "Is the cursor inside a date range?"
  9841. (interactive)
  9842. (save-excursion
  9843. (catch 'exit
  9844. (let ((pos (point)))
  9845. (skip-chars-backward "^[<\r\n")
  9846. (skip-chars-backward "<[")
  9847. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9848. (>= (match-end 0) pos)
  9849. (throw 'exit t))
  9850. (skip-chars-backward "^<[\r\n")
  9851. (skip-chars-backward "<[")
  9852. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9853. (>= (match-end 0) pos)
  9854. (throw 'exit t)))
  9855. nil)))
  9856. (defun org-get-repeat (&optional tagline)
  9857. "Check if there is a deadline/schedule with repeater in this entry."
  9858. (save-match-data
  9859. (save-excursion
  9860. (org-back-to-heading t)
  9861. (and (re-search-forward (if tagline
  9862. (concat tagline "\\s-*" org-repeat-re)
  9863. org-repeat-re)
  9864. (org-entry-end-position) t)
  9865. (match-string-no-properties 1)))))
  9866. (defvar org-last-changed-timestamp)
  9867. (defvar org-last-inserted-timestamp)
  9868. (defvar org-log-post-message)
  9869. (defvar org-log-note-purpose)
  9870. (defvar org-log-note-how)
  9871. (defvar org-log-note-extra)
  9872. (defun org-auto-repeat-maybe (done-word)
  9873. "Check if the current headline contains a repeated deadline/schedule.
  9874. If yes, set TODO state back to what it was and change the base date
  9875. of repeating deadline/scheduled time stamps to new date.
  9876. This function is run automatically after each state change to a DONE state."
  9877. ;; last-state is dynamically scoped into this function
  9878. (let* ((repeat (org-get-repeat))
  9879. (aa (assoc last-state org-todo-kwd-alist))
  9880. (interpret (nth 1 aa))
  9881. (head (nth 2 aa))
  9882. (whata '(("d" . day) ("m" . month) ("y" . year)))
  9883. (msg "Entry repeats: ")
  9884. (org-log-done nil)
  9885. (org-todo-log-states nil)
  9886. (nshiftmax 10) (nshift 0)
  9887. re type n what ts time to-state)
  9888. (when repeat
  9889. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  9890. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  9891. org-todo-repeat-to-state))
  9892. (unless (and to-state (member to-state org-todo-keywords-1))
  9893. (setq to-state (if (eq interpret 'type) last-state head)))
  9894. (org-todo to-state)
  9895. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  9896. (org-entry-put nil "LAST_REPEAT" (format-time-string
  9897. (org-time-stamp-format t t))))
  9898. (when org-log-repeat
  9899. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9900. (memq 'org-add-log-note post-command-hook))
  9901. ;; OK, we are already setup for some record
  9902. (if (eq org-log-repeat 'note)
  9903. ;; make sure we take a note, not only a time stamp
  9904. (setq org-log-note-how 'note))
  9905. ;; Set up for taking a record
  9906. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  9907. last-state
  9908. 'findpos org-log-repeat)))
  9909. (org-back-to-heading t)
  9910. (org-add-planning-info nil nil 'closed)
  9911. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  9912. org-deadline-time-regexp "\\)\\|\\("
  9913. org-ts-regexp "\\)"))
  9914. (while (re-search-forward
  9915. re (save-excursion (outline-next-heading) (point)) t)
  9916. (setq type (if (match-end 1) org-scheduled-string
  9917. (if (match-end 3) org-deadline-string "Plain:"))
  9918. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9919. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9920. (setq n (string-to-number (match-string 2 ts))
  9921. what (match-string 3 ts))
  9922. (if (equal what "w") (setq n (* n 7) what "d"))
  9923. ;; Preparation, see if we need to modify the start date for the change
  9924. (when (match-end 1)
  9925. (setq time (save-match-data (org-time-string-to-time ts)))
  9926. (cond
  9927. ((equal (match-string 1 ts) ".")
  9928. ;; Shift starting date to today
  9929. (org-timestamp-change
  9930. (- (time-to-days (current-time)) (time-to-days time))
  9931. 'day))
  9932. ((equal (match-string 1 ts) "+")
  9933. (while (or (= nshift 0)
  9934. (<= (time-to-days time) (time-to-days (current-time))))
  9935. (when (= (incf nshift) nshiftmax)
  9936. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9937. (error "Abort")))
  9938. (org-timestamp-change n (cdr (assoc what whata)))
  9939. (org-at-timestamp-p t)
  9940. (setq ts (match-string 1))
  9941. (setq time (save-match-data (org-time-string-to-time ts))))
  9942. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9943. ;; rematch, so that we have everything in place for the real shift
  9944. (org-at-timestamp-p t)
  9945. (setq ts (match-string 1))
  9946. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9947. (org-timestamp-change n (cdr (assoc what whata)))
  9948. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9949. (setq org-log-post-message msg)
  9950. (message "%s" msg))))
  9951. (defun org-show-todo-tree (arg)
  9952. "Make a compact tree which shows all headlines marked with TODO.
  9953. The tree will show the lines where the regexp matches, and all higher
  9954. headlines above the match.
  9955. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9956. With a numeric prefix N, construct a sparse tree for the Nth element
  9957. of `org-todo-keywords-1'."
  9958. (interactive "P")
  9959. (let ((case-fold-search nil)
  9960. (kwd-re
  9961. (cond ((null arg) org-not-done-regexp)
  9962. ((equal arg '(4))
  9963. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  9964. (mapcar 'list org-todo-keywords-1))))
  9965. (concat "\\("
  9966. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9967. "\\)\\>")))
  9968. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9969. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9970. org-todo-keywords-1)))
  9971. (t (error "Invalid prefix argument: %s" arg)))))
  9972. (message "%d TODO entries found"
  9973. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9974. (defun org-deadline (&optional remove time)
  9975. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9976. With argument REMOVE, remove any deadline from the item.
  9977. When TIME is set, it should be an internal time specification, and the
  9978. scheduling will use the corresponding date."
  9979. (interactive "P")
  9980. (let* ((old-date (org-entry-get nil "DEADLINE"))
  9981. (repeater (and old-date
  9982. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  9983. (match-string 1 old-date))))
  9984. (if remove
  9985. (progn
  9986. (when (and old-date org-log-redeadline)
  9987. (org-add-log-setup 'deldeadline nil old-date 'findpos
  9988. org-log-redeadline))
  9989. (org-remove-timestamp-with-keyword org-deadline-string)
  9990. (message "Item no longer has a deadline."))
  9991. (org-add-planning-info 'deadline time 'closed)
  9992. (when (and old-date org-log-redeadline
  9993. (not (equal old-date
  9994. (substring org-last-inserted-timestamp 1 -1))))
  9995. (org-add-log-setup 'redeadline nil old-date 'findpos
  9996. org-log-redeadline))
  9997. (when repeater
  9998. (save-excursion
  9999. (org-back-to-heading t)
  10000. (when (re-search-forward (concat org-deadline-string " "
  10001. org-last-inserted-timestamp)
  10002. (save-excursion
  10003. (outline-next-heading) (point)) t)
  10004. (goto-char (1- (match-end 0)))
  10005. (insert " " repeater)
  10006. (setq org-last-inserted-timestamp
  10007. (concat (substring org-last-inserted-timestamp 0 -1)
  10008. " " repeater
  10009. (substring org-last-inserted-timestamp -1))))))
  10010. (message "Deadline on %s" org-last-inserted-timestamp))))
  10011. (defun org-schedule (&optional remove time)
  10012. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10013. With argument REMOVE, remove any scheduling date from the item.
  10014. When TIME is set, it should be an internal time specification, and the
  10015. scheduling will use the corresponding date."
  10016. (interactive "P")
  10017. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10018. (repeater (and old-date
  10019. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  10020. (match-string 1 old-date))))
  10021. (if remove
  10022. (progn
  10023. (when (and old-date org-log-reschedule)
  10024. (org-add-log-setup 'delschedule nil old-date 'findpos
  10025. org-log-reschedule))
  10026. (org-remove-timestamp-with-keyword org-scheduled-string)
  10027. (message "Item is no longer scheduled."))
  10028. (org-add-planning-info 'scheduled time 'closed)
  10029. (when (and old-date org-log-reschedule
  10030. (not (equal old-date
  10031. (substring org-last-inserted-timestamp 1 -1))))
  10032. (org-add-log-setup 'reschedule nil old-date 'findpos
  10033. org-log-reschedule))
  10034. (when repeater
  10035. (save-excursion
  10036. (org-back-to-heading t)
  10037. (when (re-search-forward (concat org-scheduled-string " "
  10038. org-last-inserted-timestamp)
  10039. (save-excursion
  10040. (outline-next-heading) (point)) t)
  10041. (goto-char (1- (match-end 0)))
  10042. (insert " " repeater)
  10043. (setq org-last-inserted-timestamp
  10044. (concat (substring org-last-inserted-timestamp 0 -1)
  10045. " " repeater
  10046. (substring org-last-inserted-timestamp -1))))))
  10047. (message "Scheduled to %s" org-last-inserted-timestamp))))
  10048. (defun org-get-scheduled-time (pom &optional inherit)
  10049. "Get the scheduled time as a time tuple, of a format suitable
  10050. for calling org-schedule with, or if there is no scheduling,
  10051. returns nil."
  10052. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10053. (when time
  10054. (apply 'encode-time (org-parse-time-string time)))))
  10055. (defun org-get-deadline-time (pom &optional inherit)
  10056. "Get the deadine as a time tuple, of a format suitable for
  10057. calling org-deadline with, or if there is no scheduling, returns
  10058. nil."
  10059. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10060. (when time
  10061. (apply 'encode-time (org-parse-time-string time)))))
  10062. (defun org-remove-timestamp-with-keyword (keyword)
  10063. "Remove all time stamps with KEYWORD in the current entry."
  10064. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10065. beg)
  10066. (save-excursion
  10067. (org-back-to-heading t)
  10068. (setq beg (point))
  10069. (outline-next-heading)
  10070. (while (re-search-backward re beg t)
  10071. (replace-match "")
  10072. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10073. (equal (char-before) ?\ ))
  10074. (backward-delete-char 1)
  10075. (if (string-match "^[ \t]*$" (buffer-substring
  10076. (point-at-bol) (point-at-eol)))
  10077. (delete-region (point-at-bol)
  10078. (min (point-max) (1+ (point-at-eol))))))))))
  10079. (defun org-add-planning-info (what &optional time &rest remove)
  10080. "Insert new timestamp with keyword in the line directly after the headline.
  10081. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  10082. If non is given, the user is prompted for a date.
  10083. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10084. be removed."
  10085. (interactive)
  10086. (let (org-time-was-given org-end-time-was-given ts
  10087. end default-time default-input)
  10088. (catch 'exit
  10089. (when (and (not time) (memq what '(scheduled deadline)))
  10090. ;; Try to get a default date/time from existing timestamp
  10091. (save-excursion
  10092. (org-back-to-heading t)
  10093. (setq end (save-excursion (outline-next-heading) (point)))
  10094. (when (re-search-forward (if (eq what 'scheduled)
  10095. org-scheduled-time-regexp
  10096. org-deadline-time-regexp)
  10097. end t)
  10098. (setq ts (match-string 1)
  10099. default-time
  10100. (apply 'encode-time (org-parse-time-string ts))
  10101. default-input (and ts (org-get-compact-tod ts))))))
  10102. (when what
  10103. ;; If necessary, get the time from the user
  10104. (setq time (or time (org-read-date nil 'to-time nil nil
  10105. default-time default-input))))
  10106. (when (and org-insert-labeled-timestamps-at-point
  10107. (member what '(scheduled deadline)))
  10108. (insert
  10109. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10110. (org-insert-time-stamp time org-time-was-given
  10111. nil nil nil (list org-end-time-was-given))
  10112. (setq what nil))
  10113. (save-excursion
  10114. (save-restriction
  10115. (let (col list elt ts buffer-invisibility-spec)
  10116. (org-back-to-heading t)
  10117. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  10118. (goto-char (match-end 1))
  10119. (setq col (current-column))
  10120. (goto-char (match-end 0))
  10121. (if (eobp) (insert "\n") (forward-char 1))
  10122. (when (and (not what)
  10123. (not (looking-at
  10124. (concat "[ \t]*"
  10125. org-keyword-time-not-clock-regexp))))
  10126. ;; Nothing to add, nothing to remove...... :-)
  10127. (throw 'exit nil))
  10128. (if (and (not (looking-at outline-regexp))
  10129. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10130. "[^\r\n]*"))
  10131. (not (equal (match-string 1) org-clock-string)))
  10132. (narrow-to-region (match-beginning 0) (match-end 0))
  10133. (insert-before-markers "\n")
  10134. (backward-char 1)
  10135. (narrow-to-region (point) (point))
  10136. (and org-adapt-indentation (org-indent-to-column col)))
  10137. ;; Check if we have to remove something.
  10138. (setq list (cons what remove))
  10139. (while list
  10140. (setq elt (pop list))
  10141. (goto-char (point-min))
  10142. (when (or (and (eq elt 'scheduled)
  10143. (re-search-forward org-scheduled-time-regexp nil t))
  10144. (and (eq elt 'deadline)
  10145. (re-search-forward org-deadline-time-regexp nil t))
  10146. (and (eq elt 'closed)
  10147. (re-search-forward org-closed-time-regexp nil t)))
  10148. (replace-match "")
  10149. (if (looking-at "--+<[^>]+>") (replace-match ""))
  10150. (skip-chars-backward " ")
  10151. (if (looking-at " +") (replace-match ""))))
  10152. (goto-char (point-max))
  10153. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10154. (when what
  10155. (insert
  10156. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10157. (cond ((eq what 'scheduled) org-scheduled-string)
  10158. ((eq what 'deadline) org-deadline-string)
  10159. ((eq what 'closed) org-closed-string))
  10160. " ")
  10161. (setq ts (org-insert-time-stamp
  10162. time
  10163. (or org-time-was-given
  10164. (and (eq what 'closed) org-log-done-with-time))
  10165. (eq what 'closed)
  10166. nil nil (list org-end-time-was-given)))
  10167. (end-of-line 1))
  10168. (goto-char (point-min))
  10169. (widen)
  10170. (if (and (looking-at "[ \t]+\n")
  10171. (equal (char-before) ?\n))
  10172. (delete-region (1- (point)) (point-at-eol)))
  10173. ts))))))
  10174. (defvar org-log-note-marker (make-marker))
  10175. (defvar org-log-note-purpose nil)
  10176. (defvar org-log-note-state nil)
  10177. (defvar org-log-note-previous-state nil)
  10178. (defvar org-log-note-how nil)
  10179. (defvar org-log-note-extra nil)
  10180. (defvar org-log-note-window-configuration nil)
  10181. (defvar org-log-note-return-to (make-marker))
  10182. (defvar org-log-post-message nil
  10183. "Message to be displayed after a log note has been stored.
  10184. The auto-repeater uses this.")
  10185. (defun org-add-note ()
  10186. "Add a note to the current entry.
  10187. This is done in the same way as adding a state change note."
  10188. (interactive)
  10189. (org-add-log-setup 'note nil nil 'findpos nil))
  10190. (defvar org-property-end-re)
  10191. (defun org-add-log-setup (&optional purpose state prev-state
  10192. findpos how &optional extra)
  10193. "Set up the post command hook to take a note.
  10194. If this is about to TODO state change, the new state is expected in STATE.
  10195. When FINDPOS is non-nil, find the correct position for the note in
  10196. the current entry. If not, assume that it can be inserted at point.
  10197. HOW is an indicator what kind of note should be created.
  10198. EXTRA is additional text that will be inserted into the notes buffer."
  10199. (let* ((org-log-into-drawer (org-log-into-drawer))
  10200. (drawer (cond ((stringp org-log-into-drawer)
  10201. org-log-into-drawer)
  10202. (org-log-into-drawer "LOGBOOK")
  10203. (t nil))))
  10204. (save-restriction
  10205. (save-excursion
  10206. (when findpos
  10207. (org-back-to-heading t)
  10208. (narrow-to-region (point) (save-excursion
  10209. (outline-next-heading) (point)))
  10210. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  10211. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10212. "[^\r\n]*\\)?"))
  10213. (goto-char (match-end 0))
  10214. (cond
  10215. (drawer
  10216. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10217. nil t)
  10218. (progn
  10219. (goto-char (match-end 0))
  10220. (or org-log-states-order-reversed
  10221. (and (re-search-forward org-property-end-re nil t)
  10222. (goto-char (1- (match-beginning 0))))))
  10223. (insert "\n:" drawer ":\n:END:")
  10224. (beginning-of-line 0)
  10225. (org-indent-line-function)
  10226. (beginning-of-line 2)
  10227. (org-indent-line-function)
  10228. (end-of-line 0)))
  10229. ((and org-log-state-notes-insert-after-drawers
  10230. (save-excursion
  10231. (forward-line) (looking-at org-drawer-regexp)))
  10232. (forward-line)
  10233. (while (looking-at org-drawer-regexp)
  10234. (goto-char (match-end 0))
  10235. (re-search-forward org-property-end-re (point-max) t)
  10236. (forward-line))
  10237. (forward-line -1)))
  10238. (unless org-log-states-order-reversed
  10239. (and (= (char-after) ?\n) (forward-char 1))
  10240. (org-skip-over-state-notes)
  10241. (skip-chars-backward " \t\n\r")))
  10242. (move-marker org-log-note-marker (point))
  10243. (setq org-log-note-purpose purpose
  10244. org-log-note-state state
  10245. org-log-note-previous-state prev-state
  10246. org-log-note-how how
  10247. org-log-note-extra extra)
  10248. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10249. (defun org-skip-over-state-notes ()
  10250. "Skip past the list of State notes in an entry."
  10251. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10252. (while (looking-at "[ \t]*- State")
  10253. (condition-case nil
  10254. (org-next-item)
  10255. (error (org-end-of-item)))))
  10256. (defun org-add-log-note (&optional purpose)
  10257. "Pop up a window for taking a note, and add this note later at point."
  10258. (remove-hook 'post-command-hook 'org-add-log-note)
  10259. (setq org-log-note-window-configuration (current-window-configuration))
  10260. (delete-other-windows)
  10261. (move-marker org-log-note-return-to (point))
  10262. (switch-to-buffer (marker-buffer org-log-note-marker))
  10263. (goto-char org-log-note-marker)
  10264. (org-switch-to-buffer-other-window "*Org Note*")
  10265. (erase-buffer)
  10266. (if (memq org-log-note-how '(time state))
  10267. (let (current-prefix-arg) (org-store-log-note))
  10268. (let ((org-inhibit-startup t)) (org-mode))
  10269. (insert (format "# Insert note for %s.
  10270. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  10271. (cond
  10272. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  10273. ((eq org-log-note-purpose 'done) "closed todo item")
  10274. ((eq org-log-note-purpose 'state)
  10275. (format "state change from \"%s\" to \"%s\""
  10276. (or org-log-note-previous-state "")
  10277. (or org-log-note-state "")))
  10278. ((eq org-log-note-purpose 'reschedule)
  10279. "rescheduling")
  10280. ((eq org-log-note-purpose 'delschedule)
  10281. "no longer scheduled")
  10282. ((eq org-log-note-purpose 'redeadline)
  10283. "changing deadline")
  10284. ((eq org-log-note-purpose 'deldeadline)
  10285. "removing deadline")
  10286. ((eq org-log-note-purpose 'refile)
  10287. "refiling")
  10288. ((eq org-log-note-purpose 'note)
  10289. "this entry")
  10290. (t (error "This should not happen")))))
  10291. (if org-log-note-extra (insert org-log-note-extra))
  10292. (org-set-local 'org-finish-function 'org-store-log-note)))
  10293. (defvar org-note-abort nil) ; dynamically scoped
  10294. (defun org-store-log-note ()
  10295. "Finish taking a log note, and insert it to where it belongs."
  10296. (let ((txt (buffer-string))
  10297. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  10298. lines ind)
  10299. (kill-buffer (current-buffer))
  10300. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  10301. (setq txt (replace-match "" t t txt)))
  10302. (if (string-match "\\s-+\\'" txt)
  10303. (setq txt (replace-match "" t t txt)))
  10304. (setq lines (org-split-string txt "\n"))
  10305. (when (and note (string-match "\\S-" note))
  10306. (setq note
  10307. (org-replace-escapes
  10308. note
  10309. (list (cons "%u" (user-login-name))
  10310. (cons "%U" user-full-name)
  10311. (cons "%t" (format-time-string
  10312. (org-time-stamp-format 'long 'inactive)
  10313. (current-time)))
  10314. (cons "%s" (if org-log-note-state
  10315. (concat "\"" org-log-note-state "\"")
  10316. ""))
  10317. (cons "%S" (if org-log-note-previous-state
  10318. (concat "\"" org-log-note-previous-state "\"")
  10319. "\"\"")))))
  10320. (if lines (setq note (concat note " \\\\")))
  10321. (push note lines))
  10322. (when (or current-prefix-arg org-note-abort)
  10323. (when org-log-into-drawer
  10324. (org-remove-empty-drawer-at
  10325. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10326. org-log-note-marker))
  10327. (setq lines nil))
  10328. (when lines
  10329. (with-current-buffer (marker-buffer org-log-note-marker)
  10330. (save-excursion
  10331. (goto-char org-log-note-marker)
  10332. (move-marker org-log-note-marker nil)
  10333. (end-of-line 1)
  10334. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10335. (insert "- " (pop lines))
  10336. (org-indent-line-function)
  10337. (beginning-of-line 1)
  10338. (looking-at "[ \t]*")
  10339. (setq ind (concat (match-string 0) " "))
  10340. (end-of-line 1)
  10341. (while lines (insert "\n" ind (pop lines)))
  10342. (message "Note stored")
  10343. (org-back-to-heading t)
  10344. (org-cycle-hide-drawers 'children)))))
  10345. (set-window-configuration org-log-note-window-configuration)
  10346. (with-current-buffer (marker-buffer org-log-note-return-to)
  10347. (goto-char org-log-note-return-to))
  10348. (move-marker org-log-note-return-to nil)
  10349. (and org-log-post-message (message "%s" org-log-post-message)))
  10350. (defun org-remove-empty-drawer-at (drawer pos)
  10351. "Remove an empty drawer DRAWER at position POS.
  10352. POS may also be a marker."
  10353. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10354. (save-excursion
  10355. (save-restriction
  10356. (widen)
  10357. (goto-char pos)
  10358. (if (org-in-regexp
  10359. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10360. (replace-match ""))))))
  10361. (defun org-sparse-tree (&optional arg)
  10362. "Create a sparse tree, prompt for the details.
  10363. This command can create sparse trees. You first need to select the type
  10364. of match used to create the tree:
  10365. t Show entries with a specific TODO keyword.
  10366. m Show entries selected by a tags/property match.
  10367. p Enter a property name and its value (both with completion on existing
  10368. names/values) and show entries with that property.
  10369. / Show entries matching a regular expression (`r' can be used as well)
  10370. d Show deadlines due within `org-deadline-warning-days'.
  10371. b Show deadlines and scheduled items before a date.
  10372. a Show deadlines and scheduled items after a date."
  10373. (interactive "P")
  10374. (let (ans kwd value)
  10375. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  10376. (setq ans (read-char-exclusive))
  10377. (cond
  10378. ((equal ans ?d)
  10379. (call-interactively 'org-check-deadlines))
  10380. ((equal ans ?b)
  10381. (call-interactively 'org-check-before-date))
  10382. ((equal ans ?a)
  10383. (call-interactively 'org-check-after-date))
  10384. ((equal ans ?t)
  10385. (org-show-todo-tree '(4)))
  10386. ((member ans '(?T ?m))
  10387. (call-interactively 'org-match-sparse-tree))
  10388. ((member ans '(?p ?P))
  10389. (setq kwd (org-icompleting-read "Property: "
  10390. (mapcar 'list (org-buffer-property-keys))))
  10391. (setq value (org-icompleting-read "Value: "
  10392. (mapcar 'list (org-property-values kwd))))
  10393. (unless (string-match "\\`{.*}\\'" value)
  10394. (setq value (concat "\"" value "\"")))
  10395. (org-match-sparse-tree arg (concat kwd "=" value)))
  10396. ((member ans '(?r ?R ?/))
  10397. (call-interactively 'org-occur))
  10398. (t (error "No such sparse tree command \"%c\"" ans)))))
  10399. (defvar org-occur-highlights nil
  10400. "List of overlays used for occur matches.")
  10401. (make-variable-buffer-local 'org-occur-highlights)
  10402. (defvar org-occur-parameters nil
  10403. "Parameters of the active org-occur calls.
  10404. This is a list, each call to org-occur pushes as cons cell,
  10405. containing the regular expression and the callback, onto the list.
  10406. The list can contain several entries if `org-occur' has been called
  10407. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  10408. will only contain one set of parameters. When the highlights are
  10409. removed (for example with `C-c C-c', or with the next edit (depending
  10410. on `org-remove-highlights-with-change'), this variable is emptied
  10411. as well.")
  10412. (make-variable-buffer-local 'org-occur-parameters)
  10413. (defun org-occur (regexp &optional keep-previous callback)
  10414. "Make a compact tree which shows all matches of REGEXP.
  10415. The tree will show the lines where the regexp matches, and all higher
  10416. headlines above the match. It will also show the heading after the match,
  10417. to make sure editing the matching entry is easy.
  10418. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  10419. call to `org-occur' will be kept, to allow stacking of calls to this
  10420. command.
  10421. If CALLBACK is non-nil, it is a function which is called to confirm
  10422. that the match should indeed be shown."
  10423. (interactive "sRegexp: \nP")
  10424. (when (equal regexp "")
  10425. (error "Regexp cannot be empty"))
  10426. (unless keep-previous
  10427. (org-remove-occur-highlights nil nil t))
  10428. (push (cons regexp callback) org-occur-parameters)
  10429. (let ((cnt 0))
  10430. (save-excursion
  10431. (goto-char (point-min))
  10432. (if (or (not keep-previous) ; do not want to keep
  10433. (not org-occur-highlights)) ; no previous matches
  10434. ;; hide everything
  10435. (org-overview))
  10436. (while (re-search-forward regexp nil t)
  10437. (when (or (not callback)
  10438. (save-match-data (funcall callback)))
  10439. (setq cnt (1+ cnt))
  10440. (when org-highlight-sparse-tree-matches
  10441. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  10442. (org-show-context 'occur-tree))))
  10443. (when org-remove-highlights-with-change
  10444. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  10445. nil 'local))
  10446. (unless org-sparse-tree-open-archived-trees
  10447. (org-hide-archived-subtrees (point-min) (point-max)))
  10448. (run-hooks 'org-occur-hook)
  10449. (if (interactive-p)
  10450. (message "%d match(es) for regexp %s" cnt regexp))
  10451. cnt))
  10452. (defun org-show-context (&optional key)
  10453. "Make sure point and context and visible.
  10454. How much context is shown depends upon the variables
  10455. `org-show-hierarchy-above', `org-show-following-heading'. and
  10456. `org-show-siblings'."
  10457. (let ((heading-p (org-on-heading-p t))
  10458. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  10459. (following-p (org-get-alist-option org-show-following-heading key))
  10460. (entry-p (org-get-alist-option org-show-entry-below key))
  10461. (siblings-p (org-get-alist-option org-show-siblings key)))
  10462. (catch 'exit
  10463. ;; Show heading or entry text
  10464. (if (and heading-p (not entry-p))
  10465. (org-flag-heading nil) ; only show the heading
  10466. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  10467. (org-show-hidden-entry))) ; show entire entry
  10468. (when following-p
  10469. ;; Show next sibling, or heading below text
  10470. (save-excursion
  10471. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  10472. (org-flag-heading nil))))
  10473. (when siblings-p (org-show-siblings))
  10474. (when hierarchy-p
  10475. ;; show all higher headings, possibly with siblings
  10476. (save-excursion
  10477. (while (and (condition-case nil
  10478. (progn (org-up-heading-all 1) t)
  10479. (error nil))
  10480. (not (bobp)))
  10481. (org-flag-heading nil)
  10482. (when siblings-p (org-show-siblings))))))))
  10483. (defvar org-reveal-start-hook nil
  10484. "Hook run before revealing a location.")
  10485. (defun org-reveal (&optional siblings)
  10486. "Show current entry, hierarchy above it, and the following headline.
  10487. This can be used to show a consistent set of context around locations
  10488. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  10489. not t for the search context.
  10490. With optional argument SIBLINGS, on each level of the hierarchy all
  10491. siblings are shown. This repairs the tree structure to what it would
  10492. look like when opened with hierarchical calls to `org-cycle'.
  10493. With double optional argument `C-u C-u', go to the parent and show the
  10494. entire tree."
  10495. (interactive "P")
  10496. (run-hooks 'org-reveal-start-hook)
  10497. (let ((org-show-hierarchy-above t)
  10498. (org-show-following-heading t)
  10499. (org-show-siblings (if siblings t org-show-siblings)))
  10500. (org-show-context nil))
  10501. (when (equal siblings '(16))
  10502. (save-excursion
  10503. (when (org-up-heading-safe)
  10504. (org-show-subtree)
  10505. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  10506. (defun org-highlight-new-match (beg end)
  10507. "Highlight from BEG to END and mark the highlight is an occur headline."
  10508. (let ((ov (make-overlay beg end)))
  10509. (overlay-put ov 'face 'secondary-selection)
  10510. (push ov org-occur-highlights)))
  10511. (defun org-remove-occur-highlights (&optional beg end noremove)
  10512. "Remove the occur highlights from the buffer.
  10513. BEG and END are ignored. If NOREMOVE is nil, remove this function
  10514. from the `before-change-functions' in the current buffer."
  10515. (interactive)
  10516. (unless org-inhibit-highlight-removal
  10517. (mapc 'delete-overlay org-occur-highlights)
  10518. (setq org-occur-highlights nil)
  10519. (setq org-occur-parameters nil)
  10520. (unless noremove
  10521. (remove-hook 'before-change-functions
  10522. 'org-remove-occur-highlights 'local))))
  10523. ;;;; Priorities
  10524. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  10525. "Regular expression matching the priority indicator.")
  10526. (defvar org-remove-priority-next-time nil)
  10527. (defun org-priority-up ()
  10528. "Increase the priority of the current item."
  10529. (interactive)
  10530. (org-priority 'up))
  10531. (defun org-priority-down ()
  10532. "Decrease the priority of the current item."
  10533. (interactive)
  10534. (org-priority 'down))
  10535. (defun org-priority (&optional action)
  10536. "Change the priority of an item by ARG.
  10537. ACTION can be `set', `up', `down', or a character."
  10538. (interactive)
  10539. (unless org-enable-priority-commands
  10540. (error "Priority commands are disabled"))
  10541. (setq action (or action 'set))
  10542. (let (current new news have remove)
  10543. (save-excursion
  10544. (org-back-to-heading t)
  10545. (if (looking-at org-priority-regexp)
  10546. (setq current (string-to-char (match-string 2))
  10547. have t)
  10548. (setq current org-default-priority))
  10549. (cond
  10550. ((eq action 'remove)
  10551. (setq remove t new ?\ ))
  10552. ((or (eq action 'set)
  10553. (if (featurep 'xemacs) (characterp action) (integerp action)))
  10554. (if (not (eq action 'set))
  10555. (setq new action)
  10556. (message "Priority %c-%c, SPC to remove: "
  10557. org-highest-priority org-lowest-priority)
  10558. (setq new (read-char-exclusive)))
  10559. (if (and (= (upcase org-highest-priority) org-highest-priority)
  10560. (= (upcase org-lowest-priority) org-lowest-priority))
  10561. (setq new (upcase new)))
  10562. (cond ((equal new ?\ ) (setq remove t))
  10563. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  10564. (error "Priority must be between `%c' and `%c'"
  10565. org-highest-priority org-lowest-priority))))
  10566. ((eq action 'up)
  10567. (if (and (not have) (eq last-command this-command))
  10568. (setq new org-lowest-priority)
  10569. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10570. org-default-priority (1- current)))))
  10571. ((eq action 'down)
  10572. (if (and (not have) (eq last-command this-command))
  10573. (setq new org-highest-priority)
  10574. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10575. org-default-priority (1+ current)))))
  10576. (t (error "Invalid action")))
  10577. (if (or (< (upcase new) org-highest-priority)
  10578. (> (upcase new) org-lowest-priority))
  10579. (setq remove t))
  10580. (setq news (format "%c" new))
  10581. (if have
  10582. (if remove
  10583. (replace-match "" t t nil 1)
  10584. (replace-match news t t nil 2))
  10585. (if remove
  10586. (error "No priority cookie found in line")
  10587. (let ((case-fold-search nil))
  10588. (looking-at org-todo-line-regexp))
  10589. (if (match-end 2)
  10590. (progn
  10591. (goto-char (match-end 2))
  10592. (insert " [#" news "]"))
  10593. (goto-char (match-beginning 3))
  10594. (insert "[#" news "] "))))
  10595. (org-preserve-lc (org-set-tags nil 'align)))
  10596. (if remove
  10597. (message "Priority removed")
  10598. (message "Priority of current item set to %s" news))))
  10599. (defun org-get-priority (s)
  10600. "Find priority cookie and return priority."
  10601. (save-match-data
  10602. (if (not (string-match org-priority-regexp s))
  10603. (* 1000 (- org-lowest-priority org-default-priority))
  10604. (* 1000 (- org-lowest-priority
  10605. (string-to-char (match-string 2 s)))))))
  10606. ;;;; Tags
  10607. (defvar org-agenda-archives-mode)
  10608. (defvar org-map-continue-from nil
  10609. "Position from where mapping should continue.
  10610. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  10611. (defvar org-scanner-tags nil
  10612. "The current tag list while the tags scanner is running.")
  10613. (defvar org-trust-scanner-tags nil
  10614. "Should `org-get-tags-at' use the tags fro the scanner.
  10615. This is for internal dynamical scoping only.
  10616. When this is non-nil, the function `org-get-tags-at' will return the value
  10617. of `org-scanner-tags' instead of building the list by itself. This
  10618. can lead to large speed-ups when the tags scanner is used in a file with
  10619. many entries, and when the list of tags is retrieved, for example to
  10620. obtain a list of properties. Building the tags list for each entry in such
  10621. a file becomes an N^2 operation - but with this variable set, it scales
  10622. as N.")
  10623. (defun org-scan-tags (action matcher &optional todo-only)
  10624. "Scan headline tags with inheritance and produce output ACTION.
  10625. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  10626. or `agenda' to produce an entry list for an agenda view. It can also be
  10627. a Lisp form or a function that should be called at each matched headline, in
  10628. this case the return value is a list of all return values from these calls.
  10629. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  10630. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  10631. only lines with a TODO keyword are included in the output."
  10632. (require 'org-agenda)
  10633. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  10634. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  10635. (org-re
  10636. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  10637. (props (list 'face 'default
  10638. 'done-face 'org-agenda-done
  10639. 'undone-face 'default
  10640. 'mouse-face 'highlight
  10641. 'org-not-done-regexp org-not-done-regexp
  10642. 'org-todo-regexp org-todo-regexp
  10643. 'help-echo
  10644. (format "mouse-2 or RET jump to org file %s"
  10645. (abbreviate-file-name
  10646. (or (buffer-file-name (buffer-base-buffer))
  10647. (buffer-name (buffer-base-buffer)))))))
  10648. (case-fold-search nil)
  10649. (org-map-continue-from nil)
  10650. lspos tags tags-list
  10651. (tags-alist (list (cons 0 org-file-tags)))
  10652. (llast 0) rtn rtn1 level category i txt
  10653. todo marker entry priority)
  10654. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  10655. (setq action (list 'lambda nil action)))
  10656. (save-excursion
  10657. (goto-char (point-min))
  10658. (when (eq action 'sparse-tree)
  10659. (org-overview)
  10660. (org-remove-occur-highlights))
  10661. (while (re-search-forward re nil t)
  10662. (catch :skip
  10663. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  10664. tags (if (match-end 4) (org-match-string-no-properties 4)))
  10665. (goto-char (setq lspos (match-beginning 0)))
  10666. (setq level (org-reduced-level (funcall outline-level))
  10667. category (org-get-category))
  10668. (setq i llast llast level)
  10669. ;; remove tag lists from same and sublevels
  10670. (while (>= i level)
  10671. (when (setq entry (assoc i tags-alist))
  10672. (setq tags-alist (delete entry tags-alist)))
  10673. (setq i (1- i)))
  10674. ;; add the next tags
  10675. (when tags
  10676. (setq tags (org-split-string tags ":")
  10677. tags-alist
  10678. (cons (cons level tags) tags-alist)))
  10679. ;; compile tags for current headline
  10680. (setq tags-list
  10681. (if org-use-tag-inheritance
  10682. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  10683. tags)
  10684. org-scanner-tags tags-list)
  10685. (when org-use-tag-inheritance
  10686. (setcdr (car tags-alist)
  10687. (mapcar (lambda (x)
  10688. (setq x (copy-sequence x))
  10689. (org-add-prop-inherited x))
  10690. (cdar tags-alist))))
  10691. (when (and tags org-use-tag-inheritance
  10692. (or (not (eq t org-use-tag-inheritance))
  10693. org-tags-exclude-from-inheritance))
  10694. ;; selective inheritance, remove uninherited ones
  10695. (setcdr (car tags-alist)
  10696. (org-remove-uniherited-tags (cdar tags-alist))))
  10697. (when (and (or (not todo-only)
  10698. (and (member todo org-not-done-keywords)
  10699. (or (not org-agenda-tags-todo-honor-ignore-options)
  10700. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  10701. (let ((case-fold-search t)) (eval matcher))
  10702. (or
  10703. (not (member org-archive-tag tags-list))
  10704. ;; we have an archive tag, should we use this anyway?
  10705. (or (not org-agenda-skip-archived-trees)
  10706. (and (eq action 'agenda) org-agenda-archives-mode))))
  10707. (unless (eq action 'sparse-tree) (org-agenda-skip))
  10708. ;; select this headline
  10709. (cond
  10710. ((eq action 'sparse-tree)
  10711. (and org-highlight-sparse-tree-matches
  10712. (org-get-heading) (match-end 0)
  10713. (org-highlight-new-match
  10714. (match-beginning 0) (match-beginning 1)))
  10715. (org-show-context 'tags-tree))
  10716. ((eq action 'agenda)
  10717. (setq txt (org-format-agenda-item
  10718. ""
  10719. (concat
  10720. (if (eq org-tags-match-list-sublevels 'indented)
  10721. (make-string (1- level) ?.) "")
  10722. (org-get-heading))
  10723. category
  10724. tags-list
  10725. )
  10726. priority (org-get-priority txt))
  10727. (goto-char lspos)
  10728. (setq marker (org-agenda-new-marker))
  10729. (org-add-props txt props
  10730. 'org-marker marker 'org-hd-marker marker 'org-category category
  10731. 'todo-state todo
  10732. 'priority priority 'type "tagsmatch")
  10733. (push txt rtn))
  10734. ((functionp action)
  10735. (setq org-map-continue-from nil)
  10736. (save-excursion
  10737. (setq rtn1 (funcall action))
  10738. (push rtn1 rtn)))
  10739. (t (error "Invalid action")))
  10740. ;; if we are to skip sublevels, jump to end of subtree
  10741. (unless org-tags-match-list-sublevels
  10742. (org-end-of-subtree t)
  10743. (backward-char 1))))
  10744. ;; Get the correct position from where to continue
  10745. (if org-map-continue-from
  10746. (goto-char org-map-continue-from)
  10747. (and (= (point) lspos) (end-of-line 1)))))
  10748. (when (and (eq action 'sparse-tree)
  10749. (not org-sparse-tree-open-archived-trees))
  10750. (org-hide-archived-subtrees (point-min) (point-max)))
  10751. (nreverse rtn)))
  10752. (defun org-remove-uniherited-tags (tags)
  10753. "Remove all tags that are not inherited from the list TAGS."
  10754. (cond
  10755. ((eq org-use-tag-inheritance t)
  10756. (if org-tags-exclude-from-inheritance
  10757. (org-delete-all org-tags-exclude-from-inheritance tags)
  10758. tags))
  10759. ((not org-use-tag-inheritance) nil)
  10760. ((stringp org-use-tag-inheritance)
  10761. (delq nil (mapcar
  10762. (lambda (x)
  10763. (if (and (string-match org-use-tag-inheritance x)
  10764. (not (member x org-tags-exclude-from-inheritance)))
  10765. x nil))
  10766. tags)))
  10767. ((listp org-use-tag-inheritance)
  10768. (delq nil (mapcar
  10769. (lambda (x)
  10770. (if (member x org-use-tag-inheritance) x nil))
  10771. tags)))))
  10772. (defvar todo-only) ;; dynamically scoped
  10773. (defun org-match-sparse-tree (&optional todo-only match)
  10774. "Create a sparse tree according to tags string MATCH.
  10775. MATCH can contain positive and negative selection of tags, like
  10776. \"+WORK+URGENT-WITHBOSS\".
  10777. If optional argument TODO-ONLY is non-nil, only select lines that are
  10778. also TODO lines."
  10779. (interactive "P")
  10780. (org-prepare-agenda-buffers (list (current-buffer)))
  10781. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  10782. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  10783. (defvar org-cached-props nil)
  10784. (defun org-cached-entry-get (pom property)
  10785. (if (or (eq t org-use-property-inheritance)
  10786. (and (stringp org-use-property-inheritance)
  10787. (string-match org-use-property-inheritance property))
  10788. (and (listp org-use-property-inheritance)
  10789. (member property org-use-property-inheritance)))
  10790. ;; Caching is not possible, check it directly
  10791. (org-entry-get pom property 'inherit)
  10792. ;; Get all properties, so that we can do complicated checks easily
  10793. (cdr (assoc property (or org-cached-props
  10794. (setq org-cached-props
  10795. (org-entry-properties pom)))))))
  10796. (defun org-global-tags-completion-table (&optional files)
  10797. "Return the list of all tags in all agenda buffer/files."
  10798. (save-excursion
  10799. (org-uniquify
  10800. (delq nil
  10801. (apply 'append
  10802. (mapcar
  10803. (lambda (file)
  10804. (set-buffer (find-file-noselect file))
  10805. (append (org-get-buffer-tags)
  10806. (mapcar (lambda (x) (if (stringp (car-safe x))
  10807. (list (car-safe x)) nil))
  10808. org-tag-alist)))
  10809. (if (and files (car files))
  10810. files
  10811. (org-agenda-files))))))))
  10812. (defun org-make-tags-matcher (match)
  10813. "Create the TAGS//TODO matcher form for the selection string MATCH."
  10814. ;; todo-only is scoped dynamically into this function, and the function
  10815. ;; may change it if the matcher asks for it.
  10816. (unless match
  10817. ;; Get a new match request, with completion
  10818. (let ((org-last-tags-completion-table
  10819. (org-global-tags-completion-table)))
  10820. (setq match (org-completing-read-no-i
  10821. "Match: " 'org-tags-completion-function nil nil nil
  10822. 'org-tags-history))))
  10823. ;; Parse the string and create a lisp form
  10824. (let ((match0 match)
  10825. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  10826. minus tag mm
  10827. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  10828. orterms term orlist re-p str-p level-p level-op time-p
  10829. prop-p pn pv po cat-p gv rest)
  10830. (if (string-match "/+" match)
  10831. ;; match contains also a todo-matching request
  10832. (progn
  10833. (setq tagsmatch (substring match 0 (match-beginning 0))
  10834. todomatch (substring match (match-end 0)))
  10835. (if (string-match "^!" todomatch)
  10836. (setq todo-only t todomatch (substring todomatch 1)))
  10837. (if (string-match "^\\s-*$" todomatch)
  10838. (setq todomatch nil)))
  10839. ;; only matching tags
  10840. (setq tagsmatch match todomatch nil))
  10841. ;; Make the tags matcher
  10842. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  10843. (setq tagsmatcher t)
  10844. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  10845. (while (setq term (pop orterms))
  10846. (while (and (equal (substring term -1) "\\") orterms)
  10847. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  10848. (while (string-match re term)
  10849. (setq rest (substring term (match-end 0))
  10850. minus (and (match-end 1)
  10851. (equal (match-string 1 term) "-"))
  10852. tag (match-string 2 term)
  10853. re-p (equal (string-to-char tag) ?{)
  10854. level-p (match-end 4)
  10855. prop-p (match-end 5)
  10856. mm (cond
  10857. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  10858. (level-p
  10859. (setq level-op (org-op-to-function (match-string 3 term)))
  10860. `(,level-op level ,(string-to-number
  10861. (match-string 4 term))))
  10862. (prop-p
  10863. (setq pn (match-string 5 term)
  10864. po (match-string 6 term)
  10865. pv (match-string 7 term)
  10866. cat-p (equal pn "CATEGORY")
  10867. re-p (equal (string-to-char pv) ?{)
  10868. str-p (equal (string-to-char pv) ?\")
  10869. time-p (save-match-data
  10870. (string-match "^\"[[<].*[]>]\"$" pv))
  10871. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  10872. (if time-p (setq pv (org-matcher-time pv)))
  10873. (setq po (org-op-to-function po (if time-p 'time str-p)))
  10874. (cond
  10875. ((equal pn "CATEGORY")
  10876. (setq gv '(get-text-property (point) 'org-category)))
  10877. ((equal pn "TODO")
  10878. (setq gv 'todo))
  10879. (t
  10880. (setq gv `(org-cached-entry-get nil ,pn))))
  10881. (if re-p
  10882. (if (eq po 'org<>)
  10883. `(not (string-match ,pv (or ,gv "")))
  10884. `(string-match ,pv (or ,gv "")))
  10885. (if str-p
  10886. `(,po (or ,gv "") ,pv)
  10887. `(,po (string-to-number (or ,gv ""))
  10888. ,(string-to-number pv) ))))
  10889. (t `(member ,tag tags-list)))
  10890. mm (if minus (list 'not mm) mm)
  10891. term rest)
  10892. (push mm tagsmatcher))
  10893. (push (if (> (length tagsmatcher) 1)
  10894. (cons 'and tagsmatcher)
  10895. (car tagsmatcher))
  10896. orlist)
  10897. (setq tagsmatcher nil))
  10898. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  10899. (setq tagsmatcher
  10900. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  10901. ;; Make the todo matcher
  10902. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  10903. (setq todomatcher t)
  10904. (setq orterms (org-split-string todomatch "|") orlist nil)
  10905. (while (setq term (pop orterms))
  10906. (while (string-match re term)
  10907. (setq minus (and (match-end 1)
  10908. (equal (match-string 1 term) "-"))
  10909. kwd (match-string 2 term)
  10910. re-p (equal (string-to-char kwd) ?{)
  10911. term (substring term (match-end 0))
  10912. mm (if re-p
  10913. `(string-match ,(substring kwd 1 -1) todo)
  10914. (list 'equal 'todo kwd))
  10915. mm (if minus (list 'not mm) mm))
  10916. (push mm todomatcher))
  10917. (push (if (> (length todomatcher) 1)
  10918. (cons 'and todomatcher)
  10919. (car todomatcher))
  10920. orlist)
  10921. (setq todomatcher nil))
  10922. (setq todomatcher (if (> (length orlist) 1)
  10923. (cons 'or orlist) (car orlist))))
  10924. ;; Return the string and lisp forms of the matcher
  10925. (setq matcher (if todomatcher
  10926. (list 'and tagsmatcher todomatcher)
  10927. tagsmatcher))
  10928. (cons match0 matcher)))
  10929. (defun org-op-to-function (op &optional stringp)
  10930. "Turn an operator into the appropriate function."
  10931. (setq op
  10932. (cond
  10933. ((equal op "<" ) '(< string< org-time<))
  10934. ((equal op ">" ) '(> org-string> org-time>))
  10935. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  10936. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  10937. ((member op '("=" "==")) '(= string= org-time=))
  10938. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  10939. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  10940. (defun org<> (a b) (not (= a b)))
  10941. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  10942. (defun org-string>= (a b) (not (string< a b)))
  10943. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  10944. (defun org-string<> (a b) (not (string= a b)))
  10945. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  10946. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  10947. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  10948. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  10949. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  10950. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  10951. (defun org-2ft (s)
  10952. "Convert S to a floating point time.
  10953. If S is already a number, just return it. If it is a string, parse
  10954. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  10955. (cond
  10956. ((numberp s) s)
  10957. ((stringp s)
  10958. (condition-case nil
  10959. (float-time (apply 'encode-time (org-parse-time-string s)))
  10960. (error 0.)))
  10961. (t 0.)))
  10962. (defun org-time-today ()
  10963. "Time in seconds today at 0:00.
  10964. Returns the float number of seconds since the beginning of the
  10965. epoch to the beginning of today (00:00)."
  10966. (float-time (apply 'encode-time
  10967. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  10968. (defun org-matcher-time (s)
  10969. "Interpret a time comparison value."
  10970. (save-match-data
  10971. (cond
  10972. ((string= s "<now>") (float-time))
  10973. ((string= s "<today>") (org-time-today))
  10974. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  10975. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  10976. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  10977. (+ (org-time-today)
  10978. (* (string-to-number (match-string 1 s))
  10979. (cdr (assoc (match-string 2 s)
  10980. '(("d" . 86400.0) ("w" . 604800.0)
  10981. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10982. (t (org-2ft s)))))
  10983. (defun org-match-any-p (re list)
  10984. "Does re match any element of list?"
  10985. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10986. (delq nil list))
  10987. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10988. (defvar org-tags-overlay (make-overlay 1 1))
  10989. (org-detach-overlay org-tags-overlay)
  10990. (defun org-get-local-tags-at (&optional pos)
  10991. "Get a list of tags defined in the current headline."
  10992. (org-get-tags-at pos 'local))
  10993. (defun org-get-local-tags ()
  10994. "Get a list of tags defined in the current headline."
  10995. (org-get-tags-at nil 'local))
  10996. (defun org-get-tags-at (&optional pos local)
  10997. "Get a list of all headline tags applicable at POS.
  10998. POS defaults to point. If tags are inherited, the list contains
  10999. the targets in the same sequence as the headlines appear, i.e.
  11000. the tags of the current headline come last.
  11001. When LOCAL is non-nil, only return tags from the current headline,
  11002. ignore inherited ones."
  11003. (interactive)
  11004. (if (and org-trust-scanner-tags
  11005. (or (not pos) (equal pos (point)))
  11006. (not local))
  11007. org-scanner-tags
  11008. (let (tags ltags lastpos parent)
  11009. (save-excursion
  11010. (save-restriction
  11011. (widen)
  11012. (goto-char (or pos (point)))
  11013. (save-match-data
  11014. (catch 'done
  11015. (condition-case nil
  11016. (progn
  11017. (org-back-to-heading t)
  11018. (while (not (equal lastpos (point)))
  11019. (setq lastpos (point))
  11020. (when (looking-at
  11021. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  11022. (setq ltags (org-split-string
  11023. (org-match-string-no-properties 1) ":"))
  11024. (when parent
  11025. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  11026. (setq tags (append
  11027. (if parent
  11028. (org-remove-uniherited-tags ltags)
  11029. ltags)
  11030. tags)))
  11031. (or org-use-tag-inheritance (throw 'done t))
  11032. (if local (throw 'done t))
  11033. (or (org-up-heading-safe) (error nil))
  11034. (setq parent t)))
  11035. (error nil)))))
  11036. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  11037. (defun org-add-prop-inherited (s)
  11038. (add-text-properties 0 (length s) '(inherited t) s)
  11039. s)
  11040. (defun org-toggle-tag (tag &optional onoff)
  11041. "Toggle the tag TAG for the current line.
  11042. If ONOFF is `on' or `off', don't toggle but set to this state."
  11043. (let (res current)
  11044. (save-excursion
  11045. (org-back-to-heading t)
  11046. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  11047. (point-at-eol) t)
  11048. (progn
  11049. (setq current (match-string 1))
  11050. (replace-match ""))
  11051. (setq current ""))
  11052. (setq current (nreverse (org-split-string current ":")))
  11053. (cond
  11054. ((eq onoff 'on)
  11055. (setq res t)
  11056. (or (member tag current) (push tag current)))
  11057. ((eq onoff 'off)
  11058. (or (not (member tag current)) (setq current (delete tag current))))
  11059. (t (if (member tag current)
  11060. (setq current (delete tag current))
  11061. (setq res t)
  11062. (push tag current))))
  11063. (end-of-line 1)
  11064. (if current
  11065. (progn
  11066. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  11067. (org-set-tags nil t))
  11068. (delete-horizontal-space))
  11069. (run-hooks 'org-after-tags-change-hook))
  11070. res))
  11071. (defun org-align-tags-here (to-col)
  11072. ;; Assumes that this is a headline
  11073. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  11074. (beginning-of-line 1)
  11075. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11076. (< pos (match-beginning 2)))
  11077. (progn
  11078. (setq tags-l (- (match-end 2) (match-beginning 2)))
  11079. (goto-char (match-beginning 1))
  11080. (insert " ")
  11081. (delete-region (point) (1+ (match-beginning 2)))
  11082. (setq ncol (max (1+ (current-column))
  11083. (1+ col)
  11084. (if (> to-col 0)
  11085. to-col
  11086. (- (abs to-col) tags-l))))
  11087. (setq p (point))
  11088. (insert (make-string (- ncol (current-column)) ?\ ))
  11089. (setq ncol (current-column))
  11090. (when indent-tabs-mode (tabify p (point-at-eol)))
  11091. (org-move-to-column (min ncol col) t))
  11092. (goto-char pos))))
  11093. (defun org-set-tags-command (&optional arg just-align)
  11094. "Call the set-tags command for the current entry."
  11095. (interactive "P")
  11096. (if (org-on-heading-p)
  11097. (org-set-tags arg just-align)
  11098. (save-excursion
  11099. (org-back-to-heading t)
  11100. (org-set-tags arg just-align))))
  11101. (defun org-set-tags-to (data)
  11102. "Set the tags of the current entry to DATA, replacing the current tags.
  11103. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  11104. If DATA is nil or the empty string, any tags will be removed."
  11105. (interactive "sTags: ")
  11106. (setq data
  11107. (cond
  11108. ((eq data nil) "")
  11109. ((equal data "") "")
  11110. ((stringp data)
  11111. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  11112. ":"))
  11113. ((listp data)
  11114. (concat ":" (mapconcat 'identity data ":") ":"))
  11115. (t nil)))
  11116. (when data
  11117. (save-excursion
  11118. (org-back-to-heading t)
  11119. (when (looking-at org-complex-heading-regexp)
  11120. (if (match-end 5)
  11121. (progn
  11122. (goto-char (match-beginning 5))
  11123. (insert data)
  11124. (delete-region (point) (point-at-eol))
  11125. (org-set-tags nil 'align))
  11126. (goto-char (point-at-eol))
  11127. (insert " " data)
  11128. (org-set-tags nil 'align)))
  11129. (beginning-of-line 1)
  11130. (if (looking-at ".*?\\([ \t]+\\)$")
  11131. (delete-region (match-beginning 1) (match-end 1))))))
  11132. (defun org-align-all-tags ()
  11133. "Align the tags i all headings."
  11134. (interactive)
  11135. (save-excursion
  11136. (or (ignore-errors (org-back-to-heading t))
  11137. (outline-next-heading))
  11138. (if (org-on-heading-p)
  11139. (org-set-tags t)
  11140. (message "No headings"))))
  11141. (defun org-set-tags (&optional arg just-align)
  11142. "Set the tags for the current headline.
  11143. With prefix ARG, realign all tags in headings in the current buffer."
  11144. (interactive "P")
  11145. (let* ((re (concat "^" outline-regexp))
  11146. (current (org-get-tags-string))
  11147. (col (current-column))
  11148. (org-setting-tags t)
  11149. table current-tags inherited-tags ; computed below when needed
  11150. tags p0 c0 c1 rpl)
  11151. (if arg
  11152. (save-excursion
  11153. (goto-char (point-min))
  11154. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11155. (while (re-search-forward re nil t)
  11156. (org-set-tags nil t)
  11157. (end-of-line 1)))
  11158. (message "All tags realigned to column %d" org-tags-column))
  11159. (if just-align
  11160. (setq tags current)
  11161. ;; Get a new set of tags from the user
  11162. (save-excursion
  11163. (setq table (append org-tag-persistent-alist
  11164. (or org-tag-alist (org-get-buffer-tags))
  11165. (and org-complete-tags-always-offer-all-agenda-tags
  11166. (org-global-tags-completion-table (org-agenda-files))))
  11167. org-last-tags-completion-table table
  11168. current-tags (org-split-string current ":")
  11169. inherited-tags (nreverse
  11170. (nthcdr (length current-tags)
  11171. (nreverse (org-get-tags-at))))
  11172. tags
  11173. (if (or (eq t org-use-fast-tag-selection)
  11174. (and org-use-fast-tag-selection
  11175. (delq nil (mapcar 'cdr table))))
  11176. (org-fast-tag-selection
  11177. current-tags inherited-tags table
  11178. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  11179. (let ((org-add-colon-after-tag-completion t))
  11180. (org-trim
  11181. (org-without-partial-completion
  11182. (org-icompleting-read "Tags: " 'org-tags-completion-function
  11183. nil nil current 'org-tags-history)))))))
  11184. (while (string-match "[-+&]+" tags)
  11185. ;; No boolean logic, just a list
  11186. (setq tags (replace-match ":" t t tags))))
  11187. (if org-tags-sort-function
  11188. (setq tags (mapconcat 'identity
  11189. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  11190. org-tags-sort-function) ":")))
  11191. (if (string-match "\\`[\t ]*\\'" tags)
  11192. (setq tags "")
  11193. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11194. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11195. ;; Insert new tags at the correct column
  11196. (beginning-of-line 1)
  11197. (cond
  11198. ((and (equal current "") (equal tags "")))
  11199. ((re-search-forward
  11200. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11201. (point-at-eol) t)
  11202. (if (equal tags "")
  11203. (setq rpl "")
  11204. (goto-char (match-beginning 0))
  11205. (setq c0 (current-column) p0 (if (equal (char-before) ?*)
  11206. (1+ (point)) (point))
  11207. c1 (max (1+ c0) (if (> org-tags-column 0)
  11208. org-tags-column
  11209. (- (- org-tags-column) (length tags))))
  11210. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11211. (replace-match rpl t t)
  11212. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  11213. tags)
  11214. (t (error "Tags alignment failed")))
  11215. (org-move-to-column col)
  11216. (unless just-align
  11217. (run-hooks 'org-after-tags-change-hook)))))
  11218. (defun org-change-tag-in-region (beg end tag off)
  11219. "Add or remove TAG for each entry in the region.
  11220. This works in the agenda, and also in an org-mode buffer."
  11221. (interactive
  11222. (list (region-beginning) (region-end)
  11223. (let ((org-last-tags-completion-table
  11224. (if (org-mode-p)
  11225. (org-get-buffer-tags)
  11226. (org-global-tags-completion-table))))
  11227. (org-icompleting-read
  11228. "Tag: " 'org-tags-completion-function nil nil nil
  11229. 'org-tags-history))
  11230. (progn
  11231. (message "[s]et or [r]emove? ")
  11232. (equal (read-char-exclusive) ?r))))
  11233. (if (fboundp 'deactivate-mark) (deactivate-mark))
  11234. (let ((agendap (equal major-mode 'org-agenda-mode))
  11235. l1 l2 m buf pos newhead (cnt 0))
  11236. (goto-char end)
  11237. (setq l2 (1- (org-current-line)))
  11238. (goto-char beg)
  11239. (setq l1 (org-current-line))
  11240. (loop for l from l1 to l2 do
  11241. (org-goto-line l)
  11242. (setq m (get-text-property (point) 'org-hd-marker))
  11243. (when (or (and (org-mode-p) (org-on-heading-p))
  11244. (and agendap m))
  11245. (setq buf (if agendap (marker-buffer m) (current-buffer))
  11246. pos (if agendap m (point)))
  11247. (with-current-buffer buf
  11248. (save-excursion
  11249. (save-restriction
  11250. (goto-char pos)
  11251. (setq cnt (1+ cnt))
  11252. (org-toggle-tag tag (if off 'off 'on))
  11253. (setq newhead (org-get-heading)))))
  11254. (and agendap (org-agenda-change-all-lines newhead m))))
  11255. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  11256. (defun org-tags-completion-function (string predicate &optional flag)
  11257. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11258. (confirm (lambda (x) (stringp (car x)))))
  11259. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  11260. (setq s1 (match-string 1 string)
  11261. s2 (match-string 2 string))
  11262. (setq s1 "" s2 string))
  11263. (cond
  11264. ((eq flag nil)
  11265. ;; try completion
  11266. (setq rtn (try-completion s2 ctable confirm))
  11267. (if (stringp rtn)
  11268. (setq rtn
  11269. (concat s1 s2 (substring rtn (length s2))
  11270. (if (and org-add-colon-after-tag-completion
  11271. (assoc rtn ctable))
  11272. ":" ""))))
  11273. rtn)
  11274. ((eq flag t)
  11275. ;; all-completions
  11276. (all-completions s2 ctable confirm)
  11277. )
  11278. ((eq flag 'lambda)
  11279. ;; exact match?
  11280. (assoc s2 ctable)))
  11281. ))
  11282. (defun org-fast-tag-insert (kwd tags face &optional end)
  11283. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  11284. (insert (format "%-12s" (concat kwd ":"))
  11285. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  11286. (or end "")))
  11287. (defun org-fast-tag-show-exit (flag)
  11288. (save-excursion
  11289. (org-goto-line 3)
  11290. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  11291. (replace-match ""))
  11292. (when flag
  11293. (end-of-line 1)
  11294. (org-move-to-column (- (window-width) 19) t)
  11295. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  11296. (defun org-set-current-tags-overlay (current prefix)
  11297. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  11298. (if (featurep 'xemacs)
  11299. (org-overlay-display org-tags-overlay (concat prefix s)
  11300. 'secondary-selection)
  11301. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  11302. (org-overlay-display org-tags-overlay (concat prefix s)))))
  11303. (defvar org-last-tag-selection-key nil)
  11304. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  11305. "Fast tag selection with single keys.
  11306. CURRENT is the current list of tags in the headline, INHERITED is the
  11307. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  11308. possibly with grouping information. TODO-TABLE is a similar table with
  11309. TODO keywords, should these have keys assigned to them.
  11310. If the keys are nil, a-z are automatically assigned.
  11311. Returns the new tags string, or nil to not change the current settings."
  11312. (let* ((fulltable (append table todo-table))
  11313. (maxlen (apply 'max (mapcar
  11314. (lambda (x)
  11315. (if (stringp (car x)) (string-width (car x)) 0))
  11316. fulltable)))
  11317. (buf (current-buffer))
  11318. (expert (eq org-fast-tag-selection-single-key 'expert))
  11319. (buffer-tags nil)
  11320. (fwidth (+ maxlen 3 1 3))
  11321. (ncol (/ (- (window-width) 4) fwidth))
  11322. (i-face 'org-done)
  11323. (c-face 'org-todo)
  11324. tg cnt e c char c1 c2 ntable tbl rtn
  11325. ov-start ov-end ov-prefix
  11326. (exit-after-next org-fast-tag-selection-single-key)
  11327. (done-keywords org-done-keywords)
  11328. groups ingroup)
  11329. (save-excursion
  11330. (beginning-of-line 1)
  11331. (if (looking-at
  11332. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11333. (setq ov-start (match-beginning 1)
  11334. ov-end (match-end 1)
  11335. ov-prefix "")
  11336. (setq ov-start (1- (point-at-eol))
  11337. ov-end (1+ ov-start))
  11338. (skip-chars-forward "^\n\r")
  11339. (setq ov-prefix
  11340. (concat
  11341. (buffer-substring (1- (point)) (point))
  11342. (if (> (current-column) org-tags-column)
  11343. " "
  11344. (make-string (- org-tags-column (current-column)) ?\ ))))))
  11345. (move-overlay org-tags-overlay ov-start ov-end)
  11346. (save-window-excursion
  11347. (if expert
  11348. (set-buffer (get-buffer-create " *Org tags*"))
  11349. (delete-other-windows)
  11350. (split-window-vertically)
  11351. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  11352. (erase-buffer)
  11353. (org-set-local 'org-done-keywords done-keywords)
  11354. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  11355. (org-fast-tag-insert "Current" current c-face "\n\n")
  11356. (org-fast-tag-show-exit exit-after-next)
  11357. (org-set-current-tags-overlay current ov-prefix)
  11358. (setq tbl fulltable char ?a cnt 0)
  11359. (while (setq e (pop tbl))
  11360. (cond
  11361. ((equal (car e) :startgroup)
  11362. (push '() groups) (setq ingroup t)
  11363. (when (not (= cnt 0))
  11364. (setq cnt 0)
  11365. (insert "\n"))
  11366. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  11367. ((equal (car e) :endgroup)
  11368. (setq ingroup nil cnt 0)
  11369. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  11370. ((equal e '(:newline))
  11371. (when (not (= cnt 0))
  11372. (setq cnt 0)
  11373. (insert "\n")
  11374. (setq e (car tbl))
  11375. (while (equal (car tbl) '(:newline))
  11376. (insert "\n")
  11377. (setq tbl (cdr tbl)))))
  11378. (t
  11379. (setq tg (copy-sequence (car e)) c2 nil)
  11380. (if (cdr e)
  11381. (setq c (cdr e))
  11382. ;; automatically assign a character.
  11383. (setq c1 (string-to-char
  11384. (downcase (substring
  11385. tg (if (= (string-to-char tg) ?@) 1 0)))))
  11386. (if (or (rassoc c1 ntable) (rassoc c1 table))
  11387. (while (or (rassoc char ntable) (rassoc char table))
  11388. (setq char (1+ char)))
  11389. (setq c2 c1))
  11390. (setq c (or c2 char)))
  11391. (if ingroup (push tg (car groups)))
  11392. (setq tg (org-add-props tg nil 'face
  11393. (cond
  11394. ((not (assoc tg table))
  11395. (org-get-todo-face tg))
  11396. ((member tg current) c-face)
  11397. ((member tg inherited) i-face)
  11398. (t nil))))
  11399. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11400. (insert "[" c "] " tg (make-string
  11401. (- fwidth 4 (length tg)) ?\ ))
  11402. (push (cons tg c) ntable)
  11403. (when (= (setq cnt (1+ cnt)) ncol)
  11404. (insert "\n")
  11405. (if ingroup (insert " "))
  11406. (setq cnt 0)))))
  11407. (setq ntable (nreverse ntable))
  11408. (insert "\n")
  11409. (goto-char (point-min))
  11410. (if (not expert) (org-fit-window-to-buffer))
  11411. (setq rtn
  11412. (catch 'exit
  11413. (while t
  11414. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  11415. (if (not groups) "no " "")
  11416. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  11417. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11418. (setq org-last-tag-selection-key c)
  11419. (cond
  11420. ((= c ?\r) (throw 'exit t))
  11421. ((= c ?!)
  11422. (setq groups (not groups))
  11423. (goto-char (point-min))
  11424. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  11425. ((= c ?\C-c)
  11426. (if (not expert)
  11427. (org-fast-tag-show-exit
  11428. (setq exit-after-next (not exit-after-next)))
  11429. (setq expert nil)
  11430. (delete-other-windows)
  11431. (split-window-vertically)
  11432. (org-switch-to-buffer-other-window " *Org tags*")
  11433. (org-fit-window-to-buffer)))
  11434. ((or (= c ?\C-g)
  11435. (and (= c ?q) (not (rassoc c ntable))))
  11436. (org-detach-overlay org-tags-overlay)
  11437. (setq quit-flag t))
  11438. ((= c ?\ )
  11439. (setq current nil)
  11440. (if exit-after-next (setq exit-after-next 'now)))
  11441. ((= c ?\t)
  11442. (condition-case nil
  11443. (setq tg (org-icompleting-read
  11444. "Tag: "
  11445. (or buffer-tags
  11446. (with-current-buffer buf
  11447. (org-get-buffer-tags)))))
  11448. (quit (setq tg "")))
  11449. (when (string-match "\\S-" tg)
  11450. (add-to-list 'buffer-tags (list tg))
  11451. (if (member tg current)
  11452. (setq current (delete tg current))
  11453. (push tg current)))
  11454. (if exit-after-next (setq exit-after-next 'now)))
  11455. ((setq e (rassoc c todo-table) tg (car e))
  11456. (with-current-buffer buf
  11457. (save-excursion (org-todo tg)))
  11458. (if exit-after-next (setq exit-after-next 'now)))
  11459. ((setq e (rassoc c ntable) tg (car e))
  11460. (if (member tg current)
  11461. (setq current (delete tg current))
  11462. (loop for g in groups do
  11463. (if (member tg g)
  11464. (mapc (lambda (x)
  11465. (setq current (delete x current)))
  11466. g)))
  11467. (push tg current))
  11468. (if exit-after-next (setq exit-after-next 'now))))
  11469. ;; Create a sorted list
  11470. (setq current
  11471. (sort current
  11472. (lambda (a b)
  11473. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  11474. (if (eq exit-after-next 'now) (throw 'exit t))
  11475. (goto-char (point-min))
  11476. (beginning-of-line 2)
  11477. (delete-region (point) (point-at-eol))
  11478. (org-fast-tag-insert "Current" current c-face)
  11479. (org-set-current-tags-overlay current ov-prefix)
  11480. (while (re-search-forward
  11481. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  11482. (setq tg (match-string 1))
  11483. (add-text-properties
  11484. (match-beginning 1) (match-end 1)
  11485. (list 'face
  11486. (cond
  11487. ((member tg current) c-face)
  11488. ((member tg inherited) i-face)
  11489. (t (get-text-property (match-beginning 1) 'face))))))
  11490. (goto-char (point-min)))))
  11491. (org-detach-overlay org-tags-overlay)
  11492. (if rtn
  11493. (mapconcat 'identity current ":")
  11494. nil))))
  11495. (defun org-get-tags-string ()
  11496. "Get the TAGS string in the current headline."
  11497. (unless (org-on-heading-p t)
  11498. (error "Not on a heading"))
  11499. (save-excursion
  11500. (beginning-of-line 1)
  11501. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11502. (org-match-string-no-properties 1)
  11503. "")))
  11504. (defun org-get-tags ()
  11505. "Get the list of tags specified in the current headline."
  11506. (org-split-string (org-get-tags-string) ":"))
  11507. (defun org-get-buffer-tags ()
  11508. "Get a table of all tags used in the buffer, for completion."
  11509. (let (tags)
  11510. (save-excursion
  11511. (goto-char (point-min))
  11512. (while (re-search-forward
  11513. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  11514. (when (equal (char-after (point-at-bol 0)) ?*)
  11515. (mapc (lambda (x) (add-to-list 'tags x))
  11516. (org-split-string (org-match-string-no-properties 1) ":")))))
  11517. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  11518. (mapcar 'list tags)))
  11519. ;;;; The mapping API
  11520. ;;;###autoload
  11521. (defun org-map-entries (func &optional match scope &rest skip)
  11522. "Call FUNC at each headline selected by MATCH in SCOPE.
  11523. FUNC is a function or a lisp form. The function will be called without
  11524. arguments, with the cursor positioned at the beginning of the headline.
  11525. The return values of all calls to the function will be collected and
  11526. returned as a list.
  11527. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  11528. does not need to preserve point. After evaluation, the cursor will be
  11529. moved to the end of the line (presumably of the headline of the
  11530. processed entry) and search continues from there. Under some
  11531. circumstances, this may not produce the wanted results. For example,
  11532. if you have removed (e.g. archived) the current (sub)tree it could
  11533. mean that the next entry will be skipped entirely. In such cases, you
  11534. can specify the position from where search should continue by making
  11535. FUNC set the variable `org-map-continue-from' to the desired buffer
  11536. position.
  11537. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  11538. Only headlines that are matched by this query will be considered during
  11539. the iteration. When MATCH is nil or t, all headlines will be
  11540. visited by the iteration.
  11541. SCOPE determines the scope of this command. It can be any of:
  11542. nil The current buffer, respecting the restriction if any
  11543. tree The subtree started with the entry at point
  11544. file The current buffer, without restriction
  11545. file-with-archives
  11546. The current buffer, and any archives associated with it
  11547. agenda All agenda files
  11548. agenda-with-archives
  11549. All agenda files with any archive files associated with them
  11550. \(file1 file2 ...)
  11551. If this is a list, all files in the list will be scanned
  11552. The remaining args are treated as settings for the skipping facilities of
  11553. the scanner. The following items can be given here:
  11554. archive skip trees with the archive tag.
  11555. comment skip trees with the COMMENT keyword
  11556. function or Emacs Lisp form:
  11557. will be used as value for `org-agenda-skip-function', so whenever
  11558. the function returns t, FUNC will not be called for that
  11559. entry and search will continue from the point where the
  11560. function leaves it.
  11561. If your function needs to retrieve the tags including inherited tags
  11562. at the *current* entry, you can use the value of the variable
  11563. `org-scanner-tags' which will be much faster than getting the value
  11564. with `org-get-tags-at'. If your function gets properties with
  11565. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  11566. to t around the call to `org-entry-properties' to get the same speedup.
  11567. Note that if your function moves around to retrieve tags and properties at
  11568. a *different* entry, you cannot use these techniques."
  11569. (let* ((org-agenda-archives-mode nil) ; just to make sure
  11570. (org-agenda-skip-archived-trees (memq 'archive skip))
  11571. (org-agenda-skip-comment-trees (memq 'comment skip))
  11572. (org-agenda-skip-function
  11573. (car (org-delete-all '(comment archive) skip)))
  11574. (org-tags-match-list-sublevels t)
  11575. matcher file res
  11576. org-todo-keywords-for-agenda
  11577. org-done-keywords-for-agenda
  11578. org-todo-keyword-alist-for-agenda
  11579. org-drawers-for-agenda
  11580. org-tag-alist-for-agenda)
  11581. (cond
  11582. ((eq match t) (setq matcher t))
  11583. ((eq match nil) (setq matcher t))
  11584. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  11585. (save-excursion
  11586. (save-restriction
  11587. (when (eq scope 'tree)
  11588. (org-back-to-heading t)
  11589. (org-narrow-to-subtree)
  11590. (setq scope nil))
  11591. (if (not scope)
  11592. (progn
  11593. (org-prepare-agenda-buffers
  11594. (list (buffer-file-name (current-buffer))))
  11595. (setq res (org-scan-tags func matcher)))
  11596. ;; Get the right scope
  11597. (cond
  11598. ((and scope (listp scope) (symbolp (car scope)))
  11599. (setq scope (eval scope)))
  11600. ((eq scope 'agenda)
  11601. (setq scope (org-agenda-files t)))
  11602. ((eq scope 'agenda-with-archives)
  11603. (setq scope (org-agenda-files t))
  11604. (setq scope (org-add-archive-files scope)))
  11605. ((eq scope 'file)
  11606. (setq scope (list (buffer-file-name))))
  11607. ((eq scope 'file-with-archives)
  11608. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  11609. (org-prepare-agenda-buffers scope)
  11610. (while (setq file (pop scope))
  11611. (with-current-buffer (org-find-base-buffer-visiting file)
  11612. (save-excursion
  11613. (save-restriction
  11614. (widen)
  11615. (goto-char (point-min))
  11616. (setq res (append res (org-scan-tags func matcher))))))))))
  11617. res))
  11618. ;;;; Properties
  11619. ;;; Setting and retrieving properties
  11620. (defconst org-special-properties
  11621. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  11622. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED")
  11623. "The special properties valid in Org-mode.
  11624. These are properties that are not defined in the property drawer,
  11625. but in some other way.")
  11626. (defconst org-default-properties
  11627. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  11628. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  11629. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  11630. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  11631. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  11632. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  11633. "Some properties that are used by Org-mode for various purposes.
  11634. Being in this list makes sure that they are offered for completion.")
  11635. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  11636. "Regular expression matching the first line of a property drawer.")
  11637. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  11638. "Regular expression matching the last line of a property drawer.")
  11639. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  11640. "Regular expression matching the first line of a property drawer.")
  11641. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  11642. "Regular expression matching the first line of a property drawer.")
  11643. (defconst org-property-drawer-re
  11644. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  11645. org-property-end-re "\\)\n?")
  11646. "Matches an entire property drawer.")
  11647. (defconst org-clock-drawer-re
  11648. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  11649. org-property-end-re "\\)\n?")
  11650. "Matches an entire clock drawer.")
  11651. (defun org-property-action ()
  11652. "Do an action on properties."
  11653. (interactive)
  11654. (let (c)
  11655. (org-at-property-p)
  11656. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  11657. (setq c (read-char-exclusive))
  11658. (cond
  11659. ((equal c ?s)
  11660. (call-interactively 'org-set-property))
  11661. ((equal c ?d)
  11662. (call-interactively 'org-delete-property))
  11663. ((equal c ?D)
  11664. (call-interactively 'org-delete-property-globally))
  11665. ((equal c ?c)
  11666. (call-interactively 'org-compute-property-at-point))
  11667. (t (error "No such property action %c" c)))))
  11668. (defun org-set-effort (&optional value)
  11669. "Set the effort property of the current entry.
  11670. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  11671. allowed value."
  11672. (interactive "P")
  11673. (if (equal value 0) (setq value 10))
  11674. (let* ((completion-ignore-case t)
  11675. (prop org-effort-property)
  11676. (cur (org-entry-get nil prop))
  11677. (allowed (org-property-get-allowed-values nil prop 'table))
  11678. (existing (mapcar 'list (org-property-values prop)))
  11679. rpl
  11680. (val (cond
  11681. ((stringp value) value)
  11682. ((and allowed (integerp value))
  11683. (or (car (nth (1- value) allowed))
  11684. (car (org-last allowed))))
  11685. (allowed
  11686. (message "Select 1-9,0, [RET%s]: %s"
  11687. (if cur (concat "=" cur) "")
  11688. (mapconcat 'car allowed " "))
  11689. (setq rpl (read-char-exclusive))
  11690. (if (equal rpl ?\r)
  11691. cur
  11692. (setq rpl (- rpl ?0))
  11693. (if (equal rpl 0) (setq rpl 10))
  11694. (if (and (> rpl 0) (<= rpl (length allowed)))
  11695. (car (nth (1- rpl) allowed))
  11696. (org-completing-read "Effort: " allowed nil))))
  11697. (t
  11698. (let (org-completion-use-ido org-completion-use-iswitchb)
  11699. (org-completing-read
  11700. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  11701. (concat "[" cur "]") "")
  11702. ": ")
  11703. existing nil nil "" nil cur))))))
  11704. (unless (equal (org-entry-get nil prop) val)
  11705. (org-entry-put nil prop val))
  11706. (message "%s is now %s" prop val)))
  11707. (defun org-at-property-p ()
  11708. "Is cursor inside a property drawer?"
  11709. (save-excursion
  11710. (beginning-of-line 1)
  11711. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  11712. (let ((match (match-data)) ;; Keep match-data for use by calling
  11713. (p (point)) ;; procedures.
  11714. (range (unless (org-before-first-heading-p)
  11715. (org-get-property-block))))
  11716. (prog1 (and range (<= (car range) p) (< p (cdr range)))
  11717. (set-match-data match))))))
  11718. (defun org-get-property-block (&optional beg end force)
  11719. "Return the (beg . end) range of the body of the property drawer.
  11720. BEG and END can be beginning and end of subtree, if not given
  11721. they will be found.
  11722. If the drawer does not exist and FORCE is non-nil, create the drawer."
  11723. (catch 'exit
  11724. (save-excursion
  11725. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  11726. (end (or end (progn (outline-next-heading) (point)))))
  11727. (goto-char beg)
  11728. (if (re-search-forward org-property-start-re end t)
  11729. (setq beg (1+ (match-end 0)))
  11730. (if force
  11731. (save-excursion
  11732. (org-insert-property-drawer)
  11733. (setq end (progn (outline-next-heading) (point))))
  11734. (throw 'exit nil))
  11735. (goto-char beg)
  11736. (if (re-search-forward org-property-start-re end t)
  11737. (setq beg (1+ (match-end 0)))))
  11738. (if (re-search-forward org-property-end-re end t)
  11739. (setq end (match-beginning 0))
  11740. (or force (throw 'exit nil))
  11741. (goto-char beg)
  11742. (setq end beg)
  11743. (org-indent-line-function)
  11744. (insert ":END:\n"))
  11745. (cons beg end)))))
  11746. (defun org-entry-properties (&optional pom which specific)
  11747. "Get all properties of the entry at point-or-marker POM.
  11748. This includes the TODO keyword, the tags, time strings for deadline,
  11749. scheduled, and clocking, and any additional properties defined in the
  11750. entry. The return value is an alist, keys may occur multiple times
  11751. if the property key was used several times.
  11752. POM may also be nil, in which case the current entry is used.
  11753. If WHICH is nil or `all', get all properties. If WHICH is
  11754. `special' or `standard', only get that subclass. If WHICH
  11755. is a string only get exactly this property. Specific can be a string, the
  11756. specific property we are interested in. Specifying it can speed
  11757. things up because then unnecessary parsing is avoided."
  11758. (setq which (or which 'all))
  11759. (org-with-point-at pom
  11760. (let ((clockstr (substring org-clock-string 0 -1))
  11761. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  11762. (case-fold-search nil)
  11763. beg end range props sum-props key value string clocksum)
  11764. (save-excursion
  11765. (when (condition-case nil
  11766. (and (org-mode-p) (org-back-to-heading t))
  11767. (error nil))
  11768. (setq beg (point))
  11769. (setq sum-props (get-text-property (point) 'org-summaries))
  11770. (setq clocksum (get-text-property (point) :org-clock-minutes))
  11771. (outline-next-heading)
  11772. (setq end (point))
  11773. (when (memq which '(all special))
  11774. ;; Get the special properties, like TODO and tags
  11775. (goto-char beg)
  11776. (when (and (or (not specific) (string= specific "TODO"))
  11777. (looking-at org-todo-line-regexp) (match-end 2))
  11778. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  11779. (when (and (or (not specific) (string= specific "PRIORITY"))
  11780. (looking-at org-priority-regexp))
  11781. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  11782. (when (and (or (not specific) (string= specific "TAGS"))
  11783. (setq value (org-get-tags-string))
  11784. (string-match "\\S-" value))
  11785. (push (cons "TAGS" value) props))
  11786. (when (and (or (not specific) (string= specific "ALLTAGS"))
  11787. (setq value (org-get-tags-at)))
  11788. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  11789. ":"))
  11790. props))
  11791. (when (or (not specific) (string= specific "BLOCKED"))
  11792. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  11793. (when (or (not specific)
  11794. (member specific org-all-time-keywords)
  11795. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  11796. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  11797. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  11798. string (if (equal key clockstr)
  11799. (org-no-properties
  11800. (org-trim
  11801. (buffer-substring
  11802. (match-beginning 3) (goto-char (point-at-eol)))))
  11803. (substring (org-match-string-no-properties 3) 1 -1)))
  11804. (unless key
  11805. (if (= (char-after (match-beginning 3)) ?\[)
  11806. (setq key "TIMESTAMP_IA")
  11807. (setq key "TIMESTAMP")))
  11808. (when (or (equal key clockstr) (not (assoc key props)))
  11809. (push (cons key string) props))))
  11810. )
  11811. (when (memq which '(all standard))
  11812. ;; Get the standard properties, like :PROP: ...
  11813. (setq range (org-get-property-block beg end))
  11814. (when range
  11815. (goto-char (car range))
  11816. (while (re-search-forward
  11817. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  11818. (cdr range) t)
  11819. (setq key (org-match-string-no-properties 1)
  11820. value (org-trim (or (org-match-string-no-properties 2) "")))
  11821. (unless (member key excluded)
  11822. (push (cons key (or value "")) props)))))
  11823. (if clocksum
  11824. (push (cons "CLOCKSUM"
  11825. (org-columns-number-to-string (/ (float clocksum) 60.)
  11826. 'add_times))
  11827. props))
  11828. (unless (assoc "CATEGORY" props)
  11829. (setq value (or (org-get-category)
  11830. (progn (org-refresh-category-properties)
  11831. (org-get-category))))
  11832. (push (cons "CATEGORY" value) props))
  11833. (append sum-props (nreverse props)))))))
  11834. (defun org-entry-get (pom property &optional inherit)
  11835. "Get value of PROPERTY for entry at point-or-marker POM.
  11836. If INHERIT is non-nil and the entry does not have the property,
  11837. then also check higher levels of the hierarchy.
  11838. If INHERIT is the symbol `selective', use inheritance only if the setting
  11839. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  11840. If the property is present but empty, the return value is the empty string.
  11841. If the property is not present at all, nil is returned."
  11842. (org-with-point-at pom
  11843. (if (and inherit (if (eq inherit 'selective)
  11844. (org-property-inherit-p property)
  11845. t))
  11846. (org-entry-get-with-inheritance property)
  11847. (if (member property org-special-properties)
  11848. ;; We need a special property. Use `org-entry-properties' to
  11849. ;; retrieve it, but specify the wanted property
  11850. (cdr (assoc property (org-entry-properties nil 'special property)))
  11851. (let ((range (org-get-property-block)))
  11852. (if (and range
  11853. (goto-char (car range))
  11854. (re-search-forward
  11855. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  11856. (cdr range) t))
  11857. ;; Found the property, return it.
  11858. (if (match-end 1)
  11859. (org-match-string-no-properties 1)
  11860. "")))))))
  11861. (defun org-property-or-variable-value (var &optional inherit)
  11862. "Check if there is a property fixing the value of VAR.
  11863. If yes, return this value. If not, return the current value of the variable."
  11864. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  11865. (if (and prop (stringp prop) (string-match "\\S-" prop))
  11866. (read prop)
  11867. (symbol-value var))))
  11868. (defun org-entry-delete (pom property)
  11869. "Delete the property PROPERTY from entry at point-or-marker POM."
  11870. (org-with-point-at pom
  11871. (if (member property org-special-properties)
  11872. nil ; cannot delete these properties.
  11873. (let ((range (org-get-property-block)))
  11874. (if (and range
  11875. (goto-char (car range))
  11876. (re-search-forward
  11877. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  11878. (cdr range) t))
  11879. (progn
  11880. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  11881. t)
  11882. nil)))))
  11883. ;; Multi-values properties are properties that contain multiple values
  11884. ;; These values are assumed to be single words, separated by whitespace.
  11885. (defun org-entry-add-to-multivalued-property (pom property value)
  11886. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  11887. (let* ((old (org-entry-get pom property))
  11888. (values (and old (org-split-string old "[ \t]"))))
  11889. (setq value (org-entry-protect-space value))
  11890. (unless (member value values)
  11891. (setq values (cons value values))
  11892. (org-entry-put pom property
  11893. (mapconcat 'identity values " ")))))
  11894. (defun org-entry-remove-from-multivalued-property (pom property value)
  11895. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  11896. (let* ((old (org-entry-get pom property))
  11897. (values (and old (org-split-string old "[ \t]"))))
  11898. (setq value (org-entry-protect-space value))
  11899. (when (member value values)
  11900. (setq values (delete value values))
  11901. (org-entry-put pom property
  11902. (mapconcat 'identity values " ")))))
  11903. (defun org-entry-member-in-multivalued-property (pom property value)
  11904. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  11905. (let* ((old (org-entry-get pom property))
  11906. (values (and old (org-split-string old "[ \t]"))))
  11907. (setq value (org-entry-protect-space value))
  11908. (member value values)))
  11909. (defun org-entry-get-multivalued-property (pom property)
  11910. "Return a list of values in a multivalued property."
  11911. (let* ((value (org-entry-get pom property))
  11912. (values (and value (org-split-string value "[ \t]"))))
  11913. (mapcar 'org-entry-restore-space values)))
  11914. (defun org-entry-put-multivalued-property (pom property &rest values)
  11915. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  11916. VALUES should be a list of strings. Spaces will be protected."
  11917. (org-entry-put pom property
  11918. (mapconcat 'org-entry-protect-space values " "))
  11919. (let* ((value (org-entry-get pom property))
  11920. (values (and value (org-split-string value "[ \t]"))))
  11921. (mapcar 'org-entry-restore-space values)))
  11922. (defun org-entry-protect-space (s)
  11923. "Protect spaces and newline in string S."
  11924. (while (string-match " " s)
  11925. (setq s (replace-match "%20" t t s)))
  11926. (while (string-match "\n" s)
  11927. (setq s (replace-match "%0A" t t s)))
  11928. s)
  11929. (defun org-entry-restore-space (s)
  11930. "Restore spaces and newline in string S."
  11931. (while (string-match "%20" s)
  11932. (setq s (replace-match " " t t s)))
  11933. (while (string-match "%0A" s)
  11934. (setq s (replace-match "\n" t t s)))
  11935. s)
  11936. (defvar org-entry-property-inherited-from (make-marker)
  11937. "Marker pointing to the entry from where a property was inherited.
  11938. Each call to `org-entry-get-with-inheritance' will set this marker to the
  11939. location of the entry where the inheritance search matched. If there was
  11940. no match, the marker will point nowhere.
  11941. Note that also `org-entry-get' calls this function, if the INHERIT flag
  11942. is set.")
  11943. (defun org-entry-get-with-inheritance (property)
  11944. "Get entry property, and search higher levels if not present."
  11945. (move-marker org-entry-property-inherited-from nil)
  11946. (let (tmp)
  11947. (save-excursion
  11948. (save-restriction
  11949. (widen)
  11950. (catch 'ex
  11951. (while t
  11952. (when (setq tmp (org-entry-get nil property))
  11953. (org-back-to-heading t)
  11954. (move-marker org-entry-property-inherited-from (point))
  11955. (throw 'ex tmp))
  11956. (or (org-up-heading-safe) (throw 'ex nil)))))
  11957. (or tmp
  11958. (cdr (assoc property org-file-properties))
  11959. (cdr (assoc property org-global-properties))
  11960. (cdr (assoc property org-global-properties-fixed))))))
  11961. (defvar org-property-changed-functions nil
  11962. "Hook called when the value of a property has changed.
  11963. Each hook function should accept two arguments, the name of the property
  11964. and the new value.")
  11965. (defun org-entry-put (pom property value)
  11966. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  11967. (org-with-point-at pom
  11968. (org-back-to-heading t)
  11969. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  11970. range)
  11971. (cond
  11972. ((equal property "TODO")
  11973. (when (and (stringp value) (string-match "\\S-" value)
  11974. (not (member value org-todo-keywords-1)))
  11975. (error "\"%s\" is not a valid TODO state" value))
  11976. (if (or (not value)
  11977. (not (string-match "\\S-" value)))
  11978. (setq value 'none))
  11979. (org-todo value)
  11980. (org-set-tags nil 'align))
  11981. ((equal property "PRIORITY")
  11982. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  11983. (string-to-char value) ?\ ))
  11984. (org-set-tags nil 'align))
  11985. ((equal property "SCHEDULED")
  11986. (if (re-search-forward org-scheduled-time-regexp end t)
  11987. (cond
  11988. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11989. ((eq value 'later) (org-timestamp-change 1 'day))
  11990. (t (call-interactively 'org-schedule)))
  11991. (call-interactively 'org-schedule)))
  11992. ((equal property "DEADLINE")
  11993. (if (re-search-forward org-deadline-time-regexp end t)
  11994. (cond
  11995. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11996. ((eq value 'later) (org-timestamp-change 1 'day))
  11997. (t (call-interactively 'org-deadline)))
  11998. (call-interactively 'org-deadline)))
  11999. ((member property org-special-properties)
  12000. (error "The %s property can not yet be set with `org-entry-put'"
  12001. property))
  12002. (t ; a non-special property
  12003. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  12004. (setq range (org-get-property-block beg end 'force))
  12005. (goto-char (car range))
  12006. (if (re-search-forward
  12007. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  12008. (progn
  12009. (delete-region (match-beginning 1) (match-end 1))
  12010. (goto-char (match-beginning 1)))
  12011. (goto-char (cdr range))
  12012. (insert "\n")
  12013. (backward-char 1)
  12014. (org-indent-line-function)
  12015. (insert ":" property ":"))
  12016. (and value (insert " " value))
  12017. (org-indent-line-function)))))
  12018. (run-hook-with-args 'org-property-changed-functions property value)))
  12019. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  12020. "Get all property keys in the current buffer.
  12021. With INCLUDE-SPECIALS, also list the special properties that reflect things
  12022. like tags and TODO state.
  12023. With INCLUDE-DEFAULTS, also include properties that has special meaning
  12024. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  12025. With INCLUDE-COLUMNS, also include property names given in COLUMN
  12026. formats in the current buffer."
  12027. (let (rtn range cfmt s p)
  12028. (save-excursion
  12029. (save-restriction
  12030. (widen)
  12031. (goto-char (point-min))
  12032. (while (re-search-forward org-property-start-re nil t)
  12033. (setq range (org-get-property-block))
  12034. (goto-char (car range))
  12035. (while (re-search-forward
  12036. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  12037. (cdr range) t)
  12038. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12039. (outline-next-heading))))
  12040. (when include-specials
  12041. (setq rtn (append org-special-properties rtn)))
  12042. (when include-defaults
  12043. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  12044. (add-to-list 'rtn org-effort-property))
  12045. (when include-columns
  12046. (save-excursion
  12047. (save-restriction
  12048. (widen)
  12049. (goto-char (point-min))
  12050. (while (re-search-forward
  12051. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  12052. nil t)
  12053. (setq cfmt (match-string 2) s 0)
  12054. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  12055. cfmt s)
  12056. (setq s (match-end 0)
  12057. p (match-string 1 cfmt))
  12058. (unless (or (equal p "ITEM")
  12059. (member p org-special-properties))
  12060. (add-to-list 'rtn (match-string 1 cfmt))))))))
  12061. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12062. (defun org-property-values (key)
  12063. "Return a list of all values of property KEY."
  12064. (save-excursion
  12065. (save-restriction
  12066. (widen)
  12067. (goto-char (point-min))
  12068. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  12069. values)
  12070. (while (re-search-forward re nil t)
  12071. (add-to-list 'values (org-trim (match-string 1))))
  12072. (delete "" values)))))
  12073. (defun org-insert-property-drawer ()
  12074. "Insert a property drawer into the current entry."
  12075. (interactive)
  12076. (org-back-to-heading t)
  12077. (looking-at outline-regexp)
  12078. (let ((indent (if org-adapt-indentation
  12079. (- (match-end 0)(match-beginning 0))
  12080. 0))
  12081. (beg (point))
  12082. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12083. end hiddenp)
  12084. (outline-next-heading)
  12085. (setq end (point))
  12086. (goto-char beg)
  12087. (while (re-search-forward re end t))
  12088. (setq hiddenp (org-invisible-p))
  12089. (end-of-line 1)
  12090. (and (equal (char-after) ?\n) (forward-char 1))
  12091. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  12092. (if (member (match-string 1) '("CLOCK:" ":END:"))
  12093. ;; just skip this line
  12094. (beginning-of-line 2)
  12095. ;; Drawer start, find the end
  12096. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  12097. (beginning-of-line 1)))
  12098. (org-skip-over-state-notes)
  12099. (skip-chars-backward " \t\n\r")
  12100. (if (eq (char-before) ?*) (forward-char 1))
  12101. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  12102. (beginning-of-line 0)
  12103. (org-indent-to-column indent)
  12104. (beginning-of-line 2)
  12105. (org-indent-to-column indent)
  12106. (beginning-of-line 0)
  12107. (if hiddenp
  12108. (save-excursion
  12109. (org-back-to-heading t)
  12110. (hide-entry))
  12111. (org-flag-drawer t))))
  12112. (defun org-set-property (property value)
  12113. "In the current entry, set PROPERTY to VALUE.
  12114. When called interactively, this will prompt for a property name, offering
  12115. completion on existing and default properties. And then it will prompt
  12116. for a value, offering completion either on allowed values (via an inherited
  12117. xxx_ALL property) or on existing values in other instances of this property
  12118. in the current file."
  12119. (interactive
  12120. (let* ((completion-ignore-case t)
  12121. (keys (org-buffer-property-keys nil t t))
  12122. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  12123. (prop (if (member prop0 keys)
  12124. prop0
  12125. (or (cdr (assoc (downcase prop0)
  12126. (mapcar (lambda (x) (cons (downcase x) x))
  12127. keys)))
  12128. prop0)))
  12129. (cur (org-entry-get nil prop))
  12130. (prompt (concat prop " value"
  12131. (if (and cur (string-match "\\S-" cur))
  12132. (concat " [" cur "]") "") ": "))
  12133. (allowed (org-property-get-allowed-values nil prop 'table))
  12134. (existing (mapcar 'list (org-property-values prop)))
  12135. (val (if allowed
  12136. (org-completing-read prompt allowed nil
  12137. (not (get-text-property 0 'org-unrestricted
  12138. (caar allowed))))
  12139. (let (org-completion-use-ido org-completion-use-iswitchb)
  12140. (org-completing-read prompt existing nil nil "" nil cur)))))
  12141. (list prop (if (equal val "") cur val))))
  12142. (unless (equal (org-entry-get nil property) value)
  12143. (org-entry-put nil property value)))
  12144. (defun org-delete-property (property)
  12145. "In the current entry, delete PROPERTY."
  12146. (interactive
  12147. (let* ((completion-ignore-case t)
  12148. (prop (org-icompleting-read "Property: " (org-entry-properties nil 'standard))))
  12149. (list prop)))
  12150. (message "Property %s %s" property
  12151. (if (org-entry-delete nil property)
  12152. "deleted"
  12153. "was not present in the entry")))
  12154. (defun org-delete-property-globally (property)
  12155. "Remove PROPERTY globally, from all entries."
  12156. (interactive
  12157. (let* ((completion-ignore-case t)
  12158. (prop (org-icompleting-read
  12159. "Globally remove property: "
  12160. (mapcar 'list (org-buffer-property-keys)))))
  12161. (list prop)))
  12162. (save-excursion
  12163. (save-restriction
  12164. (widen)
  12165. (goto-char (point-min))
  12166. (let ((cnt 0))
  12167. (while (re-search-forward
  12168. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  12169. nil t)
  12170. (setq cnt (1+ cnt))
  12171. (replace-match ""))
  12172. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12173. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  12174. (defun org-compute-property-at-point ()
  12175. "Compute the property at point.
  12176. This looks for an enclosing column format, extracts the operator and
  12177. then applies it to the property in the column format's scope."
  12178. (interactive)
  12179. (unless (org-at-property-p)
  12180. (error "Not at a property"))
  12181. (let ((prop (org-match-string-no-properties 2)))
  12182. (org-columns-get-format-and-top-level)
  12183. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12184. (error "No operator defined for property %s" prop))
  12185. (org-columns-compute prop)))
  12186. (defvar org-property-allowed-value-functions nil
  12187. "Hook for functions supplying allowed values for a specific property.
  12188. The functions must take a single argument, the name of the property, and
  12189. return a flat list of allowed values. If \":ETC\" is one of
  12190. the values, this means that these values are intended as defaults for
  12191. completion, but that other values should be allowed too.
  12192. The functions must return nil if they are not responsible for this
  12193. property.")
  12194. (defun org-property-get-allowed-values (pom property &optional table)
  12195. "Get allowed values for the property PROPERTY.
  12196. When TABLE is non-nil, return an alist that can directly be used for
  12197. completion."
  12198. (let (vals)
  12199. (cond
  12200. ((equal property "TODO")
  12201. (setq vals (org-with-point-at pom
  12202. (append org-todo-keywords-1 '("")))))
  12203. ((equal property "PRIORITY")
  12204. (let ((n org-lowest-priority))
  12205. (while (>= n org-highest-priority)
  12206. (push (char-to-string n) vals)
  12207. (setq n (1- n)))))
  12208. ((member property org-special-properties))
  12209. ((setq vals (run-hook-with-args-until-success
  12210. 'org-property-allowed-value-functions property)))
  12211. (t
  12212. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12213. (when (and vals (string-match "\\S-" vals))
  12214. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12215. (setq vals (mapcar (lambda (x)
  12216. (cond ((stringp x) x)
  12217. ((numberp x) (number-to-string x))
  12218. ((symbolp x) (symbol-name x))
  12219. (t "???")))
  12220. vals)))))
  12221. (when (member ":ETC" vals)
  12222. (setq vals (remove ":ETC" vals))
  12223. (org-add-props (car vals) '(org-unrestricted t)))
  12224. (if table (mapcar 'list vals) vals)))
  12225. (defun org-property-previous-allowed-value (&optional previous)
  12226. "Switch to the next allowed value for this property."
  12227. (interactive)
  12228. (org-property-next-allowed-value t))
  12229. (defun org-property-next-allowed-value (&optional previous)
  12230. "Switch to the next allowed value for this property."
  12231. (interactive)
  12232. (unless (org-at-property-p)
  12233. (error "Not at a property"))
  12234. (let* ((key (match-string 2))
  12235. (value (match-string 3))
  12236. (allowed (or (org-property-get-allowed-values (point) key)
  12237. (and (member value '("[ ]" "[-]" "[X]"))
  12238. '("[ ]" "[X]"))))
  12239. nval)
  12240. (unless allowed
  12241. (error "Allowed values for this property have not been defined"))
  12242. (if previous (setq allowed (reverse allowed)))
  12243. (if (member value allowed)
  12244. (setq nval (car (cdr (member value allowed)))))
  12245. (setq nval (or nval (car allowed)))
  12246. (if (equal nval value)
  12247. (error "Only one allowed value for this property"))
  12248. (org-at-property-p)
  12249. (replace-match (concat " :" key ": " nval) t t)
  12250. (org-indent-line-function)
  12251. (beginning-of-line 1)
  12252. (skip-chars-forward " \t")
  12253. (run-hook-with-args 'org-property-changed-functions key nval)))
  12254. (defun org-find-entry-with-id (ident)
  12255. "Locate the entry that contains the ID property with exact value IDENT.
  12256. IDENT can be a string, a symbol or a number, this function will search for
  12257. the string representation of it.
  12258. Return the position where this entry starts, or nil if there is no such entry."
  12259. (interactive "sID: ")
  12260. (let ((id (cond
  12261. ((stringp ident) ident)
  12262. ((symbol-name ident) (symbol-name ident))
  12263. ((numberp ident) (number-to-string ident))
  12264. (t (error "IDENT %s must be a string, symbol or number" ident))))
  12265. (case-fold-search nil))
  12266. (save-excursion
  12267. (save-restriction
  12268. (widen)
  12269. (goto-char (point-min))
  12270. (when (re-search-forward
  12271. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  12272. nil t)
  12273. (org-back-to-heading t)
  12274. (point))))))
  12275. ;;;; Timestamps
  12276. (defvar org-last-changed-timestamp nil)
  12277. (defvar org-last-inserted-timestamp nil
  12278. "The last time stamp inserted with `org-insert-time-stamp'.")
  12279. (defvar org-time-was-given) ; dynamically scoped parameter
  12280. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12281. (defvar org-ts-what) ; dynamically scoped parameter
  12282. (defun org-time-stamp (arg &optional inactive)
  12283. "Prompt for a date/time and insert a time stamp.
  12284. If the user specifies a time like HH:MM, or if this command is called
  12285. with a prefix argument, the time stamp will contain date and time.
  12286. Otherwise, only the date will be included. All parts of a date not
  12287. specified by the user will be filled in from the current date/time.
  12288. So if you press just return without typing anything, the time stamp
  12289. will represent the current date/time. If there is already a timestamp
  12290. at the cursor, it will be modified."
  12291. (interactive "P")
  12292. (let* ((ts nil)
  12293. (default-time
  12294. ;; Default time is either today, or, when entering a range,
  12295. ;; the range start.
  12296. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  12297. (save-excursion
  12298. (re-search-backward
  12299. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  12300. (- (point) 20) t)))
  12301. (apply 'encode-time (org-parse-time-string (match-string 1)))
  12302. (current-time)))
  12303. (default-input (and ts (org-get-compact-tod ts)))
  12304. org-time-was-given org-end-time-was-given time)
  12305. (cond
  12306. ((and (org-at-timestamp-p t)
  12307. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  12308. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  12309. (insert "--")
  12310. (setq time (let ((this-command this-command))
  12311. (org-read-date arg 'totime nil nil
  12312. default-time default-input)))
  12313. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  12314. ((org-at-timestamp-p t)
  12315. (setq time (let ((this-command this-command))
  12316. (org-read-date arg 'totime nil nil default-time default-input)))
  12317. (when (org-at-timestamp-p t) ; just to get the match data
  12318. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  12319. (replace-match "")
  12320. (setq org-last-changed-timestamp
  12321. (org-insert-time-stamp
  12322. time (or org-time-was-given arg)
  12323. inactive nil nil (list org-end-time-was-given))))
  12324. (message "Timestamp updated"))
  12325. (t
  12326. (setq time (let ((this-command this-command))
  12327. (org-read-date arg 'totime nil nil default-time default-input)))
  12328. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  12329. nil nil (list org-end-time-was-given))))))
  12330. ;; FIXME: can we use this for something else, like computing time differences?
  12331. (defun org-get-compact-tod (s)
  12332. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  12333. (let* ((t1 (match-string 1 s))
  12334. (h1 (string-to-number (match-string 2 s)))
  12335. (m1 (string-to-number (match-string 3 s)))
  12336. (t2 (and (match-end 4) (match-string 5 s)))
  12337. (h2 (and t2 (string-to-number (match-string 6 s))))
  12338. (m2 (and t2 (string-to-number (match-string 7 s))))
  12339. dh dm)
  12340. (if (not t2)
  12341. t1
  12342. (setq dh (- h2 h1) dm (- m2 m1))
  12343. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  12344. (concat t1 "+" (number-to-string dh)
  12345. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  12346. (defun org-time-stamp-inactive (&optional arg)
  12347. "Insert an inactive time stamp.
  12348. An inactive time stamp is enclosed in square brackets instead of angle
  12349. brackets. It is inactive in the sense that it does not trigger agenda entries,
  12350. does not link to the calendar and cannot be changed with the S-cursor keys.
  12351. So these are more for recording a certain time/date."
  12352. (interactive "P")
  12353. (org-time-stamp arg 'inactive))
  12354. (defvar org-date-ovl (make-overlay 1 1))
  12355. (overlay-put org-date-ovl 'face 'org-warning)
  12356. (org-detach-overlay org-date-ovl)
  12357. (defvar org-ans1) ; dynamically scoped parameter
  12358. (defvar org-ans2) ; dynamically scoped parameter
  12359. (defvar org-plain-time-of-day-regexp) ; defined below
  12360. (defvar org-overriding-default-time nil) ; dynamically scoped
  12361. (defvar org-read-date-overlay nil)
  12362. (defvar org-dcst nil) ; dynamically scoped
  12363. (defvar org-read-date-history nil)
  12364. (defvar org-read-date-final-answer nil)
  12365. (defun org-read-date (&optional with-time to-time from-string prompt
  12366. default-time default-input)
  12367. "Read a date, possibly a time, and make things smooth for the user.
  12368. The prompt will suggest to enter an ISO date, but you can also enter anything
  12369. which will at least partially be understood by `parse-time-string'.
  12370. Unrecognized parts of the date will default to the current day, month, year,
  12371. hour and minute. If this command is called to replace a timestamp at point,
  12372. of to enter the second timestamp of a range, the default time is taken from the
  12373. existing stamp. For example,
  12374. 3-2-5 --> 2003-02-05
  12375. feb 15 --> currentyear-02-15
  12376. sep 12 9 --> 2009-09-12
  12377. 12:45 --> today 12:45
  12378. 22 sept 0:34 --> currentyear-09-22 0:34
  12379. 12 --> currentyear-currentmonth-12
  12380. Fri --> nearest Friday (today or later)
  12381. etc.
  12382. Furthermore you can specify a relative date by giving, as the *first* thing
  12383. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  12384. change in days weeks, months, years.
  12385. With a single plus or minus, the date is relative to today. With a double
  12386. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  12387. +4d --> four days from today
  12388. +4 --> same as above
  12389. +2w --> two weeks from today
  12390. ++5 --> five days from default date
  12391. The function understands only English month and weekday abbreviations,
  12392. but this can be configured with the variables `parse-time-months' and
  12393. `parse-time-weekdays'.
  12394. While prompting, a calendar is popped up - you can also select the
  12395. date with the mouse (button 1). The calendar shows a period of three
  12396. months. To scroll it to other months, use the keys `>' and `<'.
  12397. If you don't like the calendar, turn it off with
  12398. \(setq org-read-date-popup-calendar nil)
  12399. With optional argument TO-TIME, the date will immediately be converted
  12400. to an internal time.
  12401. With an optional argument WITH-TIME, the prompt will suggest to also
  12402. insert a time. Note that when WITH-TIME is not set, you can still
  12403. enter a time, and this function will inform the calling routine about
  12404. this change. The calling routine may then choose to change the format
  12405. used to insert the time stamp into the buffer to include the time.
  12406. With optional argument FROM-STRING, read from this string instead from
  12407. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  12408. the time/date that is used for everything that is not specified by the
  12409. user."
  12410. (require 'parse-time)
  12411. (let* ((org-time-stamp-rounding-minutes
  12412. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  12413. (org-dcst org-display-custom-times)
  12414. (ct (org-current-time))
  12415. (def (or org-overriding-default-time default-time ct))
  12416. (defdecode (decode-time def))
  12417. (dummy (progn
  12418. (when (< (nth 2 defdecode) org-extend-today-until)
  12419. (setcar (nthcdr 2 defdecode) -1)
  12420. (setcar (nthcdr 1 defdecode) 59)
  12421. (setq def (apply 'encode-time defdecode)
  12422. defdecode (decode-time def)))))
  12423. (calendar-frame-setup nil)
  12424. (calendar-move-hook nil)
  12425. (calendar-view-diary-initially-flag nil)
  12426. (calendar-view-holidays-initially-flag nil)
  12427. (timestr (format-time-string
  12428. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  12429. (prompt (concat (if prompt (concat prompt " ") "")
  12430. (format "Date+time [%s]: " timestr)))
  12431. ans (org-ans0 "") org-ans1 org-ans2 final)
  12432. (cond
  12433. (from-string (setq ans from-string))
  12434. (org-read-date-popup-calendar
  12435. (save-excursion
  12436. (save-window-excursion
  12437. (calendar)
  12438. (calendar-forward-day (- (time-to-days def)
  12439. (calendar-absolute-from-gregorian
  12440. (calendar-current-date))))
  12441. (org-eval-in-calendar nil t)
  12442. (let* ((old-map (current-local-map))
  12443. (map (copy-keymap calendar-mode-map))
  12444. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  12445. (org-defkey map (kbd "RET") 'org-calendar-select)
  12446. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  12447. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  12448. (org-defkey minibuffer-local-map [(meta shift left)]
  12449. (lambda () (interactive)
  12450. (org-eval-in-calendar '(calendar-backward-month 1))))
  12451. (org-defkey minibuffer-local-map [(meta shift right)]
  12452. (lambda () (interactive)
  12453. (org-eval-in-calendar '(calendar-forward-month 1))))
  12454. (org-defkey minibuffer-local-map [(meta shift up)]
  12455. (lambda () (interactive)
  12456. (org-eval-in-calendar '(calendar-backward-year 1))))
  12457. (org-defkey minibuffer-local-map [(meta shift down)]
  12458. (lambda () (interactive)
  12459. (org-eval-in-calendar '(calendar-forward-year 1))))
  12460. (org-defkey minibuffer-local-map [?\e (shift left)]
  12461. (lambda () (interactive)
  12462. (org-eval-in-calendar '(calendar-backward-month 1))))
  12463. (org-defkey minibuffer-local-map [?\e (shift right)]
  12464. (lambda () (interactive)
  12465. (org-eval-in-calendar '(calendar-forward-month 1))))
  12466. (org-defkey minibuffer-local-map [?\e (shift up)]
  12467. (lambda () (interactive)
  12468. (org-eval-in-calendar '(calendar-backward-year 1))))
  12469. (org-defkey minibuffer-local-map [?\e (shift down)]
  12470. (lambda () (interactive)
  12471. (org-eval-in-calendar '(calendar-forward-year 1))))
  12472. (org-defkey minibuffer-local-map [(shift up)]
  12473. (lambda () (interactive)
  12474. (org-eval-in-calendar '(calendar-backward-week 1))))
  12475. (org-defkey minibuffer-local-map [(shift down)]
  12476. (lambda () (interactive)
  12477. (org-eval-in-calendar '(calendar-forward-week 1))))
  12478. (org-defkey minibuffer-local-map [(shift left)]
  12479. (lambda () (interactive)
  12480. (org-eval-in-calendar '(calendar-backward-day 1))))
  12481. (org-defkey minibuffer-local-map [(shift right)]
  12482. (lambda () (interactive)
  12483. (org-eval-in-calendar '(calendar-forward-day 1))))
  12484. (org-defkey minibuffer-local-map ">"
  12485. (lambda () (interactive)
  12486. (org-eval-in-calendar '(scroll-calendar-left 1))))
  12487. (org-defkey minibuffer-local-map "<"
  12488. (lambda () (interactive)
  12489. (org-eval-in-calendar '(scroll-calendar-right 1))))
  12490. (run-hooks 'org-read-date-minibuffer-setup-hook)
  12491. (unwind-protect
  12492. (progn
  12493. (use-local-map map)
  12494. (add-hook 'post-command-hook 'org-read-date-display)
  12495. (setq org-ans0 (read-string prompt default-input
  12496. 'org-read-date-history nil))
  12497. ;; org-ans0: from prompt
  12498. ;; org-ans1: from mouse click
  12499. ;; org-ans2: from calendar motion
  12500. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  12501. (remove-hook 'post-command-hook 'org-read-date-display)
  12502. (use-local-map old-map)
  12503. (when org-read-date-overlay
  12504. (delete-overlay org-read-date-overlay)
  12505. (setq org-read-date-overlay nil)))))))
  12506. (t ; Naked prompt only
  12507. (unwind-protect
  12508. (setq ans (read-string prompt default-input
  12509. 'org-read-date-history timestr))
  12510. (when org-read-date-overlay
  12511. (delete-overlay org-read-date-overlay)
  12512. (setq org-read-date-overlay nil)))))
  12513. (setq final (org-read-date-analyze ans def defdecode))
  12514. (setq org-read-date-final-answer ans)
  12515. (if to-time
  12516. (apply 'encode-time final)
  12517. (if (and (boundp 'org-time-was-given) org-time-was-given)
  12518. (format "%04d-%02d-%02d %02d:%02d"
  12519. (nth 5 final) (nth 4 final) (nth 3 final)
  12520. (nth 2 final) (nth 1 final))
  12521. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  12522. (defvar def)
  12523. (defvar defdecode)
  12524. (defvar with-time)
  12525. (defvar org-read-date-analyze-futurep nil)
  12526. (defun org-read-date-display ()
  12527. "Display the current date prompt interpretation in the minibuffer."
  12528. (when org-read-date-display-live
  12529. (when org-read-date-overlay
  12530. (delete-overlay org-read-date-overlay))
  12531. (let ((p (point)))
  12532. (end-of-line 1)
  12533. (while (not (equal (buffer-substring
  12534. (max (point-min) (- (point) 4)) (point))
  12535. " "))
  12536. (insert " "))
  12537. (goto-char p))
  12538. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  12539. " " (or org-ans1 org-ans2)))
  12540. (org-end-time-was-given nil)
  12541. (f (org-read-date-analyze ans def defdecode))
  12542. (fmts (if org-dcst
  12543. org-time-stamp-custom-formats
  12544. org-time-stamp-formats))
  12545. (fmt (if (or with-time
  12546. (and (boundp 'org-time-was-given) org-time-was-given))
  12547. (cdr fmts)
  12548. (car fmts)))
  12549. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  12550. (when (and org-end-time-was-given
  12551. (string-match org-plain-time-of-day-regexp txt))
  12552. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  12553. org-end-time-was-given
  12554. (substring txt (match-end 0)))))
  12555. (when org-read-date-analyze-futurep
  12556. (setq txt (concat txt " (=>F)")))
  12557. (setq org-read-date-overlay
  12558. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  12559. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  12560. (defun org-read-date-analyze (ans def defdecode)
  12561. "Analyse the combined answer of the date prompt."
  12562. ;; FIXME: cleanup and comment
  12563. (let ((nowdecode (decode-time (current-time)))
  12564. delta deltan deltaw deltadef year month day
  12565. hour minute second wday pm h2 m2 tl wday1
  12566. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  12567. (setq org-read-date-analyze-futurep nil)
  12568. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  12569. (setq ans "+0"))
  12570. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  12571. (setq ans (replace-match "" t t ans)
  12572. deltan (car delta)
  12573. deltaw (nth 1 delta)
  12574. deltadef (nth 2 delta)))
  12575. ;; Check if there is an iso week date in there
  12576. ;; If yes, store the info and postpone interpreting it until the rest
  12577. ;; of the parsing is done
  12578. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  12579. (setq iso-year (if (match-end 1)
  12580. (org-small-year-to-year
  12581. (string-to-number (match-string 1 ans))))
  12582. iso-weekday (if (match-end 3)
  12583. (string-to-number (match-string 3 ans)))
  12584. iso-week (string-to-number (match-string 2 ans)))
  12585. (setq ans (replace-match "" t t ans)))
  12586. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  12587. (when (string-match
  12588. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  12589. (setq year (if (match-end 2)
  12590. (string-to-number (match-string 2 ans))
  12591. (progn (setq kill-year t)
  12592. (string-to-number (format-time-string "%Y"))))
  12593. month (string-to-number (match-string 3 ans))
  12594. day (string-to-number (match-string 4 ans)))
  12595. (if (< year 100) (setq year (+ 2000 year)))
  12596. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12597. t nil ans)))
  12598. ;; Help matching american dates, like 5/30 or 5/30/7
  12599. (when (string-match
  12600. "^ *\\([0-3]?[0-9]\\)/\\([0-1]?[0-9]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  12601. (setq year (if (match-end 4)
  12602. (string-to-number (match-string 4 ans))
  12603. (progn (setq kill-year t)
  12604. (string-to-number (format-time-string "%Y"))))
  12605. month (string-to-number (match-string 1 ans))
  12606. day (string-to-number (match-string 2 ans)))
  12607. (if (< year 100) (setq year (+ 2000 year)))
  12608. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12609. t nil ans)))
  12610. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  12611. ;; If there is a time with am/pm, and *no* time without it, we convert
  12612. ;; so that matching will be successful.
  12613. (loop for i from 1 to 2 do ; twice, for end time as well
  12614. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  12615. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  12616. (setq hour (string-to-number (match-string 1 ans))
  12617. minute (if (match-end 3)
  12618. (string-to-number (match-string 3 ans))
  12619. 0)
  12620. pm (equal ?p
  12621. (string-to-char (downcase (match-string 4 ans)))))
  12622. (if (and (= hour 12) (not pm))
  12623. (setq hour 0)
  12624. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  12625. (setq ans (replace-match (format "%02d:%02d" hour minute)
  12626. t t ans))))
  12627. ;; Check if a time range is given as a duration
  12628. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  12629. (setq hour (string-to-number (match-string 1 ans))
  12630. h2 (+ hour (string-to-number (match-string 3 ans)))
  12631. minute (string-to-number (match-string 2 ans))
  12632. m2 (+ minute (if (match-end 5) (string-to-number
  12633. (match-string 5 ans))0)))
  12634. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  12635. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  12636. t t ans)))
  12637. ;; Check if there is a time range
  12638. (when (boundp 'org-end-time-was-given)
  12639. (setq org-time-was-given nil)
  12640. (when (and (string-match org-plain-time-of-day-regexp ans)
  12641. (match-end 8))
  12642. (setq org-end-time-was-given (match-string 8 ans))
  12643. (setq ans (concat (substring ans 0 (match-beginning 7))
  12644. (substring ans (match-end 7))))))
  12645. (setq tl (parse-time-string ans)
  12646. day (or (nth 3 tl) (nth 3 defdecode))
  12647. month (or (nth 4 tl)
  12648. (if (and org-read-date-prefer-future
  12649. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  12650. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  12651. (nth 4 defdecode)))
  12652. year (or (and (not kill-year) (nth 5 tl))
  12653. (if (and org-read-date-prefer-future
  12654. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  12655. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  12656. (nth 5 defdecode)))
  12657. hour (or (nth 2 tl) (nth 2 defdecode))
  12658. minute (or (nth 1 tl) (nth 1 defdecode))
  12659. second (or (nth 0 tl) 0)
  12660. wday (nth 6 tl))
  12661. (when (and (eq org-read-date-prefer-future 'time)
  12662. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  12663. (equal day (nth 3 nowdecode))
  12664. (equal month (nth 4 nowdecode))
  12665. (equal year (nth 5 nowdecode))
  12666. (nth 2 tl)
  12667. (or (< (nth 2 tl) (nth 2 nowdecode))
  12668. (and (= (nth 2 tl) (nth 2 nowdecode))
  12669. (nth 1 tl)
  12670. (< (nth 1 tl) (nth 1 nowdecode)))))
  12671. (setq day (1+ day)
  12672. futurep t))
  12673. ;; Special date definitions below
  12674. (cond
  12675. (iso-week
  12676. ;; There was an iso week
  12677. (require 'cal-iso)
  12678. (setq futurep nil)
  12679. (setq year (or iso-year year)
  12680. day (or iso-weekday wday 1)
  12681. wday nil ; to make sure that the trigger below does not match
  12682. iso-date (calendar-gregorian-from-absolute
  12683. (calendar-absolute-from-iso
  12684. (list iso-week day year))))
  12685. ; FIXME: Should we also push ISO weeks into the future?
  12686. ; (when (and org-read-date-prefer-future
  12687. ; (not iso-year)
  12688. ; (< (calendar-absolute-from-gregorian iso-date)
  12689. ; (time-to-days (current-time))))
  12690. ; (setq year (1+ year)
  12691. ; iso-date (calendar-gregorian-from-absolute
  12692. ; (calendar-absolute-from-iso
  12693. ; (list iso-week day year)))))
  12694. (setq month (car iso-date)
  12695. year (nth 2 iso-date)
  12696. day (nth 1 iso-date)))
  12697. (deltan
  12698. (setq futurep nil)
  12699. (unless deltadef
  12700. (let ((now (decode-time (current-time))))
  12701. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  12702. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  12703. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  12704. ((equal deltaw "m") (setq month (+ month deltan)))
  12705. ((equal deltaw "y") (setq year (+ year deltan)))))
  12706. ((and wday (not (nth 3 tl)))
  12707. (setq futurep nil)
  12708. ;; Weekday was given, but no day, so pick that day in the week
  12709. ;; on or after the derived date.
  12710. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  12711. (unless (equal wday wday1)
  12712. (setq day (+ day (% (- wday wday1 -7) 7))))))
  12713. (if (and (boundp 'org-time-was-given)
  12714. (nth 2 tl))
  12715. (setq org-time-was-given t))
  12716. (if (< year 100) (setq year (+ 2000 year)))
  12717. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  12718. (setq org-read-date-analyze-futurep futurep)
  12719. (list second minute hour day month year)))
  12720. (defvar parse-time-weekdays)
  12721. (defun org-read-date-get-relative (s today default)
  12722. "Check string S for special relative date string.
  12723. TODAY and DEFAULT are internal times, for today and for a default.
  12724. Return shift list (N what def-flag)
  12725. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  12726. N is the number of WHATs to shift.
  12727. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  12728. the DEFAULT date rather than TODAY."
  12729. (when (and
  12730. (string-match
  12731. (concat
  12732. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  12733. "\\([0-9]+\\)?"
  12734. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  12735. "\\([ \t]\\|$\\)") s)
  12736. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  12737. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  12738. (string-to-char (substring (match-string 1 s) -1))
  12739. ?+))
  12740. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  12741. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  12742. (what (if (match-end 3) (match-string 3 s) "d"))
  12743. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  12744. (date (if rel default today))
  12745. (wday (nth 6 (decode-time date)))
  12746. delta)
  12747. (if wday1
  12748. (progn
  12749. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  12750. (if (= dir ?-) (setq delta (- delta 7)))
  12751. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  12752. (list delta "d" rel))
  12753. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  12754. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  12755. "Turn a user-specified date into the internal representation.
  12756. The internal representation needed by the calendar is (month day year).
  12757. This is a wrapper to handle the brain-dead convention in calendar that
  12758. user function argument order change dependent on argument order."
  12759. (if (boundp 'calendar-date-style)
  12760. (cond
  12761. ((eq calendar-date-style 'american)
  12762. (list arg1 arg2 arg3))
  12763. ((eq calendar-date-style 'european)
  12764. (list arg2 arg1 arg3))
  12765. ((eq calendar-date-style 'iso)
  12766. (list arg2 arg3 arg1)))
  12767. (if (org-bound-and-true-p european-calendar-style)
  12768. (list arg2 arg1 arg3)
  12769. (list arg1 arg2 arg3))))
  12770. (defun org-eval-in-calendar (form &optional keepdate)
  12771. "Eval FORM in the calendar window and return to current window.
  12772. Also, store the cursor date in variable org-ans2."
  12773. (let ((sf (selected-frame))
  12774. (sw (selected-window)))
  12775. (select-window (get-buffer-window "*Calendar*" t))
  12776. (eval form)
  12777. (when (and (not keepdate) (calendar-cursor-to-date))
  12778. (let* ((date (calendar-cursor-to-date))
  12779. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12780. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  12781. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  12782. (select-window sw)
  12783. (org-select-frame-set-input-focus sf)))
  12784. (defun org-calendar-select ()
  12785. "Return to `org-read-date' with the date currently selected.
  12786. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12787. (interactive)
  12788. (when (calendar-cursor-to-date)
  12789. (let* ((date (calendar-cursor-to-date))
  12790. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12791. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12792. (if (active-minibuffer-window) (exit-minibuffer))))
  12793. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  12794. "Insert a date stamp for the date given by the internal TIME.
  12795. WITH-HM means use the stamp format that includes the time of the day.
  12796. INACTIVE means use square brackets instead of angular ones, so that the
  12797. stamp will not contribute to the agenda.
  12798. PRE and POST are optional strings to be inserted before and after the
  12799. stamp.
  12800. The command returns the inserted time stamp."
  12801. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  12802. stamp)
  12803. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  12804. (insert-before-markers (or pre ""))
  12805. (insert-before-markers (setq stamp (format-time-string fmt time)))
  12806. (when (listp extra)
  12807. (setq extra (car extra))
  12808. (if (and (stringp extra)
  12809. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  12810. (setq extra (format "-%02d:%02d"
  12811. (string-to-number (match-string 1 extra))
  12812. (string-to-number (match-string 2 extra))))
  12813. (setq extra nil)))
  12814. (when extra
  12815. (backward-char 1)
  12816. (insert-before-markers extra)
  12817. (forward-char 1))
  12818. (insert-before-markers (or post ""))
  12819. (setq org-last-inserted-timestamp stamp)))
  12820. (defun org-toggle-time-stamp-overlays ()
  12821. "Toggle the use of custom time stamp formats."
  12822. (interactive)
  12823. (setq org-display-custom-times (not org-display-custom-times))
  12824. (unless org-display-custom-times
  12825. (let ((p (point-min)) (bmp (buffer-modified-p)))
  12826. (while (setq p (next-single-property-change p 'display))
  12827. (if (and (get-text-property p 'display)
  12828. (eq (get-text-property p 'face) 'org-date))
  12829. (remove-text-properties
  12830. p (setq p (next-single-property-change p 'display))
  12831. '(display t))))
  12832. (set-buffer-modified-p bmp)))
  12833. (if (featurep 'xemacs)
  12834. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  12835. (org-restart-font-lock)
  12836. (setq org-table-may-need-update t)
  12837. (if org-display-custom-times
  12838. (message "Time stamps are overlayed with custom format")
  12839. (message "Time stamp overlays removed")))
  12840. (defun org-display-custom-time (beg end)
  12841. "Overlay modified time stamp format over timestamp between BEG and END."
  12842. (let* ((ts (buffer-substring beg end))
  12843. t1 w1 with-hm tf time str w2 (off 0))
  12844. (save-match-data
  12845. (setq t1 (org-parse-time-string ts t))
  12846. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  12847. (setq off (- (match-end 0) (match-beginning 0)))))
  12848. (setq end (- end off))
  12849. (setq w1 (- end beg)
  12850. with-hm (and (nth 1 t1) (nth 2 t1))
  12851. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  12852. time (org-fix-decoded-time t1)
  12853. str (org-add-props
  12854. (format-time-string
  12855. (substring tf 1 -1) (apply 'encode-time time))
  12856. nil 'mouse-face 'highlight)
  12857. w2 (length str))
  12858. (if (not (= w2 w1))
  12859. (add-text-properties (1+ beg) (+ 2 beg)
  12860. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  12861. (if (featurep 'xemacs)
  12862. (progn
  12863. (put-text-property beg end 'invisible t)
  12864. (put-text-property beg end 'end-glyph (make-glyph str)))
  12865. (put-text-property beg end 'display str))))
  12866. (defun org-translate-time (string)
  12867. "Translate all timestamps in STRING to custom format.
  12868. But do this only if the variable `org-display-custom-times' is set."
  12869. (when org-display-custom-times
  12870. (save-match-data
  12871. (let* ((start 0)
  12872. (re org-ts-regexp-both)
  12873. t1 with-hm inactive tf time str beg end)
  12874. (while (setq start (string-match re string start))
  12875. (setq beg (match-beginning 0)
  12876. end (match-end 0)
  12877. t1 (save-match-data
  12878. (org-parse-time-string (substring string beg end) t))
  12879. with-hm (and (nth 1 t1) (nth 2 t1))
  12880. inactive (equal (substring string beg (1+ beg)) "[")
  12881. tf (funcall (if with-hm 'cdr 'car)
  12882. org-time-stamp-custom-formats)
  12883. time (org-fix-decoded-time t1)
  12884. str (format-time-string
  12885. (concat
  12886. (if inactive "[" "<") (substring tf 1 -1)
  12887. (if inactive "]" ">"))
  12888. (apply 'encode-time time))
  12889. string (replace-match str t t string)
  12890. start (+ start (length str)))))))
  12891. string)
  12892. (defun org-fix-decoded-time (time)
  12893. "Set 0 instead of nil for the first 6 elements of time.
  12894. Don't touch the rest."
  12895. (let ((n 0))
  12896. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  12897. (defun org-days-to-time (timestamp-string)
  12898. "Difference between TIMESTAMP-STRING and now in days."
  12899. (- (time-to-days (org-time-string-to-time timestamp-string))
  12900. (time-to-days (current-time))))
  12901. (defun org-deadline-close (timestamp-string &optional ndays)
  12902. "Is the time in TIMESTAMP-STRING close to the current date?"
  12903. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  12904. (and (< (org-days-to-time timestamp-string) ndays)
  12905. (not (org-entry-is-done-p))))
  12906. (defun org-get-wdays (ts)
  12907. "Get the deadline lead time appropriate for timestring TS."
  12908. (cond
  12909. ((<= org-deadline-warning-days 0)
  12910. ;; 0 or negative, enforce this value no matter what
  12911. (- org-deadline-warning-days))
  12912. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  12913. ;; lead time is specified.
  12914. (floor (* (string-to-number (match-string 1 ts))
  12915. (cdr (assoc (match-string 2 ts)
  12916. '(("d" . 1) ("w" . 7)
  12917. ("m" . 30.4) ("y" . 365.25)))))))
  12918. ;; go for the default.
  12919. (t org-deadline-warning-days)))
  12920. (defun org-calendar-select-mouse (ev)
  12921. "Return to `org-read-date' with the date currently selected.
  12922. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12923. (interactive "e")
  12924. (mouse-set-point ev)
  12925. (when (calendar-cursor-to-date)
  12926. (let* ((date (calendar-cursor-to-date))
  12927. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12928. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12929. (if (active-minibuffer-window) (exit-minibuffer))))
  12930. (defun org-check-deadlines (ndays)
  12931. "Check if there are any deadlines due or past due.
  12932. A deadline is considered due if it happens within `org-deadline-warning-days'
  12933. days from today's date. If the deadline appears in an entry marked DONE,
  12934. it is not shown. The prefix arg NDAYS can be used to test that many
  12935. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  12936. (interactive "P")
  12937. (let* ((org-warn-days
  12938. (cond
  12939. ((equal ndays '(4)) 100000)
  12940. (ndays (prefix-numeric-value ndays))
  12941. (t (abs org-deadline-warning-days))))
  12942. (case-fold-search nil)
  12943. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  12944. (callback
  12945. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  12946. (message "%d deadlines past-due or due within %d days"
  12947. (org-occur regexp nil callback)
  12948. org-warn-days)))
  12949. (defun org-check-before-date (date)
  12950. "Check if there are deadlines or scheduled entries before DATE."
  12951. (interactive (list (org-read-date)))
  12952. (let ((case-fold-search nil)
  12953. (regexp (concat "\\<\\(" org-deadline-string
  12954. "\\|" org-scheduled-string
  12955. "\\) *<\\([^>]+\\)>"))
  12956. (callback
  12957. (lambda () (time-less-p
  12958. (org-time-string-to-time (match-string 2))
  12959. (org-time-string-to-time date)))))
  12960. (message "%d entries before %s"
  12961. (org-occur regexp nil callback) date)))
  12962. (defun org-check-after-date (date)
  12963. "Check if there are deadlines or scheduled entries after DATE."
  12964. (interactive (list (org-read-date)))
  12965. (let ((case-fold-search nil)
  12966. (regexp (concat "\\<\\(" org-deadline-string
  12967. "\\|" org-scheduled-string
  12968. "\\) *<\\([^>]+\\)>"))
  12969. (callback
  12970. (lambda () (not
  12971. (time-less-p
  12972. (org-time-string-to-time (match-string 2))
  12973. (org-time-string-to-time date))))))
  12974. (message "%d entries after %s"
  12975. (org-occur regexp nil callback) date)))
  12976. (defun org-evaluate-time-range (&optional to-buffer)
  12977. "Evaluate a time range by computing the difference between start and end.
  12978. Normally the result is just printed in the echo area, but with prefix arg
  12979. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  12980. If the time range is actually in a table, the result is inserted into the
  12981. next column.
  12982. For time difference computation, a year is assumed to be exactly 365
  12983. days in order to avoid rounding problems."
  12984. (interactive "P")
  12985. (or
  12986. (org-clock-update-time-maybe)
  12987. (save-excursion
  12988. (unless (org-at-date-range-p t)
  12989. (goto-char (point-at-bol))
  12990. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  12991. (if (not (org-at-date-range-p t))
  12992. (error "Not at a time-stamp range, and none found in current line")))
  12993. (let* ((ts1 (match-string 1))
  12994. (ts2 (match-string 2))
  12995. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  12996. (match-end (match-end 0))
  12997. (time1 (org-time-string-to-time ts1))
  12998. (time2 (org-time-string-to-time ts2))
  12999. (t1 (org-float-time time1))
  13000. (t2 (org-float-time time2))
  13001. (diff (abs (- t2 t1)))
  13002. (negative (< (- t2 t1) 0))
  13003. ;; (ys (floor (* 365 24 60 60)))
  13004. (ds (* 24 60 60))
  13005. (hs (* 60 60))
  13006. (fy "%dy %dd %02d:%02d")
  13007. (fy1 "%dy %dd")
  13008. (fd "%dd %02d:%02d")
  13009. (fd1 "%dd")
  13010. (fh "%02d:%02d")
  13011. y d h m align)
  13012. (if havetime
  13013. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13014. y 0
  13015. d (floor (/ diff ds)) diff (mod diff ds)
  13016. h (floor (/ diff hs)) diff (mod diff hs)
  13017. m (floor (/ diff 60)))
  13018. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13019. y 0
  13020. d (floor (+ (/ diff ds) 0.5))
  13021. h 0 m 0))
  13022. (if (not to-buffer)
  13023. (message "%s" (org-make-tdiff-string y d h m))
  13024. (if (org-at-table-p)
  13025. (progn
  13026. (goto-char match-end)
  13027. (setq align t)
  13028. (and (looking-at " *|") (goto-char (match-end 0))))
  13029. (goto-char match-end))
  13030. (if (looking-at
  13031. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  13032. (replace-match ""))
  13033. (if negative (insert " -"))
  13034. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  13035. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  13036. (insert " " (format fh h m))))
  13037. (if align (org-table-align))
  13038. (message "Time difference inserted")))))
  13039. (defun org-make-tdiff-string (y d h m)
  13040. (let ((fmt "")
  13041. (l nil))
  13042. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  13043. l (push y l)))
  13044. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  13045. l (push d l)))
  13046. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  13047. l (push h l)))
  13048. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  13049. l (push m l)))
  13050. (apply 'format fmt (nreverse l))))
  13051. (defun org-time-string-to-time (s)
  13052. (apply 'encode-time (org-parse-time-string s)))
  13053. (defun org-time-string-to-seconds (s)
  13054. (org-float-time (org-time-string-to-time s)))
  13055. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  13056. "Convert a time stamp to an absolute day number.
  13057. If there is a specifyer for a cyclic time stamp, get the closest date to
  13058. DAYNR.
  13059. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  13060. the variable date is bound by the calendar when this is called."
  13061. (cond
  13062. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  13063. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  13064. daynr
  13065. (+ daynr 1000)))
  13066. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  13067. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  13068. (time-to-days (current-time))) (match-string 0 s)
  13069. prefer show-all))
  13070. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  13071. (defun org-days-to-iso-week (days)
  13072. "Return the iso week number."
  13073. (require 'cal-iso)
  13074. (car (calendar-iso-from-absolute days)))
  13075. (defun org-small-year-to-year (year)
  13076. "Convert 2-digit years into 4-digit years.
  13077. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  13078. The year 2000 cannot be abbreviated. Any year larger than 99
  13079. is returned unchanged."
  13080. (if (< year 38)
  13081. (setq year (+ 2000 year))
  13082. (if (< year 100)
  13083. (setq year (+ 1900 year))))
  13084. year)
  13085. (defun org-time-from-absolute (d)
  13086. "Return the time corresponding to date D.
  13087. D may be an absolute day number, or a calendar-type list (month day year)."
  13088. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  13089. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  13090. (defun org-calendar-holiday ()
  13091. "List of holidays, for Diary display in Org-mode."
  13092. (require 'holidays)
  13093. (let ((hl (funcall
  13094. (if (fboundp 'calendar-check-holidays)
  13095. 'calendar-check-holidays 'check-calendar-holidays) date)))
  13096. (if hl (mapconcat 'identity hl "; "))))
  13097. (defun org-diary-sexp-entry (sexp entry date)
  13098. "Process a SEXP diary ENTRY for DATE."
  13099. (require 'diary-lib)
  13100. (let ((result (if calendar-debug-sexp
  13101. (let ((stack-trace-on-error t))
  13102. (eval (car (read-from-string sexp))))
  13103. (condition-case nil
  13104. (eval (car (read-from-string sexp)))
  13105. (error
  13106. (beep)
  13107. (message "Bad sexp at line %d in %s: %s"
  13108. (org-current-line)
  13109. (buffer-file-name) sexp)
  13110. (sleep-for 2))))))
  13111. (cond ((stringp result) result)
  13112. ((and (consp result)
  13113. (stringp (cdr result))) (cdr result))
  13114. (result entry)
  13115. (t nil))))
  13116. (defun org-diary-to-ical-string (frombuf)
  13117. "Get iCalendar entries from diary entries in buffer FROMBUF.
  13118. This uses the icalendar.el library."
  13119. (let* ((tmpdir (if (featurep 'xemacs)
  13120. (temp-directory)
  13121. temporary-file-directory))
  13122. (tmpfile (make-temp-name
  13123. (expand-file-name "orgics" tmpdir)))
  13124. buf rtn b e)
  13125. (with-current-buffer frombuf
  13126. (icalendar-export-region (point-min) (point-max) tmpfile)
  13127. (setq buf (find-buffer-visiting tmpfile))
  13128. (set-buffer buf)
  13129. (goto-char (point-min))
  13130. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  13131. (setq b (match-beginning 0)))
  13132. (goto-char (point-max))
  13133. (if (re-search-backward "^END:VEVENT" nil t)
  13134. (setq e (match-end 0)))
  13135. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  13136. (kill-buffer buf)
  13137. (delete-file tmpfile)
  13138. rtn))
  13139. (defun org-closest-date (start current change prefer show-all)
  13140. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  13141. When PREFER is `past' return a date that is either CURRENT or past.
  13142. When PREFER is `future', return a date that is either CURRENT or future.
  13143. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  13144. ;; Make the proper lists from the dates
  13145. (catch 'exit
  13146. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  13147. dn dw sday cday n1 n2 n0
  13148. d m y y1 y2 date1 date2 nmonths nm ny m2)
  13149. (setq start (org-date-to-gregorian start)
  13150. current (org-date-to-gregorian
  13151. (if show-all
  13152. current
  13153. (time-to-days (current-time))))
  13154. sday (calendar-absolute-from-gregorian start)
  13155. cday (calendar-absolute-from-gregorian current))
  13156. (if (<= cday sday) (throw 'exit sday))
  13157. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  13158. (setq dn (string-to-number (match-string 1 change))
  13159. dw (cdr (assoc (match-string 2 change) a1)))
  13160. (error "Invalid change specifyer: %s" change))
  13161. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  13162. (cond
  13163. ((eq dw 'day)
  13164. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  13165. n2 (+ n1 dn)))
  13166. ((eq dw 'year)
  13167. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  13168. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  13169. (setq date1 (list m d y1)
  13170. n1 (calendar-absolute-from-gregorian date1)
  13171. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  13172. n2 (calendar-absolute-from-gregorian date2)))
  13173. ((eq dw 'month)
  13174. ;; approx number of month between the two dates
  13175. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  13176. ;; How often does dn fit in there?
  13177. (setq d (nth 1 start) m (car start) y (nth 2 start)
  13178. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  13179. m (+ m nm)
  13180. ny (floor (/ m 12))
  13181. y (+ y ny)
  13182. m (- m (* ny 12)))
  13183. (while (> m 12) (setq m (- m 12) y (1+ y)))
  13184. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  13185. (setq m2 (+ m dn) y2 y)
  13186. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13187. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  13188. (while (<= n2 cday)
  13189. (setq n1 n2 m m2 y y2)
  13190. (setq m2 (+ m dn) y2 y)
  13191. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13192. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  13193. ;; Make sure n1 is the earlier date
  13194. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  13195. (if show-all
  13196. (cond
  13197. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13198. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13199. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  13200. (cond
  13201. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13202. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13203. (t (if (= cday n1) n1 n2)))))))
  13204. (defun org-date-to-gregorian (date)
  13205. "Turn any specification of DATE into a gregorian date for the calendar."
  13206. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  13207. ((and (listp date) (= (length date) 3)) date)
  13208. ((stringp date)
  13209. (setq date (org-parse-time-string date))
  13210. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  13211. ((listp date)
  13212. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  13213. (defun org-parse-time-string (s &optional nodefault)
  13214. "Parse the standard Org-mode time string.
  13215. This should be a lot faster than the normal `parse-time-string'.
  13216. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  13217. hour and minute fields will be nil if not given."
  13218. (if (string-match org-ts-regexp0 s)
  13219. (list 0
  13220. (if (or (match-beginning 8) (not nodefault))
  13221. (string-to-number (or (match-string 8 s) "0")))
  13222. (if (or (match-beginning 7) (not nodefault))
  13223. (string-to-number (or (match-string 7 s) "0")))
  13224. (string-to-number (match-string 4 s))
  13225. (string-to-number (match-string 3 s))
  13226. (string-to-number (match-string 2 s))
  13227. nil nil nil)
  13228. (error "Not a standard Org-mode time string: %s" s)))
  13229. (defun org-timestamp-up (&optional arg)
  13230. "Increase the date item at the cursor by one.
  13231. If the cursor is on the year, change the year. If it is on the month or
  13232. the day, change that.
  13233. With prefix ARG, change by that many units."
  13234. (interactive "p")
  13235. (org-timestamp-change (prefix-numeric-value arg)))
  13236. (defun org-timestamp-down (&optional arg)
  13237. "Decrease the date item at the cursor by one.
  13238. If the cursor is on the year, change the year. If it is on the month or
  13239. the day, change that.
  13240. With prefix ARG, change by that many units."
  13241. (interactive "p")
  13242. (org-timestamp-change (- (prefix-numeric-value arg))))
  13243. (defun org-timestamp-up-day (&optional arg)
  13244. "Increase the date in the time stamp by one day.
  13245. With prefix ARG, change that many days."
  13246. (interactive "p")
  13247. (if (and (not (org-at-timestamp-p t))
  13248. (org-on-heading-p))
  13249. (org-todo 'up)
  13250. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  13251. (defun org-timestamp-down-day (&optional arg)
  13252. "Decrease the date in the time stamp by one day.
  13253. With prefix ARG, change that many days."
  13254. (interactive "p")
  13255. (if (and (not (org-at-timestamp-p t))
  13256. (org-on-heading-p))
  13257. (org-todo 'down)
  13258. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  13259. (defun org-at-timestamp-p (&optional inactive-ok)
  13260. "Determine if the cursor is in or at a timestamp."
  13261. (interactive)
  13262. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  13263. (pos (point))
  13264. (ans (or (looking-at tsr)
  13265. (save-excursion
  13266. (skip-chars-backward "^[<\n\r\t")
  13267. (if (> (point) (point-min)) (backward-char 1))
  13268. (and (looking-at tsr)
  13269. (> (- (match-end 0) pos) -1))))))
  13270. (and ans
  13271. (boundp 'org-ts-what)
  13272. (setq org-ts-what
  13273. (cond
  13274. ((= pos (match-beginning 0)) 'bracket)
  13275. ((= pos (1- (match-end 0))) 'bracket)
  13276. ((org-pos-in-match-range pos 2) 'year)
  13277. ((org-pos-in-match-range pos 3) 'month)
  13278. ((org-pos-in-match-range pos 7) 'hour)
  13279. ((org-pos-in-match-range pos 8) 'minute)
  13280. ((or (org-pos-in-match-range pos 4)
  13281. (org-pos-in-match-range pos 5)) 'day)
  13282. ((and (> pos (or (match-end 8) (match-end 5)))
  13283. (< pos (match-end 0)))
  13284. (- pos (or (match-end 8) (match-end 5))))
  13285. (t 'day))))
  13286. ans))
  13287. (defun org-toggle-timestamp-type ()
  13288. "Toggle the type (<active> or [inactive]) of a time stamp."
  13289. (interactive)
  13290. (when (org-at-timestamp-p t)
  13291. (let ((beg (match-beginning 0)) (end (match-end 0))
  13292. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  13293. (save-excursion
  13294. (goto-char beg)
  13295. (while (re-search-forward "[][<>]" end t)
  13296. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  13297. t t)))
  13298. (message "Timestamp is now %sactive"
  13299. (if (equal (char-after beg) ?<) "" "in")))))
  13300. (defun org-timestamp-change (n &optional what)
  13301. "Change the date in the time stamp at point.
  13302. The date will be changed by N times WHAT. WHAT can be `day', `month',
  13303. `year', `minute', `second'. If WHAT is not given, the cursor position
  13304. in the timestamp determines what will be changed."
  13305. (let ((pos (point))
  13306. with-hm inactive
  13307. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  13308. org-ts-what
  13309. extra rem
  13310. ts time time0)
  13311. (if (not (org-at-timestamp-p t))
  13312. (error "Not at a timestamp"))
  13313. (if (and (not what) (eq org-ts-what 'bracket))
  13314. (org-toggle-timestamp-type)
  13315. (if (and (not what) (not (eq org-ts-what 'day))
  13316. org-display-custom-times
  13317. (get-text-property (point) 'display)
  13318. (not (get-text-property (1- (point)) 'display)))
  13319. (setq org-ts-what 'day))
  13320. (setq org-ts-what (or what org-ts-what)
  13321. inactive (= (char-after (match-beginning 0)) ?\[)
  13322. ts (match-string 0))
  13323. (replace-match "")
  13324. (if (string-match
  13325. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  13326. ts)
  13327. (setq extra (match-string 1 ts)))
  13328. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  13329. (setq with-hm t))
  13330. (setq time0 (org-parse-time-string ts))
  13331. (when (and (eq org-ts-what 'minute)
  13332. (eq current-prefix-arg nil))
  13333. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  13334. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  13335. (setcar (cdr time0) (+ (nth 1 time0)
  13336. (if (> n 0) (- rem) (- dm rem))))))
  13337. (setq time
  13338. (encode-time (or (car time0) 0)
  13339. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  13340. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  13341. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  13342. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  13343. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  13344. (nthcdr 6 time0)))
  13345. (when (and (member org-ts-what '(hour minute))
  13346. extra
  13347. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  13348. (setq extra (org-modify-ts-extra
  13349. extra
  13350. (if (eq org-ts-what 'hour) 2 5)
  13351. n dm)))
  13352. (when (integerp org-ts-what)
  13353. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  13354. (if (eq what 'calendar)
  13355. (let ((cal-date (org-get-date-from-calendar)))
  13356. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  13357. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  13358. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  13359. (setcar time0 (or (car time0) 0))
  13360. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  13361. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  13362. (setq time (apply 'encode-time time0))))
  13363. (setq org-last-changed-timestamp
  13364. (org-insert-time-stamp time with-hm inactive nil nil extra))
  13365. (org-clock-update-time-maybe)
  13366. (goto-char pos)
  13367. ;; Try to recenter the calendar window, if any
  13368. (if (and org-calendar-follow-timestamp-change
  13369. (get-buffer-window "*Calendar*" t)
  13370. (memq org-ts-what '(day month year)))
  13371. (org-recenter-calendar (time-to-days time))))))
  13372. (defun org-modify-ts-extra (s pos n dm)
  13373. "Change the different parts of the lead-time and repeat fields in timestamp."
  13374. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  13375. ng h m new rem)
  13376. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  13377. (cond
  13378. ((or (org-pos-in-match-range pos 2)
  13379. (org-pos-in-match-range pos 3))
  13380. (setq m (string-to-number (match-string 3 s))
  13381. h (string-to-number (match-string 2 s)))
  13382. (if (org-pos-in-match-range pos 2)
  13383. (setq h (+ h n))
  13384. (setq n (* dm (org-no-warnings (signum n))))
  13385. (when (not (= 0 (setq rem (% m dm))))
  13386. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  13387. (setq m (+ m n)))
  13388. (if (< m 0) (setq m (+ m 60) h (1- h)))
  13389. (if (> m 59) (setq m (- m 60) h (1+ h)))
  13390. (setq h (min 24 (max 0 h)))
  13391. (setq ng 1 new (format "-%02d:%02d" h m)))
  13392. ((org-pos-in-match-range pos 6)
  13393. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  13394. ((org-pos-in-match-range pos 5)
  13395. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  13396. ((org-pos-in-match-range pos 9)
  13397. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  13398. ((org-pos-in-match-range pos 8)
  13399. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  13400. (when ng
  13401. (setq s (concat
  13402. (substring s 0 (match-beginning ng))
  13403. new
  13404. (substring s (match-end ng))))))
  13405. s))
  13406. (defun org-recenter-calendar (date)
  13407. "If the calendar is visible, recenter it to DATE."
  13408. (let* ((win (selected-window))
  13409. (cwin (get-buffer-window "*Calendar*" t))
  13410. (calendar-move-hook nil))
  13411. (when cwin
  13412. (select-window cwin)
  13413. (calendar-goto-date (if (listp date) date
  13414. (calendar-gregorian-from-absolute date)))
  13415. (select-window win))))
  13416. (defun org-goto-calendar (&optional arg)
  13417. "Go to the Emacs calendar at the current date.
  13418. If there is a time stamp in the current line, go to that date.
  13419. A prefix ARG can be used to force the current date."
  13420. (interactive "P")
  13421. (let ((tsr org-ts-regexp) diff
  13422. (calendar-move-hook nil)
  13423. (calendar-view-holidays-initially-flag nil)
  13424. (calendar-view-diary-initially-flag nil))
  13425. (if (or (org-at-timestamp-p)
  13426. (save-excursion
  13427. (beginning-of-line 1)
  13428. (looking-at (concat ".*" tsr))))
  13429. (let ((d1 (time-to-days (current-time)))
  13430. (d2 (time-to-days
  13431. (org-time-string-to-time (match-string 1)))))
  13432. (setq diff (- d2 d1))))
  13433. (calendar)
  13434. (calendar-goto-today)
  13435. (if (and diff (not arg)) (calendar-forward-day diff))))
  13436. (defun org-get-date-from-calendar ()
  13437. "Return a list (month day year) of date at point in calendar."
  13438. (with-current-buffer "*Calendar*"
  13439. (save-match-data
  13440. (calendar-cursor-to-date))))
  13441. (defun org-date-from-calendar ()
  13442. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  13443. If there is already a time stamp at the cursor position, update it."
  13444. (interactive)
  13445. (if (org-at-timestamp-p t)
  13446. (org-timestamp-change 0 'calendar)
  13447. (let ((cal-date (org-get-date-from-calendar)))
  13448. (org-insert-time-stamp
  13449. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  13450. (defun org-minutes-to-hh:mm-string (m)
  13451. "Compute H:MM from a number of minutes."
  13452. (let ((h (/ m 60)))
  13453. (setq m (- m (* 60 h)))
  13454. (format org-time-clocksum-format h m)))
  13455. (defun org-hh:mm-string-to-minutes (s)
  13456. "Convert a string H:MM to a number of minutes.
  13457. If the string is just a number, interpret it as minutes.
  13458. In fact, the first hh:mm or number in the string will be taken,
  13459. there can be extra stuff in the string.
  13460. If no number is found, the return value is 0."
  13461. (cond
  13462. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  13463. (+ (* (string-to-number (match-string 1 s)) 60)
  13464. (string-to-number (match-string 2 s))))
  13465. ((string-match "\\([0-9]+\\)" s)
  13466. (string-to-number (match-string 1 s)))
  13467. (t 0)))
  13468. ;;;; Files
  13469. (defun org-save-all-org-buffers ()
  13470. "Save all Org-mode buffers without user confirmation."
  13471. (interactive)
  13472. (message "Saving all Org-mode buffers...")
  13473. (save-some-buffers t 'org-mode-p)
  13474. (when (featurep 'org-id) (org-id-locations-save))
  13475. (message "Saving all Org-mode buffers... done"))
  13476. (defun org-revert-all-org-buffers ()
  13477. "Revert all Org-mode buffers.
  13478. Prompt for confirmation when there are unsaved changes.
  13479. Be sure you know what you are doing before letting this function
  13480. overwrite your changes.
  13481. This function is useful in a setup where one tracks org files
  13482. with a version control system, to revert on one machine after pulling
  13483. changes from another. I believe the procedure must be like this:
  13484. 1. M-x org-save-all-org-buffers
  13485. 2. Pull changes from the other machine, resolve conflicts
  13486. 3. M-x org-revert-all-org-buffers"
  13487. (interactive)
  13488. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  13489. (error "Abort"))
  13490. (save-excursion
  13491. (save-window-excursion
  13492. (mapc
  13493. (lambda (b)
  13494. (when (and (with-current-buffer b (org-mode-p))
  13495. (with-current-buffer b buffer-file-name))
  13496. (switch-to-buffer b)
  13497. (revert-buffer t 'no-confirm)))
  13498. (buffer-list))
  13499. (when (and (featurep 'org-id) org-id-track-globally)
  13500. (org-id-locations-load)))))
  13501. ;;;; Agenda files
  13502. ;;;###autoload
  13503. (defun org-iswitchb (&optional arg)
  13504. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  13505. With a prefix argument, restrict available to files.
  13506. With two prefix arguments, restrict available buffers to agenda files."
  13507. (interactive "P")
  13508. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  13509. ((equal arg '(16)) (org-buffer-list 'agenda))
  13510. (t (org-buffer-list)))))
  13511. (switch-to-buffer
  13512. (org-icompleting-read "Org buffer: "
  13513. (mapcar 'list (mapcar 'buffer-name blist))
  13514. nil t))))
  13515. ;;;###autoload
  13516. (defalias 'org-ido-switchb 'org-iswitchb)
  13517. (defun org-buffer-list (&optional predicate exclude-tmp)
  13518. "Return a list of Org buffers.
  13519. PREDICATE can be `export', `files' or `agenda'.
  13520. export restrict the list to Export buffers.
  13521. files restrict the list to buffers visiting Org files.
  13522. agenda restrict the list to buffers visiting agenda files.
  13523. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  13524. (let* ((bfn nil)
  13525. (agenda-files (and (eq predicate 'agenda)
  13526. (mapcar 'file-truename (org-agenda-files t))))
  13527. (filter
  13528. (cond
  13529. ((eq predicate 'files)
  13530. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  13531. ((eq predicate 'export)
  13532. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  13533. ((eq predicate 'agenda)
  13534. (lambda (b)
  13535. (with-current-buffer b
  13536. (and (eq major-mode 'org-mode)
  13537. (setq bfn (buffer-file-name b))
  13538. (member (file-truename bfn) agenda-files)))))
  13539. (t (lambda (b) (with-current-buffer b
  13540. (or (eq major-mode 'org-mode)
  13541. (string-match "\*Org .*Export"
  13542. (buffer-name b)))))))))
  13543. (delq nil
  13544. (mapcar
  13545. (lambda(b)
  13546. (if (and (funcall filter b)
  13547. (or (not exclude-tmp)
  13548. (not (string-match "tmp" (buffer-name b)))))
  13549. b
  13550. nil))
  13551. (buffer-list)))))
  13552. (defun org-agenda-files (&optional unrestricted archives)
  13553. "Get the list of agenda files.
  13554. Optional UNRESTRICTED means return the full list even if a restriction
  13555. is currently in place.
  13556. When ARCHIVES is t, include all archive files that are really being
  13557. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  13558. `org-agenda-archives-mode' is t."
  13559. (let ((files
  13560. (cond
  13561. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  13562. ((stringp org-agenda-files) (org-read-agenda-file-list))
  13563. ((listp org-agenda-files) org-agenda-files)
  13564. (t (error "Invalid value of `org-agenda-files'")))))
  13565. (setq files (apply 'append
  13566. (mapcar (lambda (f)
  13567. (if (file-directory-p f)
  13568. (directory-files
  13569. f t org-agenda-file-regexp)
  13570. (list f)))
  13571. files)))
  13572. (when org-agenda-skip-unavailable-files
  13573. (setq files (delq nil
  13574. (mapcar (function
  13575. (lambda (file)
  13576. (and (file-readable-p file) file)))
  13577. files))))
  13578. (when (or (eq archives t)
  13579. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  13580. (setq files (org-add-archive-files files)))
  13581. files))
  13582. (defun org-edit-agenda-file-list ()
  13583. "Edit the list of agenda files.
  13584. Depending on setup, this either uses customize to edit the variable
  13585. `org-agenda-files', or it visits the file that is holding the list. In the
  13586. latter case, the buffer is set up in a way that saving it automatically kills
  13587. the buffer and restores the previous window configuration."
  13588. (interactive)
  13589. (if (stringp org-agenda-files)
  13590. (let ((cw (current-window-configuration)))
  13591. (find-file org-agenda-files)
  13592. (org-set-local 'org-window-configuration cw)
  13593. (org-add-hook 'after-save-hook
  13594. (lambda ()
  13595. (set-window-configuration
  13596. (prog1 org-window-configuration
  13597. (kill-buffer (current-buffer))))
  13598. (org-install-agenda-files-menu)
  13599. (message "New agenda file list installed"))
  13600. nil 'local)
  13601. (message "%s" (substitute-command-keys
  13602. "Edit list and finish with \\[save-buffer]")))
  13603. (customize-variable 'org-agenda-files)))
  13604. (defun org-store-new-agenda-file-list (list)
  13605. "Set new value for the agenda file list and save it correctly."
  13606. (if (stringp org-agenda-files)
  13607. (let ((fe (org-read-agenda-file-list t)) b u)
  13608. (while (setq b (find-buffer-visiting org-agenda-files))
  13609. (kill-buffer b))
  13610. (with-temp-file org-agenda-files
  13611. (insert
  13612. (mapconcat
  13613. (lambda (f) ;; Keep un-expanded entries.
  13614. (if (setq u (assoc f fe))
  13615. (cdr u)
  13616. f))
  13617. list "\n")
  13618. "\n")))
  13619. (let ((org-mode-hook nil) (org-inhibit-startup t)
  13620. (org-insert-mode-line-in-empty-file nil))
  13621. (setq org-agenda-files list)
  13622. (customize-save-variable 'org-agenda-files org-agenda-files))))
  13623. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  13624. "Read the list of agenda files from a file.
  13625. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  13626. filenames, used by `org-store-new-agenda-file-list' to write back
  13627. un-expanded file names."
  13628. (when (file-directory-p org-agenda-files)
  13629. (error "`org-agenda-files' cannot be a single directory"))
  13630. (when (stringp org-agenda-files)
  13631. (with-temp-buffer
  13632. (insert-file-contents org-agenda-files)
  13633. (mapcar
  13634. (lambda (f)
  13635. (let ((e (expand-file-name (substitute-in-file-name f)
  13636. org-directory)))
  13637. (if pair-with-expansion
  13638. (cons e f)
  13639. e)))
  13640. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  13641. ;;;###autoload
  13642. (defun org-cycle-agenda-files ()
  13643. "Cycle through the files in `org-agenda-files'.
  13644. If the current buffer visits an agenda file, find the next one in the list.
  13645. If the current buffer does not, find the first agenda file."
  13646. (interactive)
  13647. (let* ((fs (org-agenda-files t))
  13648. (files (append fs (list (car fs))))
  13649. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  13650. file)
  13651. (unless files (error "No agenda files"))
  13652. (catch 'exit
  13653. (while (setq file (pop files))
  13654. (if (equal (file-truename file) tcf)
  13655. (when (car files)
  13656. (find-file (car files))
  13657. (throw 'exit t))))
  13658. (find-file (car fs)))
  13659. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  13660. (defun org-agenda-file-to-front (&optional to-end)
  13661. "Move/add the current file to the top of the agenda file list.
  13662. If the file is not present in the list, it is added to the front. If it is
  13663. present, it is moved there. With optional argument TO-END, add/move to the
  13664. end of the list."
  13665. (interactive "P")
  13666. (let ((org-agenda-skip-unavailable-files nil)
  13667. (file-alist (mapcar (lambda (x)
  13668. (cons (file-truename x) x))
  13669. (org-agenda-files t)))
  13670. (ctf (file-truename buffer-file-name))
  13671. x had)
  13672. (setq x (assoc ctf file-alist) had x)
  13673. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  13674. (if to-end
  13675. (setq file-alist (append (delq x file-alist) (list x)))
  13676. (setq file-alist (cons x (delq x file-alist))))
  13677. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  13678. (org-install-agenda-files-menu)
  13679. (message "File %s to %s of agenda file list"
  13680. (if had "moved" "added") (if to-end "end" "front"))))
  13681. (defun org-remove-file (&optional file)
  13682. "Remove current file from the list of files in variable `org-agenda-files'.
  13683. These are the files which are being checked for agenda entries.
  13684. Optional argument FILE means use this file instead of the current."
  13685. (interactive)
  13686. (let* ((org-agenda-skip-unavailable-files nil)
  13687. (file (or file buffer-file-name))
  13688. (true-file (file-truename file))
  13689. (afile (abbreviate-file-name file))
  13690. (files (delq nil (mapcar
  13691. (lambda (x)
  13692. (if (equal true-file
  13693. (file-truename x))
  13694. nil x))
  13695. (org-agenda-files t)))))
  13696. (if (not (= (length files) (length (org-agenda-files t))))
  13697. (progn
  13698. (org-store-new-agenda-file-list files)
  13699. (org-install-agenda-files-menu)
  13700. (message "Removed file: %s" afile))
  13701. (message "File was not in list: %s (not removed)" afile))))
  13702. (defun org-file-menu-entry (file)
  13703. (vector file (list 'find-file file) t))
  13704. (defun org-check-agenda-file (file)
  13705. "Make sure FILE exists. If not, ask user what to do."
  13706. (when (not (file-exists-p file))
  13707. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  13708. (abbreviate-file-name file))
  13709. (let ((r (downcase (read-char-exclusive))))
  13710. (cond
  13711. ((equal r ?r)
  13712. (org-remove-file file)
  13713. (throw 'nextfile t))
  13714. (t (error "Abort"))))))
  13715. (defun org-get-agenda-file-buffer (file)
  13716. "Get a buffer visiting FILE. If the buffer needs to be created, add
  13717. it to the list of buffers which might be released later."
  13718. (let ((buf (org-find-base-buffer-visiting file)))
  13719. (if buf
  13720. buf ; just return it
  13721. ;; Make a new buffer and remember it
  13722. (setq buf (find-file-noselect file))
  13723. (if buf (push buf org-agenda-new-buffers))
  13724. buf)))
  13725. (defun org-release-buffers (blist)
  13726. "Release all buffers in list, asking the user for confirmation when needed.
  13727. When a buffer is unmodified, it is just killed. When modified, it is saved
  13728. \(if the user agrees) and then killed."
  13729. (let (buf file)
  13730. (while (setq buf (pop blist))
  13731. (setq file (buffer-file-name buf))
  13732. (when (and (buffer-modified-p buf)
  13733. file
  13734. (y-or-n-p (format "Save file %s? " file)))
  13735. (with-current-buffer buf (save-buffer)))
  13736. (kill-buffer buf))))
  13737. (defun org-prepare-agenda-buffers (files)
  13738. "Create buffers for all agenda files, protect archived trees and comments."
  13739. (interactive)
  13740. (let ((pa '(:org-archived t))
  13741. (pc '(:org-comment t))
  13742. (pall '(:org-archived t :org-comment t))
  13743. (inhibit-read-only t)
  13744. (rea (concat ":" org-archive-tag ":"))
  13745. bmp file re)
  13746. (save-excursion
  13747. (save-restriction
  13748. (while (setq file (pop files))
  13749. (catch 'nextfile
  13750. (if (bufferp file)
  13751. (set-buffer file)
  13752. (org-check-agenda-file file)
  13753. (set-buffer (org-get-agenda-file-buffer file)))
  13754. (widen)
  13755. (setq bmp (buffer-modified-p))
  13756. (org-refresh-category-properties)
  13757. (setq org-todo-keywords-for-agenda
  13758. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  13759. (setq org-done-keywords-for-agenda
  13760. (append org-done-keywords-for-agenda org-done-keywords))
  13761. (setq org-todo-keyword-alist-for-agenda
  13762. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  13763. (setq org-drawers-for-agenda
  13764. (append org-drawers-for-agenda org-drawers))
  13765. (setq org-tag-alist-for-agenda
  13766. (append org-tag-alist-for-agenda org-tag-alist))
  13767. (save-excursion
  13768. (remove-text-properties (point-min) (point-max) pall)
  13769. (when org-agenda-skip-archived-trees
  13770. (goto-char (point-min))
  13771. (while (re-search-forward rea nil t)
  13772. (if (org-on-heading-p t)
  13773. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  13774. (goto-char (point-min))
  13775. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  13776. (while (re-search-forward re nil t)
  13777. (add-text-properties
  13778. (match-beginning 0) (org-end-of-subtree t) pc)))
  13779. (set-buffer-modified-p bmp)))))
  13780. (setq org-todo-keywords-for-agenda
  13781. (org-uniquify org-todo-keywords-for-agenda))
  13782. (setq org-todo-keyword-alist-for-agenda
  13783. (org-uniquify org-todo-keyword-alist-for-agenda)
  13784. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  13785. ;;;; Embedded LaTeX
  13786. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  13787. "Keymap for the minor `org-cdlatex-mode'.")
  13788. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  13789. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  13790. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  13791. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  13792. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  13793. (defvar org-cdlatex-texmathp-advice-is-done nil
  13794. "Flag remembering if we have applied the advice to texmathp already.")
  13795. (define-minor-mode org-cdlatex-mode
  13796. "Toggle the minor `org-cdlatex-mode'.
  13797. This mode supports entering LaTeX environment and math in LaTeX fragments
  13798. in Org-mode.
  13799. \\{org-cdlatex-mode-map}"
  13800. nil " OCDL" nil
  13801. (when org-cdlatex-mode (require 'cdlatex))
  13802. (unless org-cdlatex-texmathp-advice-is-done
  13803. (setq org-cdlatex-texmathp-advice-is-done t)
  13804. (defadvice texmathp (around org-math-always-on activate)
  13805. "Always return t in org-mode buffers.
  13806. This is because we want to insert math symbols without dollars even outside
  13807. the LaTeX math segments. If Orgmode thinks that point is actually inside
  13808. an embedded LaTeX fragment, let texmathp do its job.
  13809. \\[org-cdlatex-mode-map]"
  13810. (interactive)
  13811. (let (p)
  13812. (cond
  13813. ((not (org-mode-p)) ad-do-it)
  13814. ((eq this-command 'cdlatex-math-symbol)
  13815. (setq ad-return-value t
  13816. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  13817. (t
  13818. (let ((p (org-inside-LaTeX-fragment-p)))
  13819. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  13820. (setq ad-return-value t
  13821. texmathp-why '("Org-mode embedded math" . 0))
  13822. (if p ad-do-it)))))))))
  13823. (defun turn-on-org-cdlatex ()
  13824. "Unconditionally turn on `org-cdlatex-mode'."
  13825. (org-cdlatex-mode 1))
  13826. (defun org-inside-LaTeX-fragment-p ()
  13827. "Test if point is inside a LaTeX fragment.
  13828. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  13829. sequence appearing also before point.
  13830. Even though the matchers for math are configurable, this function assumes
  13831. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  13832. delimiters are skipped when they have been removed by customization.
  13833. The return value is nil, or a cons cell with the delimiter and
  13834. and the position of this delimiter.
  13835. This function does a reasonably good job, but can locally be fooled by
  13836. for example currency specifications. For example it will assume being in
  13837. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  13838. fragments that are properly closed, but during editing, we have to live
  13839. with the uncertainty caused by missing closing delimiters. This function
  13840. looks only before point, not after."
  13841. (catch 'exit
  13842. (let ((pos (point))
  13843. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  13844. (lim (progn
  13845. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  13846. (point)))
  13847. dd-on str (start 0) m re)
  13848. (goto-char pos)
  13849. (when dodollar
  13850. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  13851. re (nth 1 (assoc "$" org-latex-regexps)))
  13852. (while (string-match re str start)
  13853. (cond
  13854. ((= (match-end 0) (length str))
  13855. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  13856. ((= (match-end 0) (- (length str) 5))
  13857. (throw 'exit nil))
  13858. (t (setq start (match-end 0))))))
  13859. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  13860. (goto-char pos)
  13861. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  13862. (and (match-beginning 2) (throw 'exit nil))
  13863. ;; count $$
  13864. (while (re-search-backward "\\$\\$" lim t)
  13865. (setq dd-on (not dd-on)))
  13866. (goto-char pos)
  13867. (if dd-on (cons "$$" m))))))
  13868. (defun org-inside-latex-macro-p ()
  13869. "Is point inside a LaTeX macro or its arguments?"
  13870. (save-match-data
  13871. (org-in-regexp
  13872. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  13873. (defun test ()
  13874. (interactive)
  13875. (message "%s" (org-inside-latex-macro-p)))
  13876. (defun org-try-cdlatex-tab ()
  13877. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  13878. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  13879. - inside a LaTeX fragment, or
  13880. - after the first word in a line, where an abbreviation expansion could
  13881. insert a LaTeX environment."
  13882. (when org-cdlatex-mode
  13883. (cond
  13884. ((save-excursion
  13885. (skip-chars-backward "a-zA-Z0-9*")
  13886. (skip-chars-backward " \t")
  13887. (bolp))
  13888. (cdlatex-tab) t)
  13889. ((org-inside-LaTeX-fragment-p)
  13890. (cdlatex-tab) t)
  13891. (t nil))))
  13892. (defun org-cdlatex-underscore-caret (&optional arg)
  13893. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  13894. Revert to the normal definition outside of these fragments."
  13895. (interactive "P")
  13896. (if (org-inside-LaTeX-fragment-p)
  13897. (call-interactively 'cdlatex-sub-superscript)
  13898. (let (org-cdlatex-mode)
  13899. (call-interactively (key-binding (vector last-input-event))))))
  13900. (defun org-cdlatex-math-modify (&optional arg)
  13901. "Execute `cdlatex-math-modify' in LaTeX fragments.
  13902. Revert to the normal definition outside of these fragments."
  13903. (interactive "P")
  13904. (if (org-inside-LaTeX-fragment-p)
  13905. (call-interactively 'cdlatex-math-modify)
  13906. (let (org-cdlatex-mode)
  13907. (call-interactively (key-binding (vector last-input-event))))))
  13908. (defvar org-latex-fragment-image-overlays nil
  13909. "List of overlays carrying the images of latex fragments.")
  13910. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  13911. (defun org-remove-latex-fragment-image-overlays ()
  13912. "Remove all overlays with LaTeX fragment images in current buffer."
  13913. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  13914. (setq org-latex-fragment-image-overlays nil))
  13915. (defun org-preview-latex-fragment (&optional subtree)
  13916. "Preview the LaTeX fragment at point, or all locally or globally.
  13917. If the cursor is in a LaTeX fragment, create the image and overlay
  13918. it over the source code. If there is no fragment at point, display
  13919. all fragments in the current text, from one headline to the next. With
  13920. prefix SUBTREE, display all fragments in the current subtree. With a
  13921. double prefix `C-u C-u', or when the cursor is before the first headline,
  13922. display all fragments in the buffer.
  13923. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  13924. (interactive "P")
  13925. (org-remove-latex-fragment-image-overlays)
  13926. (save-excursion
  13927. (save-restriction
  13928. (let (beg end at msg)
  13929. (cond
  13930. ((or (equal subtree '(16))
  13931. (not (save-excursion
  13932. (re-search-backward (concat "^" outline-regexp) nil t))))
  13933. (setq beg (point-min) end (point-max)
  13934. msg "Creating images for buffer...%s"))
  13935. ((equal subtree '(4))
  13936. (org-back-to-heading)
  13937. (setq beg (point) end (org-end-of-subtree t)
  13938. msg "Creating images for subtree...%s"))
  13939. (t
  13940. (if (setq at (org-inside-LaTeX-fragment-p))
  13941. (goto-char (max (point-min) (- (cdr at) 2)))
  13942. (org-back-to-heading))
  13943. (setq beg (point) end (progn (outline-next-heading) (point))
  13944. msg (if at "Creating image...%s"
  13945. "Creating images for entry...%s"))))
  13946. (message msg "")
  13947. (narrow-to-region beg end)
  13948. (goto-char beg)
  13949. (org-format-latex
  13950. (concat "ltxpng/" (file-name-sans-extension
  13951. (file-name-nondirectory
  13952. buffer-file-name)))
  13953. default-directory 'overlays msg at 'forbuffer)
  13954. (message msg "done. Use `C-c C-c' to remove images.")))))
  13955. (defvar org-latex-regexps
  13956. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  13957. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  13958. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  13959. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13960. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13961. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  13962. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  13963. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  13964. "Regular expressions for matching embedded LaTeX.")
  13965. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  13966. "Replace LaTeX fragments with links to an image, and produce images.
  13967. Some of the options can be changed using the variable
  13968. `org-format-latex-options'."
  13969. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  13970. (let* ((prefixnodir (file-name-nondirectory prefix))
  13971. (absprefix (expand-file-name prefix dir))
  13972. (todir (file-name-directory absprefix))
  13973. (opt org-format-latex-options)
  13974. (matchers (plist-get opt :matchers))
  13975. (re-list org-latex-regexps)
  13976. (org-format-latex-header-extra
  13977. (plist-get (org-infile-export-plist) :latex-header-extra))
  13978. (cnt 0) txt hash link beg end re e checkdir
  13979. executables-checked
  13980. m n block linkfile movefile ov)
  13981. ;; Check the different regular expressions
  13982. (while (setq e (pop re-list))
  13983. (setq m (car e) re (nth 1 e) n (nth 2 e)
  13984. block (if (nth 3 e) "\n\n" ""))
  13985. (when (member m matchers)
  13986. (goto-char (point-min))
  13987. (while (re-search-forward re nil t)
  13988. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  13989. (not (get-text-property (match-beginning n)
  13990. 'org-protected))
  13991. (or (not overlays)
  13992. (not (eq (get-char-property (match-beginning n)
  13993. 'org-overlay-type)
  13994. 'org-latex-overlay))))
  13995. (setq txt (match-string n)
  13996. beg (match-beginning n) end (match-end n)
  13997. cnt (1+ cnt))
  13998. (let (print-length print-level) ; make sure full list is printed
  13999. (setq hash (sha1 (prin1-to-string
  14000. (list org-format-latex-header
  14001. org-format-latex-header-extra
  14002. org-export-latex-default-packages-alist
  14003. org-export-latex-packages-alist
  14004. org-format-latex-options
  14005. forbuffer txt)))
  14006. linkfile (format "%s_%s.png" prefix hash)
  14007. movefile (format "%s_%s.png" absprefix hash)))
  14008. (setq link (concat block "[[file:" linkfile "]]" block))
  14009. (if msg (message msg cnt))
  14010. (goto-char beg)
  14011. (unless checkdir ; make sure the directory exists
  14012. (setq checkdir t)
  14013. (or (file-directory-p todir) (make-directory todir)))
  14014. (unless executables-checked
  14015. (org-check-external-command
  14016. "latex" "needed to convert LaTeX fragments to images")
  14017. (org-check-external-command
  14018. "dvipng" "needed to convert LaTeX fragments to images")
  14019. (setq executables-checked t))
  14020. (unless (file-exists-p movefile)
  14021. (org-create-formula-image
  14022. txt movefile opt forbuffer))
  14023. (if overlays
  14024. (progn
  14025. (mapc (lambda (o)
  14026. (if (eq (overlay-get o 'org-overlay-type)
  14027. 'org-latex-overlay)
  14028. (delete-overlay o)))
  14029. (overlays-in beg end))
  14030. (setq ov (make-overlay beg end))
  14031. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  14032. (if (featurep 'xemacs)
  14033. (progn
  14034. (overlay-put ov 'invisible t)
  14035. (overlay-put
  14036. ov 'end-glyph
  14037. (make-glyph (vector 'png :file movefile))))
  14038. (overlay-put
  14039. ov 'display
  14040. (list 'image :type 'png :file movefile :ascent 'center)))
  14041. (push ov org-latex-fragment-image-overlays)
  14042. (goto-char end))
  14043. (delete-region beg end)
  14044. (insert (org-add-props link
  14045. (list 'org-latex-src
  14046. (replace-regexp-in-string "\"" "" txt)))))))))))
  14047. ;; This function borrows from Ganesh Swami's latex2png.el
  14048. (defun org-create-formula-image (string tofile options buffer)
  14049. "This calls dvipng."
  14050. (require 'org-latex)
  14051. (let* ((tmpdir (if (featurep 'xemacs)
  14052. (temp-directory)
  14053. temporary-file-directory))
  14054. (texfilebase (make-temp-name
  14055. (expand-file-name "orgtex" tmpdir)))
  14056. (texfile (concat texfilebase ".tex"))
  14057. (dvifile (concat texfilebase ".dvi"))
  14058. (pngfile (concat texfilebase ".png"))
  14059. (fnh (if (featurep 'xemacs)
  14060. (font-height (get-face-font 'default))
  14061. (face-attribute 'default :height nil)))
  14062. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  14063. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  14064. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  14065. "Black"))
  14066. (bg (or (plist-get options (if buffer :background :html-background))
  14067. "Transparent")))
  14068. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  14069. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  14070. (with-temp-file texfile
  14071. (insert (org-splice-latex-header
  14072. org-format-latex-header
  14073. org-export-latex-default-packages-alist
  14074. org-export-latex-packages-alist t
  14075. org-format-latex-header-extra))
  14076. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  14077. (require 'org-latex)
  14078. (org-export-latex-fix-inputenc))
  14079. (let ((dir default-directory))
  14080. (condition-case nil
  14081. (progn
  14082. (cd tmpdir)
  14083. (call-process "latex" nil nil nil texfile))
  14084. (error nil))
  14085. (cd dir))
  14086. (if (not (file-exists-p dvifile))
  14087. (progn (message "Failed to create dvi file from %s" texfile) nil)
  14088. (condition-case nil
  14089. (call-process "dvipng" nil nil nil
  14090. "-fg" fg "-bg" bg
  14091. "-D" dpi
  14092. ;;"-x" scale "-y" scale
  14093. "-T" "tight"
  14094. "-o" pngfile
  14095. dvifile)
  14096. (error nil))
  14097. (if (not (file-exists-p pngfile))
  14098. (if org-format-latex-signal-error
  14099. (error "Failed to create png file from %s" texfile)
  14100. (message "Failed to create png file from %s" texfile)
  14101. nil)
  14102. ;; Use the requested file name and clean up
  14103. (copy-file pngfile tofile 'replace)
  14104. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  14105. (delete-file (concat texfilebase e)))
  14106. pngfile))))
  14107. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  14108. "Fill a LaTeX header template TPL.
  14109. In the template, the following place holders will be recognized:
  14110. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  14111. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  14112. [PACKAGES] \\usepackage statements for PKG
  14113. [NO-PACKAGES] do not include PKG
  14114. [EXTRA] the string EXTRA
  14115. [NO-EXTRA] do not include EXTRA
  14116. For backward compatibility, if both the positive and the negative place
  14117. holder is missing, the positive one (without the \"NO-\") will be
  14118. assumed to be present at the end of the template.
  14119. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  14120. EXTRA is a string.
  14121. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  14122. (let (rpl (end ""))
  14123. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  14124. (setq rpl (if (or (match-end 1) (not def-pkg))
  14125. "" (org-latex-packages-to-string def-pkg snippets-p t))
  14126. tpl (replace-match rpl t t tpl))
  14127. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  14128. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  14129. (setq rpl (if (or (match-end 1) (not pkg))
  14130. "" (org-latex-packages-to-string pkg snippets-p t))
  14131. tpl (replace-match rpl t t tpl))
  14132. (if pkg (setq end
  14133. (concat end "\n"
  14134. (org-latex-packages-to-string pkg snippets-p)))))
  14135. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  14136. (setq rpl (if (or (match-end 1) (not extra))
  14137. "" (concat extra "\n"))
  14138. tpl (replace-match rpl t t tpl))
  14139. (if (and extra (string-match "\\S-" extra))
  14140. (setq end (concat end "\n" extra))))
  14141. (if (string-match "\\S-" end)
  14142. (concat tpl "\n" end)
  14143. tpl)))
  14144. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  14145. "Turn an alist of packages into a string with the \\usepackage macros."
  14146. (setq pkg (mapconcat (lambda(p)
  14147. (cond
  14148. ((stringp p) p)
  14149. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  14150. (format "%% Package %s omitted" (cadr p)))
  14151. ((equal "" (car p))
  14152. (format "\\usepackage{%s}" (cadr p)))
  14153. (t
  14154. (format "\\usepackage[%s]{%s}"
  14155. (car p) (cadr p)))))
  14156. pkg
  14157. "\n"))
  14158. (if newline (concat pkg "\n") pkg))
  14159. (defun org-dvipng-color (attr)
  14160. "Return an rgb color specification for dvipng."
  14161. (apply 'format "rgb %s %s %s"
  14162. (mapcar 'org-normalize-color
  14163. (color-values (face-attribute 'default attr nil)))))
  14164. (defun org-normalize-color (value)
  14165. "Return string to be used as color value for an RGB component."
  14166. (format "%g" (/ value 65535.0)))
  14167. ;; Image display
  14168. (defvar org-inline-image-overlays nil)
  14169. (make-variable-buffer-local 'org-inline-image-overlays)
  14170. (defun org-toggle-inline-images (&optional include-linked)
  14171. "Toggle the display of inline images.
  14172. INCLUDE-LINKED is passed to `org-display-inline-images'."
  14173. (interactive "P")
  14174. (if org-inline-image-overlays
  14175. (progn
  14176. (org-remove-inline-images)
  14177. (message "Inline image display turned off"))
  14178. (org-display-inline-images include-linked)
  14179. (if org-inline-image-overlays
  14180. (message "%d images displayed inline"
  14181. (length org-inline-image-overlays))
  14182. (message "No images to display inline"))))
  14183. (defun org-display-inline-images (&optional include-linked)
  14184. "Display inline images.
  14185. Normally only links without a description part are inlined, because this
  14186. is how it will work for export. When INCLUDE-LINKED is set, also links
  14187. with a description part will be inlined."
  14188. (interactive "P")
  14189. (org-remove-inline-images)
  14190. (goto-char (point-min))
  14191. (let ((re (concat "\\[\\[\\(file:\\|\\./\\)\\(~?" "[-+./_0-9a-zA-Z]+"
  14192. (substring (org-image-file-name-regexp) 0 -2)
  14193. "\\)\\]" (if include-linked "" "\\]")))
  14194. file ov)
  14195. (while (re-search-forward re nil t)
  14196. (setq file (expand-file-name (match-string 2)))
  14197. (when (file-exists-p file)
  14198. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  14199. (overlay-put ov 'display (create-image file))
  14200. (overlay-put ov 'face 'default)
  14201. (push ov org-inline-image-overlays)))))
  14202. (defun org-remove-inline-images ()
  14203. "Remove inline display of images."
  14204. (interactive)
  14205. (mapc 'delete-overlay org-inline-image-overlays)
  14206. (setq org-inline-image-overlays nil))
  14207. ;;;; Key bindings
  14208. ;; Make `C-c C-x' a prefix key
  14209. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  14210. ;; TAB key with modifiers
  14211. (org-defkey org-mode-map "\C-i" 'org-cycle)
  14212. (org-defkey org-mode-map [(tab)] 'org-cycle)
  14213. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  14214. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  14215. (org-defkey org-mode-map "\M-\t" 'org-complete)
  14216. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  14217. ;; The following line is necessary under Suse GNU/Linux
  14218. (unless (featurep 'xemacs)
  14219. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  14220. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  14221. (define-key org-mode-map [backtab] 'org-shifttab)
  14222. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  14223. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  14224. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  14225. ;; Cursor keys with modifiers
  14226. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  14227. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  14228. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  14229. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  14230. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  14231. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  14232. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  14233. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  14234. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  14235. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  14236. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  14237. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  14238. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  14239. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  14240. ;;; Extra keys for tty access.
  14241. ;; We only set them when really needed because otherwise the
  14242. ;; menus don't show the simple keys
  14243. (when (or org-use-extra-keys
  14244. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  14245. (not window-system))
  14246. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  14247. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  14248. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  14249. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  14250. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  14251. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  14252. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  14253. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  14254. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  14255. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  14256. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  14257. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  14258. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  14259. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  14260. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  14261. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  14262. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  14263. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  14264. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  14265. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  14266. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  14267. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  14268. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  14269. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  14270. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  14271. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  14272. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  14273. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  14274. ;; All the other keys
  14275. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  14276. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  14277. (if (boundp 'narrow-map)
  14278. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  14279. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  14280. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  14281. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  14282. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  14283. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  14284. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  14285. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  14286. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  14287. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  14288. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  14289. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  14290. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  14291. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  14292. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  14293. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  14294. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  14295. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  14296. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  14297. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  14298. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  14299. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  14300. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  14301. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  14302. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  14303. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  14304. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  14305. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  14306. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  14307. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  14308. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  14309. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  14310. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  14311. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  14312. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  14313. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  14314. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  14315. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  14316. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  14317. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  14318. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  14319. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  14320. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  14321. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14322. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  14323. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  14324. (org-defkey org-mode-map "\C-c^" 'org-sort)
  14325. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  14326. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  14327. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  14328. (org-defkey org-mode-map "\C-m" 'org-return)
  14329. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  14330. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  14331. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  14332. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  14333. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  14334. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  14335. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  14336. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  14337. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  14338. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  14339. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  14340. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  14341. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  14342. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  14343. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  14344. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  14345. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  14346. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  14347. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  14348. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  14349. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  14350. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  14351. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  14352. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  14353. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  14354. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  14355. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  14356. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  14357. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  14358. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  14359. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  14360. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  14361. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  14362. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  14363. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  14364. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  14365. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  14366. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  14367. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  14368. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  14369. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  14370. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  14371. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  14372. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  14373. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  14374. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  14375. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  14376. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  14377. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  14378. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  14379. (when (featurep 'xemacs)
  14380. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  14381. (defconst org-speed-commands-default
  14382. '(
  14383. ("Outline Navigation")
  14384. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  14385. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  14386. ("f" . (org-speed-move-safe 'org-forward-same-level))
  14387. ("b" . (org-speed-move-safe 'org-backward-same-level))
  14388. ("u" . (org-speed-move-safe 'outline-up-heading))
  14389. ("j" . org-goto)
  14390. ("g" . (org-refile t))
  14391. ("Outline Visibility")
  14392. ("c" . org-cycle)
  14393. ("C" . org-shifttab)
  14394. (" " . org-display-outline-path)
  14395. ("Outline Structure Editing")
  14396. ("U" . org-shiftmetaup)
  14397. ("D" . org-shiftmetadown)
  14398. ("r" . org-metaright)
  14399. ("l" . org-metaleft)
  14400. ("R" . org-shiftmetaright)
  14401. ("L" . org-shiftmetaleft)
  14402. ("i" . (progn (forward-char 1) (call-interactively
  14403. 'org-insert-heading-respect-content)))
  14404. ("^" . org-sort)
  14405. ("w" . org-refile)
  14406. ("a" . org-archive-subtree-default-with-confirmation)
  14407. ("." . outline-mark-subtree)
  14408. ("Clock Commands")
  14409. ("I" . org-clock-in)
  14410. ("O" . org-clock-out)
  14411. ("Meta Data Editing")
  14412. ("t" . org-todo)
  14413. ("0" . (org-priority ?\ ))
  14414. ("1" . (org-priority ?A))
  14415. ("2" . (org-priority ?B))
  14416. ("3" . (org-priority ?C))
  14417. (";" . org-set-tags-command)
  14418. ("e" . org-set-effort)
  14419. ("Agenda Views etc")
  14420. ("v" . org-agenda)
  14421. ("/" . org-sparse-tree)
  14422. ("Misc")
  14423. ("o" . org-open-at-point)
  14424. ("?" . org-speed-command-help)
  14425. )
  14426. "The default speed commands.")
  14427. (defun org-print-speed-command (e)
  14428. (if (> (length (car e)) 1)
  14429. (progn
  14430. (princ "\n")
  14431. (princ (car e))
  14432. (princ "\n")
  14433. (princ (make-string (length (car e)) ?-))
  14434. (princ "\n"))
  14435. (princ (car e))
  14436. (princ " ")
  14437. (if (symbolp (cdr e))
  14438. (princ (symbol-name (cdr e)))
  14439. (prin1 (cdr e)))
  14440. (princ "\n")))
  14441. (defun org-speed-command-help ()
  14442. "Show the available speed commands."
  14443. (interactive)
  14444. (if (not org-use-speed-commands)
  14445. (error "Speed commands are not activated, customize `org-use-speed-commands'.")
  14446. (with-output-to-temp-buffer "*Help*"
  14447. (princ "User-defined Speed commands\n===========================\n")
  14448. (mapc 'org-print-speed-command org-speed-commands-user)
  14449. (princ "\n")
  14450. (princ "Built-in Speed commands\n=======================\n")
  14451. (mapc 'org-print-speed-command org-speed-commands-default))
  14452. (with-current-buffer "*Help*"
  14453. (setq truncate-lines t))))
  14454. (defun org-speed-move-safe (cmd)
  14455. "Execute CMD, but make sure that the cursor always ends up in a headline.
  14456. If not, return to the original position and throw an error."
  14457. (interactive)
  14458. (let ((pos (point)))
  14459. (call-interactively cmd)
  14460. (unless (and (bolp) (org-on-heading-p))
  14461. (goto-char pos)
  14462. (error "Boundary reached while executing %s" cmd))))
  14463. (defvar org-self-insert-command-undo-counter 0)
  14464. (defvar org-table-auto-blank-field) ; defined in org-table.el
  14465. (defvar org-speed-command nil)
  14466. (defun org-self-insert-command (N)
  14467. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  14468. If the cursor is in a table looking at whitespace, the whitespace is
  14469. overwritten, and the table is not marked as requiring realignment."
  14470. (interactive "p")
  14471. (cond
  14472. ((and org-use-speed-commands
  14473. (or (and (bolp) (looking-at outline-regexp))
  14474. (and (functionp org-use-speed-commands)
  14475. (funcall org-use-speed-commands)))
  14476. (setq
  14477. org-speed-command
  14478. (or (cdr (assoc (this-command-keys) org-speed-commands-user))
  14479. (cdr (assoc (this-command-keys) org-speed-commands-default)))))
  14480. (cond
  14481. ((commandp org-speed-command)
  14482. (setq this-command org-speed-command)
  14483. (call-interactively org-speed-command))
  14484. ((functionp org-speed-command)
  14485. (funcall org-speed-command))
  14486. ((and org-speed-command (listp org-speed-command))
  14487. (eval org-speed-command))
  14488. (t (let (org-use-speed-commands)
  14489. (call-interactively 'org-self-insert-command)))))
  14490. ((and
  14491. (org-table-p)
  14492. (progn
  14493. ;; check if we blank the field, and if that triggers align
  14494. (and (featurep 'org-table) org-table-auto-blank-field
  14495. (member last-command
  14496. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  14497. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  14498. ;; got extra space, this field does not determine column width
  14499. (let (org-table-may-need-update) (org-table-blank-field))
  14500. ;; no extra space, this field may determine column width
  14501. (org-table-blank-field)))
  14502. t)
  14503. (eq N 1)
  14504. (looking-at "[^|\n]* |"))
  14505. (let (org-table-may-need-update)
  14506. (goto-char (1- (match-end 0)))
  14507. (delete-backward-char 1)
  14508. (goto-char (match-beginning 0))
  14509. (self-insert-command N)))
  14510. (t
  14511. (setq org-table-may-need-update t)
  14512. (self-insert-command N)
  14513. (org-fix-tags-on-the-fly)
  14514. (if org-self-insert-cluster-for-undo
  14515. (if (not (eq last-command 'org-self-insert-command))
  14516. (setq org-self-insert-command-undo-counter 1)
  14517. (if (>= org-self-insert-command-undo-counter 20)
  14518. (setq org-self-insert-command-undo-counter 1)
  14519. (and (> org-self-insert-command-undo-counter 0)
  14520. buffer-undo-list
  14521. (not (cadr buffer-undo-list)) ; remove nil entry
  14522. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  14523. (setq org-self-insert-command-undo-counter
  14524. (1+ org-self-insert-command-undo-counter))))))))
  14525. (defun org-fix-tags-on-the-fly ()
  14526. (when (and (equal (char-after (point-at-bol)) ?*)
  14527. (org-on-heading-p))
  14528. (org-align-tags-here org-tags-column)))
  14529. (defun org-delete-backward-char (N)
  14530. "Like `delete-backward-char', insert whitespace at field end in tables.
  14531. When deleting backwards, in tables this function will insert whitespace in
  14532. front of the next \"|\" separator, to keep the table aligned. The table will
  14533. still be marked for re-alignment if the field did fill the entire column,
  14534. because, in this case the deletion might narrow the column."
  14535. (interactive "p")
  14536. (if (and (org-table-p)
  14537. (eq N 1)
  14538. (string-match "|" (buffer-substring (point-at-bol) (point)))
  14539. (looking-at ".*?|"))
  14540. (let ((pos (point))
  14541. (noalign (looking-at "[^|\n\r]* |"))
  14542. (c org-table-may-need-update))
  14543. (backward-delete-char N)
  14544. (skip-chars-forward "^|")
  14545. (insert " ")
  14546. (goto-char (1- pos))
  14547. ;; noalign: if there were two spaces at the end, this field
  14548. ;; does not determine the width of the column.
  14549. (if noalign (setq org-table-may-need-update c)))
  14550. (backward-delete-char N)
  14551. (org-fix-tags-on-the-fly)))
  14552. (defun org-delete-char (N)
  14553. "Like `delete-char', but insert whitespace at field end in tables.
  14554. When deleting characters, in tables this function will insert whitespace in
  14555. front of the next \"|\" separator, to keep the table aligned. The table will
  14556. still be marked for re-alignment if the field did fill the entire column,
  14557. because, in this case the deletion might narrow the column."
  14558. (interactive "p")
  14559. (if (and (org-table-p)
  14560. (not (bolp))
  14561. (not (= (char-after) ?|))
  14562. (eq N 1))
  14563. (if (looking-at ".*?|")
  14564. (let ((pos (point))
  14565. (noalign (looking-at "[^|\n\r]* |"))
  14566. (c org-table-may-need-update))
  14567. (replace-match (concat
  14568. (substring (match-string 0) 1 -1)
  14569. " |"))
  14570. (goto-char pos)
  14571. ;; noalign: if there were two spaces at the end, this field
  14572. ;; does not determine the width of the column.
  14573. (if noalign (setq org-table-may-need-update c)))
  14574. (delete-char N))
  14575. (delete-char N)
  14576. (org-fix-tags-on-the-fly)))
  14577. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  14578. (put 'org-self-insert-command 'delete-selection t)
  14579. (put 'orgtbl-self-insert-command 'delete-selection t)
  14580. (put 'org-delete-char 'delete-selection 'supersede)
  14581. (put 'org-delete-backward-char 'delete-selection 'supersede)
  14582. (put 'org-yank 'delete-selection 'yank)
  14583. ;; Make `flyspell-mode' delay after some commands
  14584. (put 'org-self-insert-command 'flyspell-delayed t)
  14585. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  14586. (put 'org-delete-char 'flyspell-delayed t)
  14587. (put 'org-delete-backward-char 'flyspell-delayed t)
  14588. ;; Make pabbrev-mode expand after org-mode commands
  14589. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  14590. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  14591. ;; How to do this: Measure non-white length of current string
  14592. ;; If equal to column width, we should realign.
  14593. (defun org-remap (map &rest commands)
  14594. "In MAP, remap the functions given in COMMANDS.
  14595. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  14596. (let (new old)
  14597. (while commands
  14598. (setq old (pop commands) new (pop commands))
  14599. (if (fboundp 'command-remapping)
  14600. (org-defkey map (vector 'remap old) new)
  14601. (substitute-key-definition old new map global-map)))))
  14602. (when (eq org-enable-table-editor 'optimized)
  14603. ;; If the user wants maximum table support, we need to hijack
  14604. ;; some standard editing functions
  14605. (org-remap org-mode-map
  14606. 'self-insert-command 'org-self-insert-command
  14607. 'delete-char 'org-delete-char
  14608. 'delete-backward-char 'org-delete-backward-char)
  14609. (org-defkey org-mode-map "|" 'org-force-self-insert))
  14610. (defvar org-ctrl-c-ctrl-c-hook nil
  14611. "Hook for functions attaching themselves to `C-c C-c'.
  14612. This can be used to add additional functionality to the C-c C-c key which
  14613. executes context-dependent commands.
  14614. Each function will be called with no arguments. The function must check
  14615. if the context is appropriate for it to act. If yes, it should do its
  14616. thing and then return a non-nil value. If the context is wrong,
  14617. just do nothing and return nil.")
  14618. (defvar org-tab-first-hook nil
  14619. "Hook for functions to attach themselves to TAB.
  14620. See `org-ctrl-c-ctrl-c-hook' for more information.
  14621. This hook runs as the first action when TAB is pressed, even before
  14622. `org-cycle' messes around with the `outline-regexp' to cater for
  14623. inline tasks and plain list item folding.
  14624. If any function in this hook returns t, any other actions that
  14625. would have been caused by TAB (such as table field motion or visibility
  14626. cycling) will not occur.")
  14627. (defvar org-tab-after-check-for-table-hook nil
  14628. "Hook for functions to attach themselves to TAB.
  14629. See `org-ctrl-c-ctrl-c-hook' for more information.
  14630. This hook runs after it has been established that the cursor is not in a
  14631. table, but before checking if the cursor is in a headline or if global cycling
  14632. should be done.
  14633. If any function in this hook returns t, not other actions like visibility
  14634. cycling will be done.")
  14635. (defvar org-tab-after-check-for-cycling-hook nil
  14636. "Hook for functions to attach themselves to TAB.
  14637. See `org-ctrl-c-ctrl-c-hook' for more information.
  14638. This hook runs after it has been established that not table field motion and
  14639. not visibility should be done because of current context. This is probably
  14640. the place where a package like yasnippets can hook in.")
  14641. (defvar org-tab-before-tab-emulation-hook nil
  14642. "Hook for functions to attach themselves to TAB.
  14643. See `org-ctrl-c-ctrl-c-hook' for more information.
  14644. This hook runs after every other options for TAB have been exhausted, but
  14645. before indentation and \t insertion takes place.")
  14646. (defvar org-metaleft-hook nil
  14647. "Hook for functions attaching themselves to `M-left'.
  14648. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14649. (defvar org-metaright-hook nil
  14650. "Hook for functions attaching themselves to `M-right'.
  14651. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14652. (defvar org-metaup-hook nil
  14653. "Hook for functions attaching themselves to `M-up'.
  14654. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14655. (defvar org-metadown-hook nil
  14656. "Hook for functions attaching themselves to `M-down'.
  14657. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14658. (defvar org-shiftmetaleft-hook nil
  14659. "Hook for functions attaching themselves to `M-S-left'.
  14660. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14661. (defvar org-shiftmetaright-hook nil
  14662. "Hook for functions attaching themselves to `M-S-right'.
  14663. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14664. (defvar org-shiftmetaup-hook nil
  14665. "Hook for functions attaching themselves to `M-S-up'.
  14666. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14667. (defvar org-shiftmetadown-hook nil
  14668. "Hook for functions attaching themselves to `M-S-down'.
  14669. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14670. (defvar org-metareturn-hook nil
  14671. "Hook for functions attaching themselves to `M-RET'.
  14672. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14673. (defvar org-shiftup-hook nil
  14674. "Hook for functions attaching themselves to `S-up'.
  14675. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14676. (defvar org-shiftup-final-hook nil
  14677. "Hook for functions attaching themselves to `S-up'.
  14678. This one runs after all other options except shift-select have been excluded.
  14679. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14680. (defvar org-shiftdown-hook nil
  14681. "Hook for functions attaching themselves to `S-down'.
  14682. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14683. (defvar org-shiftdown-final-hook nil
  14684. "Hook for functions attaching themselves to `S-down'.
  14685. This one runs after all other options except shift-select have been excluded.
  14686. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14687. (defvar org-shiftleft-hook nil
  14688. "Hook for functions attaching themselves to `S-left'.
  14689. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14690. (defvar org-shiftleft-final-hook nil
  14691. "Hook for functions attaching themselves to `S-left'.
  14692. This one runs after all other options except shift-select have been excluded.
  14693. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14694. (defvar org-shiftright-hook nil
  14695. "Hook for functions attaching themselves to `S-right'.
  14696. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14697. (defvar org-shiftright-final-hook nil
  14698. "Hook for functions attaching themselves to `S-right'.
  14699. This one runs after all other options except shift-select have been excluded.
  14700. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14701. (defun org-modifier-cursor-error ()
  14702. "Throw an error, a modified cursor command was applied in wrong context."
  14703. (error "This command is active in special context like tables, headlines or items"))
  14704. (defun org-shiftselect-error ()
  14705. "Throw an error because Shift-Cursor command was applied in wrong context."
  14706. (if (and (boundp 'shift-select-mode) shift-select-mode)
  14707. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  14708. (error "This command works only in special context like headlines or timestamps")))
  14709. (defun org-call-for-shift-select (cmd)
  14710. (let ((this-command-keys-shift-translated t))
  14711. (call-interactively cmd)))
  14712. (defun org-shifttab (&optional arg)
  14713. "Global visibility cycling or move to previous table field.
  14714. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  14715. on context.
  14716. See the individual commands for more information."
  14717. (interactive "P")
  14718. (cond
  14719. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  14720. ((integerp arg)
  14721. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  14722. (message "Content view to level: %d" arg)
  14723. (org-content (prefix-numeric-value arg2))
  14724. (setq org-cycle-global-status 'overview)))
  14725. (t (call-interactively 'org-global-cycle))))
  14726. (defun org-shiftmetaleft ()
  14727. "Promote subtree or delete table column.
  14728. Calls `org-promote-subtree', `org-outdent-item',
  14729. or `org-table-delete-column', depending on context.
  14730. See the individual commands for more information."
  14731. (interactive)
  14732. (cond
  14733. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  14734. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  14735. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  14736. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  14737. (t (org-modifier-cursor-error))))
  14738. (defun org-shiftmetaright ()
  14739. "Demote subtree or insert table column.
  14740. Calls `org-demote-subtree', `org-indent-item',
  14741. or `org-table-insert-column', depending on context.
  14742. See the individual commands for more information."
  14743. (interactive)
  14744. (cond
  14745. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  14746. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  14747. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  14748. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  14749. (t (org-modifier-cursor-error))))
  14750. (defun org-shiftmetaup (&optional arg)
  14751. "Move subtree up or kill table row.
  14752. Calls `org-move-subtree-up' or `org-table-kill-row' or
  14753. `org-move-item-up' depending on context. See the individual commands
  14754. for more information."
  14755. (interactive "P")
  14756. (cond
  14757. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  14758. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  14759. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14760. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14761. (t (org-modifier-cursor-error))))
  14762. (defun org-shiftmetadown (&optional arg)
  14763. "Move subtree down or insert table row.
  14764. Calls `org-move-subtree-down' or `org-table-insert-row' or
  14765. `org-move-item-down', depending on context. See the individual
  14766. commands for more information."
  14767. (interactive "P")
  14768. (cond
  14769. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  14770. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  14771. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14772. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14773. (t (org-modifier-cursor-error))))
  14774. (defsubst org-hidden-tree-error ()
  14775. (error
  14776. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  14777. (defun org-metaleft (&optional arg)
  14778. "Promote heading or move table column to left.
  14779. Calls `org-do-promote' or `org-table-move-column', depending on context.
  14780. With no specific context, calls the Emacs default `backward-word'.
  14781. See the individual commands for more information."
  14782. (interactive "P")
  14783. (cond
  14784. ((run-hook-with-args-until-success 'org-metaleft-hook))
  14785. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  14786. ((or (org-on-heading-p)
  14787. (and (org-region-active-p)
  14788. (save-excursion
  14789. (goto-char (region-beginning))
  14790. (org-on-heading-p))))
  14791. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  14792. (call-interactively 'org-do-promote))
  14793. ((or (org-at-item-p)
  14794. (and (org-region-active-p)
  14795. (save-excursion
  14796. (goto-char (region-beginning))
  14797. (org-at-item-p))))
  14798. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  14799. (call-interactively 'org-outdent-item))
  14800. (t (call-interactively 'backward-word))))
  14801. (defun org-metaright (&optional arg)
  14802. "Demote subtree or move table column to right.
  14803. Calls `org-do-demote' or `org-table-move-column', depending on context.
  14804. With no specific context, calls the Emacs default `forward-word'.
  14805. See the individual commands for more information."
  14806. (interactive "P")
  14807. (cond
  14808. ((run-hook-with-args-until-success 'org-metaright-hook))
  14809. ((org-at-table-p) (call-interactively 'org-table-move-column))
  14810. ((or (org-on-heading-p)
  14811. (and (org-region-active-p)
  14812. (save-excursion
  14813. (goto-char (region-beginning))
  14814. (org-on-heading-p))))
  14815. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  14816. (call-interactively 'org-do-demote))
  14817. ((or (org-at-item-p)
  14818. (and (org-region-active-p)
  14819. (save-excursion
  14820. (goto-char (region-beginning))
  14821. (org-at-item-p))))
  14822. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  14823. (call-interactively 'org-indent-item))
  14824. (t (call-interactively 'forward-word))))
  14825. (defun org-check-for-hidden (what)
  14826. "Check if there are hidden headlines/items in the current visual line.
  14827. WHAT can be either `headlines' or `items'. If the current line is
  14828. an outline or item heading and it has a folded subtree below it,
  14829. this fucntion returns t, nil otherwise."
  14830. (let ((re (cond
  14831. ((eq what 'headlines) (concat "^" org-outline-regexp))
  14832. ((eq what 'items) (concat "^" (org-item-re t)))
  14833. (t (error "This should not happen"))))
  14834. beg end)
  14835. (save-excursion
  14836. (catch 'exit
  14837. (unless (org-region-active-p)
  14838. (setq beg (point-at-bol))
  14839. (beginning-of-line 2)
  14840. (while (and (not (eobp)) ;; this is like `next-line'
  14841. (get-char-property (1- (point)) 'invisible))
  14842. (beginning-of-line 2))
  14843. (setq end (point))
  14844. (goto-char beg)
  14845. (goto-char (point-at-eol))
  14846. (setq end (max end (point)))
  14847. (while (re-search-forward re end t)
  14848. (if (get-char-property (match-beginning 0) 'invisible)
  14849. (throw 'exit t))))
  14850. nil))))
  14851. (defun org-metaup (&optional arg)
  14852. "Move subtree up or move table row up.
  14853. Calls `org-move-subtree-up' or `org-table-move-row' or
  14854. `org-move-item-up', depending on context. See the individual commands
  14855. for more information."
  14856. (interactive "P")
  14857. (cond
  14858. ((run-hook-with-args-until-success 'org-metaup-hook))
  14859. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  14860. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14861. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14862. (t (transpose-lines 1) (beginning-of-line -1))))
  14863. (defun org-metadown (&optional arg)
  14864. "Move subtree down or move table row down.
  14865. Calls `org-move-subtree-down' or `org-table-move-row' or
  14866. `org-move-item-down', depending on context. See the individual
  14867. commands for more information."
  14868. (interactive "P")
  14869. (cond
  14870. ((run-hook-with-args-until-success 'org-metadown-hook))
  14871. ((org-at-table-p) (call-interactively 'org-table-move-row))
  14872. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14873. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14874. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  14875. (defun org-shiftup (&optional arg)
  14876. "Increase item in timestamp or increase priority of current headline.
  14877. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  14878. depending on context. See the individual commands for more information."
  14879. (interactive "P")
  14880. (cond
  14881. ((run-hook-with-args-until-success 'org-shiftup-hook))
  14882. ((and org-support-shift-select (org-region-active-p))
  14883. (org-call-for-shift-select 'previous-line))
  14884. ((org-at-timestamp-p t)
  14885. (call-interactively (if org-edit-timestamp-down-means-later
  14886. 'org-timestamp-down 'org-timestamp-up)))
  14887. ((and (not (eq org-support-shift-select 'always))
  14888. org-enable-priority-commands
  14889. (org-on-heading-p))
  14890. (call-interactively 'org-priority-up))
  14891. ((and (not org-support-shift-select) (org-at-item-p))
  14892. (call-interactively 'org-previous-item))
  14893. ((org-clocktable-try-shift 'up arg))
  14894. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  14895. (org-support-shift-select
  14896. (org-call-for-shift-select 'previous-line))
  14897. (t (org-shiftselect-error))))
  14898. (defun org-shiftdown (&optional arg)
  14899. "Decrease item in timestamp or decrease priority of current headline.
  14900. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  14901. depending on context. See the individual commands for more information."
  14902. (interactive "P")
  14903. (cond
  14904. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  14905. ((and org-support-shift-select (org-region-active-p))
  14906. (org-call-for-shift-select 'next-line))
  14907. ((org-at-timestamp-p t)
  14908. (call-interactively (if org-edit-timestamp-down-means-later
  14909. 'org-timestamp-up 'org-timestamp-down)))
  14910. ((and (not (eq org-support-shift-select 'always))
  14911. org-enable-priority-commands
  14912. (org-on-heading-p))
  14913. (call-interactively 'org-priority-down))
  14914. ((and (not org-support-shift-select) (org-at-item-p))
  14915. (call-interactively 'org-next-item))
  14916. ((org-clocktable-try-shift 'down arg))
  14917. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  14918. (org-support-shift-select
  14919. (org-call-for-shift-select 'next-line))
  14920. (t (org-shiftselect-error))))
  14921. (defun org-shiftright (&optional arg)
  14922. "Cycle the thing at point or in the current line, depending on context.
  14923. Depending on context, this does one of the following:
  14924. - switch a timestamp at point one day into the future
  14925. - on a headline, switch to the next TODO keyword.
  14926. - on an item, switch entire list to the next bullet type
  14927. - on a property line, switch to the next allowed value
  14928. - on a clocktable definition line, move time block into the future"
  14929. (interactive "P")
  14930. (cond
  14931. ((run-hook-with-args-until-success 'org-shiftright-hook))
  14932. ((and org-support-shift-select (org-region-active-p))
  14933. (org-call-for-shift-select 'forward-char))
  14934. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  14935. ((and (not (eq org-support-shift-select 'always))
  14936. (org-on-heading-p))
  14937. (let ((org-inhibit-logging
  14938. (not org-treat-S-cursor-todo-selection-as-state-change))
  14939. (org-inhibit-blocking
  14940. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14941. (org-call-with-arg 'org-todo 'right)))
  14942. ((or (and org-support-shift-select
  14943. (not (eq org-support-shift-select 'always))
  14944. (org-at-item-bullet-p))
  14945. (and (not org-support-shift-select) (org-at-item-p)))
  14946. (org-call-with-arg 'org-cycle-list-bullet nil))
  14947. ((and (not (eq org-support-shift-select 'always))
  14948. (org-at-property-p))
  14949. (call-interactively 'org-property-next-allowed-value))
  14950. ((org-clocktable-try-shift 'right arg))
  14951. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  14952. (org-support-shift-select
  14953. (org-call-for-shift-select 'forward-char))
  14954. (t (org-shiftselect-error))))
  14955. (defun org-shiftleft (&optional arg)
  14956. "Cycle the thing at point or in the current line, depending on context.
  14957. Depending on context, this does one of the following:
  14958. - switch a timestamp at point one day into the past
  14959. - on a headline, switch to the previous TODO keyword.
  14960. - on an item, switch entire list to the previous bullet type
  14961. - on a property line, switch to the previous allowed value
  14962. - on a clocktable definition line, move time block into the past"
  14963. (interactive "P")
  14964. (cond
  14965. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  14966. ((and org-support-shift-select (org-region-active-p))
  14967. (org-call-for-shift-select 'backward-char))
  14968. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  14969. ((and (not (eq org-support-shift-select 'always))
  14970. (org-on-heading-p))
  14971. (let ((org-inhibit-logging
  14972. (not org-treat-S-cursor-todo-selection-as-state-change))
  14973. (org-inhibit-blocking
  14974. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14975. (org-call-with-arg 'org-todo 'left)))
  14976. ((or (and org-support-shift-select
  14977. (not (eq org-support-shift-select 'always))
  14978. (org-at-item-bullet-p))
  14979. (and (not org-support-shift-select) (org-at-item-p)))
  14980. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  14981. ((and (not (eq org-support-shift-select 'always))
  14982. (org-at-property-p))
  14983. (call-interactively 'org-property-previous-allowed-value))
  14984. ((org-clocktable-try-shift 'left arg))
  14985. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  14986. (org-support-shift-select
  14987. (org-call-for-shift-select 'backward-char))
  14988. (t (org-shiftselect-error))))
  14989. (defun org-shiftcontrolright ()
  14990. "Switch to next TODO set."
  14991. (interactive)
  14992. (cond
  14993. ((and org-support-shift-select (org-region-active-p))
  14994. (org-call-for-shift-select 'forward-word))
  14995. ((and (not (eq org-support-shift-select 'always))
  14996. (org-on-heading-p))
  14997. (org-call-with-arg 'org-todo 'nextset))
  14998. (org-support-shift-select
  14999. (org-call-for-shift-select 'forward-word))
  15000. (t (org-shiftselect-error))))
  15001. (defun org-shiftcontrolleft ()
  15002. "Switch to previous TODO set."
  15003. (interactive)
  15004. (cond
  15005. ((and org-support-shift-select (org-region-active-p))
  15006. (org-call-for-shift-select 'backward-word))
  15007. ((and (not (eq org-support-shift-select 'always))
  15008. (org-on-heading-p))
  15009. (org-call-with-arg 'org-todo 'previousset))
  15010. (org-support-shift-select
  15011. (org-call-for-shift-select 'backward-word))
  15012. (t (org-shiftselect-error))))
  15013. (defun org-ctrl-c-ret ()
  15014. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  15015. (interactive)
  15016. (cond
  15017. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  15018. (t (call-interactively 'org-insert-heading))))
  15019. (defun org-copy-special ()
  15020. "Copy region in table or copy current subtree.
  15021. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  15022. See the individual commands for more information."
  15023. (interactive)
  15024. (call-interactively
  15025. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  15026. (defun org-cut-special ()
  15027. "Cut region in table or cut current subtree.
  15028. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  15029. See the individual commands for more information."
  15030. (interactive)
  15031. (call-interactively
  15032. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  15033. (defun org-paste-special (arg)
  15034. "Paste rectangular region into table, or past subtree relative to level.
  15035. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  15036. See the individual commands for more information."
  15037. (interactive "P")
  15038. (if (org-at-table-p)
  15039. (org-table-paste-rectangle)
  15040. (org-paste-subtree arg)))
  15041. (defun org-edit-special ()
  15042. "Call a special editor for the stuff at point.
  15043. When at a table, call the formula editor with `org-table-edit-formulas'.
  15044. When at the first line of an src example, call `org-edit-src-code'.
  15045. When in an #+include line, visit the include file. Otherwise call
  15046. `ffap' to visit the file at point."
  15047. (interactive)
  15048. (cond
  15049. ((org-at-table.el-p)
  15050. (org-edit-src-code))
  15051. ((org-at-table-p)
  15052. (call-interactively 'org-table-edit-formulas))
  15053. ((save-excursion
  15054. (beginning-of-line 1)
  15055. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  15056. (find-file (org-trim (match-string 1))))
  15057. ((org-edit-src-code))
  15058. ((org-edit-fixed-width-region))
  15059. (t (call-interactively 'ffap))))
  15060. (defun org-ctrl-c-ctrl-c (&optional arg)
  15061. "Set tags in headline, or update according to changed information at point.
  15062. This command does many different things, depending on context:
  15063. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  15064. this is what we do.
  15065. - If the cursor is on a statistics cookie, update it.
  15066. - If the cursor is in a headline, prompt for tags and insert them
  15067. into the current line, aligned to `org-tags-column'. When called
  15068. with prefix arg, realign all tags in the current buffer.
  15069. - If the cursor is in one of the special #+KEYWORD lines, this
  15070. triggers scanning the buffer for these lines and updating the
  15071. information.
  15072. - If the cursor is inside a table, realign the table. This command
  15073. works even if the automatic table editor has been turned off.
  15074. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  15075. the entire table.
  15076. - If the cursor is at a footnote reference or definition, jump to
  15077. the corresponding definition or references, respectively.
  15078. - If the cursor is a the beginning of a dynamic block, update it.
  15079. - If the current buffer is a remember buffer, close note and file
  15080. it. A prefix argument of 1 files to the default location
  15081. without further interaction. A prefix argument of 2 files to
  15082. the currently clocking task.
  15083. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  15084. links in this buffer.
  15085. - If the cursor is on a numbered item in a plain list, renumber the
  15086. ordered list.
  15087. - If the cursor is on a checkbox, toggle it."
  15088. (interactive "P")
  15089. (let ((org-enable-table-editor t))
  15090. (cond
  15091. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  15092. org-occur-highlights
  15093. org-latex-fragment-image-overlays)
  15094. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  15095. (org-remove-occur-highlights)
  15096. (org-remove-latex-fragment-image-overlays)
  15097. (message "Temporary highlights/overlays removed from current buffer"))
  15098. ((and (local-variable-p 'org-finish-function (current-buffer))
  15099. (fboundp org-finish-function))
  15100. (funcall org-finish-function))
  15101. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  15102. ((or (looking-at org-property-start-re)
  15103. (org-at-property-p))
  15104. (call-interactively 'org-property-action))
  15105. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  15106. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  15107. (or (org-on-heading-p) (org-at-item-p)))
  15108. (call-interactively 'org-update-statistics-cookies))
  15109. ((org-on-heading-p) (call-interactively 'org-set-tags))
  15110. ((org-at-table.el-p)
  15111. (message "Use C-c ' to edit table.el tables"))
  15112. ((org-at-table-p)
  15113. (org-table-maybe-eval-formula)
  15114. (if arg
  15115. (call-interactively 'org-table-recalculate)
  15116. (org-table-maybe-recalculate-line))
  15117. (call-interactively 'org-table-align))
  15118. ((or (org-footnote-at-reference-p)
  15119. (org-footnote-at-definition-p))
  15120. (call-interactively 'org-footnote-action))
  15121. ((org-at-item-checkbox-p)
  15122. (call-interactively 'org-toggle-checkbox))
  15123. ((org-at-item-p)
  15124. (if arg
  15125. (call-interactively 'org-toggle-checkbox)
  15126. (call-interactively 'org-maybe-renumber-ordered-list)))
  15127. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  15128. ;; Dynamic block
  15129. (beginning-of-line 1)
  15130. (save-excursion (org-update-dblock)))
  15131. ((save-excursion
  15132. (beginning-of-line 1)
  15133. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  15134. (cond
  15135. ((equal (match-string 1) "TBLFM")
  15136. ;; Recalculate the table before this line
  15137. (save-excursion
  15138. (beginning-of-line 1)
  15139. (skip-chars-backward " \r\n\t")
  15140. (if (org-at-table-p)
  15141. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  15142. (t
  15143. (let ((org-inhibit-startup-visibility-stuff t)
  15144. (org-startup-align-all-tables nil))
  15145. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  15146. (message "Local setup has been refreshed"))))
  15147. ((org-clock-update-time-maybe))
  15148. (t (error "C-c C-c can do nothing useful at this location")))))
  15149. (defun org-mode-restart ()
  15150. "Restart Org-mode, to scan again for special lines.
  15151. Also updates the keyword regular expressions."
  15152. (interactive)
  15153. (org-mode)
  15154. (message "Org-mode restarted"))
  15155. (defun org-kill-note-or-show-branches ()
  15156. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  15157. (interactive)
  15158. (if (not org-finish-function)
  15159. (call-interactively 'show-branches)
  15160. (let ((org-note-abort t))
  15161. (funcall org-finish-function))))
  15162. (defun org-return (&optional indent)
  15163. "Goto next table row or insert a newline.
  15164. Calls `org-table-next-row' or `newline', depending on context.
  15165. See the individual commands for more information."
  15166. (interactive)
  15167. (cond
  15168. ((bobp) (if indent (newline-and-indent) (newline)))
  15169. ((org-at-table-p)
  15170. (org-table-justify-field-maybe)
  15171. (call-interactively 'org-table-next-row))
  15172. ((and org-return-follows-link
  15173. (eq (get-text-property (point) 'face) 'org-link))
  15174. (call-interactively 'org-open-at-point))
  15175. ((and (org-at-heading-p)
  15176. (looking-at
  15177. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  15178. (org-show-entry)
  15179. (end-of-line 1)
  15180. (newline))
  15181. (t (if indent (newline-and-indent) (newline)))))
  15182. (defun org-return-indent ()
  15183. "Goto next table row or insert a newline and indent.
  15184. Calls `org-table-next-row' or `newline-and-indent', depending on
  15185. context. See the individual commands for more information."
  15186. (interactive)
  15187. (org-return t))
  15188. (defun org-ctrl-c-star ()
  15189. "Compute table, or change heading status of lines.
  15190. Calls `org-table-recalculate' or `org-toggle-heading',
  15191. depending on context."
  15192. (interactive)
  15193. (cond
  15194. ((org-at-table-p)
  15195. (call-interactively 'org-table-recalculate))
  15196. (t
  15197. ;; Convert all lines in region to list items
  15198. (call-interactively 'org-toggle-heading))))
  15199. (defun org-ctrl-c-minus ()
  15200. "Insert separator line in table or modify bullet status of line.
  15201. Also turns a plain line or a region of lines into list items.
  15202. Calls `org-table-insert-hline', `org-toggle-item', or
  15203. `org-cycle-list-bullet', depending on context."
  15204. (interactive)
  15205. (cond
  15206. ((org-at-table-p)
  15207. (call-interactively 'org-table-insert-hline))
  15208. ((org-region-active-p)
  15209. (call-interactively 'org-toggle-item))
  15210. ((org-in-item-p)
  15211. (call-interactively 'org-cycle-list-bullet))
  15212. (t
  15213. (call-interactively 'org-toggle-item))))
  15214. (defun org-toggle-item ()
  15215. "Convert headings or normal lines to items, items to normal lines.
  15216. If there is no active region, only the current line is considered.
  15217. If the first line in the region is a headline, convert all headlines to items.
  15218. If the first line in the region is an item, convert all items to normal lines.
  15219. If the first line is normal text, add an item bullet to each line."
  15220. (interactive)
  15221. (let (l2 l beg end)
  15222. (if (org-region-active-p)
  15223. (setq beg (region-beginning) end (region-end))
  15224. (setq beg (point-at-bol)
  15225. end (min (1+ (point-at-eol)) (point-max))))
  15226. (save-excursion
  15227. (goto-char end)
  15228. (setq l2 (org-current-line))
  15229. (goto-char beg)
  15230. (beginning-of-line 1)
  15231. (setq l (1- (org-current-line)))
  15232. (if (org-at-item-p)
  15233. ;; We already have items, de-itemize
  15234. (while (< (setq l (1+ l)) l2)
  15235. (when (org-at-item-p)
  15236. (goto-char (match-beginning 2))
  15237. (delete-region (match-beginning 2) (match-end 2))
  15238. (and (looking-at "[ \t]+") (replace-match "")))
  15239. (beginning-of-line 2))
  15240. (if (org-on-heading-p)
  15241. ;; Headings, convert to items
  15242. (while (< (setq l (1+ l)) l2)
  15243. (if (looking-at org-outline-regexp)
  15244. (replace-match "- " t t))
  15245. (beginning-of-line 2))
  15246. ;; normal lines, turn them into items
  15247. (while (< (setq l (1+ l)) l2)
  15248. (unless (org-at-item-p)
  15249. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15250. (replace-match "\\1- \\2")))
  15251. (beginning-of-line 2)))))))
  15252. (defun org-toggle-heading (&optional nstars)
  15253. "Convert headings to normal text, or items or text to headings.
  15254. If there is no active region, only the current line is considered.
  15255. If the first line is a heading, remove the stars from all headlines
  15256. in the region.
  15257. If the first line is a plain list item, turn all plain list items
  15258. into headings.
  15259. If the first line is a normal line, turn each and every line in the
  15260. region into a heading.
  15261. When converting a line into a heading, the number of stars is chosen
  15262. such that the lines become children of the current entry. However,
  15263. when a prefix argument is given, its value determines the number of
  15264. stars to add."
  15265. (interactive "P")
  15266. (let (l2 l itemp beg end)
  15267. (if (org-region-active-p)
  15268. (setq beg (region-beginning) end (region-end))
  15269. (setq beg (point-at-bol)
  15270. end (min (1+ (point-at-eol)) (point-max))))
  15271. (save-excursion
  15272. (goto-char end)
  15273. (setq l2 (org-current-line))
  15274. (goto-char beg)
  15275. (beginning-of-line 1)
  15276. (setq l (1- (org-current-line)))
  15277. (if (org-on-heading-p)
  15278. ;; We already have headlines, de-star them
  15279. (while (< (setq l (1+ l)) l2)
  15280. (when (org-on-heading-p t)
  15281. (and (looking-at outline-regexp) (replace-match "")))
  15282. (beginning-of-line 2))
  15283. (setq itemp (org-at-item-p))
  15284. (let* ((stars
  15285. (if nstars
  15286. (make-string (prefix-numeric-value current-prefix-arg)
  15287. ?*)
  15288. (save-excursion
  15289. (if (re-search-backward org-complex-heading-regexp nil t)
  15290. (match-string 1) ""))))
  15291. (add-stars (cond (nstars "")
  15292. ((equal stars "") "*")
  15293. (org-odd-levels-only "**")
  15294. (t "*")))
  15295. (rpl (concat stars add-stars " ")))
  15296. (while (< (setq l (1+ l)) l2)
  15297. (if itemp
  15298. (and (org-at-item-p) (replace-match rpl t t))
  15299. (unless (org-on-heading-p)
  15300. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15301. (replace-match (concat rpl (match-string 2))))))
  15302. (beginning-of-line 2)))))))
  15303. (defun org-meta-return (&optional arg)
  15304. "Insert a new heading or wrap a region in a table.
  15305. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  15306. See the individual commands for more information."
  15307. (interactive "P")
  15308. (cond
  15309. ((run-hook-with-args-until-success 'org-metareturn-hook))
  15310. ((org-at-table-p)
  15311. (call-interactively 'org-table-wrap-region))
  15312. (t (call-interactively 'org-insert-heading))))
  15313. ;;; Menu entries
  15314. ;; Define the Org-mode menus
  15315. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  15316. '("Tbl"
  15317. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  15318. ["Next Field" org-cycle (org-at-table-p)]
  15319. ["Previous Field" org-shifttab (org-at-table-p)]
  15320. ["Next Row" org-return (org-at-table-p)]
  15321. "--"
  15322. ["Blank Field" org-table-blank-field (org-at-table-p)]
  15323. ["Edit Field" org-table-edit-field (org-at-table-p)]
  15324. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  15325. "--"
  15326. ("Column"
  15327. ["Move Column Left" org-metaleft (org-at-table-p)]
  15328. ["Move Column Right" org-metaright (org-at-table-p)]
  15329. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  15330. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  15331. ("Row"
  15332. ["Move Row Up" org-metaup (org-at-table-p)]
  15333. ["Move Row Down" org-metadown (org-at-table-p)]
  15334. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  15335. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  15336. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  15337. "--"
  15338. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  15339. ("Rectangle"
  15340. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  15341. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  15342. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  15343. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  15344. "--"
  15345. ("Calculate"
  15346. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  15347. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  15348. ["Edit Formulas" org-edit-special (org-at-table-p)]
  15349. "--"
  15350. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  15351. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  15352. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  15353. "--"
  15354. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  15355. "--"
  15356. ["Sum Column/Rectangle" org-table-sum
  15357. (or (org-at-table-p) (org-region-active-p))]
  15358. ["Which Column?" org-table-current-column (org-at-table-p)])
  15359. ["Debug Formulas"
  15360. org-table-toggle-formula-debugger
  15361. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  15362. ["Show Col/Row Numbers"
  15363. org-table-toggle-coordinate-overlays
  15364. :style toggle
  15365. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  15366. "--"
  15367. ["Create" org-table-create (and (not (org-at-table-p))
  15368. org-enable-table-editor)]
  15369. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  15370. ["Import from File" org-table-import (not (org-at-table-p))]
  15371. ["Export to File" org-table-export (org-at-table-p)]
  15372. "--"
  15373. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  15374. (easy-menu-define org-org-menu org-mode-map "Org menu"
  15375. '("Org"
  15376. ("Show/Hide"
  15377. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  15378. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  15379. ["Sparse Tree..." org-sparse-tree t]
  15380. ["Reveal Context" org-reveal t]
  15381. ["Show All" show-all t]
  15382. "--"
  15383. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  15384. "--"
  15385. ["New Heading" org-insert-heading t]
  15386. ("Navigate Headings"
  15387. ["Up" outline-up-heading t]
  15388. ["Next" outline-next-visible-heading t]
  15389. ["Previous" outline-previous-visible-heading t]
  15390. ["Next Same Level" outline-forward-same-level t]
  15391. ["Previous Same Level" outline-backward-same-level t]
  15392. "--"
  15393. ["Jump" org-goto t])
  15394. ("Edit Structure"
  15395. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  15396. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  15397. "--"
  15398. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  15399. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  15400. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  15401. "--"
  15402. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  15403. "--"
  15404. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  15405. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  15406. ["Demote Heading" org-metaright (not (org-at-table-p))]
  15407. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  15408. "--"
  15409. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  15410. "--"
  15411. ["Convert to odd levels" org-convert-to-odd-levels t]
  15412. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  15413. ("Editing"
  15414. ["Emphasis..." org-emphasize t]
  15415. ["Edit Source Example" org-edit-special t]
  15416. "--"
  15417. ["Footnote new/jump" org-footnote-action t]
  15418. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  15419. ("Archive"
  15420. ["Archive (default method)" org-archive-subtree-default t]
  15421. "--"
  15422. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  15423. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  15424. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  15425. )
  15426. "--"
  15427. ("Hyperlinks"
  15428. ["Store Link (Global)" org-store-link t]
  15429. ["Find existing link to here" org-occur-link-in-agenda-files t]
  15430. ["Insert Link" org-insert-link t]
  15431. ["Follow Link" org-open-at-point t]
  15432. "--"
  15433. ["Next link" org-next-link t]
  15434. ["Previous link" org-previous-link t]
  15435. "--"
  15436. ["Descriptive Links"
  15437. (progn (add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  15438. :style radio
  15439. :selected (member '(org-link) buffer-invisibility-spec)]
  15440. ["Literal Links"
  15441. (progn
  15442. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  15443. :style radio
  15444. :selected (not (member '(org-link) buffer-invisibility-spec))])
  15445. "--"
  15446. ("TODO Lists"
  15447. ["TODO/DONE/-" org-todo t]
  15448. ("Select keyword"
  15449. ["Next keyword" org-shiftright (org-on-heading-p)]
  15450. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  15451. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  15452. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  15453. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  15454. ["Show TODO Tree" org-show-todo-tree t]
  15455. ["Global TODO list" org-todo-list t]
  15456. "--"
  15457. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  15458. :selected org-enforce-todo-dependencies :style toggle :active t]
  15459. "Settings for tree at point"
  15460. ["Do Children sequentially" org-toggle-ordered-property :style radio
  15461. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  15462. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  15463. ["Do Children parallel" org-toggle-ordered-property :style radio
  15464. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  15465. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  15466. "--"
  15467. ["Set Priority" org-priority t]
  15468. ["Priority Up" org-shiftup t]
  15469. ["Priority Down" org-shiftdown t]
  15470. "--"
  15471. ["Get news from all feeds" org-feed-update-all t]
  15472. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  15473. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  15474. ("TAGS and Properties"
  15475. ["Set Tags" org-set-tags-command t]
  15476. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  15477. "--"
  15478. ["Set property" org-set-property t]
  15479. ["Column view of properties" org-columns t]
  15480. ["Insert Column View DBlock" org-insert-columns-dblock t])
  15481. ("Dates and Scheduling"
  15482. ["Timestamp" org-time-stamp t]
  15483. ["Timestamp (inactive)" org-time-stamp-inactive t]
  15484. ("Change Date"
  15485. ["1 Day Later" org-shiftright t]
  15486. ["1 Day Earlier" org-shiftleft t]
  15487. ["1 ... Later" org-shiftup t]
  15488. ["1 ... Earlier" org-shiftdown t])
  15489. ["Compute Time Range" org-evaluate-time-range t]
  15490. ["Schedule Item" org-schedule t]
  15491. ["Deadline" org-deadline t]
  15492. "--"
  15493. ["Custom time format" org-toggle-time-stamp-overlays
  15494. :style radio :selected org-display-custom-times]
  15495. "--"
  15496. ["Goto Calendar" org-goto-calendar t]
  15497. ["Date from Calendar" org-date-from-calendar t]
  15498. "--"
  15499. ["Start/Restart Timer" org-timer-start t]
  15500. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  15501. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  15502. ["Insert Timer String" org-timer t]
  15503. ["Insert Timer Item" org-timer-item t])
  15504. ("Logging work"
  15505. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  15506. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  15507. ["Clock out" org-clock-out t]
  15508. ["Clock cancel" org-clock-cancel t]
  15509. "--"
  15510. ["Mark as default task" org-clock-mark-default-task t]
  15511. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  15512. ["Goto running clock" org-clock-goto t]
  15513. "--"
  15514. ["Display times" org-clock-display t]
  15515. ["Create clock table" org-clock-report t]
  15516. "--"
  15517. ["Record DONE time"
  15518. (progn (setq org-log-done (not org-log-done))
  15519. (message "Switching to %s will %s record a timestamp"
  15520. (car org-done-keywords)
  15521. (if org-log-done "automatically" "not")))
  15522. :style toggle :selected org-log-done])
  15523. "--"
  15524. ["Agenda Command..." org-agenda t]
  15525. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  15526. ("File List for Agenda")
  15527. ("Special views current file"
  15528. ["TODO Tree" org-show-todo-tree t]
  15529. ["Check Deadlines" org-check-deadlines t]
  15530. ["Timeline" org-timeline t]
  15531. ["Tags/Property tree" org-match-sparse-tree t])
  15532. "--"
  15533. ["Export/Publish..." org-export t]
  15534. ("LaTeX"
  15535. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  15536. :selected org-cdlatex-mode]
  15537. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  15538. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  15539. ["Modify math symbol" org-cdlatex-math-modify
  15540. (org-inside-LaTeX-fragment-p)]
  15541. ["Insert citation" org-reftex-citation t]
  15542. "--"
  15543. ["Export LaTeX fragments as images"
  15544. (if (featurep 'org-exp)
  15545. (setq org-export-with-LaTeX-fragments
  15546. (not org-export-with-LaTeX-fragments))
  15547. (require 'org-exp))
  15548. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  15549. org-export-with-LaTeX-fragments)]
  15550. "--"
  15551. ["Template for BEAMER" org-beamer-settings-template t])
  15552. "--"
  15553. ("MobileOrg"
  15554. ["Push Files and Views" org-mobile-push t]
  15555. ["Get Captured and Flagged" org-mobile-pull t]
  15556. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  15557. "--"
  15558. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  15559. "--"
  15560. ("Documentation"
  15561. ["Show Version" org-version t]
  15562. ["Info Documentation" org-info t])
  15563. ("Customize"
  15564. ["Browse Org Group" org-customize t]
  15565. "--"
  15566. ["Expand This Menu" org-create-customize-menu
  15567. (fboundp 'customize-menu-create)])
  15568. ["Send bug report" org-submit-bug-report t]
  15569. "--"
  15570. ("Refresh/Reload"
  15571. ["Refresh setup current buffer" org-mode-restart t]
  15572. ["Reload Org (after update)" org-reload t]
  15573. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  15574. ))
  15575. (defun org-info (&optional node)
  15576. "Read documentation for Org-mode in the info system.
  15577. With optional NODE, go directly to that node."
  15578. (interactive)
  15579. (info (format "(org)%s" (or node ""))))
  15580. ;;;###autoload
  15581. (defun org-submit-bug-report ()
  15582. "Submit a bug report on Org-mode via mail.
  15583. Don't hesitate to report any problems or inaccurate documentation.
  15584. If you don't have setup sending mail from (X)Emacs, please copy the
  15585. output buffer into your mail program, as it gives us important
  15586. information about your Org-mode version and configuration."
  15587. (interactive)
  15588. (require 'reporter)
  15589. (org-load-modules-maybe)
  15590. (org-require-autoloaded-modules)
  15591. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  15592. (reporter-submit-bug-report
  15593. "emacs-orgmode@gnu.org"
  15594. (org-version)
  15595. (let (list)
  15596. (save-window-excursion
  15597. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  15598. (delete-other-windows)
  15599. (erase-buffer)
  15600. (insert "You are about to submit a bug report to the Org-mode mailing list.
  15601. We would like to add your full Org-mode and Outline configuration to the
  15602. bug report. This greatly simplifies the work of the maintainer and
  15603. other experts on the mailing list.
  15604. HOWEVER, some variables you have customized may contain private
  15605. information. The names of customers, colleagues, or friends, might
  15606. appear in the form of file names, tags, todo states, or search strings.
  15607. If you answer yes to the prompt, you might want to check and remove
  15608. such private information before sending the email.")
  15609. (add-text-properties (point-min) (point-max) '(face org-warning))
  15610. (when (yes-or-no-p "Include your Org-mode configuration ")
  15611. (mapatoms
  15612. (lambda (v)
  15613. (and (boundp v)
  15614. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  15615. (or (and (symbol-value v)
  15616. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  15617. (and
  15618. (get v 'custom-type) (get v 'standard-value)
  15619. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  15620. (push v list)))))
  15621. (kill-buffer (get-buffer "*Warn about privacy*"))
  15622. list))
  15623. nil nil
  15624. "Remember to cover the basics, that is, what you expected to happen and
  15625. what in fact did happen. You don't know how to make a good report? See
  15626. http://orgmode.org/manual/Feedback.html#Feedback
  15627. Your bug report will be posted to the Org-mode mailing list.
  15628. ------------------------------------------------------------------------")
  15629. (save-excursion
  15630. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  15631. (replace-match "\\1Bug: \\3 [\\2]")))))
  15632. (defun org-install-agenda-files-menu ()
  15633. (let ((bl (buffer-list)))
  15634. (save-excursion
  15635. (while bl
  15636. (set-buffer (pop bl))
  15637. (if (org-mode-p) (setq bl nil)))
  15638. (when (org-mode-p)
  15639. (easy-menu-change
  15640. '("Org") "File List for Agenda"
  15641. (append
  15642. (list
  15643. ["Edit File List" (org-edit-agenda-file-list) t]
  15644. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  15645. ["Remove Current File from List" org-remove-file t]
  15646. ["Cycle through agenda files" org-cycle-agenda-files t]
  15647. ["Occur in all agenda files" org-occur-in-agenda-files t]
  15648. "--")
  15649. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  15650. ;;;; Documentation
  15651. ;;;###autoload
  15652. (defun org-require-autoloaded-modules ()
  15653. (interactive)
  15654. (mapc 'require
  15655. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  15656. org-docbook org-exp org-html org-icalendar
  15657. org-id org-latex
  15658. org-publish org-remember org-table
  15659. org-timer org-xoxo)))
  15660. ;;;###autoload
  15661. (defun org-reload (&optional uncompiled)
  15662. "Reload all org lisp files.
  15663. With prefix arg UNCOMPILED, load the uncompiled versions."
  15664. (interactive "P")
  15665. (require 'find-func)
  15666. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  15667. (dir-org (file-name-directory (org-find-library-name "org")))
  15668. (dir-org-contrib (ignore-errors
  15669. (file-name-directory
  15670. (org-find-library-name "org-contribdir"))))
  15671. (files
  15672. (append (directory-files dir-org t file-re)
  15673. (and dir-org-contrib
  15674. (directory-files dir-org-contrib t file-re))))
  15675. (remove-re (concat (if (featurep 'xemacs)
  15676. "org-colview" "org-colview-xemacs")
  15677. "\\'")))
  15678. (setq files (mapcar 'file-name-sans-extension files))
  15679. (setq files (mapcar
  15680. (lambda (x) (if (string-match remove-re x) nil x))
  15681. files))
  15682. (setq files (delq nil files))
  15683. (mapc
  15684. (lambda (f)
  15685. (when (featurep (intern (file-name-nondirectory f)))
  15686. (if (and (not uncompiled)
  15687. (file-exists-p (concat f ".elc")))
  15688. (load (concat f ".elc") nil nil t)
  15689. (load (concat f ".el") nil nil t))))
  15690. files))
  15691. (org-version))
  15692. ;;;###autoload
  15693. (defun org-customize ()
  15694. "Call the customize function with org as argument."
  15695. (interactive)
  15696. (org-load-modules-maybe)
  15697. (org-require-autoloaded-modules)
  15698. (customize-browse 'org))
  15699. (defun org-create-customize-menu ()
  15700. "Create a full customization menu for Org-mode, insert it into the menu."
  15701. (interactive)
  15702. (org-load-modules-maybe)
  15703. (org-require-autoloaded-modules)
  15704. (if (fboundp 'customize-menu-create)
  15705. (progn
  15706. (easy-menu-change
  15707. '("Org") "Customize"
  15708. `(["Browse Org group" org-customize t]
  15709. "--"
  15710. ,(customize-menu-create 'org)
  15711. ["Set" Custom-set t]
  15712. ["Save" Custom-save t]
  15713. ["Reset to Current" Custom-reset-current t]
  15714. ["Reset to Saved" Custom-reset-saved t]
  15715. ["Reset to Standard Settings" Custom-reset-standard t]))
  15716. (message "\"Org\"-menu now contains full customization menu"))
  15717. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  15718. ;;;; Miscellaneous stuff
  15719. ;;; Generally useful functions
  15720. (defun org-get-at-bol (property)
  15721. "Get text property PROPERTY at beginning of line."
  15722. (get-text-property (point-at-bol) property))
  15723. (defun org-find-text-property-in-string (prop s)
  15724. "Return the first non-nil value of property PROP in string S."
  15725. (or (get-text-property 0 prop s)
  15726. (get-text-property (or (next-single-property-change 0 prop s) 0)
  15727. prop s)))
  15728. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  15729. "Display the given MESSAGE as a warning."
  15730. (if (fboundp 'display-warning)
  15731. (display-warning 'org message
  15732. (if (featurep 'xemacs) 'warning :warning))
  15733. (let ((buf (get-buffer-create "*Org warnings*")))
  15734. (with-current-buffer buf
  15735. (goto-char (point-max))
  15736. (insert "Warning (Org): " message)
  15737. (unless (bolp)
  15738. (newline)))
  15739. (display-buffer buf)
  15740. (sit-for 0))))
  15741. (defun org-in-commented-line ()
  15742. "Is point in a line starting with `#'?"
  15743. (equal (char-after (point-at-bol)) ?#))
  15744. (defun org-in-verbatim-emphasis ()
  15745. (save-match-data
  15746. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  15747. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  15748. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  15749. (if (and marker (marker-buffer marker)
  15750. (buffer-live-p (marker-buffer marker)))
  15751. (progn
  15752. (switch-to-buffer (marker-buffer marker))
  15753. (if (or (> marker (point-max)) (< marker (point-min)))
  15754. (widen))
  15755. (goto-char marker)
  15756. (org-show-context 'org-goto))
  15757. (if bookmark
  15758. (bookmark-jump bookmark)
  15759. (error "Cannot find location"))))
  15760. (defun org-quote-csv-field (s)
  15761. "Quote field for inclusion in CSV material."
  15762. (if (string-match "[\",]" s)
  15763. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  15764. s))
  15765. (defun org-plist-delete (plist property)
  15766. "Delete PROPERTY from PLIST.
  15767. This is in contrast to merely setting it to 0."
  15768. (let (p)
  15769. (while plist
  15770. (if (not (eq property (car plist)))
  15771. (setq p (plist-put p (car plist) (nth 1 plist))))
  15772. (setq plist (cddr plist)))
  15773. p))
  15774. (defun org-force-self-insert (N)
  15775. "Needed to enforce self-insert under remapping."
  15776. (interactive "p")
  15777. (self-insert-command N))
  15778. (defun org-string-width (s)
  15779. "Compute width of string, ignoring invisible characters.
  15780. This ignores character with invisibility property `org-link', and also
  15781. characters with property `org-cwidth', because these will become invisible
  15782. upon the next fontification round."
  15783. (let (b l)
  15784. (when (or (eq t buffer-invisibility-spec)
  15785. (assq 'org-link buffer-invisibility-spec))
  15786. (while (setq b (text-property-any 0 (length s)
  15787. 'invisible 'org-link s))
  15788. (setq s (concat (substring s 0 b)
  15789. (substring s (or (next-single-property-change
  15790. b 'invisible s) (length s)))))))
  15791. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  15792. (setq s (concat (substring s 0 b)
  15793. (substring s (or (next-single-property-change
  15794. b 'org-cwidth s) (length s))))))
  15795. (setq l (string-width s) b -1)
  15796. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  15797. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  15798. l))
  15799. (defun org-get-indentation (&optional line)
  15800. "Get the indentation of the current line, interpreting tabs.
  15801. When LINE is given, assume it represents a line and compute its indentation."
  15802. (if line
  15803. (if (string-match "^ *" (org-remove-tabs line))
  15804. (match-end 0))
  15805. (save-excursion
  15806. (beginning-of-line 1)
  15807. (skip-chars-forward " \t")
  15808. (current-column))))
  15809. (defun org-remove-tabs (s &optional width)
  15810. "Replace tabulators in S with spaces.
  15811. Assumes that s is a single line, starting in column 0."
  15812. (setq width (or width tab-width))
  15813. (while (string-match "\t" s)
  15814. (setq s (replace-match
  15815. (make-string
  15816. (- (* width (/ (+ (match-beginning 0) width) width))
  15817. (match-beginning 0)) ?\ )
  15818. t t s)))
  15819. s)
  15820. (defun org-fix-indentation (line ind)
  15821. "Fix indentation in LINE.
  15822. IND is a cons cell with target and minimum indentation.
  15823. If the current indentation in LINE is smaller than the minimum,
  15824. leave it alone. If it is larger than ind, set it to the target."
  15825. (let* ((l (org-remove-tabs line))
  15826. (i (org-get-indentation l))
  15827. (i1 (car ind)) (i2 (cdr ind)))
  15828. (if (>= i i2) (setq l (substring line i2)))
  15829. (if (> i1 0)
  15830. (concat (make-string i1 ?\ ) l)
  15831. l)))
  15832. (defun org-remove-indentation (code &optional n)
  15833. "Remove the maximum common indentation from the lines in CODE.
  15834. N may optionally be the number of spaces to remove."
  15835. (with-temp-buffer
  15836. (insert code)
  15837. (org-do-remove-indentation n)
  15838. (buffer-string)))
  15839. (defun org-do-remove-indentation (&optional n)
  15840. "Remove the maximum common indentation from the buffer."
  15841. (untabify (point-min) (point-max))
  15842. (let ((min 10000) re)
  15843. (if n
  15844. (setq min n)
  15845. (goto-char (point-min))
  15846. (while (re-search-forward "^ *[^ \n]" nil t)
  15847. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  15848. (unless (or (= min 0) (= min 10000))
  15849. (setq re (format "^ \\{%d\\}" min))
  15850. (goto-char (point-min))
  15851. (while (re-search-forward re nil t)
  15852. (replace-match "")
  15853. (end-of-line 1))
  15854. min)))
  15855. (defun org-fill-template (template alist)
  15856. "Find each %key of ALIST in TEMPLATE and replace it."
  15857. (let ((case-fold-search nil)
  15858. entry key value)
  15859. (setq alist (sort (copy-sequence alist)
  15860. (lambda (a b) (< (length (car a)) (length (car b))))))
  15861. (while (setq entry (pop alist))
  15862. (setq template
  15863. (replace-regexp-in-string
  15864. (concat "%" (regexp-quote (car entry)))
  15865. (cdr entry) template t t)))
  15866. template))
  15867. (defun org-base-buffer (buffer)
  15868. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  15869. (if (not buffer)
  15870. buffer
  15871. (or (buffer-base-buffer buffer)
  15872. buffer)))
  15873. (defun org-trim (s)
  15874. "Remove whitespace at beginning and end of string."
  15875. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  15876. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  15877. s)
  15878. (defun org-wrap (string &optional width lines)
  15879. "Wrap string to either a number of lines, or a width in characters.
  15880. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  15881. that costs. If there is a word longer than WIDTH, the text is actually
  15882. wrapped to the length of that word.
  15883. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  15884. many lines, whatever width that takes.
  15885. The return value is a list of lines, without newlines at the end."
  15886. (let* ((words (org-split-string string "[ \t\n]+"))
  15887. (maxword (apply 'max (mapcar 'org-string-width words)))
  15888. w ll)
  15889. (cond (width
  15890. (org-do-wrap words (max maxword width)))
  15891. (lines
  15892. (setq w maxword)
  15893. (setq ll (org-do-wrap words maxword))
  15894. (if (<= (length ll) lines)
  15895. ll
  15896. (setq ll words)
  15897. (while (> (length ll) lines)
  15898. (setq w (1+ w))
  15899. (setq ll (org-do-wrap words w)))
  15900. ll))
  15901. (t (error "Cannot wrap this")))))
  15902. (defun org-do-wrap (words width)
  15903. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  15904. (let (lines line)
  15905. (while words
  15906. (setq line (pop words))
  15907. (while (and words (< (+ (length line) (length (car words))) width))
  15908. (setq line (concat line " " (pop words))))
  15909. (setq lines (push line lines)))
  15910. (nreverse lines)))
  15911. (defun org-split-string (string &optional separators)
  15912. "Splits STRING into substrings at SEPARATORS.
  15913. No empty strings are returned if there are matches at the beginning
  15914. and end of string."
  15915. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  15916. (start 0)
  15917. notfirst
  15918. (list nil))
  15919. (while (and (string-match rexp string
  15920. (if (and notfirst
  15921. (= start (match-beginning 0))
  15922. (< start (length string)))
  15923. (1+ start) start))
  15924. (< (match-beginning 0) (length string)))
  15925. (setq notfirst t)
  15926. (or (eq (match-beginning 0) 0)
  15927. (and (eq (match-beginning 0) (match-end 0))
  15928. (eq (match-beginning 0) start))
  15929. (setq list
  15930. (cons (substring string start (match-beginning 0))
  15931. list)))
  15932. (setq start (match-end 0)))
  15933. (or (eq start (length string))
  15934. (setq list
  15935. (cons (substring string start)
  15936. list)))
  15937. (nreverse list)))
  15938. (defun org-quote-vert (s)
  15939. "Replace \"|\" with \"\\vert\"."
  15940. (while (string-match "|" s)
  15941. (setq s (replace-match "\\vert" t t s)))
  15942. s)
  15943. (defun org-uuidgen-p (s)
  15944. "Is S an ID created by UUIDGEN?"
  15945. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  15946. (defun org-context ()
  15947. "Return a list of contexts of the current cursor position.
  15948. If several contexts apply, all are returned.
  15949. Each context entry is a list with a symbol naming the context, and
  15950. two positions indicating start and end of the context. Possible
  15951. contexts are:
  15952. :headline anywhere in a headline
  15953. :headline-stars on the leading stars in a headline
  15954. :todo-keyword on a TODO keyword (including DONE) in a headline
  15955. :tags on the TAGS in a headline
  15956. :priority on the priority cookie in a headline
  15957. :item on the first line of a plain list item
  15958. :item-bullet on the bullet/number of a plain list item
  15959. :checkbox on the checkbox in a plain list item
  15960. :table in an org-mode table
  15961. :table-special on a special filed in a table
  15962. :table-table in a table.el table
  15963. :link on a hyperlink
  15964. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  15965. :target on a <<target>>
  15966. :radio-target on a <<<radio-target>>>
  15967. :latex-fragment on a LaTeX fragment
  15968. :latex-preview on a LaTeX fragment with overlayed preview image
  15969. This function expects the position to be visible because it uses font-lock
  15970. faces as a help to recognize the following contexts: :table-special, :link,
  15971. and :keyword."
  15972. (let* ((f (get-text-property (point) 'face))
  15973. (faces (if (listp f) f (list f)))
  15974. (p (point)) clist o)
  15975. ;; First the large context
  15976. (cond
  15977. ((org-on-heading-p t)
  15978. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  15979. (when (progn
  15980. (beginning-of-line 1)
  15981. (looking-at org-todo-line-tags-regexp))
  15982. (push (org-point-in-group p 1 :headline-stars) clist)
  15983. (push (org-point-in-group p 2 :todo-keyword) clist)
  15984. (push (org-point-in-group p 4 :tags) clist))
  15985. (goto-char p)
  15986. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  15987. (if (looking-at "\\[#[A-Z0-9]\\]")
  15988. (push (org-point-in-group p 0 :priority) clist)))
  15989. ((org-at-item-p)
  15990. (push (org-point-in-group p 2 :item-bullet) clist)
  15991. (push (list :item (point-at-bol)
  15992. (save-excursion (org-end-of-item) (point)))
  15993. clist)
  15994. (and (org-at-item-checkbox-p)
  15995. (push (org-point-in-group p 0 :checkbox) clist)))
  15996. ((org-at-table-p)
  15997. (push (list :table (org-table-begin) (org-table-end)) clist)
  15998. (if (memq 'org-formula faces)
  15999. (push (list :table-special
  16000. (previous-single-property-change p 'face)
  16001. (next-single-property-change p 'face)) clist)))
  16002. ((org-at-table-p 'any)
  16003. (push (list :table-table) clist)))
  16004. (goto-char p)
  16005. ;; Now the small context
  16006. (cond
  16007. ((org-at-timestamp-p)
  16008. (push (org-point-in-group p 0 :timestamp) clist))
  16009. ((memq 'org-link faces)
  16010. (push (list :link
  16011. (previous-single-property-change p 'face)
  16012. (next-single-property-change p 'face)) clist))
  16013. ((memq 'org-special-keyword faces)
  16014. (push (list :keyword
  16015. (previous-single-property-change p 'face)
  16016. (next-single-property-change p 'face)) clist))
  16017. ((org-on-target-p)
  16018. (push (org-point-in-group p 0 :target) clist)
  16019. (goto-char (1- (match-beginning 0)))
  16020. (if (looking-at org-radio-target-regexp)
  16021. (push (org-point-in-group p 0 :radio-target) clist))
  16022. (goto-char p))
  16023. ((setq o (car (delq nil
  16024. (mapcar
  16025. (lambda (x)
  16026. (if (memq x org-latex-fragment-image-overlays) x))
  16027. (overlays-at (point))))))
  16028. (push (list :latex-fragment
  16029. (overlay-start o) (overlay-end o)) clist)
  16030. (push (list :latex-preview
  16031. (overlay-start o) (overlay-end o)) clist))
  16032. ((org-inside-LaTeX-fragment-p)
  16033. ;; FIXME: positions wrong.
  16034. (push (list :latex-fragment (point) (point)) clist)))
  16035. (setq clist (nreverse (delq nil clist)))
  16036. clist))
  16037. ;; FIXME: Compare with at-regexp-p Do we need both?
  16038. (defun org-in-regexp (re &optional nlines visually)
  16039. "Check if point is inside a match of regexp.
  16040. Normally only the current line is checked, but you can include NLINES extra
  16041. lines both before and after point into the search.
  16042. If VISUALLY is set, require that the cursor is not after the match but
  16043. really on, so that the block visually is on the match."
  16044. (catch 'exit
  16045. (let ((pos (point))
  16046. (eol (point-at-eol (+ 1 (or nlines 0))))
  16047. (inc (if visually 1 0)))
  16048. (save-excursion
  16049. (beginning-of-line (- 1 (or nlines 0)))
  16050. (while (re-search-forward re eol t)
  16051. (if (and (<= (match-beginning 0) pos)
  16052. (>= (+ inc (match-end 0)) pos))
  16053. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  16054. (defun org-at-regexp-p (regexp)
  16055. "Is point inside a match of REGEXP in the current line?"
  16056. (catch 'exit
  16057. (save-excursion
  16058. (let ((pos (point)) (end (point-at-eol)))
  16059. (beginning-of-line 1)
  16060. (while (re-search-forward regexp end t)
  16061. (if (and (<= (match-beginning 0) pos)
  16062. (>= (match-end 0) pos))
  16063. (throw 'exit t)))
  16064. nil))))
  16065. (defun org-in-regexps-block-p (start-re end-re)
  16066. "Returns t if the current point is between matches of START-RE and END-RE.
  16067. This will also return to if point is on one of the two matches."
  16068. (interactive)
  16069. (let ((p (point)))
  16070. (save-excursion
  16071. (and (or (org-at-regexp-p start-re)
  16072. (re-search-backward start-re nil t))
  16073. (re-search-forward end-re nil t)
  16074. (>= (point) p)))))
  16075. (defun org-occur-in-agenda-files (regexp &optional nlines)
  16076. "Call `multi-occur' with buffers for all agenda files."
  16077. (interactive "sOrg-files matching: \np")
  16078. (let* ((files (org-agenda-files))
  16079. (tnames (mapcar 'file-truename files))
  16080. (extra org-agenda-text-search-extra-files)
  16081. f)
  16082. (when (eq (car extra) 'agenda-archives)
  16083. (setq extra (cdr extra))
  16084. (setq files (org-add-archive-files files)))
  16085. (while (setq f (pop extra))
  16086. (unless (member (file-truename f) tnames)
  16087. (add-to-list 'files f 'append)
  16088. (add-to-list 'tnames (file-truename f) 'append)))
  16089. (multi-occur
  16090. (mapcar (lambda (x)
  16091. (with-current-buffer
  16092. (or (get-file-buffer x) (find-file-noselect x))
  16093. (widen)
  16094. (current-buffer)))
  16095. files)
  16096. regexp)))
  16097. (if (boundp 'occur-mode-find-occurrence-hook)
  16098. ;; Emacs 23
  16099. (add-hook 'occur-mode-find-occurrence-hook
  16100. (lambda ()
  16101. (when (org-mode-p)
  16102. (org-reveal))))
  16103. ;; Emacs 22
  16104. (defadvice occur-mode-goto-occurrence
  16105. (after org-occur-reveal activate)
  16106. (and (org-mode-p) (org-reveal)))
  16107. (defadvice occur-mode-goto-occurrence-other-window
  16108. (after org-occur-reveal activate)
  16109. (and (org-mode-p) (org-reveal)))
  16110. (defadvice occur-mode-display-occurrence
  16111. (after org-occur-reveal activate)
  16112. (when (org-mode-p)
  16113. (let ((pos (occur-mode-find-occurrence)))
  16114. (with-current-buffer (marker-buffer pos)
  16115. (save-excursion
  16116. (goto-char pos)
  16117. (org-reveal)))))))
  16118. (defun org-occur-link-in-agenda-files ()
  16119. "Create a link and search for it in the agendas.
  16120. The link is not stored in `org-stored-links', it is just created
  16121. for the search purpose."
  16122. (interactive)
  16123. (let ((link (condition-case nil
  16124. (org-store-link nil)
  16125. (error "Unable to create a link to here"))))
  16126. (org-occur-in-agenda-files (regexp-quote link))))
  16127. (defun org-uniquify (list)
  16128. "Remove duplicate elements from LIST."
  16129. (let (res)
  16130. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  16131. res))
  16132. (defun org-delete-all (elts list)
  16133. "Remove all elements in ELTS from LIST."
  16134. (while elts
  16135. (setq list (delete (pop elts) list)))
  16136. list)
  16137. (defun org-remove-if (predicate seq)
  16138. "Remove everything from SEQ that fulfills PREDICATE."
  16139. (let (res e)
  16140. (while seq
  16141. (setq e (pop seq))
  16142. (if (not (funcall predicate e)) (push e res)))
  16143. (nreverse res)))
  16144. (defun org-remove-if-not (predicate seq)
  16145. "Remove everything from SEQ that does not fulfill PREDICATE."
  16146. (let (res e)
  16147. (while seq
  16148. (setq e (pop seq))
  16149. (if (funcall predicate e) (push e res)))
  16150. (nreverse res)))
  16151. (defun org-back-over-empty-lines ()
  16152. "Move backwards over whitespace, to the beginning of the first empty line.
  16153. Returns the number of empty lines passed."
  16154. (let ((pos (point)))
  16155. (skip-chars-backward " \t\n\r")
  16156. (beginning-of-line 2)
  16157. (goto-char (min (point) pos))
  16158. (count-lines (point) pos)))
  16159. (defun org-skip-whitespace ()
  16160. (skip-chars-forward " \t\n\r"))
  16161. (defun org-point-in-group (point group &optional context)
  16162. "Check if POINT is in match-group GROUP.
  16163. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  16164. match. If the match group does ot exist or point is not inside it,
  16165. return nil."
  16166. (and (match-beginning group)
  16167. (>= point (match-beginning group))
  16168. (<= point (match-end group))
  16169. (if context
  16170. (list context (match-beginning group) (match-end group))
  16171. t)))
  16172. (defun org-switch-to-buffer-other-window (&rest args)
  16173. "Switch to buffer in a second window on the current frame.
  16174. In particular, do not allow pop-up frames."
  16175. (let (pop-up-frames special-display-buffer-names special-display-regexps
  16176. special-display-function)
  16177. (apply 'switch-to-buffer-other-window args)))
  16178. (defun org-combine-plists (&rest plists)
  16179. "Create a single property list from all plists in PLISTS.
  16180. The process starts by copying the first list, and then setting properties
  16181. from the other lists. Settings in the last list are the most significant
  16182. ones and overrule settings in the other lists."
  16183. (let ((rtn (copy-sequence (pop plists)))
  16184. p v ls)
  16185. (while plists
  16186. (setq ls (pop plists))
  16187. (while ls
  16188. (setq p (pop ls) v (pop ls))
  16189. (setq rtn (plist-put rtn p v))))
  16190. rtn))
  16191. (defun org-move-line-down (arg)
  16192. "Move the current line down. With prefix argument, move it past ARG lines."
  16193. (interactive "p")
  16194. (let ((col (current-column))
  16195. beg end pos)
  16196. (beginning-of-line 1) (setq beg (point))
  16197. (beginning-of-line 2) (setq end (point))
  16198. (beginning-of-line (+ 1 arg))
  16199. (setq pos (move-marker (make-marker) (point)))
  16200. (insert (delete-and-extract-region beg end))
  16201. (goto-char pos)
  16202. (org-move-to-column col)))
  16203. (defun org-move-line-up (arg)
  16204. "Move the current line up. With prefix argument, move it past ARG lines."
  16205. (interactive "p")
  16206. (let ((col (current-column))
  16207. beg end pos)
  16208. (beginning-of-line 1) (setq beg (point))
  16209. (beginning-of-line 2) (setq end (point))
  16210. (beginning-of-line (- arg))
  16211. (setq pos (move-marker (make-marker) (point)))
  16212. (insert (delete-and-extract-region beg end))
  16213. (goto-char pos)
  16214. (org-move-to-column col)))
  16215. (defun org-replace-escapes (string table)
  16216. "Replace %-escapes in STRING with values in TABLE.
  16217. TABLE is an association list with keys like \"%a\" and string values.
  16218. The sequences in STRING may contain normal field width and padding information,
  16219. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  16220. so values can contain further %-escapes if they are define later in TABLE."
  16221. (let ((tbl (copy-alist table))
  16222. (case-fold-search nil)
  16223. (pchg 0)
  16224. e re rpl)
  16225. (while (setq e (pop tbl))
  16226. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  16227. (when (string-match re (cdr e))
  16228. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  16229. (safe "SREF"))
  16230. (add-text-properties 0 3 (list 'sref sref) safe)
  16231. (setcdr e (replace-match safe t t (cdr e)))))
  16232. (while (string-match re string)
  16233. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  16234. (cdr e)))
  16235. (setq string (replace-match rpl t t string))))
  16236. (while (setq pchg (next-property-change pchg string))
  16237. (let ((sref (get-text-property pchg 'sref string)))
  16238. (when (and sref (string-match "SREF" string pchg))
  16239. (setq string (replace-match sref t t string)))))
  16240. string))
  16241. (defun org-sublist (list start end)
  16242. "Return a section of LIST, from START to END.
  16243. Counting starts at 1."
  16244. (let (rtn (c start))
  16245. (setq list (nthcdr (1- start) list))
  16246. (while (and list (<= c end))
  16247. (push (pop list) rtn)
  16248. (setq c (1+ c)))
  16249. (nreverse rtn)))
  16250. (defun org-find-base-buffer-visiting (file)
  16251. "Like `find-buffer-visiting' but always return the base buffer and
  16252. not an indirect buffer."
  16253. (let ((buf (or (get-file-buffer file)
  16254. (find-buffer-visiting file))))
  16255. (if buf
  16256. (or (buffer-base-buffer buf) buf)
  16257. nil)))
  16258. (defun org-image-file-name-regexp (&optional extensions)
  16259. "Return regexp matching the file names of images.
  16260. If EXTENSIONS is given, only match these."
  16261. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  16262. (image-file-name-regexp)
  16263. (let ((image-file-name-extensions
  16264. (or extensions
  16265. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  16266. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  16267. (concat "\\."
  16268. (regexp-opt (nconc (mapcar 'upcase
  16269. image-file-name-extensions)
  16270. image-file-name-extensions)
  16271. t)
  16272. "\\'"))))
  16273. (defun org-file-image-p (file &optional extensions)
  16274. "Return non-nil if FILE is an image."
  16275. (save-match-data
  16276. (string-match (org-image-file-name-regexp extensions) file)))
  16277. (defun org-get-cursor-date ()
  16278. "Return the date at cursor in as a time.
  16279. This works in the calendar and in the agenda, anywhere else it just
  16280. returns the current time."
  16281. (let (date day defd)
  16282. (cond
  16283. ((eq major-mode 'calendar-mode)
  16284. (setq date (calendar-cursor-to-date)
  16285. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16286. ((eq major-mode 'org-agenda-mode)
  16287. (setq day (get-text-property (point) 'day))
  16288. (if day
  16289. (setq date (calendar-gregorian-from-absolute day)
  16290. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  16291. (nth 2 date))))))
  16292. (or defd (current-time))))
  16293. (defvar org-agenda-action-marker (make-marker)
  16294. "Marker pointing to the entry for the next agenda action.")
  16295. (defun org-mark-entry-for-agenda-action ()
  16296. "Mark the current entry as target of an agenda action.
  16297. Agenda actions are actions executed from the agenda with the key `k',
  16298. which make use of the date at the cursor."
  16299. (interactive)
  16300. (move-marker org-agenda-action-marker
  16301. (save-excursion (org-back-to-heading t) (point))
  16302. (current-buffer))
  16303. (message
  16304. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  16305. ;;; Paragraph filling stuff.
  16306. ;; We want this to be just right, so use the full arsenal.
  16307. (defun org-indent-line-function ()
  16308. "Indent line like previous, but further if previous was headline or item."
  16309. (interactive)
  16310. (let* ((pos (point))
  16311. (itemp (org-at-item-p))
  16312. (case-fold-search t)
  16313. (org-drawer-regexp (or org-drawer-regexp "\000"))
  16314. column bpos bcol tpos tcol bullet btype bullet-type)
  16315. ;; Find the previous relevant line
  16316. (beginning-of-line 1)
  16317. (cond
  16318. ((looking-at "#") (setq column 0))
  16319. ((looking-at "\\*+ ") (setq column 0))
  16320. ((and (looking-at "[ \t]*:END:")
  16321. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  16322. (save-excursion
  16323. (goto-char (1- (match-beginning 1)))
  16324. (setq column (current-column))))
  16325. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  16326. (save-excursion
  16327. (re-search-backward
  16328. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  16329. (setq column (org-get-indentation (match-string 0))))
  16330. (t
  16331. (beginning-of-line 0)
  16332. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  16333. (not (looking-at "[ \t]*:END:"))
  16334. (not (looking-at org-drawer-regexp)))
  16335. (beginning-of-line 0))
  16336. (cond
  16337. ((looking-at "\\*+[ \t]+")
  16338. (if (not org-adapt-indentation)
  16339. (setq column 0)
  16340. (goto-char (match-end 0))
  16341. (setq column (current-column))))
  16342. ((looking-at org-drawer-regexp)
  16343. (goto-char (1- (match-beginning 1)))
  16344. (setq column (current-column)))
  16345. ((looking-at "\\([ \t]*\\):END:")
  16346. (goto-char (match-end 1))
  16347. (setq column (current-column)))
  16348. ((org-in-item-p)
  16349. (org-beginning-of-item)
  16350. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  16351. (setq bpos (match-beginning 1) tpos (match-end 0)
  16352. bcol (progn (goto-char bpos) (current-column))
  16353. tcol (progn (goto-char tpos) (current-column))
  16354. bullet (match-string 1)
  16355. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  16356. (if (> tcol (+ bcol org-description-max-indent))
  16357. (setq tcol (+ bcol 5)))
  16358. (if (not itemp)
  16359. (setq column tcol)
  16360. (goto-char pos)
  16361. (beginning-of-line 1)
  16362. (if (looking-at "\\S-")
  16363. (progn
  16364. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  16365. (setq bullet (match-string 1)
  16366. btype (if (string-match "[0-9]" bullet) "n" bullet))
  16367. (setq column (if (equal btype bullet-type) bcol tcol)))
  16368. (setq column (org-get-indentation)))))
  16369. (t (setq column (org-get-indentation))))))
  16370. (goto-char pos)
  16371. (if (<= (current-column) (current-indentation))
  16372. (org-indent-line-to column)
  16373. (save-excursion (org-indent-line-to column)))
  16374. (setq column (current-column))
  16375. (beginning-of-line 1)
  16376. (if (looking-at
  16377. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  16378. (replace-match (concat (match-string 1)
  16379. (format org-property-format
  16380. (match-string 2) (match-string 3)))
  16381. t t))
  16382. (org-move-to-column column)))
  16383. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  16384. "Variable to store copy of `adaptive-fill-regexp'.
  16385. Since `adaptive-fill-regexp' is set to never match, we need to
  16386. store a backup of its value before entering `org-mode' so that
  16387. the functionality can be provided as a fall-back.")
  16388. (defun org-set-autofill-regexps ()
  16389. (interactive)
  16390. ;; In the paragraph separator we include headlines, because filling
  16391. ;; text in a line directly attached to a headline would otherwise
  16392. ;; fill the headline as well.
  16393. (org-set-local 'comment-start-skip "^#+[ \t]*")
  16394. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  16395. ;; The paragraph starter includes hand-formatted lists.
  16396. (org-set-local
  16397. 'paragraph-start
  16398. (concat
  16399. "\f" "\\|"
  16400. "[ ]*$" "\\|"
  16401. "\\*+ " "\\|"
  16402. "[ \t]*#" "\\|"
  16403. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  16404. "[ \t]*[:|]" "\\|"
  16405. "\\$\\$" "\\|"
  16406. "\\\\\\(begin\\|end\\|[][]\\)"))
  16407. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  16408. ;; But only if the user has not turned off tables or fixed-width regions
  16409. (org-set-local
  16410. 'auto-fill-inhibit-regexp
  16411. (concat "\\*+ \\|#\\+"
  16412. "\\|[ \t]*" org-keyword-time-regexp
  16413. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  16414. (concat
  16415. "\\|[ \t]*["
  16416. (if org-enable-table-editor "|" "")
  16417. (if org-enable-fixed-width-editor ":" "")
  16418. "]"))))
  16419. ;; We use our own fill-paragraph function, to make sure that tables
  16420. ;; and fixed-width regions are not wrapped. That function will pass
  16421. ;; through to `fill-paragraph' when appropriate.
  16422. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  16423. ;; Adaptive filling: To get full control, first make sure that
  16424. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  16425. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  16426. (org-set-local 'org-adaptive-fill-regexp-backup
  16427. adaptive-fill-regexp))
  16428. (org-set-local 'adaptive-fill-regexp "\000")
  16429. (org-set-local 'adaptive-fill-function
  16430. 'org-adaptive-fill-function)
  16431. (org-set-local
  16432. 'align-mode-rules-list
  16433. '((org-in-buffer-settings
  16434. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  16435. (modes . '(org-mode))))))
  16436. (defun org-fill-paragraph (&optional justify)
  16437. "Re-align a table, pass through to fill-paragraph if no table."
  16438. (let ((table-p (org-at-table-p))
  16439. (table.el-p (org-at-table.el-p)))
  16440. (cond ((and (equal (char-after (point-at-bol)) ?*)
  16441. (save-excursion (goto-char (point-at-bol))
  16442. (looking-at outline-regexp)))
  16443. t) ; skip headlines
  16444. (table.el-p t) ; skip table.el tables
  16445. (table-p (org-table-align) t) ; align org-mode tables
  16446. (t nil)))) ; call paragraph-fill
  16447. ;; For reference, this is the default value of adaptive-fill-regexp
  16448. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  16449. (defun org-adaptive-fill-function ()
  16450. "Return a fill prefix for org-mode files.
  16451. In particular, this makes sure hanging paragraphs for hand-formatted lists
  16452. work correctly."
  16453. (cond
  16454. ;; Comment line
  16455. ((looking-at "#[ \t]+")
  16456. (match-string-no-properties 0))
  16457. ;; Description list
  16458. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  16459. (save-excursion
  16460. (if (> (match-end 1) (+ (match-beginning 1)
  16461. org-description-max-indent))
  16462. (goto-char (+ (match-beginning 1) 5))
  16463. (goto-char (match-end 0)))
  16464. (make-string (current-column) ?\ )))
  16465. ;; Ordered or unordered list
  16466. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)")
  16467. (save-excursion
  16468. (goto-char (match-end 0))
  16469. (make-string (current-column) ?\ )))
  16470. ;; Other text
  16471. ((looking-at org-adaptive-fill-regexp-backup)
  16472. (match-string-no-properties 0))))
  16473. ;;; Other stuff.
  16474. (defun org-toggle-fixed-width-section (arg)
  16475. "Toggle the fixed-width export.
  16476. If there is no active region, the QUOTE keyword at the current headline is
  16477. inserted or removed. When present, it causes the text between this headline
  16478. and the next to be exported as fixed-width text, and unmodified.
  16479. If there is an active region, this command adds or removes a colon as the
  16480. first character of this line. If the first character of a line is a colon,
  16481. this line is also exported in fixed-width font."
  16482. (interactive "P")
  16483. (let* ((cc 0)
  16484. (regionp (org-region-active-p))
  16485. (beg (if regionp (region-beginning) (point)))
  16486. (end (if regionp (region-end)))
  16487. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  16488. (case-fold-search nil)
  16489. (re "[ \t]*\\(: \\)")
  16490. off)
  16491. (if regionp
  16492. (save-excursion
  16493. (goto-char beg)
  16494. (setq cc (current-column))
  16495. (beginning-of-line 1)
  16496. (setq off (looking-at re))
  16497. (while (> nlines 0)
  16498. (setq nlines (1- nlines))
  16499. (beginning-of-line 1)
  16500. (cond
  16501. (arg
  16502. (org-move-to-column cc t)
  16503. (insert ": \n")
  16504. (forward-line -1))
  16505. ((and off (looking-at re))
  16506. (replace-match "" t t nil 1))
  16507. ((not off) (org-move-to-column cc t) (insert ": ")))
  16508. (forward-line 1)))
  16509. (save-excursion
  16510. (org-back-to-heading)
  16511. (if (looking-at (concat outline-regexp
  16512. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  16513. (replace-match "" t t nil 1)
  16514. (if (looking-at outline-regexp)
  16515. (progn
  16516. (goto-char (match-end 0))
  16517. (insert org-quote-string " "))))))))
  16518. (defun org-reftex-citation ()
  16519. "Use reftex-citation to insert a citation into the buffer.
  16520. This looks for a line like
  16521. #+BIBLIOGRAPHY: foo plain option:-d
  16522. and derives from it that foo.bib is the bibliography file relevant
  16523. for this document. It then installs the necessary environment for RefTeX
  16524. to work in this buffer and calls `reftex-citation' to insert a citation
  16525. into the buffer.
  16526. Export of such citations to both LaTeX and HTML is handled by the contributed
  16527. package org-exp-bibtex by Taru Karttunen."
  16528. (interactive)
  16529. (let ((reftex-docstruct-symbol 'rds)
  16530. (reftex-cite-format "\\cite{%l}")
  16531. rds bib)
  16532. (save-excursion
  16533. (save-restriction
  16534. (widen)
  16535. (let ((case-fold-search t)
  16536. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  16537. (if (not (save-excursion
  16538. (or (re-search-forward re nil t)
  16539. (re-search-backward re nil t))))
  16540. (error "No bibliography defined in file")
  16541. (setq bib (concat (match-string 1) ".bib")
  16542. rds (list (list 'bib bib)))))))
  16543. (call-interactively 'reftex-citation)))
  16544. ;;;; Functions extending outline functionality
  16545. (defun org-beginning-of-line (&optional arg)
  16546. "Go to the beginning of the current line. If that is invisible, continue
  16547. to a visible line beginning. This makes the function of C-a more intuitive.
  16548. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  16549. first attempt, and only move to after the tags when the cursor is already
  16550. beyond the end of the headline."
  16551. (interactive "P")
  16552. (let ((pos (point))
  16553. (special (if (consp org-special-ctrl-a/e)
  16554. (car org-special-ctrl-a/e)
  16555. org-special-ctrl-a/e))
  16556. refpos)
  16557. (if (org-bound-and-true-p line-move-visual)
  16558. (beginning-of-visual-line 1)
  16559. (beginning-of-line 1))
  16560. (if (and arg (fboundp 'move-beginning-of-line))
  16561. (call-interactively 'move-beginning-of-line)
  16562. (if (bobp)
  16563. nil
  16564. (backward-char 1)
  16565. (if (org-invisible-p)
  16566. (while (and (not (bobp)) (org-invisible-p))
  16567. (backward-char 1)
  16568. (beginning-of-line 1))
  16569. (forward-char 1))))
  16570. (when special
  16571. (cond
  16572. ((and (looking-at org-complex-heading-regexp)
  16573. (= (char-after (match-end 1)) ?\ ))
  16574. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  16575. (point-at-eol)))
  16576. (goto-char
  16577. (if (eq special t)
  16578. (cond ((> pos refpos) refpos)
  16579. ((= pos (point)) refpos)
  16580. (t (point)))
  16581. (cond ((> pos (point)) (point))
  16582. ((not (eq last-command this-command)) (point))
  16583. (t refpos)))))
  16584. ((org-at-item-p)
  16585. (goto-char
  16586. (if (eq special t)
  16587. (cond ((> pos (match-end 4)) (match-end 4))
  16588. ((= pos (point)) (match-end 4))
  16589. (t (point)))
  16590. (cond ((> pos (point)) (point))
  16591. ((not (eq last-command this-command)) (point))
  16592. (t (match-end 4))))))))
  16593. (org-no-warnings
  16594. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  16595. (defun org-end-of-line (&optional arg)
  16596. "Go to the end of the line.
  16597. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  16598. first attempt, and only move to after the tags when the cursor is already
  16599. beyond the end of the headline."
  16600. (interactive "P")
  16601. (let ((special (if (consp org-special-ctrl-a/e)
  16602. (cdr org-special-ctrl-a/e)
  16603. org-special-ctrl-a/e)))
  16604. (if (or (not special)
  16605. (not (org-on-heading-p))
  16606. arg)
  16607. (call-interactively
  16608. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  16609. ((fboundp 'move-end-of-line) 'move-end-of-line)
  16610. (t 'end-of-line)))
  16611. (let ((pos (point)))
  16612. (beginning-of-line 1)
  16613. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
  16614. (if (eq special t)
  16615. (if (or (< pos (match-beginning 1))
  16616. (= pos (match-end 0)))
  16617. (goto-char (match-beginning 1))
  16618. (goto-char (match-end 0)))
  16619. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  16620. (goto-char (match-end 0))
  16621. (goto-char (match-beginning 1))))
  16622. (call-interactively (if (fboundp 'move-end-of-line)
  16623. 'move-end-of-line
  16624. 'end-of-line)))))
  16625. (org-no-warnings
  16626. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  16627. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  16628. (define-key org-mode-map "\C-e" 'org-end-of-line)
  16629. (define-key org-mode-map [home] 'org-beginning-of-line)
  16630. (define-key org-mode-map [end] 'org-end-of-line)
  16631. (defun org-backward-sentence (&optional arg)
  16632. "Go to beginning of sentence, or beginning of table field.
  16633. This will call `backward-sentence' or `org-table-beginning-of-field',
  16634. depending on context."
  16635. (interactive "P")
  16636. (cond
  16637. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  16638. (t (call-interactively 'backward-sentence))))
  16639. (defun org-forward-sentence (&optional arg)
  16640. "Go to end of sentence, or end of table field.
  16641. This will call `forward-sentence' or `org-table-end-of-field',
  16642. depending on context."
  16643. (interactive "P")
  16644. (cond
  16645. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  16646. (t (call-interactively 'forward-sentence))))
  16647. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  16648. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  16649. (defun org-kill-line (&optional arg)
  16650. "Kill line, to tags or end of line."
  16651. (interactive "P")
  16652. (cond
  16653. ((or (not org-special-ctrl-k)
  16654. (bolp)
  16655. (not (org-on-heading-p)))
  16656. (call-interactively 'kill-line))
  16657. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  16658. (kill-region (point) (match-beginning 1))
  16659. (org-set-tags nil t))
  16660. (t (kill-region (point) (point-at-eol)))))
  16661. (define-key org-mode-map "\C-k" 'org-kill-line)
  16662. (defun org-yank (&optional arg)
  16663. "Yank. If the kill is a subtree, treat it specially.
  16664. This command will look at the current kill and check if is a single
  16665. subtree, or a series of subtrees[1]. If it passes the test, and if the
  16666. cursor is at the beginning of a line or after the stars of a currently
  16667. empty headline, then the yank is handled specially. How exactly depends
  16668. on the value of the following variables, both set by default.
  16669. org-yank-folded-subtrees
  16670. When set, the subtree(s) will be folded after insertion, but only
  16671. if doing so would now swallow text after the yanked text.
  16672. org-yank-adjusted-subtrees
  16673. When set, the subtree will be promoted or demoted in order to
  16674. fit into the local outline tree structure, which means that the level
  16675. will be adjusted so that it becomes the smaller one of the two
  16676. *visible* surrounding headings.
  16677. Any prefix to this command will cause `yank' to be called directly with
  16678. no special treatment. In particular, a simple `C-u' prefix will just
  16679. plainly yank the text as it is.
  16680. \[1] The test checks if the first non-white line is a heading
  16681. and if there are no other headings with fewer stars."
  16682. (interactive "P")
  16683. (org-yank-generic 'yank arg))
  16684. (defun org-yank-generic (command arg)
  16685. "Perform some yank-like command.
  16686. This function implements the behavior described in the `org-yank'
  16687. documentation. However, it has been generalized to work for any
  16688. interactive command with similar behavior."
  16689. ;; pretend to be command COMMAND
  16690. (setq this-command command)
  16691. (if arg
  16692. (call-interactively command)
  16693. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  16694. (and (org-kill-is-subtree-p)
  16695. (or (bolp)
  16696. (and (looking-at "[ \t]*$")
  16697. (string-match
  16698. "\\`\\*+\\'"
  16699. (buffer-substring (point-at-bol) (point)))))))
  16700. swallowp)
  16701. (cond
  16702. ((and subtreep org-yank-folded-subtrees)
  16703. (let ((beg (point))
  16704. end)
  16705. (if (and subtreep org-yank-adjusted-subtrees)
  16706. (org-paste-subtree nil nil 'for-yank)
  16707. (call-interactively command))
  16708. (setq end (point))
  16709. (goto-char beg)
  16710. (when (and (bolp) subtreep
  16711. (not (setq swallowp
  16712. (org-yank-folding-would-swallow-text beg end))))
  16713. (or (looking-at outline-regexp)
  16714. (re-search-forward (concat "^" outline-regexp) end t))
  16715. (while (and (< (point) end) (looking-at outline-regexp))
  16716. (hide-subtree)
  16717. (org-cycle-show-empty-lines 'folded)
  16718. (condition-case nil
  16719. (outline-forward-same-level 1)
  16720. (error (goto-char end)))))
  16721. (when swallowp
  16722. (message
  16723. "Inserted text not folded because that would swallow text"))
  16724. (goto-char end)
  16725. (skip-chars-forward " \t\n\r")
  16726. (beginning-of-line 1)
  16727. (push-mark beg 'nomsg)))
  16728. ((and subtreep org-yank-adjusted-subtrees)
  16729. (let ((beg (point-at-bol)))
  16730. (org-paste-subtree nil nil 'for-yank)
  16731. (push-mark beg 'nomsg)))
  16732. (t
  16733. (call-interactively command))))))
  16734. (defun org-yank-folding-would-swallow-text (beg end)
  16735. "Would hide-subtree at BEG swallow any text after END?"
  16736. (let (level)
  16737. (save-excursion
  16738. (goto-char beg)
  16739. (when (or (looking-at outline-regexp)
  16740. (re-search-forward (concat "^" outline-regexp) end t))
  16741. (setq level (org-outline-level)))
  16742. (goto-char end)
  16743. (skip-chars-forward " \t\r\n\v\f")
  16744. (if (or (eobp)
  16745. (and (bolp) (looking-at org-outline-regexp)
  16746. (<= (org-outline-level) level)))
  16747. nil ; Nothing would be swallowed
  16748. t)))) ; something would swallow
  16749. (define-key org-mode-map "\C-y" 'org-yank)
  16750. (defun org-invisible-p ()
  16751. "Check if point is at a character currently not visible."
  16752. ;; Early versions of noutline don't have `outline-invisible-p'.
  16753. (if (fboundp 'outline-invisible-p)
  16754. (outline-invisible-p)
  16755. (get-char-property (point) 'invisible)))
  16756. (defun org-invisible-p2 ()
  16757. "Check if point is at a character currently not visible."
  16758. (save-excursion
  16759. (if (and (eolp) (not (bobp))) (backward-char 1))
  16760. ;; Early versions of noutline don't have `outline-invisible-p'.
  16761. (if (fboundp 'outline-invisible-p)
  16762. (outline-invisible-p)
  16763. (get-char-property (point) 'invisible))))
  16764. (defun org-back-to-heading (&optional invisible-ok)
  16765. "Call `outline-back-to-heading', but provide a better error message."
  16766. (condition-case nil
  16767. (outline-back-to-heading invisible-ok)
  16768. (error (error "Before first headline at position %d in buffer %s"
  16769. (point) (current-buffer)))))
  16770. (defun org-before-first-heading-p ()
  16771. "Before first heading?"
  16772. (save-excursion
  16773. (null (re-search-backward "^\\*+ " nil t))))
  16774. (defun org-on-heading-p (&optional ignored)
  16775. (outline-on-heading-p t))
  16776. (defun org-at-heading-p (&optional ignored)
  16777. (outline-on-heading-p t))
  16778. (defun org-point-at-end-of-empty-headline ()
  16779. "If point is at the end of an empty headline, return t, else nil.
  16780. If the heading only contains a TODO keyword, it is still still considered
  16781. empty."
  16782. (and (looking-at "[ \t]*$")
  16783. (save-excursion
  16784. (beginning-of-line 1)
  16785. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  16786. "\\)?[ \t]*$")))))
  16787. (defun org-at-heading-or-item-p ()
  16788. (or (org-on-heading-p) (org-at-item-p)))
  16789. (defun org-on-target-p ()
  16790. (or (org-in-regexp org-radio-target-regexp)
  16791. (org-in-regexp org-target-regexp)))
  16792. (defun org-up-heading-all (arg)
  16793. "Move to the heading line of which the present line is a subheading.
  16794. This function considers both visible and invisible heading lines.
  16795. With argument, move up ARG levels."
  16796. (if (fboundp 'outline-up-heading-all)
  16797. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  16798. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  16799. (defun org-up-heading-safe ()
  16800. "Move to the heading line of which the present line is a subheading.
  16801. This version will not throw an error. It will return the level of the
  16802. headline found, or nil if no higher level is found.
  16803. Also, this function will be a lot faster than `outline-up-heading',
  16804. because it relies on stars being the outline starters. This can really
  16805. make a significant difference in outlines with very many siblings."
  16806. (let (start-level re)
  16807. (org-back-to-heading t)
  16808. (setq start-level (funcall outline-level))
  16809. (if (equal start-level 1)
  16810. nil
  16811. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  16812. (if (re-search-backward re nil t)
  16813. (funcall outline-level)))))
  16814. (defun org-first-sibling-p ()
  16815. "Is this heading the first child of its parents?"
  16816. (interactive)
  16817. (let ((re (concat "^" outline-regexp))
  16818. level l)
  16819. (unless (org-at-heading-p t)
  16820. (error "Not at a heading"))
  16821. (setq level (funcall outline-level))
  16822. (save-excursion
  16823. (if (not (re-search-backward re nil t))
  16824. t
  16825. (setq l (funcall outline-level))
  16826. (< l level)))))
  16827. (defun org-goto-sibling (&optional previous)
  16828. "Goto the next sibling, even if it is invisible.
  16829. When PREVIOUS is set, go to the previous sibling instead. Returns t
  16830. when a sibling was found. When none is found, return nil and don't
  16831. move point."
  16832. (let ((fun (if previous 're-search-backward 're-search-forward))
  16833. (pos (point))
  16834. (re (concat "^" outline-regexp))
  16835. level l)
  16836. (when (condition-case nil (org-back-to-heading t) (error nil))
  16837. (setq level (funcall outline-level))
  16838. (catch 'exit
  16839. (or previous (forward-char 1))
  16840. (while (funcall fun re nil t)
  16841. (setq l (funcall outline-level))
  16842. (when (< l level) (goto-char pos) (throw 'exit nil))
  16843. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  16844. (goto-char pos)
  16845. nil))))
  16846. (defun org-show-siblings ()
  16847. "Show all siblings of the current headline."
  16848. (save-excursion
  16849. (while (org-goto-sibling) (org-flag-heading nil)))
  16850. (save-excursion
  16851. (while (org-goto-sibling 'previous)
  16852. (org-flag-heading nil))))
  16853. (defun org-show-hidden-entry ()
  16854. "Show an entry where even the heading is hidden."
  16855. (save-excursion
  16856. (org-show-entry)))
  16857. (defun org-flag-heading (flag &optional entry)
  16858. "Flag the current heading. FLAG non-nil means make invisible.
  16859. When ENTRY is non-nil, show the entire entry."
  16860. (save-excursion
  16861. (org-back-to-heading t)
  16862. ;; Check if we should show the entire entry
  16863. (if entry
  16864. (progn
  16865. (org-show-entry)
  16866. (save-excursion
  16867. (and (outline-next-heading)
  16868. (org-flag-heading nil))))
  16869. (outline-flag-region (max (point-min) (1- (point)))
  16870. (save-excursion (outline-end-of-heading) (point))
  16871. flag))))
  16872. (defun org-get-next-sibling ()
  16873. "Move to next heading of the same level, and return point.
  16874. If there is no such heading, return nil.
  16875. This is like outline-next-sibling, but invisible headings are ok."
  16876. (let ((level (funcall outline-level)))
  16877. (outline-next-heading)
  16878. (while (and (not (eobp)) (> (funcall outline-level) level))
  16879. (outline-next-heading))
  16880. (if (or (eobp) (< (funcall outline-level) level))
  16881. nil
  16882. (point))))
  16883. (defun org-get-last-sibling ()
  16884. "Move to previous heading of the same level, and return point.
  16885. If there is no such heading, return nil."
  16886. (let ((opoint (point))
  16887. (level (funcall outline-level)))
  16888. (outline-previous-heading)
  16889. (when (and (/= (point) opoint) (outline-on-heading-p t))
  16890. (while (and (> (funcall outline-level) level)
  16891. (not (bobp)))
  16892. (outline-previous-heading))
  16893. (if (< (funcall outline-level) level)
  16894. nil
  16895. (point)))))
  16896. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  16897. ;; This contains an exact copy of the original function, but it uses
  16898. ;; `org-back-to-heading', to make it work also in invisible
  16899. ;; trees. And is uses an invisible-OK argument.
  16900. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  16901. ;; Furthermore, when used inside Org, finding the end of a large subtree
  16902. ;; with many children and grandchildren etc, this can be much faster
  16903. ;; than the outline version.
  16904. (org-back-to-heading invisible-OK)
  16905. (let ((first t)
  16906. (level (funcall outline-level)))
  16907. (if (and (org-mode-p) (< level 1000))
  16908. ;; A true heading (not a plain list item), in Org-mode
  16909. ;; This means we can easily find the end by looking
  16910. ;; only for the right number of stars. Using a regexp to do
  16911. ;; this is so much faster than using a Lisp loop.
  16912. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  16913. (forward-char 1)
  16914. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  16915. ;; something else, do it the slow way
  16916. (while (and (not (eobp))
  16917. (or first (> (funcall outline-level) level)))
  16918. (setq first nil)
  16919. (outline-next-heading)))
  16920. (unless to-heading
  16921. (if (memq (preceding-char) '(?\n ?\^M))
  16922. (progn
  16923. ;; Go to end of line before heading
  16924. (forward-char -1)
  16925. (if (memq (preceding-char) '(?\n ?\^M))
  16926. ;; leave blank line before heading
  16927. (forward-char -1))))))
  16928. (point))
  16929. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  16930. "Use Org version in org-mode, for dramatic speed-up."
  16931. (if (eq major-mode 'org-mode)
  16932. (progn
  16933. (org-end-of-subtree nil t)
  16934. (unless (eobp) (backward-char 1)))
  16935. ad-do-it))
  16936. (defun org-forward-same-level (arg &optional invisible-ok)
  16937. "Move forward to the arg'th subheading at same level as this one.
  16938. Stop at the first and last subheadings of a superior heading."
  16939. (interactive "p")
  16940. (org-back-to-heading invisible-ok)
  16941. (org-on-heading-p)
  16942. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  16943. (re (format "^\\*\\{1,%d\\} " level))
  16944. l)
  16945. (forward-char 1)
  16946. (while (> arg 0)
  16947. (while (and (re-search-forward re nil 'move)
  16948. (setq l (- (match-end 0) (match-beginning 0) 1))
  16949. (= l level)
  16950. (not invisible-ok)
  16951. (progn (backward-char 1) (org-invisible-p)))
  16952. (if (< l level) (setq arg 1)))
  16953. (setq arg (1- arg)))
  16954. (beginning-of-line 1)))
  16955. (defun org-backward-same-level (arg &optional invisible-ok)
  16956. "Move backward to the arg'th subheading at same level as this one.
  16957. Stop at the first and last subheadings of a superior heading."
  16958. (interactive "p")
  16959. (org-back-to-heading)
  16960. (org-on-heading-p)
  16961. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  16962. (re (format "^\\*\\{1,%d\\} " level))
  16963. l)
  16964. (while (> arg 0)
  16965. (while (and (re-search-backward re nil 'move)
  16966. (setq l (- (match-end 0) (match-beginning 0) 1))
  16967. (= l level)
  16968. (not invisible-ok)
  16969. (org-invisible-p))
  16970. (if (< l level) (setq arg 1)))
  16971. (setq arg (1- arg)))))
  16972. (defun org-show-subtree ()
  16973. "Show everything after this heading at deeper levels."
  16974. (outline-flag-region
  16975. (point)
  16976. (save-excursion
  16977. (org-end-of-subtree t t))
  16978. nil))
  16979. (defun org-show-entry ()
  16980. "Show the body directly following this heading.
  16981. Show the heading too, if it is currently invisible."
  16982. (interactive)
  16983. (save-excursion
  16984. (condition-case nil
  16985. (progn
  16986. (org-back-to-heading t)
  16987. (outline-flag-region
  16988. (max (point-min) (1- (point)))
  16989. (save-excursion
  16990. (if (re-search-forward
  16991. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  16992. (match-beginning 1)
  16993. (point-max)))
  16994. nil)
  16995. (org-cycle-hide-drawers 'children))
  16996. (error nil))))
  16997. (defun org-make-options-regexp (kwds &optional extra)
  16998. "Make a regular expression for keyword lines."
  16999. (concat
  17000. "^"
  17001. "#?[ \t]*\\+\\("
  17002. (mapconcat 'regexp-quote kwds "\\|")
  17003. (if extra (concat "\\|" extra))
  17004. "\\):[ \t]*"
  17005. "\\(.*\\)"))
  17006. ;; Make isearch reveal the necessary context
  17007. (defun org-isearch-end ()
  17008. "Reveal context after isearch exits."
  17009. (when isearch-success ; only if search was successful
  17010. (if (featurep 'xemacs)
  17011. ;; Under XEmacs, the hook is run in the correct place,
  17012. ;; we directly show the context.
  17013. (org-show-context 'isearch)
  17014. ;; In Emacs the hook runs *before* restoring the overlays.
  17015. ;; So we have to use a one-time post-command-hook to do this.
  17016. ;; (Emacs 22 has a special variable, see function `org-mode')
  17017. (unless (and (boundp 'isearch-mode-end-hook-quit)
  17018. isearch-mode-end-hook-quit)
  17019. ;; Only when the isearch was not quitted.
  17020. (org-add-hook 'post-command-hook 'org-isearch-post-command
  17021. 'append 'local)))))
  17022. (defun org-isearch-post-command ()
  17023. "Remove self from hook, and show context."
  17024. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  17025. (org-show-context 'isearch))
  17026. ;;;; Integration with and fixes for other packages
  17027. ;;; Imenu support
  17028. (defvar org-imenu-markers nil
  17029. "All markers currently used by Imenu.")
  17030. (make-variable-buffer-local 'org-imenu-markers)
  17031. (defun org-imenu-new-marker (&optional pos)
  17032. "Return a new marker for use by Imenu, and remember the marker."
  17033. (let ((m (make-marker)))
  17034. (move-marker m (or pos (point)))
  17035. (push m org-imenu-markers)
  17036. m))
  17037. (defun org-imenu-get-tree ()
  17038. "Produce the index for Imenu."
  17039. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  17040. (setq org-imenu-markers nil)
  17041. (let* ((n org-imenu-depth)
  17042. (re (concat "^" outline-regexp))
  17043. (subs (make-vector (1+ n) nil))
  17044. (last-level 0)
  17045. m level head)
  17046. (save-excursion
  17047. (save-restriction
  17048. (widen)
  17049. (goto-char (point-max))
  17050. (while (re-search-backward re nil t)
  17051. (setq level (org-reduced-level (funcall outline-level)))
  17052. (when (<= level n)
  17053. (looking-at org-complex-heading-regexp)
  17054. (setq head (org-link-display-format
  17055. (org-match-string-no-properties 4))
  17056. m (org-imenu-new-marker))
  17057. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  17058. (if (>= level last-level)
  17059. (push (cons head m) (aref subs level))
  17060. (push (cons head (aref subs (1+ level))) (aref subs level))
  17061. (loop for i from (1+ level) to n do (aset subs i nil)))
  17062. (setq last-level level)))))
  17063. (aref subs 1)))
  17064. (eval-after-load "imenu"
  17065. '(progn
  17066. (add-hook 'imenu-after-jump-hook
  17067. (lambda ()
  17068. (if (eq major-mode 'org-mode)
  17069. (org-show-context 'org-goto))))))
  17070. (defun org-link-display-format (link)
  17071. "Replace a link with either the description, or the link target
  17072. if no description is present"
  17073. (save-match-data
  17074. (if (string-match org-bracket-link-analytic-regexp link)
  17075. (replace-match (if (match-end 5)
  17076. (match-string 5 link)
  17077. (concat (match-string 1 link)
  17078. (match-string 3 link)))
  17079. nil t link)
  17080. link)))
  17081. ;; Speedbar support
  17082. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  17083. "Overlay marking the agenda restriction line in speedbar.")
  17084. (overlay-put org-speedbar-restriction-lock-overlay
  17085. 'face 'org-agenda-restriction-lock)
  17086. (overlay-put org-speedbar-restriction-lock-overlay
  17087. 'help-echo "Agendas are currently limited to this item.")
  17088. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  17089. (defun org-speedbar-set-agenda-restriction ()
  17090. "Restrict future agenda commands to the location at point in speedbar.
  17091. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  17092. (interactive)
  17093. (require 'org-agenda)
  17094. (let (p m tp np dir txt)
  17095. (cond
  17096. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  17097. 'org-imenu t))
  17098. (setq m (get-text-property p 'org-imenu-marker))
  17099. (with-current-buffer (marker-buffer m)
  17100. (goto-char m)
  17101. (org-agenda-set-restriction-lock 'subtree)))
  17102. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  17103. 'speedbar-function 'speedbar-find-file))
  17104. (setq tp (previous-single-property-change
  17105. (1+ p) 'speedbar-function)
  17106. np (next-single-property-change
  17107. tp 'speedbar-function)
  17108. dir (speedbar-line-directory)
  17109. txt (buffer-substring-no-properties (or tp (point-min))
  17110. (or np (point-max))))
  17111. (with-current-buffer (find-file-noselect
  17112. (let ((default-directory dir))
  17113. (expand-file-name txt)))
  17114. (unless (org-mode-p)
  17115. (error "Cannot restrict to non-Org-mode file"))
  17116. (org-agenda-set-restriction-lock 'file)))
  17117. (t (error "Don't know how to restrict Org-mode's agenda")))
  17118. (move-overlay org-speedbar-restriction-lock-overlay
  17119. (point-at-bol) (point-at-eol))
  17120. (setq current-prefix-arg nil)
  17121. (org-agenda-maybe-redo)))
  17122. (eval-after-load "speedbar"
  17123. '(progn
  17124. (speedbar-add-supported-extension ".org")
  17125. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  17126. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  17127. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  17128. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17129. (add-hook 'speedbar-visiting-tag-hook
  17130. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  17131. ;;; Fixes and Hacks for problems with other packages
  17132. ;; Make flyspell not check words in links, to not mess up our keymap
  17133. (defun org-mode-flyspell-verify ()
  17134. "Don't let flyspell put overlays at active buttons."
  17135. (and (not (get-text-property (point) 'keymap))
  17136. (not (get-text-property (point) 'org-no-flyspell))))
  17137. (defun org-remove-flyspell-overlays-in (beg end)
  17138. "Remove flyspell overlays in region."
  17139. (and (org-bound-and-true-p flyspell-mode)
  17140. (fboundp 'flyspell-delete-region-overlays)
  17141. (flyspell-delete-region-overlays beg end))
  17142. (add-text-properties beg end '(org-no-flyspell t)))
  17143. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  17144. (eval-after-load "bookmark"
  17145. '(if (boundp 'bookmark-after-jump-hook)
  17146. ;; We can use the hook
  17147. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  17148. ;; Hook not available, use advice
  17149. (defadvice bookmark-jump (after org-make-visible activate)
  17150. "Make the position visible."
  17151. (org-bookmark-jump-unhide))))
  17152. ;; Make sure saveplace shows the location if it was hidden
  17153. (eval-after-load "saveplace"
  17154. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  17155. "Make the position visible."
  17156. (org-bookmark-jump-unhide)))
  17157. ;; Make sure ecb shows the location if it was hidden
  17158. (eval-after-load "ecb"
  17159. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  17160. "Make hierarchy visible when jumping into location from ECB tree buffer."
  17161. (if (eq major-mode 'org-mode)
  17162. (org-show-context))))
  17163. (defun org-bookmark-jump-unhide ()
  17164. "Unhide the current position, to show the bookmark location."
  17165. (and (org-mode-p)
  17166. (or (org-invisible-p)
  17167. (save-excursion (goto-char (max (point-min) (1- (point))))
  17168. (org-invisible-p)))
  17169. (org-show-context 'bookmark-jump)))
  17170. ;; Make session.el ignore our circular variable
  17171. (eval-after-load "session"
  17172. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  17173. ;;;; Experimental code
  17174. (defun org-closed-in-range ()
  17175. "Sparse tree of items closed in a certain time range.
  17176. Still experimental, may disappear in the future."
  17177. (interactive)
  17178. ;; Get the time interval from the user.
  17179. (let* ((time1 (org-float-time
  17180. (org-read-date nil 'to-time nil "Starting date: ")))
  17181. (time2 (org-float-time
  17182. (org-read-date nil 'to-time nil "End date:")))
  17183. ;; callback function
  17184. (callback (lambda ()
  17185. (let ((time
  17186. (org-float-time
  17187. (apply 'encode-time
  17188. (org-parse-time-string
  17189. (match-string 1))))))
  17190. ;; check if time in interval
  17191. (and (>= time time1) (<= time time2))))))
  17192. ;; make tree, check each match with the callback
  17193. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  17194. ;;;; Finish up
  17195. (provide 'org)
  17196. (run-hooks 'org-load-hook)
  17197. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  17198. ;;; org.el ends here