org.el 793 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071
  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
  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"
  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 ":" (org-link-escape
  8610. path org-link-escape-chars-browser))))
  8611. ((string= type "doi")
  8612. (browse-url (concat "http://dx.doi.org/"
  8613. (org-link-escape
  8614. path org-link-escape-chars-browser))))
  8615. ((member type '("message"))
  8616. (browse-url (concat type ":" path)))
  8617. ((string= type "tags")
  8618. (org-tags-view arg path))
  8619. ((string= type "tree-match")
  8620. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8621. ((string= type "file")
  8622. (if (string-match "::\\([0-9]+\\)\\'" path)
  8623. (setq line (string-to-number (match-string 1 path))
  8624. path (substring path 0 (match-beginning 0)))
  8625. (if (string-match "::\\(.+\\)\\'" path)
  8626. (setq search (match-string 1 path)
  8627. path (substring path 0 (match-beginning 0)))))
  8628. (if (string-match "[*?{]" (file-name-nondirectory path))
  8629. (dired path)
  8630. (org-open-file path arg line search)))
  8631. ((string= type "shell")
  8632. (let ((cmd path))
  8633. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8634. (string-match org-confirm-shell-link-not-regexp cmd))
  8635. (not org-confirm-shell-link-function)
  8636. (funcall org-confirm-shell-link-function
  8637. (format "Execute \"%s\" in shell? "
  8638. (org-add-props cmd nil
  8639. 'face 'org-warning))))
  8640. (progn
  8641. (message "Executing %s" cmd)
  8642. (shell-command cmd))
  8643. (error "Abort"))))
  8644. ((string= type "elisp")
  8645. (let ((cmd path))
  8646. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8647. (string-match org-confirm-elisp-link-not-regexp cmd))
  8648. (not org-confirm-elisp-link-function)
  8649. (funcall org-confirm-elisp-link-function
  8650. (format "Execute \"%s\" as elisp? "
  8651. (org-add-props cmd nil
  8652. 'face 'org-warning))))
  8653. (message "%s => %s" cmd
  8654. (if (equal (string-to-char cmd) ?\()
  8655. (eval (read cmd))
  8656. (call-interactively (read cmd))))
  8657. (error "Abort"))))
  8658. ((and (string= type "thisfile")
  8659. (run-hook-with-args-until-success
  8660. 'org-open-link-functions path)))
  8661. ((string= type "thisfile")
  8662. (if arg
  8663. (switch-to-buffer-other-window
  8664. (org-get-buffer-for-internal-link (current-buffer)))
  8665. (org-mark-ring-push))
  8666. (let ((cmd `(org-link-search
  8667. ,path
  8668. ,(cond ((equal arg '(4)) ''occur)
  8669. ((equal arg '(16)) ''org-occur)
  8670. (t nil))
  8671. ,pos)))
  8672. (condition-case nil (let ((org-link-search-inhibit-query t))
  8673. (eval cmd))
  8674. (error (progn (widen) (eval cmd))))))
  8675. (t
  8676. (browse-url-at-point)))))))
  8677. (move-marker org-open-link-marker nil)
  8678. (run-hook-with-args 'org-follow-link-hook)))
  8679. (defun org-offer-links-in-entry (&optional nth zero)
  8680. "Offer links in the current entry and follow the selected link.
  8681. If there is only one link, follow it immediately as well.
  8682. If NTH is an integer, immediately pick the NTH link found.
  8683. If ZERO is a string, check also this string for a link, and if
  8684. there is one, offer it as link number zero."
  8685. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8686. "\\(" org-angle-link-re "\\)\\|"
  8687. "\\(" org-plain-link-re "\\)"))
  8688. (cnt ?0)
  8689. (in-emacs (if (integerp nth) nil nth))
  8690. have-zero end links link c)
  8691. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8692. (push (match-string 0 zero) links)
  8693. (setq cnt (1- cnt) have-zero t))
  8694. (save-excursion
  8695. (org-back-to-heading t)
  8696. (setq end (save-excursion (outline-next-heading) (point)))
  8697. (while (re-search-forward re end t)
  8698. (push (match-string 0) links))
  8699. (setq links (org-uniquify (reverse links))))
  8700. (cond
  8701. ((null links)
  8702. (message "No links"))
  8703. ((equal (length links) 1)
  8704. (setq link (list (car links))))
  8705. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8706. (setq link (list (nth (if have-zero nth (1- nth)) links))))
  8707. (t ; we have to select a link
  8708. (save-excursion
  8709. (save-window-excursion
  8710. (delete-other-windows)
  8711. (with-output-to-temp-buffer "*Select Link*"
  8712. (mapc (lambda (l)
  8713. (if (not (string-match org-bracket-link-regexp l))
  8714. (princ (format "[%c] %s\n" (incf cnt)
  8715. (org-remove-angle-brackets l)))
  8716. (if (match-end 3)
  8717. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8718. (match-string 3 l) (match-string 1 l)))
  8719. (princ (format "[%c] %s\n" (incf cnt)
  8720. (match-string 1 l))))))
  8721. links))
  8722. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8723. (message "Select link to open, RET to open all:")
  8724. (setq c (read-char-exclusive))
  8725. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8726. (when (equal c ?q) (error "Abort"))
  8727. (if (equal c ?\C-m)
  8728. (setq link links)
  8729. (setq nth (- c ?0))
  8730. (if have-zero (setq nth (1+ nth)))
  8731. (unless (and (integerp nth) (>= (length links) nth))
  8732. (error "Invalid link selection"))
  8733. (setq link (list (nth (1- nth) links))))))
  8734. (if link
  8735. (let ((buf (current-buffer)))
  8736. (dolist (l link)
  8737. (org-open-link-from-string l in-emacs buf))
  8738. t)
  8739. nil)))
  8740. ;; Add special file links that specify the way of opening
  8741. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8742. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8743. (defun org-open-file-with-system (path)
  8744. "Open file at PATH using the system way of opening it."
  8745. (org-open-file path 'system))
  8746. (defun org-open-file-with-emacs (path)
  8747. "Open file at PATH in Emacs."
  8748. (org-open-file path 'emacs))
  8749. (defun org-remove-file-link-modifiers ()
  8750. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8751. (goto-char (point-min))
  8752. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8753. (org-if-unprotected
  8754. (replace-match "file:" t t))))
  8755. (eval-after-load "org-exp"
  8756. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8757. 'org-remove-file-link-modifiers))
  8758. ;;;; Time estimates
  8759. (defun org-get-effort (&optional pom)
  8760. "Get the effort estimate for the current entry."
  8761. (org-entry-get pom org-effort-property))
  8762. ;;; File search
  8763. (defvar org-create-file-search-functions nil
  8764. "List of functions to construct the right search string for a file link.
  8765. These functions are called in turn with point at the location to
  8766. which the link should point.
  8767. A function in the hook should first test if it would like to
  8768. handle this file type, for example by checking the `major-mode'
  8769. or the file extension. If it decides not to handle this file, it
  8770. should just return nil to give other functions a chance. If it
  8771. does handle the file, it must return the search string to be used
  8772. when following the link. The search string will be part of the
  8773. file link, given after a double colon, and `org-open-at-point'
  8774. will automatically search for it. If special measures must be
  8775. taken to make the search successful, another function should be
  8776. added to the companion hook `org-execute-file-search-functions',
  8777. which see.
  8778. A function in this hook may also use `setq' to set the variable
  8779. `description' to provide a suggestion for the descriptive text to
  8780. be used for this link when it gets inserted into an Org-mode
  8781. buffer with \\[org-insert-link].")
  8782. (defvar org-execute-file-search-functions nil
  8783. "List of functions to execute a file search triggered by a link.
  8784. Functions added to this hook must accept a single argument, the
  8785. search string that was part of the file link, the part after the
  8786. double colon. The function must first check if it would like to
  8787. handle this search, for example by checking the `major-mode' or
  8788. the file extension. If it decides not to handle this search, it
  8789. should just return nil to give other functions a chance. If it
  8790. does handle the search, it must return a non-nil value to keep
  8791. other functions from trying.
  8792. Each function can access the current prefix argument through the
  8793. variable `current-prefix-argument'. Note that a single prefix is
  8794. used to force opening a link in Emacs, so it may be good to only
  8795. use a numeric or double prefix to guide the search function.
  8796. In case this is needed, a function in this hook can also restore
  8797. the window configuration before `org-open-at-point' was called using:
  8798. (set-window-configuration org-window-config-before-follow-link)")
  8799. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8800. (defun org-link-search (s &optional type avoid-pos stealth)
  8801. "Search for a link search option.
  8802. If S is surrounded by forward slashes, it is interpreted as a
  8803. regular expression. In org-mode files, this will create an `org-occur'
  8804. sparse tree. In ordinary files, `occur' will be used to list matches.
  8805. If the current buffer is in `dired-mode', grep will be used to search
  8806. in all files. If AVOID-POS is given, ignore matches near that position.
  8807. When optional argument STEALTH is non-nil, do not modify
  8808. visibility around point, thus ignoring
  8809. `org-show-hierarchy-above', `org-show-following-heading' and
  8810. `org-show-siblings' variables."
  8811. (let ((case-fold-search t)
  8812. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8813. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8814. (append '(("") (" ") ("\t") ("\n"))
  8815. org-emphasis-alist)
  8816. "\\|") "\\)"))
  8817. (pos (point))
  8818. (pre nil) (post nil)
  8819. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8820. (cond
  8821. ;; First check if there are any special search functions
  8822. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8823. ;; Now try the builtin stuff
  8824. ((and (equal (string-to-char s0) ?#)
  8825. (> (length s0) 1)
  8826. (save-excursion
  8827. (goto-char (point-min))
  8828. (and
  8829. (re-search-forward
  8830. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8831. (setq type 'dedicated
  8832. pos (match-beginning 0))))
  8833. ;; There is an exact target for this
  8834. (goto-char pos)
  8835. (org-back-to-heading t)))
  8836. ((save-excursion
  8837. (goto-char (point-min))
  8838. (and
  8839. (re-search-forward
  8840. (concat "<<" (regexp-quote s0) ">>") nil t)
  8841. (setq type 'dedicated
  8842. pos (match-beginning 0))))
  8843. ;; There is an exact target for this
  8844. (goto-char pos))
  8845. ((and (string-match "^(\\(.*\\))$" s0)
  8846. (save-excursion
  8847. (goto-char (point-min))
  8848. (and
  8849. (re-search-forward
  8850. (concat "[^[]" (regexp-quote
  8851. (format org-coderef-label-format
  8852. (match-string 1 s0))))
  8853. nil t)
  8854. (setq type 'dedicated
  8855. pos (1+ (match-beginning 0))))))
  8856. ;; There is a coderef target for this
  8857. (goto-char pos))
  8858. ((string-match "^/\\(.*\\)/$" s)
  8859. ;; A regular expression
  8860. (cond
  8861. ((eq major-mode 'org-mode)
  8862. (org-occur (match-string 1 s)))
  8863. ;;((eq major-mode 'dired-mode)
  8864. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8865. (t (org-do-occur (match-string 1 s)))))
  8866. ((and (eq major-mode 'org-mode) org-link-search-must-match-exact-headline)
  8867. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8868. (goto-char (point-min))
  8869. (cond
  8870. ((let (case-fold-search)
  8871. (re-search-forward (format org-complex-heading-regexp-format
  8872. (regexp-quote s))
  8873. nil t))
  8874. ;; OK, found a match
  8875. (setq type 'dedicated)
  8876. (goto-char (match-beginning 0)))
  8877. ((and (not org-link-search-inhibit-query)
  8878. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8879. (y-or-n-p "No match - create this as a new heading? "))
  8880. (goto-char (point-max))
  8881. (or (bolp) (newline))
  8882. (insert "* " s "\n")
  8883. (beginning-of-line 0))
  8884. (t
  8885. (goto-char pos)
  8886. (error "No match"))))
  8887. (t
  8888. ;; A normal search string
  8889. (when (equal (string-to-char s) ?*)
  8890. ;; Anchor on headlines, post may include tags.
  8891. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8892. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  8893. s (substring s 1)))
  8894. (remove-text-properties
  8895. 0 (length s)
  8896. '(face nil mouse-face nil keymap nil fontified nil) s)
  8897. ;; Make a series of regular expressions to find a match
  8898. (setq words (org-split-string s "[ \n\r\t]+")
  8899. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8900. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8901. "\\)" markers)
  8902. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8903. re2a (concat "[ \t\r\n]" re2a_)
  8904. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8905. re4 (concat "[^a-zA-Z_]" re4_)
  8906. re1 (concat pre re2 post)
  8907. re3 (concat pre (if pre re4_ re4) post)
  8908. re5 (concat pre ".*" re4)
  8909. re2 (concat pre re2)
  8910. re2a (concat pre (if pre re2a_ re2a))
  8911. re4 (concat pre (if pre re4_ re4))
  8912. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8913. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8914. re5 "\\)"
  8915. ))
  8916. (cond
  8917. ((eq type 'org-occur) (org-occur reall))
  8918. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8919. (t (goto-char (point-min))
  8920. (setq type 'fuzzy)
  8921. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8922. (org-search-not-self 1 re1 nil t)
  8923. (org-search-not-self 1 re2 nil t)
  8924. (org-search-not-self 1 re2a nil t)
  8925. (org-search-not-self 1 re3 nil t)
  8926. (org-search-not-self 1 re4 nil t)
  8927. (org-search-not-self 1 re5 nil t)
  8928. )
  8929. (goto-char (match-beginning 1))
  8930. (goto-char pos)
  8931. (error "No match"))))))
  8932. (and (eq major-mode 'org-mode)
  8933. (not stealth)
  8934. (org-show-context 'link-search))
  8935. type))
  8936. (defun org-search-not-self (group &rest args)
  8937. "Execute `re-search-forward', but only accept matches that do not
  8938. enclose the position of `org-open-link-marker'."
  8939. (let ((m org-open-link-marker))
  8940. (catch 'exit
  8941. (while (apply 're-search-forward args)
  8942. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8943. (goto-char (match-end group))
  8944. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8945. (> (match-beginning 0) (marker-position m))
  8946. (< (match-end 0) (marker-position m)))
  8947. (save-match-data
  8948. (or (not (org-in-regexp
  8949. org-bracket-link-analytic-regexp 1))
  8950. (not (match-end 4)) ; no description
  8951. (and (<= (match-beginning 4) (point))
  8952. (>= (match-end 4) (point))))))
  8953. (throw 'exit (point))))))))
  8954. (defun org-get-buffer-for-internal-link (buffer)
  8955. "Return a buffer to be used for displaying the link target of internal links."
  8956. (cond
  8957. ((not org-display-internal-link-with-indirect-buffer)
  8958. buffer)
  8959. ((string-match "(Clone)$" (buffer-name buffer))
  8960. (message "Buffer is already a clone, not making another one")
  8961. ;; we also do not modify visibility in this case
  8962. buffer)
  8963. (t ; make a new indirect buffer for displaying the link
  8964. (let* ((bn (buffer-name buffer))
  8965. (ibn (concat bn "(Clone)"))
  8966. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8967. (with-current-buffer ib (org-overview))
  8968. ib))))
  8969. (defun org-do-occur (regexp &optional cleanup)
  8970. "Call the Emacs command `occur'.
  8971. If CLEANUP is non-nil, remove the printout of the regular expression
  8972. in the *Occur* buffer. This is useful if the regex is long and not useful
  8973. to read."
  8974. (occur regexp)
  8975. (when cleanup
  8976. (let ((cwin (selected-window)) win beg end)
  8977. (when (setq win (get-buffer-window "*Occur*"))
  8978. (select-window win))
  8979. (goto-char (point-min))
  8980. (when (re-search-forward "match[a-z]+" nil t)
  8981. (setq beg (match-end 0))
  8982. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8983. (setq end (1- (match-beginning 0)))))
  8984. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8985. (goto-char (point-min))
  8986. (select-window cwin))))
  8987. ;;; The mark ring for links jumps
  8988. (defvar org-mark-ring nil
  8989. "Mark ring for positions before jumps in Org-mode.")
  8990. (defvar org-mark-ring-last-goto nil
  8991. "Last position in the mark ring used to go back.")
  8992. ;; Fill and close the ring
  8993. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8994. (loop for i from 1 to org-mark-ring-length do
  8995. (push (make-marker) org-mark-ring))
  8996. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8997. org-mark-ring)
  8998. (defun org-mark-ring-push (&optional pos buffer)
  8999. "Put the current position or POS into the mark ring and rotate it."
  9000. (interactive)
  9001. (setq pos (or pos (point)))
  9002. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9003. (move-marker (car org-mark-ring)
  9004. (or pos (point))
  9005. (or buffer (current-buffer)))
  9006. (message "%s"
  9007. (substitute-command-keys
  9008. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9009. (defun org-mark-ring-goto (&optional n)
  9010. "Jump to the previous position in the mark ring.
  9011. With prefix arg N, jump back that many stored positions. When
  9012. called several times in succession, walk through the entire ring.
  9013. Org-mode commands jumping to a different position in the current file,
  9014. or to another Org-mode file, automatically push the old position
  9015. onto the ring."
  9016. (interactive "p")
  9017. (let (p m)
  9018. (if (eq last-command this-command)
  9019. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9020. (setq p org-mark-ring))
  9021. (setq org-mark-ring-last-goto p)
  9022. (setq m (car p))
  9023. (org-pop-to-buffer-same-window (marker-buffer m))
  9024. (goto-char m)
  9025. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9026. (defun org-remove-angle-brackets (s)
  9027. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9028. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9029. s)
  9030. (defun org-add-angle-brackets (s)
  9031. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9032. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9033. s)
  9034. (defun org-remove-double-quotes (s)
  9035. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9036. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9037. s)
  9038. ;;; Following specific links
  9039. (defun org-follow-timestamp-link ()
  9040. (cond
  9041. ((org-at-date-range-p t)
  9042. (let ((org-agenda-start-on-weekday)
  9043. (t1 (match-string 1))
  9044. (t2 (match-string 2)))
  9045. (setq t1 (time-to-days (org-time-string-to-time t1))
  9046. t2 (time-to-days (org-time-string-to-time t2)))
  9047. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  9048. ((org-at-timestamp-p t)
  9049. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9050. (substring (match-string 1) 0 10)))
  9051. 1))
  9052. (t (error "This should not happen"))))
  9053. ;;; Following file links
  9054. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9055. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9056. (declare-function mailcap-mime-info
  9057. "mailcap" (string &optional request no-decode))
  9058. (defvar org-wait nil)
  9059. (defun org-open-file (path &optional in-emacs line search)
  9060. "Open the file at PATH.
  9061. First, this expands any special file name abbreviations. Then the
  9062. configuration variable `org-file-apps' is checked if it contains an
  9063. entry for this file type, and if yes, the corresponding command is launched.
  9064. If no application is found, Emacs simply visits the file.
  9065. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9066. With a double \\[universal-argument] \\[universal-argument] \
  9067. prefix arg, Org tries to avoid opening in Emacs
  9068. and to use an external application to visit the file.
  9069. Optional LINE specifies a line to go to, optional SEARCH a string
  9070. to search for. If LINE or SEARCH is given, the file will be
  9071. opened in Emacs, unless an entry from org-file-apps that makes
  9072. use of groups in a regexp matches.
  9073. If you want to change the way frames are used when following a
  9074. link, please customize `org-link-frame-setup'.
  9075. If the file does not exist, an error is thrown."
  9076. (let* ((file (if (equal path "")
  9077. buffer-file-name
  9078. (substitute-in-file-name (expand-file-name path))))
  9079. (file-apps (append org-file-apps (org-default-apps)))
  9080. (apps (org-remove-if
  9081. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9082. (apps-dlink (org-remove-if-not
  9083. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9084. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9085. (dirp (if remp nil (file-directory-p file)))
  9086. (file (if (and dirp org-open-directory-means-index-dot-org)
  9087. (concat (file-name-as-directory file) "index.org")
  9088. file))
  9089. (a-m-a-p (assq 'auto-mode apps))
  9090. (dfile (downcase file))
  9091. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9092. (link (cond ((and (eq line nil)
  9093. (eq search nil))
  9094. file)
  9095. (line
  9096. (concat file "::" (number-to-string line)))
  9097. (search
  9098. (concat file "::" search))))
  9099. (dlink (downcase link))
  9100. (old-buffer (current-buffer))
  9101. (old-pos (point))
  9102. (old-mode major-mode)
  9103. ext cmd link-match-data)
  9104. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9105. (setq ext (match-string 1 dfile))
  9106. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9107. (setq ext (match-string 1 dfile))))
  9108. (cond
  9109. ((member in-emacs '((16) system))
  9110. (setq cmd (cdr (assoc 'system apps))))
  9111. (in-emacs (setq cmd 'emacs))
  9112. (t
  9113. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9114. (and dirp (cdr (assoc 'directory apps)))
  9115. ; first, try matching against apps-dlink
  9116. ; if we get a match here, store the match data for later
  9117. (let ((match (assoc-default dlink apps-dlink
  9118. 'string-match)))
  9119. (if match
  9120. (progn (setq link-match-data (match-data))
  9121. match)
  9122. (progn (setq in-emacs (or in-emacs line search))
  9123. nil))) ; if we have no match in apps-dlink,
  9124. ; always open the file in emacs if line or search
  9125. ; is given (for backwards compatibility)
  9126. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9127. 'string-match)
  9128. (cdr (assoc ext apps))
  9129. (cdr (assoc t apps))))))
  9130. (when (eq cmd 'system)
  9131. (setq cmd (cdr (assoc 'system apps))))
  9132. (when (eq cmd 'default)
  9133. (setq cmd (cdr (assoc t apps))))
  9134. (when (eq cmd 'mailcap)
  9135. (require 'mailcap)
  9136. (mailcap-parse-mailcaps)
  9137. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9138. (command (mailcap-mime-info mime-type)))
  9139. (if (stringp command)
  9140. (setq cmd command)
  9141. (setq cmd 'emacs))))
  9142. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9143. (not (file-exists-p file))
  9144. (not org-open-non-existing-files))
  9145. (error "No such file: %s" file))
  9146. (cond
  9147. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9148. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9149. (while (string-match "['\"]%s['\"]" cmd)
  9150. (setq cmd (replace-match "%s" t t cmd)))
  9151. (while (string-match "%s" cmd)
  9152. (setq cmd (replace-match
  9153. (save-match-data
  9154. (shell-quote-argument
  9155. (convert-standard-filename file)))
  9156. t t cmd)))
  9157. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9158. (save-match-data
  9159. (let ((match-index 1)
  9160. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9161. (set-match-data link-match-data)
  9162. (while (<= match-index number-of-groups)
  9163. (let ((regex (concat "%" (number-to-string match-index)))
  9164. (replace-with (match-string match-index dlink)))
  9165. (while (string-match regex cmd)
  9166. (setq cmd (replace-match replace-with t t cmd))))
  9167. (setq match-index (+ match-index 1)))))
  9168. (save-window-excursion
  9169. (start-process-shell-command cmd nil cmd)
  9170. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  9171. ))
  9172. ((or (stringp cmd)
  9173. (eq cmd 'emacs))
  9174. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9175. (widen)
  9176. (if line (org-goto-line line)
  9177. (if search (org-link-search search))))
  9178. ((consp cmd)
  9179. (let ((file (convert-standard-filename file)))
  9180. (save-match-data
  9181. (set-match-data link-match-data)
  9182. (eval cmd))))
  9183. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9184. (and (eq major-mode 'org-mode) (eq old-mode 'org-mode)
  9185. (or (not (equal old-buffer (current-buffer)))
  9186. (not (equal old-pos (point))))
  9187. (org-mark-ring-push old-pos old-buffer))))
  9188. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9189. "This function returns non-nil if `entry' uses a regular
  9190. expression which should be matched against the whole link by
  9191. org-open-file.
  9192. It assumes that is the case when the entry uses a regular
  9193. expression which has at least one grouping construct and the
  9194. action is either a lisp form or a command string containing
  9195. '%1', i.e. using at least one subexpression match as a
  9196. parameter."
  9197. (let ((selector (car entry))
  9198. (action (cdr entry)))
  9199. (if (stringp selector)
  9200. (and (> (regexp-opt-depth selector) 0)
  9201. (or (and (stringp action)
  9202. (string-match "%[0-9]" action))
  9203. (consp action)))
  9204. nil)))
  9205. (defun org-default-apps ()
  9206. "Return the default applications for this operating system."
  9207. (cond
  9208. ((eq system-type 'darwin)
  9209. org-file-apps-defaults-macosx)
  9210. ((eq system-type 'windows-nt)
  9211. org-file-apps-defaults-windowsnt)
  9212. (t org-file-apps-defaults-gnu)))
  9213. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9214. "Convert extensions to regular expressions in the cars of LIST.
  9215. Also, weed out any non-string entries, because the return value is used
  9216. only for regexp matching.
  9217. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9218. point to the symbol `emacs', indicating that the file should
  9219. be opened in Emacs."
  9220. (append
  9221. (delq nil
  9222. (mapcar (lambda (x)
  9223. (if (not (stringp (car x)))
  9224. nil
  9225. (if (string-match "\\W" (car x))
  9226. x
  9227. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9228. list))
  9229. (if add-auto-mode
  9230. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9231. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9232. (defun org-file-remote-p (file)
  9233. "Test whether FILE specifies a location on a remote system.
  9234. Return non-nil if the location is indeed remote.
  9235. For example, the filename \"/user@host:/foo\" specifies a location
  9236. on the system \"/user@host:\"."
  9237. (cond ((fboundp 'file-remote-p)
  9238. (file-remote-p file))
  9239. ((fboundp 'tramp-handle-file-remote-p)
  9240. (tramp-handle-file-remote-p file))
  9241. ((and (boundp 'ange-ftp-name-format)
  9242. (string-match (car ange-ftp-name-format) file))
  9243. t)
  9244. (t nil)))
  9245. ;;;; Refiling
  9246. (defun org-get-org-file ()
  9247. "Read a filename, with default directory `org-directory'."
  9248. (let ((default (or org-default-notes-file remember-data-file)))
  9249. (read-file-name (format "File name [%s]: " default)
  9250. (file-name-as-directory org-directory)
  9251. default)))
  9252. (defun org-notes-order-reversed-p ()
  9253. "Check if the current file should receive notes in reversed order."
  9254. (cond
  9255. ((not org-reverse-note-order) nil)
  9256. ((eq t org-reverse-note-order) t)
  9257. ((not (listp org-reverse-note-order)) nil)
  9258. (t (catch 'exit
  9259. (let ((all org-reverse-note-order)
  9260. entry)
  9261. (while (setq entry (pop all))
  9262. (if (string-match (car entry) buffer-file-name)
  9263. (throw 'exit (cdr entry))))
  9264. nil)))))
  9265. (defvar org-refile-target-table nil
  9266. "The list of refile targets, created by `org-refile'.")
  9267. (defvar org-agenda-new-buffers nil
  9268. "Buffers created to visit agenda files.")
  9269. (defvar org-refile-cache nil
  9270. "Cache for refile targets.")
  9271. (defvar org-refile-markers nil
  9272. "All the markers used for caching refile locations.")
  9273. (defun org-refile-marker (pos)
  9274. "Get a new refile marker, but only if caching is in use."
  9275. (if (not org-refile-use-cache)
  9276. pos
  9277. (let ((m (make-marker)))
  9278. (move-marker m pos)
  9279. (push m org-refile-markers)
  9280. m)))
  9281. (defun org-refile-cache-clear ()
  9282. "Clear the refile cache and disable all the markers."
  9283. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9284. (setq org-refile-markers nil)
  9285. (setq org-refile-cache nil)
  9286. (message "Refile cache has been cleared"))
  9287. (defun org-refile-cache-check-set (set)
  9288. "Check if all the markers in the cache still have live buffers."
  9289. (let (marker)
  9290. (catch 'exit
  9291. (while (and set (setq marker (nth 3 (pop set))))
  9292. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9293. (when (and marker (null (marker-buffer marker)))
  9294. (message "not found") (sit-for 3)
  9295. (throw 'exit nil)))
  9296. t)))
  9297. (defun org-refile-cache-put (set &rest identifiers)
  9298. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9299. (let* ((key (sha1 (prin1-to-string identifiers)))
  9300. (entry (assoc key org-refile-cache)))
  9301. (if entry
  9302. (setcdr entry set)
  9303. (push (cons key set) org-refile-cache))))
  9304. (defun org-refile-cache-get (&rest identifiers)
  9305. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9306. (cond
  9307. ((not org-refile-cache) nil)
  9308. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9309. (t
  9310. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9311. org-refile-cache))))
  9312. (and set (org-refile-cache-check-set set) set)))))
  9313. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9314. "Produce a table with refile targets."
  9315. (let ((case-fold-search nil)
  9316. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9317. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9318. targets tgs txt re files f desc descre fast-path-p level pos0)
  9319. (message "Getting targets...")
  9320. (with-current-buffer (or default-buffer (current-buffer))
  9321. (while (setq entry (pop entries))
  9322. (setq files (car entry) desc (cdr entry))
  9323. (setq fast-path-p nil)
  9324. (cond
  9325. ((null files) (setq files (list (current-buffer))))
  9326. ((eq files 'org-agenda-files)
  9327. (setq files (org-agenda-files 'unrestricted)))
  9328. ((and (symbolp files) (fboundp files))
  9329. (setq files (funcall files)))
  9330. ((and (symbolp files) (boundp files))
  9331. (setq files (symbol-value files))))
  9332. (if (stringp files) (setq files (list files)))
  9333. (cond
  9334. ((eq (car desc) :tag)
  9335. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9336. ((eq (car desc) :todo)
  9337. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9338. ((eq (car desc) :regexp)
  9339. (setq descre (cdr desc)))
  9340. ((eq (car desc) :level)
  9341. (setq descre (concat "^\\*\\{" (number-to-string
  9342. (if org-odd-levels-only
  9343. (1- (* 2 (cdr desc)))
  9344. (cdr desc)))
  9345. "\\}[ \t]")))
  9346. ((eq (car desc) :maxlevel)
  9347. (setq fast-path-p t)
  9348. (setq descre (concat "^\\*\\{1," (number-to-string
  9349. (if org-odd-levels-only
  9350. (1- (* 2 (cdr desc)))
  9351. (cdr desc)))
  9352. "\\}[ \t]")))
  9353. (t (error "Bad refiling target description %s" desc)))
  9354. (while (setq f (pop files))
  9355. (with-current-buffer
  9356. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9357. (or
  9358. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9359. (progn
  9360. (if (bufferp f) (setq f (buffer-file-name
  9361. (buffer-base-buffer f))))
  9362. (setq f (and f (expand-file-name f)))
  9363. (if (eq org-refile-use-outline-path 'file)
  9364. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9365. (save-excursion
  9366. (save-restriction
  9367. (widen)
  9368. (goto-char (point-min))
  9369. (while (re-search-forward descre nil t)
  9370. (goto-char (setq pos0 (point-at-bol)))
  9371. (catch 'next
  9372. (when org-refile-target-verify-function
  9373. (save-match-data
  9374. (or (funcall org-refile-target-verify-function)
  9375. (throw 'next t))))
  9376. (when (and (looking-at org-complex-heading-regexp)
  9377. (not (member (match-string 4) excluded-entries)))
  9378. (setq level (org-reduced-level
  9379. (- (match-end 1) (match-beginning 1)))
  9380. txt (org-link-display-format (match-string 4))
  9381. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9382. re (format org-complex-heading-regexp-format
  9383. (regexp-quote (match-string 4))))
  9384. (when org-refile-use-outline-path
  9385. (setq txt (mapconcat
  9386. 'org-protect-slash
  9387. (append
  9388. (if (eq org-refile-use-outline-path
  9389. 'file)
  9390. (list (file-name-nondirectory
  9391. (buffer-file-name
  9392. (buffer-base-buffer))))
  9393. (if (eq org-refile-use-outline-path
  9394. 'full-file-path)
  9395. (list (buffer-file-name
  9396. (buffer-base-buffer)))))
  9397. (org-get-outline-path fast-path-p
  9398. level txt)
  9399. (list txt))
  9400. "/")))
  9401. (push (list txt f re (org-refile-marker (point)))
  9402. tgs)))
  9403. (when (= (point) pos0)
  9404. ;; verification function has not moved point
  9405. (goto-char (point-at-eol))))))))
  9406. (when org-refile-use-cache
  9407. (org-refile-cache-put tgs (buffer-file-name) descre))
  9408. (setq targets (append tgs targets))
  9409. ))))
  9410. (message "Getting targets...done")
  9411. (nreverse targets)))
  9412. (defun org-protect-slash (s)
  9413. (while (string-match "/" s)
  9414. (setq s (replace-match "\\" t t s)))
  9415. s)
  9416. (defvar org-olpa (make-vector 20 nil))
  9417. (defun org-get-outline-path (&optional fastp level heading)
  9418. "Return the outline path to the current entry, as a list.
  9419. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9420. routine which makes outline path derivations for an entire file,
  9421. avoiding backtracing. Refile target collection makes use of that."
  9422. (if fastp
  9423. (progn
  9424. (if (> level 19)
  9425. (error "Outline path failure, more than 19 levels"))
  9426. (loop for i from level upto 19 do
  9427. (aset org-olpa i nil))
  9428. (prog1
  9429. (delq nil (append org-olpa nil))
  9430. (aset org-olpa level heading)))
  9431. (let (rtn case-fold-search)
  9432. (save-excursion
  9433. (save-restriction
  9434. (widen)
  9435. (while (org-up-heading-safe)
  9436. (when (looking-at org-complex-heading-regexp)
  9437. (push (org-match-string-no-properties 4) rtn)))
  9438. rtn)))))
  9439. (defun org-format-outline-path (path &optional width prefix)
  9440. "Format the outline path PATH for display.
  9441. Width is the maximum number of characters that is available.
  9442. Prefix is a prefix to be included in the returned string,
  9443. such as the file name."
  9444. (setq width (or width 79))
  9445. (if prefix (setq width (- width (length prefix))))
  9446. (if (not path)
  9447. (or prefix "")
  9448. (let* ((nsteps (length path))
  9449. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9450. (maxwidth (if (<= total-width width)
  9451. 10000 ;; everything fits
  9452. ;; we need to shorten the level headings
  9453. (/ (- width nsteps) nsteps)))
  9454. (org-odd-levels-only nil)
  9455. (n 0)
  9456. (total (1+ (length prefix))))
  9457. (setq maxwidth (max maxwidth 10))
  9458. (concat prefix
  9459. (mapconcat
  9460. (lambda (h)
  9461. (setq n (1+ n))
  9462. (if (and (= n nsteps) (< maxwidth 10000))
  9463. (setq maxwidth (- total-width total)))
  9464. (if (< (length h) maxwidth)
  9465. (progn (setq total (+ total (length h) 1)) h)
  9466. (setq h (substring h 0 (- maxwidth 2))
  9467. total (+ total maxwidth 1))
  9468. (if (string-match "[ \t]+\\'" h)
  9469. (setq h (substring h 0 (match-beginning 0))))
  9470. (setq h (concat h "..")))
  9471. (org-add-props h nil 'face
  9472. (nth (% (1- n) org-n-level-faces)
  9473. org-level-faces))
  9474. h)
  9475. path "/")))))
  9476. (defun org-display-outline-path (&optional file current)
  9477. "Display the current outline path in the echo area."
  9478. (interactive "P")
  9479. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9480. (case-fold-search nil)
  9481. (path (and (eq major-mode 'org-mode) (org-get-outline-path))))
  9482. (if current (setq path (append path
  9483. (save-excursion
  9484. (org-back-to-heading t)
  9485. (if (looking-at org-complex-heading-regexp)
  9486. (list (match-string 4)))))))
  9487. (message "%s"
  9488. (org-format-outline-path
  9489. path
  9490. (1- (frame-width))
  9491. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9492. (defvar org-refile-history nil
  9493. "History for refiling operations.")
  9494. (defvar org-after-refile-insert-hook nil
  9495. "Hook run after `org-refile' has inserted its stuff at the new location.
  9496. Note that this is still *before* the stuff will be removed from
  9497. the *old* location.")
  9498. (defvar org-capture-last-stored-marker)
  9499. (defun org-refile (&optional goto default-buffer rfloc)
  9500. "Move the entry or entries at point to another heading.
  9501. The list of target headings is compiled using the information in
  9502. `org-refile-targets', which see.
  9503. At the target location, the entry is filed as a subitem of the target
  9504. heading. Depending on `org-reverse-note-order', the new subitem will
  9505. either be the first or the last subitem.
  9506. If there is an active region, all entries in that region will be moved.
  9507. However, the region must fulfill the requirement that the first heading
  9508. is the first one sets the top-level of the moved text - at most siblings
  9509. below it are allowed.
  9510. With prefix arg GOTO, the command will only visit the target location
  9511. and not actually move anything.
  9512. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9513. go to the location where the last refiling operation has put the subtree.
  9514. With a prefix argument of `2', refile to the running clock.
  9515. RFLOC can be a refile location obtained in a different way.
  9516. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9517. If you are using target caching (see `org-refile-use-cache'),
  9518. You have to clear the target cache in order to find new targets.
  9519. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9520. prefix argument (`C-u C-u C-u C-c C-w')."
  9521. (interactive "P")
  9522. (if (member goto '(0 (64)))
  9523. (org-refile-cache-clear)
  9524. (let* ((cbuf (current-buffer))
  9525. (regionp (org-region-active-p))
  9526. (region-start (and regionp (region-beginning)))
  9527. (region-end (and regionp (region-end)))
  9528. (region-length (and regionp (- region-end region-start)))
  9529. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9530. pos it nbuf file re level reversed)
  9531. (setq last-command nil)
  9532. (when regionp
  9533. (goto-char region-start)
  9534. (or (bolp) (goto-char (point-at-bol)))
  9535. (setq region-start (point))
  9536. (unless (or (org-kill-is-subtree-p
  9537. (buffer-substring region-start region-end))
  9538. (prog1 org-refile-active-region-within-subtree
  9539. (org-toggle-heading)))
  9540. (error "The region is not a (sequence of) subtree(s)")))
  9541. (if (equal goto '(16))
  9542. (org-refile-goto-last-stored)
  9543. (when (or
  9544. (and (equal goto 2)
  9545. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9546. (prog1
  9547. (setq it (list (or org-clock-heading "running clock")
  9548. (buffer-file-name
  9549. (marker-buffer org-clock-hd-marker))
  9550. ""
  9551. (marker-position org-clock-hd-marker)))
  9552. (setq goto nil)))
  9553. (setq it (or rfloc
  9554. (let (heading-text)
  9555. (save-excursion
  9556. (unless goto
  9557. (org-back-to-heading t)
  9558. (setq heading-text
  9559. (nth 4 (org-heading-components))))
  9560. (org-refile-get-location
  9561. (cond (goto "Goto")
  9562. (regionp "Refile region to")
  9563. (t (concat "Refile subtree \""
  9564. heading-text "\" to")))
  9565. default-buffer
  9566. org-refile-allow-creating-parent-nodes
  9567. goto))))))
  9568. (setq file (nth 1 it)
  9569. re (nth 2 it)
  9570. pos (nth 3 it))
  9571. (if (and (not goto)
  9572. pos
  9573. (equal (buffer-file-name) file)
  9574. (if regionp
  9575. (and (>= pos region-start)
  9576. (<= pos region-end))
  9577. (and (>= pos (point))
  9578. (< pos (save-excursion
  9579. (org-end-of-subtree t t))))))
  9580. (error "Cannot refile to position inside the tree or region"))
  9581. (setq nbuf (or (find-buffer-visiting file)
  9582. (find-file-noselect file)))
  9583. (if goto
  9584. (progn
  9585. (org-pop-to-buffer-same-window nbuf)
  9586. (goto-char pos)
  9587. (org-show-context 'org-goto))
  9588. (if regionp
  9589. (progn
  9590. (org-kill-new (buffer-substring region-start region-end))
  9591. (org-save-markers-in-region region-start region-end))
  9592. (org-copy-subtree 1 nil t))
  9593. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9594. (find-file-noselect file)))
  9595. (setq reversed (org-notes-order-reversed-p))
  9596. (save-excursion
  9597. (save-restriction
  9598. (widen)
  9599. (if pos
  9600. (progn
  9601. (goto-char pos)
  9602. (looking-at org-outline-regexp)
  9603. (setq level (org-get-valid-level (funcall outline-level) 1))
  9604. (goto-char
  9605. (if reversed
  9606. (or (outline-next-heading) (point-max))
  9607. (or (save-excursion (org-get-next-sibling))
  9608. (org-end-of-subtree t t)
  9609. (point-max)))))
  9610. (setq level 1)
  9611. (if (not reversed)
  9612. (goto-char (point-max))
  9613. (goto-char (point-min))
  9614. (or (outline-next-heading) (goto-char (point-max)))))
  9615. (if (not (bolp)) (newline))
  9616. (org-paste-subtree level)
  9617. (when org-log-refile
  9618. (org-add-log-setup 'refile nil nil 'findpos
  9619. org-log-refile)
  9620. (unless (eq org-log-refile 'note)
  9621. (save-excursion (org-add-log-note))))
  9622. (and org-auto-align-tags (org-set-tags nil t))
  9623. (bookmark-set "org-refile-last-stored")
  9624. ;; If we are refiling for capture, make sure that the
  9625. ;; last-capture pointers point here
  9626. (when (org-bound-and-true-p org-refile-for-capture)
  9627. (bookmark-set "org-capture-last-stored-marker")
  9628. (move-marker org-capture-last-stored-marker (point)))
  9629. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9630. (run-hooks 'org-after-refile-insert-hook))))
  9631. (if regionp
  9632. (delete-region (point) (+ (point) region-length))
  9633. (org-cut-subtree))
  9634. (when (featurep 'org-inlinetask)
  9635. (org-inlinetask-remove-END-maybe))
  9636. (setq org-markers-to-move nil)
  9637. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9638. (defun org-refile-goto-last-stored ()
  9639. "Go to the location where the last refile was stored."
  9640. (interactive)
  9641. (bookmark-jump "org-refile-last-stored")
  9642. (message "This is the location of the last refile"))
  9643. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9644. no-exclude)
  9645. "Prompt the user for a refile location, using PROMPT.
  9646. PROMPT should not be suffixed with a colon and a space, because
  9647. this function appends the default value from
  9648. `org-refile-history' automatically, if that is not empty.
  9649. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9650. this is used for the GOTO interface."
  9651. (let ((org-refile-targets org-refile-targets)
  9652. (org-refile-use-outline-path org-refile-use-outline-path)
  9653. excluded-entries)
  9654. (when (and (eq major-mode 'org-mode)
  9655. (not org-refile-use-cache)
  9656. (not no-exclude))
  9657. (org-map-tree
  9658. (lambda()
  9659. (setq excluded-entries
  9660. (append excluded-entries (list (org-get-heading t t)))))))
  9661. (setq org-refile-target-table
  9662. (org-refile-get-targets default-buffer excluded-entries)))
  9663. (unless org-refile-target-table
  9664. (error "No refile targets"))
  9665. (let* ((prompt (concat prompt
  9666. (and (car org-refile-history)
  9667. (concat " (default " (car org-refile-history) ")"))
  9668. ": "))
  9669. (cbuf (current-buffer))
  9670. (partial-completion-mode nil)
  9671. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9672. (cfunc (if (and org-refile-use-outline-path
  9673. org-outline-path-complete-in-steps)
  9674. 'org-olpath-completing-read
  9675. 'org-icompleting-read))
  9676. (extra (if org-refile-use-outline-path "/" ""))
  9677. (filename (and cfn (expand-file-name cfn)))
  9678. (tbl (mapcar
  9679. (lambda (x)
  9680. (if (and (not (member org-refile-use-outline-path
  9681. '(file full-file-path)))
  9682. (not (equal filename (nth 1 x))))
  9683. (cons (concat (car x) extra " ("
  9684. (file-name-nondirectory (nth 1 x)) ")")
  9685. (cdr x))
  9686. (cons (concat (car x) extra) (cdr x))))
  9687. org-refile-target-table))
  9688. (completion-ignore-case t)
  9689. pa answ parent-target child parent old-hist)
  9690. (setq old-hist org-refile-history)
  9691. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9692. nil 'org-refile-history (car org-refile-history)))
  9693. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9694. (org-refile-check-position pa)
  9695. (if pa
  9696. (progn
  9697. (when (or (not org-refile-history)
  9698. (not (eq old-hist org-refile-history))
  9699. (not (equal (car pa) (car org-refile-history))))
  9700. (setq org-refile-history
  9701. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9702. org-refile-history
  9703. (cdr org-refile-history))))
  9704. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9705. (pop org-refile-history)))
  9706. pa)
  9707. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9708. (progn
  9709. (setq parent (match-string 1 answ)
  9710. child (match-string 2 answ))
  9711. (setq parent-target (or (assoc parent tbl)
  9712. (assoc (concat parent "/") tbl)))
  9713. (when (and parent-target
  9714. (or (eq new-nodes t)
  9715. (and (eq new-nodes 'confirm)
  9716. (y-or-n-p (format "Create new node \"%s\"? "
  9717. child)))))
  9718. (org-refile-new-child parent-target child)))
  9719. (error "Invalid target location")))))
  9720. (defun org-refile-check-position (refile-pointer)
  9721. "Check if the refile pointer matches the readline to which it points."
  9722. (let* ((file (nth 1 refile-pointer))
  9723. (re (nth 2 refile-pointer))
  9724. (pos (nth 3 refile-pointer))
  9725. buffer)
  9726. (when (org-string-nw-p re)
  9727. (setq buffer (if (markerp pos)
  9728. (marker-buffer pos)
  9729. (or (find-buffer-visiting file)
  9730. (find-file-noselect file))))
  9731. (with-current-buffer buffer
  9732. (save-excursion
  9733. (save-restriction
  9734. (widen)
  9735. (goto-char pos)
  9736. (beginning-of-line 1)
  9737. (unless (org-looking-at-p re)
  9738. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  9739. (defun org-refile-new-child (parent-target child)
  9740. "Use refile target PARENT-TARGET to add new CHILD below it."
  9741. (unless parent-target
  9742. (error "Cannot find parent for new node"))
  9743. (let ((file (nth 1 parent-target))
  9744. (pos (nth 3 parent-target))
  9745. level)
  9746. (with-current-buffer (or (find-buffer-visiting file)
  9747. (find-file-noselect file))
  9748. (save-excursion
  9749. (save-restriction
  9750. (widen)
  9751. (if pos
  9752. (goto-char pos)
  9753. (goto-char (point-max))
  9754. (if (not (bolp)) (newline)))
  9755. (when (looking-at org-outline-regexp)
  9756. (setq level (funcall outline-level))
  9757. (org-end-of-subtree t t))
  9758. (org-back-over-empty-lines)
  9759. (insert "\n" (make-string
  9760. (if pos (org-get-valid-level level 1) 1) ?*)
  9761. " " child "\n")
  9762. (beginning-of-line 0)
  9763. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9764. (defun org-olpath-completing-read (prompt collection &rest args)
  9765. "Read an outline path like a file name."
  9766. (let ((thetable collection)
  9767. (org-completion-use-ido nil) ; does not work with ido.
  9768. (org-completion-use-iswitchb nil)) ; or iswitchb
  9769. (apply
  9770. 'org-icompleting-read prompt
  9771. (lambda (string predicate &optional flag)
  9772. (let (rtn r f (l (length string)))
  9773. (cond
  9774. ((eq flag nil)
  9775. ;; try completion
  9776. (try-completion string thetable))
  9777. ((eq flag t)
  9778. ;; all-completions
  9779. (setq rtn (all-completions string thetable predicate))
  9780. (mapcar
  9781. (lambda (x)
  9782. (setq r (substring x l))
  9783. (if (string-match " ([^)]*)$" x)
  9784. (setq f (match-string 0 x))
  9785. (setq f ""))
  9786. (if (string-match "/" r)
  9787. (concat string (substring r 0 (match-end 0)) f)
  9788. x))
  9789. rtn))
  9790. ((eq flag 'lambda)
  9791. ;; exact match?
  9792. (assoc string thetable)))
  9793. ))
  9794. args)))
  9795. ;;;; Dynamic blocks
  9796. (defun org-find-dblock (name)
  9797. "Find the first dynamic block with name NAME in the buffer.
  9798. If not found, stay at current position and return nil."
  9799. (let (pos)
  9800. (save-excursion
  9801. (goto-char (point-min))
  9802. (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
  9803. nil t)
  9804. (match-beginning 0))))
  9805. (if pos (goto-char pos))
  9806. pos))
  9807. (defconst org-dblock-start-re
  9808. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9809. "Matches the start line of a dynamic block, with parameters.")
  9810. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9811. "Matches the end of a dynamic block.")
  9812. (defun org-create-dblock (plist)
  9813. "Create a dynamic block section, with parameters taken from PLIST.
  9814. PLIST must contain a :name entry which is used as name of the block."
  9815. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9816. (end-of-line 1)
  9817. (newline))
  9818. (let ((col (current-column))
  9819. (name (plist-get plist :name)))
  9820. (insert "#+BEGIN: " name)
  9821. (while plist
  9822. (if (eq (car plist) :name)
  9823. (setq plist (cddr plist))
  9824. (insert " " (prin1-to-string (pop plist)))))
  9825. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9826. (beginning-of-line -2)))
  9827. (defun org-prepare-dblock ()
  9828. "Prepare dynamic block for refresh.
  9829. This empties the block, puts the cursor at the insert position and returns
  9830. the property list including an extra property :name with the block name."
  9831. (unless (looking-at org-dblock-start-re)
  9832. (error "Not at a dynamic block"))
  9833. (let* ((begdel (1+ (match-end 0)))
  9834. (name (org-no-properties (match-string 1)))
  9835. (params (append (list :name name)
  9836. (read (concat "(" (match-string 3) ")")))))
  9837. (save-excursion
  9838. (beginning-of-line 1)
  9839. (skip-chars-forward " \t")
  9840. (setq params (plist-put params :indentation-column (current-column))))
  9841. (unless (re-search-forward org-dblock-end-re nil t)
  9842. (error "Dynamic block not terminated"))
  9843. (setq params
  9844. (append params
  9845. (list :content (buffer-substring
  9846. begdel (match-beginning 0)))))
  9847. (delete-region begdel (match-beginning 0))
  9848. (goto-char begdel)
  9849. (open-line 1)
  9850. params))
  9851. (defun org-map-dblocks (&optional command)
  9852. "Apply COMMAND to all dynamic blocks in the current buffer.
  9853. If COMMAND is not given, use `org-update-dblock'."
  9854. (let ((cmd (or command 'org-update-dblock)))
  9855. (save-excursion
  9856. (goto-char (point-min))
  9857. (while (re-search-forward org-dblock-start-re nil t)
  9858. (goto-char (match-beginning 0))
  9859. (save-excursion
  9860. (condition-case nil
  9861. (funcall cmd)
  9862. (error (message "Error during update of dynamic block"))))
  9863. (unless (re-search-forward org-dblock-end-re nil t)
  9864. (error "Dynamic block not terminated"))))))
  9865. (defun org-dblock-update (&optional arg)
  9866. "User command for updating dynamic blocks.
  9867. Update the dynamic block at point. With prefix ARG, update all dynamic
  9868. blocks in the buffer."
  9869. (interactive "P")
  9870. (if arg
  9871. (org-update-all-dblocks)
  9872. (or (looking-at org-dblock-start-re)
  9873. (org-beginning-of-dblock))
  9874. (org-update-dblock)))
  9875. (defun org-update-dblock ()
  9876. "Update the dynamic block at point.
  9877. This means to empty the block, parse for parameters and then call
  9878. the correct writing function."
  9879. (interactive)
  9880. (save-window-excursion
  9881. (let* ((pos (point))
  9882. (line (org-current-line))
  9883. (params (org-prepare-dblock))
  9884. (name (plist-get params :name))
  9885. (indent (plist-get params :indentation-column))
  9886. (cmd (intern (concat "org-dblock-write:" name))))
  9887. (message "Updating dynamic block `%s' at line %d..." name line)
  9888. (funcall cmd params)
  9889. (message "Updating dynamic block `%s' at line %d...done" name line)
  9890. (goto-char pos)
  9891. (when (and indent (> indent 0))
  9892. (setq indent (make-string indent ?\ ))
  9893. (save-excursion
  9894. (org-beginning-of-dblock)
  9895. (forward-line 1)
  9896. (while (not (looking-at org-dblock-end-re))
  9897. (insert indent)
  9898. (beginning-of-line 2))
  9899. (when (looking-at org-dblock-end-re)
  9900. (and (looking-at "[ \t]+")
  9901. (replace-match ""))
  9902. (insert indent)))))))
  9903. (defun org-beginning-of-dblock ()
  9904. "Find the beginning of the dynamic block at point.
  9905. Error if there is no such block at point."
  9906. (let ((pos (point))
  9907. beg)
  9908. (end-of-line 1)
  9909. (if (and (re-search-backward org-dblock-start-re nil t)
  9910. (setq beg (match-beginning 0))
  9911. (re-search-forward org-dblock-end-re nil t)
  9912. (> (match-end 0) pos))
  9913. (goto-char beg)
  9914. (goto-char pos)
  9915. (error "Not in a dynamic block"))))
  9916. (defun org-update-all-dblocks ()
  9917. "Update all dynamic blocks in the buffer.
  9918. This function can be used in a hook."
  9919. (interactive)
  9920. (when (eq major-mode 'org-mode)
  9921. (org-map-dblocks 'org-update-dblock)))
  9922. ;;;; Completion
  9923. (defconst org-additional-option-like-keywords
  9924. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  9925. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  9926. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9927. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  9928. "BEGIN:" "END:"
  9929. "ORGTBL" "TBLFM:" "TBLNAME:"
  9930. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9931. "BEGIN_QUOTE" "END_QUOTE"
  9932. "BEGIN_VERSE" "END_VERSE"
  9933. "BEGIN_CENTER" "END_CENTER"
  9934. "BEGIN_SRC" "END_SRC"
  9935. "BEGIN_RESULT" "END_RESULT"
  9936. "NAME:" "RESULTS:"
  9937. "HEADER:" "HEADERS:"
  9938. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  9939. "CAPTION:" "LABEL:"
  9940. "SETUPFILE:"
  9941. "INCLUDE:"
  9942. "BIND:"
  9943. "MACRO:"))
  9944. (defcustom org-structure-template-alist
  9945. '(
  9946. ("s" "#+begin_src ?\n\n#+end_src"
  9947. "<src lang=\"?\">\n\n</src>")
  9948. ("e" "#+begin_example\n?\n#+end_example"
  9949. "<example>\n?\n</example>")
  9950. ("q" "#+begin_quote\n?\n#+end_quote"
  9951. "<quote>\n?\n</quote>")
  9952. ("v" "#+begin_verse\n?\n#+end_verse"
  9953. "<verse>\n?\n/verse>")
  9954. ("c" "#+begin_center\n?\n#+end_center"
  9955. "<center>\n?\n/center>")
  9956. ("l" "#+begin_latex\n?\n#+end_latex"
  9957. "<literal style=\"latex\">\n?\n</literal>")
  9958. ("L" "#+latex: "
  9959. "<literal style=\"latex\">?</literal>")
  9960. ("h" "#+begin_html\n?\n#+end_html"
  9961. "<literal style=\"html\">\n?\n</literal>")
  9962. ("H" "#+html: "
  9963. "<literal style=\"html\">?</literal>")
  9964. ("a" "#+begin_ascii\n?\n#+end_ascii")
  9965. ("A" "#+ascii: ")
  9966. ("i" "#+index: ?"
  9967. "#+index: ?")
  9968. ("I" "#+include %file ?"
  9969. "<include file=%file markup=\"?\">")
  9970. )
  9971. "Structure completion elements.
  9972. This is a list of abbreviation keys and values. The value gets inserted
  9973. if you type `<' followed by the key and then press the completion key,
  9974. usually `M-TAB'. %file will be replaced by a file name after prompting
  9975. for the file using completion. The cursor will be placed at the position
  9976. of the `?` in the template.
  9977. There are two templates for each key, the first uses the original Org syntax,
  9978. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  9979. the default when the /org-mtags.el/ module has been loaded. See also the
  9980. variable `org-mtags-prefer-muse-templates'.
  9981. This is an experimental feature, it is undecided if it is going to stay in."
  9982. :group 'org-completion
  9983. :type '(repeat
  9984. (string :tag "Key")
  9985. (string :tag "Template")
  9986. (string :tag "Muse Template")))
  9987. (defun org-try-structure-completion ()
  9988. "Try to complete a structure template before point.
  9989. This looks for strings like \"<e\" on an otherwise empty line and
  9990. expands them."
  9991. (let ((l (buffer-substring (point-at-bol) (point)))
  9992. a)
  9993. (when (and (looking-at "[ \t]*$")
  9994. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  9995. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  9996. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  9997. (match-beginning 1)) a)
  9998. t)))
  9999. (defun org-complete-expand-structure-template (start cell)
  10000. "Expand a structure template."
  10001. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10002. (rpl (nth (if musep 2 1) cell))
  10003. (ind ""))
  10004. (delete-region start (point))
  10005. (when (string-match "\\`#\\+" rpl)
  10006. (cond
  10007. ((bolp))
  10008. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10009. (setq ind (buffer-substring (point-at-bol) (point))))
  10010. (t (newline))))
  10011. (setq start (point))
  10012. (if (string-match "%file" rpl)
  10013. (setq rpl (replace-match
  10014. (concat
  10015. "\""
  10016. (save-match-data
  10017. (abbreviate-file-name (read-file-name "Include file: ")))
  10018. "\"")
  10019. t t rpl)))
  10020. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10021. (concat "\n" ind)))
  10022. (insert rpl)
  10023. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10024. ;;;; TODO, DEADLINE, Comments
  10025. (defun org-toggle-comment ()
  10026. "Change the COMMENT state of an entry."
  10027. (interactive)
  10028. (save-excursion
  10029. (org-back-to-heading)
  10030. (let (case-fold-search)
  10031. (cond
  10032. ((looking-at (format org-heading-keyword-regexp-format
  10033. org-comment-string))
  10034. (goto-char (match-end 1))
  10035. (looking-at (concat " +" org-comment-string))
  10036. (replace-match "" t t)
  10037. (when (eolp) (insert " ")))
  10038. ((looking-at org-outline-regexp)
  10039. (goto-char (match-end 0))
  10040. (insert org-comment-string " "))))))
  10041. (defvar org-last-todo-state-is-todo nil
  10042. "This is non-nil when the last TODO state change led to a TODO state.
  10043. If the last change removed the TODO tag or switched to DONE, then
  10044. this is nil.")
  10045. (defvar org-setting-tags nil) ; dynamically skipped
  10046. (defvar org-todo-setup-filter-hook nil
  10047. "Hook for functions that pre-filter todo specs.
  10048. Each function takes a todo spec and returns either nil or the spec
  10049. transformed into canonical form." )
  10050. (defvar org-todo-get-default-hook nil
  10051. "Hook for functions that get a default item for todo.
  10052. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10053. nil or a string to be used for the todo mark." )
  10054. (defvar org-agenda-headline-snapshot-before-repeat)
  10055. (defun org-current-effective-time ()
  10056. "Return current time adjusted for `org-extend-today-until' variable"
  10057. (let* ((ct (org-current-time))
  10058. (dct (decode-time ct))
  10059. (ct1
  10060. (if (and org-use-effective-time
  10061. (< (nth 2 dct) org-extend-today-until))
  10062. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  10063. ct)))
  10064. ct1))
  10065. (defun org-todo-yesterday (&optional arg)
  10066. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10067. (interactive "P")
  10068. (if (eq major-mode 'org-agenda-mode)
  10069. (apply 'org-agenda-todo-yesterday arg)
  10070. (let* ((hour (third (decode-time
  10071. (org-current-time))))
  10072. (org-extend-today-until (1+ hour)))
  10073. (org-todo arg))))
  10074. (defun org-todo (&optional arg)
  10075. "Change the TODO state of an item.
  10076. The state of an item is given by a keyword at the start of the heading,
  10077. like
  10078. *** TODO Write paper
  10079. *** DONE Call mom
  10080. The different keywords are specified in the variable `org-todo-keywords'.
  10081. By default the available states are \"TODO\" and \"DONE\".
  10082. So for this example: when the item starts with TODO, it is changed to DONE.
  10083. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10084. DONE are present, add TODO at the beginning of the heading.
  10085. With \\[universal-argument] prefix arg, use completion to determine the new \
  10086. state.
  10087. With numeric prefix arg, switch to that state.
  10088. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10089. keywords (nextset).
  10090. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10091. With a numeric prefix arg of 0, inhibit note taking for the change.
  10092. For calling through lisp, arg is also interpreted in the following way:
  10093. 'none -> empty state
  10094. \"\"(empty string) -> switch to empty state
  10095. 'done -> switch to DONE
  10096. 'nextset -> switch to the next set of keywords
  10097. 'previousset -> switch to the previous set of keywords
  10098. \"WAITING\" -> switch to the specified keyword, but only if it
  10099. really is a member of `org-todo-keywords'."
  10100. (interactive "P")
  10101. (if (equal arg '(16)) (setq arg 'nextset))
  10102. (let ((org-blocker-hook org-blocker-hook)
  10103. (case-fold-search nil))
  10104. (when (equal arg '(64))
  10105. (setq arg nil org-blocker-hook nil))
  10106. (when (and org-blocker-hook
  10107. (or org-inhibit-blocking
  10108. (org-entry-get nil "NOBLOCKING")))
  10109. (setq org-blocker-hook nil))
  10110. (save-excursion
  10111. (catch 'exit
  10112. (org-back-to-heading t)
  10113. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10114. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10115. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10116. (let* ((match-data (match-data))
  10117. (startpos (point-at-bol))
  10118. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10119. (org-log-done org-log-done)
  10120. (org-log-repeat org-log-repeat)
  10121. (org-todo-log-states org-todo-log-states)
  10122. (org-inhibit-logging
  10123. (if (equal arg 0)
  10124. (progn (setq arg nil) 'note) org-inhibit-logging))
  10125. (this (match-string 1))
  10126. (hl-pos (match-beginning 0))
  10127. (head (org-get-todo-sequence-head this))
  10128. (ass (assoc head org-todo-kwd-alist))
  10129. (interpret (nth 1 ass))
  10130. (done-word (nth 3 ass))
  10131. (final-done-word (nth 4 ass))
  10132. (last-state (or this ""))
  10133. (completion-ignore-case t)
  10134. (member (member this org-todo-keywords-1))
  10135. (tail (cdr member))
  10136. (state (cond
  10137. ((and org-todo-key-trigger
  10138. (or (and (equal arg '(4))
  10139. (eq org-use-fast-todo-selection 'prefix))
  10140. (and (not arg) org-use-fast-todo-selection
  10141. (not (eq org-use-fast-todo-selection
  10142. 'prefix)))))
  10143. ;; Use fast selection
  10144. (org-fast-todo-selection))
  10145. ((and (equal arg '(4))
  10146. (or (not org-use-fast-todo-selection)
  10147. (not org-todo-key-trigger)))
  10148. ;; Read a state with completion
  10149. (org-icompleting-read
  10150. "State: " (mapcar (lambda(x) (list x))
  10151. org-todo-keywords-1)
  10152. nil t))
  10153. ((eq arg 'right)
  10154. (if this
  10155. (if tail (car tail) nil)
  10156. (car org-todo-keywords-1)))
  10157. ((eq arg 'left)
  10158. (if (equal member org-todo-keywords-1)
  10159. nil
  10160. (if this
  10161. (nth (- (length org-todo-keywords-1)
  10162. (length tail) 2)
  10163. org-todo-keywords-1)
  10164. (org-last org-todo-keywords-1))))
  10165. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10166. (setq arg nil))) ; hack to fall back to cycling
  10167. (arg
  10168. ;; user or caller requests a specific state
  10169. (cond
  10170. ((equal arg "") nil)
  10171. ((eq arg 'none) nil)
  10172. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10173. ((eq arg 'nextset)
  10174. (or (car (cdr (member head org-todo-heads)))
  10175. (car org-todo-heads)))
  10176. ((eq arg 'previousset)
  10177. (let ((org-todo-heads (reverse org-todo-heads)))
  10178. (or (car (cdr (member head org-todo-heads)))
  10179. (car org-todo-heads))))
  10180. ((car (member arg org-todo-keywords-1)))
  10181. ((stringp arg)
  10182. (error "State `%s' not valid in this file" arg))
  10183. ((nth (1- (prefix-numeric-value arg))
  10184. org-todo-keywords-1))))
  10185. ((null member) (or head (car org-todo-keywords-1)))
  10186. ((equal this final-done-word) nil) ;; -> make empty
  10187. ((null tail) nil) ;; -> first entry
  10188. ((memq interpret '(type priority))
  10189. (if (eq this-command last-command)
  10190. (car tail)
  10191. (if (> (length tail) 0)
  10192. (or done-word (car org-done-keywords))
  10193. nil)))
  10194. (t
  10195. (car tail))))
  10196. (state (or
  10197. (run-hook-with-args-until-success
  10198. 'org-todo-get-default-hook state last-state)
  10199. state))
  10200. (next (if state (concat " " state " ") " "))
  10201. (change-plist (list :type 'todo-state-change :from this :to state
  10202. :position startpos))
  10203. dolog now-done-p)
  10204. (when org-blocker-hook
  10205. (setq org-last-todo-state-is-todo
  10206. (not (member this org-done-keywords)))
  10207. (unless (save-excursion
  10208. (save-match-data
  10209. (org-with-wide-buffer
  10210. (run-hook-with-args-until-failure
  10211. 'org-blocker-hook change-plist))))
  10212. (if (org-called-interactively-p 'interactive)
  10213. (error "TODO state change from %s to %s blocked" this state)
  10214. ;; fail silently
  10215. (message "TODO state change from %s to %s blocked" this state)
  10216. (throw 'exit nil))))
  10217. (store-match-data match-data)
  10218. (replace-match next t t)
  10219. (unless (pos-visible-in-window-p hl-pos)
  10220. (message "TODO state changed to %s" (org-trim next)))
  10221. (unless head
  10222. (setq head (org-get-todo-sequence-head state)
  10223. ass (assoc head org-todo-kwd-alist)
  10224. interpret (nth 1 ass)
  10225. done-word (nth 3 ass)
  10226. final-done-word (nth 4 ass)))
  10227. (when (memq arg '(nextset previousset))
  10228. (message "Keyword-Set %d/%d: %s"
  10229. (- (length org-todo-sets) -1
  10230. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  10231. (length org-todo-sets)
  10232. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  10233. (setq org-last-todo-state-is-todo
  10234. (not (member state org-done-keywords)))
  10235. (setq now-done-p (and (member state org-done-keywords)
  10236. (not (member this org-done-keywords))))
  10237. (and logging (org-local-logging logging))
  10238. (when (and (or org-todo-log-states org-log-done)
  10239. (not (eq org-inhibit-logging t))
  10240. (not (memq arg '(nextset previousset))))
  10241. ;; we need to look at recording a time and note
  10242. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  10243. (nth 2 (assoc this org-todo-log-states))))
  10244. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10245. (setq dolog 'time))
  10246. (when (and state
  10247. (member state org-not-done-keywords)
  10248. (not (member this org-not-done-keywords)))
  10249. ;; This is now a todo state and was not one before
  10250. ;; If there was a CLOSED time stamp, get rid of it.
  10251. (org-add-planning-info nil nil 'closed))
  10252. (when (and now-done-p org-log-done)
  10253. ;; It is now done, and it was not done before
  10254. (org-add-planning-info 'closed (org-current-effective-time))
  10255. (if (and (not dolog) (eq 'note org-log-done))
  10256. (org-add-log-setup 'done state this 'findpos 'note)))
  10257. (when (and state dolog)
  10258. ;; This is a non-nil state, and we need to log it
  10259. (org-add-log-setup 'state state this 'findpos dolog)))
  10260. ;; Fixup tag positioning
  10261. (org-todo-trigger-tag-changes state)
  10262. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10263. (when org-provide-todo-statistics
  10264. (org-update-parent-todo-statistics))
  10265. (run-hooks 'org-after-todo-state-change-hook)
  10266. (if (and arg (not (member state org-done-keywords)))
  10267. (setq head (org-get-todo-sequence-head state)))
  10268. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10269. ;; Do we need to trigger a repeat?
  10270. (when now-done-p
  10271. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10272. ;; This is for the agenda, take a snapshot of the headline.
  10273. (save-match-data
  10274. (setq org-agenda-headline-snapshot-before-repeat
  10275. (org-get-heading))))
  10276. (org-auto-repeat-maybe state))
  10277. ;; Fixup cursor location if close to the keyword
  10278. (if (and (outline-on-heading-p)
  10279. (not (bolp))
  10280. (save-excursion (beginning-of-line 1)
  10281. (looking-at org-todo-line-regexp))
  10282. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10283. (progn
  10284. (goto-char (or (match-end 2) (match-end 1)))
  10285. (and (looking-at " ") (just-one-space))))
  10286. (when org-trigger-hook
  10287. (save-excursion
  10288. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  10289. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10290. "Block turning an entry into a TODO, using the hierarchy.
  10291. This checks whether the current task should be blocked from state
  10292. changes. Such blocking occurs when:
  10293. 1. The task has children which are not all in a completed state.
  10294. 2. A task has a parent with the property :ORDERED:, and there
  10295. are siblings prior to the current task with incomplete
  10296. status.
  10297. 3. The parent of the task is blocked because it has siblings that should
  10298. be done first, or is child of a block grandparent TODO entry."
  10299. (if (not org-enforce-todo-dependencies)
  10300. t ; if locally turned off don't block
  10301. (catch 'dont-block
  10302. ;; If this is not a todo state change, or if this entry is already DONE,
  10303. ;; do not block
  10304. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10305. (member (plist-get change-plist :from)
  10306. (cons 'done org-done-keywords))
  10307. (member (plist-get change-plist :to)
  10308. (cons 'todo org-not-done-keywords))
  10309. (not (plist-get change-plist :to)))
  10310. (throw 'dont-block t))
  10311. ;; If this task has children, and any are undone, it's blocked
  10312. (save-excursion
  10313. (org-back-to-heading t)
  10314. (let ((this-level (funcall outline-level)))
  10315. (outline-next-heading)
  10316. (let ((child-level (funcall outline-level)))
  10317. (while (and (not (eobp))
  10318. (> child-level this-level))
  10319. ;; this todo has children, check whether they are all
  10320. ;; completed
  10321. (if (and (not (org-entry-is-done-p))
  10322. (org-entry-is-todo-p))
  10323. (throw 'dont-block nil))
  10324. (outline-next-heading)
  10325. (setq child-level (funcall outline-level))))))
  10326. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10327. ;; any previous siblings are undone, it's blocked
  10328. (save-excursion
  10329. (org-back-to-heading t)
  10330. (let* ((pos (point))
  10331. (parent-pos (and (org-up-heading-safe) (point))))
  10332. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10333. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10334. (forward-line 1)
  10335. (re-search-forward org-not-done-heading-regexp pos t))
  10336. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10337. ;; Search further up the hierarchy, to see if an anchestor is blocked
  10338. (while t
  10339. (goto-char parent-pos)
  10340. (if (not (looking-at org-not-done-heading-regexp))
  10341. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10342. (setq pos (point))
  10343. (setq parent-pos (and (org-up-heading-safe) (point)))
  10344. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10345. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10346. (forward-line 1)
  10347. (re-search-forward org-not-done-heading-regexp pos t))
  10348. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10349. (defcustom org-track-ordered-property-with-tag nil
  10350. "Should the ORDERED property also be shown as a tag?
  10351. The ORDERED property decides if an entry should require subtasks to be
  10352. completed in sequence. Since a property is not very visible, setting
  10353. this option means that toggling the ORDERED property with the command
  10354. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10355. not relevant for the behavior, but it makes things more visible.
  10356. Note that toggling the tag with tags commands will not change the property
  10357. and therefore not influence behavior!
  10358. This can be t, meaning the tag ORDERED should be used, It can also be a
  10359. string to select a different tag for this task."
  10360. :group 'org-todo
  10361. :type '(choice
  10362. (const :tag "No tracking" nil)
  10363. (const :tag "Track with ORDERED tag" t)
  10364. (string :tag "Use other tag")))
  10365. (defun org-toggle-ordered-property ()
  10366. "Toggle the ORDERED property of the current entry.
  10367. For better visibility, you can track the value of this property with a tag.
  10368. See variable `org-track-ordered-property-with-tag'."
  10369. (interactive)
  10370. (let* ((t1 org-track-ordered-property-with-tag)
  10371. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10372. (save-excursion
  10373. (org-back-to-heading)
  10374. (if (org-entry-get nil "ORDERED")
  10375. (progn
  10376. (org-delete-property "ORDERED")
  10377. (and tag (org-toggle-tag tag 'off))
  10378. (message "Subtasks can be completed in arbitrary order"))
  10379. (org-entry-put nil "ORDERED" "t")
  10380. (and tag (org-toggle-tag tag 'on))
  10381. (message "Subtasks must be completed in sequence")))))
  10382. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10383. (defun org-block-todo-from-checkboxes (change-plist)
  10384. "Block turning an entry into a TODO, using checkboxes.
  10385. This checks whether the current task should be blocked from state
  10386. changes because there are unchecked boxes in this entry."
  10387. (if (not org-enforce-todo-checkbox-dependencies)
  10388. t ; if locally turned off don't block
  10389. (catch 'dont-block
  10390. ;; If this is not a todo state change, or if this entry is already DONE,
  10391. ;; do not block
  10392. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10393. (member (plist-get change-plist :from)
  10394. (cons 'done org-done-keywords))
  10395. (member (plist-get change-plist :to)
  10396. (cons 'todo org-not-done-keywords))
  10397. (not (plist-get change-plist :to)))
  10398. (throw 'dont-block t))
  10399. ;; If this task has checkboxes that are not checked, it's blocked
  10400. (save-excursion
  10401. (org-back-to-heading t)
  10402. (let ((beg (point)) end)
  10403. (outline-next-heading)
  10404. (setq end (point))
  10405. (goto-char beg)
  10406. (if (org-list-search-forward
  10407. (concat (org-item-beginning-re)
  10408. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10409. "\\[[- ]\\]")
  10410. end t)
  10411. (progn
  10412. (if (boundp 'org-blocked-by-checkboxes)
  10413. (setq org-blocked-by-checkboxes t))
  10414. (throw 'dont-block nil)))))
  10415. t))) ; do not block
  10416. (defun org-entry-blocked-p ()
  10417. "Is the current entry blocked?"
  10418. (if (org-entry-get nil "NOBLOCKING")
  10419. nil ;; Never block this entry
  10420. (not
  10421. (run-hook-with-args-until-failure
  10422. 'org-blocker-hook
  10423. (list :type 'todo-state-change
  10424. :position (point)
  10425. :from 'todo
  10426. :to 'done)))))
  10427. (defun org-update-statistics-cookies (all)
  10428. "Update the statistics cookie, either from TODO or from checkboxes.
  10429. This should be called with the cursor in a line with a statistics cookie."
  10430. (interactive "P")
  10431. (if all
  10432. (progn
  10433. (org-update-checkbox-count 'all)
  10434. (org-map-entries 'org-update-parent-todo-statistics))
  10435. (if (not (org-on-heading-p))
  10436. (org-update-checkbox-count)
  10437. (let ((pos (move-marker (make-marker) (point)))
  10438. end l1 l2)
  10439. (ignore-errors (org-back-to-heading t))
  10440. (if (not (org-on-heading-p))
  10441. (org-update-checkbox-count)
  10442. (setq l1 (org-outline-level))
  10443. (setq end (save-excursion
  10444. (outline-next-heading)
  10445. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  10446. (point)))
  10447. (if (and (save-excursion
  10448. (re-search-forward
  10449. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10450. (not (save-excursion (re-search-forward
  10451. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10452. (org-update-checkbox-count)
  10453. (if (and l2 (> l2 l1))
  10454. (progn
  10455. (goto-char end)
  10456. (org-update-parent-todo-statistics))
  10457. (goto-char pos)
  10458. (beginning-of-line 1)
  10459. (while (re-search-forward
  10460. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10461. (point-at-eol) t)
  10462. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10463. (goto-char pos)
  10464. (move-marker pos nil)))))
  10465. (defvar org-entry-property-inherited-from) ;; defined below
  10466. (defun org-update-parent-todo-statistics ()
  10467. "Update any statistics cookie in the parent of the current headline.
  10468. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10469. the entire subtree and this will travel up the hierarchy and update
  10470. statistics everywhere."
  10471. (let* ((prop (save-excursion (org-up-heading-safe)
  10472. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10473. (recursive (or (not org-hierarchical-todo-statistics)
  10474. (and prop (string-match "\\<recursive\\>" prop))))
  10475. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10476. 0))
  10477. (first t)
  10478. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10479. level ltoggle l1 new ndel
  10480. (cnt-all 0) (cnt-done 0) is-percent kwd
  10481. checkbox-beg ov ovs ove cookie-present)
  10482. (catch 'exit
  10483. (save-excursion
  10484. (beginning-of-line 1)
  10485. (setq ltoggle (funcall outline-level))
  10486. ;; Three situations are to consider:
  10487. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10488. ;; to the top-level ancestor on the headline;
  10489. ;; 2. If parent has "recursive" property, repeat up to the
  10490. ;; headline setting that property, taking inheritance into
  10491. ;; account;
  10492. ;; 3. Else, move up to direct parent and proceed only once.
  10493. (while (and (setq level (org-up-heading-safe))
  10494. (or recursive first)
  10495. (>= (point) lim))
  10496. (setq first nil cookie-present nil)
  10497. (unless (and level
  10498. (not (string-match
  10499. "\\<checkbox\\>"
  10500. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10501. "")))))
  10502. (throw 'exit nil))
  10503. (while (re-search-forward box-re (point-at-eol) t)
  10504. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10505. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10506. (save-match-data
  10507. (unless (outline-next-heading) (throw 'exit nil))
  10508. (while (and (looking-at org-complex-heading-regexp)
  10509. (> (setq l1 (length (match-string 1))) level))
  10510. (setq kwd (and (or recursive (= l1 ltoggle))
  10511. (match-string 2)))
  10512. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10513. (and (listp org-provide-todo-statistics)
  10514. (or (member kwd org-provide-todo-statistics)
  10515. (member kwd org-done-keywords))))
  10516. (setq cnt-all (1+ cnt-all))
  10517. (if (eq org-provide-todo-statistics t)
  10518. (and kwd (setq cnt-all (1+ cnt-all)))))
  10519. (and (member kwd org-done-keywords)
  10520. (setq cnt-done (1+ cnt-done)))
  10521. (outline-next-heading)))
  10522. (setq new
  10523. (if is-percent
  10524. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10525. (format "[%d/%d]" cnt-done cnt-all))
  10526. ndel (- (match-end 0) checkbox-beg))
  10527. ;; handle overlays when updating cookie from column view
  10528. (when (setq ov (car (overlays-at checkbox-beg)))
  10529. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10530. (delete-overlay ov))
  10531. (goto-char checkbox-beg)
  10532. (insert new)
  10533. (delete-region (point) (+ (point) ndel))
  10534. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10535. (when ov (move-overlay ov ovs ove)))
  10536. (when cookie-present
  10537. (run-hook-with-args 'org-after-todo-statistics-hook
  10538. cnt-done (- cnt-all cnt-done))))))
  10539. (run-hooks 'org-todo-statistics-hook)))
  10540. (defvar org-after-todo-statistics-hook nil
  10541. "Hook that is called after a TODO statistics cookie has been updated.
  10542. Each function is called with two arguments: the number of not-done entries
  10543. and the number of done entries.
  10544. For example, the following function, when added to this hook, will switch
  10545. an entry to DONE when all children are done, and back to TODO when new
  10546. entries are set to a TODO status. Note that this hook is only called
  10547. when there is a statistics cookie in the headline!
  10548. (defun org-summary-todo (n-done n-not-done)
  10549. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10550. (let (org-log-done org-log-states) ; turn off logging
  10551. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10552. ")
  10553. (defvar org-todo-statistics-hook nil
  10554. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10555. This hook runs even if there is no statistics cookie present, in which case
  10556. `org-after-todo-statistics-hook' would not run.")
  10557. (defun org-todo-trigger-tag-changes (state)
  10558. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10559. (let ((l org-todo-state-tags-triggers)
  10560. changes)
  10561. (when (or (not state) (equal state ""))
  10562. (setq changes (append changes (cdr (assoc "" l)))))
  10563. (when (and (stringp state) (> (length state) 0))
  10564. (setq changes (append changes (cdr (assoc state l)))))
  10565. (when (member state org-not-done-keywords)
  10566. (setq changes (append changes (cdr (assoc 'todo l)))))
  10567. (when (member state org-done-keywords)
  10568. (setq changes (append changes (cdr (assoc 'done l)))))
  10569. (dolist (c changes)
  10570. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10571. (defun org-local-logging (value)
  10572. "Get logging settings from a property VALUE."
  10573. (let* (words w a)
  10574. ;; directly set the variables, they are already local.
  10575. (setq org-log-done nil
  10576. org-log-repeat nil
  10577. org-todo-log-states nil)
  10578. (setq words (org-split-string value))
  10579. (while (setq w (pop words))
  10580. (cond
  10581. ((setq a (assoc w org-startup-options))
  10582. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10583. (set (nth 1 a) (nth 2 a))))
  10584. ((setq a (org-extract-log-state-settings w))
  10585. (and (member (car a) org-todo-keywords-1)
  10586. (push a org-todo-log-states)))))))
  10587. (defun org-get-todo-sequence-head (kwd)
  10588. "Return the head of the TODO sequence to which KWD belongs.
  10589. If KWD is not set, check if there is a text property remembering the
  10590. right sequence."
  10591. (let (p)
  10592. (cond
  10593. ((not kwd)
  10594. (or (get-text-property (point-at-bol) 'org-todo-head)
  10595. (progn
  10596. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10597. nil (point-at-eol)))
  10598. (get-text-property p 'org-todo-head))))
  10599. ((not (member kwd org-todo-keywords-1))
  10600. (car org-todo-keywords-1))
  10601. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10602. (defun org-fast-todo-selection ()
  10603. "Fast TODO keyword selection with single keys.
  10604. Returns the new TODO keyword, or nil if no state change should occur."
  10605. (let* ((fulltable org-todo-key-alist)
  10606. (done-keywords org-done-keywords) ;; needed for the faces.
  10607. (maxlen (apply 'max (mapcar
  10608. (lambda (x)
  10609. (if (stringp (car x)) (string-width (car x)) 0))
  10610. fulltable)))
  10611. (expert nil)
  10612. (fwidth (+ maxlen 3 1 3))
  10613. (ncol (/ (- (window-width) 4) fwidth))
  10614. tg cnt e c tbl
  10615. groups ingroup)
  10616. (save-excursion
  10617. (save-window-excursion
  10618. (if expert
  10619. (set-buffer (get-buffer-create " *Org todo*"))
  10620. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10621. (erase-buffer)
  10622. (org-set-local 'org-done-keywords done-keywords)
  10623. (setq tbl fulltable cnt 0)
  10624. (while (setq e (pop tbl))
  10625. (cond
  10626. ((equal e '(:startgroup))
  10627. (push '() groups) (setq ingroup t)
  10628. (when (not (= cnt 0))
  10629. (setq cnt 0)
  10630. (insert "\n"))
  10631. (insert "{ "))
  10632. ((equal e '(:endgroup))
  10633. (setq ingroup nil cnt 0)
  10634. (insert "}\n"))
  10635. ((equal e '(:newline))
  10636. (when (not (= cnt 0))
  10637. (setq cnt 0)
  10638. (insert "\n")
  10639. (setq e (car tbl))
  10640. (while (equal (car tbl) '(:newline))
  10641. (insert "\n")
  10642. (setq tbl (cdr tbl)))))
  10643. (t
  10644. (setq tg (car e) c (cdr e))
  10645. (if ingroup (push tg (car groups)))
  10646. (setq tg (org-add-props tg nil 'face
  10647. (org-get-todo-face tg)))
  10648. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10649. (insert "[" c "] " tg (make-string
  10650. (- fwidth 4 (length tg)) ?\ ))
  10651. (when (= (setq cnt (1+ cnt)) ncol)
  10652. (insert "\n")
  10653. (if ingroup (insert " "))
  10654. (setq cnt 0)))))
  10655. (insert "\n")
  10656. (goto-char (point-min))
  10657. (if (not expert) (org-fit-window-to-buffer))
  10658. (message "[a-z..]:Set [SPC]:clear")
  10659. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10660. (cond
  10661. ((or (= c ?\C-g)
  10662. (and (= c ?q) (not (rassoc c fulltable))))
  10663. (setq quit-flag t))
  10664. ((= c ?\ ) nil)
  10665. ((setq e (rassoc c fulltable) tg (car e))
  10666. tg)
  10667. (t (setq quit-flag t)))))))
  10668. (defun org-entry-is-todo-p ()
  10669. (member (org-get-todo-state) org-not-done-keywords))
  10670. (defun org-entry-is-done-p ()
  10671. (member (org-get-todo-state) org-done-keywords))
  10672. (defun org-get-todo-state ()
  10673. (save-excursion
  10674. (org-back-to-heading t)
  10675. (and (looking-at org-todo-line-regexp)
  10676. (match-end 2)
  10677. (match-string 2))))
  10678. (defun org-at-date-range-p (&optional inactive-ok)
  10679. "Is the cursor inside a date range?"
  10680. (interactive)
  10681. (save-excursion
  10682. (catch 'exit
  10683. (let ((pos (point)))
  10684. (skip-chars-backward "^[<\r\n")
  10685. (skip-chars-backward "<[")
  10686. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10687. (>= (match-end 0) pos)
  10688. (throw 'exit t))
  10689. (skip-chars-backward "^<[\r\n")
  10690. (skip-chars-backward "<[")
  10691. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10692. (>= (match-end 0) pos)
  10693. (throw 'exit t)))
  10694. nil)))
  10695. (defun org-get-repeat (&optional tagline)
  10696. "Check if there is a deadline/schedule with repeater in this entry."
  10697. (save-match-data
  10698. (save-excursion
  10699. (org-back-to-heading t)
  10700. (and (re-search-forward (if tagline
  10701. (concat tagline "\\s-*" org-repeat-re)
  10702. org-repeat-re)
  10703. (org-entry-end-position) t)
  10704. (match-string-no-properties 1)))))
  10705. (defvar org-last-changed-timestamp)
  10706. (defvar org-last-inserted-timestamp)
  10707. (defvar org-log-post-message)
  10708. (defvar org-log-note-purpose)
  10709. (defvar org-log-note-how)
  10710. (defvar org-log-note-extra)
  10711. (defun org-auto-repeat-maybe (done-word)
  10712. "Check if the current headline contains a repeated deadline/schedule.
  10713. If yes, set TODO state back to what it was and change the base date
  10714. of repeating deadline/scheduled time stamps to new date.
  10715. This function is run automatically after each state change to a DONE state."
  10716. ;; last-state is dynamically scoped into this function
  10717. (let* ((repeat (org-get-repeat))
  10718. (aa (assoc last-state org-todo-kwd-alist))
  10719. (interpret (nth 1 aa))
  10720. (head (nth 2 aa))
  10721. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10722. (msg "Entry repeats: ")
  10723. (org-log-done nil)
  10724. (org-todo-log-states nil)
  10725. re type n what ts time to-state)
  10726. (when repeat
  10727. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10728. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10729. org-todo-repeat-to-state))
  10730. (unless (and to-state (member to-state org-todo-keywords-1))
  10731. (setq to-state (if (eq interpret 'type) last-state head)))
  10732. (org-todo to-state)
  10733. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10734. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10735. (org-time-stamp-format t t))))
  10736. (when org-log-repeat
  10737. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10738. (memq 'org-add-log-note post-command-hook))
  10739. ;; OK, we are already setup for some record
  10740. (if (eq org-log-repeat 'note)
  10741. ;; make sure we take a note, not only a time stamp
  10742. (setq org-log-note-how 'note))
  10743. ;; Set up for taking a record
  10744. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10745. last-state
  10746. 'findpos org-log-repeat)))
  10747. (org-back-to-heading t)
  10748. (org-add-planning-info nil nil 'closed)
  10749. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10750. org-deadline-time-regexp "\\)\\|\\("
  10751. org-ts-regexp "\\)"))
  10752. (while (re-search-forward
  10753. re (save-excursion (outline-next-heading) (point)) t)
  10754. (setq type (if (match-end 1) org-scheduled-string
  10755. (if (match-end 3) org-deadline-string "Plain:"))
  10756. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10757. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10758. (setq n (string-to-number (match-string 2 ts))
  10759. what (match-string 3 ts))
  10760. (if (equal what "w") (setq n (* n 7) what "d"))
  10761. ;; Preparation, see if we need to modify the start date for the change
  10762. (when (match-end 1)
  10763. (setq time (save-match-data (org-time-string-to-time ts)))
  10764. (cond
  10765. ((equal (match-string 1 ts) ".")
  10766. ;; Shift starting date to today
  10767. (org-timestamp-change
  10768. (- (org-today) (time-to-days time))
  10769. 'day))
  10770. ((equal (match-string 1 ts) "+")
  10771. (let ((nshiftmax 10) (nshift 0))
  10772. (while (or (= nshift 0)
  10773. (<= (time-to-days time)
  10774. (time-to-days (current-time))))
  10775. (when (= (incf nshift) nshiftmax)
  10776. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10777. (error "Abort")))
  10778. (org-timestamp-change n (cdr (assoc what whata)))
  10779. (org-at-timestamp-p t)
  10780. (setq ts (match-string 1))
  10781. (setq time (save-match-data (org-time-string-to-time ts)))))
  10782. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10783. ;; rematch, so that we have everything in place for the real shift
  10784. (org-at-timestamp-p t)
  10785. (setq ts (match-string 1))
  10786. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10787. (org-timestamp-change n (cdr (assoc what whata)))
  10788. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10789. (setq org-log-post-message msg)
  10790. (message "%s" msg))))
  10791. (defun org-show-todo-tree (arg)
  10792. "Make a compact tree which shows all headlines marked with TODO.
  10793. The tree will show the lines where the regexp matches, and all higher
  10794. headlines above the match.
  10795. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10796. With a numeric prefix N, construct a sparse tree for the Nth element
  10797. of `org-todo-keywords-1'."
  10798. (interactive "P")
  10799. (let ((case-fold-search nil)
  10800. (kwd-re
  10801. (cond ((null arg) org-not-done-regexp)
  10802. ((equal arg '(4))
  10803. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10804. (mapcar 'list org-todo-keywords-1))))
  10805. (concat "\\("
  10806. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10807. "\\)\\>")))
  10808. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10809. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10810. org-todo-keywords-1)))
  10811. (t (error "Invalid prefix argument: %s" arg)))))
  10812. (message "%d TODO entries found"
  10813. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  10814. (defun org-deadline (&optional remove time)
  10815. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10816. With argument REMOVE, remove any deadline from the item.
  10817. With argument TIME, set the deadline at the corresponding date. TIME
  10818. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10819. (interactive "P")
  10820. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10821. (let (org-loop-over-headlines-in-active-region)
  10822. (org-map-entries
  10823. `(org-deadline ',remove ,time) org-loop-over-headlines-in-active-region 'region (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10824. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10825. (repeater (and old-date
  10826. (string-match
  10827. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10828. old-date)
  10829. (match-string 1 old-date))))
  10830. (if remove
  10831. (progn
  10832. (when (and old-date org-log-redeadline)
  10833. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10834. org-log-redeadline))
  10835. (org-remove-timestamp-with-keyword org-deadline-string)
  10836. (message "Item no longer has a deadline."))
  10837. (org-add-planning-info 'deadline time 'closed)
  10838. (when (and old-date org-log-redeadline
  10839. (not (equal old-date
  10840. (substring org-last-inserted-timestamp 1 -1))))
  10841. (org-add-log-setup 'redeadline nil old-date 'findpos
  10842. org-log-redeadline))
  10843. (when repeater
  10844. (save-excursion
  10845. (org-back-to-heading t)
  10846. (when (re-search-forward (concat org-deadline-string " "
  10847. org-last-inserted-timestamp)
  10848. (save-excursion
  10849. (outline-next-heading) (point)) t)
  10850. (goto-char (1- (match-end 0)))
  10851. (insert " " repeater)
  10852. (setq org-last-inserted-timestamp
  10853. (concat (substring org-last-inserted-timestamp 0 -1)
  10854. " " repeater
  10855. (substring org-last-inserted-timestamp -1))))))
  10856. (message "Deadline on %s" org-last-inserted-timestamp)))))
  10857. (defun org-schedule (&optional remove time)
  10858. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10859. With argument REMOVE, remove any scheduling date from the item.
  10860. With argument TIME, scheduled at the corresponding date. TIME can
  10861. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10862. (interactive "P")
  10863. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10864. (let (org-loop-over-headlines-in-active-region)
  10865. (org-map-entries
  10866. `(org-schedule ',remove ,time) org-loop-over-headlines-in-active-region 'region (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10867. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10868. (repeater (and old-date
  10869. (string-match
  10870. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10871. old-date)
  10872. (match-string 1 old-date))))
  10873. (if remove
  10874. (progn
  10875. (when (and old-date org-log-reschedule)
  10876. (org-add-log-setup 'delschedule nil old-date 'findpos
  10877. org-log-reschedule))
  10878. (org-remove-timestamp-with-keyword org-scheduled-string)
  10879. (message "Item is no longer scheduled."))
  10880. (org-add-planning-info 'scheduled time 'closed)
  10881. (when (and old-date org-log-reschedule
  10882. (not (equal old-date
  10883. (substring org-last-inserted-timestamp 1 -1))))
  10884. (org-add-log-setup 'reschedule nil old-date 'findpos
  10885. org-log-reschedule))
  10886. (when repeater
  10887. (save-excursion
  10888. (org-back-to-heading t)
  10889. (when (re-search-forward (concat org-scheduled-string " "
  10890. org-last-inserted-timestamp)
  10891. (save-excursion
  10892. (outline-next-heading) (point)) t)
  10893. (goto-char (1- (match-end 0)))
  10894. (insert " " repeater)
  10895. (setq org-last-inserted-timestamp
  10896. (concat (substring org-last-inserted-timestamp 0 -1)
  10897. " " repeater
  10898. (substring org-last-inserted-timestamp -1))))))
  10899. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  10900. (defun org-get-scheduled-time (pom &optional inherit)
  10901. "Get the scheduled time as a time tuple, of a format suitable
  10902. for calling org-schedule with, or if there is no scheduling,
  10903. returns nil."
  10904. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10905. (when time
  10906. (apply 'encode-time (org-parse-time-string time)))))
  10907. (defun org-get-deadline-time (pom &optional inherit)
  10908. "Get the deadline as a time tuple, of a format suitable for
  10909. calling org-deadline with, or if there is no scheduling, returns
  10910. nil."
  10911. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10912. (when time
  10913. (apply 'encode-time (org-parse-time-string time)))))
  10914. (defun org-remove-timestamp-with-keyword (keyword)
  10915. "Remove all time stamps with KEYWORD in the current entry."
  10916. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10917. beg)
  10918. (save-excursion
  10919. (org-back-to-heading t)
  10920. (setq beg (point))
  10921. (outline-next-heading)
  10922. (while (re-search-backward re beg t)
  10923. (replace-match "")
  10924. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10925. (equal (char-before) ?\ ))
  10926. (backward-delete-char 1)
  10927. (if (string-match "^[ \t]*$" (buffer-substring
  10928. (point-at-bol) (point-at-eol)))
  10929. (delete-region (point-at-bol)
  10930. (min (point-max) (1+ (point-at-eol))))))))))
  10931. (defun org-add-planning-info (what &optional time &rest remove)
  10932. "Insert new timestamp with keyword in the line directly after the headline.
  10933. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  10934. If non is given, the user is prompted for a date.
  10935. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10936. be removed."
  10937. (interactive)
  10938. (let (org-time-was-given org-end-time-was-given ts
  10939. end default-time default-input)
  10940. (catch 'exit
  10941. (when (and (memq what '(scheduled deadline))
  10942. (or (not time)
  10943. (and (stringp time)
  10944. (string-match "^[-+]+[0-9]" time))))
  10945. ;; Try to get a default date/time from existing timestamp
  10946. (save-excursion
  10947. (org-back-to-heading t)
  10948. (setq end (save-excursion (outline-next-heading) (point)))
  10949. (when (re-search-forward (if (eq what 'scheduled)
  10950. org-scheduled-time-regexp
  10951. org-deadline-time-regexp)
  10952. end t)
  10953. (setq ts (match-string 1)
  10954. default-time
  10955. (apply 'encode-time (org-parse-time-string ts))
  10956. default-input (and ts (org-get-compact-tod ts))))))
  10957. (when what
  10958. (setq time
  10959. (if (stringp time)
  10960. ;; This is a string (relative or absolute), set proper date
  10961. (apply 'encode-time
  10962. (org-read-date-analyze
  10963. time default-time (decode-time default-time)))
  10964. ;; If necessary, get the time from the user
  10965. (or time (org-read-date nil 'to-time nil nil
  10966. default-time default-input)))))
  10967. (when (and org-insert-labeled-timestamps-at-point
  10968. (member what '(scheduled deadline)))
  10969. (insert
  10970. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10971. (org-insert-time-stamp time org-time-was-given
  10972. nil nil nil (list org-end-time-was-given))
  10973. (setq what nil))
  10974. (save-excursion
  10975. (save-restriction
  10976. (let (col list elt ts buffer-invisibility-spec)
  10977. (org-back-to-heading t)
  10978. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  10979. (goto-char (match-end 1))
  10980. (setq col (current-column))
  10981. (goto-char (match-end 0))
  10982. (if (eobp) (insert "\n") (forward-char 1))
  10983. (when (and (not what)
  10984. (not (looking-at
  10985. (concat "[ \t]*"
  10986. org-keyword-time-not-clock-regexp))))
  10987. ;; Nothing to add, nothing to remove...... :-)
  10988. (throw 'exit nil))
  10989. (if (and (not (looking-at org-outline-regexp))
  10990. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10991. "[^\r\n]*"))
  10992. (not (equal (match-string 1) org-clock-string)))
  10993. (narrow-to-region (match-beginning 0) (match-end 0))
  10994. (insert-before-markers "\n")
  10995. (backward-char 1)
  10996. (narrow-to-region (point) (point))
  10997. (and org-adapt-indentation (org-indent-to-column col)))
  10998. ;; Check if we have to remove something.
  10999. (setq list (cons what remove))
  11000. (while list
  11001. (setq elt (pop list))
  11002. (when (or (and (eq elt 'scheduled)
  11003. (re-search-forward org-scheduled-time-regexp nil t))
  11004. (and (eq elt 'deadline)
  11005. (re-search-forward org-deadline-time-regexp nil t))
  11006. (and (eq elt 'closed)
  11007. (re-search-forward org-closed-time-regexp nil t)))
  11008. (replace-match "")
  11009. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11010. (and (looking-at "[ \t]+") (replace-match ""))
  11011. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11012. (when what
  11013. (insert
  11014. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11015. (cond ((eq what 'scheduled) org-scheduled-string)
  11016. ((eq what 'deadline) org-deadline-string)
  11017. ((eq what 'closed) org-closed-string))
  11018. " ")
  11019. (setq ts (org-insert-time-stamp
  11020. time
  11021. (or org-time-was-given
  11022. (and (eq what 'closed) org-log-done-with-time))
  11023. (eq what 'closed)
  11024. nil nil (list org-end-time-was-given)))
  11025. (insert
  11026. (if (not (or (bolp) (eq (char-before) ?\ )
  11027. (memq (char-after) '(32 10))
  11028. (eobp))) " " ""))
  11029. (end-of-line 1))
  11030. (goto-char (point-min))
  11031. (widen)
  11032. (if (and (looking-at "[ \t]*\n")
  11033. (equal (char-before) ?\n))
  11034. (delete-region (1- (point)) (point-at-eol)))
  11035. ts))))))
  11036. (defvar org-log-note-marker (make-marker))
  11037. (defvar org-log-note-purpose nil)
  11038. (defvar org-log-note-state nil)
  11039. (defvar org-log-note-previous-state nil)
  11040. (defvar org-log-note-how nil)
  11041. (defvar org-log-note-extra nil)
  11042. (defvar org-log-note-window-configuration nil)
  11043. (defvar org-log-note-return-to (make-marker))
  11044. (defvar org-log-note-effective-time nil
  11045. "Remembered current time so that dynamically scoped
  11046. `org-extend-today-until' affects tha timestamps in state change
  11047. log")
  11048. (defvar org-log-post-message nil
  11049. "Message to be displayed after a log note has been stored.
  11050. The auto-repeater uses this.")
  11051. (defun org-add-note ()
  11052. "Add a note to the current entry.
  11053. This is done in the same way as adding a state change note."
  11054. (interactive)
  11055. (org-add-log-setup 'note nil nil 'findpos nil))
  11056. (defvar org-property-end-re)
  11057. (defun org-add-log-setup (&optional purpose state prev-state
  11058. findpos how extra)
  11059. "Set up the post command hook to take a note.
  11060. If this is about to TODO state change, the new state is expected in STATE.
  11061. When FINDPOS is non-nil, find the correct position for the note in
  11062. the current entry. If not, assume that it can be inserted at point.
  11063. HOW is an indicator what kind of note should be created.
  11064. EXTRA is additional text that will be inserted into the notes buffer."
  11065. (let* ((org-log-into-drawer (org-log-into-drawer))
  11066. (drawer (cond ((stringp org-log-into-drawer)
  11067. org-log-into-drawer)
  11068. (org-log-into-drawer "LOGBOOK")
  11069. (t nil))))
  11070. (save-restriction
  11071. (save-excursion
  11072. (when findpos
  11073. (org-back-to-heading t)
  11074. (narrow-to-region (point) (save-excursion
  11075. (outline-next-heading) (point)))
  11076. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11077. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11078. "[^\r\n]*\\)?"))
  11079. (goto-char (match-end 0))
  11080. (cond
  11081. (drawer
  11082. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11083. nil t)
  11084. (progn
  11085. (goto-char (match-end 0))
  11086. (or org-log-states-order-reversed
  11087. (and (re-search-forward org-property-end-re nil t)
  11088. (goto-char (1- (match-beginning 0))))))
  11089. (insert "\n:" drawer ":\n:END:")
  11090. (beginning-of-line 0)
  11091. (org-indent-line-function)
  11092. (beginning-of-line 2)
  11093. (org-indent-line-function)
  11094. (end-of-line 0)))
  11095. ((and org-log-state-notes-insert-after-drawers
  11096. (save-excursion
  11097. (forward-line) (looking-at org-drawer-regexp)))
  11098. (forward-line)
  11099. (while (looking-at org-drawer-regexp)
  11100. (goto-char (match-end 0))
  11101. (re-search-forward org-property-end-re (point-max) t)
  11102. (forward-line))
  11103. (forward-line -1)))
  11104. (unless org-log-states-order-reversed
  11105. (and (= (char-after) ?\n) (forward-char 1))
  11106. (org-skip-over-state-notes)
  11107. (skip-chars-backward " \t\n\r")))
  11108. (move-marker org-log-note-marker (point))
  11109. (setq org-log-note-purpose purpose
  11110. org-log-note-state state
  11111. org-log-note-previous-state prev-state
  11112. org-log-note-how how
  11113. org-log-note-extra extra
  11114. org-log-note-effective-time (org-current-effective-time))
  11115. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11116. (defun org-skip-over-state-notes ()
  11117. "Skip past the list of State notes in an entry."
  11118. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11119. (when (ignore-errors (goto-char (org-in-item-p)))
  11120. (let* ((struct (org-list-struct))
  11121. (prevs (org-list-prevs-alist struct)))
  11122. (while (looking-at "[ \t]*- State")
  11123. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11124. (org-list-get-item-end (point) struct)))))))
  11125. (defun org-add-log-note (&optional purpose)
  11126. "Pop up a window for taking a note, and add this note later at point."
  11127. (remove-hook 'post-command-hook 'org-add-log-note)
  11128. (setq org-log-note-window-configuration (current-window-configuration))
  11129. (delete-other-windows)
  11130. (move-marker org-log-note-return-to (point))
  11131. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11132. (goto-char org-log-note-marker)
  11133. (org-switch-to-buffer-other-window "*Org Note*")
  11134. (erase-buffer)
  11135. (if (memq org-log-note-how '(time state))
  11136. (let (current-prefix-arg) (org-store-log-note))
  11137. (let ((org-inhibit-startup t)) (org-mode))
  11138. (insert (format "# Insert note for %s.
  11139. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11140. (cond
  11141. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11142. ((eq org-log-note-purpose 'done) "closed todo item")
  11143. ((eq org-log-note-purpose 'state)
  11144. (format "state change from \"%s\" to \"%s\""
  11145. (or org-log-note-previous-state "")
  11146. (or org-log-note-state "")))
  11147. ((eq org-log-note-purpose 'reschedule)
  11148. "rescheduling")
  11149. ((eq org-log-note-purpose 'delschedule)
  11150. "no longer scheduled")
  11151. ((eq org-log-note-purpose 'redeadline)
  11152. "changing deadline")
  11153. ((eq org-log-note-purpose 'deldeadline)
  11154. "removing deadline")
  11155. ((eq org-log-note-purpose 'refile)
  11156. "refiling")
  11157. ((eq org-log-note-purpose 'note)
  11158. "this entry")
  11159. (t (error "This should not happen")))))
  11160. (if org-log-note-extra (insert org-log-note-extra))
  11161. (org-set-local 'org-finish-function 'org-store-log-note)
  11162. (run-hooks 'org-log-buffer-setup-hook)))
  11163. (defvar org-note-abort nil) ; dynamically scoped
  11164. (defun org-store-log-note ()
  11165. "Finish taking a log note, and insert it to where it belongs."
  11166. (let ((txt (buffer-string))
  11167. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11168. lines ind bul)
  11169. (kill-buffer (current-buffer))
  11170. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  11171. (setq txt (replace-match "" t t txt)))
  11172. (if (string-match "\\s-+\\'" txt)
  11173. (setq txt (replace-match "" t t txt)))
  11174. (setq lines (org-split-string txt "\n"))
  11175. (when (and note (string-match "\\S-" note))
  11176. (setq note
  11177. (org-replace-escapes
  11178. note
  11179. (list (cons "%u" (user-login-name))
  11180. (cons "%U" user-full-name)
  11181. (cons "%t" (format-time-string
  11182. (org-time-stamp-format 'long 'inactive)
  11183. org-log-note-effective-time))
  11184. (cons "%T" (format-time-string
  11185. (org-time-stamp-format 'long nil)
  11186. org-log-note-effective-time))
  11187. (cons "%d" (format-time-string
  11188. (org-time-stamp-format nil 'inactive)
  11189. org-log-note-effective-time))
  11190. (cons "%D" (format-time-string
  11191. (org-time-stamp-format nil nil)
  11192. org-log-note-effective-time))
  11193. (cons "%s" (if org-log-note-state
  11194. (concat "\"" org-log-note-state "\"")
  11195. ""))
  11196. (cons "%S" (if org-log-note-previous-state
  11197. (concat "\"" org-log-note-previous-state "\"")
  11198. "\"\"")))))
  11199. (if lines (setq note (concat note " \\\\")))
  11200. (push note lines))
  11201. (when (or current-prefix-arg org-note-abort)
  11202. (when org-log-into-drawer
  11203. (org-remove-empty-drawer-at
  11204. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11205. org-log-note-marker))
  11206. (setq lines nil))
  11207. (when lines
  11208. (with-current-buffer (marker-buffer org-log-note-marker)
  11209. (save-excursion
  11210. (goto-char org-log-note-marker)
  11211. (move-marker org-log-note-marker nil)
  11212. (end-of-line 1)
  11213. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11214. (setq ind (save-excursion
  11215. (if (ignore-errors (goto-char (org-in-item-p)))
  11216. (let ((struct (org-list-struct)))
  11217. (org-list-get-ind
  11218. (org-list-get-top-point struct) struct))
  11219. (skip-chars-backward " \r\t\n")
  11220. (cond
  11221. ((and (org-at-heading-p)
  11222. org-adapt-indentation)
  11223. (1+ (org-current-level)))
  11224. ((org-at-heading-p) 0)
  11225. (t (org-get-indentation))))))
  11226. (setq bul (org-list-bullet-string "-"))
  11227. (org-indent-line-to ind)
  11228. (insert bul (pop lines))
  11229. (let ((ind-body (+ (length bul) ind)))
  11230. (while lines
  11231. (insert "\n")
  11232. (org-indent-line-to ind-body)
  11233. (insert (pop lines))))
  11234. (message "Note stored")
  11235. (org-back-to-heading t)
  11236. (org-cycle-hide-drawers 'children)))))
  11237. (set-window-configuration org-log-note-window-configuration)
  11238. (with-current-buffer (marker-buffer org-log-note-return-to)
  11239. (goto-char org-log-note-return-to))
  11240. (move-marker org-log-note-return-to nil)
  11241. (and org-log-post-message (message "%s" org-log-post-message)))
  11242. (defun org-remove-empty-drawer-at (drawer pos)
  11243. "Remove an empty drawer DRAWER at position POS.
  11244. POS may also be a marker."
  11245. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11246. (save-excursion
  11247. (save-restriction
  11248. (widen)
  11249. (goto-char pos)
  11250. (if (org-in-regexp
  11251. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11252. (replace-match ""))))))
  11253. (defun org-sparse-tree (&optional arg)
  11254. "Create a sparse tree, prompt for the details.
  11255. This command can create sparse trees. You first need to select the type
  11256. of match used to create the tree:
  11257. t Show all TODO entries.
  11258. T Show entries with a specific TODO keyword.
  11259. m Show entries selected by a tags/property match.
  11260. p Enter a property name and its value (both with completion on existing
  11261. names/values) and show entries with that property.
  11262. r Show entries matching a regular expression (`/' can be used as well)
  11263. d Show deadlines due within `org-deadline-warning-days'.
  11264. b Show deadlines and scheduled items before a date.
  11265. a Show deadlines and scheduled items after a date."
  11266. (interactive "P")
  11267. (let (ans kwd value)
  11268. (message "Sparse tree: [r]egexp [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date")
  11269. (setq ans (read-char-exclusive))
  11270. (cond
  11271. ((equal ans ?d)
  11272. (call-interactively 'org-check-deadlines))
  11273. ((equal ans ?b)
  11274. (call-interactively 'org-check-before-date))
  11275. ((equal ans ?a)
  11276. (call-interactively 'org-check-after-date))
  11277. ((equal ans ?t)
  11278. (org-show-todo-tree nil))
  11279. ((equal ans ?T)
  11280. (org-show-todo-tree '(4)))
  11281. ((member ans '(?T ?m))
  11282. (call-interactively 'org-match-sparse-tree))
  11283. ((member ans '(?p ?P))
  11284. (setq kwd (org-icompleting-read "Property: "
  11285. (mapcar 'list (org-buffer-property-keys))))
  11286. (setq value (org-icompleting-read "Value: "
  11287. (mapcar 'list (org-property-values kwd))))
  11288. (unless (string-match "\\`{.*}\\'" value)
  11289. (setq value (concat "\"" value "\"")))
  11290. (org-match-sparse-tree arg (concat kwd "=" value)))
  11291. ((member ans '(?r ?R ?/))
  11292. (call-interactively 'org-occur))
  11293. (t (error "No such sparse tree command \"%c\"" ans)))))
  11294. (defvar org-occur-highlights nil
  11295. "List of overlays used for occur matches.")
  11296. (make-variable-buffer-local 'org-occur-highlights)
  11297. (defvar org-occur-parameters nil
  11298. "Parameters of the active org-occur calls.
  11299. This is a list, each call to org-occur pushes as cons cell,
  11300. containing the regular expression and the callback, onto the list.
  11301. The list can contain several entries if `org-occur' has been called
  11302. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11303. will only contain one set of parameters. When the highlights are
  11304. removed (for example with `C-c C-c', or with the next edit (depending
  11305. on `org-remove-highlights-with-change'), this variable is emptied
  11306. as well.")
  11307. (make-variable-buffer-local 'org-occur-parameters)
  11308. (defun org-occur (regexp &optional keep-previous callback)
  11309. "Make a compact tree which shows all matches of REGEXP.
  11310. The tree will show the lines where the regexp matches, and all higher
  11311. headlines above the match. It will also show the heading after the match,
  11312. to make sure editing the matching entry is easy.
  11313. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11314. call to `org-occur' will be kept, to allow stacking of calls to this
  11315. command.
  11316. If CALLBACK is non-nil, it is a function which is called to confirm
  11317. that the match should indeed be shown."
  11318. (interactive "sRegexp: \nP")
  11319. (when (equal regexp "")
  11320. (error "Regexp cannot be empty"))
  11321. (unless keep-previous
  11322. (org-remove-occur-highlights nil nil t))
  11323. (push (cons regexp callback) org-occur-parameters)
  11324. (let ((cnt 0))
  11325. (save-excursion
  11326. (goto-char (point-min))
  11327. (if (or (not keep-previous) ; do not want to keep
  11328. (not org-occur-highlights)) ; no previous matches
  11329. ;; hide everything
  11330. (org-overview))
  11331. (while (re-search-forward regexp nil t)
  11332. (when (or (not callback)
  11333. (save-match-data (funcall callback)))
  11334. (setq cnt (1+ cnt))
  11335. (when org-highlight-sparse-tree-matches
  11336. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11337. (org-show-context 'occur-tree))))
  11338. (when org-remove-highlights-with-change
  11339. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11340. nil 'local))
  11341. (unless org-sparse-tree-open-archived-trees
  11342. (org-hide-archived-subtrees (point-min) (point-max)))
  11343. (run-hooks 'org-occur-hook)
  11344. (if (org-called-interactively-p 'interactive)
  11345. (message "%d match(es) for regexp %s" cnt regexp))
  11346. cnt))
  11347. (defun org-occur-next-match (&optional n reset)
  11348. "Function for `next-error-function' to find sparse tree matches.
  11349. N is the number of matches to move, when negative move backwards.
  11350. RESET is entirely ignored - this function always goes back to the
  11351. starting point when no match is found."
  11352. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11353. (search-func (if (< n 0)
  11354. 'previous-single-char-property-change
  11355. 'next-single-char-property-change))
  11356. (n (abs n))
  11357. (pos (point))
  11358. p1)
  11359. (catch 'exit
  11360. (while (setq p1 (funcall search-func (point) 'org-type))
  11361. (when (equal p1 limit)
  11362. (goto-char pos)
  11363. (error "No more matches"))
  11364. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11365. (setq n (1- n))
  11366. (when (= n 0)
  11367. (goto-char p1)
  11368. (throw 'exit (point))))
  11369. (goto-char p1))
  11370. (goto-char p1)
  11371. (error "No more matches"))))
  11372. (defun org-show-context (&optional key)
  11373. "Make sure point and context are visible.
  11374. How much context is shown depends upon the variables
  11375. `org-show-hierarchy-above', `org-show-following-heading'. and
  11376. `org-show-siblings'."
  11377. (let ((heading-p (org-on-heading-p t))
  11378. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11379. (following-p (org-get-alist-option org-show-following-heading key))
  11380. (entry-p (org-get-alist-option org-show-entry-below key))
  11381. (siblings-p (org-get-alist-option org-show-siblings key)))
  11382. (catch 'exit
  11383. ;; Show heading or entry text
  11384. (if (and heading-p (not entry-p))
  11385. (org-flag-heading nil) ; only show the heading
  11386. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11387. (org-show-hidden-entry))) ; show entire entry
  11388. (when following-p
  11389. ;; Show next sibling, or heading below text
  11390. (save-excursion
  11391. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11392. (org-flag-heading nil))))
  11393. (when siblings-p (org-show-siblings))
  11394. (when hierarchy-p
  11395. ;; show all higher headings, possibly with siblings
  11396. (save-excursion
  11397. (while (and (condition-case nil
  11398. (progn (org-up-heading-all 1) t)
  11399. (error nil))
  11400. (not (bobp)))
  11401. (org-flag-heading nil)
  11402. (when siblings-p (org-show-siblings))))))))
  11403. (defvar org-reveal-start-hook nil
  11404. "Hook run before revealing a location.")
  11405. (defun org-reveal (&optional siblings)
  11406. "Show current entry, hierarchy above it, and the following headline.
  11407. This can be used to show a consistent set of context around locations
  11408. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11409. not t for the search context.
  11410. With optional argument SIBLINGS, on each level of the hierarchy all
  11411. siblings are shown. This repairs the tree structure to what it would
  11412. look like when opened with hierarchical calls to `org-cycle'.
  11413. With double optional argument \\[universal-argument] \\[universal-argument], \
  11414. go to the parent and show the
  11415. entire tree."
  11416. (interactive "P")
  11417. (run-hooks 'org-reveal-start-hook)
  11418. (let ((org-show-hierarchy-above t)
  11419. (org-show-following-heading t)
  11420. (org-show-siblings (if siblings t org-show-siblings)))
  11421. (org-show-context nil))
  11422. (when (equal siblings '(16))
  11423. (save-excursion
  11424. (when (org-up-heading-safe)
  11425. (org-show-subtree)
  11426. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11427. (defun org-highlight-new-match (beg end)
  11428. "Highlight from BEG to END and mark the highlight is an occur headline."
  11429. (let ((ov (make-overlay beg end)))
  11430. (overlay-put ov 'face 'secondary-selection)
  11431. (overlay-put ov 'org-type 'org-occur)
  11432. (push ov org-occur-highlights)))
  11433. (defun org-remove-occur-highlights (&optional beg end noremove)
  11434. "Remove the occur highlights from the buffer.
  11435. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11436. from the `before-change-functions' in the current buffer."
  11437. (interactive)
  11438. (unless org-inhibit-highlight-removal
  11439. (mapc 'delete-overlay org-occur-highlights)
  11440. (setq org-occur-highlights nil)
  11441. (setq org-occur-parameters nil)
  11442. (unless noremove
  11443. (remove-hook 'before-change-functions
  11444. 'org-remove-occur-highlights 'local))))
  11445. ;;;; Priorities
  11446. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11447. "Regular expression matching the priority indicator.")
  11448. (defvar org-remove-priority-next-time nil)
  11449. (defun org-priority-up ()
  11450. "Increase the priority of the current item."
  11451. (interactive)
  11452. (org-priority 'up))
  11453. (defun org-priority-down ()
  11454. "Decrease the priority of the current item."
  11455. (interactive)
  11456. (org-priority 'down))
  11457. (defun org-priority (&optional action)
  11458. "Change the priority of an item by ARG.
  11459. ACTION can be `set', `up', `down', or a character."
  11460. (interactive)
  11461. (unless org-enable-priority-commands
  11462. (error "Priority commands are disabled"))
  11463. (setq action (or action 'set))
  11464. (let (current new news have remove)
  11465. (save-excursion
  11466. (org-back-to-heading t)
  11467. (if (looking-at org-priority-regexp)
  11468. (setq current (string-to-char (match-string 2))
  11469. have t))
  11470. (cond
  11471. ((eq action 'remove)
  11472. (setq remove t new ?\ ))
  11473. ((or (eq action 'set)
  11474. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11475. (if (not (eq action 'set))
  11476. (setq new action)
  11477. (message "Priority %c-%c, SPC to remove: "
  11478. org-highest-priority org-lowest-priority)
  11479. (save-match-data
  11480. (setq new (read-char-exclusive))))
  11481. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11482. (= (upcase org-lowest-priority) org-lowest-priority))
  11483. (setq new (upcase new)))
  11484. (cond ((equal new ?\ ) (setq remove t))
  11485. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11486. (error "Priority must be between `%c' and `%c'"
  11487. org-highest-priority org-lowest-priority))))
  11488. ((eq action 'up)
  11489. (setq new (if have
  11490. (1- current) ; normal cycling
  11491. ;; last priority was empty
  11492. (if (eq last-command this-command)
  11493. org-lowest-priority ; wrap around empty to lowest
  11494. ;; default
  11495. (if org-priority-start-cycle-with-default
  11496. org-default-priority
  11497. (1- org-default-priority))))))
  11498. ((eq action 'down)
  11499. (setq new (if have
  11500. (1+ current) ; normal cycling
  11501. ;; last priority was empty
  11502. (if (eq last-command this-command)
  11503. org-highest-priority ; wrap around empty to highest
  11504. ;; default
  11505. (if org-priority-start-cycle-with-default
  11506. org-default-priority
  11507. (1+ org-default-priority))))))
  11508. (t (error "Invalid action")))
  11509. (if (or (< (upcase new) org-highest-priority)
  11510. (> (upcase new) org-lowest-priority))
  11511. (if (and (memq action '(up down))
  11512. (not have) (not (eq last-command this-command)))
  11513. ;; `new' is from default priority
  11514. (error
  11515. "The default can not be set, see `org-default-priority' why")
  11516. ;; normal cycling: `new' is beyond highest/lowest priority
  11517. ;; and is wrapped around to the empty priority
  11518. (setq remove t)))
  11519. (setq news (format "%c" new))
  11520. (if have
  11521. (if remove
  11522. (replace-match "" t t nil 1)
  11523. (replace-match news t t nil 2))
  11524. (if remove
  11525. (error "No priority cookie found in line")
  11526. (let ((case-fold-search nil))
  11527. (looking-at org-todo-line-regexp))
  11528. (if (match-end 2)
  11529. (progn
  11530. (goto-char (match-end 2))
  11531. (insert " [#" news "]"))
  11532. (goto-char (match-beginning 3))
  11533. (insert "[#" news "] "))))
  11534. (org-preserve-lc (org-set-tags nil 'align)))
  11535. (if remove
  11536. (message "Priority removed")
  11537. (message "Priority of current item set to %s" news))))
  11538. (defun org-get-priority (s)
  11539. "Find priority cookie and return priority."
  11540. (if (functionp org-get-priority-function)
  11541. (funcall org-get-priority-function)
  11542. (save-match-data
  11543. (if (not (string-match org-priority-regexp s))
  11544. (* 1000 (- org-lowest-priority org-default-priority))
  11545. (* 1000 (- org-lowest-priority
  11546. (string-to-char (match-string 2 s))))))))
  11547. ;;;; Tags
  11548. (defvar org-agenda-archives-mode)
  11549. (defvar org-map-continue-from nil
  11550. "Position from where mapping should continue.
  11551. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  11552. (defvar org-scanner-tags nil
  11553. "The current tag list while the tags scanner is running.")
  11554. (defvar org-trust-scanner-tags nil
  11555. "Should `org-get-tags-at' use the tags for the scanner.
  11556. This is for internal dynamical scoping only.
  11557. When this is non-nil, the function `org-get-tags-at' will return the value
  11558. of `org-scanner-tags' instead of building the list by itself. This
  11559. can lead to large speed-ups when the tags scanner is used in a file with
  11560. many entries, and when the list of tags is retrieved, for example to
  11561. obtain a list of properties. Building the tags list for each entry in such
  11562. a file becomes an N^2 operation - but with this variable set, it scales
  11563. as N.")
  11564. (defun org-scan-tags (action matcher &optional todo-only)
  11565. "Scan headline tags with inheritance and produce output ACTION.
  11566. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11567. or `agenda' to produce an entry list for an agenda view. It can also be
  11568. a Lisp form or a function that should be called at each matched headline, in
  11569. this case the return value is a list of all return values from these calls.
  11570. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11571. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11572. only lines with a TODO keyword are included in the output."
  11573. (require 'org-agenda)
  11574. (let* ((re (concat "^" org-outline-regexp " *\\(\\<\\("
  11575. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11576. (org-re
  11577. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11578. (props (list 'face 'default
  11579. 'done-face 'org-agenda-done
  11580. 'undone-face 'default
  11581. 'mouse-face 'highlight
  11582. 'org-not-done-regexp org-not-done-regexp
  11583. 'org-todo-regexp org-todo-regexp
  11584. 'org-complex-heading-regexp org-complex-heading-regexp
  11585. 'help-echo
  11586. (format "mouse-2 or RET jump to org file %s"
  11587. (abbreviate-file-name
  11588. (or (buffer-file-name (buffer-base-buffer))
  11589. (buffer-name (buffer-base-buffer)))))))
  11590. (case-fold-search nil)
  11591. (org-map-continue-from nil)
  11592. lspos tags tags-list
  11593. (tags-alist (list (cons 0 org-file-tags)))
  11594. (llast 0) rtn rtn1 level category i txt
  11595. todo marker entry priority)
  11596. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11597. (setq action (list 'lambda nil action)))
  11598. (save-excursion
  11599. (goto-char (point-min))
  11600. (when (eq action 'sparse-tree)
  11601. (org-overview)
  11602. (org-remove-occur-highlights))
  11603. (while (re-search-forward re nil t)
  11604. (catch :skip
  11605. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11606. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11607. (goto-char (setq lspos (match-beginning 0)))
  11608. (setq level (org-reduced-level (funcall outline-level))
  11609. category (org-get-category))
  11610. (setq i llast llast level)
  11611. ;; remove tag lists from same and sublevels
  11612. (while (>= i level)
  11613. (when (setq entry (assoc i tags-alist))
  11614. (setq tags-alist (delete entry tags-alist)))
  11615. (setq i (1- i)))
  11616. ;; add the next tags
  11617. (when tags
  11618. (setq tags (org-split-string tags ":")
  11619. tags-alist
  11620. (cons (cons level tags) tags-alist)))
  11621. ;; compile tags for current headline
  11622. (setq tags-list
  11623. (if org-use-tag-inheritance
  11624. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11625. tags)
  11626. org-scanner-tags tags-list)
  11627. (when org-use-tag-inheritance
  11628. (setcdr (car tags-alist)
  11629. (mapcar (lambda (x)
  11630. (setq x (copy-sequence x))
  11631. (org-add-prop-inherited x))
  11632. (cdar tags-alist))))
  11633. (when (and tags org-use-tag-inheritance
  11634. (or (not (eq t org-use-tag-inheritance))
  11635. org-tags-exclude-from-inheritance))
  11636. ;; selective inheritance, remove uninherited ones
  11637. (setcdr (car tags-alist)
  11638. (org-remove-uninherited-tags (cdar tags-alist))))
  11639. (when (and
  11640. ;; eval matcher only when the todo condition is OK
  11641. (and (or (not todo-only) (member todo org-not-done-keywords))
  11642. (let ((case-fold-search t)) (eval matcher)))
  11643. ;; Call the skipper, but return t if it does not skip,
  11644. ;; so that the `and' form continues evaluating
  11645. (progn
  11646. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11647. t)
  11648. ;; Check if timestamps are deselecting this entry
  11649. (or (not todo-only)
  11650. (and (member todo org-not-done-keywords)
  11651. (or (not org-agenda-tags-todo-honor-ignore-options)
  11652. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11653. ;; Extra check for the archive tag
  11654. ;; FIXME: Does the skipper already do this????
  11655. (or
  11656. (not (member org-archive-tag tags-list))
  11657. ;; we have an archive tag, should we use this anyway?
  11658. (or (not org-agenda-skip-archived-trees)
  11659. (and (eq action 'agenda) org-agenda-archives-mode))))
  11660. ;; select this headline
  11661. (cond
  11662. ((eq action 'sparse-tree)
  11663. (and org-highlight-sparse-tree-matches
  11664. (org-get-heading) (match-end 0)
  11665. (org-highlight-new-match
  11666. (match-beginning 0) (match-beginning 1)))
  11667. (org-show-context 'tags-tree))
  11668. ((eq action 'agenda)
  11669. (setq txt (org-agenda-format-item
  11670. ""
  11671. (concat
  11672. (if (eq org-tags-match-list-sublevels 'indented)
  11673. (make-string (1- level) ?.) "")
  11674. (org-get-heading))
  11675. category
  11676. tags-list
  11677. )
  11678. priority (org-get-priority txt))
  11679. (goto-char lspos)
  11680. (setq marker (org-agenda-new-marker))
  11681. (org-add-props txt props
  11682. 'org-marker marker 'org-hd-marker marker 'org-category category
  11683. 'todo-state todo
  11684. 'priority priority 'type "tagsmatch")
  11685. (push txt rtn))
  11686. ((functionp action)
  11687. (setq org-map-continue-from nil)
  11688. (save-excursion
  11689. (setq rtn1 (funcall action))
  11690. (push rtn1 rtn)))
  11691. (t (error "Invalid action")))
  11692. ;; if we are to skip sublevels, jump to end of subtree
  11693. (unless org-tags-match-list-sublevels
  11694. (org-end-of-subtree t)
  11695. (backward-char 1))))
  11696. ;; Get the correct position from where to continue
  11697. (if org-map-continue-from
  11698. (goto-char org-map-continue-from)
  11699. (and (= (point) lspos) (end-of-line 1)))))
  11700. (when (and (eq action 'sparse-tree)
  11701. (not org-sparse-tree-open-archived-trees))
  11702. (org-hide-archived-subtrees (point-min) (point-max)))
  11703. (nreverse rtn)))
  11704. (defun org-remove-uninherited-tags (tags)
  11705. "Remove all tags that are not inherited from the list TAGS."
  11706. (cond
  11707. ((eq org-use-tag-inheritance t)
  11708. (if org-tags-exclude-from-inheritance
  11709. (org-delete-all org-tags-exclude-from-inheritance tags)
  11710. tags))
  11711. ((not org-use-tag-inheritance) nil)
  11712. ((stringp org-use-tag-inheritance)
  11713. (delq nil (mapcar
  11714. (lambda (x)
  11715. (if (and (string-match org-use-tag-inheritance x)
  11716. (not (member x org-tags-exclude-from-inheritance)))
  11717. x nil))
  11718. tags)))
  11719. ((listp org-use-tag-inheritance)
  11720. (delq nil (mapcar
  11721. (lambda (x)
  11722. (if (member x org-use-tag-inheritance) x nil))
  11723. tags)))))
  11724. (defvar todo-only) ;; dynamically scoped
  11725. (defun org-match-sparse-tree (&optional todo-only match)
  11726. "Create a sparse tree according to tags string MATCH.
  11727. MATCH can contain positive and negative selection of tags, like
  11728. \"+WORK+URGENT-WITHBOSS\".
  11729. If optional argument TODO-ONLY is non-nil, only select lines that are
  11730. also TODO lines."
  11731. (interactive "P")
  11732. (org-prepare-agenda-buffers (list (current-buffer)))
  11733. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11734. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11735. (defvar org-cached-props nil)
  11736. (defun org-cached-entry-get (pom property)
  11737. (if (or (eq t org-use-property-inheritance)
  11738. (and (stringp org-use-property-inheritance)
  11739. (string-match org-use-property-inheritance property))
  11740. (and (listp org-use-property-inheritance)
  11741. (member property org-use-property-inheritance)))
  11742. ;; Caching is not possible, check it directly
  11743. (org-entry-get pom property 'inherit)
  11744. ;; Get all properties, so that we can do complicated checks easily
  11745. (cdr (assoc property (or org-cached-props
  11746. (setq org-cached-props
  11747. (org-entry-properties pom)))))))
  11748. (defun org-global-tags-completion-table (&optional files)
  11749. "Return the list of all tags in all agenda buffer/files.
  11750. Optional FILES argument is a list of files to which can be used
  11751. instead of the agenda files."
  11752. (save-excursion
  11753. (org-uniquify
  11754. (delq nil
  11755. (apply 'append
  11756. (mapcar
  11757. (lambda (file)
  11758. (set-buffer (find-file-noselect file))
  11759. (append (org-get-buffer-tags)
  11760. (mapcar (lambda (x) (if (stringp (car-safe x))
  11761. (list (car-safe x)) nil))
  11762. org-tag-alist)))
  11763. (if (and files (car files))
  11764. files
  11765. (org-agenda-files))))))))
  11766. (defun org-make-tags-matcher (match)
  11767. "Create the TAGS/TODO matcher form for the selection string MATCH."
  11768. ;; todo-only is scoped dynamically into this function, and the function
  11769. ;; may change it if the matcher asks for it.
  11770. (unless match
  11771. ;; Get a new match request, with completion
  11772. (let ((org-last-tags-completion-table
  11773. (org-global-tags-completion-table)))
  11774. (setq match (org-completing-read-no-i
  11775. "Match: " 'org-tags-completion-function nil nil nil
  11776. 'org-tags-history))))
  11777. ;; Parse the string and create a lisp form
  11778. (let ((match0 match)
  11779. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11780. minus tag mm
  11781. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11782. orterms term orlist re-p str-p level-p level-op time-p
  11783. prop-p pn pv po gv rest)
  11784. (if (string-match "/+" match)
  11785. ;; match contains also a todo-matching request
  11786. (progn
  11787. (setq tagsmatch (substring match 0 (match-beginning 0))
  11788. todomatch (substring match (match-end 0)))
  11789. (if (string-match "^!" todomatch)
  11790. (setq todo-only t todomatch (substring todomatch 1)))
  11791. (if (string-match "^\\s-*$" todomatch)
  11792. (setq todomatch nil)))
  11793. ;; only matching tags
  11794. (setq tagsmatch match todomatch nil))
  11795. ;; Make the tags matcher
  11796. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11797. (setq tagsmatcher t)
  11798. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11799. (while (setq term (pop orterms))
  11800. (while (and (equal (substring term -1) "\\") orterms)
  11801. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11802. (while (string-match re term)
  11803. (setq rest (substring term (match-end 0))
  11804. minus (and (match-end 1)
  11805. (equal (match-string 1 term) "-"))
  11806. tag (save-match-data (replace-regexp-in-string
  11807. "\\\\-" "-"
  11808. (match-string 2 term)))
  11809. re-p (equal (string-to-char tag) ?{)
  11810. level-p (match-end 4)
  11811. prop-p (match-end 5)
  11812. mm (cond
  11813. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11814. (level-p
  11815. (setq level-op (org-op-to-function (match-string 3 term)))
  11816. `(,level-op level ,(string-to-number
  11817. (match-string 4 term))))
  11818. (prop-p
  11819. (setq pn (match-string 5 term)
  11820. po (match-string 6 term)
  11821. pv (match-string 7 term)
  11822. re-p (equal (string-to-char pv) ?{)
  11823. str-p (equal (string-to-char pv) ?\")
  11824. time-p (save-match-data
  11825. (string-match "^\"[[<].*[]>]\"$" pv))
  11826. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11827. (if time-p (setq pv (org-matcher-time pv)))
  11828. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11829. (cond
  11830. ((equal pn "CATEGORY")
  11831. (setq gv '(get-text-property (point) 'org-category)))
  11832. ((equal pn "TODO")
  11833. (setq gv 'todo))
  11834. (t
  11835. (setq gv `(org-cached-entry-get nil ,pn))))
  11836. (if re-p
  11837. (if (eq po 'org<>)
  11838. `(not (string-match ,pv (or ,gv "")))
  11839. `(string-match ,pv (or ,gv "")))
  11840. (if str-p
  11841. `(,po (or ,gv "") ,pv)
  11842. `(,po (string-to-number (or ,gv ""))
  11843. ,(string-to-number pv) ))))
  11844. (t `(member ,tag tags-list)))
  11845. mm (if minus (list 'not mm) mm)
  11846. term rest)
  11847. (push mm tagsmatcher))
  11848. (push (if (> (length tagsmatcher) 1)
  11849. (cons 'and tagsmatcher)
  11850. (car tagsmatcher))
  11851. orlist)
  11852. (setq tagsmatcher nil))
  11853. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11854. (setq tagsmatcher
  11855. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11856. ;; Make the todo matcher
  11857. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11858. (setq todomatcher t)
  11859. (setq orterms (org-split-string todomatch "|") orlist nil)
  11860. (while (setq term (pop orterms))
  11861. (while (string-match re term)
  11862. (setq minus (and (match-end 1)
  11863. (equal (match-string 1 term) "-"))
  11864. kwd (match-string 2 term)
  11865. re-p (equal (string-to-char kwd) ?{)
  11866. term (substring term (match-end 0))
  11867. mm (if re-p
  11868. `(string-match ,(substring kwd 1 -1) todo)
  11869. (list 'equal 'todo kwd))
  11870. mm (if minus (list 'not mm) mm))
  11871. (push mm todomatcher))
  11872. (push (if (> (length todomatcher) 1)
  11873. (cons 'and todomatcher)
  11874. (car todomatcher))
  11875. orlist)
  11876. (setq todomatcher nil))
  11877. (setq todomatcher (if (> (length orlist) 1)
  11878. (cons 'or orlist) (car orlist))))
  11879. ;; Return the string and lisp forms of the matcher
  11880. (setq matcher (if todomatcher
  11881. (list 'and tagsmatcher todomatcher)
  11882. tagsmatcher))
  11883. (cons match0 matcher)))
  11884. (defun org-op-to-function (op &optional stringp)
  11885. "Turn an operator into the appropriate function."
  11886. (setq op
  11887. (cond
  11888. ((equal op "<" ) '(< string< org-time<))
  11889. ((equal op ">" ) '(> org-string> org-time>))
  11890. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11891. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11892. ((member op '("=" "==")) '(= string= org-time=))
  11893. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11894. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11895. (defun org<> (a b) (not (= a b)))
  11896. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11897. (defun org-string>= (a b) (not (string< a b)))
  11898. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11899. (defun org-string<> (a b) (not (string= a b)))
  11900. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  11901. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  11902. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  11903. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> 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) (org<> a b)))
  11906. (defun org-2ft (s)
  11907. "Convert S to a floating point time.
  11908. If S is already a number, just return it. If it is a string, parse
  11909. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11910. (cond
  11911. ((numberp s) s)
  11912. ((stringp s)
  11913. (condition-case nil
  11914. (float-time (apply 'encode-time (org-parse-time-string s)))
  11915. (error 0.)))
  11916. (t 0.)))
  11917. (defun org-time-today ()
  11918. "Time in seconds today at 0:00.
  11919. Returns the float number of seconds since the beginning of the
  11920. epoch to the beginning of today (00:00)."
  11921. (float-time (apply 'encode-time
  11922. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  11923. (defun org-matcher-time (s)
  11924. "Interpret a time comparison value."
  11925. (save-match-data
  11926. (cond
  11927. ((string= s "<now>") (float-time))
  11928. ((string= s "<today>") (org-time-today))
  11929. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  11930. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  11931. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  11932. (+ (org-time-today)
  11933. (* (string-to-number (match-string 1 s))
  11934. (cdr (assoc (match-string 2 s)
  11935. '(("d" . 86400.0) ("w" . 604800.0)
  11936. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  11937. (t (org-2ft s)))))
  11938. (defun org-match-any-p (re list)
  11939. "Does re match any element of list?"
  11940. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11941. (delq nil list))
  11942. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  11943. (defvar org-tags-overlay (make-overlay 1 1))
  11944. (org-detach-overlay org-tags-overlay)
  11945. (defun org-get-local-tags-at (&optional pos)
  11946. "Get a list of tags defined in the current headline."
  11947. (org-get-tags-at pos 'local))
  11948. (defun org-get-local-tags ()
  11949. "Get a list of tags defined in the current headline."
  11950. (org-get-tags-at nil 'local))
  11951. (defun org-get-tags-at (&optional pos local)
  11952. "Get a list of all headline tags applicable at POS.
  11953. POS defaults to point. If tags are inherited, the list contains
  11954. the targets in the same sequence as the headlines appear, i.e.
  11955. the tags of the current headline come last.
  11956. When LOCAL is non-nil, only return tags from the current headline,
  11957. ignore inherited ones."
  11958. (interactive)
  11959. (if (and org-trust-scanner-tags
  11960. (or (not pos) (equal pos (point)))
  11961. (not local))
  11962. org-scanner-tags
  11963. (let (tags ltags lastpos parent)
  11964. (save-excursion
  11965. (save-restriction
  11966. (widen)
  11967. (goto-char (or pos (point)))
  11968. (save-match-data
  11969. (catch 'done
  11970. (condition-case nil
  11971. (progn
  11972. (org-back-to-heading t)
  11973. (while (not (equal lastpos (point)))
  11974. (setq lastpos (point))
  11975. (when (looking-at
  11976. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  11977. (setq ltags (org-split-string
  11978. (org-match-string-no-properties 1) ":"))
  11979. (when parent
  11980. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  11981. (setq tags (append
  11982. (if parent
  11983. (org-remove-uninherited-tags ltags)
  11984. ltags)
  11985. tags)))
  11986. (or org-use-tag-inheritance (throw 'done t))
  11987. (if local (throw 'done t))
  11988. (or (org-up-heading-safe) (error nil))
  11989. (setq parent t)))
  11990. (error nil)))))
  11991. (if local
  11992. tags
  11993. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  11994. (defun org-add-prop-inherited (s)
  11995. (add-text-properties 0 (length s) '(inherited t) s)
  11996. s)
  11997. (defun org-toggle-tag (tag &optional onoff)
  11998. "Toggle the tag TAG for the current line.
  11999. If ONOFF is `on' or `off', don't toggle but set to this state."
  12000. (let (res current)
  12001. (save-excursion
  12002. (org-back-to-heading t)
  12003. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12004. (point-at-eol) t)
  12005. (progn
  12006. (setq current (match-string 1))
  12007. (replace-match ""))
  12008. (setq current ""))
  12009. (setq current (nreverse (org-split-string current ":")))
  12010. (cond
  12011. ((eq onoff 'on)
  12012. (setq res t)
  12013. (or (member tag current) (push tag current)))
  12014. ((eq onoff 'off)
  12015. (or (not (member tag current)) (setq current (delete tag current))))
  12016. (t (if (member tag current)
  12017. (setq current (delete tag current))
  12018. (setq res t)
  12019. (push tag current))))
  12020. (end-of-line 1)
  12021. (if current
  12022. (progn
  12023. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12024. (org-set-tags nil t))
  12025. (delete-horizontal-space))
  12026. (run-hooks 'org-after-tags-change-hook))
  12027. res))
  12028. (defun org-align-tags-here (to-col)
  12029. ;; Assumes that this is a headline
  12030. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12031. (beginning-of-line 1)
  12032. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12033. (< pos (match-beginning 2)))
  12034. (progn
  12035. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12036. (goto-char (match-beginning 1))
  12037. (insert " ")
  12038. (delete-region (point) (1+ (match-beginning 2)))
  12039. (setq ncol (max (current-column)
  12040. (1+ col)
  12041. (if (> to-col 0)
  12042. to-col
  12043. (- (abs to-col) tags-l))))
  12044. (setq p (point))
  12045. (insert (make-string (- ncol (current-column)) ?\ ))
  12046. (setq ncol (current-column))
  12047. (when indent-tabs-mode (tabify p (point-at-eol)))
  12048. (org-move-to-column (min ncol col) t))
  12049. (goto-char pos))))
  12050. (defun org-set-tags-command (&optional arg just-align)
  12051. "Call the set-tags command for the current entry."
  12052. (interactive "P")
  12053. (if (org-on-heading-p)
  12054. (org-set-tags arg just-align)
  12055. (save-excursion
  12056. (org-back-to-heading t)
  12057. (org-set-tags arg just-align))))
  12058. (defun org-set-tags-to (data)
  12059. "Set the tags of the current entry to DATA, replacing the current tags.
  12060. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12061. If DATA is nil or the empty string, any tags will be removed."
  12062. (interactive "sTags: ")
  12063. (setq data
  12064. (cond
  12065. ((eq data nil) "")
  12066. ((equal data "") "")
  12067. ((stringp data)
  12068. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12069. ":"))
  12070. ((listp data)
  12071. (concat ":" (mapconcat 'identity data ":") ":"))
  12072. (t nil)))
  12073. (when data
  12074. (save-excursion
  12075. (org-back-to-heading t)
  12076. (when (looking-at org-complex-heading-regexp)
  12077. (if (match-end 5)
  12078. (progn
  12079. (goto-char (match-beginning 5))
  12080. (insert data)
  12081. (delete-region (point) (point-at-eol))
  12082. (org-set-tags nil 'align))
  12083. (goto-char (point-at-eol))
  12084. (insert " " data)
  12085. (org-set-tags nil 'align)))
  12086. (beginning-of-line 1)
  12087. (if (looking-at ".*?\\([ \t]+\\)$")
  12088. (delete-region (match-beginning 1) (match-end 1))))))
  12089. (defun org-align-all-tags ()
  12090. "Align the tags i all headings."
  12091. (interactive)
  12092. (save-excursion
  12093. (or (ignore-errors (org-back-to-heading t))
  12094. (outline-next-heading))
  12095. (if (org-on-heading-p)
  12096. (org-set-tags t)
  12097. (message "No headings"))))
  12098. (defvar org-indent-indentation-per-level)
  12099. (defun org-set-tags (&optional arg just-align)
  12100. "Set the tags for the current headline.
  12101. With prefix ARG, realign all tags in headings in the current buffer."
  12102. (interactive "P")
  12103. (let* ((re org-outline-regexp-bol)
  12104. (current (org-get-tags-string))
  12105. (col (current-column))
  12106. (org-setting-tags t)
  12107. table current-tags inherited-tags ; computed below when needed
  12108. tags p0 c0 c1 rpl di tc level)
  12109. (if arg
  12110. (save-excursion
  12111. (goto-char (point-min))
  12112. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12113. (while (re-search-forward re nil t)
  12114. (org-set-tags nil t)
  12115. (end-of-line 1)))
  12116. (message "All tags realigned to column %d" org-tags-column))
  12117. (if just-align
  12118. (setq tags current)
  12119. ;; Get a new set of tags from the user
  12120. (save-excursion
  12121. (setq table (append org-tag-persistent-alist
  12122. (or org-tag-alist (org-get-buffer-tags))
  12123. (and
  12124. org-complete-tags-always-offer-all-agenda-tags
  12125. (org-global-tags-completion-table
  12126. (org-agenda-files))))
  12127. org-last-tags-completion-table table
  12128. current-tags (org-split-string current ":")
  12129. inherited-tags (nreverse
  12130. (nthcdr (length current-tags)
  12131. (nreverse (org-get-tags-at))))
  12132. tags
  12133. (if (or (eq t org-use-fast-tag-selection)
  12134. (and org-use-fast-tag-selection
  12135. (delq nil (mapcar 'cdr table))))
  12136. (org-fast-tag-selection
  12137. current-tags inherited-tags table
  12138. (if org-fast-tag-selection-include-todo
  12139. org-todo-key-alist))
  12140. (let ((org-add-colon-after-tag-completion t))
  12141. (org-trim
  12142. (org-icompleting-read "Tags: "
  12143. 'org-tags-completion-function
  12144. nil nil current 'org-tags-history))))))
  12145. (while (string-match "[-+&]+" tags)
  12146. ;; No boolean logic, just a list
  12147. (setq tags (replace-match ":" t t tags))))
  12148. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12149. (if org-tags-sort-function
  12150. (setq tags (mapconcat 'identity
  12151. (sort (org-split-string
  12152. tags (org-re "[^[:alnum:]_@#%]+"))
  12153. org-tags-sort-function) ":")))
  12154. (if (string-match "\\`[\t ]*\\'" tags)
  12155. (setq tags "")
  12156. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12157. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12158. ;; Insert new tags at the correct column
  12159. (beginning-of-line 1)
  12160. (setq level (or (and (looking-at org-outline-regexp)
  12161. (- (match-end 0) (point) 1))
  12162. 1))
  12163. (cond
  12164. ((and (equal current "") (equal tags "")))
  12165. ((re-search-forward
  12166. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12167. (point-at-eol) t)
  12168. (if (equal tags "")
  12169. (setq rpl "")
  12170. (goto-char (match-beginning 0))
  12171. (setq c0 (current-column)
  12172. ;; compute offset for the case of org-indent-mode active
  12173. di (if org-indent-mode
  12174. (* (1- org-indent-indentation-per-level) (1- level))
  12175. 0)
  12176. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12177. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12178. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12179. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12180. (replace-match rpl t t)
  12181. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12182. tags)
  12183. (t (error "Tags alignment failed")))
  12184. (org-move-to-column col)
  12185. (unless just-align
  12186. (run-hooks 'org-after-tags-change-hook)))))
  12187. (defun org-change-tag-in-region (beg end tag off)
  12188. "Add or remove TAG for each entry in the region.
  12189. This works in the agenda, and also in an org-mode buffer."
  12190. (interactive
  12191. (list (region-beginning) (region-end)
  12192. (let ((org-last-tags-completion-table
  12193. (if (eq major-mode 'org-mode)
  12194. (org-get-buffer-tags)
  12195. (org-global-tags-completion-table))))
  12196. (org-icompleting-read
  12197. "Tag: " 'org-tags-completion-function nil nil nil
  12198. 'org-tags-history))
  12199. (progn
  12200. (message "[s]et or [r]emove? ")
  12201. (equal (read-char-exclusive) ?r))))
  12202. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12203. (let ((agendap (equal major-mode 'org-agenda-mode))
  12204. l1 l2 m buf pos newhead (cnt 0))
  12205. (goto-char end)
  12206. (setq l2 (1- (org-current-line)))
  12207. (goto-char beg)
  12208. (setq l1 (org-current-line))
  12209. (loop for l from l1 to l2 do
  12210. (org-goto-line l)
  12211. (setq m (get-text-property (point) 'org-hd-marker))
  12212. (when (or (and (eq major-mode 'org-mode) (org-on-heading-p))
  12213. (and agendap m))
  12214. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12215. pos (if agendap m (point)))
  12216. (with-current-buffer buf
  12217. (save-excursion
  12218. (save-restriction
  12219. (goto-char pos)
  12220. (setq cnt (1+ cnt))
  12221. (org-toggle-tag tag (if off 'off 'on))
  12222. (setq newhead (org-get-heading)))))
  12223. (and agendap (org-agenda-change-all-lines newhead m))))
  12224. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12225. (defun org-tags-completion-function (string predicate &optional flag)
  12226. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12227. (confirm (lambda (x) (stringp (car x)))))
  12228. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12229. (setq s1 (match-string 1 string)
  12230. s2 (match-string 2 string))
  12231. (setq s1 "" s2 string))
  12232. (cond
  12233. ((eq flag nil)
  12234. ;; try completion
  12235. (setq rtn (try-completion s2 ctable confirm))
  12236. (if (stringp rtn)
  12237. (setq rtn
  12238. (concat s1 s2 (substring rtn (length s2))
  12239. (if (and org-add-colon-after-tag-completion
  12240. (assoc rtn ctable))
  12241. ":" ""))))
  12242. rtn)
  12243. ((eq flag t)
  12244. ;; all-completions
  12245. (all-completions s2 ctable confirm)
  12246. )
  12247. ((eq flag 'lambda)
  12248. ;; exact match?
  12249. (assoc s2 ctable)))
  12250. ))
  12251. (defun org-fast-tag-insert (kwd tags face &optional end)
  12252. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12253. (insert (format "%-12s" (concat kwd ":"))
  12254. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12255. (or end "")))
  12256. (defun org-fast-tag-show-exit (flag)
  12257. (save-excursion
  12258. (org-goto-line 3)
  12259. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12260. (replace-match ""))
  12261. (when flag
  12262. (end-of-line 1)
  12263. (org-move-to-column (- (window-width) 19) t)
  12264. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12265. (defun org-set-current-tags-overlay (current prefix)
  12266. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12267. (if (featurep 'xemacs)
  12268. (org-overlay-display org-tags-overlay (concat prefix s)
  12269. 'secondary-selection)
  12270. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12271. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12272. (defvar org-last-tag-selection-key nil)
  12273. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12274. "Fast tag selection with single keys.
  12275. CURRENT is the current list of tags in the headline, INHERITED is the
  12276. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12277. possibly with grouping information. TODO-TABLE is a similar table with
  12278. TODO keywords, should these have keys assigned to them.
  12279. If the keys are nil, a-z are automatically assigned.
  12280. Returns the new tags string, or nil to not change the current settings."
  12281. (let* ((fulltable (append table todo-table))
  12282. (maxlen (apply 'max (mapcar
  12283. (lambda (x)
  12284. (if (stringp (car x)) (string-width (car x)) 0))
  12285. fulltable)))
  12286. (buf (current-buffer))
  12287. (expert (eq org-fast-tag-selection-single-key 'expert))
  12288. (buffer-tags nil)
  12289. (fwidth (+ maxlen 3 1 3))
  12290. (ncol (/ (- (window-width) 4) fwidth))
  12291. (i-face 'org-done)
  12292. (c-face 'org-todo)
  12293. tg cnt e c char c1 c2 ntable tbl rtn
  12294. ov-start ov-end ov-prefix
  12295. (exit-after-next org-fast-tag-selection-single-key)
  12296. (done-keywords org-done-keywords)
  12297. groups ingroup)
  12298. (save-excursion
  12299. (beginning-of-line 1)
  12300. (if (looking-at
  12301. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12302. (setq ov-start (match-beginning 1)
  12303. ov-end (match-end 1)
  12304. ov-prefix "")
  12305. (setq ov-start (1- (point-at-eol))
  12306. ov-end (1+ ov-start))
  12307. (skip-chars-forward "^\n\r")
  12308. (setq ov-prefix
  12309. (concat
  12310. (buffer-substring (1- (point)) (point))
  12311. (if (> (current-column) org-tags-column)
  12312. " "
  12313. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12314. (move-overlay org-tags-overlay ov-start ov-end)
  12315. (save-window-excursion
  12316. (if expert
  12317. (set-buffer (get-buffer-create " *Org tags*"))
  12318. (delete-other-windows)
  12319. (split-window-vertically)
  12320. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12321. (erase-buffer)
  12322. (org-set-local 'org-done-keywords done-keywords)
  12323. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12324. (org-fast-tag-insert "Current" current c-face "\n\n")
  12325. (org-fast-tag-show-exit exit-after-next)
  12326. (org-set-current-tags-overlay current ov-prefix)
  12327. (setq tbl fulltable char ?a cnt 0)
  12328. (while (setq e (pop tbl))
  12329. (cond
  12330. ((equal (car e) :startgroup)
  12331. (push '() groups) (setq ingroup t)
  12332. (when (not (= cnt 0))
  12333. (setq cnt 0)
  12334. (insert "\n"))
  12335. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12336. ((equal (car e) :endgroup)
  12337. (setq ingroup nil cnt 0)
  12338. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12339. ((equal e '(:newline))
  12340. (when (not (= cnt 0))
  12341. (setq cnt 0)
  12342. (insert "\n")
  12343. (setq e (car tbl))
  12344. (while (equal (car tbl) '(:newline))
  12345. (insert "\n")
  12346. (setq tbl (cdr tbl)))))
  12347. (t
  12348. (setq tg (copy-sequence (car e)) c2 nil)
  12349. (if (cdr e)
  12350. (setq c (cdr e))
  12351. ;; automatically assign a character.
  12352. (setq c1 (string-to-char
  12353. (downcase (substring
  12354. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12355. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12356. (while (or (rassoc char ntable) (rassoc char table))
  12357. (setq char (1+ char)))
  12358. (setq c2 c1))
  12359. (setq c (or c2 char)))
  12360. (if ingroup (push tg (car groups)))
  12361. (setq tg (org-add-props tg nil 'face
  12362. (cond
  12363. ((not (assoc tg table))
  12364. (org-get-todo-face tg))
  12365. ((member tg current) c-face)
  12366. ((member tg inherited) i-face)
  12367. (t nil))))
  12368. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12369. (insert "[" c "] " tg (make-string
  12370. (- fwidth 4 (length tg)) ?\ ))
  12371. (push (cons tg c) ntable)
  12372. (when (= (setq cnt (1+ cnt)) ncol)
  12373. (insert "\n")
  12374. (if ingroup (insert " "))
  12375. (setq cnt 0)))))
  12376. (setq ntable (nreverse ntable))
  12377. (insert "\n")
  12378. (goto-char (point-min))
  12379. (if (not expert) (org-fit-window-to-buffer))
  12380. (setq rtn
  12381. (catch 'exit
  12382. (while t
  12383. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12384. (if (not groups) "no " "")
  12385. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12386. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12387. (setq org-last-tag-selection-key c)
  12388. (cond
  12389. ((= c ?\r) (throw 'exit t))
  12390. ((= c ?!)
  12391. (setq groups (not groups))
  12392. (goto-char (point-min))
  12393. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12394. ((= c ?\C-c)
  12395. (if (not expert)
  12396. (org-fast-tag-show-exit
  12397. (setq exit-after-next (not exit-after-next)))
  12398. (setq expert nil)
  12399. (delete-other-windows)
  12400. (set-window-buffer (split-window-vertically) " *Org tags*")
  12401. (org-switch-to-buffer-other-window " *Org tags*")
  12402. (org-fit-window-to-buffer)))
  12403. ((or (= c ?\C-g)
  12404. (and (= c ?q) (not (rassoc c ntable))))
  12405. (org-detach-overlay org-tags-overlay)
  12406. (setq quit-flag t))
  12407. ((= c ?\ )
  12408. (setq current nil)
  12409. (if exit-after-next (setq exit-after-next 'now)))
  12410. ((= c ?\t)
  12411. (condition-case nil
  12412. (setq tg (org-icompleting-read
  12413. "Tag: "
  12414. (or buffer-tags
  12415. (with-current-buffer buf
  12416. (org-get-buffer-tags)))))
  12417. (quit (setq tg "")))
  12418. (when (string-match "\\S-" tg)
  12419. (add-to-list 'buffer-tags (list tg))
  12420. (if (member tg current)
  12421. (setq current (delete tg current))
  12422. (push tg current)))
  12423. (if exit-after-next (setq exit-after-next 'now)))
  12424. ((setq e (rassoc c todo-table) tg (car e))
  12425. (with-current-buffer buf
  12426. (save-excursion (org-todo tg)))
  12427. (if exit-after-next (setq exit-after-next 'now)))
  12428. ((setq e (rassoc c ntable) tg (car e))
  12429. (if (member tg current)
  12430. (setq current (delete tg current))
  12431. (loop for g in groups do
  12432. (if (member tg g)
  12433. (mapc (lambda (x)
  12434. (setq current (delete x current)))
  12435. g)))
  12436. (push tg current))
  12437. (if exit-after-next (setq exit-after-next 'now))))
  12438. ;; Create a sorted list
  12439. (setq current
  12440. (sort current
  12441. (lambda (a b)
  12442. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12443. (if (eq exit-after-next 'now) (throw 'exit t))
  12444. (goto-char (point-min))
  12445. (beginning-of-line 2)
  12446. (delete-region (point) (point-at-eol))
  12447. (org-fast-tag-insert "Current" current c-face)
  12448. (org-set-current-tags-overlay current ov-prefix)
  12449. (while (re-search-forward
  12450. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12451. (setq tg (match-string 1))
  12452. (add-text-properties
  12453. (match-beginning 1) (match-end 1)
  12454. (list 'face
  12455. (cond
  12456. ((member tg current) c-face)
  12457. ((member tg inherited) i-face)
  12458. (t (get-text-property (match-beginning 1) 'face))))))
  12459. (goto-char (point-min)))))
  12460. (org-detach-overlay org-tags-overlay)
  12461. (if rtn
  12462. (mapconcat 'identity current ":")
  12463. nil))))
  12464. (defun org-get-tags-string ()
  12465. "Get the TAGS string in the current headline."
  12466. (unless (org-on-heading-p t)
  12467. (error "Not on a heading"))
  12468. (save-excursion
  12469. (beginning-of-line 1)
  12470. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12471. (org-match-string-no-properties 1)
  12472. "")))
  12473. (defun org-get-tags ()
  12474. "Get the list of tags specified in the current headline."
  12475. (org-split-string (org-get-tags-string) ":"))
  12476. (defun org-get-buffer-tags ()
  12477. "Get a table of all tags used in the buffer, for completion."
  12478. (let (tags)
  12479. (save-excursion
  12480. (goto-char (point-min))
  12481. (while (re-search-forward
  12482. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12483. (when (equal (char-after (point-at-bol 0)) ?*)
  12484. (mapc (lambda (x) (add-to-list 'tags x))
  12485. (org-split-string (org-match-string-no-properties 1) ":")))))
  12486. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12487. (mapcar 'list tags)))
  12488. ;;;; The mapping API
  12489. ;;;###autoload
  12490. (defun org-map-entries (func &optional match scope &rest skip)
  12491. "Call FUNC at each headline selected by MATCH in SCOPE.
  12492. FUNC is a function or a lisp form. The function will be called without
  12493. arguments, with the cursor positioned at the beginning of the headline.
  12494. The return values of all calls to the function will be collected and
  12495. returned as a list.
  12496. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12497. does not need to preserve point. After evaluation, the cursor will be
  12498. moved to the end of the line (presumably of the headline of the
  12499. processed entry) and search continues from there. Under some
  12500. circumstances, this may not produce the wanted results. For example,
  12501. if you have removed (e.g. archived) the current (sub)tree it could
  12502. mean that the next entry will be skipped entirely. In such cases, you
  12503. can specify the position from where search should continue by making
  12504. FUNC set the variable `org-map-continue-from' to the desired buffer
  12505. position.
  12506. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12507. Only headlines that are matched by this query will be considered during
  12508. the iteration. When MATCH is nil or t, all headlines will be
  12509. visited by the iteration.
  12510. SCOPE determines the scope of this command. It can be any of:
  12511. nil The current buffer, respecting the restriction if any
  12512. tree The subtree started with the entry at point
  12513. region The entries within the active region, if any
  12514. file The current buffer, without restriction
  12515. file-with-archives
  12516. The current buffer, and any archives associated with it
  12517. agenda All agenda files
  12518. agenda-with-archives
  12519. All agenda files with any archive files associated with them
  12520. \(file1 file2 ...)
  12521. If this is a list, all files in the list will be scanned
  12522. The remaining args are treated as settings for the skipping facilities of
  12523. the scanner. The following items can be given here:
  12524. archive skip trees with the archive tag.
  12525. comment skip trees with the COMMENT keyword
  12526. function or Emacs Lisp form:
  12527. will be used as value for `org-agenda-skip-function', so whenever
  12528. the function returns t, FUNC will not be called for that
  12529. entry and search will continue from the point where the
  12530. function leaves it.
  12531. If your function needs to retrieve the tags including inherited tags
  12532. at the *current* entry, you can use the value of the variable
  12533. `org-scanner-tags' which will be much faster than getting the value
  12534. with `org-get-tags-at'. If your function gets properties with
  12535. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12536. to t around the call to `org-entry-properties' to get the same speedup.
  12537. Note that if your function moves around to retrieve tags and properties at
  12538. a *different* entry, you cannot use these techniques."
  12539. (unless (and (eq scope 'region) (not (org-region-active-p)))
  12540. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12541. (org-agenda-skip-archived-trees (memq 'archive skip))
  12542. (org-agenda-skip-comment-trees (memq 'comment skip))
  12543. (org-agenda-skip-function
  12544. (car (org-delete-all '(comment archive) skip)))
  12545. (org-tags-match-list-sublevels t)
  12546. matcher file res
  12547. org-todo-keywords-for-agenda
  12548. org-done-keywords-for-agenda
  12549. org-todo-keyword-alist-for-agenda
  12550. org-drawers-for-agenda
  12551. org-tag-alist-for-agenda)
  12552. (cond
  12553. ((eq match t) (setq matcher t))
  12554. ((eq match nil) (setq matcher t))
  12555. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12556. (save-excursion
  12557. (save-restriction
  12558. (cond ((eq scope 'tree)
  12559. (org-back-to-heading t)
  12560. (org-narrow-to-subtree)
  12561. (setq scope nil))
  12562. ((and (eq scope 'region) (org-region-active-p))
  12563. (narrow-to-region (region-beginning)
  12564. (save-excursion
  12565. (goto-char (region-end))
  12566. (unless (and (bolp) (org-at-heading-p))
  12567. (outline-next-heading))
  12568. (point)))
  12569. (setq scope nil)))
  12570. (if (not scope)
  12571. (progn
  12572. (org-prepare-agenda-buffers
  12573. (list (buffer-file-name (current-buffer))))
  12574. (setq res (org-scan-tags func matcher)))
  12575. ;; Get the right scope
  12576. (cond
  12577. ((and scope (listp scope) (symbolp (car scope)))
  12578. (setq scope (eval scope)))
  12579. ((eq scope 'agenda)
  12580. (setq scope (org-agenda-files t)))
  12581. ((eq scope 'agenda-with-archives)
  12582. (setq scope (org-agenda-files t))
  12583. (setq scope (org-add-archive-files scope)))
  12584. ((eq scope 'file)
  12585. (setq scope (list (buffer-file-name))))
  12586. ((eq scope 'file-with-archives)
  12587. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12588. (org-prepare-agenda-buffers scope)
  12589. (while (setq file (pop scope))
  12590. (with-current-buffer (org-find-base-buffer-visiting file)
  12591. (save-excursion
  12592. (save-restriction
  12593. (widen)
  12594. (goto-char (point-min))
  12595. (setq res (append res (org-scan-tags func matcher))))))))))
  12596. res)))
  12597. ;;;; Properties
  12598. ;;; Setting and retrieving properties
  12599. (defconst org-special-properties
  12600. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12601. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
  12602. "The special properties valid in Org-mode.
  12603. These are properties that are not defined in the property drawer,
  12604. but in some other way.")
  12605. (defconst org-default-properties
  12606. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12607. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12608. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12609. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12610. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12611. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12612. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12613. "Some properties that are used by Org-mode for various purposes.
  12614. Being in this list makes sure that they are offered for completion.")
  12615. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12616. "Regular expression matching the first line of a property drawer.")
  12617. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12618. "Regular expression matching the last line of a property drawer.")
  12619. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12620. "Regular expression matching the first line of a property drawer.")
  12621. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12622. "Regular expression matching the first line of a property drawer.")
  12623. (defconst org-property-drawer-re
  12624. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12625. org-property-end-re "\\)\n?")
  12626. "Matches an entire property drawer.")
  12627. (defconst org-clock-drawer-re
  12628. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12629. org-property-end-re "\\)\n?")
  12630. "Matches an entire clock drawer.")
  12631. (defsubst org-re-property (property)
  12632. "Return a regexp matching PROPERTY.
  12633. Match group 1 will be set to the value "
  12634. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12635. (defun org-property-action ()
  12636. "Do an action on properties."
  12637. (interactive)
  12638. (let (c)
  12639. (org-at-property-p)
  12640. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12641. (setq c (read-char-exclusive))
  12642. (cond
  12643. ((equal c ?s)
  12644. (call-interactively 'org-set-property))
  12645. ((equal c ?d)
  12646. (call-interactively 'org-delete-property))
  12647. ((equal c ?D)
  12648. (call-interactively 'org-delete-property-globally))
  12649. ((equal c ?c)
  12650. (call-interactively 'org-compute-property-at-point))
  12651. (t (error "No such property action %c" c)))))
  12652. (defun org-set-effort (&optional value)
  12653. "Set the effort property of the current entry.
  12654. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12655. allowed value."
  12656. (interactive "P")
  12657. (if (equal value 0) (setq value 10))
  12658. (let* ((completion-ignore-case t)
  12659. (prop org-effort-property)
  12660. (cur (org-entry-get nil prop))
  12661. (allowed (org-property-get-allowed-values nil prop 'table))
  12662. (existing (mapcar 'list (org-property-values prop)))
  12663. rpl
  12664. (val (cond
  12665. ((stringp value) value)
  12666. ((and allowed (integerp value))
  12667. (or (car (nth (1- value) allowed))
  12668. (car (org-last allowed))))
  12669. (allowed
  12670. (message "Select 1-9,0, [RET%s]: %s"
  12671. (if cur (concat "=" cur) "")
  12672. (mapconcat 'car allowed " "))
  12673. (setq rpl (read-char-exclusive))
  12674. (if (equal rpl ?\r)
  12675. cur
  12676. (setq rpl (- rpl ?0))
  12677. (if (equal rpl 0) (setq rpl 10))
  12678. (if (and (> rpl 0) (<= rpl (length allowed)))
  12679. (car (nth (1- rpl) allowed))
  12680. (org-completing-read "Effort: " allowed nil))))
  12681. (t
  12682. (let (org-completion-use-ido org-completion-use-iswitchb)
  12683. (org-completing-read
  12684. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12685. (concat "[" cur "]") "")
  12686. ": ")
  12687. existing nil nil "" nil cur))))))
  12688. (unless (equal (org-entry-get nil prop) val)
  12689. (org-entry-put nil prop val))
  12690. (message "%s is now %s" prop val)))
  12691. (defun org-at-property-p ()
  12692. "Is cursor inside a property drawer?"
  12693. (save-excursion
  12694. (beginning-of-line 1)
  12695. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12696. (save-match-data ;; Used by calling procedures
  12697. (let ((p (point))
  12698. (range (unless (org-before-first-heading-p)
  12699. (org-get-property-block))))
  12700. (and range (<= (car range) p) (< p (cdr range))))))))
  12701. (defun org-get-property-block (&optional beg end force)
  12702. "Return the (beg . end) range of the body of the property drawer.
  12703. BEG and END can be beginning and end of subtree, if not given
  12704. they will be found.
  12705. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12706. (catch 'exit
  12707. (save-excursion
  12708. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12709. (end (or end (progn (outline-next-heading) (point)))))
  12710. (goto-char beg)
  12711. (if (re-search-forward org-property-start-re end t)
  12712. (setq beg (1+ (match-end 0)))
  12713. (if force
  12714. (save-excursion
  12715. (org-insert-property-drawer)
  12716. (setq end (progn (outline-next-heading) (point))))
  12717. (throw 'exit nil))
  12718. (goto-char beg)
  12719. (if (re-search-forward org-property-start-re end t)
  12720. (setq beg (1+ (match-end 0)))))
  12721. (if (re-search-forward org-property-end-re end t)
  12722. (setq end (match-beginning 0))
  12723. (or force (throw 'exit nil))
  12724. (goto-char beg)
  12725. (setq end beg)
  12726. (org-indent-line-function)
  12727. (insert ":END:\n"))
  12728. (cons beg end)))))
  12729. (defun org-entry-properties (&optional pom which specific)
  12730. "Get all properties of the entry at point-or-marker POM.
  12731. This includes the TODO keyword, the tags, time strings for deadline,
  12732. scheduled, and clocking, and any additional properties defined in the
  12733. entry. The return value is an alist, keys may occur multiple times
  12734. if the property key was used several times.
  12735. POM may also be nil, in which case the current entry is used.
  12736. If WHICH is nil or `all', get all properties. If WHICH is
  12737. `special' or `standard', only get that subclass. If WHICH
  12738. is a string only get exactly this property. SPECIFIC can be a string, the
  12739. specific property we are interested in. Specifying it can speed
  12740. things up because then unnecessary parsing is avoided."
  12741. (setq which (or which 'all))
  12742. (org-with-point-at pom
  12743. (let ((clockstr (substring org-clock-string 0 -1))
  12744. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12745. (case-fold-search nil)
  12746. beg end range props sum-props key key1 value string clocksum)
  12747. (save-excursion
  12748. (when (condition-case nil
  12749. (and (eq major-mode 'org-mode) (org-back-to-heading t))
  12750. (error nil))
  12751. (setq beg (point))
  12752. (setq sum-props (get-text-property (point) 'org-summaries))
  12753. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12754. (outline-next-heading)
  12755. (setq end (point))
  12756. (when (memq which '(all special))
  12757. ;; Get the special properties, like TODO and tags
  12758. (goto-char beg)
  12759. (when (and (or (not specific) (string= specific "TODO"))
  12760. (looking-at org-todo-line-regexp) (match-end 2))
  12761. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12762. (when (and (or (not specific) (string= specific "PRIORITY"))
  12763. (looking-at org-priority-regexp))
  12764. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12765. (when (or (not specific) (string= specific "FILE"))
  12766. (push (cons "FILE" buffer-file-name) props))
  12767. (when (and (or (not specific) (string= specific "TAGS"))
  12768. (setq value (org-get-tags-string))
  12769. (string-match "\\S-" value))
  12770. (push (cons "TAGS" value) props))
  12771. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12772. (setq value (org-get-tags-at)))
  12773. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12774. ":"))
  12775. props))
  12776. (when (or (not specific) (string= specific "BLOCKED"))
  12777. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12778. (when (or (not specific)
  12779. (member specific
  12780. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12781. "TIMESTAMP" "TIMESTAMP_IA")))
  12782. (catch 'match
  12783. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12784. (setq key (if (match-end 1)
  12785. (substring (org-match-string-no-properties 1)
  12786. 0 -1))
  12787. string (if (equal key clockstr)
  12788. (org-no-properties
  12789. (org-trim
  12790. (buffer-substring
  12791. (match-beginning 3) (goto-char
  12792. (point-at-eol)))))
  12793. (substring (org-match-string-no-properties 3)
  12794. 1 -1)))
  12795. ;; Get the correct property name from the key. This is
  12796. ;; necessary if the user has configured time keywords.
  12797. (setq key1 (concat key ":"))
  12798. (cond
  12799. ((not key)
  12800. (setq key
  12801. (if (= (char-after (match-beginning 3)) ?\[)
  12802. "TIMESTAMP_IA" "TIMESTAMP")))
  12803. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12804. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12805. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12806. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12807. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  12808. (progn
  12809. (push (cons key string) props)
  12810. ;; no need to search further if match is found
  12811. (throw 'match t))
  12812. (when (or (equal key "CLOCK") (not (assoc key props)))
  12813. (push (cons key string) props))))))
  12814. )
  12815. (when (memq which '(all standard))
  12816. ;; Get the standard properties, like :PROP: ...
  12817. (setq range (org-get-property-block beg end))
  12818. (when range
  12819. (goto-char (car range))
  12820. (while (re-search-forward
  12821. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12822. (cdr range) t)
  12823. (setq key (org-match-string-no-properties 1)
  12824. value (org-trim (or (org-match-string-no-properties 2) "")))
  12825. (unless (member key excluded)
  12826. (push (cons key (or value "")) props)))))
  12827. (if clocksum
  12828. (push (cons "CLOCKSUM"
  12829. (org-columns-number-to-string (/ (float clocksum) 60.)
  12830. 'add_times))
  12831. props))
  12832. (unless (assoc "CATEGORY" props)
  12833. (push (cons "CATEGORY" (org-get-category)) props))
  12834. (append sum-props (nreverse props)))))))
  12835. (defun org-entry-get (pom property &optional inherit literal-nil)
  12836. "Get value of PROPERTY for entry at point-or-marker POM.
  12837. If INHERIT is non-nil and the entry does not have the property,
  12838. then also check higher levels of the hierarchy.
  12839. If INHERIT is the symbol `selective', use inheritance only if the setting
  12840. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12841. If the property is present but empty, the return value is the empty string.
  12842. If the property is not present at all, nil is returned.
  12843. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12844. do not interpret it as the list atom nil. This is used for inheritance
  12845. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12846. (org-with-point-at pom
  12847. (if (and inherit (if (eq inherit 'selective)
  12848. (org-property-inherit-p property)
  12849. t))
  12850. (org-entry-get-with-inheritance property literal-nil)
  12851. (if (member property org-special-properties)
  12852. ;; We need a special property. Use `org-entry-properties' to
  12853. ;; retrieve it, but specify the wanted property
  12854. (cdr (assoc property (org-entry-properties nil 'special property)))
  12855. (let ((range (unless (org-before-first-heading-p)
  12856. (org-get-property-block))))
  12857. (when (and range (goto-char (car range)))
  12858. ((lambda (val) (when val (if literal-nil val (org-not-nil val))))
  12859. (cond
  12860. ((re-search-forward
  12861. (org-re-property property) (cdr range) t)
  12862. (if (match-end 1) (org-match-string-no-properties 1) ""))
  12863. ((re-search-forward
  12864. (org-re-property (concat property "+")) (cdr range) t)
  12865. (cdr (assoc
  12866. property
  12867. (org-update-property-plist
  12868. (concat property "+")
  12869. (if (match-end 1) (org-match-string-no-properties 1) "")
  12870. (list (or (assoc property org-file-properties)
  12871. (assoc property org-global-properties)
  12872. (assoc property org-global-properties-fixed)
  12873. ))))))))))))))
  12874. (defun org-property-or-variable-value (var &optional inherit)
  12875. "Check if there is a property fixing the value of VAR.
  12876. If yes, return this value. If not, return the current value of the variable."
  12877. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12878. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12879. (read prop)
  12880. (symbol-value var))))
  12881. (defun org-entry-delete (pom property)
  12882. "Delete the property PROPERTY from entry at point-or-marker POM."
  12883. (org-with-point-at pom
  12884. (if (member property org-special-properties)
  12885. nil ; cannot delete these properties.
  12886. (let ((range (org-get-property-block)))
  12887. (if (and range
  12888. (goto-char (car range))
  12889. (re-search-forward
  12890. (org-re-property property)
  12891. (cdr range) t))
  12892. (progn
  12893. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12894. t)
  12895. nil)))))
  12896. ;; Multi-values properties are properties that contain multiple values
  12897. ;; These values are assumed to be single words, separated by whitespace.
  12898. (defun org-entry-add-to-multivalued-property (pom property value)
  12899. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  12900. (let* ((old (org-entry-get pom property))
  12901. (values (and old (org-split-string old "[ \t]"))))
  12902. (setq value (org-entry-protect-space value))
  12903. (unless (member value values)
  12904. (setq values (cons value values))
  12905. (org-entry-put pom property
  12906. (mapconcat 'identity values " ")))))
  12907. (defun org-entry-remove-from-multivalued-property (pom property value)
  12908. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  12909. (let* ((old (org-entry-get pom property))
  12910. (values (and old (org-split-string old "[ \t]"))))
  12911. (setq value (org-entry-protect-space value))
  12912. (when (member value values)
  12913. (setq values (delete value values))
  12914. (org-entry-put pom property
  12915. (mapconcat 'identity values " ")))))
  12916. (defun org-entry-member-in-multivalued-property (pom property value)
  12917. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  12918. (let* ((old (org-entry-get pom property))
  12919. (values (and old (org-split-string old "[ \t]"))))
  12920. (setq value (org-entry-protect-space value))
  12921. (member value values)))
  12922. (defun org-entry-get-multivalued-property (pom property)
  12923. "Return a list of values in a multivalued property."
  12924. (let* ((value (org-entry-get pom property))
  12925. (values (and value (org-split-string value "[ \t]"))))
  12926. (mapcar 'org-entry-restore-space values)))
  12927. (defun org-entry-put-multivalued-property (pom property &rest values)
  12928. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  12929. VALUES should be a list of strings. Spaces will be protected."
  12930. (org-entry-put pom property
  12931. (mapconcat 'org-entry-protect-space values " "))
  12932. (let* ((value (org-entry-get pom property))
  12933. (values (and value (org-split-string value "[ \t]"))))
  12934. (mapcar 'org-entry-restore-space values)))
  12935. (defun org-entry-protect-space (s)
  12936. "Protect spaces and newline in string S."
  12937. (while (string-match " " s)
  12938. (setq s (replace-match "%20" t t s)))
  12939. (while (string-match "\n" s)
  12940. (setq s (replace-match "%0A" t t s)))
  12941. s)
  12942. (defun org-entry-restore-space (s)
  12943. "Restore spaces and newline in string S."
  12944. (while (string-match "%20" s)
  12945. (setq s (replace-match " " t t s)))
  12946. (while (string-match "%0A" s)
  12947. (setq s (replace-match "\n" t t s)))
  12948. s)
  12949. (defvar org-entry-property-inherited-from (make-marker)
  12950. "Marker pointing to the entry from where a property was inherited.
  12951. Each call to `org-entry-get-with-inheritance' will set this marker to the
  12952. location of the entry where the inheritance search matched. If there was
  12953. no match, the marker will point nowhere.
  12954. Note that also `org-entry-get' calls this function, if the INHERIT flag
  12955. is set.")
  12956. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  12957. "Get entry property, and search higher levels if not present.
  12958. The search will stop at the first ancestor which has the property defined.
  12959. If the value found is \"nil\", return nil to show that the property
  12960. should be considered as undefined (this is the meaning of nil here).
  12961. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  12962. (move-marker org-entry-property-inherited-from nil)
  12963. (let (tmp)
  12964. (unless (org-before-first-heading-p)
  12965. (save-excursion
  12966. (save-restriction
  12967. (widen)
  12968. (catch 'ex
  12969. (while t
  12970. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  12971. (org-back-to-heading t)
  12972. (move-marker org-entry-property-inherited-from (point))
  12973. (throw 'ex tmp))
  12974. (or (org-up-heading-safe) (throw 'ex nil)))))))
  12975. (setq tmp (or tmp
  12976. (cdr (assoc property org-file-properties))
  12977. (cdr (assoc property org-global-properties))
  12978. (cdr (assoc property org-global-properties-fixed))))
  12979. (if literal-nil tmp (org-not-nil tmp))))
  12980. (defvar org-property-changed-functions nil
  12981. "Hook called when the value of a property has changed.
  12982. Each hook function should accept two arguments, the name of the property
  12983. and the new value.")
  12984. (defun org-entry-put (pom property value)
  12985. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  12986. (org-with-point-at pom
  12987. (org-back-to-heading t)
  12988. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  12989. range)
  12990. (cond
  12991. ((equal property "TODO")
  12992. (when (and (stringp value) (string-match "\\S-" value)
  12993. (not (member value org-todo-keywords-1)))
  12994. (error "\"%s\" is not a valid TODO state" value))
  12995. (if (or (not value)
  12996. (not (string-match "\\S-" value)))
  12997. (setq value 'none))
  12998. (org-todo value)
  12999. (org-set-tags nil 'align))
  13000. ((equal property "PRIORITY")
  13001. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13002. (string-to-char value) ?\ ))
  13003. (org-set-tags nil 'align))
  13004. ((equal property "SCHEDULED")
  13005. (if (re-search-forward org-scheduled-time-regexp end t)
  13006. (cond
  13007. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13008. ((eq value 'later) (org-timestamp-change 1 'day))
  13009. (t (call-interactively 'org-schedule)))
  13010. (call-interactively 'org-schedule)))
  13011. ((equal property "DEADLINE")
  13012. (if (re-search-forward org-deadline-time-regexp end t)
  13013. (cond
  13014. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13015. ((eq value 'later) (org-timestamp-change 1 'day))
  13016. (t (call-interactively 'org-deadline)))
  13017. (call-interactively 'org-deadline)))
  13018. ((member property org-special-properties)
  13019. (error "The %s property can not yet be set with `org-entry-put'"
  13020. property))
  13021. (t ; a non-special property
  13022. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13023. (setq range (org-get-property-block beg end 'force))
  13024. (goto-char (car range))
  13025. (if (re-search-forward
  13026. (org-re-property property) (cdr range) t)
  13027. (progn
  13028. (delete-region (match-beginning 0) (match-end 0))
  13029. (goto-char (match-beginning 0)))
  13030. (goto-char (cdr range))
  13031. (insert "\n")
  13032. (backward-char 1)
  13033. (org-indent-line-function))
  13034. (insert ":" property ":")
  13035. (and value (insert " " value))
  13036. (org-indent-line-function)))))
  13037. (run-hook-with-args 'org-property-changed-functions property value)))
  13038. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13039. "Get all property keys in the current buffer.
  13040. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13041. like tags and TODO state.
  13042. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13043. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13044. and others.
  13045. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13046. formats in the current buffer."
  13047. (let (rtn range cfmt s p)
  13048. (save-excursion
  13049. (save-restriction
  13050. (widen)
  13051. (goto-char (point-min))
  13052. (while (re-search-forward org-property-start-re nil t)
  13053. (setq range (org-get-property-block))
  13054. (goto-char (car range))
  13055. (while (re-search-forward
  13056. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13057. (cdr range) t)
  13058. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13059. (outline-next-heading))))
  13060. (when include-specials
  13061. (setq rtn (append org-special-properties rtn)))
  13062. (when include-defaults
  13063. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13064. (add-to-list 'rtn org-effort-property))
  13065. (when include-columns
  13066. (save-excursion
  13067. (save-restriction
  13068. (widen)
  13069. (goto-char (point-min))
  13070. (while (re-search-forward
  13071. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13072. nil t)
  13073. (setq cfmt (match-string 2) s 0)
  13074. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13075. cfmt s)
  13076. (setq s (match-end 0)
  13077. p (match-string 1 cfmt))
  13078. (unless (or (equal p "ITEM")
  13079. (member p org-special-properties))
  13080. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13081. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13082. (defun org-property-values (key)
  13083. "Return a list of all values of property KEY in the current buffer."
  13084. (save-excursion
  13085. (save-restriction
  13086. (widen)
  13087. (goto-char (point-min))
  13088. (let ((re (org-re-property key))
  13089. values)
  13090. (while (re-search-forward re nil t)
  13091. (add-to-list 'values (org-trim (match-string 1))))
  13092. (delete "" values)))))
  13093. (defun org-insert-property-drawer ()
  13094. "Insert a property drawer into the current entry."
  13095. (interactive)
  13096. (org-back-to-heading t)
  13097. (looking-at org-outline-regexp)
  13098. (let ((indent (if org-adapt-indentation
  13099. (- (match-end 0)(match-beginning 0))
  13100. 0))
  13101. (beg (point))
  13102. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13103. end hiddenp)
  13104. (outline-next-heading)
  13105. (setq end (point))
  13106. (goto-char beg)
  13107. (while (re-search-forward re end t))
  13108. (setq hiddenp (outline-invisible-p))
  13109. (end-of-line 1)
  13110. (and (equal (char-after) ?\n) (forward-char 1))
  13111. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13112. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13113. ;; just skip this line
  13114. (beginning-of-line 2)
  13115. ;; Drawer start, find the end
  13116. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13117. (beginning-of-line 1)))
  13118. (org-skip-over-state-notes)
  13119. (skip-chars-backward " \t\n\r")
  13120. (if (eq (char-before) ?*) (forward-char 1))
  13121. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13122. (beginning-of-line 0)
  13123. (org-indent-to-column indent)
  13124. (beginning-of-line 2)
  13125. (org-indent-to-column indent)
  13126. (beginning-of-line 0)
  13127. (if hiddenp
  13128. (save-excursion
  13129. (org-back-to-heading t)
  13130. (hide-entry))
  13131. (org-flag-drawer t))))
  13132. (defvar org-property-set-functions-alist nil
  13133. "Property set function alist.
  13134. Each entry should have the following format:
  13135. (PROPERTY . READ-FUNCTION)
  13136. The read function will be called with the same argument as
  13137. `org-completing-read'.")
  13138. (defun org-set-property-function (property)
  13139. "Get the function that should be used to set PROPERTY.
  13140. This is computed according to `org-property-set-functions-alist'."
  13141. (or (cdr (assoc property org-property-set-functions-alist))
  13142. 'org-completing-read))
  13143. (defun org-read-property-value (property)
  13144. "Read PROPERTY value from user."
  13145. (let* ((completion-ignore-case t)
  13146. (allowed (org-property-get-allowed-values nil property 'table))
  13147. (cur (org-entry-get nil property))
  13148. (prompt (concat property " value"
  13149. (if (and cur (string-match "\\S-" cur))
  13150. (concat " [" cur "]") "") ": "))
  13151. (set-function (org-set-property-function property))
  13152. (val (if allowed
  13153. (funcall set-function prompt allowed nil
  13154. (not (get-text-property 0 'org-unrestricted
  13155. (caar allowed))))
  13156. (let (org-completion-use-ido org-completion-use-iswitchb)
  13157. (funcall set-function prompt
  13158. (mapcar 'list (org-property-values property))
  13159. nil nil "" nil cur)))))
  13160. (if (equal val "")
  13161. cur
  13162. val)))
  13163. (defvar org-last-set-property nil)
  13164. (defun org-read-property-name ()
  13165. "Read a property name."
  13166. (let* ((completion-ignore-case t)
  13167. (keys (org-buffer-property-keys nil t t))
  13168. (default-prop (or (save-excursion
  13169. (save-match-data
  13170. (beginning-of-line)
  13171. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13172. (null (string= (match-string 1) "END"))
  13173. (match-string 1))))
  13174. org-last-set-property))
  13175. (property (org-icompleting-read
  13176. (concat "Property"
  13177. (if default-prop (concat " [" default-prop "]") "")
  13178. ": ")
  13179. (mapcar 'list keys)
  13180. nil nil nil nil
  13181. default-prop
  13182. )))
  13183. (if (member property keys)
  13184. property
  13185. (or (cdr (assoc (downcase property)
  13186. (mapcar (lambda (x) (cons (downcase x) x))
  13187. keys)))
  13188. property))))
  13189. (defun org-set-property (property value)
  13190. "In the current entry, set PROPERTY to VALUE.
  13191. When called interactively, this will prompt for a property name, offering
  13192. completion on existing and default properties. And then it will prompt
  13193. for a value, offering completion either on allowed values (via an inherited
  13194. xxx_ALL property) or on existing values in other instances of this property
  13195. in the current file."
  13196. (interactive (list nil nil))
  13197. (let* ((property (or property (org-read-property-name)))
  13198. (value (or value (org-read-property-value property)))
  13199. (fn (assoc property org-properties-postprocess-alist)))
  13200. (setq org-last-set-property property)
  13201. ;; Possibly postprocess the inserted value:
  13202. (when fn (setq value (funcall (cadr fn) value)))
  13203. (unless (equal (org-entry-get nil property) value)
  13204. (org-entry-put nil property value))))
  13205. (defun org-delete-property (property)
  13206. "In the current entry, delete PROPERTY."
  13207. (interactive
  13208. (let* ((completion-ignore-case t)
  13209. (prop (org-icompleting-read "Property: "
  13210. (org-entry-properties nil 'standard))))
  13211. (list prop)))
  13212. (message "Property %s %s" property
  13213. (if (org-entry-delete nil property)
  13214. "deleted"
  13215. "was not present in the entry")))
  13216. (defun org-delete-property-globally (property)
  13217. "Remove PROPERTY globally, from all entries."
  13218. (interactive
  13219. (let* ((completion-ignore-case t)
  13220. (prop (org-icompleting-read
  13221. "Globally remove property: "
  13222. (mapcar 'list (org-buffer-property-keys)))))
  13223. (list prop)))
  13224. (save-excursion
  13225. (save-restriction
  13226. (widen)
  13227. (goto-char (point-min))
  13228. (let ((cnt 0))
  13229. (while (re-search-forward
  13230. (org-re-property property)
  13231. nil t)
  13232. (setq cnt (1+ cnt))
  13233. (replace-match ""))
  13234. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13235. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13236. (defun org-compute-property-at-point ()
  13237. "Compute the property at point.
  13238. This looks for an enclosing column format, extracts the operator and
  13239. then applies it to the property in the column format's scope."
  13240. (interactive)
  13241. (unless (org-at-property-p)
  13242. (error "Not at a property"))
  13243. (let ((prop (org-match-string-no-properties 2)))
  13244. (org-columns-get-format-and-top-level)
  13245. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13246. (error "No operator defined for property %s" prop))
  13247. (org-columns-compute prop)))
  13248. (defvar org-property-allowed-value-functions nil
  13249. "Hook for functions supplying allowed values for a specific property.
  13250. The functions must take a single argument, the name of the property, and
  13251. return a flat list of allowed values. If \":ETC\" is one of
  13252. the values, this means that these values are intended as defaults for
  13253. completion, but that other values should be allowed too.
  13254. The functions must return nil if they are not responsible for this
  13255. property.")
  13256. (defun org-property-get-allowed-values (pom property &optional table)
  13257. "Get allowed values for the property PROPERTY.
  13258. When TABLE is non-nil, return an alist that can directly be used for
  13259. completion."
  13260. (let (vals)
  13261. (cond
  13262. ((equal property "TODO")
  13263. (setq vals (org-with-point-at pom
  13264. (append org-todo-keywords-1 '("")))))
  13265. ((equal property "PRIORITY")
  13266. (let ((n org-lowest-priority))
  13267. (while (>= n org-highest-priority)
  13268. (push (char-to-string n) vals)
  13269. (setq n (1- n)))))
  13270. ((member property org-special-properties))
  13271. ((setq vals (run-hook-with-args-until-success
  13272. 'org-property-allowed-value-functions property)))
  13273. (t
  13274. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13275. (when (and vals (string-match "\\S-" vals))
  13276. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13277. (setq vals (mapcar (lambda (x)
  13278. (cond ((stringp x) x)
  13279. ((numberp x) (number-to-string x))
  13280. ((symbolp x) (symbol-name x))
  13281. (t "???")))
  13282. vals)))))
  13283. (when (member ":ETC" vals)
  13284. (setq vals (remove ":ETC" vals))
  13285. (org-add-props (car vals) '(org-unrestricted t)))
  13286. (if table (mapcar 'list vals) vals)))
  13287. (defun org-property-previous-allowed-value (&optional previous)
  13288. "Switch to the next allowed value for this property."
  13289. (interactive)
  13290. (org-property-next-allowed-value t))
  13291. (defun org-property-next-allowed-value (&optional previous)
  13292. "Switch to the next allowed value for this property."
  13293. (interactive)
  13294. (unless (org-at-property-p)
  13295. (error "Not at a property"))
  13296. (let* ((key (match-string 2))
  13297. (value (match-string 3))
  13298. (allowed (or (org-property-get-allowed-values (point) key)
  13299. (and (member value '("[ ]" "[-]" "[X]"))
  13300. '("[ ]" "[X]"))))
  13301. nval)
  13302. (unless allowed
  13303. (error "Allowed values for this property have not been defined"))
  13304. (if previous (setq allowed (reverse allowed)))
  13305. (if (member value allowed)
  13306. (setq nval (car (cdr (member value allowed)))))
  13307. (setq nval (or nval (car allowed)))
  13308. (if (equal nval value)
  13309. (error "Only one allowed value for this property"))
  13310. (org-at-property-p)
  13311. (replace-match (concat " :" key ": " nval) t t)
  13312. (org-indent-line-function)
  13313. (beginning-of-line 1)
  13314. (skip-chars-forward " \t")
  13315. (run-hook-with-args 'org-property-changed-functions key nval)))
  13316. (defun org-find-olp (path &optional this-buffer)
  13317. "Return a marker pointing to the entry at outline path OLP.
  13318. If anything goes wrong, throw an error.
  13319. You can wrap this call to catch the error like this:
  13320. (condition-case msg
  13321. (org-mobile-locate-entry (match-string 4))
  13322. (error (nth 1 msg)))
  13323. The return value will then be either a string with the error message,
  13324. or a marker if everything is OK.
  13325. If THIS-BUFFER is set, the outline path does not contain a file,
  13326. only headings."
  13327. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13328. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13329. (level 1)
  13330. (lmin 1)
  13331. (lmax 1)
  13332. limit re end found pos heading cnt flevel)
  13333. (unless buffer (error "File not found :%s" file))
  13334. (with-current-buffer buffer
  13335. (save-excursion
  13336. (save-restriction
  13337. (widen)
  13338. (setq limit (point-max))
  13339. (goto-char (point-min))
  13340. (while (setq heading (pop path))
  13341. (setq re (format org-complex-heading-regexp-format
  13342. (regexp-quote heading)))
  13343. (setq cnt 0 pos (point))
  13344. (while (re-search-forward re end t)
  13345. (setq level (- (match-end 1) (match-beginning 1)))
  13346. (if (and (>= level lmin) (<= level lmax))
  13347. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13348. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13349. lmax heading))
  13350. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13351. lmax heading))
  13352. (goto-char found)
  13353. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13354. (setq end (save-excursion (org-end-of-subtree t t))))
  13355. (when (org-on-heading-p)
  13356. (move-marker (make-marker) (point))))))))
  13357. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13358. "Find node HEADING in BUFFER.
  13359. Return a marker to the heading if it was found, or nil if not.
  13360. If POS-ONLY is set, return just the position instead of a marker.
  13361. The heading text must match exact, but it may have a TODO keyword,
  13362. a priority cookie and tags in the standard locations."
  13363. (with-current-buffer (or buffer (current-buffer))
  13364. (save-excursion
  13365. (save-restriction
  13366. (widen)
  13367. (goto-char (point-min))
  13368. (let (case-fold-search)
  13369. (if (re-search-forward
  13370. (format org-complex-heading-regexp-format
  13371. (regexp-quote heading)) nil t)
  13372. (if pos-only
  13373. (match-beginning 0)
  13374. (move-marker (make-marker) (match-beginning 0)))))))))
  13375. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13376. "Find Org node headline HEADING in all .org files in directory DIR.
  13377. When the target headline is found, return a marker to this location."
  13378. (let ((files (directory-files (or dir default-directory)
  13379. nil "\\`[^.#].*\\.org\\'"))
  13380. file visiting m buffer)
  13381. (catch 'found
  13382. (while (setq file (pop files))
  13383. (message "trying %s" file)
  13384. (setq visiting (org-find-base-buffer-visiting file))
  13385. (setq buffer (or visiting (find-file-noselect file)))
  13386. (setq m (org-find-exact-headline-in-buffer
  13387. heading buffer))
  13388. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13389. (and m (throw 'found m))))))
  13390. (defun org-find-entry-with-id (ident)
  13391. "Locate the entry that contains the ID property with exact value IDENT.
  13392. IDENT can be a string, a symbol or a number, this function will search for
  13393. the string representation of it.
  13394. Return the position where this entry starts, or nil if there is no such entry."
  13395. (interactive "sID: ")
  13396. (let ((id (cond
  13397. ((stringp ident) ident)
  13398. ((symbol-name ident) (symbol-name ident))
  13399. ((numberp ident) (number-to-string ident))
  13400. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13401. (case-fold-search nil))
  13402. (save-excursion
  13403. (save-restriction
  13404. (widen)
  13405. (goto-char (point-min))
  13406. (when (re-search-forward
  13407. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13408. nil t)
  13409. (org-back-to-heading t)
  13410. (point))))))
  13411. ;;;; Timestamps
  13412. (defvar org-last-changed-timestamp nil)
  13413. (defvar org-last-inserted-timestamp nil
  13414. "The last time stamp inserted with `org-insert-time-stamp'.")
  13415. (defvar org-time-was-given) ; dynamically scoped parameter
  13416. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13417. (defvar org-ts-what) ; dynamically scoped parameter
  13418. (defun org-time-stamp (arg &optional inactive)
  13419. "Prompt for a date/time and insert a time stamp.
  13420. If the user specifies a time like HH:MM, or if this command is called
  13421. with a prefix argument, the time stamp will contain date and time.
  13422. Otherwise, only the date will be included. All parts of a date not
  13423. specified by the user will be filled in from the current date/time.
  13424. So if you press just return without typing anything, the time stamp
  13425. will represent the current date/time. If there is already a timestamp
  13426. at the cursor, it will be modified."
  13427. (interactive "P")
  13428. (let* ((ts nil)
  13429. (default-time
  13430. ;; Default time is either today, or, when entering a range,
  13431. ;; the range start.
  13432. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13433. (save-excursion
  13434. (re-search-backward
  13435. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13436. (- (point) 20) t)))
  13437. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13438. (current-time)))
  13439. (default-input (and ts (org-get-compact-tod ts)))
  13440. (repeater (save-excursion
  13441. (save-match-data
  13442. (beginning-of-line)
  13443. (when (re-search-forward
  13444. "\\([.+-]+[0-9]+[dwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  13445. (save-excursion (progn (end-of-line) (point))) t)
  13446. (match-string 0)))))
  13447. org-time-was-given org-end-time-was-given time)
  13448. (cond
  13449. ((and (org-at-timestamp-p t)
  13450. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13451. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13452. (insert "--")
  13453. (setq time (let ((this-command this-command))
  13454. (org-read-date arg 'totime nil nil
  13455. default-time default-input)))
  13456. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13457. ((org-at-timestamp-p t)
  13458. (setq time (let ((this-command this-command))
  13459. (org-read-date arg 'totime nil nil default-time default-input)))
  13460. (when (org-at-timestamp-p t) ; just to get the match data
  13461. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13462. (replace-match "")
  13463. (setq org-last-changed-timestamp
  13464. (org-insert-time-stamp
  13465. time (or org-time-was-given arg)
  13466. inactive nil nil (list org-end-time-was-given)))
  13467. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13468. (setq org-last-changed-timestamp
  13469. (concat (substring org-last-inserted-timestamp 0 -1)
  13470. " " repeater ">"))))
  13471. (message "Timestamp updated"))
  13472. (t
  13473. (setq time (let ((this-command this-command))
  13474. (org-read-date arg 'totime nil nil default-time default-input)))
  13475. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13476. nil nil (list org-end-time-was-given))))))
  13477. ;; FIXME: can we use this for something else, like computing time differences?
  13478. (defun org-get-compact-tod (s)
  13479. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13480. (let* ((t1 (match-string 1 s))
  13481. (h1 (string-to-number (match-string 2 s)))
  13482. (m1 (string-to-number (match-string 3 s)))
  13483. (t2 (and (match-end 4) (match-string 5 s)))
  13484. (h2 (and t2 (string-to-number (match-string 6 s))))
  13485. (m2 (and t2 (string-to-number (match-string 7 s))))
  13486. dh dm)
  13487. (if (not t2)
  13488. t1
  13489. (setq dh (- h2 h1) dm (- m2 m1))
  13490. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13491. (concat t1 "+" (number-to-string dh)
  13492. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13493. (defun org-time-stamp-inactive (&optional arg)
  13494. "Insert an inactive time stamp.
  13495. An inactive time stamp is enclosed in square brackets instead of angle
  13496. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13497. does not link to the calendar and cannot be changed with the S-cursor keys.
  13498. So these are more for recording a certain time/date."
  13499. (interactive "P")
  13500. (org-time-stamp arg 'inactive))
  13501. (defvar org-date-ovl (make-overlay 1 1))
  13502. (overlay-put org-date-ovl 'face 'org-warning)
  13503. (org-detach-overlay org-date-ovl)
  13504. (defvar org-ans1) ; dynamically scoped parameter
  13505. (defvar org-ans2) ; dynamically scoped parameter
  13506. (defvar org-plain-time-of-day-regexp) ; defined below
  13507. (defvar org-overriding-default-time nil) ; dynamically scoped
  13508. (defvar org-read-date-overlay nil)
  13509. (defvar org-dcst nil) ; dynamically scoped
  13510. (defvar org-read-date-history nil)
  13511. (defvar org-read-date-final-answer nil)
  13512. (defvar org-read-date-analyze-futurep nil)
  13513. (defvar org-read-date-analyze-forced-year nil)
  13514. (defun org-read-date (&optional with-time to-time from-string prompt
  13515. default-time default-input)
  13516. "Read a date, possibly a time, and make things smooth for the user.
  13517. The prompt will suggest to enter an ISO date, but you can also enter anything
  13518. which will at least partially be understood by `parse-time-string'.
  13519. Unrecognized parts of the date will default to the current day, month, year,
  13520. hour and minute. If this command is called to replace a timestamp at point,
  13521. or to enter the second timestamp of a range, the default time is taken
  13522. from the existing stamp. Furthermore, the command prefers the future,
  13523. so if you are giving a date where the year is not given, and the day-month
  13524. combination is already past in the current year, it will assume you
  13525. mean next year. For details, see the manual. A few examples:
  13526. 3-2-5 --> 2003-02-05
  13527. feb 15 --> currentyear-02-15
  13528. 2/15 --> currentyear-02-15
  13529. sep 12 9 --> 2009-09-12
  13530. 12:45 --> today 12:45
  13531. 22 sept 0:34 --> currentyear-09-22 0:34
  13532. 12 --> currentyear-currentmonth-12
  13533. Fri --> nearest Friday (today or later)
  13534. etc.
  13535. Furthermore you can specify a relative date by giving, as the *first* thing
  13536. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  13537. change in days weeks, months, years.
  13538. With a single plus or minus, the date is relative to today. With a double
  13539. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13540. +4d --> four days from today
  13541. +4 --> same as above
  13542. +2w --> two weeks from today
  13543. ++5 --> five days from default date
  13544. The function understands only English month and weekday abbreviations,
  13545. but this can be configured with the variables `parse-time-months' and
  13546. `parse-time-weekdays'.
  13547. While prompting, a calendar is popped up - you can also select the
  13548. date with the mouse (button 1). The calendar shows a period of three
  13549. months. To scroll it to other months, use the keys `>' and `<'.
  13550. If you don't like the calendar, turn it off with
  13551. \(setq org-read-date-popup-calendar nil)
  13552. With optional argument TO-TIME, the date will immediately be converted
  13553. to an internal time.
  13554. With an optional argument WITH-TIME, the prompt will suggest to also
  13555. insert a time. Note that when WITH-TIME is not set, you can still
  13556. enter a time, and this function will inform the calling routine about
  13557. this change. The calling routine may then choose to change the format
  13558. used to insert the time stamp into the buffer to include the time.
  13559. With optional argument FROM-STRING, read from this string instead from
  13560. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13561. the time/date that is used for everything that is not specified by the
  13562. user."
  13563. (require 'parse-time)
  13564. (let* ((org-time-stamp-rounding-minutes
  13565. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13566. (org-dcst org-display-custom-times)
  13567. (ct (org-current-time))
  13568. (def (or org-overriding-default-time default-time ct))
  13569. (defdecode (decode-time def))
  13570. (dummy (progn
  13571. (when (< (nth 2 defdecode) org-extend-today-until)
  13572. (setcar (nthcdr 2 defdecode) -1)
  13573. (setcar (nthcdr 1 defdecode) 59)
  13574. (setq def (apply 'encode-time defdecode)
  13575. defdecode (decode-time def)))))
  13576. (calendar-frame-setup nil)
  13577. (calendar-setup nil)
  13578. (calendar-move-hook nil)
  13579. (calendar-view-diary-initially-flag nil)
  13580. (calendar-view-holidays-initially-flag nil)
  13581. (timestr (format-time-string
  13582. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  13583. (prompt (concat (if prompt (concat prompt " ") "")
  13584. (format "Date+time [%s]: " timestr)))
  13585. ans (org-ans0 "") org-ans1 org-ans2 final)
  13586. (cond
  13587. (from-string (setq ans from-string))
  13588. (org-read-date-popup-calendar
  13589. (save-excursion
  13590. (save-window-excursion
  13591. (calendar)
  13592. (unwind-protect
  13593. (progn
  13594. (calendar-forward-day (- (time-to-days def)
  13595. (calendar-absolute-from-gregorian
  13596. (calendar-current-date))))
  13597. (org-eval-in-calendar nil t)
  13598. (let* ((old-map (current-local-map))
  13599. (map (copy-keymap calendar-mode-map))
  13600. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13601. (org-defkey map (kbd "RET") 'org-calendar-select)
  13602. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13603. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13604. (org-defkey minibuffer-local-map [(meta shift left)]
  13605. (lambda () (interactive)
  13606. (org-eval-in-calendar '(calendar-backward-month 1))))
  13607. (org-defkey minibuffer-local-map [(meta shift right)]
  13608. (lambda () (interactive)
  13609. (org-eval-in-calendar '(calendar-forward-month 1))))
  13610. (org-defkey minibuffer-local-map [(meta shift up)]
  13611. (lambda () (interactive)
  13612. (org-eval-in-calendar '(calendar-backward-year 1))))
  13613. (org-defkey minibuffer-local-map [(meta shift down)]
  13614. (lambda () (interactive)
  13615. (org-eval-in-calendar '(calendar-forward-year 1))))
  13616. (org-defkey minibuffer-local-map [?\e (shift left)]
  13617. (lambda () (interactive)
  13618. (org-eval-in-calendar '(calendar-backward-month 1))))
  13619. (org-defkey minibuffer-local-map [?\e (shift right)]
  13620. (lambda () (interactive)
  13621. (org-eval-in-calendar '(calendar-forward-month 1))))
  13622. (org-defkey minibuffer-local-map [?\e (shift up)]
  13623. (lambda () (interactive)
  13624. (org-eval-in-calendar '(calendar-backward-year 1))))
  13625. (org-defkey minibuffer-local-map [?\e (shift down)]
  13626. (lambda () (interactive)
  13627. (org-eval-in-calendar '(calendar-forward-year 1))))
  13628. (org-defkey minibuffer-local-map [(shift up)]
  13629. (lambda () (interactive)
  13630. (org-eval-in-calendar '(calendar-backward-week 1))))
  13631. (org-defkey minibuffer-local-map [(shift down)]
  13632. (lambda () (interactive)
  13633. (org-eval-in-calendar '(calendar-forward-week 1))))
  13634. (org-defkey minibuffer-local-map [(shift left)]
  13635. (lambda () (interactive)
  13636. (org-eval-in-calendar '(calendar-backward-day 1))))
  13637. (org-defkey minibuffer-local-map [(shift right)]
  13638. (lambda () (interactive)
  13639. (org-eval-in-calendar '(calendar-forward-day 1))))
  13640. (org-defkey minibuffer-local-map ">"
  13641. (lambda () (interactive)
  13642. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13643. (org-defkey minibuffer-local-map "<"
  13644. (lambda () (interactive)
  13645. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13646. (org-defkey minibuffer-local-map "\C-v"
  13647. (lambda () (interactive)
  13648. (org-eval-in-calendar
  13649. '(calendar-scroll-left-three-months 1))))
  13650. (org-defkey minibuffer-local-map "\M-v"
  13651. (lambda () (interactive)
  13652. (org-eval-in-calendar
  13653. '(calendar-scroll-right-three-months 1))))
  13654. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13655. (unwind-protect
  13656. (progn
  13657. (use-local-map map)
  13658. (add-hook 'post-command-hook 'org-read-date-display)
  13659. (setq org-ans0 (read-string prompt default-input
  13660. 'org-read-date-history nil))
  13661. ;; org-ans0: from prompt
  13662. ;; org-ans1: from mouse click
  13663. ;; org-ans2: from calendar motion
  13664. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13665. (remove-hook 'post-command-hook 'org-read-date-display)
  13666. (use-local-map old-map)
  13667. (when org-read-date-overlay
  13668. (delete-overlay org-read-date-overlay)
  13669. (setq org-read-date-overlay nil)))))
  13670. (bury-buffer "*Calendar*")))))
  13671. (t ; Naked prompt only
  13672. (unwind-protect
  13673. (setq ans (read-string prompt default-input
  13674. 'org-read-date-history timestr))
  13675. (when org-read-date-overlay
  13676. (delete-overlay org-read-date-overlay)
  13677. (setq org-read-date-overlay nil)))))
  13678. (setq final (org-read-date-analyze ans def defdecode))
  13679. (when org-read-date-analyze-forced-year
  13680. (message "Year was forced into %s"
  13681. (if org-read-date-force-compatible-dates
  13682. "compatible range (1970-2037)"
  13683. "range representable on this machine"))
  13684. (ding))
  13685. ;; One round trip to get rid of 34th of August and stuff like that....
  13686. (setq final (decode-time (apply 'encode-time final)))
  13687. (setq org-read-date-final-answer ans)
  13688. (if to-time
  13689. (apply 'encode-time final)
  13690. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13691. (format "%04d-%02d-%02d %02d:%02d"
  13692. (nth 5 final) (nth 4 final) (nth 3 final)
  13693. (nth 2 final) (nth 1 final))
  13694. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13695. (defvar def)
  13696. (defvar defdecode)
  13697. (defvar with-time)
  13698. (defun org-read-date-display ()
  13699. "Display the current date prompt interpretation in the minibuffer."
  13700. (when org-read-date-display-live
  13701. (when org-read-date-overlay
  13702. (delete-overlay org-read-date-overlay))
  13703. (let ((p (point)))
  13704. (end-of-line 1)
  13705. (while (not (equal (buffer-substring
  13706. (max (point-min) (- (point) 4)) (point))
  13707. " "))
  13708. (insert " "))
  13709. (goto-char p))
  13710. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13711. " " (or org-ans1 org-ans2)))
  13712. (org-end-time-was-given nil)
  13713. (f (org-read-date-analyze ans def defdecode))
  13714. (fmts (if org-dcst
  13715. org-time-stamp-custom-formats
  13716. org-time-stamp-formats))
  13717. (fmt (if (or with-time
  13718. (and (boundp 'org-time-was-given) org-time-was-given))
  13719. (cdr fmts)
  13720. (car fmts)))
  13721. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  13722. (when (and org-end-time-was-given
  13723. (string-match org-plain-time-of-day-regexp txt))
  13724. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13725. org-end-time-was-given
  13726. (substring txt (match-end 0)))))
  13727. (when org-read-date-analyze-futurep
  13728. (setq txt (concat txt " (=>F)")))
  13729. (setq org-read-date-overlay
  13730. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13731. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13732. (defun org-read-date-analyze (ans def defdecode)
  13733. "Analyze the combined answer of the date prompt."
  13734. ;; FIXME: cleanup and comment
  13735. (let ((nowdecode (decode-time (current-time)))
  13736. delta deltan deltaw deltadef year month day
  13737. hour minute second wday pm h2 m2 tl wday1
  13738. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13739. (setq org-read-date-analyze-futurep nil
  13740. org-read-date-analyze-forced-year nil)
  13741. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13742. (setq ans "+0"))
  13743. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13744. (setq ans (replace-match "" t t ans)
  13745. deltan (car delta)
  13746. deltaw (nth 1 delta)
  13747. deltadef (nth 2 delta)))
  13748. ;; Check if there is an iso week date in there
  13749. ;; If yes, store the info and postpone interpreting it until the rest
  13750. ;; of the parsing is done
  13751. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13752. (setq iso-year (if (match-end 1)
  13753. (org-small-year-to-year
  13754. (string-to-number (match-string 1 ans))))
  13755. iso-weekday (if (match-end 3)
  13756. (string-to-number (match-string 3 ans)))
  13757. iso-week (string-to-number (match-string 2 ans)))
  13758. (setq ans (replace-match "" t t ans)))
  13759. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13760. (when (string-match
  13761. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13762. (setq year (if (match-end 2)
  13763. (string-to-number (match-string 2 ans))
  13764. (progn (setq kill-year t)
  13765. (string-to-number (format-time-string "%Y"))))
  13766. month (string-to-number (match-string 3 ans))
  13767. day (string-to-number (match-string 4 ans)))
  13768. (if (< year 100) (setq year (+ 2000 year)))
  13769. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13770. t nil ans)))
  13771. ;; Help matching dottet european dates
  13772. (when (string-match
  13773. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  13774. (setq year (if (match-end 3)
  13775. (string-to-number (match-string 3 ans))
  13776. (progn (setq kill-year t)
  13777. (string-to-number (format-time-string "%Y"))))
  13778. day (string-to-number (match-string 1 ans))
  13779. month (string-to-number (match-string 2 ans))
  13780. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13781. t nil ans)))
  13782. ;; Help matching american dates, like 5/30 or 5/30/7
  13783. (when (string-match
  13784. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13785. (setq year (if (match-end 4)
  13786. (string-to-number (match-string 4 ans))
  13787. (progn (setq kill-year t)
  13788. (string-to-number (format-time-string "%Y"))))
  13789. month (string-to-number (match-string 1 ans))
  13790. day (string-to-number (match-string 2 ans)))
  13791. (if (< year 100) (setq year (+ 2000 year)))
  13792. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13793. t nil ans)))
  13794. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13795. ;; If there is a time with am/pm, and *no* time without it, we convert
  13796. ;; so that matching will be successful.
  13797. (loop for i from 1 to 2 do ; twice, for end time as well
  13798. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13799. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13800. (setq hour (string-to-number (match-string 1 ans))
  13801. minute (if (match-end 3)
  13802. (string-to-number (match-string 3 ans))
  13803. 0)
  13804. pm (equal ?p
  13805. (string-to-char (downcase (match-string 4 ans)))))
  13806. (if (and (= hour 12) (not pm))
  13807. (setq hour 0)
  13808. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13809. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13810. t t ans))))
  13811. ;; Check if a time range is given as a duration
  13812. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13813. (setq hour (string-to-number (match-string 1 ans))
  13814. h2 (+ hour (string-to-number (match-string 3 ans)))
  13815. minute (string-to-number (match-string 2 ans))
  13816. m2 (+ minute (if (match-end 5) (string-to-number
  13817. (match-string 5 ans))0)))
  13818. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  13819. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  13820. t t ans)))
  13821. ;; Check if there is a time range
  13822. (when (boundp 'org-end-time-was-given)
  13823. (setq org-time-was-given nil)
  13824. (when (and (string-match org-plain-time-of-day-regexp ans)
  13825. (match-end 8))
  13826. (setq org-end-time-was-given (match-string 8 ans))
  13827. (setq ans (concat (substring ans 0 (match-beginning 7))
  13828. (substring ans (match-end 7))))))
  13829. (setq tl (parse-time-string ans)
  13830. day (or (nth 3 tl) (nth 3 defdecode))
  13831. month (or (nth 4 tl)
  13832. (if (and org-read-date-prefer-future
  13833. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  13834. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  13835. (nth 4 defdecode)))
  13836. year (or (and (not kill-year) (nth 5 tl))
  13837. (if (and org-read-date-prefer-future
  13838. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  13839. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  13840. (nth 5 defdecode)))
  13841. hour (or (nth 2 tl) (nth 2 defdecode))
  13842. minute (or (nth 1 tl) (nth 1 defdecode))
  13843. second (or (nth 0 tl) 0)
  13844. wday (nth 6 tl))
  13845. (when (and (eq org-read-date-prefer-future 'time)
  13846. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  13847. (equal day (nth 3 nowdecode))
  13848. (equal month (nth 4 nowdecode))
  13849. (equal year (nth 5 nowdecode))
  13850. (nth 2 tl)
  13851. (or (< (nth 2 tl) (nth 2 nowdecode))
  13852. (and (= (nth 2 tl) (nth 2 nowdecode))
  13853. (nth 1 tl)
  13854. (< (nth 1 tl) (nth 1 nowdecode)))))
  13855. (setq day (1+ day)
  13856. futurep t))
  13857. ;; Special date definitions below
  13858. (cond
  13859. (iso-week
  13860. ;; There was an iso week
  13861. (require 'cal-iso)
  13862. (setq futurep nil)
  13863. (setq year (or iso-year year)
  13864. day (or iso-weekday wday 1)
  13865. wday nil ; to make sure that the trigger below does not match
  13866. iso-date (calendar-gregorian-from-absolute
  13867. (calendar-absolute-from-iso
  13868. (list iso-week day year))))
  13869. ; FIXME: Should we also push ISO weeks into the future?
  13870. ; (when (and org-read-date-prefer-future
  13871. ; (not iso-year)
  13872. ; (< (calendar-absolute-from-gregorian iso-date)
  13873. ; (time-to-days (current-time))))
  13874. ; (setq year (1+ year)
  13875. ; iso-date (calendar-gregorian-from-absolute
  13876. ; (calendar-absolute-from-iso
  13877. ; (list iso-week day year)))))
  13878. (setq month (car iso-date)
  13879. year (nth 2 iso-date)
  13880. day (nth 1 iso-date)))
  13881. (deltan
  13882. (setq futurep nil)
  13883. (unless deltadef
  13884. (let ((now (decode-time (current-time))))
  13885. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  13886. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  13887. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  13888. ((equal deltaw "m") (setq month (+ month deltan)))
  13889. ((equal deltaw "y") (setq year (+ year deltan)))))
  13890. ((and wday (not (nth 3 tl)))
  13891. (setq futurep nil)
  13892. ;; Weekday was given, but no day, so pick that day in the week
  13893. ;; on or after the derived date.
  13894. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  13895. (unless (equal wday wday1)
  13896. (setq day (+ day (% (- wday wday1 -7) 7))))))
  13897. (if (and (boundp 'org-time-was-given)
  13898. (nth 2 tl))
  13899. (setq org-time-was-given t))
  13900. (if (< year 100) (setq year (+ 2000 year)))
  13901. ;; Check of the date is representable
  13902. (if org-read-date-force-compatible-dates
  13903. (progn
  13904. (if (< year 1970)
  13905. (setq year 1970 org-read-date-analyze-forced-year t))
  13906. (if (> year 2037)
  13907. (setq year 2037 org-read-date-analyze-forced-year t)))
  13908. (condition-case nil
  13909. (ignore (encode-time second minute hour day month year))
  13910. (error
  13911. (setq year (nth 5 defdecode))
  13912. (setq org-read-date-analyze-forced-year t))))
  13913. (setq org-read-date-analyze-futurep futurep)
  13914. (list second minute hour day month year)))
  13915. (defvar parse-time-weekdays)
  13916. (defun org-read-date-get-relative (s today default)
  13917. "Check string S for special relative date string.
  13918. TODAY and DEFAULT are internal times, for today and for a default.
  13919. Return shift list (N what def-flag)
  13920. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  13921. N is the number of WHATs to shift.
  13922. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  13923. the DEFAULT date rather than TODAY."
  13924. (require 'parse-time)
  13925. (when (and
  13926. (string-match
  13927. (concat
  13928. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  13929. "\\([0-9]+\\)?"
  13930. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  13931. "\\([ \t]\\|$\\)") s)
  13932. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  13933. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  13934. (string-to-char (substring (match-string 1 s) -1))
  13935. ?+))
  13936. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  13937. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  13938. (what (if (match-end 3) (match-string 3 s) "d"))
  13939. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  13940. (date (if rel default today))
  13941. (wday (nth 6 (decode-time date)))
  13942. delta)
  13943. (if wday1
  13944. (progn
  13945. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  13946. (if (= dir ?-) (setq delta (- delta 7)))
  13947. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  13948. (list delta "d" rel))
  13949. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  13950. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  13951. "Turn a user-specified date into the internal representation.
  13952. The internal representation needed by the calendar is (month day year).
  13953. This is a wrapper to handle the brain-dead convention in calendar that
  13954. user function argument order change dependent on argument order."
  13955. (if (boundp 'calendar-date-style)
  13956. (cond
  13957. ((eq calendar-date-style 'american)
  13958. (list arg1 arg2 arg3))
  13959. ((eq calendar-date-style 'european)
  13960. (list arg2 arg1 arg3))
  13961. ((eq calendar-date-style 'iso)
  13962. (list arg2 arg3 arg1)))
  13963. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  13964. (if (org-bound-and-true-p european-calendar-style)
  13965. (list arg2 arg1 arg3)
  13966. (list arg1 arg2 arg3)))))
  13967. (defun org-eval-in-calendar (form &optional keepdate)
  13968. "Eval FORM in the calendar window and return to current window.
  13969. Also, store the cursor date in variable org-ans2."
  13970. (let ((sf (selected-frame))
  13971. (sw (selected-window)))
  13972. (select-window (get-buffer-window "*Calendar*" t))
  13973. (eval form)
  13974. (when (and (not keepdate) (calendar-cursor-to-date))
  13975. (let* ((date (calendar-cursor-to-date))
  13976. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13977. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  13978. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  13979. (select-window sw)
  13980. (org-select-frame-set-input-focus sf)))
  13981. (defun org-calendar-select ()
  13982. "Return to `org-read-date' with the date currently selected.
  13983. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13984. (interactive)
  13985. (when (calendar-cursor-to-date)
  13986. (let* ((date (calendar-cursor-to-date))
  13987. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13988. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13989. (if (active-minibuffer-window) (exit-minibuffer))))
  13990. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  13991. "Insert a date stamp for the date given by the internal TIME.
  13992. WITH-HM means use the stamp format that includes the time of the day.
  13993. INACTIVE means use square brackets instead of angular ones, so that the
  13994. stamp will not contribute to the agenda.
  13995. PRE and POST are optional strings to be inserted before and after the
  13996. stamp.
  13997. The command returns the inserted time stamp."
  13998. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  13999. stamp)
  14000. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14001. (insert-before-markers (or pre ""))
  14002. (when (listp extra)
  14003. (setq extra (car extra))
  14004. (if (and (stringp extra)
  14005. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14006. (setq extra (format "-%02d:%02d"
  14007. (string-to-number (match-string 1 extra))
  14008. (string-to-number (match-string 2 extra))))
  14009. (setq extra nil)))
  14010. (when extra
  14011. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14012. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14013. (insert-before-markers (or post ""))
  14014. (setq org-last-inserted-timestamp stamp)))
  14015. (defun org-toggle-time-stamp-overlays ()
  14016. "Toggle the use of custom time stamp formats."
  14017. (interactive)
  14018. (setq org-display-custom-times (not org-display-custom-times))
  14019. (unless org-display-custom-times
  14020. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14021. (while (setq p (next-single-property-change p 'display))
  14022. (if (and (get-text-property p 'display)
  14023. (eq (get-text-property p 'face) 'org-date))
  14024. (remove-text-properties
  14025. p (setq p (next-single-property-change p 'display))
  14026. '(display t))))
  14027. (set-buffer-modified-p bmp)))
  14028. (if (featurep 'xemacs)
  14029. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14030. (org-restart-font-lock)
  14031. (setq org-table-may-need-update t)
  14032. (if org-display-custom-times
  14033. (message "Time stamps are overlaid with custom format")
  14034. (message "Time stamp overlays removed")))
  14035. (defun org-display-custom-time (beg end)
  14036. "Overlay modified time stamp format over timestamp between BEG and END."
  14037. (let* ((ts (buffer-substring beg end))
  14038. t1 w1 with-hm tf time str w2 (off 0))
  14039. (save-match-data
  14040. (setq t1 (org-parse-time-string ts t))
  14041. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  14042. (setq off (- (match-end 0) (match-beginning 0)))))
  14043. (setq end (- end off))
  14044. (setq w1 (- end beg)
  14045. with-hm (and (nth 1 t1) (nth 2 t1))
  14046. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14047. time (org-fix-decoded-time t1)
  14048. str (org-add-props
  14049. (format-time-string
  14050. (substring tf 1 -1) (apply 'encode-time time))
  14051. nil 'mouse-face 'highlight)
  14052. w2 (length str))
  14053. (if (not (= w2 w1))
  14054. (add-text-properties (1+ beg) (+ 2 beg)
  14055. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14056. (if (featurep 'xemacs)
  14057. (progn
  14058. (put-text-property beg end 'invisible t)
  14059. (put-text-property beg end 'end-glyph (make-glyph str)))
  14060. (put-text-property beg end 'display str))))
  14061. (defun org-translate-time (string)
  14062. "Translate all timestamps in STRING to custom format.
  14063. But do this only if the variable `org-display-custom-times' is set."
  14064. (when org-display-custom-times
  14065. (save-match-data
  14066. (let* ((start 0)
  14067. (re org-ts-regexp-both)
  14068. t1 with-hm inactive tf time str beg end)
  14069. (while (setq start (string-match re string start))
  14070. (setq beg (match-beginning 0)
  14071. end (match-end 0)
  14072. t1 (save-match-data
  14073. (org-parse-time-string (substring string beg end) t))
  14074. with-hm (and (nth 1 t1) (nth 2 t1))
  14075. inactive (equal (substring string beg (1+ beg)) "[")
  14076. tf (funcall (if with-hm 'cdr 'car)
  14077. org-time-stamp-custom-formats)
  14078. time (org-fix-decoded-time t1)
  14079. str (format-time-string
  14080. (concat
  14081. (if inactive "[" "<") (substring tf 1 -1)
  14082. (if inactive "]" ">"))
  14083. (apply 'encode-time time))
  14084. string (replace-match str t t string)
  14085. start (+ start (length str)))))))
  14086. string)
  14087. (defun org-fix-decoded-time (time)
  14088. "Set 0 instead of nil for the first 6 elements of time.
  14089. Don't touch the rest."
  14090. (let ((n 0))
  14091. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14092. (defun org-days-to-time (timestamp-string)
  14093. "Difference between TIMESTAMP-STRING and now in days."
  14094. (- (time-to-days (org-time-string-to-time timestamp-string))
  14095. (time-to-days (current-time))))
  14096. (defun org-deadline-close (timestamp-string &optional ndays)
  14097. "Is the time in TIMESTAMP-STRING close to the current date?"
  14098. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14099. (and (< (org-days-to-time timestamp-string) ndays)
  14100. (not (org-entry-is-done-p))))
  14101. (defun org-get-wdays (ts)
  14102. "Get the deadline lead time appropriate for timestring TS."
  14103. (cond
  14104. ((<= org-deadline-warning-days 0)
  14105. ;; 0 or negative, enforce this value no matter what
  14106. (- org-deadline-warning-days))
  14107. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14108. ;; lead time is specified.
  14109. (floor (* (string-to-number (match-string 1 ts))
  14110. (cdr (assoc (match-string 2 ts)
  14111. '(("d" . 1) ("w" . 7)
  14112. ("m" . 30.4) ("y" . 365.25)))))))
  14113. ;; go for the default.
  14114. (t org-deadline-warning-days)))
  14115. (defun org-calendar-select-mouse (ev)
  14116. "Return to `org-read-date' with the date currently selected.
  14117. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14118. (interactive "e")
  14119. (mouse-set-point ev)
  14120. (when (calendar-cursor-to-date)
  14121. (let* ((date (calendar-cursor-to-date))
  14122. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14123. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14124. (if (active-minibuffer-window) (exit-minibuffer))))
  14125. (defun org-check-deadlines (ndays)
  14126. "Check if there are any deadlines due or past due.
  14127. A deadline is considered due if it happens within `org-deadline-warning-days'
  14128. days from today's date. If the deadline appears in an entry marked DONE,
  14129. it is not shown. The prefix arg NDAYS can be used to test that many
  14130. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14131. (interactive "P")
  14132. (let* ((org-warn-days
  14133. (cond
  14134. ((equal ndays '(4)) 100000)
  14135. (ndays (prefix-numeric-value ndays))
  14136. (t (abs org-deadline-warning-days))))
  14137. (case-fold-search nil)
  14138. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14139. (callback
  14140. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14141. (message "%d deadlines past-due or due within %d days"
  14142. (org-occur regexp nil callback)
  14143. org-warn-days)))
  14144. (defun org-check-before-date (date)
  14145. "Check if there are deadlines or scheduled entries before DATE."
  14146. (interactive (list (org-read-date)))
  14147. (let ((case-fold-search nil)
  14148. (regexp (concat "\\<\\(" org-deadline-string
  14149. "\\|" org-scheduled-string
  14150. "\\) *<\\([^>]+\\)>"))
  14151. (callback
  14152. (lambda () (time-less-p
  14153. (org-time-string-to-time (match-string 2))
  14154. (org-time-string-to-time date)))))
  14155. (message "%d entries before %s"
  14156. (org-occur regexp nil callback) date)))
  14157. (defun org-check-after-date (date)
  14158. "Check if there are deadlines or scheduled entries after DATE."
  14159. (interactive (list (org-read-date)))
  14160. (let ((case-fold-search nil)
  14161. (regexp (concat "\\<\\(" org-deadline-string
  14162. "\\|" org-scheduled-string
  14163. "\\) *<\\([^>]+\\)>"))
  14164. (callback
  14165. (lambda () (not
  14166. (time-less-p
  14167. (org-time-string-to-time (match-string 2))
  14168. (org-time-string-to-time date))))))
  14169. (message "%d entries after %s"
  14170. (org-occur regexp nil callback) date)))
  14171. (defun org-evaluate-time-range (&optional to-buffer)
  14172. "Evaluate a time range by computing the difference between start and end.
  14173. Normally the result is just printed in the echo area, but with prefix arg
  14174. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14175. If the time range is actually in a table, the result is inserted into the
  14176. next column.
  14177. For time difference computation, a year is assumed to be exactly 365
  14178. days in order to avoid rounding problems."
  14179. (interactive "P")
  14180. (or
  14181. (org-clock-update-time-maybe)
  14182. (save-excursion
  14183. (unless (org-at-date-range-p t)
  14184. (goto-char (point-at-bol))
  14185. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14186. (if (not (org-at-date-range-p t))
  14187. (error "Not at a time-stamp range, and none found in current line")))
  14188. (let* ((ts1 (match-string 1))
  14189. (ts2 (match-string 2))
  14190. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14191. (match-end (match-end 0))
  14192. (time1 (org-time-string-to-time ts1))
  14193. (time2 (org-time-string-to-time ts2))
  14194. (t1 (org-float-time time1))
  14195. (t2 (org-float-time time2))
  14196. (diff (abs (- t2 t1)))
  14197. (negative (< (- t2 t1) 0))
  14198. ;; (ys (floor (* 365 24 60 60)))
  14199. (ds (* 24 60 60))
  14200. (hs (* 60 60))
  14201. (fy "%dy %dd %02d:%02d")
  14202. (fy1 "%dy %dd")
  14203. (fd "%dd %02d:%02d")
  14204. (fd1 "%dd")
  14205. (fh "%02d:%02d")
  14206. y d h m align)
  14207. (if havetime
  14208. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14209. y 0
  14210. d (floor (/ diff ds)) diff (mod diff ds)
  14211. h (floor (/ diff hs)) diff (mod diff hs)
  14212. m (floor (/ diff 60)))
  14213. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14214. y 0
  14215. d (floor (+ (/ diff ds) 0.5))
  14216. h 0 m 0))
  14217. (if (not to-buffer)
  14218. (message "%s" (org-make-tdiff-string y d h m))
  14219. (if (org-at-table-p)
  14220. (progn
  14221. (goto-char match-end)
  14222. (setq align t)
  14223. (and (looking-at " *|") (goto-char (match-end 0))))
  14224. (goto-char match-end))
  14225. (if (looking-at
  14226. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14227. (replace-match ""))
  14228. (if negative (insert " -"))
  14229. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14230. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14231. (insert " " (format fh h m))))
  14232. (if align (org-table-align))
  14233. (message "Time difference inserted")))))
  14234. (defun org-make-tdiff-string (y d h m)
  14235. (let ((fmt "")
  14236. (l nil))
  14237. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14238. l (push y l)))
  14239. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14240. l (push d l)))
  14241. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14242. l (push h l)))
  14243. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14244. l (push m l)))
  14245. (apply 'format fmt (nreverse l))))
  14246. (defun org-time-string-to-time (s &optional buffer pos)
  14247. (condition-case errdata
  14248. (apply 'encode-time (org-parse-time-string s))
  14249. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14250. s (if (not (and buffer pos))
  14251. ""
  14252. (format " at %d in buffer `%s'" pos buffer))
  14253. (cdr errdata)))))
  14254. (defun org-time-string-to-seconds (s)
  14255. (org-float-time (org-time-string-to-time s)))
  14256. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14257. "Convert a time stamp to an absolute day number.
  14258. If there is a specifier for a cyclic time stamp, get the closest date to
  14259. DAYNR.
  14260. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14261. The variable date is bound by the calendar when this is called."
  14262. (cond
  14263. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14264. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14265. daynr
  14266. (+ daynr 1000)))
  14267. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  14268. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14269. (time-to-days (current-time))) (match-string 0 s)
  14270. prefer show-all))
  14271. (t (time-to-days
  14272. (condition-case errdata
  14273. (apply 'encode-time (org-parse-time-string s))
  14274. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14275. s (if (not (and buffer pos))
  14276. ""
  14277. (format " at %d in buffer `%s'" pos buffer))
  14278. (cdr errdata))))))))
  14279. (defun org-days-to-iso-week (days)
  14280. "Return the iso week number."
  14281. (require 'cal-iso)
  14282. (car (calendar-iso-from-absolute days)))
  14283. (defun org-small-year-to-year (year)
  14284. "Convert 2-digit years into 4-digit years.
  14285. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14286. The year 2000 cannot be abbreviated. Any year larger than 99
  14287. is returned unchanged."
  14288. (if (< year 38)
  14289. (setq year (+ 2000 year))
  14290. (if (< year 100)
  14291. (setq year (+ 1900 year))))
  14292. year)
  14293. (defun org-time-from-absolute (d)
  14294. "Return the time corresponding to date D.
  14295. D may be an absolute day number, or a calendar-type list (month day year)."
  14296. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14297. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14298. (defun org-calendar-holiday ()
  14299. "List of holidays, for Diary display in Org-mode."
  14300. (require 'holidays)
  14301. (let ((hl (funcall
  14302. (if (fboundp 'calendar-check-holidays)
  14303. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14304. (if hl (mapconcat 'identity hl "; "))))
  14305. (defun org-diary-sexp-entry (sexp entry date)
  14306. "Process a SEXP diary ENTRY for DATE."
  14307. (require 'diary-lib)
  14308. (let ((result (if calendar-debug-sexp
  14309. (let ((stack-trace-on-error t))
  14310. (eval (car (read-from-string sexp))))
  14311. (condition-case nil
  14312. (eval (car (read-from-string sexp)))
  14313. (error
  14314. (beep)
  14315. (message "Bad sexp at line %d in %s: %s"
  14316. (org-current-line)
  14317. (buffer-file-name) sexp)
  14318. (sleep-for 2))))))
  14319. (cond ((stringp result) (split-string result "; "))
  14320. ((and (consp result)
  14321. (not (consp (cdr result)))
  14322. (stringp (cdr result))) (cdr result))
  14323. ((and (consp result)
  14324. (stringp (car result))) result)
  14325. (result entry)
  14326. (t nil))))
  14327. (defun org-diary-to-ical-string (frombuf)
  14328. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14329. This uses the icalendar.el library."
  14330. (let* ((tmpdir (if (featurep 'xemacs)
  14331. (temp-directory)
  14332. temporary-file-directory))
  14333. (tmpfile (make-temp-name
  14334. (expand-file-name "orgics" tmpdir)))
  14335. buf rtn b e)
  14336. (with-current-buffer frombuf
  14337. (icalendar-export-region (point-min) (point-max) tmpfile)
  14338. (setq buf (find-buffer-visiting tmpfile))
  14339. (set-buffer buf)
  14340. (goto-char (point-min))
  14341. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14342. (setq b (match-beginning 0)))
  14343. (goto-char (point-max))
  14344. (if (re-search-backward "^END:VEVENT" nil t)
  14345. (setq e (match-end 0)))
  14346. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14347. (kill-buffer buf)
  14348. (delete-file tmpfile)
  14349. rtn))
  14350. (defun org-closest-date (start current change prefer show-all)
  14351. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14352. When PREFER is `past', return a date that is either CURRENT or past.
  14353. When PREFER is `future', return a date that is either CURRENT or future.
  14354. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14355. ;; Make the proper lists from the dates
  14356. (catch 'exit
  14357. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14358. dn dw sday cday n1 n2 n0
  14359. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14360. (setq start (org-date-to-gregorian start)
  14361. current (org-date-to-gregorian
  14362. (if show-all
  14363. current
  14364. (time-to-days (current-time))))
  14365. sday (calendar-absolute-from-gregorian start)
  14366. cday (calendar-absolute-from-gregorian current))
  14367. (if (<= cday sday) (throw 'exit sday))
  14368. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  14369. (setq dn (string-to-number (match-string 1 change))
  14370. dw (cdr (assoc (match-string 2 change) a1)))
  14371. (error "Invalid change specifier: %s" change))
  14372. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14373. (cond
  14374. ((eq dw 'day)
  14375. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14376. n2 (+ n1 dn)))
  14377. ((eq dw 'year)
  14378. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14379. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14380. (setq date1 (list m d y1)
  14381. n1 (calendar-absolute-from-gregorian date1)
  14382. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14383. n2 (calendar-absolute-from-gregorian date2)))
  14384. ((eq dw 'month)
  14385. ;; approx number of month between the two dates
  14386. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14387. ;; How often does dn fit in there?
  14388. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14389. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14390. m (+ m nm)
  14391. ny (floor (/ m 12))
  14392. y (+ y ny)
  14393. m (- m (* ny 12)))
  14394. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14395. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14396. (setq m2 (+ m dn) y2 y)
  14397. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14398. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14399. (while (<= n2 cday)
  14400. (setq n1 n2 m m2 y y2)
  14401. (setq m2 (+ m dn) y2 y)
  14402. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14403. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14404. ;; Make sure n1 is the earlier date
  14405. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14406. (if show-all
  14407. (cond
  14408. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14409. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14410. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14411. (cond
  14412. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14413. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14414. (t (if (= cday n1) n1 n2)))))))
  14415. (defun org-date-to-gregorian (date)
  14416. "Turn any specification of DATE into a Gregorian date for the calendar."
  14417. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14418. ((and (listp date) (= (length date) 3)) date)
  14419. ((stringp date)
  14420. (setq date (org-parse-time-string date))
  14421. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14422. ((listp date)
  14423. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14424. (defun org-parse-time-string (s &optional nodefault)
  14425. "Parse the standard Org-mode time string.
  14426. This should be a lot faster than the normal `parse-time-string'.
  14427. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14428. hour and minute fields will be nil if not given."
  14429. (if (string-match org-ts-regexp0 s)
  14430. (list 0
  14431. (if (or (match-beginning 8) (not nodefault))
  14432. (string-to-number (or (match-string 8 s) "0")))
  14433. (if (or (match-beginning 7) (not nodefault))
  14434. (string-to-number (or (match-string 7 s) "0")))
  14435. (string-to-number (match-string 4 s))
  14436. (string-to-number (match-string 3 s))
  14437. (string-to-number (match-string 2 s))
  14438. nil nil nil)
  14439. (error "Not a standard Org-mode time string: %s" s)))
  14440. (defun org-timestamp-up (&optional arg)
  14441. "Increase the date item at the cursor by one.
  14442. If the cursor is on the year, change the year. If it is on the month,
  14443. the day or the time, change that.
  14444. With prefix ARG, change by that many units."
  14445. (interactive "p")
  14446. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14447. (defun org-timestamp-down (&optional arg)
  14448. "Decrease the date item at the cursor by one.
  14449. If the cursor is on the year, change the year. If it is on the month,
  14450. the day or the time, change that.
  14451. With prefix ARG, change by that many units."
  14452. (interactive "p")
  14453. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14454. (defun org-timestamp-up-day (&optional arg)
  14455. "Increase the date in the time stamp by one day.
  14456. With prefix ARG, change that many days."
  14457. (interactive "p")
  14458. (if (and (not (org-at-timestamp-p t))
  14459. (org-on-heading-p))
  14460. (org-todo 'up)
  14461. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14462. (defun org-timestamp-down-day (&optional arg)
  14463. "Decrease the date in the time stamp by one day.
  14464. With prefix ARG, change that many days."
  14465. (interactive "p")
  14466. (if (and (not (org-at-timestamp-p t))
  14467. (org-on-heading-p))
  14468. (org-todo 'down)
  14469. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14470. (defun org-at-timestamp-p (&optional inactive-ok)
  14471. "Determine if the cursor is in or at a timestamp."
  14472. (interactive)
  14473. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14474. (pos (point))
  14475. (ans (or (looking-at tsr)
  14476. (save-excursion
  14477. (skip-chars-backward "^[<\n\r\t")
  14478. (if (> (point) (point-min)) (backward-char 1))
  14479. (and (looking-at tsr)
  14480. (> (- (match-end 0) pos) -1))))))
  14481. (and ans
  14482. (boundp 'org-ts-what)
  14483. (setq org-ts-what
  14484. (cond
  14485. ((= pos (match-beginning 0)) 'bracket)
  14486. ;; Point is considered to be "on the bracket" whether
  14487. ;; it's really on it or right after it.
  14488. ((or (= pos (1- (match-end 0)))
  14489. (= pos (match-end 0))) 'bracket)
  14490. ((org-pos-in-match-range pos 2) 'year)
  14491. ((org-pos-in-match-range pos 3) 'month)
  14492. ((org-pos-in-match-range pos 7) 'hour)
  14493. ((org-pos-in-match-range pos 8) 'minute)
  14494. ((or (org-pos-in-match-range pos 4)
  14495. (org-pos-in-match-range pos 5)) 'day)
  14496. ((and (> pos (or (match-end 8) (match-end 5)))
  14497. (< pos (match-end 0)))
  14498. (- pos (or (match-end 8) (match-end 5))))
  14499. (t 'day))))
  14500. ans))
  14501. (defun org-toggle-timestamp-type ()
  14502. "Toggle the type (<active> or [inactive]) of a time stamp."
  14503. (interactive)
  14504. (when (org-at-timestamp-p t)
  14505. (let ((beg (match-beginning 0)) (end (match-end 0))
  14506. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14507. (save-excursion
  14508. (goto-char beg)
  14509. (while (re-search-forward "[][<>]" end t)
  14510. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14511. t t)))
  14512. (message "Timestamp is now %sactive"
  14513. (if (equal (char-after beg) ?<) "" "in")))))
  14514. (defun org-timestamp-change (n &optional what updown)
  14515. "Change the date in the time stamp at point.
  14516. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14517. `year', `minute', `second'. If WHAT is not given, the cursor position
  14518. in the timestamp determines what will be changed."
  14519. (let ((origin (point)) origin-cat
  14520. with-hm inactive
  14521. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  14522. org-ts-what
  14523. extra rem
  14524. ts time time0)
  14525. (if (not (org-at-timestamp-p t))
  14526. (error "Not at a timestamp"))
  14527. (if (and (not what) (eq org-ts-what 'bracket))
  14528. (org-toggle-timestamp-type)
  14529. ;; Point isn't on brackets. Remember the part of the time-stamp
  14530. ;; the point was in. Indeed, size of time-stamps may change,
  14531. ;; but point must be kept in the same category nonetheless.
  14532. (setq origin-cat org-ts-what)
  14533. (if (and (not what) (not (eq org-ts-what 'day))
  14534. org-display-custom-times
  14535. (get-text-property (point) 'display)
  14536. (not (get-text-property (1- (point)) 'display)))
  14537. (setq org-ts-what 'day))
  14538. (setq org-ts-what (or what org-ts-what)
  14539. inactive (= (char-after (match-beginning 0)) ?\[)
  14540. ts (match-string 0))
  14541. (replace-match "")
  14542. (if (string-match
  14543. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  14544. ts)
  14545. (setq extra (match-string 1 ts)))
  14546. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14547. (setq with-hm t))
  14548. (setq time0 (org-parse-time-string ts))
  14549. (when (and updown
  14550. (eq org-ts-what 'minute)
  14551. (not current-prefix-arg))
  14552. ;; This looks like s-up and s-down. Change by one rounding step.
  14553. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  14554. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  14555. (setcar (cdr time0) (+ (nth 1 time0)
  14556. (if (> n 0) (- rem) (- dm rem))))))
  14557. (setq time
  14558. (encode-time (or (car time0) 0)
  14559. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  14560. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  14561. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  14562. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  14563. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  14564. (nthcdr 6 time0)))
  14565. (when (and (member org-ts-what '(hour minute))
  14566. extra
  14567. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  14568. (setq extra (org-modify-ts-extra
  14569. extra
  14570. (if (eq org-ts-what 'hour) 2 5)
  14571. n dm)))
  14572. (when (integerp org-ts-what)
  14573. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  14574. (if (eq what 'calendar)
  14575. (let ((cal-date (org-get-date-from-calendar)))
  14576. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14577. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14578. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14579. (setcar time0 (or (car time0) 0))
  14580. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14581. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14582. (setq time (apply 'encode-time time0))))
  14583. ;; Insert the new time-stamp, and ensure point stays in the same
  14584. ;; category as before (i.e. not after the last position in that
  14585. ;; category).
  14586. (let ((pos (point)))
  14587. ;; Stay before inserted string. `save-excursion' is of no use.
  14588. (setq org-last-changed-timestamp
  14589. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14590. (goto-char pos))
  14591. (save-match-data
  14592. (looking-at org-ts-regexp3)
  14593. (goto-char (cond
  14594. ;; `day' category ends before `hour' if any, or at
  14595. ;; the end of the day name.
  14596. ((eq origin-cat 'day)
  14597. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  14598. ((eq origin-cat 'hour) (min (match-end 7) origin))
  14599. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  14600. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  14601. ;; `year' and `month' have both fixed size: point
  14602. ;; couldn't have moved into another part.
  14603. (t origin))))
  14604. ;; Update clock if on a CLOCK line.
  14605. (org-clock-update-time-maybe)
  14606. ;; Try to recenter the calendar window, if any.
  14607. (if (and org-calendar-follow-timestamp-change
  14608. (get-buffer-window "*Calendar*" t)
  14609. (memq org-ts-what '(day month year)))
  14610. (org-recenter-calendar (time-to-days time))))))
  14611. (defun org-modify-ts-extra (s pos n dm)
  14612. "Change the different parts of the lead-time and repeat fields in timestamp."
  14613. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14614. ng h m new rem)
  14615. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14616. (cond
  14617. ((or (org-pos-in-match-range pos 2)
  14618. (org-pos-in-match-range pos 3))
  14619. (setq m (string-to-number (match-string 3 s))
  14620. h (string-to-number (match-string 2 s)))
  14621. (if (org-pos-in-match-range pos 2)
  14622. (setq h (+ h n))
  14623. (setq n (* dm (org-no-warnings (signum n))))
  14624. (when (not (= 0 (setq rem (% m dm))))
  14625. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14626. (setq m (+ m n)))
  14627. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14628. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14629. (setq h (min 24 (max 0 h)))
  14630. (setq ng 1 new (format "-%02d:%02d" h m)))
  14631. ((org-pos-in-match-range pos 6)
  14632. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14633. ((org-pos-in-match-range pos 5)
  14634. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14635. ((org-pos-in-match-range pos 9)
  14636. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14637. ((org-pos-in-match-range pos 8)
  14638. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14639. (when ng
  14640. (setq s (concat
  14641. (substring s 0 (match-beginning ng))
  14642. new
  14643. (substring s (match-end ng))))))
  14644. s))
  14645. (defun org-recenter-calendar (date)
  14646. "If the calendar is visible, recenter it to DATE."
  14647. (let* ((win (selected-window))
  14648. (cwin (get-buffer-window "*Calendar*" t))
  14649. (calendar-move-hook nil))
  14650. (when cwin
  14651. (select-window cwin)
  14652. (calendar-goto-date (if (listp date) date
  14653. (calendar-gregorian-from-absolute date)))
  14654. (select-window win))))
  14655. (defun org-goto-calendar (&optional arg)
  14656. "Go to the Emacs calendar at the current date.
  14657. If there is a time stamp in the current line, go to that date.
  14658. A prefix ARG can be used to force the current date."
  14659. (interactive "P")
  14660. (let ((tsr org-ts-regexp) diff
  14661. (calendar-move-hook nil)
  14662. (calendar-view-holidays-initially-flag nil)
  14663. (calendar-view-diary-initially-flag nil))
  14664. (if (or (org-at-timestamp-p)
  14665. (save-excursion
  14666. (beginning-of-line 1)
  14667. (looking-at (concat ".*" tsr))))
  14668. (let ((d1 (time-to-days (current-time)))
  14669. (d2 (time-to-days
  14670. (org-time-string-to-time (match-string 1)))))
  14671. (setq diff (- d2 d1))))
  14672. (calendar)
  14673. (calendar-goto-today)
  14674. (if (and diff (not arg)) (calendar-forward-day diff))))
  14675. (defun org-get-date-from-calendar ()
  14676. "Return a list (month day year) of date at point in calendar."
  14677. (with-current-buffer "*Calendar*"
  14678. (save-match-data
  14679. (calendar-cursor-to-date))))
  14680. (defun org-date-from-calendar ()
  14681. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14682. If there is already a time stamp at the cursor position, update it."
  14683. (interactive)
  14684. (if (org-at-timestamp-p t)
  14685. (org-timestamp-change 0 'calendar)
  14686. (let ((cal-date (org-get-date-from-calendar)))
  14687. (org-insert-time-stamp
  14688. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14689. (defun org-minutes-to-hh:mm-string (m)
  14690. "Compute H:MM from a number of minutes."
  14691. (let ((h (/ m 60)))
  14692. (setq m (- m (* 60 h)))
  14693. (format org-time-clocksum-format h m)))
  14694. (defun org-hh:mm-string-to-minutes (s)
  14695. "Convert a string H:MM to a number of minutes.
  14696. If the string is just a number, interpret it as minutes.
  14697. In fact, the first hh:mm or number in the string will be taken,
  14698. there can be extra stuff in the string.
  14699. If no number is found, the return value is 0."
  14700. (cond
  14701. ((integerp s) s)
  14702. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14703. (+ (* (string-to-number (match-string 1 s)) 60)
  14704. (string-to-number (match-string 2 s))))
  14705. ((string-match "\\([0-9]+\\)" s)
  14706. (string-to-number (match-string 1 s)))
  14707. (t 0)))
  14708. (defcustom org-effort-durations
  14709. `(("h" . 60)
  14710. ("d" . ,(* 60 8))
  14711. ("w" . ,(* 60 8 5))
  14712. ("m" . ,(* 60 8 5 4))
  14713. ("y" . ,(* 60 8 5 40)))
  14714. "Conversion factor to minutes for an effort modifier.
  14715. Each entry has the form (MODIFIER . MINUTES).
  14716. In an effort string, a number followed by MODIFIER is multiplied
  14717. by the specified number of MINUTES to obtain an effort in
  14718. minutes.
  14719. For example, if the value of this variable is ((\"hours\" . 60)), then an
  14720. effort string \"2hours\" is equivalent to 120 minutes."
  14721. :group 'org-agenda
  14722. :type '(alist :key-type (string :tag "Modifier")
  14723. :value-type (number :tag "Minutes")))
  14724. (defun org-duration-string-to-minutes (s)
  14725. "Convert a duration string S to minutes.
  14726. A bare number is interpreted as minutes, modifiers can be set by
  14727. customizing `org-effort-durations' (which see).
  14728. Entries containing a colon are interpreted as H:MM by
  14729. `org-hh:mm-string-to-minutes'."
  14730. (let ((result 0)
  14731. (re (concat "\\([0-9]+\\) *\\("
  14732. (regexp-opt (mapcar 'car org-effort-durations))
  14733. "\\)")))
  14734. (while (string-match re s)
  14735. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  14736. (string-to-number (match-string 1 s))))
  14737. (setq s (replace-match "" nil t s)))
  14738. (incf result (org-hh:mm-string-to-minutes s))
  14739. result))
  14740. ;;;; Files
  14741. (defun org-save-all-org-buffers ()
  14742. "Save all Org-mode buffers without user confirmation."
  14743. (interactive)
  14744. (message "Saving all Org-mode buffers...")
  14745. (save-some-buffers t (lambda () (eq major-mode 'org-mode)))
  14746. (when (featurep 'org-id) (org-id-locations-save))
  14747. (message "Saving all Org-mode buffers... done"))
  14748. (defun org-revert-all-org-buffers ()
  14749. "Revert all Org-mode buffers.
  14750. Prompt for confirmation when there are unsaved changes.
  14751. Be sure you know what you are doing before letting this function
  14752. overwrite your changes.
  14753. This function is useful in a setup where one tracks org files
  14754. with a version control system, to revert on one machine after pulling
  14755. changes from another. I believe the procedure must be like this:
  14756. 1. M-x org-save-all-org-buffers
  14757. 2. Pull changes from the other machine, resolve conflicts
  14758. 3. M-x org-revert-all-org-buffers"
  14759. (interactive)
  14760. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14761. (error "Abort"))
  14762. (save-excursion
  14763. (save-window-excursion
  14764. (mapc
  14765. (lambda (b)
  14766. (when (and (with-current-buffer b (eq major-mode 'org-mode))
  14767. (with-current-buffer b buffer-file-name))
  14768. (org-pop-to-buffer-same-window b)
  14769. (revert-buffer t 'no-confirm)))
  14770. (buffer-list))
  14771. (when (and (featurep 'org-id) org-id-track-globally)
  14772. (org-id-locations-load)))))
  14773. ;;;; Agenda files
  14774. ;;;###autoload
  14775. (defun org-switchb (&optional arg)
  14776. "Switch between Org buffers.
  14777. With one prefix argument, restrict available buffers to files.
  14778. With two prefix arguments, restrict available buffers to agenda files.
  14779. Defaults to `iswitchb' for buffer name completion.
  14780. Set `org-completion-use-ido' to make it use ido instead."
  14781. (interactive "P")
  14782. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14783. ((equal arg '(16)) (org-buffer-list 'agenda))
  14784. (t (org-buffer-list))))
  14785. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14786. (org-completion-use-ido org-completion-use-ido))
  14787. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14788. (setq org-completion-use-iswitchb t))
  14789. (org-pop-to-buffer-same-window
  14790. (org-icompleting-read "Org buffer: "
  14791. (mapcar 'list (mapcar 'buffer-name blist))
  14792. nil t))))
  14793. ;;; Define some older names previously used for this functionality
  14794. ;;;###autoload
  14795. (defalias 'org-ido-switchb 'org-switchb)
  14796. ;;;###autoload
  14797. (defalias 'org-iswitchb 'org-switchb)
  14798. (defun org-buffer-list (&optional predicate exclude-tmp)
  14799. "Return a list of Org buffers.
  14800. PREDICATE can be `export', `files' or `agenda'.
  14801. export restrict the list to Export buffers.
  14802. files restrict the list to buffers visiting Org files.
  14803. agenda restrict the list to buffers visiting agenda files.
  14804. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  14805. (let* ((bfn nil)
  14806. (agenda-files (and (eq predicate 'agenda)
  14807. (mapcar 'file-truename (org-agenda-files t))))
  14808. (filter
  14809. (cond
  14810. ((eq predicate 'files)
  14811. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  14812. ((eq predicate 'export)
  14813. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  14814. ((eq predicate 'agenda)
  14815. (lambda (b)
  14816. (with-current-buffer b
  14817. (and (eq major-mode 'org-mode)
  14818. (setq bfn (buffer-file-name b))
  14819. (member (file-truename bfn) agenda-files)))))
  14820. (t (lambda (b) (with-current-buffer b
  14821. (or (eq major-mode 'org-mode)
  14822. (string-match "\*Org .*Export"
  14823. (buffer-name b)))))))))
  14824. (delq nil
  14825. (mapcar
  14826. (lambda(b)
  14827. (if (and (funcall filter b)
  14828. (or (not exclude-tmp)
  14829. (not (string-match "tmp" (buffer-name b)))))
  14830. b
  14831. nil))
  14832. (buffer-list)))))
  14833. (defun org-agenda-files (&optional unrestricted archives)
  14834. "Get the list of agenda files.
  14835. Optional UNRESTRICTED means return the full list even if a restriction
  14836. is currently in place.
  14837. When ARCHIVES is t, include all archive files that are really being
  14838. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  14839. `org-agenda-archives-mode' is t."
  14840. (let ((files
  14841. (cond
  14842. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  14843. ((stringp org-agenda-files) (org-read-agenda-file-list))
  14844. ((listp org-agenda-files) org-agenda-files)
  14845. (t (error "Invalid value of `org-agenda-files'")))))
  14846. (setq files (apply 'append
  14847. (mapcar (lambda (f)
  14848. (if (file-directory-p f)
  14849. (directory-files
  14850. f t org-agenda-file-regexp)
  14851. (list f)))
  14852. files)))
  14853. (when org-agenda-skip-unavailable-files
  14854. (setq files (delq nil
  14855. (mapcar (function
  14856. (lambda (file)
  14857. (and (file-readable-p file) file)))
  14858. files))))
  14859. (when (or (eq archives t)
  14860. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  14861. (setq files (org-add-archive-files files)))
  14862. files))
  14863. (defun org-agenda-file-p (&optional file)
  14864. "Return non-nil, if FILE is an agenda file.
  14865. If FILE is omitted, use the file associated with the current
  14866. buffer."
  14867. (member (or file (buffer-file-name))
  14868. (org-agenda-files t)))
  14869. (defun org-edit-agenda-file-list ()
  14870. "Edit the list of agenda files.
  14871. Depending on setup, this either uses customize to edit the variable
  14872. `org-agenda-files', or it visits the file that is holding the list. In the
  14873. latter case, the buffer is set up in a way that saving it automatically kills
  14874. the buffer and restores the previous window configuration."
  14875. (interactive)
  14876. (if (stringp org-agenda-files)
  14877. (let ((cw (current-window-configuration)))
  14878. (find-file org-agenda-files)
  14879. (org-set-local 'org-window-configuration cw)
  14880. (org-add-hook 'after-save-hook
  14881. (lambda ()
  14882. (set-window-configuration
  14883. (prog1 org-window-configuration
  14884. (kill-buffer (current-buffer))))
  14885. (org-install-agenda-files-menu)
  14886. (message "New agenda file list installed"))
  14887. nil 'local)
  14888. (message "%s" (substitute-command-keys
  14889. "Edit list and finish with \\[save-buffer]")))
  14890. (customize-variable 'org-agenda-files)))
  14891. (defun org-store-new-agenda-file-list (list)
  14892. "Set new value for the agenda file list and save it correctly."
  14893. (if (stringp org-agenda-files)
  14894. (let ((fe (org-read-agenda-file-list t)) b u)
  14895. (while (setq b (find-buffer-visiting org-agenda-files))
  14896. (kill-buffer b))
  14897. (with-temp-file org-agenda-files
  14898. (insert
  14899. (mapconcat
  14900. (lambda (f) ;; Keep un-expanded entries.
  14901. (if (setq u (assoc f fe))
  14902. (cdr u)
  14903. f))
  14904. list "\n")
  14905. "\n")))
  14906. (let ((org-mode-hook nil) (org-inhibit-startup t)
  14907. (org-insert-mode-line-in-empty-file nil))
  14908. (setq org-agenda-files list)
  14909. (customize-save-variable 'org-agenda-files org-agenda-files))))
  14910. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  14911. "Read the list of agenda files from a file.
  14912. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  14913. filenames, used by `org-store-new-agenda-file-list' to write back
  14914. un-expanded file names."
  14915. (when (file-directory-p org-agenda-files)
  14916. (error "`org-agenda-files' cannot be a single directory"))
  14917. (when (stringp org-agenda-files)
  14918. (with-temp-buffer
  14919. (insert-file-contents org-agenda-files)
  14920. (mapcar
  14921. (lambda (f)
  14922. (let ((e (expand-file-name (substitute-in-file-name f)
  14923. org-directory)))
  14924. (if pair-with-expansion
  14925. (cons e f)
  14926. e)))
  14927. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  14928. ;;;###autoload
  14929. (defun org-cycle-agenda-files ()
  14930. "Cycle through the files in `org-agenda-files'.
  14931. If the current buffer visits an agenda file, find the next one in the list.
  14932. If the current buffer does not, find the first agenda file."
  14933. (interactive)
  14934. (let* ((fs (org-agenda-files t))
  14935. (files (append fs (list (car fs))))
  14936. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  14937. file)
  14938. (unless files (error "No agenda files"))
  14939. (catch 'exit
  14940. (while (setq file (pop files))
  14941. (if (equal (file-truename file) tcf)
  14942. (when (car files)
  14943. (find-file (car files))
  14944. (throw 'exit t))))
  14945. (find-file (car fs)))
  14946. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  14947. (defun org-agenda-file-to-front (&optional to-end)
  14948. "Move/add the current file to the top of the agenda file list.
  14949. If the file is not present in the list, it is added to the front. If it is
  14950. present, it is moved there. With optional argument TO-END, add/move to the
  14951. end of the list."
  14952. (interactive "P")
  14953. (let ((org-agenda-skip-unavailable-files nil)
  14954. (file-alist (mapcar (lambda (x)
  14955. (cons (file-truename x) x))
  14956. (org-agenda-files t)))
  14957. (ctf (file-truename buffer-file-name))
  14958. x had)
  14959. (setq x (assoc ctf file-alist) had x)
  14960. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  14961. (if to-end
  14962. (setq file-alist (append (delq x file-alist) (list x)))
  14963. (setq file-alist (cons x (delq x file-alist))))
  14964. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  14965. (org-install-agenda-files-menu)
  14966. (message "File %s to %s of agenda file list"
  14967. (if had "moved" "added") (if to-end "end" "front"))))
  14968. (defun org-remove-file (&optional file)
  14969. "Remove current file from the list of files in variable `org-agenda-files'.
  14970. These are the files which are being checked for agenda entries.
  14971. Optional argument FILE means use this file instead of the current."
  14972. (interactive)
  14973. (let* ((org-agenda-skip-unavailable-files nil)
  14974. (file (or file buffer-file-name))
  14975. (true-file (file-truename file))
  14976. (afile (abbreviate-file-name file))
  14977. (files (delq nil (mapcar
  14978. (lambda (x)
  14979. (if (equal true-file
  14980. (file-truename x))
  14981. nil x))
  14982. (org-agenda-files t)))))
  14983. (if (not (= (length files) (length (org-agenda-files t))))
  14984. (progn
  14985. (org-store-new-agenda-file-list files)
  14986. (org-install-agenda-files-menu)
  14987. (message "Removed file: %s" afile))
  14988. (message "File was not in list: %s (not removed)" afile))))
  14989. (defun org-file-menu-entry (file)
  14990. (vector file (list 'find-file file) t))
  14991. (defun org-check-agenda-file (file)
  14992. "Make sure FILE exists. If not, ask user what to do."
  14993. (when (not (file-exists-p file))
  14994. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  14995. (abbreviate-file-name file))
  14996. (let ((r (downcase (read-char-exclusive))))
  14997. (cond
  14998. ((equal r ?r)
  14999. (org-remove-file file)
  15000. (throw 'nextfile t))
  15001. (t (error "Abort"))))))
  15002. (defun org-get-agenda-file-buffer (file)
  15003. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15004. it to the list of buffers which might be released later."
  15005. (let ((buf (org-find-base-buffer-visiting file)))
  15006. (if buf
  15007. buf ; just return it
  15008. ;; Make a new buffer and remember it
  15009. (setq buf (find-file-noselect file))
  15010. (if buf (push buf org-agenda-new-buffers))
  15011. buf)))
  15012. (defun org-release-buffers (blist)
  15013. "Release all buffers in list, asking the user for confirmation when needed.
  15014. When a buffer is unmodified, it is just killed. When modified, it is saved
  15015. \(if the user agrees) and then killed."
  15016. (let (buf file)
  15017. (while (setq buf (pop blist))
  15018. (setq file (buffer-file-name buf))
  15019. (when (and (buffer-modified-p buf)
  15020. file
  15021. (y-or-n-p (format "Save file %s? " file)))
  15022. (with-current-buffer buf (save-buffer)))
  15023. (kill-buffer buf))))
  15024. (defun org-prepare-agenda-buffers (files)
  15025. "Create buffers for all agenda files, protect archived trees and comments."
  15026. (interactive)
  15027. (let ((pa '(:org-archived t))
  15028. (pc '(:org-comment t))
  15029. (pall '(:org-archived t :org-comment t))
  15030. (inhibit-read-only t)
  15031. (rea (concat ":" org-archive-tag ":"))
  15032. bmp file re)
  15033. (save-excursion
  15034. (save-restriction
  15035. (while (setq file (pop files))
  15036. (catch 'nextfile
  15037. (if (bufferp file)
  15038. (set-buffer file)
  15039. (org-check-agenda-file file)
  15040. (set-buffer (org-get-agenda-file-buffer file)))
  15041. (widen)
  15042. (setq bmp (buffer-modified-p))
  15043. (org-refresh-category-properties)
  15044. (setq org-todo-keywords-for-agenda
  15045. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15046. (setq org-done-keywords-for-agenda
  15047. (append org-done-keywords-for-agenda org-done-keywords))
  15048. (setq org-todo-keyword-alist-for-agenda
  15049. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15050. (setq org-drawers-for-agenda
  15051. (append org-drawers-for-agenda org-drawers))
  15052. (setq org-tag-alist-for-agenda
  15053. (append org-tag-alist-for-agenda org-tag-alist))
  15054. (save-excursion
  15055. (remove-text-properties (point-min) (point-max) pall)
  15056. (when org-agenda-skip-archived-trees
  15057. (goto-char (point-min))
  15058. (while (re-search-forward rea nil t)
  15059. (if (org-on-heading-p t)
  15060. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15061. (goto-char (point-min))
  15062. (setq re (format org-heading-keyword-regexp-format
  15063. org-comment-string))
  15064. (while (re-search-forward re nil t)
  15065. (add-text-properties
  15066. (match-beginning 0) (org-end-of-subtree t) pc)))
  15067. (set-buffer-modified-p bmp)))))
  15068. (setq org-todo-keywords-for-agenda
  15069. (org-uniquify org-todo-keywords-for-agenda))
  15070. (setq org-todo-keyword-alist-for-agenda
  15071. (org-uniquify org-todo-keyword-alist-for-agenda)
  15072. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15073. ;;;; Embedded LaTeX
  15074. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15075. "Keymap for the minor `org-cdlatex-mode'.")
  15076. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15077. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15078. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15079. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15080. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15081. (defvar org-cdlatex-texmathp-advice-is-done nil
  15082. "Flag remembering if we have applied the advice to texmathp already.")
  15083. (define-minor-mode org-cdlatex-mode
  15084. "Toggle the minor `org-cdlatex-mode'.
  15085. This mode supports entering LaTeX environment and math in LaTeX fragments
  15086. in Org-mode.
  15087. \\{org-cdlatex-mode-map}"
  15088. nil " OCDL" nil
  15089. (when org-cdlatex-mode
  15090. (require 'cdlatex)
  15091. (run-hooks 'cdlatex-mode-hook)
  15092. (cdlatex-compute-tables))
  15093. (unless org-cdlatex-texmathp-advice-is-done
  15094. (setq org-cdlatex-texmathp-advice-is-done t)
  15095. (defadvice texmathp (around org-math-always-on activate)
  15096. "Always return t in org-mode buffers.
  15097. This is because we want to insert math symbols without dollars even outside
  15098. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15099. an embedded LaTeX fragment, let texmathp do its job.
  15100. \\[org-cdlatex-mode-map]"
  15101. (interactive)
  15102. (let (p)
  15103. (cond
  15104. ((not (eq major-mode 'org-mode)) ad-do-it)
  15105. ((eq this-command 'cdlatex-math-symbol)
  15106. (setq ad-return-value t
  15107. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15108. (t
  15109. (let ((p (org-inside-LaTeX-fragment-p)))
  15110. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15111. (setq ad-return-value t
  15112. texmathp-why '("Org-mode embedded math" . 0))
  15113. (if p ad-do-it)))))))))
  15114. (defun turn-on-org-cdlatex ()
  15115. "Unconditionally turn on `org-cdlatex-mode'."
  15116. (org-cdlatex-mode 1))
  15117. (defun org-inside-LaTeX-fragment-p ()
  15118. "Test if point is inside a LaTeX fragment.
  15119. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15120. sequence appearing also before point.
  15121. Even though the matchers for math are configurable, this function assumes
  15122. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15123. delimiters are skipped when they have been removed by customization.
  15124. The return value is nil, or a cons cell with the delimiter and the
  15125. position of this delimiter.
  15126. This function does a reasonably good job, but can locally be fooled by
  15127. for example currency specifications. For example it will assume being in
  15128. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15129. fragments that are properly closed, but during editing, we have to live
  15130. with the uncertainty caused by missing closing delimiters. This function
  15131. looks only before point, not after."
  15132. (catch 'exit
  15133. (let ((pos (point))
  15134. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15135. (lim (progn
  15136. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15137. (point)))
  15138. dd-on str (start 0) m re)
  15139. (goto-char pos)
  15140. (when dodollar
  15141. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15142. re (nth 1 (assoc "$" org-latex-regexps)))
  15143. (while (string-match re str start)
  15144. (cond
  15145. ((= (match-end 0) (length str))
  15146. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15147. ((= (match-end 0) (- (length str) 5))
  15148. (throw 'exit nil))
  15149. (t (setq start (match-end 0))))))
  15150. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15151. (goto-char pos)
  15152. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15153. (and (match-beginning 2) (throw 'exit nil))
  15154. ;; count $$
  15155. (while (re-search-backward "\\$\\$" lim t)
  15156. (setq dd-on (not dd-on)))
  15157. (goto-char pos)
  15158. (if dd-on (cons "$$" m))))))
  15159. (defun org-inside-latex-macro-p ()
  15160. "Is point inside a LaTeX macro or its arguments?"
  15161. (save-match-data
  15162. (org-in-regexp
  15163. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15164. (defun org-try-cdlatex-tab ()
  15165. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15166. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15167. - inside a LaTeX fragment, or
  15168. - after the first word in a line, where an abbreviation expansion could
  15169. insert a LaTeX environment."
  15170. (when org-cdlatex-mode
  15171. (cond
  15172. ;; Before any word on the line: No expansion possible.
  15173. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15174. ;; Just after first word on the line: Expand it. Make sure it
  15175. ;; cannot happen on headlines, though.
  15176. ((save-excursion
  15177. (skip-chars-backward "a-zA-Z0-9*")
  15178. (skip-chars-backward " \t")
  15179. (and (bolp) (not (org-at-heading-p))))
  15180. (cdlatex-tab) t)
  15181. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15182. (defun org-cdlatex-underscore-caret (&optional arg)
  15183. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15184. Revert to the normal definition outside of these fragments."
  15185. (interactive "P")
  15186. (if (org-inside-LaTeX-fragment-p)
  15187. (call-interactively 'cdlatex-sub-superscript)
  15188. (let (org-cdlatex-mode)
  15189. (call-interactively (key-binding (vector last-input-event))))))
  15190. (defun org-cdlatex-math-modify (&optional arg)
  15191. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15192. Revert to the normal definition outside of these fragments."
  15193. (interactive "P")
  15194. (if (org-inside-LaTeX-fragment-p)
  15195. (call-interactively 'cdlatex-math-modify)
  15196. (let (org-cdlatex-mode)
  15197. (call-interactively (key-binding (vector last-input-event))))))
  15198. (defvar org-latex-fragment-image-overlays nil
  15199. "List of overlays carrying the images of latex fragments.")
  15200. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15201. (defun org-remove-latex-fragment-image-overlays ()
  15202. "Remove all overlays with LaTeX fragment images in current buffer."
  15203. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15204. (setq org-latex-fragment-image-overlays nil))
  15205. (defun org-preview-latex-fragment (&optional subtree)
  15206. "Preview the LaTeX fragment at point, or all locally or globally.
  15207. If the cursor is in a LaTeX fragment, create the image and overlay
  15208. it over the source code. If there is no fragment at point, display
  15209. all fragments in the current text, from one headline to the next. With
  15210. prefix SUBTREE, display all fragments in the current subtree. With a
  15211. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15212. the cursor is before the first headline,
  15213. display all fragments in the buffer.
  15214. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15215. (interactive "P")
  15216. (org-remove-latex-fragment-image-overlays)
  15217. (save-excursion
  15218. (save-restriction
  15219. (let (beg end at msg)
  15220. (cond
  15221. ((or (equal subtree '(16))
  15222. (not (save-excursion
  15223. (re-search-backward org-outline-regexp-bol nil t))))
  15224. (setq beg (point-min) end (point-max)
  15225. msg "Creating images for buffer...%s"))
  15226. ((equal subtree '(4))
  15227. (org-back-to-heading)
  15228. (setq beg (point) end (org-end-of-subtree t)
  15229. msg "Creating images for subtree...%s"))
  15230. (t
  15231. (if (setq at (org-inside-LaTeX-fragment-p))
  15232. (goto-char (max (point-min) (- (cdr at) 2)))
  15233. (org-back-to-heading))
  15234. (setq beg (point) end (progn (outline-next-heading) (point))
  15235. msg (if at "Creating image...%s"
  15236. "Creating images for entry...%s"))))
  15237. (message msg "")
  15238. (narrow-to-region beg end)
  15239. (goto-char beg)
  15240. (org-format-latex
  15241. (concat "ltxpng/" (file-name-sans-extension
  15242. (file-name-nondirectory
  15243. buffer-file-name)))
  15244. default-directory 'overlays msg at 'forbuffer 'dvipng)
  15245. (message msg "done. Use `C-c C-c' to remove images.")))))
  15246. (defvar org-latex-regexps
  15247. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15248. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15249. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15250. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15251. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15252. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15253. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15254. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15255. "Regular expressions for matching embedded LaTeX.")
  15256. (defvar org-export-have-math nil) ;; dynamic scoping
  15257. (defun org-format-latex (prefix &optional dir overlays msg at
  15258. forbuffer processing-type)
  15259. "Replace LaTeX fragments with links to an image, and produce images.
  15260. Some of the options can be changed using the variable
  15261. `org-format-latex-options'."
  15262. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15263. (let* ((prefixnodir (file-name-nondirectory prefix))
  15264. (absprefix (expand-file-name prefix dir))
  15265. (todir (file-name-directory absprefix))
  15266. (opt org-format-latex-options)
  15267. (matchers (plist-get opt :matchers))
  15268. (re-list org-latex-regexps)
  15269. (org-format-latex-header-extra
  15270. (plist-get (org-infile-export-plist) :latex-header-extra))
  15271. (cnt 0) txt hash link beg end re e checkdir
  15272. executables-checked string
  15273. m n block-type block linkfile movefile ov)
  15274. ;; Check the different regular expressions
  15275. (while (setq e (pop re-list))
  15276. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15277. block (if block-type "\n\n" ""))
  15278. (when (member m matchers)
  15279. (goto-char (point-min))
  15280. (while (re-search-forward re nil t)
  15281. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15282. (not (get-text-property (match-beginning n)
  15283. 'org-protected))
  15284. (or (not overlays)
  15285. (not (eq (get-char-property (match-beginning n)
  15286. 'org-overlay-type)
  15287. 'org-latex-overlay))))
  15288. (setq org-export-have-math t)
  15289. (cond
  15290. ((eq processing-type 'verbatim)
  15291. ;; Leave the text verbatim, just protect it
  15292. (add-text-properties (match-beginning n) (match-end n)
  15293. '(org-protected t)))
  15294. ((eq processing-type 'mathjax)
  15295. ;; Prepare for MathJax processing
  15296. (setq string (match-string n))
  15297. (if (member m '("$" "$1"))
  15298. (save-excursion
  15299. (delete-region (match-beginning n) (match-end n))
  15300. (goto-char (match-beginning n))
  15301. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15302. "\\)")
  15303. '(org-protected t))))
  15304. (add-text-properties (match-beginning n) (match-end n)
  15305. '(org-protected t))))
  15306. ((eq processing-type 'dvipng)
  15307. ;; Process to an image
  15308. (setq txt (match-string n)
  15309. beg (match-beginning n) end (match-end n)
  15310. cnt (1+ cnt))
  15311. (let (print-length print-level) ; make sure full list is printed
  15312. (setq hash (sha1 (prin1-to-string
  15313. (list org-format-latex-header
  15314. org-format-latex-header-extra
  15315. org-export-latex-default-packages-alist
  15316. org-export-latex-packages-alist
  15317. org-format-latex-options
  15318. forbuffer txt)))
  15319. linkfile (format "%s_%s.png" prefix hash)
  15320. movefile (format "%s_%s.png" absprefix hash)))
  15321. (setq link (concat block "[[file:" linkfile "]]" block))
  15322. (if msg (message msg cnt))
  15323. (goto-char beg)
  15324. (unless checkdir ; make sure the directory exists
  15325. (setq checkdir t)
  15326. (or (file-directory-p todir) (make-directory todir t)))
  15327. (unless executables-checked
  15328. (org-check-external-command
  15329. "latex" "needed to convert LaTeX fragments to images")
  15330. (org-check-external-command
  15331. "dvipng" "needed to convert LaTeX fragments to images")
  15332. (setq executables-checked t))
  15333. (unless (file-exists-p movefile)
  15334. (org-create-formula-image
  15335. txt movefile opt forbuffer))
  15336. (if overlays
  15337. (progn
  15338. (mapc (lambda (o)
  15339. (if (eq (overlay-get o 'org-overlay-type)
  15340. 'org-latex-overlay)
  15341. (delete-overlay o)))
  15342. (overlays-in beg end))
  15343. (setq ov (make-overlay beg end))
  15344. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15345. (if (featurep 'xemacs)
  15346. (progn
  15347. (overlay-put ov 'invisible t)
  15348. (overlay-put
  15349. ov 'end-glyph
  15350. (make-glyph (vector 'png :file movefile))))
  15351. (overlay-put
  15352. ov 'display
  15353. (list 'image :type 'png :file movefile :ascent 'center)))
  15354. (push ov org-latex-fragment-image-overlays)
  15355. (goto-char end))
  15356. (delete-region beg end)
  15357. (insert (org-add-props link
  15358. (list 'org-latex-src
  15359. (replace-regexp-in-string
  15360. "\"" "" txt)
  15361. 'org-latex-src-embed-type
  15362. (if block-type 'paragraph 'character))))))
  15363. ((eq processing-type 'mathml)
  15364. ;; Process to MathML
  15365. (unless executables-checked
  15366. (unless (save-match-data (org-format-latex-mathml-available-p))
  15367. (error "LaTeX to MathML converter not configured"))
  15368. (setq executables-checked t))
  15369. (setq txt (match-string n)
  15370. beg (match-beginning n) end (match-end n)
  15371. cnt (1+ cnt))
  15372. (if msg (message msg cnt))
  15373. (goto-char beg)
  15374. (delete-region beg end)
  15375. (insert (org-format-latex-as-mathml
  15376. txt block-type prefix dir)))
  15377. (t
  15378. (error "Unknown conversion type %s for latex fragments"
  15379. processing-type)))))))))
  15380. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15381. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15382. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15383. sucessful, return the portion between \"<math...> </math>\"
  15384. elements otherwise return nil. When MATHML-FILE is specified,
  15385. write the results in to that file. When invoked as an
  15386. interactive command, prompt for LATEX-FRAG, with initial value
  15387. set to the current active region and echo the results for user
  15388. inspection."
  15389. (interactive (list (let ((frag (when (region-active-p)
  15390. (buffer-substring-no-properties
  15391. (region-beginning) (region-end)))))
  15392. (read-string "LaTeX Fragment: " frag nil frag))))
  15393. (unless latex-frag (error "Invalid latex-frag"))
  15394. (let* ((tmp-in-file (file-relative-name
  15395. (make-temp-name (expand-file-name "ltxmathml-in"))))
  15396. (ignore (write-region latex-frag nil tmp-in-file))
  15397. (tmp-out-file (file-relative-name
  15398. (make-temp-name (expand-file-name "ltxmathml-out"))))
  15399. (cmd (format-spec
  15400. org-latex-to-mathml-convert-command
  15401. `((?j . ,(shell-quote-argument
  15402. (expand-file-name org-latex-to-mathml-jar-file)))
  15403. (?I . ,(shell-quote-argument tmp-in-file))
  15404. (?o . ,(shell-quote-argument tmp-out-file)))))
  15405. mathml shell-command-output)
  15406. (when (org-called-interactively-p 'any)
  15407. (unless (org-format-latex-mathml-available-p)
  15408. (error "LaTeX to MathML converter not configured")))
  15409. (message "Running %s" cmd)
  15410. (setq shell-command-output (shell-command-to-string cmd))
  15411. (setq mathml
  15412. (when (file-readable-p tmp-out-file)
  15413. (with-current-buffer (find-file-noselect tmp-out-file t)
  15414. (goto-char (point-min))
  15415. (when (re-search-forward
  15416. (concat
  15417. (regexp-quote
  15418. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  15419. "\\(.\\|\n\\)*"
  15420. (regexp-quote "</math>")) nil t)
  15421. (prog1 (match-string 0) (kill-buffer))))))
  15422. (cond
  15423. (mathml
  15424. (setq mathml
  15425. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  15426. (when mathml-file
  15427. (write-region mathml nil mathml-file))
  15428. (when (org-called-interactively-p 'any)
  15429. (message mathml)))
  15430. ((message "LaTeX to MathML conversion failed")
  15431. (message shell-command-output)))
  15432. (delete-file tmp-in-file)
  15433. (when (file-exists-p tmp-out-file)
  15434. (delete-file tmp-out-file))
  15435. mathml))
  15436. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  15437. prefix &optional dir)
  15438. "Use `org-create-math-formula' but check local cache first."
  15439. (let* ((absprefix (expand-file-name prefix dir))
  15440. (print-length nil) (print-level nil)
  15441. (formula-id (concat
  15442. "formula-"
  15443. (sha1
  15444. (prin1-to-string
  15445. (list latex-frag
  15446. org-latex-to-mathml-convert-command)))))
  15447. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  15448. (formula-cache-dir (file-name-directory formula-cache)))
  15449. (unless (file-directory-p formula-cache-dir)
  15450. (make-directory formula-cache-dir t))
  15451. (unless (file-exists-p formula-cache)
  15452. (org-create-math-formula latex-frag formula-cache))
  15453. (if (file-exists-p formula-cache)
  15454. ;; Successful conversion. Return the link to MathML file.
  15455. (org-add-props
  15456. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  15457. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  15458. 'org-latex-src-embed-type (if latex-frag-type
  15459. 'paragraph 'character)))
  15460. ;; Failed conversion. Return the LaTeX fragment verbatim
  15461. (add-text-properties
  15462. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  15463. latex-frag)))
  15464. ;; This function borrows from Ganesh Swami's latex2png.el
  15465. (defun org-create-formula-image (string tofile options buffer)
  15466. "This calls dvipng."
  15467. (require 'org-latex)
  15468. (let* ((tmpdir (if (featurep 'xemacs)
  15469. (temp-directory)
  15470. temporary-file-directory))
  15471. (texfilebase (make-temp-name
  15472. (expand-file-name "orgtex" tmpdir)))
  15473. (texfile (concat texfilebase ".tex"))
  15474. (dvifile (concat texfilebase ".dvi"))
  15475. (pngfile (concat texfilebase ".png"))
  15476. (fnh (if (featurep 'xemacs)
  15477. (font-height (get-face-font 'default))
  15478. (face-attribute 'default :height nil)))
  15479. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15480. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15481. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15482. "Black"))
  15483. (bg (or (plist-get options (if buffer :background :html-background))
  15484. "Transparent")))
  15485. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  15486. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  15487. (with-temp-file texfile
  15488. (insert (org-splice-latex-header
  15489. org-format-latex-header
  15490. org-export-latex-default-packages-alist
  15491. org-export-latex-packages-alist t
  15492. org-format-latex-header-extra))
  15493. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  15494. (require 'org-latex)
  15495. (org-export-latex-fix-inputenc))
  15496. (let ((dir default-directory))
  15497. (condition-case nil
  15498. (progn
  15499. (cd tmpdir)
  15500. (call-process "latex" nil nil nil texfile))
  15501. (error nil))
  15502. (cd dir))
  15503. (if (not (file-exists-p dvifile))
  15504. (progn (message "Failed to create dvi file from %s" texfile) nil)
  15505. (condition-case nil
  15506. (call-process "dvipng" nil nil nil
  15507. "-fg" fg "-bg" bg
  15508. "-D" dpi
  15509. ;;"-x" scale "-y" scale
  15510. "-T" "tight"
  15511. "-o" pngfile
  15512. dvifile)
  15513. (error nil))
  15514. (if (not (file-exists-p pngfile))
  15515. (if org-format-latex-signal-error
  15516. (error "Failed to create png file from %s" texfile)
  15517. (message "Failed to create png file from %s" texfile)
  15518. nil)
  15519. ;; Use the requested file name and clean up
  15520. (copy-file pngfile tofile 'replace)
  15521. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  15522. (delete-file (concat texfilebase e)))
  15523. pngfile))))
  15524. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  15525. "Fill a LaTeX header template TPL.
  15526. In the template, the following place holders will be recognized:
  15527. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  15528. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  15529. [PACKAGES] \\usepackage statements for PKG
  15530. [NO-PACKAGES] do not include PKG
  15531. [EXTRA] the string EXTRA
  15532. [NO-EXTRA] do not include EXTRA
  15533. For backward compatibility, if both the positive and the negative place
  15534. holder is missing, the positive one (without the \"NO-\") will be
  15535. assumed to be present at the end of the template.
  15536. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  15537. EXTRA is a string.
  15538. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  15539. (let (rpl (end ""))
  15540. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  15541. (setq rpl (if (or (match-end 1) (not def-pkg))
  15542. "" (org-latex-packages-to-string def-pkg snippets-p t))
  15543. tpl (replace-match rpl t t tpl))
  15544. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  15545. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  15546. (setq rpl (if (or (match-end 1) (not pkg))
  15547. "" (org-latex-packages-to-string pkg snippets-p t))
  15548. tpl (replace-match rpl t t tpl))
  15549. (if pkg (setq end
  15550. (concat end "\n"
  15551. (org-latex-packages-to-string pkg snippets-p)))))
  15552. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  15553. (setq rpl (if (or (match-end 1) (not extra))
  15554. "" (concat extra "\n"))
  15555. tpl (replace-match rpl t t tpl))
  15556. (if (and extra (string-match "\\S-" extra))
  15557. (setq end (concat end "\n" extra))))
  15558. (if (string-match "\\S-" end)
  15559. (concat tpl "\n" end)
  15560. tpl)))
  15561. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  15562. "Turn an alist of packages into a string with the \\usepackage macros."
  15563. (setq pkg (mapconcat (lambda(p)
  15564. (cond
  15565. ((stringp p) p)
  15566. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  15567. (format "%% Package %s omitted" (cadr p)))
  15568. ((equal "" (car p))
  15569. (format "\\usepackage{%s}" (cadr p)))
  15570. (t
  15571. (format "\\usepackage[%s]{%s}"
  15572. (car p) (cadr p)))))
  15573. pkg
  15574. "\n"))
  15575. (if newline (concat pkg "\n") pkg))
  15576. (defun org-dvipng-color (attr)
  15577. "Return an rgb color specification for dvipng."
  15578. (apply 'format "rgb %s %s %s"
  15579. (mapcar 'org-normalize-color
  15580. (color-values (face-attribute 'default attr nil)))))
  15581. (defun org-normalize-color (value)
  15582. "Return string to be used as color value for an RGB component."
  15583. (format "%g" (/ value 65535.0)))
  15584. ;; Image display
  15585. (defvar org-inline-image-overlays nil)
  15586. (make-variable-buffer-local 'org-inline-image-overlays)
  15587. (defun org-toggle-inline-images (&optional include-linked)
  15588. "Toggle the display of inline images.
  15589. INCLUDE-LINKED is passed to `org-display-inline-images'."
  15590. (interactive "P")
  15591. (if org-inline-image-overlays
  15592. (progn
  15593. (org-remove-inline-images)
  15594. (message "Inline image display turned off"))
  15595. (org-display-inline-images include-linked)
  15596. (if org-inline-image-overlays
  15597. (message "%d images displayed inline"
  15598. (length org-inline-image-overlays))
  15599. (message "No images to display inline"))))
  15600. (defun org-display-inline-images (&optional include-linked refresh beg end)
  15601. "Display inline images.
  15602. Normally only links without a description part are inlined, because this
  15603. is how it will work for export. When INCLUDE-LINKED is set, also links
  15604. with a description part will be inlined. This can be nice for a quick
  15605. look at those images, but it does not reflect what exported files will look
  15606. like.
  15607. When REFRESH is set, refresh existing images between BEG and END.
  15608. This will create new image displays only if necessary.
  15609. BEG and END default to the buffer boundaries."
  15610. (interactive "P")
  15611. (unless refresh
  15612. (org-remove-inline-images)
  15613. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  15614. (save-excursion
  15615. (save-restriction
  15616. (widen)
  15617. (setq beg (or beg (point-min)) end (or end (point-max)))
  15618. (goto-char (point-min))
  15619. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  15620. (substring (org-image-file-name-regexp) 0 -2)
  15621. "\\)\\]" (if include-linked "" "\\]")))
  15622. old file ov img)
  15623. (while (re-search-forward re end t)
  15624. (setq old (get-char-property-and-overlay (match-beginning 1)
  15625. 'org-image-overlay))
  15626. (setq file (expand-file-name
  15627. (concat (or (match-string 3) "") (match-string 4))))
  15628. (when (file-exists-p file)
  15629. (if (and (car-safe old) refresh)
  15630. (image-refresh (overlay-get (cdr old) 'display))
  15631. (setq img (save-match-data (create-image file)))
  15632. (when img
  15633. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  15634. (overlay-put ov 'display img)
  15635. (overlay-put ov 'face 'default)
  15636. (overlay-put ov 'org-image-overlay t)
  15637. (overlay-put ov 'modification-hooks
  15638. (list 'org-display-inline-modification-hook))
  15639. (push ov org-inline-image-overlays)))))))))
  15640. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  15641. "Remove inline-display overlay if a corresponding region is modified."
  15642. (let ((inhibit-modification-hooks t))
  15643. (when (and ov after)
  15644. (delete ov org-inline-image-overlays)
  15645. (delete-overlay ov))))
  15646. (defun org-remove-inline-images ()
  15647. "Remove inline display of images."
  15648. (interactive)
  15649. (mapc 'delete-overlay org-inline-image-overlays)
  15650. (setq org-inline-image-overlays nil))
  15651. ;;;; Key bindings
  15652. ;; Outline functions from `outline-mode-prefix-map'
  15653. ;; that can be remapped in Org:
  15654. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  15655. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  15656. (define-key org-mode-map [remap outline-forward-same-level]
  15657. 'org-forward-same-level)
  15658. (define-key org-mode-map [remap outline-backward-same-level]
  15659. 'org-backward-same-level)
  15660. (define-key org-mode-map [remap show-branches]
  15661. 'org-kill-note-or-show-branches)
  15662. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  15663. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  15664. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  15665. ;; Outline functions from `outline-mode-prefix-map'
  15666. ;; that can not be remapped in Org:
  15667. ;; - the column "key binding" shows whether the Outline function is still
  15668. ;; available in Org mode on the same key that it has been bound to in
  15669. ;; Outline mode:
  15670. ;; - "overridden": key used for a different functionality in Org mode
  15671. ;; - else: key still bound to the same Outline function in Org mode
  15672. ;; | Outline function | key binding | Org replacement |
  15673. ;; |------------------------------------+-------------+-----------------------|
  15674. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  15675. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  15676. ;; | `show-children' | `C-c C-i' | visibility cycling |
  15677. ;; | `hide-subtree' | overridden | visibility cycling |
  15678. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  15679. ;; | `hide-body' | overridden | no replacement |
  15680. ;; | `show-all' | overridden | no replacement |
  15681. ;; | `hide-entry' | overridden | visibility cycling |
  15682. ;; | `show-entry' | overridden | no replacement |
  15683. ;; | `hide-leaves' | overridden | no replacement |
  15684. ;; | `hide-sublevels' | overridden | no replacement |
  15685. ;; | `hide-other' | overridden | no replacement |
  15686. ;; | `outline-move-subtree-up' | `C-c C-^' | better: org-shiftup |
  15687. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  15688. ;; Make `C-c C-x' a prefix key
  15689. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  15690. ;; TAB key with modifiers
  15691. (org-defkey org-mode-map "\C-i" 'org-cycle)
  15692. (org-defkey org-mode-map [(tab)] 'org-cycle)
  15693. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  15694. (org-defkey org-mode-map [(meta tab)] 'pcomplete)
  15695. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  15696. (org-defkey org-mode-map "\M-\C-i" 'pcomplete)
  15697. ;; The following line is necessary under Suse GNU/Linux
  15698. (unless (featurep 'xemacs)
  15699. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  15700. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  15701. (define-key org-mode-map [backtab] 'org-shifttab)
  15702. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  15703. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  15704. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  15705. ;; Cursor keys with modifiers
  15706. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  15707. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  15708. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  15709. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  15710. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  15711. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  15712. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  15713. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  15714. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  15715. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  15716. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  15717. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  15718. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  15719. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  15720. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  15721. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  15722. ;; Babel keys
  15723. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  15724. (mapc (lambda (pair)
  15725. (define-key org-babel-map (car pair) (cdr pair)))
  15726. org-babel-key-bindings)
  15727. ;;; Extra keys for tty access.
  15728. ;; We only set them when really needed because otherwise the
  15729. ;; menus don't show the simple keys
  15730. (when (or org-use-extra-keys
  15731. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  15732. (not window-system))
  15733. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  15734. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  15735. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  15736. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  15737. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  15738. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  15739. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  15740. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  15741. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  15742. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  15743. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  15744. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  15745. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  15746. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  15747. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  15748. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  15749. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  15750. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  15751. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  15752. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  15753. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  15754. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  15755. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  15756. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  15757. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  15758. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  15759. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  15760. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  15761. ;; All the other keys
  15762. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  15763. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  15764. (if (boundp 'narrow-map)
  15765. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  15766. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  15767. (if (boundp 'narrow-map)
  15768. (org-defkey narrow-map "b" 'org-narrow-to-block)
  15769. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  15770. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  15771. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  15772. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  15773. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  15774. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  15775. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  15776. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  15777. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  15778. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  15779. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  15780. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  15781. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  15782. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  15783. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  15784. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  15785. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  15786. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  15787. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  15788. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  15789. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  15790. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  15791. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  15792. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  15793. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  15794. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  15795. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  15796. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  15797. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  15798. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  15799. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  15800. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  15801. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  15802. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  15803. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  15804. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  15805. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  15806. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  15807. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  15808. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  15809. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  15810. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  15811. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15812. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  15813. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  15814. (org-defkey org-mode-map "\C-c^" 'org-sort)
  15815. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  15816. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  15817. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  15818. (org-defkey org-mode-map "\C-m" 'org-return)
  15819. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  15820. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  15821. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  15822. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  15823. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  15824. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  15825. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  15826. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  15827. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  15828. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  15829. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  15830. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  15831. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  15832. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  15833. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  15834. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  15835. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  15836. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  15837. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  15838. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  15839. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  15840. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  15841. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  15842. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  15843. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  15844. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  15845. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  15846. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  15847. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  15848. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  15849. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  15850. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  15851. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  15852. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  15853. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  15854. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  15855. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  15856. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  15857. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  15858. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  15859. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  15860. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  15861. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  15862. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  15863. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  15864. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  15865. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  15866. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  15867. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  15868. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  15869. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  15870. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  15871. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  15872. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  15873. (when (featurep 'xemacs)
  15874. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  15875. (defconst org-speed-commands-default
  15876. '(
  15877. ("Outline Navigation")
  15878. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  15879. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  15880. ("f" . (org-speed-move-safe 'org-forward-same-level))
  15881. ("b" . (org-speed-move-safe 'org-backward-same-level))
  15882. ("u" . (org-speed-move-safe 'outline-up-heading))
  15883. ("j" . org-goto)
  15884. ("g" . (org-refile t))
  15885. ("Outline Visibility")
  15886. ("c" . org-cycle)
  15887. ("C" . org-shifttab)
  15888. (" " . org-display-outline-path)
  15889. ("Outline Structure Editing")
  15890. ("U" . org-shiftmetaup)
  15891. ("D" . org-shiftmetadown)
  15892. ("r" . org-metaright)
  15893. ("l" . org-metaleft)
  15894. ("R" . org-shiftmetaright)
  15895. ("L" . org-shiftmetaleft)
  15896. ("i" . (progn (forward-char 1) (call-interactively
  15897. 'org-insert-heading-respect-content)))
  15898. ("^" . org-sort)
  15899. ("w" . org-refile)
  15900. ("a" . org-archive-subtree-default-with-confirmation)
  15901. ("." . org-mark-subtree)
  15902. ("Clock Commands")
  15903. ("I" . org-clock-in)
  15904. ("O" . org-clock-out)
  15905. ("Meta Data Editing")
  15906. ("t" . org-todo)
  15907. ("0" . (org-priority ?\ ))
  15908. ("1" . (org-priority ?A))
  15909. ("2" . (org-priority ?B))
  15910. ("3" . (org-priority ?C))
  15911. (";" . org-set-tags-command)
  15912. ("e" . org-set-effort)
  15913. ("Agenda Views etc")
  15914. ("v" . org-agenda)
  15915. ("/" . org-sparse-tree)
  15916. ("Misc")
  15917. ("o" . org-open-at-point)
  15918. ("?" . org-speed-command-help)
  15919. ("<" . (org-agenda-set-restriction-lock 'subtree))
  15920. (">" . (org-agenda-remove-restriction-lock))
  15921. )
  15922. "The default speed commands.")
  15923. (defun org-print-speed-command (e)
  15924. (if (> (length (car e)) 1)
  15925. (progn
  15926. (princ "\n")
  15927. (princ (car e))
  15928. (princ "\n")
  15929. (princ (make-string (length (car e)) ?-))
  15930. (princ "\n"))
  15931. (princ (car e))
  15932. (princ " ")
  15933. (if (symbolp (cdr e))
  15934. (princ (symbol-name (cdr e)))
  15935. (prin1 (cdr e)))
  15936. (princ "\n")))
  15937. (defun org-speed-command-help ()
  15938. "Show the available speed commands."
  15939. (interactive)
  15940. (if (not org-use-speed-commands)
  15941. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  15942. (with-output-to-temp-buffer "*Help*"
  15943. (princ "User-defined Speed commands\n===========================\n")
  15944. (mapc 'org-print-speed-command org-speed-commands-user)
  15945. (princ "\n")
  15946. (princ "Built-in Speed commands\n=======================\n")
  15947. (mapc 'org-print-speed-command org-speed-commands-default))
  15948. (with-current-buffer "*Help*"
  15949. (setq truncate-lines t))))
  15950. (defun org-speed-move-safe (cmd)
  15951. "Execute CMD, but make sure that the cursor always ends up in a headline.
  15952. If not, return to the original position and throw an error."
  15953. (interactive)
  15954. (let ((pos (point)))
  15955. (call-interactively cmd)
  15956. (unless (and (bolp) (org-on-heading-p))
  15957. (goto-char pos)
  15958. (error "Boundary reached while executing %s" cmd))))
  15959. (defvar org-self-insert-command-undo-counter 0)
  15960. (defvar org-table-auto-blank-field) ; defined in org-table.el
  15961. (defvar org-speed-command nil)
  15962. (defun org-speed-command-default-hook (keys)
  15963. "Hook for activating single-letter speed commands.
  15964. `org-speed-commands-default' specifies a minimal command set.
  15965. Use `org-speed-commands-user' for further customization."
  15966. (when (or (and (bolp) (looking-at org-outline-regexp))
  15967. (and (functionp org-use-speed-commands)
  15968. (funcall org-use-speed-commands)))
  15969. (cdr (assoc keys (append org-speed-commands-user
  15970. org-speed-commands-default)))))
  15971. (defun org-babel-speed-command-hook (keys)
  15972. "Hook for activating single-letter code block commands."
  15973. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  15974. (cdr (assoc keys org-babel-key-bindings))))
  15975. (defcustom org-speed-command-hook
  15976. '(org-speed-command-default-hook org-babel-speed-command-hook)
  15977. "Hook for activating speed commands at strategic locations.
  15978. Hook functions are called in sequence until a valid handler is
  15979. found.
  15980. Each hook takes a single argument, a user-pressed command key
  15981. which is also a `self-insert-command' from the global map.
  15982. Within the hook, examine the cursor position and the command key
  15983. and return nil or a valid handler as appropriate. Handler could
  15984. be one of an interactive command, a function, or a form.
  15985. Set `org-use-speed-commands' to non-nil value to enable this
  15986. hook. The default setting is `org-speed-command-default-hook'."
  15987. :group 'org-structure
  15988. :type 'hook)
  15989. (defun org-self-insert-command (N)
  15990. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  15991. If the cursor is in a table looking at whitespace, the whitespace is
  15992. overwritten, and the table is not marked as requiring realignment."
  15993. (interactive "p")
  15994. (org-check-before-invisible-edit 'insert)
  15995. (cond
  15996. ((and org-use-speed-commands
  15997. (setq org-speed-command
  15998. (run-hook-with-args-until-success
  15999. 'org-speed-command-hook (this-command-keys))))
  16000. (cond
  16001. ((commandp org-speed-command)
  16002. (setq this-command org-speed-command)
  16003. (call-interactively org-speed-command))
  16004. ((functionp org-speed-command)
  16005. (funcall org-speed-command))
  16006. ((and org-speed-command (listp org-speed-command))
  16007. (eval org-speed-command))
  16008. (t (let (org-use-speed-commands)
  16009. (call-interactively 'org-self-insert-command)))))
  16010. ((and
  16011. (org-table-p)
  16012. (progn
  16013. ;; check if we blank the field, and if that triggers align
  16014. (and (featurep 'org-table) org-table-auto-blank-field
  16015. (member last-command
  16016. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16017. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16018. ;; got extra space, this field does not determine column width
  16019. (let (org-table-may-need-update) (org-table-blank-field))
  16020. ;; no extra space, this field may determine column width
  16021. (org-table-blank-field)))
  16022. t)
  16023. (eq N 1)
  16024. (looking-at "[^|\n]* |"))
  16025. (let (org-table-may-need-update)
  16026. (goto-char (1- (match-end 0)))
  16027. (delete-char -1)
  16028. (goto-char (match-beginning 0))
  16029. (self-insert-command N)))
  16030. (t
  16031. (setq org-table-may-need-update t)
  16032. (self-insert-command N)
  16033. (org-fix-tags-on-the-fly)
  16034. (if org-self-insert-cluster-for-undo
  16035. (if (not (eq last-command 'org-self-insert-command))
  16036. (setq org-self-insert-command-undo-counter 1)
  16037. (if (>= org-self-insert-command-undo-counter 20)
  16038. (setq org-self-insert-command-undo-counter 1)
  16039. (and (> org-self-insert-command-undo-counter 0)
  16040. buffer-undo-list (listp buffer-undo-list)
  16041. (not (cadr buffer-undo-list)) ; remove nil entry
  16042. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16043. (setq org-self-insert-command-undo-counter
  16044. (1+ org-self-insert-command-undo-counter))))))))
  16045. (defun org-check-before-invisible-edit (kind)
  16046. "Check is editing if kind KIND would be dangerous with invisible text around.
  16047. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16048. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16049. (if (and org-catch-invisible-edits
  16050. (or (not (boundp 'visible-mode)) (not visible-mode))
  16051. (or (get-char-property (point) 'invisible)
  16052. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16053. ;; OK, we need to take a closer look
  16054. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16055. (invisible-before-point (if (bobp) nil (get-char-property
  16056. (1- (point)) 'invisible)))
  16057. (border-and-ok-direction
  16058. (or
  16059. ;; Check if we are acting predictably before invisible text
  16060. (and invisible-at-point (not invisible-before-point)
  16061. (memq kind '(insert delete-backward)))
  16062. ;; Check if we are acting predictably after invisible text
  16063. ;; This works not well, and I have turned it off. It seems
  16064. ;; better to always show and stop after invisible text.
  16065. ;; (and (not invisible-at-point) invisible-before-point
  16066. ;; (memq kind '(insert delete)))
  16067. )))
  16068. (when (or (memq invisible-at-point '(outline org-hide-block))
  16069. (memq invisible-before-point '(outline org-hide-block)))
  16070. (if (eq org-catch-invisible-edits 'error)
  16071. (error "Editing in invisible areas is prohibited - make visible first"))
  16072. ;; Make the area visible
  16073. (save-excursion
  16074. (if invisible-before-point
  16075. (goto-char (previous-single-char-property-change
  16076. (point) 'invisible)))
  16077. (org-cycle))
  16078. (cond
  16079. ((eq org-catch-invisible-edits 'show)
  16080. ;; That's it, we do the edit after showing
  16081. (message
  16082. "Unfolding invisible region around point before editing")
  16083. (sit-for 1))
  16084. ((and (eq org-catch-invisible-edits 'smart)
  16085. border-and-ok-direction)
  16086. (message "Unfolding invisible region around point before editing"))
  16087. (t
  16088. ;; Don't do the edit, make the user repeat it in full visibility
  16089. (error "Edit in invisible region aborted, repeat to confirm with text visible")))))))
  16090. (defun org-fix-tags-on-the-fly ()
  16091. (when (and (equal (char-after (point-at-bol)) ?*)
  16092. (org-on-heading-p))
  16093. (org-align-tags-here org-tags-column)))
  16094. (defun org-delete-backward-char (N)
  16095. "Like `delete-backward-char', insert whitespace at field end in tables.
  16096. When deleting backwards, in tables this function will insert whitespace in
  16097. front of the next \"|\" separator, to keep the table aligned. The table will
  16098. still be marked for re-alignment if the field did fill the entire column,
  16099. because, in this case the deletion might narrow the column."
  16100. (interactive "p")
  16101. (org-check-before-invisible-edit 'delete-backward)
  16102. (if (and (org-table-p)
  16103. (eq N 1)
  16104. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16105. (looking-at ".*?|"))
  16106. (let ((pos (point))
  16107. (noalign (looking-at "[^|\n\r]* |"))
  16108. (c org-table-may-need-update))
  16109. (backward-delete-char N)
  16110. (if (not overwrite-mode)
  16111. (progn
  16112. (skip-chars-forward "^|")
  16113. (insert " ")
  16114. (goto-char (1- pos))))
  16115. ;; noalign: if there were two spaces at the end, this field
  16116. ;; does not determine the width of the column.
  16117. (if noalign (setq org-table-may-need-update c)))
  16118. (backward-delete-char N)
  16119. (org-fix-tags-on-the-fly)))
  16120. (defun org-delete-char (N)
  16121. "Like `delete-char', but insert whitespace at field end in tables.
  16122. When deleting characters, in tables this function will insert whitespace in
  16123. front of the next \"|\" separator, to keep the table aligned. The table will
  16124. still be marked for re-alignment if the field did fill the entire column,
  16125. because, in this case the deletion might narrow the column."
  16126. (interactive "p")
  16127. (org-check-before-invisible-edit 'delete)
  16128. (if (and (org-table-p)
  16129. (not (bolp))
  16130. (not (= (char-after) ?|))
  16131. (eq N 1))
  16132. (if (looking-at ".*?|")
  16133. (let ((pos (point))
  16134. (noalign (looking-at "[^|\n\r]* |"))
  16135. (c org-table-may-need-update))
  16136. (replace-match (concat
  16137. (substring (match-string 0) 1 -1)
  16138. " |"))
  16139. (goto-char pos)
  16140. ;; noalign: if there were two spaces at the end, this field
  16141. ;; does not determine the width of the column.
  16142. (if noalign (setq org-table-may-need-update c)))
  16143. (delete-char N))
  16144. (delete-char N)
  16145. (org-fix-tags-on-the-fly)))
  16146. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16147. (put 'org-self-insert-command 'delete-selection t)
  16148. (put 'orgtbl-self-insert-command 'delete-selection t)
  16149. (put 'org-delete-char 'delete-selection 'supersede)
  16150. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16151. (put 'org-yank 'delete-selection 'yank)
  16152. ;; Make `flyspell-mode' delay after some commands
  16153. (put 'org-self-insert-command 'flyspell-delayed t)
  16154. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16155. (put 'org-delete-char 'flyspell-delayed t)
  16156. (put 'org-delete-backward-char 'flyspell-delayed t)
  16157. ;; Make pabbrev-mode expand after org-mode commands
  16158. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16159. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16160. ;; How to do this: Measure non-white length of current string
  16161. ;; If equal to column width, we should realign.
  16162. (defun org-remap (map &rest commands)
  16163. "In MAP, remap the functions given in COMMANDS.
  16164. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16165. (let (new old)
  16166. (while commands
  16167. (setq old (pop commands) new (pop commands))
  16168. (if (fboundp 'command-remapping)
  16169. (org-defkey map (vector 'remap old) new)
  16170. (substitute-key-definition old new map global-map)))))
  16171. (when (eq org-enable-table-editor 'optimized)
  16172. ;; If the user wants maximum table support, we need to hijack
  16173. ;; some standard editing functions
  16174. (org-remap org-mode-map
  16175. 'self-insert-command 'org-self-insert-command
  16176. 'delete-char 'org-delete-char
  16177. 'delete-backward-char 'org-delete-backward-char)
  16178. (org-defkey org-mode-map "|" 'org-force-self-insert))
  16179. (defvar org-ctrl-c-ctrl-c-hook nil
  16180. "Hook for functions attaching themselves to `C-c C-c'.
  16181. This can be used to add additional functionality to the C-c C-c
  16182. key which executes context-dependent commands. This hook is run
  16183. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  16184. run after the last test.
  16185. Each function will be called with no arguments. The function
  16186. must check if the context is appropriate for it to act. If yes,
  16187. it should do its thing and then return a non-nil value. If the
  16188. context is wrong, just do nothing and return nil.")
  16189. (defvar org-ctrl-c-ctrl-c-final-hook nil
  16190. "Hook for functions attaching themselves to `C-c C-c'.
  16191. This can be used to add additional functionality to the C-c C-c
  16192. key which executes context-dependent commands. This hook is run
  16193. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  16194. before the first test.
  16195. Each function will be called with no arguments. The function
  16196. must check if the context is appropriate for it to act. If yes,
  16197. it should do its thing and then return a non-nil value. If the
  16198. context is wrong, just do nothing and return nil.")
  16199. (defvar org-tab-first-hook nil
  16200. "Hook for functions to attach themselves to TAB.
  16201. See `org-ctrl-c-ctrl-c-hook' for more information.
  16202. This hook runs as the first action when TAB is pressed, even before
  16203. `org-cycle' messes around with the `outline-regexp' to cater for
  16204. inline tasks and plain list item folding.
  16205. If any function in this hook returns t, any other actions that
  16206. would have been caused by TAB (such as table field motion or visibility
  16207. cycling) will not occur.")
  16208. (defvar org-tab-after-check-for-table-hook nil
  16209. "Hook for functions to attach themselves to TAB.
  16210. See `org-ctrl-c-ctrl-c-hook' for more information.
  16211. This hook runs after it has been established that the cursor is not in a
  16212. table, but before checking if the cursor is in a headline or if global cycling
  16213. should be done.
  16214. If any function in this hook returns t, not other actions like visibility
  16215. cycling will be done.")
  16216. (defvar org-tab-after-check-for-cycling-hook nil
  16217. "Hook for functions to attach themselves to TAB.
  16218. See `org-ctrl-c-ctrl-c-hook' for more information.
  16219. This hook runs after it has been established that not table field motion and
  16220. not visibility should be done because of current context. This is probably
  16221. the place where a package like yasnippets can hook in.")
  16222. (defvar org-tab-before-tab-emulation-hook nil
  16223. "Hook for functions to attach themselves to TAB.
  16224. See `org-ctrl-c-ctrl-c-hook' for more information.
  16225. This hook runs after every other options for TAB have been exhausted, but
  16226. before indentation and \t insertion takes place.")
  16227. (defvar org-metaleft-hook nil
  16228. "Hook for functions attaching themselves to `M-left'.
  16229. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16230. (defvar org-metaright-hook nil
  16231. "Hook for functions attaching themselves to `M-right'.
  16232. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16233. (defvar org-metaup-hook nil
  16234. "Hook for functions attaching themselves to `M-up'.
  16235. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16236. (defvar org-metadown-hook nil
  16237. "Hook for functions attaching themselves to `M-down'.
  16238. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16239. (defvar org-shiftmetaleft-hook nil
  16240. "Hook for functions attaching themselves to `M-S-left'.
  16241. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16242. (defvar org-shiftmetaright-hook nil
  16243. "Hook for functions attaching themselves to `M-S-right'.
  16244. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16245. (defvar org-shiftmetaup-hook nil
  16246. "Hook for functions attaching themselves to `M-S-up'.
  16247. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16248. (defvar org-shiftmetadown-hook nil
  16249. "Hook for functions attaching themselves to `M-S-down'.
  16250. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16251. (defvar org-metareturn-hook nil
  16252. "Hook for functions attaching themselves to `M-RET'.
  16253. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16254. (defvar org-shiftup-hook nil
  16255. "Hook for functions attaching themselves to `S-up'.
  16256. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16257. (defvar org-shiftup-final-hook nil
  16258. "Hook for functions attaching themselves to `S-up'.
  16259. This one runs after all other options except shift-select have been excluded.
  16260. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16261. (defvar org-shiftdown-hook nil
  16262. "Hook for functions attaching themselves to `S-down'.
  16263. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16264. (defvar org-shiftdown-final-hook nil
  16265. "Hook for functions attaching themselves to `S-down'.
  16266. This one runs after all other options except shift-select have been excluded.
  16267. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16268. (defvar org-shiftleft-hook nil
  16269. "Hook for functions attaching themselves to `S-left'.
  16270. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16271. (defvar org-shiftleft-final-hook nil
  16272. "Hook for functions attaching themselves to `S-left'.
  16273. This one runs after all other options except shift-select have been excluded.
  16274. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16275. (defvar org-shiftright-hook nil
  16276. "Hook for functions attaching themselves to `S-right'.
  16277. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16278. (defvar org-shiftright-final-hook nil
  16279. "Hook for functions attaching themselves to `S-right'.
  16280. This one runs after all other options except shift-select have been excluded.
  16281. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16282. (defun org-modifier-cursor-error ()
  16283. "Throw an error, a modified cursor command was applied in wrong context."
  16284. (error "This command is active in special context like tables, headlines or items"))
  16285. (defun org-shiftselect-error ()
  16286. "Throw an error because Shift-Cursor command was applied in wrong context."
  16287. (if (and (boundp 'shift-select-mode) shift-select-mode)
  16288. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  16289. (error "This command works only in special context like headlines or timestamps")))
  16290. (defun org-call-for-shift-select (cmd)
  16291. (let ((this-command-keys-shift-translated t))
  16292. (call-interactively cmd)))
  16293. (defun org-shifttab (&optional arg)
  16294. "Global visibility cycling or move to previous table field.
  16295. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  16296. on context.
  16297. See the individual commands for more information."
  16298. (interactive "P")
  16299. (cond
  16300. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  16301. ((integerp arg)
  16302. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  16303. (message "Content view to level: %d" arg)
  16304. (org-content (prefix-numeric-value arg2))
  16305. (setq org-cycle-global-status 'overview)))
  16306. (t (call-interactively 'org-global-cycle))))
  16307. (defun org-shiftmetaleft ()
  16308. "Promote subtree or delete table column.
  16309. Calls `org-promote-subtree', `org-outdent-item',
  16310. or `org-table-delete-column', depending on context.
  16311. See the individual commands for more information."
  16312. (interactive)
  16313. (cond
  16314. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  16315. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  16316. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  16317. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  16318. (t (org-modifier-cursor-error))))
  16319. (defun org-shiftmetaright ()
  16320. "Demote subtree or insert table column.
  16321. Calls `org-demote-subtree', `org-indent-item',
  16322. or `org-table-insert-column', depending on context.
  16323. See the individual commands for more information."
  16324. (interactive)
  16325. (cond
  16326. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  16327. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  16328. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  16329. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  16330. (t (org-modifier-cursor-error))))
  16331. (defun org-shiftmetaup (&optional arg)
  16332. "Move subtree up or kill table row.
  16333. Calls `org-move-subtree-up' or `org-table-kill-row' or
  16334. `org-move-item-up' depending on context. See the individual commands
  16335. for more information."
  16336. (interactive "P")
  16337. (cond
  16338. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  16339. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  16340. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  16341. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16342. (t (org-modifier-cursor-error))))
  16343. (defun org-shiftmetadown (&optional arg)
  16344. "Move subtree down or insert table row.
  16345. Calls `org-move-subtree-down' or `org-table-insert-row' or
  16346. `org-move-item-down', depending on context. See the individual
  16347. commands for more information."
  16348. (interactive "P")
  16349. (cond
  16350. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  16351. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  16352. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  16353. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16354. (t (org-modifier-cursor-error))))
  16355. (defsubst org-hidden-tree-error ()
  16356. (error
  16357. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  16358. (defun org-metaleft (&optional arg)
  16359. "Promote heading or move table column to left.
  16360. Calls `org-do-promote' or `org-table-move-column', depending on context.
  16361. With no specific context, calls the Emacs default `backward-word'.
  16362. See the individual commands for more information."
  16363. (interactive "P")
  16364. (cond
  16365. ((run-hook-with-args-until-success 'org-metaleft-hook))
  16366. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  16367. ((org-with-limited-levels
  16368. (or (org-on-heading-p)
  16369. (and (org-region-active-p)
  16370. (save-excursion
  16371. (goto-char (region-beginning))
  16372. (org-on-heading-p)))))
  16373. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16374. (call-interactively 'org-do-promote))
  16375. ;; At an inline task.
  16376. ((org-on-heading-p)
  16377. (call-interactively 'org-inlinetask-promote))
  16378. ((or (org-at-item-p)
  16379. (and (org-region-active-p)
  16380. (save-excursion
  16381. (goto-char (region-beginning))
  16382. (org-at-item-p))))
  16383. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16384. (call-interactively 'org-outdent-item))
  16385. (t (call-interactively 'backward-word))))
  16386. (defun org-metaright (&optional arg)
  16387. "Demote subtree or move table column to right.
  16388. Calls `org-do-demote' or `org-table-move-column', depending on context.
  16389. With no specific context, calls the Emacs default `forward-word'.
  16390. See the individual commands for more information."
  16391. (interactive "P")
  16392. (cond
  16393. ((run-hook-with-args-until-success 'org-metaright-hook))
  16394. ((org-at-table-p) (call-interactively 'org-table-move-column))
  16395. ((org-with-limited-levels
  16396. (or (org-on-heading-p)
  16397. (and (org-region-active-p)
  16398. (save-excursion
  16399. (goto-char (region-beginning))
  16400. (org-on-heading-p)))))
  16401. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16402. (call-interactively 'org-do-demote))
  16403. ;; At an inline task.
  16404. ((org-on-heading-p)
  16405. (call-interactively 'org-inlinetask-demote))
  16406. ((or (org-at-item-p)
  16407. (and (org-region-active-p)
  16408. (save-excursion
  16409. (goto-char (region-beginning))
  16410. (org-at-item-p))))
  16411. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16412. (call-interactively 'org-indent-item))
  16413. (t (call-interactively 'forward-word))))
  16414. (defun org-check-for-hidden (what)
  16415. "Check if there are hidden headlines/items in the current visual line.
  16416. WHAT can be either `headlines' or `items'. If the current line is
  16417. an outline or item heading and it has a folded subtree below it,
  16418. this function returns t, nil otherwise."
  16419. (let ((re (cond
  16420. ((eq what 'headlines) org-outline-regexp-bol)
  16421. ((eq what 'items) (org-item-beginning-re))
  16422. (t (error "This should not happen"))))
  16423. beg end)
  16424. (save-excursion
  16425. (catch 'exit
  16426. (unless (org-region-active-p)
  16427. (setq beg (point-at-bol))
  16428. (beginning-of-line 2)
  16429. (while (and (not (eobp)) ;; this is like `next-line'
  16430. (get-char-property (1- (point)) 'invisible))
  16431. (beginning-of-line 2))
  16432. (setq end (point))
  16433. (goto-char beg)
  16434. (goto-char (point-at-eol))
  16435. (setq end (max end (point)))
  16436. (while (re-search-forward re end t)
  16437. (if (get-char-property (match-beginning 0) 'invisible)
  16438. (throw 'exit t))))
  16439. nil))))
  16440. (defun org-metaup (&optional arg)
  16441. "Move subtree up or move table row up.
  16442. Calls `org-move-subtree-up' or `org-table-move-row' or
  16443. `org-move-item-up', depending on context. See the individual commands
  16444. for more information."
  16445. (interactive "P")
  16446. (cond
  16447. ((run-hook-with-args-until-success 'org-metaup-hook))
  16448. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  16449. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  16450. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16451. (t (transpose-lines 1) (beginning-of-line -1))))
  16452. (defun org-metadown (&optional arg)
  16453. "Move subtree down or move table row down.
  16454. Calls `org-move-subtree-down' or `org-table-move-row' or
  16455. `org-move-item-down', depending on context. See the individual
  16456. commands for more information."
  16457. (interactive "P")
  16458. (cond
  16459. ((run-hook-with-args-until-success 'org-metadown-hook))
  16460. ((org-at-table-p) (call-interactively 'org-table-move-row))
  16461. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  16462. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16463. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  16464. (defun org-shiftup (&optional arg)
  16465. "Increase item in timestamp or increase priority of current headline.
  16466. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  16467. depending on context. See the individual commands for more information."
  16468. (interactive "P")
  16469. (cond
  16470. ((run-hook-with-args-until-success 'org-shiftup-hook))
  16471. ((and org-support-shift-select (org-region-active-p))
  16472. (org-call-for-shift-select 'previous-line))
  16473. ((org-at-timestamp-p t)
  16474. (call-interactively (if org-edit-timestamp-down-means-later
  16475. 'org-timestamp-down 'org-timestamp-up)))
  16476. ((and (not (eq org-support-shift-select 'always))
  16477. org-enable-priority-commands
  16478. (org-on-heading-p))
  16479. (call-interactively 'org-priority-up))
  16480. ((and (not org-support-shift-select) (org-at-item-p))
  16481. (call-interactively 'org-previous-item))
  16482. ((org-clocktable-try-shift 'up arg))
  16483. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  16484. (org-support-shift-select
  16485. (org-call-for-shift-select 'previous-line))
  16486. (t (org-shiftselect-error))))
  16487. (defun org-shiftdown (&optional arg)
  16488. "Decrease item in timestamp or decrease priority of current headline.
  16489. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  16490. depending on context. See the individual commands for more information."
  16491. (interactive "P")
  16492. (cond
  16493. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  16494. ((and org-support-shift-select (org-region-active-p))
  16495. (org-call-for-shift-select 'next-line))
  16496. ((org-at-timestamp-p t)
  16497. (call-interactively (if org-edit-timestamp-down-means-later
  16498. 'org-timestamp-up 'org-timestamp-down)))
  16499. ((and (not (eq org-support-shift-select 'always))
  16500. org-enable-priority-commands
  16501. (org-on-heading-p))
  16502. (call-interactively 'org-priority-down))
  16503. ((and (not org-support-shift-select) (org-at-item-p))
  16504. (call-interactively 'org-next-item))
  16505. ((org-clocktable-try-shift 'down arg))
  16506. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  16507. (org-support-shift-select
  16508. (org-call-for-shift-select 'next-line))
  16509. (t (org-shiftselect-error))))
  16510. (defun org-shiftright (&optional arg)
  16511. "Cycle the thing at point or in the current line, depending on context.
  16512. Depending on context, this does one of the following:
  16513. - switch a timestamp at point one day into the future
  16514. - on a headline, switch to the next TODO keyword.
  16515. - on an item, switch entire list to the next bullet type
  16516. - on a property line, switch to the next allowed value
  16517. - on a clocktable definition line, move time block into the future"
  16518. (interactive "P")
  16519. (cond
  16520. ((run-hook-with-args-until-success 'org-shiftright-hook))
  16521. ((and org-support-shift-select (org-region-active-p))
  16522. (org-call-for-shift-select 'forward-char))
  16523. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  16524. ((and (not (eq org-support-shift-select 'always))
  16525. (org-on-heading-p))
  16526. (let ((org-inhibit-logging
  16527. (not org-treat-S-cursor-todo-selection-as-state-change))
  16528. (org-inhibit-blocking
  16529. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16530. (org-call-with-arg 'org-todo 'right)))
  16531. ((or (and org-support-shift-select
  16532. (not (eq org-support-shift-select 'always))
  16533. (org-at-item-bullet-p))
  16534. (and (not org-support-shift-select) (org-at-item-p)))
  16535. (org-call-with-arg 'org-cycle-list-bullet nil))
  16536. ((and (not (eq org-support-shift-select 'always))
  16537. (org-at-property-p))
  16538. (call-interactively 'org-property-next-allowed-value))
  16539. ((org-clocktable-try-shift 'right arg))
  16540. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  16541. (org-support-shift-select
  16542. (org-call-for-shift-select 'forward-char))
  16543. (t (org-shiftselect-error))))
  16544. (defun org-shiftleft (&optional arg)
  16545. "Cycle the thing at point or in the current line, depending on context.
  16546. Depending on context, this does one of the following:
  16547. - switch a timestamp at point one day into the past
  16548. - on a headline, switch to the previous TODO keyword.
  16549. - on an item, switch entire list to the previous bullet type
  16550. - on a property line, switch to the previous allowed value
  16551. - on a clocktable definition line, move time block into the past"
  16552. (interactive "P")
  16553. (cond
  16554. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  16555. ((and org-support-shift-select (org-region-active-p))
  16556. (org-call-for-shift-select 'backward-char))
  16557. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  16558. ((and (not (eq org-support-shift-select 'always))
  16559. (org-on-heading-p))
  16560. (let ((org-inhibit-logging
  16561. (not org-treat-S-cursor-todo-selection-as-state-change))
  16562. (org-inhibit-blocking
  16563. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16564. (org-call-with-arg 'org-todo 'left)))
  16565. ((or (and org-support-shift-select
  16566. (not (eq org-support-shift-select 'always))
  16567. (org-at-item-bullet-p))
  16568. (and (not org-support-shift-select) (org-at-item-p)))
  16569. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  16570. ((and (not (eq org-support-shift-select 'always))
  16571. (org-at-property-p))
  16572. (call-interactively 'org-property-previous-allowed-value))
  16573. ((org-clocktable-try-shift 'left arg))
  16574. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  16575. (org-support-shift-select
  16576. (org-call-for-shift-select 'backward-char))
  16577. (t (org-shiftselect-error))))
  16578. (defun org-shiftcontrolright ()
  16579. "Switch to next TODO set."
  16580. (interactive)
  16581. (cond
  16582. ((and org-support-shift-select (org-region-active-p))
  16583. (org-call-for-shift-select 'forward-word))
  16584. ((and (not (eq org-support-shift-select 'always))
  16585. (org-on-heading-p))
  16586. (org-call-with-arg 'org-todo 'nextset))
  16587. (org-support-shift-select
  16588. (org-call-for-shift-select 'forward-word))
  16589. (t (org-shiftselect-error))))
  16590. (defun org-shiftcontrolleft ()
  16591. "Switch to previous TODO set."
  16592. (interactive)
  16593. (cond
  16594. ((and org-support-shift-select (org-region-active-p))
  16595. (org-call-for-shift-select 'backward-word))
  16596. ((and (not (eq org-support-shift-select 'always))
  16597. (org-on-heading-p))
  16598. (org-call-with-arg 'org-todo 'previousset))
  16599. (org-support-shift-select
  16600. (org-call-for-shift-select 'backward-word))
  16601. (t (org-shiftselect-error))))
  16602. (defun org-shiftcontrolup ()
  16603. "Change timestamps synchronously up in CLOCK log lines."
  16604. (interactive)
  16605. (cond ((and (not org-support-shift-select)
  16606. (org-at-clock-log-p)
  16607. (org-at-timestamp-p t))
  16608. (org-clock-timestamps-up))
  16609. (t (org-shiftselect-error))))
  16610. (defun org-shiftcontroldown ()
  16611. "Change timestamps synchronously down in CLOCK log lines."
  16612. (interactive)
  16613. (cond ((and (not org-support-shift-select)
  16614. (org-at-clock-log-p)
  16615. (org-at-timestamp-p t))
  16616. (org-clock-timestamps-down))
  16617. (t (org-shiftselect-error))))
  16618. (defun org-ctrl-c-ret ()
  16619. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  16620. (interactive)
  16621. (cond
  16622. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  16623. (t (call-interactively 'org-insert-heading))))
  16624. (defun org-find-visible ()
  16625. (let ((s (point)))
  16626. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16627. (get-char-property s 'invisible)))
  16628. s))
  16629. (defun org-find-invisible ()
  16630. (let ((s (point)))
  16631. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16632. (not (get-char-property s 'invisible))))
  16633. s))
  16634. (defun org-copy-visible (beg end)
  16635. "Copy the visible parts of the region."
  16636. (interactive "r")
  16637. (let (snippets s)
  16638. (save-excursion
  16639. (save-restriction
  16640. (narrow-to-region beg end)
  16641. (setq s (goto-char (point-min)))
  16642. (while (not (= (point) (point-max)))
  16643. (goto-char (org-find-invisible))
  16644. (push (buffer-substring s (point)) snippets)
  16645. (setq s (goto-char (org-find-visible))))))
  16646. (kill-new (apply 'concat (nreverse snippets)))))
  16647. (defun org-copy-special ()
  16648. "Copy region in table or copy current subtree.
  16649. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  16650. See the individual commands for more information."
  16651. (interactive)
  16652. (call-interactively
  16653. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  16654. (defun org-cut-special ()
  16655. "Cut region in table or cut current subtree.
  16656. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  16657. See the individual commands for more information."
  16658. (interactive)
  16659. (call-interactively
  16660. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  16661. (defun org-paste-special (arg)
  16662. "Paste rectangular region into table, or past subtree relative to level.
  16663. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  16664. See the individual commands for more information."
  16665. (interactive "P")
  16666. (if (org-at-table-p)
  16667. (org-table-paste-rectangle)
  16668. (org-paste-subtree arg)))
  16669. (defun org-edit-special (&optional arg)
  16670. "Call a special editor for the stuff at point.
  16671. When at a table, call the formula editor with `org-table-edit-formulas'.
  16672. When at the first line of an src example, call `org-edit-src-code'.
  16673. When in an #+include line, visit the include file. Otherwise call
  16674. `ffap' to visit the file at point."
  16675. (interactive)
  16676. ;; possibly prep session before editing source
  16677. (when arg
  16678. (let* ((info (org-babel-get-src-block-info))
  16679. (lang (nth 0 info))
  16680. (params (nth 2 info))
  16681. (session (cdr (assoc :session params))))
  16682. (when (and info session) ;; we are in a source-code block with a session
  16683. (funcall
  16684. (intern (concat "org-babel-prep-session:" lang)) session params))))
  16685. (cond ;; proceed with `org-edit-special'
  16686. ((save-excursion
  16687. (beginning-of-line 1)
  16688. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  16689. (find-file (org-trim (match-string 1))))
  16690. ((org-edit-src-code))
  16691. ((org-edit-fixed-width-region))
  16692. ((org-at-table.el-p)
  16693. (org-edit-src-code))
  16694. ((or (org-at-table-p)
  16695. (save-excursion
  16696. (beginning-of-line 1)
  16697. (looking-at "[ \t]*#\\+TBLFM:")))
  16698. (call-interactively 'org-table-edit-formulas))
  16699. (t (call-interactively 'ffap))))
  16700. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  16701. (defun org-ctrl-c-ctrl-c (&optional arg)
  16702. "Set tags in headline, or update according to changed information at point.
  16703. This command does many different things, depending on context:
  16704. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  16705. this is what we do.
  16706. - If the cursor is on a statistics cookie, update it.
  16707. - If the cursor is in a headline, prompt for tags and insert them
  16708. into the current line, aligned to `org-tags-column'. When called
  16709. with prefix arg, realign all tags in the current buffer.
  16710. - If the cursor is in one of the special #+KEYWORD lines, this
  16711. triggers scanning the buffer for these lines and updating the
  16712. information.
  16713. - If the cursor is inside a table, realign the table. This command
  16714. works even if the automatic table editor has been turned off.
  16715. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  16716. the entire table.
  16717. - If the cursor is at a footnote reference or definition, jump to
  16718. the corresponding definition or references, respectively.
  16719. - If the cursor is a the beginning of a dynamic block, update it.
  16720. - If the current buffer is a capture buffer, close note and file it.
  16721. - If the cursor is on a <<<target>>>, update radio targets and
  16722. corresponding links in this buffer.
  16723. - If the cursor is on a numbered item in a plain list, renumber the
  16724. ordered list.
  16725. - If the cursor is on a checkbox, toggle it.
  16726. - If the cursor is on a code block, evaluate it. The variable
  16727. `org-confirm-babel-evaluate' can be used to control prompting
  16728. before code block evaluation, by default every code block
  16729. evaluation requires confirmation. Code block evaluation can be
  16730. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  16731. (interactive "P")
  16732. (let ((org-enable-table-editor t))
  16733. (cond
  16734. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  16735. org-occur-highlights
  16736. org-latex-fragment-image-overlays)
  16737. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  16738. (org-remove-occur-highlights)
  16739. (org-remove-latex-fragment-image-overlays)
  16740. (message "Temporary highlights/overlays removed from current buffer"))
  16741. ((and (local-variable-p 'org-finish-function (current-buffer))
  16742. (fboundp org-finish-function))
  16743. (funcall org-finish-function))
  16744. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  16745. ((org-in-regexp org-ts-regexp-both)
  16746. (org-timestamp-change 0 'day))
  16747. ((or (looking-at org-property-start-re)
  16748. (org-at-property-p))
  16749. (call-interactively 'org-property-action))
  16750. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  16751. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  16752. (or (org-on-heading-p) (org-at-item-p)))
  16753. (call-interactively 'org-update-statistics-cookies))
  16754. ((org-on-heading-p) (call-interactively 'org-set-tags))
  16755. ((org-at-table.el-p)
  16756. (message "Use C-c ' to edit table.el tables"))
  16757. ((org-at-table-p)
  16758. (org-table-maybe-eval-formula)
  16759. (if arg
  16760. (call-interactively 'org-table-recalculate)
  16761. (org-table-maybe-recalculate-line))
  16762. (call-interactively 'org-table-align)
  16763. (orgtbl-send-table 'maybe))
  16764. ((or (org-footnote-at-reference-p)
  16765. (org-footnote-at-definition-p))
  16766. (call-interactively 'org-footnote-action))
  16767. ((org-at-item-checkbox-p)
  16768. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  16769. ;; list only if at top item.
  16770. (let* ((cbox (match-string 1))
  16771. (struct (org-list-struct))
  16772. (old-struct (copy-tree struct))
  16773. (parents (org-list-parents-alist struct))
  16774. (orderedp (org-entry-get nil "ORDERED"))
  16775. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  16776. block-item)
  16777. ;; Use a light version of `org-toggle-checkbox' to avoid
  16778. ;; computing list structure twice.
  16779. (org-list-set-checkbox (point-at-bol) struct
  16780. (cond
  16781. ((equal arg '(16)) "[-]")
  16782. ((equal arg '(4)) nil)
  16783. ((equal "[X]" cbox) "[ ]")
  16784. (t "[X]")))
  16785. ;; Replicate `org-list-write-struct', while grabbing a return
  16786. ;; value from `org-list-struct-fix-box'.
  16787. (org-list-struct-fix-ind struct parents 2)
  16788. (org-list-struct-fix-item-end struct)
  16789. (let ((prevs (org-list-prevs-alist struct)))
  16790. (org-list-struct-fix-bul struct prevs)
  16791. (org-list-struct-fix-ind struct parents)
  16792. (setq block-item
  16793. (org-list-struct-fix-box struct parents prevs orderedp)))
  16794. (org-list-struct-apply-struct struct old-struct)
  16795. (org-update-checkbox-count-maybe)
  16796. (when block-item
  16797. (message
  16798. "Checkboxes were removed due to unchecked box at line %d"
  16799. (org-current-line block-item)))
  16800. (when firstp (org-list-send-list 'maybe))))
  16801. ((org-at-item-p)
  16802. ;; Cursor at an item: repair list. Do checkbox related actions
  16803. ;; only if function was called with an argument. Send list only
  16804. ;; if at top item.
  16805. (let* ((struct (org-list-struct))
  16806. (firstp (= (org-list-get-top-point struct) (point-at-bol))))
  16807. (when arg (org-list-set-checkbox (point-at-bol) struct "[ ]"))
  16808. (org-list-write-struct struct (org-list-parents-alist struct))
  16809. (when arg (org-update-checkbox-count-maybe))
  16810. (when firstp (org-list-send-list 'maybe))))
  16811. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  16812. ;; Dynamic block
  16813. (beginning-of-line 1)
  16814. (save-excursion (org-update-dblock)))
  16815. ((save-excursion
  16816. (beginning-of-line 1)
  16817. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  16818. (cond
  16819. ((equal (match-string 1) "TBLFM")
  16820. ;; Recalculate the table before this line
  16821. (save-excursion
  16822. (beginning-of-line 1)
  16823. (skip-chars-backward " \r\n\t")
  16824. (if (org-at-table-p)
  16825. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  16826. (t
  16827. (let ((org-inhibit-startup-visibility-stuff t)
  16828. (org-startup-align-all-tables nil))
  16829. (when (boundp 'org-table-coordinate-overlays)
  16830. (mapc 'delete-overlay org-table-coordinate-overlays)
  16831. (setq org-table-coordinate-overlays nil))
  16832. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  16833. (message "Local setup has been refreshed"))))
  16834. ((org-clock-update-time-maybe))
  16835. (t
  16836. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  16837. (error "C-c C-c can do nothing useful at this location"))))))
  16838. (defun org-mode-restart ()
  16839. "Restart Org-mode, to scan again for special lines.
  16840. Also updates the keyword regular expressions."
  16841. (interactive)
  16842. (org-mode)
  16843. (message "Org-mode restarted"))
  16844. (defun org-kill-note-or-show-branches ()
  16845. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  16846. (interactive)
  16847. (if (not org-finish-function)
  16848. (progn
  16849. (hide-subtree)
  16850. (call-interactively 'show-branches))
  16851. (let ((org-note-abort t))
  16852. (funcall org-finish-function))))
  16853. (defun org-return (&optional indent)
  16854. "Goto next table row or insert a newline.
  16855. Calls `org-table-next-row' or `newline', depending on context.
  16856. See the individual commands for more information."
  16857. (interactive)
  16858. (cond
  16859. ((bobp) (if indent (newline-and-indent) (newline)))
  16860. ((org-at-table-p)
  16861. (org-table-justify-field-maybe)
  16862. (call-interactively 'org-table-next-row))
  16863. ;; when `newline-and-indent' is called within a list, make sure
  16864. ;; text moved stays inside the item.
  16865. ((and (org-in-item-p) indent)
  16866. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  16867. (progn
  16868. (save-match-data (newline))
  16869. (org-indent-line-to (length (match-string 0))))
  16870. (let ((ind (org-get-indentation)))
  16871. (newline)
  16872. (if (org-looking-back org-list-end-re)
  16873. (org-indent-line-function)
  16874. (org-indent-line-to ind)))))
  16875. ((and org-return-follows-link
  16876. (let ((tprop (get-text-property (point) 'face)))
  16877. (or (eq tprop 'org-link)
  16878. (and (listp tprop) (memq 'org-link tprop)))))
  16879. (call-interactively 'org-open-at-point))
  16880. ((and (org-at-heading-p)
  16881. (looking-at
  16882. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  16883. (org-show-entry)
  16884. (end-of-line 1)
  16885. (newline))
  16886. (t (if indent (newline-and-indent) (newline)))))
  16887. (defun org-return-indent ()
  16888. "Goto next table row or insert a newline and indent.
  16889. Calls `org-table-next-row' or `newline-and-indent', depending on
  16890. context. See the individual commands for more information."
  16891. (interactive)
  16892. (org-return t))
  16893. (defun org-ctrl-c-star ()
  16894. "Compute table, or change heading status of lines.
  16895. Calls `org-table-recalculate' or `org-toggle-heading',
  16896. depending on context."
  16897. (interactive)
  16898. (cond
  16899. ((org-at-table-p)
  16900. (call-interactively 'org-table-recalculate))
  16901. (t
  16902. ;; Convert all lines in region to list items
  16903. (call-interactively 'org-toggle-heading))))
  16904. (defun org-ctrl-c-minus ()
  16905. "Insert separator line in table or modify bullet status of line.
  16906. Also turns a plain line or a region of lines into list items.
  16907. Calls `org-table-insert-hline', `org-toggle-item', or
  16908. `org-cycle-list-bullet', depending on context."
  16909. (interactive)
  16910. (cond
  16911. ((org-at-table-p)
  16912. (call-interactively 'org-table-insert-hline))
  16913. ((org-region-active-p)
  16914. (call-interactively 'org-toggle-item))
  16915. ((org-in-item-p)
  16916. (call-interactively 'org-cycle-list-bullet))
  16917. (t
  16918. (call-interactively 'org-toggle-item))))
  16919. (defun org-toggle-item (arg)
  16920. "Convert headings or normal lines to items, items to normal lines.
  16921. If there is no active region, only the current line is considered.
  16922. If the first non blank line in the region is an headline, convert
  16923. all headlines to items, shifting text accordingly.
  16924. If it is an item, convert all items to normal lines.
  16925. If it is normal text, change region into an item. With a prefix
  16926. argument ARG, change each line in region into an item."
  16927. (interactive "P")
  16928. (let ((shift-text
  16929. (function
  16930. ;; Shift text in current section to IND, from point to END.
  16931. ;; The function leaves point to END line.
  16932. (lambda (ind end)
  16933. (let ((min-i 1000) (end (copy-marker end)))
  16934. ;; First determine the minimum indentation (MIN-I) of
  16935. ;; the text.
  16936. (save-excursion
  16937. (catch 'exit
  16938. (while (< (point) end)
  16939. (let ((i (org-get-indentation)))
  16940. (cond
  16941. ;; Skip blank lines and inline tasks.
  16942. ((looking-at "^[ \t]*$"))
  16943. ((looking-at org-outline-regexp-bol))
  16944. ;; We can't find less than 0 indentation.
  16945. ((zerop i) (throw 'exit (setq min-i 0)))
  16946. ((< i min-i) (setq min-i i))))
  16947. (forward-line))))
  16948. ;; Then indent each line so that a line indented to
  16949. ;; MIN-I becomes indented to IND. Ignore blank lines
  16950. ;; and inline tasks in the process.
  16951. (let ((delta (- ind min-i)))
  16952. (while (< (point) end)
  16953. (unless (or (looking-at "^[ \t]*$")
  16954. (looking-at org-outline-regexp-bol))
  16955. (org-indent-line-to (+ (org-get-indentation) delta)))
  16956. (forward-line)))))))
  16957. (skip-blanks
  16958. (function
  16959. ;; Return beginning of first non-blank line, starting from
  16960. ;; line at POS.
  16961. (lambda (pos)
  16962. (save-excursion
  16963. (goto-char pos)
  16964. (skip-chars-forward " \r\t\n")
  16965. (point-at-bol)))))
  16966. beg end)
  16967. ;; Determine boundaries of changes.
  16968. (if (org-region-active-p)
  16969. (setq beg (funcall skip-blanks (region-beginning))
  16970. end (copy-marker (region-end)))
  16971. (setq beg (funcall skip-blanks (point-at-bol))
  16972. end (copy-marker (point-at-eol))))
  16973. ;; Depending on the starting line, choose an action on the text
  16974. ;; between BEG and END.
  16975. (org-with-limited-levels
  16976. (save-excursion
  16977. (goto-char beg)
  16978. (cond
  16979. ;; Case 1. Start at an item: de-itemize. Note that it only
  16980. ;; happens when a region is active: `org-ctrl-c-minus'
  16981. ;; would call `org-cycle-list-bullet' otherwise.
  16982. ((org-at-item-p)
  16983. (while (< (point) end)
  16984. (when (org-at-item-p)
  16985. (skip-chars-forward " \t")
  16986. (delete-region (point) (match-end 0)))
  16987. (forward-line)))
  16988. ;; Case 2. Start at an heading: convert to items.
  16989. ((org-on-heading-p)
  16990. (let* ((bul (org-list-bullet-string "-"))
  16991. (bul-len (length bul))
  16992. ;; Indentation of the first heading. It should be
  16993. ;; relative to the indentation of its parent, if any.
  16994. (start-ind (save-excursion
  16995. (cond
  16996. ((not org-adapt-indentation) 0)
  16997. ((not (outline-previous-heading)) 0)
  16998. (t (length (match-string 0))))))
  16999. ;; Level of first heading. Further headings will be
  17000. ;; compared to it to determine hierarchy in the list.
  17001. (ref-level (org-reduced-level (org-outline-level))))
  17002. (while (< (point) end)
  17003. (let* ((level (org-reduced-level (org-outline-level)))
  17004. (delta (max 0 (- level ref-level))))
  17005. ;; If current headline is less indented than the first
  17006. ;; one, set it as reference, in order to preserve
  17007. ;; subtrees.
  17008. (when (< level ref-level) (setq ref-level level))
  17009. (replace-match bul t t)
  17010. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17011. ;; Ensure all text down to END (or SECTION-END) belongs
  17012. ;; to the newly created item.
  17013. (let ((section-end (save-excursion
  17014. (or (outline-next-heading) (point)))))
  17015. (forward-line)
  17016. (funcall shift-text
  17017. (+ start-ind (* (1+ delta) bul-len))
  17018. (min end section-end)))))))
  17019. ;; Case 3. Normal line with ARG: turn each non-item line into
  17020. ;; an item.
  17021. (arg
  17022. (while (< (point) end)
  17023. (unless (or (org-on-heading-p) (org-at-item-p))
  17024. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17025. (replace-match
  17026. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17027. (forward-line)))
  17028. ;; Case 4. Normal line without ARG: make the first line of
  17029. ;; region an item, and shift indentation of others
  17030. ;; lines to set them as item's body.
  17031. (t (let* ((bul (org-list-bullet-string "-"))
  17032. (bul-len (length bul))
  17033. (ref-ind (org-get-indentation)))
  17034. (skip-chars-forward " \t")
  17035. (insert bul)
  17036. (forward-line)
  17037. (while (< (point) end)
  17038. ;; Ensure that lines less indented than first one
  17039. ;; still get included in item body.
  17040. (funcall shift-text
  17041. (+ ref-ind bul-len)
  17042. (min end (save-excursion (or (outline-next-heading)
  17043. (point)))))
  17044. (forward-line)))))))))
  17045. (defun org-toggle-heading (&optional nstars)
  17046. "Convert headings to normal text, or items or text to headings.
  17047. If there is no active region, only the current line is considered.
  17048. If the first non blank line is an headline, remove the stars from
  17049. all headlines in the region.
  17050. If it is a plain list item, turn all plain list items into headings.
  17051. If it is a normal line, turn each and every normal line (i.e. not
  17052. an heading or an item) in the region into a heading.
  17053. When converting a line into a heading, the number of stars is chosen
  17054. such that the lines become children of the current entry. However,
  17055. when a prefix argument is given, its value determines the number of
  17056. stars to add."
  17057. (interactive "P")
  17058. (let ((skip-blanks
  17059. (function
  17060. ;; Return beginning of first non-blank line, starting from
  17061. ;; line at POS.
  17062. (lambda (pos)
  17063. (save-excursion
  17064. (goto-char pos)
  17065. (skip-chars-forward " \r\t\n")
  17066. (point-at-bol)))))
  17067. beg end)
  17068. ;; Determine boundaries of changes. If region ends at a bol, do
  17069. ;; not consider the last line to be in the region.
  17070. (if (org-region-active-p)
  17071. (setq beg (funcall skip-blanks (region-beginning))
  17072. end (copy-marker (save-excursion
  17073. (goto-char (region-end))
  17074. (if (bolp) (point) (point-at-eol)))))
  17075. (setq beg (funcall skip-blanks (point-at-bol))
  17076. end (copy-marker (point-at-eol))))
  17077. ;; Ensure inline tasks don't count as headings.
  17078. (org-with-limited-levels
  17079. (save-excursion
  17080. (goto-char beg)
  17081. (cond
  17082. ;; Case 1. Started at an heading: de-star headings.
  17083. ((org-on-heading-p)
  17084. (while (< (point) end)
  17085. (when (org-on-heading-p t)
  17086. (looking-at org-outline-regexp) (replace-match ""))
  17087. (forward-line)))
  17088. ;; Case 2. Started at an item: change items into headlines.
  17089. ;; One star will be added by `org-list-to-subtree'.
  17090. ((org-at-item-p)
  17091. (let* ((stars (make-string
  17092. (if nstars
  17093. ;; subtract the star that will be added again by
  17094. ;; `org-list-to-subtree'
  17095. (1- (prefix-numeric-value current-prefix-arg))
  17096. (or (org-current-level) 0))
  17097. ?*))
  17098. (add-stars
  17099. (cond (nstars "") ; stars from prefix only
  17100. ((equal stars "") "") ; before first heading
  17101. (org-odd-levels-only "*") ; inside heading, odd
  17102. (t "")))) ; inside heading, oddeven
  17103. (while (< (point) end)
  17104. (when (org-at-item-p)
  17105. ;; Pay attention to cases when region ends before list.
  17106. (let* ((struct (org-list-struct))
  17107. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  17108. (save-restriction
  17109. (narrow-to-region (point) list-end)
  17110. (insert
  17111. (org-list-to-subtree
  17112. (org-list-parse-list t)
  17113. '(:istart (concat stars add-stars (funcall get-stars depth))
  17114. :icount (concat stars add-stars (funcall get-stars depth))))))))
  17115. (forward-line))))
  17116. ;; Case 3. Started at normal text: make every line an heading,
  17117. ;; skipping headlines and items.
  17118. (t (let* ((stars (make-string
  17119. (if nstars
  17120. (prefix-numeric-value current-prefix-arg)
  17121. (or (org-current-level) 0))
  17122. ?*))
  17123. (add-stars
  17124. (cond (nstars "") ; stars from prefix only
  17125. ((equal stars "") "*") ; before first heading
  17126. (org-odd-levels-only "**") ; inside heading, odd
  17127. (t "*"))) ; inside heading, oddeven
  17128. (rpl (concat stars add-stars " ")))
  17129. (while (< (point) end)
  17130. (when (and (not (org-on-heading-p)) (not (org-at-item-p))
  17131. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  17132. (replace-match (concat rpl (match-string 2))))
  17133. (forward-line)))))))))
  17134. (defun org-meta-return (&optional arg)
  17135. "Insert a new heading or wrap a region in a table.
  17136. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  17137. See the individual commands for more information."
  17138. (interactive "P")
  17139. (cond
  17140. ((run-hook-with-args-until-success 'org-metareturn-hook))
  17141. ((org-at-table-p)
  17142. (call-interactively 'org-table-wrap-region))
  17143. (t (call-interactively 'org-insert-heading))))
  17144. ;;; Menu entries
  17145. ;; Define the Org-mode menus
  17146. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  17147. '("Tbl"
  17148. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  17149. ["Next Field" org-cycle (org-at-table-p)]
  17150. ["Previous Field" org-shifttab (org-at-table-p)]
  17151. ["Next Row" org-return (org-at-table-p)]
  17152. "--"
  17153. ["Blank Field" org-table-blank-field (org-at-table-p)]
  17154. ["Edit Field" org-table-edit-field (org-at-table-p)]
  17155. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  17156. "--"
  17157. ("Column"
  17158. ["Move Column Left" org-metaleft (org-at-table-p)]
  17159. ["Move Column Right" org-metaright (org-at-table-p)]
  17160. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  17161. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  17162. ("Row"
  17163. ["Move Row Up" org-metaup (org-at-table-p)]
  17164. ["Move Row Down" org-metadown (org-at-table-p)]
  17165. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  17166. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  17167. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  17168. "--"
  17169. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  17170. ("Rectangle"
  17171. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  17172. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  17173. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  17174. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  17175. "--"
  17176. ("Calculate"
  17177. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  17178. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  17179. ["Edit Formulas" org-edit-special (org-at-table-p)]
  17180. "--"
  17181. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  17182. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  17183. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  17184. "--"
  17185. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  17186. "--"
  17187. ["Sum Column/Rectangle" org-table-sum
  17188. (or (org-at-table-p) (org-region-active-p))]
  17189. ["Which Column?" org-table-current-column (org-at-table-p)])
  17190. ["Debug Formulas"
  17191. org-table-toggle-formula-debugger
  17192. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  17193. ["Show Col/Row Numbers"
  17194. org-table-toggle-coordinate-overlays
  17195. :style toggle
  17196. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  17197. "--"
  17198. ["Create" org-table-create (and (not (org-at-table-p))
  17199. org-enable-table-editor)]
  17200. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  17201. ["Import from File" org-table-import (not (org-at-table-p))]
  17202. ["Export to File" org-table-export (org-at-table-p)]
  17203. "--"
  17204. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  17205. (easy-menu-define org-org-menu org-mode-map "Org menu"
  17206. '("Org"
  17207. ("Show/Hide"
  17208. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  17209. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  17210. ["Sparse Tree..." org-sparse-tree t]
  17211. ["Reveal Context" org-reveal t]
  17212. ["Show All" show-all t]
  17213. "--"
  17214. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  17215. "--"
  17216. ["New Heading" org-insert-heading t]
  17217. ("Navigate Headings"
  17218. ["Up" outline-up-heading t]
  17219. ["Next" outline-next-visible-heading t]
  17220. ["Previous" outline-previous-visible-heading t]
  17221. ["Next Same Level" outline-forward-same-level t]
  17222. ["Previous Same Level" outline-backward-same-level t]
  17223. "--"
  17224. ["Jump" org-goto t])
  17225. ("Edit Structure"
  17226. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  17227. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  17228. "--"
  17229. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  17230. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  17231. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  17232. "--"
  17233. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  17234. "--"
  17235. ["Copy visible text" org-copy-visible t]
  17236. "--"
  17237. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  17238. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  17239. ["Demote Heading" org-metaright (not (org-at-table-p))]
  17240. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  17241. "--"
  17242. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  17243. "--"
  17244. ["Convert to odd levels" org-convert-to-odd-levels t]
  17245. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  17246. ("Editing"
  17247. ["Emphasis..." org-emphasize t]
  17248. ["Edit Source Example" org-edit-special t]
  17249. "--"
  17250. ["Footnote new/jump" org-footnote-action t]
  17251. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  17252. ("Archive"
  17253. ["Archive (default method)" org-archive-subtree-default t]
  17254. "--"
  17255. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  17256. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  17257. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  17258. )
  17259. "--"
  17260. ("Hyperlinks"
  17261. ["Store Link (Global)" org-store-link t]
  17262. ["Find existing link to here" org-occur-link-in-agenda-files t]
  17263. ["Insert Link" org-insert-link t]
  17264. ["Follow Link" org-open-at-point t]
  17265. "--"
  17266. ["Next link" org-next-link t]
  17267. ["Previous link" org-previous-link t]
  17268. "--"
  17269. ["Descriptive Links"
  17270. org-toggle-link-display
  17271. :style radio
  17272. :selected org-descriptive-links
  17273. ]
  17274. ["Literal Links"
  17275. org-toggle-link-display
  17276. :style radio
  17277. :selected (not org-descriptive-links)])
  17278. "--"
  17279. ("TODO Lists"
  17280. ["TODO/DONE/-" org-todo t]
  17281. ("Select keyword"
  17282. ["Next keyword" org-shiftright (org-on-heading-p)]
  17283. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  17284. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  17285. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  17286. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  17287. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  17288. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  17289. "--"
  17290. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  17291. :selected org-enforce-todo-dependencies :style toggle :active t]
  17292. "Settings for tree at point"
  17293. ["Do Children sequentially" org-toggle-ordered-property :style radio
  17294. :selected (org-entry-get nil "ORDERED")
  17295. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17296. ["Do Children parallel" org-toggle-ordered-property :style radio
  17297. :selected (not (org-entry-get nil "ORDERED"))
  17298. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17299. "--"
  17300. ["Set Priority" org-priority t]
  17301. ["Priority Up" org-shiftup t]
  17302. ["Priority Down" org-shiftdown t]
  17303. "--"
  17304. ["Get news from all feeds" org-feed-update-all t]
  17305. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  17306. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  17307. ("TAGS and Properties"
  17308. ["Set Tags" org-set-tags-command t]
  17309. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  17310. "--"
  17311. ["Set property" org-set-property t]
  17312. ["Column view of properties" org-columns t]
  17313. ["Insert Column View DBlock" org-insert-columns-dblock t])
  17314. ("Dates and Scheduling"
  17315. ["Timestamp" org-time-stamp t]
  17316. ["Timestamp (inactive)" org-time-stamp-inactive t]
  17317. ("Change Date"
  17318. ["1 Day Later" org-shiftright t]
  17319. ["1 Day Earlier" org-shiftleft t]
  17320. ["1 ... Later" org-shiftup t]
  17321. ["1 ... Earlier" org-shiftdown t])
  17322. ["Compute Time Range" org-evaluate-time-range t]
  17323. ["Schedule Item" org-schedule t]
  17324. ["Deadline" org-deadline t]
  17325. "--"
  17326. ["Custom time format" org-toggle-time-stamp-overlays
  17327. :style radio :selected org-display-custom-times]
  17328. "--"
  17329. ["Goto Calendar" org-goto-calendar t]
  17330. ["Date from Calendar" org-date-from-calendar t]
  17331. "--"
  17332. ["Start/Restart Timer" org-timer-start t]
  17333. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  17334. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  17335. ["Insert Timer String" org-timer t]
  17336. ["Insert Timer Item" org-timer-item t])
  17337. ("Logging work"
  17338. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  17339. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  17340. ["Clock out" org-clock-out t]
  17341. ["Clock cancel" org-clock-cancel t]
  17342. "--"
  17343. ["Mark as default task" org-clock-mark-default-task t]
  17344. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  17345. ["Goto running clock" org-clock-goto t]
  17346. "--"
  17347. ["Display times" org-clock-display t]
  17348. ["Create clock table" org-clock-report t]
  17349. "--"
  17350. ["Record DONE time"
  17351. (progn (setq org-log-done (not org-log-done))
  17352. (message "Switching to %s will %s record a timestamp"
  17353. (car org-done-keywords)
  17354. (if org-log-done "automatically" "not")))
  17355. :style toggle :selected org-log-done])
  17356. "--"
  17357. ["Agenda Command..." org-agenda t]
  17358. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  17359. ("File List for Agenda")
  17360. ("Special views current file"
  17361. ["TODO Tree" org-show-todo-tree t]
  17362. ["Check Deadlines" org-check-deadlines t]
  17363. ["Timeline" org-timeline t]
  17364. ["Tags/Property tree" org-match-sparse-tree t])
  17365. "--"
  17366. ["Export/Publish..." org-export t]
  17367. ("LaTeX"
  17368. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  17369. :selected org-cdlatex-mode]
  17370. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  17371. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  17372. ["Modify math symbol" org-cdlatex-math-modify
  17373. (org-inside-LaTeX-fragment-p)]
  17374. ["Insert citation" org-reftex-citation t]
  17375. "--"
  17376. ["Template for BEAMER" org-insert-beamer-options-template t])
  17377. "--"
  17378. ("MobileOrg"
  17379. ["Push Files and Views" org-mobile-push t]
  17380. ["Get Captured and Flagged" org-mobile-pull t]
  17381. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  17382. "--"
  17383. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  17384. "--"
  17385. ("Documentation"
  17386. ["Show Version" org-version t]
  17387. ["Info Documentation" org-info t])
  17388. ("Customize"
  17389. ["Browse Org Group" org-customize t]
  17390. "--"
  17391. ["Expand This Menu" org-create-customize-menu
  17392. (fboundp 'customize-menu-create)])
  17393. ["Send bug report" org-submit-bug-report t]
  17394. "--"
  17395. ("Refresh/Reload"
  17396. ["Refresh setup current buffer" org-mode-restart t]
  17397. ["Reload Org (after update)" org-reload t]
  17398. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  17399. ))
  17400. (defun org-info (&optional node)
  17401. "Read documentation for Org-mode in the info system.
  17402. With optional NODE, go directly to that node."
  17403. (interactive)
  17404. (info (format "(org)%s" (or node ""))))
  17405. ;;;###autoload
  17406. (defun org-submit-bug-report ()
  17407. "Submit a bug report on Org-mode via mail.
  17408. Don't hesitate to report any problems or inaccurate documentation.
  17409. If you don't have setup sending mail from (X)Emacs, please copy the
  17410. output buffer into your mail program, as it gives us important
  17411. information about your Org-mode version and configuration."
  17412. (interactive)
  17413. (require 'reporter)
  17414. (org-load-modules-maybe)
  17415. (org-require-autoloaded-modules)
  17416. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  17417. (reporter-submit-bug-report
  17418. "emacs-orgmode@gnu.org"
  17419. (org-version)
  17420. (let (list)
  17421. (save-window-excursion
  17422. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  17423. (delete-other-windows)
  17424. (erase-buffer)
  17425. (insert "You are about to submit a bug report to the Org-mode mailing list.
  17426. We would like to add your full Org-mode and Outline configuration to the
  17427. bug report. This greatly simplifies the work of the maintainer and
  17428. other experts on the mailing list.
  17429. HOWEVER, some variables you have customized may contain private
  17430. information. The names of customers, colleagues, or friends, might
  17431. appear in the form of file names, tags, todo states, or search strings.
  17432. If you answer yes to the prompt, you might want to check and remove
  17433. such private information before sending the email.")
  17434. (add-text-properties (point-min) (point-max) '(face org-warning))
  17435. (when (yes-or-no-p "Include your Org-mode configuration ")
  17436. (mapatoms
  17437. (lambda (v)
  17438. (and (boundp v)
  17439. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  17440. (or (and (symbol-value v)
  17441. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  17442. (and
  17443. (get v 'custom-type) (get v 'standard-value)
  17444. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  17445. (push v list)))))
  17446. (kill-buffer (get-buffer "*Warn about privacy*"))
  17447. list))
  17448. nil nil
  17449. "Remember to cover the basics, that is, what you expected to happen and
  17450. what in fact did happen. You don't know how to make a good report? See
  17451. http://orgmode.org/manual/Feedback.html#Feedback
  17452. Your bug report will be posted to the Org-mode mailing list.
  17453. ------------------------------------------------------------------------")
  17454. (save-excursion
  17455. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  17456. (replace-match "\\1Bug: \\3 [\\2]")))))
  17457. (defun org-install-agenda-files-menu ()
  17458. (let ((bl (buffer-list)))
  17459. (save-excursion
  17460. (while bl
  17461. (set-buffer (pop bl))
  17462. (if (eq major-mode 'org-mode) (setq bl nil)))
  17463. (when (eq major-mode 'org-mode)
  17464. (easy-menu-change
  17465. '("Org") "File List for Agenda"
  17466. (append
  17467. (list
  17468. ["Edit File List" (org-edit-agenda-file-list) t]
  17469. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  17470. ["Remove Current File from List" org-remove-file t]
  17471. ["Cycle through agenda files" org-cycle-agenda-files t]
  17472. ["Occur in all agenda files" org-occur-in-agenda-files t]
  17473. "--")
  17474. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  17475. ;;;; Documentation
  17476. ;;;###autoload
  17477. (defun org-require-autoloaded-modules ()
  17478. (interactive)
  17479. (mapc 'require
  17480. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  17481. org-docbook org-exp org-html org-icalendar
  17482. org-id org-latex
  17483. org-publish org-remember org-table
  17484. org-timer org-xoxo)))
  17485. ;;;###autoload
  17486. (defun org-reload (&optional uncompiled)
  17487. "Reload all org lisp files.
  17488. With prefix arg UNCOMPILED, load the uncompiled versions."
  17489. (interactive "P")
  17490. (require 'find-func)
  17491. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  17492. (dir-org (file-name-directory (org-find-library-name "org")))
  17493. (dir-org-contrib (ignore-errors
  17494. (file-name-directory
  17495. (org-find-library-name "org-contribdir"))))
  17496. (babel-files
  17497. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  17498. (append (list nil "comint" "eval" "exp" "keys"
  17499. "lob" "ref" "table" "tangle")
  17500. (delq nil
  17501. (mapcar
  17502. (lambda (lang)
  17503. (when (cdr lang) (symbol-name (car lang))))
  17504. org-babel-load-languages)))))
  17505. (files
  17506. (append (directory-files dir-org t file-re)
  17507. babel-files
  17508. (and dir-org-contrib
  17509. (directory-files dir-org-contrib t file-re))))
  17510. (remove-re (concat (if (featurep 'xemacs)
  17511. "org-colview" "org-colview-xemacs")
  17512. "\\'")))
  17513. (setq files (mapcar 'file-name-sans-extension files))
  17514. (setq files (mapcar
  17515. (lambda (x) (if (string-match remove-re x) nil x))
  17516. files))
  17517. (setq files (delq nil files))
  17518. (mapc
  17519. (lambda (f)
  17520. (when (featurep (intern (file-name-nondirectory f)))
  17521. (if (and (not uncompiled)
  17522. (file-exists-p (concat f ".elc")))
  17523. (load (concat f ".elc") nil nil t)
  17524. (load (concat f ".el") nil nil t))))
  17525. files))
  17526. (org-version))
  17527. ;;;###autoload
  17528. (defun org-customize ()
  17529. "Call the customize function with org as argument."
  17530. (interactive)
  17531. (org-load-modules-maybe)
  17532. (org-require-autoloaded-modules)
  17533. (customize-browse 'org))
  17534. (defun org-create-customize-menu ()
  17535. "Create a full customization menu for Org-mode, insert it into the menu."
  17536. (interactive)
  17537. (org-load-modules-maybe)
  17538. (org-require-autoloaded-modules)
  17539. (if (fboundp 'customize-menu-create)
  17540. (progn
  17541. (easy-menu-change
  17542. '("Org") "Customize"
  17543. `(["Browse Org group" org-customize t]
  17544. "--"
  17545. ,(customize-menu-create 'org)
  17546. ["Set" Custom-set t]
  17547. ["Save" Custom-save t]
  17548. ["Reset to Current" Custom-reset-current t]
  17549. ["Reset to Saved" Custom-reset-saved t]
  17550. ["Reset to Standard Settings" Custom-reset-standard t]))
  17551. (message "\"Org\"-menu now contains full customization menu"))
  17552. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  17553. ;;;; Miscellaneous stuff
  17554. ;;; Generally useful functions
  17555. (defun org-get-at-bol (property)
  17556. "Get text property PROPERTY at beginning of line."
  17557. (get-text-property (point-at-bol) property))
  17558. (defun org-find-text-property-in-string (prop s)
  17559. "Return the first non-nil value of property PROP in string S."
  17560. (or (get-text-property 0 prop s)
  17561. (get-text-property (or (next-single-property-change 0 prop s) 0)
  17562. prop s)))
  17563. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  17564. "Display the given MESSAGE as a warning."
  17565. (if (fboundp 'display-warning)
  17566. (display-warning 'org message
  17567. (if (featurep 'xemacs) 'warning :warning))
  17568. (let ((buf (get-buffer-create "*Org warnings*")))
  17569. (with-current-buffer buf
  17570. (goto-char (point-max))
  17571. (insert "Warning (Org): " message)
  17572. (unless (bolp)
  17573. (newline)))
  17574. (display-buffer buf)
  17575. (sit-for 0))))
  17576. (defun org-eval (form)
  17577. "Eval FORM and return result."
  17578. (condition-case error
  17579. (eval form)
  17580. (error (format "%%![Error: %s]" error))))
  17581. (defun org-in-commented-line ()
  17582. "Is point in a line starting with `#'?"
  17583. (equal (char-after (point-at-bol)) ?#))
  17584. (defun org-in-indented-comment-line ()
  17585. "Is point in a line starting with `#' after some white space?"
  17586. (save-excursion
  17587. (save-match-data
  17588. (goto-char (point-at-bol))
  17589. (looking-at "[ \t]*#"))))
  17590. (defun org-in-verbatim-emphasis ()
  17591. (save-match-data
  17592. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  17593. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  17594. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  17595. (if (and marker (marker-buffer marker)
  17596. (buffer-live-p (marker-buffer marker)))
  17597. (progn
  17598. (org-pop-to-buffer-same-window (marker-buffer marker))
  17599. (if (or (> marker (point-max)) (< marker (point-min)))
  17600. (widen))
  17601. (goto-char marker)
  17602. (org-show-context 'org-goto))
  17603. (if bookmark
  17604. (bookmark-jump bookmark)
  17605. (error "Cannot find location"))))
  17606. (defun org-quote-csv-field (s)
  17607. "Quote field for inclusion in CSV material."
  17608. (if (string-match "[\",]" s)
  17609. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  17610. s))
  17611. (defun org-force-self-insert (N)
  17612. "Needed to enforce self-insert under remapping."
  17613. (interactive "p")
  17614. (self-insert-command N))
  17615. (defun org-string-width (s)
  17616. "Compute width of string, ignoring invisible characters.
  17617. This ignores character with invisibility property `org-link', and also
  17618. characters with property `org-cwidth', because these will become invisible
  17619. upon the next fontification round."
  17620. (let (b l)
  17621. (when (or (eq t buffer-invisibility-spec)
  17622. (assq 'org-link buffer-invisibility-spec))
  17623. (while (setq b (text-property-any 0 (length s)
  17624. 'invisible 'org-link s))
  17625. (setq s (concat (substring s 0 b)
  17626. (substring s (or (next-single-property-change
  17627. b 'invisible s) (length s)))))))
  17628. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  17629. (setq s (concat (substring s 0 b)
  17630. (substring s (or (next-single-property-change
  17631. b 'org-cwidth s) (length s))))))
  17632. (setq l (string-width s) b -1)
  17633. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  17634. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  17635. l))
  17636. (defun org-shorten-string (s maxlength)
  17637. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  17638. If the string is shorter or has length MAXLENGTH, just return the
  17639. original string. If it is longer, the functions finds a space in the
  17640. string, breaks this string off at that locations and adds three dots
  17641. as ellipsis. Including the ellipsis, the string will not be longer
  17642. than MAXLENGTH. If finding a good breaking point in the string does
  17643. not work, the string is just chopped off in the middle of a word
  17644. if necessary."
  17645. (if (<= (length s) maxlength)
  17646. s
  17647. (let* ((n (max (- maxlength 4) 1))
  17648. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  17649. (if (string-match re s)
  17650. (concat (match-string 1 s) "...")
  17651. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  17652. (defun org-get-indentation (&optional line)
  17653. "Get the indentation of the current line, interpreting tabs.
  17654. When LINE is given, assume it represents a line and compute its indentation."
  17655. (if line
  17656. (if (string-match "^ *" (org-remove-tabs line))
  17657. (match-end 0))
  17658. (save-excursion
  17659. (beginning-of-line 1)
  17660. (skip-chars-forward " \t")
  17661. (current-column))))
  17662. (defun org-get-string-indentation (s)
  17663. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  17664. (let ((n -1) (i 0) (w tab-width) c)
  17665. (catch 'exit
  17666. (while (< (setq n (1+ n)) (length s))
  17667. (setq c (aref s n))
  17668. (cond ((= c ?\ ) (setq i (1+ i)))
  17669. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  17670. (t (throw 'exit t)))))
  17671. i))
  17672. (defun org-remove-tabs (s &optional width)
  17673. "Replace tabulators in S with spaces.
  17674. Assumes that s is a single line, starting in column 0."
  17675. (setq width (or width tab-width))
  17676. (while (string-match "\t" s)
  17677. (setq s (replace-match
  17678. (make-string
  17679. (- (* width (/ (+ (match-beginning 0) width) width))
  17680. (match-beginning 0)) ?\ )
  17681. t t s)))
  17682. s)
  17683. (defun org-fix-indentation (line ind)
  17684. "Fix indentation in LINE.
  17685. IND is a cons cell with target and minimum indentation.
  17686. If the current indentation in LINE is smaller than the minimum,
  17687. leave it alone. If it is larger than ind, set it to the target."
  17688. (let* ((l (org-remove-tabs line))
  17689. (i (org-get-indentation l))
  17690. (i1 (car ind)) (i2 (cdr ind)))
  17691. (if (>= i i2) (setq l (substring line i2)))
  17692. (if (> i1 0)
  17693. (concat (make-string i1 ?\ ) l)
  17694. l)))
  17695. (defun org-remove-indentation (code &optional n)
  17696. "Remove the maximum common indentation from the lines in CODE.
  17697. N may optionally be the number of spaces to remove."
  17698. (with-temp-buffer
  17699. (insert code)
  17700. (org-do-remove-indentation n)
  17701. (buffer-string)))
  17702. (defun org-do-remove-indentation (&optional n)
  17703. "Remove the maximum common indentation from the buffer."
  17704. (untabify (point-min) (point-max))
  17705. (let ((min 10000) re)
  17706. (if n
  17707. (setq min n)
  17708. (goto-char (point-min))
  17709. (while (re-search-forward "^ *[^ \n]" nil t)
  17710. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  17711. (unless (or (= min 0) (= min 10000))
  17712. (setq re (format "^ \\{%d\\}" min))
  17713. (goto-char (point-min))
  17714. (while (re-search-forward re nil t)
  17715. (replace-match "")
  17716. (end-of-line 1))
  17717. min)))
  17718. (defun org-fill-template (template alist)
  17719. "Find each %key of ALIST in TEMPLATE and replace it."
  17720. (let ((case-fold-search nil)
  17721. entry key value)
  17722. (setq alist (sort (copy-sequence alist)
  17723. (lambda (a b) (< (length (car a)) (length (car b))))))
  17724. (while (setq entry (pop alist))
  17725. (setq template
  17726. (replace-regexp-in-string
  17727. (concat "%" (regexp-quote (car entry)))
  17728. (cdr entry) template t t)))
  17729. template))
  17730. (defun org-base-buffer (buffer)
  17731. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  17732. (if (not buffer)
  17733. buffer
  17734. (or (buffer-base-buffer buffer)
  17735. buffer)))
  17736. (defun org-trim (s)
  17737. "Remove whitespace at beginning and end of string."
  17738. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  17739. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  17740. s)
  17741. (defun org-wrap (string &optional width lines)
  17742. "Wrap string to either a number of lines, or a width in characters.
  17743. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  17744. that costs. If there is a word longer than WIDTH, the text is actually
  17745. wrapped to the length of that word.
  17746. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  17747. many lines, whatever width that takes.
  17748. The return value is a list of lines, without newlines at the end."
  17749. (let* ((words (org-split-string string "[ \t\n]+"))
  17750. (maxword (apply 'max (mapcar 'org-string-width words)))
  17751. w ll)
  17752. (cond (width
  17753. (org-do-wrap words (max maxword width)))
  17754. (lines
  17755. (setq w maxword)
  17756. (setq ll (org-do-wrap words maxword))
  17757. (if (<= (length ll) lines)
  17758. ll
  17759. (setq ll words)
  17760. (while (> (length ll) lines)
  17761. (setq w (1+ w))
  17762. (setq ll (org-do-wrap words w)))
  17763. ll))
  17764. (t (error "Cannot wrap this")))))
  17765. (defun org-do-wrap (words width)
  17766. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  17767. (let (lines line)
  17768. (while words
  17769. (setq line (pop words))
  17770. (while (and words (< (+ (length line) (length (car words))) width))
  17771. (setq line (concat line " " (pop words))))
  17772. (setq lines (push line lines)))
  17773. (nreverse lines)))
  17774. (defun org-split-string (string &optional separators)
  17775. "Splits STRING into substrings at SEPARATORS.
  17776. No empty strings are returned if there are matches at the beginning
  17777. and end of string."
  17778. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  17779. (start 0)
  17780. notfirst
  17781. (list nil))
  17782. (while (and (string-match rexp string
  17783. (if (and notfirst
  17784. (= start (match-beginning 0))
  17785. (< start (length string)))
  17786. (1+ start) start))
  17787. (< (match-beginning 0) (length string)))
  17788. (setq notfirst t)
  17789. (or (eq (match-beginning 0) 0)
  17790. (and (eq (match-beginning 0) (match-end 0))
  17791. (eq (match-beginning 0) start))
  17792. (setq list
  17793. (cons (substring string start (match-beginning 0))
  17794. list)))
  17795. (setq start (match-end 0)))
  17796. (or (eq start (length string))
  17797. (setq list
  17798. (cons (substring string start)
  17799. list)))
  17800. (nreverse list)))
  17801. (defun org-quote-vert (s)
  17802. "Replace \"|\" with \"\\vert\"."
  17803. (while (string-match "|" s)
  17804. (setq s (replace-match "\\vert" t t s)))
  17805. s)
  17806. (defun org-uuidgen-p (s)
  17807. "Is S an ID created by UUIDGEN?"
  17808. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  17809. (defun org-context ()
  17810. "Return a list of contexts of the current cursor position.
  17811. If several contexts apply, all are returned.
  17812. Each context entry is a list with a symbol naming the context, and
  17813. two positions indicating start and end of the context. Possible
  17814. contexts are:
  17815. :headline anywhere in a headline
  17816. :headline-stars on the leading stars in a headline
  17817. :todo-keyword on a TODO keyword (including DONE) in a headline
  17818. :tags on the TAGS in a headline
  17819. :priority on the priority cookie in a headline
  17820. :item on the first line of a plain list item
  17821. :item-bullet on the bullet/number of a plain list item
  17822. :checkbox on the checkbox in a plain list item
  17823. :table in an org-mode table
  17824. :table-special on a special filed in a table
  17825. :table-table in a table.el table
  17826. :link on a hyperlink
  17827. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  17828. :target on a <<target>>
  17829. :radio-target on a <<<radio-target>>>
  17830. :latex-fragment on a LaTeX fragment
  17831. :latex-preview on a LaTeX fragment with overlaid preview image
  17832. This function expects the position to be visible because it uses font-lock
  17833. faces as a help to recognize the following contexts: :table-special, :link,
  17834. and :keyword."
  17835. (let* ((f (get-text-property (point) 'face))
  17836. (faces (if (listp f) f (list f)))
  17837. (p (point)) clist o)
  17838. ;; First the large context
  17839. (cond
  17840. ((org-on-heading-p t)
  17841. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  17842. (when (progn
  17843. (beginning-of-line 1)
  17844. (looking-at org-todo-line-tags-regexp))
  17845. (push (org-point-in-group p 1 :headline-stars) clist)
  17846. (push (org-point-in-group p 2 :todo-keyword) clist)
  17847. (push (org-point-in-group p 4 :tags) clist))
  17848. (goto-char p)
  17849. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  17850. (if (looking-at "\\[#[A-Z0-9]\\]")
  17851. (push (org-point-in-group p 0 :priority) clist)))
  17852. ((org-at-item-p)
  17853. (push (org-point-in-group p 2 :item-bullet) clist)
  17854. (push (list :item (point-at-bol)
  17855. (save-excursion (org-end-of-item) (point)))
  17856. clist)
  17857. (and (org-at-item-checkbox-p)
  17858. (push (org-point-in-group p 0 :checkbox) clist)))
  17859. ((org-at-table-p)
  17860. (push (list :table (org-table-begin) (org-table-end)) clist)
  17861. (if (memq 'org-formula faces)
  17862. (push (list :table-special
  17863. (previous-single-property-change p 'face)
  17864. (next-single-property-change p 'face)) clist)))
  17865. ((org-at-table-p 'any)
  17866. (push (list :table-table) clist)))
  17867. (goto-char p)
  17868. ;; Now the small context
  17869. (cond
  17870. ((org-at-timestamp-p)
  17871. (push (org-point-in-group p 0 :timestamp) clist))
  17872. ((memq 'org-link faces)
  17873. (push (list :link
  17874. (previous-single-property-change p 'face)
  17875. (next-single-property-change p 'face)) clist))
  17876. ((memq 'org-special-keyword faces)
  17877. (push (list :keyword
  17878. (previous-single-property-change p 'face)
  17879. (next-single-property-change p 'face)) clist))
  17880. ((org-on-target-p)
  17881. (push (org-point-in-group p 0 :target) clist)
  17882. (goto-char (1- (match-beginning 0)))
  17883. (if (looking-at org-radio-target-regexp)
  17884. (push (org-point-in-group p 0 :radio-target) clist))
  17885. (goto-char p))
  17886. ((setq o (car (delq nil
  17887. (mapcar
  17888. (lambda (x)
  17889. (if (memq x org-latex-fragment-image-overlays) x))
  17890. (overlays-at (point))))))
  17891. (push (list :latex-fragment
  17892. (overlay-start o) (overlay-end o)) clist)
  17893. (push (list :latex-preview
  17894. (overlay-start o) (overlay-end o)) clist))
  17895. ((org-inside-LaTeX-fragment-p)
  17896. ;; FIXME: positions wrong.
  17897. (push (list :latex-fragment (point) (point)) clist)))
  17898. (setq clist (nreverse (delq nil clist)))
  17899. clist))
  17900. ;; FIXME: Compare with at-regexp-p Do we need both?
  17901. (defun org-in-regexp (re &optional nlines visually)
  17902. "Check if point is inside a match of regexp.
  17903. Normally only the current line is checked, but you can include NLINES extra
  17904. lines both before and after point into the search.
  17905. If VISUALLY is set, require that the cursor is not after the match but
  17906. really on, so that the block visually is on the match."
  17907. (catch 'exit
  17908. (let ((pos (point))
  17909. (eol (point-at-eol (+ 1 (or nlines 0))))
  17910. (inc (if visually 1 0)))
  17911. (save-excursion
  17912. (beginning-of-line (- 1 (or nlines 0)))
  17913. (while (re-search-forward re eol t)
  17914. (if (and (<= (match-beginning 0) pos)
  17915. (>= (+ inc (match-end 0)) pos))
  17916. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  17917. (defun org-at-regexp-p (regexp)
  17918. "Is point inside a match of REGEXP in the current line?"
  17919. (catch 'exit
  17920. (save-excursion
  17921. (let ((pos (point)) (end (point-at-eol)))
  17922. (beginning-of-line 1)
  17923. (while (re-search-forward regexp end t)
  17924. (if (and (<= (match-beginning 0) pos)
  17925. (>= (match-end 0) pos))
  17926. (throw 'exit t)))
  17927. nil))))
  17928. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  17929. "Non-nil when point is between matches of START-RE and END-RE.
  17930. Also return a non-nil value when point is on one of the matches.
  17931. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  17932. buffer positions. Default values are the positions of headlines
  17933. surrounding the point.
  17934. The functions returns a cons cell whose car (resp. cdr) is the
  17935. position before START-RE (resp. after END-RE)."
  17936. (save-match-data
  17937. (let ((pos (point))
  17938. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  17939. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  17940. beg end)
  17941. (save-excursion
  17942. ;; Point is on a block when on START-RE or if START-RE can be
  17943. ;; found before it...
  17944. (and (or (org-at-regexp-p start-re)
  17945. (re-search-backward start-re limit-up t))
  17946. (setq beg (match-beginning 0))
  17947. ;; ... and END-RE after it...
  17948. (goto-char (match-end 0))
  17949. (re-search-forward end-re limit-down t)
  17950. (> (setq end (match-end 0)) pos)
  17951. ;; ... without another START-RE in-between.
  17952. (goto-char (match-beginning 0))
  17953. (not (re-search-backward start-re (1+ beg) t))
  17954. ;; Return value.
  17955. (cons beg end))))))
  17956. (defun org-in-block-p (names)
  17957. "Non-nil when point belongs to a block whose name belongs to NAMES.
  17958. NAMES is a list of strings containing names of blocks.
  17959. Return first block name matched, or nil. Beware that in case of
  17960. nested blocks, the returned name may not belong to the closest
  17961. block from point."
  17962. (save-match-data
  17963. (catch 'exit
  17964. (let ((case-fold-search t)
  17965. (lim-up (save-excursion (outline-previous-heading)))
  17966. (lim-down (save-excursion (outline-next-heading))))
  17967. (mapc (lambda (name)
  17968. (let ((n (regexp-quote name)))
  17969. (when (org-between-regexps-p
  17970. (concat "^[ \t]*#\\+begin_" n)
  17971. (concat "^[ \t]*#\\+end_" n)
  17972. lim-up lim-down)
  17973. (throw 'exit n))))
  17974. names))
  17975. nil)))
  17976. (defun org-occur-in-agenda-files (regexp &optional nlines)
  17977. "Call `multi-occur' with buffers for all agenda files."
  17978. (interactive "sOrg-files matching: \np")
  17979. (let* ((files (org-agenda-files))
  17980. (tnames (mapcar 'file-truename files))
  17981. (extra org-agenda-text-search-extra-files)
  17982. f)
  17983. (when (eq (car extra) 'agenda-archives)
  17984. (setq extra (cdr extra))
  17985. (setq files (org-add-archive-files files)))
  17986. (while (setq f (pop extra))
  17987. (unless (member (file-truename f) tnames)
  17988. (add-to-list 'files f 'append)
  17989. (add-to-list 'tnames (file-truename f) 'append)))
  17990. (multi-occur
  17991. (mapcar (lambda (x)
  17992. (with-current-buffer
  17993. (or (get-file-buffer x) (find-file-noselect x))
  17994. (widen)
  17995. (current-buffer)))
  17996. files)
  17997. regexp)))
  17998. (if (boundp 'occur-mode-find-occurrence-hook)
  17999. ;; Emacs 23
  18000. (add-hook 'occur-mode-find-occurrence-hook
  18001. (lambda ()
  18002. (when (eq major-mode 'org-mode)
  18003. (org-reveal))))
  18004. ;; Emacs 22
  18005. (defadvice occur-mode-goto-occurrence
  18006. (after org-occur-reveal activate)
  18007. (and (eq major-mode 'org-mode) (org-reveal)))
  18008. (defadvice occur-mode-goto-occurrence-other-window
  18009. (after org-occur-reveal activate)
  18010. (and (eq major-mode 'org-mode) (org-reveal)))
  18011. (defadvice occur-mode-display-occurrence
  18012. (after org-occur-reveal activate)
  18013. (when (eq major-mode 'org-mode)
  18014. (let ((pos (occur-mode-find-occurrence)))
  18015. (with-current-buffer (marker-buffer pos)
  18016. (save-excursion
  18017. (goto-char pos)
  18018. (org-reveal)))))))
  18019. (defun org-occur-link-in-agenda-files ()
  18020. "Create a link and search for it in the agendas.
  18021. The link is not stored in `org-stored-links', it is just created
  18022. for the search purpose."
  18023. (interactive)
  18024. (let ((link (condition-case nil
  18025. (org-store-link nil)
  18026. (error "Unable to create a link to here"))))
  18027. (org-occur-in-agenda-files (regexp-quote link))))
  18028. (defun org-uniquify (list)
  18029. "Remove duplicate elements from LIST."
  18030. (let (res)
  18031. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18032. res))
  18033. (defun org-delete-all (elts list)
  18034. "Remove all elements in ELTS from LIST."
  18035. (while elts
  18036. (setq list (delete (pop elts) list)))
  18037. list)
  18038. (defun org-count (cl-item cl-seq)
  18039. "Count the number of occurrences of ITEM in SEQ.
  18040. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18041. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18042. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18043. (while (< cl-start cl-end)
  18044. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18045. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18046. (setq cl-start (1+ cl-start)))
  18047. cl-count))
  18048. (defun org-remove-if (predicate seq)
  18049. "Remove everything from SEQ that fulfills PREDICATE."
  18050. (let (res e)
  18051. (while seq
  18052. (setq e (pop seq))
  18053. (if (not (funcall predicate e)) (push e res)))
  18054. (nreverse res)))
  18055. (defun org-remove-if-not (predicate seq)
  18056. "Remove everything from SEQ that does not fulfill PREDICATE."
  18057. (let (res e)
  18058. (while seq
  18059. (setq e (pop seq))
  18060. (if (funcall predicate e) (push e res)))
  18061. (nreverse res)))
  18062. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  18063. "Reduce two-argument FUNCTION across SEQ.
  18064. Taken from `reduce' in cl-seq.el with all keyword arguments but
  18065. \":initial-value\" removed."
  18066. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  18067. (cadr (memq :initial-value cl-keys)))
  18068. (cl-seq (pop cl-seq))
  18069. (t (funcall cl-func)))))
  18070. (while cl-seq
  18071. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  18072. cl-accum))
  18073. (defun org-back-over-empty-lines ()
  18074. "Move backwards over whitespace, to the beginning of the first empty line.
  18075. Returns the number of empty lines passed."
  18076. (let ((pos (point)))
  18077. (if (cdr (assoc 'heading org-blank-before-new-entry))
  18078. (skip-chars-backward " \t\n\r")
  18079. (unless (eobp)
  18080. (forward-line -1)))
  18081. (beginning-of-line 2)
  18082. (goto-char (min (point) pos))
  18083. (count-lines (point) pos)))
  18084. (defun org-skip-whitespace ()
  18085. (skip-chars-forward " \t\n\r"))
  18086. (defun org-point-in-group (point group &optional context)
  18087. "Check if POINT is in match-group GROUP.
  18088. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  18089. match. If the match group does not exist or point is not inside it,
  18090. return nil."
  18091. (and (match-beginning group)
  18092. (>= point (match-beginning group))
  18093. (<= point (match-end group))
  18094. (if context
  18095. (list context (match-beginning group) (match-end group))
  18096. t)))
  18097. (defun org-switch-to-buffer-other-window (&rest args)
  18098. "Switch to buffer in a second window on the current frame.
  18099. In particular, do not allow pop-up frames.
  18100. Returns the newly created buffer."
  18101. (let (pop-up-frames special-display-buffer-names special-display-regexps
  18102. special-display-function)
  18103. (apply 'switch-to-buffer-other-window args)))
  18104. (defun org-combine-plists (&rest plists)
  18105. "Create a single property list from all plists in PLISTS.
  18106. The process starts by copying the first list, and then setting properties
  18107. from the other lists. Settings in the last list are the most significant
  18108. ones and overrule settings in the other lists."
  18109. (let ((rtn (copy-sequence (pop plists)))
  18110. p v ls)
  18111. (while plists
  18112. (setq ls (pop plists))
  18113. (while ls
  18114. (setq p (pop ls) v (pop ls))
  18115. (setq rtn (plist-put rtn p v))))
  18116. rtn))
  18117. (defun org-move-line-down (arg)
  18118. "Move the current line down. With prefix argument, move it past ARG lines."
  18119. (interactive "p")
  18120. (let ((col (current-column))
  18121. beg end pos)
  18122. (beginning-of-line 1) (setq beg (point))
  18123. (beginning-of-line 2) (setq end (point))
  18124. (beginning-of-line (+ 1 arg))
  18125. (setq pos (move-marker (make-marker) (point)))
  18126. (insert (delete-and-extract-region beg end))
  18127. (goto-char pos)
  18128. (org-move-to-column col)))
  18129. (defun org-move-line-up (arg)
  18130. "Move the current line up. With prefix argument, move it past ARG lines."
  18131. (interactive "p")
  18132. (let ((col (current-column))
  18133. beg end pos)
  18134. (beginning-of-line 1) (setq beg (point))
  18135. (beginning-of-line 2) (setq end (point))
  18136. (beginning-of-line (- arg))
  18137. (setq pos (move-marker (make-marker) (point)))
  18138. (insert (delete-and-extract-region beg end))
  18139. (goto-char pos)
  18140. (org-move-to-column col)))
  18141. (defun org-replace-escapes (string table)
  18142. "Replace %-escapes in STRING with values in TABLE.
  18143. TABLE is an association list with keys like \"%a\" and string values.
  18144. The sequences in STRING may contain normal field width and padding information,
  18145. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  18146. so values can contain further %-escapes if they are define later in TABLE."
  18147. (let ((tbl (copy-alist table))
  18148. (case-fold-search nil)
  18149. (pchg 0)
  18150. e re rpl)
  18151. (while (setq e (pop tbl))
  18152. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  18153. (when (and (cdr e) (string-match re (cdr e)))
  18154. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  18155. (safe "SREF"))
  18156. (add-text-properties 0 3 (list 'sref sref) safe)
  18157. (setcdr e (replace-match safe t t (cdr e)))))
  18158. (while (string-match re string)
  18159. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  18160. (cdr e)))
  18161. (setq string (replace-match rpl t t string))))
  18162. (while (setq pchg (next-property-change pchg string))
  18163. (let ((sref (get-text-property pchg 'sref string)))
  18164. (when (and sref (string-match "SREF" string pchg))
  18165. (setq string (replace-match sref t t string)))))
  18166. string))
  18167. (defun org-sublist (list start end)
  18168. "Return a section of LIST, from START to END.
  18169. Counting starts at 1."
  18170. (let (rtn (c start))
  18171. (setq list (nthcdr (1- start) list))
  18172. (while (and list (<= c end))
  18173. (push (pop list) rtn)
  18174. (setq c (1+ c)))
  18175. (nreverse rtn)))
  18176. (defun org-find-base-buffer-visiting (file)
  18177. "Like `find-buffer-visiting' but always return the base buffer and
  18178. not an indirect buffer."
  18179. (let ((buf (or (get-file-buffer file)
  18180. (find-buffer-visiting file))))
  18181. (if buf
  18182. (or (buffer-base-buffer buf) buf)
  18183. nil)))
  18184. (defun org-image-file-name-regexp (&optional extensions)
  18185. "Return regexp matching the file names of images.
  18186. If EXTENSIONS is given, only match these."
  18187. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  18188. (image-file-name-regexp)
  18189. (let ((image-file-name-extensions
  18190. (or extensions
  18191. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  18192. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  18193. (concat "\\."
  18194. (regexp-opt (nconc (mapcar 'upcase
  18195. image-file-name-extensions)
  18196. image-file-name-extensions)
  18197. t)
  18198. "\\'"))))
  18199. (defun org-file-image-p (file &optional extensions)
  18200. "Return non-nil if FILE is an image."
  18201. (save-match-data
  18202. (string-match (org-image-file-name-regexp extensions) file)))
  18203. (defun org-get-cursor-date ()
  18204. "Return the date at cursor in as a time.
  18205. This works in the calendar and in the agenda, anywhere else it just
  18206. returns the current time."
  18207. (let (date day defd)
  18208. (cond
  18209. ((eq major-mode 'calendar-mode)
  18210. (setq date (calendar-cursor-to-date)
  18211. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  18212. ((eq major-mode 'org-agenda-mode)
  18213. (setq day (get-text-property (point) 'day))
  18214. (if day
  18215. (setq date (calendar-gregorian-from-absolute day)
  18216. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  18217. (nth 2 date))))))
  18218. (or defd (current-time))))
  18219. (defvar org-agenda-action-marker (make-marker)
  18220. "Marker pointing to the entry for the next agenda action.")
  18221. (defun org-mark-entry-for-agenda-action ()
  18222. "Mark the current entry as target of an agenda action.
  18223. Agenda actions are actions executed from the agenda with the key `k',
  18224. which make use of the date at the cursor."
  18225. (interactive)
  18226. (move-marker org-agenda-action-marker
  18227. (save-excursion (org-back-to-heading t) (point))
  18228. (current-buffer))
  18229. (message
  18230. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  18231. (defun org-mark-subtree ()
  18232. "Mark the current subtree.
  18233. This puts point at the start of the current subtree, and mark at the end.
  18234. If point is in an inline task, mark that task instead."
  18235. (interactive)
  18236. (let ((inline-task-p
  18237. (and (featurep 'org-inlinetask)
  18238. (org-inlinetask-in-task-p)))
  18239. (beg))
  18240. ;; Get beginning of subtree
  18241. (cond
  18242. (inline-task-p (org-inlinetask-goto-beginning))
  18243. ((org-at-heading-p) (beginning-of-line))
  18244. (t (org-with-limited-levels (outline-previous-visible-heading 1))))
  18245. (setq beg (point))
  18246. ;; Get end of it
  18247. (if inline-task-p
  18248. (org-inlinetask-goto-end)
  18249. (org-end-of-subtree))
  18250. ;; Mark zone
  18251. (push-mark (point) nil t)
  18252. (goto-char beg)))
  18253. ;;; Paragraph filling stuff.
  18254. ;; We want this to be just right, so use the full arsenal.
  18255. (defun org-indent-line-function ()
  18256. "Indent line depending on context."
  18257. (interactive)
  18258. (let* ((pos (point))
  18259. (itemp (org-at-item-p))
  18260. (case-fold-search t)
  18261. (org-drawer-regexp (or org-drawer-regexp "\000"))
  18262. (inline-task-p (and (featurep 'org-inlinetask)
  18263. (org-inlinetask-in-task-p)))
  18264. (inline-re (and inline-task-p
  18265. (org-inlinetask-outline-regexp)))
  18266. column)
  18267. (beginning-of-line 1)
  18268. (cond
  18269. ;; Comments
  18270. ((looking-at "# ") (setq column 0))
  18271. ;; Headings
  18272. ((looking-at org-outline-regexp) (setq column 0))
  18273. ;; Included files
  18274. ((looking-at "#\\+include:") (setq column 0))
  18275. ;; Footnote definition
  18276. ((looking-at org-footnote-definition-re) (setq column 0))
  18277. ;; Literal examples
  18278. ((looking-at "[ \t]*:\\( \\|$\\)")
  18279. (setq column (org-get-indentation))) ; do nothing
  18280. ;; Lists
  18281. ((ignore-errors (goto-char (org-in-item-p)))
  18282. (setq column (if itemp
  18283. (org-get-indentation)
  18284. (org-list-item-body-column (point))))
  18285. (goto-char pos))
  18286. ;; Drawers
  18287. ((and (looking-at "[ \t]*:END:")
  18288. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  18289. (save-excursion
  18290. (goto-char (1- (match-beginning 1)))
  18291. (setq column (current-column))))
  18292. ;; Special blocks
  18293. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  18294. (save-excursion
  18295. (re-search-backward
  18296. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  18297. (setq column (org-get-indentation (match-string 0))))
  18298. ((and (not (looking-at "[ \t]*#\\+begin_"))
  18299. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  18300. (save-excursion
  18301. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  18302. (setq column
  18303. (cond ((equal (downcase (match-string 1)) "src")
  18304. ;; src blocks: let `org-edit-src-exit' handle them
  18305. (org-get-indentation))
  18306. ((equal (downcase (match-string 1)) "example")
  18307. (max (org-get-indentation)
  18308. (org-get-indentation (match-string 0))))
  18309. (t
  18310. (org-get-indentation (match-string 0))))))
  18311. ;; This line has nothing special, look at the previous relevant
  18312. ;; line to compute indentation
  18313. (t
  18314. (beginning-of-line 0)
  18315. (while (and (not (bobp))
  18316. (not (looking-at org-drawer-regexp))
  18317. ;; When point started in an inline task, do not move
  18318. ;; above task starting line.
  18319. (not (and inline-task-p (looking-at inline-re)))
  18320. ;; Skip drawers, blocks, empty lines, verbatim,
  18321. ;; comments, tables, footnotes definitions, lists,
  18322. ;; inline tasks.
  18323. (or (and (looking-at "[ \t]*:END:")
  18324. (re-search-backward org-drawer-regexp nil t))
  18325. (and (looking-at "[ \t]*#\\+end_")
  18326. (re-search-backward "[ \t]*#\\+begin_"nil t))
  18327. (looking-at "[ \t]*[\n:#|]")
  18328. (looking-at org-footnote-definition-re)
  18329. (and (ignore-errors (goto-char (org-in-item-p)))
  18330. (goto-char
  18331. (org-list-get-top-point (org-list-struct))))
  18332. (and (not inline-task-p)
  18333. (featurep 'org-inlinetask)
  18334. (org-inlinetask-in-task-p)
  18335. (or (org-inlinetask-goto-beginning) t))))
  18336. (beginning-of-line 0))
  18337. (cond
  18338. ;; There was an heading above.
  18339. ((looking-at "\\*+[ \t]+")
  18340. (if (not org-adapt-indentation)
  18341. (setq column 0)
  18342. (goto-char (match-end 0))
  18343. (setq column (current-column))))
  18344. ;; A drawer had started and is unfinished
  18345. ((looking-at org-drawer-regexp)
  18346. (goto-char (1- (match-beginning 1)))
  18347. (setq column (current-column)))
  18348. ;; Else, nothing noticeable found: get indentation and go on.
  18349. (t (setq column (org-get-indentation))))))
  18350. ;; Now apply indentation and move cursor accordingly
  18351. (goto-char pos)
  18352. (if (<= (current-column) (current-indentation))
  18353. (org-indent-line-to column)
  18354. (save-excursion (org-indent-line-to column)))
  18355. ;; Special polishing for properties, see `org-property-format'
  18356. (setq column (current-column))
  18357. (beginning-of-line 1)
  18358. (if (looking-at
  18359. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  18360. (replace-match (concat (match-string 1)
  18361. (format org-property-format
  18362. (match-string 2) (match-string 3)))
  18363. t t))
  18364. (org-move-to-column column)))
  18365. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  18366. "Variable to store copy of `adaptive-fill-regexp'.
  18367. Since `adaptive-fill-regexp' is set to never match, we need to
  18368. store a backup of its value before entering `org-mode' so that
  18369. the functionality can be provided as a fall-back.")
  18370. (defun org-set-autofill-regexps ()
  18371. (interactive)
  18372. ;; In the paragraph separator we include headlines, because filling
  18373. ;; text in a line directly attached to a headline would otherwise
  18374. ;; fill the headline as well.
  18375. (org-set-local 'comment-start-skip "^#+[ \t]*")
  18376. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  18377. ;; The paragraph starter includes hand-formatted lists.
  18378. (org-set-local
  18379. 'paragraph-start
  18380. (concat
  18381. "\f" "\\|"
  18382. "[ ]*$" "\\|"
  18383. org-outline-regexp "\\|"
  18384. "[ \t]*#" "\\|"
  18385. (org-item-re) "\\|"
  18386. "[ \t]*[:|]" "\\|"
  18387. "\\$\\$" "\\|"
  18388. "\\\\\\(begin\\|end\\|[][]\\)"))
  18389. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  18390. ;; But only if the user has not turned off tables or fixed-width regions
  18391. (org-set-local
  18392. 'auto-fill-inhibit-regexp
  18393. (concat org-outline-regexp
  18394. "\\|#\\+"
  18395. "\\|[ \t]*" org-keyword-time-regexp
  18396. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  18397. (concat
  18398. "\\|[ \t]*["
  18399. (if org-enable-table-editor "|" "")
  18400. (if org-enable-fixed-width-editor ":" "")
  18401. "]"))))
  18402. ;; We use our own fill-paragraph function, to make sure that tables
  18403. ;; and fixed-width regions are not wrapped. That function will pass
  18404. ;; through to `fill-paragraph' when appropriate.
  18405. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  18406. ;; Prevent auto-fill from inserting unwanted new items.
  18407. (if (boundp 'fill-nobreak-predicate)
  18408. (org-set-local 'fill-nobreak-predicate
  18409. (if (memq 'org-fill-item-nobreak-p fill-nobreak-predicate)
  18410. fill-nobreak-predicate
  18411. (cons 'org-fill-item-nobreak-p fill-nobreak-predicate))))
  18412. ;; Adaptive filling: To get full control, first make sure that
  18413. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  18414. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  18415. (org-set-local 'org-adaptive-fill-regexp-backup
  18416. adaptive-fill-regexp))
  18417. (org-set-local 'adaptive-fill-regexp "\000")
  18418. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  18419. (org-set-local 'adaptive-fill-function
  18420. 'org-adaptive-fill-function)
  18421. (org-set-local
  18422. 'align-mode-rules-list
  18423. '((org-in-buffer-settings
  18424. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  18425. (modes . '(org-mode))))))
  18426. (defun org-fill-item-nobreak-p ()
  18427. "Non-nil when a line break at point would insert a new item."
  18428. (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
  18429. (defun org-fill-paragraph (&optional justify)
  18430. "Re-align a table, pass through to fill-paragraph if no table."
  18431. (let ((table-p (org-at-table-p))
  18432. (table.el-p (org-at-table.el-p))
  18433. (itemp (org-in-item-p)))
  18434. (cond ((and (equal (char-after (point-at-bol)) ?*)
  18435. (save-excursion (goto-char (point-at-bol))
  18436. (looking-at org-outline-regexp)))
  18437. t) ; skip headlines
  18438. (table.el-p t) ; skip table.el tables
  18439. (table-p (org-table-align) t) ; align Org tables
  18440. (itemp ; align text in items
  18441. (let* ((struct (save-excursion (goto-char itemp)
  18442. (org-list-struct)))
  18443. (parents (org-list-parents-alist struct))
  18444. (children (org-list-get-children itemp struct parents))
  18445. beg end prev next prefix)
  18446. ;; Determine in which part of item point is: before
  18447. ;; first child, after last child, between two
  18448. ;; sub-lists, or simply in item if there's no child.
  18449. (cond
  18450. ((not children)
  18451. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18452. beg itemp
  18453. end (org-list-get-item-end itemp struct)))
  18454. ((< (point) (setq next (car children)))
  18455. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18456. beg itemp
  18457. end next))
  18458. ((> (point) (setq prev (car (last children))))
  18459. (setq beg (org-list-get-item-end prev struct)
  18460. end (org-list-get-item-end itemp struct)
  18461. prefix (save-excursion
  18462. (goto-char beg)
  18463. (skip-chars-forward " \t")
  18464. (make-string (current-column) ?\ ))))
  18465. (t (catch 'exit
  18466. (while (setq next (pop children))
  18467. (if (> (point) next)
  18468. (setq prev next)
  18469. (setq beg (org-list-get-item-end prev struct)
  18470. end next
  18471. prefix (save-excursion
  18472. (goto-char beg)
  18473. (skip-chars-forward " \t")
  18474. (make-string (current-column) ?\ )))
  18475. (throw 'exit nil))))))
  18476. ;; Use `fill-paragraph' with buffer narrowed to item
  18477. ;; without any child, and with our computed PREFIX.
  18478. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18479. (save-restriction
  18480. (narrow-to-region beg end)
  18481. (save-excursion (fill-paragraph justify)))) t))
  18482. ;; Special case where point is not in a list but is on
  18483. ;; a paragraph adjacent to a list: make sure this paragraph
  18484. ;; doesn't get merged with the end of the list by narrowing
  18485. ;; buffer first.
  18486. ((save-excursion (forward-paragraph -1)
  18487. (setq itemp (org-in-item-p)))
  18488. (let ((struct (save-excursion (goto-char itemp)
  18489. (org-list-struct))))
  18490. (save-restriction
  18491. (narrow-to-region (org-list-get-bottom-point struct)
  18492. (save-excursion (forward-paragraph 1)
  18493. (point)))
  18494. (fill-paragraph justify) t)))
  18495. ;; Else simply call `fill-paragraph'.
  18496. (t nil))))
  18497. ;; For reference, this is the default value of adaptive-fill-regexp
  18498. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  18499. (defun org-adaptive-fill-function ()
  18500. "Return a fill prefix for org-mode files."
  18501. (let (itemp)
  18502. (save-excursion
  18503. (cond
  18504. ;; Comment line
  18505. ((looking-at "#[ \t]+")
  18506. (match-string-no-properties 0))
  18507. ;; Plain list item
  18508. ((org-at-item-p)
  18509. (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
  18510. ;; Point is in a list after `backward-paragraph': original
  18511. ;; point wasn't in the list, or filling would have been taken
  18512. ;; care of by `org-auto-fill-function', but the list and the
  18513. ;; real paragraph are not separated by a blank line. Thus, move
  18514. ;; point after the list to go back to real paragraph and
  18515. ;; determine fill-prefix.
  18516. ((setq itemp (org-in-item-p))
  18517. (goto-char itemp)
  18518. (let* ((struct (org-list-struct))
  18519. (bottom (org-list-get-bottom-point struct)))
  18520. (goto-char bottom)
  18521. (make-string (org-get-indentation) ?\ )))
  18522. ;; Other text
  18523. ((looking-at org-adaptive-fill-regexp-backup)
  18524. (match-string-no-properties 0))))))
  18525. (defun org-auto-fill-function ()
  18526. "Auto-fill function."
  18527. (let (itemp prefix)
  18528. ;; When in a list, compute an appropriate fill-prefix and make
  18529. ;; sure it will be used by `do-auto-fill'.
  18530. (if (setq itemp (org-in-item-p))
  18531. (progn
  18532. (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
  18533. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18534. (do-auto-fill)))
  18535. ;; Else just use `do-auto-fill'.
  18536. (do-auto-fill))))
  18537. ;;; Other stuff.
  18538. (defun org-toggle-fixed-width-section (arg)
  18539. "Toggle the fixed-width export.
  18540. If there is no active region, the QUOTE keyword at the current headline is
  18541. inserted or removed. When present, it causes the text between this headline
  18542. and the next to be exported as fixed-width text, and unmodified.
  18543. If there is an active region, this command adds or removes a colon as the
  18544. first character of this line. If the first character of a line is a colon,
  18545. this line is also exported in fixed-width font."
  18546. (interactive "P")
  18547. (let* ((cc 0)
  18548. (regionp (org-region-active-p))
  18549. (beg (if regionp (region-beginning) (point)))
  18550. (end (if regionp (region-end)))
  18551. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  18552. (case-fold-search nil)
  18553. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  18554. off)
  18555. (if regionp
  18556. (save-excursion
  18557. (goto-char beg)
  18558. (setq cc (current-column))
  18559. (beginning-of-line 1)
  18560. (setq off (looking-at re))
  18561. (while (> nlines 0)
  18562. (setq nlines (1- nlines))
  18563. (beginning-of-line 1)
  18564. (cond
  18565. (arg
  18566. (org-move-to-column cc t)
  18567. (insert ": \n")
  18568. (forward-line -1))
  18569. ((and off (looking-at re))
  18570. (replace-match "" t t nil 1))
  18571. ((not off) (org-move-to-column cc t) (insert ": ")))
  18572. (forward-line 1)))
  18573. (save-excursion
  18574. (org-back-to-heading)
  18575. (cond
  18576. ((looking-at (format org-heading-keyword-regexp-format
  18577. org-quote-string))
  18578. (goto-char (match-end 1))
  18579. (looking-at (concat " +" org-quote-string))
  18580. (replace-match "" t t)
  18581. (when (eolp) (insert " ")))
  18582. ((looking-at org-outline-regexp)
  18583. (goto-char (match-end 0))
  18584. (insert org-quote-string " ")))))))
  18585. (defun org-reftex-citation ()
  18586. "Use reftex-citation to insert a citation into the buffer.
  18587. This looks for a line like
  18588. #+BIBLIOGRAPHY: foo plain option:-d
  18589. and derives from it that foo.bib is the bibliography file relevant
  18590. for this document. It then installs the necessary environment for RefTeX
  18591. to work in this buffer and calls `reftex-citation' to insert a citation
  18592. into the buffer.
  18593. Export of such citations to both LaTeX and HTML is handled by the contributed
  18594. package org-exp-bibtex by Taru Karttunen."
  18595. (interactive)
  18596. (let ((reftex-docstruct-symbol 'rds)
  18597. (reftex-cite-format "\\cite{%l}")
  18598. rds bib)
  18599. (save-excursion
  18600. (save-restriction
  18601. (widen)
  18602. (let ((case-fold-search t)
  18603. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  18604. (if (not (save-excursion
  18605. (or (re-search-forward re nil t)
  18606. (re-search-backward re nil t))))
  18607. (error "No bibliography defined in file")
  18608. (setq bib (concat (match-string 1) ".bib")
  18609. rds (list (list 'bib bib)))))))
  18610. (call-interactively 'reftex-citation)))
  18611. ;;;; Functions extending outline functionality
  18612. (defun org-beginning-of-line (&optional arg)
  18613. "Go to the beginning of the current line. If that is invisible, continue
  18614. to a visible line beginning. This makes the function of C-a more intuitive.
  18615. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18616. first attempt, and only move to after the tags when the cursor is already
  18617. beyond the end of the headline."
  18618. (interactive "P")
  18619. (let ((pos (point))
  18620. (special (if (consp org-special-ctrl-a/e)
  18621. (car org-special-ctrl-a/e)
  18622. org-special-ctrl-a/e))
  18623. refpos)
  18624. (if (org-bound-and-true-p line-move-visual)
  18625. (beginning-of-visual-line 1)
  18626. (beginning-of-line 1))
  18627. (if (and arg (fboundp 'move-beginning-of-line))
  18628. (call-interactively 'move-beginning-of-line)
  18629. (if (bobp)
  18630. nil
  18631. (backward-char 1)
  18632. (if (org-truely-invisible-p)
  18633. (while (and (not (bobp)) (org-truely-invisible-p))
  18634. (backward-char 1)
  18635. (beginning-of-line 1))
  18636. (forward-char 1))))
  18637. (when special
  18638. (cond
  18639. ((and (looking-at org-complex-heading-regexp)
  18640. (= (char-after (match-end 1)) ?\ ))
  18641. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  18642. (point-at-eol)))
  18643. (goto-char
  18644. (if (eq special t)
  18645. (cond ((> pos refpos) refpos)
  18646. ((= pos (point)) refpos)
  18647. (t (point)))
  18648. (cond ((> pos (point)) (point))
  18649. ((not (eq last-command this-command)) (point))
  18650. (t refpos)))))
  18651. ((org-at-item-p)
  18652. (goto-char
  18653. (if (eq special t)
  18654. (cond ((> pos (match-end 0)) (match-end 0))
  18655. ((= pos (point)) (match-end 0))
  18656. (t (point)))
  18657. (cond ((> pos (point)) (point))
  18658. ((not (eq last-command this-command)) (point))
  18659. (t (match-end 0))))))))
  18660. (org-no-warnings
  18661. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18662. (defun org-end-of-line (&optional arg)
  18663. "Go to the end of the line.
  18664. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18665. first attempt, and only move to after the tags when the cursor is already
  18666. beyond the end of the headline."
  18667. (interactive "P")
  18668. (let ((special (if (consp org-special-ctrl-a/e)
  18669. (cdr org-special-ctrl-a/e)
  18670. org-special-ctrl-a/e)))
  18671. (cond
  18672. ((or (not special) arg
  18673. (not (or (org-on-heading-p) (org-at-item-p))))
  18674. (call-interactively
  18675. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  18676. ((fboundp 'move-end-of-line) 'move-end-of-line)
  18677. (t 'end-of-line))))
  18678. ((org-on-heading-p)
  18679. (let ((pos (point)))
  18680. (beginning-of-line 1)
  18681. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  18682. (if (eq special t)
  18683. (if (or (< pos (match-beginning 1))
  18684. (= pos (match-end 0)))
  18685. (goto-char (match-beginning 1))
  18686. (goto-char (match-end 0)))
  18687. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  18688. (goto-char (match-end 0))
  18689. (goto-char (match-beginning 1))))
  18690. (call-interactively (if (fboundp 'move-end-of-line)
  18691. 'move-end-of-line
  18692. 'end-of-line)))))
  18693. ;; At an item: Move before any hidden text.
  18694. (t (call-interactively 'end-of-line)))
  18695. (org-no-warnings
  18696. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18697. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  18698. (define-key org-mode-map "\C-e" 'org-end-of-line)
  18699. (defun org-backward-sentence (&optional arg)
  18700. "Go to beginning of sentence, or beginning of table field.
  18701. This will call `backward-sentence' or `org-table-beginning-of-field',
  18702. depending on context."
  18703. (interactive "P")
  18704. (cond
  18705. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  18706. (t (call-interactively 'backward-sentence))))
  18707. (defun org-forward-sentence (&optional arg)
  18708. "Go to end of sentence, or end of table field.
  18709. This will call `forward-sentence' or `org-table-end-of-field',
  18710. depending on context."
  18711. (interactive "P")
  18712. (cond
  18713. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  18714. (t (call-interactively 'forward-sentence))))
  18715. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  18716. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  18717. (defun org-kill-line (&optional arg)
  18718. "Kill line, to tags or end of line."
  18719. (interactive "P")
  18720. (cond
  18721. ((or (not org-special-ctrl-k)
  18722. (bolp)
  18723. (not (org-on-heading-p)))
  18724. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  18725. org-ctrl-k-protect-subtree)
  18726. (if (or (eq org-ctrl-k-protect-subtree 'error)
  18727. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  18728. (error "C-k aborted - would kill hidden subtree")))
  18729. (call-interactively 'kill-line))
  18730. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  18731. (kill-region (point) (match-beginning 1))
  18732. (org-set-tags nil t))
  18733. (t (kill-region (point) (point-at-eol)))))
  18734. (define-key org-mode-map "\C-k" 'org-kill-line)
  18735. (defun org-yank (&optional arg)
  18736. "Yank. If the kill is a subtree, treat it specially.
  18737. This command will look at the current kill and check if is a single
  18738. subtree, or a series of subtrees[1]. If it passes the test, and if the
  18739. cursor is at the beginning of a line or after the stars of a currently
  18740. empty headline, then the yank is handled specially. How exactly depends
  18741. on the value of the following variables, both set by default.
  18742. org-yank-folded-subtrees
  18743. When set, the subtree(s) will be folded after insertion, but only
  18744. if doing so would now swallow text after the yanked text.
  18745. org-yank-adjusted-subtrees
  18746. When set, the subtree will be promoted or demoted in order to
  18747. fit into the local outline tree structure, which means that the level
  18748. will be adjusted so that it becomes the smaller one of the two
  18749. *visible* surrounding headings.
  18750. Any prefix to this command will cause `yank' to be called directly with
  18751. no special treatment. In particular, a simple \\[universal-argument] prefix \
  18752. will just
  18753. plainly yank the text as it is.
  18754. \[1] The test checks if the first non-white line is a heading
  18755. and if there are no other headings with fewer stars."
  18756. (interactive "P")
  18757. (org-yank-generic 'yank arg))
  18758. (defun org-yank-generic (command arg)
  18759. "Perform some yank-like command.
  18760. This function implements the behavior described in the `org-yank'
  18761. documentation. However, it has been generalized to work for any
  18762. interactive command with similar behavior."
  18763. ;; pretend to be command COMMAND
  18764. (setq this-command command)
  18765. (if arg
  18766. (call-interactively command)
  18767. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  18768. (and (org-kill-is-subtree-p)
  18769. (or (bolp)
  18770. (and (looking-at "[ \t]*$")
  18771. (string-match
  18772. "\\`\\*+\\'"
  18773. (buffer-substring (point-at-bol) (point)))))))
  18774. swallowp)
  18775. (cond
  18776. ((and subtreep org-yank-folded-subtrees)
  18777. (let ((beg (point))
  18778. end)
  18779. (if (and subtreep org-yank-adjusted-subtrees)
  18780. (org-paste-subtree nil nil 'for-yank)
  18781. (call-interactively command))
  18782. (setq end (point))
  18783. (goto-char beg)
  18784. (when (and (bolp) subtreep
  18785. (not (setq swallowp
  18786. (org-yank-folding-would-swallow-text beg end))))
  18787. (org-with-limited-levels
  18788. (or (looking-at org-outline-regexp)
  18789. (re-search-forward org-outline-regexp-bol end t))
  18790. (while (and (< (point) end) (looking-at org-outline-regexp))
  18791. (hide-subtree)
  18792. (org-cycle-show-empty-lines 'folded)
  18793. (condition-case nil
  18794. (outline-forward-same-level 1)
  18795. (error (goto-char end))))))
  18796. (when swallowp
  18797. (message
  18798. "Inserted text not folded because that would swallow text"))
  18799. (goto-char end)
  18800. (skip-chars-forward " \t\n\r")
  18801. (beginning-of-line 1)
  18802. (push-mark beg 'nomsg)))
  18803. ((and subtreep org-yank-adjusted-subtrees)
  18804. (let ((beg (point-at-bol)))
  18805. (org-paste-subtree nil nil 'for-yank)
  18806. (push-mark beg 'nomsg)))
  18807. (t
  18808. (call-interactively command))))))
  18809. (defun org-yank-folding-would-swallow-text (beg end)
  18810. "Would hide-subtree at BEG swallow any text after END?"
  18811. (let (level)
  18812. (org-with-limited-levels
  18813. (save-excursion
  18814. (goto-char beg)
  18815. (when (or (looking-at org-outline-regexp)
  18816. (re-search-forward org-outline-regexp-bol end t))
  18817. (setq level (org-outline-level)))
  18818. (goto-char end)
  18819. (skip-chars-forward " \t\r\n\v\f")
  18820. (if (or (eobp)
  18821. (and (bolp) (looking-at org-outline-regexp)
  18822. (<= (org-outline-level) level)))
  18823. nil ; Nothing would be swallowed
  18824. t))))) ; something would swallow
  18825. (define-key org-mode-map "\C-y" 'org-yank)
  18826. (defun org-truely-invisible-p ()
  18827. "Check if point is at a character currently not visible.
  18828. This version does not only check the character property, but also
  18829. `visible-mode'."
  18830. ;; Early versions of noutline don't have `outline-invisible-p'.
  18831. (if (org-bound-and-true-p visible-mode)
  18832. nil
  18833. (outline-invisible-p)))
  18834. (defun org-invisible-p2 ()
  18835. "Check if point is at a character currently not visible."
  18836. (save-excursion
  18837. (if (and (eolp) (not (bobp))) (backward-char 1))
  18838. ;; Early versions of noutline don't have `outline-invisible-p'.
  18839. (outline-invisible-p)))
  18840. (defun org-back-to-heading (&optional invisible-ok)
  18841. "Call `outline-back-to-heading', but provide a better error message."
  18842. (condition-case nil
  18843. (outline-back-to-heading invisible-ok)
  18844. (error (error "Before first headline at position %d in buffer %s"
  18845. (point) (current-buffer)))))
  18846. (defun org-beginning-of-defun ()
  18847. "Go to the beginning of the subtree, i.e. back to the heading."
  18848. (org-back-to-heading))
  18849. (defun org-end-of-defun ()
  18850. "Go to the end of the subtree."
  18851. (org-end-of-subtree nil t))
  18852. (defun org-before-first-heading-p ()
  18853. "Before first heading?"
  18854. (save-excursion
  18855. (end-of-line)
  18856. (null (re-search-backward org-outline-regexp-bol nil t))))
  18857. (defun org-on-heading-p (&optional ignored)
  18858. (outline-on-heading-p t))
  18859. (defun org-at-heading-p (&optional ignored)
  18860. (outline-on-heading-p t))
  18861. (defun org-point-at-end-of-empty-headline ()
  18862. "If point is at the end of an empty headline, return t, else nil.
  18863. If the heading only contains a TODO keyword, it is still still considered
  18864. empty."
  18865. (and (looking-at "[ \t]*$")
  18866. (save-excursion
  18867. (beginning-of-line 1)
  18868. (let ((case-fold-search nil))
  18869. (looking-at org-todo-line-regexp)))
  18870. (string= (match-string 3) "")))
  18871. (defun org-at-heading-or-item-p ()
  18872. (or (org-on-heading-p) (org-at-item-p)))
  18873. (defun org-on-target-p ()
  18874. (or (org-in-regexp org-radio-target-regexp)
  18875. (org-in-regexp org-target-regexp)))
  18876. (defun org-up-heading-all (arg)
  18877. "Move to the heading line of which the present line is a subheading.
  18878. This function considers both visible and invisible heading lines.
  18879. With argument, move up ARG levels."
  18880. (if (fboundp 'outline-up-heading-all)
  18881. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  18882. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  18883. (defun org-up-heading-safe ()
  18884. "Move to the heading line of which the present line is a subheading.
  18885. This version will not throw an error. It will return the level of the
  18886. headline found, or nil if no higher level is found.
  18887. Also, this function will be a lot faster than `outline-up-heading',
  18888. because it relies on stars being the outline starters. This can really
  18889. make a significant difference in outlines with very many siblings."
  18890. (let (start-level re)
  18891. (org-back-to-heading t)
  18892. (setq start-level (funcall outline-level))
  18893. (if (equal start-level 1)
  18894. nil
  18895. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  18896. (if (re-search-backward re nil t)
  18897. (funcall outline-level)))))
  18898. (defun org-first-sibling-p ()
  18899. "Is this heading the first child of its parents?"
  18900. (interactive)
  18901. (let ((re org-outline-regexp-bol)
  18902. level l)
  18903. (unless (org-at-heading-p t)
  18904. (error "Not at a heading"))
  18905. (setq level (funcall outline-level))
  18906. (save-excursion
  18907. (if (not (re-search-backward re nil t))
  18908. t
  18909. (setq l (funcall outline-level))
  18910. (< l level)))))
  18911. (defun org-goto-sibling (&optional previous)
  18912. "Goto the next sibling, even if it is invisible.
  18913. When PREVIOUS is set, go to the previous sibling instead. Returns t
  18914. when a sibling was found. When none is found, return nil and don't
  18915. move point."
  18916. (let ((fun (if previous 're-search-backward 're-search-forward))
  18917. (pos (point))
  18918. (re org-outline-regexp-bol)
  18919. level l)
  18920. (when (condition-case nil (org-back-to-heading t) (error nil))
  18921. (setq level (funcall outline-level))
  18922. (catch 'exit
  18923. (or previous (forward-char 1))
  18924. (while (funcall fun re nil t)
  18925. (setq l (funcall outline-level))
  18926. (when (< l level) (goto-char pos) (throw 'exit nil))
  18927. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  18928. (goto-char pos)
  18929. nil))))
  18930. (defun org-show-siblings ()
  18931. "Show all siblings of the current headline."
  18932. (save-excursion
  18933. (while (org-goto-sibling) (org-flag-heading nil)))
  18934. (save-excursion
  18935. (while (org-goto-sibling 'previous)
  18936. (org-flag-heading nil))))
  18937. (defun org-goto-first-child ()
  18938. "Goto the first child, even if it is invisible.
  18939. Return t when a child was found. Otherwise don't move point and
  18940. return nil."
  18941. (let (level (pos (point)) (re org-outline-regexp-bol))
  18942. (when (condition-case nil (org-back-to-heading t) (error nil))
  18943. (setq level (outline-level))
  18944. (forward-char 1)
  18945. (if (and (re-search-forward re nil t) (> (outline-level) level))
  18946. (progn (goto-char (match-beginning 0)) t)
  18947. (goto-char pos) nil))))
  18948. (defun org-show-hidden-entry ()
  18949. "Show an entry where even the heading is hidden."
  18950. (save-excursion
  18951. (org-show-entry)))
  18952. (defun org-flag-heading (flag &optional entry)
  18953. "Flag the current heading. FLAG non-nil means make invisible.
  18954. When ENTRY is non-nil, show the entire entry."
  18955. (save-excursion
  18956. (org-back-to-heading t)
  18957. ;; Check if we should show the entire entry
  18958. (if entry
  18959. (progn
  18960. (org-show-entry)
  18961. (save-excursion
  18962. (and (outline-next-heading)
  18963. (org-flag-heading nil))))
  18964. (outline-flag-region (max (point-min) (1- (point)))
  18965. (save-excursion (outline-end-of-heading) (point))
  18966. flag))))
  18967. (defun org-get-next-sibling ()
  18968. "Move to next heading of the same level, and return point.
  18969. If there is no such heading, return nil.
  18970. This is like outline-next-sibling, but invisible headings are ok."
  18971. (let ((level (funcall outline-level)))
  18972. (outline-next-heading)
  18973. (while (and (not (eobp)) (> (funcall outline-level) level))
  18974. (outline-next-heading))
  18975. (if (or (eobp) (< (funcall outline-level) level))
  18976. nil
  18977. (point))))
  18978. (defun org-get-last-sibling ()
  18979. "Move to previous heading of the same level, and return point.
  18980. If there is no such heading, return nil."
  18981. (let ((opoint (point))
  18982. (level (funcall outline-level)))
  18983. (outline-previous-heading)
  18984. (when (and (/= (point) opoint) (outline-on-heading-p t))
  18985. (while (and (> (funcall outline-level) level)
  18986. (not (bobp)))
  18987. (outline-previous-heading))
  18988. (if (< (funcall outline-level) level)
  18989. nil
  18990. (point)))))
  18991. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  18992. ;; This contains an exact copy of the original function, but it uses
  18993. ;; `org-back-to-heading', to make it work also in invisible
  18994. ;; trees. And is uses an invisible-OK argument.
  18995. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  18996. ;; Furthermore, when used inside Org, finding the end of a large subtree
  18997. ;; with many children and grandchildren etc, this can be much faster
  18998. ;; than the outline version.
  18999. (org-back-to-heading invisible-OK)
  19000. (let ((first t)
  19001. (level (funcall outline-level)))
  19002. (if (and (eq major-mode 'org-mode) (< level 1000))
  19003. ;; A true heading (not a plain list item), in Org-mode
  19004. ;; This means we can easily find the end by looking
  19005. ;; only for the right number of stars. Using a regexp to do
  19006. ;; this is so much faster than using a Lisp loop.
  19007. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  19008. (forward-char 1)
  19009. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  19010. ;; something else, do it the slow way
  19011. (while (and (not (eobp))
  19012. (or first (> (funcall outline-level) level)))
  19013. (setq first nil)
  19014. (outline-next-heading)))
  19015. (unless to-heading
  19016. (if (memq (preceding-char) '(?\n ?\^M))
  19017. (progn
  19018. ;; Go to end of line before heading
  19019. (forward-char -1)
  19020. (if (memq (preceding-char) '(?\n ?\^M))
  19021. ;; leave blank line before heading
  19022. (forward-char -1))))))
  19023. (point))
  19024. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  19025. "Use Org version in org-mode, for dramatic speed-up."
  19026. (if (eq major-mode 'org-mode)
  19027. (progn
  19028. (org-end-of-subtree nil t)
  19029. (unless (eobp) (backward-char 1)))
  19030. ad-do-it))
  19031. (defun org-end-of-meta-data-and-drawers ()
  19032. "Jump to the first text after meta data and drawers in the current entry.
  19033. This will move over empty lines, lines with planning time stamps,
  19034. clocking lines, and drawers."
  19035. (org-back-to-heading t)
  19036. (let ((end (save-excursion (outline-next-heading) (point)))
  19037. (re (concat "\\(" org-drawer-regexp "\\)"
  19038. "\\|" "[ \t]*" org-keyword-time-regexp)))
  19039. (forward-line 1)
  19040. (while (re-search-forward re end t)
  19041. (if (not (match-end 1))
  19042. ;; empty or planning line
  19043. (forward-line 1)
  19044. ;; a drawer, find the end
  19045. (re-search-forward "^[ \t]*:END:" end 'move)
  19046. (forward-line 1)))
  19047. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  19048. (point)))
  19049. (defun org-forward-same-level (arg &optional invisible-ok)
  19050. "Move forward to the arg'th subheading at same level as this one.
  19051. Stop at the first and last subheadings of a superior heading.
  19052. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  19053. it wil also look at invisible ones."
  19054. (interactive "p")
  19055. (org-back-to-heading invisible-ok)
  19056. (org-on-heading-p)
  19057. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19058. (re (format "^\\*\\{1,%d\\} " level))
  19059. l)
  19060. (forward-char 1)
  19061. (while (> arg 0)
  19062. (while (and (re-search-forward re nil 'move)
  19063. (setq l (- (match-end 0) (match-beginning 0) 1))
  19064. (= l level)
  19065. (not invisible-ok)
  19066. (progn (backward-char 1) (outline-invisible-p)))
  19067. (if (< l level) (setq arg 1)))
  19068. (setq arg (1- arg)))
  19069. (beginning-of-line 1)))
  19070. (defun org-backward-same-level (arg &optional invisible-ok)
  19071. "Move backward to the arg'th subheading at same level as this one.
  19072. Stop at the first and last subheadings of a superior heading."
  19073. (interactive "p")
  19074. (org-back-to-heading)
  19075. (org-on-heading-p)
  19076. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19077. (re (format "^\\*\\{1,%d\\} " level))
  19078. l)
  19079. (while (> arg 0)
  19080. (while (and (re-search-backward re nil 'move)
  19081. (setq l (- (match-end 0) (match-beginning 0) 1))
  19082. (= l level)
  19083. (not invisible-ok)
  19084. (outline-invisible-p))
  19085. (if (< l level) (setq arg 1)))
  19086. (setq arg (1- arg)))))
  19087. (defun org-show-subtree ()
  19088. "Show everything after this heading at deeper levels."
  19089. (outline-flag-region
  19090. (point)
  19091. (save-excursion
  19092. (org-end-of-subtree t t))
  19093. nil))
  19094. (defun org-show-entry ()
  19095. "Show the body directly following this heading.
  19096. Show the heading too, if it is currently invisible."
  19097. (interactive)
  19098. (save-excursion
  19099. (condition-case nil
  19100. (progn
  19101. (org-back-to-heading t)
  19102. (outline-flag-region
  19103. (max (point-min) (1- (point)))
  19104. (save-excursion
  19105. (if (re-search-forward
  19106. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  19107. (match-beginning 1)
  19108. (point-max)))
  19109. nil)
  19110. (org-cycle-hide-drawers 'children))
  19111. (error nil))))
  19112. (defun org-make-options-regexp (kwds &optional extra)
  19113. "Make a regular expression for keyword lines."
  19114. (concat
  19115. "^"
  19116. "#?[ \t]*\\+\\("
  19117. (mapconcat 'regexp-quote kwds "\\|")
  19118. (if extra (concat "\\|" extra))
  19119. "\\):[ \t]*"
  19120. "\\(.*\\)"))
  19121. ;; Make isearch reveal the necessary context
  19122. (defun org-isearch-end ()
  19123. "Reveal context after isearch exits."
  19124. (when isearch-success ; only if search was successful
  19125. (if (featurep 'xemacs)
  19126. ;; Under XEmacs, the hook is run in the correct place,
  19127. ;; we directly show the context.
  19128. (org-show-context 'isearch)
  19129. ;; In Emacs the hook runs *before* restoring the overlays.
  19130. ;; So we have to use a one-time post-command-hook to do this.
  19131. ;; (Emacs 22 has a special variable, see function `org-mode')
  19132. (unless (and (boundp 'isearch-mode-end-hook-quit)
  19133. isearch-mode-end-hook-quit)
  19134. ;; Only when the isearch was not quitted.
  19135. (org-add-hook 'post-command-hook 'org-isearch-post-command
  19136. 'append 'local)))))
  19137. (defun org-isearch-post-command ()
  19138. "Remove self from hook, and show context."
  19139. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  19140. (org-show-context 'isearch))
  19141. ;;;; Integration with and fixes for other packages
  19142. ;;; Imenu support
  19143. (defvar org-imenu-markers nil
  19144. "All markers currently used by Imenu.")
  19145. (make-variable-buffer-local 'org-imenu-markers)
  19146. (defun org-imenu-new-marker (&optional pos)
  19147. "Return a new marker for use by Imenu, and remember the marker."
  19148. (let ((m (make-marker)))
  19149. (move-marker m (or pos (point)))
  19150. (push m org-imenu-markers)
  19151. m))
  19152. (defun org-imenu-get-tree ()
  19153. "Produce the index for Imenu."
  19154. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  19155. (setq org-imenu-markers nil)
  19156. (let* ((n org-imenu-depth)
  19157. (re (concat "^" (org-get-limited-outline-regexp)))
  19158. (subs (make-vector (1+ n) nil))
  19159. (last-level 0)
  19160. m level head)
  19161. (save-excursion
  19162. (save-restriction
  19163. (widen)
  19164. (goto-char (point-max))
  19165. (while (re-search-backward re nil t)
  19166. (setq level (org-reduced-level (funcall outline-level)))
  19167. (when (<= level n)
  19168. (looking-at org-complex-heading-regexp)
  19169. (setq head (org-link-display-format
  19170. (org-match-string-no-properties 4))
  19171. m (org-imenu-new-marker))
  19172. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  19173. (if (>= level last-level)
  19174. (push (cons head m) (aref subs level))
  19175. (push (cons head (aref subs (1+ level))) (aref subs level))
  19176. (loop for i from (1+ level) to n do (aset subs i nil)))
  19177. (setq last-level level)))))
  19178. (aref subs 1)))
  19179. (eval-after-load "imenu"
  19180. '(progn
  19181. (add-hook 'imenu-after-jump-hook
  19182. (lambda ()
  19183. (if (eq major-mode 'org-mode)
  19184. (org-show-context 'org-goto))))))
  19185. (defun org-link-display-format (link)
  19186. "Replace a link with either the description, or the link target
  19187. if no description is present"
  19188. (save-match-data
  19189. (if (string-match org-bracket-link-analytic-regexp link)
  19190. (replace-match (if (match-end 5)
  19191. (match-string 5 link)
  19192. (concat (match-string 1 link)
  19193. (match-string 3 link)))
  19194. nil t link)
  19195. link)))
  19196. (defun org-toggle-link-display ()
  19197. "Toggle the literal or descriptive display of links."
  19198. (interactive)
  19199. (if org-descriptive-links
  19200. (progn (org-remove-from-invisibility-spec '(org-link))
  19201. (org-restart-font-lock)
  19202. (setq org-descriptive-links nil))
  19203. (progn (add-to-invisibility-spec '(org-link))
  19204. (org-restart-font-lock)
  19205. (setq org-descriptive-links t))))
  19206. ;; Speedbar support
  19207. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  19208. "Overlay marking the agenda restriction line in speedbar.")
  19209. (overlay-put org-speedbar-restriction-lock-overlay
  19210. 'face 'org-agenda-restriction-lock)
  19211. (overlay-put org-speedbar-restriction-lock-overlay
  19212. 'help-echo "Agendas are currently limited to this item.")
  19213. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  19214. (defun org-speedbar-set-agenda-restriction ()
  19215. "Restrict future agenda commands to the location at point in speedbar.
  19216. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  19217. (interactive)
  19218. (require 'org-agenda)
  19219. (let (p m tp np dir txt)
  19220. (cond
  19221. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19222. 'org-imenu t))
  19223. (setq m (get-text-property p 'org-imenu-marker))
  19224. (with-current-buffer (marker-buffer m)
  19225. (goto-char m)
  19226. (org-agenda-set-restriction-lock 'subtree)))
  19227. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19228. 'speedbar-function 'speedbar-find-file))
  19229. (setq tp (previous-single-property-change
  19230. (1+ p) 'speedbar-function)
  19231. np (next-single-property-change
  19232. tp 'speedbar-function)
  19233. dir (speedbar-line-directory)
  19234. txt (buffer-substring-no-properties (or tp (point-min))
  19235. (or np (point-max))))
  19236. (with-current-buffer (find-file-noselect
  19237. (let ((default-directory dir))
  19238. (expand-file-name txt)))
  19239. (unless (eq major-mode 'org-mode)
  19240. (error "Cannot restrict to non-Org-mode file"))
  19241. (org-agenda-set-restriction-lock 'file)))
  19242. (t (error "Don't know how to restrict Org-mode's agenda")))
  19243. (move-overlay org-speedbar-restriction-lock-overlay
  19244. (point-at-bol) (point-at-eol))
  19245. (setq current-prefix-arg nil)
  19246. (org-agenda-maybe-redo)))
  19247. (eval-after-load "speedbar"
  19248. '(progn
  19249. (speedbar-add-supported-extension ".org")
  19250. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  19251. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  19252. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  19253. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  19254. (add-hook 'speedbar-visiting-tag-hook
  19255. (lambda () (and (eq major-mode 'org-mode) (org-show-context 'org-goto))))))
  19256. ;;; Fixes and Hacks for problems with other packages
  19257. ;; Make flyspell not check words in links, to not mess up our keymap
  19258. (defun org-mode-flyspell-verify ()
  19259. "Don't let flyspell put overlays at active buttons, or on
  19260. {todo,all-time,additional-option-like}-keywords."
  19261. (let ((pos (max (1- (point)) (point-min)))
  19262. (word (thing-at-point 'word)))
  19263. (and (not (get-text-property pos 'keymap))
  19264. (not (get-text-property pos 'org-no-flyspell))
  19265. (not (member word org-todo-keywords-1))
  19266. (not (member word org-all-time-keywords))
  19267. (not (member word org-additional-option-like-keywords)))))
  19268. (defun org-remove-flyspell-overlays-in (beg end)
  19269. "Remove flyspell overlays in region."
  19270. (and (org-bound-and-true-p flyspell-mode)
  19271. (fboundp 'flyspell-delete-region-overlays)
  19272. (flyspell-delete-region-overlays beg end))
  19273. (add-text-properties beg end '(org-no-flyspell t)))
  19274. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  19275. (eval-after-load "bookmark"
  19276. '(if (boundp 'bookmark-after-jump-hook)
  19277. ;; We can use the hook
  19278. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  19279. ;; Hook not available, use advice
  19280. (defadvice bookmark-jump (after org-make-visible activate)
  19281. "Make the position visible."
  19282. (org-bookmark-jump-unhide))))
  19283. ;; Make sure saveplace shows the location if it was hidden
  19284. (eval-after-load "saveplace"
  19285. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  19286. "Make the position visible."
  19287. (org-bookmark-jump-unhide)))
  19288. ;; Make sure ecb shows the location if it was hidden
  19289. (eval-after-load "ecb"
  19290. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  19291. "Make hierarchy visible when jumping into location from ECB tree buffer."
  19292. (if (eq major-mode 'org-mode)
  19293. (org-show-context))))
  19294. (defun org-bookmark-jump-unhide ()
  19295. "Unhide the current position, to show the bookmark location."
  19296. (and (eq major-mode 'org-mode)
  19297. (or (outline-invisible-p)
  19298. (save-excursion (goto-char (max (point-min) (1- (point))))
  19299. (outline-invisible-p)))
  19300. (org-show-context 'bookmark-jump)))
  19301. ;; Make session.el ignore our circular variable
  19302. (eval-after-load "session"
  19303. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  19304. ;;;; Experimental code
  19305. (defun org-closed-in-range ()
  19306. "Sparse tree of items closed in a certain time range.
  19307. Still experimental, may disappear in the future."
  19308. (interactive)
  19309. ;; Get the time interval from the user.
  19310. (let* ((time1 (org-float-time
  19311. (org-read-date nil 'to-time nil "Starting date: ")))
  19312. (time2 (org-float-time
  19313. (org-read-date nil 'to-time nil "End date:")))
  19314. ;; callback function
  19315. (callback (lambda ()
  19316. (let ((time
  19317. (org-float-time
  19318. (apply 'encode-time
  19319. (org-parse-time-string
  19320. (match-string 1))))))
  19321. ;; check if time in interval
  19322. (and (>= time time1) (<= time time2))))))
  19323. ;; make tree, check each match with the callback
  19324. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  19325. ;;;; Finish up
  19326. (provide 'org)
  19327. (run-hooks 'org-load-hook)
  19328. ;;; org.el ends here