org.el 797 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2011 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 7.8.03
  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. (when (fboundp 'defvaralias)
  74. (unless (boundp 'calendar-view-holidays-initially-flag)
  75. (defvaralias 'calendar-view-holidays-initially-flag
  76. 'view-calendar-holidays-initially))
  77. (unless (boundp 'calendar-view-diary-initially-flag)
  78. (defvaralias 'calendar-view-diary-initially-flag
  79. 'view-diary-entries-initially))
  80. (unless (boundp 'diary-fancy-buffer)
  81. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  82. (require 'outline) (require 'noutline)
  83. ;; Other stuff we need.
  84. (require 'time-date)
  85. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  86. (require 'easymenu)
  87. (require 'overlay)
  88. (require 'org-macs)
  89. (require 'org-entities)
  90. (require 'org-compat)
  91. (require 'org-faces)
  92. (require 'org-list)
  93. (require 'org-pcomplete)
  94. (require 'org-src)
  95. (require 'org-footnote)
  96. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  97. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  98. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  99. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  100. (declare-function org-at-clock-log-p "org-clock" ())
  101. (declare-function org-clock-timestamps-up "org-clock" ())
  102. (declare-function org-clock-timestamps-down "org-clock" ())
  103. ;; babel
  104. (require 'ob)
  105. (require 'ob-table)
  106. (require 'ob-lob)
  107. (require 'ob-ref)
  108. (require 'ob-tangle)
  109. (require 'ob-comint)
  110. (require 'ob-keys)
  111. ;; load languages based on value of `org-babel-load-languages'
  112. (defvar org-babel-load-languages)
  113. ;;;###autoload
  114. (defun org-babel-do-load-languages (sym value)
  115. "Load the languages defined in `org-babel-load-languages'."
  116. (set-default sym value)
  117. (mapc (lambda (pair)
  118. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  119. (if active
  120. (progn
  121. (require (intern (concat "ob-" lang))))
  122. (progn
  123. (funcall 'fmakunbound
  124. (intern (concat "org-babel-execute:" lang)))
  125. (funcall 'fmakunbound
  126. (intern (concat "org-babel-expand-body:" lang)))))))
  127. org-babel-load-languages))
  128. (defcustom org-babel-load-languages '((emacs-lisp . t))
  129. "Languages which can be evaluated in Org-mode buffers.
  130. This list can be used to load support for any of the languages
  131. below, note that each language will depend on a different set of
  132. system executables and/or Emacs modes. When a language is
  133. \"loaded\", then code blocks in that language can be evaluated
  134. with `org-babel-execute-src-block' bound by default to C-c
  135. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  136. be set to remove code block evaluation from the C-c C-c
  137. keybinding. By default only Emacs Lisp (which has no
  138. requirements) is loaded."
  139. :group 'org-babel
  140. :set 'org-babel-do-load-languages
  141. :type '(alist :tag "Babel Languages"
  142. :key-type
  143. (choice
  144. (const :tag "Awk" awk)
  145. (const :tag "C" C)
  146. (const :tag "R" R)
  147. (const :tag "Asymptote" asymptote)
  148. (const :tag "Calc" calc)
  149. (const :tag "Clojure" clojure)
  150. (const :tag "CSS" css)
  151. (const :tag "Ditaa" ditaa)
  152. (const :tag "Dot" dot)
  153. (const :tag "Emacs Lisp" emacs-lisp)
  154. (const :tag "Fortran" fortran)
  155. (const :tag "Gnuplot" gnuplot)
  156. (const :tag "Haskell" haskell)
  157. (const :tag "Java" java)
  158. (const :tag "Javascript" js)
  159. (const :tag "Latex" latex)
  160. (const :tag "Ledger" ledger)
  161. (const :tag "Lilypond" lilypond)
  162. (const :tag "Maxima" maxima)
  163. (const :tag "Matlab" matlab)
  164. (const :tag "Mscgen" mscgen)
  165. (const :tag "Ocaml" ocaml)
  166. (const :tag "Octave" octave)
  167. (const :tag "Org" org)
  168. (const :tag "Perl" perl)
  169. (const :tag "Pico Lisp" picolisp)
  170. (const :tag "PlantUML" plantuml)
  171. (const :tag "Python" python)
  172. (const :tag "Ruby" ruby)
  173. (const :tag "Sass" sass)
  174. (const :tag "Scheme" scheme)
  175. (const :tag "Screen" screen)
  176. (const :tag "Shell Script" sh)
  177. (const :tag "Shen" shen)
  178. (const :tag "Sql" sql)
  179. (const :tag "Sqlite" sqlite))
  180. :value-type (boolean :tag "Activate" :value t)))
  181. ;;;; Customization variables
  182. (defcustom org-clone-delete-id nil
  183. "Remove ID property of clones of a subtree.
  184. When non-nil, clones of a subtree don't inherit the ID property.
  185. Otherwise they inherit the ID property with a new unique
  186. identifier."
  187. :type 'boolean
  188. :group 'org-id)
  189. ;;; Version
  190. (defconst org-version "7.8.03"
  191. "The version number of the file org.el.")
  192. (defun org-version (&optional here)
  193. "Show the org-mode version in the echo area.
  194. With prefix arg HERE, insert it at point."
  195. (interactive "P")
  196. (let* ((origin default-directory)
  197. (version org-version)
  198. (git-version)
  199. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  200. (when (and (file-exists-p (expand-file-name ".git" dir))
  201. (executable-find "git"))
  202. (unwind-protect
  203. (progn
  204. (cd dir)
  205. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  206. (with-current-buffer "*Shell Command Output*"
  207. (goto-char (point-min))
  208. (setq git-version (buffer-substring (point) (point-at-eol))))
  209. (subst-char-in-string ?- ?. git-version t)
  210. (when (string-match "\\S-"
  211. (shell-command-to-string
  212. "git diff-index --name-only HEAD --"))
  213. (setq git-version (concat git-version ".dirty")))
  214. (setq version (concat version " (" git-version ")"))))
  215. (cd origin)))
  216. (setq version (format "Org-mode version %s" version))
  217. (if here (insert version))
  218. (message version)))
  219. ;;; Compatibility constants
  220. ;;; The custom variables
  221. (defgroup org nil
  222. "Outline-based notes management and organizer."
  223. :tag "Org"
  224. :group 'outlines
  225. :group 'calendar)
  226. (defcustom org-mode-hook nil
  227. "Mode hook for Org-mode, run after the mode was turned on."
  228. :group 'org
  229. :type 'hook)
  230. (defcustom org-load-hook nil
  231. "Hook that is run after org.el has been loaded."
  232. :group 'org
  233. :type 'hook)
  234. (defcustom org-log-buffer-setup-hook nil
  235. "Hook that is run after an Org log buffer is created."
  236. :group 'org
  237. :type 'hook)
  238. (defvar org-modules) ; defined below
  239. (defvar org-modules-loaded nil
  240. "Have the modules been loaded already?")
  241. (defun org-load-modules-maybe (&optional force)
  242. "Load all extensions listed in `org-modules'."
  243. (when (or force (not org-modules-loaded))
  244. (mapc (lambda (ext)
  245. (condition-case nil (require ext)
  246. (error (message "Problems while trying to load feature `%s'" ext))))
  247. org-modules)
  248. (setq org-modules-loaded t)))
  249. (defun org-set-modules (var value)
  250. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  251. (set var value)
  252. (when (featurep 'org)
  253. (org-load-modules-maybe 'force)))
  254. (when (org-bound-and-true-p org-modules)
  255. (let ((a (member 'org-infojs org-modules)))
  256. (and a (setcar a 'org-jsinfo))))
  257. (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)
  258. "Modules that should always be loaded together with org.el.
  259. If a description starts with <C>, the file is not part of Emacs
  260. and loading it will require that you have downloaded and properly installed
  261. the org-mode distribution.
  262. You can also use this system to load external packages (i.e. neither Org
  263. core modules, nor modules from the CONTRIB directory). Just add symbols
  264. to the end of the list. If the package is called org-xyz.el, then you need
  265. to add the symbol `xyz', and the package must have a call to
  266. (provide 'org-xyz)"
  267. :group 'org
  268. :set 'org-set-modules
  269. :type
  270. '(set :greedy t
  271. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  272. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  273. (const :tag " crypt: Encryption of subtrees" org-crypt)
  274. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  275. (const :tag " docview: Links to doc-view buffers" org-docview)
  276. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  277. (const :tag " id: Global IDs for identifying entries" org-id)
  278. (const :tag " info: Links to Info nodes" org-info)
  279. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  280. (const :tag " habit: Track your consistency with habits" org-habit)
  281. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  282. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  283. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  284. (const :tag " mew Links to Mew folders/messages" org-mew)
  285. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  286. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  287. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  288. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  289. (const :tag " vm: Links to VM folders/messages" org-vm)
  290. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  291. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  292. (const :tag " mouse: Additional mouse support" org-mouse)
  293. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  294. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  295. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  296. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  297. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  298. (const :tag "C collector: Collect properties into tables" org-collector)
  299. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  300. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  301. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  302. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  303. (const :tag "C eval: Include command output as text" org-eval)
  304. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  305. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  306. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  307. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  308. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  309. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  310. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  311. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  312. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  313. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  314. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  315. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  316. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  317. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  318. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  319. (const :tag "C registry: A registry for Org-mode links" org-registry)
  320. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  321. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  322. (const :tag "C secretary: Team management with org-mode" org-secretary)
  323. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  324. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  325. (const :tag "C track: Keep up with Org-mode development" org-track)
  326. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  327. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  328. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  329. (defcustom org-support-shift-select nil
  330. "Non-nil means make shift-cursor commands select text when possible.
  331. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  332. start selecting a region, or enlarge regions started in this way.
  333. In Org-mode, in special contexts, these same keys are used for
  334. other purposes, important enough to compete with shift selection.
  335. Org tries to balance these needs by supporting `shift-select-mode'
  336. outside these special contexts, under control of this variable.
  337. The default of this variable is nil, to avoid confusing behavior. Shifted
  338. cursor keys will then execute Org commands in the following contexts:
  339. - on a headline, changing TODO state (left/right) and priority (up/down)
  340. - on a time stamp, changing the time
  341. - in a plain list item, changing the bullet type
  342. - in a property definition line, switching between allowed values
  343. - in the BEGIN line of a clock table (changing the time block).
  344. Outside these contexts, the commands will throw an error.
  345. When this variable is t and the cursor is not in a special
  346. context, Org-mode will support shift-selection for making and
  347. enlarging regions. To make this more effective, the bullet
  348. cycling will no longer happen anywhere in an item line, but only
  349. if the cursor is exactly on the bullet.
  350. If you set this variable to the symbol `always', then the keys
  351. will not be special in headlines, property lines, and item lines,
  352. to make shift selection work there as well. If this is what you
  353. want, you can use the following alternative commands: `C-c C-t'
  354. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  355. can be used to switch TODO sets, `C-c -' to cycle item bullet
  356. types, and properties can be edited by hand or in column view.
  357. However, when the cursor is on a timestamp, shift-cursor commands
  358. will still edit the time stamp - this is just too good to give up.
  359. XEmacs user should have this variable set to nil, because
  360. `shift-select-mode' is in Emacs 23 or later only."
  361. :group 'org
  362. :type '(choice
  363. (const :tag "Never" nil)
  364. (const :tag "When outside special context" t)
  365. (const :tag "Everywhere except timestamps" always)))
  366. (defcustom org-loop-over-headlines-in-active-region nil
  367. "Shall some commands act upon headlines in the active region?
  368. When set to `t', some commands will be performed in all headlines
  369. within the active region.
  370. When set to `start-level', some commands will be performed in all
  371. headlines within the active region, provided that these headlines
  372. are of the same level than the first one.
  373. When set to a string, those commands will be performed on the
  374. matching headlines within the active region. Such string must be
  375. a tags/property/todo match as it is used in the agenda tags view.
  376. The list of commands is: `org-schedule', `org-deadline',
  377. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  378. `org-archive-to-archive-sibling'. The archiving commands skip
  379. already archived entries."
  380. :type '(choice (const :tag "Don't loop" nil)
  381. (const :tag "All headlines in active region" t)
  382. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  383. (string :tag "Tags/Property/Todo matcher"))
  384. :group 'org-todo
  385. :group 'org-archive)
  386. (defgroup org-startup nil
  387. "Options concerning startup of Org-mode."
  388. :tag "Org Startup"
  389. :group 'org)
  390. (defcustom org-startup-folded t
  391. "Non-nil means entering Org-mode will switch to OVERVIEW.
  392. This can also be configured on a per-file basis by adding one of
  393. the following lines anywhere in the buffer:
  394. #+STARTUP: fold (or `overview', this is equivalent)
  395. #+STARTUP: nofold (or `showall', this is equivalent)
  396. #+STARTUP: content
  397. #+STARTUP: showeverything"
  398. :group 'org-startup
  399. :type '(choice
  400. (const :tag "nofold: show all" nil)
  401. (const :tag "fold: overview" t)
  402. (const :tag "content: all headlines" content)
  403. (const :tag "show everything, even drawers" showeverything)))
  404. (defcustom org-startup-truncated t
  405. "Non-nil means entering Org-mode will set `truncate-lines'.
  406. This is useful since some lines containing links can be very long and
  407. uninteresting. Also tables look terrible when wrapped."
  408. :group 'org-startup
  409. :type 'boolean)
  410. (defcustom org-startup-indented nil
  411. "Non-nil means turn on `org-indent-mode' on startup.
  412. This can also be configured on a per-file basis by adding one of
  413. the following lines anywhere in the buffer:
  414. #+STARTUP: indent
  415. #+STARTUP: noindent"
  416. :group 'org-structure
  417. :type '(choice
  418. (const :tag "Not" nil)
  419. (const :tag "Globally (slow on startup in large files)" t)))
  420. (defcustom org-use-sub-superscripts t
  421. "Non-nil means interpret \"_\" and \"^\" for export.
  422. When this option is turned on, you can use TeX-like syntax for sub- and
  423. superscripts. Several characters after \"_\" or \"^\" will be
  424. considered as a single item - so grouping with {} is normally not
  425. needed. For example, the following things will be parsed as single
  426. sub- or superscripts.
  427. 10^24 or 10^tau several digits will be considered 1 item.
  428. 10^-12 or 10^-tau a leading sign with digits or a word
  429. x^2-y^3 will be read as x^2 - y^3, because items are
  430. terminated by almost any nonword/nondigit char.
  431. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  432. Still, ambiguity is possible - so when in doubt use {} to enclose the
  433. sub/superscript. If you set this variable to the symbol `{}',
  434. the braces are *required* in order to trigger interpretations as
  435. sub/superscript. This can be helpful in documents that need \"_\"
  436. frequently in plain text.
  437. Not all export backends support this, but HTML does.
  438. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  439. :group 'org-startup
  440. :group 'org-export-translation
  441. :type '(choice
  442. (const :tag "Always interpret" t)
  443. (const :tag "Only with braces" {})
  444. (const :tag "Never interpret" nil)))
  445. (if (fboundp 'defvaralias)
  446. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  447. (defcustom org-startup-with-beamer-mode nil
  448. "Non-nil means turn on `org-beamer-mode' on startup.
  449. This can also be configured on a per-file basis by adding one of
  450. the following lines anywhere in the buffer:
  451. #+STARTUP: beamer"
  452. :group 'org-startup
  453. :type 'boolean)
  454. (defcustom org-startup-align-all-tables nil
  455. "Non-nil means align all tables when visiting a file.
  456. This is useful when the column width in tables is forced with <N> cookies
  457. in table fields. Such tables will look correct only after the first re-align.
  458. This can also be configured on a per-file basis by adding one of
  459. the following lines anywhere in the buffer:
  460. #+STARTUP: align
  461. #+STARTUP: noalign"
  462. :group 'org-startup
  463. :type 'boolean)
  464. (defcustom org-startup-with-inline-images nil
  465. "Non-nil means show inline images when loading a new Org file.
  466. This can also be configured on a per-file basis by adding one of
  467. the following lines anywhere in the buffer:
  468. #+STARTUP: inlineimages
  469. #+STARTUP: noinlineimages"
  470. :group 'org-startup
  471. :type 'boolean)
  472. (defcustom org-insert-mode-line-in-empty-file nil
  473. "Non-nil means insert the first line setting Org-mode in empty files.
  474. When the function `org-mode' is called interactively in an empty file, this
  475. normally means that the file name does not automatically trigger Org-mode.
  476. To ensure that the file will always be in Org-mode in the future, a
  477. line enforcing Org-mode will be inserted into the buffer, if this option
  478. has been set."
  479. :group 'org-startup
  480. :type 'boolean)
  481. (defcustom org-replace-disputed-keys nil
  482. "Non-nil means use alternative key bindings for some keys.
  483. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  484. These keys are also used by other packages like shift-selection-mode'
  485. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  486. If you want to use Org-mode together with one of these other modes,
  487. or more generally if you would like to move some Org-mode commands to
  488. other keys, set this variable and configure the keys with the variable
  489. `org-disputed-keys'.
  490. This option is only relevant at load-time of Org-mode, and must be set
  491. *before* org.el is loaded. Changing it requires a restart of Emacs to
  492. become effective."
  493. :group 'org-startup
  494. :type 'boolean)
  495. (defcustom org-use-extra-keys nil
  496. "Non-nil means use extra key sequence definitions for certain commands.
  497. This happens automatically if you run XEmacs or if `window-system'
  498. is nil. This variable lets you do the same manually. You must
  499. set it before loading org.
  500. Example: on Carbon Emacs 22 running graphically, with an external
  501. keyboard on a Powerbook, the default way of setting M-left might
  502. not work for either Alt or ESC. Setting this variable will make
  503. it work for ESC."
  504. :group 'org-startup
  505. :type 'boolean)
  506. (if (fboundp 'defvaralias)
  507. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  508. (defcustom org-disputed-keys
  509. '(([(shift up)] . [(meta p)])
  510. ([(shift down)] . [(meta n)])
  511. ([(shift left)] . [(meta -)])
  512. ([(shift right)] . [(meta +)])
  513. ([(control shift right)] . [(meta shift +)])
  514. ([(control shift left)] . [(meta shift -)]))
  515. "Keys for which Org-mode and other modes compete.
  516. This is an alist, cars are the default keys, second element specifies
  517. the alternative to use when `org-replace-disputed-keys' is t.
  518. Keys can be specified in any syntax supported by `define-key'.
  519. The value of this option takes effect only at Org-mode's startup,
  520. therefore you'll have to restart Emacs to apply it after changing."
  521. :group 'org-startup
  522. :type 'alist)
  523. (defun org-key (key)
  524. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  525. Or return the original if not disputed.
  526. Also apply the translations defined in `org-xemacs-key-equivalents'."
  527. (when org-replace-disputed-keys
  528. (let* ((nkey (key-description key))
  529. (x (org-find-if (lambda (x)
  530. (equal (key-description (car x)) nkey))
  531. org-disputed-keys)))
  532. (setq key (if x (cdr x) key))))
  533. (when (featurep 'xemacs)
  534. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  535. key)
  536. (defun org-find-if (predicate seq)
  537. (catch 'exit
  538. (while seq
  539. (if (funcall predicate (car seq))
  540. (throw 'exit (car seq))
  541. (pop seq)))))
  542. (defun org-defkey (keymap key def)
  543. "Define a key, possibly translated, as returned by `org-key'."
  544. (define-key keymap (org-key key) def))
  545. (defcustom org-ellipsis nil
  546. "The ellipsis to use in the Org-mode outline.
  547. When nil, just use the standard three dots. When a string, use that instead,
  548. When a face, use the standard 3 dots, but with the specified face.
  549. The change affects only Org-mode (which will then use its own display table).
  550. Changing this requires executing `M-x org-mode' in a buffer to become
  551. effective."
  552. :group 'org-startup
  553. :type '(choice (const :tag "Default" nil)
  554. (face :tag "Face" :value org-warning)
  555. (string :tag "String" :value "...#")))
  556. (defvar org-display-table nil
  557. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  558. (defgroup org-keywords nil
  559. "Keywords in Org-mode."
  560. :tag "Org Keywords"
  561. :group 'org)
  562. (defcustom org-deadline-string "DEADLINE:"
  563. "String to mark deadline entries.
  564. A deadline is this string, followed by a time stamp. Should be a word,
  565. terminated by a colon. You can insert a schedule keyword and
  566. a timestamp with \\[org-deadline].
  567. Changes become only effective after restarting Emacs."
  568. :group 'org-keywords
  569. :type 'string)
  570. (defcustom org-scheduled-string "SCHEDULED:"
  571. "String to mark scheduled TODO entries.
  572. A schedule is this string, followed by a time stamp. Should be a word,
  573. terminated by a colon. You can insert a schedule keyword and
  574. a timestamp with \\[org-schedule].
  575. Changes become only effective after restarting Emacs."
  576. :group 'org-keywords
  577. :type 'string)
  578. (defcustom org-closed-string "CLOSED:"
  579. "String used as the prefix for timestamps logging closing a TODO entry."
  580. :group 'org-keywords
  581. :type 'string)
  582. (defcustom org-clock-string "CLOCK:"
  583. "String used as prefix for timestamps clocking work hours on an item."
  584. :group 'org-keywords
  585. :type 'string)
  586. (defcustom org-comment-string "COMMENT"
  587. "Entries starting with this keyword will never be exported.
  588. An entry can be toggled between COMMENT and normal with
  589. \\[org-toggle-comment].
  590. Changes become only effective after restarting Emacs."
  591. :group 'org-keywords
  592. :type 'string)
  593. (defcustom org-quote-string "QUOTE"
  594. "Entries starting with this keyword will be exported in fixed-width font.
  595. Quoting applies only to the text in the entry following the headline, and does
  596. not extend beyond the next headline, even if that is lower level.
  597. An entry can be toggled between QUOTE and normal with
  598. \\[org-toggle-fixed-width-section]."
  599. :group 'org-keywords
  600. :type 'string)
  601. (defconst org-repeat-re
  602. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  603. "Regular expression for specifying repeated events.
  604. After a match, group 1 contains the repeat expression.")
  605. (defgroup org-structure nil
  606. "Options concerning the general structure of Org-mode files."
  607. :tag "Org Structure"
  608. :group 'org)
  609. (defgroup org-reveal-location nil
  610. "Options about how to make context of a location visible."
  611. :tag "Org Reveal Location"
  612. :group 'org-structure)
  613. (defconst org-context-choice
  614. '(choice
  615. (const :tag "Always" t)
  616. (const :tag "Never" nil)
  617. (repeat :greedy t :tag "Individual contexts"
  618. (cons
  619. (choice :tag "Context"
  620. (const agenda)
  621. (const org-goto)
  622. (const occur-tree)
  623. (const tags-tree)
  624. (const link-search)
  625. (const mark-goto)
  626. (const bookmark-jump)
  627. (const isearch)
  628. (const default))
  629. (boolean))))
  630. "Contexts for the reveal options.")
  631. (defcustom org-show-hierarchy-above '((default . t))
  632. "Non-nil means show full hierarchy when revealing a location.
  633. Org-mode often shows locations in an org-mode file which might have
  634. been invisible before. When this is set, the hierarchy of headings
  635. above the exposed location is shown.
  636. Turning this off for example for sparse trees makes them very compact.
  637. Instead of t, this can also be an alist specifying this option for different
  638. contexts. Valid contexts are
  639. agenda when exposing an entry from the agenda
  640. org-goto when using the command `org-goto' on key C-c C-j
  641. occur-tree when using the command `org-occur' on key C-c /
  642. tags-tree when constructing a sparse tree based on tags matches
  643. link-search when exposing search matches associated with a link
  644. mark-goto when exposing the jump goal of a mark
  645. bookmark-jump when exposing a bookmark location
  646. isearch when exiting from an incremental search
  647. default default for all contexts not set explicitly"
  648. :group 'org-reveal-location
  649. :type org-context-choice)
  650. (defcustom org-show-following-heading '((default . nil))
  651. "Non-nil means show following heading when revealing a location.
  652. Org-mode often shows locations in an org-mode file which might have
  653. been invisible before. When this is set, the heading following the
  654. match is shown.
  655. Turning this off for example for sparse trees makes them very compact,
  656. but makes it harder to edit the location of the match. In such a case,
  657. use the command \\[org-reveal] to show more context.
  658. Instead of t, this can also be an alist specifying this option for different
  659. contexts. See `org-show-hierarchy-above' for valid contexts."
  660. :group 'org-reveal-location
  661. :type org-context-choice)
  662. (defcustom org-show-siblings '((default . nil) (isearch t))
  663. "Non-nil means show all sibling heading when revealing a location.
  664. Org-mode often shows locations in an org-mode file which might have
  665. been invisible before. When this is set, the sibling of the current entry
  666. heading are all made visible. If `org-show-hierarchy-above' is t,
  667. the same happens on each level of the hierarchy above the current entry.
  668. By default this is on for the isearch context, off for all other contexts.
  669. Turning this off for example for sparse trees makes them very compact,
  670. but makes it harder to edit the location of the match. In such a case,
  671. use the command \\[org-reveal] to show more context.
  672. Instead of t, this can also be an alist specifying this option for different
  673. contexts. See `org-show-hierarchy-above' for valid contexts."
  674. :group 'org-reveal-location
  675. :type org-context-choice)
  676. (defcustom org-show-entry-below '((default . nil))
  677. "Non-nil means show the entry below a headline when revealing a location.
  678. Org-mode often shows locations in an org-mode file which might have
  679. been invisible before. When this is set, the text below the headline that is
  680. exposed is also shown.
  681. By default this is off for all contexts.
  682. Instead of t, this can also be an alist specifying this option for different
  683. contexts. See `org-show-hierarchy-above' for valid contexts."
  684. :group 'org-reveal-location
  685. :type org-context-choice)
  686. (defcustom org-indirect-buffer-display 'other-window
  687. "How should indirect tree buffers be displayed?
  688. This applies to indirect buffers created with the commands
  689. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  690. Valid values are:
  691. current-window Display in the current window
  692. other-window Just display in another window.
  693. dedicated-frame Create one new frame, and re-use it each time.
  694. new-frame Make a new frame each time. Note that in this case
  695. previously-made indirect buffers are kept, and you need to
  696. kill these buffers yourself."
  697. :group 'org-structure
  698. :group 'org-agenda-windows
  699. :type '(choice
  700. (const :tag "In current window" current-window)
  701. (const :tag "In current frame, other window" other-window)
  702. (const :tag "Each time a new frame" new-frame)
  703. (const :tag "One dedicated frame" dedicated-frame)))
  704. (defcustom org-use-speed-commands nil
  705. "Non-nil means activate single letter commands at beginning of a headline.
  706. This may also be a function to test for appropriate locations where speed
  707. commands should be active."
  708. :group 'org-structure
  709. :type '(choice
  710. (const :tag "Never" nil)
  711. (const :tag "At beginning of headline stars" t)
  712. (function)))
  713. (defcustom org-speed-commands-user nil
  714. "Alist of additional speed commands.
  715. This list will be checked before `org-speed-commands-default'
  716. when the variable `org-use-speed-commands' is non-nil
  717. and when the cursor is at the beginning of a headline.
  718. The car if each entry is a string with a single letter, which must
  719. be assigned to `self-insert-command' in the global map.
  720. The cdr is either a command to be called interactively, a function
  721. to be called, or a form to be evaluated.
  722. An entry that is just a list with a single string will be interpreted
  723. as a descriptive headline that will be added when listing the speed
  724. commands in the Help buffer using the `?' speed command."
  725. :group 'org-structure
  726. :type '(repeat :value ("k" . ignore)
  727. (choice :value ("k" . ignore)
  728. (list :tag "Descriptive Headline" (string :tag "Headline"))
  729. (cons :tag "Letter and Command"
  730. (string :tag "Command letter")
  731. (choice
  732. (function)
  733. (sexp))))))
  734. (defgroup org-cycle nil
  735. "Options concerning visibility cycling in Org-mode."
  736. :tag "Org Cycle"
  737. :group 'org-structure)
  738. (defcustom org-cycle-skip-children-state-if-no-children t
  739. "Non-nil means skip CHILDREN state in entries that don't have any."
  740. :group 'org-cycle
  741. :type 'boolean)
  742. (defcustom org-cycle-max-level nil
  743. "Maximum level which should still be subject to visibility cycling.
  744. Levels higher than this will, for cycling, be treated as text, not a headline.
  745. When `org-odd-levels-only' is set, a value of N in this variable actually
  746. means 2N-1 stars as the limiting headline.
  747. When nil, cycle all levels.
  748. Note that the limiting level of cycling is also influenced by
  749. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  750. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  751. than its value."
  752. :group 'org-cycle
  753. :type '(choice
  754. (const :tag "No limit" nil)
  755. (integer :tag "Maximum level")))
  756. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  757. "Names of drawers. Drawers are not opened by cycling on the headline above.
  758. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  759. this:
  760. :DRAWERNAME:
  761. .....
  762. :END:
  763. The drawer \"PROPERTIES\" is special for capturing properties through
  764. the property API.
  765. Drawers can be defined on the per-file basis with a line like:
  766. #+DRAWERS: HIDDEN STATE PROPERTIES"
  767. :group 'org-structure
  768. :group 'org-cycle
  769. :type '(repeat (string :tag "Drawer Name")))
  770. (defcustom org-hide-block-startup nil
  771. "Non-nil means entering Org-mode will fold all blocks.
  772. This can also be set in on a per-file basis with
  773. #+STARTUP: hideblocks
  774. #+STARTUP: showblocks"
  775. :group 'org-startup
  776. :group 'org-cycle
  777. :type 'boolean)
  778. (defcustom org-cycle-global-at-bob nil
  779. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  780. This makes it possible to do global cycling without having to use S-TAB or
  781. \\[universal-argument] TAB. For this special case to work, the first line \
  782. of the buffer
  783. must not be a headline - it may be empty or some other text. When used in
  784. this way, `org-cycle-hook' is disables temporarily, to make sure the
  785. cursor stays at the beginning of the buffer.
  786. When this option is nil, don't do anything special at the beginning
  787. of the buffer."
  788. :group 'org-cycle
  789. :type 'boolean)
  790. (defcustom org-cycle-level-after-item/entry-creation t
  791. "Non-nil means cycle entry level or item indentation in new empty entries.
  792. When the cursor is at the end of an empty headline, i.e with only stars
  793. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  794. and then all possible ancestor states, before returning to the original state.
  795. This makes data entry extremely fast: M-RET to create a new headline,
  796. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  797. When the cursor is at the end of an empty plain list item, one TAB will
  798. make it a subitem, two or more tabs will back up to make this an item
  799. higher up in the item hierarchy."
  800. :group 'org-cycle
  801. :type 'boolean)
  802. (defcustom org-cycle-emulate-tab t
  803. "Where should `org-cycle' emulate TAB.
  804. nil Never
  805. white Only in completely white lines
  806. whitestart Only at the beginning of lines, before the first non-white char
  807. t Everywhere except in headlines
  808. exc-hl-bol Everywhere except at the start of a headline
  809. If TAB is used in a place where it does not emulate TAB, the current subtree
  810. visibility is cycled."
  811. :group 'org-cycle
  812. :type '(choice (const :tag "Never" nil)
  813. (const :tag "Only in completely white lines" white)
  814. (const :tag "Before first char in a line" whitestart)
  815. (const :tag "Everywhere except in headlines" t)
  816. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  817. ))
  818. (defcustom org-cycle-separator-lines 2
  819. "Number of empty lines needed to keep an empty line between collapsed trees.
  820. If you leave an empty line between the end of a subtree and the following
  821. headline, this empty line is hidden when the subtree is folded.
  822. Org-mode will leave (exactly) one empty line visible if the number of
  823. empty lines is equal or larger to the number given in this variable.
  824. So the default 2 means at least 2 empty lines after the end of a subtree
  825. are needed to produce free space between a collapsed subtree and the
  826. following headline.
  827. If the number is negative, and the number of empty lines is at least -N,
  828. all empty lines are shown.
  829. Special case: when 0, never leave empty lines in collapsed view."
  830. :group 'org-cycle
  831. :type 'integer)
  832. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  833. (defcustom org-pre-cycle-hook nil
  834. "Hook that is run before visibility cycling is happening.
  835. The function(s) in this hook must accept a single argument which indicates
  836. the new state that will be set right after running this hook. The
  837. argument is a symbol. Before a global state change, it can have the values
  838. `overview', `content', or `all'. Before a local state change, it can have
  839. the values `folded', `children', or `subtree'."
  840. :group 'org-cycle
  841. :type 'hook)
  842. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  843. org-cycle-hide-drawers
  844. org-cycle-show-empty-lines
  845. org-optimize-window-after-visibility-change)
  846. "Hook that is run after `org-cycle' has changed the buffer visibility.
  847. The function(s) in this hook must accept a single argument which indicates
  848. the new state that was set by the most recent `org-cycle' command. The
  849. argument is a symbol. After a global state change, it can have the values
  850. `overview', `content', or `all'. After a local state change, it can have
  851. the values `folded', `children', or `subtree'."
  852. :group 'org-cycle
  853. :type 'hook)
  854. (defgroup org-edit-structure nil
  855. "Options concerning structure editing in Org-mode."
  856. :tag "Org Edit Structure"
  857. :group 'org-structure)
  858. (defcustom org-odd-levels-only nil
  859. "Non-nil means skip even levels and only use odd levels for the outline.
  860. This has the effect that two stars are being added/taken away in
  861. promotion/demotion commands. It also influences how levels are
  862. handled by the exporters.
  863. Changing it requires restart of `font-lock-mode' to become effective
  864. for fontification also in regions already fontified.
  865. You may also set this on a per-file basis by adding one of the following
  866. lines to the buffer:
  867. #+STARTUP: odd
  868. #+STARTUP: oddeven"
  869. :group 'org-edit-structure
  870. :group 'org-appearance
  871. :type 'boolean)
  872. (defcustom org-adapt-indentation t
  873. "Non-nil means adapt indentation to outline node level.
  874. When this variable is set, Org assumes that you write outlines by
  875. indenting text in each node to align with the headline (after the stars).
  876. The following issues are influenced by this variable:
  877. - When this is set and the *entire* text in an entry is indented, the
  878. indentation is increased by one space in a demotion command, and
  879. decreased by one in a promotion command. If any line in the entry
  880. body starts with text at column 0, indentation is not changed at all.
  881. - Property drawers and planning information is inserted indented when
  882. this variable s set. When nil, they will not be indented.
  883. - TAB indents a line relative to context. The lines below a headline
  884. will be indented when this variable is set.
  885. Note that this is all about true indentation, by adding and removing
  886. space characters. See also `org-indent.el' which does level-dependent
  887. indentation in a virtual way, i.e. at display time in Emacs."
  888. :group 'org-edit-structure
  889. :type 'boolean)
  890. (defcustom org-special-ctrl-a/e nil
  891. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  892. When t, `C-a' will bring back the cursor to the beginning of the
  893. headline text, i.e. after the stars and after a possible TODO keyword.
  894. In an item, this will be the position after the bullet.
  895. When the cursor is already at that position, another `C-a' will bring
  896. it to the beginning of the line.
  897. `C-e' will jump to the end of the headline, ignoring the presence of tags
  898. in the headline. A second `C-e' will then jump to the true end of the
  899. line, after any tags. This also means that, when this variable is
  900. non-nil, `C-e' also will never jump beyond the end of the heading of a
  901. folded section, i.e. not after the ellipses.
  902. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  903. going to the true line boundary first. Only a directly following, identical
  904. keypress will bring the cursor to the special positions.
  905. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  906. set separately."
  907. :group 'org-edit-structure
  908. :type '(choice
  909. (const :tag "off" nil)
  910. (const :tag "on: after stars/bullet and before tags first" t)
  911. (const :tag "reversed: true line boundary first" reversed)
  912. (cons :tag "Set C-a and C-e separately"
  913. (choice :tag "Special C-a"
  914. (const :tag "off" nil)
  915. (const :tag "on: after stars/bullet first" t)
  916. (const :tag "reversed: before stars/bullet first" reversed))
  917. (choice :tag "Special C-e"
  918. (const :tag "off" nil)
  919. (const :tag "on: before tags first" t)
  920. (const :tag "reversed: after tags first" reversed)))))
  921. (if (fboundp 'defvaralias)
  922. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  923. (defcustom org-special-ctrl-k nil
  924. "Non-nil means `C-k' will behave specially in headlines.
  925. When nil, `C-k' will call the default `kill-line' command.
  926. When t, the following will happen while the cursor is in the headline:
  927. - When the cursor is at the beginning of a headline, kill the entire
  928. line and possible the folded subtree below the line.
  929. - When in the middle of the headline text, kill the headline up to the tags.
  930. - When after the headline text, kill the tags."
  931. :group 'org-edit-structure
  932. :type 'boolean)
  933. (defcustom org-ctrl-k-protect-subtree nil
  934. "Non-nil means, do not delete a hidden subtree with C-k.
  935. When set to the symbol `error', simply throw an error when C-k is
  936. used to kill (part-of) a headline that has hidden text behind it.
  937. Any other non-nil value will result in a query to the user, if it is
  938. OK to kill that hidden subtree. When nil, kill without remorse."
  939. :group 'org-edit-structure
  940. :type '(choice
  941. (const :tag "Do not protect hidden subtrees" nil)
  942. (const :tag "Protect hidden subtrees with a security query" t)
  943. (const :tag "Never kill a hidden subtree with C-k" error)))
  944. (defcustom org-catch-invisible-edits nil
  945. "Check if in invisible region before inserting or deleting a character.
  946. Valid values are:
  947. nil Do not check, so just do invisible edits.
  948. error Throw an error and do nothing.
  949. show Make point visible, and do the requested edit.
  950. show-and-error Make point visible, then throw an error and abort the edit.
  951. smart Make point visible, and do insertion/deletion if it is
  952. adjacent to visible text and the change feels predictable.
  953. Never delete a previously invisible character or add in the
  954. middle or right after an invisible region. Basically, this
  955. allows insertion and backward-delete right before ellipses.
  956. FIXME: maybe in this case we should not even show?"
  957. :group 'org-edit-structure
  958. :type '(choice
  959. (const :tag "Do not check" nil)
  960. (const :tag "Throw error when trying to edit" error)
  961. (const :tag "Unhide, but do not do the edit" show-and-error)
  962. (const :tag "Show invisible part and do the edit" show)
  963. (const :tag "Be smart and do the right thing" smart)))
  964. (defcustom org-yank-folded-subtrees t
  965. "Non-nil means when yanking subtrees, fold them.
  966. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  967. it starts with a heading and all other headings in it are either children
  968. or siblings, then fold all the subtrees. However, do this only if no
  969. text after the yank would be swallowed into a folded tree by this action."
  970. :group 'org-edit-structure
  971. :type 'boolean)
  972. (defcustom org-yank-adjusted-subtrees nil
  973. "Non-nil means when yanking subtrees, adjust the level.
  974. With this setting, `org-paste-subtree' is used to insert the subtree, see
  975. this function for details."
  976. :group 'org-edit-structure
  977. :type 'boolean)
  978. (defcustom org-M-RET-may-split-line '((default . t))
  979. "Non-nil means M-RET will split the line at the cursor position.
  980. When nil, it will go to the end of the line before making a
  981. new line.
  982. You may also set this option in a different way for different
  983. contexts. Valid contexts are:
  984. headline when creating a new headline
  985. item when creating a new item
  986. table in a table field
  987. default the value to be used for all contexts not explicitly
  988. customized"
  989. :group 'org-structure
  990. :group 'org-table
  991. :type '(choice
  992. (const :tag "Always" t)
  993. (const :tag "Never" nil)
  994. (repeat :greedy t :tag "Individual contexts"
  995. (cons
  996. (choice :tag "Context"
  997. (const headline)
  998. (const item)
  999. (const table)
  1000. (const default))
  1001. (boolean)))))
  1002. (defcustom org-insert-heading-respect-content nil
  1003. "Non-nil means insert new headings after the current subtree.
  1004. When nil, the new heading is created directly after the current line.
  1005. The commands \\[org-insert-heading-respect-content] and
  1006. \\[org-insert-todo-heading-respect-content] turn this variable on
  1007. for the duration of the command."
  1008. :group 'org-structure
  1009. :type 'boolean)
  1010. (defcustom org-blank-before-new-entry '((heading . auto)
  1011. (plain-list-item . auto))
  1012. "Should `org-insert-heading' leave a blank line before new heading/item?
  1013. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1014. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1015. which case Org will look at the surrounding headings/items and try to
  1016. make an intelligent decision whether to insert a blank line or not.
  1017. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1018. set, the setting here is ignored and no empty line is inserted, to avoid
  1019. breaking the list structure."
  1020. :group 'org-edit-structure
  1021. :type '(list
  1022. (cons (const heading)
  1023. (choice (const :tag "Never" nil)
  1024. (const :tag "Always" t)
  1025. (const :tag "Auto" auto)))
  1026. (cons (const plain-list-item)
  1027. (choice (const :tag "Never" nil)
  1028. (const :tag "Always" t)
  1029. (const :tag "Auto" auto)))))
  1030. (defcustom org-insert-heading-hook nil
  1031. "Hook being run after inserting a new heading."
  1032. :group 'org-edit-structure
  1033. :type 'hook)
  1034. (defcustom org-enable-fixed-width-editor t
  1035. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1036. This currently only means they are never auto-wrapped.
  1037. When nil, such lines will be treated like ordinary lines.
  1038. See also the QUOTE keyword."
  1039. :group 'org-edit-structure
  1040. :type 'boolean)
  1041. (defcustom org-goto-auto-isearch t
  1042. "Non-nil means typing characters in `org-goto' starts incremental search."
  1043. :group 'org-edit-structure
  1044. :type 'boolean)
  1045. (defgroup org-sparse-trees nil
  1046. "Options concerning sparse trees in Org-mode."
  1047. :tag "Org Sparse Trees"
  1048. :group 'org-structure)
  1049. (defcustom org-highlight-sparse-tree-matches t
  1050. "Non-nil means highlight all matches that define a sparse tree.
  1051. The highlights will automatically disappear the next time the buffer is
  1052. changed by an edit command."
  1053. :group 'org-sparse-trees
  1054. :type 'boolean)
  1055. (defcustom org-remove-highlights-with-change t
  1056. "Non-nil means any change to the buffer will remove temporary highlights.
  1057. Such highlights are created by `org-occur' and `org-clock-display'.
  1058. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1059. The highlights created by `org-preview-latex-fragment' always need
  1060. `C-c C-c' to be removed."
  1061. :group 'org-sparse-trees
  1062. :group 'org-time
  1063. :type 'boolean)
  1064. (defcustom org-occur-hook '(org-first-headline-recenter)
  1065. "Hook that is run after `org-occur' has constructed a sparse tree.
  1066. This can be used to recenter the window to show as much of the structure
  1067. as possible."
  1068. :group 'org-sparse-trees
  1069. :type 'hook)
  1070. (defgroup org-imenu-and-speedbar nil
  1071. "Options concerning imenu and speedbar in Org-mode."
  1072. :tag "Org Imenu and Speedbar"
  1073. :group 'org-structure)
  1074. (defcustom org-imenu-depth 2
  1075. "The maximum level for Imenu access to Org-mode headlines.
  1076. This also applied for speedbar access."
  1077. :group 'org-imenu-and-speedbar
  1078. :type 'integer)
  1079. (defgroup org-table nil
  1080. "Options concerning tables in Org-mode."
  1081. :tag "Org Table"
  1082. :group 'org)
  1083. (defcustom org-enable-table-editor 'optimized
  1084. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1085. When nil, such lines will be treated like ordinary lines.
  1086. When equal to the symbol `optimized', the table editor will be optimized to
  1087. do the following:
  1088. - Automatic overwrite mode in front of whitespace in table fields.
  1089. This makes the structure of the table stay in tact as long as the edited
  1090. field does not exceed the column width.
  1091. - Minimize the number of realigns. Normally, the table is aligned each time
  1092. TAB or RET are pressed to move to another field. With optimization this
  1093. happens only if changes to a field might have changed the column width.
  1094. Optimization requires replacing the functions `self-insert-command',
  1095. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1096. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1097. very good at guessing when a re-align will be necessary, but you can always
  1098. force one with \\[org-ctrl-c-ctrl-c].
  1099. If you would like to use the optimized version in Org-mode, but the
  1100. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1101. This variable can be used to turn on and off the table editor during a session,
  1102. but in order to toggle optimization, a restart is required.
  1103. See also the variable `org-table-auto-blank-field'."
  1104. :group 'org-table
  1105. :type '(choice
  1106. (const :tag "off" nil)
  1107. (const :tag "on" t)
  1108. (const :tag "on, optimized" optimized)))
  1109. (defcustom org-self-insert-cluster-for-undo t
  1110. "Non-nil means cluster self-insert commands for undo when possible.
  1111. If this is set, then, like in the Emacs command loop, 20 consecutive
  1112. characters will be undone together.
  1113. This is configurable, because there is some impact on typing performance."
  1114. :group 'org-table
  1115. :type 'boolean)
  1116. (defcustom org-table-tab-recognizes-table.el t
  1117. "Non-nil means TAB will automatically notice a table.el table.
  1118. When it sees such a table, it moves point into it and - if necessary -
  1119. calls `table-recognize-table'."
  1120. :group 'org-table-editing
  1121. :type 'boolean)
  1122. (defgroup org-link nil
  1123. "Options concerning links in Org-mode."
  1124. :tag "Org Link"
  1125. :group 'org)
  1126. (defvar org-link-abbrev-alist-local nil
  1127. "Buffer-local version of `org-link-abbrev-alist', which see.
  1128. The value of this is taken from the #+LINK lines.")
  1129. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1130. (defcustom org-link-abbrev-alist nil
  1131. "Alist of link abbreviations.
  1132. The car of each element is a string, to be replaced at the start of a link.
  1133. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1134. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1135. [[linkkey:tag][description]]
  1136. The 'linkkey' must be a word word, starting with a letter, followed
  1137. by letters, numbers, '-' or '_'.
  1138. If REPLACE is a string, the tag will simply be appended to create the link.
  1139. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1140. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1141. be inserted at that point (see the function `url-hexify-string').
  1142. REPLACE may also be a function that will be called with the tag as the
  1143. only argument to create the link, which should be returned as a string.
  1144. See the manual for examples."
  1145. :group 'org-link
  1146. :type '(repeat
  1147. (cons
  1148. (string :tag "Protocol")
  1149. (choice
  1150. (string :tag "Format")
  1151. (function)))))
  1152. (defcustom org-descriptive-links t
  1153. "Non-nil means Org will display descriptive links.
  1154. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1155. \"Org Website\", hiding the link itself and just displaying its
  1156. description. When set to `nil', Org will display the full links
  1157. literally.
  1158. You can interactively set the value of this variable by calling
  1159. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1160. :group 'org-link
  1161. :type 'boolean)
  1162. (defcustom org-link-file-path-type 'adaptive
  1163. "How the path name in file links should be stored.
  1164. Valid values are:
  1165. relative Relative to the current directory, i.e. the directory of the file
  1166. into which the link is being inserted.
  1167. absolute Absolute path, if possible with ~ for home directory.
  1168. noabbrev Absolute path, no abbreviation of home directory.
  1169. adaptive Use relative path for files in the current directory and sub-
  1170. directories of it. For other files, use an absolute path."
  1171. :group 'org-link
  1172. :type '(choice
  1173. (const relative)
  1174. (const absolute)
  1175. (const noabbrev)
  1176. (const adaptive)))
  1177. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1178. "Types of links that should be activated in Org-mode files.
  1179. This is a list of symbols, each leading to the activation of a certain link
  1180. type. In principle, it does not hurt to turn on most link types - there may
  1181. be a small gain when turning off unused link types. The types are:
  1182. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1183. angle Links in angular brackets that may contain whitespace like
  1184. <bbdb:Carsten Dominik>.
  1185. plain Plain links in normal text, no whitespace, like http://google.com.
  1186. radio Text that is matched by a radio target, see manual for details.
  1187. tag Tag settings in a headline (link to tag search).
  1188. date Time stamps (link to calendar).
  1189. footnote Footnote labels.
  1190. Changing this variable requires a restart of Emacs to become effective."
  1191. :group 'org-link
  1192. :type '(set :greedy t
  1193. (const :tag "Double bracket links" bracket)
  1194. (const :tag "Angular bracket links" angle)
  1195. (const :tag "Plain text links" plain)
  1196. (const :tag "Radio target matches" radio)
  1197. (const :tag "Tags" tag)
  1198. (const :tag "Timestamps" date)
  1199. (const :tag "Footnotes" footnote)))
  1200. (defcustom org-make-link-description-function nil
  1201. "Function to use to generate link descriptions from links.
  1202. If nil the link location will be used. This function must take
  1203. two parameters; the first is the link and the second the
  1204. description `org-insert-link' has generated, and should return the
  1205. description to use."
  1206. :group 'org-link
  1207. :type 'function)
  1208. (defgroup org-link-store nil
  1209. "Options concerning storing links in Org-mode."
  1210. :tag "Org Store Link"
  1211. :group 'org-link)
  1212. (defcustom org-email-link-description-format "Email %c: %.30s"
  1213. "Format of the description part of a link to an email or usenet message.
  1214. The following %-escapes will be replaced by corresponding information:
  1215. %F full \"From\" field
  1216. %f name, taken from \"From\" field, address if no name
  1217. %T full \"To\" field
  1218. %t first name in \"To\" field, address if no name
  1219. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1220. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1221. %s subject
  1222. %d date
  1223. %m message-id.
  1224. You may use normal field width specification between the % and the letter.
  1225. This is for example useful to limit the length of the subject.
  1226. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1227. :group 'org-link-store
  1228. :type 'string)
  1229. (defcustom org-from-is-user-regexp
  1230. (let (r1 r2)
  1231. (when (and user-mail-address (not (string= user-mail-address "")))
  1232. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1233. (when (and user-full-name (not (string= user-full-name "")))
  1234. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1235. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1236. "Regexp matched against the \"From:\" header of an email or usenet message.
  1237. It should match if the message is from the user him/herself."
  1238. :group 'org-link-store
  1239. :type 'regexp)
  1240. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1241. "Non-nil means storing a link to an Org file will use entry IDs.
  1242. Note that before this variable is even considered, org-id must be loaded,
  1243. so please customize `org-modules' and turn it on.
  1244. The variable can have the following values:
  1245. t Create an ID if needed to make a link to the current entry.
  1246. create-if-interactive
  1247. If `org-store-link' is called directly (interactively, as a user
  1248. command), do create an ID to support the link. But when doing the
  1249. job for remember, only use the ID if it already exists. The
  1250. purpose of this setting is to avoid proliferation of unwanted
  1251. IDs, just because you happen to be in an Org file when you
  1252. call `org-remember' that automatically and preemptively
  1253. creates a link. If you do want to get an ID link in a remember
  1254. template to an entry not having an ID, create it first by
  1255. explicitly creating a link to it, using `C-c C-l' first.
  1256. create-if-interactive-and-no-custom-id
  1257. Like create-if-interactive, but do not create an ID if there is
  1258. a CUSTOM_ID property defined in the entry. This is the default.
  1259. use-existing
  1260. Use existing ID, do not create one.
  1261. nil Never use an ID to make a link, instead link using a text search for
  1262. the headline text."
  1263. :group 'org-link-store
  1264. :type '(choice
  1265. (const :tag "Create ID to make link" t)
  1266. (const :tag "Create if storing link interactively"
  1267. create-if-interactive)
  1268. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1269. create-if-interactive-and-no-custom-id)
  1270. (const :tag "Only use existing" use-existing)
  1271. (const :tag "Do not use ID to create link" nil)))
  1272. (defcustom org-context-in-file-links t
  1273. "Non-nil means file links from `org-store-link' contain context.
  1274. A search string will be added to the file name with :: as separator and
  1275. used to find the context when the link is activated by the command
  1276. `org-open-at-point'. When this option is t, the entire active region
  1277. will be placed in the search string of the file link. If set to a
  1278. positive integer, only the first n lines of context will be stored.
  1279. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1280. negates this setting for the duration of the command."
  1281. :group 'org-link-store
  1282. :type '(choice boolean integer))
  1283. (defcustom org-keep-stored-link-after-insertion nil
  1284. "Non-nil means keep link in list for entire session.
  1285. The command `org-store-link' adds a link pointing to the current
  1286. location to an internal list. These links accumulate during a session.
  1287. The command `org-insert-link' can be used to insert links into any
  1288. Org-mode file (offering completion for all stored links). When this
  1289. option is nil, every link which has been inserted once using \\[org-insert-link]
  1290. will be removed from the list, to make completing the unused links
  1291. more efficient."
  1292. :group 'org-link-store
  1293. :type 'boolean)
  1294. (defgroup org-link-follow nil
  1295. "Options concerning following links in Org-mode."
  1296. :tag "Org Follow Link"
  1297. :group 'org-link)
  1298. (defcustom org-link-translation-function nil
  1299. "Function to translate links with different syntax to Org syntax.
  1300. This can be used to translate links created for example by the Planner
  1301. or emacs-wiki packages to Org syntax.
  1302. The function must accept two parameters, a TYPE containing the link
  1303. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1304. which is everything after the link protocol. It should return a cons
  1305. with possibly modified values of type and path.
  1306. Org contains a function for this, so if you set this variable to
  1307. `org-translate-link-from-planner', you should be able follow many
  1308. links created by planner."
  1309. :group 'org-link-follow
  1310. :type 'function)
  1311. (defcustom org-follow-link-hook nil
  1312. "Hook that is run after a link has been followed."
  1313. :group 'org-link-follow
  1314. :type 'hook)
  1315. (defcustom org-tab-follows-link nil
  1316. "Non-nil means on links TAB will follow the link.
  1317. Needs to be set before org.el is loaded.
  1318. This really should not be used, it does not make sense, and the
  1319. implementation is bad."
  1320. :group 'org-link-follow
  1321. :type 'boolean)
  1322. (defcustom org-return-follows-link nil
  1323. "Non-nil means on links RET will follow the link."
  1324. :group 'org-link-follow
  1325. :type 'boolean)
  1326. (defcustom org-mouse-1-follows-link
  1327. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1328. "Non-nil means mouse-1 on a link will follow the link.
  1329. A longer mouse click will still set point. Does not work on XEmacs.
  1330. Needs to be set before org.el is loaded."
  1331. :group 'org-link-follow
  1332. :type 'boolean)
  1333. (defcustom org-mark-ring-length 4
  1334. "Number of different positions to be recorded in the ring.
  1335. Changing this requires a restart of Emacs to work correctly."
  1336. :group 'org-link-follow
  1337. :type 'integer)
  1338. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1339. "Non-nil means internal links in Org files must exactly match a headline.
  1340. When nil, the link search tries to match a phrase with all words
  1341. in the search text."
  1342. :group 'org-link-follow
  1343. :type '(choice
  1344. (const :tag "Use fuzzy text search" nil)
  1345. (const :tag "Match only exact headline" t)
  1346. (const :tag "Match exact headline or query to create it"
  1347. query-to-create)))
  1348. (defcustom org-link-frame-setup
  1349. '((vm . vm-visit-folder-other-frame)
  1350. (gnus . org-gnus-no-new-news)
  1351. (file . find-file-other-window)
  1352. (wl . wl-other-frame))
  1353. "Setup the frame configuration for following links.
  1354. When following a link with Emacs, it may often be useful to display
  1355. this link in another window or frame. This variable can be used to
  1356. set this up for the different types of links.
  1357. For VM, use any of
  1358. `vm-visit-folder'
  1359. `vm-visit-folder-other-window'
  1360. `vm-visit-folder-other-frame'
  1361. For Gnus, use any of
  1362. `gnus'
  1363. `gnus-other-frame'
  1364. `org-gnus-no-new-news'
  1365. For FILE, use any of
  1366. `find-file'
  1367. `find-file-other-window'
  1368. `find-file-other-frame'
  1369. For Wanderlust use any of
  1370. `wl'
  1371. `wl-other-frame'
  1372. For the calendar, use the variable `calendar-setup'.
  1373. For BBDB, it is currently only possible to display the matches in
  1374. another window."
  1375. :group 'org-link-follow
  1376. :type '(list
  1377. (cons (const vm)
  1378. (choice
  1379. (const vm-visit-folder)
  1380. (const vm-visit-folder-other-window)
  1381. (const vm-visit-folder-other-frame)))
  1382. (cons (const gnus)
  1383. (choice
  1384. (const gnus)
  1385. (const gnus-other-frame)
  1386. (const org-gnus-no-new-news)))
  1387. (cons (const file)
  1388. (choice
  1389. (const find-file)
  1390. (const find-file-other-window)
  1391. (const find-file-other-frame)))
  1392. (cons (const wl)
  1393. (choice
  1394. (const wl)
  1395. (const wl-other-frame)))))
  1396. (defcustom org-display-internal-link-with-indirect-buffer nil
  1397. "Non-nil means use indirect buffer to display infile links.
  1398. Activating internal links (from one location in a file to another location
  1399. in the same file) normally just jumps to the location. When the link is
  1400. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1401. is displayed in
  1402. another window. When this option is set, the other window actually displays
  1403. an indirect buffer clone of the current buffer, to avoid any visibility
  1404. changes to the current buffer."
  1405. :group 'org-link-follow
  1406. :type 'boolean)
  1407. (defcustom org-open-non-existing-files nil
  1408. "Non-nil means `org-open-file' will open non-existing files.
  1409. When nil, an error will be generated.
  1410. This variable applies only to external applications because they
  1411. might choke on non-existing files. If the link is to a file that
  1412. will be opened in Emacs, the variable is ignored."
  1413. :group 'org-link-follow
  1414. :type 'boolean)
  1415. (defcustom org-open-directory-means-index-dot-org nil
  1416. "Non-nil means a link to a directory really means to index.org.
  1417. When nil, following a directory link will run dired or open a finder/explorer
  1418. window on that directory."
  1419. :group 'org-link-follow
  1420. :type 'boolean)
  1421. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1422. "Function and arguments to call for following mailto links.
  1423. This is a list with the first element being a Lisp function, and the
  1424. remaining elements being arguments to the function. In string arguments,
  1425. %a will be replaced by the address, and %s will be replaced by the subject
  1426. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1427. :group 'org-link-follow
  1428. :type '(choice
  1429. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1430. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1431. (const :tag "message-mail" (message-mail "%a" "%s"))
  1432. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1433. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1434. "Non-nil means ask for confirmation before executing shell links.
  1435. Shell links can be dangerous: just think about a link
  1436. [[shell:rm -rf ~/*][Google Search]]
  1437. This link would show up in your Org-mode document as \"Google Search\",
  1438. but really it would remove your entire home directory.
  1439. Therefore we advise against setting this variable to nil.
  1440. Just change it to `y-or-n-p' if you want to confirm with a
  1441. single keystroke rather than having to type \"yes\"."
  1442. :group 'org-link-follow
  1443. :type '(choice
  1444. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1445. (const :tag "with y-or-n (faster)" y-or-n-p)
  1446. (const :tag "no confirmation (dangerous)" nil)))
  1447. (put 'org-confirm-shell-link-function
  1448. 'safe-local-variable
  1449. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1450. (defcustom org-confirm-shell-link-not-regexp ""
  1451. "A regexp to skip confirmation for shell links."
  1452. :group 'org-link-follow
  1453. :type 'regexp)
  1454. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1455. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1456. Elisp links can be dangerous: just think about a link
  1457. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1458. This link would show up in your Org-mode document as \"Google Search\",
  1459. but really it would remove your entire home directory.
  1460. Therefore we advise against setting this variable to nil.
  1461. Just change it to `y-or-n-p' if you want to confirm with a
  1462. single keystroke rather than having to type \"yes\"."
  1463. :group 'org-link-follow
  1464. :type '(choice
  1465. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1466. (const :tag "with y-or-n (faster)" y-or-n-p)
  1467. (const :tag "no confirmation (dangerous)" nil)))
  1468. (put 'org-confirm-shell-link-function
  1469. 'safe-local-variable
  1470. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1471. (defcustom org-confirm-elisp-link-not-regexp ""
  1472. "A regexp to skip confirmation for Elisp links."
  1473. :group 'org-link-follow
  1474. :type 'regexp)
  1475. (defconst org-file-apps-defaults-gnu
  1476. '((remote . emacs)
  1477. (system . mailcap)
  1478. (t . mailcap))
  1479. "Default file applications on a UNIX or GNU/Linux system.
  1480. See `org-file-apps'.")
  1481. (defconst org-file-apps-defaults-macosx
  1482. '((remote . emacs)
  1483. (t . "open %s")
  1484. (system . "open %s")
  1485. ("ps.gz" . "gv %s")
  1486. ("eps.gz" . "gv %s")
  1487. ("dvi" . "xdvi %s")
  1488. ("fig" . "xfig %s"))
  1489. "Default file applications on a MacOS X system.
  1490. The system \"open\" is known as a default, but we use X11 applications
  1491. for some files for which the OS does not have a good default.
  1492. See `org-file-apps'.")
  1493. (defconst org-file-apps-defaults-windowsnt
  1494. (list
  1495. '(remote . emacs)
  1496. (cons t
  1497. (list (if (featurep 'xemacs)
  1498. 'mswindows-shell-execute
  1499. 'w32-shell-execute)
  1500. "open" 'file))
  1501. (cons 'system
  1502. (list (if (featurep 'xemacs)
  1503. 'mswindows-shell-execute
  1504. 'w32-shell-execute)
  1505. "open" 'file)))
  1506. "Default file applications on a Windows NT system.
  1507. The system \"open\" is used for most files.
  1508. See `org-file-apps'.")
  1509. (defcustom org-file-apps
  1510. '(
  1511. (auto-mode . emacs)
  1512. ("\\.mm\\'" . default)
  1513. ("\\.x?html?\\'" . default)
  1514. ("\\.pdf\\'" . default)
  1515. )
  1516. "External applications for opening `file:path' items in a document.
  1517. Org-mode uses system defaults for different file types, but
  1518. you can use this variable to set the application for a given file
  1519. extension. The entries in this list are cons cells where the car identifies
  1520. files and the cdr the corresponding command. Possible values for the
  1521. file identifier are
  1522. \"string\" A string as a file identifier can be interpreted in different
  1523. ways, depending on its contents:
  1524. - Alphanumeric characters only:
  1525. Match links with this file extension.
  1526. Example: (\"pdf\" . \"evince %s\")
  1527. to open PDFs with evince.
  1528. - Regular expression: Match links where the
  1529. filename matches the regexp. If you want to
  1530. use groups here, use shy groups.
  1531. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1532. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1533. to open *.html and *.xhtml with firefox.
  1534. - Regular expression which contains (non-shy) groups:
  1535. Match links where the whole link, including \"::\", and
  1536. anything after that, matches the regexp.
  1537. In a custom command string, %1, %2, etc. are replaced with
  1538. the parts of the link that were matched by the groups.
  1539. For backwards compatibility, if a command string is given
  1540. that does not use any of the group matches, this case is
  1541. handled identically to the second one (i.e. match against
  1542. file name only).
  1543. In a custom lisp form, you can access the group matches with
  1544. (match-string n link).
  1545. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1546. to open [[file:document.pdf::5]] with evince at page 5.
  1547. `directory' Matches a directory
  1548. `remote' Matches a remote file, accessible through tramp or efs.
  1549. Remote files most likely should be visited through Emacs
  1550. because external applications cannot handle such paths.
  1551. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1552. so all files Emacs knows how to handle. Using this with
  1553. command `emacs' will open most files in Emacs. Beware that this
  1554. will also open html files inside Emacs, unless you add
  1555. (\"html\" . default) to the list as well.
  1556. t Default for files not matched by any of the other options.
  1557. `system' The system command to open files, like `open' on Windows
  1558. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1559. that will be selected if you call `C-c C-o' with a double
  1560. \\[universal-argument] \\[universal-argument] prefix.
  1561. Possible values for the command are:
  1562. `emacs' The file will be visited by the current Emacs process.
  1563. `default' Use the default application for this file type, which is the
  1564. association for t in the list, most likely in the system-specific
  1565. part.
  1566. This can be used to overrule an unwanted setting in the
  1567. system-specific variable.
  1568. `system' Use the system command for opening files, like \"open\".
  1569. This command is specified by the entry whose car is `system'.
  1570. Most likely, the system-specific version of this variable
  1571. does define this command, but you can overrule/replace it
  1572. here.
  1573. string A command to be executed by a shell; %s will be replaced
  1574. by the path to the file.
  1575. sexp A Lisp form which will be evaluated. The file path will
  1576. be available in the Lisp variable `file'.
  1577. For more examples, see the system specific constants
  1578. `org-file-apps-defaults-macosx'
  1579. `org-file-apps-defaults-windowsnt'
  1580. `org-file-apps-defaults-gnu'."
  1581. :group 'org-link-follow
  1582. :type '(repeat
  1583. (cons (choice :value ""
  1584. (string :tag "Extension")
  1585. (const :tag "System command to open files" system)
  1586. (const :tag "Default for unrecognized files" t)
  1587. (const :tag "Remote file" remote)
  1588. (const :tag "Links to a directory" directory)
  1589. (const :tag "Any files that have Emacs modes"
  1590. auto-mode))
  1591. (choice :value ""
  1592. (const :tag "Visit with Emacs" emacs)
  1593. (const :tag "Use default" default)
  1594. (const :tag "Use the system command" system)
  1595. (string :tag "Command")
  1596. (sexp :tag "Lisp form")))))
  1597. (defgroup org-refile nil
  1598. "Options concerning refiling entries in Org-mode."
  1599. :tag "Org Refile"
  1600. :group 'org)
  1601. (defcustom org-directory "~/org"
  1602. "Directory with org files.
  1603. This is just a default location to look for Org files. There is no need
  1604. at all to put your files into this directory. It is only used in the
  1605. following situations:
  1606. 1. When a remember template specifies a target file that is not an
  1607. absolute path. The path will then be interpreted relative to
  1608. `org-directory'
  1609. 2. When a remember note is filed away in an interactive way (when exiting the
  1610. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1611. with `org-directory' as the default path."
  1612. :group 'org-refile
  1613. :group 'org-remember
  1614. :type 'directory)
  1615. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1616. "Default target for storing notes.
  1617. Used as a fall back file for org-remember.el and org-capture.el, for
  1618. templates that do not specify a target file."
  1619. :group 'org-refile
  1620. :group 'org-remember
  1621. :type '(choice
  1622. (const :tag "Default from remember-data-file" nil)
  1623. file))
  1624. (defcustom org-goto-interface 'outline
  1625. "The default interface to be used for `org-goto'.
  1626. Allowed values are:
  1627. outline The interface shows an outline of the relevant file
  1628. and the correct heading is found by moving through
  1629. the outline or by searching with incremental search.
  1630. outline-path-completion Headlines in the current buffer are offered via
  1631. completion. This is the interface also used by
  1632. the refile command."
  1633. :group 'org-refile
  1634. :type '(choice
  1635. (const :tag "Outline" outline)
  1636. (const :tag "Outline-path-completion" outline-path-completion)))
  1637. (defcustom org-goto-max-level 5
  1638. "Maximum target level when running `org-goto' with refile interface."
  1639. :group 'org-refile
  1640. :type 'integer)
  1641. (defcustom org-reverse-note-order nil
  1642. "Non-nil means store new notes at the beginning of a file or entry.
  1643. When nil, new notes will be filed to the end of a file or entry.
  1644. This can also be a list with cons cells of regular expressions that
  1645. are matched against file names, and values."
  1646. :group 'org-remember
  1647. :group 'org-refile
  1648. :type '(choice
  1649. (const :tag "Reverse always" t)
  1650. (const :tag "Reverse never" nil)
  1651. (repeat :tag "By file name regexp"
  1652. (cons regexp boolean))))
  1653. (defcustom org-log-refile nil
  1654. "Information to record when a task is refiled.
  1655. Possible values are:
  1656. nil Don't add anything
  1657. time Add a time stamp to the task
  1658. note Prompt for a note and add it with template `org-log-note-headings'
  1659. This option can also be set with on a per-file-basis with
  1660. #+STARTUP: nologrefile
  1661. #+STARTUP: logrefile
  1662. #+STARTUP: lognoterefile
  1663. You can have local logging settings for a subtree by setting the LOGGING
  1664. property to one or more of these keywords.
  1665. When bulk-refiling from the agenda, the value `note' is forbidden and
  1666. will temporarily be changed to `time'."
  1667. :group 'org-refile
  1668. :group 'org-progress
  1669. :type '(choice
  1670. (const :tag "No logging" nil)
  1671. (const :tag "Record timestamp" time)
  1672. (const :tag "Record timestamp with note." note)))
  1673. (defcustom org-refile-targets nil
  1674. "Targets for refiling entries with \\[org-refile].
  1675. This is a list of cons cells. Each cell contains:
  1676. - a specification of the files to be considered, either a list of files,
  1677. or a symbol whose function or variable value will be used to retrieve
  1678. a file name or a list of file names. If you use `org-agenda-files' for
  1679. that, all agenda files will be scanned for targets. Nil means consider
  1680. headings in the current buffer.
  1681. - A specification of how to find candidate refile targets. This may be
  1682. any of:
  1683. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1684. This tag has to be present in all target headlines, inheritance will
  1685. not be considered.
  1686. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1687. todo keyword.
  1688. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1689. headlines that are refiling targets.
  1690. - a cons cell (:level . N). Any headline of level N is considered a target.
  1691. Note that, when `org-odd-levels-only' is set, level corresponds to
  1692. order in hierarchy, not to the number of stars.
  1693. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1694. Note that, when `org-odd-levels-only' is set, level corresponds to
  1695. order in hierarchy, not to the number of stars.
  1696. Each element of this list generates a set of possible targets.
  1697. The union of these sets is presented (with completion) to
  1698. the user by `org-refile'.
  1699. You can set the variable `org-refile-target-verify-function' to a function
  1700. to verify each headline found by the simple criteria above.
  1701. When this variable is nil, all top-level headlines in the current buffer
  1702. are used, equivalent to the value `((nil . (:level . 1))'."
  1703. :group 'org-refile
  1704. :type '(repeat
  1705. (cons
  1706. (choice :value org-agenda-files
  1707. (const :tag "All agenda files" org-agenda-files)
  1708. (const :tag "Current buffer" nil)
  1709. (function) (variable) (file))
  1710. (choice :tag "Identify target headline by"
  1711. (cons :tag "Specific tag" (const :value :tag) (string))
  1712. (cons :tag "TODO keyword" (const :value :todo) (string))
  1713. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1714. (cons :tag "Level number" (const :value :level) (integer))
  1715. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1716. (defcustom org-refile-target-verify-function nil
  1717. "Function to verify if the headline at point should be a refile target.
  1718. The function will be called without arguments, with point at the
  1719. beginning of the headline. It should return t and leave point
  1720. where it is if the headline is a valid target for refiling.
  1721. If the target should not be selected, the function must return nil.
  1722. In addition to this, it may move point to a place from where the search
  1723. should be continued. For example, the function may decide that the entire
  1724. subtree of the current entry should be excluded and move point to the end
  1725. of the subtree."
  1726. :group 'org-refile
  1727. :type 'function)
  1728. (defcustom org-refile-use-cache nil
  1729. "Non-nil means cache refile targets to speed up the process.
  1730. The cache for a particular file will be updated automatically when
  1731. the buffer has been killed, or when any of the marker used for flagging
  1732. refile targets no longer points at a live buffer.
  1733. If you have added new entries to a buffer that might themselves be targets,
  1734. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1735. find that easier, `C-u C-u C-u C-c C-w'."
  1736. :group 'org-refile
  1737. :type 'boolean)
  1738. (defcustom org-refile-use-outline-path nil
  1739. "Non-nil means provide refile targets as paths.
  1740. So a level 3 headline will be available as level1/level2/level3.
  1741. When the value is `file', also include the file name (without directory)
  1742. into the path. In this case, you can also stop the completion after
  1743. the file name, to get entries inserted as top level in the file.
  1744. When `full-file-path', include the full file path."
  1745. :group 'org-refile
  1746. :type '(choice
  1747. (const :tag "Not" nil)
  1748. (const :tag "Yes" t)
  1749. (const :tag "Start with file name" file)
  1750. (const :tag "Start with full file path" full-file-path)))
  1751. (defcustom org-outline-path-complete-in-steps t
  1752. "Non-nil means complete the outline path in hierarchical steps.
  1753. When Org-mode uses the refile interface to select an outline path
  1754. \(see variable `org-refile-use-outline-path'), the completion of
  1755. the path can be done is a single go, or if can be done in steps down
  1756. the headline hierarchy. Going in steps is probably the best if you
  1757. do not use a special completion package like `ido' or `icicles'.
  1758. However, when using these packages, going in one step can be very
  1759. fast, while still showing the whole path to the entry."
  1760. :group 'org-refile
  1761. :type 'boolean)
  1762. (defcustom org-refile-allow-creating-parent-nodes nil
  1763. "Non-nil means allow to create new nodes as refile targets.
  1764. New nodes are then created by adding \"/new node name\" to the completion
  1765. of an existing node. When the value of this variable is `confirm',
  1766. new node creation must be confirmed by the user (recommended)
  1767. When nil, the completion must match an existing entry.
  1768. Note that, if the new heading is not seen by the criteria
  1769. listed in `org-refile-targets', multiple instances of the same
  1770. heading would be created by trying again to file under the new
  1771. heading."
  1772. :group 'org-refile
  1773. :type '(choice
  1774. (const :tag "Never" nil)
  1775. (const :tag "Always" t)
  1776. (const :tag "Prompt for confirmation" confirm)))
  1777. (defcustom org-refile-active-region-within-subtree nil
  1778. "Non-nil means also refile active region within a subtree.
  1779. By default `org-refile' doesn't allow refiling regions if they
  1780. don't contain a set of subtrees, but it might be convenient to
  1781. do so sometimes: in that case, the first line of the region is
  1782. converted to a headline before refiling."
  1783. :group 'org-refile
  1784. :type 'boolean)
  1785. (defgroup org-todo nil
  1786. "Options concerning TODO items in Org-mode."
  1787. :tag "Org TODO"
  1788. :group 'org)
  1789. (defgroup org-progress nil
  1790. "Options concerning Progress logging in Org-mode."
  1791. :tag "Org Progress"
  1792. :group 'org-time)
  1793. (defvar org-todo-interpretation-widgets
  1794. '((:tag "Sequence (cycling hits every state)" sequence)
  1795. (:tag "Type (cycling directly to DONE)" type))
  1796. "The available interpretation symbols for customizing `org-todo-keywords'.
  1797. Interested libraries should add to this list.")
  1798. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1799. "List of TODO entry keyword sequences and their interpretation.
  1800. \\<org-mode-map>This is a list of sequences.
  1801. Each sequence starts with a symbol, either `sequence' or `type',
  1802. indicating if the keywords should be interpreted as a sequence of
  1803. action steps, or as different types of TODO items. The first
  1804. keywords are states requiring action - these states will select a headline
  1805. for inclusion into the global TODO list Org-mode produces. If one of
  1806. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1807. signify that no further action is necessary. If \"|\" is not found,
  1808. the last keyword is treated as the only DONE state of the sequence.
  1809. The command \\[org-todo] cycles an entry through these states, and one
  1810. additional state where no keyword is present. For details about this
  1811. cycling, see the manual.
  1812. TODO keywords and interpretation can also be set on a per-file basis with
  1813. the special #+SEQ_TODO and #+TYP_TODO lines.
  1814. Each keyword can optionally specify a character for fast state selection
  1815. \(in combination with the variable `org-use-fast-todo-selection')
  1816. and specifiers for state change logging, using the same syntax
  1817. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1818. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1819. indicates to record a time stamp each time this state is selected.
  1820. Each keyword may also specify if a timestamp or a note should be
  1821. recorded when entering or leaving the state, by adding additional
  1822. characters in the parenthesis after the keyword. This looks like this:
  1823. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1824. record only the time of the state change. With X and Y being either
  1825. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1826. Y when leaving the state if and only if the *target* state does not
  1827. define X. You may omit any of the fast-selection key or X or /Y,
  1828. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1829. For backward compatibility, this variable may also be just a list
  1830. of keywords - in this case the interpretation (sequence or type) will be
  1831. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1832. :group 'org-todo
  1833. :group 'org-keywords
  1834. :type '(choice
  1835. (repeat :tag "Old syntax, just keywords"
  1836. (string :tag "Keyword"))
  1837. (repeat :tag "New syntax"
  1838. (cons
  1839. (choice
  1840. :tag "Interpretation"
  1841. ;;Quick and dirty way to see
  1842. ;;`org-todo-interpretations'. This takes the
  1843. ;;place of item arguments
  1844. :convert-widget
  1845. (lambda (widget)
  1846. (widget-put widget
  1847. :args (mapcar
  1848. #'(lambda (x)
  1849. (widget-convert
  1850. (cons 'const x)))
  1851. org-todo-interpretation-widgets))
  1852. widget))
  1853. (repeat
  1854. (string :tag "Keyword"))))))
  1855. (defvar org-todo-keywords-1 nil
  1856. "All TODO and DONE keywords active in a buffer.")
  1857. (make-variable-buffer-local 'org-todo-keywords-1)
  1858. (defvar org-todo-keywords-for-agenda nil)
  1859. (defvar org-done-keywords-for-agenda nil)
  1860. (defvar org-drawers-for-agenda nil)
  1861. (defvar org-todo-keyword-alist-for-agenda nil)
  1862. (defvar org-tag-alist-for-agenda nil)
  1863. (defvar org-agenda-contributing-files nil)
  1864. (defvar org-not-done-keywords nil)
  1865. (make-variable-buffer-local 'org-not-done-keywords)
  1866. (defvar org-done-keywords nil)
  1867. (make-variable-buffer-local 'org-done-keywords)
  1868. (defvar org-todo-heads nil)
  1869. (make-variable-buffer-local 'org-todo-heads)
  1870. (defvar org-todo-sets nil)
  1871. (make-variable-buffer-local 'org-todo-sets)
  1872. (defvar org-todo-log-states nil)
  1873. (make-variable-buffer-local 'org-todo-log-states)
  1874. (defvar org-todo-kwd-alist nil)
  1875. (make-variable-buffer-local 'org-todo-kwd-alist)
  1876. (defvar org-todo-key-alist nil)
  1877. (make-variable-buffer-local 'org-todo-key-alist)
  1878. (defvar org-todo-key-trigger nil)
  1879. (make-variable-buffer-local 'org-todo-key-trigger)
  1880. (defcustom org-todo-interpretation 'sequence
  1881. "Controls how TODO keywords are interpreted.
  1882. This variable is in principle obsolete and is only used for
  1883. backward compatibility, if the interpretation of todo keywords is
  1884. not given already in `org-todo-keywords'. See that variable for
  1885. more information."
  1886. :group 'org-todo
  1887. :group 'org-keywords
  1888. :type '(choice (const sequence)
  1889. (const type)))
  1890. (defcustom org-use-fast-todo-selection t
  1891. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1892. This variable describes if and under what circumstances the cycling
  1893. mechanism for TODO keywords will be replaced by a single-key, direct
  1894. selection scheme.
  1895. When nil, fast selection is never used.
  1896. When the symbol `prefix', it will be used when `org-todo' is called with
  1897. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1898. in an agenda buffer.
  1899. When t, fast selection is used by default. In this case, the prefix
  1900. argument forces cycling instead.
  1901. In all cases, the special interface is only used if access keys have actually
  1902. been assigned by the user, i.e. if keywords in the configuration are followed
  1903. by a letter in parenthesis, like TODO(t)."
  1904. :group 'org-todo
  1905. :type '(choice
  1906. (const :tag "Never" nil)
  1907. (const :tag "By default" t)
  1908. (const :tag "Only with C-u C-c C-t" prefix)))
  1909. (defcustom org-provide-todo-statistics t
  1910. "Non-nil means update todo statistics after insert and toggle.
  1911. ALL-HEADLINES means update todo statistics by including headlines
  1912. with no TODO keyword as well, counting them as not done.
  1913. A list of TODO keywords means the same, but skip keywords that are
  1914. not in this list.
  1915. When this is set, todo statistics is updated in the parent of the
  1916. current entry each time a todo state is changed."
  1917. :group 'org-todo
  1918. :type '(choice
  1919. (const :tag "Yes, only for TODO entries" t)
  1920. (const :tag "Yes, including all entries" 'all-headlines)
  1921. (repeat :tag "Yes, for TODOs in this list"
  1922. (string :tag "TODO keyword"))
  1923. (other :tag "No TODO statistics" nil)))
  1924. (defcustom org-hierarchical-todo-statistics t
  1925. "Non-nil means TODO statistics covers just direct children.
  1926. When nil, all entries in the subtree are considered.
  1927. This has only an effect if `org-provide-todo-statistics' is set.
  1928. To set this to nil for only a single subtree, use a COOKIE_DATA
  1929. property and include the word \"recursive\" into the value."
  1930. :group 'org-todo
  1931. :type 'boolean)
  1932. (defcustom org-after-todo-state-change-hook nil
  1933. "Hook which is run after the state of a TODO item was changed.
  1934. The new state (a string with a TODO keyword, or nil) is available in the
  1935. Lisp variable `state'."
  1936. :group 'org-todo
  1937. :type 'hook)
  1938. (defvar org-blocker-hook nil
  1939. "Hook for functions that are allowed to block a state change.
  1940. Each function gets as its single argument a property list, see
  1941. `org-trigger-hook' for more information about this list.
  1942. If any of the functions in this hook returns nil, the state change
  1943. is blocked.")
  1944. (defvar org-trigger-hook nil
  1945. "Hook for functions that are triggered by a state change.
  1946. Each function gets as its single argument a property list with at least
  1947. the following elements:
  1948. (:type type-of-change :position pos-at-entry-start
  1949. :from old-state :to new-state)
  1950. Depending on the type, more properties may be present.
  1951. This mechanism is currently implemented for:
  1952. TODO state changes
  1953. ------------------
  1954. :type todo-state-change
  1955. :from previous state (keyword as a string), or nil, or a symbol
  1956. 'todo' or 'done', to indicate the general type of state.
  1957. :to new state, like in :from")
  1958. (defcustom org-enforce-todo-dependencies nil
  1959. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1960. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1961. be blocked if any prior sibling is not yet done.
  1962. Finally, if the parent is blocked because of ordered siblings of its own,
  1963. the child will also be blocked.
  1964. This variable needs to be set before org.el is loaded, and you need to
  1965. restart Emacs after a change to make the change effective. The only way
  1966. to change is while Emacs is running is through the customize interface."
  1967. :set (lambda (var val)
  1968. (set var val)
  1969. (if val
  1970. (add-hook 'org-blocker-hook
  1971. 'org-block-todo-from-children-or-siblings-or-parent)
  1972. (remove-hook 'org-blocker-hook
  1973. 'org-block-todo-from-children-or-siblings-or-parent)))
  1974. :group 'org-todo
  1975. :type 'boolean)
  1976. (defcustom org-enforce-todo-checkbox-dependencies nil
  1977. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1978. When this is nil, checkboxes have no influence on switching TODO states.
  1979. When non-nil, you first need to check off all check boxes before the TODO
  1980. entry can be switched to DONE.
  1981. This variable needs to be set before org.el is loaded, and you need to
  1982. restart Emacs after a change to make the change effective. The only way
  1983. to change is while Emacs is running is through the customize interface."
  1984. :set (lambda (var val)
  1985. (set var val)
  1986. (if val
  1987. (add-hook 'org-blocker-hook
  1988. 'org-block-todo-from-checkboxes)
  1989. (remove-hook 'org-blocker-hook
  1990. 'org-block-todo-from-checkboxes)))
  1991. :group 'org-todo
  1992. :type 'boolean)
  1993. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1994. "Non-nil means inserting a TODO heading is treated as state change.
  1995. So when the command \\[org-insert-todo-heading] is used, state change
  1996. logging will apply if appropriate. When nil, the new TODO item will
  1997. be inserted directly, and no logging will take place."
  1998. :group 'org-todo
  1999. :type 'boolean)
  2000. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2001. "Non-nil means switching TODO states with S-cursor counts as state change.
  2002. This is the default behavior. However, setting this to nil allows a
  2003. convenient way to select a TODO state and bypass any logging associated
  2004. with that."
  2005. :group 'org-todo
  2006. :type 'boolean)
  2007. (defcustom org-todo-state-tags-triggers nil
  2008. "Tag changes that should be triggered by TODO state changes.
  2009. This is a list. Each entry is
  2010. (state-change (tag . flag) .......)
  2011. State-change can be a string with a state, and empty string to indicate the
  2012. state that has no TODO keyword, or it can be one of the symbols `todo'
  2013. or `done', meaning any not-done or done state, respectively."
  2014. :group 'org-todo
  2015. :group 'org-tags
  2016. :type '(repeat
  2017. (cons (choice :tag "When changing to"
  2018. (const :tag "Not-done state" todo)
  2019. (const :tag "Done state" done)
  2020. (string :tag "State"))
  2021. (repeat
  2022. (cons :tag "Tag action"
  2023. (string :tag "Tag")
  2024. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2025. (defcustom org-log-done nil
  2026. "Information to record when a task moves to the DONE state.
  2027. Possible values are:
  2028. nil Don't add anything, just change the keyword
  2029. time Add a time stamp to the task
  2030. note Prompt for a note and add it with template `org-log-note-headings'
  2031. This option can also be set with on a per-file-basis with
  2032. #+STARTUP: nologdone
  2033. #+STARTUP: logdone
  2034. #+STARTUP: lognotedone
  2035. You can have local logging settings for a subtree by setting the LOGGING
  2036. property to one or more of these keywords."
  2037. :group 'org-todo
  2038. :group 'org-progress
  2039. :type '(choice
  2040. (const :tag "No logging" nil)
  2041. (const :tag "Record CLOSED timestamp" time)
  2042. (const :tag "Record CLOSED timestamp with note." note)))
  2043. ;; Normalize old uses of org-log-done.
  2044. (cond
  2045. ((eq org-log-done t) (setq org-log-done 'time))
  2046. ((and (listp org-log-done) (memq 'done org-log-done))
  2047. (setq org-log-done 'note)))
  2048. (defcustom org-log-reschedule nil
  2049. "Information to record when the scheduling date of a tasks is modified.
  2050. Possible values are:
  2051. nil Don't add anything, just change the date
  2052. time Add a time stamp to the task
  2053. note Prompt for a note and add it with template `org-log-note-headings'
  2054. This option can also be set with on a per-file-basis with
  2055. #+STARTUP: nologreschedule
  2056. #+STARTUP: logreschedule
  2057. #+STARTUP: lognotereschedule"
  2058. :group 'org-todo
  2059. :group 'org-progress
  2060. :type '(choice
  2061. (const :tag "No logging" nil)
  2062. (const :tag "Record timestamp" time)
  2063. (const :tag "Record timestamp with note." note)))
  2064. (defcustom org-log-redeadline nil
  2065. "Information to record when the deadline date of a tasks is modified.
  2066. Possible values are:
  2067. nil Don't add anything, just change the date
  2068. time Add a time stamp to the task
  2069. note Prompt for a note and add it with template `org-log-note-headings'
  2070. This option can also be set with on a per-file-basis with
  2071. #+STARTUP: nologredeadline
  2072. #+STARTUP: logredeadline
  2073. #+STARTUP: lognoteredeadline
  2074. You can have local logging settings for a subtree by setting the LOGGING
  2075. property to one or more of these keywords."
  2076. :group 'org-todo
  2077. :group 'org-progress
  2078. :type '(choice
  2079. (const :tag "No logging" nil)
  2080. (const :tag "Record timestamp" time)
  2081. (const :tag "Record timestamp with note." note)))
  2082. (defcustom org-log-note-clock-out nil
  2083. "Non-nil means record a note when clocking out of an item.
  2084. This can also be configured on a per-file basis by adding one of
  2085. the following lines anywhere in the buffer:
  2086. #+STARTUP: lognoteclock-out
  2087. #+STARTUP: nolognoteclock-out"
  2088. :group 'org-todo
  2089. :group 'org-progress
  2090. :type 'boolean)
  2091. (defcustom org-log-done-with-time t
  2092. "Non-nil means the CLOSED time stamp will contain date and time.
  2093. When nil, only the date will be recorded."
  2094. :group 'org-progress
  2095. :type 'boolean)
  2096. (defcustom org-log-note-headings
  2097. '((done . "CLOSING NOTE %t")
  2098. (state . "State %-12s from %-12S %t")
  2099. (note . "Note taken on %t")
  2100. (reschedule . "Rescheduled from %S on %t")
  2101. (delschedule . "Not scheduled, was %S on %t")
  2102. (redeadline . "New deadline from %S on %t")
  2103. (deldeadline . "Removed deadline, was %S on %t")
  2104. (refile . "Refiled on %t")
  2105. (clock-out . ""))
  2106. "Headings for notes added to entries.
  2107. The value is an alist, with the car being a symbol indicating the note
  2108. context, and the cdr is the heading to be used. The heading may also be the
  2109. empty string.
  2110. %t in the heading will be replaced by a time stamp.
  2111. %T will be an active time stamp instead the default inactive one
  2112. %d will be replaced by a short-format time stamp.
  2113. %D will be replaced by an active short-format time stamp.
  2114. %s will be replaced by the new TODO state, in double quotes.
  2115. %S will be replaced by the old TODO state, in double quotes.
  2116. %u will be replaced by the user name.
  2117. %U will be replaced by the full user name.
  2118. In fact, it is not a good idea to change the `state' entry, because
  2119. agenda log mode depends on the format of these entries."
  2120. :group 'org-todo
  2121. :group 'org-progress
  2122. :type '(list :greedy t
  2123. (cons (const :tag "Heading when closing an item" done) string)
  2124. (cons (const :tag
  2125. "Heading when changing todo state (todo sequence only)"
  2126. state) string)
  2127. (cons (const :tag "Heading when just taking a note" note) string)
  2128. (cons (const :tag "Heading when clocking out" clock-out) string)
  2129. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2130. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2131. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2132. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2133. (cons (const :tag "Heading when refiling" refile) string)))
  2134. (unless (assq 'note org-log-note-headings)
  2135. (push '(note . "%t") org-log-note-headings))
  2136. (defcustom org-log-into-drawer nil
  2137. "Non-nil means insert state change notes and time stamps into a drawer.
  2138. When nil, state changes notes will be inserted after the headline and
  2139. any scheduling and clock lines, but not inside a drawer.
  2140. The value of this variable should be the name of the drawer to use.
  2141. LOGBOOK is proposed as the default drawer for this purpose, you can
  2142. also set this to a string to define the drawer of your choice.
  2143. A value of t is also allowed, representing \"LOGBOOK\".
  2144. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2145. will be ignored.
  2146. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2147. a subtree."
  2148. :group 'org-todo
  2149. :group 'org-progress
  2150. :type '(choice
  2151. (const :tag "Not into a drawer" nil)
  2152. (const :tag "LOGBOOK" t)
  2153. (string :tag "Other")))
  2154. (if (fboundp 'defvaralias)
  2155. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2156. (defun org-log-into-drawer ()
  2157. "Return the value of `org-log-into-drawer', but let properties overrule.
  2158. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2159. used instead of the default value."
  2160. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2161. (cond
  2162. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2163. ((equal p "t") "LOGBOOK")
  2164. (t p))))
  2165. (defcustom org-log-state-notes-insert-after-drawers nil
  2166. "Non-nil means insert state change notes after any drawers in entry.
  2167. Only the drawers that *immediately* follow the headline and the
  2168. deadline/scheduled line are skipped.
  2169. When nil, insert notes right after the heading and perhaps the line
  2170. with deadline/scheduling if present.
  2171. This variable will have no effect if `org-log-into-drawer' is
  2172. set."
  2173. :group 'org-todo
  2174. :group 'org-progress
  2175. :type 'boolean)
  2176. (defcustom org-log-states-order-reversed t
  2177. "Non-nil means the latest state note will be directly after heading.
  2178. When nil, the state change notes will be ordered according to time."
  2179. :group 'org-todo
  2180. :group 'org-progress
  2181. :type 'boolean)
  2182. (defcustom org-todo-repeat-to-state nil
  2183. "The TODO state to which a repeater should return the repeating task.
  2184. By default this is the first task in a TODO sequence, or the previous state
  2185. in a TODO_TYP set. But you can specify another task here.
  2186. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2187. :group 'org-todo
  2188. :type '(choice (const :tag "Head of sequence" nil)
  2189. (string :tag "Specific state")))
  2190. (defcustom org-log-repeat 'time
  2191. "Non-nil means record moving through the DONE state when triggering repeat.
  2192. An auto-repeating task is immediately switched back to TODO when
  2193. marked DONE. If you are not logging state changes (by adding \"@\"
  2194. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2195. record a closing note, there will be no record of the task moving
  2196. through DONE. This variable forces taking a note anyway.
  2197. nil Don't force a record
  2198. time Record a time stamp
  2199. note Record a note
  2200. This option can also be set with on a per-file-basis with
  2201. #+STARTUP: logrepeat
  2202. #+STARTUP: lognoterepeat
  2203. #+STARTUP: nologrepeat
  2204. You can have local logging settings for a subtree by setting the LOGGING
  2205. property to one or more of these keywords."
  2206. :group 'org-todo
  2207. :group 'org-progress
  2208. :type '(choice
  2209. (const :tag "Don't force a record" nil)
  2210. (const :tag "Force recording the DONE state" time)
  2211. (const :tag "Force recording a note with the DONE state" note)))
  2212. (defgroup org-priorities nil
  2213. "Priorities in Org-mode."
  2214. :tag "Org Priorities"
  2215. :group 'org-todo)
  2216. (defcustom org-enable-priority-commands t
  2217. "Non-nil means priority commands are active.
  2218. When nil, these commands will be disabled, so that you never accidentally
  2219. set a priority."
  2220. :group 'org-priorities
  2221. :type 'boolean)
  2222. (defcustom org-highest-priority ?A
  2223. "The highest priority of TODO items. A character like ?A, ?B etc.
  2224. Must have a smaller ASCII number than `org-lowest-priority'."
  2225. :group 'org-priorities
  2226. :type 'character)
  2227. (defcustom org-lowest-priority ?C
  2228. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2229. Must have a larger ASCII number than `org-highest-priority'."
  2230. :group 'org-priorities
  2231. :type 'character)
  2232. (defcustom org-default-priority ?B
  2233. "The default priority of TODO items.
  2234. This is the priority an item gets if no explicit priority is given.
  2235. When starting to cycle on an empty priority the first step in the cycle
  2236. depends on `org-priority-start-cycle-with-default'. The resulting first
  2237. step priority must not exceed the range from `org-highest-priority' to
  2238. `org-lowest-priority' which means that `org-default-priority' has to be
  2239. in this range exclusive or inclusive the range boundaries. Else the
  2240. first step refuses to set the default and the second will fall back
  2241. to (depending on the command used) the highest or lowest priority."
  2242. :group 'org-priorities
  2243. :type 'character)
  2244. (defcustom org-priority-start-cycle-with-default t
  2245. "Non-nil means start with default priority when starting to cycle.
  2246. When this is nil, the first step in the cycle will be (depending on the
  2247. command used) one higher or lower than the default priority.
  2248. See also `org-default-priority'."
  2249. :group 'org-priorities
  2250. :type 'boolean)
  2251. (defcustom org-get-priority-function nil
  2252. "Function to extract the priority from a string.
  2253. The string is normally the headline. If this is nil Org computes the
  2254. priority from the priority cookie like [#A] in the headline. It returns
  2255. an integer, increasing by 1000 for each priority level.
  2256. The user can set a different function here, which should take a string
  2257. as an argument and return the numeric priority."
  2258. :group 'org-priorities
  2259. :type 'function)
  2260. (defgroup org-time nil
  2261. "Options concerning time stamps and deadlines in Org-mode."
  2262. :tag "Org Time"
  2263. :group 'org)
  2264. (defcustom org-insert-labeled-timestamps-at-point nil
  2265. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2266. When nil, these labeled time stamps are forces into the second line of an
  2267. entry, just after the headline. When scheduling from the global TODO list,
  2268. the time stamp will always be forced into the second line."
  2269. :group 'org-time
  2270. :type 'boolean)
  2271. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2272. "Formats for `format-time-string' which are used for time stamps.
  2273. It is not recommended to change this constant.")
  2274. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2275. "Number of minutes to round time stamps to.
  2276. These are two values, the first applies when first creating a time stamp.
  2277. The second applies when changing it with the commands `S-up' and `S-down'.
  2278. When changing the time stamp, this means that it will change in steps
  2279. of N minutes, as given by the second value.
  2280. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2281. numbers should be factors of 60, so for example 5, 10, 15.
  2282. When this is larger than 1, you can still force an exact time stamp by using
  2283. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2284. and by using a prefix arg to `S-up/down' to specify the exact number
  2285. of minutes to shift."
  2286. :group 'org-time
  2287. :get #'(lambda (var) ; Make sure both elements are there
  2288. (if (integerp (default-value var))
  2289. (list (default-value var) 5)
  2290. (default-value var)))
  2291. :type '(list
  2292. (integer :tag "when inserting times")
  2293. (integer :tag "when modifying times")))
  2294. ;; Normalize old customizations of this variable.
  2295. (when (integerp org-time-stamp-rounding-minutes)
  2296. (setq org-time-stamp-rounding-minutes
  2297. (list org-time-stamp-rounding-minutes
  2298. org-time-stamp-rounding-minutes)))
  2299. (defcustom org-display-custom-times nil
  2300. "Non-nil means overlay custom formats over all time stamps.
  2301. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2302. To turn this on on a per-file basis, insert anywhere in the file:
  2303. #+STARTUP: customtime"
  2304. :group 'org-time
  2305. :set 'set-default
  2306. :type 'sexp)
  2307. (make-variable-buffer-local 'org-display-custom-times)
  2308. (defcustom org-time-stamp-custom-formats
  2309. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2310. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2311. These are overlaid over the default ISO format if the variable
  2312. `org-display-custom-times' is set. Time like %H:%M should be at the
  2313. end of the second format. The custom formats are also honored by export
  2314. commands, if custom time display is turned on at the time of export."
  2315. :group 'org-time
  2316. :type 'sexp)
  2317. (defun org-time-stamp-format (&optional long inactive)
  2318. "Get the right format for a time string."
  2319. (let ((f (if long (cdr org-time-stamp-formats)
  2320. (car org-time-stamp-formats))))
  2321. (if inactive
  2322. (concat "[" (substring f 1 -1) "]")
  2323. f)))
  2324. (defcustom org-time-clocksum-format "%d:%02d"
  2325. "The format string used when creating CLOCKSUM lines.
  2326. This is also used when org-mode generates a time duration."
  2327. :group 'org-time
  2328. :type 'string)
  2329. (defcustom org-time-clocksum-use-fractional nil
  2330. "If non-nil, \\[org-clock-display] uses fractional times.
  2331. org-mode generates a time duration."
  2332. :group 'org-time
  2333. :type 'boolean)
  2334. (defcustom org-time-clocksum-fractional-format "%.2f"
  2335. "The format string used when creating CLOCKSUM lines, or when
  2336. org-mode generates a time duration."
  2337. :group 'org-time
  2338. :type 'string)
  2339. (defcustom org-deadline-warning-days 14
  2340. "No. of days before expiration during which a deadline becomes active.
  2341. This variable governs the display in sparse trees and in the agenda.
  2342. When 0 or negative, it means use this number (the absolute value of it)
  2343. even if a deadline has a different individual lead time specified.
  2344. Custom commands can set this variable in the options section."
  2345. :group 'org-time
  2346. :group 'org-agenda-daily/weekly
  2347. :type 'integer)
  2348. (defcustom org-read-date-prefer-future t
  2349. "Non-nil means assume future for incomplete date input from user.
  2350. This affects the following situations:
  2351. 1. The user gives a month but not a year.
  2352. For example, if it is April and you enter \"feb 2\", this will be read
  2353. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2354. 2. The user gives a day, but no month.
  2355. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2356. read this as the third of *next* month. However, if you enter \"17\",
  2357. it will be considered as *this* month.
  2358. If you set this variable to the symbol `time', then also the following
  2359. will work:
  2360. 3. If the user gives a time, but no day. If the time is before now,
  2361. to will be interpreted as tomorrow.
  2362. Currently none of this works for ISO week specifications.
  2363. When this option is nil, the current day, month and year will always be
  2364. used as defaults.
  2365. See also `org-agenda-jump-prefer-future'."
  2366. :group 'org-time
  2367. :type '(choice
  2368. (const :tag "Never" nil)
  2369. (const :tag "Check month and day" t)
  2370. (const :tag "Check month, day, and time" time)))
  2371. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2372. "Should the agenda jump command prefer the future for incomplete dates?
  2373. The default is to do the same as configured in `org-read-date-prefer-future'.
  2374. But you can also set a deviating value here.
  2375. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2376. :group 'org-agenda
  2377. :group 'org-time
  2378. :type '(choice
  2379. (const :tag "Use org-read-date-prefer-future"
  2380. org-read-date-prefer-future)
  2381. (const :tag "Never" nil)
  2382. (const :tag "Always" t)))
  2383. (defcustom org-read-date-force-compatible-dates t
  2384. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2385. Depending on the system Emacs is running on, certain dates cannot
  2386. be represented with the type used internally to represent time.
  2387. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2388. correctly. Some systems allow for earlier dates, some for later,
  2389. some for both. One way to find out it to insert any date into an
  2390. Org buffer, putting the cursor on the year and hitting S-up and
  2391. S-down to test the range.
  2392. When this variable is set to t, the date/time prompt will not let
  2393. you specify dates outside the 1970-2037 range, so it is certain that
  2394. these dates will work in whatever version of Emacs you are
  2395. running, and also that you can move a file from one Emacs implementation
  2396. to another. WHenever Org is forcing the year for you, it will display
  2397. a message and beep.
  2398. When this variable is nil, Org will check if the date is
  2399. representable in the specific Emacs implementation you are using.
  2400. If not, it will force a year, usually the current year, and beep
  2401. to remind you. Currently this setting is not recommended because
  2402. the likelihood that you will open your Org files in an Emacs that
  2403. has limited date range is not negligible.
  2404. A workaround for this problem is to use diary sexp dates for time
  2405. stamps outside of this range."
  2406. :group 'org-time
  2407. :type 'boolean)
  2408. (defcustom org-read-date-display-live t
  2409. "Non-nil means display current interpretation of date prompt live.
  2410. This display will be in an overlay, in the minibuffer."
  2411. :group 'org-time
  2412. :type 'boolean)
  2413. (defcustom org-read-date-popup-calendar t
  2414. "Non-nil means pop up a calendar when prompting for a date.
  2415. In the calendar, the date can be selected with mouse-1. However, the
  2416. minibuffer will also be active, and you can simply enter the date as well.
  2417. When nil, only the minibuffer will be available."
  2418. :group 'org-time
  2419. :type 'boolean)
  2420. (if (fboundp 'defvaralias)
  2421. (defvaralias 'org-popup-calendar-for-date-prompt
  2422. 'org-read-date-popup-calendar))
  2423. (defcustom org-read-date-minibuffer-setup-hook nil
  2424. "Hook to be used to set up keys for the date/time interface.
  2425. Add key definitions to `minibuffer-local-map', which will be a temporary
  2426. copy."
  2427. :group 'org-time
  2428. :type 'hook)
  2429. (defcustom org-extend-today-until 0
  2430. "The hour when your day really ends. Must be an integer.
  2431. This has influence for the following applications:
  2432. - When switching the agenda to \"today\". It it is still earlier than
  2433. the time given here, the day recognized as TODAY is actually yesterday.
  2434. - When a date is read from the user and it is still before the time given
  2435. here, the current date and time will be assumed to be yesterday, 23:59.
  2436. Also, timestamps inserted in remember templates follow this rule.
  2437. IMPORTANT: This is a feature whose implementation is and likely will
  2438. remain incomplete. Really, it is only here because past midnight seems to
  2439. be the favorite working time of John Wiegley :-)"
  2440. :group 'org-time
  2441. :type 'integer)
  2442. (defcustom org-use-effective-time nil
  2443. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2444. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2445. \"effective time\" of any timestamps between midnight and 8am will be
  2446. 23:59 of the previous day."
  2447. :group 'boolean
  2448. :type 'integer)
  2449. (defcustom org-edit-timestamp-down-means-later nil
  2450. "Non-nil means S-down will increase the time in a time stamp.
  2451. When nil, S-up will increase."
  2452. :group 'org-time
  2453. :type 'boolean)
  2454. (defcustom org-calendar-follow-timestamp-change t
  2455. "Non-nil means make the calendar window follow timestamp changes.
  2456. When a timestamp is modified and the calendar window is visible, it will be
  2457. moved to the new date."
  2458. :group 'org-time
  2459. :type 'boolean)
  2460. (defgroup org-tags nil
  2461. "Options concerning tags in Org-mode."
  2462. :tag "Org Tags"
  2463. :group 'org)
  2464. (defcustom org-tag-alist nil
  2465. "List of tags allowed in Org-mode files.
  2466. When this list is nil, Org-mode will base TAG input on what is already in the
  2467. buffer.
  2468. The value of this variable is an alist, the car of each entry must be a
  2469. keyword as a string, the cdr may be a character that is used to select
  2470. that tag through the fast-tag-selection interface.
  2471. See the manual for details."
  2472. :group 'org-tags
  2473. :type '(repeat
  2474. (choice
  2475. (cons (string :tag "Tag name")
  2476. (character :tag "Access char"))
  2477. (list :tag "Start radio group"
  2478. (const :startgroup)
  2479. (option (string :tag "Group description")))
  2480. (list :tag "End radio group"
  2481. (const :endgroup)
  2482. (option (string :tag "Group description")))
  2483. (const :tag "New line" (:newline)))))
  2484. (defcustom org-tag-persistent-alist nil
  2485. "List of tags that will always appear in all Org-mode files.
  2486. This is in addition to any in buffer settings or customizations
  2487. of `org-tag-alist'.
  2488. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2489. The value of this variable is an alist, the car of each entry must be a
  2490. keyword as a string, the cdr may be a character that is used to select
  2491. that tag through the fast-tag-selection interface.
  2492. See the manual for details.
  2493. To disable these tags on a per-file basis, insert anywhere in the file:
  2494. #+STARTUP: noptag"
  2495. :group 'org-tags
  2496. :type '(repeat
  2497. (choice
  2498. (cons (string :tag "Tag name")
  2499. (character :tag "Access char"))
  2500. (const :tag "Start radio group" (:startgroup))
  2501. (const :tag "End radio group" (:endgroup))
  2502. (const :tag "New line" (:newline)))))
  2503. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2504. "If non-nil, always offer completion for all tags of all agenda files.
  2505. Instead of customizing this variable directly, you might want to
  2506. set it locally for capture buffers, because there no list of
  2507. tags in that file can be created dynamically (there are none).
  2508. (add-hook 'org-capture-mode-hook
  2509. (lambda ()
  2510. (set (make-local-variable
  2511. 'org-complete-tags-always-offer-all-agenda-tags)
  2512. t)))"
  2513. :group 'org-tags
  2514. :type 'boolean)
  2515. (defvar org-file-tags nil
  2516. "List of tags that can be inherited by all entries in the file.
  2517. The tags will be inherited if the variable `org-use-tag-inheritance'
  2518. says they should be.
  2519. This variable is populated from #+FILETAGS lines.")
  2520. (defcustom org-use-fast-tag-selection 'auto
  2521. "Non-nil means use fast tag selection scheme.
  2522. This is a special interface to select and deselect tags with single keys.
  2523. When nil, fast selection is never used.
  2524. When the symbol `auto', fast selection is used if and only if selection
  2525. characters for tags have been configured, either through the variable
  2526. `org-tag-alist' or through a #+TAGS line in the buffer.
  2527. When t, fast selection is always used and selection keys are assigned
  2528. automatically if necessary."
  2529. :group 'org-tags
  2530. :type '(choice
  2531. (const :tag "Always" t)
  2532. (const :tag "Never" nil)
  2533. (const :tag "When selection characters are configured" 'auto)))
  2534. (defcustom org-fast-tag-selection-single-key nil
  2535. "Non-nil means fast tag selection exits after first change.
  2536. When nil, you have to press RET to exit it.
  2537. During fast tag selection, you can toggle this flag with `C-c'.
  2538. This variable can also have the value `expert'. In this case, the window
  2539. displaying the tags menu is not even shown, until you press C-c again."
  2540. :group 'org-tags
  2541. :type '(choice
  2542. (const :tag "No" nil)
  2543. (const :tag "Yes" t)
  2544. (const :tag "Expert" expert)))
  2545. (defvar org-fast-tag-selection-include-todo nil
  2546. "Non-nil means fast tags selection interface will also offer TODO states.
  2547. This is an undocumented feature, you should not rely on it.")
  2548. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2549. "The column to which tags should be indented in a headline.
  2550. If this number is positive, it specifies the column. If it is negative,
  2551. it means that the tags should be flushright to that column. For example,
  2552. -80 works well for a normal 80 character screen.
  2553. When 0, place tags directly after headline text, with only one space in
  2554. between."
  2555. :group 'org-tags
  2556. :type 'integer)
  2557. (defcustom org-auto-align-tags t
  2558. "Non-nil keeps tags aligned when modifying headlines.
  2559. Some operations (i.e. demoting) change the length of a headline and
  2560. therefore shift the tags around. With this option turned on, after
  2561. each such operation the tags are again aligned to `org-tags-column'."
  2562. :group 'org-tags
  2563. :type 'boolean)
  2564. (defcustom org-use-tag-inheritance t
  2565. "Non-nil means tags in levels apply also for sublevels.
  2566. When nil, only the tags directly given in a specific line apply there.
  2567. This may also be a list of tags that should be inherited, or a regexp that
  2568. matches tags that should be inherited. Additional control is possible
  2569. with the variable `org-tags-exclude-from-inheritance' which gives an
  2570. explicit list of tags to be excluded from inheritance., even if the value of
  2571. `org-use-tag-inheritance' would select it for inheritance.
  2572. If this option is t, a match early-on in a tree can lead to a large
  2573. number of matches in the subtree when constructing the agenda or creating
  2574. a sparse tree. If you only want to see the first match in a tree during
  2575. a search, check out the variable `org-tags-match-list-sublevels'."
  2576. :group 'org-tags
  2577. :type '(choice
  2578. (const :tag "Not" nil)
  2579. (const :tag "Always" t)
  2580. (repeat :tag "Specific tags" (string :tag "Tag"))
  2581. (regexp :tag "Tags matched by regexp")))
  2582. (defcustom org-tags-exclude-from-inheritance nil
  2583. "List of tags that should never be inherited.
  2584. This is a way to exclude a few tags from inheritance. For way to do
  2585. the opposite, to actively allow inheritance for selected tags,
  2586. see the variable `org-use-tag-inheritance'."
  2587. :group 'org-tags
  2588. :type '(repeat (string :tag "Tag")))
  2589. (defun org-tag-inherit-p (tag)
  2590. "Check if TAG is one that should be inherited."
  2591. (cond
  2592. ((member tag org-tags-exclude-from-inheritance) nil)
  2593. ((eq org-use-tag-inheritance t) t)
  2594. ((not org-use-tag-inheritance) nil)
  2595. ((stringp org-use-tag-inheritance)
  2596. (string-match org-use-tag-inheritance tag))
  2597. ((listp org-use-tag-inheritance)
  2598. (member tag org-use-tag-inheritance))
  2599. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2600. (defcustom org-tags-match-list-sublevels t
  2601. "Non-nil means list also sublevels of headlines matching a search.
  2602. This variable applies to tags/property searches, and also to stuck
  2603. projects because this search is based on a tags match as well.
  2604. When set to the symbol `indented', sublevels are indented with
  2605. leading dots.
  2606. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2607. the sublevels of a headline matching a tag search often also match
  2608. the same search. Listing all of them can create very long lists.
  2609. Setting this variable to nil causes subtrees of a match to be skipped.
  2610. This variable is semi-obsolete and probably should always be true. It
  2611. is better to limit inheritance to certain tags using the variables
  2612. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2613. :group 'org-tags
  2614. :type '(choice
  2615. (const :tag "No, don't list them" nil)
  2616. (const :tag "Yes, do list them" t)
  2617. (const :tag "List them, indented with leading dots" indented)))
  2618. (defcustom org-tags-sort-function nil
  2619. "When set, tags are sorted using this function as a comparator."
  2620. :group 'org-tags
  2621. :type '(choice
  2622. (const :tag "No sorting" nil)
  2623. (const :tag "Alphabetical" string<)
  2624. (const :tag "Reverse alphabetical" string>)
  2625. (function :tag "Custom function" nil)))
  2626. (defvar org-tags-history nil
  2627. "History of minibuffer reads for tags.")
  2628. (defvar org-last-tags-completion-table nil
  2629. "The last used completion table for tags.")
  2630. (defvar org-after-tags-change-hook nil
  2631. "Hook that is run after the tags in a line have changed.")
  2632. (defgroup org-properties nil
  2633. "Options concerning properties in Org-mode."
  2634. :tag "Org Properties"
  2635. :group 'org)
  2636. (defcustom org-property-format "%-10s %s"
  2637. "How property key/value pairs should be formatted by `indent-line'.
  2638. When `indent-line' hits a property definition, it will format the line
  2639. according to this format, mainly to make sure that the values are
  2640. lined-up with respect to each other."
  2641. :group 'org-properties
  2642. :type 'string)
  2643. (defcustom org-properties-postprocess-alist nil
  2644. "Alist of properties and functions to adjust inserted values.
  2645. Elements of this alist must be of the form
  2646. ([string] [function])
  2647. where [string] must be a property name and [function] must be a
  2648. lambda expression: this lambda expression must take one argument,
  2649. the value to adjust, and return the new value as a string.
  2650. For example, this element will allow the property \"Remaining\"
  2651. to be updated wrt the relation between the \"Effort\" property
  2652. and the clock summary:
  2653. ((\"Remaining\" (lambda(value)
  2654. (let ((clocksum (org-clock-sum-current-item))
  2655. (effort (org-duration-string-to-minutes
  2656. (org-entry-get (point) \"Effort\"))))
  2657. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2658. :group 'org-properties
  2659. :type 'alist)
  2660. (defcustom org-use-property-inheritance nil
  2661. "Non-nil means properties apply also for sublevels.
  2662. This setting is chiefly used during property searches. Turning it on can
  2663. cause significant overhead when doing a search, which is why it is not
  2664. on by default.
  2665. When nil, only the properties directly given in the current entry count.
  2666. When t, every property is inherited. The value may also be a list of
  2667. properties that should have inheritance, or a regular expression matching
  2668. properties that should be inherited.
  2669. However, note that some special properties use inheritance under special
  2670. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2671. and the properties ending in \"_ALL\" when they are used as descriptor
  2672. for valid values of a property.
  2673. Note for programmers:
  2674. When querying an entry with `org-entry-get', you can control if inheritance
  2675. should be used. By default, `org-entry-get' looks only at the local
  2676. properties. You can request inheritance by setting the inherit argument
  2677. to t (to force inheritance) or to `selective' (to respect the setting
  2678. in this variable)."
  2679. :group 'org-properties
  2680. :type '(choice
  2681. (const :tag "Not" nil)
  2682. (const :tag "Always" t)
  2683. (repeat :tag "Specific properties" (string :tag "Property"))
  2684. (regexp :tag "Properties matched by regexp")))
  2685. (defun org-property-inherit-p (property)
  2686. "Check if PROPERTY is one that should be inherited."
  2687. (cond
  2688. ((eq org-use-property-inheritance t) t)
  2689. ((not org-use-property-inheritance) nil)
  2690. ((stringp org-use-property-inheritance)
  2691. (string-match org-use-property-inheritance property))
  2692. ((listp org-use-property-inheritance)
  2693. (member property org-use-property-inheritance))
  2694. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2695. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2696. "The default column format, if no other format has been defined.
  2697. This variable can be set on the per-file basis by inserting a line
  2698. #+COLUMNS: %25ITEM ....."
  2699. :group 'org-properties
  2700. :type 'string)
  2701. (defcustom org-columns-ellipses ".."
  2702. "The ellipses to be used when a field in column view is truncated.
  2703. When this is the empty string, as many characters as possible are shown,
  2704. but then there will be no visual indication that the field has been truncated.
  2705. When this is a string of length N, the last N characters of a truncated
  2706. field are replaced by this string. If the column is narrower than the
  2707. ellipses string, only part of the ellipses string will be shown."
  2708. :group 'org-properties
  2709. :type 'string)
  2710. (defcustom org-columns-modify-value-for-display-function nil
  2711. "Function that modifies values for display in column view.
  2712. For example, it can be used to cut out a certain part from a time stamp.
  2713. The function must take 2 arguments:
  2714. column-title The title of the column (*not* the property name)
  2715. value The value that should be modified.
  2716. The function should return the value that should be displayed,
  2717. or nil if the normal value should be used."
  2718. :group 'org-properties
  2719. :type 'function)
  2720. (defcustom org-effort-property "Effort"
  2721. "The property that is being used to keep track of effort estimates.
  2722. Effort estimates given in this property need to have the format H:MM."
  2723. :group 'org-properties
  2724. :group 'org-progress
  2725. :type '(string :tag "Property"))
  2726. (defconst org-global-properties-fixed
  2727. '(("VISIBILITY_ALL" . "folded children content all")
  2728. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2729. "List of property/value pairs that can be inherited by any entry.
  2730. These are fixed values, for the preset properties. The user variable
  2731. that can be used to add to this list is `org-global-properties'.
  2732. The entries in this list are cons cells where the car is a property
  2733. name and cdr is a string with the value. If the value represents
  2734. multiple items like an \"_ALL\" property, separate the items by
  2735. spaces.")
  2736. (defcustom org-global-properties nil
  2737. "List of property/value pairs that can be inherited by any entry.
  2738. This list will be combined with the constant `org-global-properties-fixed'.
  2739. The entries in this list are cons cells where the car is a property
  2740. name and cdr is a string with the value.
  2741. You can set buffer-local values for the same purpose in the variable
  2742. `org-file-properties' this by adding lines like
  2743. #+PROPERTY: NAME VALUE"
  2744. :group 'org-properties
  2745. :type '(repeat
  2746. (cons (string :tag "Property")
  2747. (string :tag "Value"))))
  2748. (defvar org-file-properties nil
  2749. "List of property/value pairs that can be inherited by any entry.
  2750. Valid for the current buffer.
  2751. This variable is populated from #+PROPERTY lines.")
  2752. (make-variable-buffer-local 'org-file-properties)
  2753. (defgroup org-agenda nil
  2754. "Options concerning agenda views in Org-mode."
  2755. :tag "Org Agenda"
  2756. :group 'org)
  2757. (defvar org-category nil
  2758. "Variable used by org files to set a category for agenda display.
  2759. Such files should use a file variable to set it, for example
  2760. # -*- mode: org; org-category: \"ELisp\"
  2761. or contain a special line
  2762. #+CATEGORY: ELisp
  2763. If the file does not specify a category, then file's base name
  2764. is used instead.")
  2765. (make-variable-buffer-local 'org-category)
  2766. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2767. (defcustom org-agenda-files nil
  2768. "The files to be used for agenda display.
  2769. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2770. \\[org-remove-file]. You can also use customize to edit the list.
  2771. If an entry is a directory, all files in that directory that are matched by
  2772. `org-agenda-file-regexp' will be part of the file list.
  2773. If the value of the variable is not a list but a single file name, then
  2774. the list of agenda files is actually stored and maintained in that file, one
  2775. agenda file per line. In this file paths can be given relative to
  2776. `org-directory'. Tilde expansion and environment variable substitution
  2777. are also made."
  2778. :group 'org-agenda
  2779. :type '(choice
  2780. (repeat :tag "List of files and directories" file)
  2781. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2782. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2783. "Regular expression to match files for `org-agenda-files'.
  2784. If any element in the list in that variable contains a directory instead
  2785. of a normal file, all files in that directory that are matched by this
  2786. regular expression will be included."
  2787. :group 'org-agenda
  2788. :type 'regexp)
  2789. (defcustom org-agenda-text-search-extra-files nil
  2790. "List of extra files to be searched by text search commands.
  2791. These files will be search in addition to the agenda files by the
  2792. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2793. Note that these files will only be searched for text search commands,
  2794. not for the other agenda views like todo lists, tag searches or the weekly
  2795. agenda. This variable is intended to list notes and possibly archive files
  2796. that should also be searched by these two commands.
  2797. In fact, if the first element in the list is the symbol `agenda-archives',
  2798. than all archive files of all agenda files will be added to the search
  2799. scope."
  2800. :group 'org-agenda
  2801. :type '(set :greedy t
  2802. (const :tag "Agenda Archives" agenda-archives)
  2803. (repeat :inline t (file))))
  2804. (if (fboundp 'defvaralias)
  2805. (defvaralias 'org-agenda-multi-occur-extra-files
  2806. 'org-agenda-text-search-extra-files))
  2807. (defcustom org-agenda-skip-unavailable-files nil
  2808. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2809. A nil value means to remove them, after a query, from the list."
  2810. :group 'org-agenda
  2811. :type 'boolean)
  2812. (defcustom org-calendar-to-agenda-key [?c]
  2813. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2814. The command `org-calendar-goto-agenda' will be bound to this key. The
  2815. default is the character `c' because then `c' can be used to switch back and
  2816. forth between agenda and calendar."
  2817. :group 'org-agenda
  2818. :type 'sexp)
  2819. (defcustom org-calendar-agenda-action-key [?k]
  2820. "The key to be installed in `calendar-mode-map' for agenda-action.
  2821. The command `org-agenda-action' will be bound to this key. The
  2822. default is the character `k' because we use the same key in the agenda."
  2823. :group 'org-agenda
  2824. :type 'sexp)
  2825. (defcustom org-calendar-insert-diary-entry-key [?i]
  2826. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2827. This option is irrelevant until `org-agenda-diary-file' has been configured
  2828. to point to an Org-mode file. When that is the case, the command
  2829. `org-agenda-diary-entry' will be bound to the key given here, by default
  2830. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2831. if you want to continue doing this, you need to change this to a different
  2832. key."
  2833. :group 'org-agenda
  2834. :type 'sexp)
  2835. (defcustom org-agenda-diary-file 'diary-file
  2836. "File to which to add new entries with the `i' key in agenda and calendar.
  2837. When this is the symbol `diary-file', the functionality in the Emacs
  2838. calendar will be used to add entries to the `diary-file'. But when this
  2839. points to a file, `org-agenda-diary-entry' will be used instead."
  2840. :group 'org-agenda
  2841. :type '(choice
  2842. (const :tag "The standard Emacs diary file" diary-file)
  2843. (file :tag "Special Org file diary entries")))
  2844. (eval-after-load "calendar"
  2845. '(progn
  2846. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2847. 'org-calendar-goto-agenda)
  2848. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2849. 'org-agenda-action)
  2850. (add-hook 'calendar-mode-hook
  2851. (lambda ()
  2852. (unless (eq org-agenda-diary-file 'diary-file)
  2853. (define-key calendar-mode-map
  2854. org-calendar-insert-diary-entry-key
  2855. 'org-agenda-diary-entry))))))
  2856. (defgroup org-latex nil
  2857. "Options for embedding LaTeX code into Org-mode."
  2858. :tag "Org LaTeX"
  2859. :group 'org)
  2860. (defcustom org-format-latex-options
  2861. '(:foreground default :background default :scale 1.0
  2862. :html-foreground "Black" :html-background "Transparent"
  2863. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2864. "Options for creating images from LaTeX fragments.
  2865. This is a property list with the following properties:
  2866. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2867. `default' means use the foreground of the default face.
  2868. :background the background color, or \"Transparent\".
  2869. `default' means use the background of the default face.
  2870. :scale a scaling factor for the size of the images, to get more pixels
  2871. :html-foreground, :html-background, :html-scale
  2872. the same numbers for HTML export.
  2873. :matchers a list indicating which matchers should be used to
  2874. find LaTeX fragments. Valid members of this list are:
  2875. \"begin\" find environments
  2876. \"$1\" find single characters surrounded by $.$
  2877. \"$\" find math expressions surrounded by $...$
  2878. \"$$\" find math expressions surrounded by $$....$$
  2879. \"\\(\" find math expressions surrounded by \\(...\\)
  2880. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2881. :group 'org-latex
  2882. :type 'plist)
  2883. (defcustom org-format-latex-signal-error t
  2884. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2885. When nil, just push out a message."
  2886. :group 'org-latex
  2887. :type 'boolean)
  2888. (defcustom org-latex-to-mathml-jar-file nil
  2889. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2890. Use this to specify additional executable file say a jar file.
  2891. When using MathToWeb as the converter, specify the full-path to
  2892. your mathtoweb.jar file."
  2893. :group 'org-latex
  2894. :type '(choice
  2895. (const :tag "None" nil)
  2896. (file :tag "JAR file" :must-match t)))
  2897. (defcustom org-latex-to-mathml-convert-command nil
  2898. "Command to convert LaTeX fragments to MathML.
  2899. Replace format-specifiers in the command as noted below and use
  2900. `shell-command' to convert LaTeX to MathML.
  2901. %j: Executable file in fully expanded form as specified by
  2902. `org-latex-to-mathml-jar-file'.
  2903. %I: Input LaTeX file in fully expanded form
  2904. %o: Output MathML file
  2905. This command is used by `org-create-math-formula'.
  2906. When using MathToWeb as the converter, set this to
  2907. \"java -jar %j -unicode -force -df %o %I\"."
  2908. :group 'org-latex
  2909. :type '(choice
  2910. (const :tag "None" nil)
  2911. (string :tag "\nShell command")))
  2912. (defun org-format-latex-mathml-available-p ()
  2913. "Return t if `org-latex-to-mathml-convert-command' is usable."
  2914. (save-match-data
  2915. (when (and (boundp 'org-latex-to-mathml-convert-command)
  2916. org-latex-to-mathml-convert-command)
  2917. (let ((executable (car (split-string
  2918. org-latex-to-mathml-convert-command))))
  2919. (when (executable-find executable)
  2920. (if (string-match
  2921. "%j" org-latex-to-mathml-convert-command)
  2922. (file-readable-p org-latex-to-mathml-jar-file)
  2923. t))))))
  2924. (defcustom org-format-latex-header "\\documentclass{article}
  2925. \\usepackage[usenames]{color}
  2926. \\usepackage{amsmath}
  2927. \\usepackage[mathscr]{eucal}
  2928. \\pagestyle{empty} % do not remove
  2929. \[PACKAGES]
  2930. \[DEFAULT-PACKAGES]
  2931. % The settings below are copied from fullpage.sty
  2932. \\setlength{\\textwidth}{\\paperwidth}
  2933. \\addtolength{\\textwidth}{-3cm}
  2934. \\setlength{\\oddsidemargin}{1.5cm}
  2935. \\addtolength{\\oddsidemargin}{-2.54cm}
  2936. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2937. \\setlength{\\textheight}{\\paperheight}
  2938. \\addtolength{\\textheight}{-\\headheight}
  2939. \\addtolength{\\textheight}{-\\headsep}
  2940. \\addtolength{\\textheight}{-\\footskip}
  2941. \\addtolength{\\textheight}{-3cm}
  2942. \\setlength{\\topmargin}{1.5cm}
  2943. \\addtolength{\\topmargin}{-2.54cm}"
  2944. "The document header used for processing LaTeX fragments.
  2945. It is imperative that this header make sure that no page number
  2946. appears on the page. The package defined in the variables
  2947. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2948. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2949. will be appended."
  2950. :group 'org-latex
  2951. :type 'string)
  2952. (defvar org-format-latex-header-extra nil)
  2953. (defun org-set-packages-alist (var val)
  2954. "Set the packages alist and make sure it has 3 elements per entry."
  2955. (set var (mapcar (lambda (x)
  2956. (if (and (consp x) (= (length x) 2))
  2957. (list (car x) (nth 1 x) t)
  2958. x))
  2959. val)))
  2960. (defun org-get-packages-alist (var)
  2961. "Get the packages alist and make sure it has 3 elements per entry."
  2962. (mapcar (lambda (x)
  2963. (if (and (consp x) (= (length x) 2))
  2964. (list (car x) (nth 1 x) t)
  2965. x))
  2966. (default-value var)))
  2967. ;; The following variables are defined here because is it also used
  2968. ;; when formatting latex fragments. Originally it was part of the
  2969. ;; LaTeX exporter, which is why the name includes "export".
  2970. (defcustom org-export-latex-default-packages-alist
  2971. '(("AUTO" "inputenc" t)
  2972. ("T1" "fontenc" t)
  2973. ("" "fixltx2e" nil)
  2974. ("" "graphicx" t)
  2975. ("" "longtable" nil)
  2976. ("" "float" nil)
  2977. ("" "wrapfig" nil)
  2978. ("" "soul" t)
  2979. ("" "textcomp" t)
  2980. ("" "marvosym" t)
  2981. ("" "wasysym" t)
  2982. ("" "latexsym" t)
  2983. ("" "amssymb" t)
  2984. ("" "hyperref" nil)
  2985. "\\tolerance=1000"
  2986. )
  2987. "Alist of default packages to be inserted in the header.
  2988. Change this only if one of the packages here causes an incompatibility
  2989. with another package you are using.
  2990. The packages in this list are needed by one part or another of Org-mode
  2991. to function properly.
  2992. - inputenc, fontenc: for basic font and character selection
  2993. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2994. for interpreting the entities in `org-entities'. You can skip some of these
  2995. packages if you don't use any of the symbols in it.
  2996. - graphicx: for including images
  2997. - float, wrapfig: for figure placement
  2998. - longtable: for long tables
  2999. - hyperref: for cross references
  3000. Therefore you should not modify this variable unless you know what you
  3001. are doing. The one reason to change it anyway is that you might be loading
  3002. some other package that conflicts with one of the default packages.
  3003. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3004. If SNIPPET-FLAG is t, the package also needs to be included when
  3005. compiling LaTeX snippets into images for inclusion into HTML."
  3006. :group 'org-export-latex
  3007. :set 'org-set-packages-alist
  3008. :get 'org-get-packages-alist
  3009. :type '(repeat
  3010. (choice
  3011. (list :tag "options/package pair"
  3012. (string :tag "options")
  3013. (string :tag "package")
  3014. (boolean :tag "Snippet"))
  3015. (string :tag "A line of LaTeX"))))
  3016. (defcustom org-export-latex-packages-alist nil
  3017. "Alist of packages to be inserted in every LaTeX header.
  3018. These will be inserted after `org-export-latex-default-packages-alist'.
  3019. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3020. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3021. turning LaTeX snippets into images for inclusion into HTML.
  3022. Make sure that you only list packages here which:
  3023. - you want in every file
  3024. - do not conflict with the default packages in
  3025. `org-export-latex-default-packages-alist'
  3026. - do not conflict with the setup in `org-format-latex-header'."
  3027. :group 'org-export-latex
  3028. :set 'org-set-packages-alist
  3029. :get 'org-get-packages-alist
  3030. :type '(repeat
  3031. (choice
  3032. (list :tag "options/package pair"
  3033. (string :tag "options")
  3034. (string :tag "package")
  3035. (boolean :tag "Snippet"))
  3036. (string :tag "A line of LaTeX"))))
  3037. (defgroup org-appearance nil
  3038. "Settings for Org-mode appearance."
  3039. :tag "Org Appearance"
  3040. :group 'org)
  3041. (defcustom org-level-color-stars-only nil
  3042. "Non-nil means fontify only the stars in each headline.
  3043. When nil, the entire headline is fontified.
  3044. Changing it requires restart of `font-lock-mode' to become effective
  3045. also in regions already fontified."
  3046. :group 'org-appearance
  3047. :type 'boolean)
  3048. (defcustom org-hide-leading-stars nil
  3049. "Non-nil means hide the first N-1 stars in a headline.
  3050. This works by using the face `org-hide' for these stars. This
  3051. face is white for a light background, and black for a dark
  3052. background. You may have to customize the face `org-hide' to
  3053. make this work.
  3054. Changing it requires restart of `font-lock-mode' to become effective
  3055. also in regions already fontified.
  3056. You may also set this on a per-file basis by adding one of the following
  3057. lines to the buffer:
  3058. #+STARTUP: hidestars
  3059. #+STARTUP: showstars"
  3060. :group 'org-appearance
  3061. :type 'boolean)
  3062. (defcustom org-hidden-keywords nil
  3063. "List of symbols corresponding to keywords to be hidden the org buffer.
  3064. For example, a value '(title) for this list will make the document's title
  3065. appear in the buffer without the initial #+TITLE: keyword."
  3066. :group 'org-appearance
  3067. :type '(set (const :tag "#+AUTHOR" author)
  3068. (const :tag "#+DATE" date)
  3069. (const :tag "#+EMAIL" email)
  3070. (const :tag "#+TITLE" title)))
  3071. (defcustom org-fontify-done-headline nil
  3072. "Non-nil means change the face of a headline if it is marked DONE.
  3073. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3074. When this is non-nil, the headline after the keyword is set to the
  3075. `org-headline-done' as an additional indication."
  3076. :group 'org-appearance
  3077. :type 'boolean)
  3078. (defcustom org-fontify-emphasized-text t
  3079. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3080. Changing this variable requires a restart of Emacs to take effect."
  3081. :group 'org-appearance
  3082. :type 'boolean)
  3083. (defcustom org-fontify-whole-heading-line nil
  3084. "Non-nil means fontify the whole line for headings.
  3085. This is useful when setting a background color for the
  3086. org-level-* faces."
  3087. :group 'org-appearance
  3088. :type 'boolean)
  3089. (defcustom org-highlight-latex-fragments-and-specials nil
  3090. "Non-nil means fontify what is treated specially by the exporters."
  3091. :group 'org-appearance
  3092. :type 'boolean)
  3093. (defcustom org-hide-emphasis-markers nil
  3094. "Non-nil mean font-lock should hide the emphasis marker characters."
  3095. :group 'org-appearance
  3096. :type 'boolean)
  3097. (defcustom org-pretty-entities nil
  3098. "Non-nil means show entities as UTF8 characters.
  3099. When nil, the \\name form remains in the buffer."
  3100. :group 'org-appearance
  3101. :type 'boolean)
  3102. (defcustom org-pretty-entities-include-sub-superscripts t
  3103. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3104. :group 'org-appearance
  3105. :type 'boolean)
  3106. (defvar org-emph-re nil
  3107. "Regular expression for matching emphasis.
  3108. After a match, the match groups contain these elements:
  3109. 0 The match of the full regular expression, including the characters
  3110. before and after the proper match
  3111. 1 The character before the proper match, or empty at beginning of line
  3112. 2 The proper match, including the leading and trailing markers
  3113. 3 The leading marker like * or /, indicating the type of highlighting
  3114. 4 The text between the emphasis markers, not including the markers
  3115. 5 The character after the match, empty at the end of a line")
  3116. (defvar org-verbatim-re nil
  3117. "Regular expression for matching verbatim text.")
  3118. (defvar org-emphasis-regexp-components) ; defined just below
  3119. (defvar org-emphasis-alist) ; defined just below
  3120. (defun org-set-emph-re (var val)
  3121. "Set variable and compute the emphasis regular expression."
  3122. (set var val)
  3123. (when (and (boundp 'org-emphasis-alist)
  3124. (boundp 'org-emphasis-regexp-components)
  3125. org-emphasis-alist org-emphasis-regexp-components)
  3126. (let* ((e org-emphasis-regexp-components)
  3127. (pre (car e))
  3128. (post (nth 1 e))
  3129. (border (nth 2 e))
  3130. (body (nth 3 e))
  3131. (nl (nth 4 e))
  3132. (body1 (concat body "*?"))
  3133. (markers (mapconcat 'car org-emphasis-alist ""))
  3134. (vmarkers (mapconcat
  3135. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3136. org-emphasis-alist "")))
  3137. ;; make sure special characters appear at the right position in the class
  3138. (if (string-match "\\^" markers)
  3139. (setq markers (concat (replace-match "" t t markers) "^")))
  3140. (if (string-match "-" markers)
  3141. (setq markers (concat (replace-match "" t t markers) "-")))
  3142. (if (string-match "\\^" vmarkers)
  3143. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3144. (if (string-match "-" vmarkers)
  3145. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3146. (if (> nl 0)
  3147. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3148. (int-to-string nl) "\\}")))
  3149. ;; Make the regexp
  3150. (setq org-emph-re
  3151. (concat "\\([" pre "]\\|^\\)"
  3152. "\\("
  3153. "\\([" markers "]\\)"
  3154. "\\("
  3155. "[^" border "]\\|"
  3156. "[^" border "]"
  3157. body1
  3158. "[^" border "]"
  3159. "\\)"
  3160. "\\3\\)"
  3161. "\\([" post "]\\|$\\)"))
  3162. (setq org-verbatim-re
  3163. (concat "\\([" pre "]\\|^\\)"
  3164. "\\("
  3165. "\\([" vmarkers "]\\)"
  3166. "\\("
  3167. "[^" border "]\\|"
  3168. "[^" border "]"
  3169. body1
  3170. "[^" border "]"
  3171. "\\)"
  3172. "\\3\\)"
  3173. "\\([" post "]\\|$\\)")))))
  3174. (defcustom org-emphasis-regexp-components
  3175. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3176. "Components used to build the regular expression for emphasis.
  3177. This is a list with five entries. Terminology: In an emphasis string
  3178. like \" *strong word* \", we call the initial space PREMATCH, the final
  3179. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3180. and \"trong wor\" is the body. The different components in this variable
  3181. specify what is allowed/forbidden in each part:
  3182. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3183. post Chars allowed as postmatch. End of line will be allowed too.
  3184. border The chars *forbidden* as border characters.
  3185. body-regexp A regexp like \".\" to match a body character. Don't use
  3186. non-shy groups here, and don't allow newline here.
  3187. newline The maximum number of newlines allowed in an emphasis exp.
  3188. Use customize to modify this, or restart Emacs after changing it."
  3189. :group 'org-appearance
  3190. :set 'org-set-emph-re
  3191. :type '(list
  3192. (sexp :tag "Allowed chars in pre ")
  3193. (sexp :tag "Allowed chars in post ")
  3194. (sexp :tag "Forbidden chars in border ")
  3195. (sexp :tag "Regexp for body ")
  3196. (integer :tag "number of newlines allowed")
  3197. (option (boolean :tag "Please ignore this button"))))
  3198. (defcustom org-emphasis-alist
  3199. `(("*" bold "<b>" "</b>")
  3200. ("/" italic "<i>" "</i>")
  3201. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3202. ("=" org-code "<code>" "</code>" verbatim)
  3203. ("~" org-verbatim "<code>" "</code>" verbatim)
  3204. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3205. "<del>" "</del>")
  3206. )
  3207. "Special syntax for emphasized text.
  3208. Text starting and ending with a special character will be emphasized, for
  3209. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3210. characters, the face to be used by font-lock for highlighting in Org-mode
  3211. Emacs buffers, and the HTML tags to be used for this.
  3212. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3213. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3214. Use customize to modify this, or restart Emacs after changing it."
  3215. :group 'org-appearance
  3216. :set 'org-set-emph-re
  3217. :type '(repeat
  3218. (list
  3219. (string :tag "Marker character")
  3220. (choice
  3221. (face :tag "Font-lock-face")
  3222. (plist :tag "Face property list"))
  3223. (string :tag "HTML start tag")
  3224. (string :tag "HTML end tag")
  3225. (option (const verbatim)))))
  3226. (defvar org-protecting-blocks
  3227. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3228. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3229. This is needed for font-lock setup.")
  3230. ;;; Miscellaneous options
  3231. (defgroup org-completion nil
  3232. "Completion in Org-mode."
  3233. :tag "Org Completion"
  3234. :group 'org)
  3235. (defcustom org-completion-use-ido nil
  3236. "Non-nil means use ido completion wherever possible.
  3237. Note that `ido-mode' must be active for this variable to be relevant.
  3238. If you decide to turn this variable on, you might well want to turn off
  3239. `org-outline-path-complete-in-steps'.
  3240. See also `org-completion-use-iswitchb'."
  3241. :group 'org-completion
  3242. :type 'boolean)
  3243. (defcustom org-completion-use-iswitchb nil
  3244. "Non-nil means use iswitchb completion wherever possible.
  3245. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3246. If you decide to turn this variable on, you might well want to turn off
  3247. `org-outline-path-complete-in-steps'.
  3248. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3249. :group 'org-completion
  3250. :type 'boolean)
  3251. (defcustom org-completion-fallback-command 'hippie-expand
  3252. "The expansion command called by \\[pcomplete] in normal context.
  3253. Normal means, no org-mode-specific context."
  3254. :group 'org-completion
  3255. :type 'function)
  3256. ;;; Functions and variables from their packages
  3257. ;; Declared here to avoid compiler warnings
  3258. ;; XEmacs only
  3259. (defvar outline-mode-menu-heading)
  3260. (defvar outline-mode-menu-show)
  3261. (defvar outline-mode-menu-hide)
  3262. (defvar zmacs-regions) ; XEmacs regions
  3263. ;; Emacs only
  3264. (defvar mark-active)
  3265. ;; Various packages
  3266. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3267. (declare-function calendar-forward-day "cal-move" (arg))
  3268. (declare-function calendar-goto-date "cal-move" (date))
  3269. (declare-function calendar-goto-today "cal-move" ())
  3270. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3271. (defvar calc-embedded-close-formula)
  3272. (defvar calc-embedded-open-formula)
  3273. (declare-function cdlatex-tab "ext:cdlatex" ())
  3274. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3275. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3276. (defvar font-lock-unfontify-region-function)
  3277. (declare-function iswitchb-read-buffer "iswitchb"
  3278. (prompt &optional default require-match start matches-set))
  3279. (defvar iswitchb-temp-buflist)
  3280. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3281. (defvar org-agenda-tags-todo-honor-ignore-options)
  3282. (declare-function org-agenda-skip "org-agenda" ())
  3283. (declare-function
  3284. org-agenda-format-item "org-agenda"
  3285. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3286. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3287. (declare-function org-agenda-change-all-lines "org-agenda"
  3288. (newhead hdmarker &optional fixface just-this))
  3289. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3290. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3291. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3292. (beg end))
  3293. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3294. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3295. "org-agenda" (&optional end))
  3296. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3297. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3298. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3299. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3300. (declare-function org-indent-mode "org-indent" (&optional arg))
  3301. (declare-function parse-time-string "parse-time" (string))
  3302. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3303. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3304. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3305. (defvar remember-data-file)
  3306. (defvar texmathp-why)
  3307. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3308. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3309. (defvar w3m-current-url)
  3310. (defvar w3m-current-title)
  3311. (defvar org-latex-regexps)
  3312. ;;; Autoload and prepare some org modules
  3313. ;; Some table stuff that needs to be defined here, because it is used
  3314. ;; by the functions setting up org-mode or checking for table context.
  3315. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3316. "Detect an org-type or table-type table.")
  3317. (defconst org-table-line-regexp "^[ \t]*|"
  3318. "Detect an org-type table line.")
  3319. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3320. "Detect an org-type table line.")
  3321. (defconst org-table-hline-regexp "^[ \t]*|-"
  3322. "Detect an org-type table hline.")
  3323. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3324. "Detect a table-type table hline.")
  3325. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3326. "Detect the first line outside a table when searching from within it.
  3327. This works for both table types.")
  3328. ;; Autoload the functions in org-table.el that are needed by functions here.
  3329. (eval-and-compile
  3330. (org-autoload "org-table"
  3331. '(org-table-align org-table-begin org-table-blank-field
  3332. org-table-convert org-table-convert-region org-table-copy-down
  3333. org-table-copy-region org-table-create
  3334. org-table-create-or-convert-from-region
  3335. org-table-create-with-table.el org-table-current-dline
  3336. org-table-cut-region org-table-delete-column org-table-edit-field
  3337. org-table-edit-formulas org-table-end org-table-eval-formula
  3338. org-table-export org-table-field-info
  3339. org-table-get-stored-formulas org-table-goto-column
  3340. org-table-hline-and-move org-table-import org-table-insert-column
  3341. org-table-insert-hline org-table-insert-row org-table-iterate
  3342. org-table-justify-field-maybe org-table-kill-row
  3343. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3344. org-table-move-column org-table-move-column-left
  3345. org-table-move-column-right org-table-move-row
  3346. org-table-move-row-down org-table-move-row-up
  3347. org-table-next-field org-table-next-row org-table-paste-rectangle
  3348. org-table-previous-field org-table-recalculate
  3349. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3350. org-table-toggle-coordinate-overlays
  3351. org-table-toggle-formula-debugger org-table-wrap-region
  3352. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3353. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3354. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3355. (defun org-at-table-p (&optional table-type)
  3356. "Return t if the cursor is inside an org-type table.
  3357. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3358. (if org-enable-table-editor
  3359. (save-excursion
  3360. (beginning-of-line 1)
  3361. (looking-at (if table-type org-table-any-line-regexp
  3362. org-table-line-regexp)))
  3363. nil))
  3364. (defsubst org-table-p () (org-at-table-p))
  3365. (defun org-at-table.el-p ()
  3366. "Return t if and only if we are at a table.el table."
  3367. (and (org-at-table-p 'any)
  3368. (save-excursion
  3369. (goto-char (org-table-begin 'any))
  3370. (looking-at org-table1-hline-regexp))))
  3371. (defun org-table-recognize-table.el ()
  3372. "If there is a table.el table nearby, recognize it and move into it."
  3373. (if org-table-tab-recognizes-table.el
  3374. (if (org-at-table.el-p)
  3375. (progn
  3376. (beginning-of-line 1)
  3377. (if (looking-at org-table-dataline-regexp)
  3378. nil
  3379. (if (looking-at org-table1-hline-regexp)
  3380. (progn
  3381. (beginning-of-line 2)
  3382. (if (looking-at org-table-any-border-regexp)
  3383. (beginning-of-line -1)))))
  3384. (if (re-search-forward "|" (org-table-end t) t)
  3385. (progn
  3386. (require 'table)
  3387. (if (table--at-cell-p (point))
  3388. t
  3389. (message "recognizing table.el table...")
  3390. (table-recognize-table)
  3391. (message "recognizing table.el table...done")))
  3392. (error "This should not happen"))
  3393. t)
  3394. nil)
  3395. nil))
  3396. (defun org-at-table-hline-p ()
  3397. "Return t if the cursor is inside a hline in a table."
  3398. (if org-enable-table-editor
  3399. (save-excursion
  3400. (beginning-of-line 1)
  3401. (looking-at org-table-hline-regexp))
  3402. nil))
  3403. (defvar org-table-clean-did-remove-column nil)
  3404. (defun org-table-map-tables (function &optional quietly)
  3405. "Apply FUNCTION to the start of all tables in the buffer."
  3406. (save-excursion
  3407. (save-restriction
  3408. (widen)
  3409. (goto-char (point-min))
  3410. (while (re-search-forward org-table-any-line-regexp nil t)
  3411. (unless quietly
  3412. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3413. (beginning-of-line 1)
  3414. (when (looking-at org-table-line-regexp)
  3415. (save-excursion (funcall function))
  3416. (or (looking-at org-table-line-regexp)
  3417. (forward-char 1)))
  3418. (re-search-forward org-table-any-border-regexp nil 1))))
  3419. (unless quietly (message "Mapping tables: done")))
  3420. ;; Declare and autoload functions from org-exp.el & Co
  3421. (declare-function org-default-export-plist "org-exp")
  3422. (declare-function org-infile-export-plist "org-exp")
  3423. (declare-function org-get-current-options "org-exp")
  3424. (eval-and-compile
  3425. (org-autoload "org-exp"
  3426. '(org-export org-export-visible
  3427. org-insert-export-options-template
  3428. org-table-clean-before-export))
  3429. (org-autoload "org-ascii"
  3430. '(org-export-as-ascii org-export-ascii-preprocess
  3431. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3432. org-export-region-as-ascii))
  3433. (org-autoload "org-latex"
  3434. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3435. org-replace-region-by-latex org-export-region-as-latex
  3436. org-export-as-latex org-export-as-pdf
  3437. org-export-as-pdf-and-open))
  3438. (org-autoload "org-html"
  3439. '(org-export-as-html-and-open
  3440. org-export-as-html-batch org-export-as-html-to-buffer
  3441. org-replace-region-by-html org-export-region-as-html
  3442. org-export-as-html))
  3443. (org-autoload "org-docbook"
  3444. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3445. org-replace-region-by-docbook org-export-region-as-docbook
  3446. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3447. org-export-as-docbook))
  3448. (org-autoload "org-icalendar"
  3449. '(org-export-icalendar-this-file
  3450. org-export-icalendar-all-agenda-files
  3451. org-export-icalendar-combine-agenda-files))
  3452. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3453. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3454. ;; Declare and autoload functions from org-agenda.el
  3455. (eval-and-compile
  3456. (org-autoload "org-agenda"
  3457. '(org-agenda org-agenda-list org-search-view
  3458. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3459. org-diary org-agenda-to-appt
  3460. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3461. ;; Autoload org-remember
  3462. (eval-and-compile
  3463. (org-autoload "org-remember"
  3464. '(org-remember-insinuate org-remember-annotation
  3465. org-remember-apply-template org-remember org-remember-handler)))
  3466. (eval-and-compile
  3467. (org-autoload "org-capture"
  3468. '(org-capture org-capture-insert-template-here
  3469. org-capture-import-remember-templates)))
  3470. ;; Autoload org-clock.el
  3471. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3472. (beg end))
  3473. (declare-function org-clock-update-mode-line "org-clock" ())
  3474. (declare-function org-resolve-clocks "org-clock"
  3475. (&optional also-non-dangling-p prompt last-valid))
  3476. (defvar org-clock-start-time)
  3477. (defvar org-clock-marker (make-marker)
  3478. "Marker recording the last clock-in.")
  3479. (defvar org-clock-hd-marker (make-marker)
  3480. "Marker recording the last clock-in, but the headline position.")
  3481. (defvar org-clock-heading ""
  3482. "The heading of the current clock entry.")
  3483. (defun org-clock-is-active ()
  3484. "Return non-nil if clock is currently running.
  3485. The return value is actually the clock marker."
  3486. (marker-buffer org-clock-marker))
  3487. (eval-and-compile
  3488. (org-autoload
  3489. "org-clock"
  3490. '(org-clock-in org-clock-out org-clock-cancel
  3491. org-clock-goto org-clock-sum org-clock-display
  3492. org-clock-remove-overlays org-clock-report
  3493. org-clocktable-shift org-dblock-write:clocktable
  3494. org-get-clocktable org-resolve-clocks)))
  3495. (defun org-clock-update-time-maybe ()
  3496. "If this is a CLOCK line, update it and return t.
  3497. Otherwise, return nil."
  3498. (interactive)
  3499. (save-excursion
  3500. (beginning-of-line 1)
  3501. (skip-chars-forward " \t")
  3502. (when (looking-at org-clock-string)
  3503. (let ((re (concat "[ \t]*" org-clock-string
  3504. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3505. "\\([ \t]*=>.*\\)?\\)?"))
  3506. ts te h m s neg)
  3507. (cond
  3508. ((not (looking-at re))
  3509. nil)
  3510. ((not (match-end 2))
  3511. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3512. (> org-clock-marker (point))
  3513. (<= org-clock-marker (point-at-eol)))
  3514. ;; The clock is running here
  3515. (setq org-clock-start-time
  3516. (apply 'encode-time
  3517. (org-parse-time-string (match-string 1))))
  3518. (org-clock-update-mode-line)))
  3519. (t
  3520. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3521. (end-of-line 1)
  3522. (setq ts (match-string 1)
  3523. te (match-string 3))
  3524. (setq s (- (org-float-time
  3525. (apply 'encode-time (org-parse-time-string te)))
  3526. (org-float-time
  3527. (apply 'encode-time (org-parse-time-string ts))))
  3528. neg (< s 0)
  3529. s (abs s)
  3530. h (floor (/ s 3600))
  3531. s (- s (* 3600 h))
  3532. m (floor (/ s 60))
  3533. s (- s (* 60 s)))
  3534. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3535. t))))))
  3536. (defun org-check-running-clock ()
  3537. "Check if the current buffer contains the running clock.
  3538. If yes, offer to stop it and to save the buffer with the changes."
  3539. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3540. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3541. (buffer-name))))
  3542. (org-clock-out)
  3543. (when (y-or-n-p "Save changed buffer?")
  3544. (save-buffer))))
  3545. (defun org-clocktable-try-shift (dir n)
  3546. "Check if this line starts a clock table, if yes, shift the time block."
  3547. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3548. (org-clocktable-shift dir n)))
  3549. ;; Autoload org-timer.el
  3550. (eval-and-compile
  3551. (org-autoload
  3552. "org-timer"
  3553. '(org-timer-start org-timer org-timer-item
  3554. org-timer-change-times-in-region
  3555. org-timer-set-timer
  3556. org-timer-reset-timers
  3557. org-timer-show-remaining-time)))
  3558. ;; Autoload org-feed.el
  3559. (eval-and-compile
  3560. (org-autoload
  3561. "org-feed"
  3562. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3563. ;; Autoload org-indent.el
  3564. ;; Define the variable already here, to make sure we have it.
  3565. (defvar org-indent-mode nil
  3566. "Non-nil if Org-Indent mode is enabled.
  3567. Use the command `org-indent-mode' to change this variable.")
  3568. (eval-and-compile
  3569. (org-autoload
  3570. "org-indent"
  3571. '(org-indent-mode)))
  3572. ;; Autoload org-mobile.el
  3573. (eval-and-compile
  3574. (org-autoload
  3575. "org-mobile"
  3576. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3577. ;; Autoload archiving code
  3578. ;; The stuff that is needed for cycling and tags has to be defined here.
  3579. (defgroup org-archive nil
  3580. "Options concerning archiving in Org-mode."
  3581. :tag "Org Archive"
  3582. :group 'org-structure)
  3583. (defcustom org-archive-location "%s_archive::"
  3584. "The location where subtrees should be archived.
  3585. The value of this variable is a string, consisting of two parts,
  3586. separated by a double-colon. The first part is a filename and
  3587. the second part is a headline.
  3588. When the filename is omitted, archiving happens in the same file.
  3589. %s in the filename will be replaced by the current file
  3590. name (without the directory part). Archiving to a different file
  3591. is useful to keep archived entries from contributing to the
  3592. Org-mode Agenda.
  3593. The archived entries will be filed as subtrees of the specified
  3594. headline. When the headline is omitted, the subtrees are simply
  3595. filed away at the end of the file, as top-level entries. Also in
  3596. the heading you can use %s to represent the file name, this can be
  3597. useful when using the same archive for a number of different files.
  3598. Here are a few examples:
  3599. \"%s_archive::\"
  3600. If the current file is Projects.org, archive in file
  3601. Projects.org_archive, as top-level trees. This is the default.
  3602. \"::* Archived Tasks\"
  3603. Archive in the current file, under the top-level headline
  3604. \"* Archived Tasks\".
  3605. \"~/org/archive.org::\"
  3606. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3607. \"~/org/archive.org::* From %s\"
  3608. Archive in file ~/org/archive.org (absolute path), under headlines
  3609. \"From FILENAME\" where file name is the current file name.
  3610. \"basement::** Finished Tasks\"
  3611. Archive in file ./basement (relative path), as level 3 trees
  3612. below the level 2 heading \"** Finished Tasks\".
  3613. You may set this option on a per-file basis by adding to the buffer a
  3614. line like
  3615. #+ARCHIVE: basement::** Finished Tasks
  3616. You may also define it locally for a subtree by setting an ARCHIVE property
  3617. in the entry. If such a property is found in an entry, or anywhere up
  3618. the hierarchy, it will be used."
  3619. :group 'org-archive
  3620. :type 'string)
  3621. (defcustom org-archive-tag "ARCHIVE"
  3622. "The tag that marks a subtree as archived.
  3623. An archived subtree does not open during visibility cycling, and does
  3624. not contribute to the agenda listings.
  3625. After changing this, font-lock must be restarted in the relevant buffers to
  3626. get the proper fontification."
  3627. :group 'org-archive
  3628. :group 'org-keywords
  3629. :type 'string)
  3630. (defcustom org-agenda-skip-archived-trees t
  3631. "Non-nil means the agenda will skip any items located in archived trees.
  3632. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3633. variable is no longer recommended, you should leave it at the value t.
  3634. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3635. :group 'org-archive
  3636. :group 'org-agenda-skip
  3637. :type 'boolean)
  3638. (defcustom org-columns-skip-archived-trees t
  3639. "Non-nil means ignore archived trees when creating column view."
  3640. :group 'org-archive
  3641. :group 'org-properties
  3642. :type 'boolean)
  3643. (defcustom org-cycle-open-archived-trees nil
  3644. "Non-nil means `org-cycle' will open archived trees.
  3645. An archived tree is a tree marked with the tag ARCHIVE.
  3646. When nil, archived trees will stay folded. You can still open them with
  3647. normal outline commands like `show-all', but not with the cycling commands."
  3648. :group 'org-archive
  3649. :group 'org-cycle
  3650. :type 'boolean)
  3651. (defcustom org-sparse-tree-open-archived-trees nil
  3652. "Non-nil means sparse tree construction shows matches in archived trees.
  3653. When nil, matches in these trees are highlighted, but the trees are kept in
  3654. collapsed state."
  3655. :group 'org-archive
  3656. :group 'org-sparse-trees
  3657. :type 'boolean)
  3658. (defun org-cycle-hide-archived-subtrees (state)
  3659. "Re-hide all archived subtrees after a visibility state change."
  3660. (when (and (not org-cycle-open-archived-trees)
  3661. (not (memq state '(overview folded))))
  3662. (save-excursion
  3663. (let* ((globalp (memq state '(contents all)))
  3664. (beg (if globalp (point-min) (point)))
  3665. (end (if globalp (point-max) (org-end-of-subtree t))))
  3666. (org-hide-archived-subtrees beg end)
  3667. (goto-char beg)
  3668. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3669. (message "%s" (substitute-command-keys
  3670. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3671. (defun org-force-cycle-archived ()
  3672. "Cycle subtree even if it is archived."
  3673. (interactive)
  3674. (setq this-command 'org-cycle)
  3675. (let ((org-cycle-open-archived-trees t))
  3676. (call-interactively 'org-cycle)))
  3677. (defun org-hide-archived-subtrees (beg end)
  3678. "Re-hide all archived subtrees after a visibility state change."
  3679. (save-excursion
  3680. (let* ((re (concat ":" org-archive-tag ":")))
  3681. (goto-char beg)
  3682. (while (re-search-forward re end t)
  3683. (when (org-at-heading-p)
  3684. (org-flag-subtree t)
  3685. (org-end-of-subtree t))))))
  3686. (defun org-flag-subtree (flag)
  3687. (save-excursion
  3688. (org-back-to-heading t)
  3689. (outline-end-of-heading)
  3690. (outline-flag-region (point)
  3691. (progn (org-end-of-subtree t) (point))
  3692. flag)))
  3693. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3694. (eval-and-compile
  3695. (org-autoload "org-archive"
  3696. '(org-add-archive-files org-archive-subtree
  3697. org-archive-to-archive-sibling org-toggle-archive-tag
  3698. org-archive-subtree-default
  3699. org-archive-subtree-default-with-confirmation)))
  3700. ;; Autoload Column View Code
  3701. (declare-function org-columns-number-to-string "org-colview")
  3702. (declare-function org-columns-get-format-and-top-level "org-colview")
  3703. (declare-function org-columns-compute "org-colview")
  3704. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3705. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3706. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3707. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3708. ;; Autoload ID code
  3709. (declare-function org-id-store-link "org-id")
  3710. (declare-function org-id-locations-load "org-id")
  3711. (declare-function org-id-locations-save "org-id")
  3712. (defvar org-id-track-globally)
  3713. (org-autoload "org-id"
  3714. '(org-id-get-create org-id-new org-id-copy org-id-get
  3715. org-id-get-with-outline-path-completion
  3716. org-id-get-with-outline-drilling org-id-store-link
  3717. org-id-goto org-id-find org-id-store-link))
  3718. ;; Autoload Plotting Code
  3719. (org-autoload "org-plot"
  3720. '(org-plot/gnuplot))
  3721. ;;; Variables for pre-computed regular expressions, all buffer local
  3722. (defvar org-drawer-regexp nil
  3723. "Matches first line of a hidden block.")
  3724. (make-variable-buffer-local 'org-drawer-regexp)
  3725. (defvar org-todo-regexp nil
  3726. "Matches any of the TODO state keywords.")
  3727. (make-variable-buffer-local 'org-todo-regexp)
  3728. (defvar org-not-done-regexp nil
  3729. "Matches any of the TODO state keywords except the last one.")
  3730. (make-variable-buffer-local 'org-not-done-regexp)
  3731. (defvar org-not-done-heading-regexp nil
  3732. "Matches a TODO headline that is not done.")
  3733. (make-variable-buffer-local 'org-not-done-regexp)
  3734. (defvar org-todo-line-regexp nil
  3735. "Matches a headline and puts TODO state into group 2 if present.")
  3736. (make-variable-buffer-local 'org-todo-line-regexp)
  3737. (defvar org-complex-heading-regexp nil
  3738. "Matches a headline and puts everything into groups:
  3739. group 1: the stars
  3740. group 2: The todo keyword, maybe
  3741. group 3: Priority cookie
  3742. group 4: True headline
  3743. group 5: Tags")
  3744. (make-variable-buffer-local 'org-complex-heading-regexp)
  3745. (defvar org-complex-heading-regexp-format nil
  3746. "Printf format to make regexp to match an exact headline.
  3747. This regexp will match the headline of any node which has the
  3748. exact headline text that is put into the format, but may have any
  3749. TODO state, priority and tags.")
  3750. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3751. (defvar org-todo-line-tags-regexp nil
  3752. "Matches a headline and puts TODO state into group 2 if present.
  3753. Also put tags into group 4 if tags are present.")
  3754. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3755. (defvar org-ds-keyword-length 12
  3756. "Maximum length of the Deadline and SCHEDULED keywords.")
  3757. (make-variable-buffer-local 'org-ds-keyword-length)
  3758. (defvar org-deadline-regexp nil
  3759. "Matches the DEADLINE keyword.")
  3760. (make-variable-buffer-local 'org-deadline-regexp)
  3761. (defvar org-deadline-time-regexp nil
  3762. "Matches the DEADLINE keyword together with a time stamp.")
  3763. (make-variable-buffer-local 'org-deadline-time-regexp)
  3764. (defvar org-deadline-line-regexp nil
  3765. "Matches the DEADLINE keyword and the rest of the line.")
  3766. (make-variable-buffer-local 'org-deadline-line-regexp)
  3767. (defvar org-scheduled-regexp nil
  3768. "Matches the SCHEDULED keyword.")
  3769. (make-variable-buffer-local 'org-scheduled-regexp)
  3770. (defvar org-scheduled-time-regexp nil
  3771. "Matches the SCHEDULED keyword together with a time stamp.")
  3772. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3773. (defvar org-closed-time-regexp nil
  3774. "Matches the CLOSED keyword together with a time stamp.")
  3775. (make-variable-buffer-local 'org-closed-time-regexp)
  3776. (defvar org-keyword-time-regexp nil
  3777. "Matches any of the 4 keywords, together with the time stamp.")
  3778. (make-variable-buffer-local 'org-keyword-time-regexp)
  3779. (defvar org-keyword-time-not-clock-regexp nil
  3780. "Matches any of the 3 keywords, together with the time stamp.")
  3781. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3782. (defvar org-maybe-keyword-time-regexp nil
  3783. "Matches a timestamp, possibly preceded by a keyword.")
  3784. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3785. (defvar org-planning-or-clock-line-re nil
  3786. "Matches a line with planning or clock info.")
  3787. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3788. (defvar org-all-time-keywords nil
  3789. "List of time keywords.")
  3790. (make-variable-buffer-local 'org-all-time-keywords)
  3791. (defconst org-plain-time-of-day-regexp
  3792. (concat
  3793. "\\(\\<[012]?[0-9]"
  3794. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3795. "\\(--?"
  3796. "\\(\\<[012]?[0-9]"
  3797. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3798. "\\)?")
  3799. "Regular expression to match a plain time or time range.
  3800. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3801. groups carry important information:
  3802. 0 the full match
  3803. 1 the first time, range or not
  3804. 8 the second time, if it is a range.")
  3805. (defconst org-plain-time-extension-regexp
  3806. (concat
  3807. "\\(\\<[012]?[0-9]"
  3808. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3809. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3810. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3811. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3812. groups carry important information:
  3813. 0 the full match
  3814. 7 hours of duration
  3815. 9 minutes of duration")
  3816. (defconst org-stamp-time-of-day-regexp
  3817. (concat
  3818. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3819. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3820. "\\(--?"
  3821. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3822. "Regular expression to match a timestamp time or time range.
  3823. After a match, the following groups carry important information:
  3824. 0 the full match
  3825. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3826. 2 the first time, range or not
  3827. 4 the second time, if it is a range.")
  3828. (defconst org-startup-options
  3829. '(("fold" org-startup-folded t)
  3830. ("overview" org-startup-folded t)
  3831. ("nofold" org-startup-folded nil)
  3832. ("showall" org-startup-folded nil)
  3833. ("showeverything" org-startup-folded showeverything)
  3834. ("content" org-startup-folded content)
  3835. ("indent" org-startup-indented t)
  3836. ("noindent" org-startup-indented nil)
  3837. ("hidestars" org-hide-leading-stars t)
  3838. ("showstars" org-hide-leading-stars nil)
  3839. ("odd" org-odd-levels-only t)
  3840. ("oddeven" org-odd-levels-only nil)
  3841. ("align" org-startup-align-all-tables t)
  3842. ("noalign" org-startup-align-all-tables nil)
  3843. ("inlineimages" org-startup-with-inline-images t)
  3844. ("noinlineimages" org-startup-with-inline-images nil)
  3845. ("customtime" org-display-custom-times t)
  3846. ("logdone" org-log-done time)
  3847. ("lognotedone" org-log-done note)
  3848. ("nologdone" org-log-done nil)
  3849. ("lognoteclock-out" org-log-note-clock-out t)
  3850. ("nolognoteclock-out" org-log-note-clock-out nil)
  3851. ("logrepeat" org-log-repeat state)
  3852. ("lognoterepeat" org-log-repeat note)
  3853. ("nologrepeat" org-log-repeat nil)
  3854. ("logreschedule" org-log-reschedule time)
  3855. ("lognotereschedule" org-log-reschedule note)
  3856. ("nologreschedule" org-log-reschedule nil)
  3857. ("logredeadline" org-log-redeadline time)
  3858. ("lognoteredeadline" org-log-redeadline note)
  3859. ("nologredeadline" org-log-redeadline nil)
  3860. ("logrefile" org-log-refile time)
  3861. ("lognoterefile" org-log-refile note)
  3862. ("nologrefile" org-log-refile nil)
  3863. ("fninline" org-footnote-define-inline t)
  3864. ("nofninline" org-footnote-define-inline nil)
  3865. ("fnlocal" org-footnote-section nil)
  3866. ("fnauto" org-footnote-auto-label t)
  3867. ("fnprompt" org-footnote-auto-label nil)
  3868. ("fnconfirm" org-footnote-auto-label confirm)
  3869. ("fnplain" org-footnote-auto-label plain)
  3870. ("fnadjust" org-footnote-auto-adjust t)
  3871. ("nofnadjust" org-footnote-auto-adjust nil)
  3872. ("constcgs" constants-unit-system cgs)
  3873. ("constSI" constants-unit-system SI)
  3874. ("noptag" org-tag-persistent-alist nil)
  3875. ("hideblocks" org-hide-block-startup t)
  3876. ("nohideblocks" org-hide-block-startup nil)
  3877. ("beamer" org-startup-with-beamer-mode t)
  3878. ("entitiespretty" org-pretty-entities t)
  3879. ("entitiesplain" org-pretty-entities nil))
  3880. "Variable associated with STARTUP options for org-mode.
  3881. Each element is a list of three items: The startup options as written
  3882. in the #+STARTUP line, the corresponding variable, and the value to
  3883. set this variable to if the option is found. An optional forth element PUSH
  3884. means to push this value onto the list in the variable.")
  3885. (defun org-update-property-plist (key val props)
  3886. "Update PROPS with KEY and VAL."
  3887. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3888. (key (if appending (substring key 0 (- (length key) 1)) key))
  3889. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3890. (previous (cdr (assoc key props))))
  3891. (if appending
  3892. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3893. (cons (cons key val) remainder))))
  3894. (defconst org-block-regexp
  3895. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3896. "Regular expression for hiding blocks.")
  3897. (defconst org-heading-keyword-regexp-format
  3898. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3899. "Printf format for a regexp matching an headline with some keyword.
  3900. This regexp will match the headline of any node which has the
  3901. exact keyword that is put into the format. The keyword isn't in
  3902. any group by default, but the stars and the body are.")
  3903. (defconst org-heading-keyword-maybe-regexp-format
  3904. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3905. "Printf format for a regexp matching an headline, possibly with some keyword.
  3906. This regexp can match any headline with the specified keyword, or
  3907. without a keyword. The keyword isn't in any group by default,
  3908. but the stars and the body are.")
  3909. (defun org-set-regexps-and-options ()
  3910. "Precompute regular expressions for current buffer."
  3911. (when (eq major-mode 'org-mode)
  3912. (org-set-local 'org-todo-kwd-alist nil)
  3913. (org-set-local 'org-todo-key-alist nil)
  3914. (org-set-local 'org-todo-key-trigger nil)
  3915. (org-set-local 'org-todo-keywords-1 nil)
  3916. (org-set-local 'org-done-keywords nil)
  3917. (org-set-local 'org-todo-heads nil)
  3918. (org-set-local 'org-todo-sets nil)
  3919. (org-set-local 'org-todo-log-states nil)
  3920. (org-set-local 'org-file-properties nil)
  3921. (org-set-local 'org-file-tags nil)
  3922. (let ((re (org-make-options-regexp
  3923. '("CATEGORY" "TODO" "COLUMNS"
  3924. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3925. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3926. "OPTIONS")
  3927. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3928. (splitre "[ \t]+")
  3929. (scripts org-use-sub-superscripts)
  3930. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3931. tail sep kws1 prio props ftags drawers beamer-p
  3932. ext-setup-or-nil setup-contents (start 0))
  3933. (save-excursion
  3934. (save-restriction
  3935. (widen)
  3936. (goto-char (point-min))
  3937. (while (or (and ext-setup-or-nil
  3938. (string-match re ext-setup-or-nil start)
  3939. (setq start (match-end 0)))
  3940. (and (setq ext-setup-or-nil nil start 0)
  3941. (re-search-forward re nil t)))
  3942. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3943. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3944. (if (stringp value) (setq value (org-trim value)))
  3945. (cond
  3946. ((equal key "CATEGORY")
  3947. (setq cat value))
  3948. ((member key '("SEQ_TODO" "TODO"))
  3949. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3950. ((equal key "TYP_TODO")
  3951. (push (cons 'type (org-split-string value splitre)) kwds))
  3952. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3953. ;; general TODO-like setup
  3954. (push (cons (intern (downcase (match-string 1 key)))
  3955. (org-split-string value splitre)) kwds))
  3956. ((equal key "TAGS")
  3957. (setq tags (append tags (if tags '("\\n") nil)
  3958. (org-split-string value splitre))))
  3959. ((equal key "COLUMNS")
  3960. (org-set-local 'org-columns-default-format value))
  3961. ((equal key "LINK")
  3962. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3963. (push (cons (match-string 1 value)
  3964. (org-trim (match-string 2 value)))
  3965. links)))
  3966. ((equal key "PRIORITIES")
  3967. (setq prio (org-split-string value " +")))
  3968. ((equal key "PROPERTY")
  3969. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3970. (setq props (org-update-property-plist (match-string 1 value)
  3971. (match-string 2 value)
  3972. props))))
  3973. ((equal key "FILETAGS")
  3974. (when (string-match "\\S-" value)
  3975. (setq ftags
  3976. (append
  3977. ftags
  3978. (apply 'append
  3979. (mapcar (lambda (x) (org-split-string x ":"))
  3980. (org-split-string value)))))))
  3981. ((equal key "DRAWERS")
  3982. (setq drawers (org-split-string value splitre)))
  3983. ((equal key "CONSTANTS")
  3984. (setq const (append const (org-split-string value splitre))))
  3985. ((equal key "STARTUP")
  3986. (let ((opts (org-split-string value splitre))
  3987. l var val)
  3988. (while (setq l (pop opts))
  3989. (when (setq l (assoc l org-startup-options))
  3990. (setq var (nth 1 l) val (nth 2 l))
  3991. (if (not (nth 3 l))
  3992. (set (make-local-variable var) val)
  3993. (if (not (listp (symbol-value var)))
  3994. (set (make-local-variable var) nil))
  3995. (set (make-local-variable var) (symbol-value var))
  3996. (add-to-list var val))))))
  3997. ((equal key "ARCHIVE")
  3998. (setq arch value)
  3999. (remove-text-properties 0 (length arch)
  4000. '(face t fontified t) arch))
  4001. ((equal key "LATEX_CLASS")
  4002. (setq beamer-p (equal value "beamer")))
  4003. ((equal key "OPTIONS")
  4004. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4005. (setq scripts (read (match-string 2 value)))))
  4006. ((equal key "SETUPFILE")
  4007. (setq setup-contents (org-file-contents
  4008. (expand-file-name
  4009. (org-remove-double-quotes value))
  4010. 'noerror))
  4011. (if (not ext-setup-or-nil)
  4012. (setq ext-setup-or-nil setup-contents start 0)
  4013. (setq ext-setup-or-nil
  4014. (concat (substring ext-setup-or-nil 0 start)
  4015. "\n" setup-contents "\n"
  4016. (substring ext-setup-or-nil start)))))))
  4017. ;; search for property blocks
  4018. (goto-char (point-min))
  4019. (while (re-search-forward org-block-regexp nil t)
  4020. (when (equal "PROPERTY" (upcase (match-string 1)))
  4021. (setq value (replace-regexp-in-string
  4022. "[\n\r]" " " (match-string 4)))
  4023. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4024. (setq props (org-update-property-plist (match-string 1 value)
  4025. (match-string 2 value)
  4026. props)))))))
  4027. (org-set-local 'org-use-sub-superscripts scripts)
  4028. (when cat
  4029. (org-set-local 'org-category (intern cat))
  4030. (push (cons "CATEGORY" cat) props))
  4031. (when prio
  4032. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4033. (setq prio (mapcar 'string-to-char prio))
  4034. (org-set-local 'org-highest-priority (nth 0 prio))
  4035. (org-set-local 'org-lowest-priority (nth 1 prio))
  4036. (org-set-local 'org-default-priority (nth 2 prio)))
  4037. (and props (org-set-local 'org-file-properties (nreverse props)))
  4038. (and ftags (org-set-local 'org-file-tags
  4039. (mapcar 'org-add-prop-inherited ftags)))
  4040. (and drawers (org-set-local 'org-drawers drawers))
  4041. (and arch (org-set-local 'org-archive-location arch))
  4042. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4043. ;; Process the TODO keywords
  4044. (unless kwds
  4045. ;; Use the global values as if they had been given locally.
  4046. (setq kwds (default-value 'org-todo-keywords))
  4047. (if (stringp (car kwds))
  4048. (setq kwds (list (cons org-todo-interpretation
  4049. (default-value 'org-todo-keywords)))))
  4050. (setq kwds (reverse kwds)))
  4051. (setq kwds (nreverse kwds))
  4052. (let (inter kws kw)
  4053. (while (setq kws (pop kwds))
  4054. (let ((kws (or
  4055. (run-hook-with-args-until-success
  4056. 'org-todo-setup-filter-hook kws)
  4057. kws)))
  4058. (setq inter (pop kws) sep (member "|" kws)
  4059. kws0 (delete "|" (copy-sequence kws))
  4060. kwsa nil
  4061. kws1 (mapcar
  4062. (lambda (x)
  4063. ;; 1 2
  4064. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4065. (progn
  4066. (setq kw (match-string 1 x)
  4067. key (and (match-end 2) (match-string 2 x))
  4068. log (org-extract-log-state-settings x))
  4069. (push (cons kw (and key (string-to-char key))) kwsa)
  4070. (and log (push log org-todo-log-states))
  4071. kw)
  4072. (error "Invalid TODO keyword %s" x)))
  4073. kws0)
  4074. kwsa (if kwsa (append '((:startgroup))
  4075. (nreverse kwsa)
  4076. '((:endgroup))))
  4077. hw (car kws1)
  4078. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4079. tail (list inter hw (car dws) (org-last dws))))
  4080. (add-to-list 'org-todo-heads hw 'append)
  4081. (push kws1 org-todo-sets)
  4082. (setq org-done-keywords (append org-done-keywords dws nil))
  4083. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4084. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4085. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4086. (setq org-todo-sets (nreverse org-todo-sets)
  4087. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4088. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4089. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4090. ;; Process the constants
  4091. (when const
  4092. (let (e cst)
  4093. (while (setq e (pop const))
  4094. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4095. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4096. (setq org-table-formula-constants-local cst)))
  4097. ;; Process the tags.
  4098. (when tags
  4099. (let (e tgs)
  4100. (while (setq e (pop tags))
  4101. (cond
  4102. ((equal e "{") (push '(:startgroup) tgs))
  4103. ((equal e "}") (push '(:endgroup) tgs))
  4104. ((equal e "\\n") (push '(:newline) tgs))
  4105. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4106. (push (cons (match-string 1 e)
  4107. (string-to-char (match-string 2 e)))
  4108. tgs))
  4109. (t (push (list e) tgs))))
  4110. (org-set-local 'org-tag-alist nil)
  4111. (while (setq e (pop tgs))
  4112. (or (and (stringp (car e))
  4113. (assoc (car e) org-tag-alist))
  4114. (push e org-tag-alist)))))
  4115. ;; Compute the regular expressions and other local variables.
  4116. ;; Using `org-outline-regexp-bol' would complicate them much,
  4117. ;; because of the fixed white space at the end of that string.
  4118. (if (not org-done-keywords)
  4119. (setq org-done-keywords (and org-todo-keywords-1
  4120. (list (org-last org-todo-keywords-1)))))
  4121. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4122. (length org-scheduled-string)
  4123. (length org-clock-string)
  4124. (length org-closed-string)))
  4125. org-drawer-regexp
  4126. (concat "^[ \t]*:\\("
  4127. (mapconcat 'regexp-quote org-drawers "\\|")
  4128. "\\):[ \t]*$")
  4129. org-not-done-keywords
  4130. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4131. org-todo-regexp
  4132. (concat "\\("
  4133. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4134. "\\)")
  4135. org-not-done-regexp
  4136. (concat "\\("
  4137. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4138. "\\)")
  4139. org-not-done-heading-regexp
  4140. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4141. org-todo-line-regexp
  4142. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4143. org-complex-heading-regexp
  4144. (concat "^\\(\\*+\\)"
  4145. "\\(?: +" org-todo-regexp "\\)?"
  4146. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4147. "\\(?: +\\(.*?\\)\\)?"
  4148. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4149. "[ \t]*$")
  4150. org-complex-heading-regexp-format
  4151. (concat "^\\(\\*+\\)"
  4152. "\\(?: +" org-todo-regexp "\\)?"
  4153. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4154. "\\(?: +"
  4155. ;; Stats cookies can be sticked to body.
  4156. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4157. "\\(%s\\)"
  4158. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4159. "\\)"
  4160. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4161. "[ \t]*$")
  4162. org-todo-line-tags-regexp
  4163. (concat "^\\(\\*+\\)"
  4164. "\\(?: +" org-todo-regexp "\\)?"
  4165. "\\(?: +\\(.*?\\)\\)?"
  4166. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4167. "[ \t]*$")
  4168. org-deadline-regexp (concat "\\<" org-deadline-string)
  4169. org-deadline-time-regexp
  4170. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4171. org-deadline-line-regexp
  4172. (concat "\\<\\(" org-deadline-string "\\).*")
  4173. org-scheduled-regexp
  4174. (concat "\\<" org-scheduled-string)
  4175. org-scheduled-time-regexp
  4176. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4177. org-closed-time-regexp
  4178. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4179. org-keyword-time-regexp
  4180. (concat "\\<\\(" org-scheduled-string
  4181. "\\|" org-deadline-string
  4182. "\\|" org-closed-string
  4183. "\\|" org-clock-string "\\)"
  4184. " *[[<]\\([^]>]+\\)[]>]")
  4185. org-keyword-time-not-clock-regexp
  4186. (concat "\\<\\(" org-scheduled-string
  4187. "\\|" org-deadline-string
  4188. "\\|" org-closed-string
  4189. "\\)"
  4190. " *[[<]\\([^]>]+\\)[]>]")
  4191. org-maybe-keyword-time-regexp
  4192. (concat "\\(\\<\\(" org-scheduled-string
  4193. "\\|" org-deadline-string
  4194. "\\|" org-closed-string
  4195. "\\|" org-clock-string "\\)\\)?"
  4196. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4197. org-planning-or-clock-line-re
  4198. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4199. "\\|" org-deadline-string
  4200. "\\|" org-closed-string "\\|" org-clock-string
  4201. "\\)\\>\\)")
  4202. org-all-time-keywords
  4203. (mapcar (lambda (w) (substring w 0 -1))
  4204. (list org-scheduled-string org-deadline-string
  4205. org-clock-string org-closed-string))
  4206. )
  4207. (org-compute-latex-and-specials-regexp)
  4208. (org-set-font-lock-defaults))))
  4209. (defun org-file-contents (file &optional noerror)
  4210. "Return the contents of FILE, as a string."
  4211. (if (or (not file)
  4212. (not (file-readable-p file)))
  4213. (if noerror
  4214. (progn
  4215. (message "Cannot read file \"%s\"" file)
  4216. (ding) (sit-for 2)
  4217. "")
  4218. (error "Cannot read file \"%s\"" file))
  4219. (with-temp-buffer
  4220. (insert-file-contents file)
  4221. (buffer-string))))
  4222. (defun org-extract-log-state-settings (x)
  4223. "Extract the log state setting from a TODO keyword string.
  4224. This will extract info from a string like \"WAIT(w@/!)\"."
  4225. (let (kw key log1 log2)
  4226. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4227. (setq kw (match-string 1 x)
  4228. key (and (match-end 2) (match-string 2 x))
  4229. log1 (and (match-end 3) (match-string 3 x))
  4230. log2 (and (match-end 4) (match-string 4 x)))
  4231. (and (or log1 log2)
  4232. (list kw
  4233. (and log1 (if (equal log1 "!") 'time 'note))
  4234. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4235. (defun org-remove-keyword-keys (list)
  4236. "Remove a pair of parenthesis at the end of each string in LIST."
  4237. (mapcar (lambda (x)
  4238. (if (string-match "(.*)$" x)
  4239. (substring x 0 (match-beginning 0))
  4240. x))
  4241. list))
  4242. (defun org-assign-fast-keys (alist)
  4243. "Assign fast keys to a keyword-key alist.
  4244. Respect keys that are already there."
  4245. (let (new e (alt ?0))
  4246. (while (setq e (pop alist))
  4247. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4248. (cdr e)) ;; Key already assigned.
  4249. (push e new)
  4250. (let ((clist (string-to-list (downcase (car e))))
  4251. (used (append new alist)))
  4252. (when (= (car clist) ?@)
  4253. (pop clist))
  4254. (while (and clist (rassoc (car clist) used))
  4255. (pop clist))
  4256. (unless clist
  4257. (while (rassoc alt used)
  4258. (incf alt)))
  4259. (push (cons (car e) (or (car clist) alt)) new))))
  4260. (nreverse new)))
  4261. ;;; Some variables used in various places
  4262. (defvar org-window-configuration nil
  4263. "Used in various places to store a window configuration.")
  4264. (defvar org-selected-window nil
  4265. "Used in various places to store a window configuration.")
  4266. (defvar org-finish-function nil
  4267. "Function to be called when `C-c C-c' is used.
  4268. This is for getting out of special buffers like remember.")
  4269. ;; FIXME: Occasionally check by commenting these, to make sure
  4270. ;; no other functions uses these, forgetting to let-bind them.
  4271. (defvar entry)
  4272. (defvar last-state)
  4273. (defvar date)
  4274. ;; Defined somewhere in this file, but used before definition.
  4275. (defvar org-entities) ;; defined in org-entities.el
  4276. (defvar org-struct-menu)
  4277. (defvar org-org-menu)
  4278. (defvar org-tbl-menu)
  4279. ;;;; Define the Org-mode
  4280. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4281. (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"))
  4282. ;; We use a before-change function to check if a table might need
  4283. ;; an update.
  4284. (defvar org-table-may-need-update t
  4285. "Indicates that a table might need an update.
  4286. This variable is set by `org-before-change-function'.
  4287. `org-table-align' sets it back to nil.")
  4288. (defun org-before-change-function (beg end)
  4289. "Every change indicates that a table might need an update."
  4290. (setq org-table-may-need-update t))
  4291. (defvar org-mode-map)
  4292. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4293. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4294. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4295. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4296. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4297. (defvar org-table-buffer-is-an nil)
  4298. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  4299. ;; some places -- e.g. see the macro org-with-limited-levels.
  4300. ;;
  4301. ;; In Org buffers, the value of `outline-regexp' is that of
  4302. ;; `org-outline-regexp'. The only function still directly relying on
  4303. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  4304. ;; job when `orgstruct-mode' is active.
  4305. (defvar org-outline-regexp "\\*+ "
  4306. "Regexp to match Org headlines.")
  4307. (defconst org-outline-regexp-bol "^\\*+ "
  4308. "Regexp to match Org headlines.
  4309. This is similar to `org-outline-regexp' but additionally makes
  4310. sure that we are at the beginning of the line.")
  4311. (defconst org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4312. "Matches an headline, putting stars and text into groups.
  4313. Stars are put in group 1 and the trimmed body in group 2.")
  4314. ;;;###autoload
  4315. (define-derived-mode org-mode outline-mode "Org"
  4316. "Outline-based notes management and organizer, alias
  4317. \"Carsten's outline-mode for keeping track of everything.\"
  4318. Org-mode develops organizational tasks around a NOTES file which
  4319. contains information about projects as plain text. Org-mode is
  4320. implemented on top of outline-mode, which is ideal to keep the content
  4321. of large files well structured. It supports ToDo items, deadlines and
  4322. time stamps, which magically appear in the diary listing of the Emacs
  4323. calendar. Tables are easily created with a built-in table editor.
  4324. Plain text URL-like links connect to websites, emails (VM), Usenet
  4325. messages (Gnus), BBDB entries, and any files related to the project.
  4326. For printing and sharing of notes, an Org-mode file (or a part of it)
  4327. can be exported as a structured ASCII or HTML file.
  4328. The following commands are available:
  4329. \\{org-mode-map}"
  4330. ;; Get rid of Outline menus, they are not needed
  4331. ;; Need to do this here because define-derived-mode sets up
  4332. ;; the keymap so late. Still, it is a waste to call this each time
  4333. ;; we switch another buffer into org-mode.
  4334. (if (featurep 'xemacs)
  4335. (when (boundp 'outline-mode-menu-heading)
  4336. ;; Assume this is Greg's port, it uses easymenu
  4337. (easy-menu-remove outline-mode-menu-heading)
  4338. (easy-menu-remove outline-mode-menu-show)
  4339. (easy-menu-remove outline-mode-menu-hide))
  4340. (define-key org-mode-map [menu-bar headings] 'undefined)
  4341. (define-key org-mode-map [menu-bar hide] 'undefined)
  4342. (define-key org-mode-map [menu-bar show] 'undefined))
  4343. (org-load-modules-maybe)
  4344. (easy-menu-add org-org-menu)
  4345. (easy-menu-add org-tbl-menu)
  4346. (org-install-agenda-files-menu)
  4347. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4348. (add-to-invisibility-spec '(org-cwidth))
  4349. (add-to-invisibility-spec '(org-hide-block . t))
  4350. (when (featurep 'xemacs)
  4351. (org-set-local 'line-move-ignore-invisible t))
  4352. (org-set-local 'outline-regexp org-outline-regexp)
  4353. (org-set-local 'outline-level 'org-outline-level)
  4354. (setq bidi-paragraph-direction 'left-to-right)
  4355. (when (and org-ellipsis
  4356. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4357. (fboundp 'make-glyph-code))
  4358. (unless org-display-table
  4359. (setq org-display-table (make-display-table)))
  4360. (set-display-table-slot
  4361. org-display-table 4
  4362. (vconcat (mapcar
  4363. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4364. org-ellipsis)))
  4365. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4366. (setq buffer-display-table org-display-table))
  4367. (org-set-regexps-and-options)
  4368. (when (and org-tag-faces (not org-tags-special-faces-re))
  4369. ;; tag faces set outside customize.... force initialization.
  4370. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4371. ;; Calc embedded
  4372. (org-set-local 'calc-embedded-open-mode "# ")
  4373. (modify-syntax-entry ?@ "w")
  4374. (if org-startup-truncated (setq truncate-lines t))
  4375. (org-set-local 'font-lock-unfontify-region-function
  4376. 'org-unfontify-region)
  4377. ;; Activate before-change-function
  4378. (org-set-local 'org-table-may-need-update t)
  4379. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4380. 'local)
  4381. ;; Check for running clock before killing a buffer
  4382. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4383. ;; Paragraphs and auto-filling
  4384. (org-set-autofill-regexps)
  4385. (setq indent-line-function 'org-indent-line-function)
  4386. (org-update-radio-target-regexp)
  4387. ;; Beginning/end of defun
  4388. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4389. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4390. ;; Next error for sparse trees
  4391. (org-set-local 'next-error-function 'org-occur-next-match)
  4392. ;; Make sure dependence stuff works reliably, even for users who set it
  4393. ;; too late :-(
  4394. (if org-enforce-todo-dependencies
  4395. (add-hook 'org-blocker-hook
  4396. 'org-block-todo-from-children-or-siblings-or-parent)
  4397. (remove-hook 'org-blocker-hook
  4398. 'org-block-todo-from-children-or-siblings-or-parent))
  4399. (if org-enforce-todo-checkbox-dependencies
  4400. (add-hook 'org-blocker-hook
  4401. 'org-block-todo-from-checkboxes)
  4402. (remove-hook 'org-blocker-hook
  4403. 'org-block-todo-from-checkboxes))
  4404. ;; Comment characters
  4405. (org-set-local 'comment-start "#")
  4406. (org-set-local 'comment-padding " ")
  4407. ;; Align options lines
  4408. (org-set-local
  4409. 'align-mode-rules-list
  4410. '((org-in-buffer-settings
  4411. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4412. (modes . '(org-mode)))))
  4413. ;; Imenu
  4414. (org-set-local 'imenu-create-index-function
  4415. 'org-imenu-get-tree)
  4416. ;; Make isearch reveal context
  4417. (if (or (featurep 'xemacs)
  4418. (not (boundp 'outline-isearch-open-invisible-function)))
  4419. ;; Emacs 21 and XEmacs make use of the hook
  4420. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4421. ;; Emacs 22 deals with this through a special variable
  4422. (org-set-local 'outline-isearch-open-invisible-function
  4423. (lambda (&rest ignore) (org-show-context 'isearch))))
  4424. ;; Turn on org-beamer-mode?
  4425. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4426. ;; Setup the pcomplete hooks
  4427. (set (make-local-variable 'pcomplete-command-completion-function)
  4428. 'org-pcomplete-initial)
  4429. (set (make-local-variable 'pcomplete-command-name-function)
  4430. 'org-command-at-point)
  4431. (set (make-local-variable 'pcomplete-default-completion-function)
  4432. 'ignore)
  4433. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4434. 'org-parse-arguments)
  4435. (set (make-local-variable 'pcomplete-termination-string) "")
  4436. (set (make-local-variable 'face-remapping-alist)
  4437. '((default org-default)))
  4438. ;; If empty file that did not turn on org-mode automatically, make it to.
  4439. (if (and org-insert-mode-line-in-empty-file
  4440. (org-called-interactively-p 'any)
  4441. (= (point-min) (point-max)))
  4442. (insert "# -*- mode: org -*-\n\n"))
  4443. (unless org-inhibit-startup
  4444. (when org-startup-align-all-tables
  4445. (let ((bmp (buffer-modified-p)))
  4446. (org-table-map-tables 'org-table-align 'quietly)
  4447. (set-buffer-modified-p bmp)))
  4448. (when org-startup-with-inline-images
  4449. (org-display-inline-images))
  4450. (when org-startup-indented
  4451. (require 'org-indent)
  4452. (org-indent-mode 1))
  4453. (unless org-inhibit-startup-visibility-stuff
  4454. (org-set-startup-visibility))))
  4455. (when (fboundp 'abbrev-table-put)
  4456. (abbrev-table-put org-mode-abbrev-table
  4457. :parents (list text-mode-abbrev-table)))
  4458. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4459. (defun org-current-time ()
  4460. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4461. (if (> (car org-time-stamp-rounding-minutes) 1)
  4462. (let ((r (car org-time-stamp-rounding-minutes))
  4463. (time (decode-time)))
  4464. (apply 'encode-time
  4465. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4466. (nthcdr 2 time))))
  4467. (current-time)))
  4468. (defun org-today ()
  4469. "Return today date, considering `org-extend-today-until'."
  4470. (time-to-days
  4471. (time-subtract (current-time)
  4472. (list 0 (* 3600 org-extend-today-until) 0))))
  4473. ;;;; Font-Lock stuff, including the activators
  4474. (defvar org-mouse-map (make-sparse-keymap))
  4475. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4476. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4477. (when org-mouse-1-follows-link
  4478. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4479. (when org-tab-follows-link
  4480. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4481. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4482. (require 'font-lock)
  4483. (defconst org-non-link-chars "]\t\n\r<>")
  4484. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4485. "shell" "elisp" "doi" "message"))
  4486. (defvar org-link-types-re nil
  4487. "Matches a link that has a url-like prefix like \"http:\"")
  4488. (defvar org-link-re-with-space nil
  4489. "Matches a link with spaces, optional angular brackets around it.")
  4490. (defvar org-link-re-with-space2 nil
  4491. "Matches a link with spaces, optional angular brackets around it.")
  4492. (defvar org-link-re-with-space3 nil
  4493. "Matches a link with spaces, only for internal part in bracket links.")
  4494. (defvar org-angle-link-re nil
  4495. "Matches link with angular brackets, spaces are allowed.")
  4496. (defvar org-plain-link-re nil
  4497. "Matches plain link, without spaces.")
  4498. (defvar org-bracket-link-regexp nil
  4499. "Matches a link in double brackets.")
  4500. (defvar org-bracket-link-analytic-regexp nil
  4501. "Regular expression used to analyze links.
  4502. Here is what the match groups contain after a match:
  4503. 1: http:
  4504. 2: http
  4505. 3: path
  4506. 4: [desc]
  4507. 5: desc")
  4508. (defvar org-bracket-link-analytic-regexp++ nil
  4509. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4510. (defvar org-any-link-re nil
  4511. "Regular expression matching any link.")
  4512. (defcustom org-match-sexp-depth 3
  4513. "Number of stacked braces for sub/superscript matching.
  4514. This has to be set before loading org.el to be effective."
  4515. :group 'org-export-translation ; ??????????????????????????/
  4516. :type 'integer)
  4517. (defun org-create-multibrace-regexp (left right n)
  4518. "Create a regular expression which will match a balanced sexp.
  4519. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4520. as single character strings.
  4521. The regexp returned will match the entire expression including the
  4522. delimiters. It will also define a single group which contains the
  4523. match except for the outermost delimiters. The maximum depth of
  4524. stacked delimiters is N. Escaping delimiters is not possible."
  4525. (let* ((nothing (concat "[^" left right "]*?"))
  4526. (or "\\|")
  4527. (re nothing)
  4528. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4529. (while (> n 1)
  4530. (setq n (1- n)
  4531. re (concat re or next)
  4532. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4533. (concat left "\\(" re "\\)" right)))
  4534. (defvar org-match-substring-regexp
  4535. (concat
  4536. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4537. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4538. "\\|"
  4539. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4540. "\\|"
  4541. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4542. "The regular expression matching a sub- or superscript.")
  4543. (defvar org-match-substring-with-braces-regexp
  4544. (concat
  4545. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4546. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4547. "\\)")
  4548. "The regular expression matching a sub- or superscript, forcing braces.")
  4549. (defun org-make-link-regexps ()
  4550. "Update the link regular expressions.
  4551. This should be called after the variable `org-link-types' has changed."
  4552. (setq org-link-types-re
  4553. (concat
  4554. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4555. org-link-re-with-space
  4556. (concat
  4557. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4558. "\\([^" org-non-link-chars " ]"
  4559. "[^" org-non-link-chars "]*"
  4560. "[^" org-non-link-chars " ]\\)>?")
  4561. org-link-re-with-space2
  4562. (concat
  4563. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4564. "\\([^" org-non-link-chars " ]"
  4565. "[^\t\n\r]*"
  4566. "[^" org-non-link-chars " ]\\)>?")
  4567. org-link-re-with-space3
  4568. (concat
  4569. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4570. "\\([^" org-non-link-chars " ]"
  4571. "[^\t\n\r]*\\)")
  4572. org-angle-link-re
  4573. (concat
  4574. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4575. "\\([^" org-non-link-chars " ]"
  4576. "[^" org-non-link-chars "]*"
  4577. "\\)>")
  4578. org-plain-link-re
  4579. (concat
  4580. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4581. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4582. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4583. org-bracket-link-regexp
  4584. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4585. org-bracket-link-analytic-regexp
  4586. (concat
  4587. "\\[\\["
  4588. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4589. "\\([^]]+\\)"
  4590. "\\]"
  4591. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4592. "\\]")
  4593. org-bracket-link-analytic-regexp++
  4594. (concat
  4595. "\\[\\["
  4596. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4597. "\\([^]]+\\)"
  4598. "\\]"
  4599. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4600. "\\]")
  4601. org-any-link-re
  4602. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4603. org-angle-link-re "\\)\\|\\("
  4604. org-plain-link-re "\\)")))
  4605. (org-make-link-regexps)
  4606. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4607. "Regular expression for fast time stamp matching.")
  4608. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4609. "Regular expression for fast time stamp matching.")
  4610. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4611. "Regular expression matching time strings for analysis.
  4612. This one does not require the space after the date, so it can be used
  4613. on a string that terminates immediately after the date.")
  4614. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4615. "Regular expression matching time strings for analysis.")
  4616. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4617. "Regular expression matching time stamps, with groups.")
  4618. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4619. "Regular expression matching time stamps (also [..]), with groups.")
  4620. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4621. "Regular expression matching a time stamp range.")
  4622. (defconst org-tr-regexp-both
  4623. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4624. "Regular expression matching a time stamp range.")
  4625. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4626. org-ts-regexp "\\)?")
  4627. "Regular expression matching a time stamp or time stamp range.")
  4628. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4629. org-ts-regexp-both "\\)?")
  4630. "Regular expression matching a time stamp or time stamp range.
  4631. The time stamps may be either active or inactive.")
  4632. (defvar org-emph-face nil)
  4633. (defun org-do-emphasis-faces (limit)
  4634. "Run through the buffer and add overlays to emphasised strings."
  4635. (let (rtn a)
  4636. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4637. (if (not (= (char-after (match-beginning 3))
  4638. (char-after (match-beginning 4))))
  4639. (progn
  4640. (setq rtn t)
  4641. (setq a (assoc (match-string 3) org-emphasis-alist))
  4642. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4643. 'face
  4644. (nth 1 a))
  4645. (and (nth 4 a)
  4646. (org-remove-flyspell-overlays-in
  4647. (match-beginning 0) (match-end 0)))
  4648. (add-text-properties (match-beginning 2) (match-end 2)
  4649. '(font-lock-multiline t org-emphasis t))
  4650. (when org-hide-emphasis-markers
  4651. (add-text-properties (match-end 4) (match-beginning 5)
  4652. '(invisible org-link))
  4653. (add-text-properties (match-beginning 3) (match-end 3)
  4654. '(invisible org-link)))))
  4655. (backward-char 1))
  4656. rtn))
  4657. (defun org-emphasize (&optional char)
  4658. "Insert or change an emphasis, i.e. a font like bold or italic.
  4659. If there is an active region, change that region to a new emphasis.
  4660. If there is no region, just insert the marker characters and position
  4661. the cursor between them.
  4662. CHAR should be either the marker character, or the first character of the
  4663. HTML tag associated with that emphasis. If CHAR is a space, the means
  4664. to remove the emphasis of the selected region.
  4665. If char is not given (for example in an interactive call) it
  4666. will be prompted for."
  4667. (interactive)
  4668. (let ((eal org-emphasis-alist) e det
  4669. (erc org-emphasis-regexp-components)
  4670. (prompt "")
  4671. (string "") beg end move tag c s)
  4672. (if (org-region-active-p)
  4673. (setq beg (region-beginning) end (region-end)
  4674. string (buffer-substring beg end))
  4675. (setq move t))
  4676. (while (setq e (pop eal))
  4677. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4678. c (aref tag 0))
  4679. (push (cons c (string-to-char (car e))) det)
  4680. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4681. (substring tag 1)))))
  4682. (setq det (nreverse det))
  4683. (unless char
  4684. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4685. (setq char (read-char-exclusive)))
  4686. (setq char (or (cdr (assoc char det)) char))
  4687. (if (equal char ?\ )
  4688. (setq s "" move nil)
  4689. (unless (assoc (char-to-string char) org-emphasis-alist)
  4690. (error "No such emphasis marker: \"%c\"" char))
  4691. (setq s (char-to-string char)))
  4692. (while (and (> (length string) 1)
  4693. (equal (substring string 0 1) (substring string -1))
  4694. (assoc (substring string 0 1) org-emphasis-alist))
  4695. (setq string (substring string 1 -1)))
  4696. (setq string (concat s string s))
  4697. (if beg (delete-region beg end))
  4698. (unless (or (bolp)
  4699. (string-match (concat "[" (nth 0 erc) "\n]")
  4700. (char-to-string (char-before (point)))))
  4701. (insert " "))
  4702. (unless (or (eobp)
  4703. (string-match (concat "[" (nth 1 erc) "\n]")
  4704. (char-to-string (char-after (point)))))
  4705. (insert " ") (backward-char 1))
  4706. (insert string)
  4707. (and move (backward-char 1))))
  4708. (defconst org-nonsticky-props
  4709. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4710. (defsubst org-rear-nonsticky-at (pos)
  4711. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4712. (defun org-activate-plain-links (limit)
  4713. "Run through the buffer and add overlays to links."
  4714. (catch 'exit
  4715. (let (f)
  4716. (if (re-search-forward org-plain-link-re limit t)
  4717. (progn
  4718. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4719. (setq f (get-text-property (match-beginning 0) 'face))
  4720. (if (or (eq f 'org-tag)
  4721. (and (listp f) (memq 'org-tag f)))
  4722. nil
  4723. (add-text-properties (match-beginning 0) (match-end 0)
  4724. (list 'mouse-face 'highlight
  4725. 'face 'org-link
  4726. 'keymap org-mouse-map))
  4727. (org-rear-nonsticky-at (match-end 0)))
  4728. t)))))
  4729. (defun org-activate-code (limit)
  4730. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4731. (progn
  4732. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4733. (remove-text-properties (match-beginning 0) (match-end 0)
  4734. '(display t invisible t intangible t))
  4735. t)))
  4736. (defcustom org-src-fontify-natively nil
  4737. "When non-nil, fontify code in code blocks."
  4738. :type 'boolean
  4739. :group 'org-appearance
  4740. :group 'org-babel)
  4741. (defun org-fontify-meta-lines-and-blocks (limit)
  4742. (condition-case nil
  4743. (org-fontify-meta-lines-and-blocks-1 limit)
  4744. (error (message "org-mode fontification error"))))
  4745. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4746. "Fontify #+ lines and blocks, in the correct ways."
  4747. (let ((case-fold-search t))
  4748. (if (re-search-forward
  4749. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4750. limit t)
  4751. (let ((beg (match-beginning 0))
  4752. (block-start (match-end 0))
  4753. (block-end nil)
  4754. (lang (match-string 7))
  4755. (beg1 (line-beginning-position 2))
  4756. (dc1 (downcase (match-string 2)))
  4757. (dc3 (downcase (match-string 3)))
  4758. end end1 quoting block-type ovl)
  4759. (cond
  4760. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4761. ;; a single line of backend-specific content
  4762. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4763. (remove-text-properties (match-beginning 0) (match-end 0)
  4764. '(display t invisible t intangible t))
  4765. (add-text-properties (match-beginning 1) (match-end 3)
  4766. '(font-lock-fontified t face org-meta-line))
  4767. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4768. '(font-lock-fontified t face org-block))
  4769. ; for backend-specific code
  4770. t)
  4771. ((and (match-end 4) (equal dc3 "begin"))
  4772. ;; Truly a block
  4773. (setq block-type (downcase (match-string 5))
  4774. quoting (member block-type org-protecting-blocks))
  4775. (when (re-search-forward
  4776. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4777. nil t) ;; on purpose, we look further than LIMIT
  4778. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4779. (setq block-end (match-beginning 0))
  4780. (when quoting
  4781. (remove-text-properties beg end
  4782. '(display t invisible t intangible t)))
  4783. (add-text-properties
  4784. beg end
  4785. '(font-lock-fontified t font-lock-multiline t))
  4786. (add-text-properties beg beg1 '(face org-meta-line))
  4787. (add-text-properties end1 (min (point-max) (1+ end))
  4788. '(face org-meta-line)) ; for end_src
  4789. (cond
  4790. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4791. (org-src-font-lock-fontify-block lang block-start block-end)
  4792. ;; remove old background overlays
  4793. (mapc (lambda (ov)
  4794. (if (eq (overlay-get ov 'face) 'org-block-background)
  4795. (delete-overlay ov)))
  4796. (overlays-at (/ (+ beg1 block-end) 2)))
  4797. ;; add a background overlay
  4798. (setq ovl (make-overlay beg1 block-end))
  4799. (overlay-put ovl 'face 'org-block-background)
  4800. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4801. (quoting
  4802. (add-text-properties beg1 (min (point-max) (1+ end1))
  4803. '(face org-block))) ; end of source block
  4804. ((not org-fontify-quote-and-verse-blocks))
  4805. ((string= block-type "quote")
  4806. (add-text-properties beg1 (1+ end1) '(face org-quote)))
  4807. ((string= block-type "verse")
  4808. (add-text-properties beg1 (1+ end1) '(face org-verse))))
  4809. (add-text-properties beg beg1 '(face org-block-begin-line))
  4810. (add-text-properties (1+ end) (1+ end1) '(face org-block-end-line))
  4811. t))
  4812. ((member dc1 '("title:" "author:" "email:" "date:"))
  4813. (add-text-properties
  4814. beg (match-end 3)
  4815. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4816. '(font-lock-fontified t invisible t)
  4817. '(font-lock-fontified t face org-document-info-keyword)))
  4818. (add-text-properties
  4819. (match-beginning 6) (match-end 6)
  4820. (if (string-equal dc1 "title:")
  4821. '(font-lock-fontified t face org-document-title)
  4822. '(font-lock-fontified t face org-document-info))))
  4823. ((not (member (char-after beg) '(?\ ?\t)))
  4824. ;; just any other in-buffer setting, but not indented
  4825. (add-text-properties
  4826. beg (1+ (match-end 0))
  4827. '(font-lock-fontified t face org-meta-line))
  4828. t)
  4829. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4830. "orgtbl:" "tblfm:" "tblname:" "results:"
  4831. "call:" "header:" "headers:" "name:"))
  4832. (and (match-end 4) (equal dc3 "attr")))
  4833. (add-text-properties
  4834. beg (match-end 0)
  4835. '(font-lock-fontified t face org-meta-line))
  4836. t)
  4837. ((member dc3 '(" " ""))
  4838. (add-text-properties
  4839. beg (match-end 0)
  4840. '(font-lock-fontified t face font-lock-comment-face)))
  4841. (t nil))))))
  4842. (defun org-activate-angle-links (limit)
  4843. "Run through the buffer and add overlays to links."
  4844. (if (re-search-forward org-angle-link-re limit t)
  4845. (progn
  4846. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4847. (add-text-properties (match-beginning 0) (match-end 0)
  4848. (list 'mouse-face 'highlight
  4849. 'keymap org-mouse-map))
  4850. (org-rear-nonsticky-at (match-end 0))
  4851. t)))
  4852. (defun org-activate-footnote-links (limit)
  4853. "Run through the buffer and add overlays to footnotes."
  4854. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4855. (when fn
  4856. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4857. (org-remove-flyspell-overlays-in beg end)
  4858. (add-text-properties beg end
  4859. (list 'mouse-face 'highlight
  4860. 'keymap org-mouse-map
  4861. 'help-echo
  4862. (if (= (point-at-bol) beg)
  4863. "Footnote definition"
  4864. "Footnote reference")
  4865. 'font-lock-fontified t
  4866. 'font-lock-multiline t
  4867. 'face 'org-footnote))))))
  4868. (defun org-activate-bracket-links (limit)
  4869. "Run through the buffer and add overlays to bracketed links."
  4870. (if (re-search-forward org-bracket-link-regexp limit t)
  4871. (let* ((help (concat "LINK: "
  4872. (org-match-string-no-properties 1)))
  4873. ;; FIXME: above we should remove the escapes.
  4874. ;; but that requires another match, protecting match data,
  4875. ;; a lot of overhead for font-lock.
  4876. (ip (org-maybe-intangible
  4877. (list 'invisible 'org-link
  4878. 'keymap org-mouse-map 'mouse-face 'highlight
  4879. 'font-lock-multiline t 'help-echo help)))
  4880. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4881. 'font-lock-multiline t 'help-echo help)))
  4882. ;; We need to remove the invisible property here. Table narrowing
  4883. ;; may have made some of this invisible.
  4884. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4885. (remove-text-properties (match-beginning 0) (match-end 0)
  4886. '(invisible nil))
  4887. (if (match-end 3)
  4888. (progn
  4889. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4890. (org-rear-nonsticky-at (match-beginning 3))
  4891. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4892. (org-rear-nonsticky-at (match-end 3))
  4893. (add-text-properties (match-end 3) (match-end 0) ip)
  4894. (org-rear-nonsticky-at (match-end 0)))
  4895. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4896. (org-rear-nonsticky-at (match-beginning 1))
  4897. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4898. (org-rear-nonsticky-at (match-end 1))
  4899. (add-text-properties (match-end 1) (match-end 0) ip)
  4900. (org-rear-nonsticky-at (match-end 0)))
  4901. t)))
  4902. (defun org-activate-dates (limit)
  4903. "Run through the buffer and add overlays to dates."
  4904. (if (re-search-forward org-tsr-regexp-both limit t)
  4905. (progn
  4906. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4907. (add-text-properties (match-beginning 0) (match-end 0)
  4908. (list 'mouse-face 'highlight
  4909. 'keymap org-mouse-map))
  4910. (org-rear-nonsticky-at (match-end 0))
  4911. (when org-display-custom-times
  4912. (if (match-end 3)
  4913. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4914. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4915. t)))
  4916. (defvar org-target-link-regexp nil
  4917. "Regular expression matching radio targets in plain text.")
  4918. (make-variable-buffer-local 'org-target-link-regexp)
  4919. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4920. "Regular expression matching a link target.")
  4921. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4922. "Regular expression matching a radio target.")
  4923. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4924. "Regular expression matching any target.")
  4925. (defun org-activate-target-links (limit)
  4926. "Run through the buffer and add overlays to target matches."
  4927. (when org-target-link-regexp
  4928. (let ((case-fold-search t))
  4929. (if (re-search-forward org-target-link-regexp limit t)
  4930. (progn
  4931. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4932. (add-text-properties (match-beginning 0) (match-end 0)
  4933. (list 'mouse-face 'highlight
  4934. 'keymap org-mouse-map
  4935. 'help-echo "Radio target link"
  4936. 'org-linked-text t))
  4937. (org-rear-nonsticky-at (match-end 0))
  4938. t)))))
  4939. (defun org-update-radio-target-regexp ()
  4940. "Find all radio targets in this file and update the regular expression."
  4941. (interactive)
  4942. (when (memq 'radio org-activate-links)
  4943. (setq org-target-link-regexp
  4944. (org-make-target-link-regexp (org-all-targets 'radio)))
  4945. (org-restart-font-lock)))
  4946. (defun org-hide-wide-columns (limit)
  4947. (let (s e)
  4948. (setq s (text-property-any (point) (or limit (point-max))
  4949. 'org-cwidth t))
  4950. (when s
  4951. (setq e (next-single-property-change s 'org-cwidth))
  4952. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4953. (goto-char e)
  4954. t)))
  4955. (defvar org-latex-and-specials-regexp nil
  4956. "Regular expression for highlighting export special stuff.")
  4957. (defvar org-match-substring-regexp)
  4958. (defvar org-match-substring-with-braces-regexp)
  4959. ;; This should be with the exporter code, but we also use if for font-locking
  4960. (defconst org-export-html-special-string-regexps
  4961. '(("\\\\-" . "&shy;")
  4962. ("---\\([^-]\\)" . "&mdash;\\1")
  4963. ("--\\([^-]\\)" . "&ndash;\\1")
  4964. ("\\.\\.\\." . "&hellip;"))
  4965. "Regular expressions for special string conversion.")
  4966. (defun org-compute-latex-and-specials-regexp ()
  4967. "Compute regular expression for stuff treated specially by exporters."
  4968. (if (not org-highlight-latex-fragments-and-specials)
  4969. (org-set-local 'org-latex-and-specials-regexp nil)
  4970. (require 'org-exp)
  4971. (let*
  4972. ((matchers (plist-get org-format-latex-options :matchers))
  4973. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4974. org-latex-regexps)))
  4975. (org-export-allow-BIND nil)
  4976. (options (org-combine-plists (org-default-export-plist)
  4977. (org-infile-export-plist)))
  4978. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4979. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4980. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4981. (org-export-html-expand (plist-get options :expand-quoted-html))
  4982. (org-export-with-special-strings (plist-get options :special-strings))
  4983. (re-sub
  4984. (cond
  4985. ((equal org-export-with-sub-superscripts '{})
  4986. (list org-match-substring-with-braces-regexp))
  4987. (org-export-with-sub-superscripts
  4988. (list org-match-substring-regexp))
  4989. (t nil)))
  4990. (re-latex
  4991. (if org-export-with-LaTeX-fragments
  4992. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4993. (re-macros
  4994. (if org-export-with-TeX-macros
  4995. (list (concat "\\\\"
  4996. (regexp-opt
  4997. (append
  4998. (delq nil
  4999. (mapcar 'car-safe
  5000. (append org-entities-user
  5001. org-entities)))
  5002. (if (boundp 'org-latex-entities)
  5003. (mapcar (lambda (x)
  5004. (or (car-safe x) x))
  5005. org-latex-entities)
  5006. nil))
  5007. 'words))) ; FIXME
  5008. ))
  5009. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5010. (re-special (if org-export-with-special-strings
  5011. (mapcar (lambda (x) (car x))
  5012. org-export-html-special-string-regexps)))
  5013. (re-rest
  5014. (delq nil
  5015. (list
  5016. (if org-export-html-expand "@<[^>\n]+>")
  5017. ))))
  5018. (org-set-local
  5019. 'org-latex-and-specials-regexp
  5020. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5021. re-rest) "\\|")))))
  5022. (defun org-do-latex-and-special-faces (limit)
  5023. "Run through the buffer and add overlays to links."
  5024. (when org-latex-and-specials-regexp
  5025. (let (rtn d)
  5026. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5027. limit t))
  5028. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5029. 'face))
  5030. '(org-code org-verbatim underline)))
  5031. (progn
  5032. (setq rtn t
  5033. d (cond ((member (char-after (1+ (match-beginning 0)))
  5034. '(?_ ?^)) 1)
  5035. (t 0)))
  5036. (font-lock-prepend-text-property
  5037. (+ d (match-beginning 0)) (match-end 0)
  5038. 'face 'org-latex-and-export-specials)
  5039. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5040. '(font-lock-multiline t)))))
  5041. rtn)))
  5042. (defun org-restart-font-lock ()
  5043. "Restart `font-lock-mode', to force refontification."
  5044. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5045. (font-lock-mode -1)
  5046. (font-lock-mode 1)))
  5047. (defun org-all-targets (&optional radio)
  5048. "Return a list of all targets in this file.
  5049. With optional argument RADIO, only find radio targets."
  5050. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  5051. rtn)
  5052. (save-excursion
  5053. (goto-char (point-min))
  5054. (while (re-search-forward re nil t)
  5055. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  5056. rtn)))
  5057. (defun org-make-target-link-regexp (targets)
  5058. "Make regular expression matching all strings in TARGETS.
  5059. The regular expression finds the targets also if there is a line break
  5060. between words."
  5061. (and targets
  5062. (concat
  5063. "\\<\\("
  5064. (mapconcat
  5065. (lambda (x)
  5066. (setq x (regexp-quote x))
  5067. (while (string-match " +" x)
  5068. (setq x (replace-match "\\s-+" t t x)))
  5069. x)
  5070. targets
  5071. "\\|")
  5072. "\\)\\>")))
  5073. (defun org-activate-tags (limit)
  5074. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5075. (progn
  5076. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5077. (add-text-properties (match-beginning 1) (match-end 1)
  5078. (list 'mouse-face 'highlight
  5079. 'keymap org-mouse-map))
  5080. (org-rear-nonsticky-at (match-end 1))
  5081. t)))
  5082. (defun org-outline-level ()
  5083. "Compute the outline level of the heading at point.
  5084. This function assumes that the cursor is at the beginning of a line matched
  5085. by `outline-regexp'. Otherwise it returns garbage.
  5086. If this is called at a normal headline, the level is the number of stars.
  5087. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5088. (save-excursion
  5089. (looking-at org-outline-regexp)
  5090. (1- (- (match-end 0) (match-beginning 0)))))
  5091. (defvar org-font-lock-keywords nil)
  5092. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5093. "Regular expression matching a property line.")
  5094. (defvar org-font-lock-hook nil
  5095. "Functions to be called for special font lock stuff.")
  5096. (defvar org-font-lock-set-keywords-hook nil
  5097. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5098. This is calles after `org-font-lock-extra-keywords' is defined, but before
  5099. it is installed to be used by font lock. This can be useful if something
  5100. needs to be inserted at a specific position in the font-lock sequence.")
  5101. (defun org-font-lock-hook (limit)
  5102. (run-hook-with-args 'org-font-lock-hook limit))
  5103. (defun org-set-font-lock-defaults ()
  5104. (let* ((em org-fontify-emphasized-text)
  5105. (lk org-activate-links)
  5106. (org-font-lock-extra-keywords
  5107. (list
  5108. ;; Call the hook
  5109. '(org-font-lock-hook)
  5110. ;; Headlines
  5111. `(,(if org-fontify-whole-heading-line
  5112. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5113. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5114. (1 (org-get-level-face 1))
  5115. (2 (org-get-level-face 2))
  5116. (3 (org-get-level-face 3)))
  5117. ;; Table lines
  5118. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5119. (1 'org-table t))
  5120. ;; Table internals
  5121. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5122. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5123. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5124. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5125. ;; Drawers
  5126. (list org-drawer-regexp '(0 'org-special-keyword t))
  5127. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5128. ;; Properties
  5129. (list org-property-re
  5130. '(1 'org-special-keyword t)
  5131. '(3 'org-property-value t))
  5132. ;; Links
  5133. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5134. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5135. (if (memq 'plain lk) '(org-activate-plain-links))
  5136. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5137. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5138. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5139. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5140. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5141. '(org-hide-wide-columns (0 nil append))
  5142. ;; TODO keyword
  5143. (list (format org-heading-keyword-regexp-format
  5144. org-todo-regexp)
  5145. '(2 (org-get-todo-face 2) t))
  5146. ;; DONE
  5147. (if org-fontify-done-headline
  5148. (list (format org-heading-keyword-regexp-format
  5149. (concat
  5150. "\\(?:"
  5151. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5152. "\\)"))
  5153. '(2 'org-headline-done t))
  5154. nil)
  5155. ;; Priorities
  5156. '(org-font-lock-add-priority-faces)
  5157. ;; Tags
  5158. '(org-font-lock-add-tag-faces)
  5159. ;; Special keywords
  5160. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5161. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5162. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5163. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5164. ;; Emphasis
  5165. (if em
  5166. (if (featurep 'xemacs)
  5167. '(org-do-emphasis-faces (0 nil append))
  5168. '(org-do-emphasis-faces)))
  5169. ;; Checkboxes
  5170. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5171. 1 'org-checkbox prepend)
  5172. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5173. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5174. (0 (org-get-checkbox-statistics-face) t)))
  5175. ;; Description list items
  5176. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5177. 1 'bold prepend)
  5178. ;; ARCHIVEd headings
  5179. (list (concat
  5180. org-outline-regexp-bol
  5181. "\\(.*:" org-archive-tag ":.*\\)")
  5182. '(1 'org-archived prepend))
  5183. ;; Specials
  5184. '(org-do-latex-and-special-faces)
  5185. '(org-fontify-entities)
  5186. '(org-raise-scripts)
  5187. ;; Code
  5188. '(org-activate-code (1 'org-code t))
  5189. ;; COMMENT
  5190. (list (format org-heading-keyword-regexp-format
  5191. (concat "\\("
  5192. org-comment-string "\\|" org-quote-string
  5193. "\\)"))
  5194. '(2 'org-special-keyword t))
  5195. '("^#.*" (0 'font-lock-comment-face t))
  5196. ;; Blocks and meta lines
  5197. '(org-fontify-meta-lines-and-blocks)
  5198. )))
  5199. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5200. (run-hooks 'org-font-lock-set-keywords-hook)
  5201. ;; Now set the full font-lock-keywords
  5202. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5203. (org-set-local 'font-lock-defaults
  5204. '(org-font-lock-keywords t nil nil backward-paragraph))
  5205. (kill-local-variable 'font-lock-keywords) nil))
  5206. (defun org-toggle-pretty-entities ()
  5207. "Toggle the composition display of entities as UTF8 characters."
  5208. (interactive)
  5209. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5210. (org-restart-font-lock)
  5211. (if org-pretty-entities
  5212. (message "Entities are displayed as UTF8 characers")
  5213. (save-restriction
  5214. (widen)
  5215. (org-decompose-region (point-min) (point-max))
  5216. (message "Entities are displayed plain"))))
  5217. (defun org-fontify-entities (limit)
  5218. "Find an entity to fontify."
  5219. (let (ee)
  5220. (when org-pretty-entities
  5221. (catch 'match
  5222. (while (re-search-forward
  5223. "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"
  5224. limit t)
  5225. (if (and (not (org-in-indented-comment-line))
  5226. (setq ee (org-entity-get (match-string 1)))
  5227. (= (length (nth 6 ee)) 1))
  5228. (progn
  5229. (add-text-properties
  5230. (match-beginning 0) (match-end 1)
  5231. (list 'font-lock-fontified t))
  5232. (compose-region (match-beginning 0) (match-end 1)
  5233. (nth 6 ee) nil)
  5234. (backward-char 1)
  5235. (throw 'match t))))
  5236. nil))))
  5237. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5238. "Fontify string S like in Org-mode."
  5239. (with-temp-buffer
  5240. (insert s)
  5241. (let ((org-odd-levels-only odd-levels))
  5242. (org-mode)
  5243. (font-lock-fontify-buffer)
  5244. (buffer-string))))
  5245. (defvar org-m nil)
  5246. (defvar org-l nil)
  5247. (defvar org-f nil)
  5248. (defun org-get-level-face (n)
  5249. "Get the right face for match N in font-lock matching of headlines."
  5250. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5251. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5252. (if org-cycle-level-faces
  5253. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5254. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5255. (cond
  5256. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5257. ((eq n 2) org-f)
  5258. (t (if org-level-color-stars-only nil org-f))))
  5259. (defun org-get-todo-face (kwd)
  5260. "Get the right face for a TODO keyword KWD.
  5261. If KWD is a number, get the corresponding match group."
  5262. (if (numberp kwd) (setq kwd (match-string kwd)))
  5263. (or (org-face-from-face-or-color
  5264. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5265. (and (member kwd org-done-keywords) 'org-done)
  5266. 'org-todo))
  5267. (defun org-face-from-face-or-color (context inherit face-or-color)
  5268. "Create a face list that inherits INHERIT, but sets the foreground color.
  5269. When FACE-OR-COLOR is not a string, just return it."
  5270. (if (stringp face-or-color)
  5271. (list :inherit inherit
  5272. (cdr (assoc context org-faces-easy-properties))
  5273. face-or-color)
  5274. face-or-color))
  5275. (defun org-font-lock-add-tag-faces (limit)
  5276. "Add the special tag faces."
  5277. (when (and org-tag-faces org-tags-special-faces-re)
  5278. (while (re-search-forward org-tags-special-faces-re limit t)
  5279. (add-text-properties (match-beginning 1) (match-end 1)
  5280. (list 'face (org-get-tag-face 1)
  5281. 'font-lock-fontified t))
  5282. (backward-char 1))))
  5283. (defun org-font-lock-add-priority-faces (limit)
  5284. "Add the special priority faces."
  5285. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5286. (add-text-properties
  5287. (match-beginning 0) (match-end 0)
  5288. (list 'face (or (org-face-from-face-or-color
  5289. 'priority 'org-special-keyword
  5290. (cdr (assoc (char-after (match-beginning 1))
  5291. org-priority-faces)))
  5292. 'org-special-keyword)
  5293. 'font-lock-fontified t))))
  5294. (defun org-get-tag-face (kwd)
  5295. "Get the right face for a TODO keyword KWD.
  5296. If KWD is a number, get the corresponding match group."
  5297. (if (numberp kwd) (setq kwd (match-string kwd)))
  5298. (or (org-face-from-face-or-color
  5299. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5300. 'org-tag))
  5301. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5302. "Remove fontification and activation overlays from links."
  5303. (font-lock-default-unfontify-region beg end)
  5304. (let* ((buffer-undo-list t)
  5305. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5306. (inhibit-modification-hooks t)
  5307. deactivate-mark buffer-file-name buffer-file-truename)
  5308. (org-decompose-region beg end)
  5309. (remove-text-properties beg end
  5310. '(mouse-face t keymap t org-linked-text t
  5311. invisible t intangible t
  5312. org-no-flyspell t org-emphasis t))
  5313. (org-remove-font-lock-display-properties beg end)))
  5314. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5315. ((raise 0.3) (height 0.7))
  5316. ((raise -0.5))
  5317. ((raise 0.5)))
  5318. "Display properties for showing superscripts and subscripts.")
  5319. (defun org-remove-font-lock-display-properties (beg end)
  5320. "Remove specific display properties that have been added by font lock.
  5321. The will remove the raise properties that are used to show superscripts
  5322. and subscripts."
  5323. (let (next prop)
  5324. (while (< beg end)
  5325. (setq next (next-single-property-change beg 'display nil end)
  5326. prop (get-text-property beg 'display))
  5327. (if (member prop org-script-display)
  5328. (put-text-property beg next 'display nil))
  5329. (setq beg next))))
  5330. (defun org-raise-scripts (limit)
  5331. "Add raise properties to sub/superscripts."
  5332. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5333. (if (re-search-forward
  5334. (if (eq org-use-sub-superscripts t)
  5335. org-match-substring-regexp
  5336. org-match-substring-with-braces-regexp)
  5337. limit t)
  5338. (let* ((pos (point)) table-p comment-p
  5339. (mpos (match-beginning 3))
  5340. (emph-p (get-text-property mpos 'org-emphasis))
  5341. (link-p (get-text-property mpos 'mouse-face))
  5342. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5343. (goto-char (point-at-bol))
  5344. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5345. comment-p (org-looking-at-p "[ \t]*#"))
  5346. (goto-char pos)
  5347. ;; FIXME: Should we go back one character here, for a_b^c
  5348. ;; (goto-char (1- pos)) ;????????????????????
  5349. (if (or comment-p emph-p link-p keyw-p)
  5350. t
  5351. (put-text-property (match-beginning 3) (match-end 0)
  5352. 'display
  5353. (if (equal (char-after (match-beginning 2)) ?^)
  5354. (nth (if table-p 3 1) org-script-display)
  5355. (nth (if table-p 2 0) org-script-display)))
  5356. (add-text-properties (match-beginning 2) (match-end 2)
  5357. (list 'invisible t
  5358. 'org-dwidth t 'org-dwidth-n 1))
  5359. (if (and (eq (char-after (match-beginning 3)) ?{)
  5360. (eq (char-before (match-end 3)) ?}))
  5361. (progn
  5362. (add-text-properties
  5363. (match-beginning 3) (1+ (match-beginning 3))
  5364. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5365. (add-text-properties
  5366. (1- (match-end 3)) (match-end 3)
  5367. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5368. t)))))
  5369. ;;;; Visibility cycling, including org-goto and indirect buffer
  5370. ;;; Cycling
  5371. (defvar org-cycle-global-status nil)
  5372. (make-variable-buffer-local 'org-cycle-global-status)
  5373. (defvar org-cycle-subtree-status nil)
  5374. (make-variable-buffer-local 'org-cycle-subtree-status)
  5375. ;;;###autoload
  5376. (defvar org-inlinetask-min-level)
  5377. (defun org-cycle (&optional arg)
  5378. "TAB-action and visibility cycling for Org-mode.
  5379. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5380. is outline visibility cycling, but it also invokes other actions
  5381. in special contexts.
  5382. - When this function is called with a prefix argument, rotate the entire
  5383. buffer through 3 states (global cycling)
  5384. 1. OVERVIEW: Show only top-level headlines.
  5385. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5386. 3. SHOW ALL: Show everything.
  5387. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5388. determined by the variable `org-startup-folded', and by any VISIBILITY
  5389. properties in the buffer.
  5390. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5391. including any drawers.
  5392. - When inside a table, re-align the table and move to the next field.
  5393. - When point is at the beginning of a headline, rotate the subtree started
  5394. by this line through 3 different states (local cycling)
  5395. 1. FOLDED: Only the main headline is shown.
  5396. 2. CHILDREN: The main headline and the direct children are shown.
  5397. From this state, you can move to one of the children
  5398. and zoom in further.
  5399. 3. SUBTREE: Show the entire subtree, including body text.
  5400. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5401. - When point is at the beginning of an empty headline and the variable
  5402. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5403. of the headline by demoting and promoting it to likely levels. This
  5404. speeds up creation document structure by pressing TAB once or several
  5405. times right after creating a new headline.
  5406. - When there is a numeric prefix, go up to a heading with level ARG, do
  5407. a `show-subtree' and return to the previous cursor position. If ARG
  5408. is negative, go up that many levels.
  5409. - When point is not at the beginning of a headline, execute the global
  5410. binding for TAB, which is re-indenting the line. See the option
  5411. `org-cycle-emulate-tab' for details.
  5412. - Special case: if point is at the beginning of the buffer and there is
  5413. no headline in line 1, this function will act as if called with prefix arg
  5414. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5415. But only if also the variable `org-cycle-global-at-bob' is t."
  5416. (interactive "P")
  5417. (org-load-modules-maybe)
  5418. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5419. (and org-cycle-level-after-item/entry-creation
  5420. (or (org-cycle-level)
  5421. (org-cycle-item-indentation))))
  5422. (let* ((limit-level
  5423. (or org-cycle-max-level
  5424. (and (boundp 'org-inlinetask-min-level)
  5425. org-inlinetask-min-level
  5426. (1- org-inlinetask-min-level))))
  5427. (nstars (and limit-level
  5428. (if org-odd-levels-only
  5429. (and limit-level (1- (* limit-level 2)))
  5430. limit-level)))
  5431. (org-outline-regexp
  5432. (if (not (eq major-mode 'org-mode))
  5433. outline-regexp
  5434. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5435. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5436. (not (looking-at org-outline-regexp))))
  5437. (org-cycle-hook
  5438. (if bob-special
  5439. (delq 'org-optimize-window-after-visibility-change
  5440. (copy-sequence org-cycle-hook))
  5441. org-cycle-hook))
  5442. (pos (point)))
  5443. (if (or bob-special (equal arg '(4)))
  5444. ;; special case: use global cycling
  5445. (setq arg t))
  5446. (cond
  5447. ((equal arg '(16))
  5448. (setq last-command 'dummy)
  5449. (org-set-startup-visibility)
  5450. (message "Startup visibility, plus VISIBILITY properties"))
  5451. ((equal arg '(64))
  5452. (show-all)
  5453. (message "Entire buffer visible, including drawers"))
  5454. ;; Table: enter it or move to the next field.
  5455. ((org-at-table-p 'any)
  5456. (if (org-at-table.el-p)
  5457. (message "Use C-c ' to edit table.el tables")
  5458. (if arg (org-table-edit-field t)
  5459. (org-table-justify-field-maybe)
  5460. (call-interactively 'org-table-next-field))))
  5461. ((run-hook-with-args-until-success
  5462. 'org-tab-after-check-for-table-hook))
  5463. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5464. ((eq arg t) (org-cycle-internal-global))
  5465. ;; Drawers: delegate to `org-flag-drawer'.
  5466. ((and org-drawers org-drawer-regexp
  5467. (save-excursion
  5468. (beginning-of-line 1)
  5469. (looking-at org-drawer-regexp)))
  5470. (org-flag-drawer ; toggle block visibility
  5471. (not (get-char-property (match-end 0) 'invisible))))
  5472. ;; Show-subtree, ARG levels up from here.
  5473. ((integerp arg)
  5474. (save-excursion
  5475. (org-back-to-heading)
  5476. (outline-up-heading (if (< arg 0) (- arg)
  5477. (- (funcall outline-level) arg)))
  5478. (org-show-subtree)))
  5479. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5480. ((and (featurep 'org-inlinetask)
  5481. (org-inlinetask-at-task-p)
  5482. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5483. (org-inlinetask-toggle-visibility))
  5484. ((org-try-cdlatex-tab))
  5485. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5486. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5487. (save-excursion (beginning-of-line 1)
  5488. (looking-at org-outline-regexp)))
  5489. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5490. (org-cycle-internal-local))
  5491. ;; From there: TAB emulation and template completion.
  5492. (buffer-read-only (org-back-to-heading))
  5493. ((run-hook-with-args-until-success
  5494. 'org-tab-after-check-for-cycling-hook))
  5495. ((org-try-structure-completion))
  5496. ((run-hook-with-args-until-success
  5497. 'org-tab-before-tab-emulation-hook))
  5498. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5499. (or (not (bolp))
  5500. (not (looking-at org-outline-regexp))))
  5501. (call-interactively (global-key-binding "\t")))
  5502. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5503. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5504. (or (and (eq org-cycle-emulate-tab 'white)
  5505. (= (match-end 0) (point-at-eol)))
  5506. (and (eq org-cycle-emulate-tab 'whitestart)
  5507. (>= (match-end 0) pos))))
  5508. t
  5509. (eq org-cycle-emulate-tab t))
  5510. (call-interactively (global-key-binding "\t")))
  5511. (t (save-excursion
  5512. (org-back-to-heading)
  5513. (org-cycle)))))))
  5514. (defun org-cycle-internal-global ()
  5515. "Do the global cycling action."
  5516. (cond
  5517. ((and (eq last-command this-command)
  5518. (eq org-cycle-global-status 'overview))
  5519. ;; We just created the overview - now do table of contents
  5520. ;; This can be slow in very large buffers, so indicate action
  5521. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5522. (message "CONTENTS...")
  5523. (org-content)
  5524. (message "CONTENTS...done")
  5525. (setq org-cycle-global-status 'contents)
  5526. (run-hook-with-args 'org-cycle-hook 'contents))
  5527. ((and (eq last-command this-command)
  5528. (eq org-cycle-global-status 'contents))
  5529. ;; We just showed the table of contents - now show everything
  5530. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5531. (show-all)
  5532. (message "SHOW ALL")
  5533. (setq org-cycle-global-status 'all)
  5534. (run-hook-with-args 'org-cycle-hook 'all))
  5535. (t
  5536. ;; Default action: go to overview
  5537. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5538. (org-overview)
  5539. (message "OVERVIEW")
  5540. (setq org-cycle-global-status 'overview)
  5541. (run-hook-with-args 'org-cycle-hook 'overview))))
  5542. (defun org-cycle-internal-local ()
  5543. "Do the local cycling action."
  5544. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5545. ;; First, determine end of headline (EOH), end of subtree or item
  5546. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5547. (save-excursion
  5548. (if (org-at-item-p)
  5549. (progn
  5550. (beginning-of-line)
  5551. (setq struct (org-list-struct))
  5552. (setq eoh (point-at-eol))
  5553. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5554. (setq has-children (org-list-has-child-p (point) struct)))
  5555. (org-back-to-heading)
  5556. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5557. (setq eos (save-excursion
  5558. (org-end-of-subtree t)
  5559. (unless (eobp)
  5560. (skip-chars-forward " \t\n"))
  5561. (if (eobp) (point) (1- (point)))))
  5562. (setq has-children
  5563. (or (save-excursion
  5564. (let ((level (funcall outline-level)))
  5565. (outline-next-heading)
  5566. (and (org-at-heading-p t)
  5567. (> (funcall outline-level) level))))
  5568. (save-excursion
  5569. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5570. ;; Determine end invisible part of buffer (EOL)
  5571. (beginning-of-line 2)
  5572. ;; XEmacs doesn't have `next-single-char-property-change'
  5573. (if (featurep 'xemacs)
  5574. (while (and (not (eobp)) ;; this is like `next-line'
  5575. (get-char-property (1- (point)) 'invisible))
  5576. (beginning-of-line 2))
  5577. (while (and (not (eobp)) ;; this is like `next-line'
  5578. (get-char-property (1- (point)) 'invisible))
  5579. (goto-char (next-single-char-property-change (point) 'invisible))
  5580. (and (eolp) (beginning-of-line 2))))
  5581. (setq eol (point)))
  5582. ;; Find out what to do next and set `this-command'
  5583. (cond
  5584. ((= eos eoh)
  5585. ;; Nothing is hidden behind this heading
  5586. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5587. (message "EMPTY ENTRY")
  5588. (setq org-cycle-subtree-status nil)
  5589. (save-excursion
  5590. (goto-char eos)
  5591. (outline-next-heading)
  5592. (if (outline-invisible-p) (org-flag-heading nil))))
  5593. ((and (or (>= eol eos)
  5594. (not (string-match "\\S-" (buffer-substring eol eos))))
  5595. (or has-children
  5596. (not (setq children-skipped
  5597. org-cycle-skip-children-state-if-no-children))))
  5598. ;; Entire subtree is hidden in one line: children view
  5599. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5600. (if (org-at-item-p)
  5601. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5602. (org-show-entry)
  5603. (show-children)
  5604. ;; Fold every list in subtree to top-level items.
  5605. (when (eq org-cycle-include-plain-lists 'integrate)
  5606. (save-excursion
  5607. (org-back-to-heading)
  5608. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5609. (beginning-of-line 1)
  5610. (let* ((struct (org-list-struct))
  5611. (prevs (org-list-prevs-alist struct))
  5612. (end (org-list-get-bottom-point struct)))
  5613. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5614. (org-list-get-all-items (point) struct prevs))
  5615. (goto-char end))))))
  5616. (message "CHILDREN")
  5617. (save-excursion
  5618. (goto-char eos)
  5619. (outline-next-heading)
  5620. (if (outline-invisible-p) (org-flag-heading nil)))
  5621. (setq org-cycle-subtree-status 'children)
  5622. (run-hook-with-args 'org-cycle-hook 'children))
  5623. ((or children-skipped
  5624. (and (eq last-command this-command)
  5625. (eq org-cycle-subtree-status 'children)))
  5626. ;; We just showed the children, or no children are there,
  5627. ;; now show everything.
  5628. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5629. (outline-flag-region eoh eos nil)
  5630. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5631. (setq org-cycle-subtree-status 'subtree)
  5632. (run-hook-with-args 'org-cycle-hook 'subtree))
  5633. (t
  5634. ;; Default action: hide the subtree.
  5635. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5636. (outline-flag-region eoh eos t)
  5637. (message "FOLDED")
  5638. (setq org-cycle-subtree-status 'folded)
  5639. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5640. ;;;###autoload
  5641. (defun org-global-cycle (&optional arg)
  5642. "Cycle the global visibility. For details see `org-cycle'.
  5643. With \\[universal-argument] prefix arg, switch to startup visibility.
  5644. With a numeric prefix, show all headlines up to that level."
  5645. (interactive "P")
  5646. (let ((org-cycle-include-plain-lists
  5647. (if (eq major-mode 'org-mode) org-cycle-include-plain-lists nil)))
  5648. (cond
  5649. ((integerp arg)
  5650. (show-all)
  5651. (hide-sublevels arg)
  5652. (setq org-cycle-global-status 'contents))
  5653. ((equal arg '(4))
  5654. (org-set-startup-visibility)
  5655. (message "Startup visibility, plus VISIBILITY properties."))
  5656. (t
  5657. (org-cycle '(4))))))
  5658. (defun org-set-startup-visibility ()
  5659. "Set the visibility required by startup options and properties."
  5660. (cond
  5661. ((eq org-startup-folded t)
  5662. (org-cycle '(4)))
  5663. ((eq org-startup-folded 'content)
  5664. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5665. (org-cycle '(4)) (org-cycle '(4)))))
  5666. (unless (eq org-startup-folded 'showeverything)
  5667. (if org-hide-block-startup (org-hide-block-all))
  5668. (org-set-visibility-according-to-property 'no-cleanup)
  5669. (org-cycle-hide-archived-subtrees 'all)
  5670. (org-cycle-hide-drawers 'all)
  5671. (org-cycle-show-empty-lines t)))
  5672. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5673. "Switch subtree visibilities according to :VISIBILITY: property."
  5674. (interactive)
  5675. (let (org-show-entry-below state)
  5676. (save-excursion
  5677. (goto-char (point-min))
  5678. (while (re-search-forward
  5679. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5680. nil t)
  5681. (setq state (match-string 1))
  5682. (save-excursion
  5683. (org-back-to-heading t)
  5684. (hide-subtree)
  5685. (org-reveal)
  5686. (cond
  5687. ((equal state '("fold" "folded"))
  5688. (hide-subtree))
  5689. ((equal state "children")
  5690. (org-show-hidden-entry)
  5691. (show-children))
  5692. ((equal state "content")
  5693. (save-excursion
  5694. (save-restriction
  5695. (org-narrow-to-subtree)
  5696. (org-content))))
  5697. ((member state '("all" "showall"))
  5698. (show-subtree)))))
  5699. (unless no-cleanup
  5700. (org-cycle-hide-archived-subtrees 'all)
  5701. (org-cycle-hide-drawers 'all)
  5702. (org-cycle-show-empty-lines 'all)))))
  5703. ;; This function uses outline-regexp instead of the more fundamental
  5704. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5705. ;; buffers, where outline-regexp is needed.
  5706. (defun org-overview ()
  5707. "Switch to overview mode, showing only top-level headlines.
  5708. Really, this shows all headlines with level equal or greater than the level
  5709. of the first headline in the buffer. This is important, because if the
  5710. first headline is not level one, then (hide-sublevels 1) gives confusing
  5711. results."
  5712. (interactive)
  5713. (let ((level (save-excursion
  5714. (goto-char (point-min))
  5715. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5716. (progn
  5717. (goto-char (match-beginning 0))
  5718. (funcall outline-level))))))
  5719. (and level (hide-sublevels level))))
  5720. (defun org-content (&optional arg)
  5721. "Show all headlines in the buffer, like a table of contents.
  5722. With numerical argument N, show content up to level N."
  5723. (interactive "P")
  5724. (save-excursion
  5725. ;; Visit all headings and show their offspring
  5726. (and (integerp arg) (org-overview))
  5727. (goto-char (point-max))
  5728. (catch 'exit
  5729. (while (and (progn (condition-case nil
  5730. (outline-previous-visible-heading 1)
  5731. (error (goto-char (point-min))))
  5732. t)
  5733. (looking-at org-outline-regexp))
  5734. (if (integerp arg)
  5735. (show-children (1- arg))
  5736. (show-branches))
  5737. (if (bobp) (throw 'exit nil))))))
  5738. (defun org-optimize-window-after-visibility-change (state)
  5739. "Adjust the window after a change in outline visibility.
  5740. This function is the default value of the hook `org-cycle-hook'."
  5741. (when (get-buffer-window (current-buffer))
  5742. (cond
  5743. ((eq state 'content) nil)
  5744. ((eq state 'all) nil)
  5745. ((eq state 'folded) nil)
  5746. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5747. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5748. (defun org-remove-empty-overlays-at (pos)
  5749. "Remove outline overlays that do not contain non-white stuff."
  5750. (mapc
  5751. (lambda (o)
  5752. (and (eq 'outline (overlay-get o 'invisible))
  5753. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5754. (overlay-end o))))
  5755. (delete-overlay o)))
  5756. (overlays-at pos)))
  5757. (defun org-clean-visibility-after-subtree-move ()
  5758. "Fix visibility issues after moving a subtree."
  5759. ;; First, find a reasonable region to look at:
  5760. ;; Start two siblings above, end three below
  5761. (let* ((beg (save-excursion
  5762. (and (org-get-last-sibling)
  5763. (org-get-last-sibling))
  5764. (point)))
  5765. (end (save-excursion
  5766. (and (org-get-next-sibling)
  5767. (org-get-next-sibling)
  5768. (org-get-next-sibling))
  5769. (if (org-at-heading-p)
  5770. (point-at-eol)
  5771. (point))))
  5772. (level (looking-at "\\*+"))
  5773. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5774. (save-excursion
  5775. (save-restriction
  5776. (narrow-to-region beg end)
  5777. (when re
  5778. ;; Properly fold already folded siblings
  5779. (goto-char (point-min))
  5780. (while (re-search-forward re nil t)
  5781. (if (and (not (outline-invisible-p))
  5782. (save-excursion
  5783. (goto-char (point-at-eol)) (outline-invisible-p)))
  5784. (hide-entry))))
  5785. (org-cycle-show-empty-lines 'overview)
  5786. (org-cycle-hide-drawers 'overview)))))
  5787. (defun org-cycle-show-empty-lines (state)
  5788. "Show empty lines above all visible headlines.
  5789. The region to be covered depends on STATE when called through
  5790. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5791. entire buffer covered. Note that an empty line is only shown if there
  5792. are at least `org-cycle-separator-lines' empty lines before the headline."
  5793. (when (not (= org-cycle-separator-lines 0))
  5794. (save-excursion
  5795. (let* ((n (abs org-cycle-separator-lines))
  5796. (re (cond
  5797. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5798. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5799. (t (let ((ns (number-to-string (- n 2))))
  5800. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5801. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5802. beg end b e)
  5803. (cond
  5804. ((memq state '(overview contents t))
  5805. (setq beg (point-min) end (point-max)))
  5806. ((memq state '(children folded))
  5807. (setq beg (point) end (progn (org-end-of-subtree t t)
  5808. (beginning-of-line 2)
  5809. (point)))))
  5810. (when beg
  5811. (goto-char beg)
  5812. (while (re-search-forward re end t)
  5813. (unless (get-char-property (match-end 1) 'invisible)
  5814. (setq e (match-end 1))
  5815. (if (< org-cycle-separator-lines 0)
  5816. (setq b (save-excursion
  5817. (goto-char (match-beginning 0))
  5818. (org-back-over-empty-lines)
  5819. (if (save-excursion
  5820. (goto-char (max (point-min) (1- (point))))
  5821. (org-at-heading-p))
  5822. (1- (point))
  5823. (point))))
  5824. (setq b (match-beginning 1)))
  5825. (outline-flag-region b e nil)))))))
  5826. ;; Never hide empty lines at the end of the file.
  5827. (save-excursion
  5828. (goto-char (point-max))
  5829. (outline-previous-heading)
  5830. (outline-end-of-heading)
  5831. (if (and (looking-at "[ \t\n]+")
  5832. (= (match-end 0) (point-max)))
  5833. (outline-flag-region (point) (match-end 0) nil))))
  5834. (defun org-show-empty-lines-in-parent ()
  5835. "Move to the parent and re-show empty lines before visible headlines."
  5836. (save-excursion
  5837. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5838. (org-cycle-show-empty-lines context))))
  5839. (defun org-files-list ()
  5840. "Return `org-agenda-files' list, plus all open org-mode files.
  5841. This is useful for operations that need to scan all of a user's
  5842. open and agenda-wise Org files."
  5843. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5844. (dolist (buf (buffer-list))
  5845. (with-current-buffer buf
  5846. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5847. (let ((file (expand-file-name (buffer-file-name))))
  5848. (unless (member file files)
  5849. (push file files))))))
  5850. files))
  5851. (defsubst org-entry-beginning-position ()
  5852. "Return the beginning position of the current entry."
  5853. (save-excursion (outline-back-to-heading t) (point)))
  5854. (defsubst org-entry-end-position ()
  5855. "Return the end position of the current entry."
  5856. (save-excursion (outline-next-heading) (point)))
  5857. (defun org-cycle-hide-drawers (state)
  5858. "Re-hide all drawers after a visibility state change."
  5859. (when (and (eq major-mode 'org-mode)
  5860. (not (memq state '(overview folded contents))))
  5861. (save-excursion
  5862. (let* ((globalp (memq state '(contents all)))
  5863. (beg (if globalp (point-min) (point)))
  5864. (end (if globalp (point-max)
  5865. (if (eq state 'children)
  5866. (save-excursion (outline-next-heading) (point))
  5867. (org-end-of-subtree t)))))
  5868. (goto-char beg)
  5869. (while (re-search-forward org-drawer-regexp end t)
  5870. (org-flag-drawer t))))))
  5871. (defun org-flag-drawer (flag)
  5872. (save-excursion
  5873. (beginning-of-line 1)
  5874. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5875. (let ((b (match-end 0)))
  5876. (if (re-search-forward
  5877. "^[ \t]*:END:"
  5878. (save-excursion (outline-next-heading) (point)) t)
  5879. (outline-flag-region b (point-at-eol) flag)
  5880. (error ":END: line missing at position %s" b))))))
  5881. (defun org-subtree-end-visible-p ()
  5882. "Is the end of the current subtree visible?"
  5883. (pos-visible-in-window-p
  5884. (save-excursion (org-end-of-subtree t) (point))))
  5885. (defun org-first-headline-recenter (&optional N)
  5886. "Move cursor to the first headline and recenter the headline.
  5887. Optional argument N means put the headline into the Nth line of the window."
  5888. (goto-char (point-min))
  5889. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  5890. (beginning-of-line)
  5891. (recenter (prefix-numeric-value N))))
  5892. ;;; Saving and restoring visibility
  5893. (defun org-outline-overlay-data (&optional use-markers)
  5894. "Return a list of the locations of all outline overlays.
  5895. These are overlays with the `invisible' property value `outline'.
  5896. The return value is a list of cons cells, with start and stop
  5897. positions for each overlay.
  5898. If USE-MARKERS is set, return the positions as markers."
  5899. (let (beg end)
  5900. (save-excursion
  5901. (save-restriction
  5902. (widen)
  5903. (delq nil
  5904. (mapcar (lambda (o)
  5905. (when (eq (overlay-get o 'invisible) 'outline)
  5906. (setq beg (overlay-start o)
  5907. end (overlay-end o))
  5908. (and beg end (> end beg)
  5909. (if use-markers
  5910. (cons (move-marker (make-marker) beg)
  5911. (move-marker (make-marker) end))
  5912. (cons beg end)))))
  5913. (overlays-in (point-min) (point-max))))))))
  5914. (defun org-set-outline-overlay-data (data)
  5915. "Create visibility overlays for all positions in DATA.
  5916. DATA should have been made by `org-outline-overlay-data'."
  5917. (let (o)
  5918. (save-excursion
  5919. (save-restriction
  5920. (widen)
  5921. (show-all)
  5922. (mapc (lambda (c)
  5923. (outline-flag-region (car c) (cdr c) t))
  5924. data)))))
  5925. ;;; Folding of blocks
  5926. (defvar org-hide-block-overlays nil
  5927. "Overlays hiding blocks.")
  5928. (make-variable-buffer-local 'org-hide-block-overlays)
  5929. (defun org-block-map (function &optional start end)
  5930. "Call FUNCTION at the head of all source blocks in the current buffer.
  5931. Optional arguments START and END can be used to limit the range."
  5932. (let ((start (or start (point-min)))
  5933. (end (or end (point-max))))
  5934. (save-excursion
  5935. (goto-char start)
  5936. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5937. (save-excursion
  5938. (save-match-data
  5939. (goto-char (match-beginning 0))
  5940. (funcall function)))))))
  5941. (defun org-hide-block-toggle-all ()
  5942. "Toggle the visibility of all blocks in the current buffer."
  5943. (org-block-map #'org-hide-block-toggle))
  5944. (defun org-hide-block-all ()
  5945. "Fold all blocks in the current buffer."
  5946. (interactive)
  5947. (org-show-block-all)
  5948. (org-block-map #'org-hide-block-toggle-maybe))
  5949. (defun org-show-block-all ()
  5950. "Unfold all blocks in the current buffer."
  5951. (interactive)
  5952. (mapc 'delete-overlay org-hide-block-overlays)
  5953. (setq org-hide-block-overlays nil))
  5954. (defun org-hide-block-toggle-maybe ()
  5955. "Toggle visibility of block at point."
  5956. (interactive)
  5957. (let ((case-fold-search t))
  5958. (if (save-excursion
  5959. (beginning-of-line 1)
  5960. (looking-at org-block-regexp))
  5961. (progn (org-hide-block-toggle)
  5962. t) ;; to signal that we took action
  5963. nil))) ;; to signal that we did not
  5964. (defun org-hide-block-toggle (&optional force)
  5965. "Toggle the visibility of the current block."
  5966. (interactive)
  5967. (save-excursion
  5968. (beginning-of-line)
  5969. (if (re-search-forward org-block-regexp nil t)
  5970. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5971. (end (match-end 0)) ;; end of entire body
  5972. ov)
  5973. (if (memq t (mapcar (lambda (overlay)
  5974. (eq (overlay-get overlay 'invisible)
  5975. 'org-hide-block))
  5976. (overlays-at start)))
  5977. (if (or (not force) (eq force 'off))
  5978. (mapc (lambda (ov)
  5979. (when (member ov org-hide-block-overlays)
  5980. (setq org-hide-block-overlays
  5981. (delq ov org-hide-block-overlays)))
  5982. (when (eq (overlay-get ov 'invisible)
  5983. 'org-hide-block)
  5984. (delete-overlay ov)))
  5985. (overlays-at start)))
  5986. (setq ov (make-overlay start end))
  5987. (overlay-put ov 'invisible 'org-hide-block)
  5988. ;; make the block accessible to isearch
  5989. (overlay-put
  5990. ov 'isearch-open-invisible
  5991. (lambda (ov)
  5992. (when (member ov org-hide-block-overlays)
  5993. (setq org-hide-block-overlays
  5994. (delq ov org-hide-block-overlays)))
  5995. (when (eq (overlay-get ov 'invisible)
  5996. 'org-hide-block)
  5997. (delete-overlay ov))))
  5998. (push ov org-hide-block-overlays)))
  5999. (error "Not looking at a source block"))))
  6000. ;; org-tab-after-check-for-cycling-hook
  6001. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6002. ;; Remove overlays when changing major mode
  6003. (add-hook 'org-mode-hook
  6004. (lambda () (org-add-hook 'change-major-mode-hook
  6005. 'org-show-block-all 'append 'local)))
  6006. ;;; Org-goto
  6007. (defvar org-goto-window-configuration nil)
  6008. (defvar org-goto-marker nil)
  6009. (defvar org-goto-map
  6010. (let ((map (make-sparse-keymap)))
  6011. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  6012. (while (setq cmd (pop cmds))
  6013. (substitute-key-definition cmd cmd map global-map)))
  6014. (suppress-keymap map)
  6015. (org-defkey map "\C-m" 'org-goto-ret)
  6016. (org-defkey map [(return)] 'org-goto-ret)
  6017. (org-defkey map [(left)] 'org-goto-left)
  6018. (org-defkey map [(right)] 'org-goto-right)
  6019. (org-defkey map [(control ?g)] 'org-goto-quit)
  6020. (org-defkey map "\C-i" 'org-cycle)
  6021. (org-defkey map [(tab)] 'org-cycle)
  6022. (org-defkey map [(down)] 'outline-next-visible-heading)
  6023. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6024. (if org-goto-auto-isearch
  6025. (if (fboundp 'define-key-after)
  6026. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6027. nil)
  6028. (org-defkey map "q" 'org-goto-quit)
  6029. (org-defkey map "n" 'outline-next-visible-heading)
  6030. (org-defkey map "p" 'outline-previous-visible-heading)
  6031. (org-defkey map "f" 'outline-forward-same-level)
  6032. (org-defkey map "b" 'outline-backward-same-level)
  6033. (org-defkey map "u" 'outline-up-heading))
  6034. (org-defkey map "/" 'org-occur)
  6035. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6036. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6037. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6038. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6039. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6040. map))
  6041. (defconst org-goto-help
  6042. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  6043. RET=jump to location [Q]uit and return to previous location
  6044. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6045. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6046. ;; FIXME: Docstring does not mention both interfaces
  6047. (defun org-goto (&optional alternative-interface)
  6048. "Look up a different location in the current file, keeping current visibility.
  6049. When you want look-up or go to a different location in a document, the
  6050. fastest way is often to fold the entire buffer and then dive into the tree.
  6051. This method has the disadvantage, that the previous location will be folded,
  6052. which may not be what you want.
  6053. This command works around this by showing a copy of the current buffer
  6054. in an indirect buffer, in overview mode. You can dive into the tree in
  6055. that copy, use org-occur and incremental search to find a location.
  6056. When pressing RET or `Q', the command returns to the original buffer in
  6057. which the visibility is still unchanged. After RET is will also jump to
  6058. the location selected in the indirect buffer and expose the headline
  6059. hierarchy above."
  6060. (interactive "P")
  6061. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6062. (org-refile-use-outline-path t)
  6063. (org-refile-target-verify-function nil)
  6064. (interface
  6065. (if (not alternative-interface)
  6066. org-goto-interface
  6067. (if (eq org-goto-interface 'outline)
  6068. 'outline-path-completion
  6069. 'outline)))
  6070. (org-goto-start-pos (point))
  6071. (selected-point
  6072. (if (eq interface 'outline)
  6073. (car (org-get-location (current-buffer) org-goto-help))
  6074. (let ((pa (org-refile-get-location "Goto")))
  6075. (org-refile-check-position pa)
  6076. (nth 3 pa)))))
  6077. (if selected-point
  6078. (progn
  6079. (org-mark-ring-push org-goto-start-pos)
  6080. (goto-char selected-point)
  6081. (if (or (outline-invisible-p) (org-invisible-p2))
  6082. (org-show-context 'org-goto)))
  6083. (message "Quit"))))
  6084. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6085. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6086. (defvar org-goto-local-auto-isearch-map) ; defined below
  6087. (defun org-get-location (buf help)
  6088. "Let the user select a location in the Org-mode buffer BUF.
  6089. This function uses a recursive edit. It returns the selected position
  6090. or nil."
  6091. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6092. (isearch-hide-immediately nil)
  6093. (isearch-search-fun-function
  6094. (lambda () 'org-goto-local-search-headings))
  6095. (org-goto-selected-point org-goto-exit-command)
  6096. (pop-up-frames nil)
  6097. (special-display-buffer-names nil)
  6098. (special-display-regexps nil)
  6099. (special-display-function nil))
  6100. (save-excursion
  6101. (save-window-excursion
  6102. (delete-other-windows)
  6103. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6104. (org-pop-to-buffer-same-window
  6105. (condition-case nil
  6106. (make-indirect-buffer (current-buffer) "*org-goto*")
  6107. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6108. (with-output-to-temp-buffer "*Help*"
  6109. (princ help))
  6110. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  6111. (setq buffer-read-only nil)
  6112. (let ((org-startup-truncated t)
  6113. (org-startup-folded nil)
  6114. (org-startup-align-all-tables nil))
  6115. (org-mode)
  6116. (org-overview))
  6117. (setq buffer-read-only t)
  6118. (if (and (boundp 'org-goto-start-pos)
  6119. (integer-or-marker-p org-goto-start-pos))
  6120. (let ((org-show-hierarchy-above t)
  6121. (org-show-siblings t)
  6122. (org-show-following-heading t))
  6123. (goto-char org-goto-start-pos)
  6124. (and (outline-invisible-p) (org-show-context)))
  6125. (goto-char (point-min)))
  6126. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6127. (message "Select location and press RET")
  6128. (use-local-map org-goto-map)
  6129. (recursive-edit)
  6130. ))
  6131. (kill-buffer "*org-goto*")
  6132. (cons org-goto-selected-point org-goto-exit-command)))
  6133. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6134. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6135. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6136. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6137. (defun org-goto-local-search-headings (string bound noerror)
  6138. "Search and make sure that any matches are in headlines."
  6139. (catch 'return
  6140. (while (if isearch-forward
  6141. (search-forward string bound noerror)
  6142. (search-backward string bound noerror))
  6143. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6144. (and (member :headline context)
  6145. (not (member :tags context))))
  6146. (throw 'return (point))))))
  6147. (defun org-goto-local-auto-isearch ()
  6148. "Start isearch."
  6149. (interactive)
  6150. (goto-char (point-min))
  6151. (let ((keys (this-command-keys)))
  6152. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6153. (isearch-mode t)
  6154. (isearch-process-search-char (string-to-char keys)))))
  6155. (defun org-goto-ret (&optional arg)
  6156. "Finish `org-goto' by going to the new location."
  6157. (interactive "P")
  6158. (setq org-goto-selected-point (point)
  6159. org-goto-exit-command 'return)
  6160. (throw 'exit nil))
  6161. (defun org-goto-left ()
  6162. "Finish `org-goto' by going to the new location."
  6163. (interactive)
  6164. (if (org-at-heading-p)
  6165. (progn
  6166. (beginning-of-line 1)
  6167. (setq org-goto-selected-point (point)
  6168. org-goto-exit-command 'left)
  6169. (throw 'exit nil))
  6170. (error "Not on a heading")))
  6171. (defun org-goto-right ()
  6172. "Finish `org-goto' by going to the new location."
  6173. (interactive)
  6174. (if (org-at-heading-p)
  6175. (progn
  6176. (setq org-goto-selected-point (point)
  6177. org-goto-exit-command 'right)
  6178. (throw 'exit nil))
  6179. (error "Not on a heading")))
  6180. (defun org-goto-quit ()
  6181. "Finish `org-goto' without cursor motion."
  6182. (interactive)
  6183. (setq org-goto-selected-point nil)
  6184. (setq org-goto-exit-command 'quit)
  6185. (throw 'exit nil))
  6186. ;;; Indirect buffer display of subtrees
  6187. (defvar org-indirect-dedicated-frame nil
  6188. "This is the frame being used for indirect tree display.")
  6189. (defvar org-last-indirect-buffer nil)
  6190. (defun org-tree-to-indirect-buffer (&optional arg)
  6191. "Create indirect buffer and narrow it to current subtree.
  6192. With numerical prefix ARG, go up to this level and then take that tree.
  6193. If ARG is negative, go up that many levels.
  6194. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6195. indirect buffer previously made with this command, to avoid proliferation of
  6196. indirect buffers. However, when you call the command with a \
  6197. \\[universal-argument] prefix, or
  6198. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6199. is kept so that you can work with several indirect buffers at the same time.
  6200. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6201. \\[universal-argument] prefix also
  6202. requests that a new frame be made for the new buffer, so that the dedicated
  6203. frame is not changed."
  6204. (interactive "P")
  6205. (let ((cbuf (current-buffer))
  6206. (cwin (selected-window))
  6207. (pos (point))
  6208. beg end level heading ibuf)
  6209. (save-excursion
  6210. (org-back-to-heading t)
  6211. (when (numberp arg)
  6212. (setq level (org-outline-level))
  6213. (if (< arg 0) (setq arg (+ level arg)))
  6214. (while (> (setq level (org-outline-level)) arg)
  6215. (outline-up-heading 1 t)))
  6216. (setq beg (point)
  6217. heading (org-get-heading))
  6218. (org-end-of-subtree t t)
  6219. (if (org-at-heading-p) (backward-char 1))
  6220. (setq end (point)))
  6221. (if (and (buffer-live-p org-last-indirect-buffer)
  6222. (not (eq org-indirect-buffer-display 'new-frame))
  6223. (not arg))
  6224. (kill-buffer org-last-indirect-buffer))
  6225. (setq ibuf (org-get-indirect-buffer cbuf)
  6226. org-last-indirect-buffer ibuf)
  6227. (cond
  6228. ((or (eq org-indirect-buffer-display 'new-frame)
  6229. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6230. (select-frame (make-frame))
  6231. (delete-other-windows)
  6232. (org-pop-to-buffer-same-window ibuf)
  6233. (org-set-frame-title heading))
  6234. ((eq org-indirect-buffer-display 'dedicated-frame)
  6235. (raise-frame
  6236. (select-frame (or (and org-indirect-dedicated-frame
  6237. (frame-live-p org-indirect-dedicated-frame)
  6238. org-indirect-dedicated-frame)
  6239. (setq org-indirect-dedicated-frame (make-frame)))))
  6240. (delete-other-windows)
  6241. (org-pop-to-buffer-same-window ibuf)
  6242. (org-set-frame-title (concat "Indirect: " heading)))
  6243. ((eq org-indirect-buffer-display 'current-window)
  6244. (org-pop-to-buffer-same-window ibuf))
  6245. ((eq org-indirect-buffer-display 'other-window)
  6246. (pop-to-buffer ibuf))
  6247. (t (error "Invalid value")))
  6248. (if (featurep 'xemacs)
  6249. (save-excursion (org-mode) (turn-on-font-lock)))
  6250. (narrow-to-region beg end)
  6251. (show-all)
  6252. (goto-char pos)
  6253. (run-hook-with-args 'org-cycle-hook 'all)
  6254. (and (window-live-p cwin) (select-window cwin))))
  6255. (defun org-get-indirect-buffer (&optional buffer)
  6256. (setq buffer (or buffer (current-buffer)))
  6257. (let ((n 1) (base (buffer-name buffer)) bname)
  6258. (while (buffer-live-p
  6259. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6260. (setq n (1+ n)))
  6261. (condition-case nil
  6262. (make-indirect-buffer buffer bname 'clone)
  6263. (error (make-indirect-buffer buffer bname)))))
  6264. (defun org-set-frame-title (title)
  6265. "Set the title of the current frame to the string TITLE."
  6266. ;; FIXME: how to name a single frame in XEmacs???
  6267. (unless (featurep 'xemacs)
  6268. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6269. ;;;; Structure editing
  6270. ;;; Inserting headlines
  6271. (defun org-previous-line-empty-p ()
  6272. (save-excursion
  6273. (and (not (bobp))
  6274. (or (beginning-of-line 0) t)
  6275. (save-match-data
  6276. (looking-at "[ \t]*$")))))
  6277. (defun org-insert-heading (&optional force-heading invisible-ok)
  6278. "Insert a new heading or item with same depth at point.
  6279. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6280. If point is at the beginning of a headline, insert a sibling before the
  6281. current headline. If point is not at the beginning, split the line,
  6282. create the new headline with the text in the current line after point
  6283. \(but see also the variable `org-M-RET-may-split-line').
  6284. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6285. This is important for non-interactive uses of the command."
  6286. (interactive "P")
  6287. (if (or (= (buffer-size) 0)
  6288. (and (not (save-excursion
  6289. (and (ignore-errors (org-back-to-heading invisible-ok))
  6290. (org-at-heading-p))))
  6291. (or force-heading (not (org-in-item-p)))))
  6292. (progn
  6293. (insert "\n* ")
  6294. (run-hooks 'org-insert-heading-hook))
  6295. (when (or force-heading (not (org-insert-item)))
  6296. (let* ((empty-line-p nil)
  6297. (level nil)
  6298. (on-heading (org-at-heading-p))
  6299. (head (save-excursion
  6300. (condition-case nil
  6301. (progn
  6302. (org-back-to-heading invisible-ok)
  6303. (when (and (not on-heading)
  6304. (featurep 'org-inlinetask)
  6305. (integerp org-inlinetask-min-level)
  6306. (>= (length (match-string 0))
  6307. org-inlinetask-min-level))
  6308. ;; Find a heading level before the inline task
  6309. (while (and (setq level (org-up-heading-safe))
  6310. (>= level org-inlinetask-min-level)))
  6311. (if (org-at-heading-p)
  6312. (org-back-to-heading invisible-ok)
  6313. (error "This should not happen")))
  6314. (setq empty-line-p (org-previous-line-empty-p))
  6315. (match-string 0))
  6316. (error "*"))))
  6317. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6318. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6319. pos hide-previous previous-pos)
  6320. (cond
  6321. ((and (org-at-heading-p) (bolp)
  6322. (or (bobp)
  6323. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6324. ;; insert before the current line
  6325. (open-line (if blank 2 1)))
  6326. ((and (bolp)
  6327. (not org-insert-heading-respect-content)
  6328. (or (bobp)
  6329. (save-excursion
  6330. (backward-char 1) (not (outline-invisible-p)))))
  6331. ;; insert right here
  6332. nil)
  6333. (t
  6334. ;; somewhere in the line
  6335. (save-excursion
  6336. (setq previous-pos (point-at-bol))
  6337. (end-of-line)
  6338. (setq hide-previous (outline-invisible-p)))
  6339. (and org-insert-heading-respect-content (org-show-subtree))
  6340. (let ((split
  6341. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6342. (save-excursion
  6343. (let ((p (point)))
  6344. (goto-char (point-at-bol))
  6345. (and (looking-at org-complex-heading-regexp)
  6346. (match-beginning 4)
  6347. (> p (match-beginning 4)))))))
  6348. tags pos)
  6349. (cond
  6350. (org-insert-heading-respect-content
  6351. (org-end-of-subtree nil t)
  6352. (when (featurep 'org-inlinetask)
  6353. (while (and (not (eobp))
  6354. (looking-at "\\(\\*+\\)[ \t]+")
  6355. (>= (length (match-string 1))
  6356. org-inlinetask-min-level))
  6357. (org-end-of-subtree nil t)))
  6358. (or (bolp) (newline))
  6359. (or (org-previous-line-empty-p)
  6360. (and blank (newline)))
  6361. (open-line 1))
  6362. ((org-at-heading-p)
  6363. (when hide-previous
  6364. (show-children)
  6365. (org-show-entry))
  6366. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6367. (setq tags (and (match-end 2) (match-string 2)))
  6368. (and (match-end 1)
  6369. (delete-region (match-beginning 1) (match-end 1)))
  6370. (setq pos (point-at-bol))
  6371. (or split (end-of-line 1))
  6372. (delete-horizontal-space)
  6373. (if (string-match "\\`\\*+\\'"
  6374. (buffer-substring (point-at-bol) (point)))
  6375. (insert " "))
  6376. (newline (if blank 2 1))
  6377. (when tags
  6378. (save-excursion
  6379. (goto-char pos)
  6380. (end-of-line 1)
  6381. (insert " " tags)
  6382. (org-set-tags nil 'align))))
  6383. (t
  6384. (or split (end-of-line 1))
  6385. (newline (if blank 2 1)))))))
  6386. (insert head) (just-one-space)
  6387. (setq pos (point))
  6388. (end-of-line 1)
  6389. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6390. (when (and org-insert-heading-respect-content hide-previous)
  6391. (save-excursion
  6392. (goto-char previous-pos)
  6393. (hide-subtree)))
  6394. (run-hooks 'org-insert-heading-hook)))))
  6395. (defun org-get-heading (&optional no-tags no-todo)
  6396. "Return the heading of the current entry, without the stars.
  6397. When NO-TAGS is non-nil, don't include tags.
  6398. When NO-TODO is non-nil, don't include TODO keywords."
  6399. (save-excursion
  6400. (org-back-to-heading t)
  6401. (cond
  6402. ((and no-tags no-todo)
  6403. (looking-at org-complex-heading-regexp)
  6404. (match-string 4))
  6405. (no-tags
  6406. (looking-at (concat org-outline-regexp
  6407. "\\(.*?\\)"
  6408. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6409. (match-string 1))
  6410. (no-todo
  6411. (looking-at org-todo-line-regexp)
  6412. (match-string 3))
  6413. (t (looking-at org-heading-regexp)
  6414. (match-string 2)))))
  6415. (defun org-heading-components ()
  6416. "Return the components of the current heading.
  6417. This is a list with the following elements:
  6418. - the level as an integer
  6419. - the reduced level, different if `org-odd-levels-only' is set.
  6420. - the TODO keyword, or nil
  6421. - the priority character, like ?A, or nil if no priority is given
  6422. - the headline text itself, or the tags string if no headline text
  6423. - the tags string, or nil."
  6424. (save-excursion
  6425. (org-back-to-heading t)
  6426. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6427. (list (length (match-string 1))
  6428. (org-reduced-level (length (match-string 1)))
  6429. (org-match-string-no-properties 2)
  6430. (and (match-end 3) (aref (match-string 3) 2))
  6431. (org-match-string-no-properties 4)
  6432. (org-match-string-no-properties 5)))))
  6433. (defun org-get-entry ()
  6434. "Get the entry text, after heading, entire subtree."
  6435. (save-excursion
  6436. (org-back-to-heading t)
  6437. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6438. (defun org-insert-heading-after-current ()
  6439. "Insert a new heading with same level as current, after current subtree."
  6440. (interactive)
  6441. (org-back-to-heading)
  6442. (org-insert-heading)
  6443. (org-move-subtree-down)
  6444. (end-of-line 1))
  6445. (defun org-insert-heading-respect-content ()
  6446. (interactive)
  6447. (let ((org-insert-heading-respect-content t))
  6448. (org-insert-heading t)))
  6449. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6450. (interactive "P")
  6451. (let ((org-insert-heading-respect-content t))
  6452. (org-insert-todo-heading force-state t)))
  6453. (defun org-insert-todo-heading (arg &optional force-heading)
  6454. "Insert a new heading with the same level and TODO state as current heading.
  6455. If the heading has no TODO state, or if the state is DONE, use the first
  6456. state (TODO by default). Also with prefix arg, force first state."
  6457. (interactive "P")
  6458. (when (or force-heading (not (org-insert-item 'checkbox)))
  6459. (org-insert-heading force-heading)
  6460. (save-excursion
  6461. (org-back-to-heading)
  6462. (outline-previous-heading)
  6463. (looking-at org-todo-line-regexp))
  6464. (let*
  6465. ((new-mark-x
  6466. (if (or arg
  6467. (not (match-beginning 2))
  6468. (member (match-string 2) org-done-keywords))
  6469. (car org-todo-keywords-1)
  6470. (match-string 2)))
  6471. (new-mark
  6472. (or
  6473. (run-hook-with-args-until-success
  6474. 'org-todo-get-default-hook new-mark-x nil)
  6475. new-mark-x)))
  6476. (beginning-of-line 1)
  6477. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6478. (if org-treat-insert-todo-heading-as-state-change
  6479. (org-todo new-mark)
  6480. (insert new-mark " "))))
  6481. (when org-provide-todo-statistics
  6482. (org-update-parent-todo-statistics))))
  6483. (defun org-insert-subheading (arg)
  6484. "Insert a new subheading and demote it.
  6485. Works for outline headings and for plain lists alike."
  6486. (interactive "P")
  6487. (org-insert-heading arg)
  6488. (cond
  6489. ((org-at-heading-p) (org-do-demote))
  6490. ((org-at-item-p) (org-indent-item))))
  6491. (defun org-insert-todo-subheading (arg)
  6492. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6493. Works for outline headings and for plain lists alike."
  6494. (interactive "P")
  6495. (org-insert-todo-heading arg)
  6496. (cond
  6497. ((org-at-heading-p) (org-do-demote))
  6498. ((org-at-item-p) (org-indent-item))))
  6499. ;;; Promotion and Demotion
  6500. (defvar org-after-demote-entry-hook nil
  6501. "Hook run after an entry has been demoted.
  6502. The cursor will be at the beginning of the entry.
  6503. When a subtree is being demoted, the hook will be called for each node.")
  6504. (defvar org-after-promote-entry-hook nil
  6505. "Hook run after an entry has been promoted.
  6506. The cursor will be at the beginning of the entry.
  6507. When a subtree is being promoted, the hook will be called for each node.")
  6508. (defun org-promote-subtree ()
  6509. "Promote the entire subtree.
  6510. See also `org-promote'."
  6511. (interactive)
  6512. (save-excursion
  6513. (org-with-limited-levels (org-map-tree 'org-promote)))
  6514. (org-fix-position-after-promote))
  6515. (defun org-demote-subtree ()
  6516. "Demote the entire subtree. See `org-demote'.
  6517. See also `org-promote'."
  6518. (interactive)
  6519. (save-excursion
  6520. (org-with-limited-levels (org-map-tree 'org-demote)))
  6521. (org-fix-position-after-promote))
  6522. (defun org-do-promote ()
  6523. "Promote the current heading higher up the tree.
  6524. If the region is active in `transient-mark-mode', promote all headings
  6525. in the region."
  6526. (interactive)
  6527. (save-excursion
  6528. (if (org-region-active-p)
  6529. (org-map-region 'org-promote (region-beginning) (region-end))
  6530. (org-promote)))
  6531. (org-fix-position-after-promote))
  6532. (defun org-do-demote ()
  6533. "Demote the current heading lower down the tree.
  6534. If the region is active in `transient-mark-mode', demote all headings
  6535. in the region."
  6536. (interactive)
  6537. (save-excursion
  6538. (if (org-region-active-p)
  6539. (org-map-region 'org-demote (region-beginning) (region-end))
  6540. (org-demote)))
  6541. (org-fix-position-after-promote))
  6542. (defun org-fix-position-after-promote ()
  6543. "Make sure that after pro/demotion cursor position is right."
  6544. (let ((pos (point)))
  6545. (when (save-excursion
  6546. (beginning-of-line 1)
  6547. (looking-at org-todo-line-regexp)
  6548. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6549. (cond ((eobp) (insert " "))
  6550. ((eolp) (insert " "))
  6551. ((equal (char-after) ?\ ) (forward-char 1))))))
  6552. (defun org-current-level ()
  6553. "Return the level of the current entry, or nil if before the first headline.
  6554. The level is the number of stars at the beginning of the headline."
  6555. (save-excursion
  6556. (org-with-limited-levels
  6557. (if (ignore-errors (org-back-to-heading t))
  6558. (funcall outline-level)))))
  6559. (defun org-get-previous-line-level ()
  6560. "Return the outline depth of the last headline before the current line.
  6561. Returns 0 for the first headline in the buffer, and nil if before the
  6562. first headline."
  6563. (let ((current-level (org-current-level))
  6564. (prev-level (when (> (line-number-at-pos) 1)
  6565. (save-excursion
  6566. (beginning-of-line 0)
  6567. (org-current-level)))))
  6568. (cond ((null current-level) nil) ; Before first headline
  6569. ((null prev-level) 0) ; At first headline
  6570. (prev-level))))
  6571. (defun org-reduced-level (l)
  6572. "Compute the effective level of a heading.
  6573. This takes into account the setting of `org-odd-levels-only'."
  6574. (cond
  6575. ((zerop l) 0)
  6576. (org-odd-levels-only (1+ (floor (/ l 2))))
  6577. (t l)))
  6578. (defun org-level-increment ()
  6579. "Return the number of stars that will be added or removed at a
  6580. time to headlines when structure editing, based on the value of
  6581. `org-odd-levels-only'."
  6582. (if org-odd-levels-only 2 1))
  6583. (defun org-get-valid-level (level &optional change)
  6584. "Rectify a level change under the influence of `org-odd-levels-only'
  6585. LEVEL is a current level, CHANGE is by how much the level should be
  6586. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6587. even level numbers will become the next higher odd number."
  6588. (if org-odd-levels-only
  6589. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6590. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6591. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6592. (max 1 (+ level (or change 0)))))
  6593. (if (boundp 'define-obsolete-function-alias)
  6594. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6595. (define-obsolete-function-alias 'org-get-legal-level
  6596. 'org-get-valid-level)
  6597. (define-obsolete-function-alias 'org-get-legal-level
  6598. 'org-get-valid-level "23.1")))
  6599. (defun org-promote ()
  6600. "Promote the current heading higher up the tree.
  6601. If the region is active in `transient-mark-mode', promote all headings
  6602. in the region."
  6603. (org-back-to-heading t)
  6604. (let* ((level (save-match-data (funcall outline-level)))
  6605. (after-change-functions (remove 'flyspell-after-change-function
  6606. after-change-functions))
  6607. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6608. (diff (abs (- level (length up-head) -1))))
  6609. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6610. (replace-match up-head nil t)
  6611. ;; Fixup tag positioning
  6612. (and org-auto-align-tags (org-set-tags nil t))
  6613. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6614. (run-hooks 'org-after-promote-entry-hook)))
  6615. (defun org-demote ()
  6616. "Demote the current heading lower down the tree.
  6617. If the region is active in `transient-mark-mode', demote all headings
  6618. in the region."
  6619. (org-back-to-heading t)
  6620. (let* ((level (save-match-data (funcall outline-level)))
  6621. (after-change-functions (remove 'flyspell-after-change-function
  6622. after-change-functions))
  6623. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6624. (diff (abs (- level (length down-head) -1))))
  6625. (replace-match down-head nil t)
  6626. ;; Fixup tag positioning
  6627. (and org-auto-align-tags (org-set-tags nil t))
  6628. (if org-adapt-indentation (org-fixup-indentation diff))
  6629. (run-hooks 'org-after-demote-entry-hook)))
  6630. (defun org-cycle-level ()
  6631. "Cycle the level of an empty headline through possible states.
  6632. This goes first to child, then to parent, level, then up the hierarchy.
  6633. After top level, it switches back to sibling level."
  6634. (interactive)
  6635. (let ((org-adapt-indentation nil))
  6636. (when (org-point-at-end-of-empty-headline)
  6637. (setq this-command 'org-cycle-level) ; Only needed for caching
  6638. (let ((cur-level (org-current-level))
  6639. (prev-level (org-get-previous-line-level)))
  6640. (cond
  6641. ;; If first headline in file, promote to top-level.
  6642. ((= prev-level 0)
  6643. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6644. do (org-do-promote)))
  6645. ;; If same level as prev, demote one.
  6646. ((= prev-level cur-level)
  6647. (org-do-demote))
  6648. ;; If parent is top-level, promote to top level if not already.
  6649. ((= prev-level 1)
  6650. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6651. do (org-do-promote)))
  6652. ;; If top-level, return to prev-level.
  6653. ((= cur-level 1)
  6654. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6655. do (org-do-demote)))
  6656. ;; If less than prev-level, promote one.
  6657. ((< cur-level prev-level)
  6658. (org-do-promote))
  6659. ;; If deeper than prev-level, promote until higher than
  6660. ;; prev-level.
  6661. ((> cur-level prev-level)
  6662. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6663. do (org-do-promote))))
  6664. t))))
  6665. (defun org-map-tree (fun)
  6666. "Call FUN for every heading underneath the current one."
  6667. (org-back-to-heading)
  6668. (let ((level (funcall outline-level)))
  6669. (save-excursion
  6670. (funcall fun)
  6671. (while (and (progn
  6672. (outline-next-heading)
  6673. (> (funcall outline-level) level))
  6674. (not (eobp)))
  6675. (funcall fun)))))
  6676. (defun org-map-region (fun beg end)
  6677. "Call FUN for every heading between BEG and END."
  6678. (let ((org-ignore-region t))
  6679. (save-excursion
  6680. (setq end (copy-marker end))
  6681. (goto-char beg)
  6682. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6683. (< (point) end))
  6684. (funcall fun))
  6685. (while (and (progn
  6686. (outline-next-heading)
  6687. (< (point) end))
  6688. (not (eobp)))
  6689. (funcall fun)))))
  6690. (defvar org-property-end-re) ; silence byte-compiler
  6691. (defun org-fixup-indentation (diff)
  6692. "Change the indentation in the current entry by DIFF.
  6693. However, if any line in the current entry has no indentation, or if it
  6694. would end up with no indentation after the change, nothing at all is done."
  6695. (save-excursion
  6696. (let ((end (save-excursion (outline-next-heading)
  6697. (point-marker)))
  6698. (prohibit (if (> diff 0)
  6699. "^\\S-"
  6700. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6701. col)
  6702. (unless (save-excursion (end-of-line 1)
  6703. (re-search-forward prohibit end t))
  6704. (while (and (< (point) end)
  6705. (re-search-forward "^[ \t]+" end t))
  6706. (goto-char (match-end 0))
  6707. (setq col (current-column))
  6708. (if (< diff 0) (replace-match ""))
  6709. (org-indent-to-column (+ diff col))))
  6710. (move-marker end nil))))
  6711. (defun org-convert-to-odd-levels ()
  6712. "Convert an org-mode file with all levels allowed to one with odd levels.
  6713. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6714. level 5 etc."
  6715. (interactive)
  6716. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6717. (let ((outline-level 'org-outline-level)
  6718. (org-odd-levels-only nil) n)
  6719. (save-excursion
  6720. (goto-char (point-min))
  6721. (while (re-search-forward "^\\*\\*+ " nil t)
  6722. (setq n (- (length (match-string 0)) 2))
  6723. (while (>= (setq n (1- n)) 0)
  6724. (org-demote))
  6725. (end-of-line 1))))))
  6726. (defun org-convert-to-oddeven-levels ()
  6727. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6728. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6729. file contains a section with an even level, conversion would
  6730. destroy the structure of the file. An error is signaled in this
  6731. case."
  6732. (interactive)
  6733. (goto-char (point-min))
  6734. ;; First check if there are no even levels
  6735. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6736. (org-show-context t)
  6737. (error "Not all levels are odd in this file. Conversion not possible"))
  6738. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6739. (let ((outline-regexp org-outline-regexp)
  6740. (outline-level 'org-outline-level)
  6741. (org-odd-levels-only nil) n)
  6742. (save-excursion
  6743. (goto-char (point-min))
  6744. (while (re-search-forward "^\\*\\*+ " nil t)
  6745. (setq n (/ (1- (length (match-string 0))) 2))
  6746. (while (>= (setq n (1- n)) 0)
  6747. (org-promote))
  6748. (end-of-line 1))))))
  6749. (defun org-tr-level (n)
  6750. "Make N odd if required."
  6751. (if org-odd-levels-only (1+ (/ n 2)) n))
  6752. ;;; Vertical tree motion, cutting and pasting of subtrees
  6753. (defun org-move-subtree-up (&optional arg)
  6754. "Move the current subtree up past ARG headlines of the same level."
  6755. (interactive "p")
  6756. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6757. (defun org-move-subtree-down (&optional arg)
  6758. "Move the current subtree down past ARG headlines of the same level."
  6759. (interactive "p")
  6760. (setq arg (prefix-numeric-value arg))
  6761. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6762. 'org-get-last-sibling))
  6763. (ins-point (make-marker))
  6764. (cnt (abs arg))
  6765. (col (current-column))
  6766. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6767. ;; Select the tree
  6768. (org-back-to-heading)
  6769. (setq beg0 (point))
  6770. (save-excursion
  6771. (setq ne-beg (org-back-over-empty-lines))
  6772. (setq beg (point)))
  6773. (save-match-data
  6774. (save-excursion (outline-end-of-heading)
  6775. (setq folded (outline-invisible-p)))
  6776. (outline-end-of-subtree))
  6777. (outline-next-heading)
  6778. (setq ne-end (org-back-over-empty-lines))
  6779. (setq end (point))
  6780. (goto-char beg0)
  6781. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6782. ;; include less whitespace
  6783. (save-excursion
  6784. (goto-char beg)
  6785. (forward-line (- ne-beg ne-end))
  6786. (setq beg (point))))
  6787. ;; Find insertion point, with error handling
  6788. (while (> cnt 0)
  6789. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6790. (progn (goto-char beg0)
  6791. (error "Cannot move past superior level or buffer limit")))
  6792. (setq cnt (1- cnt)))
  6793. (if (> arg 0)
  6794. ;; Moving forward - still need to move over subtree
  6795. (progn (org-end-of-subtree t t)
  6796. (save-excursion
  6797. (org-back-over-empty-lines)
  6798. (or (bolp) (newline)))))
  6799. (setq ne-ins (org-back-over-empty-lines))
  6800. (move-marker ins-point (point))
  6801. (setq txt (buffer-substring beg end))
  6802. (org-save-markers-in-region beg end)
  6803. (delete-region beg end)
  6804. (org-remove-empty-overlays-at beg)
  6805. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6806. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6807. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6808. (let ((bbb (point)))
  6809. (insert-before-markers txt)
  6810. (org-reinstall-markers-in-region bbb)
  6811. (move-marker ins-point bbb))
  6812. (or (bolp) (insert "\n"))
  6813. (setq ins-end (point))
  6814. (goto-char ins-point)
  6815. (org-skip-whitespace)
  6816. (when (and (< arg 0)
  6817. (org-first-sibling-p)
  6818. (> ne-ins ne-beg))
  6819. ;; Move whitespace back to beginning
  6820. (save-excursion
  6821. (goto-char ins-end)
  6822. (let ((kill-whole-line t))
  6823. (kill-line (- ne-ins ne-beg)) (point)))
  6824. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6825. (move-marker ins-point nil)
  6826. (if folded
  6827. (hide-subtree)
  6828. (org-show-entry)
  6829. (show-children)
  6830. (org-cycle-hide-drawers 'children))
  6831. (org-clean-visibility-after-subtree-move)
  6832. ;; move back to the initial column we were at
  6833. (move-to-column col)))
  6834. (defvar org-subtree-clip ""
  6835. "Clipboard for cut and paste of subtrees.
  6836. This is actually only a copy of the kill, because we use the normal kill
  6837. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6838. (defvar org-subtree-clip-folded nil
  6839. "Was the last copied subtree folded?
  6840. This is used to fold the tree back after pasting.")
  6841. (defun org-cut-subtree (&optional n)
  6842. "Cut the current subtree into the clipboard.
  6843. With prefix arg N, cut this many sequential subtrees.
  6844. This is a short-hand for marking the subtree and then cutting it."
  6845. (interactive "p")
  6846. (org-copy-subtree n 'cut))
  6847. (defun org-copy-subtree (&optional n cut force-store-markers)
  6848. "Cut the current subtree into the clipboard.
  6849. With prefix arg N, cut this many sequential subtrees.
  6850. This is a short-hand for marking the subtree and then copying it.
  6851. If CUT is non-nil, actually cut the subtree.
  6852. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6853. of some markers in the region, even if CUT is non-nil. This is
  6854. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6855. (interactive "p")
  6856. (let (beg end folded (beg0 (point)))
  6857. (if (org-called-interactively-p 'any)
  6858. (org-back-to-heading nil) ; take what looks like a subtree
  6859. (org-back-to-heading t)) ; take what is really there
  6860. (org-back-over-empty-lines)
  6861. (setq beg (point))
  6862. (skip-chars-forward " \t\r\n")
  6863. (save-match-data
  6864. (save-excursion (outline-end-of-heading)
  6865. (setq folded (outline-invisible-p)))
  6866. (condition-case nil
  6867. (org-forward-same-level (1- n) t)
  6868. (error nil))
  6869. (org-end-of-subtree t t))
  6870. (org-back-over-empty-lines)
  6871. (setq end (point))
  6872. (goto-char beg0)
  6873. (when (> end beg)
  6874. (setq org-subtree-clip-folded folded)
  6875. (when (or cut force-store-markers)
  6876. (org-save-markers-in-region beg end))
  6877. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6878. (setq org-subtree-clip (current-kill 0))
  6879. (message "%s: Subtree(s) with %d characters"
  6880. (if cut "Cut" "Copied")
  6881. (length org-subtree-clip)))))
  6882. (defun org-paste-subtree (&optional level tree for-yank)
  6883. "Paste the clipboard as a subtree, with modification of headline level.
  6884. The entire subtree is promoted or demoted in order to match a new headline
  6885. level.
  6886. If the cursor is at the beginning of a headline, the same level as
  6887. that headline is used to paste the tree
  6888. If not, the new level is derived from the *visible* headings
  6889. before and after the insertion point, and taken to be the inferior headline
  6890. level of the two. So if the previous visible heading is level 3 and the
  6891. next is level 4 (or vice versa), level 4 will be used for insertion.
  6892. This makes sure that the subtree remains an independent subtree and does
  6893. not swallow low level entries.
  6894. You can also force a different level, either by using a numeric prefix
  6895. argument, or by inserting the heading marker by hand. For example, if the
  6896. cursor is after \"*****\", then the tree will be shifted to level 5.
  6897. If optional TREE is given, use this text instead of the kill ring.
  6898. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6899. move back over whitespace before inserting, and move point to the end of
  6900. the inserted text when done."
  6901. (interactive "P")
  6902. (setq tree (or tree (and kill-ring (current-kill 0))))
  6903. (unless (org-kill-is-subtree-p tree)
  6904. (error "%s"
  6905. (substitute-command-keys
  6906. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6907. (org-with-limited-levels
  6908. (let* ((visp (not (outline-invisible-p)))
  6909. (txt tree)
  6910. (^re_ "\\(\\*+\\)[ \t]*")
  6911. (old-level (if (string-match org-outline-regexp-bol txt)
  6912. (- (match-end 0) (match-beginning 0) 1)
  6913. -1))
  6914. (force-level (cond (level (prefix-numeric-value level))
  6915. ((and (looking-at "[ \t]*$")
  6916. (string-match
  6917. "^\\*+$" (buffer-substring
  6918. (point-at-bol) (point))))
  6919. (- (match-end 1) (match-beginning 1)))
  6920. ((and (bolp)
  6921. (looking-at org-outline-regexp))
  6922. (- (match-end 0) (point) 1))
  6923. (t nil)))
  6924. (previous-level (save-excursion
  6925. (condition-case nil
  6926. (progn
  6927. (outline-previous-visible-heading 1)
  6928. (if (looking-at ^re_)
  6929. (- (match-end 0) (match-beginning 0) 1)
  6930. 1))
  6931. (error 1))))
  6932. (next-level (save-excursion
  6933. (condition-case nil
  6934. (progn
  6935. (or (looking-at org-outline-regexp)
  6936. (outline-next-visible-heading 1))
  6937. (if (looking-at ^re_)
  6938. (- (match-end 0) (match-beginning 0) 1)
  6939. 1))
  6940. (error 1))))
  6941. (new-level (or force-level (max previous-level next-level)))
  6942. (shift (if (or (= old-level -1)
  6943. (= new-level -1)
  6944. (= old-level new-level))
  6945. 0
  6946. (- new-level old-level)))
  6947. (delta (if (> shift 0) -1 1))
  6948. (func (if (> shift 0) 'org-demote 'org-promote))
  6949. (org-odd-levels-only nil)
  6950. beg end newend)
  6951. ;; Remove the forced level indicator
  6952. (if force-level
  6953. (delete-region (point-at-bol) (point)))
  6954. ;; Paste
  6955. (beginning-of-line (if (bolp) 1 2))
  6956. (unless for-yank (org-back-over-empty-lines))
  6957. (setq beg (point))
  6958. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6959. (insert-before-markers txt)
  6960. (unless (string-match "\n\\'" txt) (insert "\n"))
  6961. (setq newend (point))
  6962. (org-reinstall-markers-in-region beg)
  6963. (setq end (point))
  6964. (goto-char beg)
  6965. (skip-chars-forward " \t\n\r")
  6966. (setq beg (point))
  6967. (if (and (outline-invisible-p) visp)
  6968. (save-excursion (outline-show-heading)))
  6969. ;; Shift if necessary
  6970. (unless (= shift 0)
  6971. (save-restriction
  6972. (narrow-to-region beg end)
  6973. (while (not (= shift 0))
  6974. (org-map-region func (point-min) (point-max))
  6975. (setq shift (+ delta shift)))
  6976. (goto-char (point-min))
  6977. (setq newend (point-max))))
  6978. (when (or (org-called-interactively-p 'interactive) for-yank)
  6979. (message "Clipboard pasted as level %d subtree" new-level))
  6980. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6981. kill-ring
  6982. (eq org-subtree-clip (current-kill 0))
  6983. org-subtree-clip-folded)
  6984. ;; The tree was folded before it was killed/copied
  6985. (hide-subtree))
  6986. (and for-yank (goto-char newend)))))
  6987. (defun org-kill-is-subtree-p (&optional txt)
  6988. "Check if the current kill is an outline subtree, or a set of trees.
  6989. Returns nil if kill does not start with a headline, or if the first
  6990. headline level is not the largest headline level in the tree.
  6991. So this will actually accept several entries of equal levels as well,
  6992. which is OK for `org-paste-subtree'.
  6993. If optional TXT is given, check this string instead of the current kill."
  6994. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6995. (re (org-get-limited-outline-regexp))
  6996. (^re (concat "^" re))
  6997. (start-level (and kill
  6998. (string-match
  6999. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7000. kill)
  7001. (- (match-end 2) (match-beginning 2) 1)))
  7002. (start (1+ (or (match-beginning 2) -1))))
  7003. (if (not start-level)
  7004. (progn
  7005. nil) ;; does not even start with a heading
  7006. (catch 'exit
  7007. (while (setq start (string-match ^re kill (1+ start)))
  7008. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7009. (throw 'exit nil)))
  7010. t))))
  7011. (defvar org-markers-to-move nil
  7012. "Markers that should be moved with a cut-and-paste operation.
  7013. Those markers are stored together with their positions relative to
  7014. the start of the region.")
  7015. (defun org-save-markers-in-region (beg end)
  7016. "Check markers in region.
  7017. If these markers are between BEG and END, record their position relative
  7018. to BEG, so that after moving the block of text, we can put the markers back
  7019. into place.
  7020. This function gets called just before an entry or tree gets cut from the
  7021. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7022. called immediately, to move the markers with the entries."
  7023. (setq org-markers-to-move nil)
  7024. (when (featurep 'org-clock)
  7025. (org-clock-save-markers-for-cut-and-paste beg end))
  7026. (when (featurep 'org-agenda)
  7027. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7028. (defun org-check-and-save-marker (marker beg end)
  7029. "Check if MARKER is between BEG and END.
  7030. If yes, remember the marker and the distance to BEG."
  7031. (when (and (marker-buffer marker)
  7032. (equal (marker-buffer marker) (current-buffer)))
  7033. (if (and (>= marker beg) (< marker end))
  7034. (push (cons marker (- marker beg)) org-markers-to-move))))
  7035. (defun org-reinstall-markers-in-region (beg)
  7036. "Move all remembered markers to their position relative to BEG."
  7037. (mapc (lambda (x)
  7038. (move-marker (car x) (+ beg (cdr x))))
  7039. org-markers-to-move)
  7040. (setq org-markers-to-move nil))
  7041. (defun org-narrow-to-subtree ()
  7042. "Narrow buffer to the current subtree."
  7043. (interactive)
  7044. (save-excursion
  7045. (save-match-data
  7046. (org-with-limited-levels
  7047. (narrow-to-region
  7048. (progn (org-back-to-heading t) (point))
  7049. (progn (org-end-of-subtree t t)
  7050. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7051. (point)))))))
  7052. (defun org-narrow-to-block ()
  7053. "Narrow buffer to the current block."
  7054. (interactive)
  7055. (let* ((case-fold-search t)
  7056. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7057. "^[ \t]*#\\+end_.*")))
  7058. (if blockp
  7059. (narrow-to-region (car blockp) (cdr blockp))
  7060. (error "Not in a block"))))
  7061. (eval-when-compile
  7062. (defvar org-property-drawer-re))
  7063. (defvar org-property-start-re) ;; defined below
  7064. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7065. "Clone the task (subtree) at point N times.
  7066. The clones will be inserted as siblings.
  7067. In interactive use, the user will be prompted for the number of
  7068. clones to be produced, and for a time SHIFT, which may be a
  7069. repeater as used in time stamps, for example `+3d'.
  7070. When a valid repeater is given and the entry contains any time
  7071. stamps, the clones will become a sequence in time, with time
  7072. stamps in the subtree shifted for each clone produced. If SHIFT
  7073. is nil or the empty string, time stamps will be left alone. The
  7074. ID property of the original subtree is removed.
  7075. If the original subtree did contain time stamps with a repeater,
  7076. the following will happen:
  7077. - the repeater will be removed in each clone
  7078. - an additional clone will be produced, with the current, unshifted
  7079. date(s) in the entry.
  7080. - the original entry will be placed *after* all the clones, with
  7081. repeater intact.
  7082. - the start days in the repeater in the original entry will be shifted
  7083. to past the last clone.
  7084. I this way you can spell out a number of instances of a repeating task,
  7085. and still retain the repeater to cover future instances of the task."
  7086. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7087. (let (beg end template task idprop
  7088. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7089. (drawer-re org-drawer-regexp))
  7090. (if (not (and (integerp n) (> n 0)))
  7091. (error "Invalid number of replications %s" n))
  7092. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7093. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  7094. shift)))
  7095. (error "Invalid shift specification %s" shift))
  7096. (when doshift
  7097. (setq shift-n (string-to-number (match-string 1 shift))
  7098. shift-what (cdr (assoc (match-string 2 shift)
  7099. '(("d" . day) ("w" . week)
  7100. ("m" . month) ("y" . year))))))
  7101. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7102. (setq nmin 1 nmax n)
  7103. (org-back-to-heading t)
  7104. (setq beg (point))
  7105. (setq idprop (org-entry-get nil "ID"))
  7106. (org-end-of-subtree t t)
  7107. (or (bolp) (insert "\n"))
  7108. (setq end (point))
  7109. (setq template (buffer-substring beg end))
  7110. (when (and doshift
  7111. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  7112. (delete-region beg end)
  7113. (setq end beg)
  7114. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7115. (goto-char end)
  7116. (loop for n from nmin to nmax do
  7117. ;; prepare clone
  7118. (with-temp-buffer
  7119. (insert template)
  7120. (org-mode)
  7121. (goto-char (point-min))
  7122. (org-show-subtree)
  7123. (and idprop (if org-clone-delete-id
  7124. (org-entry-delete nil "ID")
  7125. (org-id-get-create t)))
  7126. (unless (= n 0)
  7127. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7128. (kill-whole-line))
  7129. (goto-char (point-min))
  7130. (while (re-search-forward drawer-re nil t)
  7131. (mapc (lambda (d)
  7132. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7133. (goto-char (point-min))
  7134. (when doshift
  7135. (while (re-search-forward org-ts-regexp-both nil t)
  7136. (org-timestamp-change (* n shift-n) shift-what))
  7137. (unless (= n n-no-remove)
  7138. (goto-char (point-min))
  7139. (while (re-search-forward org-ts-regexp nil t)
  7140. (save-excursion
  7141. (goto-char (match-beginning 0))
  7142. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  7143. (delete-region (match-beginning 1) (match-end 1)))))))
  7144. (setq task (buffer-string)))
  7145. (insert task))
  7146. (goto-char beg)))
  7147. ;;; Outline Sorting
  7148. (defun org-sort (with-case)
  7149. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7150. Optional argument WITH-CASE means sort case-sensitively."
  7151. (interactive "P")
  7152. (cond
  7153. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7154. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7155. (t
  7156. (org-call-with-arg 'org-sort-entries with-case))))
  7157. (defun org-sort-remove-invisible (s)
  7158. (remove-text-properties 0 (length s) org-rm-props s)
  7159. (while (string-match org-bracket-link-regexp s)
  7160. (setq s (replace-match (if (match-end 2)
  7161. (match-string 3 s)
  7162. (match-string 1 s)) t t s)))
  7163. s)
  7164. (defvar org-priority-regexp) ; defined later in the file
  7165. (defvar org-after-sorting-entries-or-items-hook nil
  7166. "Hook that is run after a bunch of entries or items have been sorted.
  7167. When children are sorted, the cursor is in the parent line when this
  7168. hook gets called. When a region or a plain list is sorted, the cursor
  7169. will be in the first entry of the sorted region/list.")
  7170. (defun org-sort-entries
  7171. (&optional with-case sorting-type getkey-func compare-func property)
  7172. "Sort entries on a certain level of an outline tree.
  7173. If there is an active region, the entries in the region are sorted.
  7174. Else, if the cursor is before the first entry, sort the top-level items.
  7175. Else, the children of the entry at point are sorted.
  7176. Sorting can be alphabetically, numerically, by date/time as given by
  7177. a time stamp, by a property or by priority.
  7178. The command prompts for the sorting type unless it has been given to the
  7179. function through the SORTING-TYPE argument, which needs to be a character,
  7180. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  7181. precise meaning of each character:
  7182. n Numerically, by converting the beginning of the entry/item to a number.
  7183. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7184. t By date/time, either the first active time stamp in the entry, or, if
  7185. none exist, by the first inactive one.
  7186. s By the scheduled date/time.
  7187. d By deadline date/time.
  7188. c By creation time, which is assumed to be the first inactive time stamp
  7189. at the beginning of a line.
  7190. p By priority according to the cookie.
  7191. r By the value of a property.
  7192. Capital letters will reverse the sort order.
  7193. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7194. called with point at the beginning of the record. It must return either
  7195. a string or a number that should serve as the sorting key for that record.
  7196. Comparing entries ignores case by default. However, with an optional argument
  7197. WITH-CASE, the sorting considers case as well."
  7198. (interactive "P")
  7199. (let ((case-func (if with-case 'identity 'downcase))
  7200. start beg end stars re re2
  7201. txt what tmp)
  7202. ;; Find beginning and end of region to sort
  7203. (cond
  7204. ((org-region-active-p)
  7205. ;; we will sort the region
  7206. (setq end (region-end)
  7207. what "region")
  7208. (goto-char (region-beginning))
  7209. (if (not (org-at-heading-p)) (outline-next-heading))
  7210. (setq start (point)))
  7211. ((or (org-at-heading-p)
  7212. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7213. ;; we will sort the children of the current headline
  7214. (org-back-to-heading)
  7215. (setq start (point)
  7216. end (progn (org-end-of-subtree t t)
  7217. (or (bolp) (insert "\n"))
  7218. (org-back-over-empty-lines)
  7219. (point))
  7220. what "children")
  7221. (goto-char start)
  7222. (show-subtree)
  7223. (outline-next-heading))
  7224. (t
  7225. ;; we will sort the top-level entries in this file
  7226. (goto-char (point-min))
  7227. (or (org-at-heading-p) (outline-next-heading))
  7228. (setq start (point))
  7229. (goto-char (point-max))
  7230. (beginning-of-line 1)
  7231. (when (looking-at ".*?\\S-")
  7232. ;; File ends in a non-white line
  7233. (end-of-line 1)
  7234. (insert "\n"))
  7235. (setq end (point-max))
  7236. (setq what "top-level")
  7237. (goto-char start)
  7238. (show-all)))
  7239. (setq beg (point))
  7240. (if (>= beg end) (error "Nothing to sort"))
  7241. (looking-at "\\(\\*+\\)")
  7242. (setq stars (match-string 1)
  7243. re (concat "^" (regexp-quote stars) " +")
  7244. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7245. txt (buffer-substring beg end))
  7246. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7247. (if (and (not (equal stars "*")) (string-match re2 txt))
  7248. (error "Region to sort contains a level above the first entry"))
  7249. (unless sorting-type
  7250. (message
  7251. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7252. [t]ime [s]cheduled [d]eadline [c]reated
  7253. A/N/T/S/D/C/P/O/F means reversed:"
  7254. what)
  7255. (setq sorting-type (read-char-exclusive))
  7256. (and (= (downcase sorting-type) ?f)
  7257. (setq getkey-func
  7258. (org-icompleting-read "Sort using function: "
  7259. obarray 'fboundp t nil nil))
  7260. (setq getkey-func (intern getkey-func)))
  7261. (and (= (downcase sorting-type) ?r)
  7262. (setq property
  7263. (org-icompleting-read "Property: "
  7264. (mapcar 'list (org-buffer-property-keys t))
  7265. nil t))))
  7266. (message "Sorting entries...")
  7267. (save-restriction
  7268. (narrow-to-region start end)
  7269. (let ((dcst (downcase sorting-type))
  7270. (case-fold-search nil)
  7271. (now (current-time)))
  7272. (sort-subr
  7273. (/= dcst sorting-type)
  7274. ;; This function moves to the beginning character of the "record" to
  7275. ;; be sorted.
  7276. (lambda nil
  7277. (if (re-search-forward re nil t)
  7278. (goto-char (match-beginning 0))
  7279. (goto-char (point-max))))
  7280. ;; This function moves to the last character of the "record" being
  7281. ;; sorted.
  7282. (lambda nil
  7283. (save-match-data
  7284. (condition-case nil
  7285. (outline-forward-same-level 1)
  7286. (error
  7287. (goto-char (point-max))))))
  7288. ;; This function returns the value that gets sorted against.
  7289. (lambda nil
  7290. (cond
  7291. ((= dcst ?n)
  7292. (if (looking-at org-complex-heading-regexp)
  7293. (string-to-number (match-string 4))
  7294. nil))
  7295. ((= dcst ?a)
  7296. (if (looking-at org-complex-heading-regexp)
  7297. (funcall case-func (match-string 4))
  7298. nil))
  7299. ((= dcst ?t)
  7300. (let ((end (save-excursion (outline-next-heading) (point))))
  7301. (if (or (re-search-forward org-ts-regexp end t)
  7302. (re-search-forward org-ts-regexp-both end t))
  7303. (org-time-string-to-seconds (match-string 0))
  7304. (org-float-time now))))
  7305. ((= dcst ?c)
  7306. (let ((end (save-excursion (outline-next-heading) (point))))
  7307. (if (re-search-forward
  7308. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7309. end t)
  7310. (org-time-string-to-seconds (match-string 0))
  7311. (org-float-time now))))
  7312. ((= dcst ?s)
  7313. (let ((end (save-excursion (outline-next-heading) (point))))
  7314. (if (re-search-forward org-scheduled-time-regexp end t)
  7315. (org-time-string-to-seconds (match-string 1))
  7316. (org-float-time now))))
  7317. ((= dcst ?d)
  7318. (let ((end (save-excursion (outline-next-heading) (point))))
  7319. (if (re-search-forward org-deadline-time-regexp end t)
  7320. (org-time-string-to-seconds (match-string 1))
  7321. (org-float-time now))))
  7322. ((= dcst ?p)
  7323. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7324. (string-to-char (match-string 2))
  7325. org-default-priority))
  7326. ((= dcst ?r)
  7327. (or (org-entry-get nil property) ""))
  7328. ((= dcst ?o)
  7329. (if (looking-at org-complex-heading-regexp)
  7330. (- 9999 (length (member (match-string 2)
  7331. org-todo-keywords-1)))))
  7332. ((= dcst ?f)
  7333. (if getkey-func
  7334. (progn
  7335. (setq tmp (funcall getkey-func))
  7336. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7337. tmp)
  7338. (error "Invalid key function `%s'" getkey-func)))
  7339. (t (error "Invalid sorting type `%c'" sorting-type))))
  7340. nil
  7341. (cond
  7342. ((= dcst ?a) 'string<)
  7343. ((= dcst ?f) compare-func)
  7344. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7345. (t nil)))))
  7346. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7347. (message "Sorting entries...done")))
  7348. (defun org-do-sort (table what &optional with-case sorting-type)
  7349. "Sort TABLE of WHAT according to SORTING-TYPE.
  7350. The user will be prompted for the SORTING-TYPE if the call to this
  7351. function does not specify it. WHAT is only for the prompt, to indicate
  7352. what is being sorted. The sorting key will be extracted from
  7353. the car of the elements of the table.
  7354. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7355. (unless sorting-type
  7356. (message
  7357. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7358. what)
  7359. (setq sorting-type (read-char-exclusive)))
  7360. (let ((dcst (downcase sorting-type))
  7361. extractfun comparefun)
  7362. ;; Define the appropriate functions
  7363. (cond
  7364. ((= dcst ?n)
  7365. (setq extractfun 'string-to-number
  7366. comparefun (if (= dcst sorting-type) '< '>)))
  7367. ((= dcst ?a)
  7368. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7369. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7370. comparefun (if (= dcst sorting-type)
  7371. 'string<
  7372. (lambda (a b) (and (not (string< a b))
  7373. (not (string= a b)))))))
  7374. ((= dcst ?t)
  7375. (setq extractfun
  7376. (lambda (x)
  7377. (if (or (string-match org-ts-regexp x)
  7378. (string-match org-ts-regexp-both x))
  7379. (org-float-time
  7380. (org-time-string-to-time (match-string 0 x)))
  7381. 0))
  7382. comparefun (if (= dcst sorting-type) '< '>)))
  7383. (t (error "Invalid sorting type `%c'" sorting-type)))
  7384. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7385. table)
  7386. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7387. ;;; The orgstruct minor mode
  7388. ;; Define a minor mode which can be used in other modes in order to
  7389. ;; integrate the org-mode structure editing commands.
  7390. ;; This is really a hack, because the org-mode structure commands use
  7391. ;; keys which normally belong to the major mode. Here is how it
  7392. ;; works: The minor mode defines all the keys necessary to operate the
  7393. ;; structure commands, but wraps the commands into a function which
  7394. ;; tests if the cursor is currently at a headline or a plain list
  7395. ;; item. If that is the case, the structure command is used,
  7396. ;; temporarily setting many Org-mode variables like regular
  7397. ;; expressions for filling etc. However, when any of those keys is
  7398. ;; used at a different location, function uses `key-binding' to look
  7399. ;; up if the key has an associated command in another currently active
  7400. ;; keymap (minor modes, major mode, global), and executes that
  7401. ;; command. There might be problems if any of the keys is otherwise
  7402. ;; used as a prefix key.
  7403. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7404. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7405. ;; addresses this by checking explicitly for both bindings.
  7406. (defvar orgstruct-mode-map (make-sparse-keymap)
  7407. "Keymap for the minor `orgstruct-mode'.")
  7408. (defvar org-local-vars nil
  7409. "List of local variables, for use by `orgstruct-mode'.")
  7410. ;;;###autoload
  7411. (define-minor-mode orgstruct-mode
  7412. "Toggle the minor mode `orgstruct-mode'.
  7413. This mode is for using Org-mode structure commands in other
  7414. modes. The following keys behave as if Org-mode were active, if
  7415. the cursor is on a headline, or on a plain list item (both as
  7416. defined by Org-mode).
  7417. M-up Move entry/item up
  7418. M-down Move entry/item down
  7419. M-left Promote
  7420. M-right Demote
  7421. M-S-up Move entry/item up
  7422. M-S-down Move entry/item down
  7423. M-S-left Promote subtree
  7424. M-S-right Demote subtree
  7425. M-q Fill paragraph and items like in Org-mode
  7426. C-c ^ Sort entries
  7427. C-c - Cycle list bullet
  7428. TAB Cycle item visibility
  7429. M-RET Insert new heading/item
  7430. S-M-RET Insert new TODO heading / Checkbox item
  7431. C-c C-c Set tags / toggle checkbox"
  7432. nil " OrgStruct" nil
  7433. (org-load-modules-maybe)
  7434. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7435. ;;;###autoload
  7436. (defun turn-on-orgstruct ()
  7437. "Unconditionally turn on `orgstruct-mode'."
  7438. (orgstruct-mode 1))
  7439. (defun orgstruct++-mode (&optional arg)
  7440. "Toggle `orgstruct-mode', the enhanced version of it.
  7441. In addition to setting orgstruct-mode, this also exports all indentation
  7442. and autofilling variables from org-mode into the buffer. It will also
  7443. recognize item context in multiline items.
  7444. Note that turning off orgstruct-mode will *not* remove the
  7445. indentation/paragraph settings. This can only be done by refreshing the
  7446. major mode, for example with \\[normal-mode]."
  7447. (interactive "P")
  7448. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7449. (if (< arg 1)
  7450. (orgstruct-mode -1)
  7451. (orgstruct-mode 1)
  7452. (let (var val)
  7453. (mapc
  7454. (lambda (x)
  7455. (when (string-match
  7456. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7457. (symbol-name (car x)))
  7458. (setq var (car x) val (nth 1 x))
  7459. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7460. org-local-vars)
  7461. (org-set-local 'orgstruct-is-++ t))))
  7462. (defvar orgstruct-is-++ nil
  7463. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7464. (make-variable-buffer-local 'orgstruct-is-++)
  7465. ;;;###autoload
  7466. (defun turn-on-orgstruct++ ()
  7467. "Unconditionally turn on `orgstruct++-mode'."
  7468. (orgstruct++-mode 1))
  7469. (defun orgstruct-error ()
  7470. "Error when there is no default binding for a structure key."
  7471. (interactive)
  7472. (error "This key has no function outside structure elements"))
  7473. (defun orgstruct-setup ()
  7474. "Setup orgstruct keymaps."
  7475. (let ((nfunc 0)
  7476. (bindings
  7477. (list
  7478. '([(meta up)] org-metaup)
  7479. '([(meta down)] org-metadown)
  7480. '([(meta left)] org-metaleft)
  7481. '([(meta right)] org-metaright)
  7482. '([(meta shift up)] org-shiftmetaup)
  7483. '([(meta shift down)] org-shiftmetadown)
  7484. '([(meta shift left)] org-shiftmetaleft)
  7485. '([(meta shift right)] org-shiftmetaright)
  7486. '([?\e (up)] org-metaup)
  7487. '([?\e (down)] org-metadown)
  7488. '([?\e (left)] org-metaleft)
  7489. '([?\e (right)] org-metaright)
  7490. '([?\e (shift up)] org-shiftmetaup)
  7491. '([?\e (shift down)] org-shiftmetadown)
  7492. '([?\e (shift left)] org-shiftmetaleft)
  7493. '([?\e (shift right)] org-shiftmetaright)
  7494. '([(shift up)] org-shiftup)
  7495. '([(shift down)] org-shiftdown)
  7496. '([(shift left)] org-shiftleft)
  7497. '([(shift right)] org-shiftright)
  7498. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7499. '("\M-q" fill-paragraph)
  7500. '("\C-c^" org-sort)
  7501. '("\C-c-" org-cycle-list-bullet)))
  7502. elt key fun cmd)
  7503. (while (setq elt (pop bindings))
  7504. (setq nfunc (1+ nfunc))
  7505. (setq key (org-key (car elt))
  7506. fun (nth 1 elt)
  7507. cmd (orgstruct-make-binding fun nfunc key))
  7508. (org-defkey orgstruct-mode-map key cmd))
  7509. ;; Special treatment needed for TAB and RET
  7510. (org-defkey orgstruct-mode-map [(tab)]
  7511. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7512. (org-defkey orgstruct-mode-map "\C-i"
  7513. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7514. (org-defkey orgstruct-mode-map "\M-\C-m"
  7515. (orgstruct-make-binding 'org-insert-heading 105
  7516. "\M-\C-m" [(meta return)]))
  7517. (org-defkey orgstruct-mode-map [(meta return)]
  7518. (orgstruct-make-binding 'org-insert-heading 106
  7519. [(meta return)] "\M-\C-m"))
  7520. (org-defkey orgstruct-mode-map [(shift meta return)]
  7521. (orgstruct-make-binding 'org-insert-todo-heading 107
  7522. [(meta return)] "\M-\C-m"))
  7523. (org-defkey orgstruct-mode-map "\e\C-m"
  7524. (orgstruct-make-binding 'org-insert-heading 108
  7525. "\e\C-m" [?\e (return)]))
  7526. (org-defkey orgstruct-mode-map [?\e (return)]
  7527. (orgstruct-make-binding 'org-insert-heading 109
  7528. [?\e (return)] "\e\C-m"))
  7529. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7530. (orgstruct-make-binding 'org-insert-todo-heading 110
  7531. [?\e (return)] "\e\C-m"))
  7532. (unless org-local-vars
  7533. (setq org-local-vars (org-get-local-variables)))
  7534. t))
  7535. (defun orgstruct-make-binding (fun n &rest keys)
  7536. "Create a function for binding in the structure minor mode.
  7537. FUN is the command to call inside a table. N is used to create a unique
  7538. command name. KEYS are keys that should be checked in for a command
  7539. to execute outside of tables."
  7540. (eval
  7541. (list 'defun
  7542. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7543. '(arg)
  7544. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7545. "Outside of structure, run the binding of `"
  7546. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7547. "'.")
  7548. '(interactive "p")
  7549. (list 'if
  7550. `(org-context-p 'headline 'item
  7551. (and orgstruct-is-++
  7552. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7553. 'item-body))
  7554. (list 'org-run-like-in-org-mode (list 'quote fun))
  7555. (list 'let '(orgstruct-mode)
  7556. (list 'call-interactively
  7557. (append '(or)
  7558. (mapcar (lambda (k)
  7559. (list 'key-binding k))
  7560. keys)
  7561. '('orgstruct-error))))))))
  7562. (defun org-context-p (&rest contexts)
  7563. "Check if local context is any of CONTEXTS.
  7564. Possible values in the list of contexts are `table', `headline', and `item'."
  7565. (let ((pos (point)))
  7566. (goto-char (point-at-bol))
  7567. (prog1 (or (and (memq 'table contexts)
  7568. (looking-at "[ \t]*|"))
  7569. (and (memq 'headline contexts)
  7570. (looking-at org-outline-regexp))
  7571. (and (memq 'item contexts)
  7572. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7573. (and (memq 'item-body contexts)
  7574. (org-in-item-p)))
  7575. (goto-char pos))))
  7576. (defun org-get-local-variables ()
  7577. "Return a list of all local variables in an org-mode buffer."
  7578. (let (varlist)
  7579. (with-current-buffer (get-buffer-create "*Org tmp*")
  7580. (erase-buffer)
  7581. (org-mode)
  7582. (setq varlist (buffer-local-variables)))
  7583. (kill-buffer "*Org tmp*")
  7584. (delq nil
  7585. (mapcar
  7586. (lambda (x)
  7587. (setq x
  7588. (if (symbolp x)
  7589. (list x)
  7590. (list (car x) (list 'quote (cdr x)))))
  7591. (if (string-match
  7592. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7593. (symbol-name (car x)))
  7594. x nil))
  7595. varlist))))
  7596. (defun org-clone-local-variables (from-buffer &optional regexp)
  7597. "Clone local variables from FROM-BUFFER.
  7598. Optional argument REGEXP selects variables to clone."
  7599. (mapc
  7600. (lambda (pair)
  7601. (and (symbolp (car pair))
  7602. (or (null regexp)
  7603. (string-match regexp (symbol-name (car pair))))
  7604. (set (make-local-variable (car pair))
  7605. (cdr pair))))
  7606. (buffer-local-variables from-buffer)))
  7607. ;;;###autoload
  7608. (defun org-run-like-in-org-mode (cmd)
  7609. "Run a command, pretending that the current buffer is in Org-mode.
  7610. This will temporarily bind local variables that are typically bound in
  7611. Org-mode to the values they have in Org-mode, and then interactively
  7612. call CMD."
  7613. (org-load-modules-maybe)
  7614. (unless org-local-vars
  7615. (setq org-local-vars (org-get-local-variables)))
  7616. (eval (list 'let org-local-vars
  7617. (list 'call-interactively (list 'quote cmd)))))
  7618. ;;;; Archiving
  7619. (defun org-get-category (&optional pos force-refresh)
  7620. "Get the category applying to position POS."
  7621. (if force-refresh (org-refresh-category-properties))
  7622. (let ((pos (or pos (point))))
  7623. (or (get-text-property pos 'org-category)
  7624. (progn (org-refresh-category-properties)
  7625. (get-text-property pos 'org-category)))))
  7626. (defun org-refresh-category-properties ()
  7627. "Refresh category text properties in the buffer."
  7628. (let ((def-cat (cond
  7629. ((null org-category)
  7630. (if buffer-file-name
  7631. (file-name-sans-extension
  7632. (file-name-nondirectory buffer-file-name))
  7633. "???"))
  7634. ((symbolp org-category) (symbol-name org-category))
  7635. (t org-category)))
  7636. beg end cat pos optionp)
  7637. (org-unmodified
  7638. (save-excursion
  7639. (save-restriction
  7640. (widen)
  7641. (goto-char (point-min))
  7642. (put-text-property (point) (point-max) 'org-category def-cat)
  7643. (while (re-search-forward
  7644. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7645. (setq pos (match-end 0)
  7646. optionp (equal (char-after (match-beginning 0)) ?#)
  7647. cat (org-trim (match-string 2)))
  7648. (if optionp
  7649. (setq beg (point-at-bol) end (point-max))
  7650. (org-back-to-heading t)
  7651. (setq beg (point) end (org-end-of-subtree t t)))
  7652. (put-text-property beg end 'org-category cat)
  7653. (put-text-property beg end 'org-category-position beg)
  7654. (goto-char pos)))))))
  7655. ;;;; Link Stuff
  7656. ;;; Link abbreviations
  7657. (defun org-link-expand-abbrev (link)
  7658. "Apply replacements as defined in `org-link-abbrev-alist."
  7659. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7660. (let* ((key (match-string 1 link))
  7661. (as (or (assoc key org-link-abbrev-alist-local)
  7662. (assoc key org-link-abbrev-alist)))
  7663. (tag (and (match-end 2) (match-string 3 link)))
  7664. rpl)
  7665. (if (not as)
  7666. link
  7667. (setq rpl (cdr as))
  7668. (cond
  7669. ((symbolp rpl) (funcall rpl tag))
  7670. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7671. ((string-match "%h" rpl)
  7672. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7673. (t (concat rpl tag)))))
  7674. link))
  7675. ;;; Storing and inserting links
  7676. (defvar org-insert-link-history nil
  7677. "Minibuffer history for links inserted with `org-insert-link'.")
  7678. (defvar org-stored-links nil
  7679. "Contains the links stored with `org-store-link'.")
  7680. (defvar org-store-link-plist nil
  7681. "Plist with info about the most recently link created with `org-store-link'.")
  7682. (defvar org-link-protocols nil
  7683. "Link protocols added to Org-mode using `org-add-link-type'.")
  7684. (defvar org-store-link-functions nil
  7685. "List of functions that are called to create and store a link.
  7686. Each function will be called in turn until one returns a non-nil
  7687. value. Each function should check if it is responsible for creating
  7688. this link (for example by looking at the major mode).
  7689. If not, it must exit and return nil.
  7690. If yes, it should return a non-nil value after a calling
  7691. `org-store-link-props' with a list of properties and values.
  7692. Special properties are:
  7693. :type The link prefix, like \"http\". This must be given.
  7694. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7695. This is obligatory as well.
  7696. :description Optional default description for the second pair
  7697. of brackets in an Org-mode link. The user can still change
  7698. this when inserting this link into an Org-mode buffer.
  7699. In addition to these, any additional properties can be specified
  7700. and then used in remember templates.")
  7701. (defun org-add-link-type (type &optional follow export)
  7702. "Add TYPE to the list of `org-link-types'.
  7703. Re-compute all regular expressions depending on `org-link-types'
  7704. FOLLOW and EXPORT are two functions.
  7705. FOLLOW should take the link path as the single argument and do whatever
  7706. is necessary to follow the link, for example find a file or display
  7707. a mail message.
  7708. EXPORT should format the link path for export to one of the export formats.
  7709. It should be a function accepting three arguments:
  7710. path the path of the link, the text after the prefix (like \"http:\")
  7711. desc the description of the link, if any, or a description added by
  7712. org-export-normalize-links if there is none
  7713. format the export format, a symbol like `html' or `latex' or `ascii'..
  7714. The function may use the FORMAT information to return different values
  7715. depending on the format. The return value will be put literally into
  7716. the exported file. If the return value is nil, this means Org should
  7717. do what it normally does with links which do not have EXPORT defined.
  7718. Org-mode has a built-in default for exporting links. If you are happy with
  7719. this default, there is no need to define an export function for the link
  7720. type. For a simple example of an export function, see `org-bbdb.el'."
  7721. (add-to-list 'org-link-types type t)
  7722. (org-make-link-regexps)
  7723. (if (assoc type org-link-protocols)
  7724. (setcdr (assoc type org-link-protocols) (list follow export))
  7725. (push (list type follow export) org-link-protocols)))
  7726. (defvar org-agenda-buffer-name)
  7727. ;;;###autoload
  7728. (defun org-store-link (arg)
  7729. "\\<org-mode-map>Store an org-link to the current location.
  7730. This link is added to `org-stored-links' and can later be inserted
  7731. into an org-buffer with \\[org-insert-link].
  7732. For some link types, a prefix arg is interpreted:
  7733. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7734. For file links, arg negates `org-context-in-file-links'."
  7735. (interactive "P")
  7736. (org-load-modules-maybe)
  7737. (setq org-store-link-plist nil) ; reset
  7738. (org-with-limited-levels
  7739. (let (link cpltxt desc description search txt custom-id agenda-link)
  7740. (cond
  7741. ((run-hook-with-args-until-success 'org-store-link-functions)
  7742. (setq link (plist-get org-store-link-plist :link)
  7743. desc (or (plist-get org-store-link-plist :description) link)))
  7744. ((equal (buffer-name) "*Org Edit Src Example*")
  7745. (let (label gc)
  7746. (while (or (not label)
  7747. (save-excursion
  7748. (save-restriction
  7749. (widen)
  7750. (goto-char (point-min))
  7751. (re-search-forward
  7752. (regexp-quote (format org-coderef-label-format label))
  7753. nil t))))
  7754. (when label (message "Label exists already") (sit-for 2))
  7755. (setq label (read-string "Code line label: " label)))
  7756. (end-of-line 1)
  7757. (setq link (format org-coderef-label-format label))
  7758. (setq gc (- 79 (length link)))
  7759. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7760. (insert link)
  7761. (setq link (concat "(" label ")") desc nil)))
  7762. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7763. ;; We are in the agenda, link to referenced location
  7764. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7765. (get-text-property (point) 'org-marker))))
  7766. (when m
  7767. (org-with-point-at m
  7768. (setq agenda-link
  7769. (if (org-called-interactively-p 'any)
  7770. (call-interactively 'org-store-link)
  7771. (org-store-link nil)))))))
  7772. ((eq major-mode 'calendar-mode)
  7773. (let ((cd (calendar-cursor-to-date)))
  7774. (setq link
  7775. (format-time-string
  7776. (car org-time-stamp-formats)
  7777. (apply 'encode-time
  7778. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7779. nil nil nil))))
  7780. (org-store-link-props :type "calendar" :date cd)))
  7781. ((eq major-mode 'w3-mode)
  7782. (setq cpltxt (if (and (buffer-name)
  7783. (not (string-match "Untitled" (buffer-name))))
  7784. (buffer-name)
  7785. (url-view-url t))
  7786. link (org-make-link (url-view-url t)))
  7787. (org-store-link-props :type "w3" :url (url-view-url t)))
  7788. ((eq major-mode 'w3m-mode)
  7789. (setq cpltxt (or w3m-current-title w3m-current-url)
  7790. link (org-make-link w3m-current-url))
  7791. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7792. ((setq search (run-hook-with-args-until-success
  7793. 'org-create-file-search-functions))
  7794. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7795. "::" search))
  7796. (setq cpltxt (or description link)))
  7797. ((eq major-mode 'image-mode)
  7798. (setq cpltxt (concat "file:"
  7799. (abbreviate-file-name buffer-file-name))
  7800. link (org-make-link cpltxt))
  7801. (org-store-link-props :type "image" :file buffer-file-name))
  7802. ((eq major-mode 'dired-mode)
  7803. ;; link to the file in the current line
  7804. (let ((file (dired-get-filename nil t)))
  7805. (setq file (if file
  7806. (abbreviate-file-name
  7807. (expand-file-name (dired-get-filename nil t)))
  7808. ;; otherwise, no file so use current directory.
  7809. default-directory))
  7810. (setq cpltxt (concat "file:" file)
  7811. link (org-make-link cpltxt))))
  7812. ((and (buffer-file-name (buffer-base-buffer)) (eq major-mode 'org-mode))
  7813. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7814. (cond
  7815. ((org-in-regexp "<<\\(.*?\\)>>")
  7816. (setq cpltxt
  7817. (concat "file:"
  7818. (abbreviate-file-name
  7819. (buffer-file-name (buffer-base-buffer)))
  7820. "::" (match-string 1))
  7821. link (org-make-link cpltxt)))
  7822. ((and (featurep 'org-id)
  7823. (or (eq org-link-to-org-use-id t)
  7824. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7825. (org-called-interactively-p 'any))
  7826. (and (eq org-link-to-org-use-id
  7827. 'create-if-interactive-and-no-custom-id)
  7828. (org-called-interactively-p 'any)
  7829. (not custom-id))
  7830. (and org-link-to-org-use-id
  7831. (org-entry-get nil "ID"))))
  7832. ;; We can make a link using the ID.
  7833. (setq link (condition-case nil
  7834. (prog1 (org-id-store-link)
  7835. (setq desc (plist-get org-store-link-plist
  7836. :description)))
  7837. (error
  7838. ;; probably before first headline, link to file only
  7839. (concat "file:"
  7840. (abbreviate-file-name
  7841. (buffer-file-name (buffer-base-buffer))))))))
  7842. (t
  7843. ;; Just link to current headline
  7844. (setq cpltxt (concat "file:"
  7845. (abbreviate-file-name
  7846. (buffer-file-name (buffer-base-buffer)))))
  7847. ;; Add a context search string
  7848. (when (org-xor org-context-in-file-links arg)
  7849. (setq txt (cond
  7850. ((org-at-heading-p) nil)
  7851. ((org-region-active-p)
  7852. (buffer-substring (region-beginning) (region-end)))
  7853. (t nil)))
  7854. (when (or (null txt) (string-match "\\S-" txt))
  7855. (setq cpltxt
  7856. (concat cpltxt "::"
  7857. (condition-case nil
  7858. (org-make-org-heading-search-string txt)
  7859. (error "")))
  7860. desc (or (nth 4 (ignore-errors
  7861. (org-heading-components))) "NONE"))))
  7862. (if (string-match "::\\'" cpltxt)
  7863. (setq cpltxt (substring cpltxt 0 -2)))
  7864. (setq link (org-make-link cpltxt)))))
  7865. ((buffer-file-name (buffer-base-buffer))
  7866. ;; Just link to this file here.
  7867. (setq cpltxt (concat "file:"
  7868. (abbreviate-file-name
  7869. (buffer-file-name (buffer-base-buffer)))))
  7870. ;; Add a context string
  7871. (when (org-xor org-context-in-file-links arg)
  7872. (setq txt (if (org-region-active-p)
  7873. (buffer-substring (region-beginning) (region-end))
  7874. (buffer-substring (point-at-bol) (point-at-eol))))
  7875. ;; Only use search option if there is some text.
  7876. (when (string-match "\\S-" txt)
  7877. (setq cpltxt
  7878. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7879. desc "NONE")))
  7880. (setq link (org-make-link cpltxt)))
  7881. ((org-called-interactively-p 'interactive)
  7882. (error "Cannot link to a buffer which is not visiting a file"))
  7883. (t (setq link nil)))
  7884. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7885. (setq link (or link cpltxt)
  7886. desc (or desc cpltxt))
  7887. (if (equal desc "NONE") (setq desc nil))
  7888. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  7889. (progn
  7890. (setq org-stored-links
  7891. (cons (list link desc) org-stored-links))
  7892. (message "Stored: %s" (or desc link))
  7893. (when custom-id
  7894. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7895. "::#" custom-id))
  7896. (setq org-stored-links
  7897. (cons (list link desc) org-stored-links))))
  7898. (or agenda-link (and link (org-make-link-string link desc)))))))
  7899. (defun org-store-link-props (&rest plist)
  7900. "Store link properties, extract names and addresses."
  7901. (let (x adr)
  7902. (when (setq x (plist-get plist :from))
  7903. (setq adr (mail-extract-address-components x))
  7904. (setq plist (plist-put plist :fromname (car adr)))
  7905. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7906. (when (setq x (plist-get plist :to))
  7907. (setq adr (mail-extract-address-components x))
  7908. (setq plist (plist-put plist :toname (car adr)))
  7909. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7910. (let ((from (plist-get plist :from))
  7911. (to (plist-get plist :to)))
  7912. (when (and from to org-from-is-user-regexp)
  7913. (setq plist
  7914. (plist-put plist :fromto
  7915. (if (string-match org-from-is-user-regexp from)
  7916. (concat "to %t")
  7917. (concat "from %f"))))))
  7918. (setq org-store-link-plist plist))
  7919. (defun org-add-link-props (&rest plist)
  7920. "Add these properties to the link property list."
  7921. (let (key value)
  7922. (while plist
  7923. (setq key (pop plist) value (pop plist))
  7924. (setq org-store-link-plist
  7925. (plist-put org-store-link-plist key value)))))
  7926. (defun org-email-link-description (&optional fmt)
  7927. "Return the description part of an email link.
  7928. This takes information from `org-store-link-plist' and formats it
  7929. according to FMT (default from `org-email-link-description-format')."
  7930. (setq fmt (or fmt org-email-link-description-format))
  7931. (let* ((p org-store-link-plist)
  7932. (to (plist-get p :toaddress))
  7933. (from (plist-get p :fromaddress))
  7934. (table
  7935. (list
  7936. (cons "%c" (plist-get p :fromto))
  7937. (cons "%F" (plist-get p :from))
  7938. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7939. (cons "%T" (plist-get p :to))
  7940. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7941. (cons "%s" (plist-get p :subject))
  7942. (cons "%d" (plist-get p :date))
  7943. (cons "%m" (plist-get p :message-id)))))
  7944. (when (string-match "%c" fmt)
  7945. ;; Check if the user wrote this message
  7946. (if (and org-from-is-user-regexp from to
  7947. (save-match-data (string-match org-from-is-user-regexp from)))
  7948. (setq fmt (replace-match "to %t" t t fmt))
  7949. (setq fmt (replace-match "from %f" t t fmt))))
  7950. (org-replace-escapes fmt table)))
  7951. (defun org-make-org-heading-search-string (&optional string heading)
  7952. "Make search string for STRING or current headline."
  7953. (interactive)
  7954. (let ((s (or string (org-get-heading)))
  7955. (lines org-context-in-file-links))
  7956. (unless (and string (not heading))
  7957. ;; We are using a headline, clean up garbage in there.
  7958. (if (string-match org-todo-regexp s)
  7959. (setq s (replace-match "" t t s)))
  7960. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  7961. (setq s (replace-match "" t t s)))
  7962. (setq s (org-trim s))
  7963. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7964. org-comment-string "\\)") s)
  7965. (setq s (replace-match "" t t s)))
  7966. (while (string-match org-ts-regexp s)
  7967. (setq s (replace-match "" t t s))))
  7968. (or string (setq s (concat "*" s))) ; Add * for headlines
  7969. (when (and string (integerp lines) (> lines 0))
  7970. (let ((slines (org-split-string s "\n")))
  7971. (when (< lines (length slines))
  7972. (setq s (mapconcat
  7973. 'identity
  7974. (reverse (nthcdr (- (length slines) lines)
  7975. (reverse slines))) "\n")))))
  7976. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7977. (defun org-make-link (&rest strings)
  7978. "Concatenate STRINGS."
  7979. (apply 'concat strings))
  7980. (defun org-make-link-string (link &optional description)
  7981. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7982. (unless (string-match "\\S-" link)
  7983. (error "Empty link"))
  7984. (when (and description
  7985. (stringp description)
  7986. (not (string-match "\\S-" description)))
  7987. (setq description nil))
  7988. (when (stringp description)
  7989. ;; Remove brackets from the description, they are fatal.
  7990. (while (string-match "\\[" description)
  7991. (setq description (replace-match "{" t t description)))
  7992. (while (string-match "\\]" description)
  7993. (setq description (replace-match "}" t t description))))
  7994. (when (equal link description)
  7995. ;; No description needed, it is identical
  7996. (setq description nil))
  7997. (when (and (not description)
  7998. (not (string-match (org-image-file-name-regexp) link))
  7999. (not (equal link (org-link-escape link))))
  8000. (setq description (org-extract-attributes link)))
  8001. (setq link
  8002. (cond ((string-match (org-image-file-name-regexp) link) link)
  8003. ((string-match org-link-types-re link)
  8004. (concat (match-string 1 link)
  8005. (org-link-escape (substring link (match-end 1)))))
  8006. (t (org-link-escape link))))
  8007. (concat "[[" link "]"
  8008. (if description (concat "[" description "]") "")
  8009. "]"))
  8010. (defconst org-link-escape-chars
  8011. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8012. "List of characters that should be escaped in link.
  8013. This is the list that is used for internal purposes.")
  8014. (defvar org-url-encoding-use-url-hexify nil)
  8015. (defconst org-link-escape-chars-browser
  8016. '(?\ )
  8017. "List of escapes for characters that are problematic in links.
  8018. This is the list that is used before handing over to the browser.")
  8019. (defun org-link-escape (text &optional table merge)
  8020. "Return percent escaped representation of TEXT.
  8021. TEXT is a string with the text to escape.
  8022. Optional argument TABLE is a list with characters that should be
  8023. escaped. When nil, `org-link-escape-chars' is used.
  8024. If optional argument MERGE is set, merge TABLE into
  8025. `org-link-escape-chars'."
  8026. (if (and org-url-encoding-use-url-hexify (not table))
  8027. (url-hexify-string text)
  8028. (cond
  8029. ((and table merge)
  8030. (mapc (lambda (defchr)
  8031. (unless (member defchr table)
  8032. (setq table (cons defchr table)))) org-link-escape-chars))
  8033. ((null table)
  8034. (setq table org-link-escape-chars)))
  8035. (mapconcat
  8036. (lambda (char)
  8037. (if (or (member char table)
  8038. (< char 32) (= char 37) (> char 126))
  8039. (mapconcat (lambda (sequence-element)
  8040. (format "%%%.2X" sequence-element))
  8041. (or (encode-coding-char char 'utf-8)
  8042. (error "Unable to percent escape character: %s"
  8043. (char-to-string char))) "")
  8044. (char-to-string char))) text "")))
  8045. (defun org-link-unescape (str)
  8046. "Unhex hexified Unicode strings as returned from the JavaScript function
  8047. encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
  8048. (unless (and (null str) (string= "" str))
  8049. (let ((pos 0) (case-fold-search t) unhexed)
  8050. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8051. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8052. (setq str (replace-match unhexed t t str))
  8053. (setq pos (+ pos (length unhexed))))))
  8054. str)
  8055. (defun org-link-unescape-compound (hex)
  8056. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
  8057. Note: this function also decodes single byte encodings like
  8058. `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
  8059. (save-match-data
  8060. (let* ((bytes (cdr (split-string hex "%")))
  8061. (ret "")
  8062. (eat 0)
  8063. (sum 0))
  8064. (while bytes
  8065. (let* ((val (string-to-number (pop bytes) 16))
  8066. (shift-xor
  8067. (if (= 0 eat)
  8068. (cond
  8069. ((>= val 252) (cons 6 252))
  8070. ((>= val 248) (cons 5 248))
  8071. ((>= val 240) (cons 4 240))
  8072. ((>= val 224) (cons 3 224))
  8073. ((>= val 192) (cons 2 192))
  8074. (t (cons 0 0)))
  8075. (cons 6 128))))
  8076. (if (>= val 192) (setq eat (car shift-xor)))
  8077. (setq val (logxor val (cdr shift-xor)))
  8078. (setq sum (+ (lsh sum (car shift-xor)) val))
  8079. (if (> eat 0) (setq eat (- eat 1)))
  8080. (cond
  8081. ((= 0 eat) ;multi byte
  8082. (setq ret (concat ret (org-char-to-string sum)))
  8083. (setq sum 0))
  8084. ((not bytes) ; single byte(s)
  8085. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8086. )) ;; end (while bytes
  8087. ret )))
  8088. (defun org-link-unescape-single-byte-sequence (hex)
  8089. "Unhexify hex-encoded single byte character sequences."
  8090. (mapconcat (lambda (byte)
  8091. (char-to-string (string-to-number byte 16)))
  8092. (cdr (split-string hex "%")) ""))
  8093. (defun org-xor (a b)
  8094. "Exclusive or."
  8095. (if a (not b) b))
  8096. (defun org-fixup-message-id-for-http (s)
  8097. "Replace special characters in a message id, so it can be used in an http query."
  8098. (when (string-match "%" s)
  8099. (setq s (mapconcat (lambda (c)
  8100. (if (eq c ?%)
  8101. "%25"
  8102. (char-to-string c)))
  8103. s "")))
  8104. (while (string-match "<" s)
  8105. (setq s (replace-match "%3C" t t s)))
  8106. (while (string-match ">" s)
  8107. (setq s (replace-match "%3E" t t s)))
  8108. (while (string-match "@" s)
  8109. (setq s (replace-match "%40" t t s)))
  8110. s)
  8111. ;;;###autoload
  8112. (defun org-insert-link-global ()
  8113. "Insert a link like Org-mode does.
  8114. This command can be called in any mode to insert a link in Org-mode syntax."
  8115. (interactive)
  8116. (org-load-modules-maybe)
  8117. (org-run-like-in-org-mode 'org-insert-link))
  8118. (defun org-insert-link (&optional complete-file link-location default-description)
  8119. "Insert a link. At the prompt, enter the link.
  8120. Completion can be used to insert any of the link protocol prefixes like
  8121. http or ftp in use.
  8122. The history can be used to select a link previously stored with
  8123. `org-store-link'. When the empty string is entered (i.e. if you just
  8124. press RET at the prompt), the link defaults to the most recently
  8125. stored link. As SPC triggers completion in the minibuffer, you need to
  8126. use M-SPC or C-q SPC to force the insertion of a space character.
  8127. You will also be prompted for a description, and if one is given, it will
  8128. be displayed in the buffer instead of the link.
  8129. If there is already a link at point, this command will allow you to edit link
  8130. and description parts.
  8131. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8132. be selected using completion. The path to the file will be relative to the
  8133. current directory if the file is in the current directory or a subdirectory.
  8134. Otherwise, the link will be the absolute path as completed in the minibuffer
  8135. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8136. option `org-link-file-path-type'.
  8137. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8138. the current directory or below.
  8139. With three \\[universal-argument] prefixes, negate the meaning of
  8140. `org-keep-stored-link-after-insertion'.
  8141. If `org-make-link-description-function' is non-nil, this function will be
  8142. called with the link target, and the result will be the default
  8143. link description.
  8144. If the LINK-LOCATION parameter is non-nil, this value will be
  8145. used as the link location instead of reading one interactively.
  8146. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8147. be used as the default description."
  8148. (interactive "P")
  8149. (let* ((wcf (current-window-configuration))
  8150. (region (if (org-region-active-p)
  8151. (buffer-substring (region-beginning) (region-end))))
  8152. (remove (and region (list (region-beginning) (region-end))))
  8153. (desc region)
  8154. tmphist ; byte-compile incorrectly complains about this
  8155. (link link-location)
  8156. entry file all-prefixes)
  8157. (cond
  8158. (link-location) ; specified by arg, just use it.
  8159. ((org-in-regexp org-bracket-link-regexp 1)
  8160. ;; We do have a link at point, and we are going to edit it.
  8161. (setq remove (list (match-beginning 0) (match-end 0)))
  8162. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8163. (setq link (read-string "Link: "
  8164. (org-link-unescape
  8165. (org-match-string-no-properties 1)))))
  8166. ((or (org-in-regexp org-angle-link-re)
  8167. (org-in-regexp org-plain-link-re))
  8168. ;; Convert to bracket link
  8169. (setq remove (list (match-beginning 0) (match-end 0))
  8170. link (read-string "Link: "
  8171. (org-remove-angle-brackets (match-string 0)))))
  8172. ((member complete-file '((4) (16)))
  8173. ;; Completing read for file names.
  8174. (setq link (org-file-complete-link complete-file)))
  8175. (t
  8176. ;; Read link, with completion for stored links.
  8177. (with-output-to-temp-buffer "*Org Links*"
  8178. (princ "Insert a link.
  8179. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8180. (when org-stored-links
  8181. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8182. (princ (mapconcat
  8183. (lambda (x)
  8184. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  8185. (reverse org-stored-links) "\n"))))
  8186. (let ((cw (selected-window)))
  8187. (select-window (get-buffer-window "*Org Links*" 'visible))
  8188. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8189. (unless (pos-visible-in-window-p (point-max))
  8190. (org-fit-window-to-buffer))
  8191. (and (window-live-p cw) (select-window cw)))
  8192. ;; Fake a link history, containing the stored links.
  8193. (setq tmphist (append (mapcar 'car org-stored-links)
  8194. org-insert-link-history))
  8195. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  8196. (mapcar 'car org-link-abbrev-alist)
  8197. org-link-types))
  8198. (unwind-protect
  8199. (progn
  8200. (setq link
  8201. (let ((org-completion-use-ido nil)
  8202. (org-completion-use-iswitchb nil))
  8203. (org-completing-read
  8204. "Link: "
  8205. (append
  8206. (mapcar (lambda (x) (list (concat x ":")))
  8207. all-prefixes)
  8208. (mapcar 'car org-stored-links))
  8209. nil nil nil
  8210. 'tmphist
  8211. (car (car org-stored-links)))))
  8212. (if (not (string-match "\\S-" link))
  8213. (error "No link selected"))
  8214. (if (or (member link all-prefixes)
  8215. (and (equal ":" (substring link -1))
  8216. (member (substring link 0 -1) all-prefixes)
  8217. (setq link (substring link 0 -1))))
  8218. (setq link (org-link-try-special-completion link))))
  8219. (set-window-configuration wcf)
  8220. (kill-buffer "*Org Links*"))
  8221. (setq entry (assoc link org-stored-links))
  8222. (or entry (push link org-insert-link-history))
  8223. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8224. (not org-keep-stored-link-after-insertion))
  8225. (setq org-stored-links (delq (assoc link org-stored-links)
  8226. org-stored-links)))
  8227. (setq desc (or desc (nth 1 entry)))))
  8228. (if (string-match org-plain-link-re link)
  8229. ;; URL-like link, normalize the use of angular brackets.
  8230. (setq link (org-make-link (org-remove-angle-brackets link))))
  8231. ;; Check if we are linking to the current file with a search option
  8232. ;; If yes, simplify the link by using only the search option.
  8233. (when (and buffer-file-name
  8234. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8235. (let* ((path (match-string 1 link))
  8236. (case-fold-search nil)
  8237. (search (match-string 2 link)))
  8238. (save-match-data
  8239. (if (equal (file-truename buffer-file-name) (file-truename path))
  8240. ;; We are linking to this same file, with a search option
  8241. (setq link search)))))
  8242. ;; Check if we can/should use a relative path. If yes, simplify the link
  8243. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8244. (let* ((type (match-string 1 link))
  8245. (path (match-string 2 link))
  8246. (origpath path)
  8247. (case-fold-search nil))
  8248. (cond
  8249. ((or (eq org-link-file-path-type 'absolute)
  8250. (equal complete-file '(16)))
  8251. (setq path (abbreviate-file-name (expand-file-name path))))
  8252. ((eq org-link-file-path-type 'noabbrev)
  8253. (setq path (expand-file-name path)))
  8254. ((eq org-link-file-path-type 'relative)
  8255. (setq path (file-relative-name path)))
  8256. (t
  8257. (save-match-data
  8258. (if (string-match (concat "^" (regexp-quote
  8259. (expand-file-name
  8260. (file-name-as-directory
  8261. default-directory))))
  8262. (expand-file-name path))
  8263. ;; We are linking a file with relative path name.
  8264. (setq path (substring (expand-file-name path)
  8265. (match-end 0)))
  8266. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8267. (setq link (concat type path))
  8268. (if (equal desc origpath)
  8269. (setq desc path))))
  8270. (if org-make-link-description-function
  8271. (setq desc (funcall org-make-link-description-function link desc))
  8272. (if default-description (setq desc default-description)))
  8273. (setq desc (read-string "Description: " desc))
  8274. (unless (string-match "\\S-" desc) (setq desc nil))
  8275. (if remove (apply 'delete-region remove))
  8276. (insert (org-make-link-string link desc))))
  8277. (defun org-link-try-special-completion (type)
  8278. "If there is completion support for link type TYPE, offer it."
  8279. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8280. (if (functionp fun)
  8281. (funcall fun)
  8282. (read-string "Link (no completion support): " (concat type ":")))))
  8283. (defun org-file-complete-link (&optional arg)
  8284. "Create a file link using completion."
  8285. (let (file link)
  8286. (setq file (read-file-name "File: "))
  8287. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8288. (pwd1 (file-name-as-directory (abbreviate-file-name
  8289. (expand-file-name ".")))))
  8290. (cond
  8291. ((equal arg '(16))
  8292. (setq link (org-make-link
  8293. "file:"
  8294. (abbreviate-file-name (expand-file-name file)))))
  8295. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8296. (setq link (org-make-link "file:" (match-string 1 file))))
  8297. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8298. (expand-file-name file))
  8299. (setq link (org-make-link
  8300. "file:" (match-string 1 (expand-file-name file)))))
  8301. (t (setq link (org-make-link "file:" file)))))
  8302. link))
  8303. (defun org-completing-read (&rest args)
  8304. "Completing-read with SPACE being a normal character."
  8305. (let ((enable-recursive-minibuffers t)
  8306. (minibuffer-local-completion-map
  8307. (copy-keymap minibuffer-local-completion-map)))
  8308. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8309. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8310. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8311. (apply 'org-icompleting-read args)))
  8312. (defun org-completing-read-no-i (&rest args)
  8313. (let (org-completion-use-ido org-completion-use-iswitchb)
  8314. (apply 'org-completing-read args)))
  8315. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8316. "Use iswitch as a completing-read replacement to choose from choices.
  8317. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8318. from."
  8319. (let* ((iswitchb-use-virtual-buffers nil)
  8320. (iswitchb-make-buflist-hook
  8321. (lambda ()
  8322. (setq iswitchb-temp-buflist choices))))
  8323. (iswitchb-read-buffer prompt)))
  8324. (defun org-icompleting-read (&rest args)
  8325. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8326. (org-without-partial-completion
  8327. (if (and org-completion-use-ido
  8328. (fboundp 'ido-completing-read)
  8329. (boundp 'ido-mode) ido-mode
  8330. (listp (second args)))
  8331. (let ((ido-enter-matching-directory nil))
  8332. (apply 'ido-completing-read (concat (car args))
  8333. (if (consp (car (nth 1 args)))
  8334. (mapcar 'car (nth 1 args))
  8335. (nth 1 args))
  8336. (cddr args)))
  8337. (if (and org-completion-use-iswitchb
  8338. (boundp 'iswitchb-mode) iswitchb-mode
  8339. (listp (second args)))
  8340. (apply 'org-iswitchb-completing-read (concat (car args))
  8341. (if (consp (car (nth 1 args)))
  8342. (mapcar 'car (nth 1 args))
  8343. (nth 1 args))
  8344. (cddr args))
  8345. (apply 'completing-read args)))))
  8346. (defun org-extract-attributes (s)
  8347. "Extract the attributes cookie from a string and set as text property."
  8348. (let (a attr (start 0) key value)
  8349. (save-match-data
  8350. (when (string-match "{{\\([^}]+\\)}}$" s)
  8351. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8352. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8353. (setq key (match-string 1 a) value (match-string 2 a)
  8354. start (match-end 0)
  8355. attr (plist-put attr (intern key) value))))
  8356. (org-add-props s nil 'org-attr attr))
  8357. s))
  8358. (defun org-extract-attributes-from-string (tag)
  8359. (let (key value attr)
  8360. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8361. (setq key (match-string 1 tag) value (match-string 2 tag)
  8362. tag (replace-match "" t t tag)
  8363. attr (plist-put attr (intern key) value)))
  8364. (cons tag attr)))
  8365. (defun org-attributes-to-string (plist)
  8366. "Format a property list into an HTML attribute list."
  8367. (let ((s "") key value)
  8368. (while plist
  8369. (setq key (pop plist) value (pop plist))
  8370. (and value
  8371. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8372. s))
  8373. ;;; Opening/following a link
  8374. (defvar org-link-search-failed nil)
  8375. (defvar org-open-link-functions nil
  8376. "Hook for functions finding a plain text link.
  8377. These functions must take a single argument, the link content.
  8378. They will be called for links that look like [[link text][description]]
  8379. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8380. like a filename (e.g. \"./blue.png\").
  8381. These functions will be called *before* Org attempts to resolve the
  8382. link by doing text searches in the current buffer - so if you want a
  8383. link \"[[target]]\" to still find \"<<target>>\", your function should
  8384. handle this as a special case.
  8385. When the function does handle the link, it must return a non-nil value.
  8386. If it decides that it is not responsible for this link, it must return
  8387. nil to indicate that that Org-mode can continue with other options
  8388. like exact and fuzzy text search.")
  8389. (defun org-next-link ()
  8390. "Move forward to the next link.
  8391. If the link is in hidden text, expose it."
  8392. (interactive)
  8393. (when (and org-link-search-failed (eq this-command last-command))
  8394. (goto-char (point-min))
  8395. (message "Link search wrapped back to beginning of buffer"))
  8396. (setq org-link-search-failed nil)
  8397. (let* ((pos (point))
  8398. (ct (org-context))
  8399. (a (assoc :link ct)))
  8400. (if a (goto-char (nth 2 a)))
  8401. (if (re-search-forward org-any-link-re nil t)
  8402. (progn
  8403. (goto-char (match-beginning 0))
  8404. (if (outline-invisible-p) (org-show-context)))
  8405. (goto-char pos)
  8406. (setq org-link-search-failed t)
  8407. (error "No further link found"))))
  8408. (defun org-previous-link ()
  8409. "Move backward to the previous link.
  8410. If the link is in hidden text, expose it."
  8411. (interactive)
  8412. (when (and org-link-search-failed (eq this-command last-command))
  8413. (goto-char (point-max))
  8414. (message "Link search wrapped back to end of buffer"))
  8415. (setq org-link-search-failed nil)
  8416. (let* ((pos (point))
  8417. (ct (org-context))
  8418. (a (assoc :link ct)))
  8419. (if a (goto-char (nth 1 a)))
  8420. (if (re-search-backward org-any-link-re nil t)
  8421. (progn
  8422. (goto-char (match-beginning 0))
  8423. (if (outline-invisible-p) (org-show-context)))
  8424. (goto-char pos)
  8425. (setq org-link-search-failed t)
  8426. (error "No further link found"))))
  8427. (defun org-translate-link (s)
  8428. "Translate a link string if a translation function has been defined."
  8429. (if (and org-link-translation-function
  8430. (fboundp org-link-translation-function)
  8431. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8432. (progn
  8433. (setq s (funcall org-link-translation-function
  8434. (match-string 1) (match-string 2)))
  8435. (concat (car s) ":" (cdr s)))
  8436. s))
  8437. (defun org-translate-link-from-planner (type path)
  8438. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8439. This is still an experimental function, your mileage may vary."
  8440. (cond
  8441. ((member type '("http" "https" "news" "ftp"))
  8442. ;; standard Internet links are the same.
  8443. nil)
  8444. ((and (equal type "irc") (string-match "^//" path))
  8445. ;; Planner has two / at the beginning of an irc link, we have 1.
  8446. ;; We should have zero, actually....
  8447. (setq path (substring path 1)))
  8448. ((and (equal type "lisp") (string-match "^/" path))
  8449. ;; Planner has a slash, we do not.
  8450. (setq type "elisp" path (substring path 1)))
  8451. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8452. ;; A typical message link. Planner has the id after the final slash,
  8453. ;; we separate it with a hash mark
  8454. (setq path (concat (match-string 1 path) "#"
  8455. (org-remove-angle-brackets (match-string 2 path)))))
  8456. )
  8457. (cons type path))
  8458. (defun org-find-file-at-mouse (ev)
  8459. "Open file link or URL at mouse."
  8460. (interactive "e")
  8461. (mouse-set-point ev)
  8462. (org-open-at-point 'in-emacs))
  8463. (defun org-open-at-mouse (ev)
  8464. "Open file link or URL at mouse.
  8465. See the docstring of `org-open-file' for details."
  8466. (interactive "e")
  8467. (mouse-set-point ev)
  8468. (if (eq major-mode 'org-agenda-mode)
  8469. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8470. (org-open-at-point))
  8471. (defvar org-window-config-before-follow-link nil
  8472. "The window configuration before following a link.
  8473. This is saved in case the need arises to restore it.")
  8474. (defvar org-open-link-marker (make-marker)
  8475. "Marker pointing to the location where `org-open-at-point; was called.")
  8476. ;;;###autoload
  8477. (defun org-open-at-point-global ()
  8478. "Follow a link like Org-mode does.
  8479. This command can be called in any mode to follow a link that has
  8480. Org-mode syntax."
  8481. (interactive)
  8482. (org-run-like-in-org-mode 'org-open-at-point))
  8483. ;;;###autoload
  8484. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8485. "Open a link in the string S, as if it was in Org-mode."
  8486. (interactive "sLink: \nP")
  8487. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8488. (with-temp-buffer
  8489. (let ((org-inhibit-startup (not reference-buffer)))
  8490. (org-mode)
  8491. (insert s)
  8492. (goto-char (point-min))
  8493. (when reference-buffer
  8494. (setq org-link-abbrev-alist-local
  8495. (with-current-buffer reference-buffer
  8496. org-link-abbrev-alist-local)))
  8497. (org-open-at-point arg reference-buffer)))))
  8498. (defvar org-open-at-point-functions nil
  8499. "Hook that is run when following a link at point.
  8500. Functions in this hook must return t if they identify and follow
  8501. a link at point. If they don't find anything interesting at point,
  8502. they must return nil.")
  8503. (defun org-open-at-point (&optional arg reference-buffer)
  8504. "Open link at or after point.
  8505. If there is no link at point, this function will search forward up to
  8506. the end of the current line.
  8507. Normally, files will be opened by an appropriate application. If the
  8508. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8509. With a double prefix argument, try to open outside of Emacs, in the
  8510. application the system uses for this file type."
  8511. (interactive "P")
  8512. ;; if in a code block, then open the block's results
  8513. (unless (call-interactively #'org-babel-open-src-block-result)
  8514. (org-load-modules-maybe)
  8515. (move-marker org-open-link-marker (point))
  8516. (setq org-window-config-before-follow-link (current-window-configuration))
  8517. (org-remove-occur-highlights nil nil t)
  8518. (cond
  8519. ((and (org-at-heading-p)
  8520. (not (org-in-regexp
  8521. (concat org-plain-link-re "\\|"
  8522. org-bracket-link-regexp "\\|"
  8523. org-angle-link-re "\\|"
  8524. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8525. (not (get-text-property (point) 'org-linked-text)))
  8526. (or (org-offer-links-in-entry arg)
  8527. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8528. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8529. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8530. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8531. (not (org-in-regexp org-bracket-link-regexp)))
  8532. (org-footnote-action))
  8533. (t
  8534. (let (type path link line search (pos (point)))
  8535. (catch 'match
  8536. (save-excursion
  8537. (skip-chars-forward "^]\n\r")
  8538. (when (org-in-regexp org-bracket-link-regexp 1)
  8539. (setq link (org-extract-attributes
  8540. (org-link-unescape (org-match-string-no-properties 1))))
  8541. (while (string-match " *\n *" link)
  8542. (setq link (replace-match " " t t link)))
  8543. (setq link (org-link-expand-abbrev link))
  8544. (cond
  8545. ((or (file-name-absolute-p link)
  8546. (string-match "^\\.\\.?/" link))
  8547. (setq type "file" path link))
  8548. ((string-match org-link-re-with-space3 link)
  8549. (setq type (match-string 1 link) path (match-string 2 link)))
  8550. (t (setq type "thisfile" path link)))
  8551. (throw 'match t)))
  8552. (when (get-text-property (point) 'org-linked-text)
  8553. (setq type "thisfile"
  8554. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8555. (1+ (point)) (point))
  8556. path (buffer-substring
  8557. (or (previous-single-property-change pos 'org-linked-text)
  8558. (point-min))
  8559. (or (next-single-property-change pos 'org-linked-text)
  8560. (point-max))))
  8561. (throw 'match t))
  8562. (save-excursion
  8563. (when (or (org-in-regexp org-angle-link-re)
  8564. (org-in-regexp org-plain-link-re))
  8565. (setq type (match-string 1)
  8566. path (org-link-unescape (match-string 2)))
  8567. (throw 'match t)))
  8568. (save-excursion
  8569. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8570. (setq type "tags"
  8571. path (match-string 1))
  8572. (while (string-match ":" path)
  8573. (setq path (replace-match "+" t t path)))
  8574. (throw 'match t)))
  8575. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8576. (setq type "tree-match"
  8577. path (match-string 1))
  8578. (throw 'match t)))
  8579. (unless path
  8580. (error "No link found"))
  8581. ;; switch back to reference buffer
  8582. ;; needed when if called in a temporary buffer through
  8583. ;; org-open-link-from-string
  8584. (with-current-buffer (or reference-buffer (current-buffer))
  8585. ;; Remove any trailing spaces in path
  8586. (if (string-match " +\\'" path)
  8587. (setq path (replace-match "" t t path)))
  8588. (if (and org-link-translation-function
  8589. (fboundp org-link-translation-function))
  8590. ;; Check if we need to translate the link
  8591. (let ((tmp (funcall org-link-translation-function type path)))
  8592. (setq type (car tmp) path (cdr tmp))))
  8593. (cond
  8594. ((assoc type org-link-protocols)
  8595. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8596. ((equal type "mailto")
  8597. (let ((cmd (car org-link-mailto-program))
  8598. (args (cdr org-link-mailto-program)) args1
  8599. (address path) (subject "") a)
  8600. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8601. (setq address (match-string 1 path)
  8602. subject (org-link-escape (match-string 2 path))))
  8603. (while args
  8604. (cond
  8605. ((not (stringp (car args))) (push (pop args) args1))
  8606. (t (setq a (pop args))
  8607. (if (string-match "%a" a)
  8608. (setq a (replace-match address t t a)))
  8609. (if (string-match "%s" a)
  8610. (setq a (replace-match subject t t a)))
  8611. (push a args1))))
  8612. (apply cmd (nreverse args1))))
  8613. ((member type '("http" "https" "ftp" "news"))
  8614. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8615. (org-link-escape
  8616. path org-link-escape-chars-browser)
  8617. path))))
  8618. ((string= type "doi")
  8619. (browse-url (concat "http://dx.doi.org/" (if (org-string-match-p "[[:nonascii:] ]" path)
  8620. (org-link-escape
  8621. path org-link-escape-chars-browser)
  8622. path))))
  8623. ((member type '("message"))
  8624. (browse-url (concat type ":" path)))
  8625. ((string= type "tags")
  8626. (org-tags-view arg path))
  8627. ((string= type "tree-match")
  8628. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8629. ((string= type "file")
  8630. (if (string-match "::\\([0-9]+\\)\\'" path)
  8631. (setq line (string-to-number (match-string 1 path))
  8632. path (substring path 0 (match-beginning 0)))
  8633. (if (string-match "::\\(.+\\)\\'" path)
  8634. (setq search (match-string 1 path)
  8635. path (substring path 0 (match-beginning 0)))))
  8636. (if (string-match "[*?{]" (file-name-nondirectory path))
  8637. (dired path)
  8638. (org-open-file path arg line search)))
  8639. ((string= type "shell")
  8640. (let ((cmd path))
  8641. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8642. (string-match org-confirm-shell-link-not-regexp cmd))
  8643. (not org-confirm-shell-link-function)
  8644. (funcall org-confirm-shell-link-function
  8645. (format "Execute \"%s\" in shell? "
  8646. (org-add-props cmd nil
  8647. 'face 'org-warning))))
  8648. (progn
  8649. (message "Executing %s" cmd)
  8650. (shell-command cmd))
  8651. (error "Abort"))))
  8652. ((string= type "elisp")
  8653. (let ((cmd path))
  8654. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8655. (string-match org-confirm-elisp-link-not-regexp cmd))
  8656. (not org-confirm-elisp-link-function)
  8657. (funcall org-confirm-elisp-link-function
  8658. (format "Execute \"%s\" as elisp? "
  8659. (org-add-props cmd nil
  8660. 'face 'org-warning))))
  8661. (message "%s => %s" cmd
  8662. (if (equal (string-to-char cmd) ?\()
  8663. (eval (read cmd))
  8664. (call-interactively (read cmd))))
  8665. (error "Abort"))))
  8666. ((and (string= type "thisfile")
  8667. (run-hook-with-args-until-success
  8668. 'org-open-link-functions path)))
  8669. ((string= type "thisfile")
  8670. (if arg
  8671. (switch-to-buffer-other-window
  8672. (org-get-buffer-for-internal-link (current-buffer)))
  8673. (org-mark-ring-push))
  8674. (let ((cmd `(org-link-search
  8675. ,path
  8676. ,(cond ((equal arg '(4)) ''occur)
  8677. ((equal arg '(16)) ''org-occur)
  8678. (t nil))
  8679. ,pos)))
  8680. (condition-case nil (let ((org-link-search-inhibit-query t))
  8681. (eval cmd))
  8682. (error (progn (widen) (eval cmd))))))
  8683. (t
  8684. (browse-url-at-point)))))))
  8685. (move-marker org-open-link-marker nil)
  8686. (run-hook-with-args 'org-follow-link-hook)))
  8687. (defun org-offer-links-in-entry (&optional nth zero)
  8688. "Offer links in the current entry and follow the selected link.
  8689. If there is only one link, follow it immediately as well.
  8690. If NTH is an integer, immediately pick the NTH link found.
  8691. If ZERO is a string, check also this string for a link, and if
  8692. there is one, offer it as link number zero."
  8693. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8694. "\\(" org-angle-link-re "\\)\\|"
  8695. "\\(" org-plain-link-re "\\)"))
  8696. (cnt ?0)
  8697. (in-emacs (if (integerp nth) nil nth))
  8698. have-zero end links link c)
  8699. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8700. (push (match-string 0 zero) links)
  8701. (setq cnt (1- cnt) have-zero t))
  8702. (save-excursion
  8703. (org-back-to-heading t)
  8704. (setq end (save-excursion (outline-next-heading) (point)))
  8705. (while (re-search-forward re end t)
  8706. (push (match-string 0) links))
  8707. (setq links (org-uniquify (reverse links))))
  8708. (cond
  8709. ((null links)
  8710. (message "No links"))
  8711. ((equal (length links) 1)
  8712. (setq link (list (car links))))
  8713. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8714. (setq link (list (nth (if have-zero nth (1- nth)) links))))
  8715. (t ; we have to select a link
  8716. (save-excursion
  8717. (save-window-excursion
  8718. (delete-other-windows)
  8719. (with-output-to-temp-buffer "*Select Link*"
  8720. (mapc (lambda (l)
  8721. (if (not (string-match org-bracket-link-regexp l))
  8722. (princ (format "[%c] %s\n" (incf cnt)
  8723. (org-remove-angle-brackets l)))
  8724. (if (match-end 3)
  8725. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8726. (match-string 3 l) (match-string 1 l)))
  8727. (princ (format "[%c] %s\n" (incf cnt)
  8728. (match-string 1 l))))))
  8729. links))
  8730. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8731. (message "Select link to open, RET to open all:")
  8732. (setq c (read-char-exclusive))
  8733. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8734. (when (equal c ?q) (error "Abort"))
  8735. (if (equal c ?\C-m)
  8736. (setq link links)
  8737. (setq nth (- c ?0))
  8738. (if have-zero (setq nth (1+ nth)))
  8739. (unless (and (integerp nth) (>= (length links) nth))
  8740. (error "Invalid link selection"))
  8741. (setq link (list (nth (1- nth) links))))))
  8742. (if link
  8743. (let ((buf (current-buffer)))
  8744. (dolist (l link)
  8745. (org-open-link-from-string l in-emacs buf))
  8746. t)
  8747. nil)))
  8748. ;; Add special file links that specify the way of opening
  8749. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8750. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8751. (defun org-open-file-with-system (path)
  8752. "Open file at PATH using the system way of opening it."
  8753. (org-open-file path 'system))
  8754. (defun org-open-file-with-emacs (path)
  8755. "Open file at PATH in Emacs."
  8756. (org-open-file path 'emacs))
  8757. (defun org-remove-file-link-modifiers ()
  8758. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8759. (goto-char (point-min))
  8760. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8761. (org-if-unprotected
  8762. (replace-match "file:" t t))))
  8763. (eval-after-load "org-exp"
  8764. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8765. 'org-remove-file-link-modifiers))
  8766. ;;;; Time estimates
  8767. (defun org-get-effort (&optional pom)
  8768. "Get the effort estimate for the current entry."
  8769. (org-entry-get pom org-effort-property))
  8770. ;;; File search
  8771. (defvar org-create-file-search-functions nil
  8772. "List of functions to construct the right search string for a file link.
  8773. These functions are called in turn with point at the location to
  8774. which the link should point.
  8775. A function in the hook should first test if it would like to
  8776. handle this file type, for example by checking the `major-mode'
  8777. or the file extension. If it decides not to handle this file, it
  8778. should just return nil to give other functions a chance. If it
  8779. does handle the file, it must return the search string to be used
  8780. when following the link. The search string will be part of the
  8781. file link, given after a double colon, and `org-open-at-point'
  8782. will automatically search for it. If special measures must be
  8783. taken to make the search successful, another function should be
  8784. added to the companion hook `org-execute-file-search-functions',
  8785. which see.
  8786. A function in this hook may also use `setq' to set the variable
  8787. `description' to provide a suggestion for the descriptive text to
  8788. be used for this link when it gets inserted into an Org-mode
  8789. buffer with \\[org-insert-link].")
  8790. (defvar org-execute-file-search-functions nil
  8791. "List of functions to execute a file search triggered by a link.
  8792. Functions added to this hook must accept a single argument, the
  8793. search string that was part of the file link, the part after the
  8794. double colon. The function must first check if it would like to
  8795. handle this search, for example by checking the `major-mode' or
  8796. the file extension. If it decides not to handle this search, it
  8797. should just return nil to give other functions a chance. If it
  8798. does handle the search, it must return a non-nil value to keep
  8799. other functions from trying.
  8800. Each function can access the current prefix argument through the
  8801. variable `current-prefix-argument'. Note that a single prefix is
  8802. used to force opening a link in Emacs, so it may be good to only
  8803. use a numeric or double prefix to guide the search function.
  8804. In case this is needed, a function in this hook can also restore
  8805. the window configuration before `org-open-at-point' was called using:
  8806. (set-window-configuration org-window-config-before-follow-link)")
  8807. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8808. (defun org-link-search (s &optional type avoid-pos stealth)
  8809. "Search for a link search option.
  8810. If S is surrounded by forward slashes, it is interpreted as a
  8811. regular expression. In org-mode files, this will create an `org-occur'
  8812. sparse tree. In ordinary files, `occur' will be used to list matches.
  8813. If the current buffer is in `dired-mode', grep will be used to search
  8814. in all files. If AVOID-POS is given, ignore matches near that position.
  8815. When optional argument STEALTH is non-nil, do not modify
  8816. visibility around point, thus ignoring
  8817. `org-show-hierarchy-above', `org-show-following-heading' and
  8818. `org-show-siblings' variables."
  8819. (let ((case-fold-search t)
  8820. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8821. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8822. (append '(("") (" ") ("\t") ("\n"))
  8823. org-emphasis-alist)
  8824. "\\|") "\\)"))
  8825. (pos (point))
  8826. (pre nil) (post nil)
  8827. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8828. (cond
  8829. ;; First check if there are any special search functions
  8830. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8831. ;; Now try the builtin stuff
  8832. ((and (equal (string-to-char s0) ?#)
  8833. (> (length s0) 1)
  8834. (save-excursion
  8835. (goto-char (point-min))
  8836. (and
  8837. (re-search-forward
  8838. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8839. (setq type 'dedicated
  8840. pos (match-beginning 0))))
  8841. ;; There is an exact target for this
  8842. (goto-char pos)
  8843. (org-back-to-heading t)))
  8844. ((save-excursion
  8845. (goto-char (point-min))
  8846. (and
  8847. (re-search-forward
  8848. (concat "<<" (regexp-quote s0) ">>") nil t)
  8849. (setq type 'dedicated
  8850. pos (match-beginning 0))))
  8851. ;; There is an exact target for this
  8852. (goto-char pos))
  8853. ((and (string-match "^(\\(.*\\))$" s0)
  8854. (save-excursion
  8855. (goto-char (point-min))
  8856. (and
  8857. (re-search-forward
  8858. (concat "[^[]" (regexp-quote
  8859. (format org-coderef-label-format
  8860. (match-string 1 s0))))
  8861. nil t)
  8862. (setq type 'dedicated
  8863. pos (1+ (match-beginning 0))))))
  8864. ;; There is a coderef target for this
  8865. (goto-char pos))
  8866. ((string-match "^/\\(.*\\)/$" s)
  8867. ;; A regular expression
  8868. (cond
  8869. ((eq major-mode 'org-mode)
  8870. (org-occur (match-string 1 s)))
  8871. ;;((eq major-mode 'dired-mode)
  8872. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8873. (t (org-do-occur (match-string 1 s)))))
  8874. ((and (eq major-mode 'org-mode) org-link-search-must-match-exact-headline)
  8875. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8876. (goto-char (point-min))
  8877. (cond
  8878. ((let (case-fold-search)
  8879. (re-search-forward (format org-complex-heading-regexp-format
  8880. (regexp-quote s))
  8881. nil t))
  8882. ;; OK, found a match
  8883. (setq type 'dedicated)
  8884. (goto-char (match-beginning 0)))
  8885. ((and (not org-link-search-inhibit-query)
  8886. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8887. (y-or-n-p "No match - create this as a new heading? "))
  8888. (goto-char (point-max))
  8889. (or (bolp) (newline))
  8890. (insert "* " s "\n")
  8891. (beginning-of-line 0))
  8892. (t
  8893. (goto-char pos)
  8894. (error "No match"))))
  8895. (t
  8896. ;; A normal search string
  8897. (when (equal (string-to-char s) ?*)
  8898. ;; Anchor on headlines, post may include tags.
  8899. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8900. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  8901. s (substring s 1)))
  8902. (remove-text-properties
  8903. 0 (length s)
  8904. '(face nil mouse-face nil keymap nil fontified nil) s)
  8905. ;; Make a series of regular expressions to find a match
  8906. (setq words (org-split-string s "[ \n\r\t]+")
  8907. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8908. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8909. "\\)" markers)
  8910. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8911. re2a (concat "[ \t\r\n]" re2a_)
  8912. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8913. re4 (concat "[^a-zA-Z_]" re4_)
  8914. re1 (concat pre re2 post)
  8915. re3 (concat pre (if pre re4_ re4) post)
  8916. re5 (concat pre ".*" re4)
  8917. re2 (concat pre re2)
  8918. re2a (concat pre (if pre re2a_ re2a))
  8919. re4 (concat pre (if pre re4_ re4))
  8920. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8921. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8922. re5 "\\)"
  8923. ))
  8924. (cond
  8925. ((eq type 'org-occur) (org-occur reall))
  8926. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8927. (t (goto-char (point-min))
  8928. (setq type 'fuzzy)
  8929. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8930. (org-search-not-self 1 re1 nil t)
  8931. (org-search-not-self 1 re2 nil t)
  8932. (org-search-not-self 1 re2a nil t)
  8933. (org-search-not-self 1 re3 nil t)
  8934. (org-search-not-self 1 re4 nil t)
  8935. (org-search-not-self 1 re5 nil t)
  8936. )
  8937. (goto-char (match-beginning 1))
  8938. (goto-char pos)
  8939. (error "No match"))))))
  8940. (and (eq major-mode 'org-mode)
  8941. (not stealth)
  8942. (org-show-context 'link-search))
  8943. type))
  8944. (defun org-search-not-self (group &rest args)
  8945. "Execute `re-search-forward', but only accept matches that do not
  8946. enclose the position of `org-open-link-marker'."
  8947. (let ((m org-open-link-marker))
  8948. (catch 'exit
  8949. (while (apply 're-search-forward args)
  8950. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8951. (goto-char (match-end group))
  8952. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8953. (> (match-beginning 0) (marker-position m))
  8954. (< (match-end 0) (marker-position m)))
  8955. (save-match-data
  8956. (or (not (org-in-regexp
  8957. org-bracket-link-analytic-regexp 1))
  8958. (not (match-end 4)) ; no description
  8959. (and (<= (match-beginning 4) (point))
  8960. (>= (match-end 4) (point))))))
  8961. (throw 'exit (point))))))))
  8962. (defun org-get-buffer-for-internal-link (buffer)
  8963. "Return a buffer to be used for displaying the link target of internal links."
  8964. (cond
  8965. ((not org-display-internal-link-with-indirect-buffer)
  8966. buffer)
  8967. ((string-match "(Clone)$" (buffer-name buffer))
  8968. (message "Buffer is already a clone, not making another one")
  8969. ;; we also do not modify visibility in this case
  8970. buffer)
  8971. (t ; make a new indirect buffer for displaying the link
  8972. (let* ((bn (buffer-name buffer))
  8973. (ibn (concat bn "(Clone)"))
  8974. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8975. (with-current-buffer ib (org-overview))
  8976. ib))))
  8977. (defun org-do-occur (regexp &optional cleanup)
  8978. "Call the Emacs command `occur'.
  8979. If CLEANUP is non-nil, remove the printout of the regular expression
  8980. in the *Occur* buffer. This is useful if the regex is long and not useful
  8981. to read."
  8982. (occur regexp)
  8983. (when cleanup
  8984. (let ((cwin (selected-window)) win beg end)
  8985. (when (setq win (get-buffer-window "*Occur*"))
  8986. (select-window win))
  8987. (goto-char (point-min))
  8988. (when (re-search-forward "match[a-z]+" nil t)
  8989. (setq beg (match-end 0))
  8990. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8991. (setq end (1- (match-beginning 0)))))
  8992. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8993. (goto-char (point-min))
  8994. (select-window cwin))))
  8995. ;;; The mark ring for links jumps
  8996. (defvar org-mark-ring nil
  8997. "Mark ring for positions before jumps in Org-mode.")
  8998. (defvar org-mark-ring-last-goto nil
  8999. "Last position in the mark ring used to go back.")
  9000. ;; Fill and close the ring
  9001. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9002. (loop for i from 1 to org-mark-ring-length do
  9003. (push (make-marker) org-mark-ring))
  9004. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9005. org-mark-ring)
  9006. (defun org-mark-ring-push (&optional pos buffer)
  9007. "Put the current position or POS into the mark ring and rotate it."
  9008. (interactive)
  9009. (setq pos (or pos (point)))
  9010. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9011. (move-marker (car org-mark-ring)
  9012. (or pos (point))
  9013. (or buffer (current-buffer)))
  9014. (message "%s"
  9015. (substitute-command-keys
  9016. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9017. (defun org-mark-ring-goto (&optional n)
  9018. "Jump to the previous position in the mark ring.
  9019. With prefix arg N, jump back that many stored positions. When
  9020. called several times in succession, walk through the entire ring.
  9021. Org-mode commands jumping to a different position in the current file,
  9022. or to another Org-mode file, automatically push the old position
  9023. onto the ring."
  9024. (interactive "p")
  9025. (let (p m)
  9026. (if (eq last-command this-command)
  9027. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9028. (setq p org-mark-ring))
  9029. (setq org-mark-ring-last-goto p)
  9030. (setq m (car p))
  9031. (org-pop-to-buffer-same-window (marker-buffer m))
  9032. (goto-char m)
  9033. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9034. (defun org-remove-angle-brackets (s)
  9035. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9036. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9037. s)
  9038. (defun org-add-angle-brackets (s)
  9039. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9040. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9041. s)
  9042. (defun org-remove-double-quotes (s)
  9043. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9044. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9045. s)
  9046. ;;; Following specific links
  9047. (defun org-follow-timestamp-link ()
  9048. (cond
  9049. ((org-at-date-range-p t)
  9050. (let ((org-agenda-start-on-weekday)
  9051. (t1 (match-string 1))
  9052. (t2 (match-string 2)))
  9053. (setq t1 (time-to-days (org-time-string-to-time t1))
  9054. t2 (time-to-days (org-time-string-to-time t2)))
  9055. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  9056. ((org-at-timestamp-p t)
  9057. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9058. (substring (match-string 1) 0 10)))
  9059. 1))
  9060. (t (error "This should not happen"))))
  9061. ;;; Following file links
  9062. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9063. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9064. (declare-function mailcap-mime-info
  9065. "mailcap" (string &optional request no-decode))
  9066. (defvar org-wait nil)
  9067. (defun org-open-file (path &optional in-emacs line search)
  9068. "Open the file at PATH.
  9069. First, this expands any special file name abbreviations. Then the
  9070. configuration variable `org-file-apps' is checked if it contains an
  9071. entry for this file type, and if yes, the corresponding command is launched.
  9072. If no application is found, Emacs simply visits the file.
  9073. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9074. With a double \\[universal-argument] \\[universal-argument] \
  9075. prefix arg, Org tries to avoid opening in Emacs
  9076. and to use an external application to visit the file.
  9077. Optional LINE specifies a line to go to, optional SEARCH a string
  9078. to search for. If LINE or SEARCH is given, the file will be
  9079. opened in Emacs, unless an entry from org-file-apps that makes
  9080. use of groups in a regexp matches.
  9081. If you want to change the way frames are used when following a
  9082. link, please customize `org-link-frame-setup'.
  9083. If the file does not exist, an error is thrown."
  9084. (let* ((file (if (equal path "")
  9085. buffer-file-name
  9086. (substitute-in-file-name (expand-file-name path))))
  9087. (file-apps (append org-file-apps (org-default-apps)))
  9088. (apps (org-remove-if
  9089. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9090. (apps-dlink (org-remove-if-not
  9091. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9092. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9093. (dirp (if remp nil (file-directory-p file)))
  9094. (file (if (and dirp org-open-directory-means-index-dot-org)
  9095. (concat (file-name-as-directory file) "index.org")
  9096. file))
  9097. (a-m-a-p (assq 'auto-mode apps))
  9098. (dfile (downcase file))
  9099. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9100. (link (cond ((and (eq line nil)
  9101. (eq search nil))
  9102. file)
  9103. (line
  9104. (concat file "::" (number-to-string line)))
  9105. (search
  9106. (concat file "::" search))))
  9107. (dlink (downcase link))
  9108. (old-buffer (current-buffer))
  9109. (old-pos (point))
  9110. (old-mode major-mode)
  9111. ext cmd link-match-data)
  9112. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9113. (setq ext (match-string 1 dfile))
  9114. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9115. (setq ext (match-string 1 dfile))))
  9116. (cond
  9117. ((member in-emacs '((16) system))
  9118. (setq cmd (cdr (assoc 'system apps))))
  9119. (in-emacs (setq cmd 'emacs))
  9120. (t
  9121. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9122. (and dirp (cdr (assoc 'directory apps)))
  9123. ; first, try matching against apps-dlink
  9124. ; if we get a match here, store the match data for later
  9125. (let ((match (assoc-default dlink apps-dlink
  9126. 'string-match)))
  9127. (if match
  9128. (progn (setq link-match-data (match-data))
  9129. match)
  9130. (progn (setq in-emacs (or in-emacs line search))
  9131. nil))) ; if we have no match in apps-dlink,
  9132. ; always open the file in emacs if line or search
  9133. ; is given (for backwards compatibility)
  9134. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9135. 'string-match)
  9136. (cdr (assoc ext apps))
  9137. (cdr (assoc t apps))))))
  9138. (when (eq cmd 'system)
  9139. (setq cmd (cdr (assoc 'system apps))))
  9140. (when (eq cmd 'default)
  9141. (setq cmd (cdr (assoc t apps))))
  9142. (when (eq cmd 'mailcap)
  9143. (require 'mailcap)
  9144. (mailcap-parse-mailcaps)
  9145. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9146. (command (mailcap-mime-info mime-type)))
  9147. (if (stringp command)
  9148. (setq cmd command)
  9149. (setq cmd 'emacs))))
  9150. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9151. (not (file-exists-p file))
  9152. (not org-open-non-existing-files))
  9153. (error "No such file: %s" file))
  9154. (cond
  9155. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9156. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9157. (while (string-match "['\"]%s['\"]" cmd)
  9158. (setq cmd (replace-match "%s" t t cmd)))
  9159. (while (string-match "%s" cmd)
  9160. (setq cmd (replace-match
  9161. (save-match-data
  9162. (shell-quote-argument
  9163. (convert-standard-filename file)))
  9164. t t cmd)))
  9165. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9166. (save-match-data
  9167. (let ((match-index 1)
  9168. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9169. (set-match-data link-match-data)
  9170. (while (<= match-index number-of-groups)
  9171. (let ((regex (concat "%" (number-to-string match-index)))
  9172. (replace-with (match-string match-index dlink)))
  9173. (while (string-match regex cmd)
  9174. (setq cmd (replace-match replace-with t t cmd))))
  9175. (setq match-index (+ match-index 1)))))
  9176. (save-window-excursion
  9177. (start-process-shell-command cmd nil cmd)
  9178. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  9179. ))
  9180. ((or (stringp cmd)
  9181. (eq cmd 'emacs))
  9182. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9183. (widen)
  9184. (if line (org-goto-line line)
  9185. (if search (org-link-search search))))
  9186. ((consp cmd)
  9187. (let ((file (convert-standard-filename file)))
  9188. (save-match-data
  9189. (set-match-data link-match-data)
  9190. (eval cmd))))
  9191. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9192. (and (eq major-mode 'org-mode) (eq old-mode 'org-mode)
  9193. (or (not (equal old-buffer (current-buffer)))
  9194. (not (equal old-pos (point))))
  9195. (org-mark-ring-push old-pos old-buffer))))
  9196. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9197. "This function returns non-nil if `entry' uses a regular
  9198. expression which should be matched against the whole link by
  9199. org-open-file.
  9200. It assumes that is the case when the entry uses a regular
  9201. expression which has at least one grouping construct and the
  9202. action is either a lisp form or a command string containing
  9203. '%1', i.e. using at least one subexpression match as a
  9204. parameter."
  9205. (let ((selector (car entry))
  9206. (action (cdr entry)))
  9207. (if (stringp selector)
  9208. (and (> (regexp-opt-depth selector) 0)
  9209. (or (and (stringp action)
  9210. (string-match "%[0-9]" action))
  9211. (consp action)))
  9212. nil)))
  9213. (defun org-default-apps ()
  9214. "Return the default applications for this operating system."
  9215. (cond
  9216. ((eq system-type 'darwin)
  9217. org-file-apps-defaults-macosx)
  9218. ((eq system-type 'windows-nt)
  9219. org-file-apps-defaults-windowsnt)
  9220. (t org-file-apps-defaults-gnu)))
  9221. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9222. "Convert extensions to regular expressions in the cars of LIST.
  9223. Also, weed out any non-string entries, because the return value is used
  9224. only for regexp matching.
  9225. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9226. point to the symbol `emacs', indicating that the file should
  9227. be opened in Emacs."
  9228. (append
  9229. (delq nil
  9230. (mapcar (lambda (x)
  9231. (if (not (stringp (car x)))
  9232. nil
  9233. (if (string-match "\\W" (car x))
  9234. x
  9235. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9236. list))
  9237. (if add-auto-mode
  9238. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9239. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9240. (defun org-file-remote-p (file)
  9241. "Test whether FILE specifies a location on a remote system.
  9242. Return non-nil if the location is indeed remote.
  9243. For example, the filename \"/user@host:/foo\" specifies a location
  9244. on the system \"/user@host:\"."
  9245. (cond ((fboundp 'file-remote-p)
  9246. (file-remote-p file))
  9247. ((fboundp 'tramp-handle-file-remote-p)
  9248. (tramp-handle-file-remote-p file))
  9249. ((and (boundp 'ange-ftp-name-format)
  9250. (string-match (car ange-ftp-name-format) file))
  9251. t)
  9252. (t nil)))
  9253. ;;;; Refiling
  9254. (defun org-get-org-file ()
  9255. "Read a filename, with default directory `org-directory'."
  9256. (let ((default (or org-default-notes-file remember-data-file)))
  9257. (read-file-name (format "File name [%s]: " default)
  9258. (file-name-as-directory org-directory)
  9259. default)))
  9260. (defun org-notes-order-reversed-p ()
  9261. "Check if the current file should receive notes in reversed order."
  9262. (cond
  9263. ((not org-reverse-note-order) nil)
  9264. ((eq t org-reverse-note-order) t)
  9265. ((not (listp org-reverse-note-order)) nil)
  9266. (t (catch 'exit
  9267. (let ((all org-reverse-note-order)
  9268. entry)
  9269. (while (setq entry (pop all))
  9270. (if (string-match (car entry) buffer-file-name)
  9271. (throw 'exit (cdr entry))))
  9272. nil)))))
  9273. (defvar org-refile-target-table nil
  9274. "The list of refile targets, created by `org-refile'.")
  9275. (defvar org-agenda-new-buffers nil
  9276. "Buffers created to visit agenda files.")
  9277. (defvar org-refile-cache nil
  9278. "Cache for refile targets.")
  9279. (defvar org-refile-markers nil
  9280. "All the markers used for caching refile locations.")
  9281. (defun org-refile-marker (pos)
  9282. "Get a new refile marker, but only if caching is in use."
  9283. (if (not org-refile-use-cache)
  9284. pos
  9285. (let ((m (make-marker)))
  9286. (move-marker m pos)
  9287. (push m org-refile-markers)
  9288. m)))
  9289. (defun org-refile-cache-clear ()
  9290. "Clear the refile cache and disable all the markers."
  9291. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9292. (setq org-refile-markers nil)
  9293. (setq org-refile-cache nil)
  9294. (message "Refile cache has been cleared"))
  9295. (defun org-refile-cache-check-set (set)
  9296. "Check if all the markers in the cache still have live buffers."
  9297. (let (marker)
  9298. (catch 'exit
  9299. (while (and set (setq marker (nth 3 (pop set))))
  9300. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9301. (when (and marker (null (marker-buffer marker)))
  9302. (message "not found") (sit-for 3)
  9303. (throw 'exit nil)))
  9304. t)))
  9305. (defun org-refile-cache-put (set &rest identifiers)
  9306. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9307. (let* ((key (sha1 (prin1-to-string identifiers)))
  9308. (entry (assoc key org-refile-cache)))
  9309. (if entry
  9310. (setcdr entry set)
  9311. (push (cons key set) org-refile-cache))))
  9312. (defun org-refile-cache-get (&rest identifiers)
  9313. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9314. (cond
  9315. ((not org-refile-cache) nil)
  9316. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9317. (t
  9318. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9319. org-refile-cache))))
  9320. (and set (org-refile-cache-check-set set) set)))))
  9321. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9322. "Produce a table with refile targets."
  9323. (let ((case-fold-search nil)
  9324. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9325. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9326. targets tgs txt re files f desc descre fast-path-p level pos0)
  9327. (message "Getting targets...")
  9328. (with-current-buffer (or default-buffer (current-buffer))
  9329. (while (setq entry (pop entries))
  9330. (setq files (car entry) desc (cdr entry))
  9331. (setq fast-path-p nil)
  9332. (cond
  9333. ((null files) (setq files (list (current-buffer))))
  9334. ((eq files 'org-agenda-files)
  9335. (setq files (org-agenda-files 'unrestricted)))
  9336. ((and (symbolp files) (fboundp files))
  9337. (setq files (funcall files)))
  9338. ((and (symbolp files) (boundp files))
  9339. (setq files (symbol-value files))))
  9340. (if (stringp files) (setq files (list files)))
  9341. (cond
  9342. ((eq (car desc) :tag)
  9343. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9344. ((eq (car desc) :todo)
  9345. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9346. ((eq (car desc) :regexp)
  9347. (setq descre (cdr desc)))
  9348. ((eq (car desc) :level)
  9349. (setq descre (concat "^\\*\\{" (number-to-string
  9350. (if org-odd-levels-only
  9351. (1- (* 2 (cdr desc)))
  9352. (cdr desc)))
  9353. "\\}[ \t]")))
  9354. ((eq (car desc) :maxlevel)
  9355. (setq fast-path-p t)
  9356. (setq descre (concat "^\\*\\{1," (number-to-string
  9357. (if org-odd-levels-only
  9358. (1- (* 2 (cdr desc)))
  9359. (cdr desc)))
  9360. "\\}[ \t]")))
  9361. (t (error "Bad refiling target description %s" desc)))
  9362. (while (setq f (pop files))
  9363. (with-current-buffer
  9364. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9365. (or
  9366. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9367. (progn
  9368. (if (bufferp f) (setq f (buffer-file-name
  9369. (buffer-base-buffer f))))
  9370. (setq f (and f (expand-file-name f)))
  9371. (if (eq org-refile-use-outline-path 'file)
  9372. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9373. (save-excursion
  9374. (save-restriction
  9375. (widen)
  9376. (goto-char (point-min))
  9377. (while (re-search-forward descre nil t)
  9378. (goto-char (setq pos0 (point-at-bol)))
  9379. (catch 'next
  9380. (when org-refile-target-verify-function
  9381. (save-match-data
  9382. (or (funcall org-refile-target-verify-function)
  9383. (throw 'next t))))
  9384. (when (and (looking-at org-complex-heading-regexp)
  9385. (not (member (match-string 4) excluded-entries))
  9386. (match-string 4))
  9387. (setq level (org-reduced-level
  9388. (- (match-end 1) (match-beginning 1)))
  9389. txt (org-link-display-format (match-string 4))
  9390. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9391. re (format org-complex-heading-regexp-format
  9392. (regexp-quote (match-string 4))))
  9393. (when org-refile-use-outline-path
  9394. (setq txt (mapconcat
  9395. 'org-protect-slash
  9396. (append
  9397. (if (eq org-refile-use-outline-path
  9398. 'file)
  9399. (list (file-name-nondirectory
  9400. (buffer-file-name
  9401. (buffer-base-buffer))))
  9402. (if (eq org-refile-use-outline-path
  9403. 'full-file-path)
  9404. (list (buffer-file-name
  9405. (buffer-base-buffer)))))
  9406. (org-get-outline-path fast-path-p
  9407. level txt)
  9408. (list txt))
  9409. "/")))
  9410. (push (list txt f re (org-refile-marker (point)))
  9411. tgs)))
  9412. (when (= (point) pos0)
  9413. ;; verification function has not moved point
  9414. (goto-char (point-at-eol))))))))
  9415. (when org-refile-use-cache
  9416. (org-refile-cache-put tgs (buffer-file-name) descre))
  9417. (setq targets (append tgs targets))
  9418. ))))
  9419. (message "Getting targets...done")
  9420. (nreverse targets)))
  9421. (defun org-protect-slash (s)
  9422. (while (string-match "/" s)
  9423. (setq s (replace-match "\\" t t s)))
  9424. s)
  9425. (defvar org-olpa (make-vector 20 nil))
  9426. (defun org-get-outline-path (&optional fastp level heading)
  9427. "Return the outline path to the current entry, as a list.
  9428. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9429. routine which makes outline path derivations for an entire file,
  9430. avoiding backtracing. Refile target collection makes use of that."
  9431. (if fastp
  9432. (progn
  9433. (if (> level 19)
  9434. (error "Outline path failure, more than 19 levels"))
  9435. (loop for i from level upto 19 do
  9436. (aset org-olpa i nil))
  9437. (prog1
  9438. (delq nil (append org-olpa nil))
  9439. (aset org-olpa level heading)))
  9440. (let (rtn case-fold-search)
  9441. (save-excursion
  9442. (save-restriction
  9443. (widen)
  9444. (while (org-up-heading-safe)
  9445. (when (looking-at org-complex-heading-regexp)
  9446. (push (org-match-string-no-properties 4) rtn)))
  9447. rtn)))))
  9448. (defun org-format-outline-path (path &optional width prefix)
  9449. "Format the outline path PATH for display.
  9450. Width is the maximum number of characters that is available.
  9451. Prefix is a prefix to be included in the returned string,
  9452. such as the file name."
  9453. (setq width (or width 79))
  9454. (if prefix (setq width (- width (length prefix))))
  9455. (if (not path)
  9456. (or prefix "")
  9457. (let* ((nsteps (length path))
  9458. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9459. (maxwidth (if (<= total-width width)
  9460. 10000 ;; everything fits
  9461. ;; we need to shorten the level headings
  9462. (/ (- width nsteps) nsteps)))
  9463. (org-odd-levels-only nil)
  9464. (n 0)
  9465. (total (1+ (length prefix))))
  9466. (setq maxwidth (max maxwidth 10))
  9467. (concat prefix
  9468. (mapconcat
  9469. (lambda (h)
  9470. (setq n (1+ n))
  9471. (if (and (= n nsteps) (< maxwidth 10000))
  9472. (setq maxwidth (- total-width total)))
  9473. (if (< (length h) maxwidth)
  9474. (progn (setq total (+ total (length h) 1)) h)
  9475. (setq h (substring h 0 (- maxwidth 2))
  9476. total (+ total maxwidth 1))
  9477. (if (string-match "[ \t]+\\'" h)
  9478. (setq h (substring h 0 (match-beginning 0))))
  9479. (setq h (concat h "..")))
  9480. (org-add-props h nil 'face
  9481. (nth (% (1- n) org-n-level-faces)
  9482. org-level-faces))
  9483. h)
  9484. path "/")))))
  9485. (defun org-display-outline-path (&optional file current)
  9486. "Display the current outline path in the echo area."
  9487. (interactive "P")
  9488. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9489. (case-fold-search nil)
  9490. (path (and (eq major-mode 'org-mode) (org-get-outline-path))))
  9491. (if current (setq path (append path
  9492. (save-excursion
  9493. (org-back-to-heading t)
  9494. (if (looking-at org-complex-heading-regexp)
  9495. (list (match-string 4)))))))
  9496. (message "%s"
  9497. (org-format-outline-path
  9498. path
  9499. (1- (frame-width))
  9500. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9501. (defvar org-refile-history nil
  9502. "History for refiling operations.")
  9503. (defvar org-after-refile-insert-hook nil
  9504. "Hook run after `org-refile' has inserted its stuff at the new location.
  9505. Note that this is still *before* the stuff will be removed from
  9506. the *old* location.")
  9507. (defvar org-capture-last-stored-marker)
  9508. (defun org-refile (&optional goto default-buffer rfloc)
  9509. "Move the entry or entries at point to another heading.
  9510. The list of target headings is compiled using the information in
  9511. `org-refile-targets', which see.
  9512. At the target location, the entry is filed as a subitem of the target
  9513. heading. Depending on `org-reverse-note-order', the new subitem will
  9514. either be the first or the last subitem.
  9515. If there is an active region, all entries in that region will be moved.
  9516. However, the region must fulfill the requirement that the first heading
  9517. is the first one sets the top-level of the moved text - at most siblings
  9518. below it are allowed.
  9519. With prefix arg GOTO, the command will only visit the target location
  9520. and not actually move anything.
  9521. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9522. go to the location where the last refiling operation has put the subtree.
  9523. With a prefix argument of `2', refile to the running clock.
  9524. RFLOC can be a refile location obtained in a different way.
  9525. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9526. If you are using target caching (see `org-refile-use-cache'),
  9527. You have to clear the target cache in order to find new targets.
  9528. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9529. prefix argument (`C-u C-u C-u C-c C-w')."
  9530. (interactive "P")
  9531. (if (member goto '(0 (64)))
  9532. (org-refile-cache-clear)
  9533. (let* ((cbuf (current-buffer))
  9534. (regionp (org-region-active-p))
  9535. (region-start (and regionp (region-beginning)))
  9536. (region-end (and regionp (region-end)))
  9537. (region-length (and regionp (- region-end region-start)))
  9538. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9539. pos it nbuf file re level reversed)
  9540. (setq last-command nil)
  9541. (when regionp
  9542. (goto-char region-start)
  9543. (or (bolp) (goto-char (point-at-bol)))
  9544. (setq region-start (point))
  9545. (unless (or (org-kill-is-subtree-p
  9546. (buffer-substring region-start region-end))
  9547. (prog1 org-refile-active-region-within-subtree
  9548. (org-toggle-heading)))
  9549. (error "The region is not a (sequence of) subtree(s)")))
  9550. (if (equal goto '(16))
  9551. (org-refile-goto-last-stored)
  9552. (when (or
  9553. (and (equal goto 2)
  9554. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9555. (prog1
  9556. (setq it (list (or org-clock-heading "running clock")
  9557. (buffer-file-name
  9558. (marker-buffer org-clock-hd-marker))
  9559. ""
  9560. (marker-position org-clock-hd-marker)))
  9561. (setq goto nil)))
  9562. (setq it (or rfloc
  9563. (let (heading-text)
  9564. (save-excursion
  9565. (unless goto
  9566. (org-back-to-heading t)
  9567. (setq heading-text
  9568. (nth 4 (org-heading-components))))
  9569. (org-refile-get-location
  9570. (cond (goto "Goto")
  9571. (regionp "Refile region to")
  9572. (t (concat "Refile subtree \""
  9573. heading-text "\" to")))
  9574. default-buffer
  9575. org-refile-allow-creating-parent-nodes
  9576. goto))))))
  9577. (setq file (nth 1 it)
  9578. re (nth 2 it)
  9579. pos (nth 3 it))
  9580. (if (and (not goto)
  9581. pos
  9582. (equal (buffer-file-name) file)
  9583. (if regionp
  9584. (and (>= pos region-start)
  9585. (<= pos region-end))
  9586. (and (>= pos (point))
  9587. (< pos (save-excursion
  9588. (org-end-of-subtree t t))))))
  9589. (error "Cannot refile to position inside the tree or region"))
  9590. (setq nbuf (or (find-buffer-visiting file)
  9591. (find-file-noselect file)))
  9592. (if goto
  9593. (progn
  9594. (org-pop-to-buffer-same-window nbuf)
  9595. (goto-char pos)
  9596. (org-show-context 'org-goto))
  9597. (if regionp
  9598. (progn
  9599. (org-kill-new (buffer-substring region-start region-end))
  9600. (org-save-markers-in-region region-start region-end))
  9601. (org-copy-subtree 1 nil t))
  9602. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9603. (find-file-noselect file)))
  9604. (setq reversed (org-notes-order-reversed-p))
  9605. (save-excursion
  9606. (save-restriction
  9607. (widen)
  9608. (if pos
  9609. (progn
  9610. (goto-char pos)
  9611. (looking-at org-outline-regexp)
  9612. (setq level (org-get-valid-level (funcall outline-level) 1))
  9613. (goto-char
  9614. (if reversed
  9615. (or (outline-next-heading) (point-max))
  9616. (or (save-excursion (org-get-next-sibling))
  9617. (org-end-of-subtree t t)
  9618. (point-max)))))
  9619. (setq level 1)
  9620. (if (not reversed)
  9621. (goto-char (point-max))
  9622. (goto-char (point-min))
  9623. (or (outline-next-heading) (goto-char (point-max)))))
  9624. (if (not (bolp)) (newline))
  9625. (org-paste-subtree level)
  9626. (when org-log-refile
  9627. (org-add-log-setup 'refile nil nil 'findpos
  9628. org-log-refile)
  9629. (unless (eq org-log-refile 'note)
  9630. (save-excursion (org-add-log-note))))
  9631. (and org-auto-align-tags (org-set-tags nil t))
  9632. (bookmark-set "org-refile-last-stored")
  9633. ;; If we are refiling for capture, make sure that the
  9634. ;; last-capture pointers point here
  9635. (when (org-bound-and-true-p org-refile-for-capture)
  9636. (bookmark-set "org-capture-last-stored-marker")
  9637. (move-marker org-capture-last-stored-marker (point)))
  9638. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9639. (run-hooks 'org-after-refile-insert-hook))))
  9640. (if regionp
  9641. (delete-region (point) (+ (point) region-length))
  9642. (org-cut-subtree))
  9643. (when (featurep 'org-inlinetask)
  9644. (org-inlinetask-remove-END-maybe))
  9645. (setq org-markers-to-move nil)
  9646. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9647. (defun org-refile-goto-last-stored ()
  9648. "Go to the location where the last refile was stored."
  9649. (interactive)
  9650. (bookmark-jump "org-refile-last-stored")
  9651. (message "This is the location of the last refile"))
  9652. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9653. no-exclude)
  9654. "Prompt the user for a refile location, using PROMPT.
  9655. PROMPT should not be suffixed with a colon and a space, because
  9656. this function appends the default value from
  9657. `org-refile-history' automatically, if that is not empty.
  9658. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9659. this is used for the GOTO interface."
  9660. (let ((org-refile-targets org-refile-targets)
  9661. (org-refile-use-outline-path org-refile-use-outline-path)
  9662. excluded-entries)
  9663. (when (and (eq major-mode 'org-mode)
  9664. (not org-refile-use-cache)
  9665. (not no-exclude))
  9666. (org-map-tree
  9667. (lambda()
  9668. (setq excluded-entries
  9669. (append excluded-entries (list (org-get-heading t t)))))))
  9670. (setq org-refile-target-table
  9671. (org-refile-get-targets default-buffer excluded-entries)))
  9672. (unless org-refile-target-table
  9673. (error "No refile targets"))
  9674. (let* ((prompt (concat prompt
  9675. (and (car org-refile-history)
  9676. (concat " (default " (car org-refile-history) ")"))
  9677. ": "))
  9678. (cbuf (current-buffer))
  9679. (partial-completion-mode nil)
  9680. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9681. (cfunc (if (and org-refile-use-outline-path
  9682. org-outline-path-complete-in-steps)
  9683. 'org-olpath-completing-read
  9684. 'org-icompleting-read))
  9685. (extra (if org-refile-use-outline-path "/" ""))
  9686. (filename (and cfn (expand-file-name cfn)))
  9687. (tbl (mapcar
  9688. (lambda (x)
  9689. (if (and (not (member org-refile-use-outline-path
  9690. '(file full-file-path)))
  9691. (not (equal filename (nth 1 x))))
  9692. (cons (concat (car x) extra " ("
  9693. (file-name-nondirectory (nth 1 x)) ")")
  9694. (cdr x))
  9695. (cons (concat (car x) extra) (cdr x))))
  9696. org-refile-target-table))
  9697. (completion-ignore-case t)
  9698. pa answ parent-target child parent old-hist)
  9699. (setq old-hist org-refile-history)
  9700. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9701. nil 'org-refile-history (car org-refile-history)))
  9702. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9703. (org-refile-check-position pa)
  9704. (if pa
  9705. (progn
  9706. (when (or (not org-refile-history)
  9707. (not (eq old-hist org-refile-history))
  9708. (not (equal (car pa) (car org-refile-history))))
  9709. (setq org-refile-history
  9710. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9711. org-refile-history
  9712. (cdr org-refile-history))))
  9713. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9714. (pop org-refile-history)))
  9715. pa)
  9716. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9717. (progn
  9718. (setq parent (match-string 1 answ)
  9719. child (match-string 2 answ))
  9720. (setq parent-target (or (assoc parent tbl)
  9721. (assoc (concat parent "/") tbl)))
  9722. (when (and parent-target
  9723. (or (eq new-nodes t)
  9724. (and (eq new-nodes 'confirm)
  9725. (y-or-n-p (format "Create new node \"%s\"? "
  9726. child)))))
  9727. (org-refile-new-child parent-target child)))
  9728. (error "Invalid target location")))))
  9729. (defun org-refile-check-position (refile-pointer)
  9730. "Check if the refile pointer matches the readline to which it points."
  9731. (let* ((file (nth 1 refile-pointer))
  9732. (re (nth 2 refile-pointer))
  9733. (pos (nth 3 refile-pointer))
  9734. buffer)
  9735. (when (org-string-nw-p re)
  9736. (setq buffer (if (markerp pos)
  9737. (marker-buffer pos)
  9738. (or (find-buffer-visiting file)
  9739. (find-file-noselect file))))
  9740. (with-current-buffer buffer
  9741. (save-excursion
  9742. (save-restriction
  9743. (widen)
  9744. (goto-char pos)
  9745. (beginning-of-line 1)
  9746. (unless (org-looking-at-p re)
  9747. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  9748. (defun org-refile-new-child (parent-target child)
  9749. "Use refile target PARENT-TARGET to add new CHILD below it."
  9750. (unless parent-target
  9751. (error "Cannot find parent for new node"))
  9752. (let ((file (nth 1 parent-target))
  9753. (pos (nth 3 parent-target))
  9754. level)
  9755. (with-current-buffer (or (find-buffer-visiting file)
  9756. (find-file-noselect file))
  9757. (save-excursion
  9758. (save-restriction
  9759. (widen)
  9760. (if pos
  9761. (goto-char pos)
  9762. (goto-char (point-max))
  9763. (if (not (bolp)) (newline)))
  9764. (when (looking-at org-outline-regexp)
  9765. (setq level (funcall outline-level))
  9766. (org-end-of-subtree t t))
  9767. (org-back-over-empty-lines)
  9768. (insert "\n" (make-string
  9769. (if pos (org-get-valid-level level 1) 1) ?*)
  9770. " " child "\n")
  9771. (beginning-of-line 0)
  9772. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9773. (defun org-olpath-completing-read (prompt collection &rest args)
  9774. "Read an outline path like a file name."
  9775. (let ((thetable collection)
  9776. (org-completion-use-ido nil) ; does not work with ido.
  9777. (org-completion-use-iswitchb nil)) ; or iswitchb
  9778. (apply
  9779. 'org-icompleting-read prompt
  9780. (lambda (string predicate &optional flag)
  9781. (let (rtn r f (l (length string)))
  9782. (cond
  9783. ((eq flag nil)
  9784. ;; try completion
  9785. (try-completion string thetable))
  9786. ((eq flag t)
  9787. ;; all-completions
  9788. (setq rtn (all-completions string thetable predicate))
  9789. (mapcar
  9790. (lambda (x)
  9791. (setq r (substring x l))
  9792. (if (string-match " ([^)]*)$" x)
  9793. (setq f (match-string 0 x))
  9794. (setq f ""))
  9795. (if (string-match "/" r)
  9796. (concat string (substring r 0 (match-end 0)) f)
  9797. x))
  9798. rtn))
  9799. ((eq flag 'lambda)
  9800. ;; exact match?
  9801. (assoc string thetable)))
  9802. ))
  9803. args)))
  9804. ;;;; Dynamic blocks
  9805. (defun org-find-dblock (name)
  9806. "Find the first dynamic block with name NAME in the buffer.
  9807. If not found, stay at current position and return nil."
  9808. (let (pos)
  9809. (save-excursion
  9810. (goto-char (point-min))
  9811. (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
  9812. nil t)
  9813. (match-beginning 0))))
  9814. (if pos (goto-char pos))
  9815. pos))
  9816. (defconst org-dblock-start-re
  9817. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9818. "Matches the start line of a dynamic block, with parameters.")
  9819. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9820. "Matches the end of a dynamic block.")
  9821. (defun org-create-dblock (plist)
  9822. "Create a dynamic block section, with parameters taken from PLIST.
  9823. PLIST must contain a :name entry which is used as name of the block."
  9824. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9825. (end-of-line 1)
  9826. (newline))
  9827. (let ((col (current-column))
  9828. (name (plist-get plist :name)))
  9829. (insert "#+BEGIN: " name)
  9830. (while plist
  9831. (if (eq (car plist) :name)
  9832. (setq plist (cddr plist))
  9833. (insert " " (prin1-to-string (pop plist)))))
  9834. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9835. (beginning-of-line -2)))
  9836. (defun org-prepare-dblock ()
  9837. "Prepare dynamic block for refresh.
  9838. This empties the block, puts the cursor at the insert position and returns
  9839. the property list including an extra property :name with the block name."
  9840. (unless (looking-at org-dblock-start-re)
  9841. (error "Not at a dynamic block"))
  9842. (let* ((begdel (1+ (match-end 0)))
  9843. (name (org-no-properties (match-string 1)))
  9844. (params (append (list :name name)
  9845. (read (concat "(" (match-string 3) ")")))))
  9846. (save-excursion
  9847. (beginning-of-line 1)
  9848. (skip-chars-forward " \t")
  9849. (setq params (plist-put params :indentation-column (current-column))))
  9850. (unless (re-search-forward org-dblock-end-re nil t)
  9851. (error "Dynamic block not terminated"))
  9852. (setq params
  9853. (append params
  9854. (list :content (buffer-substring
  9855. begdel (match-beginning 0)))))
  9856. (delete-region begdel (match-beginning 0))
  9857. (goto-char begdel)
  9858. (open-line 1)
  9859. params))
  9860. (defun org-map-dblocks (&optional command)
  9861. "Apply COMMAND to all dynamic blocks in the current buffer.
  9862. If COMMAND is not given, use `org-update-dblock'."
  9863. (let ((cmd (or command 'org-update-dblock)))
  9864. (save-excursion
  9865. (goto-char (point-min))
  9866. (while (re-search-forward org-dblock-start-re nil t)
  9867. (goto-char (match-beginning 0))
  9868. (save-excursion
  9869. (condition-case nil
  9870. (funcall cmd)
  9871. (error (message "Error during update of dynamic block"))))
  9872. (unless (re-search-forward org-dblock-end-re nil t)
  9873. (error "Dynamic block not terminated"))))))
  9874. (defun org-dblock-update (&optional arg)
  9875. "User command for updating dynamic blocks.
  9876. Update the dynamic block at point. With prefix ARG, update all dynamic
  9877. blocks in the buffer."
  9878. (interactive "P")
  9879. (if arg
  9880. (org-update-all-dblocks)
  9881. (or (looking-at org-dblock-start-re)
  9882. (org-beginning-of-dblock))
  9883. (org-update-dblock)))
  9884. (defun org-update-dblock ()
  9885. "Update the dynamic block at point.
  9886. This means to empty the block, parse for parameters and then call
  9887. the correct writing function."
  9888. (interactive)
  9889. (save-window-excursion
  9890. (let* ((pos (point))
  9891. (line (org-current-line))
  9892. (params (org-prepare-dblock))
  9893. (name (plist-get params :name))
  9894. (indent (plist-get params :indentation-column))
  9895. (cmd (intern (concat "org-dblock-write:" name))))
  9896. (message "Updating dynamic block `%s' at line %d..." name line)
  9897. (funcall cmd params)
  9898. (message "Updating dynamic block `%s' at line %d...done" name line)
  9899. (goto-char pos)
  9900. (when (and indent (> indent 0))
  9901. (setq indent (make-string indent ?\ ))
  9902. (save-excursion
  9903. (org-beginning-of-dblock)
  9904. (forward-line 1)
  9905. (while (not (looking-at org-dblock-end-re))
  9906. (insert indent)
  9907. (beginning-of-line 2))
  9908. (when (looking-at org-dblock-end-re)
  9909. (and (looking-at "[ \t]+")
  9910. (replace-match ""))
  9911. (insert indent)))))))
  9912. (defun org-beginning-of-dblock ()
  9913. "Find the beginning of the dynamic block at point.
  9914. Error if there is no such block at point."
  9915. (let ((pos (point))
  9916. beg)
  9917. (end-of-line 1)
  9918. (if (and (re-search-backward org-dblock-start-re nil t)
  9919. (setq beg (match-beginning 0))
  9920. (re-search-forward org-dblock-end-re nil t)
  9921. (> (match-end 0) pos))
  9922. (goto-char beg)
  9923. (goto-char pos)
  9924. (error "Not in a dynamic block"))))
  9925. (defun org-update-all-dblocks ()
  9926. "Update all dynamic blocks in the buffer.
  9927. This function can be used in a hook."
  9928. (interactive)
  9929. (when (eq major-mode 'org-mode)
  9930. (org-map-dblocks 'org-update-dblock)))
  9931. ;;;; Completion
  9932. (defconst org-additional-option-like-keywords
  9933. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  9934. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  9935. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9936. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  9937. "BEGIN:" "END:"
  9938. "ORGTBL" "TBLFM:" "TBLNAME:"
  9939. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9940. "BEGIN_QUOTE" "END_QUOTE"
  9941. "BEGIN_VERSE" "END_VERSE"
  9942. "BEGIN_CENTER" "END_CENTER"
  9943. "BEGIN_SRC" "END_SRC"
  9944. "BEGIN_RESULT" "END_RESULT"
  9945. "NAME:" "RESULTS:"
  9946. "HEADER:" "HEADERS:"
  9947. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  9948. "CAPTION:" "LABEL:"
  9949. "SETUPFILE:"
  9950. "INCLUDE:"
  9951. "BIND:"
  9952. "MACRO:"))
  9953. (defcustom org-structure-template-alist
  9954. '(
  9955. ("s" "#+begin_src ?\n\n#+end_src"
  9956. "<src lang=\"?\">\n\n</src>")
  9957. ("e" "#+begin_example\n?\n#+end_example"
  9958. "<example>\n?\n</example>")
  9959. ("q" "#+begin_quote\n?\n#+end_quote"
  9960. "<quote>\n?\n</quote>")
  9961. ("v" "#+begin_verse\n?\n#+end_verse"
  9962. "<verse>\n?\n/verse>")
  9963. ("c" "#+begin_center\n?\n#+end_center"
  9964. "<center>\n?\n/center>")
  9965. ("l" "#+begin_latex\n?\n#+end_latex"
  9966. "<literal style=\"latex\">\n?\n</literal>")
  9967. ("L" "#+latex: "
  9968. "<literal style=\"latex\">?</literal>")
  9969. ("h" "#+begin_html\n?\n#+end_html"
  9970. "<literal style=\"html\">\n?\n</literal>")
  9971. ("H" "#+html: "
  9972. "<literal style=\"html\">?</literal>")
  9973. ("a" "#+begin_ascii\n?\n#+end_ascii")
  9974. ("A" "#+ascii: ")
  9975. ("i" "#+index: ?"
  9976. "#+index: ?")
  9977. ("I" "#+include %file ?"
  9978. "<include file=%file markup=\"?\">")
  9979. )
  9980. "Structure completion elements.
  9981. This is a list of abbreviation keys and values. The value gets inserted
  9982. if you type `<' followed by the key and then press the completion key,
  9983. usually `M-TAB'. %file will be replaced by a file name after prompting
  9984. for the file using completion. The cursor will be placed at the position
  9985. of the `?` in the template.
  9986. There are two templates for each key, the first uses the original Org syntax,
  9987. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  9988. the default when the /org-mtags.el/ module has been loaded. See also the
  9989. variable `org-mtags-prefer-muse-templates'.
  9990. This is an experimental feature, it is undecided if it is going to stay in."
  9991. :group 'org-completion
  9992. :type '(repeat
  9993. (string :tag "Key")
  9994. (string :tag "Template")
  9995. (string :tag "Muse Template")))
  9996. (defun org-try-structure-completion ()
  9997. "Try to complete a structure template before point.
  9998. This looks for strings like \"<e\" on an otherwise empty line and
  9999. expands them."
  10000. (let ((l (buffer-substring (point-at-bol) (point)))
  10001. a)
  10002. (when (and (looking-at "[ \t]*$")
  10003. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10004. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10005. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10006. (match-beginning 1)) a)
  10007. t)))
  10008. (defun org-complete-expand-structure-template (start cell)
  10009. "Expand a structure template."
  10010. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10011. (rpl (nth (if musep 2 1) cell))
  10012. (ind ""))
  10013. (delete-region start (point))
  10014. (when (string-match "\\`#\\+" rpl)
  10015. (cond
  10016. ((bolp))
  10017. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10018. (setq ind (buffer-substring (point-at-bol) (point))))
  10019. (t (newline))))
  10020. (setq start (point))
  10021. (if (string-match "%file" rpl)
  10022. (setq rpl (replace-match
  10023. (concat
  10024. "\""
  10025. (save-match-data
  10026. (abbreviate-file-name (read-file-name "Include file: ")))
  10027. "\"")
  10028. t t rpl)))
  10029. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10030. (concat "\n" ind)))
  10031. (insert rpl)
  10032. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10033. ;;;; TODO, DEADLINE, Comments
  10034. (defun org-toggle-comment ()
  10035. "Change the COMMENT state of an entry."
  10036. (interactive)
  10037. (save-excursion
  10038. (org-back-to-heading)
  10039. (let (case-fold-search)
  10040. (cond
  10041. ((looking-at (format org-heading-keyword-regexp-format
  10042. org-comment-string))
  10043. (goto-char (match-end 1))
  10044. (looking-at (concat " +" org-comment-string))
  10045. (replace-match "" t t)
  10046. (when (eolp) (insert " ")))
  10047. ((looking-at org-outline-regexp)
  10048. (goto-char (match-end 0))
  10049. (insert org-comment-string " "))))))
  10050. (defvar org-last-todo-state-is-todo nil
  10051. "This is non-nil when the last TODO state change led to a TODO state.
  10052. If the last change removed the TODO tag or switched to DONE, then
  10053. this is nil.")
  10054. (defvar org-setting-tags nil) ; dynamically skipped
  10055. (defvar org-todo-setup-filter-hook nil
  10056. "Hook for functions that pre-filter todo specs.
  10057. Each function takes a todo spec and returns either nil or the spec
  10058. transformed into canonical form." )
  10059. (defvar org-todo-get-default-hook nil
  10060. "Hook for functions that get a default item for todo.
  10061. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10062. nil or a string to be used for the todo mark." )
  10063. (defvar org-agenda-headline-snapshot-before-repeat)
  10064. (defun org-current-effective-time ()
  10065. "Return current time adjusted for `org-extend-today-until' variable"
  10066. (let* ((ct (org-current-time))
  10067. (dct (decode-time ct))
  10068. (ct1
  10069. (if (and org-use-effective-time
  10070. (< (nth 2 dct) org-extend-today-until))
  10071. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  10072. ct)))
  10073. ct1))
  10074. (defun org-todo-yesterday (&optional arg)
  10075. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10076. (interactive "P")
  10077. (if (eq major-mode 'org-agenda-mode)
  10078. (apply 'org-agenda-todo-yesterday arg)
  10079. (let* ((hour (third (decode-time
  10080. (org-current-time))))
  10081. (org-extend-today-until (1+ hour)))
  10082. (org-todo arg))))
  10083. (defun org-todo (&optional arg)
  10084. "Change the TODO state of an item.
  10085. The state of an item is given by a keyword at the start of the heading,
  10086. like
  10087. *** TODO Write paper
  10088. *** DONE Call mom
  10089. The different keywords are specified in the variable `org-todo-keywords'.
  10090. By default the available states are \"TODO\" and \"DONE\".
  10091. So for this example: when the item starts with TODO, it is changed to DONE.
  10092. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10093. DONE are present, add TODO at the beginning of the heading.
  10094. With \\[universal-argument] prefix arg, use completion to determine the new \
  10095. state.
  10096. With numeric prefix arg, switch to that state.
  10097. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10098. keywords (nextset).
  10099. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10100. With a numeric prefix arg of 0, inhibit note taking for the change.
  10101. For calling through lisp, arg is also interpreted in the following way:
  10102. 'none -> empty state
  10103. \"\"(empty string) -> switch to empty state
  10104. 'done -> switch to DONE
  10105. 'nextset -> switch to the next set of keywords
  10106. 'previousset -> switch to the previous set of keywords
  10107. \"WAITING\" -> switch to the specified keyword, but only if it
  10108. really is a member of `org-todo-keywords'."
  10109. (interactive "P")
  10110. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10111. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10112. 'region-start-level 'region))
  10113. org-loop-over-headlines-in-active-region)
  10114. (org-map-entries
  10115. `(org-todo ,arg)
  10116. org-loop-over-headlines-in-active-region
  10117. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10118. (if (equal arg '(16)) (setq arg 'nextset))
  10119. (let ((org-blocker-hook org-blocker-hook)
  10120. (case-fold-search nil))
  10121. (when (equal arg '(64))
  10122. (setq arg nil org-blocker-hook nil))
  10123. (when (and org-blocker-hook
  10124. (or org-inhibit-blocking
  10125. (org-entry-get nil "NOBLOCKING")))
  10126. (setq org-blocker-hook nil))
  10127. (save-excursion
  10128. (catch 'exit
  10129. (org-back-to-heading t)
  10130. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10131. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10132. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10133. (let* ((match-data (match-data))
  10134. (startpos (point-at-bol))
  10135. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10136. (org-log-done org-log-done)
  10137. (org-log-repeat org-log-repeat)
  10138. (org-todo-log-states org-todo-log-states)
  10139. (org-inhibit-logging
  10140. (if (equal arg 0)
  10141. (progn (setq arg nil) 'note) org-inhibit-logging))
  10142. (this (match-string 1))
  10143. (hl-pos (match-beginning 0))
  10144. (head (org-get-todo-sequence-head this))
  10145. (ass (assoc head org-todo-kwd-alist))
  10146. (interpret (nth 1 ass))
  10147. (done-word (nth 3 ass))
  10148. (final-done-word (nth 4 ass))
  10149. (last-state (or this ""))
  10150. (completion-ignore-case t)
  10151. (member (member this org-todo-keywords-1))
  10152. (tail (cdr member))
  10153. (state (cond
  10154. ((and org-todo-key-trigger
  10155. (or (and (equal arg '(4))
  10156. (eq org-use-fast-todo-selection 'prefix))
  10157. (and (not arg) org-use-fast-todo-selection
  10158. (not (eq org-use-fast-todo-selection
  10159. 'prefix)))))
  10160. ;; Use fast selection
  10161. (org-fast-todo-selection))
  10162. ((and (equal arg '(4))
  10163. (or (not org-use-fast-todo-selection)
  10164. (not org-todo-key-trigger)))
  10165. ;; Read a state with completion
  10166. (org-icompleting-read
  10167. "State: " (mapcar (lambda(x) (list x))
  10168. org-todo-keywords-1)
  10169. nil t))
  10170. ((eq arg 'right)
  10171. (if this
  10172. (if tail (car tail) nil)
  10173. (car org-todo-keywords-1)))
  10174. ((eq arg 'left)
  10175. (if (equal member org-todo-keywords-1)
  10176. nil
  10177. (if this
  10178. (nth (- (length org-todo-keywords-1)
  10179. (length tail) 2)
  10180. org-todo-keywords-1)
  10181. (org-last org-todo-keywords-1))))
  10182. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10183. (setq arg nil))) ; hack to fall back to cycling
  10184. (arg
  10185. ;; user or caller requests a specific state
  10186. (cond
  10187. ((equal arg "") nil)
  10188. ((eq arg 'none) nil)
  10189. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10190. ((eq arg 'nextset)
  10191. (or (car (cdr (member head org-todo-heads)))
  10192. (car org-todo-heads)))
  10193. ((eq arg 'previousset)
  10194. (let ((org-todo-heads (reverse org-todo-heads)))
  10195. (or (car (cdr (member head org-todo-heads)))
  10196. (car org-todo-heads))))
  10197. ((car (member arg org-todo-keywords-1)))
  10198. ((stringp arg)
  10199. (error "State `%s' not valid in this file" arg))
  10200. ((nth (1- (prefix-numeric-value arg))
  10201. org-todo-keywords-1))))
  10202. ((null member) (or head (car org-todo-keywords-1)))
  10203. ((equal this final-done-word) nil) ;; -> make empty
  10204. ((null tail) nil) ;; -> first entry
  10205. ((memq interpret '(type priority))
  10206. (if (eq this-command last-command)
  10207. (car tail)
  10208. (if (> (length tail) 0)
  10209. (or done-word (car org-done-keywords))
  10210. nil)))
  10211. (t
  10212. (car tail))))
  10213. (state (or
  10214. (run-hook-with-args-until-success
  10215. 'org-todo-get-default-hook state last-state)
  10216. state))
  10217. (next (if state (concat " " state " ") " "))
  10218. (change-plist (list :type 'todo-state-change :from this :to state
  10219. :position startpos))
  10220. dolog now-done-p)
  10221. (when org-blocker-hook
  10222. (setq org-last-todo-state-is-todo
  10223. (not (member this org-done-keywords)))
  10224. (unless (save-excursion
  10225. (save-match-data
  10226. (org-with-wide-buffer
  10227. (run-hook-with-args-until-failure
  10228. 'org-blocker-hook change-plist))))
  10229. (if (org-called-interactively-p 'interactive)
  10230. (error "TODO state change from %s to %s blocked" this state)
  10231. ;; fail silently
  10232. (message "TODO state change from %s to %s blocked" this state)
  10233. (throw 'exit nil))))
  10234. (store-match-data match-data)
  10235. (replace-match next t t)
  10236. (unless (pos-visible-in-window-p hl-pos)
  10237. (message "TODO state changed to %s" (org-trim next)))
  10238. (unless head
  10239. (setq head (org-get-todo-sequence-head state)
  10240. ass (assoc head org-todo-kwd-alist)
  10241. interpret (nth 1 ass)
  10242. done-word (nth 3 ass)
  10243. final-done-word (nth 4 ass)))
  10244. (when (memq arg '(nextset previousset))
  10245. (message "Keyword-Set %d/%d: %s"
  10246. (- (length org-todo-sets) -1
  10247. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  10248. (length org-todo-sets)
  10249. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  10250. (setq org-last-todo-state-is-todo
  10251. (not (member state org-done-keywords)))
  10252. (setq now-done-p (and (member state org-done-keywords)
  10253. (not (member this org-done-keywords))))
  10254. (and logging (org-local-logging logging))
  10255. (when (and (or org-todo-log-states org-log-done)
  10256. (not (eq org-inhibit-logging t))
  10257. (not (memq arg '(nextset previousset))))
  10258. ;; we need to look at recording a time and note
  10259. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  10260. (nth 2 (assoc this org-todo-log-states))))
  10261. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10262. (setq dolog 'time))
  10263. (when (and state
  10264. (member state org-not-done-keywords)
  10265. (not (member this org-not-done-keywords)))
  10266. ;; This is now a todo state and was not one before
  10267. ;; If there was a CLOSED time stamp, get rid of it.
  10268. (org-add-planning-info nil nil 'closed))
  10269. (when (and now-done-p org-log-done)
  10270. ;; It is now done, and it was not done before
  10271. (org-add-planning-info 'closed (org-current-effective-time))
  10272. (if (and (not dolog) (eq 'note org-log-done))
  10273. (org-add-log-setup 'done state this 'findpos 'note)))
  10274. (when (and state dolog)
  10275. ;; This is a non-nil state, and we need to log it
  10276. (org-add-log-setup 'state state this 'findpos dolog)))
  10277. ;; Fixup tag positioning
  10278. (org-todo-trigger-tag-changes state)
  10279. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10280. (when org-provide-todo-statistics
  10281. (org-update-parent-todo-statistics))
  10282. (run-hooks 'org-after-todo-state-change-hook)
  10283. (if (and arg (not (member state org-done-keywords)))
  10284. (setq head (org-get-todo-sequence-head state)))
  10285. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10286. ;; Do we need to trigger a repeat?
  10287. (when now-done-p
  10288. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10289. ;; This is for the agenda, take a snapshot of the headline.
  10290. (save-match-data
  10291. (setq org-agenda-headline-snapshot-before-repeat
  10292. (org-get-heading))))
  10293. (org-auto-repeat-maybe state))
  10294. ;; Fixup cursor location if close to the keyword
  10295. (if (and (outline-on-heading-p)
  10296. (not (bolp))
  10297. (save-excursion (beginning-of-line 1)
  10298. (looking-at org-todo-line-regexp))
  10299. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10300. (progn
  10301. (goto-char (or (match-end 2) (match-end 1)))
  10302. (and (looking-at " ") (just-one-space))))
  10303. (when org-trigger-hook
  10304. (save-excursion
  10305. (run-hook-with-args 'org-trigger-hook change-plist)))))))))
  10306. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10307. "Block turning an entry into a TODO, using the hierarchy.
  10308. This checks whether the current task should be blocked from state
  10309. changes. Such blocking occurs when:
  10310. 1. The task has children which are not all in a completed state.
  10311. 2. A task has a parent with the property :ORDERED:, and there
  10312. are siblings prior to the current task with incomplete
  10313. status.
  10314. 3. The parent of the task is blocked because it has siblings that should
  10315. be done first, or is child of a block grandparent TODO entry."
  10316. (if (not org-enforce-todo-dependencies)
  10317. t ; if locally turned off don't block
  10318. (catch 'dont-block
  10319. ;; If this is not a todo state change, or if this entry is already DONE,
  10320. ;; do not block
  10321. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10322. (member (plist-get change-plist :from)
  10323. (cons 'done org-done-keywords))
  10324. (member (plist-get change-plist :to)
  10325. (cons 'todo org-not-done-keywords))
  10326. (not (plist-get change-plist :to)))
  10327. (throw 'dont-block t))
  10328. ;; If this task has children, and any are undone, it's blocked
  10329. (save-excursion
  10330. (org-back-to-heading t)
  10331. (let ((this-level (funcall outline-level)))
  10332. (outline-next-heading)
  10333. (let ((child-level (funcall outline-level)))
  10334. (while (and (not (eobp))
  10335. (> child-level this-level))
  10336. ;; this todo has children, check whether they are all
  10337. ;; completed
  10338. (if (and (not (org-entry-is-done-p))
  10339. (org-entry-is-todo-p))
  10340. (throw 'dont-block nil))
  10341. (outline-next-heading)
  10342. (setq child-level (funcall outline-level))))))
  10343. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10344. ;; any previous siblings are undone, it's blocked
  10345. (save-excursion
  10346. (org-back-to-heading t)
  10347. (let* ((pos (point))
  10348. (parent-pos (and (org-up-heading-safe) (point))))
  10349. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10350. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10351. (forward-line 1)
  10352. (re-search-forward org-not-done-heading-regexp pos t))
  10353. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10354. ;; Search further up the hierarchy, to see if an anchestor is blocked
  10355. (while t
  10356. (goto-char parent-pos)
  10357. (if (not (looking-at org-not-done-heading-regexp))
  10358. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10359. (setq pos (point))
  10360. (setq parent-pos (and (org-up-heading-safe) (point)))
  10361. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10362. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10363. (forward-line 1)
  10364. (re-search-forward org-not-done-heading-regexp pos t))
  10365. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10366. (defcustom org-track-ordered-property-with-tag nil
  10367. "Should the ORDERED property also be shown as a tag?
  10368. The ORDERED property decides if an entry should require subtasks to be
  10369. completed in sequence. Since a property is not very visible, setting
  10370. this option means that toggling the ORDERED property with the command
  10371. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10372. not relevant for the behavior, but it makes things more visible.
  10373. Note that toggling the tag with tags commands will not change the property
  10374. and therefore not influence behavior!
  10375. This can be t, meaning the tag ORDERED should be used, It can also be a
  10376. string to select a different tag for this task."
  10377. :group 'org-todo
  10378. :type '(choice
  10379. (const :tag "No tracking" nil)
  10380. (const :tag "Track with ORDERED tag" t)
  10381. (string :tag "Use other tag")))
  10382. (defun org-toggle-ordered-property ()
  10383. "Toggle the ORDERED property of the current entry.
  10384. For better visibility, you can track the value of this property with a tag.
  10385. See variable `org-track-ordered-property-with-tag'."
  10386. (interactive)
  10387. (let* ((t1 org-track-ordered-property-with-tag)
  10388. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10389. (save-excursion
  10390. (org-back-to-heading)
  10391. (if (org-entry-get nil "ORDERED")
  10392. (progn
  10393. (org-delete-property "ORDERED")
  10394. (and tag (org-toggle-tag tag 'off))
  10395. (message "Subtasks can be completed in arbitrary order"))
  10396. (org-entry-put nil "ORDERED" "t")
  10397. (and tag (org-toggle-tag tag 'on))
  10398. (message "Subtasks must be completed in sequence")))))
  10399. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10400. (defun org-block-todo-from-checkboxes (change-plist)
  10401. "Block turning an entry into a TODO, using checkboxes.
  10402. This checks whether the current task should be blocked from state
  10403. changes because there are unchecked boxes in this entry."
  10404. (if (not org-enforce-todo-checkbox-dependencies)
  10405. t ; if locally turned off don't block
  10406. (catch 'dont-block
  10407. ;; If this is not a todo state change, or if this entry is already DONE,
  10408. ;; do not block
  10409. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10410. (member (plist-get change-plist :from)
  10411. (cons 'done org-done-keywords))
  10412. (member (plist-get change-plist :to)
  10413. (cons 'todo org-not-done-keywords))
  10414. (not (plist-get change-plist :to)))
  10415. (throw 'dont-block t))
  10416. ;; If this task has checkboxes that are not checked, it's blocked
  10417. (save-excursion
  10418. (org-back-to-heading t)
  10419. (let ((beg (point)) end)
  10420. (outline-next-heading)
  10421. (setq end (point))
  10422. (goto-char beg)
  10423. (if (org-list-search-forward
  10424. (concat (org-item-beginning-re)
  10425. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10426. "\\[[- ]\\]")
  10427. end t)
  10428. (progn
  10429. (if (boundp 'org-blocked-by-checkboxes)
  10430. (setq org-blocked-by-checkboxes t))
  10431. (throw 'dont-block nil)))))
  10432. t))) ; do not block
  10433. (defun org-entry-blocked-p ()
  10434. "Is the current entry blocked?"
  10435. (if (org-entry-get nil "NOBLOCKING")
  10436. nil ;; Never block this entry
  10437. (not
  10438. (run-hook-with-args-until-failure
  10439. 'org-blocker-hook
  10440. (list :type 'todo-state-change
  10441. :position (point)
  10442. :from 'todo
  10443. :to 'done)))))
  10444. (defun org-update-statistics-cookies (all)
  10445. "Update the statistics cookie, either from TODO or from checkboxes.
  10446. This should be called with the cursor in a line with a statistics cookie."
  10447. (interactive "P")
  10448. (if all
  10449. (progn
  10450. (org-update-checkbox-count 'all)
  10451. (org-map-entries 'org-update-parent-todo-statistics))
  10452. (if (not (org-at-heading-p))
  10453. (org-update-checkbox-count)
  10454. (let ((pos (move-marker (make-marker) (point)))
  10455. end l1 l2)
  10456. (ignore-errors (org-back-to-heading t))
  10457. (if (not (org-at-heading-p))
  10458. (org-update-checkbox-count)
  10459. (setq l1 (org-outline-level))
  10460. (setq end (save-excursion
  10461. (outline-next-heading)
  10462. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10463. (point)))
  10464. (if (and (save-excursion
  10465. (re-search-forward
  10466. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10467. (not (save-excursion (re-search-forward
  10468. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10469. (org-update-checkbox-count)
  10470. (if (and l2 (> l2 l1))
  10471. (progn
  10472. (goto-char end)
  10473. (org-update-parent-todo-statistics))
  10474. (goto-char pos)
  10475. (beginning-of-line 1)
  10476. (while (re-search-forward
  10477. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10478. (point-at-eol) t)
  10479. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10480. (goto-char pos)
  10481. (move-marker pos nil)))))
  10482. (defvar org-entry-property-inherited-from) ;; defined below
  10483. (defun org-update-parent-todo-statistics ()
  10484. "Update any statistics cookie in the parent of the current headline.
  10485. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10486. the entire subtree and this will travel up the hierarchy and update
  10487. statistics everywhere."
  10488. (let* ((prop (save-excursion (org-up-heading-safe)
  10489. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10490. (recursive (or (not org-hierarchical-todo-statistics)
  10491. (and prop (string-match "\\<recursive\\>" prop))))
  10492. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10493. 0))
  10494. (first t)
  10495. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10496. level ltoggle l1 new ndel
  10497. (cnt-all 0) (cnt-done 0) is-percent kwd
  10498. checkbox-beg ov ovs ove cookie-present)
  10499. (catch 'exit
  10500. (save-excursion
  10501. (beginning-of-line 1)
  10502. (setq ltoggle (funcall outline-level))
  10503. ;; Three situations are to consider:
  10504. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10505. ;; to the top-level ancestor on the headline;
  10506. ;; 2. If parent has "recursive" property, repeat up to the
  10507. ;; headline setting that property, taking inheritance into
  10508. ;; account;
  10509. ;; 3. Else, move up to direct parent and proceed only once.
  10510. (while (and (setq level (org-up-heading-safe))
  10511. (or recursive first)
  10512. (>= (point) lim))
  10513. (setq first nil cookie-present nil)
  10514. (unless (and level
  10515. (not (string-match
  10516. "\\<checkbox\\>"
  10517. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10518. "")))))
  10519. (throw 'exit nil))
  10520. (while (re-search-forward box-re (point-at-eol) t)
  10521. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10522. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10523. (save-match-data
  10524. (unless (outline-next-heading) (throw 'exit nil))
  10525. (while (and (looking-at org-complex-heading-regexp)
  10526. (> (setq l1 (length (match-string 1))) level))
  10527. (setq kwd (and (or recursive (= l1 ltoggle))
  10528. (match-string 2)))
  10529. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10530. (and (listp org-provide-todo-statistics)
  10531. (or (member kwd org-provide-todo-statistics)
  10532. (member kwd org-done-keywords))))
  10533. (setq cnt-all (1+ cnt-all))
  10534. (if (eq org-provide-todo-statistics t)
  10535. (and kwd (setq cnt-all (1+ cnt-all)))))
  10536. (and (member kwd org-done-keywords)
  10537. (setq cnt-done (1+ cnt-done)))
  10538. (outline-next-heading)))
  10539. (setq new
  10540. (if is-percent
  10541. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10542. (format "[%d/%d]" cnt-done cnt-all))
  10543. ndel (- (match-end 0) checkbox-beg))
  10544. ;; handle overlays when updating cookie from column view
  10545. (when (setq ov (car (overlays-at checkbox-beg)))
  10546. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10547. (delete-overlay ov))
  10548. (goto-char checkbox-beg)
  10549. (insert new)
  10550. (delete-region (point) (+ (point) ndel))
  10551. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10552. (when ov (move-overlay ov ovs ove)))
  10553. (when cookie-present
  10554. (run-hook-with-args 'org-after-todo-statistics-hook
  10555. cnt-done (- cnt-all cnt-done))))))
  10556. (run-hooks 'org-todo-statistics-hook)))
  10557. (defvar org-after-todo-statistics-hook nil
  10558. "Hook that is called after a TODO statistics cookie has been updated.
  10559. Each function is called with two arguments: the number of not-done entries
  10560. and the number of done entries.
  10561. For example, the following function, when added to this hook, will switch
  10562. an entry to DONE when all children are done, and back to TODO when new
  10563. entries are set to a TODO status. Note that this hook is only called
  10564. when there is a statistics cookie in the headline!
  10565. (defun org-summary-todo (n-done n-not-done)
  10566. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10567. (let (org-log-done org-log-states) ; turn off logging
  10568. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10569. ")
  10570. (defvar org-todo-statistics-hook nil
  10571. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10572. This hook runs even if there is no statistics cookie present, in which case
  10573. `org-after-todo-statistics-hook' would not run.")
  10574. (defun org-todo-trigger-tag-changes (state)
  10575. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10576. (let ((l org-todo-state-tags-triggers)
  10577. changes)
  10578. (when (or (not state) (equal state ""))
  10579. (setq changes (append changes (cdr (assoc "" l)))))
  10580. (when (and (stringp state) (> (length state) 0))
  10581. (setq changes (append changes (cdr (assoc state l)))))
  10582. (when (member state org-not-done-keywords)
  10583. (setq changes (append changes (cdr (assoc 'todo l)))))
  10584. (when (member state org-done-keywords)
  10585. (setq changes (append changes (cdr (assoc 'done l)))))
  10586. (dolist (c changes)
  10587. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10588. (defun org-local-logging (value)
  10589. "Get logging settings from a property VALUE."
  10590. (let* (words w a)
  10591. ;; directly set the variables, they are already local.
  10592. (setq org-log-done nil
  10593. org-log-repeat nil
  10594. org-todo-log-states nil)
  10595. (setq words (org-split-string value))
  10596. (while (setq w (pop words))
  10597. (cond
  10598. ((setq a (assoc w org-startup-options))
  10599. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10600. (set (nth 1 a) (nth 2 a))))
  10601. ((setq a (org-extract-log-state-settings w))
  10602. (and (member (car a) org-todo-keywords-1)
  10603. (push a org-todo-log-states)))))))
  10604. (defun org-get-todo-sequence-head (kwd)
  10605. "Return the head of the TODO sequence to which KWD belongs.
  10606. If KWD is not set, check if there is a text property remembering the
  10607. right sequence."
  10608. (let (p)
  10609. (cond
  10610. ((not kwd)
  10611. (or (get-text-property (point-at-bol) 'org-todo-head)
  10612. (progn
  10613. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10614. nil (point-at-eol)))
  10615. (get-text-property p 'org-todo-head))))
  10616. ((not (member kwd org-todo-keywords-1))
  10617. (car org-todo-keywords-1))
  10618. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10619. (defun org-fast-todo-selection ()
  10620. "Fast TODO keyword selection with single keys.
  10621. Returns the new TODO keyword, or nil if no state change should occur."
  10622. (let* ((fulltable org-todo-key-alist)
  10623. (done-keywords org-done-keywords) ;; needed for the faces.
  10624. (maxlen (apply 'max (mapcar
  10625. (lambda (x)
  10626. (if (stringp (car x)) (string-width (car x)) 0))
  10627. fulltable)))
  10628. (expert nil)
  10629. (fwidth (+ maxlen 3 1 3))
  10630. (ncol (/ (- (window-width) 4) fwidth))
  10631. tg cnt e c tbl
  10632. groups ingroup)
  10633. (save-excursion
  10634. (save-window-excursion
  10635. (if expert
  10636. (set-buffer (get-buffer-create " *Org todo*"))
  10637. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10638. (erase-buffer)
  10639. (org-set-local 'org-done-keywords done-keywords)
  10640. (setq tbl fulltable cnt 0)
  10641. (while (setq e (pop tbl))
  10642. (cond
  10643. ((equal e '(:startgroup))
  10644. (push '() groups) (setq ingroup t)
  10645. (when (not (= cnt 0))
  10646. (setq cnt 0)
  10647. (insert "\n"))
  10648. (insert "{ "))
  10649. ((equal e '(:endgroup))
  10650. (setq ingroup nil cnt 0)
  10651. (insert "}\n"))
  10652. ((equal e '(:newline))
  10653. (when (not (= cnt 0))
  10654. (setq cnt 0)
  10655. (insert "\n")
  10656. (setq e (car tbl))
  10657. (while (equal (car tbl) '(:newline))
  10658. (insert "\n")
  10659. (setq tbl (cdr tbl)))))
  10660. (t
  10661. (setq tg (car e) c (cdr e))
  10662. (if ingroup (push tg (car groups)))
  10663. (setq tg (org-add-props tg nil 'face
  10664. (org-get-todo-face tg)))
  10665. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10666. (insert "[" c "] " tg (make-string
  10667. (- fwidth 4 (length tg)) ?\ ))
  10668. (when (= (setq cnt (1+ cnt)) ncol)
  10669. (insert "\n")
  10670. (if ingroup (insert " "))
  10671. (setq cnt 0)))))
  10672. (insert "\n")
  10673. (goto-char (point-min))
  10674. (if (not expert) (org-fit-window-to-buffer))
  10675. (message "[a-z..]:Set [SPC]:clear")
  10676. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10677. (cond
  10678. ((or (= c ?\C-g)
  10679. (and (= c ?q) (not (rassoc c fulltable))))
  10680. (setq quit-flag t))
  10681. ((= c ?\ ) nil)
  10682. ((setq e (rassoc c fulltable) tg (car e))
  10683. tg)
  10684. (t (setq quit-flag t)))))))
  10685. (defun org-entry-is-todo-p ()
  10686. (member (org-get-todo-state) org-not-done-keywords))
  10687. (defun org-entry-is-done-p ()
  10688. (member (org-get-todo-state) org-done-keywords))
  10689. (defun org-get-todo-state ()
  10690. (save-excursion
  10691. (org-back-to-heading t)
  10692. (and (looking-at org-todo-line-regexp)
  10693. (match-end 2)
  10694. (match-string 2))))
  10695. (defun org-at-date-range-p (&optional inactive-ok)
  10696. "Is the cursor inside a date range?"
  10697. (interactive)
  10698. (save-excursion
  10699. (catch 'exit
  10700. (let ((pos (point)))
  10701. (skip-chars-backward "^[<\r\n")
  10702. (skip-chars-backward "<[")
  10703. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10704. (>= (match-end 0) pos)
  10705. (throw 'exit t))
  10706. (skip-chars-backward "^<[\r\n")
  10707. (skip-chars-backward "<[")
  10708. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10709. (>= (match-end 0) pos)
  10710. (throw 'exit t)))
  10711. nil)))
  10712. (defun org-get-repeat (&optional tagline)
  10713. "Check if there is a deadline/schedule with repeater in this entry."
  10714. (save-match-data
  10715. (save-excursion
  10716. (org-back-to-heading t)
  10717. (and (re-search-forward (if tagline
  10718. (concat tagline "\\s-*" org-repeat-re)
  10719. org-repeat-re)
  10720. (org-entry-end-position) t)
  10721. (match-string-no-properties 1)))))
  10722. (defvar org-last-changed-timestamp)
  10723. (defvar org-last-inserted-timestamp)
  10724. (defvar org-log-post-message)
  10725. (defvar org-log-note-purpose)
  10726. (defvar org-log-note-how)
  10727. (defvar org-log-note-extra)
  10728. (defun org-auto-repeat-maybe (done-word)
  10729. "Check if the current headline contains a repeated deadline/schedule.
  10730. If yes, set TODO state back to what it was and change the base date
  10731. of repeating deadline/scheduled time stamps to new date.
  10732. This function is run automatically after each state change to a DONE state."
  10733. ;; last-state is dynamically scoped into this function
  10734. (let* ((repeat (org-get-repeat))
  10735. (aa (assoc last-state org-todo-kwd-alist))
  10736. (interpret (nth 1 aa))
  10737. (head (nth 2 aa))
  10738. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10739. (msg "Entry repeats: ")
  10740. (org-log-done nil)
  10741. (org-todo-log-states nil)
  10742. re type n what ts time to-state)
  10743. (when repeat
  10744. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10745. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10746. org-todo-repeat-to-state))
  10747. (unless (and to-state (member to-state org-todo-keywords-1))
  10748. (setq to-state (if (eq interpret 'type) last-state head)))
  10749. (org-todo to-state)
  10750. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10751. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10752. (org-time-stamp-format t t))))
  10753. (when org-log-repeat
  10754. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10755. (memq 'org-add-log-note post-command-hook))
  10756. ;; OK, we are already setup for some record
  10757. (if (eq org-log-repeat 'note)
  10758. ;; make sure we take a note, not only a time stamp
  10759. (setq org-log-note-how 'note))
  10760. ;; Set up for taking a record
  10761. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10762. last-state
  10763. 'findpos org-log-repeat)))
  10764. (org-back-to-heading t)
  10765. (org-add-planning-info nil nil 'closed)
  10766. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10767. org-deadline-time-regexp "\\)\\|\\("
  10768. org-ts-regexp "\\)"))
  10769. (while (re-search-forward
  10770. re (save-excursion (outline-next-heading) (point)) t)
  10771. (setq type (if (match-end 1) org-scheduled-string
  10772. (if (match-end 3) org-deadline-string "Plain:"))
  10773. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10774. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10775. (setq n (string-to-number (match-string 2 ts))
  10776. what (match-string 3 ts))
  10777. (if (equal what "w") (setq n (* n 7) what "d"))
  10778. ;; Preparation, see if we need to modify the start date for the change
  10779. (when (match-end 1)
  10780. (setq time (save-match-data (org-time-string-to-time ts)))
  10781. (cond
  10782. ((equal (match-string 1 ts) ".")
  10783. ;; Shift starting date to today
  10784. (org-timestamp-change
  10785. (- (org-today) (time-to-days time))
  10786. 'day))
  10787. ((equal (match-string 1 ts) "+")
  10788. (let ((nshiftmax 10) (nshift 0))
  10789. (while (or (= nshift 0)
  10790. (<= (time-to-days time)
  10791. (time-to-days (current-time))))
  10792. (when (= (incf nshift) nshiftmax)
  10793. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10794. (error "Abort")))
  10795. (org-timestamp-change n (cdr (assoc what whata)))
  10796. (org-at-timestamp-p t)
  10797. (setq ts (match-string 1))
  10798. (setq time (save-match-data (org-time-string-to-time ts)))))
  10799. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10800. ;; rematch, so that we have everything in place for the real shift
  10801. (org-at-timestamp-p t)
  10802. (setq ts (match-string 1))
  10803. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10804. (org-timestamp-change n (cdr (assoc what whata)))
  10805. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10806. (setq org-log-post-message msg)
  10807. (message "%s" msg))))
  10808. (defun org-show-todo-tree (arg)
  10809. "Make a compact tree which shows all headlines marked with TODO.
  10810. The tree will show the lines where the regexp matches, and all higher
  10811. headlines above the match.
  10812. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10813. With a numeric prefix N, construct a sparse tree for the Nth element
  10814. of `org-todo-keywords-1'."
  10815. (interactive "P")
  10816. (let ((case-fold-search nil)
  10817. (kwd-re
  10818. (cond ((null arg) org-not-done-regexp)
  10819. ((equal arg '(4))
  10820. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10821. (mapcar 'list org-todo-keywords-1))))
  10822. (concat "\\("
  10823. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10824. "\\)\\>")))
  10825. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10826. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10827. org-todo-keywords-1)))
  10828. (t (error "Invalid prefix argument: %s" arg)))))
  10829. (message "%d TODO entries found"
  10830. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  10831. (defun org-deadline (&optional remove time)
  10832. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10833. With argument REMOVE, remove any deadline from the item.
  10834. With argument TIME, set the deadline at the corresponding date. TIME
  10835. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10836. (interactive "P")
  10837. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10838. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10839. 'region-start-level 'region))
  10840. org-loop-over-headlines-in-active-region)
  10841. (org-map-entries
  10842. `(org-deadline ',remove ,time)
  10843. org-loop-over-headlines-in-active-region
  10844. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10845. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10846. (repeater (and old-date
  10847. (string-match
  10848. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10849. old-date)
  10850. (match-string 1 old-date))))
  10851. (if remove
  10852. (progn
  10853. (when (and old-date org-log-redeadline)
  10854. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10855. org-log-redeadline))
  10856. (org-remove-timestamp-with-keyword org-deadline-string)
  10857. (message "Item no longer has a deadline."))
  10858. (org-add-planning-info 'deadline time 'closed)
  10859. (when (and old-date org-log-redeadline
  10860. (not (equal old-date
  10861. (substring org-last-inserted-timestamp 1 -1))))
  10862. (org-add-log-setup 'redeadline nil old-date 'findpos
  10863. org-log-redeadline))
  10864. (when repeater
  10865. (save-excursion
  10866. (org-back-to-heading t)
  10867. (when (re-search-forward (concat org-deadline-string " "
  10868. org-last-inserted-timestamp)
  10869. (save-excursion
  10870. (outline-next-heading) (point)) t)
  10871. (goto-char (1- (match-end 0)))
  10872. (insert " " repeater)
  10873. (setq org-last-inserted-timestamp
  10874. (concat (substring org-last-inserted-timestamp 0 -1)
  10875. " " repeater
  10876. (substring org-last-inserted-timestamp -1))))))
  10877. (message "Deadline on %s" org-last-inserted-timestamp)))))
  10878. (defun org-schedule (&optional remove time)
  10879. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10880. With argument REMOVE, remove any scheduling date from the item.
  10881. With argument TIME, scheduled at the corresponding date. TIME can
  10882. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10883. (interactive "P")
  10884. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10885. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10886. 'region-start-level 'region))
  10887. org-loop-over-headlines-in-active-region)
  10888. (org-map-entries
  10889. `(org-schedule ',remove ,time)
  10890. org-loop-over-headlines-in-active-region
  10891. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10892. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10893. (repeater (and old-date
  10894. (string-match
  10895. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10896. old-date)
  10897. (match-string 1 old-date))))
  10898. (if remove
  10899. (progn
  10900. (when (and old-date org-log-reschedule)
  10901. (org-add-log-setup 'delschedule nil old-date 'findpos
  10902. org-log-reschedule))
  10903. (org-remove-timestamp-with-keyword org-scheduled-string)
  10904. (message "Item is no longer scheduled."))
  10905. (org-add-planning-info 'scheduled time 'closed)
  10906. (when (and old-date org-log-reschedule
  10907. (not (equal old-date
  10908. (substring org-last-inserted-timestamp 1 -1))))
  10909. (org-add-log-setup 'reschedule nil old-date 'findpos
  10910. org-log-reschedule))
  10911. (when repeater
  10912. (save-excursion
  10913. (org-back-to-heading t)
  10914. (when (re-search-forward (concat org-scheduled-string " "
  10915. org-last-inserted-timestamp)
  10916. (save-excursion
  10917. (outline-next-heading) (point)) t)
  10918. (goto-char (1- (match-end 0)))
  10919. (insert " " repeater)
  10920. (setq org-last-inserted-timestamp
  10921. (concat (substring org-last-inserted-timestamp 0 -1)
  10922. " " repeater
  10923. (substring org-last-inserted-timestamp -1))))))
  10924. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  10925. (defun org-get-scheduled-time (pom &optional inherit)
  10926. "Get the scheduled time as a time tuple, of a format suitable
  10927. for calling org-schedule with, or if there is no scheduling,
  10928. returns nil."
  10929. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10930. (when time
  10931. (apply 'encode-time (org-parse-time-string time)))))
  10932. (defun org-get-deadline-time (pom &optional inherit)
  10933. "Get the deadline as a time tuple, of a format suitable for
  10934. calling org-deadline with, or if there is no scheduling, returns
  10935. nil."
  10936. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10937. (when time
  10938. (apply 'encode-time (org-parse-time-string time)))))
  10939. (defun org-remove-timestamp-with-keyword (keyword)
  10940. "Remove all time stamps with KEYWORD in the current entry."
  10941. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10942. beg)
  10943. (save-excursion
  10944. (org-back-to-heading t)
  10945. (setq beg (point))
  10946. (outline-next-heading)
  10947. (while (re-search-backward re beg t)
  10948. (replace-match "")
  10949. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10950. (equal (char-before) ?\ ))
  10951. (backward-delete-char 1)
  10952. (if (string-match "^[ \t]*$" (buffer-substring
  10953. (point-at-bol) (point-at-eol)))
  10954. (delete-region (point-at-bol)
  10955. (min (point-max) (1+ (point-at-eol))))))))))
  10956. (defun org-add-planning-info (what &optional time &rest remove)
  10957. "Insert new timestamp with keyword in the line directly after the headline.
  10958. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  10959. If non is given, the user is prompted for a date.
  10960. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10961. be removed."
  10962. (interactive)
  10963. (let (org-time-was-given org-end-time-was-given ts
  10964. end default-time default-input)
  10965. (catch 'exit
  10966. (when (and (memq what '(scheduled deadline))
  10967. (or (not time)
  10968. (and (stringp time)
  10969. (string-match "^[-+]+[0-9]" time))))
  10970. ;; Try to get a default date/time from existing timestamp
  10971. (save-excursion
  10972. (org-back-to-heading t)
  10973. (setq end (save-excursion (outline-next-heading) (point)))
  10974. (when (re-search-forward (if (eq what 'scheduled)
  10975. org-scheduled-time-regexp
  10976. org-deadline-time-regexp)
  10977. end t)
  10978. (setq ts (match-string 1)
  10979. default-time
  10980. (apply 'encode-time (org-parse-time-string ts))
  10981. default-input (and ts (org-get-compact-tod ts))))))
  10982. (when what
  10983. (setq time
  10984. (if (stringp time)
  10985. ;; This is a string (relative or absolute), set proper date
  10986. (apply 'encode-time
  10987. (org-read-date-analyze
  10988. time default-time (decode-time default-time)))
  10989. ;; If necessary, get the time from the user
  10990. (or time (org-read-date nil 'to-time nil nil
  10991. default-time default-input)))))
  10992. (when (and org-insert-labeled-timestamps-at-point
  10993. (member what '(scheduled deadline)))
  10994. (insert
  10995. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10996. (org-insert-time-stamp time org-time-was-given
  10997. nil nil nil (list org-end-time-was-given))
  10998. (setq what nil))
  10999. (save-excursion
  11000. (save-restriction
  11001. (let (col list elt ts buffer-invisibility-spec)
  11002. (org-back-to-heading t)
  11003. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11004. (goto-char (match-end 1))
  11005. (setq col (current-column))
  11006. (goto-char (match-end 0))
  11007. (if (eobp) (insert "\n") (forward-char 1))
  11008. (when (and (not what)
  11009. (not (looking-at
  11010. (concat "[ \t]*"
  11011. org-keyword-time-not-clock-regexp))))
  11012. ;; Nothing to add, nothing to remove...... :-)
  11013. (throw 'exit nil))
  11014. (if (and (not (looking-at org-outline-regexp))
  11015. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11016. "[^\r\n]*"))
  11017. (not (equal (match-string 1) org-clock-string)))
  11018. (narrow-to-region (match-beginning 0) (match-end 0))
  11019. (insert-before-markers "\n")
  11020. (backward-char 1)
  11021. (narrow-to-region (point) (point))
  11022. (and org-adapt-indentation (org-indent-to-column col)))
  11023. ;; Check if we have to remove something.
  11024. (setq list (cons what remove))
  11025. (while list
  11026. (setq elt (pop list))
  11027. (when (or (and (eq elt 'scheduled)
  11028. (re-search-forward org-scheduled-time-regexp nil t))
  11029. (and (eq elt 'deadline)
  11030. (re-search-forward org-deadline-time-regexp nil t))
  11031. (and (eq elt 'closed)
  11032. (re-search-forward org-closed-time-regexp nil t)))
  11033. (replace-match "")
  11034. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11035. (and (looking-at "[ \t]+") (replace-match ""))
  11036. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11037. (when what
  11038. (insert
  11039. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11040. (cond ((eq what 'scheduled) org-scheduled-string)
  11041. ((eq what 'deadline) org-deadline-string)
  11042. ((eq what 'closed) org-closed-string))
  11043. " ")
  11044. (setq ts (org-insert-time-stamp
  11045. time
  11046. (or org-time-was-given
  11047. (and (eq what 'closed) org-log-done-with-time))
  11048. (eq what 'closed)
  11049. nil nil (list org-end-time-was-given)))
  11050. (insert
  11051. (if (not (or (bolp) (eq (char-before) ?\ )
  11052. (memq (char-after) '(32 10))
  11053. (eobp))) " " ""))
  11054. (end-of-line 1))
  11055. (goto-char (point-min))
  11056. (widen)
  11057. (if (and (looking-at "[ \t]*\n")
  11058. (equal (char-before) ?\n))
  11059. (delete-region (1- (point)) (point-at-eol)))
  11060. ts))))))
  11061. (defvar org-log-note-marker (make-marker))
  11062. (defvar org-log-note-purpose nil)
  11063. (defvar org-log-note-state nil)
  11064. (defvar org-log-note-previous-state nil)
  11065. (defvar org-log-note-how nil)
  11066. (defvar org-log-note-extra nil)
  11067. (defvar org-log-note-window-configuration nil)
  11068. (defvar org-log-note-return-to (make-marker))
  11069. (defvar org-log-note-effective-time nil
  11070. "Remembered current time so that dynamically scoped
  11071. `org-extend-today-until' affects tha timestamps in state change
  11072. log")
  11073. (defvar org-log-post-message nil
  11074. "Message to be displayed after a log note has been stored.
  11075. The auto-repeater uses this.")
  11076. (defun org-add-note ()
  11077. "Add a note to the current entry.
  11078. This is done in the same way as adding a state change note."
  11079. (interactive)
  11080. (org-add-log-setup 'note nil nil 'findpos nil))
  11081. (defvar org-property-end-re)
  11082. (defun org-add-log-setup (&optional purpose state prev-state
  11083. findpos how extra)
  11084. "Set up the post command hook to take a note.
  11085. If this is about to TODO state change, the new state is expected in STATE.
  11086. When FINDPOS is non-nil, find the correct position for the note in
  11087. the current entry. If not, assume that it can be inserted at point.
  11088. HOW is an indicator what kind of note should be created.
  11089. EXTRA is additional text that will be inserted into the notes buffer."
  11090. (let* ((org-log-into-drawer (org-log-into-drawer))
  11091. (drawer (cond ((stringp org-log-into-drawer)
  11092. org-log-into-drawer)
  11093. (org-log-into-drawer "LOGBOOK")
  11094. (t nil))))
  11095. (save-restriction
  11096. (save-excursion
  11097. (when findpos
  11098. (org-back-to-heading t)
  11099. (narrow-to-region (point) (save-excursion
  11100. (outline-next-heading) (point)))
  11101. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11102. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11103. "[^\r\n]*\\)?"))
  11104. (goto-char (match-end 0))
  11105. (cond
  11106. (drawer
  11107. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11108. nil t)
  11109. (progn
  11110. (goto-char (match-end 0))
  11111. (or org-log-states-order-reversed
  11112. (and (re-search-forward org-property-end-re nil t)
  11113. (goto-char (1- (match-beginning 0))))))
  11114. (insert "\n:" drawer ":\n:END:")
  11115. (beginning-of-line 0)
  11116. (org-indent-line-function)
  11117. (beginning-of-line 2)
  11118. (org-indent-line-function)
  11119. (end-of-line 0)))
  11120. ((and org-log-state-notes-insert-after-drawers
  11121. (save-excursion
  11122. (forward-line) (looking-at org-drawer-regexp)))
  11123. (forward-line)
  11124. (while (looking-at org-drawer-regexp)
  11125. (goto-char (match-end 0))
  11126. (re-search-forward org-property-end-re (point-max) t)
  11127. (forward-line))
  11128. (forward-line -1)))
  11129. (unless org-log-states-order-reversed
  11130. (and (= (char-after) ?\n) (forward-char 1))
  11131. (org-skip-over-state-notes)
  11132. (skip-chars-backward " \t\n\r")))
  11133. (move-marker org-log-note-marker (point))
  11134. (setq org-log-note-purpose purpose
  11135. org-log-note-state state
  11136. org-log-note-previous-state prev-state
  11137. org-log-note-how how
  11138. org-log-note-extra extra
  11139. org-log-note-effective-time (org-current-effective-time))
  11140. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11141. (defun org-skip-over-state-notes ()
  11142. "Skip past the list of State notes in an entry."
  11143. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11144. (when (ignore-errors (goto-char (org-in-item-p)))
  11145. (let* ((struct (org-list-struct))
  11146. (prevs (org-list-prevs-alist struct)))
  11147. (while (looking-at "[ \t]*- State")
  11148. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11149. (org-list-get-item-end (point) struct)))))))
  11150. (defun org-add-log-note (&optional purpose)
  11151. "Pop up a window for taking a note, and add this note later at point."
  11152. (remove-hook 'post-command-hook 'org-add-log-note)
  11153. (setq org-log-note-window-configuration (current-window-configuration))
  11154. (delete-other-windows)
  11155. (move-marker org-log-note-return-to (point))
  11156. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11157. (goto-char org-log-note-marker)
  11158. (org-switch-to-buffer-other-window "*Org Note*")
  11159. (erase-buffer)
  11160. (if (memq org-log-note-how '(time state))
  11161. (let (current-prefix-arg) (org-store-log-note))
  11162. (let ((org-inhibit-startup t)) (org-mode))
  11163. (insert (format "# Insert note for %s.
  11164. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11165. (cond
  11166. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11167. ((eq org-log-note-purpose 'done) "closed todo item")
  11168. ((eq org-log-note-purpose 'state)
  11169. (format "state change from \"%s\" to \"%s\""
  11170. (or org-log-note-previous-state "")
  11171. (or org-log-note-state "")))
  11172. ((eq org-log-note-purpose 'reschedule)
  11173. "rescheduling")
  11174. ((eq org-log-note-purpose 'delschedule)
  11175. "no longer scheduled")
  11176. ((eq org-log-note-purpose 'redeadline)
  11177. "changing deadline")
  11178. ((eq org-log-note-purpose 'deldeadline)
  11179. "removing deadline")
  11180. ((eq org-log-note-purpose 'refile)
  11181. "refiling")
  11182. ((eq org-log-note-purpose 'note)
  11183. "this entry")
  11184. (t (error "This should not happen")))))
  11185. (if org-log-note-extra (insert org-log-note-extra))
  11186. (org-set-local 'org-finish-function 'org-store-log-note)
  11187. (run-hooks 'org-log-buffer-setup-hook)))
  11188. (defvar org-note-abort nil) ; dynamically scoped
  11189. (defun org-store-log-note ()
  11190. "Finish taking a log note, and insert it to where it belongs."
  11191. (let ((txt (buffer-string))
  11192. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11193. lines ind bul)
  11194. (kill-buffer (current-buffer))
  11195. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  11196. (setq txt (replace-match "" t t txt)))
  11197. (if (string-match "\\s-+\\'" txt)
  11198. (setq txt (replace-match "" t t txt)))
  11199. (setq lines (org-split-string txt "\n"))
  11200. (when (and note (string-match "\\S-" note))
  11201. (setq note
  11202. (org-replace-escapes
  11203. note
  11204. (list (cons "%u" (user-login-name))
  11205. (cons "%U" user-full-name)
  11206. (cons "%t" (format-time-string
  11207. (org-time-stamp-format 'long 'inactive)
  11208. org-log-note-effective-time))
  11209. (cons "%T" (format-time-string
  11210. (org-time-stamp-format 'long nil)
  11211. org-log-note-effective-time))
  11212. (cons "%d" (format-time-string
  11213. (org-time-stamp-format nil 'inactive)
  11214. org-log-note-effective-time))
  11215. (cons "%D" (format-time-string
  11216. (org-time-stamp-format nil nil)
  11217. org-log-note-effective-time))
  11218. (cons "%s" (if org-log-note-state
  11219. (concat "\"" org-log-note-state "\"")
  11220. ""))
  11221. (cons "%S" (if org-log-note-previous-state
  11222. (concat "\"" org-log-note-previous-state "\"")
  11223. "\"\"")))))
  11224. (if lines (setq note (concat note " \\\\")))
  11225. (push note lines))
  11226. (when (or current-prefix-arg org-note-abort)
  11227. (when org-log-into-drawer
  11228. (org-remove-empty-drawer-at
  11229. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11230. org-log-note-marker))
  11231. (setq lines nil))
  11232. (when lines
  11233. (with-current-buffer (marker-buffer org-log-note-marker)
  11234. (save-excursion
  11235. (goto-char org-log-note-marker)
  11236. (move-marker org-log-note-marker nil)
  11237. (end-of-line 1)
  11238. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11239. (setq ind (save-excursion
  11240. (if (ignore-errors (goto-char (org-in-item-p)))
  11241. (let ((struct (org-list-struct)))
  11242. (org-list-get-ind
  11243. (org-list-get-top-point struct) struct))
  11244. (skip-chars-backward " \r\t\n")
  11245. (cond
  11246. ((and (org-at-heading-p)
  11247. org-adapt-indentation)
  11248. (1+ (org-current-level)))
  11249. ((org-at-heading-p) 0)
  11250. (t (org-get-indentation))))))
  11251. (setq bul (org-list-bullet-string "-"))
  11252. (org-indent-line-to ind)
  11253. (insert bul (pop lines))
  11254. (let ((ind-body (+ (length bul) ind)))
  11255. (while lines
  11256. (insert "\n")
  11257. (org-indent-line-to ind-body)
  11258. (insert (pop lines))))
  11259. (message "Note stored")
  11260. (org-back-to-heading t)
  11261. (org-cycle-hide-drawers 'children)))))
  11262. (set-window-configuration org-log-note-window-configuration)
  11263. (with-current-buffer (marker-buffer org-log-note-return-to)
  11264. (goto-char org-log-note-return-to))
  11265. (move-marker org-log-note-return-to nil)
  11266. (and org-log-post-message (message "%s" org-log-post-message)))
  11267. (defun org-remove-empty-drawer-at (drawer pos)
  11268. "Remove an empty drawer DRAWER at position POS.
  11269. POS may also be a marker."
  11270. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11271. (save-excursion
  11272. (save-restriction
  11273. (widen)
  11274. (goto-char pos)
  11275. (if (org-in-regexp
  11276. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11277. (replace-match ""))))))
  11278. (defun org-sparse-tree (&optional arg)
  11279. "Create a sparse tree, prompt for the details.
  11280. This command can create sparse trees. You first need to select the type
  11281. of match used to create the tree:
  11282. t Show all TODO entries.
  11283. T Show entries with a specific TODO keyword.
  11284. m Show entries selected by a tags/property match.
  11285. p Enter a property name and its value (both with completion on existing
  11286. names/values) and show entries with that property.
  11287. r Show entries matching a regular expression (`/' can be used as well)
  11288. d Show deadlines due within `org-deadline-warning-days'.
  11289. b Show deadlines and scheduled items before a date.
  11290. a Show deadlines and scheduled items after a date."
  11291. (interactive "P")
  11292. (let (ans kwd value)
  11293. (message "Sparse tree: [r]egexp [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date [D]ates range")
  11294. (setq ans (read-char-exclusive))
  11295. (cond
  11296. ((equal ans ?d)
  11297. (call-interactively 'org-check-deadlines))
  11298. ((equal ans ?b)
  11299. (call-interactively 'org-check-before-date))
  11300. ((equal ans ?a)
  11301. (call-interactively 'org-check-after-date))
  11302. ((equal ans ?D)
  11303. (call-interactively 'org-check-dates-range))
  11304. ((equal ans ?t)
  11305. (org-show-todo-tree nil))
  11306. ((equal ans ?T)
  11307. (org-show-todo-tree '(4)))
  11308. ((member ans '(?T ?m))
  11309. (call-interactively 'org-match-sparse-tree))
  11310. ((member ans '(?p ?P))
  11311. (setq kwd (org-icompleting-read "Property: "
  11312. (mapcar 'list (org-buffer-property-keys))))
  11313. (setq value (org-icompleting-read "Value: "
  11314. (mapcar 'list (org-property-values kwd))))
  11315. (unless (string-match "\\`{.*}\\'" value)
  11316. (setq value (concat "\"" value "\"")))
  11317. (org-match-sparse-tree arg (concat kwd "=" value)))
  11318. ((member ans '(?r ?R ?/))
  11319. (call-interactively 'org-occur))
  11320. (t (error "No such sparse tree command \"%c\"" ans)))))
  11321. (defvar org-occur-highlights nil
  11322. "List of overlays used for occur matches.")
  11323. (make-variable-buffer-local 'org-occur-highlights)
  11324. (defvar org-occur-parameters nil
  11325. "Parameters of the active org-occur calls.
  11326. This is a list, each call to org-occur pushes as cons cell,
  11327. containing the regular expression and the callback, onto the list.
  11328. The list can contain several entries if `org-occur' has been called
  11329. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11330. will only contain one set of parameters. When the highlights are
  11331. removed (for example with `C-c C-c', or with the next edit (depending
  11332. on `org-remove-highlights-with-change'), this variable is emptied
  11333. as well.")
  11334. (make-variable-buffer-local 'org-occur-parameters)
  11335. (defun org-occur (regexp &optional keep-previous callback)
  11336. "Make a compact tree which shows all matches of REGEXP.
  11337. The tree will show the lines where the regexp matches, and all higher
  11338. headlines above the match. It will also show the heading after the match,
  11339. to make sure editing the matching entry is easy.
  11340. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11341. call to `org-occur' will be kept, to allow stacking of calls to this
  11342. command.
  11343. If CALLBACK is non-nil, it is a function which is called to confirm
  11344. that the match should indeed be shown."
  11345. (interactive "sRegexp: \nP")
  11346. (when (equal regexp "")
  11347. (error "Regexp cannot be empty"))
  11348. (unless keep-previous
  11349. (org-remove-occur-highlights nil nil t))
  11350. (push (cons regexp callback) org-occur-parameters)
  11351. (let ((cnt 0))
  11352. (save-excursion
  11353. (goto-char (point-min))
  11354. (if (or (not keep-previous) ; do not want to keep
  11355. (not org-occur-highlights)) ; no previous matches
  11356. ;; hide everything
  11357. (org-overview))
  11358. (while (re-search-forward regexp nil t)
  11359. (when (or (not callback)
  11360. (save-match-data (funcall callback)))
  11361. (setq cnt (1+ cnt))
  11362. (when org-highlight-sparse-tree-matches
  11363. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11364. (org-show-context 'occur-tree))))
  11365. (when org-remove-highlights-with-change
  11366. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11367. nil 'local))
  11368. (unless org-sparse-tree-open-archived-trees
  11369. (org-hide-archived-subtrees (point-min) (point-max)))
  11370. (run-hooks 'org-occur-hook)
  11371. (if (org-called-interactively-p 'interactive)
  11372. (message "%d match(es) for regexp %s" cnt regexp))
  11373. cnt))
  11374. (defun org-occur-next-match (&optional n reset)
  11375. "Function for `next-error-function' to find sparse tree matches.
  11376. N is the number of matches to move, when negative move backwards.
  11377. RESET is entirely ignored - this function always goes back to the
  11378. starting point when no match is found."
  11379. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11380. (search-func (if (< n 0)
  11381. 'previous-single-char-property-change
  11382. 'next-single-char-property-change))
  11383. (n (abs n))
  11384. (pos (point))
  11385. p1)
  11386. (catch 'exit
  11387. (while (setq p1 (funcall search-func (point) 'org-type))
  11388. (when (equal p1 limit)
  11389. (goto-char pos)
  11390. (error "No more matches"))
  11391. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11392. (setq n (1- n))
  11393. (when (= n 0)
  11394. (goto-char p1)
  11395. (throw 'exit (point))))
  11396. (goto-char p1))
  11397. (goto-char p1)
  11398. (error "No more matches"))))
  11399. (defun org-show-context (&optional key)
  11400. "Make sure point and context are visible.
  11401. How much context is shown depends upon the variables
  11402. `org-show-hierarchy-above', `org-show-following-heading',
  11403. `org-show-entry-below' and `org-show-siblings'."
  11404. (let ((heading-p (org-at-heading-p t))
  11405. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11406. (following-p (org-get-alist-option org-show-following-heading key))
  11407. (entry-p (org-get-alist-option org-show-entry-below key))
  11408. (siblings-p (org-get-alist-option org-show-siblings key)))
  11409. (catch 'exit
  11410. ;; Show heading or entry text
  11411. (if (and heading-p (not entry-p))
  11412. (org-flag-heading nil) ; only show the heading
  11413. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11414. (org-show-hidden-entry))) ; show entire entry
  11415. (when following-p
  11416. ;; Show next sibling, or heading below text
  11417. (save-excursion
  11418. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11419. (org-flag-heading nil))))
  11420. (when siblings-p (org-show-siblings))
  11421. (when hierarchy-p
  11422. ;; show all higher headings, possibly with siblings
  11423. (save-excursion
  11424. (while (and (condition-case nil
  11425. (progn (org-up-heading-all 1) t)
  11426. (error nil))
  11427. (not (bobp)))
  11428. (org-flag-heading nil)
  11429. (when siblings-p (org-show-siblings))))))))
  11430. (defvar org-reveal-start-hook nil
  11431. "Hook run before revealing a location.")
  11432. (defun org-reveal (&optional siblings)
  11433. "Show current entry, hierarchy above it, and the following headline.
  11434. This can be used to show a consistent set of context around locations
  11435. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11436. not t for the search context.
  11437. With optional argument SIBLINGS, on each level of the hierarchy all
  11438. siblings are shown. This repairs the tree structure to what it would
  11439. look like when opened with hierarchical calls to `org-cycle'.
  11440. With double optional argument \\[universal-argument] \\[universal-argument], \
  11441. go to the parent and show the
  11442. entire tree."
  11443. (interactive "P")
  11444. (run-hooks 'org-reveal-start-hook)
  11445. (let ((org-show-hierarchy-above t)
  11446. (org-show-following-heading t)
  11447. (org-show-siblings (if siblings t org-show-siblings)))
  11448. (org-show-context nil))
  11449. (when (equal siblings '(16))
  11450. (save-excursion
  11451. (when (org-up-heading-safe)
  11452. (org-show-subtree)
  11453. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11454. (defun org-highlight-new-match (beg end)
  11455. "Highlight from BEG to END and mark the highlight is an occur headline."
  11456. (let ((ov (make-overlay beg end)))
  11457. (overlay-put ov 'face 'secondary-selection)
  11458. (overlay-put ov 'org-type 'org-occur)
  11459. (push ov org-occur-highlights)))
  11460. (defun org-remove-occur-highlights (&optional beg end noremove)
  11461. "Remove the occur highlights from the buffer.
  11462. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11463. from the `before-change-functions' in the current buffer."
  11464. (interactive)
  11465. (unless org-inhibit-highlight-removal
  11466. (mapc 'delete-overlay org-occur-highlights)
  11467. (setq org-occur-highlights nil)
  11468. (setq org-occur-parameters nil)
  11469. (unless noremove
  11470. (remove-hook 'before-change-functions
  11471. 'org-remove-occur-highlights 'local))))
  11472. ;;;; Priorities
  11473. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11474. "Regular expression matching the priority indicator.")
  11475. (defvar org-remove-priority-next-time nil)
  11476. (defun org-priority-up ()
  11477. "Increase the priority of the current item."
  11478. (interactive)
  11479. (org-priority 'up))
  11480. (defun org-priority-down ()
  11481. "Decrease the priority of the current item."
  11482. (interactive)
  11483. (org-priority 'down))
  11484. (defun org-priority (&optional action)
  11485. "Change the priority of an item by ARG.
  11486. ACTION can be `set', `up', `down', or a character."
  11487. (interactive)
  11488. (unless org-enable-priority-commands
  11489. (error "Priority commands are disabled"))
  11490. (setq action (or action 'set))
  11491. (let (current new news have remove)
  11492. (save-excursion
  11493. (org-back-to-heading t)
  11494. (if (looking-at org-priority-regexp)
  11495. (setq current (string-to-char (match-string 2))
  11496. have t))
  11497. (cond
  11498. ((eq action 'remove)
  11499. (setq remove t new ?\ ))
  11500. ((or (eq action 'set)
  11501. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11502. (if (not (eq action 'set))
  11503. (setq new action)
  11504. (message "Priority %c-%c, SPC to remove: "
  11505. org-highest-priority org-lowest-priority)
  11506. (save-match-data
  11507. (setq new (read-char-exclusive))))
  11508. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11509. (= (upcase org-lowest-priority) org-lowest-priority))
  11510. (setq new (upcase new)))
  11511. (cond ((equal new ?\ ) (setq remove t))
  11512. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11513. (error "Priority must be between `%c' and `%c'"
  11514. org-highest-priority org-lowest-priority))))
  11515. ((eq action 'up)
  11516. (setq new (if have
  11517. (1- current) ; normal cycling
  11518. ;; last priority was empty
  11519. (if (eq last-command this-command)
  11520. org-lowest-priority ; wrap around empty to lowest
  11521. ;; default
  11522. (if org-priority-start-cycle-with-default
  11523. org-default-priority
  11524. (1- org-default-priority))))))
  11525. ((eq action 'down)
  11526. (setq new (if have
  11527. (1+ current) ; normal cycling
  11528. ;; last priority was empty
  11529. (if (eq last-command this-command)
  11530. org-highest-priority ; wrap around empty to highest
  11531. ;; default
  11532. (if org-priority-start-cycle-with-default
  11533. org-default-priority
  11534. (1+ org-default-priority))))))
  11535. (t (error "Invalid action")))
  11536. (if (or (< (upcase new) org-highest-priority)
  11537. (> (upcase new) org-lowest-priority))
  11538. (if (and (memq action '(up down))
  11539. (not have) (not (eq last-command this-command)))
  11540. ;; `new' is from default priority
  11541. (error
  11542. "The default can not be set, see `org-default-priority' why")
  11543. ;; normal cycling: `new' is beyond highest/lowest priority
  11544. ;; and is wrapped around to the empty priority
  11545. (setq remove t)))
  11546. (setq news (format "%c" new))
  11547. (if have
  11548. (if remove
  11549. (replace-match "" t t nil 1)
  11550. (replace-match news t t nil 2))
  11551. (if remove
  11552. (error "No priority cookie found in line")
  11553. (let ((case-fold-search nil))
  11554. (looking-at org-todo-line-regexp))
  11555. (if (match-end 2)
  11556. (progn
  11557. (goto-char (match-end 2))
  11558. (insert " [#" news "]"))
  11559. (goto-char (match-beginning 3))
  11560. (insert "[#" news "] "))))
  11561. (org-preserve-lc (org-set-tags nil 'align)))
  11562. (if remove
  11563. (message "Priority removed")
  11564. (message "Priority of current item set to %s" news))))
  11565. (defun org-get-priority (s)
  11566. "Find priority cookie and return priority."
  11567. (if (functionp org-get-priority-function)
  11568. (funcall org-get-priority-function)
  11569. (save-match-data
  11570. (if (not (string-match org-priority-regexp s))
  11571. (* 1000 (- org-lowest-priority org-default-priority))
  11572. (* 1000 (- org-lowest-priority
  11573. (string-to-char (match-string 2 s))))))))
  11574. ;;;; Tags
  11575. (defvar org-agenda-archives-mode)
  11576. (defvar org-map-continue-from nil
  11577. "Position from where mapping should continue.
  11578. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  11579. (defvar org-scanner-tags nil
  11580. "The current tag list while the tags scanner is running.")
  11581. (defvar org-trust-scanner-tags nil
  11582. "Should `org-get-tags-at' use the tags for the scanner.
  11583. This is for internal dynamical scoping only.
  11584. When this is non-nil, the function `org-get-tags-at' will return the value
  11585. of `org-scanner-tags' instead of building the list by itself. This
  11586. can lead to large speed-ups when the tags scanner is used in a file with
  11587. many entries, and when the list of tags is retrieved, for example to
  11588. obtain a list of properties. Building the tags list for each entry in such
  11589. a file becomes an N^2 operation - but with this variable set, it scales
  11590. as N.")
  11591. (defun org-scan-tags (action matcher &optional todo-only start-level)
  11592. "Scan headline tags with inheritance and produce output ACTION.
  11593. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11594. or `agenda' to produce an entry list for an agenda view. It can also be
  11595. a Lisp form or a function that should be called at each matched headline, in
  11596. this case the return value is a list of all return values from these calls.
  11597. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11598. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11599. only lines with a TODO keyword are included in the output.
  11600. START-LEVEL can be a string with asterisks, reducing the scope to
  11601. headlines matching this string."
  11602. (require 'org-agenda)
  11603. (let* ((re (concat "^"
  11604. (if start-level
  11605. ;; Get the correct level to match
  11606. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  11607. org-outline-regexp)
  11608. " *\\(\\<\\("
  11609. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11610. (org-re
  11611. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11612. (props (list 'face 'default
  11613. 'done-face 'org-agenda-done
  11614. 'undone-face 'default
  11615. 'mouse-face 'highlight
  11616. 'org-not-done-regexp org-not-done-regexp
  11617. 'org-todo-regexp org-todo-regexp
  11618. 'org-complex-heading-regexp org-complex-heading-regexp
  11619. 'help-echo
  11620. (format "mouse-2 or RET jump to org file %s"
  11621. (abbreviate-file-name
  11622. (or (buffer-file-name (buffer-base-buffer))
  11623. (buffer-name (buffer-base-buffer)))))))
  11624. (case-fold-search nil)
  11625. (org-map-continue-from nil)
  11626. lspos tags tags-list
  11627. (tags-alist (list (cons 0 org-file-tags)))
  11628. (llast 0) rtn rtn1 level category i txt
  11629. todo marker entry priority)
  11630. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11631. (setq action (list 'lambda nil action)))
  11632. (save-excursion
  11633. (goto-char (point-min))
  11634. (when (eq action 'sparse-tree)
  11635. (org-overview)
  11636. (org-remove-occur-highlights))
  11637. (while (re-search-forward re nil t)
  11638. (setq org-map-continue-from nil)
  11639. (catch :skip
  11640. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11641. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11642. (goto-char (setq lspos (match-beginning 0)))
  11643. (setq level (org-reduced-level (funcall outline-level))
  11644. category (org-get-category))
  11645. (setq i llast llast level)
  11646. ;; remove tag lists from same and sublevels
  11647. (while (>= i level)
  11648. (when (setq entry (assoc i tags-alist))
  11649. (setq tags-alist (delete entry tags-alist)))
  11650. (setq i (1- i)))
  11651. ;; add the next tags
  11652. (when tags
  11653. (setq tags (org-split-string tags ":")
  11654. tags-alist
  11655. (cons (cons level tags) tags-alist)))
  11656. ;; compile tags for current headline
  11657. (setq tags-list
  11658. (if org-use-tag-inheritance
  11659. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11660. tags)
  11661. org-scanner-tags tags-list)
  11662. (when org-use-tag-inheritance
  11663. (setcdr (car tags-alist)
  11664. (mapcar (lambda (x)
  11665. (setq x (copy-sequence x))
  11666. (org-add-prop-inherited x))
  11667. (cdar tags-alist))))
  11668. (when (and tags org-use-tag-inheritance
  11669. (or (not (eq t org-use-tag-inheritance))
  11670. org-tags-exclude-from-inheritance))
  11671. ;; selective inheritance, remove uninherited ones
  11672. (setcdr (car tags-alist)
  11673. (org-remove-uninherited-tags (cdar tags-alist))))
  11674. (when (and
  11675. ;; eval matcher only when the todo condition is OK
  11676. (and (or (not todo-only) (member todo org-not-done-keywords))
  11677. (let ((case-fold-search t)) (eval matcher)))
  11678. ;; Call the skipper, but return t if it does not skip,
  11679. ;; so that the `and' form continues evaluating
  11680. (progn
  11681. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11682. t)
  11683. ;; Check if timestamps are deselecting this entry
  11684. (or (not todo-only)
  11685. (and (member todo org-not-done-keywords)
  11686. (or (not org-agenda-tags-todo-honor-ignore-options)
  11687. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11688. ;; Extra check for the archive tag
  11689. ;; FIXME: Does the skipper already do this????
  11690. (or
  11691. (not (member org-archive-tag tags-list))
  11692. ;; we have an archive tag, should we use this anyway?
  11693. (or (not org-agenda-skip-archived-trees)
  11694. (and (eq action 'agenda) org-agenda-archives-mode))))
  11695. ;; select this headline
  11696. (cond
  11697. ((eq action 'sparse-tree)
  11698. (and org-highlight-sparse-tree-matches
  11699. (org-get-heading) (match-end 0)
  11700. (org-highlight-new-match
  11701. (match-beginning 0) (match-beginning 1)))
  11702. (org-show-context 'tags-tree))
  11703. ((eq action 'agenda)
  11704. (setq txt (org-agenda-format-item
  11705. ""
  11706. (concat
  11707. (if (eq org-tags-match-list-sublevels 'indented)
  11708. (make-string (1- level) ?.) "")
  11709. (org-get-heading))
  11710. category
  11711. tags-list
  11712. )
  11713. priority (org-get-priority txt))
  11714. (goto-char lspos)
  11715. (setq marker (org-agenda-new-marker))
  11716. (org-add-props txt props
  11717. 'org-marker marker 'org-hd-marker marker 'org-category category
  11718. 'todo-state todo
  11719. 'priority priority 'type "tagsmatch")
  11720. (push txt rtn))
  11721. ((functionp action)
  11722. (setq org-map-continue-from nil)
  11723. (save-excursion
  11724. (setq rtn1 (funcall action))
  11725. (push rtn1 rtn)))
  11726. (t (error "Invalid action")))
  11727. ;; if we are to skip sublevels, jump to end of subtree
  11728. (unless org-tags-match-list-sublevels
  11729. (org-end-of-subtree t)
  11730. (backward-char 1))))
  11731. ;; Get the correct position from where to continue
  11732. (if org-map-continue-from
  11733. (goto-char org-map-continue-from)
  11734. (and (= (point) lspos) (end-of-line 1)))))
  11735. (when (and (eq action 'sparse-tree)
  11736. (not org-sparse-tree-open-archived-trees))
  11737. (org-hide-archived-subtrees (point-min) (point-max)))
  11738. (nreverse rtn)))
  11739. (defun org-remove-uninherited-tags (tags)
  11740. "Remove all tags that are not inherited from the list TAGS."
  11741. (cond
  11742. ((eq org-use-tag-inheritance t)
  11743. (if org-tags-exclude-from-inheritance
  11744. (org-delete-all org-tags-exclude-from-inheritance tags)
  11745. tags))
  11746. ((not org-use-tag-inheritance) nil)
  11747. ((stringp org-use-tag-inheritance)
  11748. (delq nil (mapcar
  11749. (lambda (x)
  11750. (if (and (string-match org-use-tag-inheritance x)
  11751. (not (member x org-tags-exclude-from-inheritance)))
  11752. x nil))
  11753. tags)))
  11754. ((listp org-use-tag-inheritance)
  11755. (delq nil (mapcar
  11756. (lambda (x)
  11757. (if (member x org-use-tag-inheritance) x nil))
  11758. tags)))))
  11759. (defvar todo-only) ;; dynamically scoped
  11760. (defun org-match-sparse-tree (&optional todo-only match)
  11761. "Create a sparse tree according to tags string MATCH.
  11762. MATCH can contain positive and negative selection of tags, like
  11763. \"+WORK+URGENT-WITHBOSS\".
  11764. If optional argument TODO-ONLY is non-nil, only select lines that are
  11765. also TODO lines."
  11766. (interactive "P")
  11767. (org-prepare-agenda-buffers (list (current-buffer)))
  11768. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11769. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11770. (defvar org-cached-props nil)
  11771. (defun org-cached-entry-get (pom property)
  11772. (if (or (eq t org-use-property-inheritance)
  11773. (and (stringp org-use-property-inheritance)
  11774. (string-match org-use-property-inheritance property))
  11775. (and (listp org-use-property-inheritance)
  11776. (member property org-use-property-inheritance)))
  11777. ;; Caching is not possible, check it directly
  11778. (org-entry-get pom property 'inherit)
  11779. ;; Get all properties, so that we can do complicated checks easily
  11780. (cdr (assoc property (or org-cached-props
  11781. (setq org-cached-props
  11782. (org-entry-properties pom)))))))
  11783. (defun org-global-tags-completion-table (&optional files)
  11784. "Return the list of all tags in all agenda buffer/files.
  11785. Optional FILES argument is a list of files to which can be used
  11786. instead of the agenda files."
  11787. (save-excursion
  11788. (org-uniquify
  11789. (delq nil
  11790. (apply 'append
  11791. (mapcar
  11792. (lambda (file)
  11793. (set-buffer (find-file-noselect file))
  11794. (append (org-get-buffer-tags)
  11795. (mapcar (lambda (x) (if (stringp (car-safe x))
  11796. (list (car-safe x)) nil))
  11797. org-tag-alist)))
  11798. (if (and files (car files))
  11799. files
  11800. (org-agenda-files))))))))
  11801. (defun org-make-tags-matcher (match)
  11802. "Create the TAGS/TODO matcher form for the selection string MATCH."
  11803. ;; todo-only is scoped dynamically into this function, and the function
  11804. ;; may change it if the matcher asks for it.
  11805. (unless match
  11806. ;; Get a new match request, with completion
  11807. (let ((org-last-tags-completion-table
  11808. (org-global-tags-completion-table)))
  11809. (setq match (org-completing-read-no-i
  11810. "Match: " 'org-tags-completion-function nil nil nil
  11811. 'org-tags-history))))
  11812. ;; Parse the string and create a lisp form
  11813. (let ((match0 match)
  11814. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11815. minus tag mm
  11816. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11817. orterms term orlist re-p str-p level-p level-op time-p
  11818. prop-p pn pv po gv rest)
  11819. (if (string-match "/+" match)
  11820. ;; match contains also a todo-matching request
  11821. (progn
  11822. (setq tagsmatch (substring match 0 (match-beginning 0))
  11823. todomatch (substring match (match-end 0)))
  11824. (if (string-match "^!" todomatch)
  11825. (setq todo-only t todomatch (substring todomatch 1)))
  11826. (if (string-match "^\\s-*$" todomatch)
  11827. (setq todomatch nil)))
  11828. ;; only matching tags
  11829. (setq tagsmatch match todomatch nil))
  11830. ;; Make the tags matcher
  11831. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11832. (setq tagsmatcher t)
  11833. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11834. (while (setq term (pop orterms))
  11835. (while (and (equal (substring term -1) "\\") orterms)
  11836. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11837. (while (string-match re term)
  11838. (setq rest (substring term (match-end 0))
  11839. minus (and (match-end 1)
  11840. (equal (match-string 1 term) "-"))
  11841. tag (save-match-data (replace-regexp-in-string
  11842. "\\\\-" "-"
  11843. (match-string 2 term)))
  11844. re-p (equal (string-to-char tag) ?{)
  11845. level-p (match-end 4)
  11846. prop-p (match-end 5)
  11847. mm (cond
  11848. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11849. (level-p
  11850. (setq level-op (org-op-to-function (match-string 3 term)))
  11851. `(,level-op level ,(string-to-number
  11852. (match-string 4 term))))
  11853. (prop-p
  11854. (setq pn (match-string 5 term)
  11855. po (match-string 6 term)
  11856. pv (match-string 7 term)
  11857. re-p (equal (string-to-char pv) ?{)
  11858. str-p (equal (string-to-char pv) ?\")
  11859. time-p (save-match-data
  11860. (string-match "^\"[[<].*[]>]\"$" pv))
  11861. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11862. (if time-p (setq pv (org-matcher-time pv)))
  11863. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11864. (cond
  11865. ((equal pn "CATEGORY")
  11866. (setq gv '(get-text-property (point) 'org-category)))
  11867. ((equal pn "TODO")
  11868. (setq gv 'todo))
  11869. (t
  11870. (setq gv `(org-cached-entry-get nil ,pn))))
  11871. (if re-p
  11872. (if (eq po 'org<>)
  11873. `(not (string-match ,pv (or ,gv "")))
  11874. `(string-match ,pv (or ,gv "")))
  11875. (if str-p
  11876. `(,po (or ,gv "") ,pv)
  11877. `(,po (string-to-number (or ,gv ""))
  11878. ,(string-to-number pv) ))))
  11879. (t `(member ,tag tags-list)))
  11880. mm (if minus (list 'not mm) mm)
  11881. term rest)
  11882. (push mm tagsmatcher))
  11883. (push (if (> (length tagsmatcher) 1)
  11884. (cons 'and tagsmatcher)
  11885. (car tagsmatcher))
  11886. orlist)
  11887. (setq tagsmatcher nil))
  11888. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11889. (setq tagsmatcher
  11890. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11891. ;; Make the todo matcher
  11892. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11893. (setq todomatcher t)
  11894. (setq orterms (org-split-string todomatch "|") orlist nil)
  11895. (while (setq term (pop orterms))
  11896. (while (string-match re term)
  11897. (setq minus (and (match-end 1)
  11898. (equal (match-string 1 term) "-"))
  11899. kwd (match-string 2 term)
  11900. re-p (equal (string-to-char kwd) ?{)
  11901. term (substring term (match-end 0))
  11902. mm (if re-p
  11903. `(string-match ,(substring kwd 1 -1) todo)
  11904. (list 'equal 'todo kwd))
  11905. mm (if minus (list 'not mm) mm))
  11906. (push mm todomatcher))
  11907. (push (if (> (length todomatcher) 1)
  11908. (cons 'and todomatcher)
  11909. (car todomatcher))
  11910. orlist)
  11911. (setq todomatcher nil))
  11912. (setq todomatcher (if (> (length orlist) 1)
  11913. (cons 'or orlist) (car orlist))))
  11914. ;; Return the string and lisp forms of the matcher
  11915. (setq matcher (if todomatcher
  11916. (list 'and tagsmatcher todomatcher)
  11917. tagsmatcher))
  11918. (cons match0 matcher)))
  11919. (defun org-op-to-function (op &optional stringp)
  11920. "Turn an operator into the appropriate function."
  11921. (setq op
  11922. (cond
  11923. ((equal op "<" ) '(< string< org-time<))
  11924. ((equal op ">" ) '(> org-string> org-time>))
  11925. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11926. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11927. ((member op '("=" "==")) '(= string= org-time=))
  11928. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11929. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11930. (defun org<> (a b) (not (= a b)))
  11931. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11932. (defun org-string>= (a b) (not (string< a b)))
  11933. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11934. (defun org-string<> (a b) (not (string= a b)))
  11935. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  11936. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  11937. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  11938. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  11939. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  11940. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  11941. (defun org-2ft (s)
  11942. "Convert S to a floating point time.
  11943. If S is already a number, just return it. If it is a string, parse
  11944. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11945. (cond
  11946. ((numberp s) s)
  11947. ((stringp s)
  11948. (condition-case nil
  11949. (float-time (apply 'encode-time (org-parse-time-string s)))
  11950. (error 0.)))
  11951. (t 0.)))
  11952. (defun org-time-today ()
  11953. "Time in seconds today at 0:00.
  11954. Returns the float number of seconds since the beginning of the
  11955. epoch to the beginning of today (00:00)."
  11956. (float-time (apply 'encode-time
  11957. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  11958. (defun org-matcher-time (s)
  11959. "Interpret a time comparison value."
  11960. (save-match-data
  11961. (cond
  11962. ((string= s "<now>") (float-time))
  11963. ((string= s "<today>") (org-time-today))
  11964. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  11965. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  11966. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  11967. (+ (org-time-today)
  11968. (* (string-to-number (match-string 1 s))
  11969. (cdr (assoc (match-string 2 s)
  11970. '(("d" . 86400.0) ("w" . 604800.0)
  11971. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  11972. (t (org-2ft s)))))
  11973. (defun org-match-any-p (re list)
  11974. "Does re match any element of list?"
  11975. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11976. (delq nil list))
  11977. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  11978. (defvar org-tags-overlay (make-overlay 1 1))
  11979. (org-detach-overlay org-tags-overlay)
  11980. (defun org-get-local-tags-at (&optional pos)
  11981. "Get a list of tags defined in the current headline."
  11982. (org-get-tags-at pos 'local))
  11983. (defun org-get-local-tags ()
  11984. "Get a list of tags defined in the current headline."
  11985. (org-get-tags-at nil 'local))
  11986. (defun org-get-tags-at (&optional pos local)
  11987. "Get a list of all headline tags applicable at POS.
  11988. POS defaults to point. If tags are inherited, the list contains
  11989. the targets in the same sequence as the headlines appear, i.e.
  11990. the tags of the current headline come last.
  11991. When LOCAL is non-nil, only return tags from the current headline,
  11992. ignore inherited ones."
  11993. (interactive)
  11994. (if (and org-trust-scanner-tags
  11995. (or (not pos) (equal pos (point)))
  11996. (not local))
  11997. org-scanner-tags
  11998. (let (tags ltags lastpos parent)
  11999. (save-excursion
  12000. (save-restriction
  12001. (widen)
  12002. (goto-char (or pos (point)))
  12003. (save-match-data
  12004. (catch 'done
  12005. (condition-case nil
  12006. (progn
  12007. (org-back-to-heading t)
  12008. (while (not (equal lastpos (point)))
  12009. (setq lastpos (point))
  12010. (when (looking-at
  12011. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12012. (setq ltags (org-split-string
  12013. (org-match-string-no-properties 1) ":"))
  12014. (when parent
  12015. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12016. (setq tags (append
  12017. (if parent
  12018. (org-remove-uninherited-tags ltags)
  12019. ltags)
  12020. tags)))
  12021. (or org-use-tag-inheritance (throw 'done t))
  12022. (if local (throw 'done t))
  12023. (or (org-up-heading-safe) (error nil))
  12024. (setq parent t)))
  12025. (error nil)))))
  12026. (if local
  12027. tags
  12028. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12029. (defun org-add-prop-inherited (s)
  12030. (add-text-properties 0 (length s) '(inherited t) s)
  12031. s)
  12032. (defun org-toggle-tag (tag &optional onoff)
  12033. "Toggle the tag TAG for the current line.
  12034. If ONOFF is `on' or `off', don't toggle but set to this state."
  12035. (let (res current)
  12036. (save-excursion
  12037. (org-back-to-heading t)
  12038. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12039. (point-at-eol) t)
  12040. (progn
  12041. (setq current (match-string 1))
  12042. (replace-match ""))
  12043. (setq current ""))
  12044. (setq current (nreverse (org-split-string current ":")))
  12045. (cond
  12046. ((eq onoff 'on)
  12047. (setq res t)
  12048. (or (member tag current) (push tag current)))
  12049. ((eq onoff 'off)
  12050. (or (not (member tag current)) (setq current (delete tag current))))
  12051. (t (if (member tag current)
  12052. (setq current (delete tag current))
  12053. (setq res t)
  12054. (push tag current))))
  12055. (end-of-line 1)
  12056. (if current
  12057. (progn
  12058. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12059. (org-set-tags nil t))
  12060. (delete-horizontal-space))
  12061. (run-hooks 'org-after-tags-change-hook))
  12062. res))
  12063. (defun org-align-tags-here (to-col)
  12064. ;; Assumes that this is a headline
  12065. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12066. (beginning-of-line 1)
  12067. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12068. (< pos (match-beginning 2)))
  12069. (progn
  12070. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12071. (goto-char (match-beginning 1))
  12072. (insert " ")
  12073. (delete-region (point) (1+ (match-beginning 2)))
  12074. (setq ncol (max (current-column)
  12075. (1+ col)
  12076. (if (> to-col 0)
  12077. to-col
  12078. (- (abs to-col) tags-l))))
  12079. (setq p (point))
  12080. (insert (make-string (- ncol (current-column)) ?\ ))
  12081. (setq ncol (current-column))
  12082. (when indent-tabs-mode (tabify p (point-at-eol)))
  12083. (org-move-to-column (min ncol col) t))
  12084. (goto-char pos))))
  12085. (defun org-set-tags-command (&optional arg just-align)
  12086. "Call the set-tags command for the current entry."
  12087. (interactive "P")
  12088. (if (org-at-heading-p)
  12089. (org-set-tags arg just-align)
  12090. (save-excursion
  12091. (org-back-to-heading t)
  12092. (org-set-tags arg just-align))))
  12093. (defun org-set-tags-to (data)
  12094. "Set the tags of the current entry to DATA, replacing the current tags.
  12095. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12096. If DATA is nil or the empty string, any tags will be removed."
  12097. (interactive "sTags: ")
  12098. (setq data
  12099. (cond
  12100. ((eq data nil) "")
  12101. ((equal data "") "")
  12102. ((stringp data)
  12103. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12104. ":"))
  12105. ((listp data)
  12106. (concat ":" (mapconcat 'identity data ":") ":"))
  12107. (t nil)))
  12108. (when data
  12109. (save-excursion
  12110. (org-back-to-heading t)
  12111. (when (looking-at org-complex-heading-regexp)
  12112. (if (match-end 5)
  12113. (progn
  12114. (goto-char (match-beginning 5))
  12115. (insert data)
  12116. (delete-region (point) (point-at-eol))
  12117. (org-set-tags nil 'align))
  12118. (goto-char (point-at-eol))
  12119. (insert " " data)
  12120. (org-set-tags nil 'align)))
  12121. (beginning-of-line 1)
  12122. (if (looking-at ".*?\\([ \t]+\\)$")
  12123. (delete-region (match-beginning 1) (match-end 1))))))
  12124. (defun org-align-all-tags ()
  12125. "Align the tags i all headings."
  12126. (interactive)
  12127. (save-excursion
  12128. (or (ignore-errors (org-back-to-heading t))
  12129. (outline-next-heading))
  12130. (if (org-at-heading-p)
  12131. (org-set-tags t)
  12132. (message "No headings"))))
  12133. (defvar org-indent-indentation-per-level)
  12134. (defun org-set-tags (&optional arg just-align)
  12135. "Set the tags for the current headline.
  12136. With prefix ARG, realign all tags in headings in the current buffer."
  12137. (interactive "P")
  12138. (let* ((re org-outline-regexp-bol)
  12139. (current (org-get-tags-string))
  12140. (col (current-column))
  12141. (org-setting-tags t)
  12142. table current-tags inherited-tags ; computed below when needed
  12143. tags p0 c0 c1 rpl di tc level)
  12144. (if arg
  12145. (save-excursion
  12146. (goto-char (point-min))
  12147. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12148. (while (re-search-forward re nil t)
  12149. (org-set-tags nil t)
  12150. (end-of-line 1)))
  12151. (message "All tags realigned to column %d" org-tags-column))
  12152. (if just-align
  12153. (setq tags current)
  12154. ;; Get a new set of tags from the user
  12155. (save-excursion
  12156. (setq table (append org-tag-persistent-alist
  12157. (or org-tag-alist (org-get-buffer-tags))
  12158. (and
  12159. org-complete-tags-always-offer-all-agenda-tags
  12160. (org-global-tags-completion-table
  12161. (org-agenda-files))))
  12162. org-last-tags-completion-table table
  12163. current-tags (org-split-string current ":")
  12164. inherited-tags (nreverse
  12165. (nthcdr (length current-tags)
  12166. (nreverse (org-get-tags-at))))
  12167. tags
  12168. (if (or (eq t org-use-fast-tag-selection)
  12169. (and org-use-fast-tag-selection
  12170. (delq nil (mapcar 'cdr table))))
  12171. (org-fast-tag-selection
  12172. current-tags inherited-tags table
  12173. (if org-fast-tag-selection-include-todo
  12174. org-todo-key-alist))
  12175. (let ((org-add-colon-after-tag-completion t))
  12176. (org-trim
  12177. (org-icompleting-read "Tags: "
  12178. 'org-tags-completion-function
  12179. nil nil current 'org-tags-history))))))
  12180. (while (string-match "[-+&]+" tags)
  12181. ;; No boolean logic, just a list
  12182. (setq tags (replace-match ":" t t tags))))
  12183. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12184. (if org-tags-sort-function
  12185. (setq tags (mapconcat 'identity
  12186. (sort (org-split-string
  12187. tags (org-re "[^[:alnum:]_@#%]+"))
  12188. org-tags-sort-function) ":")))
  12189. (if (string-match "\\`[\t ]*\\'" tags)
  12190. (setq tags "")
  12191. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12192. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12193. ;; Insert new tags at the correct column
  12194. (beginning-of-line 1)
  12195. (setq level (or (and (looking-at org-outline-regexp)
  12196. (- (match-end 0) (point) 1))
  12197. 1))
  12198. (cond
  12199. ((and (equal current "") (equal tags "")))
  12200. ((re-search-forward
  12201. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12202. (point-at-eol) t)
  12203. (if (equal tags "")
  12204. (setq rpl "")
  12205. (goto-char (match-beginning 0))
  12206. (setq c0 (current-column)
  12207. ;; compute offset for the case of org-indent-mode active
  12208. di (if org-indent-mode
  12209. (* (1- org-indent-indentation-per-level) (1- level))
  12210. 0)
  12211. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12212. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12213. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12214. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12215. (replace-match rpl t t)
  12216. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12217. tags)
  12218. (t (error "Tags alignment failed")))
  12219. (org-move-to-column col)
  12220. (unless just-align
  12221. (run-hooks 'org-after-tags-change-hook)))))
  12222. (defun org-change-tag-in-region (beg end tag off)
  12223. "Add or remove TAG for each entry in the region.
  12224. This works in the agenda, and also in an org-mode buffer."
  12225. (interactive
  12226. (list (region-beginning) (region-end)
  12227. (let ((org-last-tags-completion-table
  12228. (if (eq major-mode 'org-mode)
  12229. (org-get-buffer-tags)
  12230. (org-global-tags-completion-table))))
  12231. (org-icompleting-read
  12232. "Tag: " 'org-tags-completion-function nil nil nil
  12233. 'org-tags-history))
  12234. (progn
  12235. (message "[s]et or [r]emove? ")
  12236. (equal (read-char-exclusive) ?r))))
  12237. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12238. (let ((agendap (equal major-mode 'org-agenda-mode))
  12239. l1 l2 m buf pos newhead (cnt 0))
  12240. (goto-char end)
  12241. (setq l2 (1- (org-current-line)))
  12242. (goto-char beg)
  12243. (setq l1 (org-current-line))
  12244. (loop for l from l1 to l2 do
  12245. (org-goto-line l)
  12246. (setq m (get-text-property (point) 'org-hd-marker))
  12247. (when (or (and (eq major-mode 'org-mode) (org-at-heading-p))
  12248. (and agendap m))
  12249. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12250. pos (if agendap m (point)))
  12251. (with-current-buffer buf
  12252. (save-excursion
  12253. (save-restriction
  12254. (goto-char pos)
  12255. (setq cnt (1+ cnt))
  12256. (org-toggle-tag tag (if off 'off 'on))
  12257. (setq newhead (org-get-heading)))))
  12258. (and agendap (org-agenda-change-all-lines newhead m))))
  12259. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12260. (defun org-tags-completion-function (string predicate &optional flag)
  12261. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12262. (confirm (lambda (x) (stringp (car x)))))
  12263. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12264. (setq s1 (match-string 1 string)
  12265. s2 (match-string 2 string))
  12266. (setq s1 "" s2 string))
  12267. (cond
  12268. ((eq flag nil)
  12269. ;; try completion
  12270. (setq rtn (try-completion s2 ctable confirm))
  12271. (if (stringp rtn)
  12272. (setq rtn
  12273. (concat s1 s2 (substring rtn (length s2))
  12274. (if (and org-add-colon-after-tag-completion
  12275. (assoc rtn ctable))
  12276. ":" ""))))
  12277. rtn)
  12278. ((eq flag t)
  12279. ;; all-completions
  12280. (all-completions s2 ctable confirm)
  12281. )
  12282. ((eq flag 'lambda)
  12283. ;; exact match?
  12284. (assoc s2 ctable)))
  12285. ))
  12286. (defun org-fast-tag-insert (kwd tags face &optional end)
  12287. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12288. (insert (format "%-12s" (concat kwd ":"))
  12289. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12290. (or end "")))
  12291. (defun org-fast-tag-show-exit (flag)
  12292. (save-excursion
  12293. (org-goto-line 3)
  12294. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12295. (replace-match ""))
  12296. (when flag
  12297. (end-of-line 1)
  12298. (org-move-to-column (- (window-width) 19) t)
  12299. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12300. (defun org-set-current-tags-overlay (current prefix)
  12301. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12302. (if (featurep 'xemacs)
  12303. (org-overlay-display org-tags-overlay (concat prefix s)
  12304. 'secondary-selection)
  12305. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12306. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12307. (defvar org-last-tag-selection-key nil)
  12308. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12309. "Fast tag selection with single keys.
  12310. CURRENT is the current list of tags in the headline, INHERITED is the
  12311. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12312. possibly with grouping information. TODO-TABLE is a similar table with
  12313. TODO keywords, should these have keys assigned to them.
  12314. If the keys are nil, a-z are automatically assigned.
  12315. Returns the new tags string, or nil to not change the current settings."
  12316. (let* ((fulltable (append table todo-table))
  12317. (maxlen (apply 'max (mapcar
  12318. (lambda (x)
  12319. (if (stringp (car x)) (string-width (car x)) 0))
  12320. fulltable)))
  12321. (buf (current-buffer))
  12322. (expert (eq org-fast-tag-selection-single-key 'expert))
  12323. (buffer-tags nil)
  12324. (fwidth (+ maxlen 3 1 3))
  12325. (ncol (/ (- (window-width) 4) fwidth))
  12326. (i-face 'org-done)
  12327. (c-face 'org-todo)
  12328. tg cnt e c char c1 c2 ntable tbl rtn
  12329. ov-start ov-end ov-prefix
  12330. (exit-after-next org-fast-tag-selection-single-key)
  12331. (done-keywords org-done-keywords)
  12332. groups ingroup)
  12333. (save-excursion
  12334. (beginning-of-line 1)
  12335. (if (looking-at
  12336. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12337. (setq ov-start (match-beginning 1)
  12338. ov-end (match-end 1)
  12339. ov-prefix "")
  12340. (setq ov-start (1- (point-at-eol))
  12341. ov-end (1+ ov-start))
  12342. (skip-chars-forward "^\n\r")
  12343. (setq ov-prefix
  12344. (concat
  12345. (buffer-substring (1- (point)) (point))
  12346. (if (> (current-column) org-tags-column)
  12347. " "
  12348. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12349. (move-overlay org-tags-overlay ov-start ov-end)
  12350. (save-window-excursion
  12351. (if expert
  12352. (set-buffer (get-buffer-create " *Org tags*"))
  12353. (delete-other-windows)
  12354. (split-window-vertically)
  12355. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12356. (erase-buffer)
  12357. (org-set-local 'org-done-keywords done-keywords)
  12358. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12359. (org-fast-tag-insert "Current" current c-face "\n\n")
  12360. (org-fast-tag-show-exit exit-after-next)
  12361. (org-set-current-tags-overlay current ov-prefix)
  12362. (setq tbl fulltable char ?a cnt 0)
  12363. (while (setq e (pop tbl))
  12364. (cond
  12365. ((equal (car e) :startgroup)
  12366. (push '() groups) (setq ingroup t)
  12367. (when (not (= cnt 0))
  12368. (setq cnt 0)
  12369. (insert "\n"))
  12370. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12371. ((equal (car e) :endgroup)
  12372. (setq ingroup nil cnt 0)
  12373. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12374. ((equal e '(:newline))
  12375. (when (not (= cnt 0))
  12376. (setq cnt 0)
  12377. (insert "\n")
  12378. (setq e (car tbl))
  12379. (while (equal (car tbl) '(:newline))
  12380. (insert "\n")
  12381. (setq tbl (cdr tbl)))))
  12382. (t
  12383. (setq tg (copy-sequence (car e)) c2 nil)
  12384. (if (cdr e)
  12385. (setq c (cdr e))
  12386. ;; automatically assign a character.
  12387. (setq c1 (string-to-char
  12388. (downcase (substring
  12389. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12390. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12391. (while (or (rassoc char ntable) (rassoc char table))
  12392. (setq char (1+ char)))
  12393. (setq c2 c1))
  12394. (setq c (or c2 char)))
  12395. (if ingroup (push tg (car groups)))
  12396. (setq tg (org-add-props tg nil 'face
  12397. (cond
  12398. ((not (assoc tg table))
  12399. (org-get-todo-face tg))
  12400. ((member tg current) c-face)
  12401. ((member tg inherited) i-face)
  12402. (t nil))))
  12403. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12404. (insert "[" c "] " tg (make-string
  12405. (- fwidth 4 (length tg)) ?\ ))
  12406. (push (cons tg c) ntable)
  12407. (when (= (setq cnt (1+ cnt)) ncol)
  12408. (insert "\n")
  12409. (if ingroup (insert " "))
  12410. (setq cnt 0)))))
  12411. (setq ntable (nreverse ntable))
  12412. (insert "\n")
  12413. (goto-char (point-min))
  12414. (if (not expert) (org-fit-window-to-buffer))
  12415. (setq rtn
  12416. (catch 'exit
  12417. (while t
  12418. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12419. (if (not groups) "no " "")
  12420. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12421. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12422. (setq org-last-tag-selection-key c)
  12423. (cond
  12424. ((= c ?\r) (throw 'exit t))
  12425. ((= c ?!)
  12426. (setq groups (not groups))
  12427. (goto-char (point-min))
  12428. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12429. ((= c ?\C-c)
  12430. (if (not expert)
  12431. (org-fast-tag-show-exit
  12432. (setq exit-after-next (not exit-after-next)))
  12433. (setq expert nil)
  12434. (delete-other-windows)
  12435. (set-window-buffer (split-window-vertically) " *Org tags*")
  12436. (org-switch-to-buffer-other-window " *Org tags*")
  12437. (org-fit-window-to-buffer)))
  12438. ((or (= c ?\C-g)
  12439. (and (= c ?q) (not (rassoc c ntable))))
  12440. (org-detach-overlay org-tags-overlay)
  12441. (setq quit-flag t))
  12442. ((= c ?\ )
  12443. (setq current nil)
  12444. (if exit-after-next (setq exit-after-next 'now)))
  12445. ((= c ?\t)
  12446. (condition-case nil
  12447. (setq tg (org-icompleting-read
  12448. "Tag: "
  12449. (or buffer-tags
  12450. (with-current-buffer buf
  12451. (org-get-buffer-tags)))))
  12452. (quit (setq tg "")))
  12453. (when (string-match "\\S-" tg)
  12454. (add-to-list 'buffer-tags (list tg))
  12455. (if (member tg current)
  12456. (setq current (delete tg current))
  12457. (push tg current)))
  12458. (if exit-after-next (setq exit-after-next 'now)))
  12459. ((setq e (rassoc c todo-table) tg (car e))
  12460. (with-current-buffer buf
  12461. (save-excursion (org-todo tg)))
  12462. (if exit-after-next (setq exit-after-next 'now)))
  12463. ((setq e (rassoc c ntable) tg (car e))
  12464. (if (member tg current)
  12465. (setq current (delete tg current))
  12466. (loop for g in groups do
  12467. (if (member tg g)
  12468. (mapc (lambda (x)
  12469. (setq current (delete x current)))
  12470. g)))
  12471. (push tg current))
  12472. (if exit-after-next (setq exit-after-next 'now))))
  12473. ;; Create a sorted list
  12474. (setq current
  12475. (sort current
  12476. (lambda (a b)
  12477. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12478. (if (eq exit-after-next 'now) (throw 'exit t))
  12479. (goto-char (point-min))
  12480. (beginning-of-line 2)
  12481. (delete-region (point) (point-at-eol))
  12482. (org-fast-tag-insert "Current" current c-face)
  12483. (org-set-current-tags-overlay current ov-prefix)
  12484. (while (re-search-forward
  12485. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12486. (setq tg (match-string 1))
  12487. (add-text-properties
  12488. (match-beginning 1) (match-end 1)
  12489. (list 'face
  12490. (cond
  12491. ((member tg current) c-face)
  12492. ((member tg inherited) i-face)
  12493. (t (get-text-property (match-beginning 1) 'face))))))
  12494. (goto-char (point-min)))))
  12495. (org-detach-overlay org-tags-overlay)
  12496. (if rtn
  12497. (mapconcat 'identity current ":")
  12498. nil))))
  12499. (defun org-get-tags-string ()
  12500. "Get the TAGS string in the current headline."
  12501. (unless (org-at-heading-p t)
  12502. (error "Not on a heading"))
  12503. (save-excursion
  12504. (beginning-of-line 1)
  12505. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12506. (org-match-string-no-properties 1)
  12507. "")))
  12508. (defun org-get-tags ()
  12509. "Get the list of tags specified in the current headline."
  12510. (org-split-string (org-get-tags-string) ":"))
  12511. (defun org-get-buffer-tags ()
  12512. "Get a table of all tags used in the buffer, for completion."
  12513. (let (tags)
  12514. (save-excursion
  12515. (goto-char (point-min))
  12516. (while (re-search-forward
  12517. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12518. (when (equal (char-after (point-at-bol 0)) ?*)
  12519. (mapc (lambda (x) (add-to-list 'tags x))
  12520. (org-split-string (org-match-string-no-properties 1) ":")))))
  12521. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12522. (mapcar 'list tags)))
  12523. ;;;; The mapping API
  12524. ;;;###autoload
  12525. (defun org-map-entries (func &optional match scope &rest skip)
  12526. "Call FUNC at each headline selected by MATCH in SCOPE.
  12527. FUNC is a function or a lisp form. The function will be called without
  12528. arguments, with the cursor positioned at the beginning of the headline.
  12529. The return values of all calls to the function will be collected and
  12530. returned as a list.
  12531. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12532. does not need to preserve point. After evaluation, the cursor will be
  12533. moved to the end of the line (presumably of the headline of the
  12534. processed entry) and search continues from there. Under some
  12535. circumstances, this may not produce the wanted results. For example,
  12536. if you have removed (e.g. archived) the current (sub)tree it could
  12537. mean that the next entry will be skipped entirely. In such cases, you
  12538. can specify the position from where search should continue by making
  12539. FUNC set the variable `org-map-continue-from' to the desired buffer
  12540. position.
  12541. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12542. Only headlines that are matched by this query will be considered during
  12543. the iteration. When MATCH is nil or t, all headlines will be
  12544. visited by the iteration.
  12545. SCOPE determines the scope of this command. It can be any of:
  12546. nil The current buffer, respecting the restriction if any
  12547. tree The subtree started with the entry at point
  12548. region The entries within the active region, if any
  12549. region-start-level
  12550. The entries within the active region, but only those at
  12551. the same level than the first one.
  12552. file The current buffer, without restriction
  12553. file-with-archives
  12554. The current buffer, and any archives associated with it
  12555. agenda All agenda files
  12556. agenda-with-archives
  12557. All agenda files with any archive files associated with them
  12558. \(file1 file2 ...)
  12559. If this is a list, all files in the list will be scanned
  12560. The remaining args are treated as settings for the skipping facilities of
  12561. the scanner. The following items can be given here:
  12562. archive skip trees with the archive tag.
  12563. comment skip trees with the COMMENT keyword
  12564. function or Emacs Lisp form:
  12565. will be used as value for `org-agenda-skip-function', so whenever
  12566. the function returns t, FUNC will not be called for that
  12567. entry and search will continue from the point where the
  12568. function leaves it.
  12569. If your function needs to retrieve the tags including inherited tags
  12570. at the *current* entry, you can use the value of the variable
  12571. `org-scanner-tags' which will be much faster than getting the value
  12572. with `org-get-tags-at'. If your function gets properties with
  12573. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12574. to t around the call to `org-entry-properties' to get the same speedup.
  12575. Note that if your function moves around to retrieve tags and properties at
  12576. a *different* entry, you cannot use these techniques."
  12577. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  12578. (not (org-region-active-p)))
  12579. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12580. (org-agenda-skip-archived-trees (memq 'archive skip))
  12581. (org-agenda-skip-comment-trees (memq 'comment skip))
  12582. (org-agenda-skip-function
  12583. (car (org-delete-all '(comment archive) skip)))
  12584. (org-tags-match-list-sublevels t)
  12585. (start-level (eq scope 'region-start-level))
  12586. matcher file res
  12587. org-todo-keywords-for-agenda
  12588. org-done-keywords-for-agenda
  12589. org-todo-keyword-alist-for-agenda
  12590. org-drawers-for-agenda
  12591. org-tag-alist-for-agenda)
  12592. (cond
  12593. ((eq match t) (setq matcher t))
  12594. ((eq match nil) (setq matcher t))
  12595. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12596. (save-excursion
  12597. (save-restriction
  12598. (cond ((eq scope 'tree)
  12599. (org-back-to-heading t)
  12600. (org-narrow-to-subtree)
  12601. (setq scope nil))
  12602. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  12603. (org-region-active-p))
  12604. ;; If needed, set start-level to a string like "2"
  12605. (when start-level
  12606. (save-excursion
  12607. (goto-char (region-beginning))
  12608. (unless (org-at-heading-p) (outline-next-heading))
  12609. (setq start-level (org-current-level))))
  12610. (narrow-to-region (region-beginning)
  12611. (save-excursion
  12612. (goto-char (region-end))
  12613. (unless (and (bolp) (org-at-heading-p))
  12614. (outline-next-heading))
  12615. (point)))
  12616. (setq scope nil)))
  12617. (if (not scope)
  12618. (progn
  12619. (org-prepare-agenda-buffers
  12620. (list (buffer-file-name (current-buffer))))
  12621. (setq res (org-scan-tags func matcher nil start-level)))
  12622. ;; Get the right scope
  12623. (cond
  12624. ((and scope (listp scope) (symbolp (car scope)))
  12625. (setq scope (eval scope)))
  12626. ((eq scope 'agenda)
  12627. (setq scope (org-agenda-files t)))
  12628. ((eq scope 'agenda-with-archives)
  12629. (setq scope (org-agenda-files t))
  12630. (setq scope (org-add-archive-files scope)))
  12631. ((eq scope 'file)
  12632. (setq scope (list (buffer-file-name))))
  12633. ((eq scope 'file-with-archives)
  12634. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12635. (org-prepare-agenda-buffers scope)
  12636. (while (setq file (pop scope))
  12637. (with-current-buffer (org-find-base-buffer-visiting file)
  12638. (save-excursion
  12639. (save-restriction
  12640. (widen)
  12641. (goto-char (point-min))
  12642. (setq res (append res (org-scan-tags func matcher))))))))))
  12643. res)))
  12644. ;;;; Properties
  12645. ;;; Setting and retrieving properties
  12646. (defconst org-special-properties
  12647. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12648. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
  12649. "The special properties valid in Org-mode.
  12650. These are properties that are not defined in the property drawer,
  12651. but in some other way.")
  12652. (defconst org-default-properties
  12653. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12654. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12655. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12656. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12657. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12658. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12659. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12660. "Some properties that are used by Org-mode for various purposes.
  12661. Being in this list makes sure that they are offered for completion.")
  12662. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12663. "Regular expression matching the first line of a property drawer.")
  12664. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12665. "Regular expression matching the last line of a property drawer.")
  12666. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12667. "Regular expression matching the first line of a property drawer.")
  12668. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12669. "Regular expression matching the first line of a property drawer.")
  12670. (defconst org-property-drawer-re
  12671. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12672. org-property-end-re "\\)\n?")
  12673. "Matches an entire property drawer.")
  12674. (defconst org-clock-drawer-re
  12675. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12676. org-property-end-re "\\)\n?")
  12677. "Matches an entire clock drawer.")
  12678. (defsubst org-re-property (property)
  12679. "Return a regexp matching PROPERTY.
  12680. Match group 1 will be set to the value "
  12681. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12682. (defun org-property-action ()
  12683. "Do an action on properties."
  12684. (interactive)
  12685. (let (c)
  12686. (org-at-property-p)
  12687. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12688. (setq c (read-char-exclusive))
  12689. (cond
  12690. ((equal c ?s)
  12691. (call-interactively 'org-set-property))
  12692. ((equal c ?d)
  12693. (call-interactively 'org-delete-property))
  12694. ((equal c ?D)
  12695. (call-interactively 'org-delete-property-globally))
  12696. ((equal c ?c)
  12697. (call-interactively 'org-compute-property-at-point))
  12698. (t (error "No such property action %c" c)))))
  12699. (defun org-set-effort (&optional value)
  12700. "Set the effort property of the current entry.
  12701. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12702. allowed value."
  12703. (interactive "P")
  12704. (if (equal value 0) (setq value 10))
  12705. (let* ((completion-ignore-case t)
  12706. (prop org-effort-property)
  12707. (cur (org-entry-get nil prop))
  12708. (allowed (org-property-get-allowed-values nil prop 'table))
  12709. (existing (mapcar 'list (org-property-values prop)))
  12710. rpl
  12711. (val (cond
  12712. ((stringp value) value)
  12713. ((and allowed (integerp value))
  12714. (or (car (nth (1- value) allowed))
  12715. (car (org-last allowed))))
  12716. (allowed
  12717. (message "Select 1-9,0, [RET%s]: %s"
  12718. (if cur (concat "=" cur) "")
  12719. (mapconcat 'car allowed " "))
  12720. (setq rpl (read-char-exclusive))
  12721. (if (equal rpl ?\r)
  12722. cur
  12723. (setq rpl (- rpl ?0))
  12724. (if (equal rpl 0) (setq rpl 10))
  12725. (if (and (> rpl 0) (<= rpl (length allowed)))
  12726. (car (nth (1- rpl) allowed))
  12727. (org-completing-read "Effort: " allowed nil))))
  12728. (t
  12729. (let (org-completion-use-ido org-completion-use-iswitchb)
  12730. (org-completing-read
  12731. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12732. (concat "[" cur "]") "")
  12733. ": ")
  12734. existing nil nil "" nil cur))))))
  12735. (unless (equal (org-entry-get nil prop) val)
  12736. (org-entry-put nil prop val))
  12737. (message "%s is now %s" prop val)))
  12738. (defun org-at-property-p ()
  12739. "Is cursor inside a property drawer?"
  12740. (save-excursion
  12741. (beginning-of-line 1)
  12742. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12743. (save-match-data ;; Used by calling procedures
  12744. (let ((p (point))
  12745. (range (unless (org-before-first-heading-p)
  12746. (org-get-property-block))))
  12747. (and range (<= (car range) p) (< p (cdr range))))))))
  12748. (defun org-get-property-block (&optional beg end force)
  12749. "Return the (beg . end) range of the body of the property drawer.
  12750. BEG and END can be beginning and end of subtree, if not given
  12751. they will be found.
  12752. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12753. (catch 'exit
  12754. (save-excursion
  12755. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12756. (end (or end (progn (outline-next-heading) (point)))))
  12757. (goto-char beg)
  12758. (if (re-search-forward org-property-start-re end t)
  12759. (setq beg (1+ (match-end 0)))
  12760. (if force
  12761. (save-excursion
  12762. (org-insert-property-drawer)
  12763. (setq end (progn (outline-next-heading) (point))))
  12764. (throw 'exit nil))
  12765. (goto-char beg)
  12766. (if (re-search-forward org-property-start-re end t)
  12767. (setq beg (1+ (match-end 0)))))
  12768. (if (re-search-forward org-property-end-re end t)
  12769. (setq end (match-beginning 0))
  12770. (or force (throw 'exit nil))
  12771. (goto-char beg)
  12772. (setq end beg)
  12773. (org-indent-line-function)
  12774. (insert ":END:\n"))
  12775. (cons beg end)))))
  12776. (defun org-entry-properties (&optional pom which specific)
  12777. "Get all properties of the entry at point-or-marker POM.
  12778. This includes the TODO keyword, the tags, time strings for deadline,
  12779. scheduled, and clocking, and any additional properties defined in the
  12780. entry. The return value is an alist, keys may occur multiple times
  12781. if the property key was used several times.
  12782. POM may also be nil, in which case the current entry is used.
  12783. If WHICH is nil or `all', get all properties. If WHICH is
  12784. `special' or `standard', only get that subclass. If WHICH
  12785. is a string only get exactly this property. SPECIFIC can be a string, the
  12786. specific property we are interested in. Specifying it can speed
  12787. things up because then unnecessary parsing is avoided."
  12788. (setq which (or which 'all))
  12789. (org-with-point-at pom
  12790. (let ((clockstr (substring org-clock-string 0 -1))
  12791. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12792. (case-fold-search nil)
  12793. beg end range props sum-props key key1 value string clocksum)
  12794. (save-excursion
  12795. (when (condition-case nil
  12796. (and (eq major-mode 'org-mode) (org-back-to-heading t))
  12797. (error nil))
  12798. (setq beg (point))
  12799. (setq sum-props (get-text-property (point) 'org-summaries))
  12800. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12801. (outline-next-heading)
  12802. (setq end (point))
  12803. (when (memq which '(all special))
  12804. ;; Get the special properties, like TODO and tags
  12805. (goto-char beg)
  12806. (when (and (or (not specific) (string= specific "TODO"))
  12807. (looking-at org-todo-line-regexp) (match-end 2))
  12808. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12809. (when (and (or (not specific) (string= specific "PRIORITY"))
  12810. (looking-at org-priority-regexp))
  12811. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12812. (when (or (not specific) (string= specific "FILE"))
  12813. (push (cons "FILE" buffer-file-name) props))
  12814. (when (and (or (not specific) (string= specific "TAGS"))
  12815. (setq value (org-get-tags-string))
  12816. (string-match "\\S-" value))
  12817. (push (cons "TAGS" value) props))
  12818. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12819. (setq value (org-get-tags-at)))
  12820. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12821. ":"))
  12822. props))
  12823. (when (or (not specific) (string= specific "BLOCKED"))
  12824. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12825. (when (or (not specific)
  12826. (member specific
  12827. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12828. "TIMESTAMP" "TIMESTAMP_IA")))
  12829. (catch 'match
  12830. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12831. (setq key (if (match-end 1)
  12832. (substring (org-match-string-no-properties 1)
  12833. 0 -1))
  12834. string (if (equal key clockstr)
  12835. (org-no-properties
  12836. (org-trim
  12837. (buffer-substring
  12838. (match-beginning 3) (goto-char
  12839. (point-at-eol)))))
  12840. (substring (org-match-string-no-properties 3)
  12841. 1 -1)))
  12842. ;; Get the correct property name from the key. This is
  12843. ;; necessary if the user has configured time keywords.
  12844. (setq key1 (concat key ":"))
  12845. (cond
  12846. ((not key)
  12847. (setq key
  12848. (if (= (char-after (match-beginning 3)) ?\[)
  12849. "TIMESTAMP_IA" "TIMESTAMP")))
  12850. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12851. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12852. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12853. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12854. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  12855. (progn
  12856. (push (cons key string) props)
  12857. ;; no need to search further if match is found
  12858. (throw 'match t))
  12859. (when (or (equal key "CLOCK") (not (assoc key props)))
  12860. (push (cons key string) props))))))
  12861. )
  12862. (when (memq which '(all standard))
  12863. ;; Get the standard properties, like :PROP: ...
  12864. (setq range (org-get-property-block beg end))
  12865. (when range
  12866. (goto-char (car range))
  12867. (while (re-search-forward
  12868. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12869. (cdr range) t)
  12870. (setq key (org-match-string-no-properties 1)
  12871. value (org-trim (or (org-match-string-no-properties 2) "")))
  12872. (unless (member key excluded)
  12873. (push (cons key (or value "")) props)))))
  12874. (if clocksum
  12875. (push (cons "CLOCKSUM"
  12876. (org-columns-number-to-string (/ (float clocksum) 60.)
  12877. 'add_times))
  12878. props))
  12879. (unless (assoc "CATEGORY" props)
  12880. (push (cons "CATEGORY" (org-get-category)) props))
  12881. (append sum-props (nreverse props)))))))
  12882. (defun org-entry-get (pom property &optional inherit literal-nil)
  12883. "Get value of PROPERTY for entry at point-or-marker POM.
  12884. If INHERIT is non-nil and the entry does not have the property,
  12885. then also check higher levels of the hierarchy.
  12886. If INHERIT is the symbol `selective', use inheritance only if the setting
  12887. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12888. If the property is present but empty, the return value is the empty string.
  12889. If the property is not present at all, nil is returned.
  12890. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12891. do not interpret it as the list atom nil. This is used for inheritance
  12892. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12893. (org-with-point-at pom
  12894. (if (and inherit (if (eq inherit 'selective)
  12895. (org-property-inherit-p property)
  12896. t))
  12897. (org-entry-get-with-inheritance property literal-nil)
  12898. (if (member property org-special-properties)
  12899. ;; We need a special property. Use `org-entry-properties' to
  12900. ;; retrieve it, but specify the wanted property
  12901. (cdr (assoc property (org-entry-properties nil 'special property)))
  12902. (let ((range (unless (org-before-first-heading-p)
  12903. (org-get-property-block)))
  12904. (props (list (or (assoc property org-file-properties)
  12905. (assoc property org-global-properties)
  12906. (assoc property org-global-properties-fixed))))
  12907. val)
  12908. (flet ((ap (key)
  12909. (when (re-search-forward
  12910. (org-re-property key) (cdr range) t)
  12911. (setq props
  12912. (org-update-property-plist
  12913. key
  12914. (if (match-end 1)
  12915. (org-match-string-no-properties 1) "")
  12916. props)))))
  12917. (when (and range (goto-char (car range)))
  12918. (ap property)
  12919. (goto-char (car range))
  12920. (while (ap (concat property "+")))
  12921. (setq val (cdr (assoc property props)))
  12922. (when val (if literal-nil val (org-not-nil val))))))))))
  12923. (defun org-property-or-variable-value (var &optional inherit)
  12924. "Check if there is a property fixing the value of VAR.
  12925. If yes, return this value. If not, return the current value of the variable."
  12926. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12927. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12928. (read prop)
  12929. (symbol-value var))))
  12930. (defun org-entry-delete (pom property)
  12931. "Delete the property PROPERTY from entry at point-or-marker POM."
  12932. (org-with-point-at pom
  12933. (if (member property org-special-properties)
  12934. nil ; cannot delete these properties.
  12935. (let ((range (org-get-property-block)))
  12936. (if (and range
  12937. (goto-char (car range))
  12938. (re-search-forward
  12939. (org-re-property property)
  12940. (cdr range) t))
  12941. (progn
  12942. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12943. t)
  12944. nil)))))
  12945. ;; Multi-values properties are properties that contain multiple values
  12946. ;; These values are assumed to be single words, separated by whitespace.
  12947. (defun org-entry-add-to-multivalued-property (pom property value)
  12948. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  12949. (let* ((old (org-entry-get pom property))
  12950. (values (and old (org-split-string old "[ \t]"))))
  12951. (setq value (org-entry-protect-space value))
  12952. (unless (member value values)
  12953. (setq values (cons value values))
  12954. (org-entry-put pom property
  12955. (mapconcat 'identity values " ")))))
  12956. (defun org-entry-remove-from-multivalued-property (pom property value)
  12957. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  12958. (let* ((old (org-entry-get pom property))
  12959. (values (and old (org-split-string old "[ \t]"))))
  12960. (setq value (org-entry-protect-space value))
  12961. (when (member value values)
  12962. (setq values (delete value values))
  12963. (org-entry-put pom property
  12964. (mapconcat 'identity values " ")))))
  12965. (defun org-entry-member-in-multivalued-property (pom property value)
  12966. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  12967. (let* ((old (org-entry-get pom property))
  12968. (values (and old (org-split-string old "[ \t]"))))
  12969. (setq value (org-entry-protect-space value))
  12970. (member value values)))
  12971. (defun org-entry-get-multivalued-property (pom property)
  12972. "Return a list of values in a multivalued property."
  12973. (let* ((value (org-entry-get pom property))
  12974. (values (and value (org-split-string value "[ \t]"))))
  12975. (mapcar 'org-entry-restore-space values)))
  12976. (defun org-entry-put-multivalued-property (pom property &rest values)
  12977. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  12978. VALUES should be a list of strings. Spaces will be protected."
  12979. (org-entry-put pom property
  12980. (mapconcat 'org-entry-protect-space values " "))
  12981. (let* ((value (org-entry-get pom property))
  12982. (values (and value (org-split-string value "[ \t]"))))
  12983. (mapcar 'org-entry-restore-space values)))
  12984. (defun org-entry-protect-space (s)
  12985. "Protect spaces and newline in string S."
  12986. (while (string-match " " s)
  12987. (setq s (replace-match "%20" t t s)))
  12988. (while (string-match "\n" s)
  12989. (setq s (replace-match "%0A" t t s)))
  12990. s)
  12991. (defun org-entry-restore-space (s)
  12992. "Restore spaces and newline in string S."
  12993. (while (string-match "%20" s)
  12994. (setq s (replace-match " " t t s)))
  12995. (while (string-match "%0A" s)
  12996. (setq s (replace-match "\n" t t s)))
  12997. s)
  12998. (defvar org-entry-property-inherited-from (make-marker)
  12999. "Marker pointing to the entry from where a property was inherited.
  13000. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13001. location of the entry where the inheritance search matched. If there was
  13002. no match, the marker will point nowhere.
  13003. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13004. is set.")
  13005. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13006. "Get entry property, and search higher levels if not present.
  13007. The search will stop at the first ancestor which has the property defined.
  13008. If the value found is \"nil\", return nil to show that the property
  13009. should be considered as undefined (this is the meaning of nil here).
  13010. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13011. (move-marker org-entry-property-inherited-from nil)
  13012. (let (tmp)
  13013. (unless (org-before-first-heading-p)
  13014. (save-excursion
  13015. (save-restriction
  13016. (widen)
  13017. (catch 'ex
  13018. (while t
  13019. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13020. (org-back-to-heading t)
  13021. (move-marker org-entry-property-inherited-from (point))
  13022. (throw 'ex tmp))
  13023. (or (org-up-heading-safe) (throw 'ex nil)))))))
  13024. (setq tmp (or tmp
  13025. (cdr (assoc property org-file-properties))
  13026. (cdr (assoc property org-global-properties))
  13027. (cdr (assoc property org-global-properties-fixed))))
  13028. (if literal-nil tmp (org-not-nil tmp))))
  13029. (defvar org-property-changed-functions nil
  13030. "Hook called when the value of a property has changed.
  13031. Each hook function should accept two arguments, the name of the property
  13032. and the new value.")
  13033. (defun org-entry-put (pom property value)
  13034. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13035. (org-with-point-at pom
  13036. (org-back-to-heading t)
  13037. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13038. range)
  13039. (cond
  13040. ((equal property "TODO")
  13041. (when (and (stringp value) (string-match "\\S-" value)
  13042. (not (member value org-todo-keywords-1)))
  13043. (error "\"%s\" is not a valid TODO state" value))
  13044. (if (or (not value)
  13045. (not (string-match "\\S-" value)))
  13046. (setq value 'none))
  13047. (org-todo value)
  13048. (org-set-tags nil 'align))
  13049. ((equal property "PRIORITY")
  13050. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13051. (string-to-char value) ?\ ))
  13052. (org-set-tags nil 'align))
  13053. ((equal property "SCHEDULED")
  13054. (if (re-search-forward org-scheduled-time-regexp end t)
  13055. (cond
  13056. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13057. ((eq value 'later) (org-timestamp-change 1 'day))
  13058. (t (call-interactively 'org-schedule)))
  13059. (call-interactively 'org-schedule)))
  13060. ((equal property "DEADLINE")
  13061. (if (re-search-forward org-deadline-time-regexp end t)
  13062. (cond
  13063. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13064. ((eq value 'later) (org-timestamp-change 1 'day))
  13065. (t (call-interactively 'org-deadline)))
  13066. (call-interactively 'org-deadline)))
  13067. ((member property org-special-properties)
  13068. (error "The %s property can not yet be set with `org-entry-put'"
  13069. property))
  13070. (t ; a non-special property
  13071. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13072. (setq range (org-get-property-block beg end 'force))
  13073. (goto-char (car range))
  13074. (if (re-search-forward
  13075. (org-re-property property) (cdr range) t)
  13076. (progn
  13077. (delete-region (match-beginning 0) (match-end 0))
  13078. (goto-char (match-beginning 0)))
  13079. (goto-char (cdr range))
  13080. (insert "\n")
  13081. (backward-char 1)
  13082. (org-indent-line-function))
  13083. (insert ":" property ":")
  13084. (and value (insert " " value))
  13085. (org-indent-line-function)))))
  13086. (run-hook-with-args 'org-property-changed-functions property value)))
  13087. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13088. "Get all property keys in the current buffer.
  13089. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13090. like tags and TODO state.
  13091. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13092. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13093. and others.
  13094. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13095. formats in the current buffer."
  13096. (let (rtn range cfmt s p)
  13097. (save-excursion
  13098. (save-restriction
  13099. (widen)
  13100. (goto-char (point-min))
  13101. (while (re-search-forward org-property-start-re nil t)
  13102. (setq range (org-get-property-block))
  13103. (goto-char (car range))
  13104. (while (re-search-forward
  13105. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13106. (cdr range) t)
  13107. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13108. (outline-next-heading))))
  13109. (when include-specials
  13110. (setq rtn (append org-special-properties rtn)))
  13111. (when include-defaults
  13112. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13113. (add-to-list 'rtn org-effort-property))
  13114. (when include-columns
  13115. (save-excursion
  13116. (save-restriction
  13117. (widen)
  13118. (goto-char (point-min))
  13119. (while (re-search-forward
  13120. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13121. nil t)
  13122. (setq cfmt (match-string 2) s 0)
  13123. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13124. cfmt s)
  13125. (setq s (match-end 0)
  13126. p (match-string 1 cfmt))
  13127. (unless (or (equal p "ITEM")
  13128. (member p org-special-properties))
  13129. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13130. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13131. (defun org-property-values (key)
  13132. "Return a list of all values of property KEY in the current buffer."
  13133. (save-excursion
  13134. (save-restriction
  13135. (widen)
  13136. (goto-char (point-min))
  13137. (let ((re (org-re-property key))
  13138. values)
  13139. (while (re-search-forward re nil t)
  13140. (add-to-list 'values (org-trim (match-string 1))))
  13141. (delete "" values)))))
  13142. (defun org-insert-property-drawer ()
  13143. "Insert a property drawer into the current entry."
  13144. (interactive)
  13145. (org-back-to-heading t)
  13146. (looking-at org-outline-regexp)
  13147. (let ((indent (if org-adapt-indentation
  13148. (- (match-end 0)(match-beginning 0))
  13149. 0))
  13150. (beg (point))
  13151. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13152. end hiddenp)
  13153. (outline-next-heading)
  13154. (setq end (point))
  13155. (goto-char beg)
  13156. (while (re-search-forward re end t))
  13157. (setq hiddenp (outline-invisible-p))
  13158. (end-of-line 1)
  13159. (and (equal (char-after) ?\n) (forward-char 1))
  13160. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13161. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13162. ;; just skip this line
  13163. (beginning-of-line 2)
  13164. ;; Drawer start, find the end
  13165. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13166. (beginning-of-line 1)))
  13167. (org-skip-over-state-notes)
  13168. (skip-chars-backward " \t\n\r")
  13169. (if (eq (char-before) ?*) (forward-char 1))
  13170. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13171. (beginning-of-line 0)
  13172. (org-indent-to-column indent)
  13173. (beginning-of-line 2)
  13174. (org-indent-to-column indent)
  13175. (beginning-of-line 0)
  13176. (if hiddenp
  13177. (save-excursion
  13178. (org-back-to-heading t)
  13179. (hide-entry))
  13180. (org-flag-drawer t))))
  13181. (defvar org-property-set-functions-alist nil
  13182. "Property set function alist.
  13183. Each entry should have the following format:
  13184. (PROPERTY . READ-FUNCTION)
  13185. The read function will be called with the same argument as
  13186. `org-completing-read'.")
  13187. (defun org-set-property-function (property)
  13188. "Get the function that should be used to set PROPERTY.
  13189. This is computed according to `org-property-set-functions-alist'."
  13190. (or (cdr (assoc property org-property-set-functions-alist))
  13191. 'org-completing-read))
  13192. (defun org-read-property-value (property)
  13193. "Read PROPERTY value from user."
  13194. (let* ((completion-ignore-case t)
  13195. (allowed (org-property-get-allowed-values nil property 'table))
  13196. (cur (org-entry-get nil property))
  13197. (prompt (concat property " value"
  13198. (if (and cur (string-match "\\S-" cur))
  13199. (concat " [" cur "]") "") ": "))
  13200. (set-function (org-set-property-function property))
  13201. (val (if allowed
  13202. (funcall set-function prompt allowed nil
  13203. (not (get-text-property 0 'org-unrestricted
  13204. (caar allowed))))
  13205. (let (org-completion-use-ido org-completion-use-iswitchb)
  13206. (funcall set-function prompt
  13207. (mapcar 'list (org-property-values property))
  13208. nil nil "" nil cur)))))
  13209. (if (equal val "")
  13210. cur
  13211. val)))
  13212. (defvar org-last-set-property nil)
  13213. (defun org-read-property-name ()
  13214. "Read a property name."
  13215. (let* ((completion-ignore-case t)
  13216. (keys (org-buffer-property-keys nil t t))
  13217. (default-prop (or (save-excursion
  13218. (save-match-data
  13219. (beginning-of-line)
  13220. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13221. (null (string= (match-string 1) "END"))
  13222. (match-string 1))))
  13223. org-last-set-property))
  13224. (property (org-icompleting-read
  13225. (concat "Property"
  13226. (if default-prop (concat " [" default-prop "]") "")
  13227. ": ")
  13228. (mapcar 'list keys)
  13229. nil nil nil nil
  13230. default-prop
  13231. )))
  13232. (if (member property keys)
  13233. property
  13234. (or (cdr (assoc (downcase property)
  13235. (mapcar (lambda (x) (cons (downcase x) x))
  13236. keys)))
  13237. property))))
  13238. (defun org-set-property (property value)
  13239. "In the current entry, set PROPERTY to VALUE.
  13240. When called interactively, this will prompt for a property name, offering
  13241. completion on existing and default properties. And then it will prompt
  13242. for a value, offering completion either on allowed values (via an inherited
  13243. xxx_ALL property) or on existing values in other instances of this property
  13244. in the current file."
  13245. (interactive (list nil nil))
  13246. (let* ((property (or property (org-read-property-name)))
  13247. (value (or value (org-read-property-value property)))
  13248. (fn (assoc property org-properties-postprocess-alist)))
  13249. (setq org-last-set-property property)
  13250. ;; Possibly postprocess the inserted value:
  13251. (when fn (setq value (funcall (cadr fn) value)))
  13252. (unless (equal (org-entry-get nil property) value)
  13253. (org-entry-put nil property value))))
  13254. (defun org-delete-property (property)
  13255. "In the current entry, delete PROPERTY."
  13256. (interactive
  13257. (let* ((completion-ignore-case t)
  13258. (prop (org-icompleting-read "Property: "
  13259. (org-entry-properties nil 'standard))))
  13260. (list prop)))
  13261. (message "Property %s %s" property
  13262. (if (org-entry-delete nil property)
  13263. "deleted"
  13264. "was not present in the entry")))
  13265. (defun org-delete-property-globally (property)
  13266. "Remove PROPERTY globally, from all entries."
  13267. (interactive
  13268. (let* ((completion-ignore-case t)
  13269. (prop (org-icompleting-read
  13270. "Globally remove property: "
  13271. (mapcar 'list (org-buffer-property-keys)))))
  13272. (list prop)))
  13273. (save-excursion
  13274. (save-restriction
  13275. (widen)
  13276. (goto-char (point-min))
  13277. (let ((cnt 0))
  13278. (while (re-search-forward
  13279. (org-re-property property)
  13280. nil t)
  13281. (setq cnt (1+ cnt))
  13282. (replace-match ""))
  13283. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13284. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13285. (defun org-compute-property-at-point ()
  13286. "Compute the property at point.
  13287. This looks for an enclosing column format, extracts the operator and
  13288. then applies it to the property in the column format's scope."
  13289. (interactive)
  13290. (unless (org-at-property-p)
  13291. (error "Not at a property"))
  13292. (let ((prop (org-match-string-no-properties 2)))
  13293. (org-columns-get-format-and-top-level)
  13294. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13295. (error "No operator defined for property %s" prop))
  13296. (org-columns-compute prop)))
  13297. (defvar org-property-allowed-value-functions nil
  13298. "Hook for functions supplying allowed values for a specific property.
  13299. The functions must take a single argument, the name of the property, and
  13300. return a flat list of allowed values. If \":ETC\" is one of
  13301. the values, this means that these values are intended as defaults for
  13302. completion, but that other values should be allowed too.
  13303. The functions must return nil if they are not responsible for this
  13304. property.")
  13305. (defun org-property-get-allowed-values (pom property &optional table)
  13306. "Get allowed values for the property PROPERTY.
  13307. When TABLE is non-nil, return an alist that can directly be used for
  13308. completion."
  13309. (let (vals)
  13310. (cond
  13311. ((equal property "TODO")
  13312. (setq vals (org-with-point-at pom
  13313. (append org-todo-keywords-1 '("")))))
  13314. ((equal property "PRIORITY")
  13315. (let ((n org-lowest-priority))
  13316. (while (>= n org-highest-priority)
  13317. (push (char-to-string n) vals)
  13318. (setq n (1- n)))))
  13319. ((member property org-special-properties))
  13320. ((setq vals (run-hook-with-args-until-success
  13321. 'org-property-allowed-value-functions property)))
  13322. (t
  13323. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13324. (when (and vals (string-match "\\S-" vals))
  13325. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13326. (setq vals (mapcar (lambda (x)
  13327. (cond ((stringp x) x)
  13328. ((numberp x) (number-to-string x))
  13329. ((symbolp x) (symbol-name x))
  13330. (t "???")))
  13331. vals)))))
  13332. (when (member ":ETC" vals)
  13333. (setq vals (remove ":ETC" vals))
  13334. (org-add-props (car vals) '(org-unrestricted t)))
  13335. (if table (mapcar 'list vals) vals)))
  13336. (defun org-property-previous-allowed-value (&optional previous)
  13337. "Switch to the next allowed value for this property."
  13338. (interactive)
  13339. (org-property-next-allowed-value t))
  13340. (defun org-property-next-allowed-value (&optional previous)
  13341. "Switch to the next allowed value for this property."
  13342. (interactive)
  13343. (unless (org-at-property-p)
  13344. (error "Not at a property"))
  13345. (let* ((key (match-string 2))
  13346. (value (match-string 3))
  13347. (allowed (or (org-property-get-allowed-values (point) key)
  13348. (and (member value '("[ ]" "[-]" "[X]"))
  13349. '("[ ]" "[X]"))))
  13350. nval)
  13351. (unless allowed
  13352. (error "Allowed values for this property have not been defined"))
  13353. (if previous (setq allowed (reverse allowed)))
  13354. (if (member value allowed)
  13355. (setq nval (car (cdr (member value allowed)))))
  13356. (setq nval (or nval (car allowed)))
  13357. (if (equal nval value)
  13358. (error "Only one allowed value for this property"))
  13359. (org-at-property-p)
  13360. (replace-match (concat " :" key ": " nval) t t)
  13361. (org-indent-line-function)
  13362. (beginning-of-line 1)
  13363. (skip-chars-forward " \t")
  13364. (run-hook-with-args 'org-property-changed-functions key nval)))
  13365. (defun org-find-olp (path &optional this-buffer)
  13366. "Return a marker pointing to the entry at outline path OLP.
  13367. If anything goes wrong, throw an error.
  13368. You can wrap this call to catch the error like this:
  13369. (condition-case msg
  13370. (org-mobile-locate-entry (match-string 4))
  13371. (error (nth 1 msg)))
  13372. The return value will then be either a string with the error message,
  13373. or a marker if everything is OK.
  13374. If THIS-BUFFER is set, the outline path does not contain a file,
  13375. only headings."
  13376. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13377. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13378. (level 1)
  13379. (lmin 1)
  13380. (lmax 1)
  13381. limit re end found pos heading cnt flevel)
  13382. (unless buffer (error "File not found :%s" file))
  13383. (with-current-buffer buffer
  13384. (save-excursion
  13385. (save-restriction
  13386. (widen)
  13387. (setq limit (point-max))
  13388. (goto-char (point-min))
  13389. (while (setq heading (pop path))
  13390. (setq re (format org-complex-heading-regexp-format
  13391. (regexp-quote heading)))
  13392. (setq cnt 0 pos (point))
  13393. (while (re-search-forward re end t)
  13394. (setq level (- (match-end 1) (match-beginning 1)))
  13395. (if (and (>= level lmin) (<= level lmax))
  13396. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13397. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13398. lmax heading))
  13399. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13400. lmax heading))
  13401. (goto-char found)
  13402. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13403. (setq end (save-excursion (org-end-of-subtree t t))))
  13404. (when (org-at-heading-p)
  13405. (move-marker (make-marker) (point))))))))
  13406. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13407. "Find node HEADING in BUFFER.
  13408. Return a marker to the heading if it was found, or nil if not.
  13409. If POS-ONLY is set, return just the position instead of a marker.
  13410. The heading text must match exact, but it may have a TODO keyword,
  13411. a priority cookie and tags in the standard locations."
  13412. (with-current-buffer (or buffer (current-buffer))
  13413. (save-excursion
  13414. (save-restriction
  13415. (widen)
  13416. (goto-char (point-min))
  13417. (let (case-fold-search)
  13418. (if (re-search-forward
  13419. (format org-complex-heading-regexp-format
  13420. (regexp-quote heading)) nil t)
  13421. (if pos-only
  13422. (match-beginning 0)
  13423. (move-marker (make-marker) (match-beginning 0)))))))))
  13424. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13425. "Find Org node headline HEADING in all .org files in directory DIR.
  13426. When the target headline is found, return a marker to this location."
  13427. (let ((files (directory-files (or dir default-directory)
  13428. nil "\\`[^.#].*\\.org\\'"))
  13429. file visiting m buffer)
  13430. (catch 'found
  13431. (while (setq file (pop files))
  13432. (message "trying %s" file)
  13433. (setq visiting (org-find-base-buffer-visiting file))
  13434. (setq buffer (or visiting (find-file-noselect file)))
  13435. (setq m (org-find-exact-headline-in-buffer
  13436. heading buffer))
  13437. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13438. (and m (throw 'found m))))))
  13439. (defun org-find-entry-with-id (ident)
  13440. "Locate the entry that contains the ID property with exact value IDENT.
  13441. IDENT can be a string, a symbol or a number, this function will search for
  13442. the string representation of it.
  13443. Return the position where this entry starts, or nil if there is no such entry."
  13444. (interactive "sID: ")
  13445. (let ((id (cond
  13446. ((stringp ident) ident)
  13447. ((symbol-name ident) (symbol-name ident))
  13448. ((numberp ident) (number-to-string ident))
  13449. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13450. (case-fold-search nil))
  13451. (save-excursion
  13452. (save-restriction
  13453. (widen)
  13454. (goto-char (point-min))
  13455. (when (re-search-forward
  13456. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13457. nil t)
  13458. (org-back-to-heading t)
  13459. (point))))))
  13460. ;;;; Timestamps
  13461. (defvar org-last-changed-timestamp nil)
  13462. (defvar org-last-inserted-timestamp nil
  13463. "The last time stamp inserted with `org-insert-time-stamp'.")
  13464. (defvar org-time-was-given) ; dynamically scoped parameter
  13465. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13466. (defvar org-ts-what) ; dynamically scoped parameter
  13467. (defun org-time-stamp (arg &optional inactive)
  13468. "Prompt for a date/time and insert a time stamp.
  13469. If the user specifies a time like HH:MM, or if this command is called
  13470. with a prefix argument, the time stamp will contain date and time.
  13471. Otherwise, only the date will be included. All parts of a date not
  13472. specified by the user will be filled in from the current date/time.
  13473. So if you press just return without typing anything, the time stamp
  13474. will represent the current date/time. If there is already a timestamp
  13475. at the cursor, it will be modified."
  13476. (interactive "P")
  13477. (let* ((ts nil)
  13478. (default-time
  13479. ;; Default time is either today, or, when entering a range,
  13480. ;; the range start.
  13481. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13482. (save-excursion
  13483. (re-search-backward
  13484. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13485. (- (point) 20) t)))
  13486. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13487. (current-time)))
  13488. (default-input (and ts (org-get-compact-tod ts)))
  13489. (repeater (save-excursion
  13490. (save-match-data
  13491. (beginning-of-line)
  13492. (when (re-search-forward
  13493. "\\([.+-]+[0-9]+[dwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  13494. (save-excursion (progn (end-of-line) (point))) t)
  13495. (match-string 0)))))
  13496. org-time-was-given org-end-time-was-given time)
  13497. (cond
  13498. ((and (org-at-timestamp-p t)
  13499. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13500. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13501. (insert "--")
  13502. (setq time (let ((this-command this-command))
  13503. (org-read-date arg 'totime nil nil
  13504. default-time default-input)))
  13505. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13506. ((org-at-timestamp-p t)
  13507. (setq time (let ((this-command this-command))
  13508. (org-read-date arg 'totime nil nil default-time default-input)))
  13509. (when (org-at-timestamp-p t) ; just to get the match data
  13510. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13511. (replace-match "")
  13512. (setq org-last-changed-timestamp
  13513. (org-insert-time-stamp
  13514. time (or org-time-was-given arg)
  13515. inactive nil nil (list org-end-time-was-given)))
  13516. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13517. (setq org-last-changed-timestamp
  13518. (concat (substring org-last-inserted-timestamp 0 -1)
  13519. " " repeater ">"))))
  13520. (message "Timestamp updated"))
  13521. (t
  13522. (setq time (let ((this-command this-command))
  13523. (org-read-date arg 'totime nil nil default-time default-input)))
  13524. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13525. nil nil (list org-end-time-was-given))))))
  13526. ;; FIXME: can we use this for something else, like computing time differences?
  13527. (defun org-get-compact-tod (s)
  13528. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13529. (let* ((t1 (match-string 1 s))
  13530. (h1 (string-to-number (match-string 2 s)))
  13531. (m1 (string-to-number (match-string 3 s)))
  13532. (t2 (and (match-end 4) (match-string 5 s)))
  13533. (h2 (and t2 (string-to-number (match-string 6 s))))
  13534. (m2 (and t2 (string-to-number (match-string 7 s))))
  13535. dh dm)
  13536. (if (not t2)
  13537. t1
  13538. (setq dh (- h2 h1) dm (- m2 m1))
  13539. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13540. (concat t1 "+" (number-to-string dh)
  13541. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13542. (defun org-time-stamp-inactive (&optional arg)
  13543. "Insert an inactive time stamp.
  13544. An inactive time stamp is enclosed in square brackets instead of angle
  13545. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13546. does not link to the calendar and cannot be changed with the S-cursor keys.
  13547. So these are more for recording a certain time/date."
  13548. (interactive "P")
  13549. (org-time-stamp arg 'inactive))
  13550. (defvar org-date-ovl (make-overlay 1 1))
  13551. (overlay-put org-date-ovl 'face 'org-warning)
  13552. (org-detach-overlay org-date-ovl)
  13553. (defvar org-ans1) ; dynamically scoped parameter
  13554. (defvar org-ans2) ; dynamically scoped parameter
  13555. (defvar org-plain-time-of-day-regexp) ; defined below
  13556. (defvar org-overriding-default-time nil) ; dynamically scoped
  13557. (defvar org-read-date-overlay nil)
  13558. (defvar org-dcst nil) ; dynamically scoped
  13559. (defvar org-read-date-history nil)
  13560. (defvar org-read-date-final-answer nil)
  13561. (defvar org-read-date-analyze-futurep nil)
  13562. (defvar org-read-date-analyze-forced-year nil)
  13563. (defun org-read-date (&optional with-time to-time from-string prompt
  13564. default-time default-input)
  13565. "Read a date, possibly a time, and make things smooth for the user.
  13566. The prompt will suggest to enter an ISO date, but you can also enter anything
  13567. which will at least partially be understood by `parse-time-string'.
  13568. Unrecognized parts of the date will default to the current day, month, year,
  13569. hour and minute. If this command is called to replace a timestamp at point,
  13570. or to enter the second timestamp of a range, the default time is taken
  13571. from the existing stamp. Furthermore, the command prefers the future,
  13572. so if you are giving a date where the year is not given, and the day-month
  13573. combination is already past in the current year, it will assume you
  13574. mean next year. For details, see the manual. A few examples:
  13575. 3-2-5 --> 2003-02-05
  13576. feb 15 --> currentyear-02-15
  13577. 2/15 --> currentyear-02-15
  13578. sep 12 9 --> 2009-09-12
  13579. 12:45 --> today 12:45
  13580. 22 sept 0:34 --> currentyear-09-22 0:34
  13581. 12 --> currentyear-currentmonth-12
  13582. Fri --> nearest Friday (today or later)
  13583. etc.
  13584. Furthermore you can specify a relative date by giving, as the *first* thing
  13585. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  13586. change in days weeks, months, years.
  13587. With a single plus or minus, the date is relative to today. With a double
  13588. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13589. +4d --> four days from today
  13590. +4 --> same as above
  13591. +2w --> two weeks from today
  13592. ++5 --> five days from default date
  13593. The function understands only English month and weekday abbreviations,
  13594. but this can be configured with the variables `parse-time-months' and
  13595. `parse-time-weekdays'.
  13596. While prompting, a calendar is popped up - you can also select the
  13597. date with the mouse (button 1). The calendar shows a period of three
  13598. months. To scroll it to other months, use the keys `>' and `<'.
  13599. If you don't like the calendar, turn it off with
  13600. \(setq org-read-date-popup-calendar nil)
  13601. With optional argument TO-TIME, the date will immediately be converted
  13602. to an internal time.
  13603. With an optional argument WITH-TIME, the prompt will suggest to also
  13604. insert a time. Note that when WITH-TIME is not set, you can still
  13605. enter a time, and this function will inform the calling routine about
  13606. this change. The calling routine may then choose to change the format
  13607. used to insert the time stamp into the buffer to include the time.
  13608. With optional argument FROM-STRING, read from this string instead from
  13609. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13610. the time/date that is used for everything that is not specified by the
  13611. user."
  13612. (require 'parse-time)
  13613. (let* ((org-time-stamp-rounding-minutes
  13614. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13615. (org-dcst org-display-custom-times)
  13616. (ct (org-current-time))
  13617. (def (or org-overriding-default-time default-time ct))
  13618. (defdecode (decode-time def))
  13619. (dummy (progn
  13620. (when (< (nth 2 defdecode) org-extend-today-until)
  13621. (setcar (nthcdr 2 defdecode) -1)
  13622. (setcar (nthcdr 1 defdecode) 59)
  13623. (setq def (apply 'encode-time defdecode)
  13624. defdecode (decode-time def)))))
  13625. (calendar-frame-setup nil)
  13626. (calendar-setup nil)
  13627. (calendar-move-hook nil)
  13628. (calendar-view-diary-initially-flag nil)
  13629. (calendar-view-holidays-initially-flag nil)
  13630. (timestr (format-time-string
  13631. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  13632. (prompt (concat (if prompt (concat prompt " ") "")
  13633. (format "Date+time [%s]: " timestr)))
  13634. ans (org-ans0 "") org-ans1 org-ans2 final)
  13635. (cond
  13636. (from-string (setq ans from-string))
  13637. (org-read-date-popup-calendar
  13638. (save-excursion
  13639. (save-window-excursion
  13640. (calendar)
  13641. (unwind-protect
  13642. (progn
  13643. (calendar-forward-day (- (time-to-days def)
  13644. (calendar-absolute-from-gregorian
  13645. (calendar-current-date))))
  13646. (org-eval-in-calendar nil t)
  13647. (let* ((old-map (current-local-map))
  13648. (map (copy-keymap calendar-mode-map))
  13649. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13650. (org-defkey map (kbd "RET") 'org-calendar-select)
  13651. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13652. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13653. (org-defkey minibuffer-local-map [(meta shift left)]
  13654. (lambda () (interactive)
  13655. (org-eval-in-calendar '(calendar-backward-month 1))))
  13656. (org-defkey minibuffer-local-map [(meta shift right)]
  13657. (lambda () (interactive)
  13658. (org-eval-in-calendar '(calendar-forward-month 1))))
  13659. (org-defkey minibuffer-local-map [(meta shift up)]
  13660. (lambda () (interactive)
  13661. (org-eval-in-calendar '(calendar-backward-year 1))))
  13662. (org-defkey minibuffer-local-map [(meta shift down)]
  13663. (lambda () (interactive)
  13664. (org-eval-in-calendar '(calendar-forward-year 1))))
  13665. (org-defkey minibuffer-local-map [?\e (shift left)]
  13666. (lambda () (interactive)
  13667. (org-eval-in-calendar '(calendar-backward-month 1))))
  13668. (org-defkey minibuffer-local-map [?\e (shift right)]
  13669. (lambda () (interactive)
  13670. (org-eval-in-calendar '(calendar-forward-month 1))))
  13671. (org-defkey minibuffer-local-map [?\e (shift up)]
  13672. (lambda () (interactive)
  13673. (org-eval-in-calendar '(calendar-backward-year 1))))
  13674. (org-defkey minibuffer-local-map [?\e (shift down)]
  13675. (lambda () (interactive)
  13676. (org-eval-in-calendar '(calendar-forward-year 1))))
  13677. (org-defkey minibuffer-local-map [(shift up)]
  13678. (lambda () (interactive)
  13679. (org-eval-in-calendar '(calendar-backward-week 1))))
  13680. (org-defkey minibuffer-local-map [(shift down)]
  13681. (lambda () (interactive)
  13682. (org-eval-in-calendar '(calendar-forward-week 1))))
  13683. (org-defkey minibuffer-local-map [(shift left)]
  13684. (lambda () (interactive)
  13685. (org-eval-in-calendar '(calendar-backward-day 1))))
  13686. (org-defkey minibuffer-local-map [(shift right)]
  13687. (lambda () (interactive)
  13688. (org-eval-in-calendar '(calendar-forward-day 1))))
  13689. (org-defkey minibuffer-local-map ">"
  13690. (lambda () (interactive)
  13691. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13692. (org-defkey minibuffer-local-map "<"
  13693. (lambda () (interactive)
  13694. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13695. (org-defkey minibuffer-local-map "\C-v"
  13696. (lambda () (interactive)
  13697. (org-eval-in-calendar
  13698. '(calendar-scroll-left-three-months 1))))
  13699. (org-defkey minibuffer-local-map "\M-v"
  13700. (lambda () (interactive)
  13701. (org-eval-in-calendar
  13702. '(calendar-scroll-right-three-months 1))))
  13703. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13704. (unwind-protect
  13705. (progn
  13706. (use-local-map map)
  13707. (add-hook 'post-command-hook 'org-read-date-display)
  13708. (setq org-ans0 (read-string prompt default-input
  13709. 'org-read-date-history nil))
  13710. ;; org-ans0: from prompt
  13711. ;; org-ans1: from mouse click
  13712. ;; org-ans2: from calendar motion
  13713. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13714. (remove-hook 'post-command-hook 'org-read-date-display)
  13715. (use-local-map old-map)
  13716. (when org-read-date-overlay
  13717. (delete-overlay org-read-date-overlay)
  13718. (setq org-read-date-overlay nil)))))
  13719. (bury-buffer "*Calendar*")))))
  13720. (t ; Naked prompt only
  13721. (unwind-protect
  13722. (setq ans (read-string prompt default-input
  13723. 'org-read-date-history timestr))
  13724. (when org-read-date-overlay
  13725. (delete-overlay org-read-date-overlay)
  13726. (setq org-read-date-overlay nil)))))
  13727. (setq final (org-read-date-analyze ans def defdecode))
  13728. (when org-read-date-analyze-forced-year
  13729. (message "Year was forced into %s"
  13730. (if org-read-date-force-compatible-dates
  13731. "compatible range (1970-2037)"
  13732. "range representable on this machine"))
  13733. (ding))
  13734. ;; One round trip to get rid of 34th of August and stuff like that....
  13735. (setq final (decode-time (apply 'encode-time final)))
  13736. (setq org-read-date-final-answer ans)
  13737. (if to-time
  13738. (apply 'encode-time final)
  13739. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13740. (format "%04d-%02d-%02d %02d:%02d"
  13741. (nth 5 final) (nth 4 final) (nth 3 final)
  13742. (nth 2 final) (nth 1 final))
  13743. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13744. (defvar def)
  13745. (defvar defdecode)
  13746. (defvar with-time)
  13747. (defun org-read-date-display ()
  13748. "Display the current date prompt interpretation in the minibuffer."
  13749. (when org-read-date-display-live
  13750. (when org-read-date-overlay
  13751. (delete-overlay org-read-date-overlay))
  13752. (let ((p (point)))
  13753. (end-of-line 1)
  13754. (while (not (equal (buffer-substring
  13755. (max (point-min) (- (point) 4)) (point))
  13756. " "))
  13757. (insert " "))
  13758. (goto-char p))
  13759. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13760. " " (or org-ans1 org-ans2)))
  13761. (org-end-time-was-given nil)
  13762. (f (org-read-date-analyze ans def defdecode))
  13763. (fmts (if org-dcst
  13764. org-time-stamp-custom-formats
  13765. org-time-stamp-formats))
  13766. (fmt (if (or with-time
  13767. (and (boundp 'org-time-was-given) org-time-was-given))
  13768. (cdr fmts)
  13769. (car fmts)))
  13770. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  13771. (when (and org-end-time-was-given
  13772. (string-match org-plain-time-of-day-regexp txt))
  13773. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13774. org-end-time-was-given
  13775. (substring txt (match-end 0)))))
  13776. (when org-read-date-analyze-futurep
  13777. (setq txt (concat txt " (=>F)")))
  13778. (setq org-read-date-overlay
  13779. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13780. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13781. (defun org-read-date-analyze (ans def defdecode)
  13782. "Analyze the combined answer of the date prompt."
  13783. ;; FIXME: cleanup and comment
  13784. (let ((nowdecode (decode-time (current-time)))
  13785. delta deltan deltaw deltadef year month day
  13786. hour minute second wday pm h2 m2 tl wday1
  13787. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13788. (setq org-read-date-analyze-futurep nil
  13789. org-read-date-analyze-forced-year nil)
  13790. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13791. (setq ans "+0"))
  13792. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13793. (setq ans (replace-match "" t t ans)
  13794. deltan (car delta)
  13795. deltaw (nth 1 delta)
  13796. deltadef (nth 2 delta)))
  13797. ;; Check if there is an iso week date in there
  13798. ;; If yes, store the info and postpone interpreting it until the rest
  13799. ;; of the parsing is done
  13800. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13801. (setq iso-year (if (match-end 1)
  13802. (org-small-year-to-year
  13803. (string-to-number (match-string 1 ans))))
  13804. iso-weekday (if (match-end 3)
  13805. (string-to-number (match-string 3 ans)))
  13806. iso-week (string-to-number (match-string 2 ans)))
  13807. (setq ans (replace-match "" t t ans)))
  13808. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13809. (when (string-match
  13810. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13811. (setq year (if (match-end 2)
  13812. (string-to-number (match-string 2 ans))
  13813. (progn (setq kill-year t)
  13814. (string-to-number (format-time-string "%Y"))))
  13815. month (string-to-number (match-string 3 ans))
  13816. day (string-to-number (match-string 4 ans)))
  13817. (if (< year 100) (setq year (+ 2000 year)))
  13818. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13819. t nil ans)))
  13820. ;; Help matching dottet european dates
  13821. (when (string-match
  13822. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  13823. (setq year (if (match-end 3)
  13824. (string-to-number (match-string 3 ans))
  13825. (progn (setq kill-year t)
  13826. (string-to-number (format-time-string "%Y"))))
  13827. day (string-to-number (match-string 1 ans))
  13828. month (string-to-number (match-string 2 ans))
  13829. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13830. t nil ans)))
  13831. ;; Help matching american dates, like 5/30 or 5/30/7
  13832. (when (string-match
  13833. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13834. (setq year (if (match-end 4)
  13835. (string-to-number (match-string 4 ans))
  13836. (progn (setq kill-year t)
  13837. (string-to-number (format-time-string "%Y"))))
  13838. month (string-to-number (match-string 1 ans))
  13839. day (string-to-number (match-string 2 ans)))
  13840. (if (< year 100) (setq year (+ 2000 year)))
  13841. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13842. t nil ans)))
  13843. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13844. ;; If there is a time with am/pm, and *no* time without it, we convert
  13845. ;; so that matching will be successful.
  13846. (loop for i from 1 to 2 do ; twice, for end time as well
  13847. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13848. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13849. (setq hour (string-to-number (match-string 1 ans))
  13850. minute (if (match-end 3)
  13851. (string-to-number (match-string 3 ans))
  13852. 0)
  13853. pm (equal ?p
  13854. (string-to-char (downcase (match-string 4 ans)))))
  13855. (if (and (= hour 12) (not pm))
  13856. (setq hour 0)
  13857. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13858. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13859. t t ans))))
  13860. ;; Check if a time range is given as a duration
  13861. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13862. (setq hour (string-to-number (match-string 1 ans))
  13863. h2 (+ hour (string-to-number (match-string 3 ans)))
  13864. minute (string-to-number (match-string 2 ans))
  13865. m2 (+ minute (if (match-end 5) (string-to-number
  13866. (match-string 5 ans))0)))
  13867. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  13868. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  13869. t t ans)))
  13870. ;; Check if there is a time range
  13871. (when (boundp 'org-end-time-was-given)
  13872. (setq org-time-was-given nil)
  13873. (when (and (string-match org-plain-time-of-day-regexp ans)
  13874. (match-end 8))
  13875. (setq org-end-time-was-given (match-string 8 ans))
  13876. (setq ans (concat (substring ans 0 (match-beginning 7))
  13877. (substring ans (match-end 7))))))
  13878. (setq tl (parse-time-string ans)
  13879. day (or (nth 3 tl) (nth 3 defdecode))
  13880. month (or (nth 4 tl)
  13881. (if (and org-read-date-prefer-future
  13882. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  13883. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  13884. (nth 4 defdecode)))
  13885. year (or (and (not kill-year) (nth 5 tl))
  13886. (if (and org-read-date-prefer-future
  13887. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  13888. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  13889. (nth 5 defdecode)))
  13890. hour (or (nth 2 tl) (nth 2 defdecode))
  13891. minute (or (nth 1 tl) (nth 1 defdecode))
  13892. second (or (nth 0 tl) 0)
  13893. wday (nth 6 tl))
  13894. (when (and (eq org-read-date-prefer-future 'time)
  13895. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  13896. (equal day (nth 3 nowdecode))
  13897. (equal month (nth 4 nowdecode))
  13898. (equal year (nth 5 nowdecode))
  13899. (nth 2 tl)
  13900. (or (< (nth 2 tl) (nth 2 nowdecode))
  13901. (and (= (nth 2 tl) (nth 2 nowdecode))
  13902. (nth 1 tl)
  13903. (< (nth 1 tl) (nth 1 nowdecode)))))
  13904. (setq day (1+ day)
  13905. futurep t))
  13906. ;; Special date definitions below
  13907. (cond
  13908. (iso-week
  13909. ;; There was an iso week
  13910. (require 'cal-iso)
  13911. (setq futurep nil)
  13912. (setq year (or iso-year year)
  13913. day (or iso-weekday wday 1)
  13914. wday nil ; to make sure that the trigger below does not match
  13915. iso-date (calendar-gregorian-from-absolute
  13916. (calendar-absolute-from-iso
  13917. (list iso-week day year))))
  13918. ; FIXME: Should we also push ISO weeks into the future?
  13919. ; (when (and org-read-date-prefer-future
  13920. ; (not iso-year)
  13921. ; (< (calendar-absolute-from-gregorian iso-date)
  13922. ; (time-to-days (current-time))))
  13923. ; (setq year (1+ year)
  13924. ; iso-date (calendar-gregorian-from-absolute
  13925. ; (calendar-absolute-from-iso
  13926. ; (list iso-week day year)))))
  13927. (setq month (car iso-date)
  13928. year (nth 2 iso-date)
  13929. day (nth 1 iso-date)))
  13930. (deltan
  13931. (setq futurep nil)
  13932. (unless deltadef
  13933. (let ((now (decode-time (current-time))))
  13934. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  13935. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  13936. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  13937. ((equal deltaw "m") (setq month (+ month deltan)))
  13938. ((equal deltaw "y") (setq year (+ year deltan)))))
  13939. ((and wday (not (nth 3 tl)))
  13940. (setq futurep nil)
  13941. ;; Weekday was given, but no day, so pick that day in the week
  13942. ;; on or after the derived date.
  13943. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  13944. (unless (equal wday wday1)
  13945. (setq day (+ day (% (- wday wday1 -7) 7))))))
  13946. (if (and (boundp 'org-time-was-given)
  13947. (nth 2 tl))
  13948. (setq org-time-was-given t))
  13949. (if (< year 100) (setq year (+ 2000 year)))
  13950. ;; Check of the date is representable
  13951. (if org-read-date-force-compatible-dates
  13952. (progn
  13953. (if (< year 1970)
  13954. (setq year 1970 org-read-date-analyze-forced-year t))
  13955. (if (> year 2037)
  13956. (setq year 2037 org-read-date-analyze-forced-year t)))
  13957. (condition-case nil
  13958. (ignore (encode-time second minute hour day month year))
  13959. (error
  13960. (setq year (nth 5 defdecode))
  13961. (setq org-read-date-analyze-forced-year t))))
  13962. (setq org-read-date-analyze-futurep futurep)
  13963. (list second minute hour day month year)))
  13964. (defvar parse-time-weekdays)
  13965. (defun org-read-date-get-relative (s today default)
  13966. "Check string S for special relative date string.
  13967. TODAY and DEFAULT are internal times, for today and for a default.
  13968. Return shift list (N what def-flag)
  13969. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  13970. N is the number of WHATs to shift.
  13971. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  13972. the DEFAULT date rather than TODAY."
  13973. (require 'parse-time)
  13974. (when (and
  13975. (string-match
  13976. (concat
  13977. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  13978. "\\([0-9]+\\)?"
  13979. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  13980. "\\([ \t]\\|$\\)") s)
  13981. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  13982. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  13983. (string-to-char (substring (match-string 1 s) -1))
  13984. ?+))
  13985. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  13986. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  13987. (what (if (match-end 3) (match-string 3 s) "d"))
  13988. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  13989. (date (if rel default today))
  13990. (wday (nth 6 (decode-time date)))
  13991. delta)
  13992. (if wday1
  13993. (progn
  13994. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  13995. (if (= dir ?-) (setq delta (- delta 7)))
  13996. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  13997. (list delta "d" rel))
  13998. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  13999. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14000. "Turn a user-specified date into the internal representation.
  14001. The internal representation needed by the calendar is (month day year).
  14002. This is a wrapper to handle the brain-dead convention in calendar that
  14003. user function argument order change dependent on argument order."
  14004. (if (boundp 'calendar-date-style)
  14005. (cond
  14006. ((eq calendar-date-style 'american)
  14007. (list arg1 arg2 arg3))
  14008. ((eq calendar-date-style 'european)
  14009. (list arg2 arg1 arg3))
  14010. ((eq calendar-date-style 'iso)
  14011. (list arg2 arg3 arg1)))
  14012. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14013. (if (org-bound-and-true-p european-calendar-style)
  14014. (list arg2 arg1 arg3)
  14015. (list arg1 arg2 arg3)))))
  14016. (defun org-eval-in-calendar (form &optional keepdate)
  14017. "Eval FORM in the calendar window and return to current window.
  14018. Also, store the cursor date in variable org-ans2."
  14019. (let ((sf (selected-frame))
  14020. (sw (selected-window)))
  14021. (select-window (get-buffer-window "*Calendar*" t))
  14022. (eval form)
  14023. (when (and (not keepdate) (calendar-cursor-to-date))
  14024. (let* ((date (calendar-cursor-to-date))
  14025. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14026. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14027. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14028. (select-window sw)
  14029. (org-select-frame-set-input-focus sf)))
  14030. (defun org-calendar-select ()
  14031. "Return to `org-read-date' with the date currently selected.
  14032. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14033. (interactive)
  14034. (when (calendar-cursor-to-date)
  14035. (let* ((date (calendar-cursor-to-date))
  14036. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14037. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14038. (if (active-minibuffer-window) (exit-minibuffer))))
  14039. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14040. "Insert a date stamp for the date given by the internal TIME.
  14041. WITH-HM means use the stamp format that includes the time of the day.
  14042. INACTIVE means use square brackets instead of angular ones, so that the
  14043. stamp will not contribute to the agenda.
  14044. PRE and POST are optional strings to be inserted before and after the
  14045. stamp.
  14046. The command returns the inserted time stamp."
  14047. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14048. stamp)
  14049. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14050. (insert-before-markers (or pre ""))
  14051. (when (listp extra)
  14052. (setq extra (car extra))
  14053. (if (and (stringp extra)
  14054. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14055. (setq extra (format "-%02d:%02d"
  14056. (string-to-number (match-string 1 extra))
  14057. (string-to-number (match-string 2 extra))))
  14058. (setq extra nil)))
  14059. (when extra
  14060. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14061. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14062. (insert-before-markers (or post ""))
  14063. (setq org-last-inserted-timestamp stamp)))
  14064. (defun org-toggle-time-stamp-overlays ()
  14065. "Toggle the use of custom time stamp formats."
  14066. (interactive)
  14067. (setq org-display-custom-times (not org-display-custom-times))
  14068. (unless org-display-custom-times
  14069. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14070. (while (setq p (next-single-property-change p 'display))
  14071. (if (and (get-text-property p 'display)
  14072. (eq (get-text-property p 'face) 'org-date))
  14073. (remove-text-properties
  14074. p (setq p (next-single-property-change p 'display))
  14075. '(display t))))
  14076. (set-buffer-modified-p bmp)))
  14077. (if (featurep 'xemacs)
  14078. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14079. (org-restart-font-lock)
  14080. (setq org-table-may-need-update t)
  14081. (if org-display-custom-times
  14082. (message "Time stamps are overlaid with custom format")
  14083. (message "Time stamp overlays removed")))
  14084. (defun org-display-custom-time (beg end)
  14085. "Overlay modified time stamp format over timestamp between BEG and END."
  14086. (let* ((ts (buffer-substring beg end))
  14087. t1 w1 with-hm tf time str w2 (off 0))
  14088. (save-match-data
  14089. (setq t1 (org-parse-time-string ts t))
  14090. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  14091. (setq off (- (match-end 0) (match-beginning 0)))))
  14092. (setq end (- end off))
  14093. (setq w1 (- end beg)
  14094. with-hm (and (nth 1 t1) (nth 2 t1))
  14095. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14096. time (org-fix-decoded-time t1)
  14097. str (org-add-props
  14098. (format-time-string
  14099. (substring tf 1 -1) (apply 'encode-time time))
  14100. nil 'mouse-face 'highlight)
  14101. w2 (length str))
  14102. (if (not (= w2 w1))
  14103. (add-text-properties (1+ beg) (+ 2 beg)
  14104. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14105. (if (featurep 'xemacs)
  14106. (progn
  14107. (put-text-property beg end 'invisible t)
  14108. (put-text-property beg end 'end-glyph (make-glyph str)))
  14109. (put-text-property beg end 'display str))))
  14110. (defun org-translate-time (string)
  14111. "Translate all timestamps in STRING to custom format.
  14112. But do this only if the variable `org-display-custom-times' is set."
  14113. (when org-display-custom-times
  14114. (save-match-data
  14115. (let* ((start 0)
  14116. (re org-ts-regexp-both)
  14117. t1 with-hm inactive tf time str beg end)
  14118. (while (setq start (string-match re string start))
  14119. (setq beg (match-beginning 0)
  14120. end (match-end 0)
  14121. t1 (save-match-data
  14122. (org-parse-time-string (substring string beg end) t))
  14123. with-hm (and (nth 1 t1) (nth 2 t1))
  14124. inactive (equal (substring string beg (1+ beg)) "[")
  14125. tf (funcall (if with-hm 'cdr 'car)
  14126. org-time-stamp-custom-formats)
  14127. time (org-fix-decoded-time t1)
  14128. str (format-time-string
  14129. (concat
  14130. (if inactive "[" "<") (substring tf 1 -1)
  14131. (if inactive "]" ">"))
  14132. (apply 'encode-time time))
  14133. string (replace-match str t t string)
  14134. start (+ start (length str)))))))
  14135. string)
  14136. (defun org-fix-decoded-time (time)
  14137. "Set 0 instead of nil for the first 6 elements of time.
  14138. Don't touch the rest."
  14139. (let ((n 0))
  14140. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14141. (defun org-days-to-time (timestamp-string)
  14142. "Difference between TIMESTAMP-STRING and now in days."
  14143. (- (time-to-days (org-time-string-to-time timestamp-string))
  14144. (time-to-days (current-time))))
  14145. (defun org-deadline-close (timestamp-string &optional ndays)
  14146. "Is the time in TIMESTAMP-STRING close to the current date?"
  14147. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14148. (and (< (org-days-to-time timestamp-string) ndays)
  14149. (not (org-entry-is-done-p))))
  14150. (defun org-get-wdays (ts)
  14151. "Get the deadline lead time appropriate for timestring TS."
  14152. (cond
  14153. ((<= org-deadline-warning-days 0)
  14154. ;; 0 or negative, enforce this value no matter what
  14155. (- org-deadline-warning-days))
  14156. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14157. ;; lead time is specified.
  14158. (floor (* (string-to-number (match-string 1 ts))
  14159. (cdr (assoc (match-string 2 ts)
  14160. '(("d" . 1) ("w" . 7)
  14161. ("m" . 30.4) ("y" . 365.25)))))))
  14162. ;; go for the default.
  14163. (t org-deadline-warning-days)))
  14164. (defun org-calendar-select-mouse (ev)
  14165. "Return to `org-read-date' with the date currently selected.
  14166. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14167. (interactive "e")
  14168. (mouse-set-point ev)
  14169. (when (calendar-cursor-to-date)
  14170. (let* ((date (calendar-cursor-to-date))
  14171. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14172. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14173. (if (active-minibuffer-window) (exit-minibuffer))))
  14174. (defun org-check-deadlines (ndays)
  14175. "Check if there are any deadlines due or past due.
  14176. A deadline is considered due if it happens within `org-deadline-warning-days'
  14177. days from today's date. If the deadline appears in an entry marked DONE,
  14178. it is not shown. The prefix arg NDAYS can be used to test that many
  14179. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14180. (interactive "P")
  14181. (let* ((org-warn-days
  14182. (cond
  14183. ((equal ndays '(4)) 100000)
  14184. (ndays (prefix-numeric-value ndays))
  14185. (t (abs org-deadline-warning-days))))
  14186. (case-fold-search nil)
  14187. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14188. (callback
  14189. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14190. (message "%d deadlines past-due or due within %d days"
  14191. (org-occur regexp nil callback)
  14192. org-warn-days)))
  14193. (defun org-check-before-date (date)
  14194. "Check if there are deadlines or scheduled entries before DATE."
  14195. (interactive (list (org-read-date)))
  14196. (let ((case-fold-search nil)
  14197. (regexp (concat "\\<\\(" org-deadline-string
  14198. "\\|" org-scheduled-string
  14199. "\\) *<\\([^>]+\\)>"))
  14200. (callback
  14201. (lambda () (time-less-p
  14202. (org-time-string-to-time (match-string 2))
  14203. (org-time-string-to-time date)))))
  14204. (message "%d entries before %s"
  14205. (org-occur regexp nil callback) date)))
  14206. (defun org-check-after-date (date)
  14207. "Check if there are deadlines or scheduled entries after DATE."
  14208. (interactive (list (org-read-date)))
  14209. (let ((case-fold-search nil)
  14210. (regexp (concat "\\<\\(" org-deadline-string
  14211. "\\|" org-scheduled-string
  14212. "\\) *<\\([^>]+\\)>"))
  14213. (callback
  14214. (lambda () (not
  14215. (time-less-p
  14216. (org-time-string-to-time (match-string 2))
  14217. (org-time-string-to-time date))))))
  14218. (message "%d entries after %s"
  14219. (org-occur regexp nil callback) date)))
  14220. (defun org-check-dates-range (start-date end-date)
  14221. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14222. (interactive (list (org-read-date nil nil nil "Range starts")
  14223. (org-read-date nil nil nil "Range end")))
  14224. (let ((case-fold-search nil)
  14225. (regexp (concat "\\<\\(" org-deadline-string
  14226. "\\|" org-scheduled-string
  14227. "\\) *<\\([^>]+\\)>"))
  14228. (callback
  14229. (lambda ()
  14230. (let ((match (match-string 2)))
  14231. (and
  14232. (not (time-less-p
  14233. (org-time-string-to-time match)
  14234. (org-time-string-to-time start-date)))
  14235. (time-less-p
  14236. (org-time-string-to-time match)
  14237. (org-time-string-to-time end-date)))))))
  14238. (message "%d entries between %s and %s"
  14239. (org-occur regexp nil callback) start-date end-date)))
  14240. (defun org-evaluate-time-range (&optional to-buffer)
  14241. "Evaluate a time range by computing the difference between start and end.
  14242. Normally the result is just printed in the echo area, but with prefix arg
  14243. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14244. If the time range is actually in a table, the result is inserted into the
  14245. next column.
  14246. For time difference computation, a year is assumed to be exactly 365
  14247. days in order to avoid rounding problems."
  14248. (interactive "P")
  14249. (or
  14250. (org-clock-update-time-maybe)
  14251. (save-excursion
  14252. (unless (org-at-date-range-p t)
  14253. (goto-char (point-at-bol))
  14254. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14255. (if (not (org-at-date-range-p t))
  14256. (error "Not at a time-stamp range, and none found in current line")))
  14257. (let* ((ts1 (match-string 1))
  14258. (ts2 (match-string 2))
  14259. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14260. (match-end (match-end 0))
  14261. (time1 (org-time-string-to-time ts1))
  14262. (time2 (org-time-string-to-time ts2))
  14263. (t1 (org-float-time time1))
  14264. (t2 (org-float-time time2))
  14265. (diff (abs (- t2 t1)))
  14266. (negative (< (- t2 t1) 0))
  14267. ;; (ys (floor (* 365 24 60 60)))
  14268. (ds (* 24 60 60))
  14269. (hs (* 60 60))
  14270. (fy "%dy %dd %02d:%02d")
  14271. (fy1 "%dy %dd")
  14272. (fd "%dd %02d:%02d")
  14273. (fd1 "%dd")
  14274. (fh "%02d:%02d")
  14275. y d h m align)
  14276. (if havetime
  14277. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14278. y 0
  14279. d (floor (/ diff ds)) diff (mod diff ds)
  14280. h (floor (/ diff hs)) diff (mod diff hs)
  14281. m (floor (/ diff 60)))
  14282. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14283. y 0
  14284. d (floor (+ (/ diff ds) 0.5))
  14285. h 0 m 0))
  14286. (if (not to-buffer)
  14287. (message "%s" (org-make-tdiff-string y d h m))
  14288. (if (org-at-table-p)
  14289. (progn
  14290. (goto-char match-end)
  14291. (setq align t)
  14292. (and (looking-at " *|") (goto-char (match-end 0))))
  14293. (goto-char match-end))
  14294. (if (looking-at
  14295. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14296. (replace-match ""))
  14297. (if negative (insert " -"))
  14298. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14299. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14300. (insert " " (format fh h m))))
  14301. (if align (org-table-align))
  14302. (message "Time difference inserted")))))
  14303. (defun org-make-tdiff-string (y d h m)
  14304. (let ((fmt "")
  14305. (l nil))
  14306. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14307. l (push y l)))
  14308. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14309. l (push d l)))
  14310. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14311. l (push h l)))
  14312. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14313. l (push m l)))
  14314. (apply 'format fmt (nreverse l))))
  14315. (defun org-time-string-to-time (s &optional buffer pos)
  14316. (condition-case errdata
  14317. (apply 'encode-time (org-parse-time-string s))
  14318. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14319. s (if (not (and buffer pos))
  14320. ""
  14321. (format " at %d in buffer `%s'" pos buffer))
  14322. (cdr errdata)))))
  14323. (defun org-time-string-to-seconds (s)
  14324. (org-float-time (org-time-string-to-time s)))
  14325. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14326. "Convert a time stamp to an absolute day number.
  14327. If there is a specifier for a cyclic time stamp, get the closest date to
  14328. DAYNR.
  14329. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14330. The variable date is bound by the calendar when this is called."
  14331. (cond
  14332. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14333. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14334. daynr
  14335. (+ daynr 1000)))
  14336. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  14337. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14338. (time-to-days (current-time))) (match-string 0 s)
  14339. prefer show-all))
  14340. (t (time-to-days
  14341. (condition-case errdata
  14342. (apply 'encode-time (org-parse-time-string s))
  14343. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14344. s (if (not (and buffer pos))
  14345. ""
  14346. (format " at %d in buffer `%s'" pos buffer))
  14347. (cdr errdata))))))))
  14348. (defun org-days-to-iso-week (days)
  14349. "Return the iso week number."
  14350. (require 'cal-iso)
  14351. (car (calendar-iso-from-absolute days)))
  14352. (defun org-small-year-to-year (year)
  14353. "Convert 2-digit years into 4-digit years.
  14354. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14355. The year 2000 cannot be abbreviated. Any year larger than 99
  14356. is returned unchanged."
  14357. (if (< year 38)
  14358. (setq year (+ 2000 year))
  14359. (if (< year 100)
  14360. (setq year (+ 1900 year))))
  14361. year)
  14362. (defun org-time-from-absolute (d)
  14363. "Return the time corresponding to date D.
  14364. D may be an absolute day number, or a calendar-type list (month day year)."
  14365. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14366. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14367. (defun org-calendar-holiday ()
  14368. "List of holidays, for Diary display in Org-mode."
  14369. (require 'holidays)
  14370. (let ((hl (funcall
  14371. (if (fboundp 'calendar-check-holidays)
  14372. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14373. (if hl (mapconcat 'identity hl "; "))))
  14374. (defun org-diary-sexp-entry (sexp entry date)
  14375. "Process a SEXP diary ENTRY for DATE."
  14376. (require 'diary-lib)
  14377. (let ((result (if calendar-debug-sexp
  14378. (let ((stack-trace-on-error t))
  14379. (eval (car (read-from-string sexp))))
  14380. (condition-case nil
  14381. (eval (car (read-from-string sexp)))
  14382. (error
  14383. (beep)
  14384. (message "Bad sexp at line %d in %s: %s"
  14385. (org-current-line)
  14386. (buffer-file-name) sexp)
  14387. (sleep-for 2))))))
  14388. (cond ((stringp result) (split-string result "; "))
  14389. ((and (consp result)
  14390. (not (consp (cdr result)))
  14391. (stringp (cdr result))) (cdr result))
  14392. ((and (consp result)
  14393. (stringp (car result))) result)
  14394. (result entry)
  14395. (t nil))))
  14396. (defun org-diary-to-ical-string (frombuf)
  14397. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14398. This uses the icalendar.el library."
  14399. (let* ((tmpdir (if (featurep 'xemacs)
  14400. (temp-directory)
  14401. temporary-file-directory))
  14402. (tmpfile (make-temp-name
  14403. (expand-file-name "orgics" tmpdir)))
  14404. buf rtn b e)
  14405. (with-current-buffer frombuf
  14406. (icalendar-export-region (point-min) (point-max) tmpfile)
  14407. (setq buf (find-buffer-visiting tmpfile))
  14408. (set-buffer buf)
  14409. (goto-char (point-min))
  14410. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14411. (setq b (match-beginning 0)))
  14412. (goto-char (point-max))
  14413. (if (re-search-backward "^END:VEVENT" nil t)
  14414. (setq e (match-end 0)))
  14415. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14416. (kill-buffer buf)
  14417. (delete-file tmpfile)
  14418. rtn))
  14419. (defun org-closest-date (start current change prefer show-all)
  14420. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14421. When PREFER is `past', return a date that is either CURRENT or past.
  14422. When PREFER is `future', return a date that is either CURRENT or future.
  14423. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14424. ;; Make the proper lists from the dates
  14425. (catch 'exit
  14426. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14427. dn dw sday cday n1 n2 n0
  14428. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14429. (setq start (org-date-to-gregorian start)
  14430. current (org-date-to-gregorian
  14431. (if show-all
  14432. current
  14433. (time-to-days (current-time))))
  14434. sday (calendar-absolute-from-gregorian start)
  14435. cday (calendar-absolute-from-gregorian current))
  14436. (if (<= cday sday) (throw 'exit sday))
  14437. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  14438. (setq dn (string-to-number (match-string 1 change))
  14439. dw (cdr (assoc (match-string 2 change) a1)))
  14440. (error "Invalid change specifier: %s" change))
  14441. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14442. (cond
  14443. ((eq dw 'day)
  14444. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14445. n2 (+ n1 dn)))
  14446. ((eq dw 'year)
  14447. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14448. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14449. (setq date1 (list m d y1)
  14450. n1 (calendar-absolute-from-gregorian date1)
  14451. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14452. n2 (calendar-absolute-from-gregorian date2)))
  14453. ((eq dw 'month)
  14454. ;; approx number of month between the two dates
  14455. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14456. ;; How often does dn fit in there?
  14457. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14458. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14459. m (+ m nm)
  14460. ny (floor (/ m 12))
  14461. y (+ y ny)
  14462. m (- m (* ny 12)))
  14463. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14464. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14465. (setq m2 (+ m dn) y2 y)
  14466. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14467. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14468. (while (<= n2 cday)
  14469. (setq n1 n2 m m2 y y2)
  14470. (setq m2 (+ m dn) y2 y)
  14471. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14472. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14473. ;; Make sure n1 is the earlier date
  14474. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14475. (if show-all
  14476. (cond
  14477. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14478. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14479. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14480. (cond
  14481. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14482. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14483. (t (if (= cday n1) n1 n2)))))))
  14484. (defun org-date-to-gregorian (date)
  14485. "Turn any specification of DATE into a Gregorian date for the calendar."
  14486. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14487. ((and (listp date) (= (length date) 3)) date)
  14488. ((stringp date)
  14489. (setq date (org-parse-time-string date))
  14490. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14491. ((listp date)
  14492. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14493. (defun org-parse-time-string (s &optional nodefault)
  14494. "Parse the standard Org-mode time string.
  14495. This should be a lot faster than the normal `parse-time-string'.
  14496. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14497. hour and minute fields will be nil if not given."
  14498. (if (string-match org-ts-regexp0 s)
  14499. (list 0
  14500. (if (or (match-beginning 8) (not nodefault))
  14501. (string-to-number (or (match-string 8 s) "0")))
  14502. (if (or (match-beginning 7) (not nodefault))
  14503. (string-to-number (or (match-string 7 s) "0")))
  14504. (string-to-number (match-string 4 s))
  14505. (string-to-number (match-string 3 s))
  14506. (string-to-number (match-string 2 s))
  14507. nil nil nil)
  14508. (error "Not a standard Org-mode time string: %s" s)))
  14509. (defun org-timestamp-up (&optional arg)
  14510. "Increase the date item at the cursor by one.
  14511. If the cursor is on the year, change the year. If it is on the month,
  14512. the day or the time, change that.
  14513. With prefix ARG, change by that many units."
  14514. (interactive "p")
  14515. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14516. (defun org-timestamp-down (&optional arg)
  14517. "Decrease the date item at the cursor by one.
  14518. If the cursor is on the year, change the year. If it is on the month,
  14519. the day or the time, change that.
  14520. With prefix ARG, change by that many units."
  14521. (interactive "p")
  14522. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14523. (defun org-timestamp-up-day (&optional arg)
  14524. "Increase the date in the time stamp by one day.
  14525. With prefix ARG, change that many days."
  14526. (interactive "p")
  14527. (if (and (not (org-at-timestamp-p t))
  14528. (org-at-heading-p))
  14529. (org-todo 'up)
  14530. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14531. (defun org-timestamp-down-day (&optional arg)
  14532. "Decrease the date in the time stamp by one day.
  14533. With prefix ARG, change that many days."
  14534. (interactive "p")
  14535. (if (and (not (org-at-timestamp-p t))
  14536. (org-at-heading-p))
  14537. (org-todo 'down)
  14538. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14539. (defun org-at-timestamp-p (&optional inactive-ok)
  14540. "Determine if the cursor is in or at a timestamp."
  14541. (interactive)
  14542. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14543. (pos (point))
  14544. (ans (or (looking-at tsr)
  14545. (save-excursion
  14546. (skip-chars-backward "^[<\n\r\t")
  14547. (if (> (point) (point-min)) (backward-char 1))
  14548. (and (looking-at tsr)
  14549. (> (- (match-end 0) pos) -1))))))
  14550. (and ans
  14551. (boundp 'org-ts-what)
  14552. (setq org-ts-what
  14553. (cond
  14554. ((= pos (match-beginning 0)) 'bracket)
  14555. ;; Point is considered to be "on the bracket" whether
  14556. ;; it's really on it or right after it.
  14557. ((or (= pos (1- (match-end 0)))
  14558. (= pos (match-end 0))) 'bracket)
  14559. ((org-pos-in-match-range pos 2) 'year)
  14560. ((org-pos-in-match-range pos 3) 'month)
  14561. ((org-pos-in-match-range pos 7) 'hour)
  14562. ((org-pos-in-match-range pos 8) 'minute)
  14563. ((or (org-pos-in-match-range pos 4)
  14564. (org-pos-in-match-range pos 5)) 'day)
  14565. ((and (> pos (or (match-end 8) (match-end 5)))
  14566. (< pos (match-end 0)))
  14567. (- pos (or (match-end 8) (match-end 5))))
  14568. (t 'day))))
  14569. ans))
  14570. (defun org-toggle-timestamp-type ()
  14571. "Toggle the type (<active> or [inactive]) of a time stamp."
  14572. (interactive)
  14573. (when (org-at-timestamp-p t)
  14574. (let ((beg (match-beginning 0)) (end (match-end 0))
  14575. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14576. (save-excursion
  14577. (goto-char beg)
  14578. (while (re-search-forward "[][<>]" end t)
  14579. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14580. t t)))
  14581. (message "Timestamp is now %sactive"
  14582. (if (equal (char-after beg) ?<) "" "in")))))
  14583. (defun org-timestamp-change (n &optional what updown)
  14584. "Change the date in the time stamp at point.
  14585. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14586. `year', `minute', `second'. If WHAT is not given, the cursor position
  14587. in the timestamp determines what will be changed."
  14588. (let ((origin (point)) origin-cat
  14589. with-hm inactive
  14590. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  14591. org-ts-what
  14592. extra rem
  14593. ts time time0)
  14594. (if (not (org-at-timestamp-p t))
  14595. (error "Not at a timestamp"))
  14596. (if (and (not what) (eq org-ts-what 'bracket))
  14597. (org-toggle-timestamp-type)
  14598. ;; Point isn't on brackets. Remember the part of the time-stamp
  14599. ;; the point was in. Indeed, size of time-stamps may change,
  14600. ;; but point must be kept in the same category nonetheless.
  14601. (setq origin-cat org-ts-what)
  14602. (if (and (not what) (not (eq org-ts-what 'day))
  14603. org-display-custom-times
  14604. (get-text-property (point) 'display)
  14605. (not (get-text-property (1- (point)) 'display)))
  14606. (setq org-ts-what 'day))
  14607. (setq org-ts-what (or what org-ts-what)
  14608. inactive (= (char-after (match-beginning 0)) ?\[)
  14609. ts (match-string 0))
  14610. (replace-match "")
  14611. (if (string-match
  14612. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  14613. ts)
  14614. (setq extra (match-string 1 ts)))
  14615. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14616. (setq with-hm t))
  14617. (setq time0 (org-parse-time-string ts))
  14618. (when (and updown
  14619. (eq org-ts-what 'minute)
  14620. (not current-prefix-arg))
  14621. ;; This looks like s-up and s-down. Change by one rounding step.
  14622. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  14623. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  14624. (setcar (cdr time0) (+ (nth 1 time0)
  14625. (if (> n 0) (- rem) (- dm rem))))))
  14626. (setq time
  14627. (encode-time (or (car time0) 0)
  14628. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  14629. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  14630. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  14631. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  14632. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  14633. (nthcdr 6 time0)))
  14634. (when (and (member org-ts-what '(hour minute))
  14635. extra
  14636. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  14637. (setq extra (org-modify-ts-extra
  14638. extra
  14639. (if (eq org-ts-what 'hour) 2 5)
  14640. n dm)))
  14641. (when (integerp org-ts-what)
  14642. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  14643. (if (eq what 'calendar)
  14644. (let ((cal-date (org-get-date-from-calendar)))
  14645. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14646. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14647. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14648. (setcar time0 (or (car time0) 0))
  14649. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14650. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14651. (setq time (apply 'encode-time time0))))
  14652. ;; Insert the new time-stamp, and ensure point stays in the same
  14653. ;; category as before (i.e. not after the last position in that
  14654. ;; category).
  14655. (let ((pos (point)))
  14656. ;; Stay before inserted string. `save-excursion' is of no use.
  14657. (setq org-last-changed-timestamp
  14658. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14659. (goto-char pos))
  14660. (save-match-data
  14661. (looking-at org-ts-regexp3)
  14662. (goto-char (cond
  14663. ;; `day' category ends before `hour' if any, or at
  14664. ;; the end of the day name.
  14665. ((eq origin-cat 'day)
  14666. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  14667. ((eq origin-cat 'hour) (min (match-end 7) origin))
  14668. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  14669. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  14670. ;; `year' and `month' have both fixed size: point
  14671. ;; couldn't have moved into another part.
  14672. (t origin))))
  14673. ;; Update clock if on a CLOCK line.
  14674. (org-clock-update-time-maybe)
  14675. ;; Try to recenter the calendar window, if any.
  14676. (if (and org-calendar-follow-timestamp-change
  14677. (get-buffer-window "*Calendar*" t)
  14678. (memq org-ts-what '(day month year)))
  14679. (org-recenter-calendar (time-to-days time))))))
  14680. (defun org-modify-ts-extra (s pos n dm)
  14681. "Change the different parts of the lead-time and repeat fields in timestamp."
  14682. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14683. ng h m new rem)
  14684. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14685. (cond
  14686. ((or (org-pos-in-match-range pos 2)
  14687. (org-pos-in-match-range pos 3))
  14688. (setq m (string-to-number (match-string 3 s))
  14689. h (string-to-number (match-string 2 s)))
  14690. (if (org-pos-in-match-range pos 2)
  14691. (setq h (+ h n))
  14692. (setq n (* dm (org-no-warnings (signum n))))
  14693. (when (not (= 0 (setq rem (% m dm))))
  14694. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14695. (setq m (+ m n)))
  14696. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14697. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14698. (setq h (min 24 (max 0 h)))
  14699. (setq ng 1 new (format "-%02d:%02d" h m)))
  14700. ((org-pos-in-match-range pos 6)
  14701. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14702. ((org-pos-in-match-range pos 5)
  14703. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14704. ((org-pos-in-match-range pos 9)
  14705. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14706. ((org-pos-in-match-range pos 8)
  14707. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14708. (when ng
  14709. (setq s (concat
  14710. (substring s 0 (match-beginning ng))
  14711. new
  14712. (substring s (match-end ng))))))
  14713. s))
  14714. (defun org-recenter-calendar (date)
  14715. "If the calendar is visible, recenter it to DATE."
  14716. (let* ((win (selected-window))
  14717. (cwin (get-buffer-window "*Calendar*" t))
  14718. (calendar-move-hook nil))
  14719. (when cwin
  14720. (select-window cwin)
  14721. (calendar-goto-date (if (listp date) date
  14722. (calendar-gregorian-from-absolute date)))
  14723. (select-window win))))
  14724. (defun org-goto-calendar (&optional arg)
  14725. "Go to the Emacs calendar at the current date.
  14726. If there is a time stamp in the current line, go to that date.
  14727. A prefix ARG can be used to force the current date."
  14728. (interactive "P")
  14729. (let ((tsr org-ts-regexp) diff
  14730. (calendar-move-hook nil)
  14731. (calendar-view-holidays-initially-flag nil)
  14732. (calendar-view-diary-initially-flag nil))
  14733. (if (or (org-at-timestamp-p)
  14734. (save-excursion
  14735. (beginning-of-line 1)
  14736. (looking-at (concat ".*" tsr))))
  14737. (let ((d1 (time-to-days (current-time)))
  14738. (d2 (time-to-days
  14739. (org-time-string-to-time (match-string 1)))))
  14740. (setq diff (- d2 d1))))
  14741. (calendar)
  14742. (calendar-goto-today)
  14743. (if (and diff (not arg)) (calendar-forward-day diff))))
  14744. (defun org-get-date-from-calendar ()
  14745. "Return a list (month day year) of date at point in calendar."
  14746. (with-current-buffer "*Calendar*"
  14747. (save-match-data
  14748. (calendar-cursor-to-date))))
  14749. (defun org-date-from-calendar ()
  14750. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14751. If there is already a time stamp at the cursor position, update it."
  14752. (interactive)
  14753. (if (org-at-timestamp-p t)
  14754. (org-timestamp-change 0 'calendar)
  14755. (let ((cal-date (org-get-date-from-calendar)))
  14756. (org-insert-time-stamp
  14757. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14758. (defun org-minutes-to-hh:mm-string (m)
  14759. "Compute H:MM from a number of minutes."
  14760. (let ((h (/ m 60)))
  14761. (setq m (- m (* 60 h)))
  14762. (format org-time-clocksum-format h m)))
  14763. (defun org-hh:mm-string-to-minutes (s)
  14764. "Convert a string H:MM to a number of minutes.
  14765. If the string is just a number, interpret it as minutes.
  14766. In fact, the first hh:mm or number in the string will be taken,
  14767. there can be extra stuff in the string.
  14768. If no number is found, the return value is 0."
  14769. (cond
  14770. ((integerp s) s)
  14771. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14772. (+ (* (string-to-number (match-string 1 s)) 60)
  14773. (string-to-number (match-string 2 s))))
  14774. ((string-match "\\([0-9]+\\)" s)
  14775. (string-to-number (match-string 1 s)))
  14776. (t 0)))
  14777. (defcustom org-effort-durations
  14778. `(("h" . 60)
  14779. ("d" . ,(* 60 8))
  14780. ("w" . ,(* 60 8 5))
  14781. ("m" . ,(* 60 8 5 4))
  14782. ("y" . ,(* 60 8 5 40)))
  14783. "Conversion factor to minutes for an effort modifier.
  14784. Each entry has the form (MODIFIER . MINUTES).
  14785. In an effort string, a number followed by MODIFIER is multiplied
  14786. by the specified number of MINUTES to obtain an effort in
  14787. minutes.
  14788. For example, if the value of this variable is ((\"hours\" . 60)), then an
  14789. effort string \"2hours\" is equivalent to 120 minutes."
  14790. :group 'org-agenda
  14791. :type '(alist :key-type (string :tag "Modifier")
  14792. :value-type (number :tag "Minutes")))
  14793. (defun org-duration-string-to-minutes (s)
  14794. "Convert a duration string S to minutes.
  14795. A bare number is interpreted as minutes, modifiers can be set by
  14796. customizing `org-effort-durations' (which see).
  14797. Entries containing a colon are interpreted as H:MM by
  14798. `org-hh:mm-string-to-minutes'."
  14799. (let ((result 0)
  14800. (re (concat "\\([0-9]+\\) *\\("
  14801. (regexp-opt (mapcar 'car org-effort-durations))
  14802. "\\)")))
  14803. (while (string-match re s)
  14804. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  14805. (string-to-number (match-string 1 s))))
  14806. (setq s (replace-match "" nil t s)))
  14807. (incf result (org-hh:mm-string-to-minutes s))
  14808. result))
  14809. ;;;; Files
  14810. (defun org-save-all-org-buffers ()
  14811. "Save all Org-mode buffers without user confirmation."
  14812. (interactive)
  14813. (message "Saving all Org-mode buffers...")
  14814. (save-some-buffers t (lambda () (eq major-mode 'org-mode)))
  14815. (when (featurep 'org-id) (org-id-locations-save))
  14816. (message "Saving all Org-mode buffers... done"))
  14817. (defun org-revert-all-org-buffers ()
  14818. "Revert all Org-mode buffers.
  14819. Prompt for confirmation when there are unsaved changes.
  14820. Be sure you know what you are doing before letting this function
  14821. overwrite your changes.
  14822. This function is useful in a setup where one tracks org files
  14823. with a version control system, to revert on one machine after pulling
  14824. changes from another. I believe the procedure must be like this:
  14825. 1. M-x org-save-all-org-buffers
  14826. 2. Pull changes from the other machine, resolve conflicts
  14827. 3. M-x org-revert-all-org-buffers"
  14828. (interactive)
  14829. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14830. (error "Abort"))
  14831. (save-excursion
  14832. (save-window-excursion
  14833. (mapc
  14834. (lambda (b)
  14835. (when (and (with-current-buffer b (eq major-mode 'org-mode))
  14836. (with-current-buffer b buffer-file-name))
  14837. (org-pop-to-buffer-same-window b)
  14838. (revert-buffer t 'no-confirm)))
  14839. (buffer-list))
  14840. (when (and (featurep 'org-id) org-id-track-globally)
  14841. (org-id-locations-load)))))
  14842. ;;;; Agenda files
  14843. ;;;###autoload
  14844. (defun org-switchb (&optional arg)
  14845. "Switch between Org buffers.
  14846. With one prefix argument, restrict available buffers to files.
  14847. With two prefix arguments, restrict available buffers to agenda files.
  14848. Defaults to `iswitchb' for buffer name completion.
  14849. Set `org-completion-use-ido' to make it use ido instead."
  14850. (interactive "P")
  14851. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14852. ((equal arg '(16)) (org-buffer-list 'agenda))
  14853. (t (org-buffer-list))))
  14854. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14855. (org-completion-use-ido org-completion-use-ido))
  14856. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14857. (setq org-completion-use-iswitchb t))
  14858. (org-pop-to-buffer-same-window
  14859. (org-icompleting-read "Org buffer: "
  14860. (mapcar 'list (mapcar 'buffer-name blist))
  14861. nil t))))
  14862. ;;; Define some older names previously used for this functionality
  14863. ;;;###autoload
  14864. (defalias 'org-ido-switchb 'org-switchb)
  14865. ;;;###autoload
  14866. (defalias 'org-iswitchb 'org-switchb)
  14867. (defun org-buffer-list (&optional predicate exclude-tmp)
  14868. "Return a list of Org buffers.
  14869. PREDICATE can be `export', `files' or `agenda'.
  14870. export restrict the list to Export buffers.
  14871. files restrict the list to buffers visiting Org files.
  14872. agenda restrict the list to buffers visiting agenda files.
  14873. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  14874. (let* ((bfn nil)
  14875. (agenda-files (and (eq predicate 'agenda)
  14876. (mapcar 'file-truename (org-agenda-files t))))
  14877. (filter
  14878. (cond
  14879. ((eq predicate 'files)
  14880. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  14881. ((eq predicate 'export)
  14882. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  14883. ((eq predicate 'agenda)
  14884. (lambda (b)
  14885. (with-current-buffer b
  14886. (and (eq major-mode 'org-mode)
  14887. (setq bfn (buffer-file-name b))
  14888. (member (file-truename bfn) agenda-files)))))
  14889. (t (lambda (b) (with-current-buffer b
  14890. (or (eq major-mode 'org-mode)
  14891. (string-match "\*Org .*Export"
  14892. (buffer-name b)))))))))
  14893. (delq nil
  14894. (mapcar
  14895. (lambda(b)
  14896. (if (and (funcall filter b)
  14897. (or (not exclude-tmp)
  14898. (not (string-match "tmp" (buffer-name b)))))
  14899. b
  14900. nil))
  14901. (buffer-list)))))
  14902. (defun org-agenda-files (&optional unrestricted archives)
  14903. "Get the list of agenda files.
  14904. Optional UNRESTRICTED means return the full list even if a restriction
  14905. is currently in place.
  14906. When ARCHIVES is t, include all archive files that are really being
  14907. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  14908. `org-agenda-archives-mode' is t."
  14909. (let ((files
  14910. (cond
  14911. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  14912. ((stringp org-agenda-files) (org-read-agenda-file-list))
  14913. ((listp org-agenda-files) org-agenda-files)
  14914. (t (error "Invalid value of `org-agenda-files'")))))
  14915. (setq files (apply 'append
  14916. (mapcar (lambda (f)
  14917. (if (file-directory-p f)
  14918. (directory-files
  14919. f t org-agenda-file-regexp)
  14920. (list f)))
  14921. files)))
  14922. (when org-agenda-skip-unavailable-files
  14923. (setq files (delq nil
  14924. (mapcar (function
  14925. (lambda (file)
  14926. (and (file-readable-p file) file)))
  14927. files))))
  14928. (when (or (eq archives t)
  14929. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  14930. (setq files (org-add-archive-files files)))
  14931. files))
  14932. (defun org-agenda-file-p (&optional file)
  14933. "Return non-nil, if FILE is an agenda file.
  14934. If FILE is omitted, use the file associated with the current
  14935. buffer."
  14936. (member (or file (buffer-file-name))
  14937. (org-agenda-files t)))
  14938. (defun org-edit-agenda-file-list ()
  14939. "Edit the list of agenda files.
  14940. Depending on setup, this either uses customize to edit the variable
  14941. `org-agenda-files', or it visits the file that is holding the list. In the
  14942. latter case, the buffer is set up in a way that saving it automatically kills
  14943. the buffer and restores the previous window configuration."
  14944. (interactive)
  14945. (if (stringp org-agenda-files)
  14946. (let ((cw (current-window-configuration)))
  14947. (find-file org-agenda-files)
  14948. (org-set-local 'org-window-configuration cw)
  14949. (org-add-hook 'after-save-hook
  14950. (lambda ()
  14951. (set-window-configuration
  14952. (prog1 org-window-configuration
  14953. (kill-buffer (current-buffer))))
  14954. (org-install-agenda-files-menu)
  14955. (message "New agenda file list installed"))
  14956. nil 'local)
  14957. (message "%s" (substitute-command-keys
  14958. "Edit list and finish with \\[save-buffer]")))
  14959. (customize-variable 'org-agenda-files)))
  14960. (defun org-store-new-agenda-file-list (list)
  14961. "Set new value for the agenda file list and save it correctly."
  14962. (if (stringp org-agenda-files)
  14963. (let ((fe (org-read-agenda-file-list t)) b u)
  14964. (while (setq b (find-buffer-visiting org-agenda-files))
  14965. (kill-buffer b))
  14966. (with-temp-file org-agenda-files
  14967. (insert
  14968. (mapconcat
  14969. (lambda (f) ;; Keep un-expanded entries.
  14970. (if (setq u (assoc f fe))
  14971. (cdr u)
  14972. f))
  14973. list "\n")
  14974. "\n")))
  14975. (let ((org-mode-hook nil) (org-inhibit-startup t)
  14976. (org-insert-mode-line-in-empty-file nil))
  14977. (setq org-agenda-files list)
  14978. (customize-save-variable 'org-agenda-files org-agenda-files))))
  14979. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  14980. "Read the list of agenda files from a file.
  14981. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  14982. filenames, used by `org-store-new-agenda-file-list' to write back
  14983. un-expanded file names."
  14984. (when (file-directory-p org-agenda-files)
  14985. (error "`org-agenda-files' cannot be a single directory"))
  14986. (when (stringp org-agenda-files)
  14987. (with-temp-buffer
  14988. (insert-file-contents org-agenda-files)
  14989. (mapcar
  14990. (lambda (f)
  14991. (let ((e (expand-file-name (substitute-in-file-name f)
  14992. org-directory)))
  14993. (if pair-with-expansion
  14994. (cons e f)
  14995. e)))
  14996. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  14997. ;;;###autoload
  14998. (defun org-cycle-agenda-files ()
  14999. "Cycle through the files in `org-agenda-files'.
  15000. If the current buffer visits an agenda file, find the next one in the list.
  15001. If the current buffer does not, find the first agenda file."
  15002. (interactive)
  15003. (let* ((fs (org-agenda-files t))
  15004. (files (append fs (list (car fs))))
  15005. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15006. file)
  15007. (unless files (error "No agenda files"))
  15008. (catch 'exit
  15009. (while (setq file (pop files))
  15010. (if (equal (file-truename file) tcf)
  15011. (when (car files)
  15012. (find-file (car files))
  15013. (throw 'exit t))))
  15014. (find-file (car fs)))
  15015. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15016. (defun org-agenda-file-to-front (&optional to-end)
  15017. "Move/add the current file to the top of the agenda file list.
  15018. If the file is not present in the list, it is added to the front. If it is
  15019. present, it is moved there. With optional argument TO-END, add/move to the
  15020. end of the list."
  15021. (interactive "P")
  15022. (let ((org-agenda-skip-unavailable-files nil)
  15023. (file-alist (mapcar (lambda (x)
  15024. (cons (file-truename x) x))
  15025. (org-agenda-files t)))
  15026. (ctf (file-truename buffer-file-name))
  15027. x had)
  15028. (setq x (assoc ctf file-alist) had x)
  15029. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15030. (if to-end
  15031. (setq file-alist (append (delq x file-alist) (list x)))
  15032. (setq file-alist (cons x (delq x file-alist))))
  15033. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15034. (org-install-agenda-files-menu)
  15035. (message "File %s to %s of agenda file list"
  15036. (if had "moved" "added") (if to-end "end" "front"))))
  15037. (defun org-remove-file (&optional file)
  15038. "Remove current file from the list of files in variable `org-agenda-files'.
  15039. These are the files which are being checked for agenda entries.
  15040. Optional argument FILE means use this file instead of the current."
  15041. (interactive)
  15042. (let* ((org-agenda-skip-unavailable-files nil)
  15043. (file (or file buffer-file-name))
  15044. (true-file (file-truename file))
  15045. (afile (abbreviate-file-name file))
  15046. (files (delq nil (mapcar
  15047. (lambda (x)
  15048. (if (equal true-file
  15049. (file-truename x))
  15050. nil x))
  15051. (org-agenda-files t)))))
  15052. (if (not (= (length files) (length (org-agenda-files t))))
  15053. (progn
  15054. (org-store-new-agenda-file-list files)
  15055. (org-install-agenda-files-menu)
  15056. (message "Removed file: %s" afile))
  15057. (message "File was not in list: %s (not removed)" afile))))
  15058. (defun org-file-menu-entry (file)
  15059. (vector file (list 'find-file file) t))
  15060. (defun org-check-agenda-file (file)
  15061. "Make sure FILE exists. If not, ask user what to do."
  15062. (when (not (file-exists-p file))
  15063. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  15064. (abbreviate-file-name file))
  15065. (let ((r (downcase (read-char-exclusive))))
  15066. (cond
  15067. ((equal r ?r)
  15068. (org-remove-file file)
  15069. (throw 'nextfile t))
  15070. (t (error "Abort"))))))
  15071. (defun org-get-agenda-file-buffer (file)
  15072. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15073. it to the list of buffers which might be released later."
  15074. (let ((buf (org-find-base-buffer-visiting file)))
  15075. (if buf
  15076. buf ; just return it
  15077. ;; Make a new buffer and remember it
  15078. (setq buf (find-file-noselect file))
  15079. (if buf (push buf org-agenda-new-buffers))
  15080. buf)))
  15081. (defun org-release-buffers (blist)
  15082. "Release all buffers in list, asking the user for confirmation when needed.
  15083. When a buffer is unmodified, it is just killed. When modified, it is saved
  15084. \(if the user agrees) and then killed."
  15085. (let (buf file)
  15086. (while (setq buf (pop blist))
  15087. (setq file (buffer-file-name buf))
  15088. (when (and (buffer-modified-p buf)
  15089. file
  15090. (y-or-n-p (format "Save file %s? " file)))
  15091. (with-current-buffer buf (save-buffer)))
  15092. (kill-buffer buf))))
  15093. (defun org-prepare-agenda-buffers (files)
  15094. "Create buffers for all agenda files, protect archived trees and comments."
  15095. (interactive)
  15096. (let ((pa '(:org-archived t))
  15097. (pc '(:org-comment t))
  15098. (pall '(:org-archived t :org-comment t))
  15099. (inhibit-read-only t)
  15100. (rea (concat ":" org-archive-tag ":"))
  15101. bmp file re)
  15102. (save-excursion
  15103. (save-restriction
  15104. (while (setq file (pop files))
  15105. (catch 'nextfile
  15106. (if (bufferp file)
  15107. (set-buffer file)
  15108. (org-check-agenda-file file)
  15109. (set-buffer (org-get-agenda-file-buffer file)))
  15110. (widen)
  15111. (setq bmp (buffer-modified-p))
  15112. (org-refresh-category-properties)
  15113. (setq org-todo-keywords-for-agenda
  15114. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15115. (setq org-done-keywords-for-agenda
  15116. (append org-done-keywords-for-agenda org-done-keywords))
  15117. (setq org-todo-keyword-alist-for-agenda
  15118. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15119. (setq org-drawers-for-agenda
  15120. (append org-drawers-for-agenda org-drawers))
  15121. (setq org-tag-alist-for-agenda
  15122. (append org-tag-alist-for-agenda org-tag-alist))
  15123. (save-excursion
  15124. (remove-text-properties (point-min) (point-max) pall)
  15125. (when org-agenda-skip-archived-trees
  15126. (goto-char (point-min))
  15127. (while (re-search-forward rea nil t)
  15128. (if (org-at-heading-p t)
  15129. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15130. (goto-char (point-min))
  15131. (setq re (format org-heading-keyword-regexp-format
  15132. org-comment-string))
  15133. (while (re-search-forward re nil t)
  15134. (add-text-properties
  15135. (match-beginning 0) (org-end-of-subtree t) pc)))
  15136. (set-buffer-modified-p bmp)))))
  15137. (setq org-todo-keywords-for-agenda
  15138. (org-uniquify org-todo-keywords-for-agenda))
  15139. (setq org-todo-keyword-alist-for-agenda
  15140. (org-uniquify org-todo-keyword-alist-for-agenda)
  15141. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15142. ;;;; Embedded LaTeX
  15143. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15144. "Keymap for the minor `org-cdlatex-mode'.")
  15145. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15146. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15147. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15148. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15149. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15150. (defvar org-cdlatex-texmathp-advice-is-done nil
  15151. "Flag remembering if we have applied the advice to texmathp already.")
  15152. (define-minor-mode org-cdlatex-mode
  15153. "Toggle the minor `org-cdlatex-mode'.
  15154. This mode supports entering LaTeX environment and math in LaTeX fragments
  15155. in Org-mode.
  15156. \\{org-cdlatex-mode-map}"
  15157. nil " OCDL" nil
  15158. (when org-cdlatex-mode
  15159. (require 'cdlatex)
  15160. (run-hooks 'cdlatex-mode-hook)
  15161. (cdlatex-compute-tables))
  15162. (unless org-cdlatex-texmathp-advice-is-done
  15163. (setq org-cdlatex-texmathp-advice-is-done t)
  15164. (defadvice texmathp (around org-math-always-on activate)
  15165. "Always return t in org-mode buffers.
  15166. This is because we want to insert math symbols without dollars even outside
  15167. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15168. an embedded LaTeX fragment, let texmathp do its job.
  15169. \\[org-cdlatex-mode-map]"
  15170. (interactive)
  15171. (let (p)
  15172. (cond
  15173. ((not (eq major-mode 'org-mode)) ad-do-it)
  15174. ((eq this-command 'cdlatex-math-symbol)
  15175. (setq ad-return-value t
  15176. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15177. (t
  15178. (let ((p (org-inside-LaTeX-fragment-p)))
  15179. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15180. (setq ad-return-value t
  15181. texmathp-why '("Org-mode embedded math" . 0))
  15182. (if p ad-do-it)))))))))
  15183. (defun turn-on-org-cdlatex ()
  15184. "Unconditionally turn on `org-cdlatex-mode'."
  15185. (org-cdlatex-mode 1))
  15186. (defun org-inside-LaTeX-fragment-p ()
  15187. "Test if point is inside a LaTeX fragment.
  15188. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15189. sequence appearing also before point.
  15190. Even though the matchers for math are configurable, this function assumes
  15191. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15192. delimiters are skipped when they have been removed by customization.
  15193. The return value is nil, or a cons cell with the delimiter and the
  15194. position of this delimiter.
  15195. This function does a reasonably good job, but can locally be fooled by
  15196. for example currency specifications. For example it will assume being in
  15197. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15198. fragments that are properly closed, but during editing, we have to live
  15199. with the uncertainty caused by missing closing delimiters. This function
  15200. looks only before point, not after."
  15201. (catch 'exit
  15202. (let ((pos (point))
  15203. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15204. (lim (progn
  15205. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15206. (point)))
  15207. dd-on str (start 0) m re)
  15208. (goto-char pos)
  15209. (when dodollar
  15210. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15211. re (nth 1 (assoc "$" org-latex-regexps)))
  15212. (while (string-match re str start)
  15213. (cond
  15214. ((= (match-end 0) (length str))
  15215. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15216. ((= (match-end 0) (- (length str) 5))
  15217. (throw 'exit nil))
  15218. (t (setq start (match-end 0))))))
  15219. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15220. (goto-char pos)
  15221. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15222. (and (match-beginning 2) (throw 'exit nil))
  15223. ;; count $$
  15224. (while (re-search-backward "\\$\\$" lim t)
  15225. (setq dd-on (not dd-on)))
  15226. (goto-char pos)
  15227. (if dd-on (cons "$$" m))))))
  15228. (defun org-inside-latex-macro-p ()
  15229. "Is point inside a LaTeX macro or its arguments?"
  15230. (save-match-data
  15231. (org-in-regexp
  15232. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15233. (defun org-try-cdlatex-tab ()
  15234. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15235. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15236. - inside a LaTeX fragment, or
  15237. - after the first word in a line, where an abbreviation expansion could
  15238. insert a LaTeX environment."
  15239. (when org-cdlatex-mode
  15240. (cond
  15241. ;; Before any word on the line: No expansion possible.
  15242. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15243. ;; Just after first word on the line: Expand it. Make sure it
  15244. ;; cannot happen on headlines, though.
  15245. ((save-excursion
  15246. (skip-chars-backward "a-zA-Z0-9*")
  15247. (skip-chars-backward " \t")
  15248. (and (bolp) (not (org-at-heading-p))))
  15249. (cdlatex-tab) t)
  15250. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15251. (defun org-cdlatex-underscore-caret (&optional arg)
  15252. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15253. Revert to the normal definition outside of these fragments."
  15254. (interactive "P")
  15255. (if (org-inside-LaTeX-fragment-p)
  15256. (call-interactively 'cdlatex-sub-superscript)
  15257. (let (org-cdlatex-mode)
  15258. (call-interactively (key-binding (vector last-input-event))))))
  15259. (defun org-cdlatex-math-modify (&optional arg)
  15260. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15261. Revert to the normal definition outside of these fragments."
  15262. (interactive "P")
  15263. (if (org-inside-LaTeX-fragment-p)
  15264. (call-interactively 'cdlatex-math-modify)
  15265. (let (org-cdlatex-mode)
  15266. (call-interactively (key-binding (vector last-input-event))))))
  15267. (defvar org-latex-fragment-image-overlays nil
  15268. "List of overlays carrying the images of latex fragments.")
  15269. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15270. (defun org-remove-latex-fragment-image-overlays ()
  15271. "Remove all overlays with LaTeX fragment images in current buffer."
  15272. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15273. (setq org-latex-fragment-image-overlays nil))
  15274. (defun org-preview-latex-fragment (&optional subtree)
  15275. "Preview the LaTeX fragment at point, or all locally or globally.
  15276. If the cursor is in a LaTeX fragment, create the image and overlay
  15277. it over the source code. If there is no fragment at point, display
  15278. all fragments in the current text, from one headline to the next. With
  15279. prefix SUBTREE, display all fragments in the current subtree. With a
  15280. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15281. the cursor is before the first headline,
  15282. display all fragments in the buffer.
  15283. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15284. (interactive "P")
  15285. (org-remove-latex-fragment-image-overlays)
  15286. (save-excursion
  15287. (save-restriction
  15288. (let (beg end at msg)
  15289. (cond
  15290. ((or (equal subtree '(16))
  15291. (not (save-excursion
  15292. (re-search-backward org-outline-regexp-bol nil t))))
  15293. (setq beg (point-min) end (point-max)
  15294. msg "Creating images for buffer...%s"))
  15295. ((equal subtree '(4))
  15296. (org-back-to-heading)
  15297. (setq beg (point) end (org-end-of-subtree t)
  15298. msg "Creating images for subtree...%s"))
  15299. (t
  15300. (if (setq at (org-inside-LaTeX-fragment-p))
  15301. (goto-char (max (point-min) (- (cdr at) 2)))
  15302. (org-back-to-heading))
  15303. (setq beg (point) end (progn (outline-next-heading) (point))
  15304. msg (if at "Creating image...%s"
  15305. "Creating images for entry...%s"))))
  15306. (message msg "")
  15307. (narrow-to-region beg end)
  15308. (goto-char beg)
  15309. (org-format-latex
  15310. (concat "ltxpng/" (file-name-sans-extension
  15311. (file-name-nondirectory
  15312. buffer-file-name)))
  15313. default-directory 'overlays msg at 'forbuffer 'dvipng)
  15314. (message msg "done. Use `C-c C-c' to remove images.")))))
  15315. (defvar org-latex-regexps
  15316. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15317. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15318. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15319. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15320. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15321. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15322. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15323. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15324. "Regular expressions for matching embedded LaTeX.")
  15325. (defvar org-export-have-math nil) ;; dynamic scoping
  15326. (defun org-format-latex (prefix &optional dir overlays msg at
  15327. forbuffer processing-type)
  15328. "Replace LaTeX fragments with links to an image, and produce images.
  15329. Some of the options can be changed using the variable
  15330. `org-format-latex-options'."
  15331. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15332. (let* ((prefixnodir (file-name-nondirectory prefix))
  15333. (absprefix (expand-file-name prefix dir))
  15334. (todir (file-name-directory absprefix))
  15335. (opt org-format-latex-options)
  15336. (matchers (plist-get opt :matchers))
  15337. (re-list org-latex-regexps)
  15338. (org-format-latex-header-extra
  15339. (plist-get (org-infile-export-plist) :latex-header-extra))
  15340. (cnt 0) txt hash link beg end re e checkdir
  15341. executables-checked string
  15342. m n block-type block linkfile movefile ov)
  15343. ;; Check the different regular expressions
  15344. (while (setq e (pop re-list))
  15345. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15346. block (if block-type "\n\n" ""))
  15347. (when (member m matchers)
  15348. (goto-char (point-min))
  15349. (while (re-search-forward re nil t)
  15350. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15351. (not (get-text-property (match-beginning n)
  15352. 'org-protected))
  15353. (or (not overlays)
  15354. (not (eq (get-char-property (match-beginning n)
  15355. 'org-overlay-type)
  15356. 'org-latex-overlay))))
  15357. (setq org-export-have-math t)
  15358. (cond
  15359. ((eq processing-type 'verbatim)
  15360. ;; Leave the text verbatim, just protect it
  15361. (add-text-properties (match-beginning n) (match-end n)
  15362. '(org-protected t)))
  15363. ((eq processing-type 'mathjax)
  15364. ;; Prepare for MathJax processing
  15365. (setq string (match-string n))
  15366. (if (member m '("$" "$1"))
  15367. (save-excursion
  15368. (delete-region (match-beginning n) (match-end n))
  15369. (goto-char (match-beginning n))
  15370. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15371. "\\)")
  15372. '(org-protected t))))
  15373. (add-text-properties (match-beginning n) (match-end n)
  15374. '(org-protected t))))
  15375. ((eq processing-type 'dvipng)
  15376. ;; Process to an image
  15377. (setq txt (match-string n)
  15378. beg (match-beginning n) end (match-end n)
  15379. cnt (1+ cnt))
  15380. (let (print-length print-level) ; make sure full list is printed
  15381. (setq hash (sha1 (prin1-to-string
  15382. (list org-format-latex-header
  15383. org-format-latex-header-extra
  15384. org-export-latex-default-packages-alist
  15385. org-export-latex-packages-alist
  15386. org-format-latex-options
  15387. forbuffer txt)))
  15388. linkfile (format "%s_%s.png" prefix hash)
  15389. movefile (format "%s_%s.png" absprefix hash)))
  15390. (setq link (concat block "[[file:" linkfile "]]" block))
  15391. (if msg (message msg cnt))
  15392. (goto-char beg)
  15393. (unless checkdir ; make sure the directory exists
  15394. (setq checkdir t)
  15395. (or (file-directory-p todir) (make-directory todir t)))
  15396. (unless executables-checked
  15397. (org-check-external-command
  15398. "latex" "needed to convert LaTeX fragments to images")
  15399. (org-check-external-command
  15400. "dvipng" "needed to convert LaTeX fragments to images")
  15401. (setq executables-checked t))
  15402. (unless (file-exists-p movefile)
  15403. (org-create-formula-image
  15404. txt movefile opt forbuffer))
  15405. (if overlays
  15406. (progn
  15407. (mapc (lambda (o)
  15408. (if (eq (overlay-get o 'org-overlay-type)
  15409. 'org-latex-overlay)
  15410. (delete-overlay o)))
  15411. (overlays-in beg end))
  15412. (setq ov (make-overlay beg end))
  15413. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15414. (if (featurep 'xemacs)
  15415. (progn
  15416. (overlay-put ov 'invisible t)
  15417. (overlay-put
  15418. ov 'end-glyph
  15419. (make-glyph (vector 'png :file movefile))))
  15420. (overlay-put
  15421. ov 'display
  15422. (list 'image :type 'png :file movefile :ascent 'center)))
  15423. (push ov org-latex-fragment-image-overlays)
  15424. (goto-char end))
  15425. (delete-region beg end)
  15426. (insert (org-add-props link
  15427. (list 'org-latex-src
  15428. (replace-regexp-in-string
  15429. "\"" "" txt)
  15430. 'org-latex-src-embed-type
  15431. (if block-type 'paragraph 'character))))))
  15432. ((eq processing-type 'mathml)
  15433. ;; Process to MathML
  15434. (unless executables-checked
  15435. (unless (save-match-data (org-format-latex-mathml-available-p))
  15436. (error "LaTeX to MathML converter not configured"))
  15437. (setq executables-checked t))
  15438. (setq txt (match-string n)
  15439. beg (match-beginning n) end (match-end n)
  15440. cnt (1+ cnt))
  15441. (if msg (message msg cnt))
  15442. (goto-char beg)
  15443. (delete-region beg end)
  15444. (insert (org-format-latex-as-mathml
  15445. txt block-type prefix dir)))
  15446. (t
  15447. (error "Unknown conversion type %s for latex fragments"
  15448. processing-type)))))))))
  15449. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15450. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15451. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15452. sucessful, return the portion between \"<math...> </math>\"
  15453. elements otherwise return nil. When MATHML-FILE is specified,
  15454. write the results in to that file. When invoked as an
  15455. interactive command, prompt for LATEX-FRAG, with initial value
  15456. set to the current active region and echo the results for user
  15457. inspection."
  15458. (interactive (list (let ((frag (when (region-active-p)
  15459. (buffer-substring-no-properties
  15460. (region-beginning) (region-end)))))
  15461. (read-string "LaTeX Fragment: " frag nil frag))))
  15462. (unless latex-frag (error "Invalid latex-frag"))
  15463. (let* ((tmp-in-file (file-relative-name
  15464. (make-temp-name (expand-file-name "ltxmathml-in"))))
  15465. (ignore (write-region latex-frag nil tmp-in-file))
  15466. (tmp-out-file (file-relative-name
  15467. (make-temp-name (expand-file-name "ltxmathml-out"))))
  15468. (cmd (format-spec
  15469. org-latex-to-mathml-convert-command
  15470. `((?j . ,(shell-quote-argument
  15471. (expand-file-name org-latex-to-mathml-jar-file)))
  15472. (?I . ,(shell-quote-argument tmp-in-file))
  15473. (?o . ,(shell-quote-argument tmp-out-file)))))
  15474. mathml shell-command-output)
  15475. (when (org-called-interactively-p 'any)
  15476. (unless (org-format-latex-mathml-available-p)
  15477. (error "LaTeX to MathML converter not configured")))
  15478. (message "Running %s" cmd)
  15479. (setq shell-command-output (shell-command-to-string cmd))
  15480. (setq mathml
  15481. (when (file-readable-p tmp-out-file)
  15482. (with-current-buffer (find-file-noselect tmp-out-file t)
  15483. (goto-char (point-min))
  15484. (when (re-search-forward
  15485. (concat
  15486. (regexp-quote
  15487. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  15488. "\\(.\\|\n\\)*"
  15489. (regexp-quote "</math>")) nil t)
  15490. (prog1 (match-string 0) (kill-buffer))))))
  15491. (cond
  15492. (mathml
  15493. (setq mathml
  15494. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  15495. (when mathml-file
  15496. (write-region mathml nil mathml-file))
  15497. (when (org-called-interactively-p 'any)
  15498. (message mathml)))
  15499. ((message "LaTeX to MathML conversion failed")
  15500. (message shell-command-output)))
  15501. (delete-file tmp-in-file)
  15502. (when (file-exists-p tmp-out-file)
  15503. (delete-file tmp-out-file))
  15504. mathml))
  15505. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  15506. prefix &optional dir)
  15507. "Use `org-create-math-formula' but check local cache first."
  15508. (let* ((absprefix (expand-file-name prefix dir))
  15509. (print-length nil) (print-level nil)
  15510. (formula-id (concat
  15511. "formula-"
  15512. (sha1
  15513. (prin1-to-string
  15514. (list latex-frag
  15515. org-latex-to-mathml-convert-command)))))
  15516. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  15517. (formula-cache-dir (file-name-directory formula-cache)))
  15518. (unless (file-directory-p formula-cache-dir)
  15519. (make-directory formula-cache-dir t))
  15520. (unless (file-exists-p formula-cache)
  15521. (org-create-math-formula latex-frag formula-cache))
  15522. (if (file-exists-p formula-cache)
  15523. ;; Successful conversion. Return the link to MathML file.
  15524. (org-add-props
  15525. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  15526. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  15527. 'org-latex-src-embed-type (if latex-frag-type
  15528. 'paragraph 'character)))
  15529. ;; Failed conversion. Return the LaTeX fragment verbatim
  15530. (add-text-properties
  15531. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  15532. latex-frag)))
  15533. ;; This function borrows from Ganesh Swami's latex2png.el
  15534. (defun org-create-formula-image (string tofile options buffer)
  15535. "This calls dvipng."
  15536. (require 'org-latex)
  15537. (let* ((tmpdir (if (featurep 'xemacs)
  15538. (temp-directory)
  15539. temporary-file-directory))
  15540. (texfilebase (make-temp-name
  15541. (expand-file-name "orgtex" tmpdir)))
  15542. (texfile (concat texfilebase ".tex"))
  15543. (dvifile (concat texfilebase ".dvi"))
  15544. (pngfile (concat texfilebase ".png"))
  15545. (fnh (if (featurep 'xemacs)
  15546. (font-height (get-face-font 'default))
  15547. (face-attribute 'default :height nil)))
  15548. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15549. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15550. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15551. "Black"))
  15552. (bg (or (plist-get options (if buffer :background :html-background))
  15553. "Transparent")))
  15554. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  15555. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  15556. (with-temp-file texfile
  15557. (insert (org-splice-latex-header
  15558. org-format-latex-header
  15559. org-export-latex-default-packages-alist
  15560. org-export-latex-packages-alist t
  15561. org-format-latex-header-extra))
  15562. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  15563. (require 'org-latex)
  15564. (org-export-latex-fix-inputenc))
  15565. (let ((dir default-directory))
  15566. (condition-case nil
  15567. (progn
  15568. (cd tmpdir)
  15569. (call-process "latex" nil nil nil texfile))
  15570. (error nil))
  15571. (cd dir))
  15572. (if (not (file-exists-p dvifile))
  15573. (progn (message "Failed to create dvi file from %s" texfile) nil)
  15574. (condition-case nil
  15575. (call-process "dvipng" nil nil nil
  15576. "-fg" fg "-bg" bg
  15577. "-D" dpi
  15578. ;;"-x" scale "-y" scale
  15579. "-T" "tight"
  15580. "-o" pngfile
  15581. dvifile)
  15582. (error nil))
  15583. (if (not (file-exists-p pngfile))
  15584. (if org-format-latex-signal-error
  15585. (error "Failed to create png file from %s" texfile)
  15586. (message "Failed to create png file from %s" texfile)
  15587. nil)
  15588. ;; Use the requested file name and clean up
  15589. (copy-file pngfile tofile 'replace)
  15590. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  15591. (delete-file (concat texfilebase e)))
  15592. pngfile))))
  15593. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  15594. "Fill a LaTeX header template TPL.
  15595. In the template, the following place holders will be recognized:
  15596. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  15597. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  15598. [PACKAGES] \\usepackage statements for PKG
  15599. [NO-PACKAGES] do not include PKG
  15600. [EXTRA] the string EXTRA
  15601. [NO-EXTRA] do not include EXTRA
  15602. For backward compatibility, if both the positive and the negative place
  15603. holder is missing, the positive one (without the \"NO-\") will be
  15604. assumed to be present at the end of the template.
  15605. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  15606. EXTRA is a string.
  15607. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  15608. (let (rpl (end ""))
  15609. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  15610. (setq rpl (if (or (match-end 1) (not def-pkg))
  15611. "" (org-latex-packages-to-string def-pkg snippets-p t))
  15612. tpl (replace-match rpl t t tpl))
  15613. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  15614. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  15615. (setq rpl (if (or (match-end 1) (not pkg))
  15616. "" (org-latex-packages-to-string pkg snippets-p t))
  15617. tpl (replace-match rpl t t tpl))
  15618. (if pkg (setq end
  15619. (concat end "\n"
  15620. (org-latex-packages-to-string pkg snippets-p)))))
  15621. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  15622. (setq rpl (if (or (match-end 1) (not extra))
  15623. "" (concat extra "\n"))
  15624. tpl (replace-match rpl t t tpl))
  15625. (if (and extra (string-match "\\S-" extra))
  15626. (setq end (concat end "\n" extra))))
  15627. (if (string-match "\\S-" end)
  15628. (concat tpl "\n" end)
  15629. tpl)))
  15630. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  15631. "Turn an alist of packages into a string with the \\usepackage macros."
  15632. (setq pkg (mapconcat (lambda(p)
  15633. (cond
  15634. ((stringp p) p)
  15635. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  15636. (format "%% Package %s omitted" (cadr p)))
  15637. ((equal "" (car p))
  15638. (format "\\usepackage{%s}" (cadr p)))
  15639. (t
  15640. (format "\\usepackage[%s]{%s}"
  15641. (car p) (cadr p)))))
  15642. pkg
  15643. "\n"))
  15644. (if newline (concat pkg "\n") pkg))
  15645. (defun org-dvipng-color (attr)
  15646. "Return an rgb color specification for dvipng."
  15647. (apply 'format "rgb %s %s %s"
  15648. (mapcar 'org-normalize-color
  15649. (color-values (face-attribute 'default attr nil)))))
  15650. (defun org-normalize-color (value)
  15651. "Return string to be used as color value for an RGB component."
  15652. (format "%g" (/ value 65535.0)))
  15653. ;; Image display
  15654. (defvar org-inline-image-overlays nil)
  15655. (make-variable-buffer-local 'org-inline-image-overlays)
  15656. (defun org-toggle-inline-images (&optional include-linked)
  15657. "Toggle the display of inline images.
  15658. INCLUDE-LINKED is passed to `org-display-inline-images'."
  15659. (interactive "P")
  15660. (if org-inline-image-overlays
  15661. (progn
  15662. (org-remove-inline-images)
  15663. (message "Inline image display turned off"))
  15664. (org-display-inline-images include-linked)
  15665. (if org-inline-image-overlays
  15666. (message "%d images displayed inline"
  15667. (length org-inline-image-overlays))
  15668. (message "No images to display inline"))))
  15669. (defun org-display-inline-images (&optional include-linked refresh beg end)
  15670. "Display inline images.
  15671. Normally only links without a description part are inlined, because this
  15672. is how it will work for export. When INCLUDE-LINKED is set, also links
  15673. with a description part will be inlined. This can be nice for a quick
  15674. look at those images, but it does not reflect what exported files will look
  15675. like.
  15676. When REFRESH is set, refresh existing images between BEG and END.
  15677. This will create new image displays only if necessary.
  15678. BEG and END default to the buffer boundaries."
  15679. (interactive "P")
  15680. (unless refresh
  15681. (org-remove-inline-images)
  15682. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  15683. (save-excursion
  15684. (save-restriction
  15685. (widen)
  15686. (setq beg (or beg (point-min)) end (or end (point-max)))
  15687. (goto-char (point-min))
  15688. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  15689. (substring (org-image-file-name-regexp) 0 -2)
  15690. "\\)\\]" (if include-linked "" "\\]")))
  15691. old file ov img)
  15692. (while (re-search-forward re end t)
  15693. (setq old (get-char-property-and-overlay (match-beginning 1)
  15694. 'org-image-overlay))
  15695. (setq file (expand-file-name
  15696. (concat (or (match-string 3) "") (match-string 4))))
  15697. (when (file-exists-p file)
  15698. (if (and (car-safe old) refresh)
  15699. (image-refresh (overlay-get (cdr old) 'display))
  15700. (setq img (save-match-data (create-image file)))
  15701. (when img
  15702. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  15703. (overlay-put ov 'display img)
  15704. (overlay-put ov 'face 'default)
  15705. (overlay-put ov 'org-image-overlay t)
  15706. (overlay-put ov 'modification-hooks
  15707. (list 'org-display-inline-modification-hook))
  15708. (push ov org-inline-image-overlays)))))))))
  15709. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  15710. "Remove inline-display overlay if a corresponding region is modified."
  15711. (let ((inhibit-modification-hooks t))
  15712. (when (and ov after)
  15713. (delete ov org-inline-image-overlays)
  15714. (delete-overlay ov))))
  15715. (defun org-remove-inline-images ()
  15716. "Remove inline display of images."
  15717. (interactive)
  15718. (mapc 'delete-overlay org-inline-image-overlays)
  15719. (setq org-inline-image-overlays nil))
  15720. ;;;; Key bindings
  15721. ;; Outline functions from `outline-mode-prefix-map'
  15722. ;; that can be remapped in Org:
  15723. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  15724. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  15725. (define-key org-mode-map [remap outline-forward-same-level]
  15726. 'org-forward-same-level)
  15727. (define-key org-mode-map [remap outline-backward-same-level]
  15728. 'org-backward-same-level)
  15729. (define-key org-mode-map [remap show-branches]
  15730. 'org-kill-note-or-show-branches)
  15731. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  15732. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  15733. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  15734. ;; Outline functions from `outline-mode-prefix-map'
  15735. ;; that can not be remapped in Org:
  15736. ;; - the column "key binding" shows whether the Outline function is still
  15737. ;; available in Org mode on the same key that it has been bound to in
  15738. ;; Outline mode:
  15739. ;; - "overridden": key used for a different functionality in Org mode
  15740. ;; - else: key still bound to the same Outline function in Org mode
  15741. ;; | Outline function | key binding | Org replacement |
  15742. ;; |------------------------------------+-------------+-----------------------|
  15743. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  15744. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  15745. ;; | `show-children' | `C-c C-i' | visibility cycling |
  15746. ;; | `hide-subtree' | overridden | visibility cycling |
  15747. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  15748. ;; | `hide-body' | overridden | no replacement |
  15749. ;; | `show-all' | overridden | no replacement |
  15750. ;; | `hide-entry' | overridden | visibility cycling |
  15751. ;; | `show-entry' | overridden | no replacement |
  15752. ;; | `hide-leaves' | overridden | no replacement |
  15753. ;; | `hide-sublevels' | overridden | no replacement |
  15754. ;; | `hide-other' | overridden | no replacement |
  15755. ;; | `outline-move-subtree-up' | `C-c C-^' | better: org-shiftup |
  15756. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  15757. ;; Make `C-c C-x' a prefix key
  15758. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  15759. ;; TAB key with modifiers
  15760. (org-defkey org-mode-map "\C-i" 'org-cycle)
  15761. (org-defkey org-mode-map [(tab)] 'org-cycle)
  15762. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  15763. (org-defkey org-mode-map [(meta tab)] 'pcomplete)
  15764. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  15765. (org-defkey org-mode-map "\M-\C-i" 'pcomplete)
  15766. ;; The following line is necessary under Suse GNU/Linux
  15767. (unless (featurep 'xemacs)
  15768. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  15769. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  15770. (define-key org-mode-map [backtab] 'org-shifttab)
  15771. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  15772. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  15773. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  15774. ;; Cursor keys with modifiers
  15775. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  15776. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  15777. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  15778. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  15779. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  15780. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  15781. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  15782. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  15783. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  15784. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  15785. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  15786. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  15787. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  15788. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  15789. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  15790. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  15791. ;; Babel keys
  15792. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  15793. (mapc (lambda (pair)
  15794. (define-key org-babel-map (car pair) (cdr pair)))
  15795. org-babel-key-bindings)
  15796. ;;; Extra keys for tty access.
  15797. ;; We only set them when really needed because otherwise the
  15798. ;; menus don't show the simple keys
  15799. (when (or org-use-extra-keys
  15800. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  15801. (not window-system))
  15802. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  15803. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  15804. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  15805. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  15806. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  15807. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  15808. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  15809. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  15810. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  15811. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  15812. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  15813. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  15814. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  15815. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  15816. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  15817. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  15818. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  15819. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  15820. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  15821. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  15822. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  15823. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  15824. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  15825. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  15826. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  15827. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  15828. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  15829. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  15830. ;; All the other keys
  15831. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  15832. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  15833. (if (boundp 'narrow-map)
  15834. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  15835. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  15836. (if (boundp 'narrow-map)
  15837. (org-defkey narrow-map "b" 'org-narrow-to-block)
  15838. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  15839. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  15840. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  15841. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  15842. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  15843. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  15844. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  15845. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  15846. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  15847. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  15848. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  15849. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  15850. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  15851. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  15852. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  15853. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  15854. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  15855. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  15856. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  15857. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  15858. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  15859. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  15860. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  15861. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  15862. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  15863. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  15864. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  15865. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  15866. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  15867. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  15868. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  15869. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  15870. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  15871. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  15872. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  15873. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  15874. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  15875. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  15876. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  15877. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  15878. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  15879. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  15880. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15881. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  15882. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  15883. (org-defkey org-mode-map "\C-c^" 'org-sort)
  15884. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  15885. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  15886. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  15887. (org-defkey org-mode-map "\C-m" 'org-return)
  15888. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  15889. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  15890. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  15891. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  15892. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  15893. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  15894. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  15895. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  15896. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  15897. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  15898. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  15899. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  15900. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  15901. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  15902. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  15903. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  15904. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  15905. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  15906. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  15907. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  15908. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  15909. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  15910. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  15911. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  15912. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  15913. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  15914. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  15915. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  15916. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  15917. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  15918. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  15919. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  15920. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  15921. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  15922. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  15923. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  15924. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  15925. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  15926. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  15927. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  15928. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  15929. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  15930. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  15931. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  15932. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  15933. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  15934. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  15935. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  15936. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  15937. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  15938. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  15939. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  15940. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  15941. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  15942. (when (featurep 'xemacs)
  15943. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  15944. (defconst org-speed-commands-default
  15945. '(
  15946. ("Outline Navigation")
  15947. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  15948. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  15949. ("f" . (org-speed-move-safe 'org-forward-same-level))
  15950. ("b" . (org-speed-move-safe 'org-backward-same-level))
  15951. ("u" . (org-speed-move-safe 'outline-up-heading))
  15952. ("j" . org-goto)
  15953. ("g" . (org-refile t))
  15954. ("Outline Visibility")
  15955. ("c" . org-cycle)
  15956. ("C" . org-shifttab)
  15957. (" " . org-display-outline-path)
  15958. ("Outline Structure Editing")
  15959. ("U" . org-shiftmetaup)
  15960. ("D" . org-shiftmetadown)
  15961. ("r" . org-metaright)
  15962. ("l" . org-metaleft)
  15963. ("R" . org-shiftmetaright)
  15964. ("L" . org-shiftmetaleft)
  15965. ("i" . (progn (forward-char 1) (call-interactively
  15966. 'org-insert-heading-respect-content)))
  15967. ("^" . org-sort)
  15968. ("w" . org-refile)
  15969. ("a" . org-archive-subtree-default-with-confirmation)
  15970. ("." . org-mark-subtree)
  15971. ("Clock Commands")
  15972. ("I" . org-clock-in)
  15973. ("O" . org-clock-out)
  15974. ("Meta Data Editing")
  15975. ("t" . org-todo)
  15976. ("0" . (org-priority ?\ ))
  15977. ("1" . (org-priority ?A))
  15978. ("2" . (org-priority ?B))
  15979. ("3" . (org-priority ?C))
  15980. (";" . org-set-tags-command)
  15981. ("e" . org-set-effort)
  15982. ("Agenda Views etc")
  15983. ("v" . org-agenda)
  15984. ("/" . org-sparse-tree)
  15985. ("Misc")
  15986. ("o" . org-open-at-point)
  15987. ("?" . org-speed-command-help)
  15988. ("<" . (org-agenda-set-restriction-lock 'subtree))
  15989. (">" . (org-agenda-remove-restriction-lock))
  15990. )
  15991. "The default speed commands.")
  15992. (defun org-print-speed-command (e)
  15993. (if (> (length (car e)) 1)
  15994. (progn
  15995. (princ "\n")
  15996. (princ (car e))
  15997. (princ "\n")
  15998. (princ (make-string (length (car e)) ?-))
  15999. (princ "\n"))
  16000. (princ (car e))
  16001. (princ " ")
  16002. (if (symbolp (cdr e))
  16003. (princ (symbol-name (cdr e)))
  16004. (prin1 (cdr e)))
  16005. (princ "\n")))
  16006. (defun org-speed-command-help ()
  16007. "Show the available speed commands."
  16008. (interactive)
  16009. (if (not org-use-speed-commands)
  16010. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16011. (with-output-to-temp-buffer "*Help*"
  16012. (princ "User-defined Speed commands\n===========================\n")
  16013. (mapc 'org-print-speed-command org-speed-commands-user)
  16014. (princ "\n")
  16015. (princ "Built-in Speed commands\n=======================\n")
  16016. (mapc 'org-print-speed-command org-speed-commands-default))
  16017. (with-current-buffer "*Help*"
  16018. (setq truncate-lines t))))
  16019. (defun org-speed-move-safe (cmd)
  16020. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16021. If not, return to the original position and throw an error."
  16022. (interactive)
  16023. (let ((pos (point)))
  16024. (call-interactively cmd)
  16025. (unless (and (bolp) (org-at-heading-p))
  16026. (goto-char pos)
  16027. (error "Boundary reached while executing %s" cmd))))
  16028. (defvar org-self-insert-command-undo-counter 0)
  16029. (defvar org-table-auto-blank-field) ; defined in org-table.el
  16030. (defvar org-speed-command nil)
  16031. (defun org-speed-command-default-hook (keys)
  16032. "Hook for activating single-letter speed commands.
  16033. `org-speed-commands-default' specifies a minimal command set.
  16034. Use `org-speed-commands-user' for further customization."
  16035. (when (or (and (bolp) (looking-at org-outline-regexp))
  16036. (and (functionp org-use-speed-commands)
  16037. (funcall org-use-speed-commands)))
  16038. (cdr (assoc keys (append org-speed-commands-user
  16039. org-speed-commands-default)))))
  16040. (defun org-babel-speed-command-hook (keys)
  16041. "Hook for activating single-letter code block commands."
  16042. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  16043. (cdr (assoc keys org-babel-key-bindings))))
  16044. (defcustom org-speed-command-hook
  16045. '(org-speed-command-default-hook org-babel-speed-command-hook)
  16046. "Hook for activating speed commands at strategic locations.
  16047. Hook functions are called in sequence until a valid handler is
  16048. found.
  16049. Each hook takes a single argument, a user-pressed command key
  16050. which is also a `self-insert-command' from the global map.
  16051. Within the hook, examine the cursor position and the command key
  16052. and return nil or a valid handler as appropriate. Handler could
  16053. be one of an interactive command, a function, or a form.
  16054. Set `org-use-speed-commands' to non-nil value to enable this
  16055. hook. The default setting is `org-speed-command-default-hook'."
  16056. :group 'org-structure
  16057. :type 'hook)
  16058. (defun org-self-insert-command (N)
  16059. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16060. If the cursor is in a table looking at whitespace, the whitespace is
  16061. overwritten, and the table is not marked as requiring realignment."
  16062. (interactive "p")
  16063. (org-check-before-invisible-edit 'insert)
  16064. (cond
  16065. ((and org-use-speed-commands
  16066. (setq org-speed-command
  16067. (run-hook-with-args-until-success
  16068. 'org-speed-command-hook (this-command-keys))))
  16069. (cond
  16070. ((commandp org-speed-command)
  16071. (setq this-command org-speed-command)
  16072. (call-interactively org-speed-command))
  16073. ((functionp org-speed-command)
  16074. (funcall org-speed-command))
  16075. ((and org-speed-command (listp org-speed-command))
  16076. (eval org-speed-command))
  16077. (t (let (org-use-speed-commands)
  16078. (call-interactively 'org-self-insert-command)))))
  16079. ((and
  16080. (org-table-p)
  16081. (progn
  16082. ;; check if we blank the field, and if that triggers align
  16083. (and (featurep 'org-table) org-table-auto-blank-field
  16084. (member last-command
  16085. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16086. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16087. ;; got extra space, this field does not determine column width
  16088. (let (org-table-may-need-update) (org-table-blank-field))
  16089. ;; no extra space, this field may determine column width
  16090. (org-table-blank-field)))
  16091. t)
  16092. (eq N 1)
  16093. (looking-at "[^|\n]* |"))
  16094. (let (org-table-may-need-update)
  16095. (goto-char (1- (match-end 0)))
  16096. (delete-char -1)
  16097. (goto-char (match-beginning 0))
  16098. (self-insert-command N)))
  16099. (t
  16100. (setq org-table-may-need-update t)
  16101. (self-insert-command N)
  16102. (org-fix-tags-on-the-fly)
  16103. (if org-self-insert-cluster-for-undo
  16104. (if (not (eq last-command 'org-self-insert-command))
  16105. (setq org-self-insert-command-undo-counter 1)
  16106. (if (>= org-self-insert-command-undo-counter 20)
  16107. (setq org-self-insert-command-undo-counter 1)
  16108. (and (> org-self-insert-command-undo-counter 0)
  16109. buffer-undo-list (listp buffer-undo-list)
  16110. (not (cadr buffer-undo-list)) ; remove nil entry
  16111. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16112. (setq org-self-insert-command-undo-counter
  16113. (1+ org-self-insert-command-undo-counter))))))))
  16114. (defun org-check-before-invisible-edit (kind)
  16115. "Check is editing if kind KIND would be dangerous with invisible text around.
  16116. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16117. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16118. (if (and org-catch-invisible-edits
  16119. (or (not (boundp 'visible-mode)) (not visible-mode))
  16120. (or (get-char-property (point) 'invisible)
  16121. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16122. ;; OK, we need to take a closer look
  16123. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16124. (invisible-before-point (if (bobp) nil (get-char-property
  16125. (1- (point)) 'invisible)))
  16126. (border-and-ok-direction
  16127. (or
  16128. ;; Check if we are acting predictably before invisible text
  16129. (and invisible-at-point (not invisible-before-point)
  16130. (memq kind '(insert delete-backward)))
  16131. ;; Check if we are acting predictably after invisible text
  16132. ;; This works not well, and I have turned it off. It seems
  16133. ;; better to always show and stop after invisible text.
  16134. ;; (and (not invisible-at-point) invisible-before-point
  16135. ;; (memq kind '(insert delete)))
  16136. )))
  16137. (when (or (memq invisible-at-point '(outline org-hide-block))
  16138. (memq invisible-before-point '(outline org-hide-block)))
  16139. (if (eq org-catch-invisible-edits 'error)
  16140. (error "Editing in invisible areas is prohibited - make visible first"))
  16141. ;; Make the area visible
  16142. (save-excursion
  16143. (if invisible-before-point
  16144. (goto-char (previous-single-char-property-change
  16145. (point) 'invisible)))
  16146. (org-cycle))
  16147. (cond
  16148. ((eq org-catch-invisible-edits 'show)
  16149. ;; That's it, we do the edit after showing
  16150. (message
  16151. "Unfolding invisible region around point before editing")
  16152. (sit-for 1))
  16153. ((and (eq org-catch-invisible-edits 'smart)
  16154. border-and-ok-direction)
  16155. (message "Unfolding invisible region around point before editing"))
  16156. (t
  16157. ;; Don't do the edit, make the user repeat it in full visibility
  16158. (error "Edit in invisible region aborted, repeat to confirm with text visible")))))))
  16159. (defun org-fix-tags-on-the-fly ()
  16160. (when (and (equal (char-after (point-at-bol)) ?*)
  16161. (org-at-heading-p))
  16162. (org-align-tags-here org-tags-column)))
  16163. (defun org-delete-backward-char (N)
  16164. "Like `delete-backward-char', insert whitespace at field end in tables.
  16165. When deleting backwards, in tables this function will insert whitespace in
  16166. front of the next \"|\" separator, to keep the table aligned. The table will
  16167. still be marked for re-alignment if the field did fill the entire column,
  16168. because, in this case the deletion might narrow the column."
  16169. (interactive "p")
  16170. (org-check-before-invisible-edit 'delete-backward)
  16171. (if (and (org-table-p)
  16172. (eq N 1)
  16173. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16174. (looking-at ".*?|"))
  16175. (let ((pos (point))
  16176. (noalign (looking-at "[^|\n\r]* |"))
  16177. (c org-table-may-need-update))
  16178. (backward-delete-char N)
  16179. (if (not overwrite-mode)
  16180. (progn
  16181. (skip-chars-forward "^|")
  16182. (insert " ")
  16183. (goto-char (1- pos))))
  16184. ;; noalign: if there were two spaces at the end, this field
  16185. ;; does not determine the width of the column.
  16186. (if noalign (setq org-table-may-need-update c)))
  16187. (backward-delete-char N)
  16188. (org-fix-tags-on-the-fly)))
  16189. (defun org-delete-char (N)
  16190. "Like `delete-char', but insert whitespace at field end in tables.
  16191. When deleting characters, in tables this function will insert whitespace in
  16192. front of the next \"|\" separator, to keep the table aligned. The table will
  16193. still be marked for re-alignment if the field did fill the entire column,
  16194. because, in this case the deletion might narrow the column."
  16195. (interactive "p")
  16196. (org-check-before-invisible-edit 'delete)
  16197. (if (and (org-table-p)
  16198. (not (bolp))
  16199. (not (= (char-after) ?|))
  16200. (eq N 1))
  16201. (if (looking-at ".*?|")
  16202. (let ((pos (point))
  16203. (noalign (looking-at "[^|\n\r]* |"))
  16204. (c org-table-may-need-update))
  16205. (replace-match (concat
  16206. (substring (match-string 0) 1 -1)
  16207. " |"))
  16208. (goto-char pos)
  16209. ;; noalign: if there were two spaces at the end, this field
  16210. ;; does not determine the width of the column.
  16211. (if noalign (setq org-table-may-need-update c)))
  16212. (delete-char N))
  16213. (delete-char N)
  16214. (org-fix-tags-on-the-fly)))
  16215. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16216. (put 'org-self-insert-command 'delete-selection t)
  16217. (put 'orgtbl-self-insert-command 'delete-selection t)
  16218. (put 'org-delete-char 'delete-selection 'supersede)
  16219. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16220. (put 'org-yank 'delete-selection 'yank)
  16221. ;; Make `flyspell-mode' delay after some commands
  16222. (put 'org-self-insert-command 'flyspell-delayed t)
  16223. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16224. (put 'org-delete-char 'flyspell-delayed t)
  16225. (put 'org-delete-backward-char 'flyspell-delayed t)
  16226. ;; Make pabbrev-mode expand after org-mode commands
  16227. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16228. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16229. ;; How to do this: Measure non-white length of current string
  16230. ;; If equal to column width, we should realign.
  16231. (defun org-remap (map &rest commands)
  16232. "In MAP, remap the functions given in COMMANDS.
  16233. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16234. (let (new old)
  16235. (while commands
  16236. (setq old (pop commands) new (pop commands))
  16237. (if (fboundp 'command-remapping)
  16238. (org-defkey map (vector 'remap old) new)
  16239. (substitute-key-definition old new map global-map)))))
  16240. (when (eq org-enable-table-editor 'optimized)
  16241. ;; If the user wants maximum table support, we need to hijack
  16242. ;; some standard editing functions
  16243. (org-remap org-mode-map
  16244. 'self-insert-command 'org-self-insert-command
  16245. 'delete-char 'org-delete-char
  16246. 'delete-backward-char 'org-delete-backward-char)
  16247. (org-defkey org-mode-map "|" 'org-force-self-insert))
  16248. (defvar org-ctrl-c-ctrl-c-hook nil
  16249. "Hook for functions attaching themselves to `C-c C-c'.
  16250. This can be used to add additional functionality to the C-c C-c
  16251. key which executes context-dependent commands. This hook is run
  16252. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  16253. run after the last test.
  16254. Each function will be called with no arguments. The function
  16255. must check if the context is appropriate for it to act. If yes,
  16256. it should do its thing and then return a non-nil value. If the
  16257. context is wrong, just do nothing and return nil.")
  16258. (defvar org-ctrl-c-ctrl-c-final-hook nil
  16259. "Hook for functions attaching themselves to `C-c C-c'.
  16260. This can be used to add additional functionality to the C-c C-c
  16261. key which executes context-dependent commands. This hook is run
  16262. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  16263. before the first test.
  16264. Each function will be called with no arguments. The function
  16265. must check if the context is appropriate for it to act. If yes,
  16266. it should do its thing and then return a non-nil value. If the
  16267. context is wrong, just do nothing and return nil.")
  16268. (defvar org-tab-first-hook nil
  16269. "Hook for functions to attach themselves to TAB.
  16270. See `org-ctrl-c-ctrl-c-hook' for more information.
  16271. This hook runs as the first action when TAB is pressed, even before
  16272. `org-cycle' messes around with the `outline-regexp' to cater for
  16273. inline tasks and plain list item folding.
  16274. If any function in this hook returns t, any other actions that
  16275. would have been caused by TAB (such as table field motion or visibility
  16276. cycling) will not occur.")
  16277. (defvar org-tab-after-check-for-table-hook nil
  16278. "Hook for functions to attach themselves to TAB.
  16279. See `org-ctrl-c-ctrl-c-hook' for more information.
  16280. This hook runs after it has been established that the cursor is not in a
  16281. table, but before checking if the cursor is in a headline or if global cycling
  16282. should be done.
  16283. If any function in this hook returns t, not other actions like visibility
  16284. cycling will be done.")
  16285. (defvar org-tab-after-check-for-cycling-hook nil
  16286. "Hook for functions to attach themselves to TAB.
  16287. See `org-ctrl-c-ctrl-c-hook' for more information.
  16288. This hook runs after it has been established that not table field motion and
  16289. not visibility should be done because of current context. This is probably
  16290. the place where a package like yasnippets can hook in.")
  16291. (defvar org-tab-before-tab-emulation-hook nil
  16292. "Hook for functions to attach themselves to TAB.
  16293. See `org-ctrl-c-ctrl-c-hook' for more information.
  16294. This hook runs after every other options for TAB have been exhausted, but
  16295. before indentation and \t insertion takes place.")
  16296. (defvar org-metaleft-hook nil
  16297. "Hook for functions attaching themselves to `M-left'.
  16298. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16299. (defvar org-metaright-hook nil
  16300. "Hook for functions attaching themselves to `M-right'.
  16301. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16302. (defvar org-metaup-hook nil
  16303. "Hook for functions attaching themselves to `M-up'.
  16304. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16305. (defvar org-metadown-hook nil
  16306. "Hook for functions attaching themselves to `M-down'.
  16307. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16308. (defvar org-shiftmetaleft-hook nil
  16309. "Hook for functions attaching themselves to `M-S-left'.
  16310. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16311. (defvar org-shiftmetaright-hook nil
  16312. "Hook for functions attaching themselves to `M-S-right'.
  16313. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16314. (defvar org-shiftmetaup-hook nil
  16315. "Hook for functions attaching themselves to `M-S-up'.
  16316. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16317. (defvar org-shiftmetadown-hook nil
  16318. "Hook for functions attaching themselves to `M-S-down'.
  16319. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16320. (defvar org-metareturn-hook nil
  16321. "Hook for functions attaching themselves to `M-RET'.
  16322. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16323. (defvar org-shiftup-hook nil
  16324. "Hook for functions attaching themselves to `S-up'.
  16325. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16326. (defvar org-shiftup-final-hook nil
  16327. "Hook for functions attaching themselves to `S-up'.
  16328. This one runs after all other options except shift-select have been excluded.
  16329. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16330. (defvar org-shiftdown-hook nil
  16331. "Hook for functions attaching themselves to `S-down'.
  16332. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16333. (defvar org-shiftdown-final-hook nil
  16334. "Hook for functions attaching themselves to `S-down'.
  16335. This one runs after all other options except shift-select have been excluded.
  16336. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16337. (defvar org-shiftleft-hook nil
  16338. "Hook for functions attaching themselves to `S-left'.
  16339. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16340. (defvar org-shiftleft-final-hook nil
  16341. "Hook for functions attaching themselves to `S-left'.
  16342. This one runs after all other options except shift-select have been excluded.
  16343. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16344. (defvar org-shiftright-hook nil
  16345. "Hook for functions attaching themselves to `S-right'.
  16346. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16347. (defvar org-shiftright-final-hook nil
  16348. "Hook for functions attaching themselves to `S-right'.
  16349. This one runs after all other options except shift-select have been excluded.
  16350. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16351. (defun org-modifier-cursor-error ()
  16352. "Throw an error, a modified cursor command was applied in wrong context."
  16353. (error "This command is active in special context like tables, headlines or items"))
  16354. (defun org-shiftselect-error ()
  16355. "Throw an error because Shift-Cursor command was applied in wrong context."
  16356. (if (and (boundp 'shift-select-mode) shift-select-mode)
  16357. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  16358. (error "This command works only in special context like headlines or timestamps")))
  16359. (defun org-call-for-shift-select (cmd)
  16360. (let ((this-command-keys-shift-translated t))
  16361. (call-interactively cmd)))
  16362. (defun org-shifttab (&optional arg)
  16363. "Global visibility cycling or move to previous table field.
  16364. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  16365. on context.
  16366. See the individual commands for more information."
  16367. (interactive "P")
  16368. (cond
  16369. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  16370. ((integerp arg)
  16371. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  16372. (message "Content view to level: %d" arg)
  16373. (org-content (prefix-numeric-value arg2))
  16374. (setq org-cycle-global-status 'overview)))
  16375. (t (call-interactively 'org-global-cycle))))
  16376. (defun org-shiftmetaleft ()
  16377. "Promote subtree or delete table column.
  16378. Calls `org-promote-subtree', `org-outdent-item',
  16379. or `org-table-delete-column', depending on context.
  16380. See the individual commands for more information."
  16381. (interactive)
  16382. (cond
  16383. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  16384. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  16385. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  16386. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  16387. (t (org-modifier-cursor-error))))
  16388. (defun org-shiftmetaright ()
  16389. "Demote subtree or insert table column.
  16390. Calls `org-demote-subtree', `org-indent-item',
  16391. or `org-table-insert-column', depending on context.
  16392. See the individual commands for more information."
  16393. (interactive)
  16394. (cond
  16395. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  16396. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  16397. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  16398. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  16399. (t (org-modifier-cursor-error))))
  16400. (defun org-shiftmetaup (&optional arg)
  16401. "Move subtree up or kill table row.
  16402. Calls `org-move-subtree-up' or `org-table-kill-row' or
  16403. `org-move-item-up' depending on context. See the individual commands
  16404. for more information."
  16405. (interactive "P")
  16406. (cond
  16407. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  16408. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  16409. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16410. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16411. (t (org-modifier-cursor-error))))
  16412. (defun org-shiftmetadown (&optional arg)
  16413. "Move subtree down or insert table row.
  16414. Calls `org-move-subtree-down' or `org-table-insert-row' or
  16415. `org-move-item-down', depending on context. See the individual
  16416. commands for more information."
  16417. (interactive "P")
  16418. (cond
  16419. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  16420. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  16421. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16422. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16423. (t (org-modifier-cursor-error))))
  16424. (defsubst org-hidden-tree-error ()
  16425. (error
  16426. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  16427. (defun org-metaleft (&optional arg)
  16428. "Promote heading or move table column to left.
  16429. Calls `org-do-promote' or `org-table-move-column', depending on context.
  16430. With no specific context, calls the Emacs default `backward-word'.
  16431. See the individual commands for more information."
  16432. (interactive "P")
  16433. (cond
  16434. ((run-hook-with-args-until-success 'org-metaleft-hook))
  16435. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  16436. ((org-with-limited-levels
  16437. (or (org-at-heading-p)
  16438. (and (org-region-active-p)
  16439. (save-excursion
  16440. (goto-char (region-beginning))
  16441. (org-at-heading-p)))))
  16442. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16443. (call-interactively 'org-do-promote))
  16444. ;; At an inline task.
  16445. ((org-at-heading-p)
  16446. (call-interactively 'org-inlinetask-promote))
  16447. ((or (org-at-item-p)
  16448. (and (org-region-active-p)
  16449. (save-excursion
  16450. (goto-char (region-beginning))
  16451. (org-at-item-p))))
  16452. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16453. (call-interactively 'org-outdent-item))
  16454. (t (call-interactively 'backward-word))))
  16455. (defun org-metaright (&optional arg)
  16456. "Demote subtree or move table column to right.
  16457. Calls `org-do-demote' or `org-table-move-column', depending on context.
  16458. With no specific context, calls the Emacs default `forward-word'.
  16459. See the individual commands for more information."
  16460. (interactive "P")
  16461. (cond
  16462. ((run-hook-with-args-until-success 'org-metaright-hook))
  16463. ((org-at-table-p) (call-interactively 'org-table-move-column))
  16464. ((org-with-limited-levels
  16465. (or (org-at-heading-p)
  16466. (and (org-region-active-p)
  16467. (save-excursion
  16468. (goto-char (region-beginning))
  16469. (org-at-heading-p)))))
  16470. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16471. (call-interactively 'org-do-demote))
  16472. ;; At an inline task.
  16473. ((org-at-heading-p)
  16474. (call-interactively 'org-inlinetask-demote))
  16475. ((or (org-at-item-p)
  16476. (and (org-region-active-p)
  16477. (save-excursion
  16478. (goto-char (region-beginning))
  16479. (org-at-item-p))))
  16480. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16481. (call-interactively 'org-indent-item))
  16482. (t (call-interactively 'forward-word))))
  16483. (defun org-check-for-hidden (what)
  16484. "Check if there are hidden headlines/items in the current visual line.
  16485. WHAT can be either `headlines' or `items'. If the current line is
  16486. an outline or item heading and it has a folded subtree below it,
  16487. this function returns t, nil otherwise."
  16488. (let ((re (cond
  16489. ((eq what 'headlines) org-outline-regexp-bol)
  16490. ((eq what 'items) (org-item-beginning-re))
  16491. (t (error "This should not happen"))))
  16492. beg end)
  16493. (save-excursion
  16494. (catch 'exit
  16495. (unless (org-region-active-p)
  16496. (setq beg (point-at-bol))
  16497. (beginning-of-line 2)
  16498. (while (and (not (eobp)) ;; this is like `next-line'
  16499. (get-char-property (1- (point)) 'invisible))
  16500. (beginning-of-line 2))
  16501. (setq end (point))
  16502. (goto-char beg)
  16503. (goto-char (point-at-eol))
  16504. (setq end (max end (point)))
  16505. (while (re-search-forward re end t)
  16506. (if (get-char-property (match-beginning 0) 'invisible)
  16507. (throw 'exit t))))
  16508. nil))))
  16509. (defun org-metaup (&optional arg)
  16510. "Move subtree up or move table row up.
  16511. Calls `org-move-subtree-up' or `org-table-move-row' or
  16512. `org-move-item-up', depending on context. See the individual commands
  16513. for more information."
  16514. (interactive "P")
  16515. (cond
  16516. ((run-hook-with-args-until-success 'org-metaup-hook))
  16517. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  16518. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16519. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16520. (t (transpose-lines 1) (beginning-of-line -1))))
  16521. (defun org-metadown (&optional arg)
  16522. "Move subtree down or move table row down.
  16523. Calls `org-move-subtree-down' or `org-table-move-row' or
  16524. `org-move-item-down', depending on context. See the individual
  16525. commands for more information."
  16526. (interactive "P")
  16527. (cond
  16528. ((run-hook-with-args-until-success 'org-metadown-hook))
  16529. ((org-at-table-p) (call-interactively 'org-table-move-row))
  16530. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16531. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16532. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  16533. (defun org-shiftup (&optional arg)
  16534. "Increase item in timestamp or increase priority of current headline.
  16535. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  16536. depending on context. See the individual commands for more information."
  16537. (interactive "P")
  16538. (cond
  16539. ((run-hook-with-args-until-success 'org-shiftup-hook))
  16540. ((and org-support-shift-select (org-region-active-p))
  16541. (org-call-for-shift-select 'previous-line))
  16542. ((org-at-timestamp-p t)
  16543. (call-interactively (if org-edit-timestamp-down-means-later
  16544. 'org-timestamp-down 'org-timestamp-up)))
  16545. ((and (not (eq org-support-shift-select 'always))
  16546. org-enable-priority-commands
  16547. (org-at-heading-p))
  16548. (call-interactively 'org-priority-up))
  16549. ((and (not org-support-shift-select) (org-at-item-p))
  16550. (call-interactively 'org-previous-item))
  16551. ((org-clocktable-try-shift 'up arg))
  16552. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  16553. (org-support-shift-select
  16554. (org-call-for-shift-select 'previous-line))
  16555. (t (org-shiftselect-error))))
  16556. (defun org-shiftdown (&optional arg)
  16557. "Decrease item in timestamp or decrease priority of current headline.
  16558. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  16559. depending on context. See the individual commands for more information."
  16560. (interactive "P")
  16561. (cond
  16562. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  16563. ((and org-support-shift-select (org-region-active-p))
  16564. (org-call-for-shift-select 'next-line))
  16565. ((org-at-timestamp-p t)
  16566. (call-interactively (if org-edit-timestamp-down-means-later
  16567. 'org-timestamp-up 'org-timestamp-down)))
  16568. ((and (not (eq org-support-shift-select 'always))
  16569. org-enable-priority-commands
  16570. (org-at-heading-p))
  16571. (call-interactively 'org-priority-down))
  16572. ((and (not org-support-shift-select) (org-at-item-p))
  16573. (call-interactively 'org-next-item))
  16574. ((org-clocktable-try-shift 'down arg))
  16575. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  16576. (org-support-shift-select
  16577. (org-call-for-shift-select 'next-line))
  16578. (t (org-shiftselect-error))))
  16579. (defun org-shiftright (&optional arg)
  16580. "Cycle the thing at point or in the current line, depending on context.
  16581. Depending on context, this does one of the following:
  16582. - switch a timestamp at point one day into the future
  16583. - on a headline, switch to the next TODO keyword.
  16584. - on an item, switch entire list to the next bullet type
  16585. - on a property line, switch to the next allowed value
  16586. - on a clocktable definition line, move time block into the future"
  16587. (interactive "P")
  16588. (cond
  16589. ((run-hook-with-args-until-success 'org-shiftright-hook))
  16590. ((and org-support-shift-select (org-region-active-p))
  16591. (org-call-for-shift-select 'forward-char))
  16592. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  16593. ((and (not (eq org-support-shift-select 'always))
  16594. (org-at-heading-p))
  16595. (let ((org-inhibit-logging
  16596. (not org-treat-S-cursor-todo-selection-as-state-change))
  16597. (org-inhibit-blocking
  16598. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16599. (org-call-with-arg 'org-todo 'right)))
  16600. ((or (and org-support-shift-select
  16601. (not (eq org-support-shift-select 'always))
  16602. (org-at-item-bullet-p))
  16603. (and (not org-support-shift-select) (org-at-item-p)))
  16604. (org-call-with-arg 'org-cycle-list-bullet nil))
  16605. ((and (not (eq org-support-shift-select 'always))
  16606. (org-at-property-p))
  16607. (call-interactively 'org-property-next-allowed-value))
  16608. ((org-clocktable-try-shift 'right arg))
  16609. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  16610. (org-support-shift-select
  16611. (org-call-for-shift-select 'forward-char))
  16612. (t (org-shiftselect-error))))
  16613. (defun org-shiftleft (&optional arg)
  16614. "Cycle the thing at point or in the current line, depending on context.
  16615. Depending on context, this does one of the following:
  16616. - switch a timestamp at point one day into the past
  16617. - on a headline, switch to the previous TODO keyword.
  16618. - on an item, switch entire list to the previous bullet type
  16619. - on a property line, switch to the previous allowed value
  16620. - on a clocktable definition line, move time block into the past"
  16621. (interactive "P")
  16622. (cond
  16623. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  16624. ((and org-support-shift-select (org-region-active-p))
  16625. (org-call-for-shift-select 'backward-char))
  16626. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  16627. ((and (not (eq org-support-shift-select 'always))
  16628. (org-at-heading-p))
  16629. (let ((org-inhibit-logging
  16630. (not org-treat-S-cursor-todo-selection-as-state-change))
  16631. (org-inhibit-blocking
  16632. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16633. (org-call-with-arg 'org-todo 'left)))
  16634. ((or (and org-support-shift-select
  16635. (not (eq org-support-shift-select 'always))
  16636. (org-at-item-bullet-p))
  16637. (and (not org-support-shift-select) (org-at-item-p)))
  16638. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  16639. ((and (not (eq org-support-shift-select 'always))
  16640. (org-at-property-p))
  16641. (call-interactively 'org-property-previous-allowed-value))
  16642. ((org-clocktable-try-shift 'left arg))
  16643. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  16644. (org-support-shift-select
  16645. (org-call-for-shift-select 'backward-char))
  16646. (t (org-shiftselect-error))))
  16647. (defun org-shiftcontrolright ()
  16648. "Switch to next TODO set."
  16649. (interactive)
  16650. (cond
  16651. ((and org-support-shift-select (org-region-active-p))
  16652. (org-call-for-shift-select 'forward-word))
  16653. ((and (not (eq org-support-shift-select 'always))
  16654. (org-at-heading-p))
  16655. (org-call-with-arg 'org-todo 'nextset))
  16656. (org-support-shift-select
  16657. (org-call-for-shift-select 'forward-word))
  16658. (t (org-shiftselect-error))))
  16659. (defun org-shiftcontrolleft ()
  16660. "Switch to previous TODO set."
  16661. (interactive)
  16662. (cond
  16663. ((and org-support-shift-select (org-region-active-p))
  16664. (org-call-for-shift-select 'backward-word))
  16665. ((and (not (eq org-support-shift-select 'always))
  16666. (org-at-heading-p))
  16667. (org-call-with-arg 'org-todo 'previousset))
  16668. (org-support-shift-select
  16669. (org-call-for-shift-select 'backward-word))
  16670. (t (org-shiftselect-error))))
  16671. (defun org-shiftcontrolup ()
  16672. "Change timestamps synchronously up in CLOCK log lines."
  16673. (interactive)
  16674. (cond ((and (not org-support-shift-select)
  16675. (org-at-clock-log-p)
  16676. (org-at-timestamp-p t))
  16677. (org-clock-timestamps-up))
  16678. (t (org-shiftselect-error))))
  16679. (defun org-shiftcontroldown ()
  16680. "Change timestamps synchronously down in CLOCK log lines."
  16681. (interactive)
  16682. (cond ((and (not org-support-shift-select)
  16683. (org-at-clock-log-p)
  16684. (org-at-timestamp-p t))
  16685. (org-clock-timestamps-down))
  16686. (t (org-shiftselect-error))))
  16687. (defun org-ctrl-c-ret ()
  16688. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  16689. (interactive)
  16690. (cond
  16691. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  16692. (t (call-interactively 'org-insert-heading))))
  16693. (defun org-find-visible ()
  16694. (let ((s (point)))
  16695. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16696. (get-char-property s 'invisible)))
  16697. s))
  16698. (defun org-find-invisible ()
  16699. (let ((s (point)))
  16700. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16701. (not (get-char-property s 'invisible))))
  16702. s))
  16703. (defun org-copy-visible (beg end)
  16704. "Copy the visible parts of the region."
  16705. (interactive "r")
  16706. (let (snippets s)
  16707. (save-excursion
  16708. (save-restriction
  16709. (narrow-to-region beg end)
  16710. (setq s (goto-char (point-min)))
  16711. (while (not (= (point) (point-max)))
  16712. (goto-char (org-find-invisible))
  16713. (push (buffer-substring s (point)) snippets)
  16714. (setq s (goto-char (org-find-visible))))))
  16715. (kill-new (apply 'concat (nreverse snippets)))))
  16716. (defun org-copy-special ()
  16717. "Copy region in table or copy current subtree.
  16718. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  16719. See the individual commands for more information."
  16720. (interactive)
  16721. (call-interactively
  16722. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  16723. (defun org-cut-special ()
  16724. "Cut region in table or cut current subtree.
  16725. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  16726. See the individual commands for more information."
  16727. (interactive)
  16728. (call-interactively
  16729. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  16730. (defun org-paste-special (arg)
  16731. "Paste rectangular region into table, or past subtree relative to level.
  16732. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  16733. See the individual commands for more information."
  16734. (interactive "P")
  16735. (if (org-at-table-p)
  16736. (org-table-paste-rectangle)
  16737. (org-paste-subtree arg)))
  16738. (defun org-edit-special (&optional arg)
  16739. "Call a special editor for the stuff at point.
  16740. When at a table, call the formula editor with `org-table-edit-formulas'.
  16741. When at the first line of an src example, call `org-edit-src-code'.
  16742. When in an #+include line, visit the include file. Otherwise call
  16743. `ffap' to visit the file at point."
  16744. (interactive)
  16745. ;; possibly prep session before editing source
  16746. (when arg
  16747. (let* ((info (org-babel-get-src-block-info))
  16748. (lang (nth 0 info))
  16749. (params (nth 2 info))
  16750. (session (cdr (assoc :session params))))
  16751. (when (and info session) ;; we are in a source-code block with a session
  16752. (funcall
  16753. (intern (concat "org-babel-prep-session:" lang)) session params))))
  16754. (cond ;; proceed with `org-edit-special'
  16755. ((save-excursion
  16756. (beginning-of-line 1)
  16757. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  16758. (find-file (org-trim (match-string 1))))
  16759. ((org-edit-src-code))
  16760. ((org-edit-fixed-width-region))
  16761. ((org-at-table.el-p)
  16762. (org-edit-src-code))
  16763. ((or (org-at-table-p)
  16764. (save-excursion
  16765. (beginning-of-line 1)
  16766. (looking-at "[ \t]*#\\+TBLFM:")))
  16767. (call-interactively 'org-table-edit-formulas))
  16768. (t (call-interactively 'ffap))))
  16769. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  16770. (defun org-ctrl-c-ctrl-c (&optional arg)
  16771. "Set tags in headline, or update according to changed information at point.
  16772. This command does many different things, depending on context:
  16773. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  16774. this is what we do.
  16775. - If the cursor is on a statistics cookie, update it.
  16776. - If the cursor is in a headline, prompt for tags and insert them
  16777. into the current line, aligned to `org-tags-column'. When called
  16778. with prefix arg, realign all tags in the current buffer.
  16779. - If the cursor is in one of the special #+KEYWORD lines, this
  16780. triggers scanning the buffer for these lines and updating the
  16781. information.
  16782. - If the cursor is inside a table, realign the table. This command
  16783. works even if the automatic table editor has been turned off.
  16784. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  16785. the entire table.
  16786. - If the cursor is at a footnote reference or definition, jump to
  16787. the corresponding definition or references, respectively.
  16788. - If the cursor is a the beginning of a dynamic block, update it.
  16789. - If the current buffer is a capture buffer, close note and file it.
  16790. - If the cursor is on a <<<target>>>, update radio targets and
  16791. corresponding links in this buffer.
  16792. - If the cursor is on a numbered item in a plain list, renumber the
  16793. ordered list.
  16794. - If the cursor is on a checkbox, toggle it.
  16795. - If the cursor is on a code block, evaluate it. The variable
  16796. `org-confirm-babel-evaluate' can be used to control prompting
  16797. before code block evaluation, by default every code block
  16798. evaluation requires confirmation. Code block evaluation can be
  16799. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  16800. (interactive "P")
  16801. (let ((org-enable-table-editor t))
  16802. (cond
  16803. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  16804. org-occur-highlights
  16805. org-latex-fragment-image-overlays)
  16806. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  16807. (org-remove-occur-highlights)
  16808. (org-remove-latex-fragment-image-overlays)
  16809. (message "Temporary highlights/overlays removed from current buffer"))
  16810. ((and (local-variable-p 'org-finish-function (current-buffer))
  16811. (fboundp org-finish-function))
  16812. (funcall org-finish-function))
  16813. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  16814. ((org-in-regexp org-ts-regexp-both)
  16815. (org-timestamp-change 0 'day))
  16816. ((or (looking-at org-property-start-re)
  16817. (org-at-property-p))
  16818. (call-interactively 'org-property-action))
  16819. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  16820. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  16821. (or (org-at-heading-p) (org-at-item-p)))
  16822. (call-interactively 'org-update-statistics-cookies))
  16823. ((org-at-heading-p) (call-interactively 'org-set-tags))
  16824. ((org-at-table.el-p)
  16825. (message "Use C-c ' to edit table.el tables"))
  16826. ((org-at-table-p)
  16827. (org-table-maybe-eval-formula)
  16828. (if arg
  16829. (call-interactively 'org-table-recalculate)
  16830. (org-table-maybe-recalculate-line))
  16831. (call-interactively 'org-table-align)
  16832. (orgtbl-send-table 'maybe))
  16833. ((or (org-footnote-at-reference-p)
  16834. (org-footnote-at-definition-p))
  16835. (call-interactively 'org-footnote-action))
  16836. ((org-at-item-checkbox-p)
  16837. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  16838. ;; list only if at top item.
  16839. (let* ((cbox (match-string 1))
  16840. (struct (org-list-struct))
  16841. (old-struct (copy-tree struct))
  16842. (parents (org-list-parents-alist struct))
  16843. (orderedp (org-entry-get nil "ORDERED"))
  16844. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  16845. block-item)
  16846. ;; Use a light version of `org-toggle-checkbox' to avoid
  16847. ;; computing list structure twice.
  16848. (let ((new-box (cond
  16849. ((equal arg '(16)) "[-]")
  16850. ((equal arg '(4)) nil)
  16851. ((equal "[X]" cbox) "[ ]")
  16852. (t "[X]"))))
  16853. (if (and firstp arg)
  16854. ;; If at first item of sub-list, remove check-box from
  16855. ;; every item at the same level.
  16856. (mapc
  16857. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  16858. (org-list-get-all-items
  16859. (point-at-bol) struct (org-list-prevs-alist struct)))
  16860. (org-list-set-checkbox (point-at-bol) struct new-box)))
  16861. ;; Replicate `org-list-write-struct', while grabbing a return
  16862. ;; value from `org-list-struct-fix-box'.
  16863. (org-list-struct-fix-ind struct parents 2)
  16864. (org-list-struct-fix-item-end struct)
  16865. (let ((prevs (org-list-prevs-alist struct)))
  16866. (org-list-struct-fix-bul struct prevs)
  16867. (org-list-struct-fix-ind struct parents)
  16868. (setq block-item
  16869. (org-list-struct-fix-box struct parents prevs orderedp)))
  16870. (org-list-struct-apply-struct struct old-struct)
  16871. (org-update-checkbox-count-maybe)
  16872. (when block-item
  16873. (message
  16874. "Checkboxes were removed due to unchecked box at line %d"
  16875. (org-current-line block-item)))
  16876. (when firstp (org-list-send-list 'maybe))))
  16877. ((org-at-item-p)
  16878. ;; Cursor at an item: repair list. Do checkbox related actions
  16879. ;; only if function was called with an argument. Send list only
  16880. ;; if at top item.
  16881. (let* ((struct (org-list-struct))
  16882. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  16883. old-struct)
  16884. (when arg
  16885. (setq old-struct (copy-tree struct))
  16886. (if firstp
  16887. ;; If at first item of sub-list, add check-box to every
  16888. ;; item at the same level.
  16889. (mapc
  16890. (lambda (pos)
  16891. (unless (org-list-get-checkbox pos struct)
  16892. (org-list-set-checkbox pos struct "[ ]")))
  16893. (org-list-get-all-items
  16894. (point-at-bol) struct (org-list-prevs-alist struct)))
  16895. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  16896. (org-list-write-struct
  16897. struct (org-list-parents-alist struct) old-struct)
  16898. (when arg (org-update-checkbox-count-maybe))
  16899. (when firstp (org-list-send-list 'maybe))))
  16900. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  16901. ;; Dynamic block
  16902. (beginning-of-line 1)
  16903. (save-excursion (org-update-dblock)))
  16904. ((save-excursion
  16905. (beginning-of-line 1)
  16906. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  16907. (cond
  16908. ((equal (match-string 1) "TBLFM")
  16909. ;; Recalculate the table before this line
  16910. (save-excursion
  16911. (beginning-of-line 1)
  16912. (skip-chars-backward " \r\n\t")
  16913. (if (org-at-table-p)
  16914. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  16915. (t
  16916. (let ((org-inhibit-startup-visibility-stuff t)
  16917. (org-startup-align-all-tables nil))
  16918. (when (boundp 'org-table-coordinate-overlays)
  16919. (mapc 'delete-overlay org-table-coordinate-overlays)
  16920. (setq org-table-coordinate-overlays nil))
  16921. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  16922. (message "Local setup has been refreshed"))))
  16923. ((org-clock-update-time-maybe))
  16924. (t
  16925. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  16926. (error "C-c C-c can do nothing useful at this location"))))))
  16927. (defun org-mode-restart ()
  16928. "Restart Org-mode, to scan again for special lines.
  16929. Also updates the keyword regular expressions."
  16930. (interactive)
  16931. (org-mode)
  16932. (message "Org-mode restarted"))
  16933. (defun org-kill-note-or-show-branches ()
  16934. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  16935. (interactive)
  16936. (if (not org-finish-function)
  16937. (progn
  16938. (hide-subtree)
  16939. (call-interactively 'show-branches))
  16940. (let ((org-note-abort t))
  16941. (funcall org-finish-function))))
  16942. (defun org-return (&optional indent)
  16943. "Goto next table row or insert a newline.
  16944. Calls `org-table-next-row' or `newline', depending on context.
  16945. See the individual commands for more information."
  16946. (interactive)
  16947. (cond
  16948. ((bobp) (if indent (newline-and-indent) (newline)))
  16949. ((org-at-table-p)
  16950. (org-table-justify-field-maybe)
  16951. (call-interactively 'org-table-next-row))
  16952. ;; when `newline-and-indent' is called within a list, make sure
  16953. ;; text moved stays inside the item.
  16954. ((and (org-in-item-p) indent)
  16955. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  16956. (progn
  16957. (save-match-data (newline))
  16958. (org-indent-line-to (length (match-string 0))))
  16959. (let ((ind (org-get-indentation)))
  16960. (newline)
  16961. (if (org-looking-back org-list-end-re)
  16962. (org-indent-line-function)
  16963. (org-indent-line-to ind)))))
  16964. ((and org-return-follows-link
  16965. (let ((tprop (get-text-property (point) 'face)))
  16966. (or (eq tprop 'org-link)
  16967. (and (listp tprop) (memq 'org-link tprop)))))
  16968. (call-interactively 'org-open-at-point))
  16969. ((and (org-at-heading-p)
  16970. (looking-at
  16971. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  16972. (org-show-entry)
  16973. (end-of-line 1)
  16974. (newline))
  16975. (t (if indent (newline-and-indent) (newline)))))
  16976. (defun org-return-indent ()
  16977. "Goto next table row or insert a newline and indent.
  16978. Calls `org-table-next-row' or `newline-and-indent', depending on
  16979. context. See the individual commands for more information."
  16980. (interactive)
  16981. (org-return t))
  16982. (defun org-ctrl-c-star ()
  16983. "Compute table, or change heading status of lines.
  16984. Calls `org-table-recalculate' or `org-toggle-heading',
  16985. depending on context."
  16986. (interactive)
  16987. (cond
  16988. ((org-at-table-p)
  16989. (call-interactively 'org-table-recalculate))
  16990. (t
  16991. ;; Convert all lines in region to list items
  16992. (call-interactively 'org-toggle-heading))))
  16993. (defun org-ctrl-c-minus ()
  16994. "Insert separator line in table or modify bullet status of line.
  16995. Also turns a plain line or a region of lines into list items.
  16996. Calls `org-table-insert-hline', `org-toggle-item', or
  16997. `org-cycle-list-bullet', depending on context."
  16998. (interactive)
  16999. (cond
  17000. ((org-at-table-p)
  17001. (call-interactively 'org-table-insert-hline))
  17002. ((org-region-active-p)
  17003. (call-interactively 'org-toggle-item))
  17004. ((org-in-item-p)
  17005. (call-interactively 'org-cycle-list-bullet))
  17006. (t
  17007. (call-interactively 'org-toggle-item))))
  17008. (defun org-toggle-item (arg)
  17009. "Convert headings or normal lines to items, items to normal lines.
  17010. If there is no active region, only the current line is considered.
  17011. If the first non blank line in the region is an headline, convert
  17012. all headlines to items, shifting text accordingly.
  17013. If it is an item, convert all items to normal lines.
  17014. If it is normal text, change region into an item. With a prefix
  17015. argument ARG, change each line in region into an item."
  17016. (interactive "P")
  17017. (let ((shift-text
  17018. (function
  17019. ;; Shift text in current section to IND, from point to END.
  17020. ;; The function leaves point to END line.
  17021. (lambda (ind end)
  17022. (let ((min-i 1000) (end (copy-marker end)))
  17023. ;; First determine the minimum indentation (MIN-I) of
  17024. ;; the text.
  17025. (save-excursion
  17026. (catch 'exit
  17027. (while (< (point) end)
  17028. (let ((i (org-get-indentation)))
  17029. (cond
  17030. ;; Skip blank lines and inline tasks.
  17031. ((looking-at "^[ \t]*$"))
  17032. ((looking-at org-outline-regexp-bol))
  17033. ;; We can't find less than 0 indentation.
  17034. ((zerop i) (throw 'exit (setq min-i 0)))
  17035. ((< i min-i) (setq min-i i))))
  17036. (forward-line))))
  17037. ;; Then indent each line so that a line indented to
  17038. ;; MIN-I becomes indented to IND. Ignore blank lines
  17039. ;; and inline tasks in the process.
  17040. (let ((delta (- ind min-i)))
  17041. (while (< (point) end)
  17042. (unless (or (looking-at "^[ \t]*$")
  17043. (looking-at org-outline-regexp-bol))
  17044. (org-indent-line-to (+ (org-get-indentation) delta)))
  17045. (forward-line)))))))
  17046. (skip-blanks
  17047. (function
  17048. ;; Return beginning of first non-blank line, starting from
  17049. ;; line at POS.
  17050. (lambda (pos)
  17051. (save-excursion
  17052. (goto-char pos)
  17053. (skip-chars-forward " \r\t\n")
  17054. (point-at-bol)))))
  17055. beg end)
  17056. ;; Determine boundaries of changes.
  17057. (if (org-region-active-p)
  17058. (setq beg (funcall skip-blanks (region-beginning))
  17059. end (copy-marker (region-end)))
  17060. (setq beg (funcall skip-blanks (point-at-bol))
  17061. end (copy-marker (point-at-eol))))
  17062. ;; Depending on the starting line, choose an action on the text
  17063. ;; between BEG and END.
  17064. (org-with-limited-levels
  17065. (save-excursion
  17066. (goto-char beg)
  17067. (cond
  17068. ;; Case 1. Start at an item: de-itemize. Note that it only
  17069. ;; happens when a region is active: `org-ctrl-c-minus'
  17070. ;; would call `org-cycle-list-bullet' otherwise.
  17071. ((org-at-item-p)
  17072. (while (< (point) end)
  17073. (when (org-at-item-p)
  17074. (skip-chars-forward " \t")
  17075. (delete-region (point) (match-end 0)))
  17076. (forward-line)))
  17077. ;; Case 2. Start at an heading: convert to items.
  17078. ((org-at-heading-p)
  17079. (let* ((bul (org-list-bullet-string "-"))
  17080. (bul-len (length bul))
  17081. ;; Indentation of the first heading. It should be
  17082. ;; relative to the indentation of its parent, if any.
  17083. (start-ind (save-excursion
  17084. (cond
  17085. ((not org-adapt-indentation) 0)
  17086. ((not (outline-previous-heading)) 0)
  17087. (t (length (match-string 0))))))
  17088. ;; Level of first heading. Further headings will be
  17089. ;; compared to it to determine hierarchy in the list.
  17090. (ref-level (org-reduced-level (org-outline-level))))
  17091. (while (< (point) end)
  17092. (let* ((level (org-reduced-level (org-outline-level)))
  17093. (delta (max 0 (- level ref-level))))
  17094. ;; If current headline is less indented than the first
  17095. ;; one, set it as reference, in order to preserve
  17096. ;; subtrees.
  17097. (when (< level ref-level) (setq ref-level level))
  17098. (replace-match bul t t)
  17099. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17100. ;; Ensure all text down to END (or SECTION-END) belongs
  17101. ;; to the newly created item.
  17102. (let ((section-end (save-excursion
  17103. (or (outline-next-heading) (point)))))
  17104. (forward-line)
  17105. (funcall shift-text
  17106. (+ start-ind (* (1+ delta) bul-len))
  17107. (min end section-end)))))))
  17108. ;; Case 3. Normal line with ARG: turn each non-item line into
  17109. ;; an item.
  17110. (arg
  17111. (while (< (point) end)
  17112. (unless (or (org-at-heading-p) (org-at-item-p))
  17113. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17114. (replace-match
  17115. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17116. (forward-line)))
  17117. ;; Case 4. Normal line without ARG: make the first line of
  17118. ;; region an item, and shift indentation of others
  17119. ;; lines to set them as item's body.
  17120. (t (let* ((bul (org-list-bullet-string "-"))
  17121. (bul-len (length bul))
  17122. (ref-ind (org-get-indentation)))
  17123. (skip-chars-forward " \t")
  17124. (insert bul)
  17125. (forward-line)
  17126. (while (< (point) end)
  17127. ;; Ensure that lines less indented than first one
  17128. ;; still get included in item body.
  17129. (funcall shift-text
  17130. (+ ref-ind bul-len)
  17131. (min end (save-excursion (or (outline-next-heading)
  17132. (point)))))
  17133. (forward-line)))))))))
  17134. (defun org-toggle-heading (&optional nstars)
  17135. "Convert headings to normal text, or items or text to headings.
  17136. If there is no active region, only the current line is considered.
  17137. If the first non blank line is an headline, remove the stars from
  17138. all headlines in the region.
  17139. If it is a plain list item, turn all plain list items into headings.
  17140. If it is a normal line, turn each and every normal line (i.e. not
  17141. an heading or an item) in the region into a heading.
  17142. When converting a line into a heading, the number of stars is chosen
  17143. such that the lines become children of the current entry. However,
  17144. when a prefix argument is given, its value determines the number of
  17145. stars to add."
  17146. (interactive "P")
  17147. (let ((skip-blanks
  17148. (function
  17149. ;; Return beginning of first non-blank line, starting from
  17150. ;; line at POS.
  17151. (lambda (pos)
  17152. (save-excursion
  17153. (goto-char pos)
  17154. (skip-chars-forward " \r\t\n")
  17155. (point-at-bol)))))
  17156. beg end)
  17157. ;; Determine boundaries of changes. If region ends at a bol, do
  17158. ;; not consider the last line to be in the region.
  17159. (if (org-region-active-p)
  17160. (setq beg (funcall skip-blanks (region-beginning))
  17161. end (copy-marker (save-excursion
  17162. (goto-char (region-end))
  17163. (if (bolp) (point) (point-at-eol)))))
  17164. (setq beg (funcall skip-blanks (point-at-bol))
  17165. end (copy-marker (point-at-eol))))
  17166. ;; Ensure inline tasks don't count as headings.
  17167. (org-with-limited-levels
  17168. (save-excursion
  17169. (goto-char beg)
  17170. (cond
  17171. ;; Case 1. Started at an heading: de-star headings.
  17172. ((org-at-heading-p)
  17173. (while (< (point) end)
  17174. (when (org-at-heading-p t)
  17175. (looking-at org-outline-regexp) (replace-match ""))
  17176. (forward-line)))
  17177. ;; Case 2. Started at an item: change items into headlines.
  17178. ;; One star will be added by `org-list-to-subtree'.
  17179. ((org-at-item-p)
  17180. (let* ((stars (make-string
  17181. (if nstars
  17182. ;; subtract the star that will be added again by
  17183. ;; `org-list-to-subtree'
  17184. (1- (prefix-numeric-value current-prefix-arg))
  17185. (or (org-current-level) 0))
  17186. ?*))
  17187. (add-stars
  17188. (cond (nstars "") ; stars from prefix only
  17189. ((equal stars "") "") ; before first heading
  17190. (org-odd-levels-only "*") ; inside heading, odd
  17191. (t "")))) ; inside heading, oddeven
  17192. (while (< (point) end)
  17193. (when (org-at-item-p)
  17194. ;; Pay attention to cases when region ends before list.
  17195. (let* ((struct (org-list-struct))
  17196. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  17197. (save-restriction
  17198. (narrow-to-region (point) list-end)
  17199. (insert
  17200. (org-list-to-subtree
  17201. (org-list-parse-list t)
  17202. '(:istart (concat stars add-stars (funcall get-stars depth))
  17203. :icount (concat stars add-stars (funcall get-stars depth))))))))
  17204. (forward-line))))
  17205. ;; Case 3. Started at normal text: make every line an heading,
  17206. ;; skipping headlines and items.
  17207. (t (let* ((stars (make-string
  17208. (if nstars
  17209. (prefix-numeric-value current-prefix-arg)
  17210. (or (org-current-level) 0))
  17211. ?*))
  17212. (add-stars
  17213. (cond (nstars "") ; stars from prefix only
  17214. ((equal stars "") "*") ; before first heading
  17215. (org-odd-levels-only "**") ; inside heading, odd
  17216. (t "*"))) ; inside heading, oddeven
  17217. (rpl (concat stars add-stars " ")))
  17218. (while (< (point) end)
  17219. (when (and (not (org-at-heading-p)) (not (org-at-item-p))
  17220. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  17221. (replace-match (concat rpl (match-string 2))))
  17222. (forward-line)))))))))
  17223. (defun org-meta-return (&optional arg)
  17224. "Insert a new heading or wrap a region in a table.
  17225. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  17226. See the individual commands for more information."
  17227. (interactive "P")
  17228. (cond
  17229. ((run-hook-with-args-until-success 'org-metareturn-hook))
  17230. ((org-at-table-p)
  17231. (call-interactively 'org-table-wrap-region))
  17232. (t (call-interactively 'org-insert-heading))))
  17233. ;;; Menu entries
  17234. ;; Define the Org-mode menus
  17235. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  17236. '("Tbl"
  17237. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  17238. ["Next Field" org-cycle (org-at-table-p)]
  17239. ["Previous Field" org-shifttab (org-at-table-p)]
  17240. ["Next Row" org-return (org-at-table-p)]
  17241. "--"
  17242. ["Blank Field" org-table-blank-field (org-at-table-p)]
  17243. ["Edit Field" org-table-edit-field (org-at-table-p)]
  17244. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  17245. "--"
  17246. ("Column"
  17247. ["Move Column Left" org-metaleft (org-at-table-p)]
  17248. ["Move Column Right" org-metaright (org-at-table-p)]
  17249. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  17250. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  17251. ("Row"
  17252. ["Move Row Up" org-metaup (org-at-table-p)]
  17253. ["Move Row Down" org-metadown (org-at-table-p)]
  17254. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  17255. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  17256. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  17257. "--"
  17258. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  17259. ("Rectangle"
  17260. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  17261. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  17262. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  17263. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  17264. "--"
  17265. ("Calculate"
  17266. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  17267. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  17268. ["Edit Formulas" org-edit-special (org-at-table-p)]
  17269. "--"
  17270. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  17271. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  17272. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  17273. "--"
  17274. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  17275. "--"
  17276. ["Sum Column/Rectangle" org-table-sum
  17277. (or (org-at-table-p) (org-region-active-p))]
  17278. ["Which Column?" org-table-current-column (org-at-table-p)])
  17279. ["Debug Formulas"
  17280. org-table-toggle-formula-debugger
  17281. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  17282. ["Show Col/Row Numbers"
  17283. org-table-toggle-coordinate-overlays
  17284. :style toggle
  17285. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  17286. "--"
  17287. ["Create" org-table-create (and (not (org-at-table-p))
  17288. org-enable-table-editor)]
  17289. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  17290. ["Import from File" org-table-import (not (org-at-table-p))]
  17291. ["Export to File" org-table-export (org-at-table-p)]
  17292. "--"
  17293. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  17294. (easy-menu-define org-org-menu org-mode-map "Org menu"
  17295. '("Org"
  17296. ("Show/Hide"
  17297. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  17298. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  17299. ["Sparse Tree..." org-sparse-tree t]
  17300. ["Reveal Context" org-reveal t]
  17301. ["Show All" show-all t]
  17302. "--"
  17303. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  17304. "--"
  17305. ["New Heading" org-insert-heading t]
  17306. ("Navigate Headings"
  17307. ["Up" outline-up-heading t]
  17308. ["Next" outline-next-visible-heading t]
  17309. ["Previous" outline-previous-visible-heading t]
  17310. ["Next Same Level" outline-forward-same-level t]
  17311. ["Previous Same Level" outline-backward-same-level t]
  17312. "--"
  17313. ["Jump" org-goto t])
  17314. ("Edit Structure"
  17315. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  17316. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  17317. "--"
  17318. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  17319. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  17320. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  17321. "--"
  17322. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  17323. "--"
  17324. ["Copy visible text" org-copy-visible t]
  17325. "--"
  17326. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  17327. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  17328. ["Demote Heading" org-metaright (not (org-at-table-p))]
  17329. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  17330. "--"
  17331. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  17332. "--"
  17333. ["Convert to odd levels" org-convert-to-odd-levels t]
  17334. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  17335. ("Editing"
  17336. ["Emphasis..." org-emphasize t]
  17337. ["Edit Source Example" org-edit-special t]
  17338. "--"
  17339. ["Footnote new/jump" org-footnote-action t]
  17340. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  17341. ("Archive"
  17342. ["Archive (default method)" org-archive-subtree-default t]
  17343. "--"
  17344. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  17345. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  17346. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  17347. )
  17348. "--"
  17349. ("Hyperlinks"
  17350. ["Store Link (Global)" org-store-link t]
  17351. ["Find existing link to here" org-occur-link-in-agenda-files t]
  17352. ["Insert Link" org-insert-link t]
  17353. ["Follow Link" org-open-at-point t]
  17354. "--"
  17355. ["Next link" org-next-link t]
  17356. ["Previous link" org-previous-link t]
  17357. "--"
  17358. ["Descriptive Links"
  17359. org-toggle-link-display
  17360. :style radio
  17361. :selected org-descriptive-links
  17362. ]
  17363. ["Literal Links"
  17364. org-toggle-link-display
  17365. :style radio
  17366. :selected (not org-descriptive-links)])
  17367. "--"
  17368. ("TODO Lists"
  17369. ["TODO/DONE/-" org-todo t]
  17370. ("Select keyword"
  17371. ["Next keyword" org-shiftright (org-at-heading-p)]
  17372. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  17373. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  17374. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  17375. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  17376. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  17377. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  17378. "--"
  17379. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  17380. :selected org-enforce-todo-dependencies :style toggle :active t]
  17381. "Settings for tree at point"
  17382. ["Do Children sequentially" org-toggle-ordered-property :style radio
  17383. :selected (org-entry-get nil "ORDERED")
  17384. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17385. ["Do Children parallel" org-toggle-ordered-property :style radio
  17386. :selected (not (org-entry-get nil "ORDERED"))
  17387. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17388. "--"
  17389. ["Set Priority" org-priority t]
  17390. ["Priority Up" org-shiftup t]
  17391. ["Priority Down" org-shiftdown t]
  17392. "--"
  17393. ["Get news from all feeds" org-feed-update-all t]
  17394. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  17395. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  17396. ("TAGS and Properties"
  17397. ["Set Tags" org-set-tags-command t]
  17398. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  17399. "--"
  17400. ["Set property" org-set-property t]
  17401. ["Column view of properties" org-columns t]
  17402. ["Insert Column View DBlock" org-insert-columns-dblock t])
  17403. ("Dates and Scheduling"
  17404. ["Timestamp" org-time-stamp t]
  17405. ["Timestamp (inactive)" org-time-stamp-inactive t]
  17406. ("Change Date"
  17407. ["1 Day Later" org-shiftright t]
  17408. ["1 Day Earlier" org-shiftleft t]
  17409. ["1 ... Later" org-shiftup t]
  17410. ["1 ... Earlier" org-shiftdown t])
  17411. ["Compute Time Range" org-evaluate-time-range t]
  17412. ["Schedule Item" org-schedule t]
  17413. ["Deadline" org-deadline t]
  17414. "--"
  17415. ["Custom time format" org-toggle-time-stamp-overlays
  17416. :style radio :selected org-display-custom-times]
  17417. "--"
  17418. ["Goto Calendar" org-goto-calendar t]
  17419. ["Date from Calendar" org-date-from-calendar t]
  17420. "--"
  17421. ["Start/Restart Timer" org-timer-start t]
  17422. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  17423. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  17424. ["Insert Timer String" org-timer t]
  17425. ["Insert Timer Item" org-timer-item t])
  17426. ("Logging work"
  17427. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  17428. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  17429. ["Clock out" org-clock-out t]
  17430. ["Clock cancel" org-clock-cancel t]
  17431. "--"
  17432. ["Mark as default task" org-clock-mark-default-task t]
  17433. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  17434. ["Goto running clock" org-clock-goto t]
  17435. "--"
  17436. ["Display times" org-clock-display t]
  17437. ["Create clock table" org-clock-report t]
  17438. "--"
  17439. ["Record DONE time"
  17440. (progn (setq org-log-done (not org-log-done))
  17441. (message "Switching to %s will %s record a timestamp"
  17442. (car org-done-keywords)
  17443. (if org-log-done "automatically" "not")))
  17444. :style toggle :selected org-log-done])
  17445. "--"
  17446. ["Agenda Command..." org-agenda t]
  17447. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  17448. ("File List for Agenda")
  17449. ("Special views current file"
  17450. ["TODO Tree" org-show-todo-tree t]
  17451. ["Check Deadlines" org-check-deadlines t]
  17452. ["Timeline" org-timeline t]
  17453. ["Tags/Property tree" org-match-sparse-tree t])
  17454. "--"
  17455. ["Export/Publish..." org-export t]
  17456. ("LaTeX"
  17457. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  17458. :selected org-cdlatex-mode]
  17459. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  17460. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  17461. ["Modify math symbol" org-cdlatex-math-modify
  17462. (org-inside-LaTeX-fragment-p)]
  17463. ["Insert citation" org-reftex-citation t]
  17464. "--"
  17465. ["Template for BEAMER" org-insert-beamer-options-template t])
  17466. "--"
  17467. ("MobileOrg"
  17468. ["Push Files and Views" org-mobile-push t]
  17469. ["Get Captured and Flagged" org-mobile-pull t]
  17470. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  17471. "--"
  17472. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  17473. "--"
  17474. ("Documentation"
  17475. ["Show Version" org-version t]
  17476. ["Info Documentation" org-info t])
  17477. ("Customize"
  17478. ["Browse Org Group" org-customize t]
  17479. "--"
  17480. ["Expand This Menu" org-create-customize-menu
  17481. (fboundp 'customize-menu-create)])
  17482. ["Send bug report" org-submit-bug-report t]
  17483. "--"
  17484. ("Refresh/Reload"
  17485. ["Refresh setup current buffer" org-mode-restart t]
  17486. ["Reload Org (after update)" org-reload t]
  17487. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  17488. ))
  17489. (defun org-info (&optional node)
  17490. "Read documentation for Org-mode in the info system.
  17491. With optional NODE, go directly to that node."
  17492. (interactive)
  17493. (info (format "(org)%s" (or node ""))))
  17494. ;;;###autoload
  17495. (defun org-submit-bug-report ()
  17496. "Submit a bug report on Org-mode via mail.
  17497. Don't hesitate to report any problems or inaccurate documentation.
  17498. If you don't have setup sending mail from (X)Emacs, please copy the
  17499. output buffer into your mail program, as it gives us important
  17500. information about your Org-mode version and configuration."
  17501. (interactive)
  17502. (require 'reporter)
  17503. (org-load-modules-maybe)
  17504. (org-require-autoloaded-modules)
  17505. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  17506. (reporter-submit-bug-report
  17507. "emacs-orgmode@gnu.org"
  17508. (org-version)
  17509. (let (list)
  17510. (save-window-excursion
  17511. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  17512. (delete-other-windows)
  17513. (erase-buffer)
  17514. (insert "You are about to submit a bug report to the Org-mode mailing list.
  17515. We would like to add your full Org-mode and Outline configuration to the
  17516. bug report. This greatly simplifies the work of the maintainer and
  17517. other experts on the mailing list.
  17518. HOWEVER, some variables you have customized may contain private
  17519. information. The names of customers, colleagues, or friends, might
  17520. appear in the form of file names, tags, todo states, or search strings.
  17521. If you answer yes to the prompt, you might want to check and remove
  17522. such private information before sending the email.")
  17523. (add-text-properties (point-min) (point-max) '(face org-warning))
  17524. (when (yes-or-no-p "Include your Org-mode configuration ")
  17525. (mapatoms
  17526. (lambda (v)
  17527. (and (boundp v)
  17528. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  17529. (or (and (symbol-value v)
  17530. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  17531. (and
  17532. (get v 'custom-type) (get v 'standard-value)
  17533. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  17534. (push v list)))))
  17535. (kill-buffer (get-buffer "*Warn about privacy*"))
  17536. list))
  17537. nil nil
  17538. "Remember to cover the basics, that is, what you expected to happen and
  17539. what in fact did happen. You don't know how to make a good report? See
  17540. http://orgmode.org/manual/Feedback.html#Feedback
  17541. Your bug report will be posted to the Org-mode mailing list.
  17542. ------------------------------------------------------------------------")
  17543. (save-excursion
  17544. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  17545. (replace-match "\\1Bug: \\3 [\\2]")))))
  17546. (defun org-install-agenda-files-menu ()
  17547. (let ((bl (buffer-list)))
  17548. (save-excursion
  17549. (while bl
  17550. (set-buffer (pop bl))
  17551. (if (eq major-mode 'org-mode) (setq bl nil)))
  17552. (when (eq major-mode 'org-mode)
  17553. (easy-menu-change
  17554. '("Org") "File List for Agenda"
  17555. (append
  17556. (list
  17557. ["Edit File List" (org-edit-agenda-file-list) t]
  17558. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  17559. ["Remove Current File from List" org-remove-file t]
  17560. ["Cycle through agenda files" org-cycle-agenda-files t]
  17561. ["Occur in all agenda files" org-occur-in-agenda-files t]
  17562. "--")
  17563. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  17564. ;;;; Documentation
  17565. ;;;###autoload
  17566. (defun org-require-autoloaded-modules ()
  17567. (interactive)
  17568. (mapc 'require
  17569. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  17570. org-docbook org-exp org-html org-icalendar
  17571. org-id org-latex
  17572. org-publish org-remember org-table
  17573. org-timer org-xoxo)))
  17574. ;;;###autoload
  17575. (defun org-reload (&optional uncompiled)
  17576. "Reload all org lisp files.
  17577. With prefix arg UNCOMPILED, load the uncompiled versions."
  17578. (interactive "P")
  17579. (require 'find-func)
  17580. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  17581. (dir-org (file-name-directory (org-find-library-name "org")))
  17582. (dir-org-contrib (ignore-errors
  17583. (file-name-directory
  17584. (org-find-library-name "org-contribdir"))))
  17585. (babel-files
  17586. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  17587. (append (list nil "comint" "eval" "exp" "keys"
  17588. "lob" "ref" "table" "tangle")
  17589. (delq nil
  17590. (mapcar
  17591. (lambda (lang)
  17592. (when (cdr lang) (symbol-name (car lang))))
  17593. org-babel-load-languages)))))
  17594. (files
  17595. (append (directory-files dir-org t file-re)
  17596. babel-files
  17597. (and dir-org-contrib
  17598. (directory-files dir-org-contrib t file-re))))
  17599. (remove-re (concat (if (featurep 'xemacs)
  17600. "org-colview" "org-colview-xemacs")
  17601. "\\'")))
  17602. (setq files (mapcar 'file-name-sans-extension files))
  17603. (setq files (mapcar
  17604. (lambda (x) (if (string-match remove-re x) nil x))
  17605. files))
  17606. (setq files (delq nil files))
  17607. (mapc
  17608. (lambda (f)
  17609. (when (featurep (intern (file-name-nondirectory f)))
  17610. (if (and (not uncompiled)
  17611. (file-exists-p (concat f ".elc")))
  17612. (load (concat f ".elc") nil nil t)
  17613. (load (concat f ".el") nil nil t))))
  17614. files))
  17615. (org-version))
  17616. ;;;###autoload
  17617. (defun org-customize ()
  17618. "Call the customize function with org as argument."
  17619. (interactive)
  17620. (org-load-modules-maybe)
  17621. (org-require-autoloaded-modules)
  17622. (customize-browse 'org))
  17623. (defun org-create-customize-menu ()
  17624. "Create a full customization menu for Org-mode, insert it into the menu."
  17625. (interactive)
  17626. (org-load-modules-maybe)
  17627. (org-require-autoloaded-modules)
  17628. (if (fboundp 'customize-menu-create)
  17629. (progn
  17630. (easy-menu-change
  17631. '("Org") "Customize"
  17632. `(["Browse Org group" org-customize t]
  17633. "--"
  17634. ,(customize-menu-create 'org)
  17635. ["Set" Custom-set t]
  17636. ["Save" Custom-save t]
  17637. ["Reset to Current" Custom-reset-current t]
  17638. ["Reset to Saved" Custom-reset-saved t]
  17639. ["Reset to Standard Settings" Custom-reset-standard t]))
  17640. (message "\"Org\"-menu now contains full customization menu"))
  17641. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  17642. ;;;; Miscellaneous stuff
  17643. ;;; Generally useful functions
  17644. (defun org-get-at-bol (property)
  17645. "Get text property PROPERTY at beginning of line."
  17646. (get-text-property (point-at-bol) property))
  17647. (defun org-find-text-property-in-string (prop s)
  17648. "Return the first non-nil value of property PROP in string S."
  17649. (or (get-text-property 0 prop s)
  17650. (get-text-property (or (next-single-property-change 0 prop s) 0)
  17651. prop s)))
  17652. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  17653. "Display the given MESSAGE as a warning."
  17654. (if (fboundp 'display-warning)
  17655. (display-warning 'org message
  17656. (if (featurep 'xemacs) 'warning :warning))
  17657. (let ((buf (get-buffer-create "*Org warnings*")))
  17658. (with-current-buffer buf
  17659. (goto-char (point-max))
  17660. (insert "Warning (Org): " message)
  17661. (unless (bolp)
  17662. (newline)))
  17663. (display-buffer buf)
  17664. (sit-for 0))))
  17665. (defun org-eval (form)
  17666. "Eval FORM and return result."
  17667. (condition-case error
  17668. (eval form)
  17669. (error (format "%%![Error: %s]" error))))
  17670. (defun org-in-commented-line ()
  17671. "Is point in a line starting with `#'?"
  17672. (equal (char-after (point-at-bol)) ?#))
  17673. (defun org-in-indented-comment-line ()
  17674. "Is point in a line starting with `#' after some white space?"
  17675. (save-excursion
  17676. (save-match-data
  17677. (goto-char (point-at-bol))
  17678. (looking-at "[ \t]*#"))))
  17679. (defun org-in-verbatim-emphasis ()
  17680. (save-match-data
  17681. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  17682. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  17683. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  17684. (if (and marker (marker-buffer marker)
  17685. (buffer-live-p (marker-buffer marker)))
  17686. (progn
  17687. (org-pop-to-buffer-same-window (marker-buffer marker))
  17688. (if (or (> marker (point-max)) (< marker (point-min)))
  17689. (widen))
  17690. (goto-char marker)
  17691. (org-show-context 'org-goto))
  17692. (if bookmark
  17693. (bookmark-jump bookmark)
  17694. (error "Cannot find location"))))
  17695. (defun org-quote-csv-field (s)
  17696. "Quote field for inclusion in CSV material."
  17697. (if (string-match "[\",]" s)
  17698. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  17699. s))
  17700. (defun org-force-self-insert (N)
  17701. "Needed to enforce self-insert under remapping."
  17702. (interactive "p")
  17703. (self-insert-command N))
  17704. (defun org-string-width (s)
  17705. "Compute width of string, ignoring invisible characters.
  17706. This ignores character with invisibility property `org-link', and also
  17707. characters with property `org-cwidth', because these will become invisible
  17708. upon the next fontification round."
  17709. (let (b l)
  17710. (when (or (eq t buffer-invisibility-spec)
  17711. (assq 'org-link buffer-invisibility-spec))
  17712. (while (setq b (text-property-any 0 (length s)
  17713. 'invisible 'org-link s))
  17714. (setq s (concat (substring s 0 b)
  17715. (substring s (or (next-single-property-change
  17716. b 'invisible s) (length s)))))))
  17717. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  17718. (setq s (concat (substring s 0 b)
  17719. (substring s (or (next-single-property-change
  17720. b 'org-cwidth s) (length s))))))
  17721. (setq l (string-width s) b -1)
  17722. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  17723. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  17724. l))
  17725. (defun org-shorten-string (s maxlength)
  17726. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  17727. If the string is shorter or has length MAXLENGTH, just return the
  17728. original string. If it is longer, the functions finds a space in the
  17729. string, breaks this string off at that locations and adds three dots
  17730. as ellipsis. Including the ellipsis, the string will not be longer
  17731. than MAXLENGTH. If finding a good breaking point in the string does
  17732. not work, the string is just chopped off in the middle of a word
  17733. if necessary."
  17734. (if (<= (length s) maxlength)
  17735. s
  17736. (let* ((n (max (- maxlength 4) 1))
  17737. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  17738. (if (string-match re s)
  17739. (concat (match-string 1 s) "...")
  17740. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  17741. (defun org-get-indentation (&optional line)
  17742. "Get the indentation of the current line, interpreting tabs.
  17743. When LINE is given, assume it represents a line and compute its indentation."
  17744. (if line
  17745. (if (string-match "^ *" (org-remove-tabs line))
  17746. (match-end 0))
  17747. (save-excursion
  17748. (beginning-of-line 1)
  17749. (skip-chars-forward " \t")
  17750. (current-column))))
  17751. (defun org-get-string-indentation (s)
  17752. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  17753. (let ((n -1) (i 0) (w tab-width) c)
  17754. (catch 'exit
  17755. (while (< (setq n (1+ n)) (length s))
  17756. (setq c (aref s n))
  17757. (cond ((= c ?\ ) (setq i (1+ i)))
  17758. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  17759. (t (throw 'exit t)))))
  17760. i))
  17761. (defun org-remove-tabs (s &optional width)
  17762. "Replace tabulators in S with spaces.
  17763. Assumes that s is a single line, starting in column 0."
  17764. (setq width (or width tab-width))
  17765. (while (string-match "\t" s)
  17766. (setq s (replace-match
  17767. (make-string
  17768. (- (* width (/ (+ (match-beginning 0) width) width))
  17769. (match-beginning 0)) ?\ )
  17770. t t s)))
  17771. s)
  17772. (defun org-fix-indentation (line ind)
  17773. "Fix indentation in LINE.
  17774. IND is a cons cell with target and minimum indentation.
  17775. If the current indentation in LINE is smaller than the minimum,
  17776. leave it alone. If it is larger than ind, set it to the target."
  17777. (let* ((l (org-remove-tabs line))
  17778. (i (org-get-indentation l))
  17779. (i1 (car ind)) (i2 (cdr ind)))
  17780. (if (>= i i2) (setq l (substring line i2)))
  17781. (if (> i1 0)
  17782. (concat (make-string i1 ?\ ) l)
  17783. l)))
  17784. (defun org-remove-indentation (code &optional n)
  17785. "Remove the maximum common indentation from the lines in CODE.
  17786. N may optionally be the number of spaces to remove."
  17787. (with-temp-buffer
  17788. (insert code)
  17789. (org-do-remove-indentation n)
  17790. (buffer-string)))
  17791. (defun org-do-remove-indentation (&optional n)
  17792. "Remove the maximum common indentation from the buffer."
  17793. (untabify (point-min) (point-max))
  17794. (let ((min 10000) re)
  17795. (if n
  17796. (setq min n)
  17797. (goto-char (point-min))
  17798. (while (re-search-forward "^ *[^ \n]" nil t)
  17799. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  17800. (unless (or (= min 0) (= min 10000))
  17801. (setq re (format "^ \\{%d\\}" min))
  17802. (goto-char (point-min))
  17803. (while (re-search-forward re nil t)
  17804. (replace-match "")
  17805. (end-of-line 1))
  17806. min)))
  17807. (defun org-fill-template (template alist)
  17808. "Find each %key of ALIST in TEMPLATE and replace it."
  17809. (let ((case-fold-search nil)
  17810. entry key value)
  17811. (setq alist (sort (copy-sequence alist)
  17812. (lambda (a b) (< (length (car a)) (length (car b))))))
  17813. (while (setq entry (pop alist))
  17814. (setq template
  17815. (replace-regexp-in-string
  17816. (concat "%" (regexp-quote (car entry)))
  17817. (cdr entry) template t t)))
  17818. template))
  17819. (defun org-base-buffer (buffer)
  17820. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  17821. (if (not buffer)
  17822. buffer
  17823. (or (buffer-base-buffer buffer)
  17824. buffer)))
  17825. (defun org-trim (s)
  17826. "Remove whitespace at beginning and end of string."
  17827. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  17828. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  17829. s)
  17830. (defun org-wrap (string &optional width lines)
  17831. "Wrap string to either a number of lines, or a width in characters.
  17832. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  17833. that costs. If there is a word longer than WIDTH, the text is actually
  17834. wrapped to the length of that word.
  17835. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  17836. many lines, whatever width that takes.
  17837. The return value is a list of lines, without newlines at the end."
  17838. (let* ((words (org-split-string string "[ \t\n]+"))
  17839. (maxword (apply 'max (mapcar 'org-string-width words)))
  17840. w ll)
  17841. (cond (width
  17842. (org-do-wrap words (max maxword width)))
  17843. (lines
  17844. (setq w maxword)
  17845. (setq ll (org-do-wrap words maxword))
  17846. (if (<= (length ll) lines)
  17847. ll
  17848. (setq ll words)
  17849. (while (> (length ll) lines)
  17850. (setq w (1+ w))
  17851. (setq ll (org-do-wrap words w)))
  17852. ll))
  17853. (t (error "Cannot wrap this")))))
  17854. (defun org-do-wrap (words width)
  17855. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  17856. (let (lines line)
  17857. (while words
  17858. (setq line (pop words))
  17859. (while (and words (< (+ (length line) (length (car words))) width))
  17860. (setq line (concat line " " (pop words))))
  17861. (setq lines (push line lines)))
  17862. (nreverse lines)))
  17863. (defun org-split-string (string &optional separators)
  17864. "Splits STRING into substrings at SEPARATORS.
  17865. No empty strings are returned if there are matches at the beginning
  17866. and end of string."
  17867. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  17868. (start 0)
  17869. notfirst
  17870. (list nil))
  17871. (while (and (string-match rexp string
  17872. (if (and notfirst
  17873. (= start (match-beginning 0))
  17874. (< start (length string)))
  17875. (1+ start) start))
  17876. (< (match-beginning 0) (length string)))
  17877. (setq notfirst t)
  17878. (or (eq (match-beginning 0) 0)
  17879. (and (eq (match-beginning 0) (match-end 0))
  17880. (eq (match-beginning 0) start))
  17881. (setq list
  17882. (cons (substring string start (match-beginning 0))
  17883. list)))
  17884. (setq start (match-end 0)))
  17885. (or (eq start (length string))
  17886. (setq list
  17887. (cons (substring string start)
  17888. list)))
  17889. (nreverse list)))
  17890. (defun org-quote-vert (s)
  17891. "Replace \"|\" with \"\\vert\"."
  17892. (while (string-match "|" s)
  17893. (setq s (replace-match "\\vert" t t s)))
  17894. s)
  17895. (defun org-uuidgen-p (s)
  17896. "Is S an ID created by UUIDGEN?"
  17897. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  17898. (defun org-context ()
  17899. "Return a list of contexts of the current cursor position.
  17900. If several contexts apply, all are returned.
  17901. Each context entry is a list with a symbol naming the context, and
  17902. two positions indicating start and end of the context. Possible
  17903. contexts are:
  17904. :headline anywhere in a headline
  17905. :headline-stars on the leading stars in a headline
  17906. :todo-keyword on a TODO keyword (including DONE) in a headline
  17907. :tags on the TAGS in a headline
  17908. :priority on the priority cookie in a headline
  17909. :item on the first line of a plain list item
  17910. :item-bullet on the bullet/number of a plain list item
  17911. :checkbox on the checkbox in a plain list item
  17912. :table in an org-mode table
  17913. :table-special on a special filed in a table
  17914. :table-table in a table.el table
  17915. :link on a hyperlink
  17916. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  17917. :target on a <<target>>
  17918. :radio-target on a <<<radio-target>>>
  17919. :latex-fragment on a LaTeX fragment
  17920. :latex-preview on a LaTeX fragment with overlaid preview image
  17921. This function expects the position to be visible because it uses font-lock
  17922. faces as a help to recognize the following contexts: :table-special, :link,
  17923. and :keyword."
  17924. (let* ((f (get-text-property (point) 'face))
  17925. (faces (if (listp f) f (list f)))
  17926. (p (point)) clist o)
  17927. ;; First the large context
  17928. (cond
  17929. ((org-at-heading-p t)
  17930. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  17931. (when (progn
  17932. (beginning-of-line 1)
  17933. (looking-at org-todo-line-tags-regexp))
  17934. (push (org-point-in-group p 1 :headline-stars) clist)
  17935. (push (org-point-in-group p 2 :todo-keyword) clist)
  17936. (push (org-point-in-group p 4 :tags) clist))
  17937. (goto-char p)
  17938. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  17939. (if (looking-at "\\[#[A-Z0-9]\\]")
  17940. (push (org-point-in-group p 0 :priority) clist)))
  17941. ((org-at-item-p)
  17942. (push (org-point-in-group p 2 :item-bullet) clist)
  17943. (push (list :item (point-at-bol)
  17944. (save-excursion (org-end-of-item) (point)))
  17945. clist)
  17946. (and (org-at-item-checkbox-p)
  17947. (push (org-point-in-group p 0 :checkbox) clist)))
  17948. ((org-at-table-p)
  17949. (push (list :table (org-table-begin) (org-table-end)) clist)
  17950. (if (memq 'org-formula faces)
  17951. (push (list :table-special
  17952. (previous-single-property-change p 'face)
  17953. (next-single-property-change p 'face)) clist)))
  17954. ((org-at-table-p 'any)
  17955. (push (list :table-table) clist)))
  17956. (goto-char p)
  17957. ;; Now the small context
  17958. (cond
  17959. ((org-at-timestamp-p)
  17960. (push (org-point-in-group p 0 :timestamp) clist))
  17961. ((memq 'org-link faces)
  17962. (push (list :link
  17963. (previous-single-property-change p 'face)
  17964. (next-single-property-change p 'face)) clist))
  17965. ((memq 'org-special-keyword faces)
  17966. (push (list :keyword
  17967. (previous-single-property-change p 'face)
  17968. (next-single-property-change p 'face)) clist))
  17969. ((org-at-target-p)
  17970. (push (org-point-in-group p 0 :target) clist)
  17971. (goto-char (1- (match-beginning 0)))
  17972. (if (looking-at org-radio-target-regexp)
  17973. (push (org-point-in-group p 0 :radio-target) clist))
  17974. (goto-char p))
  17975. ((setq o (car (delq nil
  17976. (mapcar
  17977. (lambda (x)
  17978. (if (memq x org-latex-fragment-image-overlays) x))
  17979. (overlays-at (point))))))
  17980. (push (list :latex-fragment
  17981. (overlay-start o) (overlay-end o)) clist)
  17982. (push (list :latex-preview
  17983. (overlay-start o) (overlay-end o)) clist))
  17984. ((org-inside-LaTeX-fragment-p)
  17985. ;; FIXME: positions wrong.
  17986. (push (list :latex-fragment (point) (point)) clist)))
  17987. (setq clist (nreverse (delq nil clist)))
  17988. clist))
  17989. ;; FIXME: Compare with at-regexp-p Do we need both?
  17990. (defun org-in-regexp (re &optional nlines visually)
  17991. "Check if point is inside a match of regexp.
  17992. Normally only the current line is checked, but you can include NLINES extra
  17993. lines both before and after point into the search.
  17994. If VISUALLY is set, require that the cursor is not after the match but
  17995. really on, so that the block visually is on the match."
  17996. (catch 'exit
  17997. (let ((pos (point))
  17998. (eol (point-at-eol (+ 1 (or nlines 0))))
  17999. (inc (if visually 1 0)))
  18000. (save-excursion
  18001. (beginning-of-line (- 1 (or nlines 0)))
  18002. (while (re-search-forward re eol t)
  18003. (if (and (<= (match-beginning 0) pos)
  18004. (>= (+ inc (match-end 0)) pos))
  18005. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  18006. (defun org-at-regexp-p (regexp)
  18007. "Is point inside a match of REGEXP in the current line?"
  18008. (catch 'exit
  18009. (save-excursion
  18010. (let ((pos (point)) (end (point-at-eol)))
  18011. (beginning-of-line 1)
  18012. (while (re-search-forward regexp end t)
  18013. (if (and (<= (match-beginning 0) pos)
  18014. (>= (match-end 0) pos))
  18015. (throw 'exit t)))
  18016. nil))))
  18017. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  18018. "Non-nil when point is between matches of START-RE and END-RE.
  18019. Also return a non-nil value when point is on one of the matches.
  18020. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  18021. buffer positions. Default values are the positions of headlines
  18022. surrounding the point.
  18023. The functions returns a cons cell whose car (resp. cdr) is the
  18024. position before START-RE (resp. after END-RE)."
  18025. (save-match-data
  18026. (let ((pos (point))
  18027. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  18028. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  18029. beg end)
  18030. (save-excursion
  18031. ;; Point is on a block when on START-RE or if START-RE can be
  18032. ;; found before it...
  18033. (and (or (org-at-regexp-p start-re)
  18034. (re-search-backward start-re limit-up t))
  18035. (setq beg (match-beginning 0))
  18036. ;; ... and END-RE after it...
  18037. (goto-char (match-end 0))
  18038. (re-search-forward end-re limit-down t)
  18039. (> (setq end (match-end 0)) pos)
  18040. ;; ... without another START-RE in-between.
  18041. (goto-char (match-beginning 0))
  18042. (not (re-search-backward start-re (1+ beg) t))
  18043. ;; Return value.
  18044. (cons beg end))))))
  18045. (defun org-in-block-p (names)
  18046. "Non-nil when point belongs to a block whose name belongs to NAMES.
  18047. NAMES is a list of strings containing names of blocks.
  18048. Return first block name matched, or nil. Beware that in case of
  18049. nested blocks, the returned name may not belong to the closest
  18050. block from point."
  18051. (save-match-data
  18052. (catch 'exit
  18053. (let ((case-fold-search t)
  18054. (lim-up (save-excursion (outline-previous-heading)))
  18055. (lim-down (save-excursion (outline-next-heading))))
  18056. (mapc (lambda (name)
  18057. (let ((n (regexp-quote name)))
  18058. (when (org-between-regexps-p
  18059. (concat "^[ \t]*#\\+begin_" n)
  18060. (concat "^[ \t]*#\\+end_" n)
  18061. lim-up lim-down)
  18062. (throw 'exit n))))
  18063. names))
  18064. nil)))
  18065. (defun org-occur-in-agenda-files (regexp &optional nlines)
  18066. "Call `multi-occur' with buffers for all agenda files."
  18067. (interactive "sOrg-files matching: \np")
  18068. (let* ((files (org-agenda-files))
  18069. (tnames (mapcar 'file-truename files))
  18070. (extra org-agenda-text-search-extra-files)
  18071. f)
  18072. (when (eq (car extra) 'agenda-archives)
  18073. (setq extra (cdr extra))
  18074. (setq files (org-add-archive-files files)))
  18075. (while (setq f (pop extra))
  18076. (unless (member (file-truename f) tnames)
  18077. (add-to-list 'files f 'append)
  18078. (add-to-list 'tnames (file-truename f) 'append)))
  18079. (multi-occur
  18080. (mapcar (lambda (x)
  18081. (with-current-buffer
  18082. (or (get-file-buffer x) (find-file-noselect x))
  18083. (widen)
  18084. (current-buffer)))
  18085. files)
  18086. regexp)))
  18087. (if (boundp 'occur-mode-find-occurrence-hook)
  18088. ;; Emacs 23
  18089. (add-hook 'occur-mode-find-occurrence-hook
  18090. (lambda ()
  18091. (when (eq major-mode 'org-mode)
  18092. (org-reveal))))
  18093. ;; Emacs 22
  18094. (defadvice occur-mode-goto-occurrence
  18095. (after org-occur-reveal activate)
  18096. (and (eq major-mode 'org-mode) (org-reveal)))
  18097. (defadvice occur-mode-goto-occurrence-other-window
  18098. (after org-occur-reveal activate)
  18099. (and (eq major-mode 'org-mode) (org-reveal)))
  18100. (defadvice occur-mode-display-occurrence
  18101. (after org-occur-reveal activate)
  18102. (when (eq major-mode 'org-mode)
  18103. (let ((pos (occur-mode-find-occurrence)))
  18104. (with-current-buffer (marker-buffer pos)
  18105. (save-excursion
  18106. (goto-char pos)
  18107. (org-reveal)))))))
  18108. (defun org-occur-link-in-agenda-files ()
  18109. "Create a link and search for it in the agendas.
  18110. The link is not stored in `org-stored-links', it is just created
  18111. for the search purpose."
  18112. (interactive)
  18113. (let ((link (condition-case nil
  18114. (org-store-link nil)
  18115. (error "Unable to create a link to here"))))
  18116. (org-occur-in-agenda-files (regexp-quote link))))
  18117. (defun org-uniquify (list)
  18118. "Remove duplicate elements from LIST."
  18119. (let (res)
  18120. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18121. res))
  18122. (defun org-delete-all (elts list)
  18123. "Remove all elements in ELTS from LIST."
  18124. (while elts
  18125. (setq list (delete (pop elts) list)))
  18126. list)
  18127. (defun org-count (cl-item cl-seq)
  18128. "Count the number of occurrences of ITEM in SEQ.
  18129. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18130. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18131. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18132. (while (< cl-start cl-end)
  18133. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18134. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18135. (setq cl-start (1+ cl-start)))
  18136. cl-count))
  18137. (defun org-remove-if (predicate seq)
  18138. "Remove everything from SEQ that fulfills PREDICATE."
  18139. (let (res e)
  18140. (while seq
  18141. (setq e (pop seq))
  18142. (if (not (funcall predicate e)) (push e res)))
  18143. (nreverse res)))
  18144. (defun org-remove-if-not (predicate seq)
  18145. "Remove everything from SEQ that does not fulfill PREDICATE."
  18146. (let (res e)
  18147. (while seq
  18148. (setq e (pop seq))
  18149. (if (funcall predicate e) (push e res)))
  18150. (nreverse res)))
  18151. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  18152. "Reduce two-argument FUNCTION across SEQ.
  18153. Taken from `reduce' in cl-seq.el with all keyword arguments but
  18154. \":initial-value\" removed."
  18155. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  18156. (cadr (memq :initial-value cl-keys)))
  18157. (cl-seq (pop cl-seq))
  18158. (t (funcall cl-func)))))
  18159. (while cl-seq
  18160. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  18161. cl-accum))
  18162. (defun org-back-over-empty-lines ()
  18163. "Move backwards over whitespace, to the beginning of the first empty line.
  18164. Returns the number of empty lines passed."
  18165. (let ((pos (point)))
  18166. (if (cdr (assoc 'heading org-blank-before-new-entry))
  18167. (skip-chars-backward " \t\n\r")
  18168. (unless (eobp)
  18169. (forward-line -1)))
  18170. (beginning-of-line 2)
  18171. (goto-char (min (point) pos))
  18172. (count-lines (point) pos)))
  18173. (defun org-skip-whitespace ()
  18174. (skip-chars-forward " \t\n\r"))
  18175. (defun org-point-in-group (point group &optional context)
  18176. "Check if POINT is in match-group GROUP.
  18177. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  18178. match. If the match group does not exist or point is not inside it,
  18179. return nil."
  18180. (and (match-beginning group)
  18181. (>= point (match-beginning group))
  18182. (<= point (match-end group))
  18183. (if context
  18184. (list context (match-beginning group) (match-end group))
  18185. t)))
  18186. (defun org-switch-to-buffer-other-window (&rest args)
  18187. "Switch to buffer in a second window on the current frame.
  18188. In particular, do not allow pop-up frames.
  18189. Returns the newly created buffer."
  18190. (let (pop-up-frames special-display-buffer-names special-display-regexps
  18191. special-display-function)
  18192. (apply 'switch-to-buffer-other-window args)))
  18193. (defun org-combine-plists (&rest plists)
  18194. "Create a single property list from all plists in PLISTS.
  18195. The process starts by copying the first list, and then setting properties
  18196. from the other lists. Settings in the last list are the most significant
  18197. ones and overrule settings in the other lists."
  18198. (let ((rtn (copy-sequence (pop plists)))
  18199. p v ls)
  18200. (while plists
  18201. (setq ls (pop plists))
  18202. (while ls
  18203. (setq p (pop ls) v (pop ls))
  18204. (setq rtn (plist-put rtn p v))))
  18205. rtn))
  18206. (defun org-move-line-down (arg)
  18207. "Move the current line down. With prefix argument, move it past ARG lines."
  18208. (interactive "p")
  18209. (let ((col (current-column))
  18210. beg end pos)
  18211. (beginning-of-line 1) (setq beg (point))
  18212. (beginning-of-line 2) (setq end (point))
  18213. (beginning-of-line (+ 1 arg))
  18214. (setq pos (move-marker (make-marker) (point)))
  18215. (insert (delete-and-extract-region beg end))
  18216. (goto-char pos)
  18217. (org-move-to-column col)))
  18218. (defun org-move-line-up (arg)
  18219. "Move the current line up. With prefix argument, move it past ARG lines."
  18220. (interactive "p")
  18221. (let ((col (current-column))
  18222. beg end pos)
  18223. (beginning-of-line 1) (setq beg (point))
  18224. (beginning-of-line 2) (setq end (point))
  18225. (beginning-of-line (- arg))
  18226. (setq pos (move-marker (make-marker) (point)))
  18227. (insert (delete-and-extract-region beg end))
  18228. (goto-char pos)
  18229. (org-move-to-column col)))
  18230. (defun org-replace-escapes (string table)
  18231. "Replace %-escapes in STRING with values in TABLE.
  18232. TABLE is an association list with keys like \"%a\" and string values.
  18233. The sequences in STRING may contain normal field width and padding information,
  18234. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  18235. so values can contain further %-escapes if they are define later in TABLE."
  18236. (let ((tbl (copy-alist table))
  18237. (case-fold-search nil)
  18238. (pchg 0)
  18239. e re rpl)
  18240. (while (setq e (pop tbl))
  18241. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  18242. (when (and (cdr e) (string-match re (cdr e)))
  18243. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  18244. (safe "SREF"))
  18245. (add-text-properties 0 3 (list 'sref sref) safe)
  18246. (setcdr e (replace-match safe t t (cdr e)))))
  18247. (while (string-match re string)
  18248. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  18249. (cdr e)))
  18250. (setq string (replace-match rpl t t string))))
  18251. (while (setq pchg (next-property-change pchg string))
  18252. (let ((sref (get-text-property pchg 'sref string)))
  18253. (when (and sref (string-match "SREF" string pchg))
  18254. (setq string (replace-match sref t t string)))))
  18255. string))
  18256. (defun org-sublist (list start end)
  18257. "Return a section of LIST, from START to END.
  18258. Counting starts at 1."
  18259. (let (rtn (c start))
  18260. (setq list (nthcdr (1- start) list))
  18261. (while (and list (<= c end))
  18262. (push (pop list) rtn)
  18263. (setq c (1+ c)))
  18264. (nreverse rtn)))
  18265. (defun org-find-base-buffer-visiting (file)
  18266. "Like `find-buffer-visiting' but always return the base buffer and
  18267. not an indirect buffer."
  18268. (let ((buf (or (get-file-buffer file)
  18269. (find-buffer-visiting file))))
  18270. (if buf
  18271. (or (buffer-base-buffer buf) buf)
  18272. nil)))
  18273. (defun org-image-file-name-regexp (&optional extensions)
  18274. "Return regexp matching the file names of images.
  18275. If EXTENSIONS is given, only match these."
  18276. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  18277. (image-file-name-regexp)
  18278. (let ((image-file-name-extensions
  18279. (or extensions
  18280. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  18281. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  18282. (concat "\\."
  18283. (regexp-opt (nconc (mapcar 'upcase
  18284. image-file-name-extensions)
  18285. image-file-name-extensions)
  18286. t)
  18287. "\\'"))))
  18288. (defun org-file-image-p (file &optional extensions)
  18289. "Return non-nil if FILE is an image."
  18290. (save-match-data
  18291. (string-match (org-image-file-name-regexp extensions) file)))
  18292. (defun org-get-cursor-date ()
  18293. "Return the date at cursor in as a time.
  18294. This works in the calendar and in the agenda, anywhere else it just
  18295. returns the current time."
  18296. (let (date day defd)
  18297. (cond
  18298. ((eq major-mode 'calendar-mode)
  18299. (setq date (calendar-cursor-to-date)
  18300. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  18301. ((eq major-mode 'org-agenda-mode)
  18302. (setq day (get-text-property (point) 'day))
  18303. (if day
  18304. (setq date (calendar-gregorian-from-absolute day)
  18305. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  18306. (nth 2 date))))))
  18307. (or defd (current-time))))
  18308. (defvar org-agenda-action-marker (make-marker)
  18309. "Marker pointing to the entry for the next agenda action.")
  18310. (defun org-mark-entry-for-agenda-action ()
  18311. "Mark the current entry as target of an agenda action.
  18312. Agenda actions are actions executed from the agenda with the key `k',
  18313. which make use of the date at the cursor."
  18314. (interactive)
  18315. (move-marker org-agenda-action-marker
  18316. (save-excursion (org-back-to-heading t) (point))
  18317. (current-buffer))
  18318. (message
  18319. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  18320. (defun org-mark-subtree ()
  18321. "Mark the current subtree.
  18322. This puts point at the start of the current subtree, and mark at the end.
  18323. If point is in an inline task, mark that task instead."
  18324. (interactive)
  18325. (let ((inline-task-p
  18326. (and (featurep 'org-inlinetask)
  18327. (org-inlinetask-in-task-p)))
  18328. (beg))
  18329. ;; Get beginning of subtree
  18330. (cond
  18331. (inline-task-p (org-inlinetask-goto-beginning))
  18332. ((org-at-heading-p) (beginning-of-line))
  18333. (t (org-with-limited-levels (outline-previous-visible-heading 1))))
  18334. (setq beg (point))
  18335. ;; Get end of it
  18336. (if inline-task-p
  18337. (org-inlinetask-goto-end)
  18338. (org-end-of-subtree))
  18339. ;; Mark zone
  18340. (push-mark (point) nil t)
  18341. (goto-char beg)))
  18342. ;;; Paragraph filling stuff.
  18343. ;; We want this to be just right, so use the full arsenal.
  18344. (defun org-indent-line-function ()
  18345. "Indent line depending on context."
  18346. (interactive)
  18347. (let* ((pos (point))
  18348. (itemp (org-at-item-p))
  18349. (case-fold-search t)
  18350. (org-drawer-regexp (or org-drawer-regexp "\000"))
  18351. (inline-task-p (and (featurep 'org-inlinetask)
  18352. (org-inlinetask-in-task-p)))
  18353. (inline-re (and inline-task-p
  18354. (org-inlinetask-outline-regexp)))
  18355. column)
  18356. (beginning-of-line 1)
  18357. (cond
  18358. ;; Comments
  18359. ((looking-at "# ") (setq column 0))
  18360. ;; Headings
  18361. ((looking-at org-outline-regexp) (setq column 0))
  18362. ;; Included files
  18363. ((looking-at "#\\+include:") (setq column 0))
  18364. ;; Footnote definition
  18365. ((looking-at org-footnote-definition-re) (setq column 0))
  18366. ;; Literal examples
  18367. ((looking-at "[ \t]*:\\( \\|$\\)")
  18368. (setq column (org-get-indentation))) ; do nothing
  18369. ;; Lists
  18370. ((ignore-errors (goto-char (org-in-item-p)))
  18371. (setq column (if itemp
  18372. (org-get-indentation)
  18373. (org-list-item-body-column (point))))
  18374. (goto-char pos))
  18375. ;; Drawers
  18376. ((and (looking-at "[ \t]*:END:")
  18377. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  18378. (save-excursion
  18379. (goto-char (1- (match-beginning 1)))
  18380. (setq column (current-column))))
  18381. ;; Special blocks
  18382. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  18383. (save-excursion
  18384. (re-search-backward
  18385. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  18386. (setq column (org-get-indentation (match-string 0))))
  18387. ((and (not (looking-at "[ \t]*#\\+begin_"))
  18388. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  18389. (save-excursion
  18390. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  18391. (setq column
  18392. (cond ((equal (downcase (match-string 1)) "src")
  18393. ;; src blocks: let `org-edit-src-exit' handle them
  18394. (org-get-indentation))
  18395. ((equal (downcase (match-string 1)) "example")
  18396. (max (org-get-indentation)
  18397. (org-get-indentation (match-string 0))))
  18398. (t
  18399. (org-get-indentation (match-string 0))))))
  18400. ;; This line has nothing special, look at the previous relevant
  18401. ;; line to compute indentation
  18402. (t
  18403. (beginning-of-line 0)
  18404. (while (and (not (bobp))
  18405. (not (looking-at org-drawer-regexp))
  18406. ;; When point started in an inline task, do not move
  18407. ;; above task starting line.
  18408. (not (and inline-task-p (looking-at inline-re)))
  18409. ;; Skip drawers, blocks, empty lines, verbatim,
  18410. ;; comments, tables, footnotes definitions, lists,
  18411. ;; inline tasks.
  18412. (or (and (looking-at "[ \t]*:END:")
  18413. (re-search-backward org-drawer-regexp nil t))
  18414. (and (looking-at "[ \t]*#\\+end_")
  18415. (re-search-backward "[ \t]*#\\+begin_"nil t))
  18416. (looking-at "[ \t]*[\n:#|]")
  18417. (looking-at org-footnote-definition-re)
  18418. (and (ignore-errors (goto-char (org-in-item-p)))
  18419. (goto-char
  18420. (org-list-get-top-point (org-list-struct))))
  18421. (and (not inline-task-p)
  18422. (featurep 'org-inlinetask)
  18423. (org-inlinetask-in-task-p)
  18424. (or (org-inlinetask-goto-beginning) t))))
  18425. (beginning-of-line 0))
  18426. (cond
  18427. ;; There was an heading above.
  18428. ((looking-at "\\*+[ \t]+")
  18429. (if (not org-adapt-indentation)
  18430. (setq column 0)
  18431. (goto-char (match-end 0))
  18432. (setq column (current-column))))
  18433. ;; A drawer had started and is unfinished
  18434. ((looking-at org-drawer-regexp)
  18435. (goto-char (1- (match-beginning 1)))
  18436. (setq column (current-column)))
  18437. ;; Else, nothing noticeable found: get indentation and go on.
  18438. (t (setq column (org-get-indentation))))))
  18439. ;; Now apply indentation and move cursor accordingly
  18440. (goto-char pos)
  18441. (if (<= (current-column) (current-indentation))
  18442. (org-indent-line-to column)
  18443. (save-excursion (org-indent-line-to column)))
  18444. ;; Special polishing for properties, see `org-property-format'
  18445. (setq column (current-column))
  18446. (beginning-of-line 1)
  18447. (if (looking-at
  18448. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  18449. (replace-match (concat (match-string 1)
  18450. (format org-property-format
  18451. (match-string 2) (match-string 3)))
  18452. t t))
  18453. (org-move-to-column column)))
  18454. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  18455. "Variable to store copy of `adaptive-fill-regexp'.
  18456. Since `adaptive-fill-regexp' is set to never match, we need to
  18457. store a backup of its value before entering `org-mode' so that
  18458. the functionality can be provided as a fall-back.")
  18459. (defun org-set-autofill-regexps ()
  18460. (interactive)
  18461. ;; In the paragraph separator we include headlines, because filling
  18462. ;; text in a line directly attached to a headline would otherwise
  18463. ;; fill the headline as well.
  18464. (org-set-local 'comment-start-skip "^#+[ \t]*")
  18465. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  18466. ;; The paragraph starter includes hand-formatted lists.
  18467. (org-set-local
  18468. 'paragraph-start
  18469. (concat
  18470. "\f" "\\|"
  18471. "[ ]*$" "\\|"
  18472. org-outline-regexp "\\|"
  18473. "[ \t]*#" "\\|"
  18474. (org-item-re) "\\|"
  18475. "[ \t]*[:|]" "\\|"
  18476. "\\$\\$" "\\|"
  18477. "\\\\\\(begin\\|end\\|[][]\\)"))
  18478. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  18479. ;; But only if the user has not turned off tables or fixed-width regions
  18480. (org-set-local
  18481. 'auto-fill-inhibit-regexp
  18482. (concat org-outline-regexp
  18483. "\\|#\\+"
  18484. "\\|[ \t]*" org-keyword-time-regexp
  18485. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  18486. (concat
  18487. "\\|[ \t]*["
  18488. (if org-enable-table-editor "|" "")
  18489. (if org-enable-fixed-width-editor ":" "")
  18490. "]"))))
  18491. ;; We use our own fill-paragraph function, to make sure that tables
  18492. ;; and fixed-width regions are not wrapped. That function will pass
  18493. ;; through to `fill-paragraph' when appropriate.
  18494. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  18495. ;; Prevent auto-fill from inserting unwanted new items.
  18496. (if (boundp 'fill-nobreak-predicate)
  18497. (org-set-local 'fill-nobreak-predicate
  18498. (if (memq 'org-fill-item-nobreak-p fill-nobreak-predicate)
  18499. fill-nobreak-predicate
  18500. (cons 'org-fill-item-nobreak-p fill-nobreak-predicate))))
  18501. ;; Adaptive filling: To get full control, first make sure that
  18502. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  18503. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  18504. (org-set-local 'org-adaptive-fill-regexp-backup
  18505. adaptive-fill-regexp))
  18506. (org-set-local 'adaptive-fill-regexp "\000")
  18507. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  18508. (org-set-local 'adaptive-fill-function
  18509. 'org-adaptive-fill-function)
  18510. (org-set-local
  18511. 'align-mode-rules-list
  18512. '((org-in-buffer-settings
  18513. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  18514. (modes . '(org-mode))))))
  18515. (defun org-fill-item-nobreak-p ()
  18516. "Non-nil when a line break at point would insert a new item."
  18517. (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
  18518. (defun org-fill-paragraph (&optional justify)
  18519. "Re-align a table, pass through to fill-paragraph if no table."
  18520. (let ((table-p (org-at-table-p))
  18521. (table.el-p (org-at-table.el-p))
  18522. (itemp (org-in-item-p)))
  18523. (cond ((and (equal (char-after (point-at-bol)) ?*)
  18524. (save-excursion (goto-char (point-at-bol))
  18525. (looking-at org-outline-regexp)))
  18526. t) ; skip headlines
  18527. (table.el-p t) ; skip table.el tables
  18528. (table-p (org-table-align) t) ; align Org tables
  18529. (itemp ; align text in items
  18530. (let* ((struct (save-excursion (goto-char itemp)
  18531. (org-list-struct)))
  18532. (parents (org-list-parents-alist struct))
  18533. (children (org-list-get-children itemp struct parents))
  18534. beg end prev next prefix)
  18535. ;; Determine in which part of item point is: before
  18536. ;; first child, after last child, between two
  18537. ;; sub-lists, or simply in item if there's no child.
  18538. (cond
  18539. ((not children)
  18540. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18541. beg itemp
  18542. end (org-list-get-item-end itemp struct)))
  18543. ((< (point) (setq next (car children)))
  18544. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18545. beg itemp
  18546. end next))
  18547. ((> (point) (setq prev (car (last children))))
  18548. (setq beg (org-list-get-item-end prev struct)
  18549. end (org-list-get-item-end itemp struct)
  18550. prefix (save-excursion
  18551. (goto-char beg)
  18552. (skip-chars-forward " \t")
  18553. (make-string (current-column) ?\ ))))
  18554. (t (catch 'exit
  18555. (while (setq next (pop children))
  18556. (if (> (point) next)
  18557. (setq prev next)
  18558. (setq beg (org-list-get-item-end prev struct)
  18559. end next
  18560. prefix (save-excursion
  18561. (goto-char beg)
  18562. (skip-chars-forward " \t")
  18563. (make-string (current-column) ?\ )))
  18564. (throw 'exit nil))))))
  18565. ;; Use `fill-paragraph' with buffer narrowed to item
  18566. ;; without any child, and with our computed PREFIX.
  18567. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18568. (save-restriction
  18569. (narrow-to-region beg end)
  18570. (save-excursion (fill-paragraph justify)))) t))
  18571. ;; Special case where point is not in a list but is on
  18572. ;; a paragraph adjacent to a list: make sure this paragraph
  18573. ;; doesn't get merged with the end of the list by narrowing
  18574. ;; buffer first.
  18575. ((save-excursion (forward-paragraph -1)
  18576. (setq itemp (org-in-item-p)))
  18577. (let ((struct (save-excursion (goto-char itemp)
  18578. (org-list-struct))))
  18579. (save-restriction
  18580. (narrow-to-region (org-list-get-bottom-point struct)
  18581. (save-excursion (forward-paragraph 1)
  18582. (point)))
  18583. (fill-paragraph justify) t)))
  18584. ;; Else simply call `fill-paragraph'.
  18585. (t nil))))
  18586. ;; For reference, this is the default value of adaptive-fill-regexp
  18587. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  18588. (defun org-adaptive-fill-function ()
  18589. "Return a fill prefix for org-mode files."
  18590. (let (itemp)
  18591. (save-excursion
  18592. (cond
  18593. ;; Comment line
  18594. ((looking-at "#[ \t]+")
  18595. (match-string-no-properties 0))
  18596. ;; Plain list item
  18597. ((org-at-item-p)
  18598. (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
  18599. ;; Point is in a list after `backward-paragraph': original
  18600. ;; point wasn't in the list, or filling would have been taken
  18601. ;; care of by `org-auto-fill-function', but the list and the
  18602. ;; real paragraph are not separated by a blank line. Thus, move
  18603. ;; point after the list to go back to real paragraph and
  18604. ;; determine fill-prefix.
  18605. ((setq itemp (org-in-item-p))
  18606. (goto-char itemp)
  18607. (let* ((struct (org-list-struct))
  18608. (bottom (org-list-get-bottom-point struct)))
  18609. (goto-char bottom)
  18610. (make-string (org-get-indentation) ?\ )))
  18611. ;; Other text
  18612. ((looking-at org-adaptive-fill-regexp-backup)
  18613. (match-string-no-properties 0))))))
  18614. (defun org-auto-fill-function ()
  18615. "Auto-fill function."
  18616. (let (itemp prefix)
  18617. ;; When in a list, compute an appropriate fill-prefix and make
  18618. ;; sure it will be used by `do-auto-fill'.
  18619. (if (setq itemp (org-in-item-p))
  18620. (progn
  18621. (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
  18622. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18623. (do-auto-fill)))
  18624. ;; Else just use `do-auto-fill'.
  18625. (do-auto-fill))))
  18626. ;;; Other stuff.
  18627. (defun org-toggle-fixed-width-section (arg)
  18628. "Toggle the fixed-width export.
  18629. If there is no active region, the QUOTE keyword at the current headline is
  18630. inserted or removed. When present, it causes the text between this headline
  18631. and the next to be exported as fixed-width text, and unmodified.
  18632. If there is an active region, this command adds or removes a colon as the
  18633. first character of this line. If the first character of a line is a colon,
  18634. this line is also exported in fixed-width font."
  18635. (interactive "P")
  18636. (let* ((cc 0)
  18637. (regionp (org-region-active-p))
  18638. (beg (if regionp (region-beginning) (point)))
  18639. (end (if regionp (region-end)))
  18640. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  18641. (case-fold-search nil)
  18642. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  18643. off)
  18644. (if regionp
  18645. (save-excursion
  18646. (goto-char beg)
  18647. (setq cc (current-column))
  18648. (beginning-of-line 1)
  18649. (setq off (looking-at re))
  18650. (while (> nlines 0)
  18651. (setq nlines (1- nlines))
  18652. (beginning-of-line 1)
  18653. (cond
  18654. (arg
  18655. (org-move-to-column cc t)
  18656. (insert ": \n")
  18657. (forward-line -1))
  18658. ((and off (looking-at re))
  18659. (replace-match "" t t nil 1))
  18660. ((not off) (org-move-to-column cc t) (insert ": ")))
  18661. (forward-line 1)))
  18662. (save-excursion
  18663. (org-back-to-heading)
  18664. (cond
  18665. ((looking-at (format org-heading-keyword-regexp-format
  18666. org-quote-string))
  18667. (goto-char (match-end 1))
  18668. (looking-at (concat " +" org-quote-string))
  18669. (replace-match "" t t)
  18670. (when (eolp) (insert " ")))
  18671. ((looking-at org-outline-regexp)
  18672. (goto-char (match-end 0))
  18673. (insert org-quote-string " ")))))))
  18674. (defun org-reftex-citation ()
  18675. "Use reftex-citation to insert a citation into the buffer.
  18676. This looks for a line like
  18677. #+BIBLIOGRAPHY: foo plain option:-d
  18678. and derives from it that foo.bib is the bibliography file relevant
  18679. for this document. It then installs the necessary environment for RefTeX
  18680. to work in this buffer and calls `reftex-citation' to insert a citation
  18681. into the buffer.
  18682. Export of such citations to both LaTeX and HTML is handled by the contributed
  18683. package org-exp-bibtex by Taru Karttunen."
  18684. (interactive)
  18685. (let ((reftex-docstruct-symbol 'rds)
  18686. (reftex-cite-format "\\cite{%l}")
  18687. rds bib)
  18688. (save-excursion
  18689. (save-restriction
  18690. (widen)
  18691. (let ((case-fold-search t)
  18692. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  18693. (if (not (save-excursion
  18694. (or (re-search-forward re nil t)
  18695. (re-search-backward re nil t))))
  18696. (error "No bibliography defined in file")
  18697. (setq bib (concat (match-string 1) ".bib")
  18698. rds (list (list 'bib bib)))))))
  18699. (call-interactively 'reftex-citation)))
  18700. ;;;; Functions extending outline functionality
  18701. (defun org-beginning-of-line (&optional arg)
  18702. "Go to the beginning of the current line. If that is invisible, continue
  18703. to a visible line beginning. This makes the function of C-a more intuitive.
  18704. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18705. first attempt, and only move to after the tags when the cursor is already
  18706. beyond the end of the headline."
  18707. (interactive "P")
  18708. (let ((pos (point))
  18709. (special (if (consp org-special-ctrl-a/e)
  18710. (car org-special-ctrl-a/e)
  18711. org-special-ctrl-a/e))
  18712. refpos)
  18713. (if (org-bound-and-true-p line-move-visual)
  18714. (beginning-of-visual-line 1)
  18715. (beginning-of-line 1))
  18716. (if (and arg (fboundp 'move-beginning-of-line))
  18717. (call-interactively 'move-beginning-of-line)
  18718. (if (bobp)
  18719. nil
  18720. (backward-char 1)
  18721. (if (org-truely-invisible-p)
  18722. (while (and (not (bobp)) (org-truely-invisible-p))
  18723. (backward-char 1)
  18724. (beginning-of-line 1))
  18725. (forward-char 1))))
  18726. (when special
  18727. (cond
  18728. ((and (looking-at org-complex-heading-regexp)
  18729. (= (char-after (match-end 1)) ?\ ))
  18730. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  18731. (point-at-eol)))
  18732. (goto-char
  18733. (if (eq special t)
  18734. (cond ((> pos refpos) refpos)
  18735. ((= pos (point)) refpos)
  18736. (t (point)))
  18737. (cond ((> pos (point)) (point))
  18738. ((not (eq last-command this-command)) (point))
  18739. (t refpos)))))
  18740. ((org-at-item-p)
  18741. ;; Being at an item and not looking at an the item means point
  18742. ;; was previously moved to beginning of a visual line, whiche
  18743. ;; doesn't contain the item. Therefore, do nothing special,
  18744. ;; just stay here.
  18745. (when (looking-at org-list-full-item-re)
  18746. ;; Set special position at first white space character after
  18747. ;; bullet, and check-box, if any.
  18748. (let ((after-bullet
  18749. (let ((box (match-end 3)))
  18750. (if (not box) (match-end 1)
  18751. (let ((after (char-after box)))
  18752. (if (and after (= after ? )) (1+ box) box))))))
  18753. ;; Special case: Move point to special position when
  18754. ;; currently after it or at beginning of line.
  18755. (if (eq special t)
  18756. (when (or (> pos after-bullet) (= (point) pos))
  18757. (goto-char after-bullet))
  18758. ;; Reversed case: Move point to special position when
  18759. ;; point was already at beginning of line and command is
  18760. ;; repeated.
  18761. (when (and (= (point) pos) (eq last-command this-command))
  18762. (goto-char after-bullet))))))))
  18763. (org-no-warnings
  18764. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18765. (defun org-end-of-line (&optional arg)
  18766. "Go to the end of the line.
  18767. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18768. first attempt, and only move to after the tags when the cursor is already
  18769. beyond the end of the headline."
  18770. (interactive "P")
  18771. (let ((special (if (consp org-special-ctrl-a/e)
  18772. (cdr org-special-ctrl-a/e)
  18773. org-special-ctrl-a/e)))
  18774. (cond
  18775. ((or (not special) arg
  18776. (not (or (org-at-heading-p) (org-at-item-p) (org-at-drawer-p))))
  18777. (call-interactively
  18778. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  18779. ((fboundp 'move-end-of-line) 'move-end-of-line)
  18780. (t 'end-of-line))))
  18781. ((org-at-heading-p)
  18782. (let ((pos (point)))
  18783. (beginning-of-line 1)
  18784. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  18785. (if (eq special t)
  18786. (if (or (< pos (match-beginning 1))
  18787. (= pos (match-end 0)))
  18788. (goto-char (match-beginning 1))
  18789. (goto-char (match-end 0)))
  18790. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  18791. (goto-char (match-end 0))
  18792. (goto-char (match-beginning 1))))
  18793. (call-interactively (if (fboundp 'move-end-of-line)
  18794. 'move-end-of-line
  18795. 'end-of-line)))))
  18796. ((org-at-drawer-p)
  18797. (move-end-of-line 1)
  18798. (when (overlays-at (1- (point))) (backward-char 1)))
  18799. ;; At an item: Move before any hidden text.
  18800. (t (call-interactively
  18801. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  18802. ((fboundp 'move-end-of-line) 'move-end-of-line)
  18803. (t 'end-of-line)))))
  18804. (org-no-warnings
  18805. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18806. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  18807. (define-key org-mode-map "\C-e" 'org-end-of-line)
  18808. (defun org-backward-sentence (&optional arg)
  18809. "Go to beginning of sentence, or beginning of table field.
  18810. This will call `backward-sentence' or `org-table-beginning-of-field',
  18811. depending on context."
  18812. (interactive "P")
  18813. (cond
  18814. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  18815. (t (call-interactively 'backward-sentence))))
  18816. (defun org-forward-sentence (&optional arg)
  18817. "Go to end of sentence, or end of table field.
  18818. This will call `forward-sentence' or `org-table-end-of-field',
  18819. depending on context."
  18820. (interactive "P")
  18821. (cond
  18822. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  18823. (t (call-interactively 'forward-sentence))))
  18824. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  18825. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  18826. (defun org-kill-line (&optional arg)
  18827. "Kill line, to tags or end of line."
  18828. (interactive "P")
  18829. (cond
  18830. ((or (not org-special-ctrl-k)
  18831. (bolp)
  18832. (not (org-at-heading-p)))
  18833. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  18834. org-ctrl-k-protect-subtree)
  18835. (if (or (eq org-ctrl-k-protect-subtree 'error)
  18836. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  18837. (error "C-k aborted - would kill hidden subtree")))
  18838. (call-interactively 'kill-line))
  18839. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  18840. (kill-region (point) (match-beginning 1))
  18841. (org-set-tags nil t))
  18842. (t (kill-region (point) (point-at-eol)))))
  18843. (define-key org-mode-map "\C-k" 'org-kill-line)
  18844. (defun org-yank (&optional arg)
  18845. "Yank. If the kill is a subtree, treat it specially.
  18846. This command will look at the current kill and check if is a single
  18847. subtree, or a series of subtrees[1]. If it passes the test, and if the
  18848. cursor is at the beginning of a line or after the stars of a currently
  18849. empty headline, then the yank is handled specially. How exactly depends
  18850. on the value of the following variables, both set by default.
  18851. org-yank-folded-subtrees
  18852. When set, the subtree(s) will be folded after insertion, but only
  18853. if doing so would now swallow text after the yanked text.
  18854. org-yank-adjusted-subtrees
  18855. When set, the subtree will be promoted or demoted in order to
  18856. fit into the local outline tree structure, which means that the level
  18857. will be adjusted so that it becomes the smaller one of the two
  18858. *visible* surrounding headings.
  18859. Any prefix to this command will cause `yank' to be called directly with
  18860. no special treatment. In particular, a simple \\[universal-argument] prefix \
  18861. will just
  18862. plainly yank the text as it is.
  18863. \[1] The test checks if the first non-white line is a heading
  18864. and if there are no other headings with fewer stars."
  18865. (interactive "P")
  18866. (org-yank-generic 'yank arg))
  18867. (defun org-yank-generic (command arg)
  18868. "Perform some yank-like command.
  18869. This function implements the behavior described in the `org-yank'
  18870. documentation. However, it has been generalized to work for any
  18871. interactive command with similar behavior."
  18872. ;; pretend to be command COMMAND
  18873. (setq this-command command)
  18874. (if arg
  18875. (call-interactively command)
  18876. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  18877. (and (org-kill-is-subtree-p)
  18878. (or (bolp)
  18879. (and (looking-at "[ \t]*$")
  18880. (string-match
  18881. "\\`\\*+\\'"
  18882. (buffer-substring (point-at-bol) (point)))))))
  18883. swallowp)
  18884. (cond
  18885. ((and subtreep org-yank-folded-subtrees)
  18886. (let ((beg (point))
  18887. end)
  18888. (if (and subtreep org-yank-adjusted-subtrees)
  18889. (org-paste-subtree nil nil 'for-yank)
  18890. (call-interactively command))
  18891. (setq end (point))
  18892. (goto-char beg)
  18893. (when (and (bolp) subtreep
  18894. (not (setq swallowp
  18895. (org-yank-folding-would-swallow-text beg end))))
  18896. (org-with-limited-levels
  18897. (or (looking-at org-outline-regexp)
  18898. (re-search-forward org-outline-regexp-bol end t))
  18899. (while (and (< (point) end) (looking-at org-outline-regexp))
  18900. (hide-subtree)
  18901. (org-cycle-show-empty-lines 'folded)
  18902. (condition-case nil
  18903. (outline-forward-same-level 1)
  18904. (error (goto-char end))))))
  18905. (when swallowp
  18906. (message
  18907. "Inserted text not folded because that would swallow text"))
  18908. (goto-char end)
  18909. (skip-chars-forward " \t\n\r")
  18910. (beginning-of-line 1)
  18911. (push-mark beg 'nomsg)))
  18912. ((and subtreep org-yank-adjusted-subtrees)
  18913. (let ((beg (point-at-bol)))
  18914. (org-paste-subtree nil nil 'for-yank)
  18915. (push-mark beg 'nomsg)))
  18916. (t
  18917. (call-interactively command))))))
  18918. (defun org-yank-folding-would-swallow-text (beg end)
  18919. "Would hide-subtree at BEG swallow any text after END?"
  18920. (let (level)
  18921. (org-with-limited-levels
  18922. (save-excursion
  18923. (goto-char beg)
  18924. (when (or (looking-at org-outline-regexp)
  18925. (re-search-forward org-outline-regexp-bol end t))
  18926. (setq level (org-outline-level)))
  18927. (goto-char end)
  18928. (skip-chars-forward " \t\r\n\v\f")
  18929. (if (or (eobp)
  18930. (and (bolp) (looking-at org-outline-regexp)
  18931. (<= (org-outline-level) level)))
  18932. nil ; Nothing would be swallowed
  18933. t))))) ; something would swallow
  18934. (define-key org-mode-map "\C-y" 'org-yank)
  18935. (defun org-truely-invisible-p ()
  18936. "Check if point is at a character currently not visible.
  18937. This version does not only check the character property, but also
  18938. `visible-mode'."
  18939. ;; Early versions of noutline don't have `outline-invisible-p'.
  18940. (if (org-bound-and-true-p visible-mode)
  18941. nil
  18942. (outline-invisible-p)))
  18943. (defun org-invisible-p2 ()
  18944. "Check if point is at a character currently not visible."
  18945. (save-excursion
  18946. (if (and (eolp) (not (bobp))) (backward-char 1))
  18947. ;; Early versions of noutline don't have `outline-invisible-p'.
  18948. (outline-invisible-p)))
  18949. (defun org-back-to-heading (&optional invisible-ok)
  18950. "Call `outline-back-to-heading', but provide a better error message."
  18951. (condition-case nil
  18952. (outline-back-to-heading invisible-ok)
  18953. (error (error "Before first headline at position %d in buffer %s"
  18954. (point) (current-buffer)))))
  18955. (defun org-beginning-of-defun ()
  18956. "Go to the beginning of the subtree, i.e. back to the heading."
  18957. (org-back-to-heading))
  18958. (defun org-end-of-defun ()
  18959. "Go to the end of the subtree."
  18960. (org-end-of-subtree nil t))
  18961. (defun org-before-first-heading-p ()
  18962. "Before first heading?"
  18963. (save-excursion
  18964. (end-of-line)
  18965. (null (re-search-backward org-outline-regexp-bol nil t))))
  18966. (defun org-at-heading-p (&optional ignored)
  18967. (outline-on-heading-p t))
  18968. ;; Compatibility alias with Org versions < 7.8.03
  18969. (defalias 'org-on-heading-p 'org-at-heading-p)
  18970. (defun org-at-drawer-p nil
  18971. "Whether point is at a drawer."
  18972. (save-excursion
  18973. (move-beginning-of-line 1)
  18974. (looking-at org-drawer-regexp)))
  18975. (defun org-point-at-end-of-empty-headline ()
  18976. "If point is at the end of an empty headline, return t, else nil.
  18977. If the heading only contains a TODO keyword, it is still still considered
  18978. empty."
  18979. (and (looking-at "[ \t]*$")
  18980. (save-excursion
  18981. (beginning-of-line 1)
  18982. (let ((case-fold-search nil))
  18983. (looking-at org-todo-line-regexp)))
  18984. (string= (match-string 3) "")))
  18985. (defun org-at-heading-or-item-p ()
  18986. (or (org-at-heading-p) (org-at-item-p)))
  18987. (defun org-at-target-p ()
  18988. (or (org-in-regexp org-radio-target-regexp)
  18989. (org-in-regexp org-target-regexp)))
  18990. ;; Compatibility alias with Org versions < 7.8.03
  18991. (defalias 'org-on-target-p 'org-at-target-p)
  18992. (defun org-up-heading-all (arg)
  18993. "Move to the heading line of which the present line is a subheading.
  18994. This function considers both visible and invisible heading lines.
  18995. With argument, move up ARG levels."
  18996. (if (fboundp 'outline-up-heading-all)
  18997. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  18998. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  18999. (defun org-up-heading-safe ()
  19000. "Move to the heading line of which the present line is a subheading.
  19001. This version will not throw an error. It will return the level of the
  19002. headline found, or nil if no higher level is found.
  19003. Also, this function will be a lot faster than `outline-up-heading',
  19004. because it relies on stars being the outline starters. This can really
  19005. make a significant difference in outlines with very many siblings."
  19006. (let (start-level re)
  19007. (org-back-to-heading t)
  19008. (setq start-level (funcall outline-level))
  19009. (if (equal start-level 1)
  19010. nil
  19011. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  19012. (if (re-search-backward re nil t)
  19013. (funcall outline-level)))))
  19014. (defun org-first-sibling-p ()
  19015. "Is this heading the first child of its parents?"
  19016. (interactive)
  19017. (let ((re org-outline-regexp-bol)
  19018. level l)
  19019. (unless (org-at-heading-p t)
  19020. (error "Not at a heading"))
  19021. (setq level (funcall outline-level))
  19022. (save-excursion
  19023. (if (not (re-search-backward re nil t))
  19024. t
  19025. (setq l (funcall outline-level))
  19026. (< l level)))))
  19027. (defun org-goto-sibling (&optional previous)
  19028. "Goto the next sibling, even if it is invisible.
  19029. When PREVIOUS is set, go to the previous sibling instead. Returns t
  19030. when a sibling was found. When none is found, return nil and don't
  19031. move point."
  19032. (let ((fun (if previous 're-search-backward 're-search-forward))
  19033. (pos (point))
  19034. (re org-outline-regexp-bol)
  19035. level l)
  19036. (when (condition-case nil (org-back-to-heading t) (error nil))
  19037. (setq level (funcall outline-level))
  19038. (catch 'exit
  19039. (or previous (forward-char 1))
  19040. (while (funcall fun re nil t)
  19041. (setq l (funcall outline-level))
  19042. (when (< l level) (goto-char pos) (throw 'exit nil))
  19043. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  19044. (goto-char pos)
  19045. nil))))
  19046. (defun org-show-siblings ()
  19047. "Show all siblings of the current headline."
  19048. (save-excursion
  19049. (while (org-goto-sibling) (org-flag-heading nil)))
  19050. (save-excursion
  19051. (while (org-goto-sibling 'previous)
  19052. (org-flag-heading nil))))
  19053. (defun org-goto-first-child ()
  19054. "Goto the first child, even if it is invisible.
  19055. Return t when a child was found. Otherwise don't move point and
  19056. return nil."
  19057. (let (level (pos (point)) (re org-outline-regexp-bol))
  19058. (when (condition-case nil (org-back-to-heading t) (error nil))
  19059. (setq level (outline-level))
  19060. (forward-char 1)
  19061. (if (and (re-search-forward re nil t) (> (outline-level) level))
  19062. (progn (goto-char (match-beginning 0)) t)
  19063. (goto-char pos) nil))))
  19064. (defun org-show-hidden-entry ()
  19065. "Show an entry where even the heading is hidden."
  19066. (save-excursion
  19067. (org-show-entry)))
  19068. (defun org-flag-heading (flag &optional entry)
  19069. "Flag the current heading. FLAG non-nil means make invisible.
  19070. When ENTRY is non-nil, show the entire entry."
  19071. (save-excursion
  19072. (org-back-to-heading t)
  19073. ;; Check if we should show the entire entry
  19074. (if entry
  19075. (progn
  19076. (org-show-entry)
  19077. (save-excursion
  19078. (and (outline-next-heading)
  19079. (org-flag-heading nil))))
  19080. (outline-flag-region (max (point-min) (1- (point)))
  19081. (save-excursion (outline-end-of-heading) (point))
  19082. flag))))
  19083. (defun org-get-next-sibling ()
  19084. "Move to next heading of the same level, and return point.
  19085. If there is no such heading, return nil.
  19086. This is like outline-next-sibling, but invisible headings are ok."
  19087. (let ((level (funcall outline-level)))
  19088. (outline-next-heading)
  19089. (while (and (not (eobp)) (> (funcall outline-level) level))
  19090. (outline-next-heading))
  19091. (if (or (eobp) (< (funcall outline-level) level))
  19092. nil
  19093. (point))))
  19094. (defun org-get-last-sibling ()
  19095. "Move to previous heading of the same level, and return point.
  19096. If there is no such heading, return nil."
  19097. (let ((opoint (point))
  19098. (level (funcall outline-level)))
  19099. (outline-previous-heading)
  19100. (when (and (/= (point) opoint) (outline-on-heading-p t))
  19101. (while (and (> (funcall outline-level) level)
  19102. (not (bobp)))
  19103. (outline-previous-heading))
  19104. (if (< (funcall outline-level) level)
  19105. nil
  19106. (point)))))
  19107. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  19108. ;; This contains an exact copy of the original function, but it uses
  19109. ;; `org-back-to-heading', to make it work also in invisible
  19110. ;; trees. And is uses an invisible-OK argument.
  19111. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  19112. ;; Furthermore, when used inside Org, finding the end of a large subtree
  19113. ;; with many children and grandchildren etc, this can be much faster
  19114. ;; than the outline version.
  19115. (org-back-to-heading invisible-OK)
  19116. (let ((first t)
  19117. (level (funcall outline-level)))
  19118. (if (and (eq major-mode 'org-mode) (< level 1000))
  19119. ;; A true heading (not a plain list item), in Org-mode
  19120. ;; This means we can easily find the end by looking
  19121. ;; only for the right number of stars. Using a regexp to do
  19122. ;; this is so much faster than using a Lisp loop.
  19123. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  19124. (forward-char 1)
  19125. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  19126. ;; something else, do it the slow way
  19127. (while (and (not (eobp))
  19128. (or first (> (funcall outline-level) level)))
  19129. (setq first nil)
  19130. (outline-next-heading)))
  19131. (unless to-heading
  19132. (if (memq (preceding-char) '(?\n ?\^M))
  19133. (progn
  19134. ;; Go to end of line before heading
  19135. (forward-char -1)
  19136. (if (memq (preceding-char) '(?\n ?\^M))
  19137. ;; leave blank line before heading
  19138. (forward-char -1))))))
  19139. (point))
  19140. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  19141. "Use Org version in org-mode, for dramatic speed-up."
  19142. (if (eq major-mode 'org-mode)
  19143. (progn
  19144. (org-end-of-subtree nil t)
  19145. (unless (eobp) (backward-char 1)))
  19146. ad-do-it))
  19147. (defun org-end-of-meta-data-and-drawers ()
  19148. "Jump to the first text after meta data and drawers in the current entry.
  19149. This will move over empty lines, lines with planning time stamps,
  19150. clocking lines, and drawers."
  19151. (org-back-to-heading t)
  19152. (let ((end (save-excursion (outline-next-heading) (point)))
  19153. (re (concat "\\(" org-drawer-regexp "\\)"
  19154. "\\|" "[ \t]*" org-keyword-time-regexp)))
  19155. (forward-line 1)
  19156. (while (re-search-forward re end t)
  19157. (if (not (match-end 1))
  19158. ;; empty or planning line
  19159. (forward-line 1)
  19160. ;; a drawer, find the end
  19161. (re-search-forward "^[ \t]*:END:" end 'move)
  19162. (forward-line 1)))
  19163. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  19164. (point)))
  19165. (defun org-forward-same-level (arg &optional invisible-ok)
  19166. "Move forward to the arg'th subheading at same level as this one.
  19167. Stop at the first and last subheadings of a superior heading.
  19168. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  19169. it wil also look at invisible ones."
  19170. (interactive "p")
  19171. (org-back-to-heading invisible-ok)
  19172. (org-at-heading-p)
  19173. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19174. (re (format "^\\*\\{1,%d\\} " level))
  19175. l)
  19176. (forward-char 1)
  19177. (while (> arg 0)
  19178. (while (and (re-search-forward re nil 'move)
  19179. (setq l (- (match-end 0) (match-beginning 0) 1))
  19180. (= l level)
  19181. (not invisible-ok)
  19182. (progn (backward-char 1) (outline-invisible-p)))
  19183. (if (< l level) (setq arg 1)))
  19184. (setq arg (1- arg)))
  19185. (beginning-of-line 1)))
  19186. (defun org-backward-same-level (arg &optional invisible-ok)
  19187. "Move backward to the arg'th subheading at same level as this one.
  19188. Stop at the first and last subheadings of a superior heading."
  19189. (interactive "p")
  19190. (org-back-to-heading)
  19191. (org-at-heading-p)
  19192. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19193. (re (format "^\\*\\{1,%d\\} " level))
  19194. l)
  19195. (while (> arg 0)
  19196. (while (and (re-search-backward re nil 'move)
  19197. (setq l (- (match-end 0) (match-beginning 0) 1))
  19198. (= l level)
  19199. (not invisible-ok)
  19200. (outline-invisible-p))
  19201. (if (< l level) (setq arg 1)))
  19202. (setq arg (1- arg)))))
  19203. (defun org-show-subtree ()
  19204. "Show everything after this heading at deeper levels."
  19205. (outline-flag-region
  19206. (point)
  19207. (save-excursion
  19208. (org-end-of-subtree t t))
  19209. nil))
  19210. (defun org-show-entry ()
  19211. "Show the body directly following this heading.
  19212. Show the heading too, if it is currently invisible."
  19213. (interactive)
  19214. (save-excursion
  19215. (condition-case nil
  19216. (progn
  19217. (org-back-to-heading t)
  19218. (outline-flag-region
  19219. (max (point-min) (1- (point)))
  19220. (save-excursion
  19221. (if (re-search-forward
  19222. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  19223. (match-beginning 1)
  19224. (point-max)))
  19225. nil)
  19226. (org-cycle-hide-drawers 'children))
  19227. (error nil))))
  19228. (defun org-make-options-regexp (kwds &optional extra)
  19229. "Make a regular expression for keyword lines."
  19230. (concat
  19231. "^"
  19232. "#?[ \t]*\\+\\("
  19233. (mapconcat 'regexp-quote kwds "\\|")
  19234. (if extra (concat "\\|" extra))
  19235. "\\):[ \t]*"
  19236. "\\(.*\\)"))
  19237. ;; Make isearch reveal the necessary context
  19238. (defun org-isearch-end ()
  19239. "Reveal context after isearch exits."
  19240. (when isearch-success ; only if search was successful
  19241. (if (featurep 'xemacs)
  19242. ;; Under XEmacs, the hook is run in the correct place,
  19243. ;; we directly show the context.
  19244. (org-show-context 'isearch)
  19245. ;; In Emacs the hook runs *before* restoring the overlays.
  19246. ;; So we have to use a one-time post-command-hook to do this.
  19247. ;; (Emacs 22 has a special variable, see function `org-mode')
  19248. (unless (and (boundp 'isearch-mode-end-hook-quit)
  19249. isearch-mode-end-hook-quit)
  19250. ;; Only when the isearch was not quitted.
  19251. (org-add-hook 'post-command-hook 'org-isearch-post-command
  19252. 'append 'local)))))
  19253. (defun org-isearch-post-command ()
  19254. "Remove self from hook, and show context."
  19255. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  19256. (org-show-context 'isearch))
  19257. ;;;; Integration with and fixes for other packages
  19258. ;;; Imenu support
  19259. (defvar org-imenu-markers nil
  19260. "All markers currently used by Imenu.")
  19261. (make-variable-buffer-local 'org-imenu-markers)
  19262. (defun org-imenu-new-marker (&optional pos)
  19263. "Return a new marker for use by Imenu, and remember the marker."
  19264. (let ((m (make-marker)))
  19265. (move-marker m (or pos (point)))
  19266. (push m org-imenu-markers)
  19267. m))
  19268. (defun org-imenu-get-tree ()
  19269. "Produce the index for Imenu."
  19270. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  19271. (setq org-imenu-markers nil)
  19272. (let* ((n org-imenu-depth)
  19273. (re (concat "^" (org-get-limited-outline-regexp)))
  19274. (subs (make-vector (1+ n) nil))
  19275. (last-level 0)
  19276. m level head)
  19277. (save-excursion
  19278. (save-restriction
  19279. (widen)
  19280. (goto-char (point-max))
  19281. (while (re-search-backward re nil t)
  19282. (setq level (org-reduced-level (funcall outline-level)))
  19283. (when (<= level n)
  19284. (looking-at org-complex-heading-regexp)
  19285. (setq head (org-link-display-format
  19286. (org-match-string-no-properties 4))
  19287. m (org-imenu-new-marker))
  19288. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  19289. (if (>= level last-level)
  19290. (push (cons head m) (aref subs level))
  19291. (push (cons head (aref subs (1+ level))) (aref subs level))
  19292. (loop for i from (1+ level) to n do (aset subs i nil)))
  19293. (setq last-level level)))))
  19294. (aref subs 1)))
  19295. (eval-after-load "imenu"
  19296. '(progn
  19297. (add-hook 'imenu-after-jump-hook
  19298. (lambda ()
  19299. (if (eq major-mode 'org-mode)
  19300. (org-show-context 'org-goto))))))
  19301. (defun org-link-display-format (link)
  19302. "Replace a link with either the description, or the link target
  19303. if no description is present"
  19304. (save-match-data
  19305. (if (string-match org-bracket-link-analytic-regexp link)
  19306. (replace-match (if (match-end 5)
  19307. (match-string 5 link)
  19308. (concat (match-string 1 link)
  19309. (match-string 3 link)))
  19310. nil t link)
  19311. link)))
  19312. (defun org-toggle-link-display ()
  19313. "Toggle the literal or descriptive display of links."
  19314. (interactive)
  19315. (if org-descriptive-links
  19316. (progn (org-remove-from-invisibility-spec '(org-link))
  19317. (org-restart-font-lock)
  19318. (setq org-descriptive-links nil))
  19319. (progn (add-to-invisibility-spec '(org-link))
  19320. (org-restart-font-lock)
  19321. (setq org-descriptive-links t))))
  19322. ;; Speedbar support
  19323. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  19324. "Overlay marking the agenda restriction line in speedbar.")
  19325. (overlay-put org-speedbar-restriction-lock-overlay
  19326. 'face 'org-agenda-restriction-lock)
  19327. (overlay-put org-speedbar-restriction-lock-overlay
  19328. 'help-echo "Agendas are currently limited to this item.")
  19329. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  19330. (defun org-speedbar-set-agenda-restriction ()
  19331. "Restrict future agenda commands to the location at point in speedbar.
  19332. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  19333. (interactive)
  19334. (require 'org-agenda)
  19335. (let (p m tp np dir txt)
  19336. (cond
  19337. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19338. 'org-imenu t))
  19339. (setq m (get-text-property p 'org-imenu-marker))
  19340. (with-current-buffer (marker-buffer m)
  19341. (goto-char m)
  19342. (org-agenda-set-restriction-lock 'subtree)))
  19343. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19344. 'speedbar-function 'speedbar-find-file))
  19345. (setq tp (previous-single-property-change
  19346. (1+ p) 'speedbar-function)
  19347. np (next-single-property-change
  19348. tp 'speedbar-function)
  19349. dir (speedbar-line-directory)
  19350. txt (buffer-substring-no-properties (or tp (point-min))
  19351. (or np (point-max))))
  19352. (with-current-buffer (find-file-noselect
  19353. (let ((default-directory dir))
  19354. (expand-file-name txt)))
  19355. (unless (eq major-mode 'org-mode)
  19356. (error "Cannot restrict to non-Org-mode file"))
  19357. (org-agenda-set-restriction-lock 'file)))
  19358. (t (error "Don't know how to restrict Org-mode's agenda")))
  19359. (move-overlay org-speedbar-restriction-lock-overlay
  19360. (point-at-bol) (point-at-eol))
  19361. (setq current-prefix-arg nil)
  19362. (org-agenda-maybe-redo)))
  19363. (eval-after-load "speedbar"
  19364. '(progn
  19365. (speedbar-add-supported-extension ".org")
  19366. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  19367. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  19368. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  19369. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  19370. (add-hook 'speedbar-visiting-tag-hook
  19371. (lambda () (and (eq major-mode 'org-mode) (org-show-context 'org-goto))))))
  19372. ;;; Fixes and Hacks for problems with other packages
  19373. ;; Make flyspell not check words in links, to not mess up our keymap
  19374. (defun org-mode-flyspell-verify ()
  19375. "Don't let flyspell put overlays at active buttons, or on
  19376. {todo,all-time,additional-option-like}-keywords."
  19377. (let ((pos (max (1- (point)) (point-min)))
  19378. (word (thing-at-point 'word)))
  19379. (and (not (get-text-property pos 'keymap))
  19380. (not (get-text-property pos 'org-no-flyspell))
  19381. (not (member word org-todo-keywords-1))
  19382. (not (member word org-all-time-keywords))
  19383. (not (member word org-additional-option-like-keywords)))))
  19384. (defun org-remove-flyspell-overlays-in (beg end)
  19385. "Remove flyspell overlays in region."
  19386. (and (org-bound-and-true-p flyspell-mode)
  19387. (fboundp 'flyspell-delete-region-overlays)
  19388. (flyspell-delete-region-overlays beg end))
  19389. (add-text-properties beg end '(org-no-flyspell t)))
  19390. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  19391. (eval-after-load "bookmark"
  19392. '(if (boundp 'bookmark-after-jump-hook)
  19393. ;; We can use the hook
  19394. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  19395. ;; Hook not available, use advice
  19396. (defadvice bookmark-jump (after org-make-visible activate)
  19397. "Make the position visible."
  19398. (org-bookmark-jump-unhide))))
  19399. ;; Make sure saveplace shows the location if it was hidden
  19400. (eval-after-load "saveplace"
  19401. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  19402. "Make the position visible."
  19403. (org-bookmark-jump-unhide)))
  19404. ;; Make sure ecb shows the location if it was hidden
  19405. (eval-after-load "ecb"
  19406. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  19407. "Make hierarchy visible when jumping into location from ECB tree buffer."
  19408. (if (eq major-mode 'org-mode)
  19409. (org-show-context))))
  19410. (defun org-bookmark-jump-unhide ()
  19411. "Unhide the current position, to show the bookmark location."
  19412. (and (eq major-mode 'org-mode)
  19413. (or (outline-invisible-p)
  19414. (save-excursion (goto-char (max (point-min) (1- (point))))
  19415. (outline-invisible-p)))
  19416. (org-show-context 'bookmark-jump)))
  19417. ;; Make session.el ignore our circular variable
  19418. (eval-after-load "session"
  19419. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  19420. ;;;; Experimental code
  19421. (defun org-closed-in-range ()
  19422. "Sparse tree of items closed in a certain time range.
  19423. Still experimental, may disappear in the future."
  19424. (interactive)
  19425. ;; Get the time interval from the user.
  19426. (let* ((time1 (org-float-time
  19427. (org-read-date nil 'to-time nil "Starting date: ")))
  19428. (time2 (org-float-time
  19429. (org-read-date nil 'to-time nil "End date:")))
  19430. ;; callback function
  19431. (callback (lambda ()
  19432. (let ((time
  19433. (org-float-time
  19434. (apply 'encode-time
  19435. (org-parse-time-string
  19436. (match-string 1))))))
  19437. ;; check if time in interval
  19438. (and (>= time time1) (<= time time2))))))
  19439. ;; make tree, check each match with the callback
  19440. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  19441. ;;;; Finish up
  19442. (provide 'org)
  19443. (run-hooks 'org-load-hook)
  19444. ;;; org.el ends here