org.el 855 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2012 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. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum))
  70. (require 'calendar)
  71. (require 'find-func)
  72. (require 'format-spec)
  73. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  74. ;; some places -- e.g. see the macro org-with-limited-levels.
  75. ;;
  76. ;; In Org buffers, the value of `outline-regexp' is that of
  77. ;; `org-outline-regexp'. The only function still directly relying on
  78. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  79. ;; job when `orgstruct-mode' is active.
  80. (defvar org-outline-regexp "\\*+ "
  81. "Regexp to match Org headlines.")
  82. (defvar org-outline-regexp-bol "^\\*+ "
  83. "Regexp to match Org headlines.
  84. This is similar to `org-outline-regexp' but additionally makes
  85. sure that we are at the beginning of the line.")
  86. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  87. "Matches an headline, putting stars and text into groups.
  88. Stars are put in group 1 and the trimmed body in group 2.")
  89. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  90. (when (fboundp 'defvaralias)
  91. (unless (boundp 'calendar-view-holidays-initially-flag)
  92. (defvaralias 'calendar-view-holidays-initially-flag
  93. 'view-calendar-holidays-initially))
  94. (unless (boundp 'calendar-view-diary-initially-flag)
  95. (defvaralias 'calendar-view-diary-initially-flag
  96. 'view-diary-entries-initially))
  97. (unless (boundp 'diary-fancy-buffer)
  98. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  99. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  100. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  101. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  102. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  103. (declare-function org-at-clock-log-p "org-clock" ())
  104. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  105. (declare-function org-clock-timestamps-up "org-clock" ())
  106. (declare-function org-clock-timestamps-down "org-clock" ())
  107. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  108. ;; load languages based on value of `org-babel-load-languages'
  109. (defvar org-babel-load-languages)
  110. ;;;###autoload
  111. (defun org-babel-do-load-languages (sym value)
  112. "Load the languages defined in `org-babel-load-languages'."
  113. (set-default sym value)
  114. (mapc (lambda (pair)
  115. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  116. (if active
  117. (progn
  118. (require (intern (concat "ob-" lang))))
  119. (progn
  120. (funcall 'fmakunbound
  121. (intern (concat "org-babel-execute:" lang)))
  122. (funcall 'fmakunbound
  123. (intern (concat "org-babel-expand-body:" lang)))))))
  124. org-babel-load-languages))
  125. (defcustom org-babel-load-languages '((emacs-lisp . t))
  126. "Languages which can be evaluated in Org-mode buffers.
  127. This list can be used to load support for any of the languages
  128. below, note that each language will depend on a different set of
  129. system executables and/or Emacs modes. When a language is
  130. \"loaded\", then code blocks in that language can be evaluated
  131. with `org-babel-execute-src-block' bound by default to C-c
  132. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  133. be set to remove code block evaluation from the C-c C-c
  134. keybinding. By default only Emacs Lisp (which has no
  135. requirements) is loaded."
  136. :group 'org-babel
  137. :set 'org-babel-do-load-languages
  138. :version "24.1"
  139. :type '(alist :tag "Babel Languages"
  140. :key-type
  141. (choice
  142. (const :tag "Awk" awk)
  143. (const :tag "C" C)
  144. (const :tag "R" R)
  145. (const :tag "Asymptote" asymptote)
  146. (const :tag "Calc" calc)
  147. (const :tag "Clojure" clojure)
  148. (const :tag "CSS" css)
  149. (const :tag "Ditaa" ditaa)
  150. (const :tag "Dot" dot)
  151. (const :tag "Emacs Lisp" emacs-lisp)
  152. (const :tag "Fortran" fortran)
  153. (const :tag "Gnuplot" gnuplot)
  154. (const :tag "Haskell" haskell)
  155. (const :tag "IO" io)
  156. (const :tag "Java" java)
  157. (const :tag "Javascript" js)
  158. (const :tag "LaTeX" latex)
  159. (const :tag "Ledger" ledger)
  160. (const :tag "Lilypond" lilypond)
  161. (const :tag "Lisp" lisp)
  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 "Scala" scala)
  175. (const :tag "Scheme" scheme)
  176. (const :tag "Screen" screen)
  177. (const :tag "Shell Script" sh)
  178. (const :tag "Shen" shen)
  179. (const :tag "Sql" sql)
  180. (const :tag "Sqlite" sqlite))
  181. :value-type (boolean :tag "Activate" :value t)))
  182. ;;;; Customization variables
  183. (defcustom org-clone-delete-id nil
  184. "Remove ID property of clones of a subtree.
  185. When non-nil, clones of a subtree don't inherit the ID property.
  186. Otherwise they inherit the ID property with a new unique
  187. identifier."
  188. :type 'boolean
  189. :version "24.1"
  190. :group 'org-id)
  191. ;;; Version
  192. (require 'org-compat)
  193. (org-check-version)
  194. ;;;###autoload
  195. (defun org-version (&optional here full message)
  196. "Show the org-mode version in the echo area.
  197. With prefix argument HERE, insert it at point.
  198. When FULL is non-nil, use a verbose version string.
  199. When MESSAGE is non-nil, display a message with the version."
  200. (interactive "P")
  201. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  202. (org-install-dir (ignore-errors (org-find-library-dir "org-install.el")))
  203. (org-trash (or
  204. (and (fboundp 'org-release) (fboundp 'org-git-version))
  205. (load (concat org-dir "org-version.el")
  206. 'noerror 'nomessage 'nosuffix)))
  207. (org-version (org-release))
  208. (git-version (org-git-version))
  209. (version (format "Org-mode version %s (%s @ %s)"
  210. org-version
  211. git-version
  212. (if org-install-dir
  213. (if (string= org-dir org-install-dir)
  214. org-install-dir
  215. (concat "mixed installation! " org-install-dir " and " org-dir))
  216. "org-install.el can not be found!")))
  217. (_version (if full version org-version)))
  218. (if (org-called-interactively-p 'interactive)
  219. (if here
  220. (insert version)
  221. (message version))
  222. (if message (message _version))
  223. _version)))
  224. (defconst org-version (org-version))
  225. ;;; Compatibility constants
  226. ;;; The custom variables
  227. (defgroup org nil
  228. "Outline-based notes management and organizer."
  229. :tag "Org"
  230. :group 'outlines
  231. :group 'calendar)
  232. (defcustom org-mode-hook nil
  233. "Mode hook for Org-mode, run after the mode was turned on."
  234. :group 'org
  235. :type 'hook)
  236. (defcustom org-load-hook nil
  237. "Hook that is run after org.el has been loaded."
  238. :group 'org
  239. :type 'hook)
  240. (defcustom org-log-buffer-setup-hook nil
  241. "Hook that is run after an Org log buffer is created."
  242. :group 'org
  243. :version "24.1"
  244. :type 'hook)
  245. (defvar org-modules) ; defined below
  246. (defvar org-modules-loaded nil
  247. "Have the modules been loaded already?")
  248. (defun org-load-modules-maybe (&optional force)
  249. "Load all extensions listed in `org-modules'."
  250. (when (or force (not org-modules-loaded))
  251. (mapc (lambda (ext)
  252. (condition-case nil (require ext)
  253. (error (message "Problems while trying to load feature `%s'" ext))))
  254. org-modules)
  255. (setq org-modules-loaded t)))
  256. (defun org-set-modules (var value)
  257. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  258. (set var value)
  259. (when (featurep 'org)
  260. (org-load-modules-maybe 'force)))
  261. (when (org-bound-and-true-p org-modules)
  262. (let ((a (member 'org-infojs org-modules)))
  263. (and a (setcar a 'org-jsinfo))))
  264. (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)
  265. "Modules that should always be loaded together with org.el.
  266. If a description starts with <C>, the file is not part of Emacs
  267. and loading it will require that you have downloaded and properly installed
  268. the org-mode distribution.
  269. You can also use this system to load external packages (i.e. neither Org
  270. core modules, nor modules from the CONTRIB directory). Just add symbols
  271. to the end of the list. If the package is called org-xyz.el, then you need
  272. to add the symbol `xyz', and the package must have a call to
  273. (provide 'org-xyz)"
  274. :group 'org
  275. :set 'org-set-modules
  276. :type
  277. '(set :greedy t
  278. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  279. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  280. (const :tag " crypt: Encryption of subtrees" org-crypt)
  281. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  282. (const :tag " docview: Links to doc-view buffers" org-docview)
  283. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  284. (const :tag " id: Global IDs for identifying entries" org-id)
  285. (const :tag " info: Links to Info nodes" org-info)
  286. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  287. (const :tag " habit: Track your consistency with habits" org-habit)
  288. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  289. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  290. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  291. (const :tag " mew Links to Mew folders/messages" org-mew)
  292. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  293. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  294. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  295. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  296. (const :tag " vm: Links to VM folders/messages" org-vm)
  297. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  298. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  299. (const :tag " mouse: Additional mouse support" org-mouse)
  300. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  301. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  302. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  303. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  304. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  305. (const :tag "C collector: Collect properties into tables" org-collector)
  306. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  307. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  308. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  309. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  310. (const :tag "C eval: Include command output as text" org-eval)
  311. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  312. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  313. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  314. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  315. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  316. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  317. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  318. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  319. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  320. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  321. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  322. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  323. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  324. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  325. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  326. (const :tag "C registry: A registry for Org-mode links" org-registry)
  327. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  328. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  329. (const :tag "C secretary: Team management with org-mode" org-secretary)
  330. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  331. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  332. (const :tag "C track: Keep up with Org-mode development" org-track)
  333. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  334. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  335. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  336. (defcustom org-support-shift-select nil
  337. "Non-nil means make shift-cursor commands select text when possible.
  338. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  339. start selecting a region, or enlarge regions started in this way.
  340. In Org-mode, in special contexts, these same keys are used for
  341. other purposes, important enough to compete with shift selection.
  342. Org tries to balance these needs by supporting `shift-select-mode'
  343. outside these special contexts, under control of this variable.
  344. The default of this variable is nil, to avoid confusing behavior. Shifted
  345. cursor keys will then execute Org commands in the following contexts:
  346. - on a headline, changing TODO state (left/right) and priority (up/down)
  347. - on a time stamp, changing the time
  348. - in a plain list item, changing the bullet type
  349. - in a property definition line, switching between allowed values
  350. - in the BEGIN line of a clock table (changing the time block).
  351. Outside these contexts, the commands will throw an error.
  352. When this variable is t and the cursor is not in a special
  353. context, Org-mode will support shift-selection for making and
  354. enlarging regions. To make this more effective, the bullet
  355. cycling will no longer happen anywhere in an item line, but only
  356. if the cursor is exactly on the bullet.
  357. If you set this variable to the symbol `always', then the keys
  358. will not be special in headlines, property lines, and item lines,
  359. to make shift selection work there as well. If this is what you
  360. want, you can use the following alternative commands: `C-c C-t'
  361. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  362. can be used to switch TODO sets, `C-c -' to cycle item bullet
  363. types, and properties can be edited by hand or in column view.
  364. However, when the cursor is on a timestamp, shift-cursor commands
  365. will still edit the time stamp - this is just too good to give up.
  366. XEmacs user should have this variable set to nil, because
  367. `shift-select-mode' is in Emacs 23 or later only."
  368. :group 'org
  369. :type '(choice
  370. (const :tag "Never" nil)
  371. (const :tag "When outside special context" t)
  372. (const :tag "Everywhere except timestamps" always)))
  373. (defcustom org-loop-over-headlines-in-active-region nil
  374. "Shall some commands act upon headlines in the active region?
  375. When set to `t', some commands will be performed in all headlines
  376. within the active region.
  377. When set to `start-level', some commands will be performed in all
  378. headlines within the active region, provided that these headlines
  379. are of the same level than the first one.
  380. When set to a string, those commands will be performed on the
  381. matching headlines within the active region. Such string must be
  382. a tags/property/todo match as it is used in the agenda tags view.
  383. The list of commands is: `org-schedule', `org-deadline',
  384. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  385. `org-archive-to-archive-sibling'. The archiving commands skip
  386. already archived entries."
  387. :type '(choice (const :tag "Don't loop" nil)
  388. (const :tag "All headlines in active region" t)
  389. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  390. (string :tag "Tags/Property/Todo matcher"))
  391. :version "24.1"
  392. :group 'org-todo
  393. :group 'org-archive)
  394. (defgroup org-startup nil
  395. "Options concerning startup of Org-mode."
  396. :tag "Org Startup"
  397. :group 'org)
  398. (defcustom org-startup-folded t
  399. "Non-nil means entering Org-mode will switch to OVERVIEW.
  400. This can also be configured on a per-file basis by adding one of
  401. the following lines anywhere in the buffer:
  402. #+STARTUP: fold (or `overview', this is equivalent)
  403. #+STARTUP: nofold (or `showall', this is equivalent)
  404. #+STARTUP: content
  405. #+STARTUP: showeverything"
  406. :group 'org-startup
  407. :type '(choice
  408. (const :tag "nofold: show all" nil)
  409. (const :tag "fold: overview" t)
  410. (const :tag "content: all headlines" content)
  411. (const :tag "show everything, even drawers" showeverything)))
  412. (defcustom org-startup-truncated t
  413. "Non-nil means entering Org-mode will set `truncate-lines'.
  414. This is useful since some lines containing links can be very long and
  415. uninteresting. Also tables look terrible when wrapped."
  416. :group 'org-startup
  417. :type 'boolean)
  418. (defcustom org-startup-indented nil
  419. "Non-nil means turn on `org-indent-mode' on startup.
  420. This can also be configured on a per-file basis by adding one of
  421. the following lines anywhere in the buffer:
  422. #+STARTUP: indent
  423. #+STARTUP: noindent"
  424. :group 'org-structure
  425. :type '(choice
  426. (const :tag "Not" nil)
  427. (const :tag "Globally (slow on startup in large files)" t)))
  428. (defcustom org-use-sub-superscripts t
  429. "Non-nil means interpret \"_\" and \"^\" for export.
  430. When this option is turned on, you can use TeX-like syntax for sub- and
  431. superscripts. Several characters after \"_\" or \"^\" will be
  432. considered as a single item - so grouping with {} is normally not
  433. needed. For example, the following things will be parsed as single
  434. sub- or superscripts.
  435. 10^24 or 10^tau several digits will be considered 1 item.
  436. 10^-12 or 10^-tau a leading sign with digits or a word
  437. x^2-y^3 will be read as x^2 - y^3, because items are
  438. terminated by almost any nonword/nondigit char.
  439. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  440. Still, ambiguity is possible - so when in doubt use {} to enclose the
  441. sub/superscript. If you set this variable to the symbol `{}',
  442. the braces are *required* in order to trigger interpretations as
  443. sub/superscript. This can be helpful in documents that need \"_\"
  444. frequently in plain text.
  445. Not all export backends support this, but HTML does.
  446. This option can also be set with the #+OPTIONS line, e.g. \"^:nil\"."
  447. :group 'org-startup
  448. :group 'org-export-translation
  449. :version "24.1"
  450. :type '(choice
  451. (const :tag "Always interpret" t)
  452. (const :tag "Only with braces" {})
  453. (const :tag "Never interpret" nil)))
  454. (if (fboundp 'defvaralias)
  455. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  456. (defcustom org-startup-with-beamer-mode nil
  457. "Non-nil means turn on `org-beamer-mode' on startup.
  458. This can also be configured on a per-file basis by adding one of
  459. the following lines anywhere in the buffer:
  460. #+STARTUP: beamer"
  461. :group 'org-startup
  462. :version "24.1"
  463. :type 'boolean)
  464. (defcustom org-startup-align-all-tables nil
  465. "Non-nil means align all tables when visiting a file.
  466. This is useful when the column width in tables is forced with <N> cookies
  467. in table fields. Such tables will look correct only after the first re-align.
  468. This can also be configured on a per-file basis by adding one of
  469. the following lines anywhere in the buffer:
  470. #+STARTUP: align
  471. #+STARTUP: noalign"
  472. :group 'org-startup
  473. :type 'boolean)
  474. (defcustom org-startup-with-inline-images nil
  475. "Non-nil means show inline images when loading a new Org file.
  476. This can also be configured on a per-file basis by adding one of
  477. the following lines anywhere in the buffer:
  478. #+STARTUP: inlineimages
  479. #+STARTUP: noinlineimages"
  480. :group 'org-startup
  481. :version "24.1"
  482. :type 'boolean)
  483. (defcustom org-insert-mode-line-in-empty-file nil
  484. "Non-nil means insert the first line setting Org-mode in empty files.
  485. When the function `org-mode' is called interactively in an empty file, this
  486. normally means that the file name does not automatically trigger Org-mode.
  487. To ensure that the file will always be in Org-mode in the future, a
  488. line enforcing Org-mode will be inserted into the buffer, if this option
  489. has been set."
  490. :group 'org-startup
  491. :type 'boolean)
  492. (defcustom org-replace-disputed-keys nil
  493. "Non-nil means use alternative key bindings for some keys.
  494. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  495. These keys are also used by other packages like shift-selection-mode'
  496. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  497. If you want to use Org-mode together with one of these other modes,
  498. or more generally if you would like to move some Org-mode commands to
  499. other keys, set this variable and configure the keys with the variable
  500. `org-disputed-keys'.
  501. This option is only relevant at load-time of Org-mode, and must be set
  502. *before* org.el is loaded. Changing it requires a restart of Emacs to
  503. become effective."
  504. :group 'org-startup
  505. :type 'boolean)
  506. (defcustom org-use-extra-keys nil
  507. "Non-nil means use extra key sequence definitions for certain commands.
  508. This happens automatically if you run XEmacs or if `window-system'
  509. is nil. This variable lets you do the same manually. You must
  510. set it before loading org.
  511. Example: on Carbon Emacs 22 running graphically, with an external
  512. keyboard on a Powerbook, the default way of setting M-left might
  513. not work for either Alt or ESC. Setting this variable will make
  514. it work for ESC."
  515. :group 'org-startup
  516. :type 'boolean)
  517. (if (fboundp 'defvaralias)
  518. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  519. (defcustom org-disputed-keys
  520. '(([(shift up)] . [(meta p)])
  521. ([(shift down)] . [(meta n)])
  522. ([(shift left)] . [(meta -)])
  523. ([(shift right)] . [(meta +)])
  524. ([(control shift right)] . [(meta shift +)])
  525. ([(control shift left)] . [(meta shift -)]))
  526. "Keys for which Org-mode and other modes compete.
  527. This is an alist, cars are the default keys, second element specifies
  528. the alternative to use when `org-replace-disputed-keys' is t.
  529. Keys can be specified in any syntax supported by `define-key'.
  530. The value of this option takes effect only at Org-mode's startup,
  531. therefore you'll have to restart Emacs to apply it after changing."
  532. :group 'org-startup
  533. :type 'alist)
  534. (defun org-key (key)
  535. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  536. Or return the original if not disputed.
  537. Also apply the translations defined in `org-xemacs-key-equivalents'."
  538. (when org-replace-disputed-keys
  539. (let* ((nkey (key-description key))
  540. (x (org-find-if (lambda (x)
  541. (equal (key-description (car x)) nkey))
  542. org-disputed-keys)))
  543. (setq key (if x (cdr x) key))))
  544. (when (featurep 'xemacs)
  545. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  546. key)
  547. (defun org-find-if (predicate seq)
  548. (catch 'exit
  549. (while seq
  550. (if (funcall predicate (car seq))
  551. (throw 'exit (car seq))
  552. (pop seq)))))
  553. (defun org-defkey (keymap key def)
  554. "Define a key, possibly translated, as returned by `org-key'."
  555. (define-key keymap (org-key key) def))
  556. (defcustom org-ellipsis nil
  557. "The ellipsis to use in the Org-mode outline.
  558. When nil, just use the standard three dots. When a string, use that instead,
  559. When a face, use the standard 3 dots, but with the specified face.
  560. The change affects only Org-mode (which will then use its own display table).
  561. Changing this requires executing `M-x org-mode' in a buffer to become
  562. effective."
  563. :group 'org-startup
  564. :type '(choice (const :tag "Default" nil)
  565. (face :tag "Face" :value org-warning)
  566. (string :tag "String" :value "...#")))
  567. (defvar org-display-table nil
  568. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  569. (defgroup org-keywords nil
  570. "Keywords in Org-mode."
  571. :tag "Org Keywords"
  572. :group 'org)
  573. (defcustom org-deadline-string "DEADLINE:"
  574. "String to mark deadline entries.
  575. A deadline is this string, followed by a time stamp. Should be a word,
  576. terminated by a colon. You can insert a schedule keyword and
  577. a timestamp with \\[org-deadline].
  578. Changes become only effective after restarting Emacs."
  579. :group 'org-keywords
  580. :type 'string)
  581. (defcustom org-scheduled-string "SCHEDULED:"
  582. "String to mark scheduled TODO entries.
  583. A schedule is this string, followed by a time stamp. Should be a word,
  584. terminated by a colon. You can insert a schedule keyword and
  585. a timestamp with \\[org-schedule].
  586. Changes become only effective after restarting Emacs."
  587. :group 'org-keywords
  588. :type 'string)
  589. (defcustom org-closed-string "CLOSED:"
  590. "String used as the prefix for timestamps logging closing a TODO entry."
  591. :group 'org-keywords
  592. :type 'string)
  593. (defcustom org-clock-string "CLOCK:"
  594. "String used as prefix for timestamps clocking work hours on an item."
  595. :group 'org-keywords
  596. :type 'string)
  597. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  598. org-scheduled-string "\\|"
  599. org-deadline-string "\\|"
  600. org-closed-string "\\|"
  601. org-clock-string "\\)")
  602. "Matches a line with planning or clock info.")
  603. (defcustom org-comment-string "COMMENT"
  604. "Entries starting with this keyword will never be exported.
  605. An entry can be toggled between COMMENT and normal with
  606. \\[org-toggle-comment].
  607. Changes become only effective after restarting Emacs."
  608. :group 'org-keywords
  609. :type 'string)
  610. (defcustom org-quote-string "QUOTE"
  611. "Entries starting with this keyword will be exported in fixed-width font.
  612. Quoting applies only to the text in the entry following the headline, and does
  613. not extend beyond the next headline, even if that is lower level.
  614. An entry can be toggled between QUOTE and normal with
  615. \\[org-toggle-fixed-width-section]."
  616. :group 'org-keywords
  617. :type 'string)
  618. (defconst org-repeat-re
  619. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  620. "Regular expression for specifying repeated events.
  621. After a match, group 1 contains the repeat expression.")
  622. (defgroup org-structure nil
  623. "Options concerning the general structure of Org-mode files."
  624. :tag "Org Structure"
  625. :group 'org)
  626. (defgroup org-reveal-location nil
  627. "Options about how to make context of a location visible."
  628. :tag "Org Reveal Location"
  629. :group 'org-structure)
  630. (defconst org-context-choice
  631. '(choice
  632. (const :tag "Always" t)
  633. (const :tag "Never" nil)
  634. (repeat :greedy t :tag "Individual contexts"
  635. (cons
  636. (choice :tag "Context"
  637. (const agenda)
  638. (const org-goto)
  639. (const occur-tree)
  640. (const tags-tree)
  641. (const link-search)
  642. (const mark-goto)
  643. (const bookmark-jump)
  644. (const isearch)
  645. (const default))
  646. (boolean))))
  647. "Contexts for the reveal options.")
  648. (defcustom org-show-hierarchy-above '((default . t))
  649. "Non-nil means show full hierarchy when revealing a location.
  650. Org-mode often shows locations in an org-mode file which might have
  651. been invisible before. When this is set, the hierarchy of headings
  652. above the exposed location is shown.
  653. Turning this off for example for sparse trees makes them very compact.
  654. Instead of t, this can also be an alist specifying this option for different
  655. contexts. Valid contexts are
  656. agenda when exposing an entry from the agenda
  657. org-goto when using the command `org-goto' on key C-c C-j
  658. occur-tree when using the command `org-occur' on key C-c /
  659. tags-tree when constructing a sparse tree based on tags matches
  660. link-search when exposing search matches associated with a link
  661. mark-goto when exposing the jump goal of a mark
  662. bookmark-jump when exposing a bookmark location
  663. isearch when exiting from an incremental search
  664. default default for all contexts not set explicitly"
  665. :group 'org-reveal-location
  666. :type org-context-choice)
  667. (defcustom org-show-following-heading '((default . nil))
  668. "Non-nil means show following heading when revealing a location.
  669. Org-mode often shows locations in an org-mode file which might have
  670. been invisible before. When this is set, the heading following the
  671. match is shown.
  672. Turning this off for example for sparse trees makes them very compact,
  673. but makes it harder to edit the location of the match. In such a case,
  674. use the command \\[org-reveal] to show more context.
  675. Instead of t, this can also be an alist specifying this option for different
  676. contexts. See `org-show-hierarchy-above' for valid contexts."
  677. :group 'org-reveal-location
  678. :type org-context-choice)
  679. (defcustom org-show-siblings '((default . nil) (isearch t))
  680. "Non-nil means show all sibling heading when revealing a location.
  681. Org-mode often shows locations in an org-mode file which might have
  682. been invisible before. When this is set, the sibling of the current entry
  683. heading are all made visible. If `org-show-hierarchy-above' is t,
  684. the same happens on each level of the hierarchy above the current entry.
  685. By default this is on for the isearch context, off for all other contexts.
  686. Turning this off for example for sparse trees makes them very compact,
  687. but makes it harder to edit the location of the match. In such a case,
  688. use the command \\[org-reveal] to show more context.
  689. Instead of t, this can also be an alist specifying this option for different
  690. contexts. See `org-show-hierarchy-above' for valid contexts."
  691. :group 'org-reveal-location
  692. :type org-context-choice)
  693. (defcustom org-show-entry-below '((default . nil))
  694. "Non-nil means show the entry below a headline when revealing a location.
  695. Org-mode often shows locations in an org-mode file which might have
  696. been invisible before. When this is set, the text below the headline that is
  697. exposed is also shown.
  698. By default this is off for all contexts.
  699. Instead of t, this can also be an alist specifying this option for different
  700. contexts. See `org-show-hierarchy-above' for valid contexts."
  701. :group 'org-reveal-location
  702. :type org-context-choice)
  703. (defcustom org-indirect-buffer-display 'other-window
  704. "How should indirect tree buffers be displayed?
  705. This applies to indirect buffers created with the commands
  706. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  707. Valid values are:
  708. current-window Display in the current window
  709. other-window Just display in another window.
  710. dedicated-frame Create one new frame, and re-use it each time.
  711. new-frame Make a new frame each time. Note that in this case
  712. previously-made indirect buffers are kept, and you need to
  713. kill these buffers yourself."
  714. :group 'org-structure
  715. :group 'org-agenda-windows
  716. :type '(choice
  717. (const :tag "In current window" current-window)
  718. (const :tag "In current frame, other window" other-window)
  719. (const :tag "Each time a new frame" new-frame)
  720. (const :tag "One dedicated frame" dedicated-frame)))
  721. (defcustom org-use-speed-commands nil
  722. "Non-nil means activate single letter commands at beginning of a headline.
  723. This may also be a function to test for appropriate locations where speed
  724. commands should be active."
  725. :group 'org-structure
  726. :type '(choice
  727. (const :tag "Never" nil)
  728. (const :tag "At beginning of headline stars" t)
  729. (function)))
  730. (defcustom org-speed-commands-user nil
  731. "Alist of additional speed commands.
  732. This list will be checked before `org-speed-commands-default'
  733. when the variable `org-use-speed-commands' is non-nil
  734. and when the cursor is at the beginning of a headline.
  735. The car if each entry is a string with a single letter, which must
  736. be assigned to `self-insert-command' in the global map.
  737. The cdr is either a command to be called interactively, a function
  738. to be called, or a form to be evaluated.
  739. An entry that is just a list with a single string will be interpreted
  740. as a descriptive headline that will be added when listing the speed
  741. commands in the Help buffer using the `?' speed command."
  742. :group 'org-structure
  743. :type '(repeat :value ("k" . ignore)
  744. (choice :value ("k" . ignore)
  745. (list :tag "Descriptive Headline" (string :tag "Headline"))
  746. (cons :tag "Letter and Command"
  747. (string :tag "Command letter")
  748. (choice
  749. (function)
  750. (sexp))))))
  751. (defgroup org-cycle nil
  752. "Options concerning visibility cycling in Org-mode."
  753. :tag "Org Cycle"
  754. :group 'org-structure)
  755. (defcustom org-cycle-skip-children-state-if-no-children t
  756. "Non-nil means skip CHILDREN state in entries that don't have any."
  757. :group 'org-cycle
  758. :type 'boolean)
  759. (defcustom org-cycle-max-level nil
  760. "Maximum level which should still be subject to visibility cycling.
  761. Levels higher than this will, for cycling, be treated as text, not a headline.
  762. When `org-odd-levels-only' is set, a value of N in this variable actually
  763. means 2N-1 stars as the limiting headline.
  764. When nil, cycle all levels.
  765. Note that the limiting level of cycling is also influenced by
  766. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  767. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  768. than its value."
  769. :group 'org-cycle
  770. :type '(choice
  771. (const :tag "No limit" nil)
  772. (integer :tag "Maximum level")))
  773. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  774. "Names of drawers. Drawers are not opened by cycling on the headline above.
  775. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  776. this:
  777. :DRAWERNAME:
  778. .....
  779. :END:
  780. The drawer \"PROPERTIES\" is special for capturing properties through
  781. the property API.
  782. Drawers can be defined on the per-file basis with a line like:
  783. #+DRAWERS: HIDDEN STATE PROPERTIES"
  784. :group 'org-structure
  785. :group 'org-cycle
  786. :type '(repeat (string :tag "Drawer Name")))
  787. (defcustom org-hide-block-startup nil
  788. "Non-nil means entering Org-mode will fold all blocks.
  789. This can also be set in on a per-file basis with
  790. #+STARTUP: hideblocks
  791. #+STARTUP: showblocks"
  792. :group 'org-startup
  793. :group 'org-cycle
  794. :type 'boolean)
  795. (defcustom org-cycle-global-at-bob nil
  796. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  797. This makes it possible to do global cycling without having to use S-TAB or
  798. \\[universal-argument] TAB. For this special case to work, the first line
  799. of the buffer must not be a headline -- it may be empty or some other text.
  800. When used in this way, `org-cycle-hook' is disabled temporarily to make
  801. sure the cursor stays at the beginning of the buffer. When this option is
  802. nil, don't do anything special at the beginning of the buffer."
  803. :group 'org-cycle
  804. :type 'boolean)
  805. (defcustom org-cycle-level-after-item/entry-creation t
  806. "Non-nil means cycle entry level or item indentation in new empty entries.
  807. When the cursor is at the end of an empty headline, i.e with only stars
  808. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  809. and then all possible ancestor states, before returning to the original state.
  810. This makes data entry extremely fast: M-RET to create a new headline,
  811. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  812. When the cursor is at the end of an empty plain list item, one TAB will
  813. make it a subitem, two or more tabs will back up to make this an item
  814. higher up in the item hierarchy."
  815. :group 'org-cycle
  816. :type 'boolean)
  817. (defcustom org-cycle-emulate-tab t
  818. "Where should `org-cycle' emulate TAB.
  819. nil Never
  820. white Only in completely white lines
  821. whitestart Only at the beginning of lines, before the first non-white char
  822. t Everywhere except in headlines
  823. exc-hl-bol Everywhere except at the start of a headline
  824. If TAB is used in a place where it does not emulate TAB, the current subtree
  825. visibility is cycled."
  826. :group 'org-cycle
  827. :type '(choice (const :tag "Never" nil)
  828. (const :tag "Only in completely white lines" white)
  829. (const :tag "Before first char in a line" whitestart)
  830. (const :tag "Everywhere except in headlines" t)
  831. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  832. ))
  833. (defcustom org-cycle-separator-lines 2
  834. "Number of empty lines needed to keep an empty line between collapsed trees.
  835. If you leave an empty line between the end of a subtree and the following
  836. headline, this empty line is hidden when the subtree is folded.
  837. Org-mode will leave (exactly) one empty line visible if the number of
  838. empty lines is equal or larger to the number given in this variable.
  839. So the default 2 means at least 2 empty lines after the end of a subtree
  840. are needed to produce free space between a collapsed subtree and the
  841. following headline.
  842. If the number is negative, and the number of empty lines is at least -N,
  843. all empty lines are shown.
  844. Special case: when 0, never leave empty lines in collapsed view."
  845. :group 'org-cycle
  846. :type 'integer)
  847. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  848. (defcustom org-pre-cycle-hook nil
  849. "Hook that is run before visibility cycling is happening.
  850. The function(s) in this hook must accept a single argument which indicates
  851. the new state that will be set right after running this hook. The
  852. argument is a symbol. Before a global state change, it can have the values
  853. `overview', `content', or `all'. Before a local state change, it can have
  854. the values `folded', `children', or `subtree'."
  855. :group 'org-cycle
  856. :type 'hook)
  857. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  858. org-cycle-hide-drawers
  859. org-cycle-hide-inline-tasks
  860. org-cycle-show-empty-lines
  861. org-optimize-window-after-visibility-change)
  862. "Hook that is run after `org-cycle' has changed the buffer visibility.
  863. The function(s) in this hook must accept a single argument which indicates
  864. the new state that was set by the most recent `org-cycle' command. The
  865. argument is a symbol. After a global state change, it can have the values
  866. `overview', `contents', or `all'. After a local state change, it can have
  867. the values `folded', `children', or `subtree'."
  868. :group 'org-cycle
  869. :type 'hook)
  870. (defgroup org-edit-structure nil
  871. "Options concerning structure editing in Org-mode."
  872. :tag "Org Edit Structure"
  873. :group 'org-structure)
  874. (defcustom org-odd-levels-only nil
  875. "Non-nil means skip even levels and only use odd levels for the outline.
  876. This has the effect that two stars are being added/taken away in
  877. promotion/demotion commands. It also influences how levels are
  878. handled by the exporters.
  879. Changing it requires restart of `font-lock-mode' to become effective
  880. for fontification also in regions already fontified.
  881. You may also set this on a per-file basis by adding one of the following
  882. lines to the buffer:
  883. #+STARTUP: odd
  884. #+STARTUP: oddeven"
  885. :group 'org-edit-structure
  886. :group 'org-appearance
  887. :type 'boolean)
  888. (defcustom org-adapt-indentation t
  889. "Non-nil means adapt indentation to outline node level.
  890. When this variable is set, Org assumes that you write outlines by
  891. indenting text in each node to align with the headline (after the stars).
  892. The following issues are influenced by this variable:
  893. - When this is set and the *entire* text in an entry is indented, the
  894. indentation is increased by one space in a demotion command, and
  895. decreased by one in a promotion command. If any line in the entry
  896. body starts with text at column 0, indentation is not changed at all.
  897. - Property drawers and planning information is inserted indented when
  898. this variable s set. When nil, they will not be indented.
  899. - TAB indents a line relative to context. The lines below a headline
  900. will be indented when this variable is set.
  901. Note that this is all about true indentation, by adding and removing
  902. space characters. See also `org-indent.el' which does level-dependent
  903. indentation in a virtual way, i.e. at display time in Emacs."
  904. :group 'org-edit-structure
  905. :type 'boolean)
  906. (defcustom org-special-ctrl-a/e nil
  907. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  908. When t, `C-a' will bring back the cursor to the beginning of the
  909. headline text, i.e. after the stars and after a possible TODO
  910. keyword. In an item, this will be the position after bullet and
  911. check-box, if any. When the cursor is already at that position,
  912. another `C-a' will bring it to the beginning of the line.
  913. `C-e' will jump to the end of the headline, ignoring the presence
  914. of tags in the headline. A second `C-e' will then jump to the
  915. true end of the line, after any tags. This also means that, when
  916. this variable is non-nil, `C-e' also will never jump beyond the
  917. end of the heading of a folded section, i.e. not after the
  918. ellipses.
  919. When set to the symbol `reversed', the first `C-a' or `C-e' works
  920. normally, going to the true line boundary first. Only a directly
  921. following, identical keypress will bring the cursor to the
  922. special positions.
  923. This may also be a cons cell where the behavior for `C-a' and
  924. `C-e' is set separately."
  925. :group 'org-edit-structure
  926. :type '(choice
  927. (const :tag "off" nil)
  928. (const :tag "on: after stars/bullet and before tags first" t)
  929. (const :tag "reversed: true line boundary first" reversed)
  930. (cons :tag "Set C-a and C-e separately"
  931. (choice :tag "Special C-a"
  932. (const :tag "off" nil)
  933. (const :tag "on: after stars/bullet first" t)
  934. (const :tag "reversed: before stars/bullet first" reversed))
  935. (choice :tag "Special C-e"
  936. (const :tag "off" nil)
  937. (const :tag "on: before tags first" t)
  938. (const :tag "reversed: after tags first" reversed)))))
  939. (if (fboundp 'defvaralias)
  940. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  941. (defcustom org-special-ctrl-k nil
  942. "Non-nil means `C-k' will behave specially in headlines.
  943. When nil, `C-k' will call the default `kill-line' command.
  944. When t, the following will happen while the cursor is in the headline:
  945. - When the cursor is at the beginning of a headline, kill the entire
  946. line and possible the folded subtree below the line.
  947. - When in the middle of the headline text, kill the headline up to the tags.
  948. - When after the headline text, kill the tags."
  949. :group 'org-edit-structure
  950. :type 'boolean)
  951. (defcustom org-ctrl-k-protect-subtree nil
  952. "Non-nil means, do not delete a hidden subtree with C-k.
  953. When set to the symbol `error', simply throw an error when C-k is
  954. used to kill (part-of) a headline that has hidden text behind it.
  955. Any other non-nil value will result in a query to the user, if it is
  956. OK to kill that hidden subtree. When nil, kill without remorse."
  957. :group 'org-edit-structure
  958. :version "24.1"
  959. :type '(choice
  960. (const :tag "Do not protect hidden subtrees" nil)
  961. (const :tag "Protect hidden subtrees with a security query" t)
  962. (const :tag "Never kill a hidden subtree with C-k" error)))
  963. (defcustom org-catch-invisible-edits nil
  964. "Check if in invisible region before inserting or deleting a character.
  965. Valid values are:
  966. nil Do not check, so just do invisible edits.
  967. error Throw an error and do nothing.
  968. show Make point visible, and do the requested edit.
  969. show-and-error Make point visible, then throw an error and abort the edit.
  970. smart Make point visible, and do insertion/deletion if it is
  971. adjacent to visible text and the change feels predictable.
  972. Never delete a previously invisible character or add in the
  973. middle or right after an invisible region. Basically, this
  974. allows insertion and backward-delete right before ellipses.
  975. FIXME: maybe in this case we should not even show?"
  976. :group 'org-edit-structure
  977. :version "24.1"
  978. :type '(choice
  979. (const :tag "Do not check" nil)
  980. (const :tag "Throw error when trying to edit" error)
  981. (const :tag "Unhide, but do not do the edit" show-and-error)
  982. (const :tag "Show invisible part and do the edit" show)
  983. (const :tag "Be smart and do the right thing" smart)))
  984. (defcustom org-yank-folded-subtrees t
  985. "Non-nil means when yanking subtrees, fold them.
  986. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  987. it starts with a heading and all other headings in it are either children
  988. or siblings, then fold all the subtrees. However, do this only if no
  989. text after the yank would be swallowed into a folded tree by this action."
  990. :group 'org-edit-structure
  991. :type 'boolean)
  992. (defcustom org-yank-adjusted-subtrees nil
  993. "Non-nil means when yanking subtrees, adjust the level.
  994. With this setting, `org-paste-subtree' is used to insert the subtree, see
  995. this function for details."
  996. :group 'org-edit-structure
  997. :type 'boolean)
  998. (defcustom org-M-RET-may-split-line '((default . t))
  999. "Non-nil means M-RET will split the line at the cursor position.
  1000. When nil, it will go to the end of the line before making a
  1001. new line.
  1002. You may also set this option in a different way for different
  1003. contexts. Valid contexts are:
  1004. headline when creating a new headline
  1005. item when creating a new item
  1006. table in a table field
  1007. default the value to be used for all contexts not explicitly
  1008. customized"
  1009. :group 'org-structure
  1010. :group 'org-table
  1011. :type '(choice
  1012. (const :tag "Always" t)
  1013. (const :tag "Never" nil)
  1014. (repeat :greedy t :tag "Individual contexts"
  1015. (cons
  1016. (choice :tag "Context"
  1017. (const headline)
  1018. (const item)
  1019. (const table)
  1020. (const default))
  1021. (boolean)))))
  1022. (defcustom org-insert-heading-respect-content nil
  1023. "Non-nil means insert new headings after the current subtree.
  1024. When nil, the new heading is created directly after the current line.
  1025. The commands \\[org-insert-heading-respect-content] and
  1026. \\[org-insert-todo-heading-respect-content] turn this variable on
  1027. for the duration of the command."
  1028. :group 'org-structure
  1029. :type 'boolean)
  1030. (defcustom org-blank-before-new-entry '((heading . auto)
  1031. (plain-list-item . auto))
  1032. "Should `org-insert-heading' leave a blank line before new heading/item?
  1033. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1034. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1035. which case Org will look at the surrounding headings/items and try to
  1036. make an intelligent decision whether to insert a blank line or not.
  1037. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1038. set, the setting here is ignored and no empty line is inserted, to avoid
  1039. breaking the list structure."
  1040. :group 'org-edit-structure
  1041. :type '(list
  1042. (cons (const heading)
  1043. (choice (const :tag "Never" nil)
  1044. (const :tag "Always" t)
  1045. (const :tag "Auto" auto)))
  1046. (cons (const plain-list-item)
  1047. (choice (const :tag "Never" nil)
  1048. (const :tag "Always" t)
  1049. (const :tag "Auto" auto)))))
  1050. (defcustom org-insert-heading-hook nil
  1051. "Hook being run after inserting a new heading."
  1052. :group 'org-edit-structure
  1053. :type 'hook)
  1054. (defcustom org-enable-fixed-width-editor t
  1055. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1056. This currently only means they are never auto-wrapped.
  1057. When nil, such lines will be treated like ordinary lines.
  1058. See also the QUOTE keyword."
  1059. :group 'org-edit-structure
  1060. :type 'boolean)
  1061. (defcustom org-goto-auto-isearch t
  1062. "Non-nil means typing characters in `org-goto' starts incremental search."
  1063. :group 'org-edit-structure
  1064. :type 'boolean)
  1065. (defgroup org-sparse-trees nil
  1066. "Options concerning sparse trees in Org-mode."
  1067. :tag "Org Sparse Trees"
  1068. :group 'org-structure)
  1069. (defcustom org-highlight-sparse-tree-matches t
  1070. "Non-nil means highlight all matches that define a sparse tree.
  1071. The highlights will automatically disappear the next time the buffer is
  1072. changed by an edit command."
  1073. :group 'org-sparse-trees
  1074. :type 'boolean)
  1075. (defcustom org-remove-highlights-with-change t
  1076. "Non-nil means any change to the buffer will remove temporary highlights.
  1077. Such highlights are created by `org-occur' and `org-clock-display'.
  1078. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1079. The highlights created by `org-preview-latex-fragment' always need
  1080. `C-c C-c' to be removed."
  1081. :group 'org-sparse-trees
  1082. :group 'org-time
  1083. :type 'boolean)
  1084. (defcustom org-occur-hook '(org-first-headline-recenter)
  1085. "Hook that is run after `org-occur' has constructed a sparse tree.
  1086. This can be used to recenter the window to show as much of the structure
  1087. as possible."
  1088. :group 'org-sparse-trees
  1089. :type 'hook)
  1090. (defgroup org-imenu-and-speedbar nil
  1091. "Options concerning imenu and speedbar in Org-mode."
  1092. :tag "Org Imenu and Speedbar"
  1093. :group 'org-structure)
  1094. (defcustom org-imenu-depth 2
  1095. "The maximum level for Imenu access to Org-mode headlines.
  1096. This also applied for speedbar access."
  1097. :group 'org-imenu-and-speedbar
  1098. :type 'integer)
  1099. (defgroup org-table nil
  1100. "Options concerning tables in Org-mode."
  1101. :tag "Org Table"
  1102. :group 'org)
  1103. (defcustom org-enable-table-editor 'optimized
  1104. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1105. When nil, such lines will be treated like ordinary lines.
  1106. When equal to the symbol `optimized', the table editor will be optimized to
  1107. do the following:
  1108. - Automatic overwrite mode in front of whitespace in table fields.
  1109. This makes the structure of the table stay in tact as long as the edited
  1110. field does not exceed the column width.
  1111. - Minimize the number of realigns. Normally, the table is aligned each time
  1112. TAB or RET are pressed to move to another field. With optimization this
  1113. happens only if changes to a field might have changed the column width.
  1114. Optimization requires replacing the functions `self-insert-command',
  1115. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1116. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1117. very good at guessing when a re-align will be necessary, but you can always
  1118. force one with \\[org-ctrl-c-ctrl-c].
  1119. If you would like to use the optimized version in Org-mode, but the
  1120. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1121. This variable can be used to turn on and off the table editor during a session,
  1122. but in order to toggle optimization, a restart is required.
  1123. See also the variable `org-table-auto-blank-field'."
  1124. :group 'org-table
  1125. :type '(choice
  1126. (const :tag "off" nil)
  1127. (const :tag "on" t)
  1128. (const :tag "on, optimized" optimized)))
  1129. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1130. (version<= emacs-version "24.1"))
  1131. "Non-nil means cluster self-insert commands for undo when possible.
  1132. If this is set, then, like in the Emacs command loop, 20 consecutive
  1133. characters will be undone together.
  1134. This is configurable, because there is some impact on typing performance."
  1135. :group 'org-table
  1136. :type 'boolean)
  1137. (defcustom org-table-tab-recognizes-table.el t
  1138. "Non-nil means TAB will automatically notice a table.el table.
  1139. When it sees such a table, it moves point into it and - if necessary -
  1140. calls `table-recognize-table'."
  1141. :group 'org-table-editing
  1142. :type 'boolean)
  1143. (defgroup org-link nil
  1144. "Options concerning links in Org-mode."
  1145. :tag "Org Link"
  1146. :group 'org)
  1147. (defvar org-link-abbrev-alist-local nil
  1148. "Buffer-local version of `org-link-abbrev-alist', which see.
  1149. The value of this is taken from the #+LINK lines.")
  1150. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1151. (defcustom org-link-abbrev-alist nil
  1152. "Alist of link abbreviations.
  1153. The car of each element is a string, to be replaced at the start of a link.
  1154. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1155. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1156. [[linkkey:tag][description]]
  1157. The 'linkkey' must be a word word, starting with a letter, followed
  1158. by letters, numbers, '-' or '_'.
  1159. If REPLACE is a string, the tag will simply be appended to create the link.
  1160. If the string contains \"%s\", the tag will be inserted there. If the string
  1161. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1162. at that point (see the function `url-hexify-string'). If the string contains
  1163. the specifier \"%(my-function)\", then the custom function `my-function' will
  1164. be invoked: this function takes the tag as its only argument and must return
  1165. a string.
  1166. REPLACE may also be a function that will be called with the tag as the
  1167. only argument to create the link, which should be returned as a string.
  1168. See the manual for examples."
  1169. :group 'org-link
  1170. :type '(repeat
  1171. (cons
  1172. (string :tag "Protocol")
  1173. (choice
  1174. (string :tag "Format")
  1175. (function)))))
  1176. (defcustom org-descriptive-links t
  1177. "Non-nil means Org will display descriptive links.
  1178. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1179. \"Org Website\", hiding the link itself and just displaying its
  1180. description. When set to `nil', Org will display the full links
  1181. literally.
  1182. You can interactively set the value of this variable by calling
  1183. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1184. :group 'org-link
  1185. :type 'boolean)
  1186. (defcustom org-link-file-path-type 'adaptive
  1187. "How the path name in file links should be stored.
  1188. Valid values are:
  1189. relative Relative to the current directory, i.e. the directory of the file
  1190. into which the link is being inserted.
  1191. absolute Absolute path, if possible with ~ for home directory.
  1192. noabbrev Absolute path, no abbreviation of home directory.
  1193. adaptive Use relative path for files in the current directory and sub-
  1194. directories of it. For other files, use an absolute path."
  1195. :group 'org-link
  1196. :type '(choice
  1197. (const relative)
  1198. (const absolute)
  1199. (const noabbrev)
  1200. (const adaptive)))
  1201. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1202. "Types of links that should be activated in Org-mode files.
  1203. This is a list of symbols, each leading to the activation of a certain link
  1204. type. In principle, it does not hurt to turn on most link types - there may
  1205. be a small gain when turning off unused link types. The types are:
  1206. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1207. angle Links in angular brackets that may contain whitespace like
  1208. <bbdb:Carsten Dominik>.
  1209. plain Plain links in normal text, no whitespace, like http://google.com.
  1210. radio Text that is matched by a radio target, see manual for details.
  1211. tag Tag settings in a headline (link to tag search).
  1212. date Time stamps (link to calendar).
  1213. footnote Footnote labels.
  1214. Changing this variable requires a restart of Emacs to become effective."
  1215. :group 'org-link
  1216. :type '(set :greedy t
  1217. (const :tag "Double bracket links" bracket)
  1218. (const :tag "Angular bracket links" angle)
  1219. (const :tag "Plain text links" plain)
  1220. (const :tag "Radio target matches" radio)
  1221. (const :tag "Tags" tag)
  1222. (const :tag "Timestamps" date)
  1223. (const :tag "Footnotes" footnote)))
  1224. (defcustom org-make-link-description-function nil
  1225. "Function to use to generate link descriptions from links.
  1226. If nil the link location will be used. This function must take
  1227. two parameters; the first is the link and the second the
  1228. description `org-insert-link' has generated, and should return the
  1229. description to use."
  1230. :group 'org-link
  1231. :type 'function)
  1232. (defgroup org-link-store nil
  1233. "Options concerning storing links in Org-mode."
  1234. :tag "Org Store Link"
  1235. :group 'org-link)
  1236. (defcustom org-url-hexify-p t
  1237. "When non-nil, hexify URL when creating a link."
  1238. :type 'boolean
  1239. ;; :version "24.3"
  1240. :group 'org-link-store)
  1241. (defcustom org-email-link-description-format "Email %c: %.30s"
  1242. "Format of the description part of a link to an email or usenet message.
  1243. The following %-escapes will be replaced by corresponding information:
  1244. %F full \"From\" field
  1245. %f name, taken from \"From\" field, address if no name
  1246. %T full \"To\" field
  1247. %t first name in \"To\" field, address if no name
  1248. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1249. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1250. %s subject
  1251. %d date
  1252. %m message-id.
  1253. You may use normal field width specification between the % and the letter.
  1254. This is for example useful to limit the length of the subject.
  1255. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1256. :group 'org-link-store
  1257. :type 'string)
  1258. (defcustom org-from-is-user-regexp
  1259. (let (r1 r2)
  1260. (when (and user-mail-address (not (string= user-mail-address "")))
  1261. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1262. (when (and user-full-name (not (string= user-full-name "")))
  1263. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1264. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1265. "Regexp matched against the \"From:\" header of an email or usenet message.
  1266. It should match if the message is from the user him/herself."
  1267. :group 'org-link-store
  1268. :type 'regexp)
  1269. (defcustom org-context-in-file-links t
  1270. "Non-nil means file links from `org-store-link' contain context.
  1271. A search string will be added to the file name with :: as separator and
  1272. used to find the context when the link is activated by the command
  1273. `org-open-at-point'. When this option is t, the entire active region
  1274. will be placed in the search string of the file link. If set to a
  1275. positive integer, only the first n lines of context will be stored.
  1276. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1277. negates this setting for the duration of the command."
  1278. :group 'org-link-store
  1279. :type '(choice boolean integer))
  1280. (defcustom org-keep-stored-link-after-insertion nil
  1281. "Non-nil means keep link in list for entire session.
  1282. The command `org-store-link' adds a link pointing to the current
  1283. location to an internal list. These links accumulate during a session.
  1284. The command `org-insert-link' can be used to insert links into any
  1285. Org-mode file (offering completion for all stored links). When this
  1286. option is nil, every link which has been inserted once using \\[org-insert-link]
  1287. will be removed from the list, to make completing the unused links
  1288. more efficient."
  1289. :group 'org-link-store
  1290. :type 'boolean)
  1291. (defgroup org-link-follow nil
  1292. "Options concerning following links in Org-mode."
  1293. :tag "Org Follow Link"
  1294. :group 'org-link)
  1295. (defcustom org-link-translation-function nil
  1296. "Function to translate links with different syntax to Org syntax.
  1297. This can be used to translate links created for example by the Planner
  1298. or emacs-wiki packages to Org syntax.
  1299. The function must accept two parameters, a TYPE containing the link
  1300. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1301. which is everything after the link protocol. It should return a cons
  1302. with possibly modified values of type and path.
  1303. Org contains a function for this, so if you set this variable to
  1304. `org-translate-link-from-planner', you should be able follow many
  1305. links created by planner."
  1306. :group 'org-link-follow
  1307. :type 'function)
  1308. (defcustom org-follow-link-hook nil
  1309. "Hook that is run after a link has been followed."
  1310. :group 'org-link-follow
  1311. :type 'hook)
  1312. (defcustom org-tab-follows-link nil
  1313. "Non-nil means on links TAB will follow the link.
  1314. Needs to be set before org.el is loaded.
  1315. This really should not be used, it does not make sense, and the
  1316. implementation is bad."
  1317. :group 'org-link-follow
  1318. :type 'boolean)
  1319. (defcustom org-return-follows-link nil
  1320. "Non-nil means on links RET will follow the link."
  1321. :group 'org-link-follow
  1322. :type 'boolean)
  1323. (defcustom org-mouse-1-follows-link
  1324. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1325. "Non-nil means mouse-1 on a link will follow the link.
  1326. A longer mouse click will still set point. Does not work on XEmacs.
  1327. Needs to be set before org.el is loaded."
  1328. :group 'org-link-follow
  1329. :type 'boolean)
  1330. (defcustom org-mark-ring-length 4
  1331. "Number of different positions to be recorded in the ring.
  1332. Changing this requires a restart of Emacs to work correctly."
  1333. :group 'org-link-follow
  1334. :type 'integer)
  1335. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1336. "Non-nil means internal links in Org files must exactly match a headline.
  1337. When nil, the link search tries to match a phrase with all words
  1338. in the search text."
  1339. :group 'org-link-follow
  1340. :version "24.1"
  1341. :type '(choice
  1342. (const :tag "Use fuzzy text search" nil)
  1343. (const :tag "Match only exact headline" t)
  1344. (const :tag "Match exact headline or query to create it"
  1345. query-to-create)))
  1346. (defcustom org-link-frame-setup
  1347. '((vm . vm-visit-folder-other-frame)
  1348. (vm-imap . vm-visit-imap-folder-other-frame)
  1349. (gnus . org-gnus-no-new-news)
  1350. (file . find-file-other-window)
  1351. (wl . wl-other-frame))
  1352. "Setup the frame configuration for following links.
  1353. When following a link with Emacs, it may often be useful to display
  1354. this link in another window or frame. This variable can be used to
  1355. set this up for the different types of links.
  1356. For VM, use any of
  1357. `vm-visit-folder'
  1358. `vm-visit-folder-other-window'
  1359. `vm-visit-folder-other-frame'
  1360. For Gnus, use any of
  1361. `gnus'
  1362. `gnus-other-frame'
  1363. `org-gnus-no-new-news'
  1364. For FILE, use any of
  1365. `find-file'
  1366. `find-file-other-window'
  1367. `find-file-other-frame'
  1368. For Wanderlust use any of
  1369. `wl'
  1370. `wl-other-frame'
  1371. For the calendar, use the variable `calendar-setup'.
  1372. For BBDB, it is currently only possible to display the matches in
  1373. another window."
  1374. :group 'org-link-follow
  1375. :type '(list
  1376. (cons (const vm)
  1377. (choice
  1378. (const vm-visit-folder)
  1379. (const vm-visit-folder-other-window)
  1380. (const vm-visit-folder-other-frame)))
  1381. (cons (const gnus)
  1382. (choice
  1383. (const gnus)
  1384. (const gnus-other-frame)
  1385. (const org-gnus-no-new-news)))
  1386. (cons (const file)
  1387. (choice
  1388. (const find-file)
  1389. (const find-file-other-window)
  1390. (const find-file-other-frame)))
  1391. (cons (const wl)
  1392. (choice
  1393. (const wl)
  1394. (const wl-other-frame)))))
  1395. (defcustom org-display-internal-link-with-indirect-buffer nil
  1396. "Non-nil means use indirect buffer to display infile links.
  1397. Activating internal links (from one location in a file to another location
  1398. in the same file) normally just jumps to the location. When the link is
  1399. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1400. is displayed in
  1401. another window. When this option is set, the other window actually displays
  1402. an indirect buffer clone of the current buffer, to avoid any visibility
  1403. changes to the current buffer."
  1404. :group 'org-link-follow
  1405. :type 'boolean)
  1406. (defcustom org-open-non-existing-files nil
  1407. "Non-nil means `org-open-file' will open non-existing files.
  1408. When nil, an error will be generated.
  1409. This variable applies only to external applications because they
  1410. might choke on non-existing files. If the link is to a file that
  1411. will be opened in Emacs, the variable is ignored."
  1412. :group 'org-link-follow
  1413. :type 'boolean)
  1414. (defcustom org-open-directory-means-index-dot-org nil
  1415. "Non-nil means a link to a directory really means to index.org.
  1416. When nil, following a directory link will run dired or open a finder/explorer
  1417. window on that directory."
  1418. :group 'org-link-follow
  1419. :type 'boolean)
  1420. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1421. "Function and arguments to call for following mailto links.
  1422. This is a list with the first element being a Lisp function, and the
  1423. remaining elements being arguments to the function. In string arguments,
  1424. %a will be replaced by the address, and %s will be replaced by the subject
  1425. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1426. :group 'org-link-follow
  1427. :type '(choice
  1428. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1429. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1430. (const :tag "message-mail" (message-mail "%a" "%s"))
  1431. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1432. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1433. "Non-nil means ask for confirmation before executing shell links.
  1434. Shell links can be dangerous: just think about a link
  1435. [[shell:rm -rf ~/*][Google Search]]
  1436. This link would show up in your Org-mode document as \"Google Search\",
  1437. but really it would remove your entire home directory.
  1438. Therefore we advise against setting this variable to nil.
  1439. Just change it to `y-or-n-p' if you want to confirm with a
  1440. single keystroke rather than having to type \"yes\"."
  1441. :group 'org-link-follow
  1442. :type '(choice
  1443. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1444. (const :tag "with y-or-n (faster)" y-or-n-p)
  1445. (const :tag "no confirmation (dangerous)" nil)))
  1446. (put 'org-confirm-shell-link-function
  1447. 'safe-local-variable
  1448. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1449. (defcustom org-confirm-shell-link-not-regexp ""
  1450. "A regexp to skip confirmation for shell links."
  1451. :group 'org-link-follow
  1452. :version "24.1"
  1453. :type 'regexp)
  1454. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1455. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1456. Elisp links can be dangerous: just think about a link
  1457. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1458. This link would show up in your Org-mode document as \"Google Search\",
  1459. but really it would remove your entire home directory.
  1460. Therefore we advise against setting this variable to nil.
  1461. Just change it to `y-or-n-p' if you want to confirm with a
  1462. single keystroke rather than having to type \"yes\"."
  1463. :group 'org-link-follow
  1464. :type '(choice
  1465. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1466. (const :tag "with y-or-n (faster)" y-or-n-p)
  1467. (const :tag "no confirmation (dangerous)" nil)))
  1468. (put 'org-confirm-shell-link-function
  1469. 'safe-local-variable
  1470. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1471. (defcustom org-confirm-elisp-link-not-regexp ""
  1472. "A regexp to skip confirmation for Elisp links."
  1473. :group 'org-link-follow
  1474. :version "24.1"
  1475. :type 'regexp)
  1476. (defconst org-file-apps-defaults-gnu
  1477. '((remote . emacs)
  1478. (system . mailcap)
  1479. (t . mailcap))
  1480. "Default file applications on a UNIX or GNU/Linux system.
  1481. See `org-file-apps'.")
  1482. (defconst org-file-apps-defaults-macosx
  1483. '((remote . emacs)
  1484. (t . "open %s")
  1485. (system . "open %s")
  1486. ("ps.gz" . "gv %s")
  1487. ("eps.gz" . "gv %s")
  1488. ("dvi" . "xdvi %s")
  1489. ("fig" . "xfig %s"))
  1490. "Default file applications on a MacOS X system.
  1491. The system \"open\" is known as a default, but we use X11 applications
  1492. for some files for which the OS does not have a good default.
  1493. See `org-file-apps'.")
  1494. (defconst org-file-apps-defaults-windowsnt
  1495. (list
  1496. '(remote . emacs)
  1497. (cons t
  1498. (list (if (featurep 'xemacs)
  1499. 'mswindows-shell-execute
  1500. 'w32-shell-execute)
  1501. "open" 'file))
  1502. (cons 'system
  1503. (list (if (featurep 'xemacs)
  1504. 'mswindows-shell-execute
  1505. 'w32-shell-execute)
  1506. "open" 'file)))
  1507. "Default file applications on a Windows NT system.
  1508. The system \"open\" is used for most files.
  1509. See `org-file-apps'.")
  1510. (defcustom org-file-apps
  1511. '(
  1512. (auto-mode . emacs)
  1513. ("\\.mm\\'" . default)
  1514. ("\\.x?html?\\'" . default)
  1515. ("\\.pdf\\'" . default)
  1516. )
  1517. "External applications for opening `file:path' items in a document.
  1518. Org-mode uses system defaults for different file types, but
  1519. you can use this variable to set the application for a given file
  1520. extension. The entries in this list are cons cells where the car identifies
  1521. files and the cdr the corresponding command. Possible values for the
  1522. file identifier are
  1523. \"string\" A string as a file identifier can be interpreted in different
  1524. ways, depending on its contents:
  1525. - Alphanumeric characters only:
  1526. Match links with this file extension.
  1527. Example: (\"pdf\" . \"evince %s\")
  1528. to open PDFs with evince.
  1529. - Regular expression: Match links where the
  1530. filename matches the regexp. If you want to
  1531. use groups here, use shy groups.
  1532. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1533. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1534. to open *.html and *.xhtml with firefox.
  1535. - Regular expression which contains (non-shy) groups:
  1536. Match links where the whole link, including \"::\", and
  1537. anything after that, matches the regexp.
  1538. In a custom command string, %1, %2, etc. are replaced with
  1539. the parts of the link that were matched by the groups.
  1540. For backwards compatibility, if a command string is given
  1541. that does not use any of the group matches, this case is
  1542. handled identically to the second one (i.e. match against
  1543. file name only).
  1544. In a custom lisp form, you can access the group matches with
  1545. (match-string n link).
  1546. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1547. to open [[file:document.pdf::5]] with evince at page 5.
  1548. `directory' Matches a directory
  1549. `remote' Matches a remote file, accessible through tramp or efs.
  1550. Remote files most likely should be visited through Emacs
  1551. because external applications cannot handle such paths.
  1552. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1553. so all files Emacs knows how to handle. Using this with
  1554. command `emacs' will open most files in Emacs. Beware that this
  1555. will also open html files inside Emacs, unless you add
  1556. (\"html\" . default) to the list as well.
  1557. t Default for files not matched by any of the other options.
  1558. `system' The system command to open files, like `open' on Windows
  1559. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1560. that will be selected if you call `C-c C-o' with a double
  1561. \\[universal-argument] \\[universal-argument] prefix.
  1562. Possible values for the command are:
  1563. `emacs' The file will be visited by the current Emacs process.
  1564. `default' Use the default application for this file type, which is the
  1565. association for t in the list, most likely in the system-specific
  1566. part.
  1567. This can be used to overrule an unwanted setting in the
  1568. system-specific variable.
  1569. `system' Use the system command for opening files, like \"open\".
  1570. This command is specified by the entry whose car is `system'.
  1571. Most likely, the system-specific version of this variable
  1572. does define this command, but you can overrule/replace it
  1573. here.
  1574. string A command to be executed by a shell; %s will be replaced
  1575. by the path to the file.
  1576. sexp A Lisp form which will be evaluated. The file path will
  1577. be available in the Lisp variable `file'.
  1578. For more examples, see the system specific constants
  1579. `org-file-apps-defaults-macosx'
  1580. `org-file-apps-defaults-windowsnt'
  1581. `org-file-apps-defaults-gnu'."
  1582. :group 'org-link-follow
  1583. :type '(repeat
  1584. (cons (choice :value ""
  1585. (string :tag "Extension")
  1586. (const :tag "System command to open files" system)
  1587. (const :tag "Default for unrecognized files" t)
  1588. (const :tag "Remote file" remote)
  1589. (const :tag "Links to a directory" directory)
  1590. (const :tag "Any files that have Emacs modes"
  1591. auto-mode))
  1592. (choice :value ""
  1593. (const :tag "Visit with Emacs" emacs)
  1594. (const :tag "Use default" default)
  1595. (const :tag "Use the system command" system)
  1596. (string :tag "Command")
  1597. (sexp :tag "Lisp form")))))
  1598. (defcustom org-doi-server-url "http://dx.doi.org/"
  1599. "The URL of the DOI server."
  1600. :type 'string
  1601. ;; :version "24.3"
  1602. :group 'org-link-follow)
  1603. (defgroup org-refile nil
  1604. "Options concerning refiling entries in Org-mode."
  1605. :tag "Org Refile"
  1606. :group 'org)
  1607. (defcustom org-directory "~/org"
  1608. "Directory with org files.
  1609. This is just a default location to look for Org files. There is no need
  1610. at all to put your files into this directory. It is only used in the
  1611. following situations:
  1612. 1. When a capture template specifies a target file that is not an
  1613. absolute path. The path will then be interpreted relative to
  1614. `org-directory'
  1615. 2. When a capture note is filed away in an interactive way (when exiting the
  1616. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1617. with `org-directory' as the default path."
  1618. :group 'org-refile
  1619. :group 'org-remember
  1620. :group 'org-capture
  1621. :type 'directory)
  1622. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1623. "Default target for storing notes.
  1624. Used as a fall back file for org-remember.el and org-capture.el, for
  1625. templates that do not specify a target file."
  1626. :group 'org-refile
  1627. :group 'org-remember
  1628. :group 'org-capture
  1629. :type '(choice
  1630. (const :tag "Default from remember-data-file" nil)
  1631. file))
  1632. (defcustom org-goto-interface 'outline
  1633. "The default interface to be used for `org-goto'.
  1634. Allowed values are:
  1635. outline The interface shows an outline of the relevant file
  1636. and the correct heading is found by moving through
  1637. the outline or by searching with incremental search.
  1638. outline-path-completion Headlines in the current buffer are offered via
  1639. completion. This is the interface also used by
  1640. the refile command."
  1641. :group 'org-refile
  1642. :type '(choice
  1643. (const :tag "Outline" outline)
  1644. (const :tag "Outline-path-completion" outline-path-completion)))
  1645. (defcustom org-goto-max-level 5
  1646. "Maximum target level when running `org-goto' with refile interface."
  1647. :group 'org-refile
  1648. :type 'integer)
  1649. (defcustom org-reverse-note-order nil
  1650. "Non-nil means store new notes at the beginning of a file or entry.
  1651. When nil, new notes will be filed to the end of a file or entry.
  1652. This can also be a list with cons cells of regular expressions that
  1653. are matched against file names, and values."
  1654. :group 'org-remember
  1655. :group 'org-capture
  1656. :group 'org-refile
  1657. :type '(choice
  1658. (const :tag "Reverse always" t)
  1659. (const :tag "Reverse never" nil)
  1660. (repeat :tag "By file name regexp"
  1661. (cons regexp boolean))))
  1662. (defcustom org-log-refile nil
  1663. "Information to record when a task is refiled.
  1664. Possible values are:
  1665. nil Don't add anything
  1666. time Add a time stamp to the task
  1667. note Prompt for a note and add it with template `org-log-note-headings'
  1668. This option can also be set with on a per-file-basis with
  1669. #+STARTUP: nologrefile
  1670. #+STARTUP: logrefile
  1671. #+STARTUP: lognoterefile
  1672. You can have local logging settings for a subtree by setting the LOGGING
  1673. property to one or more of these keywords.
  1674. When bulk-refiling from the agenda, the value `note' is forbidden and
  1675. will temporarily be changed to `time'."
  1676. :group 'org-refile
  1677. :group 'org-progress
  1678. :version "24.1"
  1679. :type '(choice
  1680. (const :tag "No logging" nil)
  1681. (const :tag "Record timestamp" time)
  1682. (const :tag "Record timestamp with note." note)))
  1683. (defcustom org-refile-targets nil
  1684. "Targets for refiling entries with \\[org-refile].
  1685. This is a list of cons cells. Each cell contains:
  1686. - a specification of the files to be considered, either a list of files,
  1687. or a symbol whose function or variable value will be used to retrieve
  1688. a file name or a list of file names. If you use `org-agenda-files' for
  1689. that, all agenda files will be scanned for targets. Nil means consider
  1690. headings in the current buffer.
  1691. - A specification of how to find candidate refile targets. This may be
  1692. any of:
  1693. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1694. This tag has to be present in all target headlines, inheritance will
  1695. not be considered.
  1696. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1697. todo keyword.
  1698. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1699. headlines that are refiling targets.
  1700. - a cons cell (:level . N). Any headline of level N is considered a target.
  1701. Note that, when `org-odd-levels-only' is set, level corresponds to
  1702. order in hierarchy, not to the number of stars.
  1703. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1704. Note that, when `org-odd-levels-only' is set, level corresponds to
  1705. order in hierarchy, not to the number of stars.
  1706. Each element of this list generates a set of possible targets.
  1707. The union of these sets is presented (with completion) to
  1708. the user by `org-refile'.
  1709. You can set the variable `org-refile-target-verify-function' to a function
  1710. to verify each headline found by the simple criteria above.
  1711. When this variable is nil, all top-level headlines in the current buffer
  1712. are used, equivalent to the value `((nil . (:level . 1))'."
  1713. :group 'org-refile
  1714. :type '(repeat
  1715. (cons
  1716. (choice :value org-agenda-files
  1717. (const :tag "All agenda files" org-agenda-files)
  1718. (const :tag "Current buffer" nil)
  1719. (function) (variable) (file))
  1720. (choice :tag "Identify target headline by"
  1721. (cons :tag "Specific tag" (const :value :tag) (string))
  1722. (cons :tag "TODO keyword" (const :value :todo) (string))
  1723. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1724. (cons :tag "Level number" (const :value :level) (integer))
  1725. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1726. (defcustom org-refile-target-verify-function nil
  1727. "Function to verify if the headline at point should be a refile target.
  1728. The function will be called without arguments, with point at the
  1729. beginning of the headline. It should return t and leave point
  1730. where it is if the headline is a valid target for refiling.
  1731. If the target should not be selected, the function must return nil.
  1732. In addition to this, it may move point to a place from where the search
  1733. should be continued. For example, the function may decide that the entire
  1734. subtree of the current entry should be excluded and move point to the end
  1735. of the subtree."
  1736. :group 'org-refile
  1737. :type 'function)
  1738. (defcustom org-refile-use-cache nil
  1739. "Non-nil means cache refile targets to speed up the process.
  1740. The cache for a particular file will be updated automatically when
  1741. the buffer has been killed, or when any of the marker used for flagging
  1742. refile targets no longer points at a live buffer.
  1743. If you have added new entries to a buffer that might themselves be targets,
  1744. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1745. find that easier, `C-u C-u C-u C-c C-w'."
  1746. :group 'org-refile
  1747. :version "24.1"
  1748. :type 'boolean)
  1749. (defcustom org-refile-use-outline-path nil
  1750. "Non-nil means provide refile targets as paths.
  1751. So a level 3 headline will be available as level1/level2/level3.
  1752. When the value is `file', also include the file name (without directory)
  1753. into the path. In this case, you can also stop the completion after
  1754. the file name, to get entries inserted as top level in the file.
  1755. When `full-file-path', include the full file path."
  1756. :group 'org-refile
  1757. :type '(choice
  1758. (const :tag "Not" nil)
  1759. (const :tag "Yes" t)
  1760. (const :tag "Start with file name" file)
  1761. (const :tag "Start with full file path" full-file-path)))
  1762. (defcustom org-outline-path-complete-in-steps t
  1763. "Non-nil means complete the outline path in hierarchical steps.
  1764. When Org-mode uses the refile interface to select an outline path
  1765. \(see variable `org-refile-use-outline-path'), the completion of
  1766. the path can be done is a single go, or if can be done in steps down
  1767. the headline hierarchy. Going in steps is probably the best if you
  1768. do not use a special completion package like `ido' or `icicles'.
  1769. However, when using these packages, going in one step can be very
  1770. fast, while still showing the whole path to the entry."
  1771. :group 'org-refile
  1772. :type 'boolean)
  1773. (defcustom org-refile-allow-creating-parent-nodes nil
  1774. "Non-nil means allow to create new nodes as refile targets.
  1775. New nodes are then created by adding \"/new node name\" to the completion
  1776. of an existing node. When the value of this variable is `confirm',
  1777. new node creation must be confirmed by the user (recommended)
  1778. When nil, the completion must match an existing entry.
  1779. Note that, if the new heading is not seen by the criteria
  1780. listed in `org-refile-targets', multiple instances of the same
  1781. heading would be created by trying again to file under the new
  1782. heading."
  1783. :group 'org-refile
  1784. :type '(choice
  1785. (const :tag "Never" nil)
  1786. (const :tag "Always" t)
  1787. (const :tag "Prompt for confirmation" confirm)))
  1788. (defcustom org-refile-active-region-within-subtree nil
  1789. "Non-nil means also refile active region within a subtree.
  1790. By default `org-refile' doesn't allow refiling regions if they
  1791. don't contain a set of subtrees, but it might be convenient to
  1792. do so sometimes: in that case, the first line of the region is
  1793. converted to a headline before refiling."
  1794. :group 'org-refile
  1795. :version "24.1"
  1796. :type 'boolean)
  1797. (defgroup org-todo nil
  1798. "Options concerning TODO items in Org-mode."
  1799. :tag "Org TODO"
  1800. :group 'org)
  1801. (defgroup org-progress nil
  1802. "Options concerning Progress logging in Org-mode."
  1803. :tag "Org Progress"
  1804. :group 'org-time)
  1805. (defvar org-todo-interpretation-widgets
  1806. '((:tag "Sequence (cycling hits every state)" sequence)
  1807. (:tag "Type (cycling directly to DONE)" type))
  1808. "The available interpretation symbols for customizing `org-todo-keywords'.
  1809. Interested libraries should add to this list.")
  1810. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1811. "List of TODO entry keyword sequences and their interpretation.
  1812. \\<org-mode-map>This is a list of sequences.
  1813. Each sequence starts with a symbol, either `sequence' or `type',
  1814. indicating if the keywords should be interpreted as a sequence of
  1815. action steps, or as different types of TODO items. The first
  1816. keywords are states requiring action - these states will select a headline
  1817. for inclusion into the global TODO list Org-mode produces. If one of
  1818. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1819. signify that no further action is necessary. If \"|\" is not found,
  1820. the last keyword is treated as the only DONE state of the sequence.
  1821. The command \\[org-todo] cycles an entry through these states, and one
  1822. additional state where no keyword is present. For details about this
  1823. cycling, see the manual.
  1824. TODO keywords and interpretation can also be set on a per-file basis with
  1825. the special #+SEQ_TODO and #+TYP_TODO lines.
  1826. Each keyword can optionally specify a character for fast state selection
  1827. \(in combination with the variable `org-use-fast-todo-selection')
  1828. and specifiers for state change logging, using the same syntax that
  1829. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1830. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1831. indicates to record a time stamp each time this state is selected.
  1832. Each keyword may also specify if a timestamp or a note should be
  1833. recorded when entering or leaving the state, by adding additional
  1834. characters in the parenthesis after the keyword. This looks like this:
  1835. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1836. record only the time of the state change. With X and Y being either
  1837. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1838. Y when leaving the state if and only if the *target* state does not
  1839. define X. You may omit any of the fast-selection key or X or /Y,
  1840. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1841. For backward compatibility, this variable may also be just a list
  1842. of keywords. In this case the interpretation (sequence or type) will be
  1843. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1844. :group 'org-todo
  1845. :group 'org-keywords
  1846. :type '(choice
  1847. (repeat :tag "Old syntax, just keywords"
  1848. (string :tag "Keyword"))
  1849. (repeat :tag "New syntax"
  1850. (cons
  1851. (choice
  1852. :tag "Interpretation"
  1853. ;;Quick and dirty way to see
  1854. ;;`org-todo-interpretations'. This takes the
  1855. ;;place of item arguments
  1856. :convert-widget
  1857. (lambda (widget)
  1858. (widget-put widget
  1859. :args (mapcar
  1860. #'(lambda (x)
  1861. (widget-convert
  1862. (cons 'const x)))
  1863. org-todo-interpretation-widgets))
  1864. widget))
  1865. (repeat
  1866. (string :tag "Keyword"))))))
  1867. (defvar org-todo-keywords-1 nil
  1868. "All TODO and DONE keywords active in a buffer.")
  1869. (make-variable-buffer-local 'org-todo-keywords-1)
  1870. (defvar org-todo-keywords-for-agenda nil)
  1871. (defvar org-done-keywords-for-agenda nil)
  1872. (defvar org-drawers-for-agenda nil)
  1873. (defvar org-todo-keyword-alist-for-agenda nil)
  1874. (defvar org-tag-alist-for-agenda nil)
  1875. (defvar org-agenda-contributing-files nil)
  1876. (defvar org-not-done-keywords nil)
  1877. (make-variable-buffer-local 'org-not-done-keywords)
  1878. (defvar org-done-keywords nil)
  1879. (make-variable-buffer-local 'org-done-keywords)
  1880. (defvar org-todo-heads nil)
  1881. (make-variable-buffer-local 'org-todo-heads)
  1882. (defvar org-todo-sets nil)
  1883. (make-variable-buffer-local 'org-todo-sets)
  1884. (defvar org-todo-log-states nil)
  1885. (make-variable-buffer-local 'org-todo-log-states)
  1886. (defvar org-todo-kwd-alist nil)
  1887. (make-variable-buffer-local 'org-todo-kwd-alist)
  1888. (defvar org-todo-key-alist nil)
  1889. (make-variable-buffer-local 'org-todo-key-alist)
  1890. (defvar org-todo-key-trigger nil)
  1891. (make-variable-buffer-local 'org-todo-key-trigger)
  1892. (defcustom org-todo-interpretation 'sequence
  1893. "Controls how TODO keywords are interpreted.
  1894. This variable is in principle obsolete and is only used for
  1895. backward compatibility, if the interpretation of todo keywords is
  1896. not given already in `org-todo-keywords'. See that variable for
  1897. more information."
  1898. :group 'org-todo
  1899. :group 'org-keywords
  1900. :type '(choice (const sequence)
  1901. (const type)))
  1902. (defcustom org-use-fast-todo-selection t
  1903. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1904. This variable describes if and under what circumstances the cycling
  1905. mechanism for TODO keywords will be replaced by a single-key, direct
  1906. selection scheme.
  1907. When nil, fast selection is never used.
  1908. When the symbol `prefix', it will be used when `org-todo' is called
  1909. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  1910. `C-u t' in an agenda buffer.
  1911. When t, fast selection is used by default. In this case, the prefix
  1912. argument forces cycling instead.
  1913. In all cases, the special interface is only used if access keys have
  1914. actually been assigned by the user, i.e. if keywords in the configuration
  1915. are followed by a letter in parenthesis, like TODO(t)."
  1916. :group 'org-todo
  1917. :type '(choice
  1918. (const :tag "Never" nil)
  1919. (const :tag "By default" t)
  1920. (const :tag "Only with C-u C-c C-t" prefix)))
  1921. (defcustom org-provide-todo-statistics t
  1922. "Non-nil means update todo statistics after insert and toggle.
  1923. ALL-HEADLINES means update todo statistics by including headlines
  1924. with no TODO keyword as well, counting them as not done.
  1925. A list of TODO keywords means the same, but skip keywords that are
  1926. not in this list.
  1927. When this is set, todo statistics is updated in the parent of the
  1928. current entry each time a todo state is changed."
  1929. :group 'org-todo
  1930. :type '(choice
  1931. (const :tag "Yes, only for TODO entries" t)
  1932. (const :tag "Yes, including all entries" 'all-headlines)
  1933. (repeat :tag "Yes, for TODOs in this list"
  1934. (string :tag "TODO keyword"))
  1935. (other :tag "No TODO statistics" nil)))
  1936. (defcustom org-hierarchical-todo-statistics t
  1937. "Non-nil means TODO statistics covers just direct children.
  1938. When nil, all entries in the subtree are considered.
  1939. This has only an effect if `org-provide-todo-statistics' is set.
  1940. To set this to nil for only a single subtree, use a COOKIE_DATA
  1941. property and include the word \"recursive\" into the value."
  1942. :group 'org-todo
  1943. :type 'boolean)
  1944. (defcustom org-after-todo-state-change-hook nil
  1945. "Hook which is run after the state of a TODO item was changed.
  1946. The new state (a string with a TODO keyword, or nil) is available in the
  1947. Lisp variable `org-state'."
  1948. :group 'org-todo
  1949. :type 'hook)
  1950. (defvar org-blocker-hook nil
  1951. "Hook for functions that are allowed to block a state change.
  1952. Each function gets as its single argument a property list, see
  1953. `org-trigger-hook' for more information about this list.
  1954. If any of the functions in this hook returns nil, the state change
  1955. is blocked.")
  1956. (defvar org-trigger-hook nil
  1957. "Hook for functions that are triggered by a state change.
  1958. Each function gets as its single argument a property list with at least
  1959. the following elements:
  1960. (:type type-of-change :position pos-at-entry-start
  1961. :from old-state :to new-state)
  1962. Depending on the type, more properties may be present.
  1963. This mechanism is currently implemented for:
  1964. TODO state changes
  1965. ------------------
  1966. :type todo-state-change
  1967. :from previous state (keyword as a string), or nil, or a symbol
  1968. 'todo' or 'done', to indicate the general type of state.
  1969. :to new state, like in :from")
  1970. (defcustom org-enforce-todo-dependencies nil
  1971. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1972. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1973. be blocked if any prior sibling is not yet done.
  1974. Finally, if the parent is blocked because of ordered siblings of its own,
  1975. the child will also be blocked."
  1976. :set (lambda (var val)
  1977. (set var val)
  1978. (if val
  1979. (add-hook 'org-blocker-hook
  1980. 'org-block-todo-from-children-or-siblings-or-parent)
  1981. (remove-hook 'org-blocker-hook
  1982. 'org-block-todo-from-children-or-siblings-or-parent)))
  1983. :group 'org-todo
  1984. :type 'boolean)
  1985. (defcustom org-enforce-todo-checkbox-dependencies nil
  1986. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1987. When this is nil, checkboxes have no influence on switching TODO states.
  1988. When non-nil, you first need to check off all check boxes before the TODO
  1989. entry can be switched to DONE.
  1990. This variable needs to be set before org.el is loaded, and you need to
  1991. restart Emacs after a change to make the change effective. The only way
  1992. to change is while Emacs is running is through the customize interface."
  1993. :set (lambda (var val)
  1994. (set var val)
  1995. (if val
  1996. (add-hook 'org-blocker-hook
  1997. 'org-block-todo-from-checkboxes)
  1998. (remove-hook 'org-blocker-hook
  1999. 'org-block-todo-from-checkboxes)))
  2000. :group 'org-todo
  2001. :type 'boolean)
  2002. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2003. "Non-nil means inserting a TODO heading is treated as state change.
  2004. So when the command \\[org-insert-todo-heading] is used, state change
  2005. logging will apply if appropriate. When nil, the new TODO item will
  2006. be inserted directly, and no logging will take place."
  2007. :group 'org-todo
  2008. :type 'boolean)
  2009. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2010. "Non-nil means switching TODO states with S-cursor counts as state change.
  2011. This is the default behavior. However, setting this to nil allows a
  2012. convenient way to select a TODO state and bypass any logging associated
  2013. with that."
  2014. :group 'org-todo
  2015. :type 'boolean)
  2016. (defcustom org-todo-state-tags-triggers nil
  2017. "Tag changes that should be triggered by TODO state changes.
  2018. This is a list. Each entry is
  2019. (state-change (tag . flag) .......)
  2020. State-change can be a string with a state, and empty string to indicate the
  2021. state that has no TODO keyword, or it can be one of the symbols `todo'
  2022. or `done', meaning any not-done or done state, respectively."
  2023. :group 'org-todo
  2024. :group 'org-tags
  2025. :type '(repeat
  2026. (cons (choice :tag "When changing to"
  2027. (const :tag "Not-done state" todo)
  2028. (const :tag "Done state" done)
  2029. (string :tag "State"))
  2030. (repeat
  2031. (cons :tag "Tag action"
  2032. (string :tag "Tag")
  2033. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2034. (defcustom org-log-done nil
  2035. "Information to record when a task moves to the DONE state.
  2036. Possible values are:
  2037. nil Don't add anything, just change the keyword
  2038. time Add a time stamp to the task
  2039. note Prompt for a note and add it with template `org-log-note-headings'
  2040. This option can also be set with on a per-file-basis with
  2041. #+STARTUP: nologdone
  2042. #+STARTUP: logdone
  2043. #+STARTUP: lognotedone
  2044. You can have local logging settings for a subtree by setting the LOGGING
  2045. property to one or more of these keywords."
  2046. :group 'org-todo
  2047. :group 'org-progress
  2048. :type '(choice
  2049. (const :tag "No logging" nil)
  2050. (const :tag "Record CLOSED timestamp" time)
  2051. (const :tag "Record CLOSED timestamp with note." note)))
  2052. ;; Normalize old uses of org-log-done.
  2053. (cond
  2054. ((eq org-log-done t) (setq org-log-done 'time))
  2055. ((and (listp org-log-done) (memq 'done org-log-done))
  2056. (setq org-log-done 'note)))
  2057. (defcustom org-log-reschedule nil
  2058. "Information to record when the scheduling date of a tasks is modified.
  2059. Possible values are:
  2060. nil Don't add anything, just change the date
  2061. time Add a time stamp to the task
  2062. note Prompt for a note and add it with template `org-log-note-headings'
  2063. This option can also be set with on a per-file-basis with
  2064. #+STARTUP: nologreschedule
  2065. #+STARTUP: logreschedule
  2066. #+STARTUP: lognotereschedule"
  2067. :group 'org-todo
  2068. :group 'org-progress
  2069. :type '(choice
  2070. (const :tag "No logging" nil)
  2071. (const :tag "Record timestamp" time)
  2072. (const :tag "Record timestamp with note." note)))
  2073. (defcustom org-log-redeadline nil
  2074. "Information to record when the deadline date of a tasks is modified.
  2075. Possible values are:
  2076. nil Don't add anything, just change the date
  2077. time Add a time stamp to the task
  2078. note Prompt for a note and add it with template `org-log-note-headings'
  2079. This option can also be set with on a per-file-basis with
  2080. #+STARTUP: nologredeadline
  2081. #+STARTUP: logredeadline
  2082. #+STARTUP: lognoteredeadline
  2083. You can have local logging settings for a subtree by setting the LOGGING
  2084. property to one or more of these keywords."
  2085. :group 'org-todo
  2086. :group 'org-progress
  2087. :type '(choice
  2088. (const :tag "No logging" nil)
  2089. (const :tag "Record timestamp" time)
  2090. (const :tag "Record timestamp with note." note)))
  2091. (defcustom org-log-note-clock-out nil
  2092. "Non-nil means record a note when clocking out of an item.
  2093. This can also be configured on a per-file basis by adding one of
  2094. the following lines anywhere in the buffer:
  2095. #+STARTUP: lognoteclock-out
  2096. #+STARTUP: nolognoteclock-out"
  2097. :group 'org-todo
  2098. :group 'org-progress
  2099. :type 'boolean)
  2100. (defcustom org-log-done-with-time t
  2101. "Non-nil means the CLOSED time stamp will contain date and time.
  2102. When nil, only the date will be recorded."
  2103. :group 'org-progress
  2104. :type 'boolean)
  2105. (defcustom org-log-note-headings
  2106. '((done . "CLOSING NOTE %t")
  2107. (state . "State %-12s from %-12S %t")
  2108. (note . "Note taken on %t")
  2109. (reschedule . "Rescheduled from %S on %t")
  2110. (delschedule . "Not scheduled, was %S on %t")
  2111. (redeadline . "New deadline from %S on %t")
  2112. (deldeadline . "Removed deadline, was %S on %t")
  2113. (refile . "Refiled on %t")
  2114. (clock-out . ""))
  2115. "Headings for notes added to entries.
  2116. The value is an alist, with the car being a symbol indicating the note
  2117. context, and the cdr is the heading to be used. The heading may also be the
  2118. empty string.
  2119. %t in the heading will be replaced by a time stamp.
  2120. %T will be an active time stamp instead the default inactive one
  2121. %d will be replaced by a short-format time stamp.
  2122. %D will be replaced by an active short-format time stamp.
  2123. %s will be replaced by the new TODO state, in double quotes.
  2124. %S will be replaced by the old TODO state, in double quotes.
  2125. %u will be replaced by the user name.
  2126. %U will be replaced by the full user name.
  2127. In fact, it is not a good idea to change the `state' entry, because
  2128. agenda log mode depends on the format of these entries."
  2129. :group 'org-todo
  2130. :group 'org-progress
  2131. :type '(list :greedy t
  2132. (cons (const :tag "Heading when closing an item" done) string)
  2133. (cons (const :tag
  2134. "Heading when changing todo state (todo sequence only)"
  2135. state) string)
  2136. (cons (const :tag "Heading when just taking a note" note) string)
  2137. (cons (const :tag "Heading when clocking out" clock-out) string)
  2138. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2139. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2140. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2141. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2142. (cons (const :tag "Heading when refiling" refile) string)))
  2143. (unless (assq 'note org-log-note-headings)
  2144. (push '(note . "%t") org-log-note-headings))
  2145. (defcustom org-log-into-drawer nil
  2146. "Non-nil means insert state change notes and time stamps into a drawer.
  2147. When nil, state changes notes will be inserted after the headline and
  2148. any scheduling and clock lines, but not inside a drawer.
  2149. The value of this variable should be the name of the drawer to use.
  2150. LOGBOOK is proposed as the default drawer for this purpose, you can
  2151. also set this to a string to define the drawer of your choice.
  2152. A value of t is also allowed, representing \"LOGBOOK\".
  2153. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2154. will be ignored.
  2155. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2156. a subtree."
  2157. :group 'org-todo
  2158. :group 'org-progress
  2159. :type '(choice
  2160. (const :tag "Not into a drawer" nil)
  2161. (const :tag "LOGBOOK" t)
  2162. (string :tag "Other")))
  2163. (if (fboundp 'defvaralias)
  2164. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2165. (defun org-log-into-drawer ()
  2166. "Return the value of `org-log-into-drawer', but let properties overrule.
  2167. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2168. used instead of the default value."
  2169. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2170. (cond
  2171. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2172. ((equal p "t") "LOGBOOK")
  2173. (t p))))
  2174. (defcustom org-log-state-notes-insert-after-drawers nil
  2175. "Non-nil means insert state change notes after any drawers in entry.
  2176. Only the drawers that *immediately* follow the headline and the
  2177. deadline/scheduled line are skipped.
  2178. When nil, insert notes right after the heading and perhaps the line
  2179. with deadline/scheduling if present.
  2180. This variable will have no effect if `org-log-into-drawer' is
  2181. set."
  2182. :group 'org-todo
  2183. :group 'org-progress
  2184. :type 'boolean)
  2185. (defcustom org-log-states-order-reversed t
  2186. "Non-nil means the latest state note will be directly after heading.
  2187. When nil, the state change notes will be ordered according to time."
  2188. :group 'org-todo
  2189. :group 'org-progress
  2190. :type 'boolean)
  2191. (defcustom org-todo-repeat-to-state nil
  2192. "The TODO state to which a repeater should return the repeating task.
  2193. By default this is the first task in a TODO sequence, or the previous state
  2194. in a TODO_TYP set. But you can specify another task here.
  2195. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2196. :group 'org-todo
  2197. :version "24.1"
  2198. :type '(choice (const :tag "Head of sequence" nil)
  2199. (string :tag "Specific state")))
  2200. (defcustom org-log-repeat 'time
  2201. "Non-nil means record moving through the DONE state when triggering repeat.
  2202. An auto-repeating task is immediately switched back to TODO when
  2203. marked DONE. If you are not logging state changes (by adding \"@\"
  2204. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2205. record a closing note, there will be no record of the task moving
  2206. through DONE. This variable forces taking a note anyway.
  2207. nil Don't force a record
  2208. time Record a time stamp
  2209. note Prompt for a note and add it with template `org-log-note-headings'
  2210. This option can also be set with on a per-file-basis with
  2211. #+STARTUP: nologrepeat
  2212. #+STARTUP: logrepeat
  2213. #+STARTUP: lognoterepeat
  2214. You can have local logging settings for a subtree by setting the LOGGING
  2215. property to one or more of these keywords."
  2216. :group 'org-todo
  2217. :group 'org-progress
  2218. :type '(choice
  2219. (const :tag "Don't force a record" nil)
  2220. (const :tag "Force recording the DONE state" time)
  2221. (const :tag "Force recording a note with the DONE state" note)))
  2222. (defgroup org-priorities nil
  2223. "Priorities in Org-mode."
  2224. :tag "Org Priorities"
  2225. :group 'org-todo)
  2226. (defcustom org-enable-priority-commands t
  2227. "Non-nil means priority commands are active.
  2228. When nil, these commands will be disabled, so that you never accidentally
  2229. set a priority."
  2230. :group 'org-priorities
  2231. :type 'boolean)
  2232. (defcustom org-highest-priority ?A
  2233. "The highest priority of TODO items. A character like ?A, ?B etc.
  2234. Must have a smaller ASCII number than `org-lowest-priority'."
  2235. :group 'org-priorities
  2236. :type 'character)
  2237. (defcustom org-lowest-priority ?C
  2238. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2239. Must have a larger ASCII number than `org-highest-priority'."
  2240. :group 'org-priorities
  2241. :type 'character)
  2242. (defcustom org-default-priority ?B
  2243. "The default priority of TODO items.
  2244. This is the priority an item gets if no explicit priority is given.
  2245. When starting to cycle on an empty priority the first step in the cycle
  2246. depends on `org-priority-start-cycle-with-default'. The resulting first
  2247. step priority must not exceed the range from `org-highest-priority' to
  2248. `org-lowest-priority' which means that `org-default-priority' has to be
  2249. in this range exclusive or inclusive the range boundaries. Else the
  2250. first step refuses to set the default and the second will fall back
  2251. to (depending on the command used) the highest or lowest priority."
  2252. :group 'org-priorities
  2253. :type 'character)
  2254. (defcustom org-priority-start-cycle-with-default t
  2255. "Non-nil means start with default priority when starting to cycle.
  2256. When this is nil, the first step in the cycle will be (depending on the
  2257. command used) one higher or lower than the default priority.
  2258. See also `org-default-priority'."
  2259. :group 'org-priorities
  2260. :type 'boolean)
  2261. (defcustom org-get-priority-function nil
  2262. "Function to extract the priority from a string.
  2263. The string is normally the headline. If this is nil Org computes the
  2264. priority from the priority cookie like [#A] in the headline. It returns
  2265. an integer, increasing by 1000 for each priority level.
  2266. The user can set a different function here, which should take a string
  2267. as an argument and return the numeric priority."
  2268. :group 'org-priorities
  2269. :version "24.1"
  2270. :type 'function)
  2271. (defgroup org-time nil
  2272. "Options concerning time stamps and deadlines in Org-mode."
  2273. :tag "Org Time"
  2274. :group 'org)
  2275. (defcustom org-insert-labeled-timestamps-at-point nil
  2276. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2277. When nil, these labeled time stamps are forces into the second line of an
  2278. entry, just after the headline. When scheduling from the global TODO list,
  2279. the time stamp will always be forced into the second line."
  2280. :group 'org-time
  2281. :type 'boolean)
  2282. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2283. "Formats for `format-time-string' which are used for time stamps.
  2284. It is not recommended to change this constant.")
  2285. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2286. "Number of minutes to round time stamps to.
  2287. These are two values, the first applies when first creating a time stamp.
  2288. The second applies when changing it with the commands `S-up' and `S-down'.
  2289. When changing the time stamp, this means that it will change in steps
  2290. of N minutes, as given by the second value.
  2291. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2292. numbers should be factors of 60, so for example 5, 10, 15.
  2293. When this is larger than 1, you can still force an exact time stamp by using
  2294. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2295. and by using a prefix arg to `S-up/down' to specify the exact number
  2296. of minutes to shift."
  2297. :group 'org-time
  2298. :get #'(lambda (var) ; Make sure both elements are there
  2299. (if (integerp (default-value var))
  2300. (list (default-value var) 5)
  2301. (default-value var)))
  2302. :type '(list
  2303. (integer :tag "when inserting times")
  2304. (integer :tag "when modifying times")))
  2305. ;; Normalize old customizations of this variable.
  2306. (when (integerp org-time-stamp-rounding-minutes)
  2307. (setq org-time-stamp-rounding-minutes
  2308. (list org-time-stamp-rounding-minutes
  2309. org-time-stamp-rounding-minutes)))
  2310. (defcustom org-display-custom-times nil
  2311. "Non-nil means overlay custom formats over all time stamps.
  2312. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2313. To turn this on on a per-file basis, insert anywhere in the file:
  2314. #+STARTUP: customtime"
  2315. :group 'org-time
  2316. :set 'set-default
  2317. :type 'sexp)
  2318. (make-variable-buffer-local 'org-display-custom-times)
  2319. (defcustom org-time-stamp-custom-formats
  2320. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2321. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2322. These are overlaid over the default ISO format if the variable
  2323. `org-display-custom-times' is set. Time like %H:%M should be at the
  2324. end of the second format. The custom formats are also honored by export
  2325. commands, if custom time display is turned on at the time of export."
  2326. :group 'org-time
  2327. :type 'sexp)
  2328. (defun org-time-stamp-format (&optional long inactive)
  2329. "Get the right format for a time string."
  2330. (let ((f (if long (cdr org-time-stamp-formats)
  2331. (car org-time-stamp-formats))))
  2332. (if inactive
  2333. (concat "[" (substring f 1 -1) "]")
  2334. f)))
  2335. (defcustom org-time-clocksum-format "%d:%02d"
  2336. "The format string used when creating CLOCKSUM lines.
  2337. This is also used when org-mode generates a time duration."
  2338. :group 'org-time
  2339. :type 'string)
  2340. (defcustom org-time-clocksum-use-fractional nil
  2341. "If non-nil, \\[org-clock-display] uses fractional times.
  2342. org-mode generates a time duration."
  2343. :group 'org-time
  2344. :type 'boolean)
  2345. (defcustom org-time-clocksum-fractional-format "%.2f"
  2346. "The format string used when creating CLOCKSUM lines, or when
  2347. org-mode generates a time duration."
  2348. :group 'org-time
  2349. :type 'string)
  2350. (defcustom org-deadline-warning-days 14
  2351. "No. of days before expiration during which a deadline becomes active.
  2352. This variable governs the display in sparse trees and in the agenda.
  2353. When 0 or negative, it means use this number (the absolute value of it)
  2354. even if a deadline has a different individual lead time specified.
  2355. Custom commands can set this variable in the options section."
  2356. :group 'org-time
  2357. :group 'org-agenda-daily/weekly
  2358. :type 'integer)
  2359. (defcustom org-read-date-prefer-future t
  2360. "Non-nil means assume future for incomplete date input from user.
  2361. This affects the following situations:
  2362. 1. The user gives a month but not a year.
  2363. For example, if it is April and you enter \"feb 2\", this will be read
  2364. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2365. 2. The user gives a day, but no month.
  2366. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2367. read this as the third of *next* month. However, if you enter \"17\",
  2368. it will be considered as *this* month.
  2369. If you set this variable to the symbol `time', then also the following
  2370. will work:
  2371. 3. If the user gives a time.
  2372. If the time is before now, it will be interpreted as tomorrow.
  2373. Currently none of this works for ISO week specifications.
  2374. When this option is nil, the current day, month and year will always be
  2375. used as defaults.
  2376. See also `org-agenda-jump-prefer-future'."
  2377. :group 'org-time
  2378. :type '(choice
  2379. (const :tag "Never" nil)
  2380. (const :tag "Check month and day" t)
  2381. (const :tag "Check month, day, and time" time)))
  2382. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2383. "Should the agenda jump command prefer the future for incomplete dates?
  2384. The default is to do the same as configured in `org-read-date-prefer-future'.
  2385. But you can also set a deviating value here.
  2386. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2387. :group 'org-agenda
  2388. :group 'org-time
  2389. :version "24.1"
  2390. :type '(choice
  2391. (const :tag "Use org-read-date-prefer-future"
  2392. org-read-date-prefer-future)
  2393. (const :tag "Never" nil)
  2394. (const :tag "Always" t)))
  2395. (defcustom org-read-date-force-compatible-dates t
  2396. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2397. Depending on the system Emacs is running on, certain dates cannot
  2398. be represented with the type used internally to represent time.
  2399. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2400. correctly. Some systems allow for earlier dates, some for later,
  2401. some for both. One way to find out it to insert any date into an
  2402. Org buffer, putting the cursor on the year and hitting S-up and
  2403. S-down to test the range.
  2404. When this variable is set to t, the date/time prompt will not let
  2405. you specify dates outside the 1970-2037 range, so it is certain that
  2406. these dates will work in whatever version of Emacs you are
  2407. running, and also that you can move a file from one Emacs implementation
  2408. to another. WHenever Org is forcing the year for you, it will display
  2409. a message and beep.
  2410. When this variable is nil, Org will check if the date is
  2411. representable in the specific Emacs implementation you are using.
  2412. If not, it will force a year, usually the current year, and beep
  2413. to remind you. Currently this setting is not recommended because
  2414. the likelihood that you will open your Org files in an Emacs that
  2415. has limited date range is not negligible.
  2416. A workaround for this problem is to use diary sexp dates for time
  2417. stamps outside of this range."
  2418. :group 'org-time
  2419. :version "24.1"
  2420. :type 'boolean)
  2421. (defcustom org-read-date-display-live t
  2422. "Non-nil means display current interpretation of date prompt live.
  2423. This display will be in an overlay, in the minibuffer."
  2424. :group 'org-time
  2425. :type 'boolean)
  2426. (defcustom org-read-date-popup-calendar t
  2427. "Non-nil means pop up a calendar when prompting for a date.
  2428. In the calendar, the date can be selected with mouse-1. However, the
  2429. minibuffer will also be active, and you can simply enter the date as well.
  2430. When nil, only the minibuffer will be available."
  2431. :group 'org-time
  2432. :type 'boolean)
  2433. (if (fboundp 'defvaralias)
  2434. (defvaralias 'org-popup-calendar-for-date-prompt
  2435. 'org-read-date-popup-calendar))
  2436. (defcustom org-read-date-minibuffer-setup-hook nil
  2437. "Hook to be used to set up keys for the date/time interface.
  2438. Add key definitions to `minibuffer-local-map', which will be a temporary
  2439. copy."
  2440. :group 'org-time
  2441. :type 'hook)
  2442. (defcustom org-extend-today-until 0
  2443. "The hour when your day really ends. Must be an integer.
  2444. This has influence for the following applications:
  2445. - When switching the agenda to \"today\". It it is still earlier than
  2446. the time given here, the day recognized as TODAY is actually yesterday.
  2447. - When a date is read from the user and it is still before the time given
  2448. here, the current date and time will be assumed to be yesterday, 23:59.
  2449. Also, timestamps inserted in capture templates follow this rule.
  2450. IMPORTANT: This is a feature whose implementation is and likely will
  2451. remain incomplete. Really, it is only here because past midnight seems to
  2452. be the favorite working time of John Wiegley :-)"
  2453. :group 'org-time
  2454. :type 'integer)
  2455. (defcustom org-use-effective-time nil
  2456. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2457. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2458. \"effective time\" of any timestamps between midnight and 8am will be
  2459. 23:59 of the previous day."
  2460. :group 'org-time
  2461. :version "24.1"
  2462. :type 'boolean)
  2463. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2464. "When non-nil, use the last clock out time for `org-todo'.
  2465. Note that this option has precedence over the combined use of
  2466. `org-use-effective-time' and `org-extend-today-until'."
  2467. :group 'org-time
  2468. ;; :version "24.3"
  2469. :type 'boolean)
  2470. (defcustom org-edit-timestamp-down-means-later nil
  2471. "Non-nil means S-down will increase the time in a time stamp.
  2472. When nil, S-up will increase."
  2473. :group 'org-time
  2474. :type 'boolean)
  2475. (defcustom org-calendar-follow-timestamp-change t
  2476. "Non-nil means make the calendar window follow timestamp changes.
  2477. When a timestamp is modified and the calendar window is visible, it will be
  2478. moved to the new date."
  2479. :group 'org-time
  2480. :type 'boolean)
  2481. (defgroup org-tags nil
  2482. "Options concerning tags in Org-mode."
  2483. :tag "Org Tags"
  2484. :group 'org)
  2485. (defcustom org-tag-alist nil
  2486. "List of tags allowed in Org-mode files.
  2487. When this list is nil, Org-mode will base TAG input on what is already in the
  2488. buffer.
  2489. The value of this variable is an alist, the car of each entry must be a
  2490. keyword as a string, the cdr may be a character that is used to select
  2491. that tag through the fast-tag-selection interface.
  2492. See the manual for details."
  2493. :group 'org-tags
  2494. :type '(repeat
  2495. (choice
  2496. (cons (string :tag "Tag name")
  2497. (character :tag "Access char"))
  2498. (list :tag "Start radio group"
  2499. (const :startgroup)
  2500. (option (string :tag "Group description")))
  2501. (list :tag "End radio group"
  2502. (const :endgroup)
  2503. (option (string :tag "Group description")))
  2504. (const :tag "New line" (:newline)))))
  2505. (defcustom org-tag-persistent-alist nil
  2506. "List of tags that will always appear in all Org-mode files.
  2507. This is in addition to any in buffer settings or customizations
  2508. of `org-tag-alist'.
  2509. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2510. The value of this variable is an alist, the car of each entry must be a
  2511. keyword as a string, the cdr may be a character that is used to select
  2512. that tag through the fast-tag-selection interface.
  2513. See the manual for details.
  2514. To disable these tags on a per-file basis, insert anywhere in the file:
  2515. #+STARTUP: noptag"
  2516. :group 'org-tags
  2517. :type '(repeat
  2518. (choice
  2519. (cons (string :tag "Tag name")
  2520. (character :tag "Access char"))
  2521. (const :tag "Start radio group" (:startgroup))
  2522. (const :tag "End radio group" (:endgroup))
  2523. (const :tag "New line" (:newline)))))
  2524. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2525. "If non-nil, always offer completion for all tags of all agenda files.
  2526. Instead of customizing this variable directly, you might want to
  2527. set it locally for capture buffers, because there no list of
  2528. tags in that file can be created dynamically (there are none).
  2529. (add-hook 'org-capture-mode-hook
  2530. (lambda ()
  2531. (set (make-local-variable
  2532. 'org-complete-tags-always-offer-all-agenda-tags)
  2533. t)))"
  2534. :group 'org-tags
  2535. :version "24.1"
  2536. :type 'boolean)
  2537. (defvar org-file-tags nil
  2538. "List of tags that can be inherited by all entries in the file.
  2539. The tags will be inherited if the variable `org-use-tag-inheritance'
  2540. says they should be.
  2541. This variable is populated from #+FILETAGS lines.")
  2542. (defcustom org-use-fast-tag-selection 'auto
  2543. "Non-nil means use fast tag selection scheme.
  2544. This is a special interface to select and deselect tags with single keys.
  2545. When nil, fast selection is never used.
  2546. When the symbol `auto', fast selection is used if and only if selection
  2547. characters for tags have been configured, either through the variable
  2548. `org-tag-alist' or through a #+TAGS line in the buffer.
  2549. When t, fast selection is always used and selection keys are assigned
  2550. automatically if necessary."
  2551. :group 'org-tags
  2552. :type '(choice
  2553. (const :tag "Always" t)
  2554. (const :tag "Never" nil)
  2555. (const :tag "When selection characters are configured" 'auto)))
  2556. (defcustom org-fast-tag-selection-single-key nil
  2557. "Non-nil means fast tag selection exits after first change.
  2558. When nil, you have to press RET to exit it.
  2559. During fast tag selection, you can toggle this flag with `C-c'.
  2560. This variable can also have the value `expert'. In this case, the window
  2561. displaying the tags menu is not even shown, until you press C-c again."
  2562. :group 'org-tags
  2563. :type '(choice
  2564. (const :tag "No" nil)
  2565. (const :tag "Yes" t)
  2566. (const :tag "Expert" expert)))
  2567. (defvar org-fast-tag-selection-include-todo nil
  2568. "Non-nil means fast tags selection interface will also offer TODO states.
  2569. This is an undocumented feature, you should not rely on it.")
  2570. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2571. "The column to which tags should be indented in a headline.
  2572. If this number is positive, it specifies the column. If it is negative,
  2573. it means that the tags should be flushright to that column. For example,
  2574. -80 works well for a normal 80 character screen.
  2575. When 0, place tags directly after headline text, with only one space in
  2576. between."
  2577. :group 'org-tags
  2578. :type 'integer)
  2579. (defcustom org-auto-align-tags t
  2580. "Non-nil keeps tags aligned when modifying headlines.
  2581. Some operations (i.e. demoting) change the length of a headline and
  2582. therefore shift the tags around. With this option turned on, after
  2583. each such operation the tags are again aligned to `org-tags-column'."
  2584. :group 'org-tags
  2585. :type 'boolean)
  2586. (defcustom org-use-tag-inheritance t
  2587. "Non-nil means tags in levels apply also for sublevels.
  2588. When nil, only the tags directly given in a specific line apply there.
  2589. This may also be a list of tags that should be inherited, or a regexp that
  2590. matches tags that should be inherited. Additional control is possible
  2591. with the variable `org-tags-exclude-from-inheritance' which gives an
  2592. explicit list of tags to be excluded from inheritance., even if the value of
  2593. `org-use-tag-inheritance' would select it for inheritance.
  2594. If this option is t, a match early-on in a tree can lead to a large
  2595. number of matches in the subtree when constructing the agenda or creating
  2596. a sparse tree. If you only want to see the first match in a tree during
  2597. a search, check out the variable `org-tags-match-list-sublevels'."
  2598. :group 'org-tags
  2599. :type '(choice
  2600. (const :tag "Not" nil)
  2601. (const :tag "Always" t)
  2602. (repeat :tag "Specific tags" (string :tag "Tag"))
  2603. (regexp :tag "Tags matched by regexp")))
  2604. (defcustom org-tags-exclude-from-inheritance nil
  2605. "List of tags that should never be inherited.
  2606. This is a way to exclude a few tags from inheritance. For way to do
  2607. the opposite, to actively allow inheritance for selected tags,
  2608. see the variable `org-use-tag-inheritance'."
  2609. :group 'org-tags
  2610. :type '(repeat (string :tag "Tag")))
  2611. (defun org-tag-inherit-p (tag)
  2612. "Check if TAG is one that should be inherited."
  2613. (cond
  2614. ((member tag org-tags-exclude-from-inheritance) nil)
  2615. ((eq org-use-tag-inheritance t) t)
  2616. ((not org-use-tag-inheritance) nil)
  2617. ((stringp org-use-tag-inheritance)
  2618. (string-match org-use-tag-inheritance tag))
  2619. ((listp org-use-tag-inheritance)
  2620. (member tag org-use-tag-inheritance))
  2621. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2622. (defcustom org-tags-match-list-sublevels t
  2623. "Non-nil means list also sublevels of headlines matching a search.
  2624. This variable applies to tags/property searches, and also to stuck
  2625. projects because this search is based on a tags match as well.
  2626. When set to the symbol `indented', sublevels are indented with
  2627. leading dots.
  2628. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2629. the sublevels of a headline matching a tag search often also match
  2630. the same search. Listing all of them can create very long lists.
  2631. Setting this variable to nil causes subtrees of a match to be skipped.
  2632. This variable is semi-obsolete and probably should always be true. It
  2633. is better to limit inheritance to certain tags using the variables
  2634. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2635. :group 'org-tags
  2636. :type '(choice
  2637. (const :tag "No, don't list them" nil)
  2638. (const :tag "Yes, do list them" t)
  2639. (const :tag "List them, indented with leading dots" indented)))
  2640. (defcustom org-tags-sort-function nil
  2641. "When set, tags are sorted using this function as a comparator."
  2642. :group 'org-tags
  2643. :type '(choice
  2644. (const :tag "No sorting" nil)
  2645. (const :tag "Alphabetical" string<)
  2646. (const :tag "Reverse alphabetical" string>)
  2647. (function :tag "Custom function" nil)))
  2648. (defvar org-tags-history nil
  2649. "History of minibuffer reads for tags.")
  2650. (defvar org-last-tags-completion-table nil
  2651. "The last used completion table for tags.")
  2652. (defvar org-after-tags-change-hook nil
  2653. "Hook that is run after the tags in a line have changed.")
  2654. (defgroup org-properties nil
  2655. "Options concerning properties in Org-mode."
  2656. :tag "Org Properties"
  2657. :group 'org)
  2658. (defcustom org-property-format "%-10s %s"
  2659. "How property key/value pairs should be formatted by `indent-line'.
  2660. When `indent-line' hits a property definition, it will format the line
  2661. according to this format, mainly to make sure that the values are
  2662. lined-up with respect to each other."
  2663. :group 'org-properties
  2664. :type 'string)
  2665. (defcustom org-properties-postprocess-alist nil
  2666. "Alist of properties and functions to adjust inserted values.
  2667. Elements of this alist must be of the form
  2668. ([string] [function])
  2669. where [string] must be a property name and [function] must be a
  2670. lambda expression: this lambda expression must take one argument,
  2671. the value to adjust, and return the new value as a string.
  2672. For example, this element will allow the property \"Remaining\"
  2673. to be updated wrt the relation between the \"Effort\" property
  2674. and the clock summary:
  2675. ((\"Remaining\" (lambda(value)
  2676. (let ((clocksum (org-clock-sum-current-item))
  2677. (effort (org-duration-string-to-minutes
  2678. (org-entry-get (point) \"Effort\"))))
  2679. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2680. :group 'org-properties
  2681. :version "24.1"
  2682. :type '(alist :key-type (string :tag "Property")
  2683. :value-type (function :tag "Function")))
  2684. (defcustom org-use-property-inheritance nil
  2685. "Non-nil means properties apply also for sublevels.
  2686. This setting is chiefly used during property searches. Turning it on can
  2687. cause significant overhead when doing a search, which is why it is not
  2688. on by default.
  2689. When nil, only the properties directly given in the current entry count.
  2690. When t, every property is inherited. The value may also be a list of
  2691. properties that should have inheritance, or a regular expression matching
  2692. properties that should be inherited.
  2693. However, note that some special properties use inheritance under special
  2694. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2695. and the properties ending in \"_ALL\" when they are used as descriptor
  2696. for valid values of a property.
  2697. Note for programmers:
  2698. When querying an entry with `org-entry-get', you can control if inheritance
  2699. should be used. By default, `org-entry-get' looks only at the local
  2700. properties. You can request inheritance by setting the inherit argument
  2701. to t (to force inheritance) or to `selective' (to respect the setting
  2702. in this variable)."
  2703. :group 'org-properties
  2704. :type '(choice
  2705. (const :tag "Not" nil)
  2706. (const :tag "Always" t)
  2707. (repeat :tag "Specific properties" (string :tag "Property"))
  2708. (regexp :tag "Properties matched by regexp")))
  2709. (defun org-property-inherit-p (property)
  2710. "Check if PROPERTY is one that should be inherited."
  2711. (cond
  2712. ((eq org-use-property-inheritance t) t)
  2713. ((not org-use-property-inheritance) nil)
  2714. ((stringp org-use-property-inheritance)
  2715. (string-match org-use-property-inheritance property))
  2716. ((listp org-use-property-inheritance)
  2717. (member property org-use-property-inheritance))
  2718. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2719. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2720. "The default column format, if no other format has been defined.
  2721. This variable can be set on the per-file basis by inserting a line
  2722. #+COLUMNS: %25ITEM ....."
  2723. :group 'org-properties
  2724. :type 'string)
  2725. (defcustom org-columns-ellipses ".."
  2726. "The ellipses to be used when a field in column view is truncated.
  2727. When this is the empty string, as many characters as possible are shown,
  2728. but then there will be no visual indication that the field has been truncated.
  2729. When this is a string of length N, the last N characters of a truncated
  2730. field are replaced by this string. If the column is narrower than the
  2731. ellipses string, only part of the ellipses string will be shown."
  2732. :group 'org-properties
  2733. :type 'string)
  2734. (defcustom org-columns-modify-value-for-display-function nil
  2735. "Function that modifies values for display in column view.
  2736. For example, it can be used to cut out a certain part from a time stamp.
  2737. The function must take 2 arguments:
  2738. column-title The title of the column (*not* the property name)
  2739. value The value that should be modified.
  2740. The function should return the value that should be displayed,
  2741. or nil if the normal value should be used."
  2742. :group 'org-properties
  2743. :type 'function)
  2744. (defcustom org-effort-property "Effort"
  2745. "The property that is being used to keep track of effort estimates.
  2746. Effort estimates given in this property need to have the format H:MM."
  2747. :group 'org-properties
  2748. :group 'org-progress
  2749. :type '(string :tag "Property"))
  2750. (defconst org-global-properties-fixed
  2751. '(("VISIBILITY_ALL" . "folded children content all")
  2752. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2753. "List of property/value pairs that can be inherited by any entry.
  2754. These are fixed values, for the preset properties. The user variable
  2755. that can be used to add to this list is `org-global-properties'.
  2756. The entries in this list are cons cells where the car is a property
  2757. name and cdr is a string with the value. If the value represents
  2758. multiple items like an \"_ALL\" property, separate the items by
  2759. spaces.")
  2760. (defcustom org-global-properties nil
  2761. "List of property/value pairs that can be inherited by any entry.
  2762. This list will be combined with the constant `org-global-properties-fixed'.
  2763. The entries in this list are cons cells where the car is a property
  2764. name and cdr is a string with the value.
  2765. You can set buffer-local values for the same purpose in the variable
  2766. `org-file-properties' this by adding lines like
  2767. #+PROPERTY: NAME VALUE"
  2768. :group 'org-properties
  2769. :type '(repeat
  2770. (cons (string :tag "Property")
  2771. (string :tag "Value"))))
  2772. (defvar org-file-properties nil
  2773. "List of property/value pairs that can be inherited by any entry.
  2774. Valid for the current buffer.
  2775. This variable is populated from #+PROPERTY lines.")
  2776. (make-variable-buffer-local 'org-file-properties)
  2777. (defgroup org-agenda nil
  2778. "Options concerning agenda views in Org-mode."
  2779. :tag "Org Agenda"
  2780. :group 'org)
  2781. (defvar org-category nil
  2782. "Variable used by org files to set a category for agenda display.
  2783. Such files should use a file variable to set it, for example
  2784. # -*- mode: org; org-category: \"ELisp\"
  2785. or contain a special line
  2786. #+CATEGORY: ELisp
  2787. If the file does not specify a category, then file's base name
  2788. is used instead.")
  2789. (make-variable-buffer-local 'org-category)
  2790. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2791. (defcustom org-agenda-files nil
  2792. "The files to be used for agenda display.
  2793. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2794. \\[org-remove-file]. You can also use customize to edit the list.
  2795. If an entry is a directory, all files in that directory that are matched by
  2796. `org-agenda-file-regexp' will be part of the file list.
  2797. If the value of the variable is not a list but a single file name, then
  2798. the list of agenda files is actually stored and maintained in that file, one
  2799. agenda file per line. In this file paths can be given relative to
  2800. `org-directory'. Tilde expansion and environment variable substitution
  2801. are also made."
  2802. :group 'org-agenda
  2803. :type '(choice
  2804. (repeat :tag "List of files and directories" file)
  2805. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2806. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2807. "Regular expression to match files for `org-agenda-files'.
  2808. If any element in the list in that variable contains a directory instead
  2809. of a normal file, all files in that directory that are matched by this
  2810. regular expression will be included."
  2811. :group 'org-agenda
  2812. :type 'regexp)
  2813. (defcustom org-agenda-text-search-extra-files nil
  2814. "List of extra files to be searched by text search commands.
  2815. These files will be search in addition to the agenda files by the
  2816. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2817. Note that these files will only be searched for text search commands,
  2818. not for the other agenda views like todo lists, tag searches or the weekly
  2819. agenda. This variable is intended to list notes and possibly archive files
  2820. that should also be searched by these two commands.
  2821. In fact, if the first element in the list is the symbol `agenda-archives',
  2822. than all archive files of all agenda files will be added to the search
  2823. scope."
  2824. :group 'org-agenda
  2825. :type '(set :greedy t
  2826. (const :tag "Agenda Archives" agenda-archives)
  2827. (repeat :inline t (file))))
  2828. (if (fboundp 'defvaralias)
  2829. (defvaralias 'org-agenda-multi-occur-extra-files
  2830. 'org-agenda-text-search-extra-files))
  2831. (defcustom org-agenda-skip-unavailable-files nil
  2832. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2833. A nil value means to remove them, after a query, from the list."
  2834. :group 'org-agenda
  2835. :type 'boolean)
  2836. (defcustom org-calendar-to-agenda-key [?c]
  2837. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2838. The command `org-calendar-goto-agenda' will be bound to this key. The
  2839. default is the character `c' because then `c' can be used to switch back and
  2840. forth between agenda and calendar."
  2841. :group 'org-agenda
  2842. :type 'sexp)
  2843. (defcustom org-calendar-agenda-action-key [?k]
  2844. "The key to be installed in `calendar-mode-map' for agenda-action.
  2845. The command `org-agenda-action' will be bound to this key. The
  2846. default is the character `k' because we use the same key in the agenda."
  2847. :group 'org-agenda
  2848. :type 'sexp)
  2849. (defcustom org-calendar-insert-diary-entry-key [?i]
  2850. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2851. This option is irrelevant until `org-agenda-diary-file' has been configured
  2852. to point to an Org-mode file. When that is the case, the command
  2853. `org-agenda-diary-entry' will be bound to the key given here, by default
  2854. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2855. if you want to continue doing this, you need to change this to a different
  2856. key."
  2857. :group 'org-agenda
  2858. :type 'sexp)
  2859. (defcustom org-agenda-diary-file 'diary-file
  2860. "File to which to add new entries with the `i' key in agenda and calendar.
  2861. When this is the symbol `diary-file', the functionality in the Emacs
  2862. calendar will be used to add entries to the `diary-file'. But when this
  2863. points to a file, `org-agenda-diary-entry' will be used instead."
  2864. :group 'org-agenda
  2865. :type '(choice
  2866. (const :tag "The standard Emacs diary file" diary-file)
  2867. (file :tag "Special Org file diary entries")))
  2868. (eval-after-load "calendar"
  2869. '(progn
  2870. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2871. 'org-calendar-goto-agenda)
  2872. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2873. 'org-agenda-action)
  2874. (add-hook 'calendar-mode-hook
  2875. (lambda ()
  2876. (unless (eq org-agenda-diary-file 'diary-file)
  2877. (define-key calendar-mode-map
  2878. org-calendar-insert-diary-entry-key
  2879. 'org-agenda-diary-entry))))))
  2880. (defgroup org-latex nil
  2881. "Options for embedding LaTeX code into Org-mode."
  2882. :tag "Org LaTeX"
  2883. :group 'org)
  2884. (defcustom org-format-latex-options
  2885. '(:foreground default :background default :scale 1.0
  2886. :html-foreground "Black" :html-background "Transparent"
  2887. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2888. "Options for creating images from LaTeX fragments.
  2889. This is a property list with the following properties:
  2890. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2891. `default' means use the foreground of the default face.
  2892. `auto' means use the foreground from the text face.
  2893. :background the background color, or \"Transparent\".
  2894. `default' means use the background of the default face.
  2895. `auto' means use the background from the text face.
  2896. :scale a scaling factor for the size of the images, to get more pixels
  2897. :html-foreground, :html-background, :html-scale
  2898. the same numbers for HTML export.
  2899. :matchers a list indicating which matchers should be used to
  2900. find LaTeX fragments. Valid members of this list are:
  2901. \"begin\" find environments
  2902. \"$1\" find single characters surrounded by $.$
  2903. \"$\" find math expressions surrounded by $...$
  2904. \"$$\" find math expressions surrounded by $$....$$
  2905. \"\\(\" find math expressions surrounded by \\(...\\)
  2906. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2907. :group 'org-latex
  2908. :type 'plist)
  2909. (defcustom org-format-latex-signal-error t
  2910. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2911. When nil, just push out a message."
  2912. :group 'org-latex
  2913. :version "24.1"
  2914. :type 'boolean)
  2915. (defcustom org-latex-to-mathml-jar-file nil
  2916. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2917. Use this to specify additional executable file say a jar file.
  2918. When using MathToWeb as the converter, specify the full-path to
  2919. your mathtoweb.jar file."
  2920. :group 'org-latex
  2921. :version "24.1"
  2922. :type '(choice
  2923. (const :tag "None" nil)
  2924. (file :tag "JAR file" :must-match t)))
  2925. (defcustom org-latex-to-mathml-convert-command nil
  2926. "Command to convert LaTeX fragments to MathML.
  2927. Replace format-specifiers in the command as noted below and use
  2928. `shell-command' to convert LaTeX to MathML.
  2929. %j: Executable file in fully expanded form as specified by
  2930. `org-latex-to-mathml-jar-file'.
  2931. %I: Input LaTeX file in fully expanded form
  2932. %o: Output MathML file
  2933. This command is used by `org-create-math-formula'.
  2934. When using MathToWeb as the converter, set this to
  2935. \"java -jar %j -unicode -force -df %o %I\"."
  2936. :group 'org-latex
  2937. :version "24.1"
  2938. :type '(choice
  2939. (const :tag "None" nil)
  2940. (string :tag "\nShell command")))
  2941. (defcustom org-latex-create-formula-image-program 'dvipng
  2942. "Program to convert LaTeX fragments with.
  2943. dvipng Process the LaTeX fragments to dvi file, then convert
  2944. dvi files to png files using dvipng.
  2945. This will also include processing of non-math environments.
  2946. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  2947. to convert pdf files to png files"
  2948. :group 'org-latex
  2949. :version "24.1"
  2950. :type '(choice
  2951. (const :tag "dvipng" dvipng)
  2952. (const :tag "imagemagick" imagemagick)))
  2953. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  2954. "Path to store latex preview images. A relative path here creates many
  2955. directories relative to the processed org files paths. An absolute path
  2956. puts all preview images at the same place."
  2957. :group 'org-latex
  2958. ;; :version "24.3"
  2959. :type 'string)
  2960. (defun org-format-latex-mathml-available-p ()
  2961. "Return t if `org-latex-to-mathml-convert-command' is usable."
  2962. (save-match-data
  2963. (when (and (boundp 'org-latex-to-mathml-convert-command)
  2964. org-latex-to-mathml-convert-command)
  2965. (let ((executable (car (split-string
  2966. org-latex-to-mathml-convert-command))))
  2967. (when (executable-find executable)
  2968. (if (string-match
  2969. "%j" org-latex-to-mathml-convert-command)
  2970. (file-readable-p org-latex-to-mathml-jar-file)
  2971. t))))))
  2972. (defcustom org-format-latex-header "\\documentclass{article}
  2973. \\usepackage[usenames]{color}
  2974. \\usepackage{amsmath}
  2975. \\usepackage[mathscr]{eucal}
  2976. \\pagestyle{empty} % do not remove
  2977. \[PACKAGES]
  2978. \[DEFAULT-PACKAGES]
  2979. % The settings below are copied from fullpage.sty
  2980. \\setlength{\\textwidth}{\\paperwidth}
  2981. \\addtolength{\\textwidth}{-3cm}
  2982. \\setlength{\\oddsidemargin}{1.5cm}
  2983. \\addtolength{\\oddsidemargin}{-2.54cm}
  2984. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2985. \\setlength{\\textheight}{\\paperheight}
  2986. \\addtolength{\\textheight}{-\\headheight}
  2987. \\addtolength{\\textheight}{-\\headsep}
  2988. \\addtolength{\\textheight}{-\\footskip}
  2989. \\addtolength{\\textheight}{-3cm}
  2990. \\setlength{\\topmargin}{1.5cm}
  2991. \\addtolength{\\topmargin}{-2.54cm}"
  2992. "The document header used for processing LaTeX fragments.
  2993. It is imperative that this header make sure that no page number
  2994. appears on the page. The package defined in the variables
  2995. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2996. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2997. will be appended."
  2998. :group 'org-latex
  2999. :type 'string)
  3000. (defvar org-format-latex-header-extra nil)
  3001. (defun org-set-packages-alist (var val)
  3002. "Set the packages alist and make sure it has 3 elements per entry."
  3003. (set var (mapcar (lambda (x)
  3004. (if (and (consp x) (= (length x) 2))
  3005. (list (car x) (nth 1 x) t)
  3006. x))
  3007. val)))
  3008. (defun org-get-packages-alist (var)
  3009. "Get the packages alist and make sure it has 3 elements per entry."
  3010. (mapcar (lambda (x)
  3011. (if (and (consp x) (= (length x) 2))
  3012. (list (car x) (nth 1 x) t)
  3013. x))
  3014. (default-value var)))
  3015. ;; The following variables are defined here because is it also used
  3016. ;; when formatting latex fragments. Originally it was part of the
  3017. ;; LaTeX exporter, which is why the name includes "export".
  3018. (defcustom org-export-latex-default-packages-alist
  3019. '(("AUTO" "inputenc" t)
  3020. ("T1" "fontenc" t)
  3021. ("" "fixltx2e" nil)
  3022. ("" "graphicx" t)
  3023. ("" "longtable" nil)
  3024. ("" "float" nil)
  3025. ("" "wrapfig" nil)
  3026. ("" "soul" t)
  3027. ("" "textcomp" t)
  3028. ("" "marvosym" t)
  3029. ("" "wasysym" t)
  3030. ("" "latexsym" t)
  3031. ("" "amssymb" t)
  3032. ("" "hyperref" nil)
  3033. "\\tolerance=1000"
  3034. )
  3035. "Alist of default packages to be inserted in the header.
  3036. Change this only if one of the packages here causes an incompatibility
  3037. with another package you are using.
  3038. The packages in this list are needed by one part or another of Org-mode
  3039. to function properly.
  3040. - inputenc, fontenc: for basic font and character selection
  3041. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  3042. for interpreting the entities in `org-entities'. You can skip some of these
  3043. packages if you don't use any of the symbols in it.
  3044. - graphicx: for including images
  3045. - float, wrapfig: for figure placement
  3046. - longtable: for long tables
  3047. - hyperref: for cross references
  3048. Therefore you should not modify this variable unless you know what you
  3049. are doing. The one reason to change it anyway is that you might be loading
  3050. some other package that conflicts with one of the default packages.
  3051. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3052. If SNIPPET-FLAG is t, the package also needs to be included when
  3053. compiling LaTeX snippets into images for inclusion into HTML."
  3054. :group 'org-export-latex
  3055. :set 'org-set-packages-alist
  3056. :get 'org-get-packages-alist
  3057. :version "24.1"
  3058. :type '(repeat
  3059. (choice
  3060. (list :tag "options/package pair"
  3061. (string :tag "options")
  3062. (string :tag "package")
  3063. (boolean :tag "Snippet"))
  3064. (string :tag "A line of LaTeX"))))
  3065. (defcustom org-export-latex-packages-alist nil
  3066. "Alist of packages to be inserted in every LaTeX header.
  3067. These will be inserted after `org-export-latex-default-packages-alist'.
  3068. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3069. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3070. turning LaTeX snippets into images for inclusion into HTML.
  3071. Make sure that you only list packages here which:
  3072. - you want in every file
  3073. - do not conflict with the default packages in
  3074. `org-export-latex-default-packages-alist'
  3075. - do not conflict with the setup in `org-format-latex-header'."
  3076. :group 'org-export-latex
  3077. :set 'org-set-packages-alist
  3078. :get 'org-get-packages-alist
  3079. :type '(repeat
  3080. (choice
  3081. (list :tag "options/package pair"
  3082. (string :tag "options")
  3083. (string :tag "package")
  3084. (boolean :tag "Snippet"))
  3085. (string :tag "A line of LaTeX"))))
  3086. (defgroup org-appearance nil
  3087. "Settings for Org-mode appearance."
  3088. :tag "Org Appearance"
  3089. :group 'org)
  3090. (defcustom org-level-color-stars-only nil
  3091. "Non-nil means fontify only the stars in each headline.
  3092. When nil, the entire headline is fontified.
  3093. Changing it requires restart of `font-lock-mode' to become effective
  3094. also in regions already fontified."
  3095. :group 'org-appearance
  3096. :type 'boolean)
  3097. (defcustom org-hide-leading-stars nil
  3098. "Non-nil means hide the first N-1 stars in a headline.
  3099. This works by using the face `org-hide' for these stars. This
  3100. face is white for a light background, and black for a dark
  3101. background. You may have to customize the face `org-hide' to
  3102. make this work.
  3103. Changing it requires restart of `font-lock-mode' to become effective
  3104. also in regions already fontified.
  3105. You may also set this on a per-file basis by adding one of the following
  3106. lines to the buffer:
  3107. #+STARTUP: hidestars
  3108. #+STARTUP: showstars"
  3109. :group 'org-appearance
  3110. :type 'boolean)
  3111. (defcustom org-hidden-keywords nil
  3112. "List of symbols corresponding to keywords to be hidden the org buffer.
  3113. For example, a value '(title) for this list will make the document's title
  3114. appear in the buffer without the initial #+TITLE: keyword."
  3115. :group 'org-appearance
  3116. :version "24.1"
  3117. :type '(set (const :tag "#+AUTHOR" author)
  3118. (const :tag "#+DATE" date)
  3119. (const :tag "#+EMAIL" email)
  3120. (const :tag "#+TITLE" title)))
  3121. (defcustom org-custom-properties nil
  3122. "List of properties (as strings) with a special meaning.
  3123. The default use of these custom properties is to let the user
  3124. hide them with `org-toggle-custom-properties-visibility'."
  3125. :group 'org-properties
  3126. :group 'org-appearance
  3127. ;; :version "24.3"
  3128. :type '(repeat (string :tag "Property Name")))
  3129. (defcustom org-fontify-done-headline nil
  3130. "Non-nil means change the face of a headline if it is marked DONE.
  3131. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3132. When this is non-nil, the headline after the keyword is set to the
  3133. `org-headline-done' as an additional indication."
  3134. :group 'org-appearance
  3135. :type 'boolean)
  3136. (defcustom org-fontify-emphasized-text t
  3137. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3138. Changing this variable requires a restart of Emacs to take effect."
  3139. :group 'org-appearance
  3140. :type 'boolean)
  3141. (defcustom org-fontify-whole-heading-line nil
  3142. "Non-nil means fontify the whole line for headings.
  3143. This is useful when setting a background color for the
  3144. org-level-* faces."
  3145. :group 'org-appearance
  3146. :type 'boolean)
  3147. (defcustom org-highlight-latex-fragments-and-specials nil
  3148. "Non-nil means fontify what is treated specially by the exporters."
  3149. :group 'org-appearance
  3150. :type 'boolean)
  3151. (defcustom org-hide-emphasis-markers nil
  3152. "Non-nil mean font-lock should hide the emphasis marker characters."
  3153. :group 'org-appearance
  3154. :type 'boolean)
  3155. (defcustom org-pretty-entities nil
  3156. "Non-nil means show entities as UTF8 characters.
  3157. When nil, the \\name form remains in the buffer."
  3158. :group 'org-appearance
  3159. :version "24.1"
  3160. :type 'boolean)
  3161. (defcustom org-pretty-entities-include-sub-superscripts t
  3162. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3163. :group 'org-appearance
  3164. :version "24.1"
  3165. :type 'boolean)
  3166. (defvar org-emph-re nil
  3167. "Regular expression for matching emphasis.
  3168. After a match, the match groups contain these elements:
  3169. 0 The match of the full regular expression, including the characters
  3170. before and after the proper match
  3171. 1 The character before the proper match, or empty at beginning of line
  3172. 2 The proper match, including the leading and trailing markers
  3173. 3 The leading marker like * or /, indicating the type of highlighting
  3174. 4 The text between the emphasis markers, not including the markers
  3175. 5 The character after the match, empty at the end of a line")
  3176. (defvar org-verbatim-re nil
  3177. "Regular expression for matching verbatim text.")
  3178. (defvar org-emphasis-regexp-components) ; defined just below
  3179. (defvar org-emphasis-alist) ; defined just below
  3180. (defun org-set-emph-re (var val)
  3181. "Set variable and compute the emphasis regular expression."
  3182. (set var val)
  3183. (when (and (boundp 'org-emphasis-alist)
  3184. (boundp 'org-emphasis-regexp-components)
  3185. org-emphasis-alist org-emphasis-regexp-components)
  3186. (let* ((e org-emphasis-regexp-components)
  3187. (pre (car e))
  3188. (post (nth 1 e))
  3189. (border (nth 2 e))
  3190. (body (nth 3 e))
  3191. (nl (nth 4 e))
  3192. (body1 (concat body "*?"))
  3193. (markers (mapconcat 'car org-emphasis-alist ""))
  3194. (vmarkers (mapconcat
  3195. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3196. org-emphasis-alist "")))
  3197. ;; make sure special characters appear at the right position in the class
  3198. (if (string-match "\\^" markers)
  3199. (setq markers (concat (replace-match "" t t markers) "^")))
  3200. (if (string-match "-" markers)
  3201. (setq markers (concat (replace-match "" t t markers) "-")))
  3202. (if (string-match "\\^" vmarkers)
  3203. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3204. (if (string-match "-" vmarkers)
  3205. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3206. (if (> nl 0)
  3207. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3208. (int-to-string nl) "\\}")))
  3209. ;; Make the regexp
  3210. (setq org-emph-re
  3211. (concat "\\([" pre "]\\|^\\)"
  3212. "\\("
  3213. "\\([" markers "]\\)"
  3214. "\\("
  3215. "[^" border "]\\|"
  3216. "[^" border "]"
  3217. body1
  3218. "[^" border "]"
  3219. "\\)"
  3220. "\\3\\)"
  3221. "\\([" post "]\\|$\\)"))
  3222. (setq org-verbatim-re
  3223. (concat "\\([" pre "]\\|^\\)"
  3224. "\\("
  3225. "\\([" vmarkers "]\\)"
  3226. "\\("
  3227. "[^" border "]\\|"
  3228. "[^" border "]"
  3229. body1
  3230. "[^" border "]"
  3231. "\\)"
  3232. "\\3\\)"
  3233. "\\([" post "]\\|$\\)")))))
  3234. (defcustom org-emphasis-regexp-components
  3235. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3236. "Components used to build the regular expression for emphasis.
  3237. This is a list with five entries. Terminology: In an emphasis string
  3238. like \" *strong word* \", we call the initial space PREMATCH, the final
  3239. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3240. and \"trong wor\" is the body. The different components in this variable
  3241. specify what is allowed/forbidden in each part:
  3242. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3243. post Chars allowed as postmatch. End of line will be allowed too.
  3244. border The chars *forbidden* as border characters.
  3245. body-regexp A regexp like \".\" to match a body character. Don't use
  3246. non-shy groups here, and don't allow newline here.
  3247. newline The maximum number of newlines allowed in an emphasis exp.
  3248. Use customize to modify this, or restart Emacs after changing it."
  3249. :group 'org-appearance
  3250. :set 'org-set-emph-re
  3251. :type '(list
  3252. (sexp :tag "Allowed chars in pre ")
  3253. (sexp :tag "Allowed chars in post ")
  3254. (sexp :tag "Forbidden chars in border ")
  3255. (sexp :tag "Regexp for body ")
  3256. (integer :tag "number of newlines allowed")
  3257. (option (boolean :tag "Please ignore this button"))))
  3258. (defcustom org-emphasis-alist
  3259. `(("*" bold "<b>" "</b>")
  3260. ("/" italic "<i>" "</i>")
  3261. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3262. ("=" org-code "<code>" "</code>" verbatim)
  3263. ("~" org-verbatim "<code>" "</code>" verbatim)
  3264. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3265. "<del>" "</del>")
  3266. )
  3267. "Special syntax for emphasized text.
  3268. Text starting and ending with a special character will be emphasized, for
  3269. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3270. characters, the face to be used by font-lock for highlighting in Org-mode
  3271. Emacs buffers, and the HTML tags to be used for this.
  3272. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3273. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3274. Use customize to modify this, or restart Emacs after changing it."
  3275. :group 'org-appearance
  3276. :set 'org-set-emph-re
  3277. :type '(repeat
  3278. (list
  3279. (string :tag "Marker character")
  3280. (choice
  3281. (face :tag "Font-lock-face")
  3282. (plist :tag "Face property list"))
  3283. (string :tag "HTML start tag")
  3284. (string :tag "HTML end tag")
  3285. (option (const verbatim)))))
  3286. (defvar org-protecting-blocks
  3287. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3288. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3289. This is needed for font-lock setup.")
  3290. ;;; Miscellaneous options
  3291. (defgroup org-completion nil
  3292. "Completion in Org-mode."
  3293. :tag "Org Completion"
  3294. :group 'org)
  3295. (defcustom org-completion-use-ido nil
  3296. "Non-nil means use ido completion wherever possible.
  3297. Note that `ido-mode' must be active for this variable to be relevant.
  3298. If you decide to turn this variable on, you might well want to turn off
  3299. `org-outline-path-complete-in-steps'.
  3300. See also `org-completion-use-iswitchb'."
  3301. :group 'org-completion
  3302. :type 'boolean)
  3303. (defcustom org-completion-use-iswitchb nil
  3304. "Non-nil means use iswitchb completion wherever possible.
  3305. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3306. If you decide to turn this variable on, you might well want to turn off
  3307. `org-outline-path-complete-in-steps'.
  3308. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3309. :group 'org-completion
  3310. :type 'boolean)
  3311. (defcustom org-completion-fallback-command 'hippie-expand
  3312. "The expansion command called by \\[pcomplete] in normal context.
  3313. Normal means, no org-mode-specific context."
  3314. :group 'org-completion
  3315. :type 'function)
  3316. ;;; Functions and variables from their packages
  3317. ;; Declared here to avoid compiler warnings
  3318. ;; XEmacs only
  3319. (defvar outline-mode-menu-heading)
  3320. (defvar outline-mode-menu-show)
  3321. (defvar outline-mode-menu-hide)
  3322. (defvar zmacs-regions) ; XEmacs regions
  3323. ;; Emacs only
  3324. (defvar mark-active)
  3325. ;; Various packages
  3326. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3327. (declare-function calendar-forward-day "cal-move" (arg))
  3328. (declare-function calendar-goto-date "cal-move" (date))
  3329. (declare-function calendar-goto-today "cal-move" ())
  3330. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3331. (defvar calc-embedded-close-formula)
  3332. (defvar calc-embedded-open-formula)
  3333. (declare-function cdlatex-tab "ext:cdlatex" ())
  3334. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3335. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3336. (defvar font-lock-unfontify-region-function)
  3337. (declare-function iswitchb-read-buffer "iswitchb"
  3338. (prompt &optional default require-match start matches-set))
  3339. (defvar iswitchb-temp-buflist)
  3340. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3341. (defvar org-agenda-tags-todo-honor-ignore-options)
  3342. (declare-function org-agenda-skip "org-agenda" ())
  3343. (declare-function
  3344. org-agenda-format-item "org-agenda"
  3345. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3346. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3347. (declare-function org-agenda-change-all-lines "org-agenda"
  3348. (newhead hdmarker &optional fixface just-this))
  3349. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3350. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3351. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3352. (beg end))
  3353. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3354. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3355. "org-agenda" (&optional end))
  3356. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3357. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3358. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3359. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3360. (declare-function org-indent-mode "org-indent" (&optional arg))
  3361. (declare-function parse-time-string "parse-time" (string))
  3362. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3363. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3364. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3365. (defvar remember-data-file)
  3366. (defvar texmathp-why)
  3367. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3368. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3369. (defvar w3m-current-url)
  3370. (defvar w3m-current-title)
  3371. (defvar org-latex-regexps)
  3372. ;;; Autoload and prepare some org modules
  3373. ;; Some table stuff that needs to be defined here, because it is used
  3374. ;; by the functions setting up org-mode or checking for table context.
  3375. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3376. "Detect an org-type or table-type table.")
  3377. (defconst org-table-line-regexp "^[ \t]*|"
  3378. "Detect an org-type table line.")
  3379. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3380. "Detect an org-type table line.")
  3381. (defconst org-table-hline-regexp "^[ \t]*|-"
  3382. "Detect an org-type table hline.")
  3383. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3384. "Detect a table-type table hline.")
  3385. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3386. "Detect the first line outside a table when searching from within it.
  3387. This works for both table types.")
  3388. ;; Autoload the functions in org-table.el that are needed by functions here.
  3389. (eval-and-compile
  3390. (org-autoload "org-table"
  3391. '(org-table-align org-table-begin org-table-blank-field
  3392. org-table-convert org-table-convert-region org-table-copy-down
  3393. org-table-copy-region org-table-create
  3394. org-table-create-or-convert-from-region
  3395. org-table-create-with-table.el org-table-current-dline
  3396. org-table-cut-region org-table-delete-column org-table-edit-field
  3397. org-table-edit-formulas org-table-end org-table-eval-formula
  3398. org-table-export org-table-field-info
  3399. org-table-get-stored-formulas org-table-goto-column
  3400. org-table-hline-and-move org-table-import org-table-insert-column
  3401. org-table-insert-hline org-table-insert-row org-table-iterate
  3402. org-table-justify-field-maybe org-table-kill-row
  3403. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3404. org-table-move-column org-table-move-column-left
  3405. org-table-move-column-right org-table-move-row
  3406. org-table-move-row-down org-table-move-row-up
  3407. org-table-next-field org-table-next-row org-table-paste-rectangle
  3408. org-table-previous-field org-table-recalculate
  3409. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3410. org-table-toggle-coordinate-overlays
  3411. org-table-toggle-formula-debugger org-table-wrap-region
  3412. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3413. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3414. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3415. (defun org-at-table-p (&optional table-type)
  3416. "Return t if the cursor is inside an org-type table.
  3417. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3418. (if org-enable-table-editor
  3419. (save-excursion
  3420. (beginning-of-line 1)
  3421. (looking-at (if table-type org-table-any-line-regexp
  3422. org-table-line-regexp)))
  3423. nil))
  3424. (defsubst org-table-p () (org-at-table-p))
  3425. (defun org-at-table.el-p ()
  3426. "Return t if and only if we are at a table.el table."
  3427. (and (org-at-table-p 'any)
  3428. (save-excursion
  3429. (goto-char (org-table-begin 'any))
  3430. (looking-at org-table1-hline-regexp))))
  3431. (defun org-table-recognize-table.el ()
  3432. "If there is a table.el table nearby, recognize it and move into it."
  3433. (if org-table-tab-recognizes-table.el
  3434. (if (org-at-table.el-p)
  3435. (progn
  3436. (beginning-of-line 1)
  3437. (if (looking-at org-table-dataline-regexp)
  3438. nil
  3439. (if (looking-at org-table1-hline-regexp)
  3440. (progn
  3441. (beginning-of-line 2)
  3442. (if (looking-at org-table-any-border-regexp)
  3443. (beginning-of-line -1)))))
  3444. (if (re-search-forward "|" (org-table-end t) t)
  3445. (progn
  3446. (require 'table)
  3447. (if (table--at-cell-p (point))
  3448. t
  3449. (message "recognizing table.el table...")
  3450. (table-recognize-table)
  3451. (message "recognizing table.el table...done")))
  3452. (error "This should not happen"))
  3453. t)
  3454. nil)
  3455. nil))
  3456. (defun org-at-table-hline-p ()
  3457. "Return t if the cursor is inside a hline in a table."
  3458. (if org-enable-table-editor
  3459. (save-excursion
  3460. (beginning-of-line 1)
  3461. (looking-at org-table-hline-regexp))
  3462. nil))
  3463. (defvar org-table-clean-did-remove-column nil)
  3464. (defun org-table-map-tables (function &optional quietly)
  3465. "Apply FUNCTION to the start of all tables in the buffer."
  3466. (save-excursion
  3467. (save-restriction
  3468. (widen)
  3469. (goto-char (point-min))
  3470. (while (re-search-forward org-table-any-line-regexp nil t)
  3471. (unless quietly
  3472. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3473. (beginning-of-line 1)
  3474. (when (and (looking-at org-table-line-regexp)
  3475. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3476. (not (org-in-block-p '("src" "example"))))
  3477. (save-excursion (funcall function))
  3478. (or (looking-at org-table-line-regexp)
  3479. (forward-char 1)))
  3480. (re-search-forward org-table-any-border-regexp nil 1))))
  3481. (unless quietly (message "Mapping tables: done")))
  3482. ;; Declare and autoload functions from org-exp.el & Co
  3483. (declare-function org-default-export-plist "org-exp")
  3484. (declare-function org-infile-export-plist "org-exp")
  3485. (declare-function org-get-current-options "org-exp")
  3486. (eval-and-compile
  3487. (org-autoload "org-exp"
  3488. '(org-export org-export-visible
  3489. org-insert-export-options-template
  3490. org-table-clean-before-export))
  3491. (org-autoload "org-ascii"
  3492. '(org-export-as-ascii org-export-ascii-preprocess
  3493. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3494. org-export-region-as-ascii))
  3495. (org-autoload "org-latex"
  3496. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3497. org-replace-region-by-latex org-export-region-as-latex
  3498. org-export-as-latex org-export-as-pdf
  3499. org-export-as-pdf-and-open))
  3500. (org-autoload "org-html"
  3501. '(org-export-as-html-and-open
  3502. org-export-as-html-batch org-export-as-html-to-buffer
  3503. org-replace-region-by-html org-export-region-as-html
  3504. org-export-as-html))
  3505. (org-autoload "org-docbook"
  3506. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3507. org-replace-region-by-docbook org-export-region-as-docbook
  3508. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3509. org-export-as-docbook))
  3510. (org-autoload "org-icalendar"
  3511. '(org-export-icalendar-this-file
  3512. org-export-icalendar-all-agenda-files
  3513. org-export-icalendar-combine-agenda-files))
  3514. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3515. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3516. ;; Declare and autoload functions from org-agenda.el
  3517. (eval-and-compile
  3518. (org-autoload "org-agenda"
  3519. '(org-agenda org-agenda-list org-search-view
  3520. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3521. org-diary org-agenda-to-appt
  3522. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3523. ;; Autoload org-remember
  3524. (eval-and-compile
  3525. (org-autoload "org-remember"
  3526. '(org-remember-insinuate org-remember-annotation
  3527. org-remember-apply-template org-remember org-remember-handler)))
  3528. (eval-and-compile
  3529. (org-autoload "org-capture"
  3530. '(org-capture org-capture-insert-template-here
  3531. org-capture-import-remember-templates)))
  3532. ;; Autoload org-clock.el
  3533. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3534. (beg end))
  3535. (declare-function org-clock-update-mode-line "org-clock" ())
  3536. (declare-function org-resolve-clocks "org-clock"
  3537. (&optional also-non-dangling-p prompt last-valid))
  3538. (defvar org-clock-start-time)
  3539. (defvar org-clock-marker (make-marker)
  3540. "Marker recording the last clock-in.")
  3541. (defvar org-clock-hd-marker (make-marker)
  3542. "Marker recording the last clock-in, but the headline position.")
  3543. (defvar org-clock-heading ""
  3544. "The heading of the current clock entry.")
  3545. (defun org-clock-is-active ()
  3546. "Return non-nil if clock is currently running.
  3547. The return value is actually the clock marker."
  3548. (marker-buffer org-clock-marker))
  3549. (eval-and-compile
  3550. (org-autoload
  3551. "org-clock"
  3552. '(org-clock-in org-clock-out org-clock-cancel
  3553. org-clock-goto org-clock-sum org-clock-display
  3554. org-clock-remove-overlays org-clock-report
  3555. org-clocktable-shift org-dblock-write:clocktable
  3556. org-get-clocktable org-resolve-clocks)))
  3557. (defun org-clock-update-time-maybe ()
  3558. "If this is a CLOCK line, update it and return t.
  3559. Otherwise, return nil."
  3560. (interactive)
  3561. (save-excursion
  3562. (beginning-of-line 1)
  3563. (skip-chars-forward " \t")
  3564. (when (looking-at org-clock-string)
  3565. (let ((re (concat "[ \t]*" org-clock-string
  3566. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3567. "\\([ \t]*=>.*\\)?\\)?"))
  3568. ts te h m s neg)
  3569. (cond
  3570. ((not (looking-at re))
  3571. nil)
  3572. ((not (match-end 2))
  3573. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3574. (> org-clock-marker (point))
  3575. (<= org-clock-marker (point-at-eol)))
  3576. ;; The clock is running here
  3577. (setq org-clock-start-time
  3578. (apply 'encode-time
  3579. (org-parse-time-string (match-string 1))))
  3580. (org-clock-update-mode-line)))
  3581. (t
  3582. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3583. (end-of-line 1)
  3584. (setq ts (match-string 1)
  3585. te (match-string 3))
  3586. (setq s (- (org-float-time
  3587. (apply 'encode-time (org-parse-time-string te)))
  3588. (org-float-time
  3589. (apply 'encode-time (org-parse-time-string ts))))
  3590. neg (< s 0)
  3591. s (abs s)
  3592. h (floor (/ s 3600))
  3593. s (- s (* 3600 h))
  3594. m (floor (/ s 60))
  3595. s (- s (* 60 s)))
  3596. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3597. t))))))
  3598. (defun org-check-running-clock ()
  3599. "Check if the current buffer contains the running clock.
  3600. If yes, offer to stop it and to save the buffer with the changes."
  3601. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3602. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3603. (buffer-name))))
  3604. (org-clock-out)
  3605. (when (y-or-n-p "Save changed buffer?")
  3606. (save-buffer))))
  3607. (defun org-clocktable-try-shift (dir n)
  3608. "Check if this line starts a clock table, if yes, shift the time block."
  3609. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3610. (org-clocktable-shift dir n)))
  3611. ;; Autoload org-timer.el
  3612. (eval-and-compile
  3613. (org-autoload
  3614. "org-timer"
  3615. '(org-timer-start org-timer org-timer-item
  3616. org-timer-change-times-in-region
  3617. org-timer-set-timer
  3618. org-timer-reset-timers
  3619. org-timer-show-remaining-time)))
  3620. ;; Autoload org-feed.el
  3621. (eval-and-compile
  3622. (org-autoload
  3623. "org-feed"
  3624. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3625. ;; Autoload org-indent.el
  3626. ;; Define the variable already here, to make sure we have it.
  3627. (defvar org-indent-mode nil
  3628. "Non-nil if Org-Indent mode is enabled.
  3629. Use the command `org-indent-mode' to change this variable.")
  3630. (eval-and-compile
  3631. (org-autoload
  3632. "org-indent"
  3633. '(org-indent-mode)))
  3634. ;; Autoload org-mobile.el
  3635. (eval-and-compile
  3636. (org-autoload
  3637. "org-mobile"
  3638. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3639. ;; Autoload archiving code
  3640. ;; The stuff that is needed for cycling and tags has to be defined here.
  3641. (defgroup org-archive nil
  3642. "Options concerning archiving in Org-mode."
  3643. :tag "Org Archive"
  3644. :group 'org-structure)
  3645. (defcustom org-archive-location "%s_archive::"
  3646. "The location where subtrees should be archived.
  3647. The value of this variable is a string, consisting of two parts,
  3648. separated by a double-colon. The first part is a filename and
  3649. the second part is a headline.
  3650. When the filename is omitted, archiving happens in the same file.
  3651. %s in the filename will be replaced by the current file
  3652. name (without the directory part). Archiving to a different file
  3653. is useful to keep archived entries from contributing to the
  3654. Org-mode Agenda.
  3655. The archived entries will be filed as subtrees of the specified
  3656. headline. When the headline is omitted, the subtrees are simply
  3657. filed away at the end of the file, as top-level entries. Also in
  3658. the heading you can use %s to represent the file name, this can be
  3659. useful when using the same archive for a number of different files.
  3660. Here are a few examples:
  3661. \"%s_archive::\"
  3662. If the current file is Projects.org, archive in file
  3663. Projects.org_archive, as top-level trees. This is the default.
  3664. \"::* Archived Tasks\"
  3665. Archive in the current file, under the top-level headline
  3666. \"* Archived Tasks\".
  3667. \"~/org/archive.org::\"
  3668. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3669. \"~/org/archive.org::* From %s\"
  3670. Archive in file ~/org/archive.org (absolute path), under headlines
  3671. \"From FILENAME\" where file name is the current file name.
  3672. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3673. The \"datetree/\" string is special, signifying to archive
  3674. items to the datetree. Items are placed in either the CLOSED
  3675. date of the item, or the current date if there is no CLOSED date.
  3676. The heading will be a subentry to the current date. There doesn't
  3677. need to be a heading, but there always needs to be a slash after
  3678. datetree. For example, to store archived items directly in the
  3679. datetree, use \"~/org/datetree.org::datetree/\".
  3680. \"basement::** Finished Tasks\"
  3681. Archive in file ./basement (relative path), as level 3 trees
  3682. below the level 2 heading \"** Finished Tasks\".
  3683. You may set this option on a per-file basis by adding to the buffer a
  3684. line like
  3685. #+ARCHIVE: basement::** Finished Tasks
  3686. You may also define it locally for a subtree by setting an ARCHIVE property
  3687. in the entry. If such a property is found in an entry, or anywhere up
  3688. the hierarchy, it will be used."
  3689. :group 'org-archive
  3690. :type 'string)
  3691. (defcustom org-archive-tag "ARCHIVE"
  3692. "The tag that marks a subtree as archived.
  3693. An archived subtree does not open during visibility cycling, and does
  3694. not contribute to the agenda listings.
  3695. After changing this, font-lock must be restarted in the relevant buffers to
  3696. get the proper fontification."
  3697. :group 'org-archive
  3698. :group 'org-keywords
  3699. :type 'string)
  3700. (defcustom org-agenda-skip-archived-trees t
  3701. "Non-nil means the agenda will skip any items located in archived trees.
  3702. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3703. variable is no longer recommended, you should leave it at the value t.
  3704. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3705. :group 'org-archive
  3706. :group 'org-agenda-skip
  3707. :type 'boolean)
  3708. (defcustom org-columns-skip-archived-trees t
  3709. "Non-nil means ignore archived trees when creating column view."
  3710. :group 'org-archive
  3711. :group 'org-properties
  3712. :type 'boolean)
  3713. (defcustom org-cycle-open-archived-trees nil
  3714. "Non-nil means `org-cycle' will open archived trees.
  3715. An archived tree is a tree marked with the tag ARCHIVE.
  3716. When nil, archived trees will stay folded. You can still open them with
  3717. normal outline commands like `show-all', but not with the cycling commands."
  3718. :group 'org-archive
  3719. :group 'org-cycle
  3720. :type 'boolean)
  3721. (defcustom org-sparse-tree-open-archived-trees nil
  3722. "Non-nil means sparse tree construction shows matches in archived trees.
  3723. When nil, matches in these trees are highlighted, but the trees are kept in
  3724. collapsed state."
  3725. :group 'org-archive
  3726. :group 'org-sparse-trees
  3727. :type 'boolean)
  3728. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3729. "The default date type when building a sparse tree.
  3730. When this is nil, a date is a scheduled or a deadline timestamp.
  3731. Otherwise, these types are allowed:
  3732. all: all timestamps
  3733. active: only active timestamps (<...>)
  3734. inactive: only inactive timestamps (<...)
  3735. scheduled: only scheduled timestamps
  3736. deadline: only deadline timestamps"
  3737. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3738. (const :tag "All timestamps" all)
  3739. (const :tag "Only active timestamps" active)
  3740. (const :tag "Only inactive timestamps" inactive)
  3741. (const :tag "Only scheduled timestamps" scheduled)
  3742. (const :tag "Only deadline timestamps" deadline))
  3743. ;; :version "24.3"
  3744. :group 'org-sparse-trees)
  3745. (defun org-cycle-hide-archived-subtrees (state)
  3746. "Re-hide all archived subtrees after a visibility state change."
  3747. (when (and (not org-cycle-open-archived-trees)
  3748. (not (memq state '(overview folded))))
  3749. (save-excursion
  3750. (let* ((globalp (memq state '(contents all)))
  3751. (beg (if globalp (point-min) (point)))
  3752. (end (if globalp (point-max) (org-end-of-subtree t))))
  3753. (org-hide-archived-subtrees beg end)
  3754. (goto-char beg)
  3755. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3756. (message "%s" (substitute-command-keys
  3757. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3758. (defun org-force-cycle-archived ()
  3759. "Cycle subtree even if it is archived."
  3760. (interactive)
  3761. (setq this-command 'org-cycle)
  3762. (let ((org-cycle-open-archived-trees t))
  3763. (call-interactively 'org-cycle)))
  3764. (defun org-hide-archived-subtrees (beg end)
  3765. "Re-hide all archived subtrees after a visibility state change."
  3766. (save-excursion
  3767. (let* ((re (concat ":" org-archive-tag ":")))
  3768. (goto-char beg)
  3769. (while (re-search-forward re end t)
  3770. (when (org-at-heading-p)
  3771. (org-flag-subtree t)
  3772. (org-end-of-subtree t))))))
  3773. (declare-function outline-end-of-heading "outline" ())
  3774. (declare-function outline-flag-region "outline" (from to flag))
  3775. (defun org-flag-subtree (flag)
  3776. (save-excursion
  3777. (org-back-to-heading t)
  3778. (outline-end-of-heading)
  3779. (outline-flag-region (point)
  3780. (progn (org-end-of-subtree t) (point))
  3781. flag)))
  3782. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3783. (eval-and-compile
  3784. (org-autoload "org-archive"
  3785. '(org-add-archive-files org-archive-subtree
  3786. org-archive-to-archive-sibling org-toggle-archive-tag
  3787. org-archive-subtree-default
  3788. org-archive-subtree-default-with-confirmation)))
  3789. ;; Autoload Column View Code
  3790. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3791. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3792. (declare-function org-columns-compute "org-colview" (property))
  3793. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3794. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3795. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3796. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3797. ;; Autoload ID code
  3798. (declare-function org-id-store-link "org-id")
  3799. (declare-function org-id-locations-load "org-id")
  3800. (declare-function org-id-locations-save "org-id")
  3801. (defvar org-id-track-globally)
  3802. (org-autoload "org-id"
  3803. '(org-id-get-create org-id-new org-id-copy org-id-get
  3804. org-id-get-with-outline-path-completion
  3805. org-id-get-with-outline-drilling org-id-store-link
  3806. org-id-goto org-id-find org-id-store-link))
  3807. ;; Autoload Plotting Code
  3808. (org-autoload "org-plot"
  3809. '(org-plot/gnuplot))
  3810. ;;; Variables for pre-computed regular expressions, all buffer local
  3811. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3812. "Matches first line of a hidden block.")
  3813. (make-variable-buffer-local 'org-drawer-regexp)
  3814. (defvar org-todo-regexp nil
  3815. "Matches any of the TODO state keywords.")
  3816. (make-variable-buffer-local 'org-todo-regexp)
  3817. (defvar org-not-done-regexp nil
  3818. "Matches any of the TODO state keywords except the last one.")
  3819. (make-variable-buffer-local 'org-not-done-regexp)
  3820. (defvar org-not-done-heading-regexp nil
  3821. "Matches a TODO headline that is not done.")
  3822. (make-variable-buffer-local 'org-not-done-regexp)
  3823. (defvar org-todo-line-regexp nil
  3824. "Matches a headline and puts TODO state into group 2 if present.")
  3825. (make-variable-buffer-local 'org-todo-line-regexp)
  3826. (defvar org-complex-heading-regexp nil
  3827. "Matches a headline and puts everything into groups:
  3828. group 1: the stars
  3829. group 2: The todo keyword, maybe
  3830. group 3: Priority cookie
  3831. group 4: True headline
  3832. group 5: Tags")
  3833. (make-variable-buffer-local 'org-complex-heading-regexp)
  3834. (defvar org-complex-heading-regexp-format nil
  3835. "Printf format to make regexp to match an exact headline.
  3836. This regexp will match the headline of any node which has the
  3837. exact headline text that is put into the format, but may have any
  3838. TODO state, priority and tags.")
  3839. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3840. (defvar org-todo-line-tags-regexp nil
  3841. "Matches a headline and puts TODO state into group 2 if present.
  3842. Also put tags into group 4 if tags are present.")
  3843. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3844. (defvar org-ds-keyword-length 12
  3845. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3846. (make-variable-buffer-local 'org-ds-keyword-length)
  3847. (defvar org-deadline-regexp nil
  3848. "Matches the DEADLINE keyword.")
  3849. (make-variable-buffer-local 'org-deadline-regexp)
  3850. (defvar org-deadline-time-regexp nil
  3851. "Matches the DEADLINE keyword together with a time stamp.")
  3852. (make-variable-buffer-local 'org-deadline-time-regexp)
  3853. (defvar org-deadline-line-regexp nil
  3854. "Matches the DEADLINE keyword and the rest of the line.")
  3855. (make-variable-buffer-local 'org-deadline-line-regexp)
  3856. (defvar org-scheduled-regexp nil
  3857. "Matches the SCHEDULED keyword.")
  3858. (make-variable-buffer-local 'org-scheduled-regexp)
  3859. (defvar org-scheduled-time-regexp nil
  3860. "Matches the SCHEDULED keyword together with a time stamp.")
  3861. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3862. (defvar org-closed-time-regexp nil
  3863. "Matches the CLOSED keyword together with a time stamp.")
  3864. (make-variable-buffer-local 'org-closed-time-regexp)
  3865. (defvar org-keyword-time-regexp nil
  3866. "Matches any of the 4 keywords, together with the time stamp.")
  3867. (make-variable-buffer-local 'org-keyword-time-regexp)
  3868. (defvar org-keyword-time-not-clock-regexp nil
  3869. "Matches any of the 3 keywords, together with the time stamp.")
  3870. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3871. (defvar org-maybe-keyword-time-regexp nil
  3872. "Matches a timestamp, possibly preceded by a keyword.")
  3873. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3874. (defvar org-all-time-keywords nil
  3875. "List of time keywords.")
  3876. (make-variable-buffer-local 'org-all-time-keywords)
  3877. (defconst org-plain-time-of-day-regexp
  3878. (concat
  3879. "\\(\\<[012]?[0-9]"
  3880. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3881. "\\(--?"
  3882. "\\(\\<[012]?[0-9]"
  3883. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3884. "\\)?")
  3885. "Regular expression to match a plain time or time range.
  3886. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3887. groups carry important information:
  3888. 0 the full match
  3889. 1 the first time, range or not
  3890. 8 the second time, if it is a range.")
  3891. (defconst org-plain-time-extension-regexp
  3892. (concat
  3893. "\\(\\<[012]?[0-9]"
  3894. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3895. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3896. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3897. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3898. groups carry important information:
  3899. 0 the full match
  3900. 7 hours of duration
  3901. 9 minutes of duration")
  3902. (defconst org-stamp-time-of-day-regexp
  3903. (concat
  3904. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3905. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3906. "\\(--?"
  3907. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3908. "Regular expression to match a timestamp time or time range.
  3909. After a match, the following groups carry important information:
  3910. 0 the full match
  3911. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3912. 2 the first time, range or not
  3913. 4 the second time, if it is a range.")
  3914. (defconst org-startup-options
  3915. '(("fold" org-startup-folded t)
  3916. ("overview" org-startup-folded t)
  3917. ("nofold" org-startup-folded nil)
  3918. ("showall" org-startup-folded nil)
  3919. ("showeverything" org-startup-folded showeverything)
  3920. ("content" org-startup-folded content)
  3921. ("indent" org-startup-indented t)
  3922. ("noindent" org-startup-indented nil)
  3923. ("hidestars" org-hide-leading-stars t)
  3924. ("showstars" org-hide-leading-stars nil)
  3925. ("odd" org-odd-levels-only t)
  3926. ("oddeven" org-odd-levels-only nil)
  3927. ("align" org-startup-align-all-tables t)
  3928. ("noalign" org-startup-align-all-tables nil)
  3929. ("inlineimages" org-startup-with-inline-images t)
  3930. ("noinlineimages" org-startup-with-inline-images nil)
  3931. ("customtime" org-display-custom-times t)
  3932. ("logdone" org-log-done time)
  3933. ("lognotedone" org-log-done note)
  3934. ("nologdone" org-log-done nil)
  3935. ("lognoteclock-out" org-log-note-clock-out t)
  3936. ("nolognoteclock-out" org-log-note-clock-out nil)
  3937. ("logrepeat" org-log-repeat state)
  3938. ("lognoterepeat" org-log-repeat note)
  3939. ("nologrepeat" org-log-repeat nil)
  3940. ("logreschedule" org-log-reschedule time)
  3941. ("lognotereschedule" org-log-reschedule note)
  3942. ("nologreschedule" org-log-reschedule nil)
  3943. ("logredeadline" org-log-redeadline time)
  3944. ("lognoteredeadline" org-log-redeadline note)
  3945. ("nologredeadline" org-log-redeadline nil)
  3946. ("logrefile" org-log-refile time)
  3947. ("lognoterefile" org-log-refile note)
  3948. ("nologrefile" org-log-refile nil)
  3949. ("fninline" org-footnote-define-inline t)
  3950. ("nofninline" org-footnote-define-inline nil)
  3951. ("fnlocal" org-footnote-section nil)
  3952. ("fnauto" org-footnote-auto-label t)
  3953. ("fnprompt" org-footnote-auto-label nil)
  3954. ("fnconfirm" org-footnote-auto-label confirm)
  3955. ("fnplain" org-footnote-auto-label plain)
  3956. ("fnadjust" org-footnote-auto-adjust t)
  3957. ("nofnadjust" org-footnote-auto-adjust nil)
  3958. ("constcgs" constants-unit-system cgs)
  3959. ("constSI" constants-unit-system SI)
  3960. ("noptag" org-tag-persistent-alist nil)
  3961. ("hideblocks" org-hide-block-startup t)
  3962. ("nohideblocks" org-hide-block-startup nil)
  3963. ("beamer" org-startup-with-beamer-mode t)
  3964. ("entitiespretty" org-pretty-entities t)
  3965. ("entitiesplain" org-pretty-entities nil))
  3966. "Variable associated with STARTUP options for org-mode.
  3967. Each element is a list of three items: the startup options (as written
  3968. in the #+STARTUP line), the corresponding variable, and the value to set
  3969. this variable to if the option is found. An optional forth element PUSH
  3970. means to push this value onto the list in the variable.")
  3971. (defun org-update-property-plist (key val props)
  3972. "Update PROPS with KEY and VAL."
  3973. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3974. (key (if appending (substring key 0 (- (length key) 1)) key))
  3975. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3976. (previous (cdr (assoc key props))))
  3977. (if appending
  3978. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3979. (cons (cons key val) remainder))))
  3980. (defconst org-block-regexp
  3981. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3982. "Regular expression for hiding blocks.")
  3983. (defconst org-heading-keyword-regexp-format
  3984. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3985. "Printf format for a regexp matching an headline with some keyword.
  3986. This regexp will match the headline of any node which has the
  3987. exact keyword that is put into the format. The keyword isn't in
  3988. any group by default, but the stars and the body are.")
  3989. (defconst org-heading-keyword-maybe-regexp-format
  3990. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3991. "Printf format for a regexp matching an headline, possibly with some keyword.
  3992. This regexp can match any headline with the specified keyword, or
  3993. without a keyword. The keyword isn't in any group by default,
  3994. but the stars and the body are.")
  3995. (defun org-set-regexps-and-options ()
  3996. "Precompute regular expressions for current buffer."
  3997. (when (derived-mode-p 'org-mode)
  3998. (org-set-local 'org-todo-kwd-alist nil)
  3999. (org-set-local 'org-todo-key-alist nil)
  4000. (org-set-local 'org-todo-key-trigger nil)
  4001. (org-set-local 'org-todo-keywords-1 nil)
  4002. (org-set-local 'org-done-keywords nil)
  4003. (org-set-local 'org-todo-heads nil)
  4004. (org-set-local 'org-todo-sets nil)
  4005. (org-set-local 'org-todo-log-states nil)
  4006. (org-set-local 'org-file-properties nil)
  4007. (org-set-local 'org-file-tags nil)
  4008. (let ((re (org-make-options-regexp
  4009. '("CATEGORY" "TODO" "COLUMNS"
  4010. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  4011. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  4012. "OPTIONS")
  4013. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4014. (splitre "[ \t]+")
  4015. (scripts org-use-sub-superscripts)
  4016. kwds kws0 kwsa key log value cat arch tags const links hw dws
  4017. tail sep kws1 prio props ftags drawers beamer-p
  4018. ext-setup-or-nil setup-contents (start 0))
  4019. (save-excursion
  4020. (save-restriction
  4021. (widen)
  4022. (goto-char (point-min))
  4023. (while (or (and ext-setup-or-nil
  4024. (string-match re ext-setup-or-nil start)
  4025. (setq start (match-end 0)))
  4026. (and (setq ext-setup-or-nil nil start 0)
  4027. (re-search-forward re nil t)))
  4028. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4029. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4030. (if (stringp value) (setq value (org-trim value)))
  4031. (cond
  4032. ((equal key "CATEGORY")
  4033. (setq cat value))
  4034. ((member key '("SEQ_TODO" "TODO"))
  4035. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4036. ((equal key "TYP_TODO")
  4037. (push (cons 'type (org-split-string value splitre)) kwds))
  4038. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4039. ;; general TODO-like setup
  4040. (push (cons (intern (downcase (match-string 1 key)))
  4041. (org-split-string value splitre)) kwds))
  4042. ((equal key "TAGS")
  4043. (setq tags (append tags (if tags '("\\n") nil)
  4044. (org-split-string value splitre))))
  4045. ((equal key "COLUMNS")
  4046. (org-set-local 'org-columns-default-format value))
  4047. ((equal key "LINK")
  4048. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4049. (push (cons (match-string 1 value)
  4050. (org-trim (match-string 2 value)))
  4051. links)))
  4052. ((equal key "PRIORITIES")
  4053. (setq prio (org-split-string value " +")))
  4054. ((equal key "PROPERTY")
  4055. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4056. (setq props (org-update-property-plist (match-string 1 value)
  4057. (match-string 2 value)
  4058. props))))
  4059. ((equal key "FILETAGS")
  4060. (when (string-match "\\S-" value)
  4061. (setq ftags
  4062. (append
  4063. ftags
  4064. (apply 'append
  4065. (mapcar (lambda (x) (org-split-string x ":"))
  4066. (org-split-string value)))))))
  4067. ((equal key "DRAWERS")
  4068. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4069. ((equal key "CONSTANTS")
  4070. (setq const (append const (org-split-string value splitre))))
  4071. ((equal key "STARTUP")
  4072. (let ((opts (org-split-string value splitre))
  4073. l var val)
  4074. (while (setq l (pop opts))
  4075. (when (setq l (assoc l org-startup-options))
  4076. (setq var (nth 1 l) val (nth 2 l))
  4077. (if (not (nth 3 l))
  4078. (set (make-local-variable var) val)
  4079. (if (not (listp (symbol-value var)))
  4080. (set (make-local-variable var) nil))
  4081. (set (make-local-variable var) (symbol-value var))
  4082. (add-to-list var val))))))
  4083. ((equal key "ARCHIVE")
  4084. (setq arch value)
  4085. (remove-text-properties 0 (length arch)
  4086. '(face t fontified t) arch))
  4087. ((equal key "LATEX_CLASS")
  4088. (setq beamer-p (equal value "beamer")))
  4089. ((equal key "OPTIONS")
  4090. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4091. (setq scripts (read (match-string 2 value)))))
  4092. ((equal key "SETUPFILE")
  4093. (setq setup-contents (org-file-contents
  4094. (expand-file-name
  4095. (org-remove-double-quotes value))
  4096. 'noerror))
  4097. (if (not ext-setup-or-nil)
  4098. (setq ext-setup-or-nil setup-contents start 0)
  4099. (setq ext-setup-or-nil
  4100. (concat (substring ext-setup-or-nil 0 start)
  4101. "\n" setup-contents "\n"
  4102. (substring ext-setup-or-nil start)))))))
  4103. ;; search for property blocks
  4104. (goto-char (point-min))
  4105. (while (re-search-forward org-block-regexp nil t)
  4106. (when (equal "PROPERTY" (upcase (match-string 1)))
  4107. (setq value (replace-regexp-in-string
  4108. "[\n\r]" " " (match-string 4)))
  4109. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4110. (setq props (org-update-property-plist (match-string 1 value)
  4111. (match-string 2 value)
  4112. props)))))))
  4113. (org-set-local 'org-use-sub-superscripts scripts)
  4114. (when cat
  4115. (org-set-local 'org-category (intern cat))
  4116. (push (cons "CATEGORY" cat) props))
  4117. (when prio
  4118. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4119. (setq prio (mapcar 'string-to-char prio))
  4120. (org-set-local 'org-highest-priority (nth 0 prio))
  4121. (org-set-local 'org-lowest-priority (nth 1 prio))
  4122. (org-set-local 'org-default-priority (nth 2 prio)))
  4123. (and props (org-set-local 'org-file-properties (nreverse props)))
  4124. (and ftags (org-set-local 'org-file-tags
  4125. (mapcar 'org-add-prop-inherited ftags)))
  4126. (and drawers (org-set-local 'org-drawers drawers))
  4127. (and arch (org-set-local 'org-archive-location arch))
  4128. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4129. ;; Process the TODO keywords
  4130. (unless kwds
  4131. ;; Use the global values as if they had been given locally.
  4132. (setq kwds (default-value 'org-todo-keywords))
  4133. (if (stringp (car kwds))
  4134. (setq kwds (list (cons org-todo-interpretation
  4135. (default-value 'org-todo-keywords)))))
  4136. (setq kwds (reverse kwds)))
  4137. (setq kwds (nreverse kwds))
  4138. (let (inter kws kw)
  4139. (while (setq kws (pop kwds))
  4140. (let ((kws (or
  4141. (run-hook-with-args-until-success
  4142. 'org-todo-setup-filter-hook kws)
  4143. kws)))
  4144. (setq inter (pop kws) sep (member "|" kws)
  4145. kws0 (delete "|" (copy-sequence kws))
  4146. kwsa nil
  4147. kws1 (mapcar
  4148. (lambda (x)
  4149. ;; 1 2
  4150. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4151. (progn
  4152. (setq kw (match-string 1 x)
  4153. key (and (match-end 2) (match-string 2 x))
  4154. log (org-extract-log-state-settings x))
  4155. (push (cons kw (and key (string-to-char key))) kwsa)
  4156. (and log (push log org-todo-log-states))
  4157. kw)
  4158. (error "Invalid TODO keyword %s" x)))
  4159. kws0)
  4160. kwsa (if kwsa (append '((:startgroup))
  4161. (nreverse kwsa)
  4162. '((:endgroup))))
  4163. hw (car kws1)
  4164. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4165. tail (list inter hw (car dws) (org-last dws))))
  4166. (add-to-list 'org-todo-heads hw 'append)
  4167. (push kws1 org-todo-sets)
  4168. (setq org-done-keywords (append org-done-keywords dws nil))
  4169. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4170. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4171. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4172. (setq org-todo-sets (nreverse org-todo-sets)
  4173. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4174. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4175. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4176. ;; Process the constants
  4177. (when const
  4178. (let (e cst)
  4179. (while (setq e (pop const))
  4180. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4181. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4182. (setq org-table-formula-constants-local cst)))
  4183. ;; Process the tags.
  4184. (when tags
  4185. (let (e tgs)
  4186. (while (setq e (pop tags))
  4187. (cond
  4188. ((equal e "{") (push '(:startgroup) tgs))
  4189. ((equal e "}") (push '(:endgroup) tgs))
  4190. ((equal e "\\n") (push '(:newline) tgs))
  4191. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4192. (push (cons (match-string 1 e)
  4193. (string-to-char (match-string 2 e)))
  4194. tgs))
  4195. (t (push (list e) tgs))))
  4196. (org-set-local 'org-tag-alist nil)
  4197. (while (setq e (pop tgs))
  4198. (or (and (stringp (car e))
  4199. (assoc (car e) org-tag-alist))
  4200. (push e org-tag-alist)))))
  4201. ;; Compute the regular expressions and other local variables.
  4202. ;; Using `org-outline-regexp-bol' would complicate them much,
  4203. ;; because of the fixed white space at the end of that string.
  4204. (if (not org-done-keywords)
  4205. (setq org-done-keywords (and org-todo-keywords-1
  4206. (list (org-last org-todo-keywords-1)))))
  4207. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4208. (length org-scheduled-string)
  4209. (length org-clock-string)
  4210. (length org-closed-string)))
  4211. org-drawer-regexp
  4212. (concat "^[ \t]*:\\("
  4213. (mapconcat 'regexp-quote org-drawers "\\|")
  4214. "\\):[ \t]*$")
  4215. org-not-done-keywords
  4216. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4217. org-todo-regexp
  4218. (concat "\\("
  4219. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4220. "\\)")
  4221. org-not-done-regexp
  4222. (concat "\\("
  4223. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4224. "\\)")
  4225. org-not-done-heading-regexp
  4226. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4227. org-todo-line-regexp
  4228. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4229. org-complex-heading-regexp
  4230. (concat "^\\(\\*+\\)"
  4231. "\\(?: +" org-todo-regexp "\\)?"
  4232. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4233. "\\(?: +\\(.*?\\)\\)??"
  4234. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4235. "[ \t]*$")
  4236. org-complex-heading-regexp-format
  4237. (concat "^\\(\\*+\\)"
  4238. "\\(?: +" org-todo-regexp "\\)?"
  4239. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4240. "\\(?: +"
  4241. ;; Stats cookies can be stuck to body.
  4242. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4243. "\\(%s\\)"
  4244. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4245. "\\)"
  4246. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4247. "[ \t]*$")
  4248. org-todo-line-tags-regexp
  4249. (concat "^\\(\\*+\\)"
  4250. "\\(?: +" org-todo-regexp "\\)?"
  4251. "\\(?: +\\(.*?\\)\\)??"
  4252. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4253. "[ \t]*$")
  4254. org-deadline-regexp (concat "\\<" org-deadline-string)
  4255. org-deadline-time-regexp
  4256. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4257. org-deadline-line-regexp
  4258. (concat "\\<\\(" org-deadline-string "\\).*")
  4259. org-scheduled-regexp
  4260. (concat "\\<" org-scheduled-string)
  4261. org-scheduled-time-regexp
  4262. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4263. org-closed-time-regexp
  4264. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4265. org-keyword-time-regexp
  4266. (concat "\\<\\(" org-scheduled-string
  4267. "\\|" org-deadline-string
  4268. "\\|" org-closed-string
  4269. "\\|" org-clock-string "\\)"
  4270. " *[[<]\\([^]>]+\\)[]>]")
  4271. org-keyword-time-not-clock-regexp
  4272. (concat "\\<\\(" org-scheduled-string
  4273. "\\|" org-deadline-string
  4274. "\\|" org-closed-string
  4275. "\\)"
  4276. " *[[<]\\([^]>]+\\)[]>]")
  4277. org-maybe-keyword-time-regexp
  4278. (concat "\\(\\<\\(" org-scheduled-string
  4279. "\\|" org-deadline-string
  4280. "\\|" org-closed-string
  4281. "\\|" org-clock-string "\\)\\)?"
  4282. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4283. org-all-time-keywords
  4284. (mapcar (lambda (w) (substring w 0 -1))
  4285. (list org-scheduled-string org-deadline-string
  4286. org-clock-string org-closed-string))
  4287. )
  4288. (org-compute-latex-and-specials-regexp)
  4289. (org-set-font-lock-defaults))))
  4290. (defun org-file-contents (file &optional noerror)
  4291. "Return the contents of FILE, as a string."
  4292. (if (or (not file)
  4293. (not (file-readable-p file)))
  4294. (if noerror
  4295. (progn
  4296. (message "Cannot read file \"%s\"" file)
  4297. (ding) (sit-for 2)
  4298. "")
  4299. (error "Cannot read file \"%s\"" file))
  4300. (with-temp-buffer
  4301. (insert-file-contents file)
  4302. (buffer-string))))
  4303. (defun org-extract-log-state-settings (x)
  4304. "Extract the log state setting from a TODO keyword string.
  4305. This will extract info from a string like \"WAIT(w@/!)\"."
  4306. (let (kw key log1 log2)
  4307. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4308. (setq kw (match-string 1 x)
  4309. key (and (match-end 2) (match-string 2 x))
  4310. log1 (and (match-end 3) (match-string 3 x))
  4311. log2 (and (match-end 4) (match-string 4 x)))
  4312. (and (or log1 log2)
  4313. (list kw
  4314. (and log1 (if (equal log1 "!") 'time 'note))
  4315. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4316. (defun org-remove-keyword-keys (list)
  4317. "Remove a pair of parenthesis at the end of each string in LIST."
  4318. (mapcar (lambda (x)
  4319. (if (string-match "(.*)$" x)
  4320. (substring x 0 (match-beginning 0))
  4321. x))
  4322. list))
  4323. (defun org-assign-fast-keys (alist)
  4324. "Assign fast keys to a keyword-key alist.
  4325. Respect keys that are already there."
  4326. (let (new e (alt ?0))
  4327. (while (setq e (pop alist))
  4328. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4329. (cdr e)) ;; Key already assigned.
  4330. (push e new)
  4331. (let ((clist (string-to-list (downcase (car e))))
  4332. (used (append new alist)))
  4333. (when (= (car clist) ?@)
  4334. (pop clist))
  4335. (while (and clist (rassoc (car clist) used))
  4336. (pop clist))
  4337. (unless clist
  4338. (while (rassoc alt used)
  4339. (incf alt)))
  4340. (push (cons (car e) (or (car clist) alt)) new))))
  4341. (nreverse new)))
  4342. ;;; Some variables used in various places
  4343. (defvar org-window-configuration nil
  4344. "Used in various places to store a window configuration.")
  4345. (defvar org-selected-window nil
  4346. "Used in various places to store a window configuration.")
  4347. (defvar org-finish-function nil
  4348. "Function to be called when `C-c C-c' is used.
  4349. This is for getting out of special buffers like capture.")
  4350. ;; FIXME: Occasionally check by commenting these, to make sure
  4351. ;; no other functions uses these, forgetting to let-bind them.
  4352. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4353. (defvar org-last-state)
  4354. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4355. ;; Defined somewhere in this file, but used before definition.
  4356. (defvar org-entities) ;; defined in org-entities.el
  4357. (defvar org-struct-menu)
  4358. (defvar org-org-menu)
  4359. (defvar org-tbl-menu)
  4360. ;;;; Define the Org-mode
  4361. ;; We use a before-change function to check if a table might need
  4362. ;; an update.
  4363. (defvar org-table-may-need-update t
  4364. "Indicates that a table might need an update.
  4365. This variable is set by `org-before-change-function'.
  4366. `org-table-align' sets it back to nil.")
  4367. (defun org-before-change-function (beg end)
  4368. "Every change indicates that a table might need an update."
  4369. (setq org-table-may-need-update t))
  4370. (defvar org-mode-map)
  4371. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4372. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4373. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4374. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4375. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4376. (defvar org-table-buffer-is-an nil)
  4377. (defvar bidi-paragraph-direction)
  4378. (defvar buffer-face-mode-face)
  4379. (require 'outline)
  4380. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4381. (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"))
  4382. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4383. ;; Other stuff we need.
  4384. (require 'time-date)
  4385. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4386. (require 'easymenu)
  4387. (require 'overlay)
  4388. (require 'org-macs)
  4389. (require 'org-entities)
  4390. ;; (require 'org-compat) moved higher up in the file before it is first used
  4391. (require 'org-faces)
  4392. (require 'org-list)
  4393. (require 'org-pcomplete)
  4394. (require 'org-src)
  4395. (require 'org-footnote)
  4396. ;; babel
  4397. (require 'ob)
  4398. (require 'ob-table)
  4399. (require 'ob-lob)
  4400. (require 'ob-ref)
  4401. (require 'ob-tangle)
  4402. (require 'ob-comint)
  4403. (require 'ob-keys)
  4404. ;;;###autoload
  4405. (define-derived-mode org-mode outline-mode "Org"
  4406. "Outline-based notes management and organizer, alias
  4407. \"Carsten's outline-mode for keeping track of everything.\"
  4408. Org-mode develops organizational tasks around a NOTES file which
  4409. contains information about projects as plain text. Org-mode is
  4410. implemented on top of outline-mode, which is ideal to keep the content
  4411. of large files well structured. It supports ToDo items, deadlines and
  4412. time stamps, which magically appear in the diary listing of the Emacs
  4413. calendar. Tables are easily created with a built-in table editor.
  4414. Plain text URL-like links connect to websites, emails (VM), Usenet
  4415. messages (Gnus), BBDB entries, and any files related to the project.
  4416. For printing and sharing of notes, an Org-mode file (or a part of it)
  4417. can be exported as a structured ASCII or HTML file.
  4418. The following commands are available:
  4419. \\{org-mode-map}"
  4420. ;; Get rid of Outline menus, they are not needed
  4421. ;; Need to do this here because define-derived-mode sets up
  4422. ;; the keymap so late. Still, it is a waste to call this each time
  4423. ;; we switch another buffer into org-mode.
  4424. (if (featurep 'xemacs)
  4425. (when (boundp 'outline-mode-menu-heading)
  4426. ;; Assume this is Greg's port, it uses easymenu
  4427. (easy-menu-remove outline-mode-menu-heading)
  4428. (easy-menu-remove outline-mode-menu-show)
  4429. (easy-menu-remove outline-mode-menu-hide))
  4430. (define-key org-mode-map [menu-bar headings] 'undefined)
  4431. (define-key org-mode-map [menu-bar hide] 'undefined)
  4432. (define-key org-mode-map [menu-bar show] 'undefined))
  4433. (org-load-modules-maybe)
  4434. (easy-menu-add org-org-menu)
  4435. (easy-menu-add org-tbl-menu)
  4436. (org-install-agenda-files-menu)
  4437. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4438. (add-to-invisibility-spec '(org-cwidth))
  4439. (add-to-invisibility-spec '(org-hide-block . t))
  4440. (when (featurep 'xemacs)
  4441. (org-set-local 'line-move-ignore-invisible t))
  4442. (org-set-local 'outline-regexp org-outline-regexp)
  4443. (org-set-local 'outline-level 'org-outline-level)
  4444. (setq bidi-paragraph-direction 'left-to-right)
  4445. (when (and org-ellipsis
  4446. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4447. (fboundp 'make-glyph-code))
  4448. (unless org-display-table
  4449. (setq org-display-table (make-display-table)))
  4450. (set-display-table-slot
  4451. org-display-table 4
  4452. (vconcat (mapcar
  4453. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4454. org-ellipsis)))
  4455. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4456. (setq buffer-display-table org-display-table))
  4457. (org-set-regexps-and-options)
  4458. (when (and org-tag-faces (not org-tags-special-faces-re))
  4459. ;; tag faces set outside customize.... force initialization.
  4460. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4461. ;; Calc embedded
  4462. (org-set-local 'calc-embedded-open-mode "# ")
  4463. (modify-syntax-entry ?@ "w")
  4464. (modify-syntax-entry ?\" "\"")
  4465. (if org-startup-truncated (setq truncate-lines t))
  4466. (org-set-local 'font-lock-unfontify-region-function
  4467. 'org-unfontify-region)
  4468. ;; Activate before-change-function
  4469. (org-set-local 'org-table-may-need-update t)
  4470. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4471. 'local)
  4472. ;; Check for running clock before killing a buffer
  4473. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4474. ;; Initialize macros templates.
  4475. (org-macro-initialize-templates)
  4476. ;; Initialize radio targets.
  4477. (org-update-radio-target-regexp)
  4478. ;; Indentation.
  4479. (org-set-local 'indent-line-function 'org-indent-line)
  4480. (org-set-local 'indent-region-function 'org-indent-region)
  4481. ;; Filling and auto-filling.
  4482. (org-setup-filling)
  4483. ;; Comments.
  4484. (org-setup-comments-handling)
  4485. ;; Beginning/end of defun
  4486. (org-set-local 'beginning-of-defun-function 'org-back-to-heading)
  4487. (org-set-local 'end-of-defun-function (lambda () (interactive) (org-end-of-subtree nil t)))
  4488. ;; Next error for sparse trees
  4489. (org-set-local 'next-error-function 'org-occur-next-match)
  4490. ;; Make sure dependence stuff works reliably, even for users who set it
  4491. ;; too late :-(
  4492. (if org-enforce-todo-dependencies
  4493. (add-hook 'org-blocker-hook
  4494. 'org-block-todo-from-children-or-siblings-or-parent)
  4495. (remove-hook 'org-blocker-hook
  4496. 'org-block-todo-from-children-or-siblings-or-parent))
  4497. (if org-enforce-todo-checkbox-dependencies
  4498. (add-hook 'org-blocker-hook
  4499. 'org-block-todo-from-checkboxes)
  4500. (remove-hook 'org-blocker-hook
  4501. 'org-block-todo-from-checkboxes))
  4502. ;; Align options lines
  4503. (org-set-local
  4504. 'align-mode-rules-list
  4505. '((org-in-buffer-settings
  4506. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4507. (modes . '(org-mode)))))
  4508. ;; Imenu
  4509. (org-set-local 'imenu-create-index-function
  4510. 'org-imenu-get-tree)
  4511. ;; Make isearch reveal context
  4512. (if (or (featurep 'xemacs)
  4513. (not (boundp 'outline-isearch-open-invisible-function)))
  4514. ;; Emacs 21 and XEmacs make use of the hook
  4515. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4516. ;; Emacs 22 deals with this through a special variable
  4517. (org-set-local 'outline-isearch-open-invisible-function
  4518. (lambda (&rest ignore) (org-show-context 'isearch))))
  4519. ;; Turn on org-beamer-mode?
  4520. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4521. ;; Setup the pcomplete hooks
  4522. (set (make-local-variable 'pcomplete-command-completion-function)
  4523. 'org-pcomplete-initial)
  4524. (set (make-local-variable 'pcomplete-command-name-function)
  4525. 'org-command-at-point)
  4526. (set (make-local-variable 'pcomplete-default-completion-function)
  4527. 'ignore)
  4528. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4529. 'org-parse-arguments)
  4530. (set (make-local-variable 'pcomplete-termination-string) "")
  4531. (when (>= emacs-major-version 23)
  4532. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4533. ;; If empty file that did not turn on org-mode automatically, make it to.
  4534. (if (and org-insert-mode-line-in-empty-file
  4535. (org-called-interactively-p 'any)
  4536. (= (point-min) (point-max)))
  4537. (insert "# -*- mode: org -*-\n\n"))
  4538. (unless org-inhibit-startup
  4539. (when org-startup-align-all-tables
  4540. (let ((bmp (buffer-modified-p)))
  4541. (org-table-map-tables 'org-table-align 'quietly)
  4542. (set-buffer-modified-p bmp)))
  4543. (when org-startup-with-inline-images
  4544. (org-display-inline-images))
  4545. (when org-startup-indented
  4546. (require 'org-indent)
  4547. (org-indent-mode 1))
  4548. (unless org-inhibit-startup-visibility-stuff
  4549. (org-set-startup-visibility)))
  4550. ;; Try to set org-hide correctly
  4551. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4552. (when (fboundp 'abbrev-table-put)
  4553. (abbrev-table-put org-mode-abbrev-table
  4554. :parents (list text-mode-abbrev-table)))
  4555. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4556. (defun org-find-invisible-foreground ()
  4557. (let ((candidates (remove
  4558. "unspecified-bg"
  4559. (list
  4560. (face-background 'default)
  4561. (face-background 'org-default)
  4562. (cdr (assoc 'background-color default-frame-alist))
  4563. (cdr (assoc 'background-color initial-frame-alist))
  4564. (cdr (assoc 'background-color window-system-default-frame-alist))
  4565. (face-foreground 'org-hide)))))
  4566. (car (remove nil candidates))))
  4567. (defun org-current-time ()
  4568. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4569. (if (> (car org-time-stamp-rounding-minutes) 1)
  4570. (let ((r (car org-time-stamp-rounding-minutes))
  4571. (time (decode-time)))
  4572. (apply 'encode-time
  4573. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4574. (nthcdr 2 time))))
  4575. (current-time)))
  4576. (defun org-today ()
  4577. "Return today date, considering `org-extend-today-until'."
  4578. (time-to-days
  4579. (time-subtract (current-time)
  4580. (list 0 (* 3600 org-extend-today-until) 0))))
  4581. ;;;; Font-Lock stuff, including the activators
  4582. (defvar org-mouse-map (make-sparse-keymap))
  4583. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4584. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4585. (when org-mouse-1-follows-link
  4586. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4587. (when org-tab-follows-link
  4588. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4589. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4590. (require 'font-lock)
  4591. (defconst org-non-link-chars "]\t\n\r<>")
  4592. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4593. "shell" "elisp" "doi" "message"))
  4594. (defvar org-link-types-re nil
  4595. "Matches a link that has a url-like prefix like \"http:\"")
  4596. (defvar org-link-re-with-space nil
  4597. "Matches a link with spaces, optional angular brackets around it.")
  4598. (defvar org-link-re-with-space2 nil
  4599. "Matches a link with spaces, optional angular brackets around it.")
  4600. (defvar org-link-re-with-space3 nil
  4601. "Matches a link with spaces, only for internal part in bracket links.")
  4602. (defvar org-angle-link-re nil
  4603. "Matches link with angular brackets, spaces are allowed.")
  4604. (defvar org-plain-link-re nil
  4605. "Matches plain link, without spaces.")
  4606. (defvar org-bracket-link-regexp nil
  4607. "Matches a link in double brackets.")
  4608. (defvar org-bracket-link-analytic-regexp nil
  4609. "Regular expression used to analyze links.
  4610. Here is what the match groups contain after a match:
  4611. 1: http:
  4612. 2: http
  4613. 3: path
  4614. 4: [desc]
  4615. 5: desc")
  4616. (defvar org-bracket-link-analytic-regexp++ nil
  4617. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4618. (defvar org-any-link-re nil
  4619. "Regular expression matching any link.")
  4620. (defcustom org-match-sexp-depth 3
  4621. "Number of stacked braces for sub/superscript matching.
  4622. This has to be set before loading org.el to be effective."
  4623. :group 'org-export-translation ; ??????????????????????????/
  4624. :type 'integer)
  4625. (defun org-create-multibrace-regexp (left right n)
  4626. "Create a regular expression which will match a balanced sexp.
  4627. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4628. as single character strings.
  4629. The regexp returned will match the entire expression including the
  4630. delimiters. It will also define a single group which contains the
  4631. match except for the outermost delimiters. The maximum depth of
  4632. stacked delimiters is N. Escaping delimiters is not possible."
  4633. (let* ((nothing (concat "[^" left right "]*?"))
  4634. (or "\\|")
  4635. (re nothing)
  4636. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4637. (while (> n 1)
  4638. (setq n (1- n)
  4639. re (concat re or next)
  4640. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4641. (concat left "\\(" re "\\)" right)))
  4642. (defvar org-match-substring-regexp
  4643. (concat
  4644. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4645. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4646. "\\|"
  4647. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4648. "\\|"
  4649. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4650. "The regular expression matching a sub- or superscript.")
  4651. (defvar org-match-substring-with-braces-regexp
  4652. (concat
  4653. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4654. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4655. "\\)")
  4656. "The regular expression matching a sub- or superscript, forcing braces.")
  4657. (defun org-make-link-regexps ()
  4658. "Update the link regular expressions.
  4659. This should be called after the variable `org-link-types' has changed."
  4660. (setq org-link-types-re
  4661. (concat
  4662. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4663. org-link-re-with-space
  4664. (concat
  4665. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4666. "\\([^" org-non-link-chars " ]"
  4667. "[^" org-non-link-chars "]*"
  4668. "[^" org-non-link-chars " ]\\)>?")
  4669. org-link-re-with-space2
  4670. (concat
  4671. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4672. "\\([^" org-non-link-chars " ]"
  4673. "[^\t\n\r]*"
  4674. "[^" org-non-link-chars " ]\\)>?")
  4675. org-link-re-with-space3
  4676. (concat
  4677. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4678. "\\([^" org-non-link-chars " ]"
  4679. "[^\t\n\r]*\\)")
  4680. org-angle-link-re
  4681. (concat
  4682. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4683. "\\([^" org-non-link-chars " ]"
  4684. "[^" org-non-link-chars "]*"
  4685. "\\)>")
  4686. org-plain-link-re
  4687. (concat
  4688. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4689. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4690. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4691. org-bracket-link-regexp
  4692. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4693. org-bracket-link-analytic-regexp
  4694. (concat
  4695. "\\[\\["
  4696. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4697. "\\([^]]+\\)"
  4698. "\\]"
  4699. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4700. "\\]")
  4701. org-bracket-link-analytic-regexp++
  4702. (concat
  4703. "\\[\\["
  4704. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4705. "\\([^]]+\\)"
  4706. "\\]"
  4707. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4708. "\\]")
  4709. org-any-link-re
  4710. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4711. org-angle-link-re "\\)\\|\\("
  4712. org-plain-link-re "\\)")))
  4713. (org-make-link-regexps)
  4714. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4715. "Regular expression for fast time stamp matching.")
  4716. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4717. "Regular expression for fast time stamp matching.")
  4718. (defconst org-ts-regexp0
  4719. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4720. "Regular expression matching time strings for analysis.
  4721. This one does not require the space after the date, so it can be used
  4722. on a string that terminates immediately after the date.")
  4723. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4724. "Regular expression matching time strings for analysis.")
  4725. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4726. "Regular expression matching time stamps, with groups.")
  4727. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4728. "Regular expression matching time stamps (also [..]), with groups.")
  4729. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4730. "Regular expression matching a time stamp range.")
  4731. (defconst org-tr-regexp-both
  4732. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4733. "Regular expression matching a time stamp range.")
  4734. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4735. org-ts-regexp "\\)?")
  4736. "Regular expression matching a time stamp or time stamp range.")
  4737. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4738. org-ts-regexp-both "\\)?")
  4739. "Regular expression matching a time stamp or time stamp range.
  4740. The time stamps may be either active or inactive.")
  4741. (defvar org-emph-face nil)
  4742. (defun org-do-emphasis-faces (limit)
  4743. "Run through the buffer and add overlays to emphasized strings."
  4744. (let (rtn a)
  4745. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4746. (if (not (= (char-after (match-beginning 3))
  4747. (char-after (match-beginning 4))))
  4748. (progn
  4749. (setq rtn t)
  4750. (setq a (assoc (match-string 3) org-emphasis-alist))
  4751. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4752. 'face
  4753. (nth 1 a))
  4754. (and (nth 4 a)
  4755. (org-remove-flyspell-overlays-in
  4756. (match-beginning 0) (match-end 0)))
  4757. (add-text-properties (match-beginning 2) (match-end 2)
  4758. '(font-lock-multiline t org-emphasis t))
  4759. (when org-hide-emphasis-markers
  4760. (add-text-properties (match-end 4) (match-beginning 5)
  4761. '(invisible org-link))
  4762. (add-text-properties (match-beginning 3) (match-end 3)
  4763. '(invisible org-link)))))
  4764. (backward-char 1))
  4765. rtn))
  4766. (defun org-emphasize (&optional char)
  4767. "Insert or change an emphasis, i.e. a font like bold or italic.
  4768. If there is an active region, change that region to a new emphasis.
  4769. If there is no region, just insert the marker characters and position
  4770. the cursor between them.
  4771. CHAR should be either the marker character, or the first character of the
  4772. HTML tag associated with that emphasis. If CHAR is a space, the means
  4773. to remove the emphasis of the selected region.
  4774. If char is not given (for example in an interactive call) it
  4775. will be prompted for."
  4776. (interactive)
  4777. (let ((eal org-emphasis-alist) e det
  4778. (erc org-emphasis-regexp-components)
  4779. (prompt "")
  4780. (string "") beg end move tag c s)
  4781. (if (org-region-active-p)
  4782. (setq beg (region-beginning) end (region-end)
  4783. string (buffer-substring beg end))
  4784. (setq move t))
  4785. (while (setq e (pop eal))
  4786. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4787. c (aref tag 0))
  4788. (push (cons c (string-to-char (car e))) det)
  4789. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4790. (substring tag 1)))))
  4791. (setq det (nreverse det))
  4792. (unless char
  4793. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4794. (setq char (read-char-exclusive)))
  4795. (setq char (or (cdr (assoc char det)) char))
  4796. (if (equal char ?\ )
  4797. (setq s "" move nil)
  4798. (unless (assoc (char-to-string char) org-emphasis-alist)
  4799. (error "No such emphasis marker: \"%c\"" char))
  4800. (setq s (char-to-string char)))
  4801. (while (and (> (length string) 1)
  4802. (equal (substring string 0 1) (substring string -1))
  4803. (assoc (substring string 0 1) org-emphasis-alist))
  4804. (setq string (substring string 1 -1)))
  4805. (setq string (concat s string s))
  4806. (if beg (delete-region beg end))
  4807. (unless (or (bolp)
  4808. (string-match (concat "[" (nth 0 erc) "\n]")
  4809. (char-to-string (char-before (point)))))
  4810. (insert " "))
  4811. (unless (or (eobp)
  4812. (string-match (concat "[" (nth 1 erc) "\n]")
  4813. (char-to-string (char-after (point)))))
  4814. (insert " ") (backward-char 1))
  4815. (insert string)
  4816. (and move (backward-char 1))))
  4817. (defconst org-nonsticky-props
  4818. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4819. (defsubst org-rear-nonsticky-at (pos)
  4820. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4821. (defun org-activate-plain-links (limit)
  4822. "Run through the buffer and add overlays to links."
  4823. (catch 'exit
  4824. (let (f hl)
  4825. (when (re-search-forward (concat org-plain-link-re) limit t)
  4826. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4827. (setq f (get-text-property (match-beginning 0) 'face))
  4828. (setq hl (org-match-string-no-properties 0))
  4829. (if (or (eq f 'org-tag)
  4830. (and (listp f) (memq 'org-tag f)))
  4831. nil
  4832. (add-text-properties (match-beginning 0) (match-end 0)
  4833. (list 'mouse-face 'highlight
  4834. 'face 'org-link
  4835. 'htmlize-link `(:uri ,hl)
  4836. 'keymap org-mouse-map))
  4837. (org-rear-nonsticky-at (match-end 0)))
  4838. t))))
  4839. (defun org-activate-code (limit)
  4840. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4841. (progn
  4842. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4843. (remove-text-properties (match-beginning 0) (match-end 0)
  4844. '(display t invisible t intangible t))
  4845. t)))
  4846. (defcustom org-src-fontify-natively nil
  4847. "When non-nil, fontify code in code blocks."
  4848. :type 'boolean
  4849. :version "24.1"
  4850. :group 'org-appearance
  4851. :group 'org-babel)
  4852. (defcustom org-allow-promoting-top-level-subtree nil
  4853. "When non-nil, allow promoting a top level subtree.
  4854. The leading star of the top level headline will be replaced
  4855. by a #."
  4856. :type 'boolean
  4857. :version "24.1"
  4858. :group 'org-appearance)
  4859. (defun org-fontify-meta-lines-and-blocks (limit)
  4860. (condition-case nil
  4861. (org-fontify-meta-lines-and-blocks-1 limit)
  4862. (error (message "org-mode fontification error"))))
  4863. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4864. "Fontify #+ lines and blocks, in the correct ways."
  4865. (let ((case-fold-search t))
  4866. (if (re-search-forward
  4867. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4868. limit t)
  4869. (let ((beg (match-beginning 0))
  4870. (block-start (match-end 0))
  4871. (block-end nil)
  4872. (lang (match-string 7))
  4873. (beg1 (line-beginning-position 2))
  4874. (dc1 (downcase (match-string 2)))
  4875. (dc3 (downcase (match-string 3)))
  4876. end end1 quoting block-type ovl)
  4877. (cond
  4878. ((member dc1 '("+html:" "+ascii:" "+latex:" "+docbook:"))
  4879. ;; a single line of backend-specific content
  4880. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4881. (remove-text-properties (match-beginning 0) (match-end 0)
  4882. '(display t invisible t intangible t))
  4883. (add-text-properties (match-beginning 1) (match-end 3)
  4884. '(font-lock-fontified t face org-meta-line))
  4885. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4886. '(font-lock-fontified t face org-block))
  4887. ; for backend-specific code
  4888. t)
  4889. ((and (match-end 4) (equal dc3 "+begin"))
  4890. ;; Truly a block
  4891. (setq block-type (downcase (match-string 5))
  4892. quoting (member block-type org-protecting-blocks))
  4893. (when (re-search-forward
  4894. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4895. nil t) ;; on purpose, we look further than LIMIT
  4896. (setq end (min (point-max) (match-end 0))
  4897. end1 (min (point-max) (1- (match-beginning 0))))
  4898. (setq block-end (match-beginning 0))
  4899. (when quoting
  4900. (remove-text-properties beg end
  4901. '(display t invisible t intangible t)))
  4902. (add-text-properties
  4903. beg end
  4904. '(font-lock-fontified t font-lock-multiline t))
  4905. (add-text-properties beg beg1 '(face org-meta-line))
  4906. (add-text-properties end1 (min (point-max) (1+ end))
  4907. '(face org-meta-line)) ; for end_src
  4908. (cond
  4909. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4910. (org-src-font-lock-fontify-block lang block-start block-end)
  4911. ;; remove old background overlays
  4912. (mapc (lambda (ov)
  4913. (if (eq (overlay-get ov 'face) 'org-block-background)
  4914. (delete-overlay ov)))
  4915. (overlays-at (/ (+ beg1 block-end) 2)))
  4916. ;; add a background overlay
  4917. (setq ovl (make-overlay beg1 block-end))
  4918. (overlay-put ovl 'face 'org-block-background)
  4919. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4920. (quoting
  4921. (add-text-properties beg1 (min (point-max) (1+ end1))
  4922. '(face org-block))) ; end of source block
  4923. ((not org-fontify-quote-and-verse-blocks))
  4924. ((string= block-type "quote")
  4925. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  4926. ((string= block-type "verse")
  4927. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  4928. (add-text-properties beg beg1 '(face org-block-begin-line))
  4929. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  4930. '(face org-block-end-line))
  4931. t))
  4932. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  4933. (add-text-properties
  4934. beg (match-end 3)
  4935. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4936. '(font-lock-fontified t invisible t)
  4937. '(font-lock-fontified t face org-document-info-keyword)))
  4938. (add-text-properties
  4939. (match-beginning 6) (match-end 6)
  4940. (if (string-equal dc1 "+title:")
  4941. '(font-lock-fontified t face org-document-title)
  4942. '(font-lock-fontified t face org-document-info))))
  4943. ((or (equal dc1 "+results")
  4944. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  4945. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  4946. "+call:" "+header:" "+headers:" "+name:"))
  4947. (and (match-end 4) (equal dc3 "+attr")))
  4948. (add-text-properties
  4949. beg (match-end 0)
  4950. '(font-lock-fontified t face org-meta-line))
  4951. t)
  4952. ((member dc3 '(" " ""))
  4953. (add-text-properties
  4954. beg (match-end 0)
  4955. '(font-lock-fontified t face font-lock-comment-face)))
  4956. ((not (member (char-after beg) '(?\ ?\t)))
  4957. ;; just any other in-buffer setting, but not indented
  4958. (add-text-properties
  4959. beg (match-end 0)
  4960. '(font-lock-fontified t face org-meta-line))
  4961. t)
  4962. (t nil))))))
  4963. (defun org-strip-protective-commas (beg end)
  4964. "Strip protective commas between BEG and END in the current buffer."
  4965. (interactive "r")
  4966. (save-excursion
  4967. (save-match-data
  4968. (goto-char beg)
  4969. (let ((front-line (save-excursion
  4970. (re-search-forward
  4971. "[^[:space:]]" end t)
  4972. (goto-char (match-beginning 0))
  4973. (current-column))))
  4974. (while (re-search-forward "^[ \t]*\\(,\\)\\([*]\\|#\\)" end t)
  4975. (goto-char (match-beginning 1))
  4976. (when (= (current-column) front-line)
  4977. (replace-match "" nil nil nil 1)))))))
  4978. (defun org-activate-angle-links (limit)
  4979. "Run through the buffer and add overlays to links."
  4980. (if (re-search-forward org-angle-link-re limit t)
  4981. (progn
  4982. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4983. (add-text-properties (match-beginning 0) (match-end 0)
  4984. (list 'mouse-face 'highlight
  4985. 'keymap org-mouse-map))
  4986. (org-rear-nonsticky-at (match-end 0))
  4987. t)))
  4988. (defun org-activate-footnote-links (limit)
  4989. "Run through the buffer and add overlays to footnotes."
  4990. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4991. (when fn
  4992. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4993. (org-remove-flyspell-overlays-in beg end)
  4994. (add-text-properties beg end
  4995. (list 'mouse-face 'highlight
  4996. 'keymap org-mouse-map
  4997. 'help-echo
  4998. (if (= (point-at-bol) beg)
  4999. "Footnote definition"
  5000. "Footnote reference")
  5001. 'font-lock-fontified t
  5002. 'font-lock-multiline t
  5003. 'face 'org-footnote))))))
  5004. (defun org-activate-bracket-links (limit)
  5005. "Run through the buffer and add overlays to bracketed links."
  5006. (if (re-search-forward org-bracket-link-regexp limit t)
  5007. (let* ((hl (org-match-string-no-properties 1))
  5008. (help (concat "LINK: " hl))
  5009. ;; FIXME: Above we should remove the escapes. But that
  5010. ;; requires another match, protecting match data, a lot
  5011. ;; of overhead for font-lock.
  5012. (ip (org-maybe-intangible
  5013. (list 'invisible 'org-link
  5014. 'keymap org-mouse-map 'mouse-face 'highlight
  5015. 'font-lock-multiline t 'help-echo help
  5016. 'htmlize-link `(:uri ,hl))))
  5017. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5018. 'font-lock-multiline t 'help-echo help
  5019. 'htmlize-link `(:uri ,hl))))
  5020. ;; We need to remove the invisible property here. Table narrowing
  5021. ;; may have made some of this invisible.
  5022. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5023. (remove-text-properties (match-beginning 0) (match-end 0)
  5024. '(invisible nil))
  5025. (if (match-end 3)
  5026. (progn
  5027. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5028. (org-rear-nonsticky-at (match-beginning 3))
  5029. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5030. (org-rear-nonsticky-at (match-end 3))
  5031. (add-text-properties (match-end 3) (match-end 0) ip)
  5032. (org-rear-nonsticky-at (match-end 0)))
  5033. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5034. (org-rear-nonsticky-at (match-beginning 1))
  5035. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5036. (org-rear-nonsticky-at (match-end 1))
  5037. (add-text-properties (match-end 1) (match-end 0) ip)
  5038. (org-rear-nonsticky-at (match-end 0)))
  5039. t)))
  5040. (defun org-activate-dates (limit)
  5041. "Run through the buffer and add overlays to dates."
  5042. (if (re-search-forward org-tsr-regexp-both limit t)
  5043. (progn
  5044. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5045. (add-text-properties (match-beginning 0) (match-end 0)
  5046. (list 'mouse-face 'highlight
  5047. 'keymap org-mouse-map))
  5048. (org-rear-nonsticky-at (match-end 0))
  5049. (when org-display-custom-times
  5050. (if (match-end 3)
  5051. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5052. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5053. t)))
  5054. (defvar org-target-link-regexp nil
  5055. "Regular expression matching radio targets in plain text.")
  5056. (make-variable-buffer-local 'org-target-link-regexp)
  5057. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5058. "Regular expression matching a link target.")
  5059. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5060. "Regular expression matching a radio target.")
  5061. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5062. "Regular expression matching any target.")
  5063. (defun org-activate-target-links (limit)
  5064. "Run through the buffer and add overlays to target matches."
  5065. (when org-target-link-regexp
  5066. (let ((case-fold-search t))
  5067. (if (re-search-forward org-target-link-regexp limit t)
  5068. (progn
  5069. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5070. (add-text-properties (match-beginning 0) (match-end 0)
  5071. (list 'mouse-face 'highlight
  5072. 'keymap org-mouse-map
  5073. 'help-echo "Radio target link"
  5074. 'org-linked-text t))
  5075. (org-rear-nonsticky-at (match-end 0))
  5076. t)))))
  5077. (defun org-update-radio-target-regexp ()
  5078. "Find all radio targets in this file and update the regular expression."
  5079. (interactive)
  5080. (when (memq 'radio org-activate-links)
  5081. (setq org-target-link-regexp
  5082. (org-make-target-link-regexp (org-all-targets 'radio)))
  5083. (org-restart-font-lock)))
  5084. (defun org-hide-wide-columns (limit)
  5085. (let (s e)
  5086. (setq s (text-property-any (point) (or limit (point-max))
  5087. 'org-cwidth t))
  5088. (when s
  5089. (setq e (next-single-property-change s 'org-cwidth))
  5090. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5091. (goto-char e)
  5092. t)))
  5093. (defvar org-latex-and-specials-regexp nil
  5094. "Regular expression for highlighting export special stuff.")
  5095. (defvar org-match-substring-regexp)
  5096. (defvar org-match-substring-with-braces-regexp)
  5097. ;; This should be with the exporter code, but we also use if for font-locking
  5098. (defconst org-export-html-special-string-regexps
  5099. '(("\\\\-" . "&shy;")
  5100. ("---\\([^-]\\)" . "&mdash;\\1")
  5101. ("--\\([^-]\\)" . "&ndash;\\1")
  5102. ("\\.\\.\\." . "&hellip;"))
  5103. "Regular expressions for special string conversion.")
  5104. (defun org-compute-latex-and-specials-regexp ()
  5105. "Compute regular expression for stuff treated specially by exporters."
  5106. (if (not org-highlight-latex-fragments-and-specials)
  5107. (org-set-local 'org-latex-and-specials-regexp nil)
  5108. (require 'org-exp)
  5109. (let*
  5110. ((matchers (plist-get org-format-latex-options :matchers))
  5111. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  5112. org-latex-regexps)))
  5113. (org-export-allow-BIND nil)
  5114. (options (org-combine-plists (org-default-export-plist)
  5115. (org-infile-export-plist)))
  5116. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  5117. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  5118. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  5119. (org-export-html-expand (plist-get options :expand-quoted-html))
  5120. (org-export-with-special-strings (plist-get options :special-strings))
  5121. (re-sub
  5122. (cond
  5123. ((equal org-export-with-sub-superscripts '{})
  5124. (list org-match-substring-with-braces-regexp))
  5125. (org-export-with-sub-superscripts
  5126. (list org-match-substring-regexp))))
  5127. (re-latex
  5128. (if org-export-with-LaTeX-fragments
  5129. (mapcar (lambda (x) (nth 1 x)) latexs)))
  5130. (re-macros
  5131. (if org-export-with-TeX-macros
  5132. (list (concat "\\\\"
  5133. (regexp-opt
  5134. (append
  5135. (delq nil
  5136. (mapcar 'car-safe
  5137. (append org-entities-user
  5138. org-entities)))
  5139. (if (boundp 'org-latex-entities)
  5140. (mapcar (lambda (x)
  5141. (or (car-safe x) x))
  5142. org-latex-entities)
  5143. nil))
  5144. 'words))) ; FIXME
  5145. ))
  5146. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5147. (re-special (if org-export-with-special-strings
  5148. (mapcar (lambda (x) (car x))
  5149. org-export-html-special-string-regexps)))
  5150. (re-rest
  5151. (delq nil
  5152. (list
  5153. (if org-export-html-expand "@<[^>\n]+>")
  5154. ))))
  5155. (org-set-local
  5156. 'org-latex-and-specials-regexp
  5157. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5158. re-rest) "\\|")))))
  5159. (defun org-do-latex-and-special-faces (limit)
  5160. "Run through the buffer and add overlays to links."
  5161. (when org-latex-and-specials-regexp
  5162. (let (rtn d)
  5163. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5164. limit t))
  5165. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5166. 'face))
  5167. '(org-code org-verbatim underline)))
  5168. (progn
  5169. (setq rtn t
  5170. d (cond ((member (char-after (1+ (match-beginning 0)))
  5171. '(?_ ?^)) 1)
  5172. (t 0)))
  5173. (font-lock-prepend-text-property
  5174. (+ d (match-beginning 0)) (match-end 0)
  5175. 'face 'org-latex-and-export-specials)
  5176. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5177. '(font-lock-multiline t)))))
  5178. rtn)))
  5179. (defun org-restart-font-lock ()
  5180. "Restart `font-lock-mode', to force refontification."
  5181. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5182. (font-lock-mode -1)
  5183. (font-lock-mode 1)))
  5184. (defun org-all-targets (&optional radio)
  5185. "Return a list of all targets in this file.
  5186. With optional argument RADIO, only find radio targets."
  5187. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  5188. rtn)
  5189. (save-excursion
  5190. (goto-char (point-min))
  5191. (while (re-search-forward re nil t)
  5192. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  5193. rtn)))
  5194. (defun org-make-target-link-regexp (targets)
  5195. "Make regular expression matching all strings in TARGETS.
  5196. The regular expression finds the targets also if there is a line break
  5197. between words."
  5198. (and targets
  5199. (concat
  5200. "\\<\\("
  5201. (mapconcat
  5202. (lambda (x)
  5203. (setq x (regexp-quote x))
  5204. (while (string-match " +" x)
  5205. (setq x (replace-match "\\s-+" t t x)))
  5206. x)
  5207. targets
  5208. "\\|")
  5209. "\\)\\>")))
  5210. (defun org-activate-tags (limit)
  5211. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5212. (progn
  5213. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5214. (add-text-properties (match-beginning 1) (match-end 1)
  5215. (list 'mouse-face 'highlight
  5216. 'keymap org-mouse-map))
  5217. (org-rear-nonsticky-at (match-end 1))
  5218. t)))
  5219. (defun org-outline-level ()
  5220. "Compute the outline level of the heading at point.
  5221. If this is called at a normal headline, the level is the number of stars.
  5222. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5223. (save-excursion
  5224. (if (not (condition-case nil
  5225. (org-back-to-heading t)
  5226. (error nil)))
  5227. 0
  5228. (looking-at org-outline-regexp)
  5229. (1- (- (match-end 0) (match-beginning 0))))))
  5230. (defvar org-font-lock-keywords nil)
  5231. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5232. "Regular expression matching a property line.")
  5233. (defvar org-font-lock-hook nil
  5234. "Functions to be called for special font lock stuff.")
  5235. (defvar org-font-lock-set-keywords-hook nil
  5236. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5237. This is called after `org-font-lock-extra-keywords' is defined, but before
  5238. it is installed to be used by font lock. This can be useful if something
  5239. needs to be inserted at a specific position in the font-lock sequence.")
  5240. (defun org-font-lock-hook (limit)
  5241. "Run `org-font-lock-hook' within LIMIT."
  5242. (run-hook-with-args 'org-font-lock-hook limit))
  5243. (defun org-set-font-lock-defaults ()
  5244. "Set font lock defaults for the current buffer."
  5245. (let* ((em org-fontify-emphasized-text)
  5246. (lk org-activate-links)
  5247. (org-font-lock-extra-keywords
  5248. (list
  5249. ;; Call the hook
  5250. '(org-font-lock-hook)
  5251. ;; Headlines
  5252. `(,(if org-fontify-whole-heading-line
  5253. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5254. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5255. (1 (org-get-level-face 1))
  5256. (2 (org-get-level-face 2))
  5257. (3 (org-get-level-face 3)))
  5258. ;; Table lines
  5259. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5260. (1 'org-table t))
  5261. ;; Table internals
  5262. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5263. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5264. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5265. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5266. ;; Drawers
  5267. (list org-drawer-regexp '(0 'org-special-keyword t))
  5268. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5269. ;; Properties
  5270. (list org-property-re
  5271. '(1 'org-special-keyword t)
  5272. '(3 'org-property-value t))
  5273. ;; Links
  5274. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5275. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5276. (if (memq 'plain lk) '(org-activate-plain-links))
  5277. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5278. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5279. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5280. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5281. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5282. '(org-hide-wide-columns (0 nil append))
  5283. ;; TODO keyword
  5284. (list (format org-heading-keyword-regexp-format
  5285. org-todo-regexp)
  5286. '(2 (org-get-todo-face 2) t))
  5287. ;; DONE
  5288. (if org-fontify-done-headline
  5289. (list (format org-heading-keyword-regexp-format
  5290. (concat
  5291. "\\(?:"
  5292. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5293. "\\)"))
  5294. '(2 'org-headline-done t))
  5295. nil)
  5296. ;; Priorities
  5297. '(org-font-lock-add-priority-faces)
  5298. ;; Tags
  5299. '(org-font-lock-add-tag-faces)
  5300. ;; Special keywords
  5301. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5302. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5303. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5304. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5305. ;; Emphasis
  5306. (if em
  5307. (if (featurep 'xemacs)
  5308. '(org-do-emphasis-faces (0 nil append))
  5309. '(org-do-emphasis-faces)))
  5310. ;; Checkboxes
  5311. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5312. 1 'org-checkbox prepend)
  5313. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5314. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5315. (0 (org-get-checkbox-statistics-face) t)))
  5316. ;; Description list items
  5317. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5318. 1 'org-list-dt prepend)
  5319. ;; ARCHIVEd headings
  5320. (list (concat
  5321. org-outline-regexp-bol
  5322. "\\(.*:" org-archive-tag ":.*\\)")
  5323. '(1 'org-archived prepend))
  5324. ;; Specials
  5325. '(org-do-latex-and-special-faces)
  5326. '(org-fontify-entities)
  5327. '(org-raise-scripts)
  5328. ;; Code
  5329. '(org-activate-code (1 'org-code t))
  5330. ;; COMMENT
  5331. (list (format org-heading-keyword-regexp-format
  5332. (concat "\\("
  5333. org-comment-string "\\|" org-quote-string
  5334. "\\)"))
  5335. '(2 'org-special-keyword t))
  5336. ;; Blocks and meta lines
  5337. '(org-fontify-meta-lines-and-blocks)
  5338. )))
  5339. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5340. (run-hooks 'org-font-lock-set-keywords-hook)
  5341. ;; Now set the full font-lock-keywords
  5342. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5343. (org-set-local 'font-lock-defaults
  5344. '(org-font-lock-keywords t nil nil backward-paragraph))
  5345. (kill-local-variable 'font-lock-keywords) nil))
  5346. (defun org-toggle-pretty-entities ()
  5347. "Toggle the composition display of entities as UTF8 characters."
  5348. (interactive)
  5349. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5350. (org-restart-font-lock)
  5351. (if org-pretty-entities
  5352. (message "Entities are displayed as UTF8 characters")
  5353. (save-restriction
  5354. (widen)
  5355. (org-decompose-region (point-min) (point-max))
  5356. (message "Entities are displayed plain"))))
  5357. (defvar org-custom-properties-overlays nil
  5358. "List of overlays used for custom properties.")
  5359. (make-variable-buffer-local 'org-custom-properties-overlays)
  5360. (defun org-toggle-custom-properties-visibility ()
  5361. "Display or hide properties in `org-custom-properties'."
  5362. (interactive)
  5363. (if org-custom-properties-overlays
  5364. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5365. (setq org-custom-properties-overlays nil))
  5366. (unless (not org-custom-properties)
  5367. (save-excursion
  5368. (save-restriction
  5369. (widen)
  5370. (goto-char (point-min))
  5371. (while (re-search-forward org-property-re nil t)
  5372. (mapc (lambda(p)
  5373. (when (equal p (substring (match-string 1) 1 -1))
  5374. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5375. (overlay-put o 'invisible t)
  5376. (overlay-put o 'org-custom-property t)
  5377. (push o org-custom-properties-overlays))))
  5378. org-custom-properties)))))))
  5379. (defun org-fontify-entities (limit)
  5380. "Find an entity to fontify."
  5381. (let (ee)
  5382. (when org-pretty-entities
  5383. (catch 'match
  5384. (while (re-search-forward
  5385. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5386. limit t)
  5387. (if (and (not (org-in-indented-comment-line))
  5388. (setq ee (org-entity-get (match-string 1)))
  5389. (= (length (nth 6 ee)) 1))
  5390. (let*
  5391. ((end (if (equal (match-string 2) "{}")
  5392. (match-end 2)
  5393. (match-end 1))))
  5394. (add-text-properties
  5395. (match-beginning 0) end
  5396. (list 'font-lock-fontified t))
  5397. (compose-region (match-beginning 0) end
  5398. (nth 6 ee) nil)
  5399. (backward-char 1)
  5400. (throw 'match t))))
  5401. nil))))
  5402. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5403. "Fontify string S like in Org-mode."
  5404. (with-temp-buffer
  5405. (insert s)
  5406. (let ((org-odd-levels-only odd-levels))
  5407. (org-mode)
  5408. (font-lock-fontify-buffer)
  5409. (buffer-string))))
  5410. (defvar org-m nil)
  5411. (defvar org-l nil)
  5412. (defvar org-f nil)
  5413. (defun org-get-level-face (n)
  5414. "Get the right face for match N in font-lock matching of headlines."
  5415. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5416. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5417. (if org-cycle-level-faces
  5418. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5419. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5420. (cond
  5421. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5422. ((eq n 2) org-f)
  5423. (t (if org-level-color-stars-only nil org-f))))
  5424. (defun org-get-todo-face (kwd)
  5425. "Get the right face for a TODO keyword KWD.
  5426. If KWD is a number, get the corresponding match group."
  5427. (if (numberp kwd) (setq kwd (match-string kwd)))
  5428. (or (org-face-from-face-or-color
  5429. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5430. (and (member kwd org-done-keywords) 'org-done)
  5431. 'org-todo))
  5432. (defun org-face-from-face-or-color (context inherit face-or-color)
  5433. "Create a face list that inherits INHERIT, but sets the foreground color.
  5434. When FACE-OR-COLOR is not a string, just return it."
  5435. (if (stringp face-or-color)
  5436. (list :inherit inherit
  5437. (cdr (assoc context org-faces-easy-properties))
  5438. face-or-color)
  5439. face-or-color))
  5440. (defun org-font-lock-add-tag-faces (limit)
  5441. "Add the special tag faces."
  5442. (when (and org-tag-faces org-tags-special-faces-re)
  5443. (while (re-search-forward org-tags-special-faces-re limit t)
  5444. (add-text-properties (match-beginning 1) (match-end 1)
  5445. (list 'face (org-get-tag-face 1)
  5446. 'font-lock-fontified t))
  5447. (backward-char 1))))
  5448. (defun org-font-lock-add-priority-faces (limit)
  5449. "Add the special priority faces."
  5450. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5451. (when (save-match-data (org-at-heading-p))
  5452. (add-text-properties
  5453. (match-beginning 0) (match-end 0)
  5454. (list 'face (or (org-face-from-face-or-color
  5455. 'priority 'org-special-keyword
  5456. (cdr (assoc (char-after (match-beginning 1))
  5457. org-priority-faces)))
  5458. 'org-special-keyword)
  5459. 'font-lock-fontified t)))))
  5460. (defun org-get-tag-face (kwd)
  5461. "Get the right face for a TODO keyword KWD.
  5462. If KWD is a number, get the corresponding match group."
  5463. (if (numberp kwd) (setq kwd (match-string kwd)))
  5464. (or (org-face-from-face-or-color
  5465. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5466. 'org-tag))
  5467. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5468. "Remove fontification and activation overlays from links."
  5469. (font-lock-default-unfontify-region beg end)
  5470. (let* ((buffer-undo-list t)
  5471. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5472. (inhibit-modification-hooks t)
  5473. deactivate-mark buffer-file-name buffer-file-truename)
  5474. (org-decompose-region beg end)
  5475. (remove-text-properties beg end
  5476. '(mouse-face t keymap t org-linked-text t
  5477. invisible t intangible t
  5478. org-no-flyspell t org-emphasis t))
  5479. (org-remove-font-lock-display-properties beg end)))
  5480. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5481. ((raise 0.3) (height 0.7))
  5482. ((raise -0.5))
  5483. ((raise 0.5)))
  5484. "Display properties for showing superscripts and subscripts.")
  5485. (defun org-remove-font-lock-display-properties (beg end)
  5486. "Remove specific display properties that have been added by font lock.
  5487. The will remove the raise properties that are used to show superscripts
  5488. and subscripts."
  5489. (let (next prop)
  5490. (while (< beg end)
  5491. (setq next (next-single-property-change beg 'display nil end)
  5492. prop (get-text-property beg 'display))
  5493. (if (member prop org-script-display)
  5494. (put-text-property beg next 'display nil))
  5495. (setq beg next))))
  5496. (defun org-raise-scripts (limit)
  5497. "Add raise properties to sub/superscripts."
  5498. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5499. (if (re-search-forward
  5500. (if (eq org-use-sub-superscripts t)
  5501. org-match-substring-regexp
  5502. org-match-substring-with-braces-regexp)
  5503. limit t)
  5504. (let* ((pos (point)) table-p comment-p
  5505. (mpos (match-beginning 3))
  5506. (emph-p (get-text-property mpos 'org-emphasis))
  5507. (link-p (get-text-property mpos 'mouse-face))
  5508. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5509. (goto-char (point-at-bol))
  5510. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5511. comment-p (org-looking-at-p "[ \t]*#"))
  5512. (goto-char pos)
  5513. ;; FIXME: Should we go back one character here, for a_b^c
  5514. ;; (goto-char (1- pos)) ;????????????????????
  5515. (if (or comment-p emph-p link-p keyw-p)
  5516. t
  5517. (put-text-property (match-beginning 3) (match-end 0)
  5518. 'display
  5519. (if (equal (char-after (match-beginning 2)) ?^)
  5520. (nth (if table-p 3 1) org-script-display)
  5521. (nth (if table-p 2 0) org-script-display)))
  5522. (add-text-properties (match-beginning 2) (match-end 2)
  5523. (list 'invisible t
  5524. 'org-dwidth t 'org-dwidth-n 1))
  5525. (if (and (eq (char-after (match-beginning 3)) ?{)
  5526. (eq (char-before (match-end 3)) ?}))
  5527. (progn
  5528. (add-text-properties
  5529. (match-beginning 3) (1+ (match-beginning 3))
  5530. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5531. (add-text-properties
  5532. (1- (match-end 3)) (match-end 3)
  5533. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5534. t)))))
  5535. ;;;; Visibility cycling, including org-goto and indirect buffer
  5536. ;;; Cycling
  5537. (defvar org-cycle-global-status nil)
  5538. (make-variable-buffer-local 'org-cycle-global-status)
  5539. (defvar org-cycle-subtree-status nil)
  5540. (make-variable-buffer-local 'org-cycle-subtree-status)
  5541. ;;;###autoload
  5542. (defvar org-inlinetask-min-level)
  5543. (defun org-cycle (&optional arg)
  5544. "TAB-action and visibility cycling for Org-mode.
  5545. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5546. is outline visibility cycling, but it also invokes other actions
  5547. in special contexts.
  5548. - When this function is called with a prefix argument, rotate the entire
  5549. buffer through 3 states (global cycling)
  5550. 1. OVERVIEW: Show only top-level headlines.
  5551. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5552. 3. SHOW ALL: Show everything.
  5553. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5554. determined by the variable `org-startup-folded', and by any VISIBILITY
  5555. properties in the buffer.
  5556. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5557. including any drawers.
  5558. - When inside a table, re-align the table and move to the next field.
  5559. - When point is at the beginning of a headline, rotate the subtree started
  5560. by this line through 3 different states (local cycling)
  5561. 1. FOLDED: Only the main headline is shown.
  5562. 2. CHILDREN: The main headline and the direct children are shown.
  5563. From this state, you can move to one of the children
  5564. and zoom in further.
  5565. 3. SUBTREE: Show the entire subtree, including body text.
  5566. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5567. - When point is at the beginning of an empty headline and the variable
  5568. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5569. of the headline by demoting and promoting it to likely levels. This
  5570. speeds up creation document structure by pressing TAB once or several
  5571. times right after creating a new headline.
  5572. - When there is a numeric prefix, go up to a heading with level ARG, do
  5573. a `show-subtree' and return to the previous cursor position. If ARG
  5574. is negative, go up that many levels.
  5575. - When point is not at the beginning of a headline, execute the global
  5576. binding for TAB, which is re-indenting the line. See the option
  5577. `org-cycle-emulate-tab' for details.
  5578. - Special case: if point is at the beginning of the buffer and there is
  5579. no headline in line 1, this function will act as if called with prefix arg
  5580. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5581. But only if also the variable `org-cycle-global-at-bob' is t."
  5582. (interactive "P")
  5583. (org-load-modules-maybe)
  5584. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5585. (and org-cycle-level-after-item/entry-creation
  5586. (or (org-cycle-level)
  5587. (org-cycle-item-indentation))))
  5588. (let* ((limit-level
  5589. (or org-cycle-max-level
  5590. (and (boundp 'org-inlinetask-min-level)
  5591. org-inlinetask-min-level
  5592. (1- org-inlinetask-min-level))))
  5593. (nstars (and limit-level
  5594. (if org-odd-levels-only
  5595. (and limit-level (1- (* limit-level 2)))
  5596. limit-level)))
  5597. (org-outline-regexp
  5598. (if (not (derived-mode-p 'org-mode))
  5599. outline-regexp
  5600. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5601. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5602. (not (looking-at org-outline-regexp))))
  5603. (org-cycle-hook
  5604. (if bob-special
  5605. (delq 'org-optimize-window-after-visibility-change
  5606. (copy-sequence org-cycle-hook))
  5607. org-cycle-hook))
  5608. (pos (point)))
  5609. (if (or bob-special (equal arg '(4)))
  5610. ;; special case: use global cycling
  5611. (setq arg t))
  5612. (cond
  5613. ((equal arg '(16))
  5614. (setq last-command 'dummy)
  5615. (org-set-startup-visibility)
  5616. (message "Startup visibility, plus VISIBILITY properties"))
  5617. ((equal arg '(64))
  5618. (show-all)
  5619. (message "Entire buffer visible, including drawers"))
  5620. ;; Table: enter it or move to the next field.
  5621. ((org-at-table-p 'any)
  5622. (if (org-at-table.el-p)
  5623. (message "Use C-c ' to edit table.el tables")
  5624. (if arg (org-table-edit-field t)
  5625. (org-table-justify-field-maybe)
  5626. (call-interactively 'org-table-next-field))))
  5627. ((run-hook-with-args-until-success
  5628. 'org-tab-after-check-for-table-hook))
  5629. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5630. ((eq arg t) (org-cycle-internal-global))
  5631. ;; Drawers: delegate to `org-flag-drawer'.
  5632. ((and org-drawers org-drawer-regexp
  5633. (save-excursion
  5634. (beginning-of-line 1)
  5635. (looking-at org-drawer-regexp)))
  5636. (org-flag-drawer ; toggle block visibility
  5637. (not (get-char-property (match-end 0) 'invisible))))
  5638. ;; Show-subtree, ARG levels up from here.
  5639. ((integerp arg)
  5640. (save-excursion
  5641. (org-back-to-heading)
  5642. (outline-up-heading (if (< arg 0) (- arg)
  5643. (- (funcall outline-level) arg)))
  5644. (org-show-subtree)))
  5645. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5646. ((and (featurep 'org-inlinetask)
  5647. (org-inlinetask-at-task-p)
  5648. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5649. (org-inlinetask-toggle-visibility))
  5650. ((org-try-cdlatex-tab))
  5651. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5652. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5653. (save-excursion (beginning-of-line 1)
  5654. (looking-at org-outline-regexp)))
  5655. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5656. (org-cycle-internal-local))
  5657. ;; From there: TAB emulation and template completion.
  5658. (buffer-read-only (org-back-to-heading))
  5659. ((run-hook-with-args-until-success
  5660. 'org-tab-after-check-for-cycling-hook))
  5661. ((org-try-structure-completion))
  5662. ((run-hook-with-args-until-success
  5663. 'org-tab-before-tab-emulation-hook))
  5664. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5665. (or (not (bolp))
  5666. (not (looking-at org-outline-regexp))))
  5667. (call-interactively (global-key-binding "\t")))
  5668. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5669. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5670. (or (and (eq org-cycle-emulate-tab 'white)
  5671. (= (match-end 0) (point-at-eol)))
  5672. (and (eq org-cycle-emulate-tab 'whitestart)
  5673. (>= (match-end 0) pos))))
  5674. t
  5675. (eq org-cycle-emulate-tab t))
  5676. (call-interactively (global-key-binding "\t")))
  5677. (t (save-excursion
  5678. (org-back-to-heading)
  5679. (org-cycle)))))))
  5680. (defun org-cycle-internal-global ()
  5681. "Do the global cycling action."
  5682. ;; Hack to avoid display of messages for .org attachments in Gnus
  5683. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5684. (cond
  5685. ((and (eq last-command this-command)
  5686. (eq org-cycle-global-status 'overview))
  5687. ;; We just created the overview - now do table of contents
  5688. ;; This can be slow in very large buffers, so indicate action
  5689. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5690. (unless ga (message "CONTENTS..."))
  5691. (org-content)
  5692. (unless ga (message "CONTENTS...done"))
  5693. (setq org-cycle-global-status 'contents)
  5694. (run-hook-with-args 'org-cycle-hook 'contents))
  5695. ((and (eq last-command this-command)
  5696. (eq org-cycle-global-status 'contents))
  5697. ;; We just showed the table of contents - now show everything
  5698. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5699. (show-all)
  5700. (unless ga (message "SHOW ALL"))
  5701. (setq org-cycle-global-status 'all)
  5702. (run-hook-with-args 'org-cycle-hook 'all))
  5703. (t
  5704. ;; Default action: go to overview
  5705. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5706. (org-overview)
  5707. (unless ga (message "OVERVIEW"))
  5708. (setq org-cycle-global-status 'overview)
  5709. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5710. (defun org-cycle-internal-local ()
  5711. "Do the local cycling action."
  5712. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5713. ;; First, determine end of headline (EOH), end of subtree or item
  5714. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5715. (save-excursion
  5716. (if (org-at-item-p)
  5717. (progn
  5718. (beginning-of-line)
  5719. (setq struct (org-list-struct))
  5720. (setq eoh (point-at-eol))
  5721. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5722. (setq has-children (org-list-has-child-p (point) struct)))
  5723. (org-back-to-heading)
  5724. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5725. (setq eos (save-excursion
  5726. (org-end-of-subtree t)
  5727. (unless (eobp)
  5728. (skip-chars-forward " \t\n"))
  5729. (if (eobp) (point) (1- (point)))))
  5730. (setq has-children
  5731. (or (save-excursion
  5732. (let ((level (funcall outline-level)))
  5733. (outline-next-heading)
  5734. (and (org-at-heading-p t)
  5735. (> (funcall outline-level) level))))
  5736. (save-excursion
  5737. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5738. ;; Determine end invisible part of buffer (EOL)
  5739. (beginning-of-line 2)
  5740. ;; XEmacs doesn't have `next-single-char-property-change'
  5741. (if (featurep 'xemacs)
  5742. (while (and (not (eobp)) ;; this is like `next-line'
  5743. (get-char-property (1- (point)) 'invisible))
  5744. (beginning-of-line 2))
  5745. (while (and (not (eobp)) ;; this is like `next-line'
  5746. (get-char-property (1- (point)) 'invisible))
  5747. (goto-char (next-single-char-property-change (point) 'invisible))
  5748. (and (eolp) (beginning-of-line 2))))
  5749. (setq eol (point)))
  5750. ;; Find out what to do next and set `this-command'
  5751. (cond
  5752. ((= eos eoh)
  5753. ;; Nothing is hidden behind this heading
  5754. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5755. (message "EMPTY ENTRY")
  5756. (setq org-cycle-subtree-status nil)
  5757. (save-excursion
  5758. (goto-char eos)
  5759. (outline-next-heading)
  5760. (if (outline-invisible-p) (org-flag-heading nil))))
  5761. ((and (or (>= eol eos)
  5762. (not (string-match "\\S-" (buffer-substring eol eos))))
  5763. (or has-children
  5764. (not (setq children-skipped
  5765. org-cycle-skip-children-state-if-no-children))))
  5766. ;; Entire subtree is hidden in one line: children view
  5767. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5768. (if (org-at-item-p)
  5769. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5770. (org-show-entry)
  5771. (org-with-limited-levels (show-children))
  5772. ;; FIXME: This slows down the func way too much.
  5773. ;; How keep drawers hidden in subtree anyway?
  5774. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5775. ;; (org-cycle-hide-drawers 'subtree))
  5776. ;; Fold every list in subtree to top-level items.
  5777. (when (eq org-cycle-include-plain-lists 'integrate)
  5778. (save-excursion
  5779. (org-back-to-heading)
  5780. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5781. (beginning-of-line 1)
  5782. (let* ((struct (org-list-struct))
  5783. (prevs (org-list-prevs-alist struct))
  5784. (end (org-list-get-bottom-point struct)))
  5785. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5786. (org-list-get-all-items (point) struct prevs))
  5787. (goto-char end))))))
  5788. (message "CHILDREN")
  5789. (save-excursion
  5790. (goto-char eos)
  5791. (outline-next-heading)
  5792. (if (outline-invisible-p) (org-flag-heading nil)))
  5793. (setq org-cycle-subtree-status 'children)
  5794. (run-hook-with-args 'org-cycle-hook 'children))
  5795. ((or children-skipped
  5796. (and (eq last-command this-command)
  5797. (eq org-cycle-subtree-status 'children)))
  5798. ;; We just showed the children, or no children are there,
  5799. ;; now show everything.
  5800. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5801. (outline-flag-region eoh eos nil)
  5802. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5803. (setq org-cycle-subtree-status 'subtree)
  5804. (run-hook-with-args 'org-cycle-hook 'subtree))
  5805. (t
  5806. ;; Default action: hide the subtree.
  5807. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5808. (outline-flag-region eoh eos t)
  5809. (message "FOLDED")
  5810. (setq org-cycle-subtree-status 'folded)
  5811. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5812. ;;;###autoload
  5813. (defun org-global-cycle (&optional arg)
  5814. "Cycle the global visibility. For details see `org-cycle'.
  5815. With \\[universal-argument] prefix arg, switch to startup visibility.
  5816. With a numeric prefix, show all headlines up to that level."
  5817. (interactive "P")
  5818. (let ((org-cycle-include-plain-lists
  5819. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5820. (cond
  5821. ((integerp arg)
  5822. (show-all)
  5823. (hide-sublevels arg)
  5824. (setq org-cycle-global-status 'contents))
  5825. ((equal arg '(4))
  5826. (org-set-startup-visibility)
  5827. (message "Startup visibility, plus VISIBILITY properties."))
  5828. (t
  5829. (org-cycle '(4))))))
  5830. (defun org-set-startup-visibility ()
  5831. "Set the visibility required by startup options and properties."
  5832. (cond
  5833. ((eq org-startup-folded t)
  5834. (org-cycle '(4)))
  5835. ((eq org-startup-folded 'content)
  5836. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5837. (org-cycle '(4)) (org-cycle '(4)))))
  5838. (unless (eq org-startup-folded 'showeverything)
  5839. (if org-hide-block-startup (org-hide-block-all))
  5840. (org-set-visibility-according-to-property 'no-cleanup)
  5841. (org-cycle-hide-archived-subtrees 'all)
  5842. (org-cycle-hide-drawers 'all)
  5843. (org-cycle-show-empty-lines t)))
  5844. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5845. "Switch subtree visibilities according to :VISIBILITY: property."
  5846. (interactive)
  5847. (let (org-show-entry-below state)
  5848. (save-excursion
  5849. (goto-char (point-min))
  5850. (while (re-search-forward
  5851. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5852. nil t)
  5853. (setq state (match-string 1))
  5854. (save-excursion
  5855. (org-back-to-heading t)
  5856. (hide-subtree)
  5857. (org-reveal)
  5858. (cond
  5859. ((equal state '("fold" "folded"))
  5860. (hide-subtree))
  5861. ((equal state "children")
  5862. (org-show-hidden-entry)
  5863. (show-children))
  5864. ((equal state "content")
  5865. (save-excursion
  5866. (save-restriction
  5867. (org-narrow-to-subtree)
  5868. (org-content))))
  5869. ((member state '("all" "showall"))
  5870. (show-subtree)))))
  5871. (unless no-cleanup
  5872. (org-cycle-hide-archived-subtrees 'all)
  5873. (org-cycle-hide-drawers 'all)
  5874. (org-cycle-show-empty-lines 'all)))))
  5875. ;; This function uses outline-regexp instead of the more fundamental
  5876. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5877. ;; buffers, where outline-regexp is needed.
  5878. (defun org-overview ()
  5879. "Switch to overview mode, showing only top-level headlines.
  5880. Really, this shows all headlines with level equal or greater than the level
  5881. of the first headline in the buffer. This is important, because if the
  5882. first headline is not level one, then (hide-sublevels 1) gives confusing
  5883. results."
  5884. (interactive)
  5885. (let ((level (save-excursion
  5886. (goto-char (point-min))
  5887. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5888. (progn
  5889. (goto-char (match-beginning 0))
  5890. (funcall outline-level))))))
  5891. (and level (hide-sublevels level))))
  5892. (defun org-content (&optional arg)
  5893. "Show all headlines in the buffer, like a table of contents.
  5894. With numerical argument N, show content up to level N."
  5895. (interactive "P")
  5896. (save-excursion
  5897. ;; Visit all headings and show their offspring
  5898. (and (integerp arg) (org-overview))
  5899. (goto-char (point-max))
  5900. (catch 'exit
  5901. (while (and (progn (condition-case nil
  5902. (outline-previous-visible-heading 1)
  5903. (error (goto-char (point-min))))
  5904. t)
  5905. (looking-at org-outline-regexp))
  5906. (if (integerp arg)
  5907. (show-children (1- arg))
  5908. (show-branches))
  5909. (if (bobp) (throw 'exit nil))))))
  5910. (defun org-optimize-window-after-visibility-change (state)
  5911. "Adjust the window after a change in outline visibility.
  5912. This function is the default value of the hook `org-cycle-hook'."
  5913. (when (get-buffer-window (current-buffer))
  5914. (cond
  5915. ((eq state 'content) nil)
  5916. ((eq state 'all) nil)
  5917. ((eq state 'folded) nil)
  5918. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5919. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5920. (defun org-remove-empty-overlays-at (pos)
  5921. "Remove outline overlays that do not contain non-white stuff."
  5922. (mapc
  5923. (lambda (o)
  5924. (and (eq 'outline (overlay-get o 'invisible))
  5925. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5926. (overlay-end o))))
  5927. (delete-overlay o)))
  5928. (overlays-at pos)))
  5929. (defun org-clean-visibility-after-subtree-move ()
  5930. "Fix visibility issues after moving a subtree."
  5931. ;; First, find a reasonable region to look at:
  5932. ;; Start two siblings above, end three below
  5933. (let* ((beg (save-excursion
  5934. (and (org-get-last-sibling)
  5935. (org-get-last-sibling))
  5936. (point)))
  5937. (end (save-excursion
  5938. (and (org-get-next-sibling)
  5939. (org-get-next-sibling)
  5940. (org-get-next-sibling))
  5941. (if (org-at-heading-p)
  5942. (point-at-eol)
  5943. (point))))
  5944. (level (looking-at "\\*+"))
  5945. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5946. (save-excursion
  5947. (save-restriction
  5948. (narrow-to-region beg end)
  5949. (when re
  5950. ;; Properly fold already folded siblings
  5951. (goto-char (point-min))
  5952. (while (re-search-forward re nil t)
  5953. (if (and (not (outline-invisible-p))
  5954. (save-excursion
  5955. (goto-char (point-at-eol)) (outline-invisible-p)))
  5956. (hide-entry))))
  5957. (org-cycle-show-empty-lines 'overview)
  5958. (org-cycle-hide-drawers 'overview)))))
  5959. (defun org-cycle-show-empty-lines (state)
  5960. "Show empty lines above all visible headlines.
  5961. The region to be covered depends on STATE when called through
  5962. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5963. entire buffer covered. Note that an empty line is only shown if there
  5964. are at least `org-cycle-separator-lines' empty lines before the headline."
  5965. (when (not (= org-cycle-separator-lines 0))
  5966. (save-excursion
  5967. (let* ((n (abs org-cycle-separator-lines))
  5968. (re (cond
  5969. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5970. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5971. (t (let ((ns (number-to-string (- n 2))))
  5972. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5973. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5974. beg end b e)
  5975. (cond
  5976. ((memq state '(overview contents t))
  5977. (setq beg (point-min) end (point-max)))
  5978. ((memq state '(children folded))
  5979. (setq beg (point) end (progn (org-end-of-subtree t t)
  5980. (beginning-of-line 2)
  5981. (point)))))
  5982. (when beg
  5983. (goto-char beg)
  5984. (while (re-search-forward re end t)
  5985. (unless (get-char-property (match-end 1) 'invisible)
  5986. (setq e (match-end 1))
  5987. (if (< org-cycle-separator-lines 0)
  5988. (setq b (save-excursion
  5989. (goto-char (match-beginning 0))
  5990. (org-back-over-empty-lines)
  5991. (if (save-excursion
  5992. (goto-char (max (point-min) (1- (point))))
  5993. (org-at-heading-p))
  5994. (1- (point))
  5995. (point))))
  5996. (setq b (match-beginning 1)))
  5997. (outline-flag-region b e nil)))))))
  5998. ;; Never hide empty lines at the end of the file.
  5999. (save-excursion
  6000. (goto-char (point-max))
  6001. (outline-previous-heading)
  6002. (outline-end-of-heading)
  6003. (if (and (looking-at "[ \t\n]+")
  6004. (= (match-end 0) (point-max)))
  6005. (outline-flag-region (point) (match-end 0) nil))))
  6006. (defun org-show-empty-lines-in-parent ()
  6007. "Move to the parent and re-show empty lines before visible headlines."
  6008. (save-excursion
  6009. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6010. (org-cycle-show-empty-lines context))))
  6011. (defun org-files-list ()
  6012. "Return `org-agenda-files' list, plus all open org-mode files.
  6013. This is useful for operations that need to scan all of a user's
  6014. open and agenda-wise Org files."
  6015. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6016. (dolist (buf (buffer-list))
  6017. (with-current-buffer buf
  6018. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6019. (let ((file (expand-file-name (buffer-file-name))))
  6020. (unless (member file files)
  6021. (push file files))))))
  6022. files))
  6023. (defsubst org-entry-beginning-position ()
  6024. "Return the beginning position of the current entry."
  6025. (save-excursion (outline-back-to-heading t) (point)))
  6026. (defsubst org-entry-end-position ()
  6027. "Return the end position of the current entry."
  6028. (save-excursion (outline-next-heading) (point)))
  6029. (defun org-cycle-hide-drawers (state)
  6030. "Re-hide all drawers after a visibility state change."
  6031. (when (and (derived-mode-p 'org-mode)
  6032. (not (memq state '(overview folded contents))))
  6033. (save-excursion
  6034. (let* ((globalp (memq state '(contents all)))
  6035. (beg (if globalp (point-min) (point)))
  6036. (end (if globalp (point-max)
  6037. (if (eq state 'children)
  6038. (save-excursion (outline-next-heading) (point))
  6039. (org-end-of-subtree t)))))
  6040. (goto-char beg)
  6041. (while (re-search-forward org-drawer-regexp end t)
  6042. (org-flag-drawer t))))))
  6043. (defun org-cycle-hide-inline-tasks (state)
  6044. "Re-hide inline task when switching to 'contents visibility state."
  6045. (when (and (eq state 'contents)
  6046. (boundp 'org-inlinetask-min-level)
  6047. org-inlinetask-min-level)
  6048. (hide-sublevels (1- org-inlinetask-min-level))))
  6049. (defun org-flag-drawer (flag)
  6050. "When FLAG is non-nil, hide the drawer we are within.
  6051. Otherwise make it visible."
  6052. (save-excursion
  6053. (beginning-of-line 1)
  6054. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6055. (let ((b (match-end 0)))
  6056. (if (re-search-forward
  6057. "^[ \t]*:END:"
  6058. (save-excursion (outline-next-heading) (point)) t)
  6059. (outline-flag-region b (point-at-eol) flag)
  6060. (error ":END: line missing at position %s" b))))))
  6061. (defun org-subtree-end-visible-p ()
  6062. "Is the end of the current subtree visible?"
  6063. (pos-visible-in-window-p
  6064. (save-excursion (org-end-of-subtree t) (point))))
  6065. (defun org-first-headline-recenter (&optional N)
  6066. "Move cursor to the first headline and recenter the headline.
  6067. Optional argument N means put the headline into the Nth line of the window."
  6068. (goto-char (point-min))
  6069. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6070. (beginning-of-line)
  6071. (recenter (prefix-numeric-value N))))
  6072. ;;; Saving and restoring visibility
  6073. (defun org-outline-overlay-data (&optional use-markers)
  6074. "Return a list of the locations of all outline overlays.
  6075. These are overlays with the `invisible' property value `outline'.
  6076. The return value is a list of cons cells, with start and stop
  6077. positions for each overlay.
  6078. If USE-MARKERS is set, return the positions as markers."
  6079. (let (beg end)
  6080. (save-excursion
  6081. (save-restriction
  6082. (widen)
  6083. (delq nil
  6084. (mapcar (lambda (o)
  6085. (when (eq (overlay-get o 'invisible) 'outline)
  6086. (setq beg (overlay-start o)
  6087. end (overlay-end o))
  6088. (and beg end (> end beg)
  6089. (if use-markers
  6090. (cons (move-marker (make-marker) beg)
  6091. (move-marker (make-marker) end))
  6092. (cons beg end)))))
  6093. (overlays-in (point-min) (point-max))))))))
  6094. (defun org-set-outline-overlay-data (data)
  6095. "Create visibility overlays for all positions in DATA.
  6096. DATA should have been made by `org-outline-overlay-data'."
  6097. (let (o)
  6098. (save-excursion
  6099. (save-restriction
  6100. (widen)
  6101. (show-all)
  6102. (mapc (lambda (c)
  6103. (outline-flag-region (car c) (cdr c) t))
  6104. data)))))
  6105. ;;; Folding of blocks
  6106. (defvar org-hide-block-overlays nil
  6107. "Overlays hiding blocks.")
  6108. (make-variable-buffer-local 'org-hide-block-overlays)
  6109. (defun org-block-map (function &optional start end)
  6110. "Call FUNCTION at the head of all source blocks in the current buffer.
  6111. Optional arguments START and END can be used to limit the range."
  6112. (let ((start (or start (point-min)))
  6113. (end (or end (point-max))))
  6114. (save-excursion
  6115. (goto-char start)
  6116. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6117. (save-excursion
  6118. (save-match-data
  6119. (goto-char (match-beginning 0))
  6120. (funcall function)))))))
  6121. (defun org-hide-block-toggle-all ()
  6122. "Toggle the visibility of all blocks in the current buffer."
  6123. (org-block-map #'org-hide-block-toggle))
  6124. (defun org-hide-block-all ()
  6125. "Fold all blocks in the current buffer."
  6126. (interactive)
  6127. (org-show-block-all)
  6128. (org-block-map #'org-hide-block-toggle-maybe))
  6129. (defun org-show-block-all ()
  6130. "Unfold all blocks in the current buffer."
  6131. (interactive)
  6132. (mapc 'delete-overlay org-hide-block-overlays)
  6133. (setq org-hide-block-overlays nil))
  6134. (defun org-hide-block-toggle-maybe ()
  6135. "Toggle visibility of block at point."
  6136. (interactive)
  6137. (let ((case-fold-search t))
  6138. (if (save-excursion
  6139. (beginning-of-line 1)
  6140. (looking-at org-block-regexp))
  6141. (progn (org-hide-block-toggle)
  6142. t) ;; to signal that we took action
  6143. nil))) ;; to signal that we did not
  6144. (defun org-hide-block-toggle (&optional force)
  6145. "Toggle the visibility of the current block."
  6146. (interactive)
  6147. (save-excursion
  6148. (beginning-of-line)
  6149. (if (re-search-forward org-block-regexp nil t)
  6150. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6151. (end (match-end 0)) ;; end of entire body
  6152. ov)
  6153. (if (memq t (mapcar (lambda (overlay)
  6154. (eq (overlay-get overlay 'invisible)
  6155. 'org-hide-block))
  6156. (overlays-at start)))
  6157. (if (or (not force) (eq force 'off))
  6158. (mapc (lambda (ov)
  6159. (when (member ov org-hide-block-overlays)
  6160. (setq org-hide-block-overlays
  6161. (delq ov org-hide-block-overlays)))
  6162. (when (eq (overlay-get ov 'invisible)
  6163. 'org-hide-block)
  6164. (delete-overlay ov)))
  6165. (overlays-at start)))
  6166. (setq ov (make-overlay start end))
  6167. (overlay-put ov 'invisible 'org-hide-block)
  6168. ;; make the block accessible to isearch
  6169. (overlay-put
  6170. ov 'isearch-open-invisible
  6171. (lambda (ov)
  6172. (when (member ov org-hide-block-overlays)
  6173. (setq org-hide-block-overlays
  6174. (delq ov org-hide-block-overlays)))
  6175. (when (eq (overlay-get ov 'invisible)
  6176. 'org-hide-block)
  6177. (delete-overlay ov))))
  6178. (push ov org-hide-block-overlays)))
  6179. (error "Not looking at a source block"))))
  6180. ;; org-tab-after-check-for-cycling-hook
  6181. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6182. ;; Remove overlays when changing major mode
  6183. (add-hook 'org-mode-hook
  6184. (lambda () (org-add-hook 'change-major-mode-hook
  6185. 'org-show-block-all 'append 'local)))
  6186. ;;; Org-goto
  6187. (defvar org-goto-window-configuration nil)
  6188. (defvar org-goto-marker nil)
  6189. (defvar org-goto-map
  6190. (let ((map (make-sparse-keymap)))
  6191. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  6192. (while (setq cmd (pop cmds))
  6193. (substitute-key-definition cmd cmd map global-map)))
  6194. (suppress-keymap map)
  6195. (org-defkey map "\C-m" 'org-goto-ret)
  6196. (org-defkey map [(return)] 'org-goto-ret)
  6197. (org-defkey map [(left)] 'org-goto-left)
  6198. (org-defkey map [(right)] 'org-goto-right)
  6199. (org-defkey map [(control ?g)] 'org-goto-quit)
  6200. (org-defkey map "\C-i" 'org-cycle)
  6201. (org-defkey map [(tab)] 'org-cycle)
  6202. (org-defkey map [(down)] 'outline-next-visible-heading)
  6203. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6204. (if org-goto-auto-isearch
  6205. (if (fboundp 'define-key-after)
  6206. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6207. nil)
  6208. (org-defkey map "q" 'org-goto-quit)
  6209. (org-defkey map "n" 'outline-next-visible-heading)
  6210. (org-defkey map "p" 'outline-previous-visible-heading)
  6211. (org-defkey map "f" 'outline-forward-same-level)
  6212. (org-defkey map "b" 'outline-backward-same-level)
  6213. (org-defkey map "u" 'outline-up-heading))
  6214. (org-defkey map "/" 'org-occur)
  6215. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6216. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6217. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6218. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6219. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6220. map))
  6221. (defconst org-goto-help
  6222. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  6223. RET=jump to location [Q]uit and return to previous location
  6224. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6225. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6226. ;; FIXME: Docstring does not mention both interfaces
  6227. (defun org-goto (&optional alternative-interface)
  6228. "Look up a different location in the current file, keeping current visibility.
  6229. When you want look-up or go to a different location in a
  6230. document, the fastest way is often to fold the entire buffer and
  6231. then dive into the tree. This method has the disadvantage, that
  6232. the previous location will be folded, which may not be what you
  6233. want.
  6234. This command works around this by showing a copy of the current
  6235. buffer in an indirect buffer, in overview mode. You can dive
  6236. into the tree in that copy, use org-occur and incremental search
  6237. to find a location. When pressing RET or `Q', the command
  6238. returns to the original buffer in which the visibility is still
  6239. unchanged. After RET it will also jump to the location selected
  6240. in the indirect buffer and expose the headline hierarchy above.
  6241. With a prefix argument, use the alternative interface: e.g. if
  6242. `org-goto-interface' is 'outline use 'outline-path-completion."
  6243. (interactive "P")
  6244. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6245. (org-refile-use-outline-path t)
  6246. (org-refile-target-verify-function nil)
  6247. (interface
  6248. (if (not alternative-interface)
  6249. org-goto-interface
  6250. (if (eq org-goto-interface 'outline)
  6251. 'outline-path-completion
  6252. 'outline)))
  6253. (org-goto-start-pos (point))
  6254. (selected-point
  6255. (if (eq interface 'outline)
  6256. (car (org-get-location (current-buffer) org-goto-help))
  6257. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6258. (org-refile-check-position pa)
  6259. (nth 3 pa)))))
  6260. (if selected-point
  6261. (progn
  6262. (org-mark-ring-push org-goto-start-pos)
  6263. (goto-char selected-point)
  6264. (if (or (outline-invisible-p) (org-invisible-p2))
  6265. (org-show-context 'org-goto)))
  6266. (message "Quit"))))
  6267. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6268. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6269. (defvar org-goto-local-auto-isearch-map) ; defined below
  6270. (defun org-get-location (buf help)
  6271. "Let the user select a location in the Org-mode buffer BUF.
  6272. This function uses a recursive edit. It returns the selected position
  6273. or nil."
  6274. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6275. (isearch-hide-immediately nil)
  6276. (isearch-search-fun-function
  6277. (lambda () 'org-goto-local-search-headings))
  6278. (org-goto-selected-point org-goto-exit-command)
  6279. (pop-up-frames nil)
  6280. (special-display-buffer-names nil)
  6281. (special-display-regexps nil)
  6282. (special-display-function nil))
  6283. (save-excursion
  6284. (save-window-excursion
  6285. (delete-other-windows)
  6286. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6287. (org-pop-to-buffer-same-window
  6288. (condition-case nil
  6289. (make-indirect-buffer (current-buffer) "*org-goto*")
  6290. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6291. (with-output-to-temp-buffer "*Help*"
  6292. (princ help))
  6293. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  6294. (setq buffer-read-only nil)
  6295. (let ((org-startup-truncated t)
  6296. (org-startup-folded nil)
  6297. (org-startup-align-all-tables nil))
  6298. (org-mode)
  6299. (org-overview))
  6300. (setq buffer-read-only t)
  6301. (if (and (boundp 'org-goto-start-pos)
  6302. (integer-or-marker-p org-goto-start-pos))
  6303. (let ((org-show-hierarchy-above t)
  6304. (org-show-siblings t)
  6305. (org-show-following-heading t))
  6306. (goto-char org-goto-start-pos)
  6307. (and (outline-invisible-p) (org-show-context)))
  6308. (goto-char (point-min)))
  6309. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6310. (message "Select location and press RET")
  6311. (use-local-map org-goto-map)
  6312. (recursive-edit)
  6313. ))
  6314. (kill-buffer "*org-goto*")
  6315. (cons org-goto-selected-point org-goto-exit-command)))
  6316. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6317. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6318. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6319. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6320. (defun org-goto-local-search-headings (string bound noerror)
  6321. "Search and make sure that any matches are in headlines."
  6322. (catch 'return
  6323. (while (if isearch-forward
  6324. (search-forward string bound noerror)
  6325. (search-backward string bound noerror))
  6326. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6327. (and (member :headline context)
  6328. (not (member :tags context))))
  6329. (throw 'return (point))))))
  6330. (defun org-goto-local-auto-isearch ()
  6331. "Start isearch."
  6332. (interactive)
  6333. (goto-char (point-min))
  6334. (let ((keys (this-command-keys)))
  6335. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6336. (isearch-mode t)
  6337. (isearch-process-search-char (string-to-char keys)))))
  6338. (defun org-goto-ret (&optional arg)
  6339. "Finish `org-goto' by going to the new location."
  6340. (interactive "P")
  6341. (setq org-goto-selected-point (point)
  6342. org-goto-exit-command 'return)
  6343. (throw 'exit nil))
  6344. (defun org-goto-left ()
  6345. "Finish `org-goto' by going to the new location."
  6346. (interactive)
  6347. (if (org-at-heading-p)
  6348. (progn
  6349. (beginning-of-line 1)
  6350. (setq org-goto-selected-point (point)
  6351. org-goto-exit-command 'left)
  6352. (throw 'exit nil))
  6353. (error "Not on a heading")))
  6354. (defun org-goto-right ()
  6355. "Finish `org-goto' by going to the new location."
  6356. (interactive)
  6357. (if (org-at-heading-p)
  6358. (progn
  6359. (setq org-goto-selected-point (point)
  6360. org-goto-exit-command 'right)
  6361. (throw 'exit nil))
  6362. (error "Not on a heading")))
  6363. (defun org-goto-quit ()
  6364. "Finish `org-goto' without cursor motion."
  6365. (interactive)
  6366. (setq org-goto-selected-point nil)
  6367. (setq org-goto-exit-command 'quit)
  6368. (throw 'exit nil))
  6369. ;;; Indirect buffer display of subtrees
  6370. (defvar org-indirect-dedicated-frame nil
  6371. "This is the frame being used for indirect tree display.")
  6372. (defvar org-last-indirect-buffer nil)
  6373. (defun org-tree-to-indirect-buffer (&optional arg)
  6374. "Create indirect buffer and narrow it to current subtree.
  6375. With a numerical prefix ARG, go up to this level and then take that tree.
  6376. If ARG is negative, go up that many levels.
  6377. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6378. indirect buffer previously made with this command, to avoid proliferation of
  6379. indirect buffers. However, when you call the command with a \
  6380. \\[universal-argument] prefix, or
  6381. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6382. is kept so that you can work with several indirect buffers at the same time.
  6383. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6384. \\[universal-argument] prefix also
  6385. requests that a new frame be made for the new buffer, so that the dedicated
  6386. frame is not changed."
  6387. (interactive "P")
  6388. (let ((cbuf (current-buffer))
  6389. (cwin (selected-window))
  6390. (pos (point))
  6391. beg end level heading ibuf)
  6392. (save-excursion
  6393. (org-back-to-heading t)
  6394. (when (numberp arg)
  6395. (setq level (org-outline-level))
  6396. (if (< arg 0) (setq arg (+ level arg)))
  6397. (while (> (setq level (org-outline-level)) arg)
  6398. (org-up-heading-safe)))
  6399. (setq beg (point)
  6400. heading (org-get-heading))
  6401. (org-end-of-subtree t t)
  6402. (if (org-at-heading-p) (backward-char 1))
  6403. (setq end (point)))
  6404. (if (and (buffer-live-p org-last-indirect-buffer)
  6405. (not (eq org-indirect-buffer-display 'new-frame))
  6406. (not arg))
  6407. (kill-buffer org-last-indirect-buffer))
  6408. (setq ibuf (org-get-indirect-buffer cbuf)
  6409. org-last-indirect-buffer ibuf)
  6410. (cond
  6411. ((or (eq org-indirect-buffer-display 'new-frame)
  6412. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6413. (select-frame (make-frame))
  6414. (delete-other-windows)
  6415. (org-pop-to-buffer-same-window ibuf)
  6416. (org-set-frame-title heading))
  6417. ((eq org-indirect-buffer-display 'dedicated-frame)
  6418. (raise-frame
  6419. (select-frame (or (and org-indirect-dedicated-frame
  6420. (frame-live-p org-indirect-dedicated-frame)
  6421. org-indirect-dedicated-frame)
  6422. (setq org-indirect-dedicated-frame (make-frame)))))
  6423. (delete-other-windows)
  6424. (org-pop-to-buffer-same-window ibuf)
  6425. (org-set-frame-title (concat "Indirect: " heading)))
  6426. ((eq org-indirect-buffer-display 'current-window)
  6427. (org-pop-to-buffer-same-window ibuf))
  6428. ((eq org-indirect-buffer-display 'other-window)
  6429. (pop-to-buffer ibuf))
  6430. (t (error "Invalid value")))
  6431. (if (featurep 'xemacs)
  6432. (save-excursion (org-mode) (turn-on-font-lock)))
  6433. (narrow-to-region beg end)
  6434. (show-all)
  6435. (goto-char pos)
  6436. (run-hook-with-args 'org-cycle-hook 'all)
  6437. (and (window-live-p cwin) (select-window cwin))))
  6438. (defun org-get-indirect-buffer (&optional buffer)
  6439. (setq buffer (or buffer (current-buffer)))
  6440. (let ((n 1) (base (buffer-name buffer)) bname)
  6441. (while (buffer-live-p
  6442. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6443. (setq n (1+ n)))
  6444. (condition-case nil
  6445. (make-indirect-buffer buffer bname 'clone)
  6446. (error (make-indirect-buffer buffer bname)))))
  6447. (defun org-set-frame-title (title)
  6448. "Set the title of the current frame to the string TITLE."
  6449. ;; FIXME: how to name a single frame in XEmacs???
  6450. (unless (featurep 'xemacs)
  6451. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6452. ;;;; Structure editing
  6453. ;;; Inserting headlines
  6454. (defun org-previous-line-empty-p ()
  6455. (save-excursion
  6456. (and (not (bobp))
  6457. (or (beginning-of-line 0) t)
  6458. (save-match-data
  6459. (looking-at "[ \t]*$")))))
  6460. (defun org-insert-heading (&optional force-heading invisible-ok)
  6461. "Insert a new heading or item with same depth at point.
  6462. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6463. If point is at the beginning of a headline, insert a sibling before the
  6464. current headline. If point is not at the beginning, split the line,
  6465. create the new headline with the text in the current line after point
  6466. \(but see also the variable `org-M-RET-may-split-line').
  6467. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6468. This is important for non-interactive uses of the command."
  6469. (interactive "P")
  6470. (if (or (= (buffer-size) 0)
  6471. (and (not (save-excursion
  6472. (and (ignore-errors (org-back-to-heading invisible-ok))
  6473. (org-at-heading-p))))
  6474. (or force-heading (not (org-in-item-p)))))
  6475. (progn
  6476. (insert "\n* ")
  6477. (run-hooks 'org-insert-heading-hook))
  6478. (when (or force-heading (not (org-insert-item)))
  6479. (let* ((empty-line-p nil)
  6480. (level nil)
  6481. (on-heading (org-at-heading-p))
  6482. (head (save-excursion
  6483. (condition-case nil
  6484. (progn
  6485. (org-back-to-heading invisible-ok)
  6486. (when (and (not on-heading)
  6487. (featurep 'org-inlinetask)
  6488. (integerp org-inlinetask-min-level)
  6489. (>= (length (match-string 0))
  6490. org-inlinetask-min-level))
  6491. ;; Find a heading level before the inline task
  6492. (while (and (setq level (org-up-heading-safe))
  6493. (>= level org-inlinetask-min-level)))
  6494. (if (org-at-heading-p)
  6495. (org-back-to-heading invisible-ok)
  6496. (error "This should not happen")))
  6497. (setq empty-line-p (org-previous-line-empty-p))
  6498. (match-string 0))
  6499. (error "*"))))
  6500. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6501. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6502. pos hide-previous previous-pos)
  6503. (cond
  6504. ((and (org-at-heading-p) (bolp)
  6505. (or (bobp)
  6506. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6507. ;; insert before the current line
  6508. (open-line (if blank 2 1)))
  6509. ((and (bolp)
  6510. (not org-insert-heading-respect-content)
  6511. (or (bobp)
  6512. (save-excursion
  6513. (backward-char 1) (not (outline-invisible-p)))))
  6514. ;; insert right here
  6515. nil)
  6516. (t
  6517. ;; somewhere in the line
  6518. (save-excursion
  6519. (setq previous-pos (point-at-bol))
  6520. (end-of-line)
  6521. (setq hide-previous (outline-invisible-p)))
  6522. (and org-insert-heading-respect-content (org-show-subtree))
  6523. (let ((split
  6524. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6525. (save-excursion
  6526. (let ((p (point)))
  6527. (goto-char (point-at-bol))
  6528. (and (looking-at org-complex-heading-regexp)
  6529. (match-beginning 4)
  6530. (> p (match-beginning 4)))))))
  6531. tags pos)
  6532. (cond
  6533. (org-insert-heading-respect-content
  6534. (org-end-of-subtree nil t)
  6535. (when (featurep 'org-inlinetask)
  6536. (while (and (not (eobp))
  6537. (looking-at "\\(\\*+\\)[ \t]+")
  6538. (>= (length (match-string 1))
  6539. org-inlinetask-min-level))
  6540. (org-end-of-subtree nil t)))
  6541. (or (bolp) (newline))
  6542. (or (org-previous-line-empty-p)
  6543. (and blank (newline)))
  6544. (open-line 1))
  6545. ((org-at-heading-p)
  6546. (when hide-previous
  6547. (show-children)
  6548. (org-show-entry))
  6549. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6550. (setq tags (and (match-end 2) (match-string 2)))
  6551. (and (match-end 1)
  6552. (delete-region (match-beginning 1) (match-end 1)))
  6553. (setq pos (point-at-bol))
  6554. (or split (end-of-line 1))
  6555. (delete-horizontal-space)
  6556. (if (string-match "\\`\\*+\\'"
  6557. (buffer-substring (point-at-bol) (point)))
  6558. (insert " "))
  6559. (newline (if blank 2 1))
  6560. (when tags
  6561. (save-excursion
  6562. (goto-char pos)
  6563. (end-of-line 1)
  6564. (insert " " tags)
  6565. (org-set-tags nil 'align))))
  6566. (t
  6567. (or split (end-of-line 1))
  6568. (newline (if blank 2 1)))))))
  6569. (insert head) (just-one-space)
  6570. (setq pos (point))
  6571. (end-of-line 1)
  6572. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6573. (when (and org-insert-heading-respect-content hide-previous)
  6574. (save-excursion
  6575. (goto-char previous-pos)
  6576. (hide-subtree)))
  6577. (run-hooks 'org-insert-heading-hook)))))
  6578. (defun org-get-heading (&optional no-tags no-todo)
  6579. "Return the heading of the current entry, without the stars.
  6580. When NO-TAGS is non-nil, don't include tags.
  6581. When NO-TODO is non-nil, don't include TODO keywords."
  6582. (save-excursion
  6583. (org-back-to-heading t)
  6584. (cond
  6585. ((and no-tags no-todo)
  6586. (looking-at org-complex-heading-regexp)
  6587. (match-string 4))
  6588. (no-tags
  6589. (looking-at (concat org-outline-regexp
  6590. "\\(.*?\\)"
  6591. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6592. (match-string 1))
  6593. (no-todo
  6594. (looking-at org-todo-line-regexp)
  6595. (match-string 3))
  6596. (t (looking-at org-heading-regexp)
  6597. (match-string 2)))))
  6598. (defun org-heading-components ()
  6599. "Return the components of the current heading.
  6600. This is a list with the following elements:
  6601. - the level as an integer
  6602. - the reduced level, different if `org-odd-levels-only' is set.
  6603. - the TODO keyword, or nil
  6604. - the priority character, like ?A, or nil if no priority is given
  6605. - the headline text itself, or the tags string if no headline text
  6606. - the tags string, or nil."
  6607. (save-excursion
  6608. (org-back-to-heading t)
  6609. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6610. (list (length (match-string 1))
  6611. (org-reduced-level (length (match-string 1)))
  6612. (org-match-string-no-properties 2)
  6613. (and (match-end 3) (aref (match-string 3) 2))
  6614. (org-match-string-no-properties 4)
  6615. (org-match-string-no-properties 5)))))
  6616. (defun org-get-entry ()
  6617. "Get the entry text, after heading, entire subtree."
  6618. (save-excursion
  6619. (org-back-to-heading t)
  6620. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6621. (defun org-insert-heading-after-current ()
  6622. "Insert a new heading with same level as current, after current subtree."
  6623. (interactive)
  6624. (org-back-to-heading)
  6625. (org-insert-heading)
  6626. (org-move-subtree-down)
  6627. (end-of-line 1))
  6628. (defun org-insert-heading-respect-content ()
  6629. (interactive)
  6630. (let ((org-insert-heading-respect-content t))
  6631. (org-insert-heading t)))
  6632. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6633. (interactive "P")
  6634. (let ((org-insert-heading-respect-content t))
  6635. (org-insert-todo-heading force-state t)))
  6636. (defun org-insert-todo-heading (arg &optional force-heading)
  6637. "Insert a new heading with the same level and TODO state as current heading.
  6638. If the heading has no TODO state, or if the state is DONE, use the first
  6639. state (TODO by default). Also with prefix arg, force first state."
  6640. (interactive "P")
  6641. (when (or force-heading (not (org-insert-item 'checkbox)))
  6642. (org-insert-heading force-heading)
  6643. (save-excursion
  6644. (org-back-to-heading)
  6645. (outline-previous-heading)
  6646. (looking-at org-todo-line-regexp))
  6647. (let*
  6648. ((new-mark-x
  6649. (if (or arg
  6650. (not (match-beginning 2))
  6651. (member (match-string 2) org-done-keywords))
  6652. (car org-todo-keywords-1)
  6653. (match-string 2)))
  6654. (new-mark
  6655. (or
  6656. (run-hook-with-args-until-success
  6657. 'org-todo-get-default-hook new-mark-x nil)
  6658. new-mark-x)))
  6659. (beginning-of-line 1)
  6660. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6661. (if org-treat-insert-todo-heading-as-state-change
  6662. (org-todo new-mark)
  6663. (insert new-mark " "))))
  6664. (when org-provide-todo-statistics
  6665. (org-update-parent-todo-statistics))))
  6666. (defun org-insert-subheading (arg)
  6667. "Insert a new subheading and demote it.
  6668. Works for outline headings and for plain lists alike."
  6669. (interactive "P")
  6670. (org-insert-heading arg)
  6671. (cond
  6672. ((org-at-heading-p) (org-do-demote))
  6673. ((org-at-item-p) (org-indent-item))))
  6674. (defun org-insert-todo-subheading (arg)
  6675. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6676. Works for outline headings and for plain lists alike."
  6677. (interactive "P")
  6678. (org-insert-todo-heading arg)
  6679. (cond
  6680. ((org-at-heading-p) (org-do-demote))
  6681. ((org-at-item-p) (org-indent-item))))
  6682. ;;; Promotion and Demotion
  6683. (defvar org-after-demote-entry-hook nil
  6684. "Hook run after an entry has been demoted.
  6685. The cursor will be at the beginning of the entry.
  6686. When a subtree is being demoted, the hook will be called for each node.")
  6687. (defvar org-after-promote-entry-hook nil
  6688. "Hook run after an entry has been promoted.
  6689. The cursor will be at the beginning of the entry.
  6690. When a subtree is being promoted, the hook will be called for each node.")
  6691. (defun org-promote-subtree ()
  6692. "Promote the entire subtree.
  6693. See also `org-promote'."
  6694. (interactive)
  6695. (save-excursion
  6696. (org-with-limited-levels (org-map-tree 'org-promote)))
  6697. (org-fix-position-after-promote))
  6698. (defun org-demote-subtree ()
  6699. "Demote the entire subtree. See `org-demote'.
  6700. See also `org-promote'."
  6701. (interactive)
  6702. (save-excursion
  6703. (org-with-limited-levels (org-map-tree 'org-demote)))
  6704. (org-fix-position-after-promote))
  6705. (defun org-do-promote ()
  6706. "Promote the current heading higher up the tree.
  6707. If the region is active in `transient-mark-mode', promote all headings
  6708. in the region."
  6709. (interactive)
  6710. (save-excursion
  6711. (if (org-region-active-p)
  6712. (org-map-region 'org-promote (region-beginning) (region-end))
  6713. (org-promote)))
  6714. (org-fix-position-after-promote))
  6715. (defun org-do-demote ()
  6716. "Demote the current heading lower down the tree.
  6717. If the region is active in `transient-mark-mode', demote all headings
  6718. in the region."
  6719. (interactive)
  6720. (save-excursion
  6721. (if (org-region-active-p)
  6722. (org-map-region 'org-demote (region-beginning) (region-end))
  6723. (org-demote)))
  6724. (org-fix-position-after-promote))
  6725. (defun org-fix-position-after-promote ()
  6726. "Make sure that after pro/demotion cursor position is right."
  6727. (let ((pos (point)))
  6728. (when (save-excursion
  6729. (beginning-of-line 1)
  6730. (looking-at org-todo-line-regexp)
  6731. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6732. (cond ((eobp) (insert " "))
  6733. ((eolp) (insert " "))
  6734. ((equal (char-after) ?\ ) (forward-char 1))))))
  6735. (defun org-current-level ()
  6736. "Return the level of the current entry, or nil if before the first headline.
  6737. The level is the number of stars at the beginning of the headline."
  6738. (save-excursion
  6739. (org-with-limited-levels
  6740. (if (ignore-errors (org-back-to-heading t))
  6741. (funcall outline-level)))))
  6742. (defun org-get-previous-line-level ()
  6743. "Return the outline depth of the last headline before the current line.
  6744. Returns 0 for the first headline in the buffer, and nil if before the
  6745. first headline."
  6746. (let ((current-level (org-current-level))
  6747. (prev-level (when (> (line-number-at-pos) 1)
  6748. (save-excursion
  6749. (beginning-of-line 0)
  6750. (org-current-level)))))
  6751. (cond ((null current-level) nil) ; Before first headline
  6752. ((null prev-level) 0) ; At first headline
  6753. (prev-level))))
  6754. (defun org-reduced-level (l)
  6755. "Compute the effective level of a heading.
  6756. This takes into account the setting of `org-odd-levels-only'."
  6757. (cond
  6758. ((zerop l) 0)
  6759. (org-odd-levels-only (1+ (floor (/ l 2))))
  6760. (t l)))
  6761. (defun org-level-increment ()
  6762. "Return the number of stars that will be added or removed at a
  6763. time to headlines when structure editing, based on the value of
  6764. `org-odd-levels-only'."
  6765. (if org-odd-levels-only 2 1))
  6766. (defun org-get-valid-level (level &optional change)
  6767. "Rectify a level change under the influence of `org-odd-levels-only'
  6768. LEVEL is a current level, CHANGE is by how much the level should be
  6769. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6770. even level numbers will become the next higher odd number."
  6771. (if org-odd-levels-only
  6772. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6773. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6774. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6775. (max 1 (+ level (or change 0)))))
  6776. (if (boundp 'define-obsolete-function-alias)
  6777. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6778. (define-obsolete-function-alias 'org-get-legal-level
  6779. 'org-get-valid-level)
  6780. (define-obsolete-function-alias 'org-get-legal-level
  6781. 'org-get-valid-level "23.1")))
  6782. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6783. ;; ̀org-with-limited-levels'
  6784. (defun org-promote ()
  6785. "Promote the current heading higher up the tree.
  6786. If the region is active in `transient-mark-mode', promote all headings
  6787. in the region."
  6788. (org-back-to-heading t)
  6789. (let* ((level (save-match-data (funcall outline-level)))
  6790. (after-change-functions (remove 'flyspell-after-change-function
  6791. after-change-functions))
  6792. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6793. (diff (abs (- level (length up-head) -1))))
  6794. (cond ((and (= level 1) org-called-with-limited-levels
  6795. org-allow-promoting-top-level-subtree)
  6796. (replace-match "# " nil t))
  6797. ((= level 1)
  6798. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6799. (t (replace-match up-head nil t)))
  6800. ;; Fixup tag positioning
  6801. (unless (= level 1)
  6802. (and org-auto-align-tags (org-set-tags nil t))
  6803. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6804. (run-hooks 'org-after-promote-entry-hook)))
  6805. (defun org-demote ()
  6806. "Demote the current heading lower down the tree.
  6807. If the region is active in `transient-mark-mode', demote all headings
  6808. in the region."
  6809. (org-back-to-heading t)
  6810. (let* ((level (save-match-data (funcall outline-level)))
  6811. (after-change-functions (remove 'flyspell-after-change-function
  6812. after-change-functions))
  6813. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6814. (diff (abs (- level (length down-head) -1))))
  6815. (replace-match down-head nil t)
  6816. ;; Fixup tag positioning
  6817. (and org-auto-align-tags (org-set-tags nil t))
  6818. (if org-adapt-indentation (org-fixup-indentation diff))
  6819. (run-hooks 'org-after-demote-entry-hook)))
  6820. (defun org-cycle-level ()
  6821. "Cycle the level of an empty headline through possible states.
  6822. This goes first to child, then to parent, level, then up the hierarchy.
  6823. After top level, it switches back to sibling level."
  6824. (interactive)
  6825. (let ((org-adapt-indentation nil))
  6826. (when (org-point-at-end-of-empty-headline)
  6827. (setq this-command 'org-cycle-level) ; Only needed for caching
  6828. (let ((cur-level (org-current-level))
  6829. (prev-level (org-get-previous-line-level)))
  6830. (cond
  6831. ;; If first headline in file, promote to top-level.
  6832. ((= prev-level 0)
  6833. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6834. do (org-do-promote)))
  6835. ;; If same level as prev, demote one.
  6836. ((= prev-level cur-level)
  6837. (org-do-demote))
  6838. ;; If parent is top-level, promote to top level if not already.
  6839. ((= prev-level 1)
  6840. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6841. do (org-do-promote)))
  6842. ;; If top-level, return to prev-level.
  6843. ((= cur-level 1)
  6844. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6845. do (org-do-demote)))
  6846. ;; If less than prev-level, promote one.
  6847. ((< cur-level prev-level)
  6848. (org-do-promote))
  6849. ;; If deeper than prev-level, promote until higher than
  6850. ;; prev-level.
  6851. ((> cur-level prev-level)
  6852. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6853. do (org-do-promote))))
  6854. t))))
  6855. (defun org-map-tree (fun)
  6856. "Call FUN for every heading underneath the current one."
  6857. (org-back-to-heading)
  6858. (let ((level (funcall outline-level)))
  6859. (save-excursion
  6860. (funcall fun)
  6861. (while (and (progn
  6862. (outline-next-heading)
  6863. (> (funcall outline-level) level))
  6864. (not (eobp)))
  6865. (funcall fun)))))
  6866. (defun org-map-region (fun beg end)
  6867. "Call FUN for every heading between BEG and END."
  6868. (let ((org-ignore-region t))
  6869. (save-excursion
  6870. (setq end (copy-marker end))
  6871. (goto-char beg)
  6872. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6873. (< (point) end))
  6874. (funcall fun))
  6875. (while (and (progn
  6876. (outline-next-heading)
  6877. (< (point) end))
  6878. (not (eobp)))
  6879. (funcall fun)))))
  6880. (defvar org-property-end-re) ; silence byte-compiler
  6881. (defun org-fixup-indentation (diff)
  6882. "Change the indentation in the current entry by DIFF.
  6883. However, if any line in the current entry has no indentation, or if it
  6884. would end up with no indentation after the change, nothing at all is done."
  6885. (save-excursion
  6886. (let ((end (save-excursion (outline-next-heading)
  6887. (point-marker)))
  6888. (prohibit (if (> diff 0)
  6889. "^\\S-"
  6890. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6891. col)
  6892. (unless (save-excursion (end-of-line 1)
  6893. (re-search-forward prohibit end t))
  6894. (while (and (< (point) end)
  6895. (re-search-forward "^[ \t]+" end t))
  6896. (goto-char (match-end 0))
  6897. (setq col (current-column))
  6898. (if (< diff 0) (replace-match ""))
  6899. (org-indent-to-column (+ diff col))))
  6900. (move-marker end nil))))
  6901. (defun org-convert-to-odd-levels ()
  6902. "Convert an org-mode file with all levels allowed to one with odd levels.
  6903. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6904. level 5 etc."
  6905. (interactive)
  6906. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6907. (let ((outline-level 'org-outline-level)
  6908. (org-odd-levels-only nil) n)
  6909. (save-excursion
  6910. (goto-char (point-min))
  6911. (while (re-search-forward "^\\*\\*+ " nil t)
  6912. (setq n (- (length (match-string 0)) 2))
  6913. (while (>= (setq n (1- n)) 0)
  6914. (org-demote))
  6915. (end-of-line 1))))))
  6916. (defun org-convert-to-oddeven-levels ()
  6917. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6918. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6919. file contains a section with an even level, conversion would
  6920. destroy the structure of the file. An error is signaled in this
  6921. case."
  6922. (interactive)
  6923. (goto-char (point-min))
  6924. ;; First check if there are no even levels
  6925. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6926. (org-show-context t)
  6927. (error "Not all levels are odd in this file. Conversion not possible"))
  6928. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6929. (let ((outline-regexp org-outline-regexp)
  6930. (outline-level 'org-outline-level)
  6931. (org-odd-levels-only nil) n)
  6932. (save-excursion
  6933. (goto-char (point-min))
  6934. (while (re-search-forward "^\\*\\*+ " nil t)
  6935. (setq n (/ (1- (length (match-string 0))) 2))
  6936. (while (>= (setq n (1- n)) 0)
  6937. (org-promote))
  6938. (end-of-line 1))))))
  6939. (defun org-tr-level (n)
  6940. "Make N odd if required."
  6941. (if org-odd-levels-only (1+ (/ n 2)) n))
  6942. ;;; Vertical tree motion, cutting and pasting of subtrees
  6943. (defun org-move-subtree-up (&optional arg)
  6944. "Move the current subtree up past ARG headlines of the same level."
  6945. (interactive "p")
  6946. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6947. (defun org-move-subtree-down (&optional arg)
  6948. "Move the current subtree down past ARG headlines of the same level."
  6949. (interactive "p")
  6950. (setq arg (prefix-numeric-value arg))
  6951. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6952. 'org-get-last-sibling))
  6953. (ins-point (make-marker))
  6954. (cnt (abs arg))
  6955. (col (current-column))
  6956. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6957. ;; Select the tree
  6958. (org-back-to-heading)
  6959. (setq beg0 (point))
  6960. (save-excursion
  6961. (setq ne-beg (org-back-over-empty-lines))
  6962. (setq beg (point)))
  6963. (save-match-data
  6964. (save-excursion (outline-end-of-heading)
  6965. (setq folded (outline-invisible-p)))
  6966. (outline-end-of-subtree))
  6967. (outline-next-heading)
  6968. (setq ne-end (org-back-over-empty-lines))
  6969. (setq end (point))
  6970. (goto-char beg0)
  6971. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6972. ;; include less whitespace
  6973. (save-excursion
  6974. (goto-char beg)
  6975. (forward-line (- ne-beg ne-end))
  6976. (setq beg (point))))
  6977. ;; Find insertion point, with error handling
  6978. (while (> cnt 0)
  6979. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6980. (progn (goto-char beg0)
  6981. (error "Cannot move past superior level or buffer limit")))
  6982. (setq cnt (1- cnt)))
  6983. (if (> arg 0)
  6984. ;; Moving forward - still need to move over subtree
  6985. (progn (org-end-of-subtree t t)
  6986. (save-excursion
  6987. (org-back-over-empty-lines)
  6988. (or (bolp) (newline)))))
  6989. (setq ne-ins (org-back-over-empty-lines))
  6990. (move-marker ins-point (point))
  6991. (setq txt (buffer-substring beg end))
  6992. (org-save-markers-in-region beg end)
  6993. (delete-region beg end)
  6994. (org-remove-empty-overlays-at beg)
  6995. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6996. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6997. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6998. (let ((bbb (point)))
  6999. (insert-before-markers txt)
  7000. (org-reinstall-markers-in-region bbb)
  7001. (move-marker ins-point bbb))
  7002. (or (bolp) (insert "\n"))
  7003. (setq ins-end (point))
  7004. (goto-char ins-point)
  7005. (org-skip-whitespace)
  7006. (when (and (< arg 0)
  7007. (org-first-sibling-p)
  7008. (> ne-ins ne-beg))
  7009. ;; Move whitespace back to beginning
  7010. (save-excursion
  7011. (goto-char ins-end)
  7012. (let ((kill-whole-line t))
  7013. (kill-line (- ne-ins ne-beg)) (point)))
  7014. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7015. (move-marker ins-point nil)
  7016. (if folded
  7017. (hide-subtree)
  7018. (org-show-entry)
  7019. (show-children)
  7020. (org-cycle-hide-drawers 'children))
  7021. (org-clean-visibility-after-subtree-move)
  7022. ;; move back to the initial column we were at
  7023. (move-to-column col)))
  7024. (defvar org-subtree-clip ""
  7025. "Clipboard for cut and paste of subtrees.
  7026. This is actually only a copy of the kill, because we use the normal kill
  7027. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7028. (defvar org-subtree-clip-folded nil
  7029. "Was the last copied subtree folded?
  7030. This is used to fold the tree back after pasting.")
  7031. (defun org-cut-subtree (&optional n)
  7032. "Cut the current subtree into the clipboard.
  7033. With prefix arg N, cut this many sequential subtrees.
  7034. This is a short-hand for marking the subtree and then cutting it."
  7035. (interactive "p")
  7036. (org-copy-subtree n 'cut))
  7037. (defun org-copy-subtree (&optional n cut force-store-markers)
  7038. "Cut the current subtree into the clipboard.
  7039. With prefix arg N, cut this many sequential subtrees.
  7040. This is a short-hand for marking the subtree and then copying it.
  7041. If CUT is non-nil, actually cut the subtree.
  7042. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7043. of some markers in the region, even if CUT is non-nil. This is
  7044. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7045. (interactive "p")
  7046. (let (beg end folded (beg0 (point)))
  7047. (if (org-called-interactively-p 'any)
  7048. (org-back-to-heading nil) ; take what looks like a subtree
  7049. (org-back-to-heading t)) ; take what is really there
  7050. (org-back-over-empty-lines)
  7051. (setq beg (point))
  7052. (skip-chars-forward " \t\r\n")
  7053. (save-match-data
  7054. (save-excursion (outline-end-of-heading)
  7055. (setq folded (outline-invisible-p)))
  7056. (condition-case nil
  7057. (org-forward-heading-same-level (1- n) t)
  7058. (error nil))
  7059. (org-end-of-subtree t t))
  7060. (org-back-over-empty-lines)
  7061. (setq end (point))
  7062. (goto-char beg0)
  7063. (when (> end beg)
  7064. (setq org-subtree-clip-folded folded)
  7065. (when (or cut force-store-markers)
  7066. (org-save-markers-in-region beg end))
  7067. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7068. (setq org-subtree-clip (current-kill 0))
  7069. (message "%s: Subtree(s) with %d characters"
  7070. (if cut "Cut" "Copied")
  7071. (length org-subtree-clip)))))
  7072. (defun org-paste-subtree (&optional level tree for-yank)
  7073. "Paste the clipboard as a subtree, with modification of headline level.
  7074. The entire subtree is promoted or demoted in order to match a new headline
  7075. level.
  7076. If the cursor is at the beginning of a headline, the same level as
  7077. that headline is used to paste the tree
  7078. If not, the new level is derived from the *visible* headings
  7079. before and after the insertion point, and taken to be the inferior headline
  7080. level of the two. So if the previous visible heading is level 3 and the
  7081. next is level 4 (or vice versa), level 4 will be used for insertion.
  7082. This makes sure that the subtree remains an independent subtree and does
  7083. not swallow low level entries.
  7084. You can also force a different level, either by using a numeric prefix
  7085. argument, or by inserting the heading marker by hand. For example, if the
  7086. cursor is after \"*****\", then the tree will be shifted to level 5.
  7087. If optional TREE is given, use this text instead of the kill ring.
  7088. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7089. move back over whitespace before inserting, and move point to the end of
  7090. the inserted text when done."
  7091. (interactive "P")
  7092. (setq tree (or tree (and kill-ring (current-kill 0))))
  7093. (unless (org-kill-is-subtree-p tree)
  7094. (error "%s"
  7095. (substitute-command-keys
  7096. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7097. (org-with-limited-levels
  7098. (let* ((visp (not (outline-invisible-p)))
  7099. (txt tree)
  7100. (^re_ "\\(\\*+\\)[ \t]*")
  7101. (old-level (if (string-match org-outline-regexp-bol txt)
  7102. (- (match-end 0) (match-beginning 0) 1)
  7103. -1))
  7104. (force-level (cond (level (prefix-numeric-value level))
  7105. ((and (looking-at "[ \t]*$")
  7106. (string-match
  7107. "^\\*+$" (buffer-substring
  7108. (point-at-bol) (point))))
  7109. (- (match-end 1) (match-beginning 1)))
  7110. ((and (bolp)
  7111. (looking-at org-outline-regexp))
  7112. (- (match-end 0) (point) 1))))
  7113. (previous-level (save-excursion
  7114. (condition-case nil
  7115. (progn
  7116. (outline-previous-visible-heading 1)
  7117. (if (looking-at ^re_)
  7118. (- (match-end 0) (match-beginning 0) 1)
  7119. 1))
  7120. (error 1))))
  7121. (next-level (save-excursion
  7122. (condition-case nil
  7123. (progn
  7124. (or (looking-at org-outline-regexp)
  7125. (outline-next-visible-heading 1))
  7126. (if (looking-at ^re_)
  7127. (- (match-end 0) (match-beginning 0) 1)
  7128. 1))
  7129. (error 1))))
  7130. (new-level (or force-level (max previous-level next-level)))
  7131. (shift (if (or (= old-level -1)
  7132. (= new-level -1)
  7133. (= old-level new-level))
  7134. 0
  7135. (- new-level old-level)))
  7136. (delta (if (> shift 0) -1 1))
  7137. (func (if (> shift 0) 'org-demote 'org-promote))
  7138. (org-odd-levels-only nil)
  7139. beg end newend)
  7140. ;; Remove the forced level indicator
  7141. (if force-level
  7142. (delete-region (point-at-bol) (point)))
  7143. ;; Paste
  7144. (beginning-of-line (if (bolp) 1 2))
  7145. (unless for-yank (org-back-over-empty-lines))
  7146. (setq beg (point))
  7147. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7148. (insert-before-markers txt)
  7149. (unless (string-match "\n\\'" txt) (insert "\n"))
  7150. (setq newend (point))
  7151. (org-reinstall-markers-in-region beg)
  7152. (setq end (point))
  7153. (goto-char beg)
  7154. (skip-chars-forward " \t\n\r")
  7155. (setq beg (point))
  7156. (if (and (outline-invisible-p) visp)
  7157. (save-excursion (outline-show-heading)))
  7158. ;; Shift if necessary
  7159. (unless (= shift 0)
  7160. (save-restriction
  7161. (narrow-to-region beg end)
  7162. (while (not (= shift 0))
  7163. (org-map-region func (point-min) (point-max))
  7164. (setq shift (+ delta shift)))
  7165. (goto-char (point-min))
  7166. (setq newend (point-max))))
  7167. (when (or (org-called-interactively-p 'interactive) for-yank)
  7168. (message "Clipboard pasted as level %d subtree" new-level))
  7169. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7170. kill-ring
  7171. (eq org-subtree-clip (current-kill 0))
  7172. org-subtree-clip-folded)
  7173. ;; The tree was folded before it was killed/copied
  7174. (hide-subtree))
  7175. (and for-yank (goto-char newend)))))
  7176. (defun org-kill-is-subtree-p (&optional txt)
  7177. "Check if the current kill is an outline subtree, or a set of trees.
  7178. Returns nil if kill does not start with a headline, or if the first
  7179. headline level is not the largest headline level in the tree.
  7180. So this will actually accept several entries of equal levels as well,
  7181. which is OK for `org-paste-subtree'.
  7182. If optional TXT is given, check this string instead of the current kill."
  7183. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7184. (re (org-get-limited-outline-regexp))
  7185. (^re (concat "^" re))
  7186. (start-level (and kill
  7187. (string-match
  7188. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7189. kill)
  7190. (- (match-end 2) (match-beginning 2) 1)))
  7191. (start (1+ (or (match-beginning 2) -1))))
  7192. (if (not start-level)
  7193. (progn
  7194. nil) ;; does not even start with a heading
  7195. (catch 'exit
  7196. (while (setq start (string-match ^re kill (1+ start)))
  7197. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7198. (throw 'exit nil)))
  7199. t))))
  7200. (defvar org-markers-to-move nil
  7201. "Markers that should be moved with a cut-and-paste operation.
  7202. Those markers are stored together with their positions relative to
  7203. the start of the region.")
  7204. (defun org-save-markers-in-region (beg end)
  7205. "Check markers in region.
  7206. If these markers are between BEG and END, record their position relative
  7207. to BEG, so that after moving the block of text, we can put the markers back
  7208. into place.
  7209. This function gets called just before an entry or tree gets cut from the
  7210. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7211. called immediately, to move the markers with the entries."
  7212. (setq org-markers-to-move nil)
  7213. (when (featurep 'org-clock)
  7214. (org-clock-save-markers-for-cut-and-paste beg end))
  7215. (when (featurep 'org-agenda)
  7216. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7217. (defun org-check-and-save-marker (marker beg end)
  7218. "Check if MARKER is between BEG and END.
  7219. If yes, remember the marker and the distance to BEG."
  7220. (when (and (marker-buffer marker)
  7221. (equal (marker-buffer marker) (current-buffer)))
  7222. (if (and (>= marker beg) (< marker end))
  7223. (push (cons marker (- marker beg)) org-markers-to-move))))
  7224. (defun org-reinstall-markers-in-region (beg)
  7225. "Move all remembered markers to their position relative to BEG."
  7226. (mapc (lambda (x)
  7227. (move-marker (car x) (+ beg (cdr x))))
  7228. org-markers-to-move)
  7229. (setq org-markers-to-move nil))
  7230. (defun org-narrow-to-subtree ()
  7231. "Narrow buffer to the current subtree."
  7232. (interactive)
  7233. (save-excursion
  7234. (save-match-data
  7235. (org-with-limited-levels
  7236. (narrow-to-region
  7237. (progn (org-back-to-heading t) (point))
  7238. (progn (org-end-of-subtree t t)
  7239. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7240. (point)))))))
  7241. (defun org-narrow-to-block ()
  7242. "Narrow buffer to the current block."
  7243. (interactive)
  7244. (let* ((case-fold-search t)
  7245. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7246. "^[ \t]*#\\+end_.*")))
  7247. (if blockp
  7248. (narrow-to-region (car blockp) (cdr blockp))
  7249. (error "Not in a block"))))
  7250. (eval-when-compile
  7251. (defvar org-property-drawer-re))
  7252. (defvar org-property-start-re) ;; defined below
  7253. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7254. "Clone the task (subtree) at point N times.
  7255. The clones will be inserted as siblings.
  7256. In interactive use, the user will be prompted for the number of
  7257. clones to be produced, and for a time SHIFT, which may be a
  7258. repeater as used in time stamps, for example `+3d'.
  7259. When a valid repeater is given and the entry contains any time
  7260. stamps, the clones will become a sequence in time, with time
  7261. stamps in the subtree shifted for each clone produced. If SHIFT
  7262. is nil or the empty string, time stamps will be left alone. The
  7263. ID property of the original subtree is removed.
  7264. If the original subtree did contain time stamps with a repeater,
  7265. the following will happen:
  7266. - the repeater will be removed in each clone
  7267. - an additional clone will be produced, with the current, unshifted
  7268. date(s) in the entry.
  7269. - the original entry will be placed *after* all the clones, with
  7270. repeater intact.
  7271. - the start days in the repeater in the original entry will be shifted
  7272. to past the last clone.
  7273. In this way you can spell out a number of instances of a repeating task,
  7274. and still retain the repeater to cover future instances of the task."
  7275. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7276. (let (beg end template task idprop
  7277. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7278. (drawer-re org-drawer-regexp))
  7279. (if (not (and (integerp n) (> n 0)))
  7280. (error "Invalid number of replications %s" n))
  7281. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7282. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7283. shift)))
  7284. (error "Invalid shift specification %s" shift))
  7285. (when doshift
  7286. (setq shift-n (string-to-number (match-string 1 shift))
  7287. shift-what (cdr (assoc (match-string 2 shift)
  7288. '(("d" . day) ("w" . week)
  7289. ("m" . month) ("y" . year))))))
  7290. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7291. (setq nmin 1 nmax n)
  7292. (org-back-to-heading t)
  7293. (setq beg (point))
  7294. (setq idprop (org-entry-get nil "ID"))
  7295. (org-end-of-subtree t t)
  7296. (or (bolp) (insert "\n"))
  7297. (setq end (point))
  7298. (setq template (buffer-substring beg end))
  7299. (when (and doshift
  7300. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7301. (delete-region beg end)
  7302. (setq end beg)
  7303. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7304. (goto-char end)
  7305. (loop for n from nmin to nmax do
  7306. ;; prepare clone
  7307. (with-temp-buffer
  7308. (insert template)
  7309. (org-mode)
  7310. (goto-char (point-min))
  7311. (org-show-subtree)
  7312. (and idprop (if org-clone-delete-id
  7313. (org-entry-delete nil "ID")
  7314. (org-id-get-create t)))
  7315. (unless (= n 0)
  7316. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7317. (kill-whole-line))
  7318. (goto-char (point-min))
  7319. (while (re-search-forward drawer-re nil t)
  7320. (mapc (lambda (d)
  7321. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7322. (goto-char (point-min))
  7323. (when doshift
  7324. (while (re-search-forward org-ts-regexp-both nil t)
  7325. (org-timestamp-change (* n shift-n) shift-what))
  7326. (unless (= n n-no-remove)
  7327. (goto-char (point-min))
  7328. (while (re-search-forward org-ts-regexp nil t)
  7329. (save-excursion
  7330. (goto-char (match-beginning 0))
  7331. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7332. (delete-region (match-beginning 1) (match-end 1)))))))
  7333. (setq task (buffer-string)))
  7334. (insert task))
  7335. (goto-char beg)))
  7336. ;;; Outline Sorting
  7337. (defun org-sort (with-case)
  7338. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7339. Optional argument WITH-CASE means sort case-sensitively."
  7340. (interactive "P")
  7341. (cond
  7342. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7343. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7344. (t
  7345. (org-call-with-arg 'org-sort-entries with-case))))
  7346. (defun org-sort-remove-invisible (s)
  7347. (remove-text-properties 0 (length s) org-rm-props s)
  7348. (while (string-match org-bracket-link-regexp s)
  7349. (setq s (replace-match (if (match-end 2)
  7350. (match-string 3 s)
  7351. (match-string 1 s)) t t s)))
  7352. s)
  7353. (defvar org-priority-regexp) ; defined later in the file
  7354. (defvar org-after-sorting-entries-or-items-hook nil
  7355. "Hook that is run after a bunch of entries or items have been sorted.
  7356. When children are sorted, the cursor is in the parent line when this
  7357. hook gets called. When a region or a plain list is sorted, the cursor
  7358. will be in the first entry of the sorted region/list.")
  7359. (defun org-sort-entries
  7360. (&optional with-case sorting-type getkey-func compare-func property)
  7361. "Sort entries on a certain level of an outline tree.
  7362. If there is an active region, the entries in the region are sorted.
  7363. Else, if the cursor is before the first entry, sort the top-level items.
  7364. Else, the children of the entry at point are sorted.
  7365. Sorting can be alphabetically, numerically, by date/time as given by
  7366. a time stamp, by a property or by priority.
  7367. The command prompts for the sorting type unless it has been given to the
  7368. function through the SORTING-TYPE argument, which needs to be a character,
  7369. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  7370. precise meaning of each character:
  7371. n Numerically, by converting the beginning of the entry/item to a number.
  7372. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7373. t By date/time, either the first active time stamp in the entry, or, if
  7374. none exist, by the first inactive one.
  7375. s By the scheduled date/time.
  7376. d By deadline date/time.
  7377. c By creation time, which is assumed to be the first inactive time stamp
  7378. at the beginning of a line.
  7379. p By priority according to the cookie.
  7380. r By the value of a property.
  7381. Capital letters will reverse the sort order.
  7382. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7383. called with point at the beginning of the record. It must return either
  7384. a string or a number that should serve as the sorting key for that record.
  7385. Comparing entries ignores case by default. However, with an optional argument
  7386. WITH-CASE, the sorting considers case as well."
  7387. (interactive "P")
  7388. (let ((case-func (if with-case 'identity 'downcase))
  7389. start beg end stars re re2
  7390. txt what tmp)
  7391. ;; Find beginning and end of region to sort
  7392. (cond
  7393. ((org-region-active-p)
  7394. ;; we will sort the region
  7395. (setq end (region-end)
  7396. what "region")
  7397. (goto-char (region-beginning))
  7398. (if (not (org-at-heading-p)) (outline-next-heading))
  7399. (setq start (point)))
  7400. ((or (org-at-heading-p)
  7401. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7402. ;; we will sort the children of the current headline
  7403. (org-back-to-heading)
  7404. (setq start (point)
  7405. end (progn (org-end-of-subtree t t)
  7406. (or (bolp) (insert "\n"))
  7407. (org-back-over-empty-lines)
  7408. (point))
  7409. what "children")
  7410. (goto-char start)
  7411. (show-subtree)
  7412. (outline-next-heading))
  7413. (t
  7414. ;; we will sort the top-level entries in this file
  7415. (goto-char (point-min))
  7416. (or (org-at-heading-p) (outline-next-heading))
  7417. (setq start (point))
  7418. (goto-char (point-max))
  7419. (beginning-of-line 1)
  7420. (when (looking-at ".*?\\S-")
  7421. ;; File ends in a non-white line
  7422. (end-of-line 1)
  7423. (insert "\n"))
  7424. (setq end (point-max))
  7425. (setq what "top-level")
  7426. (goto-char start)
  7427. (show-all)))
  7428. (setq beg (point))
  7429. (if (>= beg end) (error "Nothing to sort"))
  7430. (looking-at "\\(\\*+\\)")
  7431. (setq stars (match-string 1)
  7432. re (concat "^" (regexp-quote stars) " +")
  7433. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7434. txt (buffer-substring beg end))
  7435. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7436. (if (and (not (equal stars "*")) (string-match re2 txt))
  7437. (error "Region to sort contains a level above the first entry"))
  7438. (unless sorting-type
  7439. (message
  7440. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7441. [t]ime [s]cheduled [d]eadline [c]reated
  7442. A/N/T/S/D/C/P/O/F means reversed:"
  7443. what)
  7444. (setq sorting-type (read-char-exclusive))
  7445. (and (= (downcase sorting-type) ?f)
  7446. (setq getkey-func
  7447. (org-icompleting-read "Sort using function: "
  7448. obarray 'fboundp t nil nil))
  7449. (setq getkey-func (intern getkey-func)))
  7450. (and (= (downcase sorting-type) ?r)
  7451. (setq property
  7452. (org-icompleting-read "Property: "
  7453. (mapcar 'list (org-buffer-property-keys t))
  7454. nil t))))
  7455. (message "Sorting entries...")
  7456. (save-restriction
  7457. (narrow-to-region start end)
  7458. (let ((dcst (downcase sorting-type))
  7459. (case-fold-search nil)
  7460. (now (current-time)))
  7461. (sort-subr
  7462. (/= dcst sorting-type)
  7463. ;; This function moves to the beginning character of the "record" to
  7464. ;; be sorted.
  7465. (lambda nil
  7466. (if (re-search-forward re nil t)
  7467. (goto-char (match-beginning 0))
  7468. (goto-char (point-max))))
  7469. ;; This function moves to the last character of the "record" being
  7470. ;; sorted.
  7471. (lambda nil
  7472. (save-match-data
  7473. (condition-case nil
  7474. (outline-forward-same-level 1)
  7475. (error
  7476. (goto-char (point-max))))))
  7477. ;; This function returns the value that gets sorted against.
  7478. (lambda nil
  7479. (cond
  7480. ((= dcst ?n)
  7481. (if (looking-at org-complex-heading-regexp)
  7482. (string-to-number (match-string 4))
  7483. nil))
  7484. ((= dcst ?a)
  7485. (if (looking-at org-complex-heading-regexp)
  7486. (funcall case-func (match-string 4))
  7487. nil))
  7488. ((= dcst ?t)
  7489. (let ((end (save-excursion (outline-next-heading) (point))))
  7490. (if (or (re-search-forward org-ts-regexp end t)
  7491. (re-search-forward org-ts-regexp-both end t))
  7492. (org-time-string-to-seconds (match-string 0))
  7493. (org-float-time now))))
  7494. ((= dcst ?c)
  7495. (let ((end (save-excursion (outline-next-heading) (point))))
  7496. (if (re-search-forward
  7497. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7498. end t)
  7499. (org-time-string-to-seconds (match-string 0))
  7500. (org-float-time now))))
  7501. ((= dcst ?s)
  7502. (let ((end (save-excursion (outline-next-heading) (point))))
  7503. (if (re-search-forward org-scheduled-time-regexp end t)
  7504. (org-time-string-to-seconds (match-string 1))
  7505. (org-float-time now))))
  7506. ((= dcst ?d)
  7507. (let ((end (save-excursion (outline-next-heading) (point))))
  7508. (if (re-search-forward org-deadline-time-regexp end t)
  7509. (org-time-string-to-seconds (match-string 1))
  7510. (org-float-time now))))
  7511. ((= dcst ?p)
  7512. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7513. (string-to-char (match-string 2))
  7514. org-default-priority))
  7515. ((= dcst ?r)
  7516. (or (org-entry-get nil property) ""))
  7517. ((= dcst ?o)
  7518. (if (looking-at org-complex-heading-regexp)
  7519. (- 9999 (length (member (match-string 2)
  7520. org-todo-keywords-1)))))
  7521. ((= dcst ?f)
  7522. (if getkey-func
  7523. (progn
  7524. (setq tmp (funcall getkey-func))
  7525. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7526. tmp)
  7527. (error "Invalid key function `%s'" getkey-func)))
  7528. (t (error "Invalid sorting type `%c'" sorting-type))))
  7529. nil
  7530. (cond
  7531. ((= dcst ?a) 'string<)
  7532. ((= dcst ?f) compare-func)
  7533. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7534. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7535. (message "Sorting entries...done")))
  7536. (defun org-do-sort (table what &optional with-case sorting-type)
  7537. "Sort TABLE of WHAT according to SORTING-TYPE.
  7538. The user will be prompted for the SORTING-TYPE if the call to this
  7539. function does not specify it. WHAT is only for the prompt, to indicate
  7540. what is being sorted. The sorting key will be extracted from
  7541. the car of the elements of the table.
  7542. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7543. (unless sorting-type
  7544. (message
  7545. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7546. what)
  7547. (setq sorting-type (read-char-exclusive)))
  7548. (let ((dcst (downcase sorting-type))
  7549. extractfun comparefun)
  7550. ;; Define the appropriate functions
  7551. (cond
  7552. ((= dcst ?n)
  7553. (setq extractfun 'string-to-number
  7554. comparefun (if (= dcst sorting-type) '< '>)))
  7555. ((= dcst ?a)
  7556. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7557. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7558. comparefun (if (= dcst sorting-type)
  7559. 'string<
  7560. (lambda (a b) (and (not (string< a b))
  7561. (not (string= a b)))))))
  7562. ((= dcst ?t)
  7563. (setq extractfun
  7564. (lambda (x)
  7565. (if (or (string-match org-ts-regexp x)
  7566. (string-match org-ts-regexp-both x))
  7567. (org-float-time
  7568. (org-time-string-to-time (match-string 0 x)))
  7569. 0))
  7570. comparefun (if (= dcst sorting-type) '< '>)))
  7571. (t (error "Invalid sorting type `%c'" sorting-type)))
  7572. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7573. table)
  7574. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7575. ;;; The orgstruct minor mode
  7576. ;; Define a minor mode which can be used in other modes in order to
  7577. ;; integrate the org-mode structure editing commands.
  7578. ;; This is really a hack, because the org-mode structure commands use
  7579. ;; keys which normally belong to the major mode. Here is how it
  7580. ;; works: The minor mode defines all the keys necessary to operate the
  7581. ;; structure commands, but wraps the commands into a function which
  7582. ;; tests if the cursor is currently at a headline or a plain list
  7583. ;; item. If that is the case, the structure command is used,
  7584. ;; temporarily setting many Org-mode variables like regular
  7585. ;; expressions for filling etc. However, when any of those keys is
  7586. ;; used at a different location, function uses `key-binding' to look
  7587. ;; up if the key has an associated command in another currently active
  7588. ;; keymap (minor modes, major mode, global), and executes that
  7589. ;; command. There might be problems if any of the keys is otherwise
  7590. ;; used as a prefix key.
  7591. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7592. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7593. ;; addresses this by checking explicitly for both bindings.
  7594. (defvar orgstruct-mode-map (make-sparse-keymap)
  7595. "Keymap for the minor `orgstruct-mode'.")
  7596. (defvar org-local-vars nil
  7597. "List of local variables, for use by `orgstruct-mode'.")
  7598. ;;;###autoload
  7599. (define-minor-mode orgstruct-mode
  7600. "Toggle the minor mode `orgstruct-mode'.
  7601. This mode is for using Org-mode structure commands in other
  7602. modes. The following keys behave as if Org-mode were active, if
  7603. the cursor is on a headline, or on a plain list item (both as
  7604. defined by Org-mode).
  7605. M-up Move entry/item up
  7606. M-down Move entry/item down
  7607. M-left Promote
  7608. M-right Demote
  7609. M-S-up Move entry/item up
  7610. M-S-down Move entry/item down
  7611. M-S-left Promote subtree
  7612. M-S-right Demote subtree
  7613. M-q Fill paragraph and items like in Org-mode
  7614. C-c ^ Sort entries
  7615. C-c - Cycle list bullet
  7616. TAB Cycle item visibility
  7617. M-RET Insert new heading/item
  7618. S-M-RET Insert new TODO heading / Checkbox item
  7619. C-c C-c Set tags / toggle checkbox"
  7620. nil " OrgStruct" nil
  7621. (org-load-modules-maybe)
  7622. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7623. ;;;###autoload
  7624. (defun turn-on-orgstruct ()
  7625. "Unconditionally turn on `orgstruct-mode'."
  7626. (orgstruct-mode 1))
  7627. (defvar org-fb-vars nil)
  7628. (make-variable-buffer-local 'org-fb-vars)
  7629. (defun orgstruct++-mode (&optional arg)
  7630. "Toggle `orgstruct-mode', the enhanced version of it.
  7631. In addition to setting orgstruct-mode, this also exports all
  7632. indentation and autofilling variables from org-mode into the
  7633. buffer. It will also recognize item context in multiline items."
  7634. (interactive "P")
  7635. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7636. (if (< arg 1)
  7637. (progn (orgstruct-mode -1)
  7638. (mapc (lambda(v)
  7639. (org-set-local (car v)
  7640. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7641. org-fb-vars))
  7642. (orgstruct-mode 1)
  7643. (setq org-fb-vars nil)
  7644. (let (var val)
  7645. (mapc
  7646. (lambda (x)
  7647. (when (string-match
  7648. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7649. (symbol-name (car x)))
  7650. (setq var (car x) val (nth 1 x))
  7651. (push (list var `(quote ,(eval var))) org-fb-vars)
  7652. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7653. org-local-vars)
  7654. (org-set-local 'orgstruct-is-++ t))))
  7655. (defvar orgstruct-is-++ nil
  7656. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7657. (make-variable-buffer-local 'orgstruct-is-++)
  7658. ;;;###autoload
  7659. (defun turn-on-orgstruct++ ()
  7660. "Unconditionally turn on `orgstruct++-mode'."
  7661. (orgstruct++-mode 1))
  7662. (defun orgstruct-error ()
  7663. "Error when there is no default binding for a structure key."
  7664. (interactive)
  7665. (error "This key has no function outside structure elements"))
  7666. (defun orgstruct-setup ()
  7667. "Setup orgstruct keymaps."
  7668. (let ((nfunc 0)
  7669. (bindings
  7670. (list
  7671. '([(meta up)] org-metaup)
  7672. '([(meta down)] org-metadown)
  7673. '([(meta left)] org-metaleft)
  7674. '([(meta right)] org-metaright)
  7675. '([(meta shift up)] org-shiftmetaup)
  7676. '([(meta shift down)] org-shiftmetadown)
  7677. '([(meta shift left)] org-shiftmetaleft)
  7678. '([(meta shift right)] org-shiftmetaright)
  7679. '([?\e (up)] org-metaup)
  7680. '([?\e (down)] org-metadown)
  7681. '([?\e (left)] org-metaleft)
  7682. '([?\e (right)] org-metaright)
  7683. '([?\e (shift up)] org-shiftmetaup)
  7684. '([?\e (shift down)] org-shiftmetadown)
  7685. '([?\e (shift left)] org-shiftmetaleft)
  7686. '([?\e (shift right)] org-shiftmetaright)
  7687. '([(shift up)] org-shiftup)
  7688. '([(shift down)] org-shiftdown)
  7689. '([(shift left)] org-shiftleft)
  7690. '([(shift right)] org-shiftright)
  7691. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7692. '("\M-q" fill-paragraph)
  7693. '("\C-c^" org-sort)
  7694. '("\C-c-" org-cycle-list-bullet)))
  7695. elt key fun cmd)
  7696. (while (setq elt (pop bindings))
  7697. (setq nfunc (1+ nfunc))
  7698. (setq key (org-key (car elt))
  7699. fun (nth 1 elt)
  7700. cmd (orgstruct-make-binding fun nfunc key))
  7701. (org-defkey orgstruct-mode-map key cmd))
  7702. ;; Prevent an error for users who forgot to make autoloads
  7703. (require 'org-element)
  7704. ;; Special treatment needed for TAB and RET
  7705. (org-defkey orgstruct-mode-map [(tab)]
  7706. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7707. (org-defkey orgstruct-mode-map "\C-i"
  7708. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7709. (org-defkey orgstruct-mode-map "\M-\C-m"
  7710. (orgstruct-make-binding 'org-insert-heading 105
  7711. "\M-\C-m" [(meta return)]))
  7712. (org-defkey orgstruct-mode-map [(meta return)]
  7713. (orgstruct-make-binding 'org-insert-heading 106
  7714. [(meta return)] "\M-\C-m"))
  7715. (org-defkey orgstruct-mode-map [(shift meta return)]
  7716. (orgstruct-make-binding 'org-insert-todo-heading 107
  7717. [(meta return)] "\M-\C-m"))
  7718. (org-defkey orgstruct-mode-map "\e\C-m"
  7719. (orgstruct-make-binding 'org-insert-heading 108
  7720. "\e\C-m" [?\e (return)]))
  7721. (org-defkey orgstruct-mode-map [?\e (return)]
  7722. (orgstruct-make-binding 'org-insert-heading 109
  7723. [?\e (return)] "\e\C-m"))
  7724. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7725. (orgstruct-make-binding 'org-insert-todo-heading 110
  7726. [?\e (return)] "\e\C-m"))
  7727. (unless org-local-vars
  7728. (setq org-local-vars (org-get-local-variables)))
  7729. t))
  7730. (defun orgstruct-make-binding (fun n &rest keys)
  7731. "Create a function for binding in the structure minor mode.
  7732. FUN is the command to call inside a table. N is used to create a unique
  7733. command name. KEYS are keys that should be checked in for a command
  7734. to execute outside of tables."
  7735. (eval
  7736. (list 'defun
  7737. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7738. '(arg)
  7739. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7740. "Outside of structure, run the binding of `"
  7741. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7742. "'.")
  7743. '(interactive "p")
  7744. (list 'if
  7745. `(org-context-p 'headline 'item
  7746. (and orgstruct-is-++
  7747. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7748. 'item-body))
  7749. (list 'org-run-like-in-org-mode (list 'quote fun))
  7750. (list 'let '(orgstruct-mode)
  7751. (list 'call-interactively
  7752. (append '(or)
  7753. (mapcar (lambda (k)
  7754. (list 'key-binding k))
  7755. keys)
  7756. '('orgstruct-error))))))))
  7757. (defun org-contextualize-keys (alist contexts)
  7758. "Return valid elements in ALIST depending on CONTEXTS.
  7759. `org-agenda-custom-commands' or `org-capture-templates' are the
  7760. values used for ALIST, and `org-agenda-custom-commands-contexts'
  7761. or `org-capture-templates-contexts' are the associated contexts
  7762. definitions."
  7763. (let ((contexts
  7764. ;; normalize contexts
  7765. (mapcar
  7766. (lambda(c) (cond ((listp (cadr c))
  7767. (list (car c) (car c) (cadr c)))
  7768. ((string= "" (cadr c))
  7769. (list (car c) (car c) (caddr c)))
  7770. (t c))) contexts))
  7771. (a alist) c r s)
  7772. ;; loop over all commands or templates
  7773. (while (setq c (pop a))
  7774. (let (vrules repl)
  7775. (cond
  7776. ((not (assoc (car c) contexts))
  7777. (push c r))
  7778. ((and (assoc (car c) contexts)
  7779. (setq vrules (org-contextualize-validate-key
  7780. (car c) contexts)))
  7781. (mapc (lambda (vr)
  7782. (when (not (equal (car vr) (cadr vr)))
  7783. (setq repl vr))) vrules)
  7784. (if (not repl) (push c r)
  7785. (push (cadr repl) s)
  7786. (push
  7787. (cons (car c)
  7788. (cdr (or (assoc (cadr repl) alist)
  7789. (error "Undefined key `%s' as contextual replacement for `%s'"
  7790. (cadr repl) (car c)))))
  7791. r))))))
  7792. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  7793. (delq
  7794. nil
  7795. (delete-dups
  7796. (mapcar (lambda (x)
  7797. (let ((tpl (car x)))
  7798. (when (not (delq
  7799. nil
  7800. (mapcar (lambda(y)
  7801. (equal y tpl)) s))) x)))
  7802. (reverse r))))))
  7803. (defun org-contextualize-validate-key (key contexts)
  7804. "Check CONTEXTS for agenda or capture KEY."
  7805. (let (r rr res)
  7806. (while (setq r (pop contexts))
  7807. (mapc
  7808. (lambda (rr)
  7809. (when
  7810. (and (equal key (car r))
  7811. (if (functionp rr) (funcall rr)
  7812. (or (and (eq (car rr) 'in-file)
  7813. (buffer-file-name)
  7814. (string-match (cdr rr) (buffer-file-name)))
  7815. (and (eq (car rr) 'in-mode)
  7816. (string-match (cdr rr) (symbol-name major-mode)))
  7817. (when (and (eq (car rr) 'not-in-file)
  7818. (buffer-file-name))
  7819. (not (string-match (cdr rr) (buffer-file-name))))
  7820. (when (eq (car rr) 'not-in-mode)
  7821. (not (string-match (cdr rr) (symbol-name major-mode)))))))
  7822. (push r res)))
  7823. (car (last r))))
  7824. (delete-dups (delq nil res))))
  7825. (defun org-context-p (&rest contexts)
  7826. "Check if local context is any of CONTEXTS.
  7827. Possible values in the list of contexts are `table', `headline', and `item'."
  7828. (let ((pos (point)))
  7829. (goto-char (point-at-bol))
  7830. (prog1 (or (and (memq 'table contexts)
  7831. (looking-at "[ \t]*|"))
  7832. (and (memq 'headline contexts)
  7833. (looking-at org-outline-regexp))
  7834. (and (memq 'item contexts)
  7835. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7836. (and (memq 'item-body contexts)
  7837. (org-in-item-p)))
  7838. (goto-char pos))))
  7839. (defun org-get-local-variables ()
  7840. "Return a list of all local variables in an Org mode buffer."
  7841. (let (varlist)
  7842. (with-current-buffer (get-buffer-create "*Org tmp*")
  7843. (erase-buffer)
  7844. (org-mode)
  7845. (setq varlist (buffer-local-variables)))
  7846. (kill-buffer "*Org tmp*")
  7847. (delq nil
  7848. (mapcar
  7849. (lambda (x)
  7850. (setq x
  7851. (if (symbolp x)
  7852. (list x)
  7853. (list (car x) (list 'quote (cdr x)))))
  7854. (if (string-match
  7855. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  7856. (symbol-name (car x)))
  7857. x nil))
  7858. varlist))))
  7859. (defun org-clone-local-variables (from-buffer &optional regexp)
  7860. "Clone local variables from FROM-BUFFER.
  7861. Optional argument REGEXP selects variables to clone."
  7862. (mapc
  7863. (lambda (pair)
  7864. (and (symbolp (car pair))
  7865. (or (null regexp)
  7866. (string-match regexp (symbol-name (car pair))))
  7867. (set (make-local-variable (car pair))
  7868. (cdr pair))))
  7869. (buffer-local-variables from-buffer)))
  7870. ;;;###autoload
  7871. (defun org-run-like-in-org-mode (cmd)
  7872. "Run a command, pretending that the current buffer is in Org-mode.
  7873. This will temporarily bind local variables that are typically bound in
  7874. Org-mode to the values they have in Org-mode, and then interactively
  7875. call CMD."
  7876. (org-load-modules-maybe)
  7877. (unless org-local-vars
  7878. (setq org-local-vars (org-get-local-variables)))
  7879. (eval (list 'let org-local-vars
  7880. (list 'call-interactively (list 'quote cmd)))))
  7881. ;;;; Archiving
  7882. (defun org-get-category (&optional pos force-refresh)
  7883. "Get the category applying to position POS."
  7884. (save-match-data
  7885. (if force-refresh (org-refresh-category-properties))
  7886. (let ((pos (or pos (point))))
  7887. (or (get-text-property pos 'org-category)
  7888. (progn (org-refresh-category-properties)
  7889. (get-text-property pos 'org-category))))))
  7890. (defun org-refresh-category-properties ()
  7891. "Refresh category text properties in the buffer."
  7892. (let ((case-fold-search t)
  7893. (inhibit-read-only t)
  7894. (def-cat (cond
  7895. ((null org-category)
  7896. (if buffer-file-name
  7897. (file-name-sans-extension
  7898. (file-name-nondirectory buffer-file-name))
  7899. "???"))
  7900. ((symbolp org-category) (symbol-name org-category))
  7901. (t org-category)))
  7902. beg end cat pos optionp)
  7903. (org-unmodified
  7904. (save-excursion
  7905. (save-restriction
  7906. (widen)
  7907. (goto-char (point-min))
  7908. (put-text-property (point) (point-max) 'org-category def-cat)
  7909. (while (re-search-forward
  7910. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7911. (setq pos (match-end 0)
  7912. optionp (equal (char-after (match-beginning 0)) ?#)
  7913. cat (org-trim (match-string 2)))
  7914. (if optionp
  7915. (setq beg (point-at-bol) end (point-max))
  7916. (org-back-to-heading t)
  7917. (setq beg (point) end (org-end-of-subtree t t)))
  7918. (put-text-property beg end 'org-category cat)
  7919. (put-text-property beg end 'org-category-position beg)
  7920. (goto-char pos)))))))
  7921. ;;;; Link Stuff
  7922. ;;; Link abbreviations
  7923. (defun org-link-expand-abbrev (link)
  7924. "Apply replacements as defined in `org-link-abbrev-alist'."
  7925. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7926. (let* ((key (match-string 1 link))
  7927. (as (or (assoc key org-link-abbrev-alist-local)
  7928. (assoc key org-link-abbrev-alist)))
  7929. (tag (and (match-end 2) (match-string 3 link)))
  7930. rpl)
  7931. (if (not as)
  7932. link
  7933. (setq rpl (cdr as))
  7934. (cond
  7935. ((symbolp rpl) (funcall rpl tag))
  7936. ((string-match "%(\\([^)]+\\))" rpl)
  7937. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  7938. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7939. ((string-match "%h" rpl)
  7940. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7941. (t (concat rpl tag)))))
  7942. link))
  7943. ;;; Storing and inserting links
  7944. (defvar org-insert-link-history nil
  7945. "Minibuffer history for links inserted with `org-insert-link'.")
  7946. (defvar org-stored-links nil
  7947. "Contains the links stored with `org-store-link'.")
  7948. (defvar org-store-link-plist nil
  7949. "Plist with info about the most recently link created with `org-store-link'.")
  7950. (defvar org-link-protocols nil
  7951. "Link protocols added to Org-mode using `org-add-link-type'.")
  7952. (defvar org-store-link-functions nil
  7953. "List of functions that are called to create and store a link.
  7954. Each function will be called in turn until one returns a non-nil
  7955. value. Each function should check if it is responsible for creating
  7956. this link (for example by looking at the major mode).
  7957. If not, it must exit and return nil.
  7958. If yes, it should return a non-nil value after a calling
  7959. `org-store-link-props' with a list of properties and values.
  7960. Special properties are:
  7961. :type The link prefix, like \"http\". This must be given.
  7962. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7963. This is obligatory as well.
  7964. :description Optional default description for the second pair
  7965. of brackets in an Org-mode link. The user can still change
  7966. this when inserting this link into an Org-mode buffer.
  7967. In addition to these, any additional properties can be specified
  7968. and then used in capture templates.")
  7969. (defun org-add-link-type (type &optional follow export)
  7970. "Add TYPE to the list of `org-link-types'.
  7971. Re-compute all regular expressions depending on `org-link-types'
  7972. FOLLOW and EXPORT are two functions.
  7973. FOLLOW should take the link path as the single argument and do whatever
  7974. is necessary to follow the link, for example find a file or display
  7975. a mail message.
  7976. EXPORT should format the link path for export to one of the export formats.
  7977. It should be a function accepting three arguments:
  7978. path the path of the link, the text after the prefix (like \"http:\")
  7979. desc the description of the link, if any, or a description added by
  7980. org-export-normalize-links if there is none
  7981. format the export format, a symbol like `html' or `latex' or `ascii'..
  7982. The function may use the FORMAT information to return different values
  7983. depending on the format. The return value will be put literally into
  7984. the exported file. If the return value is nil, this means Org should
  7985. do what it normally does with links which do not have EXPORT defined.
  7986. Org-mode has a built-in default for exporting links. If you are happy with
  7987. this default, there is no need to define an export function for the link
  7988. type. For a simple example of an export function, see `org-bbdb.el'."
  7989. (add-to-list 'org-link-types type t)
  7990. (org-make-link-regexps)
  7991. (if (assoc type org-link-protocols)
  7992. (setcdr (assoc type org-link-protocols) (list follow export))
  7993. (push (list type follow export) org-link-protocols)))
  7994. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  7995. (defvar org-link-to-org-use-id) ; Defined in org-id.el
  7996. ;;;###autoload
  7997. (defun org-store-link (arg)
  7998. "\\<org-mode-map>Store an org-link to the current location.
  7999. This link is added to `org-stored-links' and can later be inserted
  8000. into an org-buffer with \\[org-insert-link].
  8001. For some link types, a prefix arg is interpreted:
  8002. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  8003. For file links, arg negates `org-context-in-file-links'."
  8004. (interactive "P")
  8005. (org-load-modules-maybe)
  8006. (setq org-store-link-plist nil) ; reset
  8007. (org-with-limited-levels
  8008. (let (link cpltxt desc description search txt custom-id agenda-link)
  8009. (cond
  8010. ((run-hook-with-args-until-success 'org-store-link-functions)
  8011. (setq link (plist-get org-store-link-plist :link)
  8012. desc (or (plist-get org-store-link-plist :description) link)))
  8013. ((org-src-edit-buffer-p)
  8014. (let (label gc)
  8015. (while (or (not label)
  8016. (save-excursion
  8017. (save-restriction
  8018. (widen)
  8019. (goto-char (point-min))
  8020. (re-search-forward
  8021. (regexp-quote (format org-coderef-label-format label))
  8022. nil t))))
  8023. (when label (message "Label exists already") (sit-for 2))
  8024. (setq label (read-string "Code line label: " label)))
  8025. (end-of-line 1)
  8026. (setq link (format org-coderef-label-format label))
  8027. (setq gc (- 79 (length link)))
  8028. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8029. (insert link)
  8030. (setq link (concat "(" label ")") desc nil)))
  8031. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8032. ;; We are in the agenda, link to referenced location
  8033. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8034. (get-text-property (point) 'org-marker))))
  8035. (when m
  8036. (org-with-point-at m
  8037. (setq agenda-link
  8038. (if (org-called-interactively-p 'any)
  8039. (call-interactively 'org-store-link)
  8040. (org-store-link nil)))))))
  8041. ((eq major-mode 'calendar-mode)
  8042. (let ((cd (calendar-cursor-to-date)))
  8043. (setq link
  8044. (format-time-string
  8045. (car org-time-stamp-formats)
  8046. (apply 'encode-time
  8047. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8048. nil nil nil))))
  8049. (org-store-link-props :type "calendar" :date cd)))
  8050. ((eq major-mode 'help-mode)
  8051. (setq link (concat "help:" (save-excursion
  8052. (goto-char (point-min))
  8053. (looking-at "^[^ ]+")
  8054. (match-string 0))))
  8055. (org-store-link-props :type "help"))
  8056. ((eq major-mode 'w3-mode)
  8057. (setq cpltxt (if (and (buffer-name)
  8058. (not (string-match "Untitled" (buffer-name))))
  8059. (buffer-name)
  8060. (url-view-url t))
  8061. link (url-view-url t))
  8062. (org-store-link-props :type "w3" :url (url-view-url t)))
  8063. ((eq major-mode 'w3m-mode)
  8064. (setq cpltxt (or w3m-current-title w3m-current-url)
  8065. link w3m-current-url)
  8066. (org-store-link-props :type "w3m" :url (url-view-url t)))
  8067. ((setq search (run-hook-with-args-until-success
  8068. 'org-create-file-search-functions))
  8069. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8070. "::" search))
  8071. (setq cpltxt (or description link)))
  8072. ((eq major-mode 'image-mode)
  8073. (setq cpltxt (concat "file:"
  8074. (abbreviate-file-name buffer-file-name))
  8075. link cpltxt)
  8076. (org-store-link-props :type "image" :file buffer-file-name))
  8077. ((eq major-mode 'dired-mode)
  8078. ;; link to the file in the current line
  8079. (let ((file (dired-get-filename nil t)))
  8080. (setq file (if file
  8081. (abbreviate-file-name
  8082. (expand-file-name (dired-get-filename nil t)))
  8083. ;; otherwise, no file so use current directory.
  8084. default-directory))
  8085. (setq cpltxt (concat "file:" file)
  8086. link cpltxt)))
  8087. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8088. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8089. (cond
  8090. ((org-in-regexp "<<\\(.*?\\)>>")
  8091. (setq cpltxt
  8092. (concat "file:"
  8093. (abbreviate-file-name
  8094. (buffer-file-name (buffer-base-buffer)))
  8095. "::" (match-string 1))
  8096. link cpltxt))
  8097. ((and (featurep 'org-id)
  8098. (or (eq org-link-to-org-use-id t)
  8099. (and (org-called-interactively-p 'any)
  8100. (or (eq org-link-to-org-use-id 'create-if-interactive)
  8101. (and (eq org-link-to-org-use-id
  8102. 'create-if-interactive-and-no-custom-id)
  8103. (not custom-id))))
  8104. (and org-link-to-org-use-id (org-entry-get nil "ID"))))
  8105. ;; We can make a link using the ID.
  8106. (setq link (condition-case nil
  8107. (prog1 (org-id-store-link)
  8108. (setq desc (plist-get org-store-link-plist :description)))
  8109. (error
  8110. ;; probably before first headline, link to file only
  8111. (concat "file:"
  8112. (abbreviate-file-name
  8113. (buffer-file-name (buffer-base-buffer))))))))
  8114. (t
  8115. ;; Just link to current headline
  8116. (setq cpltxt (concat "file:"
  8117. (abbreviate-file-name
  8118. (buffer-file-name (buffer-base-buffer)))))
  8119. ;; Add a context search string
  8120. (when (org-xor org-context-in-file-links arg)
  8121. (setq txt (cond
  8122. ((org-at-heading-p) nil)
  8123. ((org-region-active-p)
  8124. (buffer-substring (region-beginning) (region-end)))))
  8125. (when (or (null txt) (string-match "\\S-" txt))
  8126. (setq cpltxt
  8127. (concat cpltxt "::"
  8128. (condition-case nil
  8129. (org-make-org-heading-search-string txt)
  8130. (error "")))
  8131. desc (or (nth 4 (ignore-errors
  8132. (org-heading-components))) "NONE"))))
  8133. (if (string-match "::\\'" cpltxt)
  8134. (setq cpltxt (substring cpltxt 0 -2)))
  8135. (setq link cpltxt))))
  8136. ((buffer-file-name (buffer-base-buffer))
  8137. ;; Just link to this file here.
  8138. (setq cpltxt (concat "file:"
  8139. (abbreviate-file-name
  8140. (buffer-file-name (buffer-base-buffer)))))
  8141. ;; Add a context string
  8142. (when (org-xor org-context-in-file-links arg)
  8143. (setq txt (if (org-region-active-p)
  8144. (buffer-substring (region-beginning) (region-end))
  8145. (buffer-substring (point-at-bol) (point-at-eol))))
  8146. ;; Only use search option if there is some text.
  8147. (when (string-match "\\S-" txt)
  8148. (setq cpltxt
  8149. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8150. desc "NONE")))
  8151. (setq link cpltxt))
  8152. ((org-called-interactively-p 'interactive)
  8153. (error "Cannot link to a buffer which is not visiting a file"))
  8154. (t (setq link nil)))
  8155. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8156. (setq link (or link cpltxt)
  8157. desc (or desc cpltxt))
  8158. (if (equal desc "NONE") (setq desc nil))
  8159. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8160. (progn
  8161. (setq org-stored-links
  8162. (cons (list link desc) org-stored-links))
  8163. (message "Stored: %s" (or desc link))
  8164. (when custom-id
  8165. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8166. "::#" custom-id))
  8167. (setq org-stored-links
  8168. (cons (list link desc) org-stored-links))))
  8169. (or agenda-link (and link (org-make-link-string link desc)))))))
  8170. (defun org-store-link-props (&rest plist)
  8171. "Store link properties, extract names and addresses."
  8172. (let (x adr)
  8173. (when (setq x (plist-get plist :from))
  8174. (setq adr (mail-extract-address-components x))
  8175. (setq plist (plist-put plist :fromname (car adr)))
  8176. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8177. (when (setq x (plist-get plist :to))
  8178. (setq adr (mail-extract-address-components x))
  8179. (setq plist (plist-put plist :toname (car adr)))
  8180. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8181. (let ((from (plist-get plist :from))
  8182. (to (plist-get plist :to)))
  8183. (when (and from to org-from-is-user-regexp)
  8184. (setq plist
  8185. (plist-put plist :fromto
  8186. (if (string-match org-from-is-user-regexp from)
  8187. (concat "to %t")
  8188. (concat "from %f"))))))
  8189. (setq org-store-link-plist plist))
  8190. (defun org-add-link-props (&rest plist)
  8191. "Add these properties to the link property list."
  8192. (let (key value)
  8193. (while plist
  8194. (setq key (pop plist) value (pop plist))
  8195. (setq org-store-link-plist
  8196. (plist-put org-store-link-plist key value)))))
  8197. (defun org-email-link-description (&optional fmt)
  8198. "Return the description part of an email link.
  8199. This takes information from `org-store-link-plist' and formats it
  8200. according to FMT (default from `org-email-link-description-format')."
  8201. (setq fmt (or fmt org-email-link-description-format))
  8202. (let* ((p org-store-link-plist)
  8203. (to (plist-get p :toaddress))
  8204. (from (plist-get p :fromaddress))
  8205. (table
  8206. (list
  8207. (cons "%c" (plist-get p :fromto))
  8208. (cons "%F" (plist-get p :from))
  8209. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8210. (cons "%T" (plist-get p :to))
  8211. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8212. (cons "%s" (plist-get p :subject))
  8213. (cons "%d" (plist-get p :date))
  8214. (cons "%m" (plist-get p :message-id)))))
  8215. (when (string-match "%c" fmt)
  8216. ;; Check if the user wrote this message
  8217. (if (and org-from-is-user-regexp from to
  8218. (save-match-data (string-match org-from-is-user-regexp from)))
  8219. (setq fmt (replace-match "to %t" t t fmt))
  8220. (setq fmt (replace-match "from %f" t t fmt))))
  8221. (org-replace-escapes fmt table)))
  8222. (defun org-make-org-heading-search-string (&optional string heading)
  8223. "Make search string for STRING or current headline."
  8224. (interactive)
  8225. (let ((s (or string (org-get-heading)))
  8226. (lines org-context-in-file-links))
  8227. (unless (and string (not heading))
  8228. ;; We are using a headline, clean up garbage in there.
  8229. (if (string-match org-todo-regexp s)
  8230. (setq s (replace-match "" t t s)))
  8231. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  8232. (setq s (replace-match "" t t s)))
  8233. (setq s (org-trim s))
  8234. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8235. org-comment-string "\\)") s)
  8236. (setq s (replace-match "" t t s)))
  8237. (while (string-match org-ts-regexp s)
  8238. (setq s (replace-match "" t t s))))
  8239. (or string (setq s (concat "*" s))) ; Add * for headlines
  8240. (when (and string (integerp lines) (> lines 0))
  8241. (let ((slines (org-split-string s "\n")))
  8242. (when (< lines (length slines))
  8243. (setq s (mapconcat
  8244. 'identity
  8245. (reverse (nthcdr (- (length slines) lines)
  8246. (reverse slines))) "\n")))))
  8247. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8248. (defun org-make-link-string (link &optional description)
  8249. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8250. (unless (string-match "\\S-" link)
  8251. (error "Empty link"))
  8252. (when (and description
  8253. (stringp description)
  8254. (not (string-match "\\S-" description)))
  8255. (setq description nil))
  8256. (when (stringp description)
  8257. ;; Remove brackets from the description, they are fatal.
  8258. (while (string-match "\\[" description)
  8259. (setq description (replace-match "{" t t description)))
  8260. (while (string-match "\\]" description)
  8261. (setq description (replace-match "}" t t description))))
  8262. (when (equal link description)
  8263. ;; No description needed, it is identical
  8264. (setq description nil))
  8265. (when (and (not description)
  8266. (not (string-match (org-image-file-name-regexp) link))
  8267. (not (equal link (org-link-escape link))))
  8268. (setq description (org-extract-attributes link)))
  8269. (setq link
  8270. (cond ((string-match (org-image-file-name-regexp) link) link)
  8271. ((string-match org-link-types-re link)
  8272. (concat (match-string 1 link)
  8273. (org-link-escape (substring link (match-end 1)))))
  8274. (t (org-link-escape link))))
  8275. (concat "[[" link "]"
  8276. (if description (concat "[" description "]") "")
  8277. "]"))
  8278. (defconst org-link-escape-chars
  8279. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8280. "List of characters that should be escaped in link.
  8281. This is the list that is used for internal purposes.")
  8282. (defconst org-link-escape-chars-browser
  8283. '(?\ )
  8284. "List of escapes for characters that are problematic in links.
  8285. This is the list that is used before handing over to the browser.")
  8286. (defun org-link-escape (text &optional table merge)
  8287. "Return percent escaped representation of TEXT.
  8288. TEXT is a string with the text to escape.
  8289. Optional argument TABLE is a list with characters that should be
  8290. escaped. When nil, `org-link-escape-chars' is used.
  8291. If optional argument MERGE is set, merge TABLE into
  8292. `org-link-escape-chars'."
  8293. (cond
  8294. ((and table merge)
  8295. (mapc (lambda (defchr)
  8296. (unless (member defchr table)
  8297. (setq table (cons defchr table)))) org-link-escape-chars))
  8298. ((null table)
  8299. (setq table org-link-escape-chars)))
  8300. (mapconcat
  8301. (lambda (char)
  8302. (if (or (member char table)
  8303. (and (or (< char 32) (= char 37) (> char 126))
  8304. org-url-hexify-p))
  8305. (mapconcat (lambda (sequence-element)
  8306. (format "%%%.2X" sequence-element))
  8307. (or (encode-coding-char char 'utf-8)
  8308. (error "Unable to percent escape character: %s"
  8309. (char-to-string char))) "")
  8310. (char-to-string char))) text ""))
  8311. (defun org-link-unescape (str)
  8312. "Unhex hexified Unicode strings as returned from the JavaScript function
  8313. encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
  8314. (unless (and (null str) (string= "" str))
  8315. (let ((pos 0) (case-fold-search t) unhexed)
  8316. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8317. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8318. (setq str (replace-match unhexed t t str))
  8319. (setq pos (+ pos (length unhexed))))))
  8320. str)
  8321. (defun org-link-unescape-compound (hex)
  8322. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
  8323. Note: this function also decodes single byte encodings like
  8324. `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
  8325. (save-match-data
  8326. (let* ((bytes (cdr (split-string hex "%")))
  8327. (ret "")
  8328. (eat 0)
  8329. (sum 0))
  8330. (while bytes
  8331. (let* ((val (string-to-number (pop bytes) 16))
  8332. (shift-xor
  8333. (if (= 0 eat)
  8334. (cond
  8335. ((>= val 252) (cons 6 252))
  8336. ((>= val 248) (cons 5 248))
  8337. ((>= val 240) (cons 4 240))
  8338. ((>= val 224) (cons 3 224))
  8339. ((>= val 192) (cons 2 192))
  8340. (t (cons 0 0)))
  8341. (cons 6 128))))
  8342. (if (>= val 192) (setq eat (car shift-xor)))
  8343. (setq val (logxor val (cdr shift-xor)))
  8344. (setq sum (+ (lsh sum (car shift-xor)) val))
  8345. (if (> eat 0) (setq eat (- eat 1)))
  8346. (cond
  8347. ((= 0 eat) ;multi byte
  8348. (setq ret (concat ret (org-char-to-string sum)))
  8349. (setq sum 0))
  8350. ((not bytes) ; single byte(s)
  8351. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8352. )) ;; end (while bytes
  8353. ret )))
  8354. (defun org-link-unescape-single-byte-sequence (hex)
  8355. "Unhexify hex-encoded single byte character sequences."
  8356. (mapconcat (lambda (byte)
  8357. (char-to-string (string-to-number byte 16)))
  8358. (cdr (split-string hex "%")) ""))
  8359. (defun org-xor (a b)
  8360. "Exclusive or."
  8361. (if a (not b) b))
  8362. (defun org-fixup-message-id-for-http (s)
  8363. "Replace special characters in a message id, so it can be used in an http query."
  8364. (when (string-match "%" s)
  8365. (setq s (mapconcat (lambda (c)
  8366. (if (eq c ?%)
  8367. "%25"
  8368. (char-to-string c)))
  8369. s "")))
  8370. (while (string-match "<" s)
  8371. (setq s (replace-match "%3C" t t s)))
  8372. (while (string-match ">" s)
  8373. (setq s (replace-match "%3E" t t s)))
  8374. (while (string-match "@" s)
  8375. (setq s (replace-match "%40" t t s)))
  8376. s)
  8377. (defun org-link-prettify (link)
  8378. "Return a human-readable representation of LINK.
  8379. The car of LINK must be a raw link the cdr of LINK must be either
  8380. a link description or nil."
  8381. (let ((desc (or (cadr link) "<no description>")))
  8382. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8383. "<" (car link) ">")))
  8384. ;;;###autoload
  8385. (defun org-insert-link-global ()
  8386. "Insert a link like Org-mode does.
  8387. This command can be called in any mode to insert a link in Org-mode syntax."
  8388. (interactive)
  8389. (org-load-modules-maybe)
  8390. (org-run-like-in-org-mode 'org-insert-link))
  8391. (defun org-insert-all-links (&optional keep)
  8392. "Insert all links in `org-stored-links'."
  8393. (interactive "P")
  8394. (let ((links (copy-sequence org-stored-links)) l)
  8395. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8396. (insert "- ")
  8397. (org-insert-link nil (car l) (cadr l))
  8398. (insert "\n"))))
  8399. (defun org-link-fontify-links-to-this-file ()
  8400. "Fontify links to the current file in `org-stored-links'."
  8401. (let ((f (buffer-file-name)) a b)
  8402. (setq a (mapcar (lambda(l)
  8403. (let ((ll (car l)))
  8404. (when (and (string-match "^file:\\(.+\\)::" ll)
  8405. (equal f (expand-file-name (match-string 1 ll))))
  8406. ll)))
  8407. org-stored-links))
  8408. (when (featurep 'org-id)
  8409. (setq b (mapcar (lambda(l)
  8410. (let ((ll (car l)))
  8411. (when (and (string-match "^id:\\(.+\\)$" ll)
  8412. (equal f (expand-file-name
  8413. (or (org-id-find-id-file
  8414. (match-string 1 ll)) ""))))
  8415. ll)))
  8416. org-stored-links)))
  8417. (mapcar (lambda(l)
  8418. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8419. (delq nil (append a b)))))
  8420. (defvar org-link-links-in-this-file nil)
  8421. (defun org-insert-link (&optional complete-file link-location default-description)
  8422. "Insert a link. At the prompt, enter the link.
  8423. Completion can be used to insert any of the link protocol prefixes like
  8424. http or ftp in use.
  8425. The history can be used to select a link previously stored with
  8426. `org-store-link'. When the empty string is entered (i.e. if you just
  8427. press RET at the prompt), the link defaults to the most recently
  8428. stored link. As SPC triggers completion in the minibuffer, you need to
  8429. use M-SPC or C-q SPC to force the insertion of a space character.
  8430. You will also be prompted for a description, and if one is given, it will
  8431. be displayed in the buffer instead of the link.
  8432. If there is already a link at point, this command will allow you to edit link
  8433. and description parts.
  8434. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8435. be selected using completion. The path to the file will be relative to the
  8436. current directory if the file is in the current directory or a subdirectory.
  8437. Otherwise, the link will be the absolute path as completed in the minibuffer
  8438. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8439. option `org-link-file-path-type'.
  8440. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8441. the current directory or below.
  8442. With three \\[universal-argument] prefixes, negate the meaning of
  8443. `org-keep-stored-link-after-insertion'.
  8444. If `org-make-link-description-function' is non-nil, this function will be
  8445. called with the link target, and the result will be the default
  8446. link description.
  8447. If the LINK-LOCATION parameter is non-nil, this value will be
  8448. used as the link location instead of reading one interactively.
  8449. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8450. be used as the default description."
  8451. (interactive "P")
  8452. (let* ((wcf (current-window-configuration))
  8453. (origbuf (current-buffer))
  8454. (region (if (org-region-active-p)
  8455. (buffer-substring (region-beginning) (region-end))))
  8456. (remove (and region (list (region-beginning) (region-end))))
  8457. (desc region)
  8458. tmphist ; byte-compile incorrectly complains about this
  8459. (link link-location)
  8460. (abbrevs org-link-abbrev-alist-local)
  8461. entry file all-prefixes auto-desc)
  8462. (cond
  8463. (link-location) ; specified by arg, just use it.
  8464. ((org-in-regexp org-bracket-link-regexp 1)
  8465. ;; We do have a link at point, and we are going to edit it.
  8466. (setq remove (list (match-beginning 0) (match-end 0)))
  8467. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8468. (setq link (read-string "Link: "
  8469. (org-link-unescape
  8470. (org-match-string-no-properties 1)))))
  8471. ((or (org-in-regexp org-angle-link-re)
  8472. (org-in-regexp org-plain-link-re))
  8473. ;; Convert to bracket link
  8474. (setq remove (list (match-beginning 0) (match-end 0))
  8475. link (read-string "Link: "
  8476. (org-remove-angle-brackets (match-string 0)))))
  8477. ((member complete-file '((4) (16)))
  8478. ;; Completing read for file names.
  8479. (setq link (org-file-complete-link complete-file)))
  8480. (t
  8481. ;; Read link, with completion for stored links.
  8482. (org-link-fontify-links-to-this-file)
  8483. (org-switch-to-buffer-other-window "*Org Links*")
  8484. (with-current-buffer "*Org Links*"
  8485. (erase-buffer)
  8486. (insert "Insert a link.
  8487. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8488. (when org-stored-links
  8489. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8490. (insert (mapconcat 'org-link-prettify
  8491. (reverse org-stored-links) "\n")))
  8492. (goto-char (point-min)))
  8493. (let ((cw (selected-window)))
  8494. (select-window (get-buffer-window "*Org Links*" 'visible))
  8495. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8496. (unless (pos-visible-in-window-p (point-max))
  8497. (org-fit-window-to-buffer))
  8498. (and (window-live-p cw) (select-window cw)))
  8499. ;; Fake a link history, containing the stored links.
  8500. (setq tmphist (append (mapcar 'car org-stored-links)
  8501. org-insert-link-history))
  8502. (setq all-prefixes (append (mapcar 'car abbrevs)
  8503. (mapcar 'car org-link-abbrev-alist)
  8504. org-link-types))
  8505. (unwind-protect
  8506. (progn
  8507. (setq link
  8508. (let ((org-completion-use-ido nil)
  8509. (org-completion-use-iswitchb nil))
  8510. (org-completing-read
  8511. "Link: "
  8512. (append
  8513. (mapcar (lambda (x) (list (concat x ":")))
  8514. all-prefixes)
  8515. (mapcar 'car org-stored-links)
  8516. (mapcar 'cadr org-stored-links))
  8517. nil nil nil
  8518. 'tmphist
  8519. (caar org-stored-links))))
  8520. (if (not (string-match "\\S-" link))
  8521. (error "No link selected"))
  8522. (mapc (lambda(l)
  8523. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8524. org-stored-links)
  8525. (if (or (member link all-prefixes)
  8526. (and (equal ":" (substring link -1))
  8527. (member (substring link 0 -1) all-prefixes)
  8528. (setq link (substring link 0 -1))))
  8529. (setq link (with-current-buffer origbuf
  8530. (org-link-try-special-completion link)))))
  8531. (set-window-configuration wcf)
  8532. (kill-buffer "*Org Links*"))
  8533. (setq entry (assoc link org-stored-links))
  8534. (or entry (push link org-insert-link-history))
  8535. (setq desc (or desc (nth 1 entry)))))
  8536. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8537. (not org-keep-stored-link-after-insertion))
  8538. (setq org-stored-links (delq (assoc link org-stored-links)
  8539. org-stored-links)))
  8540. (if (string-match org-plain-link-re link)
  8541. ;; URL-like link, normalize the use of angular brackets.
  8542. (setq link (org-remove-angle-brackets link)))
  8543. ;; Check if we are linking to the current file with a search option
  8544. ;; If yes, simplify the link by using only the search option.
  8545. (when (and buffer-file-name
  8546. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8547. (let* ((path (match-string 1 link))
  8548. (case-fold-search nil)
  8549. (search (match-string 2 link)))
  8550. (save-match-data
  8551. (if (equal (file-truename buffer-file-name) (file-truename path))
  8552. ;; We are linking to this same file, with a search option
  8553. (setq link search)))))
  8554. ;; Check if we can/should use a relative path. If yes, simplify the link
  8555. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8556. (let* ((type (match-string 1 link))
  8557. (path (match-string 2 link))
  8558. (origpath path)
  8559. (case-fold-search nil))
  8560. (cond
  8561. ((or (eq org-link-file-path-type 'absolute)
  8562. (equal complete-file '(16)))
  8563. (setq path (abbreviate-file-name (expand-file-name path))))
  8564. ((eq org-link-file-path-type 'noabbrev)
  8565. (setq path (expand-file-name path)))
  8566. ((eq org-link-file-path-type 'relative)
  8567. (setq path (file-relative-name path)))
  8568. (t
  8569. (save-match-data
  8570. (if (string-match (concat "^" (regexp-quote
  8571. (expand-file-name
  8572. (file-name-as-directory
  8573. default-directory))))
  8574. (expand-file-name path))
  8575. ;; We are linking a file with relative path name.
  8576. (setq path (substring (expand-file-name path)
  8577. (match-end 0)))
  8578. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8579. (setq link (concat type path))
  8580. (if (equal desc origpath)
  8581. (setq desc path))))
  8582. (if org-make-link-description-function
  8583. (setq desc (funcall org-make-link-description-function link desc))
  8584. (if default-description (setq desc default-description)
  8585. (setq desc (or (and auto-desc desc)
  8586. (read-string "Description: " desc)))))
  8587. (unless (string-match "\\S-" desc) (setq desc nil))
  8588. (if remove (apply 'delete-region remove))
  8589. (insert (org-make-link-string link desc))))
  8590. (defun org-link-try-special-completion (type)
  8591. "If there is completion support for link type TYPE, offer it."
  8592. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8593. (if (functionp fun)
  8594. (funcall fun)
  8595. (read-string "Link (no completion support): " (concat type ":")))))
  8596. (defun org-file-complete-link (&optional arg)
  8597. "Create a file link using completion."
  8598. (let (file link)
  8599. (setq file (read-file-name "File: "))
  8600. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8601. (pwd1 (file-name-as-directory (abbreviate-file-name
  8602. (expand-file-name ".")))))
  8603. (cond
  8604. ((equal arg '(16))
  8605. (setq link (concat
  8606. "file:"
  8607. (abbreviate-file-name (expand-file-name file)))))
  8608. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8609. (setq link (concat "file:" (match-string 1 file))))
  8610. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8611. (expand-file-name file))
  8612. (setq link (concat
  8613. "file:" (match-string 1 (expand-file-name file)))))
  8614. (t (setq link (concat "file:" file)))))
  8615. link))
  8616. (defun org-completing-read (&rest args)
  8617. "Completing-read with SPACE being a normal character."
  8618. (let ((enable-recursive-minibuffers t)
  8619. (minibuffer-local-completion-map
  8620. (copy-keymap minibuffer-local-completion-map)))
  8621. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8622. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8623. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8624. (apply 'org-icompleting-read args)))
  8625. (defun org-completing-read-no-i (&rest args)
  8626. (let (org-completion-use-ido org-completion-use-iswitchb)
  8627. (apply 'org-completing-read args)))
  8628. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8629. "Use iswitch as a completing-read replacement to choose from choices.
  8630. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8631. from."
  8632. (let* ((iswitchb-use-virtual-buffers nil)
  8633. (iswitchb-make-buflist-hook
  8634. (lambda ()
  8635. (setq iswitchb-temp-buflist choices))))
  8636. (iswitchb-read-buffer prompt)))
  8637. (defun org-icompleting-read (&rest args)
  8638. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8639. (org-without-partial-completion
  8640. (if (and org-completion-use-ido
  8641. (fboundp 'ido-completing-read)
  8642. (boundp 'ido-mode) ido-mode
  8643. (listp (second args)))
  8644. (let ((ido-enter-matching-directory nil))
  8645. (apply 'ido-completing-read (concat (car args))
  8646. (if (consp (car (nth 1 args)))
  8647. (mapcar 'car (nth 1 args))
  8648. (nth 1 args))
  8649. (cddr args)))
  8650. (if (and org-completion-use-iswitchb
  8651. (boundp 'iswitchb-mode) iswitchb-mode
  8652. (listp (second args)))
  8653. (apply 'org-iswitchb-completing-read (concat (car args))
  8654. (if (consp (car (nth 1 args)))
  8655. (mapcar 'car (nth 1 args))
  8656. (nth 1 args))
  8657. (cddr args))
  8658. (apply 'completing-read args)))))
  8659. (defun org-extract-attributes (s)
  8660. "Extract the attributes cookie from a string and set as text property."
  8661. (let (a attr (start 0) key value)
  8662. (save-match-data
  8663. (when (string-match "{{\\([^}]+\\)}}$" s)
  8664. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8665. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8666. (setq key (match-string 1 a) value (match-string 2 a)
  8667. start (match-end 0)
  8668. attr (plist-put attr (intern key) value))))
  8669. (org-add-props s nil 'org-attr attr))
  8670. s))
  8671. (defun org-extract-attributes-from-string (tag)
  8672. (let (key value attr)
  8673. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8674. (setq key (match-string 1 tag) value (match-string 2 tag)
  8675. tag (replace-match "" t t tag)
  8676. attr (plist-put attr (intern key) value)))
  8677. (cons tag attr)))
  8678. (defun org-attributes-to-string (plist)
  8679. "Format a property list into an HTML attribute list."
  8680. (let ((s "") key value)
  8681. (while plist
  8682. (setq key (pop plist) value (pop plist))
  8683. (and value
  8684. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8685. s))
  8686. ;;; Opening/following a link
  8687. (defvar org-link-search-failed nil)
  8688. (defvar org-open-link-functions nil
  8689. "Hook for functions finding a plain text link.
  8690. These functions must take a single argument, the link content.
  8691. They will be called for links that look like [[link text][description]]
  8692. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8693. like a filename (e.g. \"./blue.png\").
  8694. These functions will be called *before* Org attempts to resolve the
  8695. link by doing text searches in the current buffer - so if you want a
  8696. link \"[[target]]\" to still find \"<<target>>\", your function should
  8697. handle this as a special case.
  8698. When the function does handle the link, it must return a non-nil value.
  8699. If it decides that it is not responsible for this link, it must return
  8700. nil to indicate that that Org-mode can continue with other options
  8701. like exact and fuzzy text search.")
  8702. (defun org-next-link ()
  8703. "Move forward to the next link.
  8704. If the link is in hidden text, expose it."
  8705. (interactive)
  8706. (when (and org-link-search-failed (eq this-command last-command))
  8707. (goto-char (point-min))
  8708. (message "Link search wrapped back to beginning of buffer"))
  8709. (setq org-link-search-failed nil)
  8710. (let* ((pos (point))
  8711. (ct (org-context))
  8712. (a (assoc :link ct)))
  8713. (if a (goto-char (nth 2 a)))
  8714. (if (re-search-forward org-any-link-re nil t)
  8715. (progn
  8716. (goto-char (match-beginning 0))
  8717. (if (outline-invisible-p) (org-show-context)))
  8718. (goto-char pos)
  8719. (setq org-link-search-failed t)
  8720. (error "No further link found"))))
  8721. (defun org-previous-link ()
  8722. "Move backward to the previous link.
  8723. If the link is in hidden text, expose it."
  8724. (interactive)
  8725. (when (and org-link-search-failed (eq this-command last-command))
  8726. (goto-char (point-max))
  8727. (message "Link search wrapped back to end of buffer"))
  8728. (setq org-link-search-failed nil)
  8729. (let* ((pos (point))
  8730. (ct (org-context))
  8731. (a (assoc :link ct)))
  8732. (if a (goto-char (nth 1 a)))
  8733. (if (re-search-backward org-any-link-re nil t)
  8734. (progn
  8735. (goto-char (match-beginning 0))
  8736. (if (outline-invisible-p) (org-show-context)))
  8737. (goto-char pos)
  8738. (setq org-link-search-failed t)
  8739. (error "No further link found"))))
  8740. (defun org-translate-link (s)
  8741. "Translate a link string if a translation function has been defined."
  8742. (if (and org-link-translation-function
  8743. (fboundp org-link-translation-function)
  8744. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8745. (progn
  8746. (setq s (funcall org-link-translation-function
  8747. (match-string 1 s) (match-string 2 s)))
  8748. (concat (car s) ":" (cdr s)))
  8749. s))
  8750. (defun org-translate-link-from-planner (type path)
  8751. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8752. This is still an experimental function, your mileage may vary."
  8753. (cond
  8754. ((member type '("http" "https" "news" "ftp"))
  8755. ;; standard Internet links are the same.
  8756. nil)
  8757. ((and (equal type "irc") (string-match "^//" path))
  8758. ;; Planner has two / at the beginning of an irc link, we have 1.
  8759. ;; We should have zero, actually....
  8760. (setq path (substring path 1)))
  8761. ((and (equal type "lisp") (string-match "^/" path))
  8762. ;; Planner has a slash, we do not.
  8763. (setq type "elisp" path (substring path 1)))
  8764. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8765. ;; A typical message link. Planner has the id after the final slash,
  8766. ;; we separate it with a hash mark
  8767. (setq path (concat (match-string 1 path) "#"
  8768. (org-remove-angle-brackets (match-string 2 path)))))
  8769. )
  8770. (cons type path))
  8771. (defun org-find-file-at-mouse (ev)
  8772. "Open file link or URL at mouse."
  8773. (interactive "e")
  8774. (mouse-set-point ev)
  8775. (org-open-at-point 'in-emacs))
  8776. (defun org-open-at-mouse (ev)
  8777. "Open file link or URL at mouse.
  8778. See the docstring of `org-open-file' for details."
  8779. (interactive "e")
  8780. (mouse-set-point ev)
  8781. (if (eq major-mode 'org-agenda-mode)
  8782. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8783. (org-open-at-point))
  8784. (defvar org-window-config-before-follow-link nil
  8785. "The window configuration before following a link.
  8786. This is saved in case the need arises to restore it.")
  8787. (defvar org-open-link-marker (make-marker)
  8788. "Marker pointing to the location where `org-open-at-point; was called.")
  8789. ;;;###autoload
  8790. (defun org-open-at-point-global ()
  8791. "Follow a link like Org-mode does.
  8792. This command can be called in any mode to follow a link that has
  8793. Org-mode syntax."
  8794. (interactive)
  8795. (org-run-like-in-org-mode 'org-open-at-point))
  8796. ;;;###autoload
  8797. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8798. "Open a link in the string S, as if it was in Org-mode."
  8799. (interactive "sLink: \nP")
  8800. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8801. (with-temp-buffer
  8802. (let ((org-inhibit-startup (not reference-buffer)))
  8803. (org-mode)
  8804. (insert s)
  8805. (goto-char (point-min))
  8806. (when reference-buffer
  8807. (setq org-link-abbrev-alist-local
  8808. (with-current-buffer reference-buffer
  8809. org-link-abbrev-alist-local)))
  8810. (org-open-at-point arg reference-buffer)))))
  8811. (defvar org-open-at-point-functions nil
  8812. "Hook that is run when following a link at point.
  8813. Functions in this hook must return t if they identify and follow
  8814. a link at point. If they don't find anything interesting at point,
  8815. they must return nil.")
  8816. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  8817. (defun org-open-at-point (&optional arg reference-buffer)
  8818. "Open link at or after point.
  8819. If there is no link at point, this function will search forward up to
  8820. the end of the current line.
  8821. Normally, files will be opened by an appropriate application. If the
  8822. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8823. With a double prefix argument, try to open outside of Emacs, in the
  8824. application the system uses for this file type."
  8825. (interactive "P")
  8826. ;; if in a code block, then open the block's results
  8827. (unless (call-interactively #'org-babel-open-src-block-result)
  8828. (org-load-modules-maybe)
  8829. (move-marker org-open-link-marker (point))
  8830. (setq org-window-config-before-follow-link (current-window-configuration))
  8831. (org-remove-occur-highlights nil nil t)
  8832. (cond
  8833. ((and (org-at-heading-p)
  8834. (not (org-at-timestamp-p t))
  8835. (not (org-in-regexp
  8836. (concat org-plain-link-re "\\|"
  8837. org-bracket-link-regexp "\\|"
  8838. org-angle-link-re "\\|"
  8839. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8840. (not (get-text-property (point) 'org-linked-text)))
  8841. (or (org-offer-links-in-entry arg)
  8842. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8843. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8844. ((and (org-at-timestamp-p t)
  8845. (not (org-in-regexp org-bracket-link-regexp)))
  8846. (org-follow-timestamp-link))
  8847. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8848. (not (org-in-regexp org-bracket-link-regexp)))
  8849. (org-footnote-action))
  8850. (t
  8851. (let (type path link line search (pos (point)))
  8852. (catch 'match
  8853. (save-excursion
  8854. (skip-chars-forward "^]\n\r")
  8855. (when (org-in-regexp org-bracket-link-regexp 1)
  8856. (setq link (org-extract-attributes
  8857. (org-link-unescape (org-match-string-no-properties 1))))
  8858. (while (string-match " *\n *" link)
  8859. (setq link (replace-match " " t t link)))
  8860. (setq link (org-link-expand-abbrev link))
  8861. (cond
  8862. ((or (file-name-absolute-p link)
  8863. (string-match "^\\.\\.?/" link))
  8864. (setq type "file" path link))
  8865. ((string-match org-link-re-with-space3 link)
  8866. (setq type (match-string 1 link) path (match-string 2 link)))
  8867. ((string-match "^help:+\\(.+\\)" link)
  8868. (setq type "help" path (match-string 1 link)))
  8869. (t (setq type "thisfile" path link)))
  8870. (throw 'match t)))
  8871. (when (get-text-property (point) 'org-linked-text)
  8872. (setq type "thisfile"
  8873. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8874. (1+ (point)) (point))
  8875. path (buffer-substring
  8876. (or (previous-single-property-change pos 'org-linked-text)
  8877. (point-min))
  8878. (or (next-single-property-change pos 'org-linked-text)
  8879. (point-max))))
  8880. (throw 'match t))
  8881. (save-excursion
  8882. (when (or (org-in-regexp org-angle-link-re)
  8883. (and (goto-char (car (org-in-regexp org-plain-link-re)))
  8884. (save-match-data (not (looking-back "\\[\\[")))))
  8885. (setq type (match-string 1)
  8886. path (org-link-unescape (match-string 2)))
  8887. (throw 'match t)))
  8888. (save-excursion
  8889. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8890. (setq type "tags"
  8891. path (match-string 1))
  8892. (while (string-match ":" path)
  8893. (setq path (replace-match "+" t t path)))
  8894. (throw 'match t)))
  8895. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8896. (setq type "tree-match"
  8897. path (match-string 1))
  8898. (throw 'match t)))
  8899. (unless path
  8900. (error "No link found"))
  8901. ;; switch back to reference buffer
  8902. ;; needed when if called in a temporary buffer through
  8903. ;; org-open-link-from-string
  8904. (with-current-buffer (or reference-buffer (current-buffer))
  8905. ;; Remove any trailing spaces in path
  8906. (if (string-match " +\\'" path)
  8907. (setq path (replace-match "" t t path)))
  8908. (if (and org-link-translation-function
  8909. (fboundp org-link-translation-function))
  8910. ;; Check if we need to translate the link
  8911. (let ((tmp (funcall org-link-translation-function type path)))
  8912. (setq type (car tmp) path (cdr tmp))))
  8913. (cond
  8914. ((assoc type org-link-protocols)
  8915. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8916. ((equal type "help")
  8917. (let ((f-or-v (intern path)))
  8918. (cond ((fboundp f-or-v)
  8919. (describe-function f-or-v))
  8920. ((boundp f-or-v)
  8921. (describe-variable f-or-v))
  8922. (t (error "Not a known function or variable")))))
  8923. ((equal type "mailto")
  8924. (let ((cmd (car org-link-mailto-program))
  8925. (args (cdr org-link-mailto-program)) args1
  8926. (address path) (subject "") a)
  8927. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8928. (setq address (match-string 1 path)
  8929. subject (org-link-escape (match-string 2 path))))
  8930. (while args
  8931. (cond
  8932. ((not (stringp (car args))) (push (pop args) args1))
  8933. (t (setq a (pop args))
  8934. (if (string-match "%a" a)
  8935. (setq a (replace-match address t t a)))
  8936. (if (string-match "%s" a)
  8937. (setq a (replace-match subject t t a)))
  8938. (push a args1))))
  8939. (apply cmd (nreverse args1))))
  8940. ((member type '("http" "https" "ftp" "news"))
  8941. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8942. (org-link-escape
  8943. path org-link-escape-chars-browser)
  8944. path))))
  8945. ((string= type "doi")
  8946. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  8947. (org-link-escape
  8948. path org-link-escape-chars-browser)
  8949. path))))
  8950. ((member type '("message"))
  8951. (browse-url (concat type ":" path)))
  8952. ((string= type "tags")
  8953. (org-tags-view arg path))
  8954. ((string= type "tree-match")
  8955. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8956. ((string= type "file")
  8957. (if (string-match "::\\([0-9]+\\)\\'" path)
  8958. (setq line (string-to-number (match-string 1 path))
  8959. path (substring path 0 (match-beginning 0)))
  8960. (if (string-match "::\\(.+\\)\\'" path)
  8961. (setq search (match-string 1 path)
  8962. path (substring path 0 (match-beginning 0)))))
  8963. (if (string-match "[*?{]" (file-name-nondirectory path))
  8964. (dired path)
  8965. (org-open-file path arg line search)))
  8966. ((string= type "shell")
  8967. (let ((buf (generate-new-buffer "*Org Shell Output"))
  8968. (cmd path))
  8969. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8970. (string-match org-confirm-shell-link-not-regexp cmd))
  8971. (not org-confirm-shell-link-function)
  8972. (funcall org-confirm-shell-link-function
  8973. (format "Execute \"%s\" in shell? "
  8974. (org-add-props cmd nil
  8975. 'face 'org-warning))))
  8976. (progn
  8977. (message "Executing %s" cmd)
  8978. (shell-command cmd buf)
  8979. (if (featurep 'midnight)
  8980. (setq clean-buffer-list-kill-buffer-names
  8981. (cons buf clean-buffer-list-kill-buffer-names))))
  8982. (error "Abort"))))
  8983. ((string= type "elisp")
  8984. (let ((cmd path))
  8985. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8986. (string-match org-confirm-elisp-link-not-regexp cmd))
  8987. (not org-confirm-elisp-link-function)
  8988. (funcall org-confirm-elisp-link-function
  8989. (format "Execute \"%s\" as elisp? "
  8990. (org-add-props cmd nil
  8991. 'face 'org-warning))))
  8992. (message "%s => %s" cmd
  8993. (if (equal (string-to-char cmd) ?\()
  8994. (eval (read cmd))
  8995. (call-interactively (read cmd))))
  8996. (error "Abort"))))
  8997. ((and (string= type "thisfile")
  8998. (run-hook-with-args-until-success
  8999. 'org-open-link-functions path)))
  9000. ((string= type "thisfile")
  9001. (if arg
  9002. (switch-to-buffer-other-window
  9003. (org-get-buffer-for-internal-link (current-buffer)))
  9004. (org-mark-ring-push))
  9005. (let ((cmd `(org-link-search
  9006. ,path
  9007. ,(cond ((equal arg '(4)) ''occur)
  9008. ((equal arg '(16)) ''org-occur))
  9009. ,pos)))
  9010. (condition-case nil (let ((org-link-search-inhibit-query t))
  9011. (eval cmd))
  9012. (error (progn (widen) (eval cmd))))))
  9013. (t (browse-url-at-point)))))))
  9014. (move-marker org-open-link-marker nil)
  9015. (run-hook-with-args 'org-follow-link-hook)))
  9016. (defun org-offer-links-in-entry (&optional nth zero)
  9017. "Offer links in the current entry and follow the selected link.
  9018. If there is only one link, follow it immediately as well.
  9019. If NTH is an integer, immediately pick the NTH link found.
  9020. If ZERO is a string, check also this string for a link, and if
  9021. there is one, offer it as link number zero."
  9022. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9023. "\\(" org-angle-link-re "\\)\\|"
  9024. "\\(" org-plain-link-re "\\)"))
  9025. (cnt ?0)
  9026. (in-emacs (if (integerp nth) nil nth))
  9027. have-zero end links link c)
  9028. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9029. (push (match-string 0 zero) links)
  9030. (setq cnt (1- cnt) have-zero t))
  9031. (save-excursion
  9032. (org-back-to-heading t)
  9033. (setq end (save-excursion (outline-next-heading) (point)))
  9034. (while (re-search-forward re end t)
  9035. (push (match-string 0) links))
  9036. (setq links (org-uniquify (reverse links))))
  9037. (cond
  9038. ((null links)
  9039. (message "No links"))
  9040. ((equal (length links) 1)
  9041. (setq link (list (car links))))
  9042. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9043. (setq link (list (nth (if have-zero nth (1- nth)) links))))
  9044. (t ; we have to select a link
  9045. (save-excursion
  9046. (save-window-excursion
  9047. (delete-other-windows)
  9048. (with-output-to-temp-buffer "*Select Link*"
  9049. (mapc (lambda (l)
  9050. (if (not (string-match org-bracket-link-regexp l))
  9051. (princ (format "[%c] %s\n" (incf cnt)
  9052. (org-remove-angle-brackets l)))
  9053. (if (match-end 3)
  9054. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9055. (match-string 3 l) (match-string 1 l)))
  9056. (princ (format "[%c] %s\n" (incf cnt)
  9057. (match-string 1 l))))))
  9058. links))
  9059. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9060. (message "Select link to open, RET to open all:")
  9061. (setq c (read-char-exclusive))
  9062. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9063. (when (equal c ?q) (error "Abort"))
  9064. (if (equal c ?\C-m)
  9065. (setq link links)
  9066. (setq nth (- c ?0))
  9067. (if have-zero (setq nth (1+ nth)))
  9068. (unless (and (integerp nth) (>= (length links) nth))
  9069. (error "Invalid link selection"))
  9070. (setq link (list (nth (1- nth) links))))))
  9071. (if link
  9072. (let ((buf (current-buffer)))
  9073. (dolist (l link)
  9074. (org-open-link-from-string l in-emacs buf))
  9075. t)
  9076. nil)))
  9077. ;; Add special file links that specify the way of opening
  9078. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9079. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9080. (defun org-open-file-with-system (path)
  9081. "Open file at PATH using the system way of opening it."
  9082. (org-open-file path 'system))
  9083. (defun org-open-file-with-emacs (path)
  9084. "Open file at PATH in Emacs."
  9085. (org-open-file path 'emacs))
  9086. (defun org-remove-file-link-modifiers ()
  9087. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  9088. (goto-char (point-min))
  9089. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  9090. (org-if-unprotected
  9091. (replace-match "file:" t t))))
  9092. (eval-after-load "org-exp"
  9093. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  9094. 'org-remove-file-link-modifiers))
  9095. ;;;; Time estimates
  9096. (defun org-get-effort (&optional pom)
  9097. "Get the effort estimate for the current entry."
  9098. (org-entry-get pom org-effort-property))
  9099. ;;; File search
  9100. (defvar org-create-file-search-functions nil
  9101. "List of functions to construct the right search string for a file link.
  9102. These functions are called in turn with point at the location to
  9103. which the link should point.
  9104. A function in the hook should first test if it would like to
  9105. handle this file type, for example by checking the `major-mode'
  9106. or the file extension. If it decides not to handle this file, it
  9107. should just return nil to give other functions a chance. If it
  9108. does handle the file, it must return the search string to be used
  9109. when following the link. The search string will be part of the
  9110. file link, given after a double colon, and `org-open-at-point'
  9111. will automatically search for it. If special measures must be
  9112. taken to make the search successful, another function should be
  9113. added to the companion hook `org-execute-file-search-functions',
  9114. which see.
  9115. A function in this hook may also use `setq' to set the variable
  9116. `description' to provide a suggestion for the descriptive text to
  9117. be used for this link when it gets inserted into an Org-mode
  9118. buffer with \\[org-insert-link].")
  9119. (defvar org-execute-file-search-functions nil
  9120. "List of functions to execute a file search triggered by a link.
  9121. Functions added to this hook must accept a single argument, the
  9122. search string that was part of the file link, the part after the
  9123. double colon. The function must first check if it would like to
  9124. handle this search, for example by checking the `major-mode' or
  9125. the file extension. If it decides not to handle this search, it
  9126. should just return nil to give other functions a chance. If it
  9127. does handle the search, it must return a non-nil value to keep
  9128. other functions from trying.
  9129. Each function can access the current prefix argument through the
  9130. variable `current-prefix-argument'. Note that a single prefix is
  9131. used to force opening a link in Emacs, so it may be good to only
  9132. use a numeric or double prefix to guide the search function.
  9133. In case this is needed, a function in this hook can also restore
  9134. the window configuration before `org-open-at-point' was called using:
  9135. (set-window-configuration org-window-config-before-follow-link)")
  9136. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9137. (defun org-link-search (s &optional type avoid-pos stealth)
  9138. "Search for a link search option.
  9139. If S is surrounded by forward slashes, it is interpreted as a
  9140. regular expression. In org-mode files, this will create an `org-occur'
  9141. sparse tree. In ordinary files, `occur' will be used to list matches.
  9142. If the current buffer is in `dired-mode', grep will be used to search
  9143. in all files. If AVOID-POS is given, ignore matches near that position.
  9144. When optional argument STEALTH is non-nil, do not modify
  9145. visibility around point, thus ignoring
  9146. `org-show-hierarchy-above', `org-show-following-heading' and
  9147. `org-show-siblings' variables."
  9148. (let ((case-fold-search t)
  9149. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9150. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9151. (append '(("") (" ") ("\t") ("\n"))
  9152. org-emphasis-alist)
  9153. "\\|") "\\)"))
  9154. (pos (point))
  9155. (pre nil) (post nil)
  9156. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9157. (cond
  9158. ;; First check if there are any special search functions
  9159. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9160. ;; Now try the builtin stuff
  9161. ((and (equal (string-to-char s0) ?#)
  9162. (> (length s0) 1)
  9163. (save-excursion
  9164. (goto-char (point-min))
  9165. (and
  9166. (re-search-forward
  9167. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9168. (setq type 'dedicated
  9169. pos (match-beginning 0))))
  9170. ;; There is an exact target for this
  9171. (goto-char pos)
  9172. (org-back-to-heading t)))
  9173. ((save-excursion
  9174. (goto-char (point-min))
  9175. (and
  9176. (re-search-forward
  9177. (concat "<<" (regexp-quote s0) ">>") nil t)
  9178. (setq type 'dedicated
  9179. pos (match-beginning 0))))
  9180. ;; There is an exact target for this
  9181. (goto-char pos))
  9182. ((save-excursion
  9183. (goto-char (point-min))
  9184. (and
  9185. (re-search-forward
  9186. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9187. (setq type 'dedicated pos (match-beginning 0))))
  9188. ;; Found an invisible target.
  9189. (goto-char pos))
  9190. ((save-excursion
  9191. (goto-char (point-min))
  9192. (and
  9193. (re-search-forward
  9194. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9195. (setq type 'dedicated pos (match-beginning 0))))
  9196. ;; Found an element with a matching #+name affiliated keyword.
  9197. (goto-char pos))
  9198. ((and (string-match "^(\\(.*\\))$" s0)
  9199. (save-excursion
  9200. (goto-char (point-min))
  9201. (and
  9202. (re-search-forward
  9203. (concat "[^[]" (regexp-quote
  9204. (format org-coderef-label-format
  9205. (match-string 1 s0))))
  9206. nil t)
  9207. (setq type 'dedicated
  9208. pos (1+ (match-beginning 0))))))
  9209. ;; There is a coderef target for this
  9210. (goto-char pos))
  9211. ((string-match "^/\\(.*\\)/$" s)
  9212. ;; A regular expression
  9213. (cond
  9214. ((derived-mode-p 'org-mode)
  9215. (org-occur (match-string 1 s)))
  9216. ;;((eq major-mode 'dired-mode)
  9217. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  9218. (t (org-do-occur (match-string 1 s)))))
  9219. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9220. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9221. (goto-char (point-min))
  9222. (cond
  9223. ((let (case-fold-search)
  9224. (re-search-forward (format org-complex-heading-regexp-format
  9225. (regexp-quote s))
  9226. nil t))
  9227. ;; OK, found a match
  9228. (setq type 'dedicated)
  9229. (goto-char (match-beginning 0)))
  9230. ((and (not org-link-search-inhibit-query)
  9231. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9232. (y-or-n-p "No match - create this as a new heading? "))
  9233. (goto-char (point-max))
  9234. (or (bolp) (newline))
  9235. (insert "* " s "\n")
  9236. (beginning-of-line 0))
  9237. (t
  9238. (goto-char pos)
  9239. (error "No match"))))
  9240. (t
  9241. ;; A normal search string
  9242. (when (equal (string-to-char s) ?*)
  9243. ;; Anchor on headlines, post may include tags.
  9244. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9245. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9246. s (substring s 1)))
  9247. (remove-text-properties
  9248. 0 (length s)
  9249. '(face nil mouse-face nil keymap nil fontified nil) s)
  9250. ;; Make a series of regular expressions to find a match
  9251. (setq words (org-split-string s "[ \n\r\t]+")
  9252. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9253. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9254. "\\)" markers)
  9255. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9256. re2a (concat "[ \t\r\n]" re2a_)
  9257. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9258. re4 (concat "[^a-zA-Z_]" re4_)
  9259. re1 (concat pre re2 post)
  9260. re3 (concat pre (if pre re4_ re4) post)
  9261. re5 (concat pre ".*" re4)
  9262. re2 (concat pre re2)
  9263. re2a (concat pre (if pre re2a_ re2a))
  9264. re4 (concat pre (if pre re4_ re4))
  9265. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9266. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9267. re5 "\\)"
  9268. ))
  9269. (cond
  9270. ((eq type 'org-occur) (org-occur reall))
  9271. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9272. (t (goto-char (point-min))
  9273. (setq type 'fuzzy)
  9274. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9275. (org-search-not-self 1 re1 nil t)
  9276. (org-search-not-self 1 re2 nil t)
  9277. (org-search-not-self 1 re2a nil t)
  9278. (org-search-not-self 1 re3 nil t)
  9279. (org-search-not-self 1 re4 nil t)
  9280. (org-search-not-self 1 re5 nil t)
  9281. )
  9282. (goto-char (match-beginning 1))
  9283. (goto-char pos)
  9284. (error "No match"))))))
  9285. (and (derived-mode-p 'org-mode)
  9286. (not stealth)
  9287. (org-show-context 'link-search))
  9288. type))
  9289. (defun org-search-not-self (group &rest args)
  9290. "Execute `re-search-forward', but only accept matches that do not
  9291. enclose the position of `org-open-link-marker'."
  9292. (let ((m org-open-link-marker))
  9293. (catch 'exit
  9294. (while (apply 're-search-forward args)
  9295. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9296. (goto-char (match-end group))
  9297. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9298. (> (match-beginning 0) (marker-position m))
  9299. (< (match-end 0) (marker-position m)))
  9300. (save-match-data
  9301. (or (not (org-in-regexp
  9302. org-bracket-link-analytic-regexp 1))
  9303. (not (match-end 4)) ; no description
  9304. (and (<= (match-beginning 4) (point))
  9305. (>= (match-end 4) (point))))))
  9306. (throw 'exit (point))))))))
  9307. (defun org-get-buffer-for-internal-link (buffer)
  9308. "Return a buffer to be used for displaying the link target of internal links."
  9309. (cond
  9310. ((not org-display-internal-link-with-indirect-buffer)
  9311. buffer)
  9312. ((string-match "(Clone)$" (buffer-name buffer))
  9313. (message "Buffer is already a clone, not making another one")
  9314. ;; we also do not modify visibility in this case
  9315. buffer)
  9316. (t ; make a new indirect buffer for displaying the link
  9317. (let* ((bn (buffer-name buffer))
  9318. (ibn (concat bn "(Clone)"))
  9319. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9320. (with-current-buffer ib (org-overview))
  9321. ib))))
  9322. (defun org-do-occur (regexp &optional cleanup)
  9323. "Call the Emacs command `occur'.
  9324. If CLEANUP is non-nil, remove the printout of the regular expression
  9325. in the *Occur* buffer. This is useful if the regex is long and not useful
  9326. to read."
  9327. (occur regexp)
  9328. (when cleanup
  9329. (let ((cwin (selected-window)) win beg end)
  9330. (when (setq win (get-buffer-window "*Occur*"))
  9331. (select-window win))
  9332. (goto-char (point-min))
  9333. (when (re-search-forward "match[a-z]+" nil t)
  9334. (setq beg (match-end 0))
  9335. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9336. (setq end (1- (match-beginning 0)))))
  9337. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9338. (goto-char (point-min))
  9339. (select-window cwin))))
  9340. ;;; The mark ring for links jumps
  9341. (defvar org-mark-ring nil
  9342. "Mark ring for positions before jumps in Org-mode.")
  9343. (defvar org-mark-ring-last-goto nil
  9344. "Last position in the mark ring used to go back.")
  9345. ;; Fill and close the ring
  9346. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9347. (loop for i from 1 to org-mark-ring-length do
  9348. (push (make-marker) org-mark-ring))
  9349. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9350. org-mark-ring)
  9351. (defun org-mark-ring-push (&optional pos buffer)
  9352. "Put the current position or POS into the mark ring and rotate it."
  9353. (interactive)
  9354. (setq pos (or pos (point)))
  9355. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9356. (move-marker (car org-mark-ring)
  9357. (or pos (point))
  9358. (or buffer (current-buffer)))
  9359. (message "%s"
  9360. (substitute-command-keys
  9361. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9362. (defun org-mark-ring-goto (&optional n)
  9363. "Jump to the previous position in the mark ring.
  9364. With prefix arg N, jump back that many stored positions. When
  9365. called several times in succession, walk through the entire ring.
  9366. Org-mode commands jumping to a different position in the current file,
  9367. or to another Org-mode file, automatically push the old position
  9368. onto the ring."
  9369. (interactive "p")
  9370. (let (p m)
  9371. (if (eq last-command this-command)
  9372. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9373. (setq p org-mark-ring))
  9374. (setq org-mark-ring-last-goto p)
  9375. (setq m (car p))
  9376. (org-pop-to-buffer-same-window (marker-buffer m))
  9377. (goto-char m)
  9378. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9379. (defun org-remove-angle-brackets (s)
  9380. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9381. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9382. s)
  9383. (defun org-add-angle-brackets (s)
  9384. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9385. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9386. s)
  9387. (defun org-remove-double-quotes (s)
  9388. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9389. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9390. s)
  9391. ;;; Following specific links
  9392. (defun org-follow-timestamp-link ()
  9393. "Open an agenda view for the time-stamp date/range at point."
  9394. (cond
  9395. ((org-at-date-range-p t)
  9396. (let ((org-agenda-start-on-weekday)
  9397. (t1 (match-string 1))
  9398. (t2 (match-string 2)) tt1 tt2)
  9399. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9400. tt2 (time-to-days (org-time-string-to-time t2)))
  9401. (let ((org-agenda-buffer-tmp-name
  9402. (format "*Org Agenda(a:%s)"
  9403. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9404. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9405. ((org-at-timestamp-p t)
  9406. (let ((org-agenda-buffer-tmp-name
  9407. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9408. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9409. (substring (match-string 1) 0 10)))
  9410. 1)))
  9411. (t (error "This should not happen"))))
  9412. ;;; Following file links
  9413. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9414. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9415. (declare-function mailcap-mime-info
  9416. "mailcap" (string &optional request no-decode))
  9417. (defvar org-wait nil)
  9418. (defun org-open-file (path &optional in-emacs line search)
  9419. "Open the file at PATH.
  9420. First, this expands any special file name abbreviations. Then the
  9421. configuration variable `org-file-apps' is checked if it contains an
  9422. entry for this file type, and if yes, the corresponding command is launched.
  9423. If no application is found, Emacs simply visits the file.
  9424. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9425. With a double \\[universal-argument] \\[universal-argument] \
  9426. prefix arg, Org tries to avoid opening in Emacs
  9427. and to use an external application to visit the file.
  9428. Optional LINE specifies a line to go to, optional SEARCH a string
  9429. to search for. If LINE or SEARCH is given, the file will be
  9430. opened in Emacs, unless an entry from org-file-apps that makes
  9431. use of groups in a regexp matches.
  9432. If you want to change the way frames are used when following a
  9433. link, please customize `org-link-frame-setup'.
  9434. If the file does not exist, an error is thrown."
  9435. (let* ((file (if (equal path "")
  9436. buffer-file-name
  9437. (substitute-in-file-name (expand-file-name path))))
  9438. (file-apps (append org-file-apps (org-default-apps)))
  9439. (apps (org-remove-if
  9440. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9441. (apps-dlink (org-remove-if-not
  9442. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9443. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9444. (dirp (if remp nil (file-directory-p file)))
  9445. (file (if (and dirp org-open-directory-means-index-dot-org)
  9446. (concat (file-name-as-directory file) "index.org")
  9447. file))
  9448. (a-m-a-p (assq 'auto-mode apps))
  9449. (dfile (downcase file))
  9450. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9451. (link (cond ((and (eq line nil)
  9452. (eq search nil))
  9453. file)
  9454. (line
  9455. (concat file "::" (number-to-string line)))
  9456. (search
  9457. (concat file "::" search))))
  9458. (dlink (downcase link))
  9459. (old-buffer (current-buffer))
  9460. (old-pos (point))
  9461. (old-mode major-mode)
  9462. ext cmd link-match-data)
  9463. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9464. (setq ext (match-string 1 dfile))
  9465. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9466. (setq ext (match-string 1 dfile))))
  9467. (cond
  9468. ((member in-emacs '((16) system))
  9469. (setq cmd (cdr (assoc 'system apps))))
  9470. (in-emacs (setq cmd 'emacs))
  9471. (t
  9472. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9473. (and dirp (cdr (assoc 'directory apps)))
  9474. ; first, try matching against apps-dlink
  9475. ; if we get a match here, store the match data for later
  9476. (let ((match (assoc-default dlink apps-dlink
  9477. 'string-match)))
  9478. (if match
  9479. (progn (setq link-match-data (match-data))
  9480. match)
  9481. (progn (setq in-emacs (or in-emacs line search))
  9482. nil))) ; if we have no match in apps-dlink,
  9483. ; always open the file in emacs if line or search
  9484. ; is given (for backwards compatibility)
  9485. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9486. 'string-match)
  9487. (cdr (assoc ext apps))
  9488. (cdr (assoc t apps))))))
  9489. (when (eq cmd 'system)
  9490. (setq cmd (cdr (assoc 'system apps))))
  9491. (when (eq cmd 'default)
  9492. (setq cmd (cdr (assoc t apps))))
  9493. (when (eq cmd 'mailcap)
  9494. (require 'mailcap)
  9495. (mailcap-parse-mailcaps)
  9496. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9497. (command (mailcap-mime-info mime-type)))
  9498. (if (stringp command)
  9499. (setq cmd command)
  9500. (setq cmd 'emacs))))
  9501. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9502. (not (file-exists-p file))
  9503. (not org-open-non-existing-files))
  9504. (error "No such file: %s" file))
  9505. (cond
  9506. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9507. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9508. (while (string-match "['\"]%s['\"]" cmd)
  9509. (setq cmd (replace-match "%s" t t cmd)))
  9510. (while (string-match "%s" cmd)
  9511. (setq cmd (replace-match
  9512. (save-match-data
  9513. (shell-quote-argument
  9514. (convert-standard-filename file)))
  9515. t t cmd)))
  9516. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9517. (save-match-data
  9518. (let ((match-index 1)
  9519. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9520. (set-match-data link-match-data)
  9521. (while (<= match-index number-of-groups)
  9522. (let ((regex (concat "%" (number-to-string match-index)))
  9523. (replace-with (match-string match-index dlink)))
  9524. (while (string-match regex cmd)
  9525. (setq cmd (replace-match replace-with t t cmd))))
  9526. (setq match-index (+ match-index 1)))))
  9527. (save-window-excursion
  9528. (message "Running %s...done" cmd)
  9529. (start-process-shell-command cmd nil cmd)
  9530. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9531. ((or (stringp cmd)
  9532. (eq cmd 'emacs))
  9533. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9534. (widen)
  9535. (if line (org-goto-line line)
  9536. (if search (org-link-search search))))
  9537. ((consp cmd)
  9538. (let ((file (convert-standard-filename file)))
  9539. (save-match-data
  9540. (set-match-data link-match-data)
  9541. (eval cmd))))
  9542. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9543. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9544. (or (not (equal old-buffer (current-buffer)))
  9545. (not (equal old-pos (point))))
  9546. (org-mark-ring-push old-pos old-buffer))))
  9547. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9548. "This function returns non-nil if `entry' uses a regular
  9549. expression which should be matched against the whole link by
  9550. org-open-file.
  9551. It assumes that is the case when the entry uses a regular
  9552. expression which has at least one grouping construct and the
  9553. action is either a lisp form or a command string containing
  9554. '%1', i.e. using at least one subexpression match as a
  9555. parameter."
  9556. (let ((selector (car entry))
  9557. (action (cdr entry)))
  9558. (if (stringp selector)
  9559. (and (> (regexp-opt-depth selector) 0)
  9560. (or (and (stringp action)
  9561. (string-match "%[0-9]" action))
  9562. (consp action)))
  9563. nil)))
  9564. (defun org-default-apps ()
  9565. "Return the default applications for this operating system."
  9566. (cond
  9567. ((eq system-type 'darwin)
  9568. org-file-apps-defaults-macosx)
  9569. ((eq system-type 'windows-nt)
  9570. org-file-apps-defaults-windowsnt)
  9571. (t org-file-apps-defaults-gnu)))
  9572. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9573. "Convert extensions to regular expressions in the cars of LIST.
  9574. Also, weed out any non-string entries, because the return value is used
  9575. only for regexp matching.
  9576. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9577. point to the symbol `emacs', indicating that the file should
  9578. be opened in Emacs."
  9579. (append
  9580. (delq nil
  9581. (mapcar (lambda (x)
  9582. (if (not (stringp (car x)))
  9583. nil
  9584. (if (string-match "\\W" (car x))
  9585. x
  9586. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9587. list))
  9588. (if add-auto-mode
  9589. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9590. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9591. (defun org-file-remote-p (file)
  9592. "Test whether FILE specifies a location on a remote system.
  9593. Return non-nil if the location is indeed remote.
  9594. For example, the filename \"/user@host:/foo\" specifies a location
  9595. on the system \"/user@host:\"."
  9596. (cond ((fboundp 'file-remote-p)
  9597. (file-remote-p file))
  9598. ((fboundp 'tramp-handle-file-remote-p)
  9599. (tramp-handle-file-remote-p file))
  9600. ((and (boundp 'ange-ftp-name-format)
  9601. (string-match (car ange-ftp-name-format) file))
  9602. t)))
  9603. ;;;; Refiling
  9604. (defun org-get-org-file ()
  9605. "Read a filename, with default directory `org-directory'."
  9606. (let ((default (or org-default-notes-file remember-data-file)))
  9607. (read-file-name (format "File name [%s]: " default)
  9608. (file-name-as-directory org-directory)
  9609. default)))
  9610. (defun org-notes-order-reversed-p ()
  9611. "Check if the current file should receive notes in reversed order."
  9612. (cond
  9613. ((not org-reverse-note-order) nil)
  9614. ((eq t org-reverse-note-order) t)
  9615. ((not (listp org-reverse-note-order)) nil)
  9616. (t (catch 'exit
  9617. (let ((all org-reverse-note-order)
  9618. entry)
  9619. (while (setq entry (pop all))
  9620. (if (string-match (car entry) buffer-file-name)
  9621. (throw 'exit (cdr entry))))
  9622. nil)))))
  9623. (defvar org-refile-target-table nil
  9624. "The list of refile targets, created by `org-refile'.")
  9625. (defvar org-agenda-new-buffers nil
  9626. "Buffers created to visit agenda files.")
  9627. (defvar org-refile-cache nil
  9628. "Cache for refile targets.")
  9629. (defvar org-refile-markers nil
  9630. "All the markers used for caching refile locations.")
  9631. (defun org-refile-marker (pos)
  9632. "Get a new refile marker, but only if caching is in use."
  9633. (if (not org-refile-use-cache)
  9634. pos
  9635. (let ((m (make-marker)))
  9636. (move-marker m pos)
  9637. (push m org-refile-markers)
  9638. m)))
  9639. (defun org-refile-cache-clear ()
  9640. "Clear the refile cache and disable all the markers."
  9641. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9642. (setq org-refile-markers nil)
  9643. (setq org-refile-cache nil)
  9644. (message "Refile cache has been cleared"))
  9645. (defun org-refile-cache-check-set (set)
  9646. "Check if all the markers in the cache still have live buffers."
  9647. (let (marker)
  9648. (catch 'exit
  9649. (while (and set (setq marker (nth 3 (pop set))))
  9650. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9651. (when (and marker (null (marker-buffer marker)))
  9652. (message "not found") (sit-for 3)
  9653. (throw 'exit nil)))
  9654. t)))
  9655. (defun org-refile-cache-put (set &rest identifiers)
  9656. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9657. (let* ((key (sha1 (prin1-to-string identifiers)))
  9658. (entry (assoc key org-refile-cache)))
  9659. (if entry
  9660. (setcdr entry set)
  9661. (push (cons key set) org-refile-cache))))
  9662. (defun org-refile-cache-get (&rest identifiers)
  9663. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9664. (cond
  9665. ((not org-refile-cache) nil)
  9666. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9667. (t
  9668. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9669. org-refile-cache))))
  9670. (and set (org-refile-cache-check-set set) set)))))
  9671. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9672. "Produce a table with refile targets."
  9673. (let ((case-fold-search nil)
  9674. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9675. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9676. targets tgs txt re files f desc descre fast-path-p level pos0)
  9677. (message "Getting targets...")
  9678. (with-current-buffer (or default-buffer (current-buffer))
  9679. (while (setq entry (pop entries))
  9680. (setq files (car entry) desc (cdr entry))
  9681. (setq fast-path-p nil)
  9682. (cond
  9683. ((null files) (setq files (list (current-buffer))))
  9684. ((eq files 'org-agenda-files)
  9685. (setq files (org-agenda-files 'unrestricted)))
  9686. ((and (symbolp files) (fboundp files))
  9687. (setq files (funcall files)))
  9688. ((and (symbolp files) (boundp files))
  9689. (setq files (symbol-value files))))
  9690. (if (stringp files) (setq files (list files)))
  9691. (cond
  9692. ((eq (car desc) :tag)
  9693. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9694. ((eq (car desc) :todo)
  9695. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9696. ((eq (car desc) :regexp)
  9697. (setq descre (cdr desc)))
  9698. ((eq (car desc) :level)
  9699. (setq descre (concat "^\\*\\{" (number-to-string
  9700. (if org-odd-levels-only
  9701. (1- (* 2 (cdr desc)))
  9702. (cdr desc)))
  9703. "\\}[ \t]")))
  9704. ((eq (car desc) :maxlevel)
  9705. (setq fast-path-p t)
  9706. (setq descre (concat "^\\*\\{1," (number-to-string
  9707. (if org-odd-levels-only
  9708. (1- (* 2 (cdr desc)))
  9709. (cdr desc)))
  9710. "\\}[ \t]")))
  9711. (t (error "Bad refiling target description %s" desc)))
  9712. (while (setq f (pop files))
  9713. (with-current-buffer
  9714. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9715. (or
  9716. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9717. (progn
  9718. (if (bufferp f) (setq f (buffer-file-name
  9719. (buffer-base-buffer f))))
  9720. (setq f (and f (expand-file-name f)))
  9721. (if (eq org-refile-use-outline-path 'file)
  9722. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9723. (save-excursion
  9724. (save-restriction
  9725. (widen)
  9726. (goto-char (point-min))
  9727. (while (re-search-forward descre nil t)
  9728. (goto-char (setq pos0 (point-at-bol)))
  9729. (catch 'next
  9730. (when org-refile-target-verify-function
  9731. (save-match-data
  9732. (or (funcall org-refile-target-verify-function)
  9733. (throw 'next t))))
  9734. (when (and (looking-at org-complex-heading-regexp)
  9735. (not (member (match-string 4) excluded-entries))
  9736. (match-string 4))
  9737. (setq level (org-reduced-level
  9738. (- (match-end 1) (match-beginning 1)))
  9739. txt (org-link-display-format (match-string 4))
  9740. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9741. re (format org-complex-heading-regexp-format
  9742. (regexp-quote (match-string 4))))
  9743. (when org-refile-use-outline-path
  9744. (setq txt (mapconcat
  9745. 'org-protect-slash
  9746. (append
  9747. (if (eq org-refile-use-outline-path
  9748. 'file)
  9749. (list (file-name-nondirectory
  9750. (buffer-file-name
  9751. (buffer-base-buffer))))
  9752. (if (eq org-refile-use-outline-path
  9753. 'full-file-path)
  9754. (list (buffer-file-name
  9755. (buffer-base-buffer)))))
  9756. (org-get-outline-path fast-path-p
  9757. level txt)
  9758. (list txt))
  9759. "/")))
  9760. (push (list txt f re (org-refile-marker (point)))
  9761. tgs)))
  9762. (when (= (point) pos0)
  9763. ;; verification function has not moved point
  9764. (goto-char (point-at-eol))))))))
  9765. (when org-refile-use-cache
  9766. (org-refile-cache-put tgs (buffer-file-name) descre))
  9767. (setq targets (append tgs targets))
  9768. ))))
  9769. (message "Getting targets...done")
  9770. (nreverse targets)))
  9771. (defun org-protect-slash (s)
  9772. (while (string-match "/" s)
  9773. (setq s (replace-match "\\" t t s)))
  9774. s)
  9775. (defvar org-olpa (make-vector 20 nil))
  9776. (defun org-get-outline-path (&optional fastp level heading)
  9777. "Return the outline path to the current entry, as a list.
  9778. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9779. routine which makes outline path derivations for an entire file,
  9780. avoiding backtracing. Refile target collection makes use of that."
  9781. (if fastp
  9782. (progn
  9783. (if (> level 19)
  9784. (error "Outline path failure, more than 19 levels"))
  9785. (loop for i from level upto 19 do
  9786. (aset org-olpa i nil))
  9787. (prog1
  9788. (delq nil (append org-olpa nil))
  9789. (aset org-olpa level heading)))
  9790. (let (rtn case-fold-search)
  9791. (save-excursion
  9792. (save-restriction
  9793. (widen)
  9794. (while (org-up-heading-safe)
  9795. (when (looking-at org-complex-heading-regexp)
  9796. (push (org-match-string-no-properties 4) rtn)))
  9797. rtn)))))
  9798. (defun org-format-outline-path (path &optional width prefix separator)
  9799. "Format the outline path PATH for display.
  9800. WIDTH is the maximum number of characters that is available.
  9801. PREFIX is a prefix to be included in the returned string,
  9802. such as the file name.
  9803. SEPARATOR is inserted between the different parts of the path,
  9804. the default is \"/\"."
  9805. (setq width (or width 79))
  9806. (if prefix (setq width (- width (length prefix))))
  9807. (if (not path)
  9808. (or prefix "")
  9809. (let* ((nsteps (length path))
  9810. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9811. (maxwidth (if (<= total-width width)
  9812. 10000 ;; everything fits
  9813. ;; we need to shorten the level headings
  9814. (/ (- width nsteps) nsteps)))
  9815. (org-odd-levels-only nil)
  9816. (n 0)
  9817. (total (1+ (length prefix))))
  9818. (setq maxwidth (max maxwidth 10))
  9819. (concat prefix
  9820. (if prefix (or separator "/"))
  9821. (mapconcat
  9822. (lambda (h)
  9823. (setq n (1+ n))
  9824. (if (and (= n nsteps) (< maxwidth 10000))
  9825. (setq maxwidth (- total-width total)))
  9826. (if (< (length h) maxwidth)
  9827. (progn (setq total (+ total (length h) 1)) h)
  9828. (setq h (substring h 0 (- maxwidth 2))
  9829. total (+ total maxwidth 1))
  9830. (if (string-match "[ \t]+\\'" h)
  9831. (setq h (substring h 0 (match-beginning 0))))
  9832. (setq h (concat h "..")))
  9833. (org-add-props h nil 'face
  9834. (nth (% (1- n) org-n-level-faces)
  9835. org-level-faces))
  9836. h)
  9837. path (or separator "/"))))))
  9838. (defun org-display-outline-path (&optional file current separator just-return-string)
  9839. "Display the current outline path in the echo area.
  9840. If FILE is non-nil, prepend the output with the file name.
  9841. If CURRENT is non-nil, append the current heading to the output.
  9842. SEPARATOR is passed through to `org-format-outline-path'. It separates
  9843. the different parts of the path and defaults to \"/\".
  9844. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  9845. (interactive "P")
  9846. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9847. (case-fold-search nil)
  9848. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  9849. res)
  9850. (if current (setq path (append path
  9851. (save-excursion
  9852. (org-back-to-heading t)
  9853. (if (looking-at org-complex-heading-regexp)
  9854. (list (match-string 4)))))))
  9855. (setq res
  9856. (org-format-outline-path
  9857. path
  9858. (1- (frame-width))
  9859. (and file bfn (concat (file-name-nondirectory bfn) separator))
  9860. separator))
  9861. (if just-return-string
  9862. (org-no-properties res)
  9863. (message "%s" res))))
  9864. (defvar org-refile-history nil
  9865. "History for refiling operations.")
  9866. (defvar org-after-refile-insert-hook nil
  9867. "Hook run after `org-refile' has inserted its stuff at the new location.
  9868. Note that this is still *before* the stuff will be removed from
  9869. the *old* location.")
  9870. (defvar org-capture-last-stored-marker)
  9871. (defvar org-refile-keep nil
  9872. "Non-nil means `org-refile' will copy instead of refile.")
  9873. (defun org-copy ()
  9874. "Like `org-refile', but copy."
  9875. (interactive)
  9876. (let ((org-refile-keep t))
  9877. (funcall 'org-refile nil nil nil "Copy")))
  9878. (defun org-refile (&optional goto default-buffer rfloc msg)
  9879. "Move the entry or entries at point to another heading.
  9880. The list of target headings is compiled using the information in
  9881. `org-refile-targets', which see.
  9882. At the target location, the entry is filed as a subitem of the target
  9883. heading. Depending on `org-reverse-note-order', the new subitem will
  9884. either be the first or the last subitem.
  9885. If there is an active region, all entries in that region will be moved.
  9886. However, the region must fulfill the requirement that the first heading
  9887. is the first one sets the top-level of the moved text - at most siblings
  9888. below it are allowed.
  9889. With prefix arg GOTO, the command will only visit the target location
  9890. and not actually move anything.
  9891. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9892. go to the location where the last refiling operation has put the subtree.
  9893. With a prefix argument of `2', refile to the running clock.
  9894. RFLOC can be a refile location obtained in a different way.
  9895. MSG is a string to replace \"Refile\" in the default prompt with
  9896. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  9897. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9898. If you are using target caching (see `org-refile-use-cache'),
  9899. you have to clear the target cache in order to find new targets.
  9900. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9901. prefix argument (`C-u C-u C-u C-c C-w')."
  9902. (interactive "P")
  9903. (if (member goto '(0 (64)))
  9904. (org-refile-cache-clear)
  9905. (let* ((actionmsg (or msg "Refile"))
  9906. (cbuf (current-buffer))
  9907. (regionp (org-region-active-p))
  9908. (region-start (and regionp (region-beginning)))
  9909. (region-end (and regionp (region-end)))
  9910. (region-length (and regionp (- region-end region-start)))
  9911. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9912. pos it nbuf file re level reversed)
  9913. (setq last-command nil)
  9914. (when regionp
  9915. (goto-char region-start)
  9916. (or (bolp) (goto-char (point-at-bol)))
  9917. (setq region-start (point))
  9918. (unless (or (org-kill-is-subtree-p
  9919. (buffer-substring region-start region-end))
  9920. (prog1 org-refile-active-region-within-subtree
  9921. (org-toggle-heading)))
  9922. (error "The region is not a (sequence of) subtree(s)")))
  9923. (if (equal goto '(16))
  9924. (org-refile-goto-last-stored)
  9925. (when (or
  9926. (and (equal goto 2)
  9927. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9928. (prog1
  9929. (setq it (list (or org-clock-heading "running clock")
  9930. (buffer-file-name
  9931. (marker-buffer org-clock-hd-marker))
  9932. ""
  9933. (marker-position org-clock-hd-marker)))
  9934. (setq goto nil)))
  9935. (setq it (or rfloc
  9936. (let (heading-text)
  9937. (save-excursion
  9938. (unless goto
  9939. (org-back-to-heading t)
  9940. (setq heading-text
  9941. (nth 4 (org-heading-components))))
  9942. (org-refile-get-location
  9943. (cond (goto "Goto")
  9944. (regionp (concat actionmsg " region to"))
  9945. (t (concat actionmsg " subtree \""
  9946. heading-text "\" to")))
  9947. default-buffer
  9948. (and (not (equal '(4) goto))
  9949. org-refile-allow-creating-parent-nodes)
  9950. goto))))))
  9951. (setq file (nth 1 it)
  9952. re (nth 2 it)
  9953. pos (nth 3 it))
  9954. (if (and (not goto)
  9955. pos
  9956. (equal (buffer-file-name) file)
  9957. (if regionp
  9958. (and (>= pos region-start)
  9959. (<= pos region-end))
  9960. (and (>= pos (point))
  9961. (< pos (save-excursion
  9962. (org-end-of-subtree t t))))))
  9963. (error "Cannot refile to position inside the tree or region"))
  9964. (setq nbuf (or (find-buffer-visiting file)
  9965. (find-file-noselect file)))
  9966. (if goto
  9967. (progn
  9968. (org-pop-to-buffer-same-window nbuf)
  9969. (goto-char pos)
  9970. (org-show-context 'org-goto))
  9971. (if regionp
  9972. (progn
  9973. (org-kill-new (buffer-substring region-start region-end))
  9974. (org-save-markers-in-region region-start region-end))
  9975. (org-copy-subtree 1 nil t))
  9976. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9977. (find-file-noselect file)))
  9978. (setq reversed (org-notes-order-reversed-p))
  9979. (save-excursion
  9980. (save-restriction
  9981. (widen)
  9982. (if pos
  9983. (progn
  9984. (goto-char pos)
  9985. (looking-at org-outline-regexp)
  9986. (setq level (org-get-valid-level (funcall outline-level) 1))
  9987. (goto-char
  9988. (if reversed
  9989. (or (outline-next-heading) (point-max))
  9990. (or (save-excursion (org-get-next-sibling))
  9991. (org-end-of-subtree t t)
  9992. (point-max)))))
  9993. (setq level 1)
  9994. (if (not reversed)
  9995. (goto-char (point-max))
  9996. (goto-char (point-min))
  9997. (or (outline-next-heading) (goto-char (point-max)))))
  9998. (if (not (bolp)) (newline))
  9999. (org-paste-subtree level)
  10000. (when org-log-refile
  10001. (org-add-log-setup 'refile nil nil 'findpos
  10002. org-log-refile)
  10003. (unless (eq org-log-refile 'note)
  10004. (save-excursion (org-add-log-note))))
  10005. (and org-auto-align-tags
  10006. (let ((org-loop-over-headlines-in-active-region nil))
  10007. (org-set-tags nil t)))
  10008. (bookmark-set "org-refile-last-stored")
  10009. ;; If we are refiling for capture, make sure that the
  10010. ;; last-capture pointers point here
  10011. (when (org-bound-and-true-p org-refile-for-capture)
  10012. (bookmark-set "org-capture-last-stored-marker")
  10013. (move-marker org-capture-last-stored-marker (point)))
  10014. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10015. (run-hooks 'org-after-refile-insert-hook))))
  10016. (unless org-refile-keep
  10017. (if regionp
  10018. (delete-region (point) (+ (point) region-length))
  10019. (org-cut-subtree)))
  10020. (when (featurep 'org-inlinetask)
  10021. (org-inlinetask-remove-END-maybe))
  10022. (setq org-markers-to-move nil)
  10023. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10024. (defun org-refile-goto-last-stored ()
  10025. "Go to the location where the last refile was stored."
  10026. (interactive)
  10027. (bookmark-jump "org-refile-last-stored")
  10028. (message "This is the location of the last refile"))
  10029. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10030. no-exclude)
  10031. "Prompt the user for a refile location, using PROMPT.
  10032. PROMPT should not be suffixed with a colon and a space, because
  10033. this function appends the default value from
  10034. `org-refile-history' automatically, if that is not empty.
  10035. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10036. this is used for the GOTO interface."
  10037. (let ((org-refile-targets org-refile-targets)
  10038. (org-refile-use-outline-path org-refile-use-outline-path)
  10039. excluded-entries)
  10040. (when (and (derived-mode-p 'org-mode)
  10041. (not org-refile-use-cache)
  10042. (not no-exclude))
  10043. (org-map-tree
  10044. (lambda()
  10045. (setq excluded-entries
  10046. (append excluded-entries (list (org-get-heading t t)))))))
  10047. (setq org-refile-target-table
  10048. (org-refile-get-targets default-buffer excluded-entries)))
  10049. (unless org-refile-target-table
  10050. (error "No refile targets"))
  10051. (let* ((cbuf (current-buffer))
  10052. (partial-completion-mode nil)
  10053. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10054. (cfunc (if (and org-refile-use-outline-path
  10055. org-outline-path-complete-in-steps)
  10056. 'org-olpath-completing-read
  10057. 'org-icompleting-read))
  10058. (extra (if org-refile-use-outline-path "/" ""))
  10059. (cbnex (concat (buffer-name) extra))
  10060. (filename (and cfn (expand-file-name cfn)))
  10061. (tbl (mapcar
  10062. (lambda (x)
  10063. (if (and (not (member org-refile-use-outline-path
  10064. '(file full-file-path)))
  10065. (not (equal filename (nth 1 x))))
  10066. (cons (concat (car x) extra " ("
  10067. (file-name-nondirectory (nth 1 x)) ")")
  10068. (cdr x))
  10069. (cons (concat (car x) extra) (cdr x))))
  10070. org-refile-target-table))
  10071. (completion-ignore-case t)
  10072. cdef
  10073. (prompt (concat prompt
  10074. (or (and (car org-refile-history)
  10075. (concat " (default " (car org-refile-history) ")"))
  10076. (and (assoc cbnex tbl) (setq cdef cbnex)
  10077. (concat " (default " cbnex ")"))) ": "))
  10078. pa answ parent-target child parent old-hist)
  10079. (setq old-hist org-refile-history)
  10080. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10081. nil 'org-refile-history (or cdef (car org-refile-history))))
  10082. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10083. (org-refile-check-position pa)
  10084. (if pa
  10085. (progn
  10086. (when (or (not org-refile-history)
  10087. (not (eq old-hist org-refile-history))
  10088. (not (equal (car pa) (car org-refile-history))))
  10089. (setq org-refile-history
  10090. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10091. org-refile-history
  10092. (cdr org-refile-history))))
  10093. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10094. (pop org-refile-history)))
  10095. pa)
  10096. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10097. (progn
  10098. (setq parent (match-string 1 answ)
  10099. child (match-string 2 answ))
  10100. (setq parent-target (or (assoc parent tbl)
  10101. (assoc (concat parent "/") tbl)))
  10102. (when (and parent-target
  10103. (or (eq new-nodes t)
  10104. (and (eq new-nodes 'confirm)
  10105. (y-or-n-p (format "Create new node \"%s\"? "
  10106. child)))))
  10107. (org-refile-new-child parent-target child)))
  10108. (error "Invalid target location")))))
  10109. (declare-function org-string-nw-p "org-macs.el" (s))
  10110. (defun org-refile-check-position (refile-pointer)
  10111. "Check if the refile pointer matches the readline to which it points."
  10112. (let* ((file (nth 1 refile-pointer))
  10113. (re (nth 2 refile-pointer))
  10114. (pos (nth 3 refile-pointer))
  10115. buffer)
  10116. (when (org-string-nw-p re)
  10117. (setq buffer (if (markerp pos)
  10118. (marker-buffer pos)
  10119. (or (find-buffer-visiting file)
  10120. (find-file-noselect file))))
  10121. (with-current-buffer buffer
  10122. (save-excursion
  10123. (save-restriction
  10124. (widen)
  10125. (goto-char pos)
  10126. (beginning-of-line 1)
  10127. (unless (org-looking-at-p re)
  10128. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  10129. (defun org-refile-new-child (parent-target child)
  10130. "Use refile target PARENT-TARGET to add new CHILD below it."
  10131. (unless parent-target
  10132. (error "Cannot find parent for new node"))
  10133. (let ((file (nth 1 parent-target))
  10134. (pos (nth 3 parent-target))
  10135. level)
  10136. (with-current-buffer (or (find-buffer-visiting file)
  10137. (find-file-noselect file))
  10138. (save-excursion
  10139. (save-restriction
  10140. (widen)
  10141. (if pos
  10142. (goto-char pos)
  10143. (goto-char (point-max))
  10144. (if (not (bolp)) (newline)))
  10145. (when (looking-at org-outline-regexp)
  10146. (setq level (funcall outline-level))
  10147. (org-end-of-subtree t t))
  10148. (org-back-over-empty-lines)
  10149. (insert "\n" (make-string
  10150. (if pos (org-get-valid-level level 1) 1) ?*)
  10151. " " child "\n")
  10152. (beginning-of-line 0)
  10153. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10154. (defun org-olpath-completing-read (prompt collection &rest args)
  10155. "Read an outline path like a file name."
  10156. (let ((thetable collection)
  10157. (org-completion-use-ido nil) ; does not work with ido.
  10158. (org-completion-use-iswitchb nil)) ; or iswitchb
  10159. (apply
  10160. 'org-icompleting-read prompt
  10161. (lambda (string predicate &optional flag)
  10162. (let (rtn r f (l (length string)))
  10163. (cond
  10164. ((eq flag nil)
  10165. ;; try completion
  10166. (try-completion string thetable))
  10167. ((eq flag t)
  10168. ;; all-completions
  10169. (setq rtn (all-completions string thetable predicate))
  10170. (mapcar
  10171. (lambda (x)
  10172. (setq r (substring x l))
  10173. (if (string-match " ([^)]*)$" x)
  10174. (setq f (match-string 0 x))
  10175. (setq f ""))
  10176. (if (string-match "/" r)
  10177. (concat string (substring r 0 (match-end 0)) f)
  10178. x))
  10179. rtn))
  10180. ((eq flag 'lambda)
  10181. ;; exact match?
  10182. (assoc string thetable)))))
  10183. args)))
  10184. ;;;; Dynamic blocks
  10185. (defun org-find-dblock (name)
  10186. "Find the first dynamic block with name NAME in the buffer.
  10187. If not found, stay at current position and return nil."
  10188. (let ((case-fold-search t) pos)
  10189. (save-excursion
  10190. (goto-char (point-min))
  10191. (setq pos (and (re-search-forward
  10192. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10193. (match-beginning 0))))
  10194. (if pos (goto-char pos))
  10195. pos))
  10196. (defconst org-dblock-start-re
  10197. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10198. "Matches the start line of a dynamic block, with parameters.")
  10199. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10200. "Matches the end of a dynamic block.")
  10201. (defun org-create-dblock (plist)
  10202. "Create a dynamic block section, with parameters taken from PLIST.
  10203. PLIST must contain a :name entry which is used as name of the block."
  10204. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10205. (end-of-line 1)
  10206. (newline))
  10207. (let ((col (current-column))
  10208. (name (plist-get plist :name)))
  10209. (insert "#+BEGIN: " name)
  10210. (while plist
  10211. (if (eq (car plist) :name)
  10212. (setq plist (cddr plist))
  10213. (insert " " (prin1-to-string (pop plist)))))
  10214. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10215. (beginning-of-line -2)))
  10216. (defun org-prepare-dblock ()
  10217. "Prepare dynamic block for refresh.
  10218. This empties the block, puts the cursor at the insert position and returns
  10219. the property list including an extra property :name with the block name."
  10220. (unless (looking-at org-dblock-start-re)
  10221. (error "Not at a dynamic block"))
  10222. (let* ((begdel (1+ (match-end 0)))
  10223. (name (org-no-properties (match-string 1)))
  10224. (params (append (list :name name)
  10225. (read (concat "(" (match-string 3) ")")))))
  10226. (save-excursion
  10227. (beginning-of-line 1)
  10228. (skip-chars-forward " \t")
  10229. (setq params (plist-put params :indentation-column (current-column))))
  10230. (unless (re-search-forward org-dblock-end-re nil t)
  10231. (error "Dynamic block not terminated"))
  10232. (setq params
  10233. (append params
  10234. (list :content (buffer-substring
  10235. begdel (match-beginning 0)))))
  10236. (delete-region begdel (match-beginning 0))
  10237. (goto-char begdel)
  10238. (open-line 1)
  10239. params))
  10240. (defun org-map-dblocks (&optional command)
  10241. "Apply COMMAND to all dynamic blocks in the current buffer.
  10242. If COMMAND is not given, use `org-update-dblock'."
  10243. (let ((cmd (or command 'org-update-dblock)))
  10244. (save-excursion
  10245. (goto-char (point-min))
  10246. (while (re-search-forward org-dblock-start-re nil t)
  10247. (goto-char (match-beginning 0))
  10248. (save-excursion
  10249. (condition-case nil
  10250. (funcall cmd)
  10251. (error (message "Error during update of dynamic block"))))
  10252. (unless (re-search-forward org-dblock-end-re nil t)
  10253. (error "Dynamic block not terminated"))))))
  10254. (defun org-dblock-update (&optional arg)
  10255. "User command for updating dynamic blocks.
  10256. Update the dynamic block at point. With prefix ARG, update all dynamic
  10257. blocks in the buffer."
  10258. (interactive "P")
  10259. (if arg
  10260. (org-update-all-dblocks)
  10261. (or (looking-at org-dblock-start-re)
  10262. (org-beginning-of-dblock))
  10263. (org-update-dblock)))
  10264. (defun org-update-dblock ()
  10265. "Update the dynamic block at point.
  10266. This means to empty the block, parse for parameters and then call
  10267. the correct writing function."
  10268. (interactive)
  10269. (save-window-excursion
  10270. (let* ((pos (point))
  10271. (line (org-current-line))
  10272. (params (org-prepare-dblock))
  10273. (name (plist-get params :name))
  10274. (indent (plist-get params :indentation-column))
  10275. (cmd (intern (concat "org-dblock-write:" name))))
  10276. (message "Updating dynamic block `%s' at line %d..." name line)
  10277. (funcall cmd params)
  10278. (message "Updating dynamic block `%s' at line %d...done" name line)
  10279. (goto-char pos)
  10280. (when (and indent (> indent 0))
  10281. (setq indent (make-string indent ?\ ))
  10282. (save-excursion
  10283. (org-beginning-of-dblock)
  10284. (forward-line 1)
  10285. (while (not (looking-at org-dblock-end-re))
  10286. (insert indent)
  10287. (beginning-of-line 2))
  10288. (when (looking-at org-dblock-end-re)
  10289. (and (looking-at "[ \t]+")
  10290. (replace-match ""))
  10291. (insert indent)))))))
  10292. (defun org-beginning-of-dblock ()
  10293. "Find the beginning of the dynamic block at point.
  10294. Error if there is no such block at point."
  10295. (let ((pos (point))
  10296. beg)
  10297. (end-of-line 1)
  10298. (if (and (re-search-backward org-dblock-start-re nil t)
  10299. (setq beg (match-beginning 0))
  10300. (re-search-forward org-dblock-end-re nil t)
  10301. (> (match-end 0) pos))
  10302. (goto-char beg)
  10303. (goto-char pos)
  10304. (error "Not in a dynamic block"))))
  10305. ;;;###autoload
  10306. (defun org-update-all-dblocks ()
  10307. "Update all dynamic blocks in the buffer.
  10308. This function can be used in a hook."
  10309. (interactive)
  10310. (when (derived-mode-p 'org-mode)
  10311. (org-map-dblocks 'org-update-dblock)))
  10312. ;;;; Completion
  10313. (defconst org-additional-option-like-keywords
  10314. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  10315. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  10316. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  10317. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  10318. "BEGIN:" "END:"
  10319. "ORGTBL" "TBLFM:" "TBLNAME:"
  10320. "BEGIN_EXAMPLE" "END_EXAMPLE"
  10321. "BEGIN_VERBATIM" "END_VERBATIM"
  10322. "BEGIN_QUOTE" "END_QUOTE"
  10323. "BEGIN_VERSE" "END_VERSE"
  10324. "BEGIN_CENTER" "END_CENTER"
  10325. "BEGIN_SRC" "END_SRC"
  10326. "BEGIN_RESULT" "END_RESULT"
  10327. "BEGIN_lstlisting" "END_lstlisting"
  10328. "NAME:" "RESULTS:"
  10329. "HEADER:" "HEADERS:"
  10330. "COLUMNS:" "PROPERTY:"
  10331. "CAPTION:" "LABEL:"
  10332. "SETUPFILE:"
  10333. "INCLUDE:"
  10334. "BIND:"
  10335. "MACRO:"))
  10336. (defconst org-options-keywords
  10337. '("TITLE:" "AUTHOR:" "EMAIL:" "DATE:"
  10338. "DESCRIPTION:" "KEYWORDS:" "LANGUAGE:" "OPTIONS:"
  10339. "EXPORT_SELECT_TAGS:" "EXPORT_EXCLUDE_TAGS:"
  10340. "LINK_UP:" "LINK_HOME:" "LINK:" "TODO:"
  10341. "XSLT:" "MATHJAX:" "CATEGORY:" "SEQ_TODO:" "TYP_TODO:"
  10342. "PRIORITIES:" "DRAWERS:" "STARTUP:" "TAGS:" "STYLE:"
  10343. "FILETAGS:" "ARCHIVE:" "INFOJS_OPT:"))
  10344. (defconst org-additional-option-like-keywords-for-flyspell
  10345. (delete-dups
  10346. (split-string
  10347. (mapconcat (lambda(k)
  10348. (replace-regexp-in-string
  10349. "_\\|:" " "
  10350. (concat k " " (downcase k) " " (upcase k))))
  10351. (append org-options-keywords org-additional-option-like-keywords)
  10352. " ")
  10353. " +" t)))
  10354. (defcustom org-structure-template-alist
  10355. '(
  10356. ("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10357. "<src lang=\"?\">\n\n</src>")
  10358. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10359. "<example>\n?\n</example>")
  10360. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10361. "<quote>\n?\n</quote>")
  10362. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10363. "<verse>\n?\n</verse>")
  10364. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10365. "<center>\n?\n</center>")
  10366. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10367. "<literal style=\"latex\">\n?\n</literal>")
  10368. ("L" "#+LaTeX: "
  10369. "<literal style=\"latex\">?</literal>")
  10370. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10371. "<literal style=\"html\">\n?\n</literal>")
  10372. ("H" "#+HTML: "
  10373. "<literal style=\"html\">?</literal>")
  10374. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10375. ("A" "#+ASCII: ")
  10376. ("i" "#+INDEX: ?"
  10377. "#+INDEX: ?")
  10378. ("I" "#+INCLUDE: %file ?"
  10379. "<include file=%file markup=\"?\">")
  10380. )
  10381. "Structure completion elements.
  10382. This is a list of abbreviation keys and values. The value gets inserted
  10383. if you type `<' followed by the key and then press the completion key,
  10384. usually `M-TAB'. %file will be replaced by a file name after prompting
  10385. for the file using completion. The cursor will be placed at the position
  10386. of the `?` in the template.
  10387. There are two templates for each key, the first uses the original Org syntax,
  10388. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10389. the default when the /org-mtags.el/ module has been loaded. See also the
  10390. variable `org-mtags-prefer-muse-templates'."
  10391. :group 'org-completion
  10392. :type '(repeat
  10393. (string :tag "Key")
  10394. (string :tag "Template")
  10395. (string :tag "Muse Template")))
  10396. (defun org-try-structure-completion ()
  10397. "Try to complete a structure template before point.
  10398. This looks for strings like \"<e\" on an otherwise empty line and
  10399. expands them."
  10400. (let ((l (buffer-substring (point-at-bol) (point)))
  10401. a)
  10402. (when (and (looking-at "[ \t]*$")
  10403. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10404. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10405. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10406. (match-beginning 1)) a)
  10407. t)))
  10408. (defun org-complete-expand-structure-template (start cell)
  10409. "Expand a structure template."
  10410. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10411. (rpl (nth (if musep 2 1) cell))
  10412. (ind ""))
  10413. (delete-region start (point))
  10414. (when (string-match "\\`#\\+" rpl)
  10415. (cond
  10416. ((bolp))
  10417. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10418. (setq ind (buffer-substring (point-at-bol) (point))))
  10419. (t (newline))))
  10420. (setq start (point))
  10421. (if (string-match "%file" rpl)
  10422. (setq rpl (replace-match
  10423. (concat
  10424. "\""
  10425. (save-match-data
  10426. (abbreviate-file-name (read-file-name "Include file: ")))
  10427. "\"")
  10428. t t rpl)))
  10429. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10430. (concat "\n" ind)))
  10431. (insert rpl)
  10432. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10433. ;;;; TODO, DEADLINE, Comments
  10434. (defun org-toggle-comment ()
  10435. "Change the COMMENT state of an entry."
  10436. (interactive)
  10437. (save-excursion
  10438. (org-back-to-heading)
  10439. (let (case-fold-search)
  10440. (cond
  10441. ((looking-at (format org-heading-keyword-regexp-format
  10442. org-comment-string))
  10443. (goto-char (match-end 1))
  10444. (looking-at (concat " +" org-comment-string))
  10445. (replace-match "" t t)
  10446. (when (eolp) (insert " ")))
  10447. ((looking-at org-outline-regexp)
  10448. (goto-char (match-end 0))
  10449. (insert org-comment-string " "))))))
  10450. (defvar org-last-todo-state-is-todo nil
  10451. "This is non-nil when the last TODO state change led to a TODO state.
  10452. If the last change removed the TODO tag or switched to DONE, then
  10453. this is nil.")
  10454. (defvar org-setting-tags nil) ; dynamically skipped
  10455. (defvar org-todo-setup-filter-hook nil
  10456. "Hook for functions that pre-filter todo specs.
  10457. Each function takes a todo spec and returns either nil or the spec
  10458. transformed into canonical form." )
  10459. (defvar org-todo-get-default-hook nil
  10460. "Hook for functions that get a default item for todo.
  10461. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10462. nil or a string to be used for the todo mark." )
  10463. (defvar org-agenda-headline-snapshot-before-repeat)
  10464. (defun org-current-effective-time ()
  10465. "Return current time adjusted for `org-extend-today-until' variable."
  10466. (let* ((ct (org-current-time))
  10467. (dct (decode-time ct))
  10468. (ct1
  10469. (cond
  10470. (org-use-last-clock-out-time-as-effective-time
  10471. (or (org-clock-get-last-clock-out-time) ct))
  10472. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10473. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10474. (t ct))))
  10475. ct1))
  10476. (defun org-todo-yesterday (&optional arg)
  10477. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10478. (interactive "P")
  10479. (if (eq major-mode 'org-agenda-mode)
  10480. (apply 'org-agenda-todo-yesterday arg)
  10481. (let* ((hour (third (decode-time
  10482. (org-current-time))))
  10483. (org-extend-today-until (1+ hour)))
  10484. (org-todo arg))))
  10485. (defun org-todo (&optional arg)
  10486. "Change the TODO state of an item.
  10487. The state of an item is given by a keyword at the start of the heading,
  10488. like
  10489. *** TODO Write paper
  10490. *** DONE Call mom
  10491. The different keywords are specified in the variable `org-todo-keywords'.
  10492. By default the available states are \"TODO\" and \"DONE\".
  10493. So for this example: when the item starts with TODO, it is changed to DONE.
  10494. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10495. DONE are present, add TODO at the beginning of the heading.
  10496. With \\[universal-argument] prefix arg, use completion to determine the new \
  10497. state.
  10498. With numeric prefix arg, switch to that state.
  10499. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10500. keywords (nextset).
  10501. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10502. With a numeric prefix arg of 0, inhibit note taking for the change.
  10503. For calling through lisp, arg is also interpreted in the following way:
  10504. 'none -> empty state
  10505. \"\"(empty string) -> switch to empty state
  10506. 'done -> switch to DONE
  10507. 'nextset -> switch to the next set of keywords
  10508. 'previousset -> switch to the previous set of keywords
  10509. \"WAITING\" -> switch to the specified keyword, but only if it
  10510. really is a member of `org-todo-keywords'."
  10511. (interactive "P")
  10512. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10513. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10514. 'region-start-level 'region))
  10515. org-loop-over-headlines-in-active-region)
  10516. (org-map-entries
  10517. `(org-todo ,arg)
  10518. org-loop-over-headlines-in-active-region
  10519. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10520. (if (equal arg '(16)) (setq arg 'nextset))
  10521. (let ((org-blocker-hook org-blocker-hook)
  10522. (case-fold-search nil))
  10523. (when (equal arg '(64))
  10524. (setq arg nil org-blocker-hook nil))
  10525. (when (and org-blocker-hook
  10526. (or org-inhibit-blocking
  10527. (org-entry-get nil "NOBLOCKING")))
  10528. (setq org-blocker-hook nil))
  10529. (save-excursion
  10530. (catch 'exit
  10531. (org-back-to-heading t)
  10532. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10533. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10534. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10535. (let* ((match-data (match-data))
  10536. (startpos (point-at-bol))
  10537. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10538. (org-log-done org-log-done)
  10539. (org-log-repeat org-log-repeat)
  10540. (org-todo-log-states org-todo-log-states)
  10541. (org-inhibit-logging
  10542. (if (equal arg 0)
  10543. (progn (setq arg nil) 'note) org-inhibit-logging))
  10544. (this (match-string 1))
  10545. (hl-pos (match-beginning 0))
  10546. (head (org-get-todo-sequence-head this))
  10547. (ass (assoc head org-todo-kwd-alist))
  10548. (interpret (nth 1 ass))
  10549. (done-word (nth 3 ass))
  10550. (final-done-word (nth 4 ass))
  10551. (org-last-state (or this ""))
  10552. (completion-ignore-case t)
  10553. (member (member this org-todo-keywords-1))
  10554. (tail (cdr member))
  10555. (org-state (cond
  10556. ((and org-todo-key-trigger
  10557. (or (and (equal arg '(4))
  10558. (eq org-use-fast-todo-selection 'prefix))
  10559. (and (not arg) org-use-fast-todo-selection
  10560. (not (eq org-use-fast-todo-selection
  10561. 'prefix)))))
  10562. ;; Use fast selection
  10563. (org-fast-todo-selection))
  10564. ((and (equal arg '(4))
  10565. (or (not org-use-fast-todo-selection)
  10566. (not org-todo-key-trigger)))
  10567. ;; Read a state with completion
  10568. (org-icompleting-read
  10569. "State: " (mapcar (lambda(x) (list x))
  10570. org-todo-keywords-1)
  10571. nil t))
  10572. ((eq arg 'right)
  10573. (if this
  10574. (if tail (car tail) nil)
  10575. (car org-todo-keywords-1)))
  10576. ((eq arg 'left)
  10577. (if (equal member org-todo-keywords-1)
  10578. nil
  10579. (if this
  10580. (nth (- (length org-todo-keywords-1)
  10581. (length tail) 2)
  10582. org-todo-keywords-1)
  10583. (org-last org-todo-keywords-1))))
  10584. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10585. (setq arg nil))) ; hack to fall back to cycling
  10586. (arg
  10587. ;; user or caller requests a specific state
  10588. (cond
  10589. ((equal arg "") nil)
  10590. ((eq arg 'none) nil)
  10591. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10592. ((eq arg 'nextset)
  10593. (or (car (cdr (member head org-todo-heads)))
  10594. (car org-todo-heads)))
  10595. ((eq arg 'previousset)
  10596. (let ((org-todo-heads (reverse org-todo-heads)))
  10597. (or (car (cdr (member head org-todo-heads)))
  10598. (car org-todo-heads))))
  10599. ((car (member arg org-todo-keywords-1)))
  10600. ((stringp arg)
  10601. (error "State `%s' not valid in this file" arg))
  10602. ((nth (1- (prefix-numeric-value arg))
  10603. org-todo-keywords-1))))
  10604. ((null member) (or head (car org-todo-keywords-1)))
  10605. ((equal this final-done-word) nil) ;; -> make empty
  10606. ((null tail) nil) ;; -> first entry
  10607. ((memq interpret '(type priority))
  10608. (if (eq this-command last-command)
  10609. (car tail)
  10610. (if (> (length tail) 0)
  10611. (or done-word (car org-done-keywords))
  10612. nil)))
  10613. (t
  10614. (car tail))))
  10615. (org-state (or
  10616. (run-hook-with-args-until-success
  10617. 'org-todo-get-default-hook org-state org-last-state)
  10618. org-state))
  10619. (next (if org-state (concat " " org-state " ") " "))
  10620. (change-plist (list :type 'todo-state-change :from this :to org-state
  10621. :position startpos))
  10622. dolog now-done-p)
  10623. (when org-blocker-hook
  10624. (setq org-last-todo-state-is-todo
  10625. (not (member this org-done-keywords)))
  10626. (unless (save-excursion
  10627. (save-match-data
  10628. (org-with-wide-buffer
  10629. (run-hook-with-args-until-failure
  10630. 'org-blocker-hook change-plist))))
  10631. (if (org-called-interactively-p 'interactive)
  10632. (error "TODO state change from %s to %s blocked" this org-state)
  10633. ;; fail silently
  10634. (message "TODO state change from %s to %s blocked" this org-state)
  10635. (throw 'exit nil))))
  10636. (store-match-data match-data)
  10637. (replace-match next t t)
  10638. (unless (pos-visible-in-window-p hl-pos)
  10639. (message "TODO state changed to %s" (org-trim next)))
  10640. (unless head
  10641. (setq head (org-get-todo-sequence-head org-state)
  10642. ass (assoc head org-todo-kwd-alist)
  10643. interpret (nth 1 ass)
  10644. done-word (nth 3 ass)
  10645. final-done-word (nth 4 ass)))
  10646. (when (memq arg '(nextset previousset))
  10647. (message "Keyword-Set %d/%d: %s"
  10648. (- (length org-todo-sets) -1
  10649. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10650. (length org-todo-sets)
  10651. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10652. (setq org-last-todo-state-is-todo
  10653. (not (member org-state org-done-keywords)))
  10654. (setq now-done-p (and (member org-state org-done-keywords)
  10655. (not (member this org-done-keywords))))
  10656. (and logging (org-local-logging logging))
  10657. (when (and (or org-todo-log-states org-log-done)
  10658. (not (eq org-inhibit-logging t))
  10659. (not (memq arg '(nextset previousset))))
  10660. ;; we need to look at recording a time and note
  10661. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10662. (nth 2 (assoc this org-todo-log-states))))
  10663. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10664. (setq dolog 'time))
  10665. (when (and org-state
  10666. (member org-state org-not-done-keywords)
  10667. (not (member this org-not-done-keywords)))
  10668. ;; This is now a todo state and was not one before
  10669. ;; If there was a CLOSED time stamp, get rid of it.
  10670. (org-add-planning-info nil nil 'closed))
  10671. (when (and now-done-p org-log-done)
  10672. ;; It is now done, and it was not done before
  10673. (org-add-planning-info 'closed (org-current-effective-time))
  10674. (if (and (not dolog) (eq 'note org-log-done))
  10675. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10676. (when (and org-state dolog)
  10677. ;; This is a non-nil state, and we need to log it
  10678. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10679. ;; Fixup tag positioning
  10680. (org-todo-trigger-tag-changes org-state)
  10681. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10682. (when org-provide-todo-statistics
  10683. (org-update-parent-todo-statistics))
  10684. (run-hooks 'org-after-todo-state-change-hook)
  10685. (if (and arg (not (member org-state org-done-keywords)))
  10686. (setq head (org-get-todo-sequence-head org-state)))
  10687. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10688. ;; Do we need to trigger a repeat?
  10689. (when now-done-p
  10690. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10691. ;; This is for the agenda, take a snapshot of the headline.
  10692. (save-match-data
  10693. (setq org-agenda-headline-snapshot-before-repeat
  10694. (org-get-heading))))
  10695. (org-auto-repeat-maybe org-state))
  10696. ;; Fixup cursor location if close to the keyword
  10697. (if (and (outline-on-heading-p)
  10698. (not (bolp))
  10699. (save-excursion (beginning-of-line 1)
  10700. (looking-at org-todo-line-regexp))
  10701. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10702. (progn
  10703. (goto-char (or (match-end 2) (match-end 1)))
  10704. (and (looking-at " ") (just-one-space))))
  10705. (when org-trigger-hook
  10706. (save-excursion
  10707. (run-hook-with-args 'org-trigger-hook change-plist)))))))))
  10708. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10709. "Block turning an entry into a TODO, using the hierarchy.
  10710. This checks whether the current task should be blocked from state
  10711. changes. Such blocking occurs when:
  10712. 1. The task has children which are not all in a completed state.
  10713. 2. A task has a parent with the property :ORDERED:, and there
  10714. are siblings prior to the current task with incomplete
  10715. status.
  10716. 3. The parent of the task is blocked because it has siblings that should
  10717. be done first, or is child of a block grandparent TODO entry."
  10718. (if (not org-enforce-todo-dependencies)
  10719. t ; if locally turned off don't block
  10720. (catch 'dont-block
  10721. ;; If this is not a todo state change, or if this entry is already DONE,
  10722. ;; do not block
  10723. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10724. (member (plist-get change-plist :from)
  10725. (cons 'done org-done-keywords))
  10726. (member (plist-get change-plist :to)
  10727. (cons 'todo org-not-done-keywords))
  10728. (not (plist-get change-plist :to)))
  10729. (throw 'dont-block t))
  10730. ;; If this task has children, and any are undone, it's blocked
  10731. (save-excursion
  10732. (org-back-to-heading t)
  10733. (let ((this-level (funcall outline-level)))
  10734. (outline-next-heading)
  10735. (let ((child-level (funcall outline-level)))
  10736. (while (and (not (eobp))
  10737. (> child-level this-level))
  10738. ;; this todo has children, check whether they are all
  10739. ;; completed
  10740. (if (and (not (org-entry-is-done-p))
  10741. (org-entry-is-todo-p))
  10742. (throw 'dont-block nil))
  10743. (outline-next-heading)
  10744. (setq child-level (funcall outline-level))))))
  10745. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10746. ;; any previous siblings are undone, it's blocked
  10747. (save-excursion
  10748. (org-back-to-heading t)
  10749. (let* ((pos (point))
  10750. (parent-pos (and (org-up-heading-safe) (point))))
  10751. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10752. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10753. (forward-line 1)
  10754. (re-search-forward org-not-done-heading-regexp pos t))
  10755. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10756. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10757. (while t
  10758. (goto-char parent-pos)
  10759. (if (not (looking-at org-not-done-heading-regexp))
  10760. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10761. (setq pos (point))
  10762. (setq parent-pos (and (org-up-heading-safe) (point)))
  10763. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10764. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10765. (forward-line 1)
  10766. (re-search-forward org-not-done-heading-regexp pos t))
  10767. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10768. (defcustom org-track-ordered-property-with-tag nil
  10769. "Should the ORDERED property also be shown as a tag?
  10770. The ORDERED property decides if an entry should require subtasks to be
  10771. completed in sequence. Since a property is not very visible, setting
  10772. this option means that toggling the ORDERED property with the command
  10773. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10774. not relevant for the behavior, but it makes things more visible.
  10775. Note that toggling the tag with tags commands will not change the property
  10776. and therefore not influence behavior!
  10777. This can be t, meaning the tag ORDERED should be used, It can also be a
  10778. string to select a different tag for this task."
  10779. :group 'org-todo
  10780. :type '(choice
  10781. (const :tag "No tracking" nil)
  10782. (const :tag "Track with ORDERED tag" t)
  10783. (string :tag "Use other tag")))
  10784. (defun org-toggle-ordered-property ()
  10785. "Toggle the ORDERED property of the current entry.
  10786. For better visibility, you can track the value of this property with a tag.
  10787. See variable `org-track-ordered-property-with-tag'."
  10788. (interactive)
  10789. (let* ((t1 org-track-ordered-property-with-tag)
  10790. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10791. (save-excursion
  10792. (org-back-to-heading)
  10793. (if (org-entry-get nil "ORDERED")
  10794. (progn
  10795. (org-delete-property "ORDERED")
  10796. (and tag (org-toggle-tag tag 'off))
  10797. (message "Subtasks can be completed in arbitrary order"))
  10798. (org-entry-put nil "ORDERED" "t")
  10799. (and tag (org-toggle-tag tag 'on))
  10800. (message "Subtasks must be completed in sequence")))))
  10801. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10802. (defun org-block-todo-from-checkboxes (change-plist)
  10803. "Block turning an entry into a TODO, using checkboxes.
  10804. This checks whether the current task should be blocked from state
  10805. changes because there are unchecked boxes in this entry."
  10806. (if (not org-enforce-todo-checkbox-dependencies)
  10807. t ; if locally turned off don't block
  10808. (catch 'dont-block
  10809. ;; If this is not a todo state change, or if this entry is already DONE,
  10810. ;; do not block
  10811. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10812. (member (plist-get change-plist :from)
  10813. (cons 'done org-done-keywords))
  10814. (member (plist-get change-plist :to)
  10815. (cons 'todo org-not-done-keywords))
  10816. (not (plist-get change-plist :to)))
  10817. (throw 'dont-block t))
  10818. ;; If this task has checkboxes that are not checked, it's blocked
  10819. (save-excursion
  10820. (org-back-to-heading t)
  10821. (let ((beg (point)) end)
  10822. (outline-next-heading)
  10823. (setq end (point))
  10824. (goto-char beg)
  10825. (if (org-list-search-forward
  10826. (concat (org-item-beginning-re)
  10827. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10828. "\\[[- ]\\]")
  10829. end t)
  10830. (progn
  10831. (if (boundp 'org-blocked-by-checkboxes)
  10832. (setq org-blocked-by-checkboxes t))
  10833. (throw 'dont-block nil)))))
  10834. t))) ; do not block
  10835. (defun org-entry-blocked-p ()
  10836. "Is the current entry blocked?"
  10837. (if (org-entry-get nil "NOBLOCKING")
  10838. nil ;; Never block this entry
  10839. (not
  10840. (run-hook-with-args-until-failure
  10841. 'org-blocker-hook
  10842. (list :type 'todo-state-change
  10843. :position (point)
  10844. :from 'todo
  10845. :to 'done)))))
  10846. (defun org-update-statistics-cookies (all)
  10847. "Update the statistics cookie, either from TODO or from checkboxes.
  10848. This should be called with the cursor in a line with a statistics cookie."
  10849. (interactive "P")
  10850. (if all
  10851. (progn
  10852. (org-update-checkbox-count 'all)
  10853. (org-map-entries 'org-update-parent-todo-statistics))
  10854. (if (not (org-at-heading-p))
  10855. (org-update-checkbox-count)
  10856. (let ((pos (move-marker (make-marker) (point)))
  10857. end l1 l2)
  10858. (ignore-errors (org-back-to-heading t))
  10859. (if (not (org-at-heading-p))
  10860. (org-update-checkbox-count)
  10861. (setq l1 (org-outline-level))
  10862. (setq end (save-excursion
  10863. (outline-next-heading)
  10864. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10865. (point)))
  10866. (if (and (save-excursion
  10867. (re-search-forward
  10868. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10869. (not (save-excursion (re-search-forward
  10870. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10871. (org-update-checkbox-count)
  10872. (if (and l2 (> l2 l1))
  10873. (progn
  10874. (goto-char end)
  10875. (org-update-parent-todo-statistics))
  10876. (goto-char pos)
  10877. (beginning-of-line 1)
  10878. (while (re-search-forward
  10879. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10880. (point-at-eol) t)
  10881. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10882. (goto-char pos)
  10883. (move-marker pos nil)))))
  10884. (defvar org-entry-property-inherited-from) ;; defined below
  10885. (defun org-update-parent-todo-statistics ()
  10886. "Update any statistics cookie in the parent of the current headline.
  10887. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10888. the entire subtree and this will travel up the hierarchy and update
  10889. statistics everywhere."
  10890. (let* ((prop (save-excursion (org-up-heading-safe)
  10891. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10892. (recursive (or (not org-hierarchical-todo-statistics)
  10893. (and prop (string-match "\\<recursive\\>" prop))))
  10894. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10895. 0))
  10896. (first t)
  10897. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10898. level ltoggle l1 new ndel
  10899. (cnt-all 0) (cnt-done 0) is-percent kwd
  10900. checkbox-beg ov ovs ove cookie-present)
  10901. (catch 'exit
  10902. (save-excursion
  10903. (beginning-of-line 1)
  10904. (setq ltoggle (funcall outline-level))
  10905. ;; Three situations are to consider:
  10906. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10907. ;; to the top-level ancestor on the headline;
  10908. ;; 2. If parent has "recursive" property, repeat up to the
  10909. ;; headline setting that property, taking inheritance into
  10910. ;; account;
  10911. ;; 3. Else, move up to direct parent and proceed only once.
  10912. (while (and (setq level (org-up-heading-safe))
  10913. (or recursive first)
  10914. (>= (point) lim))
  10915. (setq first nil cookie-present nil)
  10916. (unless (and level
  10917. (not (string-match
  10918. "\\<checkbox\\>"
  10919. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10920. "")))))
  10921. (throw 'exit nil))
  10922. (while (re-search-forward box-re (point-at-eol) t)
  10923. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10924. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10925. (save-match-data
  10926. (unless (outline-next-heading) (throw 'exit nil))
  10927. (while (and (looking-at org-complex-heading-regexp)
  10928. (> (setq l1 (length (match-string 1))) level))
  10929. (setq kwd (and (or recursive (= l1 ltoggle))
  10930. (match-string 2)))
  10931. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10932. (and (listp org-provide-todo-statistics)
  10933. (or (member kwd org-provide-todo-statistics)
  10934. (member kwd org-done-keywords))))
  10935. (setq cnt-all (1+ cnt-all))
  10936. (if (eq org-provide-todo-statistics t)
  10937. (and kwd (setq cnt-all (1+ cnt-all)))))
  10938. (and (member kwd org-done-keywords)
  10939. (setq cnt-done (1+ cnt-done)))
  10940. (outline-next-heading)))
  10941. (setq new
  10942. (if is-percent
  10943. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10944. (format "[%d/%d]" cnt-done cnt-all))
  10945. ndel (- (match-end 0) checkbox-beg))
  10946. ;; handle overlays when updating cookie from column view
  10947. (when (setq ov (car (overlays-at checkbox-beg)))
  10948. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10949. (delete-overlay ov))
  10950. (goto-char checkbox-beg)
  10951. (insert new)
  10952. (delete-region (point) (+ (point) ndel))
  10953. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10954. (when ov (move-overlay ov ovs ove)))
  10955. (when cookie-present
  10956. (run-hook-with-args 'org-after-todo-statistics-hook
  10957. cnt-done (- cnt-all cnt-done))))))
  10958. (run-hooks 'org-todo-statistics-hook)))
  10959. (defvar org-after-todo-statistics-hook nil
  10960. "Hook that is called after a TODO statistics cookie has been updated.
  10961. Each function is called with two arguments: the number of not-done entries
  10962. and the number of done entries.
  10963. For example, the following function, when added to this hook, will switch
  10964. an entry to DONE when all children are done, and back to TODO when new
  10965. entries are set to a TODO status. Note that this hook is only called
  10966. when there is a statistics cookie in the headline!
  10967. (defun org-summary-todo (n-done n-not-done)
  10968. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10969. (let (org-log-done org-log-states) ; turn off logging
  10970. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10971. ")
  10972. (defvar org-todo-statistics-hook nil
  10973. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10974. This hook runs even if there is no statistics cookie present, in which case
  10975. `org-after-todo-statistics-hook' would not run.")
  10976. (defun org-todo-trigger-tag-changes (state)
  10977. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10978. (let ((l org-todo-state-tags-triggers)
  10979. changes)
  10980. (when (or (not state) (equal state ""))
  10981. (setq changes (append changes (cdr (assoc "" l)))))
  10982. (when (and (stringp state) (> (length state) 0))
  10983. (setq changes (append changes (cdr (assoc state l)))))
  10984. (when (member state org-not-done-keywords)
  10985. (setq changes (append changes (cdr (assoc 'todo l)))))
  10986. (when (member state org-done-keywords)
  10987. (setq changes (append changes (cdr (assoc 'done l)))))
  10988. (dolist (c changes)
  10989. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10990. (defun org-local-logging (value)
  10991. "Get logging settings from a property VALUE."
  10992. (let* (words w a)
  10993. ;; directly set the variables, they are already local.
  10994. (setq org-log-done nil
  10995. org-log-repeat nil
  10996. org-todo-log-states nil)
  10997. (setq words (org-split-string value))
  10998. (while (setq w (pop words))
  10999. (cond
  11000. ((setq a (assoc w org-startup-options))
  11001. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11002. (set (nth 1 a) (nth 2 a))))
  11003. ((setq a (org-extract-log-state-settings w))
  11004. (and (member (car a) org-todo-keywords-1)
  11005. (push a org-todo-log-states)))))))
  11006. (defun org-get-todo-sequence-head (kwd)
  11007. "Return the head of the TODO sequence to which KWD belongs.
  11008. If KWD is not set, check if there is a text property remembering the
  11009. right sequence."
  11010. (let (p)
  11011. (cond
  11012. ((not kwd)
  11013. (or (get-text-property (point-at-bol) 'org-todo-head)
  11014. (progn
  11015. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11016. nil (point-at-eol)))
  11017. (get-text-property p 'org-todo-head))))
  11018. ((not (member kwd org-todo-keywords-1))
  11019. (car org-todo-keywords-1))
  11020. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11021. (defun org-fast-todo-selection ()
  11022. "Fast TODO keyword selection with single keys.
  11023. Returns the new TODO keyword, or nil if no state change should occur."
  11024. (let* ((fulltable org-todo-key-alist)
  11025. (done-keywords org-done-keywords) ;; needed for the faces.
  11026. (maxlen (apply 'max (mapcar
  11027. (lambda (x)
  11028. (if (stringp (car x)) (string-width (car x)) 0))
  11029. fulltable)))
  11030. (expert nil)
  11031. (fwidth (+ maxlen 3 1 3))
  11032. (ncol (/ (- (window-width) 4) fwidth))
  11033. tg cnt e c tbl
  11034. groups ingroup)
  11035. (save-excursion
  11036. (save-window-excursion
  11037. (if expert
  11038. (set-buffer (get-buffer-create " *Org todo*"))
  11039. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11040. (erase-buffer)
  11041. (org-set-local 'org-done-keywords done-keywords)
  11042. (setq tbl fulltable cnt 0)
  11043. (while (setq e (pop tbl))
  11044. (cond
  11045. ((equal e '(:startgroup))
  11046. (push '() groups) (setq ingroup t)
  11047. (when (not (= cnt 0))
  11048. (setq cnt 0)
  11049. (insert "\n"))
  11050. (insert "{ "))
  11051. ((equal e '(:endgroup))
  11052. (setq ingroup nil cnt 0)
  11053. (insert "}\n"))
  11054. ((equal e '(:newline))
  11055. (when (not (= cnt 0))
  11056. (setq cnt 0)
  11057. (insert "\n")
  11058. (setq e (car tbl))
  11059. (while (equal (car tbl) '(:newline))
  11060. (insert "\n")
  11061. (setq tbl (cdr tbl)))))
  11062. (t
  11063. (setq tg (car e) c (cdr e))
  11064. (if ingroup (push tg (car groups)))
  11065. (setq tg (org-add-props tg nil 'face
  11066. (org-get-todo-face tg)))
  11067. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11068. (insert "[" c "] " tg (make-string
  11069. (- fwidth 4 (length tg)) ?\ ))
  11070. (when (= (setq cnt (1+ cnt)) ncol)
  11071. (insert "\n")
  11072. (if ingroup (insert " "))
  11073. (setq cnt 0)))))
  11074. (insert "\n")
  11075. (goto-char (point-min))
  11076. (if (not expert) (org-fit-window-to-buffer))
  11077. (message "[a-z..]:Set [SPC]:clear")
  11078. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11079. (cond
  11080. ((or (= c ?\C-g)
  11081. (and (= c ?q) (not (rassoc c fulltable))))
  11082. (setq quit-flag t))
  11083. ((= c ?\ ) nil)
  11084. ((setq e (rassoc c fulltable) tg (car e))
  11085. tg)
  11086. (t (setq quit-flag t)))))))
  11087. (defun org-entry-is-todo-p ()
  11088. (member (org-get-todo-state) org-not-done-keywords))
  11089. (defun org-entry-is-done-p ()
  11090. (member (org-get-todo-state) org-done-keywords))
  11091. (defun org-get-todo-state ()
  11092. (save-excursion
  11093. (org-back-to-heading t)
  11094. (and (looking-at org-todo-line-regexp)
  11095. (match-end 2)
  11096. (match-string 2))))
  11097. (defun org-at-date-range-p (&optional inactive-ok)
  11098. "Is the cursor inside a date range?"
  11099. (interactive)
  11100. (save-excursion
  11101. (catch 'exit
  11102. (let ((pos (point)))
  11103. (skip-chars-backward "^[<\r\n")
  11104. (skip-chars-backward "<[")
  11105. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11106. (>= (match-end 0) pos)
  11107. (throw 'exit t))
  11108. (skip-chars-backward "^<[\r\n")
  11109. (skip-chars-backward "<[")
  11110. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11111. (>= (match-end 0) pos)
  11112. (throw 'exit t)))
  11113. nil)))
  11114. (defun org-get-repeat (&optional tagline)
  11115. "Check if there is a deadline/schedule with repeater in this entry."
  11116. (save-match-data
  11117. (save-excursion
  11118. (org-back-to-heading t)
  11119. (and (re-search-forward (if tagline
  11120. (concat tagline "\\s-*" org-repeat-re)
  11121. org-repeat-re)
  11122. (org-entry-end-position) t)
  11123. (match-string-no-properties 1)))))
  11124. (defvar org-last-changed-timestamp)
  11125. (defvar org-last-inserted-timestamp)
  11126. (defvar org-log-post-message)
  11127. (defvar org-log-note-purpose)
  11128. (defvar org-log-note-how)
  11129. (defvar org-log-note-extra)
  11130. (defun org-auto-repeat-maybe (done-word)
  11131. "Check if the current headline contains a repeated deadline/schedule.
  11132. If yes, set TODO state back to what it was and change the base date
  11133. of repeating deadline/scheduled time stamps to new date.
  11134. This function is run automatically after each state change to a DONE state."
  11135. ;; last-state is dynamically scoped into this function
  11136. (let* ((repeat (org-get-repeat))
  11137. (aa (assoc org-last-state org-todo-kwd-alist))
  11138. (interpret (nth 1 aa))
  11139. (head (nth 2 aa))
  11140. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11141. (msg "Entry repeats: ")
  11142. (org-log-done nil)
  11143. (org-todo-log-states nil)
  11144. re type n what ts time to-state)
  11145. (when repeat
  11146. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11147. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11148. org-todo-repeat-to-state))
  11149. (unless (and to-state (member to-state org-todo-keywords-1))
  11150. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11151. (org-todo to-state)
  11152. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11153. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11154. (org-time-stamp-format t t))))
  11155. (when org-log-repeat
  11156. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11157. (memq 'org-add-log-note post-command-hook))
  11158. ;; OK, we are already setup for some record
  11159. (if (eq org-log-repeat 'note)
  11160. ;; make sure we take a note, not only a time stamp
  11161. (setq org-log-note-how 'note))
  11162. ;; Set up for taking a record
  11163. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11164. org-last-state
  11165. 'findpos org-log-repeat)))
  11166. (org-back-to-heading t)
  11167. (org-add-planning-info nil nil 'closed)
  11168. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11169. org-deadline-time-regexp "\\)\\|\\("
  11170. org-ts-regexp "\\)"))
  11171. (while (re-search-forward
  11172. re (save-excursion (outline-next-heading) (point)) t)
  11173. (setq type (if (match-end 1) org-scheduled-string
  11174. (if (match-end 3) org-deadline-string "Plain:"))
  11175. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11176. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11177. (setq n (string-to-number (match-string 2 ts))
  11178. what (match-string 3 ts))
  11179. (if (equal what "w") (setq n (* n 7) what "d"))
  11180. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11181. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11182. ;; Preparation, see if we need to modify the start date for the change
  11183. (when (match-end 1)
  11184. (setq time (save-match-data (org-time-string-to-time ts)))
  11185. (cond
  11186. ((equal (match-string 1 ts) ".")
  11187. ;; Shift starting date to today
  11188. (org-timestamp-change
  11189. (- (org-today) (time-to-days time))
  11190. 'day))
  11191. ((equal (match-string 1 ts) "+")
  11192. (let ((nshiftmax 10) (nshift 0))
  11193. (while (or (= nshift 0)
  11194. (<= (time-to-days time)
  11195. (time-to-days (current-time))))
  11196. (when (= (incf nshift) nshiftmax)
  11197. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11198. (error "Abort")))
  11199. (org-timestamp-change n (cdr (assoc what whata)))
  11200. (org-at-timestamp-p t)
  11201. (setq ts (match-string 1))
  11202. (setq time (save-match-data (org-time-string-to-time ts)))))
  11203. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11204. ;; rematch, so that we have everything in place for the real shift
  11205. (org-at-timestamp-p t)
  11206. (setq ts (match-string 1))
  11207. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11208. (org-timestamp-change n (cdr (assoc what whata)))
  11209. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11210. (setq org-log-post-message msg)
  11211. (message "%s" msg))))
  11212. (defun org-show-todo-tree (arg)
  11213. "Make a compact tree which shows all headlines marked with TODO.
  11214. The tree will show the lines where the regexp matches, and all higher
  11215. headlines above the match.
  11216. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11217. With a numeric prefix N, construct a sparse tree for the Nth element
  11218. of `org-todo-keywords-1'."
  11219. (interactive "P")
  11220. (let ((case-fold-search nil)
  11221. (kwd-re
  11222. (cond ((null arg) org-not-done-regexp)
  11223. ((equal arg '(4))
  11224. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11225. (mapcar 'list org-todo-keywords-1))))
  11226. (concat "\\("
  11227. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11228. "\\)\\>")))
  11229. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11230. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11231. org-todo-keywords-1)))
  11232. (t (error "Invalid prefix argument: %s" arg)))))
  11233. (message "%d TODO entries found"
  11234. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11235. (defun org-deadline (&optional remove time)
  11236. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11237. With argument REMOVE, remove any deadline from the item.
  11238. With argument TIME, set the deadline at the corresponding date. TIME
  11239. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11240. (interactive "P")
  11241. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11242. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11243. 'region-start-level 'region))
  11244. org-loop-over-headlines-in-active-region)
  11245. (org-map-entries
  11246. `(org-deadline ',remove ,time)
  11247. org-loop-over-headlines-in-active-region
  11248. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11249. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11250. (repeater (and old-date
  11251. (string-match
  11252. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11253. old-date)
  11254. (match-string 1 old-date))))
  11255. (if remove
  11256. (progn
  11257. (when (and old-date org-log-redeadline)
  11258. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11259. org-log-redeadline))
  11260. (org-remove-timestamp-with-keyword org-deadline-string)
  11261. (message "Item no longer has a deadline."))
  11262. (org-add-planning-info 'deadline time 'closed)
  11263. (when (and old-date org-log-redeadline
  11264. (not (equal old-date
  11265. (substring org-last-inserted-timestamp 1 -1))))
  11266. (org-add-log-setup 'redeadline nil old-date 'findpos
  11267. org-log-redeadline))
  11268. (when repeater
  11269. (save-excursion
  11270. (org-back-to-heading t)
  11271. (when (re-search-forward (concat org-deadline-string " "
  11272. org-last-inserted-timestamp)
  11273. (save-excursion
  11274. (outline-next-heading) (point)) t)
  11275. (goto-char (1- (match-end 0)))
  11276. (insert " " repeater)
  11277. (setq org-last-inserted-timestamp
  11278. (concat (substring org-last-inserted-timestamp 0 -1)
  11279. " " repeater
  11280. (substring org-last-inserted-timestamp -1))))))
  11281. (message "Deadline on %s" org-last-inserted-timestamp)))))
  11282. (defun org-schedule (&optional remove time)
  11283. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11284. With argument REMOVE, remove any scheduling date from the item.
  11285. With argument TIME, scheduled at the corresponding date. TIME can
  11286. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11287. (interactive "P")
  11288. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11289. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11290. 'region-start-level 'region))
  11291. org-loop-over-headlines-in-active-region)
  11292. (org-map-entries
  11293. `(org-schedule ',remove ,time)
  11294. org-loop-over-headlines-in-active-region
  11295. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11296. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11297. (repeater (and old-date
  11298. (string-match
  11299. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11300. old-date)
  11301. (match-string 1 old-date))))
  11302. (if remove
  11303. (progn
  11304. (when (and old-date org-log-reschedule)
  11305. (org-add-log-setup 'delschedule nil old-date 'findpos
  11306. org-log-reschedule))
  11307. (org-remove-timestamp-with-keyword org-scheduled-string)
  11308. (message "Item is no longer scheduled."))
  11309. (org-add-planning-info 'scheduled time 'closed)
  11310. (when (and old-date org-log-reschedule
  11311. (not (equal old-date
  11312. (substring org-last-inserted-timestamp 1 -1))))
  11313. (org-add-log-setup 'reschedule nil old-date 'findpos
  11314. org-log-reschedule))
  11315. (when repeater
  11316. (save-excursion
  11317. (org-back-to-heading t)
  11318. (when (re-search-forward (concat org-scheduled-string " "
  11319. org-last-inserted-timestamp)
  11320. (save-excursion
  11321. (outline-next-heading) (point)) t)
  11322. (goto-char (1- (match-end 0)))
  11323. (insert " " repeater)
  11324. (setq org-last-inserted-timestamp
  11325. (concat (substring org-last-inserted-timestamp 0 -1)
  11326. " " repeater
  11327. (substring org-last-inserted-timestamp -1))))))
  11328. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  11329. (defun org-get-scheduled-time (pom &optional inherit)
  11330. "Get the scheduled time as a time tuple, of a format suitable
  11331. for calling org-schedule with, or if there is no scheduling,
  11332. returns nil."
  11333. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11334. (when time
  11335. (apply 'encode-time (org-parse-time-string time)))))
  11336. (defun org-get-deadline-time (pom &optional inherit)
  11337. "Get the deadline as a time tuple, of a format suitable for
  11338. calling org-deadline with, or if there is no scheduling, returns
  11339. nil."
  11340. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11341. (when time
  11342. (apply 'encode-time (org-parse-time-string time)))))
  11343. (defun org-remove-timestamp-with-keyword (keyword)
  11344. "Remove all time stamps with KEYWORD in the current entry."
  11345. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11346. beg)
  11347. (save-excursion
  11348. (org-back-to-heading t)
  11349. (setq beg (point))
  11350. (outline-next-heading)
  11351. (while (re-search-backward re beg t)
  11352. (replace-match "")
  11353. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11354. (equal (char-before) ?\ ))
  11355. (backward-delete-char 1)
  11356. (if (string-match "^[ \t]*$" (buffer-substring
  11357. (point-at-bol) (point-at-eol)))
  11358. (delete-region (point-at-bol)
  11359. (min (point-max) (1+ (point-at-eol))))))))))
  11360. (defun org-add-planning-info (what &optional time &rest remove)
  11361. "Insert new timestamp with keyword in the line directly after the headline.
  11362. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11363. If non is given, the user is prompted for a date.
  11364. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11365. be removed."
  11366. (interactive)
  11367. (let (org-time-was-given org-end-time-was-given ts
  11368. end default-time default-input)
  11369. (catch 'exit
  11370. (when (and (memq what '(scheduled deadline))
  11371. (or (not time)
  11372. (and (stringp time)
  11373. (string-match "^[-+]+[0-9]" time))))
  11374. ;; Try to get a default date/time from existing timestamp
  11375. (save-excursion
  11376. (org-back-to-heading t)
  11377. (setq end (save-excursion (outline-next-heading) (point)))
  11378. (when (re-search-forward (if (eq what 'scheduled)
  11379. org-scheduled-time-regexp
  11380. org-deadline-time-regexp)
  11381. end t)
  11382. (setq ts (match-string 1)
  11383. default-time
  11384. (apply 'encode-time (org-parse-time-string ts))
  11385. default-input (and ts (org-get-compact-tod ts))))))
  11386. (when what
  11387. (setq time
  11388. (if (stringp time)
  11389. ;; This is a string (relative or absolute), set proper date
  11390. (apply 'encode-time
  11391. (org-read-date-analyze
  11392. time default-time (decode-time default-time)))
  11393. ;; If necessary, get the time from the user
  11394. (or time (org-read-date nil 'to-time nil nil
  11395. default-time default-input)))))
  11396. (when (and org-insert-labeled-timestamps-at-point
  11397. (member what '(scheduled deadline)))
  11398. (insert
  11399. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11400. (org-insert-time-stamp time org-time-was-given
  11401. nil nil nil (list org-end-time-was-given))
  11402. (setq what nil))
  11403. (save-excursion
  11404. (save-restriction
  11405. (let (col list elt ts buffer-invisibility-spec)
  11406. (org-back-to-heading t)
  11407. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11408. (goto-char (match-end 1))
  11409. (setq col (current-column))
  11410. (goto-char (match-end 0))
  11411. (if (eobp) (insert "\n") (forward-char 1))
  11412. (when (and (not what)
  11413. (not (looking-at
  11414. (concat "[ \t]*"
  11415. org-keyword-time-not-clock-regexp))))
  11416. ;; Nothing to add, nothing to remove...... :-)
  11417. (throw 'exit nil))
  11418. (if (and (not (looking-at org-outline-regexp))
  11419. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11420. "[^\r\n]*"))
  11421. (not (equal (match-string 1) org-clock-string)))
  11422. (narrow-to-region (match-beginning 0) (match-end 0))
  11423. (insert-before-markers "\n")
  11424. (backward-char 1)
  11425. (narrow-to-region (point) (point))
  11426. (and org-adapt-indentation (org-indent-to-column col)))
  11427. ;; Check if we have to remove something.
  11428. (setq list (cons what remove))
  11429. (while list
  11430. (setq elt (pop list))
  11431. (when (or (and (eq elt 'scheduled)
  11432. (re-search-forward org-scheduled-time-regexp nil t))
  11433. (and (eq elt 'deadline)
  11434. (re-search-forward org-deadline-time-regexp nil t))
  11435. (and (eq elt 'closed)
  11436. (re-search-forward org-closed-time-regexp nil t)))
  11437. (replace-match "")
  11438. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11439. (and (looking-at "[ \t]+") (replace-match ""))
  11440. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11441. (when what
  11442. (insert
  11443. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11444. (cond ((eq what 'scheduled) org-scheduled-string)
  11445. ((eq what 'deadline) org-deadline-string)
  11446. ((eq what 'closed) org-closed-string))
  11447. " ")
  11448. (setq ts (org-insert-time-stamp
  11449. time
  11450. (or org-time-was-given
  11451. (and (eq what 'closed) org-log-done-with-time))
  11452. (eq what 'closed)
  11453. nil nil (list org-end-time-was-given)))
  11454. (insert
  11455. (if (not (or (bolp) (eq (char-before) ?\ )
  11456. (memq (char-after) '(32 10))
  11457. (eobp))) " " ""))
  11458. (end-of-line 1))
  11459. (goto-char (point-min))
  11460. (widen)
  11461. (if (and (looking-at "[ \t]*\n")
  11462. (equal (char-before) ?\n))
  11463. (delete-region (1- (point)) (point-at-eol)))
  11464. ts))))))
  11465. (defvar org-log-note-marker (make-marker))
  11466. (defvar org-log-note-purpose nil)
  11467. (defvar org-log-note-state nil)
  11468. (defvar org-log-note-previous-state nil)
  11469. (defvar org-log-note-how nil)
  11470. (defvar org-log-note-extra nil)
  11471. (defvar org-log-note-window-configuration nil)
  11472. (defvar org-log-note-return-to (make-marker))
  11473. (defvar org-log-note-effective-time nil
  11474. "Remembered current time so that dynamically scoped
  11475. `org-extend-today-until' affects tha timestamps in state change
  11476. log")
  11477. (defvar org-log-post-message nil
  11478. "Message to be displayed after a log note has been stored.
  11479. The auto-repeater uses this.")
  11480. (defun org-add-note ()
  11481. "Add a note to the current entry.
  11482. This is done in the same way as adding a state change note."
  11483. (interactive)
  11484. (org-add-log-setup 'note nil nil 'findpos nil))
  11485. (defvar org-property-end-re)
  11486. (defun org-add-log-setup (&optional purpose state prev-state
  11487. findpos how extra)
  11488. "Set up the post command hook to take a note.
  11489. If this is about to TODO state change, the new state is expected in STATE.
  11490. When FINDPOS is non-nil, find the correct position for the note in
  11491. the current entry. If not, assume that it can be inserted at point.
  11492. HOW is an indicator what kind of note should be created.
  11493. EXTRA is additional text that will be inserted into the notes buffer."
  11494. (let* ((org-log-into-drawer (org-log-into-drawer))
  11495. (drawer (cond ((stringp org-log-into-drawer)
  11496. org-log-into-drawer)
  11497. (org-log-into-drawer "LOGBOOK"))))
  11498. (save-restriction
  11499. (save-excursion
  11500. (when findpos
  11501. (org-back-to-heading t)
  11502. (narrow-to-region (point) (save-excursion
  11503. (outline-next-heading) (point)))
  11504. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11505. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11506. "[^\r\n]*\\)?"))
  11507. (goto-char (match-end 0))
  11508. (cond
  11509. (drawer
  11510. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11511. nil t)
  11512. (progn
  11513. (goto-char (match-end 0))
  11514. (or org-log-states-order-reversed
  11515. (and (re-search-forward org-property-end-re nil t)
  11516. (goto-char (1- (match-beginning 0))))))
  11517. (insert "\n:" drawer ":\n:END:")
  11518. (beginning-of-line 0)
  11519. (org-indent-line)
  11520. (beginning-of-line 2)
  11521. (org-indent-line)
  11522. (end-of-line 0)))
  11523. ((and org-log-state-notes-insert-after-drawers
  11524. (save-excursion
  11525. (forward-line) (looking-at org-drawer-regexp)))
  11526. (forward-line)
  11527. (while (looking-at org-drawer-regexp)
  11528. (goto-char (match-end 0))
  11529. (re-search-forward org-property-end-re (point-max) t)
  11530. (forward-line))
  11531. (forward-line -1)))
  11532. (unless org-log-states-order-reversed
  11533. (and (= (char-after) ?\n) (forward-char 1))
  11534. (org-skip-over-state-notes)
  11535. (skip-chars-backward " \t\n\r")))
  11536. (move-marker org-log-note-marker (point))
  11537. (setq org-log-note-purpose purpose
  11538. org-log-note-state state
  11539. org-log-note-previous-state prev-state
  11540. org-log-note-how how
  11541. org-log-note-extra extra
  11542. org-log-note-effective-time (org-current-effective-time))
  11543. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11544. (defun org-skip-over-state-notes ()
  11545. "Skip past the list of State notes in an entry."
  11546. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11547. (when (ignore-errors (goto-char (org-in-item-p)))
  11548. (let* ((struct (org-list-struct))
  11549. (prevs (org-list-prevs-alist struct)))
  11550. (while (looking-at "[ \t]*- State")
  11551. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11552. (org-list-get-item-end (point) struct)))))))
  11553. (defun org-add-log-note (&optional purpose)
  11554. "Pop up a window for taking a note, and add this note later at point."
  11555. (remove-hook 'post-command-hook 'org-add-log-note)
  11556. (setq org-log-note-window-configuration (current-window-configuration))
  11557. (delete-other-windows)
  11558. (move-marker org-log-note-return-to (point))
  11559. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11560. (goto-char org-log-note-marker)
  11561. (org-switch-to-buffer-other-window "*Org Note*")
  11562. (erase-buffer)
  11563. (if (memq org-log-note-how '(time state))
  11564. (let (current-prefix-arg) (org-store-log-note))
  11565. (let ((org-inhibit-startup t)) (org-mode))
  11566. (insert (format "# Insert note for %s.
  11567. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11568. (cond
  11569. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11570. ((eq org-log-note-purpose 'done) "closed todo item")
  11571. ((eq org-log-note-purpose 'state)
  11572. (format "state change from \"%s\" to \"%s\""
  11573. (or org-log-note-previous-state "")
  11574. (or org-log-note-state "")))
  11575. ((eq org-log-note-purpose 'reschedule)
  11576. "rescheduling")
  11577. ((eq org-log-note-purpose 'delschedule)
  11578. "no longer scheduled")
  11579. ((eq org-log-note-purpose 'redeadline)
  11580. "changing deadline")
  11581. ((eq org-log-note-purpose 'deldeadline)
  11582. "removing deadline")
  11583. ((eq org-log-note-purpose 'refile)
  11584. "refiling")
  11585. ((eq org-log-note-purpose 'note)
  11586. "this entry")
  11587. (t (error "This should not happen")))))
  11588. (if org-log-note-extra (insert org-log-note-extra))
  11589. (org-set-local 'org-finish-function 'org-store-log-note)
  11590. (run-hooks 'org-log-buffer-setup-hook)))
  11591. (defvar org-note-abort nil) ; dynamically scoped
  11592. (defun org-store-log-note ()
  11593. "Finish taking a log note, and insert it to where it belongs."
  11594. (let ((txt (buffer-string))
  11595. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11596. lines ind bul)
  11597. (kill-buffer (current-buffer))
  11598. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11599. (setq txt (replace-match "" t t txt)))
  11600. (if (string-match "\\s-+\\'" txt)
  11601. (setq txt (replace-match "" t t txt)))
  11602. (setq lines (org-split-string txt "\n"))
  11603. (when (and note (string-match "\\S-" note))
  11604. (setq note
  11605. (org-replace-escapes
  11606. note
  11607. (list (cons "%u" (user-login-name))
  11608. (cons "%U" user-full-name)
  11609. (cons "%t" (format-time-string
  11610. (org-time-stamp-format 'long 'inactive)
  11611. org-log-note-effective-time))
  11612. (cons "%T" (format-time-string
  11613. (org-time-stamp-format 'long nil)
  11614. org-log-note-effective-time))
  11615. (cons "%d" (format-time-string
  11616. (org-time-stamp-format nil 'inactive)
  11617. org-log-note-effective-time))
  11618. (cons "%D" (format-time-string
  11619. (org-time-stamp-format nil nil)
  11620. org-log-note-effective-time))
  11621. (cons "%s" (if org-log-note-state
  11622. (concat "\"" org-log-note-state "\"")
  11623. ""))
  11624. (cons "%S" (if org-log-note-previous-state
  11625. (concat "\"" org-log-note-previous-state "\"")
  11626. "\"\"")))))
  11627. (if lines (setq note (concat note " \\\\")))
  11628. (push note lines))
  11629. (when (or current-prefix-arg org-note-abort)
  11630. (when org-log-into-drawer
  11631. (org-remove-empty-drawer-at
  11632. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11633. org-log-note-marker))
  11634. (setq lines nil))
  11635. (when lines
  11636. (with-current-buffer (marker-buffer org-log-note-marker)
  11637. (save-excursion
  11638. (goto-char org-log-note-marker)
  11639. (move-marker org-log-note-marker nil)
  11640. (end-of-line 1)
  11641. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11642. (setq ind (save-excursion
  11643. (if (ignore-errors (goto-char (org-in-item-p)))
  11644. (let ((struct (org-list-struct)))
  11645. (org-list-get-ind
  11646. (org-list-get-top-point struct) struct))
  11647. (skip-chars-backward " \r\t\n")
  11648. (cond
  11649. ((and (org-at-heading-p)
  11650. org-adapt-indentation)
  11651. (1+ (org-current-level)))
  11652. ((org-at-heading-p) 0)
  11653. (t (org-get-indentation))))))
  11654. (setq bul (org-list-bullet-string "-"))
  11655. (org-indent-line-to ind)
  11656. (insert bul (pop lines))
  11657. (let ((ind-body (+ (length bul) ind)))
  11658. (while lines
  11659. (insert "\n")
  11660. (org-indent-line-to ind-body)
  11661. (insert (pop lines))))
  11662. (message "Note stored")
  11663. (org-back-to-heading t)
  11664. (org-cycle-hide-drawers 'children)))))
  11665. (set-window-configuration org-log-note-window-configuration)
  11666. (with-current-buffer (marker-buffer org-log-note-return-to)
  11667. (goto-char org-log-note-return-to))
  11668. (move-marker org-log-note-return-to nil)
  11669. (and org-log-post-message (message "%s" org-log-post-message)))
  11670. (defun org-remove-empty-drawer-at (drawer pos)
  11671. "Remove an empty drawer DRAWER at position POS.
  11672. POS may also be a marker."
  11673. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11674. (save-excursion
  11675. (save-restriction
  11676. (widen)
  11677. (goto-char pos)
  11678. (if (org-in-regexp
  11679. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11680. (replace-match ""))))))
  11681. (defvar org-ts-type nil)
  11682. (defun org-sparse-tree (&optional arg type)
  11683. "Create a sparse tree, prompt for the details.
  11684. This command can create sparse trees. You first need to select the type
  11685. of match used to create the tree:
  11686. t Show all TODO entries.
  11687. T Show entries with a specific TODO keyword.
  11688. m Show entries selected by a tags/property match.
  11689. p Enter a property name and its value (both with completion on existing
  11690. names/values) and show entries with that property.
  11691. r Show entries matching a regular expression (`/' can be used as well).
  11692. b Show deadlines and scheduled items before a date.
  11693. a Show deadlines and scheduled items after a date.
  11694. d Show deadlines due within `org-deadline-warning-days'.
  11695. D Show deadlines and scheduled items between a date range."
  11696. (interactive "P")
  11697. (let (ans kwd value ts-type)
  11698. (setq type (or type org-sparse-tree-default-date-type))
  11699. (setq org-ts-type type)
  11700. (message "Sparse tree: [r]egexp [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date [D]ates range\n [c]ycle through date types: %s"
  11701. (cond ((eq type 'all) "all timestamps")
  11702. ((eq type 'scheduled) "only scheduled")
  11703. ((eq type 'deadline) "only deadline")
  11704. ((eq type 'active) "only active timestamps")
  11705. ((eq type 'inactive) "only inactive timestamps")
  11706. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  11707. (t "scheduled/deadline")))
  11708. (setq ans (read-char-exclusive))
  11709. (cond
  11710. ((equal ans ?c)
  11711. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  11712. ((equal ans ?d)
  11713. (call-interactively 'org-check-deadlines))
  11714. ((equal ans ?b)
  11715. (call-interactively 'org-check-before-date))
  11716. ((equal ans ?a)
  11717. (call-interactively 'org-check-after-date))
  11718. ((equal ans ?D)
  11719. (call-interactively 'org-check-dates-range))
  11720. ((equal ans ?t)
  11721. (org-show-todo-tree nil))
  11722. ((equal ans ?T)
  11723. (org-show-todo-tree '(4)))
  11724. ((member ans '(?T ?m))
  11725. (call-interactively 'org-match-sparse-tree))
  11726. ((member ans '(?p ?P))
  11727. (setq kwd (org-icompleting-read "Property: "
  11728. (mapcar 'list (org-buffer-property-keys))))
  11729. (setq value (org-icompleting-read "Value: "
  11730. (mapcar 'list (org-property-values kwd))))
  11731. (unless (string-match "\\`{.*}\\'" value)
  11732. (setq value (concat "\"" value "\"")))
  11733. (org-match-sparse-tree arg (concat kwd "=" value)))
  11734. ((member ans '(?r ?R ?/))
  11735. (call-interactively 'org-occur))
  11736. (t (error "No such sparse tree command \"%c\"" ans)))))
  11737. (defvar org-occur-highlights nil
  11738. "List of overlays used for occur matches.")
  11739. (make-variable-buffer-local 'org-occur-highlights)
  11740. (defvar org-occur-parameters nil
  11741. "Parameters of the active org-occur calls.
  11742. This is a list, each call to org-occur pushes as cons cell,
  11743. containing the regular expression and the callback, onto the list.
  11744. The list can contain several entries if `org-occur' has been called
  11745. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11746. will only contain one set of parameters. When the highlights are
  11747. removed (for example with `C-c C-c', or with the next edit (depending
  11748. on `org-remove-highlights-with-change'), this variable is emptied
  11749. as well.")
  11750. (make-variable-buffer-local 'org-occur-parameters)
  11751. (defun org-occur (regexp &optional keep-previous callback)
  11752. "Make a compact tree which shows all matches of REGEXP.
  11753. The tree will show the lines where the regexp matches, and all higher
  11754. headlines above the match. It will also show the heading after the match,
  11755. to make sure editing the matching entry is easy.
  11756. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11757. call to `org-occur' will be kept, to allow stacking of calls to this
  11758. command.
  11759. If CALLBACK is non-nil, it is a function which is called to confirm
  11760. that the match should indeed be shown."
  11761. (interactive "sRegexp: \nP")
  11762. (when (equal regexp "")
  11763. (error "Regexp cannot be empty"))
  11764. (unless keep-previous
  11765. (org-remove-occur-highlights nil nil t))
  11766. (push (cons regexp callback) org-occur-parameters)
  11767. (let ((cnt 0))
  11768. (save-excursion
  11769. (goto-char (point-min))
  11770. (if (or (not keep-previous) ; do not want to keep
  11771. (not org-occur-highlights)) ; no previous matches
  11772. ;; hide everything
  11773. (org-overview))
  11774. (while (re-search-forward regexp nil t)
  11775. (when (or (not callback)
  11776. (save-match-data (funcall callback)))
  11777. (setq cnt (1+ cnt))
  11778. (when org-highlight-sparse-tree-matches
  11779. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11780. (org-show-context 'occur-tree))))
  11781. (when org-remove-highlights-with-change
  11782. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11783. nil 'local))
  11784. (unless org-sparse-tree-open-archived-trees
  11785. (org-hide-archived-subtrees (point-min) (point-max)))
  11786. (run-hooks 'org-occur-hook)
  11787. (if (org-called-interactively-p 'interactive)
  11788. (message "%d match(es) for regexp %s" cnt regexp))
  11789. cnt))
  11790. (defun org-occur-next-match (&optional n reset)
  11791. "Function for `next-error-function' to find sparse tree matches.
  11792. N is the number of matches to move, when negative move backwards.
  11793. RESET is entirely ignored - this function always goes back to the
  11794. starting point when no match is found."
  11795. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11796. (search-func (if (< n 0)
  11797. 'previous-single-char-property-change
  11798. 'next-single-char-property-change))
  11799. (n (abs n))
  11800. (pos (point))
  11801. p1)
  11802. (catch 'exit
  11803. (while (setq p1 (funcall search-func (point) 'org-type))
  11804. (when (equal p1 limit)
  11805. (goto-char pos)
  11806. (error "No more matches"))
  11807. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11808. (setq n (1- n))
  11809. (when (= n 0)
  11810. (goto-char p1)
  11811. (throw 'exit (point))))
  11812. (goto-char p1))
  11813. (goto-char p1)
  11814. (error "No more matches"))))
  11815. (defun org-show-context (&optional key)
  11816. "Make sure point and context are visible.
  11817. How much context is shown depends upon the variables
  11818. `org-show-hierarchy-above', `org-show-following-heading',
  11819. `org-show-entry-below' and `org-show-siblings'."
  11820. (let ((heading-p (org-at-heading-p t))
  11821. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11822. (following-p (org-get-alist-option org-show-following-heading key))
  11823. (entry-p (org-get-alist-option org-show-entry-below key))
  11824. (siblings-p (org-get-alist-option org-show-siblings key)))
  11825. (catch 'exit
  11826. ;; Show heading or entry text
  11827. (if (and heading-p (not entry-p))
  11828. (org-flag-heading nil) ; only show the heading
  11829. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11830. (org-show-hidden-entry))) ; show entire entry
  11831. (when following-p
  11832. ;; Show next sibling, or heading below text
  11833. (save-excursion
  11834. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11835. (org-flag-heading nil))))
  11836. (when siblings-p (org-show-siblings))
  11837. (when hierarchy-p
  11838. ;; show all higher headings, possibly with siblings
  11839. (save-excursion
  11840. (while (and (condition-case nil
  11841. (progn (org-up-heading-all 1) t)
  11842. (error nil))
  11843. (not (bobp)))
  11844. (org-flag-heading nil)
  11845. (when siblings-p (org-show-siblings))))))))
  11846. (defvar org-reveal-start-hook nil
  11847. "Hook run before revealing a location.")
  11848. (defun org-reveal (&optional siblings)
  11849. "Show current entry, hierarchy above it, and the following headline.
  11850. This can be used to show a consistent set of context around locations
  11851. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11852. not t for the search context.
  11853. With optional argument SIBLINGS, on each level of the hierarchy all
  11854. siblings are shown. This repairs the tree structure to what it would
  11855. look like when opened with hierarchical calls to `org-cycle'.
  11856. With double optional argument \\[universal-argument] \\[universal-argument], \
  11857. go to the parent and show the
  11858. entire tree."
  11859. (interactive "P")
  11860. (run-hooks 'org-reveal-start-hook)
  11861. (let ((org-show-hierarchy-above t)
  11862. (org-show-following-heading t)
  11863. (org-show-siblings (if siblings t org-show-siblings)))
  11864. (org-show-context nil))
  11865. (when (equal siblings '(16))
  11866. (save-excursion
  11867. (when (org-up-heading-safe)
  11868. (org-show-subtree)
  11869. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11870. (defun org-highlight-new-match (beg end)
  11871. "Highlight from BEG to END and mark the highlight is an occur headline."
  11872. (let ((ov (make-overlay beg end)))
  11873. (overlay-put ov 'face 'secondary-selection)
  11874. (overlay-put ov 'org-type 'org-occur)
  11875. (push ov org-occur-highlights)))
  11876. (defun org-remove-occur-highlights (&optional beg end noremove)
  11877. "Remove the occur highlights from the buffer.
  11878. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11879. from the `before-change-functions' in the current buffer."
  11880. (interactive)
  11881. (unless org-inhibit-highlight-removal
  11882. (mapc 'delete-overlay org-occur-highlights)
  11883. (setq org-occur-highlights nil)
  11884. (setq org-occur-parameters nil)
  11885. (unless noremove
  11886. (remove-hook 'before-change-functions
  11887. 'org-remove-occur-highlights 'local))))
  11888. ;;;; Priorities
  11889. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11890. "Regular expression matching the priority indicator.")
  11891. (defvar org-remove-priority-next-time nil)
  11892. (defun org-priority-up ()
  11893. "Increase the priority of the current item."
  11894. (interactive)
  11895. (org-priority 'up))
  11896. (defun org-priority-down ()
  11897. "Decrease the priority of the current item."
  11898. (interactive)
  11899. (org-priority 'down))
  11900. (defun org-priority (&optional action show)
  11901. "Change the priority of an item.
  11902. ACTION can be `set', `up', `down', or a character."
  11903. (interactive "P")
  11904. (if (equal action '(4))
  11905. (org-show-priority)
  11906. (unless org-enable-priority-commands
  11907. (error "Priority commands are disabled"))
  11908. (setq action (or action 'set))
  11909. (let (current new news have remove)
  11910. (save-excursion
  11911. (org-back-to-heading t)
  11912. (if (looking-at org-priority-regexp)
  11913. (setq current (string-to-char (match-string 2))
  11914. have t))
  11915. (cond
  11916. ((eq action 'remove)
  11917. (setq remove t new ?\ ))
  11918. ((or (eq action 'set)
  11919. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11920. (if (not (eq action 'set))
  11921. (setq new action)
  11922. (message "Priority %c-%c, SPC to remove: "
  11923. org-highest-priority org-lowest-priority)
  11924. (save-match-data
  11925. (setq new (read-char-exclusive))))
  11926. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11927. (= (upcase org-lowest-priority) org-lowest-priority))
  11928. (setq new (upcase new)))
  11929. (cond ((equal new ?\ ) (setq remove t))
  11930. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11931. (error "Priority must be between `%c' and `%c'"
  11932. org-highest-priority org-lowest-priority))))
  11933. ((eq action 'up)
  11934. (setq new (if have
  11935. (1- current) ; normal cycling
  11936. ;; last priority was empty
  11937. (if (eq last-command this-command)
  11938. org-lowest-priority ; wrap around empty to lowest
  11939. ;; default
  11940. (if org-priority-start-cycle-with-default
  11941. org-default-priority
  11942. (1- org-default-priority))))))
  11943. ((eq action 'down)
  11944. (setq new (if have
  11945. (1+ current) ; normal cycling
  11946. ;; last priority was empty
  11947. (if (eq last-command this-command)
  11948. org-highest-priority ; wrap around empty to highest
  11949. ;; default
  11950. (if org-priority-start-cycle-with-default
  11951. org-default-priority
  11952. (1+ org-default-priority))))))
  11953. (t (error "Invalid action")))
  11954. (if (or (< (upcase new) org-highest-priority)
  11955. (> (upcase new) org-lowest-priority))
  11956. (if (and (memq action '(up down))
  11957. (not have) (not (eq last-command this-command)))
  11958. ;; `new' is from default priority
  11959. (error
  11960. "The default can not be set, see `org-default-priority' why")
  11961. ;; normal cycling: `new' is beyond highest/lowest priority
  11962. ;; and is wrapped around to the empty priority
  11963. (setq remove t)))
  11964. (setq news (format "%c" new))
  11965. (if have
  11966. (if remove
  11967. (replace-match "" t t nil 1)
  11968. (replace-match news t t nil 2))
  11969. (if remove
  11970. (error "No priority cookie found in line")
  11971. (let ((case-fold-search nil))
  11972. (looking-at org-todo-line-regexp))
  11973. (if (match-end 2)
  11974. (progn
  11975. (goto-char (match-end 2))
  11976. (insert " [#" news "]"))
  11977. (goto-char (match-beginning 3))
  11978. (insert "[#" news "] "))))
  11979. (org-preserve-lc (org-set-tags nil 'align)))
  11980. (if remove
  11981. (message "Priority removed")
  11982. (message "Priority of current item set to %s" news)))))
  11983. (defun org-show-priority ()
  11984. "Show the priority of the current item.
  11985. This priority is composed of the main priority given with the [#A] cookies,
  11986. and by additional input from the age of a schedules or deadline entry."
  11987. (interactive)
  11988. (let ((pri (if (eq major-mode 'org-agenda-mode)
  11989. (org-get-at-bol 'priority)
  11990. (save-excursion
  11991. (save-match-data
  11992. (beginning-of-line)
  11993. (and (looking-at org-heading-regexp)
  11994. (org-get-priority (match-string 0))))))))
  11995. (message "Priority is %d" (if pri pri -1000))))
  11996. (defun org-get-priority (s)
  11997. "Find priority cookie and return priority."
  11998. (if (functionp org-get-priority-function)
  11999. (funcall org-get-priority-function)
  12000. (save-match-data
  12001. (if (not (string-match org-priority-regexp s))
  12002. (* 1000 (- org-lowest-priority org-default-priority))
  12003. (* 1000 (- org-lowest-priority
  12004. (string-to-char (match-string 2 s))))))))
  12005. ;;;; Tags
  12006. (defvar org-agenda-archives-mode)
  12007. (defvar org-map-continue-from nil
  12008. "Position from where mapping should continue.
  12009. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12010. (defvar org-scanner-tags nil
  12011. "The current tag list while the tags scanner is running.")
  12012. (defvar org-trust-scanner-tags nil
  12013. "Should `org-get-tags-at' use the tags for the scanner.
  12014. This is for internal dynamical scoping only.
  12015. When this is non-nil, the function `org-get-tags-at' will return the value
  12016. of `org-scanner-tags' instead of building the list by itself. This
  12017. can lead to large speed-ups when the tags scanner is used in a file with
  12018. many entries, and when the list of tags is retrieved, for example to
  12019. obtain a list of properties. Building the tags list for each entry in such
  12020. a file becomes an N^2 operation - but with this variable set, it scales
  12021. as N.")
  12022. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12023. "Scan headline tags with inheritance and produce output ACTION.
  12024. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12025. or `agenda' to produce an entry list for an agenda view. It can also be
  12026. a Lisp form or a function that should be called at each matched headline, in
  12027. this case the return value is a list of all return values from these calls.
  12028. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12029. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12030. only lines with a not-done TODO keyword are included in the output.
  12031. This should be the same variable that was scoped into
  12032. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12033. START-LEVEL can be a string with asterisks, reducing the scope to
  12034. headlines matching this string."
  12035. (require 'org-agenda)
  12036. (let* ((re (concat "^"
  12037. (if start-level
  12038. ;; Get the correct level to match
  12039. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12040. org-outline-regexp)
  12041. " *\\(\\<\\("
  12042. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12043. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12044. (props (list 'face 'default
  12045. 'done-face 'org-agenda-done
  12046. 'undone-face 'default
  12047. 'mouse-face 'highlight
  12048. 'org-not-done-regexp org-not-done-regexp
  12049. 'org-todo-regexp org-todo-regexp
  12050. 'org-complex-heading-regexp org-complex-heading-regexp
  12051. 'help-echo
  12052. (format "mouse-2 or RET jump to org file %s"
  12053. (abbreviate-file-name
  12054. (or (buffer-file-name (buffer-base-buffer))
  12055. (buffer-name (buffer-base-buffer)))))))
  12056. (case-fold-search nil)
  12057. (org-map-continue-from nil)
  12058. lspos tags tags-list
  12059. (tags-alist (list (cons 0 org-file-tags)))
  12060. (llast 0) rtn rtn1 level category i txt
  12061. todo marker entry priority)
  12062. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12063. (setq action (list 'lambda nil action)))
  12064. (save-excursion
  12065. (goto-char (point-min))
  12066. (when (eq action 'sparse-tree)
  12067. (org-overview)
  12068. (org-remove-occur-highlights))
  12069. (while (re-search-forward re nil t)
  12070. (setq org-map-continue-from nil)
  12071. (catch :skip
  12072. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12073. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12074. (goto-char (setq lspos (match-beginning 0)))
  12075. (setq level (org-reduced-level (org-outline-level))
  12076. category (org-get-category))
  12077. (setq i llast llast level)
  12078. ;; remove tag lists from same and sublevels
  12079. (while (>= i level)
  12080. (when (setq entry (assoc i tags-alist))
  12081. (setq tags-alist (delete entry tags-alist)))
  12082. (setq i (1- i)))
  12083. ;; add the next tags
  12084. (when tags
  12085. (setq tags (org-split-string tags ":")
  12086. tags-alist
  12087. (cons (cons level tags) tags-alist)))
  12088. ;; compile tags for current headline
  12089. (setq tags-list
  12090. (if org-use-tag-inheritance
  12091. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12092. tags)
  12093. org-scanner-tags tags-list)
  12094. (when org-use-tag-inheritance
  12095. (setcdr (car tags-alist)
  12096. (mapcar (lambda (x)
  12097. (setq x (copy-sequence x))
  12098. (org-add-prop-inherited x))
  12099. (cdar tags-alist))))
  12100. (when (and tags org-use-tag-inheritance
  12101. (or (not (eq t org-use-tag-inheritance))
  12102. org-tags-exclude-from-inheritance))
  12103. ;; selective inheritance, remove uninherited ones
  12104. (setcdr (car tags-alist)
  12105. (org-remove-uninherited-tags (cdar tags-alist))))
  12106. (when (and
  12107. ;; eval matcher only when the todo condition is OK
  12108. (and (or (not todo-only) (member todo org-not-done-keywords))
  12109. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12110. (eval matcher)))
  12111. ;; Call the skipper, but return t if it does not skip,
  12112. ;; so that the `and' form continues evaluating
  12113. (progn
  12114. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12115. t)
  12116. ;; Check if timestamps are deselecting this entry
  12117. (or (not todo-only)
  12118. (and (member todo org-not-done-keywords)
  12119. (or (not org-agenda-tags-todo-honor-ignore-options)
  12120. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  12121. ;; Extra check for the archive tag
  12122. ;; FIXME: Does the skipper already do this????
  12123. (or
  12124. (not (member org-archive-tag tags-list))
  12125. ;; we have an archive tag, should we use this anyway?
  12126. (or (not org-agenda-skip-archived-trees)
  12127. (and (eq action 'agenda) org-agenda-archives-mode))))
  12128. ;; select this headline
  12129. (cond
  12130. ((eq action 'sparse-tree)
  12131. (and org-highlight-sparse-tree-matches
  12132. (org-get-heading) (match-end 0)
  12133. (org-highlight-new-match
  12134. (match-beginning 1) (match-end 1)))
  12135. (org-show-context 'tags-tree))
  12136. ((eq action 'agenda)
  12137. (setq txt (org-agenda-format-item
  12138. ""
  12139. (concat
  12140. (if (eq org-tags-match-list-sublevels 'indented)
  12141. (make-string (1- level) ?.) "")
  12142. (org-get-heading))
  12143. level category
  12144. tags-list)
  12145. priority (org-get-priority txt))
  12146. (goto-char lspos)
  12147. (setq marker (org-agenda-new-marker))
  12148. (org-add-props txt props
  12149. 'org-marker marker 'org-hd-marker marker 'org-category category
  12150. 'todo-state todo
  12151. 'priority priority 'type "tagsmatch")
  12152. (push txt rtn))
  12153. ((functionp action)
  12154. (setq org-map-continue-from nil)
  12155. (save-excursion
  12156. (setq rtn1 (funcall action))
  12157. (push rtn1 rtn)))
  12158. (t (error "Invalid action")))
  12159. ;; if we are to skip sublevels, jump to end of subtree
  12160. (unless org-tags-match-list-sublevels
  12161. (org-end-of-subtree t)
  12162. (backward-char 1))))
  12163. ;; Get the correct position from where to continue
  12164. (if org-map-continue-from
  12165. (goto-char org-map-continue-from)
  12166. (and (= (point) lspos) (end-of-line 1)))))
  12167. (when (and (eq action 'sparse-tree)
  12168. (not org-sparse-tree-open-archived-trees))
  12169. (org-hide-archived-subtrees (point-min) (point-max)))
  12170. (nreverse rtn)))
  12171. (defun org-remove-uninherited-tags (tags)
  12172. "Remove all tags that are not inherited from the list TAGS."
  12173. (cond
  12174. ((eq org-use-tag-inheritance t)
  12175. (if org-tags-exclude-from-inheritance
  12176. (org-delete-all org-tags-exclude-from-inheritance tags)
  12177. tags))
  12178. ((not org-use-tag-inheritance) nil)
  12179. ((stringp org-use-tag-inheritance)
  12180. (delq nil (mapcar
  12181. (lambda (x)
  12182. (if (and (string-match org-use-tag-inheritance x)
  12183. (not (member x org-tags-exclude-from-inheritance)))
  12184. x nil))
  12185. tags)))
  12186. ((listp org-use-tag-inheritance)
  12187. (delq nil (mapcar
  12188. (lambda (x)
  12189. (if (member x org-use-tag-inheritance) x nil))
  12190. tags)))))
  12191. (defun org-match-sparse-tree (&optional todo-only match)
  12192. "Create a sparse tree according to tags string MATCH.
  12193. MATCH can contain positive and negative selection of tags, like
  12194. \"+WORK+URGENT-WITHBOSS\".
  12195. If optional argument TODO-ONLY is non-nil, only select lines that are
  12196. also TODO lines."
  12197. (interactive "P")
  12198. (org-agenda-prepare-buffers (list (current-buffer)))
  12199. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12200. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12201. (defvar org-cached-props nil)
  12202. (defun org-cached-entry-get (pom property)
  12203. (if (or (eq t org-use-property-inheritance)
  12204. (and (stringp org-use-property-inheritance)
  12205. (string-match org-use-property-inheritance property))
  12206. (and (listp org-use-property-inheritance)
  12207. (member property org-use-property-inheritance)))
  12208. ;; Caching is not possible, check it directly
  12209. (org-entry-get pom property 'inherit)
  12210. ;; Get all properties, so that we can do complicated checks easily
  12211. (cdr (assoc property (or org-cached-props
  12212. (setq org-cached-props
  12213. (org-entry-properties pom)))))))
  12214. (defun org-global-tags-completion-table (&optional files)
  12215. "Return the list of all tags in all agenda buffer/files.
  12216. Optional FILES argument is a list of files which can be used
  12217. instead of the agenda files."
  12218. (save-excursion
  12219. (org-uniquify
  12220. (delq nil
  12221. (apply 'append
  12222. (mapcar
  12223. (lambda (file)
  12224. (set-buffer (find-file-noselect file))
  12225. (append (org-get-buffer-tags)
  12226. (mapcar (lambda (x) (if (stringp (car-safe x))
  12227. (list (car-safe x)) nil))
  12228. org-tag-alist)))
  12229. (if (and files (car files))
  12230. files
  12231. (org-agenda-files))))))))
  12232. (defun org-make-tags-matcher (match)
  12233. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12234. The variable `todo-only' is scoped dynamically into this function.
  12235. It will be set to t if the matcher restricts matching to TODO entries,
  12236. otherwise will not be touched.
  12237. Returns a cons of the selection string MATCH and the constructed
  12238. lisp form implementing the matcher. The matcher is to be evaluated
  12239. at an Org entry, with point on the headline, and returns t if the
  12240. entry matches the selection string MATCH. The returned lisp form
  12241. references two variables with information about the entry, which
  12242. must be bound around the form's evaluation: todo, the TODO keyword
  12243. at the entry (or nil of none); and tags-list, the list of all tags
  12244. at the entry including inherited ones. Additionally, the category
  12245. of the entry (if any) must be specified as the text property
  12246. 'org-category on the headline.
  12247. See also `org-scan-tags'.
  12248. "
  12249. (declare (special todo-only))
  12250. (unless (boundp 'todo-only)
  12251. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12252. (unless match
  12253. ;; Get a new match request, with completion
  12254. (let ((org-last-tags-completion-table
  12255. (org-global-tags-completion-table)))
  12256. (setq match (org-completing-read-no-i
  12257. "Match: " 'org-tags-completion-function nil nil nil
  12258. 'org-tags-history))))
  12259. ;; Parse the string and create a lisp form
  12260. (let ((match0 match)
  12261. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12262. minus tag mm
  12263. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12264. orterms term orlist re-p str-p level-p level-op time-p
  12265. prop-p pn pv po gv rest)
  12266. (if (string-match "/+" match)
  12267. ;; match contains also a todo-matching request
  12268. (progn
  12269. (setq tagsmatch (substring match 0 (match-beginning 0))
  12270. todomatch (substring match (match-end 0)))
  12271. (if (string-match "^!" todomatch)
  12272. (setq todo-only t todomatch (substring todomatch 1)))
  12273. (if (string-match "^\\s-*$" todomatch)
  12274. (setq todomatch nil)))
  12275. ;; only matching tags
  12276. (setq tagsmatch match todomatch nil))
  12277. ;; Make the tags matcher
  12278. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12279. (setq tagsmatcher t)
  12280. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12281. (while (setq term (pop orterms))
  12282. (while (and (equal (substring term -1) "\\") orterms)
  12283. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12284. (while (string-match re term)
  12285. (setq rest (substring term (match-end 0))
  12286. minus (and (match-end 1)
  12287. (equal (match-string 1 term) "-"))
  12288. tag (save-match-data (replace-regexp-in-string
  12289. "\\\\-" "-"
  12290. (match-string 2 term)))
  12291. re-p (equal (string-to-char tag) ?{)
  12292. level-p (match-end 4)
  12293. prop-p (match-end 5)
  12294. mm (cond
  12295. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12296. (level-p
  12297. (setq level-op (org-op-to-function (match-string 3 term)))
  12298. `(,level-op level ,(string-to-number
  12299. (match-string 4 term))))
  12300. (prop-p
  12301. (setq pn (match-string 5 term)
  12302. po (match-string 6 term)
  12303. pv (match-string 7 term)
  12304. re-p (equal (string-to-char pv) ?{)
  12305. str-p (equal (string-to-char pv) ?\")
  12306. time-p (save-match-data
  12307. (string-match "^\"[[<].*[]>]\"$" pv))
  12308. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12309. (if time-p (setq pv (org-matcher-time pv)))
  12310. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12311. (cond
  12312. ((equal pn "CATEGORY")
  12313. (setq gv '(get-text-property (point) 'org-category)))
  12314. ((equal pn "TODO")
  12315. (setq gv 'todo))
  12316. (t
  12317. (setq gv `(org-cached-entry-get nil ,pn))))
  12318. (if re-p
  12319. (if (eq po 'org<>)
  12320. `(not (string-match ,pv (or ,gv "")))
  12321. `(string-match ,pv (or ,gv "")))
  12322. (if str-p
  12323. `(,po (or ,gv "") ,pv)
  12324. `(,po (string-to-number (or ,gv ""))
  12325. ,(string-to-number pv) ))))
  12326. (t `(member ,tag tags-list)))
  12327. mm (if minus (list 'not mm) mm)
  12328. term rest)
  12329. (push mm tagsmatcher))
  12330. (push (if (> (length tagsmatcher) 1)
  12331. (cons 'and tagsmatcher)
  12332. (car tagsmatcher))
  12333. orlist)
  12334. (setq tagsmatcher nil))
  12335. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12336. (setq tagsmatcher
  12337. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12338. ;; Make the todo matcher
  12339. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12340. (setq todomatcher t)
  12341. (setq orterms (org-split-string todomatch "|") orlist nil)
  12342. (while (setq term (pop orterms))
  12343. (while (string-match re term)
  12344. (setq minus (and (match-end 1)
  12345. (equal (match-string 1 term) "-"))
  12346. kwd (match-string 2 term)
  12347. re-p (equal (string-to-char kwd) ?{)
  12348. term (substring term (match-end 0))
  12349. mm (if re-p
  12350. `(string-match ,(substring kwd 1 -1) todo)
  12351. (list 'equal 'todo kwd))
  12352. mm (if minus (list 'not mm) mm))
  12353. (push mm todomatcher))
  12354. (push (if (> (length todomatcher) 1)
  12355. (cons 'and todomatcher)
  12356. (car todomatcher))
  12357. orlist)
  12358. (setq todomatcher nil))
  12359. (setq todomatcher (if (> (length orlist) 1)
  12360. (cons 'or orlist) (car orlist))))
  12361. ;; Return the string and lisp forms of the matcher
  12362. (setq matcher (if todomatcher
  12363. (list 'and tagsmatcher todomatcher)
  12364. tagsmatcher))
  12365. (when todo-only
  12366. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12367. matcher)))
  12368. (cons match0 matcher)))
  12369. (defun org-op-to-function (op &optional stringp)
  12370. "Turn an operator into the appropriate function."
  12371. (setq op
  12372. (cond
  12373. ((equal op "<" ) '(< string< org-time<))
  12374. ((equal op ">" ) '(> org-string> org-time>))
  12375. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12376. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12377. ((member op '("=" "==")) '(= string= org-time=))
  12378. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12379. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12380. (defun org<> (a b) (not (= a b)))
  12381. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12382. (defun org-string>= (a b) (not (string< a b)))
  12383. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12384. (defun org-string<> (a b) (not (string= a b)))
  12385. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12386. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12387. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12388. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12389. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12390. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12391. (defun org-2ft (s)
  12392. "Convert S to a floating point time.
  12393. If S is already a number, just return it. If it is a string, parse
  12394. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12395. (cond
  12396. ((numberp s) s)
  12397. ((stringp s)
  12398. (condition-case nil
  12399. (float-time (apply 'encode-time (org-parse-time-string s)))
  12400. (error 0.)))
  12401. (t 0.)))
  12402. (defun org-time-today ()
  12403. "Time in seconds today at 0:00.
  12404. Returns the float number of seconds since the beginning of the
  12405. epoch to the beginning of today (00:00)."
  12406. (float-time (apply 'encode-time
  12407. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12408. (defun org-matcher-time (s)
  12409. "Interpret a time comparison value."
  12410. (save-match-data
  12411. (cond
  12412. ((string= s "<now>") (float-time))
  12413. ((string= s "<today>") (org-time-today))
  12414. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12415. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12416. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12417. (+ (org-time-today)
  12418. (* (string-to-number (match-string 1 s))
  12419. (cdr (assoc (match-string 2 s)
  12420. '(("d" . 86400.0) ("w" . 604800.0)
  12421. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12422. (t (org-2ft s)))))
  12423. (defun org-match-any-p (re list)
  12424. "Does re match any element of list?"
  12425. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12426. (delq nil list))
  12427. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12428. (defvar org-tags-overlay (make-overlay 1 1))
  12429. (org-detach-overlay org-tags-overlay)
  12430. (defun org-get-local-tags-at (&optional pos)
  12431. "Get a list of tags defined in the current headline."
  12432. (org-get-tags-at pos 'local))
  12433. (defun org-get-local-tags ()
  12434. "Get a list of tags defined in the current headline."
  12435. (org-get-tags-at nil 'local))
  12436. (defun org-get-tags-at (&optional pos local)
  12437. "Get a list of all headline tags applicable at POS.
  12438. POS defaults to point. If tags are inherited, the list contains
  12439. the targets in the same sequence as the headlines appear, i.e.
  12440. the tags of the current headline come last.
  12441. When LOCAL is non-nil, only return tags from the current headline,
  12442. ignore inherited ones."
  12443. (interactive)
  12444. (if (and org-trust-scanner-tags
  12445. (or (not pos) (equal pos (point)))
  12446. (not local))
  12447. org-scanner-tags
  12448. (let (tags ltags lastpos parent)
  12449. (save-excursion
  12450. (save-restriction
  12451. (widen)
  12452. (goto-char (or pos (point)))
  12453. (save-match-data
  12454. (catch 'done
  12455. (condition-case nil
  12456. (progn
  12457. (org-back-to-heading t)
  12458. (while (not (equal lastpos (point)))
  12459. (setq lastpos (point))
  12460. (when (looking-at
  12461. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12462. (setq ltags (org-split-string
  12463. (org-match-string-no-properties 1) ":"))
  12464. (when parent
  12465. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12466. (setq tags (append
  12467. (if parent
  12468. (org-remove-uninherited-tags ltags)
  12469. ltags)
  12470. tags)))
  12471. (or org-use-tag-inheritance (throw 'done t))
  12472. (if local (throw 'done t))
  12473. (or (org-up-heading-safe) (error nil))
  12474. (setq parent t)))
  12475. (error nil)))))
  12476. (if local
  12477. tags
  12478. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12479. (defun org-add-prop-inherited (s)
  12480. (add-text-properties 0 (length s) '(inherited t) s)
  12481. s)
  12482. (defun org-toggle-tag (tag &optional onoff)
  12483. "Toggle the tag TAG for the current line.
  12484. If ONOFF is `on' or `off', don't toggle but set to this state."
  12485. (let (res current)
  12486. (save-excursion
  12487. (org-back-to-heading t)
  12488. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12489. (point-at-eol) t)
  12490. (progn
  12491. (setq current (match-string 1))
  12492. (replace-match ""))
  12493. (setq current ""))
  12494. (setq current (nreverse (org-split-string current ":")))
  12495. (cond
  12496. ((eq onoff 'on)
  12497. (setq res t)
  12498. (or (member tag current) (push tag current)))
  12499. ((eq onoff 'off)
  12500. (or (not (member tag current)) (setq current (delete tag current))))
  12501. (t (if (member tag current)
  12502. (setq current (delete tag current))
  12503. (setq res t)
  12504. (push tag current))))
  12505. (end-of-line 1)
  12506. (if current
  12507. (progn
  12508. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12509. (org-set-tags nil t))
  12510. (delete-horizontal-space))
  12511. (run-hooks 'org-after-tags-change-hook))
  12512. res))
  12513. (defun org-align-tags-here (to-col)
  12514. ;; Assumes that this is a headline
  12515. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12516. (beginning-of-line 1)
  12517. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12518. (< pos (match-beginning 2)))
  12519. (progn
  12520. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12521. (goto-char (match-beginning 1))
  12522. (insert " ")
  12523. (delete-region (point) (1+ (match-beginning 2)))
  12524. (setq ncol (max (current-column)
  12525. (1+ col)
  12526. (if (> to-col 0)
  12527. to-col
  12528. (- (abs to-col) tags-l))))
  12529. (setq p (point))
  12530. (insert (make-string (- ncol (current-column)) ?\ ))
  12531. (setq ncol (current-column))
  12532. (when indent-tabs-mode (tabify p (point-at-eol)))
  12533. (org-move-to-column (min ncol col) t))
  12534. (goto-char pos))))
  12535. (defun org-set-tags-command (&optional arg just-align)
  12536. "Call the set-tags command for the current entry."
  12537. (interactive "P")
  12538. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12539. (org-set-tags arg just-align)
  12540. (save-excursion
  12541. (org-back-to-heading t)
  12542. (org-set-tags arg just-align))))
  12543. (defun org-set-tags-to (data)
  12544. "Set the tags of the current entry to DATA, replacing the current tags.
  12545. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12546. If DATA is nil or the empty string, any tags will be removed."
  12547. (interactive "sTags: ")
  12548. (setq data
  12549. (cond
  12550. ((eq data nil) "")
  12551. ((equal data "") "")
  12552. ((stringp data)
  12553. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12554. ":"))
  12555. ((listp data)
  12556. (concat ":" (mapconcat 'identity data ":") ":"))))
  12557. (when data
  12558. (save-excursion
  12559. (org-back-to-heading t)
  12560. (when (looking-at org-complex-heading-regexp)
  12561. (if (match-end 5)
  12562. (progn
  12563. (goto-char (match-beginning 5))
  12564. (insert data)
  12565. (delete-region (point) (point-at-eol))
  12566. (org-set-tags nil 'align))
  12567. (goto-char (point-at-eol))
  12568. (insert " " data)
  12569. (org-set-tags nil 'align)))
  12570. (beginning-of-line 1)
  12571. (if (looking-at ".*?\\([ \t]+\\)$")
  12572. (delete-region (match-beginning 1) (match-end 1))))))
  12573. (defun org-align-all-tags ()
  12574. "Align the tags i all headings."
  12575. (interactive)
  12576. (save-excursion
  12577. (or (ignore-errors (org-back-to-heading t))
  12578. (outline-next-heading))
  12579. (if (org-at-heading-p)
  12580. (org-set-tags t)
  12581. (message "No headings"))))
  12582. (defvar org-indent-indentation-per-level)
  12583. (defun org-set-tags (&optional arg just-align)
  12584. "Set the tags for the current headline.
  12585. With prefix ARG, realign all tags in headings in the current buffer."
  12586. (interactive "P")
  12587. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12588. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12589. 'region-start-level 'region))
  12590. org-loop-over-headlines-in-active-region)
  12591. (org-map-entries
  12592. ;; We don't use ARG and JUST-ALIGN here these args are not
  12593. ;; useful when looping over headlines
  12594. `(org-set-tags)
  12595. org-loop-over-headlines-in-active-region
  12596. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12597. (let* ((re org-outline-regexp-bol)
  12598. (current (unless arg (org-get-tags-string)))
  12599. (col (current-column))
  12600. (org-setting-tags t)
  12601. table current-tags inherited-tags ; computed below when needed
  12602. tags p0 c0 c1 rpl di tc level)
  12603. (if arg
  12604. (save-excursion
  12605. (goto-char (point-min))
  12606. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12607. (while (re-search-forward re nil t)
  12608. (org-set-tags nil t)
  12609. (end-of-line 1)))
  12610. (message "All tags realigned to column %d" org-tags-column))
  12611. (if just-align
  12612. (setq tags current)
  12613. ;; Get a new set of tags from the user
  12614. (save-excursion
  12615. (setq table (append org-tag-persistent-alist
  12616. (or org-tag-alist (org-get-buffer-tags))
  12617. (and
  12618. org-complete-tags-always-offer-all-agenda-tags
  12619. (org-global-tags-completion-table
  12620. (org-agenda-files))))
  12621. org-last-tags-completion-table table
  12622. current-tags (org-split-string current ":")
  12623. inherited-tags (nreverse
  12624. (nthcdr (length current-tags)
  12625. (nreverse (org-get-tags-at))))
  12626. tags
  12627. (if (or (eq t org-use-fast-tag-selection)
  12628. (and org-use-fast-tag-selection
  12629. (delq nil (mapcar 'cdr table))))
  12630. (org-fast-tag-selection
  12631. current-tags inherited-tags table
  12632. (if org-fast-tag-selection-include-todo
  12633. org-todo-key-alist))
  12634. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12635. (org-trim
  12636. (org-icompleting-read "Tags: "
  12637. 'org-tags-completion-function
  12638. nil nil current 'org-tags-history))))))
  12639. (while (string-match "[-+&]+" tags)
  12640. ;; No boolean logic, just a list
  12641. (setq tags (replace-match ":" t t tags))))
  12642. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12643. (if org-tags-sort-function
  12644. (setq tags (mapconcat 'identity
  12645. (sort (org-split-string
  12646. tags (org-re "[^[:alnum:]_@#%]+"))
  12647. org-tags-sort-function) ":")))
  12648. (if (string-match "\\`[\t ]*\\'" tags)
  12649. (setq tags "")
  12650. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12651. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12652. ;; Insert new tags at the correct column
  12653. (beginning-of-line 1)
  12654. (setq level (or (and (looking-at org-outline-regexp)
  12655. (- (match-end 0) (point) 1))
  12656. 1))
  12657. (cond
  12658. ((and (equal current "") (equal tags "")))
  12659. ((re-search-forward
  12660. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12661. (point-at-eol) t)
  12662. (if (equal tags "")
  12663. (setq rpl "")
  12664. (goto-char (match-beginning 0))
  12665. (setq c0 (current-column)
  12666. ;; compute offset for the case of org-indent-mode active
  12667. di (if org-indent-mode
  12668. (* (1- org-indent-indentation-per-level) (1- level))
  12669. 0)
  12670. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12671. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12672. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12673. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12674. (replace-match rpl t t)
  12675. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12676. tags)
  12677. (t (error "Tags alignment failed")))
  12678. (org-move-to-column col)
  12679. (unless just-align
  12680. (run-hooks 'org-after-tags-change-hook))))))
  12681. (defun org-change-tag-in-region (beg end tag off)
  12682. "Add or remove TAG for each entry in the region.
  12683. This works in the agenda, and also in an org-mode buffer."
  12684. (interactive
  12685. (list (region-beginning) (region-end)
  12686. (let ((org-last-tags-completion-table
  12687. (if (derived-mode-p 'org-mode)
  12688. (org-get-buffer-tags)
  12689. (org-global-tags-completion-table))))
  12690. (org-icompleting-read
  12691. "Tag: " 'org-tags-completion-function nil nil nil
  12692. 'org-tags-history))
  12693. (progn
  12694. (message "[s]et or [r]emove? ")
  12695. (equal (read-char-exclusive) ?r))))
  12696. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12697. (let ((agendap (equal major-mode 'org-agenda-mode))
  12698. l1 l2 m buf pos newhead (cnt 0))
  12699. (goto-char end)
  12700. (setq l2 (1- (org-current-line)))
  12701. (goto-char beg)
  12702. (setq l1 (org-current-line))
  12703. (loop for l from l1 to l2 do
  12704. (org-goto-line l)
  12705. (setq m (get-text-property (point) 'org-hd-marker))
  12706. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12707. (and agendap m))
  12708. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12709. pos (if agendap m (point)))
  12710. (with-current-buffer buf
  12711. (save-excursion
  12712. (save-restriction
  12713. (goto-char pos)
  12714. (setq cnt (1+ cnt))
  12715. (org-toggle-tag tag (if off 'off 'on))
  12716. (setq newhead (org-get-heading)))))
  12717. (and agendap (org-agenda-change-all-lines newhead m))))
  12718. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12719. (defun org-tags-completion-function (string predicate &optional flag)
  12720. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12721. (confirm (lambda (x) (stringp (car x)))))
  12722. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12723. (setq s1 (match-string 1 string)
  12724. s2 (match-string 2 string))
  12725. (setq s1 "" s2 string))
  12726. (cond
  12727. ((eq flag nil)
  12728. ;; try completion
  12729. (setq rtn (try-completion s2 ctable confirm))
  12730. (if (stringp rtn)
  12731. (setq rtn
  12732. (concat s1 s2 (substring rtn (length s2))
  12733. (if (and org-add-colon-after-tag-completion
  12734. (assoc rtn ctable))
  12735. ":" ""))))
  12736. rtn)
  12737. ((eq flag t)
  12738. ;; all-completions
  12739. (all-completions s2 ctable confirm)
  12740. )
  12741. ((eq flag 'lambda)
  12742. ;; exact match?
  12743. (assoc s2 ctable)))
  12744. ))
  12745. (defun org-fast-tag-insert (kwd tags face &optional end)
  12746. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12747. (insert (format "%-12s" (concat kwd ":"))
  12748. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12749. (or end "")))
  12750. (defun org-fast-tag-show-exit (flag)
  12751. (save-excursion
  12752. (org-goto-line 3)
  12753. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12754. (replace-match ""))
  12755. (when flag
  12756. (end-of-line 1)
  12757. (org-move-to-column (- (window-width) 19) t)
  12758. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12759. (defun org-set-current-tags-overlay (current prefix)
  12760. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12761. (if (featurep 'xemacs)
  12762. (org-overlay-display org-tags-overlay (concat prefix s)
  12763. 'secondary-selection)
  12764. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12765. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12766. (defvar org-last-tag-selection-key nil)
  12767. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12768. "Fast tag selection with single keys.
  12769. CURRENT is the current list of tags in the headline, INHERITED is the
  12770. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12771. possibly with grouping information. TODO-TABLE is a similar table with
  12772. TODO keywords, should these have keys assigned to them.
  12773. If the keys are nil, a-z are automatically assigned.
  12774. Returns the new tags string, or nil to not change the current settings."
  12775. (let* ((fulltable (append table todo-table))
  12776. (maxlen (apply 'max (mapcar
  12777. (lambda (x)
  12778. (if (stringp (car x)) (string-width (car x)) 0))
  12779. fulltable)))
  12780. (buf (current-buffer))
  12781. (expert (eq org-fast-tag-selection-single-key 'expert))
  12782. (buffer-tags nil)
  12783. (fwidth (+ maxlen 3 1 3))
  12784. (ncol (/ (- (window-width) 4) fwidth))
  12785. (i-face 'org-done)
  12786. (c-face 'org-todo)
  12787. tg cnt e c char c1 c2 ntable tbl rtn
  12788. ov-start ov-end ov-prefix
  12789. (exit-after-next org-fast-tag-selection-single-key)
  12790. (done-keywords org-done-keywords)
  12791. groups ingroup)
  12792. (save-excursion
  12793. (beginning-of-line 1)
  12794. (if (looking-at
  12795. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12796. (setq ov-start (match-beginning 1)
  12797. ov-end (match-end 1)
  12798. ov-prefix "")
  12799. (setq ov-start (1- (point-at-eol))
  12800. ov-end (1+ ov-start))
  12801. (skip-chars-forward "^\n\r")
  12802. (setq ov-prefix
  12803. (concat
  12804. (buffer-substring (1- (point)) (point))
  12805. (if (> (current-column) org-tags-column)
  12806. " "
  12807. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12808. (move-overlay org-tags-overlay ov-start ov-end)
  12809. (save-window-excursion
  12810. (if expert
  12811. (set-buffer (get-buffer-create " *Org tags*"))
  12812. (delete-other-windows)
  12813. (split-window-vertically)
  12814. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12815. (erase-buffer)
  12816. (org-set-local 'org-done-keywords done-keywords)
  12817. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12818. (org-fast-tag-insert "Current" current c-face "\n\n")
  12819. (org-fast-tag-show-exit exit-after-next)
  12820. (org-set-current-tags-overlay current ov-prefix)
  12821. (setq tbl fulltable char ?a cnt 0)
  12822. (while (setq e (pop tbl))
  12823. (cond
  12824. ((equal (car e) :startgroup)
  12825. (push '() groups) (setq ingroup t)
  12826. (when (not (= cnt 0))
  12827. (setq cnt 0)
  12828. (insert "\n"))
  12829. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12830. ((equal (car e) :endgroup)
  12831. (setq ingroup nil cnt 0)
  12832. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12833. ((equal e '(:newline))
  12834. (when (not (= cnt 0))
  12835. (setq cnt 0)
  12836. (insert "\n")
  12837. (setq e (car tbl))
  12838. (while (equal (car tbl) '(:newline))
  12839. (insert "\n")
  12840. (setq tbl (cdr tbl)))))
  12841. (t
  12842. (setq tg (copy-sequence (car e)) c2 nil)
  12843. (if (cdr e)
  12844. (setq c (cdr e))
  12845. ;; automatically assign a character.
  12846. (setq c1 (string-to-char
  12847. (downcase (substring
  12848. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12849. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12850. (while (or (rassoc char ntable) (rassoc char table))
  12851. (setq char (1+ char)))
  12852. (setq c2 c1))
  12853. (setq c (or c2 char)))
  12854. (if ingroup (push tg (car groups)))
  12855. (setq tg (org-add-props tg nil 'face
  12856. (cond
  12857. ((not (assoc tg table))
  12858. (org-get-todo-face tg))
  12859. ((member tg current) c-face)
  12860. ((member tg inherited) i-face))))
  12861. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12862. (insert "[" c "] " tg (make-string
  12863. (- fwidth 4 (length tg)) ?\ ))
  12864. (push (cons tg c) ntable)
  12865. (when (= (setq cnt (1+ cnt)) ncol)
  12866. (insert "\n")
  12867. (if ingroup (insert " "))
  12868. (setq cnt 0)))))
  12869. (setq ntable (nreverse ntable))
  12870. (insert "\n")
  12871. (goto-char (point-min))
  12872. (if (not expert) (org-fit-window-to-buffer))
  12873. (setq rtn
  12874. (catch 'exit
  12875. (while t
  12876. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12877. (if (not groups) "no " "")
  12878. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12879. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12880. (setq org-last-tag-selection-key c)
  12881. (cond
  12882. ((= c ?\r) (throw 'exit t))
  12883. ((= c ?!)
  12884. (setq groups (not groups))
  12885. (goto-char (point-min))
  12886. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12887. ((= c ?\C-c)
  12888. (if (not expert)
  12889. (org-fast-tag-show-exit
  12890. (setq exit-after-next (not exit-after-next)))
  12891. (setq expert nil)
  12892. (delete-other-windows)
  12893. (set-window-buffer (split-window-vertically) " *Org tags*")
  12894. (org-switch-to-buffer-other-window " *Org tags*")
  12895. (org-fit-window-to-buffer)))
  12896. ((or (= c ?\C-g)
  12897. (and (= c ?q) (not (rassoc c ntable))))
  12898. (org-detach-overlay org-tags-overlay)
  12899. (setq quit-flag t))
  12900. ((= c ?\ )
  12901. (setq current nil)
  12902. (if exit-after-next (setq exit-after-next 'now)))
  12903. ((= c ?\t)
  12904. (condition-case nil
  12905. (setq tg (org-icompleting-read
  12906. "Tag: "
  12907. (or buffer-tags
  12908. (with-current-buffer buf
  12909. (org-get-buffer-tags)))))
  12910. (quit (setq tg "")))
  12911. (when (string-match "\\S-" tg)
  12912. (add-to-list 'buffer-tags (list tg))
  12913. (if (member tg current)
  12914. (setq current (delete tg current))
  12915. (push tg current)))
  12916. (if exit-after-next (setq exit-after-next 'now)))
  12917. ((setq e (rassoc c todo-table) tg (car e))
  12918. (with-current-buffer buf
  12919. (save-excursion (org-todo tg)))
  12920. (if exit-after-next (setq exit-after-next 'now)))
  12921. ((setq e (rassoc c ntable) tg (car e))
  12922. (if (member tg current)
  12923. (setq current (delete tg current))
  12924. (loop for g in groups do
  12925. (if (member tg g)
  12926. (mapc (lambda (x)
  12927. (setq current (delete x current)))
  12928. g)))
  12929. (push tg current))
  12930. (if exit-after-next (setq exit-after-next 'now))))
  12931. ;; Create a sorted list
  12932. (setq current
  12933. (sort current
  12934. (lambda (a b)
  12935. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12936. (if (eq exit-after-next 'now) (throw 'exit t))
  12937. (goto-char (point-min))
  12938. (beginning-of-line 2)
  12939. (delete-region (point) (point-at-eol))
  12940. (org-fast-tag-insert "Current" current c-face)
  12941. (org-set-current-tags-overlay current ov-prefix)
  12942. (while (re-search-forward
  12943. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12944. (setq tg (match-string 1))
  12945. (add-text-properties
  12946. (match-beginning 1) (match-end 1)
  12947. (list 'face
  12948. (cond
  12949. ((member tg current) c-face)
  12950. ((member tg inherited) i-face)
  12951. (t (get-text-property (match-beginning 1) 'face))))))
  12952. (goto-char (point-min)))))
  12953. (org-detach-overlay org-tags-overlay)
  12954. (if rtn
  12955. (mapconcat 'identity current ":")
  12956. nil))))
  12957. (defun org-get-tags-string ()
  12958. "Get the TAGS string in the current headline."
  12959. (unless (org-at-heading-p t)
  12960. (error "Not on a heading"))
  12961. (save-excursion
  12962. (beginning-of-line 1)
  12963. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12964. (org-match-string-no-properties 1)
  12965. "")))
  12966. (defun org-get-tags ()
  12967. "Get the list of tags specified in the current headline."
  12968. (org-split-string (org-get-tags-string) ":"))
  12969. (defun org-get-buffer-tags ()
  12970. "Get a table of all tags used in the buffer, for completion."
  12971. (let (tags)
  12972. (save-excursion
  12973. (goto-char (point-min))
  12974. (while (re-search-forward
  12975. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12976. (when (equal (char-after (point-at-bol 0)) ?*)
  12977. (mapc (lambda (x) (add-to-list 'tags x))
  12978. (org-split-string (org-match-string-no-properties 1) ":")))))
  12979. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12980. (mapcar 'list tags)))
  12981. ;;;; The mapping API
  12982. ;;;###autoload
  12983. (defun org-map-entries (func &optional match scope &rest skip)
  12984. "Call FUNC at each headline selected by MATCH in SCOPE.
  12985. FUNC is a function or a lisp form. The function will be called without
  12986. arguments, with the cursor positioned at the beginning of the headline.
  12987. The return values of all calls to the function will be collected and
  12988. returned as a list.
  12989. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12990. does not need to preserve point. After evaluation, the cursor will be
  12991. moved to the end of the line (presumably of the headline of the
  12992. processed entry) and search continues from there. Under some
  12993. circumstances, this may not produce the wanted results. For example,
  12994. if you have removed (e.g. archived) the current (sub)tree it could
  12995. mean that the next entry will be skipped entirely. In such cases, you
  12996. can specify the position from where search should continue by making
  12997. FUNC set the variable `org-map-continue-from' to the desired buffer
  12998. position.
  12999. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13000. Only headlines that are matched by this query will be considered during
  13001. the iteration. When MATCH is nil or t, all headlines will be
  13002. visited by the iteration.
  13003. SCOPE determines the scope of this command. It can be any of:
  13004. nil The current buffer, respecting the restriction if any
  13005. tree The subtree started with the entry at point
  13006. region The entries within the active region, if any
  13007. region-start-level
  13008. The entries within the active region, but only those at
  13009. the same level than the first one.
  13010. file The current buffer, without restriction
  13011. file-with-archives
  13012. The current buffer, and any archives associated with it
  13013. agenda All agenda files
  13014. agenda-with-archives
  13015. All agenda files with any archive files associated with them
  13016. \(file1 file2 ...)
  13017. If this is a list, all files in the list will be scanned
  13018. The remaining args are treated as settings for the skipping facilities of
  13019. the scanner. The following items can be given here:
  13020. archive skip trees with the archive tag.
  13021. comment skip trees with the COMMENT keyword
  13022. function or Emacs Lisp form:
  13023. will be used as value for `org-agenda-skip-function', so whenever
  13024. the function returns t, FUNC will not be called for that
  13025. entry and search will continue from the point where the
  13026. function leaves it.
  13027. If your function needs to retrieve the tags including inherited tags
  13028. at the *current* entry, you can use the value of the variable
  13029. `org-scanner-tags' which will be much faster than getting the value
  13030. with `org-get-tags-at'. If your function gets properties with
  13031. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13032. to t around the call to `org-entry-properties' to get the same speedup.
  13033. Note that if your function moves around to retrieve tags and properties at
  13034. a *different* entry, you cannot use these techniques."
  13035. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13036. (not (org-region-active-p)))
  13037. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13038. (org-agenda-skip-archived-trees (memq 'archive skip))
  13039. (org-agenda-skip-comment-trees (memq 'comment skip))
  13040. (org-agenda-skip-function
  13041. (car (org-delete-all '(comment archive) skip)))
  13042. (org-tags-match-list-sublevels t)
  13043. (start-level (eq scope 'region-start-level))
  13044. matcher file res
  13045. org-todo-keywords-for-agenda
  13046. org-done-keywords-for-agenda
  13047. org-todo-keyword-alist-for-agenda
  13048. org-drawers-for-agenda
  13049. org-tag-alist-for-agenda
  13050. todo-only)
  13051. (cond
  13052. ((eq match t) (setq matcher t))
  13053. ((eq match nil) (setq matcher t))
  13054. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13055. (save-excursion
  13056. (save-restriction
  13057. (cond ((eq scope 'tree)
  13058. (org-back-to-heading t)
  13059. (org-narrow-to-subtree)
  13060. (setq scope nil))
  13061. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13062. (org-region-active-p))
  13063. ;; If needed, set start-level to a string like "2"
  13064. (when start-level
  13065. (save-excursion
  13066. (goto-char (region-beginning))
  13067. (unless (org-at-heading-p) (outline-next-heading))
  13068. (setq start-level (org-current-level))))
  13069. (narrow-to-region (region-beginning)
  13070. (save-excursion
  13071. (goto-char (region-end))
  13072. (unless (and (bolp) (org-at-heading-p))
  13073. (outline-next-heading))
  13074. (point)))
  13075. (setq scope nil)))
  13076. (if (not scope)
  13077. (progn
  13078. (org-agenda-prepare-buffers
  13079. (list (buffer-file-name (current-buffer))))
  13080. (setq res (org-scan-tags func matcher todo-only start-level)))
  13081. ;; Get the right scope
  13082. (cond
  13083. ((and scope (listp scope) (symbolp (car scope)))
  13084. (setq scope (eval scope)))
  13085. ((eq scope 'agenda)
  13086. (setq scope (org-agenda-files t)))
  13087. ((eq scope 'agenda-with-archives)
  13088. (setq scope (org-agenda-files t))
  13089. (setq scope (org-add-archive-files scope)))
  13090. ((eq scope 'file)
  13091. (setq scope (list (buffer-file-name))))
  13092. ((eq scope 'file-with-archives)
  13093. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13094. (org-agenda-prepare-buffers scope)
  13095. (while (setq file (pop scope))
  13096. (with-current-buffer (org-find-base-buffer-visiting file)
  13097. (save-excursion
  13098. (save-restriction
  13099. (widen)
  13100. (goto-char (point-min))
  13101. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13102. res)))
  13103. ;;;; Properties
  13104. ;;; Setting and retrieving properties
  13105. (defconst org-special-properties
  13106. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13107. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13108. "The special properties valid in Org-mode.
  13109. These are properties that are not defined in the property drawer,
  13110. but in some other way.")
  13111. (defconst org-default-properties
  13112. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13113. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13114. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13115. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13116. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13117. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13118. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13119. "Some properties that are used by Org-mode for various purposes.
  13120. Being in this list makes sure that they are offered for completion.")
  13121. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13122. "Regular expression matching the first line of a property drawer.")
  13123. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13124. "Regular expression matching the last line of a property drawer.")
  13125. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13126. "Regular expression matching the first line of a property drawer.")
  13127. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13128. "Regular expression matching the first line of a property drawer.")
  13129. (defconst org-property-drawer-re
  13130. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13131. org-property-end-re "\\)\n?")
  13132. "Matches an entire property drawer.")
  13133. (defconst org-clock-drawer-re
  13134. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13135. org-property-end-re "\\)\n?")
  13136. "Matches an entire clock drawer.")
  13137. (defsubst org-re-property (property)
  13138. "Return a regexp matching a PROPERTY line.
  13139. Match group 1 will be set to the value."
  13140. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13141. (defsubst org-re-property-keyword (property)
  13142. "Return a regexp matching a PROPERTY line, possibly with no
  13143. value for the property."
  13144. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13145. (defun org-property-action ()
  13146. "Do an action on properties."
  13147. (interactive)
  13148. (let (c)
  13149. (org-at-property-p)
  13150. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13151. (setq c (read-char-exclusive))
  13152. (cond
  13153. ((equal c ?s)
  13154. (call-interactively 'org-set-property))
  13155. ((equal c ?d)
  13156. (call-interactively 'org-delete-property))
  13157. ((equal c ?D)
  13158. (call-interactively 'org-delete-property-globally))
  13159. ((equal c ?c)
  13160. (call-interactively 'org-compute-property-at-point))
  13161. (t (error "No such property action %c" c)))))
  13162. (defun org-inc-effort ()
  13163. "Increment the value of the effort property in the current entry."
  13164. (interactive)
  13165. (org-set-effort nil t))
  13166. (defun org-set-effort (&optional value increment)
  13167. "Set the effort property of the current entry.
  13168. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13169. 10th allowed value.
  13170. When INCREMENT is non-nil, set the property to the next allowed value."
  13171. (interactive "P")
  13172. (if (equal value 0) (setq value 10))
  13173. (let* ((completion-ignore-case t)
  13174. (prop org-effort-property)
  13175. (cur (org-entry-get nil prop))
  13176. (allowed (org-property-get-allowed-values nil prop 'table))
  13177. (existing (mapcar 'list (org-property-values prop)))
  13178. rpl
  13179. (val (cond
  13180. ((stringp value) value)
  13181. ((and allowed (integerp value))
  13182. (or (car (nth (1- value) allowed))
  13183. (car (org-last allowed))))
  13184. ((and allowed increment)
  13185. (or (caadr (member (list cur) allowed))
  13186. (error "Allowed effort values are not set")))
  13187. (allowed
  13188. (message "Select 1-9,0, [RET%s]: %s"
  13189. (if cur (concat "=" cur) "")
  13190. (mapconcat 'car allowed " "))
  13191. (setq rpl (read-char-exclusive))
  13192. (if (equal rpl ?\r)
  13193. cur
  13194. (setq rpl (- rpl ?0))
  13195. (if (equal rpl 0) (setq rpl 10))
  13196. (if (and (> rpl 0) (<= rpl (length allowed)))
  13197. (car (nth (1- rpl) allowed))
  13198. (org-completing-read "Effort: " allowed nil))))
  13199. (t
  13200. (let (org-completion-use-ido org-completion-use-iswitchb)
  13201. (org-completing-read
  13202. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13203. (concat "[" cur "]") "")
  13204. ": ")
  13205. existing nil nil "" nil cur))))))
  13206. (unless (equal (org-entry-get nil prop) val)
  13207. (org-entry-put nil prop val))
  13208. (message "%s is now %s" prop val)))
  13209. (defun org-at-property-p ()
  13210. "Is cursor inside a property drawer?"
  13211. (save-excursion
  13212. (beginning-of-line 1)
  13213. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13214. (save-match-data ;; Used by calling procedures
  13215. (let ((p (point))
  13216. (range (unless (org-before-first-heading-p)
  13217. (org-get-property-block))))
  13218. (and range (<= (car range) p) (< p (cdr range))))))))
  13219. (defun org-get-property-block (&optional beg end force)
  13220. "Return the (beg . end) range of the body of the property drawer.
  13221. BEG and END are the beginning and end of the current subtree, or of
  13222. the part before the first headline. If they are not given, they will
  13223. be found. If the drawer does not exist and FORCE is non-nil, create
  13224. the drawer."
  13225. (catch 'exit
  13226. (save-excursion
  13227. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13228. (progn (org-back-to-heading t) (point))))
  13229. (end (or end (and (not (outline-next-heading)) (point-max))
  13230. (point))))
  13231. (goto-char beg)
  13232. (if (re-search-forward org-property-start-re end t)
  13233. (setq beg (1+ (match-end 0)))
  13234. (if force
  13235. (save-excursion
  13236. (org-insert-property-drawer)
  13237. (setq end (progn (outline-next-heading) (point))))
  13238. (throw 'exit nil))
  13239. (goto-char beg)
  13240. (if (re-search-forward org-property-start-re end t)
  13241. (setq beg (1+ (match-end 0)))))
  13242. (if (re-search-forward org-property-end-re end t)
  13243. (setq end (match-beginning 0))
  13244. (or force (throw 'exit nil))
  13245. (goto-char beg)
  13246. (setq end beg)
  13247. (org-indent-line)
  13248. (insert ":END:\n"))
  13249. (cons beg end)))))
  13250. (defun org-entry-properties (&optional pom which specific)
  13251. "Get all properties of the entry at point-or-marker POM.
  13252. This includes the TODO keyword, the tags, time strings for deadline,
  13253. scheduled, and clocking, and any additional properties defined in the
  13254. entry. The return value is an alist, keys may occur multiple times
  13255. if the property key was used several times.
  13256. POM may also be nil, in which case the current entry is used.
  13257. If WHICH is nil or `all', get all properties. If WHICH is
  13258. `special' or `standard', only get that subclass. If WHICH
  13259. is a string only get exactly this property. SPECIFIC can be a string, the
  13260. specific property we are interested in. Specifying it can speed
  13261. things up because then unnecessary parsing is avoided."
  13262. (setq which (or which 'all))
  13263. (org-with-point-at pom
  13264. (let ((clockstr (substring org-clock-string 0 -1))
  13265. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13266. (case-fold-search nil)
  13267. beg end range props sum-props key key1 value string clocksum clocksumt)
  13268. (save-excursion
  13269. (when (condition-case nil
  13270. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13271. (error nil))
  13272. (setq beg (point))
  13273. (setq sum-props (get-text-property (point) 'org-summaries))
  13274. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13275. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13276. (outline-next-heading)
  13277. (setq end (point))
  13278. (when (memq which '(all special))
  13279. ;; Get the special properties, like TODO and tags
  13280. (goto-char beg)
  13281. (when (and (or (not specific) (string= specific "TODO"))
  13282. (looking-at org-todo-line-regexp) (match-end 2))
  13283. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13284. (when (and (or (not specific) (string= specific "PRIORITY"))
  13285. (looking-at org-priority-regexp))
  13286. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13287. (when (or (not specific) (string= specific "FILE"))
  13288. (push (cons "FILE" buffer-file-name) props))
  13289. (when (and (or (not specific) (string= specific "TAGS"))
  13290. (setq value (org-get-tags-string))
  13291. (string-match "\\S-" value))
  13292. (push (cons "TAGS" value) props))
  13293. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13294. (setq value (org-get-tags-at)))
  13295. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13296. ":"))
  13297. props))
  13298. (when (or (not specific) (string= specific "BLOCKED"))
  13299. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13300. (when (or (not specific)
  13301. (member specific
  13302. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13303. "TIMESTAMP" "TIMESTAMP_IA")))
  13304. (catch 'match
  13305. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13306. (setq key (if (match-end 1)
  13307. (substring (org-match-string-no-properties 1)
  13308. 0 -1))
  13309. string (if (equal key clockstr)
  13310. (org-trim
  13311. (buffer-substring-no-properties
  13312. (match-beginning 3) (goto-char
  13313. (point-at-eol))))
  13314. (substring (org-match-string-no-properties 3)
  13315. 1 -1)))
  13316. ;; Get the correct property name from the key. This is
  13317. ;; necessary if the user has configured time keywords.
  13318. (setq key1 (concat key ":"))
  13319. (cond
  13320. ((not key)
  13321. (setq key
  13322. (if (= (char-after (match-beginning 3)) ?\[)
  13323. "TIMESTAMP_IA" "TIMESTAMP")))
  13324. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13325. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13326. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13327. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13328. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13329. (progn
  13330. (push (cons key string) props)
  13331. ;; no need to search further if match is found
  13332. (throw 'match t))
  13333. (when (or (equal key "CLOCK") (not (assoc key props)))
  13334. (push (cons key string) props)))))))
  13335. (when (memq which '(all standard))
  13336. ;; Get the standard properties, like :PROP: ...
  13337. (setq range (org-get-property-block beg end))
  13338. (when range
  13339. (goto-char (car range))
  13340. (while (re-search-forward
  13341. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13342. (cdr range) t)
  13343. (setq key (org-match-string-no-properties 1)
  13344. value (org-trim (or (org-match-string-no-properties 2) "")))
  13345. (unless (member key excluded)
  13346. (push (cons key (or value "")) props)))))
  13347. (if clocksum
  13348. (push (cons "CLOCKSUM"
  13349. (org-columns-number-to-string (/ (float clocksum) 60.)
  13350. 'add_times))
  13351. props))
  13352. (if clocksumt
  13353. (push (cons "CLOCKSUM_T"
  13354. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13355. 'add_times))
  13356. props))
  13357. (unless (assoc "CATEGORY" props)
  13358. (push (cons "CATEGORY" (org-get-category)) props))
  13359. (append sum-props (nreverse props)))))))
  13360. (defun org-entry-get (pom property &optional inherit literal-nil)
  13361. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13362. If INHERIT is non-nil and the entry does not have the property,
  13363. then also check higher levels of the hierarchy.
  13364. If INHERIT is the symbol `selective', use inheritance only if the setting
  13365. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13366. If the property is present but empty, the return value is the empty string.
  13367. If the property is not present at all, nil is returned.
  13368. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13369. do not interpret it as the list atom nil. This is used for inheritance
  13370. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13371. (org-with-point-at pom
  13372. (if (and inherit (if (eq inherit 'selective)
  13373. (org-property-inherit-p property)
  13374. t))
  13375. (org-entry-get-with-inheritance property literal-nil)
  13376. (if (member property org-special-properties)
  13377. ;; We need a special property. Use `org-entry-properties' to
  13378. ;; retrieve it, but specify the wanted property
  13379. (cdr (assoc property (org-entry-properties nil 'special property)))
  13380. (let* ((range (org-get-property-block))
  13381. (props (list (or (assoc property org-file-properties)
  13382. (assoc property org-global-properties)
  13383. (assoc property org-global-properties-fixed))))
  13384. (ap (lambda (key)
  13385. (when (re-search-forward
  13386. (org-re-property key) (cdr range) t)
  13387. (setq props
  13388. (org-update-property-plist
  13389. key
  13390. (if (match-end 1)
  13391. (org-match-string-no-properties 1) "")
  13392. props)))))
  13393. val)
  13394. (when (and range (goto-char (car range)))
  13395. (funcall ap property)
  13396. (goto-char (car range))
  13397. (while (funcall ap (concat property "+")))
  13398. (setq val (cdr (assoc property props)))
  13399. (when val (if literal-nil val (org-not-nil val)))))))))
  13400. (defun org-property-or-variable-value (var &optional inherit)
  13401. "Check if there is a property fixing the value of VAR.
  13402. If yes, return this value. If not, return the current value of the variable."
  13403. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13404. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13405. (read prop)
  13406. (symbol-value var))))
  13407. (defun org-entry-delete (pom property)
  13408. "Delete the property PROPERTY from entry at point-or-marker POM."
  13409. (org-with-point-at pom
  13410. (if (member property org-special-properties)
  13411. nil ; cannot delete these properties.
  13412. (let ((range (org-get-property-block)))
  13413. (if (and range
  13414. (goto-char (car range))
  13415. (re-search-forward
  13416. (org-re-property property)
  13417. (cdr range) t))
  13418. (progn
  13419. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13420. t)
  13421. nil)))))
  13422. ;; Multi-values properties are properties that contain multiple values
  13423. ;; These values are assumed to be single words, separated by whitespace.
  13424. (defun org-entry-add-to-multivalued-property (pom property value)
  13425. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13426. (let* ((old (org-entry-get pom property))
  13427. (values (and old (org-split-string old "[ \t]"))))
  13428. (setq value (org-entry-protect-space value))
  13429. (unless (member value values)
  13430. (setq values (cons value values))
  13431. (org-entry-put pom property
  13432. (mapconcat 'identity values " ")))))
  13433. (defun org-entry-remove-from-multivalued-property (pom property value)
  13434. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13435. (let* ((old (org-entry-get pom property))
  13436. (values (and old (org-split-string old "[ \t]"))))
  13437. (setq value (org-entry-protect-space value))
  13438. (when (member value values)
  13439. (setq values (delete value values))
  13440. (org-entry-put pom property
  13441. (mapconcat 'identity values " ")))))
  13442. (defun org-entry-member-in-multivalued-property (pom property value)
  13443. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13444. (let* ((old (org-entry-get pom property))
  13445. (values (and old (org-split-string old "[ \t]"))))
  13446. (setq value (org-entry-protect-space value))
  13447. (member value values)))
  13448. (defun org-entry-get-multivalued-property (pom property)
  13449. "Return a list of values in a multivalued property."
  13450. (let* ((value (org-entry-get pom property))
  13451. (values (and value (org-split-string value "[ \t]"))))
  13452. (mapcar 'org-entry-restore-space values)))
  13453. (defun org-entry-put-multivalued-property (pom property &rest values)
  13454. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13455. VALUES should be a list of strings. Spaces will be protected."
  13456. (org-entry-put pom property
  13457. (mapconcat 'org-entry-protect-space values " "))
  13458. (let* ((value (org-entry-get pom property))
  13459. (values (and value (org-split-string value "[ \t]"))))
  13460. (mapcar 'org-entry-restore-space values)))
  13461. (defun org-entry-protect-space (s)
  13462. "Protect spaces and newline in string S."
  13463. (while (string-match " " s)
  13464. (setq s (replace-match "%20" t t s)))
  13465. (while (string-match "\n" s)
  13466. (setq s (replace-match "%0A" t t s)))
  13467. s)
  13468. (defun org-entry-restore-space (s)
  13469. "Restore spaces and newline in string S."
  13470. (while (string-match "%20" s)
  13471. (setq s (replace-match " " t t s)))
  13472. (while (string-match "%0A" s)
  13473. (setq s (replace-match "\n" t t s)))
  13474. s)
  13475. (defvar org-entry-property-inherited-from (make-marker)
  13476. "Marker pointing to the entry from where a property was inherited.
  13477. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13478. location of the entry where the inheritance search matched. If there was
  13479. no match, the marker will point nowhere.
  13480. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13481. is set.")
  13482. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13483. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13484. The search will stop at the first ancestor which has the property defined.
  13485. If the value found is \"nil\", return nil to show that the property
  13486. should be considered as undefined (this is the meaning of nil here).
  13487. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13488. (move-marker org-entry-property-inherited-from nil)
  13489. (let (tmp)
  13490. (save-excursion
  13491. (save-restriction
  13492. (widen)
  13493. (catch 'ex
  13494. (while t
  13495. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13496. (or (ignore-errors (org-back-to-heading t))
  13497. (goto-char (point-min)))
  13498. (move-marker org-entry-property-inherited-from (point))
  13499. (throw 'ex tmp))
  13500. (or (ignore-errors (org-up-heading-safe))
  13501. (throw 'ex nil))))))
  13502. (setq tmp (or tmp
  13503. (cdr (assoc property org-file-properties))
  13504. (cdr (assoc property org-global-properties))
  13505. (cdr (assoc property org-global-properties-fixed))))
  13506. (if literal-nil tmp (org-not-nil tmp))))
  13507. (defvar org-property-changed-functions nil
  13508. "Hook called when the value of a property has changed.
  13509. Each hook function should accept two arguments, the name of the property
  13510. and the new value.")
  13511. (defun org-entry-put (pom property value)
  13512. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13513. (org-with-point-at pom
  13514. (org-back-to-heading t)
  13515. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13516. range)
  13517. (cond
  13518. ((equal property "TODO")
  13519. (when (and (stringp value) (string-match "\\S-" value)
  13520. (not (member value org-todo-keywords-1)))
  13521. (error "\"%s\" is not a valid TODO state" value))
  13522. (if (or (not value)
  13523. (not (string-match "\\S-" value)))
  13524. (setq value 'none))
  13525. (org-todo value)
  13526. (org-set-tags nil 'align))
  13527. ((equal property "PRIORITY")
  13528. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13529. (string-to-char value) ?\ ))
  13530. (org-set-tags nil 'align))
  13531. ((equal property "CLOCKSUM")
  13532. (if (not (re-search-forward
  13533. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  13534. (error "Cannot find a clock log")
  13535. (goto-char (- (match-end 1) 2))
  13536. (cond
  13537. ((eq value 'earlier) (org-timestamp-down))
  13538. ((eq value 'later) (org-timestamp-up)))
  13539. (org-clock-sum-current-item)))
  13540. ((equal property "SCHEDULED")
  13541. (if (re-search-forward org-scheduled-time-regexp end t)
  13542. (cond
  13543. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13544. ((eq value 'later) (org-timestamp-change 1 'day))
  13545. (t (call-interactively 'org-schedule)))
  13546. (call-interactively 'org-schedule)))
  13547. ((equal property "DEADLINE")
  13548. (if (re-search-forward org-deadline-time-regexp end t)
  13549. (cond
  13550. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13551. ((eq value 'later) (org-timestamp-change 1 'day))
  13552. (t (call-interactively 'org-deadline)))
  13553. (call-interactively 'org-deadline)))
  13554. ((member property org-special-properties)
  13555. (error "The %s property can not yet be set with `org-entry-put'"
  13556. property))
  13557. (t ; a non-special property
  13558. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13559. (setq range (org-get-property-block beg end 'force))
  13560. (goto-char (car range))
  13561. (if (re-search-forward
  13562. (org-re-property-keyword property) (cdr range) t)
  13563. (progn
  13564. (delete-region (match-beginning 0) (match-end 0))
  13565. (goto-char (match-beginning 0)))
  13566. (goto-char (cdr range))
  13567. (insert "\n")
  13568. (backward-char 1)
  13569. (org-indent-line))
  13570. (insert ":" property ":")
  13571. (and value (insert " " value))
  13572. (org-indent-line)))))
  13573. (run-hook-with-args 'org-property-changed-functions property value)))
  13574. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13575. "Get all property keys in the current buffer.
  13576. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13577. like tags and TODO state.
  13578. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13579. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13580. and others.
  13581. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13582. formats in the current buffer."
  13583. (let (rtn range cfmt s p)
  13584. (save-excursion
  13585. (save-restriction
  13586. (widen)
  13587. (goto-char (point-min))
  13588. (while (re-search-forward org-property-start-re nil t)
  13589. (setq range (org-get-property-block))
  13590. (goto-char (car range))
  13591. (while (re-search-forward
  13592. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13593. (cdr range) t)
  13594. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13595. (outline-next-heading))))
  13596. (when include-specials
  13597. (setq rtn (append org-special-properties rtn)))
  13598. (when include-defaults
  13599. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13600. (add-to-list 'rtn org-effort-property))
  13601. (when include-columns
  13602. (save-excursion
  13603. (save-restriction
  13604. (widen)
  13605. (goto-char (point-min))
  13606. (while (re-search-forward
  13607. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13608. nil t)
  13609. (setq cfmt (match-string 2) s 0)
  13610. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13611. cfmt s)
  13612. (setq s (match-end 0)
  13613. p (match-string 1 cfmt))
  13614. (unless (or (equal p "ITEM")
  13615. (member p org-special-properties))
  13616. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13617. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13618. (defun org-property-values (key)
  13619. "Return a list of all values of property KEY in the current buffer."
  13620. (save-excursion
  13621. (save-restriction
  13622. (widen)
  13623. (goto-char (point-min))
  13624. (let ((re (org-re-property key))
  13625. values)
  13626. (while (re-search-forward re nil t)
  13627. (add-to-list 'values (org-trim (match-string 1))))
  13628. (delete "" values)))))
  13629. (defun org-insert-property-drawer ()
  13630. "Insert a property drawer into the current entry."
  13631. (org-back-to-heading t)
  13632. (looking-at org-outline-regexp)
  13633. (let ((indent (if org-adapt-indentation
  13634. (- (match-end 0) (match-beginning 0))
  13635. 0))
  13636. (beg (point))
  13637. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13638. end hiddenp)
  13639. (outline-next-heading)
  13640. (setq end (point))
  13641. (goto-char beg)
  13642. (while (re-search-forward re end t))
  13643. (setq hiddenp (outline-invisible-p))
  13644. (end-of-line 1)
  13645. (and (equal (char-after) ?\n) (forward-char 1))
  13646. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13647. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13648. ;; just skip this line
  13649. (beginning-of-line 2)
  13650. ;; Drawer start, find the end
  13651. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13652. (beginning-of-line 1)))
  13653. (org-skip-over-state-notes)
  13654. (skip-chars-backward " \t\n\r")
  13655. (if (eq (char-before) ?*) (forward-char 1))
  13656. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13657. (beginning-of-line 0)
  13658. (org-indent-to-column indent)
  13659. (beginning-of-line 2)
  13660. (org-indent-to-column indent)
  13661. (beginning-of-line 0)
  13662. (if hiddenp
  13663. (save-excursion
  13664. (org-back-to-heading t)
  13665. (hide-entry))
  13666. (org-flag-drawer t))))
  13667. (defun org-insert-drawer (&optional arg drawer)
  13668. "Insert a drawer at point.
  13669. Optional argument DRAWER, when non-nil, is a string representing
  13670. drawer's name. Otherwise, the user is prompted for a name.
  13671. If a region is active, insert the drawer around that region
  13672. instead.
  13673. Point is left between drawer's boundaries."
  13674. (interactive "P")
  13675. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13676. "LOGBOOK"))
  13677. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13678. ;; internally by Org. They are meant to be inserted
  13679. ;; automatically.
  13680. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13681. ;; Remove system drawers from list. Note: For some reason,
  13682. ;; `org-completing-read' ignores the predicate while
  13683. ;; `completing-read' handles it fine.
  13684. (drawer (if arg "PROPERTIES"
  13685. (or drawer
  13686. (completing-read
  13687. "Drawer: " org-drawers
  13688. (lambda (d) (not (member d system-drawers))))))))
  13689. (cond
  13690. ;; With C-u, fall back on `org-insert-property-drawer'
  13691. (arg (org-insert-property-drawer))
  13692. ;; With an active region, insert a drawer at point.
  13693. ((not (org-region-active-p))
  13694. (progn
  13695. (unless (bolp) (insert "\n"))
  13696. (insert (format ":%s:\n\n:END:\n" drawer))
  13697. (forward-line -2)))
  13698. ;; Otherwise, insert the drawer at point
  13699. (t
  13700. (let ((rbeg (region-beginning))
  13701. (rend (copy-marker (region-end))))
  13702. (unwind-protect
  13703. (progn
  13704. (goto-char rbeg)
  13705. (beginning-of-line)
  13706. (when (save-excursion
  13707. (re-search-forward org-outline-regexp-bol rend t))
  13708. (error "Drawers cannot contain headlines"))
  13709. ;; Position point at the beginning of the first
  13710. ;; non-blank line in region. Insert drawer's opening
  13711. ;; there, then indent it.
  13712. (org-skip-whitespace)
  13713. (beginning-of-line)
  13714. (insert ":" drawer ":\n")
  13715. (forward-line -1)
  13716. (indent-for-tab-command)
  13717. ;; Move point to the beginning of the first blank line
  13718. ;; after the last non-blank line in region. Insert
  13719. ;; drawer's closing, then indent it.
  13720. (goto-char rend)
  13721. (skip-chars-backward " \r\t\n")
  13722. (insert "\n:END:")
  13723. (deactivate-mark t)
  13724. (indent-for-tab-command)
  13725. (unless (eolp) (insert "\n")))
  13726. ;; Clear marker, whatever the outcome of insertion is.
  13727. (set-marker rend nil)))))))
  13728. (defvar org-property-set-functions-alist nil
  13729. "Property set function alist.
  13730. Each entry should have the following format:
  13731. (PROPERTY . READ-FUNCTION)
  13732. The read function will be called with the same argument as
  13733. `org-completing-read'.")
  13734. (defun org-set-property-function (property)
  13735. "Get the function that should be used to set PROPERTY.
  13736. This is computed according to `org-property-set-functions-alist'."
  13737. (or (cdr (assoc property org-property-set-functions-alist))
  13738. 'org-completing-read))
  13739. (defun org-read-property-value (property)
  13740. "Read PROPERTY value from user."
  13741. (let* ((completion-ignore-case t)
  13742. (allowed (org-property-get-allowed-values nil property 'table))
  13743. (cur (org-entry-get nil property))
  13744. (prompt (concat property " value"
  13745. (if (and cur (string-match "\\S-" cur))
  13746. (concat " [" cur "]") "") ": "))
  13747. (set-function (org-set-property-function property))
  13748. (val (if allowed
  13749. (funcall set-function prompt allowed nil
  13750. (not (get-text-property 0 'org-unrestricted
  13751. (caar allowed))))
  13752. (let (org-completion-use-ido org-completion-use-iswitchb)
  13753. (funcall set-function prompt
  13754. (mapcar 'list (org-property-values property))
  13755. nil nil "" nil cur)))))
  13756. (if (equal val "")
  13757. cur
  13758. val)))
  13759. (defvar org-last-set-property nil)
  13760. (defun org-read-property-name ()
  13761. "Read a property name."
  13762. (let* ((completion-ignore-case t)
  13763. (keys (org-buffer-property-keys nil t t))
  13764. (default-prop (or (save-excursion
  13765. (save-match-data
  13766. (beginning-of-line)
  13767. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13768. (null (string= (match-string 1) "END"))
  13769. (match-string 1))))
  13770. org-last-set-property))
  13771. (property (org-icompleting-read
  13772. (concat "Property"
  13773. (if default-prop (concat " [" default-prop "]") "")
  13774. ": ")
  13775. (mapcar 'list keys)
  13776. nil nil nil nil
  13777. default-prop
  13778. )))
  13779. (if (member property keys)
  13780. property
  13781. (or (cdr (assoc (downcase property)
  13782. (mapcar (lambda (x) (cons (downcase x) x))
  13783. keys)))
  13784. property))))
  13785. (defun org-set-property (property value)
  13786. "In the current entry, set PROPERTY to VALUE.
  13787. When called interactively, this will prompt for a property name, offering
  13788. completion on existing and default properties. And then it will prompt
  13789. for a value, offering completion either on allowed values (via an inherited
  13790. xxx_ALL property) or on existing values in other instances of this property
  13791. in the current file."
  13792. (interactive (list nil nil))
  13793. (let* ((property (or property (org-read-property-name)))
  13794. (value (or value (org-read-property-value property)))
  13795. (fn (cdr (assoc property org-properties-postprocess-alist))))
  13796. (setq org-last-set-property property)
  13797. ;; Possibly postprocess the inserted value:
  13798. (when fn (setq value (funcall fn value)))
  13799. (unless (equal (org-entry-get nil property) value)
  13800. (org-entry-put nil property value))))
  13801. (defun org-delete-property (property)
  13802. "In the current entry, delete PROPERTY."
  13803. (interactive
  13804. (let* ((completion-ignore-case t)
  13805. (prop (org-icompleting-read "Property: "
  13806. (org-entry-properties nil 'standard))))
  13807. (list prop)))
  13808. (message "Property %s %s" property
  13809. (if (org-entry-delete nil property)
  13810. "deleted"
  13811. "was not present in the entry")))
  13812. (defun org-delete-property-globally (property)
  13813. "Remove PROPERTY globally, from all entries."
  13814. (interactive
  13815. (let* ((completion-ignore-case t)
  13816. (prop (org-icompleting-read
  13817. "Globally remove property: "
  13818. (mapcar 'list (org-buffer-property-keys)))))
  13819. (list prop)))
  13820. (save-excursion
  13821. (save-restriction
  13822. (widen)
  13823. (goto-char (point-min))
  13824. (let ((cnt 0))
  13825. (while (re-search-forward
  13826. (org-re-property property)
  13827. nil t)
  13828. (setq cnt (1+ cnt))
  13829. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  13830. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13831. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13832. (defun org-compute-property-at-point ()
  13833. "Compute the property at point.
  13834. This looks for an enclosing column format, extracts the operator and
  13835. then applies it to the property in the column format's scope."
  13836. (interactive)
  13837. (unless (org-at-property-p)
  13838. (error "Not at a property"))
  13839. (let ((prop (org-match-string-no-properties 2)))
  13840. (org-columns-get-format-and-top-level)
  13841. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13842. (error "No operator defined for property %s" prop))
  13843. (org-columns-compute prop)))
  13844. (defvar org-property-allowed-value-functions nil
  13845. "Hook for functions supplying allowed values for a specific property.
  13846. The functions must take a single argument, the name of the property, and
  13847. return a flat list of allowed values. If \":ETC\" is one of
  13848. the values, this means that these values are intended as defaults for
  13849. completion, but that other values should be allowed too.
  13850. The functions must return nil if they are not responsible for this
  13851. property.")
  13852. (defun org-property-get-allowed-values (pom property &optional table)
  13853. "Get allowed values for the property PROPERTY.
  13854. When TABLE is non-nil, return an alist that can directly be used for
  13855. completion."
  13856. (let (vals)
  13857. (cond
  13858. ((equal property "TODO")
  13859. (setq vals (org-with-point-at pom
  13860. (append org-todo-keywords-1 '("")))))
  13861. ((equal property "PRIORITY")
  13862. (let ((n org-lowest-priority))
  13863. (while (>= n org-highest-priority)
  13864. (push (char-to-string n) vals)
  13865. (setq n (1- n)))))
  13866. ((member property org-special-properties))
  13867. ((setq vals (run-hook-with-args-until-success
  13868. 'org-property-allowed-value-functions property)))
  13869. (t
  13870. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13871. (when (and vals (string-match "\\S-" vals))
  13872. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13873. (setq vals (mapcar (lambda (x)
  13874. (cond ((stringp x) x)
  13875. ((numberp x) (number-to-string x))
  13876. ((symbolp x) (symbol-name x))
  13877. (t "???")))
  13878. vals)))))
  13879. (when (member ":ETC" vals)
  13880. (setq vals (remove ":ETC" vals))
  13881. (org-add-props (car vals) '(org-unrestricted t)))
  13882. (if table (mapcar 'list vals) vals)))
  13883. (defun org-property-previous-allowed-value (&optional previous)
  13884. "Switch to the next allowed value for this property."
  13885. (interactive)
  13886. (org-property-next-allowed-value t))
  13887. (defun org-property-next-allowed-value (&optional previous)
  13888. "Switch to the next allowed value for this property."
  13889. (interactive)
  13890. (unless (org-at-property-p)
  13891. (error "Not at a property"))
  13892. (let* ((key (match-string 2))
  13893. (value (match-string 3))
  13894. (allowed (or (org-property-get-allowed-values (point) key)
  13895. (and (member value '("[ ]" "[-]" "[X]"))
  13896. '("[ ]" "[X]"))))
  13897. nval)
  13898. (unless allowed
  13899. (error "Allowed values for this property have not been defined"))
  13900. (if previous (setq allowed (reverse allowed)))
  13901. (if (member value allowed)
  13902. (setq nval (car (cdr (member value allowed)))))
  13903. (setq nval (or nval (car allowed)))
  13904. (if (equal nval value)
  13905. (error "Only one allowed value for this property"))
  13906. (org-at-property-p)
  13907. (replace-match (concat " :" key ": " nval) t t)
  13908. (org-indent-line)
  13909. (beginning-of-line 1)
  13910. (skip-chars-forward " \t")
  13911. (run-hook-with-args 'org-property-changed-functions key nval)))
  13912. (defun org-find-olp (path &optional this-buffer)
  13913. "Return a marker pointing to the entry at outline path OLP.
  13914. If anything goes wrong, throw an error.
  13915. You can wrap this call to catch the error like this:
  13916. (condition-case msg
  13917. (org-mobile-locate-entry (match-string 4))
  13918. (error (nth 1 msg)))
  13919. The return value will then be either a string with the error message,
  13920. or a marker if everything is OK.
  13921. If THIS-BUFFER is set, the outline path does not contain a file,
  13922. only headings."
  13923. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13924. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13925. (level 1)
  13926. (lmin 1)
  13927. (lmax 1)
  13928. limit re end found pos heading cnt flevel)
  13929. (unless buffer (error "File not found :%s" file))
  13930. (with-current-buffer buffer
  13931. (save-excursion
  13932. (save-restriction
  13933. (widen)
  13934. (setq limit (point-max))
  13935. (goto-char (point-min))
  13936. (while (setq heading (pop path))
  13937. (setq re (format org-complex-heading-regexp-format
  13938. (regexp-quote heading)))
  13939. (setq cnt 0 pos (point))
  13940. (while (re-search-forward re end t)
  13941. (setq level (- (match-end 1) (match-beginning 1)))
  13942. (if (and (>= level lmin) (<= level lmax))
  13943. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13944. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13945. lmax heading))
  13946. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13947. lmax heading))
  13948. (goto-char found)
  13949. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13950. (setq end (save-excursion (org-end-of-subtree t t))))
  13951. (when (org-at-heading-p)
  13952. (move-marker (make-marker) (point))))))))
  13953. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13954. "Find node HEADING in BUFFER.
  13955. Return a marker to the heading if it was found, or nil if not.
  13956. If POS-ONLY is set, return just the position instead of a marker.
  13957. The heading text must match exact, but it may have a TODO keyword,
  13958. a priority cookie and tags in the standard locations."
  13959. (with-current-buffer (or buffer (current-buffer))
  13960. (save-excursion
  13961. (save-restriction
  13962. (widen)
  13963. (goto-char (point-min))
  13964. (let (case-fold-search)
  13965. (if (re-search-forward
  13966. (format org-complex-heading-regexp-format
  13967. (regexp-quote heading)) nil t)
  13968. (if pos-only
  13969. (match-beginning 0)
  13970. (move-marker (make-marker) (match-beginning 0)))))))))
  13971. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13972. "Find Org node headline HEADING in all .org files in directory DIR.
  13973. When the target headline is found, return a marker to this location."
  13974. (let ((files (directory-files (or dir default-directory)
  13975. nil "\\`[^.#].*\\.org\\'"))
  13976. file visiting m buffer)
  13977. (catch 'found
  13978. (while (setq file (pop files))
  13979. (message "trying %s" file)
  13980. (setq visiting (org-find-base-buffer-visiting file))
  13981. (setq buffer (or visiting (find-file-noselect file)))
  13982. (setq m (org-find-exact-headline-in-buffer
  13983. heading buffer))
  13984. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13985. (and m (throw 'found m))))))
  13986. (defun org-find-entry-with-id (ident)
  13987. "Locate the entry that contains the ID property with exact value IDENT.
  13988. IDENT can be a string, a symbol or a number, this function will search for
  13989. the string representation of it.
  13990. Return the position where this entry starts, or nil if there is no such entry."
  13991. (interactive "sID: ")
  13992. (let ((id (cond
  13993. ((stringp ident) ident)
  13994. ((symbol-name ident) (symbol-name ident))
  13995. ((numberp ident) (number-to-string ident))
  13996. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13997. (case-fold-search nil))
  13998. (save-excursion
  13999. (save-restriction
  14000. (widen)
  14001. (goto-char (point-min))
  14002. (when (re-search-forward
  14003. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14004. nil t)
  14005. (org-back-to-heading t)
  14006. (point))))))
  14007. ;;;; Timestamps
  14008. (defvar org-last-changed-timestamp nil)
  14009. (defvar org-last-inserted-timestamp nil
  14010. "The last time stamp inserted with `org-insert-time-stamp'.")
  14011. (defvar org-time-was-given) ; dynamically scoped parameter
  14012. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14013. (defvar org-ts-what) ; dynamically scoped parameter
  14014. (defun org-time-stamp (arg &optional inactive)
  14015. "Prompt for a date/time and insert a time stamp.
  14016. If the user specifies a time like HH:MM or if this command is
  14017. called with at least one prefix argument, the time stamp contains
  14018. the date and the time. Otherwise, only the date is be included.
  14019. All parts of a date not specified by the user is filled in from
  14020. the current date/time. So if you just press return without
  14021. typing anything, the time stamp will represent the current
  14022. date/time.
  14023. If there is already a timestamp at the cursor, it will be
  14024. modified.
  14025. With two universal prefix arguments, insert an active timestamp
  14026. with the current time without prompting the user."
  14027. (interactive "P")
  14028. (let* ((ts nil)
  14029. (default-time
  14030. ;; Default time is either today, or, when entering a range,
  14031. ;; the range start.
  14032. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14033. (save-excursion
  14034. (re-search-backward
  14035. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14036. (- (point) 20) t)))
  14037. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14038. (current-time)))
  14039. (default-input (and ts (org-get-compact-tod ts)))
  14040. (repeater (save-excursion
  14041. (save-match-data
  14042. (beginning-of-line)
  14043. (when (re-search-forward
  14044. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14045. (save-excursion (progn (end-of-line) (point))) t)
  14046. (match-string 0)))))
  14047. org-time-was-given org-end-time-was-given time)
  14048. (cond
  14049. ((and (org-at-timestamp-p t)
  14050. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14051. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14052. (insert "--")
  14053. (setq time (let ((this-command this-command))
  14054. (org-read-date arg 'totime nil nil
  14055. default-time default-input inactive)))
  14056. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14057. ((org-at-timestamp-p t)
  14058. (setq time (let ((this-command this-command))
  14059. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14060. (when (org-at-timestamp-p t) ; just to get the match data
  14061. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14062. (replace-match "")
  14063. (setq org-last-changed-timestamp
  14064. (org-insert-time-stamp
  14065. time (or org-time-was-given arg)
  14066. inactive nil nil (list org-end-time-was-given)))
  14067. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14068. (setq org-last-changed-timestamp
  14069. (concat (substring org-last-inserted-timestamp 0 -1)
  14070. " " repeater ">"))))
  14071. (message "Timestamp updated"))
  14072. ((equal arg '(16))
  14073. (org-insert-time-stamp (current-time) t))
  14074. (t
  14075. (setq time (let ((this-command this-command))
  14076. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14077. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14078. nil nil (list org-end-time-was-given))))))
  14079. ;; FIXME: can we use this for something else, like computing time differences?
  14080. (defun org-get-compact-tod (s)
  14081. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14082. (let* ((t1 (match-string 1 s))
  14083. (h1 (string-to-number (match-string 2 s)))
  14084. (m1 (string-to-number (match-string 3 s)))
  14085. (t2 (and (match-end 4) (match-string 5 s)))
  14086. (h2 (and t2 (string-to-number (match-string 6 s))))
  14087. (m2 (and t2 (string-to-number (match-string 7 s))))
  14088. dh dm)
  14089. (if (not t2)
  14090. t1
  14091. (setq dh (- h2 h1) dm (- m2 m1))
  14092. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14093. (concat t1 "+" (number-to-string dh)
  14094. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14095. (defun org-time-stamp-inactive (&optional arg)
  14096. "Insert an inactive time stamp.
  14097. An inactive time stamp is enclosed in square brackets instead of angle
  14098. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14099. does not link to the calendar and cannot be changed with the S-cursor keys.
  14100. So these are more for recording a certain time/date."
  14101. (interactive "P")
  14102. (org-time-stamp arg 'inactive))
  14103. (defvar org-date-ovl (make-overlay 1 1))
  14104. (overlay-put org-date-ovl 'face 'org-date-selected)
  14105. (org-detach-overlay org-date-ovl)
  14106. (defvar org-ans1) ; dynamically scoped parameter
  14107. (defvar org-ans2) ; dynamically scoped parameter
  14108. (defvar org-plain-time-of-day-regexp) ; defined below
  14109. (defvar org-overriding-default-time nil) ; dynamically scoped
  14110. (defvar org-read-date-overlay nil)
  14111. (defvar org-dcst nil) ; dynamically scoped
  14112. (defvar org-read-date-history nil)
  14113. (defvar org-read-date-final-answer nil)
  14114. (defvar org-read-date-analyze-futurep nil)
  14115. (defvar org-read-date-analyze-forced-year nil)
  14116. (defvar org-read-date-inactive)
  14117. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14118. default-time default-input inactive)
  14119. "Read a date, possibly a time, and make things smooth for the user.
  14120. The prompt will suggest to enter an ISO date, but you can also enter anything
  14121. which will at least partially be understood by `parse-time-string'.
  14122. Unrecognized parts of the date will default to the current day, month, year,
  14123. hour and minute. If this command is called to replace a timestamp at point,
  14124. or to enter the second timestamp of a range, the default time is taken
  14125. from the existing stamp. Furthermore, the command prefers the future,
  14126. so if you are giving a date where the year is not given, and the day-month
  14127. combination is already past in the current year, it will assume you
  14128. mean next year. For details, see the manual. A few examples:
  14129. 3-2-5 --> 2003-02-05
  14130. feb 15 --> currentyear-02-15
  14131. 2/15 --> currentyear-02-15
  14132. sep 12 9 --> 2009-09-12
  14133. 12:45 --> today 12:45
  14134. 22 sept 0:34 --> currentyear-09-22 0:34
  14135. 12 --> currentyear-currentmonth-12
  14136. Fri --> nearest Friday (today or later)
  14137. etc.
  14138. Furthermore you can specify a relative date by giving, as the *first* thing
  14139. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14140. change in days weeks, months, years.
  14141. With a single plus or minus, the date is relative to today. With a double
  14142. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14143. +4d --> four days from today
  14144. +4 --> same as above
  14145. +2w --> two weeks from today
  14146. ++5 --> five days from default date
  14147. The function understands only English month and weekday abbreviations.
  14148. While prompting, a calendar is popped up - you can also select the
  14149. date with the mouse (button 1). The calendar shows a period of three
  14150. months. To scroll it to other months, use the keys `>' and `<'.
  14151. If you don't like the calendar, turn it off with
  14152. \(setq org-read-date-popup-calendar nil)
  14153. With optional argument TO-TIME, the date will immediately be converted
  14154. to an internal time.
  14155. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14156. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14157. still enter a time, and this function will inform the calling routine
  14158. about this change. The calling routine may then choose to change the
  14159. format used to insert the time stamp into the buffer to include the time.
  14160. With optional argument FROM-STRING, read from this string instead from
  14161. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14162. the time/date that is used for everything that is not specified by the
  14163. user."
  14164. (require 'parse-time)
  14165. (let* ((org-time-stamp-rounding-minutes
  14166. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14167. (org-dcst org-display-custom-times)
  14168. (ct (org-current-time))
  14169. (org-def (or org-overriding-default-time default-time ct))
  14170. (org-defdecode (decode-time org-def))
  14171. (dummy (progn
  14172. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14173. (setcar (nthcdr 2 org-defdecode) -1)
  14174. (setcar (nthcdr 1 org-defdecode) 59)
  14175. (setq org-def (apply 'encode-time org-defdecode)
  14176. org-defdecode (decode-time org-def)))))
  14177. (calendar-frame-setup nil)
  14178. (calendar-setup nil)
  14179. (calendar-move-hook nil)
  14180. (calendar-view-diary-initially-flag nil)
  14181. (calendar-view-holidays-initially-flag nil)
  14182. (timestr (format-time-string
  14183. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14184. (prompt (concat (if prompt (concat prompt " ") "")
  14185. (format "Date+time [%s]: " timestr)))
  14186. ans (org-ans0 "") org-ans1 org-ans2 final)
  14187. (cond
  14188. (from-string (setq ans from-string))
  14189. (org-read-date-popup-calendar
  14190. (save-excursion
  14191. (save-window-excursion
  14192. (calendar)
  14193. (org-eval-in-calendar '(setq cursor-type nil) t)
  14194. (unwind-protect
  14195. (progn
  14196. (calendar-forward-day (- (time-to-days org-def)
  14197. (calendar-absolute-from-gregorian
  14198. (calendar-current-date))))
  14199. (org-eval-in-calendar nil t)
  14200. (let* ((old-map (current-local-map))
  14201. (map (copy-keymap calendar-mode-map))
  14202. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  14203. (org-defkey map (kbd "RET") 'org-calendar-select)
  14204. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14205. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14206. (org-defkey minibuffer-local-map [(meta shift left)]
  14207. (lambda () (interactive)
  14208. (org-eval-in-calendar '(calendar-backward-month 1))))
  14209. (org-defkey minibuffer-local-map [(meta shift right)]
  14210. (lambda () (interactive)
  14211. (org-eval-in-calendar '(calendar-forward-month 1))))
  14212. (org-defkey minibuffer-local-map [(meta shift up)]
  14213. (lambda () (interactive)
  14214. (org-eval-in-calendar '(calendar-backward-year 1))))
  14215. (org-defkey minibuffer-local-map [(meta shift down)]
  14216. (lambda () (interactive)
  14217. (org-eval-in-calendar '(calendar-forward-year 1))))
  14218. (org-defkey minibuffer-local-map [?\e (shift left)]
  14219. (lambda () (interactive)
  14220. (org-eval-in-calendar '(calendar-backward-month 1))))
  14221. (org-defkey minibuffer-local-map [?\e (shift right)]
  14222. (lambda () (interactive)
  14223. (org-eval-in-calendar '(calendar-forward-month 1))))
  14224. (org-defkey minibuffer-local-map [?\e (shift up)]
  14225. (lambda () (interactive)
  14226. (org-eval-in-calendar '(calendar-backward-year 1))))
  14227. (org-defkey minibuffer-local-map [?\e (shift down)]
  14228. (lambda () (interactive)
  14229. (org-eval-in-calendar '(calendar-forward-year 1))))
  14230. (org-defkey minibuffer-local-map [(shift up)]
  14231. (lambda () (interactive)
  14232. (org-eval-in-calendar '(calendar-backward-week 1))))
  14233. (org-defkey minibuffer-local-map [(shift down)]
  14234. (lambda () (interactive)
  14235. (org-eval-in-calendar '(calendar-forward-week 1))))
  14236. (org-defkey minibuffer-local-map [(shift left)]
  14237. (lambda () (interactive)
  14238. (org-eval-in-calendar '(calendar-backward-day 1))))
  14239. (org-defkey minibuffer-local-map [(shift right)]
  14240. (lambda () (interactive)
  14241. (org-eval-in-calendar '(calendar-forward-day 1))))
  14242. (org-defkey minibuffer-local-map ">"
  14243. (lambda () (interactive)
  14244. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14245. (org-defkey minibuffer-local-map "<"
  14246. (lambda () (interactive)
  14247. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14248. (org-defkey minibuffer-local-map "\C-v"
  14249. (lambda () (interactive)
  14250. (org-eval-in-calendar
  14251. '(calendar-scroll-left-three-months 1))))
  14252. (org-defkey minibuffer-local-map "\M-v"
  14253. (lambda () (interactive)
  14254. (org-eval-in-calendar
  14255. '(calendar-scroll-right-three-months 1))))
  14256. (run-hooks 'org-read-date-minibuffer-setup-hook)
  14257. (unwind-protect
  14258. (progn
  14259. (use-local-map map)
  14260. (setq org-read-date-inactive inactive)
  14261. (add-hook 'post-command-hook 'org-read-date-display)
  14262. (setq org-ans0 (read-string prompt default-input
  14263. 'org-read-date-history nil))
  14264. ;; org-ans0: from prompt
  14265. ;; org-ans1: from mouse click
  14266. ;; org-ans2: from calendar motion
  14267. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14268. (remove-hook 'post-command-hook 'org-read-date-display)
  14269. (use-local-map old-map)
  14270. (when org-read-date-overlay
  14271. (delete-overlay org-read-date-overlay)
  14272. (setq org-read-date-overlay nil)))))
  14273. (bury-buffer "*Calendar*")))))
  14274. (t ; Naked prompt only
  14275. (unwind-protect
  14276. (setq ans (read-string prompt default-input
  14277. 'org-read-date-history timestr))
  14278. (when org-read-date-overlay
  14279. (delete-overlay org-read-date-overlay)
  14280. (setq org-read-date-overlay nil)))))
  14281. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14282. (when org-read-date-analyze-forced-year
  14283. (message "Year was forced into %s"
  14284. (if org-read-date-force-compatible-dates
  14285. "compatible range (1970-2037)"
  14286. "range representable on this machine"))
  14287. (ding))
  14288. ;; One round trip to get rid of 34th of August and stuff like that....
  14289. (setq final (decode-time (apply 'encode-time final)))
  14290. (setq org-read-date-final-answer ans)
  14291. (if to-time
  14292. (apply 'encode-time final)
  14293. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14294. (format "%04d-%02d-%02d %02d:%02d"
  14295. (nth 5 final) (nth 4 final) (nth 3 final)
  14296. (nth 2 final) (nth 1 final))
  14297. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14298. (defvar org-def)
  14299. (defvar org-defdecode)
  14300. (defvar org-with-time)
  14301. (defun org-read-date-display ()
  14302. "Display the current date prompt interpretation in the minibuffer."
  14303. (when org-read-date-display-live
  14304. (when org-read-date-overlay
  14305. (delete-overlay org-read-date-overlay))
  14306. (when (minibufferp (current-buffer))
  14307. (save-excursion
  14308. (end-of-line 1)
  14309. (while (not (equal (buffer-substring
  14310. (max (point-min) (- (point) 4)) (point))
  14311. " "))
  14312. (insert " ")))
  14313. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14314. " " (or org-ans1 org-ans2)))
  14315. (org-end-time-was-given nil)
  14316. (f (org-read-date-analyze ans org-def org-defdecode))
  14317. (fmts (if org-dcst
  14318. org-time-stamp-custom-formats
  14319. org-time-stamp-formats))
  14320. (fmt (if (or org-with-time
  14321. (and (boundp 'org-time-was-given) org-time-was-given))
  14322. (cdr fmts)
  14323. (car fmts)))
  14324. (txt (format-time-string fmt (apply 'encode-time f)))
  14325. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14326. (txt (concat "=> " txt)))
  14327. (when (and org-end-time-was-given
  14328. (string-match org-plain-time-of-day-regexp txt))
  14329. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14330. org-end-time-was-given
  14331. (substring txt (match-end 0)))))
  14332. (when org-read-date-analyze-futurep
  14333. (setq txt (concat txt " (=>F)")))
  14334. (setq org-read-date-overlay
  14335. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14336. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14337. (defun org-read-date-analyze (ans org-def org-defdecode)
  14338. "Analyze the combined answer of the date prompt."
  14339. ;; FIXME: cleanup and comment
  14340. (let ((nowdecode (decode-time (current-time)))
  14341. delta deltan deltaw deltadef year month day
  14342. hour minute second wday pm h2 m2 tl wday1
  14343. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14344. (setq org-read-date-analyze-futurep nil
  14345. org-read-date-analyze-forced-year nil)
  14346. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14347. (setq ans "+0"))
  14348. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14349. (setq ans (replace-match "" t t ans)
  14350. deltan (car delta)
  14351. deltaw (nth 1 delta)
  14352. deltadef (nth 2 delta)))
  14353. ;; Check if there is an iso week date in there. If yes, store the
  14354. ;; info and postpone interpreting it until the rest of the parsing
  14355. ;; is done.
  14356. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14357. (setq iso-year (if (match-end 1)
  14358. (org-small-year-to-year
  14359. (string-to-number (match-string 1 ans))))
  14360. iso-weekday (if (match-end 3)
  14361. (string-to-number (match-string 3 ans)))
  14362. iso-week (string-to-number (match-string 2 ans)))
  14363. (setq ans (replace-match "" t t ans)))
  14364. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14365. (when (string-match
  14366. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14367. (setq year (if (match-end 2)
  14368. (string-to-number (match-string 2 ans))
  14369. (progn (setq kill-year t)
  14370. (string-to-number (format-time-string "%Y"))))
  14371. month (string-to-number (match-string 3 ans))
  14372. day (string-to-number (match-string 4 ans)))
  14373. (if (< year 100) (setq year (+ 2000 year)))
  14374. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14375. t nil ans)))
  14376. ;; Help matching dotted european dates
  14377. (when (string-match
  14378. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  14379. (setq year (if (match-end 3)
  14380. (string-to-number (match-string 3 ans))
  14381. (progn (setq kill-year t)
  14382. (string-to-number (format-time-string "%Y"))))
  14383. day (string-to-number (match-string 1 ans))
  14384. month (string-to-number (match-string 2 ans))
  14385. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14386. t nil ans)))
  14387. ;; Help matching american dates, like 5/30 or 5/30/7
  14388. (when (string-match
  14389. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14390. (setq year (if (match-end 4)
  14391. (string-to-number (match-string 4 ans))
  14392. (progn (setq kill-year t)
  14393. (string-to-number (format-time-string "%Y"))))
  14394. month (string-to-number (match-string 1 ans))
  14395. day (string-to-number (match-string 2 ans)))
  14396. (if (< year 100) (setq year (+ 2000 year)))
  14397. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14398. t nil ans)))
  14399. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14400. ;; If there is a time with am/pm, and *no* time without it, we convert
  14401. ;; so that matching will be successful.
  14402. (loop for i from 1 to 2 do ; twice, for end time as well
  14403. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14404. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14405. (setq hour (string-to-number (match-string 1 ans))
  14406. minute (if (match-end 3)
  14407. (string-to-number (match-string 3 ans))
  14408. 0)
  14409. pm (equal ?p
  14410. (string-to-char (downcase (match-string 4 ans)))))
  14411. (if (and (= hour 12) (not pm))
  14412. (setq hour 0)
  14413. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14414. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14415. t t ans))))
  14416. ;; Check if a time range is given as a duration
  14417. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14418. (setq hour (string-to-number (match-string 1 ans))
  14419. h2 (+ hour (string-to-number (match-string 3 ans)))
  14420. minute (string-to-number (match-string 2 ans))
  14421. m2 (+ minute (if (match-end 5) (string-to-number
  14422. (match-string 5 ans))0)))
  14423. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14424. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14425. t t ans)))
  14426. ;; Check if there is a time range
  14427. (when (boundp 'org-end-time-was-given)
  14428. (setq org-time-was-given nil)
  14429. (when (and (string-match org-plain-time-of-day-regexp ans)
  14430. (match-end 8))
  14431. (setq org-end-time-was-given (match-string 8 ans))
  14432. (setq ans (concat (substring ans 0 (match-beginning 7))
  14433. (substring ans (match-end 7))))))
  14434. (setq tl (parse-time-string ans)
  14435. day (or (nth 3 tl) (nth 3 org-defdecode))
  14436. month (or (nth 4 tl)
  14437. (if (and org-read-date-prefer-future
  14438. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14439. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14440. (nth 4 org-defdecode)))
  14441. year (or (and (not kill-year) (nth 5 tl))
  14442. (if (and org-read-date-prefer-future
  14443. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14444. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14445. (nth 5 org-defdecode)))
  14446. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14447. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14448. second (or (nth 0 tl) 0)
  14449. wday (nth 6 tl))
  14450. (when (and (eq org-read-date-prefer-future 'time)
  14451. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14452. (equal day (nth 3 nowdecode))
  14453. (equal month (nth 4 nowdecode))
  14454. (equal year (nth 5 nowdecode))
  14455. (nth 2 tl)
  14456. (or (< (nth 2 tl) (nth 2 nowdecode))
  14457. (and (= (nth 2 tl) (nth 2 nowdecode))
  14458. (nth 1 tl)
  14459. (< (nth 1 tl) (nth 1 nowdecode)))))
  14460. (setq day (1+ day)
  14461. futurep t))
  14462. ;; Special date definitions below
  14463. (cond
  14464. (iso-week
  14465. ;; There was an iso week
  14466. (require 'cal-iso)
  14467. (setq futurep nil)
  14468. (setq year (or iso-year year)
  14469. day (or iso-weekday wday 1)
  14470. wday nil ; to make sure that the trigger below does not match
  14471. iso-date (calendar-gregorian-from-absolute
  14472. (calendar-absolute-from-iso
  14473. (list iso-week day year))))
  14474. ; FIXME: Should we also push ISO weeks into the future?
  14475. ; (when (and org-read-date-prefer-future
  14476. ; (not iso-year)
  14477. ; (< (calendar-absolute-from-gregorian iso-date)
  14478. ; (time-to-days (current-time))))
  14479. ; (setq year (1+ year)
  14480. ; iso-date (calendar-gregorian-from-absolute
  14481. ; (calendar-absolute-from-iso
  14482. ; (list iso-week day year)))))
  14483. (setq month (car iso-date)
  14484. year (nth 2 iso-date)
  14485. day (nth 1 iso-date)))
  14486. (deltan
  14487. (setq futurep nil)
  14488. (unless deltadef
  14489. (let ((now (decode-time (current-time))))
  14490. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14491. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14492. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14493. ((equal deltaw "m") (setq month (+ month deltan)))
  14494. ((equal deltaw "y") (setq year (+ year deltan)))))
  14495. ((and wday (not (nth 3 tl)))
  14496. ;; Weekday was given, but no day, so pick that day in the week
  14497. ;; on or after the derived date.
  14498. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14499. (unless (equal wday wday1)
  14500. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14501. (if (and (boundp 'org-time-was-given)
  14502. (nth 2 tl))
  14503. (setq org-time-was-given t))
  14504. (if (< year 100) (setq year (+ 2000 year)))
  14505. ;; Check of the date is representable
  14506. (if org-read-date-force-compatible-dates
  14507. (progn
  14508. (if (< year 1970)
  14509. (setq year 1970 org-read-date-analyze-forced-year t))
  14510. (if (> year 2037)
  14511. (setq year 2037 org-read-date-analyze-forced-year t)))
  14512. (condition-case nil
  14513. (ignore (encode-time second minute hour day month year))
  14514. (error
  14515. (setq year (nth 5 org-defdecode))
  14516. (setq org-read-date-analyze-forced-year t))))
  14517. (setq org-read-date-analyze-futurep futurep)
  14518. (list second minute hour day month year)))
  14519. (defvar parse-time-weekdays)
  14520. (defun org-read-date-get-relative (s today default)
  14521. "Check string S for special relative date string.
  14522. TODAY and DEFAULT are internal times, for today and for a default.
  14523. Return shift list (N what def-flag)
  14524. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14525. N is the number of WHATs to shift.
  14526. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14527. the DEFAULT date rather than TODAY."
  14528. (require 'parse-time)
  14529. (when (and
  14530. (string-match
  14531. (concat
  14532. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14533. "\\([0-9]+\\)?"
  14534. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14535. "\\([ \t]\\|$\\)") s)
  14536. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14537. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14538. (string-to-char (substring (match-string 1 s) -1))
  14539. ?+))
  14540. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14541. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14542. (what (if (match-end 3) (match-string 3 s) "d"))
  14543. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14544. (date (if rel default today))
  14545. (wday (nth 6 (decode-time date)))
  14546. delta)
  14547. (if wday1
  14548. (progn
  14549. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14550. (if (= dir ?-) (setq delta (- delta 7)))
  14551. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14552. (list delta "d" rel))
  14553. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14554. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14555. "Turn a user-specified date into the internal representation.
  14556. The internal representation needed by the calendar is (month day year).
  14557. This is a wrapper to handle the brain-dead convention in calendar that
  14558. user function argument order change dependent on argument order."
  14559. (if (boundp 'calendar-date-style)
  14560. (cond
  14561. ((eq calendar-date-style 'american)
  14562. (list arg1 arg2 arg3))
  14563. ((eq calendar-date-style 'european)
  14564. (list arg2 arg1 arg3))
  14565. ((eq calendar-date-style 'iso)
  14566. (list arg2 arg3 arg1)))
  14567. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14568. (if (org-bound-and-true-p european-calendar-style)
  14569. (list arg2 arg1 arg3)
  14570. (list arg1 arg2 arg3)))))
  14571. (defun org-eval-in-calendar (form &optional keepdate)
  14572. "Eval FORM in the calendar window and return to current window.
  14573. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14574. otherwise stick to the current value of `org-ans2'."
  14575. (let ((sf (selected-frame))
  14576. (sw (selected-window)))
  14577. (select-window (get-buffer-window "*Calendar*" t))
  14578. (eval form)
  14579. (when (and (not keepdate) (calendar-cursor-to-date))
  14580. (let* ((date (calendar-cursor-to-date))
  14581. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14582. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14583. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14584. (select-window sw)
  14585. (org-select-frame-set-input-focus sf)))
  14586. (defun org-calendar-select ()
  14587. "Return to `org-read-date' with the date currently selected.
  14588. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14589. (interactive)
  14590. (when (calendar-cursor-to-date)
  14591. (let* ((date (calendar-cursor-to-date))
  14592. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14593. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14594. (if (active-minibuffer-window) (exit-minibuffer))))
  14595. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14596. "Insert a date stamp for the date given by the internal TIME.
  14597. WITH-HM means use the stamp format that includes the time of the day.
  14598. INACTIVE means use square brackets instead of angular ones, so that the
  14599. stamp will not contribute to the agenda.
  14600. PRE and POST are optional strings to be inserted before and after the
  14601. stamp.
  14602. The command returns the inserted time stamp."
  14603. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14604. stamp)
  14605. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14606. (insert-before-markers (or pre ""))
  14607. (when (listp extra)
  14608. (setq extra (car extra))
  14609. (if (and (stringp extra)
  14610. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14611. (setq extra (format "-%02d:%02d"
  14612. (string-to-number (match-string 1 extra))
  14613. (string-to-number (match-string 2 extra))))
  14614. (setq extra nil)))
  14615. (when extra
  14616. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14617. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14618. (insert-before-markers (or post ""))
  14619. (setq org-last-inserted-timestamp stamp)))
  14620. (defun org-toggle-time-stamp-overlays ()
  14621. "Toggle the use of custom time stamp formats."
  14622. (interactive)
  14623. (setq org-display-custom-times (not org-display-custom-times))
  14624. (unless org-display-custom-times
  14625. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14626. (while (setq p (next-single-property-change p 'display))
  14627. (if (and (get-text-property p 'display)
  14628. (eq (get-text-property p 'face) 'org-date))
  14629. (remove-text-properties
  14630. p (setq p (next-single-property-change p 'display))
  14631. '(display t))))
  14632. (set-buffer-modified-p bmp)))
  14633. (if (featurep 'xemacs)
  14634. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14635. (org-restart-font-lock)
  14636. (setq org-table-may-need-update t)
  14637. (if org-display-custom-times
  14638. (message "Time stamps are overlaid with custom format")
  14639. (message "Time stamp overlays removed")))
  14640. (defun org-display-custom-time (beg end)
  14641. "Overlay modified time stamp format over timestamp between BEG and END."
  14642. (let* ((ts (buffer-substring beg end))
  14643. t1 w1 with-hm tf time str w2 (off 0))
  14644. (save-match-data
  14645. (setq t1 (org-parse-time-string ts t))
  14646. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14647. (setq off (- (match-end 0) (match-beginning 0)))))
  14648. (setq end (- end off))
  14649. (setq w1 (- end beg)
  14650. with-hm (and (nth 1 t1) (nth 2 t1))
  14651. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14652. time (org-fix-decoded-time t1)
  14653. str (org-add-props
  14654. (format-time-string
  14655. (substring tf 1 -1) (apply 'encode-time time))
  14656. nil 'mouse-face 'highlight)
  14657. w2 (length str))
  14658. (if (not (= w2 w1))
  14659. (add-text-properties (1+ beg) (+ 2 beg)
  14660. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14661. (if (featurep 'xemacs)
  14662. (progn
  14663. (put-text-property beg end 'invisible t)
  14664. (put-text-property beg end 'end-glyph (make-glyph str)))
  14665. (put-text-property beg end 'display str))))
  14666. (defun org-translate-time (string)
  14667. "Translate all timestamps in STRING to custom format.
  14668. But do this only if the variable `org-display-custom-times' is set."
  14669. (when org-display-custom-times
  14670. (save-match-data
  14671. (let* ((start 0)
  14672. (re org-ts-regexp-both)
  14673. t1 with-hm inactive tf time str beg end)
  14674. (while (setq start (string-match re string start))
  14675. (setq beg (match-beginning 0)
  14676. end (match-end 0)
  14677. t1 (save-match-data
  14678. (org-parse-time-string (substring string beg end) t))
  14679. with-hm (and (nth 1 t1) (nth 2 t1))
  14680. inactive (equal (substring string beg (1+ beg)) "[")
  14681. tf (funcall (if with-hm 'cdr 'car)
  14682. org-time-stamp-custom-formats)
  14683. time (org-fix-decoded-time t1)
  14684. str (format-time-string
  14685. (concat
  14686. (if inactive "[" "<") (substring tf 1 -1)
  14687. (if inactive "]" ">"))
  14688. (apply 'encode-time time))
  14689. string (replace-match str t t string)
  14690. start (+ start (length str)))))))
  14691. string)
  14692. (defun org-fix-decoded-time (time)
  14693. "Set 0 instead of nil for the first 6 elements of time.
  14694. Don't touch the rest."
  14695. (let ((n 0))
  14696. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14697. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.3")
  14698. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  14699. "Difference between TIMESTAMP-STRING and now in days.
  14700. If SECONDS is non-nil, return the difference in seconds."
  14701. (let ((fdiff (if seconds 'time-to-seconds 'time-to-days)))
  14702. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  14703. (funcall fdiff (current-time)))))
  14704. (defun org-deadline-close (timestamp-string &optional ndays)
  14705. "Is the time in TIMESTAMP-STRING close to the current date?"
  14706. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14707. (and (< (org-days-to-time timestamp-string) ndays)
  14708. (not (org-entry-is-done-p))))
  14709. (defun org-get-wdays (ts)
  14710. "Get the deadline lead time appropriate for timestring TS."
  14711. (cond
  14712. ((<= org-deadline-warning-days 0)
  14713. ;; 0 or negative, enforce this value no matter what
  14714. (- org-deadline-warning-days))
  14715. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14716. ;; lead time is specified.
  14717. (floor (* (string-to-number (match-string 1 ts))
  14718. (cdr (assoc (match-string 2 ts)
  14719. '(("d" . 1) ("w" . 7)
  14720. ("m" . 30.4) ("y" . 365.25)))))))
  14721. ;; go for the default.
  14722. (t org-deadline-warning-days)))
  14723. (defun org-calendar-select-mouse (ev)
  14724. "Return to `org-read-date' with the date currently selected.
  14725. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14726. (interactive "e")
  14727. (mouse-set-point ev)
  14728. (when (calendar-cursor-to-date)
  14729. (let* ((date (calendar-cursor-to-date))
  14730. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14731. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14732. (if (active-minibuffer-window) (exit-minibuffer))))
  14733. (defun org-check-deadlines (ndays)
  14734. "Check if there are any deadlines due or past due.
  14735. A deadline is considered due if it happens within `org-deadline-warning-days'
  14736. days from today's date. If the deadline appears in an entry marked DONE,
  14737. it is not shown. The prefix arg NDAYS can be used to test that many
  14738. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14739. (interactive "P")
  14740. (let* ((org-warn-days
  14741. (cond
  14742. ((equal ndays '(4)) 100000)
  14743. (ndays (prefix-numeric-value ndays))
  14744. (t (abs org-deadline-warning-days))))
  14745. (case-fold-search nil)
  14746. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14747. (callback
  14748. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14749. (message "%d deadlines past-due or due within %d days"
  14750. (org-occur regexp nil callback)
  14751. org-warn-days)))
  14752. (defsubst org-re-timestamp (type)
  14753. "Return a regexp for timestamp TYPE.
  14754. Allowed values for TYPE are:
  14755. all: all timestamps
  14756. active: only active timestamps (<...>)
  14757. inactive: only inactive timestamps ([...])
  14758. scheduled: only scheduled timestamps
  14759. deadline: only deadline timestamps
  14760. When TYPE is nil, fall back on returning a regexp that matches
  14761. both scheduled and deadline timestamps."
  14762. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  14763. ((eq type 'active) org-ts-regexp)
  14764. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  14765. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  14766. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14767. ((eq type 'scheduled-or-deadline)
  14768. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  14769. (defun org-check-before-date (date)
  14770. "Check if there are deadlines or scheduled entries before DATE."
  14771. (interactive (list (org-read-date)))
  14772. (let ((case-fold-search nil)
  14773. (regexp (org-re-timestamp org-ts-type))
  14774. (callback
  14775. (lambda () (time-less-p
  14776. (org-time-string-to-time (match-string 1))
  14777. (org-time-string-to-time date)))))
  14778. (message "%d entries before %s"
  14779. (org-occur regexp nil callback) date)))
  14780. (defun org-check-after-date (date)
  14781. "Check if there are deadlines or scheduled entries after DATE."
  14782. (interactive (list (org-read-date)))
  14783. (let ((case-fold-search nil)
  14784. (regexp (org-re-timestamp org-ts-type))
  14785. (callback
  14786. (lambda () (not
  14787. (time-less-p
  14788. (org-time-string-to-time (match-string 1))
  14789. (org-time-string-to-time date))))))
  14790. (message "%d entries after %s"
  14791. (org-occur regexp nil callback) date)))
  14792. (defun org-check-dates-range (start-date end-date)
  14793. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14794. (interactive (list (org-read-date nil nil nil "Range starts")
  14795. (org-read-date nil nil nil "Range end")))
  14796. (let ((case-fold-search nil)
  14797. (regexp (org-re-timestamp org-ts-type))
  14798. (callback
  14799. (lambda ()
  14800. (let ((match (match-string 1)))
  14801. (and
  14802. (not (time-less-p
  14803. (org-time-string-to-time match)
  14804. (org-time-string-to-time start-date)))
  14805. (time-less-p
  14806. (org-time-string-to-time match)
  14807. (org-time-string-to-time end-date)))))))
  14808. (message "%d entries between %s and %s"
  14809. (org-occur regexp nil callback) start-date end-date)))
  14810. (defun org-evaluate-time-range (&optional to-buffer)
  14811. "Evaluate a time range by computing the difference between start and end.
  14812. Normally the result is just printed in the echo area, but with prefix arg
  14813. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14814. If the time range is actually in a table, the result is inserted into the
  14815. next column.
  14816. For time difference computation, a year is assumed to be exactly 365
  14817. days in order to avoid rounding problems."
  14818. (interactive "P")
  14819. (or
  14820. (org-clock-update-time-maybe)
  14821. (save-excursion
  14822. (unless (org-at-date-range-p t)
  14823. (goto-char (point-at-bol))
  14824. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14825. (if (not (org-at-date-range-p t))
  14826. (error "Not at a time-stamp range, and none found in current line")))
  14827. (let* ((ts1 (match-string 1))
  14828. (ts2 (match-string 2))
  14829. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14830. (match-end (match-end 0))
  14831. (time1 (org-time-string-to-time ts1))
  14832. (time2 (org-time-string-to-time ts2))
  14833. (t1 (org-float-time time1))
  14834. (t2 (org-float-time time2))
  14835. (diff (abs (- t2 t1)))
  14836. (negative (< (- t2 t1) 0))
  14837. ;; (ys (floor (* 365 24 60 60)))
  14838. (ds (* 24 60 60))
  14839. (hs (* 60 60))
  14840. (fy "%dy %dd %02d:%02d")
  14841. (fy1 "%dy %dd")
  14842. (fd "%dd %02d:%02d")
  14843. (fd1 "%dd")
  14844. (fh "%02d:%02d")
  14845. y d h m align)
  14846. (if havetime
  14847. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14848. y 0
  14849. d (floor (/ diff ds)) diff (mod diff ds)
  14850. h (floor (/ diff hs)) diff (mod diff hs)
  14851. m (floor (/ diff 60)))
  14852. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14853. y 0
  14854. d (floor (+ (/ diff ds) 0.5))
  14855. h 0 m 0))
  14856. (if (not to-buffer)
  14857. (message "%s" (org-make-tdiff-string y d h m))
  14858. (if (org-at-table-p)
  14859. (progn
  14860. (goto-char match-end)
  14861. (setq align t)
  14862. (and (looking-at " *|") (goto-char (match-end 0))))
  14863. (goto-char match-end))
  14864. (if (looking-at
  14865. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14866. (replace-match ""))
  14867. (if negative (insert " -"))
  14868. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14869. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14870. (insert " " (format fh h m))))
  14871. (if align (org-table-align))
  14872. (message "Time difference inserted")))))
  14873. (defun org-make-tdiff-string (y d h m)
  14874. (let ((fmt "")
  14875. (l nil))
  14876. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14877. l (push y l)))
  14878. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14879. l (push d l)))
  14880. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14881. l (push h l)))
  14882. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14883. l (push m l)))
  14884. (apply 'format fmt (nreverse l))))
  14885. (defun org-time-string-to-time (s &optional buffer pos)
  14886. "Convert a timestamp string into internal time."
  14887. (condition-case errdata
  14888. (apply 'encode-time (org-parse-time-string s))
  14889. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14890. s (if (not (and buffer pos))
  14891. ""
  14892. (format " at %d in buffer `%s'" pos buffer))
  14893. (cdr errdata)))))
  14894. (defun org-time-string-to-seconds (s)
  14895. "Convert a timestamp string to a number of seconds."
  14896. (org-float-time (org-time-string-to-time s)))
  14897. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14898. "Convert a time stamp to an absolute day number.
  14899. If there is a specifier for a cyclic time stamp, get the closest date to
  14900. DAYNR.
  14901. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14902. The variable date is bound by the calendar when this is called."
  14903. (cond
  14904. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14905. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14906. daynr
  14907. (+ daynr 1000)))
  14908. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  14909. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14910. (time-to-days (current-time))) (match-string 0 s)
  14911. prefer show-all))
  14912. (t (time-to-days
  14913. (condition-case errdata
  14914. (apply 'encode-time (org-parse-time-string s))
  14915. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14916. s (if (not (and buffer pos))
  14917. ""
  14918. (format " at %d in buffer `%s'" pos buffer))
  14919. (cdr errdata))))))))
  14920. (defun org-days-to-iso-week (days)
  14921. "Return the iso week number."
  14922. (require 'cal-iso)
  14923. (car (calendar-iso-from-absolute days)))
  14924. (defun org-small-year-to-year (year)
  14925. "Convert 2-digit years into 4-digit years.
  14926. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14927. The year 2000 cannot be abbreviated. Any year larger than 99
  14928. is returned unchanged."
  14929. (if (< year 38)
  14930. (setq year (+ 2000 year))
  14931. (if (< year 100)
  14932. (setq year (+ 1900 year))))
  14933. year)
  14934. (defun org-time-from-absolute (d)
  14935. "Return the time corresponding to date D.
  14936. D may be an absolute day number, or a calendar-type list (month day year)."
  14937. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14938. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14939. (defun org-calendar-holiday ()
  14940. "List of holidays, for Diary display in Org-mode."
  14941. (require 'holidays)
  14942. (let ((hl (funcall
  14943. (if (fboundp 'calendar-check-holidays)
  14944. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14945. (if hl (mapconcat 'identity hl "; "))))
  14946. (defun org-diary-sexp-entry (sexp entry date)
  14947. "Process a SEXP diary ENTRY for DATE."
  14948. (require 'diary-lib)
  14949. (let ((result (if calendar-debug-sexp
  14950. (let ((stack-trace-on-error t))
  14951. (eval (car (read-from-string sexp))))
  14952. (condition-case nil
  14953. (eval (car (read-from-string sexp)))
  14954. (error
  14955. (beep)
  14956. (message "Bad sexp at line %d in %s: %s"
  14957. (org-current-line)
  14958. (buffer-file-name) sexp)
  14959. (sleep-for 2))))))
  14960. (cond ((stringp result) (split-string result "; "))
  14961. ((and (consp result)
  14962. (not (consp (cdr result)))
  14963. (stringp (cdr result))) (cdr result))
  14964. ((and (consp result)
  14965. (stringp (car result))) result)
  14966. (result entry))))
  14967. (defun org-diary-to-ical-string (frombuf)
  14968. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14969. This uses the icalendar.el library."
  14970. (let* ((tmpdir (if (featurep 'xemacs)
  14971. (temp-directory)
  14972. temporary-file-directory))
  14973. (tmpfile (make-temp-name
  14974. (expand-file-name "orgics" tmpdir)))
  14975. buf rtn b e)
  14976. (with-current-buffer frombuf
  14977. (icalendar-export-region (point-min) (point-max) tmpfile)
  14978. (setq buf (find-buffer-visiting tmpfile))
  14979. (set-buffer buf)
  14980. (goto-char (point-min))
  14981. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14982. (setq b (match-beginning 0)))
  14983. (goto-char (point-max))
  14984. (if (re-search-backward "^END:VEVENT" nil t)
  14985. (setq e (match-end 0)))
  14986. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14987. (kill-buffer buf)
  14988. (delete-file tmpfile)
  14989. rtn))
  14990. (defun org-closest-date (start current change prefer show-all)
  14991. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14992. When PREFER is `past', return a date that is either CURRENT or past.
  14993. When PREFER is `future', return a date that is either CURRENT or future.
  14994. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14995. ;; Make the proper lists from the dates
  14996. (catch 'exit
  14997. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14998. dn dw sday cday n1 n2 n0
  14999. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15000. (setq start (org-date-to-gregorian start)
  15001. current (org-date-to-gregorian
  15002. (if show-all
  15003. current
  15004. (time-to-days (current-time))))
  15005. sday (calendar-absolute-from-gregorian start)
  15006. cday (calendar-absolute-from-gregorian current))
  15007. (if (<= cday sday) (throw 'exit sday))
  15008. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15009. (setq dn (string-to-number (match-string 1 change))
  15010. dw (cdr (assoc (match-string 2 change) a1)))
  15011. (error "Invalid change specifier: %s" change))
  15012. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15013. (cond
  15014. ((eq dw 'day)
  15015. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15016. n2 (+ n1 dn)))
  15017. ((eq dw 'year)
  15018. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15019. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15020. (setq date1 (list m d y1)
  15021. n1 (calendar-absolute-from-gregorian date1)
  15022. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15023. n2 (calendar-absolute-from-gregorian date2)))
  15024. ((eq dw 'month)
  15025. ;; approx number of month between the two dates
  15026. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15027. ;; How often does dn fit in there?
  15028. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15029. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15030. m (+ m nm)
  15031. ny (floor (/ m 12))
  15032. y (+ y ny)
  15033. m (- m (* ny 12)))
  15034. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15035. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15036. (setq m2 (+ m dn) y2 y)
  15037. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15038. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15039. (while (<= n2 cday)
  15040. (setq n1 n2 m m2 y y2)
  15041. (setq m2 (+ m dn) y2 y)
  15042. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15043. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15044. ;; Make sure n1 is the earlier date
  15045. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15046. (if show-all
  15047. (cond
  15048. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15049. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15050. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15051. (cond
  15052. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15053. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15054. (t (if (= cday n1) n1 n2)))))))
  15055. (defun org-date-to-gregorian (date)
  15056. "Turn any specification of DATE into a Gregorian date for the calendar."
  15057. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15058. ((and (listp date) (= (length date) 3)) date)
  15059. ((stringp date)
  15060. (setq date (org-parse-time-string date))
  15061. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15062. ((listp date)
  15063. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15064. (defun org-parse-time-string (s &optional nodefault)
  15065. "Parse the standard Org-mode time string.
  15066. This should be a lot faster than the normal `parse-time-string'.
  15067. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15068. hour and minute fields will be nil if not given."
  15069. (if (string-match org-ts-regexp0 s)
  15070. (list 0
  15071. (if (or (match-beginning 8) (not nodefault))
  15072. (string-to-number (or (match-string 8 s) "0")))
  15073. (if (or (match-beginning 7) (not nodefault))
  15074. (string-to-number (or (match-string 7 s) "0")))
  15075. (string-to-number (match-string 4 s))
  15076. (string-to-number (match-string 3 s))
  15077. (string-to-number (match-string 2 s))
  15078. nil nil nil)
  15079. (error "Not a standard Org-mode time string: %s" s)))
  15080. (defun org-timestamp-up (&optional arg)
  15081. "Increase the date item at the cursor by one.
  15082. If the cursor is on the year, change the year. If it is on the month,
  15083. the day or the time, change that.
  15084. With prefix ARG, change by that many units."
  15085. (interactive "p")
  15086. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15087. (defun org-timestamp-down (&optional arg)
  15088. "Decrease the date item at the cursor by one.
  15089. If the cursor is on the year, change the year. If it is on the month,
  15090. the day or the time, change that.
  15091. With prefix ARG, change by that many units."
  15092. (interactive "p")
  15093. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15094. (defun org-timestamp-up-day (&optional arg)
  15095. "Increase the date in the time stamp by one day.
  15096. With prefix ARG, change that many days."
  15097. (interactive "p")
  15098. (if (and (not (org-at-timestamp-p t))
  15099. (org-at-heading-p))
  15100. (org-todo 'up)
  15101. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15102. (defun org-timestamp-down-day (&optional arg)
  15103. "Decrease the date in the time stamp by one day.
  15104. With prefix ARG, change that many days."
  15105. (interactive "p")
  15106. (if (and (not (org-at-timestamp-p t))
  15107. (org-at-heading-p))
  15108. (org-todo 'down)
  15109. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15110. (defun org-at-timestamp-p (&optional inactive-ok)
  15111. "Determine if the cursor is in or at a timestamp."
  15112. (interactive)
  15113. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15114. (pos (point))
  15115. (ans (or (looking-at tsr)
  15116. (save-excursion
  15117. (skip-chars-backward "^[<\n\r\t")
  15118. (if (> (point) (point-min)) (backward-char 1))
  15119. (and (looking-at tsr)
  15120. (> (- (match-end 0) pos) -1))))))
  15121. (and ans
  15122. (boundp 'org-ts-what)
  15123. (setq org-ts-what
  15124. (cond
  15125. ((= pos (match-beginning 0)) 'bracket)
  15126. ;; Point is considered to be "on the bracket" whether
  15127. ;; it's really on it or right after it.
  15128. ((= pos (1- (match-end 0))) 'bracket)
  15129. ((= pos (match-end 0)) 'after)
  15130. ((org-pos-in-match-range pos 2) 'year)
  15131. ((org-pos-in-match-range pos 3) 'month)
  15132. ((org-pos-in-match-range pos 7) 'hour)
  15133. ((org-pos-in-match-range pos 8) 'minute)
  15134. ((or (org-pos-in-match-range pos 4)
  15135. (org-pos-in-match-range pos 5)) 'day)
  15136. ((and (> pos (or (match-end 8) (match-end 5)))
  15137. (< pos (match-end 0)))
  15138. (- pos (or (match-end 8) (match-end 5))))
  15139. (t 'day))))
  15140. ans))
  15141. (defun org-toggle-timestamp-type ()
  15142. "Toggle the type (<active> or [inactive]) of a time stamp."
  15143. (interactive)
  15144. (when (org-at-timestamp-p t)
  15145. (let ((beg (match-beginning 0)) (end (match-end 0))
  15146. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15147. (save-excursion
  15148. (goto-char beg)
  15149. (while (re-search-forward "[][<>]" end t)
  15150. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15151. t t)))
  15152. (message "Timestamp is now %sactive"
  15153. (if (equal (char-after beg) ?<) "" "in")))))
  15154. (defvar org-clock-history) ; defined in org-clock.el
  15155. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15156. (defun org-timestamp-change (n &optional what updown)
  15157. "Change the date in the time stamp at point.
  15158. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15159. `year', `minute', `second'. If WHAT is not given, the cursor position
  15160. in the timestamp determines what will be changed."
  15161. (let ((origin (point)) origin-cat
  15162. with-hm inactive
  15163. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15164. org-ts-what
  15165. extra rem
  15166. ts time time0 fixnext clrgx)
  15167. (if (not (org-at-timestamp-p t))
  15168. (error "Not at a timestamp"))
  15169. (if (and (not what) (eq org-ts-what 'bracket))
  15170. (org-toggle-timestamp-type)
  15171. ;; Point isn't on brackets. Remember the part of the time-stamp
  15172. ;; the point was in. Indeed, size of time-stamps may change,
  15173. ;; but point must be kept in the same category nonetheless.
  15174. (setq origin-cat org-ts-what)
  15175. (if (and (not what) (not (eq org-ts-what 'day))
  15176. org-display-custom-times
  15177. (get-text-property (point) 'display)
  15178. (not (get-text-property (1- (point)) 'display)))
  15179. (setq org-ts-what 'day))
  15180. (setq org-ts-what (or what org-ts-what)
  15181. inactive (= (char-after (match-beginning 0)) ?\[)
  15182. ts (match-string 0))
  15183. (replace-match "")
  15184. (if (string-match
  15185. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15186. ts)
  15187. (setq extra (match-string 1 ts)))
  15188. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15189. (setq with-hm t))
  15190. (setq time0 (org-parse-time-string ts))
  15191. (when (and updown
  15192. (eq org-ts-what 'minute)
  15193. (not current-prefix-arg))
  15194. ;; This looks like s-up and s-down. Change by one rounding step.
  15195. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15196. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15197. (setcar (cdr time0) (+ (nth 1 time0)
  15198. (if (> n 0) (- rem) (- dm rem))))))
  15199. (setq time
  15200. (encode-time (or (car time0) 0)
  15201. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15202. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15203. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15204. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15205. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15206. (nthcdr 6 time0)))
  15207. (when (and (member org-ts-what '(hour minute))
  15208. extra
  15209. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15210. (setq extra (org-modify-ts-extra
  15211. extra
  15212. (if (eq org-ts-what 'hour) 2 5)
  15213. n dm)))
  15214. (when (integerp org-ts-what)
  15215. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15216. (if (eq what 'calendar)
  15217. (let ((cal-date (org-get-date-from-calendar)))
  15218. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15219. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15220. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15221. (setcar time0 (or (car time0) 0))
  15222. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15223. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15224. (setq time (apply 'encode-time time0))))
  15225. ;; Insert the new time-stamp, and ensure point stays in the same
  15226. ;; category as before (i.e. not after the last position in that
  15227. ;; category).
  15228. (let ((pos (point)))
  15229. ;; Stay before inserted string. `save-excursion' is of no use.
  15230. (setq org-last-changed-timestamp
  15231. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15232. (goto-char pos))
  15233. (save-match-data
  15234. (looking-at org-ts-regexp3)
  15235. (goto-char (cond
  15236. ;; `day' category ends before `hour' if any, or at
  15237. ;; the end of the day name.
  15238. ((eq origin-cat 'day)
  15239. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15240. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15241. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15242. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15243. ;; `year' and `month' have both fixed size: point
  15244. ;; couldn't have moved into another part.
  15245. (t origin))))
  15246. ;; Update clock if on a CLOCK line.
  15247. (org-clock-update-time-maybe)
  15248. ;; Maybe adjust the closest clock in `org-clock-history'
  15249. (when org-clock-adjust-closest
  15250. (if (not (and (org-at-clock-log-p)
  15251. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15252. org-clock-history))))))
  15253. (message "No clock to adjust")
  15254. (cond ((save-excursion ; fix previous clock?
  15255. (re-search-backward org-ts-regexp0 nil t)
  15256. (org-looking-back (concat org-clock-string " \\[")))
  15257. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15258. ((save-excursion ; fix next clock?
  15259. (re-search-backward org-ts-regexp0 nil t)
  15260. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15261. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15262. (save-window-excursion
  15263. ;; Find closest clock to point, adjust the previous/next one in history
  15264. (let* ((p (save-excursion (org-back-to-heading t)))
  15265. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15266. (clfixnth
  15267. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15268. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15269. (if (not clfixpos)
  15270. (message "No clock to adjust")
  15271. (save-excursion
  15272. (org-goto-marker-or-bmk clfixpos)
  15273. (org-show-subtree)
  15274. (when (re-search-forward clrgx nil t)
  15275. (goto-char (match-beginning 1))
  15276. (let (org-clock-adjust-closest)
  15277. (org-timestamp-change n org-ts-what updown))
  15278. (message "Clock adjusted in %s for heading: %s"
  15279. (file-name-nondirectory (buffer-file-name))
  15280. (org-get-heading t t)))))))))
  15281. ;; Try to recenter the calendar window, if any.
  15282. (if (and org-calendar-follow-timestamp-change
  15283. (get-buffer-window "*Calendar*" t)
  15284. (memq org-ts-what '(day month year)))
  15285. (org-recenter-calendar (time-to-days time))))))
  15286. (defun org-modify-ts-extra (s pos n dm)
  15287. "Change the different parts of the lead-time and repeat fields in timestamp."
  15288. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15289. ng h m new rem)
  15290. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15291. (cond
  15292. ((or (org-pos-in-match-range pos 2)
  15293. (org-pos-in-match-range pos 3))
  15294. (setq m (string-to-number (match-string 3 s))
  15295. h (string-to-number (match-string 2 s)))
  15296. (if (org-pos-in-match-range pos 2)
  15297. (setq h (+ h n))
  15298. (setq n (* dm (org-no-warnings (signum n))))
  15299. (when (not (= 0 (setq rem (% m dm))))
  15300. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15301. (setq m (+ m n)))
  15302. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15303. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15304. (setq h (min 24 (max 0 h)))
  15305. (setq ng 1 new (format "-%02d:%02d" h m)))
  15306. ((org-pos-in-match-range pos 6)
  15307. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15308. ((org-pos-in-match-range pos 5)
  15309. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15310. ((org-pos-in-match-range pos 9)
  15311. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15312. ((org-pos-in-match-range pos 8)
  15313. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15314. (when ng
  15315. (setq s (concat
  15316. (substring s 0 (match-beginning ng))
  15317. new
  15318. (substring s (match-end ng))))))
  15319. s))
  15320. (defun org-recenter-calendar (date)
  15321. "If the calendar is visible, recenter it to DATE."
  15322. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15323. (when cwin
  15324. (let ((calendar-move-hook nil))
  15325. (with-selected-window cwin
  15326. (calendar-goto-date (if (listp date) date
  15327. (calendar-gregorian-from-absolute date))))))))
  15328. (defun org-goto-calendar (&optional arg)
  15329. "Go to the Emacs calendar at the current date.
  15330. If there is a time stamp in the current line, go to that date.
  15331. A prefix ARG can be used to force the current date."
  15332. (interactive "P")
  15333. (let ((tsr org-ts-regexp) diff
  15334. (calendar-move-hook nil)
  15335. (calendar-view-holidays-initially-flag nil)
  15336. (calendar-view-diary-initially-flag nil))
  15337. (if (or (org-at-timestamp-p)
  15338. (save-excursion
  15339. (beginning-of-line 1)
  15340. (looking-at (concat ".*" tsr))))
  15341. (let ((d1 (time-to-days (current-time)))
  15342. (d2 (time-to-days
  15343. (org-time-string-to-time (match-string 1)))))
  15344. (setq diff (- d2 d1))))
  15345. (calendar)
  15346. (calendar-goto-today)
  15347. (if (and diff (not arg)) (calendar-forward-day diff))))
  15348. (defun org-get-date-from-calendar ()
  15349. "Return a list (month day year) of date at point in calendar."
  15350. (with-current-buffer "*Calendar*"
  15351. (save-match-data
  15352. (calendar-cursor-to-date))))
  15353. (defun org-date-from-calendar ()
  15354. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15355. If there is already a time stamp at the cursor position, update it."
  15356. (interactive)
  15357. (if (org-at-timestamp-p t)
  15358. (org-timestamp-change 0 'calendar)
  15359. (let ((cal-date (org-get-date-from-calendar)))
  15360. (org-insert-time-stamp
  15361. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15362. (defun org-minutes-to-hh:mm-string (m)
  15363. "Compute H:MM from a number of minutes."
  15364. (let ((h (/ m 60)))
  15365. (setq m (- m (* 60 h)))
  15366. (format org-time-clocksum-format h m)))
  15367. (defun org-hh:mm-string-to-minutes (s)
  15368. "Convert a string H:MM to a number of minutes.
  15369. If the string is just a number, interpret it as minutes.
  15370. In fact, the first hh:mm or number in the string will be taken,
  15371. there can be extra stuff in the string.
  15372. If no number is found, the return value is 0."
  15373. (cond
  15374. ((integerp s) s)
  15375. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15376. (+ (* (string-to-number (match-string 1 s)) 60)
  15377. (string-to-number (match-string 2 s))))
  15378. ((string-match "\\([0-9]+\\)" s)
  15379. (string-to-number (match-string 1 s)))
  15380. (t 0)))
  15381. (defcustom org-effort-durations
  15382. `(("h" . 60)
  15383. ("d" . ,(* 60 8))
  15384. ("w" . ,(* 60 8 5))
  15385. ("m" . ,(* 60 8 5 4))
  15386. ("y" . ,(* 60 8 5 40)))
  15387. "Conversion factor to minutes for an effort modifier.
  15388. Each entry has the form (MODIFIER . MINUTES).
  15389. In an effort string, a number followed by MODIFIER is multiplied
  15390. by the specified number of MINUTES to obtain an effort in
  15391. minutes.
  15392. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15393. effort string \"2hours\" is equivalent to 120 minutes."
  15394. :group 'org-agenda
  15395. :version "24.1"
  15396. :type '(alist :key-type (string :tag "Modifier")
  15397. :value-type (number :tag "Minutes")))
  15398. (defcustom org-image-actual-width t
  15399. "Should we use the actual width of images when inlining them?
  15400. When set to `t', always use the image width.
  15401. When set to a number, use imagemagick (when available) to set
  15402. the image's width to this value.
  15403. When set to a number in a list, try to get the width from the
  15404. #+ATTR.* keyword if it matches a width specification like
  15405. width=\"[0-9]+\", and fall back on that number if none is found.
  15406. When set to nil, try to get the width from an #+ATTR.* keyword
  15407. and fall back on the original width if none is found.
  15408. This requires Emacs >= 24.1, build with imagemagick support."
  15409. :group 'org-appearance
  15410. :version "24.3"
  15411. :type '(choice
  15412. (const :tag "Use the image width" t)
  15413. (integer :tag "Use a number of pixels")
  15414. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  15415. (const :tag "Use #+ATTR* or don't resize" nil)))
  15416. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15417. "Convert a duration string S to minutes.
  15418. A bare number is interpreted as minutes, modifiers can be set by
  15419. customizing `org-effort-durations' (which see).
  15420. Entries containing a colon are interpreted as H:MM by
  15421. `org-hh:mm-string-to-minutes'."
  15422. (let ((result 0)
  15423. (re (concat "\\([0-9.]+\\) *\\("
  15424. (regexp-opt (mapcar 'car org-effort-durations))
  15425. "\\)")))
  15426. (while (string-match re s)
  15427. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15428. (string-to-number (match-string 1 s))))
  15429. (setq s (replace-match "" nil t s)))
  15430. (setq result (floor result))
  15431. (incf result (org-hh:mm-string-to-minutes s))
  15432. (if output-to-string (number-to-string result) result)))
  15433. ;;;; Files
  15434. (defun org-save-all-org-buffers ()
  15435. "Save all Org-mode buffers without user confirmation."
  15436. (interactive)
  15437. (message "Saving all Org-mode buffers...")
  15438. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15439. (when (featurep 'org-id) (org-id-locations-save))
  15440. (message "Saving all Org-mode buffers... done"))
  15441. (defun org-revert-all-org-buffers ()
  15442. "Revert all Org-mode buffers.
  15443. Prompt for confirmation when there are unsaved changes.
  15444. Be sure you know what you are doing before letting this function
  15445. overwrite your changes.
  15446. This function is useful in a setup where one tracks org files
  15447. with a version control system, to revert on one machine after pulling
  15448. changes from another. I believe the procedure must be like this:
  15449. 1. M-x org-save-all-org-buffers
  15450. 2. Pull changes from the other machine, resolve conflicts
  15451. 3. M-x org-revert-all-org-buffers"
  15452. (interactive)
  15453. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15454. (error "Abort"))
  15455. (save-excursion
  15456. (save-window-excursion
  15457. (mapc
  15458. (lambda (b)
  15459. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15460. (with-current-buffer b buffer-file-name))
  15461. (org-pop-to-buffer-same-window b)
  15462. (revert-buffer t 'no-confirm)))
  15463. (buffer-list))
  15464. (when (and (featurep 'org-id) org-id-track-globally)
  15465. (org-id-locations-load)))))
  15466. ;;;; Agenda files
  15467. ;;;###autoload
  15468. (defun org-switchb (&optional arg)
  15469. "Switch between Org buffers.
  15470. With one prefix argument, restrict available buffers to files.
  15471. With two prefix arguments, restrict available buffers to agenda files.
  15472. Defaults to `iswitchb' for buffer name completion.
  15473. Set `org-completion-use-ido' to make it use ido instead."
  15474. (interactive "P")
  15475. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15476. ((equal arg '(16)) (org-buffer-list 'agenda))
  15477. (t (org-buffer-list))))
  15478. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15479. (org-completion-use-ido org-completion-use-ido))
  15480. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15481. (setq org-completion-use-iswitchb t))
  15482. (org-pop-to-buffer-same-window
  15483. (org-icompleting-read "Org buffer: "
  15484. (mapcar 'list (mapcar 'buffer-name blist))
  15485. nil t))))
  15486. ;;; Define some older names previously used for this functionality
  15487. ;;;###autoload
  15488. (defalias 'org-ido-switchb 'org-switchb)
  15489. ;;;###autoload
  15490. (defalias 'org-iswitchb 'org-switchb)
  15491. (defun org-buffer-list (&optional predicate exclude-tmp)
  15492. "Return a list of Org buffers.
  15493. PREDICATE can be `export', `files' or `agenda'.
  15494. export restrict the list to Export buffers.
  15495. files restrict the list to buffers visiting Org files.
  15496. agenda restrict the list to buffers visiting agenda files.
  15497. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15498. (let* ((bfn nil)
  15499. (agenda-files (and (eq predicate 'agenda)
  15500. (mapcar 'file-truename (org-agenda-files t))))
  15501. (filter
  15502. (cond
  15503. ((eq predicate 'files)
  15504. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15505. ((eq predicate 'export)
  15506. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15507. ((eq predicate 'agenda)
  15508. (lambda (b)
  15509. (with-current-buffer b
  15510. (and (derived-mode-p 'org-mode)
  15511. (setq bfn (buffer-file-name b))
  15512. (member (file-truename bfn) agenda-files)))))
  15513. (t (lambda (b) (with-current-buffer b
  15514. (or (derived-mode-p 'org-mode)
  15515. (string-match "\*Org .*Export"
  15516. (buffer-name b)))))))))
  15517. (delq nil
  15518. (mapcar
  15519. (lambda(b)
  15520. (if (and (funcall filter b)
  15521. (or (not exclude-tmp)
  15522. (not (string-match "tmp" (buffer-name b)))))
  15523. b
  15524. nil))
  15525. (buffer-list)))))
  15526. (defun org-agenda-files (&optional unrestricted archives)
  15527. "Get the list of agenda files.
  15528. Optional UNRESTRICTED means return the full list even if a restriction
  15529. is currently in place.
  15530. When ARCHIVES is t, include all archive files that are really being
  15531. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15532. `org-agenda-archives-mode' is t."
  15533. (let ((files
  15534. (cond
  15535. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15536. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15537. ((listp org-agenda-files) org-agenda-files)
  15538. (t (error "Invalid value of `org-agenda-files'")))))
  15539. (setq files (apply 'append
  15540. (mapcar (lambda (f)
  15541. (if (file-directory-p f)
  15542. (directory-files
  15543. f t org-agenda-file-regexp)
  15544. (list f)))
  15545. files)))
  15546. (when org-agenda-skip-unavailable-files
  15547. (setq files (delq nil
  15548. (mapcar (function
  15549. (lambda (file)
  15550. (and (file-readable-p file) file)))
  15551. files))))
  15552. (when (or (eq archives t)
  15553. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15554. (setq files (org-add-archive-files files)))
  15555. files))
  15556. (defun org-agenda-file-p (&optional file)
  15557. "Return non-nil, if FILE is an agenda file.
  15558. If FILE is omitted, use the file associated with the current
  15559. buffer."
  15560. (member (or file (buffer-file-name))
  15561. (org-agenda-files t)))
  15562. (defun org-edit-agenda-file-list ()
  15563. "Edit the list of agenda files.
  15564. Depending on setup, this either uses customize to edit the variable
  15565. `org-agenda-files', or it visits the file that is holding the list. In the
  15566. latter case, the buffer is set up in a way that saving it automatically kills
  15567. the buffer and restores the previous window configuration."
  15568. (interactive)
  15569. (if (stringp org-agenda-files)
  15570. (let ((cw (current-window-configuration)))
  15571. (find-file org-agenda-files)
  15572. (org-set-local 'org-window-configuration cw)
  15573. (org-add-hook 'after-save-hook
  15574. (lambda ()
  15575. (set-window-configuration
  15576. (prog1 org-window-configuration
  15577. (kill-buffer (current-buffer))))
  15578. (org-install-agenda-files-menu)
  15579. (message "New agenda file list installed"))
  15580. nil 'local)
  15581. (message "%s" (substitute-command-keys
  15582. "Edit list and finish with \\[save-buffer]")))
  15583. (customize-variable 'org-agenda-files)))
  15584. (defun org-store-new-agenda-file-list (list)
  15585. "Set new value for the agenda file list and save it correctly."
  15586. (if (stringp org-agenda-files)
  15587. (let ((fe (org-read-agenda-file-list t)) b u)
  15588. (while (setq b (find-buffer-visiting org-agenda-files))
  15589. (kill-buffer b))
  15590. (with-temp-file org-agenda-files
  15591. (insert
  15592. (mapconcat
  15593. (lambda (f) ;; Keep un-expanded entries.
  15594. (if (setq u (assoc f fe))
  15595. (cdr u)
  15596. f))
  15597. list "\n")
  15598. "\n")))
  15599. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15600. (org-insert-mode-line-in-empty-file nil))
  15601. (setq org-agenda-files list)
  15602. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15603. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15604. "Read the list of agenda files from a file.
  15605. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15606. filenames, used by `org-store-new-agenda-file-list' to write back
  15607. un-expanded file names."
  15608. (when (file-directory-p org-agenda-files)
  15609. (error "`org-agenda-files' cannot be a single directory"))
  15610. (when (stringp org-agenda-files)
  15611. (with-temp-buffer
  15612. (insert-file-contents org-agenda-files)
  15613. (mapcar
  15614. (lambda (f)
  15615. (let ((e (expand-file-name (substitute-in-file-name f)
  15616. org-directory)))
  15617. (if pair-with-expansion
  15618. (cons e f)
  15619. e)))
  15620. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15621. ;;;###autoload
  15622. (defun org-cycle-agenda-files ()
  15623. "Cycle through the files in `org-agenda-files'.
  15624. If the current buffer visits an agenda file, find the next one in the list.
  15625. If the current buffer does not, find the first agenda file."
  15626. (interactive)
  15627. (let* ((fs (org-agenda-files t))
  15628. (files (append fs (list (car fs))))
  15629. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15630. file)
  15631. (unless files (error "No agenda files"))
  15632. (catch 'exit
  15633. (while (setq file (pop files))
  15634. (if (equal (file-truename file) tcf)
  15635. (when (car files)
  15636. (find-file (car files))
  15637. (throw 'exit t))))
  15638. (find-file (car fs)))
  15639. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15640. (defun org-agenda-file-to-front (&optional to-end)
  15641. "Move/add the current file to the top of the agenda file list.
  15642. If the file is not present in the list, it is added to the front. If it is
  15643. present, it is moved there. With optional argument TO-END, add/move to the
  15644. end of the list."
  15645. (interactive "P")
  15646. (let ((org-agenda-skip-unavailable-files nil)
  15647. (file-alist (mapcar (lambda (x)
  15648. (cons (file-truename x) x))
  15649. (org-agenda-files t)))
  15650. (ctf (file-truename buffer-file-name))
  15651. x had)
  15652. (setq x (assoc ctf file-alist) had x)
  15653. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15654. (if to-end
  15655. (setq file-alist (append (delq x file-alist) (list x)))
  15656. (setq file-alist (cons x (delq x file-alist))))
  15657. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15658. (org-install-agenda-files-menu)
  15659. (message "File %s to %s of agenda file list"
  15660. (if had "moved" "added") (if to-end "end" "front"))))
  15661. (defun org-remove-file (&optional file)
  15662. "Remove current file from the list of files in variable `org-agenda-files'.
  15663. These are the files which are being checked for agenda entries.
  15664. Optional argument FILE means use this file instead of the current."
  15665. (interactive)
  15666. (let* ((org-agenda-skip-unavailable-files nil)
  15667. (file (or file buffer-file-name))
  15668. (true-file (file-truename file))
  15669. (afile (abbreviate-file-name file))
  15670. (files (delq nil (mapcar
  15671. (lambda (x)
  15672. (if (equal true-file
  15673. (file-truename x))
  15674. nil x))
  15675. (org-agenda-files t)))))
  15676. (if (not (= (length files) (length (org-agenda-files t))))
  15677. (progn
  15678. (org-store-new-agenda-file-list files)
  15679. (org-install-agenda-files-menu)
  15680. (message "Removed file: %s" afile))
  15681. (message "File was not in list: %s (not removed)" afile))))
  15682. (defun org-file-menu-entry (file)
  15683. (vector file (list 'find-file file) t))
  15684. (defun org-check-agenda-file (file)
  15685. "Make sure FILE exists. If not, ask user what to do."
  15686. (when (not (file-exists-p file))
  15687. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  15688. (abbreviate-file-name file))
  15689. (let ((r (downcase (read-char-exclusive))))
  15690. (cond
  15691. ((equal r ?r)
  15692. (org-remove-file file)
  15693. (throw 'nextfile t))
  15694. (t (error "Abort"))))))
  15695. (defun org-get-agenda-file-buffer (file)
  15696. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15697. it to the list of buffers which might be released later."
  15698. (let ((buf (org-find-base-buffer-visiting file)))
  15699. (if buf
  15700. buf ; just return it
  15701. ;; Make a new buffer and remember it
  15702. (setq buf (find-file-noselect file))
  15703. (if buf (push buf org-agenda-new-buffers))
  15704. buf)))
  15705. (defun org-release-buffers (blist)
  15706. "Release all buffers in list, asking the user for confirmation when needed.
  15707. When a buffer is unmodified, it is just killed. When modified, it is saved
  15708. \(if the user agrees) and then killed."
  15709. (let (buf file)
  15710. (while (setq buf (pop blist))
  15711. (setq file (buffer-file-name buf))
  15712. (when (and (buffer-modified-p buf)
  15713. file
  15714. (y-or-n-p (format "Save file %s? " file)))
  15715. (with-current-buffer buf (save-buffer)))
  15716. (kill-buffer buf))))
  15717. (defun org-agenda-prepare-buffers (files)
  15718. "Create buffers for all agenda files, protect archived trees and comments."
  15719. (interactive)
  15720. (let ((pa '(:org-archived t))
  15721. (pc '(:org-comment t))
  15722. (pall '(:org-archived t :org-comment t))
  15723. (inhibit-read-only t)
  15724. (rea (concat ":" org-archive-tag ":"))
  15725. bmp file re)
  15726. (save-excursion
  15727. (save-restriction
  15728. (while (setq file (pop files))
  15729. (catch 'nextfile
  15730. (if (bufferp file)
  15731. (set-buffer file)
  15732. (org-check-agenda-file file)
  15733. (set-buffer (org-get-agenda-file-buffer file)))
  15734. (widen)
  15735. (setq bmp (buffer-modified-p))
  15736. (org-refresh-category-properties)
  15737. (setq org-todo-keywords-for-agenda
  15738. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15739. (setq org-done-keywords-for-agenda
  15740. (append org-done-keywords-for-agenda org-done-keywords))
  15741. (setq org-todo-keyword-alist-for-agenda
  15742. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15743. (setq org-drawers-for-agenda
  15744. (append org-drawers-for-agenda org-drawers))
  15745. (setq org-tag-alist-for-agenda
  15746. (append org-tag-alist-for-agenda org-tag-alist))
  15747. (save-excursion
  15748. (remove-text-properties (point-min) (point-max) pall)
  15749. (when org-agenda-skip-archived-trees
  15750. (goto-char (point-min))
  15751. (while (re-search-forward rea nil t)
  15752. (if (org-at-heading-p t)
  15753. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15754. (goto-char (point-min))
  15755. (setq re (format org-heading-keyword-regexp-format
  15756. org-comment-string))
  15757. (while (re-search-forward re nil t)
  15758. (add-text-properties
  15759. (match-beginning 0) (org-end-of-subtree t) pc)))
  15760. (set-buffer-modified-p bmp)))))
  15761. (setq org-todo-keywords-for-agenda
  15762. (org-uniquify org-todo-keywords-for-agenda))
  15763. (setq org-todo-keyword-alist-for-agenda
  15764. (org-uniquify org-todo-keyword-alist-for-agenda)
  15765. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15766. ;;;; Embedded LaTeX
  15767. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15768. "Keymap for the minor `org-cdlatex-mode'.")
  15769. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15770. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15771. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15772. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15773. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15774. (defvar org-cdlatex-texmathp-advice-is-done nil
  15775. "Flag remembering if we have applied the advice to texmathp already.")
  15776. (define-minor-mode org-cdlatex-mode
  15777. "Toggle the minor `org-cdlatex-mode'.
  15778. This mode supports entering LaTeX environment and math in LaTeX fragments
  15779. in Org-mode.
  15780. \\{org-cdlatex-mode-map}"
  15781. nil " OCDL" nil
  15782. (when org-cdlatex-mode
  15783. (require 'cdlatex)
  15784. (run-hooks 'cdlatex-mode-hook)
  15785. (cdlatex-compute-tables))
  15786. (unless org-cdlatex-texmathp-advice-is-done
  15787. (setq org-cdlatex-texmathp-advice-is-done t)
  15788. (defadvice texmathp (around org-math-always-on activate)
  15789. "Always return t in org-mode buffers.
  15790. This is because we want to insert math symbols without dollars even outside
  15791. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15792. an embedded LaTeX fragment, let texmathp do its job.
  15793. \\[org-cdlatex-mode-map]"
  15794. (interactive)
  15795. (let (p)
  15796. (cond
  15797. ((not (derived-mode-p 'org-mode)) ad-do-it)
  15798. ((eq this-command 'cdlatex-math-symbol)
  15799. (setq ad-return-value t
  15800. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15801. (t
  15802. (let ((p (org-inside-LaTeX-fragment-p)))
  15803. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15804. (setq ad-return-value t
  15805. texmathp-why '("Org-mode embedded math" . 0))
  15806. (if p ad-do-it)))))))))
  15807. (defun turn-on-org-cdlatex ()
  15808. "Unconditionally turn on `org-cdlatex-mode'."
  15809. (org-cdlatex-mode 1))
  15810. (defun org-inside-LaTeX-fragment-p ()
  15811. "Test if point is inside a LaTeX fragment.
  15812. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15813. sequence appearing also before point.
  15814. Even though the matchers for math are configurable, this function assumes
  15815. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15816. delimiters are skipped when they have been removed by customization.
  15817. The return value is nil, or a cons cell with the delimiter and the
  15818. position of this delimiter.
  15819. This function does a reasonably good job, but can locally be fooled by
  15820. for example currency specifications. For example it will assume being in
  15821. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15822. fragments that are properly closed, but during editing, we have to live
  15823. with the uncertainty caused by missing closing delimiters. This function
  15824. looks only before point, not after."
  15825. (catch 'exit
  15826. (let ((pos (point))
  15827. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15828. (lim (progn
  15829. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15830. (point)))
  15831. dd-on str (start 0) m re)
  15832. (goto-char pos)
  15833. (when dodollar
  15834. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15835. re (nth 1 (assoc "$" org-latex-regexps)))
  15836. (while (string-match re str start)
  15837. (cond
  15838. ((= (match-end 0) (length str))
  15839. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15840. ((= (match-end 0) (- (length str) 5))
  15841. (throw 'exit nil))
  15842. (t (setq start (match-end 0))))))
  15843. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15844. (goto-char pos)
  15845. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15846. (and (match-beginning 2) (throw 'exit nil))
  15847. ;; count $$
  15848. (while (re-search-backward "\\$\\$" lim t)
  15849. (setq dd-on (not dd-on)))
  15850. (goto-char pos)
  15851. (if dd-on (cons "$$" m))))))
  15852. (defun org-inside-latex-macro-p ()
  15853. "Is point inside a LaTeX macro or its arguments?"
  15854. (save-match-data
  15855. (org-in-regexp
  15856. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15857. (defun org-try-cdlatex-tab ()
  15858. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15859. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15860. - inside a LaTeX fragment, or
  15861. - after the first word in a line, where an abbreviation expansion could
  15862. insert a LaTeX environment."
  15863. (when org-cdlatex-mode
  15864. (cond
  15865. ;; Before any word on the line: No expansion possible.
  15866. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15867. ;; Just after first word on the line: Expand it. Make sure it
  15868. ;; cannot happen on headlines, though.
  15869. ((save-excursion
  15870. (skip-chars-backward "a-zA-Z0-9*")
  15871. (skip-chars-backward " \t")
  15872. (and (bolp) (not (org-at-heading-p))))
  15873. (cdlatex-tab) t)
  15874. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15875. (defun org-cdlatex-underscore-caret (&optional arg)
  15876. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15877. Revert to the normal definition outside of these fragments."
  15878. (interactive "P")
  15879. (if (org-inside-LaTeX-fragment-p)
  15880. (call-interactively 'cdlatex-sub-superscript)
  15881. (let (org-cdlatex-mode)
  15882. (call-interactively (key-binding (vector last-input-event))))))
  15883. (defun org-cdlatex-math-modify (&optional arg)
  15884. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15885. Revert to the normal definition outside of these fragments."
  15886. (interactive "P")
  15887. (if (org-inside-LaTeX-fragment-p)
  15888. (call-interactively 'cdlatex-math-modify)
  15889. (let (org-cdlatex-mode)
  15890. (call-interactively (key-binding (vector last-input-event))))))
  15891. (defvar org-latex-fragment-image-overlays nil
  15892. "List of overlays carrying the images of latex fragments.")
  15893. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15894. (defun org-remove-latex-fragment-image-overlays ()
  15895. "Remove all overlays with LaTeX fragment images in current buffer."
  15896. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15897. (setq org-latex-fragment-image-overlays nil))
  15898. (defun org-preview-latex-fragment (&optional subtree)
  15899. "Preview the LaTeX fragment at point, or all locally or globally.
  15900. If the cursor is in a LaTeX fragment, create the image and overlay
  15901. it over the source code. If there is no fragment at point, display
  15902. all fragments in the current text, from one headline to the next. With
  15903. prefix SUBTREE, display all fragments in the current subtree. With a
  15904. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15905. the cursor is before the first headline,
  15906. display all fragments in the buffer.
  15907. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15908. (interactive "P")
  15909. (unless buffer-file-name
  15910. (error "Can't preview LaTeX fragment in a non-file buffer"))
  15911. (org-remove-latex-fragment-image-overlays)
  15912. (save-excursion
  15913. (save-restriction
  15914. (let (beg end at msg)
  15915. (cond
  15916. ((or (equal subtree '(16))
  15917. (not (save-excursion
  15918. (re-search-backward org-outline-regexp-bol nil t))))
  15919. (setq beg (point-min) end (point-max)
  15920. msg "Creating images for buffer...%s"))
  15921. ((equal subtree '(4))
  15922. (org-back-to-heading)
  15923. (setq beg (point) end (org-end-of-subtree t)
  15924. msg "Creating images for subtree...%s"))
  15925. (t
  15926. (if (setq at (org-inside-LaTeX-fragment-p))
  15927. (goto-char (max (point-min) (- (cdr at) 2)))
  15928. (org-back-to-heading))
  15929. (setq beg (point) end (progn (outline-next-heading) (point))
  15930. msg (if at "Creating image...%s"
  15931. "Creating images for entry...%s"))))
  15932. (message msg "")
  15933. (narrow-to-region beg end)
  15934. (goto-char beg)
  15935. (org-format-latex
  15936. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  15937. (file-name-nondirectory
  15938. buffer-file-name)))
  15939. default-directory 'overlays msg at 'forbuffer
  15940. org-latex-create-formula-image-program)
  15941. (message msg "done. Use `C-c C-c' to remove images.")))))
  15942. (defvar org-latex-regexps
  15943. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15944. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15945. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15946. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15947. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15948. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15949. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15950. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15951. "Regular expressions for matching embedded LaTeX.")
  15952. (defvar org-export-have-math nil) ;; dynamic scoping
  15953. (defun org-format-latex (prefix &optional dir overlays msg at
  15954. forbuffer processing-type)
  15955. "Replace LaTeX fragments with links to an image, and produce images.
  15956. Some of the options can be changed using the variable
  15957. `org-format-latex-options'."
  15958. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15959. (let* ((prefixnodir (file-name-nondirectory prefix))
  15960. (absprefix (expand-file-name prefix dir))
  15961. (todir (file-name-directory absprefix))
  15962. (opt org-format-latex-options)
  15963. (optnew org-format-latex-options)
  15964. (matchers (plist-get opt :matchers))
  15965. (re-list org-latex-regexps)
  15966. (org-format-latex-header-extra
  15967. (plist-get (org-infile-export-plist) :latex-header-extra))
  15968. (cnt 0) txt hash link beg end re e checkdir
  15969. string
  15970. m n block-type block linkfile movefile ov)
  15971. ;; Check the different regular expressions
  15972. (while (setq e (pop re-list))
  15973. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15974. block (if block-type "\n\n" ""))
  15975. (when (member m matchers)
  15976. (goto-char (point-min))
  15977. (while (re-search-forward re nil t)
  15978. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15979. (not (get-text-property (match-beginning n)
  15980. 'org-protected))
  15981. (or (not overlays)
  15982. (not (eq (get-char-property (match-beginning n)
  15983. 'org-overlay-type)
  15984. 'org-latex-overlay))))
  15985. (setq org-export-have-math t)
  15986. (cond
  15987. ((eq processing-type 'verbatim)
  15988. ;; Leave the text verbatim, just protect it
  15989. (add-text-properties (match-beginning n) (match-end n)
  15990. '(org-protected t)))
  15991. ((eq processing-type 'mathjax)
  15992. ;; Prepare for MathJax processing
  15993. (setq string (match-string n))
  15994. (if (member m '("$" "$1"))
  15995. (save-excursion
  15996. (delete-region (match-beginning n) (match-end n))
  15997. (goto-char (match-beginning n))
  15998. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15999. "\\)")
  16000. '(org-protected t))))
  16001. (add-text-properties (match-beginning n) (match-end n)
  16002. '(org-protected t))))
  16003. ((or (eq processing-type 'dvipng)
  16004. (eq processing-type 'imagemagick))
  16005. ;; Process to an image
  16006. (setq txt (match-string n)
  16007. beg (match-beginning n) end (match-end n)
  16008. cnt (1+ cnt))
  16009. (let ((face (face-at-point))
  16010. (fg (plist-get opt :foreground))
  16011. (bg (plist-get opt :background))
  16012. print-length print-level) ; make sure full list is printed
  16013. (when forbuffer
  16014. ; Get the colors from the face at point
  16015. (goto-char beg)
  16016. (when (eq fg 'auto)
  16017. (setq fg (face-attribute face :foreground nil 'default)))
  16018. (when (eq bg 'auto)
  16019. (setq bg (face-attribute face :background nil 'default)))
  16020. (setq optnew (copy-sequence opt))
  16021. (plist-put optnew :foreground fg)
  16022. (plist-put optnew :background bg))
  16023. (setq hash (sha1 (prin1-to-string
  16024. (list org-format-latex-header
  16025. org-format-latex-header-extra
  16026. org-export-latex-default-packages-alist
  16027. org-export-latex-packages-alist
  16028. org-format-latex-options
  16029. forbuffer txt fg bg)))
  16030. linkfile (format "%s_%s.png" prefix hash)
  16031. movefile (format "%s_%s.png" absprefix hash)))
  16032. (setq link (concat block "[[file:" linkfile "]]" block))
  16033. (if msg (message msg cnt))
  16034. (goto-char beg)
  16035. (unless checkdir ; make sure the directory exists
  16036. (setq checkdir t)
  16037. (or (file-directory-p todir) (make-directory todir t)))
  16038. (org-create-formula-image
  16039. txt movefile optnew forbuffer processing-type)
  16040. (if overlays
  16041. (progn
  16042. (mapc (lambda (o)
  16043. (if (eq (overlay-get o 'org-overlay-type)
  16044. 'org-latex-overlay)
  16045. (delete-overlay o)))
  16046. (overlays-in beg end))
  16047. (setq ov (make-overlay beg end))
  16048. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16049. (if (featurep 'xemacs)
  16050. (progn
  16051. (overlay-put ov 'invisible t)
  16052. (overlay-put
  16053. ov 'end-glyph
  16054. (make-glyph (vector 'png :file movefile))))
  16055. (overlay-put
  16056. ov 'display
  16057. (list 'image :type 'png :file movefile :ascent 'center)))
  16058. (push ov org-latex-fragment-image-overlays)
  16059. (goto-char end))
  16060. (delete-region beg end)
  16061. (insert (org-add-props link
  16062. (list 'org-latex-src
  16063. (replace-regexp-in-string
  16064. "\"" "" txt)
  16065. 'org-latex-src-embed-type
  16066. (if block-type 'paragraph 'character))))))
  16067. ((eq processing-type 'mathml)
  16068. ;; Process to MathML
  16069. (unless (save-match-data (org-format-latex-mathml-available-p))
  16070. (error "LaTeX to MathML converter not configured"))
  16071. (setq txt (match-string n)
  16072. beg (match-beginning n) end (match-end n)
  16073. cnt (1+ cnt))
  16074. (if msg (message msg cnt))
  16075. (goto-char beg)
  16076. (delete-region beg end)
  16077. (insert (org-format-latex-as-mathml
  16078. txt block-type prefix dir)))
  16079. (t
  16080. (error "Unknown conversion type %s for latex fragments"
  16081. processing-type)))))))))
  16082. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16083. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16084. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16085. sucessful, return the portion between \"<math...> </math>\"
  16086. elements otherwise return nil. When MATHML-FILE is specified,
  16087. write the results in to that file. When invoked as an
  16088. interactive command, prompt for LATEX-FRAG, with initial value
  16089. set to the current active region and echo the results for user
  16090. inspection."
  16091. (interactive (list (let ((frag (when (org-region-active-p)
  16092. (buffer-substring-no-properties
  16093. (region-beginning) (region-end)))))
  16094. (read-string "LaTeX Fragment: " frag nil frag))))
  16095. (unless latex-frag (error "Invalid latex-frag"))
  16096. (let* ((tmp-in-file (file-relative-name
  16097. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16098. (ignore (write-region latex-frag nil tmp-in-file))
  16099. (tmp-out-file (file-relative-name
  16100. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16101. (cmd (format-spec
  16102. org-latex-to-mathml-convert-command
  16103. `((?j . ,(shell-quote-argument
  16104. (expand-file-name org-latex-to-mathml-jar-file)))
  16105. (?I . ,(shell-quote-argument tmp-in-file))
  16106. (?o . ,(shell-quote-argument tmp-out-file)))))
  16107. mathml shell-command-output)
  16108. (when (org-called-interactively-p 'any)
  16109. (unless (org-format-latex-mathml-available-p)
  16110. (error "LaTeX to MathML converter not configured")))
  16111. (message "Running %s" cmd)
  16112. (setq shell-command-output (shell-command-to-string cmd))
  16113. (setq mathml
  16114. (when (file-readable-p tmp-out-file)
  16115. (with-current-buffer (find-file-noselect tmp-out-file t)
  16116. (goto-char (point-min))
  16117. (when (re-search-forward
  16118. (concat
  16119. (regexp-quote
  16120. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16121. "\\(.\\|\n\\)*"
  16122. (regexp-quote "</math>")) nil t)
  16123. (prog1 (match-string 0) (kill-buffer))))))
  16124. (cond
  16125. (mathml
  16126. (setq mathml
  16127. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16128. (when mathml-file
  16129. (write-region mathml nil mathml-file))
  16130. (when (org-called-interactively-p 'any)
  16131. (message mathml)))
  16132. ((message "LaTeX to MathML conversion failed")
  16133. (message shell-command-output)))
  16134. (delete-file tmp-in-file)
  16135. (when (file-exists-p tmp-out-file)
  16136. (delete-file tmp-out-file))
  16137. mathml))
  16138. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16139. prefix &optional dir)
  16140. "Use `org-create-math-formula' but check local cache first."
  16141. (let* ((absprefix (expand-file-name prefix dir))
  16142. (print-length nil) (print-level nil)
  16143. (formula-id (concat
  16144. "formula-"
  16145. (sha1
  16146. (prin1-to-string
  16147. (list latex-frag
  16148. org-latex-to-mathml-convert-command)))))
  16149. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16150. (formula-cache-dir (file-name-directory formula-cache)))
  16151. (unless (file-directory-p formula-cache-dir)
  16152. (make-directory formula-cache-dir t))
  16153. (unless (file-exists-p formula-cache)
  16154. (org-create-math-formula latex-frag formula-cache))
  16155. (if (file-exists-p formula-cache)
  16156. ;; Successful conversion. Return the link to MathML file.
  16157. (org-add-props
  16158. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16159. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16160. 'org-latex-src-embed-type (if latex-frag-type
  16161. 'paragraph 'character)))
  16162. ;; Failed conversion. Return the LaTeX fragment verbatim
  16163. (add-text-properties
  16164. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  16165. latex-frag)))
  16166. (defun org-create-formula-image (string tofile options buffer &optional type)
  16167. "Create an image from LaTeX source using dvipng or convert.
  16168. This function calls either `org-create-formula-image-with-dvipng'
  16169. or `org-create-formula-image-with-imagemagick' depending on the
  16170. value of `org-latex-create-formula-image-program' or on the value
  16171. of the optional TYPE variable.
  16172. Note: ultimately these two function should be combined as they
  16173. share a good deal of logic."
  16174. (org-check-external-command
  16175. "latex" "needed to convert LaTeX fragments to images")
  16176. (funcall
  16177. (case (or type org-latex-create-formula-image-program)
  16178. ('dvipng
  16179. (org-check-external-command
  16180. "dvipng" "needed to convert LaTeX fragments to images")
  16181. #'org-create-formula-image-with-dvipng)
  16182. ('imagemagick
  16183. (org-check-external-command
  16184. "convert" "you need to install imagemagick")
  16185. #'org-create-formula-image-with-imagemagick)
  16186. (t (error
  16187. "invalid value of `org-latex-create-formula-image-program'")))
  16188. string tofile options buffer))
  16189. ;; This function borrows from Ganesh Swami's latex2png.el
  16190. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16191. "This calls dvipng."
  16192. (require 'org-latex)
  16193. (let* ((tmpdir (if (featurep 'xemacs)
  16194. (temp-directory)
  16195. temporary-file-directory))
  16196. (texfilebase (make-temp-name
  16197. (expand-file-name "orgtex" tmpdir)))
  16198. (texfile (concat texfilebase ".tex"))
  16199. (dvifile (concat texfilebase ".dvi"))
  16200. (pngfile (concat texfilebase ".png"))
  16201. (fnh (if (featurep 'xemacs)
  16202. (font-height (face-font 'default))
  16203. (face-attribute 'default :height nil)))
  16204. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16205. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16206. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16207. "Black"))
  16208. (bg (or (plist-get options (if buffer :background :html-background))
  16209. "Transparent")))
  16210. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16211. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16212. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16213. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16214. (with-temp-file texfile
  16215. (insert (org-splice-latex-header
  16216. org-format-latex-header
  16217. org-export-latex-default-packages-alist
  16218. org-export-latex-packages-alist t
  16219. org-format-latex-header-extra))
  16220. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  16221. (require 'org-latex)
  16222. (org-export-latex-fix-inputenc))
  16223. (let ((dir default-directory))
  16224. (condition-case nil
  16225. (progn
  16226. (cd tmpdir)
  16227. (call-process "latex" nil nil nil texfile))
  16228. (error nil))
  16229. (cd dir))
  16230. (if (not (file-exists-p dvifile))
  16231. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16232. (condition-case nil
  16233. (if (featurep 'xemacs)
  16234. (call-process "dvipng" nil nil nil
  16235. "-fg" fg "-bg" bg
  16236. "-T" "tight"
  16237. "-o" pngfile
  16238. dvifile)
  16239. (call-process "dvipng" nil nil nil
  16240. "-fg" fg "-bg" bg
  16241. "-D" dpi
  16242. ;;"-x" scale "-y" scale
  16243. "-T" "tight"
  16244. "-o" pngfile
  16245. dvifile))
  16246. (error nil))
  16247. (if (not (file-exists-p pngfile))
  16248. (if org-format-latex-signal-error
  16249. (error "Failed to create png file from %s" texfile)
  16250. (message "Failed to create png file from %s" texfile)
  16251. nil)
  16252. ;; Use the requested file name and clean up
  16253. (copy-file pngfile tofile 'replace)
  16254. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16255. (if (file-exists-p (concat texfilebase e))
  16256. (delete-file (concat texfilebase e))))
  16257. pngfile))))
  16258. (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
  16259. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16260. "This calls convert, which is included into imagemagick."
  16261. (require 'org-latex)
  16262. (let* ((tmpdir (if (featurep 'xemacs)
  16263. (temp-directory)
  16264. temporary-file-directory))
  16265. (texfilebase (make-temp-name
  16266. (expand-file-name "orgtex" tmpdir)))
  16267. (texfile (concat texfilebase ".tex"))
  16268. (pdffile (concat texfilebase ".pdf"))
  16269. (pngfile (concat texfilebase ".png"))
  16270. (fnh (if (featurep 'xemacs)
  16271. (font-height (face-font 'default))
  16272. (face-attribute 'default :height nil)))
  16273. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16274. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16275. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16276. "black"))
  16277. (bg (or (plist-get options (if buffer :background :html-background))
  16278. "white")))
  16279. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16280. (setq fg (org-latex-color-format fg)))
  16281. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16282. (setq bg (org-latex-color-format
  16283. (if (string= bg "Transparent") "white" bg))))
  16284. (with-temp-file texfile
  16285. (insert (org-splice-latex-header
  16286. org-format-latex-header
  16287. org-export-latex-default-packages-alist
  16288. org-export-latex-packages-alist t
  16289. org-format-latex-header-extra))
  16290. (insert "\n\\begin{document}\n"
  16291. "\\definecolor{fg}{rgb}{" fg "}\n"
  16292. "\\definecolor{bg}{rgb}{" bg "}\n"
  16293. "\n\\pagecolor{bg}\n"
  16294. "\n{\\color{fg}\n"
  16295. string
  16296. "\n}\n"
  16297. "\n\\end{document}\n" )
  16298. (require 'org-latex)
  16299. (org-export-latex-fix-inputenc))
  16300. (let ((dir default-directory) cmd cmds latex-frags-cmds)
  16301. (condition-case nil
  16302. (progn
  16303. (cd tmpdir)
  16304. (setq cmds org-latex-to-pdf-process)
  16305. (while cmds
  16306. (setq latex-frags-cmds (pop cmds))
  16307. (if (listp latex-frags-cmds)
  16308. (setq cmds nil)
  16309. (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
  16310. (while latex-frags-cmds
  16311. (setq cmd (pop latex-frags-cmds))
  16312. (while (string-match "%b" cmd)
  16313. (setq cmd (replace-match
  16314. (save-match-data
  16315. (shell-quote-argument texfile))
  16316. t t cmd)))
  16317. (while (string-match "%f" cmd)
  16318. (setq cmd (replace-match
  16319. (save-match-data
  16320. (shell-quote-argument (file-name-nondirectory texfile)))
  16321. t t cmd)))
  16322. (while (string-match "%o" cmd)
  16323. (setq cmd (replace-match
  16324. (save-match-data
  16325. (shell-quote-argument (file-name-directory texfile)))
  16326. t t cmd)))
  16327. (setq cmd (split-string cmd))
  16328. (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
  16329. (error nil))
  16330. (cd dir))
  16331. (if (not (file-exists-p pdffile))
  16332. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16333. (condition-case nil
  16334. (if (featurep 'xemacs)
  16335. (call-process "convert" nil nil nil
  16336. "-density" "96"
  16337. "-trim"
  16338. "-antialias"
  16339. pdffile
  16340. "-quality" "100"
  16341. ;; "-sharpen" "0x1.0"
  16342. pngfile)
  16343. (call-process "convert" nil nil nil
  16344. "-density" dpi
  16345. "-trim"
  16346. "-antialias"
  16347. pdffile
  16348. "-quality" "100"
  16349. ; "-sharpen" "0x1.0"
  16350. pngfile))
  16351. (error nil))
  16352. (if (not (file-exists-p pngfile))
  16353. (if org-format-latex-signal-error
  16354. (error "Failed to create png file from %s" texfile)
  16355. (message "Failed to create png file from %s" texfile)
  16356. nil)
  16357. ;; Use the requested file name and clean up
  16358. (copy-file pngfile tofile 'replace)
  16359. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16360. (if (file-exists-p (concat texfilebase e))
  16361. (delete-file (concat texfilebase e))))
  16362. pngfile))))
  16363. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16364. "Fill a LaTeX header template TPL.
  16365. In the template, the following place holders will be recognized:
  16366. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16367. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16368. [PACKAGES] \\usepackage statements for PKG
  16369. [NO-PACKAGES] do not include PKG
  16370. [EXTRA] the string EXTRA
  16371. [NO-EXTRA] do not include EXTRA
  16372. For backward compatibility, if both the positive and the negative place
  16373. holder is missing, the positive one (without the \"NO-\") will be
  16374. assumed to be present at the end of the template.
  16375. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16376. EXTRA is a string.
  16377. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16378. (let (rpl (end ""))
  16379. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16380. (setq rpl (if (or (match-end 1) (not def-pkg))
  16381. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16382. tpl (replace-match rpl t t tpl))
  16383. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16384. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16385. (setq rpl (if (or (match-end 1) (not pkg))
  16386. "" (org-latex-packages-to-string pkg snippets-p t))
  16387. tpl (replace-match rpl t t tpl))
  16388. (if pkg (setq end
  16389. (concat end "\n"
  16390. (org-latex-packages-to-string pkg snippets-p)))))
  16391. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16392. (setq rpl (if (or (match-end 1) (not extra))
  16393. "" (concat extra "\n"))
  16394. tpl (replace-match rpl t t tpl))
  16395. (if (and extra (string-match "\\S-" extra))
  16396. (setq end (concat end "\n" extra))))
  16397. (if (string-match "\\S-" end)
  16398. (concat tpl "\n" end)
  16399. tpl)))
  16400. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16401. "Turn an alist of packages into a string with the \\usepackage macros."
  16402. (setq pkg (mapconcat (lambda(p)
  16403. (cond
  16404. ((stringp p) p)
  16405. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16406. (format "%% Package %s omitted" (cadr p)))
  16407. ((equal "" (car p))
  16408. (format "\\usepackage{%s}" (cadr p)))
  16409. (t
  16410. (format "\\usepackage[%s]{%s}"
  16411. (car p) (cadr p)))))
  16412. pkg
  16413. "\n"))
  16414. (if newline (concat pkg "\n") pkg))
  16415. (defun org-dvipng-color (attr)
  16416. "Return a RGB color specification for dvipng."
  16417. (apply 'format "rgb %s %s %s"
  16418. (mapcar 'org-normalize-color
  16419. (if (featurep 'xemacs)
  16420. (color-rgb-components
  16421. (face-property 'default
  16422. (cond ((eq attr :foreground) 'foreground)
  16423. ((eq attr :background) 'background))))
  16424. (color-values (face-attribute 'default attr nil))))))
  16425. (defun org-dvipng-color-format (color-name)
  16426. "Convert COLOR-NAME to a RGB color value for dvipng."
  16427. (apply 'format "rgb %s %s %s"
  16428. (mapcar 'org-normalize-color
  16429. (color-values color-name))))
  16430. (defun org-latex-color (attr)
  16431. "Return a RGB color for the LaTeX color package."
  16432. (apply 'format "%s,%s,%s"
  16433. (mapcar 'org-normalize-color
  16434. (if (featurep 'xemacs)
  16435. (color-rgb-components
  16436. (face-property 'default
  16437. (cond ((eq attr :foreground) 'foreground)
  16438. ((eq attr :background) 'background))))
  16439. (color-values (face-attribute 'default attr nil))))))
  16440. (defun org-latex-color-format (color-name)
  16441. "Convert COLOR-NAME to a RGB color value."
  16442. (apply 'format "%s,%s,%s"
  16443. (mapcar 'org-normalize-color
  16444. (color-values color-name))))
  16445. (defun org-normalize-color (value)
  16446. "Return string to be used as color value for an RGB component."
  16447. (format "%g" (/ value 65535.0)))
  16448. ;; Image display
  16449. (defvar org-inline-image-overlays nil)
  16450. (make-variable-buffer-local 'org-inline-image-overlays)
  16451. (defun org-toggle-inline-images (&optional include-linked)
  16452. "Toggle the display of inline images.
  16453. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16454. (interactive "P")
  16455. (if org-inline-image-overlays
  16456. (progn
  16457. (org-remove-inline-images)
  16458. (message "Inline image display turned off"))
  16459. (org-display-inline-images include-linked)
  16460. (if (and (org-called-interactively-p)
  16461. org-inline-image-overlays)
  16462. (message "%d images displayed inline"
  16463. (length org-inline-image-overlays))
  16464. (message "No images to display inline"))))
  16465. (defun org-redisplay-inline-images ()
  16466. "Refresh the display of inline images."
  16467. (interactive)
  16468. (if (not org-inline-image-overlays)
  16469. (org-toggle-inline-images)
  16470. (org-toggle-inline-images)
  16471. (org-toggle-inline-images)))
  16472. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16473. "Display inline images.
  16474. Normally only links without a description part are inlined, because this
  16475. is how it will work for export. When INCLUDE-LINKED is set, also links
  16476. with a description part will be inlined. This can be nice for a quick
  16477. look at those images, but it does not reflect what exported files will look
  16478. like.
  16479. When REFRESH is set, refresh existing images between BEG and END.
  16480. This will create new image displays only if necessary.
  16481. BEG and END default to the buffer boundaries."
  16482. (interactive "P")
  16483. (unless refresh
  16484. (org-remove-inline-images)
  16485. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16486. (save-excursion
  16487. (save-restriction
  16488. (widen)
  16489. (setq beg (or beg (point-min)) end (or end (point-max)))
  16490. (goto-char beg)
  16491. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16492. (substring (org-image-file-name-regexp) 0 -2)
  16493. "\\)\\]" (if include-linked "" "\\]")))
  16494. old file ov img type attrwidth width)
  16495. (while (re-search-forward re end t)
  16496. (setq old (get-char-property-and-overlay (match-beginning 1)
  16497. 'org-image-overlay)
  16498. file (expand-file-name
  16499. (concat (or (match-string 3) "") (match-string 4))))
  16500. (when (image-type-available-p 'imagemagick)
  16501. (setq attrwidth (if (or (listp org-image-actual-width)
  16502. (null org-image-actual-width))
  16503. (save-excursion
  16504. (save-match-data
  16505. (when (re-search-backward
  16506. "#\\+ATTR.*width=\"\\([^\"]+\\)\""
  16507. (save-excursion
  16508. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  16509. (string-to-number (match-string 1))))))
  16510. width (cond ((eq org-image-actual-width t) nil)
  16511. ((null org-image-actual-width) attrwidth)
  16512. ((numberp org-image-actual-width)
  16513. org-image-actual-width)
  16514. ((listp org-image-actual-width)
  16515. (or attrwidth (car org-image-actual-width))))
  16516. type (if width 'imagemagick)))
  16517. (when (file-exists-p file)
  16518. (if (and (car-safe old) refresh)
  16519. (image-refresh (overlay-get (cdr old) 'display))
  16520. (setq img (save-match-data (create-image file type nil :width width)))
  16521. (when img
  16522. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16523. (overlay-put ov 'display img)
  16524. (overlay-put ov 'face 'default)
  16525. (overlay-put ov 'org-image-overlay t)
  16526. (overlay-put ov 'modification-hooks
  16527. (list 'org-display-inline-remove-overlay))
  16528. (push ov org-inline-image-overlays)))))))))
  16529. (define-obsolete-function-alias
  16530. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  16531. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  16532. "Remove inline-display overlay if a corresponding region is modified."
  16533. (let ((inhibit-modification-hooks t))
  16534. (when (and ov after)
  16535. (delete ov org-inline-image-overlays)
  16536. (delete-overlay ov))))
  16537. (defun org-remove-inline-images ()
  16538. "Remove inline display of images."
  16539. (interactive)
  16540. (mapc 'delete-overlay org-inline-image-overlays)
  16541. (setq org-inline-image-overlays nil))
  16542. ;;;; Key bindings
  16543. ;; Outline functions from `outline-mode-prefix-map'
  16544. ;; that can be remapped in Org:
  16545. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16546. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16547. (define-key org-mode-map [remap outline-forward-same-level]
  16548. 'org-forward-heading-same-level)
  16549. (define-key org-mode-map [remap outline-backward-same-level]
  16550. 'org-backward-heading-same-level)
  16551. (define-key org-mode-map [remap show-branches]
  16552. 'org-kill-note-or-show-branches)
  16553. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16554. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16555. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16556. ;; Outline functions from `outline-mode-prefix-map' that can not
  16557. ;; be remapped in Org:
  16558. ;;
  16559. ;; - the column "key binding" shows whether the Outline function is still
  16560. ;; available in Org mode on the same key that it has been bound to in
  16561. ;; Outline mode:
  16562. ;; - "overridden": key used for a different functionality in Org mode
  16563. ;; - else: key still bound to the same Outline function in Org mode
  16564. ;;
  16565. ;; | Outline function | key binding | Org replacement |
  16566. ;; |------------------------------------+-------------+-----------------------|
  16567. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16568. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16569. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16570. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  16571. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16572. ;; | `show-entry' | overridden | no replacement |
  16573. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16574. ;; | `show-branches' | `C-c C-k' | still same function |
  16575. ;; | `show-subtree' | overridden | visibility cycling |
  16576. ;; | `show-all' | overridden | no replacement |
  16577. ;; | `hide-subtree' | overridden | visibility cycling |
  16578. ;; | `hide-body' | overridden | no replacement |
  16579. ;; | `hide-entry' | overridden | visibility cycling |
  16580. ;; | `hide-leaves' | overridden | no replacement |
  16581. ;; | `hide-sublevels' | overridden | no replacement |
  16582. ;; | `hide-other' | overridden | no replacement |
  16583. ;; Make `C-c C-x' a prefix key
  16584. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16585. ;; TAB key with modifiers
  16586. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16587. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16588. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16589. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16590. ;; The following line is necessary under Suse GNU/Linux
  16591. (unless (featurep 'xemacs)
  16592. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16593. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16594. (define-key org-mode-map [backtab] 'org-shifttab)
  16595. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  16596. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  16597. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  16598. ;; Cursor keys with modifiers
  16599. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  16600. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  16601. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  16602. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  16603. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  16604. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  16605. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  16606. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  16607. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  16608. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  16609. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  16610. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  16611. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  16612. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  16613. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  16614. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  16615. ;; Babel keys
  16616. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  16617. (mapc (lambda (pair)
  16618. (define-key org-babel-map (car pair) (cdr pair)))
  16619. org-babel-key-bindings)
  16620. ;;; Extra keys for tty access.
  16621. ;; We only set them when really needed because otherwise the
  16622. ;; menus don't show the simple keys
  16623. (when (or org-use-extra-keys
  16624. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  16625. (not window-system))
  16626. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  16627. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  16628. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  16629. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  16630. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  16631. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  16632. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  16633. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  16634. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  16635. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  16636. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  16637. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  16638. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  16639. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  16640. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  16641. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  16642. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  16643. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  16644. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  16645. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  16646. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  16647. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  16648. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  16649. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  16650. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  16651. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  16652. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  16653. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  16654. ;; All the other keys
  16655. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16656. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  16657. (if (boundp 'narrow-map)
  16658. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  16659. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  16660. (if (boundp 'narrow-map)
  16661. (org-defkey narrow-map "b" 'org-narrow-to-block)
  16662. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  16663. (if (boundp 'narrow-map)
  16664. (org-defkey narrow-map "e" 'org-narrow-to-element)
  16665. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  16666. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  16667. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  16668. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  16669. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  16670. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  16671. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  16672. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  16673. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  16674. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16675. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  16676. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  16677. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  16678. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  16679. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16680. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  16681. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16682. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16683. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16684. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16685. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16686. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16687. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  16688. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16689. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  16690. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  16691. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  16692. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  16693. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  16694. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  16695. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  16696. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  16697. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  16698. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  16699. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  16700. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  16701. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16702. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16703. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16704. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16705. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16706. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16707. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16708. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16709. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16710. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16711. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16712. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16713. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16714. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16715. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16716. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16717. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16718. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16719. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16720. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16721. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16722. (org-defkey org-mode-map "\C-m" 'org-return)
  16723. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16724. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16725. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16726. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16727. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16728. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16729. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16730. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16731. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16732. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16733. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16734. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16735. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16736. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16737. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16738. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16739. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16740. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16741. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16742. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16743. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  16744. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16745. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16746. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  16747. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16748. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16749. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16750. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16751. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16752. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  16753. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  16754. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16755. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16756. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  16757. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16758. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16759. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16760. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16761. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  16762. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  16763. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  16764. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16765. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  16766. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  16767. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  16768. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  16769. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  16770. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  16771. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  16772. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  16773. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16774. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  16775. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  16776. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  16777. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  16778. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  16779. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  16780. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  16781. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  16782. (when (featurep 'xemacs)
  16783. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  16784. (defconst org-speed-commands-default
  16785. '(
  16786. ("Outline Navigation")
  16787. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  16788. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  16789. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  16790. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  16791. ("u" . (org-speed-move-safe 'outline-up-heading))
  16792. ("j" . org-goto)
  16793. ("g" . (org-refile t))
  16794. ("Outline Visibility")
  16795. ("c" . org-cycle)
  16796. ("C" . org-shifttab)
  16797. (" " . org-display-outline-path)
  16798. (":" . org-columns)
  16799. ("Outline Structure Editing")
  16800. ("U" . org-shiftmetaup)
  16801. ("D" . org-shiftmetadown)
  16802. ("r" . org-metaright)
  16803. ("l" . org-metaleft)
  16804. ("R" . org-shiftmetaright)
  16805. ("L" . org-shiftmetaleft)
  16806. ("i" . (progn (forward-char 1) (call-interactively
  16807. 'org-insert-heading-respect-content)))
  16808. ("^" . org-sort)
  16809. ("w" . org-refile)
  16810. ("a" . org-archive-subtree-default-with-confirmation)
  16811. ("." . org-mark-subtree)
  16812. ("#" . org-toggle-comment)
  16813. ("Clock Commands")
  16814. ("I" . org-clock-in)
  16815. ("O" . org-clock-out)
  16816. ("Meta Data Editing")
  16817. ("t" . org-todo)
  16818. ("," . (org-priority))
  16819. ("0" . (org-priority ?\ ))
  16820. ("1" . (org-priority ?A))
  16821. ("2" . (org-priority ?B))
  16822. ("3" . (org-priority ?C))
  16823. (";" . org-set-tags-command)
  16824. ("e" . org-set-effort)
  16825. ("E" . org-inc-effort)
  16826. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  16827. (org-entry-put (point) "APPT_WARNTIME" m)))
  16828. ("Agenda Views etc")
  16829. ("v" . org-agenda)
  16830. ("/" . org-sparse-tree)
  16831. ("Misc")
  16832. ("o" . org-open-at-point)
  16833. ("?" . org-speed-command-help)
  16834. ("<" . (org-agenda-set-restriction-lock 'subtree))
  16835. (">" . (org-agenda-remove-restriction-lock))
  16836. )
  16837. "The default speed commands.")
  16838. (defun org-print-speed-command (e)
  16839. (if (> (length (car e)) 1)
  16840. (progn
  16841. (princ "\n")
  16842. (princ (car e))
  16843. (princ "\n")
  16844. (princ (make-string (length (car e)) ?-))
  16845. (princ "\n"))
  16846. (princ (car e))
  16847. (princ " ")
  16848. (if (symbolp (cdr e))
  16849. (princ (symbol-name (cdr e)))
  16850. (prin1 (cdr e)))
  16851. (princ "\n")))
  16852. (defun org-speed-command-help ()
  16853. "Show the available speed commands."
  16854. (interactive)
  16855. (if (not org-use-speed-commands)
  16856. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16857. (with-output-to-temp-buffer "*Help*"
  16858. (princ "User-defined Speed commands\n===========================\n")
  16859. (mapc 'org-print-speed-command org-speed-commands-user)
  16860. (princ "\n")
  16861. (princ "Built-in Speed commands\n=======================\n")
  16862. (mapc 'org-print-speed-command org-speed-commands-default))
  16863. (with-current-buffer "*Help*"
  16864. (setq truncate-lines t))))
  16865. (defun org-speed-move-safe (cmd)
  16866. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16867. If not, return to the original position and throw an error."
  16868. (interactive)
  16869. (let ((pos (point)))
  16870. (call-interactively cmd)
  16871. (unless (and (bolp) (org-at-heading-p))
  16872. (goto-char pos)
  16873. (error "Boundary reached while executing %s" cmd))))
  16874. (defvar org-self-insert-command-undo-counter 0)
  16875. (defvar org-table-auto-blank-field) ; defined in org-table.el
  16876. (defvar org-speed-command nil)
  16877. (define-obsolete-function-alias
  16878. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  16879. (defun org-speed-command-activate (keys)
  16880. "Hook for activating single-letter speed commands.
  16881. `org-speed-commands-default' specifies a minimal command set.
  16882. Use `org-speed-commands-user' for further customization."
  16883. (when (or (and (bolp) (looking-at org-outline-regexp))
  16884. (and (functionp org-use-speed-commands)
  16885. (funcall org-use-speed-commands)))
  16886. (cdr (assoc keys (append org-speed-commands-user
  16887. org-speed-commands-default)))))
  16888. (define-obsolete-function-alias
  16889. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  16890. (defun org-babel-speed-command-activate (keys)
  16891. "Hook for activating single-letter code block commands."
  16892. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  16893. (cdr (assoc keys org-babel-key-bindings))))
  16894. (defcustom org-speed-command-hook
  16895. '(org-speed-command-default-hook org-babel-speed-command-hook)
  16896. "Hook for activating speed commands at strategic locations.
  16897. Hook functions are called in sequence until a valid handler is
  16898. found.
  16899. Each hook takes a single argument, a user-pressed command key
  16900. which is also a `self-insert-command' from the global map.
  16901. Within the hook, examine the cursor position and the command key
  16902. and return nil or a valid handler as appropriate. Handler could
  16903. be one of an interactive command, a function, or a form.
  16904. Set `org-use-speed-commands' to non-nil value to enable this
  16905. hook. The default setting is `org-speed-command-activate'."
  16906. :group 'org-structure
  16907. :version "24.1"
  16908. :type 'hook)
  16909. (defun org-self-insert-command (N)
  16910. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16911. If the cursor is in a table looking at whitespace, the whitespace is
  16912. overwritten, and the table is not marked as requiring realignment."
  16913. (interactive "p")
  16914. (org-check-before-invisible-edit 'insert)
  16915. (cond
  16916. ((and org-use-speed-commands
  16917. (setq org-speed-command
  16918. (run-hook-with-args-until-success
  16919. 'org-speed-command-hook (this-command-keys))))
  16920. (cond
  16921. ((commandp org-speed-command)
  16922. (setq this-command org-speed-command)
  16923. (call-interactively org-speed-command))
  16924. ((functionp org-speed-command)
  16925. (funcall org-speed-command))
  16926. ((and org-speed-command (listp org-speed-command))
  16927. (eval org-speed-command))
  16928. (t (let (org-use-speed-commands)
  16929. (call-interactively 'org-self-insert-command)))))
  16930. ((and
  16931. (org-table-p)
  16932. (progn
  16933. ;; check if we blank the field, and if that triggers align
  16934. (and (featurep 'org-table) org-table-auto-blank-field
  16935. (member last-command
  16936. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16937. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16938. ;; got extra space, this field does not determine column width
  16939. (let (org-table-may-need-update) (org-table-blank-field))
  16940. ;; no extra space, this field may determine column width
  16941. (org-table-blank-field)))
  16942. t)
  16943. (eq N 1)
  16944. (looking-at "[^|\n]* |"))
  16945. (let (org-table-may-need-update)
  16946. (goto-char (1- (match-end 0)))
  16947. (backward-delete-char 1)
  16948. (goto-char (match-beginning 0))
  16949. (self-insert-command N)))
  16950. (t
  16951. (setq org-table-may-need-update t)
  16952. (self-insert-command N)
  16953. (org-fix-tags-on-the-fly)
  16954. (if org-self-insert-cluster-for-undo
  16955. (if (not (eq last-command 'org-self-insert-command))
  16956. (setq org-self-insert-command-undo-counter 1)
  16957. (if (>= org-self-insert-command-undo-counter 20)
  16958. (setq org-self-insert-command-undo-counter 1)
  16959. (and (> org-self-insert-command-undo-counter 0)
  16960. buffer-undo-list (listp buffer-undo-list)
  16961. (not (cadr buffer-undo-list)) ; remove nil entry
  16962. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16963. (setq org-self-insert-command-undo-counter
  16964. (1+ org-self-insert-command-undo-counter))))))))
  16965. (defun org-check-before-invisible-edit (kind)
  16966. "Check is editing if kind KIND would be dangerous with invisible text around.
  16967. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16968. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16969. (if (and org-catch-invisible-edits
  16970. (or (not (boundp 'visible-mode)) (not visible-mode))
  16971. (or (get-char-property (point) 'invisible)
  16972. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16973. ;; OK, we need to take a closer look
  16974. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16975. (invisible-before-point (if (bobp) nil (get-char-property
  16976. (1- (point)) 'invisible)))
  16977. (border-and-ok-direction
  16978. (or
  16979. ;; Check if we are acting predictably before invisible text
  16980. (and invisible-at-point (not invisible-before-point)
  16981. (memq kind '(insert delete-backward)))
  16982. ;; Check if we are acting predictably after invisible text
  16983. ;; This works not well, and I have turned it off. It seems
  16984. ;; better to always show and stop after invisible text.
  16985. ;; (and (not invisible-at-point) invisible-before-point
  16986. ;; (memq kind '(insert delete)))
  16987. )))
  16988. (when (or (memq invisible-at-point '(outline org-hide-block t))
  16989. (memq invisible-before-point '(outline org-hide-block t)))
  16990. (if (eq org-catch-invisible-edits 'error)
  16991. (error "Editing in invisible areas is prohibited - make visible first"))
  16992. (if (and org-custom-properties-overlays
  16993. (y-or-n-p "Display invisible properties in this buffer? "))
  16994. (org-toggle-custom-properties-visibility)
  16995. ;; Make the area visible
  16996. (save-excursion
  16997. (if invisible-before-point
  16998. (goto-char (previous-single-char-property-change
  16999. (point) 'invisible)))
  17000. (org-cycle))
  17001. (cond
  17002. ((eq org-catch-invisible-edits 'show)
  17003. ;; That's it, we do the edit after showing
  17004. (message
  17005. "Unfolding invisible region around point before editing")
  17006. (sit-for 1))
  17007. ((and (eq org-catch-invisible-edits 'smart)
  17008. border-and-ok-direction)
  17009. (message "Unfolding invisible region around point before editing"))
  17010. (t
  17011. ;; Don't do the edit, make the user repeat it in full visibility
  17012. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17013. (defun org-fix-tags-on-the-fly ()
  17014. (when (and (equal (char-after (point-at-bol)) ?*)
  17015. (org-at-heading-p))
  17016. (org-align-tags-here org-tags-column)))
  17017. (defun org-delete-backward-char (N)
  17018. "Like `delete-backward-char', insert whitespace at field end in tables.
  17019. When deleting backwards, in tables this function will insert whitespace in
  17020. front of the next \"|\" separator, to keep the table aligned. The table will
  17021. still be marked for re-alignment if the field did fill the entire column,
  17022. because, in this case the deletion might narrow the column."
  17023. (interactive "p")
  17024. (org-check-before-invisible-edit 'delete-backward)
  17025. (if (and (org-table-p)
  17026. (eq N 1)
  17027. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17028. (looking-at ".*?|"))
  17029. (let ((pos (point))
  17030. (noalign (looking-at "[^|\n\r]* |"))
  17031. (c org-table-may-need-update))
  17032. (backward-delete-char N)
  17033. (if (not overwrite-mode)
  17034. (progn
  17035. (skip-chars-forward "^|")
  17036. (insert " ")
  17037. (goto-char (1- pos))))
  17038. ;; noalign: if there were two spaces at the end, this field
  17039. ;; does not determine the width of the column.
  17040. (if noalign (setq org-table-may-need-update c)))
  17041. (backward-delete-char N)
  17042. (org-fix-tags-on-the-fly)))
  17043. (defun org-delete-char (N)
  17044. "Like `delete-char', but insert whitespace at field end in tables.
  17045. When deleting characters, in tables this function will insert whitespace in
  17046. front of the next \"|\" separator, to keep the table aligned. The table will
  17047. still be marked for re-alignment if the field did fill the entire column,
  17048. because, in this case the deletion might narrow the column."
  17049. (interactive "p")
  17050. (org-check-before-invisible-edit 'delete)
  17051. (if (and (org-table-p)
  17052. (not (bolp))
  17053. (not (= (char-after) ?|))
  17054. (eq N 1))
  17055. (if (looking-at ".*?|")
  17056. (let ((pos (point))
  17057. (noalign (looking-at "[^|\n\r]* |"))
  17058. (c org-table-may-need-update))
  17059. (replace-match (concat
  17060. (substring (match-string 0) 1 -1)
  17061. " |"))
  17062. (goto-char pos)
  17063. ;; noalign: if there were two spaces at the end, this field
  17064. ;; does not determine the width of the column.
  17065. (if noalign (setq org-table-may-need-update c)))
  17066. (delete-char N))
  17067. (delete-char N)
  17068. (org-fix-tags-on-the-fly)))
  17069. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17070. (put 'org-self-insert-command 'delete-selection t)
  17071. (put 'orgtbl-self-insert-command 'delete-selection t)
  17072. (put 'org-delete-char 'delete-selection 'supersede)
  17073. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17074. (put 'org-yank 'delete-selection 'yank)
  17075. ;; Make `flyspell-mode' delay after some commands
  17076. (put 'org-self-insert-command 'flyspell-delayed t)
  17077. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17078. (put 'org-delete-char 'flyspell-delayed t)
  17079. (put 'org-delete-backward-char 'flyspell-delayed t)
  17080. ;; Make pabbrev-mode expand after org-mode commands
  17081. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17082. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17083. ;; How to do this: Measure non-white length of current string
  17084. ;; If equal to column width, we should realign.
  17085. (defun org-remap (map &rest commands)
  17086. "In MAP, remap the functions given in COMMANDS.
  17087. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17088. (let (new old)
  17089. (while commands
  17090. (setq old (pop commands) new (pop commands))
  17091. (if (fboundp 'command-remapping)
  17092. (org-defkey map (vector 'remap old) new)
  17093. (substitute-key-definition old new map global-map)))))
  17094. (when (eq org-enable-table-editor 'optimized)
  17095. ;; If the user wants maximum table support, we need to hijack
  17096. ;; some standard editing functions
  17097. (org-remap org-mode-map
  17098. 'self-insert-command 'org-self-insert-command
  17099. 'delete-char 'org-delete-char
  17100. 'delete-backward-char 'org-delete-backward-char)
  17101. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17102. (defvar org-ctrl-c-ctrl-c-hook nil
  17103. "Hook for functions attaching themselves to `C-c C-c'.
  17104. This can be used to add additional functionality to the C-c C-c
  17105. key which executes context-dependent commands. This hook is run
  17106. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17107. run after the last test.
  17108. Each function will be called with no arguments. The function
  17109. must check if the context is appropriate for it to act. If yes,
  17110. it should do its thing and then return a non-nil value. If the
  17111. context is wrong, just do nothing and return nil.")
  17112. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17113. "Hook for functions attaching themselves to `C-c C-c'.
  17114. This can be used to add additional functionality to the C-c C-c
  17115. key which executes context-dependent commands. This hook is run
  17116. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17117. before the first test.
  17118. Each function will be called with no arguments. The function
  17119. must check if the context is appropriate for it to act. If yes,
  17120. it should do its thing and then return a non-nil value. If the
  17121. context is wrong, just do nothing and return nil.")
  17122. (defvar org-tab-first-hook nil
  17123. "Hook for functions to attach themselves to TAB.
  17124. See `org-ctrl-c-ctrl-c-hook' for more information.
  17125. This hook runs as the first action when TAB is pressed, even before
  17126. `org-cycle' messes around with the `outline-regexp' to cater for
  17127. inline tasks and plain list item folding.
  17128. If any function in this hook returns t, any other actions that
  17129. would have been caused by TAB (such as table field motion or visibility
  17130. cycling) will not occur.")
  17131. (defvar org-tab-after-check-for-table-hook nil
  17132. "Hook for functions to attach themselves to TAB.
  17133. See `org-ctrl-c-ctrl-c-hook' for more information.
  17134. This hook runs after it has been established that the cursor is not in a
  17135. table, but before checking if the cursor is in a headline or if global cycling
  17136. should be done.
  17137. If any function in this hook returns t, not other actions like visibility
  17138. cycling will be done.")
  17139. (defvar org-tab-after-check-for-cycling-hook nil
  17140. "Hook for functions to attach themselves to TAB.
  17141. See `org-ctrl-c-ctrl-c-hook' for more information.
  17142. This hook runs after it has been established that not table field motion and
  17143. not visibility should be done because of current context. This is probably
  17144. the place where a package like yasnippets can hook in.")
  17145. (defvar org-tab-before-tab-emulation-hook nil
  17146. "Hook for functions to attach themselves to TAB.
  17147. See `org-ctrl-c-ctrl-c-hook' for more information.
  17148. This hook runs after every other options for TAB have been exhausted, but
  17149. before indentation and \t insertion takes place.")
  17150. (defvar org-metaleft-hook nil
  17151. "Hook for functions attaching themselves to `M-left'.
  17152. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17153. (defvar org-metaright-hook nil
  17154. "Hook for functions attaching themselves to `M-right'.
  17155. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17156. (defvar org-metaup-hook nil
  17157. "Hook for functions attaching themselves to `M-up'.
  17158. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17159. (defvar org-metadown-hook nil
  17160. "Hook for functions attaching themselves to `M-down'.
  17161. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17162. (defvar org-shiftmetaleft-hook nil
  17163. "Hook for functions attaching themselves to `M-S-left'.
  17164. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17165. (defvar org-shiftmetaright-hook nil
  17166. "Hook for functions attaching themselves to `M-S-right'.
  17167. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17168. (defvar org-shiftmetaup-hook nil
  17169. "Hook for functions attaching themselves to `M-S-up'.
  17170. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17171. (defvar org-shiftmetadown-hook nil
  17172. "Hook for functions attaching themselves to `M-S-down'.
  17173. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17174. (defvar org-metareturn-hook nil
  17175. "Hook for functions attaching themselves to `M-RET'.
  17176. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17177. (defvar org-shiftup-hook nil
  17178. "Hook for functions attaching themselves to `S-up'.
  17179. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17180. (defvar org-shiftup-final-hook nil
  17181. "Hook for functions attaching themselves to `S-up'.
  17182. This one runs after all other options except shift-select have been excluded.
  17183. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17184. (defvar org-shiftdown-hook nil
  17185. "Hook for functions attaching themselves to `S-down'.
  17186. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17187. (defvar org-shiftdown-final-hook nil
  17188. "Hook for functions attaching themselves to `S-down'.
  17189. This one runs after all other options except shift-select have been excluded.
  17190. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17191. (defvar org-shiftleft-hook nil
  17192. "Hook for functions attaching themselves to `S-left'.
  17193. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17194. (defvar org-shiftleft-final-hook nil
  17195. "Hook for functions attaching themselves to `S-left'.
  17196. This one runs after all other options except shift-select have been excluded.
  17197. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17198. (defvar org-shiftright-hook nil
  17199. "Hook for functions attaching themselves to `S-right'.
  17200. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17201. (defvar org-shiftright-final-hook nil
  17202. "Hook for functions attaching themselves to `S-right'.
  17203. This one runs after all other options except shift-select have been excluded.
  17204. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17205. (defun org-modifier-cursor-error ()
  17206. "Throw an error, a modified cursor command was applied in wrong context."
  17207. (error "This command is active in special context like tables, headlines or items"))
  17208. (defun org-shiftselect-error ()
  17209. "Throw an error because Shift-Cursor command was applied in wrong context."
  17210. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17211. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17212. (error "This command works only in special context like headlines or timestamps")))
  17213. (defun org-call-for-shift-select (cmd)
  17214. (let ((this-command-keys-shift-translated t))
  17215. (call-interactively cmd)))
  17216. (defun org-shifttab (&optional arg)
  17217. "Global visibility cycling or move to previous table field.
  17218. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17219. on context.
  17220. See the individual commands for more information."
  17221. (interactive "P")
  17222. (cond
  17223. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17224. ((integerp arg)
  17225. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17226. (message "Content view to level: %d" arg)
  17227. (org-content (prefix-numeric-value arg2))
  17228. (setq org-cycle-global-status 'overview)))
  17229. (t (call-interactively 'org-global-cycle))))
  17230. (defun org-shiftmetaleft ()
  17231. "Promote subtree or delete table column.
  17232. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17233. `org-table-delete-column', depending on context. See the
  17234. individual commands for more information."
  17235. (interactive)
  17236. (cond
  17237. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17238. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17239. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17240. ((if (not (org-region-active-p)) (org-at-item-p)
  17241. (save-excursion (goto-char (region-beginning))
  17242. (org-at-item-p)))
  17243. (call-interactively 'org-outdent-item-tree))
  17244. (t (org-modifier-cursor-error))))
  17245. (defun org-shiftmetaright ()
  17246. "Demote subtree or insert table column.
  17247. Calls `org-demote-subtree', `org-indent-item-tree', or
  17248. `org-table-insert-column', depending on context. See the
  17249. individual commands for more information."
  17250. (interactive)
  17251. (cond
  17252. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17253. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17254. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17255. ((if (not (org-region-active-p)) (org-at-item-p)
  17256. (save-excursion (goto-char (region-beginning))
  17257. (org-at-item-p)))
  17258. (call-interactively 'org-indent-item-tree))
  17259. (t (org-modifier-cursor-error))))
  17260. (defun org-shiftmetaup (&optional arg)
  17261. "Move subtree up or kill table row.
  17262. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17263. `org-move-item-up' or `org-timestamp-up', depending on context.
  17264. See the individual commands for more information."
  17265. (interactive "P")
  17266. (cond
  17267. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17268. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17269. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17270. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17271. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17272. (call-interactively 'org-timestamp-up)))
  17273. (t (org-modifier-cursor-error))))
  17274. (defun org-shiftmetadown (&optional arg)
  17275. "Move subtree down or insert table row.
  17276. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17277. `org-move-item-down' or `org-timestamp-up', depending on context.
  17278. See the individual commands for more information."
  17279. (interactive "P")
  17280. (cond
  17281. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17282. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17283. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17284. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17285. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17286. (call-interactively 'org-timestamp-down)))
  17287. (t (org-modifier-cursor-error))))
  17288. (defsubst org-hidden-tree-error ()
  17289. (error
  17290. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17291. (defun org-metaleft (&optional arg)
  17292. "Promote heading or move table column to left.
  17293. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17294. With no specific context, calls the Emacs default `backward-word'.
  17295. See the individual commands for more information."
  17296. (interactive "P")
  17297. (cond
  17298. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17299. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17300. ((org-with-limited-levels
  17301. (or (org-at-heading-p)
  17302. (and (org-region-active-p)
  17303. (save-excursion
  17304. (goto-char (region-beginning))
  17305. (org-at-heading-p)))))
  17306. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17307. (call-interactively 'org-do-promote))
  17308. ;; At an inline task.
  17309. ((org-at-heading-p)
  17310. (call-interactively 'org-inlinetask-promote))
  17311. ((or (org-at-item-p)
  17312. (and (org-region-active-p)
  17313. (save-excursion
  17314. (goto-char (region-beginning))
  17315. (org-at-item-p))))
  17316. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17317. (call-interactively 'org-outdent-item))
  17318. (t (call-interactively 'backward-word))))
  17319. (defun org-metaright (&optional arg)
  17320. "Demote a subtree, a list item or move table column to right.
  17321. In front of a drawer or a block keyword, indent it correctly.
  17322. With no specific context, calls the Emacs default `forward-word'.
  17323. See the individual commands for more information."
  17324. (interactive "P")
  17325. (cond
  17326. ((run-hook-with-args-until-success 'org-metaright-hook))
  17327. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17328. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17329. ((org-at-block-p) (call-interactively 'org-indent-block))
  17330. ((org-with-limited-levels
  17331. (or (org-at-heading-p)
  17332. (and (org-region-active-p)
  17333. (save-excursion
  17334. (goto-char (region-beginning))
  17335. (org-at-heading-p)))))
  17336. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17337. (call-interactively 'org-do-demote))
  17338. ;; At an inline task.
  17339. ((org-at-heading-p)
  17340. (call-interactively 'org-inlinetask-demote))
  17341. ((or (org-at-item-p)
  17342. (and (org-region-active-p)
  17343. (save-excursion
  17344. (goto-char (region-beginning))
  17345. (org-at-item-p))))
  17346. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17347. (call-interactively 'org-indent-item))
  17348. (t (call-interactively 'forward-word))))
  17349. (defun org-check-for-hidden (what)
  17350. "Check if there are hidden headlines/items in the current visual line.
  17351. WHAT can be either `headlines' or `items'. If the current line is
  17352. an outline or item heading and it has a folded subtree below it,
  17353. this function returns t, nil otherwise."
  17354. (let ((re (cond
  17355. ((eq what 'headlines) org-outline-regexp-bol)
  17356. ((eq what 'items) (org-item-beginning-re))
  17357. (t (error "This should not happen"))))
  17358. beg end)
  17359. (save-excursion
  17360. (catch 'exit
  17361. (unless (org-region-active-p)
  17362. (setq beg (point-at-bol))
  17363. (beginning-of-line 2)
  17364. (while (and (not (eobp)) ;; this is like `next-line'
  17365. (get-char-property (1- (point)) 'invisible))
  17366. (beginning-of-line 2))
  17367. (setq end (point))
  17368. (goto-char beg)
  17369. (goto-char (point-at-eol))
  17370. (setq end (max end (point)))
  17371. (while (re-search-forward re end t)
  17372. (if (get-char-property (match-beginning 0) 'invisible)
  17373. (throw 'exit t))))
  17374. nil))))
  17375. (autoload 'org-element-at-point "org-element")
  17376. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  17377. (declare-function org-element-type "org-element" (element))
  17378. (declare-function org-element-context "org-element" ())
  17379. (declare-function org-element-contents "org-element" (element))
  17380. (declare-function org-element-property "org-element" (property element))
  17381. (declare-function org-element-paragraph-parser "org-element" (limit))
  17382. (declare-function org-element-map "org-element" (data types fun &optional info first-match no-recursion))
  17383. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  17384. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  17385. (declare-function org-element--parse-objects "org-element" (beg end acc restriction))
  17386. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  17387. (defun org-metaup (&optional arg)
  17388. "Move subtree up or move table row up.
  17389. Calls `org-move-subtree-up' or `org-table-move-row' or
  17390. `org-move-item-up', depending on context. See the individual commands
  17391. for more information."
  17392. (interactive "P")
  17393. (cond
  17394. ((run-hook-with-args-until-success 'org-metaup-hook))
  17395. ((org-region-active-p)
  17396. (let* ((a (min (region-beginning) (region-end)))
  17397. (b (1- (max (region-beginning) (region-end))))
  17398. (c (save-excursion (goto-char a)
  17399. (move-beginning-of-line 0)))
  17400. (d (save-excursion (goto-char a)
  17401. (move-end-of-line 0) (point))))
  17402. (transpose-regions a b c d)
  17403. (goto-char c)))
  17404. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17405. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17406. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17407. (t (org-drag-element-backward))))
  17408. (defun org-metadown (&optional arg)
  17409. "Move subtree down or move table row down.
  17410. Calls `org-move-subtree-down' or `org-table-move-row' or
  17411. `org-move-item-down', depending on context. See the individual
  17412. commands for more information."
  17413. (interactive "P")
  17414. (cond
  17415. ((run-hook-with-args-until-success 'org-metadown-hook))
  17416. ((org-region-active-p)
  17417. (let* ((a (min (region-beginning) (region-end)))
  17418. (b (max (region-beginning) (region-end)))
  17419. (c (save-excursion (goto-char b)
  17420. (move-beginning-of-line 1)))
  17421. (d (save-excursion (goto-char b)
  17422. (move-end-of-line 1) (1+ (point)))))
  17423. (transpose-regions a b c d)
  17424. (goto-char d)))
  17425. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17426. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17427. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17428. (t (org-drag-element-forward))))
  17429. (defun org-shiftup (&optional arg)
  17430. "Increase item in timestamp or increase priority of current headline.
  17431. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17432. depending on context. See the individual commands for more information."
  17433. (interactive "P")
  17434. (cond
  17435. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17436. ((and org-support-shift-select (org-region-active-p))
  17437. (org-call-for-shift-select 'previous-line))
  17438. ((org-at-timestamp-p t)
  17439. (call-interactively (if org-edit-timestamp-down-means-later
  17440. 'org-timestamp-down 'org-timestamp-up)))
  17441. ((and (not (eq org-support-shift-select 'always))
  17442. org-enable-priority-commands
  17443. (org-at-heading-p))
  17444. (call-interactively 'org-priority-up))
  17445. ((and (not org-support-shift-select) (org-at-item-p))
  17446. (call-interactively 'org-previous-item))
  17447. ((org-clocktable-try-shift 'up arg))
  17448. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17449. (org-support-shift-select
  17450. (org-call-for-shift-select 'previous-line))
  17451. (t (org-shiftselect-error))))
  17452. (defun org-shiftdown (&optional arg)
  17453. "Decrease item in timestamp or decrease priority of current headline.
  17454. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17455. depending on context. See the individual commands for more information."
  17456. (interactive "P")
  17457. (cond
  17458. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17459. ((and org-support-shift-select (org-region-active-p))
  17460. (org-call-for-shift-select 'next-line))
  17461. ((org-at-timestamp-p t)
  17462. (call-interactively (if org-edit-timestamp-down-means-later
  17463. 'org-timestamp-up 'org-timestamp-down)))
  17464. ((and (not (eq org-support-shift-select 'always))
  17465. org-enable-priority-commands
  17466. (org-at-heading-p))
  17467. (call-interactively 'org-priority-down))
  17468. ((and (not org-support-shift-select) (org-at-item-p))
  17469. (call-interactively 'org-next-item))
  17470. ((org-clocktable-try-shift 'down arg))
  17471. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17472. (org-support-shift-select
  17473. (org-call-for-shift-select 'next-line))
  17474. (t (org-shiftselect-error))))
  17475. (defun org-shiftright (&optional arg)
  17476. "Cycle the thing at point or in the current line, depending on context.
  17477. Depending on context, this does one of the following:
  17478. - switch a timestamp at point one day into the future
  17479. - on a headline, switch to the next TODO keyword.
  17480. - on an item, switch entire list to the next bullet type
  17481. - on a property line, switch to the next allowed value
  17482. - on a clocktable definition line, move time block into the future"
  17483. (interactive "P")
  17484. (cond
  17485. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17486. ((and org-support-shift-select (org-region-active-p))
  17487. (org-call-for-shift-select 'forward-char))
  17488. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17489. ((and (not (eq org-support-shift-select 'always))
  17490. (org-at-heading-p))
  17491. (let ((org-inhibit-logging
  17492. (not org-treat-S-cursor-todo-selection-as-state-change))
  17493. (org-inhibit-blocking
  17494. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17495. (org-call-with-arg 'org-todo 'right)))
  17496. ((or (and org-support-shift-select
  17497. (not (eq org-support-shift-select 'always))
  17498. (org-at-item-bullet-p))
  17499. (and (not org-support-shift-select) (org-at-item-p)))
  17500. (org-call-with-arg 'org-cycle-list-bullet nil))
  17501. ((and (not (eq org-support-shift-select 'always))
  17502. (org-at-property-p))
  17503. (call-interactively 'org-property-next-allowed-value))
  17504. ((org-clocktable-try-shift 'right arg))
  17505. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17506. (org-support-shift-select
  17507. (org-call-for-shift-select 'forward-char))
  17508. (t (org-shiftselect-error))))
  17509. (defun org-shiftleft (&optional arg)
  17510. "Cycle the thing at point or in the current line, depending on context.
  17511. Depending on context, this does one of the following:
  17512. - switch a timestamp at point one day into the past
  17513. - on a headline, switch to the previous TODO keyword.
  17514. - on an item, switch entire list to the previous bullet type
  17515. - on a property line, switch to the previous allowed value
  17516. - on a clocktable definition line, move time block into the past"
  17517. (interactive "P")
  17518. (cond
  17519. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17520. ((and org-support-shift-select (org-region-active-p))
  17521. (org-call-for-shift-select 'backward-char))
  17522. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  17523. ((and (not (eq org-support-shift-select 'always))
  17524. (org-at-heading-p))
  17525. (let ((org-inhibit-logging
  17526. (not org-treat-S-cursor-todo-selection-as-state-change))
  17527. (org-inhibit-blocking
  17528. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17529. (org-call-with-arg 'org-todo 'left)))
  17530. ((or (and org-support-shift-select
  17531. (not (eq org-support-shift-select 'always))
  17532. (org-at-item-bullet-p))
  17533. (and (not org-support-shift-select) (org-at-item-p)))
  17534. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  17535. ((and (not (eq org-support-shift-select 'always))
  17536. (org-at-property-p))
  17537. (call-interactively 'org-property-previous-allowed-value))
  17538. ((org-clocktable-try-shift 'left arg))
  17539. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17540. (org-support-shift-select
  17541. (org-call-for-shift-select 'backward-char))
  17542. (t (org-shiftselect-error))))
  17543. (defun org-shiftcontrolright ()
  17544. "Switch to next TODO set."
  17545. (interactive)
  17546. (cond
  17547. ((and org-support-shift-select (org-region-active-p))
  17548. (org-call-for-shift-select 'forward-word))
  17549. ((and (not (eq org-support-shift-select 'always))
  17550. (org-at-heading-p))
  17551. (org-call-with-arg 'org-todo 'nextset))
  17552. (org-support-shift-select
  17553. (org-call-for-shift-select 'forward-word))
  17554. (t (org-shiftselect-error))))
  17555. (defun org-shiftcontrolleft ()
  17556. "Switch to previous TODO set."
  17557. (interactive)
  17558. (cond
  17559. ((and org-support-shift-select (org-region-active-p))
  17560. (org-call-for-shift-select 'backward-word))
  17561. ((and (not (eq org-support-shift-select 'always))
  17562. (org-at-heading-p))
  17563. (org-call-with-arg 'org-todo 'previousset))
  17564. (org-support-shift-select
  17565. (org-call-for-shift-select 'backward-word))
  17566. (t (org-shiftselect-error))))
  17567. (defun org-shiftcontrolup ()
  17568. "Change timestamps synchronously up in CLOCK log lines."
  17569. (interactive)
  17570. (cond ((and (not org-support-shift-select)
  17571. (org-at-clock-log-p)
  17572. (org-at-timestamp-p t))
  17573. (org-clock-timestamps-up))
  17574. (t (org-shiftselect-error))))
  17575. (defun org-shiftcontroldown ()
  17576. "Change timestamps synchronously down in CLOCK log lines."
  17577. (interactive)
  17578. (cond ((and (not org-support-shift-select)
  17579. (org-at-clock-log-p)
  17580. (org-at-timestamp-p t))
  17581. (org-clock-timestamps-down))
  17582. (t (org-shiftselect-error))))
  17583. (defun org-ctrl-c-ret ()
  17584. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17585. (interactive)
  17586. (cond
  17587. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17588. (t (call-interactively 'org-insert-heading))))
  17589. (defun org-find-visible ()
  17590. (let ((s (point)))
  17591. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17592. (get-char-property s 'invisible)))
  17593. s))
  17594. (defun org-find-invisible ()
  17595. (let ((s (point)))
  17596. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17597. (not (get-char-property s 'invisible))))
  17598. s))
  17599. (defun org-copy-visible (beg end)
  17600. "Copy the visible parts of the region."
  17601. (interactive "r")
  17602. (let (snippets s)
  17603. (save-excursion
  17604. (save-restriction
  17605. (narrow-to-region beg end)
  17606. (setq s (goto-char (point-min)))
  17607. (while (not (= (point) (point-max)))
  17608. (goto-char (org-find-invisible))
  17609. (push (buffer-substring s (point)) snippets)
  17610. (setq s (goto-char (org-find-visible))))))
  17611. (kill-new (apply 'concat (nreverse snippets)))))
  17612. (defun org-copy-special ()
  17613. "Copy region in table or copy current subtree.
  17614. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  17615. See the individual commands for more information."
  17616. (interactive)
  17617. (call-interactively
  17618. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  17619. (defun org-cut-special ()
  17620. "Cut region in table or cut current subtree.
  17621. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  17622. See the individual commands for more information."
  17623. (interactive)
  17624. (call-interactively
  17625. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  17626. (defun org-paste-special (arg)
  17627. "Paste rectangular region into table, or past subtree relative to level.
  17628. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  17629. See the individual commands for more information."
  17630. (interactive "P")
  17631. (if (org-at-table-p)
  17632. (org-table-paste-rectangle)
  17633. (org-paste-subtree arg)))
  17634. (defun org-edit-special (&optional arg)
  17635. "Call a special editor for the stuff at point.
  17636. When at a table, call the formula editor with `org-table-edit-formulas'.
  17637. When at the first line of an src example, call `org-edit-src-code'.
  17638. When in an #+include line, visit the include file. Otherwise call
  17639. `ffap' to visit the file at point."
  17640. (interactive)
  17641. ;; possibly prep session before editing source
  17642. (when arg
  17643. (let* ((info (org-babel-get-src-block-info))
  17644. (lang (nth 0 info))
  17645. (params (nth 2 info))
  17646. (session (cdr (assoc :session params))))
  17647. (when (and info session) ;; we are in a source-code block with a session
  17648. (funcall
  17649. (intern (concat "org-babel-prep-session:" lang)) session params))))
  17650. (cond ;; proceed with `org-edit-special'
  17651. ((save-excursion
  17652. (beginning-of-line 1)
  17653. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  17654. (find-file (org-trim (match-string 1))))
  17655. ((org-edit-src-code))
  17656. ((org-edit-fixed-width-region))
  17657. ((org-at-table.el-p)
  17658. (org-edit-src-code))
  17659. ((or (org-at-table-p)
  17660. (save-excursion
  17661. (beginning-of-line 1)
  17662. (let ((case-fold-search )) (looking-at "[ \t]*#\\+tblfm:"))))
  17663. (call-interactively 'org-table-edit-formulas))
  17664. (t (call-interactively 'ffap))))
  17665. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  17666. (defun org-ctrl-c-ctrl-c (&optional arg)
  17667. "Set tags in headline, or update according to changed information at point.
  17668. This command does many different things, depending on context:
  17669. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  17670. this is what we do.
  17671. - If the cursor is on a statistics cookie, update it.
  17672. - If the cursor is in a headline, prompt for tags and insert them
  17673. into the current line, aligned to `org-tags-column'. When called
  17674. with prefix arg, realign all tags in the current buffer.
  17675. - If the cursor is in one of the special #+KEYWORD lines, this
  17676. triggers scanning the buffer for these lines and updating the
  17677. information.
  17678. - If the cursor is inside a table, realign the table. This command
  17679. works even if the automatic table editor has been turned off.
  17680. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  17681. the entire table.
  17682. - If the cursor is at a footnote reference or definition, jump to
  17683. the corresponding definition or references, respectively.
  17684. - If the cursor is a the beginning of a dynamic block, update it.
  17685. - If the current buffer is a capture buffer, close note and file it.
  17686. - If the cursor is on a <<<target>>>, update radio targets and
  17687. corresponding links in this buffer.
  17688. - If the cursor is on a numbered item in a plain list, renumber the
  17689. ordered list.
  17690. - If the cursor is on a checkbox, toggle it.
  17691. - If the cursor is on a code block, evaluate it. The variable
  17692. `org-confirm-babel-evaluate' can be used to control prompting
  17693. before code block evaluation, by default every code block
  17694. evaluation requires confirmation. Code block evaluation can be
  17695. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  17696. (interactive "P")
  17697. (let ((org-enable-table-editor t))
  17698. (cond
  17699. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  17700. org-occur-highlights
  17701. org-latex-fragment-image-overlays)
  17702. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  17703. (org-remove-occur-highlights)
  17704. (org-remove-latex-fragment-image-overlays)
  17705. (message "Temporary highlights/overlays removed from current buffer"))
  17706. ((and (local-variable-p 'org-finish-function (current-buffer))
  17707. (fboundp org-finish-function))
  17708. (funcall org-finish-function))
  17709. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  17710. ((org-in-regexp org-ts-regexp-both)
  17711. (org-timestamp-change 0 'day))
  17712. ((or (looking-at org-property-start-re)
  17713. (org-at-property-p))
  17714. (call-interactively 'org-property-action))
  17715. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  17716. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  17717. (or (org-at-heading-p) (org-at-item-p)))
  17718. (call-interactively 'org-update-statistics-cookies))
  17719. ((org-at-heading-p) (call-interactively 'org-set-tags))
  17720. ((org-at-table.el-p)
  17721. (message "Use C-c ' to edit table.el tables"))
  17722. ((org-at-table-p)
  17723. (org-table-maybe-eval-formula)
  17724. (if arg
  17725. (call-interactively 'org-table-recalculate)
  17726. (org-table-maybe-recalculate-line))
  17727. (call-interactively 'org-table-align)
  17728. (orgtbl-send-table 'maybe))
  17729. ((or (org-footnote-at-reference-p)
  17730. (org-footnote-at-definition-p))
  17731. (call-interactively 'org-footnote-action))
  17732. ((org-at-item-checkbox-p)
  17733. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  17734. ;; list only if at top item.
  17735. (let* ((cbox (match-string 1))
  17736. (struct (org-list-struct))
  17737. (old-struct (copy-tree struct))
  17738. (parents (org-list-parents-alist struct))
  17739. (orderedp (org-entry-get nil "ORDERED"))
  17740. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17741. block-item)
  17742. ;; Use a light version of `org-toggle-checkbox' to avoid
  17743. ;; computing list structure twice.
  17744. (let ((new-box (cond
  17745. ((equal arg '(16)) "[-]")
  17746. ((equal arg '(4)) nil)
  17747. ((equal "[X]" cbox) "[ ]")
  17748. (t "[X]"))))
  17749. (if (and firstp arg)
  17750. ;; If at first item of sub-list, remove check-box from
  17751. ;; every item at the same level.
  17752. (mapc
  17753. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  17754. (org-list-get-all-items
  17755. (point-at-bol) struct (org-list-prevs-alist struct)))
  17756. (org-list-set-checkbox (point-at-bol) struct new-box)))
  17757. ;; Replicate `org-list-write-struct', while grabbing a return
  17758. ;; value from `org-list-struct-fix-box'.
  17759. (org-list-struct-fix-ind struct parents 2)
  17760. (org-list-struct-fix-item-end struct)
  17761. (let ((prevs (org-list-prevs-alist struct)))
  17762. (org-list-struct-fix-bul struct prevs)
  17763. (org-list-struct-fix-ind struct parents)
  17764. (setq block-item
  17765. (org-list-struct-fix-box struct parents prevs orderedp)))
  17766. (org-list-struct-apply-struct struct old-struct)
  17767. (org-update-checkbox-count-maybe)
  17768. (when block-item
  17769. (message
  17770. "Checkboxes were removed due to unchecked box at line %d"
  17771. (org-current-line block-item)))
  17772. (when firstp (org-list-send-list 'maybe))))
  17773. ((org-at-item-p)
  17774. ;; Cursor at an item: repair list. Do checkbox related actions
  17775. ;; only if function was called with an argument. Send list only
  17776. ;; if at top item.
  17777. (let* ((struct (org-list-struct))
  17778. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17779. old-struct)
  17780. (when arg
  17781. (setq old-struct (copy-tree struct))
  17782. (if firstp
  17783. ;; If at first item of sub-list, add check-box to every
  17784. ;; item at the same level.
  17785. (mapc
  17786. (lambda (pos)
  17787. (unless (org-list-get-checkbox pos struct)
  17788. (org-list-set-checkbox pos struct "[ ]")))
  17789. (org-list-get-all-items
  17790. (point-at-bol) struct (org-list-prevs-alist struct)))
  17791. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  17792. (org-list-write-struct
  17793. struct (org-list-parents-alist struct) old-struct)
  17794. (when arg (org-update-checkbox-count-maybe))
  17795. (when firstp (org-list-send-list 'maybe))))
  17796. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  17797. ;; Dynamic block
  17798. (beginning-of-line 1)
  17799. (save-excursion (org-update-dblock)))
  17800. ((save-excursion
  17801. (let ((case-fold-search t))
  17802. (beginning-of-line 1)
  17803. (looking-at "[ \t]*#\\+\\([a-z]+\\)")))
  17804. (cond
  17805. ((or (equal (match-string 1) "TBLFM")
  17806. (equal (match-string 1) "tblfm"))
  17807. ;; Recalculate the table before this line
  17808. (save-excursion
  17809. (beginning-of-line 1)
  17810. (skip-chars-backward " \r\n\t")
  17811. (if (org-at-table-p)
  17812. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  17813. (t
  17814. (let ((org-inhibit-startup-visibility-stuff t)
  17815. (org-startup-align-all-tables nil))
  17816. (when (boundp 'org-table-coordinate-overlays)
  17817. (mapc 'delete-overlay org-table-coordinate-overlays)
  17818. (setq org-table-coordinate-overlays nil))
  17819. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  17820. (message "Local setup has been refreshed"))))
  17821. ((org-clock-update-time-maybe))
  17822. (t
  17823. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17824. (error "C-c C-c can do nothing useful at this location"))))))
  17825. (defun org-mode-restart ()
  17826. "Restart Org-mode, to scan again for special lines.
  17827. Also updates the keyword regular expressions."
  17828. (interactive)
  17829. (org-mode)
  17830. (message "Org-mode restarted"))
  17831. (defun org-kill-note-or-show-branches ()
  17832. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  17833. (interactive)
  17834. (if (not org-finish-function)
  17835. (progn
  17836. (hide-subtree)
  17837. (call-interactively 'show-branches))
  17838. (let ((org-note-abort t))
  17839. (funcall org-finish-function))))
  17840. (defun org-return (&optional indent)
  17841. "Goto next table row or insert a newline.
  17842. Calls `org-table-next-row' or `newline', depending on context.
  17843. See the individual commands for more information."
  17844. (interactive)
  17845. (let (org-ts-what)
  17846. (cond
  17847. ((or (bobp) (org-in-src-block-p))
  17848. (if indent (newline-and-indent) (newline)))
  17849. ((org-at-table-p)
  17850. (org-table-justify-field-maybe)
  17851. (call-interactively 'org-table-next-row))
  17852. ;; when `newline-and-indent' is called within a list, make sure
  17853. ;; text moved stays inside the item.
  17854. ((and (org-in-item-p) indent)
  17855. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  17856. (progn
  17857. (save-match-data (newline))
  17858. (org-indent-line-to (length (match-string 0))))
  17859. (let ((ind (org-get-indentation)))
  17860. (newline)
  17861. (if (org-looking-back org-list-end-re)
  17862. (org-indent-line)
  17863. (org-indent-line-to ind)))))
  17864. ((and org-return-follows-link
  17865. (org-at-timestamp-p t)
  17866. (not (eq org-ts-what 'after)))
  17867. (org-follow-timestamp-link))
  17868. ((and org-return-follows-link
  17869. (let ((tprop (get-text-property (point) 'face)))
  17870. (or (eq tprop 'org-link)
  17871. (and (listp tprop) (memq 'org-link tprop)))))
  17872. (call-interactively 'org-open-at-point))
  17873. ((and (org-at-heading-p)
  17874. (looking-at
  17875. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  17876. (org-show-entry)
  17877. (end-of-line 1)
  17878. (newline))
  17879. (t (if indent (newline-and-indent) (newline))))))
  17880. (defun org-return-indent ()
  17881. "Goto next table row or insert a newline and indent.
  17882. Calls `org-table-next-row' or `newline-and-indent', depending on
  17883. context. See the individual commands for more information."
  17884. (interactive)
  17885. (org-return t))
  17886. (defun org-ctrl-c-star ()
  17887. "Compute table, or change heading status of lines.
  17888. Calls `org-table-recalculate' or `org-toggle-heading',
  17889. depending on context."
  17890. (interactive)
  17891. (cond
  17892. ((org-at-table-p)
  17893. (call-interactively 'org-table-recalculate))
  17894. (t
  17895. ;; Convert all lines in region to list items
  17896. (call-interactively 'org-toggle-heading))))
  17897. (defun org-ctrl-c-minus ()
  17898. "Insert separator line in table or modify bullet status of line.
  17899. Also turns a plain line or a region of lines into list items.
  17900. Calls `org-table-insert-hline', `org-toggle-item', or
  17901. `org-cycle-list-bullet', depending on context."
  17902. (interactive)
  17903. (cond
  17904. ((org-at-table-p)
  17905. (call-interactively 'org-table-insert-hline))
  17906. ((org-region-active-p)
  17907. (call-interactively 'org-toggle-item))
  17908. ((org-in-item-p)
  17909. (call-interactively 'org-cycle-list-bullet))
  17910. (t
  17911. (call-interactively 'org-toggle-item))))
  17912. (defun org-toggle-item (arg)
  17913. "Convert headings or normal lines to items, items to normal lines.
  17914. If there is no active region, only the current line is considered.
  17915. If the first non blank line in the region is an headline, convert
  17916. all headlines to items, shifting text accordingly.
  17917. If it is an item, convert all items to normal lines.
  17918. If it is normal text, change region into an item. With a prefix
  17919. argument ARG, change each line in region into an item."
  17920. (interactive "P")
  17921. (let ((shift-text
  17922. (function
  17923. ;; Shift text in current section to IND, from point to END.
  17924. ;; The function leaves point to END line.
  17925. (lambda (ind end)
  17926. (let ((min-i 1000) (end (copy-marker end)))
  17927. ;; First determine the minimum indentation (MIN-I) of
  17928. ;; the text.
  17929. (save-excursion
  17930. (catch 'exit
  17931. (while (< (point) end)
  17932. (let ((i (org-get-indentation)))
  17933. (cond
  17934. ;; Skip blank lines and inline tasks.
  17935. ((looking-at "^[ \t]*$"))
  17936. ((looking-at org-outline-regexp-bol))
  17937. ;; We can't find less than 0 indentation.
  17938. ((zerop i) (throw 'exit (setq min-i 0)))
  17939. ((< i min-i) (setq min-i i))))
  17940. (forward-line))))
  17941. ;; Then indent each line so that a line indented to
  17942. ;; MIN-I becomes indented to IND. Ignore blank lines
  17943. ;; and inline tasks in the process.
  17944. (let ((delta (- ind min-i)))
  17945. (while (< (point) end)
  17946. (unless (or (looking-at "^[ \t]*$")
  17947. (looking-at org-outline-regexp-bol))
  17948. (org-indent-line-to (+ (org-get-indentation) delta)))
  17949. (forward-line)))))))
  17950. (skip-blanks
  17951. (function
  17952. ;; Return beginning of first non-blank line, starting from
  17953. ;; line at POS.
  17954. (lambda (pos)
  17955. (save-excursion
  17956. (goto-char pos)
  17957. (skip-chars-forward " \r\t\n")
  17958. (point-at-bol)))))
  17959. beg end)
  17960. ;; Determine boundaries of changes.
  17961. (if (org-region-active-p)
  17962. (setq beg (funcall skip-blanks (region-beginning))
  17963. end (copy-marker (region-end)))
  17964. (setq beg (funcall skip-blanks (point-at-bol))
  17965. end (copy-marker (point-at-eol))))
  17966. ;; Depending on the starting line, choose an action on the text
  17967. ;; between BEG and END.
  17968. (org-with-limited-levels
  17969. (save-excursion
  17970. (goto-char beg)
  17971. (cond
  17972. ;; Case 1. Start at an item: de-itemize. Note that it only
  17973. ;; happens when a region is active: `org-ctrl-c-minus'
  17974. ;; would call `org-cycle-list-bullet' otherwise.
  17975. ((org-at-item-p)
  17976. (while (< (point) end)
  17977. (when (org-at-item-p)
  17978. (skip-chars-forward " \t")
  17979. (delete-region (point) (match-end 0)))
  17980. (forward-line)))
  17981. ;; Case 2. Start at an heading: convert to items.
  17982. ((org-at-heading-p)
  17983. (let* ((bul (org-list-bullet-string "-"))
  17984. (bul-len (length bul))
  17985. ;; Indentation of the first heading. It should be
  17986. ;; relative to the indentation of its parent, if any.
  17987. (start-ind (save-excursion
  17988. (cond
  17989. ((not org-adapt-indentation) 0)
  17990. ((not (outline-previous-heading)) 0)
  17991. (t (length (match-string 0))))))
  17992. ;; Level of first heading. Further headings will be
  17993. ;; compared to it to determine hierarchy in the list.
  17994. (ref-level (org-reduced-level (org-outline-level))))
  17995. (while (< (point) end)
  17996. (let* ((level (org-reduced-level (org-outline-level)))
  17997. (delta (max 0 (- level ref-level))))
  17998. ;; If current headline is less indented than the first
  17999. ;; one, set it as reference, in order to preserve
  18000. ;; subtrees.
  18001. (when (< level ref-level) (setq ref-level level))
  18002. (replace-match bul t t)
  18003. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18004. ;; Ensure all text down to END (or SECTION-END) belongs
  18005. ;; to the newly created item.
  18006. (let ((section-end (save-excursion
  18007. (or (outline-next-heading) (point)))))
  18008. (forward-line)
  18009. (funcall shift-text
  18010. (+ start-ind (* (1+ delta) bul-len))
  18011. (min end section-end)))))))
  18012. ;; Case 3. Normal line with ARG: turn each non-item line into
  18013. ;; an item.
  18014. (arg
  18015. (while (< (point) end)
  18016. (unless (or (org-at-heading-p) (org-at-item-p))
  18017. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18018. (replace-match
  18019. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18020. (forward-line)))
  18021. ;; Case 4. Normal line without ARG: make the first line of
  18022. ;; region an item, and shift indentation of others
  18023. ;; lines to set them as item's body.
  18024. (t (let* ((bul (org-list-bullet-string "-"))
  18025. (bul-len (length bul))
  18026. (ref-ind (org-get-indentation)))
  18027. (skip-chars-forward " \t")
  18028. (insert bul)
  18029. (forward-line)
  18030. (while (< (point) end)
  18031. ;; Ensure that lines less indented than first one
  18032. ;; still get included in item body.
  18033. (funcall shift-text
  18034. (+ ref-ind bul-len)
  18035. (min end (save-excursion (or (outline-next-heading)
  18036. (point)))))
  18037. (forward-line)))))))))
  18038. (defun org-toggle-heading (&optional nstars)
  18039. "Convert headings to normal text, or items or text to headings.
  18040. If there is no active region, only the current line is considered.
  18041. With a \\[universal-argument] prefix, convert the whole list at
  18042. point into heading.
  18043. In a region:
  18044. - If the first non blank line is an headline, remove the stars
  18045. from all headlines in the region.
  18046. - If it is a normal line turn each and every normal line (i.e. not an
  18047. heading or an item) in the region into a heading.
  18048. - If it is a plain list item, turn all plain list items into headings.
  18049. When converting a line into a heading, the number of stars is chosen
  18050. such that the lines become children of the current entry. However,
  18051. when a prefix argument is given, its value determines the number of
  18052. stars to add."
  18053. (interactive "P")
  18054. (let ((skip-blanks
  18055. (function
  18056. ;; Return beginning of first non-blank line, starting from
  18057. ;; line at POS.
  18058. (lambda (pos)
  18059. (save-excursion
  18060. (goto-char pos)
  18061. (while (org-at-comment-p) (forward-line))
  18062. (skip-chars-forward " \r\t\n")
  18063. (point-at-bol)))))
  18064. beg end toggled)
  18065. ;; Determine boundaries of changes. If a universal prefix has
  18066. ;; been given, put the list in a region. If region ends at a bol,
  18067. ;; do not consider the last line to be in the region.
  18068. (when (and current-prefix-arg (org-at-item-p))
  18069. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  18070. (org-mark-element))
  18071. (if (org-region-active-p)
  18072. (setq beg (funcall skip-blanks (region-beginning))
  18073. end (copy-marker (save-excursion
  18074. (goto-char (region-end))
  18075. (if (bolp) (point) (point-at-eol)))))
  18076. (setq beg (funcall skip-blanks (point-at-bol))
  18077. end (copy-marker (point-at-eol))))
  18078. ;; Ensure inline tasks don't count as headings.
  18079. (org-with-limited-levels
  18080. (save-excursion
  18081. (goto-char beg)
  18082. (cond
  18083. ;; Case 1. Started at an heading: de-star headings.
  18084. ((org-at-heading-p)
  18085. (while (< (point) end)
  18086. (when (org-at-heading-p t)
  18087. (looking-at org-outline-regexp) (replace-match "")
  18088. (setq toggled t))
  18089. (forward-line)))
  18090. ;; Case 2. Started at an item: change items into headlines.
  18091. ;; One star will be added by `org-list-to-subtree'.
  18092. ((org-at-item-p)
  18093. (let* ((stars (make-string
  18094. (if nstars
  18095. ;; subtract the star that will be added again by
  18096. ;; `org-list-to-subtree'
  18097. (1- (prefix-numeric-value current-prefix-arg))
  18098. (or (org-current-level) 0))
  18099. ?*))
  18100. (add-stars
  18101. (cond (nstars "") ; stars from prefix only
  18102. ((equal stars "") "") ; before first heading
  18103. (org-odd-levels-only "*") ; inside heading, odd
  18104. (t "")))) ; inside heading, oddeven
  18105. (while (< (point) end)
  18106. (when (org-at-item-p)
  18107. ;; Pay attention to cases when region ends before list.
  18108. (let* ((struct (org-list-struct))
  18109. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18110. (save-restriction
  18111. (narrow-to-region (point) list-end)
  18112. (insert
  18113. (org-list-to-subtree
  18114. (org-list-parse-list t)
  18115. '(:istart (concat stars add-stars (funcall get-stars depth))
  18116. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18117. (setq toggled t))
  18118. (forward-line))))
  18119. ;; Case 3. Started at normal text: make every line an heading,
  18120. ;; skipping headlines and items.
  18121. (t (let* ((stars (make-string
  18122. (if nstars
  18123. (prefix-numeric-value current-prefix-arg)
  18124. (or (org-current-level) 0))
  18125. ?*))
  18126. (add-stars
  18127. (cond (nstars "") ; stars from prefix only
  18128. ((equal stars "") "*") ; before first heading
  18129. (org-odd-levels-only "**") ; inside heading, odd
  18130. (t "*"))) ; inside heading, oddeven
  18131. (rpl (concat stars add-stars " ")))
  18132. (while (< (point) end)
  18133. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18134. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18135. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18136. (forward-line)))))))
  18137. (unless toggled (message "Cannot toggle heading from here"))))
  18138. (defun org-meta-return (&optional arg)
  18139. "Insert a new heading or wrap a region in a table.
  18140. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18141. See the individual commands for more information."
  18142. (interactive "P")
  18143. (cond
  18144. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18145. ((or (org-at-drawer-p) (org-at-property-p))
  18146. (newline-and-indent))
  18147. ((org-at-table-p)
  18148. (call-interactively 'org-table-wrap-region))
  18149. (t (call-interactively 'org-insert-heading))))
  18150. ;;; Menu entries
  18151. (defsubst org-in-subtree-not-table-p ()
  18152. "Are we in a subtree and not in a table?"
  18153. (and (not (org-before-first-heading-p))
  18154. (not (org-at-table-p))))
  18155. ;; Define the Org-mode menus
  18156. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18157. '("Tbl"
  18158. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18159. ["Next Field" org-cycle (org-at-table-p)]
  18160. ["Previous Field" org-shifttab (org-at-table-p)]
  18161. ["Next Row" org-return (org-at-table-p)]
  18162. "--"
  18163. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18164. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18165. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18166. "--"
  18167. ("Column"
  18168. ["Move Column Left" org-metaleft (org-at-table-p)]
  18169. ["Move Column Right" org-metaright (org-at-table-p)]
  18170. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18171. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18172. ("Row"
  18173. ["Move Row Up" org-metaup (org-at-table-p)]
  18174. ["Move Row Down" org-metadown (org-at-table-p)]
  18175. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18176. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18177. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18178. "--"
  18179. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18180. ("Rectangle"
  18181. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18182. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18183. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18184. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18185. "--"
  18186. ("Calculate"
  18187. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18188. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18189. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18190. "--"
  18191. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18192. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18193. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18194. "--"
  18195. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18196. "--"
  18197. ["Sum Column/Rectangle" org-table-sum
  18198. (or (org-at-table-p) (org-region-active-p))]
  18199. ["Which Column?" org-table-current-column (org-at-table-p)])
  18200. ["Debug Formulas"
  18201. org-table-toggle-formula-debugger
  18202. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18203. ["Show Col/Row Numbers"
  18204. org-table-toggle-coordinate-overlays
  18205. :style toggle
  18206. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18207. "--"
  18208. ["Create" org-table-create (and (not (org-at-table-p))
  18209. org-enable-table-editor)]
  18210. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18211. ["Import from File" org-table-import (not (org-at-table-p))]
  18212. ["Export to File" org-table-export (org-at-table-p)]
  18213. "--"
  18214. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18215. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18216. '("Org"
  18217. ("Show/Hide"
  18218. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18219. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18220. ["Sparse Tree..." org-sparse-tree t]
  18221. ["Reveal Context" org-reveal t]
  18222. ["Show All" show-all t]
  18223. "--"
  18224. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18225. "--"
  18226. ["New Heading" org-insert-heading t]
  18227. ("Navigate Headings"
  18228. ["Up" outline-up-heading t]
  18229. ["Next" outline-next-visible-heading t]
  18230. ["Previous" outline-previous-visible-heading t]
  18231. ["Next Same Level" outline-forward-same-level t]
  18232. ["Previous Same Level" outline-backward-same-level t]
  18233. "--"
  18234. ["Jump" org-goto t])
  18235. ("Edit Structure"
  18236. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18237. "--"
  18238. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18239. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18240. "--"
  18241. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18242. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18243. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18244. "--"
  18245. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18246. "--"
  18247. ["Copy visible text" org-copy-visible t]
  18248. "--"
  18249. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18250. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18251. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18252. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18253. "--"
  18254. ["Sort Region/Children" org-sort t]
  18255. "--"
  18256. ["Convert to odd levels" org-convert-to-odd-levels t]
  18257. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18258. ("Editing"
  18259. ["Emphasis..." org-emphasize t]
  18260. ["Edit Source Example" org-edit-special t]
  18261. "--"
  18262. ["Footnote new/jump" org-footnote-action t]
  18263. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18264. ("Archive"
  18265. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18266. "--"
  18267. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18268. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18269. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18270. )
  18271. "--"
  18272. ("Hyperlinks"
  18273. ["Store Link (Global)" org-store-link t]
  18274. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18275. ["Insert Link" org-insert-link t]
  18276. ["Follow Link" org-open-at-point t]
  18277. "--"
  18278. ["Next link" org-next-link t]
  18279. ["Previous link" org-previous-link t]
  18280. "--"
  18281. ["Descriptive Links"
  18282. org-toggle-link-display
  18283. :style radio
  18284. :selected org-descriptive-links
  18285. ]
  18286. ["Literal Links"
  18287. org-toggle-link-display
  18288. :style radio
  18289. :selected (not org-descriptive-links)])
  18290. "--"
  18291. ("TODO Lists"
  18292. ["TODO/DONE/-" org-todo t]
  18293. ("Select keyword"
  18294. ["Next keyword" org-shiftright (org-at-heading-p)]
  18295. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18296. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18297. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18298. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18299. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18300. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18301. "--"
  18302. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18303. :selected org-enforce-todo-dependencies :style toggle :active t]
  18304. "Settings for tree at point"
  18305. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18306. :selected (org-entry-get nil "ORDERED")
  18307. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18308. ["Do Children parallel" org-toggle-ordered-property :style radio
  18309. :selected (not (org-entry-get nil "ORDERED"))
  18310. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18311. "--"
  18312. ["Set Priority" org-priority t]
  18313. ["Priority Up" org-shiftup t]
  18314. ["Priority Down" org-shiftdown t]
  18315. "--"
  18316. ["Get news from all feeds" org-feed-update-all t]
  18317. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18318. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18319. ("TAGS and Properties"
  18320. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18321. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18322. "--"
  18323. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18324. ["Column view of properties" org-columns t]
  18325. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18326. ("Dates and Scheduling"
  18327. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18328. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18329. ("Change Date"
  18330. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18331. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18332. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18333. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18334. ["Compute Time Range" org-evaluate-time-range t]
  18335. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18336. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18337. "--"
  18338. ["Custom time format" org-toggle-time-stamp-overlays
  18339. :style radio :selected org-display-custom-times]
  18340. "--"
  18341. ["Goto Calendar" org-goto-calendar t]
  18342. ["Date from Calendar" org-date-from-calendar t]
  18343. "--"
  18344. ["Start/Restart Timer" org-timer-start t]
  18345. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18346. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18347. ["Insert Timer String" org-timer t]
  18348. ["Insert Timer Item" org-timer-item t])
  18349. ("Logging work"
  18350. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18351. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18352. ["Clock out" org-clock-out t]
  18353. ["Clock cancel" org-clock-cancel t]
  18354. "--"
  18355. ["Mark as default task" org-clock-mark-default-task t]
  18356. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18357. ["Goto running clock" org-clock-goto t]
  18358. "--"
  18359. ["Display times" org-clock-display t]
  18360. ["Create clock table" org-clock-report t]
  18361. "--"
  18362. ["Record DONE time"
  18363. (progn (setq org-log-done (not org-log-done))
  18364. (message "Switching to %s will %s record a timestamp"
  18365. (car org-done-keywords)
  18366. (if org-log-done "automatically" "not")))
  18367. :style toggle :selected org-log-done])
  18368. "--"
  18369. ["Agenda Command..." org-agenda t]
  18370. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18371. ("File List for Agenda")
  18372. ("Special views current file"
  18373. ["TODO Tree" org-show-todo-tree t]
  18374. ["Check Deadlines" org-check-deadlines t]
  18375. ["Timeline" org-timeline t]
  18376. ["Tags/Property tree" org-match-sparse-tree t])
  18377. "--"
  18378. ["Export/Publish..." org-export t]
  18379. ("LaTeX"
  18380. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18381. :selected org-cdlatex-mode]
  18382. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18383. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18384. ["Modify math symbol" org-cdlatex-math-modify
  18385. (org-inside-LaTeX-fragment-p)]
  18386. ["Insert citation" org-reftex-citation t]
  18387. "--"
  18388. ["Template for BEAMER" (progn (require 'org-beamer)
  18389. (org-insert-beamer-options-template)) t])
  18390. "--"
  18391. ("MobileOrg"
  18392. ["Push Files and Views" org-mobile-push t]
  18393. ["Get Captured and Flagged" org-mobile-pull t]
  18394. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18395. "--"
  18396. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18397. "--"
  18398. ("Documentation"
  18399. ["Show Version" org-version t]
  18400. ["Info Documentation" org-info t])
  18401. ("Customize"
  18402. ["Browse Org Group" org-customize t]
  18403. "--"
  18404. ["Expand This Menu" org-create-customize-menu
  18405. (fboundp 'customize-menu-create)])
  18406. ["Send bug report" org-submit-bug-report t]
  18407. "--"
  18408. ("Refresh/Reload"
  18409. ["Refresh setup current buffer" org-mode-restart t]
  18410. ["Reload Org (after update)" org-reload t]
  18411. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  18412. ))
  18413. (defun org-info (&optional node)
  18414. "Read documentation for Org-mode in the info system.
  18415. With optional NODE, go directly to that node."
  18416. (interactive)
  18417. (info (format "(org)%s" (or node ""))))
  18418. ;;;###autoload
  18419. (defun org-submit-bug-report ()
  18420. "Submit a bug report on Org-mode via mail.
  18421. Don't hesitate to report any problems or inaccurate documentation.
  18422. If you don't have setup sending mail from (X)Emacs, please copy the
  18423. output buffer into your mail program, as it gives us important
  18424. information about your Org-mode version and configuration."
  18425. (interactive)
  18426. (require 'reporter)
  18427. (org-load-modules-maybe)
  18428. (org-require-autoloaded-modules)
  18429. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18430. (reporter-submit-bug-report
  18431. "emacs-orgmode@gnu.org"
  18432. (org-version nil 'full)
  18433. (let (list)
  18434. (save-window-excursion
  18435. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18436. (delete-other-windows)
  18437. (erase-buffer)
  18438. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18439. We would like to add your full Org-mode and Outline configuration to the
  18440. bug report. This greatly simplifies the work of the maintainer and
  18441. other experts on the mailing list.
  18442. HOWEVER, some variables you have customized may contain private
  18443. information. The names of customers, colleagues, or friends, might
  18444. appear in the form of file names, tags, todo states, or search strings.
  18445. If you answer yes to the prompt, you might want to check and remove
  18446. such private information before sending the email.")
  18447. (add-text-properties (point-min) (point-max) '(face org-warning))
  18448. (when (yes-or-no-p "Include your Org-mode configuration ")
  18449. (mapatoms
  18450. (lambda (v)
  18451. (and (boundp v)
  18452. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18453. (or (and (symbol-value v)
  18454. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18455. (and
  18456. (get v 'custom-type) (get v 'standard-value)
  18457. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18458. (push v list)))))
  18459. (kill-buffer (get-buffer "*Warn about privacy*"))
  18460. list))
  18461. nil nil
  18462. "Remember to cover the basics, that is, what you expected to happen and
  18463. what in fact did happen. You don't know how to make a good report? See
  18464. http://orgmode.org/manual/Feedback.html#Feedback
  18465. Your bug report will be posted to the Org-mode mailing list.
  18466. ------------------------------------------------------------------------")
  18467. (save-excursion
  18468. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18469. (replace-match "\\1Bug: \\3 [\\2]")))))
  18470. (defun org-install-agenda-files-menu ()
  18471. (let ((bl (buffer-list)))
  18472. (save-excursion
  18473. (while bl
  18474. (set-buffer (pop bl))
  18475. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18476. (when (derived-mode-p 'org-mode)
  18477. (easy-menu-change
  18478. '("Org") "File List for Agenda"
  18479. (append
  18480. (list
  18481. ["Edit File List" (org-edit-agenda-file-list) t]
  18482. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  18483. ["Remove Current File from List" org-remove-file t]
  18484. ["Cycle through agenda files" org-cycle-agenda-files t]
  18485. ["Occur in all agenda files" org-occur-in-agenda-files t]
  18486. "--")
  18487. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  18488. ;;;; Documentation
  18489. ;;;###autoload
  18490. (defun org-require-autoloaded-modules ()
  18491. (interactive)
  18492. (mapc 'require
  18493. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  18494. org-docbook org-exp org-html org-icalendar
  18495. org-id org-latex
  18496. org-publish org-remember org-table
  18497. org-timer org-xoxo)))
  18498. ;;;###autoload
  18499. (defun org-reload (&optional uncompiled)
  18500. "Reload all org lisp files.
  18501. With prefix arg UNCOMPILED, load the uncompiled versions."
  18502. (interactive "P")
  18503. (require 'find-func)
  18504. (let* ((file-re "^org\\(-.*\\)?\\.el")
  18505. (dir-org (file-name-directory (org-find-library-dir "org")))
  18506. (dir-org-contrib (ignore-errors
  18507. (file-name-directory
  18508. (org-find-library-dir "org-contribdir"))))
  18509. (babel-files
  18510. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  18511. (append (list nil "comint" "eval" "exp" "keys"
  18512. "lob" "ref" "table" "tangle")
  18513. (delq nil
  18514. (mapcar
  18515. (lambda (lang)
  18516. (when (cdr lang) (symbol-name (car lang))))
  18517. org-babel-load-languages)))))
  18518. (files
  18519. (append babel-files
  18520. (and dir-org-contrib
  18521. (directory-files dir-org-contrib t file-re))
  18522. (directory-files dir-org t file-re)))
  18523. (remove-re (concat (if (featurep 'xemacs)
  18524. "org-colview" "org-colview-xemacs")
  18525. "\\'")))
  18526. (setq files (mapcar 'file-name-sans-extension files))
  18527. (setq files (mapcar
  18528. (lambda (x) (if (string-match remove-re x) nil x))
  18529. files))
  18530. (setq files (delq nil files))
  18531. (mapc
  18532. (lambda (f)
  18533. (when (featurep (intern (file-name-nondirectory f)))
  18534. (if (and (not uncompiled)
  18535. (file-exists-p (concat f ".elc")))
  18536. (load (concat f ".elc") nil nil 'nosuffix)
  18537. (load (concat f ".el") nil nil 'nosuffix))))
  18538. files)
  18539. (load (concat dir-org "org-version.el") 'noerror nil 'nosuffix))
  18540. (org-version nil 'full 'message))
  18541. ;;;###autoload
  18542. (defun org-customize ()
  18543. "Call the customize function with org as argument."
  18544. (interactive)
  18545. (org-load-modules-maybe)
  18546. (org-require-autoloaded-modules)
  18547. (customize-browse 'org))
  18548. (defun org-create-customize-menu ()
  18549. "Create a full customization menu for Org-mode, insert it into the menu."
  18550. (interactive)
  18551. (org-load-modules-maybe)
  18552. (org-require-autoloaded-modules)
  18553. (if (fboundp 'customize-menu-create)
  18554. (progn
  18555. (easy-menu-change
  18556. '("Org") "Customize"
  18557. `(["Browse Org group" org-customize t]
  18558. "--"
  18559. ,(customize-menu-create 'org)
  18560. ["Set" Custom-set t]
  18561. ["Save" Custom-save t]
  18562. ["Reset to Current" Custom-reset-current t]
  18563. ["Reset to Saved" Custom-reset-saved t]
  18564. ["Reset to Standard Settings" Custom-reset-standard t]))
  18565. (message "\"Org\"-menu now contains full customization menu"))
  18566. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  18567. ;;;; Miscellaneous stuff
  18568. ;;; Generally useful functions
  18569. (defun org-get-at-bol (property)
  18570. "Get text property PROPERTY at beginning of line."
  18571. (get-text-property (point-at-bol) property))
  18572. (defun org-find-text-property-in-string (prop s)
  18573. "Return the first non-nil value of property PROP in string S."
  18574. (or (get-text-property 0 prop s)
  18575. (get-text-property (or (next-single-property-change 0 prop s) 0)
  18576. prop s)))
  18577. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  18578. "Display the given MESSAGE as a warning."
  18579. (if (fboundp 'display-warning)
  18580. (display-warning 'org message
  18581. (if (featurep 'xemacs) 'warning :warning))
  18582. (let ((buf (get-buffer-create "*Org warnings*")))
  18583. (with-current-buffer buf
  18584. (goto-char (point-max))
  18585. (insert "Warning (Org): " message)
  18586. (unless (bolp)
  18587. (newline)))
  18588. (display-buffer buf)
  18589. (sit-for 0))))
  18590. (defun org-eval (form)
  18591. "Eval FORM and return result."
  18592. (condition-case error
  18593. (eval form)
  18594. (error (format "%%![Error: %s]" error))))
  18595. (defun org-in-clocktable-p ()
  18596. "Check if the cursor is in a clocktable."
  18597. (let ((pos (point)) start)
  18598. (save-excursion
  18599. (end-of-line 1)
  18600. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  18601. (setq start (match-beginning 0))
  18602. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  18603. (>= (match-end 0) pos)
  18604. start))))
  18605. (defun org-in-commented-line ()
  18606. "Is point in a line starting with `#'?"
  18607. (equal (char-after (point-at-bol)) ?#))
  18608. (defun org-in-indented-comment-line ()
  18609. "Is point in a line starting with `#' after some white space?"
  18610. (save-excursion
  18611. (save-match-data
  18612. (goto-char (point-at-bol))
  18613. (looking-at "[ \t]*#"))))
  18614. (defun org-in-verbatim-emphasis ()
  18615. (save-match-data
  18616. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  18617. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  18618. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  18619. (if (and marker (marker-buffer marker)
  18620. (buffer-live-p (marker-buffer marker)))
  18621. (progn
  18622. (org-pop-to-buffer-same-window (marker-buffer marker))
  18623. (if (or (> marker (point-max)) (< marker (point-min)))
  18624. (widen))
  18625. (goto-char marker)
  18626. (org-show-context 'org-goto))
  18627. (if bookmark
  18628. (bookmark-jump bookmark)
  18629. (error "Cannot find location"))))
  18630. (defun org-quote-csv-field (s)
  18631. "Quote field for inclusion in CSV material."
  18632. (if (string-match "[\",]" s)
  18633. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  18634. s))
  18635. (defun org-force-self-insert (N)
  18636. "Needed to enforce self-insert under remapping."
  18637. (interactive "p")
  18638. (self-insert-command N))
  18639. (defun org-string-width (s)
  18640. "Compute width of string, ignoring invisible characters.
  18641. This ignores character with invisibility property `org-link', and also
  18642. characters with property `org-cwidth', because these will become invisible
  18643. upon the next fontification round."
  18644. (let (b l)
  18645. (when (or (eq t buffer-invisibility-spec)
  18646. (assq 'org-link buffer-invisibility-spec))
  18647. (while (setq b (text-property-any 0 (length s)
  18648. 'invisible 'org-link s))
  18649. (setq s (concat (substring s 0 b)
  18650. (substring s (or (next-single-property-change
  18651. b 'invisible s) (length s)))))))
  18652. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  18653. (setq s (concat (substring s 0 b)
  18654. (substring s (or (next-single-property-change
  18655. b 'org-cwidth s) (length s))))))
  18656. (setq l (string-width s) b -1)
  18657. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  18658. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  18659. l))
  18660. (defun org-shorten-string (s maxlength)
  18661. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  18662. If the string is shorter or has length MAXLENGTH, just return the
  18663. original string. If it is longer, the functions finds a space in the
  18664. string, breaks this string off at that locations and adds three dots
  18665. as ellipsis. Including the ellipsis, the string will not be longer
  18666. than MAXLENGTH. If finding a good breaking point in the string does
  18667. not work, the string is just chopped off in the middle of a word
  18668. if necessary."
  18669. (if (<= (length s) maxlength)
  18670. s
  18671. (let* ((n (max (- maxlength 4) 1))
  18672. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  18673. (if (string-match re s)
  18674. (concat (match-string 1 s) "...")
  18675. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  18676. (defun org-get-indentation (&optional line)
  18677. "Get the indentation of the current line, interpreting tabs.
  18678. When LINE is given, assume it represents a line and compute its indentation."
  18679. (if line
  18680. (if (string-match "^ *" (org-remove-tabs line))
  18681. (match-end 0))
  18682. (save-excursion
  18683. (beginning-of-line 1)
  18684. (skip-chars-forward " \t")
  18685. (current-column))))
  18686. (defun org-get-string-indentation (s)
  18687. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  18688. (let ((n -1) (i 0) (w tab-width) c)
  18689. (catch 'exit
  18690. (while (< (setq n (1+ n)) (length s))
  18691. (setq c (aref s n))
  18692. (cond ((= c ?\ ) (setq i (1+ i)))
  18693. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  18694. (t (throw 'exit t)))))
  18695. i))
  18696. (defun org-remove-tabs (s &optional width)
  18697. "Replace tabulators in S with spaces.
  18698. Assumes that s is a single line, starting in column 0."
  18699. (setq width (or width tab-width))
  18700. (while (string-match "\t" s)
  18701. (setq s (replace-match
  18702. (make-string
  18703. (- (* width (/ (+ (match-beginning 0) width) width))
  18704. (match-beginning 0)) ?\ )
  18705. t t s)))
  18706. s)
  18707. (defun org-fix-indentation (line ind)
  18708. "Fix indentation in LINE.
  18709. IND is a cons cell with target and minimum indentation.
  18710. If the current indentation in LINE is smaller than the minimum,
  18711. leave it alone. If it is larger than ind, set it to the target."
  18712. (let* ((l (org-remove-tabs line))
  18713. (i (org-get-indentation l))
  18714. (i1 (car ind)) (i2 (cdr ind)))
  18715. (if (>= i i2) (setq l (substring line i2)))
  18716. (if (> i1 0)
  18717. (concat (make-string i1 ?\ ) l)
  18718. l)))
  18719. (defun org-remove-indentation (code &optional n)
  18720. "Remove the maximum common indentation from the lines in CODE.
  18721. N may optionally be the number of spaces to remove."
  18722. (with-temp-buffer
  18723. (insert code)
  18724. (org-do-remove-indentation n)
  18725. (buffer-string)))
  18726. (defun org-do-remove-indentation (&optional n)
  18727. "Remove the maximum common indentation from the buffer."
  18728. (untabify (point-min) (point-max))
  18729. (let ((min 10000) re)
  18730. (if n
  18731. (setq min n)
  18732. (goto-char (point-min))
  18733. (while (re-search-forward "^ *[^ \n]" nil t)
  18734. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  18735. (unless (or (= min 0) (= min 10000))
  18736. (setq re (format "^ \\{%d\\}" min))
  18737. (goto-char (point-min))
  18738. (while (re-search-forward re nil t)
  18739. (replace-match "")
  18740. (end-of-line 1))
  18741. min)))
  18742. (defun org-fill-template (template alist)
  18743. "Find each %key of ALIST in TEMPLATE and replace it."
  18744. (let ((case-fold-search nil)
  18745. entry key value)
  18746. (setq alist (sort (copy-sequence alist)
  18747. (lambda (a b) (< (length (car a)) (length (car b))))))
  18748. (while (setq entry (pop alist))
  18749. (setq template
  18750. (replace-regexp-in-string
  18751. (concat "%" (regexp-quote (car entry)))
  18752. (or (cdr entry) "") template t t)))
  18753. template))
  18754. (defun org-base-buffer (buffer)
  18755. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  18756. (if (not buffer)
  18757. buffer
  18758. (or (buffer-base-buffer buffer)
  18759. buffer)))
  18760. (defun org-trim (s)
  18761. "Remove whitespace at beginning and end of string."
  18762. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  18763. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  18764. s)
  18765. (defun org-wrap (string &optional width lines)
  18766. "Wrap string to either a number of lines, or a width in characters.
  18767. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  18768. that costs. If there is a word longer than WIDTH, the text is actually
  18769. wrapped to the length of that word.
  18770. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  18771. many lines, whatever width that takes.
  18772. The return value is a list of lines, without newlines at the end."
  18773. (let* ((words (org-split-string string "[ \t\n]+"))
  18774. (maxword (apply 'max (mapcar 'org-string-width words)))
  18775. w ll)
  18776. (cond (width
  18777. (org-do-wrap words (max maxword width)))
  18778. (lines
  18779. (setq w maxword)
  18780. (setq ll (org-do-wrap words maxword))
  18781. (if (<= (length ll) lines)
  18782. ll
  18783. (setq ll words)
  18784. (while (> (length ll) lines)
  18785. (setq w (1+ w))
  18786. (setq ll (org-do-wrap words w)))
  18787. ll))
  18788. (t (error "Cannot wrap this")))))
  18789. (defun org-do-wrap (words width)
  18790. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  18791. (let (lines line)
  18792. (while words
  18793. (setq line (pop words))
  18794. (while (and words (< (+ (length line) (length (car words))) width))
  18795. (setq line (concat line " " (pop words))))
  18796. (setq lines (push line lines)))
  18797. (nreverse lines)))
  18798. (defun org-split-string (string &optional separators)
  18799. "Splits STRING into substrings at SEPARATORS.
  18800. No empty strings are returned if there are matches at the beginning
  18801. and end of string."
  18802. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  18803. (start 0)
  18804. notfirst
  18805. (list nil))
  18806. (while (and (string-match rexp string
  18807. (if (and notfirst
  18808. (= start (match-beginning 0))
  18809. (< start (length string)))
  18810. (1+ start) start))
  18811. (< (match-beginning 0) (length string)))
  18812. (setq notfirst t)
  18813. (or (eq (match-beginning 0) 0)
  18814. (and (eq (match-beginning 0) (match-end 0))
  18815. (eq (match-beginning 0) start))
  18816. (setq list
  18817. (cons (substring string start (match-beginning 0))
  18818. list)))
  18819. (setq start (match-end 0)))
  18820. (or (eq start (length string))
  18821. (setq list
  18822. (cons (substring string start)
  18823. list)))
  18824. (nreverse list)))
  18825. (defun org-quote-vert (s)
  18826. "Replace \"|\" with \"\\vert\"."
  18827. (while (string-match "|" s)
  18828. (setq s (replace-match "\\vert" t t s)))
  18829. s)
  18830. (defun org-uuidgen-p (s)
  18831. "Is S an ID created by UUIDGEN?"
  18832. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  18833. (defun org-in-src-block-p nil
  18834. "Whether point is in a code source block."
  18835. (let (ov)
  18836. (when (setq ov (overlays-at (point)))
  18837. (memq 'org-block-background
  18838. (overlay-properties
  18839. (car ov))))))
  18840. (defun org-context ()
  18841. "Return a list of contexts of the current cursor position.
  18842. If several contexts apply, all are returned.
  18843. Each context entry is a list with a symbol naming the context, and
  18844. two positions indicating start and end of the context. Possible
  18845. contexts are:
  18846. :headline anywhere in a headline
  18847. :headline-stars on the leading stars in a headline
  18848. :todo-keyword on a TODO keyword (including DONE) in a headline
  18849. :tags on the TAGS in a headline
  18850. :priority on the priority cookie in a headline
  18851. :item on the first line of a plain list item
  18852. :item-bullet on the bullet/number of a plain list item
  18853. :checkbox on the checkbox in a plain list item
  18854. :table in an org-mode table
  18855. :table-special on a special filed in a table
  18856. :table-table in a table.el table
  18857. :clocktable in a clocktable
  18858. :src-block in a source block
  18859. :link on a hyperlink
  18860. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  18861. :target on a <<target>>
  18862. :radio-target on a <<<radio-target>>>
  18863. :latex-fragment on a LaTeX fragment
  18864. :latex-preview on a LaTeX fragment with overlaid preview image
  18865. This function expects the position to be visible because it uses font-lock
  18866. faces as a help to recognize the following contexts: :table-special, :link,
  18867. and :keyword."
  18868. (let* ((f (get-text-property (point) 'face))
  18869. (faces (if (listp f) f (list f)))
  18870. (case-fold-search t)
  18871. (p (point)) clist o)
  18872. ;; First the large context
  18873. (cond
  18874. ((org-at-heading-p t)
  18875. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  18876. (when (progn
  18877. (beginning-of-line 1)
  18878. (looking-at org-todo-line-tags-regexp))
  18879. (push (org-point-in-group p 1 :headline-stars) clist)
  18880. (push (org-point-in-group p 2 :todo-keyword) clist)
  18881. (push (org-point-in-group p 4 :tags) clist))
  18882. (goto-char p)
  18883. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  18884. (if (looking-at "\\[#[A-Z0-9]\\]")
  18885. (push (org-point-in-group p 0 :priority) clist)))
  18886. ((org-at-item-p)
  18887. (push (org-point-in-group p 2 :item-bullet) clist)
  18888. (push (list :item (point-at-bol)
  18889. (save-excursion (org-end-of-item) (point)))
  18890. clist)
  18891. (and (org-at-item-checkbox-p)
  18892. (push (org-point-in-group p 0 :checkbox) clist)))
  18893. ((org-at-table-p)
  18894. (push (list :table (org-table-begin) (org-table-end)) clist)
  18895. (if (memq 'org-formula faces)
  18896. (push (list :table-special
  18897. (previous-single-property-change p 'face)
  18898. (next-single-property-change p 'face)) clist)))
  18899. ((org-at-table-p 'any)
  18900. (push (list :table-table) clist)))
  18901. (goto-char p)
  18902. (let ((case-fold-search t))
  18903. ;; New the "medium" contexts: clocktables, source blocks
  18904. (cond ((org-in-clocktable-p)
  18905. (push (list :clocktable
  18906. (and (or (looking-at "#\\+BEGIN: clocktable")
  18907. (search-backward "#+BEGIN: clocktable" nil t))
  18908. (match-beginning 0))
  18909. (and (re-search-forward "#\\+END:?" nil t)
  18910. (match-end 0))) clist))
  18911. ((org-in-src-block-p)
  18912. (push (list :src-block
  18913. (and (or (looking-at "#\\+BEGIN_SRC")
  18914. (search-backward "#+BEGIN_SRC" nil t))
  18915. (match-beginning 0))
  18916. (and (search-forward "#+END_SRC" nil t)
  18917. (match-beginning 0))) clist))))
  18918. (goto-char p)
  18919. ;; Now the small context
  18920. (cond
  18921. ((org-at-timestamp-p)
  18922. (push (org-point-in-group p 0 :timestamp) clist))
  18923. ((memq 'org-link faces)
  18924. (push (list :link
  18925. (previous-single-property-change p 'face)
  18926. (next-single-property-change p 'face)) clist))
  18927. ((memq 'org-special-keyword faces)
  18928. (push (list :keyword
  18929. (previous-single-property-change p 'face)
  18930. (next-single-property-change p 'face)) clist))
  18931. ((org-at-target-p)
  18932. (push (org-point-in-group p 0 :target) clist)
  18933. (goto-char (1- (match-beginning 0)))
  18934. (if (looking-at org-radio-target-regexp)
  18935. (push (org-point-in-group p 0 :radio-target) clist))
  18936. (goto-char p))
  18937. ((setq o (car (delq nil
  18938. (mapcar
  18939. (lambda (x)
  18940. (if (memq x org-latex-fragment-image-overlays) x))
  18941. (overlays-at (point))))))
  18942. (push (list :latex-fragment
  18943. (overlay-start o) (overlay-end o)) clist)
  18944. (push (list :latex-preview
  18945. (overlay-start o) (overlay-end o)) clist))
  18946. ((org-inside-LaTeX-fragment-p)
  18947. ;; FIXME: positions wrong.
  18948. (push (list :latex-fragment (point) (point)) clist)))
  18949. (setq clist (nreverse (delq nil clist)))
  18950. clist))
  18951. ;; FIXME: Compare with at-regexp-p Do we need both?
  18952. (defun org-in-regexp (re &optional nlines visually)
  18953. "Check if point is inside a match of regexp.
  18954. Normally only the current line is checked, but you can include NLINES extra
  18955. lines both before and after point into the search.
  18956. If VISUALLY is set, require that the cursor is not after the match but
  18957. really on, so that the block visually is on the match."
  18958. (catch 'exit
  18959. (let ((pos (point))
  18960. (eol (point-at-eol (+ 1 (or nlines 0))))
  18961. (inc (if visually 1 0)))
  18962. (save-excursion
  18963. (beginning-of-line (- 1 (or nlines 0)))
  18964. (while (re-search-forward re eol t)
  18965. (if (and (<= (match-beginning 0) pos)
  18966. (>= (+ inc (match-end 0)) pos))
  18967. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  18968. (defun org-at-regexp-p (regexp)
  18969. "Is point inside a match of REGEXP in the current line?"
  18970. (catch 'exit
  18971. (save-excursion
  18972. (let ((pos (point)) (end (point-at-eol)))
  18973. (beginning-of-line 1)
  18974. (while (re-search-forward regexp end t)
  18975. (if (and (<= (match-beginning 0) pos)
  18976. (>= (match-end 0) pos))
  18977. (throw 'exit t)))
  18978. nil))))
  18979. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  18980. "Non-nil when point is between matches of START-RE and END-RE.
  18981. Also return a non-nil value when point is on one of the matches.
  18982. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  18983. buffer positions. Default values are the positions of headlines
  18984. surrounding the point.
  18985. The functions returns a cons cell whose car (resp. cdr) is the
  18986. position before START-RE (resp. after END-RE)."
  18987. (save-match-data
  18988. (let ((pos (point))
  18989. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  18990. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  18991. beg end)
  18992. (save-excursion
  18993. ;; Point is on a block when on START-RE or if START-RE can be
  18994. ;; found before it...
  18995. (and (or (org-at-regexp-p start-re)
  18996. (re-search-backward start-re limit-up t))
  18997. (setq beg (match-beginning 0))
  18998. ;; ... and END-RE after it...
  18999. (goto-char (match-end 0))
  19000. (re-search-forward end-re limit-down t)
  19001. (> (setq end (match-end 0)) pos)
  19002. ;; ... without another START-RE in-between.
  19003. (goto-char (match-beginning 0))
  19004. (not (re-search-backward start-re (1+ beg) t))
  19005. ;; Return value.
  19006. (cons beg end))))))
  19007. (defun org-in-block-p (names)
  19008. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19009. NAMES is a list of strings containing names of blocks.
  19010. Return first block name matched, or nil. Beware that in case of
  19011. nested blocks, the returned name may not belong to the closest
  19012. block from point."
  19013. (save-match-data
  19014. (catch 'exit
  19015. (let ((case-fold-search t)
  19016. (lim-up (save-excursion (outline-previous-heading)))
  19017. (lim-down (save-excursion (outline-next-heading))))
  19018. (mapc (lambda (name)
  19019. (let ((n (regexp-quote name)))
  19020. (when (org-between-regexps-p
  19021. (concat "^[ \t]*#\\+begin_" n)
  19022. (concat "^[ \t]*#\\+end_" n)
  19023. lim-up lim-down)
  19024. (throw 'exit n))))
  19025. names))
  19026. nil)))
  19027. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19028. "Call `multi-occur' with buffers for all agenda files."
  19029. (interactive "sOrg-files matching: \np")
  19030. (let* ((files (org-agenda-files))
  19031. (tnames (mapcar 'file-truename files))
  19032. (extra org-agenda-text-search-extra-files)
  19033. f)
  19034. (when (eq (car extra) 'agenda-archives)
  19035. (setq extra (cdr extra))
  19036. (setq files (org-add-archive-files files)))
  19037. (while (setq f (pop extra))
  19038. (unless (member (file-truename f) tnames)
  19039. (add-to-list 'files f 'append)
  19040. (add-to-list 'tnames (file-truename f) 'append)))
  19041. (multi-occur
  19042. (mapcar (lambda (x)
  19043. (with-current-buffer
  19044. (or (get-file-buffer x) (find-file-noselect x))
  19045. (widen)
  19046. (current-buffer)))
  19047. files)
  19048. regexp)))
  19049. (if (boundp 'occur-mode-find-occurrence-hook)
  19050. ;; Emacs 23
  19051. (add-hook 'occur-mode-find-occurrence-hook
  19052. (lambda ()
  19053. (when (derived-mode-p 'org-mode)
  19054. (org-reveal))))
  19055. ;; Emacs 22
  19056. (defadvice occur-mode-goto-occurrence
  19057. (after org-occur-reveal activate)
  19058. (and (derived-mode-p 'org-mode) (org-reveal)))
  19059. (defadvice occur-mode-goto-occurrence-other-window
  19060. (after org-occur-reveal activate)
  19061. (and (derived-mode-p 'org-mode) (org-reveal)))
  19062. (defadvice occur-mode-display-occurrence
  19063. (after org-occur-reveal activate)
  19064. (when (derived-mode-p 'org-mode)
  19065. (let ((pos (occur-mode-find-occurrence)))
  19066. (with-current-buffer (marker-buffer pos)
  19067. (save-excursion
  19068. (goto-char pos)
  19069. (org-reveal)))))))
  19070. (defun org-occur-link-in-agenda-files ()
  19071. "Create a link and search for it in the agendas.
  19072. The link is not stored in `org-stored-links', it is just created
  19073. for the search purpose."
  19074. (interactive)
  19075. (let ((link (condition-case nil
  19076. (org-store-link nil)
  19077. (error "Unable to create a link to here"))))
  19078. (org-occur-in-agenda-files (regexp-quote link))))
  19079. (defun org-uniquify (list)
  19080. "Remove duplicate elements from LIST."
  19081. (let (res)
  19082. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19083. res))
  19084. (defun org-delete-all (elts list)
  19085. "Remove all elements in ELTS from LIST."
  19086. (while elts
  19087. (setq list (delete (pop elts) list)))
  19088. list)
  19089. (defun org-count (cl-item cl-seq)
  19090. "Count the number of occurrences of ITEM in SEQ.
  19091. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19092. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19093. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19094. (while (< cl-start cl-end)
  19095. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19096. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19097. (setq cl-start (1+ cl-start)))
  19098. cl-count))
  19099. (defun org-remove-if (predicate seq)
  19100. "Remove everything from SEQ that fulfills PREDICATE."
  19101. (let (res e)
  19102. (while seq
  19103. (setq e (pop seq))
  19104. (if (not (funcall predicate e)) (push e res)))
  19105. (nreverse res)))
  19106. (defun org-remove-if-not (predicate seq)
  19107. "Remove everything from SEQ that does not fulfill PREDICATE."
  19108. (let (res e)
  19109. (while seq
  19110. (setq e (pop seq))
  19111. (if (funcall predicate e) (push e res)))
  19112. (nreverse res)))
  19113. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19114. "Reduce two-argument FUNCTION across SEQ.
  19115. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19116. \":initial-value\" removed."
  19117. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19118. (cadr (memq :initial-value cl-keys)))
  19119. (cl-seq (pop cl-seq))
  19120. (t (funcall cl-func)))))
  19121. (while cl-seq
  19122. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19123. cl-accum))
  19124. (defun org-back-over-empty-lines ()
  19125. "Move backwards over whitespace, to the beginning of the first empty line.
  19126. Returns the number of empty lines passed."
  19127. (let ((pos (point)))
  19128. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19129. (skip-chars-backward " \t\n\r")
  19130. (unless (eobp)
  19131. (forward-line -1)))
  19132. (beginning-of-line 2)
  19133. (goto-char (min (point) pos))
  19134. (count-lines (point) pos)))
  19135. (defun org-skip-whitespace ()
  19136. (skip-chars-forward " \t\n\r"))
  19137. (defun org-point-in-group (point group &optional context)
  19138. "Check if POINT is in match-group GROUP.
  19139. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19140. match. If the match group does not exist or point is not inside it,
  19141. return nil."
  19142. (and (match-beginning group)
  19143. (>= point (match-beginning group))
  19144. (<= point (match-end group))
  19145. (if context
  19146. (list context (match-beginning group) (match-end group))
  19147. t)))
  19148. (defun org-switch-to-buffer-other-window (&rest args)
  19149. "Switch to buffer in a second window on the current frame.
  19150. In particular, do not allow pop-up frames.
  19151. Returns the newly created buffer."
  19152. (let (pop-up-frames special-display-buffer-names special-display-regexps
  19153. special-display-function)
  19154. (apply 'switch-to-buffer-other-window args)))
  19155. (defun org-combine-plists (&rest plists)
  19156. "Create a single property list from all plists in PLISTS.
  19157. The process starts by copying the first list, and then setting properties
  19158. from the other lists. Settings in the last list are the most significant
  19159. ones and overrule settings in the other lists."
  19160. (let ((rtn (copy-sequence (pop plists)))
  19161. p v ls)
  19162. (while plists
  19163. (setq ls (pop plists))
  19164. (while ls
  19165. (setq p (pop ls) v (pop ls))
  19166. (setq rtn (plist-put rtn p v))))
  19167. rtn))
  19168. (defun org-replace-escapes (string table)
  19169. "Replace %-escapes in STRING with values in TABLE.
  19170. TABLE is an association list with keys like \"%a\" and string values.
  19171. The sequences in STRING may contain normal field width and padding information,
  19172. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19173. so values can contain further %-escapes if they are define later in TABLE."
  19174. (let ((tbl (copy-alist table))
  19175. (case-fold-search nil)
  19176. (pchg 0)
  19177. e re rpl)
  19178. (while (setq e (pop tbl))
  19179. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19180. (when (and (cdr e) (string-match re (cdr e)))
  19181. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19182. (safe "SREF"))
  19183. (add-text-properties 0 3 (list 'sref sref) safe)
  19184. (setcdr e (replace-match safe t t (cdr e)))))
  19185. (while (string-match re string)
  19186. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19187. (cdr e)))
  19188. (setq string (replace-match rpl t t string))))
  19189. (while (setq pchg (next-property-change pchg string))
  19190. (let ((sref (get-text-property pchg 'sref string)))
  19191. (when (and sref (string-match "SREF" string pchg))
  19192. (setq string (replace-match sref t t string)))))
  19193. string))
  19194. (defun org-sublist (list start end)
  19195. "Return a section of LIST, from START to END.
  19196. Counting starts at 1."
  19197. (let (rtn (c start))
  19198. (setq list (nthcdr (1- start) list))
  19199. (while (and list (<= c end))
  19200. (push (pop list) rtn)
  19201. (setq c (1+ c)))
  19202. (nreverse rtn)))
  19203. (defun org-find-base-buffer-visiting (file)
  19204. "Like `find-buffer-visiting' but always return the base buffer and
  19205. not an indirect buffer."
  19206. (let ((buf (or (get-file-buffer file)
  19207. (find-buffer-visiting file))))
  19208. (if buf
  19209. (or (buffer-base-buffer buf) buf)
  19210. nil)))
  19211. (defun org-image-file-name-regexp (&optional extensions)
  19212. "Return regexp matching the file names of images.
  19213. If EXTENSIONS is given, only match these."
  19214. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19215. (image-file-name-regexp)
  19216. (let ((image-file-name-extensions
  19217. (or extensions
  19218. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19219. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19220. (concat "\\."
  19221. (regexp-opt (nconc (mapcar 'upcase
  19222. image-file-name-extensions)
  19223. image-file-name-extensions)
  19224. t)
  19225. "\\'"))))
  19226. (defun org-file-image-p (file &optional extensions)
  19227. "Return non-nil if FILE is an image."
  19228. (save-match-data
  19229. (string-match (org-image-file-name-regexp extensions) file)))
  19230. (defun org-get-cursor-date ()
  19231. "Return the date at cursor in as a time.
  19232. This works in the calendar and in the agenda, anywhere else it just
  19233. returns the current time."
  19234. (let (date day defd)
  19235. (cond
  19236. ((eq major-mode 'calendar-mode)
  19237. (setq date (calendar-cursor-to-date)
  19238. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19239. ((eq major-mode 'org-agenda-mode)
  19240. (setq day (get-text-property (point) 'day))
  19241. (if day
  19242. (setq date (calendar-gregorian-from-absolute day)
  19243. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  19244. (nth 2 date))))))
  19245. (or defd (current-time))))
  19246. (defvar org-agenda-action-marker (make-marker)
  19247. "Marker pointing to the entry for the next agenda action.")
  19248. (defun org-mark-entry-for-agenda-action ()
  19249. "Mark the current entry as target of an agenda action.
  19250. Agenda actions are actions executed from the agenda with the key `k',
  19251. which make use of the date at the cursor."
  19252. (interactive)
  19253. (move-marker org-agenda-action-marker
  19254. (save-excursion (org-back-to-heading t) (point))
  19255. (current-buffer))
  19256. (message
  19257. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  19258. (defun org-mark-subtree (&optional up)
  19259. "Mark the current subtree.
  19260. This puts point at the start of the current subtree, and mark at
  19261. the end. If a numeric prefix UP is given, move up into the
  19262. hierarchy of headlines by UP levels before marking the subtree."
  19263. (interactive "P")
  19264. (org-with-limited-levels
  19265. (cond ((org-at-heading-p) (beginning-of-line))
  19266. ((org-before-first-heading-p) (error "Not in a subtree"))
  19267. (t (outline-previous-visible-heading 1))))
  19268. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19269. (if (org-called-interactively-p 'any)
  19270. (call-interactively 'org-mark-element)
  19271. (org-mark-element)))
  19272. ;;; Macros
  19273. ;; Macros are expanded with `org-macro-replace-all', which relies
  19274. ;; internally on `org-macro-expand'.
  19275. ;; Templates for expansion are stored in the buffer-local variable
  19276. ;; `org-macro-templates'. This variable is updated by
  19277. ;; `org-macro-initialize-templates'.
  19278. (defvar org-macro-templates nil
  19279. "Alist containing all macro templates in current buffer.
  19280. Associations are in the shape of (NAME . TEMPLATE) where NAME
  19281. stands for macro's name and template for its replacement value,
  19282. both as strings. This is an internal variable. Do not set it
  19283. directly, use instead:
  19284. #+MACRO: name template")
  19285. (make-variable-buffer-local 'org-macro-templates)
  19286. (defun org-macro-expand (macro)
  19287. "Return expanded MACRO, as a string.
  19288. MACRO is an object, obtained, for example, with
  19289. `org-element-context'. Return nil if no template was found."
  19290. (let ((template
  19291. (cdr (assoc-string (org-element-property :key macro)
  19292. org-macro-templates
  19293. ;; Macro names are case-insensitive.
  19294. t))))
  19295. (when template
  19296. (let ((value (replace-regexp-in-string
  19297. "\\$[0-9]+"
  19298. (lambda (arg)
  19299. (or (nth (1- (string-to-number (substring arg 1)))
  19300. (org-element-property :args macro))
  19301. ;; No argument provided: remove
  19302. ;; place-holder.
  19303. ""))
  19304. template)))
  19305. ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
  19306. (when (string-match "\\`(eval\\>" value)
  19307. (setq value (eval (read value))))
  19308. ;; Return string.
  19309. (format "%s" (or value ""))))))
  19310. (defun org-macro-replace-all ()
  19311. "Replace all macros in current buffer by their expansion."
  19312. (save-excursion
  19313. (goto-char (point-min))
  19314. (while (re-search-forward "{{{[-A-Za-z0-9_]" nil t)
  19315. (let ((object (org-element-context)))
  19316. (when (eq (org-element-type object) 'macro)
  19317. (let ((value (org-macro-expand object)))
  19318. (when value
  19319. (delete-region
  19320. (org-element-property :begin object)
  19321. ;; Preserve white spaces after the macro.
  19322. (progn (goto-char (org-element-property :end object))
  19323. (skip-chars-backward " \t")
  19324. (point)))
  19325. ;; Leave point before replacement in case of recursive
  19326. ;; expansions.
  19327. (save-excursion (insert value)))))))))
  19328. (defun org-macro-initialize-templates ()
  19329. "Collect macro templates defined in current buffer.
  19330. Templates are stored in buffer-local variable
  19331. `org-macro-templates'. In addition to buffer-defined macros, the
  19332. function installs the following ones: \"property\", \"date\",
  19333. \"time\". and, if appropriate, \"input-file\" and
  19334. \"modification-time\"."
  19335. (let ((case-fold-search t)
  19336. (set-template
  19337. (lambda (cell)
  19338. ;; Add CELL to `org-macro-templates' if there's no
  19339. ;; association matching its name already. Otherwise,
  19340. ;; replace old association with the new one in that
  19341. ;; variable.
  19342. (let ((old-template (assoc (car cell) org-macro-templates)))
  19343. (if old-template (setcdr old-template (cdr cell))
  19344. (push cell org-macro-templates))))))
  19345. ;; Install buffer-local macros.
  19346. (org-with-wide-buffer
  19347. (goto-char (point-min))
  19348. (while (re-search-forward "^[ \t]*#\\+MACRO:" nil t)
  19349. (let ((element (org-element-at-point)))
  19350. (when (eq (org-element-type element) 'keyword)
  19351. (let ((value (org-element-property :value element)))
  19352. (when (string-match "^\\(.*?\\)\\(?:\\s-+\\(.*\\)\\)?\\s-*$" value)
  19353. (funcall set-template
  19354. (cons (match-string 1 value)
  19355. (or (match-string 2 value) "")))))))))
  19356. ;; Install hard-coded macros.
  19357. (mapc (lambda (cell) (funcall set-template cell))
  19358. (list
  19359. (cons "property" "(eval (org-entry-get nil \"$1\" 'selective))")
  19360. (cons "date" "(eval (format-time-string \"$1\"))")
  19361. (cons "time" "(eval (format-time-string \"$1\"))")))
  19362. (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  19363. (when (and visited-file (file-exists-p visited-file))
  19364. (mapc (lambda (cell) (funcall set-template cell))
  19365. (list
  19366. (cons "input-file" (file-name-nondirectory visited-file))
  19367. (cons "modification-time"
  19368. (format "(eval (format-time-string \"$1\" '%s))"
  19369. (prin1-to-string
  19370. (nth 5 (file-attributes visited-file)))))))))))
  19371. ;;; Indentation
  19372. (defun org-indent-line ()
  19373. "Indent line depending on context."
  19374. (interactive)
  19375. (let* ((pos (point))
  19376. (itemp (org-at-item-p))
  19377. (case-fold-search t)
  19378. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19379. (inline-task-p (and (featurep 'org-inlinetask)
  19380. (org-inlinetask-in-task-p)))
  19381. (inline-re (and inline-task-p
  19382. (org-inlinetask-outline-regexp)))
  19383. column)
  19384. (if (and orgstruct-is-++ (eq pos (point)))
  19385. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19386. (indent-according-to-mode))
  19387. (beginning-of-line 1)
  19388. (cond
  19389. ;; Headings
  19390. ((looking-at org-outline-regexp) (setq column 0))
  19391. ;; Included files
  19392. ((looking-at "#\\+include:") (setq column 0))
  19393. ;; Footnote definition
  19394. ((looking-at org-footnote-definition-re) (setq column 0))
  19395. ;; Literal examples
  19396. ((looking-at "[ \t]*:\\( \\|$\\)")
  19397. (setq column (org-get-indentation))) ; do nothing
  19398. ;; Lists
  19399. ((ignore-errors (goto-char (org-in-item-p)))
  19400. (setq column (if itemp
  19401. (org-get-indentation)
  19402. (org-list-item-body-column (point))))
  19403. (goto-char pos))
  19404. ;; Drawers
  19405. ((and (looking-at "[ \t]*:END:")
  19406. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19407. (save-excursion
  19408. (goto-char (1- (match-beginning 1)))
  19409. (setq column (current-column))))
  19410. ;; Special blocks
  19411. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19412. (save-excursion
  19413. (re-search-backward
  19414. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19415. (setq column (org-get-indentation (match-string 0))))
  19416. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19417. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19418. (save-excursion
  19419. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19420. (setq column
  19421. (cond ((equal (downcase (match-string 1)) "src")
  19422. ;; src blocks: let `org-edit-src-exit' handle them
  19423. (org-get-indentation))
  19424. ((equal (downcase (match-string 1)) "example")
  19425. (max (org-get-indentation)
  19426. (org-get-indentation (match-string 0))))
  19427. (t
  19428. (org-get-indentation (match-string 0))))))
  19429. ;; This line has nothing special, look at the previous relevant
  19430. ;; line to compute indentation
  19431. (t
  19432. (beginning-of-line 0)
  19433. (while (and (not (bobp))
  19434. (not (looking-at org-drawer-regexp))
  19435. ;; When point started in an inline task, do not move
  19436. ;; above task starting line.
  19437. (not (and inline-task-p (looking-at inline-re)))
  19438. ;; Skip drawers, blocks, empty lines, verbatim,
  19439. ;; comments, tables, footnotes definitions, lists,
  19440. ;; inline tasks.
  19441. (or (and (looking-at "[ \t]*:END:")
  19442. (re-search-backward org-drawer-regexp nil t))
  19443. (and (looking-at "[ \t]*#\\+end_")
  19444. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19445. (looking-at "[ \t]*[\n:#|]")
  19446. (looking-at org-footnote-definition-re)
  19447. (and (ignore-errors (goto-char (org-in-item-p)))
  19448. (goto-char
  19449. (org-list-get-top-point (org-list-struct))))
  19450. (and (not inline-task-p)
  19451. (featurep 'org-inlinetask)
  19452. (org-inlinetask-in-task-p)
  19453. (or (org-inlinetask-goto-beginning) t))))
  19454. (beginning-of-line 0))
  19455. (cond
  19456. ;; There was an heading above.
  19457. ((looking-at "\\*+[ \t]+")
  19458. (if (not org-adapt-indentation)
  19459. (setq column 0)
  19460. (goto-char (match-end 0))
  19461. (setq column (current-column))))
  19462. ;; A drawer had started and is unfinished
  19463. ((looking-at org-drawer-regexp)
  19464. (goto-char (1- (match-beginning 1)))
  19465. (setq column (current-column)))
  19466. ;; Else, nothing noticeable found: get indentation and go on.
  19467. (t (setq column (org-get-indentation))))))
  19468. ;; Now apply indentation and move cursor accordingly
  19469. (goto-char pos)
  19470. (if (<= (current-column) (current-indentation))
  19471. (org-indent-line-to column)
  19472. (save-excursion (org-indent-line-to column)))
  19473. ;; Special polishing for properties, see `org-property-format'
  19474. (setq column (current-column))
  19475. (beginning-of-line 1)
  19476. (if (looking-at
  19477. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19478. (replace-match (concat (match-string 1)
  19479. (format org-property-format
  19480. (match-string 2) (match-string 3)))
  19481. t t))
  19482. (org-move-to-column column))))
  19483. (defun org-indent-drawer ()
  19484. "Indent the drawer at point."
  19485. (interactive)
  19486. (let ((p (point))
  19487. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19488. (match-end 0)))
  19489. (folded
  19490. (save-excursion
  19491. (end-of-line)
  19492. (when (overlays-at (point))
  19493. (member 'invisible (overlay-properties
  19494. (car (overlays-at (point)))))))))
  19495. (when folded (org-cycle))
  19496. (indent-for-tab-command)
  19497. (while (and (move-beginning-of-line 2) (< (point) e))
  19498. (indent-for-tab-command))
  19499. (goto-char p)
  19500. (when folded (org-cycle)))
  19501. (message "Drawer at point indented"))
  19502. (defun org-indent-block ()
  19503. "Indent the block at point."
  19504. (interactive)
  19505. (let ((p (point))
  19506. (case-fold-search t)
  19507. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19508. (match-end 0)))
  19509. (folded
  19510. (save-excursion
  19511. (end-of-line)
  19512. (when (overlays-at (point))
  19513. (member 'invisible (overlay-properties
  19514. (car (overlays-at (point)))))))))
  19515. (when folded (org-cycle))
  19516. (indent-for-tab-command)
  19517. (while (and (move-beginning-of-line 2) (< (point) e))
  19518. (indent-for-tab-command))
  19519. (goto-char p)
  19520. (when folded (org-cycle)))
  19521. (message "Block at point indented"))
  19522. (defun org-indent-region (start end)
  19523. "Indent region."
  19524. (interactive "r")
  19525. (save-excursion
  19526. (let ((line-end (org-current-line end)))
  19527. (goto-char start)
  19528. (while (< (org-current-line) line-end)
  19529. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19530. (t (call-interactively 'org-indent-line)))
  19531. (move-beginning-of-line 2)))))
  19532. ;;; Filling
  19533. ;; We use our own fill-paragraph and auto-fill functions.
  19534. ;; `org-fill-paragraph' relies on adaptive filling and context
  19535. ;; checking. Appropriate `fill-prefix' is computed with
  19536. ;; `org-adaptive-fill-function'.
  19537. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19538. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19539. ;; `org-adaptive-fill-function' value. Internally,
  19540. ;; `org-comment-line-break-function' breaks the line.
  19541. ;; `org-setup-filling' installs filling and auto-filling related
  19542. ;; variables during `org-mode' initialization.
  19543. (defun org-setup-filling ()
  19544. (interactive)
  19545. ;; Prevent auto-fill from inserting unwanted new items.
  19546. (when (boundp 'fill-nobreak-predicate)
  19547. (org-set-local
  19548. 'fill-nobreak-predicate
  19549. (org-uniquify
  19550. (append fill-nobreak-predicate
  19551. '(org-fill-paragraph-separate-nobreak-p
  19552. org-fill-line-break-nobreak-p)))))
  19553. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19554. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  19555. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19556. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  19557. (defvar org-element-paragraph-separate) ; org-element.el
  19558. (defun org-fill-paragraph-separate-nobreak-p ()
  19559. "Non-nil when a line break at point would insert a new item."
  19560. (looking-at (substring org-element-paragraph-separate 1)))
  19561. (defun org-fill-line-break-nobreak-p ()
  19562. "Non-nil when a line break at point would create an Org line break."
  19563. (save-excursion
  19564. (skip-chars-backward "[ \t]")
  19565. (skip-chars-backward "\\\\")
  19566. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  19567. (declare-function message-in-body-p "message" ())
  19568. (defvar org-element--affiliated-re) ; From org-element.el
  19569. (defun org-adaptive-fill-function ()
  19570. "Compute a fill prefix for the current line.
  19571. Return fill prefix, as a string, or nil if current line isn't
  19572. meant to be filled."
  19573. (org-with-wide-buffer
  19574. (unless (and (derived-mode-p 'message-mode) (not (message-in-body-p)))
  19575. ;; FIXME: This is really the job of orgstruct++-mode
  19576. (let* ((p (line-beginning-position))
  19577. (element (save-excursion (beginning-of-line)
  19578. (org-element-at-point)))
  19579. (type (org-element-type element))
  19580. (post-affiliated
  19581. (save-excursion
  19582. (goto-char (org-element-property :begin element))
  19583. (while (looking-at org-element--affiliated-re) (forward-line))
  19584. (point))))
  19585. (unless (< p post-affiliated)
  19586. (case type
  19587. (comment (looking-at "[ \t]*# ?") (match-string 0))
  19588. (footnote-definition "")
  19589. ((item plain-list)
  19590. (make-string (org-list-item-body-column post-affiliated) ? ))
  19591. (paragraph
  19592. ;; Fill prefix is usually the same as the current line,
  19593. ;; except if the paragraph is at the beginning of an item.
  19594. (let ((parent (org-element-property :parent element)))
  19595. (cond ((eq (org-element-type parent) 'item)
  19596. (make-string (org-list-item-body-column
  19597. (org-element-property :begin parent))
  19598. ? ))
  19599. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19600. (match-string 0))
  19601. (t ""))))
  19602. (comment-block
  19603. ;; Only fill contents if P is within block boundaries.
  19604. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  19605. (forward-line)
  19606. (point)))
  19607. (cend (save-excursion
  19608. (goto-char (org-element-property :end element))
  19609. (skip-chars-backward " \r\t\n")
  19610. (line-beginning-position))))
  19611. (when (and (>= p cbeg) (< p cend))
  19612. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19613. (match-string 0)
  19614. ""))))))))))
  19615. (declare-function message-goto-body "message" ())
  19616. (defvar message-cite-prefix-regexp) ; From message.el
  19617. (defvar org-element-all-objects) ; From org-element.el
  19618. (defun org-fill-paragraph (&optional justify)
  19619. "Fill element at point, when applicable.
  19620. This function only applies to comment blocks, comments, example
  19621. blocks and paragraphs. Also, as a special case, re-align table
  19622. when point is at one.
  19623. If JUSTIFY is non-nil (interactively, with prefix argument),
  19624. justify as well. If `sentence-end-double-space' is non-nil, then
  19625. period followed by one space does not end a sentence, so don't
  19626. break a line there. The variable `fill-column' controls the
  19627. width for filling.
  19628. For convenience, when point is at a plain list, an item or
  19629. a footnote definition, try to fill the first paragraph within."
  19630. ;; Falls back on message-fill-paragraph when necessary
  19631. (interactive)
  19632. (if (and (derived-mode-p 'message-mode)
  19633. (or (not (message-in-body-p))
  19634. (save-excursion (move-beginning-of-line 1)
  19635. (looking-at message-cite-prefix-regexp))))
  19636. (let ((fill-paragraph-function
  19637. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  19638. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  19639. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  19640. (paragraph-separate
  19641. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  19642. (fill-paragraph nil))
  19643. (save-excursion
  19644. ;; Move to end of line in order to get the first paragraph
  19645. ;; within a plain list or a footnote definition.
  19646. (end-of-line)
  19647. (let ((element (org-element-at-point)))
  19648. ;; First check if point is in a blank line at the beginning of
  19649. ;; the buffer. In that case, ignore filling.
  19650. (if (< (point) (org-element-property :begin element)) t
  19651. (case (org-element-type element)
  19652. ;; Align Org tables, leave table.el tables as-is.
  19653. (table-row (org-table-align) t)
  19654. (table
  19655. (when (eq (org-element-property :type element) 'org)
  19656. (org-table-align))
  19657. t)
  19658. (paragraph
  19659. ;; Paragraphs may contain `line-break' type objects.
  19660. (let ((beg (max (point-min)
  19661. (org-element-property :contents-begin element)))
  19662. (end (min (point-max)
  19663. (org-element-property :contents-end element))))
  19664. ;; Do nothing if point is at an affiliated keyword.
  19665. (if (< (point) beg) t
  19666. (when (derived-mode-p 'message-mode)
  19667. ;; In `message-mode', do not fill following
  19668. ;; citation in current paragraph nor text before
  19669. ;; message body.
  19670. (let ((body-start (save-excursion (message-goto-body))))
  19671. (when body-start (setq beg (max body-start beg))))
  19672. (when (save-excursion
  19673. (re-search-forward
  19674. (concat "^" message-cite-prefix-regexp) end t))
  19675. (setq end (match-beginning 0))))
  19676. ;; Fill paragraph, taking line breaks into
  19677. ;; consideration. For that, slice the paragraph
  19678. ;; using line breaks as separators, and fill the
  19679. ;; parts in reverse order to avoid messing with
  19680. ;; markers.
  19681. (save-excursion
  19682. (goto-char end)
  19683. (mapc
  19684. (lambda (pos)
  19685. (fill-region-as-paragraph pos (point) justify)
  19686. (goto-char pos))
  19687. ;; Find the list of ending positions for line
  19688. ;; breaks in the current paragraph. Add paragraph
  19689. ;; beginning to include first slice.
  19690. (nreverse
  19691. (cons
  19692. beg
  19693. (org-element-map
  19694. (org-element--parse-objects
  19695. beg end nil org-element-all-objects)
  19696. 'line-break
  19697. (lambda (lb) (org-element-property :end lb)))))))
  19698. t)))
  19699. ;; Contents of `comment-block' type elements should be
  19700. ;; filled as plain text, but only if point is within block
  19701. ;; markers.
  19702. (comment-block
  19703. (let* ((case-fold-search t)
  19704. (beg (save-excursion
  19705. (goto-char (org-element-property :begin element))
  19706. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  19707. (forward-line)
  19708. (point)))
  19709. (end (save-excursion
  19710. (goto-char (org-element-property :end element))
  19711. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  19712. (line-beginning-position))))
  19713. (when (and (>= (point) beg) (< (point) end))
  19714. (fill-region-as-paragraph
  19715. (save-excursion
  19716. (end-of-line)
  19717. (re-search-backward "^[ \t]*$" beg 'move)
  19718. (line-beginning-position))
  19719. (save-excursion
  19720. (beginning-of-line)
  19721. (re-search-forward "^[ \t]*$" end 'move)
  19722. (line-beginning-position))
  19723. justify)))
  19724. t)
  19725. ;; Fill comments.
  19726. (comment (fill-comment-paragraph justify))
  19727. ;; Ignore every other element.
  19728. (otherwise t)))))))
  19729. (defun org-auto-fill-function ()
  19730. "Auto-fill function."
  19731. ;; Check if auto-filling is meaningful.
  19732. (let ((fc (current-fill-column)))
  19733. (when (and fc (> (current-column) fc))
  19734. (let ((fill-prefix (org-adaptive-fill-function)))
  19735. (when fill-prefix (do-auto-fill))))))
  19736. (defun org-comment-line-break-function (&optional soft)
  19737. "Break line at point and indent, continuing comment if within one.
  19738. The inserted newline is marked hard if variable
  19739. `use-hard-newlines' is true, unless optional argument SOFT is
  19740. non-nil."
  19741. (if soft (insert-and-inherit ?\n) (newline 1))
  19742. (save-excursion (forward-char -1) (delete-horizontal-space))
  19743. (delete-horizontal-space)
  19744. (indent-to-left-margin)
  19745. (insert-before-markers-and-inherit fill-prefix))
  19746. ;;; Comments
  19747. ;; Org comments syntax is quite complex. It requires the entire line
  19748. ;; to be just a comment. Also, even with the right syntax at the
  19749. ;; beginning of line, some some elements (i.e. verse-block or
  19750. ;; example-block) don't accept comments. Usual Emacs comment commands
  19751. ;; cannot cope with those requirements. Therefore, Org replaces them.
  19752. ;; Org still relies on `comment-dwim', but cannot trust
  19753. ;; `comment-only-p'. So, `comment-region-function' and
  19754. ;; `uncomment-region-function' both point
  19755. ;; to`org-comment-or-uncomment-region'. Eventually,
  19756. ;; `org-insert-comment' takes care of insertion of comments at the
  19757. ;; beginning of line.
  19758. ;; `org-setup-comments-handling' install comments related variables
  19759. ;; during `org-mode' initialization.
  19760. (defun org-setup-comments-handling ()
  19761. (interactive)
  19762. (org-set-local 'comment-use-syntax nil)
  19763. (org-set-local 'comment-start "# ")
  19764. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  19765. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  19766. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  19767. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  19768. (defun org-insert-comment ()
  19769. "Insert an empty comment above current line.
  19770. If the line is empty, insert comment at its beginning."
  19771. (beginning-of-line)
  19772. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  19773. (org-indent-line)
  19774. (insert "# "))
  19775. (defvar comment-empty-lines) ; From newcomment.el.
  19776. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  19777. "Comment or uncomment each non-blank line in the region.
  19778. Uncomment each non-blank line between BEG and END if it only
  19779. contains commented lines. Otherwise, comment them."
  19780. (save-restriction
  19781. ;; Restrict region
  19782. (narrow-to-region (save-excursion (goto-char beg)
  19783. (skip-chars-forward " \r\t\n" end)
  19784. (line-beginning-position))
  19785. (save-excursion (goto-char end)
  19786. (skip-chars-backward " \r\t\n" beg)
  19787. (line-end-position)))
  19788. (let ((uncommentp
  19789. ;; UNCOMMENTP is non-nil when every non blank line between
  19790. ;; BEG and END is a comment.
  19791. (save-excursion
  19792. (goto-char (point-min))
  19793. (while (and (not (eobp))
  19794. (let ((element (org-element-at-point)))
  19795. (and (eq (org-element-type element) 'comment)
  19796. (goto-char (min (point-max)
  19797. (org-element-property
  19798. :end element)))))))
  19799. (eobp))))
  19800. (if uncommentp
  19801. ;; Only blank lines and comments in region: uncomment it.
  19802. (save-excursion
  19803. (goto-char (point-min))
  19804. (while (not (eobp))
  19805. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  19806. (replace-match "" nil nil nil 1))
  19807. (forward-line)))
  19808. ;; Comment each line in region.
  19809. (let ((min-indent (point-max)))
  19810. ;; First find the minimum indentation across all lines.
  19811. (save-excursion
  19812. (goto-char (point-min))
  19813. (while (and (not (eobp)) (not (zerop min-indent)))
  19814. (unless (looking-at "[ \t]*$")
  19815. (setq min-indent (min min-indent (current-indentation))))
  19816. (forward-line)))
  19817. ;; Then loop over all lines.
  19818. (save-excursion
  19819. (goto-char (point-min))
  19820. (while (not (eobp))
  19821. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  19822. (org-move-to-column min-indent t)
  19823. (insert comment-start))
  19824. (forward-line))))))))
  19825. ;;; Other stuff.
  19826. (defun org-toggle-fixed-width-section (arg)
  19827. "Toggle the fixed-width export.
  19828. If there is no active region, the QUOTE keyword at the current headline is
  19829. inserted or removed. When present, it causes the text between this headline
  19830. and the next to be exported as fixed-width text, and unmodified.
  19831. If there is an active region, this command adds or removes a colon as the
  19832. first character of this line. If the first character of a line is a colon,
  19833. this line is also exported in fixed-width font."
  19834. (interactive "P")
  19835. (let* ((cc 0)
  19836. (regionp (org-region-active-p))
  19837. (beg (if regionp (region-beginning) (point)))
  19838. (end (if regionp (region-end)))
  19839. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  19840. (case-fold-search nil)
  19841. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  19842. off)
  19843. (if regionp
  19844. (save-excursion
  19845. (goto-char beg)
  19846. (setq cc (current-column))
  19847. (beginning-of-line 1)
  19848. (setq off (looking-at re))
  19849. (while (> nlines 0)
  19850. (setq nlines (1- nlines))
  19851. (beginning-of-line 1)
  19852. (cond
  19853. (arg
  19854. (org-move-to-column cc t)
  19855. (insert ": \n")
  19856. (forward-line -1))
  19857. ((and off (looking-at re))
  19858. (replace-match "" t t nil 1))
  19859. ((not off) (org-move-to-column cc t) (insert ": ")))
  19860. (forward-line 1)))
  19861. (save-excursion
  19862. (org-back-to-heading)
  19863. (cond
  19864. ((looking-at (format org-heading-keyword-regexp-format
  19865. org-quote-string))
  19866. (goto-char (match-end 1))
  19867. (looking-at (concat " +" org-quote-string))
  19868. (replace-match "" t t)
  19869. (when (eolp) (insert " ")))
  19870. ((looking-at org-outline-regexp)
  19871. (goto-char (match-end 0))
  19872. (insert org-quote-string " ")))))))
  19873. (defun org-reftex-citation ()
  19874. "Use reftex-citation to insert a citation into the buffer.
  19875. This looks for a line like
  19876. #+BIBLIOGRAPHY: foo plain option:-d
  19877. and derives from it that foo.bib is the bibliography file relevant
  19878. for this document. It then installs the necessary environment for RefTeX
  19879. to work in this buffer and calls `reftex-citation' to insert a citation
  19880. into the buffer.
  19881. Export of such citations to both LaTeX and HTML is handled by the contributed
  19882. package org-exp-bibtex by Taru Karttunen."
  19883. (interactive)
  19884. (let ((reftex-docstruct-symbol 'rds)
  19885. (reftex-cite-format "\\cite{%l}")
  19886. rds bib)
  19887. (save-excursion
  19888. (save-restriction
  19889. (widen)
  19890. (let ((case-fold-search t)
  19891. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  19892. (if (not (save-excursion
  19893. (or (re-search-forward re nil t)
  19894. (re-search-backward re nil t))))
  19895. (error "No bibliography defined in file")
  19896. (setq bib (concat (match-string 1) ".bib")
  19897. rds (list (list 'bib bib)))))))
  19898. (call-interactively 'reftex-citation)))
  19899. ;;;; Functions extending outline functionality
  19900. (defun org-beginning-of-line (&optional arg)
  19901. "Go to the beginning of the current line. If that is invisible, continue
  19902. to a visible line beginning. This makes the function of C-a more intuitive.
  19903. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19904. first attempt, and only move to after the tags when the cursor is already
  19905. beyond the end of the headline."
  19906. (interactive "P")
  19907. (let ((pos (point))
  19908. (special (if (consp org-special-ctrl-a/e)
  19909. (car org-special-ctrl-a/e)
  19910. org-special-ctrl-a/e))
  19911. refpos)
  19912. (if (org-bound-and-true-p line-move-visual)
  19913. (beginning-of-visual-line 1)
  19914. (beginning-of-line 1))
  19915. (if (and arg (fboundp 'move-beginning-of-line))
  19916. (call-interactively 'move-beginning-of-line)
  19917. (if (bobp)
  19918. nil
  19919. (backward-char 1)
  19920. (if (org-truely-invisible-p)
  19921. (while (and (not (bobp)) (org-truely-invisible-p))
  19922. (backward-char 1)
  19923. (beginning-of-line 1))
  19924. (forward-char 1))))
  19925. (when special
  19926. (cond
  19927. ((and (looking-at org-complex-heading-regexp)
  19928. (= (char-after (match-end 1)) ?\ ))
  19929. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  19930. (point-at-eol)))
  19931. (goto-char
  19932. (if (eq special t)
  19933. (cond ((> pos refpos) refpos)
  19934. ((= pos (point)) refpos)
  19935. (t (point)))
  19936. (cond ((> pos (point)) (point))
  19937. ((not (eq last-command this-command)) (point))
  19938. (t refpos)))))
  19939. ((org-at-item-p)
  19940. ;; Being at an item and not looking at an the item means point
  19941. ;; was previously moved to beginning of a visual line, which
  19942. ;; doesn't contain the item. Therefore, do nothing special,
  19943. ;; just stay here.
  19944. (when (looking-at org-list-full-item-re)
  19945. ;; Set special position at first white space character after
  19946. ;; bullet, and check-box, if any.
  19947. (let ((after-bullet
  19948. (let ((box (match-end 3)))
  19949. (if (not box) (match-end 1)
  19950. (let ((after (char-after box)))
  19951. (if (and after (= after ? )) (1+ box) box))))))
  19952. ;; Special case: Move point to special position when
  19953. ;; currently after it or at beginning of line.
  19954. (if (eq special t)
  19955. (when (or (> pos after-bullet) (= (point) pos))
  19956. (goto-char after-bullet))
  19957. ;; Reversed case: Move point to special position when
  19958. ;; point was already at beginning of line and command is
  19959. ;; repeated.
  19960. (when (and (= (point) pos) (eq last-command this-command))
  19961. (goto-char after-bullet))))))))
  19962. (org-no-warnings
  19963. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19964. (defun org-end-of-line (&optional arg)
  19965. "Go to the end of the line.
  19966. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19967. first attempt, and only move to after the tags when the cursor is already
  19968. beyond the end of the headline."
  19969. (interactive "P")
  19970. (let ((special (if (consp org-special-ctrl-a/e)
  19971. (cdr org-special-ctrl-a/e)
  19972. org-special-ctrl-a/e)))
  19973. (cond
  19974. ((or (not special) arg
  19975. (not (or (org-at-heading-p) (org-at-item-p) (org-at-drawer-p))))
  19976. (call-interactively
  19977. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19978. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19979. (t 'end-of-line))))
  19980. ((org-at-heading-p)
  19981. (let ((pos (point)))
  19982. (beginning-of-line 1)
  19983. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  19984. (if (eq special t)
  19985. (if (or (< pos (match-beginning 1))
  19986. (= pos (match-end 0)))
  19987. (goto-char (match-beginning 1))
  19988. (goto-char (match-end 0)))
  19989. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  19990. (goto-char (match-end 0))
  19991. (goto-char (match-beginning 1))))
  19992. (call-interactively (if (fboundp 'move-end-of-line)
  19993. 'move-end-of-line
  19994. 'end-of-line)))))
  19995. ((org-at-drawer-p)
  19996. (move-end-of-line 1)
  19997. (when (overlays-at (1- (point))) (backward-char 1)))
  19998. ;; At an item: Move before any hidden text.
  19999. (t (call-interactively
  20000. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  20001. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20002. (t 'end-of-line)))))
  20003. (org-no-warnings
  20004. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20005. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20006. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20007. (defun org-backward-sentence (&optional arg)
  20008. "Go to beginning of sentence, or beginning of table field.
  20009. This will call `backward-sentence' or `org-table-beginning-of-field',
  20010. depending on context."
  20011. (interactive "P")
  20012. (cond
  20013. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20014. (t (call-interactively 'backward-sentence))))
  20015. (defun org-forward-sentence (&optional arg)
  20016. "Go to end of sentence, or end of table field.
  20017. This will call `forward-sentence' or `org-table-end-of-field',
  20018. depending on context."
  20019. (interactive "P")
  20020. (cond
  20021. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20022. (t (call-interactively 'forward-sentence))))
  20023. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20024. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20025. (defun org-kill-line (&optional arg)
  20026. "Kill line, to tags or end of line."
  20027. (interactive "P")
  20028. (cond
  20029. ((or (not org-special-ctrl-k)
  20030. (bolp)
  20031. (not (org-at-heading-p)))
  20032. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20033. org-ctrl-k-protect-subtree)
  20034. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20035. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20036. (error "C-k aborted - would kill hidden subtree")))
  20037. (call-interactively
  20038. (if (and (boundp 'visual-line-mode) visual-line-mode) 'kill-visual-line 'kill-line)))
  20039. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20040. (kill-region (point) (match-beginning 1))
  20041. (org-set-tags nil t))
  20042. (t (kill-region (point) (point-at-eol)))))
  20043. (define-key org-mode-map "\C-k" 'org-kill-line)
  20044. (defun org-yank (&optional arg)
  20045. "Yank. If the kill is a subtree, treat it specially.
  20046. This command will look at the current kill and check if is a single
  20047. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20048. cursor is at the beginning of a line or after the stars of a currently
  20049. empty headline, then the yank is handled specially. How exactly depends
  20050. on the value of the following variables, both set by default.
  20051. org-yank-folded-subtrees
  20052. When set, the subtree(s) will be folded after insertion, but only
  20053. if doing so would now swallow text after the yanked text.
  20054. org-yank-adjusted-subtrees
  20055. When set, the subtree will be promoted or demoted in order to
  20056. fit into the local outline tree structure, which means that the level
  20057. will be adjusted so that it becomes the smaller one of the two
  20058. *visible* surrounding headings.
  20059. Any prefix to this command will cause `yank' to be called directly with
  20060. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20061. will just
  20062. plainly yank the text as it is.
  20063. \[1] The test checks if the first non-white line is a heading
  20064. and if there are no other headings with fewer stars."
  20065. (interactive "P")
  20066. (org-yank-generic 'yank arg))
  20067. (defun org-yank-generic (command arg)
  20068. "Perform some yank-like command.
  20069. This function implements the behavior described in the `org-yank'
  20070. documentation. However, it has been generalized to work for any
  20071. interactive command with similar behavior."
  20072. ;; pretend to be command COMMAND
  20073. (setq this-command command)
  20074. (if arg
  20075. (call-interactively command)
  20076. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20077. (and (org-kill-is-subtree-p)
  20078. (or (bolp)
  20079. (and (looking-at "[ \t]*$")
  20080. (string-match
  20081. "\\`\\*+\\'"
  20082. (buffer-substring (point-at-bol) (point)))))))
  20083. swallowp)
  20084. (cond
  20085. ((and subtreep org-yank-folded-subtrees)
  20086. (let ((beg (point))
  20087. end)
  20088. (if (and subtreep org-yank-adjusted-subtrees)
  20089. (org-paste-subtree nil nil 'for-yank)
  20090. (call-interactively command))
  20091. (setq end (point))
  20092. (goto-char beg)
  20093. (when (and (bolp) subtreep
  20094. (not (setq swallowp
  20095. (org-yank-folding-would-swallow-text beg end))))
  20096. (org-with-limited-levels
  20097. (or (looking-at org-outline-regexp)
  20098. (re-search-forward org-outline-regexp-bol end t))
  20099. (while (and (< (point) end) (looking-at org-outline-regexp))
  20100. (hide-subtree)
  20101. (org-cycle-show-empty-lines 'folded)
  20102. (condition-case nil
  20103. (outline-forward-same-level 1)
  20104. (error (goto-char end))))))
  20105. (when swallowp
  20106. (message
  20107. "Inserted text not folded because that would swallow text"))
  20108. (goto-char end)
  20109. (skip-chars-forward " \t\n\r")
  20110. (beginning-of-line 1)
  20111. (push-mark beg 'nomsg)))
  20112. ((and subtreep org-yank-adjusted-subtrees)
  20113. (let ((beg (point-at-bol)))
  20114. (org-paste-subtree nil nil 'for-yank)
  20115. (push-mark beg 'nomsg)))
  20116. (t
  20117. (call-interactively command))))))
  20118. (defun org-yank-folding-would-swallow-text (beg end)
  20119. "Would hide-subtree at BEG swallow any text after END?"
  20120. (let (level)
  20121. (org-with-limited-levels
  20122. (save-excursion
  20123. (goto-char beg)
  20124. (when (or (looking-at org-outline-regexp)
  20125. (re-search-forward org-outline-regexp-bol end t))
  20126. (setq level (org-outline-level)))
  20127. (goto-char end)
  20128. (skip-chars-forward " \t\r\n\v\f")
  20129. (if (or (eobp)
  20130. (and (bolp) (looking-at org-outline-regexp)
  20131. (<= (org-outline-level) level)))
  20132. nil ; Nothing would be swallowed
  20133. t))))) ; something would swallow
  20134. (define-key org-mode-map "\C-y" 'org-yank)
  20135. (defun org-truely-invisible-p ()
  20136. "Check if point is at a character currently not visible.
  20137. This version does not only check the character property, but also
  20138. `visible-mode'."
  20139. ;; Early versions of noutline don't have `outline-invisible-p'.
  20140. (if (org-bound-and-true-p visible-mode)
  20141. nil
  20142. (outline-invisible-p)))
  20143. (defun org-invisible-p2 ()
  20144. "Check if point is at a character currently not visible."
  20145. (save-excursion
  20146. (if (and (eolp) (not (bobp))) (backward-char 1))
  20147. ;; Early versions of noutline don't have `outline-invisible-p'.
  20148. (outline-invisible-p)))
  20149. (defun org-back-to-heading (&optional invisible-ok)
  20150. "Call `outline-back-to-heading', but provide a better error message."
  20151. (condition-case nil
  20152. (outline-back-to-heading invisible-ok)
  20153. (error (error "Before first headline at position %d in buffer %s"
  20154. (point) (current-buffer)))))
  20155. (defun org-before-first-heading-p ()
  20156. "Before first heading?"
  20157. (save-excursion
  20158. (end-of-line)
  20159. (null (re-search-backward org-outline-regexp-bol nil t))))
  20160. (defun org-at-heading-p (&optional ignored)
  20161. (outline-on-heading-p t))
  20162. ;; Compatibility alias with Org versions < 7.8.03
  20163. (defalias 'org-on-heading-p 'org-at-heading-p)
  20164. (defun org-at-comment-p nil
  20165. "Is cursor in a line starting with a # character?"
  20166. (save-excursion
  20167. (beginning-of-line)
  20168. (looking-at "^#")))
  20169. (defun org-at-drawer-p nil
  20170. "Is cursor at a drawer keyword?"
  20171. (save-excursion
  20172. (move-beginning-of-line 1)
  20173. (looking-at org-drawer-regexp)))
  20174. (defun org-at-block-p nil
  20175. "Is cursor at a block keyword?"
  20176. (save-excursion
  20177. (move-beginning-of-line 1)
  20178. (looking-at org-block-regexp)))
  20179. (defun org-point-at-end-of-empty-headline ()
  20180. "If point is at the end of an empty headline, return t, else nil.
  20181. If the heading only contains a TODO keyword, it is still still considered
  20182. empty."
  20183. (and (looking-at "[ \t]*$")
  20184. (when org-todo-line-regexp
  20185. (save-excursion
  20186. (beginning-of-line 1)
  20187. (let ((case-fold-search nil))
  20188. (looking-at org-todo-line-regexp)
  20189. (string= (match-string 3) ""))))))
  20190. (defun org-at-heading-or-item-p ()
  20191. (or (org-at-heading-p) (org-at-item-p)))
  20192. (defun org-at-target-p ()
  20193. (or (org-in-regexp org-radio-target-regexp)
  20194. (org-in-regexp org-target-regexp)))
  20195. ;; Compatibility alias with Org versions < 7.8.03
  20196. (defalias 'org-on-target-p 'org-at-target-p)
  20197. (defun org-up-heading-all (arg)
  20198. "Move to the heading line of which the present line is a subheading.
  20199. This function considers both visible and invisible heading lines.
  20200. With argument, move up ARG levels."
  20201. (if (fboundp 'outline-up-heading-all)
  20202. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20203. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20204. (defun org-up-heading-safe ()
  20205. "Move to the heading line of which the present line is a subheading.
  20206. This version will not throw an error. It will return the level of the
  20207. headline found, or nil if no higher level is found.
  20208. Also, this function will be a lot faster than `outline-up-heading',
  20209. because it relies on stars being the outline starters. This can really
  20210. make a significant difference in outlines with very many siblings."
  20211. (let (start-level re)
  20212. (org-back-to-heading t)
  20213. (setq start-level (funcall outline-level))
  20214. (if (equal start-level 1)
  20215. nil
  20216. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20217. (if (re-search-backward re nil t)
  20218. (funcall outline-level)))))
  20219. (defun org-first-sibling-p ()
  20220. "Is this heading the first child of its parents?"
  20221. (interactive)
  20222. (let ((re org-outline-regexp-bol)
  20223. level l)
  20224. (unless (org-at-heading-p t)
  20225. (error "Not at a heading"))
  20226. (setq level (funcall outline-level))
  20227. (save-excursion
  20228. (if (not (re-search-backward re nil t))
  20229. t
  20230. (setq l (funcall outline-level))
  20231. (< l level)))))
  20232. (defun org-goto-sibling (&optional previous)
  20233. "Goto the next sibling, even if it is invisible.
  20234. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20235. when a sibling was found. When none is found, return nil and don't
  20236. move point."
  20237. (let ((fun (if previous 're-search-backward 're-search-forward))
  20238. (pos (point))
  20239. (re org-outline-regexp-bol)
  20240. level l)
  20241. (when (condition-case nil (org-back-to-heading t) (error nil))
  20242. (setq level (funcall outline-level))
  20243. (catch 'exit
  20244. (or previous (forward-char 1))
  20245. (while (funcall fun re nil t)
  20246. (setq l (funcall outline-level))
  20247. (when (< l level) (goto-char pos) (throw 'exit nil))
  20248. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20249. (goto-char pos)
  20250. nil))))
  20251. (defun org-show-siblings ()
  20252. "Show all siblings of the current headline."
  20253. (save-excursion
  20254. (while (org-goto-sibling) (org-flag-heading nil)))
  20255. (save-excursion
  20256. (while (org-goto-sibling 'previous)
  20257. (org-flag-heading nil))))
  20258. (defun org-goto-first-child ()
  20259. "Goto the first child, even if it is invisible.
  20260. Return t when a child was found. Otherwise don't move point and
  20261. return nil."
  20262. (let (level (pos (point)) (re org-outline-regexp-bol))
  20263. (when (condition-case nil (org-back-to-heading t) (error nil))
  20264. (setq level (outline-level))
  20265. (forward-char 1)
  20266. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20267. (progn (goto-char (match-beginning 0)) t)
  20268. (goto-char pos) nil))))
  20269. (defun org-show-hidden-entry ()
  20270. "Show an entry where even the heading is hidden."
  20271. (save-excursion
  20272. (org-show-entry)))
  20273. (defun org-flag-heading (flag &optional entry)
  20274. "Flag the current heading. FLAG non-nil means make invisible.
  20275. When ENTRY is non-nil, show the entire entry."
  20276. (save-excursion
  20277. (org-back-to-heading t)
  20278. ;; Check if we should show the entire entry
  20279. (if entry
  20280. (progn
  20281. (org-show-entry)
  20282. (save-excursion
  20283. (and (outline-next-heading)
  20284. (org-flag-heading nil))))
  20285. (outline-flag-region (max (point-min) (1- (point)))
  20286. (save-excursion (outline-end-of-heading) (point))
  20287. flag))))
  20288. (defun org-get-next-sibling ()
  20289. "Move to next heading of the same level, and return point.
  20290. If there is no such heading, return nil.
  20291. This is like outline-next-sibling, but invisible headings are ok."
  20292. (let ((level (funcall outline-level)))
  20293. (outline-next-heading)
  20294. (while (and (not (eobp)) (> (funcall outline-level) level))
  20295. (outline-next-heading))
  20296. (if (or (eobp) (< (funcall outline-level) level))
  20297. nil
  20298. (point))))
  20299. (defun org-get-last-sibling ()
  20300. "Move to previous heading of the same level, and return point.
  20301. If there is no such heading, return nil."
  20302. (let ((opoint (point))
  20303. (level (funcall outline-level)))
  20304. (outline-previous-heading)
  20305. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20306. (while (and (> (funcall outline-level) level)
  20307. (not (bobp)))
  20308. (outline-previous-heading))
  20309. (if (< (funcall outline-level) level)
  20310. nil
  20311. (point)))))
  20312. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20313. "Goto to the end of a subtree."
  20314. ;; This contains an exact copy of the original function, but it uses
  20315. ;; `org-back-to-heading', to make it work also in invisible
  20316. ;; trees. And is uses an invisible-ok argument.
  20317. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20318. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20319. ;; with many children and grandchildren etc, this can be much faster
  20320. ;; than the outline version.
  20321. (org-back-to-heading invisible-ok)
  20322. (let ((first t)
  20323. (level (funcall outline-level)))
  20324. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20325. ;; A true heading (not a plain list item), in Org-mode
  20326. ;; This means we can easily find the end by looking
  20327. ;; only for the right number of stars. Using a regexp to do
  20328. ;; this is so much faster than using a Lisp loop.
  20329. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20330. (forward-char 1)
  20331. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20332. ;; something else, do it the slow way
  20333. (while (and (not (eobp))
  20334. (or first (> (funcall outline-level) level)))
  20335. (setq first nil)
  20336. (outline-next-heading)))
  20337. (unless to-heading
  20338. (if (memq (preceding-char) '(?\n ?\^M))
  20339. (progn
  20340. ;; Go to end of line before heading
  20341. (forward-char -1)
  20342. (if (memq (preceding-char) '(?\n ?\^M))
  20343. ;; leave blank line before heading
  20344. (forward-char -1))))))
  20345. (point))
  20346. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20347. "Use Org version in org-mode, for dramatic speed-up."
  20348. (if (derived-mode-p 'org-mode)
  20349. (progn
  20350. (org-end-of-subtree nil t)
  20351. (unless (eobp) (backward-char 1)))
  20352. ad-do-it))
  20353. (defun org-end-of-meta-data-and-drawers ()
  20354. "Jump to the first text after meta data and drawers in the current entry.
  20355. This will move over empty lines, lines with planning time stamps,
  20356. clocking lines, and drawers."
  20357. (org-back-to-heading t)
  20358. (let ((end (save-excursion (outline-next-heading) (point)))
  20359. (re (concat "\\(" org-drawer-regexp "\\)"
  20360. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20361. (forward-line 1)
  20362. (while (re-search-forward re end t)
  20363. (if (not (match-end 1))
  20364. ;; empty or planning line
  20365. (forward-line 1)
  20366. ;; a drawer, find the end
  20367. (re-search-forward "^[ \t]*:END:" end 'move)
  20368. (forward-line 1)))
  20369. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20370. (point)))
  20371. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20372. "Move forward to the arg'th subheading at same level as this one.
  20373. Stop at the first and last subheadings of a superior heading.
  20374. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20375. non-nil it will also look at invisible ones."
  20376. (interactive "p")
  20377. (org-back-to-heading invisible-ok)
  20378. (org-at-heading-p)
  20379. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20380. (re (format "^\\*\\{1,%d\\} " level))
  20381. l)
  20382. (forward-char 1)
  20383. (while (> arg 0)
  20384. (while (and (re-search-forward re nil 'move)
  20385. (setq l (- (match-end 0) (match-beginning 0) 1))
  20386. (= l level)
  20387. (not invisible-ok)
  20388. (progn (backward-char 1) (outline-invisible-p)))
  20389. (if (< l level) (setq arg 1)))
  20390. (setq arg (1- arg)))
  20391. (beginning-of-line 1)))
  20392. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20393. "Move backward to the arg'th subheading at same level as this one.
  20394. Stop at the first and last subheadings of a superior heading."
  20395. (interactive "p")
  20396. (org-back-to-heading)
  20397. (org-at-heading-p)
  20398. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20399. (re (format "^\\*\\{1,%d\\} " level))
  20400. l)
  20401. (while (> arg 0)
  20402. (while (and (re-search-backward re nil 'move)
  20403. (setq l (- (match-end 0) (match-beginning 0) 1))
  20404. (= l level)
  20405. (not invisible-ok)
  20406. (outline-invisible-p))
  20407. (if (< l level) (setq arg 1)))
  20408. (setq arg (1- arg)))))
  20409. ;;;###autoload
  20410. (defun org-forward-element ()
  20411. "Move forward by one element.
  20412. Move to the next element at the same level, when possible."
  20413. (interactive)
  20414. (cond ((eobp) (error "Cannot move further down"))
  20415. ((org-with-limited-levels (org-at-heading-p))
  20416. (let ((origin (point)))
  20417. (org-forward-heading-same-level 1)
  20418. (unless (org-with-limited-levels (org-at-heading-p))
  20419. (goto-char origin)
  20420. (error "Cannot move further down"))))
  20421. (t
  20422. (let* ((elem (org-element-at-point))
  20423. (end (org-element-property :end elem))
  20424. (parent (org-element-property :parent elem)))
  20425. (if (and parent (= (org-element-property :contents-end parent) end))
  20426. (goto-char (org-element-property :end parent))
  20427. (goto-char end))))))
  20428. ;;;###autoload
  20429. (defun org-backward-element ()
  20430. "Move backward by one element.
  20431. Move to the previous element at the same level, when possible."
  20432. (interactive)
  20433. (cond ((bobp) (error "Cannot move further up"))
  20434. ((org-with-limited-levels (org-at-heading-p))
  20435. ;; At an headline, move to the previous one, if any, or stay
  20436. ;; here.
  20437. (let ((origin (point)))
  20438. (org-backward-heading-same-level 1)
  20439. (unless (org-with-limited-levels (org-at-heading-p))
  20440. (goto-char origin)
  20441. (error "Cannot move further up"))))
  20442. (t
  20443. (let* ((trail (org-element-at-point 'keep-trail))
  20444. (elem (car trail))
  20445. (prev-elem (nth 1 trail))
  20446. (beg (org-element-property :begin elem)))
  20447. (cond
  20448. ;; Move to beginning of current element if point isn't
  20449. ;; there already.
  20450. ((/= (point) beg) (goto-char beg))
  20451. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  20452. ((org-before-first-heading-p) (goto-char (point-min)))
  20453. (t (org-back-to-heading)))))))
  20454. ;;;###autoload
  20455. (defun org-up-element ()
  20456. "Move to upper element."
  20457. (interactive)
  20458. (if (org-with-limited-levels (org-at-heading-p))
  20459. (unless (org-up-heading-safe) (error "No surrounding element"))
  20460. (let* ((elem (org-element-at-point))
  20461. (parent (org-element-property :parent elem)))
  20462. (if parent (goto-char (org-element-property :begin parent))
  20463. (if (org-with-limited-levels (org-before-first-heading-p))
  20464. (error "No surrounding element")
  20465. (org-with-limited-levels (org-back-to-heading)))))))
  20466. ;;;###autoload
  20467. (defvar org-element-greater-elements)
  20468. (defun org-down-element ()
  20469. "Move to inner element."
  20470. (interactive)
  20471. (let ((element (org-element-at-point)))
  20472. (cond
  20473. ((memq (org-element-type element) '(plain-list table))
  20474. (goto-char (org-element-property :contents-begin element))
  20475. (forward-char))
  20476. ((memq (org-element-type element) org-element-greater-elements)
  20477. ;; If contents are hidden, first disclose them.
  20478. (when (org-element-property :hiddenp element) (org-cycle))
  20479. (goto-char (or (org-element-property :contents-begin element)
  20480. (error "No content for this element"))))
  20481. (t (error "No inner element")))))
  20482. ;;;###autoload
  20483. (defun org-drag-element-backward ()
  20484. "Move backward element at point."
  20485. (interactive)
  20486. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  20487. (let* ((trail (org-element-at-point 'keep-trail))
  20488. (elem (car trail))
  20489. (prev-elem (nth 1 trail)))
  20490. ;; Error out if no previous element or previous element is
  20491. ;; a parent of the current one.
  20492. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  20493. (error "Cannot drag element backward")
  20494. (let ((pos (point)))
  20495. (org-element-swap-A-B prev-elem elem)
  20496. (goto-char (+ (org-element-property :begin prev-elem)
  20497. (- pos (org-element-property :begin elem)))))))))
  20498. ;;;###autoload
  20499. (defun org-drag-element-forward ()
  20500. "Move forward element at point."
  20501. (interactive)
  20502. (let* ((pos (point))
  20503. (elem (org-element-at-point)))
  20504. (when (= (point-max) (org-element-property :end elem))
  20505. (error "Cannot drag element forward"))
  20506. (goto-char (org-element-property :end elem))
  20507. (let ((next-elem (org-element-at-point)))
  20508. (when (or (org-element-nested-p elem next-elem)
  20509. (and (eq (org-element-type next-elem) 'headline)
  20510. (not (eq (org-element-type elem) 'headline))))
  20511. (goto-char pos)
  20512. (error "Cannot drag element forward"))
  20513. ;; Compute new position of point: it's shifted by NEXT-ELEM
  20514. ;; body's length (without final blanks) and by the length of
  20515. ;; blanks between ELEM and NEXT-ELEM.
  20516. (let ((size-next (- (save-excursion
  20517. (goto-char (org-element-property :end next-elem))
  20518. (skip-chars-backward " \r\t\n")
  20519. (forward-line)
  20520. ;; Small correction if buffer doesn't end
  20521. ;; with a newline character.
  20522. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  20523. (org-element-property :begin next-elem)))
  20524. (size-blank (- (org-element-property :end elem)
  20525. (save-excursion
  20526. (goto-char (org-element-property :end elem))
  20527. (skip-chars-backward " \r\t\n")
  20528. (forward-line)
  20529. (point)))))
  20530. (org-element-swap-A-B elem next-elem)
  20531. (goto-char (+ pos size-next size-blank))))))
  20532. ;;;###autoload
  20533. (defun org-mark-element ()
  20534. "Put point at beginning of this element, mark at end.
  20535. Interactively, if this command is repeated or (in Transient Mark
  20536. mode) if the mark is active, it marks the next element after the
  20537. ones already marked."
  20538. (interactive)
  20539. (let (deactivate-mark)
  20540. (if (and (org-called-interactively-p 'any)
  20541. (or (and (eq last-command this-command) (mark t))
  20542. (and transient-mark-mode mark-active)))
  20543. (set-mark
  20544. (save-excursion
  20545. (goto-char (mark))
  20546. (goto-char (org-element-property :end (org-element-at-point)))))
  20547. (let ((element (org-element-at-point)))
  20548. (end-of-line)
  20549. (push-mark (org-element-property :end element) t t)
  20550. (goto-char (org-element-property :begin element))))))
  20551. ;;;###autoload
  20552. (defun org-narrow-to-element ()
  20553. "Narrow buffer to current element."
  20554. (interactive)
  20555. (let ((elem (org-element-at-point)))
  20556. (cond
  20557. ((eq (car elem) 'headline)
  20558. (narrow-to-region
  20559. (org-element-property :begin elem)
  20560. (org-element-property :end elem)))
  20561. ((memq (car elem) org-element-greater-elements)
  20562. (narrow-to-region
  20563. (org-element-property :contents-begin elem)
  20564. (org-element-property :contents-end elem)))
  20565. (t
  20566. (narrow-to-region
  20567. (org-element-property :begin elem)
  20568. (org-element-property :end elem))))))
  20569. ;;;###autoload
  20570. (defun org-transpose-element ()
  20571. "Transpose current and previous elements, keeping blank lines between.
  20572. Point is moved after both elements."
  20573. (interactive)
  20574. (org-skip-whitespace)
  20575. (let ((end (org-element-property :end (org-element-at-point))))
  20576. (org-drag-element-backward)
  20577. (goto-char end)))
  20578. ;;;###autoload
  20579. (defun org-unindent-buffer ()
  20580. "Un-indent the visible part of the buffer.
  20581. Relative indentation (between items, inside blocks, etc.) isn't
  20582. modified."
  20583. (interactive)
  20584. (unless (eq major-mode 'org-mode)
  20585. (error "Cannot un-indent a buffer not in Org mode"))
  20586. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  20587. unindent-tree ; For byte-compiler.
  20588. (unindent-tree
  20589. (function
  20590. (lambda (contents)
  20591. (mapc
  20592. (lambda (element)
  20593. (if (memq (org-element-type element) '(headline section))
  20594. (funcall unindent-tree (org-element-contents element))
  20595. (save-excursion
  20596. (save-restriction
  20597. (narrow-to-region
  20598. (org-element-property :begin element)
  20599. (org-element-property :end element))
  20600. (org-do-remove-indentation)))))
  20601. (reverse contents))))))
  20602. (funcall unindent-tree (org-element-contents parse-tree))))
  20603. (defun org-show-subtree ()
  20604. "Show everything after this heading at deeper levels."
  20605. (interactive)
  20606. (outline-flag-region
  20607. (point)
  20608. (save-excursion
  20609. (org-end-of-subtree t t))
  20610. nil))
  20611. (defun org-show-entry ()
  20612. "Show the body directly following this heading.
  20613. Show the heading too, if it is currently invisible."
  20614. (interactive)
  20615. (save-excursion
  20616. (condition-case nil
  20617. (progn
  20618. (org-back-to-heading t)
  20619. (outline-flag-region
  20620. (max (point-min) (1- (point)))
  20621. (save-excursion
  20622. (if (re-search-forward
  20623. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  20624. (match-beginning 1)
  20625. (point-max)))
  20626. nil)
  20627. (org-cycle-hide-drawers 'children))
  20628. (error nil))))
  20629. (defun org-make-options-regexp (kwds &optional extra)
  20630. "Make a regular expression for keyword lines."
  20631. (concat
  20632. "^#\\+\\("
  20633. (mapconcat 'regexp-quote kwds "\\|")
  20634. (if extra (concat "\\|" extra))
  20635. "\\):[ \t]*\\(.*\\)"))
  20636. ;; Make isearch reveal the necessary context
  20637. (defun org-isearch-end ()
  20638. "Reveal context after isearch exits."
  20639. (when isearch-success ; only if search was successful
  20640. (if (featurep 'xemacs)
  20641. ;; Under XEmacs, the hook is run in the correct place,
  20642. ;; we directly show the context.
  20643. (org-show-context 'isearch)
  20644. ;; In Emacs the hook runs *before* restoring the overlays.
  20645. ;; So we have to use a one-time post-command-hook to do this.
  20646. ;; (Emacs 22 has a special variable, see function `org-mode')
  20647. (unless (and (boundp 'isearch-mode-end-hook-quit)
  20648. isearch-mode-end-hook-quit)
  20649. ;; Only when the isearch was not quitted.
  20650. (org-add-hook 'post-command-hook 'org-isearch-post-command
  20651. 'append 'local)))))
  20652. (defun org-isearch-post-command ()
  20653. "Remove self from hook, and show context."
  20654. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  20655. (org-show-context 'isearch))
  20656. ;;;; Integration with and fixes for other packages
  20657. ;;; Imenu support
  20658. (defvar org-imenu-markers nil
  20659. "All markers currently used by Imenu.")
  20660. (make-variable-buffer-local 'org-imenu-markers)
  20661. (defun org-imenu-new-marker (&optional pos)
  20662. "Return a new marker for use by Imenu, and remember the marker."
  20663. (let ((m (make-marker)))
  20664. (move-marker m (or pos (point)))
  20665. (push m org-imenu-markers)
  20666. m))
  20667. (defun org-imenu-get-tree ()
  20668. "Produce the index for Imenu."
  20669. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  20670. (setq org-imenu-markers nil)
  20671. (let* ((n org-imenu-depth)
  20672. (re (concat "^" (org-get-limited-outline-regexp)))
  20673. (subs (make-vector (1+ n) nil))
  20674. (last-level 0)
  20675. m level head)
  20676. (save-excursion
  20677. (save-restriction
  20678. (widen)
  20679. (goto-char (point-max))
  20680. (while (re-search-backward re nil t)
  20681. (setq level (org-reduced-level (funcall outline-level)))
  20682. (when (and (<= level n)
  20683. (looking-at org-complex-heading-regexp))
  20684. (setq head (org-link-display-format
  20685. (org-match-string-no-properties 4))
  20686. m (org-imenu-new-marker))
  20687. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  20688. (if (>= level last-level)
  20689. (push (cons head m) (aref subs level))
  20690. (push (cons head (aref subs (1+ level))) (aref subs level))
  20691. (loop for i from (1+ level) to n do (aset subs i nil)))
  20692. (setq last-level level)))))
  20693. (aref subs 1)))
  20694. (eval-after-load "imenu"
  20695. '(progn
  20696. (add-hook 'imenu-after-jump-hook
  20697. (lambda ()
  20698. (if (derived-mode-p 'org-mode)
  20699. (org-show-context 'org-goto))))))
  20700. (defun org-link-display-format (link)
  20701. "Replace a link with either the description, or the link target
  20702. if no description is present"
  20703. (save-match-data
  20704. (if (string-match org-bracket-link-analytic-regexp link)
  20705. (replace-match (if (match-end 5)
  20706. (match-string 5 link)
  20707. (concat (match-string 1 link)
  20708. (match-string 3 link)))
  20709. nil t link)
  20710. link)))
  20711. (defun org-toggle-link-display ()
  20712. "Toggle the literal or descriptive display of links."
  20713. (interactive)
  20714. (if org-descriptive-links
  20715. (progn (org-remove-from-invisibility-spec '(org-link))
  20716. (org-restart-font-lock)
  20717. (setq org-descriptive-links nil))
  20718. (progn (add-to-invisibility-spec '(org-link))
  20719. (org-restart-font-lock)
  20720. (setq org-descriptive-links t))))
  20721. ;; Speedbar support
  20722. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  20723. "Overlay marking the agenda restriction line in speedbar.")
  20724. (overlay-put org-speedbar-restriction-lock-overlay
  20725. 'face 'org-agenda-restriction-lock)
  20726. (overlay-put org-speedbar-restriction-lock-overlay
  20727. 'help-echo "Agendas are currently limited to this item.")
  20728. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20729. (defun org-speedbar-set-agenda-restriction ()
  20730. "Restrict future agenda commands to the location at point in speedbar.
  20731. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  20732. (interactive)
  20733. (require 'org-agenda)
  20734. (let (p m tp np dir txt)
  20735. (cond
  20736. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  20737. 'org-imenu t))
  20738. (setq m (get-text-property p 'org-imenu-marker))
  20739. (with-current-buffer (marker-buffer m)
  20740. (goto-char m)
  20741. (org-agenda-set-restriction-lock 'subtree)))
  20742. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  20743. 'speedbar-function 'speedbar-find-file))
  20744. (setq tp (previous-single-property-change
  20745. (1+ p) 'speedbar-function)
  20746. np (next-single-property-change
  20747. tp 'speedbar-function)
  20748. dir (speedbar-line-directory)
  20749. txt (buffer-substring-no-properties (or tp (point-min))
  20750. (or np (point-max))))
  20751. (with-current-buffer (find-file-noselect
  20752. (let ((default-directory dir))
  20753. (expand-file-name txt)))
  20754. (unless (derived-mode-p 'org-mode)
  20755. (error "Cannot restrict to non-Org-mode file"))
  20756. (org-agenda-set-restriction-lock 'file)))
  20757. (t (error "Don't know how to restrict Org-mode's agenda")))
  20758. (move-overlay org-speedbar-restriction-lock-overlay
  20759. (point-at-bol) (point-at-eol))
  20760. (setq current-prefix-arg nil)
  20761. (org-agenda-maybe-redo)))
  20762. (eval-after-load "speedbar"
  20763. '(progn
  20764. (speedbar-add-supported-extension ".org")
  20765. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  20766. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  20767. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  20768. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  20769. (add-hook 'speedbar-visiting-tag-hook
  20770. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  20771. ;;; Fixes and Hacks for problems with other packages
  20772. ;; Make flyspell not check words in links, to not mess up our keymap
  20773. (defun org-mode-flyspell-verify ()
  20774. "Don't let flyspell put overlays at active buttons, or on
  20775. {todo,all-time,additional-option-like}-keywords."
  20776. (let ((pos (max (1- (point)) (point-min)))
  20777. (word (thing-at-point 'word)))
  20778. (and (not (get-text-property pos 'keymap))
  20779. (not (get-text-property pos 'org-no-flyspell))
  20780. (not (member word org-todo-keywords-1))
  20781. (not (member word org-all-time-keywords))
  20782. (not (member word org-options-keywords))
  20783. (not (member word (mapcar 'car org-startup-options)))
  20784. (not (member word org-additional-option-like-keywords-for-flyspell)))))
  20785. (defun org-remove-flyspell-overlays-in (beg end)
  20786. "Remove flyspell overlays in region."
  20787. (and (org-bound-and-true-p flyspell-mode)
  20788. (fboundp 'flyspell-delete-region-overlays)
  20789. (flyspell-delete-region-overlays beg end))
  20790. (add-text-properties beg end '(org-no-flyspell t)))
  20791. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  20792. (eval-after-load "bookmark"
  20793. '(if (boundp 'bookmark-after-jump-hook)
  20794. ;; We can use the hook
  20795. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  20796. ;; Hook not available, use advice
  20797. (defadvice bookmark-jump (after org-make-visible activate)
  20798. "Make the position visible."
  20799. (org-bookmark-jump-unhide))))
  20800. ;; Make sure saveplace shows the location if it was hidden
  20801. (eval-after-load "saveplace"
  20802. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  20803. "Make the position visible."
  20804. (org-bookmark-jump-unhide)))
  20805. ;; Make sure ecb shows the location if it was hidden
  20806. (eval-after-load "ecb"
  20807. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  20808. "Make hierarchy visible when jumping into location from ECB tree buffer."
  20809. (if (derived-mode-p 'org-mode)
  20810. (org-show-context))))
  20811. (defun org-bookmark-jump-unhide ()
  20812. "Unhide the current position, to show the bookmark location."
  20813. (and (derived-mode-p 'org-mode)
  20814. (or (outline-invisible-p)
  20815. (save-excursion (goto-char (max (point-min) (1- (point))))
  20816. (outline-invisible-p)))
  20817. (org-show-context 'bookmark-jump)))
  20818. ;; Make session.el ignore our circular variable
  20819. (eval-after-load "session"
  20820. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  20821. ;;;; Experimental code
  20822. (defun org-closed-in-range ()
  20823. "Sparse tree of items closed in a certain time range.
  20824. Still experimental, may disappear in the future."
  20825. (interactive)
  20826. ;; Get the time interval from the user.
  20827. (let* ((time1 (org-float-time
  20828. (org-read-date nil 'to-time nil "Starting date: ")))
  20829. (time2 (org-float-time
  20830. (org-read-date nil 'to-time nil "End date:")))
  20831. ;; callback function
  20832. (callback (lambda ()
  20833. (let ((time
  20834. (org-float-time
  20835. (apply 'encode-time
  20836. (org-parse-time-string
  20837. (match-string 1))))))
  20838. ;; check if time in interval
  20839. (and (>= time time1) (<= time time2))))))
  20840. ;; make tree, check each match with the callback
  20841. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  20842. ;;;; Finish up
  20843. (provide 'org)
  20844. (run-hooks 'org-load-hook)
  20845. ;;; org.el ends here