org.el 793 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075
  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.02
  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.02"
  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 a string, those commands will be performed on the
  371. matching headlines within the active region. Such string must be
  372. a tags/property/todo match as it is used in the agenda tags view.
  373. The list of commands is:
  374. - `org-schedule'
  375. - `org-deadline'"
  376. :type '(choice (const :tag "Don't loop" nil)
  377. (const :tag "All headlines in active region" t)
  378. (string :tag "Tags/Property/Todo matcher"))
  379. :group 'org-todo
  380. :group 'org-archive)
  381. (defgroup org-startup nil
  382. "Options concerning startup of Org-mode."
  383. :tag "Org Startup"
  384. :group 'org)
  385. (defcustom org-startup-folded t
  386. "Non-nil means entering Org-mode will switch to OVERVIEW.
  387. This can also be configured on a per-file basis by adding one of
  388. the following lines anywhere in the buffer:
  389. #+STARTUP: fold (or `overview', this is equivalent)
  390. #+STARTUP: nofold (or `showall', this is equivalent)
  391. #+STARTUP: content
  392. #+STARTUP: showeverything"
  393. :group 'org-startup
  394. :type '(choice
  395. (const :tag "nofold: show all" nil)
  396. (const :tag "fold: overview" t)
  397. (const :tag "content: all headlines" content)
  398. (const :tag "show everything, even drawers" showeverything)))
  399. (defcustom org-startup-truncated t
  400. "Non-nil means entering Org-mode will set `truncate-lines'.
  401. This is useful since some lines containing links can be very long and
  402. uninteresting. Also tables look terrible when wrapped."
  403. :group 'org-startup
  404. :type 'boolean)
  405. (defcustom org-startup-indented nil
  406. "Non-nil means turn on `org-indent-mode' on startup.
  407. This can also be configured on a per-file basis by adding one of
  408. the following lines anywhere in the buffer:
  409. #+STARTUP: indent
  410. #+STARTUP: noindent"
  411. :group 'org-structure
  412. :type '(choice
  413. (const :tag "Not" nil)
  414. (const :tag "Globally (slow on startup in large files)" t)))
  415. (defcustom org-use-sub-superscripts t
  416. "Non-nil means interpret \"_\" and \"^\" for export.
  417. When this option is turned on, you can use TeX-like syntax for sub- and
  418. superscripts. Several characters after \"_\" or \"^\" will be
  419. considered as a single item - so grouping with {} is normally not
  420. needed. For example, the following things will be parsed as single
  421. sub- or superscripts.
  422. 10^24 or 10^tau several digits will be considered 1 item.
  423. 10^-12 or 10^-tau a leading sign with digits or a word
  424. x^2-y^3 will be read as x^2 - y^3, because items are
  425. terminated by almost any nonword/nondigit char.
  426. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  427. Still, ambiguity is possible - so when in doubt use {} to enclose the
  428. sub/superscript. If you set this variable to the symbol `{}',
  429. the braces are *required* in order to trigger interpretations as
  430. sub/superscript. This can be helpful in documents that need \"_\"
  431. frequently in plain text.
  432. Not all export backends support this, but HTML does.
  433. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  434. :group 'org-startup
  435. :group 'org-export-translation
  436. :type '(choice
  437. (const :tag "Always interpret" t)
  438. (const :tag "Only with braces" {})
  439. (const :tag "Never interpret" nil)))
  440. (if (fboundp 'defvaralias)
  441. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  442. (defcustom org-startup-with-beamer-mode nil
  443. "Non-nil means turn on `org-beamer-mode' on startup.
  444. This can also be configured on a per-file basis by adding one of
  445. the following lines anywhere in the buffer:
  446. #+STARTUP: beamer"
  447. :group 'org-startup
  448. :type 'boolean)
  449. (defcustom org-startup-align-all-tables nil
  450. "Non-nil means align all tables when visiting a file.
  451. This is useful when the column width in tables is forced with <N> cookies
  452. in table fields. Such tables will look correct only after the first re-align.
  453. This can also be configured on a per-file basis by adding one of
  454. the following lines anywhere in the buffer:
  455. #+STARTUP: align
  456. #+STARTUP: noalign"
  457. :group 'org-startup
  458. :type 'boolean)
  459. (defcustom org-startup-with-inline-images nil
  460. "Non-nil means show inline images when loading a new Org file.
  461. This can also be configured on a per-file basis by adding one of
  462. the following lines anywhere in the buffer:
  463. #+STARTUP: inlineimages
  464. #+STARTUP: noinlineimages"
  465. :group 'org-startup
  466. :type 'boolean)
  467. (defcustom org-insert-mode-line-in-empty-file nil
  468. "Non-nil means insert the first line setting Org-mode in empty files.
  469. When the function `org-mode' is called interactively in an empty file, this
  470. normally means that the file name does not automatically trigger Org-mode.
  471. To ensure that the file will always be in Org-mode in the future, a
  472. line enforcing Org-mode will be inserted into the buffer, if this option
  473. has been set."
  474. :group 'org-startup
  475. :type 'boolean)
  476. (defcustom org-replace-disputed-keys nil
  477. "Non-nil means use alternative key bindings for some keys.
  478. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  479. These keys are also used by other packages like shift-selection-mode'
  480. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  481. If you want to use Org-mode together with one of these other modes,
  482. or more generally if you would like to move some Org-mode commands to
  483. other keys, set this variable and configure the keys with the variable
  484. `org-disputed-keys'.
  485. This option is only relevant at load-time of Org-mode, and must be set
  486. *before* org.el is loaded. Changing it requires a restart of Emacs to
  487. become effective."
  488. :group 'org-startup
  489. :type 'boolean)
  490. (defcustom org-use-extra-keys nil
  491. "Non-nil means use extra key sequence definitions for certain commands.
  492. This happens automatically if you run XEmacs or if `window-system'
  493. is nil. This variable lets you do the same manually. You must
  494. set it before loading org.
  495. Example: on Carbon Emacs 22 running graphically, with an external
  496. keyboard on a Powerbook, the default way of setting M-left might
  497. not work for either Alt or ESC. Setting this variable will make
  498. it work for ESC."
  499. :group 'org-startup
  500. :type 'boolean)
  501. (if (fboundp 'defvaralias)
  502. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  503. (defcustom org-disputed-keys
  504. '(([(shift up)] . [(meta p)])
  505. ([(shift down)] . [(meta n)])
  506. ([(shift left)] . [(meta -)])
  507. ([(shift right)] . [(meta +)])
  508. ([(control shift right)] . [(meta shift +)])
  509. ([(control shift left)] . [(meta shift -)]))
  510. "Keys for which Org-mode and other modes compete.
  511. This is an alist, cars are the default keys, second element specifies
  512. the alternative to use when `org-replace-disputed-keys' is t.
  513. Keys can be specified in any syntax supported by `define-key'.
  514. The value of this option takes effect only at Org-mode's startup,
  515. therefore you'll have to restart Emacs to apply it after changing."
  516. :group 'org-startup
  517. :type 'alist)
  518. (defun org-key (key)
  519. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  520. Or return the original if not disputed.
  521. Also apply the translations defined in `org-xemacs-key-equivalents'."
  522. (when org-replace-disputed-keys
  523. (let* ((nkey (key-description key))
  524. (x (org-find-if (lambda (x)
  525. (equal (key-description (car x)) nkey))
  526. org-disputed-keys)))
  527. (setq key (if x (cdr x) key))))
  528. (when (featurep 'xemacs)
  529. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  530. key)
  531. (defun org-find-if (predicate seq)
  532. (catch 'exit
  533. (while seq
  534. (if (funcall predicate (car seq))
  535. (throw 'exit (car seq))
  536. (pop seq)))))
  537. (defun org-defkey (keymap key def)
  538. "Define a key, possibly translated, as returned by `org-key'."
  539. (define-key keymap (org-key key) def))
  540. (defcustom org-ellipsis nil
  541. "The ellipsis to use in the Org-mode outline.
  542. When nil, just use the standard three dots. When a string, use that instead,
  543. When a face, use the standard 3 dots, but with the specified face.
  544. The change affects only Org-mode (which will then use its own display table).
  545. Changing this requires executing `M-x org-mode' in a buffer to become
  546. effective."
  547. :group 'org-startup
  548. :type '(choice (const :tag "Default" nil)
  549. (face :tag "Face" :value org-warning)
  550. (string :tag "String" :value "...#")))
  551. (defvar org-display-table nil
  552. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  553. (defgroup org-keywords nil
  554. "Keywords in Org-mode."
  555. :tag "Org Keywords"
  556. :group 'org)
  557. (defcustom org-deadline-string "DEADLINE:"
  558. "String to mark deadline entries.
  559. A deadline is this string, followed by a time stamp. Should be a word,
  560. terminated by a colon. You can insert a schedule keyword and
  561. a timestamp with \\[org-deadline].
  562. Changes become only effective after restarting Emacs."
  563. :group 'org-keywords
  564. :type 'string)
  565. (defcustom org-scheduled-string "SCHEDULED:"
  566. "String to mark scheduled TODO entries.
  567. A schedule is this string, followed by a time stamp. Should be a word,
  568. terminated by a colon. You can insert a schedule keyword and
  569. a timestamp with \\[org-schedule].
  570. Changes become only effective after restarting Emacs."
  571. :group 'org-keywords
  572. :type 'string)
  573. (defcustom org-closed-string "CLOSED:"
  574. "String used as the prefix for timestamps logging closing a TODO entry."
  575. :group 'org-keywords
  576. :type 'string)
  577. (defcustom org-clock-string "CLOCK:"
  578. "String used as prefix for timestamps clocking work hours on an item."
  579. :group 'org-keywords
  580. :type 'string)
  581. (defcustom org-comment-string "COMMENT"
  582. "Entries starting with this keyword will never be exported.
  583. An entry can be toggled between COMMENT and normal with
  584. \\[org-toggle-comment].
  585. Changes become only effective after restarting Emacs."
  586. :group 'org-keywords
  587. :type 'string)
  588. (defcustom org-quote-string "QUOTE"
  589. "Entries starting with this keyword will be exported in fixed-width font.
  590. Quoting applies only to the text in the entry following the headline, and does
  591. not extend beyond the next headline, even if that is lower level.
  592. An entry can be toggled between QUOTE and normal with
  593. \\[org-toggle-fixed-width-section]."
  594. :group 'org-keywords
  595. :type 'string)
  596. (defconst org-repeat-re
  597. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  598. "Regular expression for specifying repeated events.
  599. After a match, group 1 contains the repeat expression.")
  600. (defgroup org-structure nil
  601. "Options concerning the general structure of Org-mode files."
  602. :tag "Org Structure"
  603. :group 'org)
  604. (defgroup org-reveal-location nil
  605. "Options about how to make context of a location visible."
  606. :tag "Org Reveal Location"
  607. :group 'org-structure)
  608. (defconst org-context-choice
  609. '(choice
  610. (const :tag "Always" t)
  611. (const :tag "Never" nil)
  612. (repeat :greedy t :tag "Individual contexts"
  613. (cons
  614. (choice :tag "Context"
  615. (const agenda)
  616. (const org-goto)
  617. (const occur-tree)
  618. (const tags-tree)
  619. (const link-search)
  620. (const mark-goto)
  621. (const bookmark-jump)
  622. (const isearch)
  623. (const default))
  624. (boolean))))
  625. "Contexts for the reveal options.")
  626. (defcustom org-show-hierarchy-above '((default . t))
  627. "Non-nil means show full hierarchy when revealing a location.
  628. Org-mode often shows locations in an org-mode file which might have
  629. been invisible before. When this is set, the hierarchy of headings
  630. above the exposed location is shown.
  631. Turning this off for example for sparse trees makes them very compact.
  632. Instead of t, this can also be an alist specifying this option for different
  633. contexts. Valid contexts are
  634. agenda when exposing an entry from the agenda
  635. org-goto when using the command `org-goto' on key C-c C-j
  636. occur-tree when using the command `org-occur' on key C-c /
  637. tags-tree when constructing a sparse tree based on tags matches
  638. link-search when exposing search matches associated with a link
  639. mark-goto when exposing the jump goal of a mark
  640. bookmark-jump when exposing a bookmark location
  641. isearch when exiting from an incremental search
  642. default default for all contexts not set explicitly"
  643. :group 'org-reveal-location
  644. :type org-context-choice)
  645. (defcustom org-show-following-heading '((default . nil))
  646. "Non-nil means show following heading when revealing a location.
  647. Org-mode often shows locations in an org-mode file which might have
  648. been invisible before. When this is set, the heading following the
  649. match is shown.
  650. Turning this off for example for sparse trees makes them very compact,
  651. but makes it harder to edit the location of the match. In such a case,
  652. use the command \\[org-reveal] to show more context.
  653. Instead of t, this can also be an alist specifying this option for different
  654. contexts. See `org-show-hierarchy-above' for valid contexts."
  655. :group 'org-reveal-location
  656. :type org-context-choice)
  657. (defcustom org-show-siblings '((default . nil) (isearch t))
  658. "Non-nil means show all sibling heading when revealing a location.
  659. Org-mode often shows locations in an org-mode file which might have
  660. been invisible before. When this is set, the sibling of the current entry
  661. heading are all made visible. If `org-show-hierarchy-above' is t,
  662. the same happens on each level of the hierarchy above the current entry.
  663. By default this is on for the isearch context, off for all other contexts.
  664. Turning this off for example for sparse trees makes them very compact,
  665. but makes it harder to edit the location of the match. In such a case,
  666. use the command \\[org-reveal] to show more context.
  667. Instead of t, this can also be an alist specifying this option for different
  668. contexts. See `org-show-hierarchy-above' for valid contexts."
  669. :group 'org-reveal-location
  670. :type org-context-choice)
  671. (defcustom org-show-entry-below '((default . nil))
  672. "Non-nil means show the entry below a headline when revealing a location.
  673. Org-mode often shows locations in an org-mode file which might have
  674. been invisible before. When this is set, the text below the headline that is
  675. exposed is also shown.
  676. By default this is off for all contexts.
  677. Instead of t, this can also be an alist specifying this option for different
  678. contexts. See `org-show-hierarchy-above' for valid contexts."
  679. :group 'org-reveal-location
  680. :type org-context-choice)
  681. (defcustom org-indirect-buffer-display 'other-window
  682. "How should indirect tree buffers be displayed?
  683. This applies to indirect buffers created with the commands
  684. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  685. Valid values are:
  686. current-window Display in the current window
  687. other-window Just display in another window.
  688. dedicated-frame Create one new frame, and re-use it each time.
  689. new-frame Make a new frame each time. Note that in this case
  690. previously-made indirect buffers are kept, and you need to
  691. kill these buffers yourself."
  692. :group 'org-structure
  693. :group 'org-agenda-windows
  694. :type '(choice
  695. (const :tag "In current window" current-window)
  696. (const :tag "In current frame, other window" other-window)
  697. (const :tag "Each time a new frame" new-frame)
  698. (const :tag "One dedicated frame" dedicated-frame)))
  699. (defcustom org-use-speed-commands nil
  700. "Non-nil means activate single letter commands at beginning of a headline.
  701. This may also be a function to test for appropriate locations where speed
  702. commands should be active."
  703. :group 'org-structure
  704. :type '(choice
  705. (const :tag "Never" nil)
  706. (const :tag "At beginning of headline stars" t)
  707. (function)))
  708. (defcustom org-speed-commands-user nil
  709. "Alist of additional speed commands.
  710. This list will be checked before `org-speed-commands-default'
  711. when the variable `org-use-speed-commands' is non-nil
  712. and when the cursor is at the beginning of a headline.
  713. The car if each entry is a string with a single letter, which must
  714. be assigned to `self-insert-command' in the global map.
  715. The cdr is either a command to be called interactively, a function
  716. to be called, or a form to be evaluated.
  717. An entry that is just a list with a single string will be interpreted
  718. as a descriptive headline that will be added when listing the speed
  719. commands in the Help buffer using the `?' speed command."
  720. :group 'org-structure
  721. :type '(repeat :value ("k" . ignore)
  722. (choice :value ("k" . ignore)
  723. (list :tag "Descriptive Headline" (string :tag "Headline"))
  724. (cons :tag "Letter and Command"
  725. (string :tag "Command letter")
  726. (choice
  727. (function)
  728. (sexp))))))
  729. (defgroup org-cycle nil
  730. "Options concerning visibility cycling in Org-mode."
  731. :tag "Org Cycle"
  732. :group 'org-structure)
  733. (defcustom org-cycle-skip-children-state-if-no-children t
  734. "Non-nil means skip CHILDREN state in entries that don't have any."
  735. :group 'org-cycle
  736. :type 'boolean)
  737. (defcustom org-cycle-max-level nil
  738. "Maximum level which should still be subject to visibility cycling.
  739. Levels higher than this will, for cycling, be treated as text, not a headline.
  740. When `org-odd-levels-only' is set, a value of N in this variable actually
  741. means 2N-1 stars as the limiting headline.
  742. When nil, cycle all levels.
  743. Note that the limiting level of cycling is also influenced by
  744. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  745. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  746. than its value."
  747. :group 'org-cycle
  748. :type '(choice
  749. (const :tag "No limit" nil)
  750. (integer :tag "Maximum level")))
  751. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  752. "Names of drawers. Drawers are not opened by cycling on the headline above.
  753. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  754. this:
  755. :DRAWERNAME:
  756. .....
  757. :END:
  758. The drawer \"PROPERTIES\" is special for capturing properties through
  759. the property API.
  760. Drawers can be defined on the per-file basis with a line like:
  761. #+DRAWERS: HIDDEN STATE PROPERTIES"
  762. :group 'org-structure
  763. :group 'org-cycle
  764. :type '(repeat (string :tag "Drawer Name")))
  765. (defcustom org-hide-block-startup nil
  766. "Non-nil means entering Org-mode will fold all blocks.
  767. This can also be set in on a per-file basis with
  768. #+STARTUP: hideblocks
  769. #+STARTUP: showblocks"
  770. :group 'org-startup
  771. :group 'org-cycle
  772. :type 'boolean)
  773. (defcustom org-cycle-global-at-bob nil
  774. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  775. This makes it possible to do global cycling without having to use S-TAB or
  776. \\[universal-argument] TAB. For this special case to work, the first line \
  777. of the buffer
  778. must not be a headline - it may be empty or some other text. When used in
  779. this way, `org-cycle-hook' is disables temporarily, to make sure the
  780. cursor stays at the beginning of the buffer.
  781. When this option is nil, don't do anything special at the beginning
  782. of the buffer."
  783. :group 'org-cycle
  784. :type 'boolean)
  785. (defcustom org-cycle-level-after-item/entry-creation t
  786. "Non-nil means cycle entry level or item indentation in new empty entries.
  787. When the cursor is at the end of an empty headline, i.e with only stars
  788. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  789. and then all possible ancestor states, before returning to the original state.
  790. This makes data entry extremely fast: M-RET to create a new headline,
  791. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  792. When the cursor is at the end of an empty plain list item, one TAB will
  793. make it a subitem, two or more tabs will back up to make this an item
  794. higher up in the item hierarchy."
  795. :group 'org-cycle
  796. :type 'boolean)
  797. (defcustom org-cycle-emulate-tab t
  798. "Where should `org-cycle' emulate TAB.
  799. nil Never
  800. white Only in completely white lines
  801. whitestart Only at the beginning of lines, before the first non-white char
  802. t Everywhere except in headlines
  803. exc-hl-bol Everywhere except at the start of a headline
  804. If TAB is used in a place where it does not emulate TAB, the current subtree
  805. visibility is cycled."
  806. :group 'org-cycle
  807. :type '(choice (const :tag "Never" nil)
  808. (const :tag "Only in completely white lines" white)
  809. (const :tag "Before first char in a line" whitestart)
  810. (const :tag "Everywhere except in headlines" t)
  811. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  812. ))
  813. (defcustom org-cycle-separator-lines 2
  814. "Number of empty lines needed to keep an empty line between collapsed trees.
  815. If you leave an empty line between the end of a subtree and the following
  816. headline, this empty line is hidden when the subtree is folded.
  817. Org-mode will leave (exactly) one empty line visible if the number of
  818. empty lines is equal or larger to the number given in this variable.
  819. So the default 2 means at least 2 empty lines after the end of a subtree
  820. are needed to produce free space between a collapsed subtree and the
  821. following headline.
  822. If the number is negative, and the number of empty lines is at least -N,
  823. all empty lines are shown.
  824. Special case: when 0, never leave empty lines in collapsed view."
  825. :group 'org-cycle
  826. :type 'integer)
  827. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  828. (defcustom org-pre-cycle-hook nil
  829. "Hook that is run before visibility cycling is happening.
  830. The function(s) in this hook must accept a single argument which indicates
  831. the new state that will be set right after running this hook. The
  832. argument is a symbol. Before a global state change, it can have the values
  833. `overview', `content', or `all'. Before a local state change, it can have
  834. the values `folded', `children', or `subtree'."
  835. :group 'org-cycle
  836. :type 'hook)
  837. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  838. org-cycle-hide-drawers
  839. org-cycle-show-empty-lines
  840. org-optimize-window-after-visibility-change)
  841. "Hook that is run after `org-cycle' has changed the buffer visibility.
  842. The function(s) in this hook must accept a single argument which indicates
  843. the new state that was set by the most recent `org-cycle' command. The
  844. argument is a symbol. After a global state change, it can have the values
  845. `overview', `content', or `all'. After a local state change, it can have
  846. the values `folded', `children', or `subtree'."
  847. :group 'org-cycle
  848. :type 'hook)
  849. (defgroup org-edit-structure nil
  850. "Options concerning structure editing in Org-mode."
  851. :tag "Org Edit Structure"
  852. :group 'org-structure)
  853. (defcustom org-odd-levels-only nil
  854. "Non-nil means skip even levels and only use odd levels for the outline.
  855. This has the effect that two stars are being added/taken away in
  856. promotion/demotion commands. It also influences how levels are
  857. handled by the exporters.
  858. Changing it requires restart of `font-lock-mode' to become effective
  859. for fontification also in regions already fontified.
  860. You may also set this on a per-file basis by adding one of the following
  861. lines to the buffer:
  862. #+STARTUP: odd
  863. #+STARTUP: oddeven"
  864. :group 'org-edit-structure
  865. :group 'org-appearance
  866. :type 'boolean)
  867. (defcustom org-adapt-indentation t
  868. "Non-nil means adapt indentation to outline node level.
  869. When this variable is set, Org assumes that you write outlines by
  870. indenting text in each node to align with the headline (after the stars).
  871. The following issues are influenced by this variable:
  872. - When this is set and the *entire* text in an entry is indented, the
  873. indentation is increased by one space in a demotion command, and
  874. decreased by one in a promotion command. If any line in the entry
  875. body starts with text at column 0, indentation is not changed at all.
  876. - Property drawers and planning information is inserted indented when
  877. this variable s set. When nil, they will not be indented.
  878. - TAB indents a line relative to context. The lines below a headline
  879. will be indented when this variable is set.
  880. Note that this is all about true indentation, by adding and removing
  881. space characters. See also `org-indent.el' which does level-dependent
  882. indentation in a virtual way, i.e. at display time in Emacs."
  883. :group 'org-edit-structure
  884. :type 'boolean)
  885. (defcustom org-special-ctrl-a/e nil
  886. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  887. When t, `C-a' will bring back the cursor to the beginning of the
  888. headline text, i.e. after the stars and after a possible TODO keyword.
  889. In an item, this will be the position after the bullet.
  890. When the cursor is already at that position, another `C-a' will bring
  891. it to the beginning of the line.
  892. `C-e' will jump to the end of the headline, ignoring the presence of tags
  893. in the headline. A second `C-e' will then jump to the true end of the
  894. line, after any tags. This also means that, when this variable is
  895. non-nil, `C-e' also will never jump beyond the end of the heading of a
  896. folded section, i.e. not after the ellipses.
  897. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  898. going to the true line boundary first. Only a directly following, identical
  899. keypress will bring the cursor to the special positions.
  900. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  901. set separately."
  902. :group 'org-edit-structure
  903. :type '(choice
  904. (const :tag "off" nil)
  905. (const :tag "on: after stars/bullet and before tags first" t)
  906. (const :tag "reversed: true line boundary first" reversed)
  907. (cons :tag "Set C-a and C-e separately"
  908. (choice :tag "Special C-a"
  909. (const :tag "off" nil)
  910. (const :tag "on: after stars/bullet first" t)
  911. (const :tag "reversed: before stars/bullet first" reversed))
  912. (choice :tag "Special C-e"
  913. (const :tag "off" nil)
  914. (const :tag "on: before tags first" t)
  915. (const :tag "reversed: after tags first" reversed)))))
  916. (if (fboundp 'defvaralias)
  917. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  918. (defcustom org-special-ctrl-k nil
  919. "Non-nil means `C-k' will behave specially in headlines.
  920. When nil, `C-k' will call the default `kill-line' command.
  921. When t, the following will happen while the cursor is in the headline:
  922. - When the cursor is at the beginning of a headline, kill the entire
  923. line and possible the folded subtree below the line.
  924. - When in the middle of the headline text, kill the headline up to the tags.
  925. - When after the headline text, kill the tags."
  926. :group 'org-edit-structure
  927. :type 'boolean)
  928. (defcustom org-ctrl-k-protect-subtree nil
  929. "Non-nil means, do not delete a hidden subtree with C-k.
  930. When set to the symbol `error', simply throw an error when C-k is
  931. used to kill (part-of) a headline that has hidden text behind it.
  932. Any other non-nil value will result in a query to the user, if it is
  933. OK to kill that hidden subtree. When nil, kill without remorse."
  934. :group 'org-edit-structure
  935. :type '(choice
  936. (const :tag "Do not protect hidden subtrees" nil)
  937. (const :tag "Protect hidden subtrees with a security query" t)
  938. (const :tag "Never kill a hidden subtree with C-k" error)))
  939. (defcustom org-catch-invisible-edits nil
  940. "Check if in invisible region before inserting or deleting a character.
  941. Valid values are:
  942. nil Do not check, so just do invisible edits.
  943. error Throw an error and do nothing.
  944. show Make point visible, and do the requested edit.
  945. show-and-error Make point visible, then throw an error and abort the edit.
  946. smart Make point visible, and do insertion/deletion if it is
  947. adjacent to visible text and the change feels predictable.
  948. Never delete a previously invisible character or add in the
  949. middle or right after an invisible region. Basically, this
  950. allows insertion and backward-delete right before ellipses.
  951. FIXME: maybe in this case we should not even show?"
  952. :group 'org-edit-structure
  953. :type '(choice
  954. (const :tag "Do not check" nil)
  955. (const :tag "Throw error when trying to edit" error)
  956. (const :tag "Unhide, but do not do the edit" show-and-error)
  957. (const :tag "Show invisible part and do the edit" show)
  958. (const :tag "Be smart and do the right thing" smart)))
  959. (defcustom org-yank-folded-subtrees t
  960. "Non-nil means when yanking subtrees, fold them.
  961. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  962. it starts with a heading and all other headings in it are either children
  963. or siblings, then fold all the subtrees. However, do this only if no
  964. text after the yank would be swallowed into a folded tree by this action."
  965. :group 'org-edit-structure
  966. :type 'boolean)
  967. (defcustom org-yank-adjusted-subtrees nil
  968. "Non-nil means when yanking subtrees, adjust the level.
  969. With this setting, `org-paste-subtree' is used to insert the subtree, see
  970. this function for details."
  971. :group 'org-edit-structure
  972. :type 'boolean)
  973. (defcustom org-M-RET-may-split-line '((default . t))
  974. "Non-nil means M-RET will split the line at the cursor position.
  975. When nil, it will go to the end of the line before making a
  976. new line.
  977. You may also set this option in a different way for different
  978. contexts. Valid contexts are:
  979. headline when creating a new headline
  980. item when creating a new item
  981. table in a table field
  982. default the value to be used for all contexts not explicitly
  983. customized"
  984. :group 'org-structure
  985. :group 'org-table
  986. :type '(choice
  987. (const :tag "Always" t)
  988. (const :tag "Never" nil)
  989. (repeat :greedy t :tag "Individual contexts"
  990. (cons
  991. (choice :tag "Context"
  992. (const headline)
  993. (const item)
  994. (const table)
  995. (const default))
  996. (boolean)))))
  997. (defcustom org-insert-heading-respect-content nil
  998. "Non-nil means insert new headings after the current subtree.
  999. When nil, the new heading is created directly after the current line.
  1000. The commands \\[org-insert-heading-respect-content] and
  1001. \\[org-insert-todo-heading-respect-content] turn this variable on
  1002. for the duration of the command."
  1003. :group 'org-structure
  1004. :type 'boolean)
  1005. (defcustom org-blank-before-new-entry '((heading . auto)
  1006. (plain-list-item . auto))
  1007. "Should `org-insert-heading' leave a blank line before new heading/item?
  1008. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1009. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1010. which case Org will look at the surrounding headings/items and try to
  1011. make an intelligent decision whether to insert a blank line or not.
  1012. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1013. set, the setting here is ignored and no empty line is inserted, to avoid
  1014. breaking the list structure."
  1015. :group 'org-edit-structure
  1016. :type '(list
  1017. (cons (const heading)
  1018. (choice (const :tag "Never" nil)
  1019. (const :tag "Always" t)
  1020. (const :tag "Auto" auto)))
  1021. (cons (const plain-list-item)
  1022. (choice (const :tag "Never" nil)
  1023. (const :tag "Always" t)
  1024. (const :tag "Auto" auto)))))
  1025. (defcustom org-insert-heading-hook nil
  1026. "Hook being run after inserting a new heading."
  1027. :group 'org-edit-structure
  1028. :type 'hook)
  1029. (defcustom org-enable-fixed-width-editor t
  1030. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1031. This currently only means they are never auto-wrapped.
  1032. When nil, such lines will be treated like ordinary lines.
  1033. See also the QUOTE keyword."
  1034. :group 'org-edit-structure
  1035. :type 'boolean)
  1036. (defcustom org-goto-auto-isearch t
  1037. "Non-nil means typing characters in `org-goto' starts incremental search."
  1038. :group 'org-edit-structure
  1039. :type 'boolean)
  1040. (defgroup org-sparse-trees nil
  1041. "Options concerning sparse trees in Org-mode."
  1042. :tag "Org Sparse Trees"
  1043. :group 'org-structure)
  1044. (defcustom org-highlight-sparse-tree-matches t
  1045. "Non-nil means highlight all matches that define a sparse tree.
  1046. The highlights will automatically disappear the next time the buffer is
  1047. changed by an edit command."
  1048. :group 'org-sparse-trees
  1049. :type 'boolean)
  1050. (defcustom org-remove-highlights-with-change t
  1051. "Non-nil means any change to the buffer will remove temporary highlights.
  1052. Such highlights are created by `org-occur' and `org-clock-display'.
  1053. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1054. The highlights created by `org-preview-latex-fragment' always need
  1055. `C-c C-c' to be removed."
  1056. :group 'org-sparse-trees
  1057. :group 'org-time
  1058. :type 'boolean)
  1059. (defcustom org-occur-hook '(org-first-headline-recenter)
  1060. "Hook that is run after `org-occur' has constructed a sparse tree.
  1061. This can be used to recenter the window to show as much of the structure
  1062. as possible."
  1063. :group 'org-sparse-trees
  1064. :type 'hook)
  1065. (defgroup org-imenu-and-speedbar nil
  1066. "Options concerning imenu and speedbar in Org-mode."
  1067. :tag "Org Imenu and Speedbar"
  1068. :group 'org-structure)
  1069. (defcustom org-imenu-depth 2
  1070. "The maximum level for Imenu access to Org-mode headlines.
  1071. This also applied for speedbar access."
  1072. :group 'org-imenu-and-speedbar
  1073. :type 'integer)
  1074. (defgroup org-table nil
  1075. "Options concerning tables in Org-mode."
  1076. :tag "Org Table"
  1077. :group 'org)
  1078. (defcustom org-enable-table-editor 'optimized
  1079. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1080. When nil, such lines will be treated like ordinary lines.
  1081. When equal to the symbol `optimized', the table editor will be optimized to
  1082. do the following:
  1083. - Automatic overwrite mode in front of whitespace in table fields.
  1084. This makes the structure of the table stay in tact as long as the edited
  1085. field does not exceed the column width.
  1086. - Minimize the number of realigns. Normally, the table is aligned each time
  1087. TAB or RET are pressed to move to another field. With optimization this
  1088. happens only if changes to a field might have changed the column width.
  1089. Optimization requires replacing the functions `self-insert-command',
  1090. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1091. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1092. very good at guessing when a re-align will be necessary, but you can always
  1093. force one with \\[org-ctrl-c-ctrl-c].
  1094. If you would like to use the optimized version in Org-mode, but the
  1095. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1096. This variable can be used to turn on and off the table editor during a session,
  1097. but in order to toggle optimization, a restart is required.
  1098. See also the variable `org-table-auto-blank-field'."
  1099. :group 'org-table
  1100. :type '(choice
  1101. (const :tag "off" nil)
  1102. (const :tag "on" t)
  1103. (const :tag "on, optimized" optimized)))
  1104. (defcustom org-self-insert-cluster-for-undo t
  1105. "Non-nil means cluster self-insert commands for undo when possible.
  1106. If this is set, then, like in the Emacs command loop, 20 consecutive
  1107. characters will be undone together.
  1108. This is configurable, because there is some impact on typing performance."
  1109. :group 'org-table
  1110. :type 'boolean)
  1111. (defcustom org-table-tab-recognizes-table.el t
  1112. "Non-nil means TAB will automatically notice a table.el table.
  1113. When it sees such a table, it moves point into it and - if necessary -
  1114. calls `table-recognize-table'."
  1115. :group 'org-table-editing
  1116. :type 'boolean)
  1117. (defgroup org-link nil
  1118. "Options concerning links in Org-mode."
  1119. :tag "Org Link"
  1120. :group 'org)
  1121. (defvar org-link-abbrev-alist-local nil
  1122. "Buffer-local version of `org-link-abbrev-alist', which see.
  1123. The value of this is taken from the #+LINK lines.")
  1124. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1125. (defcustom org-link-abbrev-alist nil
  1126. "Alist of link abbreviations.
  1127. The car of each element is a string, to be replaced at the start of a link.
  1128. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1129. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1130. [[linkkey:tag][description]]
  1131. The 'linkkey' must be a word word, starting with a letter, followed
  1132. by letters, numbers, '-' or '_'.
  1133. If REPLACE is a string, the tag will simply be appended to create the link.
  1134. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1135. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1136. be inserted at that point (see the function `url-hexify-string').
  1137. REPLACE may also be a function that will be called with the tag as the
  1138. only argument to create the link, which should be returned as a string.
  1139. See the manual for examples."
  1140. :group 'org-link
  1141. :type '(repeat
  1142. (cons
  1143. (string :tag "Protocol")
  1144. (choice
  1145. (string :tag "Format")
  1146. (function)))))
  1147. (defcustom org-descriptive-links t
  1148. "Non-nil means Org will display descriptive links.
  1149. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1150. \"Org Website\", hiding the link itself and just displaying its
  1151. description. When set to `nil', Org will display the full links
  1152. literally.
  1153. You can interactively set the value of this variable by calling
  1154. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1155. :group 'org-link
  1156. :type 'boolean)
  1157. (defcustom org-link-file-path-type 'adaptive
  1158. "How the path name in file links should be stored.
  1159. Valid values are:
  1160. relative Relative to the current directory, i.e. the directory of the file
  1161. into which the link is being inserted.
  1162. absolute Absolute path, if possible with ~ for home directory.
  1163. noabbrev Absolute path, no abbreviation of home directory.
  1164. adaptive Use relative path for files in the current directory and sub-
  1165. directories of it. For other files, use an absolute path."
  1166. :group 'org-link
  1167. :type '(choice
  1168. (const relative)
  1169. (const absolute)
  1170. (const noabbrev)
  1171. (const adaptive)))
  1172. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1173. "Types of links that should be activated in Org-mode files.
  1174. This is a list of symbols, each leading to the activation of a certain link
  1175. type. In principle, it does not hurt to turn on most link types - there may
  1176. be a small gain when turning off unused link types. The types are:
  1177. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1178. angle Links in angular brackets that may contain whitespace like
  1179. <bbdb:Carsten Dominik>.
  1180. plain Plain links in normal text, no whitespace, like http://google.com.
  1181. radio Text that is matched by a radio target, see manual for details.
  1182. tag Tag settings in a headline (link to tag search).
  1183. date Time stamps (link to calendar).
  1184. footnote Footnote labels.
  1185. Changing this variable requires a restart of Emacs to become effective."
  1186. :group 'org-link
  1187. :type '(set :greedy t
  1188. (const :tag "Double bracket links" bracket)
  1189. (const :tag "Angular bracket links" angle)
  1190. (const :tag "Plain text links" plain)
  1191. (const :tag "Radio target matches" radio)
  1192. (const :tag "Tags" tag)
  1193. (const :tag "Timestamps" date)
  1194. (const :tag "Footnotes" footnote)))
  1195. (defcustom org-make-link-description-function nil
  1196. "Function to use to generate link descriptions from links.
  1197. If nil the link location will be used. This function must take
  1198. two parameters; the first is the link and the second the
  1199. description `org-insert-link' has generated, and should return the
  1200. description to use."
  1201. :group 'org-link
  1202. :type 'function)
  1203. (defgroup org-link-store nil
  1204. "Options concerning storing links in Org-mode."
  1205. :tag "Org Store Link"
  1206. :group 'org-link)
  1207. (defcustom org-email-link-description-format "Email %c: %.30s"
  1208. "Format of the description part of a link to an email or usenet message.
  1209. The following %-escapes will be replaced by corresponding information:
  1210. %F full \"From\" field
  1211. %f name, taken from \"From\" field, address if no name
  1212. %T full \"To\" field
  1213. %t first name in \"To\" field, address if no name
  1214. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1215. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1216. %s subject
  1217. %d date
  1218. %m message-id.
  1219. You may use normal field width specification between the % and the letter.
  1220. This is for example useful to limit the length of the subject.
  1221. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1222. :group 'org-link-store
  1223. :type 'string)
  1224. (defcustom org-from-is-user-regexp
  1225. (let (r1 r2)
  1226. (when (and user-mail-address (not (string= user-mail-address "")))
  1227. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1228. (when (and user-full-name (not (string= user-full-name "")))
  1229. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1230. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1231. "Regexp matched against the \"From:\" header of an email or usenet message.
  1232. It should match if the message is from the user him/herself."
  1233. :group 'org-link-store
  1234. :type 'regexp)
  1235. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1236. "Non-nil means storing a link to an Org file will use entry IDs.
  1237. Note that before this variable is even considered, org-id must be loaded,
  1238. so please customize `org-modules' and turn it on.
  1239. The variable can have the following values:
  1240. t Create an ID if needed to make a link to the current entry.
  1241. create-if-interactive
  1242. If `org-store-link' is called directly (interactively, as a user
  1243. command), do create an ID to support the link. But when doing the
  1244. job for remember, only use the ID if it already exists. The
  1245. purpose of this setting is to avoid proliferation of unwanted
  1246. IDs, just because you happen to be in an Org file when you
  1247. call `org-remember' that automatically and preemptively
  1248. creates a link. If you do want to get an ID link in a remember
  1249. template to an entry not having an ID, create it first by
  1250. explicitly creating a link to it, using `C-c C-l' first.
  1251. create-if-interactive-and-no-custom-id
  1252. Like create-if-interactive, but do not create an ID if there is
  1253. a CUSTOM_ID property defined in the entry. This is the default.
  1254. use-existing
  1255. Use existing ID, do not create one.
  1256. nil Never use an ID to make a link, instead link using a text search for
  1257. the headline text."
  1258. :group 'org-link-store
  1259. :type '(choice
  1260. (const :tag "Create ID to make link" t)
  1261. (const :tag "Create if storing link interactively"
  1262. create-if-interactive)
  1263. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1264. create-if-interactive-and-no-custom-id)
  1265. (const :tag "Only use existing" use-existing)
  1266. (const :tag "Do not use ID to create link" nil)))
  1267. (defcustom org-context-in-file-links t
  1268. "Non-nil means file links from `org-store-link' contain context.
  1269. A search string will be added to the file name with :: as separator and
  1270. used to find the context when the link is activated by the command
  1271. `org-open-at-point'. When this option is t, the entire active region
  1272. will be placed in the search string of the file link. If set to a
  1273. positive integer, only the first n lines of context will be stored.
  1274. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1275. negates this setting for the duration of the command."
  1276. :group 'org-link-store
  1277. :type '(choice boolean integer))
  1278. (defcustom org-keep-stored-link-after-insertion nil
  1279. "Non-nil means keep link in list for entire session.
  1280. The command `org-store-link' adds a link pointing to the current
  1281. location to an internal list. These links accumulate during a session.
  1282. The command `org-insert-link' can be used to insert links into any
  1283. Org-mode file (offering completion for all stored links). When this
  1284. option is nil, every link which has been inserted once using \\[org-insert-link]
  1285. will be removed from the list, to make completing the unused links
  1286. more efficient."
  1287. :group 'org-link-store
  1288. :type 'boolean)
  1289. (defgroup org-link-follow nil
  1290. "Options concerning following links in Org-mode."
  1291. :tag "Org Follow Link"
  1292. :group 'org-link)
  1293. (defcustom org-link-translation-function nil
  1294. "Function to translate links with different syntax to Org syntax.
  1295. This can be used to translate links created for example by the Planner
  1296. or emacs-wiki packages to Org syntax.
  1297. The function must accept two parameters, a TYPE containing the link
  1298. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1299. which is everything after the link protocol. It should return a cons
  1300. with possibly modified values of type and path.
  1301. Org contains a function for this, so if you set this variable to
  1302. `org-translate-link-from-planner', you should be able follow many
  1303. links created by planner."
  1304. :group 'org-link-follow
  1305. :type 'function)
  1306. (defcustom org-follow-link-hook nil
  1307. "Hook that is run after a link has been followed."
  1308. :group 'org-link-follow
  1309. :type 'hook)
  1310. (defcustom org-tab-follows-link nil
  1311. "Non-nil means on links TAB will follow the link.
  1312. Needs to be set before org.el is loaded.
  1313. This really should not be used, it does not make sense, and the
  1314. implementation is bad."
  1315. :group 'org-link-follow
  1316. :type 'boolean)
  1317. (defcustom org-return-follows-link nil
  1318. "Non-nil means on links RET will follow the link."
  1319. :group 'org-link-follow
  1320. :type 'boolean)
  1321. (defcustom org-mouse-1-follows-link
  1322. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1323. "Non-nil means mouse-1 on a link will follow the link.
  1324. A longer mouse click will still set point. Does not work on XEmacs.
  1325. Needs to be set before org.el is loaded."
  1326. :group 'org-link-follow
  1327. :type 'boolean)
  1328. (defcustom org-mark-ring-length 4
  1329. "Number of different positions to be recorded in the ring.
  1330. Changing this requires a restart of Emacs to work correctly."
  1331. :group 'org-link-follow
  1332. :type 'integer)
  1333. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1334. "Non-nil means internal links in Org files must exactly match a headline.
  1335. When nil, the link search tries to match a phrase with all words
  1336. in the search text."
  1337. :group 'org-link-follow
  1338. :type '(choice
  1339. (const :tag "Use fuzzy text search" nil)
  1340. (const :tag "Match only exact headline" t)
  1341. (const :tag "Match exact headline or query to create it"
  1342. query-to-create)))
  1343. (defcustom org-link-frame-setup
  1344. '((vm . vm-visit-folder-other-frame)
  1345. (gnus . org-gnus-no-new-news)
  1346. (file . find-file-other-window)
  1347. (wl . wl-other-frame))
  1348. "Setup the frame configuration for following links.
  1349. When following a link with Emacs, it may often be useful to display
  1350. this link in another window or frame. This variable can be used to
  1351. set this up for the different types of links.
  1352. For VM, use any of
  1353. `vm-visit-folder'
  1354. `vm-visit-folder-other-window'
  1355. `vm-visit-folder-other-frame'
  1356. For Gnus, use any of
  1357. `gnus'
  1358. `gnus-other-frame'
  1359. `org-gnus-no-new-news'
  1360. For FILE, use any of
  1361. `find-file'
  1362. `find-file-other-window'
  1363. `find-file-other-frame'
  1364. For Wanderlust use any of
  1365. `wl'
  1366. `wl-other-frame'
  1367. For the calendar, use the variable `calendar-setup'.
  1368. For BBDB, it is currently only possible to display the matches in
  1369. another window."
  1370. :group 'org-link-follow
  1371. :type '(list
  1372. (cons (const vm)
  1373. (choice
  1374. (const vm-visit-folder)
  1375. (const vm-visit-folder-other-window)
  1376. (const vm-visit-folder-other-frame)))
  1377. (cons (const gnus)
  1378. (choice
  1379. (const gnus)
  1380. (const gnus-other-frame)
  1381. (const org-gnus-no-new-news)))
  1382. (cons (const file)
  1383. (choice
  1384. (const find-file)
  1385. (const find-file-other-window)
  1386. (const find-file-other-frame)))
  1387. (cons (const wl)
  1388. (choice
  1389. (const wl)
  1390. (const wl-other-frame)))))
  1391. (defcustom org-display-internal-link-with-indirect-buffer nil
  1392. "Non-nil means use indirect buffer to display infile links.
  1393. Activating internal links (from one location in a file to another location
  1394. in the same file) normally just jumps to the location. When the link is
  1395. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1396. is displayed in
  1397. another window. When this option is set, the other window actually displays
  1398. an indirect buffer clone of the current buffer, to avoid any visibility
  1399. changes to the current buffer."
  1400. :group 'org-link-follow
  1401. :type 'boolean)
  1402. (defcustom org-open-non-existing-files nil
  1403. "Non-nil means `org-open-file' will open non-existing files.
  1404. When nil, an error will be generated.
  1405. This variable applies only to external applications because they
  1406. might choke on non-existing files. If the link is to a file that
  1407. will be opened in Emacs, the variable is ignored."
  1408. :group 'org-link-follow
  1409. :type 'boolean)
  1410. (defcustom org-open-directory-means-index-dot-org nil
  1411. "Non-nil means a link to a directory really means to index.org.
  1412. When nil, following a directory link will run dired or open a finder/explorer
  1413. window on that directory."
  1414. :group 'org-link-follow
  1415. :type 'boolean)
  1416. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1417. "Function and arguments to call for following mailto links.
  1418. This is a list with the first element being a Lisp function, and the
  1419. remaining elements being arguments to the function. In string arguments,
  1420. %a will be replaced by the address, and %s will be replaced by the subject
  1421. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1422. :group 'org-link-follow
  1423. :type '(choice
  1424. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1425. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1426. (const :tag "message-mail" (message-mail "%a" "%s"))
  1427. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1428. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1429. "Non-nil means ask for confirmation before executing shell links.
  1430. Shell links can be dangerous: just think about a link
  1431. [[shell:rm -rf ~/*][Google Search]]
  1432. This link would show up in your Org-mode document as \"Google Search\",
  1433. but really it would remove your entire home directory.
  1434. Therefore we advise against setting this variable to nil.
  1435. Just change it to `y-or-n-p' if you want to confirm with a
  1436. single keystroke rather than having to type \"yes\"."
  1437. :group 'org-link-follow
  1438. :type '(choice
  1439. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1440. (const :tag "with y-or-n (faster)" y-or-n-p)
  1441. (const :tag "no confirmation (dangerous)" nil)))
  1442. (put 'org-confirm-shell-link-function
  1443. 'safe-local-variable
  1444. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1445. (defcustom org-confirm-shell-link-not-regexp ""
  1446. "A regexp to skip confirmation for shell links."
  1447. :group 'org-link-follow
  1448. :type 'regexp)
  1449. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1450. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1451. Elisp links can be dangerous: just think about a link
  1452. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1453. This link would show up in your Org-mode document as \"Google Search\",
  1454. but really it would remove your entire home directory.
  1455. Therefore we advise against setting this variable to nil.
  1456. Just change it to `y-or-n-p' if you want to confirm with a
  1457. single keystroke rather than having to type \"yes\"."
  1458. :group 'org-link-follow
  1459. :type '(choice
  1460. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1461. (const :tag "with y-or-n (faster)" y-or-n-p)
  1462. (const :tag "no confirmation (dangerous)" nil)))
  1463. (put 'org-confirm-shell-link-function
  1464. 'safe-local-variable
  1465. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1466. (defcustom org-confirm-elisp-link-not-regexp ""
  1467. "A regexp to skip confirmation for Elisp links."
  1468. :group 'org-link-follow
  1469. :type 'regexp)
  1470. (defconst org-file-apps-defaults-gnu
  1471. '((remote . emacs)
  1472. (system . mailcap)
  1473. (t . mailcap))
  1474. "Default file applications on a UNIX or GNU/Linux system.
  1475. See `org-file-apps'.")
  1476. (defconst org-file-apps-defaults-macosx
  1477. '((remote . emacs)
  1478. (t . "open %s")
  1479. (system . "open %s")
  1480. ("ps.gz" . "gv %s")
  1481. ("eps.gz" . "gv %s")
  1482. ("dvi" . "xdvi %s")
  1483. ("fig" . "xfig %s"))
  1484. "Default file applications on a MacOS X system.
  1485. The system \"open\" is known as a default, but we use X11 applications
  1486. for some files for which the OS does not have a good default.
  1487. See `org-file-apps'.")
  1488. (defconst org-file-apps-defaults-windowsnt
  1489. (list
  1490. '(remote . emacs)
  1491. (cons t
  1492. (list (if (featurep 'xemacs)
  1493. 'mswindows-shell-execute
  1494. 'w32-shell-execute)
  1495. "open" 'file))
  1496. (cons 'system
  1497. (list (if (featurep 'xemacs)
  1498. 'mswindows-shell-execute
  1499. 'w32-shell-execute)
  1500. "open" 'file)))
  1501. "Default file applications on a Windows NT system.
  1502. The system \"open\" is used for most files.
  1503. See `org-file-apps'.")
  1504. (defcustom org-file-apps
  1505. '(
  1506. (auto-mode . emacs)
  1507. ("\\.mm\\'" . default)
  1508. ("\\.x?html?\\'" . default)
  1509. ("\\.pdf\\'" . default)
  1510. )
  1511. "External applications for opening `file:path' items in a document.
  1512. Org-mode uses system defaults for different file types, but
  1513. you can use this variable to set the application for a given file
  1514. extension. The entries in this list are cons cells where the car identifies
  1515. files and the cdr the corresponding command. Possible values for the
  1516. file identifier are
  1517. \"string\" A string as a file identifier can be interpreted in different
  1518. ways, depending on its contents:
  1519. - Alphanumeric characters only:
  1520. Match links with this file extension.
  1521. Example: (\"pdf\" . \"evince %s\")
  1522. to open PDFs with evince.
  1523. - Regular expression: Match links where the
  1524. filename matches the regexp. If you want to
  1525. use groups here, use shy groups.
  1526. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1527. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1528. to open *.html and *.xhtml with firefox.
  1529. - Regular expression which contains (non-shy) groups:
  1530. Match links where the whole link, including \"::\", and
  1531. anything after that, matches the regexp.
  1532. In a custom command string, %1, %2, etc. are replaced with
  1533. the parts of the link that were matched by the groups.
  1534. For backwards compatibility, if a command string is given
  1535. that does not use any of the group matches, this case is
  1536. handled identically to the second one (i.e. match against
  1537. file name only).
  1538. In a custom lisp form, you can access the group matches with
  1539. (match-string n link).
  1540. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1541. to open [[file:document.pdf::5]] with evince at page 5.
  1542. `directory' Matches a directory
  1543. `remote' Matches a remote file, accessible through tramp or efs.
  1544. Remote files most likely should be visited through Emacs
  1545. because external applications cannot handle such paths.
  1546. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1547. so all files Emacs knows how to handle. Using this with
  1548. command `emacs' will open most files in Emacs. Beware that this
  1549. will also open html files inside Emacs, unless you add
  1550. (\"html\" . default) to the list as well.
  1551. t Default for files not matched by any of the other options.
  1552. `system' The system command to open files, like `open' on Windows
  1553. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1554. that will be selected if you call `C-c C-o' with a double
  1555. \\[universal-argument] \\[universal-argument] prefix.
  1556. Possible values for the command are:
  1557. `emacs' The file will be visited by the current Emacs process.
  1558. `default' Use the default application for this file type, which is the
  1559. association for t in the list, most likely in the system-specific
  1560. part.
  1561. This can be used to overrule an unwanted setting in the
  1562. system-specific variable.
  1563. `system' Use the system command for opening files, like \"open\".
  1564. This command is specified by the entry whose car is `system'.
  1565. Most likely, the system-specific version of this variable
  1566. does define this command, but you can overrule/replace it
  1567. here.
  1568. string A command to be executed by a shell; %s will be replaced
  1569. by the path to the file.
  1570. sexp A Lisp form which will be evaluated. The file path will
  1571. be available in the Lisp variable `file'.
  1572. For more examples, see the system specific constants
  1573. `org-file-apps-defaults-macosx'
  1574. `org-file-apps-defaults-windowsnt'
  1575. `org-file-apps-defaults-gnu'."
  1576. :group 'org-link-follow
  1577. :type '(repeat
  1578. (cons (choice :value ""
  1579. (string :tag "Extension")
  1580. (const :tag "System command to open files" system)
  1581. (const :tag "Default for unrecognized files" t)
  1582. (const :tag "Remote file" remote)
  1583. (const :tag "Links to a directory" directory)
  1584. (const :tag "Any files that have Emacs modes"
  1585. auto-mode))
  1586. (choice :value ""
  1587. (const :tag "Visit with Emacs" emacs)
  1588. (const :tag "Use default" default)
  1589. (const :tag "Use the system command" system)
  1590. (string :tag "Command")
  1591. (sexp :tag "Lisp form")))))
  1592. (defgroup org-refile nil
  1593. "Options concerning refiling entries in Org-mode."
  1594. :tag "Org Refile"
  1595. :group 'org)
  1596. (defcustom org-directory "~/org"
  1597. "Directory with org files.
  1598. This is just a default location to look for Org files. There is no need
  1599. at all to put your files into this directory. It is only used in the
  1600. following situations:
  1601. 1. When a remember template specifies a target file that is not an
  1602. absolute path. The path will then be interpreted relative to
  1603. `org-directory'
  1604. 2. When a remember note is filed away in an interactive way (when exiting the
  1605. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1606. with `org-directory' as the default path."
  1607. :group 'org-refile
  1608. :group 'org-remember
  1609. :type 'directory)
  1610. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1611. "Default target for storing notes.
  1612. Used as a fall back file for org-remember.el and org-capture.el, for
  1613. templates that do not specify a target file."
  1614. :group 'org-refile
  1615. :group 'org-remember
  1616. :type '(choice
  1617. (const :tag "Default from remember-data-file" nil)
  1618. file))
  1619. (defcustom org-goto-interface 'outline
  1620. "The default interface to be used for `org-goto'.
  1621. Allowed values are:
  1622. outline The interface shows an outline of the relevant file
  1623. and the correct heading is found by moving through
  1624. the outline or by searching with incremental search.
  1625. outline-path-completion Headlines in the current buffer are offered via
  1626. completion. This is the interface also used by
  1627. the refile command."
  1628. :group 'org-refile
  1629. :type '(choice
  1630. (const :tag "Outline" outline)
  1631. (const :tag "Outline-path-completion" outline-path-completion)))
  1632. (defcustom org-goto-max-level 5
  1633. "Maximum target level when running `org-goto' with refile interface."
  1634. :group 'org-refile
  1635. :type 'integer)
  1636. (defcustom org-reverse-note-order nil
  1637. "Non-nil means store new notes at the beginning of a file or entry.
  1638. When nil, new notes will be filed to the end of a file or entry.
  1639. This can also be a list with cons cells of regular expressions that
  1640. are matched against file names, and values."
  1641. :group 'org-remember
  1642. :group 'org-refile
  1643. :type '(choice
  1644. (const :tag "Reverse always" t)
  1645. (const :tag "Reverse never" nil)
  1646. (repeat :tag "By file name regexp"
  1647. (cons regexp boolean))))
  1648. (defcustom org-log-refile nil
  1649. "Information to record when a task is refiled.
  1650. Possible values are:
  1651. nil Don't add anything
  1652. time Add a time stamp to the task
  1653. note Prompt for a note and add it with template `org-log-note-headings'
  1654. This option can also be set with on a per-file-basis with
  1655. #+STARTUP: nologrefile
  1656. #+STARTUP: logrefile
  1657. #+STARTUP: lognoterefile
  1658. You can have local logging settings for a subtree by setting the LOGGING
  1659. property to one or more of these keywords.
  1660. When bulk-refiling from the agenda, the value `note' is forbidden and
  1661. will temporarily be changed to `time'."
  1662. :group 'org-refile
  1663. :group 'org-progress
  1664. :type '(choice
  1665. (const :tag "No logging" nil)
  1666. (const :tag "Record timestamp" time)
  1667. (const :tag "Record timestamp with note." note)))
  1668. (defcustom org-refile-targets nil
  1669. "Targets for refiling entries with \\[org-refile].
  1670. This is a list of cons cells. Each cell contains:
  1671. - a specification of the files to be considered, either a list of files,
  1672. or a symbol whose function or variable value will be used to retrieve
  1673. a file name or a list of file names. If you use `org-agenda-files' for
  1674. that, all agenda files will be scanned for targets. Nil means consider
  1675. headings in the current buffer.
  1676. - A specification of how to find candidate refile targets. This may be
  1677. any of:
  1678. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1679. This tag has to be present in all target headlines, inheritance will
  1680. not be considered.
  1681. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1682. todo keyword.
  1683. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1684. headlines that are refiling targets.
  1685. - a cons cell (:level . N). Any headline of level N is considered a target.
  1686. Note that, when `org-odd-levels-only' is set, level corresponds to
  1687. order in hierarchy, not to the number of stars.
  1688. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1689. Note that, when `org-odd-levels-only' is set, level corresponds to
  1690. order in hierarchy, not to the number of stars.
  1691. Each element of this list generates a set of possible targets.
  1692. The union of these sets is presented (with completion) to
  1693. the user by `org-refile'.
  1694. You can set the variable `org-refile-target-verify-function' to a function
  1695. to verify each headline found by the simple criteria above.
  1696. When this variable is nil, all top-level headlines in the current buffer
  1697. are used, equivalent to the value `((nil . (:level . 1))'."
  1698. :group 'org-refile
  1699. :type '(repeat
  1700. (cons
  1701. (choice :value org-agenda-files
  1702. (const :tag "All agenda files" org-agenda-files)
  1703. (const :tag "Current buffer" nil)
  1704. (function) (variable) (file))
  1705. (choice :tag "Identify target headline by"
  1706. (cons :tag "Specific tag" (const :value :tag) (string))
  1707. (cons :tag "TODO keyword" (const :value :todo) (string))
  1708. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1709. (cons :tag "Level number" (const :value :level) (integer))
  1710. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1711. (defcustom org-refile-target-verify-function nil
  1712. "Function to verify if the headline at point should be a refile target.
  1713. The function will be called without arguments, with point at the
  1714. beginning of the headline. It should return t and leave point
  1715. where it is if the headline is a valid target for refiling.
  1716. If the target should not be selected, the function must return nil.
  1717. In addition to this, it may move point to a place from where the search
  1718. should be continued. For example, the function may decide that the entire
  1719. subtree of the current entry should be excluded and move point to the end
  1720. of the subtree."
  1721. :group 'org-refile
  1722. :type 'function)
  1723. (defcustom org-refile-use-cache nil
  1724. "Non-nil means cache refile targets to speed up the process.
  1725. The cache for a particular file will be updated automatically when
  1726. the buffer has been killed, or when any of the marker used for flagging
  1727. refile targets no longer points at a live buffer.
  1728. If you have added new entries to a buffer that might themselves be targets,
  1729. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1730. find that easier, `C-u C-u C-u C-c C-w'."
  1731. :group 'org-refile
  1732. :type 'boolean)
  1733. (defcustom org-refile-use-outline-path nil
  1734. "Non-nil means provide refile targets as paths.
  1735. So a level 3 headline will be available as level1/level2/level3.
  1736. When the value is `file', also include the file name (without directory)
  1737. into the path. In this case, you can also stop the completion after
  1738. the file name, to get entries inserted as top level in the file.
  1739. When `full-file-path', include the full file path."
  1740. :group 'org-refile
  1741. :type '(choice
  1742. (const :tag "Not" nil)
  1743. (const :tag "Yes" t)
  1744. (const :tag "Start with file name" file)
  1745. (const :tag "Start with full file path" full-file-path)))
  1746. (defcustom org-outline-path-complete-in-steps t
  1747. "Non-nil means complete the outline path in hierarchical steps.
  1748. When Org-mode uses the refile interface to select an outline path
  1749. \(see variable `org-refile-use-outline-path'), the completion of
  1750. the path can be done is a single go, or if can be done in steps down
  1751. the headline hierarchy. Going in steps is probably the best if you
  1752. do not use a special completion package like `ido' or `icicles'.
  1753. However, when using these packages, going in one step can be very
  1754. fast, while still showing the whole path to the entry."
  1755. :group 'org-refile
  1756. :type 'boolean)
  1757. (defcustom org-refile-allow-creating-parent-nodes nil
  1758. "Non-nil means allow to create new nodes as refile targets.
  1759. New nodes are then created by adding \"/new node name\" to the completion
  1760. of an existing node. When the value of this variable is `confirm',
  1761. new node creation must be confirmed by the user (recommended)
  1762. When nil, the completion must match an existing entry.
  1763. Note that, if the new heading is not seen by the criteria
  1764. listed in `org-refile-targets', multiple instances of the same
  1765. heading would be created by trying again to file under the new
  1766. heading."
  1767. :group 'org-refile
  1768. :type '(choice
  1769. (const :tag "Never" nil)
  1770. (const :tag "Always" t)
  1771. (const :tag "Prompt for confirmation" confirm)))
  1772. (defcustom org-refile-active-region-within-subtree nil
  1773. "Non-nil means also refile active region within a subtree.
  1774. By default `org-refile' doesn't allow refiling regions if they
  1775. don't contain a set of subtrees, but it might be convenient to
  1776. do so sometimes: in that case, the first line of the region is
  1777. converted to a headline before refiling."
  1778. :group 'org-refile
  1779. :type 'boolean)
  1780. (defgroup org-todo nil
  1781. "Options concerning TODO items in Org-mode."
  1782. :tag "Org TODO"
  1783. :group 'org)
  1784. (defgroup org-progress nil
  1785. "Options concerning Progress logging in Org-mode."
  1786. :tag "Org Progress"
  1787. :group 'org-time)
  1788. (defvar org-todo-interpretation-widgets
  1789. '((:tag "Sequence (cycling hits every state)" sequence)
  1790. (:tag "Type (cycling directly to DONE)" type))
  1791. "The available interpretation symbols for customizing `org-todo-keywords'.
  1792. Interested libraries should add to this list.")
  1793. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1794. "List of TODO entry keyword sequences and their interpretation.
  1795. \\<org-mode-map>This is a list of sequences.
  1796. Each sequence starts with a symbol, either `sequence' or `type',
  1797. indicating if the keywords should be interpreted as a sequence of
  1798. action steps, or as different types of TODO items. The first
  1799. keywords are states requiring action - these states will select a headline
  1800. for inclusion into the global TODO list Org-mode produces. If one of
  1801. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1802. signify that no further action is necessary. If \"|\" is not found,
  1803. the last keyword is treated as the only DONE state of the sequence.
  1804. The command \\[org-todo] cycles an entry through these states, and one
  1805. additional state where no keyword is present. For details about this
  1806. cycling, see the manual.
  1807. TODO keywords and interpretation can also be set on a per-file basis with
  1808. the special #+SEQ_TODO and #+TYP_TODO lines.
  1809. Each keyword can optionally specify a character for fast state selection
  1810. \(in combination with the variable `org-use-fast-todo-selection')
  1811. and specifiers for state change logging, using the same syntax
  1812. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1813. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1814. indicates to record a time stamp each time this state is selected.
  1815. Each keyword may also specify if a timestamp or a note should be
  1816. recorded when entering or leaving the state, by adding additional
  1817. characters in the parenthesis after the keyword. This looks like this:
  1818. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1819. record only the time of the state change. With X and Y being either
  1820. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1821. Y when leaving the state if and only if the *target* state does not
  1822. define X. You may omit any of the fast-selection key or X or /Y,
  1823. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1824. For backward compatibility, this variable may also be just a list
  1825. of keywords - in this case the interpretation (sequence or type) will be
  1826. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1827. :group 'org-todo
  1828. :group 'org-keywords
  1829. :type '(choice
  1830. (repeat :tag "Old syntax, just keywords"
  1831. (string :tag "Keyword"))
  1832. (repeat :tag "New syntax"
  1833. (cons
  1834. (choice
  1835. :tag "Interpretation"
  1836. ;;Quick and dirty way to see
  1837. ;;`org-todo-interpretations'. This takes the
  1838. ;;place of item arguments
  1839. :convert-widget
  1840. (lambda (widget)
  1841. (widget-put widget
  1842. :args (mapcar
  1843. #'(lambda (x)
  1844. (widget-convert
  1845. (cons 'const x)))
  1846. org-todo-interpretation-widgets))
  1847. widget))
  1848. (repeat
  1849. (string :tag "Keyword"))))))
  1850. (defvar org-todo-keywords-1 nil
  1851. "All TODO and DONE keywords active in a buffer.")
  1852. (make-variable-buffer-local 'org-todo-keywords-1)
  1853. (defvar org-todo-keywords-for-agenda nil)
  1854. (defvar org-done-keywords-for-agenda nil)
  1855. (defvar org-drawers-for-agenda nil)
  1856. (defvar org-todo-keyword-alist-for-agenda nil)
  1857. (defvar org-tag-alist-for-agenda nil)
  1858. (defvar org-agenda-contributing-files nil)
  1859. (defvar org-not-done-keywords nil)
  1860. (make-variable-buffer-local 'org-not-done-keywords)
  1861. (defvar org-done-keywords nil)
  1862. (make-variable-buffer-local 'org-done-keywords)
  1863. (defvar org-todo-heads nil)
  1864. (make-variable-buffer-local 'org-todo-heads)
  1865. (defvar org-todo-sets nil)
  1866. (make-variable-buffer-local 'org-todo-sets)
  1867. (defvar org-todo-log-states nil)
  1868. (make-variable-buffer-local 'org-todo-log-states)
  1869. (defvar org-todo-kwd-alist nil)
  1870. (make-variable-buffer-local 'org-todo-kwd-alist)
  1871. (defvar org-todo-key-alist nil)
  1872. (make-variable-buffer-local 'org-todo-key-alist)
  1873. (defvar org-todo-key-trigger nil)
  1874. (make-variable-buffer-local 'org-todo-key-trigger)
  1875. (defcustom org-todo-interpretation 'sequence
  1876. "Controls how TODO keywords are interpreted.
  1877. This variable is in principle obsolete and is only used for
  1878. backward compatibility, if the interpretation of todo keywords is
  1879. not given already in `org-todo-keywords'. See that variable for
  1880. more information."
  1881. :group 'org-todo
  1882. :group 'org-keywords
  1883. :type '(choice (const sequence)
  1884. (const type)))
  1885. (defcustom org-use-fast-todo-selection t
  1886. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1887. This variable describes if and under what circumstances the cycling
  1888. mechanism for TODO keywords will be replaced by a single-key, direct
  1889. selection scheme.
  1890. When nil, fast selection is never used.
  1891. When the symbol `prefix', it will be used when `org-todo' is called with
  1892. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1893. in an agenda buffer.
  1894. When t, fast selection is used by default. In this case, the prefix
  1895. argument forces cycling instead.
  1896. In all cases, the special interface is only used if access keys have actually
  1897. been assigned by the user, i.e. if keywords in the configuration are followed
  1898. by a letter in parenthesis, like TODO(t)."
  1899. :group 'org-todo
  1900. :type '(choice
  1901. (const :tag "Never" nil)
  1902. (const :tag "By default" t)
  1903. (const :tag "Only with C-u C-c C-t" prefix)))
  1904. (defcustom org-provide-todo-statistics t
  1905. "Non-nil means update todo statistics after insert and toggle.
  1906. ALL-HEADLINES means update todo statistics by including headlines
  1907. with no TODO keyword as well, counting them as not done.
  1908. A list of TODO keywords means the same, but skip keywords that are
  1909. not in this list.
  1910. When this is set, todo statistics is updated in the parent of the
  1911. current entry each time a todo state is changed."
  1912. :group 'org-todo
  1913. :type '(choice
  1914. (const :tag "Yes, only for TODO entries" t)
  1915. (const :tag "Yes, including all entries" 'all-headlines)
  1916. (repeat :tag "Yes, for TODOs in this list"
  1917. (string :tag "TODO keyword"))
  1918. (other :tag "No TODO statistics" nil)))
  1919. (defcustom org-hierarchical-todo-statistics t
  1920. "Non-nil means TODO statistics covers just direct children.
  1921. When nil, all entries in the subtree are considered.
  1922. This has only an effect if `org-provide-todo-statistics' is set.
  1923. To set this to nil for only a single subtree, use a COOKIE_DATA
  1924. property and include the word \"recursive\" into the value."
  1925. :group 'org-todo
  1926. :type 'boolean)
  1927. (defcustom org-after-todo-state-change-hook nil
  1928. "Hook which is run after the state of a TODO item was changed.
  1929. The new state (a string with a TODO keyword, or nil) is available in the
  1930. Lisp variable `state'."
  1931. :group 'org-todo
  1932. :type 'hook)
  1933. (defvar org-blocker-hook nil
  1934. "Hook for functions that are allowed to block a state change.
  1935. Each function gets as its single argument a property list, see
  1936. `org-trigger-hook' for more information about this list.
  1937. If any of the functions in this hook returns nil, the state change
  1938. is blocked.")
  1939. (defvar org-trigger-hook nil
  1940. "Hook for functions that are triggered by a state change.
  1941. Each function gets as its single argument a property list with at least
  1942. the following elements:
  1943. (:type type-of-change :position pos-at-entry-start
  1944. :from old-state :to new-state)
  1945. Depending on the type, more properties may be present.
  1946. This mechanism is currently implemented for:
  1947. TODO state changes
  1948. ------------------
  1949. :type todo-state-change
  1950. :from previous state (keyword as a string), or nil, or a symbol
  1951. 'todo' or 'done', to indicate the general type of state.
  1952. :to new state, like in :from")
  1953. (defcustom org-enforce-todo-dependencies nil
  1954. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1955. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1956. be blocked if any prior sibling is not yet done.
  1957. Finally, if the parent is blocked because of ordered siblings of its own,
  1958. the child will also be blocked.
  1959. This variable needs to be set before org.el is loaded, and you need to
  1960. restart Emacs after a change to make the change effective. The only way
  1961. to change is while Emacs is running is through the customize interface."
  1962. :set (lambda (var val)
  1963. (set var val)
  1964. (if val
  1965. (add-hook 'org-blocker-hook
  1966. 'org-block-todo-from-children-or-siblings-or-parent)
  1967. (remove-hook 'org-blocker-hook
  1968. 'org-block-todo-from-children-or-siblings-or-parent)))
  1969. :group 'org-todo
  1970. :type 'boolean)
  1971. (defcustom org-enforce-todo-checkbox-dependencies nil
  1972. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1973. When this is nil, checkboxes have no influence on switching TODO states.
  1974. When non-nil, you first need to check off all check boxes before the TODO
  1975. entry can be switched to DONE.
  1976. This variable needs to be set before org.el is loaded, and you need to
  1977. restart Emacs after a change to make the change effective. The only way
  1978. to change is while Emacs is running is through the customize interface."
  1979. :set (lambda (var val)
  1980. (set var val)
  1981. (if val
  1982. (add-hook 'org-blocker-hook
  1983. 'org-block-todo-from-checkboxes)
  1984. (remove-hook 'org-blocker-hook
  1985. 'org-block-todo-from-checkboxes)))
  1986. :group 'org-todo
  1987. :type 'boolean)
  1988. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1989. "Non-nil means inserting a TODO heading is treated as state change.
  1990. So when the command \\[org-insert-todo-heading] is used, state change
  1991. logging will apply if appropriate. When nil, the new TODO item will
  1992. be inserted directly, and no logging will take place."
  1993. :group 'org-todo
  1994. :type 'boolean)
  1995. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1996. "Non-nil means switching TODO states with S-cursor counts as state change.
  1997. This is the default behavior. However, setting this to nil allows a
  1998. convenient way to select a TODO state and bypass any logging associated
  1999. with that."
  2000. :group 'org-todo
  2001. :type 'boolean)
  2002. (defcustom org-todo-state-tags-triggers nil
  2003. "Tag changes that should be triggered by TODO state changes.
  2004. This is a list. Each entry is
  2005. (state-change (tag . flag) .......)
  2006. State-change can be a string with a state, and empty string to indicate the
  2007. state that has no TODO keyword, or it can be one of the symbols `todo'
  2008. or `done', meaning any not-done or done state, respectively."
  2009. :group 'org-todo
  2010. :group 'org-tags
  2011. :type '(repeat
  2012. (cons (choice :tag "When changing to"
  2013. (const :tag "Not-done state" todo)
  2014. (const :tag "Done state" done)
  2015. (string :tag "State"))
  2016. (repeat
  2017. (cons :tag "Tag action"
  2018. (string :tag "Tag")
  2019. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2020. (defcustom org-log-done nil
  2021. "Information to record when a task moves to the DONE state.
  2022. Possible values are:
  2023. nil Don't add anything, just change the keyword
  2024. time Add a time stamp to the task
  2025. note Prompt for a note and add it with template `org-log-note-headings'
  2026. This option can also be set with on a per-file-basis with
  2027. #+STARTUP: nologdone
  2028. #+STARTUP: logdone
  2029. #+STARTUP: lognotedone
  2030. You can have local logging settings for a subtree by setting the LOGGING
  2031. property to one or more of these keywords."
  2032. :group 'org-todo
  2033. :group 'org-progress
  2034. :type '(choice
  2035. (const :tag "No logging" nil)
  2036. (const :tag "Record CLOSED timestamp" time)
  2037. (const :tag "Record CLOSED timestamp with note." note)))
  2038. ;; Normalize old uses of org-log-done.
  2039. (cond
  2040. ((eq org-log-done t) (setq org-log-done 'time))
  2041. ((and (listp org-log-done) (memq 'done org-log-done))
  2042. (setq org-log-done 'note)))
  2043. (defcustom org-log-reschedule nil
  2044. "Information to record when the scheduling date of a tasks is modified.
  2045. Possible values are:
  2046. nil Don't add anything, just change the date
  2047. time Add a time stamp to the task
  2048. note Prompt for a note and add it with template `org-log-note-headings'
  2049. This option can also be set with on a per-file-basis with
  2050. #+STARTUP: nologreschedule
  2051. #+STARTUP: logreschedule
  2052. #+STARTUP: lognotereschedule"
  2053. :group 'org-todo
  2054. :group 'org-progress
  2055. :type '(choice
  2056. (const :tag "No logging" nil)
  2057. (const :tag "Record timestamp" time)
  2058. (const :tag "Record timestamp with note." note)))
  2059. (defcustom org-log-redeadline nil
  2060. "Information to record when the deadline date of a tasks is modified.
  2061. Possible values are:
  2062. nil Don't add anything, just change the date
  2063. time Add a time stamp to the task
  2064. note Prompt for a note and add it with template `org-log-note-headings'
  2065. This option can also be set with on a per-file-basis with
  2066. #+STARTUP: nologredeadline
  2067. #+STARTUP: logredeadline
  2068. #+STARTUP: lognoteredeadline
  2069. You can have local logging settings for a subtree by setting the LOGGING
  2070. property to one or more of these keywords."
  2071. :group 'org-todo
  2072. :group 'org-progress
  2073. :type '(choice
  2074. (const :tag "No logging" nil)
  2075. (const :tag "Record timestamp" time)
  2076. (const :tag "Record timestamp with note." note)))
  2077. (defcustom org-log-note-clock-out nil
  2078. "Non-nil means record a note when clocking out of an item.
  2079. This can also be configured on a per-file basis by adding one of
  2080. the following lines anywhere in the buffer:
  2081. #+STARTUP: lognoteclock-out
  2082. #+STARTUP: nolognoteclock-out"
  2083. :group 'org-todo
  2084. :group 'org-progress
  2085. :type 'boolean)
  2086. (defcustom org-log-done-with-time t
  2087. "Non-nil means the CLOSED time stamp will contain date and time.
  2088. When nil, only the date will be recorded."
  2089. :group 'org-progress
  2090. :type 'boolean)
  2091. (defcustom org-log-note-headings
  2092. '((done . "CLOSING NOTE %t")
  2093. (state . "State %-12s from %-12S %t")
  2094. (note . "Note taken on %t")
  2095. (reschedule . "Rescheduled from %S on %t")
  2096. (delschedule . "Not scheduled, was %S on %t")
  2097. (redeadline . "New deadline from %S on %t")
  2098. (deldeadline . "Removed deadline, was %S on %t")
  2099. (refile . "Refiled on %t")
  2100. (clock-out . ""))
  2101. "Headings for notes added to entries.
  2102. The value is an alist, with the car being a symbol indicating the note
  2103. context, and the cdr is the heading to be used. The heading may also be the
  2104. empty string.
  2105. %t in the heading will be replaced by a time stamp.
  2106. %T will be an active time stamp instead the default inactive one
  2107. %d will be replaced by a short-format time stamp.
  2108. %D will be replaced by an active short-format time stamp.
  2109. %s will be replaced by the new TODO state, in double quotes.
  2110. %S will be replaced by the old TODO state, in double quotes.
  2111. %u will be replaced by the user name.
  2112. %U will be replaced by the full user name.
  2113. In fact, it is not a good idea to change the `state' entry, because
  2114. agenda log mode depends on the format of these entries."
  2115. :group 'org-todo
  2116. :group 'org-progress
  2117. :type '(list :greedy t
  2118. (cons (const :tag "Heading when closing an item" done) string)
  2119. (cons (const :tag
  2120. "Heading when changing todo state (todo sequence only)"
  2121. state) string)
  2122. (cons (const :tag "Heading when just taking a note" note) string)
  2123. (cons (const :tag "Heading when clocking out" clock-out) string)
  2124. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2125. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2126. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2127. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2128. (cons (const :tag "Heading when refiling" refile) string)))
  2129. (unless (assq 'note org-log-note-headings)
  2130. (push '(note . "%t") org-log-note-headings))
  2131. (defcustom org-log-into-drawer nil
  2132. "Non-nil means insert state change notes and time stamps into a drawer.
  2133. When nil, state changes notes will be inserted after the headline and
  2134. any scheduling and clock lines, but not inside a drawer.
  2135. The value of this variable should be the name of the drawer to use.
  2136. LOGBOOK is proposed as the default drawer for this purpose, you can
  2137. also set this to a string to define the drawer of your choice.
  2138. A value of t is also allowed, representing \"LOGBOOK\".
  2139. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2140. will be ignored.
  2141. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2142. a subtree."
  2143. :group 'org-todo
  2144. :group 'org-progress
  2145. :type '(choice
  2146. (const :tag "Not into a drawer" nil)
  2147. (const :tag "LOGBOOK" t)
  2148. (string :tag "Other")))
  2149. (if (fboundp 'defvaralias)
  2150. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2151. (defun org-log-into-drawer ()
  2152. "Return the value of `org-log-into-drawer', but let properties overrule.
  2153. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2154. used instead of the default value."
  2155. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2156. (cond
  2157. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2158. ((equal p "t") "LOGBOOK")
  2159. (t p))))
  2160. (defcustom org-log-state-notes-insert-after-drawers nil
  2161. "Non-nil means insert state change notes after any drawers in entry.
  2162. Only the drawers that *immediately* follow the headline and the
  2163. deadline/scheduled line are skipped.
  2164. When nil, insert notes right after the heading and perhaps the line
  2165. with deadline/scheduling if present.
  2166. This variable will have no effect if `org-log-into-drawer' is
  2167. set."
  2168. :group 'org-todo
  2169. :group 'org-progress
  2170. :type 'boolean)
  2171. (defcustom org-log-states-order-reversed t
  2172. "Non-nil means the latest state note will be directly after heading.
  2173. When nil, the state change notes will be ordered according to time."
  2174. :group 'org-todo
  2175. :group 'org-progress
  2176. :type 'boolean)
  2177. (defcustom org-todo-repeat-to-state nil
  2178. "The TODO state to which a repeater should return the repeating task.
  2179. By default this is the first task in a TODO sequence, or the previous state
  2180. in a TODO_TYP set. But you can specify another task here.
  2181. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2182. :group 'org-todo
  2183. :type '(choice (const :tag "Head of sequence" nil)
  2184. (string :tag "Specific state")))
  2185. (defcustom org-log-repeat 'time
  2186. "Non-nil means record moving through the DONE state when triggering repeat.
  2187. An auto-repeating task is immediately switched back to TODO when
  2188. marked DONE. If you are not logging state changes (by adding \"@\"
  2189. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2190. record a closing note, there will be no record of the task moving
  2191. through DONE. This variable forces taking a note anyway.
  2192. nil Don't force a record
  2193. time Record a time stamp
  2194. note Record a note
  2195. This option can also be set with on a per-file-basis with
  2196. #+STARTUP: logrepeat
  2197. #+STARTUP: lognoterepeat
  2198. #+STARTUP: nologrepeat
  2199. You can have local logging settings for a subtree by setting the LOGGING
  2200. property to one or more of these keywords."
  2201. :group 'org-todo
  2202. :group 'org-progress
  2203. :type '(choice
  2204. (const :tag "Don't force a record" nil)
  2205. (const :tag "Force recording the DONE state" time)
  2206. (const :tag "Force recording a note with the DONE state" note)))
  2207. (defgroup org-priorities nil
  2208. "Priorities in Org-mode."
  2209. :tag "Org Priorities"
  2210. :group 'org-todo)
  2211. (defcustom org-enable-priority-commands t
  2212. "Non-nil means priority commands are active.
  2213. When nil, these commands will be disabled, so that you never accidentally
  2214. set a priority."
  2215. :group 'org-priorities
  2216. :type 'boolean)
  2217. (defcustom org-highest-priority ?A
  2218. "The highest priority of TODO items. A character like ?A, ?B etc.
  2219. Must have a smaller ASCII number than `org-lowest-priority'."
  2220. :group 'org-priorities
  2221. :type 'character)
  2222. (defcustom org-lowest-priority ?C
  2223. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2224. Must have a larger ASCII number than `org-highest-priority'."
  2225. :group 'org-priorities
  2226. :type 'character)
  2227. (defcustom org-default-priority ?B
  2228. "The default priority of TODO items.
  2229. This is the priority an item gets if no explicit priority is given.
  2230. When starting to cycle on an empty priority the first step in the cycle
  2231. depends on `org-priority-start-cycle-with-default'. The resulting first
  2232. step priority must not exceed the range from `org-highest-priority' to
  2233. `org-lowest-priority' which means that `org-default-priority' has to be
  2234. in this range exclusive or inclusive the range boundaries. Else the
  2235. first step refuses to set the default and the second will fall back
  2236. to (depending on the command used) the highest or lowest priority."
  2237. :group 'org-priorities
  2238. :type 'character)
  2239. (defcustom org-priority-start-cycle-with-default t
  2240. "Non-nil means start with default priority when starting to cycle.
  2241. When this is nil, the first step in the cycle will be (depending on the
  2242. command used) one higher or lower than the default priority.
  2243. See also `org-default-priority'."
  2244. :group 'org-priorities
  2245. :type 'boolean)
  2246. (defcustom org-get-priority-function nil
  2247. "Function to extract the priority from a string.
  2248. The string is normally the headline. If this is nil Org computes the
  2249. priority from the priority cookie like [#A] in the headline. It returns
  2250. an integer, increasing by 1000 for each priority level.
  2251. The user can set a different function here, which should take a string
  2252. as an argument and return the numeric priority."
  2253. :group 'org-priorities
  2254. :type 'function)
  2255. (defgroup org-time nil
  2256. "Options concerning time stamps and deadlines in Org-mode."
  2257. :tag "Org Time"
  2258. :group 'org)
  2259. (defcustom org-insert-labeled-timestamps-at-point nil
  2260. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2261. When nil, these labeled time stamps are forces into the second line of an
  2262. entry, just after the headline. When scheduling from the global TODO list,
  2263. the time stamp will always be forced into the second line."
  2264. :group 'org-time
  2265. :type 'boolean)
  2266. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2267. "Formats for `format-time-string' which are used for time stamps.
  2268. It is not recommended to change this constant.")
  2269. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2270. "Number of minutes to round time stamps to.
  2271. These are two values, the first applies when first creating a time stamp.
  2272. The second applies when changing it with the commands `S-up' and `S-down'.
  2273. When changing the time stamp, this means that it will change in steps
  2274. of N minutes, as given by the second value.
  2275. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2276. numbers should be factors of 60, so for example 5, 10, 15.
  2277. When this is larger than 1, you can still force an exact time stamp by using
  2278. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2279. and by using a prefix arg to `S-up/down' to specify the exact number
  2280. of minutes to shift."
  2281. :group 'org-time
  2282. :get #'(lambda (var) ; Make sure both elements are there
  2283. (if (integerp (default-value var))
  2284. (list (default-value var) 5)
  2285. (default-value var)))
  2286. :type '(list
  2287. (integer :tag "when inserting times")
  2288. (integer :tag "when modifying times")))
  2289. ;; Normalize old customizations of this variable.
  2290. (when (integerp org-time-stamp-rounding-minutes)
  2291. (setq org-time-stamp-rounding-minutes
  2292. (list org-time-stamp-rounding-minutes
  2293. org-time-stamp-rounding-minutes)))
  2294. (defcustom org-display-custom-times nil
  2295. "Non-nil means overlay custom formats over all time stamps.
  2296. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2297. To turn this on on a per-file basis, insert anywhere in the file:
  2298. #+STARTUP: customtime"
  2299. :group 'org-time
  2300. :set 'set-default
  2301. :type 'sexp)
  2302. (make-variable-buffer-local 'org-display-custom-times)
  2303. (defcustom org-time-stamp-custom-formats
  2304. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2305. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2306. These are overlaid over the default ISO format if the variable
  2307. `org-display-custom-times' is set. Time like %H:%M should be at the
  2308. end of the second format. The custom formats are also honored by export
  2309. commands, if custom time display is turned on at the time of export."
  2310. :group 'org-time
  2311. :type 'sexp)
  2312. (defun org-time-stamp-format (&optional long inactive)
  2313. "Get the right format for a time string."
  2314. (let ((f (if long (cdr org-time-stamp-formats)
  2315. (car org-time-stamp-formats))))
  2316. (if inactive
  2317. (concat "[" (substring f 1 -1) "]")
  2318. f)))
  2319. (defcustom org-time-clocksum-format "%d:%02d"
  2320. "The format string used when creating CLOCKSUM lines.
  2321. This is also used when org-mode generates a time duration."
  2322. :group 'org-time
  2323. :type 'string)
  2324. (defcustom org-time-clocksum-use-fractional nil
  2325. "If non-nil, \\[org-clock-display] uses fractional times.
  2326. org-mode generates a time duration."
  2327. :group 'org-time
  2328. :type 'boolean)
  2329. (defcustom org-time-clocksum-fractional-format "%.2f"
  2330. "The format string used when creating CLOCKSUM lines, or when
  2331. org-mode generates a time duration."
  2332. :group 'org-time
  2333. :type 'string)
  2334. (defcustom org-deadline-warning-days 14
  2335. "No. of days before expiration during which a deadline becomes active.
  2336. This variable governs the display in sparse trees and in the agenda.
  2337. When 0 or negative, it means use this number (the absolute value of it)
  2338. even if a deadline has a different individual lead time specified.
  2339. Custom commands can set this variable in the options section."
  2340. :group 'org-time
  2341. :group 'org-agenda-daily/weekly
  2342. :type 'integer)
  2343. (defcustom org-read-date-prefer-future t
  2344. "Non-nil means assume future for incomplete date input from user.
  2345. This affects the following situations:
  2346. 1. The user gives a month but not a year.
  2347. For example, if it is April and you enter \"feb 2\", this will be read
  2348. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2349. 2. The user gives a day, but no month.
  2350. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2351. read this as the third of *next* month. However, if you enter \"17\",
  2352. it will be considered as *this* month.
  2353. If you set this variable to the symbol `time', then also the following
  2354. will work:
  2355. 3. If the user gives a time, but no day. If the time is before now,
  2356. to will be interpreted as tomorrow.
  2357. Currently none of this works for ISO week specifications.
  2358. When this option is nil, the current day, month and year will always be
  2359. used as defaults.
  2360. See also `org-agenda-jump-prefer-future'."
  2361. :group 'org-time
  2362. :type '(choice
  2363. (const :tag "Never" nil)
  2364. (const :tag "Check month and day" t)
  2365. (const :tag "Check month, day, and time" time)))
  2366. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2367. "Should the agenda jump command prefer the future for incomplete dates?
  2368. The default is to do the same as configured in `org-read-date-prefer-future'.
  2369. But you can also set a deviating value here.
  2370. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2371. :group 'org-agenda
  2372. :group 'org-time
  2373. :type '(choice
  2374. (const :tag "Use org-read-date-prefer-future"
  2375. org-read-date-prefer-future)
  2376. (const :tag "Never" nil)
  2377. (const :tag "Always" t)))
  2378. (defcustom org-read-date-force-compatible-dates t
  2379. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2380. Depending on the system Emacs is running on, certain dates cannot
  2381. be represented with the type used internally to represent time.
  2382. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2383. correctly. Some systems allow for earlier dates, some for later,
  2384. some for both. One way to find out it to insert any date into an
  2385. Org buffer, putting the cursor on the year and hitting S-up and
  2386. S-down to test the range.
  2387. When this variable is set to t, the date/time prompt will not let
  2388. you specify dates outside the 1970-2037 range, so it is certain that
  2389. these dates will work in whatever version of Emacs you are
  2390. running, and also that you can move a file from one Emacs implementation
  2391. to another. WHenever Org is forcing the year for you, it will display
  2392. a message and beep.
  2393. When this variable is nil, Org will check if the date is
  2394. representable in the specific Emacs implementation you are using.
  2395. If not, it will force a year, usually the current year, and beep
  2396. to remind you. Currently this setting is not recommended because
  2397. the likelihood that you will open your Org files in an Emacs that
  2398. has limited date range is not negligible.
  2399. A workaround for this problem is to use diary sexp dates for time
  2400. stamps outside of this range."
  2401. :group 'org-time
  2402. :type 'boolean)
  2403. (defcustom org-read-date-display-live t
  2404. "Non-nil means display current interpretation of date prompt live.
  2405. This display will be in an overlay, in the minibuffer."
  2406. :group 'org-time
  2407. :type 'boolean)
  2408. (defcustom org-read-date-popup-calendar t
  2409. "Non-nil means pop up a calendar when prompting for a date.
  2410. In the calendar, the date can be selected with mouse-1. However, the
  2411. minibuffer will also be active, and you can simply enter the date as well.
  2412. When nil, only the minibuffer will be available."
  2413. :group 'org-time
  2414. :type 'boolean)
  2415. (if (fboundp 'defvaralias)
  2416. (defvaralias 'org-popup-calendar-for-date-prompt
  2417. 'org-read-date-popup-calendar))
  2418. (defcustom org-read-date-minibuffer-setup-hook nil
  2419. "Hook to be used to set up keys for the date/time interface.
  2420. Add key definitions to `minibuffer-local-map', which will be a temporary
  2421. copy."
  2422. :group 'org-time
  2423. :type 'hook)
  2424. (defcustom org-extend-today-until 0
  2425. "The hour when your day really ends. Must be an integer.
  2426. This has influence for the following applications:
  2427. - When switching the agenda to \"today\". It it is still earlier than
  2428. the time given here, the day recognized as TODAY is actually yesterday.
  2429. - When a date is read from the user and it is still before the time given
  2430. here, the current date and time will be assumed to be yesterday, 23:59.
  2431. Also, timestamps inserted in remember templates follow this rule.
  2432. IMPORTANT: This is a feature whose implementation is and likely will
  2433. remain incomplete. Really, it is only here because past midnight seems to
  2434. be the favorite working time of John Wiegley :-)"
  2435. :group 'org-time
  2436. :type 'integer)
  2437. (defcustom org-use-effective-time nil
  2438. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2439. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2440. \"effective time\" of any timestamps between midnight and 8am will be
  2441. 23:59 of the previous day."
  2442. :group 'boolean
  2443. :type 'integer)
  2444. (defcustom org-edit-timestamp-down-means-later nil
  2445. "Non-nil means S-down will increase the time in a time stamp.
  2446. When nil, S-up will increase."
  2447. :group 'org-time
  2448. :type 'boolean)
  2449. (defcustom org-calendar-follow-timestamp-change t
  2450. "Non-nil means make the calendar window follow timestamp changes.
  2451. When a timestamp is modified and the calendar window is visible, it will be
  2452. moved to the new date."
  2453. :group 'org-time
  2454. :type 'boolean)
  2455. (defgroup org-tags nil
  2456. "Options concerning tags in Org-mode."
  2457. :tag "Org Tags"
  2458. :group 'org)
  2459. (defcustom org-tag-alist nil
  2460. "List of tags allowed in Org-mode files.
  2461. When this list is nil, Org-mode will base TAG input on what is already in the
  2462. buffer.
  2463. The value of this variable is an alist, the car of each entry must be a
  2464. keyword as a string, the cdr may be a character that is used to select
  2465. that tag through the fast-tag-selection interface.
  2466. See the manual for details."
  2467. :group 'org-tags
  2468. :type '(repeat
  2469. (choice
  2470. (cons (string :tag "Tag name")
  2471. (character :tag "Access char"))
  2472. (list :tag "Start radio group"
  2473. (const :startgroup)
  2474. (option (string :tag "Group description")))
  2475. (list :tag "End radio group"
  2476. (const :endgroup)
  2477. (option (string :tag "Group description")))
  2478. (const :tag "New line" (:newline)))))
  2479. (defcustom org-tag-persistent-alist nil
  2480. "List of tags that will always appear in all Org-mode files.
  2481. This is in addition to any in buffer settings or customizations
  2482. of `org-tag-alist'.
  2483. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2484. The value of this variable is an alist, the car of each entry must be a
  2485. keyword as a string, the cdr may be a character that is used to select
  2486. that tag through the fast-tag-selection interface.
  2487. See the manual for details.
  2488. To disable these tags on a per-file basis, insert anywhere in the file:
  2489. #+STARTUP: noptag"
  2490. :group 'org-tags
  2491. :type '(repeat
  2492. (choice
  2493. (cons (string :tag "Tag name")
  2494. (character :tag "Access char"))
  2495. (const :tag "Start radio group" (:startgroup))
  2496. (const :tag "End radio group" (:endgroup))
  2497. (const :tag "New line" (:newline)))))
  2498. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2499. "If non-nil, always offer completion for all tags of all agenda files.
  2500. Instead of customizing this variable directly, you might want to
  2501. set it locally for capture buffers, because there no list of
  2502. tags in that file can be created dynamically (there are none).
  2503. (add-hook 'org-capture-mode-hook
  2504. (lambda ()
  2505. (set (make-local-variable
  2506. 'org-complete-tags-always-offer-all-agenda-tags)
  2507. t)))"
  2508. :group 'org-tags
  2509. :type 'boolean)
  2510. (defvar org-file-tags nil
  2511. "List of tags that can be inherited by all entries in the file.
  2512. The tags will be inherited if the variable `org-use-tag-inheritance'
  2513. says they should be.
  2514. This variable is populated from #+FILETAGS lines.")
  2515. (defcustom org-use-fast-tag-selection 'auto
  2516. "Non-nil means use fast tag selection scheme.
  2517. This is a special interface to select and deselect tags with single keys.
  2518. When nil, fast selection is never used.
  2519. When the symbol `auto', fast selection is used if and only if selection
  2520. characters for tags have been configured, either through the variable
  2521. `org-tag-alist' or through a #+TAGS line in the buffer.
  2522. When t, fast selection is always used and selection keys are assigned
  2523. automatically if necessary."
  2524. :group 'org-tags
  2525. :type '(choice
  2526. (const :tag "Always" t)
  2527. (const :tag "Never" nil)
  2528. (const :tag "When selection characters are configured" 'auto)))
  2529. (defcustom org-fast-tag-selection-single-key nil
  2530. "Non-nil means fast tag selection exits after first change.
  2531. When nil, you have to press RET to exit it.
  2532. During fast tag selection, you can toggle this flag with `C-c'.
  2533. This variable can also have the value `expert'. In this case, the window
  2534. displaying the tags menu is not even shown, until you press C-c again."
  2535. :group 'org-tags
  2536. :type '(choice
  2537. (const :tag "No" nil)
  2538. (const :tag "Yes" t)
  2539. (const :tag "Expert" expert)))
  2540. (defvar org-fast-tag-selection-include-todo nil
  2541. "Non-nil means fast tags selection interface will also offer TODO states.
  2542. This is an undocumented feature, you should not rely on it.")
  2543. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2544. "The column to which tags should be indented in a headline.
  2545. If this number is positive, it specifies the column. If it is negative,
  2546. it means that the tags should be flushright to that column. For example,
  2547. -80 works well for a normal 80 character screen.
  2548. When 0, place tags directly after headline text, with only one space in
  2549. between."
  2550. :group 'org-tags
  2551. :type 'integer)
  2552. (defcustom org-auto-align-tags t
  2553. "Non-nil keeps tags aligned when modifying headlines.
  2554. Some operations (i.e. demoting) change the length of a headline and
  2555. therefore shift the tags around. With this option turned on, after
  2556. each such operation the tags are again aligned to `org-tags-column'."
  2557. :group 'org-tags
  2558. :type 'boolean)
  2559. (defcustom org-use-tag-inheritance t
  2560. "Non-nil means tags in levels apply also for sublevels.
  2561. When nil, only the tags directly given in a specific line apply there.
  2562. This may also be a list of tags that should be inherited, or a regexp that
  2563. matches tags that should be inherited. Additional control is possible
  2564. with the variable `org-tags-exclude-from-inheritance' which gives an
  2565. explicit list of tags to be excluded from inheritance., even if the value of
  2566. `org-use-tag-inheritance' would select it for inheritance.
  2567. If this option is t, a match early-on in a tree can lead to a large
  2568. number of matches in the subtree when constructing the agenda or creating
  2569. a sparse tree. If you only want to see the first match in a tree during
  2570. a search, check out the variable `org-tags-match-list-sublevels'."
  2571. :group 'org-tags
  2572. :type '(choice
  2573. (const :tag "Not" nil)
  2574. (const :tag "Always" t)
  2575. (repeat :tag "Specific tags" (string :tag "Tag"))
  2576. (regexp :tag "Tags matched by regexp")))
  2577. (defcustom org-tags-exclude-from-inheritance nil
  2578. "List of tags that should never be inherited.
  2579. This is a way to exclude a few tags from inheritance. For way to do
  2580. the opposite, to actively allow inheritance for selected tags,
  2581. see the variable `org-use-tag-inheritance'."
  2582. :group 'org-tags
  2583. :type '(repeat (string :tag "Tag")))
  2584. (defun org-tag-inherit-p (tag)
  2585. "Check if TAG is one that should be inherited."
  2586. (cond
  2587. ((member tag org-tags-exclude-from-inheritance) nil)
  2588. ((eq org-use-tag-inheritance t) t)
  2589. ((not org-use-tag-inheritance) nil)
  2590. ((stringp org-use-tag-inheritance)
  2591. (string-match org-use-tag-inheritance tag))
  2592. ((listp org-use-tag-inheritance)
  2593. (member tag org-use-tag-inheritance))
  2594. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2595. (defcustom org-tags-match-list-sublevels t
  2596. "Non-nil means list also sublevels of headlines matching a search.
  2597. This variable applies to tags/property searches, and also to stuck
  2598. projects because this search is based on a tags match as well.
  2599. When set to the symbol `indented', sublevels are indented with
  2600. leading dots.
  2601. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2602. the sublevels of a headline matching a tag search often also match
  2603. the same search. Listing all of them can create very long lists.
  2604. Setting this variable to nil causes subtrees of a match to be skipped.
  2605. This variable is semi-obsolete and probably should always be true. It
  2606. is better to limit inheritance to certain tags using the variables
  2607. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2608. :group 'org-tags
  2609. :type '(choice
  2610. (const :tag "No, don't list them" nil)
  2611. (const :tag "Yes, do list them" t)
  2612. (const :tag "List them, indented with leading dots" indented)))
  2613. (defcustom org-tags-sort-function nil
  2614. "When set, tags are sorted using this function as a comparator."
  2615. :group 'org-tags
  2616. :type '(choice
  2617. (const :tag "No sorting" nil)
  2618. (const :tag "Alphabetical" string<)
  2619. (const :tag "Reverse alphabetical" string>)
  2620. (function :tag "Custom function" nil)))
  2621. (defvar org-tags-history nil
  2622. "History of minibuffer reads for tags.")
  2623. (defvar org-last-tags-completion-table nil
  2624. "The last used completion table for tags.")
  2625. (defvar org-after-tags-change-hook nil
  2626. "Hook that is run after the tags in a line have changed.")
  2627. (defgroup org-properties nil
  2628. "Options concerning properties in Org-mode."
  2629. :tag "Org Properties"
  2630. :group 'org)
  2631. (defcustom org-property-format "%-10s %s"
  2632. "How property key/value pairs should be formatted by `indent-line'.
  2633. When `indent-line' hits a property definition, it will format the line
  2634. according to this format, mainly to make sure that the values are
  2635. lined-up with respect to each other."
  2636. :group 'org-properties
  2637. :type 'string)
  2638. (defcustom org-properties-postprocess-alist nil
  2639. "Alist of properties and functions to adjust inserted values.
  2640. Elements of this alist must be of the form
  2641. ([string] [function])
  2642. where [string] must be a property name and [function] must be a
  2643. lambda expression: this lambda expression must take one argument,
  2644. the value to adjust, and return the new value as a string.
  2645. For example, this element will allow the property \"Remaining\"
  2646. to be updated wrt the relation between the \"Effort\" property
  2647. and the clock summary:
  2648. ((\"Remaining\" (lambda(value)
  2649. (let ((clocksum (org-clock-sum-current-item))
  2650. (effort (org-duration-string-to-minutes
  2651. (org-entry-get (point) \"Effort\"))))
  2652. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2653. :group 'org-properties
  2654. :type 'alist)
  2655. (defcustom org-use-property-inheritance nil
  2656. "Non-nil means properties apply also for sublevels.
  2657. This setting is chiefly used during property searches. Turning it on can
  2658. cause significant overhead when doing a search, which is why it is not
  2659. on by default.
  2660. When nil, only the properties directly given in the current entry count.
  2661. When t, every property is inherited. The value may also be a list of
  2662. properties that should have inheritance, or a regular expression matching
  2663. properties that should be inherited.
  2664. However, note that some special properties use inheritance under special
  2665. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2666. and the properties ending in \"_ALL\" when they are used as descriptor
  2667. for valid values of a property.
  2668. Note for programmers:
  2669. When querying an entry with `org-entry-get', you can control if inheritance
  2670. should be used. By default, `org-entry-get' looks only at the local
  2671. properties. You can request inheritance by setting the inherit argument
  2672. to t (to force inheritance) or to `selective' (to respect the setting
  2673. in this variable)."
  2674. :group 'org-properties
  2675. :type '(choice
  2676. (const :tag "Not" nil)
  2677. (const :tag "Always" t)
  2678. (repeat :tag "Specific properties" (string :tag "Property"))
  2679. (regexp :tag "Properties matched by regexp")))
  2680. (defun org-property-inherit-p (property)
  2681. "Check if PROPERTY is one that should be inherited."
  2682. (cond
  2683. ((eq org-use-property-inheritance t) t)
  2684. ((not org-use-property-inheritance) nil)
  2685. ((stringp org-use-property-inheritance)
  2686. (string-match org-use-property-inheritance property))
  2687. ((listp org-use-property-inheritance)
  2688. (member property org-use-property-inheritance))
  2689. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2690. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2691. "The default column format, if no other format has been defined.
  2692. This variable can be set on the per-file basis by inserting a line
  2693. #+COLUMNS: %25ITEM ....."
  2694. :group 'org-properties
  2695. :type 'string)
  2696. (defcustom org-columns-ellipses ".."
  2697. "The ellipses to be used when a field in column view is truncated.
  2698. When this is the empty string, as many characters as possible are shown,
  2699. but then there will be no visual indication that the field has been truncated.
  2700. When this is a string of length N, the last N characters of a truncated
  2701. field are replaced by this string. If the column is narrower than the
  2702. ellipses string, only part of the ellipses string will be shown."
  2703. :group 'org-properties
  2704. :type 'string)
  2705. (defcustom org-columns-modify-value-for-display-function nil
  2706. "Function that modifies values for display in column view.
  2707. For example, it can be used to cut out a certain part from a time stamp.
  2708. The function must take 2 arguments:
  2709. column-title The title of the column (*not* the property name)
  2710. value The value that should be modified.
  2711. The function should return the value that should be displayed,
  2712. or nil if the normal value should be used."
  2713. :group 'org-properties
  2714. :type 'function)
  2715. (defcustom org-effort-property "Effort"
  2716. "The property that is being used to keep track of effort estimates.
  2717. Effort estimates given in this property need to have the format H:MM."
  2718. :group 'org-properties
  2719. :group 'org-progress
  2720. :type '(string :tag "Property"))
  2721. (defconst org-global-properties-fixed
  2722. '(("VISIBILITY_ALL" . "folded children content all")
  2723. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2724. "List of property/value pairs that can be inherited by any entry.
  2725. These are fixed values, for the preset properties. The user variable
  2726. that can be used to add to this list is `org-global-properties'.
  2727. The entries in this list are cons cells where the car is a property
  2728. name and cdr is a string with the value. If the value represents
  2729. multiple items like an \"_ALL\" property, separate the items by
  2730. spaces.")
  2731. (defcustom org-global-properties nil
  2732. "List of property/value pairs that can be inherited by any entry.
  2733. This list will be combined with the constant `org-global-properties-fixed'.
  2734. The entries in this list are cons cells where the car is a property
  2735. name and cdr is a string with the value.
  2736. You can set buffer-local values for the same purpose in the variable
  2737. `org-file-properties' this by adding lines like
  2738. #+PROPERTY: NAME VALUE"
  2739. :group 'org-properties
  2740. :type '(repeat
  2741. (cons (string :tag "Property")
  2742. (string :tag "Value"))))
  2743. (defvar org-file-properties nil
  2744. "List of property/value pairs that can be inherited by any entry.
  2745. Valid for the current buffer.
  2746. This variable is populated from #+PROPERTY lines.")
  2747. (make-variable-buffer-local 'org-file-properties)
  2748. (defgroup org-agenda nil
  2749. "Options concerning agenda views in Org-mode."
  2750. :tag "Org Agenda"
  2751. :group 'org)
  2752. (defvar org-category nil
  2753. "Variable used by org files to set a category for agenda display.
  2754. Such files should use a file variable to set it, for example
  2755. # -*- mode: org; org-category: \"ELisp\"
  2756. or contain a special line
  2757. #+CATEGORY: ELisp
  2758. If the file does not specify a category, then file's base name
  2759. is used instead.")
  2760. (make-variable-buffer-local 'org-category)
  2761. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2762. (defcustom org-agenda-files nil
  2763. "The files to be used for agenda display.
  2764. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2765. \\[org-remove-file]. You can also use customize to edit the list.
  2766. If an entry is a directory, all files in that directory that are matched by
  2767. `org-agenda-file-regexp' will be part of the file list.
  2768. If the value of the variable is not a list but a single file name, then
  2769. the list of agenda files is actually stored and maintained in that file, one
  2770. agenda file per line. In this file paths can be given relative to
  2771. `org-directory'. Tilde expansion and environment variable substitution
  2772. are also made."
  2773. :group 'org-agenda
  2774. :type '(choice
  2775. (repeat :tag "List of files and directories" file)
  2776. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2777. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2778. "Regular expression to match files for `org-agenda-files'.
  2779. If any element in the list in that variable contains a directory instead
  2780. of a normal file, all files in that directory that are matched by this
  2781. regular expression will be included."
  2782. :group 'org-agenda
  2783. :type 'regexp)
  2784. (defcustom org-agenda-text-search-extra-files nil
  2785. "List of extra files to be searched by text search commands.
  2786. These files will be search in addition to the agenda files by the
  2787. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2788. Note that these files will only be searched for text search commands,
  2789. not for the other agenda views like todo lists, tag searches or the weekly
  2790. agenda. This variable is intended to list notes and possibly archive files
  2791. that should also be searched by these two commands.
  2792. In fact, if the first element in the list is the symbol `agenda-archives',
  2793. than all archive files of all agenda files will be added to the search
  2794. scope."
  2795. :group 'org-agenda
  2796. :type '(set :greedy t
  2797. (const :tag "Agenda Archives" agenda-archives)
  2798. (repeat :inline t (file))))
  2799. (if (fboundp 'defvaralias)
  2800. (defvaralias 'org-agenda-multi-occur-extra-files
  2801. 'org-agenda-text-search-extra-files))
  2802. (defcustom org-agenda-skip-unavailable-files nil
  2803. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2804. A nil value means to remove them, after a query, from the list."
  2805. :group 'org-agenda
  2806. :type 'boolean)
  2807. (defcustom org-calendar-to-agenda-key [?c]
  2808. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2809. The command `org-calendar-goto-agenda' will be bound to this key. The
  2810. default is the character `c' because then `c' can be used to switch back and
  2811. forth between agenda and calendar."
  2812. :group 'org-agenda
  2813. :type 'sexp)
  2814. (defcustom org-calendar-agenda-action-key [?k]
  2815. "The key to be installed in `calendar-mode-map' for agenda-action.
  2816. The command `org-agenda-action' will be bound to this key. The
  2817. default is the character `k' because we use the same key in the agenda."
  2818. :group 'org-agenda
  2819. :type 'sexp)
  2820. (defcustom org-calendar-insert-diary-entry-key [?i]
  2821. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2822. This option is irrelevant until `org-agenda-diary-file' has been configured
  2823. to point to an Org-mode file. When that is the case, the command
  2824. `org-agenda-diary-entry' will be bound to the key given here, by default
  2825. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2826. if you want to continue doing this, you need to change this to a different
  2827. key."
  2828. :group 'org-agenda
  2829. :type 'sexp)
  2830. (defcustom org-agenda-diary-file 'diary-file
  2831. "File to which to add new entries with the `i' key in agenda and calendar.
  2832. When this is the symbol `diary-file', the functionality in the Emacs
  2833. calendar will be used to add entries to the `diary-file'. But when this
  2834. points to a file, `org-agenda-diary-entry' will be used instead."
  2835. :group 'org-agenda
  2836. :type '(choice
  2837. (const :tag "The standard Emacs diary file" diary-file)
  2838. (file :tag "Special Org file diary entries")))
  2839. (eval-after-load "calendar"
  2840. '(progn
  2841. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2842. 'org-calendar-goto-agenda)
  2843. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2844. 'org-agenda-action)
  2845. (add-hook 'calendar-mode-hook
  2846. (lambda ()
  2847. (unless (eq org-agenda-diary-file 'diary-file)
  2848. (define-key calendar-mode-map
  2849. org-calendar-insert-diary-entry-key
  2850. 'org-agenda-diary-entry))))))
  2851. (defgroup org-latex nil
  2852. "Options for embedding LaTeX code into Org-mode."
  2853. :tag "Org LaTeX"
  2854. :group 'org)
  2855. (defcustom org-format-latex-options
  2856. '(:foreground default :background default :scale 1.0
  2857. :html-foreground "Black" :html-background "Transparent"
  2858. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2859. "Options for creating images from LaTeX fragments.
  2860. This is a property list with the following properties:
  2861. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2862. `default' means use the foreground of the default face.
  2863. :background the background color, or \"Transparent\".
  2864. `default' means use the background of the default face.
  2865. :scale a scaling factor for the size of the images, to get more pixels
  2866. :html-foreground, :html-background, :html-scale
  2867. the same numbers for HTML export.
  2868. :matchers a list indicating which matchers should be used to
  2869. find LaTeX fragments. Valid members of this list are:
  2870. \"begin\" find environments
  2871. \"$1\" find single characters surrounded by $.$
  2872. \"$\" find math expressions surrounded by $...$
  2873. \"$$\" find math expressions surrounded by $$....$$
  2874. \"\\(\" find math expressions surrounded by \\(...\\)
  2875. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2876. :group 'org-latex
  2877. :type 'plist)
  2878. (defcustom org-format-latex-signal-error t
  2879. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2880. When nil, just push out a message."
  2881. :group 'org-latex
  2882. :type 'boolean)
  2883. (defcustom org-latex-to-mathml-jar-file nil
  2884. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2885. Use this to specify additional executable file say a jar file.
  2886. When using MathToWeb as the converter, specify the full-path to
  2887. your mathtoweb.jar file."
  2888. :group 'org-latex
  2889. :type '(choice
  2890. (const :tag "None" nil)
  2891. (file :tag "JAR file" :must-match t)))
  2892. (defcustom org-latex-to-mathml-convert-command nil
  2893. "Command to convert LaTeX fragments to MathML.
  2894. Replace format-specifiers in the command as noted below and use
  2895. `shell-command' to convert LaTeX to MathML.
  2896. %j: Executable file in fully expanded form as specified by
  2897. `org-latex-to-mathml-jar-file'.
  2898. %I: Input LaTeX file in fully expanded form
  2899. %o: Output MathML file
  2900. This command is used by `org-create-math-formula'.
  2901. When using MathToWeb as the converter, set this to
  2902. \"java -jar %j -unicode -force -df %o %I\"."
  2903. :group 'org-latex
  2904. :type '(choice
  2905. (const :tag "None" nil)
  2906. (string :tag "\nShell command")))
  2907. (defun org-format-latex-mathml-available-p ()
  2908. "Return t if `org-latex-to-mathml-convert-command' is usable."
  2909. (save-match-data
  2910. (when (and (boundp 'org-latex-to-mathml-convert-command)
  2911. org-latex-to-mathml-convert-command)
  2912. (let ((executable (car (split-string
  2913. org-latex-to-mathml-convert-command))))
  2914. (when (executable-find executable)
  2915. (if (string-match
  2916. "%j" org-latex-to-mathml-convert-command)
  2917. (file-readable-p org-latex-to-mathml-jar-file)
  2918. t))))))
  2919. (defcustom org-format-latex-header "\\documentclass{article}
  2920. \\usepackage[usenames]{color}
  2921. \\usepackage{amsmath}
  2922. \\usepackage[mathscr]{eucal}
  2923. \\pagestyle{empty} % do not remove
  2924. \[PACKAGES]
  2925. \[DEFAULT-PACKAGES]
  2926. % The settings below are copied from fullpage.sty
  2927. \\setlength{\\textwidth}{\\paperwidth}
  2928. \\addtolength{\\textwidth}{-3cm}
  2929. \\setlength{\\oddsidemargin}{1.5cm}
  2930. \\addtolength{\\oddsidemargin}{-2.54cm}
  2931. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2932. \\setlength{\\textheight}{\\paperheight}
  2933. \\addtolength{\\textheight}{-\\headheight}
  2934. \\addtolength{\\textheight}{-\\headsep}
  2935. \\addtolength{\\textheight}{-\\footskip}
  2936. \\addtolength{\\textheight}{-3cm}
  2937. \\setlength{\\topmargin}{1.5cm}
  2938. \\addtolength{\\topmargin}{-2.54cm}"
  2939. "The document header used for processing LaTeX fragments.
  2940. It is imperative that this header make sure that no page number
  2941. appears on the page. The package defined in the variables
  2942. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2943. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2944. will be appended."
  2945. :group 'org-latex
  2946. :type 'string)
  2947. (defvar org-format-latex-header-extra nil)
  2948. (defun org-set-packages-alist (var val)
  2949. "Set the packages alist and make sure it has 3 elements per entry."
  2950. (set var (mapcar (lambda (x)
  2951. (if (and (consp x) (= (length x) 2))
  2952. (list (car x) (nth 1 x) t)
  2953. x))
  2954. val)))
  2955. (defun org-get-packages-alist (var)
  2956. "Get the packages alist and make sure it has 3 elements per entry."
  2957. (mapcar (lambda (x)
  2958. (if (and (consp x) (= (length x) 2))
  2959. (list (car x) (nth 1 x) t)
  2960. x))
  2961. (default-value var)))
  2962. ;; The following variables are defined here because is it also used
  2963. ;; when formatting latex fragments. Originally it was part of the
  2964. ;; LaTeX exporter, which is why the name includes "export".
  2965. (defcustom org-export-latex-default-packages-alist
  2966. '(("AUTO" "inputenc" t)
  2967. ("T1" "fontenc" t)
  2968. ("" "fixltx2e" nil)
  2969. ("" "graphicx" t)
  2970. ("" "longtable" nil)
  2971. ("" "float" nil)
  2972. ("" "wrapfig" nil)
  2973. ("" "soul" t)
  2974. ("" "textcomp" t)
  2975. ("" "marvosym" t)
  2976. ("" "wasysym" t)
  2977. ("" "latexsym" t)
  2978. ("" "amssymb" t)
  2979. ("" "hyperref" nil)
  2980. "\\tolerance=1000"
  2981. )
  2982. "Alist of default packages to be inserted in the header.
  2983. Change this only if one of the packages here causes an incompatibility
  2984. with another package you are using.
  2985. The packages in this list are needed by one part or another of Org-mode
  2986. to function properly.
  2987. - inputenc, fontenc: for basic font and character selection
  2988. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2989. for interpreting the entities in `org-entities'. You can skip some of these
  2990. packages if you don't use any of the symbols in it.
  2991. - graphicx: for including images
  2992. - float, wrapfig: for figure placement
  2993. - longtable: for long tables
  2994. - hyperref: for cross references
  2995. Therefore you should not modify this variable unless you know what you
  2996. are doing. The one reason to change it anyway is that you might be loading
  2997. some other package that conflicts with one of the default packages.
  2998. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  2999. If SNIPPET-FLAG is t, the package also needs to be included when
  3000. compiling LaTeX snippets into images for inclusion into HTML."
  3001. :group 'org-export-latex
  3002. :set 'org-set-packages-alist
  3003. :get 'org-get-packages-alist
  3004. :type '(repeat
  3005. (choice
  3006. (list :tag "options/package pair"
  3007. (string :tag "options")
  3008. (string :tag "package")
  3009. (boolean :tag "Snippet"))
  3010. (string :tag "A line of LaTeX"))))
  3011. (defcustom org-export-latex-packages-alist nil
  3012. "Alist of packages to be inserted in every LaTeX header.
  3013. These will be inserted after `org-export-latex-default-packages-alist'.
  3014. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3015. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3016. turning LaTeX snippets into images for inclusion into HTML.
  3017. Make sure that you only list packages here which:
  3018. - you want in every file
  3019. - do not conflict with the default packages in
  3020. `org-export-latex-default-packages-alist'
  3021. - do not conflict with the setup in `org-format-latex-header'."
  3022. :group 'org-export-latex
  3023. :set 'org-set-packages-alist
  3024. :get 'org-get-packages-alist
  3025. :type '(repeat
  3026. (choice
  3027. (list :tag "options/package pair"
  3028. (string :tag "options")
  3029. (string :tag "package")
  3030. (boolean :tag "Snippet"))
  3031. (string :tag "A line of LaTeX"))))
  3032. (defgroup org-appearance nil
  3033. "Settings for Org-mode appearance."
  3034. :tag "Org Appearance"
  3035. :group 'org)
  3036. (defcustom org-level-color-stars-only nil
  3037. "Non-nil means fontify only the stars in each headline.
  3038. When nil, the entire headline is fontified.
  3039. Changing it requires restart of `font-lock-mode' to become effective
  3040. also in regions already fontified."
  3041. :group 'org-appearance
  3042. :type 'boolean)
  3043. (defcustom org-hide-leading-stars nil
  3044. "Non-nil means hide the first N-1 stars in a headline.
  3045. This works by using the face `org-hide' for these stars. This
  3046. face is white for a light background, and black for a dark
  3047. background. You may have to customize the face `org-hide' to
  3048. make this work.
  3049. Changing it requires restart of `font-lock-mode' to become effective
  3050. also in regions already fontified.
  3051. You may also set this on a per-file basis by adding one of the following
  3052. lines to the buffer:
  3053. #+STARTUP: hidestars
  3054. #+STARTUP: showstars"
  3055. :group 'org-appearance
  3056. :type 'boolean)
  3057. (defcustom org-hidden-keywords nil
  3058. "List of symbols corresponding to keywords to be hidden the org buffer.
  3059. For example, a value '(title) for this list will make the document's title
  3060. appear in the buffer without the initial #+TITLE: keyword."
  3061. :group 'org-appearance
  3062. :type '(set (const :tag "#+AUTHOR" author)
  3063. (const :tag "#+DATE" date)
  3064. (const :tag "#+EMAIL" email)
  3065. (const :tag "#+TITLE" title)))
  3066. (defcustom org-fontify-done-headline nil
  3067. "Non-nil means change the face of a headline if it is marked DONE.
  3068. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3069. When this is non-nil, the headline after the keyword is set to the
  3070. `org-headline-done' as an additional indication."
  3071. :group 'org-appearance
  3072. :type 'boolean)
  3073. (defcustom org-fontify-emphasized-text t
  3074. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3075. Changing this variable requires a restart of Emacs to take effect."
  3076. :group 'org-appearance
  3077. :type 'boolean)
  3078. (defcustom org-fontify-whole-heading-line nil
  3079. "Non-nil means fontify the whole line for headings.
  3080. This is useful when setting a background color for the
  3081. org-level-* faces."
  3082. :group 'org-appearance
  3083. :type 'boolean)
  3084. (defcustom org-highlight-latex-fragments-and-specials nil
  3085. "Non-nil means fontify what is treated specially by the exporters."
  3086. :group 'org-appearance
  3087. :type 'boolean)
  3088. (defcustom org-hide-emphasis-markers nil
  3089. "Non-nil mean font-lock should hide the emphasis marker characters."
  3090. :group 'org-appearance
  3091. :type 'boolean)
  3092. (defcustom org-pretty-entities nil
  3093. "Non-nil means show entities as UTF8 characters.
  3094. When nil, the \\name form remains in the buffer."
  3095. :group 'org-appearance
  3096. :type 'boolean)
  3097. (defcustom org-pretty-entities-include-sub-superscripts t
  3098. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3099. :group 'org-appearance
  3100. :type 'boolean)
  3101. (defvar org-emph-re nil
  3102. "Regular expression for matching emphasis.
  3103. After a match, the match groups contain these elements:
  3104. 0 The match of the full regular expression, including the characters
  3105. before and after the proper match
  3106. 1 The character before the proper match, or empty at beginning of line
  3107. 2 The proper match, including the leading and trailing markers
  3108. 3 The leading marker like * or /, indicating the type of highlighting
  3109. 4 The text between the emphasis markers, not including the markers
  3110. 5 The character after the match, empty at the end of a line")
  3111. (defvar org-verbatim-re nil
  3112. "Regular expression for matching verbatim text.")
  3113. (defvar org-emphasis-regexp-components) ; defined just below
  3114. (defvar org-emphasis-alist) ; defined just below
  3115. (defun org-set-emph-re (var val)
  3116. "Set variable and compute the emphasis regular expression."
  3117. (set var val)
  3118. (when (and (boundp 'org-emphasis-alist)
  3119. (boundp 'org-emphasis-regexp-components)
  3120. org-emphasis-alist org-emphasis-regexp-components)
  3121. (let* ((e org-emphasis-regexp-components)
  3122. (pre (car e))
  3123. (post (nth 1 e))
  3124. (border (nth 2 e))
  3125. (body (nth 3 e))
  3126. (nl (nth 4 e))
  3127. (body1 (concat body "*?"))
  3128. (markers (mapconcat 'car org-emphasis-alist ""))
  3129. (vmarkers (mapconcat
  3130. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3131. org-emphasis-alist "")))
  3132. ;; make sure special characters appear at the right position in the class
  3133. (if (string-match "\\^" markers)
  3134. (setq markers (concat (replace-match "" t t markers) "^")))
  3135. (if (string-match "-" markers)
  3136. (setq markers (concat (replace-match "" t t markers) "-")))
  3137. (if (string-match "\\^" vmarkers)
  3138. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3139. (if (string-match "-" vmarkers)
  3140. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3141. (if (> nl 0)
  3142. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3143. (int-to-string nl) "\\}")))
  3144. ;; Make the regexp
  3145. (setq org-emph-re
  3146. (concat "\\([" pre "]\\|^\\)"
  3147. "\\("
  3148. "\\([" markers "]\\)"
  3149. "\\("
  3150. "[^" border "]\\|"
  3151. "[^" border "]"
  3152. body1
  3153. "[^" border "]"
  3154. "\\)"
  3155. "\\3\\)"
  3156. "\\([" post "]\\|$\\)"))
  3157. (setq org-verbatim-re
  3158. (concat "\\([" pre "]\\|^\\)"
  3159. "\\("
  3160. "\\([" vmarkers "]\\)"
  3161. "\\("
  3162. "[^" border "]\\|"
  3163. "[^" border "]"
  3164. body1
  3165. "[^" border "]"
  3166. "\\)"
  3167. "\\3\\)"
  3168. "\\([" post "]\\|$\\)")))))
  3169. (defcustom org-emphasis-regexp-components
  3170. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3171. "Components used to build the regular expression for emphasis.
  3172. This is a list with five entries. Terminology: In an emphasis string
  3173. like \" *strong word* \", we call the initial space PREMATCH, the final
  3174. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3175. and \"trong wor\" is the body. The different components in this variable
  3176. specify what is allowed/forbidden in each part:
  3177. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3178. post Chars allowed as postmatch. End of line will be allowed too.
  3179. border The chars *forbidden* as border characters.
  3180. body-regexp A regexp like \".\" to match a body character. Don't use
  3181. non-shy groups here, and don't allow newline here.
  3182. newline The maximum number of newlines allowed in an emphasis exp.
  3183. Use customize to modify this, or restart Emacs after changing it."
  3184. :group 'org-appearance
  3185. :set 'org-set-emph-re
  3186. :type '(list
  3187. (sexp :tag "Allowed chars in pre ")
  3188. (sexp :tag "Allowed chars in post ")
  3189. (sexp :tag "Forbidden chars in border ")
  3190. (sexp :tag "Regexp for body ")
  3191. (integer :tag "number of newlines allowed")
  3192. (option (boolean :tag "Please ignore this button"))))
  3193. (defcustom org-emphasis-alist
  3194. `(("*" bold "<b>" "</b>")
  3195. ("/" italic "<i>" "</i>")
  3196. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3197. ("=" org-code "<code>" "</code>" verbatim)
  3198. ("~" org-verbatim "<code>" "</code>" verbatim)
  3199. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3200. "<del>" "</del>")
  3201. )
  3202. "Special syntax for emphasized text.
  3203. Text starting and ending with a special character will be emphasized, for
  3204. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3205. characters, the face to be used by font-lock for highlighting in Org-mode
  3206. Emacs buffers, and the HTML tags to be used for this.
  3207. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3208. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3209. Use customize to modify this, or restart Emacs after changing it."
  3210. :group 'org-appearance
  3211. :set 'org-set-emph-re
  3212. :type '(repeat
  3213. (list
  3214. (string :tag "Marker character")
  3215. (choice
  3216. (face :tag "Font-lock-face")
  3217. (plist :tag "Face property list"))
  3218. (string :tag "HTML start tag")
  3219. (string :tag "HTML end tag")
  3220. (option (const verbatim)))))
  3221. (defvar org-protecting-blocks
  3222. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3223. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3224. This is needed for font-lock setup.")
  3225. ;;; Miscellaneous options
  3226. (defgroup org-completion nil
  3227. "Completion in Org-mode."
  3228. :tag "Org Completion"
  3229. :group 'org)
  3230. (defcustom org-completion-use-ido nil
  3231. "Non-nil means use ido completion wherever possible.
  3232. Note that `ido-mode' must be active for this variable to be relevant.
  3233. If you decide to turn this variable on, you might well want to turn off
  3234. `org-outline-path-complete-in-steps'.
  3235. See also `org-completion-use-iswitchb'."
  3236. :group 'org-completion
  3237. :type 'boolean)
  3238. (defcustom org-completion-use-iswitchb nil
  3239. "Non-nil means use iswitchb completion wherever possible.
  3240. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3241. If you decide to turn this variable on, you might well want to turn off
  3242. `org-outline-path-complete-in-steps'.
  3243. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3244. :group 'org-completion
  3245. :type 'boolean)
  3246. (defcustom org-completion-fallback-command 'hippie-expand
  3247. "The expansion command called by \\[pcomplete] in normal context.
  3248. Normal means, no org-mode-specific context."
  3249. :group 'org-completion
  3250. :type 'function)
  3251. ;;; Functions and variables from their packages
  3252. ;; Declared here to avoid compiler warnings
  3253. ;; XEmacs only
  3254. (defvar outline-mode-menu-heading)
  3255. (defvar outline-mode-menu-show)
  3256. (defvar outline-mode-menu-hide)
  3257. (defvar zmacs-regions) ; XEmacs regions
  3258. ;; Emacs only
  3259. (defvar mark-active)
  3260. ;; Various packages
  3261. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3262. (declare-function calendar-forward-day "cal-move" (arg))
  3263. (declare-function calendar-goto-date "cal-move" (date))
  3264. (declare-function calendar-goto-today "cal-move" ())
  3265. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3266. (defvar calc-embedded-close-formula)
  3267. (defvar calc-embedded-open-formula)
  3268. (declare-function cdlatex-tab "ext:cdlatex" ())
  3269. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3270. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3271. (defvar font-lock-unfontify-region-function)
  3272. (declare-function iswitchb-read-buffer "iswitchb"
  3273. (prompt &optional default require-match start matches-set))
  3274. (defvar iswitchb-temp-buflist)
  3275. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3276. (defvar org-agenda-tags-todo-honor-ignore-options)
  3277. (declare-function org-agenda-skip "org-agenda" ())
  3278. (declare-function
  3279. org-agenda-format-item "org-agenda"
  3280. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3281. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3282. (declare-function org-agenda-change-all-lines "org-agenda"
  3283. (newhead hdmarker &optional fixface just-this))
  3284. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3285. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3286. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3287. (beg end))
  3288. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3289. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3290. "org-agenda" (&optional end))
  3291. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3292. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3293. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3294. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3295. (declare-function org-indent-mode "org-indent" (&optional arg))
  3296. (declare-function parse-time-string "parse-time" (string))
  3297. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3298. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3299. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3300. (defvar remember-data-file)
  3301. (defvar texmathp-why)
  3302. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3303. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3304. (defvar w3m-current-url)
  3305. (defvar w3m-current-title)
  3306. (defvar org-latex-regexps)
  3307. ;;; Autoload and prepare some org modules
  3308. ;; Some table stuff that needs to be defined here, because it is used
  3309. ;; by the functions setting up org-mode or checking for table context.
  3310. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3311. "Detect an org-type or table-type table.")
  3312. (defconst org-table-line-regexp "^[ \t]*|"
  3313. "Detect an org-type table line.")
  3314. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3315. "Detect an org-type table line.")
  3316. (defconst org-table-hline-regexp "^[ \t]*|-"
  3317. "Detect an org-type table hline.")
  3318. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3319. "Detect a table-type table hline.")
  3320. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3321. "Detect the first line outside a table when searching from within it.
  3322. This works for both table types.")
  3323. ;; Autoload the functions in org-table.el that are needed by functions here.
  3324. (eval-and-compile
  3325. (org-autoload "org-table"
  3326. '(org-table-align org-table-begin org-table-blank-field
  3327. org-table-convert org-table-convert-region org-table-copy-down
  3328. org-table-copy-region org-table-create
  3329. org-table-create-or-convert-from-region
  3330. org-table-create-with-table.el org-table-current-dline
  3331. org-table-cut-region org-table-delete-column org-table-edit-field
  3332. org-table-edit-formulas org-table-end org-table-eval-formula
  3333. org-table-export org-table-field-info
  3334. org-table-get-stored-formulas org-table-goto-column
  3335. org-table-hline-and-move org-table-import org-table-insert-column
  3336. org-table-insert-hline org-table-insert-row org-table-iterate
  3337. org-table-justify-field-maybe org-table-kill-row
  3338. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3339. org-table-move-column org-table-move-column-left
  3340. org-table-move-column-right org-table-move-row
  3341. org-table-move-row-down org-table-move-row-up
  3342. org-table-next-field org-table-next-row org-table-paste-rectangle
  3343. org-table-previous-field org-table-recalculate
  3344. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3345. org-table-toggle-coordinate-overlays
  3346. org-table-toggle-formula-debugger org-table-wrap-region
  3347. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3348. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3349. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3350. (defun org-at-table-p (&optional table-type)
  3351. "Return t if the cursor is inside an org-type table.
  3352. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3353. (if org-enable-table-editor
  3354. (save-excursion
  3355. (beginning-of-line 1)
  3356. (looking-at (if table-type org-table-any-line-regexp
  3357. org-table-line-regexp)))
  3358. nil))
  3359. (defsubst org-table-p () (org-at-table-p))
  3360. (defun org-at-table.el-p ()
  3361. "Return t if and only if we are at a table.el table."
  3362. (and (org-at-table-p 'any)
  3363. (save-excursion
  3364. (goto-char (org-table-begin 'any))
  3365. (looking-at org-table1-hline-regexp))))
  3366. (defun org-table-recognize-table.el ()
  3367. "If there is a table.el table nearby, recognize it and move into it."
  3368. (if org-table-tab-recognizes-table.el
  3369. (if (org-at-table.el-p)
  3370. (progn
  3371. (beginning-of-line 1)
  3372. (if (looking-at org-table-dataline-regexp)
  3373. nil
  3374. (if (looking-at org-table1-hline-regexp)
  3375. (progn
  3376. (beginning-of-line 2)
  3377. (if (looking-at org-table-any-border-regexp)
  3378. (beginning-of-line -1)))))
  3379. (if (re-search-forward "|" (org-table-end t) t)
  3380. (progn
  3381. (require 'table)
  3382. (if (table--at-cell-p (point))
  3383. t
  3384. (message "recognizing table.el table...")
  3385. (table-recognize-table)
  3386. (message "recognizing table.el table...done")))
  3387. (error "This should not happen"))
  3388. t)
  3389. nil)
  3390. nil))
  3391. (defun org-at-table-hline-p ()
  3392. "Return t if the cursor is inside a hline in a table."
  3393. (if org-enable-table-editor
  3394. (save-excursion
  3395. (beginning-of-line 1)
  3396. (looking-at org-table-hline-regexp))
  3397. nil))
  3398. (defvar org-table-clean-did-remove-column nil)
  3399. (defun org-table-map-tables (function &optional quietly)
  3400. "Apply FUNCTION to the start of all tables in the buffer."
  3401. (save-excursion
  3402. (save-restriction
  3403. (widen)
  3404. (goto-char (point-min))
  3405. (while (re-search-forward org-table-any-line-regexp nil t)
  3406. (unless quietly
  3407. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3408. (beginning-of-line 1)
  3409. (when (looking-at org-table-line-regexp)
  3410. (save-excursion (funcall function))
  3411. (or (looking-at org-table-line-regexp)
  3412. (forward-char 1)))
  3413. (re-search-forward org-table-any-border-regexp nil 1))))
  3414. (unless quietly (message "Mapping tables: done")))
  3415. ;; Declare and autoload functions from org-exp.el & Co
  3416. (declare-function org-default-export-plist "org-exp")
  3417. (declare-function org-infile-export-plist "org-exp")
  3418. (declare-function org-get-current-options "org-exp")
  3419. (eval-and-compile
  3420. (org-autoload "org-exp"
  3421. '(org-export org-export-visible
  3422. org-insert-export-options-template
  3423. org-table-clean-before-export))
  3424. (org-autoload "org-ascii"
  3425. '(org-export-as-ascii org-export-ascii-preprocess
  3426. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3427. org-export-region-as-ascii))
  3428. (org-autoload "org-latex"
  3429. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3430. org-replace-region-by-latex org-export-region-as-latex
  3431. org-export-as-latex org-export-as-pdf
  3432. org-export-as-pdf-and-open))
  3433. (org-autoload "org-html"
  3434. '(org-export-as-html-and-open
  3435. org-export-as-html-batch org-export-as-html-to-buffer
  3436. org-replace-region-by-html org-export-region-as-html
  3437. org-export-as-html))
  3438. (org-autoload "org-docbook"
  3439. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3440. org-replace-region-by-docbook org-export-region-as-docbook
  3441. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3442. org-export-as-docbook))
  3443. (org-autoload "org-icalendar"
  3444. '(org-export-icalendar-this-file
  3445. org-export-icalendar-all-agenda-files
  3446. org-export-icalendar-combine-agenda-files))
  3447. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3448. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3449. ;; Declare and autoload functions from org-agenda.el
  3450. (eval-and-compile
  3451. (org-autoload "org-agenda"
  3452. '(org-agenda org-agenda-list org-search-view
  3453. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3454. org-diary org-agenda-to-appt
  3455. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3456. ;; Autoload org-remember
  3457. (eval-and-compile
  3458. (org-autoload "org-remember"
  3459. '(org-remember-insinuate org-remember-annotation
  3460. org-remember-apply-template org-remember org-remember-handler)))
  3461. (eval-and-compile
  3462. (org-autoload "org-capture"
  3463. '(org-capture org-capture-insert-template-here
  3464. org-capture-import-remember-templates)))
  3465. ;; Autoload org-clock.el
  3466. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3467. (beg end))
  3468. (declare-function org-clock-update-mode-line "org-clock" ())
  3469. (declare-function org-resolve-clocks "org-clock"
  3470. (&optional also-non-dangling-p prompt last-valid))
  3471. (defvar org-clock-start-time)
  3472. (defvar org-clock-marker (make-marker)
  3473. "Marker recording the last clock-in.")
  3474. (defvar org-clock-hd-marker (make-marker)
  3475. "Marker recording the last clock-in, but the headline position.")
  3476. (defvar org-clock-heading ""
  3477. "The heading of the current clock entry.")
  3478. (defun org-clock-is-active ()
  3479. "Return non-nil if clock is currently running.
  3480. The return value is actually the clock marker."
  3481. (marker-buffer org-clock-marker))
  3482. (eval-and-compile
  3483. (org-autoload
  3484. "org-clock"
  3485. '(org-clock-in org-clock-out org-clock-cancel
  3486. org-clock-goto org-clock-sum org-clock-display
  3487. org-clock-remove-overlays org-clock-report
  3488. org-clocktable-shift org-dblock-write:clocktable
  3489. org-get-clocktable org-resolve-clocks)))
  3490. (defun org-clock-update-time-maybe ()
  3491. "If this is a CLOCK line, update it and return t.
  3492. Otherwise, return nil."
  3493. (interactive)
  3494. (save-excursion
  3495. (beginning-of-line 1)
  3496. (skip-chars-forward " \t")
  3497. (when (looking-at org-clock-string)
  3498. (let ((re (concat "[ \t]*" org-clock-string
  3499. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3500. "\\([ \t]*=>.*\\)?\\)?"))
  3501. ts te h m s neg)
  3502. (cond
  3503. ((not (looking-at re))
  3504. nil)
  3505. ((not (match-end 2))
  3506. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3507. (> org-clock-marker (point))
  3508. (<= org-clock-marker (point-at-eol)))
  3509. ;; The clock is running here
  3510. (setq org-clock-start-time
  3511. (apply 'encode-time
  3512. (org-parse-time-string (match-string 1))))
  3513. (org-clock-update-mode-line)))
  3514. (t
  3515. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3516. (end-of-line 1)
  3517. (setq ts (match-string 1)
  3518. te (match-string 3))
  3519. (setq s (- (org-float-time
  3520. (apply 'encode-time (org-parse-time-string te)))
  3521. (org-float-time
  3522. (apply 'encode-time (org-parse-time-string ts))))
  3523. neg (< s 0)
  3524. s (abs s)
  3525. h (floor (/ s 3600))
  3526. s (- s (* 3600 h))
  3527. m (floor (/ s 60))
  3528. s (- s (* 60 s)))
  3529. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3530. t))))))
  3531. (defun org-check-running-clock ()
  3532. "Check if the current buffer contains the running clock.
  3533. If yes, offer to stop it and to save the buffer with the changes."
  3534. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3535. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3536. (buffer-name))))
  3537. (org-clock-out)
  3538. (when (y-or-n-p "Save changed buffer?")
  3539. (save-buffer))))
  3540. (defun org-clocktable-try-shift (dir n)
  3541. "Check if this line starts a clock table, if yes, shift the time block."
  3542. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3543. (org-clocktable-shift dir n)))
  3544. ;; Autoload org-timer.el
  3545. (eval-and-compile
  3546. (org-autoload
  3547. "org-timer"
  3548. '(org-timer-start org-timer org-timer-item
  3549. org-timer-change-times-in-region
  3550. org-timer-set-timer
  3551. org-timer-reset-timers
  3552. org-timer-show-remaining-time)))
  3553. ;; Autoload org-feed.el
  3554. (eval-and-compile
  3555. (org-autoload
  3556. "org-feed"
  3557. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3558. ;; Autoload org-indent.el
  3559. ;; Define the variable already here, to make sure we have it.
  3560. (defvar org-indent-mode nil
  3561. "Non-nil if Org-Indent mode is enabled.
  3562. Use the command `org-indent-mode' to change this variable.")
  3563. (eval-and-compile
  3564. (org-autoload
  3565. "org-indent"
  3566. '(org-indent-mode)))
  3567. ;; Autoload org-mobile.el
  3568. (eval-and-compile
  3569. (org-autoload
  3570. "org-mobile"
  3571. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3572. ;; Autoload archiving code
  3573. ;; The stuff that is needed for cycling and tags has to be defined here.
  3574. (defgroup org-archive nil
  3575. "Options concerning archiving in Org-mode."
  3576. :tag "Org Archive"
  3577. :group 'org-structure)
  3578. (defcustom org-archive-location "%s_archive::"
  3579. "The location where subtrees should be archived.
  3580. The value of this variable is a string, consisting of two parts,
  3581. separated by a double-colon. The first part is a filename and
  3582. the second part is a headline.
  3583. When the filename is omitted, archiving happens in the same file.
  3584. %s in the filename will be replaced by the current file
  3585. name (without the directory part). Archiving to a different file
  3586. is useful to keep archived entries from contributing to the
  3587. Org-mode Agenda.
  3588. The archived entries will be filed as subtrees of the specified
  3589. headline. When the headline is omitted, the subtrees are simply
  3590. filed away at the end of the file, as top-level entries. Also in
  3591. the heading you can use %s to represent the file name, this can be
  3592. useful when using the same archive for a number of different files.
  3593. Here are a few examples:
  3594. \"%s_archive::\"
  3595. If the current file is Projects.org, archive in file
  3596. Projects.org_archive, as top-level trees. This is the default.
  3597. \"::* Archived Tasks\"
  3598. Archive in the current file, under the top-level headline
  3599. \"* Archived Tasks\".
  3600. \"~/org/archive.org::\"
  3601. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3602. \"~/org/archive.org::* From %s\"
  3603. Archive in file ~/org/archive.org (absolute path), under headlines
  3604. \"From FILENAME\" where file name is the current file name.
  3605. \"basement::** Finished Tasks\"
  3606. Archive in file ./basement (relative path), as level 3 trees
  3607. below the level 2 heading \"** Finished Tasks\".
  3608. You may set this option on a per-file basis by adding to the buffer a
  3609. line like
  3610. #+ARCHIVE: basement::** Finished Tasks
  3611. You may also define it locally for a subtree by setting an ARCHIVE property
  3612. in the entry. If such a property is found in an entry, or anywhere up
  3613. the hierarchy, it will be used."
  3614. :group 'org-archive
  3615. :type 'string)
  3616. (defcustom org-archive-tag "ARCHIVE"
  3617. "The tag that marks a subtree as archived.
  3618. An archived subtree does not open during visibility cycling, and does
  3619. not contribute to the agenda listings.
  3620. After changing this, font-lock must be restarted in the relevant buffers to
  3621. get the proper fontification."
  3622. :group 'org-archive
  3623. :group 'org-keywords
  3624. :type 'string)
  3625. (defcustom org-agenda-skip-archived-trees t
  3626. "Non-nil means the agenda will skip any items located in archived trees.
  3627. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3628. variable is no longer recommended, you should leave it at the value t.
  3629. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3630. :group 'org-archive
  3631. :group 'org-agenda-skip
  3632. :type 'boolean)
  3633. (defcustom org-columns-skip-archived-trees t
  3634. "Non-nil means ignore archived trees when creating column view."
  3635. :group 'org-archive
  3636. :group 'org-properties
  3637. :type 'boolean)
  3638. (defcustom org-cycle-open-archived-trees nil
  3639. "Non-nil means `org-cycle' will open archived trees.
  3640. An archived tree is a tree marked with the tag ARCHIVE.
  3641. When nil, archived trees will stay folded. You can still open them with
  3642. normal outline commands like `show-all', but not with the cycling commands."
  3643. :group 'org-archive
  3644. :group 'org-cycle
  3645. :type 'boolean)
  3646. (defcustom org-sparse-tree-open-archived-trees nil
  3647. "Non-nil means sparse tree construction shows matches in archived trees.
  3648. When nil, matches in these trees are highlighted, but the trees are kept in
  3649. collapsed state."
  3650. :group 'org-archive
  3651. :group 'org-sparse-trees
  3652. :type 'boolean)
  3653. (defun org-cycle-hide-archived-subtrees (state)
  3654. "Re-hide all archived subtrees after a visibility state change."
  3655. (when (and (not org-cycle-open-archived-trees)
  3656. (not (memq state '(overview folded))))
  3657. (save-excursion
  3658. (let* ((globalp (memq state '(contents all)))
  3659. (beg (if globalp (point-min) (point)))
  3660. (end (if globalp (point-max) (org-end-of-subtree t))))
  3661. (org-hide-archived-subtrees beg end)
  3662. (goto-char beg)
  3663. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3664. (message "%s" (substitute-command-keys
  3665. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3666. (defun org-force-cycle-archived ()
  3667. "Cycle subtree even if it is archived."
  3668. (interactive)
  3669. (setq this-command 'org-cycle)
  3670. (let ((org-cycle-open-archived-trees t))
  3671. (call-interactively 'org-cycle)))
  3672. (defun org-hide-archived-subtrees (beg end)
  3673. "Re-hide all archived subtrees after a visibility state change."
  3674. (save-excursion
  3675. (let* ((re (concat ":" org-archive-tag ":")))
  3676. (goto-char beg)
  3677. (while (re-search-forward re end t)
  3678. (when (org-on-heading-p)
  3679. (org-flag-subtree t)
  3680. (org-end-of-subtree t))))))
  3681. (defun org-flag-subtree (flag)
  3682. (save-excursion
  3683. (org-back-to-heading t)
  3684. (outline-end-of-heading)
  3685. (outline-flag-region (point)
  3686. (progn (org-end-of-subtree t) (point))
  3687. flag)))
  3688. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3689. (eval-and-compile
  3690. (org-autoload "org-archive"
  3691. '(org-add-archive-files org-archive-subtree
  3692. org-archive-to-archive-sibling org-toggle-archive-tag
  3693. org-archive-subtree-default
  3694. org-archive-subtree-default-with-confirmation)))
  3695. ;; Autoload Column View Code
  3696. (declare-function org-columns-number-to-string "org-colview")
  3697. (declare-function org-columns-get-format-and-top-level "org-colview")
  3698. (declare-function org-columns-compute "org-colview")
  3699. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3700. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3701. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3702. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3703. ;; Autoload ID code
  3704. (declare-function org-id-store-link "org-id")
  3705. (declare-function org-id-locations-load "org-id")
  3706. (declare-function org-id-locations-save "org-id")
  3707. (defvar org-id-track-globally)
  3708. (org-autoload "org-id"
  3709. '(org-id-get-create org-id-new org-id-copy org-id-get
  3710. org-id-get-with-outline-path-completion
  3711. org-id-get-with-outline-drilling org-id-store-link
  3712. org-id-goto org-id-find org-id-store-link))
  3713. ;; Autoload Plotting Code
  3714. (org-autoload "org-plot"
  3715. '(org-plot/gnuplot))
  3716. ;;; Variables for pre-computed regular expressions, all buffer local
  3717. (defvar org-drawer-regexp nil
  3718. "Matches first line of a hidden block.")
  3719. (make-variable-buffer-local 'org-drawer-regexp)
  3720. (defvar org-todo-regexp nil
  3721. "Matches any of the TODO state keywords.")
  3722. (make-variable-buffer-local 'org-todo-regexp)
  3723. (defvar org-not-done-regexp nil
  3724. "Matches any of the TODO state keywords except the last one.")
  3725. (make-variable-buffer-local 'org-not-done-regexp)
  3726. (defvar org-not-done-heading-regexp nil
  3727. "Matches a TODO headline that is not done.")
  3728. (make-variable-buffer-local 'org-not-done-regexp)
  3729. (defvar org-todo-line-regexp nil
  3730. "Matches a headline and puts TODO state into group 2 if present.")
  3731. (make-variable-buffer-local 'org-todo-line-regexp)
  3732. (defvar org-complex-heading-regexp nil
  3733. "Matches a headline and puts everything into groups:
  3734. group 1: the stars
  3735. group 2: The todo keyword, maybe
  3736. group 3: Priority cookie
  3737. group 4: True headline
  3738. group 5: Tags")
  3739. (make-variable-buffer-local 'org-complex-heading-regexp)
  3740. (defvar org-complex-heading-regexp-format nil
  3741. "Printf format to make regexp to match an exact headline.
  3742. This regexp will match the headline of any node which has the
  3743. exact headline text that is put into the format, but may have any
  3744. TODO state, priority and tags.")
  3745. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3746. (defvar org-todo-line-tags-regexp nil
  3747. "Matches a headline and puts TODO state into group 2 if present.
  3748. Also put tags into group 4 if tags are present.")
  3749. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3750. (defvar org-ds-keyword-length 12
  3751. "Maximum length of the Deadline and SCHEDULED keywords.")
  3752. (make-variable-buffer-local 'org-ds-keyword-length)
  3753. (defvar org-deadline-regexp nil
  3754. "Matches the DEADLINE keyword.")
  3755. (make-variable-buffer-local 'org-deadline-regexp)
  3756. (defvar org-deadline-time-regexp nil
  3757. "Matches the DEADLINE keyword together with a time stamp.")
  3758. (make-variable-buffer-local 'org-deadline-time-regexp)
  3759. (defvar org-deadline-line-regexp nil
  3760. "Matches the DEADLINE keyword and the rest of the line.")
  3761. (make-variable-buffer-local 'org-deadline-line-regexp)
  3762. (defvar org-scheduled-regexp nil
  3763. "Matches the SCHEDULED keyword.")
  3764. (make-variable-buffer-local 'org-scheduled-regexp)
  3765. (defvar org-scheduled-time-regexp nil
  3766. "Matches the SCHEDULED keyword together with a time stamp.")
  3767. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3768. (defvar org-closed-time-regexp nil
  3769. "Matches the CLOSED keyword together with a time stamp.")
  3770. (make-variable-buffer-local 'org-closed-time-regexp)
  3771. (defvar org-keyword-time-regexp nil
  3772. "Matches any of the 4 keywords, together with the time stamp.")
  3773. (make-variable-buffer-local 'org-keyword-time-regexp)
  3774. (defvar org-keyword-time-not-clock-regexp nil
  3775. "Matches any of the 3 keywords, together with the time stamp.")
  3776. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3777. (defvar org-maybe-keyword-time-regexp nil
  3778. "Matches a timestamp, possibly preceded by a keyword.")
  3779. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3780. (defvar org-planning-or-clock-line-re nil
  3781. "Matches a line with planning or clock info.")
  3782. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3783. (defvar org-all-time-keywords nil
  3784. "List of time keywords.")
  3785. (make-variable-buffer-local 'org-all-time-keywords)
  3786. (defconst org-plain-time-of-day-regexp
  3787. (concat
  3788. "\\(\\<[012]?[0-9]"
  3789. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3790. "\\(--?"
  3791. "\\(\\<[012]?[0-9]"
  3792. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3793. "\\)?")
  3794. "Regular expression to match a plain time or time range.
  3795. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3796. groups carry important information:
  3797. 0 the full match
  3798. 1 the first time, range or not
  3799. 8 the second time, if it is a range.")
  3800. (defconst org-plain-time-extension-regexp
  3801. (concat
  3802. "\\(\\<[012]?[0-9]"
  3803. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3804. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3805. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3806. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3807. groups carry important information:
  3808. 0 the full match
  3809. 7 hours of duration
  3810. 9 minutes of duration")
  3811. (defconst org-stamp-time-of-day-regexp
  3812. (concat
  3813. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3814. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3815. "\\(--?"
  3816. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3817. "Regular expression to match a timestamp time or time range.
  3818. After a match, the following groups carry important information:
  3819. 0 the full match
  3820. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3821. 2 the first time, range or not
  3822. 4 the second time, if it is a range.")
  3823. (defconst org-startup-options
  3824. '(("fold" org-startup-folded t)
  3825. ("overview" org-startup-folded t)
  3826. ("nofold" org-startup-folded nil)
  3827. ("showall" org-startup-folded nil)
  3828. ("showeverything" org-startup-folded showeverything)
  3829. ("content" org-startup-folded content)
  3830. ("indent" org-startup-indented t)
  3831. ("noindent" org-startup-indented nil)
  3832. ("hidestars" org-hide-leading-stars t)
  3833. ("showstars" org-hide-leading-stars nil)
  3834. ("odd" org-odd-levels-only t)
  3835. ("oddeven" org-odd-levels-only nil)
  3836. ("align" org-startup-align-all-tables t)
  3837. ("noalign" org-startup-align-all-tables nil)
  3838. ("inlineimages" org-startup-with-inline-images t)
  3839. ("noinlineimages" org-startup-with-inline-images nil)
  3840. ("customtime" org-display-custom-times t)
  3841. ("logdone" org-log-done time)
  3842. ("lognotedone" org-log-done note)
  3843. ("nologdone" org-log-done nil)
  3844. ("lognoteclock-out" org-log-note-clock-out t)
  3845. ("nolognoteclock-out" org-log-note-clock-out nil)
  3846. ("logrepeat" org-log-repeat state)
  3847. ("lognoterepeat" org-log-repeat note)
  3848. ("nologrepeat" org-log-repeat nil)
  3849. ("logreschedule" org-log-reschedule time)
  3850. ("lognotereschedule" org-log-reschedule note)
  3851. ("nologreschedule" org-log-reschedule nil)
  3852. ("logredeadline" org-log-redeadline time)
  3853. ("lognoteredeadline" org-log-redeadline note)
  3854. ("nologredeadline" org-log-redeadline nil)
  3855. ("logrefile" org-log-refile time)
  3856. ("lognoterefile" org-log-refile note)
  3857. ("nologrefile" org-log-refile nil)
  3858. ("fninline" org-footnote-define-inline t)
  3859. ("nofninline" org-footnote-define-inline nil)
  3860. ("fnlocal" org-footnote-section nil)
  3861. ("fnauto" org-footnote-auto-label t)
  3862. ("fnprompt" org-footnote-auto-label nil)
  3863. ("fnconfirm" org-footnote-auto-label confirm)
  3864. ("fnplain" org-footnote-auto-label plain)
  3865. ("fnadjust" org-footnote-auto-adjust t)
  3866. ("nofnadjust" org-footnote-auto-adjust nil)
  3867. ("constcgs" constants-unit-system cgs)
  3868. ("constSI" constants-unit-system SI)
  3869. ("noptag" org-tag-persistent-alist nil)
  3870. ("hideblocks" org-hide-block-startup t)
  3871. ("nohideblocks" org-hide-block-startup nil)
  3872. ("beamer" org-startup-with-beamer-mode t)
  3873. ("entitiespretty" org-pretty-entities t)
  3874. ("entitiesplain" org-pretty-entities nil))
  3875. "Variable associated with STARTUP options for org-mode.
  3876. Each element is a list of three items: The startup options as written
  3877. in the #+STARTUP line, the corresponding variable, and the value to
  3878. set this variable to if the option is found. An optional forth element PUSH
  3879. means to push this value onto the list in the variable.")
  3880. (defun org-update-property-plist (key val props)
  3881. "Update PROPS with KEY and VAL."
  3882. (if (string= "+" (substring key (- (length key) 1)))
  3883. (let* ((key (substring key 0 (- (length key) 1)))
  3884. (previous (cdr (assoc key props))))
  3885. (cons (cons key (concat previous " " val))
  3886. (org-remove-if (lambda (p) (string= (car p) key)) props)))
  3887. (cons (cons key val) props)))
  3888. (defconst org-block-regexp
  3889. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3890. "Regular expression for hiding blocks.")
  3891. (defconst org-heading-keyword-regexp-format
  3892. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3893. "Printf format for a regexp matching an headline with some keyword.
  3894. This regexp will match the headline of any node which has the
  3895. exact keyword that is put into the format. The keyword isn't in
  3896. any group by default, but the stars and the body are.")
  3897. (defconst org-heading-keyword-maybe-regexp-format
  3898. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3899. "Printf format for a regexp matching an headline, possibly with some keyword.
  3900. This regexp can match any headline with the specified keyword, or
  3901. without a keyword. The keyword isn't in any group by default,
  3902. but the stars and the body are.")
  3903. (defun org-set-regexps-and-options ()
  3904. "Precompute regular expressions for current buffer."
  3905. (when (eq major-mode 'org-mode)
  3906. (org-set-local 'org-todo-kwd-alist nil)
  3907. (org-set-local 'org-todo-key-alist nil)
  3908. (org-set-local 'org-todo-key-trigger nil)
  3909. (org-set-local 'org-todo-keywords-1 nil)
  3910. (org-set-local 'org-done-keywords nil)
  3911. (org-set-local 'org-todo-heads nil)
  3912. (org-set-local 'org-todo-sets nil)
  3913. (org-set-local 'org-todo-log-states nil)
  3914. (org-set-local 'org-file-properties nil)
  3915. (org-set-local 'org-file-tags nil)
  3916. (let ((re (org-make-options-regexp
  3917. '("CATEGORY" "TODO" "COLUMNS"
  3918. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3919. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3920. "OPTIONS")
  3921. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3922. (splitre "[ \t]+")
  3923. (scripts org-use-sub-superscripts)
  3924. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3925. tail sep kws1 prio props ftags drawers beamer-p
  3926. ext-setup-or-nil setup-contents (start 0))
  3927. (save-excursion
  3928. (save-restriction
  3929. (widen)
  3930. (goto-char (point-min))
  3931. (while (or (and ext-setup-or-nil
  3932. (string-match re ext-setup-or-nil start)
  3933. (setq start (match-end 0)))
  3934. (and (setq ext-setup-or-nil nil start 0)
  3935. (re-search-forward re nil t)))
  3936. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3937. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3938. (if (stringp value) (setq value (org-trim value)))
  3939. (cond
  3940. ((equal key "CATEGORY")
  3941. (setq cat value))
  3942. ((member key '("SEQ_TODO" "TODO"))
  3943. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3944. ((equal key "TYP_TODO")
  3945. (push (cons 'type (org-split-string value splitre)) kwds))
  3946. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3947. ;; general TODO-like setup
  3948. (push (cons (intern (downcase (match-string 1 key)))
  3949. (org-split-string value splitre)) kwds))
  3950. ((equal key "TAGS")
  3951. (setq tags (append tags (if tags '("\\n") nil)
  3952. (org-split-string value splitre))))
  3953. ((equal key "COLUMNS")
  3954. (org-set-local 'org-columns-default-format value))
  3955. ((equal key "LINK")
  3956. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3957. (push (cons (match-string 1 value)
  3958. (org-trim (match-string 2 value)))
  3959. links)))
  3960. ((equal key "PRIORITIES")
  3961. (setq prio (org-split-string value " +")))
  3962. ((equal key "PROPERTY")
  3963. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3964. (setq props (org-update-property-plist (match-string 1 value)
  3965. (match-string 2 value)
  3966. props))))
  3967. ((equal key "FILETAGS")
  3968. (when (string-match "\\S-" value)
  3969. (setq ftags
  3970. (append
  3971. ftags
  3972. (apply 'append
  3973. (mapcar (lambda (x) (org-split-string x ":"))
  3974. (org-split-string value)))))))
  3975. ((equal key "DRAWERS")
  3976. (setq drawers (org-split-string value splitre)))
  3977. ((equal key "CONSTANTS")
  3978. (setq const (append const (org-split-string value splitre))))
  3979. ((equal key "STARTUP")
  3980. (let ((opts (org-split-string value splitre))
  3981. l var val)
  3982. (while (setq l (pop opts))
  3983. (when (setq l (assoc l org-startup-options))
  3984. (setq var (nth 1 l) val (nth 2 l))
  3985. (if (not (nth 3 l))
  3986. (set (make-local-variable var) val)
  3987. (if (not (listp (symbol-value var)))
  3988. (set (make-local-variable var) nil))
  3989. (set (make-local-variable var) (symbol-value var))
  3990. (add-to-list var val))))))
  3991. ((equal key "ARCHIVE")
  3992. (setq arch value)
  3993. (remove-text-properties 0 (length arch)
  3994. '(face t fontified t) arch))
  3995. ((equal key "LATEX_CLASS")
  3996. (setq beamer-p (equal value "beamer")))
  3997. ((equal key "OPTIONS")
  3998. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  3999. (setq scripts (read (match-string 2 value)))))
  4000. ((equal key "SETUPFILE")
  4001. (setq setup-contents (org-file-contents
  4002. (expand-file-name
  4003. (org-remove-double-quotes value))
  4004. 'noerror))
  4005. (if (not ext-setup-or-nil)
  4006. (setq ext-setup-or-nil setup-contents start 0)
  4007. (setq ext-setup-or-nil
  4008. (concat (substring ext-setup-or-nil 0 start)
  4009. "\n" setup-contents "\n"
  4010. (substring ext-setup-or-nil start)))))))
  4011. ;; search for property blocks
  4012. (goto-char (point-min))
  4013. (while (re-search-forward org-block-regexp nil t)
  4014. (when (equal "PROPERTY" (upcase (match-string 1)))
  4015. (setq value (replace-regexp-in-string
  4016. "[\n\r]" " " (match-string 4)))
  4017. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4018. (setq props (org-update-property-plist (match-string 1 value)
  4019. (match-string 2 value)
  4020. props)))))))
  4021. (org-set-local 'org-use-sub-superscripts scripts)
  4022. (when cat
  4023. (org-set-local 'org-category (intern cat))
  4024. (push (cons "CATEGORY" cat) props))
  4025. (when prio
  4026. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4027. (setq prio (mapcar 'string-to-char prio))
  4028. (org-set-local 'org-highest-priority (nth 0 prio))
  4029. (org-set-local 'org-lowest-priority (nth 1 prio))
  4030. (org-set-local 'org-default-priority (nth 2 prio)))
  4031. (and props (org-set-local 'org-file-properties (nreverse props)))
  4032. (and ftags (org-set-local 'org-file-tags
  4033. (mapcar 'org-add-prop-inherited ftags)))
  4034. (and drawers (org-set-local 'org-drawers drawers))
  4035. (and arch (org-set-local 'org-archive-location arch))
  4036. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4037. ;; Process the TODO keywords
  4038. (unless kwds
  4039. ;; Use the global values as if they had been given locally.
  4040. (setq kwds (default-value 'org-todo-keywords))
  4041. (if (stringp (car kwds))
  4042. (setq kwds (list (cons org-todo-interpretation
  4043. (default-value 'org-todo-keywords)))))
  4044. (setq kwds (reverse kwds)))
  4045. (setq kwds (nreverse kwds))
  4046. (let (inter kws kw)
  4047. (while (setq kws (pop kwds))
  4048. (let ((kws (or
  4049. (run-hook-with-args-until-success
  4050. 'org-todo-setup-filter-hook kws)
  4051. kws)))
  4052. (setq inter (pop kws) sep (member "|" kws)
  4053. kws0 (delete "|" (copy-sequence kws))
  4054. kwsa nil
  4055. kws1 (mapcar
  4056. (lambda (x)
  4057. ;; 1 2
  4058. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4059. (progn
  4060. (setq kw (match-string 1 x)
  4061. key (and (match-end 2) (match-string 2 x))
  4062. log (org-extract-log-state-settings x))
  4063. (push (cons kw (and key (string-to-char key))) kwsa)
  4064. (and log (push log org-todo-log-states))
  4065. kw)
  4066. (error "Invalid TODO keyword %s" x)))
  4067. kws0)
  4068. kwsa (if kwsa (append '((:startgroup))
  4069. (nreverse kwsa)
  4070. '((:endgroup))))
  4071. hw (car kws1)
  4072. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4073. tail (list inter hw (car dws) (org-last dws))))
  4074. (add-to-list 'org-todo-heads hw 'append)
  4075. (push kws1 org-todo-sets)
  4076. (setq org-done-keywords (append org-done-keywords dws nil))
  4077. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4078. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4079. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4080. (setq org-todo-sets (nreverse org-todo-sets)
  4081. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4082. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4083. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4084. ;; Process the constants
  4085. (when const
  4086. (let (e cst)
  4087. (while (setq e (pop const))
  4088. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4089. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4090. (setq org-table-formula-constants-local cst)))
  4091. ;; Process the tags.
  4092. (when tags
  4093. (let (e tgs)
  4094. (while (setq e (pop tags))
  4095. (cond
  4096. ((equal e "{") (push '(:startgroup) tgs))
  4097. ((equal e "}") (push '(:endgroup) tgs))
  4098. ((equal e "\\n") (push '(:newline) tgs))
  4099. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4100. (push (cons (match-string 1 e)
  4101. (string-to-char (match-string 2 e)))
  4102. tgs))
  4103. (t (push (list e) tgs))))
  4104. (org-set-local 'org-tag-alist nil)
  4105. (while (setq e (pop tgs))
  4106. (or (and (stringp (car e))
  4107. (assoc (car e) org-tag-alist))
  4108. (push e org-tag-alist)))))
  4109. ;; Compute the regular expressions and other local variables.
  4110. ;; Using `org-outline-regexp-bol' would complicate them much,
  4111. ;; because of the fixed white space at the end of that string.
  4112. (if (not org-done-keywords)
  4113. (setq org-done-keywords (and org-todo-keywords-1
  4114. (list (org-last org-todo-keywords-1)))))
  4115. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4116. (length org-scheduled-string)
  4117. (length org-clock-string)
  4118. (length org-closed-string)))
  4119. org-drawer-regexp
  4120. (concat "^[ \t]*:\\("
  4121. (mapconcat 'regexp-quote org-drawers "\\|")
  4122. "\\):[ \t]*$")
  4123. org-not-done-keywords
  4124. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4125. org-todo-regexp
  4126. (concat "\\("
  4127. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4128. "\\)")
  4129. org-not-done-regexp
  4130. (concat "\\("
  4131. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4132. "\\)")
  4133. org-not-done-heading-regexp
  4134. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4135. org-todo-line-regexp
  4136. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4137. org-complex-heading-regexp
  4138. (concat "^\\(\\*+\\)"
  4139. "\\(?: +" org-todo-regexp "\\)?"
  4140. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4141. "\\(?: +\\(.*?\\)\\)?"
  4142. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4143. "[ \t]*$")
  4144. org-complex-heading-regexp-format
  4145. (concat "^\\(\\*+\\)"
  4146. "\\(?: +" org-todo-regexp "\\)?"
  4147. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4148. "\\(?: +"
  4149. ;; Stats cookies can be sticked to body.
  4150. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4151. "\\(%s\\)"
  4152. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4153. "\\)"
  4154. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4155. "[ \t]*$")
  4156. org-todo-line-tags-regexp
  4157. (concat "^\\(\\*+\\)"
  4158. "\\(?: +" org-todo-regexp "\\)?"
  4159. "\\(?: +\\(.*?\\)\\)?"
  4160. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4161. "[ \t]*$")
  4162. org-deadline-regexp (concat "\\<" org-deadline-string)
  4163. org-deadline-time-regexp
  4164. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4165. org-deadline-line-regexp
  4166. (concat "\\<\\(" org-deadline-string "\\).*")
  4167. org-scheduled-regexp
  4168. (concat "\\<" org-scheduled-string)
  4169. org-scheduled-time-regexp
  4170. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4171. org-closed-time-regexp
  4172. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4173. org-keyword-time-regexp
  4174. (concat "\\<\\(" org-scheduled-string
  4175. "\\|" org-deadline-string
  4176. "\\|" org-closed-string
  4177. "\\|" org-clock-string "\\)"
  4178. " *[[<]\\([^]>]+\\)[]>]")
  4179. org-keyword-time-not-clock-regexp
  4180. (concat "\\<\\(" org-scheduled-string
  4181. "\\|" org-deadline-string
  4182. "\\|" org-closed-string
  4183. "\\)"
  4184. " *[[<]\\([^]>]+\\)[]>]")
  4185. org-maybe-keyword-time-regexp
  4186. (concat "\\(\\<\\(" org-scheduled-string
  4187. "\\|" org-deadline-string
  4188. "\\|" org-closed-string
  4189. "\\|" org-clock-string "\\)\\)?"
  4190. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4191. org-planning-or-clock-line-re
  4192. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4193. "\\|" org-deadline-string
  4194. "\\|" org-closed-string "\\|" org-clock-string
  4195. "\\)\\>\\)")
  4196. org-all-time-keywords
  4197. (mapcar (lambda (w) (substring w 0 -1))
  4198. (list org-scheduled-string org-deadline-string
  4199. org-clock-string org-closed-string))
  4200. )
  4201. (org-compute-latex-and-specials-regexp)
  4202. (org-set-font-lock-defaults))))
  4203. (defun org-file-contents (file &optional noerror)
  4204. "Return the contents of FILE, as a string."
  4205. (if (or (not file)
  4206. (not (file-readable-p file)))
  4207. (if noerror
  4208. (progn
  4209. (message "Cannot read file \"%s\"" file)
  4210. (ding) (sit-for 2)
  4211. "")
  4212. (error "Cannot read file \"%s\"" file))
  4213. (with-temp-buffer
  4214. (insert-file-contents file)
  4215. (buffer-string))))
  4216. (defun org-extract-log-state-settings (x)
  4217. "Extract the log state setting from a TODO keyword string.
  4218. This will extract info from a string like \"WAIT(w@/!)\"."
  4219. (let (kw key log1 log2)
  4220. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4221. (setq kw (match-string 1 x)
  4222. key (and (match-end 2) (match-string 2 x))
  4223. log1 (and (match-end 3) (match-string 3 x))
  4224. log2 (and (match-end 4) (match-string 4 x)))
  4225. (and (or log1 log2)
  4226. (list kw
  4227. (and log1 (if (equal log1 "!") 'time 'note))
  4228. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4229. (defun org-remove-keyword-keys (list)
  4230. "Remove a pair of parenthesis at the end of each string in LIST."
  4231. (mapcar (lambda (x)
  4232. (if (string-match "(.*)$" x)
  4233. (substring x 0 (match-beginning 0))
  4234. x))
  4235. list))
  4236. (defun org-assign-fast-keys (alist)
  4237. "Assign fast keys to a keyword-key alist.
  4238. Respect keys that are already there."
  4239. (let (new e (alt ?0))
  4240. (while (setq e (pop alist))
  4241. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4242. (cdr e)) ;; Key already assigned.
  4243. (push e new)
  4244. (let ((clist (string-to-list (downcase (car e))))
  4245. (used (append new alist)))
  4246. (when (= (car clist) ?@)
  4247. (pop clist))
  4248. (while (and clist (rassoc (car clist) used))
  4249. (pop clist))
  4250. (unless clist
  4251. (while (rassoc alt used)
  4252. (incf alt)))
  4253. (push (cons (car e) (or (car clist) alt)) new))))
  4254. (nreverse new)))
  4255. ;;; Some variables used in various places
  4256. (defvar org-window-configuration nil
  4257. "Used in various places to store a window configuration.")
  4258. (defvar org-selected-window nil
  4259. "Used in various places to store a window configuration.")
  4260. (defvar org-finish-function nil
  4261. "Function to be called when `C-c C-c' is used.
  4262. This is for getting out of special buffers like remember.")
  4263. ;; FIXME: Occasionally check by commenting these, to make sure
  4264. ;; no other functions uses these, forgetting to let-bind them.
  4265. (defvar entry)
  4266. (defvar last-state)
  4267. (defvar date)
  4268. ;; Defined somewhere in this file, but used before definition.
  4269. (defvar org-entities) ;; defined in org-entities.el
  4270. (defvar org-struct-menu)
  4271. (defvar org-org-menu)
  4272. (defvar org-tbl-menu)
  4273. ;;;; Define the Org-mode
  4274. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4275. (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"))
  4276. ;; We use a before-change function to check if a table might need
  4277. ;; an update.
  4278. (defvar org-table-may-need-update t
  4279. "Indicates that a table might need an update.
  4280. This variable is set by `org-before-change-function'.
  4281. `org-table-align' sets it back to nil.")
  4282. (defun org-before-change-function (beg end)
  4283. "Every change indicates that a table might need an update."
  4284. (setq org-table-may-need-update t))
  4285. (defvar org-mode-map)
  4286. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4287. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4288. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4289. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4290. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4291. (defvar org-table-buffer-is-an nil)
  4292. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  4293. ;; some places -- e.g. see the macro org-with-limited-levels.
  4294. ;;
  4295. ;; In Org buffers, the value of `outline-regexp' is that of
  4296. ;; `org-outline-regexp'. The only function still directly relying on
  4297. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  4298. ;; job when `orgstruct-mode' is active.
  4299. (defvar org-outline-regexp "\\*+ "
  4300. "Regexp to match Org headlines.")
  4301. (defconst org-outline-regexp-bol "^\\*+ "
  4302. "Regexp to match Org headlines.
  4303. This is similar to `org-outline-regexp' but additionally makes
  4304. sure that we are at the beginning of the line.")
  4305. (defconst org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4306. "Matches an headline, putting stars and text into groups.
  4307. Stars are put in group 1 and the trimmed body in group 2.")
  4308. ;;;###autoload
  4309. (define-derived-mode org-mode outline-mode "Org"
  4310. "Outline-based notes management and organizer, alias
  4311. \"Carsten's outline-mode for keeping track of everything.\"
  4312. Org-mode develops organizational tasks around a NOTES file which
  4313. contains information about projects as plain text. Org-mode is
  4314. implemented on top of outline-mode, which is ideal to keep the content
  4315. of large files well structured. It supports ToDo items, deadlines and
  4316. time stamps, which magically appear in the diary listing of the Emacs
  4317. calendar. Tables are easily created with a built-in table editor.
  4318. Plain text URL-like links connect to websites, emails (VM), Usenet
  4319. messages (Gnus), BBDB entries, and any files related to the project.
  4320. For printing and sharing of notes, an Org-mode file (or a part of it)
  4321. can be exported as a structured ASCII or HTML file.
  4322. The following commands are available:
  4323. \\{org-mode-map}"
  4324. ;; Get rid of Outline menus, they are not needed
  4325. ;; Need to do this here because define-derived-mode sets up
  4326. ;; the keymap so late. Still, it is a waste to call this each time
  4327. ;; we switch another buffer into org-mode.
  4328. (if (featurep 'xemacs)
  4329. (when (boundp 'outline-mode-menu-heading)
  4330. ;; Assume this is Greg's port, it uses easymenu
  4331. (easy-menu-remove outline-mode-menu-heading)
  4332. (easy-menu-remove outline-mode-menu-show)
  4333. (easy-menu-remove outline-mode-menu-hide))
  4334. (define-key org-mode-map [menu-bar headings] 'undefined)
  4335. (define-key org-mode-map [menu-bar hide] 'undefined)
  4336. (define-key org-mode-map [menu-bar show] 'undefined))
  4337. (org-load-modules-maybe)
  4338. (easy-menu-add org-org-menu)
  4339. (easy-menu-add org-tbl-menu)
  4340. (org-install-agenda-files-menu)
  4341. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4342. (add-to-invisibility-spec '(org-cwidth))
  4343. (add-to-invisibility-spec '(org-hide-block . t))
  4344. (when (featurep 'xemacs)
  4345. (org-set-local 'line-move-ignore-invisible t))
  4346. (org-set-local 'outline-regexp org-outline-regexp)
  4347. (org-set-local 'outline-level 'org-outline-level)
  4348. (setq bidi-paragraph-direction 'left-to-right)
  4349. (when (and org-ellipsis
  4350. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4351. (fboundp 'make-glyph-code))
  4352. (unless org-display-table
  4353. (setq org-display-table (make-display-table)))
  4354. (set-display-table-slot
  4355. org-display-table 4
  4356. (vconcat (mapcar
  4357. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4358. org-ellipsis)))
  4359. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4360. (setq buffer-display-table org-display-table))
  4361. (org-set-regexps-and-options)
  4362. (when (and org-tag-faces (not org-tags-special-faces-re))
  4363. ;; tag faces set outside customize.... force initialization.
  4364. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4365. ;; Calc embedded
  4366. (org-set-local 'calc-embedded-open-mode "# ")
  4367. (modify-syntax-entry ?@ "w")
  4368. (if org-startup-truncated (setq truncate-lines t))
  4369. (org-set-local 'font-lock-unfontify-region-function
  4370. 'org-unfontify-region)
  4371. ;; Activate before-change-function
  4372. (org-set-local 'org-table-may-need-update t)
  4373. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4374. 'local)
  4375. ;; Check for running clock before killing a buffer
  4376. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4377. ;; Paragraphs and auto-filling
  4378. (org-set-autofill-regexps)
  4379. (setq indent-line-function 'org-indent-line-function)
  4380. (org-update-radio-target-regexp)
  4381. ;; Beginning/end of defun
  4382. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4383. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4384. ;; Next error for sparse trees
  4385. (org-set-local 'next-error-function 'org-occur-next-match)
  4386. ;; Make sure dependence stuff works reliably, even for users who set it
  4387. ;; too late :-(
  4388. (if org-enforce-todo-dependencies
  4389. (add-hook 'org-blocker-hook
  4390. 'org-block-todo-from-children-or-siblings-or-parent)
  4391. (remove-hook 'org-blocker-hook
  4392. 'org-block-todo-from-children-or-siblings-or-parent))
  4393. (if org-enforce-todo-checkbox-dependencies
  4394. (add-hook 'org-blocker-hook
  4395. 'org-block-todo-from-checkboxes)
  4396. (remove-hook 'org-blocker-hook
  4397. 'org-block-todo-from-checkboxes))
  4398. ;; Comment characters
  4399. (org-set-local 'comment-start "#")
  4400. (org-set-local 'comment-padding " ")
  4401. ;; Align options lines
  4402. (org-set-local
  4403. 'align-mode-rules-list
  4404. '((org-in-buffer-settings
  4405. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4406. (modes . '(org-mode)))))
  4407. ;; Imenu
  4408. (org-set-local 'imenu-create-index-function
  4409. 'org-imenu-get-tree)
  4410. ;; Make isearch reveal context
  4411. (if (or (featurep 'xemacs)
  4412. (not (boundp 'outline-isearch-open-invisible-function)))
  4413. ;; Emacs 21 and XEmacs make use of the hook
  4414. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4415. ;; Emacs 22 deals with this through a special variable
  4416. (org-set-local 'outline-isearch-open-invisible-function
  4417. (lambda (&rest ignore) (org-show-context 'isearch))))
  4418. ;; Turn on org-beamer-mode?
  4419. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4420. ;; Setup the pcomplete hooks
  4421. (set (make-local-variable 'pcomplete-command-completion-function)
  4422. 'org-pcomplete-initial)
  4423. (set (make-local-variable 'pcomplete-command-name-function)
  4424. 'org-command-at-point)
  4425. (set (make-local-variable 'pcomplete-default-completion-function)
  4426. 'ignore)
  4427. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4428. 'org-parse-arguments)
  4429. (set (make-local-variable 'pcomplete-termination-string) "")
  4430. (set (make-local-variable 'face-remapping-alist)
  4431. '((default org-default)))
  4432. ;; If empty file that did not turn on org-mode automatically, make it to.
  4433. (if (and org-insert-mode-line-in-empty-file
  4434. (org-called-interactively-p 'any)
  4435. (= (point-min) (point-max)))
  4436. (insert "# -*- mode: org -*-\n\n"))
  4437. (unless org-inhibit-startup
  4438. (when org-startup-align-all-tables
  4439. (let ((bmp (buffer-modified-p)))
  4440. (org-table-map-tables 'org-table-align 'quietly)
  4441. (set-buffer-modified-p bmp)))
  4442. (when org-startup-with-inline-images
  4443. (org-display-inline-images))
  4444. (when org-startup-indented
  4445. (require 'org-indent)
  4446. (org-indent-mode 1))
  4447. (unless org-inhibit-startup-visibility-stuff
  4448. (org-set-startup-visibility))))
  4449. (when (fboundp 'abbrev-table-put)
  4450. (abbrev-table-put org-mode-abbrev-table
  4451. :parents (list text-mode-abbrev-table)))
  4452. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4453. (defun org-current-time ()
  4454. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4455. (if (> (car org-time-stamp-rounding-minutes) 1)
  4456. (let ((r (car org-time-stamp-rounding-minutes))
  4457. (time (decode-time)))
  4458. (apply 'encode-time
  4459. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4460. (nthcdr 2 time))))
  4461. (current-time)))
  4462. (defun org-today ()
  4463. "Return today date, considering `org-extend-today-until'."
  4464. (time-to-days
  4465. (time-subtract (current-time)
  4466. (list 0 (* 3600 org-extend-today-until) 0))))
  4467. ;;;; Font-Lock stuff, including the activators
  4468. (defvar org-mouse-map (make-sparse-keymap))
  4469. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4470. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4471. (when org-mouse-1-follows-link
  4472. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4473. (when org-tab-follows-link
  4474. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4475. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4476. (require 'font-lock)
  4477. (defconst org-non-link-chars "]\t\n\r<>")
  4478. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4479. "shell" "elisp" "doi" "message"))
  4480. (defvar org-link-types-re nil
  4481. "Matches a link that has a url-like prefix like \"http:\"")
  4482. (defvar org-link-re-with-space nil
  4483. "Matches a link with spaces, optional angular brackets around it.")
  4484. (defvar org-link-re-with-space2 nil
  4485. "Matches a link with spaces, optional angular brackets around it.")
  4486. (defvar org-link-re-with-space3 nil
  4487. "Matches a link with spaces, only for internal part in bracket links.")
  4488. (defvar org-angle-link-re nil
  4489. "Matches link with angular brackets, spaces are allowed.")
  4490. (defvar org-plain-link-re nil
  4491. "Matches plain link, without spaces.")
  4492. (defvar org-bracket-link-regexp nil
  4493. "Matches a link in double brackets.")
  4494. (defvar org-bracket-link-analytic-regexp nil
  4495. "Regular expression used to analyze links.
  4496. Here is what the match groups contain after a match:
  4497. 1: http:
  4498. 2: http
  4499. 3: path
  4500. 4: [desc]
  4501. 5: desc")
  4502. (defvar org-bracket-link-analytic-regexp++ nil
  4503. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4504. (defvar org-any-link-re nil
  4505. "Regular expression matching any link.")
  4506. (defcustom org-match-sexp-depth 3
  4507. "Number of stacked braces for sub/superscript matching.
  4508. This has to be set before loading org.el to be effective."
  4509. :group 'org-export-translation ; ??????????????????????????/
  4510. :type 'integer)
  4511. (defun org-create-multibrace-regexp (left right n)
  4512. "Create a regular expression which will match a balanced sexp.
  4513. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4514. as single character strings.
  4515. The regexp returned will match the entire expression including the
  4516. delimiters. It will also define a single group which contains the
  4517. match except for the outermost delimiters. The maximum depth of
  4518. stacked delimiters is N. Escaping delimiters is not possible."
  4519. (let* ((nothing (concat "[^" left right "]*?"))
  4520. (or "\\|")
  4521. (re nothing)
  4522. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4523. (while (> n 1)
  4524. (setq n (1- n)
  4525. re (concat re or next)
  4526. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4527. (concat left "\\(" re "\\)" right)))
  4528. (defvar org-match-substring-regexp
  4529. (concat
  4530. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4531. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4532. "\\|"
  4533. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4534. "\\|"
  4535. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4536. "The regular expression matching a sub- or superscript.")
  4537. (defvar org-match-substring-with-braces-regexp
  4538. (concat
  4539. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4540. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4541. "\\)")
  4542. "The regular expression matching a sub- or superscript, forcing braces.")
  4543. (defun org-make-link-regexps ()
  4544. "Update the link regular expressions.
  4545. This should be called after the variable `org-link-types' has changed."
  4546. (setq org-link-types-re
  4547. (concat
  4548. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4549. org-link-re-with-space
  4550. (concat
  4551. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4552. "\\([^" org-non-link-chars " ]"
  4553. "[^" org-non-link-chars "]*"
  4554. "[^" org-non-link-chars " ]\\)>?")
  4555. org-link-re-with-space2
  4556. (concat
  4557. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4558. "\\([^" org-non-link-chars " ]"
  4559. "[^\t\n\r]*"
  4560. "[^" org-non-link-chars " ]\\)>?")
  4561. org-link-re-with-space3
  4562. (concat
  4563. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4564. "\\([^" org-non-link-chars " ]"
  4565. "[^\t\n\r]*\\)")
  4566. org-angle-link-re
  4567. (concat
  4568. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4569. "\\([^" org-non-link-chars " ]"
  4570. "[^" org-non-link-chars "]*"
  4571. "\\)>")
  4572. org-plain-link-re
  4573. (concat
  4574. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4575. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4576. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4577. org-bracket-link-regexp
  4578. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4579. org-bracket-link-analytic-regexp
  4580. (concat
  4581. "\\[\\["
  4582. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4583. "\\([^]]+\\)"
  4584. "\\]"
  4585. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4586. "\\]")
  4587. org-bracket-link-analytic-regexp++
  4588. (concat
  4589. "\\[\\["
  4590. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4591. "\\([^]]+\\)"
  4592. "\\]"
  4593. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4594. "\\]")
  4595. org-any-link-re
  4596. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4597. org-angle-link-re "\\)\\|\\("
  4598. org-plain-link-re "\\)")))
  4599. (org-make-link-regexps)
  4600. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4601. "Regular expression for fast time stamp matching.")
  4602. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4603. "Regular expression for fast time stamp matching.")
  4604. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4605. "Regular expression matching time strings for analysis.
  4606. This one does not require the space after the date, so it can be used
  4607. on a string that terminates immediately after the date.")
  4608. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4609. "Regular expression matching time strings for analysis.")
  4610. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4611. "Regular expression matching time stamps, with groups.")
  4612. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4613. "Regular expression matching time stamps (also [..]), with groups.")
  4614. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4615. "Regular expression matching a time stamp range.")
  4616. (defconst org-tr-regexp-both
  4617. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4618. "Regular expression matching a time stamp range.")
  4619. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4620. org-ts-regexp "\\)?")
  4621. "Regular expression matching a time stamp or time stamp range.")
  4622. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4623. org-ts-regexp-both "\\)?")
  4624. "Regular expression matching a time stamp or time stamp range.
  4625. The time stamps may be either active or inactive.")
  4626. (defvar org-emph-face nil)
  4627. (defun org-do-emphasis-faces (limit)
  4628. "Run through the buffer and add overlays to emphasised strings."
  4629. (let (rtn a)
  4630. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4631. (if (not (= (char-after (match-beginning 3))
  4632. (char-after (match-beginning 4))))
  4633. (progn
  4634. (setq rtn t)
  4635. (setq a (assoc (match-string 3) org-emphasis-alist))
  4636. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4637. 'face
  4638. (nth 1 a))
  4639. (and (nth 4 a)
  4640. (org-remove-flyspell-overlays-in
  4641. (match-beginning 0) (match-end 0)))
  4642. (add-text-properties (match-beginning 2) (match-end 2)
  4643. '(font-lock-multiline t org-emphasis t))
  4644. (when org-hide-emphasis-markers
  4645. (add-text-properties (match-end 4) (match-beginning 5)
  4646. '(invisible org-link))
  4647. (add-text-properties (match-beginning 3) (match-end 3)
  4648. '(invisible org-link)))))
  4649. (backward-char 1))
  4650. rtn))
  4651. (defun org-emphasize (&optional char)
  4652. "Insert or change an emphasis, i.e. a font like bold or italic.
  4653. If there is an active region, change that region to a new emphasis.
  4654. If there is no region, just insert the marker characters and position
  4655. the cursor between them.
  4656. CHAR should be either the marker character, or the first character of the
  4657. HTML tag associated with that emphasis. If CHAR is a space, the means
  4658. to remove the emphasis of the selected region.
  4659. If char is not given (for example in an interactive call) it
  4660. will be prompted for."
  4661. (interactive)
  4662. (let ((eal org-emphasis-alist) e det
  4663. (erc org-emphasis-regexp-components)
  4664. (prompt "")
  4665. (string "") beg end move tag c s)
  4666. (if (org-region-active-p)
  4667. (setq beg (region-beginning) end (region-end)
  4668. string (buffer-substring beg end))
  4669. (setq move t))
  4670. (while (setq e (pop eal))
  4671. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4672. c (aref tag 0))
  4673. (push (cons c (string-to-char (car e))) det)
  4674. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4675. (substring tag 1)))))
  4676. (setq det (nreverse det))
  4677. (unless char
  4678. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4679. (setq char (read-char-exclusive)))
  4680. (setq char (or (cdr (assoc char det)) char))
  4681. (if (equal char ?\ )
  4682. (setq s "" move nil)
  4683. (unless (assoc (char-to-string char) org-emphasis-alist)
  4684. (error "No such emphasis marker: \"%c\"" char))
  4685. (setq s (char-to-string char)))
  4686. (while (and (> (length string) 1)
  4687. (equal (substring string 0 1) (substring string -1))
  4688. (assoc (substring string 0 1) org-emphasis-alist))
  4689. (setq string (substring string 1 -1)))
  4690. (setq string (concat s string s))
  4691. (if beg (delete-region beg end))
  4692. (unless (or (bolp)
  4693. (string-match (concat "[" (nth 0 erc) "\n]")
  4694. (char-to-string (char-before (point)))))
  4695. (insert " "))
  4696. (unless (or (eobp)
  4697. (string-match (concat "[" (nth 1 erc) "\n]")
  4698. (char-to-string (char-after (point)))))
  4699. (insert " ") (backward-char 1))
  4700. (insert string)
  4701. (and move (backward-char 1))))
  4702. (defconst org-nonsticky-props
  4703. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4704. (defsubst org-rear-nonsticky-at (pos)
  4705. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4706. (defun org-activate-plain-links (limit)
  4707. "Run through the buffer and add overlays to links."
  4708. (catch 'exit
  4709. (let (f)
  4710. (if (re-search-forward org-plain-link-re limit t)
  4711. (progn
  4712. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4713. (setq f (get-text-property (match-beginning 0) 'face))
  4714. (if (or (eq f 'org-tag)
  4715. (and (listp f) (memq 'org-tag f)))
  4716. nil
  4717. (add-text-properties (match-beginning 0) (match-end 0)
  4718. (list 'mouse-face 'highlight
  4719. 'face 'org-link
  4720. 'keymap org-mouse-map))
  4721. (org-rear-nonsticky-at (match-end 0)))
  4722. t)))))
  4723. (defun org-activate-code (limit)
  4724. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4725. (progn
  4726. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4727. (remove-text-properties (match-beginning 0) (match-end 0)
  4728. '(display t invisible t intangible t))
  4729. t)))
  4730. (defcustom org-src-fontify-natively nil
  4731. "When non-nil, fontify code in code blocks."
  4732. :type 'boolean
  4733. :group 'org-appearance
  4734. :group 'org-babel)
  4735. (defun org-fontify-meta-lines-and-blocks (limit)
  4736. (condition-case nil
  4737. (org-fontify-meta-lines-and-blocks-1 limit)
  4738. (error (message "org-mode fontification error"))))
  4739. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4740. "Fontify #+ lines and blocks, in the correct ways."
  4741. (let ((case-fold-search t))
  4742. (if (re-search-forward
  4743. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4744. limit t)
  4745. (let ((beg (match-beginning 0))
  4746. (block-start (match-end 0))
  4747. (block-end nil)
  4748. (lang (match-string 7))
  4749. (beg1 (line-beginning-position 2))
  4750. (dc1 (downcase (match-string 2)))
  4751. (dc3 (downcase (match-string 3)))
  4752. end end1 quoting block-type ovl)
  4753. (cond
  4754. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4755. ;; a single line of backend-specific content
  4756. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4757. (remove-text-properties (match-beginning 0) (match-end 0)
  4758. '(display t invisible t intangible t))
  4759. (add-text-properties (match-beginning 1) (match-end 3)
  4760. '(font-lock-fontified t face org-meta-line))
  4761. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4762. '(font-lock-fontified t face org-block))
  4763. ; for backend-specific code
  4764. t)
  4765. ((and (match-end 4) (equal dc3 "begin"))
  4766. ;; Truly a block
  4767. (setq block-type (downcase (match-string 5))
  4768. quoting (member block-type org-protecting-blocks))
  4769. (when (re-search-forward
  4770. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4771. nil t) ;; on purpose, we look further than LIMIT
  4772. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4773. (setq block-end (match-beginning 0))
  4774. (when quoting
  4775. (remove-text-properties beg end
  4776. '(display t invisible t intangible t)))
  4777. (add-text-properties
  4778. beg end
  4779. '(font-lock-fontified t font-lock-multiline t))
  4780. (add-text-properties beg beg1 '(face org-meta-line))
  4781. (add-text-properties end1 (min (point-max) (1+ end))
  4782. '(face org-meta-line)) ; for end_src
  4783. (cond
  4784. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4785. (org-src-font-lock-fontify-block lang block-start block-end)
  4786. ;; remove old background overlays
  4787. (mapc (lambda (ov)
  4788. (if (eq (overlay-get ov 'face) 'org-block-background)
  4789. (delete-overlay ov)))
  4790. (overlays-at (/ (+ beg1 block-end) 2)))
  4791. ;; add a background overlay
  4792. (setq ovl (make-overlay beg1 block-end))
  4793. (overlay-put ovl 'face 'org-block-background)
  4794. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4795. (quoting
  4796. (add-text-properties beg1 (min (point-max) (1+ end1))
  4797. '(face org-block))) ; end of source block
  4798. ((not org-fontify-quote-and-verse-blocks))
  4799. ((string= block-type "quote")
  4800. (add-text-properties beg1 (1+ end1) '(face org-quote)))
  4801. ((string= block-type "verse")
  4802. (add-text-properties beg1 (1+ end1) '(face org-verse))))
  4803. (add-text-properties beg beg1 '(face org-block-begin-line))
  4804. (add-text-properties (1+ end) (1+ end1) '(face org-block-end-line))
  4805. t))
  4806. ((member dc1 '("title:" "author:" "email:" "date:"))
  4807. (add-text-properties
  4808. beg (match-end 3)
  4809. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4810. '(font-lock-fontified t invisible t)
  4811. '(font-lock-fontified t face org-document-info-keyword)))
  4812. (add-text-properties
  4813. (match-beginning 6) (match-end 6)
  4814. (if (string-equal dc1 "title:")
  4815. '(font-lock-fontified t face org-document-title)
  4816. '(font-lock-fontified t face org-document-info))))
  4817. ((not (member (char-after beg) '(?\ ?\t)))
  4818. ;; just any other in-buffer setting, but not indented
  4819. (add-text-properties
  4820. beg (1+ (match-end 0))
  4821. '(font-lock-fontified t face org-meta-line))
  4822. t)
  4823. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4824. "orgtbl:" "tblfm:" "tblname:" "results:"
  4825. "call:" "header:" "headers:" "name:"))
  4826. (and (match-end 4) (equal dc3 "attr")))
  4827. (add-text-properties
  4828. beg (match-end 0)
  4829. '(font-lock-fontified t face org-meta-line))
  4830. t)
  4831. ((member dc3 '(" " ""))
  4832. (add-text-properties
  4833. beg (match-end 0)
  4834. '(font-lock-fontified t face font-lock-comment-face)))
  4835. (t nil))))))
  4836. (defun org-activate-angle-links (limit)
  4837. "Run through the buffer and add overlays to links."
  4838. (if (re-search-forward org-angle-link-re limit t)
  4839. (progn
  4840. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4841. (add-text-properties (match-beginning 0) (match-end 0)
  4842. (list 'mouse-face 'highlight
  4843. 'keymap org-mouse-map))
  4844. (org-rear-nonsticky-at (match-end 0))
  4845. t)))
  4846. (defun org-activate-footnote-links (limit)
  4847. "Run through the buffer and add overlays to footnotes."
  4848. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4849. (when fn
  4850. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4851. (org-remove-flyspell-overlays-in beg end)
  4852. (add-text-properties beg end
  4853. (list 'mouse-face 'highlight
  4854. 'keymap org-mouse-map
  4855. 'help-echo
  4856. (if (= (point-at-bol) beg)
  4857. "Footnote definition"
  4858. "Footnote reference")
  4859. 'font-lock-fontified t
  4860. 'font-lock-multiline t
  4861. 'face 'org-footnote))))))
  4862. (defun org-activate-bracket-links (limit)
  4863. "Run through the buffer and add overlays to bracketed links."
  4864. (if (re-search-forward org-bracket-link-regexp limit t)
  4865. (let* ((help (concat "LINK: "
  4866. (org-match-string-no-properties 1)))
  4867. ;; FIXME: above we should remove the escapes.
  4868. ;; but that requires another match, protecting match data,
  4869. ;; a lot of overhead for font-lock.
  4870. (ip (org-maybe-intangible
  4871. (list 'invisible 'org-link
  4872. 'keymap org-mouse-map 'mouse-face 'highlight
  4873. 'font-lock-multiline t 'help-echo help)))
  4874. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4875. 'font-lock-multiline t 'help-echo help)))
  4876. ;; We need to remove the invisible property here. Table narrowing
  4877. ;; may have made some of this invisible.
  4878. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4879. (remove-text-properties (match-beginning 0) (match-end 0)
  4880. '(invisible nil))
  4881. (if (match-end 3)
  4882. (progn
  4883. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4884. (org-rear-nonsticky-at (match-beginning 3))
  4885. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4886. (org-rear-nonsticky-at (match-end 3))
  4887. (add-text-properties (match-end 3) (match-end 0) ip)
  4888. (org-rear-nonsticky-at (match-end 0)))
  4889. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4890. (org-rear-nonsticky-at (match-beginning 1))
  4891. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4892. (org-rear-nonsticky-at (match-end 1))
  4893. (add-text-properties (match-end 1) (match-end 0) ip)
  4894. (org-rear-nonsticky-at (match-end 0)))
  4895. t)))
  4896. (defun org-activate-dates (limit)
  4897. "Run through the buffer and add overlays to dates."
  4898. (if (re-search-forward org-tsr-regexp-both limit t)
  4899. (progn
  4900. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4901. (add-text-properties (match-beginning 0) (match-end 0)
  4902. (list 'mouse-face 'highlight
  4903. 'keymap org-mouse-map))
  4904. (org-rear-nonsticky-at (match-end 0))
  4905. (when org-display-custom-times
  4906. (if (match-end 3)
  4907. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4908. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4909. t)))
  4910. (defvar org-target-link-regexp nil
  4911. "Regular expression matching radio targets in plain text.")
  4912. (make-variable-buffer-local 'org-target-link-regexp)
  4913. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4914. "Regular expression matching a link target.")
  4915. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4916. "Regular expression matching a radio target.")
  4917. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4918. "Regular expression matching any target.")
  4919. (defun org-activate-target-links (limit)
  4920. "Run through the buffer and add overlays to target matches."
  4921. (when org-target-link-regexp
  4922. (let ((case-fold-search t))
  4923. (if (re-search-forward org-target-link-regexp limit t)
  4924. (progn
  4925. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4926. (add-text-properties (match-beginning 0) (match-end 0)
  4927. (list 'mouse-face 'highlight
  4928. 'keymap org-mouse-map
  4929. 'help-echo "Radio target link"
  4930. 'org-linked-text t))
  4931. (org-rear-nonsticky-at (match-end 0))
  4932. t)))))
  4933. (defun org-update-radio-target-regexp ()
  4934. "Find all radio targets in this file and update the regular expression."
  4935. (interactive)
  4936. (when (memq 'radio org-activate-links)
  4937. (setq org-target-link-regexp
  4938. (org-make-target-link-regexp (org-all-targets 'radio)))
  4939. (org-restart-font-lock)))
  4940. (defun org-hide-wide-columns (limit)
  4941. (let (s e)
  4942. (setq s (text-property-any (point) (or limit (point-max))
  4943. 'org-cwidth t))
  4944. (when s
  4945. (setq e (next-single-property-change s 'org-cwidth))
  4946. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4947. (goto-char e)
  4948. t)))
  4949. (defvar org-latex-and-specials-regexp nil
  4950. "Regular expression for highlighting export special stuff.")
  4951. (defvar org-match-substring-regexp)
  4952. (defvar org-match-substring-with-braces-regexp)
  4953. ;; This should be with the exporter code, but we also use if for font-locking
  4954. (defconst org-export-html-special-string-regexps
  4955. '(("\\\\-" . "&shy;")
  4956. ("---\\([^-]\\)" . "&mdash;\\1")
  4957. ("--\\([^-]\\)" . "&ndash;\\1")
  4958. ("\\.\\.\\." . "&hellip;"))
  4959. "Regular expressions for special string conversion.")
  4960. (defun org-compute-latex-and-specials-regexp ()
  4961. "Compute regular expression for stuff treated specially by exporters."
  4962. (if (not org-highlight-latex-fragments-and-specials)
  4963. (org-set-local 'org-latex-and-specials-regexp nil)
  4964. (require 'org-exp)
  4965. (let*
  4966. ((matchers (plist-get org-format-latex-options :matchers))
  4967. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4968. org-latex-regexps)))
  4969. (org-export-allow-BIND nil)
  4970. (options (org-combine-plists (org-default-export-plist)
  4971. (org-infile-export-plist)))
  4972. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4973. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4974. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4975. (org-export-html-expand (plist-get options :expand-quoted-html))
  4976. (org-export-with-special-strings (plist-get options :special-strings))
  4977. (re-sub
  4978. (cond
  4979. ((equal org-export-with-sub-superscripts '{})
  4980. (list org-match-substring-with-braces-regexp))
  4981. (org-export-with-sub-superscripts
  4982. (list org-match-substring-regexp))
  4983. (t nil)))
  4984. (re-latex
  4985. (if org-export-with-LaTeX-fragments
  4986. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4987. (re-macros
  4988. (if org-export-with-TeX-macros
  4989. (list (concat "\\\\"
  4990. (regexp-opt
  4991. (append
  4992. (delq nil
  4993. (mapcar 'car-safe
  4994. (append org-entities-user
  4995. org-entities)))
  4996. (if (boundp 'org-latex-entities)
  4997. (mapcar (lambda (x)
  4998. (or (car-safe x) x))
  4999. org-latex-entities)
  5000. nil))
  5001. 'words))) ; FIXME
  5002. ))
  5003. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5004. (re-special (if org-export-with-special-strings
  5005. (mapcar (lambda (x) (car x))
  5006. org-export-html-special-string-regexps)))
  5007. (re-rest
  5008. (delq nil
  5009. (list
  5010. (if org-export-html-expand "@<[^>\n]+>")
  5011. ))))
  5012. (org-set-local
  5013. 'org-latex-and-specials-regexp
  5014. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5015. re-rest) "\\|")))))
  5016. (defun org-do-latex-and-special-faces (limit)
  5017. "Run through the buffer and add overlays to links."
  5018. (when org-latex-and-specials-regexp
  5019. (let (rtn d)
  5020. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5021. limit t))
  5022. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5023. 'face))
  5024. '(org-code org-verbatim underline)))
  5025. (progn
  5026. (setq rtn t
  5027. d (cond ((member (char-after (1+ (match-beginning 0)))
  5028. '(?_ ?^)) 1)
  5029. (t 0)))
  5030. (font-lock-prepend-text-property
  5031. (+ d (match-beginning 0)) (match-end 0)
  5032. 'face 'org-latex-and-export-specials)
  5033. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5034. '(font-lock-multiline t)))))
  5035. rtn)))
  5036. (defun org-restart-font-lock ()
  5037. "Restart `font-lock-mode', to force refontification."
  5038. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5039. (font-lock-mode -1)
  5040. (font-lock-mode 1)))
  5041. (defun org-all-targets (&optional radio)
  5042. "Return a list of all targets in this file.
  5043. With optional argument RADIO, only find radio targets."
  5044. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  5045. rtn)
  5046. (save-excursion
  5047. (goto-char (point-min))
  5048. (while (re-search-forward re nil t)
  5049. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  5050. rtn)))
  5051. (defun org-make-target-link-regexp (targets)
  5052. "Make regular expression matching all strings in TARGETS.
  5053. The regular expression finds the targets also if there is a line break
  5054. between words."
  5055. (and targets
  5056. (concat
  5057. "\\<\\("
  5058. (mapconcat
  5059. (lambda (x)
  5060. (setq x (regexp-quote x))
  5061. (while (string-match " +" x)
  5062. (setq x (replace-match "\\s-+" t t x)))
  5063. x)
  5064. targets
  5065. "\\|")
  5066. "\\)\\>")))
  5067. (defun org-activate-tags (limit)
  5068. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5069. (progn
  5070. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5071. (add-text-properties (match-beginning 1) (match-end 1)
  5072. (list 'mouse-face 'highlight
  5073. 'keymap org-mouse-map))
  5074. (org-rear-nonsticky-at (match-end 1))
  5075. t)))
  5076. (defun org-outline-level ()
  5077. "Compute the outline level of the heading at point.
  5078. This function assumes that the cursor is at the beginning of a line matched
  5079. by `outline-regexp'. Otherwise it returns garbage.
  5080. If this is called at a normal headline, the level is the number of stars.
  5081. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5082. (save-excursion
  5083. (looking-at org-outline-regexp)
  5084. (1- (- (match-end 0) (match-beginning 0)))))
  5085. (defvar org-font-lock-keywords nil)
  5086. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5087. "Regular expression matching a property line.")
  5088. (defvar org-font-lock-hook nil
  5089. "Functions to be called for special font lock stuff.")
  5090. (defvar org-font-lock-set-keywords-hook nil
  5091. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5092. This is calles after `org-font-lock-extra-keywords' is defined, but before
  5093. it is installed to be used by font lock. This can be useful if something
  5094. needs to be inserted at a specific position in the font-lock sequence.")
  5095. (defun org-font-lock-hook (limit)
  5096. (run-hook-with-args 'org-font-lock-hook limit))
  5097. (defun org-set-font-lock-defaults ()
  5098. (let* ((em org-fontify-emphasized-text)
  5099. (lk org-activate-links)
  5100. (org-font-lock-extra-keywords
  5101. (list
  5102. ;; Call the hook
  5103. '(org-font-lock-hook)
  5104. ;; Headlines
  5105. `(,(if org-fontify-whole-heading-line
  5106. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5107. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5108. (1 (org-get-level-face 1))
  5109. (2 (org-get-level-face 2))
  5110. (3 (org-get-level-face 3)))
  5111. ;; Table lines
  5112. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5113. (1 'org-table t))
  5114. ;; Table internals
  5115. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5116. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5117. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5118. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5119. ;; Drawers
  5120. (list org-drawer-regexp '(0 'org-special-keyword t))
  5121. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5122. ;; Properties
  5123. (list org-property-re
  5124. '(1 'org-special-keyword t)
  5125. '(3 'org-property-value t))
  5126. ;; Links
  5127. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5128. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5129. (if (memq 'plain lk) '(org-activate-plain-links))
  5130. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5131. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5132. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5133. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5134. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5135. '(org-hide-wide-columns (0 nil append))
  5136. ;; TODO keyword
  5137. (list (format org-heading-keyword-regexp-format
  5138. org-todo-regexp)
  5139. '(2 (org-get-todo-face 2) t))
  5140. ;; DONE
  5141. (if org-fontify-done-headline
  5142. (list (format org-heading-keyword-regexp-format
  5143. (concat
  5144. "\\("
  5145. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5146. "\\)"))
  5147. '(2 'org-headline-done t))
  5148. nil)
  5149. ;; Priorities
  5150. '(org-font-lock-add-priority-faces)
  5151. ;; Tags
  5152. '(org-font-lock-add-tag-faces)
  5153. ;; Special keywords
  5154. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5155. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5156. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5157. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5158. ;; Emphasis
  5159. (if em
  5160. (if (featurep 'xemacs)
  5161. '(org-do-emphasis-faces (0 nil append))
  5162. '(org-do-emphasis-faces)))
  5163. ;; Checkboxes
  5164. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5165. 1 'org-checkbox prepend)
  5166. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5167. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5168. (0 (org-get-checkbox-statistics-face) t)))
  5169. ;; Description list items
  5170. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5171. 1 'bold prepend)
  5172. ;; ARCHIVEd headings
  5173. (list (concat
  5174. org-outline-regexp-bol
  5175. "\\(.*:" org-archive-tag ":.*\\)")
  5176. '(1 'org-archived prepend))
  5177. ;; Specials
  5178. '(org-do-latex-and-special-faces)
  5179. '(org-fontify-entities)
  5180. '(org-raise-scripts)
  5181. ;; Code
  5182. '(org-activate-code (1 'org-code t))
  5183. ;; COMMENT
  5184. (list (format org-heading-keyword-regexp-format
  5185. (concat "\\("
  5186. org-comment-string "\\|" org-quote-string
  5187. "\\)"))
  5188. '(2 'org-special-keyword t))
  5189. '("^#.*" (0 'font-lock-comment-face t))
  5190. ;; Blocks and meta lines
  5191. '(org-fontify-meta-lines-and-blocks)
  5192. )))
  5193. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5194. (run-hooks 'org-font-lock-set-keywords-hook)
  5195. ;; Now set the full font-lock-keywords
  5196. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5197. (org-set-local 'font-lock-defaults
  5198. '(org-font-lock-keywords t nil nil backward-paragraph))
  5199. (kill-local-variable 'font-lock-keywords) nil))
  5200. (defun org-toggle-pretty-entities ()
  5201. "Toggle the composition display of entities as UTF8 characters."
  5202. (interactive)
  5203. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5204. (org-restart-font-lock)
  5205. (if org-pretty-entities
  5206. (message "Entities are displayed as UTF8 characers")
  5207. (save-restriction
  5208. (widen)
  5209. (org-decompose-region (point-min) (point-max))
  5210. (message "Entities are displayed plain"))))
  5211. (defun org-fontify-entities (limit)
  5212. "Find an entity to fontify."
  5213. (let (ee)
  5214. (when org-pretty-entities
  5215. (catch 'match
  5216. (while (re-search-forward
  5217. "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"
  5218. limit t)
  5219. (if (and (not (org-in-indented-comment-line))
  5220. (setq ee (org-entity-get (match-string 1)))
  5221. (= (length (nth 6 ee)) 1))
  5222. (progn
  5223. (add-text-properties
  5224. (match-beginning 0) (match-end 1)
  5225. (list 'font-lock-fontified t))
  5226. (compose-region (match-beginning 0) (match-end 1)
  5227. (nth 6 ee) nil)
  5228. (backward-char 1)
  5229. (throw 'match t))))
  5230. nil))))
  5231. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5232. "Fontify string S like in Org-mode."
  5233. (with-temp-buffer
  5234. (insert s)
  5235. (let ((org-odd-levels-only odd-levels))
  5236. (org-mode)
  5237. (font-lock-fontify-buffer)
  5238. (buffer-string))))
  5239. (defvar org-m nil)
  5240. (defvar org-l nil)
  5241. (defvar org-f nil)
  5242. (defun org-get-level-face (n)
  5243. "Get the right face for match N in font-lock matching of headlines."
  5244. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5245. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5246. (if org-cycle-level-faces
  5247. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5248. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5249. (cond
  5250. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5251. ((eq n 2) org-f)
  5252. (t (if org-level-color-stars-only nil org-f))))
  5253. (defun org-get-todo-face (kwd)
  5254. "Get the right face for a TODO keyword KWD.
  5255. If KWD is a number, get the corresponding match group."
  5256. (if (numberp kwd) (setq kwd (match-string kwd)))
  5257. (or (org-face-from-face-or-color
  5258. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5259. (and (member kwd org-done-keywords) 'org-done)
  5260. 'org-todo))
  5261. (defun org-face-from-face-or-color (context inherit face-or-color)
  5262. "Create a face list that inherits INHERIT, but sets the foreground color.
  5263. When FACE-OR-COLOR is not a string, just return it."
  5264. (if (stringp face-or-color)
  5265. (list :inherit inherit
  5266. (cdr (assoc context org-faces-easy-properties))
  5267. face-or-color)
  5268. face-or-color))
  5269. (defun org-font-lock-add-tag-faces (limit)
  5270. "Add the special tag faces."
  5271. (when (and org-tag-faces org-tags-special-faces-re)
  5272. (while (re-search-forward org-tags-special-faces-re limit t)
  5273. (add-text-properties (match-beginning 1) (match-end 1)
  5274. (list 'face (org-get-tag-face 1)
  5275. 'font-lock-fontified t))
  5276. (backward-char 1))))
  5277. (defun org-font-lock-add-priority-faces (limit)
  5278. "Add the special priority faces."
  5279. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5280. (add-text-properties
  5281. (match-beginning 0) (match-end 0)
  5282. (list 'face (or (org-face-from-face-or-color
  5283. 'priority 'org-special-keyword
  5284. (cdr (assoc (char-after (match-beginning 1))
  5285. org-priority-faces)))
  5286. 'org-special-keyword)
  5287. 'font-lock-fontified t))))
  5288. (defun org-get-tag-face (kwd)
  5289. "Get the right face for a TODO keyword KWD.
  5290. If KWD is a number, get the corresponding match group."
  5291. (if (numberp kwd) (setq kwd (match-string kwd)))
  5292. (or (org-face-from-face-or-color
  5293. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5294. 'org-tag))
  5295. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5296. "Remove fontification and activation overlays from links."
  5297. (font-lock-default-unfontify-region beg end)
  5298. (let* ((buffer-undo-list t)
  5299. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5300. (inhibit-modification-hooks t)
  5301. deactivate-mark buffer-file-name buffer-file-truename)
  5302. (org-decompose-region beg end)
  5303. (remove-text-properties beg end
  5304. '(mouse-face t keymap t org-linked-text t
  5305. invisible t intangible t
  5306. org-no-flyspell t org-emphasis t))
  5307. (org-remove-font-lock-display-properties beg end)))
  5308. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5309. ((raise 0.3) (height 0.7))
  5310. ((raise -0.5))
  5311. ((raise 0.5)))
  5312. "Display properties for showing superscripts and subscripts.")
  5313. (defun org-remove-font-lock-display-properties (beg end)
  5314. "Remove specific display properties that have been added by font lock.
  5315. The will remove the raise properties that are used to show superscripts
  5316. and subscripts."
  5317. (let (next prop)
  5318. (while (< beg end)
  5319. (setq next (next-single-property-change beg 'display nil end)
  5320. prop (get-text-property beg 'display))
  5321. (if (member prop org-script-display)
  5322. (put-text-property beg next 'display nil))
  5323. (setq beg next))))
  5324. (defun org-raise-scripts (limit)
  5325. "Add raise properties to sub/superscripts."
  5326. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5327. (if (re-search-forward
  5328. (if (eq org-use-sub-superscripts t)
  5329. org-match-substring-regexp
  5330. org-match-substring-with-braces-regexp)
  5331. limit t)
  5332. (let* ((pos (point)) table-p comment-p
  5333. (mpos (match-beginning 3))
  5334. (emph-p (get-text-property mpos 'org-emphasis))
  5335. (link-p (get-text-property mpos 'mouse-face))
  5336. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5337. (goto-char (point-at-bol))
  5338. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5339. comment-p (org-looking-at-p "[ \t]*#"))
  5340. (goto-char pos)
  5341. ;; FIXME: Should we go back one character here, for a_b^c
  5342. ;; (goto-char (1- pos)) ;????????????????????
  5343. (if (or comment-p emph-p link-p keyw-p)
  5344. t
  5345. (put-text-property (match-beginning 3) (match-end 0)
  5346. 'display
  5347. (if (equal (char-after (match-beginning 2)) ?^)
  5348. (nth (if table-p 3 1) org-script-display)
  5349. (nth (if table-p 2 0) org-script-display)))
  5350. (add-text-properties (match-beginning 2) (match-end 2)
  5351. (list 'invisible t
  5352. 'org-dwidth t 'org-dwidth-n 1))
  5353. (if (and (eq (char-after (match-beginning 3)) ?{)
  5354. (eq (char-before (match-end 3)) ?}))
  5355. (progn
  5356. (add-text-properties
  5357. (match-beginning 3) (1+ (match-beginning 3))
  5358. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5359. (add-text-properties
  5360. (1- (match-end 3)) (match-end 3)
  5361. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5362. t)))))
  5363. ;;;; Visibility cycling, including org-goto and indirect buffer
  5364. ;;; Cycling
  5365. (defvar org-cycle-global-status nil)
  5366. (make-variable-buffer-local 'org-cycle-global-status)
  5367. (defvar org-cycle-subtree-status nil)
  5368. (make-variable-buffer-local 'org-cycle-subtree-status)
  5369. ;;;###autoload
  5370. (defvar org-inlinetask-min-level)
  5371. (defun org-cycle (&optional arg)
  5372. "TAB-action and visibility cycling for Org-mode.
  5373. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5374. is outline visibility cycling, but it also invokes other actions
  5375. in special contexts.
  5376. - When this function is called with a prefix argument, rotate the entire
  5377. buffer through 3 states (global cycling)
  5378. 1. OVERVIEW: Show only top-level headlines.
  5379. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5380. 3. SHOW ALL: Show everything.
  5381. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5382. determined by the variable `org-startup-folded', and by any VISIBILITY
  5383. properties in the buffer.
  5384. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5385. including any drawers.
  5386. - When inside a table, re-align the table and move to the next field.
  5387. - When point is at the beginning of a headline, rotate the subtree started
  5388. by this line through 3 different states (local cycling)
  5389. 1. FOLDED: Only the main headline is shown.
  5390. 2. CHILDREN: The main headline and the direct children are shown.
  5391. From this state, you can move to one of the children
  5392. and zoom in further.
  5393. 3. SUBTREE: Show the entire subtree, including body text.
  5394. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5395. - When point is at the beginning of an empty headline and the variable
  5396. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5397. of the headline by demoting and promoting it to likely levels. This
  5398. speeds up creation document structure by pressing TAB once or several
  5399. times right after creating a new headline.
  5400. - When there is a numeric prefix, go up to a heading with level ARG, do
  5401. a `show-subtree' and return to the previous cursor position. If ARG
  5402. is negative, go up that many levels.
  5403. - When point is not at the beginning of a headline, execute the global
  5404. binding for TAB, which is re-indenting the line. See the option
  5405. `org-cycle-emulate-tab' for details.
  5406. - Special case: if point is at the beginning of the buffer and there is
  5407. no headline in line 1, this function will act as if called with prefix arg
  5408. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5409. But only if also the variable `org-cycle-global-at-bob' is t."
  5410. (interactive "P")
  5411. (org-load-modules-maybe)
  5412. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5413. (and org-cycle-level-after-item/entry-creation
  5414. (or (org-cycle-level)
  5415. (org-cycle-item-indentation))))
  5416. (let* ((limit-level
  5417. (or org-cycle-max-level
  5418. (and (boundp 'org-inlinetask-min-level)
  5419. org-inlinetask-min-level
  5420. (1- org-inlinetask-min-level))))
  5421. (nstars (and limit-level
  5422. (if org-odd-levels-only
  5423. (and limit-level (1- (* limit-level 2)))
  5424. limit-level)))
  5425. (org-outline-regexp
  5426. (if (not (eq major-mode 'org-mode))
  5427. outline-regexp
  5428. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5429. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5430. (not (looking-at org-outline-regexp))))
  5431. (org-cycle-hook
  5432. (if bob-special
  5433. (delq 'org-optimize-window-after-visibility-change
  5434. (copy-sequence org-cycle-hook))
  5435. org-cycle-hook))
  5436. (pos (point)))
  5437. (if (or bob-special (equal arg '(4)))
  5438. ;; special case: use global cycling
  5439. (setq arg t))
  5440. (cond
  5441. ((equal arg '(16))
  5442. (setq last-command 'dummy)
  5443. (org-set-startup-visibility)
  5444. (message "Startup visibility, plus VISIBILITY properties"))
  5445. ((equal arg '(64))
  5446. (show-all)
  5447. (message "Entire buffer visible, including drawers"))
  5448. ;; Table: enter it or move to the next field.
  5449. ((org-at-table-p 'any)
  5450. (if (org-at-table.el-p)
  5451. (message "Use C-c ' to edit table.el tables")
  5452. (if arg (org-table-edit-field t)
  5453. (org-table-justify-field-maybe)
  5454. (call-interactively 'org-table-next-field))))
  5455. ((run-hook-with-args-until-success
  5456. 'org-tab-after-check-for-table-hook))
  5457. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5458. ((eq arg t) (org-cycle-internal-global))
  5459. ;; Drawers: delegate to `org-flag-drawer'.
  5460. ((and org-drawers org-drawer-regexp
  5461. (save-excursion
  5462. (beginning-of-line 1)
  5463. (looking-at org-drawer-regexp)))
  5464. (org-flag-drawer ; toggle block visibility
  5465. (not (get-char-property (match-end 0) 'invisible))))
  5466. ;; Show-subtree, ARG levels up from here.
  5467. ((integerp arg)
  5468. (save-excursion
  5469. (org-back-to-heading)
  5470. (outline-up-heading (if (< arg 0) (- arg)
  5471. (- (funcall outline-level) arg)))
  5472. (org-show-subtree)))
  5473. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5474. ((and (featurep 'org-inlinetask)
  5475. (org-inlinetask-at-task-p)
  5476. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5477. (org-inlinetask-toggle-visibility))
  5478. ((org-try-cdlatex-tab))
  5479. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5480. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5481. (save-excursion (beginning-of-line 1)
  5482. (looking-at org-outline-regexp)))
  5483. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5484. (org-cycle-internal-local))
  5485. ;; From there: TAB emulation and template completion.
  5486. (buffer-read-only (org-back-to-heading))
  5487. ((run-hook-with-args-until-success
  5488. 'org-tab-after-check-for-cycling-hook))
  5489. ((org-try-structure-completion))
  5490. ((run-hook-with-args-until-success
  5491. 'org-tab-before-tab-emulation-hook))
  5492. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5493. (or (not (bolp))
  5494. (not (looking-at org-outline-regexp))))
  5495. (call-interactively (global-key-binding "\t")))
  5496. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5497. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5498. (or (and (eq org-cycle-emulate-tab 'white)
  5499. (= (match-end 0) (point-at-eol)))
  5500. (and (eq org-cycle-emulate-tab 'whitestart)
  5501. (>= (match-end 0) pos))))
  5502. t
  5503. (eq org-cycle-emulate-tab t))
  5504. (call-interactively (global-key-binding "\t")))
  5505. (t (save-excursion
  5506. (org-back-to-heading)
  5507. (org-cycle)))))))
  5508. (defun org-cycle-internal-global ()
  5509. "Do the global cycling action."
  5510. (cond
  5511. ((and (eq last-command this-command)
  5512. (eq org-cycle-global-status 'overview))
  5513. ;; We just created the overview - now do table of contents
  5514. ;; This can be slow in very large buffers, so indicate action
  5515. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5516. (message "CONTENTS...")
  5517. (org-content)
  5518. (message "CONTENTS...done")
  5519. (setq org-cycle-global-status 'contents)
  5520. (run-hook-with-args 'org-cycle-hook 'contents))
  5521. ((and (eq last-command this-command)
  5522. (eq org-cycle-global-status 'contents))
  5523. ;; We just showed the table of contents - now show everything
  5524. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5525. (show-all)
  5526. (message "SHOW ALL")
  5527. (setq org-cycle-global-status 'all)
  5528. (run-hook-with-args 'org-cycle-hook 'all))
  5529. (t
  5530. ;; Default action: go to overview
  5531. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5532. (org-overview)
  5533. (message "OVERVIEW")
  5534. (setq org-cycle-global-status 'overview)
  5535. (run-hook-with-args 'org-cycle-hook 'overview))))
  5536. (defun org-cycle-internal-local ()
  5537. "Do the local cycling action."
  5538. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5539. ;; First, determine end of headline (EOH), end of subtree or item
  5540. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5541. (save-excursion
  5542. (if (org-at-item-p)
  5543. (progn
  5544. (beginning-of-line)
  5545. (setq struct (org-list-struct))
  5546. (setq eoh (point-at-eol))
  5547. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5548. (setq has-children (org-list-has-child-p (point) struct)))
  5549. (org-back-to-heading)
  5550. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5551. (setq eos (save-excursion
  5552. (org-end-of-subtree t)
  5553. (unless (eobp)
  5554. (skip-chars-forward " \t\n"))
  5555. (if (eobp) (point) (1- (point)))))
  5556. (setq has-children
  5557. (or (save-excursion
  5558. (let ((level (funcall outline-level)))
  5559. (outline-next-heading)
  5560. (and (org-at-heading-p t)
  5561. (> (funcall outline-level) level))))
  5562. (save-excursion
  5563. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5564. ;; Determine end invisible part of buffer (EOL)
  5565. (beginning-of-line 2)
  5566. ;; XEmacs doesn't have `next-single-char-property-change'
  5567. (if (featurep 'xemacs)
  5568. (while (and (not (eobp)) ;; this is like `next-line'
  5569. (get-char-property (1- (point)) 'invisible))
  5570. (beginning-of-line 2))
  5571. (while (and (not (eobp)) ;; this is like `next-line'
  5572. (get-char-property (1- (point)) 'invisible))
  5573. (goto-char (next-single-char-property-change (point) 'invisible))
  5574. (and (eolp) (beginning-of-line 2))))
  5575. (setq eol (point)))
  5576. ;; Find out what to do next and set `this-command'
  5577. (cond
  5578. ((= eos eoh)
  5579. ;; Nothing is hidden behind this heading
  5580. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5581. (message "EMPTY ENTRY")
  5582. (setq org-cycle-subtree-status nil)
  5583. (save-excursion
  5584. (goto-char eos)
  5585. (outline-next-heading)
  5586. (if (outline-invisible-p) (org-flag-heading nil))))
  5587. ((and (or (>= eol eos)
  5588. (not (string-match "\\S-" (buffer-substring eol eos))))
  5589. (or has-children
  5590. (not (setq children-skipped
  5591. org-cycle-skip-children-state-if-no-children))))
  5592. ;; Entire subtree is hidden in one line: children view
  5593. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5594. (if (org-at-item-p)
  5595. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5596. (org-show-entry)
  5597. (show-children)
  5598. ;; Fold every list in subtree to top-level items.
  5599. (when (eq org-cycle-include-plain-lists 'integrate)
  5600. (save-excursion
  5601. (org-back-to-heading)
  5602. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5603. (beginning-of-line 1)
  5604. (let* ((struct (org-list-struct))
  5605. (prevs (org-list-prevs-alist struct))
  5606. (end (org-list-get-bottom-point struct)))
  5607. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5608. (org-list-get-all-items (point) struct prevs))
  5609. (goto-char end))))))
  5610. (message "CHILDREN")
  5611. (save-excursion
  5612. (goto-char eos)
  5613. (outline-next-heading)
  5614. (if (outline-invisible-p) (org-flag-heading nil)))
  5615. (setq org-cycle-subtree-status 'children)
  5616. (run-hook-with-args 'org-cycle-hook 'children))
  5617. ((or children-skipped
  5618. (and (eq last-command this-command)
  5619. (eq org-cycle-subtree-status 'children)))
  5620. ;; We just showed the children, or no children are there,
  5621. ;; now show everything.
  5622. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5623. (outline-flag-region eoh eos nil)
  5624. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5625. (setq org-cycle-subtree-status 'subtree)
  5626. (run-hook-with-args 'org-cycle-hook 'subtree))
  5627. (t
  5628. ;; Default action: hide the subtree.
  5629. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5630. (outline-flag-region eoh eos t)
  5631. (message "FOLDED")
  5632. (setq org-cycle-subtree-status 'folded)
  5633. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5634. ;;;###autoload
  5635. (defun org-global-cycle (&optional arg)
  5636. "Cycle the global visibility. For details see `org-cycle'.
  5637. With \\[universal-argument] prefix arg, switch to startup visibility.
  5638. With a numeric prefix, show all headlines up to that level."
  5639. (interactive "P")
  5640. (let ((org-cycle-include-plain-lists
  5641. (if (eq major-mode 'org-mode) org-cycle-include-plain-lists nil)))
  5642. (cond
  5643. ((integerp arg)
  5644. (show-all)
  5645. (hide-sublevels arg)
  5646. (setq org-cycle-global-status 'contents))
  5647. ((equal arg '(4))
  5648. (org-set-startup-visibility)
  5649. (message "Startup visibility, plus VISIBILITY properties."))
  5650. (t
  5651. (org-cycle '(4))))))
  5652. (defun org-set-startup-visibility ()
  5653. "Set the visibility required by startup options and properties."
  5654. (cond
  5655. ((eq org-startup-folded t)
  5656. (org-cycle '(4)))
  5657. ((eq org-startup-folded 'content)
  5658. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5659. (org-cycle '(4)) (org-cycle '(4)))))
  5660. (unless (eq org-startup-folded 'showeverything)
  5661. (if org-hide-block-startup (org-hide-block-all))
  5662. (org-set-visibility-according-to-property 'no-cleanup)
  5663. (org-cycle-hide-archived-subtrees 'all)
  5664. (org-cycle-hide-drawers 'all)
  5665. (org-cycle-show-empty-lines t)))
  5666. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5667. "Switch subtree visibilities according to :VISIBILITY: property."
  5668. (interactive)
  5669. (let (org-show-entry-below state)
  5670. (save-excursion
  5671. (goto-char (point-min))
  5672. (while (re-search-forward
  5673. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5674. nil t)
  5675. (setq state (match-string 1))
  5676. (save-excursion
  5677. (org-back-to-heading t)
  5678. (hide-subtree)
  5679. (org-reveal)
  5680. (cond
  5681. ((equal state '("fold" "folded"))
  5682. (hide-subtree))
  5683. ((equal state "children")
  5684. (org-show-hidden-entry)
  5685. (show-children))
  5686. ((equal state "content")
  5687. (save-excursion
  5688. (save-restriction
  5689. (org-narrow-to-subtree)
  5690. (org-content))))
  5691. ((member state '("all" "showall"))
  5692. (show-subtree)))))
  5693. (unless no-cleanup
  5694. (org-cycle-hide-archived-subtrees 'all)
  5695. (org-cycle-hide-drawers 'all)
  5696. (org-cycle-show-empty-lines 'all)))))
  5697. ;; This function uses outline-regexp instead of the more fundamental
  5698. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5699. ;; buffers, where outline-regexp is needed.
  5700. (defun org-overview ()
  5701. "Switch to overview mode, showing only top-level headlines.
  5702. Really, this shows all headlines with level equal or greater than the level
  5703. of the first headline in the buffer. This is important, because if the
  5704. first headline is not level one, then (hide-sublevels 1) gives confusing
  5705. results."
  5706. (interactive)
  5707. (let ((level (save-excursion
  5708. (goto-char (point-min))
  5709. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5710. (progn
  5711. (goto-char (match-beginning 0))
  5712. (funcall outline-level))))))
  5713. (and level (hide-sublevels level))))
  5714. (defun org-content (&optional arg)
  5715. "Show all headlines in the buffer, like a table of contents.
  5716. With numerical argument N, show content up to level N."
  5717. (interactive "P")
  5718. (save-excursion
  5719. ;; Visit all headings and show their offspring
  5720. (and (integerp arg) (org-overview))
  5721. (goto-char (point-max))
  5722. (catch 'exit
  5723. (while (and (progn (condition-case nil
  5724. (outline-previous-visible-heading 1)
  5725. (error (goto-char (point-min))))
  5726. t)
  5727. (looking-at org-outline-regexp))
  5728. (if (integerp arg)
  5729. (show-children (1- arg))
  5730. (show-branches))
  5731. (if (bobp) (throw 'exit nil))))))
  5732. (defun org-optimize-window-after-visibility-change (state)
  5733. "Adjust the window after a change in outline visibility.
  5734. This function is the default value of the hook `org-cycle-hook'."
  5735. (when (get-buffer-window (current-buffer))
  5736. (cond
  5737. ((eq state 'content) nil)
  5738. ((eq state 'all) nil)
  5739. ((eq state 'folded) nil)
  5740. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5741. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5742. (defun org-remove-empty-overlays-at (pos)
  5743. "Remove outline overlays that do not contain non-white stuff."
  5744. (mapc
  5745. (lambda (o)
  5746. (and (eq 'outline (overlay-get o 'invisible))
  5747. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5748. (overlay-end o))))
  5749. (delete-overlay o)))
  5750. (overlays-at pos)))
  5751. (defun org-clean-visibility-after-subtree-move ()
  5752. "Fix visibility issues after moving a subtree."
  5753. ;; First, find a reasonable region to look at:
  5754. ;; Start two siblings above, end three below
  5755. (let* ((beg (save-excursion
  5756. (and (org-get-last-sibling)
  5757. (org-get-last-sibling))
  5758. (point)))
  5759. (end (save-excursion
  5760. (and (org-get-next-sibling)
  5761. (org-get-next-sibling)
  5762. (org-get-next-sibling))
  5763. (if (org-at-heading-p)
  5764. (point-at-eol)
  5765. (point))))
  5766. (level (looking-at "\\*+"))
  5767. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5768. (save-excursion
  5769. (save-restriction
  5770. (narrow-to-region beg end)
  5771. (when re
  5772. ;; Properly fold already folded siblings
  5773. (goto-char (point-min))
  5774. (while (re-search-forward re nil t)
  5775. (if (and (not (outline-invisible-p))
  5776. (save-excursion
  5777. (goto-char (point-at-eol)) (outline-invisible-p)))
  5778. (hide-entry))))
  5779. (org-cycle-show-empty-lines 'overview)
  5780. (org-cycle-hide-drawers 'overview)))))
  5781. (defun org-cycle-show-empty-lines (state)
  5782. "Show empty lines above all visible headlines.
  5783. The region to be covered depends on STATE when called through
  5784. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5785. entire buffer covered. Note that an empty line is only shown if there
  5786. are at least `org-cycle-separator-lines' empty lines before the headline."
  5787. (when (not (= org-cycle-separator-lines 0))
  5788. (save-excursion
  5789. (let* ((n (abs org-cycle-separator-lines))
  5790. (re (cond
  5791. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5792. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5793. (t (let ((ns (number-to-string (- n 2))))
  5794. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5795. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5796. beg end b e)
  5797. (cond
  5798. ((memq state '(overview contents t))
  5799. (setq beg (point-min) end (point-max)))
  5800. ((memq state '(children folded))
  5801. (setq beg (point) end (progn (org-end-of-subtree t t)
  5802. (beginning-of-line 2)
  5803. (point)))))
  5804. (when beg
  5805. (goto-char beg)
  5806. (while (re-search-forward re end t)
  5807. (unless (get-char-property (match-end 1) 'invisible)
  5808. (setq e (match-end 1))
  5809. (if (< org-cycle-separator-lines 0)
  5810. (setq b (save-excursion
  5811. (goto-char (match-beginning 0))
  5812. (org-back-over-empty-lines)
  5813. (if (save-excursion
  5814. (goto-char (max (point-min) (1- (point))))
  5815. (org-on-heading-p))
  5816. (1- (point))
  5817. (point))))
  5818. (setq b (match-beginning 1)))
  5819. (outline-flag-region b e nil)))))))
  5820. ;; Never hide empty lines at the end of the file.
  5821. (save-excursion
  5822. (goto-char (point-max))
  5823. (outline-previous-heading)
  5824. (outline-end-of-heading)
  5825. (if (and (looking-at "[ \t\n]+")
  5826. (= (match-end 0) (point-max)))
  5827. (outline-flag-region (point) (match-end 0) nil))))
  5828. (defun org-show-empty-lines-in-parent ()
  5829. "Move to the parent and re-show empty lines before visible headlines."
  5830. (save-excursion
  5831. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5832. (org-cycle-show-empty-lines context))))
  5833. (defun org-files-list ()
  5834. "Return `org-agenda-files' list, plus all open org-mode files.
  5835. This is useful for operations that need to scan all of a user's
  5836. open and agenda-wise Org files."
  5837. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5838. (dolist (buf (buffer-list))
  5839. (with-current-buffer buf
  5840. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5841. (let ((file (expand-file-name (buffer-file-name))))
  5842. (unless (member file files)
  5843. (push file files))))))
  5844. files))
  5845. (defsubst org-entry-beginning-position ()
  5846. "Return the beginning position of the current entry."
  5847. (save-excursion (outline-back-to-heading t) (point)))
  5848. (defsubst org-entry-end-position ()
  5849. "Return the end position of the current entry."
  5850. (save-excursion (outline-next-heading) (point)))
  5851. (defun org-cycle-hide-drawers (state)
  5852. "Re-hide all drawers after a visibility state change."
  5853. (when (and (eq major-mode 'org-mode)
  5854. (not (memq state '(overview folded contents))))
  5855. (save-excursion
  5856. (let* ((globalp (memq state '(contents all)))
  5857. (beg (if globalp (point-min) (point)))
  5858. (end (if globalp (point-max)
  5859. (if (eq state 'children)
  5860. (save-excursion (outline-next-heading) (point))
  5861. (org-end-of-subtree t)))))
  5862. (goto-char beg)
  5863. (while (re-search-forward org-drawer-regexp end t)
  5864. (org-flag-drawer t))))))
  5865. (defun org-flag-drawer (flag)
  5866. (save-excursion
  5867. (beginning-of-line 1)
  5868. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5869. (let ((b (match-end 0)))
  5870. (if (re-search-forward
  5871. "^[ \t]*:END:"
  5872. (save-excursion (outline-next-heading) (point)) t)
  5873. (outline-flag-region b (point-at-eol) flag)
  5874. (error ":END: line missing at position %s" b))))))
  5875. (defun org-subtree-end-visible-p ()
  5876. "Is the end of the current subtree visible?"
  5877. (pos-visible-in-window-p
  5878. (save-excursion (org-end-of-subtree t) (point))))
  5879. (defun org-first-headline-recenter (&optional N)
  5880. "Move cursor to the first headline and recenter the headline.
  5881. Optional argument N means put the headline into the Nth line of the window."
  5882. (goto-char (point-min))
  5883. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  5884. (beginning-of-line)
  5885. (recenter (prefix-numeric-value N))))
  5886. ;;; Saving and restoring visibility
  5887. (defun org-outline-overlay-data (&optional use-markers)
  5888. "Return a list of the locations of all outline overlays.
  5889. These are overlays with the `invisible' property value `outline'.
  5890. The return value is a list of cons cells, with start and stop
  5891. positions for each overlay.
  5892. If USE-MARKERS is set, return the positions as markers."
  5893. (let (beg end)
  5894. (save-excursion
  5895. (save-restriction
  5896. (widen)
  5897. (delq nil
  5898. (mapcar (lambda (o)
  5899. (when (eq (overlay-get o 'invisible) 'outline)
  5900. (setq beg (overlay-start o)
  5901. end (overlay-end o))
  5902. (and beg end (> end beg)
  5903. (if use-markers
  5904. (cons (move-marker (make-marker) beg)
  5905. (move-marker (make-marker) end))
  5906. (cons beg end)))))
  5907. (overlays-in (point-min) (point-max))))))))
  5908. (defun org-set-outline-overlay-data (data)
  5909. "Create visibility overlays for all positions in DATA.
  5910. DATA should have been made by `org-outline-overlay-data'."
  5911. (let (o)
  5912. (save-excursion
  5913. (save-restriction
  5914. (widen)
  5915. (show-all)
  5916. (mapc (lambda (c)
  5917. (outline-flag-region (car c) (cdr c) t))
  5918. data)))))
  5919. ;;; Folding of blocks
  5920. (defvar org-hide-block-overlays nil
  5921. "Overlays hiding blocks.")
  5922. (make-variable-buffer-local 'org-hide-block-overlays)
  5923. (defun org-block-map (function &optional start end)
  5924. "Call FUNCTION at the head of all source blocks in the current buffer.
  5925. Optional arguments START and END can be used to limit the range."
  5926. (let ((start (or start (point-min)))
  5927. (end (or end (point-max))))
  5928. (save-excursion
  5929. (goto-char start)
  5930. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5931. (save-excursion
  5932. (save-match-data
  5933. (goto-char (match-beginning 0))
  5934. (funcall function)))))))
  5935. (defun org-hide-block-toggle-all ()
  5936. "Toggle the visibility of all blocks in the current buffer."
  5937. (org-block-map #'org-hide-block-toggle))
  5938. (defun org-hide-block-all ()
  5939. "Fold all blocks in the current buffer."
  5940. (interactive)
  5941. (org-show-block-all)
  5942. (org-block-map #'org-hide-block-toggle-maybe))
  5943. (defun org-show-block-all ()
  5944. "Unfold all blocks in the current buffer."
  5945. (interactive)
  5946. (mapc 'delete-overlay org-hide-block-overlays)
  5947. (setq org-hide-block-overlays nil))
  5948. (defun org-hide-block-toggle-maybe ()
  5949. "Toggle visibility of block at point."
  5950. (interactive)
  5951. (let ((case-fold-search t))
  5952. (if (save-excursion
  5953. (beginning-of-line 1)
  5954. (looking-at org-block-regexp))
  5955. (progn (org-hide-block-toggle)
  5956. t) ;; to signal that we took action
  5957. nil))) ;; to signal that we did not
  5958. (defun org-hide-block-toggle (&optional force)
  5959. "Toggle the visibility of the current block."
  5960. (interactive)
  5961. (save-excursion
  5962. (beginning-of-line)
  5963. (if (re-search-forward org-block-regexp nil t)
  5964. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5965. (end (match-end 0)) ;; end of entire body
  5966. ov)
  5967. (if (memq t (mapcar (lambda (overlay)
  5968. (eq (overlay-get overlay 'invisible)
  5969. 'org-hide-block))
  5970. (overlays-at start)))
  5971. (if (or (not force) (eq force 'off))
  5972. (mapc (lambda (ov)
  5973. (when (member ov org-hide-block-overlays)
  5974. (setq org-hide-block-overlays
  5975. (delq ov org-hide-block-overlays)))
  5976. (when (eq (overlay-get ov 'invisible)
  5977. 'org-hide-block)
  5978. (delete-overlay ov)))
  5979. (overlays-at start)))
  5980. (setq ov (make-overlay start end))
  5981. (overlay-put ov 'invisible 'org-hide-block)
  5982. ;; make the block accessible to isearch
  5983. (overlay-put
  5984. ov 'isearch-open-invisible
  5985. (lambda (ov)
  5986. (when (member ov org-hide-block-overlays)
  5987. (setq org-hide-block-overlays
  5988. (delq ov org-hide-block-overlays)))
  5989. (when (eq (overlay-get ov 'invisible)
  5990. 'org-hide-block)
  5991. (delete-overlay ov))))
  5992. (push ov org-hide-block-overlays)))
  5993. (error "Not looking at a source block"))))
  5994. ;; org-tab-after-check-for-cycling-hook
  5995. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5996. ;; Remove overlays when changing major mode
  5997. (add-hook 'org-mode-hook
  5998. (lambda () (org-add-hook 'change-major-mode-hook
  5999. 'org-show-block-all 'append 'local)))
  6000. ;;; Org-goto
  6001. (defvar org-goto-window-configuration nil)
  6002. (defvar org-goto-marker nil)
  6003. (defvar org-goto-map
  6004. (let ((map (make-sparse-keymap)))
  6005. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  6006. (while (setq cmd (pop cmds))
  6007. (substitute-key-definition cmd cmd map global-map)))
  6008. (suppress-keymap map)
  6009. (org-defkey map "\C-m" 'org-goto-ret)
  6010. (org-defkey map [(return)] 'org-goto-ret)
  6011. (org-defkey map [(left)] 'org-goto-left)
  6012. (org-defkey map [(right)] 'org-goto-right)
  6013. (org-defkey map [(control ?g)] 'org-goto-quit)
  6014. (org-defkey map "\C-i" 'org-cycle)
  6015. (org-defkey map [(tab)] 'org-cycle)
  6016. (org-defkey map [(down)] 'outline-next-visible-heading)
  6017. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6018. (if org-goto-auto-isearch
  6019. (if (fboundp 'define-key-after)
  6020. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6021. nil)
  6022. (org-defkey map "q" 'org-goto-quit)
  6023. (org-defkey map "n" 'outline-next-visible-heading)
  6024. (org-defkey map "p" 'outline-previous-visible-heading)
  6025. (org-defkey map "f" 'outline-forward-same-level)
  6026. (org-defkey map "b" 'outline-backward-same-level)
  6027. (org-defkey map "u" 'outline-up-heading))
  6028. (org-defkey map "/" 'org-occur)
  6029. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6030. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6031. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6032. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6033. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6034. map))
  6035. (defconst org-goto-help
  6036. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  6037. RET=jump to location [Q]uit and return to previous location
  6038. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6039. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6040. ;; FIXME: Docstring does not mention both interfaces
  6041. (defun org-goto (&optional alternative-interface)
  6042. "Look up a different location in the current file, keeping current visibility.
  6043. When you want look-up or go to a different location in a document, the
  6044. fastest way is often to fold the entire buffer and then dive into the tree.
  6045. This method has the disadvantage, that the previous location will be folded,
  6046. which may not be what you want.
  6047. This command works around this by showing a copy of the current buffer
  6048. in an indirect buffer, in overview mode. You can dive into the tree in
  6049. that copy, use org-occur and incremental search to find a location.
  6050. When pressing RET or `Q', the command returns to the original buffer in
  6051. which the visibility is still unchanged. After RET is will also jump to
  6052. the location selected in the indirect buffer and expose the headline
  6053. hierarchy above."
  6054. (interactive "P")
  6055. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6056. (org-refile-use-outline-path t)
  6057. (org-refile-target-verify-function nil)
  6058. (interface
  6059. (if (not alternative-interface)
  6060. org-goto-interface
  6061. (if (eq org-goto-interface 'outline)
  6062. 'outline-path-completion
  6063. 'outline)))
  6064. (org-goto-start-pos (point))
  6065. (selected-point
  6066. (if (eq interface 'outline)
  6067. (car (org-get-location (current-buffer) org-goto-help))
  6068. (let ((pa (org-refile-get-location "Goto")))
  6069. (org-refile-check-position pa)
  6070. (nth 3 pa)))))
  6071. (if selected-point
  6072. (progn
  6073. (org-mark-ring-push org-goto-start-pos)
  6074. (goto-char selected-point)
  6075. (if (or (outline-invisible-p) (org-invisible-p2))
  6076. (org-show-context 'org-goto)))
  6077. (message "Quit"))))
  6078. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6079. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6080. (defvar org-goto-local-auto-isearch-map) ; defined below
  6081. (defun org-get-location (buf help)
  6082. "Let the user select a location in the Org-mode buffer BUF.
  6083. This function uses a recursive edit. It returns the selected position
  6084. or nil."
  6085. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6086. (isearch-hide-immediately nil)
  6087. (isearch-search-fun-function
  6088. (lambda () 'org-goto-local-search-headings))
  6089. (org-goto-selected-point org-goto-exit-command)
  6090. (pop-up-frames nil)
  6091. (special-display-buffer-names nil)
  6092. (special-display-regexps nil)
  6093. (special-display-function nil))
  6094. (save-excursion
  6095. (save-window-excursion
  6096. (delete-other-windows)
  6097. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6098. (org-pop-to-buffer-same-window
  6099. (condition-case nil
  6100. (make-indirect-buffer (current-buffer) "*org-goto*")
  6101. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6102. (with-output-to-temp-buffer "*Help*"
  6103. (princ help))
  6104. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  6105. (setq buffer-read-only nil)
  6106. (let ((org-startup-truncated t)
  6107. (org-startup-folded nil)
  6108. (org-startup-align-all-tables nil))
  6109. (org-mode)
  6110. (org-overview))
  6111. (setq buffer-read-only t)
  6112. (if (and (boundp 'org-goto-start-pos)
  6113. (integer-or-marker-p org-goto-start-pos))
  6114. (let ((org-show-hierarchy-above t)
  6115. (org-show-siblings t)
  6116. (org-show-following-heading t))
  6117. (goto-char org-goto-start-pos)
  6118. (and (outline-invisible-p) (org-show-context)))
  6119. (goto-char (point-min)))
  6120. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6121. (message "Select location and press RET")
  6122. (use-local-map org-goto-map)
  6123. (recursive-edit)
  6124. ))
  6125. (kill-buffer "*org-goto*")
  6126. (cons org-goto-selected-point org-goto-exit-command)))
  6127. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6128. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6129. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6130. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6131. (defun org-goto-local-search-headings (string bound noerror)
  6132. "Search and make sure that any matches are in headlines."
  6133. (catch 'return
  6134. (while (if isearch-forward
  6135. (search-forward string bound noerror)
  6136. (search-backward string bound noerror))
  6137. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6138. (and (member :headline context)
  6139. (not (member :tags context))))
  6140. (throw 'return (point))))))
  6141. (defun org-goto-local-auto-isearch ()
  6142. "Start isearch."
  6143. (interactive)
  6144. (goto-char (point-min))
  6145. (let ((keys (this-command-keys)))
  6146. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6147. (isearch-mode t)
  6148. (isearch-process-search-char (string-to-char keys)))))
  6149. (defun org-goto-ret (&optional arg)
  6150. "Finish `org-goto' by going to the new location."
  6151. (interactive "P")
  6152. (setq org-goto-selected-point (point)
  6153. org-goto-exit-command 'return)
  6154. (throw 'exit nil))
  6155. (defun org-goto-left ()
  6156. "Finish `org-goto' by going to the new location."
  6157. (interactive)
  6158. (if (org-on-heading-p)
  6159. (progn
  6160. (beginning-of-line 1)
  6161. (setq org-goto-selected-point (point)
  6162. org-goto-exit-command 'left)
  6163. (throw 'exit nil))
  6164. (error "Not on a heading")))
  6165. (defun org-goto-right ()
  6166. "Finish `org-goto' by going to the new location."
  6167. (interactive)
  6168. (if (org-on-heading-p)
  6169. (progn
  6170. (setq org-goto-selected-point (point)
  6171. org-goto-exit-command 'right)
  6172. (throw 'exit nil))
  6173. (error "Not on a heading")))
  6174. (defun org-goto-quit ()
  6175. "Finish `org-goto' without cursor motion."
  6176. (interactive)
  6177. (setq org-goto-selected-point nil)
  6178. (setq org-goto-exit-command 'quit)
  6179. (throw 'exit nil))
  6180. ;;; Indirect buffer display of subtrees
  6181. (defvar org-indirect-dedicated-frame nil
  6182. "This is the frame being used for indirect tree display.")
  6183. (defvar org-last-indirect-buffer nil)
  6184. (defun org-tree-to-indirect-buffer (&optional arg)
  6185. "Create indirect buffer and narrow it to current subtree.
  6186. With numerical prefix ARG, go up to this level and then take that tree.
  6187. If ARG is negative, go up that many levels.
  6188. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6189. indirect buffer previously made with this command, to avoid proliferation of
  6190. indirect buffers. However, when you call the command with a \
  6191. \\[universal-argument] prefix, or
  6192. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6193. is kept so that you can work with several indirect buffers at the same time.
  6194. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6195. \\[universal-argument] prefix also
  6196. requests that a new frame be made for the new buffer, so that the dedicated
  6197. frame is not changed."
  6198. (interactive "P")
  6199. (let ((cbuf (current-buffer))
  6200. (cwin (selected-window))
  6201. (pos (point))
  6202. beg end level heading ibuf)
  6203. (save-excursion
  6204. (org-back-to-heading t)
  6205. (when (numberp arg)
  6206. (setq level (org-outline-level))
  6207. (if (< arg 0) (setq arg (+ level arg)))
  6208. (while (> (setq level (org-outline-level)) arg)
  6209. (outline-up-heading 1 t)))
  6210. (setq beg (point)
  6211. heading (org-get-heading))
  6212. (org-end-of-subtree t t)
  6213. (if (org-on-heading-p) (backward-char 1))
  6214. (setq end (point)))
  6215. (if (and (buffer-live-p org-last-indirect-buffer)
  6216. (not (eq org-indirect-buffer-display 'new-frame))
  6217. (not arg))
  6218. (kill-buffer org-last-indirect-buffer))
  6219. (setq ibuf (org-get-indirect-buffer cbuf)
  6220. org-last-indirect-buffer ibuf)
  6221. (cond
  6222. ((or (eq org-indirect-buffer-display 'new-frame)
  6223. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6224. (select-frame (make-frame))
  6225. (delete-other-windows)
  6226. (org-pop-to-buffer-same-window ibuf)
  6227. (org-set-frame-title heading))
  6228. ((eq org-indirect-buffer-display 'dedicated-frame)
  6229. (raise-frame
  6230. (select-frame (or (and org-indirect-dedicated-frame
  6231. (frame-live-p org-indirect-dedicated-frame)
  6232. org-indirect-dedicated-frame)
  6233. (setq org-indirect-dedicated-frame (make-frame)))))
  6234. (delete-other-windows)
  6235. (org-pop-to-buffer-same-window ibuf)
  6236. (org-set-frame-title (concat "Indirect: " heading)))
  6237. ((eq org-indirect-buffer-display 'current-window)
  6238. (org-pop-to-buffer-same-window ibuf))
  6239. ((eq org-indirect-buffer-display 'other-window)
  6240. (pop-to-buffer ibuf))
  6241. (t (error "Invalid value")))
  6242. (if (featurep 'xemacs)
  6243. (save-excursion (org-mode) (turn-on-font-lock)))
  6244. (narrow-to-region beg end)
  6245. (show-all)
  6246. (goto-char pos)
  6247. (run-hook-with-args 'org-cycle-hook 'all)
  6248. (and (window-live-p cwin) (select-window cwin))))
  6249. (defun org-get-indirect-buffer (&optional buffer)
  6250. (setq buffer (or buffer (current-buffer)))
  6251. (let ((n 1) (base (buffer-name buffer)) bname)
  6252. (while (buffer-live-p
  6253. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6254. (setq n (1+ n)))
  6255. (condition-case nil
  6256. (make-indirect-buffer buffer bname 'clone)
  6257. (error (make-indirect-buffer buffer bname)))))
  6258. (defun org-set-frame-title (title)
  6259. "Set the title of the current frame to the string TITLE."
  6260. ;; FIXME: how to name a single frame in XEmacs???
  6261. (unless (featurep 'xemacs)
  6262. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6263. ;;;; Structure editing
  6264. ;;; Inserting headlines
  6265. (defun org-previous-line-empty-p ()
  6266. (save-excursion
  6267. (and (not (bobp))
  6268. (or (beginning-of-line 0) t)
  6269. (save-match-data
  6270. (looking-at "[ \t]*$")))))
  6271. (defun org-insert-heading (&optional force-heading invisible-ok)
  6272. "Insert a new heading or item with same depth at point.
  6273. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6274. If point is at the beginning of a headline, insert a sibling before the
  6275. current headline. If point is not at the beginning, split the line,
  6276. create the new headline with the text in the current line after point
  6277. \(but see also the variable `org-M-RET-may-split-line').
  6278. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6279. This is important for non-interactive uses of the command."
  6280. (interactive "P")
  6281. (if (or (= (buffer-size) 0)
  6282. (and (not (save-excursion
  6283. (and (ignore-errors (org-back-to-heading invisible-ok))
  6284. (org-on-heading-p))))
  6285. (or force-heading (not (org-in-item-p)))))
  6286. (progn
  6287. (insert "\n* ")
  6288. (run-hooks 'org-insert-heading-hook))
  6289. (when (or force-heading (not (org-insert-item)))
  6290. (let* ((empty-line-p nil)
  6291. (level nil)
  6292. (on-heading (org-on-heading-p))
  6293. (head (save-excursion
  6294. (condition-case nil
  6295. (progn
  6296. (org-back-to-heading invisible-ok)
  6297. (when (and (not on-heading)
  6298. (featurep 'org-inlinetask)
  6299. (integerp org-inlinetask-min-level)
  6300. (>= (length (match-string 0))
  6301. org-inlinetask-min-level))
  6302. ;; Find a heading level before the inline task
  6303. (while (and (setq level (org-up-heading-safe))
  6304. (>= level org-inlinetask-min-level)))
  6305. (if (org-on-heading-p)
  6306. (org-back-to-heading invisible-ok)
  6307. (error "This should not happen")))
  6308. (setq empty-line-p (org-previous-line-empty-p))
  6309. (match-string 0))
  6310. (error "*"))))
  6311. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6312. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6313. pos hide-previous previous-pos)
  6314. (cond
  6315. ((and (org-on-heading-p) (bolp)
  6316. (or (bobp)
  6317. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6318. ;; insert before the current line
  6319. (open-line (if blank 2 1)))
  6320. ((and (bolp)
  6321. (not org-insert-heading-respect-content)
  6322. (or (bobp)
  6323. (save-excursion
  6324. (backward-char 1) (not (outline-invisible-p)))))
  6325. ;; insert right here
  6326. nil)
  6327. (t
  6328. ;; somewhere in the line
  6329. (save-excursion
  6330. (setq previous-pos (point-at-bol))
  6331. (end-of-line)
  6332. (setq hide-previous (outline-invisible-p)))
  6333. (and org-insert-heading-respect-content (org-show-subtree))
  6334. (let ((split
  6335. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6336. (save-excursion
  6337. (let ((p (point)))
  6338. (goto-char (point-at-bol))
  6339. (and (looking-at org-complex-heading-regexp)
  6340. (match-beginning 4)
  6341. (> p (match-beginning 4)))))))
  6342. tags pos)
  6343. (cond
  6344. (org-insert-heading-respect-content
  6345. (org-end-of-subtree nil t)
  6346. (when (featurep 'org-inlinetask)
  6347. (while (and (not (eobp))
  6348. (looking-at "\\(\\*+\\)[ \t]+")
  6349. (>= (length (match-string 1))
  6350. org-inlinetask-min-level))
  6351. (org-end-of-subtree nil t)))
  6352. (or (bolp) (newline))
  6353. (or (org-previous-line-empty-p)
  6354. (and blank (newline)))
  6355. (open-line 1))
  6356. ((org-on-heading-p)
  6357. (when hide-previous
  6358. (show-children)
  6359. (org-show-entry))
  6360. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6361. (setq tags (and (match-end 2) (match-string 2)))
  6362. (and (match-end 1)
  6363. (delete-region (match-beginning 1) (match-end 1)))
  6364. (setq pos (point-at-bol))
  6365. (or split (end-of-line 1))
  6366. (delete-horizontal-space)
  6367. (if (string-match "\\`\\*+\\'"
  6368. (buffer-substring (point-at-bol) (point)))
  6369. (insert " "))
  6370. (newline (if blank 2 1))
  6371. (when tags
  6372. (save-excursion
  6373. (goto-char pos)
  6374. (end-of-line 1)
  6375. (insert " " tags)
  6376. (org-set-tags nil 'align))))
  6377. (t
  6378. (or split (end-of-line 1))
  6379. (newline (if blank 2 1)))))))
  6380. (insert head) (just-one-space)
  6381. (setq pos (point))
  6382. (end-of-line 1)
  6383. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6384. (when (and org-insert-heading-respect-content hide-previous)
  6385. (save-excursion
  6386. (goto-char previous-pos)
  6387. (hide-subtree)))
  6388. (run-hooks 'org-insert-heading-hook)))))
  6389. (defun org-get-heading (&optional no-tags no-todo)
  6390. "Return the heading of the current entry, without the stars.
  6391. When NO-TAGS is non-nil, don't include tags.
  6392. When NO-TODO is non-nil, don't include TODO keywords."
  6393. (save-excursion
  6394. (org-back-to-heading t)
  6395. (cond
  6396. ((and no-tags no-todo)
  6397. (looking-at org-complex-heading-regexp)
  6398. (match-string 4))
  6399. (no-tags
  6400. (looking-at (concat org-outline-regexp
  6401. "\\(.*?\\)"
  6402. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6403. (match-string 1))
  6404. (no-todo
  6405. (looking-at org-todo-line-regexp)
  6406. (match-string 3))
  6407. (t (looking-at org-heading-regexp)
  6408. (match-string 2)))))
  6409. (defun org-heading-components ()
  6410. "Return the components of the current heading.
  6411. This is a list with the following elements:
  6412. - the level as an integer
  6413. - the reduced level, different if `org-odd-levels-only' is set.
  6414. - the TODO keyword, or nil
  6415. - the priority character, like ?A, or nil if no priority is given
  6416. - the headline text itself, or the tags string if no headline text
  6417. - the tags string, or nil."
  6418. (save-excursion
  6419. (org-back-to-heading t)
  6420. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6421. (list (length (match-string 1))
  6422. (org-reduced-level (length (match-string 1)))
  6423. (org-match-string-no-properties 2)
  6424. (and (match-end 3) (aref (match-string 3) 2))
  6425. (org-match-string-no-properties 4)
  6426. (org-match-string-no-properties 5)))))
  6427. (defun org-get-entry ()
  6428. "Get the entry text, after heading, entire subtree."
  6429. (save-excursion
  6430. (org-back-to-heading t)
  6431. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6432. (defun org-insert-heading-after-current ()
  6433. "Insert a new heading with same level as current, after current subtree."
  6434. (interactive)
  6435. (org-back-to-heading)
  6436. (org-insert-heading)
  6437. (org-move-subtree-down)
  6438. (end-of-line 1))
  6439. (defun org-insert-heading-respect-content ()
  6440. (interactive)
  6441. (let ((org-insert-heading-respect-content t))
  6442. (org-insert-heading t)))
  6443. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6444. (interactive "P")
  6445. (let ((org-insert-heading-respect-content t))
  6446. (org-insert-todo-heading force-state t)))
  6447. (defun org-insert-todo-heading (arg &optional force-heading)
  6448. "Insert a new heading with the same level and TODO state as current heading.
  6449. If the heading has no TODO state, or if the state is DONE, use the first
  6450. state (TODO by default). Also with prefix arg, force first state."
  6451. (interactive "P")
  6452. (when (or force-heading (not (org-insert-item 'checkbox)))
  6453. (org-insert-heading force-heading)
  6454. (save-excursion
  6455. (org-back-to-heading)
  6456. (outline-previous-heading)
  6457. (looking-at org-todo-line-regexp))
  6458. (let*
  6459. ((new-mark-x
  6460. (if (or arg
  6461. (not (match-beginning 2))
  6462. (member (match-string 2) org-done-keywords))
  6463. (car org-todo-keywords-1)
  6464. (match-string 2)))
  6465. (new-mark
  6466. (or
  6467. (run-hook-with-args-until-success
  6468. 'org-todo-get-default-hook new-mark-x nil)
  6469. new-mark-x)))
  6470. (beginning-of-line 1)
  6471. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6472. (if org-treat-insert-todo-heading-as-state-change
  6473. (org-todo new-mark)
  6474. (insert new-mark " "))))
  6475. (when org-provide-todo-statistics
  6476. (org-update-parent-todo-statistics))))
  6477. (defun org-insert-subheading (arg)
  6478. "Insert a new subheading and demote it.
  6479. Works for outline headings and for plain lists alike."
  6480. (interactive "P")
  6481. (org-insert-heading arg)
  6482. (cond
  6483. ((org-on-heading-p) (org-do-demote))
  6484. ((org-at-item-p) (org-indent-item))))
  6485. (defun org-insert-todo-subheading (arg)
  6486. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6487. Works for outline headings and for plain lists alike."
  6488. (interactive "P")
  6489. (org-insert-todo-heading arg)
  6490. (cond
  6491. ((org-on-heading-p) (org-do-demote))
  6492. ((org-at-item-p) (org-indent-item))))
  6493. ;;; Promotion and Demotion
  6494. (defvar org-after-demote-entry-hook nil
  6495. "Hook run after an entry has been demoted.
  6496. The cursor will be at the beginning of the entry.
  6497. When a subtree is being demoted, the hook will be called for each node.")
  6498. (defvar org-after-promote-entry-hook nil
  6499. "Hook run after an entry has been promoted.
  6500. The cursor will be at the beginning of the entry.
  6501. When a subtree is being promoted, the hook will be called for each node.")
  6502. (defun org-promote-subtree ()
  6503. "Promote the entire subtree.
  6504. See also `org-promote'."
  6505. (interactive)
  6506. (save-excursion
  6507. (org-with-limited-levels (org-map-tree 'org-promote)))
  6508. (org-fix-position-after-promote))
  6509. (defun org-demote-subtree ()
  6510. "Demote the entire subtree. See `org-demote'.
  6511. See also `org-promote'."
  6512. (interactive)
  6513. (save-excursion
  6514. (org-with-limited-levels (org-map-tree 'org-demote)))
  6515. (org-fix-position-after-promote))
  6516. (defun org-do-promote ()
  6517. "Promote the current heading higher up the tree.
  6518. If the region is active in `transient-mark-mode', promote all headings
  6519. in the region."
  6520. (interactive)
  6521. (save-excursion
  6522. (if (org-region-active-p)
  6523. (org-map-region 'org-promote (region-beginning) (region-end))
  6524. (org-promote)))
  6525. (org-fix-position-after-promote))
  6526. (defun org-do-demote ()
  6527. "Demote the current heading lower down the tree.
  6528. If the region is active in `transient-mark-mode', demote all headings
  6529. in the region."
  6530. (interactive)
  6531. (save-excursion
  6532. (if (org-region-active-p)
  6533. (org-map-region 'org-demote (region-beginning) (region-end))
  6534. (org-demote)))
  6535. (org-fix-position-after-promote))
  6536. (defun org-fix-position-after-promote ()
  6537. "Make sure that after pro/demotion cursor position is right."
  6538. (let ((pos (point)))
  6539. (when (save-excursion
  6540. (beginning-of-line 1)
  6541. (looking-at org-todo-line-regexp)
  6542. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6543. (cond ((eobp) (insert " "))
  6544. ((eolp) (insert " "))
  6545. ((equal (char-after) ?\ ) (forward-char 1))))))
  6546. (defun org-current-level ()
  6547. "Return the level of the current entry, or nil if before the first headline.
  6548. The level is the number of stars at the beginning of the headline."
  6549. (save-excursion
  6550. (org-with-limited-levels
  6551. (if (ignore-errors (org-back-to-heading t))
  6552. (funcall outline-level)))))
  6553. (defun org-get-previous-line-level ()
  6554. "Return the outline depth of the last headline before the current line.
  6555. Returns 0 for the first headline in the buffer, and nil if before the
  6556. first headline."
  6557. (let ((current-level (org-current-level))
  6558. (prev-level (when (> (line-number-at-pos) 1)
  6559. (save-excursion
  6560. (beginning-of-line 0)
  6561. (org-current-level)))))
  6562. (cond ((null current-level) nil) ; Before first headline
  6563. ((null prev-level) 0) ; At first headline
  6564. (prev-level))))
  6565. (defun org-reduced-level (l)
  6566. "Compute the effective level of a heading.
  6567. This takes into account the setting of `org-odd-levels-only'."
  6568. (cond
  6569. ((zerop l) 0)
  6570. (org-odd-levels-only (1+ (floor (/ l 2))))
  6571. (t l)))
  6572. (defun org-level-increment ()
  6573. "Return the number of stars that will be added or removed at a
  6574. time to headlines when structure editing, based on the value of
  6575. `org-odd-levels-only'."
  6576. (if org-odd-levels-only 2 1))
  6577. (defun org-get-valid-level (level &optional change)
  6578. "Rectify a level change under the influence of `org-odd-levels-only'
  6579. LEVEL is a current level, CHANGE is by how much the level should be
  6580. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6581. even level numbers will become the next higher odd number."
  6582. (if org-odd-levels-only
  6583. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6584. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6585. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6586. (max 1 (+ level (or change 0)))))
  6587. (if (boundp 'define-obsolete-function-alias)
  6588. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6589. (define-obsolete-function-alias 'org-get-legal-level
  6590. 'org-get-valid-level)
  6591. (define-obsolete-function-alias 'org-get-legal-level
  6592. 'org-get-valid-level "23.1")))
  6593. (defun org-promote ()
  6594. "Promote the current heading higher up the tree.
  6595. If the region is active in `transient-mark-mode', promote all headings
  6596. in the region."
  6597. (org-back-to-heading t)
  6598. (let* ((level (save-match-data (funcall outline-level)))
  6599. (after-change-functions (remove 'flyspell-after-change-function
  6600. after-change-functions))
  6601. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6602. (diff (abs (- level (length up-head) -1))))
  6603. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6604. (replace-match up-head nil t)
  6605. ;; Fixup tag positioning
  6606. (and org-auto-align-tags (org-set-tags nil t))
  6607. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6608. (run-hooks 'org-after-promote-entry-hook)))
  6609. (defun org-demote ()
  6610. "Demote the current heading lower down the tree.
  6611. If the region is active in `transient-mark-mode', demote all headings
  6612. in the region."
  6613. (org-back-to-heading t)
  6614. (let* ((level (save-match-data (funcall outline-level)))
  6615. (after-change-functions (remove 'flyspell-after-change-function
  6616. after-change-functions))
  6617. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6618. (diff (abs (- level (length down-head) -1))))
  6619. (replace-match down-head nil t)
  6620. ;; Fixup tag positioning
  6621. (and org-auto-align-tags (org-set-tags nil t))
  6622. (if org-adapt-indentation (org-fixup-indentation diff))
  6623. (run-hooks 'org-after-demote-entry-hook)))
  6624. (defun org-cycle-level ()
  6625. "Cycle the level of an empty headline through possible states.
  6626. This goes first to child, then to parent, level, then up the hierarchy.
  6627. After top level, it switches back to sibling level."
  6628. (interactive)
  6629. (let ((org-adapt-indentation nil))
  6630. (when (org-point-at-end-of-empty-headline)
  6631. (setq this-command 'org-cycle-level) ; Only needed for caching
  6632. (let ((cur-level (org-current-level))
  6633. (prev-level (org-get-previous-line-level)))
  6634. (cond
  6635. ;; If first headline in file, promote to top-level.
  6636. ((= prev-level 0)
  6637. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6638. do (org-do-promote)))
  6639. ;; If same level as prev, demote one.
  6640. ((= prev-level cur-level)
  6641. (org-do-demote))
  6642. ;; If parent is top-level, promote to top level if not already.
  6643. ((= prev-level 1)
  6644. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6645. do (org-do-promote)))
  6646. ;; If top-level, return to prev-level.
  6647. ((= cur-level 1)
  6648. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6649. do (org-do-demote)))
  6650. ;; If less than prev-level, promote one.
  6651. ((< cur-level prev-level)
  6652. (org-do-promote))
  6653. ;; If deeper than prev-level, promote until higher than
  6654. ;; prev-level.
  6655. ((> cur-level prev-level)
  6656. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6657. do (org-do-promote))))
  6658. t))))
  6659. (defun org-map-tree (fun)
  6660. "Call FUN for every heading underneath the current one."
  6661. (org-back-to-heading)
  6662. (let ((level (funcall outline-level)))
  6663. (save-excursion
  6664. (funcall fun)
  6665. (while (and (progn
  6666. (outline-next-heading)
  6667. (> (funcall outline-level) level))
  6668. (not (eobp)))
  6669. (funcall fun)))))
  6670. (defun org-map-region (fun beg end)
  6671. "Call FUN for every heading between BEG and END."
  6672. (let ((org-ignore-region t))
  6673. (save-excursion
  6674. (setq end (copy-marker end))
  6675. (goto-char beg)
  6676. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6677. (< (point) end))
  6678. (funcall fun))
  6679. (while (and (progn
  6680. (outline-next-heading)
  6681. (< (point) end))
  6682. (not (eobp)))
  6683. (funcall fun)))))
  6684. (defvar org-property-end-re) ; silence byte-compiler
  6685. (defun org-fixup-indentation (diff)
  6686. "Change the indentation in the current entry by DIFF.
  6687. However, if any line in the current entry has no indentation, or if it
  6688. would end up with no indentation after the change, nothing at all is done."
  6689. (save-excursion
  6690. (let ((end (save-excursion (outline-next-heading)
  6691. (point-marker)))
  6692. (prohibit (if (> diff 0)
  6693. "^\\S-"
  6694. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6695. col)
  6696. (unless (save-excursion (end-of-line 1)
  6697. (re-search-forward prohibit end t))
  6698. (while (and (< (point) end)
  6699. (re-search-forward "^[ \t]+" end t))
  6700. (goto-char (match-end 0))
  6701. (setq col (current-column))
  6702. (if (< diff 0) (replace-match ""))
  6703. (org-indent-to-column (+ diff col))))
  6704. (move-marker end nil))))
  6705. (defun org-convert-to-odd-levels ()
  6706. "Convert an org-mode file with all levels allowed to one with odd levels.
  6707. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6708. level 5 etc."
  6709. (interactive)
  6710. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6711. (let ((outline-level 'org-outline-level)
  6712. (org-odd-levels-only nil) n)
  6713. (save-excursion
  6714. (goto-char (point-min))
  6715. (while (re-search-forward "^\\*\\*+ " nil t)
  6716. (setq n (- (length (match-string 0)) 2))
  6717. (while (>= (setq n (1- n)) 0)
  6718. (org-demote))
  6719. (end-of-line 1))))))
  6720. (defun org-convert-to-oddeven-levels ()
  6721. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6722. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6723. file contains a section with an even level, conversion would
  6724. destroy the structure of the file. An error is signaled in this
  6725. case."
  6726. (interactive)
  6727. (goto-char (point-min))
  6728. ;; First check if there are no even levels
  6729. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6730. (org-show-context t)
  6731. (error "Not all levels are odd in this file. Conversion not possible"))
  6732. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6733. (let ((outline-regexp org-outline-regexp)
  6734. (outline-level 'org-outline-level)
  6735. (org-odd-levels-only nil) n)
  6736. (save-excursion
  6737. (goto-char (point-min))
  6738. (while (re-search-forward "^\\*\\*+ " nil t)
  6739. (setq n (/ (1- (length (match-string 0))) 2))
  6740. (while (>= (setq n (1- n)) 0)
  6741. (org-promote))
  6742. (end-of-line 1))))))
  6743. (defun org-tr-level (n)
  6744. "Make N odd if required."
  6745. (if org-odd-levels-only (1+ (/ n 2)) n))
  6746. ;;; Vertical tree motion, cutting and pasting of subtrees
  6747. (defun org-move-subtree-up (&optional arg)
  6748. "Move the current subtree up past ARG headlines of the same level."
  6749. (interactive "p")
  6750. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6751. (defun org-move-subtree-down (&optional arg)
  6752. "Move the current subtree down past ARG headlines of the same level."
  6753. (interactive "p")
  6754. (setq arg (prefix-numeric-value arg))
  6755. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6756. 'org-get-last-sibling))
  6757. (ins-point (make-marker))
  6758. (cnt (abs arg))
  6759. (col (current-column))
  6760. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6761. ;; Select the tree
  6762. (org-back-to-heading)
  6763. (setq beg0 (point))
  6764. (save-excursion
  6765. (setq ne-beg (org-back-over-empty-lines))
  6766. (setq beg (point)))
  6767. (save-match-data
  6768. (save-excursion (outline-end-of-heading)
  6769. (setq folded (outline-invisible-p)))
  6770. (outline-end-of-subtree))
  6771. (outline-next-heading)
  6772. (setq ne-end (org-back-over-empty-lines))
  6773. (setq end (point))
  6774. (goto-char beg0)
  6775. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6776. ;; include less whitespace
  6777. (save-excursion
  6778. (goto-char beg)
  6779. (forward-line (- ne-beg ne-end))
  6780. (setq beg (point))))
  6781. ;; Find insertion point, with error handling
  6782. (while (> cnt 0)
  6783. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6784. (progn (goto-char beg0)
  6785. (error "Cannot move past superior level or buffer limit")))
  6786. (setq cnt (1- cnt)))
  6787. (if (> arg 0)
  6788. ;; Moving forward - still need to move over subtree
  6789. (progn (org-end-of-subtree t t)
  6790. (save-excursion
  6791. (org-back-over-empty-lines)
  6792. (or (bolp) (newline)))))
  6793. (setq ne-ins (org-back-over-empty-lines))
  6794. (move-marker ins-point (point))
  6795. (setq txt (buffer-substring beg end))
  6796. (org-save-markers-in-region beg end)
  6797. (delete-region beg end)
  6798. (org-remove-empty-overlays-at beg)
  6799. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6800. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6801. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6802. (let ((bbb (point)))
  6803. (insert-before-markers txt)
  6804. (org-reinstall-markers-in-region bbb)
  6805. (move-marker ins-point bbb))
  6806. (or (bolp) (insert "\n"))
  6807. (setq ins-end (point))
  6808. (goto-char ins-point)
  6809. (org-skip-whitespace)
  6810. (when (and (< arg 0)
  6811. (org-first-sibling-p)
  6812. (> ne-ins ne-beg))
  6813. ;; Move whitespace back to beginning
  6814. (save-excursion
  6815. (goto-char ins-end)
  6816. (let ((kill-whole-line t))
  6817. (kill-line (- ne-ins ne-beg)) (point)))
  6818. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6819. (move-marker ins-point nil)
  6820. (if folded
  6821. (hide-subtree)
  6822. (org-show-entry)
  6823. (show-children)
  6824. (org-cycle-hide-drawers 'children))
  6825. (org-clean-visibility-after-subtree-move)
  6826. ;; move back to the initial column we were at
  6827. (move-to-column col)))
  6828. (defvar org-subtree-clip ""
  6829. "Clipboard for cut and paste of subtrees.
  6830. This is actually only a copy of the kill, because we use the normal kill
  6831. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6832. (defvar org-subtree-clip-folded nil
  6833. "Was the last copied subtree folded?
  6834. This is used to fold the tree back after pasting.")
  6835. (defun org-cut-subtree (&optional n)
  6836. "Cut the current subtree into the clipboard.
  6837. With prefix arg N, cut this many sequential subtrees.
  6838. This is a short-hand for marking the subtree and then cutting it."
  6839. (interactive "p")
  6840. (org-copy-subtree n 'cut))
  6841. (defun org-copy-subtree (&optional n cut force-store-markers)
  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 copying it.
  6845. If CUT is non-nil, actually cut the subtree.
  6846. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6847. of some markers in the region, even if CUT is non-nil. This is
  6848. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6849. (interactive "p")
  6850. (let (beg end folded (beg0 (point)))
  6851. (if (org-called-interactively-p 'any)
  6852. (org-back-to-heading nil) ; take what looks like a subtree
  6853. (org-back-to-heading t)) ; take what is really there
  6854. (org-back-over-empty-lines)
  6855. (setq beg (point))
  6856. (skip-chars-forward " \t\r\n")
  6857. (save-match-data
  6858. (save-excursion (outline-end-of-heading)
  6859. (setq folded (outline-invisible-p)))
  6860. (condition-case nil
  6861. (org-forward-same-level (1- n) t)
  6862. (error nil))
  6863. (org-end-of-subtree t t))
  6864. (org-back-over-empty-lines)
  6865. (setq end (point))
  6866. (goto-char beg0)
  6867. (when (> end beg)
  6868. (setq org-subtree-clip-folded folded)
  6869. (when (or cut force-store-markers)
  6870. (org-save-markers-in-region beg end))
  6871. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6872. (setq org-subtree-clip (current-kill 0))
  6873. (message "%s: Subtree(s) with %d characters"
  6874. (if cut "Cut" "Copied")
  6875. (length org-subtree-clip)))))
  6876. (defun org-paste-subtree (&optional level tree for-yank)
  6877. "Paste the clipboard as a subtree, with modification of headline level.
  6878. The entire subtree is promoted or demoted in order to match a new headline
  6879. level.
  6880. If the cursor is at the beginning of a headline, the same level as
  6881. that headline is used to paste the tree
  6882. If not, the new level is derived from the *visible* headings
  6883. before and after the insertion point, and taken to be the inferior headline
  6884. level of the two. So if the previous visible heading is level 3 and the
  6885. next is level 4 (or vice versa), level 4 will be used for insertion.
  6886. This makes sure that the subtree remains an independent subtree and does
  6887. not swallow low level entries.
  6888. You can also force a different level, either by using a numeric prefix
  6889. argument, or by inserting the heading marker by hand. For example, if the
  6890. cursor is after \"*****\", then the tree will be shifted to level 5.
  6891. If optional TREE is given, use this text instead of the kill ring.
  6892. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6893. move back over whitespace before inserting, and move point to the end of
  6894. the inserted text when done."
  6895. (interactive "P")
  6896. (setq tree (or tree (and kill-ring (current-kill 0))))
  6897. (unless (org-kill-is-subtree-p tree)
  6898. (error "%s"
  6899. (substitute-command-keys
  6900. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6901. (org-with-limited-levels
  6902. (let* ((visp (not (outline-invisible-p)))
  6903. (txt tree)
  6904. (^re_ "\\(\\*+\\)[ \t]*")
  6905. (old-level (if (string-match org-outline-regexp-bol txt)
  6906. (- (match-end 0) (match-beginning 0) 1)
  6907. -1))
  6908. (force-level (cond (level (prefix-numeric-value level))
  6909. ((and (looking-at "[ \t]*$")
  6910. (string-match
  6911. "^\\*+$" (buffer-substring
  6912. (point-at-bol) (point))))
  6913. (- (match-end 1) (match-beginning 1)))
  6914. ((and (bolp)
  6915. (looking-at org-outline-regexp))
  6916. (- (match-end 0) (point) 1))
  6917. (t nil)))
  6918. (previous-level (save-excursion
  6919. (condition-case nil
  6920. (progn
  6921. (outline-previous-visible-heading 1)
  6922. (if (looking-at ^re_)
  6923. (- (match-end 0) (match-beginning 0) 1)
  6924. 1))
  6925. (error 1))))
  6926. (next-level (save-excursion
  6927. (condition-case nil
  6928. (progn
  6929. (or (looking-at org-outline-regexp)
  6930. (outline-next-visible-heading 1))
  6931. (if (looking-at ^re_)
  6932. (- (match-end 0) (match-beginning 0) 1)
  6933. 1))
  6934. (error 1))))
  6935. (new-level (or force-level (max previous-level next-level)))
  6936. (shift (if (or (= old-level -1)
  6937. (= new-level -1)
  6938. (= old-level new-level))
  6939. 0
  6940. (- new-level old-level)))
  6941. (delta (if (> shift 0) -1 1))
  6942. (func (if (> shift 0) 'org-demote 'org-promote))
  6943. (org-odd-levels-only nil)
  6944. beg end newend)
  6945. ;; Remove the forced level indicator
  6946. (if force-level
  6947. (delete-region (point-at-bol) (point)))
  6948. ;; Paste
  6949. (beginning-of-line (if (bolp) 1 2))
  6950. (unless for-yank (org-back-over-empty-lines))
  6951. (setq beg (point))
  6952. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6953. (insert-before-markers txt)
  6954. (unless (string-match "\n\\'" txt) (insert "\n"))
  6955. (setq newend (point))
  6956. (org-reinstall-markers-in-region beg)
  6957. (setq end (point))
  6958. (goto-char beg)
  6959. (skip-chars-forward " \t\n\r")
  6960. (setq beg (point))
  6961. (if (and (outline-invisible-p) visp)
  6962. (save-excursion (outline-show-heading)))
  6963. ;; Shift if necessary
  6964. (unless (= shift 0)
  6965. (save-restriction
  6966. (narrow-to-region beg end)
  6967. (while (not (= shift 0))
  6968. (org-map-region func (point-min) (point-max))
  6969. (setq shift (+ delta shift)))
  6970. (goto-char (point-min))
  6971. (setq newend (point-max))))
  6972. (when (or (org-called-interactively-p 'interactive) for-yank)
  6973. (message "Clipboard pasted as level %d subtree" new-level))
  6974. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6975. kill-ring
  6976. (eq org-subtree-clip (current-kill 0))
  6977. org-subtree-clip-folded)
  6978. ;; The tree was folded before it was killed/copied
  6979. (hide-subtree))
  6980. (and for-yank (goto-char newend)))))
  6981. (defun org-kill-is-subtree-p (&optional txt)
  6982. "Check if the current kill is an outline subtree, or a set of trees.
  6983. Returns nil if kill does not start with a headline, or if the first
  6984. headline level is not the largest headline level in the tree.
  6985. So this will actually accept several entries of equal levels as well,
  6986. which is OK for `org-paste-subtree'.
  6987. If optional TXT is given, check this string instead of the current kill."
  6988. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6989. (re (org-get-limited-outline-regexp))
  6990. (^re (concat "^" re))
  6991. (start-level (and kill
  6992. (string-match
  6993. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  6994. kill)
  6995. (- (match-end 2) (match-beginning 2) 1)))
  6996. (start (1+ (or (match-beginning 2) -1))))
  6997. (if (not start-level)
  6998. (progn
  6999. nil) ;; does not even start with a heading
  7000. (catch 'exit
  7001. (while (setq start (string-match ^re kill (1+ start)))
  7002. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7003. (throw 'exit nil)))
  7004. t))))
  7005. (defvar org-markers-to-move nil
  7006. "Markers that should be moved with a cut-and-paste operation.
  7007. Those markers are stored together with their positions relative to
  7008. the start of the region.")
  7009. (defun org-save-markers-in-region (beg end)
  7010. "Check markers in region.
  7011. If these markers are between BEG and END, record their position relative
  7012. to BEG, so that after moving the block of text, we can put the markers back
  7013. into place.
  7014. This function gets called just before an entry or tree gets cut from the
  7015. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7016. called immediately, to move the markers with the entries."
  7017. (setq org-markers-to-move nil)
  7018. (when (featurep 'org-clock)
  7019. (org-clock-save-markers-for-cut-and-paste beg end))
  7020. (when (featurep 'org-agenda)
  7021. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7022. (defun org-check-and-save-marker (marker beg end)
  7023. "Check if MARKER is between BEG and END.
  7024. If yes, remember the marker and the distance to BEG."
  7025. (when (and (marker-buffer marker)
  7026. (equal (marker-buffer marker) (current-buffer)))
  7027. (if (and (>= marker beg) (< marker end))
  7028. (push (cons marker (- marker beg)) org-markers-to-move))))
  7029. (defun org-reinstall-markers-in-region (beg)
  7030. "Move all remembered markers to their position relative to BEG."
  7031. (mapc (lambda (x)
  7032. (move-marker (car x) (+ beg (cdr x))))
  7033. org-markers-to-move)
  7034. (setq org-markers-to-move nil))
  7035. (defun org-narrow-to-subtree ()
  7036. "Narrow buffer to the current subtree."
  7037. (interactive)
  7038. (save-excursion
  7039. (save-match-data
  7040. (org-with-limited-levels
  7041. (narrow-to-region
  7042. (progn (org-back-to-heading t) (point))
  7043. (progn (org-end-of-subtree t t)
  7044. (if (and (org-on-heading-p) (not (eobp))) (backward-char 1))
  7045. (point)))))))
  7046. (defun org-narrow-to-block ()
  7047. "Narrow buffer to the current block."
  7048. (interactive)
  7049. (let* ((case-fold-search t)
  7050. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7051. "^[ \t]*#\\+end_.*")))
  7052. (if blockp
  7053. (narrow-to-region (car blockp) (cdr blockp))
  7054. (error "Not in a block"))))
  7055. (eval-when-compile
  7056. (defvar org-property-drawer-re))
  7057. (defvar org-property-start-re) ;; defined below
  7058. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7059. "Clone the task (subtree) at point N times.
  7060. The clones will be inserted as siblings.
  7061. In interactive use, the user will be prompted for the number of
  7062. clones to be produced, and for a time SHIFT, which may be a
  7063. repeater as used in time stamps, for example `+3d'.
  7064. When a valid repeater is given and the entry contains any time
  7065. stamps, the clones will become a sequence in time, with time
  7066. stamps in the subtree shifted for each clone produced. If SHIFT
  7067. is nil or the empty string, time stamps will be left alone. The
  7068. ID property of the original subtree is removed.
  7069. If the original subtree did contain time stamps with a repeater,
  7070. the following will happen:
  7071. - the repeater will be removed in each clone
  7072. - an additional clone will be produced, with the current, unshifted
  7073. date(s) in the entry.
  7074. - the original entry will be placed *after* all the clones, with
  7075. repeater intact.
  7076. - the start days in the repeater in the original entry will be shifted
  7077. to past the last clone.
  7078. I this way you can spell out a number of instances of a repeating task,
  7079. and still retain the repeater to cover future instances of the task."
  7080. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7081. (let (beg end template task idprop
  7082. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7083. (drawer-re org-drawer-regexp))
  7084. (if (not (and (integerp n) (> n 0)))
  7085. (error "Invalid number of replications %s" n))
  7086. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7087. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  7088. shift)))
  7089. (error "Invalid shift specification %s" shift))
  7090. (when doshift
  7091. (setq shift-n (string-to-number (match-string 1 shift))
  7092. shift-what (cdr (assoc (match-string 2 shift)
  7093. '(("d" . day) ("w" . week)
  7094. ("m" . month) ("y" . year))))))
  7095. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7096. (setq nmin 1 nmax n)
  7097. (org-back-to-heading t)
  7098. (setq beg (point))
  7099. (setq idprop (org-entry-get nil "ID"))
  7100. (org-end-of-subtree t t)
  7101. (or (bolp) (insert "\n"))
  7102. (setq end (point))
  7103. (setq template (buffer-substring beg end))
  7104. (when (and doshift
  7105. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  7106. (delete-region beg end)
  7107. (setq end beg)
  7108. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7109. (goto-char end)
  7110. (loop for n from nmin to nmax do
  7111. ;; prepare clone
  7112. (with-temp-buffer
  7113. (insert template)
  7114. (org-mode)
  7115. (goto-char (point-min))
  7116. (org-show-subtree)
  7117. (and idprop (if org-clone-delete-id
  7118. (org-entry-delete nil "ID")
  7119. (org-id-get-create t)))
  7120. (unless (= n 0)
  7121. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7122. (kill-whole-line))
  7123. (goto-char (point-min))
  7124. (while (re-search-forward drawer-re nil t)
  7125. (mapc (lambda (d)
  7126. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7127. (goto-char (point-min))
  7128. (when doshift
  7129. (while (re-search-forward org-ts-regexp-both nil t)
  7130. (org-timestamp-change (* n shift-n) shift-what))
  7131. (unless (= n n-no-remove)
  7132. (goto-char (point-min))
  7133. (while (re-search-forward org-ts-regexp nil t)
  7134. (save-excursion
  7135. (goto-char (match-beginning 0))
  7136. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  7137. (delete-region (match-beginning 1) (match-end 1)))))))
  7138. (setq task (buffer-string)))
  7139. (insert task))
  7140. (goto-char beg)))
  7141. ;;; Outline Sorting
  7142. (defun org-sort (with-case)
  7143. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7144. Optional argument WITH-CASE means sort case-sensitively.
  7145. With a double prefix argument, also remove duplicate entries."
  7146. (interactive "P")
  7147. (cond
  7148. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7149. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7150. (t
  7151. (org-call-with-arg 'org-sort-entries with-case))))
  7152. (defun org-sort-remove-invisible (s)
  7153. (remove-text-properties 0 (length s) org-rm-props s)
  7154. (while (string-match org-bracket-link-regexp s)
  7155. (setq s (replace-match (if (match-end 2)
  7156. (match-string 3 s)
  7157. (match-string 1 s)) t t s)))
  7158. s)
  7159. (defvar org-priority-regexp) ; defined later in the file
  7160. (defvar org-after-sorting-entries-or-items-hook nil
  7161. "Hook that is run after a bunch of entries or items have been sorted.
  7162. When children are sorted, the cursor is in the parent line when this
  7163. hook gets called. When a region or a plain list is sorted, the cursor
  7164. will be in the first entry of the sorted region/list.")
  7165. (defun org-sort-entries
  7166. (&optional with-case sorting-type getkey-func compare-func property)
  7167. "Sort entries on a certain level of an outline tree.
  7168. If there is an active region, the entries in the region are sorted.
  7169. Else, if the cursor is before the first entry, sort the top-level items.
  7170. Else, the children of the entry at point are sorted.
  7171. Sorting can be alphabetically, numerically, by date/time as given by
  7172. a time stamp, by a property or by priority.
  7173. The command prompts for the sorting type unless it has been given to the
  7174. function through the SORTING-TYPE argument, which needs to be a character,
  7175. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  7176. precise meaning of each character:
  7177. n Numerically, by converting the beginning of the entry/item to a number.
  7178. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7179. t By date/time, either the first active time stamp in the entry, or, if
  7180. none exist, by the first inactive one.
  7181. s By the scheduled date/time.
  7182. d By deadline date/time.
  7183. c By creation time, which is assumed to be the first inactive time stamp
  7184. at the beginning of a line.
  7185. p By priority according to the cookie.
  7186. r By the value of a property.
  7187. Capital letters will reverse the sort order.
  7188. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7189. called with point at the beginning of the record. It must return either
  7190. a string or a number that should serve as the sorting key for that record.
  7191. Comparing entries ignores case by default. However, with an optional argument
  7192. WITH-CASE, the sorting considers case as well."
  7193. (interactive "P")
  7194. (let ((case-func (if with-case 'identity 'downcase))
  7195. start beg end stars re re2
  7196. txt what tmp)
  7197. ;; Find beginning and end of region to sort
  7198. (cond
  7199. ((org-region-active-p)
  7200. ;; we will sort the region
  7201. (setq end (region-end)
  7202. what "region")
  7203. (goto-char (region-beginning))
  7204. (if (not (org-on-heading-p)) (outline-next-heading))
  7205. (setq start (point)))
  7206. ((or (org-on-heading-p)
  7207. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7208. ;; we will sort the children of the current headline
  7209. (org-back-to-heading)
  7210. (setq start (point)
  7211. end (progn (org-end-of-subtree t t)
  7212. (or (bolp) (insert "\n"))
  7213. (org-back-over-empty-lines)
  7214. (point))
  7215. what "children")
  7216. (goto-char start)
  7217. (show-subtree)
  7218. (outline-next-heading))
  7219. (t
  7220. ;; we will sort the top-level entries in this file
  7221. (goto-char (point-min))
  7222. (or (org-on-heading-p) (outline-next-heading))
  7223. (setq start (point))
  7224. (goto-char (point-max))
  7225. (beginning-of-line 1)
  7226. (when (looking-at ".*?\\S-")
  7227. ;; File ends in a non-white line
  7228. (end-of-line 1)
  7229. (insert "\n"))
  7230. (setq end (point-max))
  7231. (setq what "top-level")
  7232. (goto-char start)
  7233. (show-all)))
  7234. (setq beg (point))
  7235. (if (>= beg end) (error "Nothing to sort"))
  7236. (looking-at "\\(\\*+\\)")
  7237. (setq stars (match-string 1)
  7238. re (concat "^" (regexp-quote stars) " +")
  7239. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7240. txt (buffer-substring beg end))
  7241. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7242. (if (and (not (equal stars "*")) (string-match re2 txt))
  7243. (error "Region to sort contains a level above the first entry"))
  7244. (unless sorting-type
  7245. (message
  7246. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7247. [t]ime [s]cheduled [d]eadline [c]reated
  7248. A/N/T/S/D/C/P/O/F means reversed:"
  7249. what)
  7250. (setq sorting-type (read-char-exclusive))
  7251. (and (= (downcase sorting-type) ?f)
  7252. (setq getkey-func
  7253. (org-icompleting-read "Sort using function: "
  7254. obarray 'fboundp t nil nil))
  7255. (setq getkey-func (intern getkey-func)))
  7256. (and (= (downcase sorting-type) ?r)
  7257. (setq property
  7258. (org-icompleting-read "Property: "
  7259. (mapcar 'list (org-buffer-property-keys t))
  7260. nil t))))
  7261. (message "Sorting entries...")
  7262. (save-restriction
  7263. (narrow-to-region start end)
  7264. (let ((dcst (downcase sorting-type))
  7265. (case-fold-search nil)
  7266. (now (current-time)))
  7267. (sort-subr
  7268. (/= dcst sorting-type)
  7269. ;; This function moves to the beginning character of the "record" to
  7270. ;; be sorted.
  7271. (lambda nil
  7272. (if (re-search-forward re nil t)
  7273. (goto-char (match-beginning 0))
  7274. (goto-char (point-max))))
  7275. ;; This function moves to the last character of the "record" being
  7276. ;; sorted.
  7277. (lambda nil
  7278. (save-match-data
  7279. (condition-case nil
  7280. (outline-forward-same-level 1)
  7281. (error
  7282. (goto-char (point-max))))))
  7283. ;; This function returns the value that gets sorted against.
  7284. (lambda nil
  7285. (cond
  7286. ((= dcst ?n)
  7287. (if (looking-at org-complex-heading-regexp)
  7288. (string-to-number (match-string 4))
  7289. nil))
  7290. ((= dcst ?a)
  7291. (if (looking-at org-complex-heading-regexp)
  7292. (funcall case-func (match-string 4))
  7293. nil))
  7294. ((= dcst ?t)
  7295. (let ((end (save-excursion (outline-next-heading) (point))))
  7296. (if (or (re-search-forward org-ts-regexp end t)
  7297. (re-search-forward org-ts-regexp-both end t))
  7298. (org-time-string-to-seconds (match-string 0))
  7299. (org-float-time now))))
  7300. ((= dcst ?c)
  7301. (let ((end (save-excursion (outline-next-heading) (point))))
  7302. (if (re-search-forward
  7303. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7304. end t)
  7305. (org-time-string-to-seconds (match-string 0))
  7306. (org-float-time now))))
  7307. ((= dcst ?s)
  7308. (let ((end (save-excursion (outline-next-heading) (point))))
  7309. (if (re-search-forward org-scheduled-time-regexp end t)
  7310. (org-time-string-to-seconds (match-string 1))
  7311. (org-float-time now))))
  7312. ((= dcst ?d)
  7313. (let ((end (save-excursion (outline-next-heading) (point))))
  7314. (if (re-search-forward org-deadline-time-regexp end t)
  7315. (org-time-string-to-seconds (match-string 1))
  7316. (org-float-time now))))
  7317. ((= dcst ?p)
  7318. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7319. (string-to-char (match-string 2))
  7320. org-default-priority))
  7321. ((= dcst ?r)
  7322. (or (org-entry-get nil property) ""))
  7323. ((= dcst ?o)
  7324. (if (looking-at org-complex-heading-regexp)
  7325. (- 9999 (length (member (match-string 2)
  7326. org-todo-keywords-1)))))
  7327. ((= dcst ?f)
  7328. (if getkey-func
  7329. (progn
  7330. (setq tmp (funcall getkey-func))
  7331. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7332. tmp)
  7333. (error "Invalid key function `%s'" getkey-func)))
  7334. (t (error "Invalid sorting type `%c'" sorting-type))))
  7335. nil
  7336. (cond
  7337. ((= dcst ?a) 'string<)
  7338. ((= dcst ?f) compare-func)
  7339. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7340. (t nil)))))
  7341. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7342. (message "Sorting entries...done")))
  7343. (defun org-do-sort (table what &optional with-case sorting-type)
  7344. "Sort TABLE of WHAT according to SORTING-TYPE.
  7345. The user will be prompted for the SORTING-TYPE if the call to this
  7346. function does not specify it. WHAT is only for the prompt, to indicate
  7347. what is being sorted. The sorting key will be extracted from
  7348. the car of the elements of the table.
  7349. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7350. (unless sorting-type
  7351. (message
  7352. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7353. what)
  7354. (setq sorting-type (read-char-exclusive)))
  7355. (let ((dcst (downcase sorting-type))
  7356. extractfun comparefun)
  7357. ;; Define the appropriate functions
  7358. (cond
  7359. ((= dcst ?n)
  7360. (setq extractfun 'string-to-number
  7361. comparefun (if (= dcst sorting-type) '< '>)))
  7362. ((= dcst ?a)
  7363. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7364. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7365. comparefun (if (= dcst sorting-type)
  7366. 'string<
  7367. (lambda (a b) (and (not (string< a b))
  7368. (not (string= a b)))))))
  7369. ((= dcst ?t)
  7370. (setq extractfun
  7371. (lambda (x)
  7372. (if (or (string-match org-ts-regexp x)
  7373. (string-match org-ts-regexp-both x))
  7374. (org-float-time
  7375. (org-time-string-to-time (match-string 0 x)))
  7376. 0))
  7377. comparefun (if (= dcst sorting-type) '< '>)))
  7378. (t (error "Invalid sorting type `%c'" sorting-type)))
  7379. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7380. table)
  7381. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7382. ;;; The orgstruct minor mode
  7383. ;; Define a minor mode which can be used in other modes in order to
  7384. ;; integrate the org-mode structure editing commands.
  7385. ;; This is really a hack, because the org-mode structure commands use
  7386. ;; keys which normally belong to the major mode. Here is how it
  7387. ;; works: The minor mode defines all the keys necessary to operate the
  7388. ;; structure commands, but wraps the commands into a function which
  7389. ;; tests if the cursor is currently at a headline or a plain list
  7390. ;; item. If that is the case, the structure command is used,
  7391. ;; temporarily setting many Org-mode variables like regular
  7392. ;; expressions for filling etc. However, when any of those keys is
  7393. ;; used at a different location, function uses `key-binding' to look
  7394. ;; up if the key has an associated command in another currently active
  7395. ;; keymap (minor modes, major mode, global), and executes that
  7396. ;; command. There might be problems if any of the keys is otherwise
  7397. ;; used as a prefix key.
  7398. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7399. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7400. ;; addresses this by checking explicitly for both bindings.
  7401. (defvar orgstruct-mode-map (make-sparse-keymap)
  7402. "Keymap for the minor `orgstruct-mode'.")
  7403. (defvar org-local-vars nil
  7404. "List of local variables, for use by `orgstruct-mode'.")
  7405. ;;;###autoload
  7406. (define-minor-mode orgstruct-mode
  7407. "Toggle the minor mode `orgstruct-mode'.
  7408. This mode is for using Org-mode structure commands in other
  7409. modes. The following keys behave as if Org-mode were active, if
  7410. the cursor is on a headline, or on a plain list item (both as
  7411. defined by Org-mode).
  7412. M-up Move entry/item up
  7413. M-down Move entry/item down
  7414. M-left Promote
  7415. M-right Demote
  7416. M-S-up Move entry/item up
  7417. M-S-down Move entry/item down
  7418. M-S-left Promote subtree
  7419. M-S-right Demote subtree
  7420. M-q Fill paragraph and items like in Org-mode
  7421. C-c ^ Sort entries
  7422. C-c - Cycle list bullet
  7423. TAB Cycle item visibility
  7424. M-RET Insert new heading/item
  7425. S-M-RET Insert new TODO heading / Checkbox item
  7426. C-c C-c Set tags / toggle checkbox"
  7427. nil " OrgStruct" nil
  7428. (org-load-modules-maybe)
  7429. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7430. ;;;###autoload
  7431. (defun turn-on-orgstruct ()
  7432. "Unconditionally turn on `orgstruct-mode'."
  7433. (orgstruct-mode 1))
  7434. (defun orgstruct++-mode (&optional arg)
  7435. "Toggle `orgstruct-mode', the enhanced version of it.
  7436. In addition to setting orgstruct-mode, this also exports all indentation
  7437. and autofilling variables from org-mode into the buffer. It will also
  7438. recognize item context in multiline items.
  7439. Note that turning off orgstruct-mode will *not* remove the
  7440. indentation/paragraph settings. This can only be done by refreshing the
  7441. major mode, for example with \\[normal-mode]."
  7442. (interactive "P")
  7443. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7444. (if (< arg 1)
  7445. (orgstruct-mode -1)
  7446. (orgstruct-mode 1)
  7447. (let (var val)
  7448. (mapc
  7449. (lambda (x)
  7450. (when (string-match
  7451. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7452. (symbol-name (car x)))
  7453. (setq var (car x) val (nth 1 x))
  7454. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7455. org-local-vars)
  7456. (org-set-local 'orgstruct-is-++ t))))
  7457. (defvar orgstruct-is-++ nil
  7458. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7459. (make-variable-buffer-local 'orgstruct-is-++)
  7460. ;;;###autoload
  7461. (defun turn-on-orgstruct++ ()
  7462. "Unconditionally turn on `orgstruct++-mode'."
  7463. (orgstruct++-mode 1))
  7464. (defun orgstruct-error ()
  7465. "Error when there is no default binding for a structure key."
  7466. (interactive)
  7467. (error "This key has no function outside structure elements"))
  7468. (defun orgstruct-setup ()
  7469. "Setup orgstruct keymaps."
  7470. (let ((nfunc 0)
  7471. (bindings
  7472. (list
  7473. '([(meta up)] org-metaup)
  7474. '([(meta down)] org-metadown)
  7475. '([(meta left)] org-metaleft)
  7476. '([(meta right)] org-metaright)
  7477. '([(meta shift up)] org-shiftmetaup)
  7478. '([(meta shift down)] org-shiftmetadown)
  7479. '([(meta shift left)] org-shiftmetaleft)
  7480. '([(meta shift right)] org-shiftmetaright)
  7481. '([?\e (up)] org-metaup)
  7482. '([?\e (down)] org-metadown)
  7483. '([?\e (left)] org-metaleft)
  7484. '([?\e (right)] org-metaright)
  7485. '([?\e (shift up)] org-shiftmetaup)
  7486. '([?\e (shift down)] org-shiftmetadown)
  7487. '([?\e (shift left)] org-shiftmetaleft)
  7488. '([?\e (shift right)] org-shiftmetaright)
  7489. '([(shift up)] org-shiftup)
  7490. '([(shift down)] org-shiftdown)
  7491. '([(shift left)] org-shiftleft)
  7492. '([(shift right)] org-shiftright)
  7493. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7494. '("\M-q" fill-paragraph)
  7495. '("\C-c^" org-sort)
  7496. '("\C-c-" org-cycle-list-bullet)))
  7497. elt key fun cmd)
  7498. (while (setq elt (pop bindings))
  7499. (setq nfunc (1+ nfunc))
  7500. (setq key (org-key (car elt))
  7501. fun (nth 1 elt)
  7502. cmd (orgstruct-make-binding fun nfunc key))
  7503. (org-defkey orgstruct-mode-map key cmd))
  7504. ;; Special treatment needed for TAB and RET
  7505. (org-defkey orgstruct-mode-map [(tab)]
  7506. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7507. (org-defkey orgstruct-mode-map "\C-i"
  7508. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7509. (org-defkey orgstruct-mode-map "\M-\C-m"
  7510. (orgstruct-make-binding 'org-insert-heading 105
  7511. "\M-\C-m" [(meta return)]))
  7512. (org-defkey orgstruct-mode-map [(meta return)]
  7513. (orgstruct-make-binding 'org-insert-heading 106
  7514. [(meta return)] "\M-\C-m"))
  7515. (org-defkey orgstruct-mode-map [(shift meta return)]
  7516. (orgstruct-make-binding 'org-insert-todo-heading 107
  7517. [(meta return)] "\M-\C-m"))
  7518. (org-defkey orgstruct-mode-map "\e\C-m"
  7519. (orgstruct-make-binding 'org-insert-heading 108
  7520. "\e\C-m" [?\e (return)]))
  7521. (org-defkey orgstruct-mode-map [?\e (return)]
  7522. (orgstruct-make-binding 'org-insert-heading 109
  7523. [?\e (return)] "\e\C-m"))
  7524. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7525. (orgstruct-make-binding 'org-insert-todo-heading 110
  7526. [?\e (return)] "\e\C-m"))
  7527. (unless org-local-vars
  7528. (setq org-local-vars (org-get-local-variables)))
  7529. t))
  7530. (defun orgstruct-make-binding (fun n &rest keys)
  7531. "Create a function for binding in the structure minor mode.
  7532. FUN is the command to call inside a table. N is used to create a unique
  7533. command name. KEYS are keys that should be checked in for a command
  7534. to execute outside of tables."
  7535. (eval
  7536. (list 'defun
  7537. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7538. '(arg)
  7539. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7540. "Outside of structure, run the binding of `"
  7541. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7542. "'.")
  7543. '(interactive "p")
  7544. (list 'if
  7545. `(org-context-p 'headline 'item
  7546. (and orgstruct-is-++
  7547. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7548. 'item-body))
  7549. (list 'org-run-like-in-org-mode (list 'quote fun))
  7550. (list 'let '(orgstruct-mode)
  7551. (list 'call-interactively
  7552. (append '(or)
  7553. (mapcar (lambda (k)
  7554. (list 'key-binding k))
  7555. keys)
  7556. '('orgstruct-error))))))))
  7557. (defun org-context-p (&rest contexts)
  7558. "Check if local context is any of CONTEXTS.
  7559. Possible values in the list of contexts are `table', `headline', and `item'."
  7560. (let ((pos (point)))
  7561. (goto-char (point-at-bol))
  7562. (prog1 (or (and (memq 'table contexts)
  7563. (looking-at "[ \t]*|"))
  7564. (and (memq 'headline contexts)
  7565. (looking-at org-outline-regexp))
  7566. (and (memq 'item contexts)
  7567. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7568. (and (memq 'item-body contexts)
  7569. (org-in-item-p)))
  7570. (goto-char pos))))
  7571. (defun org-get-local-variables ()
  7572. "Return a list of all local variables in an org-mode buffer."
  7573. (let (varlist)
  7574. (with-current-buffer (get-buffer-create "*Org tmp*")
  7575. (erase-buffer)
  7576. (org-mode)
  7577. (setq varlist (buffer-local-variables)))
  7578. (kill-buffer "*Org tmp*")
  7579. (delq nil
  7580. (mapcar
  7581. (lambda (x)
  7582. (setq x
  7583. (if (symbolp x)
  7584. (list x)
  7585. (list (car x) (list 'quote (cdr x)))))
  7586. (if (string-match
  7587. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7588. (symbol-name (car x)))
  7589. x nil))
  7590. varlist))))
  7591. (defun org-clone-local-variables (from-buffer &optional regexp)
  7592. "Clone local variables from FROM-BUFFER.
  7593. Optional argument REGEXP selects variables to clone."
  7594. (mapc
  7595. (lambda (pair)
  7596. (and (symbolp (car pair))
  7597. (or (null regexp)
  7598. (string-match regexp (symbol-name (car pair))))
  7599. (set (make-local-variable (car pair))
  7600. (cdr pair))))
  7601. (buffer-local-variables from-buffer)))
  7602. ;;;###autoload
  7603. (defun org-run-like-in-org-mode (cmd)
  7604. "Run a command, pretending that the current buffer is in Org-mode.
  7605. This will temporarily bind local variables that are typically bound in
  7606. Org-mode to the values they have in Org-mode, and then interactively
  7607. call CMD."
  7608. (org-load-modules-maybe)
  7609. (unless org-local-vars
  7610. (setq org-local-vars (org-get-local-variables)))
  7611. (eval (list 'let org-local-vars
  7612. (list 'call-interactively (list 'quote cmd)))))
  7613. ;;;; Archiving
  7614. (defun org-get-category (&optional pos force-refresh)
  7615. "Get the category applying to position POS."
  7616. (if force-refresh (org-refresh-category-properties))
  7617. (let ((pos (or pos (point))))
  7618. (or (get-text-property pos 'org-category)
  7619. (progn (org-refresh-category-properties)
  7620. (get-text-property pos 'org-category)))))
  7621. (defun org-refresh-category-properties ()
  7622. "Refresh category text properties in the buffer."
  7623. (let ((def-cat (cond
  7624. ((null org-category)
  7625. (if buffer-file-name
  7626. (file-name-sans-extension
  7627. (file-name-nondirectory buffer-file-name))
  7628. "???"))
  7629. ((symbolp org-category) (symbol-name org-category))
  7630. (t org-category)))
  7631. beg end cat pos optionp)
  7632. (org-unmodified
  7633. (save-excursion
  7634. (save-restriction
  7635. (widen)
  7636. (goto-char (point-min))
  7637. (put-text-property (point) (point-max) 'org-category def-cat)
  7638. (while (re-search-forward
  7639. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7640. (setq pos (match-end 0)
  7641. optionp (equal (char-after (match-beginning 0)) ?#)
  7642. cat (org-trim (match-string 2)))
  7643. (if optionp
  7644. (setq beg (point-at-bol) end (point-max))
  7645. (org-back-to-heading t)
  7646. (setq beg (point) end (org-end-of-subtree t t)))
  7647. (put-text-property beg end 'org-category cat)
  7648. (put-text-property beg end 'org-category-position beg)
  7649. (goto-char pos)))))))
  7650. ;;;; Link Stuff
  7651. ;;; Link abbreviations
  7652. (defun org-link-expand-abbrev (link)
  7653. "Apply replacements as defined in `org-link-abbrev-alist."
  7654. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7655. (let* ((key (match-string 1 link))
  7656. (as (or (assoc key org-link-abbrev-alist-local)
  7657. (assoc key org-link-abbrev-alist)))
  7658. (tag (and (match-end 2) (match-string 3 link)))
  7659. rpl)
  7660. (if (not as)
  7661. link
  7662. (setq rpl (cdr as))
  7663. (cond
  7664. ((symbolp rpl) (funcall rpl tag))
  7665. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7666. ((string-match "%h" rpl)
  7667. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7668. (t (concat rpl tag)))))
  7669. link))
  7670. ;;; Storing and inserting links
  7671. (defvar org-insert-link-history nil
  7672. "Minibuffer history for links inserted with `org-insert-link'.")
  7673. (defvar org-stored-links nil
  7674. "Contains the links stored with `org-store-link'.")
  7675. (defvar org-store-link-plist nil
  7676. "Plist with info about the most recently link created with `org-store-link'.")
  7677. (defvar org-link-protocols nil
  7678. "Link protocols added to Org-mode using `org-add-link-type'.")
  7679. (defvar org-store-link-functions nil
  7680. "List of functions that are called to create and store a link.
  7681. Each function will be called in turn until one returns a non-nil
  7682. value. Each function should check if it is responsible for creating
  7683. this link (for example by looking at the major mode).
  7684. If not, it must exit and return nil.
  7685. If yes, it should return a non-nil value after a calling
  7686. `org-store-link-props' with a list of properties and values.
  7687. Special properties are:
  7688. :type The link prefix, like \"http\". This must be given.
  7689. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7690. This is obligatory as well.
  7691. :description Optional default description for the second pair
  7692. of brackets in an Org-mode link. The user can still change
  7693. this when inserting this link into an Org-mode buffer.
  7694. In addition to these, any additional properties can be specified
  7695. and then used in remember templates.")
  7696. (defun org-add-link-type (type &optional follow export)
  7697. "Add TYPE to the list of `org-link-types'.
  7698. Re-compute all regular expressions depending on `org-link-types'
  7699. FOLLOW and EXPORT are two functions.
  7700. FOLLOW should take the link path as the single argument and do whatever
  7701. is necessary to follow the link, for example find a file or display
  7702. a mail message.
  7703. EXPORT should format the link path for export to one of the export formats.
  7704. It should be a function accepting three arguments:
  7705. path the path of the link, the text after the prefix (like \"http:\")
  7706. desc the description of the link, if any, or a description added by
  7707. org-export-normalize-links if there is none
  7708. format the export format, a symbol like `html' or `latex' or `ascii'..
  7709. The function may use the FORMAT information to return different values
  7710. depending on the format. The return value will be put literally into
  7711. the exported file. If the return value is nil, this means Org should
  7712. do what it normally does with links which do not have EXPORT defined.
  7713. Org-mode has a built-in default for exporting links. If you are happy with
  7714. this default, there is no need to define an export function for the link
  7715. type. For a simple example of an export function, see `org-bbdb.el'."
  7716. (add-to-list 'org-link-types type t)
  7717. (org-make-link-regexps)
  7718. (if (assoc type org-link-protocols)
  7719. (setcdr (assoc type org-link-protocols) (list follow export))
  7720. (push (list type follow export) org-link-protocols)))
  7721. (defvar org-agenda-buffer-name)
  7722. ;;;###autoload
  7723. (defun org-store-link (arg)
  7724. "\\<org-mode-map>Store an org-link to the current location.
  7725. This link is added to `org-stored-links' and can later be inserted
  7726. into an org-buffer with \\[org-insert-link].
  7727. For some link types, a prefix arg is interpreted:
  7728. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7729. For file links, arg negates `org-context-in-file-links'."
  7730. (interactive "P")
  7731. (org-load-modules-maybe)
  7732. (setq org-store-link-plist nil) ; reset
  7733. (org-with-limited-levels
  7734. (let (link cpltxt desc description search txt custom-id agenda-link)
  7735. (cond
  7736. ((run-hook-with-args-until-success 'org-store-link-functions)
  7737. (setq link (plist-get org-store-link-plist :link)
  7738. desc (or (plist-get org-store-link-plist :description) link)))
  7739. ((equal (buffer-name) "*Org Edit Src Example*")
  7740. (let (label gc)
  7741. (while (or (not label)
  7742. (save-excursion
  7743. (save-restriction
  7744. (widen)
  7745. (goto-char (point-min))
  7746. (re-search-forward
  7747. (regexp-quote (format org-coderef-label-format label))
  7748. nil t))))
  7749. (when label (message "Label exists already") (sit-for 2))
  7750. (setq label (read-string "Code line label: " label)))
  7751. (end-of-line 1)
  7752. (setq link (format org-coderef-label-format label))
  7753. (setq gc (- 79 (length link)))
  7754. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7755. (insert link)
  7756. (setq link (concat "(" label ")") desc nil)))
  7757. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7758. ;; We are in the agenda, link to referenced location
  7759. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7760. (get-text-property (point) 'org-marker))))
  7761. (when m
  7762. (org-with-point-at m
  7763. (setq agenda-link
  7764. (if (org-called-interactively-p 'any)
  7765. (call-interactively 'org-store-link)
  7766. (org-store-link nil)))))))
  7767. ((eq major-mode 'calendar-mode)
  7768. (let ((cd (calendar-cursor-to-date)))
  7769. (setq link
  7770. (format-time-string
  7771. (car org-time-stamp-formats)
  7772. (apply 'encode-time
  7773. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7774. nil nil nil))))
  7775. (org-store-link-props :type "calendar" :date cd)))
  7776. ((eq major-mode 'w3-mode)
  7777. (setq cpltxt (if (and (buffer-name)
  7778. (not (string-match "Untitled" (buffer-name))))
  7779. (buffer-name)
  7780. (url-view-url t))
  7781. link (org-make-link (url-view-url t)))
  7782. (org-store-link-props :type "w3" :url (url-view-url t)))
  7783. ((eq major-mode 'w3m-mode)
  7784. (setq cpltxt (or w3m-current-title w3m-current-url)
  7785. link (org-make-link w3m-current-url))
  7786. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7787. ((setq search (run-hook-with-args-until-success
  7788. 'org-create-file-search-functions))
  7789. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7790. "::" search))
  7791. (setq cpltxt (or description link)))
  7792. ((eq major-mode 'image-mode)
  7793. (setq cpltxt (concat "file:"
  7794. (abbreviate-file-name buffer-file-name))
  7795. link (org-make-link cpltxt))
  7796. (org-store-link-props :type "image" :file buffer-file-name))
  7797. ((eq major-mode 'dired-mode)
  7798. ;; link to the file in the current line
  7799. (let ((file (dired-get-filename nil t)))
  7800. (setq file (if file
  7801. (abbreviate-file-name
  7802. (expand-file-name (dired-get-filename nil t)))
  7803. ;; otherwise, no file so use current directory.
  7804. default-directory))
  7805. (setq cpltxt (concat "file:" file)
  7806. link (org-make-link cpltxt))))
  7807. ((and (buffer-file-name (buffer-base-buffer)) (eq major-mode 'org-mode))
  7808. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7809. (cond
  7810. ((org-in-regexp "<<\\(.*?\\)>>")
  7811. (setq cpltxt
  7812. (concat "file:"
  7813. (abbreviate-file-name
  7814. (buffer-file-name (buffer-base-buffer)))
  7815. "::" (match-string 1))
  7816. link (org-make-link cpltxt)))
  7817. ((and (featurep 'org-id)
  7818. (or (eq org-link-to-org-use-id t)
  7819. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7820. (org-called-interactively-p 'any))
  7821. (and (eq org-link-to-org-use-id
  7822. 'create-if-interactive-and-no-custom-id)
  7823. (org-called-interactively-p 'any)
  7824. (not custom-id))
  7825. (and org-link-to-org-use-id
  7826. (org-entry-get nil "ID"))))
  7827. ;; We can make a link using the ID.
  7828. (setq link (condition-case nil
  7829. (prog1 (org-id-store-link)
  7830. (setq desc (plist-get org-store-link-plist
  7831. :description)))
  7832. (error
  7833. ;; probably before first headline, link to file only
  7834. (concat "file:"
  7835. (abbreviate-file-name
  7836. (buffer-file-name (buffer-base-buffer))))))))
  7837. (t
  7838. ;; Just link to current headline
  7839. (setq cpltxt (concat "file:"
  7840. (abbreviate-file-name
  7841. (buffer-file-name (buffer-base-buffer)))))
  7842. ;; Add a context search string
  7843. (when (org-xor org-context-in-file-links arg)
  7844. (setq txt (cond
  7845. ((org-on-heading-p) nil)
  7846. ((org-region-active-p)
  7847. (buffer-substring (region-beginning) (region-end)))
  7848. (t nil)))
  7849. (when (or (null txt) (string-match "\\S-" txt))
  7850. (setq cpltxt
  7851. (concat cpltxt "::"
  7852. (condition-case nil
  7853. (org-make-org-heading-search-string txt)
  7854. (error "")))
  7855. desc (or (nth 4 (ignore-errors
  7856. (org-heading-components))) "NONE"))))
  7857. (if (string-match "::\\'" cpltxt)
  7858. (setq cpltxt (substring cpltxt 0 -2)))
  7859. (setq link (org-make-link cpltxt)))))
  7860. ((buffer-file-name (buffer-base-buffer))
  7861. ;; Just link to this file here.
  7862. (setq cpltxt (concat "file:"
  7863. (abbreviate-file-name
  7864. (buffer-file-name (buffer-base-buffer)))))
  7865. ;; Add a context string
  7866. (when (org-xor org-context-in-file-links arg)
  7867. (setq txt (if (org-region-active-p)
  7868. (buffer-substring (region-beginning) (region-end))
  7869. (buffer-substring (point-at-bol) (point-at-eol))))
  7870. ;; Only use search option if there is some text.
  7871. (when (string-match "\\S-" txt)
  7872. (setq cpltxt
  7873. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7874. desc "NONE")))
  7875. (setq link (org-make-link cpltxt)))
  7876. ((org-called-interactively-p 'interactive)
  7877. (error "Cannot link to a buffer which is not visiting a file"))
  7878. (t (setq link nil)))
  7879. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7880. (setq link (or link cpltxt)
  7881. desc (or desc cpltxt))
  7882. (if (equal desc "NONE") (setq desc nil))
  7883. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  7884. (progn
  7885. (setq org-stored-links
  7886. (cons (list link desc) org-stored-links))
  7887. (message "Stored: %s" (or desc link))
  7888. (when custom-id
  7889. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7890. "::#" custom-id))
  7891. (setq org-stored-links
  7892. (cons (list link desc) org-stored-links))))
  7893. (or agenda-link (and link (org-make-link-string link desc)))))))
  7894. (defun org-store-link-props (&rest plist)
  7895. "Store link properties, extract names and addresses."
  7896. (let (x adr)
  7897. (when (setq x (plist-get plist :from))
  7898. (setq adr (mail-extract-address-components x))
  7899. (setq plist (plist-put plist :fromname (car adr)))
  7900. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7901. (when (setq x (plist-get plist :to))
  7902. (setq adr (mail-extract-address-components x))
  7903. (setq plist (plist-put plist :toname (car adr)))
  7904. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7905. (let ((from (plist-get plist :from))
  7906. (to (plist-get plist :to)))
  7907. (when (and from to org-from-is-user-regexp)
  7908. (setq plist
  7909. (plist-put plist :fromto
  7910. (if (string-match org-from-is-user-regexp from)
  7911. (concat "to %t")
  7912. (concat "from %f"))))))
  7913. (setq org-store-link-plist plist))
  7914. (defun org-add-link-props (&rest plist)
  7915. "Add these properties to the link property list."
  7916. (let (key value)
  7917. (while plist
  7918. (setq key (pop plist) value (pop plist))
  7919. (setq org-store-link-plist
  7920. (plist-put org-store-link-plist key value)))))
  7921. (defun org-email-link-description (&optional fmt)
  7922. "Return the description part of an email link.
  7923. This takes information from `org-store-link-plist' and formats it
  7924. according to FMT (default from `org-email-link-description-format')."
  7925. (setq fmt (or fmt org-email-link-description-format))
  7926. (let* ((p org-store-link-plist)
  7927. (to (plist-get p :toaddress))
  7928. (from (plist-get p :fromaddress))
  7929. (table
  7930. (list
  7931. (cons "%c" (plist-get p :fromto))
  7932. (cons "%F" (plist-get p :from))
  7933. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7934. (cons "%T" (plist-get p :to))
  7935. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7936. (cons "%s" (plist-get p :subject))
  7937. (cons "%d" (plist-get p :date))
  7938. (cons "%m" (plist-get p :message-id)))))
  7939. (when (string-match "%c" fmt)
  7940. ;; Check if the user wrote this message
  7941. (if (and org-from-is-user-regexp from to
  7942. (save-match-data (string-match org-from-is-user-regexp from)))
  7943. (setq fmt (replace-match "to %t" t t fmt))
  7944. (setq fmt (replace-match "from %f" t t fmt))))
  7945. (org-replace-escapes fmt table)))
  7946. (defun org-make-org-heading-search-string (&optional string heading)
  7947. "Make search string for STRING or current headline."
  7948. (interactive)
  7949. (let ((s (or string (org-get-heading)))
  7950. (lines org-context-in-file-links))
  7951. (unless (and string (not heading))
  7952. ;; We are using a headline, clean up garbage in there.
  7953. (if (string-match org-todo-regexp s)
  7954. (setq s (replace-match "" t t s)))
  7955. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  7956. (setq s (replace-match "" t t s)))
  7957. (setq s (org-trim s))
  7958. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7959. org-comment-string "\\)") s)
  7960. (setq s (replace-match "" t t s)))
  7961. (while (string-match org-ts-regexp s)
  7962. (setq s (replace-match "" t t s))))
  7963. (or string (setq s (concat "*" s))) ; Add * for headlines
  7964. (when (and string (integerp lines) (> lines 0))
  7965. (let ((slines (org-split-string s "\n")))
  7966. (when (< lines (length slines))
  7967. (setq s (mapconcat
  7968. 'identity
  7969. (reverse (nthcdr (- (length slines) lines)
  7970. (reverse slines))) "\n")))))
  7971. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7972. (defun org-make-link (&rest strings)
  7973. "Concatenate STRINGS."
  7974. (apply 'concat strings))
  7975. (defun org-make-link-string (link &optional description)
  7976. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7977. (unless (string-match "\\S-" link)
  7978. (error "Empty link"))
  7979. (when (and description
  7980. (stringp description)
  7981. (not (string-match "\\S-" description)))
  7982. (setq description nil))
  7983. (when (stringp description)
  7984. ;; Remove brackets from the description, they are fatal.
  7985. (while (string-match "\\[" description)
  7986. (setq description (replace-match "{" t t description)))
  7987. (while (string-match "\\]" description)
  7988. (setq description (replace-match "}" t t description))))
  7989. (when (equal link description)
  7990. ;; No description needed, it is identical
  7991. (setq description nil))
  7992. (when (and (not description)
  7993. (not (string-match (org-image-file-name-regexp) link))
  7994. (not (equal link (org-link-escape link))))
  7995. (setq description (org-extract-attributes link)))
  7996. (setq link
  7997. (cond ((string-match (org-image-file-name-regexp) link) link)
  7998. ((string-match org-link-types-re link)
  7999. (concat (match-string 1 link)
  8000. (org-link-escape (substring link (match-end 1)))))
  8001. (t (org-link-escape link))))
  8002. (concat "[[" link "]"
  8003. (if description (concat "[" description "]") "")
  8004. "]"))
  8005. (defconst org-link-escape-chars
  8006. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8007. "List of characters that should be escaped in link.
  8008. This is the list that is used for internal purposes.")
  8009. (defvar org-url-encoding-use-url-hexify nil)
  8010. (defconst org-link-escape-chars-browser
  8011. '(?\ )
  8012. "List of escapes for characters that are problematic in links.
  8013. This is the list that is used before handing over to the browser.")
  8014. (defun org-link-escape (text &optional table merge)
  8015. "Return percent escaped representation of TEXT.
  8016. TEXT is a string with the text to escape.
  8017. Optional argument TABLE is a list with characters that should be
  8018. escaped. When nil, `org-link-escape-chars' is used.
  8019. If optional argument MERGE is set, merge TABLE into
  8020. `org-link-escape-chars'."
  8021. (if (and org-url-encoding-use-url-hexify (not table))
  8022. (url-hexify-string text)
  8023. (cond
  8024. ((and table merge)
  8025. (mapc (lambda (defchr)
  8026. (unless (member defchr table)
  8027. (setq table (cons defchr table)))) org-link-escape-chars))
  8028. ((null table)
  8029. (setq table org-link-escape-chars)))
  8030. (mapconcat
  8031. (lambda (char)
  8032. (if (or (member char table)
  8033. (< char 32) (= char 37) (> char 126))
  8034. (mapconcat (lambda (sequence-element)
  8035. (format "%%%.2X" sequence-element))
  8036. (or (encode-coding-char char 'utf-8)
  8037. (error "Unable to percent escape character: %s"
  8038. (char-to-string char))) "")
  8039. (char-to-string char))) text "")))
  8040. (defun org-link-unescape (str)
  8041. "Unhex hexified Unicode strings as returned from the JavaScript function
  8042. encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
  8043. (unless (and (null str) (string= "" str))
  8044. (let ((pos 0) (case-fold-search t) unhexed)
  8045. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8046. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8047. (setq str (replace-match unhexed t t str))
  8048. (setq pos (+ pos (length unhexed))))))
  8049. str)
  8050. (defun org-link-unescape-compound (hex)
  8051. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
  8052. Note: this function also decodes single byte encodings like
  8053. `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
  8054. (save-match-data
  8055. (let* ((bytes (cdr (split-string hex "%")))
  8056. (ret "")
  8057. (eat 0)
  8058. (sum 0))
  8059. (while bytes
  8060. (let* ((val (string-to-number (pop bytes) 16))
  8061. (shift-xor
  8062. (if (= 0 eat)
  8063. (cond
  8064. ((>= val 252) (cons 6 252))
  8065. ((>= val 248) (cons 5 248))
  8066. ((>= val 240) (cons 4 240))
  8067. ((>= val 224) (cons 3 224))
  8068. ((>= val 192) (cons 2 192))
  8069. (t (cons 0 0)))
  8070. (cons 6 128))))
  8071. (if (>= val 192) (setq eat (car shift-xor)))
  8072. (setq val (logxor val (cdr shift-xor)))
  8073. (setq sum (+ (lsh sum (car shift-xor)) val))
  8074. (if (> eat 0) (setq eat (- eat 1)))
  8075. (cond
  8076. ((= 0 eat) ;multi byte
  8077. (setq ret (concat ret (org-char-to-string sum)))
  8078. (setq sum 0))
  8079. ((not bytes) ; single byte(s)
  8080. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8081. )) ;; end (while bytes
  8082. ret )))
  8083. (defun org-link-unescape-single-byte-sequence (hex)
  8084. "Unhexify hex-encoded single byte character sequences."
  8085. (mapconcat (lambda (byte)
  8086. (char-to-string (string-to-number byte 16)))
  8087. (cdr (split-string hex "%")) ""))
  8088. (defun org-xor (a b)
  8089. "Exclusive or."
  8090. (if a (not b) b))
  8091. (defun org-fixup-message-id-for-http (s)
  8092. "Replace special characters in a message id, so it can be used in an http query."
  8093. (when (string-match "%" s)
  8094. (setq s (mapconcat (lambda (c)
  8095. (if (eq c ?%)
  8096. "%25"
  8097. (char-to-string c)))
  8098. s "")))
  8099. (while (string-match "<" s)
  8100. (setq s (replace-match "%3C" t t s)))
  8101. (while (string-match ">" s)
  8102. (setq s (replace-match "%3E" t t s)))
  8103. (while (string-match "@" s)
  8104. (setq s (replace-match "%40" t t s)))
  8105. s)
  8106. ;;;###autoload
  8107. (defun org-insert-link-global ()
  8108. "Insert a link like Org-mode does.
  8109. This command can be called in any mode to insert a link in Org-mode syntax."
  8110. (interactive)
  8111. (org-load-modules-maybe)
  8112. (org-run-like-in-org-mode 'org-insert-link))
  8113. (defun org-insert-link (&optional complete-file link-location default-description)
  8114. "Insert a link. At the prompt, enter the link.
  8115. Completion can be used to insert any of the link protocol prefixes like
  8116. http or ftp in use.
  8117. The history can be used to select a link previously stored with
  8118. `org-store-link'. When the empty string is entered (i.e. if you just
  8119. press RET at the prompt), the link defaults to the most recently
  8120. stored link. As SPC triggers completion in the minibuffer, you need to
  8121. use M-SPC or C-q SPC to force the insertion of a space character.
  8122. You will also be prompted for a description, and if one is given, it will
  8123. be displayed in the buffer instead of the link.
  8124. If there is already a link at point, this command will allow you to edit link
  8125. and description parts.
  8126. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8127. be selected using completion. The path to the file will be relative to the
  8128. current directory if the file is in the current directory or a subdirectory.
  8129. Otherwise, the link will be the absolute path as completed in the minibuffer
  8130. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8131. option `org-link-file-path-type'.
  8132. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8133. the current directory or below.
  8134. With three \\[universal-argument] prefixes, negate the meaning of
  8135. `org-keep-stored-link-after-insertion'.
  8136. If `org-make-link-description-function' is non-nil, this function will be
  8137. called with the link target, and the result will be the default
  8138. link description.
  8139. If the LINK-LOCATION parameter is non-nil, this value will be
  8140. used as the link location instead of reading one interactively.
  8141. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8142. be used as the default description."
  8143. (interactive "P")
  8144. (let* ((wcf (current-window-configuration))
  8145. (region (if (org-region-active-p)
  8146. (buffer-substring (region-beginning) (region-end))))
  8147. (remove (and region (list (region-beginning) (region-end))))
  8148. (desc region)
  8149. tmphist ; byte-compile incorrectly complains about this
  8150. (link link-location)
  8151. entry file all-prefixes)
  8152. (cond
  8153. (link-location) ; specified by arg, just use it.
  8154. ((org-in-regexp org-bracket-link-regexp 1)
  8155. ;; We do have a link at point, and we are going to edit it.
  8156. (setq remove (list (match-beginning 0) (match-end 0)))
  8157. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8158. (setq link (read-string "Link: "
  8159. (org-link-unescape
  8160. (org-match-string-no-properties 1)))))
  8161. ((or (org-in-regexp org-angle-link-re)
  8162. (org-in-regexp org-plain-link-re))
  8163. ;; Convert to bracket link
  8164. (setq remove (list (match-beginning 0) (match-end 0))
  8165. link (read-string "Link: "
  8166. (org-remove-angle-brackets (match-string 0)))))
  8167. ((member complete-file '((4) (16)))
  8168. ;; Completing read for file names.
  8169. (setq link (org-file-complete-link complete-file)))
  8170. (t
  8171. ;; Read link, with completion for stored links.
  8172. (with-output-to-temp-buffer "*Org Links*"
  8173. (princ "Insert a link.
  8174. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8175. (when org-stored-links
  8176. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8177. (princ (mapconcat
  8178. (lambda (x)
  8179. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  8180. (reverse org-stored-links) "\n"))))
  8181. (let ((cw (selected-window)))
  8182. (select-window (get-buffer-window "*Org Links*" 'visible))
  8183. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8184. (unless (pos-visible-in-window-p (point-max))
  8185. (org-fit-window-to-buffer))
  8186. (and (window-live-p cw) (select-window cw)))
  8187. ;; Fake a link history, containing the stored links.
  8188. (setq tmphist (append (mapcar 'car org-stored-links)
  8189. org-insert-link-history))
  8190. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  8191. (mapcar 'car org-link-abbrev-alist)
  8192. org-link-types))
  8193. (unwind-protect
  8194. (progn
  8195. (setq link
  8196. (let ((org-completion-use-ido nil)
  8197. (org-completion-use-iswitchb nil))
  8198. (org-completing-read
  8199. "Link: "
  8200. (append
  8201. (mapcar (lambda (x) (list (concat x ":")))
  8202. all-prefixes)
  8203. (mapcar 'car org-stored-links))
  8204. nil nil nil
  8205. 'tmphist
  8206. (car (car org-stored-links)))))
  8207. (if (not (string-match "\\S-" link))
  8208. (error "No link selected"))
  8209. (if (or (member link all-prefixes)
  8210. (and (equal ":" (substring link -1))
  8211. (member (substring link 0 -1) all-prefixes)
  8212. (setq link (substring link 0 -1))))
  8213. (setq link (org-link-try-special-completion link))))
  8214. (set-window-configuration wcf)
  8215. (kill-buffer "*Org Links*"))
  8216. (setq entry (assoc link org-stored-links))
  8217. (or entry (push link org-insert-link-history))
  8218. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8219. (not org-keep-stored-link-after-insertion))
  8220. (setq org-stored-links (delq (assoc link org-stored-links)
  8221. org-stored-links)))
  8222. (setq desc (or desc (nth 1 entry)))))
  8223. (if (string-match org-plain-link-re link)
  8224. ;; URL-like link, normalize the use of angular brackets.
  8225. (setq link (org-make-link (org-remove-angle-brackets link))))
  8226. ;; Check if we are linking to the current file with a search option
  8227. ;; If yes, simplify the link by using only the search option.
  8228. (when (and buffer-file-name
  8229. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8230. (let* ((path (match-string 1 link))
  8231. (case-fold-search nil)
  8232. (search (match-string 2 link)))
  8233. (save-match-data
  8234. (if (equal (file-truename buffer-file-name) (file-truename path))
  8235. ;; We are linking to this same file, with a search option
  8236. (setq link search)))))
  8237. ;; Check if we can/should use a relative path. If yes, simplify the link
  8238. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8239. (let* ((type (match-string 1 link))
  8240. (path (match-string 2 link))
  8241. (origpath path)
  8242. (case-fold-search nil))
  8243. (cond
  8244. ((or (eq org-link-file-path-type 'absolute)
  8245. (equal complete-file '(16)))
  8246. (setq path (abbreviate-file-name (expand-file-name path))))
  8247. ((eq org-link-file-path-type 'noabbrev)
  8248. (setq path (expand-file-name path)))
  8249. ((eq org-link-file-path-type 'relative)
  8250. (setq path (file-relative-name path)))
  8251. (t
  8252. (save-match-data
  8253. (if (string-match (concat "^" (regexp-quote
  8254. (expand-file-name
  8255. (file-name-as-directory
  8256. default-directory))))
  8257. (expand-file-name path))
  8258. ;; We are linking a file with relative path name.
  8259. (setq path (substring (expand-file-name path)
  8260. (match-end 0)))
  8261. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8262. (setq link (concat type path))
  8263. (if (equal desc origpath)
  8264. (setq desc path))))
  8265. (if org-make-link-description-function
  8266. (setq desc (funcall org-make-link-description-function link desc))
  8267. (if default-description (setq desc default-description)))
  8268. (setq desc (read-string "Description: " desc))
  8269. (unless (string-match "\\S-" desc) (setq desc nil))
  8270. (if remove (apply 'delete-region remove))
  8271. (insert (org-make-link-string link desc))))
  8272. (defun org-link-try-special-completion (type)
  8273. "If there is completion support for link type TYPE, offer it."
  8274. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8275. (if (functionp fun)
  8276. (funcall fun)
  8277. (read-string "Link (no completion support): " (concat type ":")))))
  8278. (defun org-file-complete-link (&optional arg)
  8279. "Create a file link using completion."
  8280. (let (file link)
  8281. (setq file (read-file-name "File: "))
  8282. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8283. (pwd1 (file-name-as-directory (abbreviate-file-name
  8284. (expand-file-name ".")))))
  8285. (cond
  8286. ((equal arg '(16))
  8287. (setq link (org-make-link
  8288. "file:"
  8289. (abbreviate-file-name (expand-file-name file)))))
  8290. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8291. (setq link (org-make-link "file:" (match-string 1 file))))
  8292. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8293. (expand-file-name file))
  8294. (setq link (org-make-link
  8295. "file:" (match-string 1 (expand-file-name file)))))
  8296. (t (setq link (org-make-link "file:" file)))))
  8297. link))
  8298. (defun org-completing-read (&rest args)
  8299. "Completing-read with SPACE being a normal character."
  8300. (let ((enable-recursive-minibuffers t)
  8301. (minibuffer-local-completion-map
  8302. (copy-keymap minibuffer-local-completion-map)))
  8303. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8304. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8305. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8306. (apply 'org-icompleting-read args)))
  8307. (defun org-completing-read-no-i (&rest args)
  8308. (let (org-completion-use-ido org-completion-use-iswitchb)
  8309. (apply 'org-completing-read args)))
  8310. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8311. "Use iswitch as a completing-read replacement to choose from choices.
  8312. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8313. from."
  8314. (let* ((iswitchb-use-virtual-buffers nil)
  8315. (iswitchb-make-buflist-hook
  8316. (lambda ()
  8317. (setq iswitchb-temp-buflist choices))))
  8318. (iswitchb-read-buffer prompt)))
  8319. (defun org-icompleting-read (&rest args)
  8320. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8321. (org-without-partial-completion
  8322. (if (and org-completion-use-ido
  8323. (fboundp 'ido-completing-read)
  8324. (boundp 'ido-mode) ido-mode
  8325. (listp (second args)))
  8326. (let ((ido-enter-matching-directory nil))
  8327. (apply 'ido-completing-read (concat (car args))
  8328. (if (consp (car (nth 1 args)))
  8329. (mapcar 'car (nth 1 args))
  8330. (nth 1 args))
  8331. (cddr args)))
  8332. (if (and org-completion-use-iswitchb
  8333. (boundp 'iswitchb-mode) iswitchb-mode
  8334. (listp (second args)))
  8335. (apply 'org-iswitchb-completing-read (concat (car args))
  8336. (if (consp (car (nth 1 args)))
  8337. (mapcar 'car (nth 1 args))
  8338. (nth 1 args))
  8339. (cddr args))
  8340. (apply 'completing-read args)))))
  8341. (defun org-extract-attributes (s)
  8342. "Extract the attributes cookie from a string and set as text property."
  8343. (let (a attr (start 0) key value)
  8344. (save-match-data
  8345. (when (string-match "{{\\([^}]+\\)}}$" s)
  8346. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8347. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8348. (setq key (match-string 1 a) value (match-string 2 a)
  8349. start (match-end 0)
  8350. attr (plist-put attr (intern key) value))))
  8351. (org-add-props s nil 'org-attr attr))
  8352. s))
  8353. (defun org-extract-attributes-from-string (tag)
  8354. (let (key value attr)
  8355. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8356. (setq key (match-string 1 tag) value (match-string 2 tag)
  8357. tag (replace-match "" t t tag)
  8358. attr (plist-put attr (intern key) value)))
  8359. (cons tag attr)))
  8360. (defun org-attributes-to-string (plist)
  8361. "Format a property list into an HTML attribute list."
  8362. (let ((s "") key value)
  8363. (while plist
  8364. (setq key (pop plist) value (pop plist))
  8365. (and value
  8366. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8367. s))
  8368. ;;; Opening/following a link
  8369. (defvar org-link-search-failed nil)
  8370. (defvar org-open-link-functions nil
  8371. "Hook for functions finding a plain text link.
  8372. These functions must take a single argument, the link content.
  8373. They will be called for links that look like [[link text][description]]
  8374. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8375. like a filename (e.g. \"./blue.png\").
  8376. These functions will be called *before* Org attempts to resolve the
  8377. link by doing text searches in the current buffer - so if you want a
  8378. link \"[[target]]\" to still find \"<<target>>\", your function should
  8379. handle this as a special case.
  8380. When the function does handle the link, it must return a non-nil value.
  8381. If it decides that it is not responsible for this link, it must return
  8382. nil to indicate that that Org-mode can continue with other options
  8383. like exact and fuzzy text search.")
  8384. (defun org-next-link ()
  8385. "Move forward to the next link.
  8386. If the link is in hidden text, expose it."
  8387. (interactive)
  8388. (when (and org-link-search-failed (eq this-command last-command))
  8389. (goto-char (point-min))
  8390. (message "Link search wrapped back to beginning of buffer"))
  8391. (setq org-link-search-failed nil)
  8392. (let* ((pos (point))
  8393. (ct (org-context))
  8394. (a (assoc :link ct)))
  8395. (if a (goto-char (nth 2 a)))
  8396. (if (re-search-forward org-any-link-re nil t)
  8397. (progn
  8398. (goto-char (match-beginning 0))
  8399. (if (outline-invisible-p) (org-show-context)))
  8400. (goto-char pos)
  8401. (setq org-link-search-failed t)
  8402. (error "No further link found"))))
  8403. (defun org-previous-link ()
  8404. "Move backward to the previous link.
  8405. If the link is in hidden text, expose it."
  8406. (interactive)
  8407. (when (and org-link-search-failed (eq this-command last-command))
  8408. (goto-char (point-max))
  8409. (message "Link search wrapped back to end of buffer"))
  8410. (setq org-link-search-failed nil)
  8411. (let* ((pos (point))
  8412. (ct (org-context))
  8413. (a (assoc :link ct)))
  8414. (if a (goto-char (nth 1 a)))
  8415. (if (re-search-backward org-any-link-re nil t)
  8416. (progn
  8417. (goto-char (match-beginning 0))
  8418. (if (outline-invisible-p) (org-show-context)))
  8419. (goto-char pos)
  8420. (setq org-link-search-failed t)
  8421. (error "No further link found"))))
  8422. (defun org-translate-link (s)
  8423. "Translate a link string if a translation function has been defined."
  8424. (if (and org-link-translation-function
  8425. (fboundp org-link-translation-function)
  8426. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8427. (progn
  8428. (setq s (funcall org-link-translation-function
  8429. (match-string 1) (match-string 2)))
  8430. (concat (car s) ":" (cdr s)))
  8431. s))
  8432. (defun org-translate-link-from-planner (type path)
  8433. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8434. This is still an experimental function, your mileage may vary."
  8435. (cond
  8436. ((member type '("http" "https" "news" "ftp"))
  8437. ;; standard Internet links are the same.
  8438. nil)
  8439. ((and (equal type "irc") (string-match "^//" path))
  8440. ;; Planner has two / at the beginning of an irc link, we have 1.
  8441. ;; We should have zero, actually....
  8442. (setq path (substring path 1)))
  8443. ((and (equal type "lisp") (string-match "^/" path))
  8444. ;; Planner has a slash, we do not.
  8445. (setq type "elisp" path (substring path 1)))
  8446. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8447. ;; A typical message link. Planner has the id after the final slash,
  8448. ;; we separate it with a hash mark
  8449. (setq path (concat (match-string 1 path) "#"
  8450. (org-remove-angle-brackets (match-string 2 path)))))
  8451. )
  8452. (cons type path))
  8453. (defun org-find-file-at-mouse (ev)
  8454. "Open file link or URL at mouse."
  8455. (interactive "e")
  8456. (mouse-set-point ev)
  8457. (org-open-at-point 'in-emacs))
  8458. (defun org-open-at-mouse (ev)
  8459. "Open file link or URL at mouse.
  8460. See the docstring of `org-open-file' for details."
  8461. (interactive "e")
  8462. (mouse-set-point ev)
  8463. (if (eq major-mode 'org-agenda-mode)
  8464. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8465. (org-open-at-point))
  8466. (defvar org-window-config-before-follow-link nil
  8467. "The window configuration before following a link.
  8468. This is saved in case the need arises to restore it.")
  8469. (defvar org-open-link-marker (make-marker)
  8470. "Marker pointing to the location where `org-open-at-point; was called.")
  8471. ;;;###autoload
  8472. (defun org-open-at-point-global ()
  8473. "Follow a link like Org-mode does.
  8474. This command can be called in any mode to follow a link that has
  8475. Org-mode syntax."
  8476. (interactive)
  8477. (org-run-like-in-org-mode 'org-open-at-point))
  8478. ;;;###autoload
  8479. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8480. "Open a link in the string S, as if it was in Org-mode."
  8481. (interactive "sLink: \nP")
  8482. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8483. (with-temp-buffer
  8484. (let ((org-inhibit-startup t))
  8485. (org-mode)
  8486. (insert s)
  8487. (goto-char (point-min))
  8488. (when reference-buffer
  8489. (setq org-link-abbrev-alist-local
  8490. (with-current-buffer reference-buffer
  8491. org-link-abbrev-alist-local)))
  8492. (org-open-at-point arg reference-buffer)))))
  8493. (defvar org-open-at-point-functions nil
  8494. "Hook that is run when following a link at point.
  8495. Functions in this hook must return t if they identify and follow
  8496. a link at point. If they don't find anything interesting at point,
  8497. they must return nil.")
  8498. (defun org-open-at-point (&optional arg reference-buffer)
  8499. "Open link at or after point.
  8500. If there is no link at point, this function will search forward up to
  8501. the end of the current line.
  8502. Normally, files will be opened by an appropriate application. If the
  8503. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8504. With a double prefix argument, try to open outside of Emacs, in the
  8505. application the system uses for this file type."
  8506. (interactive "P")
  8507. ;; if in a code block, then open the block's results
  8508. (unless (call-interactively #'org-babel-open-src-block-result)
  8509. (org-load-modules-maybe)
  8510. (move-marker org-open-link-marker (point))
  8511. (setq org-window-config-before-follow-link (current-window-configuration))
  8512. (org-remove-occur-highlights nil nil t)
  8513. (cond
  8514. ((and (org-on-heading-p)
  8515. (not (org-in-regexp
  8516. (concat org-plain-link-re "\\|"
  8517. org-bracket-link-regexp "\\|"
  8518. org-angle-link-re "\\|"
  8519. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8520. (not (get-text-property (point) 'org-linked-text)))
  8521. (or (org-offer-links-in-entry arg)
  8522. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8523. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8524. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8525. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8526. (not (org-in-regexp org-bracket-link-regexp)))
  8527. (org-footnote-action))
  8528. (t
  8529. (let (type path link line search (pos (point)))
  8530. (catch 'match
  8531. (save-excursion
  8532. (skip-chars-forward "^]\n\r")
  8533. (when (org-in-regexp org-bracket-link-regexp 1)
  8534. (setq link (org-extract-attributes
  8535. (org-link-unescape (org-match-string-no-properties 1))))
  8536. (while (string-match " *\n *" link)
  8537. (setq link (replace-match " " t t link)))
  8538. (setq link (org-link-expand-abbrev link))
  8539. (cond
  8540. ((or (file-name-absolute-p link)
  8541. (string-match "^\\.\\.?/" link))
  8542. (setq type "file" path link))
  8543. ((string-match org-link-re-with-space3 link)
  8544. (setq type (match-string 1 link) path (match-string 2 link)))
  8545. (t (setq type "thisfile" path link)))
  8546. (throw 'match t)))
  8547. (when (get-text-property (point) 'org-linked-text)
  8548. (setq type "thisfile"
  8549. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8550. (1+ (point)) (point))
  8551. path (buffer-substring
  8552. (or (previous-single-property-change pos 'org-linked-text)
  8553. (point-min))
  8554. (or (next-single-property-change pos 'org-linked-text)
  8555. (point-max))))
  8556. (throw 'match t))
  8557. (save-excursion
  8558. (when (or (org-in-regexp org-angle-link-re)
  8559. (org-in-regexp org-plain-link-re))
  8560. (setq type (match-string 1)
  8561. path (org-link-unescape (match-string 2)))
  8562. (throw 'match t)))
  8563. (save-excursion
  8564. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8565. (setq type "tags"
  8566. path (match-string 1))
  8567. (while (string-match ":" path)
  8568. (setq path (replace-match "+" t t path)))
  8569. (throw 'match t)))
  8570. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8571. (setq type "tree-match"
  8572. path (match-string 1))
  8573. (throw 'match t)))
  8574. (unless path
  8575. (error "No link found"))
  8576. ;; switch back to reference buffer
  8577. ;; needed when if called in a temporary buffer through
  8578. ;; org-open-link-from-string
  8579. (with-current-buffer (or reference-buffer (current-buffer))
  8580. ;; Remove any trailing spaces in path
  8581. (if (string-match " +\\'" path)
  8582. (setq path (replace-match "" t t path)))
  8583. (if (and org-link-translation-function
  8584. (fboundp org-link-translation-function))
  8585. ;; Check if we need to translate the link
  8586. (let ((tmp (funcall org-link-translation-function type path)))
  8587. (setq type (car tmp) path (cdr tmp))))
  8588. (cond
  8589. ((assoc type org-link-protocols)
  8590. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8591. ((equal type "mailto")
  8592. (let ((cmd (car org-link-mailto-program))
  8593. (args (cdr org-link-mailto-program)) args1
  8594. (address path) (subject "") a)
  8595. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8596. (setq address (match-string 1 path)
  8597. subject (org-link-escape (match-string 2 path))))
  8598. (while args
  8599. (cond
  8600. ((not (stringp (car args))) (push (pop args) args1))
  8601. (t (setq a (pop args))
  8602. (if (string-match "%a" a)
  8603. (setq a (replace-match address t t a)))
  8604. (if (string-match "%s" a)
  8605. (setq a (replace-match subject t t a)))
  8606. (push a args1))))
  8607. (apply cmd (nreverse args1))))
  8608. ((member type '("http" "https" "ftp" "news"))
  8609. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8610. (org-link-escape
  8611. path org-link-escape-chars-browser)
  8612. path))))
  8613. ((string= type "doi")
  8614. (browse-url (concat "http://dx.doi.org/" (if (org-string-match-p "[[:nonascii:] ]" path)
  8615. (org-link-escape
  8616. path org-link-escape-chars-browser)
  8617. path))))
  8618. ((member type '("message"))
  8619. (browse-url (concat type ":" path)))
  8620. ((string= type "tags")
  8621. (org-tags-view arg path))
  8622. ((string= type "tree-match")
  8623. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8624. ((string= type "file")
  8625. (if (string-match "::\\([0-9]+\\)\\'" path)
  8626. (setq line (string-to-number (match-string 1 path))
  8627. path (substring path 0 (match-beginning 0)))
  8628. (if (string-match "::\\(.+\\)\\'" path)
  8629. (setq search (match-string 1 path)
  8630. path (substring path 0 (match-beginning 0)))))
  8631. (if (string-match "[*?{]" (file-name-nondirectory path))
  8632. (dired path)
  8633. (org-open-file path arg line search)))
  8634. ((string= type "shell")
  8635. (let ((cmd path))
  8636. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8637. (string-match org-confirm-shell-link-not-regexp cmd))
  8638. (not org-confirm-shell-link-function)
  8639. (funcall org-confirm-shell-link-function
  8640. (format "Execute \"%s\" in shell? "
  8641. (org-add-props cmd nil
  8642. 'face 'org-warning))))
  8643. (progn
  8644. (message "Executing %s" cmd)
  8645. (shell-command cmd))
  8646. (error "Abort"))))
  8647. ((string= type "elisp")
  8648. (let ((cmd path))
  8649. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8650. (string-match org-confirm-elisp-link-not-regexp cmd))
  8651. (not org-confirm-elisp-link-function)
  8652. (funcall org-confirm-elisp-link-function
  8653. (format "Execute \"%s\" as elisp? "
  8654. (org-add-props cmd nil
  8655. 'face 'org-warning))))
  8656. (message "%s => %s" cmd
  8657. (if (equal (string-to-char cmd) ?\()
  8658. (eval (read cmd))
  8659. (call-interactively (read cmd))))
  8660. (error "Abort"))))
  8661. ((and (string= type "thisfile")
  8662. (run-hook-with-args-until-success
  8663. 'org-open-link-functions path)))
  8664. ((string= type "thisfile")
  8665. (if arg
  8666. (switch-to-buffer-other-window
  8667. (org-get-buffer-for-internal-link (current-buffer)))
  8668. (org-mark-ring-push))
  8669. (let ((cmd `(org-link-search
  8670. ,path
  8671. ,(cond ((equal arg '(4)) ''occur)
  8672. ((equal arg '(16)) ''org-occur)
  8673. (t nil))
  8674. ,pos)))
  8675. (condition-case nil (let ((org-link-search-inhibit-query t))
  8676. (eval cmd))
  8677. (error (progn (widen) (eval cmd))))))
  8678. (t
  8679. (browse-url-at-point)))))))
  8680. (move-marker org-open-link-marker nil)
  8681. (run-hook-with-args 'org-follow-link-hook)))
  8682. (defun org-offer-links-in-entry (&optional nth zero)
  8683. "Offer links in the current entry and follow the selected link.
  8684. If there is only one link, follow it immediately as well.
  8685. If NTH is an integer, immediately pick the NTH link found.
  8686. If ZERO is a string, check also this string for a link, and if
  8687. there is one, offer it as link number zero."
  8688. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8689. "\\(" org-angle-link-re "\\)\\|"
  8690. "\\(" org-plain-link-re "\\)"))
  8691. (cnt ?0)
  8692. (in-emacs (if (integerp nth) nil nth))
  8693. have-zero end links link c)
  8694. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8695. (push (match-string 0 zero) links)
  8696. (setq cnt (1- cnt) have-zero t))
  8697. (save-excursion
  8698. (org-back-to-heading t)
  8699. (setq end (save-excursion (outline-next-heading) (point)))
  8700. (while (re-search-forward re end t)
  8701. (push (match-string 0) links))
  8702. (setq links (org-uniquify (reverse links))))
  8703. (cond
  8704. ((null links)
  8705. (message "No links"))
  8706. ((equal (length links) 1)
  8707. (setq link (list (car links))))
  8708. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8709. (setq link (list (nth (if have-zero nth (1- nth)) links))))
  8710. (t ; we have to select a link
  8711. (save-excursion
  8712. (save-window-excursion
  8713. (delete-other-windows)
  8714. (with-output-to-temp-buffer "*Select Link*"
  8715. (mapc (lambda (l)
  8716. (if (not (string-match org-bracket-link-regexp l))
  8717. (princ (format "[%c] %s\n" (incf cnt)
  8718. (org-remove-angle-brackets l)))
  8719. (if (match-end 3)
  8720. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8721. (match-string 3 l) (match-string 1 l)))
  8722. (princ (format "[%c] %s\n" (incf cnt)
  8723. (match-string 1 l))))))
  8724. links))
  8725. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8726. (message "Select link to open, RET to open all:")
  8727. (setq c (read-char-exclusive))
  8728. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8729. (when (equal c ?q) (error "Abort"))
  8730. (if (equal c ?\C-m)
  8731. (setq link links)
  8732. (setq nth (- c ?0))
  8733. (if have-zero (setq nth (1+ nth)))
  8734. (unless (and (integerp nth) (>= (length links) nth))
  8735. (error "Invalid link selection"))
  8736. (setq link (list (nth (1- nth) links))))))
  8737. (if link
  8738. (let ((buf (current-buffer)))
  8739. (dolist (l link)
  8740. (org-open-link-from-string l in-emacs buf))
  8741. t)
  8742. nil)))
  8743. ;; Add special file links that specify the way of opening
  8744. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8745. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8746. (defun org-open-file-with-system (path)
  8747. "Open file at PATH using the system way of opening it."
  8748. (org-open-file path 'system))
  8749. (defun org-open-file-with-emacs (path)
  8750. "Open file at PATH in Emacs."
  8751. (org-open-file path 'emacs))
  8752. (defun org-remove-file-link-modifiers ()
  8753. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8754. (goto-char (point-min))
  8755. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8756. (org-if-unprotected
  8757. (replace-match "file:" t t))))
  8758. (eval-after-load "org-exp"
  8759. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8760. 'org-remove-file-link-modifiers))
  8761. ;;;; Time estimates
  8762. (defun org-get-effort (&optional pom)
  8763. "Get the effort estimate for the current entry."
  8764. (org-entry-get pom org-effort-property))
  8765. ;;; File search
  8766. (defvar org-create-file-search-functions nil
  8767. "List of functions to construct the right search string for a file link.
  8768. These functions are called in turn with point at the location to
  8769. which the link should point.
  8770. A function in the hook should first test if it would like to
  8771. handle this file type, for example by checking the `major-mode'
  8772. or the file extension. If it decides not to handle this file, it
  8773. should just return nil to give other functions a chance. If it
  8774. does handle the file, it must return the search string to be used
  8775. when following the link. The search string will be part of the
  8776. file link, given after a double colon, and `org-open-at-point'
  8777. will automatically search for it. If special measures must be
  8778. taken to make the search successful, another function should be
  8779. added to the companion hook `org-execute-file-search-functions',
  8780. which see.
  8781. A function in this hook may also use `setq' to set the variable
  8782. `description' to provide a suggestion for the descriptive text to
  8783. be used for this link when it gets inserted into an Org-mode
  8784. buffer with \\[org-insert-link].")
  8785. (defvar org-execute-file-search-functions nil
  8786. "List of functions to execute a file search triggered by a link.
  8787. Functions added to this hook must accept a single argument, the
  8788. search string that was part of the file link, the part after the
  8789. double colon. The function must first check if it would like to
  8790. handle this search, for example by checking the `major-mode' or
  8791. the file extension. If it decides not to handle this search, it
  8792. should just return nil to give other functions a chance. If it
  8793. does handle the search, it must return a non-nil value to keep
  8794. other functions from trying.
  8795. Each function can access the current prefix argument through the
  8796. variable `current-prefix-argument'. Note that a single prefix is
  8797. used to force opening a link in Emacs, so it may be good to only
  8798. use a numeric or double prefix to guide the search function.
  8799. In case this is needed, a function in this hook can also restore
  8800. the window configuration before `org-open-at-point' was called using:
  8801. (set-window-configuration org-window-config-before-follow-link)")
  8802. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8803. (defun org-link-search (s &optional type avoid-pos stealth)
  8804. "Search for a link search option.
  8805. If S is surrounded by forward slashes, it is interpreted as a
  8806. regular expression. In org-mode files, this will create an `org-occur'
  8807. sparse tree. In ordinary files, `occur' will be used to list matches.
  8808. If the current buffer is in `dired-mode', grep will be used to search
  8809. in all files. If AVOID-POS is given, ignore matches near that position.
  8810. When optional argument STEALTH is non-nil, do not modify
  8811. visibility around point, thus ignoring
  8812. `org-show-hierarchy-above', `org-show-following-heading' and
  8813. `org-show-siblings' variables."
  8814. (let ((case-fold-search t)
  8815. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8816. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8817. (append '(("") (" ") ("\t") ("\n"))
  8818. org-emphasis-alist)
  8819. "\\|") "\\)"))
  8820. (pos (point))
  8821. (pre nil) (post nil)
  8822. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8823. (cond
  8824. ;; First check if there are any special search functions
  8825. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8826. ;; Now try the builtin stuff
  8827. ((and (equal (string-to-char s0) ?#)
  8828. (> (length s0) 1)
  8829. (save-excursion
  8830. (goto-char (point-min))
  8831. (and
  8832. (re-search-forward
  8833. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8834. (setq type 'dedicated
  8835. pos (match-beginning 0))))
  8836. ;; There is an exact target for this
  8837. (goto-char pos)
  8838. (org-back-to-heading t)))
  8839. ((save-excursion
  8840. (goto-char (point-min))
  8841. (and
  8842. (re-search-forward
  8843. (concat "<<" (regexp-quote s0) ">>") nil t)
  8844. (setq type 'dedicated
  8845. pos (match-beginning 0))))
  8846. ;; There is an exact target for this
  8847. (goto-char pos))
  8848. ((and (string-match "^(\\(.*\\))$" s0)
  8849. (save-excursion
  8850. (goto-char (point-min))
  8851. (and
  8852. (re-search-forward
  8853. (concat "[^[]" (regexp-quote
  8854. (format org-coderef-label-format
  8855. (match-string 1 s0))))
  8856. nil t)
  8857. (setq type 'dedicated
  8858. pos (1+ (match-beginning 0))))))
  8859. ;; There is a coderef target for this
  8860. (goto-char pos))
  8861. ((string-match "^/\\(.*\\)/$" s)
  8862. ;; A regular expression
  8863. (cond
  8864. ((eq major-mode 'org-mode)
  8865. (org-occur (match-string 1 s)))
  8866. ;;((eq major-mode 'dired-mode)
  8867. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8868. (t (org-do-occur (match-string 1 s)))))
  8869. ((and (eq major-mode 'org-mode) org-link-search-must-match-exact-headline)
  8870. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8871. (goto-char (point-min))
  8872. (cond
  8873. ((let (case-fold-search)
  8874. (re-search-forward (format org-complex-heading-regexp-format
  8875. (regexp-quote s))
  8876. nil t))
  8877. ;; OK, found a match
  8878. (setq type 'dedicated)
  8879. (goto-char (match-beginning 0)))
  8880. ((and (not org-link-search-inhibit-query)
  8881. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8882. (y-or-n-p "No match - create this as a new heading? "))
  8883. (goto-char (point-max))
  8884. (or (bolp) (newline))
  8885. (insert "* " s "\n")
  8886. (beginning-of-line 0))
  8887. (t
  8888. (goto-char pos)
  8889. (error "No match"))))
  8890. (t
  8891. ;; A normal search string
  8892. (when (equal (string-to-char s) ?*)
  8893. ;; Anchor on headlines, post may include tags.
  8894. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8895. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  8896. s (substring s 1)))
  8897. (remove-text-properties
  8898. 0 (length s)
  8899. '(face nil mouse-face nil keymap nil fontified nil) s)
  8900. ;; Make a series of regular expressions to find a match
  8901. (setq words (org-split-string s "[ \n\r\t]+")
  8902. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8903. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8904. "\\)" markers)
  8905. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8906. re2a (concat "[ \t\r\n]" re2a_)
  8907. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8908. re4 (concat "[^a-zA-Z_]" re4_)
  8909. re1 (concat pre re2 post)
  8910. re3 (concat pre (if pre re4_ re4) post)
  8911. re5 (concat pre ".*" re4)
  8912. re2 (concat pre re2)
  8913. re2a (concat pre (if pre re2a_ re2a))
  8914. re4 (concat pre (if pre re4_ re4))
  8915. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8916. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8917. re5 "\\)"
  8918. ))
  8919. (cond
  8920. ((eq type 'org-occur) (org-occur reall))
  8921. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8922. (t (goto-char (point-min))
  8923. (setq type 'fuzzy)
  8924. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8925. (org-search-not-self 1 re1 nil t)
  8926. (org-search-not-self 1 re2 nil t)
  8927. (org-search-not-self 1 re2a nil t)
  8928. (org-search-not-self 1 re3 nil t)
  8929. (org-search-not-self 1 re4 nil t)
  8930. (org-search-not-self 1 re5 nil t)
  8931. )
  8932. (goto-char (match-beginning 1))
  8933. (goto-char pos)
  8934. (error "No match"))))))
  8935. (and (eq major-mode 'org-mode)
  8936. (not stealth)
  8937. (org-show-context 'link-search))
  8938. type))
  8939. (defun org-search-not-self (group &rest args)
  8940. "Execute `re-search-forward', but only accept matches that do not
  8941. enclose the position of `org-open-link-marker'."
  8942. (let ((m org-open-link-marker))
  8943. (catch 'exit
  8944. (while (apply 're-search-forward args)
  8945. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8946. (goto-char (match-end group))
  8947. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8948. (> (match-beginning 0) (marker-position m))
  8949. (< (match-end 0) (marker-position m)))
  8950. (save-match-data
  8951. (or (not (org-in-regexp
  8952. org-bracket-link-analytic-regexp 1))
  8953. (not (match-end 4)) ; no description
  8954. (and (<= (match-beginning 4) (point))
  8955. (>= (match-end 4) (point))))))
  8956. (throw 'exit (point))))))))
  8957. (defun org-get-buffer-for-internal-link (buffer)
  8958. "Return a buffer to be used for displaying the link target of internal links."
  8959. (cond
  8960. ((not org-display-internal-link-with-indirect-buffer)
  8961. buffer)
  8962. ((string-match "(Clone)$" (buffer-name buffer))
  8963. (message "Buffer is already a clone, not making another one")
  8964. ;; we also do not modify visibility in this case
  8965. buffer)
  8966. (t ; make a new indirect buffer for displaying the link
  8967. (let* ((bn (buffer-name buffer))
  8968. (ibn (concat bn "(Clone)"))
  8969. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8970. (with-current-buffer ib (org-overview))
  8971. ib))))
  8972. (defun org-do-occur (regexp &optional cleanup)
  8973. "Call the Emacs command `occur'.
  8974. If CLEANUP is non-nil, remove the printout of the regular expression
  8975. in the *Occur* buffer. This is useful if the regex is long and not useful
  8976. to read."
  8977. (occur regexp)
  8978. (when cleanup
  8979. (let ((cwin (selected-window)) win beg end)
  8980. (when (setq win (get-buffer-window "*Occur*"))
  8981. (select-window win))
  8982. (goto-char (point-min))
  8983. (when (re-search-forward "match[a-z]+" nil t)
  8984. (setq beg (match-end 0))
  8985. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8986. (setq end (1- (match-beginning 0)))))
  8987. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8988. (goto-char (point-min))
  8989. (select-window cwin))))
  8990. ;;; The mark ring for links jumps
  8991. (defvar org-mark-ring nil
  8992. "Mark ring for positions before jumps in Org-mode.")
  8993. (defvar org-mark-ring-last-goto nil
  8994. "Last position in the mark ring used to go back.")
  8995. ;; Fill and close the ring
  8996. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8997. (loop for i from 1 to org-mark-ring-length do
  8998. (push (make-marker) org-mark-ring))
  8999. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9000. org-mark-ring)
  9001. (defun org-mark-ring-push (&optional pos buffer)
  9002. "Put the current position or POS into the mark ring and rotate it."
  9003. (interactive)
  9004. (setq pos (or pos (point)))
  9005. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9006. (move-marker (car org-mark-ring)
  9007. (or pos (point))
  9008. (or buffer (current-buffer)))
  9009. (message "%s"
  9010. (substitute-command-keys
  9011. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9012. (defun org-mark-ring-goto (&optional n)
  9013. "Jump to the previous position in the mark ring.
  9014. With prefix arg N, jump back that many stored positions. When
  9015. called several times in succession, walk through the entire ring.
  9016. Org-mode commands jumping to a different position in the current file,
  9017. or to another Org-mode file, automatically push the old position
  9018. onto the ring."
  9019. (interactive "p")
  9020. (let (p m)
  9021. (if (eq last-command this-command)
  9022. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9023. (setq p org-mark-ring))
  9024. (setq org-mark-ring-last-goto p)
  9025. (setq m (car p))
  9026. (org-pop-to-buffer-same-window (marker-buffer m))
  9027. (goto-char m)
  9028. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9029. (defun org-remove-angle-brackets (s)
  9030. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9031. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9032. s)
  9033. (defun org-add-angle-brackets (s)
  9034. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9035. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9036. s)
  9037. (defun org-remove-double-quotes (s)
  9038. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9039. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9040. s)
  9041. ;;; Following specific links
  9042. (defun org-follow-timestamp-link ()
  9043. (cond
  9044. ((org-at-date-range-p t)
  9045. (let ((org-agenda-start-on-weekday)
  9046. (t1 (match-string 1))
  9047. (t2 (match-string 2)))
  9048. (setq t1 (time-to-days (org-time-string-to-time t1))
  9049. t2 (time-to-days (org-time-string-to-time t2)))
  9050. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  9051. ((org-at-timestamp-p t)
  9052. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9053. (substring (match-string 1) 0 10)))
  9054. 1))
  9055. (t (error "This should not happen"))))
  9056. ;;; Following file links
  9057. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9058. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9059. (declare-function mailcap-mime-info
  9060. "mailcap" (string &optional request no-decode))
  9061. (defvar org-wait nil)
  9062. (defun org-open-file (path &optional in-emacs line search)
  9063. "Open the file at PATH.
  9064. First, this expands any special file name abbreviations. Then the
  9065. configuration variable `org-file-apps' is checked if it contains an
  9066. entry for this file type, and if yes, the corresponding command is launched.
  9067. If no application is found, Emacs simply visits the file.
  9068. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9069. With a double \\[universal-argument] \\[universal-argument] \
  9070. prefix arg, Org tries to avoid opening in Emacs
  9071. and to use an external application to visit the file.
  9072. Optional LINE specifies a line to go to, optional SEARCH a string
  9073. to search for. If LINE or SEARCH is given, the file will be
  9074. opened in Emacs, unless an entry from org-file-apps that makes
  9075. use of groups in a regexp matches.
  9076. If you want to change the way frames are used when following a
  9077. link, please customize `org-link-frame-setup'.
  9078. If the file does not exist, an error is thrown."
  9079. (let* ((file (if (equal path "")
  9080. buffer-file-name
  9081. (substitute-in-file-name (expand-file-name path))))
  9082. (file-apps (append org-file-apps (org-default-apps)))
  9083. (apps (org-remove-if
  9084. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9085. (apps-dlink (org-remove-if-not
  9086. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9087. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9088. (dirp (if remp nil (file-directory-p file)))
  9089. (file (if (and dirp org-open-directory-means-index-dot-org)
  9090. (concat (file-name-as-directory file) "index.org")
  9091. file))
  9092. (a-m-a-p (assq 'auto-mode apps))
  9093. (dfile (downcase file))
  9094. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9095. (link (cond ((and (eq line nil)
  9096. (eq search nil))
  9097. file)
  9098. (line
  9099. (concat file "::" (number-to-string line)))
  9100. (search
  9101. (concat file "::" search))))
  9102. (dlink (downcase link))
  9103. (old-buffer (current-buffer))
  9104. (old-pos (point))
  9105. (old-mode major-mode)
  9106. ext cmd link-match-data)
  9107. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9108. (setq ext (match-string 1 dfile))
  9109. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9110. (setq ext (match-string 1 dfile))))
  9111. (cond
  9112. ((member in-emacs '((16) system))
  9113. (setq cmd (cdr (assoc 'system apps))))
  9114. (in-emacs (setq cmd 'emacs))
  9115. (t
  9116. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9117. (and dirp (cdr (assoc 'directory apps)))
  9118. ; first, try matching against apps-dlink
  9119. ; if we get a match here, store the match data for later
  9120. (let ((match (assoc-default dlink apps-dlink
  9121. 'string-match)))
  9122. (if match
  9123. (progn (setq link-match-data (match-data))
  9124. match)
  9125. (progn (setq in-emacs (or in-emacs line search))
  9126. nil))) ; if we have no match in apps-dlink,
  9127. ; always open the file in emacs if line or search
  9128. ; is given (for backwards compatibility)
  9129. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9130. 'string-match)
  9131. (cdr (assoc ext apps))
  9132. (cdr (assoc t apps))))))
  9133. (when (eq cmd 'system)
  9134. (setq cmd (cdr (assoc 'system apps))))
  9135. (when (eq cmd 'default)
  9136. (setq cmd (cdr (assoc t apps))))
  9137. (when (eq cmd 'mailcap)
  9138. (require 'mailcap)
  9139. (mailcap-parse-mailcaps)
  9140. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9141. (command (mailcap-mime-info mime-type)))
  9142. (if (stringp command)
  9143. (setq cmd command)
  9144. (setq cmd 'emacs))))
  9145. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9146. (not (file-exists-p file))
  9147. (not org-open-non-existing-files))
  9148. (error "No such file: %s" file))
  9149. (cond
  9150. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9151. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9152. (while (string-match "['\"]%s['\"]" cmd)
  9153. (setq cmd (replace-match "%s" t t cmd)))
  9154. (while (string-match "%s" cmd)
  9155. (setq cmd (replace-match
  9156. (save-match-data
  9157. (shell-quote-argument
  9158. (convert-standard-filename file)))
  9159. t t cmd)))
  9160. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9161. (save-match-data
  9162. (let ((match-index 1)
  9163. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9164. (set-match-data link-match-data)
  9165. (while (<= match-index number-of-groups)
  9166. (let ((regex (concat "%" (number-to-string match-index)))
  9167. (replace-with (match-string match-index dlink)))
  9168. (while (string-match regex cmd)
  9169. (setq cmd (replace-match replace-with t t cmd))))
  9170. (setq match-index (+ match-index 1)))))
  9171. (save-window-excursion
  9172. (start-process-shell-command cmd nil cmd)
  9173. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  9174. ))
  9175. ((or (stringp cmd)
  9176. (eq cmd 'emacs))
  9177. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9178. (widen)
  9179. (if line (org-goto-line line)
  9180. (if search (org-link-search search))))
  9181. ((consp cmd)
  9182. (let ((file (convert-standard-filename file)))
  9183. (save-match-data
  9184. (set-match-data link-match-data)
  9185. (eval cmd))))
  9186. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9187. (and (eq major-mode 'org-mode) (eq old-mode 'org-mode)
  9188. (or (not (equal old-buffer (current-buffer)))
  9189. (not (equal old-pos (point))))
  9190. (org-mark-ring-push old-pos old-buffer))))
  9191. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9192. "This function returns non-nil if `entry' uses a regular
  9193. expression which should be matched against the whole link by
  9194. org-open-file.
  9195. It assumes that is the case when the entry uses a regular
  9196. expression which has at least one grouping construct and the
  9197. action is either a lisp form or a command string containing
  9198. '%1', i.e. using at least one subexpression match as a
  9199. parameter."
  9200. (let ((selector (car entry))
  9201. (action (cdr entry)))
  9202. (if (stringp selector)
  9203. (and (> (regexp-opt-depth selector) 0)
  9204. (or (and (stringp action)
  9205. (string-match "%[0-9]" action))
  9206. (consp action)))
  9207. nil)))
  9208. (defun org-default-apps ()
  9209. "Return the default applications for this operating system."
  9210. (cond
  9211. ((eq system-type 'darwin)
  9212. org-file-apps-defaults-macosx)
  9213. ((eq system-type 'windows-nt)
  9214. org-file-apps-defaults-windowsnt)
  9215. (t org-file-apps-defaults-gnu)))
  9216. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9217. "Convert extensions to regular expressions in the cars of LIST.
  9218. Also, weed out any non-string entries, because the return value is used
  9219. only for regexp matching.
  9220. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9221. point to the symbol `emacs', indicating that the file should
  9222. be opened in Emacs."
  9223. (append
  9224. (delq nil
  9225. (mapcar (lambda (x)
  9226. (if (not (stringp (car x)))
  9227. nil
  9228. (if (string-match "\\W" (car x))
  9229. x
  9230. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9231. list))
  9232. (if add-auto-mode
  9233. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9234. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9235. (defun org-file-remote-p (file)
  9236. "Test whether FILE specifies a location on a remote system.
  9237. Return non-nil if the location is indeed remote.
  9238. For example, the filename \"/user@host:/foo\" specifies a location
  9239. on the system \"/user@host:\"."
  9240. (cond ((fboundp 'file-remote-p)
  9241. (file-remote-p file))
  9242. ((fboundp 'tramp-handle-file-remote-p)
  9243. (tramp-handle-file-remote-p file))
  9244. ((and (boundp 'ange-ftp-name-format)
  9245. (string-match (car ange-ftp-name-format) file))
  9246. t)
  9247. (t nil)))
  9248. ;;;; Refiling
  9249. (defun org-get-org-file ()
  9250. "Read a filename, with default directory `org-directory'."
  9251. (let ((default (or org-default-notes-file remember-data-file)))
  9252. (read-file-name (format "File name [%s]: " default)
  9253. (file-name-as-directory org-directory)
  9254. default)))
  9255. (defun org-notes-order-reversed-p ()
  9256. "Check if the current file should receive notes in reversed order."
  9257. (cond
  9258. ((not org-reverse-note-order) nil)
  9259. ((eq t org-reverse-note-order) t)
  9260. ((not (listp org-reverse-note-order)) nil)
  9261. (t (catch 'exit
  9262. (let ((all org-reverse-note-order)
  9263. entry)
  9264. (while (setq entry (pop all))
  9265. (if (string-match (car entry) buffer-file-name)
  9266. (throw 'exit (cdr entry))))
  9267. nil)))))
  9268. (defvar org-refile-target-table nil
  9269. "The list of refile targets, created by `org-refile'.")
  9270. (defvar org-agenda-new-buffers nil
  9271. "Buffers created to visit agenda files.")
  9272. (defvar org-refile-cache nil
  9273. "Cache for refile targets.")
  9274. (defvar org-refile-markers nil
  9275. "All the markers used for caching refile locations.")
  9276. (defun org-refile-marker (pos)
  9277. "Get a new refile marker, but only if caching is in use."
  9278. (if (not org-refile-use-cache)
  9279. pos
  9280. (let ((m (make-marker)))
  9281. (move-marker m pos)
  9282. (push m org-refile-markers)
  9283. m)))
  9284. (defun org-refile-cache-clear ()
  9285. "Clear the refile cache and disable all the markers."
  9286. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9287. (setq org-refile-markers nil)
  9288. (setq org-refile-cache nil)
  9289. (message "Refile cache has been cleared"))
  9290. (defun org-refile-cache-check-set (set)
  9291. "Check if all the markers in the cache still have live buffers."
  9292. (let (marker)
  9293. (catch 'exit
  9294. (while (and set (setq marker (nth 3 (pop set))))
  9295. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9296. (when (and marker (null (marker-buffer marker)))
  9297. (message "not found") (sit-for 3)
  9298. (throw 'exit nil)))
  9299. t)))
  9300. (defun org-refile-cache-put (set &rest identifiers)
  9301. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9302. (let* ((key (sha1 (prin1-to-string identifiers)))
  9303. (entry (assoc key org-refile-cache)))
  9304. (if entry
  9305. (setcdr entry set)
  9306. (push (cons key set) org-refile-cache))))
  9307. (defun org-refile-cache-get (&rest identifiers)
  9308. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9309. (cond
  9310. ((not org-refile-cache) nil)
  9311. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9312. (t
  9313. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9314. org-refile-cache))))
  9315. (and set (org-refile-cache-check-set set) set)))))
  9316. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9317. "Produce a table with refile targets."
  9318. (let ((case-fold-search nil)
  9319. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9320. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9321. targets tgs txt re files f desc descre fast-path-p level pos0)
  9322. (message "Getting targets...")
  9323. (with-current-buffer (or default-buffer (current-buffer))
  9324. (while (setq entry (pop entries))
  9325. (setq files (car entry) desc (cdr entry))
  9326. (setq fast-path-p nil)
  9327. (cond
  9328. ((null files) (setq files (list (current-buffer))))
  9329. ((eq files 'org-agenda-files)
  9330. (setq files (org-agenda-files 'unrestricted)))
  9331. ((and (symbolp files) (fboundp files))
  9332. (setq files (funcall files)))
  9333. ((and (symbolp files) (boundp files))
  9334. (setq files (symbol-value files))))
  9335. (if (stringp files) (setq files (list files)))
  9336. (cond
  9337. ((eq (car desc) :tag)
  9338. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9339. ((eq (car desc) :todo)
  9340. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9341. ((eq (car desc) :regexp)
  9342. (setq descre (cdr desc)))
  9343. ((eq (car desc) :level)
  9344. (setq descre (concat "^\\*\\{" (number-to-string
  9345. (if org-odd-levels-only
  9346. (1- (* 2 (cdr desc)))
  9347. (cdr desc)))
  9348. "\\}[ \t]")))
  9349. ((eq (car desc) :maxlevel)
  9350. (setq fast-path-p t)
  9351. (setq descre (concat "^\\*\\{1," (number-to-string
  9352. (if org-odd-levels-only
  9353. (1- (* 2 (cdr desc)))
  9354. (cdr desc)))
  9355. "\\}[ \t]")))
  9356. (t (error "Bad refiling target description %s" desc)))
  9357. (while (setq f (pop files))
  9358. (with-current-buffer
  9359. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9360. (or
  9361. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9362. (progn
  9363. (if (bufferp f) (setq f (buffer-file-name
  9364. (buffer-base-buffer f))))
  9365. (setq f (and f (expand-file-name f)))
  9366. (if (eq org-refile-use-outline-path 'file)
  9367. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9368. (save-excursion
  9369. (save-restriction
  9370. (widen)
  9371. (goto-char (point-min))
  9372. (while (re-search-forward descre nil t)
  9373. (goto-char (setq pos0 (point-at-bol)))
  9374. (catch 'next
  9375. (when org-refile-target-verify-function
  9376. (save-match-data
  9377. (or (funcall org-refile-target-verify-function)
  9378. (throw 'next t))))
  9379. (when (and (looking-at org-complex-heading-regexp)
  9380. (not (member (match-string 4) excluded-entries))
  9381. (match-string 4))
  9382. (setq level (org-reduced-level
  9383. (- (match-end 1) (match-beginning 1)))
  9384. txt (org-link-display-format (match-string 4))
  9385. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9386. re (format org-complex-heading-regexp-format
  9387. (regexp-quote (match-string 4))))
  9388. (when org-refile-use-outline-path
  9389. (setq txt (mapconcat
  9390. 'org-protect-slash
  9391. (append
  9392. (if (eq org-refile-use-outline-path
  9393. 'file)
  9394. (list (file-name-nondirectory
  9395. (buffer-file-name
  9396. (buffer-base-buffer))))
  9397. (if (eq org-refile-use-outline-path
  9398. 'full-file-path)
  9399. (list (buffer-file-name
  9400. (buffer-base-buffer)))))
  9401. (org-get-outline-path fast-path-p
  9402. level txt)
  9403. (list txt))
  9404. "/")))
  9405. (push (list txt f re (org-refile-marker (point)))
  9406. tgs)))
  9407. (when (= (point) pos0)
  9408. ;; verification function has not moved point
  9409. (goto-char (point-at-eol))))))))
  9410. (when org-refile-use-cache
  9411. (org-refile-cache-put tgs (buffer-file-name) descre))
  9412. (setq targets (append tgs targets))
  9413. ))))
  9414. (message "Getting targets...done")
  9415. (nreverse targets)))
  9416. (defun org-protect-slash (s)
  9417. (while (string-match "/" s)
  9418. (setq s (replace-match "\\" t t s)))
  9419. s)
  9420. (defvar org-olpa (make-vector 20 nil))
  9421. (defun org-get-outline-path (&optional fastp level heading)
  9422. "Return the outline path to the current entry, as a list.
  9423. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9424. routine which makes outline path derivations for an entire file,
  9425. avoiding backtracing. Refile target collection makes use of that."
  9426. (if fastp
  9427. (progn
  9428. (if (> level 19)
  9429. (error "Outline path failure, more than 19 levels"))
  9430. (loop for i from level upto 19 do
  9431. (aset org-olpa i nil))
  9432. (prog1
  9433. (delq nil (append org-olpa nil))
  9434. (aset org-olpa level heading)))
  9435. (let (rtn case-fold-search)
  9436. (save-excursion
  9437. (save-restriction
  9438. (widen)
  9439. (while (org-up-heading-safe)
  9440. (when (looking-at org-complex-heading-regexp)
  9441. (push (org-match-string-no-properties 4) rtn)))
  9442. rtn)))))
  9443. (defun org-format-outline-path (path &optional width prefix)
  9444. "Format the outline path PATH for display.
  9445. Width is the maximum number of characters that is available.
  9446. Prefix is a prefix to be included in the returned string,
  9447. such as the file name."
  9448. (setq width (or width 79))
  9449. (if prefix (setq width (- width (length prefix))))
  9450. (if (not path)
  9451. (or prefix "")
  9452. (let* ((nsteps (length path))
  9453. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9454. (maxwidth (if (<= total-width width)
  9455. 10000 ;; everything fits
  9456. ;; we need to shorten the level headings
  9457. (/ (- width nsteps) nsteps)))
  9458. (org-odd-levels-only nil)
  9459. (n 0)
  9460. (total (1+ (length prefix))))
  9461. (setq maxwidth (max maxwidth 10))
  9462. (concat prefix
  9463. (mapconcat
  9464. (lambda (h)
  9465. (setq n (1+ n))
  9466. (if (and (= n nsteps) (< maxwidth 10000))
  9467. (setq maxwidth (- total-width total)))
  9468. (if (< (length h) maxwidth)
  9469. (progn (setq total (+ total (length h) 1)) h)
  9470. (setq h (substring h 0 (- maxwidth 2))
  9471. total (+ total maxwidth 1))
  9472. (if (string-match "[ \t]+\\'" h)
  9473. (setq h (substring h 0 (match-beginning 0))))
  9474. (setq h (concat h "..")))
  9475. (org-add-props h nil 'face
  9476. (nth (% (1- n) org-n-level-faces)
  9477. org-level-faces))
  9478. h)
  9479. path "/")))))
  9480. (defun org-display-outline-path (&optional file current)
  9481. "Display the current outline path in the echo area."
  9482. (interactive "P")
  9483. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9484. (case-fold-search nil)
  9485. (path (and (eq major-mode 'org-mode) (org-get-outline-path))))
  9486. (if current (setq path (append path
  9487. (save-excursion
  9488. (org-back-to-heading t)
  9489. (if (looking-at org-complex-heading-regexp)
  9490. (list (match-string 4)))))))
  9491. (message "%s"
  9492. (org-format-outline-path
  9493. path
  9494. (1- (frame-width))
  9495. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9496. (defvar org-refile-history nil
  9497. "History for refiling operations.")
  9498. (defvar org-after-refile-insert-hook nil
  9499. "Hook run after `org-refile' has inserted its stuff at the new location.
  9500. Note that this is still *before* the stuff will be removed from
  9501. the *old* location.")
  9502. (defvar org-capture-last-stored-marker)
  9503. (defun org-refile (&optional goto default-buffer rfloc)
  9504. "Move the entry or entries at point to another heading.
  9505. The list of target headings is compiled using the information in
  9506. `org-refile-targets', which see.
  9507. At the target location, the entry is filed as a subitem of the target
  9508. heading. Depending on `org-reverse-note-order', the new subitem will
  9509. either be the first or the last subitem.
  9510. If there is an active region, all entries in that region will be moved.
  9511. However, the region must fulfill the requirement that the first heading
  9512. is the first one sets the top-level of the moved text - at most siblings
  9513. below it are allowed.
  9514. With prefix arg GOTO, the command will only visit the target location
  9515. and not actually move anything.
  9516. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9517. go to the location where the last refiling operation has put the subtree.
  9518. With a prefix argument of `2', refile to the running clock.
  9519. RFLOC can be a refile location obtained in a different way.
  9520. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9521. If you are using target caching (see `org-refile-use-cache'),
  9522. You have to clear the target cache in order to find new targets.
  9523. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9524. prefix argument (`C-u C-u C-u C-c C-w')."
  9525. (interactive "P")
  9526. (if (member goto '(0 (64)))
  9527. (org-refile-cache-clear)
  9528. (let* ((cbuf (current-buffer))
  9529. (regionp (org-region-active-p))
  9530. (region-start (and regionp (region-beginning)))
  9531. (region-end (and regionp (region-end)))
  9532. (region-length (and regionp (- region-end region-start)))
  9533. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9534. pos it nbuf file re level reversed)
  9535. (setq last-command nil)
  9536. (when regionp
  9537. (goto-char region-start)
  9538. (or (bolp) (goto-char (point-at-bol)))
  9539. (setq region-start (point))
  9540. (unless (or (org-kill-is-subtree-p
  9541. (buffer-substring region-start region-end))
  9542. (prog1 org-refile-active-region-within-subtree
  9543. (org-toggle-heading)))
  9544. (error "The region is not a (sequence of) subtree(s)")))
  9545. (if (equal goto '(16))
  9546. (org-refile-goto-last-stored)
  9547. (when (or
  9548. (and (equal goto 2)
  9549. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9550. (prog1
  9551. (setq it (list (or org-clock-heading "running clock")
  9552. (buffer-file-name
  9553. (marker-buffer org-clock-hd-marker))
  9554. ""
  9555. (marker-position org-clock-hd-marker)))
  9556. (setq goto nil)))
  9557. (setq it (or rfloc
  9558. (let (heading-text)
  9559. (save-excursion
  9560. (unless goto
  9561. (org-back-to-heading t)
  9562. (setq heading-text
  9563. (nth 4 (org-heading-components))))
  9564. (org-refile-get-location
  9565. (cond (goto "Goto")
  9566. (regionp "Refile region to")
  9567. (t (concat "Refile subtree \""
  9568. heading-text "\" to")))
  9569. default-buffer
  9570. org-refile-allow-creating-parent-nodes
  9571. goto))))))
  9572. (setq file (nth 1 it)
  9573. re (nth 2 it)
  9574. pos (nth 3 it))
  9575. (if (and (not goto)
  9576. pos
  9577. (equal (buffer-file-name) file)
  9578. (if regionp
  9579. (and (>= pos region-start)
  9580. (<= pos region-end))
  9581. (and (>= pos (point))
  9582. (< pos (save-excursion
  9583. (org-end-of-subtree t t))))))
  9584. (error "Cannot refile to position inside the tree or region"))
  9585. (setq nbuf (or (find-buffer-visiting file)
  9586. (find-file-noselect file)))
  9587. (if goto
  9588. (progn
  9589. (org-pop-to-buffer-same-window nbuf)
  9590. (goto-char pos)
  9591. (org-show-context 'org-goto))
  9592. (if regionp
  9593. (progn
  9594. (org-kill-new (buffer-substring region-start region-end))
  9595. (org-save-markers-in-region region-start region-end))
  9596. (org-copy-subtree 1 nil t))
  9597. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9598. (find-file-noselect file)))
  9599. (setq reversed (org-notes-order-reversed-p))
  9600. (save-excursion
  9601. (save-restriction
  9602. (widen)
  9603. (if pos
  9604. (progn
  9605. (goto-char pos)
  9606. (looking-at org-outline-regexp)
  9607. (setq level (org-get-valid-level (funcall outline-level) 1))
  9608. (goto-char
  9609. (if reversed
  9610. (or (outline-next-heading) (point-max))
  9611. (or (save-excursion (org-get-next-sibling))
  9612. (org-end-of-subtree t t)
  9613. (point-max)))))
  9614. (setq level 1)
  9615. (if (not reversed)
  9616. (goto-char (point-max))
  9617. (goto-char (point-min))
  9618. (or (outline-next-heading) (goto-char (point-max)))))
  9619. (if (not (bolp)) (newline))
  9620. (org-paste-subtree level)
  9621. (when org-log-refile
  9622. (org-add-log-setup 'refile nil nil 'findpos
  9623. org-log-refile)
  9624. (unless (eq org-log-refile 'note)
  9625. (save-excursion (org-add-log-note))))
  9626. (and org-auto-align-tags (org-set-tags nil t))
  9627. (bookmark-set "org-refile-last-stored")
  9628. ;; If we are refiling for capture, make sure that the
  9629. ;; last-capture pointers point here
  9630. (when (org-bound-and-true-p org-refile-for-capture)
  9631. (bookmark-set "org-capture-last-stored-marker")
  9632. (move-marker org-capture-last-stored-marker (point)))
  9633. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9634. (run-hooks 'org-after-refile-insert-hook))))
  9635. (if regionp
  9636. (delete-region (point) (+ (point) region-length))
  9637. (org-cut-subtree))
  9638. (when (featurep 'org-inlinetask)
  9639. (org-inlinetask-remove-END-maybe))
  9640. (setq org-markers-to-move nil)
  9641. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9642. (defun org-refile-goto-last-stored ()
  9643. "Go to the location where the last refile was stored."
  9644. (interactive)
  9645. (bookmark-jump "org-refile-last-stored")
  9646. (message "This is the location of the last refile"))
  9647. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9648. no-exclude)
  9649. "Prompt the user for a refile location, using PROMPT.
  9650. PROMPT should not be suffixed with a colon and a space, because
  9651. this function appends the default value from
  9652. `org-refile-history' automatically, if that is not empty.
  9653. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9654. this is used for the GOTO interface."
  9655. (let ((org-refile-targets org-refile-targets)
  9656. (org-refile-use-outline-path org-refile-use-outline-path)
  9657. excluded-entries)
  9658. (when (and (eq major-mode 'org-mode)
  9659. (not org-refile-use-cache)
  9660. (not no-exclude))
  9661. (org-map-tree
  9662. (lambda()
  9663. (setq excluded-entries
  9664. (append excluded-entries (list (org-get-heading t t)))))))
  9665. (setq org-refile-target-table
  9666. (org-refile-get-targets default-buffer excluded-entries)))
  9667. (unless org-refile-target-table
  9668. (error "No refile targets"))
  9669. (let* ((prompt (concat prompt
  9670. (and (car org-refile-history)
  9671. (concat " (default " (car org-refile-history) ")"))
  9672. ": "))
  9673. (cbuf (current-buffer))
  9674. (partial-completion-mode nil)
  9675. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9676. (cfunc (if (and org-refile-use-outline-path
  9677. org-outline-path-complete-in-steps)
  9678. 'org-olpath-completing-read
  9679. 'org-icompleting-read))
  9680. (extra (if org-refile-use-outline-path "/" ""))
  9681. (filename (and cfn (expand-file-name cfn)))
  9682. (tbl (mapcar
  9683. (lambda (x)
  9684. (if (and (not (member org-refile-use-outline-path
  9685. '(file full-file-path)))
  9686. (not (equal filename (nth 1 x))))
  9687. (cons (concat (car x) extra " ("
  9688. (file-name-nondirectory (nth 1 x)) ")")
  9689. (cdr x))
  9690. (cons (concat (car x) extra) (cdr x))))
  9691. org-refile-target-table))
  9692. (completion-ignore-case t)
  9693. pa answ parent-target child parent old-hist)
  9694. (setq old-hist org-refile-history)
  9695. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9696. nil 'org-refile-history (car org-refile-history)))
  9697. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9698. (org-refile-check-position pa)
  9699. (if pa
  9700. (progn
  9701. (when (or (not org-refile-history)
  9702. (not (eq old-hist org-refile-history))
  9703. (not (equal (car pa) (car org-refile-history))))
  9704. (setq org-refile-history
  9705. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9706. org-refile-history
  9707. (cdr org-refile-history))))
  9708. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9709. (pop org-refile-history)))
  9710. pa)
  9711. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9712. (progn
  9713. (setq parent (match-string 1 answ)
  9714. child (match-string 2 answ))
  9715. (setq parent-target (or (assoc parent tbl)
  9716. (assoc (concat parent "/") tbl)))
  9717. (when (and parent-target
  9718. (or (eq new-nodes t)
  9719. (and (eq new-nodes 'confirm)
  9720. (y-or-n-p (format "Create new node \"%s\"? "
  9721. child)))))
  9722. (org-refile-new-child parent-target child)))
  9723. (error "Invalid target location")))))
  9724. (defun org-refile-check-position (refile-pointer)
  9725. "Check if the refile pointer matches the readline to which it points."
  9726. (let* ((file (nth 1 refile-pointer))
  9727. (re (nth 2 refile-pointer))
  9728. (pos (nth 3 refile-pointer))
  9729. buffer)
  9730. (when (org-string-nw-p re)
  9731. (setq buffer (if (markerp pos)
  9732. (marker-buffer pos)
  9733. (or (find-buffer-visiting file)
  9734. (find-file-noselect file))))
  9735. (with-current-buffer buffer
  9736. (save-excursion
  9737. (save-restriction
  9738. (widen)
  9739. (goto-char pos)
  9740. (beginning-of-line 1)
  9741. (unless (org-looking-at-p re)
  9742. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  9743. (defun org-refile-new-child (parent-target child)
  9744. "Use refile target PARENT-TARGET to add new CHILD below it."
  9745. (unless parent-target
  9746. (error "Cannot find parent for new node"))
  9747. (let ((file (nth 1 parent-target))
  9748. (pos (nth 3 parent-target))
  9749. level)
  9750. (with-current-buffer (or (find-buffer-visiting file)
  9751. (find-file-noselect file))
  9752. (save-excursion
  9753. (save-restriction
  9754. (widen)
  9755. (if pos
  9756. (goto-char pos)
  9757. (goto-char (point-max))
  9758. (if (not (bolp)) (newline)))
  9759. (when (looking-at org-outline-regexp)
  9760. (setq level (funcall outline-level))
  9761. (org-end-of-subtree t t))
  9762. (org-back-over-empty-lines)
  9763. (insert "\n" (make-string
  9764. (if pos (org-get-valid-level level 1) 1) ?*)
  9765. " " child "\n")
  9766. (beginning-of-line 0)
  9767. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9768. (defun org-olpath-completing-read (prompt collection &rest args)
  9769. "Read an outline path like a file name."
  9770. (let ((thetable collection)
  9771. (org-completion-use-ido nil) ; does not work with ido.
  9772. (org-completion-use-iswitchb nil)) ; or iswitchb
  9773. (apply
  9774. 'org-icompleting-read prompt
  9775. (lambda (string predicate &optional flag)
  9776. (let (rtn r f (l (length string)))
  9777. (cond
  9778. ((eq flag nil)
  9779. ;; try completion
  9780. (try-completion string thetable))
  9781. ((eq flag t)
  9782. ;; all-completions
  9783. (setq rtn (all-completions string thetable predicate))
  9784. (mapcar
  9785. (lambda (x)
  9786. (setq r (substring x l))
  9787. (if (string-match " ([^)]*)$" x)
  9788. (setq f (match-string 0 x))
  9789. (setq f ""))
  9790. (if (string-match "/" r)
  9791. (concat string (substring r 0 (match-end 0)) f)
  9792. x))
  9793. rtn))
  9794. ((eq flag 'lambda)
  9795. ;; exact match?
  9796. (assoc string thetable)))
  9797. ))
  9798. args)))
  9799. ;;;; Dynamic blocks
  9800. (defun org-find-dblock (name)
  9801. "Find the first dynamic block with name NAME in the buffer.
  9802. If not found, stay at current position and return nil."
  9803. (let (pos)
  9804. (save-excursion
  9805. (goto-char (point-min))
  9806. (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
  9807. nil t)
  9808. (match-beginning 0))))
  9809. (if pos (goto-char pos))
  9810. pos))
  9811. (defconst org-dblock-start-re
  9812. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9813. "Matches the start line of a dynamic block, with parameters.")
  9814. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9815. "Matches the end of a dynamic block.")
  9816. (defun org-create-dblock (plist)
  9817. "Create a dynamic block section, with parameters taken from PLIST.
  9818. PLIST must contain a :name entry which is used as name of the block."
  9819. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9820. (end-of-line 1)
  9821. (newline))
  9822. (let ((col (current-column))
  9823. (name (plist-get plist :name)))
  9824. (insert "#+BEGIN: " name)
  9825. (while plist
  9826. (if (eq (car plist) :name)
  9827. (setq plist (cddr plist))
  9828. (insert " " (prin1-to-string (pop plist)))))
  9829. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9830. (beginning-of-line -2)))
  9831. (defun org-prepare-dblock ()
  9832. "Prepare dynamic block for refresh.
  9833. This empties the block, puts the cursor at the insert position and returns
  9834. the property list including an extra property :name with the block name."
  9835. (unless (looking-at org-dblock-start-re)
  9836. (error "Not at a dynamic block"))
  9837. (let* ((begdel (1+ (match-end 0)))
  9838. (name (org-no-properties (match-string 1)))
  9839. (params (append (list :name name)
  9840. (read (concat "(" (match-string 3) ")")))))
  9841. (save-excursion
  9842. (beginning-of-line 1)
  9843. (skip-chars-forward " \t")
  9844. (setq params (plist-put params :indentation-column (current-column))))
  9845. (unless (re-search-forward org-dblock-end-re nil t)
  9846. (error "Dynamic block not terminated"))
  9847. (setq params
  9848. (append params
  9849. (list :content (buffer-substring
  9850. begdel (match-beginning 0)))))
  9851. (delete-region begdel (match-beginning 0))
  9852. (goto-char begdel)
  9853. (open-line 1)
  9854. params))
  9855. (defun org-map-dblocks (&optional command)
  9856. "Apply COMMAND to all dynamic blocks in the current buffer.
  9857. If COMMAND is not given, use `org-update-dblock'."
  9858. (let ((cmd (or command 'org-update-dblock)))
  9859. (save-excursion
  9860. (goto-char (point-min))
  9861. (while (re-search-forward org-dblock-start-re nil t)
  9862. (goto-char (match-beginning 0))
  9863. (save-excursion
  9864. (condition-case nil
  9865. (funcall cmd)
  9866. (error (message "Error during update of dynamic block"))))
  9867. (unless (re-search-forward org-dblock-end-re nil t)
  9868. (error "Dynamic block not terminated"))))))
  9869. (defun org-dblock-update (&optional arg)
  9870. "User command for updating dynamic blocks.
  9871. Update the dynamic block at point. With prefix ARG, update all dynamic
  9872. blocks in the buffer."
  9873. (interactive "P")
  9874. (if arg
  9875. (org-update-all-dblocks)
  9876. (or (looking-at org-dblock-start-re)
  9877. (org-beginning-of-dblock))
  9878. (org-update-dblock)))
  9879. (defun org-update-dblock ()
  9880. "Update the dynamic block at point.
  9881. This means to empty the block, parse for parameters and then call
  9882. the correct writing function."
  9883. (interactive)
  9884. (save-window-excursion
  9885. (let* ((pos (point))
  9886. (line (org-current-line))
  9887. (params (org-prepare-dblock))
  9888. (name (plist-get params :name))
  9889. (indent (plist-get params :indentation-column))
  9890. (cmd (intern (concat "org-dblock-write:" name))))
  9891. (message "Updating dynamic block `%s' at line %d..." name line)
  9892. (funcall cmd params)
  9893. (message "Updating dynamic block `%s' at line %d...done" name line)
  9894. (goto-char pos)
  9895. (when (and indent (> indent 0))
  9896. (setq indent (make-string indent ?\ ))
  9897. (save-excursion
  9898. (org-beginning-of-dblock)
  9899. (forward-line 1)
  9900. (while (not (looking-at org-dblock-end-re))
  9901. (insert indent)
  9902. (beginning-of-line 2))
  9903. (when (looking-at org-dblock-end-re)
  9904. (and (looking-at "[ \t]+")
  9905. (replace-match ""))
  9906. (insert indent)))))))
  9907. (defun org-beginning-of-dblock ()
  9908. "Find the beginning of the dynamic block at point.
  9909. Error if there is no such block at point."
  9910. (let ((pos (point))
  9911. beg)
  9912. (end-of-line 1)
  9913. (if (and (re-search-backward org-dblock-start-re nil t)
  9914. (setq beg (match-beginning 0))
  9915. (re-search-forward org-dblock-end-re nil t)
  9916. (> (match-end 0) pos))
  9917. (goto-char beg)
  9918. (goto-char pos)
  9919. (error "Not in a dynamic block"))))
  9920. (defun org-update-all-dblocks ()
  9921. "Update all dynamic blocks in the buffer.
  9922. This function can be used in a hook."
  9923. (interactive)
  9924. (when (eq major-mode 'org-mode)
  9925. (org-map-dblocks 'org-update-dblock)))
  9926. ;;;; Completion
  9927. (defconst org-additional-option-like-keywords
  9928. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  9929. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  9930. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9931. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  9932. "BEGIN:" "END:"
  9933. "ORGTBL" "TBLFM:" "TBLNAME:"
  9934. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9935. "BEGIN_QUOTE" "END_QUOTE"
  9936. "BEGIN_VERSE" "END_VERSE"
  9937. "BEGIN_CENTER" "END_CENTER"
  9938. "BEGIN_SRC" "END_SRC"
  9939. "BEGIN_RESULT" "END_RESULT"
  9940. "NAME:" "RESULTS:"
  9941. "HEADER:" "HEADERS:"
  9942. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  9943. "CAPTION:" "LABEL:"
  9944. "SETUPFILE:"
  9945. "INCLUDE:"
  9946. "BIND:"
  9947. "MACRO:"))
  9948. (defcustom org-structure-template-alist
  9949. '(
  9950. ("s" "#+begin_src ?\n\n#+end_src"
  9951. "<src lang=\"?\">\n\n</src>")
  9952. ("e" "#+begin_example\n?\n#+end_example"
  9953. "<example>\n?\n</example>")
  9954. ("q" "#+begin_quote\n?\n#+end_quote"
  9955. "<quote>\n?\n</quote>")
  9956. ("v" "#+begin_verse\n?\n#+end_verse"
  9957. "<verse>\n?\n/verse>")
  9958. ("c" "#+begin_center\n?\n#+end_center"
  9959. "<center>\n?\n/center>")
  9960. ("l" "#+begin_latex\n?\n#+end_latex"
  9961. "<literal style=\"latex\">\n?\n</literal>")
  9962. ("L" "#+latex: "
  9963. "<literal style=\"latex\">?</literal>")
  9964. ("h" "#+begin_html\n?\n#+end_html"
  9965. "<literal style=\"html\">\n?\n</literal>")
  9966. ("H" "#+html: "
  9967. "<literal style=\"html\">?</literal>")
  9968. ("a" "#+begin_ascii\n?\n#+end_ascii")
  9969. ("A" "#+ascii: ")
  9970. ("i" "#+index: ?"
  9971. "#+index: ?")
  9972. ("I" "#+include %file ?"
  9973. "<include file=%file markup=\"?\">")
  9974. )
  9975. "Structure completion elements.
  9976. This is a list of abbreviation keys and values. The value gets inserted
  9977. if you type `<' followed by the key and then press the completion key,
  9978. usually `M-TAB'. %file will be replaced by a file name after prompting
  9979. for the file using completion. The cursor will be placed at the position
  9980. of the `?` in the template.
  9981. There are two templates for each key, the first uses the original Org syntax,
  9982. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  9983. the default when the /org-mtags.el/ module has been loaded. See also the
  9984. variable `org-mtags-prefer-muse-templates'.
  9985. This is an experimental feature, it is undecided if it is going to stay in."
  9986. :group 'org-completion
  9987. :type '(repeat
  9988. (string :tag "Key")
  9989. (string :tag "Template")
  9990. (string :tag "Muse Template")))
  9991. (defun org-try-structure-completion ()
  9992. "Try to complete a structure template before point.
  9993. This looks for strings like \"<e\" on an otherwise empty line and
  9994. expands them."
  9995. (let ((l (buffer-substring (point-at-bol) (point)))
  9996. a)
  9997. (when (and (looking-at "[ \t]*$")
  9998. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  9999. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10000. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10001. (match-beginning 1)) a)
  10002. t)))
  10003. (defun org-complete-expand-structure-template (start cell)
  10004. "Expand a structure template."
  10005. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10006. (rpl (nth (if musep 2 1) cell))
  10007. (ind ""))
  10008. (delete-region start (point))
  10009. (when (string-match "\\`#\\+" rpl)
  10010. (cond
  10011. ((bolp))
  10012. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10013. (setq ind (buffer-substring (point-at-bol) (point))))
  10014. (t (newline))))
  10015. (setq start (point))
  10016. (if (string-match "%file" rpl)
  10017. (setq rpl (replace-match
  10018. (concat
  10019. "\""
  10020. (save-match-data
  10021. (abbreviate-file-name (read-file-name "Include file: ")))
  10022. "\"")
  10023. t t rpl)))
  10024. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10025. (concat "\n" ind)))
  10026. (insert rpl)
  10027. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10028. ;;;; TODO, DEADLINE, Comments
  10029. (defun org-toggle-comment ()
  10030. "Change the COMMENT state of an entry."
  10031. (interactive)
  10032. (save-excursion
  10033. (org-back-to-heading)
  10034. (let (case-fold-search)
  10035. (cond
  10036. ((looking-at (format org-heading-keyword-regexp-format
  10037. org-comment-string))
  10038. (goto-char (match-end 1))
  10039. (looking-at (concat " +" org-comment-string))
  10040. (replace-match "" t t)
  10041. (when (eolp) (insert " ")))
  10042. ((looking-at org-outline-regexp)
  10043. (goto-char (match-end 0))
  10044. (insert org-comment-string " "))))))
  10045. (defvar org-last-todo-state-is-todo nil
  10046. "This is non-nil when the last TODO state change led to a TODO state.
  10047. If the last change removed the TODO tag or switched to DONE, then
  10048. this is nil.")
  10049. (defvar org-setting-tags nil) ; dynamically skipped
  10050. (defvar org-todo-setup-filter-hook nil
  10051. "Hook for functions that pre-filter todo specs.
  10052. Each function takes a todo spec and returns either nil or the spec
  10053. transformed into canonical form." )
  10054. (defvar org-todo-get-default-hook nil
  10055. "Hook for functions that get a default item for todo.
  10056. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10057. nil or a string to be used for the todo mark." )
  10058. (defvar org-agenda-headline-snapshot-before-repeat)
  10059. (defun org-current-effective-time ()
  10060. "Return current time adjusted for `org-extend-today-until' variable"
  10061. (let* ((ct (org-current-time))
  10062. (dct (decode-time ct))
  10063. (ct1
  10064. (if (and org-use-effective-time
  10065. (< (nth 2 dct) org-extend-today-until))
  10066. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  10067. ct)))
  10068. ct1))
  10069. (defun org-todo-yesterday (&optional arg)
  10070. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10071. (interactive "P")
  10072. (if (eq major-mode 'org-agenda-mode)
  10073. (apply 'org-agenda-todo-yesterday arg)
  10074. (let* ((hour (third (decode-time
  10075. (org-current-time))))
  10076. (org-extend-today-until (1+ hour)))
  10077. (org-todo arg))))
  10078. (defun org-todo (&optional arg)
  10079. "Change the TODO state of an item.
  10080. The state of an item is given by a keyword at the start of the heading,
  10081. like
  10082. *** TODO Write paper
  10083. *** DONE Call mom
  10084. The different keywords are specified in the variable `org-todo-keywords'.
  10085. By default the available states are \"TODO\" and \"DONE\".
  10086. So for this example: when the item starts with TODO, it is changed to DONE.
  10087. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10088. DONE are present, add TODO at the beginning of the heading.
  10089. With \\[universal-argument] prefix arg, use completion to determine the new \
  10090. state.
  10091. With numeric prefix arg, switch to that state.
  10092. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10093. keywords (nextset).
  10094. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10095. With a numeric prefix arg of 0, inhibit note taking for the change.
  10096. For calling through lisp, arg is also interpreted in the following way:
  10097. 'none -> empty state
  10098. \"\"(empty string) -> switch to empty state
  10099. 'done -> switch to DONE
  10100. 'nextset -> switch to the next set of keywords
  10101. 'previousset -> switch to the previous set of keywords
  10102. \"WAITING\" -> switch to the specified keyword, but only if it
  10103. really is a member of `org-todo-keywords'."
  10104. (interactive "P")
  10105. (if (equal arg '(16)) (setq arg 'nextset))
  10106. (let ((org-blocker-hook org-blocker-hook)
  10107. (case-fold-search nil))
  10108. (when (equal arg '(64))
  10109. (setq arg nil org-blocker-hook nil))
  10110. (when (and org-blocker-hook
  10111. (or org-inhibit-blocking
  10112. (org-entry-get nil "NOBLOCKING")))
  10113. (setq org-blocker-hook nil))
  10114. (save-excursion
  10115. (catch 'exit
  10116. (org-back-to-heading t)
  10117. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10118. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10119. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10120. (let* ((match-data (match-data))
  10121. (startpos (point-at-bol))
  10122. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10123. (org-log-done org-log-done)
  10124. (org-log-repeat org-log-repeat)
  10125. (org-todo-log-states org-todo-log-states)
  10126. (org-inhibit-logging
  10127. (if (equal arg 0)
  10128. (progn (setq arg nil) 'note) org-inhibit-logging))
  10129. (this (match-string 1))
  10130. (hl-pos (match-beginning 0))
  10131. (head (org-get-todo-sequence-head this))
  10132. (ass (assoc head org-todo-kwd-alist))
  10133. (interpret (nth 1 ass))
  10134. (done-word (nth 3 ass))
  10135. (final-done-word (nth 4 ass))
  10136. (last-state (or this ""))
  10137. (completion-ignore-case t)
  10138. (member (member this org-todo-keywords-1))
  10139. (tail (cdr member))
  10140. (state (cond
  10141. ((and org-todo-key-trigger
  10142. (or (and (equal arg '(4))
  10143. (eq org-use-fast-todo-selection 'prefix))
  10144. (and (not arg) org-use-fast-todo-selection
  10145. (not (eq org-use-fast-todo-selection
  10146. 'prefix)))))
  10147. ;; Use fast selection
  10148. (org-fast-todo-selection))
  10149. ((and (equal arg '(4))
  10150. (or (not org-use-fast-todo-selection)
  10151. (not org-todo-key-trigger)))
  10152. ;; Read a state with completion
  10153. (org-icompleting-read
  10154. "State: " (mapcar (lambda(x) (list x))
  10155. org-todo-keywords-1)
  10156. nil t))
  10157. ((eq arg 'right)
  10158. (if this
  10159. (if tail (car tail) nil)
  10160. (car org-todo-keywords-1)))
  10161. ((eq arg 'left)
  10162. (if (equal member org-todo-keywords-1)
  10163. nil
  10164. (if this
  10165. (nth (- (length org-todo-keywords-1)
  10166. (length tail) 2)
  10167. org-todo-keywords-1)
  10168. (org-last org-todo-keywords-1))))
  10169. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10170. (setq arg nil))) ; hack to fall back to cycling
  10171. (arg
  10172. ;; user or caller requests a specific state
  10173. (cond
  10174. ((equal arg "") nil)
  10175. ((eq arg 'none) nil)
  10176. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10177. ((eq arg 'nextset)
  10178. (or (car (cdr (member head org-todo-heads)))
  10179. (car org-todo-heads)))
  10180. ((eq arg 'previousset)
  10181. (let ((org-todo-heads (reverse org-todo-heads)))
  10182. (or (car (cdr (member head org-todo-heads)))
  10183. (car org-todo-heads))))
  10184. ((car (member arg org-todo-keywords-1)))
  10185. ((stringp arg)
  10186. (error "State `%s' not valid in this file" arg))
  10187. ((nth (1- (prefix-numeric-value arg))
  10188. org-todo-keywords-1))))
  10189. ((null member) (or head (car org-todo-keywords-1)))
  10190. ((equal this final-done-word) nil) ;; -> make empty
  10191. ((null tail) nil) ;; -> first entry
  10192. ((memq interpret '(type priority))
  10193. (if (eq this-command last-command)
  10194. (car tail)
  10195. (if (> (length tail) 0)
  10196. (or done-word (car org-done-keywords))
  10197. nil)))
  10198. (t
  10199. (car tail))))
  10200. (state (or
  10201. (run-hook-with-args-until-success
  10202. 'org-todo-get-default-hook state last-state)
  10203. state))
  10204. (next (if state (concat " " state " ") " "))
  10205. (change-plist (list :type 'todo-state-change :from this :to state
  10206. :position startpos))
  10207. dolog now-done-p)
  10208. (when org-blocker-hook
  10209. (setq org-last-todo-state-is-todo
  10210. (not (member this org-done-keywords)))
  10211. (unless (save-excursion
  10212. (save-match-data
  10213. (org-with-wide-buffer
  10214. (run-hook-with-args-until-failure
  10215. 'org-blocker-hook change-plist))))
  10216. (if (org-called-interactively-p 'interactive)
  10217. (error "TODO state change from %s to %s blocked" this state)
  10218. ;; fail silently
  10219. (message "TODO state change from %s to %s blocked" this state)
  10220. (throw 'exit nil))))
  10221. (store-match-data match-data)
  10222. (replace-match next t t)
  10223. (unless (pos-visible-in-window-p hl-pos)
  10224. (message "TODO state changed to %s" (org-trim next)))
  10225. (unless head
  10226. (setq head (org-get-todo-sequence-head state)
  10227. ass (assoc head org-todo-kwd-alist)
  10228. interpret (nth 1 ass)
  10229. done-word (nth 3 ass)
  10230. final-done-word (nth 4 ass)))
  10231. (when (memq arg '(nextset previousset))
  10232. (message "Keyword-Set %d/%d: %s"
  10233. (- (length org-todo-sets) -1
  10234. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  10235. (length org-todo-sets)
  10236. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  10237. (setq org-last-todo-state-is-todo
  10238. (not (member state org-done-keywords)))
  10239. (setq now-done-p (and (member state org-done-keywords)
  10240. (not (member this org-done-keywords))))
  10241. (and logging (org-local-logging logging))
  10242. (when (and (or org-todo-log-states org-log-done)
  10243. (not (eq org-inhibit-logging t))
  10244. (not (memq arg '(nextset previousset))))
  10245. ;; we need to look at recording a time and note
  10246. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  10247. (nth 2 (assoc this org-todo-log-states))))
  10248. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10249. (setq dolog 'time))
  10250. (when (and state
  10251. (member state org-not-done-keywords)
  10252. (not (member this org-not-done-keywords)))
  10253. ;; This is now a todo state and was not one before
  10254. ;; If there was a CLOSED time stamp, get rid of it.
  10255. (org-add-planning-info nil nil 'closed))
  10256. (when (and now-done-p org-log-done)
  10257. ;; It is now done, and it was not done before
  10258. (org-add-planning-info 'closed (org-current-effective-time))
  10259. (if (and (not dolog) (eq 'note org-log-done))
  10260. (org-add-log-setup 'done state this 'findpos 'note)))
  10261. (when (and state dolog)
  10262. ;; This is a non-nil state, and we need to log it
  10263. (org-add-log-setup 'state state this 'findpos dolog)))
  10264. ;; Fixup tag positioning
  10265. (org-todo-trigger-tag-changes state)
  10266. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10267. (when org-provide-todo-statistics
  10268. (org-update-parent-todo-statistics))
  10269. (run-hooks 'org-after-todo-state-change-hook)
  10270. (if (and arg (not (member state org-done-keywords)))
  10271. (setq head (org-get-todo-sequence-head state)))
  10272. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10273. ;; Do we need to trigger a repeat?
  10274. (when now-done-p
  10275. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10276. ;; This is for the agenda, take a snapshot of the headline.
  10277. (save-match-data
  10278. (setq org-agenda-headline-snapshot-before-repeat
  10279. (org-get-heading))))
  10280. (org-auto-repeat-maybe state))
  10281. ;; Fixup cursor location if close to the keyword
  10282. (if (and (outline-on-heading-p)
  10283. (not (bolp))
  10284. (save-excursion (beginning-of-line 1)
  10285. (looking-at org-todo-line-regexp))
  10286. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10287. (progn
  10288. (goto-char (or (match-end 2) (match-end 1)))
  10289. (and (looking-at " ") (just-one-space))))
  10290. (when org-trigger-hook
  10291. (save-excursion
  10292. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  10293. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10294. "Block turning an entry into a TODO, using the hierarchy.
  10295. This checks whether the current task should be blocked from state
  10296. changes. Such blocking occurs when:
  10297. 1. The task has children which are not all in a completed state.
  10298. 2. A task has a parent with the property :ORDERED:, and there
  10299. are siblings prior to the current task with incomplete
  10300. status.
  10301. 3. The parent of the task is blocked because it has siblings that should
  10302. be done first, or is child of a block grandparent TODO entry."
  10303. (if (not org-enforce-todo-dependencies)
  10304. t ; if locally turned off don't block
  10305. (catch 'dont-block
  10306. ;; If this is not a todo state change, or if this entry is already DONE,
  10307. ;; do not block
  10308. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10309. (member (plist-get change-plist :from)
  10310. (cons 'done org-done-keywords))
  10311. (member (plist-get change-plist :to)
  10312. (cons 'todo org-not-done-keywords))
  10313. (not (plist-get change-plist :to)))
  10314. (throw 'dont-block t))
  10315. ;; If this task has children, and any are undone, it's blocked
  10316. (save-excursion
  10317. (org-back-to-heading t)
  10318. (let ((this-level (funcall outline-level)))
  10319. (outline-next-heading)
  10320. (let ((child-level (funcall outline-level)))
  10321. (while (and (not (eobp))
  10322. (> child-level this-level))
  10323. ;; this todo has children, check whether they are all
  10324. ;; completed
  10325. (if (and (not (org-entry-is-done-p))
  10326. (org-entry-is-todo-p))
  10327. (throw 'dont-block nil))
  10328. (outline-next-heading)
  10329. (setq child-level (funcall outline-level))))))
  10330. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10331. ;; any previous siblings are undone, it's blocked
  10332. (save-excursion
  10333. (org-back-to-heading t)
  10334. (let* ((pos (point))
  10335. (parent-pos (and (org-up-heading-safe) (point))))
  10336. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10337. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10338. (forward-line 1)
  10339. (re-search-forward org-not-done-heading-regexp pos t))
  10340. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10341. ;; Search further up the hierarchy, to see if an anchestor is blocked
  10342. (while t
  10343. (goto-char parent-pos)
  10344. (if (not (looking-at org-not-done-heading-regexp))
  10345. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10346. (setq pos (point))
  10347. (setq parent-pos (and (org-up-heading-safe) (point)))
  10348. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10349. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10350. (forward-line 1)
  10351. (re-search-forward org-not-done-heading-regexp pos t))
  10352. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10353. (defcustom org-track-ordered-property-with-tag nil
  10354. "Should the ORDERED property also be shown as a tag?
  10355. The ORDERED property decides if an entry should require subtasks to be
  10356. completed in sequence. Since a property is not very visible, setting
  10357. this option means that toggling the ORDERED property with the command
  10358. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10359. not relevant for the behavior, but it makes things more visible.
  10360. Note that toggling the tag with tags commands will not change the property
  10361. and therefore not influence behavior!
  10362. This can be t, meaning the tag ORDERED should be used, It can also be a
  10363. string to select a different tag for this task."
  10364. :group 'org-todo
  10365. :type '(choice
  10366. (const :tag "No tracking" nil)
  10367. (const :tag "Track with ORDERED tag" t)
  10368. (string :tag "Use other tag")))
  10369. (defun org-toggle-ordered-property ()
  10370. "Toggle the ORDERED property of the current entry.
  10371. For better visibility, you can track the value of this property with a tag.
  10372. See variable `org-track-ordered-property-with-tag'."
  10373. (interactive)
  10374. (let* ((t1 org-track-ordered-property-with-tag)
  10375. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10376. (save-excursion
  10377. (org-back-to-heading)
  10378. (if (org-entry-get nil "ORDERED")
  10379. (progn
  10380. (org-delete-property "ORDERED")
  10381. (and tag (org-toggle-tag tag 'off))
  10382. (message "Subtasks can be completed in arbitrary order"))
  10383. (org-entry-put nil "ORDERED" "t")
  10384. (and tag (org-toggle-tag tag 'on))
  10385. (message "Subtasks must be completed in sequence")))))
  10386. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10387. (defun org-block-todo-from-checkboxes (change-plist)
  10388. "Block turning an entry into a TODO, using checkboxes.
  10389. This checks whether the current task should be blocked from state
  10390. changes because there are unchecked boxes in this entry."
  10391. (if (not org-enforce-todo-checkbox-dependencies)
  10392. t ; if locally turned off don't block
  10393. (catch 'dont-block
  10394. ;; If this is not a todo state change, or if this entry is already DONE,
  10395. ;; do not block
  10396. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10397. (member (plist-get change-plist :from)
  10398. (cons 'done org-done-keywords))
  10399. (member (plist-get change-plist :to)
  10400. (cons 'todo org-not-done-keywords))
  10401. (not (plist-get change-plist :to)))
  10402. (throw 'dont-block t))
  10403. ;; If this task has checkboxes that are not checked, it's blocked
  10404. (save-excursion
  10405. (org-back-to-heading t)
  10406. (let ((beg (point)) end)
  10407. (outline-next-heading)
  10408. (setq end (point))
  10409. (goto-char beg)
  10410. (if (org-list-search-forward
  10411. (concat (org-item-beginning-re)
  10412. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10413. "\\[[- ]\\]")
  10414. end t)
  10415. (progn
  10416. (if (boundp 'org-blocked-by-checkboxes)
  10417. (setq org-blocked-by-checkboxes t))
  10418. (throw 'dont-block nil)))))
  10419. t))) ; do not block
  10420. (defun org-entry-blocked-p ()
  10421. "Is the current entry blocked?"
  10422. (if (org-entry-get nil "NOBLOCKING")
  10423. nil ;; Never block this entry
  10424. (not
  10425. (run-hook-with-args-until-failure
  10426. 'org-blocker-hook
  10427. (list :type 'todo-state-change
  10428. :position (point)
  10429. :from 'todo
  10430. :to 'done)))))
  10431. (defun org-update-statistics-cookies (all)
  10432. "Update the statistics cookie, either from TODO or from checkboxes.
  10433. This should be called with the cursor in a line with a statistics cookie."
  10434. (interactive "P")
  10435. (if all
  10436. (progn
  10437. (org-update-checkbox-count 'all)
  10438. (org-map-entries 'org-update-parent-todo-statistics))
  10439. (if (not (org-on-heading-p))
  10440. (org-update-checkbox-count)
  10441. (let ((pos (move-marker (make-marker) (point)))
  10442. end l1 l2)
  10443. (ignore-errors (org-back-to-heading t))
  10444. (if (not (org-on-heading-p))
  10445. (org-update-checkbox-count)
  10446. (setq l1 (org-outline-level))
  10447. (setq end (save-excursion
  10448. (outline-next-heading)
  10449. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  10450. (point)))
  10451. (if (and (save-excursion
  10452. (re-search-forward
  10453. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10454. (not (save-excursion (re-search-forward
  10455. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10456. (org-update-checkbox-count)
  10457. (if (and l2 (> l2 l1))
  10458. (progn
  10459. (goto-char end)
  10460. (org-update-parent-todo-statistics))
  10461. (goto-char pos)
  10462. (beginning-of-line 1)
  10463. (while (re-search-forward
  10464. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10465. (point-at-eol) t)
  10466. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10467. (goto-char pos)
  10468. (move-marker pos nil)))))
  10469. (defvar org-entry-property-inherited-from) ;; defined below
  10470. (defun org-update-parent-todo-statistics ()
  10471. "Update any statistics cookie in the parent of the current headline.
  10472. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10473. the entire subtree and this will travel up the hierarchy and update
  10474. statistics everywhere."
  10475. (let* ((prop (save-excursion (org-up-heading-safe)
  10476. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10477. (recursive (or (not org-hierarchical-todo-statistics)
  10478. (and prop (string-match "\\<recursive\\>" prop))))
  10479. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10480. 0))
  10481. (first t)
  10482. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10483. level ltoggle l1 new ndel
  10484. (cnt-all 0) (cnt-done 0) is-percent kwd
  10485. checkbox-beg ov ovs ove cookie-present)
  10486. (catch 'exit
  10487. (save-excursion
  10488. (beginning-of-line 1)
  10489. (setq ltoggle (funcall outline-level))
  10490. ;; Three situations are to consider:
  10491. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10492. ;; to the top-level ancestor on the headline;
  10493. ;; 2. If parent has "recursive" property, repeat up to the
  10494. ;; headline setting that property, taking inheritance into
  10495. ;; account;
  10496. ;; 3. Else, move up to direct parent and proceed only once.
  10497. (while (and (setq level (org-up-heading-safe))
  10498. (or recursive first)
  10499. (>= (point) lim))
  10500. (setq first nil cookie-present nil)
  10501. (unless (and level
  10502. (not (string-match
  10503. "\\<checkbox\\>"
  10504. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10505. "")))))
  10506. (throw 'exit nil))
  10507. (while (re-search-forward box-re (point-at-eol) t)
  10508. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10509. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10510. (save-match-data
  10511. (unless (outline-next-heading) (throw 'exit nil))
  10512. (while (and (looking-at org-complex-heading-regexp)
  10513. (> (setq l1 (length (match-string 1))) level))
  10514. (setq kwd (and (or recursive (= l1 ltoggle))
  10515. (match-string 2)))
  10516. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10517. (and (listp org-provide-todo-statistics)
  10518. (or (member kwd org-provide-todo-statistics)
  10519. (member kwd org-done-keywords))))
  10520. (setq cnt-all (1+ cnt-all))
  10521. (if (eq org-provide-todo-statistics t)
  10522. (and kwd (setq cnt-all (1+ cnt-all)))))
  10523. (and (member kwd org-done-keywords)
  10524. (setq cnt-done (1+ cnt-done)))
  10525. (outline-next-heading)))
  10526. (setq new
  10527. (if is-percent
  10528. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10529. (format "[%d/%d]" cnt-done cnt-all))
  10530. ndel (- (match-end 0) checkbox-beg))
  10531. ;; handle overlays when updating cookie from column view
  10532. (when (setq ov (car (overlays-at checkbox-beg)))
  10533. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10534. (delete-overlay ov))
  10535. (goto-char checkbox-beg)
  10536. (insert new)
  10537. (delete-region (point) (+ (point) ndel))
  10538. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10539. (when ov (move-overlay ov ovs ove)))
  10540. (when cookie-present
  10541. (run-hook-with-args 'org-after-todo-statistics-hook
  10542. cnt-done (- cnt-all cnt-done))))))
  10543. (run-hooks 'org-todo-statistics-hook)))
  10544. (defvar org-after-todo-statistics-hook nil
  10545. "Hook that is called after a TODO statistics cookie has been updated.
  10546. Each function is called with two arguments: the number of not-done entries
  10547. and the number of done entries.
  10548. For example, the following function, when added to this hook, will switch
  10549. an entry to DONE when all children are done, and back to TODO when new
  10550. entries are set to a TODO status. Note that this hook is only called
  10551. when there is a statistics cookie in the headline!
  10552. (defun org-summary-todo (n-done n-not-done)
  10553. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10554. (let (org-log-done org-log-states) ; turn off logging
  10555. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10556. ")
  10557. (defvar org-todo-statistics-hook nil
  10558. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10559. This hook runs even if there is no statistics cookie present, in which case
  10560. `org-after-todo-statistics-hook' would not run.")
  10561. (defun org-todo-trigger-tag-changes (state)
  10562. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10563. (let ((l org-todo-state-tags-triggers)
  10564. changes)
  10565. (when (or (not state) (equal state ""))
  10566. (setq changes (append changes (cdr (assoc "" l)))))
  10567. (when (and (stringp state) (> (length state) 0))
  10568. (setq changes (append changes (cdr (assoc state l)))))
  10569. (when (member state org-not-done-keywords)
  10570. (setq changes (append changes (cdr (assoc 'todo l)))))
  10571. (when (member state org-done-keywords)
  10572. (setq changes (append changes (cdr (assoc 'done l)))))
  10573. (dolist (c changes)
  10574. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10575. (defun org-local-logging (value)
  10576. "Get logging settings from a property VALUE."
  10577. (let* (words w a)
  10578. ;; directly set the variables, they are already local.
  10579. (setq org-log-done nil
  10580. org-log-repeat nil
  10581. org-todo-log-states nil)
  10582. (setq words (org-split-string value))
  10583. (while (setq w (pop words))
  10584. (cond
  10585. ((setq a (assoc w org-startup-options))
  10586. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10587. (set (nth 1 a) (nth 2 a))))
  10588. ((setq a (org-extract-log-state-settings w))
  10589. (and (member (car a) org-todo-keywords-1)
  10590. (push a org-todo-log-states)))))))
  10591. (defun org-get-todo-sequence-head (kwd)
  10592. "Return the head of the TODO sequence to which KWD belongs.
  10593. If KWD is not set, check if there is a text property remembering the
  10594. right sequence."
  10595. (let (p)
  10596. (cond
  10597. ((not kwd)
  10598. (or (get-text-property (point-at-bol) 'org-todo-head)
  10599. (progn
  10600. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10601. nil (point-at-eol)))
  10602. (get-text-property p 'org-todo-head))))
  10603. ((not (member kwd org-todo-keywords-1))
  10604. (car org-todo-keywords-1))
  10605. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10606. (defun org-fast-todo-selection ()
  10607. "Fast TODO keyword selection with single keys.
  10608. Returns the new TODO keyword, or nil if no state change should occur."
  10609. (let* ((fulltable org-todo-key-alist)
  10610. (done-keywords org-done-keywords) ;; needed for the faces.
  10611. (maxlen (apply 'max (mapcar
  10612. (lambda (x)
  10613. (if (stringp (car x)) (string-width (car x)) 0))
  10614. fulltable)))
  10615. (expert nil)
  10616. (fwidth (+ maxlen 3 1 3))
  10617. (ncol (/ (- (window-width) 4) fwidth))
  10618. tg cnt e c tbl
  10619. groups ingroup)
  10620. (save-excursion
  10621. (save-window-excursion
  10622. (if expert
  10623. (set-buffer (get-buffer-create " *Org todo*"))
  10624. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10625. (erase-buffer)
  10626. (org-set-local 'org-done-keywords done-keywords)
  10627. (setq tbl fulltable cnt 0)
  10628. (while (setq e (pop tbl))
  10629. (cond
  10630. ((equal e '(:startgroup))
  10631. (push '() groups) (setq ingroup t)
  10632. (when (not (= cnt 0))
  10633. (setq cnt 0)
  10634. (insert "\n"))
  10635. (insert "{ "))
  10636. ((equal e '(:endgroup))
  10637. (setq ingroup nil cnt 0)
  10638. (insert "}\n"))
  10639. ((equal e '(:newline))
  10640. (when (not (= cnt 0))
  10641. (setq cnt 0)
  10642. (insert "\n")
  10643. (setq e (car tbl))
  10644. (while (equal (car tbl) '(:newline))
  10645. (insert "\n")
  10646. (setq tbl (cdr tbl)))))
  10647. (t
  10648. (setq tg (car e) c (cdr e))
  10649. (if ingroup (push tg (car groups)))
  10650. (setq tg (org-add-props tg nil 'face
  10651. (org-get-todo-face tg)))
  10652. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10653. (insert "[" c "] " tg (make-string
  10654. (- fwidth 4 (length tg)) ?\ ))
  10655. (when (= (setq cnt (1+ cnt)) ncol)
  10656. (insert "\n")
  10657. (if ingroup (insert " "))
  10658. (setq cnt 0)))))
  10659. (insert "\n")
  10660. (goto-char (point-min))
  10661. (if (not expert) (org-fit-window-to-buffer))
  10662. (message "[a-z..]:Set [SPC]:clear")
  10663. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10664. (cond
  10665. ((or (= c ?\C-g)
  10666. (and (= c ?q) (not (rassoc c fulltable))))
  10667. (setq quit-flag t))
  10668. ((= c ?\ ) nil)
  10669. ((setq e (rassoc c fulltable) tg (car e))
  10670. tg)
  10671. (t (setq quit-flag t)))))))
  10672. (defun org-entry-is-todo-p ()
  10673. (member (org-get-todo-state) org-not-done-keywords))
  10674. (defun org-entry-is-done-p ()
  10675. (member (org-get-todo-state) org-done-keywords))
  10676. (defun org-get-todo-state ()
  10677. (save-excursion
  10678. (org-back-to-heading t)
  10679. (and (looking-at org-todo-line-regexp)
  10680. (match-end 2)
  10681. (match-string 2))))
  10682. (defun org-at-date-range-p (&optional inactive-ok)
  10683. "Is the cursor inside a date range?"
  10684. (interactive)
  10685. (save-excursion
  10686. (catch 'exit
  10687. (let ((pos (point)))
  10688. (skip-chars-backward "^[<\r\n")
  10689. (skip-chars-backward "<[")
  10690. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10691. (>= (match-end 0) pos)
  10692. (throw 'exit t))
  10693. (skip-chars-backward "^<[\r\n")
  10694. (skip-chars-backward "<[")
  10695. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10696. (>= (match-end 0) pos)
  10697. (throw 'exit t)))
  10698. nil)))
  10699. (defun org-get-repeat (&optional tagline)
  10700. "Check if there is a deadline/schedule with repeater in this entry."
  10701. (save-match-data
  10702. (save-excursion
  10703. (org-back-to-heading t)
  10704. (and (re-search-forward (if tagline
  10705. (concat tagline "\\s-*" org-repeat-re)
  10706. org-repeat-re)
  10707. (org-entry-end-position) t)
  10708. (match-string-no-properties 1)))))
  10709. (defvar org-last-changed-timestamp)
  10710. (defvar org-last-inserted-timestamp)
  10711. (defvar org-log-post-message)
  10712. (defvar org-log-note-purpose)
  10713. (defvar org-log-note-how)
  10714. (defvar org-log-note-extra)
  10715. (defun org-auto-repeat-maybe (done-word)
  10716. "Check if the current headline contains a repeated deadline/schedule.
  10717. If yes, set TODO state back to what it was and change the base date
  10718. of repeating deadline/scheduled time stamps to new date.
  10719. This function is run automatically after each state change to a DONE state."
  10720. ;; last-state is dynamically scoped into this function
  10721. (let* ((repeat (org-get-repeat))
  10722. (aa (assoc last-state org-todo-kwd-alist))
  10723. (interpret (nth 1 aa))
  10724. (head (nth 2 aa))
  10725. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10726. (msg "Entry repeats: ")
  10727. (org-log-done nil)
  10728. (org-todo-log-states nil)
  10729. re type n what ts time to-state)
  10730. (when repeat
  10731. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10732. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10733. org-todo-repeat-to-state))
  10734. (unless (and to-state (member to-state org-todo-keywords-1))
  10735. (setq to-state (if (eq interpret 'type) last-state head)))
  10736. (org-todo to-state)
  10737. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10738. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10739. (org-time-stamp-format t t))))
  10740. (when org-log-repeat
  10741. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10742. (memq 'org-add-log-note post-command-hook))
  10743. ;; OK, we are already setup for some record
  10744. (if (eq org-log-repeat 'note)
  10745. ;; make sure we take a note, not only a time stamp
  10746. (setq org-log-note-how 'note))
  10747. ;; Set up for taking a record
  10748. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10749. last-state
  10750. 'findpos org-log-repeat)))
  10751. (org-back-to-heading t)
  10752. (org-add-planning-info nil nil 'closed)
  10753. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10754. org-deadline-time-regexp "\\)\\|\\("
  10755. org-ts-regexp "\\)"))
  10756. (while (re-search-forward
  10757. re (save-excursion (outline-next-heading) (point)) t)
  10758. (setq type (if (match-end 1) org-scheduled-string
  10759. (if (match-end 3) org-deadline-string "Plain:"))
  10760. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10761. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10762. (setq n (string-to-number (match-string 2 ts))
  10763. what (match-string 3 ts))
  10764. (if (equal what "w") (setq n (* n 7) what "d"))
  10765. ;; Preparation, see if we need to modify the start date for the change
  10766. (when (match-end 1)
  10767. (setq time (save-match-data (org-time-string-to-time ts)))
  10768. (cond
  10769. ((equal (match-string 1 ts) ".")
  10770. ;; Shift starting date to today
  10771. (org-timestamp-change
  10772. (- (org-today) (time-to-days time))
  10773. 'day))
  10774. ((equal (match-string 1 ts) "+")
  10775. (let ((nshiftmax 10) (nshift 0))
  10776. (while (or (= nshift 0)
  10777. (<= (time-to-days time)
  10778. (time-to-days (current-time))))
  10779. (when (= (incf nshift) nshiftmax)
  10780. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10781. (error "Abort")))
  10782. (org-timestamp-change n (cdr (assoc what whata)))
  10783. (org-at-timestamp-p t)
  10784. (setq ts (match-string 1))
  10785. (setq time (save-match-data (org-time-string-to-time ts)))))
  10786. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10787. ;; rematch, so that we have everything in place for the real shift
  10788. (org-at-timestamp-p t)
  10789. (setq ts (match-string 1))
  10790. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10791. (org-timestamp-change n (cdr (assoc what whata)))
  10792. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10793. (setq org-log-post-message msg)
  10794. (message "%s" msg))))
  10795. (defun org-show-todo-tree (arg)
  10796. "Make a compact tree which shows all headlines marked with TODO.
  10797. The tree will show the lines where the regexp matches, and all higher
  10798. headlines above the match.
  10799. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10800. With a numeric prefix N, construct a sparse tree for the Nth element
  10801. of `org-todo-keywords-1'."
  10802. (interactive "P")
  10803. (let ((case-fold-search nil)
  10804. (kwd-re
  10805. (cond ((null arg) org-not-done-regexp)
  10806. ((equal arg '(4))
  10807. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10808. (mapcar 'list org-todo-keywords-1))))
  10809. (concat "\\("
  10810. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10811. "\\)\\>")))
  10812. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10813. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10814. org-todo-keywords-1)))
  10815. (t (error "Invalid prefix argument: %s" arg)))))
  10816. (message "%d TODO entries found"
  10817. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  10818. (defun org-deadline (&optional remove time)
  10819. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10820. With argument REMOVE, remove any deadline from the item.
  10821. With argument TIME, set the deadline at the corresponding date. TIME
  10822. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10823. (interactive "P")
  10824. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10825. (let (org-loop-over-headlines-in-active-region)
  10826. (org-map-entries
  10827. `(org-deadline ',remove ,time) org-loop-over-headlines-in-active-region 'region (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10828. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10829. (repeater (and old-date
  10830. (string-match
  10831. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10832. old-date)
  10833. (match-string 1 old-date))))
  10834. (if remove
  10835. (progn
  10836. (when (and old-date org-log-redeadline)
  10837. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10838. org-log-redeadline))
  10839. (org-remove-timestamp-with-keyword org-deadline-string)
  10840. (message "Item no longer has a deadline."))
  10841. (org-add-planning-info 'deadline time 'closed)
  10842. (when (and old-date org-log-redeadline
  10843. (not (equal old-date
  10844. (substring org-last-inserted-timestamp 1 -1))))
  10845. (org-add-log-setup 'redeadline nil old-date 'findpos
  10846. org-log-redeadline))
  10847. (when repeater
  10848. (save-excursion
  10849. (org-back-to-heading t)
  10850. (when (re-search-forward (concat org-deadline-string " "
  10851. org-last-inserted-timestamp)
  10852. (save-excursion
  10853. (outline-next-heading) (point)) t)
  10854. (goto-char (1- (match-end 0)))
  10855. (insert " " repeater)
  10856. (setq org-last-inserted-timestamp
  10857. (concat (substring org-last-inserted-timestamp 0 -1)
  10858. " " repeater
  10859. (substring org-last-inserted-timestamp -1))))))
  10860. (message "Deadline on %s" org-last-inserted-timestamp)))))
  10861. (defun org-schedule (&optional remove time)
  10862. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10863. With argument REMOVE, remove any scheduling date from the item.
  10864. With argument TIME, scheduled at the corresponding date. TIME can
  10865. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10866. (interactive "P")
  10867. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10868. (let (org-loop-over-headlines-in-active-region)
  10869. (org-map-entries
  10870. `(org-schedule ',remove ,time) org-loop-over-headlines-in-active-region 'region (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10871. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10872. (repeater (and old-date
  10873. (string-match
  10874. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10875. old-date)
  10876. (match-string 1 old-date))))
  10877. (if remove
  10878. (progn
  10879. (when (and old-date org-log-reschedule)
  10880. (org-add-log-setup 'delschedule nil old-date 'findpos
  10881. org-log-reschedule))
  10882. (org-remove-timestamp-with-keyword org-scheduled-string)
  10883. (message "Item is no longer scheduled."))
  10884. (org-add-planning-info 'scheduled time 'closed)
  10885. (when (and old-date org-log-reschedule
  10886. (not (equal old-date
  10887. (substring org-last-inserted-timestamp 1 -1))))
  10888. (org-add-log-setup 'reschedule nil old-date 'findpos
  10889. org-log-reschedule))
  10890. (when repeater
  10891. (save-excursion
  10892. (org-back-to-heading t)
  10893. (when (re-search-forward (concat org-scheduled-string " "
  10894. org-last-inserted-timestamp)
  10895. (save-excursion
  10896. (outline-next-heading) (point)) t)
  10897. (goto-char (1- (match-end 0)))
  10898. (insert " " repeater)
  10899. (setq org-last-inserted-timestamp
  10900. (concat (substring org-last-inserted-timestamp 0 -1)
  10901. " " repeater
  10902. (substring org-last-inserted-timestamp -1))))))
  10903. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  10904. (defun org-get-scheduled-time (pom &optional inherit)
  10905. "Get the scheduled time as a time tuple, of a format suitable
  10906. for calling org-schedule with, or if there is no scheduling,
  10907. returns nil."
  10908. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10909. (when time
  10910. (apply 'encode-time (org-parse-time-string time)))))
  10911. (defun org-get-deadline-time (pom &optional inherit)
  10912. "Get the deadline as a time tuple, of a format suitable for
  10913. calling org-deadline with, or if there is no scheduling, returns
  10914. nil."
  10915. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10916. (when time
  10917. (apply 'encode-time (org-parse-time-string time)))))
  10918. (defun org-remove-timestamp-with-keyword (keyword)
  10919. "Remove all time stamps with KEYWORD in the current entry."
  10920. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10921. beg)
  10922. (save-excursion
  10923. (org-back-to-heading t)
  10924. (setq beg (point))
  10925. (outline-next-heading)
  10926. (while (re-search-backward re beg t)
  10927. (replace-match "")
  10928. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10929. (equal (char-before) ?\ ))
  10930. (backward-delete-char 1)
  10931. (if (string-match "^[ \t]*$" (buffer-substring
  10932. (point-at-bol) (point-at-eol)))
  10933. (delete-region (point-at-bol)
  10934. (min (point-max) (1+ (point-at-eol))))))))))
  10935. (defun org-add-planning-info (what &optional time &rest remove)
  10936. "Insert new timestamp with keyword in the line directly after the headline.
  10937. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  10938. If non is given, the user is prompted for a date.
  10939. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10940. be removed."
  10941. (interactive)
  10942. (let (org-time-was-given org-end-time-was-given ts
  10943. end default-time default-input)
  10944. (catch 'exit
  10945. (when (and (memq what '(scheduled deadline))
  10946. (or (not time)
  10947. (and (stringp time)
  10948. (string-match "^[-+]+[0-9]" time))))
  10949. ;; Try to get a default date/time from existing timestamp
  10950. (save-excursion
  10951. (org-back-to-heading t)
  10952. (setq end (save-excursion (outline-next-heading) (point)))
  10953. (when (re-search-forward (if (eq what 'scheduled)
  10954. org-scheduled-time-regexp
  10955. org-deadline-time-regexp)
  10956. end t)
  10957. (setq ts (match-string 1)
  10958. default-time
  10959. (apply 'encode-time (org-parse-time-string ts))
  10960. default-input (and ts (org-get-compact-tod ts))))))
  10961. (when what
  10962. (setq time
  10963. (if (stringp time)
  10964. ;; This is a string (relative or absolute), set proper date
  10965. (apply 'encode-time
  10966. (org-read-date-analyze
  10967. time default-time (decode-time default-time)))
  10968. ;; If necessary, get the time from the user
  10969. (or time (org-read-date nil 'to-time nil nil
  10970. default-time default-input)))))
  10971. (when (and org-insert-labeled-timestamps-at-point
  10972. (member what '(scheduled deadline)))
  10973. (insert
  10974. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10975. (org-insert-time-stamp time org-time-was-given
  10976. nil nil nil (list org-end-time-was-given))
  10977. (setq what nil))
  10978. (save-excursion
  10979. (save-restriction
  10980. (let (col list elt ts buffer-invisibility-spec)
  10981. (org-back-to-heading t)
  10982. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  10983. (goto-char (match-end 1))
  10984. (setq col (current-column))
  10985. (goto-char (match-end 0))
  10986. (if (eobp) (insert "\n") (forward-char 1))
  10987. (when (and (not what)
  10988. (not (looking-at
  10989. (concat "[ \t]*"
  10990. org-keyword-time-not-clock-regexp))))
  10991. ;; Nothing to add, nothing to remove...... :-)
  10992. (throw 'exit nil))
  10993. (if (and (not (looking-at org-outline-regexp))
  10994. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10995. "[^\r\n]*"))
  10996. (not (equal (match-string 1) org-clock-string)))
  10997. (narrow-to-region (match-beginning 0) (match-end 0))
  10998. (insert-before-markers "\n")
  10999. (backward-char 1)
  11000. (narrow-to-region (point) (point))
  11001. (and org-adapt-indentation (org-indent-to-column col)))
  11002. ;; Check if we have to remove something.
  11003. (setq list (cons what remove))
  11004. (while list
  11005. (setq elt (pop list))
  11006. (when (or (and (eq elt 'scheduled)
  11007. (re-search-forward org-scheduled-time-regexp nil t))
  11008. (and (eq elt 'deadline)
  11009. (re-search-forward org-deadline-time-regexp nil t))
  11010. (and (eq elt 'closed)
  11011. (re-search-forward org-closed-time-regexp nil t)))
  11012. (replace-match "")
  11013. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11014. (and (looking-at "[ \t]+") (replace-match ""))
  11015. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11016. (when what
  11017. (insert
  11018. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11019. (cond ((eq what 'scheduled) org-scheduled-string)
  11020. ((eq what 'deadline) org-deadline-string)
  11021. ((eq what 'closed) org-closed-string))
  11022. " ")
  11023. (setq ts (org-insert-time-stamp
  11024. time
  11025. (or org-time-was-given
  11026. (and (eq what 'closed) org-log-done-with-time))
  11027. (eq what 'closed)
  11028. nil nil (list org-end-time-was-given)))
  11029. (insert
  11030. (if (not (or (bolp) (eq (char-before) ?\ )
  11031. (memq (char-after) '(32 10))
  11032. (eobp))) " " ""))
  11033. (end-of-line 1))
  11034. (goto-char (point-min))
  11035. (widen)
  11036. (if (and (looking-at "[ \t]*\n")
  11037. (equal (char-before) ?\n))
  11038. (delete-region (1- (point)) (point-at-eol)))
  11039. ts))))))
  11040. (defvar org-log-note-marker (make-marker))
  11041. (defvar org-log-note-purpose nil)
  11042. (defvar org-log-note-state nil)
  11043. (defvar org-log-note-previous-state nil)
  11044. (defvar org-log-note-how nil)
  11045. (defvar org-log-note-extra nil)
  11046. (defvar org-log-note-window-configuration nil)
  11047. (defvar org-log-note-return-to (make-marker))
  11048. (defvar org-log-note-effective-time nil
  11049. "Remembered current time so that dynamically scoped
  11050. `org-extend-today-until' affects tha timestamps in state change
  11051. log")
  11052. (defvar org-log-post-message nil
  11053. "Message to be displayed after a log note has been stored.
  11054. The auto-repeater uses this.")
  11055. (defun org-add-note ()
  11056. "Add a note to the current entry.
  11057. This is done in the same way as adding a state change note."
  11058. (interactive)
  11059. (org-add-log-setup 'note nil nil 'findpos nil))
  11060. (defvar org-property-end-re)
  11061. (defun org-add-log-setup (&optional purpose state prev-state
  11062. findpos how extra)
  11063. "Set up the post command hook to take a note.
  11064. If this is about to TODO state change, the new state is expected in STATE.
  11065. When FINDPOS is non-nil, find the correct position for the note in
  11066. the current entry. If not, assume that it can be inserted at point.
  11067. HOW is an indicator what kind of note should be created.
  11068. EXTRA is additional text that will be inserted into the notes buffer."
  11069. (let* ((org-log-into-drawer (org-log-into-drawer))
  11070. (drawer (cond ((stringp org-log-into-drawer)
  11071. org-log-into-drawer)
  11072. (org-log-into-drawer "LOGBOOK")
  11073. (t nil))))
  11074. (save-restriction
  11075. (save-excursion
  11076. (when findpos
  11077. (org-back-to-heading t)
  11078. (narrow-to-region (point) (save-excursion
  11079. (outline-next-heading) (point)))
  11080. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11081. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11082. "[^\r\n]*\\)?"))
  11083. (goto-char (match-end 0))
  11084. (cond
  11085. (drawer
  11086. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11087. nil t)
  11088. (progn
  11089. (goto-char (match-end 0))
  11090. (or org-log-states-order-reversed
  11091. (and (re-search-forward org-property-end-re nil t)
  11092. (goto-char (1- (match-beginning 0))))))
  11093. (insert "\n:" drawer ":\n:END:")
  11094. (beginning-of-line 0)
  11095. (org-indent-line-function)
  11096. (beginning-of-line 2)
  11097. (org-indent-line-function)
  11098. (end-of-line 0)))
  11099. ((and org-log-state-notes-insert-after-drawers
  11100. (save-excursion
  11101. (forward-line) (looking-at org-drawer-regexp)))
  11102. (forward-line)
  11103. (while (looking-at org-drawer-regexp)
  11104. (goto-char (match-end 0))
  11105. (re-search-forward org-property-end-re (point-max) t)
  11106. (forward-line))
  11107. (forward-line -1)))
  11108. (unless org-log-states-order-reversed
  11109. (and (= (char-after) ?\n) (forward-char 1))
  11110. (org-skip-over-state-notes)
  11111. (skip-chars-backward " \t\n\r")))
  11112. (move-marker org-log-note-marker (point))
  11113. (setq org-log-note-purpose purpose
  11114. org-log-note-state state
  11115. org-log-note-previous-state prev-state
  11116. org-log-note-how how
  11117. org-log-note-extra extra
  11118. org-log-note-effective-time (org-current-effective-time))
  11119. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11120. (defun org-skip-over-state-notes ()
  11121. "Skip past the list of State notes in an entry."
  11122. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11123. (when (ignore-errors (goto-char (org-in-item-p)))
  11124. (let* ((struct (org-list-struct))
  11125. (prevs (org-list-prevs-alist struct)))
  11126. (while (looking-at "[ \t]*- State")
  11127. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11128. (org-list-get-item-end (point) struct)))))))
  11129. (defun org-add-log-note (&optional purpose)
  11130. "Pop up a window for taking a note, and add this note later at point."
  11131. (remove-hook 'post-command-hook 'org-add-log-note)
  11132. (setq org-log-note-window-configuration (current-window-configuration))
  11133. (delete-other-windows)
  11134. (move-marker org-log-note-return-to (point))
  11135. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11136. (goto-char org-log-note-marker)
  11137. (org-switch-to-buffer-other-window "*Org Note*")
  11138. (erase-buffer)
  11139. (if (memq org-log-note-how '(time state))
  11140. (let (current-prefix-arg) (org-store-log-note))
  11141. (let ((org-inhibit-startup t)) (org-mode))
  11142. (insert (format "# Insert note for %s.
  11143. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11144. (cond
  11145. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11146. ((eq org-log-note-purpose 'done) "closed todo item")
  11147. ((eq org-log-note-purpose 'state)
  11148. (format "state change from \"%s\" to \"%s\""
  11149. (or org-log-note-previous-state "")
  11150. (or org-log-note-state "")))
  11151. ((eq org-log-note-purpose 'reschedule)
  11152. "rescheduling")
  11153. ((eq org-log-note-purpose 'delschedule)
  11154. "no longer scheduled")
  11155. ((eq org-log-note-purpose 'redeadline)
  11156. "changing deadline")
  11157. ((eq org-log-note-purpose 'deldeadline)
  11158. "removing deadline")
  11159. ((eq org-log-note-purpose 'refile)
  11160. "refiling")
  11161. ((eq org-log-note-purpose 'note)
  11162. "this entry")
  11163. (t (error "This should not happen")))))
  11164. (if org-log-note-extra (insert org-log-note-extra))
  11165. (org-set-local 'org-finish-function 'org-store-log-note)
  11166. (run-hooks 'org-log-buffer-setup-hook)))
  11167. (defvar org-note-abort nil) ; dynamically scoped
  11168. (defun org-store-log-note ()
  11169. "Finish taking a log note, and insert it to where it belongs."
  11170. (let ((txt (buffer-string))
  11171. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11172. lines ind bul)
  11173. (kill-buffer (current-buffer))
  11174. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  11175. (setq txt (replace-match "" t t txt)))
  11176. (if (string-match "\\s-+\\'" txt)
  11177. (setq txt (replace-match "" t t txt)))
  11178. (setq lines (org-split-string txt "\n"))
  11179. (when (and note (string-match "\\S-" note))
  11180. (setq note
  11181. (org-replace-escapes
  11182. note
  11183. (list (cons "%u" (user-login-name))
  11184. (cons "%U" user-full-name)
  11185. (cons "%t" (format-time-string
  11186. (org-time-stamp-format 'long 'inactive)
  11187. org-log-note-effective-time))
  11188. (cons "%T" (format-time-string
  11189. (org-time-stamp-format 'long nil)
  11190. org-log-note-effective-time))
  11191. (cons "%d" (format-time-string
  11192. (org-time-stamp-format nil 'inactive)
  11193. org-log-note-effective-time))
  11194. (cons "%D" (format-time-string
  11195. (org-time-stamp-format nil nil)
  11196. org-log-note-effective-time))
  11197. (cons "%s" (if org-log-note-state
  11198. (concat "\"" org-log-note-state "\"")
  11199. ""))
  11200. (cons "%S" (if org-log-note-previous-state
  11201. (concat "\"" org-log-note-previous-state "\"")
  11202. "\"\"")))))
  11203. (if lines (setq note (concat note " \\\\")))
  11204. (push note lines))
  11205. (when (or current-prefix-arg org-note-abort)
  11206. (when org-log-into-drawer
  11207. (org-remove-empty-drawer-at
  11208. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11209. org-log-note-marker))
  11210. (setq lines nil))
  11211. (when lines
  11212. (with-current-buffer (marker-buffer org-log-note-marker)
  11213. (save-excursion
  11214. (goto-char org-log-note-marker)
  11215. (move-marker org-log-note-marker nil)
  11216. (end-of-line 1)
  11217. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11218. (setq ind (save-excursion
  11219. (if (ignore-errors (goto-char (org-in-item-p)))
  11220. (let ((struct (org-list-struct)))
  11221. (org-list-get-ind
  11222. (org-list-get-top-point struct) struct))
  11223. (skip-chars-backward " \r\t\n")
  11224. (cond
  11225. ((and (org-at-heading-p)
  11226. org-adapt-indentation)
  11227. (1+ (org-current-level)))
  11228. ((org-at-heading-p) 0)
  11229. (t (org-get-indentation))))))
  11230. (setq bul (org-list-bullet-string "-"))
  11231. (org-indent-line-to ind)
  11232. (insert bul (pop lines))
  11233. (let ((ind-body (+ (length bul) ind)))
  11234. (while lines
  11235. (insert "\n")
  11236. (org-indent-line-to ind-body)
  11237. (insert (pop lines))))
  11238. (message "Note stored")
  11239. (org-back-to-heading t)
  11240. (org-cycle-hide-drawers 'children)))))
  11241. (set-window-configuration org-log-note-window-configuration)
  11242. (with-current-buffer (marker-buffer org-log-note-return-to)
  11243. (goto-char org-log-note-return-to))
  11244. (move-marker org-log-note-return-to nil)
  11245. (and org-log-post-message (message "%s" org-log-post-message)))
  11246. (defun org-remove-empty-drawer-at (drawer pos)
  11247. "Remove an empty drawer DRAWER at position POS.
  11248. POS may also be a marker."
  11249. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11250. (save-excursion
  11251. (save-restriction
  11252. (widen)
  11253. (goto-char pos)
  11254. (if (org-in-regexp
  11255. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11256. (replace-match ""))))))
  11257. (defun org-sparse-tree (&optional arg)
  11258. "Create a sparse tree, prompt for the details.
  11259. This command can create sparse trees. You first need to select the type
  11260. of match used to create the tree:
  11261. t Show all TODO entries.
  11262. T Show entries with a specific TODO keyword.
  11263. m Show entries selected by a tags/property match.
  11264. p Enter a property name and its value (both with completion on existing
  11265. names/values) and show entries with that property.
  11266. r Show entries matching a regular expression (`/' can be used as well)
  11267. d Show deadlines due within `org-deadline-warning-days'.
  11268. b Show deadlines and scheduled items before a date.
  11269. a Show deadlines and scheduled items after a date."
  11270. (interactive "P")
  11271. (let (ans kwd value)
  11272. (message "Sparse tree: [r]egexp [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date")
  11273. (setq ans (read-char-exclusive))
  11274. (cond
  11275. ((equal ans ?d)
  11276. (call-interactively 'org-check-deadlines))
  11277. ((equal ans ?b)
  11278. (call-interactively 'org-check-before-date))
  11279. ((equal ans ?a)
  11280. (call-interactively 'org-check-after-date))
  11281. ((equal ans ?t)
  11282. (org-show-todo-tree nil))
  11283. ((equal ans ?T)
  11284. (org-show-todo-tree '(4)))
  11285. ((member ans '(?T ?m))
  11286. (call-interactively 'org-match-sparse-tree))
  11287. ((member ans '(?p ?P))
  11288. (setq kwd (org-icompleting-read "Property: "
  11289. (mapcar 'list (org-buffer-property-keys))))
  11290. (setq value (org-icompleting-read "Value: "
  11291. (mapcar 'list (org-property-values kwd))))
  11292. (unless (string-match "\\`{.*}\\'" value)
  11293. (setq value (concat "\"" value "\"")))
  11294. (org-match-sparse-tree arg (concat kwd "=" value)))
  11295. ((member ans '(?r ?R ?/))
  11296. (call-interactively 'org-occur))
  11297. (t (error "No such sparse tree command \"%c\"" ans)))))
  11298. (defvar org-occur-highlights nil
  11299. "List of overlays used for occur matches.")
  11300. (make-variable-buffer-local 'org-occur-highlights)
  11301. (defvar org-occur-parameters nil
  11302. "Parameters of the active org-occur calls.
  11303. This is a list, each call to org-occur pushes as cons cell,
  11304. containing the regular expression and the callback, onto the list.
  11305. The list can contain several entries if `org-occur' has been called
  11306. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11307. will only contain one set of parameters. When the highlights are
  11308. removed (for example with `C-c C-c', or with the next edit (depending
  11309. on `org-remove-highlights-with-change'), this variable is emptied
  11310. as well.")
  11311. (make-variable-buffer-local 'org-occur-parameters)
  11312. (defun org-occur (regexp &optional keep-previous callback)
  11313. "Make a compact tree which shows all matches of REGEXP.
  11314. The tree will show the lines where the regexp matches, and all higher
  11315. headlines above the match. It will also show the heading after the match,
  11316. to make sure editing the matching entry is easy.
  11317. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11318. call to `org-occur' will be kept, to allow stacking of calls to this
  11319. command.
  11320. If CALLBACK is non-nil, it is a function which is called to confirm
  11321. that the match should indeed be shown."
  11322. (interactive "sRegexp: \nP")
  11323. (when (equal regexp "")
  11324. (error "Regexp cannot be empty"))
  11325. (unless keep-previous
  11326. (org-remove-occur-highlights nil nil t))
  11327. (push (cons regexp callback) org-occur-parameters)
  11328. (let ((cnt 0))
  11329. (save-excursion
  11330. (goto-char (point-min))
  11331. (if (or (not keep-previous) ; do not want to keep
  11332. (not org-occur-highlights)) ; no previous matches
  11333. ;; hide everything
  11334. (org-overview))
  11335. (while (re-search-forward regexp nil t)
  11336. (when (or (not callback)
  11337. (save-match-data (funcall callback)))
  11338. (setq cnt (1+ cnt))
  11339. (when org-highlight-sparse-tree-matches
  11340. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11341. (org-show-context 'occur-tree))))
  11342. (when org-remove-highlights-with-change
  11343. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11344. nil 'local))
  11345. (unless org-sparse-tree-open-archived-trees
  11346. (org-hide-archived-subtrees (point-min) (point-max)))
  11347. (run-hooks 'org-occur-hook)
  11348. (if (org-called-interactively-p 'interactive)
  11349. (message "%d match(es) for regexp %s" cnt regexp))
  11350. cnt))
  11351. (defun org-occur-next-match (&optional n reset)
  11352. "Function for `next-error-function' to find sparse tree matches.
  11353. N is the number of matches to move, when negative move backwards.
  11354. RESET is entirely ignored - this function always goes back to the
  11355. starting point when no match is found."
  11356. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11357. (search-func (if (< n 0)
  11358. 'previous-single-char-property-change
  11359. 'next-single-char-property-change))
  11360. (n (abs n))
  11361. (pos (point))
  11362. p1)
  11363. (catch 'exit
  11364. (while (setq p1 (funcall search-func (point) 'org-type))
  11365. (when (equal p1 limit)
  11366. (goto-char pos)
  11367. (error "No more matches"))
  11368. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11369. (setq n (1- n))
  11370. (when (= n 0)
  11371. (goto-char p1)
  11372. (throw 'exit (point))))
  11373. (goto-char p1))
  11374. (goto-char p1)
  11375. (error "No more matches"))))
  11376. (defun org-show-context (&optional key)
  11377. "Make sure point and context are visible.
  11378. How much context is shown depends upon the variables
  11379. `org-show-hierarchy-above', `org-show-following-heading'. and
  11380. `org-show-siblings'."
  11381. (let ((heading-p (org-on-heading-p t))
  11382. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11383. (following-p (org-get-alist-option org-show-following-heading key))
  11384. (entry-p (org-get-alist-option org-show-entry-below key))
  11385. (siblings-p (org-get-alist-option org-show-siblings key)))
  11386. (catch 'exit
  11387. ;; Show heading or entry text
  11388. (if (and heading-p (not entry-p))
  11389. (org-flag-heading nil) ; only show the heading
  11390. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11391. (org-show-hidden-entry))) ; show entire entry
  11392. (when following-p
  11393. ;; Show next sibling, or heading below text
  11394. (save-excursion
  11395. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11396. (org-flag-heading nil))))
  11397. (when siblings-p (org-show-siblings))
  11398. (when hierarchy-p
  11399. ;; show all higher headings, possibly with siblings
  11400. (save-excursion
  11401. (while (and (condition-case nil
  11402. (progn (org-up-heading-all 1) t)
  11403. (error nil))
  11404. (not (bobp)))
  11405. (org-flag-heading nil)
  11406. (when siblings-p (org-show-siblings))))))))
  11407. (defvar org-reveal-start-hook nil
  11408. "Hook run before revealing a location.")
  11409. (defun org-reveal (&optional siblings)
  11410. "Show current entry, hierarchy above it, and the following headline.
  11411. This can be used to show a consistent set of context around locations
  11412. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11413. not t for the search context.
  11414. With optional argument SIBLINGS, on each level of the hierarchy all
  11415. siblings are shown. This repairs the tree structure to what it would
  11416. look like when opened with hierarchical calls to `org-cycle'.
  11417. With double optional argument \\[universal-argument] \\[universal-argument], \
  11418. go to the parent and show the
  11419. entire tree."
  11420. (interactive "P")
  11421. (run-hooks 'org-reveal-start-hook)
  11422. (let ((org-show-hierarchy-above t)
  11423. (org-show-following-heading t)
  11424. (org-show-siblings (if siblings t org-show-siblings)))
  11425. (org-show-context nil))
  11426. (when (equal siblings '(16))
  11427. (save-excursion
  11428. (when (org-up-heading-safe)
  11429. (org-show-subtree)
  11430. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11431. (defun org-highlight-new-match (beg end)
  11432. "Highlight from BEG to END and mark the highlight is an occur headline."
  11433. (let ((ov (make-overlay beg end)))
  11434. (overlay-put ov 'face 'secondary-selection)
  11435. (overlay-put ov 'org-type 'org-occur)
  11436. (push ov org-occur-highlights)))
  11437. (defun org-remove-occur-highlights (&optional beg end noremove)
  11438. "Remove the occur highlights from the buffer.
  11439. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11440. from the `before-change-functions' in the current buffer."
  11441. (interactive)
  11442. (unless org-inhibit-highlight-removal
  11443. (mapc 'delete-overlay org-occur-highlights)
  11444. (setq org-occur-highlights nil)
  11445. (setq org-occur-parameters nil)
  11446. (unless noremove
  11447. (remove-hook 'before-change-functions
  11448. 'org-remove-occur-highlights 'local))))
  11449. ;;;; Priorities
  11450. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11451. "Regular expression matching the priority indicator.")
  11452. (defvar org-remove-priority-next-time nil)
  11453. (defun org-priority-up ()
  11454. "Increase the priority of the current item."
  11455. (interactive)
  11456. (org-priority 'up))
  11457. (defun org-priority-down ()
  11458. "Decrease the priority of the current item."
  11459. (interactive)
  11460. (org-priority 'down))
  11461. (defun org-priority (&optional action)
  11462. "Change the priority of an item by ARG.
  11463. ACTION can be `set', `up', `down', or a character."
  11464. (interactive)
  11465. (unless org-enable-priority-commands
  11466. (error "Priority commands are disabled"))
  11467. (setq action (or action 'set))
  11468. (let (current new news have remove)
  11469. (save-excursion
  11470. (org-back-to-heading t)
  11471. (if (looking-at org-priority-regexp)
  11472. (setq current (string-to-char (match-string 2))
  11473. have t))
  11474. (cond
  11475. ((eq action 'remove)
  11476. (setq remove t new ?\ ))
  11477. ((or (eq action 'set)
  11478. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11479. (if (not (eq action 'set))
  11480. (setq new action)
  11481. (message "Priority %c-%c, SPC to remove: "
  11482. org-highest-priority org-lowest-priority)
  11483. (save-match-data
  11484. (setq new (read-char-exclusive))))
  11485. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11486. (= (upcase org-lowest-priority) org-lowest-priority))
  11487. (setq new (upcase new)))
  11488. (cond ((equal new ?\ ) (setq remove t))
  11489. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11490. (error "Priority must be between `%c' and `%c'"
  11491. org-highest-priority org-lowest-priority))))
  11492. ((eq action 'up)
  11493. (setq new (if have
  11494. (1- current) ; normal cycling
  11495. ;; last priority was empty
  11496. (if (eq last-command this-command)
  11497. org-lowest-priority ; wrap around empty to lowest
  11498. ;; default
  11499. (if org-priority-start-cycle-with-default
  11500. org-default-priority
  11501. (1- org-default-priority))))))
  11502. ((eq action 'down)
  11503. (setq new (if have
  11504. (1+ current) ; normal cycling
  11505. ;; last priority was empty
  11506. (if (eq last-command this-command)
  11507. org-highest-priority ; wrap around empty to highest
  11508. ;; default
  11509. (if org-priority-start-cycle-with-default
  11510. org-default-priority
  11511. (1+ org-default-priority))))))
  11512. (t (error "Invalid action")))
  11513. (if (or (< (upcase new) org-highest-priority)
  11514. (> (upcase new) org-lowest-priority))
  11515. (if (and (memq action '(up down))
  11516. (not have) (not (eq last-command this-command)))
  11517. ;; `new' is from default priority
  11518. (error
  11519. "The default can not be set, see `org-default-priority' why")
  11520. ;; normal cycling: `new' is beyond highest/lowest priority
  11521. ;; and is wrapped around to the empty priority
  11522. (setq remove t)))
  11523. (setq news (format "%c" new))
  11524. (if have
  11525. (if remove
  11526. (replace-match "" t t nil 1)
  11527. (replace-match news t t nil 2))
  11528. (if remove
  11529. (error "No priority cookie found in line")
  11530. (let ((case-fold-search nil))
  11531. (looking-at org-todo-line-regexp))
  11532. (if (match-end 2)
  11533. (progn
  11534. (goto-char (match-end 2))
  11535. (insert " [#" news "]"))
  11536. (goto-char (match-beginning 3))
  11537. (insert "[#" news "] "))))
  11538. (org-preserve-lc (org-set-tags nil 'align)))
  11539. (if remove
  11540. (message "Priority removed")
  11541. (message "Priority of current item set to %s" news))))
  11542. (defun org-get-priority (s)
  11543. "Find priority cookie and return priority."
  11544. (if (functionp org-get-priority-function)
  11545. (funcall org-get-priority-function)
  11546. (save-match-data
  11547. (if (not (string-match org-priority-regexp s))
  11548. (* 1000 (- org-lowest-priority org-default-priority))
  11549. (* 1000 (- org-lowest-priority
  11550. (string-to-char (match-string 2 s))))))))
  11551. ;;;; Tags
  11552. (defvar org-agenda-archives-mode)
  11553. (defvar org-map-continue-from nil
  11554. "Position from where mapping should continue.
  11555. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  11556. (defvar org-scanner-tags nil
  11557. "The current tag list while the tags scanner is running.")
  11558. (defvar org-trust-scanner-tags nil
  11559. "Should `org-get-tags-at' use the tags for the scanner.
  11560. This is for internal dynamical scoping only.
  11561. When this is non-nil, the function `org-get-tags-at' will return the value
  11562. of `org-scanner-tags' instead of building the list by itself. This
  11563. can lead to large speed-ups when the tags scanner is used in a file with
  11564. many entries, and when the list of tags is retrieved, for example to
  11565. obtain a list of properties. Building the tags list for each entry in such
  11566. a file becomes an N^2 operation - but with this variable set, it scales
  11567. as N.")
  11568. (defun org-scan-tags (action matcher &optional todo-only)
  11569. "Scan headline tags with inheritance and produce output ACTION.
  11570. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11571. or `agenda' to produce an entry list for an agenda view. It can also be
  11572. a Lisp form or a function that should be called at each matched headline, in
  11573. this case the return value is a list of all return values from these calls.
  11574. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11575. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11576. only lines with a TODO keyword are included in the output."
  11577. (require 'org-agenda)
  11578. (let* ((re (concat "^" org-outline-regexp " *\\(\\<\\("
  11579. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11580. (org-re
  11581. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11582. (props (list 'face 'default
  11583. 'done-face 'org-agenda-done
  11584. 'undone-face 'default
  11585. 'mouse-face 'highlight
  11586. 'org-not-done-regexp org-not-done-regexp
  11587. 'org-todo-regexp org-todo-regexp
  11588. 'org-complex-heading-regexp org-complex-heading-regexp
  11589. 'help-echo
  11590. (format "mouse-2 or RET jump to org file %s"
  11591. (abbreviate-file-name
  11592. (or (buffer-file-name (buffer-base-buffer))
  11593. (buffer-name (buffer-base-buffer)))))))
  11594. (case-fold-search nil)
  11595. (org-map-continue-from nil)
  11596. lspos tags tags-list
  11597. (tags-alist (list (cons 0 org-file-tags)))
  11598. (llast 0) rtn rtn1 level category i txt
  11599. todo marker entry priority)
  11600. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11601. (setq action (list 'lambda nil action)))
  11602. (save-excursion
  11603. (goto-char (point-min))
  11604. (when (eq action 'sparse-tree)
  11605. (org-overview)
  11606. (org-remove-occur-highlights))
  11607. (while (re-search-forward re nil t)
  11608. (catch :skip
  11609. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11610. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11611. (goto-char (setq lspos (match-beginning 0)))
  11612. (setq level (org-reduced-level (funcall outline-level))
  11613. category (org-get-category))
  11614. (setq i llast llast level)
  11615. ;; remove tag lists from same and sublevels
  11616. (while (>= i level)
  11617. (when (setq entry (assoc i tags-alist))
  11618. (setq tags-alist (delete entry tags-alist)))
  11619. (setq i (1- i)))
  11620. ;; add the next tags
  11621. (when tags
  11622. (setq tags (org-split-string tags ":")
  11623. tags-alist
  11624. (cons (cons level tags) tags-alist)))
  11625. ;; compile tags for current headline
  11626. (setq tags-list
  11627. (if org-use-tag-inheritance
  11628. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11629. tags)
  11630. org-scanner-tags tags-list)
  11631. (when org-use-tag-inheritance
  11632. (setcdr (car tags-alist)
  11633. (mapcar (lambda (x)
  11634. (setq x (copy-sequence x))
  11635. (org-add-prop-inherited x))
  11636. (cdar tags-alist))))
  11637. (when (and tags org-use-tag-inheritance
  11638. (or (not (eq t org-use-tag-inheritance))
  11639. org-tags-exclude-from-inheritance))
  11640. ;; selective inheritance, remove uninherited ones
  11641. (setcdr (car tags-alist)
  11642. (org-remove-uninherited-tags (cdar tags-alist))))
  11643. (when (and
  11644. ;; eval matcher only when the todo condition is OK
  11645. (and (or (not todo-only) (member todo org-not-done-keywords))
  11646. (let ((case-fold-search t)) (eval matcher)))
  11647. ;; Call the skipper, but return t if it does not skip,
  11648. ;; so that the `and' form continues evaluating
  11649. (progn
  11650. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11651. t)
  11652. ;; Check if timestamps are deselecting this entry
  11653. (or (not todo-only)
  11654. (and (member todo org-not-done-keywords)
  11655. (or (not org-agenda-tags-todo-honor-ignore-options)
  11656. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11657. ;; Extra check for the archive tag
  11658. ;; FIXME: Does the skipper already do this????
  11659. (or
  11660. (not (member org-archive-tag tags-list))
  11661. ;; we have an archive tag, should we use this anyway?
  11662. (or (not org-agenda-skip-archived-trees)
  11663. (and (eq action 'agenda) org-agenda-archives-mode))))
  11664. ;; select this headline
  11665. (cond
  11666. ((eq action 'sparse-tree)
  11667. (and org-highlight-sparse-tree-matches
  11668. (org-get-heading) (match-end 0)
  11669. (org-highlight-new-match
  11670. (match-beginning 0) (match-beginning 1)))
  11671. (org-show-context 'tags-tree))
  11672. ((eq action 'agenda)
  11673. (setq txt (org-agenda-format-item
  11674. ""
  11675. (concat
  11676. (if (eq org-tags-match-list-sublevels 'indented)
  11677. (make-string (1- level) ?.) "")
  11678. (org-get-heading))
  11679. category
  11680. tags-list
  11681. )
  11682. priority (org-get-priority txt))
  11683. (goto-char lspos)
  11684. (setq marker (org-agenda-new-marker))
  11685. (org-add-props txt props
  11686. 'org-marker marker 'org-hd-marker marker 'org-category category
  11687. 'todo-state todo
  11688. 'priority priority 'type "tagsmatch")
  11689. (push txt rtn))
  11690. ((functionp action)
  11691. (setq org-map-continue-from nil)
  11692. (save-excursion
  11693. (setq rtn1 (funcall action))
  11694. (push rtn1 rtn)))
  11695. (t (error "Invalid action")))
  11696. ;; if we are to skip sublevels, jump to end of subtree
  11697. (unless org-tags-match-list-sublevels
  11698. (org-end-of-subtree t)
  11699. (backward-char 1))))
  11700. ;; Get the correct position from where to continue
  11701. (if org-map-continue-from
  11702. (goto-char org-map-continue-from)
  11703. (and (= (point) lspos) (end-of-line 1)))))
  11704. (when (and (eq action 'sparse-tree)
  11705. (not org-sparse-tree-open-archived-trees))
  11706. (org-hide-archived-subtrees (point-min) (point-max)))
  11707. (nreverse rtn)))
  11708. (defun org-remove-uninherited-tags (tags)
  11709. "Remove all tags that are not inherited from the list TAGS."
  11710. (cond
  11711. ((eq org-use-tag-inheritance t)
  11712. (if org-tags-exclude-from-inheritance
  11713. (org-delete-all org-tags-exclude-from-inheritance tags)
  11714. tags))
  11715. ((not org-use-tag-inheritance) nil)
  11716. ((stringp org-use-tag-inheritance)
  11717. (delq nil (mapcar
  11718. (lambda (x)
  11719. (if (and (string-match org-use-tag-inheritance x)
  11720. (not (member x org-tags-exclude-from-inheritance)))
  11721. x nil))
  11722. tags)))
  11723. ((listp org-use-tag-inheritance)
  11724. (delq nil (mapcar
  11725. (lambda (x)
  11726. (if (member x org-use-tag-inheritance) x nil))
  11727. tags)))))
  11728. (defvar todo-only) ;; dynamically scoped
  11729. (defun org-match-sparse-tree (&optional todo-only match)
  11730. "Create a sparse tree according to tags string MATCH.
  11731. MATCH can contain positive and negative selection of tags, like
  11732. \"+WORK+URGENT-WITHBOSS\".
  11733. If optional argument TODO-ONLY is non-nil, only select lines that are
  11734. also TODO lines."
  11735. (interactive "P")
  11736. (org-prepare-agenda-buffers (list (current-buffer)))
  11737. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11738. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11739. (defvar org-cached-props nil)
  11740. (defun org-cached-entry-get (pom property)
  11741. (if (or (eq t org-use-property-inheritance)
  11742. (and (stringp org-use-property-inheritance)
  11743. (string-match org-use-property-inheritance property))
  11744. (and (listp org-use-property-inheritance)
  11745. (member property org-use-property-inheritance)))
  11746. ;; Caching is not possible, check it directly
  11747. (org-entry-get pom property 'inherit)
  11748. ;; Get all properties, so that we can do complicated checks easily
  11749. (cdr (assoc property (or org-cached-props
  11750. (setq org-cached-props
  11751. (org-entry-properties pom)))))))
  11752. (defun org-global-tags-completion-table (&optional files)
  11753. "Return the list of all tags in all agenda buffer/files.
  11754. Optional FILES argument is a list of files to which can be used
  11755. instead of the agenda files."
  11756. (save-excursion
  11757. (org-uniquify
  11758. (delq nil
  11759. (apply 'append
  11760. (mapcar
  11761. (lambda (file)
  11762. (set-buffer (find-file-noselect file))
  11763. (append (org-get-buffer-tags)
  11764. (mapcar (lambda (x) (if (stringp (car-safe x))
  11765. (list (car-safe x)) nil))
  11766. org-tag-alist)))
  11767. (if (and files (car files))
  11768. files
  11769. (org-agenda-files))))))))
  11770. (defun org-make-tags-matcher (match)
  11771. "Create the TAGS/TODO matcher form for the selection string MATCH."
  11772. ;; todo-only is scoped dynamically into this function, and the function
  11773. ;; may change it if the matcher asks for it.
  11774. (unless match
  11775. ;; Get a new match request, with completion
  11776. (let ((org-last-tags-completion-table
  11777. (org-global-tags-completion-table)))
  11778. (setq match (org-completing-read-no-i
  11779. "Match: " 'org-tags-completion-function nil nil nil
  11780. 'org-tags-history))))
  11781. ;; Parse the string and create a lisp form
  11782. (let ((match0 match)
  11783. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11784. minus tag mm
  11785. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11786. orterms term orlist re-p str-p level-p level-op time-p
  11787. prop-p pn pv po gv rest)
  11788. (if (string-match "/+" match)
  11789. ;; match contains also a todo-matching request
  11790. (progn
  11791. (setq tagsmatch (substring match 0 (match-beginning 0))
  11792. todomatch (substring match (match-end 0)))
  11793. (if (string-match "^!" todomatch)
  11794. (setq todo-only t todomatch (substring todomatch 1)))
  11795. (if (string-match "^\\s-*$" todomatch)
  11796. (setq todomatch nil)))
  11797. ;; only matching tags
  11798. (setq tagsmatch match todomatch nil))
  11799. ;; Make the tags matcher
  11800. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11801. (setq tagsmatcher t)
  11802. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11803. (while (setq term (pop orterms))
  11804. (while (and (equal (substring term -1) "\\") orterms)
  11805. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11806. (while (string-match re term)
  11807. (setq rest (substring term (match-end 0))
  11808. minus (and (match-end 1)
  11809. (equal (match-string 1 term) "-"))
  11810. tag (save-match-data (replace-regexp-in-string
  11811. "\\\\-" "-"
  11812. (match-string 2 term)))
  11813. re-p (equal (string-to-char tag) ?{)
  11814. level-p (match-end 4)
  11815. prop-p (match-end 5)
  11816. mm (cond
  11817. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11818. (level-p
  11819. (setq level-op (org-op-to-function (match-string 3 term)))
  11820. `(,level-op level ,(string-to-number
  11821. (match-string 4 term))))
  11822. (prop-p
  11823. (setq pn (match-string 5 term)
  11824. po (match-string 6 term)
  11825. pv (match-string 7 term)
  11826. re-p (equal (string-to-char pv) ?{)
  11827. str-p (equal (string-to-char pv) ?\")
  11828. time-p (save-match-data
  11829. (string-match "^\"[[<].*[]>]\"$" pv))
  11830. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11831. (if time-p (setq pv (org-matcher-time pv)))
  11832. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11833. (cond
  11834. ((equal pn "CATEGORY")
  11835. (setq gv '(get-text-property (point) 'org-category)))
  11836. ((equal pn "TODO")
  11837. (setq gv 'todo))
  11838. (t
  11839. (setq gv `(org-cached-entry-get nil ,pn))))
  11840. (if re-p
  11841. (if (eq po 'org<>)
  11842. `(not (string-match ,pv (or ,gv "")))
  11843. `(string-match ,pv (or ,gv "")))
  11844. (if str-p
  11845. `(,po (or ,gv "") ,pv)
  11846. `(,po (string-to-number (or ,gv ""))
  11847. ,(string-to-number pv) ))))
  11848. (t `(member ,tag tags-list)))
  11849. mm (if minus (list 'not mm) mm)
  11850. term rest)
  11851. (push mm tagsmatcher))
  11852. (push (if (> (length tagsmatcher) 1)
  11853. (cons 'and tagsmatcher)
  11854. (car tagsmatcher))
  11855. orlist)
  11856. (setq tagsmatcher nil))
  11857. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11858. (setq tagsmatcher
  11859. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11860. ;; Make the todo matcher
  11861. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11862. (setq todomatcher t)
  11863. (setq orterms (org-split-string todomatch "|") orlist nil)
  11864. (while (setq term (pop orterms))
  11865. (while (string-match re term)
  11866. (setq minus (and (match-end 1)
  11867. (equal (match-string 1 term) "-"))
  11868. kwd (match-string 2 term)
  11869. re-p (equal (string-to-char kwd) ?{)
  11870. term (substring term (match-end 0))
  11871. mm (if re-p
  11872. `(string-match ,(substring kwd 1 -1) todo)
  11873. (list 'equal 'todo kwd))
  11874. mm (if minus (list 'not mm) mm))
  11875. (push mm todomatcher))
  11876. (push (if (> (length todomatcher) 1)
  11877. (cons 'and todomatcher)
  11878. (car todomatcher))
  11879. orlist)
  11880. (setq todomatcher nil))
  11881. (setq todomatcher (if (> (length orlist) 1)
  11882. (cons 'or orlist) (car orlist))))
  11883. ;; Return the string and lisp forms of the matcher
  11884. (setq matcher (if todomatcher
  11885. (list 'and tagsmatcher todomatcher)
  11886. tagsmatcher))
  11887. (cons match0 matcher)))
  11888. (defun org-op-to-function (op &optional stringp)
  11889. "Turn an operator into the appropriate function."
  11890. (setq op
  11891. (cond
  11892. ((equal op "<" ) '(< string< org-time<))
  11893. ((equal op ">" ) '(> org-string> org-time>))
  11894. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11895. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11896. ((member op '("=" "==")) '(= string= org-time=))
  11897. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11898. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11899. (defun org<> (a b) (not (= a b)))
  11900. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11901. (defun org-string>= (a b) (not (string< a b)))
  11902. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11903. (defun org-string<> (a b) (not (string= a b)))
  11904. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  11905. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  11906. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  11907. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  11908. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  11909. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  11910. (defun org-2ft (s)
  11911. "Convert S to a floating point time.
  11912. If S is already a number, just return it. If it is a string, parse
  11913. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11914. (cond
  11915. ((numberp s) s)
  11916. ((stringp s)
  11917. (condition-case nil
  11918. (float-time (apply 'encode-time (org-parse-time-string s)))
  11919. (error 0.)))
  11920. (t 0.)))
  11921. (defun org-time-today ()
  11922. "Time in seconds today at 0:00.
  11923. Returns the float number of seconds since the beginning of the
  11924. epoch to the beginning of today (00:00)."
  11925. (float-time (apply 'encode-time
  11926. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  11927. (defun org-matcher-time (s)
  11928. "Interpret a time comparison value."
  11929. (save-match-data
  11930. (cond
  11931. ((string= s "<now>") (float-time))
  11932. ((string= s "<today>") (org-time-today))
  11933. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  11934. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  11935. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  11936. (+ (org-time-today)
  11937. (* (string-to-number (match-string 1 s))
  11938. (cdr (assoc (match-string 2 s)
  11939. '(("d" . 86400.0) ("w" . 604800.0)
  11940. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  11941. (t (org-2ft s)))))
  11942. (defun org-match-any-p (re list)
  11943. "Does re match any element of list?"
  11944. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11945. (delq nil list))
  11946. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  11947. (defvar org-tags-overlay (make-overlay 1 1))
  11948. (org-detach-overlay org-tags-overlay)
  11949. (defun org-get-local-tags-at (&optional pos)
  11950. "Get a list of tags defined in the current headline."
  11951. (org-get-tags-at pos 'local))
  11952. (defun org-get-local-tags ()
  11953. "Get a list of tags defined in the current headline."
  11954. (org-get-tags-at nil 'local))
  11955. (defun org-get-tags-at (&optional pos local)
  11956. "Get a list of all headline tags applicable at POS.
  11957. POS defaults to point. If tags are inherited, the list contains
  11958. the targets in the same sequence as the headlines appear, i.e.
  11959. the tags of the current headline come last.
  11960. When LOCAL is non-nil, only return tags from the current headline,
  11961. ignore inherited ones."
  11962. (interactive)
  11963. (if (and org-trust-scanner-tags
  11964. (or (not pos) (equal pos (point)))
  11965. (not local))
  11966. org-scanner-tags
  11967. (let (tags ltags lastpos parent)
  11968. (save-excursion
  11969. (save-restriction
  11970. (widen)
  11971. (goto-char (or pos (point)))
  11972. (save-match-data
  11973. (catch 'done
  11974. (condition-case nil
  11975. (progn
  11976. (org-back-to-heading t)
  11977. (while (not (equal lastpos (point)))
  11978. (setq lastpos (point))
  11979. (when (looking-at
  11980. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  11981. (setq ltags (org-split-string
  11982. (org-match-string-no-properties 1) ":"))
  11983. (when parent
  11984. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  11985. (setq tags (append
  11986. (if parent
  11987. (org-remove-uninherited-tags ltags)
  11988. ltags)
  11989. tags)))
  11990. (or org-use-tag-inheritance (throw 'done t))
  11991. (if local (throw 'done t))
  11992. (or (org-up-heading-safe) (error nil))
  11993. (setq parent t)))
  11994. (error nil)))))
  11995. (if local
  11996. tags
  11997. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  11998. (defun org-add-prop-inherited (s)
  11999. (add-text-properties 0 (length s) '(inherited t) s)
  12000. s)
  12001. (defun org-toggle-tag (tag &optional onoff)
  12002. "Toggle the tag TAG for the current line.
  12003. If ONOFF is `on' or `off', don't toggle but set to this state."
  12004. (let (res current)
  12005. (save-excursion
  12006. (org-back-to-heading t)
  12007. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12008. (point-at-eol) t)
  12009. (progn
  12010. (setq current (match-string 1))
  12011. (replace-match ""))
  12012. (setq current ""))
  12013. (setq current (nreverse (org-split-string current ":")))
  12014. (cond
  12015. ((eq onoff 'on)
  12016. (setq res t)
  12017. (or (member tag current) (push tag current)))
  12018. ((eq onoff 'off)
  12019. (or (not (member tag current)) (setq current (delete tag current))))
  12020. (t (if (member tag current)
  12021. (setq current (delete tag current))
  12022. (setq res t)
  12023. (push tag current))))
  12024. (end-of-line 1)
  12025. (if current
  12026. (progn
  12027. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12028. (org-set-tags nil t))
  12029. (delete-horizontal-space))
  12030. (run-hooks 'org-after-tags-change-hook))
  12031. res))
  12032. (defun org-align-tags-here (to-col)
  12033. ;; Assumes that this is a headline
  12034. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12035. (beginning-of-line 1)
  12036. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12037. (< pos (match-beginning 2)))
  12038. (progn
  12039. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12040. (goto-char (match-beginning 1))
  12041. (insert " ")
  12042. (delete-region (point) (1+ (match-beginning 2)))
  12043. (setq ncol (max (current-column)
  12044. (1+ col)
  12045. (if (> to-col 0)
  12046. to-col
  12047. (- (abs to-col) tags-l))))
  12048. (setq p (point))
  12049. (insert (make-string (- ncol (current-column)) ?\ ))
  12050. (setq ncol (current-column))
  12051. (when indent-tabs-mode (tabify p (point-at-eol)))
  12052. (org-move-to-column (min ncol col) t))
  12053. (goto-char pos))))
  12054. (defun org-set-tags-command (&optional arg just-align)
  12055. "Call the set-tags command for the current entry."
  12056. (interactive "P")
  12057. (if (org-on-heading-p)
  12058. (org-set-tags arg just-align)
  12059. (save-excursion
  12060. (org-back-to-heading t)
  12061. (org-set-tags arg just-align))))
  12062. (defun org-set-tags-to (data)
  12063. "Set the tags of the current entry to DATA, replacing the current tags.
  12064. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12065. If DATA is nil or the empty string, any tags will be removed."
  12066. (interactive "sTags: ")
  12067. (setq data
  12068. (cond
  12069. ((eq data nil) "")
  12070. ((equal data "") "")
  12071. ((stringp data)
  12072. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12073. ":"))
  12074. ((listp data)
  12075. (concat ":" (mapconcat 'identity data ":") ":"))
  12076. (t nil)))
  12077. (when data
  12078. (save-excursion
  12079. (org-back-to-heading t)
  12080. (when (looking-at org-complex-heading-regexp)
  12081. (if (match-end 5)
  12082. (progn
  12083. (goto-char (match-beginning 5))
  12084. (insert data)
  12085. (delete-region (point) (point-at-eol))
  12086. (org-set-tags nil 'align))
  12087. (goto-char (point-at-eol))
  12088. (insert " " data)
  12089. (org-set-tags nil 'align)))
  12090. (beginning-of-line 1)
  12091. (if (looking-at ".*?\\([ \t]+\\)$")
  12092. (delete-region (match-beginning 1) (match-end 1))))))
  12093. (defun org-align-all-tags ()
  12094. "Align the tags i all headings."
  12095. (interactive)
  12096. (save-excursion
  12097. (or (ignore-errors (org-back-to-heading t))
  12098. (outline-next-heading))
  12099. (if (org-on-heading-p)
  12100. (org-set-tags t)
  12101. (message "No headings"))))
  12102. (defvar org-indent-indentation-per-level)
  12103. (defun org-set-tags (&optional arg just-align)
  12104. "Set the tags for the current headline.
  12105. With prefix ARG, realign all tags in headings in the current buffer."
  12106. (interactive "P")
  12107. (let* ((re org-outline-regexp-bol)
  12108. (current (org-get-tags-string))
  12109. (col (current-column))
  12110. (org-setting-tags t)
  12111. table current-tags inherited-tags ; computed below when needed
  12112. tags p0 c0 c1 rpl di tc level)
  12113. (if arg
  12114. (save-excursion
  12115. (goto-char (point-min))
  12116. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12117. (while (re-search-forward re nil t)
  12118. (org-set-tags nil t)
  12119. (end-of-line 1)))
  12120. (message "All tags realigned to column %d" org-tags-column))
  12121. (if just-align
  12122. (setq tags current)
  12123. ;; Get a new set of tags from the user
  12124. (save-excursion
  12125. (setq table (append org-tag-persistent-alist
  12126. (or org-tag-alist (org-get-buffer-tags))
  12127. (and
  12128. org-complete-tags-always-offer-all-agenda-tags
  12129. (org-global-tags-completion-table
  12130. (org-agenda-files))))
  12131. org-last-tags-completion-table table
  12132. current-tags (org-split-string current ":")
  12133. inherited-tags (nreverse
  12134. (nthcdr (length current-tags)
  12135. (nreverse (org-get-tags-at))))
  12136. tags
  12137. (if (or (eq t org-use-fast-tag-selection)
  12138. (and org-use-fast-tag-selection
  12139. (delq nil (mapcar 'cdr table))))
  12140. (org-fast-tag-selection
  12141. current-tags inherited-tags table
  12142. (if org-fast-tag-selection-include-todo
  12143. org-todo-key-alist))
  12144. (let ((org-add-colon-after-tag-completion t))
  12145. (org-trim
  12146. (org-icompleting-read "Tags: "
  12147. 'org-tags-completion-function
  12148. nil nil current 'org-tags-history))))))
  12149. (while (string-match "[-+&]+" tags)
  12150. ;; No boolean logic, just a list
  12151. (setq tags (replace-match ":" t t tags))))
  12152. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12153. (if org-tags-sort-function
  12154. (setq tags (mapconcat 'identity
  12155. (sort (org-split-string
  12156. tags (org-re "[^[:alnum:]_@#%]+"))
  12157. org-tags-sort-function) ":")))
  12158. (if (string-match "\\`[\t ]*\\'" tags)
  12159. (setq tags "")
  12160. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12161. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12162. ;; Insert new tags at the correct column
  12163. (beginning-of-line 1)
  12164. (setq level (or (and (looking-at org-outline-regexp)
  12165. (- (match-end 0) (point) 1))
  12166. 1))
  12167. (cond
  12168. ((and (equal current "") (equal tags "")))
  12169. ((re-search-forward
  12170. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12171. (point-at-eol) t)
  12172. (if (equal tags "")
  12173. (setq rpl "")
  12174. (goto-char (match-beginning 0))
  12175. (setq c0 (current-column)
  12176. ;; compute offset for the case of org-indent-mode active
  12177. di (if org-indent-mode
  12178. (* (1- org-indent-indentation-per-level) (1- level))
  12179. 0)
  12180. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12181. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12182. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12183. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12184. (replace-match rpl t t)
  12185. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12186. tags)
  12187. (t (error "Tags alignment failed")))
  12188. (org-move-to-column col)
  12189. (unless just-align
  12190. (run-hooks 'org-after-tags-change-hook)))))
  12191. (defun org-change-tag-in-region (beg end tag off)
  12192. "Add or remove TAG for each entry in the region.
  12193. This works in the agenda, and also in an org-mode buffer."
  12194. (interactive
  12195. (list (region-beginning) (region-end)
  12196. (let ((org-last-tags-completion-table
  12197. (if (eq major-mode 'org-mode)
  12198. (org-get-buffer-tags)
  12199. (org-global-tags-completion-table))))
  12200. (org-icompleting-read
  12201. "Tag: " 'org-tags-completion-function nil nil nil
  12202. 'org-tags-history))
  12203. (progn
  12204. (message "[s]et or [r]emove? ")
  12205. (equal (read-char-exclusive) ?r))))
  12206. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12207. (let ((agendap (equal major-mode 'org-agenda-mode))
  12208. l1 l2 m buf pos newhead (cnt 0))
  12209. (goto-char end)
  12210. (setq l2 (1- (org-current-line)))
  12211. (goto-char beg)
  12212. (setq l1 (org-current-line))
  12213. (loop for l from l1 to l2 do
  12214. (org-goto-line l)
  12215. (setq m (get-text-property (point) 'org-hd-marker))
  12216. (when (or (and (eq major-mode 'org-mode) (org-on-heading-p))
  12217. (and agendap m))
  12218. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12219. pos (if agendap m (point)))
  12220. (with-current-buffer buf
  12221. (save-excursion
  12222. (save-restriction
  12223. (goto-char pos)
  12224. (setq cnt (1+ cnt))
  12225. (org-toggle-tag tag (if off 'off 'on))
  12226. (setq newhead (org-get-heading)))))
  12227. (and agendap (org-agenda-change-all-lines newhead m))))
  12228. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12229. (defun org-tags-completion-function (string predicate &optional flag)
  12230. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12231. (confirm (lambda (x) (stringp (car x)))))
  12232. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12233. (setq s1 (match-string 1 string)
  12234. s2 (match-string 2 string))
  12235. (setq s1 "" s2 string))
  12236. (cond
  12237. ((eq flag nil)
  12238. ;; try completion
  12239. (setq rtn (try-completion s2 ctable confirm))
  12240. (if (stringp rtn)
  12241. (setq rtn
  12242. (concat s1 s2 (substring rtn (length s2))
  12243. (if (and org-add-colon-after-tag-completion
  12244. (assoc rtn ctable))
  12245. ":" ""))))
  12246. rtn)
  12247. ((eq flag t)
  12248. ;; all-completions
  12249. (all-completions s2 ctable confirm)
  12250. )
  12251. ((eq flag 'lambda)
  12252. ;; exact match?
  12253. (assoc s2 ctable)))
  12254. ))
  12255. (defun org-fast-tag-insert (kwd tags face &optional end)
  12256. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12257. (insert (format "%-12s" (concat kwd ":"))
  12258. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12259. (or end "")))
  12260. (defun org-fast-tag-show-exit (flag)
  12261. (save-excursion
  12262. (org-goto-line 3)
  12263. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12264. (replace-match ""))
  12265. (when flag
  12266. (end-of-line 1)
  12267. (org-move-to-column (- (window-width) 19) t)
  12268. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12269. (defun org-set-current-tags-overlay (current prefix)
  12270. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12271. (if (featurep 'xemacs)
  12272. (org-overlay-display org-tags-overlay (concat prefix s)
  12273. 'secondary-selection)
  12274. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12275. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12276. (defvar org-last-tag-selection-key nil)
  12277. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12278. "Fast tag selection with single keys.
  12279. CURRENT is the current list of tags in the headline, INHERITED is the
  12280. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12281. possibly with grouping information. TODO-TABLE is a similar table with
  12282. TODO keywords, should these have keys assigned to them.
  12283. If the keys are nil, a-z are automatically assigned.
  12284. Returns the new tags string, or nil to not change the current settings."
  12285. (let* ((fulltable (append table todo-table))
  12286. (maxlen (apply 'max (mapcar
  12287. (lambda (x)
  12288. (if (stringp (car x)) (string-width (car x)) 0))
  12289. fulltable)))
  12290. (buf (current-buffer))
  12291. (expert (eq org-fast-tag-selection-single-key 'expert))
  12292. (buffer-tags nil)
  12293. (fwidth (+ maxlen 3 1 3))
  12294. (ncol (/ (- (window-width) 4) fwidth))
  12295. (i-face 'org-done)
  12296. (c-face 'org-todo)
  12297. tg cnt e c char c1 c2 ntable tbl rtn
  12298. ov-start ov-end ov-prefix
  12299. (exit-after-next org-fast-tag-selection-single-key)
  12300. (done-keywords org-done-keywords)
  12301. groups ingroup)
  12302. (save-excursion
  12303. (beginning-of-line 1)
  12304. (if (looking-at
  12305. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12306. (setq ov-start (match-beginning 1)
  12307. ov-end (match-end 1)
  12308. ov-prefix "")
  12309. (setq ov-start (1- (point-at-eol))
  12310. ov-end (1+ ov-start))
  12311. (skip-chars-forward "^\n\r")
  12312. (setq ov-prefix
  12313. (concat
  12314. (buffer-substring (1- (point)) (point))
  12315. (if (> (current-column) org-tags-column)
  12316. " "
  12317. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12318. (move-overlay org-tags-overlay ov-start ov-end)
  12319. (save-window-excursion
  12320. (if expert
  12321. (set-buffer (get-buffer-create " *Org tags*"))
  12322. (delete-other-windows)
  12323. (split-window-vertically)
  12324. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12325. (erase-buffer)
  12326. (org-set-local 'org-done-keywords done-keywords)
  12327. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12328. (org-fast-tag-insert "Current" current c-face "\n\n")
  12329. (org-fast-tag-show-exit exit-after-next)
  12330. (org-set-current-tags-overlay current ov-prefix)
  12331. (setq tbl fulltable char ?a cnt 0)
  12332. (while (setq e (pop tbl))
  12333. (cond
  12334. ((equal (car e) :startgroup)
  12335. (push '() groups) (setq ingroup t)
  12336. (when (not (= cnt 0))
  12337. (setq cnt 0)
  12338. (insert "\n"))
  12339. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12340. ((equal (car e) :endgroup)
  12341. (setq ingroup nil cnt 0)
  12342. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12343. ((equal e '(:newline))
  12344. (when (not (= cnt 0))
  12345. (setq cnt 0)
  12346. (insert "\n")
  12347. (setq e (car tbl))
  12348. (while (equal (car tbl) '(:newline))
  12349. (insert "\n")
  12350. (setq tbl (cdr tbl)))))
  12351. (t
  12352. (setq tg (copy-sequence (car e)) c2 nil)
  12353. (if (cdr e)
  12354. (setq c (cdr e))
  12355. ;; automatically assign a character.
  12356. (setq c1 (string-to-char
  12357. (downcase (substring
  12358. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12359. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12360. (while (or (rassoc char ntable) (rassoc char table))
  12361. (setq char (1+ char)))
  12362. (setq c2 c1))
  12363. (setq c (or c2 char)))
  12364. (if ingroup (push tg (car groups)))
  12365. (setq tg (org-add-props tg nil 'face
  12366. (cond
  12367. ((not (assoc tg table))
  12368. (org-get-todo-face tg))
  12369. ((member tg current) c-face)
  12370. ((member tg inherited) i-face)
  12371. (t nil))))
  12372. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12373. (insert "[" c "] " tg (make-string
  12374. (- fwidth 4 (length tg)) ?\ ))
  12375. (push (cons tg c) ntable)
  12376. (when (= (setq cnt (1+ cnt)) ncol)
  12377. (insert "\n")
  12378. (if ingroup (insert " "))
  12379. (setq cnt 0)))))
  12380. (setq ntable (nreverse ntable))
  12381. (insert "\n")
  12382. (goto-char (point-min))
  12383. (if (not expert) (org-fit-window-to-buffer))
  12384. (setq rtn
  12385. (catch 'exit
  12386. (while t
  12387. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12388. (if (not groups) "no " "")
  12389. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12390. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12391. (setq org-last-tag-selection-key c)
  12392. (cond
  12393. ((= c ?\r) (throw 'exit t))
  12394. ((= c ?!)
  12395. (setq groups (not groups))
  12396. (goto-char (point-min))
  12397. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12398. ((= c ?\C-c)
  12399. (if (not expert)
  12400. (org-fast-tag-show-exit
  12401. (setq exit-after-next (not exit-after-next)))
  12402. (setq expert nil)
  12403. (delete-other-windows)
  12404. (set-window-buffer (split-window-vertically) " *Org tags*")
  12405. (org-switch-to-buffer-other-window " *Org tags*")
  12406. (org-fit-window-to-buffer)))
  12407. ((or (= c ?\C-g)
  12408. (and (= c ?q) (not (rassoc c ntable))))
  12409. (org-detach-overlay org-tags-overlay)
  12410. (setq quit-flag t))
  12411. ((= c ?\ )
  12412. (setq current nil)
  12413. (if exit-after-next (setq exit-after-next 'now)))
  12414. ((= c ?\t)
  12415. (condition-case nil
  12416. (setq tg (org-icompleting-read
  12417. "Tag: "
  12418. (or buffer-tags
  12419. (with-current-buffer buf
  12420. (org-get-buffer-tags)))))
  12421. (quit (setq tg "")))
  12422. (when (string-match "\\S-" tg)
  12423. (add-to-list 'buffer-tags (list tg))
  12424. (if (member tg current)
  12425. (setq current (delete tg current))
  12426. (push tg current)))
  12427. (if exit-after-next (setq exit-after-next 'now)))
  12428. ((setq e (rassoc c todo-table) tg (car e))
  12429. (with-current-buffer buf
  12430. (save-excursion (org-todo tg)))
  12431. (if exit-after-next (setq exit-after-next 'now)))
  12432. ((setq e (rassoc c ntable) tg (car e))
  12433. (if (member tg current)
  12434. (setq current (delete tg current))
  12435. (loop for g in groups do
  12436. (if (member tg g)
  12437. (mapc (lambda (x)
  12438. (setq current (delete x current)))
  12439. g)))
  12440. (push tg current))
  12441. (if exit-after-next (setq exit-after-next 'now))))
  12442. ;; Create a sorted list
  12443. (setq current
  12444. (sort current
  12445. (lambda (a b)
  12446. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12447. (if (eq exit-after-next 'now) (throw 'exit t))
  12448. (goto-char (point-min))
  12449. (beginning-of-line 2)
  12450. (delete-region (point) (point-at-eol))
  12451. (org-fast-tag-insert "Current" current c-face)
  12452. (org-set-current-tags-overlay current ov-prefix)
  12453. (while (re-search-forward
  12454. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12455. (setq tg (match-string 1))
  12456. (add-text-properties
  12457. (match-beginning 1) (match-end 1)
  12458. (list 'face
  12459. (cond
  12460. ((member tg current) c-face)
  12461. ((member tg inherited) i-face)
  12462. (t (get-text-property (match-beginning 1) 'face))))))
  12463. (goto-char (point-min)))))
  12464. (org-detach-overlay org-tags-overlay)
  12465. (if rtn
  12466. (mapconcat 'identity current ":")
  12467. nil))))
  12468. (defun org-get-tags-string ()
  12469. "Get the TAGS string in the current headline."
  12470. (unless (org-on-heading-p t)
  12471. (error "Not on a heading"))
  12472. (save-excursion
  12473. (beginning-of-line 1)
  12474. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12475. (org-match-string-no-properties 1)
  12476. "")))
  12477. (defun org-get-tags ()
  12478. "Get the list of tags specified in the current headline."
  12479. (org-split-string (org-get-tags-string) ":"))
  12480. (defun org-get-buffer-tags ()
  12481. "Get a table of all tags used in the buffer, for completion."
  12482. (let (tags)
  12483. (save-excursion
  12484. (goto-char (point-min))
  12485. (while (re-search-forward
  12486. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12487. (when (equal (char-after (point-at-bol 0)) ?*)
  12488. (mapc (lambda (x) (add-to-list 'tags x))
  12489. (org-split-string (org-match-string-no-properties 1) ":")))))
  12490. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12491. (mapcar 'list tags)))
  12492. ;;;; The mapping API
  12493. ;;;###autoload
  12494. (defun org-map-entries (func &optional match scope &rest skip)
  12495. "Call FUNC at each headline selected by MATCH in SCOPE.
  12496. FUNC is a function or a lisp form. The function will be called without
  12497. arguments, with the cursor positioned at the beginning of the headline.
  12498. The return values of all calls to the function will be collected and
  12499. returned as a list.
  12500. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12501. does not need to preserve point. After evaluation, the cursor will be
  12502. moved to the end of the line (presumably of the headline of the
  12503. processed entry) and search continues from there. Under some
  12504. circumstances, this may not produce the wanted results. For example,
  12505. if you have removed (e.g. archived) the current (sub)tree it could
  12506. mean that the next entry will be skipped entirely. In such cases, you
  12507. can specify the position from where search should continue by making
  12508. FUNC set the variable `org-map-continue-from' to the desired buffer
  12509. position.
  12510. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12511. Only headlines that are matched by this query will be considered during
  12512. the iteration. When MATCH is nil or t, all headlines will be
  12513. visited by the iteration.
  12514. SCOPE determines the scope of this command. It can be any of:
  12515. nil The current buffer, respecting the restriction if any
  12516. tree The subtree started with the entry at point
  12517. region The entries within the active region, if any
  12518. file The current buffer, without restriction
  12519. file-with-archives
  12520. The current buffer, and any archives associated with it
  12521. agenda All agenda files
  12522. agenda-with-archives
  12523. All agenda files with any archive files associated with them
  12524. \(file1 file2 ...)
  12525. If this is a list, all files in the list will be scanned
  12526. The remaining args are treated as settings for the skipping facilities of
  12527. the scanner. The following items can be given here:
  12528. archive skip trees with the archive tag.
  12529. comment skip trees with the COMMENT keyword
  12530. function or Emacs Lisp form:
  12531. will be used as value for `org-agenda-skip-function', so whenever
  12532. the function returns t, FUNC will not be called for that
  12533. entry and search will continue from the point where the
  12534. function leaves it.
  12535. If your function needs to retrieve the tags including inherited tags
  12536. at the *current* entry, you can use the value of the variable
  12537. `org-scanner-tags' which will be much faster than getting the value
  12538. with `org-get-tags-at'. If your function gets properties with
  12539. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12540. to t around the call to `org-entry-properties' to get the same speedup.
  12541. Note that if your function moves around to retrieve tags and properties at
  12542. a *different* entry, you cannot use these techniques."
  12543. (unless (and (eq scope 'region) (not (org-region-active-p)))
  12544. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12545. (org-agenda-skip-archived-trees (memq 'archive skip))
  12546. (org-agenda-skip-comment-trees (memq 'comment skip))
  12547. (org-agenda-skip-function
  12548. (car (org-delete-all '(comment archive) skip)))
  12549. (org-tags-match-list-sublevels t)
  12550. matcher file res
  12551. org-todo-keywords-for-agenda
  12552. org-done-keywords-for-agenda
  12553. org-todo-keyword-alist-for-agenda
  12554. org-drawers-for-agenda
  12555. org-tag-alist-for-agenda)
  12556. (cond
  12557. ((eq match t) (setq matcher t))
  12558. ((eq match nil) (setq matcher t))
  12559. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12560. (save-excursion
  12561. (save-restriction
  12562. (cond ((eq scope 'tree)
  12563. (org-back-to-heading t)
  12564. (org-narrow-to-subtree)
  12565. (setq scope nil))
  12566. ((and (eq scope 'region) (org-region-active-p))
  12567. (narrow-to-region (region-beginning)
  12568. (save-excursion
  12569. (goto-char (region-end))
  12570. (unless (and (bolp) (org-at-heading-p))
  12571. (outline-next-heading))
  12572. (point)))
  12573. (setq scope nil)))
  12574. (if (not scope)
  12575. (progn
  12576. (org-prepare-agenda-buffers
  12577. (list (buffer-file-name (current-buffer))))
  12578. (setq res (org-scan-tags func matcher)))
  12579. ;; Get the right scope
  12580. (cond
  12581. ((and scope (listp scope) (symbolp (car scope)))
  12582. (setq scope (eval scope)))
  12583. ((eq scope 'agenda)
  12584. (setq scope (org-agenda-files t)))
  12585. ((eq scope 'agenda-with-archives)
  12586. (setq scope (org-agenda-files t))
  12587. (setq scope (org-add-archive-files scope)))
  12588. ((eq scope 'file)
  12589. (setq scope (list (buffer-file-name))))
  12590. ((eq scope 'file-with-archives)
  12591. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12592. (org-prepare-agenda-buffers scope)
  12593. (while (setq file (pop scope))
  12594. (with-current-buffer (org-find-base-buffer-visiting file)
  12595. (save-excursion
  12596. (save-restriction
  12597. (widen)
  12598. (goto-char (point-min))
  12599. (setq res (append res (org-scan-tags func matcher))))))))))
  12600. res)))
  12601. ;;;; Properties
  12602. ;;; Setting and retrieving properties
  12603. (defconst org-special-properties
  12604. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12605. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
  12606. "The special properties valid in Org-mode.
  12607. These are properties that are not defined in the property drawer,
  12608. but in some other way.")
  12609. (defconst org-default-properties
  12610. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12611. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12612. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12613. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12614. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12615. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12616. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12617. "Some properties that are used by Org-mode for various purposes.
  12618. Being in this list makes sure that they are offered for completion.")
  12619. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12620. "Regular expression matching the first line of a property drawer.")
  12621. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12622. "Regular expression matching the last line of a property drawer.")
  12623. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12624. "Regular expression matching the first line of a property drawer.")
  12625. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12626. "Regular expression matching the first line of a property drawer.")
  12627. (defconst org-property-drawer-re
  12628. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12629. org-property-end-re "\\)\n?")
  12630. "Matches an entire property drawer.")
  12631. (defconst org-clock-drawer-re
  12632. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12633. org-property-end-re "\\)\n?")
  12634. "Matches an entire clock drawer.")
  12635. (defsubst org-re-property (property)
  12636. "Return a regexp matching PROPERTY.
  12637. Match group 1 will be set to the value "
  12638. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12639. (defun org-property-action ()
  12640. "Do an action on properties."
  12641. (interactive)
  12642. (let (c)
  12643. (org-at-property-p)
  12644. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12645. (setq c (read-char-exclusive))
  12646. (cond
  12647. ((equal c ?s)
  12648. (call-interactively 'org-set-property))
  12649. ((equal c ?d)
  12650. (call-interactively 'org-delete-property))
  12651. ((equal c ?D)
  12652. (call-interactively 'org-delete-property-globally))
  12653. ((equal c ?c)
  12654. (call-interactively 'org-compute-property-at-point))
  12655. (t (error "No such property action %c" c)))))
  12656. (defun org-set-effort (&optional value)
  12657. "Set the effort property of the current entry.
  12658. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12659. allowed value."
  12660. (interactive "P")
  12661. (if (equal value 0) (setq value 10))
  12662. (let* ((completion-ignore-case t)
  12663. (prop org-effort-property)
  12664. (cur (org-entry-get nil prop))
  12665. (allowed (org-property-get-allowed-values nil prop 'table))
  12666. (existing (mapcar 'list (org-property-values prop)))
  12667. rpl
  12668. (val (cond
  12669. ((stringp value) value)
  12670. ((and allowed (integerp value))
  12671. (or (car (nth (1- value) allowed))
  12672. (car (org-last allowed))))
  12673. (allowed
  12674. (message "Select 1-9,0, [RET%s]: %s"
  12675. (if cur (concat "=" cur) "")
  12676. (mapconcat 'car allowed " "))
  12677. (setq rpl (read-char-exclusive))
  12678. (if (equal rpl ?\r)
  12679. cur
  12680. (setq rpl (- rpl ?0))
  12681. (if (equal rpl 0) (setq rpl 10))
  12682. (if (and (> rpl 0) (<= rpl (length allowed)))
  12683. (car (nth (1- rpl) allowed))
  12684. (org-completing-read "Effort: " allowed nil))))
  12685. (t
  12686. (let (org-completion-use-ido org-completion-use-iswitchb)
  12687. (org-completing-read
  12688. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12689. (concat "[" cur "]") "")
  12690. ": ")
  12691. existing nil nil "" nil cur))))))
  12692. (unless (equal (org-entry-get nil prop) val)
  12693. (org-entry-put nil prop val))
  12694. (message "%s is now %s" prop val)))
  12695. (defun org-at-property-p ()
  12696. "Is cursor inside a property drawer?"
  12697. (save-excursion
  12698. (beginning-of-line 1)
  12699. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12700. (save-match-data ;; Used by calling procedures
  12701. (let ((p (point))
  12702. (range (unless (org-before-first-heading-p)
  12703. (org-get-property-block))))
  12704. (and range (<= (car range) p) (< p (cdr range))))))))
  12705. (defun org-get-property-block (&optional beg end force)
  12706. "Return the (beg . end) range of the body of the property drawer.
  12707. BEG and END can be beginning and end of subtree, if not given
  12708. they will be found.
  12709. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12710. (catch 'exit
  12711. (save-excursion
  12712. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12713. (end (or end (progn (outline-next-heading) (point)))))
  12714. (goto-char beg)
  12715. (if (re-search-forward org-property-start-re end t)
  12716. (setq beg (1+ (match-end 0)))
  12717. (if force
  12718. (save-excursion
  12719. (org-insert-property-drawer)
  12720. (setq end (progn (outline-next-heading) (point))))
  12721. (throw 'exit nil))
  12722. (goto-char beg)
  12723. (if (re-search-forward org-property-start-re end t)
  12724. (setq beg (1+ (match-end 0)))))
  12725. (if (re-search-forward org-property-end-re end t)
  12726. (setq end (match-beginning 0))
  12727. (or force (throw 'exit nil))
  12728. (goto-char beg)
  12729. (setq end beg)
  12730. (org-indent-line-function)
  12731. (insert ":END:\n"))
  12732. (cons beg end)))))
  12733. (defun org-entry-properties (&optional pom which specific)
  12734. "Get all properties of the entry at point-or-marker POM.
  12735. This includes the TODO keyword, the tags, time strings for deadline,
  12736. scheduled, and clocking, and any additional properties defined in the
  12737. entry. The return value is an alist, keys may occur multiple times
  12738. if the property key was used several times.
  12739. POM may also be nil, in which case the current entry is used.
  12740. If WHICH is nil or `all', get all properties. If WHICH is
  12741. `special' or `standard', only get that subclass. If WHICH
  12742. is a string only get exactly this property. SPECIFIC can be a string, the
  12743. specific property we are interested in. Specifying it can speed
  12744. things up because then unnecessary parsing is avoided."
  12745. (setq which (or which 'all))
  12746. (org-with-point-at pom
  12747. (let ((clockstr (substring org-clock-string 0 -1))
  12748. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12749. (case-fold-search nil)
  12750. beg end range props sum-props key key1 value string clocksum)
  12751. (save-excursion
  12752. (when (condition-case nil
  12753. (and (eq major-mode 'org-mode) (org-back-to-heading t))
  12754. (error nil))
  12755. (setq beg (point))
  12756. (setq sum-props (get-text-property (point) 'org-summaries))
  12757. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12758. (outline-next-heading)
  12759. (setq end (point))
  12760. (when (memq which '(all special))
  12761. ;; Get the special properties, like TODO and tags
  12762. (goto-char beg)
  12763. (when (and (or (not specific) (string= specific "TODO"))
  12764. (looking-at org-todo-line-regexp) (match-end 2))
  12765. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12766. (when (and (or (not specific) (string= specific "PRIORITY"))
  12767. (looking-at org-priority-regexp))
  12768. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12769. (when (or (not specific) (string= specific "FILE"))
  12770. (push (cons "FILE" buffer-file-name) props))
  12771. (when (and (or (not specific) (string= specific "TAGS"))
  12772. (setq value (org-get-tags-string))
  12773. (string-match "\\S-" value))
  12774. (push (cons "TAGS" value) props))
  12775. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12776. (setq value (org-get-tags-at)))
  12777. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12778. ":"))
  12779. props))
  12780. (when (or (not specific) (string= specific "BLOCKED"))
  12781. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12782. (when (or (not specific)
  12783. (member specific
  12784. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12785. "TIMESTAMP" "TIMESTAMP_IA")))
  12786. (catch 'match
  12787. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12788. (setq key (if (match-end 1)
  12789. (substring (org-match-string-no-properties 1)
  12790. 0 -1))
  12791. string (if (equal key clockstr)
  12792. (org-no-properties
  12793. (org-trim
  12794. (buffer-substring
  12795. (match-beginning 3) (goto-char
  12796. (point-at-eol)))))
  12797. (substring (org-match-string-no-properties 3)
  12798. 1 -1)))
  12799. ;; Get the correct property name from the key. This is
  12800. ;; necessary if the user has configured time keywords.
  12801. (setq key1 (concat key ":"))
  12802. (cond
  12803. ((not key)
  12804. (setq key
  12805. (if (= (char-after (match-beginning 3)) ?\[)
  12806. "TIMESTAMP_IA" "TIMESTAMP")))
  12807. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12808. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12809. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12810. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12811. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  12812. (progn
  12813. (push (cons key string) props)
  12814. ;; no need to search further if match is found
  12815. (throw 'match t))
  12816. (when (or (equal key "CLOCK") (not (assoc key props)))
  12817. (push (cons key string) props))))))
  12818. )
  12819. (when (memq which '(all standard))
  12820. ;; Get the standard properties, like :PROP: ...
  12821. (setq range (org-get-property-block beg end))
  12822. (when range
  12823. (goto-char (car range))
  12824. (while (re-search-forward
  12825. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12826. (cdr range) t)
  12827. (setq key (org-match-string-no-properties 1)
  12828. value (org-trim (or (org-match-string-no-properties 2) "")))
  12829. (unless (member key excluded)
  12830. (push (cons key (or value "")) props)))))
  12831. (if clocksum
  12832. (push (cons "CLOCKSUM"
  12833. (org-columns-number-to-string (/ (float clocksum) 60.)
  12834. 'add_times))
  12835. props))
  12836. (unless (assoc "CATEGORY" props)
  12837. (push (cons "CATEGORY" (org-get-category)) props))
  12838. (append sum-props (nreverse props)))))))
  12839. (defun org-entry-get (pom property &optional inherit literal-nil)
  12840. "Get value of PROPERTY for entry at point-or-marker POM.
  12841. If INHERIT is non-nil and the entry does not have the property,
  12842. then also check higher levels of the hierarchy.
  12843. If INHERIT is the symbol `selective', use inheritance only if the setting
  12844. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12845. If the property is present but empty, the return value is the empty string.
  12846. If the property is not present at all, nil is returned.
  12847. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12848. do not interpret it as the list atom nil. This is used for inheritance
  12849. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12850. (org-with-point-at pom
  12851. (if (and inherit (if (eq inherit 'selective)
  12852. (org-property-inherit-p property)
  12853. t))
  12854. (org-entry-get-with-inheritance property literal-nil)
  12855. (if (member property org-special-properties)
  12856. ;; We need a special property. Use `org-entry-properties' to
  12857. ;; retrieve it, but specify the wanted property
  12858. (cdr (assoc property (org-entry-properties nil 'special property)))
  12859. (let ((range (unless (org-before-first-heading-p)
  12860. (org-get-property-block))))
  12861. (when (and range (goto-char (car range)))
  12862. ((lambda (val) (when val (if literal-nil val (org-not-nil val))))
  12863. (cond
  12864. ((re-search-forward
  12865. (org-re-property property) (cdr range) t)
  12866. (if (match-end 1) (org-match-string-no-properties 1) ""))
  12867. ((re-search-forward
  12868. (org-re-property (concat property "+")) (cdr range) t)
  12869. (cdr (assoc
  12870. property
  12871. (org-update-property-plist
  12872. (concat property "+")
  12873. (if (match-end 1) (org-match-string-no-properties 1) "")
  12874. (list (or (assoc property org-file-properties)
  12875. (assoc property org-global-properties)
  12876. (assoc property org-global-properties-fixed)
  12877. ))))))))))))))
  12878. (defun org-property-or-variable-value (var &optional inherit)
  12879. "Check if there is a property fixing the value of VAR.
  12880. If yes, return this value. If not, return the current value of the variable."
  12881. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12882. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12883. (read prop)
  12884. (symbol-value var))))
  12885. (defun org-entry-delete (pom property)
  12886. "Delete the property PROPERTY from entry at point-or-marker POM."
  12887. (org-with-point-at pom
  12888. (if (member property org-special-properties)
  12889. nil ; cannot delete these properties.
  12890. (let ((range (org-get-property-block)))
  12891. (if (and range
  12892. (goto-char (car range))
  12893. (re-search-forward
  12894. (org-re-property property)
  12895. (cdr range) t))
  12896. (progn
  12897. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12898. t)
  12899. nil)))))
  12900. ;; Multi-values properties are properties that contain multiple values
  12901. ;; These values are assumed to be single words, separated by whitespace.
  12902. (defun org-entry-add-to-multivalued-property (pom property value)
  12903. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  12904. (let* ((old (org-entry-get pom property))
  12905. (values (and old (org-split-string old "[ \t]"))))
  12906. (setq value (org-entry-protect-space value))
  12907. (unless (member value values)
  12908. (setq values (cons value values))
  12909. (org-entry-put pom property
  12910. (mapconcat 'identity values " ")))))
  12911. (defun org-entry-remove-from-multivalued-property (pom property value)
  12912. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  12913. (let* ((old (org-entry-get pom property))
  12914. (values (and old (org-split-string old "[ \t]"))))
  12915. (setq value (org-entry-protect-space value))
  12916. (when (member value values)
  12917. (setq values (delete value values))
  12918. (org-entry-put pom property
  12919. (mapconcat 'identity values " ")))))
  12920. (defun org-entry-member-in-multivalued-property (pom property value)
  12921. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  12922. (let* ((old (org-entry-get pom property))
  12923. (values (and old (org-split-string old "[ \t]"))))
  12924. (setq value (org-entry-protect-space value))
  12925. (member value values)))
  12926. (defun org-entry-get-multivalued-property (pom property)
  12927. "Return a list of values in a multivalued property."
  12928. (let* ((value (org-entry-get pom property))
  12929. (values (and value (org-split-string value "[ \t]"))))
  12930. (mapcar 'org-entry-restore-space values)))
  12931. (defun org-entry-put-multivalued-property (pom property &rest values)
  12932. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  12933. VALUES should be a list of strings. Spaces will be protected."
  12934. (org-entry-put pom property
  12935. (mapconcat 'org-entry-protect-space values " "))
  12936. (let* ((value (org-entry-get pom property))
  12937. (values (and value (org-split-string value "[ \t]"))))
  12938. (mapcar 'org-entry-restore-space values)))
  12939. (defun org-entry-protect-space (s)
  12940. "Protect spaces and newline in string S."
  12941. (while (string-match " " s)
  12942. (setq s (replace-match "%20" t t s)))
  12943. (while (string-match "\n" s)
  12944. (setq s (replace-match "%0A" t t s)))
  12945. s)
  12946. (defun org-entry-restore-space (s)
  12947. "Restore spaces and newline in string S."
  12948. (while (string-match "%20" s)
  12949. (setq s (replace-match " " t t s)))
  12950. (while (string-match "%0A" s)
  12951. (setq s (replace-match "\n" t t s)))
  12952. s)
  12953. (defvar org-entry-property-inherited-from (make-marker)
  12954. "Marker pointing to the entry from where a property was inherited.
  12955. Each call to `org-entry-get-with-inheritance' will set this marker to the
  12956. location of the entry where the inheritance search matched. If there was
  12957. no match, the marker will point nowhere.
  12958. Note that also `org-entry-get' calls this function, if the INHERIT flag
  12959. is set.")
  12960. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  12961. "Get entry property, and search higher levels if not present.
  12962. The search will stop at the first ancestor which has the property defined.
  12963. If the value found is \"nil\", return nil to show that the property
  12964. should be considered as undefined (this is the meaning of nil here).
  12965. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  12966. (move-marker org-entry-property-inherited-from nil)
  12967. (let (tmp)
  12968. (unless (org-before-first-heading-p)
  12969. (save-excursion
  12970. (save-restriction
  12971. (widen)
  12972. (catch 'ex
  12973. (while t
  12974. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  12975. (org-back-to-heading t)
  12976. (move-marker org-entry-property-inherited-from (point))
  12977. (throw 'ex tmp))
  12978. (or (org-up-heading-safe) (throw 'ex nil)))))))
  12979. (setq tmp (or tmp
  12980. (cdr (assoc property org-file-properties))
  12981. (cdr (assoc property org-global-properties))
  12982. (cdr (assoc property org-global-properties-fixed))))
  12983. (if literal-nil tmp (org-not-nil tmp))))
  12984. (defvar org-property-changed-functions nil
  12985. "Hook called when the value of a property has changed.
  12986. Each hook function should accept two arguments, the name of the property
  12987. and the new value.")
  12988. (defun org-entry-put (pom property value)
  12989. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  12990. (org-with-point-at pom
  12991. (org-back-to-heading t)
  12992. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  12993. range)
  12994. (cond
  12995. ((equal property "TODO")
  12996. (when (and (stringp value) (string-match "\\S-" value)
  12997. (not (member value org-todo-keywords-1)))
  12998. (error "\"%s\" is not a valid TODO state" value))
  12999. (if (or (not value)
  13000. (not (string-match "\\S-" value)))
  13001. (setq value 'none))
  13002. (org-todo value)
  13003. (org-set-tags nil 'align))
  13004. ((equal property "PRIORITY")
  13005. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13006. (string-to-char value) ?\ ))
  13007. (org-set-tags nil 'align))
  13008. ((equal property "SCHEDULED")
  13009. (if (re-search-forward org-scheduled-time-regexp end t)
  13010. (cond
  13011. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13012. ((eq value 'later) (org-timestamp-change 1 'day))
  13013. (t (call-interactively 'org-schedule)))
  13014. (call-interactively 'org-schedule)))
  13015. ((equal property "DEADLINE")
  13016. (if (re-search-forward org-deadline-time-regexp end t)
  13017. (cond
  13018. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13019. ((eq value 'later) (org-timestamp-change 1 'day))
  13020. (t (call-interactively 'org-deadline)))
  13021. (call-interactively 'org-deadline)))
  13022. ((member property org-special-properties)
  13023. (error "The %s property can not yet be set with `org-entry-put'"
  13024. property))
  13025. (t ; a non-special property
  13026. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13027. (setq range (org-get-property-block beg end 'force))
  13028. (goto-char (car range))
  13029. (if (re-search-forward
  13030. (org-re-property property) (cdr range) t)
  13031. (progn
  13032. (delete-region (match-beginning 0) (match-end 0))
  13033. (goto-char (match-beginning 0)))
  13034. (goto-char (cdr range))
  13035. (insert "\n")
  13036. (backward-char 1)
  13037. (org-indent-line-function))
  13038. (insert ":" property ":")
  13039. (and value (insert " " value))
  13040. (org-indent-line-function)))))
  13041. (run-hook-with-args 'org-property-changed-functions property value)))
  13042. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13043. "Get all property keys in the current buffer.
  13044. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13045. like tags and TODO state.
  13046. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13047. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13048. and others.
  13049. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13050. formats in the current buffer."
  13051. (let (rtn range cfmt s p)
  13052. (save-excursion
  13053. (save-restriction
  13054. (widen)
  13055. (goto-char (point-min))
  13056. (while (re-search-forward org-property-start-re nil t)
  13057. (setq range (org-get-property-block))
  13058. (goto-char (car range))
  13059. (while (re-search-forward
  13060. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13061. (cdr range) t)
  13062. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13063. (outline-next-heading))))
  13064. (when include-specials
  13065. (setq rtn (append org-special-properties rtn)))
  13066. (when include-defaults
  13067. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13068. (add-to-list 'rtn org-effort-property))
  13069. (when include-columns
  13070. (save-excursion
  13071. (save-restriction
  13072. (widen)
  13073. (goto-char (point-min))
  13074. (while (re-search-forward
  13075. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13076. nil t)
  13077. (setq cfmt (match-string 2) s 0)
  13078. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13079. cfmt s)
  13080. (setq s (match-end 0)
  13081. p (match-string 1 cfmt))
  13082. (unless (or (equal p "ITEM")
  13083. (member p org-special-properties))
  13084. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13085. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13086. (defun org-property-values (key)
  13087. "Return a list of all values of property KEY in the current buffer."
  13088. (save-excursion
  13089. (save-restriction
  13090. (widen)
  13091. (goto-char (point-min))
  13092. (let ((re (org-re-property key))
  13093. values)
  13094. (while (re-search-forward re nil t)
  13095. (add-to-list 'values (org-trim (match-string 1))))
  13096. (delete "" values)))))
  13097. (defun org-insert-property-drawer ()
  13098. "Insert a property drawer into the current entry."
  13099. (interactive)
  13100. (org-back-to-heading t)
  13101. (looking-at org-outline-regexp)
  13102. (let ((indent (if org-adapt-indentation
  13103. (- (match-end 0)(match-beginning 0))
  13104. 0))
  13105. (beg (point))
  13106. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13107. end hiddenp)
  13108. (outline-next-heading)
  13109. (setq end (point))
  13110. (goto-char beg)
  13111. (while (re-search-forward re end t))
  13112. (setq hiddenp (outline-invisible-p))
  13113. (end-of-line 1)
  13114. (and (equal (char-after) ?\n) (forward-char 1))
  13115. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13116. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13117. ;; just skip this line
  13118. (beginning-of-line 2)
  13119. ;; Drawer start, find the end
  13120. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13121. (beginning-of-line 1)))
  13122. (org-skip-over-state-notes)
  13123. (skip-chars-backward " \t\n\r")
  13124. (if (eq (char-before) ?*) (forward-char 1))
  13125. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13126. (beginning-of-line 0)
  13127. (org-indent-to-column indent)
  13128. (beginning-of-line 2)
  13129. (org-indent-to-column indent)
  13130. (beginning-of-line 0)
  13131. (if hiddenp
  13132. (save-excursion
  13133. (org-back-to-heading t)
  13134. (hide-entry))
  13135. (org-flag-drawer t))))
  13136. (defvar org-property-set-functions-alist nil
  13137. "Property set function alist.
  13138. Each entry should have the following format:
  13139. (PROPERTY . READ-FUNCTION)
  13140. The read function will be called with the same argument as
  13141. `org-completing-read'.")
  13142. (defun org-set-property-function (property)
  13143. "Get the function that should be used to set PROPERTY.
  13144. This is computed according to `org-property-set-functions-alist'."
  13145. (or (cdr (assoc property org-property-set-functions-alist))
  13146. 'org-completing-read))
  13147. (defun org-read-property-value (property)
  13148. "Read PROPERTY value from user."
  13149. (let* ((completion-ignore-case t)
  13150. (allowed (org-property-get-allowed-values nil property 'table))
  13151. (cur (org-entry-get nil property))
  13152. (prompt (concat property " value"
  13153. (if (and cur (string-match "\\S-" cur))
  13154. (concat " [" cur "]") "") ": "))
  13155. (set-function (org-set-property-function property))
  13156. (val (if allowed
  13157. (funcall set-function prompt allowed nil
  13158. (not (get-text-property 0 'org-unrestricted
  13159. (caar allowed))))
  13160. (let (org-completion-use-ido org-completion-use-iswitchb)
  13161. (funcall set-function prompt
  13162. (mapcar 'list (org-property-values property))
  13163. nil nil "" nil cur)))))
  13164. (if (equal val "")
  13165. cur
  13166. val)))
  13167. (defvar org-last-set-property nil)
  13168. (defun org-read-property-name ()
  13169. "Read a property name."
  13170. (let* ((completion-ignore-case t)
  13171. (keys (org-buffer-property-keys nil t t))
  13172. (default-prop (or (save-excursion
  13173. (save-match-data
  13174. (beginning-of-line)
  13175. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13176. (null (string= (match-string 1) "END"))
  13177. (match-string 1))))
  13178. org-last-set-property))
  13179. (property (org-icompleting-read
  13180. (concat "Property"
  13181. (if default-prop (concat " [" default-prop "]") "")
  13182. ": ")
  13183. (mapcar 'list keys)
  13184. nil nil nil nil
  13185. default-prop
  13186. )))
  13187. (if (member property keys)
  13188. property
  13189. (or (cdr (assoc (downcase property)
  13190. (mapcar (lambda (x) (cons (downcase x) x))
  13191. keys)))
  13192. property))))
  13193. (defun org-set-property (property value)
  13194. "In the current entry, set PROPERTY to VALUE.
  13195. When called interactively, this will prompt for a property name, offering
  13196. completion on existing and default properties. And then it will prompt
  13197. for a value, offering completion either on allowed values (via an inherited
  13198. xxx_ALL property) or on existing values in other instances of this property
  13199. in the current file."
  13200. (interactive (list nil nil))
  13201. (let* ((property (or property (org-read-property-name)))
  13202. (value (or value (org-read-property-value property)))
  13203. (fn (assoc property org-properties-postprocess-alist)))
  13204. (setq org-last-set-property property)
  13205. ;; Possibly postprocess the inserted value:
  13206. (when fn (setq value (funcall (cadr fn) value)))
  13207. (unless (equal (org-entry-get nil property) value)
  13208. (org-entry-put nil property value))))
  13209. (defun org-delete-property (property)
  13210. "In the current entry, delete PROPERTY."
  13211. (interactive
  13212. (let* ((completion-ignore-case t)
  13213. (prop (org-icompleting-read "Property: "
  13214. (org-entry-properties nil 'standard))))
  13215. (list prop)))
  13216. (message "Property %s %s" property
  13217. (if (org-entry-delete nil property)
  13218. "deleted"
  13219. "was not present in the entry")))
  13220. (defun org-delete-property-globally (property)
  13221. "Remove PROPERTY globally, from all entries."
  13222. (interactive
  13223. (let* ((completion-ignore-case t)
  13224. (prop (org-icompleting-read
  13225. "Globally remove property: "
  13226. (mapcar 'list (org-buffer-property-keys)))))
  13227. (list prop)))
  13228. (save-excursion
  13229. (save-restriction
  13230. (widen)
  13231. (goto-char (point-min))
  13232. (let ((cnt 0))
  13233. (while (re-search-forward
  13234. (org-re-property property)
  13235. nil t)
  13236. (setq cnt (1+ cnt))
  13237. (replace-match ""))
  13238. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13239. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13240. (defun org-compute-property-at-point ()
  13241. "Compute the property at point.
  13242. This looks for an enclosing column format, extracts the operator and
  13243. then applies it to the property in the column format's scope."
  13244. (interactive)
  13245. (unless (org-at-property-p)
  13246. (error "Not at a property"))
  13247. (let ((prop (org-match-string-no-properties 2)))
  13248. (org-columns-get-format-and-top-level)
  13249. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13250. (error "No operator defined for property %s" prop))
  13251. (org-columns-compute prop)))
  13252. (defvar org-property-allowed-value-functions nil
  13253. "Hook for functions supplying allowed values for a specific property.
  13254. The functions must take a single argument, the name of the property, and
  13255. return a flat list of allowed values. If \":ETC\" is one of
  13256. the values, this means that these values are intended as defaults for
  13257. completion, but that other values should be allowed too.
  13258. The functions must return nil if they are not responsible for this
  13259. property.")
  13260. (defun org-property-get-allowed-values (pom property &optional table)
  13261. "Get allowed values for the property PROPERTY.
  13262. When TABLE is non-nil, return an alist that can directly be used for
  13263. completion."
  13264. (let (vals)
  13265. (cond
  13266. ((equal property "TODO")
  13267. (setq vals (org-with-point-at pom
  13268. (append org-todo-keywords-1 '("")))))
  13269. ((equal property "PRIORITY")
  13270. (let ((n org-lowest-priority))
  13271. (while (>= n org-highest-priority)
  13272. (push (char-to-string n) vals)
  13273. (setq n (1- n)))))
  13274. ((member property org-special-properties))
  13275. ((setq vals (run-hook-with-args-until-success
  13276. 'org-property-allowed-value-functions property)))
  13277. (t
  13278. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13279. (when (and vals (string-match "\\S-" vals))
  13280. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13281. (setq vals (mapcar (lambda (x)
  13282. (cond ((stringp x) x)
  13283. ((numberp x) (number-to-string x))
  13284. ((symbolp x) (symbol-name x))
  13285. (t "???")))
  13286. vals)))))
  13287. (when (member ":ETC" vals)
  13288. (setq vals (remove ":ETC" vals))
  13289. (org-add-props (car vals) '(org-unrestricted t)))
  13290. (if table (mapcar 'list vals) vals)))
  13291. (defun org-property-previous-allowed-value (&optional previous)
  13292. "Switch to the next allowed value for this property."
  13293. (interactive)
  13294. (org-property-next-allowed-value t))
  13295. (defun org-property-next-allowed-value (&optional previous)
  13296. "Switch to the next allowed value for this property."
  13297. (interactive)
  13298. (unless (org-at-property-p)
  13299. (error "Not at a property"))
  13300. (let* ((key (match-string 2))
  13301. (value (match-string 3))
  13302. (allowed (or (org-property-get-allowed-values (point) key)
  13303. (and (member value '("[ ]" "[-]" "[X]"))
  13304. '("[ ]" "[X]"))))
  13305. nval)
  13306. (unless allowed
  13307. (error "Allowed values for this property have not been defined"))
  13308. (if previous (setq allowed (reverse allowed)))
  13309. (if (member value allowed)
  13310. (setq nval (car (cdr (member value allowed)))))
  13311. (setq nval (or nval (car allowed)))
  13312. (if (equal nval value)
  13313. (error "Only one allowed value for this property"))
  13314. (org-at-property-p)
  13315. (replace-match (concat " :" key ": " nval) t t)
  13316. (org-indent-line-function)
  13317. (beginning-of-line 1)
  13318. (skip-chars-forward " \t")
  13319. (run-hook-with-args 'org-property-changed-functions key nval)))
  13320. (defun org-find-olp (path &optional this-buffer)
  13321. "Return a marker pointing to the entry at outline path OLP.
  13322. If anything goes wrong, throw an error.
  13323. You can wrap this call to catch the error like this:
  13324. (condition-case msg
  13325. (org-mobile-locate-entry (match-string 4))
  13326. (error (nth 1 msg)))
  13327. The return value will then be either a string with the error message,
  13328. or a marker if everything is OK.
  13329. If THIS-BUFFER is set, the outline path does not contain a file,
  13330. only headings."
  13331. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13332. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13333. (level 1)
  13334. (lmin 1)
  13335. (lmax 1)
  13336. limit re end found pos heading cnt flevel)
  13337. (unless buffer (error "File not found :%s" file))
  13338. (with-current-buffer buffer
  13339. (save-excursion
  13340. (save-restriction
  13341. (widen)
  13342. (setq limit (point-max))
  13343. (goto-char (point-min))
  13344. (while (setq heading (pop path))
  13345. (setq re (format org-complex-heading-regexp-format
  13346. (regexp-quote heading)))
  13347. (setq cnt 0 pos (point))
  13348. (while (re-search-forward re end t)
  13349. (setq level (- (match-end 1) (match-beginning 1)))
  13350. (if (and (>= level lmin) (<= level lmax))
  13351. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13352. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13353. lmax heading))
  13354. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13355. lmax heading))
  13356. (goto-char found)
  13357. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13358. (setq end (save-excursion (org-end-of-subtree t t))))
  13359. (when (org-on-heading-p)
  13360. (move-marker (make-marker) (point))))))))
  13361. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13362. "Find node HEADING in BUFFER.
  13363. Return a marker to the heading if it was found, or nil if not.
  13364. If POS-ONLY is set, return just the position instead of a marker.
  13365. The heading text must match exact, but it may have a TODO keyword,
  13366. a priority cookie and tags in the standard locations."
  13367. (with-current-buffer (or buffer (current-buffer))
  13368. (save-excursion
  13369. (save-restriction
  13370. (widen)
  13371. (goto-char (point-min))
  13372. (let (case-fold-search)
  13373. (if (re-search-forward
  13374. (format org-complex-heading-regexp-format
  13375. (regexp-quote heading)) nil t)
  13376. (if pos-only
  13377. (match-beginning 0)
  13378. (move-marker (make-marker) (match-beginning 0)))))))))
  13379. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13380. "Find Org node headline HEADING in all .org files in directory DIR.
  13381. When the target headline is found, return a marker to this location."
  13382. (let ((files (directory-files (or dir default-directory)
  13383. nil "\\`[^.#].*\\.org\\'"))
  13384. file visiting m buffer)
  13385. (catch 'found
  13386. (while (setq file (pop files))
  13387. (message "trying %s" file)
  13388. (setq visiting (org-find-base-buffer-visiting file))
  13389. (setq buffer (or visiting (find-file-noselect file)))
  13390. (setq m (org-find-exact-headline-in-buffer
  13391. heading buffer))
  13392. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13393. (and m (throw 'found m))))))
  13394. (defun org-find-entry-with-id (ident)
  13395. "Locate the entry that contains the ID property with exact value IDENT.
  13396. IDENT can be a string, a symbol or a number, this function will search for
  13397. the string representation of it.
  13398. Return the position where this entry starts, or nil if there is no such entry."
  13399. (interactive "sID: ")
  13400. (let ((id (cond
  13401. ((stringp ident) ident)
  13402. ((symbol-name ident) (symbol-name ident))
  13403. ((numberp ident) (number-to-string ident))
  13404. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13405. (case-fold-search nil))
  13406. (save-excursion
  13407. (save-restriction
  13408. (widen)
  13409. (goto-char (point-min))
  13410. (when (re-search-forward
  13411. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13412. nil t)
  13413. (org-back-to-heading t)
  13414. (point))))))
  13415. ;;;; Timestamps
  13416. (defvar org-last-changed-timestamp nil)
  13417. (defvar org-last-inserted-timestamp nil
  13418. "The last time stamp inserted with `org-insert-time-stamp'.")
  13419. (defvar org-time-was-given) ; dynamically scoped parameter
  13420. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13421. (defvar org-ts-what) ; dynamically scoped parameter
  13422. (defun org-time-stamp (arg &optional inactive)
  13423. "Prompt for a date/time and insert a time stamp.
  13424. If the user specifies a time like HH:MM, or if this command is called
  13425. with a prefix argument, the time stamp will contain date and time.
  13426. Otherwise, only the date will be included. All parts of a date not
  13427. specified by the user will be filled in from the current date/time.
  13428. So if you press just return without typing anything, the time stamp
  13429. will represent the current date/time. If there is already a timestamp
  13430. at the cursor, it will be modified."
  13431. (interactive "P")
  13432. (let* ((ts nil)
  13433. (default-time
  13434. ;; Default time is either today, or, when entering a range,
  13435. ;; the range start.
  13436. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13437. (save-excursion
  13438. (re-search-backward
  13439. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13440. (- (point) 20) t)))
  13441. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13442. (current-time)))
  13443. (default-input (and ts (org-get-compact-tod ts)))
  13444. (repeater (save-excursion
  13445. (save-match-data
  13446. (beginning-of-line)
  13447. (when (re-search-forward
  13448. "\\([.+-]+[0-9]+[dwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  13449. (save-excursion (progn (end-of-line) (point))) t)
  13450. (match-string 0)))))
  13451. org-time-was-given org-end-time-was-given time)
  13452. (cond
  13453. ((and (org-at-timestamp-p t)
  13454. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13455. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13456. (insert "--")
  13457. (setq time (let ((this-command this-command))
  13458. (org-read-date arg 'totime nil nil
  13459. default-time default-input)))
  13460. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13461. ((org-at-timestamp-p t)
  13462. (setq time (let ((this-command this-command))
  13463. (org-read-date arg 'totime nil nil default-time default-input)))
  13464. (when (org-at-timestamp-p t) ; just to get the match data
  13465. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13466. (replace-match "")
  13467. (setq org-last-changed-timestamp
  13468. (org-insert-time-stamp
  13469. time (or org-time-was-given arg)
  13470. inactive nil nil (list org-end-time-was-given)))
  13471. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13472. (setq org-last-changed-timestamp
  13473. (concat (substring org-last-inserted-timestamp 0 -1)
  13474. " " repeater ">"))))
  13475. (message "Timestamp updated"))
  13476. (t
  13477. (setq time (let ((this-command this-command))
  13478. (org-read-date arg 'totime nil nil default-time default-input)))
  13479. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13480. nil nil (list org-end-time-was-given))))))
  13481. ;; FIXME: can we use this for something else, like computing time differences?
  13482. (defun org-get-compact-tod (s)
  13483. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13484. (let* ((t1 (match-string 1 s))
  13485. (h1 (string-to-number (match-string 2 s)))
  13486. (m1 (string-to-number (match-string 3 s)))
  13487. (t2 (and (match-end 4) (match-string 5 s)))
  13488. (h2 (and t2 (string-to-number (match-string 6 s))))
  13489. (m2 (and t2 (string-to-number (match-string 7 s))))
  13490. dh dm)
  13491. (if (not t2)
  13492. t1
  13493. (setq dh (- h2 h1) dm (- m2 m1))
  13494. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13495. (concat t1 "+" (number-to-string dh)
  13496. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13497. (defun org-time-stamp-inactive (&optional arg)
  13498. "Insert an inactive time stamp.
  13499. An inactive time stamp is enclosed in square brackets instead of angle
  13500. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13501. does not link to the calendar and cannot be changed with the S-cursor keys.
  13502. So these are more for recording a certain time/date."
  13503. (interactive "P")
  13504. (org-time-stamp arg 'inactive))
  13505. (defvar org-date-ovl (make-overlay 1 1))
  13506. (overlay-put org-date-ovl 'face 'org-warning)
  13507. (org-detach-overlay org-date-ovl)
  13508. (defvar org-ans1) ; dynamically scoped parameter
  13509. (defvar org-ans2) ; dynamically scoped parameter
  13510. (defvar org-plain-time-of-day-regexp) ; defined below
  13511. (defvar org-overriding-default-time nil) ; dynamically scoped
  13512. (defvar org-read-date-overlay nil)
  13513. (defvar org-dcst nil) ; dynamically scoped
  13514. (defvar org-read-date-history nil)
  13515. (defvar org-read-date-final-answer nil)
  13516. (defvar org-read-date-analyze-futurep nil)
  13517. (defvar org-read-date-analyze-forced-year nil)
  13518. (defun org-read-date (&optional with-time to-time from-string prompt
  13519. default-time default-input)
  13520. "Read a date, possibly a time, and make things smooth for the user.
  13521. The prompt will suggest to enter an ISO date, but you can also enter anything
  13522. which will at least partially be understood by `parse-time-string'.
  13523. Unrecognized parts of the date will default to the current day, month, year,
  13524. hour and minute. If this command is called to replace a timestamp at point,
  13525. or to enter the second timestamp of a range, the default time is taken
  13526. from the existing stamp. Furthermore, the command prefers the future,
  13527. so if you are giving a date where the year is not given, and the day-month
  13528. combination is already past in the current year, it will assume you
  13529. mean next year. For details, see the manual. A few examples:
  13530. 3-2-5 --> 2003-02-05
  13531. feb 15 --> currentyear-02-15
  13532. 2/15 --> currentyear-02-15
  13533. sep 12 9 --> 2009-09-12
  13534. 12:45 --> today 12:45
  13535. 22 sept 0:34 --> currentyear-09-22 0:34
  13536. 12 --> currentyear-currentmonth-12
  13537. Fri --> nearest Friday (today or later)
  13538. etc.
  13539. Furthermore you can specify a relative date by giving, as the *first* thing
  13540. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  13541. change in days weeks, months, years.
  13542. With a single plus or minus, the date is relative to today. With a double
  13543. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13544. +4d --> four days from today
  13545. +4 --> same as above
  13546. +2w --> two weeks from today
  13547. ++5 --> five days from default date
  13548. The function understands only English month and weekday abbreviations,
  13549. but this can be configured with the variables `parse-time-months' and
  13550. `parse-time-weekdays'.
  13551. While prompting, a calendar is popped up - you can also select the
  13552. date with the mouse (button 1). The calendar shows a period of three
  13553. months. To scroll it to other months, use the keys `>' and `<'.
  13554. If you don't like the calendar, turn it off with
  13555. \(setq org-read-date-popup-calendar nil)
  13556. With optional argument TO-TIME, the date will immediately be converted
  13557. to an internal time.
  13558. With an optional argument WITH-TIME, the prompt will suggest to also
  13559. insert a time. Note that when WITH-TIME is not set, you can still
  13560. enter a time, and this function will inform the calling routine about
  13561. this change. The calling routine may then choose to change the format
  13562. used to insert the time stamp into the buffer to include the time.
  13563. With optional argument FROM-STRING, read from this string instead from
  13564. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13565. the time/date that is used for everything that is not specified by the
  13566. user."
  13567. (require 'parse-time)
  13568. (let* ((org-time-stamp-rounding-minutes
  13569. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13570. (org-dcst org-display-custom-times)
  13571. (ct (org-current-time))
  13572. (def (or org-overriding-default-time default-time ct))
  13573. (defdecode (decode-time def))
  13574. (dummy (progn
  13575. (when (< (nth 2 defdecode) org-extend-today-until)
  13576. (setcar (nthcdr 2 defdecode) -1)
  13577. (setcar (nthcdr 1 defdecode) 59)
  13578. (setq def (apply 'encode-time defdecode)
  13579. defdecode (decode-time def)))))
  13580. (calendar-frame-setup nil)
  13581. (calendar-setup nil)
  13582. (calendar-move-hook nil)
  13583. (calendar-view-diary-initially-flag nil)
  13584. (calendar-view-holidays-initially-flag nil)
  13585. (timestr (format-time-string
  13586. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  13587. (prompt (concat (if prompt (concat prompt " ") "")
  13588. (format "Date+time [%s]: " timestr)))
  13589. ans (org-ans0 "") org-ans1 org-ans2 final)
  13590. (cond
  13591. (from-string (setq ans from-string))
  13592. (org-read-date-popup-calendar
  13593. (save-excursion
  13594. (save-window-excursion
  13595. (calendar)
  13596. (unwind-protect
  13597. (progn
  13598. (calendar-forward-day (- (time-to-days def)
  13599. (calendar-absolute-from-gregorian
  13600. (calendar-current-date))))
  13601. (org-eval-in-calendar nil t)
  13602. (let* ((old-map (current-local-map))
  13603. (map (copy-keymap calendar-mode-map))
  13604. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13605. (org-defkey map (kbd "RET") 'org-calendar-select)
  13606. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13607. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13608. (org-defkey minibuffer-local-map [(meta shift left)]
  13609. (lambda () (interactive)
  13610. (org-eval-in-calendar '(calendar-backward-month 1))))
  13611. (org-defkey minibuffer-local-map [(meta shift right)]
  13612. (lambda () (interactive)
  13613. (org-eval-in-calendar '(calendar-forward-month 1))))
  13614. (org-defkey minibuffer-local-map [(meta shift up)]
  13615. (lambda () (interactive)
  13616. (org-eval-in-calendar '(calendar-backward-year 1))))
  13617. (org-defkey minibuffer-local-map [(meta shift down)]
  13618. (lambda () (interactive)
  13619. (org-eval-in-calendar '(calendar-forward-year 1))))
  13620. (org-defkey minibuffer-local-map [?\e (shift left)]
  13621. (lambda () (interactive)
  13622. (org-eval-in-calendar '(calendar-backward-month 1))))
  13623. (org-defkey minibuffer-local-map [?\e (shift right)]
  13624. (lambda () (interactive)
  13625. (org-eval-in-calendar '(calendar-forward-month 1))))
  13626. (org-defkey minibuffer-local-map [?\e (shift up)]
  13627. (lambda () (interactive)
  13628. (org-eval-in-calendar '(calendar-backward-year 1))))
  13629. (org-defkey minibuffer-local-map [?\e (shift down)]
  13630. (lambda () (interactive)
  13631. (org-eval-in-calendar '(calendar-forward-year 1))))
  13632. (org-defkey minibuffer-local-map [(shift up)]
  13633. (lambda () (interactive)
  13634. (org-eval-in-calendar '(calendar-backward-week 1))))
  13635. (org-defkey minibuffer-local-map [(shift down)]
  13636. (lambda () (interactive)
  13637. (org-eval-in-calendar '(calendar-forward-week 1))))
  13638. (org-defkey minibuffer-local-map [(shift left)]
  13639. (lambda () (interactive)
  13640. (org-eval-in-calendar '(calendar-backward-day 1))))
  13641. (org-defkey minibuffer-local-map [(shift right)]
  13642. (lambda () (interactive)
  13643. (org-eval-in-calendar '(calendar-forward-day 1))))
  13644. (org-defkey minibuffer-local-map ">"
  13645. (lambda () (interactive)
  13646. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13647. (org-defkey minibuffer-local-map "<"
  13648. (lambda () (interactive)
  13649. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13650. (org-defkey minibuffer-local-map "\C-v"
  13651. (lambda () (interactive)
  13652. (org-eval-in-calendar
  13653. '(calendar-scroll-left-three-months 1))))
  13654. (org-defkey minibuffer-local-map "\M-v"
  13655. (lambda () (interactive)
  13656. (org-eval-in-calendar
  13657. '(calendar-scroll-right-three-months 1))))
  13658. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13659. (unwind-protect
  13660. (progn
  13661. (use-local-map map)
  13662. (add-hook 'post-command-hook 'org-read-date-display)
  13663. (setq org-ans0 (read-string prompt default-input
  13664. 'org-read-date-history nil))
  13665. ;; org-ans0: from prompt
  13666. ;; org-ans1: from mouse click
  13667. ;; org-ans2: from calendar motion
  13668. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13669. (remove-hook 'post-command-hook 'org-read-date-display)
  13670. (use-local-map old-map)
  13671. (when org-read-date-overlay
  13672. (delete-overlay org-read-date-overlay)
  13673. (setq org-read-date-overlay nil)))))
  13674. (bury-buffer "*Calendar*")))))
  13675. (t ; Naked prompt only
  13676. (unwind-protect
  13677. (setq ans (read-string prompt default-input
  13678. 'org-read-date-history timestr))
  13679. (when org-read-date-overlay
  13680. (delete-overlay org-read-date-overlay)
  13681. (setq org-read-date-overlay nil)))))
  13682. (setq final (org-read-date-analyze ans def defdecode))
  13683. (when org-read-date-analyze-forced-year
  13684. (message "Year was forced into %s"
  13685. (if org-read-date-force-compatible-dates
  13686. "compatible range (1970-2037)"
  13687. "range representable on this machine"))
  13688. (ding))
  13689. ;; One round trip to get rid of 34th of August and stuff like that....
  13690. (setq final (decode-time (apply 'encode-time final)))
  13691. (setq org-read-date-final-answer ans)
  13692. (if to-time
  13693. (apply 'encode-time final)
  13694. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13695. (format "%04d-%02d-%02d %02d:%02d"
  13696. (nth 5 final) (nth 4 final) (nth 3 final)
  13697. (nth 2 final) (nth 1 final))
  13698. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13699. (defvar def)
  13700. (defvar defdecode)
  13701. (defvar with-time)
  13702. (defun org-read-date-display ()
  13703. "Display the current date prompt interpretation in the minibuffer."
  13704. (when org-read-date-display-live
  13705. (when org-read-date-overlay
  13706. (delete-overlay org-read-date-overlay))
  13707. (let ((p (point)))
  13708. (end-of-line 1)
  13709. (while (not (equal (buffer-substring
  13710. (max (point-min) (- (point) 4)) (point))
  13711. " "))
  13712. (insert " "))
  13713. (goto-char p))
  13714. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13715. " " (or org-ans1 org-ans2)))
  13716. (org-end-time-was-given nil)
  13717. (f (org-read-date-analyze ans def defdecode))
  13718. (fmts (if org-dcst
  13719. org-time-stamp-custom-formats
  13720. org-time-stamp-formats))
  13721. (fmt (if (or with-time
  13722. (and (boundp 'org-time-was-given) org-time-was-given))
  13723. (cdr fmts)
  13724. (car fmts)))
  13725. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  13726. (when (and org-end-time-was-given
  13727. (string-match org-plain-time-of-day-regexp txt))
  13728. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13729. org-end-time-was-given
  13730. (substring txt (match-end 0)))))
  13731. (when org-read-date-analyze-futurep
  13732. (setq txt (concat txt " (=>F)")))
  13733. (setq org-read-date-overlay
  13734. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13735. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13736. (defun org-read-date-analyze (ans def defdecode)
  13737. "Analyze the combined answer of the date prompt."
  13738. ;; FIXME: cleanup and comment
  13739. (let ((nowdecode (decode-time (current-time)))
  13740. delta deltan deltaw deltadef year month day
  13741. hour minute second wday pm h2 m2 tl wday1
  13742. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13743. (setq org-read-date-analyze-futurep nil
  13744. org-read-date-analyze-forced-year nil)
  13745. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13746. (setq ans "+0"))
  13747. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13748. (setq ans (replace-match "" t t ans)
  13749. deltan (car delta)
  13750. deltaw (nth 1 delta)
  13751. deltadef (nth 2 delta)))
  13752. ;; Check if there is an iso week date in there
  13753. ;; If yes, store the info and postpone interpreting it until the rest
  13754. ;; of the parsing is done
  13755. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13756. (setq iso-year (if (match-end 1)
  13757. (org-small-year-to-year
  13758. (string-to-number (match-string 1 ans))))
  13759. iso-weekday (if (match-end 3)
  13760. (string-to-number (match-string 3 ans)))
  13761. iso-week (string-to-number (match-string 2 ans)))
  13762. (setq ans (replace-match "" t t ans)))
  13763. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13764. (when (string-match
  13765. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13766. (setq year (if (match-end 2)
  13767. (string-to-number (match-string 2 ans))
  13768. (progn (setq kill-year t)
  13769. (string-to-number (format-time-string "%Y"))))
  13770. month (string-to-number (match-string 3 ans))
  13771. day (string-to-number (match-string 4 ans)))
  13772. (if (< year 100) (setq year (+ 2000 year)))
  13773. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13774. t nil ans)))
  13775. ;; Help matching dottet european dates
  13776. (when (string-match
  13777. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  13778. (setq year (if (match-end 3)
  13779. (string-to-number (match-string 3 ans))
  13780. (progn (setq kill-year t)
  13781. (string-to-number (format-time-string "%Y"))))
  13782. day (string-to-number (match-string 1 ans))
  13783. month (string-to-number (match-string 2 ans))
  13784. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13785. t nil ans)))
  13786. ;; Help matching american dates, like 5/30 or 5/30/7
  13787. (when (string-match
  13788. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13789. (setq year (if (match-end 4)
  13790. (string-to-number (match-string 4 ans))
  13791. (progn (setq kill-year t)
  13792. (string-to-number (format-time-string "%Y"))))
  13793. month (string-to-number (match-string 1 ans))
  13794. day (string-to-number (match-string 2 ans)))
  13795. (if (< year 100) (setq year (+ 2000 year)))
  13796. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13797. t nil ans)))
  13798. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13799. ;; If there is a time with am/pm, and *no* time without it, we convert
  13800. ;; so that matching will be successful.
  13801. (loop for i from 1 to 2 do ; twice, for end time as well
  13802. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13803. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13804. (setq hour (string-to-number (match-string 1 ans))
  13805. minute (if (match-end 3)
  13806. (string-to-number (match-string 3 ans))
  13807. 0)
  13808. pm (equal ?p
  13809. (string-to-char (downcase (match-string 4 ans)))))
  13810. (if (and (= hour 12) (not pm))
  13811. (setq hour 0)
  13812. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13813. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13814. t t ans))))
  13815. ;; Check if a time range is given as a duration
  13816. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13817. (setq hour (string-to-number (match-string 1 ans))
  13818. h2 (+ hour (string-to-number (match-string 3 ans)))
  13819. minute (string-to-number (match-string 2 ans))
  13820. m2 (+ minute (if (match-end 5) (string-to-number
  13821. (match-string 5 ans))0)))
  13822. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  13823. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  13824. t t ans)))
  13825. ;; Check if there is a time range
  13826. (when (boundp 'org-end-time-was-given)
  13827. (setq org-time-was-given nil)
  13828. (when (and (string-match org-plain-time-of-day-regexp ans)
  13829. (match-end 8))
  13830. (setq org-end-time-was-given (match-string 8 ans))
  13831. (setq ans (concat (substring ans 0 (match-beginning 7))
  13832. (substring ans (match-end 7))))))
  13833. (setq tl (parse-time-string ans)
  13834. day (or (nth 3 tl) (nth 3 defdecode))
  13835. month (or (nth 4 tl)
  13836. (if (and org-read-date-prefer-future
  13837. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  13838. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  13839. (nth 4 defdecode)))
  13840. year (or (and (not kill-year) (nth 5 tl))
  13841. (if (and org-read-date-prefer-future
  13842. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  13843. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  13844. (nth 5 defdecode)))
  13845. hour (or (nth 2 tl) (nth 2 defdecode))
  13846. minute (or (nth 1 tl) (nth 1 defdecode))
  13847. second (or (nth 0 tl) 0)
  13848. wday (nth 6 tl))
  13849. (when (and (eq org-read-date-prefer-future 'time)
  13850. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  13851. (equal day (nth 3 nowdecode))
  13852. (equal month (nth 4 nowdecode))
  13853. (equal year (nth 5 nowdecode))
  13854. (nth 2 tl)
  13855. (or (< (nth 2 tl) (nth 2 nowdecode))
  13856. (and (= (nth 2 tl) (nth 2 nowdecode))
  13857. (nth 1 tl)
  13858. (< (nth 1 tl) (nth 1 nowdecode)))))
  13859. (setq day (1+ day)
  13860. futurep t))
  13861. ;; Special date definitions below
  13862. (cond
  13863. (iso-week
  13864. ;; There was an iso week
  13865. (require 'cal-iso)
  13866. (setq futurep nil)
  13867. (setq year (or iso-year year)
  13868. day (or iso-weekday wday 1)
  13869. wday nil ; to make sure that the trigger below does not match
  13870. iso-date (calendar-gregorian-from-absolute
  13871. (calendar-absolute-from-iso
  13872. (list iso-week day year))))
  13873. ; FIXME: Should we also push ISO weeks into the future?
  13874. ; (when (and org-read-date-prefer-future
  13875. ; (not iso-year)
  13876. ; (< (calendar-absolute-from-gregorian iso-date)
  13877. ; (time-to-days (current-time))))
  13878. ; (setq year (1+ year)
  13879. ; iso-date (calendar-gregorian-from-absolute
  13880. ; (calendar-absolute-from-iso
  13881. ; (list iso-week day year)))))
  13882. (setq month (car iso-date)
  13883. year (nth 2 iso-date)
  13884. day (nth 1 iso-date)))
  13885. (deltan
  13886. (setq futurep nil)
  13887. (unless deltadef
  13888. (let ((now (decode-time (current-time))))
  13889. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  13890. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  13891. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  13892. ((equal deltaw "m") (setq month (+ month deltan)))
  13893. ((equal deltaw "y") (setq year (+ year deltan)))))
  13894. ((and wday (not (nth 3 tl)))
  13895. (setq futurep nil)
  13896. ;; Weekday was given, but no day, so pick that day in the week
  13897. ;; on or after the derived date.
  13898. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  13899. (unless (equal wday wday1)
  13900. (setq day (+ day (% (- wday wday1 -7) 7))))))
  13901. (if (and (boundp 'org-time-was-given)
  13902. (nth 2 tl))
  13903. (setq org-time-was-given t))
  13904. (if (< year 100) (setq year (+ 2000 year)))
  13905. ;; Check of the date is representable
  13906. (if org-read-date-force-compatible-dates
  13907. (progn
  13908. (if (< year 1970)
  13909. (setq year 1970 org-read-date-analyze-forced-year t))
  13910. (if (> year 2037)
  13911. (setq year 2037 org-read-date-analyze-forced-year t)))
  13912. (condition-case nil
  13913. (ignore (encode-time second minute hour day month year))
  13914. (error
  13915. (setq year (nth 5 defdecode))
  13916. (setq org-read-date-analyze-forced-year t))))
  13917. (setq org-read-date-analyze-futurep futurep)
  13918. (list second minute hour day month year)))
  13919. (defvar parse-time-weekdays)
  13920. (defun org-read-date-get-relative (s today default)
  13921. "Check string S for special relative date string.
  13922. TODAY and DEFAULT are internal times, for today and for a default.
  13923. Return shift list (N what def-flag)
  13924. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  13925. N is the number of WHATs to shift.
  13926. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  13927. the DEFAULT date rather than TODAY."
  13928. (require 'parse-time)
  13929. (when (and
  13930. (string-match
  13931. (concat
  13932. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  13933. "\\([0-9]+\\)?"
  13934. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  13935. "\\([ \t]\\|$\\)") s)
  13936. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  13937. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  13938. (string-to-char (substring (match-string 1 s) -1))
  13939. ?+))
  13940. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  13941. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  13942. (what (if (match-end 3) (match-string 3 s) "d"))
  13943. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  13944. (date (if rel default today))
  13945. (wday (nth 6 (decode-time date)))
  13946. delta)
  13947. (if wday1
  13948. (progn
  13949. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  13950. (if (= dir ?-) (setq delta (- delta 7)))
  13951. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  13952. (list delta "d" rel))
  13953. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  13954. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  13955. "Turn a user-specified date into the internal representation.
  13956. The internal representation needed by the calendar is (month day year).
  13957. This is a wrapper to handle the brain-dead convention in calendar that
  13958. user function argument order change dependent on argument order."
  13959. (if (boundp 'calendar-date-style)
  13960. (cond
  13961. ((eq calendar-date-style 'american)
  13962. (list arg1 arg2 arg3))
  13963. ((eq calendar-date-style 'european)
  13964. (list arg2 arg1 arg3))
  13965. ((eq calendar-date-style 'iso)
  13966. (list arg2 arg3 arg1)))
  13967. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  13968. (if (org-bound-and-true-p european-calendar-style)
  13969. (list arg2 arg1 arg3)
  13970. (list arg1 arg2 arg3)))))
  13971. (defun org-eval-in-calendar (form &optional keepdate)
  13972. "Eval FORM in the calendar window and return to current window.
  13973. Also, store the cursor date in variable org-ans2."
  13974. (let ((sf (selected-frame))
  13975. (sw (selected-window)))
  13976. (select-window (get-buffer-window "*Calendar*" t))
  13977. (eval form)
  13978. (when (and (not keepdate) (calendar-cursor-to-date))
  13979. (let* ((date (calendar-cursor-to-date))
  13980. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13981. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  13982. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  13983. (select-window sw)
  13984. (org-select-frame-set-input-focus sf)))
  13985. (defun org-calendar-select ()
  13986. "Return to `org-read-date' with the date currently selected.
  13987. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13988. (interactive)
  13989. (when (calendar-cursor-to-date)
  13990. (let* ((date (calendar-cursor-to-date))
  13991. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13992. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13993. (if (active-minibuffer-window) (exit-minibuffer))))
  13994. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  13995. "Insert a date stamp for the date given by the internal TIME.
  13996. WITH-HM means use the stamp format that includes the time of the day.
  13997. INACTIVE means use square brackets instead of angular ones, so that the
  13998. stamp will not contribute to the agenda.
  13999. PRE and POST are optional strings to be inserted before and after the
  14000. stamp.
  14001. The command returns the inserted time stamp."
  14002. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14003. stamp)
  14004. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14005. (insert-before-markers (or pre ""))
  14006. (when (listp extra)
  14007. (setq extra (car extra))
  14008. (if (and (stringp extra)
  14009. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14010. (setq extra (format "-%02d:%02d"
  14011. (string-to-number (match-string 1 extra))
  14012. (string-to-number (match-string 2 extra))))
  14013. (setq extra nil)))
  14014. (when extra
  14015. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14016. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14017. (insert-before-markers (or post ""))
  14018. (setq org-last-inserted-timestamp stamp)))
  14019. (defun org-toggle-time-stamp-overlays ()
  14020. "Toggle the use of custom time stamp formats."
  14021. (interactive)
  14022. (setq org-display-custom-times (not org-display-custom-times))
  14023. (unless org-display-custom-times
  14024. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14025. (while (setq p (next-single-property-change p 'display))
  14026. (if (and (get-text-property p 'display)
  14027. (eq (get-text-property p 'face) 'org-date))
  14028. (remove-text-properties
  14029. p (setq p (next-single-property-change p 'display))
  14030. '(display t))))
  14031. (set-buffer-modified-p bmp)))
  14032. (if (featurep 'xemacs)
  14033. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14034. (org-restart-font-lock)
  14035. (setq org-table-may-need-update t)
  14036. (if org-display-custom-times
  14037. (message "Time stamps are overlaid with custom format")
  14038. (message "Time stamp overlays removed")))
  14039. (defun org-display-custom-time (beg end)
  14040. "Overlay modified time stamp format over timestamp between BEG and END."
  14041. (let* ((ts (buffer-substring beg end))
  14042. t1 w1 with-hm tf time str w2 (off 0))
  14043. (save-match-data
  14044. (setq t1 (org-parse-time-string ts t))
  14045. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  14046. (setq off (- (match-end 0) (match-beginning 0)))))
  14047. (setq end (- end off))
  14048. (setq w1 (- end beg)
  14049. with-hm (and (nth 1 t1) (nth 2 t1))
  14050. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14051. time (org-fix-decoded-time t1)
  14052. str (org-add-props
  14053. (format-time-string
  14054. (substring tf 1 -1) (apply 'encode-time time))
  14055. nil 'mouse-face 'highlight)
  14056. w2 (length str))
  14057. (if (not (= w2 w1))
  14058. (add-text-properties (1+ beg) (+ 2 beg)
  14059. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14060. (if (featurep 'xemacs)
  14061. (progn
  14062. (put-text-property beg end 'invisible t)
  14063. (put-text-property beg end 'end-glyph (make-glyph str)))
  14064. (put-text-property beg end 'display str))))
  14065. (defun org-translate-time (string)
  14066. "Translate all timestamps in STRING to custom format.
  14067. But do this only if the variable `org-display-custom-times' is set."
  14068. (when org-display-custom-times
  14069. (save-match-data
  14070. (let* ((start 0)
  14071. (re org-ts-regexp-both)
  14072. t1 with-hm inactive tf time str beg end)
  14073. (while (setq start (string-match re string start))
  14074. (setq beg (match-beginning 0)
  14075. end (match-end 0)
  14076. t1 (save-match-data
  14077. (org-parse-time-string (substring string beg end) t))
  14078. with-hm (and (nth 1 t1) (nth 2 t1))
  14079. inactive (equal (substring string beg (1+ beg)) "[")
  14080. tf (funcall (if with-hm 'cdr 'car)
  14081. org-time-stamp-custom-formats)
  14082. time (org-fix-decoded-time t1)
  14083. str (format-time-string
  14084. (concat
  14085. (if inactive "[" "<") (substring tf 1 -1)
  14086. (if inactive "]" ">"))
  14087. (apply 'encode-time time))
  14088. string (replace-match str t t string)
  14089. start (+ start (length str)))))))
  14090. string)
  14091. (defun org-fix-decoded-time (time)
  14092. "Set 0 instead of nil for the first 6 elements of time.
  14093. Don't touch the rest."
  14094. (let ((n 0))
  14095. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14096. (defun org-days-to-time (timestamp-string)
  14097. "Difference between TIMESTAMP-STRING and now in days."
  14098. (- (time-to-days (org-time-string-to-time timestamp-string))
  14099. (time-to-days (current-time))))
  14100. (defun org-deadline-close (timestamp-string &optional ndays)
  14101. "Is the time in TIMESTAMP-STRING close to the current date?"
  14102. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14103. (and (< (org-days-to-time timestamp-string) ndays)
  14104. (not (org-entry-is-done-p))))
  14105. (defun org-get-wdays (ts)
  14106. "Get the deadline lead time appropriate for timestring TS."
  14107. (cond
  14108. ((<= org-deadline-warning-days 0)
  14109. ;; 0 or negative, enforce this value no matter what
  14110. (- org-deadline-warning-days))
  14111. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14112. ;; lead time is specified.
  14113. (floor (* (string-to-number (match-string 1 ts))
  14114. (cdr (assoc (match-string 2 ts)
  14115. '(("d" . 1) ("w" . 7)
  14116. ("m" . 30.4) ("y" . 365.25)))))))
  14117. ;; go for the default.
  14118. (t org-deadline-warning-days)))
  14119. (defun org-calendar-select-mouse (ev)
  14120. "Return to `org-read-date' with the date currently selected.
  14121. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14122. (interactive "e")
  14123. (mouse-set-point ev)
  14124. (when (calendar-cursor-to-date)
  14125. (let* ((date (calendar-cursor-to-date))
  14126. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14127. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14128. (if (active-minibuffer-window) (exit-minibuffer))))
  14129. (defun org-check-deadlines (ndays)
  14130. "Check if there are any deadlines due or past due.
  14131. A deadline is considered due if it happens within `org-deadline-warning-days'
  14132. days from today's date. If the deadline appears in an entry marked DONE,
  14133. it is not shown. The prefix arg NDAYS can be used to test that many
  14134. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14135. (interactive "P")
  14136. (let* ((org-warn-days
  14137. (cond
  14138. ((equal ndays '(4)) 100000)
  14139. (ndays (prefix-numeric-value ndays))
  14140. (t (abs org-deadline-warning-days))))
  14141. (case-fold-search nil)
  14142. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14143. (callback
  14144. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14145. (message "%d deadlines past-due or due within %d days"
  14146. (org-occur regexp nil callback)
  14147. org-warn-days)))
  14148. (defun org-check-before-date (date)
  14149. "Check if there are deadlines or scheduled entries before DATE."
  14150. (interactive (list (org-read-date)))
  14151. (let ((case-fold-search nil)
  14152. (regexp (concat "\\<\\(" org-deadline-string
  14153. "\\|" org-scheduled-string
  14154. "\\) *<\\([^>]+\\)>"))
  14155. (callback
  14156. (lambda () (time-less-p
  14157. (org-time-string-to-time (match-string 2))
  14158. (org-time-string-to-time date)))))
  14159. (message "%d entries before %s"
  14160. (org-occur regexp nil callback) date)))
  14161. (defun org-check-after-date (date)
  14162. "Check if there are deadlines or scheduled entries after DATE."
  14163. (interactive (list (org-read-date)))
  14164. (let ((case-fold-search nil)
  14165. (regexp (concat "\\<\\(" org-deadline-string
  14166. "\\|" org-scheduled-string
  14167. "\\) *<\\([^>]+\\)>"))
  14168. (callback
  14169. (lambda () (not
  14170. (time-less-p
  14171. (org-time-string-to-time (match-string 2))
  14172. (org-time-string-to-time date))))))
  14173. (message "%d entries after %s"
  14174. (org-occur regexp nil callback) date)))
  14175. (defun org-evaluate-time-range (&optional to-buffer)
  14176. "Evaluate a time range by computing the difference between start and end.
  14177. Normally the result is just printed in the echo area, but with prefix arg
  14178. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14179. If the time range is actually in a table, the result is inserted into the
  14180. next column.
  14181. For time difference computation, a year is assumed to be exactly 365
  14182. days in order to avoid rounding problems."
  14183. (interactive "P")
  14184. (or
  14185. (org-clock-update-time-maybe)
  14186. (save-excursion
  14187. (unless (org-at-date-range-p t)
  14188. (goto-char (point-at-bol))
  14189. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14190. (if (not (org-at-date-range-p t))
  14191. (error "Not at a time-stamp range, and none found in current line")))
  14192. (let* ((ts1 (match-string 1))
  14193. (ts2 (match-string 2))
  14194. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14195. (match-end (match-end 0))
  14196. (time1 (org-time-string-to-time ts1))
  14197. (time2 (org-time-string-to-time ts2))
  14198. (t1 (org-float-time time1))
  14199. (t2 (org-float-time time2))
  14200. (diff (abs (- t2 t1)))
  14201. (negative (< (- t2 t1) 0))
  14202. ;; (ys (floor (* 365 24 60 60)))
  14203. (ds (* 24 60 60))
  14204. (hs (* 60 60))
  14205. (fy "%dy %dd %02d:%02d")
  14206. (fy1 "%dy %dd")
  14207. (fd "%dd %02d:%02d")
  14208. (fd1 "%dd")
  14209. (fh "%02d:%02d")
  14210. y d h m align)
  14211. (if havetime
  14212. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14213. y 0
  14214. d (floor (/ diff ds)) diff (mod diff ds)
  14215. h (floor (/ diff hs)) diff (mod diff hs)
  14216. m (floor (/ diff 60)))
  14217. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14218. y 0
  14219. d (floor (+ (/ diff ds) 0.5))
  14220. h 0 m 0))
  14221. (if (not to-buffer)
  14222. (message "%s" (org-make-tdiff-string y d h m))
  14223. (if (org-at-table-p)
  14224. (progn
  14225. (goto-char match-end)
  14226. (setq align t)
  14227. (and (looking-at " *|") (goto-char (match-end 0))))
  14228. (goto-char match-end))
  14229. (if (looking-at
  14230. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14231. (replace-match ""))
  14232. (if negative (insert " -"))
  14233. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14234. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14235. (insert " " (format fh h m))))
  14236. (if align (org-table-align))
  14237. (message "Time difference inserted")))))
  14238. (defun org-make-tdiff-string (y d h m)
  14239. (let ((fmt "")
  14240. (l nil))
  14241. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14242. l (push y l)))
  14243. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14244. l (push d l)))
  14245. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14246. l (push h l)))
  14247. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14248. l (push m l)))
  14249. (apply 'format fmt (nreverse l))))
  14250. (defun org-time-string-to-time (s &optional buffer pos)
  14251. (condition-case errdata
  14252. (apply 'encode-time (org-parse-time-string s))
  14253. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14254. s (if (not (and buffer pos))
  14255. ""
  14256. (format " at %d in buffer `%s'" pos buffer))
  14257. (cdr errdata)))))
  14258. (defun org-time-string-to-seconds (s)
  14259. (org-float-time (org-time-string-to-time s)))
  14260. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14261. "Convert a time stamp to an absolute day number.
  14262. If there is a specifier for a cyclic time stamp, get the closest date to
  14263. DAYNR.
  14264. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14265. The variable date is bound by the calendar when this is called."
  14266. (cond
  14267. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14268. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14269. daynr
  14270. (+ daynr 1000)))
  14271. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  14272. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14273. (time-to-days (current-time))) (match-string 0 s)
  14274. prefer show-all))
  14275. (t (time-to-days
  14276. (condition-case errdata
  14277. (apply 'encode-time (org-parse-time-string s))
  14278. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14279. s (if (not (and buffer pos))
  14280. ""
  14281. (format " at %d in buffer `%s'" pos buffer))
  14282. (cdr errdata))))))))
  14283. (defun org-days-to-iso-week (days)
  14284. "Return the iso week number."
  14285. (require 'cal-iso)
  14286. (car (calendar-iso-from-absolute days)))
  14287. (defun org-small-year-to-year (year)
  14288. "Convert 2-digit years into 4-digit years.
  14289. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14290. The year 2000 cannot be abbreviated. Any year larger than 99
  14291. is returned unchanged."
  14292. (if (< year 38)
  14293. (setq year (+ 2000 year))
  14294. (if (< year 100)
  14295. (setq year (+ 1900 year))))
  14296. year)
  14297. (defun org-time-from-absolute (d)
  14298. "Return the time corresponding to date D.
  14299. D may be an absolute day number, or a calendar-type list (month day year)."
  14300. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14301. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14302. (defun org-calendar-holiday ()
  14303. "List of holidays, for Diary display in Org-mode."
  14304. (require 'holidays)
  14305. (let ((hl (funcall
  14306. (if (fboundp 'calendar-check-holidays)
  14307. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14308. (if hl (mapconcat 'identity hl "; "))))
  14309. (defun org-diary-sexp-entry (sexp entry date)
  14310. "Process a SEXP diary ENTRY for DATE."
  14311. (require 'diary-lib)
  14312. (let ((result (if calendar-debug-sexp
  14313. (let ((stack-trace-on-error t))
  14314. (eval (car (read-from-string sexp))))
  14315. (condition-case nil
  14316. (eval (car (read-from-string sexp)))
  14317. (error
  14318. (beep)
  14319. (message "Bad sexp at line %d in %s: %s"
  14320. (org-current-line)
  14321. (buffer-file-name) sexp)
  14322. (sleep-for 2))))))
  14323. (cond ((stringp result) (split-string result "; "))
  14324. ((and (consp result)
  14325. (not (consp (cdr result)))
  14326. (stringp (cdr result))) (cdr result))
  14327. ((and (consp result)
  14328. (stringp (car result))) result)
  14329. (result entry)
  14330. (t nil))))
  14331. (defun org-diary-to-ical-string (frombuf)
  14332. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14333. This uses the icalendar.el library."
  14334. (let* ((tmpdir (if (featurep 'xemacs)
  14335. (temp-directory)
  14336. temporary-file-directory))
  14337. (tmpfile (make-temp-name
  14338. (expand-file-name "orgics" tmpdir)))
  14339. buf rtn b e)
  14340. (with-current-buffer frombuf
  14341. (icalendar-export-region (point-min) (point-max) tmpfile)
  14342. (setq buf (find-buffer-visiting tmpfile))
  14343. (set-buffer buf)
  14344. (goto-char (point-min))
  14345. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14346. (setq b (match-beginning 0)))
  14347. (goto-char (point-max))
  14348. (if (re-search-backward "^END:VEVENT" nil t)
  14349. (setq e (match-end 0)))
  14350. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14351. (kill-buffer buf)
  14352. (delete-file tmpfile)
  14353. rtn))
  14354. (defun org-closest-date (start current change prefer show-all)
  14355. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14356. When PREFER is `past', return a date that is either CURRENT or past.
  14357. When PREFER is `future', return a date that is either CURRENT or future.
  14358. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14359. ;; Make the proper lists from the dates
  14360. (catch 'exit
  14361. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14362. dn dw sday cday n1 n2 n0
  14363. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14364. (setq start (org-date-to-gregorian start)
  14365. current (org-date-to-gregorian
  14366. (if show-all
  14367. current
  14368. (time-to-days (current-time))))
  14369. sday (calendar-absolute-from-gregorian start)
  14370. cday (calendar-absolute-from-gregorian current))
  14371. (if (<= cday sday) (throw 'exit sday))
  14372. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  14373. (setq dn (string-to-number (match-string 1 change))
  14374. dw (cdr (assoc (match-string 2 change) a1)))
  14375. (error "Invalid change specifier: %s" change))
  14376. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14377. (cond
  14378. ((eq dw 'day)
  14379. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14380. n2 (+ n1 dn)))
  14381. ((eq dw 'year)
  14382. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14383. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14384. (setq date1 (list m d y1)
  14385. n1 (calendar-absolute-from-gregorian date1)
  14386. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14387. n2 (calendar-absolute-from-gregorian date2)))
  14388. ((eq dw 'month)
  14389. ;; approx number of month between the two dates
  14390. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14391. ;; How often does dn fit in there?
  14392. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14393. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14394. m (+ m nm)
  14395. ny (floor (/ m 12))
  14396. y (+ y ny)
  14397. m (- m (* ny 12)))
  14398. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14399. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14400. (setq m2 (+ m dn) y2 y)
  14401. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14402. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14403. (while (<= n2 cday)
  14404. (setq n1 n2 m m2 y y2)
  14405. (setq m2 (+ m dn) y2 y)
  14406. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14407. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14408. ;; Make sure n1 is the earlier date
  14409. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14410. (if show-all
  14411. (cond
  14412. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14413. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14414. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14415. (cond
  14416. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14417. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14418. (t (if (= cday n1) n1 n2)))))))
  14419. (defun org-date-to-gregorian (date)
  14420. "Turn any specification of DATE into a Gregorian date for the calendar."
  14421. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14422. ((and (listp date) (= (length date) 3)) date)
  14423. ((stringp date)
  14424. (setq date (org-parse-time-string date))
  14425. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14426. ((listp date)
  14427. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14428. (defun org-parse-time-string (s &optional nodefault)
  14429. "Parse the standard Org-mode time string.
  14430. This should be a lot faster than the normal `parse-time-string'.
  14431. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14432. hour and minute fields will be nil if not given."
  14433. (if (string-match org-ts-regexp0 s)
  14434. (list 0
  14435. (if (or (match-beginning 8) (not nodefault))
  14436. (string-to-number (or (match-string 8 s) "0")))
  14437. (if (or (match-beginning 7) (not nodefault))
  14438. (string-to-number (or (match-string 7 s) "0")))
  14439. (string-to-number (match-string 4 s))
  14440. (string-to-number (match-string 3 s))
  14441. (string-to-number (match-string 2 s))
  14442. nil nil nil)
  14443. (error "Not a standard Org-mode time string: %s" s)))
  14444. (defun org-timestamp-up (&optional arg)
  14445. "Increase the date item at the cursor by one.
  14446. If the cursor is on the year, change the year. If it is on the month,
  14447. the day or the time, change that.
  14448. With prefix ARG, change by that many units."
  14449. (interactive "p")
  14450. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14451. (defun org-timestamp-down (&optional arg)
  14452. "Decrease the date item at the cursor by one.
  14453. If the cursor is on the year, change the year. If it is on the month,
  14454. the day or the time, change that.
  14455. With prefix ARG, change by that many units."
  14456. (interactive "p")
  14457. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14458. (defun org-timestamp-up-day (&optional arg)
  14459. "Increase the date in the time stamp by one day.
  14460. With prefix ARG, change that many days."
  14461. (interactive "p")
  14462. (if (and (not (org-at-timestamp-p t))
  14463. (org-on-heading-p))
  14464. (org-todo 'up)
  14465. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14466. (defun org-timestamp-down-day (&optional arg)
  14467. "Decrease the date in the time stamp by one day.
  14468. With prefix ARG, change that many days."
  14469. (interactive "p")
  14470. (if (and (not (org-at-timestamp-p t))
  14471. (org-on-heading-p))
  14472. (org-todo 'down)
  14473. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14474. (defun org-at-timestamp-p (&optional inactive-ok)
  14475. "Determine if the cursor is in or at a timestamp."
  14476. (interactive)
  14477. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14478. (pos (point))
  14479. (ans (or (looking-at tsr)
  14480. (save-excursion
  14481. (skip-chars-backward "^[<\n\r\t")
  14482. (if (> (point) (point-min)) (backward-char 1))
  14483. (and (looking-at tsr)
  14484. (> (- (match-end 0) pos) -1))))))
  14485. (and ans
  14486. (boundp 'org-ts-what)
  14487. (setq org-ts-what
  14488. (cond
  14489. ((= pos (match-beginning 0)) 'bracket)
  14490. ;; Point is considered to be "on the bracket" whether
  14491. ;; it's really on it or right after it.
  14492. ((or (= pos (1- (match-end 0)))
  14493. (= pos (match-end 0))) 'bracket)
  14494. ((org-pos-in-match-range pos 2) 'year)
  14495. ((org-pos-in-match-range pos 3) 'month)
  14496. ((org-pos-in-match-range pos 7) 'hour)
  14497. ((org-pos-in-match-range pos 8) 'minute)
  14498. ((or (org-pos-in-match-range pos 4)
  14499. (org-pos-in-match-range pos 5)) 'day)
  14500. ((and (> pos (or (match-end 8) (match-end 5)))
  14501. (< pos (match-end 0)))
  14502. (- pos (or (match-end 8) (match-end 5))))
  14503. (t 'day))))
  14504. ans))
  14505. (defun org-toggle-timestamp-type ()
  14506. "Toggle the type (<active> or [inactive]) of a time stamp."
  14507. (interactive)
  14508. (when (org-at-timestamp-p t)
  14509. (let ((beg (match-beginning 0)) (end (match-end 0))
  14510. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14511. (save-excursion
  14512. (goto-char beg)
  14513. (while (re-search-forward "[][<>]" end t)
  14514. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14515. t t)))
  14516. (message "Timestamp is now %sactive"
  14517. (if (equal (char-after beg) ?<) "" "in")))))
  14518. (defun org-timestamp-change (n &optional what updown)
  14519. "Change the date in the time stamp at point.
  14520. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14521. `year', `minute', `second'. If WHAT is not given, the cursor position
  14522. in the timestamp determines what will be changed."
  14523. (let ((origin (point)) origin-cat
  14524. with-hm inactive
  14525. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  14526. org-ts-what
  14527. extra rem
  14528. ts time time0)
  14529. (if (not (org-at-timestamp-p t))
  14530. (error "Not at a timestamp"))
  14531. (if (and (not what) (eq org-ts-what 'bracket))
  14532. (org-toggle-timestamp-type)
  14533. ;; Point isn't on brackets. Remember the part of the time-stamp
  14534. ;; the point was in. Indeed, size of time-stamps may change,
  14535. ;; but point must be kept in the same category nonetheless.
  14536. (setq origin-cat org-ts-what)
  14537. (if (and (not what) (not (eq org-ts-what 'day))
  14538. org-display-custom-times
  14539. (get-text-property (point) 'display)
  14540. (not (get-text-property (1- (point)) 'display)))
  14541. (setq org-ts-what 'day))
  14542. (setq org-ts-what (or what org-ts-what)
  14543. inactive (= (char-after (match-beginning 0)) ?\[)
  14544. ts (match-string 0))
  14545. (replace-match "")
  14546. (if (string-match
  14547. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  14548. ts)
  14549. (setq extra (match-string 1 ts)))
  14550. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14551. (setq with-hm t))
  14552. (setq time0 (org-parse-time-string ts))
  14553. (when (and updown
  14554. (eq org-ts-what 'minute)
  14555. (not current-prefix-arg))
  14556. ;; This looks like s-up and s-down. Change by one rounding step.
  14557. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  14558. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  14559. (setcar (cdr time0) (+ (nth 1 time0)
  14560. (if (> n 0) (- rem) (- dm rem))))))
  14561. (setq time
  14562. (encode-time (or (car time0) 0)
  14563. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  14564. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  14565. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  14566. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  14567. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  14568. (nthcdr 6 time0)))
  14569. (when (and (member org-ts-what '(hour minute))
  14570. extra
  14571. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  14572. (setq extra (org-modify-ts-extra
  14573. extra
  14574. (if (eq org-ts-what 'hour) 2 5)
  14575. n dm)))
  14576. (when (integerp org-ts-what)
  14577. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  14578. (if (eq what 'calendar)
  14579. (let ((cal-date (org-get-date-from-calendar)))
  14580. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14581. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14582. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14583. (setcar time0 (or (car time0) 0))
  14584. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14585. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14586. (setq time (apply 'encode-time time0))))
  14587. ;; Insert the new time-stamp, and ensure point stays in the same
  14588. ;; category as before (i.e. not after the last position in that
  14589. ;; category).
  14590. (let ((pos (point)))
  14591. ;; Stay before inserted string. `save-excursion' is of no use.
  14592. (setq org-last-changed-timestamp
  14593. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14594. (goto-char pos))
  14595. (save-match-data
  14596. (looking-at org-ts-regexp3)
  14597. (goto-char (cond
  14598. ;; `day' category ends before `hour' if any, or at
  14599. ;; the end of the day name.
  14600. ((eq origin-cat 'day)
  14601. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  14602. ((eq origin-cat 'hour) (min (match-end 7) origin))
  14603. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  14604. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  14605. ;; `year' and `month' have both fixed size: point
  14606. ;; couldn't have moved into another part.
  14607. (t origin))))
  14608. ;; Update clock if on a CLOCK line.
  14609. (org-clock-update-time-maybe)
  14610. ;; Try to recenter the calendar window, if any.
  14611. (if (and org-calendar-follow-timestamp-change
  14612. (get-buffer-window "*Calendar*" t)
  14613. (memq org-ts-what '(day month year)))
  14614. (org-recenter-calendar (time-to-days time))))))
  14615. (defun org-modify-ts-extra (s pos n dm)
  14616. "Change the different parts of the lead-time and repeat fields in timestamp."
  14617. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14618. ng h m new rem)
  14619. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14620. (cond
  14621. ((or (org-pos-in-match-range pos 2)
  14622. (org-pos-in-match-range pos 3))
  14623. (setq m (string-to-number (match-string 3 s))
  14624. h (string-to-number (match-string 2 s)))
  14625. (if (org-pos-in-match-range pos 2)
  14626. (setq h (+ h n))
  14627. (setq n (* dm (org-no-warnings (signum n))))
  14628. (when (not (= 0 (setq rem (% m dm))))
  14629. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14630. (setq m (+ m n)))
  14631. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14632. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14633. (setq h (min 24 (max 0 h)))
  14634. (setq ng 1 new (format "-%02d:%02d" h m)))
  14635. ((org-pos-in-match-range pos 6)
  14636. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14637. ((org-pos-in-match-range pos 5)
  14638. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14639. ((org-pos-in-match-range pos 9)
  14640. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14641. ((org-pos-in-match-range pos 8)
  14642. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14643. (when ng
  14644. (setq s (concat
  14645. (substring s 0 (match-beginning ng))
  14646. new
  14647. (substring s (match-end ng))))))
  14648. s))
  14649. (defun org-recenter-calendar (date)
  14650. "If the calendar is visible, recenter it to DATE."
  14651. (let* ((win (selected-window))
  14652. (cwin (get-buffer-window "*Calendar*" t))
  14653. (calendar-move-hook nil))
  14654. (when cwin
  14655. (select-window cwin)
  14656. (calendar-goto-date (if (listp date) date
  14657. (calendar-gregorian-from-absolute date)))
  14658. (select-window win))))
  14659. (defun org-goto-calendar (&optional arg)
  14660. "Go to the Emacs calendar at the current date.
  14661. If there is a time stamp in the current line, go to that date.
  14662. A prefix ARG can be used to force the current date."
  14663. (interactive "P")
  14664. (let ((tsr org-ts-regexp) diff
  14665. (calendar-move-hook nil)
  14666. (calendar-view-holidays-initially-flag nil)
  14667. (calendar-view-diary-initially-flag nil))
  14668. (if (or (org-at-timestamp-p)
  14669. (save-excursion
  14670. (beginning-of-line 1)
  14671. (looking-at (concat ".*" tsr))))
  14672. (let ((d1 (time-to-days (current-time)))
  14673. (d2 (time-to-days
  14674. (org-time-string-to-time (match-string 1)))))
  14675. (setq diff (- d2 d1))))
  14676. (calendar)
  14677. (calendar-goto-today)
  14678. (if (and diff (not arg)) (calendar-forward-day diff))))
  14679. (defun org-get-date-from-calendar ()
  14680. "Return a list (month day year) of date at point in calendar."
  14681. (with-current-buffer "*Calendar*"
  14682. (save-match-data
  14683. (calendar-cursor-to-date))))
  14684. (defun org-date-from-calendar ()
  14685. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14686. If there is already a time stamp at the cursor position, update it."
  14687. (interactive)
  14688. (if (org-at-timestamp-p t)
  14689. (org-timestamp-change 0 'calendar)
  14690. (let ((cal-date (org-get-date-from-calendar)))
  14691. (org-insert-time-stamp
  14692. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14693. (defun org-minutes-to-hh:mm-string (m)
  14694. "Compute H:MM from a number of minutes."
  14695. (let ((h (/ m 60)))
  14696. (setq m (- m (* 60 h)))
  14697. (format org-time-clocksum-format h m)))
  14698. (defun org-hh:mm-string-to-minutes (s)
  14699. "Convert a string H:MM to a number of minutes.
  14700. If the string is just a number, interpret it as minutes.
  14701. In fact, the first hh:mm or number in the string will be taken,
  14702. there can be extra stuff in the string.
  14703. If no number is found, the return value is 0."
  14704. (cond
  14705. ((integerp s) s)
  14706. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14707. (+ (* (string-to-number (match-string 1 s)) 60)
  14708. (string-to-number (match-string 2 s))))
  14709. ((string-match "\\([0-9]+\\)" s)
  14710. (string-to-number (match-string 1 s)))
  14711. (t 0)))
  14712. (defcustom org-effort-durations
  14713. `(("h" . 60)
  14714. ("d" . ,(* 60 8))
  14715. ("w" . ,(* 60 8 5))
  14716. ("m" . ,(* 60 8 5 4))
  14717. ("y" . ,(* 60 8 5 40)))
  14718. "Conversion factor to minutes for an effort modifier.
  14719. Each entry has the form (MODIFIER . MINUTES).
  14720. In an effort string, a number followed by MODIFIER is multiplied
  14721. by the specified number of MINUTES to obtain an effort in
  14722. minutes.
  14723. For example, if the value of this variable is ((\"hours\" . 60)), then an
  14724. effort string \"2hours\" is equivalent to 120 minutes."
  14725. :group 'org-agenda
  14726. :type '(alist :key-type (string :tag "Modifier")
  14727. :value-type (number :tag "Minutes")))
  14728. (defun org-duration-string-to-minutes (s)
  14729. "Convert a duration string S to minutes.
  14730. A bare number is interpreted as minutes, modifiers can be set by
  14731. customizing `org-effort-durations' (which see).
  14732. Entries containing a colon are interpreted as H:MM by
  14733. `org-hh:mm-string-to-minutes'."
  14734. (let ((result 0)
  14735. (re (concat "\\([0-9]+\\) *\\("
  14736. (regexp-opt (mapcar 'car org-effort-durations))
  14737. "\\)")))
  14738. (while (string-match re s)
  14739. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  14740. (string-to-number (match-string 1 s))))
  14741. (setq s (replace-match "" nil t s)))
  14742. (incf result (org-hh:mm-string-to-minutes s))
  14743. result))
  14744. ;;;; Files
  14745. (defun org-save-all-org-buffers ()
  14746. "Save all Org-mode buffers without user confirmation."
  14747. (interactive)
  14748. (message "Saving all Org-mode buffers...")
  14749. (save-some-buffers t (lambda () (eq major-mode 'org-mode)))
  14750. (when (featurep 'org-id) (org-id-locations-save))
  14751. (message "Saving all Org-mode buffers... done"))
  14752. (defun org-revert-all-org-buffers ()
  14753. "Revert all Org-mode buffers.
  14754. Prompt for confirmation when there are unsaved changes.
  14755. Be sure you know what you are doing before letting this function
  14756. overwrite your changes.
  14757. This function is useful in a setup where one tracks org files
  14758. with a version control system, to revert on one machine after pulling
  14759. changes from another. I believe the procedure must be like this:
  14760. 1. M-x org-save-all-org-buffers
  14761. 2. Pull changes from the other machine, resolve conflicts
  14762. 3. M-x org-revert-all-org-buffers"
  14763. (interactive)
  14764. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14765. (error "Abort"))
  14766. (save-excursion
  14767. (save-window-excursion
  14768. (mapc
  14769. (lambda (b)
  14770. (when (and (with-current-buffer b (eq major-mode 'org-mode))
  14771. (with-current-buffer b buffer-file-name))
  14772. (org-pop-to-buffer-same-window b)
  14773. (revert-buffer t 'no-confirm)))
  14774. (buffer-list))
  14775. (when (and (featurep 'org-id) org-id-track-globally)
  14776. (org-id-locations-load)))))
  14777. ;;;; Agenda files
  14778. ;;;###autoload
  14779. (defun org-switchb (&optional arg)
  14780. "Switch between Org buffers.
  14781. With one prefix argument, restrict available buffers to files.
  14782. With two prefix arguments, restrict available buffers to agenda files.
  14783. Defaults to `iswitchb' for buffer name completion.
  14784. Set `org-completion-use-ido' to make it use ido instead."
  14785. (interactive "P")
  14786. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14787. ((equal arg '(16)) (org-buffer-list 'agenda))
  14788. (t (org-buffer-list))))
  14789. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14790. (org-completion-use-ido org-completion-use-ido))
  14791. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14792. (setq org-completion-use-iswitchb t))
  14793. (org-pop-to-buffer-same-window
  14794. (org-icompleting-read "Org buffer: "
  14795. (mapcar 'list (mapcar 'buffer-name blist))
  14796. nil t))))
  14797. ;;; Define some older names previously used for this functionality
  14798. ;;;###autoload
  14799. (defalias 'org-ido-switchb 'org-switchb)
  14800. ;;;###autoload
  14801. (defalias 'org-iswitchb 'org-switchb)
  14802. (defun org-buffer-list (&optional predicate exclude-tmp)
  14803. "Return a list of Org buffers.
  14804. PREDICATE can be `export', `files' or `agenda'.
  14805. export restrict the list to Export buffers.
  14806. files restrict the list to buffers visiting Org files.
  14807. agenda restrict the list to buffers visiting agenda files.
  14808. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  14809. (let* ((bfn nil)
  14810. (agenda-files (and (eq predicate 'agenda)
  14811. (mapcar 'file-truename (org-agenda-files t))))
  14812. (filter
  14813. (cond
  14814. ((eq predicate 'files)
  14815. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  14816. ((eq predicate 'export)
  14817. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  14818. ((eq predicate 'agenda)
  14819. (lambda (b)
  14820. (with-current-buffer b
  14821. (and (eq major-mode 'org-mode)
  14822. (setq bfn (buffer-file-name b))
  14823. (member (file-truename bfn) agenda-files)))))
  14824. (t (lambda (b) (with-current-buffer b
  14825. (or (eq major-mode 'org-mode)
  14826. (string-match "\*Org .*Export"
  14827. (buffer-name b)))))))))
  14828. (delq nil
  14829. (mapcar
  14830. (lambda(b)
  14831. (if (and (funcall filter b)
  14832. (or (not exclude-tmp)
  14833. (not (string-match "tmp" (buffer-name b)))))
  14834. b
  14835. nil))
  14836. (buffer-list)))))
  14837. (defun org-agenda-files (&optional unrestricted archives)
  14838. "Get the list of agenda files.
  14839. Optional UNRESTRICTED means return the full list even if a restriction
  14840. is currently in place.
  14841. When ARCHIVES is t, include all archive files that are really being
  14842. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  14843. `org-agenda-archives-mode' is t."
  14844. (let ((files
  14845. (cond
  14846. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  14847. ((stringp org-agenda-files) (org-read-agenda-file-list))
  14848. ((listp org-agenda-files) org-agenda-files)
  14849. (t (error "Invalid value of `org-agenda-files'")))))
  14850. (setq files (apply 'append
  14851. (mapcar (lambda (f)
  14852. (if (file-directory-p f)
  14853. (directory-files
  14854. f t org-agenda-file-regexp)
  14855. (list f)))
  14856. files)))
  14857. (when org-agenda-skip-unavailable-files
  14858. (setq files (delq nil
  14859. (mapcar (function
  14860. (lambda (file)
  14861. (and (file-readable-p file) file)))
  14862. files))))
  14863. (when (or (eq archives t)
  14864. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  14865. (setq files (org-add-archive-files files)))
  14866. files))
  14867. (defun org-agenda-file-p (&optional file)
  14868. "Return non-nil, if FILE is an agenda file.
  14869. If FILE is omitted, use the file associated with the current
  14870. buffer."
  14871. (member (or file (buffer-file-name))
  14872. (org-agenda-files t)))
  14873. (defun org-edit-agenda-file-list ()
  14874. "Edit the list of agenda files.
  14875. Depending on setup, this either uses customize to edit the variable
  14876. `org-agenda-files', or it visits the file that is holding the list. In the
  14877. latter case, the buffer is set up in a way that saving it automatically kills
  14878. the buffer and restores the previous window configuration."
  14879. (interactive)
  14880. (if (stringp org-agenda-files)
  14881. (let ((cw (current-window-configuration)))
  14882. (find-file org-agenda-files)
  14883. (org-set-local 'org-window-configuration cw)
  14884. (org-add-hook 'after-save-hook
  14885. (lambda ()
  14886. (set-window-configuration
  14887. (prog1 org-window-configuration
  14888. (kill-buffer (current-buffer))))
  14889. (org-install-agenda-files-menu)
  14890. (message "New agenda file list installed"))
  14891. nil 'local)
  14892. (message "%s" (substitute-command-keys
  14893. "Edit list and finish with \\[save-buffer]")))
  14894. (customize-variable 'org-agenda-files)))
  14895. (defun org-store-new-agenda-file-list (list)
  14896. "Set new value for the agenda file list and save it correctly."
  14897. (if (stringp org-agenda-files)
  14898. (let ((fe (org-read-agenda-file-list t)) b u)
  14899. (while (setq b (find-buffer-visiting org-agenda-files))
  14900. (kill-buffer b))
  14901. (with-temp-file org-agenda-files
  14902. (insert
  14903. (mapconcat
  14904. (lambda (f) ;; Keep un-expanded entries.
  14905. (if (setq u (assoc f fe))
  14906. (cdr u)
  14907. f))
  14908. list "\n")
  14909. "\n")))
  14910. (let ((org-mode-hook nil) (org-inhibit-startup t)
  14911. (org-insert-mode-line-in-empty-file nil))
  14912. (setq org-agenda-files list)
  14913. (customize-save-variable 'org-agenda-files org-agenda-files))))
  14914. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  14915. "Read the list of agenda files from a file.
  14916. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  14917. filenames, used by `org-store-new-agenda-file-list' to write back
  14918. un-expanded file names."
  14919. (when (file-directory-p org-agenda-files)
  14920. (error "`org-agenda-files' cannot be a single directory"))
  14921. (when (stringp org-agenda-files)
  14922. (with-temp-buffer
  14923. (insert-file-contents org-agenda-files)
  14924. (mapcar
  14925. (lambda (f)
  14926. (let ((e (expand-file-name (substitute-in-file-name f)
  14927. org-directory)))
  14928. (if pair-with-expansion
  14929. (cons e f)
  14930. e)))
  14931. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  14932. ;;;###autoload
  14933. (defun org-cycle-agenda-files ()
  14934. "Cycle through the files in `org-agenda-files'.
  14935. If the current buffer visits an agenda file, find the next one in the list.
  14936. If the current buffer does not, find the first agenda file."
  14937. (interactive)
  14938. (let* ((fs (org-agenda-files t))
  14939. (files (append fs (list (car fs))))
  14940. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  14941. file)
  14942. (unless files (error "No agenda files"))
  14943. (catch 'exit
  14944. (while (setq file (pop files))
  14945. (if (equal (file-truename file) tcf)
  14946. (when (car files)
  14947. (find-file (car files))
  14948. (throw 'exit t))))
  14949. (find-file (car fs)))
  14950. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  14951. (defun org-agenda-file-to-front (&optional to-end)
  14952. "Move/add the current file to the top of the agenda file list.
  14953. If the file is not present in the list, it is added to the front. If it is
  14954. present, it is moved there. With optional argument TO-END, add/move to the
  14955. end of the list."
  14956. (interactive "P")
  14957. (let ((org-agenda-skip-unavailable-files nil)
  14958. (file-alist (mapcar (lambda (x)
  14959. (cons (file-truename x) x))
  14960. (org-agenda-files t)))
  14961. (ctf (file-truename buffer-file-name))
  14962. x had)
  14963. (setq x (assoc ctf file-alist) had x)
  14964. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  14965. (if to-end
  14966. (setq file-alist (append (delq x file-alist) (list x)))
  14967. (setq file-alist (cons x (delq x file-alist))))
  14968. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  14969. (org-install-agenda-files-menu)
  14970. (message "File %s to %s of agenda file list"
  14971. (if had "moved" "added") (if to-end "end" "front"))))
  14972. (defun org-remove-file (&optional file)
  14973. "Remove current file from the list of files in variable `org-agenda-files'.
  14974. These are the files which are being checked for agenda entries.
  14975. Optional argument FILE means use this file instead of the current."
  14976. (interactive)
  14977. (let* ((org-agenda-skip-unavailable-files nil)
  14978. (file (or file buffer-file-name))
  14979. (true-file (file-truename file))
  14980. (afile (abbreviate-file-name file))
  14981. (files (delq nil (mapcar
  14982. (lambda (x)
  14983. (if (equal true-file
  14984. (file-truename x))
  14985. nil x))
  14986. (org-agenda-files t)))))
  14987. (if (not (= (length files) (length (org-agenda-files t))))
  14988. (progn
  14989. (org-store-new-agenda-file-list files)
  14990. (org-install-agenda-files-menu)
  14991. (message "Removed file: %s" afile))
  14992. (message "File was not in list: %s (not removed)" afile))))
  14993. (defun org-file-menu-entry (file)
  14994. (vector file (list 'find-file file) t))
  14995. (defun org-check-agenda-file (file)
  14996. "Make sure FILE exists. If not, ask user what to do."
  14997. (when (not (file-exists-p file))
  14998. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  14999. (abbreviate-file-name file))
  15000. (let ((r (downcase (read-char-exclusive))))
  15001. (cond
  15002. ((equal r ?r)
  15003. (org-remove-file file)
  15004. (throw 'nextfile t))
  15005. (t (error "Abort"))))))
  15006. (defun org-get-agenda-file-buffer (file)
  15007. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15008. it to the list of buffers which might be released later."
  15009. (let ((buf (org-find-base-buffer-visiting file)))
  15010. (if buf
  15011. buf ; just return it
  15012. ;; Make a new buffer and remember it
  15013. (setq buf (find-file-noselect file))
  15014. (if buf (push buf org-agenda-new-buffers))
  15015. buf)))
  15016. (defun org-release-buffers (blist)
  15017. "Release all buffers in list, asking the user for confirmation when needed.
  15018. When a buffer is unmodified, it is just killed. When modified, it is saved
  15019. \(if the user agrees) and then killed."
  15020. (let (buf file)
  15021. (while (setq buf (pop blist))
  15022. (setq file (buffer-file-name buf))
  15023. (when (and (buffer-modified-p buf)
  15024. file
  15025. (y-or-n-p (format "Save file %s? " file)))
  15026. (with-current-buffer buf (save-buffer)))
  15027. (kill-buffer buf))))
  15028. (defun org-prepare-agenda-buffers (files)
  15029. "Create buffers for all agenda files, protect archived trees and comments."
  15030. (interactive)
  15031. (let ((pa '(:org-archived t))
  15032. (pc '(:org-comment t))
  15033. (pall '(:org-archived t :org-comment t))
  15034. (inhibit-read-only t)
  15035. (rea (concat ":" org-archive-tag ":"))
  15036. bmp file re)
  15037. (save-excursion
  15038. (save-restriction
  15039. (while (setq file (pop files))
  15040. (catch 'nextfile
  15041. (if (bufferp file)
  15042. (set-buffer file)
  15043. (org-check-agenda-file file)
  15044. (set-buffer (org-get-agenda-file-buffer file)))
  15045. (widen)
  15046. (setq bmp (buffer-modified-p))
  15047. (org-refresh-category-properties)
  15048. (setq org-todo-keywords-for-agenda
  15049. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15050. (setq org-done-keywords-for-agenda
  15051. (append org-done-keywords-for-agenda org-done-keywords))
  15052. (setq org-todo-keyword-alist-for-agenda
  15053. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15054. (setq org-drawers-for-agenda
  15055. (append org-drawers-for-agenda org-drawers))
  15056. (setq org-tag-alist-for-agenda
  15057. (append org-tag-alist-for-agenda org-tag-alist))
  15058. (save-excursion
  15059. (remove-text-properties (point-min) (point-max) pall)
  15060. (when org-agenda-skip-archived-trees
  15061. (goto-char (point-min))
  15062. (while (re-search-forward rea nil t)
  15063. (if (org-on-heading-p t)
  15064. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15065. (goto-char (point-min))
  15066. (setq re (format org-heading-keyword-regexp-format
  15067. org-comment-string))
  15068. (while (re-search-forward re nil t)
  15069. (add-text-properties
  15070. (match-beginning 0) (org-end-of-subtree t) pc)))
  15071. (set-buffer-modified-p bmp)))))
  15072. (setq org-todo-keywords-for-agenda
  15073. (org-uniquify org-todo-keywords-for-agenda))
  15074. (setq org-todo-keyword-alist-for-agenda
  15075. (org-uniquify org-todo-keyword-alist-for-agenda)
  15076. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15077. ;;;; Embedded LaTeX
  15078. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15079. "Keymap for the minor `org-cdlatex-mode'.")
  15080. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15081. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15082. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15083. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15084. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15085. (defvar org-cdlatex-texmathp-advice-is-done nil
  15086. "Flag remembering if we have applied the advice to texmathp already.")
  15087. (define-minor-mode org-cdlatex-mode
  15088. "Toggle the minor `org-cdlatex-mode'.
  15089. This mode supports entering LaTeX environment and math in LaTeX fragments
  15090. in Org-mode.
  15091. \\{org-cdlatex-mode-map}"
  15092. nil " OCDL" nil
  15093. (when org-cdlatex-mode
  15094. (require 'cdlatex)
  15095. (run-hooks 'cdlatex-mode-hook)
  15096. (cdlatex-compute-tables))
  15097. (unless org-cdlatex-texmathp-advice-is-done
  15098. (setq org-cdlatex-texmathp-advice-is-done t)
  15099. (defadvice texmathp (around org-math-always-on activate)
  15100. "Always return t in org-mode buffers.
  15101. This is because we want to insert math symbols without dollars even outside
  15102. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15103. an embedded LaTeX fragment, let texmathp do its job.
  15104. \\[org-cdlatex-mode-map]"
  15105. (interactive)
  15106. (let (p)
  15107. (cond
  15108. ((not (eq major-mode 'org-mode)) ad-do-it)
  15109. ((eq this-command 'cdlatex-math-symbol)
  15110. (setq ad-return-value t
  15111. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15112. (t
  15113. (let ((p (org-inside-LaTeX-fragment-p)))
  15114. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15115. (setq ad-return-value t
  15116. texmathp-why '("Org-mode embedded math" . 0))
  15117. (if p ad-do-it)))))))))
  15118. (defun turn-on-org-cdlatex ()
  15119. "Unconditionally turn on `org-cdlatex-mode'."
  15120. (org-cdlatex-mode 1))
  15121. (defun org-inside-LaTeX-fragment-p ()
  15122. "Test if point is inside a LaTeX fragment.
  15123. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15124. sequence appearing also before point.
  15125. Even though the matchers for math are configurable, this function assumes
  15126. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15127. delimiters are skipped when they have been removed by customization.
  15128. The return value is nil, or a cons cell with the delimiter and the
  15129. position of this delimiter.
  15130. This function does a reasonably good job, but can locally be fooled by
  15131. for example currency specifications. For example it will assume being in
  15132. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15133. fragments that are properly closed, but during editing, we have to live
  15134. with the uncertainty caused by missing closing delimiters. This function
  15135. looks only before point, not after."
  15136. (catch 'exit
  15137. (let ((pos (point))
  15138. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15139. (lim (progn
  15140. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15141. (point)))
  15142. dd-on str (start 0) m re)
  15143. (goto-char pos)
  15144. (when dodollar
  15145. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15146. re (nth 1 (assoc "$" org-latex-regexps)))
  15147. (while (string-match re str start)
  15148. (cond
  15149. ((= (match-end 0) (length str))
  15150. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15151. ((= (match-end 0) (- (length str) 5))
  15152. (throw 'exit nil))
  15153. (t (setq start (match-end 0))))))
  15154. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15155. (goto-char pos)
  15156. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15157. (and (match-beginning 2) (throw 'exit nil))
  15158. ;; count $$
  15159. (while (re-search-backward "\\$\\$" lim t)
  15160. (setq dd-on (not dd-on)))
  15161. (goto-char pos)
  15162. (if dd-on (cons "$$" m))))))
  15163. (defun org-inside-latex-macro-p ()
  15164. "Is point inside a LaTeX macro or its arguments?"
  15165. (save-match-data
  15166. (org-in-regexp
  15167. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15168. (defun org-try-cdlatex-tab ()
  15169. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15170. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15171. - inside a LaTeX fragment, or
  15172. - after the first word in a line, where an abbreviation expansion could
  15173. insert a LaTeX environment."
  15174. (when org-cdlatex-mode
  15175. (cond
  15176. ;; Before any word on the line: No expansion possible.
  15177. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15178. ;; Just after first word on the line: Expand it. Make sure it
  15179. ;; cannot happen on headlines, though.
  15180. ((save-excursion
  15181. (skip-chars-backward "a-zA-Z0-9*")
  15182. (skip-chars-backward " \t")
  15183. (and (bolp) (not (org-at-heading-p))))
  15184. (cdlatex-tab) t)
  15185. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15186. (defun org-cdlatex-underscore-caret (&optional arg)
  15187. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15188. Revert to the normal definition outside of these fragments."
  15189. (interactive "P")
  15190. (if (org-inside-LaTeX-fragment-p)
  15191. (call-interactively 'cdlatex-sub-superscript)
  15192. (let (org-cdlatex-mode)
  15193. (call-interactively (key-binding (vector last-input-event))))))
  15194. (defun org-cdlatex-math-modify (&optional arg)
  15195. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15196. Revert to the normal definition outside of these fragments."
  15197. (interactive "P")
  15198. (if (org-inside-LaTeX-fragment-p)
  15199. (call-interactively 'cdlatex-math-modify)
  15200. (let (org-cdlatex-mode)
  15201. (call-interactively (key-binding (vector last-input-event))))))
  15202. (defvar org-latex-fragment-image-overlays nil
  15203. "List of overlays carrying the images of latex fragments.")
  15204. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15205. (defun org-remove-latex-fragment-image-overlays ()
  15206. "Remove all overlays with LaTeX fragment images in current buffer."
  15207. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15208. (setq org-latex-fragment-image-overlays nil))
  15209. (defun org-preview-latex-fragment (&optional subtree)
  15210. "Preview the LaTeX fragment at point, or all locally or globally.
  15211. If the cursor is in a LaTeX fragment, create the image and overlay
  15212. it over the source code. If there is no fragment at point, display
  15213. all fragments in the current text, from one headline to the next. With
  15214. prefix SUBTREE, display all fragments in the current subtree. With a
  15215. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15216. the cursor is before the first headline,
  15217. display all fragments in the buffer.
  15218. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15219. (interactive "P")
  15220. (org-remove-latex-fragment-image-overlays)
  15221. (save-excursion
  15222. (save-restriction
  15223. (let (beg end at msg)
  15224. (cond
  15225. ((or (equal subtree '(16))
  15226. (not (save-excursion
  15227. (re-search-backward org-outline-regexp-bol nil t))))
  15228. (setq beg (point-min) end (point-max)
  15229. msg "Creating images for buffer...%s"))
  15230. ((equal subtree '(4))
  15231. (org-back-to-heading)
  15232. (setq beg (point) end (org-end-of-subtree t)
  15233. msg "Creating images for subtree...%s"))
  15234. (t
  15235. (if (setq at (org-inside-LaTeX-fragment-p))
  15236. (goto-char (max (point-min) (- (cdr at) 2)))
  15237. (org-back-to-heading))
  15238. (setq beg (point) end (progn (outline-next-heading) (point))
  15239. msg (if at "Creating image...%s"
  15240. "Creating images for entry...%s"))))
  15241. (message msg "")
  15242. (narrow-to-region beg end)
  15243. (goto-char beg)
  15244. (org-format-latex
  15245. (concat "ltxpng/" (file-name-sans-extension
  15246. (file-name-nondirectory
  15247. buffer-file-name)))
  15248. default-directory 'overlays msg at 'forbuffer 'dvipng)
  15249. (message msg "done. Use `C-c C-c' to remove images.")))))
  15250. (defvar org-latex-regexps
  15251. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15252. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15253. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15254. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15255. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15256. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15257. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15258. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15259. "Regular expressions for matching embedded LaTeX.")
  15260. (defvar org-export-have-math nil) ;; dynamic scoping
  15261. (defun org-format-latex (prefix &optional dir overlays msg at
  15262. forbuffer processing-type)
  15263. "Replace LaTeX fragments with links to an image, and produce images.
  15264. Some of the options can be changed using the variable
  15265. `org-format-latex-options'."
  15266. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15267. (let* ((prefixnodir (file-name-nondirectory prefix))
  15268. (absprefix (expand-file-name prefix dir))
  15269. (todir (file-name-directory absprefix))
  15270. (opt org-format-latex-options)
  15271. (matchers (plist-get opt :matchers))
  15272. (re-list org-latex-regexps)
  15273. (org-format-latex-header-extra
  15274. (plist-get (org-infile-export-plist) :latex-header-extra))
  15275. (cnt 0) txt hash link beg end re e checkdir
  15276. executables-checked string
  15277. m n block-type block linkfile movefile ov)
  15278. ;; Check the different regular expressions
  15279. (while (setq e (pop re-list))
  15280. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15281. block (if block-type "\n\n" ""))
  15282. (when (member m matchers)
  15283. (goto-char (point-min))
  15284. (while (re-search-forward re nil t)
  15285. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15286. (not (get-text-property (match-beginning n)
  15287. 'org-protected))
  15288. (or (not overlays)
  15289. (not (eq (get-char-property (match-beginning n)
  15290. 'org-overlay-type)
  15291. 'org-latex-overlay))))
  15292. (setq org-export-have-math t)
  15293. (cond
  15294. ((eq processing-type 'verbatim)
  15295. ;; Leave the text verbatim, just protect it
  15296. (add-text-properties (match-beginning n) (match-end n)
  15297. '(org-protected t)))
  15298. ((eq processing-type 'mathjax)
  15299. ;; Prepare for MathJax processing
  15300. (setq string (match-string n))
  15301. (if (member m '("$" "$1"))
  15302. (save-excursion
  15303. (delete-region (match-beginning n) (match-end n))
  15304. (goto-char (match-beginning n))
  15305. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15306. "\\)")
  15307. '(org-protected t))))
  15308. (add-text-properties (match-beginning n) (match-end n)
  15309. '(org-protected t))))
  15310. ((eq processing-type 'dvipng)
  15311. ;; Process to an image
  15312. (setq txt (match-string n)
  15313. beg (match-beginning n) end (match-end n)
  15314. cnt (1+ cnt))
  15315. (let (print-length print-level) ; make sure full list is printed
  15316. (setq hash (sha1 (prin1-to-string
  15317. (list org-format-latex-header
  15318. org-format-latex-header-extra
  15319. org-export-latex-default-packages-alist
  15320. org-export-latex-packages-alist
  15321. org-format-latex-options
  15322. forbuffer txt)))
  15323. linkfile (format "%s_%s.png" prefix hash)
  15324. movefile (format "%s_%s.png" absprefix hash)))
  15325. (setq link (concat block "[[file:" linkfile "]]" block))
  15326. (if msg (message msg cnt))
  15327. (goto-char beg)
  15328. (unless checkdir ; make sure the directory exists
  15329. (setq checkdir t)
  15330. (or (file-directory-p todir) (make-directory todir t)))
  15331. (unless executables-checked
  15332. (org-check-external-command
  15333. "latex" "needed to convert LaTeX fragments to images")
  15334. (org-check-external-command
  15335. "dvipng" "needed to convert LaTeX fragments to images")
  15336. (setq executables-checked t))
  15337. (unless (file-exists-p movefile)
  15338. (org-create-formula-image
  15339. txt movefile opt forbuffer))
  15340. (if overlays
  15341. (progn
  15342. (mapc (lambda (o)
  15343. (if (eq (overlay-get o 'org-overlay-type)
  15344. 'org-latex-overlay)
  15345. (delete-overlay o)))
  15346. (overlays-in beg end))
  15347. (setq ov (make-overlay beg end))
  15348. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15349. (if (featurep 'xemacs)
  15350. (progn
  15351. (overlay-put ov 'invisible t)
  15352. (overlay-put
  15353. ov 'end-glyph
  15354. (make-glyph (vector 'png :file movefile))))
  15355. (overlay-put
  15356. ov 'display
  15357. (list 'image :type 'png :file movefile :ascent 'center)))
  15358. (push ov org-latex-fragment-image-overlays)
  15359. (goto-char end))
  15360. (delete-region beg end)
  15361. (insert (org-add-props link
  15362. (list 'org-latex-src
  15363. (replace-regexp-in-string
  15364. "\"" "" txt)
  15365. 'org-latex-src-embed-type
  15366. (if block-type 'paragraph 'character))))))
  15367. ((eq processing-type 'mathml)
  15368. ;; Process to MathML
  15369. (unless executables-checked
  15370. (unless (save-match-data (org-format-latex-mathml-available-p))
  15371. (error "LaTeX to MathML converter not configured"))
  15372. (setq executables-checked t))
  15373. (setq txt (match-string n)
  15374. beg (match-beginning n) end (match-end n)
  15375. cnt (1+ cnt))
  15376. (if msg (message msg cnt))
  15377. (goto-char beg)
  15378. (delete-region beg end)
  15379. (insert (org-format-latex-as-mathml
  15380. txt block-type prefix dir)))
  15381. (t
  15382. (error "Unknown conversion type %s for latex fragments"
  15383. processing-type)))))))))
  15384. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15385. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15386. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15387. sucessful, return the portion between \"<math...> </math>\"
  15388. elements otherwise return nil. When MATHML-FILE is specified,
  15389. write the results in to that file. When invoked as an
  15390. interactive command, prompt for LATEX-FRAG, with initial value
  15391. set to the current active region and echo the results for user
  15392. inspection."
  15393. (interactive (list (let ((frag (when (region-active-p)
  15394. (buffer-substring-no-properties
  15395. (region-beginning) (region-end)))))
  15396. (read-string "LaTeX Fragment: " frag nil frag))))
  15397. (unless latex-frag (error "Invalid latex-frag"))
  15398. (let* ((tmp-in-file (file-relative-name
  15399. (make-temp-name (expand-file-name "ltxmathml-in"))))
  15400. (ignore (write-region latex-frag nil tmp-in-file))
  15401. (tmp-out-file (file-relative-name
  15402. (make-temp-name (expand-file-name "ltxmathml-out"))))
  15403. (cmd (format-spec
  15404. org-latex-to-mathml-convert-command
  15405. `((?j . ,(shell-quote-argument
  15406. (expand-file-name org-latex-to-mathml-jar-file)))
  15407. (?I . ,(shell-quote-argument tmp-in-file))
  15408. (?o . ,(shell-quote-argument tmp-out-file)))))
  15409. mathml shell-command-output)
  15410. (when (org-called-interactively-p 'any)
  15411. (unless (org-format-latex-mathml-available-p)
  15412. (error "LaTeX to MathML converter not configured")))
  15413. (message "Running %s" cmd)
  15414. (setq shell-command-output (shell-command-to-string cmd))
  15415. (setq mathml
  15416. (when (file-readable-p tmp-out-file)
  15417. (with-current-buffer (find-file-noselect tmp-out-file t)
  15418. (goto-char (point-min))
  15419. (when (re-search-forward
  15420. (concat
  15421. (regexp-quote
  15422. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  15423. "\\(.\\|\n\\)*"
  15424. (regexp-quote "</math>")) nil t)
  15425. (prog1 (match-string 0) (kill-buffer))))))
  15426. (cond
  15427. (mathml
  15428. (setq mathml
  15429. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  15430. (when mathml-file
  15431. (write-region mathml nil mathml-file))
  15432. (when (org-called-interactively-p 'any)
  15433. (message mathml)))
  15434. ((message "LaTeX to MathML conversion failed")
  15435. (message shell-command-output)))
  15436. (delete-file tmp-in-file)
  15437. (when (file-exists-p tmp-out-file)
  15438. (delete-file tmp-out-file))
  15439. mathml))
  15440. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  15441. prefix &optional dir)
  15442. "Use `org-create-math-formula' but check local cache first."
  15443. (let* ((absprefix (expand-file-name prefix dir))
  15444. (print-length nil) (print-level nil)
  15445. (formula-id (concat
  15446. "formula-"
  15447. (sha1
  15448. (prin1-to-string
  15449. (list latex-frag
  15450. org-latex-to-mathml-convert-command)))))
  15451. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  15452. (formula-cache-dir (file-name-directory formula-cache)))
  15453. (unless (file-directory-p formula-cache-dir)
  15454. (make-directory formula-cache-dir t))
  15455. (unless (file-exists-p formula-cache)
  15456. (org-create-math-formula latex-frag formula-cache))
  15457. (if (file-exists-p formula-cache)
  15458. ;; Successful conversion. Return the link to MathML file.
  15459. (org-add-props
  15460. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  15461. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  15462. 'org-latex-src-embed-type (if latex-frag-type
  15463. 'paragraph 'character)))
  15464. ;; Failed conversion. Return the LaTeX fragment verbatim
  15465. (add-text-properties
  15466. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  15467. latex-frag)))
  15468. ;; This function borrows from Ganesh Swami's latex2png.el
  15469. (defun org-create-formula-image (string tofile options buffer)
  15470. "This calls dvipng."
  15471. (require 'org-latex)
  15472. (let* ((tmpdir (if (featurep 'xemacs)
  15473. (temp-directory)
  15474. temporary-file-directory))
  15475. (texfilebase (make-temp-name
  15476. (expand-file-name "orgtex" tmpdir)))
  15477. (texfile (concat texfilebase ".tex"))
  15478. (dvifile (concat texfilebase ".dvi"))
  15479. (pngfile (concat texfilebase ".png"))
  15480. (fnh (if (featurep 'xemacs)
  15481. (font-height (get-face-font 'default))
  15482. (face-attribute 'default :height nil)))
  15483. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15484. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15485. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15486. "Black"))
  15487. (bg (or (plist-get options (if buffer :background :html-background))
  15488. "Transparent")))
  15489. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  15490. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  15491. (with-temp-file texfile
  15492. (insert (org-splice-latex-header
  15493. org-format-latex-header
  15494. org-export-latex-default-packages-alist
  15495. org-export-latex-packages-alist t
  15496. org-format-latex-header-extra))
  15497. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  15498. (require 'org-latex)
  15499. (org-export-latex-fix-inputenc))
  15500. (let ((dir default-directory))
  15501. (condition-case nil
  15502. (progn
  15503. (cd tmpdir)
  15504. (call-process "latex" nil nil nil texfile))
  15505. (error nil))
  15506. (cd dir))
  15507. (if (not (file-exists-p dvifile))
  15508. (progn (message "Failed to create dvi file from %s" texfile) nil)
  15509. (condition-case nil
  15510. (call-process "dvipng" nil nil nil
  15511. "-fg" fg "-bg" bg
  15512. "-D" dpi
  15513. ;;"-x" scale "-y" scale
  15514. "-T" "tight"
  15515. "-o" pngfile
  15516. dvifile)
  15517. (error nil))
  15518. (if (not (file-exists-p pngfile))
  15519. (if org-format-latex-signal-error
  15520. (error "Failed to create png file from %s" texfile)
  15521. (message "Failed to create png file from %s" texfile)
  15522. nil)
  15523. ;; Use the requested file name and clean up
  15524. (copy-file pngfile tofile 'replace)
  15525. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  15526. (delete-file (concat texfilebase e)))
  15527. pngfile))))
  15528. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  15529. "Fill a LaTeX header template TPL.
  15530. In the template, the following place holders will be recognized:
  15531. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  15532. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  15533. [PACKAGES] \\usepackage statements for PKG
  15534. [NO-PACKAGES] do not include PKG
  15535. [EXTRA] the string EXTRA
  15536. [NO-EXTRA] do not include EXTRA
  15537. For backward compatibility, if both the positive and the negative place
  15538. holder is missing, the positive one (without the \"NO-\") will be
  15539. assumed to be present at the end of the template.
  15540. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  15541. EXTRA is a string.
  15542. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  15543. (let (rpl (end ""))
  15544. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  15545. (setq rpl (if (or (match-end 1) (not def-pkg))
  15546. "" (org-latex-packages-to-string def-pkg snippets-p t))
  15547. tpl (replace-match rpl t t tpl))
  15548. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  15549. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  15550. (setq rpl (if (or (match-end 1) (not pkg))
  15551. "" (org-latex-packages-to-string pkg snippets-p t))
  15552. tpl (replace-match rpl t t tpl))
  15553. (if pkg (setq end
  15554. (concat end "\n"
  15555. (org-latex-packages-to-string pkg snippets-p)))))
  15556. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  15557. (setq rpl (if (or (match-end 1) (not extra))
  15558. "" (concat extra "\n"))
  15559. tpl (replace-match rpl t t tpl))
  15560. (if (and extra (string-match "\\S-" extra))
  15561. (setq end (concat end "\n" extra))))
  15562. (if (string-match "\\S-" end)
  15563. (concat tpl "\n" end)
  15564. tpl)))
  15565. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  15566. "Turn an alist of packages into a string with the \\usepackage macros."
  15567. (setq pkg (mapconcat (lambda(p)
  15568. (cond
  15569. ((stringp p) p)
  15570. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  15571. (format "%% Package %s omitted" (cadr p)))
  15572. ((equal "" (car p))
  15573. (format "\\usepackage{%s}" (cadr p)))
  15574. (t
  15575. (format "\\usepackage[%s]{%s}"
  15576. (car p) (cadr p)))))
  15577. pkg
  15578. "\n"))
  15579. (if newline (concat pkg "\n") pkg))
  15580. (defun org-dvipng-color (attr)
  15581. "Return an rgb color specification for dvipng."
  15582. (apply 'format "rgb %s %s %s"
  15583. (mapcar 'org-normalize-color
  15584. (color-values (face-attribute 'default attr nil)))))
  15585. (defun org-normalize-color (value)
  15586. "Return string to be used as color value for an RGB component."
  15587. (format "%g" (/ value 65535.0)))
  15588. ;; Image display
  15589. (defvar org-inline-image-overlays nil)
  15590. (make-variable-buffer-local 'org-inline-image-overlays)
  15591. (defun org-toggle-inline-images (&optional include-linked)
  15592. "Toggle the display of inline images.
  15593. INCLUDE-LINKED is passed to `org-display-inline-images'."
  15594. (interactive "P")
  15595. (if org-inline-image-overlays
  15596. (progn
  15597. (org-remove-inline-images)
  15598. (message "Inline image display turned off"))
  15599. (org-display-inline-images include-linked)
  15600. (if org-inline-image-overlays
  15601. (message "%d images displayed inline"
  15602. (length org-inline-image-overlays))
  15603. (message "No images to display inline"))))
  15604. (defun org-display-inline-images (&optional include-linked refresh beg end)
  15605. "Display inline images.
  15606. Normally only links without a description part are inlined, because this
  15607. is how it will work for export. When INCLUDE-LINKED is set, also links
  15608. with a description part will be inlined. This can be nice for a quick
  15609. look at those images, but it does not reflect what exported files will look
  15610. like.
  15611. When REFRESH is set, refresh existing images between BEG and END.
  15612. This will create new image displays only if necessary.
  15613. BEG and END default to the buffer boundaries."
  15614. (interactive "P")
  15615. (unless refresh
  15616. (org-remove-inline-images)
  15617. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  15618. (save-excursion
  15619. (save-restriction
  15620. (widen)
  15621. (setq beg (or beg (point-min)) end (or end (point-max)))
  15622. (goto-char (point-min))
  15623. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  15624. (substring (org-image-file-name-regexp) 0 -2)
  15625. "\\)\\]" (if include-linked "" "\\]")))
  15626. old file ov img)
  15627. (while (re-search-forward re end t)
  15628. (setq old (get-char-property-and-overlay (match-beginning 1)
  15629. 'org-image-overlay))
  15630. (setq file (expand-file-name
  15631. (concat (or (match-string 3) "") (match-string 4))))
  15632. (when (file-exists-p file)
  15633. (if (and (car-safe old) refresh)
  15634. (image-refresh (overlay-get (cdr old) 'display))
  15635. (setq img (save-match-data (create-image file)))
  15636. (when img
  15637. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  15638. (overlay-put ov 'display img)
  15639. (overlay-put ov 'face 'default)
  15640. (overlay-put ov 'org-image-overlay t)
  15641. (overlay-put ov 'modification-hooks
  15642. (list 'org-display-inline-modification-hook))
  15643. (push ov org-inline-image-overlays)))))))))
  15644. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  15645. "Remove inline-display overlay if a corresponding region is modified."
  15646. (let ((inhibit-modification-hooks t))
  15647. (when (and ov after)
  15648. (delete ov org-inline-image-overlays)
  15649. (delete-overlay ov))))
  15650. (defun org-remove-inline-images ()
  15651. "Remove inline display of images."
  15652. (interactive)
  15653. (mapc 'delete-overlay org-inline-image-overlays)
  15654. (setq org-inline-image-overlays nil))
  15655. ;;;; Key bindings
  15656. ;; Outline functions from `outline-mode-prefix-map'
  15657. ;; that can be remapped in Org:
  15658. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  15659. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  15660. (define-key org-mode-map [remap outline-forward-same-level]
  15661. 'org-forward-same-level)
  15662. (define-key org-mode-map [remap outline-backward-same-level]
  15663. 'org-backward-same-level)
  15664. (define-key org-mode-map [remap show-branches]
  15665. 'org-kill-note-or-show-branches)
  15666. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  15667. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  15668. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  15669. ;; Outline functions from `outline-mode-prefix-map'
  15670. ;; that can not be remapped in Org:
  15671. ;; - the column "key binding" shows whether the Outline function is still
  15672. ;; available in Org mode on the same key that it has been bound to in
  15673. ;; Outline mode:
  15674. ;; - "overridden": key used for a different functionality in Org mode
  15675. ;; - else: key still bound to the same Outline function in Org mode
  15676. ;; | Outline function | key binding | Org replacement |
  15677. ;; |------------------------------------+-------------+-----------------------|
  15678. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  15679. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  15680. ;; | `show-children' | `C-c C-i' | visibility cycling |
  15681. ;; | `hide-subtree' | overridden | visibility cycling |
  15682. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  15683. ;; | `hide-body' | overridden | no replacement |
  15684. ;; | `show-all' | overridden | no replacement |
  15685. ;; | `hide-entry' | overridden | visibility cycling |
  15686. ;; | `show-entry' | overridden | no replacement |
  15687. ;; | `hide-leaves' | overridden | no replacement |
  15688. ;; | `hide-sublevels' | overridden | no replacement |
  15689. ;; | `hide-other' | overridden | no replacement |
  15690. ;; | `outline-move-subtree-up' | `C-c C-^' | better: org-shiftup |
  15691. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  15692. ;; Make `C-c C-x' a prefix key
  15693. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  15694. ;; TAB key with modifiers
  15695. (org-defkey org-mode-map "\C-i" 'org-cycle)
  15696. (org-defkey org-mode-map [(tab)] 'org-cycle)
  15697. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  15698. (org-defkey org-mode-map [(meta tab)] 'pcomplete)
  15699. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  15700. (org-defkey org-mode-map "\M-\C-i" 'pcomplete)
  15701. ;; The following line is necessary under Suse GNU/Linux
  15702. (unless (featurep 'xemacs)
  15703. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  15704. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  15705. (define-key org-mode-map [backtab] 'org-shifttab)
  15706. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  15707. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  15708. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  15709. ;; Cursor keys with modifiers
  15710. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  15711. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  15712. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  15713. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  15714. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  15715. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  15716. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  15717. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  15718. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  15719. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  15720. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  15721. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  15722. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  15723. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  15724. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  15725. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  15726. ;; Babel keys
  15727. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  15728. (mapc (lambda (pair)
  15729. (define-key org-babel-map (car pair) (cdr pair)))
  15730. org-babel-key-bindings)
  15731. ;;; Extra keys for tty access.
  15732. ;; We only set them when really needed because otherwise the
  15733. ;; menus don't show the simple keys
  15734. (when (or org-use-extra-keys
  15735. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  15736. (not window-system))
  15737. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  15738. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  15739. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  15740. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  15741. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  15742. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  15743. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  15744. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  15745. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  15746. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  15747. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  15748. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  15749. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  15750. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  15751. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  15752. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  15753. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  15754. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  15755. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  15756. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  15757. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  15758. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  15759. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  15760. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  15761. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  15762. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  15763. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  15764. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  15765. ;; All the other keys
  15766. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  15767. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  15768. (if (boundp 'narrow-map)
  15769. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  15770. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  15771. (if (boundp 'narrow-map)
  15772. (org-defkey narrow-map "b" 'org-narrow-to-block)
  15773. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  15774. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  15775. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  15776. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  15777. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  15778. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  15779. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  15780. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  15781. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  15782. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  15783. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  15784. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  15785. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  15786. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  15787. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  15788. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  15789. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  15790. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  15791. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  15792. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  15793. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  15794. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  15795. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  15796. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  15797. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  15798. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  15799. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  15800. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  15801. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  15802. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  15803. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  15804. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  15805. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  15806. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  15807. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  15808. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  15809. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  15810. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  15811. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  15812. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  15813. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  15814. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  15815. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15816. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  15817. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  15818. (org-defkey org-mode-map "\C-c^" 'org-sort)
  15819. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  15820. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  15821. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  15822. (org-defkey org-mode-map "\C-m" 'org-return)
  15823. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  15824. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  15825. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  15826. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  15827. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  15828. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  15829. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  15830. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  15831. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  15832. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  15833. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  15834. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  15835. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  15836. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  15837. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  15838. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  15839. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  15840. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  15841. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  15842. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  15843. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  15844. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  15845. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  15846. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  15847. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  15848. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  15849. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  15850. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  15851. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  15852. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  15853. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  15854. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  15855. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  15856. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  15857. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  15858. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  15859. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  15860. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  15861. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  15862. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  15863. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  15864. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  15865. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  15866. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  15867. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  15868. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  15869. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  15870. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  15871. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  15872. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  15873. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  15874. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  15875. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  15876. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  15877. (when (featurep 'xemacs)
  15878. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  15879. (defconst org-speed-commands-default
  15880. '(
  15881. ("Outline Navigation")
  15882. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  15883. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  15884. ("f" . (org-speed-move-safe 'org-forward-same-level))
  15885. ("b" . (org-speed-move-safe 'org-backward-same-level))
  15886. ("u" . (org-speed-move-safe 'outline-up-heading))
  15887. ("j" . org-goto)
  15888. ("g" . (org-refile t))
  15889. ("Outline Visibility")
  15890. ("c" . org-cycle)
  15891. ("C" . org-shifttab)
  15892. (" " . org-display-outline-path)
  15893. ("Outline Structure Editing")
  15894. ("U" . org-shiftmetaup)
  15895. ("D" . org-shiftmetadown)
  15896. ("r" . org-metaright)
  15897. ("l" . org-metaleft)
  15898. ("R" . org-shiftmetaright)
  15899. ("L" . org-shiftmetaleft)
  15900. ("i" . (progn (forward-char 1) (call-interactively
  15901. 'org-insert-heading-respect-content)))
  15902. ("^" . org-sort)
  15903. ("w" . org-refile)
  15904. ("a" . org-archive-subtree-default-with-confirmation)
  15905. ("." . org-mark-subtree)
  15906. ("Clock Commands")
  15907. ("I" . org-clock-in)
  15908. ("O" . org-clock-out)
  15909. ("Meta Data Editing")
  15910. ("t" . org-todo)
  15911. ("0" . (org-priority ?\ ))
  15912. ("1" . (org-priority ?A))
  15913. ("2" . (org-priority ?B))
  15914. ("3" . (org-priority ?C))
  15915. (";" . org-set-tags-command)
  15916. ("e" . org-set-effort)
  15917. ("Agenda Views etc")
  15918. ("v" . org-agenda)
  15919. ("/" . org-sparse-tree)
  15920. ("Misc")
  15921. ("o" . org-open-at-point)
  15922. ("?" . org-speed-command-help)
  15923. ("<" . (org-agenda-set-restriction-lock 'subtree))
  15924. (">" . (org-agenda-remove-restriction-lock))
  15925. )
  15926. "The default speed commands.")
  15927. (defun org-print-speed-command (e)
  15928. (if (> (length (car e)) 1)
  15929. (progn
  15930. (princ "\n")
  15931. (princ (car e))
  15932. (princ "\n")
  15933. (princ (make-string (length (car e)) ?-))
  15934. (princ "\n"))
  15935. (princ (car e))
  15936. (princ " ")
  15937. (if (symbolp (cdr e))
  15938. (princ (symbol-name (cdr e)))
  15939. (prin1 (cdr e)))
  15940. (princ "\n")))
  15941. (defun org-speed-command-help ()
  15942. "Show the available speed commands."
  15943. (interactive)
  15944. (if (not org-use-speed-commands)
  15945. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  15946. (with-output-to-temp-buffer "*Help*"
  15947. (princ "User-defined Speed commands\n===========================\n")
  15948. (mapc 'org-print-speed-command org-speed-commands-user)
  15949. (princ "\n")
  15950. (princ "Built-in Speed commands\n=======================\n")
  15951. (mapc 'org-print-speed-command org-speed-commands-default))
  15952. (with-current-buffer "*Help*"
  15953. (setq truncate-lines t))))
  15954. (defun org-speed-move-safe (cmd)
  15955. "Execute CMD, but make sure that the cursor always ends up in a headline.
  15956. If not, return to the original position and throw an error."
  15957. (interactive)
  15958. (let ((pos (point)))
  15959. (call-interactively cmd)
  15960. (unless (and (bolp) (org-on-heading-p))
  15961. (goto-char pos)
  15962. (error "Boundary reached while executing %s" cmd))))
  15963. (defvar org-self-insert-command-undo-counter 0)
  15964. (defvar org-table-auto-blank-field) ; defined in org-table.el
  15965. (defvar org-speed-command nil)
  15966. (defun org-speed-command-default-hook (keys)
  15967. "Hook for activating single-letter speed commands.
  15968. `org-speed-commands-default' specifies a minimal command set.
  15969. Use `org-speed-commands-user' for further customization."
  15970. (when (or (and (bolp) (looking-at org-outline-regexp))
  15971. (and (functionp org-use-speed-commands)
  15972. (funcall org-use-speed-commands)))
  15973. (cdr (assoc keys (append org-speed-commands-user
  15974. org-speed-commands-default)))))
  15975. (defun org-babel-speed-command-hook (keys)
  15976. "Hook for activating single-letter code block commands."
  15977. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  15978. (cdr (assoc keys org-babel-key-bindings))))
  15979. (defcustom org-speed-command-hook
  15980. '(org-speed-command-default-hook org-babel-speed-command-hook)
  15981. "Hook for activating speed commands at strategic locations.
  15982. Hook functions are called in sequence until a valid handler is
  15983. found.
  15984. Each hook takes a single argument, a user-pressed command key
  15985. which is also a `self-insert-command' from the global map.
  15986. Within the hook, examine the cursor position and the command key
  15987. and return nil or a valid handler as appropriate. Handler could
  15988. be one of an interactive command, a function, or a form.
  15989. Set `org-use-speed-commands' to non-nil value to enable this
  15990. hook. The default setting is `org-speed-command-default-hook'."
  15991. :group 'org-structure
  15992. :type 'hook)
  15993. (defun org-self-insert-command (N)
  15994. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  15995. If the cursor is in a table looking at whitespace, the whitespace is
  15996. overwritten, and the table is not marked as requiring realignment."
  15997. (interactive "p")
  15998. (org-check-before-invisible-edit 'insert)
  15999. (cond
  16000. ((and org-use-speed-commands
  16001. (setq org-speed-command
  16002. (run-hook-with-args-until-success
  16003. 'org-speed-command-hook (this-command-keys))))
  16004. (cond
  16005. ((commandp org-speed-command)
  16006. (setq this-command org-speed-command)
  16007. (call-interactively org-speed-command))
  16008. ((functionp org-speed-command)
  16009. (funcall org-speed-command))
  16010. ((and org-speed-command (listp org-speed-command))
  16011. (eval org-speed-command))
  16012. (t (let (org-use-speed-commands)
  16013. (call-interactively 'org-self-insert-command)))))
  16014. ((and
  16015. (org-table-p)
  16016. (progn
  16017. ;; check if we blank the field, and if that triggers align
  16018. (and (featurep 'org-table) org-table-auto-blank-field
  16019. (member last-command
  16020. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16021. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16022. ;; got extra space, this field does not determine column width
  16023. (let (org-table-may-need-update) (org-table-blank-field))
  16024. ;; no extra space, this field may determine column width
  16025. (org-table-blank-field)))
  16026. t)
  16027. (eq N 1)
  16028. (looking-at "[^|\n]* |"))
  16029. (let (org-table-may-need-update)
  16030. (goto-char (1- (match-end 0)))
  16031. (delete-char -1)
  16032. (goto-char (match-beginning 0))
  16033. (self-insert-command N)))
  16034. (t
  16035. (setq org-table-may-need-update t)
  16036. (self-insert-command N)
  16037. (org-fix-tags-on-the-fly)
  16038. (if org-self-insert-cluster-for-undo
  16039. (if (not (eq last-command 'org-self-insert-command))
  16040. (setq org-self-insert-command-undo-counter 1)
  16041. (if (>= org-self-insert-command-undo-counter 20)
  16042. (setq org-self-insert-command-undo-counter 1)
  16043. (and (> org-self-insert-command-undo-counter 0)
  16044. buffer-undo-list (listp buffer-undo-list)
  16045. (not (cadr buffer-undo-list)) ; remove nil entry
  16046. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16047. (setq org-self-insert-command-undo-counter
  16048. (1+ org-self-insert-command-undo-counter))))))))
  16049. (defun org-check-before-invisible-edit (kind)
  16050. "Check is editing if kind KIND would be dangerous with invisible text around.
  16051. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16052. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16053. (if (and org-catch-invisible-edits
  16054. (or (not (boundp 'visible-mode)) (not visible-mode))
  16055. (or (get-char-property (point) 'invisible)
  16056. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16057. ;; OK, we need to take a closer look
  16058. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16059. (invisible-before-point (if (bobp) nil (get-char-property
  16060. (1- (point)) 'invisible)))
  16061. (border-and-ok-direction
  16062. (or
  16063. ;; Check if we are acting predictably before invisible text
  16064. (and invisible-at-point (not invisible-before-point)
  16065. (memq kind '(insert delete-backward)))
  16066. ;; Check if we are acting predictably after invisible text
  16067. ;; This works not well, and I have turned it off. It seems
  16068. ;; better to always show and stop after invisible text.
  16069. ;; (and (not invisible-at-point) invisible-before-point
  16070. ;; (memq kind '(insert delete)))
  16071. )))
  16072. (when (or (memq invisible-at-point '(outline org-hide-block))
  16073. (memq invisible-before-point '(outline org-hide-block)))
  16074. (if (eq org-catch-invisible-edits 'error)
  16075. (error "Editing in invisible areas is prohibited - make visible first"))
  16076. ;; Make the area visible
  16077. (save-excursion
  16078. (if invisible-before-point
  16079. (goto-char (previous-single-char-property-change
  16080. (point) 'invisible)))
  16081. (org-cycle))
  16082. (cond
  16083. ((eq org-catch-invisible-edits 'show)
  16084. ;; That's it, we do the edit after showing
  16085. (message
  16086. "Unfolding invisible region around point before editing")
  16087. (sit-for 1))
  16088. ((and (eq org-catch-invisible-edits 'smart)
  16089. border-and-ok-direction)
  16090. (message "Unfolding invisible region around point before editing"))
  16091. (t
  16092. ;; Don't do the edit, make the user repeat it in full visibility
  16093. (error "Edit in invisible region aborted, repeat to confirm with text visible")))))))
  16094. (defun org-fix-tags-on-the-fly ()
  16095. (when (and (equal (char-after (point-at-bol)) ?*)
  16096. (org-on-heading-p))
  16097. (org-align-tags-here org-tags-column)))
  16098. (defun org-delete-backward-char (N)
  16099. "Like `delete-backward-char', insert whitespace at field end in tables.
  16100. When deleting backwards, in tables this function will insert whitespace in
  16101. front of the next \"|\" separator, to keep the table aligned. The table will
  16102. still be marked for re-alignment if the field did fill the entire column,
  16103. because, in this case the deletion might narrow the column."
  16104. (interactive "p")
  16105. (org-check-before-invisible-edit 'delete-backward)
  16106. (if (and (org-table-p)
  16107. (eq N 1)
  16108. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16109. (looking-at ".*?|"))
  16110. (let ((pos (point))
  16111. (noalign (looking-at "[^|\n\r]* |"))
  16112. (c org-table-may-need-update))
  16113. (backward-delete-char N)
  16114. (if (not overwrite-mode)
  16115. (progn
  16116. (skip-chars-forward "^|")
  16117. (insert " ")
  16118. (goto-char (1- pos))))
  16119. ;; noalign: if there were two spaces at the end, this field
  16120. ;; does not determine the width of the column.
  16121. (if noalign (setq org-table-may-need-update c)))
  16122. (backward-delete-char N)
  16123. (org-fix-tags-on-the-fly)))
  16124. (defun org-delete-char (N)
  16125. "Like `delete-char', but insert whitespace at field end in tables.
  16126. When deleting characters, in tables this function will insert whitespace in
  16127. front of the next \"|\" separator, to keep the table aligned. The table will
  16128. still be marked for re-alignment if the field did fill the entire column,
  16129. because, in this case the deletion might narrow the column."
  16130. (interactive "p")
  16131. (org-check-before-invisible-edit 'delete)
  16132. (if (and (org-table-p)
  16133. (not (bolp))
  16134. (not (= (char-after) ?|))
  16135. (eq N 1))
  16136. (if (looking-at ".*?|")
  16137. (let ((pos (point))
  16138. (noalign (looking-at "[^|\n\r]* |"))
  16139. (c org-table-may-need-update))
  16140. (replace-match (concat
  16141. (substring (match-string 0) 1 -1)
  16142. " |"))
  16143. (goto-char pos)
  16144. ;; noalign: if there were two spaces at the end, this field
  16145. ;; does not determine the width of the column.
  16146. (if noalign (setq org-table-may-need-update c)))
  16147. (delete-char N))
  16148. (delete-char N)
  16149. (org-fix-tags-on-the-fly)))
  16150. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16151. (put 'org-self-insert-command 'delete-selection t)
  16152. (put 'orgtbl-self-insert-command 'delete-selection t)
  16153. (put 'org-delete-char 'delete-selection 'supersede)
  16154. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16155. (put 'org-yank 'delete-selection 'yank)
  16156. ;; Make `flyspell-mode' delay after some commands
  16157. (put 'org-self-insert-command 'flyspell-delayed t)
  16158. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16159. (put 'org-delete-char 'flyspell-delayed t)
  16160. (put 'org-delete-backward-char 'flyspell-delayed t)
  16161. ;; Make pabbrev-mode expand after org-mode commands
  16162. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16163. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16164. ;; How to do this: Measure non-white length of current string
  16165. ;; If equal to column width, we should realign.
  16166. (defun org-remap (map &rest commands)
  16167. "In MAP, remap the functions given in COMMANDS.
  16168. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16169. (let (new old)
  16170. (while commands
  16171. (setq old (pop commands) new (pop commands))
  16172. (if (fboundp 'command-remapping)
  16173. (org-defkey map (vector 'remap old) new)
  16174. (substitute-key-definition old new map global-map)))))
  16175. (when (eq org-enable-table-editor 'optimized)
  16176. ;; If the user wants maximum table support, we need to hijack
  16177. ;; some standard editing functions
  16178. (org-remap org-mode-map
  16179. 'self-insert-command 'org-self-insert-command
  16180. 'delete-char 'org-delete-char
  16181. 'delete-backward-char 'org-delete-backward-char)
  16182. (org-defkey org-mode-map "|" 'org-force-self-insert))
  16183. (defvar org-ctrl-c-ctrl-c-hook nil
  16184. "Hook for functions attaching themselves to `C-c C-c'.
  16185. This can be used to add additional functionality to the C-c C-c
  16186. key which executes context-dependent commands. This hook is run
  16187. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  16188. run after the last test.
  16189. Each function will be called with no arguments. The function
  16190. must check if the context is appropriate for it to act. If yes,
  16191. it should do its thing and then return a non-nil value. If the
  16192. context is wrong, just do nothing and return nil.")
  16193. (defvar org-ctrl-c-ctrl-c-final-hook nil
  16194. "Hook for functions attaching themselves to `C-c C-c'.
  16195. This can be used to add additional functionality to the C-c C-c
  16196. key which executes context-dependent commands. This hook is run
  16197. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  16198. before the first test.
  16199. Each function will be called with no arguments. The function
  16200. must check if the context is appropriate for it to act. If yes,
  16201. it should do its thing and then return a non-nil value. If the
  16202. context is wrong, just do nothing and return nil.")
  16203. (defvar org-tab-first-hook nil
  16204. "Hook for functions to attach themselves to TAB.
  16205. See `org-ctrl-c-ctrl-c-hook' for more information.
  16206. This hook runs as the first action when TAB is pressed, even before
  16207. `org-cycle' messes around with the `outline-regexp' to cater for
  16208. inline tasks and plain list item folding.
  16209. If any function in this hook returns t, any other actions that
  16210. would have been caused by TAB (such as table field motion or visibility
  16211. cycling) will not occur.")
  16212. (defvar org-tab-after-check-for-table-hook nil
  16213. "Hook for functions to attach themselves to TAB.
  16214. See `org-ctrl-c-ctrl-c-hook' for more information.
  16215. This hook runs after it has been established that the cursor is not in a
  16216. table, but before checking if the cursor is in a headline or if global cycling
  16217. should be done.
  16218. If any function in this hook returns t, not other actions like visibility
  16219. cycling will be done.")
  16220. (defvar org-tab-after-check-for-cycling-hook nil
  16221. "Hook for functions to attach themselves to TAB.
  16222. See `org-ctrl-c-ctrl-c-hook' for more information.
  16223. This hook runs after it has been established that not table field motion and
  16224. not visibility should be done because of current context. This is probably
  16225. the place where a package like yasnippets can hook in.")
  16226. (defvar org-tab-before-tab-emulation-hook nil
  16227. "Hook for functions to attach themselves to TAB.
  16228. See `org-ctrl-c-ctrl-c-hook' for more information.
  16229. This hook runs after every other options for TAB have been exhausted, but
  16230. before indentation and \t insertion takes place.")
  16231. (defvar org-metaleft-hook nil
  16232. "Hook for functions attaching themselves to `M-left'.
  16233. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16234. (defvar org-metaright-hook nil
  16235. "Hook for functions attaching themselves to `M-right'.
  16236. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16237. (defvar org-metaup-hook nil
  16238. "Hook for functions attaching themselves to `M-up'.
  16239. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16240. (defvar org-metadown-hook nil
  16241. "Hook for functions attaching themselves to `M-down'.
  16242. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16243. (defvar org-shiftmetaleft-hook nil
  16244. "Hook for functions attaching themselves to `M-S-left'.
  16245. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16246. (defvar org-shiftmetaright-hook nil
  16247. "Hook for functions attaching themselves to `M-S-right'.
  16248. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16249. (defvar org-shiftmetaup-hook nil
  16250. "Hook for functions attaching themselves to `M-S-up'.
  16251. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16252. (defvar org-shiftmetadown-hook nil
  16253. "Hook for functions attaching themselves to `M-S-down'.
  16254. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16255. (defvar org-metareturn-hook nil
  16256. "Hook for functions attaching themselves to `M-RET'.
  16257. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16258. (defvar org-shiftup-hook nil
  16259. "Hook for functions attaching themselves to `S-up'.
  16260. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16261. (defvar org-shiftup-final-hook nil
  16262. "Hook for functions attaching themselves to `S-up'.
  16263. This one runs after all other options except shift-select have been excluded.
  16264. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16265. (defvar org-shiftdown-hook nil
  16266. "Hook for functions attaching themselves to `S-down'.
  16267. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16268. (defvar org-shiftdown-final-hook nil
  16269. "Hook for functions attaching themselves to `S-down'.
  16270. This one runs after all other options except shift-select have been excluded.
  16271. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16272. (defvar org-shiftleft-hook nil
  16273. "Hook for functions attaching themselves to `S-left'.
  16274. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16275. (defvar org-shiftleft-final-hook nil
  16276. "Hook for functions attaching themselves to `S-left'.
  16277. This one runs after all other options except shift-select have been excluded.
  16278. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16279. (defvar org-shiftright-hook nil
  16280. "Hook for functions attaching themselves to `S-right'.
  16281. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16282. (defvar org-shiftright-final-hook nil
  16283. "Hook for functions attaching themselves to `S-right'.
  16284. This one runs after all other options except shift-select have been excluded.
  16285. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16286. (defun org-modifier-cursor-error ()
  16287. "Throw an error, a modified cursor command was applied in wrong context."
  16288. (error "This command is active in special context like tables, headlines or items"))
  16289. (defun org-shiftselect-error ()
  16290. "Throw an error because Shift-Cursor command was applied in wrong context."
  16291. (if (and (boundp 'shift-select-mode) shift-select-mode)
  16292. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  16293. (error "This command works only in special context like headlines or timestamps")))
  16294. (defun org-call-for-shift-select (cmd)
  16295. (let ((this-command-keys-shift-translated t))
  16296. (call-interactively cmd)))
  16297. (defun org-shifttab (&optional arg)
  16298. "Global visibility cycling or move to previous table field.
  16299. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  16300. on context.
  16301. See the individual commands for more information."
  16302. (interactive "P")
  16303. (cond
  16304. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  16305. ((integerp arg)
  16306. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  16307. (message "Content view to level: %d" arg)
  16308. (org-content (prefix-numeric-value arg2))
  16309. (setq org-cycle-global-status 'overview)))
  16310. (t (call-interactively 'org-global-cycle))))
  16311. (defun org-shiftmetaleft ()
  16312. "Promote subtree or delete table column.
  16313. Calls `org-promote-subtree', `org-outdent-item',
  16314. or `org-table-delete-column', depending on context.
  16315. See the individual commands for more information."
  16316. (interactive)
  16317. (cond
  16318. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  16319. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  16320. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  16321. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  16322. (t (org-modifier-cursor-error))))
  16323. (defun org-shiftmetaright ()
  16324. "Demote subtree or insert table column.
  16325. Calls `org-demote-subtree', `org-indent-item',
  16326. or `org-table-insert-column', depending on context.
  16327. See the individual commands for more information."
  16328. (interactive)
  16329. (cond
  16330. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  16331. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  16332. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  16333. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  16334. (t (org-modifier-cursor-error))))
  16335. (defun org-shiftmetaup (&optional arg)
  16336. "Move subtree up or kill table row.
  16337. Calls `org-move-subtree-up' or `org-table-kill-row' or
  16338. `org-move-item-up' depending on context. See the individual commands
  16339. for more information."
  16340. (interactive "P")
  16341. (cond
  16342. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  16343. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  16344. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  16345. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16346. (t (org-modifier-cursor-error))))
  16347. (defun org-shiftmetadown (&optional arg)
  16348. "Move subtree down or insert table row.
  16349. Calls `org-move-subtree-down' or `org-table-insert-row' or
  16350. `org-move-item-down', depending on context. See the individual
  16351. commands for more information."
  16352. (interactive "P")
  16353. (cond
  16354. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  16355. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  16356. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  16357. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16358. (t (org-modifier-cursor-error))))
  16359. (defsubst org-hidden-tree-error ()
  16360. (error
  16361. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  16362. (defun org-metaleft (&optional arg)
  16363. "Promote heading or move table column to left.
  16364. Calls `org-do-promote' or `org-table-move-column', depending on context.
  16365. With no specific context, calls the Emacs default `backward-word'.
  16366. See the individual commands for more information."
  16367. (interactive "P")
  16368. (cond
  16369. ((run-hook-with-args-until-success 'org-metaleft-hook))
  16370. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  16371. ((org-with-limited-levels
  16372. (or (org-on-heading-p)
  16373. (and (org-region-active-p)
  16374. (save-excursion
  16375. (goto-char (region-beginning))
  16376. (org-on-heading-p)))))
  16377. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16378. (call-interactively 'org-do-promote))
  16379. ;; At an inline task.
  16380. ((org-on-heading-p)
  16381. (call-interactively 'org-inlinetask-promote))
  16382. ((or (org-at-item-p)
  16383. (and (org-region-active-p)
  16384. (save-excursion
  16385. (goto-char (region-beginning))
  16386. (org-at-item-p))))
  16387. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16388. (call-interactively 'org-outdent-item))
  16389. (t (call-interactively 'backward-word))))
  16390. (defun org-metaright (&optional arg)
  16391. "Demote subtree or move table column to right.
  16392. Calls `org-do-demote' or `org-table-move-column', depending on context.
  16393. With no specific context, calls the Emacs default `forward-word'.
  16394. See the individual commands for more information."
  16395. (interactive "P")
  16396. (cond
  16397. ((run-hook-with-args-until-success 'org-metaright-hook))
  16398. ((org-at-table-p) (call-interactively 'org-table-move-column))
  16399. ((org-with-limited-levels
  16400. (or (org-on-heading-p)
  16401. (and (org-region-active-p)
  16402. (save-excursion
  16403. (goto-char (region-beginning))
  16404. (org-on-heading-p)))))
  16405. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16406. (call-interactively 'org-do-demote))
  16407. ;; At an inline task.
  16408. ((org-on-heading-p)
  16409. (call-interactively 'org-inlinetask-demote))
  16410. ((or (org-at-item-p)
  16411. (and (org-region-active-p)
  16412. (save-excursion
  16413. (goto-char (region-beginning))
  16414. (org-at-item-p))))
  16415. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16416. (call-interactively 'org-indent-item))
  16417. (t (call-interactively 'forward-word))))
  16418. (defun org-check-for-hidden (what)
  16419. "Check if there are hidden headlines/items in the current visual line.
  16420. WHAT can be either `headlines' or `items'. If the current line is
  16421. an outline or item heading and it has a folded subtree below it,
  16422. this function returns t, nil otherwise."
  16423. (let ((re (cond
  16424. ((eq what 'headlines) org-outline-regexp-bol)
  16425. ((eq what 'items) (org-item-beginning-re))
  16426. (t (error "This should not happen"))))
  16427. beg end)
  16428. (save-excursion
  16429. (catch 'exit
  16430. (unless (org-region-active-p)
  16431. (setq beg (point-at-bol))
  16432. (beginning-of-line 2)
  16433. (while (and (not (eobp)) ;; this is like `next-line'
  16434. (get-char-property (1- (point)) 'invisible))
  16435. (beginning-of-line 2))
  16436. (setq end (point))
  16437. (goto-char beg)
  16438. (goto-char (point-at-eol))
  16439. (setq end (max end (point)))
  16440. (while (re-search-forward re end t)
  16441. (if (get-char-property (match-beginning 0) 'invisible)
  16442. (throw 'exit t))))
  16443. nil))))
  16444. (defun org-metaup (&optional arg)
  16445. "Move subtree up or move table row up.
  16446. Calls `org-move-subtree-up' or `org-table-move-row' or
  16447. `org-move-item-up', depending on context. See the individual commands
  16448. for more information."
  16449. (interactive "P")
  16450. (cond
  16451. ((run-hook-with-args-until-success 'org-metaup-hook))
  16452. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  16453. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  16454. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16455. (t (transpose-lines 1) (beginning-of-line -1))))
  16456. (defun org-metadown (&optional arg)
  16457. "Move subtree down or move table row down.
  16458. Calls `org-move-subtree-down' or `org-table-move-row' or
  16459. `org-move-item-down', depending on context. See the individual
  16460. commands for more information."
  16461. (interactive "P")
  16462. (cond
  16463. ((run-hook-with-args-until-success 'org-metadown-hook))
  16464. ((org-at-table-p) (call-interactively 'org-table-move-row))
  16465. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  16466. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16467. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  16468. (defun org-shiftup (&optional arg)
  16469. "Increase item in timestamp or increase priority of current headline.
  16470. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  16471. depending on context. See the individual commands for more information."
  16472. (interactive "P")
  16473. (cond
  16474. ((run-hook-with-args-until-success 'org-shiftup-hook))
  16475. ((and org-support-shift-select (org-region-active-p))
  16476. (org-call-for-shift-select 'previous-line))
  16477. ((org-at-timestamp-p t)
  16478. (call-interactively (if org-edit-timestamp-down-means-later
  16479. 'org-timestamp-down 'org-timestamp-up)))
  16480. ((and (not (eq org-support-shift-select 'always))
  16481. org-enable-priority-commands
  16482. (org-on-heading-p))
  16483. (call-interactively 'org-priority-up))
  16484. ((and (not org-support-shift-select) (org-at-item-p))
  16485. (call-interactively 'org-previous-item))
  16486. ((org-clocktable-try-shift 'up arg))
  16487. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  16488. (org-support-shift-select
  16489. (org-call-for-shift-select 'previous-line))
  16490. (t (org-shiftselect-error))))
  16491. (defun org-shiftdown (&optional arg)
  16492. "Decrease item in timestamp or decrease priority of current headline.
  16493. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  16494. depending on context. See the individual commands for more information."
  16495. (interactive "P")
  16496. (cond
  16497. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  16498. ((and org-support-shift-select (org-region-active-p))
  16499. (org-call-for-shift-select 'next-line))
  16500. ((org-at-timestamp-p t)
  16501. (call-interactively (if org-edit-timestamp-down-means-later
  16502. 'org-timestamp-up 'org-timestamp-down)))
  16503. ((and (not (eq org-support-shift-select 'always))
  16504. org-enable-priority-commands
  16505. (org-on-heading-p))
  16506. (call-interactively 'org-priority-down))
  16507. ((and (not org-support-shift-select) (org-at-item-p))
  16508. (call-interactively 'org-next-item))
  16509. ((org-clocktable-try-shift 'down arg))
  16510. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  16511. (org-support-shift-select
  16512. (org-call-for-shift-select 'next-line))
  16513. (t (org-shiftselect-error))))
  16514. (defun org-shiftright (&optional arg)
  16515. "Cycle the thing at point or in the current line, depending on context.
  16516. Depending on context, this does one of the following:
  16517. - switch a timestamp at point one day into the future
  16518. - on a headline, switch to the next TODO keyword.
  16519. - on an item, switch entire list to the next bullet type
  16520. - on a property line, switch to the next allowed value
  16521. - on a clocktable definition line, move time block into the future"
  16522. (interactive "P")
  16523. (cond
  16524. ((run-hook-with-args-until-success 'org-shiftright-hook))
  16525. ((and org-support-shift-select (org-region-active-p))
  16526. (org-call-for-shift-select 'forward-char))
  16527. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  16528. ((and (not (eq org-support-shift-select 'always))
  16529. (org-on-heading-p))
  16530. (let ((org-inhibit-logging
  16531. (not org-treat-S-cursor-todo-selection-as-state-change))
  16532. (org-inhibit-blocking
  16533. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16534. (org-call-with-arg 'org-todo 'right)))
  16535. ((or (and org-support-shift-select
  16536. (not (eq org-support-shift-select 'always))
  16537. (org-at-item-bullet-p))
  16538. (and (not org-support-shift-select) (org-at-item-p)))
  16539. (org-call-with-arg 'org-cycle-list-bullet nil))
  16540. ((and (not (eq org-support-shift-select 'always))
  16541. (org-at-property-p))
  16542. (call-interactively 'org-property-next-allowed-value))
  16543. ((org-clocktable-try-shift 'right arg))
  16544. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  16545. (org-support-shift-select
  16546. (org-call-for-shift-select 'forward-char))
  16547. (t (org-shiftselect-error))))
  16548. (defun org-shiftleft (&optional arg)
  16549. "Cycle the thing at point or in the current line, depending on context.
  16550. Depending on context, this does one of the following:
  16551. - switch a timestamp at point one day into the past
  16552. - on a headline, switch to the previous TODO keyword.
  16553. - on an item, switch entire list to the previous bullet type
  16554. - on a property line, switch to the previous allowed value
  16555. - on a clocktable definition line, move time block into the past"
  16556. (interactive "P")
  16557. (cond
  16558. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  16559. ((and org-support-shift-select (org-region-active-p))
  16560. (org-call-for-shift-select 'backward-char))
  16561. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  16562. ((and (not (eq org-support-shift-select 'always))
  16563. (org-on-heading-p))
  16564. (let ((org-inhibit-logging
  16565. (not org-treat-S-cursor-todo-selection-as-state-change))
  16566. (org-inhibit-blocking
  16567. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16568. (org-call-with-arg 'org-todo 'left)))
  16569. ((or (and org-support-shift-select
  16570. (not (eq org-support-shift-select 'always))
  16571. (org-at-item-bullet-p))
  16572. (and (not org-support-shift-select) (org-at-item-p)))
  16573. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  16574. ((and (not (eq org-support-shift-select 'always))
  16575. (org-at-property-p))
  16576. (call-interactively 'org-property-previous-allowed-value))
  16577. ((org-clocktable-try-shift 'left arg))
  16578. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  16579. (org-support-shift-select
  16580. (org-call-for-shift-select 'backward-char))
  16581. (t (org-shiftselect-error))))
  16582. (defun org-shiftcontrolright ()
  16583. "Switch to next TODO set."
  16584. (interactive)
  16585. (cond
  16586. ((and org-support-shift-select (org-region-active-p))
  16587. (org-call-for-shift-select 'forward-word))
  16588. ((and (not (eq org-support-shift-select 'always))
  16589. (org-on-heading-p))
  16590. (org-call-with-arg 'org-todo 'nextset))
  16591. (org-support-shift-select
  16592. (org-call-for-shift-select 'forward-word))
  16593. (t (org-shiftselect-error))))
  16594. (defun org-shiftcontrolleft ()
  16595. "Switch to previous TODO set."
  16596. (interactive)
  16597. (cond
  16598. ((and org-support-shift-select (org-region-active-p))
  16599. (org-call-for-shift-select 'backward-word))
  16600. ((and (not (eq org-support-shift-select 'always))
  16601. (org-on-heading-p))
  16602. (org-call-with-arg 'org-todo 'previousset))
  16603. (org-support-shift-select
  16604. (org-call-for-shift-select 'backward-word))
  16605. (t (org-shiftselect-error))))
  16606. (defun org-shiftcontrolup ()
  16607. "Change timestamps synchronously up in CLOCK log lines."
  16608. (interactive)
  16609. (cond ((and (not org-support-shift-select)
  16610. (org-at-clock-log-p)
  16611. (org-at-timestamp-p t))
  16612. (org-clock-timestamps-up))
  16613. (t (org-shiftselect-error))))
  16614. (defun org-shiftcontroldown ()
  16615. "Change timestamps synchronously down in CLOCK log lines."
  16616. (interactive)
  16617. (cond ((and (not org-support-shift-select)
  16618. (org-at-clock-log-p)
  16619. (org-at-timestamp-p t))
  16620. (org-clock-timestamps-down))
  16621. (t (org-shiftselect-error))))
  16622. (defun org-ctrl-c-ret ()
  16623. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  16624. (interactive)
  16625. (cond
  16626. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  16627. (t (call-interactively 'org-insert-heading))))
  16628. (defun org-find-visible ()
  16629. (let ((s (point)))
  16630. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16631. (get-char-property s 'invisible)))
  16632. s))
  16633. (defun org-find-invisible ()
  16634. (let ((s (point)))
  16635. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16636. (not (get-char-property s 'invisible))))
  16637. s))
  16638. (defun org-copy-visible (beg end)
  16639. "Copy the visible parts of the region."
  16640. (interactive "r")
  16641. (let (snippets s)
  16642. (save-excursion
  16643. (save-restriction
  16644. (narrow-to-region beg end)
  16645. (setq s (goto-char (point-min)))
  16646. (while (not (= (point) (point-max)))
  16647. (goto-char (org-find-invisible))
  16648. (push (buffer-substring s (point)) snippets)
  16649. (setq s (goto-char (org-find-visible))))))
  16650. (kill-new (apply 'concat (nreverse snippets)))))
  16651. (defun org-copy-special ()
  16652. "Copy region in table or copy current subtree.
  16653. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  16654. See the individual commands for more information."
  16655. (interactive)
  16656. (call-interactively
  16657. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  16658. (defun org-cut-special ()
  16659. "Cut region in table or cut current subtree.
  16660. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  16661. See the individual commands for more information."
  16662. (interactive)
  16663. (call-interactively
  16664. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  16665. (defun org-paste-special (arg)
  16666. "Paste rectangular region into table, or past subtree relative to level.
  16667. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  16668. See the individual commands for more information."
  16669. (interactive "P")
  16670. (if (org-at-table-p)
  16671. (org-table-paste-rectangle)
  16672. (org-paste-subtree arg)))
  16673. (defun org-edit-special (&optional arg)
  16674. "Call a special editor for the stuff at point.
  16675. When at a table, call the formula editor with `org-table-edit-formulas'.
  16676. When at the first line of an src example, call `org-edit-src-code'.
  16677. When in an #+include line, visit the include file. Otherwise call
  16678. `ffap' to visit the file at point."
  16679. (interactive)
  16680. ;; possibly prep session before editing source
  16681. (when arg
  16682. (let* ((info (org-babel-get-src-block-info))
  16683. (lang (nth 0 info))
  16684. (params (nth 2 info))
  16685. (session (cdr (assoc :session params))))
  16686. (when (and info session) ;; we are in a source-code block with a session
  16687. (funcall
  16688. (intern (concat "org-babel-prep-session:" lang)) session params))))
  16689. (cond ;; proceed with `org-edit-special'
  16690. ((save-excursion
  16691. (beginning-of-line 1)
  16692. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  16693. (find-file (org-trim (match-string 1))))
  16694. ((org-edit-src-code))
  16695. ((org-edit-fixed-width-region))
  16696. ((org-at-table.el-p)
  16697. (org-edit-src-code))
  16698. ((or (org-at-table-p)
  16699. (save-excursion
  16700. (beginning-of-line 1)
  16701. (looking-at "[ \t]*#\\+TBLFM:")))
  16702. (call-interactively 'org-table-edit-formulas))
  16703. (t (call-interactively 'ffap))))
  16704. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  16705. (defun org-ctrl-c-ctrl-c (&optional arg)
  16706. "Set tags in headline, or update according to changed information at point.
  16707. This command does many different things, depending on context:
  16708. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  16709. this is what we do.
  16710. - If the cursor is on a statistics cookie, update it.
  16711. - If the cursor is in a headline, prompt for tags and insert them
  16712. into the current line, aligned to `org-tags-column'. When called
  16713. with prefix arg, realign all tags in the current buffer.
  16714. - If the cursor is in one of the special #+KEYWORD lines, this
  16715. triggers scanning the buffer for these lines and updating the
  16716. information.
  16717. - If the cursor is inside a table, realign the table. This command
  16718. works even if the automatic table editor has been turned off.
  16719. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  16720. the entire table.
  16721. - If the cursor is at a footnote reference or definition, jump to
  16722. the corresponding definition or references, respectively.
  16723. - If the cursor is a the beginning of a dynamic block, update it.
  16724. - If the current buffer is a capture buffer, close note and file it.
  16725. - If the cursor is on a <<<target>>>, update radio targets and
  16726. corresponding links in this buffer.
  16727. - If the cursor is on a numbered item in a plain list, renumber the
  16728. ordered list.
  16729. - If the cursor is on a checkbox, toggle it.
  16730. - If the cursor is on a code block, evaluate it. The variable
  16731. `org-confirm-babel-evaluate' can be used to control prompting
  16732. before code block evaluation, by default every code block
  16733. evaluation requires confirmation. Code block evaluation can be
  16734. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  16735. (interactive "P")
  16736. (let ((org-enable-table-editor t))
  16737. (cond
  16738. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  16739. org-occur-highlights
  16740. org-latex-fragment-image-overlays)
  16741. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  16742. (org-remove-occur-highlights)
  16743. (org-remove-latex-fragment-image-overlays)
  16744. (message "Temporary highlights/overlays removed from current buffer"))
  16745. ((and (local-variable-p 'org-finish-function (current-buffer))
  16746. (fboundp org-finish-function))
  16747. (funcall org-finish-function))
  16748. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  16749. ((org-in-regexp org-ts-regexp-both)
  16750. (org-timestamp-change 0 'day))
  16751. ((or (looking-at org-property-start-re)
  16752. (org-at-property-p))
  16753. (call-interactively 'org-property-action))
  16754. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  16755. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  16756. (or (org-on-heading-p) (org-at-item-p)))
  16757. (call-interactively 'org-update-statistics-cookies))
  16758. ((org-on-heading-p) (call-interactively 'org-set-tags))
  16759. ((org-at-table.el-p)
  16760. (message "Use C-c ' to edit table.el tables"))
  16761. ((org-at-table-p)
  16762. (org-table-maybe-eval-formula)
  16763. (if arg
  16764. (call-interactively 'org-table-recalculate)
  16765. (org-table-maybe-recalculate-line))
  16766. (call-interactively 'org-table-align)
  16767. (orgtbl-send-table 'maybe))
  16768. ((or (org-footnote-at-reference-p)
  16769. (org-footnote-at-definition-p))
  16770. (call-interactively 'org-footnote-action))
  16771. ((org-at-item-checkbox-p)
  16772. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  16773. ;; list only if at top item.
  16774. (let* ((cbox (match-string 1))
  16775. (struct (org-list-struct))
  16776. (old-struct (copy-tree struct))
  16777. (parents (org-list-parents-alist struct))
  16778. (orderedp (org-entry-get nil "ORDERED"))
  16779. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  16780. block-item)
  16781. ;; Use a light version of `org-toggle-checkbox' to avoid
  16782. ;; computing list structure twice.
  16783. (org-list-set-checkbox (point-at-bol) struct
  16784. (cond
  16785. ((equal arg '(16)) "[-]")
  16786. ((equal arg '(4)) nil)
  16787. ((equal "[X]" cbox) "[ ]")
  16788. (t "[X]")))
  16789. ;; Replicate `org-list-write-struct', while grabbing a return
  16790. ;; value from `org-list-struct-fix-box'.
  16791. (org-list-struct-fix-ind struct parents 2)
  16792. (org-list-struct-fix-item-end struct)
  16793. (let ((prevs (org-list-prevs-alist struct)))
  16794. (org-list-struct-fix-bul struct prevs)
  16795. (org-list-struct-fix-ind struct parents)
  16796. (setq block-item
  16797. (org-list-struct-fix-box struct parents prevs orderedp)))
  16798. (org-list-struct-apply-struct struct old-struct)
  16799. (org-update-checkbox-count-maybe)
  16800. (when block-item
  16801. (message
  16802. "Checkboxes were removed due to unchecked box at line %d"
  16803. (org-current-line block-item)))
  16804. (when firstp (org-list-send-list 'maybe))))
  16805. ((org-at-item-p)
  16806. ;; Cursor at an item: repair list. Do checkbox related actions
  16807. ;; only if function was called with an argument. Send list only
  16808. ;; if at top item.
  16809. (let* ((struct (org-list-struct))
  16810. (firstp (= (org-list-get-top-point struct) (point-at-bol))))
  16811. (when arg (org-list-set-checkbox (point-at-bol) struct "[ ]"))
  16812. (org-list-write-struct struct (org-list-parents-alist struct))
  16813. (when arg (org-update-checkbox-count-maybe))
  16814. (when firstp (org-list-send-list 'maybe))))
  16815. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  16816. ;; Dynamic block
  16817. (beginning-of-line 1)
  16818. (save-excursion (org-update-dblock)))
  16819. ((save-excursion
  16820. (beginning-of-line 1)
  16821. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  16822. (cond
  16823. ((equal (match-string 1) "TBLFM")
  16824. ;; Recalculate the table before this line
  16825. (save-excursion
  16826. (beginning-of-line 1)
  16827. (skip-chars-backward " \r\n\t")
  16828. (if (org-at-table-p)
  16829. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  16830. (t
  16831. (let ((org-inhibit-startup-visibility-stuff t)
  16832. (org-startup-align-all-tables nil))
  16833. (when (boundp 'org-table-coordinate-overlays)
  16834. (mapc 'delete-overlay org-table-coordinate-overlays)
  16835. (setq org-table-coordinate-overlays nil))
  16836. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  16837. (message "Local setup has been refreshed"))))
  16838. ((org-clock-update-time-maybe))
  16839. (t
  16840. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  16841. (error "C-c C-c can do nothing useful at this location"))))))
  16842. (defun org-mode-restart ()
  16843. "Restart Org-mode, to scan again for special lines.
  16844. Also updates the keyword regular expressions."
  16845. (interactive)
  16846. (org-mode)
  16847. (message "Org-mode restarted"))
  16848. (defun org-kill-note-or-show-branches ()
  16849. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  16850. (interactive)
  16851. (if (not org-finish-function)
  16852. (progn
  16853. (hide-subtree)
  16854. (call-interactively 'show-branches))
  16855. (let ((org-note-abort t))
  16856. (funcall org-finish-function))))
  16857. (defun org-return (&optional indent)
  16858. "Goto next table row or insert a newline.
  16859. Calls `org-table-next-row' or `newline', depending on context.
  16860. See the individual commands for more information."
  16861. (interactive)
  16862. (cond
  16863. ((bobp) (if indent (newline-and-indent) (newline)))
  16864. ((org-at-table-p)
  16865. (org-table-justify-field-maybe)
  16866. (call-interactively 'org-table-next-row))
  16867. ;; when `newline-and-indent' is called within a list, make sure
  16868. ;; text moved stays inside the item.
  16869. ((and (org-in-item-p) indent)
  16870. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  16871. (progn
  16872. (save-match-data (newline))
  16873. (org-indent-line-to (length (match-string 0))))
  16874. (let ((ind (org-get-indentation)))
  16875. (newline)
  16876. (if (org-looking-back org-list-end-re)
  16877. (org-indent-line-function)
  16878. (org-indent-line-to ind)))))
  16879. ((and org-return-follows-link
  16880. (let ((tprop (get-text-property (point) 'face)))
  16881. (or (eq tprop 'org-link)
  16882. (and (listp tprop) (memq 'org-link tprop)))))
  16883. (call-interactively 'org-open-at-point))
  16884. ((and (org-at-heading-p)
  16885. (looking-at
  16886. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  16887. (org-show-entry)
  16888. (end-of-line 1)
  16889. (newline))
  16890. (t (if indent (newline-and-indent) (newline)))))
  16891. (defun org-return-indent ()
  16892. "Goto next table row or insert a newline and indent.
  16893. Calls `org-table-next-row' or `newline-and-indent', depending on
  16894. context. See the individual commands for more information."
  16895. (interactive)
  16896. (org-return t))
  16897. (defun org-ctrl-c-star ()
  16898. "Compute table, or change heading status of lines.
  16899. Calls `org-table-recalculate' or `org-toggle-heading',
  16900. depending on context."
  16901. (interactive)
  16902. (cond
  16903. ((org-at-table-p)
  16904. (call-interactively 'org-table-recalculate))
  16905. (t
  16906. ;; Convert all lines in region to list items
  16907. (call-interactively 'org-toggle-heading))))
  16908. (defun org-ctrl-c-minus ()
  16909. "Insert separator line in table or modify bullet status of line.
  16910. Also turns a plain line or a region of lines into list items.
  16911. Calls `org-table-insert-hline', `org-toggle-item', or
  16912. `org-cycle-list-bullet', depending on context."
  16913. (interactive)
  16914. (cond
  16915. ((org-at-table-p)
  16916. (call-interactively 'org-table-insert-hline))
  16917. ((org-region-active-p)
  16918. (call-interactively 'org-toggle-item))
  16919. ((org-in-item-p)
  16920. (call-interactively 'org-cycle-list-bullet))
  16921. (t
  16922. (call-interactively 'org-toggle-item))))
  16923. (defun org-toggle-item (arg)
  16924. "Convert headings or normal lines to items, items to normal lines.
  16925. If there is no active region, only the current line is considered.
  16926. If the first non blank line in the region is an headline, convert
  16927. all headlines to items, shifting text accordingly.
  16928. If it is an item, convert all items to normal lines.
  16929. If it is normal text, change region into an item. With a prefix
  16930. argument ARG, change each line in region into an item."
  16931. (interactive "P")
  16932. (let ((shift-text
  16933. (function
  16934. ;; Shift text in current section to IND, from point to END.
  16935. ;; The function leaves point to END line.
  16936. (lambda (ind end)
  16937. (let ((min-i 1000) (end (copy-marker end)))
  16938. ;; First determine the minimum indentation (MIN-I) of
  16939. ;; the text.
  16940. (save-excursion
  16941. (catch 'exit
  16942. (while (< (point) end)
  16943. (let ((i (org-get-indentation)))
  16944. (cond
  16945. ;; Skip blank lines and inline tasks.
  16946. ((looking-at "^[ \t]*$"))
  16947. ((looking-at org-outline-regexp-bol))
  16948. ;; We can't find less than 0 indentation.
  16949. ((zerop i) (throw 'exit (setq min-i 0)))
  16950. ((< i min-i) (setq min-i i))))
  16951. (forward-line))))
  16952. ;; Then indent each line so that a line indented to
  16953. ;; MIN-I becomes indented to IND. Ignore blank lines
  16954. ;; and inline tasks in the process.
  16955. (let ((delta (- ind min-i)))
  16956. (while (< (point) end)
  16957. (unless (or (looking-at "^[ \t]*$")
  16958. (looking-at org-outline-regexp-bol))
  16959. (org-indent-line-to (+ (org-get-indentation) delta)))
  16960. (forward-line)))))))
  16961. (skip-blanks
  16962. (function
  16963. ;; Return beginning of first non-blank line, starting from
  16964. ;; line at POS.
  16965. (lambda (pos)
  16966. (save-excursion
  16967. (goto-char pos)
  16968. (skip-chars-forward " \r\t\n")
  16969. (point-at-bol)))))
  16970. beg end)
  16971. ;; Determine boundaries of changes.
  16972. (if (org-region-active-p)
  16973. (setq beg (funcall skip-blanks (region-beginning))
  16974. end (copy-marker (region-end)))
  16975. (setq beg (funcall skip-blanks (point-at-bol))
  16976. end (copy-marker (point-at-eol))))
  16977. ;; Depending on the starting line, choose an action on the text
  16978. ;; between BEG and END.
  16979. (org-with-limited-levels
  16980. (save-excursion
  16981. (goto-char beg)
  16982. (cond
  16983. ;; Case 1. Start at an item: de-itemize. Note that it only
  16984. ;; happens when a region is active: `org-ctrl-c-minus'
  16985. ;; would call `org-cycle-list-bullet' otherwise.
  16986. ((org-at-item-p)
  16987. (while (< (point) end)
  16988. (when (org-at-item-p)
  16989. (skip-chars-forward " \t")
  16990. (delete-region (point) (match-end 0)))
  16991. (forward-line)))
  16992. ;; Case 2. Start at an heading: convert to items.
  16993. ((org-on-heading-p)
  16994. (let* ((bul (org-list-bullet-string "-"))
  16995. (bul-len (length bul))
  16996. ;; Indentation of the first heading. It should be
  16997. ;; relative to the indentation of its parent, if any.
  16998. (start-ind (save-excursion
  16999. (cond
  17000. ((not org-adapt-indentation) 0)
  17001. ((not (outline-previous-heading)) 0)
  17002. (t (length (match-string 0))))))
  17003. ;; Level of first heading. Further headings will be
  17004. ;; compared to it to determine hierarchy in the list.
  17005. (ref-level (org-reduced-level (org-outline-level))))
  17006. (while (< (point) end)
  17007. (let* ((level (org-reduced-level (org-outline-level)))
  17008. (delta (max 0 (- level ref-level))))
  17009. ;; If current headline is less indented than the first
  17010. ;; one, set it as reference, in order to preserve
  17011. ;; subtrees.
  17012. (when (< level ref-level) (setq ref-level level))
  17013. (replace-match bul t t)
  17014. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17015. ;; Ensure all text down to END (or SECTION-END) belongs
  17016. ;; to the newly created item.
  17017. (let ((section-end (save-excursion
  17018. (or (outline-next-heading) (point)))))
  17019. (forward-line)
  17020. (funcall shift-text
  17021. (+ start-ind (* (1+ delta) bul-len))
  17022. (min end section-end)))))))
  17023. ;; Case 3. Normal line with ARG: turn each non-item line into
  17024. ;; an item.
  17025. (arg
  17026. (while (< (point) end)
  17027. (unless (or (org-on-heading-p) (org-at-item-p))
  17028. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17029. (replace-match
  17030. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17031. (forward-line)))
  17032. ;; Case 4. Normal line without ARG: make the first line of
  17033. ;; region an item, and shift indentation of others
  17034. ;; lines to set them as item's body.
  17035. (t (let* ((bul (org-list-bullet-string "-"))
  17036. (bul-len (length bul))
  17037. (ref-ind (org-get-indentation)))
  17038. (skip-chars-forward " \t")
  17039. (insert bul)
  17040. (forward-line)
  17041. (while (< (point) end)
  17042. ;; Ensure that lines less indented than first one
  17043. ;; still get included in item body.
  17044. (funcall shift-text
  17045. (+ ref-ind bul-len)
  17046. (min end (save-excursion (or (outline-next-heading)
  17047. (point)))))
  17048. (forward-line)))))))))
  17049. (defun org-toggle-heading (&optional nstars)
  17050. "Convert headings to normal text, or items or text to headings.
  17051. If there is no active region, only the current line is considered.
  17052. If the first non blank line is an headline, remove the stars from
  17053. all headlines in the region.
  17054. If it is a plain list item, turn all plain list items into headings.
  17055. If it is a normal line, turn each and every normal line (i.e. not
  17056. an heading or an item) in the region into a heading.
  17057. When converting a line into a heading, the number of stars is chosen
  17058. such that the lines become children of the current entry. However,
  17059. when a prefix argument is given, its value determines the number of
  17060. stars to add."
  17061. (interactive "P")
  17062. (let ((skip-blanks
  17063. (function
  17064. ;; Return beginning of first non-blank line, starting from
  17065. ;; line at POS.
  17066. (lambda (pos)
  17067. (save-excursion
  17068. (goto-char pos)
  17069. (skip-chars-forward " \r\t\n")
  17070. (point-at-bol)))))
  17071. beg end)
  17072. ;; Determine boundaries of changes. If region ends at a bol, do
  17073. ;; not consider the last line to be in the region.
  17074. (if (org-region-active-p)
  17075. (setq beg (funcall skip-blanks (region-beginning))
  17076. end (copy-marker (save-excursion
  17077. (goto-char (region-end))
  17078. (if (bolp) (point) (point-at-eol)))))
  17079. (setq beg (funcall skip-blanks (point-at-bol))
  17080. end (copy-marker (point-at-eol))))
  17081. ;; Ensure inline tasks don't count as headings.
  17082. (org-with-limited-levels
  17083. (save-excursion
  17084. (goto-char beg)
  17085. (cond
  17086. ;; Case 1. Started at an heading: de-star headings.
  17087. ((org-on-heading-p)
  17088. (while (< (point) end)
  17089. (when (org-on-heading-p t)
  17090. (looking-at org-outline-regexp) (replace-match ""))
  17091. (forward-line)))
  17092. ;; Case 2. Started at an item: change items into headlines.
  17093. ;; One star will be added by `org-list-to-subtree'.
  17094. ((org-at-item-p)
  17095. (let* ((stars (make-string
  17096. (if nstars
  17097. ;; subtract the star that will be added again by
  17098. ;; `org-list-to-subtree'
  17099. (1- (prefix-numeric-value current-prefix-arg))
  17100. (or (org-current-level) 0))
  17101. ?*))
  17102. (add-stars
  17103. (cond (nstars "") ; stars from prefix only
  17104. ((equal stars "") "") ; before first heading
  17105. (org-odd-levels-only "*") ; inside heading, odd
  17106. (t "")))) ; inside heading, oddeven
  17107. (while (< (point) end)
  17108. (when (org-at-item-p)
  17109. ;; Pay attention to cases when region ends before list.
  17110. (let* ((struct (org-list-struct))
  17111. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  17112. (save-restriction
  17113. (narrow-to-region (point) list-end)
  17114. (insert
  17115. (org-list-to-subtree
  17116. (org-list-parse-list t)
  17117. '(:istart (concat stars add-stars (funcall get-stars depth))
  17118. :icount (concat stars add-stars (funcall get-stars depth))))))))
  17119. (forward-line))))
  17120. ;; Case 3. Started at normal text: make every line an heading,
  17121. ;; skipping headlines and items.
  17122. (t (let* ((stars (make-string
  17123. (if nstars
  17124. (prefix-numeric-value current-prefix-arg)
  17125. (or (org-current-level) 0))
  17126. ?*))
  17127. (add-stars
  17128. (cond (nstars "") ; stars from prefix only
  17129. ((equal stars "") "*") ; before first heading
  17130. (org-odd-levels-only "**") ; inside heading, odd
  17131. (t "*"))) ; inside heading, oddeven
  17132. (rpl (concat stars add-stars " ")))
  17133. (while (< (point) end)
  17134. (when (and (not (org-on-heading-p)) (not (org-at-item-p))
  17135. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  17136. (replace-match (concat rpl (match-string 2))))
  17137. (forward-line)))))))))
  17138. (defun org-meta-return (&optional arg)
  17139. "Insert a new heading or wrap a region in a table.
  17140. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  17141. See the individual commands for more information."
  17142. (interactive "P")
  17143. (cond
  17144. ((run-hook-with-args-until-success 'org-metareturn-hook))
  17145. ((org-at-table-p)
  17146. (call-interactively 'org-table-wrap-region))
  17147. (t (call-interactively 'org-insert-heading))))
  17148. ;;; Menu entries
  17149. ;; Define the Org-mode menus
  17150. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  17151. '("Tbl"
  17152. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  17153. ["Next Field" org-cycle (org-at-table-p)]
  17154. ["Previous Field" org-shifttab (org-at-table-p)]
  17155. ["Next Row" org-return (org-at-table-p)]
  17156. "--"
  17157. ["Blank Field" org-table-blank-field (org-at-table-p)]
  17158. ["Edit Field" org-table-edit-field (org-at-table-p)]
  17159. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  17160. "--"
  17161. ("Column"
  17162. ["Move Column Left" org-metaleft (org-at-table-p)]
  17163. ["Move Column Right" org-metaright (org-at-table-p)]
  17164. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  17165. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  17166. ("Row"
  17167. ["Move Row Up" org-metaup (org-at-table-p)]
  17168. ["Move Row Down" org-metadown (org-at-table-p)]
  17169. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  17170. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  17171. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  17172. "--"
  17173. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  17174. ("Rectangle"
  17175. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  17176. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  17177. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  17178. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  17179. "--"
  17180. ("Calculate"
  17181. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  17182. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  17183. ["Edit Formulas" org-edit-special (org-at-table-p)]
  17184. "--"
  17185. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  17186. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  17187. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  17188. "--"
  17189. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  17190. "--"
  17191. ["Sum Column/Rectangle" org-table-sum
  17192. (or (org-at-table-p) (org-region-active-p))]
  17193. ["Which Column?" org-table-current-column (org-at-table-p)])
  17194. ["Debug Formulas"
  17195. org-table-toggle-formula-debugger
  17196. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  17197. ["Show Col/Row Numbers"
  17198. org-table-toggle-coordinate-overlays
  17199. :style toggle
  17200. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  17201. "--"
  17202. ["Create" org-table-create (and (not (org-at-table-p))
  17203. org-enable-table-editor)]
  17204. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  17205. ["Import from File" org-table-import (not (org-at-table-p))]
  17206. ["Export to File" org-table-export (org-at-table-p)]
  17207. "--"
  17208. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  17209. (easy-menu-define org-org-menu org-mode-map "Org menu"
  17210. '("Org"
  17211. ("Show/Hide"
  17212. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  17213. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  17214. ["Sparse Tree..." org-sparse-tree t]
  17215. ["Reveal Context" org-reveal t]
  17216. ["Show All" show-all t]
  17217. "--"
  17218. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  17219. "--"
  17220. ["New Heading" org-insert-heading t]
  17221. ("Navigate Headings"
  17222. ["Up" outline-up-heading t]
  17223. ["Next" outline-next-visible-heading t]
  17224. ["Previous" outline-previous-visible-heading t]
  17225. ["Next Same Level" outline-forward-same-level t]
  17226. ["Previous Same Level" outline-backward-same-level t]
  17227. "--"
  17228. ["Jump" org-goto t])
  17229. ("Edit Structure"
  17230. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  17231. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  17232. "--"
  17233. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  17234. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  17235. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  17236. "--"
  17237. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  17238. "--"
  17239. ["Copy visible text" org-copy-visible t]
  17240. "--"
  17241. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  17242. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  17243. ["Demote Heading" org-metaright (not (org-at-table-p))]
  17244. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  17245. "--"
  17246. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  17247. "--"
  17248. ["Convert to odd levels" org-convert-to-odd-levels t]
  17249. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  17250. ("Editing"
  17251. ["Emphasis..." org-emphasize t]
  17252. ["Edit Source Example" org-edit-special t]
  17253. "--"
  17254. ["Footnote new/jump" org-footnote-action t]
  17255. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  17256. ("Archive"
  17257. ["Archive (default method)" org-archive-subtree-default t]
  17258. "--"
  17259. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  17260. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  17261. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  17262. )
  17263. "--"
  17264. ("Hyperlinks"
  17265. ["Store Link (Global)" org-store-link t]
  17266. ["Find existing link to here" org-occur-link-in-agenda-files t]
  17267. ["Insert Link" org-insert-link t]
  17268. ["Follow Link" org-open-at-point t]
  17269. "--"
  17270. ["Next link" org-next-link t]
  17271. ["Previous link" org-previous-link t]
  17272. "--"
  17273. ["Descriptive Links"
  17274. org-toggle-link-display
  17275. :style radio
  17276. :selected org-descriptive-links
  17277. ]
  17278. ["Literal Links"
  17279. org-toggle-link-display
  17280. :style radio
  17281. :selected (not org-descriptive-links)])
  17282. "--"
  17283. ("TODO Lists"
  17284. ["TODO/DONE/-" org-todo t]
  17285. ("Select keyword"
  17286. ["Next keyword" org-shiftright (org-on-heading-p)]
  17287. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  17288. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  17289. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  17290. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  17291. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  17292. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  17293. "--"
  17294. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  17295. :selected org-enforce-todo-dependencies :style toggle :active t]
  17296. "Settings for tree at point"
  17297. ["Do Children sequentially" org-toggle-ordered-property :style radio
  17298. :selected (org-entry-get nil "ORDERED")
  17299. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17300. ["Do Children parallel" org-toggle-ordered-property :style radio
  17301. :selected (not (org-entry-get nil "ORDERED"))
  17302. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17303. "--"
  17304. ["Set Priority" org-priority t]
  17305. ["Priority Up" org-shiftup t]
  17306. ["Priority Down" org-shiftdown t]
  17307. "--"
  17308. ["Get news from all feeds" org-feed-update-all t]
  17309. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  17310. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  17311. ("TAGS and Properties"
  17312. ["Set Tags" org-set-tags-command t]
  17313. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  17314. "--"
  17315. ["Set property" org-set-property t]
  17316. ["Column view of properties" org-columns t]
  17317. ["Insert Column View DBlock" org-insert-columns-dblock t])
  17318. ("Dates and Scheduling"
  17319. ["Timestamp" org-time-stamp t]
  17320. ["Timestamp (inactive)" org-time-stamp-inactive t]
  17321. ("Change Date"
  17322. ["1 Day Later" org-shiftright t]
  17323. ["1 Day Earlier" org-shiftleft t]
  17324. ["1 ... Later" org-shiftup t]
  17325. ["1 ... Earlier" org-shiftdown t])
  17326. ["Compute Time Range" org-evaluate-time-range t]
  17327. ["Schedule Item" org-schedule t]
  17328. ["Deadline" org-deadline t]
  17329. "--"
  17330. ["Custom time format" org-toggle-time-stamp-overlays
  17331. :style radio :selected org-display-custom-times]
  17332. "--"
  17333. ["Goto Calendar" org-goto-calendar t]
  17334. ["Date from Calendar" org-date-from-calendar t]
  17335. "--"
  17336. ["Start/Restart Timer" org-timer-start t]
  17337. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  17338. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  17339. ["Insert Timer String" org-timer t]
  17340. ["Insert Timer Item" org-timer-item t])
  17341. ("Logging work"
  17342. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  17343. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  17344. ["Clock out" org-clock-out t]
  17345. ["Clock cancel" org-clock-cancel t]
  17346. "--"
  17347. ["Mark as default task" org-clock-mark-default-task t]
  17348. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  17349. ["Goto running clock" org-clock-goto t]
  17350. "--"
  17351. ["Display times" org-clock-display t]
  17352. ["Create clock table" org-clock-report t]
  17353. "--"
  17354. ["Record DONE time"
  17355. (progn (setq org-log-done (not org-log-done))
  17356. (message "Switching to %s will %s record a timestamp"
  17357. (car org-done-keywords)
  17358. (if org-log-done "automatically" "not")))
  17359. :style toggle :selected org-log-done])
  17360. "--"
  17361. ["Agenda Command..." org-agenda t]
  17362. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  17363. ("File List for Agenda")
  17364. ("Special views current file"
  17365. ["TODO Tree" org-show-todo-tree t]
  17366. ["Check Deadlines" org-check-deadlines t]
  17367. ["Timeline" org-timeline t]
  17368. ["Tags/Property tree" org-match-sparse-tree t])
  17369. "--"
  17370. ["Export/Publish..." org-export t]
  17371. ("LaTeX"
  17372. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  17373. :selected org-cdlatex-mode]
  17374. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  17375. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  17376. ["Modify math symbol" org-cdlatex-math-modify
  17377. (org-inside-LaTeX-fragment-p)]
  17378. ["Insert citation" org-reftex-citation t]
  17379. "--"
  17380. ["Template for BEAMER" org-insert-beamer-options-template t])
  17381. "--"
  17382. ("MobileOrg"
  17383. ["Push Files and Views" org-mobile-push t]
  17384. ["Get Captured and Flagged" org-mobile-pull t]
  17385. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  17386. "--"
  17387. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  17388. "--"
  17389. ("Documentation"
  17390. ["Show Version" org-version t]
  17391. ["Info Documentation" org-info t])
  17392. ("Customize"
  17393. ["Browse Org Group" org-customize t]
  17394. "--"
  17395. ["Expand This Menu" org-create-customize-menu
  17396. (fboundp 'customize-menu-create)])
  17397. ["Send bug report" org-submit-bug-report t]
  17398. "--"
  17399. ("Refresh/Reload"
  17400. ["Refresh setup current buffer" org-mode-restart t]
  17401. ["Reload Org (after update)" org-reload t]
  17402. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  17403. ))
  17404. (defun org-info (&optional node)
  17405. "Read documentation for Org-mode in the info system.
  17406. With optional NODE, go directly to that node."
  17407. (interactive)
  17408. (info (format "(org)%s" (or node ""))))
  17409. ;;;###autoload
  17410. (defun org-submit-bug-report ()
  17411. "Submit a bug report on Org-mode via mail.
  17412. Don't hesitate to report any problems or inaccurate documentation.
  17413. If you don't have setup sending mail from (X)Emacs, please copy the
  17414. output buffer into your mail program, as it gives us important
  17415. information about your Org-mode version and configuration."
  17416. (interactive)
  17417. (require 'reporter)
  17418. (org-load-modules-maybe)
  17419. (org-require-autoloaded-modules)
  17420. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  17421. (reporter-submit-bug-report
  17422. "emacs-orgmode@gnu.org"
  17423. (org-version)
  17424. (let (list)
  17425. (save-window-excursion
  17426. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  17427. (delete-other-windows)
  17428. (erase-buffer)
  17429. (insert "You are about to submit a bug report to the Org-mode mailing list.
  17430. We would like to add your full Org-mode and Outline configuration to the
  17431. bug report. This greatly simplifies the work of the maintainer and
  17432. other experts on the mailing list.
  17433. HOWEVER, some variables you have customized may contain private
  17434. information. The names of customers, colleagues, or friends, might
  17435. appear in the form of file names, tags, todo states, or search strings.
  17436. If you answer yes to the prompt, you might want to check and remove
  17437. such private information before sending the email.")
  17438. (add-text-properties (point-min) (point-max) '(face org-warning))
  17439. (when (yes-or-no-p "Include your Org-mode configuration ")
  17440. (mapatoms
  17441. (lambda (v)
  17442. (and (boundp v)
  17443. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  17444. (or (and (symbol-value v)
  17445. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  17446. (and
  17447. (get v 'custom-type) (get v 'standard-value)
  17448. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  17449. (push v list)))))
  17450. (kill-buffer (get-buffer "*Warn about privacy*"))
  17451. list))
  17452. nil nil
  17453. "Remember to cover the basics, that is, what you expected to happen and
  17454. what in fact did happen. You don't know how to make a good report? See
  17455. http://orgmode.org/manual/Feedback.html#Feedback
  17456. Your bug report will be posted to the Org-mode mailing list.
  17457. ------------------------------------------------------------------------")
  17458. (save-excursion
  17459. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  17460. (replace-match "\\1Bug: \\3 [\\2]")))))
  17461. (defun org-install-agenda-files-menu ()
  17462. (let ((bl (buffer-list)))
  17463. (save-excursion
  17464. (while bl
  17465. (set-buffer (pop bl))
  17466. (if (eq major-mode 'org-mode) (setq bl nil)))
  17467. (when (eq major-mode 'org-mode)
  17468. (easy-menu-change
  17469. '("Org") "File List for Agenda"
  17470. (append
  17471. (list
  17472. ["Edit File List" (org-edit-agenda-file-list) t]
  17473. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  17474. ["Remove Current File from List" org-remove-file t]
  17475. ["Cycle through agenda files" org-cycle-agenda-files t]
  17476. ["Occur in all agenda files" org-occur-in-agenda-files t]
  17477. "--")
  17478. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  17479. ;;;; Documentation
  17480. ;;;###autoload
  17481. (defun org-require-autoloaded-modules ()
  17482. (interactive)
  17483. (mapc 'require
  17484. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  17485. org-docbook org-exp org-html org-icalendar
  17486. org-id org-latex
  17487. org-publish org-remember org-table
  17488. org-timer org-xoxo)))
  17489. ;;;###autoload
  17490. (defun org-reload (&optional uncompiled)
  17491. "Reload all org lisp files.
  17492. With prefix arg UNCOMPILED, load the uncompiled versions."
  17493. (interactive "P")
  17494. (require 'find-func)
  17495. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  17496. (dir-org (file-name-directory (org-find-library-name "org")))
  17497. (dir-org-contrib (ignore-errors
  17498. (file-name-directory
  17499. (org-find-library-name "org-contribdir"))))
  17500. (babel-files
  17501. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  17502. (append (list nil "comint" "eval" "exp" "keys"
  17503. "lob" "ref" "table" "tangle")
  17504. (delq nil
  17505. (mapcar
  17506. (lambda (lang)
  17507. (when (cdr lang) (symbol-name (car lang))))
  17508. org-babel-load-languages)))))
  17509. (files
  17510. (append (directory-files dir-org t file-re)
  17511. babel-files
  17512. (and dir-org-contrib
  17513. (directory-files dir-org-contrib t file-re))))
  17514. (remove-re (concat (if (featurep 'xemacs)
  17515. "org-colview" "org-colview-xemacs")
  17516. "\\'")))
  17517. (setq files (mapcar 'file-name-sans-extension files))
  17518. (setq files (mapcar
  17519. (lambda (x) (if (string-match remove-re x) nil x))
  17520. files))
  17521. (setq files (delq nil files))
  17522. (mapc
  17523. (lambda (f)
  17524. (when (featurep (intern (file-name-nondirectory f)))
  17525. (if (and (not uncompiled)
  17526. (file-exists-p (concat f ".elc")))
  17527. (load (concat f ".elc") nil nil t)
  17528. (load (concat f ".el") nil nil t))))
  17529. files))
  17530. (org-version))
  17531. ;;;###autoload
  17532. (defun org-customize ()
  17533. "Call the customize function with org as argument."
  17534. (interactive)
  17535. (org-load-modules-maybe)
  17536. (org-require-autoloaded-modules)
  17537. (customize-browse 'org))
  17538. (defun org-create-customize-menu ()
  17539. "Create a full customization menu for Org-mode, insert it into the menu."
  17540. (interactive)
  17541. (org-load-modules-maybe)
  17542. (org-require-autoloaded-modules)
  17543. (if (fboundp 'customize-menu-create)
  17544. (progn
  17545. (easy-menu-change
  17546. '("Org") "Customize"
  17547. `(["Browse Org group" org-customize t]
  17548. "--"
  17549. ,(customize-menu-create 'org)
  17550. ["Set" Custom-set t]
  17551. ["Save" Custom-save t]
  17552. ["Reset to Current" Custom-reset-current t]
  17553. ["Reset to Saved" Custom-reset-saved t]
  17554. ["Reset to Standard Settings" Custom-reset-standard t]))
  17555. (message "\"Org\"-menu now contains full customization menu"))
  17556. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  17557. ;;;; Miscellaneous stuff
  17558. ;;; Generally useful functions
  17559. (defun org-get-at-bol (property)
  17560. "Get text property PROPERTY at beginning of line."
  17561. (get-text-property (point-at-bol) property))
  17562. (defun org-find-text-property-in-string (prop s)
  17563. "Return the first non-nil value of property PROP in string S."
  17564. (or (get-text-property 0 prop s)
  17565. (get-text-property (or (next-single-property-change 0 prop s) 0)
  17566. prop s)))
  17567. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  17568. "Display the given MESSAGE as a warning."
  17569. (if (fboundp 'display-warning)
  17570. (display-warning 'org message
  17571. (if (featurep 'xemacs) 'warning :warning))
  17572. (let ((buf (get-buffer-create "*Org warnings*")))
  17573. (with-current-buffer buf
  17574. (goto-char (point-max))
  17575. (insert "Warning (Org): " message)
  17576. (unless (bolp)
  17577. (newline)))
  17578. (display-buffer buf)
  17579. (sit-for 0))))
  17580. (defun org-eval (form)
  17581. "Eval FORM and return result."
  17582. (condition-case error
  17583. (eval form)
  17584. (error (format "%%![Error: %s]" error))))
  17585. (defun org-in-commented-line ()
  17586. "Is point in a line starting with `#'?"
  17587. (equal (char-after (point-at-bol)) ?#))
  17588. (defun org-in-indented-comment-line ()
  17589. "Is point in a line starting with `#' after some white space?"
  17590. (save-excursion
  17591. (save-match-data
  17592. (goto-char (point-at-bol))
  17593. (looking-at "[ \t]*#"))))
  17594. (defun org-in-verbatim-emphasis ()
  17595. (save-match-data
  17596. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  17597. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  17598. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  17599. (if (and marker (marker-buffer marker)
  17600. (buffer-live-p (marker-buffer marker)))
  17601. (progn
  17602. (org-pop-to-buffer-same-window (marker-buffer marker))
  17603. (if (or (> marker (point-max)) (< marker (point-min)))
  17604. (widen))
  17605. (goto-char marker)
  17606. (org-show-context 'org-goto))
  17607. (if bookmark
  17608. (bookmark-jump bookmark)
  17609. (error "Cannot find location"))))
  17610. (defun org-quote-csv-field (s)
  17611. "Quote field for inclusion in CSV material."
  17612. (if (string-match "[\",]" s)
  17613. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  17614. s))
  17615. (defun org-force-self-insert (N)
  17616. "Needed to enforce self-insert under remapping."
  17617. (interactive "p")
  17618. (self-insert-command N))
  17619. (defun org-string-width (s)
  17620. "Compute width of string, ignoring invisible characters.
  17621. This ignores character with invisibility property `org-link', and also
  17622. characters with property `org-cwidth', because these will become invisible
  17623. upon the next fontification round."
  17624. (let (b l)
  17625. (when (or (eq t buffer-invisibility-spec)
  17626. (assq 'org-link buffer-invisibility-spec))
  17627. (while (setq b (text-property-any 0 (length s)
  17628. 'invisible 'org-link s))
  17629. (setq s (concat (substring s 0 b)
  17630. (substring s (or (next-single-property-change
  17631. b 'invisible s) (length s)))))))
  17632. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  17633. (setq s (concat (substring s 0 b)
  17634. (substring s (or (next-single-property-change
  17635. b 'org-cwidth s) (length s))))))
  17636. (setq l (string-width s) b -1)
  17637. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  17638. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  17639. l))
  17640. (defun org-shorten-string (s maxlength)
  17641. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  17642. If the string is shorter or has length MAXLENGTH, just return the
  17643. original string. If it is longer, the functions finds a space in the
  17644. string, breaks this string off at that locations and adds three dots
  17645. as ellipsis. Including the ellipsis, the string will not be longer
  17646. than MAXLENGTH. If finding a good breaking point in the string does
  17647. not work, the string is just chopped off in the middle of a word
  17648. if necessary."
  17649. (if (<= (length s) maxlength)
  17650. s
  17651. (let* ((n (max (- maxlength 4) 1))
  17652. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  17653. (if (string-match re s)
  17654. (concat (match-string 1 s) "...")
  17655. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  17656. (defun org-get-indentation (&optional line)
  17657. "Get the indentation of the current line, interpreting tabs.
  17658. When LINE is given, assume it represents a line and compute its indentation."
  17659. (if line
  17660. (if (string-match "^ *" (org-remove-tabs line))
  17661. (match-end 0))
  17662. (save-excursion
  17663. (beginning-of-line 1)
  17664. (skip-chars-forward " \t")
  17665. (current-column))))
  17666. (defun org-get-string-indentation (s)
  17667. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  17668. (let ((n -1) (i 0) (w tab-width) c)
  17669. (catch 'exit
  17670. (while (< (setq n (1+ n)) (length s))
  17671. (setq c (aref s n))
  17672. (cond ((= c ?\ ) (setq i (1+ i)))
  17673. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  17674. (t (throw 'exit t)))))
  17675. i))
  17676. (defun org-remove-tabs (s &optional width)
  17677. "Replace tabulators in S with spaces.
  17678. Assumes that s is a single line, starting in column 0."
  17679. (setq width (or width tab-width))
  17680. (while (string-match "\t" s)
  17681. (setq s (replace-match
  17682. (make-string
  17683. (- (* width (/ (+ (match-beginning 0) width) width))
  17684. (match-beginning 0)) ?\ )
  17685. t t s)))
  17686. s)
  17687. (defun org-fix-indentation (line ind)
  17688. "Fix indentation in LINE.
  17689. IND is a cons cell with target and minimum indentation.
  17690. If the current indentation in LINE is smaller than the minimum,
  17691. leave it alone. If it is larger than ind, set it to the target."
  17692. (let* ((l (org-remove-tabs line))
  17693. (i (org-get-indentation l))
  17694. (i1 (car ind)) (i2 (cdr ind)))
  17695. (if (>= i i2) (setq l (substring line i2)))
  17696. (if (> i1 0)
  17697. (concat (make-string i1 ?\ ) l)
  17698. l)))
  17699. (defun org-remove-indentation (code &optional n)
  17700. "Remove the maximum common indentation from the lines in CODE.
  17701. N may optionally be the number of spaces to remove."
  17702. (with-temp-buffer
  17703. (insert code)
  17704. (org-do-remove-indentation n)
  17705. (buffer-string)))
  17706. (defun org-do-remove-indentation (&optional n)
  17707. "Remove the maximum common indentation from the buffer."
  17708. (untabify (point-min) (point-max))
  17709. (let ((min 10000) re)
  17710. (if n
  17711. (setq min n)
  17712. (goto-char (point-min))
  17713. (while (re-search-forward "^ *[^ \n]" nil t)
  17714. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  17715. (unless (or (= min 0) (= min 10000))
  17716. (setq re (format "^ \\{%d\\}" min))
  17717. (goto-char (point-min))
  17718. (while (re-search-forward re nil t)
  17719. (replace-match "")
  17720. (end-of-line 1))
  17721. min)))
  17722. (defun org-fill-template (template alist)
  17723. "Find each %key of ALIST in TEMPLATE and replace it."
  17724. (let ((case-fold-search nil)
  17725. entry key value)
  17726. (setq alist (sort (copy-sequence alist)
  17727. (lambda (a b) (< (length (car a)) (length (car b))))))
  17728. (while (setq entry (pop alist))
  17729. (setq template
  17730. (replace-regexp-in-string
  17731. (concat "%" (regexp-quote (car entry)))
  17732. (cdr entry) template t t)))
  17733. template))
  17734. (defun org-base-buffer (buffer)
  17735. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  17736. (if (not buffer)
  17737. buffer
  17738. (or (buffer-base-buffer buffer)
  17739. buffer)))
  17740. (defun org-trim (s)
  17741. "Remove whitespace at beginning and end of string."
  17742. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  17743. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  17744. s)
  17745. (defun org-wrap (string &optional width lines)
  17746. "Wrap string to either a number of lines, or a width in characters.
  17747. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  17748. that costs. If there is a word longer than WIDTH, the text is actually
  17749. wrapped to the length of that word.
  17750. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  17751. many lines, whatever width that takes.
  17752. The return value is a list of lines, without newlines at the end."
  17753. (let* ((words (org-split-string string "[ \t\n]+"))
  17754. (maxword (apply 'max (mapcar 'org-string-width words)))
  17755. w ll)
  17756. (cond (width
  17757. (org-do-wrap words (max maxword width)))
  17758. (lines
  17759. (setq w maxword)
  17760. (setq ll (org-do-wrap words maxword))
  17761. (if (<= (length ll) lines)
  17762. ll
  17763. (setq ll words)
  17764. (while (> (length ll) lines)
  17765. (setq w (1+ w))
  17766. (setq ll (org-do-wrap words w)))
  17767. ll))
  17768. (t (error "Cannot wrap this")))))
  17769. (defun org-do-wrap (words width)
  17770. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  17771. (let (lines line)
  17772. (while words
  17773. (setq line (pop words))
  17774. (while (and words (< (+ (length line) (length (car words))) width))
  17775. (setq line (concat line " " (pop words))))
  17776. (setq lines (push line lines)))
  17777. (nreverse lines)))
  17778. (defun org-split-string (string &optional separators)
  17779. "Splits STRING into substrings at SEPARATORS.
  17780. No empty strings are returned if there are matches at the beginning
  17781. and end of string."
  17782. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  17783. (start 0)
  17784. notfirst
  17785. (list nil))
  17786. (while (and (string-match rexp string
  17787. (if (and notfirst
  17788. (= start (match-beginning 0))
  17789. (< start (length string)))
  17790. (1+ start) start))
  17791. (< (match-beginning 0) (length string)))
  17792. (setq notfirst t)
  17793. (or (eq (match-beginning 0) 0)
  17794. (and (eq (match-beginning 0) (match-end 0))
  17795. (eq (match-beginning 0) start))
  17796. (setq list
  17797. (cons (substring string start (match-beginning 0))
  17798. list)))
  17799. (setq start (match-end 0)))
  17800. (or (eq start (length string))
  17801. (setq list
  17802. (cons (substring string start)
  17803. list)))
  17804. (nreverse list)))
  17805. (defun org-quote-vert (s)
  17806. "Replace \"|\" with \"\\vert\"."
  17807. (while (string-match "|" s)
  17808. (setq s (replace-match "\\vert" t t s)))
  17809. s)
  17810. (defun org-uuidgen-p (s)
  17811. "Is S an ID created by UUIDGEN?"
  17812. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  17813. (defun org-context ()
  17814. "Return a list of contexts of the current cursor position.
  17815. If several contexts apply, all are returned.
  17816. Each context entry is a list with a symbol naming the context, and
  17817. two positions indicating start and end of the context. Possible
  17818. contexts are:
  17819. :headline anywhere in a headline
  17820. :headline-stars on the leading stars in a headline
  17821. :todo-keyword on a TODO keyword (including DONE) in a headline
  17822. :tags on the TAGS in a headline
  17823. :priority on the priority cookie in a headline
  17824. :item on the first line of a plain list item
  17825. :item-bullet on the bullet/number of a plain list item
  17826. :checkbox on the checkbox in a plain list item
  17827. :table in an org-mode table
  17828. :table-special on a special filed in a table
  17829. :table-table in a table.el table
  17830. :link on a hyperlink
  17831. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  17832. :target on a <<target>>
  17833. :radio-target on a <<<radio-target>>>
  17834. :latex-fragment on a LaTeX fragment
  17835. :latex-preview on a LaTeX fragment with overlaid preview image
  17836. This function expects the position to be visible because it uses font-lock
  17837. faces as a help to recognize the following contexts: :table-special, :link,
  17838. and :keyword."
  17839. (let* ((f (get-text-property (point) 'face))
  17840. (faces (if (listp f) f (list f)))
  17841. (p (point)) clist o)
  17842. ;; First the large context
  17843. (cond
  17844. ((org-on-heading-p t)
  17845. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  17846. (when (progn
  17847. (beginning-of-line 1)
  17848. (looking-at org-todo-line-tags-regexp))
  17849. (push (org-point-in-group p 1 :headline-stars) clist)
  17850. (push (org-point-in-group p 2 :todo-keyword) clist)
  17851. (push (org-point-in-group p 4 :tags) clist))
  17852. (goto-char p)
  17853. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  17854. (if (looking-at "\\[#[A-Z0-9]\\]")
  17855. (push (org-point-in-group p 0 :priority) clist)))
  17856. ((org-at-item-p)
  17857. (push (org-point-in-group p 2 :item-bullet) clist)
  17858. (push (list :item (point-at-bol)
  17859. (save-excursion (org-end-of-item) (point)))
  17860. clist)
  17861. (and (org-at-item-checkbox-p)
  17862. (push (org-point-in-group p 0 :checkbox) clist)))
  17863. ((org-at-table-p)
  17864. (push (list :table (org-table-begin) (org-table-end)) clist)
  17865. (if (memq 'org-formula faces)
  17866. (push (list :table-special
  17867. (previous-single-property-change p 'face)
  17868. (next-single-property-change p 'face)) clist)))
  17869. ((org-at-table-p 'any)
  17870. (push (list :table-table) clist)))
  17871. (goto-char p)
  17872. ;; Now the small context
  17873. (cond
  17874. ((org-at-timestamp-p)
  17875. (push (org-point-in-group p 0 :timestamp) clist))
  17876. ((memq 'org-link faces)
  17877. (push (list :link
  17878. (previous-single-property-change p 'face)
  17879. (next-single-property-change p 'face)) clist))
  17880. ((memq 'org-special-keyword faces)
  17881. (push (list :keyword
  17882. (previous-single-property-change p 'face)
  17883. (next-single-property-change p 'face)) clist))
  17884. ((org-on-target-p)
  17885. (push (org-point-in-group p 0 :target) clist)
  17886. (goto-char (1- (match-beginning 0)))
  17887. (if (looking-at org-radio-target-regexp)
  17888. (push (org-point-in-group p 0 :radio-target) clist))
  17889. (goto-char p))
  17890. ((setq o (car (delq nil
  17891. (mapcar
  17892. (lambda (x)
  17893. (if (memq x org-latex-fragment-image-overlays) x))
  17894. (overlays-at (point))))))
  17895. (push (list :latex-fragment
  17896. (overlay-start o) (overlay-end o)) clist)
  17897. (push (list :latex-preview
  17898. (overlay-start o) (overlay-end o)) clist))
  17899. ((org-inside-LaTeX-fragment-p)
  17900. ;; FIXME: positions wrong.
  17901. (push (list :latex-fragment (point) (point)) clist)))
  17902. (setq clist (nreverse (delq nil clist)))
  17903. clist))
  17904. ;; FIXME: Compare with at-regexp-p Do we need both?
  17905. (defun org-in-regexp (re &optional nlines visually)
  17906. "Check if point is inside a match of regexp.
  17907. Normally only the current line is checked, but you can include NLINES extra
  17908. lines both before and after point into the search.
  17909. If VISUALLY is set, require that the cursor is not after the match but
  17910. really on, so that the block visually is on the match."
  17911. (catch 'exit
  17912. (let ((pos (point))
  17913. (eol (point-at-eol (+ 1 (or nlines 0))))
  17914. (inc (if visually 1 0)))
  17915. (save-excursion
  17916. (beginning-of-line (- 1 (or nlines 0)))
  17917. (while (re-search-forward re eol t)
  17918. (if (and (<= (match-beginning 0) pos)
  17919. (>= (+ inc (match-end 0)) pos))
  17920. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  17921. (defun org-at-regexp-p (regexp)
  17922. "Is point inside a match of REGEXP in the current line?"
  17923. (catch 'exit
  17924. (save-excursion
  17925. (let ((pos (point)) (end (point-at-eol)))
  17926. (beginning-of-line 1)
  17927. (while (re-search-forward regexp end t)
  17928. (if (and (<= (match-beginning 0) pos)
  17929. (>= (match-end 0) pos))
  17930. (throw 'exit t)))
  17931. nil))))
  17932. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  17933. "Non-nil when point is between matches of START-RE and END-RE.
  17934. Also return a non-nil value when point is on one of the matches.
  17935. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  17936. buffer positions. Default values are the positions of headlines
  17937. surrounding the point.
  17938. The functions returns a cons cell whose car (resp. cdr) is the
  17939. position before START-RE (resp. after END-RE)."
  17940. (save-match-data
  17941. (let ((pos (point))
  17942. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  17943. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  17944. beg end)
  17945. (save-excursion
  17946. ;; Point is on a block when on START-RE or if START-RE can be
  17947. ;; found before it...
  17948. (and (or (org-at-regexp-p start-re)
  17949. (re-search-backward start-re limit-up t))
  17950. (setq beg (match-beginning 0))
  17951. ;; ... and END-RE after it...
  17952. (goto-char (match-end 0))
  17953. (re-search-forward end-re limit-down t)
  17954. (> (setq end (match-end 0)) pos)
  17955. ;; ... without another START-RE in-between.
  17956. (goto-char (match-beginning 0))
  17957. (not (re-search-backward start-re (1+ beg) t))
  17958. ;; Return value.
  17959. (cons beg end))))))
  17960. (defun org-in-block-p (names)
  17961. "Non-nil when point belongs to a block whose name belongs to NAMES.
  17962. NAMES is a list of strings containing names of blocks.
  17963. Return first block name matched, or nil. Beware that in case of
  17964. nested blocks, the returned name may not belong to the closest
  17965. block from point."
  17966. (save-match-data
  17967. (catch 'exit
  17968. (let ((case-fold-search t)
  17969. (lim-up (save-excursion (outline-previous-heading)))
  17970. (lim-down (save-excursion (outline-next-heading))))
  17971. (mapc (lambda (name)
  17972. (let ((n (regexp-quote name)))
  17973. (when (org-between-regexps-p
  17974. (concat "^[ \t]*#\\+begin_" n)
  17975. (concat "^[ \t]*#\\+end_" n)
  17976. lim-up lim-down)
  17977. (throw 'exit n))))
  17978. names))
  17979. nil)))
  17980. (defun org-occur-in-agenda-files (regexp &optional nlines)
  17981. "Call `multi-occur' with buffers for all agenda files."
  17982. (interactive "sOrg-files matching: \np")
  17983. (let* ((files (org-agenda-files))
  17984. (tnames (mapcar 'file-truename files))
  17985. (extra org-agenda-text-search-extra-files)
  17986. f)
  17987. (when (eq (car extra) 'agenda-archives)
  17988. (setq extra (cdr extra))
  17989. (setq files (org-add-archive-files files)))
  17990. (while (setq f (pop extra))
  17991. (unless (member (file-truename f) tnames)
  17992. (add-to-list 'files f 'append)
  17993. (add-to-list 'tnames (file-truename f) 'append)))
  17994. (multi-occur
  17995. (mapcar (lambda (x)
  17996. (with-current-buffer
  17997. (or (get-file-buffer x) (find-file-noselect x))
  17998. (widen)
  17999. (current-buffer)))
  18000. files)
  18001. regexp)))
  18002. (if (boundp 'occur-mode-find-occurrence-hook)
  18003. ;; Emacs 23
  18004. (add-hook 'occur-mode-find-occurrence-hook
  18005. (lambda ()
  18006. (when (eq major-mode 'org-mode)
  18007. (org-reveal))))
  18008. ;; Emacs 22
  18009. (defadvice occur-mode-goto-occurrence
  18010. (after org-occur-reveal activate)
  18011. (and (eq major-mode 'org-mode) (org-reveal)))
  18012. (defadvice occur-mode-goto-occurrence-other-window
  18013. (after org-occur-reveal activate)
  18014. (and (eq major-mode 'org-mode) (org-reveal)))
  18015. (defadvice occur-mode-display-occurrence
  18016. (after org-occur-reveal activate)
  18017. (when (eq major-mode 'org-mode)
  18018. (let ((pos (occur-mode-find-occurrence)))
  18019. (with-current-buffer (marker-buffer pos)
  18020. (save-excursion
  18021. (goto-char pos)
  18022. (org-reveal)))))))
  18023. (defun org-occur-link-in-agenda-files ()
  18024. "Create a link and search for it in the agendas.
  18025. The link is not stored in `org-stored-links', it is just created
  18026. for the search purpose."
  18027. (interactive)
  18028. (let ((link (condition-case nil
  18029. (org-store-link nil)
  18030. (error "Unable to create a link to here"))))
  18031. (org-occur-in-agenda-files (regexp-quote link))))
  18032. (defun org-uniquify (list)
  18033. "Remove duplicate elements from LIST."
  18034. (let (res)
  18035. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18036. res))
  18037. (defun org-delete-all (elts list)
  18038. "Remove all elements in ELTS from LIST."
  18039. (while elts
  18040. (setq list (delete (pop elts) list)))
  18041. list)
  18042. (defun org-count (cl-item cl-seq)
  18043. "Count the number of occurrences of ITEM in SEQ.
  18044. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18045. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18046. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18047. (while (< cl-start cl-end)
  18048. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18049. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18050. (setq cl-start (1+ cl-start)))
  18051. cl-count))
  18052. (defun org-remove-if (predicate seq)
  18053. "Remove everything from SEQ that fulfills PREDICATE."
  18054. (let (res e)
  18055. (while seq
  18056. (setq e (pop seq))
  18057. (if (not (funcall predicate e)) (push e res)))
  18058. (nreverse res)))
  18059. (defun org-remove-if-not (predicate seq)
  18060. "Remove everything from SEQ that does not fulfill PREDICATE."
  18061. (let (res e)
  18062. (while seq
  18063. (setq e (pop seq))
  18064. (if (funcall predicate e) (push e res)))
  18065. (nreverse res)))
  18066. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  18067. "Reduce two-argument FUNCTION across SEQ.
  18068. Taken from `reduce' in cl-seq.el with all keyword arguments but
  18069. \":initial-value\" removed."
  18070. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  18071. (cadr (memq :initial-value cl-keys)))
  18072. (cl-seq (pop cl-seq))
  18073. (t (funcall cl-func)))))
  18074. (while cl-seq
  18075. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  18076. cl-accum))
  18077. (defun org-back-over-empty-lines ()
  18078. "Move backwards over whitespace, to the beginning of the first empty line.
  18079. Returns the number of empty lines passed."
  18080. (let ((pos (point)))
  18081. (if (cdr (assoc 'heading org-blank-before-new-entry))
  18082. (skip-chars-backward " \t\n\r")
  18083. (unless (eobp)
  18084. (forward-line -1)))
  18085. (beginning-of-line 2)
  18086. (goto-char (min (point) pos))
  18087. (count-lines (point) pos)))
  18088. (defun org-skip-whitespace ()
  18089. (skip-chars-forward " \t\n\r"))
  18090. (defun org-point-in-group (point group &optional context)
  18091. "Check if POINT is in match-group GROUP.
  18092. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  18093. match. If the match group does not exist or point is not inside it,
  18094. return nil."
  18095. (and (match-beginning group)
  18096. (>= point (match-beginning group))
  18097. (<= point (match-end group))
  18098. (if context
  18099. (list context (match-beginning group) (match-end group))
  18100. t)))
  18101. (defun org-switch-to-buffer-other-window (&rest args)
  18102. "Switch to buffer in a second window on the current frame.
  18103. In particular, do not allow pop-up frames.
  18104. Returns the newly created buffer."
  18105. (let (pop-up-frames special-display-buffer-names special-display-regexps
  18106. special-display-function)
  18107. (apply 'switch-to-buffer-other-window args)))
  18108. (defun org-combine-plists (&rest plists)
  18109. "Create a single property list from all plists in PLISTS.
  18110. The process starts by copying the first list, and then setting properties
  18111. from the other lists. Settings in the last list are the most significant
  18112. ones and overrule settings in the other lists."
  18113. (let ((rtn (copy-sequence (pop plists)))
  18114. p v ls)
  18115. (while plists
  18116. (setq ls (pop plists))
  18117. (while ls
  18118. (setq p (pop ls) v (pop ls))
  18119. (setq rtn (plist-put rtn p v))))
  18120. rtn))
  18121. (defun org-move-line-down (arg)
  18122. "Move the current line down. With prefix argument, move it past ARG lines."
  18123. (interactive "p")
  18124. (let ((col (current-column))
  18125. beg end pos)
  18126. (beginning-of-line 1) (setq beg (point))
  18127. (beginning-of-line 2) (setq end (point))
  18128. (beginning-of-line (+ 1 arg))
  18129. (setq pos (move-marker (make-marker) (point)))
  18130. (insert (delete-and-extract-region beg end))
  18131. (goto-char pos)
  18132. (org-move-to-column col)))
  18133. (defun org-move-line-up (arg)
  18134. "Move the current line up. With prefix argument, move it past ARG lines."
  18135. (interactive "p")
  18136. (let ((col (current-column))
  18137. beg end pos)
  18138. (beginning-of-line 1) (setq beg (point))
  18139. (beginning-of-line 2) (setq end (point))
  18140. (beginning-of-line (- arg))
  18141. (setq pos (move-marker (make-marker) (point)))
  18142. (insert (delete-and-extract-region beg end))
  18143. (goto-char pos)
  18144. (org-move-to-column col)))
  18145. (defun org-replace-escapes (string table)
  18146. "Replace %-escapes in STRING with values in TABLE.
  18147. TABLE is an association list with keys like \"%a\" and string values.
  18148. The sequences in STRING may contain normal field width and padding information,
  18149. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  18150. so values can contain further %-escapes if they are define later in TABLE."
  18151. (let ((tbl (copy-alist table))
  18152. (case-fold-search nil)
  18153. (pchg 0)
  18154. e re rpl)
  18155. (while (setq e (pop tbl))
  18156. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  18157. (when (and (cdr e) (string-match re (cdr e)))
  18158. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  18159. (safe "SREF"))
  18160. (add-text-properties 0 3 (list 'sref sref) safe)
  18161. (setcdr e (replace-match safe t t (cdr e)))))
  18162. (while (string-match re string)
  18163. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  18164. (cdr e)))
  18165. (setq string (replace-match rpl t t string))))
  18166. (while (setq pchg (next-property-change pchg string))
  18167. (let ((sref (get-text-property pchg 'sref string)))
  18168. (when (and sref (string-match "SREF" string pchg))
  18169. (setq string (replace-match sref t t string)))))
  18170. string))
  18171. (defun org-sublist (list start end)
  18172. "Return a section of LIST, from START to END.
  18173. Counting starts at 1."
  18174. (let (rtn (c start))
  18175. (setq list (nthcdr (1- start) list))
  18176. (while (and list (<= c end))
  18177. (push (pop list) rtn)
  18178. (setq c (1+ c)))
  18179. (nreverse rtn)))
  18180. (defun org-find-base-buffer-visiting (file)
  18181. "Like `find-buffer-visiting' but always return the base buffer and
  18182. not an indirect buffer."
  18183. (let ((buf (or (get-file-buffer file)
  18184. (find-buffer-visiting file))))
  18185. (if buf
  18186. (or (buffer-base-buffer buf) buf)
  18187. nil)))
  18188. (defun org-image-file-name-regexp (&optional extensions)
  18189. "Return regexp matching the file names of images.
  18190. If EXTENSIONS is given, only match these."
  18191. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  18192. (image-file-name-regexp)
  18193. (let ((image-file-name-extensions
  18194. (or extensions
  18195. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  18196. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  18197. (concat "\\."
  18198. (regexp-opt (nconc (mapcar 'upcase
  18199. image-file-name-extensions)
  18200. image-file-name-extensions)
  18201. t)
  18202. "\\'"))))
  18203. (defun org-file-image-p (file &optional extensions)
  18204. "Return non-nil if FILE is an image."
  18205. (save-match-data
  18206. (string-match (org-image-file-name-regexp extensions) file)))
  18207. (defun org-get-cursor-date ()
  18208. "Return the date at cursor in as a time.
  18209. This works in the calendar and in the agenda, anywhere else it just
  18210. returns the current time."
  18211. (let (date day defd)
  18212. (cond
  18213. ((eq major-mode 'calendar-mode)
  18214. (setq date (calendar-cursor-to-date)
  18215. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  18216. ((eq major-mode 'org-agenda-mode)
  18217. (setq day (get-text-property (point) 'day))
  18218. (if day
  18219. (setq date (calendar-gregorian-from-absolute day)
  18220. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  18221. (nth 2 date))))))
  18222. (or defd (current-time))))
  18223. (defvar org-agenda-action-marker (make-marker)
  18224. "Marker pointing to the entry for the next agenda action.")
  18225. (defun org-mark-entry-for-agenda-action ()
  18226. "Mark the current entry as target of an agenda action.
  18227. Agenda actions are actions executed from the agenda with the key `k',
  18228. which make use of the date at the cursor."
  18229. (interactive)
  18230. (move-marker org-agenda-action-marker
  18231. (save-excursion (org-back-to-heading t) (point))
  18232. (current-buffer))
  18233. (message
  18234. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  18235. (defun org-mark-subtree ()
  18236. "Mark the current subtree.
  18237. This puts point at the start of the current subtree, and mark at the end.
  18238. If point is in an inline task, mark that task instead."
  18239. (interactive)
  18240. (let ((inline-task-p
  18241. (and (featurep 'org-inlinetask)
  18242. (org-inlinetask-in-task-p)))
  18243. (beg))
  18244. ;; Get beginning of subtree
  18245. (cond
  18246. (inline-task-p (org-inlinetask-goto-beginning))
  18247. ((org-at-heading-p) (beginning-of-line))
  18248. (t (org-with-limited-levels (outline-previous-visible-heading 1))))
  18249. (setq beg (point))
  18250. ;; Get end of it
  18251. (if inline-task-p
  18252. (org-inlinetask-goto-end)
  18253. (org-end-of-subtree))
  18254. ;; Mark zone
  18255. (push-mark (point) nil t)
  18256. (goto-char beg)))
  18257. ;;; Paragraph filling stuff.
  18258. ;; We want this to be just right, so use the full arsenal.
  18259. (defun org-indent-line-function ()
  18260. "Indent line depending on context."
  18261. (interactive)
  18262. (let* ((pos (point))
  18263. (itemp (org-at-item-p))
  18264. (case-fold-search t)
  18265. (org-drawer-regexp (or org-drawer-regexp "\000"))
  18266. (inline-task-p (and (featurep 'org-inlinetask)
  18267. (org-inlinetask-in-task-p)))
  18268. (inline-re (and inline-task-p
  18269. (org-inlinetask-outline-regexp)))
  18270. column)
  18271. (beginning-of-line 1)
  18272. (cond
  18273. ;; Comments
  18274. ((looking-at "# ") (setq column 0))
  18275. ;; Headings
  18276. ((looking-at org-outline-regexp) (setq column 0))
  18277. ;; Included files
  18278. ((looking-at "#\\+include:") (setq column 0))
  18279. ;; Footnote definition
  18280. ((looking-at org-footnote-definition-re) (setq column 0))
  18281. ;; Literal examples
  18282. ((looking-at "[ \t]*:\\( \\|$\\)")
  18283. (setq column (org-get-indentation))) ; do nothing
  18284. ;; Lists
  18285. ((ignore-errors (goto-char (org-in-item-p)))
  18286. (setq column (if itemp
  18287. (org-get-indentation)
  18288. (org-list-item-body-column (point))))
  18289. (goto-char pos))
  18290. ;; Drawers
  18291. ((and (looking-at "[ \t]*:END:")
  18292. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  18293. (save-excursion
  18294. (goto-char (1- (match-beginning 1)))
  18295. (setq column (current-column))))
  18296. ;; Special blocks
  18297. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  18298. (save-excursion
  18299. (re-search-backward
  18300. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  18301. (setq column (org-get-indentation (match-string 0))))
  18302. ((and (not (looking-at "[ \t]*#\\+begin_"))
  18303. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  18304. (save-excursion
  18305. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  18306. (setq column
  18307. (cond ((equal (downcase (match-string 1)) "src")
  18308. ;; src blocks: let `org-edit-src-exit' handle them
  18309. (org-get-indentation))
  18310. ((equal (downcase (match-string 1)) "example")
  18311. (max (org-get-indentation)
  18312. (org-get-indentation (match-string 0))))
  18313. (t
  18314. (org-get-indentation (match-string 0))))))
  18315. ;; This line has nothing special, look at the previous relevant
  18316. ;; line to compute indentation
  18317. (t
  18318. (beginning-of-line 0)
  18319. (while (and (not (bobp))
  18320. (not (looking-at org-drawer-regexp))
  18321. ;; When point started in an inline task, do not move
  18322. ;; above task starting line.
  18323. (not (and inline-task-p (looking-at inline-re)))
  18324. ;; Skip drawers, blocks, empty lines, verbatim,
  18325. ;; comments, tables, footnotes definitions, lists,
  18326. ;; inline tasks.
  18327. (or (and (looking-at "[ \t]*:END:")
  18328. (re-search-backward org-drawer-regexp nil t))
  18329. (and (looking-at "[ \t]*#\\+end_")
  18330. (re-search-backward "[ \t]*#\\+begin_"nil t))
  18331. (looking-at "[ \t]*[\n:#|]")
  18332. (looking-at org-footnote-definition-re)
  18333. (and (ignore-errors (goto-char (org-in-item-p)))
  18334. (goto-char
  18335. (org-list-get-top-point (org-list-struct))))
  18336. (and (not inline-task-p)
  18337. (featurep 'org-inlinetask)
  18338. (org-inlinetask-in-task-p)
  18339. (or (org-inlinetask-goto-beginning) t))))
  18340. (beginning-of-line 0))
  18341. (cond
  18342. ;; There was an heading above.
  18343. ((looking-at "\\*+[ \t]+")
  18344. (if (not org-adapt-indentation)
  18345. (setq column 0)
  18346. (goto-char (match-end 0))
  18347. (setq column (current-column))))
  18348. ;; A drawer had started and is unfinished
  18349. ((looking-at org-drawer-regexp)
  18350. (goto-char (1- (match-beginning 1)))
  18351. (setq column (current-column)))
  18352. ;; Else, nothing noticeable found: get indentation and go on.
  18353. (t (setq column (org-get-indentation))))))
  18354. ;; Now apply indentation and move cursor accordingly
  18355. (goto-char pos)
  18356. (if (<= (current-column) (current-indentation))
  18357. (org-indent-line-to column)
  18358. (save-excursion (org-indent-line-to column)))
  18359. ;; Special polishing for properties, see `org-property-format'
  18360. (setq column (current-column))
  18361. (beginning-of-line 1)
  18362. (if (looking-at
  18363. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  18364. (replace-match (concat (match-string 1)
  18365. (format org-property-format
  18366. (match-string 2) (match-string 3)))
  18367. t t))
  18368. (org-move-to-column column)))
  18369. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  18370. "Variable to store copy of `adaptive-fill-regexp'.
  18371. Since `adaptive-fill-regexp' is set to never match, we need to
  18372. store a backup of its value before entering `org-mode' so that
  18373. the functionality can be provided as a fall-back.")
  18374. (defun org-set-autofill-regexps ()
  18375. (interactive)
  18376. ;; In the paragraph separator we include headlines, because filling
  18377. ;; text in a line directly attached to a headline would otherwise
  18378. ;; fill the headline as well.
  18379. (org-set-local 'comment-start-skip "^#+[ \t]*")
  18380. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  18381. ;; The paragraph starter includes hand-formatted lists.
  18382. (org-set-local
  18383. 'paragraph-start
  18384. (concat
  18385. "\f" "\\|"
  18386. "[ ]*$" "\\|"
  18387. org-outline-regexp "\\|"
  18388. "[ \t]*#" "\\|"
  18389. (org-item-re) "\\|"
  18390. "[ \t]*[:|]" "\\|"
  18391. "\\$\\$" "\\|"
  18392. "\\\\\\(begin\\|end\\|[][]\\)"))
  18393. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  18394. ;; But only if the user has not turned off tables or fixed-width regions
  18395. (org-set-local
  18396. 'auto-fill-inhibit-regexp
  18397. (concat org-outline-regexp
  18398. "\\|#\\+"
  18399. "\\|[ \t]*" org-keyword-time-regexp
  18400. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  18401. (concat
  18402. "\\|[ \t]*["
  18403. (if org-enable-table-editor "|" "")
  18404. (if org-enable-fixed-width-editor ":" "")
  18405. "]"))))
  18406. ;; We use our own fill-paragraph function, to make sure that tables
  18407. ;; and fixed-width regions are not wrapped. That function will pass
  18408. ;; through to `fill-paragraph' when appropriate.
  18409. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  18410. ;; Prevent auto-fill from inserting unwanted new items.
  18411. (if (boundp 'fill-nobreak-predicate)
  18412. (org-set-local 'fill-nobreak-predicate
  18413. (if (memq 'org-fill-item-nobreak-p fill-nobreak-predicate)
  18414. fill-nobreak-predicate
  18415. (cons 'org-fill-item-nobreak-p fill-nobreak-predicate))))
  18416. ;; Adaptive filling: To get full control, first make sure that
  18417. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  18418. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  18419. (org-set-local 'org-adaptive-fill-regexp-backup
  18420. adaptive-fill-regexp))
  18421. (org-set-local 'adaptive-fill-regexp "\000")
  18422. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  18423. (org-set-local 'adaptive-fill-function
  18424. 'org-adaptive-fill-function)
  18425. (org-set-local
  18426. 'align-mode-rules-list
  18427. '((org-in-buffer-settings
  18428. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  18429. (modes . '(org-mode))))))
  18430. (defun org-fill-item-nobreak-p ()
  18431. "Non-nil when a line break at point would insert a new item."
  18432. (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
  18433. (defun org-fill-paragraph (&optional justify)
  18434. "Re-align a table, pass through to fill-paragraph if no table."
  18435. (let ((table-p (org-at-table-p))
  18436. (table.el-p (org-at-table.el-p))
  18437. (itemp (org-in-item-p)))
  18438. (cond ((and (equal (char-after (point-at-bol)) ?*)
  18439. (save-excursion (goto-char (point-at-bol))
  18440. (looking-at org-outline-regexp)))
  18441. t) ; skip headlines
  18442. (table.el-p t) ; skip table.el tables
  18443. (table-p (org-table-align) t) ; align Org tables
  18444. (itemp ; align text in items
  18445. (let* ((struct (save-excursion (goto-char itemp)
  18446. (org-list-struct)))
  18447. (parents (org-list-parents-alist struct))
  18448. (children (org-list-get-children itemp struct parents))
  18449. beg end prev next prefix)
  18450. ;; Determine in which part of item point is: before
  18451. ;; first child, after last child, between two
  18452. ;; sub-lists, or simply in item if there's no child.
  18453. (cond
  18454. ((not children)
  18455. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18456. beg itemp
  18457. end (org-list-get-item-end itemp struct)))
  18458. ((< (point) (setq next (car children)))
  18459. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18460. beg itemp
  18461. end next))
  18462. ((> (point) (setq prev (car (last children))))
  18463. (setq beg (org-list-get-item-end prev struct)
  18464. end (org-list-get-item-end itemp struct)
  18465. prefix (save-excursion
  18466. (goto-char beg)
  18467. (skip-chars-forward " \t")
  18468. (make-string (current-column) ?\ ))))
  18469. (t (catch 'exit
  18470. (while (setq next (pop children))
  18471. (if (> (point) next)
  18472. (setq prev next)
  18473. (setq beg (org-list-get-item-end prev struct)
  18474. end next
  18475. prefix (save-excursion
  18476. (goto-char beg)
  18477. (skip-chars-forward " \t")
  18478. (make-string (current-column) ?\ )))
  18479. (throw 'exit nil))))))
  18480. ;; Use `fill-paragraph' with buffer narrowed to item
  18481. ;; without any child, and with our computed PREFIX.
  18482. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18483. (save-restriction
  18484. (narrow-to-region beg end)
  18485. (save-excursion (fill-paragraph justify)))) t))
  18486. ;; Special case where point is not in a list but is on
  18487. ;; a paragraph adjacent to a list: make sure this paragraph
  18488. ;; doesn't get merged with the end of the list by narrowing
  18489. ;; buffer first.
  18490. ((save-excursion (forward-paragraph -1)
  18491. (setq itemp (org-in-item-p)))
  18492. (let ((struct (save-excursion (goto-char itemp)
  18493. (org-list-struct))))
  18494. (save-restriction
  18495. (narrow-to-region (org-list-get-bottom-point struct)
  18496. (save-excursion (forward-paragraph 1)
  18497. (point)))
  18498. (fill-paragraph justify) t)))
  18499. ;; Else simply call `fill-paragraph'.
  18500. (t nil))))
  18501. ;; For reference, this is the default value of adaptive-fill-regexp
  18502. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  18503. (defun org-adaptive-fill-function ()
  18504. "Return a fill prefix for org-mode files."
  18505. (let (itemp)
  18506. (save-excursion
  18507. (cond
  18508. ;; Comment line
  18509. ((looking-at "#[ \t]+")
  18510. (match-string-no-properties 0))
  18511. ;; Plain list item
  18512. ((org-at-item-p)
  18513. (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
  18514. ;; Point is in a list after `backward-paragraph': original
  18515. ;; point wasn't in the list, or filling would have been taken
  18516. ;; care of by `org-auto-fill-function', but the list and the
  18517. ;; real paragraph are not separated by a blank line. Thus, move
  18518. ;; point after the list to go back to real paragraph and
  18519. ;; determine fill-prefix.
  18520. ((setq itemp (org-in-item-p))
  18521. (goto-char itemp)
  18522. (let* ((struct (org-list-struct))
  18523. (bottom (org-list-get-bottom-point struct)))
  18524. (goto-char bottom)
  18525. (make-string (org-get-indentation) ?\ )))
  18526. ;; Other text
  18527. ((looking-at org-adaptive-fill-regexp-backup)
  18528. (match-string-no-properties 0))))))
  18529. (defun org-auto-fill-function ()
  18530. "Auto-fill function."
  18531. (let (itemp prefix)
  18532. ;; When in a list, compute an appropriate fill-prefix and make
  18533. ;; sure it will be used by `do-auto-fill'.
  18534. (if (setq itemp (org-in-item-p))
  18535. (progn
  18536. (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
  18537. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18538. (do-auto-fill)))
  18539. ;; Else just use `do-auto-fill'.
  18540. (do-auto-fill))))
  18541. ;;; Other stuff.
  18542. (defun org-toggle-fixed-width-section (arg)
  18543. "Toggle the fixed-width export.
  18544. If there is no active region, the QUOTE keyword at the current headline is
  18545. inserted or removed. When present, it causes the text between this headline
  18546. and the next to be exported as fixed-width text, and unmodified.
  18547. If there is an active region, this command adds or removes a colon as the
  18548. first character of this line. If the first character of a line is a colon,
  18549. this line is also exported in fixed-width font."
  18550. (interactive "P")
  18551. (let* ((cc 0)
  18552. (regionp (org-region-active-p))
  18553. (beg (if regionp (region-beginning) (point)))
  18554. (end (if regionp (region-end)))
  18555. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  18556. (case-fold-search nil)
  18557. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  18558. off)
  18559. (if regionp
  18560. (save-excursion
  18561. (goto-char beg)
  18562. (setq cc (current-column))
  18563. (beginning-of-line 1)
  18564. (setq off (looking-at re))
  18565. (while (> nlines 0)
  18566. (setq nlines (1- nlines))
  18567. (beginning-of-line 1)
  18568. (cond
  18569. (arg
  18570. (org-move-to-column cc t)
  18571. (insert ": \n")
  18572. (forward-line -1))
  18573. ((and off (looking-at re))
  18574. (replace-match "" t t nil 1))
  18575. ((not off) (org-move-to-column cc t) (insert ": ")))
  18576. (forward-line 1)))
  18577. (save-excursion
  18578. (org-back-to-heading)
  18579. (cond
  18580. ((looking-at (format org-heading-keyword-regexp-format
  18581. org-quote-string))
  18582. (goto-char (match-end 1))
  18583. (looking-at (concat " +" org-quote-string))
  18584. (replace-match "" t t)
  18585. (when (eolp) (insert " ")))
  18586. ((looking-at org-outline-regexp)
  18587. (goto-char (match-end 0))
  18588. (insert org-quote-string " ")))))))
  18589. (defun org-reftex-citation ()
  18590. "Use reftex-citation to insert a citation into the buffer.
  18591. This looks for a line like
  18592. #+BIBLIOGRAPHY: foo plain option:-d
  18593. and derives from it that foo.bib is the bibliography file relevant
  18594. for this document. It then installs the necessary environment for RefTeX
  18595. to work in this buffer and calls `reftex-citation' to insert a citation
  18596. into the buffer.
  18597. Export of such citations to both LaTeX and HTML is handled by the contributed
  18598. package org-exp-bibtex by Taru Karttunen."
  18599. (interactive)
  18600. (let ((reftex-docstruct-symbol 'rds)
  18601. (reftex-cite-format "\\cite{%l}")
  18602. rds bib)
  18603. (save-excursion
  18604. (save-restriction
  18605. (widen)
  18606. (let ((case-fold-search t)
  18607. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  18608. (if (not (save-excursion
  18609. (or (re-search-forward re nil t)
  18610. (re-search-backward re nil t))))
  18611. (error "No bibliography defined in file")
  18612. (setq bib (concat (match-string 1) ".bib")
  18613. rds (list (list 'bib bib)))))))
  18614. (call-interactively 'reftex-citation)))
  18615. ;;;; Functions extending outline functionality
  18616. (defun org-beginning-of-line (&optional arg)
  18617. "Go to the beginning of the current line. If that is invisible, continue
  18618. to a visible line beginning. This makes the function of C-a more intuitive.
  18619. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18620. first attempt, and only move to after the tags when the cursor is already
  18621. beyond the end of the headline."
  18622. (interactive "P")
  18623. (let ((pos (point))
  18624. (special (if (consp org-special-ctrl-a/e)
  18625. (car org-special-ctrl-a/e)
  18626. org-special-ctrl-a/e))
  18627. refpos)
  18628. (if (org-bound-and-true-p line-move-visual)
  18629. (beginning-of-visual-line 1)
  18630. (beginning-of-line 1))
  18631. (if (and arg (fboundp 'move-beginning-of-line))
  18632. (call-interactively 'move-beginning-of-line)
  18633. (if (bobp)
  18634. nil
  18635. (backward-char 1)
  18636. (if (org-truely-invisible-p)
  18637. (while (and (not (bobp)) (org-truely-invisible-p))
  18638. (backward-char 1)
  18639. (beginning-of-line 1))
  18640. (forward-char 1))))
  18641. (when special
  18642. (cond
  18643. ((and (looking-at org-complex-heading-regexp)
  18644. (= (char-after (match-end 1)) ?\ ))
  18645. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  18646. (point-at-eol)))
  18647. (goto-char
  18648. (if (eq special t)
  18649. (cond ((> pos refpos) refpos)
  18650. ((= pos (point)) refpos)
  18651. (t (point)))
  18652. (cond ((> pos (point)) (point))
  18653. ((not (eq last-command this-command)) (point))
  18654. (t refpos)))))
  18655. ((org-at-item-p)
  18656. (goto-char
  18657. (if (eq special t)
  18658. (cond ((> pos (match-end 0)) (match-end 0))
  18659. ((= pos (point)) (match-end 0))
  18660. (t (point)))
  18661. (cond ((> pos (point)) (point))
  18662. ((not (eq last-command this-command)) (point))
  18663. (t (match-end 0))))))))
  18664. (org-no-warnings
  18665. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18666. (defun org-end-of-line (&optional arg)
  18667. "Go to the end of the line.
  18668. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18669. first attempt, and only move to after the tags when the cursor is already
  18670. beyond the end of the headline."
  18671. (interactive "P")
  18672. (let ((special (if (consp org-special-ctrl-a/e)
  18673. (cdr org-special-ctrl-a/e)
  18674. org-special-ctrl-a/e)))
  18675. (cond
  18676. ((or (not special) arg
  18677. (not (or (org-on-heading-p) (org-at-item-p))))
  18678. (call-interactively
  18679. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  18680. ((fboundp 'move-end-of-line) 'move-end-of-line)
  18681. (t 'end-of-line))))
  18682. ((org-on-heading-p)
  18683. (let ((pos (point)))
  18684. (beginning-of-line 1)
  18685. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  18686. (if (eq special t)
  18687. (if (or (< pos (match-beginning 1))
  18688. (= pos (match-end 0)))
  18689. (goto-char (match-beginning 1))
  18690. (goto-char (match-end 0)))
  18691. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  18692. (goto-char (match-end 0))
  18693. (goto-char (match-beginning 1))))
  18694. (call-interactively (if (fboundp 'move-end-of-line)
  18695. 'move-end-of-line
  18696. 'end-of-line)))))
  18697. ;; At an item: Move before any hidden text.
  18698. (t (call-interactively 'end-of-line)))
  18699. (org-no-warnings
  18700. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18701. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  18702. (define-key org-mode-map "\C-e" 'org-end-of-line)
  18703. (defun org-backward-sentence (&optional arg)
  18704. "Go to beginning of sentence, or beginning of table field.
  18705. This will call `backward-sentence' or `org-table-beginning-of-field',
  18706. depending on context."
  18707. (interactive "P")
  18708. (cond
  18709. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  18710. (t (call-interactively 'backward-sentence))))
  18711. (defun org-forward-sentence (&optional arg)
  18712. "Go to end of sentence, or end of table field.
  18713. This will call `forward-sentence' or `org-table-end-of-field',
  18714. depending on context."
  18715. (interactive "P")
  18716. (cond
  18717. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  18718. (t (call-interactively 'forward-sentence))))
  18719. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  18720. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  18721. (defun org-kill-line (&optional arg)
  18722. "Kill line, to tags or end of line."
  18723. (interactive "P")
  18724. (cond
  18725. ((or (not org-special-ctrl-k)
  18726. (bolp)
  18727. (not (org-on-heading-p)))
  18728. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  18729. org-ctrl-k-protect-subtree)
  18730. (if (or (eq org-ctrl-k-protect-subtree 'error)
  18731. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  18732. (error "C-k aborted - would kill hidden subtree")))
  18733. (call-interactively 'kill-line))
  18734. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  18735. (kill-region (point) (match-beginning 1))
  18736. (org-set-tags nil t))
  18737. (t (kill-region (point) (point-at-eol)))))
  18738. (define-key org-mode-map "\C-k" 'org-kill-line)
  18739. (defun org-yank (&optional arg)
  18740. "Yank. If the kill is a subtree, treat it specially.
  18741. This command will look at the current kill and check if is a single
  18742. subtree, or a series of subtrees[1]. If it passes the test, and if the
  18743. cursor is at the beginning of a line or after the stars of a currently
  18744. empty headline, then the yank is handled specially. How exactly depends
  18745. on the value of the following variables, both set by default.
  18746. org-yank-folded-subtrees
  18747. When set, the subtree(s) will be folded after insertion, but only
  18748. if doing so would now swallow text after the yanked text.
  18749. org-yank-adjusted-subtrees
  18750. When set, the subtree will be promoted or demoted in order to
  18751. fit into the local outline tree structure, which means that the level
  18752. will be adjusted so that it becomes the smaller one of the two
  18753. *visible* surrounding headings.
  18754. Any prefix to this command will cause `yank' to be called directly with
  18755. no special treatment. In particular, a simple \\[universal-argument] prefix \
  18756. will just
  18757. plainly yank the text as it is.
  18758. \[1] The test checks if the first non-white line is a heading
  18759. and if there are no other headings with fewer stars."
  18760. (interactive "P")
  18761. (org-yank-generic 'yank arg))
  18762. (defun org-yank-generic (command arg)
  18763. "Perform some yank-like command.
  18764. This function implements the behavior described in the `org-yank'
  18765. documentation. However, it has been generalized to work for any
  18766. interactive command with similar behavior."
  18767. ;; pretend to be command COMMAND
  18768. (setq this-command command)
  18769. (if arg
  18770. (call-interactively command)
  18771. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  18772. (and (org-kill-is-subtree-p)
  18773. (or (bolp)
  18774. (and (looking-at "[ \t]*$")
  18775. (string-match
  18776. "\\`\\*+\\'"
  18777. (buffer-substring (point-at-bol) (point)))))))
  18778. swallowp)
  18779. (cond
  18780. ((and subtreep org-yank-folded-subtrees)
  18781. (let ((beg (point))
  18782. end)
  18783. (if (and subtreep org-yank-adjusted-subtrees)
  18784. (org-paste-subtree nil nil 'for-yank)
  18785. (call-interactively command))
  18786. (setq end (point))
  18787. (goto-char beg)
  18788. (when (and (bolp) subtreep
  18789. (not (setq swallowp
  18790. (org-yank-folding-would-swallow-text beg end))))
  18791. (org-with-limited-levels
  18792. (or (looking-at org-outline-regexp)
  18793. (re-search-forward org-outline-regexp-bol end t))
  18794. (while (and (< (point) end) (looking-at org-outline-regexp))
  18795. (hide-subtree)
  18796. (org-cycle-show-empty-lines 'folded)
  18797. (condition-case nil
  18798. (outline-forward-same-level 1)
  18799. (error (goto-char end))))))
  18800. (when swallowp
  18801. (message
  18802. "Inserted text not folded because that would swallow text"))
  18803. (goto-char end)
  18804. (skip-chars-forward " \t\n\r")
  18805. (beginning-of-line 1)
  18806. (push-mark beg 'nomsg)))
  18807. ((and subtreep org-yank-adjusted-subtrees)
  18808. (let ((beg (point-at-bol)))
  18809. (org-paste-subtree nil nil 'for-yank)
  18810. (push-mark beg 'nomsg)))
  18811. (t
  18812. (call-interactively command))))))
  18813. (defun org-yank-folding-would-swallow-text (beg end)
  18814. "Would hide-subtree at BEG swallow any text after END?"
  18815. (let (level)
  18816. (org-with-limited-levels
  18817. (save-excursion
  18818. (goto-char beg)
  18819. (when (or (looking-at org-outline-regexp)
  18820. (re-search-forward org-outline-regexp-bol end t))
  18821. (setq level (org-outline-level)))
  18822. (goto-char end)
  18823. (skip-chars-forward " \t\r\n\v\f")
  18824. (if (or (eobp)
  18825. (and (bolp) (looking-at org-outline-regexp)
  18826. (<= (org-outline-level) level)))
  18827. nil ; Nothing would be swallowed
  18828. t))))) ; something would swallow
  18829. (define-key org-mode-map "\C-y" 'org-yank)
  18830. (defun org-truely-invisible-p ()
  18831. "Check if point is at a character currently not visible.
  18832. This version does not only check the character property, but also
  18833. `visible-mode'."
  18834. ;; Early versions of noutline don't have `outline-invisible-p'.
  18835. (if (org-bound-and-true-p visible-mode)
  18836. nil
  18837. (outline-invisible-p)))
  18838. (defun org-invisible-p2 ()
  18839. "Check if point is at a character currently not visible."
  18840. (save-excursion
  18841. (if (and (eolp) (not (bobp))) (backward-char 1))
  18842. ;; Early versions of noutline don't have `outline-invisible-p'.
  18843. (outline-invisible-p)))
  18844. (defun org-back-to-heading (&optional invisible-ok)
  18845. "Call `outline-back-to-heading', but provide a better error message."
  18846. (condition-case nil
  18847. (outline-back-to-heading invisible-ok)
  18848. (error (error "Before first headline at position %d in buffer %s"
  18849. (point) (current-buffer)))))
  18850. (defun org-beginning-of-defun ()
  18851. "Go to the beginning of the subtree, i.e. back to the heading."
  18852. (org-back-to-heading))
  18853. (defun org-end-of-defun ()
  18854. "Go to the end of the subtree."
  18855. (org-end-of-subtree nil t))
  18856. (defun org-before-first-heading-p ()
  18857. "Before first heading?"
  18858. (save-excursion
  18859. (end-of-line)
  18860. (null (re-search-backward org-outline-regexp-bol nil t))))
  18861. (defun org-on-heading-p (&optional ignored)
  18862. (outline-on-heading-p t))
  18863. (defun org-at-heading-p (&optional ignored)
  18864. (outline-on-heading-p t))
  18865. (defun org-point-at-end-of-empty-headline ()
  18866. "If point is at the end of an empty headline, return t, else nil.
  18867. If the heading only contains a TODO keyword, it is still still considered
  18868. empty."
  18869. (and (looking-at "[ \t]*$")
  18870. (save-excursion
  18871. (beginning-of-line 1)
  18872. (let ((case-fold-search nil))
  18873. (looking-at org-todo-line-regexp)))
  18874. (string= (match-string 3) "")))
  18875. (defun org-at-heading-or-item-p ()
  18876. (or (org-on-heading-p) (org-at-item-p)))
  18877. (defun org-on-target-p ()
  18878. (or (org-in-regexp org-radio-target-regexp)
  18879. (org-in-regexp org-target-regexp)))
  18880. (defun org-up-heading-all (arg)
  18881. "Move to the heading line of which the present line is a subheading.
  18882. This function considers both visible and invisible heading lines.
  18883. With argument, move up ARG levels."
  18884. (if (fboundp 'outline-up-heading-all)
  18885. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  18886. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  18887. (defun org-up-heading-safe ()
  18888. "Move to the heading line of which the present line is a subheading.
  18889. This version will not throw an error. It will return the level of the
  18890. headline found, or nil if no higher level is found.
  18891. Also, this function will be a lot faster than `outline-up-heading',
  18892. because it relies on stars being the outline starters. This can really
  18893. make a significant difference in outlines with very many siblings."
  18894. (let (start-level re)
  18895. (org-back-to-heading t)
  18896. (setq start-level (funcall outline-level))
  18897. (if (equal start-level 1)
  18898. nil
  18899. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  18900. (if (re-search-backward re nil t)
  18901. (funcall outline-level)))))
  18902. (defun org-first-sibling-p ()
  18903. "Is this heading the first child of its parents?"
  18904. (interactive)
  18905. (let ((re org-outline-regexp-bol)
  18906. level l)
  18907. (unless (org-at-heading-p t)
  18908. (error "Not at a heading"))
  18909. (setq level (funcall outline-level))
  18910. (save-excursion
  18911. (if (not (re-search-backward re nil t))
  18912. t
  18913. (setq l (funcall outline-level))
  18914. (< l level)))))
  18915. (defun org-goto-sibling (&optional previous)
  18916. "Goto the next sibling, even if it is invisible.
  18917. When PREVIOUS is set, go to the previous sibling instead. Returns t
  18918. when a sibling was found. When none is found, return nil and don't
  18919. move point."
  18920. (let ((fun (if previous 're-search-backward 're-search-forward))
  18921. (pos (point))
  18922. (re org-outline-regexp-bol)
  18923. level l)
  18924. (when (condition-case nil (org-back-to-heading t) (error nil))
  18925. (setq level (funcall outline-level))
  18926. (catch 'exit
  18927. (or previous (forward-char 1))
  18928. (while (funcall fun re nil t)
  18929. (setq l (funcall outline-level))
  18930. (when (< l level) (goto-char pos) (throw 'exit nil))
  18931. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  18932. (goto-char pos)
  18933. nil))))
  18934. (defun org-show-siblings ()
  18935. "Show all siblings of the current headline."
  18936. (save-excursion
  18937. (while (org-goto-sibling) (org-flag-heading nil)))
  18938. (save-excursion
  18939. (while (org-goto-sibling 'previous)
  18940. (org-flag-heading nil))))
  18941. (defun org-goto-first-child ()
  18942. "Goto the first child, even if it is invisible.
  18943. Return t when a child was found. Otherwise don't move point and
  18944. return nil."
  18945. (let (level (pos (point)) (re org-outline-regexp-bol))
  18946. (when (condition-case nil (org-back-to-heading t) (error nil))
  18947. (setq level (outline-level))
  18948. (forward-char 1)
  18949. (if (and (re-search-forward re nil t) (> (outline-level) level))
  18950. (progn (goto-char (match-beginning 0)) t)
  18951. (goto-char pos) nil))))
  18952. (defun org-show-hidden-entry ()
  18953. "Show an entry where even the heading is hidden."
  18954. (save-excursion
  18955. (org-show-entry)))
  18956. (defun org-flag-heading (flag &optional entry)
  18957. "Flag the current heading. FLAG non-nil means make invisible.
  18958. When ENTRY is non-nil, show the entire entry."
  18959. (save-excursion
  18960. (org-back-to-heading t)
  18961. ;; Check if we should show the entire entry
  18962. (if entry
  18963. (progn
  18964. (org-show-entry)
  18965. (save-excursion
  18966. (and (outline-next-heading)
  18967. (org-flag-heading nil))))
  18968. (outline-flag-region (max (point-min) (1- (point)))
  18969. (save-excursion (outline-end-of-heading) (point))
  18970. flag))))
  18971. (defun org-get-next-sibling ()
  18972. "Move to next heading of the same level, and return point.
  18973. If there is no such heading, return nil.
  18974. This is like outline-next-sibling, but invisible headings are ok."
  18975. (let ((level (funcall outline-level)))
  18976. (outline-next-heading)
  18977. (while (and (not (eobp)) (> (funcall outline-level) level))
  18978. (outline-next-heading))
  18979. (if (or (eobp) (< (funcall outline-level) level))
  18980. nil
  18981. (point))))
  18982. (defun org-get-last-sibling ()
  18983. "Move to previous heading of the same level, and return point.
  18984. If there is no such heading, return nil."
  18985. (let ((opoint (point))
  18986. (level (funcall outline-level)))
  18987. (outline-previous-heading)
  18988. (when (and (/= (point) opoint) (outline-on-heading-p t))
  18989. (while (and (> (funcall outline-level) level)
  18990. (not (bobp)))
  18991. (outline-previous-heading))
  18992. (if (< (funcall outline-level) level)
  18993. nil
  18994. (point)))))
  18995. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  18996. ;; This contains an exact copy of the original function, but it uses
  18997. ;; `org-back-to-heading', to make it work also in invisible
  18998. ;; trees. And is uses an invisible-OK argument.
  18999. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  19000. ;; Furthermore, when used inside Org, finding the end of a large subtree
  19001. ;; with many children and grandchildren etc, this can be much faster
  19002. ;; than the outline version.
  19003. (org-back-to-heading invisible-OK)
  19004. (let ((first t)
  19005. (level (funcall outline-level)))
  19006. (if (and (eq major-mode 'org-mode) (< level 1000))
  19007. ;; A true heading (not a plain list item), in Org-mode
  19008. ;; This means we can easily find the end by looking
  19009. ;; only for the right number of stars. Using a regexp to do
  19010. ;; this is so much faster than using a Lisp loop.
  19011. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  19012. (forward-char 1)
  19013. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  19014. ;; something else, do it the slow way
  19015. (while (and (not (eobp))
  19016. (or first (> (funcall outline-level) level)))
  19017. (setq first nil)
  19018. (outline-next-heading)))
  19019. (unless to-heading
  19020. (if (memq (preceding-char) '(?\n ?\^M))
  19021. (progn
  19022. ;; Go to end of line before heading
  19023. (forward-char -1)
  19024. (if (memq (preceding-char) '(?\n ?\^M))
  19025. ;; leave blank line before heading
  19026. (forward-char -1))))))
  19027. (point))
  19028. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  19029. "Use Org version in org-mode, for dramatic speed-up."
  19030. (if (eq major-mode 'org-mode)
  19031. (progn
  19032. (org-end-of-subtree nil t)
  19033. (unless (eobp) (backward-char 1)))
  19034. ad-do-it))
  19035. (defun org-end-of-meta-data-and-drawers ()
  19036. "Jump to the first text after meta data and drawers in the current entry.
  19037. This will move over empty lines, lines with planning time stamps,
  19038. clocking lines, and drawers."
  19039. (org-back-to-heading t)
  19040. (let ((end (save-excursion (outline-next-heading) (point)))
  19041. (re (concat "\\(" org-drawer-regexp "\\)"
  19042. "\\|" "[ \t]*" org-keyword-time-regexp)))
  19043. (forward-line 1)
  19044. (while (re-search-forward re end t)
  19045. (if (not (match-end 1))
  19046. ;; empty or planning line
  19047. (forward-line 1)
  19048. ;; a drawer, find the end
  19049. (re-search-forward "^[ \t]*:END:" end 'move)
  19050. (forward-line 1)))
  19051. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  19052. (point)))
  19053. (defun org-forward-same-level (arg &optional invisible-ok)
  19054. "Move forward to the arg'th subheading at same level as this one.
  19055. Stop at the first and last subheadings of a superior heading.
  19056. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  19057. it wil also look at invisible ones."
  19058. (interactive "p")
  19059. (org-back-to-heading invisible-ok)
  19060. (org-on-heading-p)
  19061. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19062. (re (format "^\\*\\{1,%d\\} " level))
  19063. l)
  19064. (forward-char 1)
  19065. (while (> arg 0)
  19066. (while (and (re-search-forward re nil 'move)
  19067. (setq l (- (match-end 0) (match-beginning 0) 1))
  19068. (= l level)
  19069. (not invisible-ok)
  19070. (progn (backward-char 1) (outline-invisible-p)))
  19071. (if (< l level) (setq arg 1)))
  19072. (setq arg (1- arg)))
  19073. (beginning-of-line 1)))
  19074. (defun org-backward-same-level (arg &optional invisible-ok)
  19075. "Move backward to the arg'th subheading at same level as this one.
  19076. Stop at the first and last subheadings of a superior heading."
  19077. (interactive "p")
  19078. (org-back-to-heading)
  19079. (org-on-heading-p)
  19080. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19081. (re (format "^\\*\\{1,%d\\} " level))
  19082. l)
  19083. (while (> arg 0)
  19084. (while (and (re-search-backward re nil 'move)
  19085. (setq l (- (match-end 0) (match-beginning 0) 1))
  19086. (= l level)
  19087. (not invisible-ok)
  19088. (outline-invisible-p))
  19089. (if (< l level) (setq arg 1)))
  19090. (setq arg (1- arg)))))
  19091. (defun org-show-subtree ()
  19092. "Show everything after this heading at deeper levels."
  19093. (outline-flag-region
  19094. (point)
  19095. (save-excursion
  19096. (org-end-of-subtree t t))
  19097. nil))
  19098. (defun org-show-entry ()
  19099. "Show the body directly following this heading.
  19100. Show the heading too, if it is currently invisible."
  19101. (interactive)
  19102. (save-excursion
  19103. (condition-case nil
  19104. (progn
  19105. (org-back-to-heading t)
  19106. (outline-flag-region
  19107. (max (point-min) (1- (point)))
  19108. (save-excursion
  19109. (if (re-search-forward
  19110. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  19111. (match-beginning 1)
  19112. (point-max)))
  19113. nil)
  19114. (org-cycle-hide-drawers 'children))
  19115. (error nil))))
  19116. (defun org-make-options-regexp (kwds &optional extra)
  19117. "Make a regular expression for keyword lines."
  19118. (concat
  19119. "^"
  19120. "#?[ \t]*\\+\\("
  19121. (mapconcat 'regexp-quote kwds "\\|")
  19122. (if extra (concat "\\|" extra))
  19123. "\\):[ \t]*"
  19124. "\\(.*\\)"))
  19125. ;; Make isearch reveal the necessary context
  19126. (defun org-isearch-end ()
  19127. "Reveal context after isearch exits."
  19128. (when isearch-success ; only if search was successful
  19129. (if (featurep 'xemacs)
  19130. ;; Under XEmacs, the hook is run in the correct place,
  19131. ;; we directly show the context.
  19132. (org-show-context 'isearch)
  19133. ;; In Emacs the hook runs *before* restoring the overlays.
  19134. ;; So we have to use a one-time post-command-hook to do this.
  19135. ;; (Emacs 22 has a special variable, see function `org-mode')
  19136. (unless (and (boundp 'isearch-mode-end-hook-quit)
  19137. isearch-mode-end-hook-quit)
  19138. ;; Only when the isearch was not quitted.
  19139. (org-add-hook 'post-command-hook 'org-isearch-post-command
  19140. 'append 'local)))))
  19141. (defun org-isearch-post-command ()
  19142. "Remove self from hook, and show context."
  19143. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  19144. (org-show-context 'isearch))
  19145. ;;;; Integration with and fixes for other packages
  19146. ;;; Imenu support
  19147. (defvar org-imenu-markers nil
  19148. "All markers currently used by Imenu.")
  19149. (make-variable-buffer-local 'org-imenu-markers)
  19150. (defun org-imenu-new-marker (&optional pos)
  19151. "Return a new marker for use by Imenu, and remember the marker."
  19152. (let ((m (make-marker)))
  19153. (move-marker m (or pos (point)))
  19154. (push m org-imenu-markers)
  19155. m))
  19156. (defun org-imenu-get-tree ()
  19157. "Produce the index for Imenu."
  19158. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  19159. (setq org-imenu-markers nil)
  19160. (let* ((n org-imenu-depth)
  19161. (re (concat "^" (org-get-limited-outline-regexp)))
  19162. (subs (make-vector (1+ n) nil))
  19163. (last-level 0)
  19164. m level head)
  19165. (save-excursion
  19166. (save-restriction
  19167. (widen)
  19168. (goto-char (point-max))
  19169. (while (re-search-backward re nil t)
  19170. (setq level (org-reduced-level (funcall outline-level)))
  19171. (when (<= level n)
  19172. (looking-at org-complex-heading-regexp)
  19173. (setq head (org-link-display-format
  19174. (org-match-string-no-properties 4))
  19175. m (org-imenu-new-marker))
  19176. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  19177. (if (>= level last-level)
  19178. (push (cons head m) (aref subs level))
  19179. (push (cons head (aref subs (1+ level))) (aref subs level))
  19180. (loop for i from (1+ level) to n do (aset subs i nil)))
  19181. (setq last-level level)))))
  19182. (aref subs 1)))
  19183. (eval-after-load "imenu"
  19184. '(progn
  19185. (add-hook 'imenu-after-jump-hook
  19186. (lambda ()
  19187. (if (eq major-mode 'org-mode)
  19188. (org-show-context 'org-goto))))))
  19189. (defun org-link-display-format (link)
  19190. "Replace a link with either the description, or the link target
  19191. if no description is present"
  19192. (save-match-data
  19193. (if (string-match org-bracket-link-analytic-regexp link)
  19194. (replace-match (if (match-end 5)
  19195. (match-string 5 link)
  19196. (concat (match-string 1 link)
  19197. (match-string 3 link)))
  19198. nil t link)
  19199. link)))
  19200. (defun org-toggle-link-display ()
  19201. "Toggle the literal or descriptive display of links."
  19202. (interactive)
  19203. (if org-descriptive-links
  19204. (progn (org-remove-from-invisibility-spec '(org-link))
  19205. (org-restart-font-lock)
  19206. (setq org-descriptive-links nil))
  19207. (progn (add-to-invisibility-spec '(org-link))
  19208. (org-restart-font-lock)
  19209. (setq org-descriptive-links t))))
  19210. ;; Speedbar support
  19211. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  19212. "Overlay marking the agenda restriction line in speedbar.")
  19213. (overlay-put org-speedbar-restriction-lock-overlay
  19214. 'face 'org-agenda-restriction-lock)
  19215. (overlay-put org-speedbar-restriction-lock-overlay
  19216. 'help-echo "Agendas are currently limited to this item.")
  19217. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  19218. (defun org-speedbar-set-agenda-restriction ()
  19219. "Restrict future agenda commands to the location at point in speedbar.
  19220. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  19221. (interactive)
  19222. (require 'org-agenda)
  19223. (let (p m tp np dir txt)
  19224. (cond
  19225. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19226. 'org-imenu t))
  19227. (setq m (get-text-property p 'org-imenu-marker))
  19228. (with-current-buffer (marker-buffer m)
  19229. (goto-char m)
  19230. (org-agenda-set-restriction-lock 'subtree)))
  19231. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19232. 'speedbar-function 'speedbar-find-file))
  19233. (setq tp (previous-single-property-change
  19234. (1+ p) 'speedbar-function)
  19235. np (next-single-property-change
  19236. tp 'speedbar-function)
  19237. dir (speedbar-line-directory)
  19238. txt (buffer-substring-no-properties (or tp (point-min))
  19239. (or np (point-max))))
  19240. (with-current-buffer (find-file-noselect
  19241. (let ((default-directory dir))
  19242. (expand-file-name txt)))
  19243. (unless (eq major-mode 'org-mode)
  19244. (error "Cannot restrict to non-Org-mode file"))
  19245. (org-agenda-set-restriction-lock 'file)))
  19246. (t (error "Don't know how to restrict Org-mode's agenda")))
  19247. (move-overlay org-speedbar-restriction-lock-overlay
  19248. (point-at-bol) (point-at-eol))
  19249. (setq current-prefix-arg nil)
  19250. (org-agenda-maybe-redo)))
  19251. (eval-after-load "speedbar"
  19252. '(progn
  19253. (speedbar-add-supported-extension ".org")
  19254. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  19255. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  19256. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  19257. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  19258. (add-hook 'speedbar-visiting-tag-hook
  19259. (lambda () (and (eq major-mode 'org-mode) (org-show-context 'org-goto))))))
  19260. ;;; Fixes and Hacks for problems with other packages
  19261. ;; Make flyspell not check words in links, to not mess up our keymap
  19262. (defun org-mode-flyspell-verify ()
  19263. "Don't let flyspell put overlays at active buttons, or on
  19264. {todo,all-time,additional-option-like}-keywords."
  19265. (let ((pos (max (1- (point)) (point-min)))
  19266. (word (thing-at-point 'word)))
  19267. (and (not (get-text-property pos 'keymap))
  19268. (not (get-text-property pos 'org-no-flyspell))
  19269. (not (member word org-todo-keywords-1))
  19270. (not (member word org-all-time-keywords))
  19271. (not (member word org-additional-option-like-keywords)))))
  19272. (defun org-remove-flyspell-overlays-in (beg end)
  19273. "Remove flyspell overlays in region."
  19274. (and (org-bound-and-true-p flyspell-mode)
  19275. (fboundp 'flyspell-delete-region-overlays)
  19276. (flyspell-delete-region-overlays beg end))
  19277. (add-text-properties beg end '(org-no-flyspell t)))
  19278. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  19279. (eval-after-load "bookmark"
  19280. '(if (boundp 'bookmark-after-jump-hook)
  19281. ;; We can use the hook
  19282. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  19283. ;; Hook not available, use advice
  19284. (defadvice bookmark-jump (after org-make-visible activate)
  19285. "Make the position visible."
  19286. (org-bookmark-jump-unhide))))
  19287. ;; Make sure saveplace shows the location if it was hidden
  19288. (eval-after-load "saveplace"
  19289. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  19290. "Make the position visible."
  19291. (org-bookmark-jump-unhide)))
  19292. ;; Make sure ecb shows the location if it was hidden
  19293. (eval-after-load "ecb"
  19294. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  19295. "Make hierarchy visible when jumping into location from ECB tree buffer."
  19296. (if (eq major-mode 'org-mode)
  19297. (org-show-context))))
  19298. (defun org-bookmark-jump-unhide ()
  19299. "Unhide the current position, to show the bookmark location."
  19300. (and (eq major-mode 'org-mode)
  19301. (or (outline-invisible-p)
  19302. (save-excursion (goto-char (max (point-min) (1- (point))))
  19303. (outline-invisible-p)))
  19304. (org-show-context 'bookmark-jump)))
  19305. ;; Make session.el ignore our circular variable
  19306. (eval-after-load "session"
  19307. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  19308. ;;;; Experimental code
  19309. (defun org-closed-in-range ()
  19310. "Sparse tree of items closed in a certain time range.
  19311. Still experimental, may disappear in the future."
  19312. (interactive)
  19313. ;; Get the time interval from the user.
  19314. (let* ((time1 (org-float-time
  19315. (org-read-date nil 'to-time nil "Starting date: ")))
  19316. (time2 (org-float-time
  19317. (org-read-date nil 'to-time nil "End date:")))
  19318. ;; callback function
  19319. (callback (lambda ()
  19320. (let ((time
  19321. (org-float-time
  19322. (apply 'encode-time
  19323. (org-parse-time-string
  19324. (match-string 1))))))
  19325. ;; check if time in interval
  19326. (and (>= time time1) (<= time time2))))))
  19327. ;; make tree, check each match with the callback
  19328. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  19329. ;;;; Finish up
  19330. (provide 'org)
  19331. (run-hooks 'org-load-hook)
  19332. ;;; org.el ends here