org.el 882 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2013 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. (load "org-loaddefs.el" t t t)
  74. (require 'org-macs)
  75. (require 'org-compat)
  76. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  77. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  78. ;;
  79. ;; In Org buffers, the value of `outline-regexp' is that of
  80. ;; `org-outline-regexp'. The only function still directly relying on
  81. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  82. ;; job when `orgstruct-mode' is active.
  83. (defvar org-outline-regexp "\\*+ "
  84. "Regexp to match Org headlines.")
  85. (defvar org-outline-regexp-bol "^\\*+ "
  86. "Regexp to match Org headlines.
  87. This is similar to `org-outline-regexp' but additionally makes
  88. sure that we are at the beginning of the line.")
  89. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  90. "Matches a headline, putting stars and text into groups.
  91. Stars are put in group 1 and the trimmed body in group 2.")
  92. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  93. (unless (boundp 'calendar-view-holidays-initially-flag)
  94. (org-defvaralias 'calendar-view-holidays-initially-flag
  95. 'view-calendar-holidays-initially))
  96. (unless (boundp 'calendar-view-diary-initially-flag)
  97. (org-defvaralias 'calendar-view-diary-initially-flag
  98. 'view-diary-entries-initially))
  99. (unless (boundp 'diary-fancy-buffer)
  100. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  101. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  102. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  103. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  104. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  105. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  106. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  107. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  108. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  109. (declare-function orgtbl-mode "org-table" (&optional arg))
  110. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  111. (declare-function org-beamer-mode "ox-beamer" ())
  112. (declare-function org-table-edit-field "org-table" (arg))
  113. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  114. (declare-function org-id-get-create "org-id" (&optional force))
  115. (declare-function org-id-find-id-file "org-id" (id))
  116. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  117. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  118. (declare-function org-table-align "org-table" ())
  119. (declare-function org-table-paste-rectangle "org-table" ())
  120. (declare-function org-table-maybe-eval-formula "org-table" ())
  121. (declare-function org-table-maybe-recalculate-line "org-table" ())
  122. (declare-function org-element--parse-objects "org-element"
  123. (beg end acc restriction))
  124. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  125. (declare-function org-element-contents "org-element" (element))
  126. (declare-function org-element-context "org-element" (&optional element))
  127. (declare-function org-element-interpret-data "org-element"
  128. (data &optional parent))
  129. (declare-function org-element-map "org-element"
  130. (data types fun &optional info first-match no-recursion))
  131. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  132. (declare-function org-element-parse-buffer "org-element"
  133. (&optional granularity visible-only))
  134. (declare-function org-element-property "org-element" (property element))
  135. (declare-function org-element-put-property "org-element"
  136. (element property value))
  137. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  138. (declare-function org-element--parse-objects "org-element"
  139. (beg end acc restriction))
  140. (declare-function org-element-parse-buffer "org-element"
  141. (&optional granularity visible-only))
  142. (declare-function org-element-restriction "org-element" (element))
  143. (declare-function org-element-type "org-element" (element))
  144. ;; load languages based on value of `org-babel-load-languages'
  145. (defvar org-babel-load-languages)
  146. ;;;###autoload
  147. (defun org-babel-do-load-languages (sym value)
  148. "Load the languages defined in `org-babel-load-languages'."
  149. (set-default sym value)
  150. (mapc (lambda (pair)
  151. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  152. (if active
  153. (progn
  154. (require (intern (concat "ob-" lang))))
  155. (progn
  156. (funcall 'fmakunbound
  157. (intern (concat "org-babel-execute:" lang)))
  158. (funcall 'fmakunbound
  159. (intern (concat "org-babel-expand-body:" lang)))))))
  160. org-babel-load-languages))
  161. (defcustom org-babel-load-languages '((emacs-lisp . t))
  162. "Languages which can be evaluated in Org-mode buffers.
  163. This list can be used to load support for any of the languages
  164. below, note that each language will depend on a different set of
  165. system executables and/or Emacs modes. When a language is
  166. \"loaded\", then code blocks in that language can be evaluated
  167. with `org-babel-execute-src-block' bound by default to C-c
  168. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  169. be set to remove code block evaluation from the C-c C-c
  170. keybinding. By default only Emacs Lisp (which has no
  171. requirements) is loaded."
  172. :group 'org-babel
  173. :set 'org-babel-do-load-languages
  174. :version "24.1"
  175. :type '(alist :tag "Babel Languages"
  176. :key-type
  177. (choice
  178. (const :tag "Awk" awk)
  179. (const :tag "C" C)
  180. (const :tag "R" R)
  181. (const :tag "Asymptote" asymptote)
  182. (const :tag "Calc" calc)
  183. (const :tag "Clojure" clojure)
  184. (const :tag "CSS" css)
  185. (const :tag "Ditaa" ditaa)
  186. (const :tag "Dot" dot)
  187. (const :tag "Emacs Lisp" emacs-lisp)
  188. (const :tag "Fortran" fortran)
  189. (const :tag "Gnuplot" gnuplot)
  190. (const :tag "Haskell" haskell)
  191. (const :tag "IO" io)
  192. (const :tag "Java" java)
  193. (const :tag "Javascript" js)
  194. (const :tag "LaTeX" latex)
  195. (const :tag "Ledger" ledger)
  196. (const :tag "Lilypond" lilypond)
  197. (const :tag "Lisp" lisp)
  198. (const :tag "Makefile" makefile)
  199. (const :tag "Maxima" maxima)
  200. (const :tag "Matlab" matlab)
  201. (const :tag "Mscgen" mscgen)
  202. (const :tag "Ocaml" ocaml)
  203. (const :tag "Octave" octave)
  204. (const :tag "Org" org)
  205. (const :tag "Perl" perl)
  206. (const :tag "Pico Lisp" picolisp)
  207. (const :tag "PlantUML" plantuml)
  208. (const :tag "Python" python)
  209. (const :tag "Ruby" ruby)
  210. (const :tag "Sass" sass)
  211. (const :tag "Scala" scala)
  212. (const :tag "Scheme" scheme)
  213. (const :tag "Screen" screen)
  214. (const :tag "Shell Script" sh)
  215. (const :tag "Shen" shen)
  216. (const :tag "Sql" sql)
  217. (const :tag "Sqlite" sqlite))
  218. :value-type (boolean :tag "Activate" :value t)))
  219. ;;;; Customization variables
  220. (defcustom org-clone-delete-id nil
  221. "Remove ID property of clones of a subtree.
  222. When non-nil, clones of a subtree don't inherit the ID property.
  223. Otherwise they inherit the ID property with a new unique
  224. identifier."
  225. :type 'boolean
  226. :version "24.1"
  227. :group 'org-id)
  228. ;;; Version
  229. (org-check-version)
  230. ;;;###autoload
  231. (defun org-version (&optional here full message)
  232. "Show the org-mode version in the echo area.
  233. With prefix argument HERE, insert it at point.
  234. When FULL is non-nil, use a verbose version string.
  235. When MESSAGE is non-nil, display a message with the version."
  236. (interactive "P")
  237. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  238. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  239. (load-suffixes (list ".el"))
  240. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  241. (org-trash (or
  242. (and (fboundp 'org-release) (fboundp 'org-git-version))
  243. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  244. (load-suffixes save-load-suffixes)
  245. (org-version (org-release))
  246. (git-version (org-git-version))
  247. (version (format "Org-mode version %s (%s @ %s)"
  248. org-version
  249. git-version
  250. (if org-install-dir
  251. (if (string= org-dir org-install-dir)
  252. org-install-dir
  253. (concat "mixed installation! " org-install-dir " and " org-dir))
  254. "org-loaddefs.el can not be found!")))
  255. (_version (if full version org-version)))
  256. (if (org-called-interactively-p 'interactive)
  257. (if here
  258. (insert version)
  259. (message version))
  260. (if message (message _version))
  261. _version)))
  262. (defconst org-version (org-version))
  263. ;;; Compatibility constants
  264. ;;; The custom variables
  265. (defgroup org nil
  266. "Outline-based notes management and organizer."
  267. :tag "Org"
  268. :group 'outlines
  269. :group 'calendar)
  270. (defcustom org-mode-hook nil
  271. "Mode hook for Org-mode, run after the mode was turned on."
  272. :group 'org
  273. :type 'hook)
  274. (defcustom org-load-hook nil
  275. "Hook that is run after org.el has been loaded."
  276. :group 'org
  277. :type 'hook)
  278. (defcustom org-log-buffer-setup-hook nil
  279. "Hook that is run after an Org log buffer is created."
  280. :group 'org
  281. :version "24.1"
  282. :type 'hook)
  283. (defvar org-modules) ; defined below
  284. (defvar org-modules-loaded nil
  285. "Have the modules been loaded already?")
  286. (defun org-load-modules-maybe (&optional force)
  287. "Load all extensions listed in `org-modules'."
  288. (when (or force (not org-modules-loaded))
  289. (mapc (lambda (ext)
  290. (condition-case nil (require ext)
  291. (error (message "Problems while trying to load feature `%s'" ext))))
  292. org-modules)
  293. (setq org-modules-loaded t)))
  294. (defun org-set-modules (var value)
  295. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  296. (set var value)
  297. (when (featurep 'org)
  298. (org-load-modules-maybe 'force)))
  299. (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  300. "Modules that should always be loaded together with org.el.
  301. If a description starts with <C>, the file is not part of Emacs
  302. and loading it will require that you have downloaded and properly
  303. installed the Org mode distribution.
  304. You can also use this system to load external packages (i.e. neither Org
  305. core modules, nor modules from the CONTRIB directory). Just add symbols
  306. to the end of the list. If the package is called org-xyz.el, then you need
  307. to add the symbol `xyz', and the package must have a call to:
  308. \(provide 'org-xyz)
  309. For export specific modules, see also `org-export-backends'."
  310. :group 'org
  311. :set 'org-set-modules
  312. :version "24.4"
  313. :package-version '(Org . "8.0")
  314. :type
  315. '(set :greedy t
  316. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  317. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  318. (const :tag " crypt: Encryption of subtrees" org-crypt)
  319. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  320. (const :tag " docview: Links to doc-view buffers" org-docview)
  321. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  322. (const :tag " id: Global IDs for identifying entries" org-id)
  323. (const :tag " info: Links to Info nodes" org-info)
  324. (const :tag " habit: Track your consistency with habits" org-habit)
  325. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  326. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  327. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  328. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  329. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  330. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  331. (const :tag " mouse: Additional mouse support" org-mouse)
  332. (const :tag "C vm: Links to VM folders/messages" org-vm)
  333. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  334. (const :tag "C w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  335. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  336. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  337. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  338. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  339. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  340. (const :tag "C collector: Collect properties into tables" org-collector)
  341. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  342. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  343. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  344. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  345. (const :tag "C eval: Include command output as text" org-eval)
  346. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  347. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  348. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  349. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  350. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  351. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  352. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  353. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  354. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  355. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  356. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  357. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  358. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  359. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  360. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  361. (const :tag "C registry: A registry for Org-mode links" org-registry)
  362. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  363. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  364. (const :tag "C secretary: Team management with org-mode" org-secretary)
  365. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  366. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  367. (const :tag "C track: Keep up with Org-mode development" org-track)
  368. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  369. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  370. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  371. (defvar org-export-registered-backends) ; From ox.el
  372. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  373. (defcustom org-export-backends '(ascii html icalendar latex)
  374. "List of export back-ends that should be always available.
  375. If a description starts with <C>, the file is not part of Emacs
  376. and loading it will require that you have downloaded and properly
  377. installed the Org mode distribution.
  378. Unlike to `org-modules', libraries in this list will not be
  379. loaded along with Org, but only once the export framework is
  380. needed.
  381. This variable needs to be set before org.el is loaded. If you
  382. need to make a change while Emacs is running, use the customize
  383. interface or run the following code, where VALUE stands for the
  384. new value of the variable, after updating it:
  385. \(progn
  386. \(setq org-export-registered-backends
  387. \(org-remove-if-not
  388. \(lambda (backend)
  389. \(or (memq backend val)
  390. \(catch 'parentp
  391. \(mapc
  392. \(lambda (b)
  393. \(and (org-export-derived-backend-p b (car backend))
  394. \(throw 'parentp t)))
  395. val)
  396. nil)))
  397. org-export-registered-backends))
  398. \(let ((new-list (mapcar 'car org-export-registered-backends)))
  399. \(dolist (backend val)
  400. \(cond
  401. \((not (load (format \"ox-%s\" backend) t t))
  402. \(message \"Problems while trying to load export back-end `%s'\"
  403. backend))
  404. \((not (memq backend new-list)) (push backend new-list))))
  405. \(set-default var new-list)))
  406. Adding a back-end to this list will also pull the back-end it
  407. depends on, if any."
  408. :group 'org
  409. :group 'org-export
  410. :version "24.4"
  411. :package-version '(Org . "8.0")
  412. :initialize 'custom-initialize-set
  413. :set (lambda (var val)
  414. (if (not (featurep 'ox)) (set-default var val)
  415. ;; Any back-end not required anymore (not present in VAL and not
  416. ;; a parent of any back-end in the new value) is removed from the
  417. ;; list of registered back-ends.
  418. (setq org-export-registered-backends
  419. (org-remove-if-not
  420. (lambda (backend)
  421. (or (memq backend val)
  422. (catch 'parentp
  423. (mapc
  424. (lambda (b)
  425. (and (org-export-derived-backend-p b (car backend))
  426. (throw 'parentp t)))
  427. val)
  428. nil)))
  429. org-export-registered-backends))
  430. ;; Now build NEW-LIST of both new back-ends and required
  431. ;; parents.
  432. (let ((new-list (mapcar 'car org-export-registered-backends)))
  433. (dolist (backend val)
  434. (cond
  435. ((not (load (format "ox-%s" backend) t t))
  436. (message "Problems while trying to load export back-end `%s'"
  437. backend))
  438. ((not (memq backend new-list)) (push backend new-list))))
  439. ;; Set VAR to that list with fixed dependencies.
  440. (set-default var new-list))))
  441. :type '(set :greedy t
  442. (const :tag " ascii Export buffer to ASCII format" ascii)
  443. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  444. (const :tag " html Export buffer to HTML format" html)
  445. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  446. (const :tag " latex Export buffer to LaTeX format" latex)
  447. (const :tag " man Export buffer to MAN format" man)
  448. (const :tag " md Export buffer to Markdown format" md)
  449. (const :tag " odt Export buffer to ODT format" odt)
  450. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  451. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  452. (const :tag "C deck Export buffer to deck.js presentations" deck)
  453. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  454. (const :tag "C groff Export buffer to Groff format" groff)
  455. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  456. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  457. (const :tag "C s5 Export buffer to s5 presentations" s5)
  458. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  459. (eval-after-load 'ox
  460. '(mapc
  461. (lambda (backend)
  462. (condition-case nil (require (intern (format "ox-%s" backend)))
  463. (error (message "Problems while trying to load export back-end `%s'"
  464. backend))))
  465. org-export-backends))
  466. (defcustom org-support-shift-select nil
  467. "Non-nil means make shift-cursor commands select text when possible.
  468. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  469. start selecting a region, or enlarge regions started in this way.
  470. In Org-mode, in special contexts, these same keys are used for
  471. other purposes, important enough to compete with shift selection.
  472. Org tries to balance these needs by supporting `shift-select-mode'
  473. outside these special contexts, under control of this variable.
  474. The default of this variable is nil, to avoid confusing behavior. Shifted
  475. cursor keys will then execute Org commands in the following contexts:
  476. - on a headline, changing TODO state (left/right) and priority (up/down)
  477. - on a time stamp, changing the time
  478. - in a plain list item, changing the bullet type
  479. - in a property definition line, switching between allowed values
  480. - in the BEGIN line of a clock table (changing the time block).
  481. Outside these contexts, the commands will throw an error.
  482. When this variable is t and the cursor is not in a special
  483. context, Org-mode will support shift-selection for making and
  484. enlarging regions. To make this more effective, the bullet
  485. cycling will no longer happen anywhere in an item line, but only
  486. if the cursor is exactly on the bullet.
  487. If you set this variable to the symbol `always', then the keys
  488. will not be special in headlines, property lines, and item lines,
  489. to make shift selection work there as well. If this is what you
  490. want, you can use the following alternative commands: `C-c C-t'
  491. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  492. can be used to switch TODO sets, `C-c -' to cycle item bullet
  493. types, and properties can be edited by hand or in column view.
  494. However, when the cursor is on a timestamp, shift-cursor commands
  495. will still edit the time stamp - this is just too good to give up.
  496. XEmacs user should have this variable set to nil, because
  497. `shift-select-mode' is in Emacs 23 or later only."
  498. :group 'org
  499. :type '(choice
  500. (const :tag "Never" nil)
  501. (const :tag "When outside special context" t)
  502. (const :tag "Everywhere except timestamps" always)))
  503. (defcustom org-loop-over-headlines-in-active-region nil
  504. "Shall some commands act upon headlines in the active region?
  505. When set to `t', some commands will be performed in all headlines
  506. within the active region.
  507. When set to `start-level', some commands will be performed in all
  508. headlines within the active region, provided that these headlines
  509. are of the same level than the first one.
  510. When set to a string, those commands will be performed on the
  511. matching headlines within the active region. Such string must be
  512. a tags/property/todo match as it is used in the agenda tags view.
  513. The list of commands is: `org-schedule', `org-deadline',
  514. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  515. `org-archive-to-archive-sibling'. The archiving commands skip
  516. already archived entries."
  517. :type '(choice (const :tag "Don't loop" nil)
  518. (const :tag "All headlines in active region" t)
  519. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  520. (string :tag "Tags/Property/Todo matcher"))
  521. :version "24.1"
  522. :group 'org-todo
  523. :group 'org-archive)
  524. (defgroup org-startup nil
  525. "Options concerning startup of Org-mode."
  526. :tag "Org Startup"
  527. :group 'org)
  528. (defcustom org-startup-folded t
  529. "Non-nil means entering Org-mode will switch to OVERVIEW.
  530. This can also be configured on a per-file basis by adding one of
  531. the following lines anywhere in the buffer:
  532. #+STARTUP: fold (or `overview', this is equivalent)
  533. #+STARTUP: nofold (or `showall', this is equivalent)
  534. #+STARTUP: content
  535. #+STARTUP: showeverything
  536. By default, this option is ignored when Org opens agenda files
  537. for the first time. If you want the agenda to honor the startup
  538. option, set `org-agenda-inhibit-startup' to nil."
  539. :group 'org-startup
  540. :type '(choice
  541. (const :tag "nofold: show all" nil)
  542. (const :tag "fold: overview" t)
  543. (const :tag "content: all headlines" content)
  544. (const :tag "show everything, even drawers" showeverything)))
  545. (defcustom org-startup-truncated t
  546. "Non-nil means entering Org-mode will set `truncate-lines'.
  547. This is useful since some lines containing links can be very long and
  548. uninteresting. Also tables look terrible when wrapped."
  549. :group 'org-startup
  550. :type 'boolean)
  551. (defcustom org-startup-indented nil
  552. "Non-nil means turn on `org-indent-mode' on startup.
  553. This can also be configured on a per-file basis by adding one of
  554. the following lines anywhere in the buffer:
  555. #+STARTUP: indent
  556. #+STARTUP: noindent"
  557. :group 'org-structure
  558. :type '(choice
  559. (const :tag "Not" nil)
  560. (const :tag "Globally (slow on startup in large files)" t)))
  561. (defcustom org-use-sub-superscripts t
  562. "Non-nil means interpret \"_\" and \"^\" for display.
  563. When this option is turned on, you can use TeX-like syntax for sub- and
  564. superscripts. Several characters after \"_\" or \"^\" will be
  565. considered as a single item - so grouping with {} is normally not
  566. needed. For example, the following things will be parsed as single
  567. sub- or superscripts.
  568. 10^24 or 10^tau several digits will be considered 1 item.
  569. 10^-12 or 10^-tau a leading sign with digits or a word
  570. x^2-y^3 will be read as x^2 - y^3, because items are
  571. terminated by almost any nonword/nondigit char.
  572. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  573. Still, ambiguity is possible - so when in doubt use {} to enclose
  574. the sub/superscript. If you set this variable to the symbol
  575. `{}', the braces are *required* in order to trigger
  576. interpretations as sub/superscript. This can be helpful in
  577. documents that need \"_\" frequently in plain text."
  578. :group 'org-startup
  579. :version "24.1"
  580. :type '(choice
  581. (const :tag "Always interpret" t)
  582. (const :tag "Only with braces" {})
  583. (const :tag "Never interpret" nil)))
  584. (defcustom org-startup-with-beamer-mode nil
  585. "Non-nil means turn on `org-beamer-mode' on startup.
  586. This can also be configured on a per-file basis by adding one of
  587. the following lines anywhere in the buffer:
  588. #+STARTUP: beamer"
  589. :group 'org-startup
  590. :version "24.1"
  591. :type 'boolean)
  592. (defcustom org-startup-align-all-tables nil
  593. "Non-nil means align all tables when visiting a file.
  594. This is useful when the column width in tables is forced with <N> cookies
  595. in table fields. Such tables will look correct only after the first re-align.
  596. This can also be configured on a per-file basis by adding one of
  597. the following lines anywhere in the buffer:
  598. #+STARTUP: align
  599. #+STARTUP: noalign"
  600. :group 'org-startup
  601. :type 'boolean)
  602. (defcustom org-startup-with-inline-images nil
  603. "Non-nil means show inline images when loading a new Org file.
  604. This can also be configured on a per-file basis by adding one of
  605. the following lines anywhere in the buffer:
  606. #+STARTUP: inlineimages
  607. #+STARTUP: noinlineimages"
  608. :group 'org-startup
  609. :version "24.1"
  610. :type 'boolean)
  611. (defcustom org-startup-with-latex-preview nil
  612. "Non-nil means preview LaTeX fragments when loading a new Org file.
  613. This can also be configured on a per-file basis by adding one of
  614. the followinglines anywhere in the buffer:
  615. #+STARTUP: latexpreview
  616. #+STARTUP: nolatexpreview"
  617. :group 'org-startup
  618. :version "24.4"
  619. :package-version '(Org . "8.0")
  620. :type 'boolean)
  621. (defcustom org-insert-mode-line-in-empty-file nil
  622. "Non-nil means insert the first line setting Org-mode in empty files.
  623. When the function `org-mode' is called interactively in an empty file, this
  624. normally means that the file name does not automatically trigger Org-mode.
  625. To ensure that the file will always be in Org-mode in the future, a
  626. line enforcing Org-mode will be inserted into the buffer, if this option
  627. has been set."
  628. :group 'org-startup
  629. :type 'boolean)
  630. (defcustom org-replace-disputed-keys nil
  631. "Non-nil means use alternative key bindings for some keys.
  632. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  633. These keys are also used by other packages like shift-selection-mode'
  634. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  635. If you want to use Org-mode together with one of these other modes,
  636. or more generally if you would like to move some Org-mode commands to
  637. other keys, set this variable and configure the keys with the variable
  638. `org-disputed-keys'.
  639. This option is only relevant at load-time of Org-mode, and must be set
  640. *before* org.el is loaded. Changing it requires a restart of Emacs to
  641. become effective."
  642. :group 'org-startup
  643. :type 'boolean)
  644. (defcustom org-use-extra-keys nil
  645. "Non-nil means use extra key sequence definitions for certain commands.
  646. This happens automatically if you run XEmacs or if `window-system'
  647. is nil. This variable lets you do the same manually. You must
  648. set it before loading org.
  649. Example: on Carbon Emacs 22 running graphically, with an external
  650. keyboard on a Powerbook, the default way of setting M-left might
  651. not work for either Alt or ESC. Setting this variable will make
  652. it work for ESC."
  653. :group 'org-startup
  654. :type 'boolean)
  655. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  656. (defcustom org-disputed-keys
  657. '(([(shift up)] . [(meta p)])
  658. ([(shift down)] . [(meta n)])
  659. ([(shift left)] . [(meta -)])
  660. ([(shift right)] . [(meta +)])
  661. ([(control shift right)] . [(meta shift +)])
  662. ([(control shift left)] . [(meta shift -)]))
  663. "Keys for which Org-mode and other modes compete.
  664. This is an alist, cars are the default keys, second element specifies
  665. the alternative to use when `org-replace-disputed-keys' is t.
  666. Keys can be specified in any syntax supported by `define-key'.
  667. The value of this option takes effect only at Org-mode's startup,
  668. therefore you'll have to restart Emacs to apply it after changing."
  669. :group 'org-startup
  670. :type 'alist)
  671. (defun org-key (key)
  672. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  673. Or return the original if not disputed.
  674. Also apply the translations defined in `org-xemacs-key-equivalents'."
  675. (when org-replace-disputed-keys
  676. (let* ((nkey (key-description key))
  677. (x (org-find-if (lambda (x)
  678. (equal (key-description (car x)) nkey))
  679. org-disputed-keys)))
  680. (setq key (if x (cdr x) key))))
  681. (when (featurep 'xemacs)
  682. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  683. key)
  684. (defun org-find-if (predicate seq)
  685. (catch 'exit
  686. (while seq
  687. (if (funcall predicate (car seq))
  688. (throw 'exit (car seq))
  689. (pop seq)))))
  690. (defun org-defkey (keymap key def)
  691. "Define a key, possibly translated, as returned by `org-key'."
  692. (define-key keymap (org-key key) def))
  693. (defcustom org-ellipsis nil
  694. "The ellipsis to use in the Org-mode outline.
  695. When nil, just use the standard three dots. When a string, use that instead,
  696. When a face, use the standard 3 dots, but with the specified face.
  697. The change affects only Org-mode (which will then use its own display table).
  698. Changing this requires executing `M-x org-mode' in a buffer to become
  699. effective."
  700. :group 'org-startup
  701. :type '(choice (const :tag "Default" nil)
  702. (face :tag "Face" :value org-warning)
  703. (string :tag "String" :value "...#")))
  704. (defvar org-display-table nil
  705. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  706. (defgroup org-keywords nil
  707. "Keywords in Org-mode."
  708. :tag "Org Keywords"
  709. :group 'org)
  710. (defcustom org-deadline-string "DEADLINE:"
  711. "String to mark deadline entries.
  712. A deadline is this string, followed by a time stamp. Should be a word,
  713. terminated by a colon. You can insert a schedule keyword and
  714. a timestamp with \\[org-deadline].
  715. Changes become only effective after restarting Emacs."
  716. :group 'org-keywords
  717. :type 'string)
  718. (defcustom org-scheduled-string "SCHEDULED:"
  719. "String to mark scheduled TODO entries.
  720. A schedule is this string, followed by a time stamp. Should be a word,
  721. terminated by a colon. You can insert a schedule keyword and
  722. a timestamp with \\[org-schedule].
  723. Changes become only effective after restarting Emacs."
  724. :group 'org-keywords
  725. :type 'string)
  726. (defcustom org-closed-string "CLOSED:"
  727. "String used as the prefix for timestamps logging closing a TODO entry."
  728. :group 'org-keywords
  729. :type 'string)
  730. (defcustom org-clock-string "CLOCK:"
  731. "String used as prefix for timestamps clocking work hours on an item."
  732. :group 'org-keywords
  733. :type 'string)
  734. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  735. org-scheduled-string "\\|"
  736. org-deadline-string "\\|"
  737. org-closed-string "\\|"
  738. org-clock-string "\\)")
  739. "Matches a line with planning or clock info.")
  740. (defcustom org-comment-string "COMMENT"
  741. "Entries starting with this keyword will never be exported.
  742. An entry can be toggled between COMMENT and normal with
  743. \\[org-toggle-comment].
  744. Changes become only effective after restarting Emacs."
  745. :group 'org-keywords
  746. :type 'string)
  747. (defcustom org-quote-string "QUOTE"
  748. "Entries starting with this keyword will be exported in fixed-width font.
  749. Quoting applies only to the text in the entry following the headline, and does
  750. not extend beyond the next headline, even if that is lower level.
  751. An entry can be toggled between QUOTE and normal with
  752. \\[org-toggle-fixed-width-section]."
  753. :group 'org-keywords
  754. :type 'string)
  755. (defconst org-repeat-re
  756. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  757. "Regular expression for specifying repeated events.
  758. After a match, group 1 contains the repeat expression.")
  759. (defgroup org-structure nil
  760. "Options concerning the general structure of Org-mode files."
  761. :tag "Org Structure"
  762. :group 'org)
  763. (defgroup org-reveal-location nil
  764. "Options about how to make context of a location visible."
  765. :tag "Org Reveal Location"
  766. :group 'org-structure)
  767. (defconst org-context-choice
  768. '(choice
  769. (const :tag "Always" t)
  770. (const :tag "Never" nil)
  771. (repeat :greedy t :tag "Individual contexts"
  772. (cons
  773. (choice :tag "Context"
  774. (const agenda)
  775. (const org-goto)
  776. (const occur-tree)
  777. (const tags-tree)
  778. (const link-search)
  779. (const mark-goto)
  780. (const bookmark-jump)
  781. (const isearch)
  782. (const default))
  783. (boolean))))
  784. "Contexts for the reveal options.")
  785. (defcustom org-show-hierarchy-above '((default . t))
  786. "Non-nil means show full hierarchy when revealing a location.
  787. Org-mode often shows locations in an org-mode file which might have
  788. been invisible before. When this is set, the hierarchy of headings
  789. above the exposed location is shown.
  790. Turning this off for example for sparse trees makes them very compact.
  791. Instead of t, this can also be an alist specifying this option for different
  792. contexts. Valid contexts are
  793. agenda when exposing an entry from the agenda
  794. org-goto when using the command `org-goto' on key C-c C-j
  795. occur-tree when using the command `org-occur' on key C-c /
  796. tags-tree when constructing a sparse tree based on tags matches
  797. link-search when exposing search matches associated with a link
  798. mark-goto when exposing the jump goal of a mark
  799. bookmark-jump when exposing a bookmark location
  800. isearch when exiting from an incremental search
  801. default default for all contexts not set explicitly"
  802. :group 'org-reveal-location
  803. :type org-context-choice)
  804. (defcustom org-show-following-heading '((default . nil))
  805. "Non-nil means show following heading when revealing a location.
  806. Org-mode often shows locations in an org-mode file which might have
  807. been invisible before. When this is set, the heading following the
  808. match is shown.
  809. Turning this off for example for sparse trees makes them very compact,
  810. but makes it harder to edit the location of the match. In such a case,
  811. use the command \\[org-reveal] to show more context.
  812. Instead of t, this can also be an alist specifying this option for different
  813. contexts. See `org-show-hierarchy-above' for valid contexts."
  814. :group 'org-reveal-location
  815. :type org-context-choice)
  816. (defcustom org-show-siblings '((default . nil) (isearch t))
  817. "Non-nil means show all sibling heading when revealing a location.
  818. Org-mode often shows locations in an org-mode file which might have
  819. been invisible before. When this is set, the sibling of the current entry
  820. heading are all made visible. If `org-show-hierarchy-above' is t,
  821. the same happens on each level of the hierarchy above the current entry.
  822. By default this is on for the isearch context, off for all other contexts.
  823. Turning this off for example for sparse trees makes them very compact,
  824. but makes it harder to edit the location of the match. In such a case,
  825. use the command \\[org-reveal] to show more context.
  826. Instead of t, this can also be an alist specifying this option for different
  827. contexts. See `org-show-hierarchy-above' for valid contexts."
  828. :group 'org-reveal-location
  829. :type org-context-choice)
  830. (defcustom org-show-entry-below '((default . nil))
  831. "Non-nil means show the entry below a headline when revealing a location.
  832. Org-mode often shows locations in an org-mode file which might have
  833. been invisible before. When this is set, the text below the headline that is
  834. exposed is also shown.
  835. By default this is off for all contexts.
  836. Instead of t, this can also be an alist specifying this option for different
  837. contexts. See `org-show-hierarchy-above' for valid contexts."
  838. :group 'org-reveal-location
  839. :type org-context-choice)
  840. (defcustom org-indirect-buffer-display 'other-window
  841. "How should indirect tree buffers be displayed?
  842. This applies to indirect buffers created with the commands
  843. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  844. Valid values are:
  845. current-window Display in the current window
  846. other-window Just display in another window.
  847. dedicated-frame Create one new frame, and re-use it each time.
  848. new-frame Make a new frame each time. Note that in this case
  849. previously-made indirect buffers are kept, and you need to
  850. kill these buffers yourself."
  851. :group 'org-structure
  852. :group 'org-agenda-windows
  853. :type '(choice
  854. (const :tag "In current window" current-window)
  855. (const :tag "In current frame, other window" other-window)
  856. (const :tag "Each time a new frame" new-frame)
  857. (const :tag "One dedicated frame" dedicated-frame)))
  858. (defcustom org-use-speed-commands nil
  859. "Non-nil means activate single letter commands at beginning of a headline.
  860. This may also be a function to test for appropriate locations where speed
  861. commands should be active."
  862. :group 'org-structure
  863. :type '(choice
  864. (const :tag "Never" nil)
  865. (const :tag "At beginning of headline stars" t)
  866. (function)))
  867. (defcustom org-speed-commands-user nil
  868. "Alist of additional speed commands.
  869. This list will be checked before `org-speed-commands-default'
  870. when the variable `org-use-speed-commands' is non-nil
  871. and when the cursor is at the beginning of a headline.
  872. The car if each entry is a string with a single letter, which must
  873. be assigned to `self-insert-command' in the global map.
  874. The cdr is either a command to be called interactively, a function
  875. to be called, or a form to be evaluated.
  876. An entry that is just a list with a single string will be interpreted
  877. as a descriptive headline that will be added when listing the speed
  878. commands in the Help buffer using the `?' speed command."
  879. :group 'org-structure
  880. :type '(repeat :value ("k" . ignore)
  881. (choice :value ("k" . ignore)
  882. (list :tag "Descriptive Headline" (string :tag "Headline"))
  883. (cons :tag "Letter and Command"
  884. (string :tag "Command letter")
  885. (choice
  886. (function)
  887. (sexp))))))
  888. (defgroup org-cycle nil
  889. "Options concerning visibility cycling in Org-mode."
  890. :tag "Org Cycle"
  891. :group 'org-structure)
  892. (defcustom org-cycle-skip-children-state-if-no-children t
  893. "Non-nil means skip CHILDREN state in entries that don't have any."
  894. :group 'org-cycle
  895. :type 'boolean)
  896. (defcustom org-cycle-max-level nil
  897. "Maximum level which should still be subject to visibility cycling.
  898. Levels higher than this will, for cycling, be treated as text, not a headline.
  899. When `org-odd-levels-only' is set, a value of N in this variable actually
  900. means 2N-1 stars as the limiting headline.
  901. When nil, cycle all levels.
  902. Note that the limiting level of cycling is also influenced by
  903. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  904. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  905. than its value."
  906. :group 'org-cycle
  907. :type '(choice
  908. (const :tag "No limit" nil)
  909. (integer :tag "Maximum level")))
  910. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  911. "Names of drawers. Drawers are not opened by cycling on the headline above.
  912. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  913. this:
  914. :DRAWERNAME:
  915. .....
  916. :END:
  917. The drawer \"PROPERTIES\" is special for capturing properties through
  918. the property API.
  919. Drawers can be defined on the per-file basis with a line like:
  920. #+DRAWERS: HIDDEN STATE PROPERTIES"
  921. :group 'org-structure
  922. :group 'org-cycle
  923. :type '(repeat (string :tag "Drawer Name")))
  924. (defcustom org-hide-block-startup nil
  925. "Non-nil means entering Org-mode will fold all blocks.
  926. This can also be set in on a per-file basis with
  927. #+STARTUP: hideblocks
  928. #+STARTUP: showblocks"
  929. :group 'org-startup
  930. :group 'org-cycle
  931. :type 'boolean)
  932. (defcustom org-cycle-global-at-bob nil
  933. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  934. This makes it possible to do global cycling without having to use S-TAB or
  935. \\[universal-argument] TAB. For this special case to work, the first line
  936. of the buffer must not be a headline -- it may be empty or some other text.
  937. When used in this way, `org-cycle-hook' is disabled temporarily to make
  938. sure the cursor stays at the beginning of the buffer. When this option is
  939. nil, don't do anything special at the beginning of the buffer."
  940. :group 'org-cycle
  941. :type 'boolean)
  942. (defcustom org-cycle-level-after-item/entry-creation t
  943. "Non-nil means cycle entry level or item indentation in new empty entries.
  944. When the cursor is at the end of an empty headline, i.e., with only stars
  945. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  946. and then all possible ancestor states, before returning to the original state.
  947. This makes data entry extremely fast: M-RET to create a new headline,
  948. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  949. When the cursor is at the end of an empty plain list item, one TAB will
  950. make it a subitem, two or more tabs will back up to make this an item
  951. higher up in the item hierarchy."
  952. :group 'org-cycle
  953. :type 'boolean)
  954. (defcustom org-cycle-emulate-tab t
  955. "Where should `org-cycle' emulate TAB.
  956. nil Never
  957. white Only in completely white lines
  958. whitestart Only at the beginning of lines, before the first non-white char
  959. t Everywhere except in headlines
  960. exc-hl-bol Everywhere except at the start of a headline
  961. If TAB is used in a place where it does not emulate TAB, the current subtree
  962. visibility is cycled."
  963. :group 'org-cycle
  964. :type '(choice (const :tag "Never" nil)
  965. (const :tag "Only in completely white lines" white)
  966. (const :tag "Before first char in a line" whitestart)
  967. (const :tag "Everywhere except in headlines" t)
  968. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  969. ))
  970. (defcustom org-cycle-separator-lines 2
  971. "Number of empty lines needed to keep an empty line between collapsed trees.
  972. If you leave an empty line between the end of a subtree and the following
  973. headline, this empty line is hidden when the subtree is folded.
  974. Org-mode will leave (exactly) one empty line visible if the number of
  975. empty lines is equal or larger to the number given in this variable.
  976. So the default 2 means at least 2 empty lines after the end of a subtree
  977. are needed to produce free space between a collapsed subtree and the
  978. following headline.
  979. If the number is negative, and the number of empty lines is at least -N,
  980. all empty lines are shown.
  981. Special case: when 0, never leave empty lines in collapsed view."
  982. :group 'org-cycle
  983. :type 'integer)
  984. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  985. (defcustom org-pre-cycle-hook nil
  986. "Hook that is run before visibility cycling is happening.
  987. The function(s) in this hook must accept a single argument which indicates
  988. the new state that will be set right after running this hook. The
  989. argument is a symbol. Before a global state change, it can have the values
  990. `overview', `content', or `all'. Before a local state change, it can have
  991. the values `folded', `children', or `subtree'."
  992. :group 'org-cycle
  993. :type 'hook)
  994. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  995. org-cycle-hide-drawers
  996. org-cycle-hide-inline-tasks
  997. org-cycle-show-empty-lines
  998. org-optimize-window-after-visibility-change)
  999. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1000. The function(s) in this hook must accept a single argument which indicates
  1001. the new state that was set by the most recent `org-cycle' command. The
  1002. argument is a symbol. After a global state change, it can have the values
  1003. `overview', `contents', or `all'. After a local state change, it can have
  1004. the values `folded', `children', or `subtree'."
  1005. :group 'org-cycle
  1006. :type 'hook)
  1007. (defgroup org-edit-structure nil
  1008. "Options concerning structure editing in Org-mode."
  1009. :tag "Org Edit Structure"
  1010. :group 'org-structure)
  1011. (defcustom org-odd-levels-only nil
  1012. "Non-nil means skip even levels and only use odd levels for the outline.
  1013. This has the effect that two stars are being added/taken away in
  1014. promotion/demotion commands. It also influences how levels are
  1015. handled by the exporters.
  1016. Changing it requires restart of `font-lock-mode' to become effective
  1017. for fontification also in regions already fontified.
  1018. You may also set this on a per-file basis by adding one of the following
  1019. lines to the buffer:
  1020. #+STARTUP: odd
  1021. #+STARTUP: oddeven"
  1022. :group 'org-edit-structure
  1023. :group 'org-appearance
  1024. :type 'boolean)
  1025. (defcustom org-adapt-indentation t
  1026. "Non-nil means adapt indentation to outline node level.
  1027. When this variable is set, Org assumes that you write outlines by
  1028. indenting text in each node to align with the headline (after the stars).
  1029. The following issues are influenced by this variable:
  1030. - When this is set and the *entire* text in an entry is indented, the
  1031. indentation is increased by one space in a demotion command, and
  1032. decreased by one in a promotion command. If any line in the entry
  1033. body starts with text at column 0, indentation is not changed at all.
  1034. - Property drawers and planning information is inserted indented when
  1035. this variable s set. When nil, they will not be indented.
  1036. - TAB indents a line relative to context. The lines below a headline
  1037. will be indented when this variable is set.
  1038. Note that this is all about true indentation, by adding and removing
  1039. space characters. See also `org-indent.el' which does level-dependent
  1040. indentation in a virtual way, i.e. at display time in Emacs."
  1041. :group 'org-edit-structure
  1042. :type 'boolean)
  1043. (defcustom org-special-ctrl-a/e nil
  1044. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1045. When t, `C-a' will bring back the cursor to the beginning of the
  1046. headline text, i.e. after the stars and after a possible TODO
  1047. keyword. In an item, this will be the position after bullet and
  1048. check-box, if any. When the cursor is already at that position,
  1049. another `C-a' will bring it to the beginning of the line.
  1050. `C-e' will jump to the end of the headline, ignoring the presence
  1051. of tags in the headline. A second `C-e' will then jump to the
  1052. true end of the line, after any tags. This also means that, when
  1053. this variable is non-nil, `C-e' also will never jump beyond the
  1054. end of the heading of a folded section, i.e. not after the
  1055. ellipses.
  1056. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1057. normally, going to the true line boundary first. Only a directly
  1058. following, identical keypress will bring the cursor to the
  1059. special positions.
  1060. This may also be a cons cell where the behavior for `C-a' and
  1061. `C-e' is set separately."
  1062. :group 'org-edit-structure
  1063. :type '(choice
  1064. (const :tag "off" nil)
  1065. (const :tag "on: after stars/bullet and before tags first" t)
  1066. (const :tag "reversed: true line boundary first" reversed)
  1067. (cons :tag "Set C-a and C-e separately"
  1068. (choice :tag "Special C-a"
  1069. (const :tag "off" nil)
  1070. (const :tag "on: after stars/bullet first" t)
  1071. (const :tag "reversed: before stars/bullet first" reversed))
  1072. (choice :tag "Special C-e"
  1073. (const :tag "off" nil)
  1074. (const :tag "on: before tags first" t)
  1075. (const :tag "reversed: after tags first" reversed)))))
  1076. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1077. (defcustom org-special-ctrl-k nil
  1078. "Non-nil means `C-k' will behave specially in headlines.
  1079. When nil, `C-k' will call the default `kill-line' command.
  1080. When t, the following will happen while the cursor is in the headline:
  1081. - When the cursor is at the beginning of a headline, kill the entire
  1082. line and possible the folded subtree below the line.
  1083. - When in the middle of the headline text, kill the headline up to the tags.
  1084. - When after the headline text, kill the tags."
  1085. :group 'org-edit-structure
  1086. :type 'boolean)
  1087. (defcustom org-ctrl-k-protect-subtree nil
  1088. "Non-nil means, do not delete a hidden subtree with C-k.
  1089. When set to the symbol `error', simply throw an error when C-k is
  1090. used to kill (part-of) a headline that has hidden text behind it.
  1091. Any other non-nil value will result in a query to the user, if it is
  1092. OK to kill that hidden subtree. When nil, kill without remorse."
  1093. :group 'org-edit-structure
  1094. :version "24.1"
  1095. :type '(choice
  1096. (const :tag "Do not protect hidden subtrees" nil)
  1097. (const :tag "Protect hidden subtrees with a security query" t)
  1098. (const :tag "Never kill a hidden subtree with C-k" error)))
  1099. (defcustom org-catch-invisible-edits nil
  1100. "Check if in invisible region before inserting or deleting a character.
  1101. Valid values are:
  1102. nil Do not check, so just do invisible edits.
  1103. error Throw an error and do nothing.
  1104. show Make point visible, and do the requested edit.
  1105. show-and-error Make point visible, then throw an error and abort the edit.
  1106. smart Make point visible, and do insertion/deletion if it is
  1107. adjacent to visible text and the change feels predictable.
  1108. Never delete a previously invisible character or add in the
  1109. middle or right after an invisible region. Basically, this
  1110. allows insertion and backward-delete right before ellipses.
  1111. FIXME: maybe in this case we should not even show?"
  1112. :group 'org-edit-structure
  1113. :version "24.1"
  1114. :type '(choice
  1115. (const :tag "Do not check" nil)
  1116. (const :tag "Throw error when trying to edit" error)
  1117. (const :tag "Unhide, but do not do the edit" show-and-error)
  1118. (const :tag "Show invisible part and do the edit" show)
  1119. (const :tag "Be smart and do the right thing" smart)))
  1120. (defcustom org-yank-folded-subtrees t
  1121. "Non-nil means when yanking subtrees, fold them.
  1122. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1123. it starts with a heading and all other headings in it are either children
  1124. or siblings, then fold all the subtrees. However, do this only if no
  1125. text after the yank would be swallowed into a folded tree by this action."
  1126. :group 'org-edit-structure
  1127. :type 'boolean)
  1128. (defcustom org-yank-adjusted-subtrees nil
  1129. "Non-nil means when yanking subtrees, adjust the level.
  1130. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1131. this function for details."
  1132. :group 'org-edit-structure
  1133. :type 'boolean)
  1134. (defcustom org-M-RET-may-split-line '((default . t))
  1135. "Non-nil means M-RET will split the line at the cursor position.
  1136. When nil, it will go to the end of the line before making a
  1137. new line.
  1138. You may also set this option in a different way for different
  1139. contexts. Valid contexts are:
  1140. headline when creating a new headline
  1141. item when creating a new item
  1142. table in a table field
  1143. default the value to be used for all contexts not explicitly
  1144. customized"
  1145. :group 'org-structure
  1146. :group 'org-table
  1147. :type '(choice
  1148. (const :tag "Always" t)
  1149. (const :tag "Never" nil)
  1150. (repeat :greedy t :tag "Individual contexts"
  1151. (cons
  1152. (choice :tag "Context"
  1153. (const headline)
  1154. (const item)
  1155. (const table)
  1156. (const default))
  1157. (boolean)))))
  1158. (defcustom org-insert-heading-respect-content nil
  1159. "Non-nil means insert new headings after the current subtree.
  1160. When nil, the new heading is created directly after the current line.
  1161. The commands \\[org-insert-heading-respect-content] and
  1162. \\[org-insert-todo-heading-respect-content] turn this variable on
  1163. for the duration of the command."
  1164. :group 'org-structure
  1165. :type 'boolean)
  1166. (defcustom org-blank-before-new-entry '((heading . auto)
  1167. (plain-list-item . auto))
  1168. "Should `org-insert-heading' leave a blank line before new heading/item?
  1169. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1170. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1171. which case Org will look at the surrounding headings/items and try to
  1172. make an intelligent decision whether to insert a blank line or not.
  1173. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1174. set, the setting here is ignored and no empty line is inserted, to avoid
  1175. breaking the list structure."
  1176. :group 'org-edit-structure
  1177. :type '(list
  1178. (cons (const heading)
  1179. (choice (const :tag "Never" nil)
  1180. (const :tag "Always" t)
  1181. (const :tag "Auto" auto)))
  1182. (cons (const plain-list-item)
  1183. (choice (const :tag "Never" nil)
  1184. (const :tag "Always" t)
  1185. (const :tag "Auto" auto)))))
  1186. (defcustom org-insert-heading-hook nil
  1187. "Hook being run after inserting a new heading."
  1188. :group 'org-edit-structure
  1189. :type 'hook)
  1190. (defcustom org-enable-fixed-width-editor t
  1191. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1192. This currently only means they are never auto-wrapped.
  1193. When nil, such lines will be treated like ordinary lines.
  1194. See also the QUOTE keyword."
  1195. :group 'org-edit-structure
  1196. :type 'boolean)
  1197. (defcustom org-goto-auto-isearch t
  1198. "Non-nil means typing characters in `org-goto' starts incremental search.
  1199. When nil, you can use these keybindings to navigate the buffer:
  1200. q Quit the org-goto interface
  1201. n Go to the next visible heading
  1202. p Go to the previous visible heading
  1203. f Go one heading forward on same level
  1204. b Go one heading backward on same level
  1205. u Go one heading up"
  1206. :group 'org-edit-structure
  1207. :type 'boolean)
  1208. (defgroup org-sparse-trees nil
  1209. "Options concerning sparse trees in Org-mode."
  1210. :tag "Org Sparse Trees"
  1211. :group 'org-structure)
  1212. (defcustom org-highlight-sparse-tree-matches t
  1213. "Non-nil means highlight all matches that define a sparse tree.
  1214. The highlights will automatically disappear the next time the buffer is
  1215. changed by an edit command."
  1216. :group 'org-sparse-trees
  1217. :type 'boolean)
  1218. (defcustom org-remove-highlights-with-change t
  1219. "Non-nil means any change to the buffer will remove temporary highlights.
  1220. Such highlights are created by `org-occur' and `org-clock-display'.
  1221. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1222. The highlights created by `org-preview-latex-fragment' always need
  1223. `C-c C-c' to be removed."
  1224. :group 'org-sparse-trees
  1225. :group 'org-time
  1226. :type 'boolean)
  1227. (defcustom org-occur-hook '(org-first-headline-recenter)
  1228. "Hook that is run after `org-occur' has constructed a sparse tree.
  1229. This can be used to recenter the window to show as much of the structure
  1230. as possible."
  1231. :group 'org-sparse-trees
  1232. :type 'hook)
  1233. (defgroup org-imenu-and-speedbar nil
  1234. "Options concerning imenu and speedbar in Org-mode."
  1235. :tag "Org Imenu and Speedbar"
  1236. :group 'org-structure)
  1237. (defcustom org-imenu-depth 2
  1238. "The maximum level for Imenu access to Org-mode headlines.
  1239. This also applied for speedbar access."
  1240. :group 'org-imenu-and-speedbar
  1241. :type 'integer)
  1242. (defgroup org-table nil
  1243. "Options concerning tables in Org-mode."
  1244. :tag "Org Table"
  1245. :group 'org)
  1246. (defcustom org-enable-table-editor 'optimized
  1247. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1248. When nil, such lines will be treated like ordinary lines.
  1249. When equal to the symbol `optimized', the table editor will be optimized to
  1250. do the following:
  1251. - Automatic overwrite mode in front of whitespace in table fields.
  1252. This makes the structure of the table stay in tact as long as the edited
  1253. field does not exceed the column width.
  1254. - Minimize the number of realigns. Normally, the table is aligned each time
  1255. TAB or RET are pressed to move to another field. With optimization this
  1256. happens only if changes to a field might have changed the column width.
  1257. Optimization requires replacing the functions `self-insert-command',
  1258. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1259. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1260. very good at guessing when a re-align will be necessary, but you can always
  1261. force one with \\[org-ctrl-c-ctrl-c].
  1262. If you would like to use the optimized version in Org-mode, but the
  1263. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1264. This variable can be used to turn on and off the table editor during a session,
  1265. but in order to toggle optimization, a restart is required.
  1266. See also the variable `org-table-auto-blank-field'."
  1267. :group 'org-table
  1268. :type '(choice
  1269. (const :tag "off" nil)
  1270. (const :tag "on" t)
  1271. (const :tag "on, optimized" optimized)))
  1272. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1273. (version<= emacs-version "24.1"))
  1274. "Non-nil means cluster self-insert commands for undo when possible.
  1275. If this is set, then, like in the Emacs command loop, 20 consecutive
  1276. characters will be undone together.
  1277. This is configurable, because there is some impact on typing performance."
  1278. :group 'org-table
  1279. :type 'boolean)
  1280. (defcustom org-table-tab-recognizes-table.el t
  1281. "Non-nil means TAB will automatically notice a table.el table.
  1282. When it sees such a table, it moves point into it and - if necessary -
  1283. calls `table-recognize-table'."
  1284. :group 'org-table-editing
  1285. :type 'boolean)
  1286. (defgroup org-link nil
  1287. "Options concerning links in Org-mode."
  1288. :tag "Org Link"
  1289. :group 'org)
  1290. (defvar org-link-abbrev-alist-local nil
  1291. "Buffer-local version of `org-link-abbrev-alist', which see.
  1292. The value of this is taken from the #+LINK lines.")
  1293. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1294. (defcustom org-link-abbrev-alist nil
  1295. "Alist of link abbreviations.
  1296. The car of each element is a string, to be replaced at the start of a link.
  1297. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1298. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1299. [[linkkey:tag][description]]
  1300. The 'linkkey' must be a word word, starting with a letter, followed
  1301. by letters, numbers, '-' or '_'.
  1302. If REPLACE is a string, the tag will simply be appended to create the link.
  1303. If the string contains \"%s\", the tag will be inserted there. If the string
  1304. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1305. at that point (see the function `url-hexify-string'). If the string contains
  1306. the specifier \"%(my-function)\", then the custom function `my-function' will
  1307. be invoked: this function takes the tag as its only argument and must return
  1308. a string.
  1309. REPLACE may also be a function that will be called with the tag as the
  1310. only argument to create the link, which should be returned as a string.
  1311. See the manual for examples."
  1312. :group 'org-link
  1313. :type '(repeat
  1314. (cons
  1315. (string :tag "Protocol")
  1316. (choice
  1317. (string :tag "Format")
  1318. (function)))))
  1319. (defcustom org-descriptive-links t
  1320. "Non-nil means Org will display descriptive links.
  1321. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1322. \"Org Website\", hiding the link itself and just displaying its
  1323. description. When set to `nil', Org will display the full links
  1324. literally.
  1325. You can interactively set the value of this variable by calling
  1326. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1327. :group 'org-link
  1328. :type 'boolean)
  1329. (defcustom org-link-file-path-type 'adaptive
  1330. "How the path name in file links should be stored.
  1331. Valid values are:
  1332. relative Relative to the current directory, i.e. the directory of the file
  1333. into which the link is being inserted.
  1334. absolute Absolute path, if possible with ~ for home directory.
  1335. noabbrev Absolute path, no abbreviation of home directory.
  1336. adaptive Use relative path for files in the current directory and sub-
  1337. directories of it. For other files, use an absolute path."
  1338. :group 'org-link
  1339. :type '(choice
  1340. (const relative)
  1341. (const absolute)
  1342. (const noabbrev)
  1343. (const adaptive)))
  1344. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1345. "Types of links that should be activated in Org-mode files.
  1346. This is a list of symbols, each leading to the activation of a certain link
  1347. type. In principle, it does not hurt to turn on most link types - there may
  1348. be a small gain when turning off unused link types. The types are:
  1349. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1350. angle Links in angular brackets that may contain whitespace like
  1351. <bbdb:Carsten Dominik>.
  1352. plain Plain links in normal text, no whitespace, like http://google.com.
  1353. radio Text that is matched by a radio target, see manual for details.
  1354. tag Tag settings in a headline (link to tag search).
  1355. date Time stamps (link to calendar).
  1356. footnote Footnote labels.
  1357. Changing this variable requires a restart of Emacs to become effective."
  1358. :group 'org-link
  1359. :type '(set :greedy t
  1360. (const :tag "Double bracket links" bracket)
  1361. (const :tag "Angular bracket links" angle)
  1362. (const :tag "Plain text links" plain)
  1363. (const :tag "Radio target matches" radio)
  1364. (const :tag "Tags" tag)
  1365. (const :tag "Timestamps" date)
  1366. (const :tag "Footnotes" footnote)))
  1367. (defcustom org-make-link-description-function nil
  1368. "Function to use for generating link descriptions from links.
  1369. When nil, the link location will be used. This function must take
  1370. two parameters: the first one is the link, the second one is the
  1371. description generated by `org-insert-link'. The function should
  1372. return the description to use."
  1373. :group 'org-link
  1374. :type 'function)
  1375. (defgroup org-link-store nil
  1376. "Options concerning storing links in Org-mode."
  1377. :tag "Org Store Link"
  1378. :group 'org-link)
  1379. (defcustom org-url-hexify-p t
  1380. "When non-nil, hexify URL when creating a link."
  1381. :type 'boolean
  1382. :version "24.3"
  1383. :group 'org-link-store)
  1384. (defcustom org-email-link-description-format "Email %c: %.30s"
  1385. "Format of the description part of a link to an email or usenet message.
  1386. The following %-escapes will be replaced by corresponding information:
  1387. %F full \"From\" field
  1388. %f name, taken from \"From\" field, address if no name
  1389. %T full \"To\" field
  1390. %t first name in \"To\" field, address if no name
  1391. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1392. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1393. %s subject
  1394. %d date
  1395. %m message-id.
  1396. You may use normal field width specification between the % and the letter.
  1397. This is for example useful to limit the length of the subject.
  1398. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1399. :group 'org-link-store
  1400. :type 'string)
  1401. (defcustom org-from-is-user-regexp
  1402. (let (r1 r2)
  1403. (when (and user-mail-address (not (string= user-mail-address "")))
  1404. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1405. (when (and user-full-name (not (string= user-full-name "")))
  1406. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1407. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1408. "Regexp matched against the \"From:\" header of an email or usenet message.
  1409. It should match if the message is from the user him/herself."
  1410. :group 'org-link-store
  1411. :type 'regexp)
  1412. (defcustom org-context-in-file-links t
  1413. "Non-nil means file links from `org-store-link' contain context.
  1414. A search string will be added to the file name with :: as separator and
  1415. used to find the context when the link is activated by the command
  1416. `org-open-at-point'. When this option is t, the entire active region
  1417. will be placed in the search string of the file link. If set to a
  1418. positive integer, only the first n lines of context will be stored.
  1419. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1420. negates this setting for the duration of the command."
  1421. :group 'org-link-store
  1422. :type '(choice boolean integer))
  1423. (defcustom org-keep-stored-link-after-insertion nil
  1424. "Non-nil means keep link in list for entire session.
  1425. The command `org-store-link' adds a link pointing to the current
  1426. location to an internal list. These links accumulate during a session.
  1427. The command `org-insert-link' can be used to insert links into any
  1428. Org-mode file (offering completion for all stored links). When this
  1429. option is nil, every link which has been inserted once using \\[org-insert-link]
  1430. will be removed from the list, to make completing the unused links
  1431. more efficient."
  1432. :group 'org-link-store
  1433. :type 'boolean)
  1434. (defgroup org-link-follow nil
  1435. "Options concerning following links in Org-mode."
  1436. :tag "Org Follow Link"
  1437. :group 'org-link)
  1438. (defcustom org-link-translation-function nil
  1439. "Function to translate links with different syntax to Org syntax.
  1440. This can be used to translate links created for example by the Planner
  1441. or emacs-wiki packages to Org syntax.
  1442. The function must accept two parameters, a TYPE containing the link
  1443. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1444. which is everything after the link protocol. It should return a cons
  1445. with possibly modified values of type and path.
  1446. Org contains a function for this, so if you set this variable to
  1447. `org-translate-link-from-planner', you should be able follow many
  1448. links created by planner."
  1449. :group 'org-link-follow
  1450. :type 'function)
  1451. (defcustom org-follow-link-hook nil
  1452. "Hook that is run after a link has been followed."
  1453. :group 'org-link-follow
  1454. :type 'hook)
  1455. (defcustom org-tab-follows-link nil
  1456. "Non-nil means on links TAB will follow the link.
  1457. Needs to be set before org.el is loaded.
  1458. This really should not be used, it does not make sense, and the
  1459. implementation is bad."
  1460. :group 'org-link-follow
  1461. :type 'boolean)
  1462. (defcustom org-return-follows-link nil
  1463. "Non-nil means on links RET will follow the link.
  1464. In tables, the special behavior of RET has precedence."
  1465. :group 'org-link-follow
  1466. :type 'boolean)
  1467. (defcustom org-mouse-1-follows-link
  1468. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1469. "Non-nil means mouse-1 on a link will follow the link.
  1470. A longer mouse click will still set point. Does not work on XEmacs.
  1471. Needs to be set before org.el is loaded."
  1472. :group 'org-link-follow
  1473. :type 'boolean)
  1474. (defcustom org-mark-ring-length 4
  1475. "Number of different positions to be recorded in the ring.
  1476. Changing this requires a restart of Emacs to work correctly."
  1477. :group 'org-link-follow
  1478. :type 'integer)
  1479. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1480. "Non-nil means internal links in Org files must exactly match a headline.
  1481. When nil, the link search tries to match a phrase with all words
  1482. in the search text."
  1483. :group 'org-link-follow
  1484. :version "24.1"
  1485. :type '(choice
  1486. (const :tag "Use fuzzy text search" nil)
  1487. (const :tag "Match only exact headline" t)
  1488. (const :tag "Match exact headline or query to create it"
  1489. query-to-create)))
  1490. (defcustom org-link-frame-setup
  1491. '((vm . vm-visit-folder-other-frame)
  1492. (vm-imap . vm-visit-imap-folder-other-frame)
  1493. (gnus . org-gnus-no-new-news)
  1494. (file . find-file-other-window)
  1495. (wl . wl-other-frame))
  1496. "Setup the frame configuration for following links.
  1497. When following a link with Emacs, it may often be useful to display
  1498. this link in another window or frame. This variable can be used to
  1499. set this up for the different types of links.
  1500. For VM, use any of
  1501. `vm-visit-folder'
  1502. `vm-visit-folder-other-window'
  1503. `vm-visit-folder-other-frame'
  1504. For Gnus, use any of
  1505. `gnus'
  1506. `gnus-other-frame'
  1507. `org-gnus-no-new-news'
  1508. For FILE, use any of
  1509. `find-file'
  1510. `find-file-other-window'
  1511. `find-file-other-frame'
  1512. For Wanderlust use any of
  1513. `wl'
  1514. `wl-other-frame'
  1515. For the calendar, use the variable `calendar-setup'.
  1516. For BBDB, it is currently only possible to display the matches in
  1517. another window."
  1518. :group 'org-link-follow
  1519. :type '(list
  1520. (cons (const vm)
  1521. (choice
  1522. (const vm-visit-folder)
  1523. (const vm-visit-folder-other-window)
  1524. (const vm-visit-folder-other-frame)))
  1525. (cons (const gnus)
  1526. (choice
  1527. (const gnus)
  1528. (const gnus-other-frame)
  1529. (const org-gnus-no-new-news)))
  1530. (cons (const file)
  1531. (choice
  1532. (const find-file)
  1533. (const find-file-other-window)
  1534. (const find-file-other-frame)))
  1535. (cons (const wl)
  1536. (choice
  1537. (const wl)
  1538. (const wl-other-frame)))))
  1539. (defcustom org-display-internal-link-with-indirect-buffer nil
  1540. "Non-nil means use indirect buffer to display infile links.
  1541. Activating internal links (from one location in a file to another location
  1542. in the same file) normally just jumps to the location. When the link is
  1543. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1544. is displayed in
  1545. another window. When this option is set, the other window actually displays
  1546. an indirect buffer clone of the current buffer, to avoid any visibility
  1547. changes to the current buffer."
  1548. :group 'org-link-follow
  1549. :type 'boolean)
  1550. (defcustom org-open-non-existing-files nil
  1551. "Non-nil means `org-open-file' will open non-existing files.
  1552. When nil, an error will be generated.
  1553. This variable applies only to external applications because they
  1554. might choke on non-existing files. If the link is to a file that
  1555. will be opened in Emacs, the variable is ignored."
  1556. :group 'org-link-follow
  1557. :type 'boolean)
  1558. (defcustom org-open-directory-means-index-dot-org nil
  1559. "Non-nil means a link to a directory really means to index.org.
  1560. When nil, following a directory link will run dired or open a finder/explorer
  1561. window on that directory."
  1562. :group 'org-link-follow
  1563. :type 'boolean)
  1564. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1565. "Function and arguments to call for following mailto links.
  1566. This is a list with the first element being a Lisp function, and the
  1567. remaining elements being arguments to the function. In string arguments,
  1568. %a will be replaced by the address, and %s will be replaced by the subject
  1569. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1570. :group 'org-link-follow
  1571. :type '(choice
  1572. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1573. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1574. (const :tag "message-mail" (message-mail "%a" "%s"))
  1575. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1576. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1577. "Non-nil means ask for confirmation before executing shell links.
  1578. Shell links can be dangerous: just think about a link
  1579. [[shell:rm -rf ~/*][Google Search]]
  1580. This link would show up in your Org-mode document as \"Google Search\",
  1581. but really it would remove your entire home directory.
  1582. Therefore we advise against setting this variable to nil.
  1583. Just change it to `y-or-n-p' if you want to confirm with a
  1584. single keystroke rather than having to type \"yes\"."
  1585. :group 'org-link-follow
  1586. :type '(choice
  1587. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1588. (const :tag "with y-or-n (faster)" y-or-n-p)
  1589. (const :tag "no confirmation (dangerous)" nil)))
  1590. (put 'org-confirm-shell-link-function
  1591. 'safe-local-variable
  1592. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1593. (defcustom org-confirm-shell-link-not-regexp ""
  1594. "A regexp to skip confirmation for shell links."
  1595. :group 'org-link-follow
  1596. :version "24.1"
  1597. :type 'regexp)
  1598. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1599. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1600. Elisp links can be dangerous: just think about a link
  1601. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1602. This link would show up in your Org-mode document as \"Google Search\",
  1603. but really it would remove your entire home directory.
  1604. Therefore we advise against setting this variable to nil.
  1605. Just change it to `y-or-n-p' if you want to confirm with a
  1606. single keystroke rather than having to type \"yes\"."
  1607. :group 'org-link-follow
  1608. :type '(choice
  1609. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1610. (const :tag "with y-or-n (faster)" y-or-n-p)
  1611. (const :tag "no confirmation (dangerous)" nil)))
  1612. (put 'org-confirm-shell-link-function
  1613. 'safe-local-variable
  1614. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1615. (defcustom org-confirm-elisp-link-not-regexp ""
  1616. "A regexp to skip confirmation for Elisp links."
  1617. :group 'org-link-follow
  1618. :version "24.1"
  1619. :type 'regexp)
  1620. (defconst org-file-apps-defaults-gnu
  1621. '((remote . emacs)
  1622. (system . mailcap)
  1623. (t . mailcap))
  1624. "Default file applications on a UNIX or GNU/Linux system.
  1625. See `org-file-apps'.")
  1626. (defconst org-file-apps-defaults-macosx
  1627. '((remote . emacs)
  1628. (t . "open %s")
  1629. (system . "open %s")
  1630. ("ps.gz" . "gv %s")
  1631. ("eps.gz" . "gv %s")
  1632. ("dvi" . "xdvi %s")
  1633. ("fig" . "xfig %s"))
  1634. "Default file applications on a MacOS X system.
  1635. The system \"open\" is known as a default, but we use X11 applications
  1636. for some files for which the OS does not have a good default.
  1637. See `org-file-apps'.")
  1638. (defconst org-file-apps-defaults-windowsnt
  1639. (list
  1640. '(remote . emacs)
  1641. (cons t
  1642. (list (if (featurep 'xemacs)
  1643. 'mswindows-shell-execute
  1644. 'w32-shell-execute)
  1645. "open" 'file))
  1646. (cons 'system
  1647. (list (if (featurep 'xemacs)
  1648. 'mswindows-shell-execute
  1649. 'w32-shell-execute)
  1650. "open" 'file)))
  1651. "Default file applications on a Windows NT system.
  1652. The system \"open\" is used for most files.
  1653. See `org-file-apps'.")
  1654. (defcustom org-file-apps
  1655. '(
  1656. (auto-mode . emacs)
  1657. ("\\.mm\\'" . default)
  1658. ("\\.x?html?\\'" . default)
  1659. ("\\.pdf\\'" . default)
  1660. )
  1661. "External applications for opening `file:path' items in a document.
  1662. Org-mode uses system defaults for different file types, but
  1663. you can use this variable to set the application for a given file
  1664. extension. The entries in this list are cons cells where the car identifies
  1665. files and the cdr the corresponding command. Possible values for the
  1666. file identifier are
  1667. \"string\" A string as a file identifier can be interpreted in different
  1668. ways, depending on its contents:
  1669. - Alphanumeric characters only:
  1670. Match links with this file extension.
  1671. Example: (\"pdf\" . \"evince %s\")
  1672. to open PDFs with evince.
  1673. - Regular expression: Match links where the
  1674. filename matches the regexp. If you want to
  1675. use groups here, use shy groups.
  1676. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1677. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1678. to open *.html and *.xhtml with firefox.
  1679. - Regular expression which contains (non-shy) groups:
  1680. Match links where the whole link, including \"::\", and
  1681. anything after that, matches the regexp.
  1682. In a custom command string, %1, %2, etc. are replaced with
  1683. the parts of the link that were matched by the groups.
  1684. For backwards compatibility, if a command string is given
  1685. that does not use any of the group matches, this case is
  1686. handled identically to the second one (i.e. match against
  1687. file name only).
  1688. In a custom lisp form, you can access the group matches with
  1689. (match-string n link).
  1690. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1691. to open [[file:document.pdf::5]] with evince at page 5.
  1692. `directory' Matches a directory
  1693. `remote' Matches a remote file, accessible through tramp or efs.
  1694. Remote files most likely should be visited through Emacs
  1695. because external applications cannot handle such paths.
  1696. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1697. so all files Emacs knows how to handle. Using this with
  1698. command `emacs' will open most files in Emacs. Beware that this
  1699. will also open html files inside Emacs, unless you add
  1700. (\"html\" . default) to the list as well.
  1701. t Default for files not matched by any of the other options.
  1702. `system' The system command to open files, like `open' on Windows
  1703. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1704. that will be selected if you call `C-c C-o' with a double
  1705. \\[universal-argument] \\[universal-argument] prefix.
  1706. Possible values for the command are:
  1707. `emacs' The file will be visited by the current Emacs process.
  1708. `default' Use the default application for this file type, which is the
  1709. association for t in the list, most likely in the system-specific
  1710. part.
  1711. This can be used to overrule an unwanted setting in the
  1712. system-specific variable.
  1713. `system' Use the system command for opening files, like \"open\".
  1714. This command is specified by the entry whose car is `system'.
  1715. Most likely, the system-specific version of this variable
  1716. does define this command, but you can overrule/replace it
  1717. here.
  1718. string A command to be executed by a shell; %s will be replaced
  1719. by the path to the file.
  1720. sexp A Lisp form which will be evaluated. The file path will
  1721. be available in the Lisp variable `file'.
  1722. For more examples, see the system specific constants
  1723. `org-file-apps-defaults-macosx'
  1724. `org-file-apps-defaults-windowsnt'
  1725. `org-file-apps-defaults-gnu'."
  1726. :group 'org-link-follow
  1727. :type '(repeat
  1728. (cons (choice :value ""
  1729. (string :tag "Extension")
  1730. (const :tag "System command to open files" system)
  1731. (const :tag "Default for unrecognized files" t)
  1732. (const :tag "Remote file" remote)
  1733. (const :tag "Links to a directory" directory)
  1734. (const :tag "Any files that have Emacs modes"
  1735. auto-mode))
  1736. (choice :value ""
  1737. (const :tag "Visit with Emacs" emacs)
  1738. (const :tag "Use default" default)
  1739. (const :tag "Use the system command" system)
  1740. (string :tag "Command")
  1741. (sexp :tag "Lisp form")))))
  1742. (defcustom org-doi-server-url "http://dx.doi.org/"
  1743. "The URL of the DOI server."
  1744. :type 'string
  1745. :version "24.3"
  1746. :group 'org-link-follow)
  1747. (defgroup org-refile nil
  1748. "Options concerning refiling entries in Org-mode."
  1749. :tag "Org Refile"
  1750. :group 'org)
  1751. (defcustom org-directory "~/org"
  1752. "Directory with org files.
  1753. This is just a default location to look for Org files. There is no need
  1754. at all to put your files into this directory. It is only used in the
  1755. following situations:
  1756. 1. When a capture template specifies a target file that is not an
  1757. absolute path. The path will then be interpreted relative to
  1758. `org-directory'
  1759. 2. When a capture note is filed away in an interactive way (when exiting the
  1760. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1761. with `org-directory' as the default path."
  1762. :group 'org-refile
  1763. :group 'org-remember
  1764. :group 'org-capture
  1765. :type 'directory)
  1766. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1767. "Default target for storing notes.
  1768. Used as a fall back file for org-remember.el and org-capture.el, for
  1769. templates that do not specify a target file."
  1770. :group 'org-refile
  1771. :group 'org-remember
  1772. :group 'org-capture
  1773. :type '(choice
  1774. (const :tag "Default from remember-data-file" nil)
  1775. file))
  1776. (defcustom org-goto-interface 'outline
  1777. "The default interface to be used for `org-goto'.
  1778. Allowed values are:
  1779. outline The interface shows an outline of the relevant file
  1780. and the correct heading is found by moving through
  1781. the outline or by searching with incremental search.
  1782. outline-path-completion Headlines in the current buffer are offered via
  1783. completion. This is the interface also used by
  1784. the refile command."
  1785. :group 'org-refile
  1786. :type '(choice
  1787. (const :tag "Outline" outline)
  1788. (const :tag "Outline-path-completion" outline-path-completion)))
  1789. (defcustom org-goto-max-level 5
  1790. "Maximum target level when running `org-goto' with refile interface."
  1791. :group 'org-refile
  1792. :type 'integer)
  1793. (defcustom org-reverse-note-order nil
  1794. "Non-nil means store new notes at the beginning of a file or entry.
  1795. When nil, new notes will be filed to the end of a file or entry.
  1796. This can also be a list with cons cells of regular expressions that
  1797. are matched against file names, and values."
  1798. :group 'org-remember
  1799. :group 'org-capture
  1800. :group 'org-refile
  1801. :type '(choice
  1802. (const :tag "Reverse always" t)
  1803. (const :tag "Reverse never" nil)
  1804. (repeat :tag "By file name regexp"
  1805. (cons regexp boolean))))
  1806. (defcustom org-log-refile nil
  1807. "Information to record when a task is refiled.
  1808. Possible values are:
  1809. nil Don't add anything
  1810. time Add a time stamp to the task
  1811. note Prompt for a note and add it with template `org-log-note-headings'
  1812. This option can also be set with on a per-file-basis with
  1813. #+STARTUP: nologrefile
  1814. #+STARTUP: logrefile
  1815. #+STARTUP: lognoterefile
  1816. You can have local logging settings for a subtree by setting the LOGGING
  1817. property to one or more of these keywords.
  1818. When bulk-refiling from the agenda, the value `note' is forbidden and
  1819. will temporarily be changed to `time'."
  1820. :group 'org-refile
  1821. :group 'org-progress
  1822. :version "24.1"
  1823. :type '(choice
  1824. (const :tag "No logging" nil)
  1825. (const :tag "Record timestamp" time)
  1826. (const :tag "Record timestamp with note." note)))
  1827. (defcustom org-refile-targets nil
  1828. "Targets for refiling entries with \\[org-refile].
  1829. This is a list of cons cells. Each cell contains:
  1830. - a specification of the files to be considered, either a list of files,
  1831. or a symbol whose function or variable value will be used to retrieve
  1832. a file name or a list of file names. If you use `org-agenda-files' for
  1833. that, all agenda files will be scanned for targets. Nil means consider
  1834. headings in the current buffer.
  1835. - A specification of how to find candidate refile targets. This may be
  1836. any of:
  1837. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1838. This tag has to be present in all target headlines, inheritance will
  1839. not be considered.
  1840. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1841. todo keyword.
  1842. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1843. headlines that are refiling targets.
  1844. - a cons cell (:level . N). Any headline of level N is considered a target.
  1845. Note that, when `org-odd-levels-only' is set, level corresponds to
  1846. order in hierarchy, not to the number of stars.
  1847. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1848. Note that, when `org-odd-levels-only' is set, level corresponds to
  1849. order in hierarchy, not to the number of stars.
  1850. Each element of this list generates a set of possible targets.
  1851. The union of these sets is presented (with completion) to
  1852. the user by `org-refile'.
  1853. You can set the variable `org-refile-target-verify-function' to a function
  1854. to verify each headline found by the simple criteria above.
  1855. When this variable is nil, all top-level headlines in the current buffer
  1856. are used, equivalent to the value `((nil . (:level . 1))'."
  1857. :group 'org-refile
  1858. :type '(repeat
  1859. (cons
  1860. (choice :value org-agenda-files
  1861. (const :tag "All agenda files" org-agenda-files)
  1862. (const :tag "Current buffer" nil)
  1863. (function) (variable) (file))
  1864. (choice :tag "Identify target headline by"
  1865. (cons :tag "Specific tag" (const :value :tag) (string))
  1866. (cons :tag "TODO keyword" (const :value :todo) (string))
  1867. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1868. (cons :tag "Level number" (const :value :level) (integer))
  1869. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1870. (defcustom org-refile-target-verify-function nil
  1871. "Function to verify if the headline at point should be a refile target.
  1872. The function will be called without arguments, with point at the
  1873. beginning of the headline. It should return t and leave point
  1874. where it is if the headline is a valid target for refiling.
  1875. If the target should not be selected, the function must return nil.
  1876. In addition to this, it may move point to a place from where the search
  1877. should be continued. For example, the function may decide that the entire
  1878. subtree of the current entry should be excluded and move point to the end
  1879. of the subtree."
  1880. :group 'org-refile
  1881. :type 'function)
  1882. (defcustom org-refile-use-cache nil
  1883. "Non-nil means cache refile targets to speed up the process.
  1884. The cache for a particular file will be updated automatically when
  1885. the buffer has been killed, or when any of the marker used for flagging
  1886. refile targets no longer points at a live buffer.
  1887. If you have added new entries to a buffer that might themselves be targets,
  1888. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1889. find that easier, `C-u C-u C-u C-c C-w'."
  1890. :group 'org-refile
  1891. :version "24.1"
  1892. :type 'boolean)
  1893. (defcustom org-refile-use-outline-path nil
  1894. "Non-nil means provide refile targets as paths.
  1895. So a level 3 headline will be available as level1/level2/level3.
  1896. When the value is `file', also include the file name (without directory)
  1897. into the path. In this case, you can also stop the completion after
  1898. the file name, to get entries inserted as top level in the file.
  1899. When `full-file-path', include the full file path."
  1900. :group 'org-refile
  1901. :type '(choice
  1902. (const :tag "Not" nil)
  1903. (const :tag "Yes" t)
  1904. (const :tag "Start with file name" file)
  1905. (const :tag "Start with full file path" full-file-path)))
  1906. (defcustom org-outline-path-complete-in-steps t
  1907. "Non-nil means complete the outline path in hierarchical steps.
  1908. When Org-mode uses the refile interface to select an outline path
  1909. \(see variable `org-refile-use-outline-path'), the completion of
  1910. the path can be done is a single go, or if can be done in steps down
  1911. the headline hierarchy. Going in steps is probably the best if you
  1912. do not use a special completion package like `ido' or `icicles'.
  1913. However, when using these packages, going in one step can be very
  1914. fast, while still showing the whole path to the entry."
  1915. :group 'org-refile
  1916. :type 'boolean)
  1917. (defcustom org-refile-allow-creating-parent-nodes nil
  1918. "Non-nil means allow to create new nodes as refile targets.
  1919. New nodes are then created by adding \"/new node name\" to the completion
  1920. of an existing node. When the value of this variable is `confirm',
  1921. new node creation must be confirmed by the user (recommended)
  1922. When nil, the completion must match an existing entry.
  1923. Note that, if the new heading is not seen by the criteria
  1924. listed in `org-refile-targets', multiple instances of the same
  1925. heading would be created by trying again to file under the new
  1926. heading."
  1927. :group 'org-refile
  1928. :type '(choice
  1929. (const :tag "Never" nil)
  1930. (const :tag "Always" t)
  1931. (const :tag "Prompt for confirmation" confirm)))
  1932. (defcustom org-refile-active-region-within-subtree nil
  1933. "Non-nil means also refile active region within a subtree.
  1934. By default `org-refile' doesn't allow refiling regions if they
  1935. don't contain a set of subtrees, but it might be convenient to
  1936. do so sometimes: in that case, the first line of the region is
  1937. converted to a headline before refiling."
  1938. :group 'org-refile
  1939. :version "24.1"
  1940. :type 'boolean)
  1941. (defgroup org-todo nil
  1942. "Options concerning TODO items in Org-mode."
  1943. :tag "Org TODO"
  1944. :group 'org)
  1945. (defgroup org-progress nil
  1946. "Options concerning Progress logging in Org-mode."
  1947. :tag "Org Progress"
  1948. :group 'org-time)
  1949. (defvar org-todo-interpretation-widgets
  1950. '((:tag "Sequence (cycling hits every state)" sequence)
  1951. (:tag "Type (cycling directly to DONE)" type))
  1952. "The available interpretation symbols for customizing `org-todo-keywords'.
  1953. Interested libraries should add to this list.")
  1954. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1955. "List of TODO entry keyword sequences and their interpretation.
  1956. \\<org-mode-map>This is a list of sequences.
  1957. Each sequence starts with a symbol, either `sequence' or `type',
  1958. indicating if the keywords should be interpreted as a sequence of
  1959. action steps, or as different types of TODO items. The first
  1960. keywords are states requiring action - these states will select a headline
  1961. for inclusion into the global TODO list Org-mode produces. If one of
  1962. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1963. signify that no further action is necessary. If \"|\" is not found,
  1964. the last keyword is treated as the only DONE state of the sequence.
  1965. The command \\[org-todo] cycles an entry through these states, and one
  1966. additional state where no keyword is present. For details about this
  1967. cycling, see the manual.
  1968. TODO keywords and interpretation can also be set on a per-file basis with
  1969. the special #+SEQ_TODO and #+TYP_TODO lines.
  1970. Each keyword can optionally specify a character for fast state selection
  1971. \(in combination with the variable `org-use-fast-todo-selection')
  1972. and specifiers for state change logging, using the same syntax that
  1973. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1974. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1975. indicates to record a time stamp each time this state is selected.
  1976. Each keyword may also specify if a timestamp or a note should be
  1977. recorded when entering or leaving the state, by adding additional
  1978. characters in the parenthesis after the keyword. This looks like this:
  1979. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1980. record only the time of the state change. With X and Y being either
  1981. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1982. Y when leaving the state if and only if the *target* state does not
  1983. define X. You may omit any of the fast-selection key or X or /Y,
  1984. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1985. For backward compatibility, this variable may also be just a list
  1986. of keywords. In this case the interpretation (sequence or type) will be
  1987. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1988. :group 'org-todo
  1989. :group 'org-keywords
  1990. :type '(choice
  1991. (repeat :tag "Old syntax, just keywords"
  1992. (string :tag "Keyword"))
  1993. (repeat :tag "New syntax"
  1994. (cons
  1995. (choice
  1996. :tag "Interpretation"
  1997. ;;Quick and dirty way to see
  1998. ;;`org-todo-interpretations'. This takes the
  1999. ;;place of item arguments
  2000. :convert-widget
  2001. (lambda (widget)
  2002. (widget-put widget
  2003. :args (mapcar
  2004. #'(lambda (x)
  2005. (widget-convert
  2006. (cons 'const x)))
  2007. org-todo-interpretation-widgets))
  2008. widget))
  2009. (repeat
  2010. (string :tag "Keyword"))))))
  2011. (defvar org-todo-keywords-1 nil
  2012. "All TODO and DONE keywords active in a buffer.")
  2013. (make-variable-buffer-local 'org-todo-keywords-1)
  2014. (defvar org-todo-keywords-for-agenda nil)
  2015. (defvar org-done-keywords-for-agenda nil)
  2016. (defvar org-drawers-for-agenda nil)
  2017. (defvar org-todo-keyword-alist-for-agenda nil)
  2018. (defvar org-tag-alist-for-agenda nil)
  2019. (defvar org-agenda-contributing-files nil)
  2020. (defvar org-not-done-keywords nil)
  2021. (make-variable-buffer-local 'org-not-done-keywords)
  2022. (defvar org-done-keywords nil)
  2023. (make-variable-buffer-local 'org-done-keywords)
  2024. (defvar org-todo-heads nil)
  2025. (make-variable-buffer-local 'org-todo-heads)
  2026. (defvar org-todo-sets nil)
  2027. (make-variable-buffer-local 'org-todo-sets)
  2028. (defvar org-todo-log-states nil)
  2029. (make-variable-buffer-local 'org-todo-log-states)
  2030. (defvar org-todo-kwd-alist nil)
  2031. (make-variable-buffer-local 'org-todo-kwd-alist)
  2032. (defvar org-todo-key-alist nil)
  2033. (make-variable-buffer-local 'org-todo-key-alist)
  2034. (defvar org-todo-key-trigger nil)
  2035. (make-variable-buffer-local 'org-todo-key-trigger)
  2036. (defcustom org-todo-interpretation 'sequence
  2037. "Controls how TODO keywords are interpreted.
  2038. This variable is in principle obsolete and is only used for
  2039. backward compatibility, if the interpretation of todo keywords is
  2040. not given already in `org-todo-keywords'. See that variable for
  2041. more information."
  2042. :group 'org-todo
  2043. :group 'org-keywords
  2044. :type '(choice (const sequence)
  2045. (const type)))
  2046. (defcustom org-use-fast-todo-selection t
  2047. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2048. This variable describes if and under what circumstances the cycling
  2049. mechanism for TODO keywords will be replaced by a single-key, direct
  2050. selection scheme.
  2051. When nil, fast selection is never used.
  2052. When the symbol `prefix', it will be used when `org-todo' is called
  2053. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2054. `C-u t' in an agenda buffer.
  2055. When t, fast selection is used by default. In this case, the prefix
  2056. argument forces cycling instead.
  2057. In all cases, the special interface is only used if access keys have
  2058. actually been assigned by the user, i.e. if keywords in the configuration
  2059. are followed by a letter in parenthesis, like TODO(t)."
  2060. :group 'org-todo
  2061. :type '(choice
  2062. (const :tag "Never" nil)
  2063. (const :tag "By default" t)
  2064. (const :tag "Only with C-u C-c C-t" prefix)))
  2065. (defcustom org-provide-todo-statistics t
  2066. "Non-nil means update todo statistics after insert and toggle.
  2067. ALL-HEADLINES means update todo statistics by including headlines
  2068. with no TODO keyword as well, counting them as not done.
  2069. A list of TODO keywords means the same, but skip keywords that are
  2070. not in this list.
  2071. When this is set, todo statistics is updated in the parent of the
  2072. current entry each time a todo state is changed."
  2073. :group 'org-todo
  2074. :type '(choice
  2075. (const :tag "Yes, only for TODO entries" t)
  2076. (const :tag "Yes, including all entries" 'all-headlines)
  2077. (repeat :tag "Yes, for TODOs in this list"
  2078. (string :tag "TODO keyword"))
  2079. (other :tag "No TODO statistics" nil)))
  2080. (defcustom org-hierarchical-todo-statistics t
  2081. "Non-nil means TODO statistics covers just direct children.
  2082. When nil, all entries in the subtree are considered.
  2083. This has only an effect if `org-provide-todo-statistics' is set.
  2084. To set this to nil for only a single subtree, use a COOKIE_DATA
  2085. property and include the word \"recursive\" into the value."
  2086. :group 'org-todo
  2087. :type 'boolean)
  2088. (defcustom org-after-todo-state-change-hook nil
  2089. "Hook which is run after the state of a TODO item was changed.
  2090. The new state (a string with a TODO keyword, or nil) is available in the
  2091. Lisp variable `org-state'."
  2092. :group 'org-todo
  2093. :type 'hook)
  2094. (defvar org-blocker-hook nil
  2095. "Hook for functions that are allowed to block a state change.
  2096. Functions in this hook should not modify the buffer.
  2097. Each function gets as its single argument a property list,
  2098. see `org-trigger-hook' for more information about this list.
  2099. If any of the functions in this hook returns nil, the state change
  2100. is blocked.")
  2101. (defvar org-trigger-hook nil
  2102. "Hook for functions that are triggered by a state change.
  2103. Each function gets as its single argument a property list with at
  2104. least the following elements:
  2105. (:type type-of-change :position pos-at-entry-start
  2106. :from old-state :to new-state)
  2107. Depending on the type, more properties may be present.
  2108. This mechanism is currently implemented for:
  2109. TODO state changes
  2110. ------------------
  2111. :type todo-state-change
  2112. :from previous state (keyword as a string), or nil, or a symbol
  2113. 'todo' or 'done', to indicate the general type of state.
  2114. :to new state, like in :from")
  2115. (defcustom org-enforce-todo-dependencies nil
  2116. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2117. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2118. be blocked if any prior sibling is not yet done.
  2119. Finally, if the parent is blocked because of ordered siblings of its own,
  2120. the child will also be blocked."
  2121. :set (lambda (var val)
  2122. (set var val)
  2123. (if val
  2124. (add-hook 'org-blocker-hook
  2125. 'org-block-todo-from-children-or-siblings-or-parent)
  2126. (remove-hook 'org-blocker-hook
  2127. 'org-block-todo-from-children-or-siblings-or-parent)))
  2128. :group 'org-todo
  2129. :type 'boolean)
  2130. (defcustom org-enforce-todo-checkbox-dependencies nil
  2131. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2132. When this is nil, checkboxes have no influence on switching TODO states.
  2133. When non-nil, you first need to check off all check boxes before the TODO
  2134. entry can be switched to DONE.
  2135. This variable needs to be set before org.el is loaded, and you need to
  2136. restart Emacs after a change to make the change effective. The only way
  2137. to change is while Emacs is running is through the customize interface."
  2138. :set (lambda (var val)
  2139. (set var val)
  2140. (if val
  2141. (add-hook 'org-blocker-hook
  2142. 'org-block-todo-from-checkboxes)
  2143. (remove-hook 'org-blocker-hook
  2144. 'org-block-todo-from-checkboxes)))
  2145. :group 'org-todo
  2146. :type 'boolean)
  2147. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2148. "Non-nil means inserting a TODO heading is treated as state change.
  2149. So when the command \\[org-insert-todo-heading] is used, state change
  2150. logging will apply if appropriate. When nil, the new TODO item will
  2151. be inserted directly, and no logging will take place."
  2152. :group 'org-todo
  2153. :type 'boolean)
  2154. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2155. "Non-nil means switching TODO states with S-cursor counts as state change.
  2156. This is the default behavior. However, setting this to nil allows a
  2157. convenient way to select a TODO state and bypass any logging associated
  2158. with that."
  2159. :group 'org-todo
  2160. :type 'boolean)
  2161. (defcustom org-todo-state-tags-triggers nil
  2162. "Tag changes that should be triggered by TODO state changes.
  2163. This is a list. Each entry is
  2164. (state-change (tag . flag) .......)
  2165. State-change can be a string with a state, and empty string to indicate the
  2166. state that has no TODO keyword, or it can be one of the symbols `todo'
  2167. or `done', meaning any not-done or done state, respectively."
  2168. :group 'org-todo
  2169. :group 'org-tags
  2170. :type '(repeat
  2171. (cons (choice :tag "When changing to"
  2172. (const :tag "Not-done state" todo)
  2173. (const :tag "Done state" done)
  2174. (string :tag "State"))
  2175. (repeat
  2176. (cons :tag "Tag action"
  2177. (string :tag "Tag")
  2178. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2179. (defcustom org-log-done nil
  2180. "Information to record when a task moves to the DONE state.
  2181. Possible values are:
  2182. nil Don't add anything, just change the keyword
  2183. time Add a time stamp to the task
  2184. note Prompt for a note and add it with template `org-log-note-headings'
  2185. This option can also be set with on a per-file-basis with
  2186. #+STARTUP: nologdone
  2187. #+STARTUP: logdone
  2188. #+STARTUP: lognotedone
  2189. You can have local logging settings for a subtree by setting the LOGGING
  2190. property to one or more of these keywords."
  2191. :group 'org-todo
  2192. :group 'org-progress
  2193. :type '(choice
  2194. (const :tag "No logging" nil)
  2195. (const :tag "Record CLOSED timestamp" time)
  2196. (const :tag "Record CLOSED timestamp with note." note)))
  2197. ;; Normalize old uses of org-log-done.
  2198. (cond
  2199. ((eq org-log-done t) (setq org-log-done 'time))
  2200. ((and (listp org-log-done) (memq 'done org-log-done))
  2201. (setq org-log-done 'note)))
  2202. (defcustom org-log-reschedule nil
  2203. "Information to record when the scheduling date of a tasks is modified.
  2204. Possible values are:
  2205. nil Don't add anything, just change the date
  2206. time Add a time stamp to the task
  2207. note Prompt for a note and add it with template `org-log-note-headings'
  2208. This option can also be set with on a per-file-basis with
  2209. #+STARTUP: nologreschedule
  2210. #+STARTUP: logreschedule
  2211. #+STARTUP: lognotereschedule"
  2212. :group 'org-todo
  2213. :group 'org-progress
  2214. :type '(choice
  2215. (const :tag "No logging" nil)
  2216. (const :tag "Record timestamp" time)
  2217. (const :tag "Record timestamp with note." note)))
  2218. (defcustom org-log-redeadline nil
  2219. "Information to record when the deadline date of a tasks is modified.
  2220. Possible values are:
  2221. nil Don't add anything, just change the date
  2222. time Add a time stamp to the task
  2223. note Prompt for a note and add it with template `org-log-note-headings'
  2224. This option can also be set with on a per-file-basis with
  2225. #+STARTUP: nologredeadline
  2226. #+STARTUP: logredeadline
  2227. #+STARTUP: lognoteredeadline
  2228. You can have local logging settings for a subtree by setting the LOGGING
  2229. property to one or more of these keywords."
  2230. :group 'org-todo
  2231. :group 'org-progress
  2232. :type '(choice
  2233. (const :tag "No logging" nil)
  2234. (const :tag "Record timestamp" time)
  2235. (const :tag "Record timestamp with note." note)))
  2236. (defcustom org-log-note-clock-out nil
  2237. "Non-nil means record a note when clocking out of an item.
  2238. This can also be configured on a per-file basis by adding one of
  2239. the following lines anywhere in the buffer:
  2240. #+STARTUP: lognoteclock-out
  2241. #+STARTUP: nolognoteclock-out"
  2242. :group 'org-todo
  2243. :group 'org-progress
  2244. :type 'boolean)
  2245. (defcustom org-log-done-with-time t
  2246. "Non-nil means the CLOSED time stamp will contain date and time.
  2247. When nil, only the date will be recorded."
  2248. :group 'org-progress
  2249. :type 'boolean)
  2250. (defcustom org-log-note-headings
  2251. '((done . "CLOSING NOTE %t")
  2252. (state . "State %-12s from %-12S %t")
  2253. (note . "Note taken on %t")
  2254. (reschedule . "Rescheduled from %S on %t")
  2255. (delschedule . "Not scheduled, was %S on %t")
  2256. (redeadline . "New deadline from %S on %t")
  2257. (deldeadline . "Removed deadline, was %S on %t")
  2258. (refile . "Refiled on %t")
  2259. (clock-out . ""))
  2260. "Headings for notes added to entries.
  2261. The value is an alist, with the car being a symbol indicating the note
  2262. context, and the cdr is the heading to be used. The heading may also be the
  2263. empty string.
  2264. %t in the heading will be replaced by a time stamp.
  2265. %T will be an active time stamp instead the default inactive one
  2266. %d will be replaced by a short-format time stamp.
  2267. %D will be replaced by an active short-format time stamp.
  2268. %s will be replaced by the new TODO state, in double quotes.
  2269. %S will be replaced by the old TODO state, in double quotes.
  2270. %u will be replaced by the user name.
  2271. %U will be replaced by the full user name.
  2272. In fact, it is not a good idea to change the `state' entry, because
  2273. agenda log mode depends on the format of these entries."
  2274. :group 'org-todo
  2275. :group 'org-progress
  2276. :type '(list :greedy t
  2277. (cons (const :tag "Heading when closing an item" done) string)
  2278. (cons (const :tag
  2279. "Heading when changing todo state (todo sequence only)"
  2280. state) string)
  2281. (cons (const :tag "Heading when just taking a note" note) string)
  2282. (cons (const :tag "Heading when clocking out" clock-out) string)
  2283. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2284. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2285. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2286. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2287. (cons (const :tag "Heading when refiling" refile) string)))
  2288. (unless (assq 'note org-log-note-headings)
  2289. (push '(note . "%t") org-log-note-headings))
  2290. (defcustom org-log-into-drawer nil
  2291. "Non-nil means insert state change notes and time stamps into a drawer.
  2292. When nil, state changes notes will be inserted after the headline and
  2293. any scheduling and clock lines, but not inside a drawer.
  2294. The value of this variable should be the name of the drawer to use.
  2295. LOGBOOK is proposed as the default drawer for this purpose, you can
  2296. also set this to a string to define the drawer of your choice.
  2297. A value of t is also allowed, representing \"LOGBOOK\".
  2298. A value of t or nil can also be set with on a per-file-basis with
  2299. #+STARTUP: logdrawer
  2300. #+STARTUP: nologdrawer
  2301. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2302. will be ignored.
  2303. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2304. a subtree."
  2305. :group 'org-todo
  2306. :group 'org-progress
  2307. :type '(choice
  2308. (const :tag "Not into a drawer" nil)
  2309. (const :tag "LOGBOOK" t)
  2310. (string :tag "Other")))
  2311. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2312. (defun org-log-into-drawer ()
  2313. "Return the value of `org-log-into-drawer', but let properties overrule.
  2314. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2315. used instead of the default value."
  2316. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2317. (cond
  2318. ((not p) org-log-into-drawer)
  2319. ((equal p "nil") nil)
  2320. ((equal p "t") "LOGBOOK")
  2321. (t p))))
  2322. (defcustom org-log-state-notes-insert-after-drawers nil
  2323. "Non-nil means insert state change notes after any drawers in entry.
  2324. Only the drawers that *immediately* follow the headline and the
  2325. deadline/scheduled line are skipped.
  2326. When nil, insert notes right after the heading and perhaps the line
  2327. with deadline/scheduling if present.
  2328. This variable will have no effect if `org-log-into-drawer' is
  2329. set."
  2330. :group 'org-todo
  2331. :group 'org-progress
  2332. :type 'boolean)
  2333. (defcustom org-log-states-order-reversed t
  2334. "Non-nil means the latest state note will be directly after heading.
  2335. When nil, the state change notes will be ordered according to time.
  2336. This option can also be set with on a per-file-basis with
  2337. #+STARTUP: logstatesreversed
  2338. #+STARTUP: nologstatesreversed"
  2339. :group 'org-todo
  2340. :group 'org-progress
  2341. :type 'boolean)
  2342. (defcustom org-todo-repeat-to-state nil
  2343. "The TODO state to which a repeater should return the repeating task.
  2344. By default this is the first task in a TODO sequence, or the previous state
  2345. in a TODO_TYP set. But you can specify another task here.
  2346. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2347. :group 'org-todo
  2348. :version "24.1"
  2349. :type '(choice (const :tag "Head of sequence" nil)
  2350. (string :tag "Specific state")))
  2351. (defcustom org-log-repeat 'time
  2352. "Non-nil means record moving through the DONE state when triggering repeat.
  2353. An auto-repeating task is immediately switched back to TODO when
  2354. marked DONE. If you are not logging state changes (by adding \"@\"
  2355. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2356. record a closing note, there will be no record of the task moving
  2357. through DONE. This variable forces taking a note anyway.
  2358. nil Don't force a record
  2359. time Record a time stamp
  2360. note Prompt for a note and add it with template `org-log-note-headings'
  2361. This option can also be set with on a per-file-basis with
  2362. #+STARTUP: nologrepeat
  2363. #+STARTUP: logrepeat
  2364. #+STARTUP: lognoterepeat
  2365. You can have local logging settings for a subtree by setting the LOGGING
  2366. property to one or more of these keywords."
  2367. :group 'org-todo
  2368. :group 'org-progress
  2369. :type '(choice
  2370. (const :tag "Don't force a record" nil)
  2371. (const :tag "Force recording the DONE state" time)
  2372. (const :tag "Force recording a note with the DONE state" note)))
  2373. (defgroup org-priorities nil
  2374. "Priorities in Org-mode."
  2375. :tag "Org Priorities"
  2376. :group 'org-todo)
  2377. (defcustom org-enable-priority-commands t
  2378. "Non-nil means priority commands are active.
  2379. When nil, these commands will be disabled, so that you never accidentally
  2380. set a priority."
  2381. :group 'org-priorities
  2382. :type 'boolean)
  2383. (defcustom org-highest-priority ?A
  2384. "The highest priority of TODO items. A character like ?A, ?B etc.
  2385. Must have a smaller ASCII number than `org-lowest-priority'."
  2386. :group 'org-priorities
  2387. :type 'character)
  2388. (defcustom org-lowest-priority ?C
  2389. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2390. Must have a larger ASCII number than `org-highest-priority'."
  2391. :group 'org-priorities
  2392. :type 'character)
  2393. (defcustom org-default-priority ?B
  2394. "The default priority of TODO items.
  2395. This is the priority an item gets if no explicit priority is given.
  2396. When starting to cycle on an empty priority the first step in the cycle
  2397. depends on `org-priority-start-cycle-with-default'. The resulting first
  2398. step priority must not exceed the range from `org-highest-priority' to
  2399. `org-lowest-priority' which means that `org-default-priority' has to be
  2400. in this range exclusive or inclusive the range boundaries. Else the
  2401. first step refuses to set the default and the second will fall back
  2402. to (depending on the command used) the highest or lowest priority."
  2403. :group 'org-priorities
  2404. :type 'character)
  2405. (defcustom org-priority-start-cycle-with-default t
  2406. "Non-nil means start with default priority when starting to cycle.
  2407. When this is nil, the first step in the cycle will be (depending on the
  2408. command used) one higher or lower than the default priority.
  2409. See also `org-default-priority'."
  2410. :group 'org-priorities
  2411. :type 'boolean)
  2412. (defcustom org-get-priority-function nil
  2413. "Function to extract the priority from a string.
  2414. The string is normally the headline. If this is nil Org computes the
  2415. priority from the priority cookie like [#A] in the headline. It returns
  2416. an integer, increasing by 1000 for each priority level.
  2417. The user can set a different function here, which should take a string
  2418. as an argument and return the numeric priority."
  2419. :group 'org-priorities
  2420. :version "24.1"
  2421. :type 'function)
  2422. (defgroup org-time nil
  2423. "Options concerning time stamps and deadlines in Org-mode."
  2424. :tag "Org Time"
  2425. :group 'org)
  2426. (defcustom org-insert-labeled-timestamps-at-point nil
  2427. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2428. When nil, these labeled time stamps are forces into the second line of an
  2429. entry, just after the headline. When scheduling from the global TODO list,
  2430. the time stamp will always be forced into the second line."
  2431. :group 'org-time
  2432. :type 'boolean)
  2433. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2434. "Formats for `format-time-string' which are used for time stamps.
  2435. It is not recommended to change this constant.")
  2436. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2437. "Number of minutes to round time stamps to.
  2438. These are two values, the first applies when first creating a time stamp.
  2439. The second applies when changing it with the commands `S-up' and `S-down'.
  2440. When changing the time stamp, this means that it will change in steps
  2441. of N minutes, as given by the second value.
  2442. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2443. numbers should be factors of 60, so for example 5, 10, 15.
  2444. When this is larger than 1, you can still force an exact time stamp by using
  2445. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2446. and by using a prefix arg to `S-up/down' to specify the exact number
  2447. of minutes to shift."
  2448. :group 'org-time
  2449. :get #'(lambda (var) ; Make sure both elements are there
  2450. (if (integerp (default-value var))
  2451. (list (default-value var) 5)
  2452. (default-value var)))
  2453. :type '(list
  2454. (integer :tag "when inserting times")
  2455. (integer :tag "when modifying times")))
  2456. ;; Normalize old customizations of this variable.
  2457. (when (integerp org-time-stamp-rounding-minutes)
  2458. (setq org-time-stamp-rounding-minutes
  2459. (list org-time-stamp-rounding-minutes
  2460. org-time-stamp-rounding-minutes)))
  2461. (defcustom org-display-custom-times nil
  2462. "Non-nil means overlay custom formats over all time stamps.
  2463. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2464. To turn this on on a per-file basis, insert anywhere in the file:
  2465. #+STARTUP: customtime"
  2466. :group 'org-time
  2467. :set 'set-default
  2468. :type 'sexp)
  2469. (make-variable-buffer-local 'org-display-custom-times)
  2470. (defcustom org-time-stamp-custom-formats
  2471. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2472. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2473. These are overlaid over the default ISO format if the variable
  2474. `org-display-custom-times' is set. Time like %H:%M should be at the
  2475. end of the second format. The custom formats are also honored by export
  2476. commands, if custom time display is turned on at the time of export."
  2477. :group 'org-time
  2478. :type 'sexp)
  2479. (defun org-time-stamp-format (&optional long inactive)
  2480. "Get the right format for a time string."
  2481. (let ((f (if long (cdr org-time-stamp-formats)
  2482. (car org-time-stamp-formats))))
  2483. (if inactive
  2484. (concat "[" (substring f 1 -1) "]")
  2485. f)))
  2486. (defcustom org-time-clocksum-format
  2487. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2488. "The format string used when creating CLOCKSUM lines.
  2489. This is also used when Org mode generates a time duration.
  2490. The value can be a single format string containing two
  2491. %-sequences, which will be filled with the number of hours and
  2492. minutes in that order.
  2493. Alternatively, the value can be a plist associating any of the
  2494. keys :years, :months, :weeks, :days, :hours or :minutes with
  2495. format strings. The time duration is formatted using only the
  2496. time components that are needed and concatenating the results.
  2497. If a time unit in absent, it falls back to the next smallest
  2498. unit.
  2499. The keys :require-years, :require-months, :require-days,
  2500. :require-weeks, :require-hours, :require-minutes are also
  2501. meaningful. A non-nil value for these keys indicates that the
  2502. corresponding time component should always be included, even if
  2503. its value is 0.
  2504. For example,
  2505. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2506. :require-minutes t)
  2507. means durations longer than a day will be expressed in days,
  2508. hours and minutes, and durations less than a day will always be
  2509. expressed in hours and minutes (even for durations less than an
  2510. hour).
  2511. The value
  2512. \(:days \"%dd\" :minutes \"%dm\")
  2513. means durations longer than a day will be expressed in days and
  2514. minutes, and durations less than a day will be expressed entirely
  2515. in minutes (even for durations longer than an hour)."
  2516. :group 'org-time
  2517. :group 'org-clock
  2518. :version "24.4"
  2519. :package-version '(Org . "8.0")
  2520. :type '(choice (string :tag "Format string")
  2521. (set :tag "Plist"
  2522. (group :inline t (const :tag "Years" :years)
  2523. (string :tag "Format string"))
  2524. (group :inline t
  2525. (const :tag "Always show years" :require-years)
  2526. (const t))
  2527. (group :inline t (const :tag "Months" :months)
  2528. (string :tag "Format string"))
  2529. (group :inline t
  2530. (const :tag "Always show months" :require-months)
  2531. (const t))
  2532. (group :inline t (const :tag "Weeks" :weeks)
  2533. (string :tag "Format string"))
  2534. (group :inline t
  2535. (const :tag "Always show weeks" :require-weeks)
  2536. (const t))
  2537. (group :inline t (const :tag "Days" :days)
  2538. (string :tag "Format string"))
  2539. (group :inline t
  2540. (const :tag "Always show days" :require-days)
  2541. (const t))
  2542. (group :inline t (const :tag "Hours" :hours)
  2543. (string :tag "Format string"))
  2544. (group :inline t
  2545. (const :tag "Always show hours" :require-hours)
  2546. (const t))
  2547. (group :inline t (const :tag "Minutes" :minutes)
  2548. (string :tag "Format string"))
  2549. (group :inline t
  2550. (const :tag "Always show minutes" :require-minutes)
  2551. (const t)))))
  2552. (defcustom org-time-clocksum-use-fractional nil
  2553. "When non-nil, \\[org-clock-display] uses fractional times.
  2554. See `org-time-clocksum-format' for more on time clock formats."
  2555. :group 'org-time
  2556. :group 'org-clock
  2557. :version "24.3"
  2558. :type 'boolean)
  2559. (defcustom org-time-clocksum-use-effort-durations nil
  2560. "When non-nil, \\[org-clock-display] uses effort durations.
  2561. E.g. by default, one day is considered to be a 8 hours effort,
  2562. so a task that has been clocked for 16 hours will be displayed
  2563. as during 2 days in the clock display or in the clocktable.
  2564. See `org-effort-durations' on how to set effort durations
  2565. and `org-time-clocksum-format' for more on time clock formats."
  2566. :group 'org-time
  2567. :group 'org-clock
  2568. :version "24.4"
  2569. :package-version '(Org . "8.0")
  2570. :type 'boolean)
  2571. (defcustom org-time-clocksum-fractional-format "%.2f"
  2572. "The format string used when creating CLOCKSUM lines,
  2573. or when Org mode generates a time duration, if
  2574. `org-time-clocksum-use-fractional' is enabled.
  2575. The value can be a single format string containing one
  2576. %-sequence, which will be filled with the number of hours as
  2577. a float.
  2578. Alternatively, the value can be a plist associating any of the
  2579. keys :years, :months, :weeks, :days, :hours or :minutes with
  2580. a format string. The time duration is formatted using the
  2581. largest time unit which gives a non-zero integer part. If all
  2582. specified formats have zero integer part, the smallest time unit
  2583. is used."
  2584. :group 'org-time
  2585. :type '(choice (string :tag "Format string")
  2586. (set (group :inline t (const :tag "Years" :years)
  2587. (string :tag "Format string"))
  2588. (group :inline t (const :tag "Months" :months)
  2589. (string :tag "Format string"))
  2590. (group :inline t (const :tag "Weeks" :weeks)
  2591. (string :tag "Format string"))
  2592. (group :inline t (const :tag "Days" :days)
  2593. (string :tag "Format string"))
  2594. (group :inline t (const :tag "Hours" :hours)
  2595. (string :tag "Format string"))
  2596. (group :inline t (const :tag "Minutes" :minutes)
  2597. (string :tag "Format string")))))
  2598. (defcustom org-deadline-warning-days 14
  2599. "Number of days before expiration during which a deadline becomes active.
  2600. This variable governs the display in sparse trees and in the agenda.
  2601. When 0 or negative, it means use this number (the absolute value of it)
  2602. even if a deadline has a different individual lead time specified.
  2603. Custom commands can set this variable in the options section."
  2604. :group 'org-time
  2605. :group 'org-agenda-daily/weekly
  2606. :type 'integer)
  2607. (defcustom org-scheduled-delay-days 0
  2608. "Number of days before a scheduled item becomes active.
  2609. This variable governs the display in sparse trees and in the agenda.
  2610. The default value (i.e. 0) means: don't delay scheduled item.
  2611. When negative, it means use this number (the absolute value of it)
  2612. even if a scheduled item has a different individual delay time
  2613. specified.
  2614. Custom commands can set this variable in the options section."
  2615. :group 'org-time
  2616. :group 'org-agenda-daily/weekly
  2617. :version "24.4"
  2618. :package-version '(Org . "8.0")
  2619. :type 'integer)
  2620. (defcustom org-read-date-prefer-future t
  2621. "Non-nil means assume future for incomplete date input from user.
  2622. This affects the following situations:
  2623. 1. The user gives a month but not a year.
  2624. For example, if it is April and you enter \"feb 2\", this will be read
  2625. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2626. 2. The user gives a day, but no month.
  2627. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2628. read this as the third of *next* month. However, if you enter \"17\",
  2629. it will be considered as *this* month.
  2630. If you set this variable to the symbol `time', then also the following
  2631. will work:
  2632. 3. If the user gives a time.
  2633. If the time is before now, it will be interpreted as tomorrow.
  2634. Currently none of this works for ISO week specifications.
  2635. When this option is nil, the current day, month and year will always be
  2636. used as defaults.
  2637. See also `org-agenda-jump-prefer-future'."
  2638. :group 'org-time
  2639. :type '(choice
  2640. (const :tag "Never" nil)
  2641. (const :tag "Check month and day" t)
  2642. (const :tag "Check month, day, and time" time)))
  2643. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2644. "Should the agenda jump command prefer the future for incomplete dates?
  2645. The default is to do the same as configured in `org-read-date-prefer-future'.
  2646. But you can also set a deviating value here.
  2647. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2648. :group 'org-agenda
  2649. :group 'org-time
  2650. :version "24.1"
  2651. :type '(choice
  2652. (const :tag "Use org-read-date-prefer-future"
  2653. org-read-date-prefer-future)
  2654. (const :tag "Never" nil)
  2655. (const :tag "Always" t)))
  2656. (defcustom org-read-date-force-compatible-dates t
  2657. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2658. Depending on the system Emacs is running on, certain dates cannot
  2659. be represented with the type used internally to represent time.
  2660. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2661. correctly. Some systems allow for earlier dates, some for later,
  2662. some for both. One way to find out it to insert any date into an
  2663. Org buffer, putting the cursor on the year and hitting S-up and
  2664. S-down to test the range.
  2665. When this variable is set to t, the date/time prompt will not let
  2666. you specify dates outside the 1970-2037 range, so it is certain that
  2667. these dates will work in whatever version of Emacs you are
  2668. running, and also that you can move a file from one Emacs implementation
  2669. to another. WHenever Org is forcing the year for you, it will display
  2670. a message and beep.
  2671. When this variable is nil, Org will check if the date is
  2672. representable in the specific Emacs implementation you are using.
  2673. If not, it will force a year, usually the current year, and beep
  2674. to remind you. Currently this setting is not recommended because
  2675. the likelihood that you will open your Org files in an Emacs that
  2676. has limited date range is not negligible.
  2677. A workaround for this problem is to use diary sexp dates for time
  2678. stamps outside of this range."
  2679. :group 'org-time
  2680. :version "24.1"
  2681. :type 'boolean)
  2682. (defcustom org-read-date-display-live t
  2683. "Non-nil means display current interpretation of date prompt live.
  2684. This display will be in an overlay, in the minibuffer."
  2685. :group 'org-time
  2686. :type 'boolean)
  2687. (defcustom org-read-date-popup-calendar t
  2688. "Non-nil means pop up a calendar when prompting for a date.
  2689. In the calendar, the date can be selected with mouse-1. However, the
  2690. minibuffer will also be active, and you can simply enter the date as well.
  2691. When nil, only the minibuffer will be available."
  2692. :group 'org-time
  2693. :type 'boolean)
  2694. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2695. 'org-read-date-popup-calendar)
  2696. (make-obsolete-variable
  2697. 'org-read-date-minibuffer-setup-hook
  2698. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2699. (defcustom org-read-date-minibuffer-setup-hook nil
  2700. "Hook to be used to set up keys for the date/time interface.
  2701. Add key definitions to `minibuffer-local-map', which will be a
  2702. temporary copy.
  2703. WARNING: This option is obsolete, you should use
  2704. `org-read-date-minibuffer-local-map' to set up keys."
  2705. :group 'org-time
  2706. :type 'hook)
  2707. (defcustom org-extend-today-until 0
  2708. "The hour when your day really ends. Must be an integer.
  2709. This has influence for the following applications:
  2710. - When switching the agenda to \"today\". It it is still earlier than
  2711. the time given here, the day recognized as TODAY is actually yesterday.
  2712. - When a date is read from the user and it is still before the time given
  2713. here, the current date and time will be assumed to be yesterday, 23:59.
  2714. Also, timestamps inserted in capture templates follow this rule.
  2715. IMPORTANT: This is a feature whose implementation is and likely will
  2716. remain incomplete. Really, it is only here because past midnight seems to
  2717. be the favorite working time of John Wiegley :-)"
  2718. :group 'org-time
  2719. :type 'integer)
  2720. (defcustom org-use-effective-time nil
  2721. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2722. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2723. \"effective time\" of any timestamps between midnight and 8am will be
  2724. 23:59 of the previous day."
  2725. :group 'org-time
  2726. :version "24.1"
  2727. :type 'boolean)
  2728. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2729. "When non-nil, use the last clock out time for `org-todo'.
  2730. Note that this option has precedence over the combined use of
  2731. `org-use-effective-time' and `org-extend-today-until'."
  2732. :group 'org-time
  2733. :version "24.4"
  2734. :package-version '(Org . "8.0")
  2735. :type 'boolean)
  2736. (defcustom org-edit-timestamp-down-means-later nil
  2737. "Non-nil means S-down will increase the time in a time stamp.
  2738. When nil, S-up will increase."
  2739. :group 'org-time
  2740. :type 'boolean)
  2741. (defcustom org-calendar-follow-timestamp-change t
  2742. "Non-nil means make the calendar window follow timestamp changes.
  2743. When a timestamp is modified and the calendar window is visible, it will be
  2744. moved to the new date."
  2745. :group 'org-time
  2746. :type 'boolean)
  2747. (defgroup org-tags nil
  2748. "Options concerning tags in Org-mode."
  2749. :tag "Org Tags"
  2750. :group 'org)
  2751. (defcustom org-tag-alist nil
  2752. "List of tags allowed in Org-mode files.
  2753. When this list is nil, Org-mode will base TAG input on what is already in the
  2754. buffer.
  2755. The value of this variable is an alist, the car of each entry must be a
  2756. keyword as a string, the cdr may be a character that is used to select
  2757. that tag through the fast-tag-selection interface.
  2758. See the manual for details."
  2759. :group 'org-tags
  2760. :type '(repeat
  2761. (choice
  2762. (cons (string :tag "Tag name")
  2763. (character :tag "Access char"))
  2764. (list :tag "Start radio group"
  2765. (const :startgroup)
  2766. (option (string :tag "Group description")))
  2767. (list :tag "End radio group"
  2768. (const :endgroup)
  2769. (option (string :tag "Group description")))
  2770. (const :tag "New line" (:newline)))))
  2771. (defcustom org-tag-persistent-alist nil
  2772. "List of tags that will always appear in all Org-mode files.
  2773. This is in addition to any in buffer settings or customizations
  2774. of `org-tag-alist'.
  2775. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2776. The value of this variable is an alist, the car of each entry must be a
  2777. keyword as a string, the cdr may be a character that is used to select
  2778. that tag through the fast-tag-selection interface.
  2779. See the manual for details.
  2780. To disable these tags on a per-file basis, insert anywhere in the file:
  2781. #+STARTUP: noptag"
  2782. :group 'org-tags
  2783. :type '(repeat
  2784. (choice
  2785. (cons (string :tag "Tag name")
  2786. (character :tag "Access char"))
  2787. (const :tag "Start radio group" (:startgroup))
  2788. (const :tag "End radio group" (:endgroup))
  2789. (const :tag "New line" (:newline)))))
  2790. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2791. "If non-nil, always offer completion for all tags of all agenda files.
  2792. Instead of customizing this variable directly, you might want to
  2793. set it locally for capture buffers, because there no list of
  2794. tags in that file can be created dynamically (there are none).
  2795. (add-hook 'org-capture-mode-hook
  2796. (lambda ()
  2797. (set (make-local-variable
  2798. 'org-complete-tags-always-offer-all-agenda-tags)
  2799. t)))"
  2800. :group 'org-tags
  2801. :version "24.1"
  2802. :type 'boolean)
  2803. (defvar org-file-tags nil
  2804. "List of tags that can be inherited by all entries in the file.
  2805. The tags will be inherited if the variable `org-use-tag-inheritance'
  2806. says they should be.
  2807. This variable is populated from #+FILETAGS lines.")
  2808. (defcustom org-use-fast-tag-selection 'auto
  2809. "Non-nil means use fast tag selection scheme.
  2810. This is a special interface to select and deselect tags with single keys.
  2811. When nil, fast selection is never used.
  2812. When the symbol `auto', fast selection is used if and only if selection
  2813. characters for tags have been configured, either through the variable
  2814. `org-tag-alist' or through a #+TAGS line in the buffer.
  2815. When t, fast selection is always used and selection keys are assigned
  2816. automatically if necessary."
  2817. :group 'org-tags
  2818. :type '(choice
  2819. (const :tag "Always" t)
  2820. (const :tag "Never" nil)
  2821. (const :tag "When selection characters are configured" 'auto)))
  2822. (defcustom org-fast-tag-selection-single-key nil
  2823. "Non-nil means fast tag selection exits after first change.
  2824. When nil, you have to press RET to exit it.
  2825. During fast tag selection, you can toggle this flag with `C-c'.
  2826. This variable can also have the value `expert'. In this case, the window
  2827. displaying the tags menu is not even shown, until you press C-c again."
  2828. :group 'org-tags
  2829. :type '(choice
  2830. (const :tag "No" nil)
  2831. (const :tag "Yes" t)
  2832. (const :tag "Expert" expert)))
  2833. (defvar org-fast-tag-selection-include-todo nil
  2834. "Non-nil means fast tags selection interface will also offer TODO states.
  2835. This is an undocumented feature, you should not rely on it.")
  2836. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2837. "The column to which tags should be indented in a headline.
  2838. If this number is positive, it specifies the column. If it is negative,
  2839. it means that the tags should be flushright to that column. For example,
  2840. -80 works well for a normal 80 character screen.
  2841. When 0, place tags directly after headline text, with only one space in
  2842. between."
  2843. :group 'org-tags
  2844. :type 'integer)
  2845. (defcustom org-auto-align-tags t
  2846. "Non-nil keeps tags aligned when modifying headlines.
  2847. Some operations (i.e. demoting) change the length of a headline and
  2848. therefore shift the tags around. With this option turned on, after
  2849. each such operation the tags are again aligned to `org-tags-column'."
  2850. :group 'org-tags
  2851. :type 'boolean)
  2852. (defcustom org-use-tag-inheritance t
  2853. "Non-nil means tags in levels apply also for sublevels.
  2854. When nil, only the tags directly given in a specific line apply there.
  2855. This may also be a list of tags that should be inherited, or a regexp that
  2856. matches tags that should be inherited. Additional control is possible
  2857. with the variable `org-tags-exclude-from-inheritance' which gives an
  2858. explicit list of tags to be excluded from inheritance, even if the value of
  2859. `org-use-tag-inheritance' would select it for inheritance.
  2860. If this option is t, a match early-on in a tree can lead to a large
  2861. number of matches in the subtree when constructing the agenda or creating
  2862. a sparse tree. If you only want to see the first match in a tree during
  2863. a search, check out the variable `org-tags-match-list-sublevels'."
  2864. :group 'org-tags
  2865. :type '(choice
  2866. (const :tag "Not" nil)
  2867. (const :tag "Always" t)
  2868. (repeat :tag "Specific tags" (string :tag "Tag"))
  2869. (regexp :tag "Tags matched by regexp")))
  2870. (defcustom org-tags-exclude-from-inheritance nil
  2871. "List of tags that should never be inherited.
  2872. This is a way to exclude a few tags from inheritance. For way to do
  2873. the opposite, to actively allow inheritance for selected tags,
  2874. see the variable `org-use-tag-inheritance'."
  2875. :group 'org-tags
  2876. :type '(repeat (string :tag "Tag")))
  2877. (defun org-tag-inherit-p (tag)
  2878. "Check if TAG is one that should be inherited."
  2879. (cond
  2880. ((member tag org-tags-exclude-from-inheritance) nil)
  2881. ((eq org-use-tag-inheritance t) t)
  2882. ((not org-use-tag-inheritance) nil)
  2883. ((stringp org-use-tag-inheritance)
  2884. (string-match org-use-tag-inheritance tag))
  2885. ((listp org-use-tag-inheritance)
  2886. (member tag org-use-tag-inheritance))
  2887. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2888. (defcustom org-tags-match-list-sublevels t
  2889. "Non-nil means list also sublevels of headlines matching a search.
  2890. This variable applies to tags/property searches, and also to stuck
  2891. projects because this search is based on a tags match as well.
  2892. When set to the symbol `indented', sublevels are indented with
  2893. leading dots.
  2894. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2895. the sublevels of a headline matching a tag search often also match
  2896. the same search. Listing all of them can create very long lists.
  2897. Setting this variable to nil causes subtrees of a match to be skipped.
  2898. This variable is semi-obsolete and probably should always be true. It
  2899. is better to limit inheritance to certain tags using the variables
  2900. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2901. :group 'org-tags
  2902. :type '(choice
  2903. (const :tag "No, don't list them" nil)
  2904. (const :tag "Yes, do list them" t)
  2905. (const :tag "List them, indented with leading dots" indented)))
  2906. (defcustom org-tags-sort-function nil
  2907. "When set, tags are sorted using this function as a comparator."
  2908. :group 'org-tags
  2909. :type '(choice
  2910. (const :tag "No sorting" nil)
  2911. (const :tag "Alphabetical" string<)
  2912. (const :tag "Reverse alphabetical" string>)
  2913. (function :tag "Custom function" nil)))
  2914. (defvar org-tags-history nil
  2915. "History of minibuffer reads for tags.")
  2916. (defvar org-last-tags-completion-table nil
  2917. "The last used completion table for tags.")
  2918. (defvar org-after-tags-change-hook nil
  2919. "Hook that is run after the tags in a line have changed.")
  2920. (defgroup org-properties nil
  2921. "Options concerning properties in Org-mode."
  2922. :tag "Org Properties"
  2923. :group 'org)
  2924. (defcustom org-property-format "%-10s %s"
  2925. "How property key/value pairs should be formatted by `indent-line'.
  2926. When `indent-line' hits a property definition, it will format the line
  2927. according to this format, mainly to make sure that the values are
  2928. lined-up with respect to each other."
  2929. :group 'org-properties
  2930. :type 'string)
  2931. (defcustom org-properties-postprocess-alist nil
  2932. "Alist of properties and functions to adjust inserted values.
  2933. Elements of this alist must be of the form
  2934. ([string] [function])
  2935. where [string] must be a property name and [function] must be a
  2936. lambda expression: this lambda expression must take one argument,
  2937. the value to adjust, and return the new value as a string.
  2938. For example, this element will allow the property \"Remaining\"
  2939. to be updated wrt the relation between the \"Effort\" property
  2940. and the clock summary:
  2941. ((\"Remaining\" (lambda(value)
  2942. (let ((clocksum (org-clock-sum-current-item))
  2943. (effort (org-duration-string-to-minutes
  2944. (org-entry-get (point) \"Effort\"))))
  2945. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2946. :group 'org-properties
  2947. :version "24.1"
  2948. :type '(alist :key-type (string :tag "Property")
  2949. :value-type (function :tag "Function")))
  2950. (defcustom org-use-property-inheritance nil
  2951. "Non-nil means properties apply also for sublevels.
  2952. This setting is chiefly used during property searches. Turning it on can
  2953. cause significant overhead when doing a search, which is why it is not
  2954. on by default.
  2955. When nil, only the properties directly given in the current entry count.
  2956. When t, every property is inherited. The value may also be a list of
  2957. properties that should have inheritance, or a regular expression matching
  2958. properties that should be inherited.
  2959. However, note that some special properties use inheritance under special
  2960. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2961. and the properties ending in \"_ALL\" when they are used as descriptor
  2962. for valid values of a property.
  2963. Note for programmers:
  2964. When querying an entry with `org-entry-get', you can control if inheritance
  2965. should be used. By default, `org-entry-get' looks only at the local
  2966. properties. You can request inheritance by setting the inherit argument
  2967. to t (to force inheritance) or to `selective' (to respect the setting
  2968. in this variable)."
  2969. :group 'org-properties
  2970. :type '(choice
  2971. (const :tag "Not" nil)
  2972. (const :tag "Always" t)
  2973. (repeat :tag "Specific properties" (string :tag "Property"))
  2974. (regexp :tag "Properties matched by regexp")))
  2975. (defun org-property-inherit-p (property)
  2976. "Check if PROPERTY is one that should be inherited."
  2977. (cond
  2978. ((eq org-use-property-inheritance t) t)
  2979. ((not org-use-property-inheritance) nil)
  2980. ((stringp org-use-property-inheritance)
  2981. (string-match org-use-property-inheritance property))
  2982. ((listp org-use-property-inheritance)
  2983. (member property org-use-property-inheritance))
  2984. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2985. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2986. "The default column format, if no other format has been defined.
  2987. This variable can be set on the per-file basis by inserting a line
  2988. #+COLUMNS: %25ITEM ....."
  2989. :group 'org-properties
  2990. :type 'string)
  2991. (defcustom org-columns-ellipses ".."
  2992. "The ellipses to be used when a field in column view is truncated.
  2993. When this is the empty string, as many characters as possible are shown,
  2994. but then there will be no visual indication that the field has been truncated.
  2995. When this is a string of length N, the last N characters of a truncated
  2996. field are replaced by this string. If the column is narrower than the
  2997. ellipses string, only part of the ellipses string will be shown."
  2998. :group 'org-properties
  2999. :type 'string)
  3000. (defcustom org-columns-modify-value-for-display-function nil
  3001. "Function that modifies values for display in column view.
  3002. For example, it can be used to cut out a certain part from a time stamp.
  3003. The function must take 2 arguments:
  3004. column-title The title of the column (*not* the property name)
  3005. value The value that should be modified.
  3006. The function should return the value that should be displayed,
  3007. or nil if the normal value should be used."
  3008. :group 'org-properties
  3009. :type 'function)
  3010. (defcustom org-effort-property "Effort"
  3011. "The property that is being used to keep track of effort estimates.
  3012. Effort estimates given in this property need to have the format H:MM."
  3013. :group 'org-properties
  3014. :group 'org-progress
  3015. :type '(string :tag "Property"))
  3016. (defconst org-global-properties-fixed
  3017. '(("VISIBILITY_ALL" . "folded children content all")
  3018. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3019. "List of property/value pairs that can be inherited by any entry.
  3020. These are fixed values, for the preset properties. The user variable
  3021. that can be used to add to this list is `org-global-properties'.
  3022. The entries in this list are cons cells where the car is a property
  3023. name and cdr is a string with the value. If the value represents
  3024. multiple items like an \"_ALL\" property, separate the items by
  3025. spaces.")
  3026. (defcustom org-global-properties nil
  3027. "List of property/value pairs that can be inherited by any entry.
  3028. This list will be combined with the constant `org-global-properties-fixed'.
  3029. The entries in this list are cons cells where the car is a property
  3030. name and cdr is a string with the value.
  3031. You can set buffer-local values for the same purpose in the variable
  3032. `org-file-properties' this by adding lines like
  3033. #+PROPERTY: NAME VALUE"
  3034. :group 'org-properties
  3035. :type '(repeat
  3036. (cons (string :tag "Property")
  3037. (string :tag "Value"))))
  3038. (defvar org-file-properties nil
  3039. "List of property/value pairs that can be inherited by any entry.
  3040. Valid for the current buffer.
  3041. This variable is populated from #+PROPERTY lines.")
  3042. (make-variable-buffer-local 'org-file-properties)
  3043. (defgroup org-agenda nil
  3044. "Options concerning agenda views in Org-mode."
  3045. :tag "Org Agenda"
  3046. :group 'org)
  3047. (defvar org-category nil
  3048. "Variable used by org files to set a category for agenda display.
  3049. Such files should use a file variable to set it, for example
  3050. # -*- mode: org; org-category: \"ELisp\"
  3051. or contain a special line
  3052. #+CATEGORY: ELisp
  3053. If the file does not specify a category, then file's base name
  3054. is used instead.")
  3055. (make-variable-buffer-local 'org-category)
  3056. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3057. (defcustom org-agenda-files nil
  3058. "The files to be used for agenda display.
  3059. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3060. \\[org-remove-file]. You can also use customize to edit the list.
  3061. If an entry is a directory, all files in that directory that are matched by
  3062. `org-agenda-file-regexp' will be part of the file list.
  3063. If the value of the variable is not a list but a single file name, then
  3064. the list of agenda files is actually stored and maintained in that file, one
  3065. agenda file per line. In this file paths can be given relative to
  3066. `org-directory'. Tilde expansion and environment variable substitution
  3067. are also made."
  3068. :group 'org-agenda
  3069. :type '(choice
  3070. (repeat :tag "List of files and directories" file)
  3071. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3072. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3073. "Regular expression to match files for `org-agenda-files'.
  3074. If any element in the list in that variable contains a directory instead
  3075. of a normal file, all files in that directory that are matched by this
  3076. regular expression will be included."
  3077. :group 'org-agenda
  3078. :type 'regexp)
  3079. (defcustom org-agenda-text-search-extra-files nil
  3080. "List of extra files to be searched by text search commands.
  3081. These files will be search in addition to the agenda files by the
  3082. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3083. Note that these files will only be searched for text search commands,
  3084. not for the other agenda views like todo lists, tag searches or the weekly
  3085. agenda. This variable is intended to list notes and possibly archive files
  3086. that should also be searched by these two commands.
  3087. In fact, if the first element in the list is the symbol `agenda-archives',
  3088. than all archive files of all agenda files will be added to the search
  3089. scope."
  3090. :group 'org-agenda
  3091. :type '(set :greedy t
  3092. (const :tag "Agenda Archives" agenda-archives)
  3093. (repeat :inline t (file))))
  3094. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3095. 'org-agenda-text-search-extra-files)
  3096. (defcustom org-agenda-skip-unavailable-files nil
  3097. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3098. A nil value means to remove them, after a query, from the list."
  3099. :group 'org-agenda
  3100. :type 'boolean)
  3101. (defcustom org-calendar-to-agenda-key [?c]
  3102. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3103. The command `org-calendar-goto-agenda' will be bound to this key. The
  3104. default is the character `c' because then `c' can be used to switch back and
  3105. forth between agenda and calendar."
  3106. :group 'org-agenda
  3107. :type 'sexp)
  3108. (defcustom org-calendar-insert-diary-entry-key [?i]
  3109. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3110. This option is irrelevant until `org-agenda-diary-file' has been configured
  3111. to point to an Org-mode file. When that is the case, the command
  3112. `org-agenda-diary-entry' will be bound to the key given here, by default
  3113. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3114. if you want to continue doing this, you need to change this to a different
  3115. key."
  3116. :group 'org-agenda
  3117. :type 'sexp)
  3118. (defcustom org-agenda-diary-file 'diary-file
  3119. "File to which to add new entries with the `i' key in agenda and calendar.
  3120. When this is the symbol `diary-file', the functionality in the Emacs
  3121. calendar will be used to add entries to the `diary-file'. But when this
  3122. points to a file, `org-agenda-diary-entry' will be used instead."
  3123. :group 'org-agenda
  3124. :type '(choice
  3125. (const :tag "The standard Emacs diary file" diary-file)
  3126. (file :tag "Special Org file diary entries")))
  3127. (eval-after-load "calendar"
  3128. '(progn
  3129. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3130. 'org-calendar-goto-agenda)
  3131. (add-hook 'calendar-mode-hook
  3132. (lambda ()
  3133. (unless (eq org-agenda-diary-file 'diary-file)
  3134. (define-key calendar-mode-map
  3135. org-calendar-insert-diary-entry-key
  3136. 'org-agenda-diary-entry))))))
  3137. (defgroup org-latex nil
  3138. "Options for embedding LaTeX code into Org-mode."
  3139. :tag "Org LaTeX"
  3140. :group 'org)
  3141. (defcustom org-format-latex-options
  3142. '(:foreground default :background default :scale 1.0
  3143. :html-foreground "Black" :html-background "Transparent"
  3144. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3145. "Options for creating images from LaTeX fragments.
  3146. This is a property list with the following properties:
  3147. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3148. `default' means use the foreground of the default face.
  3149. `auto' means use the foreground from the text face.
  3150. :background the background color, or \"Transparent\".
  3151. `default' means use the background of the default face.
  3152. `auto' means use the background from the text face.
  3153. :scale a scaling factor for the size of the images, to get more pixels
  3154. :html-foreground, :html-background, :html-scale
  3155. the same numbers for HTML export.
  3156. :matchers a list indicating which matchers should be used to
  3157. find LaTeX fragments. Valid members of this list are:
  3158. \"begin\" find environments
  3159. \"$1\" find single characters surrounded by $.$
  3160. \"$\" find math expressions surrounded by $...$
  3161. \"$$\" find math expressions surrounded by $$....$$
  3162. \"\\(\" find math expressions surrounded by \\(...\\)
  3163. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3164. :group 'org-latex
  3165. :type 'plist)
  3166. (defcustom org-format-latex-signal-error t
  3167. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3168. When nil, just push out a message."
  3169. :group 'org-latex
  3170. :version "24.1"
  3171. :type 'boolean)
  3172. (defcustom org-latex-to-mathml-jar-file nil
  3173. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3174. Use this to specify additional executable file say a jar file.
  3175. When using MathToWeb as the converter, specify the full-path to
  3176. your mathtoweb.jar file."
  3177. :group 'org-latex
  3178. :version "24.1"
  3179. :type '(choice
  3180. (const :tag "None" nil)
  3181. (file :tag "JAR file" :must-match t)))
  3182. (defcustom org-latex-to-mathml-convert-command nil
  3183. "Command to convert LaTeX fragments to MathML.
  3184. Replace format-specifiers in the command as noted below and use
  3185. `shell-command' to convert LaTeX to MathML.
  3186. %j: Executable file in fully expanded form as specified by
  3187. `org-latex-to-mathml-jar-file'.
  3188. %I: Input LaTeX file in fully expanded form
  3189. %o: Output MathML file
  3190. This command is used by `org-create-math-formula'.
  3191. When using MathToWeb as the converter, set this to
  3192. \"java -jar %j -unicode -force -df %o %I\"."
  3193. :group 'org-latex
  3194. :version "24.1"
  3195. :type '(choice
  3196. (const :tag "None" nil)
  3197. (string :tag "\nShell command")))
  3198. (defcustom org-latex-create-formula-image-program 'dvipng
  3199. "Program to convert LaTeX fragments with.
  3200. dvipng Process the LaTeX fragments to dvi file, then convert
  3201. dvi files to png files using dvipng.
  3202. This will also include processing of non-math environments.
  3203. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3204. to convert pdf files to png files"
  3205. :group 'org-latex
  3206. :version "24.1"
  3207. :type '(choice
  3208. (const :tag "dvipng" dvipng)
  3209. (const :tag "imagemagick" imagemagick)))
  3210. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3211. "Path to store latex preview images.
  3212. A relative path here creates many directories relative to the
  3213. processed org files paths. An absolute path puts all preview
  3214. images at the same place."
  3215. :group 'org-latex
  3216. :version "24.3"
  3217. :type 'string)
  3218. (defun org-format-latex-mathml-available-p ()
  3219. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3220. (save-match-data
  3221. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3222. org-latex-to-mathml-convert-command)
  3223. (let ((executable (car (split-string
  3224. org-latex-to-mathml-convert-command))))
  3225. (when (executable-find executable)
  3226. (if (string-match
  3227. "%j" org-latex-to-mathml-convert-command)
  3228. (file-readable-p org-latex-to-mathml-jar-file)
  3229. t))))))
  3230. (defcustom org-format-latex-header "\\documentclass{article}
  3231. \\usepackage[usenames]{color}
  3232. \\usepackage{amsmath}
  3233. \\usepackage[mathscr]{eucal}
  3234. \\pagestyle{empty} % do not remove
  3235. \[PACKAGES]
  3236. \[DEFAULT-PACKAGES]
  3237. % The settings below are copied from fullpage.sty
  3238. \\setlength{\\textwidth}{\\paperwidth}
  3239. \\addtolength{\\textwidth}{-3cm}
  3240. \\setlength{\\oddsidemargin}{1.5cm}
  3241. \\addtolength{\\oddsidemargin}{-2.54cm}
  3242. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3243. \\setlength{\\textheight}{\\paperheight}
  3244. \\addtolength{\\textheight}{-\\headheight}
  3245. \\addtolength{\\textheight}{-\\headsep}
  3246. \\addtolength{\\textheight}{-\\footskip}
  3247. \\addtolength{\\textheight}{-3cm}
  3248. \\setlength{\\topmargin}{1.5cm}
  3249. \\addtolength{\\topmargin}{-2.54cm}"
  3250. "The document header used for processing LaTeX fragments.
  3251. It is imperative that this header make sure that no page number
  3252. appears on the page. The package defined in the variables
  3253. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3254. will either replace the placeholder \"[PACKAGES]\" in this
  3255. header, or they will be appended."
  3256. :group 'org-latex
  3257. :type 'string)
  3258. (defun org-set-packages-alist (var val)
  3259. "Set the packages alist and make sure it has 3 elements per entry."
  3260. (set var (mapcar (lambda (x)
  3261. (if (and (consp x) (= (length x) 2))
  3262. (list (car x) (nth 1 x) t)
  3263. x))
  3264. val)))
  3265. (defun org-get-packages-alist (var)
  3266. "Get the packages alist and make sure it has 3 elements per entry."
  3267. (mapcar (lambda (x)
  3268. (if (and (consp x) (= (length x) 2))
  3269. (list (car x) (nth 1 x) t)
  3270. x))
  3271. (default-value var)))
  3272. (defcustom org-latex-default-packages-alist
  3273. '(("AUTO" "inputenc" t)
  3274. ("T1" "fontenc" t)
  3275. ("" "fixltx2e" nil)
  3276. ("" "graphicx" t)
  3277. ("" "longtable" nil)
  3278. ("" "float" nil)
  3279. ("" "wrapfig" nil)
  3280. ("" "soul" t)
  3281. ("" "textcomp" t)
  3282. ("" "marvosym" t)
  3283. ("" "wasysym" t)
  3284. ("" "latexsym" t)
  3285. ("" "amssymb" t)
  3286. ("" "hyperref" nil)
  3287. "\\tolerance=1000")
  3288. "Alist of default packages to be inserted in the header.
  3289. Change this only if one of the packages here causes an
  3290. incompatibility with another package you are using.
  3291. The packages in this list are needed by one part or another of
  3292. Org mode to function properly:
  3293. - inputenc, fontenc: for basic font and character selection
  3294. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3295. symbols used for interpreting the entities in `org-entities'.
  3296. You can skip some of these packages if you don't use any of the
  3297. symbols in it.
  3298. - graphicx: for including images
  3299. - float, wrapfig: for figure placement
  3300. - longtable: for long tables
  3301. - hyperref: for cross references
  3302. Therefore you should not modify this variable unless you know
  3303. what you are doing. The one reason to change it anyway is that
  3304. you might be loading some other package that conflicts with one
  3305. of the default packages. Each cell is of the format
  3306. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3307. the package also needs to be included when compiling LaTeX
  3308. snippets into images for inclusion into non-LaTeX output."
  3309. :group 'org-latex
  3310. :group 'org-export-latex
  3311. :set 'org-set-packages-alist
  3312. :get 'org-get-packages-alist
  3313. :version "24.1"
  3314. :type '(repeat
  3315. (choice
  3316. (list :tag "options/package pair"
  3317. (string :tag "options")
  3318. (string :tag "package")
  3319. (boolean :tag "Snippet"))
  3320. (string :tag "A line of LaTeX"))))
  3321. (defcustom org-latex-packages-alist nil
  3322. "Alist of packages to be inserted in every LaTeX header.
  3323. These will be inserted after `org-latex-default-packages-alist'.
  3324. Each cell is of the format:
  3325. \(\"options\" \"package\" snippet-flag)
  3326. SNIPPET-FLAG, when t, indicates that this package is also needed
  3327. when turning LaTeX snippets into images for inclusion into
  3328. non-LaTeX output.
  3329. Make sure that you only list packages here which:
  3330. - you want in every file
  3331. - do not conflict with the setup in `org-format-latex-header'.
  3332. - do not conflict with the default packages in
  3333. `org-latex-default-packages-alist'."
  3334. :group 'org-latex
  3335. :group 'org-export-latex
  3336. :set 'org-set-packages-alist
  3337. :get 'org-get-packages-alist
  3338. :type '(repeat
  3339. (choice
  3340. (list :tag "options/package pair"
  3341. (string :tag "options")
  3342. (string :tag "package")
  3343. (boolean :tag "Snippet"))
  3344. (string :tag "A line of LaTeX"))))
  3345. (defgroup org-appearance nil
  3346. "Settings for Org-mode appearance."
  3347. :tag "Org Appearance"
  3348. :group 'org)
  3349. (defcustom org-level-color-stars-only nil
  3350. "Non-nil means fontify only the stars in each headline.
  3351. When nil, the entire headline is fontified.
  3352. Changing it requires restart of `font-lock-mode' to become effective
  3353. also in regions already fontified."
  3354. :group 'org-appearance
  3355. :type 'boolean)
  3356. (defcustom org-hide-leading-stars nil
  3357. "Non-nil means hide the first N-1 stars in a headline.
  3358. This works by using the face `org-hide' for these stars. This
  3359. face is white for a light background, and black for a dark
  3360. background. You may have to customize the face `org-hide' to
  3361. make this work.
  3362. Changing it requires restart of `font-lock-mode' to become effective
  3363. also in regions already fontified.
  3364. You may also set this on a per-file basis by adding one of the following
  3365. lines to the buffer:
  3366. #+STARTUP: hidestars
  3367. #+STARTUP: showstars"
  3368. :group 'org-appearance
  3369. :type 'boolean)
  3370. (defcustom org-hidden-keywords nil
  3371. "List of symbols corresponding to keywords to be hidden the org buffer.
  3372. For example, a value '(title) for this list will make the document's title
  3373. appear in the buffer without the initial #+TITLE: keyword."
  3374. :group 'org-appearance
  3375. :version "24.1"
  3376. :type '(set (const :tag "#+AUTHOR" author)
  3377. (const :tag "#+DATE" date)
  3378. (const :tag "#+EMAIL" email)
  3379. (const :tag "#+TITLE" title)))
  3380. (defcustom org-custom-properties nil
  3381. "List of properties (as strings) with a special meaning.
  3382. The default use of these custom properties is to let the user
  3383. hide them with `org-toggle-custom-properties-visibility'."
  3384. :group 'org-properties
  3385. :group 'org-appearance
  3386. :version "24.3"
  3387. :type '(repeat (string :tag "Property Name")))
  3388. (defcustom org-fontify-done-headline nil
  3389. "Non-nil means change the face of a headline if it is marked DONE.
  3390. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3391. When this is non-nil, the headline after the keyword is set to the
  3392. `org-headline-done' as an additional indication."
  3393. :group 'org-appearance
  3394. :type 'boolean)
  3395. (defcustom org-fontify-emphasized-text t
  3396. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3397. Changing this variable requires a restart of Emacs to take effect."
  3398. :group 'org-appearance
  3399. :type 'boolean)
  3400. (defcustom org-fontify-whole-heading-line nil
  3401. "Non-nil means fontify the whole line for headings.
  3402. This is useful when setting a background color for the
  3403. org-level-* faces."
  3404. :group 'org-appearance
  3405. :type 'boolean)
  3406. (defcustom org-highlight-latex-and-related nil
  3407. "Non-nil means highlight LaTeX related syntax in the buffer.
  3408. When non nil, the value should be a list containing any of the
  3409. following symbols:
  3410. `latex' Highlight LaTeX snippets and environments.
  3411. `script' Highlight subscript and superscript.
  3412. `entities' Highlight entities."
  3413. :group 'org-appearance
  3414. :version "24.4"
  3415. :package-version '(Org . "8.0")
  3416. :type '(choice
  3417. (const :tag "No highlighting" nil)
  3418. (set :greedy t :tag "Highlight"
  3419. (const :tag "LaTeX snippets and environments" latex)
  3420. (const :tag "Subscript and superscript" script)
  3421. (const :tag "Entities" entities))))
  3422. (defcustom org-hide-emphasis-markers nil
  3423. "Non-nil mean font-lock should hide the emphasis marker characters."
  3424. :group 'org-appearance
  3425. :type 'boolean)
  3426. (defcustom org-pretty-entities nil
  3427. "Non-nil means show entities as UTF8 characters.
  3428. When nil, the \\name form remains in the buffer."
  3429. :group 'org-appearance
  3430. :version "24.1"
  3431. :type 'boolean)
  3432. (defcustom org-pretty-entities-include-sub-superscripts t
  3433. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3434. :group 'org-appearance
  3435. :version "24.1"
  3436. :type 'boolean)
  3437. (defvar org-emph-re nil
  3438. "Regular expression for matching emphasis.
  3439. After a match, the match groups contain these elements:
  3440. 0 The match of the full regular expression, including the characters
  3441. before and after the proper match
  3442. 1 The character before the proper match, or empty at beginning of line
  3443. 2 The proper match, including the leading and trailing markers
  3444. 3 The leading marker like * or /, indicating the type of highlighting
  3445. 4 The text between the emphasis markers, not including the markers
  3446. 5 The character after the match, empty at the end of a line")
  3447. (defvar org-verbatim-re nil
  3448. "Regular expression for matching verbatim text.")
  3449. (defvar org-emphasis-regexp-components) ; defined just below
  3450. (defvar org-emphasis-alist) ; defined just below
  3451. (defun org-set-emph-re (var val)
  3452. "Set variable and compute the emphasis regular expression."
  3453. (set var val)
  3454. (when (and (boundp 'org-emphasis-alist)
  3455. (boundp 'org-emphasis-regexp-components)
  3456. org-emphasis-alist org-emphasis-regexp-components)
  3457. (let* ((e org-emphasis-regexp-components)
  3458. (pre (car e))
  3459. (post (nth 1 e))
  3460. (border (nth 2 e))
  3461. (body (nth 3 e))
  3462. (nl (nth 4 e))
  3463. (body1 (concat body "*?"))
  3464. (markers (mapconcat 'car org-emphasis-alist ""))
  3465. (vmarkers (mapconcat
  3466. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3467. org-emphasis-alist "")))
  3468. ;; make sure special characters appear at the right position in the class
  3469. (if (string-match "\\^" markers)
  3470. (setq markers (concat (replace-match "" t t markers) "^")))
  3471. (if (string-match "-" markers)
  3472. (setq markers (concat (replace-match "" t t markers) "-")))
  3473. (if (string-match "\\^" vmarkers)
  3474. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3475. (if (string-match "-" vmarkers)
  3476. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3477. (if (> nl 0)
  3478. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3479. (int-to-string nl) "\\}")))
  3480. ;; Make the regexp
  3481. (setq org-emph-re
  3482. (concat "\\([" pre "]\\|^\\)"
  3483. "\\("
  3484. "\\([" markers "]\\)"
  3485. "\\("
  3486. "[^" border "]\\|"
  3487. "[^" border "]"
  3488. body1
  3489. "[^" border "]"
  3490. "\\)"
  3491. "\\3\\)"
  3492. "\\([" post "]\\|$\\)"))
  3493. (setq org-verbatim-re
  3494. (concat "\\([" pre "]\\|^\\)"
  3495. "\\("
  3496. "\\([" vmarkers "]\\)"
  3497. "\\("
  3498. "[^" border "]\\|"
  3499. "[^" border "]"
  3500. body1
  3501. "[^" border "]"
  3502. "\\)"
  3503. "\\3\\)"
  3504. "\\([" post "]\\|$\\)")))))
  3505. (defcustom org-emphasis-regexp-components
  3506. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3507. "Components used to build the regular expression for emphasis.
  3508. This is a list with five entries. Terminology: In an emphasis string
  3509. like \" *strong word* \", we call the initial space PREMATCH, the final
  3510. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3511. and \"trong wor\" is the body. The different components in this variable
  3512. specify what is allowed/forbidden in each part:
  3513. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3514. post Chars allowed as postmatch. End of line will be allowed too.
  3515. border The chars *forbidden* as border characters.
  3516. body-regexp A regexp like \".\" to match a body character. Don't use
  3517. non-shy groups here, and don't allow newline here.
  3518. newline The maximum number of newlines allowed in an emphasis exp.
  3519. Use customize to modify this, or restart Emacs after changing it."
  3520. :group 'org-appearance
  3521. :set 'org-set-emph-re
  3522. :type '(list
  3523. (sexp :tag "Allowed chars in pre ")
  3524. (sexp :tag "Allowed chars in post ")
  3525. (sexp :tag "Forbidden chars in border ")
  3526. (sexp :tag "Regexp for body ")
  3527. (integer :tag "number of newlines allowed")
  3528. (option (boolean :tag "Please ignore this button"))))
  3529. (defcustom org-emphasis-alist
  3530. `(("*" bold "<b>" "</b>")
  3531. ("/" italic "<i>" "</i>")
  3532. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3533. ("=" org-code "<code>" "</code>" verbatim)
  3534. ("~" org-verbatim "<code>" "</code>" verbatim)
  3535. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3536. "<del>" "</del>")
  3537. )
  3538. "Special syntax for emphasized text.
  3539. Text starting and ending with a special character will be emphasized, for
  3540. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3541. characters, the face to be used by font-lock for highlighting in Org-mode
  3542. Emacs buffers, and the HTML tags to be used for this.
  3543. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3544. Use customize to modify this, or restart Emacs after changing it."
  3545. :group 'org-appearance
  3546. :set 'org-set-emph-re
  3547. :type '(repeat
  3548. (list
  3549. (string :tag "Marker character")
  3550. (choice
  3551. (face :tag "Font-lock-face")
  3552. (plist :tag "Face property list"))
  3553. (string :tag "HTML start tag")
  3554. (string :tag "HTML end tag")
  3555. (option (const verbatim)))))
  3556. (defvar org-protecting-blocks
  3557. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3558. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3559. This is needed for font-lock setup.")
  3560. ;;; Miscellaneous options
  3561. (defgroup org-completion nil
  3562. "Completion in Org-mode."
  3563. :tag "Org Completion"
  3564. :group 'org)
  3565. (defcustom org-completion-use-ido nil
  3566. "Non-nil means use ido completion wherever possible.
  3567. Note that `ido-mode' must be active for this variable to be relevant.
  3568. If you decide to turn this variable on, you might well want to turn off
  3569. `org-outline-path-complete-in-steps'.
  3570. See also `org-completion-use-iswitchb'."
  3571. :group 'org-completion
  3572. :type 'boolean)
  3573. (defcustom org-completion-use-iswitchb nil
  3574. "Non-nil means use iswitchb completion wherever possible.
  3575. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3576. If you decide to turn this variable on, you might well want to turn off
  3577. `org-outline-path-complete-in-steps'.
  3578. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3579. :group 'org-completion
  3580. :type 'boolean)
  3581. (defcustom org-completion-fallback-command 'hippie-expand
  3582. "The expansion command called by \\[pcomplete] in normal context.
  3583. Normal means, no org-mode-specific context."
  3584. :group 'org-completion
  3585. :type 'function)
  3586. ;;; Functions and variables from their packages
  3587. ;; Declared here to avoid compiler warnings
  3588. ;; XEmacs only
  3589. (defvar outline-mode-menu-heading)
  3590. (defvar outline-mode-menu-show)
  3591. (defvar outline-mode-menu-hide)
  3592. (defvar zmacs-regions) ; XEmacs regions
  3593. ;; Emacs only
  3594. (defvar mark-active)
  3595. ;; Various packages
  3596. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3597. (declare-function calendar-forward-day "cal-move" (arg))
  3598. (declare-function calendar-goto-date "cal-move" (date))
  3599. (declare-function calendar-goto-today "cal-move" ())
  3600. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3601. (defvar calc-embedded-close-formula)
  3602. (defvar calc-embedded-open-formula)
  3603. (declare-function cdlatex-tab "ext:cdlatex" ())
  3604. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3605. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3606. (defvar font-lock-unfontify-region-function)
  3607. (declare-function iswitchb-read-buffer "iswitchb"
  3608. (prompt &optional default require-match start matches-set))
  3609. (defvar iswitchb-temp-buflist)
  3610. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3611. (defvar org-agenda-tags-todo-honor-ignore-options)
  3612. (declare-function org-agenda-skip "org-agenda" ())
  3613. (declare-function
  3614. org-agenda-format-item "org-agenda"
  3615. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3616. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3617. (declare-function org-agenda-change-all-lines "org-agenda"
  3618. (newhead hdmarker &optional fixface just-this))
  3619. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3620. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3621. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3622. (beg end))
  3623. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3624. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3625. "org-agenda" (&optional end))
  3626. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3627. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3628. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3629. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3630. (declare-function org-indent-mode "org-indent" (&optional arg))
  3631. (declare-function parse-time-string "parse-time" (string))
  3632. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3633. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3634. (defvar remember-data-file)
  3635. (defvar texmathp-why)
  3636. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3637. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3638. (defvar org-latex-regexps)
  3639. ;;; Autoload and prepare some org modules
  3640. ;; Some table stuff that needs to be defined here, because it is used
  3641. ;; by the functions setting up org-mode or checking for table context.
  3642. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3643. "Detect an org-type or table-type table.")
  3644. (defconst org-table-line-regexp "^[ \t]*|"
  3645. "Detect an org-type table line.")
  3646. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3647. "Detect an org-type table line.")
  3648. (defconst org-table-hline-regexp "^[ \t]*|-"
  3649. "Detect an org-type table hline.")
  3650. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3651. "Detect a table-type table hline.")
  3652. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3653. "Detect the first line outside a table when searching from within it.
  3654. This works for both table types.")
  3655. ;; Autoload the functions in org-table.el that are needed by functions here.
  3656. (eval-and-compile
  3657. (org-autoload "org-table"
  3658. '(org-table-begin org-table-blank-field org-table-end)))
  3659. ;;;###autoload
  3660. (defun turn-on-orgtbl ()
  3661. "Unconditionally turn on `orgtbl-mode'."
  3662. (require 'org-table)
  3663. (orgtbl-mode 1))
  3664. (defun org-at-table-p (&optional table-type)
  3665. "Return t if the cursor is inside an org-type table.
  3666. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3667. (if org-enable-table-editor
  3668. (save-excursion
  3669. (beginning-of-line 1)
  3670. (looking-at (if table-type org-table-any-line-regexp
  3671. org-table-line-regexp)))
  3672. nil))
  3673. (defsubst org-table-p () (org-at-table-p))
  3674. (defun org-at-table.el-p ()
  3675. "Return t if and only if we are at a table.el table."
  3676. (and (org-at-table-p 'any)
  3677. (save-excursion
  3678. (goto-char (org-table-begin 'any))
  3679. (looking-at org-table1-hline-regexp))))
  3680. (defun org-table-recognize-table.el ()
  3681. "If there is a table.el table nearby, recognize it and move into it."
  3682. (if org-table-tab-recognizes-table.el
  3683. (if (org-at-table.el-p)
  3684. (progn
  3685. (beginning-of-line 1)
  3686. (if (looking-at org-table-dataline-regexp)
  3687. nil
  3688. (if (looking-at org-table1-hline-regexp)
  3689. (progn
  3690. (beginning-of-line 2)
  3691. (if (looking-at org-table-any-border-regexp)
  3692. (beginning-of-line -1)))))
  3693. (if (re-search-forward "|" (org-table-end t) t)
  3694. (progn
  3695. (require 'table)
  3696. (if (table--at-cell-p (point))
  3697. t
  3698. (message "recognizing table.el table...")
  3699. (table-recognize-table)
  3700. (message "recognizing table.el table...done")))
  3701. (error "This should not happen"))
  3702. t)
  3703. nil)
  3704. nil))
  3705. (defun org-at-table-hline-p ()
  3706. "Return t if the cursor is inside a hline in a table."
  3707. (if org-enable-table-editor
  3708. (save-excursion
  3709. (beginning-of-line 1)
  3710. (looking-at org-table-hline-regexp))
  3711. nil))
  3712. (defvar org-table-clean-did-remove-column nil)
  3713. (defun org-table-map-tables (function &optional quietly)
  3714. "Apply FUNCTION to the start of all tables in the buffer."
  3715. (save-excursion
  3716. (save-restriction
  3717. (widen)
  3718. (goto-char (point-min))
  3719. (while (re-search-forward org-table-any-line-regexp nil t)
  3720. (unless quietly
  3721. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3722. (beginning-of-line 1)
  3723. (when (and (looking-at org-table-line-regexp)
  3724. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3725. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3726. (save-excursion (funcall function))
  3727. (or (looking-at org-table-line-regexp)
  3728. (forward-char 1)))
  3729. (re-search-forward org-table-any-border-regexp nil 1))))
  3730. (unless quietly (message "Mapping tables: done")))
  3731. ;; Declare and autoload functions from ox.el and al.
  3732. (declare-function org-export-get-environment "ox"
  3733. (&optional backend subtreep ext-plist))
  3734. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  3735. ;; Declare and autoload functions from org-agenda.el
  3736. (eval-and-compile
  3737. (org-autoload "org-agenda"
  3738. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3739. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3740. (declare-function org-clock-update-mode-line "org-clock" ())
  3741. (declare-function org-resolve-clocks "org-clock"
  3742. (&optional also-non-dangling-p prompt last-valid))
  3743. (defvar org-clock-start-time)
  3744. (defvar org-clock-marker (make-marker)
  3745. "Marker recording the last clock-in.")
  3746. (defvar org-clock-hd-marker (make-marker)
  3747. "Marker recording the last clock-in, but the headline position.")
  3748. (defvar org-clock-heading ""
  3749. "The heading of the current clock entry.")
  3750. (defun org-clock-is-active ()
  3751. "Return non-nil if clock is currently running.
  3752. The return value is actually the clock marker."
  3753. (marker-buffer org-clock-marker))
  3754. (eval-and-compile
  3755. (org-autoload "org-clock" '(org-clock-remove-overlays
  3756. org-clock-update-time-maybe
  3757. org-clocktable-shift)))
  3758. (defun org-check-running-clock ()
  3759. "Check if the current buffer contains the running clock.
  3760. If yes, offer to stop it and to save the buffer with the changes."
  3761. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3762. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3763. (buffer-name))))
  3764. (org-clock-out)
  3765. (when (y-or-n-p "Save changed buffer?")
  3766. (save-buffer))))
  3767. (defun org-clocktable-try-shift (dir n)
  3768. "Check if this line starts a clock table, if yes, shift the time block."
  3769. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3770. (org-clocktable-shift dir n)))
  3771. ;;;###autoload
  3772. (defun org-clock-persistence-insinuate ()
  3773. "Set up hooks for clock persistence."
  3774. (require 'org-clock)
  3775. (add-hook 'org-mode-hook 'org-clock-load)
  3776. (add-hook 'kill-emacs-hook 'org-clock-save))
  3777. ;; Define the variable already here, to make sure we have it.
  3778. (defvar org-indent-mode nil
  3779. "Non-nil if Org-Indent mode is enabled.
  3780. Use the command `org-indent-mode' to change this variable.")
  3781. ;; Autoload archiving code
  3782. ;; The stuff that is needed for cycling and tags has to be defined here.
  3783. (defgroup org-archive nil
  3784. "Options concerning archiving in Org-mode."
  3785. :tag "Org Archive"
  3786. :group 'org-structure)
  3787. (defcustom org-archive-location "%s_archive::"
  3788. "The location where subtrees should be archived.
  3789. The value of this variable is a string, consisting of two parts,
  3790. separated by a double-colon. The first part is a filename and
  3791. the second part is a headline.
  3792. When the filename is omitted, archiving happens in the same file.
  3793. %s in the filename will be replaced by the current file
  3794. name (without the directory part). Archiving to a different file
  3795. is useful to keep archived entries from contributing to the
  3796. Org-mode Agenda.
  3797. The archived entries will be filed as subtrees of the specified
  3798. headline. When the headline is omitted, the subtrees are simply
  3799. filed away at the end of the file, as top-level entries. Also in
  3800. the heading you can use %s to represent the file name, this can be
  3801. useful when using the same archive for a number of different files.
  3802. Here are a few examples:
  3803. \"%s_archive::\"
  3804. If the current file is Projects.org, archive in file
  3805. Projects.org_archive, as top-level trees. This is the default.
  3806. \"::* Archived Tasks\"
  3807. Archive in the current file, under the top-level headline
  3808. \"* Archived Tasks\".
  3809. \"~/org/archive.org::\"
  3810. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3811. \"~/org/archive.org::* From %s\"
  3812. Archive in file ~/org/archive.org (absolute path), under headlines
  3813. \"From FILENAME\" where file name is the current file name.
  3814. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3815. The \"datetree/\" string is special, signifying to archive
  3816. items to the datetree. Items are placed in either the CLOSED
  3817. date of the item, or the current date if there is no CLOSED date.
  3818. The heading will be a subentry to the current date. There doesn't
  3819. need to be a heading, but there always needs to be a slash after
  3820. datetree. For example, to store archived items directly in the
  3821. datetree, use \"~/org/datetree.org::datetree/\".
  3822. \"basement::** Finished Tasks\"
  3823. Archive in file ./basement (relative path), as level 3 trees
  3824. below the level 2 heading \"** Finished Tasks\".
  3825. You may set this option on a per-file basis by adding to the buffer a
  3826. line like
  3827. #+ARCHIVE: basement::** Finished Tasks
  3828. You may also define it locally for a subtree by setting an ARCHIVE property
  3829. in the entry. If such a property is found in an entry, or anywhere up
  3830. the hierarchy, it will be used."
  3831. :group 'org-archive
  3832. :type 'string)
  3833. (defcustom org-archive-tag "ARCHIVE"
  3834. "The tag that marks a subtree as archived.
  3835. An archived subtree does not open during visibility cycling, and does
  3836. not contribute to the agenda listings.
  3837. After changing this, font-lock must be restarted in the relevant buffers to
  3838. get the proper fontification."
  3839. :group 'org-archive
  3840. :group 'org-keywords
  3841. :type 'string)
  3842. (defcustom org-agenda-skip-archived-trees t
  3843. "Non-nil means the agenda will skip any items located in archived trees.
  3844. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3845. variable is no longer recommended, you should leave it at the value t.
  3846. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3847. :group 'org-archive
  3848. :group 'org-agenda-skip
  3849. :type 'boolean)
  3850. (defcustom org-columns-skip-archived-trees t
  3851. "Non-nil means ignore archived trees when creating column view."
  3852. :group 'org-archive
  3853. :group 'org-properties
  3854. :type 'boolean)
  3855. (defcustom org-cycle-open-archived-trees nil
  3856. "Non-nil means `org-cycle' will open archived trees.
  3857. An archived tree is a tree marked with the tag ARCHIVE.
  3858. When nil, archived trees will stay folded. You can still open them with
  3859. normal outline commands like `show-all', but not with the cycling commands."
  3860. :group 'org-archive
  3861. :group 'org-cycle
  3862. :type 'boolean)
  3863. (defcustom org-sparse-tree-open-archived-trees nil
  3864. "Non-nil means sparse tree construction shows matches in archived trees.
  3865. When nil, matches in these trees are highlighted, but the trees are kept in
  3866. collapsed state."
  3867. :group 'org-archive
  3868. :group 'org-sparse-trees
  3869. :type 'boolean)
  3870. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3871. "The default date type when building a sparse tree.
  3872. When this is nil, a date is a scheduled or a deadline timestamp.
  3873. Otherwise, these types are allowed:
  3874. all: all timestamps
  3875. active: only active timestamps (<...>)
  3876. inactive: only inactive timestamps (<...)
  3877. scheduled: only scheduled timestamps
  3878. deadline: only deadline timestamps"
  3879. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3880. (const :tag "All timestamps" all)
  3881. (const :tag "Only active timestamps" active)
  3882. (const :tag "Only inactive timestamps" inactive)
  3883. (const :tag "Only scheduled timestamps" scheduled)
  3884. (const :tag "Only deadline timestamps" deadline))
  3885. :version "24.3"
  3886. :group 'org-sparse-trees)
  3887. (defun org-cycle-hide-archived-subtrees (state)
  3888. "Re-hide all archived subtrees after a visibility state change."
  3889. (when (and (not org-cycle-open-archived-trees)
  3890. (not (memq state '(overview folded))))
  3891. (save-excursion
  3892. (let* ((globalp (memq state '(contents all)))
  3893. (beg (if globalp (point-min) (point)))
  3894. (end (if globalp (point-max) (org-end-of-subtree t))))
  3895. (org-hide-archived-subtrees beg end)
  3896. (goto-char beg)
  3897. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3898. (message "%s" (substitute-command-keys
  3899. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3900. (defun org-force-cycle-archived ()
  3901. "Cycle subtree even if it is archived."
  3902. (interactive)
  3903. (setq this-command 'org-cycle)
  3904. (let ((org-cycle-open-archived-trees t))
  3905. (call-interactively 'org-cycle)))
  3906. (defun org-hide-archived-subtrees (beg end)
  3907. "Re-hide all archived subtrees after a visibility state change."
  3908. (save-excursion
  3909. (let* ((re (concat ":" org-archive-tag ":")))
  3910. (goto-char beg)
  3911. (while (re-search-forward re end t)
  3912. (when (org-at-heading-p)
  3913. (org-flag-subtree t)
  3914. (org-end-of-subtree t))))))
  3915. (declare-function outline-end-of-heading "outline" ())
  3916. (declare-function outline-flag-region "outline" (from to flag))
  3917. (defun org-flag-subtree (flag)
  3918. (save-excursion
  3919. (org-back-to-heading t)
  3920. (outline-end-of-heading)
  3921. (outline-flag-region (point)
  3922. (progn (org-end-of-subtree t) (point))
  3923. flag)))
  3924. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3925. (eval-and-compile
  3926. (org-autoload "org-archive"
  3927. '(org-add-archive-files)))
  3928. ;; Autoload Column View Code
  3929. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3930. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3931. (declare-function org-columns-compute "org-colview" (property))
  3932. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3933. '(org-columns-number-to-string
  3934. org-columns-get-format-and-top-level
  3935. org-columns-compute
  3936. org-columns-remove-overlays))
  3937. ;; Autoload ID code
  3938. (declare-function org-id-store-link "org-id")
  3939. (declare-function org-id-locations-load "org-id")
  3940. (declare-function org-id-locations-save "org-id")
  3941. (defvar org-id-track-globally)
  3942. (org-autoload "org-id"
  3943. '(org-id-new
  3944. org-id-copy
  3945. org-id-get-with-outline-path-completion
  3946. org-id-get-with-outline-drilling))
  3947. ;;; Variables for pre-computed regular expressions, all buffer local
  3948. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3949. "Matches first line of a hidden block.")
  3950. (make-variable-buffer-local 'org-drawer-regexp)
  3951. (defvar org-todo-regexp nil
  3952. "Matches any of the TODO state keywords.")
  3953. (make-variable-buffer-local 'org-todo-regexp)
  3954. (defvar org-not-done-regexp nil
  3955. "Matches any of the TODO state keywords except the last one.")
  3956. (make-variable-buffer-local 'org-not-done-regexp)
  3957. (defvar org-not-done-heading-regexp nil
  3958. "Matches a TODO headline that is not done.")
  3959. (make-variable-buffer-local 'org-not-done-regexp)
  3960. (defvar org-todo-line-regexp nil
  3961. "Matches a headline and puts TODO state into group 2 if present.")
  3962. (make-variable-buffer-local 'org-todo-line-regexp)
  3963. (defvar org-complex-heading-regexp nil
  3964. "Matches a headline and puts everything into groups:
  3965. group 1: the stars
  3966. group 2: The todo keyword, maybe
  3967. group 3: Priority cookie
  3968. group 4: True headline
  3969. group 5: Tags")
  3970. (make-variable-buffer-local 'org-complex-heading-regexp)
  3971. (defvar org-complex-heading-regexp-format nil
  3972. "Printf format to make regexp to match an exact headline.
  3973. This regexp will match the headline of any node which has the
  3974. exact headline text that is put into the format, but may have any
  3975. TODO state, priority and tags.")
  3976. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3977. (defvar org-todo-line-tags-regexp nil
  3978. "Matches a headline and puts TODO state into group 2 if present.
  3979. Also put tags into group 4 if tags are present.")
  3980. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3981. (defvar org-ds-keyword-length 12
  3982. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3983. (make-variable-buffer-local 'org-ds-keyword-length)
  3984. (defvar org-deadline-regexp nil
  3985. "Matches the DEADLINE keyword.")
  3986. (make-variable-buffer-local 'org-deadline-regexp)
  3987. (defvar org-deadline-time-regexp nil
  3988. "Matches the DEADLINE keyword together with a time stamp.")
  3989. (make-variable-buffer-local 'org-deadline-time-regexp)
  3990. (defvar org-deadline-time-hour-regexp nil
  3991. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  3992. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  3993. (defvar org-deadline-line-regexp nil
  3994. "Matches the DEADLINE keyword and the rest of the line.")
  3995. (make-variable-buffer-local 'org-deadline-line-regexp)
  3996. (defvar org-scheduled-regexp nil
  3997. "Matches the SCHEDULED keyword.")
  3998. (make-variable-buffer-local 'org-scheduled-regexp)
  3999. (defvar org-scheduled-time-regexp nil
  4000. "Matches the SCHEDULED keyword together with a time stamp.")
  4001. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4002. (defvar org-scheduled-time-hour-regexp nil
  4003. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4004. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4005. (defvar org-closed-time-regexp nil
  4006. "Matches the CLOSED keyword together with a time stamp.")
  4007. (make-variable-buffer-local 'org-closed-time-regexp)
  4008. (defvar org-keyword-time-regexp nil
  4009. "Matches any of the 4 keywords, together with the time stamp.")
  4010. (make-variable-buffer-local 'org-keyword-time-regexp)
  4011. (defvar org-keyword-time-not-clock-regexp nil
  4012. "Matches any of the 3 keywords, together with the time stamp.")
  4013. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4014. (defvar org-maybe-keyword-time-regexp nil
  4015. "Matches a timestamp, possibly preceded by a keyword.")
  4016. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4017. (defvar org-all-time-keywords nil
  4018. "List of time keywords.")
  4019. (make-variable-buffer-local 'org-all-time-keywords)
  4020. (defconst org-plain-time-of-day-regexp
  4021. (concat
  4022. "\\(\\<[012]?[0-9]"
  4023. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4024. "\\(--?"
  4025. "\\(\\<[012]?[0-9]"
  4026. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4027. "\\)?")
  4028. "Regular expression to match a plain time or time range.
  4029. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4030. groups carry important information:
  4031. 0 the full match
  4032. 1 the first time, range or not
  4033. 8 the second time, if it is a range.")
  4034. (defconst org-plain-time-extension-regexp
  4035. (concat
  4036. "\\(\\<[012]?[0-9]"
  4037. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4038. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4039. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4040. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4041. groups carry important information:
  4042. 0 the full match
  4043. 7 hours of duration
  4044. 9 minutes of duration")
  4045. (defconst org-stamp-time-of-day-regexp
  4046. (concat
  4047. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4048. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4049. "\\(--?"
  4050. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4051. "Regular expression to match a timestamp time or time range.
  4052. After a match, the following groups carry important information:
  4053. 0 the full match
  4054. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4055. 2 the first time, range or not
  4056. 4 the second time, if it is a range.")
  4057. (defconst org-startup-options
  4058. '(("fold" org-startup-folded t)
  4059. ("overview" org-startup-folded t)
  4060. ("nofold" org-startup-folded nil)
  4061. ("showall" org-startup-folded nil)
  4062. ("showeverything" org-startup-folded showeverything)
  4063. ("content" org-startup-folded content)
  4064. ("indent" org-startup-indented t)
  4065. ("noindent" org-startup-indented nil)
  4066. ("hidestars" org-hide-leading-stars t)
  4067. ("showstars" org-hide-leading-stars nil)
  4068. ("odd" org-odd-levels-only t)
  4069. ("oddeven" org-odd-levels-only nil)
  4070. ("align" org-startup-align-all-tables t)
  4071. ("noalign" org-startup-align-all-tables nil)
  4072. ("inlineimages" org-startup-with-inline-images t)
  4073. ("noinlineimages" org-startup-with-inline-images nil)
  4074. ("latexpreview" org-startup-with-latex-preview t)
  4075. ("nolatexpreview" org-startup-with-latex-preview nil)
  4076. ("customtime" org-display-custom-times t)
  4077. ("logdone" org-log-done time)
  4078. ("lognotedone" org-log-done note)
  4079. ("nologdone" org-log-done nil)
  4080. ("lognoteclock-out" org-log-note-clock-out t)
  4081. ("nolognoteclock-out" org-log-note-clock-out nil)
  4082. ("logrepeat" org-log-repeat state)
  4083. ("lognoterepeat" org-log-repeat note)
  4084. ("logdrawer" org-log-into-drawer t)
  4085. ("nologdrawer" org-log-into-drawer nil)
  4086. ("logstatesreversed" org-log-states-order-reversed t)
  4087. ("nologstatesreversed" org-log-states-order-reversed nil)
  4088. ("nologrepeat" org-log-repeat nil)
  4089. ("logreschedule" org-log-reschedule time)
  4090. ("lognotereschedule" org-log-reschedule note)
  4091. ("nologreschedule" org-log-reschedule nil)
  4092. ("logredeadline" org-log-redeadline time)
  4093. ("lognoteredeadline" org-log-redeadline note)
  4094. ("nologredeadline" org-log-redeadline nil)
  4095. ("logrefile" org-log-refile time)
  4096. ("lognoterefile" org-log-refile note)
  4097. ("nologrefile" org-log-refile nil)
  4098. ("fninline" org-footnote-define-inline t)
  4099. ("nofninline" org-footnote-define-inline nil)
  4100. ("fnlocal" org-footnote-section nil)
  4101. ("fnauto" org-footnote-auto-label t)
  4102. ("fnprompt" org-footnote-auto-label nil)
  4103. ("fnconfirm" org-footnote-auto-label confirm)
  4104. ("fnplain" org-footnote-auto-label plain)
  4105. ("fnadjust" org-footnote-auto-adjust t)
  4106. ("nofnadjust" org-footnote-auto-adjust nil)
  4107. ("constcgs" constants-unit-system cgs)
  4108. ("constSI" constants-unit-system SI)
  4109. ("noptag" org-tag-persistent-alist nil)
  4110. ("hideblocks" org-hide-block-startup t)
  4111. ("nohideblocks" org-hide-block-startup nil)
  4112. ("beamer" org-startup-with-beamer-mode t)
  4113. ("entitiespretty" org-pretty-entities t)
  4114. ("entitiesplain" org-pretty-entities nil))
  4115. "Variable associated with STARTUP options for org-mode.
  4116. Each element is a list of three items: the startup options (as written
  4117. in the #+STARTUP line), the corresponding variable, and the value to set
  4118. this variable to if the option is found. An optional forth element PUSH
  4119. means to push this value onto the list in the variable.")
  4120. (defun org-update-property-plist (key val props)
  4121. "Update PROPS with KEY and VAL."
  4122. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4123. (key (if appending (substring key 0 (- (length key) 1)) key))
  4124. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4125. (previous (cdr (assoc key props))))
  4126. (if appending
  4127. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4128. (cons (cons key val) remainder))))
  4129. (defconst org-block-regexp
  4130. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4131. "Regular expression for hiding blocks.")
  4132. (defconst org-heading-keyword-regexp-format
  4133. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4134. "Printf format for a regexp matching a headline with some keyword.
  4135. This regexp will match the headline of any node which has the
  4136. exact keyword that is put into the format. The keyword isn't in
  4137. any group by default, but the stars and the body are.")
  4138. (defconst org-heading-keyword-maybe-regexp-format
  4139. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4140. "Printf format for a regexp matching a headline, possibly with some keyword.
  4141. This regexp can match any headline with the specified keyword, or
  4142. without a keyword. The keyword isn't in any group by default,
  4143. but the stars and the body are.")
  4144. (defun org-set-regexps-and-options ()
  4145. "Precompute regular expressions for current buffer."
  4146. (when (derived-mode-p 'org-mode)
  4147. (org-set-local 'org-todo-kwd-alist nil)
  4148. (org-set-local 'org-todo-key-alist nil)
  4149. (org-set-local 'org-todo-key-trigger nil)
  4150. (org-set-local 'org-todo-keywords-1 nil)
  4151. (org-set-local 'org-done-keywords nil)
  4152. (org-set-local 'org-todo-heads nil)
  4153. (org-set-local 'org-todo-sets nil)
  4154. (org-set-local 'org-todo-log-states nil)
  4155. (org-set-local 'org-file-properties nil)
  4156. (org-set-local 'org-file-tags nil)
  4157. (let ((re (org-make-options-regexp
  4158. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE" "FILETAGS"
  4159. "TAGS" "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4160. "SETUPFILE" "OPTIONS")
  4161. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4162. (splitre "[ \t]+")
  4163. (scripts org-use-sub-superscripts)
  4164. kwds kws0 kwsa key log value cat arch tags const links hw dws
  4165. tail sep kws1 prio props ftags drawers ext-setup-or-nil setup-contents
  4166. (start 0))
  4167. (save-excursion
  4168. (save-restriction
  4169. (widen)
  4170. (goto-char (point-min))
  4171. (while (or (and ext-setup-or-nil
  4172. (string-match re ext-setup-or-nil start)
  4173. (setq start (match-end 0)))
  4174. (and (setq ext-setup-or-nil nil start 0)
  4175. (re-search-forward re nil t)))
  4176. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4177. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4178. (if (stringp value) (setq value (org-trim value)))
  4179. (cond
  4180. ((equal key "CATEGORY")
  4181. (setq cat value))
  4182. ((member key '("SEQ_TODO" "TODO"))
  4183. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4184. ((equal key "TYP_TODO")
  4185. (push (cons 'type (org-split-string value splitre)) kwds))
  4186. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4187. ;; general TODO-like setup
  4188. (push (cons (intern (downcase (match-string 1 key)))
  4189. (org-split-string value splitre)) kwds))
  4190. ((equal key "TAGS")
  4191. (setq tags (append tags (if tags '("\\n") nil)
  4192. (org-split-string value splitre))))
  4193. ((equal key "COLUMNS")
  4194. (org-set-local 'org-columns-default-format value))
  4195. ((equal key "LINK")
  4196. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4197. (push (cons (match-string 1 value)
  4198. (org-trim (match-string 2 value)))
  4199. links)))
  4200. ((equal key "PRIORITIES")
  4201. (setq prio (org-split-string value " +")))
  4202. ((equal key "PROPERTY")
  4203. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4204. (setq props (org-update-property-plist (match-string 1 value)
  4205. (match-string 2 value)
  4206. props))))
  4207. ((equal key "FILETAGS")
  4208. (when (string-match "\\S-" value)
  4209. (setq ftags
  4210. (append
  4211. ftags
  4212. (apply 'append
  4213. (mapcar (lambda (x) (org-split-string x ":"))
  4214. (org-split-string value)))))))
  4215. ((equal key "DRAWERS")
  4216. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4217. ((equal key "CONSTANTS")
  4218. (org-table-set-constants))
  4219. ((equal key "STARTUP")
  4220. (let ((opts (org-split-string value splitre))
  4221. l var val)
  4222. (while (setq l (pop opts))
  4223. (when (setq l (assoc l org-startup-options))
  4224. (setq var (nth 1 l) val (nth 2 l))
  4225. (if (not (nth 3 l))
  4226. (set (make-local-variable var) val)
  4227. (if (not (listp (symbol-value var)))
  4228. (set (make-local-variable var) nil))
  4229. (set (make-local-variable var) (symbol-value var))
  4230. (add-to-list var val))))))
  4231. ((equal key "ARCHIVE")
  4232. (setq arch value)
  4233. (remove-text-properties 0 (length arch)
  4234. '(face t fontified t) arch))
  4235. ((equal key "OPTIONS")
  4236. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4237. (setq scripts (read (match-string 2 value)))))
  4238. ((equal key "SETUPFILE")
  4239. (setq setup-contents (org-file-contents
  4240. (expand-file-name
  4241. (org-remove-double-quotes value))
  4242. 'noerror))
  4243. (if (not ext-setup-or-nil)
  4244. (setq ext-setup-or-nil setup-contents start 0)
  4245. (setq ext-setup-or-nil
  4246. (concat (substring ext-setup-or-nil 0 start)
  4247. "\n" setup-contents "\n"
  4248. (substring ext-setup-or-nil start)))))))
  4249. ;; search for property blocks
  4250. (goto-char (point-min))
  4251. (while (re-search-forward org-block-regexp nil t)
  4252. (when (equal "PROPERTY" (upcase (match-string 1)))
  4253. (setq value (replace-regexp-in-string
  4254. "[\n\r]" " " (match-string 4)))
  4255. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4256. (setq props (org-update-property-plist (match-string 1 value)
  4257. (match-string 2 value)
  4258. props)))))))
  4259. (org-set-local 'org-use-sub-superscripts scripts)
  4260. (when cat
  4261. (org-set-local 'org-category (intern cat))
  4262. (push (cons "CATEGORY" cat) props))
  4263. (when prio
  4264. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4265. (setq prio (mapcar 'string-to-char prio))
  4266. (org-set-local 'org-highest-priority (nth 0 prio))
  4267. (org-set-local 'org-lowest-priority (nth 1 prio))
  4268. (org-set-local 'org-default-priority (nth 2 prio)))
  4269. (and props (org-set-local 'org-file-properties (nreverse props)))
  4270. (and ftags (org-set-local 'org-file-tags
  4271. (mapcar 'org-add-prop-inherited ftags)))
  4272. (and drawers (org-set-local 'org-drawers drawers))
  4273. (and arch (org-set-local 'org-archive-location arch))
  4274. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4275. ;; Process the TODO keywords
  4276. (unless kwds
  4277. ;; Use the global values as if they had been given locally.
  4278. (setq kwds (default-value 'org-todo-keywords))
  4279. (if (stringp (car kwds))
  4280. (setq kwds (list (cons org-todo-interpretation
  4281. (default-value 'org-todo-keywords)))))
  4282. (setq kwds (reverse kwds)))
  4283. (setq kwds (nreverse kwds))
  4284. (let (inter kws kw)
  4285. (while (setq kws (pop kwds))
  4286. (let ((kws (or
  4287. (run-hook-with-args-until-success
  4288. 'org-todo-setup-filter-hook kws)
  4289. kws)))
  4290. (setq inter (pop kws) sep (member "|" kws)
  4291. kws0 (delete "|" (copy-sequence kws))
  4292. kwsa nil
  4293. kws1 (mapcar
  4294. (lambda (x)
  4295. ;; 1 2
  4296. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4297. (progn
  4298. (setq kw (match-string 1 x)
  4299. key (and (match-end 2) (match-string 2 x))
  4300. log (org-extract-log-state-settings x))
  4301. (push (cons kw (and key (string-to-char key))) kwsa)
  4302. (and log (push log org-todo-log-states))
  4303. kw)
  4304. (error "Invalid TODO keyword %s" x)))
  4305. kws0)
  4306. kwsa (if kwsa (append '((:startgroup))
  4307. (nreverse kwsa)
  4308. '((:endgroup))))
  4309. hw (car kws1)
  4310. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4311. tail (list inter hw (car dws) (org-last dws))))
  4312. (add-to-list 'org-todo-heads hw 'append)
  4313. (push kws1 org-todo-sets)
  4314. (setq org-done-keywords (append org-done-keywords dws nil))
  4315. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4316. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4317. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4318. (setq org-todo-sets (nreverse org-todo-sets)
  4319. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4320. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4321. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4322. ;; Process the tags.
  4323. (when tags
  4324. (let (e tgs)
  4325. (while (setq e (pop tags))
  4326. (cond
  4327. ((equal e "{") (push '(:startgroup) tgs))
  4328. ((equal e "}") (push '(:endgroup) tgs))
  4329. ((equal e "\\n") (push '(:newline) tgs))
  4330. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4331. (push (cons (match-string 1 e)
  4332. (string-to-char (match-string 2 e)))
  4333. tgs))
  4334. (t (push (list e) tgs))))
  4335. (org-set-local 'org-tag-alist nil)
  4336. (while (setq e (pop tgs))
  4337. (or (and (stringp (car e))
  4338. (assoc (car e) org-tag-alist))
  4339. (push e org-tag-alist)))))
  4340. ;; Compute the regular expressions and other local variables.
  4341. ;; Using `org-outline-regexp-bol' would complicate them much,
  4342. ;; because of the fixed white space at the end of that string.
  4343. (if (not org-done-keywords)
  4344. (setq org-done-keywords (and org-todo-keywords-1
  4345. (list (org-last org-todo-keywords-1)))))
  4346. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4347. (length org-scheduled-string)
  4348. (length org-clock-string)
  4349. (length org-closed-string)))
  4350. org-drawer-regexp
  4351. (concat "^[ \t]*:\\("
  4352. (mapconcat 'regexp-quote org-drawers "\\|")
  4353. "\\):[ \t]*$")
  4354. org-not-done-keywords
  4355. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4356. org-todo-regexp
  4357. (concat "\\("
  4358. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4359. "\\)")
  4360. org-not-done-regexp
  4361. (concat "\\("
  4362. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4363. "\\)")
  4364. org-not-done-heading-regexp
  4365. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4366. org-todo-line-regexp
  4367. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4368. org-complex-heading-regexp
  4369. (concat "^\\(\\*+\\)"
  4370. "\\(?: +" org-todo-regexp "\\)?"
  4371. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4372. "\\(?: +\\(.*?\\)\\)??"
  4373. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4374. "[ \t]*$")
  4375. org-complex-heading-regexp-format
  4376. (concat "^\\(\\*+\\)"
  4377. "\\(?: +" org-todo-regexp "\\)?"
  4378. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4379. "\\(?: +"
  4380. ;; Stats cookies can be stuck to body.
  4381. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4382. "\\(%s\\)"
  4383. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4384. "\\)"
  4385. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4386. "[ \t]*$")
  4387. org-todo-line-tags-regexp
  4388. (concat "^\\(\\*+\\)"
  4389. "\\(?: +" org-todo-regexp "\\)?"
  4390. "\\(?: +\\(.*?\\)\\)??"
  4391. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4392. "[ \t]*$")
  4393. org-deadline-regexp (concat "\\<" org-deadline-string)
  4394. org-deadline-time-regexp
  4395. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4396. org-deadline-time-hour-regexp
  4397. (concat "\\<" org-deadline-string
  4398. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4399. org-deadline-line-regexp
  4400. (concat "\\<\\(" org-deadline-string "\\).*")
  4401. org-scheduled-regexp
  4402. (concat "\\<" org-scheduled-string)
  4403. org-scheduled-time-regexp
  4404. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4405. org-scheduled-time-hour-regexp
  4406. (concat "\\<" org-scheduled-string
  4407. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4408. org-closed-time-regexp
  4409. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4410. org-keyword-time-regexp
  4411. (concat "\\<\\(" org-scheduled-string
  4412. "\\|" org-deadline-string
  4413. "\\|" org-closed-string
  4414. "\\|" org-clock-string "\\)"
  4415. " *[[<]\\([^]>]+\\)[]>]")
  4416. org-keyword-time-not-clock-regexp
  4417. (concat "\\<\\(" org-scheduled-string
  4418. "\\|" org-deadline-string
  4419. "\\|" org-closed-string
  4420. "\\)"
  4421. " *[[<]\\([^]>]+\\)[]>]")
  4422. org-maybe-keyword-time-regexp
  4423. (concat "\\(\\<\\(" org-scheduled-string
  4424. "\\|" org-deadline-string
  4425. "\\|" org-closed-string
  4426. "\\|" org-clock-string "\\)\\)?"
  4427. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4428. org-all-time-keywords
  4429. (mapcar (lambda (w) (substring w 0 -1))
  4430. (list org-scheduled-string org-deadline-string
  4431. org-clock-string org-closed-string)))
  4432. (org-compute-latex-and-related-regexp)
  4433. (org-set-font-lock-defaults))))
  4434. (defun org-file-contents (file &optional noerror)
  4435. "Return the contents of FILE, as a string."
  4436. (if (or (not file)
  4437. (not (file-readable-p file)))
  4438. (if noerror
  4439. (progn
  4440. (message "Cannot read file \"%s\"" file)
  4441. (ding) (sit-for 2)
  4442. "")
  4443. (error "Cannot read file \"%s\"" file))
  4444. (with-temp-buffer
  4445. (insert-file-contents file)
  4446. (buffer-string))))
  4447. (defun org-extract-log-state-settings (x)
  4448. "Extract the log state setting from a TODO keyword string.
  4449. This will extract info from a string like \"WAIT(w@/!)\"."
  4450. (let (kw key log1 log2)
  4451. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4452. (setq kw (match-string 1 x)
  4453. key (and (match-end 2) (match-string 2 x))
  4454. log1 (and (match-end 3) (match-string 3 x))
  4455. log2 (and (match-end 4) (match-string 4 x)))
  4456. (and (or log1 log2)
  4457. (list kw
  4458. (and log1 (if (equal log1 "!") 'time 'note))
  4459. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4460. (defun org-remove-keyword-keys (list)
  4461. "Remove a pair of parenthesis at the end of each string in LIST."
  4462. (mapcar (lambda (x)
  4463. (if (string-match "(.*)$" x)
  4464. (substring x 0 (match-beginning 0))
  4465. x))
  4466. list))
  4467. (defun org-assign-fast-keys (alist)
  4468. "Assign fast keys to a keyword-key alist.
  4469. Respect keys that are already there."
  4470. (let (new e (alt ?0))
  4471. (while (setq e (pop alist))
  4472. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4473. (cdr e)) ;; Key already assigned.
  4474. (push e new)
  4475. (let ((clist (string-to-list (downcase (car e))))
  4476. (used (append new alist)))
  4477. (when (= (car clist) ?@)
  4478. (pop clist))
  4479. (while (and clist (rassoc (car clist) used))
  4480. (pop clist))
  4481. (unless clist
  4482. (while (rassoc alt used)
  4483. (incf alt)))
  4484. (push (cons (car e) (or (car clist) alt)) new))))
  4485. (nreverse new)))
  4486. ;;; Some variables used in various places
  4487. (defvar org-window-configuration nil
  4488. "Used in various places to store a window configuration.")
  4489. (defvar org-selected-window nil
  4490. "Used in various places to store a window configuration.")
  4491. (defvar org-finish-function nil
  4492. "Function to be called when `C-c C-c' is used.
  4493. This is for getting out of special buffers like capture.")
  4494. ;; FIXME: Occasionally check by commenting these, to make sure
  4495. ;; no other functions uses these, forgetting to let-bind them.
  4496. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4497. (defvar org-last-state)
  4498. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4499. ;; Defined somewhere in this file, but used before definition.
  4500. (defvar org-entities) ;; defined in org-entities.el
  4501. (defvar org-struct-menu)
  4502. (defvar org-org-menu)
  4503. (defvar org-tbl-menu)
  4504. ;;;; Define the Org-mode
  4505. ;; We use a before-change function to check if a table might need
  4506. ;; an update.
  4507. (defvar org-table-may-need-update t
  4508. "Indicates that a table might need an update.
  4509. This variable is set by `org-before-change-function'.
  4510. `org-table-align' sets it back to nil.")
  4511. (defun org-before-change-function (beg end)
  4512. "Every change indicates that a table might need an update."
  4513. (setq org-table-may-need-update t))
  4514. (defvar org-mode-map)
  4515. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4516. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4517. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4518. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4519. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4520. (defvar org-table-buffer-is-an nil)
  4521. (defvar bidi-paragraph-direction)
  4522. (defvar buffer-face-mode-face)
  4523. (require 'outline)
  4524. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4525. (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"))
  4526. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4527. ;; Other stuff we need.
  4528. (require 'time-date)
  4529. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4530. (require 'easymenu)
  4531. (require 'overlay)
  4532. ;; (require 'org-macs) moved higher up in the file before it is first used
  4533. (require 'org-entities)
  4534. ;; (require 'org-compat) moved higher up in the file before it is first used
  4535. (require 'org-faces)
  4536. (require 'org-list)
  4537. (require 'org-pcomplete)
  4538. (require 'org-src)
  4539. (require 'org-footnote)
  4540. (require 'org-macro)
  4541. ;; babel
  4542. (require 'ob)
  4543. ;;;###autoload
  4544. (define-derived-mode org-mode outline-mode "Org"
  4545. "Outline-based notes management and organizer, alias
  4546. \"Carsten's outline-mode for keeping track of everything.\"
  4547. Org-mode develops organizational tasks around a NOTES file which
  4548. contains information about projects as plain text. Org-mode is
  4549. implemented on top of outline-mode, which is ideal to keep the content
  4550. of large files well structured. It supports ToDo items, deadlines and
  4551. time stamps, which magically appear in the diary listing of the Emacs
  4552. calendar. Tables are easily created with a built-in table editor.
  4553. Plain text URL-like links connect to websites, emails (VM), Usenet
  4554. messages (Gnus), BBDB entries, and any files related to the project.
  4555. For printing and sharing of notes, an Org-mode file (or a part of it)
  4556. can be exported as a structured ASCII or HTML file.
  4557. The following commands are available:
  4558. \\{org-mode-map}"
  4559. ;; Get rid of Outline menus, they are not needed
  4560. ;; Need to do this here because define-derived-mode sets up
  4561. ;; the keymap so late. Still, it is a waste to call this each time
  4562. ;; we switch another buffer into org-mode.
  4563. (if (featurep 'xemacs)
  4564. (when (boundp 'outline-mode-menu-heading)
  4565. ;; Assume this is Greg's port, it uses easymenu
  4566. (easy-menu-remove outline-mode-menu-heading)
  4567. (easy-menu-remove outline-mode-menu-show)
  4568. (easy-menu-remove outline-mode-menu-hide))
  4569. (define-key org-mode-map [menu-bar headings] 'undefined)
  4570. (define-key org-mode-map [menu-bar hide] 'undefined)
  4571. (define-key org-mode-map [menu-bar show] 'undefined))
  4572. (org-load-modules-maybe)
  4573. (easy-menu-add org-org-menu)
  4574. (easy-menu-add org-tbl-menu)
  4575. (org-install-agenda-files-menu)
  4576. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4577. (add-to-invisibility-spec '(org-cwidth))
  4578. (add-to-invisibility-spec '(org-hide-block . t))
  4579. (when (featurep 'xemacs)
  4580. (org-set-local 'line-move-ignore-invisible t))
  4581. (org-set-local 'outline-regexp org-outline-regexp)
  4582. (org-set-local 'outline-level 'org-outline-level)
  4583. (setq bidi-paragraph-direction 'left-to-right)
  4584. ;; FIXME Circumvent a bug in outline.el (Emacs <24.4)
  4585. (set (make-local-variable 'paragraph-start) " \\|[ \t]*$\\|\\*+ ")
  4586. (when (and org-ellipsis
  4587. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4588. (fboundp 'make-glyph-code))
  4589. (unless org-display-table
  4590. (setq org-display-table (make-display-table)))
  4591. (set-display-table-slot
  4592. org-display-table 4
  4593. (vconcat (mapcar
  4594. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4595. org-ellipsis)))
  4596. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4597. (setq buffer-display-table org-display-table))
  4598. (org-set-regexps-and-options)
  4599. (when (and org-tag-faces (not org-tags-special-faces-re))
  4600. ;; tag faces set outside customize.... force initialization.
  4601. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4602. ;; Calc embedded
  4603. (org-set-local 'calc-embedded-open-mode "# ")
  4604. ;; Modify a few syntax entries
  4605. (modify-syntax-entry ?@ "w")
  4606. (modify-syntax-entry ?\" "\"")
  4607. (if org-startup-truncated (setq truncate-lines t))
  4608. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4609. (org-set-local 'font-lock-unfontify-region-function
  4610. 'org-unfontify-region)
  4611. ;; Activate before-change-function
  4612. (org-set-local 'org-table-may-need-update t)
  4613. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4614. 'local)
  4615. ;; Check for running clock before killing a buffer
  4616. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4617. ;; Initialize macros templates.
  4618. (org-macro-initialize-templates)
  4619. ;; Initialize radio targets.
  4620. (org-update-radio-target-regexp)
  4621. ;; Indentation.
  4622. (org-set-local 'indent-line-function 'org-indent-line)
  4623. (org-set-local 'indent-region-function 'org-indent-region)
  4624. ;; Filling and auto-filling.
  4625. (org-setup-filling)
  4626. ;; Comments.
  4627. (org-setup-comments-handling)
  4628. ;; Beginning/end of defun
  4629. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4630. (org-set-local 'end-of-defun-function 'org-forward-element)
  4631. ;; Next error for sparse trees
  4632. (org-set-local 'next-error-function 'org-occur-next-match)
  4633. ;; Make sure dependence stuff works reliably, even for users who set it
  4634. ;; too late :-(
  4635. (if org-enforce-todo-dependencies
  4636. (add-hook 'org-blocker-hook
  4637. 'org-block-todo-from-children-or-siblings-or-parent)
  4638. (remove-hook 'org-blocker-hook
  4639. 'org-block-todo-from-children-or-siblings-or-parent))
  4640. (if org-enforce-todo-checkbox-dependencies
  4641. (add-hook 'org-blocker-hook
  4642. 'org-block-todo-from-checkboxes)
  4643. (remove-hook 'org-blocker-hook
  4644. 'org-block-todo-from-checkboxes))
  4645. ;; Align options lines
  4646. (org-set-local
  4647. 'align-mode-rules-list
  4648. '((org-in-buffer-settings
  4649. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4650. (modes . '(org-mode)))))
  4651. ;; Imenu
  4652. (org-set-local 'imenu-create-index-function
  4653. 'org-imenu-get-tree)
  4654. ;; Make isearch reveal context
  4655. (if (or (featurep 'xemacs)
  4656. (not (boundp 'outline-isearch-open-invisible-function)))
  4657. ;; Emacs 21 and XEmacs make use of the hook
  4658. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4659. ;; Emacs 22 deals with this through a special variable
  4660. (org-set-local 'outline-isearch-open-invisible-function
  4661. (lambda (&rest ignore) (org-show-context 'isearch)))
  4662. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4663. ;; Setup the pcomplete hooks
  4664. (set (make-local-variable 'pcomplete-command-completion-function)
  4665. 'org-pcomplete-initial)
  4666. (set (make-local-variable 'pcomplete-command-name-function)
  4667. 'org-command-at-point)
  4668. (set (make-local-variable 'pcomplete-default-completion-function)
  4669. 'ignore)
  4670. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4671. 'org-parse-arguments)
  4672. (set (make-local-variable 'pcomplete-termination-string) "")
  4673. (when (>= emacs-major-version 23)
  4674. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4675. ;; If empty file that did not turn on org-mode automatically, make it to.
  4676. (if (and org-insert-mode-line-in-empty-file
  4677. (org-called-interactively-p 'any)
  4678. (= (point-min) (point-max)))
  4679. (insert "# -*- mode: org -*-\n\n"))
  4680. (unless org-inhibit-startup
  4681. (org-unmodified
  4682. (and org-startup-with-beamer-mode (org-beamer-mode))
  4683. (when org-startup-align-all-tables
  4684. (org-table-map-tables 'org-table-align 'quietly))
  4685. (when org-startup-with-inline-images
  4686. (org-display-inline-images))
  4687. (when org-startup-with-latex-preview
  4688. (org-preview-latex-fragment))
  4689. (unless org-inhibit-startup-visibility-stuff
  4690. (org-set-startup-visibility))))
  4691. ;; Try to set org-hide correctly
  4692. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4693. ;; Update `customize-package-emacs-version-alist'
  4694. (add-to-list 'customize-package-emacs-version-alist
  4695. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4696. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4697. ("8.0" . "24.4")))
  4698. (defvar org-mode-transpose-word-syntax-table
  4699. (let ((st (make-syntax-table)))
  4700. (mapc (lambda(c) (modify-syntax-entry
  4701. (string-to-char (car c)) "w p" st))
  4702. org-emphasis-alist)
  4703. st))
  4704. (when (fboundp 'abbrev-table-put)
  4705. (abbrev-table-put org-mode-abbrev-table
  4706. :parents (list text-mode-abbrev-table)))
  4707. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4708. (defsubst org-fix-ellipsis-at-bol ()
  4709. (save-excursion (goto-char (window-start)) (recenter 0)))
  4710. (defun org-find-invisible-foreground ()
  4711. (let ((candidates (remove
  4712. "unspecified-bg"
  4713. (nconc
  4714. (list (face-background 'default)
  4715. (face-background 'org-default))
  4716. (mapcar
  4717. (lambda (alist)
  4718. (when (boundp alist)
  4719. (cdr (assoc 'background-color (symbol-value alist)))))
  4720. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4721. (list (face-foreground 'org-hide))))))
  4722. (car (remove nil candidates))))
  4723. (defun org-current-time (&optional rounding-minutes past)
  4724. "Current time, possibly rounded to ROUNDING-MINUTES.
  4725. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4726. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4727. the rounding returns a past time."
  4728. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4729. (car org-time-stamp-rounding-minutes)))
  4730. (time (decode-time)) res)
  4731. (if (< r 1)
  4732. (current-time)
  4733. (setq res
  4734. (apply 'encode-time
  4735. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4736. (nthcdr 2 time))))
  4737. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4738. (seconds-to-time (- (org-float-time res) (* r 60)))
  4739. res))))
  4740. (defun org-today ()
  4741. "Return today date, considering `org-extend-today-until'."
  4742. (time-to-days
  4743. (time-subtract (current-time)
  4744. (list 0 (* 3600 org-extend-today-until) 0))))
  4745. ;;;; Font-Lock stuff, including the activators
  4746. (defvar org-mouse-map (make-sparse-keymap))
  4747. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4748. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4749. (when org-mouse-1-follows-link
  4750. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4751. (when org-tab-follows-link
  4752. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4753. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4754. (require 'font-lock)
  4755. (defconst org-non-link-chars "]\t\n\r<>")
  4756. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4757. "shell" "elisp" "doi" "message"))
  4758. (defvar org-link-types-re nil
  4759. "Matches a link that has a url-like prefix like \"http:\"")
  4760. (defvar org-link-re-with-space nil
  4761. "Matches a link with spaces, optional angular brackets around it.")
  4762. (defvar org-link-re-with-space2 nil
  4763. "Matches a link with spaces, optional angular brackets around it.")
  4764. (defvar org-link-re-with-space3 nil
  4765. "Matches a link with spaces, only for internal part in bracket links.")
  4766. (defvar org-angle-link-re nil
  4767. "Matches link with angular brackets, spaces are allowed.")
  4768. (defvar org-plain-link-re nil
  4769. "Matches plain link, without spaces.")
  4770. (defvar org-bracket-link-regexp nil
  4771. "Matches a link in double brackets.")
  4772. (defvar org-bracket-link-analytic-regexp nil
  4773. "Regular expression used to analyze links.
  4774. Here is what the match groups contain after a match:
  4775. 1: http:
  4776. 2: http
  4777. 3: path
  4778. 4: [desc]
  4779. 5: desc")
  4780. (defvar org-bracket-link-analytic-regexp++ nil
  4781. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4782. (defvar org-any-link-re nil
  4783. "Regular expression matching any link.")
  4784. (defconst org-match-sexp-depth 3
  4785. "Number of stacked braces for sub/superscript matching.")
  4786. (defun org-create-multibrace-regexp (left right n)
  4787. "Create a regular expression which will match a balanced sexp.
  4788. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4789. as single character strings.
  4790. The regexp returned will match the entire expression including the
  4791. delimiters. It will also define a single group which contains the
  4792. match except for the outermost delimiters. The maximum depth of
  4793. stacked delimiters is N. Escaping delimiters is not possible."
  4794. (let* ((nothing (concat "[^" left right "]*?"))
  4795. (or "\\|")
  4796. (re nothing)
  4797. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4798. (while (> n 1)
  4799. (setq n (1- n)
  4800. re (concat re or next)
  4801. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4802. (concat left "\\(" re "\\)" right)))
  4803. (defvar org-match-substring-regexp
  4804. (concat
  4805. "\\(\\S-\\)\\([_^]\\)\\("
  4806. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4807. "\\|"
  4808. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4809. "\\|"
  4810. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4811. "The regular expression matching a sub- or superscript.")
  4812. (defvar org-match-substring-with-braces-regexp
  4813. (concat
  4814. "\\(\\S-\\)\\([_^]\\)\\("
  4815. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4816. "\\)")
  4817. "The regular expression matching a sub- or superscript, forcing braces.")
  4818. (defun org-make-link-regexps ()
  4819. "Update the link regular expressions.
  4820. This should be called after the variable `org-link-types' has changed."
  4821. (setq org-link-types-re
  4822. (concat
  4823. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4824. org-link-re-with-space
  4825. (concat
  4826. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4827. "\\([^" org-non-link-chars " ]"
  4828. "[^" org-non-link-chars "]*"
  4829. "[^" org-non-link-chars " ]\\)>?")
  4830. org-link-re-with-space2
  4831. (concat
  4832. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4833. "\\([^" org-non-link-chars " ]"
  4834. "[^\t\n\r]*"
  4835. "[^" org-non-link-chars " ]\\)>?")
  4836. org-link-re-with-space3
  4837. (concat
  4838. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4839. "\\([^" org-non-link-chars " ]"
  4840. "[^\t\n\r]*\\)")
  4841. org-angle-link-re
  4842. (concat
  4843. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4844. "\\([^" org-non-link-chars " ]"
  4845. "[^" org-non-link-chars "]*"
  4846. "\\)>")
  4847. org-plain-link-re
  4848. (concat
  4849. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4850. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4851. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4852. org-bracket-link-regexp
  4853. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4854. org-bracket-link-analytic-regexp
  4855. (concat
  4856. "\\[\\["
  4857. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4858. "\\([^]]+\\)"
  4859. "\\]"
  4860. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4861. "\\]")
  4862. org-bracket-link-analytic-regexp++
  4863. (concat
  4864. "\\[\\["
  4865. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4866. "\\([^]]+\\)"
  4867. "\\]"
  4868. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4869. "\\]")
  4870. org-any-link-re
  4871. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4872. org-angle-link-re "\\)\\|\\("
  4873. org-plain-link-re "\\)")))
  4874. (org-make-link-regexps)
  4875. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4876. "Regular expression for fast time stamp matching.")
  4877. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4878. "Regular expression for fast time stamp matching.")
  4879. (defconst org-ts-regexp0
  4880. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4881. "Regular expression matching time strings for analysis.
  4882. This one does not require the space after the date, so it can be used
  4883. on a string that terminates immediately after the date.")
  4884. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4885. "Regular expression matching time strings for analysis.")
  4886. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4887. "Regular expression matching time stamps, with groups.")
  4888. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4889. "Regular expression matching time stamps (also [..]), with groups.")
  4890. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4891. "Regular expression matching a time stamp range.")
  4892. (defconst org-tr-regexp-both
  4893. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4894. "Regular expression matching a time stamp range.")
  4895. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4896. org-ts-regexp "\\)?")
  4897. "Regular expression matching a time stamp or time stamp range.")
  4898. (defconst org-tsr-regexp-both
  4899. (concat org-ts-regexp-both "\\(--?-?"
  4900. org-ts-regexp-both "\\)?")
  4901. "Regular expression matching a time stamp or time stamp range.
  4902. The time stamps may be either active or inactive.")
  4903. (defvar org-emph-face nil)
  4904. (defun org-do-emphasis-faces (limit)
  4905. "Run through the buffer and add overlays to emphasized strings."
  4906. (let (rtn a)
  4907. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4908. (if (not (= (char-after (match-beginning 3))
  4909. (char-after (match-beginning 4))))
  4910. (progn
  4911. (setq rtn t)
  4912. (setq a (assoc (match-string 3) org-emphasis-alist))
  4913. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4914. 'face
  4915. (nth 1 a))
  4916. (and (nth 4 a)
  4917. (org-remove-flyspell-overlays-in
  4918. (match-beginning 0) (match-end 0)))
  4919. (add-text-properties (match-beginning 2) (match-end 2)
  4920. '(font-lock-multiline t org-emphasis t))
  4921. (when org-hide-emphasis-markers
  4922. (add-text-properties (match-end 4) (match-beginning 5)
  4923. '(invisible org-link))
  4924. (add-text-properties (match-beginning 3) (match-end 3)
  4925. '(invisible org-link)))))
  4926. (backward-char 1))
  4927. rtn))
  4928. (defun org-emphasize (&optional char)
  4929. "Insert or change an emphasis, i.e. a font like bold or italic.
  4930. If there is an active region, change that region to a new emphasis.
  4931. If there is no region, just insert the marker characters and position
  4932. the cursor between them.
  4933. CHAR should be either the marker character, or the first character of the
  4934. HTML tag associated with that emphasis. If CHAR is a space, the means
  4935. to remove the emphasis of the selected region.
  4936. If char is not given (for example in an interactive call) it
  4937. will be prompted for."
  4938. (interactive)
  4939. (let ((eal org-emphasis-alist) e det
  4940. (erc org-emphasis-regexp-components)
  4941. (prompt "")
  4942. (string "") beg end move tag c s)
  4943. (if (org-region-active-p)
  4944. (setq beg (region-beginning) end (region-end)
  4945. string (buffer-substring beg end))
  4946. (setq move t))
  4947. (while (setq e (pop eal))
  4948. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4949. c (aref tag 0))
  4950. (push (cons c (string-to-char (car e))) det)
  4951. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4952. (substring tag 1)))))
  4953. (setq det (nreverse det))
  4954. (unless char
  4955. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4956. (setq char (read-char-exclusive)))
  4957. (setq char (or (cdr (assoc char det)) char))
  4958. (if (equal char ?\ )
  4959. (setq s "" move nil)
  4960. (unless (assoc (char-to-string char) org-emphasis-alist)
  4961. (error "No such emphasis marker: \"%c\"" char))
  4962. (setq s (char-to-string char)))
  4963. (while (and (> (length string) 1)
  4964. (equal (substring string 0 1) (substring string -1))
  4965. (assoc (substring string 0 1) org-emphasis-alist))
  4966. (setq string (substring string 1 -1)))
  4967. (setq string (concat s string s))
  4968. (if beg (delete-region beg end))
  4969. (unless (or (bolp)
  4970. (string-match (concat "[" (nth 0 erc) "\n]")
  4971. (char-to-string (char-before (point)))))
  4972. (insert " "))
  4973. (unless (or (eobp)
  4974. (string-match (concat "[" (nth 1 erc) "\n]")
  4975. (char-to-string (char-after (point)))))
  4976. (insert " ") (backward-char 1))
  4977. (insert string)
  4978. (and move (backward-char 1))))
  4979. (defconst org-nonsticky-props
  4980. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  4981. (defsubst org-rear-nonsticky-at (pos)
  4982. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4983. (defun org-activate-plain-links (limit)
  4984. "Run through the buffer and add overlays to links."
  4985. (let (f hl)
  4986. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  4987. (not (org-in-src-block-p)))
  4988. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4989. (setq f (get-text-property (match-beginning 0) 'face))
  4990. (setq hl (org-match-string-no-properties 0))
  4991. (if (or (eq f 'org-tag)
  4992. (and (listp f) (memq 'org-tag f)))
  4993. nil
  4994. (add-text-properties (match-beginning 0) (match-end 0)
  4995. (list 'mouse-face 'highlight
  4996. 'face 'org-link
  4997. 'htmlize-link `(:uri ,hl)
  4998. 'keymap org-mouse-map))
  4999. (org-rear-nonsticky-at (match-end 0)))
  5000. t)))
  5001. (defun org-activate-code (limit)
  5002. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5003. (progn
  5004. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5005. (remove-text-properties (match-beginning 0) (match-end 0)
  5006. '(display t invisible t intangible t))
  5007. t)))
  5008. (defcustom org-src-fontify-natively nil
  5009. "When non-nil, fontify code in code blocks."
  5010. :type 'boolean
  5011. :version "24.1"
  5012. :group 'org-appearance
  5013. :group 'org-babel)
  5014. (defcustom org-allow-promoting-top-level-subtree nil
  5015. "When non-nil, allow promoting a top level subtree.
  5016. The leading star of the top level headline will be replaced
  5017. by a #."
  5018. :type 'boolean
  5019. :version "24.1"
  5020. :group 'org-appearance)
  5021. (defun org-fontify-meta-lines-and-blocks (limit)
  5022. (condition-case nil
  5023. (org-fontify-meta-lines-and-blocks-1 limit)
  5024. (error (message "org-mode fontification error"))))
  5025. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5026. "Fontify #+ lines and blocks, in the correct ways."
  5027. (let ((case-fold-search t))
  5028. (if (re-search-forward
  5029. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5030. limit t)
  5031. (let ((beg (match-beginning 0))
  5032. (block-start (match-end 0))
  5033. (block-end nil)
  5034. (lang (match-string 7))
  5035. (beg1 (line-beginning-position 2))
  5036. (dc1 (downcase (match-string 2)))
  5037. (dc3 (downcase (match-string 3)))
  5038. end end1 quoting block-type ovl)
  5039. (cond
  5040. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5041. ;; a single line of backend-specific content
  5042. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5043. (remove-text-properties (match-beginning 0) (match-end 0)
  5044. '(display t invisible t intangible t))
  5045. (add-text-properties (match-beginning 1) (match-end 3)
  5046. '(font-lock-fontified t face org-meta-line))
  5047. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5048. '(font-lock-fontified t face org-block))
  5049. ; for backend-specific code
  5050. t)
  5051. ((and (match-end 4) (equal dc3 "+begin"))
  5052. ;; Truly a block
  5053. (setq block-type (downcase (match-string 5))
  5054. quoting (member block-type org-protecting-blocks))
  5055. (when (re-search-forward
  5056. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5057. nil t) ;; on purpose, we look further than LIMIT
  5058. (setq end (min (point-max) (match-end 0))
  5059. end1 (min (point-max) (1- (match-beginning 0))))
  5060. (setq block-end (match-beginning 0))
  5061. (when quoting
  5062. (remove-text-properties beg end
  5063. '(display t invisible t intangible t)))
  5064. (add-text-properties
  5065. beg end
  5066. '(font-lock-fontified t font-lock-multiline t))
  5067. (add-text-properties beg beg1 '(face org-meta-line))
  5068. (add-text-properties end1 (min (point-max) (1+ end))
  5069. '(face org-meta-line)) ; for end_src
  5070. (cond
  5071. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5072. (org-src-font-lock-fontify-block lang block-start block-end)
  5073. ;; remove old background overlays
  5074. (mapc (lambda (ov)
  5075. (if (eq (overlay-get ov 'face) 'org-block-background)
  5076. (delete-overlay ov)))
  5077. (overlays-at (/ (+ beg1 block-end) 2)))
  5078. ;; add a background overlay
  5079. (setq ovl (make-overlay beg1 block-end))
  5080. (overlay-put ovl 'face 'org-block-background)
  5081. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5082. (quoting
  5083. (add-text-properties beg1 (min (point-max) (1+ end1))
  5084. '(face org-block))) ; end of source block
  5085. ((not org-fontify-quote-and-verse-blocks))
  5086. ((string= block-type "quote")
  5087. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5088. ((string= block-type "verse")
  5089. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5090. (add-text-properties beg beg1 '(face org-block-begin-line))
  5091. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5092. '(face org-block-end-line))
  5093. t))
  5094. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5095. (add-text-properties
  5096. beg (match-end 3)
  5097. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5098. '(font-lock-fontified t invisible t)
  5099. '(font-lock-fontified t face org-document-info-keyword)))
  5100. (add-text-properties
  5101. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5102. (if (string-equal dc1 "+title:")
  5103. '(font-lock-fontified t face org-document-title)
  5104. '(font-lock-fontified t face org-document-info))))
  5105. ((or (equal dc1 "+results")
  5106. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5107. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5108. "+call:" "+header:" "+headers:" "+name:"))
  5109. (and (match-end 4) (equal dc3 "+attr")))
  5110. (add-text-properties
  5111. beg (match-end 0)
  5112. '(font-lock-fontified t face org-meta-line))
  5113. t)
  5114. ((member dc3 '(" " ""))
  5115. (add-text-properties
  5116. beg (match-end 0)
  5117. '(font-lock-fontified t face font-lock-comment-face)))
  5118. ((not (member (char-after beg) '(?\ ?\t)))
  5119. ;; just any other in-buffer setting, but not indented
  5120. (add-text-properties
  5121. beg (match-end 0)
  5122. '(font-lock-fontified t face org-meta-line))
  5123. t)
  5124. (t nil))))))
  5125. (defun org-activate-angle-links (limit)
  5126. "Run through the buffer and add overlays to links."
  5127. (if (and (re-search-forward org-angle-link-re limit t)
  5128. (not (org-in-src-block-p)))
  5129. (progn
  5130. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5131. (add-text-properties (match-beginning 0) (match-end 0)
  5132. (list 'mouse-face 'highlight
  5133. 'keymap org-mouse-map))
  5134. (org-rear-nonsticky-at (match-end 0))
  5135. t)))
  5136. (defun org-activate-footnote-links (limit)
  5137. "Run through the buffer and add overlays to footnotes."
  5138. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5139. (when fn
  5140. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5141. (org-remove-flyspell-overlays-in beg end)
  5142. (add-text-properties beg end
  5143. (list 'mouse-face 'highlight
  5144. 'keymap org-mouse-map
  5145. 'help-echo
  5146. (if (= (point-at-bol) beg)
  5147. "Footnote definition"
  5148. "Footnote reference")
  5149. 'font-lock-fontified t
  5150. 'font-lock-multiline t
  5151. 'face 'org-footnote))))))
  5152. (defun org-activate-bracket-links (limit)
  5153. "Run through the buffer and add overlays to bracketed links."
  5154. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5155. (not (org-in-src-block-p)))
  5156. (let* ((hl (org-match-string-no-properties 1))
  5157. (help (concat "LINK: " hl))
  5158. ;; FIXME: Above we should remove the escapes. But that
  5159. ;; requires another match, protecting match data, a lot
  5160. ;; of overhead for font-lock.
  5161. (ip (org-maybe-intangible
  5162. (list 'invisible 'org-link
  5163. 'keymap org-mouse-map 'mouse-face 'highlight
  5164. 'font-lock-multiline t 'help-echo help
  5165. 'htmlize-link `(:uri ,hl))))
  5166. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5167. 'font-lock-multiline t 'help-echo help
  5168. 'htmlize-link `(:uri ,hl))))
  5169. ;; We need to remove the invisible property here. Table narrowing
  5170. ;; may have made some of this invisible.
  5171. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5172. (remove-text-properties (match-beginning 0) (match-end 0)
  5173. '(invisible nil))
  5174. (if (match-end 3)
  5175. (progn
  5176. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5177. (org-rear-nonsticky-at (match-beginning 3))
  5178. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5179. (org-rear-nonsticky-at (match-end 3))
  5180. (add-text-properties (match-end 3) (match-end 0) ip)
  5181. (org-rear-nonsticky-at (match-end 0)))
  5182. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5183. (org-rear-nonsticky-at (match-beginning 1))
  5184. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5185. (org-rear-nonsticky-at (match-end 1))
  5186. (add-text-properties (match-end 1) (match-end 0) ip)
  5187. (org-rear-nonsticky-at (match-end 0)))
  5188. t)))
  5189. (defun org-activate-dates (limit)
  5190. "Run through the buffer and add overlays to dates."
  5191. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5192. (not (equal (char-before (match-beginning 0)) 91)))
  5193. (progn
  5194. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5195. (add-text-properties (match-beginning 0) (match-end 0)
  5196. (list 'mouse-face 'highlight
  5197. 'keymap org-mouse-map))
  5198. (org-rear-nonsticky-at (match-end 0))
  5199. (when org-display-custom-times
  5200. (if (match-end 3)
  5201. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5202. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5203. t)))
  5204. (defvar org-target-link-regexp nil
  5205. "Regular expression matching radio targets in plain text.")
  5206. (make-variable-buffer-local 'org-target-link-regexp)
  5207. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5208. "Regular expression matching a link target.")
  5209. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5210. "Regular expression matching a radio target.")
  5211. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5212. "Regular expression matching any target.")
  5213. (defun org-activate-target-links (limit)
  5214. "Run through the buffer and add overlays to target matches."
  5215. (when org-target-link-regexp
  5216. (let ((case-fold-search t))
  5217. (if (re-search-forward org-target-link-regexp limit t)
  5218. (progn
  5219. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5220. (add-text-properties (match-beginning 0) (match-end 0)
  5221. (list 'mouse-face 'highlight
  5222. 'keymap org-mouse-map
  5223. 'help-echo "Radio target link"
  5224. 'org-linked-text t))
  5225. (org-rear-nonsticky-at (match-end 0))
  5226. t)))))
  5227. (defun org-update-radio-target-regexp ()
  5228. "Find all radio targets in this file and update the regular expression."
  5229. (interactive)
  5230. (when (memq 'radio org-activate-links)
  5231. (setq org-target-link-regexp
  5232. (org-make-target-link-regexp (org-all-targets 'radio)))
  5233. (org-restart-font-lock)))
  5234. (defun org-hide-wide-columns (limit)
  5235. (let (s e)
  5236. (setq s (text-property-any (point) (or limit (point-max))
  5237. 'org-cwidth t))
  5238. (when s
  5239. (setq e (next-single-property-change s 'org-cwidth))
  5240. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5241. (goto-char e)
  5242. t)))
  5243. (defvar org-latex-and-related-regexp nil
  5244. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5245. (defvar org-match-substring-regexp)
  5246. (defvar org-match-substring-with-braces-regexp)
  5247. (defun org-compute-latex-and-related-regexp ()
  5248. "Compute regular expression for LaTeX, entities and sub/superscript.
  5249. Result depends on variable `org-highlight-latex-and-related'."
  5250. (org-set-local
  5251. 'org-latex-and-related-regexp
  5252. (let* ((re-sub
  5253. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5254. ((eq org-use-sub-superscripts '{})
  5255. (list org-match-substring-with-braces-regexp))
  5256. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5257. (re-latex
  5258. (when (memq 'latex org-highlight-latex-and-related)
  5259. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5260. (delq nil
  5261. (mapcar (lambda (x)
  5262. (and (member (car x) matchers) (nth 1 x)))
  5263. org-latex-regexps)))))
  5264. (re-entities
  5265. (when (memq 'entities org-highlight-latex-and-related)
  5266. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5267. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5268. (defun org-do-latex-and-related (limit)
  5269. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5270. LIMIT bounds the search for syntax to highlight. Stop at first
  5271. highlighted object, if any. Return t if some highlighting was
  5272. done, nil otherwise."
  5273. (when (org-string-nw-p org-latex-and-related-regexp)
  5274. (catch 'found
  5275. (while (re-search-forward org-latex-and-related-regexp limit t)
  5276. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5277. 'face))
  5278. '(org-code org-verbatim underline))
  5279. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5280. '(?_ ?^))
  5281. 1
  5282. 0)))
  5283. (font-lock-prepend-text-property
  5284. (+ offset (match-beginning 0)) (match-end 0)
  5285. 'face 'org-latex-and-related)
  5286. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5287. '(font-lock-multiline t)))
  5288. (throw 'found t)))
  5289. nil)))
  5290. (defun org-restart-font-lock ()
  5291. "Restart `font-lock-mode', to force refontification."
  5292. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5293. (font-lock-mode -1)
  5294. (font-lock-mode 1)))
  5295. (defun org-all-targets (&optional radio)
  5296. "Return a list of all targets in this file.
  5297. When optional argument RADIO is non-nil, only find radio
  5298. targets."
  5299. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5300. (save-excursion
  5301. (goto-char (point-min))
  5302. (while (re-search-forward re nil t)
  5303. ;; Make sure point is really within the object.
  5304. (backward-char)
  5305. (let ((obj (org-element-context)))
  5306. (when (memq (org-element-type obj) '(radio-target target))
  5307. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5308. rtn)))
  5309. (defun org-make-target-link-regexp (targets)
  5310. "Make regular expression matching all strings in TARGETS.
  5311. The regular expression finds the targets also if there is a line break
  5312. between words."
  5313. (and targets
  5314. (concat
  5315. "\\<\\("
  5316. (mapconcat
  5317. (lambda (x)
  5318. (setq x (regexp-quote x))
  5319. (while (string-match " +" x)
  5320. (setq x (replace-match "\\s-+" t t x)))
  5321. x)
  5322. targets
  5323. "\\|")
  5324. "\\)\\>")))
  5325. (defun org-activate-tags (limit)
  5326. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5327. (progn
  5328. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5329. (add-text-properties (match-beginning 1) (match-end 1)
  5330. (list 'mouse-face 'highlight
  5331. 'keymap org-mouse-map))
  5332. (org-rear-nonsticky-at (match-end 1))
  5333. t)))
  5334. (defun org-outline-level ()
  5335. "Compute the outline level of the heading at point.
  5336. If this is called at a normal headline, the level is the number of stars.
  5337. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5338. (save-excursion
  5339. (if (not (condition-case nil
  5340. (org-back-to-heading t)
  5341. (error nil)))
  5342. 0
  5343. (looking-at org-outline-regexp)
  5344. (1- (- (match-end 0) (match-beginning 0))))))
  5345. (defvar org-font-lock-keywords nil)
  5346. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5347. "Regular expression matching a property line.")
  5348. (defvar org-font-lock-hook nil
  5349. "Functions to be called for special font lock stuff.")
  5350. (defvar org-font-lock-set-keywords-hook nil
  5351. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5352. This is called after `org-font-lock-extra-keywords' is defined, but before
  5353. it is installed to be used by font lock. This can be useful if something
  5354. needs to be inserted at a specific position in the font-lock sequence.")
  5355. (defun org-font-lock-hook (limit)
  5356. "Run `org-font-lock-hook' within LIMIT."
  5357. (run-hook-with-args 'org-font-lock-hook limit))
  5358. (defun org-set-font-lock-defaults ()
  5359. "Set font lock defaults for the current buffer."
  5360. (let* ((em org-fontify-emphasized-text)
  5361. (lk org-activate-links)
  5362. (org-font-lock-extra-keywords
  5363. (list
  5364. ;; Call the hook
  5365. '(org-font-lock-hook)
  5366. ;; Headlines
  5367. `(,(if org-fontify-whole-heading-line
  5368. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5369. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5370. (1 (org-get-level-face 1))
  5371. (2 (org-get-level-face 2))
  5372. (3 (org-get-level-face 3)))
  5373. ;; Table lines
  5374. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5375. (1 'org-table t))
  5376. ;; Table internals
  5377. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5378. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5379. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5380. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5381. ;; Drawers
  5382. (list org-drawer-regexp '(0 'org-special-keyword t))
  5383. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5384. ;; Properties
  5385. (list org-property-re
  5386. '(1 'org-special-keyword t)
  5387. '(3 'org-property-value t))
  5388. ;; Links
  5389. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5390. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5391. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5392. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5393. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5394. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5395. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5396. ;; Targets.
  5397. (list org-any-target-regexp '(0 'org-target t))
  5398. ;; Diary sexps.
  5399. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5400. ;; Macro
  5401. '("{{{.+}}}" (0 'org-macro t))
  5402. '(org-hide-wide-columns (0 nil append))
  5403. ;; TODO keyword
  5404. (list (format org-heading-keyword-regexp-format
  5405. org-todo-regexp)
  5406. '(2 (org-get-todo-face 2) t))
  5407. ;; DONE
  5408. (if org-fontify-done-headline
  5409. (list (format org-heading-keyword-regexp-format
  5410. (concat
  5411. "\\(?:"
  5412. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5413. "\\)"))
  5414. '(2 'org-headline-done t))
  5415. nil)
  5416. ;; Priorities
  5417. '(org-font-lock-add-priority-faces)
  5418. ;; Tags
  5419. '(org-font-lock-add-tag-faces)
  5420. ;; Special keywords
  5421. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5422. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5423. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5424. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5425. ;; Emphasis
  5426. (if em
  5427. (if (featurep 'xemacs)
  5428. '(org-do-emphasis-faces (0 nil append))
  5429. '(org-do-emphasis-faces)))
  5430. ;; Checkboxes
  5431. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5432. 1 'org-checkbox prepend)
  5433. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5434. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5435. (0 (org-get-checkbox-statistics-face) t)))
  5436. ;; Description list items
  5437. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5438. 1 'org-list-dt prepend)
  5439. ;; ARCHIVEd headings
  5440. (list (concat
  5441. org-outline-regexp-bol
  5442. "\\(.*:" org-archive-tag ":.*\\)")
  5443. '(1 'org-archived prepend))
  5444. ;; Specials
  5445. '(org-do-latex-and-related)
  5446. '(org-fontify-entities)
  5447. '(org-raise-scripts)
  5448. ;; Code
  5449. '(org-activate-code (1 'org-code t))
  5450. ;; COMMENT
  5451. (list (format org-heading-keyword-regexp-format
  5452. (concat "\\("
  5453. org-comment-string "\\|" org-quote-string
  5454. "\\)"))
  5455. '(2 'org-special-keyword t))
  5456. ;; Blocks and meta lines
  5457. '(org-fontify-meta-lines-and-blocks)
  5458. )))
  5459. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5460. (run-hooks 'org-font-lock-set-keywords-hook)
  5461. ;; Now set the full font-lock-keywords
  5462. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5463. (org-set-local 'font-lock-defaults
  5464. '(org-font-lock-keywords t nil nil backward-paragraph))
  5465. (kill-local-variable 'font-lock-keywords) nil))
  5466. (defun org-toggle-pretty-entities ()
  5467. "Toggle the composition display of entities as UTF8 characters."
  5468. (interactive)
  5469. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5470. (org-restart-font-lock)
  5471. (if org-pretty-entities
  5472. (message "Entities are displayed as UTF8 characters")
  5473. (save-restriction
  5474. (widen)
  5475. (org-decompose-region (point-min) (point-max))
  5476. (message "Entities are displayed plain"))))
  5477. (defvar org-custom-properties-overlays nil
  5478. "List of overlays used for custom properties.")
  5479. (make-variable-buffer-local 'org-custom-properties-overlays)
  5480. (defun org-toggle-custom-properties-visibility ()
  5481. "Display or hide properties in `org-custom-properties'."
  5482. (interactive)
  5483. (if org-custom-properties-overlays
  5484. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5485. (setq org-custom-properties-overlays nil))
  5486. (unless (not org-custom-properties)
  5487. (save-excursion
  5488. (save-restriction
  5489. (widen)
  5490. (goto-char (point-min))
  5491. (while (re-search-forward org-property-re nil t)
  5492. (mapc (lambda(p)
  5493. (when (equal p (substring (match-string 1) 1 -1))
  5494. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5495. (overlay-put o 'invisible t)
  5496. (overlay-put o 'org-custom-property t)
  5497. (push o org-custom-properties-overlays))))
  5498. org-custom-properties)))))))
  5499. (defun org-fontify-entities (limit)
  5500. "Find an entity to fontify."
  5501. (let (ee)
  5502. (when org-pretty-entities
  5503. (catch 'match
  5504. (while (re-search-forward
  5505. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5506. limit t)
  5507. (if (and (not (org-in-indented-comment-line))
  5508. (setq ee (org-entity-get (match-string 1)))
  5509. (= (length (nth 6 ee)) 1))
  5510. (let*
  5511. ((end (if (equal (match-string 2) "{}")
  5512. (match-end 2)
  5513. (match-end 1))))
  5514. (add-text-properties
  5515. (match-beginning 0) end
  5516. (list 'font-lock-fontified t))
  5517. (compose-region (match-beginning 0) end
  5518. (nth 6 ee) nil)
  5519. (backward-char 1)
  5520. (throw 'match t))))
  5521. nil))))
  5522. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5523. "Fontify string S like in Org-mode."
  5524. (with-temp-buffer
  5525. (insert s)
  5526. (let ((org-odd-levels-only odd-levels))
  5527. (org-mode)
  5528. (font-lock-fontify-buffer)
  5529. (buffer-string))))
  5530. (defvar org-m nil)
  5531. (defvar org-l nil)
  5532. (defvar org-f nil)
  5533. (defun org-get-level-face (n)
  5534. "Get the right face for match N in font-lock matching of headlines."
  5535. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5536. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5537. (if org-cycle-level-faces
  5538. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5539. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5540. (cond
  5541. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5542. ((eq n 2) org-f)
  5543. (t (if org-level-color-stars-only nil org-f))))
  5544. (defun org-get-todo-face (kwd)
  5545. "Get the right face for a TODO keyword KWD.
  5546. If KWD is a number, get the corresponding match group."
  5547. (if (numberp kwd) (setq kwd (match-string kwd)))
  5548. (or (org-face-from-face-or-color
  5549. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5550. (and (member kwd org-done-keywords) 'org-done)
  5551. 'org-todo))
  5552. (defun org-face-from-face-or-color (context inherit face-or-color)
  5553. "Create a face list that inherits INHERIT, but sets the foreground color.
  5554. When FACE-OR-COLOR is not a string, just return it."
  5555. (if (stringp face-or-color)
  5556. (list :inherit inherit
  5557. (cdr (assoc context org-faces-easy-properties))
  5558. face-or-color)
  5559. face-or-color))
  5560. (defun org-font-lock-add-tag-faces (limit)
  5561. "Add the special tag faces."
  5562. (when (and org-tag-faces org-tags-special-faces-re)
  5563. (while (re-search-forward org-tags-special-faces-re limit t)
  5564. (add-text-properties (match-beginning 1) (match-end 1)
  5565. (list 'face (org-get-tag-face 1)
  5566. 'font-lock-fontified t))
  5567. (backward-char 1))))
  5568. (defun org-font-lock-add-priority-faces (limit)
  5569. "Add the special priority faces."
  5570. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5571. (when (save-match-data (org-at-heading-p))
  5572. (add-text-properties
  5573. (match-beginning 0) (match-end 0)
  5574. (list 'face (or (org-face-from-face-or-color
  5575. 'priority 'org-priority
  5576. (cdr (assoc (char-after (match-beginning 1))
  5577. org-priority-faces)))
  5578. 'org-priority)
  5579. 'font-lock-fontified t)))))
  5580. (defun org-get-tag-face (kwd)
  5581. "Get the right face for a TODO keyword KWD.
  5582. If KWD is a number, get the corresponding match group."
  5583. (if (numberp kwd) (setq kwd (match-string kwd)))
  5584. (or (org-face-from-face-or-color
  5585. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5586. 'org-tag))
  5587. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5588. "Remove fontification and activation overlays from links."
  5589. (font-lock-default-unfontify-region beg end)
  5590. (let* ((buffer-undo-list t)
  5591. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5592. (inhibit-modification-hooks t)
  5593. deactivate-mark buffer-file-name buffer-file-truename)
  5594. (org-decompose-region beg end)
  5595. (remove-text-properties beg end
  5596. '(mouse-face t keymap t org-linked-text t
  5597. invisible t intangible t
  5598. org-no-flyspell t org-emphasis t))
  5599. (org-remove-font-lock-display-properties beg end)))
  5600. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5601. ((raise 0.3) (height 0.7))
  5602. ((raise -0.5))
  5603. ((raise 0.5)))
  5604. "Display properties for showing superscripts and subscripts.")
  5605. (defun org-remove-font-lock-display-properties (beg end)
  5606. "Remove specific display properties that have been added by font lock.
  5607. The will remove the raise properties that are used to show superscripts
  5608. and subscripts."
  5609. (let (next prop)
  5610. (while (< beg end)
  5611. (setq next (next-single-property-change beg 'display nil end)
  5612. prop (get-text-property beg 'display))
  5613. (if (member prop org-script-display)
  5614. (put-text-property beg next 'display nil))
  5615. (setq beg next))))
  5616. (defun org-raise-scripts (limit)
  5617. "Add raise properties to sub/superscripts."
  5618. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5619. (if (re-search-forward
  5620. (if (eq org-use-sub-superscripts t)
  5621. org-match-substring-regexp
  5622. org-match-substring-with-braces-regexp)
  5623. limit t)
  5624. (let* ((pos (point)) table-p comment-p
  5625. (mpos (match-beginning 3))
  5626. (emph-p (get-text-property mpos 'org-emphasis))
  5627. (link-p (get-text-property mpos 'mouse-face))
  5628. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5629. (goto-char (point-at-bol))
  5630. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5631. comment-p (org-looking-at-p "[ \t]*#"))
  5632. (goto-char pos)
  5633. ;; FIXME: Should we go back one character here, for a_b^c
  5634. ;; (goto-char (1- pos)) ;????????????????????
  5635. (if (or comment-p emph-p link-p keyw-p)
  5636. t
  5637. (put-text-property (match-beginning 3) (match-end 0)
  5638. 'display
  5639. (if (equal (char-after (match-beginning 2)) ?^)
  5640. (nth (if table-p 3 1) org-script-display)
  5641. (nth (if table-p 2 0) org-script-display)))
  5642. (add-text-properties (match-beginning 2) (match-end 2)
  5643. (list 'invisible t
  5644. 'org-dwidth t 'org-dwidth-n 1))
  5645. (if (and (eq (char-after (match-beginning 3)) ?{)
  5646. (eq (char-before (match-end 3)) ?}))
  5647. (progn
  5648. (add-text-properties
  5649. (match-beginning 3) (1+ (match-beginning 3))
  5650. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5651. (add-text-properties
  5652. (1- (match-end 3)) (match-end 3)
  5653. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5654. t)))))
  5655. ;;;; Visibility cycling, including org-goto and indirect buffer
  5656. ;;; Cycling
  5657. (defvar org-cycle-global-status nil)
  5658. (make-variable-buffer-local 'org-cycle-global-status)
  5659. (put 'org-cycle-global-status 'org-state t)
  5660. (defvar org-cycle-subtree-status nil)
  5661. (make-variable-buffer-local 'org-cycle-subtree-status)
  5662. (put 'org-cycle-subtree-status 'org-state t)
  5663. (defvar org-inlinetask-min-level)
  5664. ;;;###autoload
  5665. (defun org-cycle (&optional arg)
  5666. "TAB-action and visibility cycling for Org-mode.
  5667. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5668. is outline visibility cycling, but it also invokes other actions
  5669. in special contexts.
  5670. - When this function is called with a prefix argument, rotate the entire
  5671. buffer through 3 states (global cycling)
  5672. 1. OVERVIEW: Show only top-level headlines.
  5673. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5674. 3. SHOW ALL: Show everything.
  5675. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5676. determined by the variable `org-startup-folded', and by any VISIBILITY
  5677. properties in the buffer.
  5678. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5679. including any drawers.
  5680. - When inside a table, re-align the table and move to the next field.
  5681. - When point is at the beginning of a headline, rotate the subtree started
  5682. by this line through 3 different states (local cycling)
  5683. 1. FOLDED: Only the main headline is shown.
  5684. 2. CHILDREN: The main headline and the direct children are shown.
  5685. From this state, you can move to one of the children
  5686. and zoom in further.
  5687. 3. SUBTREE: Show the entire subtree, including body text.
  5688. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5689. - When point is at the beginning of an empty headline and the variable
  5690. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5691. of the headline by demoting and promoting it to likely levels. This
  5692. speeds up creation document structure by pressing TAB once or several
  5693. times right after creating a new headline.
  5694. - When there is a numeric prefix, go up to a heading with level ARG, do
  5695. a `show-subtree' and return to the previous cursor position. If ARG
  5696. is negative, go up that many levels.
  5697. - When point is not at the beginning of a headline, execute the global
  5698. binding for TAB, which is re-indenting the line. See the option
  5699. `org-cycle-emulate-tab' for details.
  5700. - Special case: if point is at the beginning of the buffer and there is
  5701. no headline in line 1, this function will act as if called with prefix arg
  5702. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5703. But only if also the variable `org-cycle-global-at-bob' is t."
  5704. (interactive "P")
  5705. (org-load-modules-maybe)
  5706. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5707. (and org-cycle-level-after-item/entry-creation
  5708. (or (org-cycle-level)
  5709. (org-cycle-item-indentation))))
  5710. (let* (message-log-max ; Don't populate the *Messages* buffer
  5711. (limit-level
  5712. (or org-cycle-max-level
  5713. (and (boundp 'org-inlinetask-min-level)
  5714. org-inlinetask-min-level
  5715. (1- org-inlinetask-min-level))))
  5716. (nstars (and limit-level
  5717. (if org-odd-levels-only
  5718. (and limit-level (1- (* limit-level 2)))
  5719. limit-level)))
  5720. (org-outline-regexp
  5721. (if (not (derived-mode-p 'org-mode))
  5722. outline-regexp
  5723. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5724. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5725. (not (looking-at org-outline-regexp))))
  5726. (org-cycle-hook
  5727. (if bob-special
  5728. (delq 'org-optimize-window-after-visibility-change
  5729. (copy-sequence org-cycle-hook))
  5730. org-cycle-hook))
  5731. (pos (point)))
  5732. (if (or bob-special (equal arg '(4)))
  5733. ;; special case: use global cycling
  5734. (setq arg t))
  5735. (cond
  5736. ((equal arg '(16))
  5737. (setq last-command 'dummy)
  5738. (org-set-startup-visibility)
  5739. (message "Startup visibility, plus VISIBILITY properties"))
  5740. ((equal arg '(64))
  5741. (show-all)
  5742. (message "Entire buffer visible, including drawers"))
  5743. ;; Table: enter it or move to the next field.
  5744. ((org-at-table-p 'any)
  5745. (if (org-at-table.el-p)
  5746. (message "Use C-c ' to edit table.el tables")
  5747. (if arg (org-table-edit-field t)
  5748. (org-table-justify-field-maybe)
  5749. (call-interactively 'org-table-next-field))))
  5750. ((run-hook-with-args-until-success
  5751. 'org-tab-after-check-for-table-hook))
  5752. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5753. ((eq arg t) (org-cycle-internal-global))
  5754. ;; Drawers: delegate to `org-flag-drawer'.
  5755. ((and org-drawers org-drawer-regexp
  5756. (save-excursion
  5757. (beginning-of-line 1)
  5758. (looking-at org-drawer-regexp)))
  5759. (org-flag-drawer ; toggle block visibility
  5760. (not (get-char-property (match-end 0) 'invisible))))
  5761. ;; Show-subtree, ARG levels up from here.
  5762. ((integerp arg)
  5763. (save-excursion
  5764. (org-back-to-heading)
  5765. (outline-up-heading (if (< arg 0) (- arg)
  5766. (- (funcall outline-level) arg)))
  5767. (org-show-subtree)))
  5768. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5769. ((and (featurep 'org-inlinetask)
  5770. (org-inlinetask-at-task-p)
  5771. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5772. (org-inlinetask-toggle-visibility))
  5773. ((org-try-cdlatex-tab))
  5774. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5775. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5776. (save-excursion (beginning-of-line 1)
  5777. (looking-at org-outline-regexp)))
  5778. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5779. (org-cycle-internal-local))
  5780. ;; From there: TAB emulation and template completion.
  5781. (buffer-read-only (org-back-to-heading))
  5782. ((run-hook-with-args-until-success
  5783. 'org-tab-after-check-for-cycling-hook))
  5784. ((org-try-structure-completion))
  5785. ((run-hook-with-args-until-success
  5786. 'org-tab-before-tab-emulation-hook))
  5787. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5788. (or (not (bolp))
  5789. (not (looking-at org-outline-regexp))))
  5790. (call-interactively (global-key-binding "\t")))
  5791. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5792. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5793. (or (and (eq org-cycle-emulate-tab 'white)
  5794. (= (match-end 0) (point-at-eol)))
  5795. (and (eq org-cycle-emulate-tab 'whitestart)
  5796. (>= (match-end 0) pos))))
  5797. t
  5798. (eq org-cycle-emulate-tab t))
  5799. (call-interactively (global-key-binding "\t")))
  5800. (t (save-excursion
  5801. (org-back-to-heading)
  5802. (org-cycle)))))))
  5803. (defun org-cycle-internal-global ()
  5804. "Do the global cycling action."
  5805. ;; Hack to avoid display of messages for .org attachments in Gnus
  5806. (let (message-log-max ; Don't populate the *Messages* buffer
  5807. (ga (string-match "\\*fontification" (buffer-name))))
  5808. (cond
  5809. ((and (eq last-command this-command)
  5810. (eq org-cycle-global-status 'overview))
  5811. ;; We just created the overview - now do table of contents
  5812. ;; This can be slow in very large buffers, so indicate action
  5813. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5814. (unless ga (message "CONTENTS..."))
  5815. (org-content)
  5816. (unless ga (message "CONTENTS...done"))
  5817. (setq org-cycle-global-status 'contents)
  5818. (run-hook-with-args 'org-cycle-hook 'contents))
  5819. ((and (eq last-command this-command)
  5820. (eq org-cycle-global-status 'contents))
  5821. ;; We just showed the table of contents - now show everything
  5822. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5823. (show-all)
  5824. (unless ga (message "SHOW ALL"))
  5825. (setq org-cycle-global-status 'all)
  5826. (run-hook-with-args 'org-cycle-hook 'all))
  5827. (t
  5828. ;; Default action: go to overview
  5829. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5830. (org-overview)
  5831. (unless ga (message "OVERVIEW"))
  5832. (setq org-cycle-global-status 'overview)
  5833. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5834. (defun org-cycle-internal-local ()
  5835. "Do the local cycling action."
  5836. (let (message-log-max ; Don't populate the *Messages* buffer
  5837. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5838. ;; First, determine end of headline (EOH), end of subtree or item
  5839. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5840. (save-excursion
  5841. (if (org-at-item-p)
  5842. (progn
  5843. (beginning-of-line)
  5844. (setq struct (org-list-struct))
  5845. (setq eoh (point-at-eol))
  5846. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5847. (setq has-children (org-list-has-child-p (point) struct)))
  5848. (org-back-to-heading)
  5849. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5850. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5851. (setq has-children
  5852. (or (save-excursion
  5853. (let ((level (funcall outline-level)))
  5854. (outline-next-heading)
  5855. (and (org-at-heading-p t)
  5856. (> (funcall outline-level) level))))
  5857. (save-excursion
  5858. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5859. ;; Determine end invisible part of buffer (EOL)
  5860. (beginning-of-line 2)
  5861. ;; XEmacs doesn't have `next-single-char-property-change'
  5862. (if (featurep 'xemacs)
  5863. (while (and (not (eobp)) ;; this is like `next-line'
  5864. (get-char-property (1- (point)) 'invisible))
  5865. (beginning-of-line 2))
  5866. (while (and (not (eobp)) ;; this is like `next-line'
  5867. (get-char-property (1- (point)) 'invisible))
  5868. (goto-char (next-single-char-property-change (point) 'invisible))
  5869. (and (eolp) (beginning-of-line 2))))
  5870. (setq eol (point)))
  5871. ;; Find out what to do next and set `this-command'
  5872. (cond
  5873. ((= eos eoh)
  5874. ;; Nothing is hidden behind this heading
  5875. (unless (org-before-first-heading-p)
  5876. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5877. (message "EMPTY ENTRY")
  5878. (setq org-cycle-subtree-status nil)
  5879. (save-excursion
  5880. (goto-char eos)
  5881. (outline-next-heading)
  5882. (if (outline-invisible-p) (org-flag-heading nil))))
  5883. ((and (or (>= eol eos)
  5884. (not (string-match "\\S-" (buffer-substring eol eos))))
  5885. (or has-children
  5886. (not (setq children-skipped
  5887. org-cycle-skip-children-state-if-no-children))))
  5888. ;; Entire subtree is hidden in one line: children view
  5889. (unless (org-before-first-heading-p)
  5890. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5891. (if (org-at-item-p)
  5892. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5893. (org-show-entry)
  5894. (org-with-limited-levels (show-children))
  5895. ;; FIXME: This slows down the func way too much.
  5896. ;; How keep drawers hidden in subtree anyway?
  5897. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5898. ;; (org-cycle-hide-drawers 'subtree))
  5899. ;; Fold every list in subtree to top-level items.
  5900. (when (eq org-cycle-include-plain-lists 'integrate)
  5901. (save-excursion
  5902. (org-back-to-heading)
  5903. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5904. (beginning-of-line 1)
  5905. (let* ((struct (org-list-struct))
  5906. (prevs (org-list-prevs-alist struct))
  5907. (end (org-list-get-bottom-point struct)))
  5908. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5909. (org-list-get-all-items (point) struct prevs))
  5910. (goto-char end))))))
  5911. (message "CHILDREN")
  5912. (save-excursion
  5913. (goto-char eos)
  5914. (outline-next-heading)
  5915. (if (outline-invisible-p) (org-flag-heading nil)))
  5916. (setq org-cycle-subtree-status 'children)
  5917. (unless (org-before-first-heading-p)
  5918. (run-hook-with-args 'org-cycle-hook 'children)))
  5919. ((or children-skipped
  5920. (and (eq last-command this-command)
  5921. (eq org-cycle-subtree-status 'children)))
  5922. ;; We just showed the children, or no children are there,
  5923. ;; now show everything.
  5924. (unless (org-before-first-heading-p)
  5925. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  5926. (outline-flag-region eoh eos nil)
  5927. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5928. (setq org-cycle-subtree-status 'subtree)
  5929. (unless (org-before-first-heading-p)
  5930. (run-hook-with-args 'org-cycle-hook 'subtree)))
  5931. (t
  5932. ;; Default action: hide the subtree.
  5933. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5934. (outline-flag-region eoh eos t)
  5935. (message "FOLDED")
  5936. (setq org-cycle-subtree-status 'folded)
  5937. (unless (org-before-first-heading-p)
  5938. (run-hook-with-args 'org-cycle-hook 'folded))))))
  5939. ;;;###autoload
  5940. (defun org-global-cycle (&optional arg)
  5941. "Cycle the global visibility. For details see `org-cycle'.
  5942. With \\[universal-argument] prefix arg, switch to startup visibility.
  5943. With a numeric prefix, show all headlines up to that level."
  5944. (interactive "P")
  5945. (let ((org-cycle-include-plain-lists
  5946. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5947. (cond
  5948. ((integerp arg)
  5949. (show-all)
  5950. (hide-sublevels arg)
  5951. (setq org-cycle-global-status 'contents))
  5952. ((equal arg '(4))
  5953. (org-set-startup-visibility)
  5954. (message "Startup visibility, plus VISIBILITY properties."))
  5955. (t
  5956. (org-cycle '(4))))))
  5957. (defun org-set-startup-visibility ()
  5958. "Set the visibility required by startup options and properties."
  5959. (cond
  5960. ((eq org-startup-folded t)
  5961. (org-cycle '(4)))
  5962. ((eq org-startup-folded 'content)
  5963. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5964. (org-cycle '(4)) (org-cycle '(4)))))
  5965. (unless (eq org-startup-folded 'showeverything)
  5966. (if org-hide-block-startup (org-hide-block-all))
  5967. (org-set-visibility-according-to-property 'no-cleanup)
  5968. (org-cycle-hide-archived-subtrees 'all)
  5969. (org-cycle-hide-drawers 'all)
  5970. (org-cycle-show-empty-lines t)))
  5971. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5972. "Switch subtree visibilities according to :VISIBILITY: property."
  5973. (interactive)
  5974. (let (org-show-entry-below state)
  5975. (save-excursion
  5976. (goto-char (point-min))
  5977. (while (re-search-forward
  5978. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5979. nil t)
  5980. (setq state (match-string 1))
  5981. (save-excursion
  5982. (org-back-to-heading t)
  5983. (hide-subtree)
  5984. (org-reveal)
  5985. (cond
  5986. ((equal state '("fold" "folded"))
  5987. (hide-subtree))
  5988. ((equal state "children")
  5989. (org-show-hidden-entry)
  5990. (show-children))
  5991. ((equal state "content")
  5992. (save-excursion
  5993. (save-restriction
  5994. (org-narrow-to-subtree)
  5995. (org-content))))
  5996. ((member state '("all" "showall"))
  5997. (show-subtree)))))
  5998. (unless no-cleanup
  5999. (org-cycle-hide-archived-subtrees 'all)
  6000. (org-cycle-hide-drawers 'all)
  6001. (org-cycle-show-empty-lines 'all)))))
  6002. ;; This function uses outline-regexp instead of the more fundamental
  6003. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6004. ;; buffers, where outline-regexp is needed.
  6005. (defun org-overview ()
  6006. "Switch to overview mode, showing only top-level headlines.
  6007. Really, this shows all headlines with level equal or greater than the level
  6008. of the first headline in the buffer. This is important, because if the
  6009. first headline is not level one, then (hide-sublevels 1) gives confusing
  6010. results."
  6011. (interactive)
  6012. (let ((l (org-current-line))
  6013. (level (save-excursion
  6014. (goto-char (point-min))
  6015. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6016. (progn
  6017. (goto-char (match-beginning 0))
  6018. (funcall outline-level))))))
  6019. (and level (hide-sublevels level))
  6020. (recenter '(4))
  6021. (org-goto-line l)))
  6022. (defun org-content (&optional arg)
  6023. "Show all headlines in the buffer, like a table of contents.
  6024. With numerical argument N, show content up to level N."
  6025. (interactive "P")
  6026. (save-excursion
  6027. ;; Visit all headings and show their offspring
  6028. (and (integerp arg) (org-overview))
  6029. (goto-char (point-max))
  6030. (catch 'exit
  6031. (while (and (progn (condition-case nil
  6032. (outline-previous-visible-heading 1)
  6033. (error (goto-char (point-min))))
  6034. t)
  6035. (looking-at org-outline-regexp))
  6036. (if (integerp arg)
  6037. (show-children (1- arg))
  6038. (show-branches))
  6039. (if (bobp) (throw 'exit nil))))))
  6040. (defun org-optimize-window-after-visibility-change (state)
  6041. "Adjust the window after a change in outline visibility.
  6042. This function is the default value of the hook `org-cycle-hook'."
  6043. (when (get-buffer-window (current-buffer))
  6044. (cond
  6045. ((eq state 'content) nil)
  6046. ((eq state 'all) nil)
  6047. ((eq state 'folded) nil)
  6048. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6049. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6050. (defun org-remove-empty-overlays-at (pos)
  6051. "Remove outline overlays that do not contain non-white stuff."
  6052. (mapc
  6053. (lambda (o)
  6054. (and (eq 'outline (overlay-get o 'invisible))
  6055. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6056. (overlay-end o))))
  6057. (delete-overlay o)))
  6058. (overlays-at pos)))
  6059. (defun org-clean-visibility-after-subtree-move ()
  6060. "Fix visibility issues after moving a subtree."
  6061. ;; First, find a reasonable region to look at:
  6062. ;; Start two siblings above, end three below
  6063. (let* ((beg (save-excursion
  6064. (and (org-get-last-sibling)
  6065. (org-get-last-sibling))
  6066. (point)))
  6067. (end (save-excursion
  6068. (and (org-get-next-sibling)
  6069. (org-get-next-sibling)
  6070. (org-get-next-sibling))
  6071. (if (org-at-heading-p)
  6072. (point-at-eol)
  6073. (point))))
  6074. (level (looking-at "\\*+"))
  6075. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6076. (save-excursion
  6077. (save-restriction
  6078. (narrow-to-region beg end)
  6079. (when re
  6080. ;; Properly fold already folded siblings
  6081. (goto-char (point-min))
  6082. (while (re-search-forward re nil t)
  6083. (if (and (not (outline-invisible-p))
  6084. (save-excursion
  6085. (goto-char (point-at-eol)) (outline-invisible-p)))
  6086. (hide-entry))))
  6087. (org-cycle-show-empty-lines 'overview)
  6088. (org-cycle-hide-drawers 'overview)))))
  6089. (defun org-cycle-show-empty-lines (state)
  6090. "Show empty lines above all visible headlines.
  6091. The region to be covered depends on STATE when called through
  6092. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6093. entire buffer covered. Note that an empty line is only shown if there
  6094. are at least `org-cycle-separator-lines' empty lines before the headline."
  6095. (when (not (= org-cycle-separator-lines 0))
  6096. (save-excursion
  6097. (let* ((n (abs org-cycle-separator-lines))
  6098. (re (cond
  6099. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6100. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6101. (t (let ((ns (number-to-string (- n 2))))
  6102. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6103. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6104. beg end b e)
  6105. (cond
  6106. ((memq state '(overview contents t))
  6107. (setq beg (point-min) end (point-max)))
  6108. ((memq state '(children folded))
  6109. (setq beg (point) end (progn (org-end-of-subtree t t)
  6110. (beginning-of-line 2)
  6111. (point)))))
  6112. (when beg
  6113. (goto-char beg)
  6114. (while (re-search-forward re end t)
  6115. (unless (get-char-property (match-end 1) 'invisible)
  6116. (setq e (match-end 1))
  6117. (if (< org-cycle-separator-lines 0)
  6118. (setq b (save-excursion
  6119. (goto-char (match-beginning 0))
  6120. (org-back-over-empty-lines)
  6121. (if (save-excursion
  6122. (goto-char (max (point-min) (1- (point))))
  6123. (org-at-heading-p))
  6124. (1- (point))
  6125. (point))))
  6126. (setq b (match-beginning 1)))
  6127. (outline-flag-region b e nil)))))))
  6128. ;; Never hide empty lines at the end of the file.
  6129. (save-excursion
  6130. (goto-char (point-max))
  6131. (outline-previous-heading)
  6132. (outline-end-of-heading)
  6133. (if (and (looking-at "[ \t\n]+")
  6134. (= (match-end 0) (point-max)))
  6135. (outline-flag-region (point) (match-end 0) nil))))
  6136. (defun org-show-empty-lines-in-parent ()
  6137. "Move to the parent and re-show empty lines before visible headlines."
  6138. (save-excursion
  6139. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6140. (org-cycle-show-empty-lines context))))
  6141. (defun org-files-list ()
  6142. "Return `org-agenda-files' list, plus all open org-mode files.
  6143. This is useful for operations that need to scan all of a user's
  6144. open and agenda-wise Org files."
  6145. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6146. (dolist (buf (buffer-list))
  6147. (with-current-buffer buf
  6148. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6149. (let ((file (expand-file-name (buffer-file-name))))
  6150. (unless (member file files)
  6151. (push file files))))))
  6152. files))
  6153. (defsubst org-entry-beginning-position ()
  6154. "Return the beginning position of the current entry."
  6155. (save-excursion (outline-back-to-heading t) (point)))
  6156. (defsubst org-entry-end-position ()
  6157. "Return the end position of the current entry."
  6158. (save-excursion (outline-next-heading) (point)))
  6159. (defun org-cycle-hide-drawers (state)
  6160. "Re-hide all drawers after a visibility state change."
  6161. (when (and (derived-mode-p 'org-mode)
  6162. (not (memq state '(overview folded contents))))
  6163. (save-excursion
  6164. (let* ((globalp (memq state '(contents all)))
  6165. (beg (if globalp (point-min) (point)))
  6166. (end (if globalp (point-max)
  6167. (if (eq state 'children)
  6168. (save-excursion (outline-next-heading) (point))
  6169. (org-end-of-subtree t)))))
  6170. (goto-char beg)
  6171. (while (re-search-forward org-drawer-regexp end t)
  6172. (org-flag-drawer t))))))
  6173. (defun org-cycle-hide-inline-tasks (state)
  6174. "Re-hide inline task when switching to 'contents visibility state."
  6175. (when (and (eq state 'contents)
  6176. (boundp 'org-inlinetask-min-level)
  6177. org-inlinetask-min-level)
  6178. (hide-sublevels (1- org-inlinetask-min-level))))
  6179. (defun org-flag-drawer (flag)
  6180. "When FLAG is non-nil, hide the drawer we are within.
  6181. Otherwise make it visible."
  6182. (save-excursion
  6183. (beginning-of-line 1)
  6184. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6185. (let ((b (match-end 0)))
  6186. (if (re-search-forward
  6187. "^[ \t]*:END:"
  6188. (save-excursion (outline-next-heading) (point)) t)
  6189. (outline-flag-region b (point-at-eol) flag)
  6190. (error ":END: line missing at position %s" b))))))
  6191. (defun org-subtree-end-visible-p ()
  6192. "Is the end of the current subtree visible?"
  6193. (pos-visible-in-window-p
  6194. (save-excursion (org-end-of-subtree t) (point))))
  6195. (defun org-first-headline-recenter (&optional N)
  6196. "Move cursor to the first headline and recenter the headline.
  6197. Optional argument N means put the headline into the Nth line of the window."
  6198. (goto-char (point-min))
  6199. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6200. (beginning-of-line)
  6201. (recenter (prefix-numeric-value N))))
  6202. ;;; Saving and restoring visibility
  6203. (defun org-outline-overlay-data (&optional use-markers)
  6204. "Return a list of the locations of all outline overlays.
  6205. These are overlays with the `invisible' property value `outline'.
  6206. The return value is a list of cons cells, with start and stop
  6207. positions for each overlay.
  6208. If USE-MARKERS is set, return the positions as markers."
  6209. (let (beg end)
  6210. (save-excursion
  6211. (save-restriction
  6212. (widen)
  6213. (delq nil
  6214. (mapcar (lambda (o)
  6215. (when (eq (overlay-get o 'invisible) 'outline)
  6216. (setq beg (overlay-start o)
  6217. end (overlay-end o))
  6218. (and beg end (> end beg)
  6219. (if use-markers
  6220. (cons (move-marker (make-marker) beg)
  6221. (move-marker (make-marker) end))
  6222. (cons beg end)))))
  6223. (overlays-in (point-min) (point-max))))))))
  6224. (defun org-set-outline-overlay-data (data)
  6225. "Create visibility overlays for all positions in DATA.
  6226. DATA should have been made by `org-outline-overlay-data'."
  6227. (let (o)
  6228. (save-excursion
  6229. (save-restriction
  6230. (widen)
  6231. (show-all)
  6232. (mapc (lambda (c)
  6233. (outline-flag-region (car c) (cdr c) t))
  6234. data)))))
  6235. ;;; Folding of blocks
  6236. (defvar org-hide-block-overlays nil
  6237. "Overlays hiding blocks.")
  6238. (make-variable-buffer-local 'org-hide-block-overlays)
  6239. (defun org-block-map (function &optional start end)
  6240. "Call FUNCTION at the head of all source blocks in the current buffer.
  6241. Optional arguments START and END can be used to limit the range."
  6242. (let ((start (or start (point-min)))
  6243. (end (or end (point-max))))
  6244. (save-excursion
  6245. (goto-char start)
  6246. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6247. (save-excursion
  6248. (save-match-data
  6249. (goto-char (match-beginning 0))
  6250. (funcall function)))))))
  6251. (defun org-hide-block-toggle-all ()
  6252. "Toggle the visibility of all blocks in the current buffer."
  6253. (org-block-map #'org-hide-block-toggle))
  6254. (defun org-hide-block-all ()
  6255. "Fold all blocks in the current buffer."
  6256. (interactive)
  6257. (org-show-block-all)
  6258. (org-block-map #'org-hide-block-toggle-maybe))
  6259. (defun org-show-block-all ()
  6260. "Unfold all blocks in the current buffer."
  6261. (interactive)
  6262. (mapc 'delete-overlay org-hide-block-overlays)
  6263. (setq org-hide-block-overlays nil))
  6264. (defun org-hide-block-toggle-maybe ()
  6265. "Toggle visibility of block at point."
  6266. (interactive)
  6267. (let ((case-fold-search t))
  6268. (if (save-excursion
  6269. (beginning-of-line 1)
  6270. (looking-at org-block-regexp))
  6271. (progn (org-hide-block-toggle)
  6272. t) ;; to signal that we took action
  6273. nil))) ;; to signal that we did not
  6274. (defun org-hide-block-toggle (&optional force)
  6275. "Toggle the visibility of the current block."
  6276. (interactive)
  6277. (save-excursion
  6278. (beginning-of-line)
  6279. (if (re-search-forward org-block-regexp nil t)
  6280. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6281. (end (match-end 0)) ;; end of entire body
  6282. ov)
  6283. (if (memq t (mapcar (lambda (overlay)
  6284. (eq (overlay-get overlay 'invisible)
  6285. 'org-hide-block))
  6286. (overlays-at start)))
  6287. (if (or (not force) (eq force 'off))
  6288. (mapc (lambda (ov)
  6289. (when (member ov org-hide-block-overlays)
  6290. (setq org-hide-block-overlays
  6291. (delq ov org-hide-block-overlays)))
  6292. (when (eq (overlay-get ov 'invisible)
  6293. 'org-hide-block)
  6294. (delete-overlay ov)))
  6295. (overlays-at start)))
  6296. (setq ov (make-overlay start end))
  6297. (overlay-put ov 'invisible 'org-hide-block)
  6298. ;; make the block accessible to isearch
  6299. (overlay-put
  6300. ov 'isearch-open-invisible
  6301. (lambda (ov)
  6302. (when (member ov org-hide-block-overlays)
  6303. (setq org-hide-block-overlays
  6304. (delq ov org-hide-block-overlays)))
  6305. (when (eq (overlay-get ov 'invisible)
  6306. 'org-hide-block)
  6307. (delete-overlay ov))))
  6308. (push ov org-hide-block-overlays)))
  6309. (error "Not looking at a source block"))))
  6310. ;; org-tab-after-check-for-cycling-hook
  6311. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6312. ;; Remove overlays when changing major mode
  6313. (add-hook 'org-mode-hook
  6314. (lambda () (org-add-hook 'change-major-mode-hook
  6315. 'org-show-block-all 'append 'local)))
  6316. ;;; Org-goto
  6317. (defvar org-goto-window-configuration nil)
  6318. (defvar org-goto-marker nil)
  6319. (defvar org-goto-map)
  6320. (defun org-goto-map ()
  6321. "Set the keymap `org-goto'."
  6322. (setq org-goto-map
  6323. (let ((map (make-sparse-keymap)))
  6324. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6325. mouse-drag-region universal-argument org-occur))
  6326. cmd)
  6327. (while (setq cmd (pop cmds))
  6328. (substitute-key-definition cmd cmd map global-map)))
  6329. (suppress-keymap map)
  6330. (org-defkey map "\C-m" 'org-goto-ret)
  6331. (org-defkey map [(return)] 'org-goto-ret)
  6332. (org-defkey map [(left)] 'org-goto-left)
  6333. (org-defkey map [(right)] 'org-goto-right)
  6334. (org-defkey map [(control ?g)] 'org-goto-quit)
  6335. (org-defkey map "\C-i" 'org-cycle)
  6336. (org-defkey map [(tab)] 'org-cycle)
  6337. (org-defkey map [(down)] 'outline-next-visible-heading)
  6338. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6339. (if org-goto-auto-isearch
  6340. (if (fboundp 'define-key-after)
  6341. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6342. nil)
  6343. (org-defkey map "q" 'org-goto-quit)
  6344. (org-defkey map "n" 'outline-next-visible-heading)
  6345. (org-defkey map "p" 'outline-previous-visible-heading)
  6346. (org-defkey map "f" 'outline-forward-same-level)
  6347. (org-defkey map "b" 'outline-backward-same-level)
  6348. (org-defkey map "u" 'outline-up-heading))
  6349. (org-defkey map "/" 'org-occur)
  6350. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6351. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6352. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6353. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6354. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6355. map)))
  6356. (defconst org-goto-help
  6357. "Browse buffer copy, to find location or copy text.%s
  6358. RET=jump to location C-g=quit and return to previous location
  6359. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6360. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6361. (defun org-goto (&optional alternative-interface)
  6362. "Look up a different location in the current file, keeping current visibility.
  6363. When you want look-up or go to a different location in a
  6364. document, the fastest way is often to fold the entire buffer and
  6365. then dive into the tree. This method has the disadvantage, that
  6366. the previous location will be folded, which may not be what you
  6367. want.
  6368. This command works around this by showing a copy of the current
  6369. buffer in an indirect buffer, in overview mode. You can dive
  6370. into the tree in that copy, use org-occur and incremental search
  6371. to find a location. When pressing RET or `Q', the command
  6372. returns to the original buffer in which the visibility is still
  6373. unchanged. After RET it will also jump to the location selected
  6374. in the indirect buffer and expose the headline hierarchy above.
  6375. With a prefix argument, use the alternative interface: e.g. if
  6376. `org-goto-interface' is 'outline use 'outline-path-completion."
  6377. (interactive "P")
  6378. (org-goto-map)
  6379. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6380. (org-refile-use-outline-path t)
  6381. (org-refile-target-verify-function nil)
  6382. (interface
  6383. (if (not alternative-interface)
  6384. org-goto-interface
  6385. (if (eq org-goto-interface 'outline)
  6386. 'outline-path-completion
  6387. 'outline)))
  6388. (org-goto-start-pos (point))
  6389. (selected-point
  6390. (if (eq interface 'outline)
  6391. (car (org-get-location (current-buffer) org-goto-help))
  6392. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6393. (org-refile-check-position pa)
  6394. (nth 3 pa)))))
  6395. (if selected-point
  6396. (progn
  6397. (org-mark-ring-push org-goto-start-pos)
  6398. (goto-char selected-point)
  6399. (if (or (outline-invisible-p) (org-invisible-p2))
  6400. (org-show-context 'org-goto)))
  6401. (message "Quit"))))
  6402. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6403. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6404. (defvar org-goto-local-auto-isearch-map) ; defined below
  6405. (defun org-get-location (buf help)
  6406. "Let the user select a location in the Org-mode buffer BUF.
  6407. This function uses a recursive edit. It returns the selected position
  6408. or nil."
  6409. (org-no-popups
  6410. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6411. (isearch-hide-immediately nil)
  6412. (isearch-search-fun-function
  6413. (lambda () 'org-goto-local-search-headings))
  6414. (org-goto-selected-point org-goto-exit-command))
  6415. (save-excursion
  6416. (save-window-excursion
  6417. (delete-other-windows)
  6418. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6419. (org-pop-to-buffer-same-window
  6420. (condition-case nil
  6421. (make-indirect-buffer (current-buffer) "*org-goto*")
  6422. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6423. (with-output-to-temp-buffer "*Org Help*"
  6424. (princ (format help (if org-goto-auto-isearch
  6425. " Just type for auto-isearch."
  6426. " n/p/f/b/u to navigate, q to quit."))))
  6427. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6428. (setq buffer-read-only nil)
  6429. (let ((org-startup-truncated t)
  6430. (org-startup-folded nil)
  6431. (org-startup-align-all-tables nil))
  6432. (org-mode)
  6433. (org-overview))
  6434. (setq buffer-read-only t)
  6435. (if (and (boundp 'org-goto-start-pos)
  6436. (integer-or-marker-p org-goto-start-pos))
  6437. (let ((org-show-hierarchy-above t)
  6438. (org-show-siblings t)
  6439. (org-show-following-heading t))
  6440. (goto-char org-goto-start-pos)
  6441. (and (outline-invisible-p) (org-show-context)))
  6442. (goto-char (point-min)))
  6443. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6444. (message "Select location and press RET")
  6445. (use-local-map org-goto-map)
  6446. (recursive-edit)))
  6447. (kill-buffer "*org-goto*")
  6448. (cons org-goto-selected-point org-goto-exit-command))))
  6449. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6450. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6451. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6452. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6453. (defun org-goto-local-search-headings (string bound noerror)
  6454. "Search and make sure that any matches are in headlines."
  6455. (catch 'return
  6456. (while (if isearch-forward
  6457. (search-forward string bound noerror)
  6458. (search-backward string bound noerror))
  6459. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6460. (and (member :headline context)
  6461. (not (member :tags context))))
  6462. (throw 'return (point))))))
  6463. (defun org-goto-local-auto-isearch ()
  6464. "Start isearch."
  6465. (interactive)
  6466. (goto-char (point-min))
  6467. (let ((keys (this-command-keys)))
  6468. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6469. (isearch-mode t)
  6470. (isearch-process-search-char (string-to-char keys)))))
  6471. (defun org-goto-ret (&optional arg)
  6472. "Finish `org-goto' by going to the new location."
  6473. (interactive "P")
  6474. (setq org-goto-selected-point (point)
  6475. org-goto-exit-command 'return)
  6476. (throw 'exit nil))
  6477. (defun org-goto-left ()
  6478. "Finish `org-goto' by going to the new location."
  6479. (interactive)
  6480. (if (org-at-heading-p)
  6481. (progn
  6482. (beginning-of-line 1)
  6483. (setq org-goto-selected-point (point)
  6484. org-goto-exit-command 'left)
  6485. (throw 'exit nil))
  6486. (error "Not on a heading")))
  6487. (defun org-goto-right ()
  6488. "Finish `org-goto' by going to the new location."
  6489. (interactive)
  6490. (if (org-at-heading-p)
  6491. (progn
  6492. (setq org-goto-selected-point (point)
  6493. org-goto-exit-command 'right)
  6494. (throw 'exit nil))
  6495. (error "Not on a heading")))
  6496. (defun org-goto-quit ()
  6497. "Finish `org-goto' without cursor motion."
  6498. (interactive)
  6499. (setq org-goto-selected-point nil)
  6500. (setq org-goto-exit-command 'quit)
  6501. (throw 'exit nil))
  6502. ;;; Indirect buffer display of subtrees
  6503. (defvar org-indirect-dedicated-frame nil
  6504. "This is the frame being used for indirect tree display.")
  6505. (defvar org-last-indirect-buffer nil)
  6506. (defun org-tree-to-indirect-buffer (&optional arg)
  6507. "Create indirect buffer and narrow it to current subtree.
  6508. With a numerical prefix ARG, go up to this level and then take that tree.
  6509. If ARG is negative, go up that many levels.
  6510. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6511. indirect buffer previously made with this command, to avoid proliferation of
  6512. indirect buffers. However, when you call the command with a \
  6513. \\[universal-argument] prefix, or
  6514. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6515. is kept so that you can work with several indirect buffers at the same time.
  6516. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6517. \\[universal-argument] prefix also
  6518. requests that a new frame be made for the new buffer, so that the dedicated
  6519. frame is not changed."
  6520. (interactive "P")
  6521. (let ((cbuf (current-buffer))
  6522. (cwin (selected-window))
  6523. (pos (point))
  6524. beg end level heading ibuf)
  6525. (save-excursion
  6526. (org-back-to-heading t)
  6527. (when (numberp arg)
  6528. (setq level (org-outline-level))
  6529. (if (< arg 0) (setq arg (+ level arg)))
  6530. (while (> (setq level (org-outline-level)) arg)
  6531. (org-up-heading-safe)))
  6532. (setq beg (point)
  6533. heading (org-get-heading))
  6534. (org-end-of-subtree t t)
  6535. (if (org-at-heading-p) (backward-char 1))
  6536. (setq end (point)))
  6537. (if (and (buffer-live-p org-last-indirect-buffer)
  6538. (not (eq org-indirect-buffer-display 'new-frame))
  6539. (not arg))
  6540. (kill-buffer org-last-indirect-buffer))
  6541. (setq ibuf (org-get-indirect-buffer cbuf)
  6542. org-last-indirect-buffer ibuf)
  6543. (cond
  6544. ((or (eq org-indirect-buffer-display 'new-frame)
  6545. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6546. (select-frame (make-frame))
  6547. (delete-other-windows)
  6548. (org-pop-to-buffer-same-window ibuf)
  6549. (org-set-frame-title heading))
  6550. ((eq org-indirect-buffer-display 'dedicated-frame)
  6551. (raise-frame
  6552. (select-frame (or (and org-indirect-dedicated-frame
  6553. (frame-live-p org-indirect-dedicated-frame)
  6554. org-indirect-dedicated-frame)
  6555. (setq org-indirect-dedicated-frame (make-frame)))))
  6556. (delete-other-windows)
  6557. (org-pop-to-buffer-same-window ibuf)
  6558. (org-set-frame-title (concat "Indirect: " heading)))
  6559. ((eq org-indirect-buffer-display 'current-window)
  6560. (org-pop-to-buffer-same-window ibuf))
  6561. ((eq org-indirect-buffer-display 'other-window)
  6562. (pop-to-buffer ibuf))
  6563. (t (error "Invalid value")))
  6564. (if (featurep 'xemacs)
  6565. (save-excursion (org-mode) (turn-on-font-lock)))
  6566. (narrow-to-region beg end)
  6567. (show-all)
  6568. (goto-char pos)
  6569. (run-hook-with-args 'org-cycle-hook 'all)
  6570. (and (window-live-p cwin) (select-window cwin))))
  6571. (defun org-get-indirect-buffer (&optional buffer)
  6572. (setq buffer (or buffer (current-buffer)))
  6573. (let ((n 1) (base (buffer-name buffer)) bname)
  6574. (while (buffer-live-p
  6575. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6576. (setq n (1+ n)))
  6577. (condition-case nil
  6578. (make-indirect-buffer buffer bname 'clone)
  6579. (error (make-indirect-buffer buffer bname)))))
  6580. (defun org-set-frame-title (title)
  6581. "Set the title of the current frame to the string TITLE."
  6582. ;; FIXME: how to name a single frame in XEmacs???
  6583. (unless (featurep 'xemacs)
  6584. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6585. ;;;; Structure editing
  6586. ;;; Inserting headlines
  6587. (defun org-previous-line-empty-p (&optional next)
  6588. "Is the previous line a blank line?
  6589. When NEXT is non-nil, check the next line instead."
  6590. (save-excursion
  6591. (and (not (bobp))
  6592. (or (beginning-of-line (if next 2 0)) t)
  6593. (save-match-data
  6594. (looking-at "[ \t]*$")))))
  6595. (defun org-insert-heading (&optional arg invisible-ok)
  6596. "Insert a new heading or item with same depth at point.
  6597. If point is in a plain list and ARG is nil, create a new list item.
  6598. With one universal prefix argument, insert a heading even in lists.
  6599. With two universal prefix arguments, insert the heading at the end
  6600. of the parent subtree.
  6601. If point is at the beginning of a headline, insert a sibling before
  6602. the current headline. If point is not at the beginning, split the line
  6603. and create a new headline with the text in the current line after point
  6604. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6605. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6606. This is important for non-interactive uses of the command."
  6607. (interactive "P")
  6608. (cond
  6609. ((or (= (buffer-size) 0)
  6610. (and (not (save-excursion
  6611. (and (ignore-errors (org-back-to-heading invisible-ok))
  6612. (org-at-heading-p))))
  6613. (or arg (not (org-in-item-p)))))
  6614. (insert
  6615. (if (org-previous-line-empty-p) "" "\n")
  6616. (if (org-in-src-block-p) ",* " "* "))
  6617. (run-hooks 'org-insert-heading-hook))
  6618. ((or arg (not (org-insert-item
  6619. (save-excursion
  6620. (beginning-of-line)
  6621. (looking-at org-list-full-item-re)
  6622. (match-string 3)))))
  6623. (let (begn endn)
  6624. (when (org-buffer-narrowed-p)
  6625. (setq begn (point-min) endn (point-max))
  6626. (widen))
  6627. (let* ((empty-line-p nil)
  6628. (eops (equal arg '(16))) ; insert at end of parent subtree
  6629. (org-insert-heading-respect-content
  6630. (or (not (null arg)) org-insert-heading-respect-content))
  6631. (level nil)
  6632. (on-heading (org-at-heading-p))
  6633. (head (save-excursion
  6634. (condition-case nil
  6635. (progn
  6636. (org-back-to-heading invisible-ok)
  6637. (when (and (not on-heading)
  6638. (featurep 'org-inlinetask)
  6639. (integerp org-inlinetask-min-level)
  6640. (>= (length (match-string 0))
  6641. org-inlinetask-min-level))
  6642. ;; Find a heading level before the inline task
  6643. (while (and (setq level (org-up-heading-safe))
  6644. (>= level org-inlinetask-min-level)))
  6645. (if (org-at-heading-p)
  6646. (org-back-to-heading invisible-ok)
  6647. (error "This should not happen")))
  6648. (unless (and (save-excursion
  6649. (save-match-data
  6650. (org-backward-heading-same-level 1 invisible-ok))
  6651. (= (point) (match-beginning 0)))
  6652. (not (org-previous-line-empty-p t)))
  6653. (setq empty-line-p (org-previous-line-empty-p)))
  6654. (match-string 0))
  6655. (error "* "))))
  6656. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6657. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6658. pos hide-previous previous-pos)
  6659. (if ;; At the beginning of a heading, open a new line for insertiong
  6660. (and (bolp) (org-at-heading-p)
  6661. (not eops)
  6662. (or (bobp)
  6663. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6664. (open-line (if blank 2 1))
  6665. (save-excursion
  6666. (setq previous-pos (point-at-bol))
  6667. (end-of-line)
  6668. (setq hide-previous (outline-invisible-p)))
  6669. (and org-insert-heading-respect-content
  6670. (save-excursion
  6671. (while (outline-invisible-p)
  6672. (org-show-subtree)
  6673. (org-up-heading-safe))))
  6674. (let ((split
  6675. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6676. (save-excursion
  6677. (let ((p (point)))
  6678. (goto-char (point-at-bol))
  6679. (and (looking-at org-complex-heading-regexp)
  6680. (match-beginning 4)
  6681. (> p (match-beginning 4)))))))
  6682. tags pos)
  6683. (cond
  6684. ;; Insert a new line, possibly at end of parent subtree
  6685. (org-insert-heading-respect-content
  6686. (if (not eops)
  6687. (progn
  6688. (org-end-of-subtree nil t)
  6689. (and (looking-at "^\\*") (backward-char 1))
  6690. (while (and (not (bobp))
  6691. ;; Don't delete spaces in empty headlines
  6692. (not (looking-back org-outline-regexp))
  6693. (member (char-before) '(?\ ?\t ?\n)))
  6694. (backward-delete-char 1)))
  6695. (let ((p (point)))
  6696. (org-up-heading-safe)
  6697. (if (= p (point))
  6698. (goto-char (point-max))
  6699. (org-end-of-subtree nil t))))
  6700. (when (featurep 'org-inlinetask)
  6701. (while (and (not (eobp))
  6702. (looking-at "\\(\\*+\\)[ \t]+")
  6703. (>= (length (match-string 1))
  6704. org-inlinetask-min-level))
  6705. (org-end-of-subtree nil t)))
  6706. (or (bolp) (newline))
  6707. (or (org-previous-line-empty-p)
  6708. (and blank (newline)))
  6709. (if (or empty-line-p eops) (open-line 1)))
  6710. ;; Insert a headling containing text after point
  6711. ((org-at-heading-p)
  6712. (when hide-previous
  6713. (show-children)
  6714. (org-show-entry))
  6715. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6716. (setq tags (and (match-end 2) (match-string 2)))
  6717. (and (match-end 1)
  6718. (delete-region (match-beginning 1) (match-end 1)))
  6719. (setq pos (point-at-bol))
  6720. (or split (end-of-line 1))
  6721. (delete-horizontal-space)
  6722. (if (string-match "\\`\\*+\\'"
  6723. (buffer-substring (point-at-bol) (point)))
  6724. (insert " "))
  6725. (newline (if blank 2 1))
  6726. (when tags
  6727. (save-excursion
  6728. (goto-char pos)
  6729. (end-of-line 1)
  6730. (insert " " tags)
  6731. (org-set-tags nil 'align))))
  6732. (t
  6733. (or split (end-of-line 1))
  6734. (newline (if blank 2 1))))))
  6735. (insert head) (just-one-space)
  6736. (setq pos (point))
  6737. (end-of-line 1)
  6738. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6739. (when (and org-insert-heading-respect-content hide-previous)
  6740. (save-excursion
  6741. (goto-char previous-pos)
  6742. (hide-subtree)))
  6743. (when (and begn endn)
  6744. (narrow-to-region (min (point) begn) (max (point) endn)))
  6745. (run-hooks 'org-insert-heading-hook))))))
  6746. (defun org-get-heading (&optional no-tags no-todo)
  6747. "Return the heading of the current entry, without the stars.
  6748. When NO-TAGS is non-nil, don't include tags.
  6749. When NO-TODO is non-nil, don't include TODO keywords."
  6750. (save-excursion
  6751. (org-back-to-heading t)
  6752. (cond
  6753. ((and no-tags no-todo)
  6754. (looking-at org-complex-heading-regexp)
  6755. (match-string 4))
  6756. (no-tags
  6757. (looking-at (concat org-outline-regexp
  6758. "\\(.*?\\)"
  6759. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6760. (match-string 1))
  6761. (no-todo
  6762. (looking-at org-todo-line-regexp)
  6763. (match-string 3))
  6764. (t (looking-at org-heading-regexp)
  6765. (match-string 2)))))
  6766. (defvar orgstruct-mode) ; defined below
  6767. (defun org-heading-components ()
  6768. "Return the components of the current heading.
  6769. This is a list with the following elements:
  6770. - the level as an integer
  6771. - the reduced level, different if `org-odd-levels-only' is set.
  6772. - the TODO keyword, or nil
  6773. - the priority character, like ?A, or nil if no priority is given
  6774. - the headline text itself, or the tags string if no headline text
  6775. - the tags string, or nil."
  6776. (save-excursion
  6777. (org-back-to-heading t)
  6778. (if (let (case-fold-search)
  6779. (looking-at
  6780. (if orgstruct-mode
  6781. org-heading-regexp
  6782. org-complex-heading-regexp)))
  6783. (if orgstruct-mode
  6784. (list (length (match-string 1))
  6785. (org-reduced-level (length (match-string 1)))
  6786. nil
  6787. nil
  6788. (match-string 2)
  6789. nil)
  6790. (list (length (match-string 1))
  6791. (org-reduced-level (length (match-string 1)))
  6792. (org-match-string-no-properties 2)
  6793. (and (match-end 3) (aref (match-string 3) 2))
  6794. (org-match-string-no-properties 4)
  6795. (org-match-string-no-properties 5))))))
  6796. (defun org-get-entry ()
  6797. "Get the entry text, after heading, entire subtree."
  6798. (save-excursion
  6799. (org-back-to-heading t)
  6800. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6801. (defun org-insert-heading-after-current ()
  6802. "Insert a new heading with same level as current, after current subtree."
  6803. (interactive)
  6804. (org-back-to-heading)
  6805. (org-insert-heading)
  6806. (org-move-subtree-down)
  6807. (end-of-line 1))
  6808. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6809. "Insert heading with `org-insert-heading-respect-content' set to t."
  6810. (interactive "P")
  6811. (let ((org-insert-heading-respect-content t))
  6812. (org-insert-heading arg invisible-ok)))
  6813. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6814. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6815. (interactive "P")
  6816. (let ((org-insert-heading-respect-content t))
  6817. (org-insert-todo-heading force-state t)))
  6818. (defun org-insert-todo-heading (arg &optional force-heading)
  6819. "Insert a new heading with the same level and TODO state as current heading.
  6820. If the heading has no TODO state, or if the state is DONE, use the first
  6821. state (TODO by default). Also one prefix arg, force first state. With two
  6822. prefix args, force inserting at the end of the parent subtree."
  6823. (interactive "P")
  6824. (when (or force-heading (not (org-insert-item 'checkbox)))
  6825. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6826. force-heading))
  6827. (save-excursion
  6828. (org-back-to-heading)
  6829. (outline-previous-heading)
  6830. (looking-at org-todo-line-regexp))
  6831. (let*
  6832. ((new-mark-x
  6833. (if (or arg
  6834. (not (match-beginning 2))
  6835. (member (match-string 2) org-done-keywords))
  6836. (car org-todo-keywords-1)
  6837. (match-string 2)))
  6838. (new-mark
  6839. (or
  6840. (run-hook-with-args-until-success
  6841. 'org-todo-get-default-hook new-mark-x nil)
  6842. new-mark-x)))
  6843. (beginning-of-line 1)
  6844. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6845. (if org-treat-insert-todo-heading-as-state-change
  6846. (org-todo new-mark)
  6847. (insert new-mark " "))))
  6848. (when org-provide-todo-statistics
  6849. (org-update-parent-todo-statistics))))
  6850. (defun org-insert-subheading (arg)
  6851. "Insert a new subheading and demote it.
  6852. Works for outline headings and for plain lists alike."
  6853. (interactive "P")
  6854. (org-insert-heading arg)
  6855. (cond
  6856. ((org-at-heading-p) (org-do-demote))
  6857. ((org-at-item-p) (org-indent-item))))
  6858. (defun org-insert-todo-subheading (arg)
  6859. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6860. Works for outline headings and for plain lists alike."
  6861. (interactive "P")
  6862. (org-insert-todo-heading arg)
  6863. (cond
  6864. ((org-at-heading-p) (org-do-demote))
  6865. ((org-at-item-p) (org-indent-item))))
  6866. ;;; Promotion and Demotion
  6867. (defvar org-after-demote-entry-hook nil
  6868. "Hook run after an entry has been demoted.
  6869. The cursor will be at the beginning of the entry.
  6870. When a subtree is being demoted, the hook will be called for each node.")
  6871. (defvar org-after-promote-entry-hook nil
  6872. "Hook run after an entry has been promoted.
  6873. The cursor will be at the beginning of the entry.
  6874. When a subtree is being promoted, the hook will be called for each node.")
  6875. (defun org-promote-subtree ()
  6876. "Promote the entire subtree.
  6877. See also `org-promote'."
  6878. (interactive)
  6879. (save-excursion
  6880. (org-with-limited-levels (org-map-tree 'org-promote)))
  6881. (org-fix-position-after-promote))
  6882. (defun org-demote-subtree ()
  6883. "Demote the entire subtree. See `org-demote'.
  6884. See also `org-promote'."
  6885. (interactive)
  6886. (save-excursion
  6887. (org-with-limited-levels (org-map-tree 'org-demote)))
  6888. (org-fix-position-after-promote))
  6889. (defun org-do-promote ()
  6890. "Promote the current heading higher up the tree.
  6891. If the region is active in `transient-mark-mode', promote all headings
  6892. in the region."
  6893. (interactive)
  6894. (save-excursion
  6895. (if (org-region-active-p)
  6896. (org-map-region 'org-promote (region-beginning) (region-end))
  6897. (org-promote)))
  6898. (org-fix-position-after-promote))
  6899. (defun org-do-demote ()
  6900. "Demote the current heading lower down the tree.
  6901. If the region is active in `transient-mark-mode', demote all headings
  6902. in the region."
  6903. (interactive)
  6904. (save-excursion
  6905. (if (org-region-active-p)
  6906. (org-map-region 'org-demote (region-beginning) (region-end))
  6907. (org-demote)))
  6908. (org-fix-position-after-promote))
  6909. (defun org-fix-position-after-promote ()
  6910. "Make sure that after pro/demotion cursor position is right."
  6911. (let ((pos (point)))
  6912. (when (save-excursion
  6913. (beginning-of-line 1)
  6914. (looking-at org-todo-line-regexp)
  6915. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6916. (cond ((eobp) (insert " "))
  6917. ((eolp) (insert " "))
  6918. ((equal (char-after) ?\ ) (forward-char 1))))))
  6919. (defun org-current-level ()
  6920. "Return the level of the current entry, or nil if before the first headline.
  6921. The level is the number of stars at the beginning of the headline."
  6922. (save-excursion
  6923. (org-with-limited-levels
  6924. (if (ignore-errors (org-back-to-heading t))
  6925. (funcall outline-level)))))
  6926. (defun org-get-previous-line-level ()
  6927. "Return the outline depth of the last headline before the current line.
  6928. Returns 0 for the first headline in the buffer, and nil if before the
  6929. first headline."
  6930. (let ((current-level (org-current-level))
  6931. (prev-level (when (> (line-number-at-pos) 1)
  6932. (save-excursion
  6933. (beginning-of-line 0)
  6934. (org-current-level)))))
  6935. (cond ((null current-level) nil) ; Before first headline
  6936. ((null prev-level) 0) ; At first headline
  6937. (prev-level))))
  6938. (defun org-reduced-level (l)
  6939. "Compute the effective level of a heading.
  6940. This takes into account the setting of `org-odd-levels-only'."
  6941. (cond
  6942. ((zerop l) 0)
  6943. (org-odd-levels-only (1+ (floor (/ l 2))))
  6944. (t l)))
  6945. (defun org-level-increment ()
  6946. "Return the number of stars that will be added or removed at a
  6947. time to headlines when structure editing, based on the value of
  6948. `org-odd-levels-only'."
  6949. (if org-odd-levels-only 2 1))
  6950. (defun org-get-valid-level (level &optional change)
  6951. "Rectify a level change under the influence of `org-odd-levels-only'
  6952. LEVEL is a current level, CHANGE is by how much the level should be
  6953. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6954. even level numbers will become the next higher odd number."
  6955. (if org-odd-levels-only
  6956. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6957. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6958. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6959. (max 1 (+ level (or change 0)))))
  6960. (if (boundp 'define-obsolete-function-alias)
  6961. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6962. (define-obsolete-function-alias 'org-get-legal-level
  6963. 'org-get-valid-level)
  6964. (define-obsolete-function-alias 'org-get-legal-level
  6965. 'org-get-valid-level "23.1")))
  6966. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6967. ;; ̀org-with-limited-levels'
  6968. (defun org-promote ()
  6969. "Promote the current heading higher up the tree.
  6970. If the region is active in `transient-mark-mode', promote all headings
  6971. in the region."
  6972. (org-back-to-heading t)
  6973. (let* ((level (save-match-data (funcall outline-level)))
  6974. (after-change-functions (remove 'flyspell-after-change-function
  6975. after-change-functions))
  6976. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6977. (diff (abs (- level (length up-head) -1))))
  6978. (cond ((and (= level 1) org-called-with-limited-levels
  6979. org-allow-promoting-top-level-subtree)
  6980. (replace-match "# " nil t))
  6981. ((= level 1)
  6982. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6983. (t (replace-match up-head nil t)))
  6984. ;; Fixup tag positioning
  6985. (unless (= level 1)
  6986. (and org-auto-align-tags (org-set-tags nil t))
  6987. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6988. (run-hooks 'org-after-promote-entry-hook)))
  6989. (defun org-demote ()
  6990. "Demote the current heading lower down the tree.
  6991. If the region is active in `transient-mark-mode', demote all headings
  6992. in the region."
  6993. (org-back-to-heading t)
  6994. (let* ((level (save-match-data (funcall outline-level)))
  6995. (after-change-functions (remove 'flyspell-after-change-function
  6996. after-change-functions))
  6997. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6998. (diff (abs (- level (length down-head) -1))))
  6999. (replace-match down-head nil t)
  7000. ;; Fixup tag positioning
  7001. (and org-auto-align-tags (org-set-tags nil t))
  7002. (if org-adapt-indentation (org-fixup-indentation diff))
  7003. (run-hooks 'org-after-demote-entry-hook)))
  7004. (defun org-cycle-level ()
  7005. "Cycle the level of an empty headline through possible states.
  7006. This goes first to child, then to parent, level, then up the hierarchy.
  7007. After top level, it switches back to sibling level."
  7008. (interactive)
  7009. (let ((org-adapt-indentation nil))
  7010. (when (org-point-at-end-of-empty-headline)
  7011. (setq this-command 'org-cycle-level) ; Only needed for caching
  7012. (let ((cur-level (org-current-level))
  7013. (prev-level (org-get-previous-line-level)))
  7014. (cond
  7015. ;; If first headline in file, promote to top-level.
  7016. ((= prev-level 0)
  7017. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7018. do (org-do-promote)))
  7019. ;; If same level as prev, demote one.
  7020. ((= prev-level cur-level)
  7021. (org-do-demote))
  7022. ;; If parent is top-level, promote to top level if not already.
  7023. ((= prev-level 1)
  7024. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7025. do (org-do-promote)))
  7026. ;; If top-level, return to prev-level.
  7027. ((= cur-level 1)
  7028. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7029. do (org-do-demote)))
  7030. ;; If less than prev-level, promote one.
  7031. ((< cur-level prev-level)
  7032. (org-do-promote))
  7033. ;; If deeper than prev-level, promote until higher than
  7034. ;; prev-level.
  7035. ((> cur-level prev-level)
  7036. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7037. do (org-do-promote))))
  7038. t))))
  7039. (defun org-map-tree (fun)
  7040. "Call FUN for every heading underneath the current one."
  7041. (org-back-to-heading)
  7042. (let ((level (funcall outline-level)))
  7043. (save-excursion
  7044. (funcall fun)
  7045. (while (and (progn
  7046. (outline-next-heading)
  7047. (> (funcall outline-level) level))
  7048. (not (eobp)))
  7049. (funcall fun)))))
  7050. (defun org-map-region (fun beg end)
  7051. "Call FUN for every heading between BEG and END."
  7052. (let ((org-ignore-region t))
  7053. (save-excursion
  7054. (setq end (copy-marker end))
  7055. (goto-char beg)
  7056. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7057. (< (point) end))
  7058. (funcall fun))
  7059. (while (and (progn
  7060. (outline-next-heading)
  7061. (< (point) end))
  7062. (not (eobp)))
  7063. (funcall fun)))))
  7064. (defvar org-property-end-re) ; silence byte-compiler
  7065. (defun org-fixup-indentation (diff)
  7066. "Change the indentation in the current entry by DIFF.
  7067. However, if any line in the current entry has no indentation, or if it
  7068. would end up with no indentation after the change, nothing at all is done."
  7069. (save-excursion
  7070. (let ((end (save-excursion (outline-next-heading)
  7071. (point-marker)))
  7072. (prohibit (if (> diff 0)
  7073. "^\\S-"
  7074. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7075. col)
  7076. (unless (save-excursion (end-of-line 1)
  7077. (re-search-forward prohibit end t))
  7078. (while (and (< (point) end)
  7079. (re-search-forward "^[ \t]+" end t))
  7080. (goto-char (match-end 0))
  7081. (setq col (current-column))
  7082. (if (< diff 0) (replace-match ""))
  7083. (org-indent-to-column (+ diff col))))
  7084. (move-marker end nil))))
  7085. (defun org-convert-to-odd-levels ()
  7086. "Convert an org-mode file with all levels allowed to one with odd levels.
  7087. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7088. level 5 etc."
  7089. (interactive)
  7090. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7091. (let ((outline-level 'org-outline-level)
  7092. (org-odd-levels-only nil) n)
  7093. (save-excursion
  7094. (goto-char (point-min))
  7095. (while (re-search-forward "^\\*\\*+ " nil t)
  7096. (setq n (- (length (match-string 0)) 2))
  7097. (while (>= (setq n (1- n)) 0)
  7098. (org-demote))
  7099. (end-of-line 1))))))
  7100. (defun org-convert-to-oddeven-levels ()
  7101. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7102. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7103. file contains a section with an even level, conversion would
  7104. destroy the structure of the file. An error is signaled in this
  7105. case."
  7106. (interactive)
  7107. (goto-char (point-min))
  7108. ;; First check if there are no even levels
  7109. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7110. (org-show-context t)
  7111. (error "Not all levels are odd in this file. Conversion not possible"))
  7112. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7113. (let ((outline-regexp org-outline-regexp)
  7114. (outline-level 'org-outline-level)
  7115. (org-odd-levels-only nil) n)
  7116. (save-excursion
  7117. (goto-char (point-min))
  7118. (while (re-search-forward "^\\*\\*+ " nil t)
  7119. (setq n (/ (1- (length (match-string 0))) 2))
  7120. (while (>= (setq n (1- n)) 0)
  7121. (org-promote))
  7122. (end-of-line 1))))))
  7123. (defun org-tr-level (n)
  7124. "Make N odd if required."
  7125. (if org-odd-levels-only (1+ (/ n 2)) n))
  7126. ;;; Vertical tree motion, cutting and pasting of subtrees
  7127. (defun org-move-subtree-up (&optional arg)
  7128. "Move the current subtree up past ARG headlines of the same level."
  7129. (interactive "p")
  7130. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7131. (defun org-move-subtree-down (&optional arg)
  7132. "Move the current subtree down past ARG headlines of the same level."
  7133. (interactive "p")
  7134. (setq arg (prefix-numeric-value arg))
  7135. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7136. 'org-get-last-sibling))
  7137. (ins-point (make-marker))
  7138. (cnt (abs arg))
  7139. (col (current-column))
  7140. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7141. ;; Select the tree
  7142. (org-back-to-heading)
  7143. (setq beg0 (point))
  7144. (save-excursion
  7145. (setq ne-beg (org-back-over-empty-lines))
  7146. (setq beg (point)))
  7147. (save-match-data
  7148. (save-excursion (outline-end-of-heading)
  7149. (setq folded (outline-invisible-p)))
  7150. (outline-end-of-subtree))
  7151. (outline-next-heading)
  7152. (setq ne-end (org-back-over-empty-lines))
  7153. (setq end (point))
  7154. (goto-char beg0)
  7155. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7156. ;; include less whitespace
  7157. (save-excursion
  7158. (goto-char beg)
  7159. (forward-line (- ne-beg ne-end))
  7160. (setq beg (point))))
  7161. ;; Find insertion point, with error handling
  7162. (while (> cnt 0)
  7163. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7164. (progn (goto-char beg0)
  7165. (user-error "Cannot move past superior level or buffer limit")))
  7166. (setq cnt (1- cnt)))
  7167. (if (> arg 0)
  7168. ;; Moving forward - still need to move over subtree
  7169. (progn (org-end-of-subtree t t)
  7170. (save-excursion
  7171. (org-back-over-empty-lines)
  7172. (or (bolp) (newline)))))
  7173. (setq ne-ins (org-back-over-empty-lines))
  7174. (move-marker ins-point (point))
  7175. (setq txt (buffer-substring beg end))
  7176. (org-save-markers-in-region beg end)
  7177. (delete-region beg end)
  7178. (org-remove-empty-overlays-at beg)
  7179. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7180. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7181. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7182. (let ((bbb (point)))
  7183. (insert-before-markers txt)
  7184. (org-reinstall-markers-in-region bbb)
  7185. (move-marker ins-point bbb))
  7186. (or (bolp) (insert "\n"))
  7187. (setq ins-end (point))
  7188. (goto-char ins-point)
  7189. (org-skip-whitespace)
  7190. (when (and (< arg 0)
  7191. (org-first-sibling-p)
  7192. (> ne-ins ne-beg))
  7193. ;; Move whitespace back to beginning
  7194. (save-excursion
  7195. (goto-char ins-end)
  7196. (let ((kill-whole-line t))
  7197. (kill-line (- ne-ins ne-beg)) (point)))
  7198. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7199. (move-marker ins-point nil)
  7200. (if folded
  7201. (hide-subtree)
  7202. (org-show-entry)
  7203. (show-children)
  7204. (org-cycle-hide-drawers 'children))
  7205. (org-clean-visibility-after-subtree-move)
  7206. ;; move back to the initial column we were at
  7207. (move-to-column col)))
  7208. (defvar org-subtree-clip ""
  7209. "Clipboard for cut and paste of subtrees.
  7210. This is actually only a copy of the kill, because we use the normal kill
  7211. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7212. (defvar org-subtree-clip-folded nil
  7213. "Was the last copied subtree folded?
  7214. This is used to fold the tree back after pasting.")
  7215. (defun org-cut-subtree (&optional n)
  7216. "Cut the current subtree into the clipboard.
  7217. With prefix arg N, cut this many sequential subtrees.
  7218. This is a short-hand for marking the subtree and then cutting it."
  7219. (interactive "p")
  7220. (org-copy-subtree n 'cut))
  7221. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7222. "Cut the current subtree into the clipboard.
  7223. With prefix arg N, cut this many sequential subtrees.
  7224. This is a short-hand for marking the subtree and then copying it.
  7225. If CUT is non-nil, actually cut the subtree.
  7226. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7227. of some markers in the region, even if CUT is non-nil. This is
  7228. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7229. (interactive "p")
  7230. (let (beg end folded (beg0 (point)))
  7231. (if (org-called-interactively-p 'any)
  7232. (org-back-to-heading nil) ; take what looks like a subtree
  7233. (org-back-to-heading t)) ; take what is really there
  7234. (setq beg (point))
  7235. (skip-chars-forward " \t\r\n")
  7236. (save-match-data
  7237. (if nosubtrees
  7238. (outline-next-heading)
  7239. (save-excursion (outline-end-of-heading)
  7240. (setq folded (outline-invisible-p)))
  7241. (condition-case nil
  7242. (org-forward-heading-same-level (1- n) t)
  7243. (error nil))
  7244. (org-end-of-subtree t t)))
  7245. (setq end (point))
  7246. (goto-char beg0)
  7247. (when (> end beg)
  7248. (setq org-subtree-clip-folded folded)
  7249. (when (or cut force-store-markers)
  7250. (org-save-markers-in-region beg end))
  7251. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7252. (setq org-subtree-clip (current-kill 0))
  7253. (message "%s: Subtree(s) with %d characters"
  7254. (if cut "Cut" "Copied")
  7255. (length org-subtree-clip)))))
  7256. (defun org-paste-subtree (&optional level tree for-yank)
  7257. "Paste the clipboard as a subtree, with modification of headline level.
  7258. The entire subtree is promoted or demoted in order to match a new headline
  7259. level.
  7260. If the cursor is at the beginning of a headline, the same level as
  7261. that headline is used to paste the tree.
  7262. If not, the new level is derived from the *visible* headings
  7263. before and after the insertion point, and taken to be the inferior headline
  7264. level of the two. So if the previous visible heading is level 3 and the
  7265. next is level 4 (or vice versa), level 4 will be used for insertion.
  7266. This makes sure that the subtree remains an independent subtree and does
  7267. not swallow low level entries.
  7268. You can also force a different level, either by using a numeric prefix
  7269. argument, or by inserting the heading marker by hand. For example, if the
  7270. cursor is after \"*****\", then the tree will be shifted to level 5.
  7271. If optional TREE is given, use this text instead of the kill ring.
  7272. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7273. move back over whitespace before inserting, and move point to the end of
  7274. the inserted text when done."
  7275. (interactive "P")
  7276. (setq tree (or tree (and kill-ring (current-kill 0))))
  7277. (unless (org-kill-is-subtree-p tree)
  7278. (error "%s"
  7279. (substitute-command-keys
  7280. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7281. (org-with-limited-levels
  7282. (let* ((visp (not (outline-invisible-p)))
  7283. (txt tree)
  7284. (^re_ "\\(\\*+\\)[ \t]*")
  7285. (old-level (if (string-match org-outline-regexp-bol txt)
  7286. (- (match-end 0) (match-beginning 0) 1)
  7287. -1))
  7288. (force-level (cond (level (prefix-numeric-value level))
  7289. ((and (looking-at "[ \t]*$")
  7290. (string-match
  7291. "^\\*+$" (buffer-substring
  7292. (point-at-bol) (point))))
  7293. (- (match-end 1) (match-beginning 1)))
  7294. ((and (bolp)
  7295. (looking-at org-outline-regexp))
  7296. (- (match-end 0) (point) 1))))
  7297. (previous-level (save-excursion
  7298. (condition-case nil
  7299. (progn
  7300. (outline-previous-visible-heading 1)
  7301. (if (looking-at ^re_)
  7302. (- (match-end 0) (match-beginning 0) 1)
  7303. 1))
  7304. (error 1))))
  7305. (next-level (save-excursion
  7306. (condition-case nil
  7307. (progn
  7308. (or (looking-at org-outline-regexp)
  7309. (outline-next-visible-heading 1))
  7310. (if (looking-at ^re_)
  7311. (- (match-end 0) (match-beginning 0) 1)
  7312. 1))
  7313. (error 1))))
  7314. (new-level (or force-level (max previous-level next-level)))
  7315. (shift (if (or (= old-level -1)
  7316. (= new-level -1)
  7317. (= old-level new-level))
  7318. 0
  7319. (- new-level old-level)))
  7320. (delta (if (> shift 0) -1 1))
  7321. (func (if (> shift 0) 'org-demote 'org-promote))
  7322. (org-odd-levels-only nil)
  7323. beg end newend)
  7324. ;; Remove the forced level indicator
  7325. (if force-level
  7326. (delete-region (point-at-bol) (point)))
  7327. ;; Paste
  7328. (beginning-of-line (if (bolp) 1 2))
  7329. (setq beg (point))
  7330. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7331. (insert-before-markers txt)
  7332. (unless (string-match "\n\\'" txt) (insert "\n"))
  7333. (setq newend (point))
  7334. (org-reinstall-markers-in-region beg)
  7335. (setq end (point))
  7336. (goto-char beg)
  7337. (skip-chars-forward " \t\n\r")
  7338. (setq beg (point))
  7339. (if (and (outline-invisible-p) visp)
  7340. (save-excursion (outline-show-heading)))
  7341. ;; Shift if necessary
  7342. (unless (= shift 0)
  7343. (save-restriction
  7344. (narrow-to-region beg end)
  7345. (while (not (= shift 0))
  7346. (org-map-region func (point-min) (point-max))
  7347. (setq shift (+ delta shift)))
  7348. (goto-char (point-min))
  7349. (setq newend (point-max))))
  7350. (when (or (org-called-interactively-p 'interactive) for-yank)
  7351. (message "Clipboard pasted as level %d subtree" new-level))
  7352. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7353. kill-ring
  7354. (eq org-subtree-clip (current-kill 0))
  7355. org-subtree-clip-folded)
  7356. ;; The tree was folded before it was killed/copied
  7357. (hide-subtree))
  7358. (and for-yank (goto-char newend)))))
  7359. (defun org-kill-is-subtree-p (&optional txt)
  7360. "Check if the current kill is an outline subtree, or a set of trees.
  7361. Returns nil if kill does not start with a headline, or if the first
  7362. headline level is not the largest headline level in the tree.
  7363. So this will actually accept several entries of equal levels as well,
  7364. which is OK for `org-paste-subtree'.
  7365. If optional TXT is given, check this string instead of the current kill."
  7366. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7367. (re (org-get-limited-outline-regexp))
  7368. (^re (concat "^" re))
  7369. (start-level (and kill
  7370. (string-match
  7371. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7372. kill)
  7373. (- (match-end 2) (match-beginning 2) 1)))
  7374. (start (1+ (or (match-beginning 2) -1))))
  7375. (if (not start-level)
  7376. (progn
  7377. nil) ;; does not even start with a heading
  7378. (catch 'exit
  7379. (while (setq start (string-match ^re kill (1+ start)))
  7380. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7381. (throw 'exit nil)))
  7382. t))))
  7383. (defvar org-markers-to-move nil
  7384. "Markers that should be moved with a cut-and-paste operation.
  7385. Those markers are stored together with their positions relative to
  7386. the start of the region.")
  7387. (defun org-save-markers-in-region (beg end)
  7388. "Check markers in region.
  7389. If these markers are between BEG and END, record their position relative
  7390. to BEG, so that after moving the block of text, we can put the markers back
  7391. into place.
  7392. This function gets called just before an entry or tree gets cut from the
  7393. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7394. called immediately, to move the markers with the entries."
  7395. (setq org-markers-to-move nil)
  7396. (when (featurep 'org-clock)
  7397. (org-clock-save-markers-for-cut-and-paste beg end))
  7398. (when (featurep 'org-agenda)
  7399. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7400. (defun org-check-and-save-marker (marker beg end)
  7401. "Check if MARKER is between BEG and END.
  7402. If yes, remember the marker and the distance to BEG."
  7403. (when (and (marker-buffer marker)
  7404. (equal (marker-buffer marker) (current-buffer)))
  7405. (if (and (>= marker beg) (< marker end))
  7406. (push (cons marker (- marker beg)) org-markers-to-move))))
  7407. (defun org-reinstall-markers-in-region (beg)
  7408. "Move all remembered markers to their position relative to BEG."
  7409. (mapc (lambda (x)
  7410. (move-marker (car x) (+ beg (cdr x))))
  7411. org-markers-to-move)
  7412. (setq org-markers-to-move nil))
  7413. (defun org-narrow-to-subtree ()
  7414. "Narrow buffer to the current subtree."
  7415. (interactive)
  7416. (save-excursion
  7417. (save-match-data
  7418. (org-with-limited-levels
  7419. (narrow-to-region
  7420. (progn (org-back-to-heading t) (point))
  7421. (progn (org-end-of-subtree t t)
  7422. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7423. (point)))))))
  7424. (defun org-narrow-to-block ()
  7425. "Narrow buffer to the current block."
  7426. (interactive)
  7427. (let* ((case-fold-search t)
  7428. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7429. "^[ \t]*#\\+end_.*")))
  7430. (if blockp
  7431. (narrow-to-region (car blockp) (cdr blockp))
  7432. (error "Not in a block"))))
  7433. (eval-when-compile
  7434. (defvar org-property-drawer-re))
  7435. (defvar org-property-start-re) ;; defined below
  7436. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7437. "Clone the task (subtree) at point N times.
  7438. The clones will be inserted as siblings.
  7439. In interactive use, the user will be prompted for the number of
  7440. clones to be produced. When called with a universal prefix argument
  7441. and if the entry has a timestamp, the user will also be prompted for
  7442. a time shift, which may be a repeater as used in time stamps, for
  7443. example `+3d'.
  7444. When a valid repeater is given and the entry contains any time
  7445. stamps, the clones will become a sequence in time, with time
  7446. stamps in the subtree shifted for each clone produced. If SHIFT
  7447. is nil or the empty string, time stamps will be left alone. The
  7448. ID property of the original subtree is removed.
  7449. If the original subtree did contain time stamps with a repeater,
  7450. the following will happen:
  7451. - the repeater will be removed in each clone
  7452. - an additional clone will be produced, with the current, unshifted
  7453. date(s) in the entry.
  7454. - the original entry will be placed *after* all the clones, with
  7455. repeater intact.
  7456. - the start days in the repeater in the original entry will be shifted
  7457. to past the last clone.
  7458. In this way you can spell out a number of instances of a repeating task,
  7459. and still retain the repeater to cover future instances of the task."
  7460. (interactive "nNumber of clones to produce: ")
  7461. (let ((shift
  7462. (or shift
  7463. (if (and (equal current-prefix-arg '(4))
  7464. (save-excursion
  7465. (re-search-forward org-ts-regexp-both
  7466. (save-excursion
  7467. (org-end-of-subtree t)
  7468. (point)) t)))
  7469. (read-from-minibuffer
  7470. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7471. ""))) ;; No time shift
  7472. (n-no-remove -1)
  7473. (drawer-re org-drawer-regexp)
  7474. beg end template task idprop
  7475. shift-n shift-what doshift nmin nmax)
  7476. (if (not (and (integerp n) (> n 0)))
  7477. (error "Invalid number of replications %s" n))
  7478. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7479. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7480. shift)))
  7481. (error "Invalid shift specification %s" shift))
  7482. (when doshift
  7483. (setq shift-n (string-to-number (match-string 1 shift))
  7484. shift-what (cdr (assoc (match-string 2 shift)
  7485. '(("d" . day) ("w" . week)
  7486. ("m" . month) ("y" . year))))))
  7487. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7488. (setq nmin 1 nmax n)
  7489. (org-back-to-heading t)
  7490. (setq beg (point))
  7491. (setq idprop (org-entry-get nil "ID"))
  7492. (org-end-of-subtree t t)
  7493. (or (bolp) (insert "\n"))
  7494. (setq end (point))
  7495. (setq template (buffer-substring beg end))
  7496. (when (and doshift
  7497. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7498. (delete-region beg end)
  7499. (setq end beg)
  7500. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7501. (goto-char end)
  7502. (loop for n from nmin to nmax do
  7503. ;; prepare clone
  7504. (with-temp-buffer
  7505. (insert template)
  7506. (org-mode)
  7507. (goto-char (point-min))
  7508. (org-show-subtree)
  7509. (and idprop (if org-clone-delete-id
  7510. (org-entry-delete nil "ID")
  7511. (org-id-get-create t)))
  7512. (unless (= n 0)
  7513. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7514. (kill-whole-line))
  7515. (goto-char (point-min))
  7516. (while (re-search-forward drawer-re nil t)
  7517. (mapc (lambda (d)
  7518. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7519. (goto-char (point-min))
  7520. (when doshift
  7521. (while (re-search-forward org-ts-regexp-both nil t)
  7522. (org-timestamp-change (* n shift-n) shift-what))
  7523. (unless (= n n-no-remove)
  7524. (goto-char (point-min))
  7525. (while (re-search-forward org-ts-regexp nil t)
  7526. (save-excursion
  7527. (goto-char (match-beginning 0))
  7528. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7529. (delete-region (match-beginning 1) (match-end 1)))))))
  7530. (setq task (buffer-string)))
  7531. (insert task))
  7532. (goto-char beg)))
  7533. ;;; Outline Sorting
  7534. (defun org-sort (with-case)
  7535. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7536. Optional argument WITH-CASE means sort case-sensitively."
  7537. (interactive "P")
  7538. (cond
  7539. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7540. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7541. (t
  7542. (org-call-with-arg 'org-sort-entries with-case))))
  7543. (defun org-sort-remove-invisible (s)
  7544. "Remove invisible links from string S."
  7545. (remove-text-properties 0 (length s) org-rm-props s)
  7546. (while (string-match org-bracket-link-regexp s)
  7547. (setq s (replace-match (if (match-end 2)
  7548. (match-string 3 s)
  7549. (match-string 1 s)) t t s)))
  7550. (let ((st (format " %s " s)))
  7551. (while (string-match org-emph-re st)
  7552. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7553. (setq s (substring st 1 -1)))
  7554. s)
  7555. (defvar org-priority-regexp) ; defined later in the file
  7556. (defvar org-after-sorting-entries-or-items-hook nil
  7557. "Hook that is run after a bunch of entries or items have been sorted.
  7558. When children are sorted, the cursor is in the parent line when this
  7559. hook gets called. When a region or a plain list is sorted, the cursor
  7560. will be in the first entry of the sorted region/list.")
  7561. (defun org-sort-entries
  7562. (&optional with-case sorting-type getkey-func compare-func property)
  7563. "Sort entries on a certain level of an outline tree.
  7564. If there is an active region, the entries in the region are sorted.
  7565. Else, if the cursor is before the first entry, sort the top-level items.
  7566. Else, the children of the entry at point are sorted.
  7567. Sorting can be alphabetically, numerically, by date/time as given by
  7568. a time stamp, by a property, by priority order, or by a custom function.
  7569. The command prompts for the sorting type unless it has been given to the
  7570. function through the SORTING-TYPE argument, which needs to be a character,
  7571. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7572. precise meaning of each character:
  7573. n Numerically, by converting the beginning of the entry/item to a number.
  7574. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7575. o By order of TODO keywords.
  7576. t By date/time, either the first active time stamp in the entry, or, if
  7577. none exist, by the first inactive one.
  7578. s By the scheduled date/time.
  7579. d By deadline date/time.
  7580. c By creation time, which is assumed to be the first inactive time stamp
  7581. at the beginning of a line.
  7582. p By priority according to the cookie.
  7583. r By the value of a property.
  7584. Capital letters will reverse the sort order.
  7585. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7586. called with point at the beginning of the record. It must return either
  7587. a string or a number that should serve as the sorting key for that record.
  7588. Comparing entries ignores case by default. However, with an optional argument
  7589. WITH-CASE, the sorting considers case as well.
  7590. Sorting is done against the visible part of the headlines, it ignores hidden
  7591. links."
  7592. (interactive "P")
  7593. (let ((case-func (if with-case 'identity 'downcase))
  7594. (cmstr
  7595. ;; The clock marker is lost when using `sort-subr', let's
  7596. ;; store the clocking string.
  7597. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7598. (save-excursion
  7599. (goto-char org-clock-marker)
  7600. (looking-back "^.*") (match-string-no-properties 0))))
  7601. start beg end stars re re2
  7602. txt what tmp)
  7603. ;; Find beginning and end of region to sort
  7604. (cond
  7605. ((org-region-active-p)
  7606. ;; we will sort the region
  7607. (setq end (region-end)
  7608. what "region")
  7609. (goto-char (region-beginning))
  7610. (if (not (org-at-heading-p)) (outline-next-heading))
  7611. (setq start (point)))
  7612. ((or (org-at-heading-p)
  7613. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7614. ;; we will sort the children of the current headline
  7615. (org-back-to-heading)
  7616. (setq start (point)
  7617. end (progn (org-end-of-subtree t t)
  7618. (or (bolp) (insert "\n"))
  7619. (org-back-over-empty-lines)
  7620. (point))
  7621. what "children")
  7622. (goto-char start)
  7623. (show-subtree)
  7624. (outline-next-heading))
  7625. (t
  7626. ;; we will sort the top-level entries in this file
  7627. (goto-char (point-min))
  7628. (or (org-at-heading-p) (outline-next-heading))
  7629. (setq start (point))
  7630. (goto-char (point-max))
  7631. (beginning-of-line 1)
  7632. (when (looking-at ".*?\\S-")
  7633. ;; File ends in a non-white line
  7634. (end-of-line 1)
  7635. (insert "\n"))
  7636. (setq end (point-max))
  7637. (setq what "top-level")
  7638. (goto-char start)
  7639. (show-all)))
  7640. (setq beg (point))
  7641. (if (>= beg end) (error "Nothing to sort"))
  7642. (looking-at "\\(\\*+\\)")
  7643. (setq stars (match-string 1)
  7644. re (concat "^" (regexp-quote stars) " +")
  7645. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7646. txt (buffer-substring beg end))
  7647. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7648. (if (and (not (equal stars "*")) (string-match re2 txt))
  7649. (error "Region to sort contains a level above the first entry"))
  7650. (unless sorting-type
  7651. (message
  7652. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7653. [t]ime [s]cheduled [d]eadline [c]reated
  7654. A/N/P/R/O/F/T/S/D/C means reversed:"
  7655. what)
  7656. (setq sorting-type (read-char-exclusive))
  7657. (unless getkey-func
  7658. (and (= (downcase sorting-type) ?f)
  7659. (setq getkey-func
  7660. (org-icompleting-read "Sort using function: "
  7661. obarray 'fboundp t nil nil))
  7662. (setq getkey-func (intern getkey-func))))
  7663. (and (= (downcase sorting-type) ?r)
  7664. (not property)
  7665. (setq property
  7666. (org-icompleting-read "Property: "
  7667. (mapcar 'list (org-buffer-property-keys t))
  7668. nil t))))
  7669. (message "Sorting entries...")
  7670. (save-restriction
  7671. (narrow-to-region start end)
  7672. (let ((dcst (downcase sorting-type))
  7673. (case-fold-search nil)
  7674. (now (current-time)))
  7675. (sort-subr
  7676. (/= dcst sorting-type)
  7677. ;; This function moves to the beginning character of the "record" to
  7678. ;; be sorted.
  7679. (lambda nil
  7680. (if (re-search-forward re nil t)
  7681. (goto-char (match-beginning 0))
  7682. (goto-char (point-max))))
  7683. ;; This function moves to the last character of the "record" being
  7684. ;; sorted.
  7685. (lambda nil
  7686. (save-match-data
  7687. (condition-case nil
  7688. (outline-forward-same-level 1)
  7689. (error
  7690. (goto-char (point-max))))))
  7691. ;; This function returns the value that gets sorted against.
  7692. (lambda nil
  7693. (cond
  7694. ((= dcst ?n)
  7695. (if (looking-at org-complex-heading-regexp)
  7696. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7697. nil))
  7698. ((= dcst ?a)
  7699. (if (looking-at org-complex-heading-regexp)
  7700. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7701. nil))
  7702. ((= dcst ?t)
  7703. (let ((end (save-excursion (outline-next-heading) (point))))
  7704. (if (or (re-search-forward org-ts-regexp end t)
  7705. (re-search-forward org-ts-regexp-both end t))
  7706. (org-time-string-to-seconds (match-string 0))
  7707. (org-float-time now))))
  7708. ((= dcst ?c)
  7709. (let ((end (save-excursion (outline-next-heading) (point))))
  7710. (if (re-search-forward
  7711. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7712. end t)
  7713. (org-time-string-to-seconds (match-string 0))
  7714. (org-float-time now))))
  7715. ((= dcst ?s)
  7716. (let ((end (save-excursion (outline-next-heading) (point))))
  7717. (if (re-search-forward org-scheduled-time-regexp end t)
  7718. (org-time-string-to-seconds (match-string 1))
  7719. (org-float-time now))))
  7720. ((= dcst ?d)
  7721. (let ((end (save-excursion (outline-next-heading) (point))))
  7722. (if (re-search-forward org-deadline-time-regexp end t)
  7723. (org-time-string-to-seconds (match-string 1))
  7724. (org-float-time now))))
  7725. ((= dcst ?p)
  7726. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7727. (string-to-char (match-string 2))
  7728. org-default-priority))
  7729. ((= dcst ?r)
  7730. (or (org-entry-get nil property) ""))
  7731. ((= dcst ?o)
  7732. (if (looking-at org-complex-heading-regexp)
  7733. (- 9999 (length (member (match-string 2)
  7734. org-todo-keywords-1)))))
  7735. ((= dcst ?f)
  7736. (if getkey-func
  7737. (progn
  7738. (setq tmp (funcall getkey-func))
  7739. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7740. tmp)
  7741. (error "Invalid key function `%s'" getkey-func)))
  7742. (t (error "Invalid sorting type `%c'" sorting-type))))
  7743. nil
  7744. (cond
  7745. ((= dcst ?a) 'string<)
  7746. ((= dcst ?f) compare-func)
  7747. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7748. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7749. ;; Reset the clock marker if needed
  7750. (when cmstr
  7751. (save-excursion
  7752. (goto-char start)
  7753. (search-forward cmstr nil t)
  7754. (move-marker org-clock-marker (point))))
  7755. (message "Sorting entries...done")))
  7756. (defun org-do-sort (table what &optional with-case sorting-type)
  7757. "Sort TABLE of WHAT according to SORTING-TYPE.
  7758. The user will be prompted for the SORTING-TYPE if the call to this
  7759. function does not specify it. WHAT is only for the prompt, to indicate
  7760. what is being sorted. The sorting key will be extracted from
  7761. the car of the elements of the table.
  7762. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7763. (unless sorting-type
  7764. (message
  7765. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7766. what)
  7767. (setq sorting-type (read-char-exclusive)))
  7768. (let ((dcst (downcase sorting-type))
  7769. extractfun comparefun)
  7770. ;; Define the appropriate functions
  7771. (cond
  7772. ((= dcst ?n)
  7773. (setq extractfun 'string-to-number
  7774. comparefun (if (= dcst sorting-type) '< '>)))
  7775. ((= dcst ?a)
  7776. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7777. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7778. comparefun (if (= dcst sorting-type)
  7779. 'string<
  7780. (lambda (a b) (and (not (string< a b))
  7781. (not (string= a b)))))))
  7782. ((= dcst ?t)
  7783. (setq extractfun
  7784. (lambda (x)
  7785. (if (or (string-match org-ts-regexp x)
  7786. (string-match org-ts-regexp-both x))
  7787. (org-float-time
  7788. (org-time-string-to-time (match-string 0 x)))
  7789. 0))
  7790. comparefun (if (= dcst sorting-type) '< '>)))
  7791. (t (error "Invalid sorting type `%c'" sorting-type)))
  7792. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7793. table)
  7794. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7795. ;;; The orgstruct minor mode
  7796. ;; Define a minor mode which can be used in other modes in order to
  7797. ;; integrate the org-mode structure editing commands.
  7798. ;; This is really a hack, because the org-mode structure commands use
  7799. ;; keys which normally belong to the major mode. Here is how it
  7800. ;; works: The minor mode defines all the keys necessary to operate the
  7801. ;; structure commands, but wraps the commands into a function which
  7802. ;; tests if the cursor is currently at a headline or a plain list
  7803. ;; item. If that is the case, the structure command is used,
  7804. ;; temporarily setting many Org-mode variables like regular
  7805. ;; expressions for filling etc. However, when any of those keys is
  7806. ;; used at a different location, function uses `key-binding' to look
  7807. ;; up if the key has an associated command in another currently active
  7808. ;; keymap (minor modes, major mode, global), and executes that
  7809. ;; command. There might be problems if any of the keys is otherwise
  7810. ;; used as a prefix key.
  7811. (defcustom orgstruct-heading-prefix-regexp nil
  7812. "Regexp that matches the custom prefix of Org headlines in
  7813. orgstruct(++)-mode."
  7814. :group 'org
  7815. :version "24.4"
  7816. :package-version '(Org . "8.0")
  7817. :type 'string)
  7818. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7819. (defcustom orgstruct-setup-hook nil
  7820. "Hook run after orgstruct-mode-map is filled."
  7821. :group 'org
  7822. :version "24.4"
  7823. :package-version '(Org . "8.0")
  7824. :type 'hook)
  7825. (defvar orgstruct-initialized nil)
  7826. (defvar org-local-vars nil
  7827. "List of local variables, for use by `orgstruct-mode'.")
  7828. ;;;###autoload
  7829. (define-minor-mode orgstruct-mode
  7830. "Toggle the minor mode `orgstruct-mode'.
  7831. This mode is for using Org-mode structure commands in other
  7832. modes. The following keys behave as if Org-mode were active, if
  7833. the cursor is on a headline, or on a plain list item (both as
  7834. defined by Org-mode)."
  7835. nil " OrgStruct" (make-sparse-keymap)
  7836. (funcall (if orgstruct-mode
  7837. 'add-to-invisibility-spec
  7838. 'remove-from-invisibility-spec)
  7839. '(outline . t))
  7840. (when orgstruct-mode
  7841. (org-load-modules-maybe)
  7842. (unless orgstruct-initialized
  7843. (orgstruct-setup)
  7844. (setq orgstruct-initialized t))))
  7845. ;;;###autoload
  7846. (defun turn-on-orgstruct ()
  7847. "Unconditionally turn on `orgstruct-mode'."
  7848. (orgstruct-mode 1))
  7849. (defvar org-fb-vars nil)
  7850. (make-variable-buffer-local 'org-fb-vars)
  7851. (defun orgstruct++-mode (&optional arg)
  7852. "Toggle `orgstruct-mode', the enhanced version of it.
  7853. In addition to setting orgstruct-mode, this also exports all
  7854. indentation and autofilling variables from org-mode into the
  7855. buffer. It will also recognize item context in multiline items."
  7856. (interactive "P")
  7857. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7858. (if (< arg 1)
  7859. (progn (orgstruct-mode -1)
  7860. (mapc (lambda(v)
  7861. (org-set-local (car v)
  7862. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7863. org-fb-vars))
  7864. (orgstruct-mode 1)
  7865. (setq org-fb-vars nil)
  7866. (let (var val)
  7867. (mapc
  7868. (lambda (x)
  7869. (when (string-match
  7870. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7871. (symbol-name (car x)))
  7872. (setq var (car x) val (nth 1 x))
  7873. (push (list var `(quote ,(eval var))) org-fb-vars)
  7874. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7875. org-local-vars)
  7876. (org-set-local 'orgstruct-is-++ t))))
  7877. (defvar orgstruct-is-++ nil
  7878. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7879. (make-variable-buffer-local 'orgstruct-is-++)
  7880. ;;;###autoload
  7881. (defun turn-on-orgstruct++ ()
  7882. "Unconditionally turn on `orgstruct++-mode'."
  7883. (orgstruct++-mode 1))
  7884. (defun orgstruct-error ()
  7885. "Error when there is no default binding for a structure key."
  7886. (interactive)
  7887. (funcall (if (fboundp 'user-error)
  7888. 'user-error
  7889. 'error)
  7890. "This key has no function outside structure elements"))
  7891. (defun orgstruct-setup ()
  7892. "Setup orgstruct keymap."
  7893. (dolist (cell '((org-demote . t)
  7894. (org-metaleft . t)
  7895. (org-metaright . t)
  7896. (org-promote . t)
  7897. (org-shiftmetaleft . t)
  7898. (org-shiftmetaright . t)
  7899. org-backward-element
  7900. org-backward-heading-same-level
  7901. org-ctrl-c-ret
  7902. org-ctrl-c-minus
  7903. org-ctrl-c-star
  7904. org-cycle
  7905. org-forward-heading-same-level
  7906. org-insert-heading
  7907. org-insert-heading-respect-content
  7908. org-kill-note-or-show-branches
  7909. org-mark-subtree
  7910. org-meta-return
  7911. org-metadown
  7912. org-metaup
  7913. org-narrow-to-subtree
  7914. org-promote-subtree
  7915. org-reveal
  7916. org-shiftdown
  7917. org-shiftleft
  7918. org-shiftmetadown
  7919. org-shiftmetaup
  7920. org-shiftright
  7921. org-shifttab
  7922. org-shifttab
  7923. org-shiftup
  7924. org-show-subtree
  7925. org-sort
  7926. org-up-element
  7927. outline-demote
  7928. outline-next-visible-heading
  7929. outline-previous-visible-heading
  7930. outline-promote
  7931. outline-up-heading
  7932. show-children))
  7933. (let ((f (or (car-safe cell) cell))
  7934. (disable-when-heading-prefix (cdr-safe cell)))
  7935. (when (fboundp f)
  7936. (dolist (binding (nconc (where-is-internal f org-mode-map)
  7937. (where-is-internal f outline-mode-map)))
  7938. ;; TODO use local-function-key-map
  7939. (dolist (rep '(("<tab>" . "TAB")
  7940. ("<return>" . "RET")
  7941. ("<escape>" . "ESC")
  7942. ("<delete>" . "DEL")))
  7943. (setq binding (read-kbd-macro
  7944. (let ((case-fold-search))
  7945. (replace-regexp-in-string
  7946. (regexp-quote (cdr rep))
  7947. (car rep)
  7948. (key-description binding))))))
  7949. (let ((key (lookup-key orgstruct-mode-map binding)))
  7950. (when (or (not key) (numberp key))
  7951. (condition-case nil
  7952. (org-defkey orgstruct-mode-map
  7953. binding
  7954. (orgstruct-make-binding f binding disable-when-heading-prefix))
  7955. (error nil))))))))
  7956. (run-hooks 'orgstruct-setup-hook))
  7957. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  7958. "Create a function for binding in the structure minor mode.
  7959. FUN is the command to call inside a table. KEY is the key that
  7960. should be checked in for a command to execute outside of tables.
  7961. Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
  7962. if `orgstruct-heading-prefix-regexp' is non-nil."
  7963. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  7964. (let ((nname name)
  7965. (i 0))
  7966. (while (fboundp (intern nname))
  7967. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  7968. (setq name (intern nname)))
  7969. (eval
  7970. (let ((bindings '((org-heading-regexp
  7971. (concat "^"
  7972. orgstruct-heading-prefix-regexp
  7973. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  7974. (org-outline-regexp
  7975. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  7976. (org-outline-regexp-bol
  7977. (concat "^" org-outline-regexp))
  7978. (outline-regexp org-outline-regexp)
  7979. (outline-heading-end-regexp "\n")
  7980. (outline-level 'org-outline-level)
  7981. (outline-heading-alist))))
  7982. `(defun ,name (arg)
  7983. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  7984. "Outside of structure, run the binding of `"
  7985. (key-description key) "'."
  7986. (when disable-when-heading-prefix
  7987. (concat
  7988. "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
  7989. "back to the default binding due to limitations of Org's implementation of\n"
  7990. "`" (symbol-name fun) "'.")))
  7991. (interactive "p")
  7992. (let* ((disable
  7993. ,(when disable-when-heading-prefix
  7994. '(and orgstruct-heading-prefix-regexp
  7995. (not (string= orgstruct-heading-prefix-regexp "")))))
  7996. (fallback
  7997. (or disable
  7998. (not
  7999. (let* ,bindings
  8000. (org-context-p 'headline 'item
  8001. ,(when (memq fun '(org-insert-heading))
  8002. '(when orgstruct-is-++
  8003. 'item-body))))))))
  8004. (if fallback
  8005. (let* ((orgstruct-mode)
  8006. (binding
  8007. (loop with key = ,key
  8008. for rep in '(nil
  8009. ("<tab>" . "TAB")
  8010. ("<return>" . "RET")
  8011. ("<escape>" . "ESC")
  8012. ("<delete>" . "DEL"))
  8013. do
  8014. (when rep
  8015. (setq key (read-kbd-macro
  8016. (let ((case-fold-search))
  8017. (replace-regexp-in-string
  8018. (regexp-quote (car rep))
  8019. (cdr rep)
  8020. (key-description key))))))
  8021. thereis (key-binding key))))
  8022. (if (keymapp binding)
  8023. (set-temporary-overlay-map binding)
  8024. (let ((func (or binding
  8025. (unless disable
  8026. 'orgstruct-error))))
  8027. (when func
  8028. (call-interactively func)))))
  8029. (org-run-like-in-org-mode
  8030. (lambda ()
  8031. (interactive)
  8032. (let* ,bindings
  8033. (call-interactively ',fun)))))))))
  8034. name))
  8035. (defun org-contextualize-keys (alist contexts)
  8036. "Return valid elements in ALIST depending on CONTEXTS.
  8037. `org-agenda-custom-commands' or `org-capture-templates' are the
  8038. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8039. or `org-capture-templates-contexts' are the associated contexts
  8040. definitions."
  8041. (let ((contexts
  8042. ;; normalize contexts
  8043. (mapcar
  8044. (lambda(c) (cond ((listp (cadr c))
  8045. (list (car c) (car c) (cadr c)))
  8046. ((string= "" (cadr c))
  8047. (list (car c) (car c) (caddr c)))
  8048. (t c))) contexts))
  8049. (a alist) c r s)
  8050. ;; loop over all commands or templates
  8051. (while (setq c (pop a))
  8052. (let (vrules repl)
  8053. (cond
  8054. ((not (assoc (car c) contexts))
  8055. (push c r))
  8056. ((and (assoc (car c) contexts)
  8057. (setq vrules (org-contextualize-validate-key
  8058. (car c) contexts)))
  8059. (mapc (lambda (vr)
  8060. (when (not (equal (car vr) (cadr vr)))
  8061. (setq repl vr))) vrules)
  8062. (if (not repl) (push c r)
  8063. (push (cadr repl) s)
  8064. (push
  8065. (cons (car c)
  8066. (cdr (or (assoc (cadr repl) alist)
  8067. (error "Undefined key `%s' as contextual replacement for `%s'"
  8068. (cadr repl) (car c)))))
  8069. r))))))
  8070. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8071. (delq
  8072. nil
  8073. (delete-dups
  8074. (mapcar (lambda (x)
  8075. (let ((tpl (car x)))
  8076. (when (not (delq
  8077. nil
  8078. (mapcar (lambda(y)
  8079. (equal y tpl)) s))) x)))
  8080. (reverse r))))))
  8081. (defun org-contextualize-validate-key (key contexts)
  8082. "Check CONTEXTS for agenda or capture KEY."
  8083. (let (r rr res)
  8084. (while (setq r (pop contexts))
  8085. (mapc
  8086. (lambda (rr)
  8087. (when
  8088. (and (equal key (car r))
  8089. (if (functionp rr) (funcall rr)
  8090. (or (and (eq (car rr) 'in-file)
  8091. (buffer-file-name)
  8092. (string-match (cdr rr) (buffer-file-name)))
  8093. (and (eq (car rr) 'in-mode)
  8094. (string-match (cdr rr) (symbol-name major-mode)))
  8095. (and (eq (car rr) 'in-buffer)
  8096. (string-match (cdr rr) (buffer-name)))
  8097. (when (and (eq (car rr) 'not-in-file)
  8098. (buffer-file-name))
  8099. (not (string-match (cdr rr) (buffer-file-name))))
  8100. (when (eq (car rr) 'not-in-mode)
  8101. (not (string-match (cdr rr) (symbol-name major-mode))))
  8102. (when (eq (car rr) 'not-in-buffer)
  8103. (not (string-match (cdr rr) (buffer-name)))))))
  8104. (push r res)))
  8105. (car (last r))))
  8106. (delete-dups (delq nil res))))
  8107. (defun org-context-p (&rest contexts)
  8108. "Check if local context is any of CONTEXTS.
  8109. Possible values in the list of contexts are `table', `headline', and `item'."
  8110. (let ((pos (point)))
  8111. (goto-char (point-at-bol))
  8112. (prog1 (or (and (memq 'table contexts)
  8113. (looking-at "[ \t]*|"))
  8114. (and (memq 'headline contexts)
  8115. (looking-at org-outline-regexp))
  8116. (and (memq 'item contexts)
  8117. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8118. (and (memq 'item-body contexts)
  8119. (org-in-item-p)))
  8120. (goto-char pos))))
  8121. (defun org-get-local-variables ()
  8122. "Return a list of all local variables in an Org mode buffer."
  8123. (let (varlist)
  8124. (with-current-buffer (get-buffer-create "*Org tmp*")
  8125. (erase-buffer)
  8126. (org-mode)
  8127. (setq varlist (buffer-local-variables)))
  8128. (kill-buffer "*Org tmp*")
  8129. (delq nil
  8130. (mapcar
  8131. (lambda (x)
  8132. (setq x
  8133. (if (symbolp x)
  8134. (list x)
  8135. (list (car x) (cdr x))))
  8136. (if (and (not (get (car x) 'org-state))
  8137. (string-match
  8138. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8139. (symbol-name (car x))))
  8140. x nil))
  8141. varlist))))
  8142. (defun org-clone-local-variables (from-buffer &optional regexp)
  8143. "Clone local variables from FROM-BUFFER.
  8144. Optional argument REGEXP selects variables to clone."
  8145. (mapc
  8146. (lambda (pair)
  8147. (and (symbolp (car pair))
  8148. (or (null regexp)
  8149. (string-match regexp (symbol-name (car pair))))
  8150. (set (make-local-variable (car pair))
  8151. (cdr pair))))
  8152. (buffer-local-variables from-buffer)))
  8153. ;;;###autoload
  8154. (defun org-run-like-in-org-mode (cmd)
  8155. "Run a command, pretending that the current buffer is in Org-mode.
  8156. This will temporarily bind local variables that are typically bound in
  8157. Org-mode to the values they have in Org-mode, and then interactively
  8158. call CMD."
  8159. (org-load-modules-maybe)
  8160. (unless org-local-vars
  8161. (setq org-local-vars (org-get-local-variables)))
  8162. (let (binds)
  8163. (dolist (var org-local-vars)
  8164. (when (or (not (boundp (car var)))
  8165. (eq (symbol-value (car var))
  8166. (default-value (car var))))
  8167. (push (list (car var) `(quote ,(cadr var))) binds)))
  8168. (eval `(let ,binds
  8169. (call-interactively (quote ,cmd))))))
  8170. ;;;; Archiving
  8171. (defun org-get-category (&optional pos force-refresh)
  8172. "Get the category applying to position POS."
  8173. (save-match-data
  8174. (if force-refresh (org-refresh-category-properties))
  8175. (let ((pos (or pos (point))))
  8176. (or (get-text-property pos 'org-category)
  8177. (progn (org-refresh-category-properties)
  8178. (get-text-property pos 'org-category))))))
  8179. (defun org-refresh-category-properties ()
  8180. "Refresh category text properties in the buffer."
  8181. (let ((case-fold-search t)
  8182. (inhibit-read-only t)
  8183. (def-cat (cond
  8184. ((null org-category)
  8185. (if buffer-file-name
  8186. (file-name-sans-extension
  8187. (file-name-nondirectory buffer-file-name))
  8188. "???"))
  8189. ((symbolp org-category) (symbol-name org-category))
  8190. (t org-category)))
  8191. beg end cat pos optionp)
  8192. (org-with-silent-modifications
  8193. (save-excursion
  8194. (save-restriction
  8195. (widen)
  8196. (goto-char (point-min))
  8197. (put-text-property (point) (point-max) 'org-category def-cat)
  8198. (while (re-search-forward
  8199. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8200. (setq pos (match-end 0)
  8201. optionp (equal (char-after (match-beginning 0)) ?#)
  8202. cat (org-trim (match-string 2)))
  8203. (if optionp
  8204. (setq beg (point-at-bol) end (point-max))
  8205. (org-back-to-heading t)
  8206. (setq beg (point) end (org-end-of-subtree t t)))
  8207. (put-text-property beg end 'org-category cat)
  8208. (put-text-property beg end 'org-category-position beg)
  8209. (goto-char pos)))))))
  8210. (defun org-refresh-properties (dprop tprop)
  8211. "Refresh buffer text properties.
  8212. DPROP is the drawer property and TPROP is the corresponding text
  8213. property to set."
  8214. (let ((case-fold-search t)
  8215. (inhibit-read-only t) p)
  8216. (org-with-silent-modifications
  8217. (save-excursion
  8218. (save-restriction
  8219. (widen)
  8220. (goto-char (point-min))
  8221. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8222. (setq p (org-match-string-no-properties 1))
  8223. (save-excursion
  8224. (org-back-to-heading t)
  8225. (put-text-property
  8226. (point-at-bol) (point-at-eol) tprop p))))))))
  8227. ;;;; Link Stuff
  8228. ;;; Link abbreviations
  8229. (defun org-link-expand-abbrev (link)
  8230. "Apply replacements as defined in `org-link-abbrev-alist'."
  8231. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8232. (let* ((key (match-string 1 link))
  8233. (as (or (assoc key org-link-abbrev-alist-local)
  8234. (assoc key org-link-abbrev-alist)))
  8235. (tag (and (match-end 2) (match-string 3 link)))
  8236. rpl)
  8237. (if (not as)
  8238. link
  8239. (setq rpl (cdr as))
  8240. (cond
  8241. ((symbolp rpl) (funcall rpl tag))
  8242. ((string-match "%(\\([^)]+\\))" rpl)
  8243. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  8244. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8245. ((string-match "%h" rpl)
  8246. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8247. (t (concat rpl tag)))))
  8248. link))
  8249. ;;; Storing and inserting links
  8250. (defvar org-insert-link-history nil
  8251. "Minibuffer history for links inserted with `org-insert-link'.")
  8252. (defvar org-stored-links nil
  8253. "Contains the links stored with `org-store-link'.")
  8254. (defvar org-store-link-plist nil
  8255. "Plist with info about the most recently link created with `org-store-link'.")
  8256. (defvar org-link-protocols nil
  8257. "Link protocols added to Org-mode using `org-add-link-type'.")
  8258. (defvar org-store-link-functions nil
  8259. "List of functions that are called to create and store a link.
  8260. Each function will be called in turn until one returns a non-nil
  8261. value. Each function should check if it is responsible for creating
  8262. this link (for example by looking at the major mode).
  8263. If not, it must exit and return nil.
  8264. If yes, it should return a non-nil value after a calling
  8265. `org-store-link-props' with a list of properties and values.
  8266. Special properties are:
  8267. :type The link prefix, like \"http\". This must be given.
  8268. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8269. This is obligatory as well.
  8270. :description Optional default description for the second pair
  8271. of brackets in an Org-mode link. The user can still change
  8272. this when inserting this link into an Org-mode buffer.
  8273. In addition to these, any additional properties can be specified
  8274. and then used in capture templates.")
  8275. (defun org-add-link-type (type &optional follow export)
  8276. "Add TYPE to the list of `org-link-types'.
  8277. Re-compute all regular expressions depending on `org-link-types'
  8278. FOLLOW and EXPORT are two functions.
  8279. FOLLOW should take the link path as the single argument and do whatever
  8280. is necessary to follow the link, for example find a file or display
  8281. a mail message.
  8282. EXPORT should format the link path for export to one of the export formats.
  8283. It should be a function accepting three arguments:
  8284. path the path of the link, the text after the prefix (like \"http:\")
  8285. desc the description of the link, if any, or a description added by
  8286. org-export-normalize-links if there is none
  8287. format the export format, a symbol like `html' or `latex' or `ascii'..
  8288. The function may use the FORMAT information to return different values
  8289. depending on the format. The return value will be put literally into
  8290. the exported file. If the return value is nil, this means Org should
  8291. do what it normally does with links which do not have EXPORT defined.
  8292. Org-mode has a built-in default for exporting links. If you are happy with
  8293. this default, there is no need to define an export function for the link
  8294. type. For a simple example of an export function, see `org-bbdb.el'."
  8295. (add-to-list 'org-link-types type t)
  8296. (org-make-link-regexps)
  8297. (if (assoc type org-link-protocols)
  8298. (setcdr (assoc type org-link-protocols) (list follow export))
  8299. (push (list type follow export) org-link-protocols)))
  8300. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8301. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8302. ;;;###autoload
  8303. (defun org-store-link (arg &optional ignore-region)
  8304. "\\<org-mode-map>Store an org-link to the current location.
  8305. This link is added to `org-stored-links' and can later be inserted
  8306. into an org-buffer with \\[org-insert-link].
  8307. For some link types, a prefix arg is interpreted.
  8308. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8309. For file links, arg negates `org-context-in-file-links'.
  8310. A double prefix arg force skipping storing functions that are not
  8311. part of Org's core.
  8312. When the region is active and IGNORE-REGION is nil, store each line
  8313. in the region as a separate link."
  8314. (interactive "P")
  8315. (org-load-modules-maybe)
  8316. (setq org-store-link-plist nil) ; reset
  8317. (if (and (org-region-active-p) (not ignore-region))
  8318. (save-excursion
  8319. (let ((beg (region-beginning)) (end (region-end)))
  8320. (goto-char beg)
  8321. (while (< (point-at-eol) end)
  8322. (funcall 'org-store-link arg t)
  8323. (move-beginning-of-line 2))))
  8324. (org-with-limited-levels
  8325. (let (link cpltxt desc description search txt custom-id agenda-link sfuns sfunsn)
  8326. (cond
  8327. ((and (not (equal arg '(16)))
  8328. (setq sfuns
  8329. (delq
  8330. nil (mapcar (lambda (f) (let (fs) (if (funcall f) (push f fs))))
  8331. org-store-link-functions))
  8332. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8333. (or (and (cdr sfuns)
  8334. (funcall (intern
  8335. (completing-read "Which function for creating the link? "
  8336. sfunsn t (car sfunsn)))))
  8337. (funcall (caar sfuns)))
  8338. (setq link (plist-get org-store-link-plist :link)
  8339. desc (or (plist-get org-store-link-plist :description) link))))
  8340. ((org-src-edit-buffer-p)
  8341. (let (label gc)
  8342. (while (or (not label)
  8343. (save-excursion
  8344. (save-restriction
  8345. (widen)
  8346. (goto-char (point-min))
  8347. (re-search-forward
  8348. (regexp-quote (format org-coderef-label-format label))
  8349. nil t))))
  8350. (when label (message "Label exists already") (sit-for 2))
  8351. (setq label (read-string "Code line label: " label)))
  8352. (end-of-line 1)
  8353. (setq link (format org-coderef-label-format label))
  8354. (setq gc (- 79 (length link)))
  8355. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8356. (insert link)
  8357. (setq link (concat "(" label ")") desc nil)))
  8358. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8359. ;; We are in the agenda, link to referenced location
  8360. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8361. (get-text-property (point) 'org-marker))))
  8362. (when m
  8363. (org-with-point-at m
  8364. (setq agenda-link
  8365. (if (org-called-interactively-p 'any)
  8366. (call-interactively 'org-store-link)
  8367. (org-store-link nil)))))))
  8368. ((eq major-mode 'calendar-mode)
  8369. (let ((cd (calendar-cursor-to-date)))
  8370. (setq link
  8371. (format-time-string
  8372. (car org-time-stamp-formats)
  8373. (apply 'encode-time
  8374. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8375. nil nil nil))))
  8376. (org-store-link-props :type "calendar" :date cd)))
  8377. ((eq major-mode 'help-mode)
  8378. (setq link (concat "help:" (save-excursion
  8379. (goto-char (point-min))
  8380. (looking-at "^[^ ]+")
  8381. (match-string 0))))
  8382. (org-store-link-props :type "help"))
  8383. ((eq major-mode 'w3-mode)
  8384. (setq cpltxt (if (and (buffer-name)
  8385. (not (string-match "Untitled" (buffer-name))))
  8386. (buffer-name)
  8387. (url-view-url t))
  8388. link (url-view-url t))
  8389. (org-store-link-props :type "w3" :url (url-view-url t)))
  8390. ((setq search (run-hook-with-args-until-success
  8391. 'org-create-file-search-functions))
  8392. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8393. "::" search))
  8394. (setq cpltxt (or description link)))
  8395. ((eq major-mode 'image-mode)
  8396. (setq cpltxt (concat "file:"
  8397. (abbreviate-file-name buffer-file-name))
  8398. link cpltxt)
  8399. (org-store-link-props :type "image" :file buffer-file-name))
  8400. ((eq major-mode 'dired-mode)
  8401. ;; link to the file in the current line
  8402. (let ((file (dired-get-filename nil t)))
  8403. (setq file (if file
  8404. (abbreviate-file-name
  8405. (expand-file-name (dired-get-filename nil t)))
  8406. ;; otherwise, no file so use current directory.
  8407. default-directory))
  8408. (setq cpltxt (concat "file:" file)
  8409. link cpltxt)))
  8410. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8411. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8412. (cond
  8413. ((org-in-regexp "<<\\(.*?\\)>>")
  8414. (setq cpltxt
  8415. (concat "file:"
  8416. (abbreviate-file-name
  8417. (buffer-file-name (buffer-base-buffer)))
  8418. "::" (match-string 1))
  8419. link cpltxt))
  8420. ((and (featurep 'org-id)
  8421. (or (eq org-id-link-to-org-use-id t)
  8422. (and (org-called-interactively-p 'any)
  8423. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8424. (and (eq org-id-link-to-org-use-id
  8425. 'create-if-interactive-and-no-custom-id)
  8426. (not custom-id))))
  8427. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8428. ;; We can make a link using the ID.
  8429. (setq link (condition-case nil
  8430. (prog1 (org-id-store-link)
  8431. (setq desc (plist-get org-store-link-plist :description)))
  8432. (error
  8433. ;; probably before first headline, link to file only
  8434. (concat "file:"
  8435. (abbreviate-file-name
  8436. (buffer-file-name (buffer-base-buffer))))))))
  8437. (t
  8438. ;; Just link to current headline
  8439. (setq cpltxt (concat "file:"
  8440. (abbreviate-file-name
  8441. (buffer-file-name (buffer-base-buffer)))))
  8442. ;; Add a context search string
  8443. (when (org-xor org-context-in-file-links arg)
  8444. (let* ((ee (org-element-at-point))
  8445. (et (org-element-type ee))
  8446. (ev (plist-get (cadr ee) :value)))
  8447. (setq txt (cond
  8448. ((org-at-heading-p) nil)
  8449. ((eq et 'keyword) ev)
  8450. ((org-region-active-p)
  8451. (buffer-substring (region-beginning) (region-end)))))
  8452. (when (or (null txt) (string-match "\\S-" txt))
  8453. (setq cpltxt
  8454. (concat cpltxt "::"
  8455. (condition-case nil
  8456. (org-make-org-heading-search-string txt)
  8457. (error "")))
  8458. desc (or (and (eq et 'keyword) ev)
  8459. (nth 4 (ignore-errors (org-heading-components)))
  8460. "NONE")))))
  8461. (if (string-match "::\\'" cpltxt)
  8462. (setq cpltxt (substring cpltxt 0 -2)))
  8463. (setq link cpltxt))))
  8464. ((buffer-file-name (buffer-base-buffer))
  8465. ;; Just link to this file here.
  8466. (setq cpltxt (concat "file:"
  8467. (abbreviate-file-name
  8468. (buffer-file-name (buffer-base-buffer)))))
  8469. ;; Add a context string
  8470. (when (org-xor org-context-in-file-links arg)
  8471. (setq txt (if (org-region-active-p)
  8472. (buffer-substring (region-beginning) (region-end))
  8473. (buffer-substring (point-at-bol) (point-at-eol))))
  8474. ;; Only use search option if there is some text.
  8475. (when (string-match "\\S-" txt)
  8476. (setq cpltxt
  8477. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8478. desc "NONE")))
  8479. (setq link cpltxt))
  8480. ((org-called-interactively-p 'interactive)
  8481. (user-error "No method for storing a link from this buffer"))
  8482. (t (setq link nil)))
  8483. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8484. (setq link (or link cpltxt)
  8485. desc (or desc cpltxt))
  8486. (cond ((equal desc "NONE") (setq desc nil))
  8487. ((string-match org-bracket-link-regexp desc)
  8488. (setq desc (replace-regexp-in-string
  8489. org-bracket-link-regexp
  8490. (concat "\\3" (if (equal (length (match-string 0 desc))
  8491. (length desc)) "*" "")) desc))))
  8492. (if (and (or (org-called-interactively-p 'any)
  8493. executing-kbd-macro ignore-region) link)
  8494. (progn
  8495. (setq org-stored-links
  8496. (cons (list link desc) org-stored-links))
  8497. (message "Stored: %s" (or desc link))
  8498. (when custom-id
  8499. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8500. "::#" custom-id))
  8501. (setq org-stored-links
  8502. (cons (list link desc) org-stored-links))))
  8503. (or agenda-link (and link (org-make-link-string link desc))))))))
  8504. (defun org-store-link-props (&rest plist)
  8505. "Store link properties, extract names and addresses."
  8506. (let (x adr)
  8507. (when (setq x (plist-get plist :from))
  8508. (setq adr (mail-extract-address-components x))
  8509. (setq plist (plist-put plist :fromname (car adr)))
  8510. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8511. (when (setq x (plist-get plist :to))
  8512. (setq adr (mail-extract-address-components x))
  8513. (setq plist (plist-put plist :toname (car adr)))
  8514. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8515. (let ((from (plist-get plist :from))
  8516. (to (plist-get plist :to)))
  8517. (when (and from to org-from-is-user-regexp)
  8518. (setq plist
  8519. (plist-put plist :fromto
  8520. (if (string-match org-from-is-user-regexp from)
  8521. (concat "to %t")
  8522. (concat "from %f"))))))
  8523. (setq org-store-link-plist plist))
  8524. (defun org-add-link-props (&rest plist)
  8525. "Add these properties to the link property list."
  8526. (let (key value)
  8527. (while plist
  8528. (setq key (pop plist) value (pop plist))
  8529. (setq org-store-link-plist
  8530. (plist-put org-store-link-plist key value)))))
  8531. (defun org-email-link-description (&optional fmt)
  8532. "Return the description part of an email link.
  8533. This takes information from `org-store-link-plist' and formats it
  8534. according to FMT (default from `org-email-link-description-format')."
  8535. (setq fmt (or fmt org-email-link-description-format))
  8536. (let* ((p org-store-link-plist)
  8537. (to (plist-get p :toaddress))
  8538. (from (plist-get p :fromaddress))
  8539. (table
  8540. (list
  8541. (cons "%c" (plist-get p :fromto))
  8542. (cons "%F" (plist-get p :from))
  8543. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8544. (cons "%T" (plist-get p :to))
  8545. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8546. (cons "%s" (plist-get p :subject))
  8547. (cons "%d" (plist-get p :date))
  8548. (cons "%m" (plist-get p :message-id)))))
  8549. (when (string-match "%c" fmt)
  8550. ;; Check if the user wrote this message
  8551. (if (and org-from-is-user-regexp from to
  8552. (save-match-data (string-match org-from-is-user-regexp from)))
  8553. (setq fmt (replace-match "to %t" t t fmt))
  8554. (setq fmt (replace-match "from %f" t t fmt))))
  8555. (org-replace-escapes fmt table)))
  8556. (defun org-make-org-heading-search-string (&optional string)
  8557. "Make search string for the current headline or STRING."
  8558. (let ((s (or string
  8559. (and (derived-mode-p 'org-mode)
  8560. (save-excursion
  8561. (org-back-to-heading t)
  8562. (org-element-property :raw-value (org-element-at-point))))))
  8563. (lines org-context-in-file-links))
  8564. (or string (setq s (concat "*" s))) ; Add * for headlines
  8565. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8566. (when (and string (integerp lines) (> lines 0))
  8567. (let ((slines (org-split-string s "\n")))
  8568. (when (< lines (length slines))
  8569. (setq s (mapconcat
  8570. 'identity
  8571. (reverse (nthcdr (- (length slines) lines)
  8572. (reverse slines))) "\n")))))
  8573. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8574. (defun org-make-link-string (link &optional description)
  8575. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8576. (unless (string-match "\\S-" link)
  8577. (error "Empty link"))
  8578. (when (and description
  8579. (stringp description)
  8580. (not (string-match "\\S-" description)))
  8581. (setq description nil))
  8582. (when (stringp description)
  8583. ;; Remove brackets from the description, they are fatal.
  8584. (while (string-match "\\[" description)
  8585. (setq description (replace-match "{" t t description)))
  8586. (while (string-match "\\]" description)
  8587. (setq description (replace-match "}" t t description))))
  8588. (when (equal link description)
  8589. ;; No description needed, it is identical
  8590. (setq description nil))
  8591. (when (and (not description)
  8592. (not (string-match (org-image-file-name-regexp) link))
  8593. (not (equal link (org-link-escape link))))
  8594. (setq description (org-extract-attributes link)))
  8595. (setq link
  8596. (cond ((string-match (org-image-file-name-regexp) link) link)
  8597. ((string-match org-link-types-re link)
  8598. (concat (match-string 1 link)
  8599. (org-link-escape (substring link (match-end 1)))))
  8600. (t (org-link-escape link))))
  8601. (concat "[[" link "]"
  8602. (if description (concat "[" description "]") "")
  8603. "]"))
  8604. (defconst org-link-escape-chars
  8605. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8606. "List of characters that should be escaped in link.
  8607. This is the list that is used for internal purposes.")
  8608. (defconst org-link-escape-chars-browser
  8609. '(?\ )
  8610. "List of escapes for characters that are problematic in links.
  8611. This is the list that is used before handing over to the browser.")
  8612. (defun org-link-escape (text &optional table merge)
  8613. "Return percent escaped representation of TEXT.
  8614. TEXT is a string with the text to escape.
  8615. Optional argument TABLE is a list with characters that should be
  8616. escaped. When nil, `org-link-escape-chars' is used.
  8617. If optional argument MERGE is set, merge TABLE into
  8618. `org-link-escape-chars'."
  8619. (cond
  8620. ((and table merge)
  8621. (mapc (lambda (defchr)
  8622. (unless (member defchr table)
  8623. (setq table (cons defchr table)))) org-link-escape-chars))
  8624. ((null table)
  8625. (setq table org-link-escape-chars)))
  8626. (mapconcat
  8627. (lambda (char)
  8628. (if (or (member char table)
  8629. (and (or (< char 32) (= char 37) (> char 126))
  8630. org-url-hexify-p))
  8631. (mapconcat (lambda (sequence-element)
  8632. (format "%%%.2X" sequence-element))
  8633. (or (encode-coding-char char 'utf-8)
  8634. (error "Unable to percent escape character: %s"
  8635. (char-to-string char))) "")
  8636. (char-to-string char))) text ""))
  8637. (defun org-link-unescape (str)
  8638. "Unhex hexified Unicode strings as returned from the JavaScript function
  8639. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8640. (unless (and (null str) (string= "" str))
  8641. (let ((pos 0) (case-fold-search t) unhexed)
  8642. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8643. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8644. (setq str (replace-match unhexed t t str))
  8645. (setq pos (+ pos (length unhexed))))))
  8646. str)
  8647. (defun org-link-unescape-compound (hex)
  8648. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8649. Note: this function also decodes single byte encodings like
  8650. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8651. (save-match-data
  8652. (let* ((bytes (cdr (split-string hex "%")))
  8653. (ret "")
  8654. (eat 0)
  8655. (sum 0))
  8656. (while bytes
  8657. (let* ((val (string-to-number (pop bytes) 16))
  8658. (shift-xor
  8659. (if (= 0 eat)
  8660. (cond
  8661. ((>= val 252) (cons 6 252))
  8662. ((>= val 248) (cons 5 248))
  8663. ((>= val 240) (cons 4 240))
  8664. ((>= val 224) (cons 3 224))
  8665. ((>= val 192) (cons 2 192))
  8666. (t (cons 0 0)))
  8667. (cons 6 128))))
  8668. (if (>= val 192) (setq eat (car shift-xor)))
  8669. (setq val (logxor val (cdr shift-xor)))
  8670. (setq sum (+ (lsh sum (car shift-xor)) val))
  8671. (if (> eat 0) (setq eat (- eat 1)))
  8672. (cond
  8673. ((= 0 eat) ;multi byte
  8674. (setq ret (concat ret (org-char-to-string sum)))
  8675. (setq sum 0))
  8676. ((not bytes) ; single byte(s)
  8677. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8678. )) ;; end (while bytes
  8679. ret )))
  8680. (defun org-link-unescape-single-byte-sequence (hex)
  8681. "Unhexify hex-encoded single byte character sequences."
  8682. (mapconcat (lambda (byte)
  8683. (char-to-string (string-to-number byte 16)))
  8684. (cdr (split-string hex "%")) ""))
  8685. (defun org-xor (a b)
  8686. "Exclusive or."
  8687. (if a (not b) b))
  8688. (defun org-fixup-message-id-for-http (s)
  8689. "Replace special characters in a message id, so it can be used in an http query."
  8690. (when (string-match "%" s)
  8691. (setq s (mapconcat (lambda (c)
  8692. (if (eq c ?%)
  8693. "%25"
  8694. (char-to-string c)))
  8695. s "")))
  8696. (while (string-match "<" s)
  8697. (setq s (replace-match "%3C" t t s)))
  8698. (while (string-match ">" s)
  8699. (setq s (replace-match "%3E" t t s)))
  8700. (while (string-match "@" s)
  8701. (setq s (replace-match "%40" t t s)))
  8702. s)
  8703. (defun org-link-prettify (link)
  8704. "Return a human-readable representation of LINK.
  8705. The car of LINK must be a raw link the cdr of LINK must be either
  8706. a link description or nil."
  8707. (let ((desc (or (cadr link) "<no description>")))
  8708. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8709. "<" (car link) ">")))
  8710. ;;;###autoload
  8711. (defun org-insert-link-global ()
  8712. "Insert a link like Org-mode does.
  8713. This command can be called in any mode to insert a link in Org-mode syntax."
  8714. (interactive)
  8715. (org-load-modules-maybe)
  8716. (org-run-like-in-org-mode 'org-insert-link))
  8717. (defun org-insert-all-links (&optional keep)
  8718. "Insert all links in `org-stored-links'."
  8719. (interactive "P")
  8720. (let ((links (copy-sequence org-stored-links)) l)
  8721. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8722. (insert "- ")
  8723. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8724. (insert "\n"))))
  8725. (defun org-link-fontify-links-to-this-file ()
  8726. "Fontify links to the current file in `org-stored-links'."
  8727. (let ((f (buffer-file-name)) a b)
  8728. (setq a (mapcar (lambda(l)
  8729. (let ((ll (car l)))
  8730. (when (and (string-match "^file:\\(.+\\)::" ll)
  8731. (equal f (expand-file-name (match-string 1 ll))))
  8732. ll)))
  8733. org-stored-links))
  8734. (when (featurep 'org-id)
  8735. (setq b (mapcar (lambda(l)
  8736. (let ((ll (car l)))
  8737. (when (and (string-match "^id:\\(.+\\)$" ll)
  8738. (equal f (expand-file-name
  8739. (or (org-id-find-id-file
  8740. (match-string 1 ll)) ""))))
  8741. ll)))
  8742. org-stored-links)))
  8743. (mapcar (lambda(l)
  8744. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8745. (delq nil (append a b)))))
  8746. (defvar org-link-links-in-this-file nil)
  8747. (defun org-insert-link (&optional complete-file link-location default-description)
  8748. "Insert a link. At the prompt, enter the link.
  8749. Completion can be used to insert any of the link protocol prefixes like
  8750. http or ftp in use.
  8751. The history can be used to select a link previously stored with
  8752. `org-store-link'. When the empty string is entered (i.e. if you just
  8753. press RET at the prompt), the link defaults to the most recently
  8754. stored link. As SPC triggers completion in the minibuffer, you need to
  8755. use M-SPC or C-q SPC to force the insertion of a space character.
  8756. You will also be prompted for a description, and if one is given, it will
  8757. be displayed in the buffer instead of the link.
  8758. If there is already a link at point, this command will allow you to edit link
  8759. and description parts.
  8760. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8761. be selected using completion. The path to the file will be relative to the
  8762. current directory if the file is in the current directory or a subdirectory.
  8763. Otherwise, the link will be the absolute path as completed in the minibuffer
  8764. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8765. option `org-link-file-path-type'.
  8766. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8767. the current directory or below.
  8768. With three \\[universal-argument] prefixes, negate the meaning of
  8769. `org-keep-stored-link-after-insertion'.
  8770. If `org-make-link-description-function' is non-nil, this function will be
  8771. called with the link target, and the result will be the default
  8772. link description.
  8773. If the LINK-LOCATION parameter is non-nil, this value will be
  8774. used as the link location instead of reading one interactively.
  8775. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8776. be used as the default description."
  8777. (interactive "P")
  8778. (let* ((wcf (current-window-configuration))
  8779. (origbuf (current-buffer))
  8780. (region (if (org-region-active-p)
  8781. (buffer-substring (region-beginning) (region-end))))
  8782. (remove (and region (list (region-beginning) (region-end))))
  8783. (desc region)
  8784. tmphist ; byte-compile incorrectly complains about this
  8785. (link link-location)
  8786. (abbrevs org-link-abbrev-alist-local)
  8787. entry file all-prefixes auto-desc)
  8788. (cond
  8789. (link-location) ; specified by arg, just use it.
  8790. ((org-in-regexp org-bracket-link-regexp 1)
  8791. ;; We do have a link at point, and we are going to edit it.
  8792. (setq remove (list (match-beginning 0) (match-end 0)))
  8793. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8794. (setq link (read-string "Link: "
  8795. (org-link-unescape
  8796. (org-match-string-no-properties 1)))))
  8797. ((or (org-in-regexp org-angle-link-re)
  8798. (org-in-regexp org-plain-link-re))
  8799. ;; Convert to bracket link
  8800. (setq remove (list (match-beginning 0) (match-end 0))
  8801. link (read-string "Link: "
  8802. (org-remove-angle-brackets (match-string 0)))))
  8803. ((member complete-file '((4) (16)))
  8804. ;; Completing read for file names.
  8805. (setq link (org-file-complete-link complete-file)))
  8806. (t
  8807. ;; Read link, with completion for stored links.
  8808. (org-link-fontify-links-to-this-file)
  8809. (org-switch-to-buffer-other-window "*Org Links*")
  8810. (with-current-buffer "*Org Links*"
  8811. (erase-buffer)
  8812. (insert "Insert a link.
  8813. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8814. (when org-stored-links
  8815. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8816. (insert (mapconcat 'org-link-prettify
  8817. (reverse org-stored-links) "\n")))
  8818. (goto-char (point-min)))
  8819. (let ((cw (selected-window)))
  8820. (select-window (get-buffer-window "*Org Links*" 'visible))
  8821. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8822. (unless (pos-visible-in-window-p (point-max))
  8823. (org-fit-window-to-buffer))
  8824. (and (window-live-p cw) (select-window cw)))
  8825. ;; Fake a link history, containing the stored links.
  8826. (setq tmphist (append (mapcar 'car org-stored-links)
  8827. org-insert-link-history))
  8828. (setq all-prefixes (append (mapcar 'car abbrevs)
  8829. (mapcar 'car org-link-abbrev-alist)
  8830. org-link-types))
  8831. (unwind-protect
  8832. (progn
  8833. (setq link
  8834. (org-completing-read
  8835. "Link: "
  8836. (append
  8837. (mapcar (lambda (x) (concat x ":"))
  8838. all-prefixes)
  8839. (mapcar 'car org-stored-links))
  8840. nil nil nil
  8841. 'tmphist
  8842. (caar org-stored-links)))
  8843. (if (not (string-match "\\S-" link))
  8844. (error "No link selected"))
  8845. (mapc (lambda(l)
  8846. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8847. org-stored-links)
  8848. (if (or (member link all-prefixes)
  8849. (and (equal ":" (substring link -1))
  8850. (member (substring link 0 -1) all-prefixes)
  8851. (setq link (substring link 0 -1))))
  8852. (setq link (with-current-buffer origbuf
  8853. (org-link-try-special-completion link)))))
  8854. (set-window-configuration wcf)
  8855. (kill-buffer "*Org Links*"))
  8856. (setq entry (assoc link org-stored-links))
  8857. (or entry (push link org-insert-link-history))
  8858. (setq desc (or desc (nth 1 entry)))))
  8859. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8860. (not org-keep-stored-link-after-insertion))
  8861. (setq org-stored-links (delq (assoc link org-stored-links)
  8862. org-stored-links)))
  8863. (if (string-match org-plain-link-re link)
  8864. ;; URL-like link, normalize the use of angular brackets.
  8865. (setq link (org-remove-angle-brackets link)))
  8866. ;; Check if we are linking to the current file with a search
  8867. ;; option If yes, simplify the link by using only the search
  8868. ;; option.
  8869. (when (and buffer-file-name
  8870. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8871. (let* ((path (match-string 1 link))
  8872. (case-fold-search nil)
  8873. (search (match-string 2 link)))
  8874. (save-match-data
  8875. (if (equal (file-truename buffer-file-name) (file-truename path))
  8876. ;; We are linking to this same file, with a search option
  8877. (setq link search)))))
  8878. ;; Check if we can/should use a relative path. If yes, simplify the link
  8879. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8880. (let* ((type (match-string 1 link))
  8881. (path (match-string 2 link))
  8882. (origpath path)
  8883. (case-fold-search nil))
  8884. (cond
  8885. ((or (eq org-link-file-path-type 'absolute)
  8886. (equal complete-file '(16)))
  8887. (setq path (abbreviate-file-name (expand-file-name path))))
  8888. ((eq org-link-file-path-type 'noabbrev)
  8889. (setq path (expand-file-name path)))
  8890. ((eq org-link-file-path-type 'relative)
  8891. (setq path (file-relative-name path)))
  8892. (t
  8893. (save-match-data
  8894. (if (string-match (concat "^" (regexp-quote
  8895. (expand-file-name
  8896. (file-name-as-directory
  8897. default-directory))))
  8898. (expand-file-name path))
  8899. ;; We are linking a file with relative path name.
  8900. (setq path (substring (expand-file-name path)
  8901. (match-end 0)))
  8902. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8903. (setq link (concat type path))
  8904. (if (equal desc origpath)
  8905. (setq desc path))))
  8906. (if org-make-link-description-function
  8907. (setq desc
  8908. (or (condition-case nil
  8909. (funcall org-make-link-description-function link desc)
  8910. (error (progn (message "Can't get link description from `%s'"
  8911. (symbol-name org-make-link-description-function))
  8912. (sit-for 2) nil)))
  8913. (read-string "Description: " default-description)))
  8914. (if default-description (setq desc default-description)
  8915. (setq desc (or (and auto-desc desc)
  8916. (read-string "Description: " desc)))))
  8917. (unless (string-match "\\S-" desc) (setq desc nil))
  8918. (if remove (apply 'delete-region remove))
  8919. (insert (org-make-link-string link desc))))
  8920. (defun org-link-try-special-completion (type)
  8921. "If there is completion support for link type TYPE, offer it."
  8922. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8923. (if (functionp fun)
  8924. (funcall fun)
  8925. (read-string "Link (no completion support): " (concat type ":")))))
  8926. (defun org-file-complete-link (&optional arg)
  8927. "Create a file link using completion."
  8928. (let (file link)
  8929. (setq file (org-iread-file-name "File: "))
  8930. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8931. (pwd1 (file-name-as-directory (abbreviate-file-name
  8932. (expand-file-name ".")))))
  8933. (cond
  8934. ((equal arg '(16))
  8935. (setq link (concat
  8936. "file:"
  8937. (abbreviate-file-name (expand-file-name file)))))
  8938. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8939. (setq link (concat "file:" (match-string 1 file))))
  8940. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8941. (expand-file-name file))
  8942. (setq link (concat
  8943. "file:" (match-string 1 (expand-file-name file)))))
  8944. (t (setq link (concat "file:" file)))))
  8945. link))
  8946. (defun org-iread-file-name (&rest args)
  8947. "Read-file-name using `ido-mode' speedup if available.
  8948. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  8949. See `read-file-name' for a description of parameters."
  8950. (org-without-partial-completion
  8951. (if (and org-completion-use-ido
  8952. (fboundp 'ido-read-file-name)
  8953. (boundp 'ido-mode) ido-mode
  8954. (listp (second args)))
  8955. (let ((ido-enter-matching-directory nil))
  8956. (apply 'ido-read-file-name args))
  8957. (apply 'read-file-name args))))
  8958. (defun org-completing-read (&rest args)
  8959. "Completing-read with SPACE being a normal character."
  8960. (let ((enable-recursive-minibuffers t)
  8961. (minibuffer-local-completion-map
  8962. (copy-keymap minibuffer-local-completion-map)))
  8963. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8964. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8965. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8966. (apply 'org-icompleting-read args)))
  8967. (defun org-completing-read-no-i (&rest args)
  8968. (let (org-completion-use-ido org-completion-use-iswitchb)
  8969. (apply 'org-completing-read args)))
  8970. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8971. "Use iswitch as a completing-read replacement to choose from choices.
  8972. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8973. from."
  8974. (let* ((iswitchb-use-virtual-buffers nil)
  8975. (iswitchb-make-buflist-hook
  8976. (lambda ()
  8977. (setq iswitchb-temp-buflist choices))))
  8978. (iswitchb-read-buffer prompt)))
  8979. (defun org-icompleting-read (&rest args)
  8980. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8981. (org-without-partial-completion
  8982. (if (and org-completion-use-ido
  8983. (fboundp 'ido-completing-read)
  8984. (boundp 'ido-mode) ido-mode
  8985. (listp (second args)))
  8986. (let ((ido-enter-matching-directory nil))
  8987. (apply 'ido-completing-read (concat (car args))
  8988. (if (consp (car (nth 1 args)))
  8989. (mapcar 'car (nth 1 args))
  8990. (nth 1 args))
  8991. (cddr args)))
  8992. (if (and org-completion-use-iswitchb
  8993. (boundp 'iswitchb-mode) iswitchb-mode
  8994. (listp (second args)))
  8995. (apply 'org-iswitchb-completing-read (concat (car args))
  8996. (if (consp (car (nth 1 args)))
  8997. (mapcar 'car (nth 1 args))
  8998. (nth 1 args))
  8999. (cddr args))
  9000. (apply 'completing-read args)))))
  9001. (defun org-extract-attributes (s)
  9002. "Extract the attributes cookie from a string and set as text property."
  9003. (let (a attr (start 0) key value)
  9004. (save-match-data
  9005. (when (string-match "{{\\([^}]+\\)}}$" s)
  9006. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9007. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9008. (setq key (match-string 1 a) value (match-string 2 a)
  9009. start (match-end 0)
  9010. attr (plist-put attr (intern key) value))))
  9011. (org-add-props s nil 'org-attr attr))
  9012. s))
  9013. (defun org-extract-attributes-from-string (tag)
  9014. (let (key value attr)
  9015. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  9016. (setq key (match-string 1 tag) value (match-string 2 tag)
  9017. tag (replace-match "" t t tag)
  9018. attr (plist-put attr (intern key) value)))
  9019. (cons tag attr)))
  9020. (defun org-attributes-to-string (plist)
  9021. "Format a property list into an HTML attribute list."
  9022. (let ((s "") key value)
  9023. (while plist
  9024. (setq key (pop plist) value (pop plist))
  9025. (and value
  9026. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  9027. s))
  9028. ;;; Opening/following a link
  9029. (defvar org-link-search-failed nil)
  9030. (defvar org-open-link-functions nil
  9031. "Hook for functions finding a plain text link.
  9032. These functions must take a single argument, the link content.
  9033. They will be called for links that look like [[link text][description]]
  9034. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9035. like a filename (e.g. \"./blue.png\").
  9036. These functions will be called *before* Org attempts to resolve the
  9037. link by doing text searches in the current buffer - so if you want a
  9038. link \"[[target]]\" to still find \"<<target>>\", your function should
  9039. handle this as a special case.
  9040. When the function does handle the link, it must return a non-nil value.
  9041. If it decides that it is not responsible for this link, it must return
  9042. nil to indicate that that Org-mode can continue with other options
  9043. like exact and fuzzy text search.")
  9044. (defun org-next-link (&optional search-backward)
  9045. "Move forward to the next link.
  9046. If the link is in hidden text, expose it."
  9047. (interactive "P")
  9048. (when (and org-link-search-failed (eq this-command last-command))
  9049. (goto-char (point-min))
  9050. (message "Link search wrapped back to beginning of buffer"))
  9051. (setq org-link-search-failed nil)
  9052. (let* ((pos (point))
  9053. (ct (org-context))
  9054. (a (assoc :link ct))
  9055. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9056. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9057. ((looking-at org-any-link-re)
  9058. ;; Don't stay stuck at link without an org-link face
  9059. (forward-char (if search-backward -1 1))))
  9060. (if (funcall srch-fun org-any-link-re nil t)
  9061. (progn
  9062. (goto-char (match-beginning 0))
  9063. (if (outline-invisible-p) (org-show-context)))
  9064. (goto-char pos)
  9065. (setq org-link-search-failed t)
  9066. (message "No further link found"))))
  9067. (defun org-previous-link ()
  9068. "Move backward to the previous link.
  9069. If the link is in hidden text, expose it."
  9070. (interactive)
  9071. (funcall 'org-next-link t))
  9072. (defun org-translate-link (s)
  9073. "Translate a link string if a translation function has been defined."
  9074. (if (and org-link-translation-function
  9075. (fboundp org-link-translation-function)
  9076. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9077. (progn
  9078. (setq s (funcall org-link-translation-function
  9079. (match-string 1 s) (match-string 2 s)))
  9080. (concat (car s) ":" (cdr s)))
  9081. s))
  9082. (defun org-translate-link-from-planner (type path)
  9083. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9084. This is still an experimental function, your mileage may vary."
  9085. (cond
  9086. ((member type '("http" "https" "news" "ftp"))
  9087. ;; standard Internet links are the same.
  9088. nil)
  9089. ((and (equal type "irc") (string-match "^//" path))
  9090. ;; Planner has two / at the beginning of an irc link, we have 1.
  9091. ;; We should have zero, actually....
  9092. (setq path (substring path 1)))
  9093. ((and (equal type "lisp") (string-match "^/" path))
  9094. ;; Planner has a slash, we do not.
  9095. (setq type "elisp" path (substring path 1)))
  9096. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9097. ;; A typical message link. Planner has the id after the final slash,
  9098. ;; we separate it with a hash mark
  9099. (setq path (concat (match-string 1 path) "#"
  9100. (org-remove-angle-brackets (match-string 2 path)))))
  9101. )
  9102. (cons type path))
  9103. (defun org-find-file-at-mouse (ev)
  9104. "Open file link or URL at mouse."
  9105. (interactive "e")
  9106. (mouse-set-point ev)
  9107. (org-open-at-point 'in-emacs))
  9108. (defun org-open-at-mouse (ev)
  9109. "Open file link or URL at mouse.
  9110. See the docstring of `org-open-file' for details."
  9111. (interactive "e")
  9112. (mouse-set-point ev)
  9113. (if (eq major-mode 'org-agenda-mode)
  9114. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9115. (org-open-at-point))
  9116. (defvar org-window-config-before-follow-link nil
  9117. "The window configuration before following a link.
  9118. This is saved in case the need arises to restore it.")
  9119. (defvar org-open-link-marker (make-marker)
  9120. "Marker pointing to the location where `org-open-at-point; was called.")
  9121. ;;;###autoload
  9122. (defun org-open-at-point-global ()
  9123. "Follow a link like Org-mode does.
  9124. This command can be called in any mode to follow a link that has
  9125. Org-mode syntax."
  9126. (interactive)
  9127. (org-run-like-in-org-mode 'org-open-at-point))
  9128. ;;;###autoload
  9129. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9130. "Open a link in the string S, as if it was in Org-mode."
  9131. (interactive "sLink: \nP")
  9132. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9133. (with-temp-buffer
  9134. (let ((org-inhibit-startup (not reference-buffer)))
  9135. (org-mode)
  9136. (insert s)
  9137. (goto-char (point-min))
  9138. (when reference-buffer
  9139. (setq org-link-abbrev-alist-local
  9140. (with-current-buffer reference-buffer
  9141. org-link-abbrev-alist-local)))
  9142. (org-open-at-point arg reference-buffer)))))
  9143. (defvar org-open-at-point-functions nil
  9144. "Hook that is run when following a link at point.
  9145. Functions in this hook must return t if they identify and follow
  9146. a link at point. If they don't find anything interesting at point,
  9147. they must return nil.")
  9148. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9149. (defun org-open-at-point (&optional arg reference-buffer)
  9150. "Open link at or after point.
  9151. If there is no link at point, this function will search forward up to
  9152. the end of the current line.
  9153. Normally, files will be opened by an appropriate application. If the
  9154. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9155. With a double prefix argument, try to open outside of Emacs, in the
  9156. application the system uses for this file type."
  9157. (interactive "P")
  9158. ;; if in a code block, then open the block's results
  9159. (unless (call-interactively #'org-babel-open-src-block-result)
  9160. (org-load-modules-maybe)
  9161. (move-marker org-open-link-marker (point))
  9162. (setq org-window-config-before-follow-link (current-window-configuration))
  9163. (org-remove-occur-highlights nil nil t)
  9164. (cond
  9165. ((and (org-at-heading-p)
  9166. (not (org-at-timestamp-p t))
  9167. (not (org-in-regexp
  9168. (concat org-plain-link-re "\\|"
  9169. org-bracket-link-regexp "\\|"
  9170. org-angle-link-re "\\|"
  9171. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9172. (not (get-text-property (point) 'org-linked-text)))
  9173. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9174. (lk0 (car lkall))
  9175. (lk (if (stringp lk0) (list lk0) lk0))
  9176. (lkend (cdr lkall)))
  9177. (mapcar (lambda(l)
  9178. (search-forward l nil lkend)
  9179. (goto-char (match-beginning 0))
  9180. (org-open-at-point))
  9181. lk))
  9182. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9183. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9184. ((and (org-at-timestamp-p t)
  9185. (not (org-in-regexp org-bracket-link-regexp)))
  9186. (org-follow-timestamp-link))
  9187. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9188. (not (org-in-regexp org-any-link-re)))
  9189. (org-footnote-action))
  9190. (t
  9191. (let (type path link line search (pos (point)))
  9192. (catch 'match
  9193. (save-excursion
  9194. (skip-chars-forward "^]\n\r")
  9195. (when (org-in-regexp org-bracket-link-regexp 1)
  9196. (setq link (org-extract-attributes
  9197. (org-link-unescape (org-match-string-no-properties 1))))
  9198. (while (string-match " *\n *" link)
  9199. (setq link (replace-match " " t t link)))
  9200. (setq link (org-link-expand-abbrev link))
  9201. (cond
  9202. ((or (file-name-absolute-p link)
  9203. (string-match "^\\.\\.?/" link))
  9204. (setq type "file" path link))
  9205. ((string-match org-link-re-with-space3 link)
  9206. (setq type (match-string 1 link) path (match-string 2 link)))
  9207. ((string-match "^help:+\\(.+\\)" link)
  9208. (setq type "help" path (match-string 1 link)))
  9209. (t (setq type "thisfile" path link)))
  9210. (throw 'match t)))
  9211. (when (get-text-property (point) 'org-linked-text)
  9212. (setq type "thisfile"
  9213. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9214. (1+ (point)) (point))
  9215. path (buffer-substring
  9216. (or (previous-single-property-change pos 'org-linked-text)
  9217. (point-min))
  9218. (or (next-single-property-change pos 'org-linked-text)
  9219. (point-max))))
  9220. (throw 'match t))
  9221. (save-excursion
  9222. (when (or (org-in-regexp org-angle-link-re)
  9223. (let ((match (org-in-regexp org-plain-link-re)))
  9224. ;; Check a plain link is not within a bracket link
  9225. (and match
  9226. (save-excursion
  9227. (progn
  9228. (goto-char (car match))
  9229. (not (org-in-regexp org-bracket-link-regexp))))))
  9230. (let ((line_ending (save-excursion (end-of-line) (point))))
  9231. ;; We are in a line before a plain or bracket link
  9232. (or (re-search-forward org-plain-link-re line_ending t)
  9233. (re-search-forward org-bracket-link-regexp line_ending t))))
  9234. (setq type (match-string 1)
  9235. path (org-link-unescape (match-string 2)))
  9236. (throw 'match t)))
  9237. (save-excursion
  9238. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9239. (setq type "tags"
  9240. path (match-string 1))
  9241. (while (string-match ":" path)
  9242. (setq path (replace-match "+" t t path)))
  9243. (throw 'match t)))
  9244. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9245. (setq type "tree-match"
  9246. path (match-string 1))
  9247. (throw 'match t)))
  9248. (unless path
  9249. (user-error "No link found"))
  9250. ;; switch back to reference buffer
  9251. ;; needed when if called in a temporary buffer through
  9252. ;; org-open-link-from-string
  9253. (with-current-buffer (or reference-buffer (current-buffer))
  9254. ;; Remove any trailing spaces in path
  9255. (if (string-match " +\\'" path)
  9256. (setq path (replace-match "" t t path)))
  9257. (if (and org-link-translation-function
  9258. (fboundp org-link-translation-function))
  9259. ;; Check if we need to translate the link
  9260. (let ((tmp (funcall org-link-translation-function type path)))
  9261. (setq type (car tmp) path (cdr tmp))))
  9262. (cond
  9263. ((assoc type org-link-protocols)
  9264. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9265. ((equal type "help")
  9266. (let ((f-or-v (intern path)))
  9267. (cond ((fboundp f-or-v)
  9268. (describe-function f-or-v))
  9269. ((boundp f-or-v)
  9270. (describe-variable f-or-v))
  9271. (t (error "Not a known function or variable")))))
  9272. ((equal type "mailto")
  9273. (let ((cmd (car org-link-mailto-program))
  9274. (args (cdr org-link-mailto-program)) args1
  9275. (address path) (subject "") a)
  9276. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9277. (setq address (match-string 1 path)
  9278. subject (org-link-escape (match-string 2 path))))
  9279. (while args
  9280. (cond
  9281. ((not (stringp (car args))) (push (pop args) args1))
  9282. (t (setq a (pop args))
  9283. (if (string-match "%a" a)
  9284. (setq a (replace-match address t t a)))
  9285. (if (string-match "%s" a)
  9286. (setq a (replace-match subject t t a)))
  9287. (push a args1))))
  9288. (apply cmd (nreverse args1))))
  9289. ((member type '("http" "https" "ftp" "news"))
  9290. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9291. (org-link-escape
  9292. path org-link-escape-chars-browser)
  9293. path))))
  9294. ((string= type "doi")
  9295. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9296. (org-link-escape
  9297. path org-link-escape-chars-browser)
  9298. path))))
  9299. ((member type '("message"))
  9300. (browse-url (concat type ":" path)))
  9301. ((string= type "tags")
  9302. (org-tags-view arg path))
  9303. ((string= type "tree-match")
  9304. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9305. ((string= type "file")
  9306. (if (string-match "::\\([0-9]+\\)\\'" path)
  9307. (setq line (string-to-number (match-string 1 path))
  9308. path (substring path 0 (match-beginning 0)))
  9309. (if (string-match "::\\(.+\\)\\'" path)
  9310. (setq search (match-string 1 path)
  9311. path (substring path 0 (match-beginning 0)))))
  9312. (if (string-match "[*?{]" (file-name-nondirectory path))
  9313. (dired path)
  9314. (org-open-file path arg line search)))
  9315. ((string= type "shell")
  9316. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9317. (cmd path))
  9318. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9319. (string-match org-confirm-shell-link-not-regexp cmd))
  9320. (not org-confirm-shell-link-function)
  9321. (funcall org-confirm-shell-link-function
  9322. (format "Execute \"%s\" in shell? "
  9323. (org-add-props cmd nil
  9324. 'face 'org-warning))))
  9325. (progn
  9326. (message "Executing %s" cmd)
  9327. (shell-command cmd buf)
  9328. (if (featurep 'midnight)
  9329. (setq clean-buffer-list-kill-buffer-names
  9330. (cons buf clean-buffer-list-kill-buffer-names))))
  9331. (error "Abort"))))
  9332. ((string= type "elisp")
  9333. (let ((cmd path))
  9334. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9335. (string-match org-confirm-elisp-link-not-regexp cmd))
  9336. (not org-confirm-elisp-link-function)
  9337. (funcall org-confirm-elisp-link-function
  9338. (format "Execute \"%s\" as elisp? "
  9339. (org-add-props cmd nil
  9340. 'face 'org-warning))))
  9341. (message "%s => %s" cmd
  9342. (if (equal (string-to-char cmd) ?\()
  9343. (eval (read cmd))
  9344. (call-interactively (read cmd))))
  9345. (error "Abort"))))
  9346. ((and (string= type "thisfile")
  9347. (run-hook-with-args-until-success
  9348. 'org-open-link-functions path)))
  9349. ((string= type "thisfile")
  9350. (if arg
  9351. (switch-to-buffer-other-window
  9352. (org-get-buffer-for-internal-link (current-buffer)))
  9353. (org-mark-ring-push))
  9354. (let ((cmd `(org-link-search
  9355. ,path
  9356. ,(cond ((equal arg '(4)) ''occur)
  9357. ((equal arg '(16)) ''org-occur))
  9358. ,pos)))
  9359. (condition-case nil (let ((org-link-search-inhibit-query t))
  9360. (eval cmd))
  9361. (error (progn (widen) (eval cmd))))))
  9362. (t (browse-url-at-point)))))))
  9363. (move-marker org-open-link-marker nil)
  9364. (run-hook-with-args 'org-follow-link-hook)))
  9365. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9366. "Offer links in the current entry and return the selected link.
  9367. If there is only one link, return it.
  9368. If NTH is an integer, return the NTH link found.
  9369. If ZERO is a string, check also this string for a link, and if
  9370. there is one, return it."
  9371. (with-current-buffer buffer
  9372. (save-excursion
  9373. (save-restriction
  9374. (widen)
  9375. (goto-char marker)
  9376. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9377. "\\(" org-angle-link-re "\\)\\|"
  9378. "\\(" org-plain-link-re "\\)"))
  9379. (cnt ?0)
  9380. (in-emacs (if (integerp nth) nil nth))
  9381. have-zero end links link c)
  9382. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9383. (push (match-string 0 zero) links)
  9384. (setq cnt (1- cnt) have-zero t))
  9385. (save-excursion
  9386. (org-back-to-heading t)
  9387. (setq end (save-excursion (outline-next-heading) (point)))
  9388. (while (re-search-forward re end t)
  9389. (push (match-string 0) links))
  9390. (setq links (org-uniquify (reverse links))))
  9391. (cond
  9392. ((null links)
  9393. (message "No links"))
  9394. ((equal (length links) 1)
  9395. (setq link (car links)))
  9396. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9397. (setq link (nth (if have-zero nth (1- nth)) links)))
  9398. (t ; we have to select a link
  9399. (save-excursion
  9400. (save-window-excursion
  9401. (delete-other-windows)
  9402. (with-output-to-temp-buffer "*Select Link*"
  9403. (mapc (lambda (l)
  9404. (if (not (string-match org-bracket-link-regexp l))
  9405. (princ (format "[%c] %s\n" (incf cnt)
  9406. (org-remove-angle-brackets l)))
  9407. (if (match-end 3)
  9408. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9409. (match-string 3 l) (match-string 1 l)))
  9410. (princ (format "[%c] %s\n" (incf cnt)
  9411. (match-string 1 l))))))
  9412. links))
  9413. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9414. (message "Select link to open, RET to open all:")
  9415. (setq c (read-char-exclusive))
  9416. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9417. (when (equal c ?q) (error "Abort"))
  9418. (if (equal c ?\C-m)
  9419. (setq link links)
  9420. (setq nth (- c ?0))
  9421. (if have-zero (setq nth (1+ nth)))
  9422. (unless (and (integerp nth) (>= (length links) nth))
  9423. (error "Invalid link selection"))
  9424. (setq link (nth (1- nth) links)))))
  9425. (cons link end))))))
  9426. ;; Add special file links that specify the way of opening
  9427. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9428. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9429. (defun org-open-file-with-system (path)
  9430. "Open file at PATH using the system way of opening it."
  9431. (org-open-file path 'system))
  9432. (defun org-open-file-with-emacs (path)
  9433. "Open file at PATH in Emacs."
  9434. (org-open-file path 'emacs))
  9435. ;;; File search
  9436. (defvar org-create-file-search-functions nil
  9437. "List of functions to construct the right search string for a file link.
  9438. These functions are called in turn with point at the location to
  9439. which the link should point.
  9440. A function in the hook should first test if it would like to
  9441. handle this file type, for example by checking the `major-mode'
  9442. or the file extension. If it decides not to handle this file, it
  9443. should just return nil to give other functions a chance. If it
  9444. does handle the file, it must return the search string to be used
  9445. when following the link. The search string will be part of the
  9446. file link, given after a double colon, and `org-open-at-point'
  9447. will automatically search for it. If special measures must be
  9448. taken to make the search successful, another function should be
  9449. added to the companion hook `org-execute-file-search-functions',
  9450. which see.
  9451. A function in this hook may also use `setq' to set the variable
  9452. `description' to provide a suggestion for the descriptive text to
  9453. be used for this link when it gets inserted into an Org-mode
  9454. buffer with \\[org-insert-link].")
  9455. (defvar org-execute-file-search-functions nil
  9456. "List of functions to execute a file search triggered by a link.
  9457. Functions added to this hook must accept a single argument, the
  9458. search string that was part of the file link, the part after the
  9459. double colon. The function must first check if it would like to
  9460. handle this search, for example by checking the `major-mode' or
  9461. the file extension. If it decides not to handle this search, it
  9462. should just return nil to give other functions a chance. If it
  9463. does handle the search, it must return a non-nil value to keep
  9464. other functions from trying.
  9465. Each function can access the current prefix argument through the
  9466. variable `current-prefix-arg'. Note that a single prefix is used
  9467. to force opening a link in Emacs, so it may be good to only use a
  9468. numeric or double prefix to guide the search function.
  9469. In case this is needed, a function in this hook can also restore
  9470. the window configuration before `org-open-at-point' was called using:
  9471. (set-window-configuration org-window-config-before-follow-link)")
  9472. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9473. (defun org-link-search (s &optional type avoid-pos stealth)
  9474. "Search for a link search option.
  9475. If S is surrounded by forward slashes, it is interpreted as a
  9476. regular expression. In org-mode files, this will create an `org-occur'
  9477. sparse tree. In ordinary files, `occur' will be used to list matches.
  9478. If the current buffer is in `dired-mode', grep will be used to search
  9479. in all files. If AVOID-POS is given, ignore matches near that position.
  9480. When optional argument STEALTH is non-nil, do not modify
  9481. visibility around point, thus ignoring
  9482. `org-show-hierarchy-above', `org-show-following-heading' and
  9483. `org-show-siblings' variables."
  9484. (let ((case-fold-search t)
  9485. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9486. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9487. (append '(("") (" ") ("\t") ("\n"))
  9488. org-emphasis-alist)
  9489. "\\|") "\\)"))
  9490. (pos (point))
  9491. (pre nil) (post nil)
  9492. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9493. (cond
  9494. ;; First check if there are any special search functions
  9495. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9496. ;; Now try the builtin stuff
  9497. ((and (equal (string-to-char s0) ?#)
  9498. (> (length s0) 1)
  9499. (save-excursion
  9500. (goto-char (point-min))
  9501. (and
  9502. (re-search-forward
  9503. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9504. (setq type 'dedicated
  9505. pos (match-beginning 0))))
  9506. ;; There is an exact target for this
  9507. (goto-char pos)
  9508. (org-back-to-heading t)))
  9509. ((save-excursion
  9510. (goto-char (point-min))
  9511. (and
  9512. (re-search-forward
  9513. (concat "<<" (regexp-quote s0) ">>") nil t)
  9514. (setq type 'dedicated
  9515. pos (match-beginning 0))))
  9516. ;; There is an exact target for this
  9517. (goto-char pos))
  9518. ((save-excursion
  9519. (goto-char (point-min))
  9520. (and
  9521. (re-search-forward
  9522. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9523. (setq type 'dedicated pos (match-beginning 0))))
  9524. ;; Found an invisible target.
  9525. (goto-char pos))
  9526. ((save-excursion
  9527. (goto-char (point-min))
  9528. (and
  9529. (re-search-forward
  9530. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9531. (setq type 'dedicated pos (match-beginning 0))))
  9532. ;; Found an element with a matching #+name affiliated keyword.
  9533. (goto-char pos))
  9534. ((and (string-match "^(\\(.*\\))$" s0)
  9535. (save-excursion
  9536. (goto-char (point-min))
  9537. (and
  9538. (re-search-forward
  9539. (concat "[^[]" (regexp-quote
  9540. (format org-coderef-label-format
  9541. (match-string 1 s0))))
  9542. nil t)
  9543. (setq type 'dedicated
  9544. pos (1+ (match-beginning 0))))))
  9545. ;; There is a coderef target for this
  9546. (goto-char pos))
  9547. ((string-match "^/\\(.*\\)/$" s)
  9548. ;; A regular expression
  9549. (cond
  9550. ((derived-mode-p 'org-mode)
  9551. (org-occur (match-string 1 s)))
  9552. (t (org-do-occur (match-string 1 s)))))
  9553. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9554. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9555. (goto-char (point-min))
  9556. (cond
  9557. ((let (case-fold-search)
  9558. (re-search-forward (format org-complex-heading-regexp-format
  9559. (regexp-quote s))
  9560. nil t))
  9561. ;; OK, found a match
  9562. (setq type 'dedicated)
  9563. (goto-char (match-beginning 0)))
  9564. ((and (not org-link-search-inhibit-query)
  9565. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9566. (y-or-n-p "No match - create this as a new heading? "))
  9567. (goto-char (point-max))
  9568. (or (bolp) (newline))
  9569. (insert "* " s "\n")
  9570. (beginning-of-line 0))
  9571. (t
  9572. (goto-char pos)
  9573. (error "No match"))))
  9574. (t
  9575. ;; A normal search string
  9576. (when (equal (string-to-char s) ?*)
  9577. ;; Anchor on headlines, post may include tags.
  9578. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9579. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9580. s (substring s 1)))
  9581. (remove-text-properties
  9582. 0 (length s)
  9583. '(face nil mouse-face nil keymap nil fontified nil) s)
  9584. ;; Make a series of regular expressions to find a match
  9585. (setq words (org-split-string s "[ \n\r\t]+")
  9586. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9587. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9588. "\\)" markers)
  9589. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9590. re2a (concat "[ \t\r\n]" re2a_)
  9591. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9592. re4 (concat "[^a-zA-Z_]" re4_)
  9593. re1 (concat pre re2 post)
  9594. re3 (concat pre (if pre re4_ re4) post)
  9595. re5 (concat pre ".*" re4)
  9596. re2 (concat pre re2)
  9597. re2a (concat pre (if pre re2a_ re2a))
  9598. re4 (concat pre (if pre re4_ re4))
  9599. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9600. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9601. re5 "\\)"
  9602. ))
  9603. (cond
  9604. ((eq type 'org-occur) (org-occur reall))
  9605. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9606. (t (goto-char (point-min))
  9607. (setq type 'fuzzy)
  9608. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9609. (org-search-not-self 1 re1 nil t)
  9610. (org-search-not-self 1 re2 nil t)
  9611. (org-search-not-self 1 re2a nil t)
  9612. (org-search-not-self 1 re3 nil t)
  9613. (org-search-not-self 1 re4 nil t)
  9614. (org-search-not-self 1 re5 nil t)
  9615. )
  9616. (goto-char (match-beginning 1))
  9617. (goto-char pos)
  9618. (error "No match"))))))
  9619. (and (derived-mode-p 'org-mode)
  9620. (not stealth)
  9621. (org-show-context 'link-search))
  9622. type))
  9623. (defun org-search-not-self (group &rest args)
  9624. "Execute `re-search-forward', but only accept matches that do not
  9625. enclose the position of `org-open-link-marker'."
  9626. (let ((m org-open-link-marker))
  9627. (catch 'exit
  9628. (while (apply 're-search-forward args)
  9629. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9630. (goto-char (match-end group))
  9631. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9632. (> (match-beginning 0) (marker-position m))
  9633. (< (match-end 0) (marker-position m)))
  9634. (save-match-data
  9635. (or (not (org-in-regexp
  9636. org-bracket-link-analytic-regexp 1))
  9637. (not (match-end 4)) ; no description
  9638. (and (<= (match-beginning 4) (point))
  9639. (>= (match-end 4) (point))))))
  9640. (throw 'exit (point))))))))
  9641. (defun org-get-buffer-for-internal-link (buffer)
  9642. "Return a buffer to be used for displaying the link target of internal links."
  9643. (cond
  9644. ((not org-display-internal-link-with-indirect-buffer)
  9645. buffer)
  9646. ((string-match "(Clone)$" (buffer-name buffer))
  9647. (message "Buffer is already a clone, not making another one")
  9648. ;; we also do not modify visibility in this case
  9649. buffer)
  9650. (t ; make a new indirect buffer for displaying the link
  9651. (let* ((bn (buffer-name buffer))
  9652. (ibn (concat bn "(Clone)"))
  9653. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9654. (with-current-buffer ib (org-overview))
  9655. ib))))
  9656. (defun org-do-occur (regexp &optional cleanup)
  9657. "Call the Emacs command `occur'.
  9658. If CLEANUP is non-nil, remove the printout of the regular expression
  9659. in the *Occur* buffer. This is useful if the regex is long and not useful
  9660. to read."
  9661. (occur regexp)
  9662. (when cleanup
  9663. (let ((cwin (selected-window)) win beg end)
  9664. (when (setq win (get-buffer-window "*Occur*"))
  9665. (select-window win))
  9666. (goto-char (point-min))
  9667. (when (re-search-forward "match[a-z]+" nil t)
  9668. (setq beg (match-end 0))
  9669. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9670. (setq end (1- (match-beginning 0)))))
  9671. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9672. (goto-char (point-min))
  9673. (select-window cwin))))
  9674. ;;; The mark ring for links jumps
  9675. (defvar org-mark-ring nil
  9676. "Mark ring for positions before jumps in Org-mode.")
  9677. (defvar org-mark-ring-last-goto nil
  9678. "Last position in the mark ring used to go back.")
  9679. ;; Fill and close the ring
  9680. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9681. (loop for i from 1 to org-mark-ring-length do
  9682. (push (make-marker) org-mark-ring))
  9683. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9684. org-mark-ring)
  9685. (defun org-mark-ring-push (&optional pos buffer)
  9686. "Put the current position or POS into the mark ring and rotate it."
  9687. (interactive)
  9688. (setq pos (or pos (point)))
  9689. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9690. (move-marker (car org-mark-ring)
  9691. (or pos (point))
  9692. (or buffer (current-buffer)))
  9693. (message "%s"
  9694. (substitute-command-keys
  9695. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9696. (defun org-mark-ring-goto (&optional n)
  9697. "Jump to the previous position in the mark ring.
  9698. With prefix arg N, jump back that many stored positions. When
  9699. called several times in succession, walk through the entire ring.
  9700. Org-mode commands jumping to a different position in the current file,
  9701. or to another Org-mode file, automatically push the old position
  9702. onto the ring."
  9703. (interactive "p")
  9704. (let (p m)
  9705. (if (eq last-command this-command)
  9706. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9707. (setq p org-mark-ring))
  9708. (setq org-mark-ring-last-goto p)
  9709. (setq m (car p))
  9710. (org-pop-to-buffer-same-window (marker-buffer m))
  9711. (goto-char m)
  9712. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9713. (defun org-remove-angle-brackets (s)
  9714. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9715. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9716. s)
  9717. (defun org-add-angle-brackets (s)
  9718. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9719. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9720. s)
  9721. (defun org-remove-double-quotes (s)
  9722. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9723. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9724. s)
  9725. ;;; Following specific links
  9726. (defun org-follow-timestamp-link ()
  9727. "Open an agenda view for the time-stamp date/range at point."
  9728. (cond
  9729. ((org-at-date-range-p t)
  9730. (let ((org-agenda-start-on-weekday)
  9731. (t1 (match-string 1))
  9732. (t2 (match-string 2)) tt1 tt2)
  9733. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9734. tt2 (time-to-days (org-time-string-to-time t2)))
  9735. (let ((org-agenda-buffer-tmp-name
  9736. (format "*Org Agenda(a:%s)"
  9737. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9738. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9739. ((org-at-timestamp-p t)
  9740. (let ((org-agenda-buffer-tmp-name
  9741. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9742. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9743. (substring (match-string 1) 0 10)))
  9744. 1)))
  9745. (t (error "This should not happen"))))
  9746. ;;; Following file links
  9747. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9748. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9749. (declare-function mailcap-mime-info
  9750. "mailcap" (string &optional request no-decode))
  9751. (defvar org-wait nil)
  9752. (defun org-open-file (path &optional in-emacs line search)
  9753. "Open the file at PATH.
  9754. First, this expands any special file name abbreviations. Then the
  9755. configuration variable `org-file-apps' is checked if it contains an
  9756. entry for this file type, and if yes, the corresponding command is launched.
  9757. If no application is found, Emacs simply visits the file.
  9758. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9759. With a double \\[universal-argument] \\[universal-argument] \
  9760. prefix arg, Org tries to avoid opening in Emacs
  9761. and to use an external application to visit the file.
  9762. Optional LINE specifies a line to go to, optional SEARCH a string
  9763. to search for. If LINE or SEARCH is given, the file will be
  9764. opened in Emacs, unless an entry from org-file-apps that makes
  9765. use of groups in a regexp matches.
  9766. If you want to change the way frames are used when following a
  9767. link, please customize `org-link-frame-setup'.
  9768. If the file does not exist, an error is thrown."
  9769. (let* ((file (if (equal path "")
  9770. buffer-file-name
  9771. (substitute-in-file-name (expand-file-name path))))
  9772. (file-apps (append org-file-apps (org-default-apps)))
  9773. (apps (org-remove-if
  9774. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9775. (apps-dlink (org-remove-if-not
  9776. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9777. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9778. (dirp (if remp nil (file-directory-p file)))
  9779. (file (if (and dirp org-open-directory-means-index-dot-org)
  9780. (concat (file-name-as-directory file) "index.org")
  9781. file))
  9782. (a-m-a-p (assq 'auto-mode apps))
  9783. (dfile (downcase file))
  9784. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9785. (link (cond ((and (eq line nil)
  9786. (eq search nil))
  9787. file)
  9788. (line
  9789. (concat file "::" (number-to-string line)))
  9790. (search
  9791. (concat file "::" search))))
  9792. (dlink (downcase link))
  9793. (old-buffer (current-buffer))
  9794. (old-pos (point))
  9795. (old-mode major-mode)
  9796. ext cmd link-match-data)
  9797. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9798. (setq ext (match-string 1 dfile))
  9799. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9800. (setq ext (match-string 1 dfile))))
  9801. (cond
  9802. ((member in-emacs '((16) system))
  9803. (setq cmd (cdr (assoc 'system apps))))
  9804. (in-emacs (setq cmd 'emacs))
  9805. (t
  9806. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9807. (and dirp (cdr (assoc 'directory apps)))
  9808. ; first, try matching against apps-dlink
  9809. ; if we get a match here, store the match data for later
  9810. (let ((match (assoc-default dlink apps-dlink
  9811. 'string-match)))
  9812. (if match
  9813. (progn (setq link-match-data (match-data))
  9814. match)
  9815. (progn (setq in-emacs (or in-emacs line search))
  9816. nil))) ; if we have no match in apps-dlink,
  9817. ; always open the file in emacs if line or search
  9818. ; is given (for backwards compatibility)
  9819. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9820. 'string-match)
  9821. (cdr (assoc ext apps))
  9822. (cdr (assoc t apps))))))
  9823. (when (eq cmd 'system)
  9824. (setq cmd (cdr (assoc 'system apps))))
  9825. (when (eq cmd 'default)
  9826. (setq cmd (cdr (assoc t apps))))
  9827. (when (eq cmd 'mailcap)
  9828. (require 'mailcap)
  9829. (mailcap-parse-mailcaps)
  9830. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9831. (command (mailcap-mime-info mime-type)))
  9832. (if (stringp command)
  9833. (setq cmd command)
  9834. (setq cmd 'emacs))))
  9835. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9836. (not (file-exists-p file))
  9837. (not org-open-non-existing-files))
  9838. (error "No such file: %s" file))
  9839. (cond
  9840. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9841. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9842. (while (string-match "['\"]%s['\"]" cmd)
  9843. (setq cmd (replace-match "%s" t t cmd)))
  9844. (while (string-match "%s" cmd)
  9845. (setq cmd (replace-match
  9846. (save-match-data
  9847. (shell-quote-argument
  9848. (convert-standard-filename file)))
  9849. t t cmd)))
  9850. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9851. (save-match-data
  9852. (let ((match-index 1)
  9853. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9854. (set-match-data link-match-data)
  9855. (while (<= match-index number-of-groups)
  9856. (let ((regex (concat "%" (number-to-string match-index)))
  9857. (replace-with (match-string match-index dlink)))
  9858. (while (string-match regex cmd)
  9859. (setq cmd (replace-match replace-with t t cmd))))
  9860. (setq match-index (+ match-index 1)))))
  9861. (save-window-excursion
  9862. (message "Running %s...done" cmd)
  9863. (start-process-shell-command cmd nil cmd)
  9864. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9865. ((or (stringp cmd)
  9866. (eq cmd 'emacs))
  9867. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9868. (widen)
  9869. (if line (org-goto-line line)
  9870. (if search (org-link-search search))))
  9871. ((consp cmd)
  9872. (let ((file (convert-standard-filename file)))
  9873. (save-match-data
  9874. (set-match-data link-match-data)
  9875. (eval cmd))))
  9876. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9877. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9878. (or (not (equal old-buffer (current-buffer)))
  9879. (not (equal old-pos (point))))
  9880. (org-mark-ring-push old-pos old-buffer))))
  9881. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9882. "This function returns non-nil if `entry' uses a regular
  9883. expression which should be matched against the whole link by
  9884. org-open-file.
  9885. It assumes that is the case when the entry uses a regular
  9886. expression which has at least one grouping construct and the
  9887. action is either a lisp form or a command string containing
  9888. '%1', i.e. using at least one subexpression match as a
  9889. parameter."
  9890. (let ((selector (car entry))
  9891. (action (cdr entry)))
  9892. (if (stringp selector)
  9893. (and (> (regexp-opt-depth selector) 0)
  9894. (or (and (stringp action)
  9895. (string-match "%[0-9]" action))
  9896. (consp action)))
  9897. nil)))
  9898. (defun org-default-apps ()
  9899. "Return the default applications for this operating system."
  9900. (cond
  9901. ((eq system-type 'darwin)
  9902. org-file-apps-defaults-macosx)
  9903. ((eq system-type 'windows-nt)
  9904. org-file-apps-defaults-windowsnt)
  9905. (t org-file-apps-defaults-gnu)))
  9906. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9907. "Convert extensions to regular expressions in the cars of LIST.
  9908. Also, weed out any non-string entries, because the return value is used
  9909. only for regexp matching.
  9910. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9911. point to the symbol `emacs', indicating that the file should
  9912. be opened in Emacs."
  9913. (append
  9914. (delq nil
  9915. (mapcar (lambda (x)
  9916. (if (not (stringp (car x)))
  9917. nil
  9918. (if (string-match "\\W" (car x))
  9919. x
  9920. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9921. list))
  9922. (if add-auto-mode
  9923. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9924. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9925. (defun org-file-remote-p (file)
  9926. "Test whether FILE specifies a location on a remote system.
  9927. Return non-nil if the location is indeed remote.
  9928. For example, the filename \"/user@host:/foo\" specifies a location
  9929. on the system \"/user@host:\"."
  9930. (cond ((fboundp 'file-remote-p)
  9931. (file-remote-p file))
  9932. ((fboundp 'tramp-handle-file-remote-p)
  9933. (tramp-handle-file-remote-p file))
  9934. ((and (boundp 'ange-ftp-name-format)
  9935. (string-match (car ange-ftp-name-format) file))
  9936. t)))
  9937. ;;;; Refiling
  9938. (defun org-get-org-file ()
  9939. "Read a filename, with default directory `org-directory'."
  9940. (let ((default (or org-default-notes-file remember-data-file)))
  9941. (read-file-name (format "File name [%s]: " default)
  9942. (file-name-as-directory org-directory)
  9943. default)))
  9944. (defun org-notes-order-reversed-p ()
  9945. "Check if the current file should receive notes in reversed order."
  9946. (cond
  9947. ((not org-reverse-note-order) nil)
  9948. ((eq t org-reverse-note-order) t)
  9949. ((not (listp org-reverse-note-order)) nil)
  9950. (t (catch 'exit
  9951. (let ((all org-reverse-note-order)
  9952. entry)
  9953. (while (setq entry (pop all))
  9954. (if (string-match (car entry) buffer-file-name)
  9955. (throw 'exit (cdr entry))))
  9956. nil)))))
  9957. (defvar org-refile-target-table nil
  9958. "The list of refile targets, created by `org-refile'.")
  9959. (defvar org-agenda-new-buffers nil
  9960. "Buffers created to visit agenda files.")
  9961. (defvar org-refile-cache nil
  9962. "Cache for refile targets.")
  9963. (defvar org-refile-markers nil
  9964. "All the markers used for caching refile locations.")
  9965. (defun org-refile-marker (pos)
  9966. "Get a new refile marker, but only if caching is in use."
  9967. (if (not org-refile-use-cache)
  9968. pos
  9969. (let ((m (make-marker)))
  9970. (move-marker m pos)
  9971. (push m org-refile-markers)
  9972. m)))
  9973. (defun org-refile-cache-clear ()
  9974. "Clear the refile cache and disable all the markers."
  9975. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9976. (setq org-refile-markers nil)
  9977. (setq org-refile-cache nil)
  9978. (message "Refile cache has been cleared"))
  9979. (defun org-refile-cache-check-set (set)
  9980. "Check if all the markers in the cache still have live buffers."
  9981. (let (marker)
  9982. (catch 'exit
  9983. (while (and set (setq marker (nth 3 (pop set))))
  9984. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9985. (when (and marker (null (marker-buffer marker)))
  9986. (message "not found") (sit-for 3)
  9987. (throw 'exit nil)))
  9988. t)))
  9989. (defun org-refile-cache-put (set &rest identifiers)
  9990. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9991. (let* ((key (sha1 (prin1-to-string identifiers)))
  9992. (entry (assoc key org-refile-cache)))
  9993. (if entry
  9994. (setcdr entry set)
  9995. (push (cons key set) org-refile-cache))))
  9996. (defun org-refile-cache-get (&rest identifiers)
  9997. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9998. (cond
  9999. ((not org-refile-cache) nil)
  10000. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10001. (t
  10002. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10003. org-refile-cache))))
  10004. (and set (org-refile-cache-check-set set) set)))))
  10005. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10006. "Produce a table with refile targets."
  10007. (let ((case-fold-search nil)
  10008. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10009. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10010. targets tgs txt re files f desc descre fast-path-p level pos0)
  10011. (message "Getting targets...")
  10012. (with-current-buffer (or default-buffer (current-buffer))
  10013. (while (setq entry (pop entries))
  10014. (setq files (car entry) desc (cdr entry))
  10015. (setq fast-path-p nil)
  10016. (cond
  10017. ((null files) (setq files (list (current-buffer))))
  10018. ((eq files 'org-agenda-files)
  10019. (setq files (org-agenda-files 'unrestricted)))
  10020. ((and (symbolp files) (fboundp files))
  10021. (setq files (funcall files)))
  10022. ((and (symbolp files) (boundp files))
  10023. (setq files (symbol-value files))))
  10024. (if (stringp files) (setq files (list files)))
  10025. (cond
  10026. ((eq (car desc) :tag)
  10027. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10028. ((eq (car desc) :todo)
  10029. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10030. ((eq (car desc) :regexp)
  10031. (setq descre (cdr desc)))
  10032. ((eq (car desc) :level)
  10033. (setq descre (concat "^\\*\\{" (number-to-string
  10034. (if org-odd-levels-only
  10035. (1- (* 2 (cdr desc)))
  10036. (cdr desc)))
  10037. "\\}[ \t]")))
  10038. ((eq (car desc) :maxlevel)
  10039. (setq fast-path-p t)
  10040. (setq descre (concat "^\\*\\{1," (number-to-string
  10041. (if org-odd-levels-only
  10042. (1- (* 2 (cdr desc)))
  10043. (cdr desc)))
  10044. "\\}[ \t]")))
  10045. (t (error "Bad refiling target description %s" desc)))
  10046. (while (setq f (pop files))
  10047. (with-current-buffer
  10048. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10049. (or
  10050. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10051. (progn
  10052. (if (bufferp f) (setq f (buffer-file-name
  10053. (buffer-base-buffer f))))
  10054. (setq f (and f (expand-file-name f)))
  10055. (if (eq org-refile-use-outline-path 'file)
  10056. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10057. (save-excursion
  10058. (save-restriction
  10059. (widen)
  10060. (goto-char (point-min))
  10061. (while (re-search-forward descre nil t)
  10062. (goto-char (setq pos0 (point-at-bol)))
  10063. (catch 'next
  10064. (when org-refile-target-verify-function
  10065. (save-match-data
  10066. (or (funcall org-refile-target-verify-function)
  10067. (throw 'next t))))
  10068. (when (and (looking-at org-complex-heading-regexp)
  10069. (not (member (match-string 4) excluded-entries))
  10070. (match-string 4))
  10071. (setq level (org-reduced-level
  10072. (- (match-end 1) (match-beginning 1)))
  10073. txt (org-link-display-format (match-string 4))
  10074. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10075. re (format org-complex-heading-regexp-format
  10076. (regexp-quote (match-string 4))))
  10077. (when org-refile-use-outline-path
  10078. (setq txt (mapconcat
  10079. 'org-protect-slash
  10080. (append
  10081. (if (eq org-refile-use-outline-path
  10082. 'file)
  10083. (list (file-name-nondirectory
  10084. (buffer-file-name
  10085. (buffer-base-buffer))))
  10086. (if (eq org-refile-use-outline-path
  10087. 'full-file-path)
  10088. (list (buffer-file-name
  10089. (buffer-base-buffer)))))
  10090. (org-get-outline-path fast-path-p
  10091. level txt)
  10092. (list txt))
  10093. "/")))
  10094. (push (list txt f re (org-refile-marker (point)))
  10095. tgs)))
  10096. (when (= (point) pos0)
  10097. ;; verification function has not moved point
  10098. (goto-char (point-at-eol))))))))
  10099. (when org-refile-use-cache
  10100. (org-refile-cache-put tgs (buffer-file-name) descre))
  10101. (setq targets (append tgs targets))
  10102. ))))
  10103. (message "Getting targets...done")
  10104. (nreverse targets)))
  10105. (defun org-protect-slash (s)
  10106. (while (string-match "/" s)
  10107. (setq s (replace-match "\\" t t s)))
  10108. s)
  10109. (defvar org-olpa (make-vector 20 nil))
  10110. (defun org-get-outline-path (&optional fastp level heading)
  10111. "Return the outline path to the current entry, as a list.
  10112. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10113. routine which makes outline path derivations for an entire file,
  10114. avoiding backtracing. Refile target collection makes use of that."
  10115. (if fastp
  10116. (progn
  10117. (if (> level 19)
  10118. (error "Outline path failure, more than 19 levels"))
  10119. (loop for i from level upto 19 do
  10120. (aset org-olpa i nil))
  10121. (prog1
  10122. (delq nil (append org-olpa nil))
  10123. (aset org-olpa level heading)))
  10124. (let (rtn case-fold-search)
  10125. (save-excursion
  10126. (save-restriction
  10127. (widen)
  10128. (while (org-up-heading-safe)
  10129. (when (looking-at org-complex-heading-regexp)
  10130. (push (org-match-string-no-properties 4) rtn)))
  10131. rtn)))))
  10132. (defun org-format-outline-path (path &optional width prefix separator)
  10133. "Format the outline path PATH for display.
  10134. WIDTH is the maximum number of characters that is available.
  10135. PREFIX is a prefix to be included in the returned string,
  10136. such as the file name.
  10137. SEPARATOR is inserted between the different parts of the path,
  10138. the default is \"/\"."
  10139. (setq width (or width 79))
  10140. (if prefix (setq width (- width (length prefix))))
  10141. (if (not path)
  10142. (or prefix "")
  10143. (let* ((nsteps (length path))
  10144. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10145. (maxwidth (if (<= total-width width)
  10146. 10000 ;; everything fits
  10147. ;; we need to shorten the level headings
  10148. (/ (- width nsteps) nsteps)))
  10149. (org-odd-levels-only nil)
  10150. (n 0)
  10151. (total (1+ (length prefix))))
  10152. (setq maxwidth (max maxwidth 10))
  10153. (concat prefix
  10154. (if prefix (or separator "/"))
  10155. (mapconcat
  10156. (lambda (h)
  10157. (setq n (1+ n))
  10158. (if (and (= n nsteps) (< maxwidth 10000))
  10159. (setq maxwidth (- total-width total)))
  10160. (if (< (length h) maxwidth)
  10161. (progn (setq total (+ total (length h) 1)) h)
  10162. (setq h (substring h 0 (- maxwidth 2))
  10163. total (+ total maxwidth 1))
  10164. (if (string-match "[ \t]+\\'" h)
  10165. (setq h (substring h 0 (match-beginning 0))))
  10166. (setq h (concat h "..")))
  10167. (org-add-props h nil 'face
  10168. (nth (% (1- n) org-n-level-faces)
  10169. org-level-faces))
  10170. h)
  10171. path (or separator "/"))))))
  10172. (defun org-display-outline-path (&optional file current separator just-return-string)
  10173. "Display the current outline path in the echo area.
  10174. If FILE is non-nil, prepend the output with the file name.
  10175. If CURRENT is non-nil, append the current heading to the output.
  10176. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10177. the different parts of the path and defaults to \"/\".
  10178. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10179. (interactive "P")
  10180. (let* (case-fold-search
  10181. message-log-max ; Don't populate the *Messages* buffer
  10182. (bfn (buffer-file-name (buffer-base-buffer)))
  10183. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10184. res)
  10185. (if current (setq path (append path
  10186. (save-excursion
  10187. (org-back-to-heading t)
  10188. (if (looking-at org-complex-heading-regexp)
  10189. (list (match-string 4)))))))
  10190. (setq res
  10191. (org-format-outline-path
  10192. path
  10193. (1- (frame-width))
  10194. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10195. separator))
  10196. (if just-return-string
  10197. (org-no-properties res)
  10198. (message "%s" res))))
  10199. (defvar org-refile-history nil
  10200. "History for refiling operations.")
  10201. (defvar org-after-refile-insert-hook nil
  10202. "Hook run after `org-refile' has inserted its stuff at the new location.
  10203. Note that this is still *before* the stuff will be removed from
  10204. the *old* location.")
  10205. (defvar org-capture-last-stored-marker)
  10206. (defvar org-refile-keep nil
  10207. "Non-nil means `org-refile' will copy instead of refile.")
  10208. (defun org-copy ()
  10209. "Like `org-refile', but copy."
  10210. (interactive)
  10211. (let ((org-refile-keep t))
  10212. (funcall 'org-refile nil nil nil "Copy")))
  10213. (defun org-refile (&optional goto default-buffer rfloc msg)
  10214. "Move the entry or entries at point to another heading.
  10215. The list of target headings is compiled using the information in
  10216. `org-refile-targets', which see.
  10217. At the target location, the entry is filed as a subitem of the target
  10218. heading. Depending on `org-reverse-note-order', the new subitem will
  10219. either be the first or the last subitem.
  10220. If there is an active region, all entries in that region will be moved.
  10221. However, the region must fulfill the requirement that the first heading
  10222. is the first one sets the top-level of the moved text - at most siblings
  10223. below it are allowed.
  10224. With prefix arg GOTO, the command will only visit the target location
  10225. and not actually move anything.
  10226. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10227. go to the location where the last refiling operation has put the subtree.
  10228. With a prefix argument of `2', refile to the running clock.
  10229. RFLOC can be a refile location obtained in a different way.
  10230. MSG is a string to replace \"Refile\" in the default prompt with
  10231. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10232. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10233. If you are using target caching (see `org-refile-use-cache'),
  10234. you have to clear the target cache in order to find new targets.
  10235. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10236. prefix argument (`C-u C-u C-u C-c C-w')."
  10237. (interactive "P")
  10238. (if (member goto '(0 (64)))
  10239. (org-refile-cache-clear)
  10240. (let* ((actionmsg (or msg "Refile"))
  10241. (cbuf (current-buffer))
  10242. (regionp (org-region-active-p))
  10243. (region-start (and regionp (region-beginning)))
  10244. (region-end (and regionp (region-end)))
  10245. (region-length (and regionp (- region-end region-start)))
  10246. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10247. pos it nbuf file re level reversed)
  10248. (setq last-command nil)
  10249. (when regionp
  10250. (goto-char region-start)
  10251. (or (bolp) (goto-char (point-at-bol)))
  10252. (setq region-start (point))
  10253. (unless (or (org-kill-is-subtree-p
  10254. (buffer-substring region-start region-end))
  10255. (prog1 org-refile-active-region-within-subtree
  10256. (org-toggle-heading)))
  10257. (error "The region is not a (sequence of) subtree(s)")))
  10258. (if (equal goto '(16))
  10259. (org-refile-goto-last-stored)
  10260. (when (or
  10261. (and (equal goto 2)
  10262. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10263. (prog1
  10264. (setq it (list (or org-clock-heading "running clock")
  10265. (buffer-file-name
  10266. (marker-buffer org-clock-hd-marker))
  10267. ""
  10268. (marker-position org-clock-hd-marker)))
  10269. (setq goto nil)))
  10270. (setq it (or rfloc
  10271. (let (heading-text)
  10272. (save-excursion
  10273. (unless goto
  10274. (org-back-to-heading t)
  10275. (setq heading-text
  10276. (nth 4 (org-heading-components))))
  10277. (org-refile-get-location
  10278. (cond (goto "Goto")
  10279. (regionp (concat actionmsg " region to"))
  10280. (t (concat actionmsg " subtree \""
  10281. heading-text "\" to")))
  10282. default-buffer
  10283. (and (not (equal '(4) goto))
  10284. org-refile-allow-creating-parent-nodes)
  10285. goto))))))
  10286. (setq file (nth 1 it)
  10287. re (nth 2 it)
  10288. pos (nth 3 it))
  10289. (if (and (not goto)
  10290. pos
  10291. (equal (buffer-file-name) file)
  10292. (if regionp
  10293. (and (>= pos region-start)
  10294. (<= pos region-end))
  10295. (and (>= pos (point))
  10296. (< pos (save-excursion
  10297. (org-end-of-subtree t t))))))
  10298. (error "Cannot refile to position inside the tree or region"))
  10299. (setq nbuf (or (find-buffer-visiting file)
  10300. (find-file-noselect file)))
  10301. (if goto
  10302. (progn
  10303. (org-pop-to-buffer-same-window nbuf)
  10304. (goto-char pos)
  10305. (org-show-context 'org-goto))
  10306. (if regionp
  10307. (progn
  10308. (org-kill-new (buffer-substring region-start region-end))
  10309. (org-save-markers-in-region region-start region-end))
  10310. (org-copy-subtree 1 nil t))
  10311. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10312. (find-file-noselect file)))
  10313. (setq reversed (org-notes-order-reversed-p))
  10314. (save-excursion
  10315. (save-restriction
  10316. (widen)
  10317. (if pos
  10318. (progn
  10319. (goto-char pos)
  10320. (looking-at org-outline-regexp)
  10321. (setq level (org-get-valid-level (funcall outline-level) 1))
  10322. (goto-char
  10323. (if reversed
  10324. (or (outline-next-heading) (point-max))
  10325. (or (save-excursion (org-get-next-sibling))
  10326. (org-end-of-subtree t t)
  10327. (point-max)))))
  10328. (setq level 1)
  10329. (if (not reversed)
  10330. (goto-char (point-max))
  10331. (goto-char (point-min))
  10332. (or (outline-next-heading) (goto-char (point-max)))))
  10333. (if (not (bolp)) (newline))
  10334. (org-paste-subtree level)
  10335. (when org-log-refile
  10336. (org-add-log-setup 'refile nil nil 'findpos
  10337. org-log-refile)
  10338. (unless (eq org-log-refile 'note)
  10339. (save-excursion (org-add-log-note))))
  10340. (and org-auto-align-tags
  10341. (let ((org-loop-over-headlines-in-active-region nil))
  10342. (org-set-tags nil t)))
  10343. (with-demoted-errors
  10344. (bookmark-set "org-refile-last-stored"))
  10345. ;; If we are refiling for capture, make sure that the
  10346. ;; last-capture pointers point here
  10347. (when (org-bound-and-true-p org-refile-for-capture)
  10348. (with-demoted-errors
  10349. (bookmark-set "org-capture-last-stored-marker"))
  10350. (move-marker org-capture-last-stored-marker (point)))
  10351. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10352. (run-hooks 'org-after-refile-insert-hook))))
  10353. (unless org-refile-keep
  10354. (if regionp
  10355. (delete-region (point) (+ (point) region-length))
  10356. (org-cut-subtree)))
  10357. (when (featurep 'org-inlinetask)
  10358. (org-inlinetask-remove-END-maybe))
  10359. (setq org-markers-to-move nil)
  10360. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10361. (defun org-refile-goto-last-stored ()
  10362. "Go to the location where the last refile was stored."
  10363. (interactive)
  10364. (bookmark-jump "org-refile-last-stored")
  10365. (message "This is the location of the last refile"))
  10366. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10367. no-exclude)
  10368. "Prompt the user for a refile location, using PROMPT.
  10369. PROMPT should not be suffixed with a colon and a space, because
  10370. this function appends the default value from
  10371. `org-refile-history' automatically, if that is not empty.
  10372. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10373. this is used for the GOTO interface."
  10374. (let ((org-refile-targets org-refile-targets)
  10375. (org-refile-use-outline-path org-refile-use-outline-path)
  10376. excluded-entries)
  10377. (when (and (derived-mode-p 'org-mode)
  10378. (not org-refile-use-cache)
  10379. (not no-exclude))
  10380. (org-map-tree
  10381. (lambda()
  10382. (setq excluded-entries
  10383. (append excluded-entries (list (org-get-heading t t)))))))
  10384. (setq org-refile-target-table
  10385. (org-refile-get-targets default-buffer excluded-entries)))
  10386. (unless org-refile-target-table
  10387. (error "No refile targets"))
  10388. (let* ((cbuf (current-buffer))
  10389. (partial-completion-mode nil)
  10390. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10391. (cfunc (if (and org-refile-use-outline-path
  10392. org-outline-path-complete-in-steps)
  10393. 'org-olpath-completing-read
  10394. 'org-icompleting-read))
  10395. (extra (if org-refile-use-outline-path "/" ""))
  10396. (cbnex (concat (buffer-name) extra))
  10397. (filename (and cfn (expand-file-name cfn)))
  10398. (tbl (mapcar
  10399. (lambda (x)
  10400. (if (and (not (member org-refile-use-outline-path
  10401. '(file full-file-path)))
  10402. (not (equal filename (nth 1 x))))
  10403. (cons (concat (car x) extra " ("
  10404. (file-name-nondirectory (nth 1 x)) ")")
  10405. (cdr x))
  10406. (cons (concat (car x) extra) (cdr x))))
  10407. org-refile-target-table))
  10408. (completion-ignore-case t)
  10409. cdef
  10410. (prompt (concat prompt
  10411. (or (and (car org-refile-history)
  10412. (concat " (default " (car org-refile-history) ")"))
  10413. (and (assoc cbnex tbl) (setq cdef cbnex)
  10414. (concat " (default " cbnex ")"))) ": "))
  10415. pa answ parent-target child parent old-hist)
  10416. (setq old-hist org-refile-history)
  10417. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10418. nil 'org-refile-history (or cdef (car org-refile-history))))
  10419. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10420. (org-refile-check-position pa)
  10421. (if pa
  10422. (progn
  10423. (when (or (not org-refile-history)
  10424. (not (eq old-hist org-refile-history))
  10425. (not (equal (car pa) (car org-refile-history))))
  10426. (setq org-refile-history
  10427. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10428. org-refile-history
  10429. (cdr org-refile-history))))
  10430. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10431. (pop org-refile-history)))
  10432. pa)
  10433. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10434. (progn
  10435. (setq parent (match-string 1 answ)
  10436. child (match-string 2 answ))
  10437. (setq parent-target (or (assoc parent tbl)
  10438. (assoc (concat parent "/") tbl)))
  10439. (when (and parent-target
  10440. (or (eq new-nodes t)
  10441. (and (eq new-nodes 'confirm)
  10442. (y-or-n-p (format "Create new node \"%s\"? "
  10443. child)))))
  10444. (org-refile-new-child parent-target child)))
  10445. (error "Invalid target location")))))
  10446. (declare-function org-string-nw-p "org-macs" (s))
  10447. (defun org-refile-check-position (refile-pointer)
  10448. "Check if the refile pointer matches the headline to which it points."
  10449. (let* ((file (nth 1 refile-pointer))
  10450. (re (nth 2 refile-pointer))
  10451. (pos (nth 3 refile-pointer))
  10452. buffer)
  10453. (if (and (not (markerp pos)) (not file))
  10454. (error "Please save the buffer to a file before refiling")
  10455. (when (org-string-nw-p re)
  10456. (setq buffer (if (markerp pos)
  10457. (marker-buffer pos)
  10458. (or (find-buffer-visiting file)
  10459. (find-file-noselect file))))
  10460. (with-current-buffer buffer
  10461. (save-excursion
  10462. (save-restriction
  10463. (widen)
  10464. (goto-char pos)
  10465. (beginning-of-line 1)
  10466. (unless (org-looking-at-p re)
  10467. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10468. (defun org-refile-new-child (parent-target child)
  10469. "Use refile target PARENT-TARGET to add new CHILD below it."
  10470. (unless parent-target
  10471. (error "Cannot find parent for new node"))
  10472. (let ((file (nth 1 parent-target))
  10473. (pos (nth 3 parent-target))
  10474. level)
  10475. (with-current-buffer (or (find-buffer-visiting file)
  10476. (find-file-noselect file))
  10477. (save-excursion
  10478. (save-restriction
  10479. (widen)
  10480. (if pos
  10481. (goto-char pos)
  10482. (goto-char (point-max))
  10483. (if (not (bolp)) (newline)))
  10484. (when (looking-at org-outline-regexp)
  10485. (setq level (funcall outline-level))
  10486. (org-end-of-subtree t t))
  10487. (org-back-over-empty-lines)
  10488. (insert "\n" (make-string
  10489. (if pos (org-get-valid-level level 1) 1) ?*)
  10490. " " child "\n")
  10491. (beginning-of-line 0)
  10492. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10493. (defun org-olpath-completing-read (prompt collection &rest args)
  10494. "Read an outline path like a file name."
  10495. (let ((thetable collection)
  10496. (org-completion-use-ido nil) ; does not work with ido.
  10497. (org-completion-use-iswitchb nil)) ; or iswitchb
  10498. (apply
  10499. 'org-icompleting-read prompt
  10500. (lambda (string predicate &optional flag)
  10501. (let (rtn r f (l (length string)))
  10502. (cond
  10503. ((eq flag nil)
  10504. ;; try completion
  10505. (try-completion string thetable))
  10506. ((eq flag t)
  10507. ;; all-completions
  10508. (setq rtn (all-completions string thetable predicate))
  10509. (mapcar
  10510. (lambda (x)
  10511. (setq r (substring x l))
  10512. (if (string-match " ([^)]*)$" x)
  10513. (setq f (match-string 0 x))
  10514. (setq f ""))
  10515. (if (string-match "/" r)
  10516. (concat string (substring r 0 (match-end 0)) f)
  10517. x))
  10518. rtn))
  10519. ((eq flag 'lambda)
  10520. ;; exact match?
  10521. (assoc string thetable)))))
  10522. args)))
  10523. ;;;; Dynamic blocks
  10524. (defun org-find-dblock (name)
  10525. "Find the first dynamic block with name NAME in the buffer.
  10526. If not found, stay at current position and return nil."
  10527. (let ((case-fold-search t) pos)
  10528. (save-excursion
  10529. (goto-char (point-min))
  10530. (setq pos (and (re-search-forward
  10531. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10532. (match-beginning 0))))
  10533. (if pos (goto-char pos))
  10534. pos))
  10535. (defconst org-dblock-start-re
  10536. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10537. "Matches the start line of a dynamic block, with parameters.")
  10538. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10539. "Matches the end of a dynamic block.")
  10540. (defun org-create-dblock (plist)
  10541. "Create a dynamic block section, with parameters taken from PLIST.
  10542. PLIST must contain a :name entry which is used as name of the block."
  10543. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10544. (end-of-line 1)
  10545. (newline))
  10546. (let ((col (current-column))
  10547. (name (plist-get plist :name)))
  10548. (insert "#+BEGIN: " name)
  10549. (while plist
  10550. (if (eq (car plist) :name)
  10551. (setq plist (cddr plist))
  10552. (insert " " (prin1-to-string (pop plist)))))
  10553. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10554. (beginning-of-line -2)))
  10555. (defun org-prepare-dblock ()
  10556. "Prepare dynamic block for refresh.
  10557. This empties the block, puts the cursor at the insert position and returns
  10558. the property list including an extra property :name with the block name."
  10559. (unless (looking-at org-dblock-start-re)
  10560. (error "Not at a dynamic block"))
  10561. (let* ((begdel (1+ (match-end 0)))
  10562. (name (org-no-properties (match-string 1)))
  10563. (params (append (list :name name)
  10564. (read (concat "(" (match-string 3) ")")))))
  10565. (save-excursion
  10566. (beginning-of-line 1)
  10567. (skip-chars-forward " \t")
  10568. (setq params (plist-put params :indentation-column (current-column))))
  10569. (unless (re-search-forward org-dblock-end-re nil t)
  10570. (error "Dynamic block not terminated"))
  10571. (setq params
  10572. (append params
  10573. (list :content (buffer-substring
  10574. begdel (match-beginning 0)))))
  10575. (delete-region begdel (match-beginning 0))
  10576. (goto-char begdel)
  10577. (open-line 1)
  10578. params))
  10579. (defun org-map-dblocks (&optional command)
  10580. "Apply COMMAND to all dynamic blocks in the current buffer.
  10581. If COMMAND is not given, use `org-update-dblock'."
  10582. (let ((cmd (or command 'org-update-dblock)))
  10583. (save-excursion
  10584. (goto-char (point-min))
  10585. (while (re-search-forward org-dblock-start-re nil t)
  10586. (goto-char (match-beginning 0))
  10587. (save-excursion
  10588. (condition-case nil
  10589. (funcall cmd)
  10590. (error (message "Error during update of dynamic block"))))
  10591. (unless (re-search-forward org-dblock-end-re nil t)
  10592. (error "Dynamic block not terminated"))))))
  10593. (defun org-dblock-update (&optional arg)
  10594. "User command for updating dynamic blocks.
  10595. Update the dynamic block at point. With prefix ARG, update all dynamic
  10596. blocks in the buffer."
  10597. (interactive "P")
  10598. (if arg
  10599. (org-update-all-dblocks)
  10600. (or (looking-at org-dblock-start-re)
  10601. (org-beginning-of-dblock))
  10602. (org-update-dblock)))
  10603. (defun org-update-dblock ()
  10604. "Update the dynamic block at point.
  10605. This means to empty the block, parse for parameters and then call
  10606. the correct writing function."
  10607. (interactive)
  10608. (save-window-excursion
  10609. (let* ((pos (point))
  10610. (line (org-current-line))
  10611. (params (org-prepare-dblock))
  10612. (name (plist-get params :name))
  10613. (indent (plist-get params :indentation-column))
  10614. (cmd (intern (concat "org-dblock-write:" name))))
  10615. (message "Updating dynamic block `%s' at line %d..." name line)
  10616. (funcall cmd params)
  10617. (message "Updating dynamic block `%s' at line %d...done" name line)
  10618. (goto-char pos)
  10619. (when (and indent (> indent 0))
  10620. (setq indent (make-string indent ?\ ))
  10621. (save-excursion
  10622. (org-beginning-of-dblock)
  10623. (forward-line 1)
  10624. (while (not (looking-at org-dblock-end-re))
  10625. (insert indent)
  10626. (beginning-of-line 2))
  10627. (when (looking-at org-dblock-end-re)
  10628. (and (looking-at "[ \t]+")
  10629. (replace-match ""))
  10630. (insert indent)))))))
  10631. (defun org-beginning-of-dblock ()
  10632. "Find the beginning of the dynamic block at point.
  10633. Error if there is no such block at point."
  10634. (let ((pos (point))
  10635. beg)
  10636. (end-of-line 1)
  10637. (if (and (re-search-backward org-dblock-start-re nil t)
  10638. (setq beg (match-beginning 0))
  10639. (re-search-forward org-dblock-end-re nil t)
  10640. (> (match-end 0) pos))
  10641. (goto-char beg)
  10642. (goto-char pos)
  10643. (error "Not in a dynamic block"))))
  10644. (defun org-update-all-dblocks ()
  10645. "Update all dynamic blocks in the buffer.
  10646. This function can be used in a hook."
  10647. (interactive)
  10648. (when (derived-mode-p 'org-mode)
  10649. (org-map-dblocks 'org-update-dblock)))
  10650. ;;;; Completion
  10651. (defun org-get-export-keywords ()
  10652. "Return a list of all currently understood export keywords.
  10653. Export keywords include options, block names, attributes and
  10654. keywords relative to each registered export back-end."
  10655. (delq nil
  10656. (let (keywords)
  10657. (mapc
  10658. (lambda (back-end)
  10659. (let ((props (cdr back-end)))
  10660. ;; Back-end name (for keywords, like #+LATEX:)
  10661. (push (upcase (symbol-name (car back-end))) keywords)
  10662. ;; Back-end options.
  10663. (mapc (lambda (option) (push (cadr option) keywords))
  10664. (plist-get (cdr back-end) :options-alist))))
  10665. (org-bound-and-true-p org-export-registered-backends))
  10666. keywords)))
  10667. (defconst org-options-keywords
  10668. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10669. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10670. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10671. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10672. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10673. (defcustom org-structure-template-alist
  10674. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10675. "<src lang=\"?\">\n\n</src>")
  10676. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10677. "<example>\n?\n</example>")
  10678. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10679. "<quote>\n?\n</quote>")
  10680. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10681. "<verse>\n?\n</verse>")
  10682. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10683. "<verbatim>\n?\n</verbatim>")
  10684. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10685. "<center>\n?\n</center>")
  10686. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10687. "<literal style=\"latex\">\n?\n</literal>")
  10688. ("L" "#+LaTeX: "
  10689. "<literal style=\"latex\">?</literal>")
  10690. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10691. "<literal style=\"html\">\n?\n</literal>")
  10692. ("H" "#+HTML: "
  10693. "<literal style=\"html\">?</literal>")
  10694. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10695. ("A" "#+ASCII: ")
  10696. ("i" "#+INDEX: ?"
  10697. "#+INDEX: ?")
  10698. ("I" "#+INCLUDE: %file ?"
  10699. "<include file=%file markup=\"?\">"))
  10700. "Structure completion elements.
  10701. This is a list of abbreviation keys and values. The value gets inserted
  10702. if you type `<' followed by the key and then press the completion key,
  10703. usually `M-TAB'. %file will be replaced by a file name after prompting
  10704. for the file using completion. The cursor will be placed at the position
  10705. of the `?` in the template.
  10706. There are two templates for each key, the first uses the original Org syntax,
  10707. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10708. the default when the /org-mtags.el/ module has been loaded. See also the
  10709. variable `org-mtags-prefer-muse-templates'."
  10710. :group 'org-completion
  10711. :type '(repeat
  10712. (string :tag "Key")
  10713. (string :tag "Template")
  10714. (string :tag "Muse Template")))
  10715. (defun org-try-structure-completion ()
  10716. "Try to complete a structure template before point.
  10717. This looks for strings like \"<e\" on an otherwise empty line and
  10718. expands them."
  10719. (let ((l (buffer-substring (point-at-bol) (point)))
  10720. a)
  10721. (when (and (looking-at "[ \t]*$")
  10722. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10723. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10724. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10725. (match-beginning 1)) a)
  10726. t)))
  10727. (defun org-complete-expand-structure-template (start cell)
  10728. "Expand a structure template."
  10729. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10730. (rpl (nth (if musep 2 1) cell))
  10731. (ind ""))
  10732. (delete-region start (point))
  10733. (when (string-match "\\`#\\+" rpl)
  10734. (cond
  10735. ((bolp))
  10736. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10737. (setq ind (buffer-substring (point-at-bol) (point))))
  10738. (t (newline))))
  10739. (setq start (point))
  10740. (if (string-match "%file" rpl)
  10741. (setq rpl (replace-match
  10742. (concat
  10743. "\""
  10744. (save-match-data
  10745. (abbreviate-file-name (read-file-name "Include file: ")))
  10746. "\"")
  10747. t t rpl)))
  10748. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10749. (concat "\n" ind)))
  10750. (insert rpl)
  10751. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10752. ;;;; TODO, DEADLINE, Comments
  10753. (defun org-toggle-comment ()
  10754. "Change the COMMENT state of an entry."
  10755. (interactive)
  10756. (save-excursion
  10757. (org-back-to-heading)
  10758. (let (case-fold-search)
  10759. (cond
  10760. ((looking-at (format org-heading-keyword-regexp-format
  10761. org-comment-string))
  10762. (goto-char (match-end 1))
  10763. (looking-at (concat " +" org-comment-string))
  10764. (replace-match "" t t)
  10765. (when (eolp) (insert " ")))
  10766. ((looking-at org-outline-regexp)
  10767. (goto-char (match-end 0))
  10768. (insert org-comment-string " "))))))
  10769. (defvar org-last-todo-state-is-todo nil
  10770. "This is non-nil when the last TODO state change led to a TODO state.
  10771. If the last change removed the TODO tag or switched to DONE, then
  10772. this is nil.")
  10773. (defvar org-setting-tags nil) ; dynamically skipped
  10774. (defvar org-todo-setup-filter-hook nil
  10775. "Hook for functions that pre-filter todo specs.
  10776. Each function takes a todo spec and returns either nil or the spec
  10777. transformed into canonical form." )
  10778. (defvar org-todo-get-default-hook nil
  10779. "Hook for functions that get a default item for todo.
  10780. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10781. nil or a string to be used for the todo mark." )
  10782. (defvar org-agenda-headline-snapshot-before-repeat)
  10783. (defun org-current-effective-time ()
  10784. "Return current time adjusted for `org-extend-today-until' variable."
  10785. (let* ((ct (org-current-time))
  10786. (dct (decode-time ct))
  10787. (ct1
  10788. (cond
  10789. (org-use-last-clock-out-time-as-effective-time
  10790. (or (org-clock-get-last-clock-out-time) ct))
  10791. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10792. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10793. (t ct))))
  10794. ct1))
  10795. (defun org-todo-yesterday (&optional arg)
  10796. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10797. (interactive "P")
  10798. (if (eq major-mode 'org-agenda-mode)
  10799. (apply 'org-agenda-todo-yesterday arg)
  10800. (let* ((hour (third (decode-time
  10801. (org-current-time))))
  10802. (org-extend-today-until (1+ hour)))
  10803. (org-todo arg))))
  10804. (defvar org-block-entry-blocking ""
  10805. "First entry preventing the TODO state change.")
  10806. (defun org-todo (&optional arg)
  10807. "Change the TODO state of an item.
  10808. The state of an item is given by a keyword at the start of the heading,
  10809. like
  10810. *** TODO Write paper
  10811. *** DONE Call mom
  10812. The different keywords are specified in the variable `org-todo-keywords'.
  10813. By default the available states are \"TODO\" and \"DONE\".
  10814. So for this example: when the item starts with TODO, it is changed to DONE.
  10815. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10816. DONE are present, add TODO at the beginning of the heading.
  10817. With \\[universal-argument] prefix arg, use completion to determine the new \
  10818. state.
  10819. With numeric prefix arg, switch to that state.
  10820. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10821. keywords (nextset).
  10822. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10823. With a numeric prefix arg of 0, inhibit note taking for the change.
  10824. For calling through lisp, arg is also interpreted in the following way:
  10825. 'none -> empty state
  10826. \"\"(empty string) -> switch to empty state
  10827. 'done -> switch to DONE
  10828. 'nextset -> switch to the next set of keywords
  10829. 'previousset -> switch to the previous set of keywords
  10830. \"WAITING\" -> switch to the specified keyword, but only if it
  10831. really is a member of `org-todo-keywords'."
  10832. (interactive "P")
  10833. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10834. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10835. 'region-start-level 'region))
  10836. org-loop-over-headlines-in-active-region)
  10837. (org-map-entries
  10838. `(org-todo ,arg)
  10839. org-loop-over-headlines-in-active-region
  10840. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10841. (if (equal arg '(16)) (setq arg 'nextset))
  10842. (let ((org-blocker-hook org-blocker-hook)
  10843. commentp
  10844. case-fold-search)
  10845. (when (equal arg '(64))
  10846. (setq arg nil org-blocker-hook nil))
  10847. (when (and org-blocker-hook
  10848. (or org-inhibit-blocking
  10849. (org-entry-get nil "NOBLOCKING")))
  10850. (setq org-blocker-hook nil))
  10851. (save-excursion
  10852. (catch 'exit
  10853. (org-back-to-heading t)
  10854. (when (looking-at (concat "^\\*+ " org-comment-string))
  10855. (org-toggle-comment)
  10856. (setq commentp t))
  10857. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10858. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10859. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10860. (let* ((match-data (match-data))
  10861. (startpos (point-at-bol))
  10862. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10863. (org-log-done org-log-done)
  10864. (org-log-repeat org-log-repeat)
  10865. (org-todo-log-states org-todo-log-states)
  10866. (org-inhibit-logging
  10867. (if (equal arg 0)
  10868. (progn (setq arg nil) 'note) org-inhibit-logging))
  10869. (this (match-string 1))
  10870. (hl-pos (match-beginning 0))
  10871. (head (org-get-todo-sequence-head this))
  10872. (ass (assoc head org-todo-kwd-alist))
  10873. (interpret (nth 1 ass))
  10874. (done-word (nth 3 ass))
  10875. (final-done-word (nth 4 ass))
  10876. (org-last-state (or this ""))
  10877. (completion-ignore-case t)
  10878. (member (member this org-todo-keywords-1))
  10879. (tail (cdr member))
  10880. (org-state (cond
  10881. ((and org-todo-key-trigger
  10882. (or (and (equal arg '(4))
  10883. (eq org-use-fast-todo-selection 'prefix))
  10884. (and (not arg) org-use-fast-todo-selection
  10885. (not (eq org-use-fast-todo-selection
  10886. 'prefix)))))
  10887. ;; Use fast selection
  10888. (org-fast-todo-selection))
  10889. ((and (equal arg '(4))
  10890. (or (not org-use-fast-todo-selection)
  10891. (not org-todo-key-trigger)))
  10892. ;; Read a state with completion
  10893. (org-icompleting-read
  10894. "State: " (mapcar (lambda(x) (list x))
  10895. org-todo-keywords-1)
  10896. nil t))
  10897. ((eq arg 'right)
  10898. (if this
  10899. (if tail (car tail) nil)
  10900. (car org-todo-keywords-1)))
  10901. ((eq arg 'left)
  10902. (if (equal member org-todo-keywords-1)
  10903. nil
  10904. (if this
  10905. (nth (- (length org-todo-keywords-1)
  10906. (length tail) 2)
  10907. org-todo-keywords-1)
  10908. (org-last org-todo-keywords-1))))
  10909. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10910. (setq arg nil))) ; hack to fall back to cycling
  10911. (arg
  10912. ;; user or caller requests a specific state
  10913. (cond
  10914. ((equal arg "") nil)
  10915. ((eq arg 'none) nil)
  10916. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10917. ((eq arg 'nextset)
  10918. (or (car (cdr (member head org-todo-heads)))
  10919. (car org-todo-heads)))
  10920. ((eq arg 'previousset)
  10921. (let ((org-todo-heads (reverse org-todo-heads)))
  10922. (or (car (cdr (member head org-todo-heads)))
  10923. (car org-todo-heads))))
  10924. ((car (member arg org-todo-keywords-1)))
  10925. ((stringp arg)
  10926. (error "State `%s' not valid in this file" arg))
  10927. ((nth (1- (prefix-numeric-value arg))
  10928. org-todo-keywords-1))))
  10929. ((null member) (or head (car org-todo-keywords-1)))
  10930. ((equal this final-done-word) nil) ;; -> make empty
  10931. ((null tail) nil) ;; -> first entry
  10932. ((memq interpret '(type priority))
  10933. (if (eq this-command last-command)
  10934. (car tail)
  10935. (if (> (length tail) 0)
  10936. (or done-word (car org-done-keywords))
  10937. nil)))
  10938. (t
  10939. (car tail))))
  10940. (org-state (or
  10941. (run-hook-with-args-until-success
  10942. 'org-todo-get-default-hook org-state org-last-state)
  10943. org-state))
  10944. (next (if org-state (concat " " org-state " ") " "))
  10945. (change-plist (list :type 'todo-state-change :from this :to org-state
  10946. :position startpos))
  10947. dolog now-done-p)
  10948. (when org-blocker-hook
  10949. (setq org-last-todo-state-is-todo
  10950. (not (member this org-done-keywords)))
  10951. (unless (save-excursion
  10952. (save-match-data
  10953. (org-with-wide-buffer
  10954. (run-hook-with-args-until-failure
  10955. 'org-blocker-hook change-plist))))
  10956. (if (org-called-interactively-p 'interactive)
  10957. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  10958. this org-state org-block-entry-blocking)
  10959. ;; fail silently
  10960. (message "TODO state change from %s to %s blocked (by \"%s\")"
  10961. this org-state org-block-entry-blocking)
  10962. (throw 'exit nil))))
  10963. (store-match-data match-data)
  10964. (replace-match next t t)
  10965. (unless (pos-visible-in-window-p hl-pos)
  10966. (message "TODO state changed to %s" (org-trim next)))
  10967. (unless head
  10968. (setq head (org-get-todo-sequence-head org-state)
  10969. ass (assoc head org-todo-kwd-alist)
  10970. interpret (nth 1 ass)
  10971. done-word (nth 3 ass)
  10972. final-done-word (nth 4 ass)))
  10973. (when (memq arg '(nextset previousset))
  10974. (message "Keyword-Set %d/%d: %s"
  10975. (- (length org-todo-sets) -1
  10976. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10977. (length org-todo-sets)
  10978. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10979. (setq org-last-todo-state-is-todo
  10980. (not (member org-state org-done-keywords)))
  10981. (setq now-done-p (and (member org-state org-done-keywords)
  10982. (not (member this org-done-keywords))))
  10983. (and logging (org-local-logging logging))
  10984. (when (and (or org-todo-log-states org-log-done)
  10985. (not (eq org-inhibit-logging t))
  10986. (not (memq arg '(nextset previousset))))
  10987. ;; we need to look at recording a time and note
  10988. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10989. (nth 2 (assoc this org-todo-log-states))))
  10990. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10991. (setq dolog 'time))
  10992. (when (and org-state
  10993. (member org-state org-not-done-keywords)
  10994. (not (member this org-not-done-keywords)))
  10995. ;; This is now a todo state and was not one before
  10996. ;; If there was a CLOSED time stamp, get rid of it.
  10997. (org-add-planning-info nil nil 'closed))
  10998. (when (and now-done-p org-log-done)
  10999. ;; It is now done, and it was not done before
  11000. (org-add-planning-info 'closed (org-current-effective-time))
  11001. (if (and (not dolog) (eq 'note org-log-done))
  11002. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11003. (when (and org-state dolog)
  11004. ;; This is a non-nil state, and we need to log it
  11005. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11006. ;; Fixup tag positioning
  11007. (org-todo-trigger-tag-changes org-state)
  11008. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11009. (when org-provide-todo-statistics
  11010. (org-update-parent-todo-statistics))
  11011. (run-hooks 'org-after-todo-state-change-hook)
  11012. (if (and arg (not (member org-state org-done-keywords)))
  11013. (setq head (org-get-todo-sequence-head org-state)))
  11014. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11015. ;; Do we need to trigger a repeat?
  11016. (when now-done-p
  11017. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11018. ;; This is for the agenda, take a snapshot of the headline.
  11019. (save-match-data
  11020. (setq org-agenda-headline-snapshot-before-repeat
  11021. (org-get-heading))))
  11022. (org-auto-repeat-maybe org-state))
  11023. ;; Fixup cursor location if close to the keyword
  11024. (if (and (outline-on-heading-p)
  11025. (not (bolp))
  11026. (save-excursion (beginning-of-line 1)
  11027. (looking-at org-todo-line-regexp))
  11028. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11029. (progn
  11030. (goto-char (or (match-end 2) (match-end 1)))
  11031. (and (looking-at " ") (just-one-space))))
  11032. (when org-trigger-hook
  11033. (save-excursion
  11034. (run-hook-with-args 'org-trigger-hook change-plist)))
  11035. (when commentp (org-toggle-comment))))))))
  11036. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11037. "Block turning an entry into a TODO, using the hierarchy.
  11038. This checks whether the current task should be blocked from state
  11039. changes. Such blocking occurs when:
  11040. 1. The task has children which are not all in a completed state.
  11041. 2. A task has a parent with the property :ORDERED:, and there
  11042. are siblings prior to the current task with incomplete
  11043. status.
  11044. 3. The parent of the task is blocked because it has siblings that should
  11045. be done first, or is child of a block grandparent TODO entry."
  11046. (if (not org-enforce-todo-dependencies)
  11047. t ; if locally turned off don't block
  11048. (catch 'dont-block
  11049. ;; If this is not a todo state change, or if this entry is already DONE,
  11050. ;; do not block
  11051. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11052. (member (plist-get change-plist :from)
  11053. (cons 'done org-done-keywords))
  11054. (member (plist-get change-plist :to)
  11055. (cons 'todo org-not-done-keywords))
  11056. (not (plist-get change-plist :to)))
  11057. (throw 'dont-block t))
  11058. ;; If this task has children, and any are undone, it's blocked
  11059. (save-excursion
  11060. (org-back-to-heading t)
  11061. (let ((this-level (funcall outline-level)))
  11062. (outline-next-heading)
  11063. (let ((child-level (funcall outline-level)))
  11064. (while (and (not (eobp))
  11065. (> child-level this-level))
  11066. ;; this todo has children, check whether they are all
  11067. ;; completed
  11068. (if (and (not (org-entry-is-done-p))
  11069. (org-entry-is-todo-p))
  11070. (progn (setq org-block-entry-blocking (org-get-heading))
  11071. (throw 'dont-block nil)))
  11072. (outline-next-heading)
  11073. (setq child-level (funcall outline-level))))))
  11074. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11075. ;; any previous siblings are undone, it's blocked
  11076. (save-excursion
  11077. (org-back-to-heading t)
  11078. (let* ((pos (point))
  11079. (parent-pos (and (org-up-heading-safe) (point))))
  11080. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11081. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11082. (forward-line 1)
  11083. (re-search-forward org-not-done-heading-regexp pos t))
  11084. (setq org-block-entry-blocking (match-string 0))
  11085. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11086. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11087. (while t
  11088. (goto-char parent-pos)
  11089. (if (not (looking-at org-not-done-heading-regexp))
  11090. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11091. (setq pos (point))
  11092. (setq parent-pos (and (org-up-heading-safe) (point)))
  11093. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11094. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11095. (forward-line 1)
  11096. (re-search-forward org-not-done-heading-regexp pos t)
  11097. (setq org-block-entry-blocking (org-get-heading)))
  11098. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11099. (defcustom org-track-ordered-property-with-tag nil
  11100. "Should the ORDERED property also be shown as a tag?
  11101. The ORDERED property decides if an entry should require subtasks to be
  11102. completed in sequence. Since a property is not very visible, setting
  11103. this option means that toggling the ORDERED property with the command
  11104. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11105. not relevant for the behavior, but it makes things more visible.
  11106. Note that toggling the tag with tags commands will not change the property
  11107. and therefore not influence behavior!
  11108. This can be t, meaning the tag ORDERED should be used, It can also be a
  11109. string to select a different tag for this task."
  11110. :group 'org-todo
  11111. :type '(choice
  11112. (const :tag "No tracking" nil)
  11113. (const :tag "Track with ORDERED tag" t)
  11114. (string :tag "Use other tag")))
  11115. (defun org-toggle-ordered-property ()
  11116. "Toggle the ORDERED property of the current entry.
  11117. For better visibility, you can track the value of this property with a tag.
  11118. See variable `org-track-ordered-property-with-tag'."
  11119. (interactive)
  11120. (let* ((t1 org-track-ordered-property-with-tag)
  11121. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11122. (save-excursion
  11123. (org-back-to-heading)
  11124. (if (org-entry-get nil "ORDERED")
  11125. (progn
  11126. (org-delete-property "ORDERED" "PROPERTIES")
  11127. (and tag (org-toggle-tag tag 'off))
  11128. (message "Subtasks can be completed in arbitrary order"))
  11129. (org-entry-put nil "ORDERED" "t")
  11130. (and tag (org-toggle-tag tag 'on))
  11131. (message "Subtasks must be completed in sequence")))))
  11132. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11133. (defun org-block-todo-from-checkboxes (change-plist)
  11134. "Block turning an entry into a TODO, using checkboxes.
  11135. This checks whether the current task should be blocked from state
  11136. changes because there are unchecked boxes in this entry."
  11137. (if (not org-enforce-todo-checkbox-dependencies)
  11138. t ; if locally turned off don't block
  11139. (catch 'dont-block
  11140. ;; If this is not a todo state change, or if this entry is already DONE,
  11141. ;; do not block
  11142. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11143. (member (plist-get change-plist :from)
  11144. (cons 'done org-done-keywords))
  11145. (member (plist-get change-plist :to)
  11146. (cons 'todo org-not-done-keywords))
  11147. (not (plist-get change-plist :to)))
  11148. (throw 'dont-block t))
  11149. ;; If this task has checkboxes that are not checked, it's blocked
  11150. (save-excursion
  11151. (org-back-to-heading t)
  11152. (let ((beg (point)) end)
  11153. (outline-next-heading)
  11154. (setq end (point))
  11155. (goto-char beg)
  11156. (if (org-list-search-forward
  11157. (concat (org-item-beginning-re)
  11158. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11159. "\\[[- ]\\]")
  11160. end t)
  11161. (progn
  11162. (if (boundp 'org-blocked-by-checkboxes)
  11163. (setq org-blocked-by-checkboxes t))
  11164. (throw 'dont-block nil)))))
  11165. t))) ; do not block
  11166. (defun org-entry-blocked-p ()
  11167. "Is the current entry blocked?"
  11168. (org-with-silent-modifications
  11169. (if (org-entry-get nil "NOBLOCKING")
  11170. nil ;; Never block this entry
  11171. (not (run-hook-with-args-until-failure
  11172. 'org-blocker-hook
  11173. (list :type 'todo-state-change
  11174. :position (point)
  11175. :from 'todo
  11176. :to 'done))))))
  11177. (defun org-update-statistics-cookies (all)
  11178. "Update the statistics cookie, either from TODO or from checkboxes.
  11179. This should be called with the cursor in a line with a statistics cookie."
  11180. (interactive "P")
  11181. (if all
  11182. (progn
  11183. (org-update-checkbox-count 'all)
  11184. (org-map-entries 'org-update-parent-todo-statistics))
  11185. (if (not (org-at-heading-p))
  11186. (org-update-checkbox-count)
  11187. (let ((pos (point-marker))
  11188. end l1 l2)
  11189. (ignore-errors (org-back-to-heading t))
  11190. (if (not (org-at-heading-p))
  11191. (org-update-checkbox-count)
  11192. (setq l1 (org-outline-level))
  11193. (setq end (save-excursion
  11194. (outline-next-heading)
  11195. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11196. (point)))
  11197. (if (and (save-excursion
  11198. (re-search-forward
  11199. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11200. (not (save-excursion (re-search-forward
  11201. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11202. (org-update-checkbox-count)
  11203. (if (and l2 (> l2 l1))
  11204. (progn
  11205. (goto-char end)
  11206. (org-update-parent-todo-statistics))
  11207. (goto-char pos)
  11208. (beginning-of-line 1)
  11209. (while (re-search-forward
  11210. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11211. (point-at-eol) t)
  11212. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11213. (goto-char pos)
  11214. (move-marker pos nil)))))
  11215. (defvar org-entry-property-inherited-from) ;; defined below
  11216. (defun org-update-parent-todo-statistics ()
  11217. "Update any statistics cookie in the parent of the current headline.
  11218. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11219. the entire subtree and this will travel up the hierarchy and update
  11220. statistics everywhere."
  11221. (let* ((prop (save-excursion (org-up-heading-safe)
  11222. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11223. (recursive (or (not org-hierarchical-todo-statistics)
  11224. (and prop (string-match "\\<recursive\\>" prop))))
  11225. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11226. 0))
  11227. (first t)
  11228. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11229. level ltoggle l1 new ndel
  11230. (cnt-all 0) (cnt-done 0) is-percent kwd
  11231. checkbox-beg ov ovs ove cookie-present)
  11232. (catch 'exit
  11233. (save-excursion
  11234. (beginning-of-line 1)
  11235. (setq ltoggle (funcall outline-level))
  11236. ;; Three situations are to consider:
  11237. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11238. ;; to the top-level ancestor on the headline;
  11239. ;; 2. If parent has "recursive" property, repeat up to the
  11240. ;; headline setting that property, taking inheritance into
  11241. ;; account;
  11242. ;; 3. Else, move up to direct parent and proceed only once.
  11243. (while (and (setq level (org-up-heading-safe))
  11244. (or recursive first)
  11245. (>= (point) lim))
  11246. (setq first nil cookie-present nil)
  11247. (unless (and level
  11248. (not (string-match
  11249. "\\<checkbox\\>"
  11250. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11251. "")))))
  11252. (throw 'exit nil))
  11253. (while (re-search-forward box-re (point-at-eol) t)
  11254. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11255. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11256. (save-match-data
  11257. (unless (outline-next-heading) (throw 'exit nil))
  11258. (while (and (looking-at org-complex-heading-regexp)
  11259. (> (setq l1 (length (match-string 1))) level))
  11260. (setq kwd (and (or recursive (= l1 ltoggle))
  11261. (match-string 2)))
  11262. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11263. (and (listp org-provide-todo-statistics)
  11264. (or (member kwd org-provide-todo-statistics)
  11265. (member kwd org-done-keywords))))
  11266. (setq cnt-all (1+ cnt-all))
  11267. (if (eq org-provide-todo-statistics t)
  11268. (and kwd (setq cnt-all (1+ cnt-all)))))
  11269. (and (member kwd org-done-keywords)
  11270. (setq cnt-done (1+ cnt-done)))
  11271. (outline-next-heading)))
  11272. (setq new
  11273. (if is-percent
  11274. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11275. (format "[%d/%d]" cnt-done cnt-all))
  11276. ndel (- (match-end 0) checkbox-beg))
  11277. ;; handle overlays when updating cookie from column view
  11278. (when (setq ov (car (overlays-at checkbox-beg)))
  11279. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11280. (delete-overlay ov))
  11281. (goto-char checkbox-beg)
  11282. (insert new)
  11283. (delete-region (point) (+ (point) ndel))
  11284. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11285. (when ov (move-overlay ov ovs ove)))
  11286. (when cookie-present
  11287. (run-hook-with-args 'org-after-todo-statistics-hook
  11288. cnt-done (- cnt-all cnt-done))))))
  11289. (run-hooks 'org-todo-statistics-hook)))
  11290. (defvar org-after-todo-statistics-hook nil
  11291. "Hook that is called after a TODO statistics cookie has been updated.
  11292. Each function is called with two arguments: the number of not-done entries
  11293. and the number of done entries.
  11294. For example, the following function, when added to this hook, will switch
  11295. an entry to DONE when all children are done, and back to TODO when new
  11296. entries are set to a TODO status. Note that this hook is only called
  11297. when there is a statistics cookie in the headline!
  11298. (defun org-summary-todo (n-done n-not-done)
  11299. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11300. (let (org-log-done org-log-states) ; turn off logging
  11301. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11302. ")
  11303. (defvar org-todo-statistics-hook nil
  11304. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11305. This hook runs even if there is no statistics cookie present, in which case
  11306. `org-after-todo-statistics-hook' would not run.")
  11307. (defun org-todo-trigger-tag-changes (state)
  11308. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11309. (let ((l org-todo-state-tags-triggers)
  11310. changes)
  11311. (when (or (not state) (equal state ""))
  11312. (setq changes (append changes (cdr (assoc "" l)))))
  11313. (when (and (stringp state) (> (length state) 0))
  11314. (setq changes (append changes (cdr (assoc state l)))))
  11315. (when (member state org-not-done-keywords)
  11316. (setq changes (append changes (cdr (assoc 'todo l)))))
  11317. (when (member state org-done-keywords)
  11318. (setq changes (append changes (cdr (assoc 'done l)))))
  11319. (dolist (c changes)
  11320. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11321. (defun org-local-logging (value)
  11322. "Get logging settings from a property VALUE."
  11323. (let* (words w a)
  11324. ;; directly set the variables, they are already local.
  11325. (setq org-log-done nil
  11326. org-log-repeat nil
  11327. org-todo-log-states nil)
  11328. (setq words (org-split-string value))
  11329. (while (setq w (pop words))
  11330. (cond
  11331. ((setq a (assoc w org-startup-options))
  11332. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11333. (set (nth 1 a) (nth 2 a))))
  11334. ((setq a (org-extract-log-state-settings w))
  11335. (and (member (car a) org-todo-keywords-1)
  11336. (push a org-todo-log-states)))))))
  11337. (defun org-get-todo-sequence-head (kwd)
  11338. "Return the head of the TODO sequence to which KWD belongs.
  11339. If KWD is not set, check if there is a text property remembering the
  11340. right sequence."
  11341. (let (p)
  11342. (cond
  11343. ((not kwd)
  11344. (or (get-text-property (point-at-bol) 'org-todo-head)
  11345. (progn
  11346. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11347. nil (point-at-eol)))
  11348. (get-text-property p 'org-todo-head))))
  11349. ((not (member kwd org-todo-keywords-1))
  11350. (car org-todo-keywords-1))
  11351. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11352. (defun org-fast-todo-selection ()
  11353. "Fast TODO keyword selection with single keys.
  11354. Returns the new TODO keyword, or nil if no state change should occur."
  11355. (let* ((fulltable org-todo-key-alist)
  11356. (done-keywords org-done-keywords) ;; needed for the faces.
  11357. (maxlen (apply 'max (mapcar
  11358. (lambda (x)
  11359. (if (stringp (car x)) (string-width (car x)) 0))
  11360. fulltable)))
  11361. (expert nil)
  11362. (fwidth (+ maxlen 3 1 3))
  11363. (ncol (/ (- (window-width) 4) fwidth))
  11364. tg cnt e c tbl
  11365. groups ingroup)
  11366. (save-excursion
  11367. (save-window-excursion
  11368. (if expert
  11369. (set-buffer (get-buffer-create " *Org todo*"))
  11370. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11371. (erase-buffer)
  11372. (org-set-local 'org-done-keywords done-keywords)
  11373. (setq tbl fulltable cnt 0)
  11374. (while (setq e (pop tbl))
  11375. (cond
  11376. ((equal e '(:startgroup))
  11377. (push '() groups) (setq ingroup t)
  11378. (when (not (= cnt 0))
  11379. (setq cnt 0)
  11380. (insert "\n"))
  11381. (insert "{ "))
  11382. ((equal e '(:endgroup))
  11383. (setq ingroup nil cnt 0)
  11384. (insert "}\n"))
  11385. ((equal e '(:newline))
  11386. (when (not (= cnt 0))
  11387. (setq cnt 0)
  11388. (insert "\n")
  11389. (setq e (car tbl))
  11390. (while (equal (car tbl) '(:newline))
  11391. (insert "\n")
  11392. (setq tbl (cdr tbl)))))
  11393. (t
  11394. (setq tg (car e) c (cdr e))
  11395. (if ingroup (push tg (car groups)))
  11396. (setq tg (org-add-props tg nil 'face
  11397. (org-get-todo-face tg)))
  11398. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11399. (insert "[" c "] " tg (make-string
  11400. (- fwidth 4 (length tg)) ?\ ))
  11401. (when (= (setq cnt (1+ cnt)) ncol)
  11402. (insert "\n")
  11403. (if ingroup (insert " "))
  11404. (setq cnt 0)))))
  11405. (insert "\n")
  11406. (goto-char (point-min))
  11407. (if (not expert) (org-fit-window-to-buffer))
  11408. (message "[a-z..]:Set [SPC]:clear")
  11409. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11410. (cond
  11411. ((or (= c ?\C-g)
  11412. (and (= c ?q) (not (rassoc c fulltable))))
  11413. (setq quit-flag t))
  11414. ((= c ?\ ) nil)
  11415. ((setq e (rassoc c fulltable) tg (car e))
  11416. tg)
  11417. (t (setq quit-flag t)))))))
  11418. (defun org-entry-is-todo-p ()
  11419. (member (org-get-todo-state) org-not-done-keywords))
  11420. (defun org-entry-is-done-p ()
  11421. (member (org-get-todo-state) org-done-keywords))
  11422. (defun org-get-todo-state ()
  11423. (save-excursion
  11424. (org-back-to-heading t)
  11425. (and (looking-at org-todo-line-regexp)
  11426. (match-end 2)
  11427. (match-string 2))))
  11428. (defun org-at-date-range-p (&optional inactive-ok)
  11429. "Is the cursor inside a date range?"
  11430. (interactive)
  11431. (save-excursion
  11432. (catch 'exit
  11433. (let ((pos (point)))
  11434. (skip-chars-backward "^[<\r\n")
  11435. (skip-chars-backward "<[")
  11436. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11437. (>= (match-end 0) pos)
  11438. (throw 'exit t))
  11439. (skip-chars-backward "^<[\r\n")
  11440. (skip-chars-backward "<[")
  11441. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11442. (>= (match-end 0) pos)
  11443. (throw 'exit t)))
  11444. nil)))
  11445. (defun org-get-repeat (&optional tagline)
  11446. "Check if there is a deadline/schedule with repeater in this entry."
  11447. (save-match-data
  11448. (save-excursion
  11449. (org-back-to-heading t)
  11450. (and (re-search-forward (if tagline
  11451. (concat tagline "\\s-*" org-repeat-re)
  11452. org-repeat-re)
  11453. (org-entry-end-position) t)
  11454. (match-string-no-properties 1)))))
  11455. (defvar org-last-changed-timestamp)
  11456. (defvar org-last-inserted-timestamp)
  11457. (defvar org-log-post-message)
  11458. (defvar org-log-note-purpose)
  11459. (defvar org-log-note-how)
  11460. (defvar org-log-note-extra)
  11461. (defun org-auto-repeat-maybe (done-word)
  11462. "Check if the current headline contains a repeated deadline/schedule.
  11463. If yes, set TODO state back to what it was and change the base date
  11464. of repeating deadline/scheduled time stamps to new date.
  11465. This function is run automatically after each state change to a DONE state."
  11466. ;; last-state is dynamically scoped into this function
  11467. (let* ((repeat (org-get-repeat))
  11468. (aa (assoc org-last-state org-todo-kwd-alist))
  11469. (interpret (nth 1 aa))
  11470. (head (nth 2 aa))
  11471. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11472. (msg "Entry repeats: ")
  11473. (org-log-done nil)
  11474. (org-todo-log-states nil)
  11475. re type n what ts time to-state)
  11476. (when repeat
  11477. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11478. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11479. org-todo-repeat-to-state))
  11480. (unless (and to-state (member to-state org-todo-keywords-1))
  11481. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11482. (org-todo to-state)
  11483. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11484. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11485. (org-time-stamp-format t t))))
  11486. (when org-log-repeat
  11487. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11488. (memq 'org-add-log-note post-command-hook))
  11489. ;; OK, we are already setup for some record
  11490. (if (eq org-log-repeat 'note)
  11491. ;; make sure we take a note, not only a time stamp
  11492. (setq org-log-note-how 'note))
  11493. ;; Set up for taking a record
  11494. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11495. org-last-state
  11496. 'findpos org-log-repeat)))
  11497. (org-back-to-heading t)
  11498. (org-add-planning-info nil nil 'closed)
  11499. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11500. org-deadline-time-regexp "\\)\\|\\("
  11501. org-ts-regexp "\\)"))
  11502. (while (re-search-forward
  11503. re (save-excursion (outline-next-heading) (point)) t)
  11504. (setq type (if (match-end 1) org-scheduled-string
  11505. (if (match-end 3) org-deadline-string "Plain:"))
  11506. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11507. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11508. (setq n (string-to-number (match-string 2 ts))
  11509. what (match-string 3 ts))
  11510. (if (equal what "w") (setq n (* n 7) what "d"))
  11511. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11512. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11513. ;; Preparation, see if we need to modify the start date for the change
  11514. (when (match-end 1)
  11515. (setq time (save-match-data (org-time-string-to-time ts)))
  11516. (cond
  11517. ((equal (match-string 1 ts) ".")
  11518. ;; Shift starting date to today
  11519. (org-timestamp-change
  11520. (- (org-today) (time-to-days time))
  11521. 'day))
  11522. ((equal (match-string 1 ts) "+")
  11523. (let ((nshiftmax 10) (nshift 0))
  11524. (while (or (= nshift 0)
  11525. (<= (time-to-days time)
  11526. (time-to-days (current-time))))
  11527. (when (= (incf nshift) nshiftmax)
  11528. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11529. (error "Abort")))
  11530. (org-timestamp-change n (cdr (assoc what whata)))
  11531. (org-at-timestamp-p t)
  11532. (setq ts (match-string 1))
  11533. (setq time (save-match-data (org-time-string-to-time ts)))))
  11534. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11535. ;; rematch, so that we have everything in place for the real shift
  11536. (org-at-timestamp-p t)
  11537. (setq ts (match-string 1))
  11538. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11539. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11540. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11541. (setq org-log-post-message msg)
  11542. (message "%s" msg))))
  11543. (defun org-show-todo-tree (arg)
  11544. "Make a compact tree which shows all headlines marked with TODO.
  11545. The tree will show the lines where the regexp matches, and all higher
  11546. headlines above the match.
  11547. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11548. With a numeric prefix N, construct a sparse tree for the Nth element
  11549. of `org-todo-keywords-1'."
  11550. (interactive "P")
  11551. (let ((case-fold-search nil)
  11552. (kwd-re
  11553. (cond ((null arg) org-not-done-regexp)
  11554. ((equal arg '(4))
  11555. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11556. (mapcar 'list org-todo-keywords-1))))
  11557. (concat "\\("
  11558. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11559. "\\)\\>")))
  11560. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11561. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11562. org-todo-keywords-1)))
  11563. (t (error "Invalid prefix argument: %s" arg)))))
  11564. (message "%d TODO entries found"
  11565. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11566. (defun org-deadline (arg &optional time)
  11567. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11568. With one universal prefix argument, remove any deadline from the item.
  11569. With two universal prefix arguments, prompt for a warning delay.
  11570. With argument TIME, set the deadline at the corresponding date. TIME
  11571. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11572. (interactive "P")
  11573. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11574. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11575. 'region-start-level 'region))
  11576. org-loop-over-headlines-in-active-region)
  11577. (org-map-entries
  11578. `(org-deadline ',arg ,time)
  11579. org-loop-over-headlines-in-active-region
  11580. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11581. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11582. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11583. (repeater (and old-date
  11584. (string-match
  11585. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11586. old-date)
  11587. (match-string 1 old-date))))
  11588. (cond
  11589. ((equal arg '(4))
  11590. (when (and old-date org-log-redeadline)
  11591. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11592. org-log-redeadline))
  11593. (org-remove-timestamp-with-keyword org-deadline-string)
  11594. (message "Item no longer has a deadline."))
  11595. ((equal arg '(16))
  11596. (save-excursion
  11597. (if (re-search-forward
  11598. org-deadline-time-regexp
  11599. (save-excursion (outline-next-heading) (point)) t)
  11600. (let* ((rpl0 (match-string 1))
  11601. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11602. (replace-match
  11603. (concat org-deadline-string
  11604. " <" rpl
  11605. (format " -%dd"
  11606. (abs
  11607. (- (time-to-days
  11608. (save-match-data
  11609. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11610. (time-to-days old-date-time))))
  11611. ">") t t))
  11612. (user-error "No deadline information to update"))))
  11613. (t
  11614. (org-add-planning-info 'deadline time 'closed)
  11615. (when (and old-date org-log-redeadline
  11616. (not (equal old-date
  11617. (substring org-last-inserted-timestamp 1 -1))))
  11618. (org-add-log-setup 'redeadline nil old-date 'findpos
  11619. org-log-redeadline))
  11620. (when repeater
  11621. (save-excursion
  11622. (org-back-to-heading t)
  11623. (when (re-search-forward (concat org-deadline-string " "
  11624. org-last-inserted-timestamp)
  11625. (save-excursion
  11626. (outline-next-heading) (point)) t)
  11627. (goto-char (1- (match-end 0)))
  11628. (insert " " repeater)
  11629. (setq org-last-inserted-timestamp
  11630. (concat (substring org-last-inserted-timestamp 0 -1)
  11631. " " repeater
  11632. (substring org-last-inserted-timestamp -1))))))
  11633. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11634. (defun org-schedule (arg &optional time)
  11635. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11636. With one universal prefix argument, remove any scheduling date from the item.
  11637. With two universal prefix arguments, prompt for a delay cookie.
  11638. With argument TIME, scheduled at the corresponding date. TIME can
  11639. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11640. (interactive "P")
  11641. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11642. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11643. 'region-start-level 'region))
  11644. org-loop-over-headlines-in-active-region)
  11645. (org-map-entries
  11646. `(org-schedule ',arg ,time)
  11647. org-loop-over-headlines-in-active-region
  11648. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11649. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11650. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11651. (repeater (and old-date
  11652. (string-match
  11653. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11654. old-date)
  11655. (match-string 1 old-date))))
  11656. (cond
  11657. ((equal arg '(4))
  11658. (progn
  11659. (when (and old-date org-log-reschedule)
  11660. (org-add-log-setup 'delschedule nil old-date 'findpos
  11661. org-log-reschedule))
  11662. (org-remove-timestamp-with-keyword org-scheduled-string)
  11663. (message "Item is no longer scheduled.")))
  11664. ((equal arg '(16))
  11665. (save-excursion
  11666. (if (re-search-forward
  11667. org-scheduled-time-regexp
  11668. (save-excursion (outline-next-heading) (point)) t)
  11669. (let* ((rpl0 (match-string 1))
  11670. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11671. (replace-match
  11672. (concat org-scheduled-string
  11673. " <" rpl
  11674. (format " -%dd"
  11675. (abs
  11676. (- (time-to-days
  11677. (save-match-data
  11678. (org-read-date nil t nil "Delay until" old-date-time)))
  11679. (time-to-days old-date-time))))
  11680. ">") t t))
  11681. (user-error "No scheduled information to update"))))
  11682. (t
  11683. (org-add-planning-info 'scheduled time 'closed)
  11684. (when (and old-date org-log-reschedule
  11685. (not (equal old-date
  11686. (substring org-last-inserted-timestamp 1 -1))))
  11687. (org-add-log-setup 'reschedule nil old-date 'findpos
  11688. org-log-reschedule))
  11689. (when repeater
  11690. (save-excursion
  11691. (org-back-to-heading t)
  11692. (when (re-search-forward (concat org-scheduled-string " "
  11693. org-last-inserted-timestamp)
  11694. (save-excursion
  11695. (outline-next-heading) (point)) t)
  11696. (goto-char (1- (match-end 0)))
  11697. (insert " " repeater)
  11698. (setq org-last-inserted-timestamp
  11699. (concat (substring org-last-inserted-timestamp 0 -1)
  11700. " " repeater
  11701. (substring org-last-inserted-timestamp -1))))))
  11702. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11703. (defun org-get-scheduled-time (pom &optional inherit)
  11704. "Get the scheduled time as a time tuple, of a format suitable
  11705. for calling org-schedule with, or if there is no scheduling,
  11706. returns nil."
  11707. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11708. (when time
  11709. (apply 'encode-time (org-parse-time-string time)))))
  11710. (defun org-get-deadline-time (pom &optional inherit)
  11711. "Get the deadline as a time tuple, of a format suitable for
  11712. calling org-deadline with, or if there is no scheduling, returns
  11713. nil."
  11714. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11715. (when time
  11716. (apply 'encode-time (org-parse-time-string time)))))
  11717. (defun org-remove-timestamp-with-keyword (keyword)
  11718. "Remove all time stamps with KEYWORD in the current entry."
  11719. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11720. beg)
  11721. (save-excursion
  11722. (org-back-to-heading t)
  11723. (setq beg (point))
  11724. (outline-next-heading)
  11725. (while (re-search-backward re beg t)
  11726. (replace-match "")
  11727. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11728. (equal (char-before) ?\ ))
  11729. (backward-delete-char 1)
  11730. (if (string-match "^[ \t]*$" (buffer-substring
  11731. (point-at-bol) (point-at-eol)))
  11732. (delete-region (point-at-bol)
  11733. (min (point-max) (1+ (point-at-eol))))))))))
  11734. (defun org-add-planning-info (what &optional time &rest remove)
  11735. "Insert new timestamp with keyword in the line directly after the headline.
  11736. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11737. If non is given, the user is prompted for a date.
  11738. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11739. be removed."
  11740. (interactive)
  11741. (let (org-time-was-given org-end-time-was-given ts
  11742. end default-time default-input)
  11743. (catch 'exit
  11744. (when (and (memq what '(scheduled deadline))
  11745. (or (not time)
  11746. (and (stringp time)
  11747. (string-match "^[-+]+[0-9]" time))))
  11748. ;; Try to get a default date/time from existing timestamp
  11749. (save-excursion
  11750. (org-back-to-heading t)
  11751. (setq end (save-excursion (outline-next-heading) (point)))
  11752. (when (re-search-forward (if (eq what 'scheduled)
  11753. org-scheduled-time-regexp
  11754. org-deadline-time-regexp)
  11755. end t)
  11756. (setq ts (match-string 1)
  11757. default-time
  11758. (apply 'encode-time (org-parse-time-string ts))
  11759. default-input (and ts (org-get-compact-tod ts))))))
  11760. (when what
  11761. (setq time
  11762. (if (stringp time)
  11763. ;; This is a string (relative or absolute), set proper date
  11764. (apply 'encode-time
  11765. (org-read-date-analyze
  11766. time default-time (decode-time default-time)))
  11767. ;; If necessary, get the time from the user
  11768. (or time (org-read-date nil 'to-time nil nil
  11769. default-time default-input)))))
  11770. (when (and org-insert-labeled-timestamps-at-point
  11771. (member what '(scheduled deadline)))
  11772. (insert
  11773. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11774. (org-insert-time-stamp time org-time-was-given
  11775. nil nil nil (list org-end-time-was-given))
  11776. (setq what nil))
  11777. (save-excursion
  11778. (save-restriction
  11779. (let (col list elt ts buffer-invisibility-spec)
  11780. (org-back-to-heading t)
  11781. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11782. (goto-char (match-end 1))
  11783. (setq col (current-column))
  11784. (goto-char (match-end 0))
  11785. (if (eobp) (insert "\n") (forward-char 1))
  11786. (when (and (not what)
  11787. (not (looking-at
  11788. (concat "[ \t]*"
  11789. org-keyword-time-not-clock-regexp))))
  11790. ;; Nothing to add, nothing to remove...... :-)
  11791. (throw 'exit nil))
  11792. (if (and (not (looking-at org-outline-regexp))
  11793. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11794. "[^\r\n]*"))
  11795. (not (equal (match-string 1) org-clock-string)))
  11796. (narrow-to-region (match-beginning 0) (match-end 0))
  11797. (insert-before-markers "\n")
  11798. (backward-char 1)
  11799. (narrow-to-region (point) (point))
  11800. (and org-adapt-indentation (org-indent-to-column col)))
  11801. ;; Check if we have to remove something.
  11802. (setq list (cons what remove))
  11803. (while list
  11804. (setq elt (pop list))
  11805. (when (or (and (eq elt 'scheduled)
  11806. (re-search-forward org-scheduled-time-regexp nil t))
  11807. (and (eq elt 'deadline)
  11808. (re-search-forward org-deadline-time-regexp nil t))
  11809. (and (eq elt 'closed)
  11810. (re-search-forward org-closed-time-regexp nil t)))
  11811. (replace-match "")
  11812. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11813. (and (looking-at "[ \t]+") (replace-match ""))
  11814. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11815. (when what
  11816. (insert
  11817. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11818. (cond ((eq what 'scheduled) org-scheduled-string)
  11819. ((eq what 'deadline) org-deadline-string)
  11820. ((eq what 'closed) org-closed-string))
  11821. " ")
  11822. (setq ts (org-insert-time-stamp
  11823. time
  11824. (or org-time-was-given
  11825. (and (eq what 'closed) org-log-done-with-time))
  11826. (eq what 'closed)
  11827. nil nil (list org-end-time-was-given)))
  11828. (insert
  11829. (if (not (or (bolp) (eq (char-before) ?\ )
  11830. (memq (char-after) '(32 10))
  11831. (eobp))) " " ""))
  11832. (end-of-line 1))
  11833. (goto-char (point-min))
  11834. (widen)
  11835. (if (and (looking-at "[ \t]*\n")
  11836. (equal (char-before) ?\n))
  11837. (delete-region (1- (point)) (point-at-eol)))
  11838. ts))))))
  11839. (defvar org-log-note-marker (make-marker))
  11840. (defvar org-log-note-purpose nil)
  11841. (defvar org-log-note-state nil)
  11842. (defvar org-log-note-previous-state nil)
  11843. (defvar org-log-note-how nil)
  11844. (defvar org-log-note-extra nil)
  11845. (defvar org-log-note-window-configuration nil)
  11846. (defvar org-log-note-return-to (make-marker))
  11847. (defvar org-log-note-effective-time nil
  11848. "Remembered current time so that dynamically scoped
  11849. `org-extend-today-until' affects tha timestamps in state change
  11850. log")
  11851. (defvar org-log-post-message nil
  11852. "Message to be displayed after a log note has been stored.
  11853. The auto-repeater uses this.")
  11854. (defun org-add-note ()
  11855. "Add a note to the current entry.
  11856. This is done in the same way as adding a state change note."
  11857. (interactive)
  11858. (org-add-log-setup 'note nil nil 'findpos nil))
  11859. (defvar org-property-end-re)
  11860. (defun org-add-log-setup (&optional purpose state prev-state
  11861. findpos how extra)
  11862. "Set up the post command hook to take a note.
  11863. If this is about to TODO state change, the new state is expected in STATE.
  11864. When FINDPOS is non-nil, find the correct position for the note in
  11865. the current entry. If not, assume that it can be inserted at point.
  11866. HOW is an indicator what kind of note should be created.
  11867. EXTRA is additional text that will be inserted into the notes buffer."
  11868. (let* ((org-log-into-drawer (org-log-into-drawer))
  11869. (drawer (cond ((stringp org-log-into-drawer)
  11870. org-log-into-drawer)
  11871. (org-log-into-drawer "LOGBOOK"))))
  11872. (save-restriction
  11873. (save-excursion
  11874. (when findpos
  11875. (org-back-to-heading t)
  11876. (narrow-to-region (point) (save-excursion
  11877. (outline-next-heading) (point)))
  11878. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11879. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11880. "[^\r\n]*\\)?"))
  11881. (goto-char (match-end 0))
  11882. (cond
  11883. (drawer
  11884. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11885. nil t)
  11886. (progn
  11887. (goto-char (match-end 0))
  11888. (or org-log-states-order-reversed
  11889. (and (re-search-forward org-property-end-re nil t)
  11890. (goto-char (1- (match-beginning 0))))))
  11891. (insert "\n:" drawer ":\n:END:")
  11892. (beginning-of-line 0)
  11893. (org-indent-line)
  11894. (beginning-of-line 2)
  11895. (org-indent-line)
  11896. (end-of-line 0)))
  11897. ((and org-log-state-notes-insert-after-drawers
  11898. (save-excursion
  11899. (forward-line) (looking-at org-drawer-regexp)))
  11900. (forward-line)
  11901. (while (looking-at org-drawer-regexp)
  11902. (goto-char (match-end 0))
  11903. (re-search-forward org-property-end-re (point-max) t)
  11904. (forward-line))
  11905. (forward-line -1)))
  11906. (unless org-log-states-order-reversed
  11907. (and (= (char-after) ?\n) (forward-char 1))
  11908. (org-skip-over-state-notes)
  11909. (skip-chars-backward " \t\n\r")))
  11910. (move-marker org-log-note-marker (point))
  11911. (setq org-log-note-purpose purpose
  11912. org-log-note-state state
  11913. org-log-note-previous-state prev-state
  11914. org-log-note-how how
  11915. org-log-note-extra extra
  11916. org-log-note-effective-time (org-current-effective-time))
  11917. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11918. (defun org-skip-over-state-notes ()
  11919. "Skip past the list of State notes in an entry."
  11920. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11921. (when (ignore-errors (goto-char (org-in-item-p)))
  11922. (let* ((struct (org-list-struct))
  11923. (prevs (org-list-prevs-alist struct)))
  11924. (while (looking-at "[ \t]*- State")
  11925. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11926. (org-list-get-item-end (point) struct)))))))
  11927. (defun org-add-log-note (&optional purpose)
  11928. "Pop up a window for taking a note, and add this note later at point."
  11929. (remove-hook 'post-command-hook 'org-add-log-note)
  11930. (setq org-log-note-window-configuration (current-window-configuration))
  11931. (delete-other-windows)
  11932. (move-marker org-log-note-return-to (point))
  11933. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11934. (goto-char org-log-note-marker)
  11935. (org-switch-to-buffer-other-window "*Org Note*")
  11936. (erase-buffer)
  11937. (if (memq org-log-note-how '(time state))
  11938. (let (current-prefix-arg) (org-store-log-note))
  11939. (let ((org-inhibit-startup t)) (org-mode))
  11940. (insert (format "# Insert note for %s.
  11941. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11942. (cond
  11943. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11944. ((eq org-log-note-purpose 'done) "closed todo item")
  11945. ((eq org-log-note-purpose 'state)
  11946. (format "state change from \"%s\" to \"%s\""
  11947. (or org-log-note-previous-state "")
  11948. (or org-log-note-state "")))
  11949. ((eq org-log-note-purpose 'reschedule)
  11950. "rescheduling")
  11951. ((eq org-log-note-purpose 'delschedule)
  11952. "no longer scheduled")
  11953. ((eq org-log-note-purpose 'redeadline)
  11954. "changing deadline")
  11955. ((eq org-log-note-purpose 'deldeadline)
  11956. "removing deadline")
  11957. ((eq org-log-note-purpose 'refile)
  11958. "refiling")
  11959. ((eq org-log-note-purpose 'note)
  11960. "this entry")
  11961. (t (error "This should not happen")))))
  11962. (if org-log-note-extra (insert org-log-note-extra))
  11963. (org-set-local 'org-finish-function 'org-store-log-note)
  11964. (run-hooks 'org-log-buffer-setup-hook)))
  11965. (defvar org-note-abort nil) ; dynamically scoped
  11966. (defun org-store-log-note ()
  11967. "Finish taking a log note, and insert it to where it belongs."
  11968. (let ((txt (buffer-string)))
  11969. (kill-buffer (current-buffer))
  11970. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11971. lines ind bul)
  11972. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11973. (setq txt (replace-match "" t t txt)))
  11974. (if (string-match "\\s-+\\'" txt)
  11975. (setq txt (replace-match "" t t txt)))
  11976. (setq lines (org-split-string txt "\n"))
  11977. (when (and note (string-match "\\S-" note))
  11978. (setq note
  11979. (org-replace-escapes
  11980. note
  11981. (list (cons "%u" (user-login-name))
  11982. (cons "%U" user-full-name)
  11983. (cons "%t" (format-time-string
  11984. (org-time-stamp-format 'long 'inactive)
  11985. org-log-note-effective-time))
  11986. (cons "%T" (format-time-string
  11987. (org-time-stamp-format 'long nil)
  11988. org-log-note-effective-time))
  11989. (cons "%d" (format-time-string
  11990. (org-time-stamp-format nil 'inactive)
  11991. org-log-note-effective-time))
  11992. (cons "%D" (format-time-string
  11993. (org-time-stamp-format nil nil)
  11994. org-log-note-effective-time))
  11995. (cons "%s" (if org-log-note-state
  11996. (concat "\"" org-log-note-state "\"")
  11997. ""))
  11998. (cons "%S" (if org-log-note-previous-state
  11999. (concat "\"" org-log-note-previous-state "\"")
  12000. "\"\"")))))
  12001. (if lines (setq note (concat note " \\\\")))
  12002. (push note lines))
  12003. (when (or current-prefix-arg org-note-abort)
  12004. (when org-log-into-drawer
  12005. (org-remove-empty-drawer-at
  12006. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  12007. org-log-note-marker))
  12008. (setq lines nil))
  12009. (when lines
  12010. (with-current-buffer (marker-buffer org-log-note-marker)
  12011. (save-excursion
  12012. (goto-char org-log-note-marker)
  12013. (move-marker org-log-note-marker nil)
  12014. (end-of-line 1)
  12015. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12016. (setq ind (save-excursion
  12017. (if (ignore-errors (goto-char (org-in-item-p)))
  12018. (let ((struct (org-list-struct)))
  12019. (org-list-get-ind
  12020. (org-list-get-top-point struct) struct))
  12021. (skip-chars-backward " \r\t\n")
  12022. (cond
  12023. ((and (org-at-heading-p)
  12024. org-adapt-indentation)
  12025. (1+ (org-current-level)))
  12026. ((org-at-heading-p) 0)
  12027. (t (org-get-indentation))))))
  12028. (setq bul (org-list-bullet-string "-"))
  12029. (org-indent-line-to ind)
  12030. (insert bul (pop lines))
  12031. (let ((ind-body (+ (length bul) ind)))
  12032. (while lines
  12033. (insert "\n")
  12034. (org-indent-line-to ind-body)
  12035. (insert (pop lines))))
  12036. (message "Note stored")
  12037. (org-back-to-heading t)
  12038. (org-cycle-hide-drawers 'children))))))
  12039. (set-window-configuration org-log-note-window-configuration)
  12040. (with-current-buffer (marker-buffer org-log-note-return-to)
  12041. (goto-char org-log-note-return-to))
  12042. (move-marker org-log-note-return-to nil)
  12043. (and org-log-post-message (message "%s" org-log-post-message)))
  12044. (defun org-remove-empty-drawer-at (drawer pos)
  12045. "Remove an empty drawer DRAWER at position POS.
  12046. POS may also be a marker."
  12047. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12048. (save-excursion
  12049. (save-restriction
  12050. (widen)
  12051. (goto-char pos)
  12052. (if (org-in-regexp
  12053. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12054. (replace-match ""))))))
  12055. (defvar org-ts-type nil)
  12056. (defun org-sparse-tree (&optional arg type)
  12057. "Create a sparse tree, prompt for the details.
  12058. This command can create sparse trees. You first need to select the type
  12059. of match used to create the tree:
  12060. t Show all TODO entries.
  12061. T Show entries with a specific TODO keyword.
  12062. m Show entries selected by a tags/property match.
  12063. p Enter a property name and its value (both with completion on existing
  12064. names/values) and show entries with that property.
  12065. r Show entries matching a regular expression (`/' can be used as well).
  12066. b Show deadlines and scheduled items before a date.
  12067. a Show deadlines and scheduled items after a date.
  12068. d Show deadlines due within `org-deadline-warning-days'.
  12069. D Show deadlines and scheduled items between a date range."
  12070. (interactive "P")
  12071. (let (ans kwd value ts-type)
  12072. (setq type (or type org-sparse-tree-default-date-type))
  12073. (setq org-ts-type type)
  12074. (message "Sparse tree: [/]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"
  12075. (cond ((eq type 'all) "all timestamps")
  12076. ((eq type 'scheduled) "only scheduled")
  12077. ((eq type 'deadline) "only deadline")
  12078. ((eq type 'active) "only active timestamps")
  12079. ((eq type 'inactive) "only inactive timestamps")
  12080. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12081. (t "scheduled/deadline")))
  12082. (setq ans (read-char-exclusive))
  12083. (cond
  12084. ((equal ans ?c)
  12085. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  12086. ((equal ans ?d)
  12087. (call-interactively 'org-check-deadlines))
  12088. ((equal ans ?b)
  12089. (call-interactively 'org-check-before-date))
  12090. ((equal ans ?a)
  12091. (call-interactively 'org-check-after-date))
  12092. ((equal ans ?D)
  12093. (call-interactively 'org-check-dates-range))
  12094. ((equal ans ?t)
  12095. (call-interactively 'org-show-todo-tree))
  12096. ((equal ans ?T)
  12097. (org-show-todo-tree '(4)))
  12098. ((member ans '(?T ?m))
  12099. (call-interactively 'org-match-sparse-tree))
  12100. ((member ans '(?p ?P))
  12101. (setq kwd (org-icompleting-read "Property: "
  12102. (mapcar 'list (org-buffer-property-keys))))
  12103. (setq value (org-icompleting-read "Value: "
  12104. (mapcar 'list (org-property-values kwd))))
  12105. (unless (string-match "\\`{.*}\\'" value)
  12106. (setq value (concat "\"" value "\"")))
  12107. (org-match-sparse-tree arg (concat kwd "=" value)))
  12108. ((member ans '(?r ?R ?/))
  12109. (call-interactively 'org-occur))
  12110. (t (error "No such sparse tree command \"%c\"" ans)))))
  12111. (defvar org-occur-highlights nil
  12112. "List of overlays used for occur matches.")
  12113. (make-variable-buffer-local 'org-occur-highlights)
  12114. (defvar org-occur-parameters nil
  12115. "Parameters of the active org-occur calls.
  12116. This is a list, each call to org-occur pushes as cons cell,
  12117. containing the regular expression and the callback, onto the list.
  12118. The list can contain several entries if `org-occur' has been called
  12119. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12120. will only contain one set of parameters. When the highlights are
  12121. removed (for example with `C-c C-c', or with the next edit (depending
  12122. on `org-remove-highlights-with-change'), this variable is emptied
  12123. as well.")
  12124. (make-variable-buffer-local 'org-occur-parameters)
  12125. (defun org-occur (regexp &optional keep-previous callback)
  12126. "Make a compact tree which shows all matches of REGEXP.
  12127. The tree will show the lines where the regexp matches, and all higher
  12128. headlines above the match. It will also show the heading after the match,
  12129. to make sure editing the matching entry is easy.
  12130. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12131. call to `org-occur' will be kept, to allow stacking of calls to this
  12132. command.
  12133. If CALLBACK is non-nil, it is a function which is called to confirm
  12134. that the match should indeed be shown."
  12135. (interactive "sRegexp: \nP")
  12136. (when (equal regexp "")
  12137. (error "Regexp cannot be empty"))
  12138. (unless keep-previous
  12139. (org-remove-occur-highlights nil nil t))
  12140. (push (cons regexp callback) org-occur-parameters)
  12141. (let ((cnt 0))
  12142. (save-excursion
  12143. (goto-char (point-min))
  12144. (if (or (not keep-previous) ; do not want to keep
  12145. (not org-occur-highlights)) ; no previous matches
  12146. ;; hide everything
  12147. (org-overview))
  12148. (while (re-search-forward regexp nil t)
  12149. (when (or (not callback)
  12150. (save-match-data (funcall callback)))
  12151. (setq cnt (1+ cnt))
  12152. (when org-highlight-sparse-tree-matches
  12153. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12154. (org-show-context 'occur-tree))))
  12155. (when org-remove-highlights-with-change
  12156. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12157. nil 'local))
  12158. (unless org-sparse-tree-open-archived-trees
  12159. (org-hide-archived-subtrees (point-min) (point-max)))
  12160. (run-hooks 'org-occur-hook)
  12161. (if (org-called-interactively-p 'interactive)
  12162. (message "%d match(es) for regexp %s" cnt regexp))
  12163. cnt))
  12164. (defun org-occur-next-match (&optional n reset)
  12165. "Function for `next-error-function' to find sparse tree matches.
  12166. N is the number of matches to move, when negative move backwards.
  12167. RESET is entirely ignored - this function always goes back to the
  12168. starting point when no match is found."
  12169. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12170. (search-func (if (< n 0)
  12171. 'previous-single-char-property-change
  12172. 'next-single-char-property-change))
  12173. (n (abs n))
  12174. (pos (point))
  12175. p1)
  12176. (catch 'exit
  12177. (while (setq p1 (funcall search-func (point) 'org-type))
  12178. (when (equal p1 limit)
  12179. (goto-char pos)
  12180. (error "No more matches"))
  12181. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12182. (setq n (1- n))
  12183. (when (= n 0)
  12184. (goto-char p1)
  12185. (throw 'exit (point))))
  12186. (goto-char p1))
  12187. (goto-char p1)
  12188. (error "No more matches"))))
  12189. (defun org-show-context (&optional key)
  12190. "Make sure point and context are visible.
  12191. How much context is shown depends upon the variables
  12192. `org-show-hierarchy-above', `org-show-following-heading',
  12193. `org-show-entry-below' and `org-show-siblings'."
  12194. (let ((heading-p (org-at-heading-p t))
  12195. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12196. (following-p (org-get-alist-option org-show-following-heading key))
  12197. (entry-p (org-get-alist-option org-show-entry-below key))
  12198. (siblings-p (org-get-alist-option org-show-siblings key)))
  12199. ;; Show heading or entry text
  12200. (if (and heading-p (not entry-p))
  12201. (org-flag-heading nil) ; only show the heading
  12202. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12203. (org-show-hidden-entry))) ; show entire entry
  12204. (when following-p
  12205. ;; Show next sibling, or heading below text
  12206. (save-excursion
  12207. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12208. (org-flag-heading nil))))
  12209. (when siblings-p (org-show-siblings))
  12210. (when hierarchy-p
  12211. ;; show all higher headings, possibly with siblings
  12212. (save-excursion
  12213. (while (and (condition-case nil
  12214. (progn (org-up-heading-all 1) t)
  12215. (error nil))
  12216. (not (bobp)))
  12217. (org-flag-heading nil)
  12218. (when siblings-p (org-show-siblings)))))
  12219. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12220. (defvar org-reveal-start-hook nil
  12221. "Hook run before revealing a location.")
  12222. (defun org-reveal (&optional siblings)
  12223. "Show current entry, hierarchy above it, and the following headline.
  12224. This can be used to show a consistent set of context around locations
  12225. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12226. not t for the search context.
  12227. With optional argument SIBLINGS, on each level of the hierarchy all
  12228. siblings are shown. This repairs the tree structure to what it would
  12229. look like when opened with hierarchical calls to `org-cycle'.
  12230. With double optional argument \\[universal-argument] \\[universal-argument], \
  12231. go to the parent and show the
  12232. entire tree."
  12233. (interactive "P")
  12234. (run-hooks 'org-reveal-start-hook)
  12235. (let ((org-show-hierarchy-above t)
  12236. (org-show-following-heading t)
  12237. (org-show-siblings (if siblings t org-show-siblings)))
  12238. (org-show-context nil))
  12239. (when (equal siblings '(16))
  12240. (save-excursion
  12241. (when (org-up-heading-safe)
  12242. (org-show-subtree)
  12243. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12244. (defun org-highlight-new-match (beg end)
  12245. "Highlight from BEG to END and mark the highlight is an occur headline."
  12246. (let ((ov (make-overlay beg end)))
  12247. (overlay-put ov 'face 'secondary-selection)
  12248. (overlay-put ov 'org-type 'org-occur)
  12249. (push ov org-occur-highlights)))
  12250. (defun org-remove-occur-highlights (&optional beg end noremove)
  12251. "Remove the occur highlights from the buffer.
  12252. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12253. from the `before-change-functions' in the current buffer."
  12254. (interactive)
  12255. (unless org-inhibit-highlight-removal
  12256. (mapc 'delete-overlay org-occur-highlights)
  12257. (setq org-occur-highlights nil)
  12258. (setq org-occur-parameters nil)
  12259. (unless noremove
  12260. (remove-hook 'before-change-functions
  12261. 'org-remove-occur-highlights 'local))))
  12262. ;;;; Priorities
  12263. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12264. "Regular expression matching the priority indicator.")
  12265. (defvar org-remove-priority-next-time nil)
  12266. (defun org-priority-up ()
  12267. "Increase the priority of the current item."
  12268. (interactive)
  12269. (org-priority 'up))
  12270. (defun org-priority-down ()
  12271. "Decrease the priority of the current item."
  12272. (interactive)
  12273. (org-priority 'down))
  12274. (defun org-priority (&optional action show)
  12275. "Change the priority of an item.
  12276. ACTION can be `set', `up', `down', or a character."
  12277. (interactive "P")
  12278. (if (equal action '(4))
  12279. (org-show-priority)
  12280. (unless org-enable-priority-commands
  12281. (error "Priority commands are disabled"))
  12282. (setq action (or action 'set))
  12283. (let (current new news have remove)
  12284. (save-excursion
  12285. (org-back-to-heading t)
  12286. (if (looking-at org-priority-regexp)
  12287. (setq current (string-to-char (match-string 2))
  12288. have t))
  12289. (cond
  12290. ((eq action 'remove)
  12291. (setq remove t new ?\ ))
  12292. ((or (eq action 'set)
  12293. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12294. (if (not (eq action 'set))
  12295. (setq new action)
  12296. (message "Priority %c-%c, SPC to remove: "
  12297. org-highest-priority org-lowest-priority)
  12298. (save-match-data
  12299. (setq new (read-char-exclusive))))
  12300. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12301. (= (upcase org-lowest-priority) org-lowest-priority))
  12302. (setq new (upcase new)))
  12303. (cond ((equal new ?\ ) (setq remove t))
  12304. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12305. (error "Priority must be between `%c' and `%c'"
  12306. org-highest-priority org-lowest-priority))))
  12307. ((eq action 'up)
  12308. (setq new (if have
  12309. (1- current) ; normal cycling
  12310. ;; last priority was empty
  12311. (if (eq last-command this-command)
  12312. org-lowest-priority ; wrap around empty to lowest
  12313. ;; default
  12314. (if org-priority-start-cycle-with-default
  12315. org-default-priority
  12316. (1- org-default-priority))))))
  12317. ((eq action 'down)
  12318. (setq new (if have
  12319. (1+ current) ; normal cycling
  12320. ;; last priority was empty
  12321. (if (eq last-command this-command)
  12322. org-highest-priority ; wrap around empty to highest
  12323. ;; default
  12324. (if org-priority-start-cycle-with-default
  12325. org-default-priority
  12326. (1+ org-default-priority))))))
  12327. (t (error "Invalid action")))
  12328. (if (or (< (upcase new) org-highest-priority)
  12329. (> (upcase new) org-lowest-priority))
  12330. (if (and (memq action '(up down))
  12331. (not have) (not (eq last-command this-command)))
  12332. ;; `new' is from default priority
  12333. (error
  12334. "The default can not be set, see `org-default-priority' why")
  12335. ;; normal cycling: `new' is beyond highest/lowest priority
  12336. ;; and is wrapped around to the empty priority
  12337. (setq remove t)))
  12338. (setq news (format "%c" new))
  12339. (if have
  12340. (if remove
  12341. (replace-match "" t t nil 1)
  12342. (replace-match news t t nil 2))
  12343. (if remove
  12344. (error "No priority cookie found in line")
  12345. (let ((case-fold-search nil))
  12346. (looking-at org-todo-line-regexp))
  12347. (if (match-end 2)
  12348. (progn
  12349. (goto-char (match-end 2))
  12350. (insert " [#" news "]"))
  12351. (goto-char (match-beginning 3))
  12352. (insert "[#" news "] "))))
  12353. (org-preserve-lc (org-set-tags nil 'align)))
  12354. (if remove
  12355. (message "Priority removed")
  12356. (message "Priority of current item set to %s" news)))))
  12357. (defun org-show-priority ()
  12358. "Show the priority of the current item.
  12359. This priority is composed of the main priority given with the [#A] cookies,
  12360. and by additional input from the age of a schedules or deadline entry."
  12361. (interactive)
  12362. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12363. (org-get-at-bol 'priority)
  12364. (save-excursion
  12365. (save-match-data
  12366. (beginning-of-line)
  12367. (and (looking-at org-heading-regexp)
  12368. (org-get-priority (match-string 0))))))))
  12369. (message "Priority is %d" (if pri pri -1000))))
  12370. (defun org-get-priority (s)
  12371. "Find priority cookie and return priority."
  12372. (save-match-data
  12373. (if (functionp org-get-priority-function)
  12374. (funcall org-get-priority-function)
  12375. (if (not (string-match org-priority-regexp s))
  12376. (* 1000 (- org-lowest-priority org-default-priority))
  12377. (* 1000 (- org-lowest-priority
  12378. (string-to-char (match-string 2 s))))))))
  12379. ;;;; Tags
  12380. (defvar org-agenda-archives-mode)
  12381. (defvar org-map-continue-from nil
  12382. "Position from where mapping should continue.
  12383. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12384. (defvar org-scanner-tags nil
  12385. "The current tag list while the tags scanner is running.")
  12386. (defvar org-trust-scanner-tags nil
  12387. "Should `org-get-tags-at' use the tags for the scanner.
  12388. This is for internal dynamical scoping only.
  12389. When this is non-nil, the function `org-get-tags-at' will return the value
  12390. of `org-scanner-tags' instead of building the list by itself. This
  12391. can lead to large speed-ups when the tags scanner is used in a file with
  12392. many entries, and when the list of tags is retrieved, for example to
  12393. obtain a list of properties. Building the tags list for each entry in such
  12394. a file becomes an N^2 operation - but with this variable set, it scales
  12395. as N.")
  12396. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12397. "Sca headline tags with inheritance and produce output ACTION.
  12398. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12399. or `agenda' to produce an entry list for an agenda view. It can also be
  12400. a Lisp form or a function that should be called at each matched headline, in
  12401. this case the return value is a list of all return values from these calls.
  12402. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12403. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12404. only lines with a not-done TODO keyword are included in the output.
  12405. This should be the same variable that was scoped into
  12406. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12407. START-LEVEL can be a string with asterisks, reducing the scope to
  12408. headlines matching this string."
  12409. (require 'org-agenda)
  12410. (let* ((re (concat "^"
  12411. (if start-level
  12412. ;; Get the correct level to match
  12413. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12414. org-outline-regexp)
  12415. " *\\(\\<\\("
  12416. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12417. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12418. (props (list 'face 'default
  12419. 'done-face 'org-agenda-done
  12420. 'undone-face 'default
  12421. 'mouse-face 'highlight
  12422. 'org-not-done-regexp org-not-done-regexp
  12423. 'org-todo-regexp org-todo-regexp
  12424. 'org-complex-heading-regexp org-complex-heading-regexp
  12425. 'help-echo
  12426. (format "mouse-2 or RET jump to org file %s"
  12427. (abbreviate-file-name
  12428. (or (buffer-file-name (buffer-base-buffer))
  12429. (buffer-name (buffer-base-buffer)))))))
  12430. (case-fold-search nil)
  12431. (org-map-continue-from nil)
  12432. lspos tags tags-list
  12433. (tags-alist (list (cons 0 org-file-tags)))
  12434. (llast 0) rtn rtn1 level category i txt
  12435. todo marker entry priority)
  12436. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12437. (setq action (list 'lambda nil action)))
  12438. (save-excursion
  12439. (goto-char (point-min))
  12440. (when (eq action 'sparse-tree)
  12441. (org-overview)
  12442. (org-remove-occur-highlights))
  12443. (while (re-search-forward re nil t)
  12444. (setq org-map-continue-from nil)
  12445. (catch :skip
  12446. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12447. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12448. (goto-char (setq lspos (match-beginning 0)))
  12449. (setq level (org-reduced-level (org-outline-level))
  12450. category (org-get-category))
  12451. (setq i llast llast level)
  12452. ;; remove tag lists from same and sublevels
  12453. (while (>= i level)
  12454. (when (setq entry (assoc i tags-alist))
  12455. (setq tags-alist (delete entry tags-alist)))
  12456. (setq i (1- i)))
  12457. ;; add the next tags
  12458. (when tags
  12459. (setq tags (org-split-string tags ":")
  12460. tags-alist
  12461. (cons (cons level tags) tags-alist)))
  12462. ;; compile tags for current headline
  12463. (setq tags-list
  12464. (if org-use-tag-inheritance
  12465. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12466. tags)
  12467. org-scanner-tags tags-list)
  12468. (when org-use-tag-inheritance
  12469. (setcdr (car tags-alist)
  12470. (mapcar (lambda (x)
  12471. (setq x (copy-sequence x))
  12472. (org-add-prop-inherited x))
  12473. (cdar tags-alist))))
  12474. (when (and tags org-use-tag-inheritance
  12475. (or (not (eq t org-use-tag-inheritance))
  12476. org-tags-exclude-from-inheritance))
  12477. ;; selective inheritance, remove uninherited ones
  12478. (setcdr (car tags-alist)
  12479. (org-remove-uninherited-tags (cdar tags-alist))))
  12480. (when (and
  12481. ;; eval matcher only when the todo condition is OK
  12482. (and (or (not todo-only) (member todo org-not-done-keywords))
  12483. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12484. (eval matcher)))
  12485. ;; Call the skipper, but return t if it does not skip,
  12486. ;; so that the `and' form continues evaluating
  12487. (progn
  12488. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12489. t)
  12490. ;; Check if timestamps are deselecting this entry
  12491. (or (not todo-only)
  12492. (and (member todo org-not-done-keywords)
  12493. (or (not org-agenda-tags-todo-honor-ignore-options)
  12494. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12495. ;; select this headline
  12496. (cond
  12497. ((eq action 'sparse-tree)
  12498. (and org-highlight-sparse-tree-matches
  12499. (org-get-heading) (match-end 0)
  12500. (org-highlight-new-match
  12501. (match-beginning 1) (match-end 1)))
  12502. (org-show-context 'tags-tree))
  12503. ((eq action 'agenda)
  12504. (setq txt (org-agenda-format-item
  12505. ""
  12506. (concat
  12507. (if (eq org-tags-match-list-sublevels 'indented)
  12508. (make-string (1- level) ?.) "")
  12509. (org-get-heading))
  12510. level category
  12511. tags-list)
  12512. priority (org-get-priority txt))
  12513. (goto-char lspos)
  12514. (setq marker (org-agenda-new-marker))
  12515. (org-add-props txt props
  12516. 'org-marker marker 'org-hd-marker marker 'org-category category
  12517. 'todo-state todo
  12518. 'priority priority 'type "tagsmatch")
  12519. (push txt rtn))
  12520. ((functionp action)
  12521. (setq org-map-continue-from nil)
  12522. (save-excursion
  12523. (setq rtn1 (funcall action))
  12524. (push rtn1 rtn)))
  12525. (t (error "Invalid action")))
  12526. ;; if we are to skip sublevels, jump to end of subtree
  12527. (unless org-tags-match-list-sublevels
  12528. (org-end-of-subtree t)
  12529. (backward-char 1))))
  12530. ;; Get the correct position from where to continue
  12531. (if org-map-continue-from
  12532. (goto-char org-map-continue-from)
  12533. (and (= (point) lspos) (end-of-line 1)))))
  12534. (when (and (eq action 'sparse-tree)
  12535. (not org-sparse-tree-open-archived-trees))
  12536. (org-hide-archived-subtrees (point-min) (point-max)))
  12537. (nreverse rtn)))
  12538. (defun org-remove-uninherited-tags (tags)
  12539. "Remove all tags that are not inherited from the list TAGS."
  12540. (cond
  12541. ((eq org-use-tag-inheritance t)
  12542. (if org-tags-exclude-from-inheritance
  12543. (org-delete-all org-tags-exclude-from-inheritance tags)
  12544. tags))
  12545. ((not org-use-tag-inheritance) nil)
  12546. ((stringp org-use-tag-inheritance)
  12547. (delq nil (mapcar
  12548. (lambda (x)
  12549. (if (and (string-match org-use-tag-inheritance x)
  12550. (not (member x org-tags-exclude-from-inheritance)))
  12551. x nil))
  12552. tags)))
  12553. ((listp org-use-tag-inheritance)
  12554. (delq nil (mapcar
  12555. (lambda (x)
  12556. (if (member x org-use-tag-inheritance) x nil))
  12557. tags)))))
  12558. (defun org-match-sparse-tree (&optional todo-only match)
  12559. "Create a sparse tree according to tags string MATCH.
  12560. MATCH can contain positive and negative selection of tags, like
  12561. \"+WORK+URGENT-WITHBOSS\".
  12562. If optional argument TODO-ONLY is non-nil, only select lines that are
  12563. also TODO lines."
  12564. (interactive "P")
  12565. (org-agenda-prepare-buffers (list (current-buffer)))
  12566. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12567. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12568. (defvar org-cached-props nil)
  12569. (defun org-cached-entry-get (pom property)
  12570. (if (or (eq t org-use-property-inheritance)
  12571. (and (stringp org-use-property-inheritance)
  12572. (string-match org-use-property-inheritance property))
  12573. (and (listp org-use-property-inheritance)
  12574. (member property org-use-property-inheritance)))
  12575. ;; Caching is not possible, check it directly
  12576. (org-entry-get pom property 'inherit)
  12577. ;; Get all properties, so that we can do complicated checks easily
  12578. (cdr (assoc property (or org-cached-props
  12579. (setq org-cached-props
  12580. (org-entry-properties pom)))))))
  12581. (defun org-global-tags-completion-table (&optional files)
  12582. "Return the list of all tags in all agenda buffer/files.
  12583. Optional FILES argument is a list of files which can be used
  12584. instead of the agenda files."
  12585. (save-excursion
  12586. (org-uniquify
  12587. (delq nil
  12588. (apply 'append
  12589. (mapcar
  12590. (lambda (file)
  12591. (set-buffer (find-file-noselect file))
  12592. (append (org-get-buffer-tags)
  12593. (mapcar (lambda (x) (if (stringp (car-safe x))
  12594. (list (car-safe x)) nil))
  12595. org-tag-alist)))
  12596. (if (and files (car files))
  12597. files
  12598. (org-agenda-files))))))))
  12599. (defun org-make-tags-matcher (match)
  12600. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12601. The variable `todo-only' is scoped dynamically into this function.
  12602. It will be set to t if the matcher restricts matching to TODO entries,
  12603. otherwise will not be touched.
  12604. Returns a cons of the selection string MATCH and the constructed
  12605. lisp form implementing the matcher. The matcher is to be evaluated
  12606. at an Org entry, with point on the headline, and returns t if the
  12607. entry matches the selection string MATCH. The returned lisp form
  12608. references two variables with information about the entry, which
  12609. must be bound around the form's evaluation: todo, the TODO keyword
  12610. at the entry (or nil of none); and tags-list, the list of all tags
  12611. at the entry including inherited ones. Additionally, the category
  12612. of the entry (if any) must be specified as the text property
  12613. 'org-category on the headline.
  12614. See also `org-scan-tags'.
  12615. "
  12616. (declare (special todo-only))
  12617. (unless (boundp 'todo-only)
  12618. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12619. (unless match
  12620. ;; Get a new match request, with completion
  12621. (let ((org-last-tags-completion-table
  12622. (org-global-tags-completion-table)))
  12623. (setq match (org-completing-read-no-i
  12624. "Match: " 'org-tags-completion-function nil nil nil
  12625. 'org-tags-history))))
  12626. ;; Parse the string and create a lisp form
  12627. (let ((match0 match)
  12628. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12629. minus tag mm
  12630. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12631. orterms term orlist re-p str-p level-p level-op time-p
  12632. prop-p pn pv po gv rest)
  12633. (if (string-match "/+" match)
  12634. ;; match contains also a todo-matching request
  12635. (progn
  12636. (setq tagsmatch (substring match 0 (match-beginning 0))
  12637. todomatch (substring match (match-end 0)))
  12638. (if (string-match "^!" todomatch)
  12639. (setq todo-only t todomatch (substring todomatch 1)))
  12640. (if (string-match "^\\s-*$" todomatch)
  12641. (setq todomatch nil)))
  12642. ;; only matching tags
  12643. (setq tagsmatch match todomatch nil))
  12644. ;; Make the tags matcher
  12645. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12646. (setq tagsmatcher t)
  12647. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12648. (while (setq term (pop orterms))
  12649. (while (and (equal (substring term -1) "\\") orterms)
  12650. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12651. (while (string-match re term)
  12652. (setq rest (substring term (match-end 0))
  12653. minus (and (match-end 1)
  12654. (equal (match-string 1 term) "-"))
  12655. tag (save-match-data (replace-regexp-in-string
  12656. "\\\\-" "-"
  12657. (match-string 2 term)))
  12658. re-p (equal (string-to-char tag) ?{)
  12659. level-p (match-end 4)
  12660. prop-p (match-end 5)
  12661. mm (cond
  12662. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12663. (level-p
  12664. (setq level-op (org-op-to-function (match-string 3 term)))
  12665. `(,level-op level ,(string-to-number
  12666. (match-string 4 term))))
  12667. (prop-p
  12668. (setq pn (match-string 5 term)
  12669. po (match-string 6 term)
  12670. pv (match-string 7 term)
  12671. re-p (equal (string-to-char pv) ?{)
  12672. str-p (equal (string-to-char pv) ?\")
  12673. time-p (save-match-data
  12674. (string-match "^\"[[<].*[]>]\"$" pv))
  12675. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12676. (if time-p (setq pv (org-matcher-time pv)))
  12677. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12678. (cond
  12679. ((equal pn "CATEGORY")
  12680. (setq gv '(get-text-property (point) 'org-category)))
  12681. ((equal pn "TODO")
  12682. (setq gv 'todo))
  12683. (t
  12684. (setq gv `(org-cached-entry-get nil ,pn))))
  12685. (if re-p
  12686. (if (eq po 'org<>)
  12687. `(not (string-match ,pv (or ,gv "")))
  12688. `(string-match ,pv (or ,gv "")))
  12689. (if str-p
  12690. `(,po (or ,gv "") ,pv)
  12691. `(,po (string-to-number (or ,gv ""))
  12692. ,(string-to-number pv) ))))
  12693. (t `(member ,tag tags-list)))
  12694. mm (if minus (list 'not mm) mm)
  12695. term rest)
  12696. (push mm tagsmatcher))
  12697. (push (if (> (length tagsmatcher) 1)
  12698. (cons 'and tagsmatcher)
  12699. (car tagsmatcher))
  12700. orlist)
  12701. (setq tagsmatcher nil))
  12702. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12703. (setq tagsmatcher
  12704. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12705. ;; Make the todo matcher
  12706. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12707. (setq todomatcher t)
  12708. (setq orterms (org-split-string todomatch "|") orlist nil)
  12709. (while (setq term (pop orterms))
  12710. (while (string-match re term)
  12711. (setq minus (and (match-end 1)
  12712. (equal (match-string 1 term) "-"))
  12713. kwd (match-string 2 term)
  12714. re-p (equal (string-to-char kwd) ?{)
  12715. term (substring term (match-end 0))
  12716. mm (if re-p
  12717. `(string-match ,(substring kwd 1 -1) todo)
  12718. (list 'equal 'todo kwd))
  12719. mm (if minus (list 'not mm) mm))
  12720. (push mm todomatcher))
  12721. (push (if (> (length todomatcher) 1)
  12722. (cons 'and todomatcher)
  12723. (car todomatcher))
  12724. orlist)
  12725. (setq todomatcher nil))
  12726. (setq todomatcher (if (> (length orlist) 1)
  12727. (cons 'or orlist) (car orlist))))
  12728. ;; Return the string and lisp forms of the matcher
  12729. (setq matcher (if todomatcher
  12730. (list 'and tagsmatcher todomatcher)
  12731. tagsmatcher))
  12732. (when todo-only
  12733. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12734. matcher)))
  12735. (cons match0 matcher)))
  12736. (defun org-op-to-function (op &optional stringp)
  12737. "Turn an operator into the appropriate function."
  12738. (setq op
  12739. (cond
  12740. ((equal op "<" ) '(< string< org-time<))
  12741. ((equal op ">" ) '(> org-string> org-time>))
  12742. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12743. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12744. ((member op '("=" "==")) '(= string= org-time=))
  12745. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12746. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12747. (defun org<> (a b) (not (= a b)))
  12748. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12749. (defun org-string>= (a b) (not (string< a b)))
  12750. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12751. (defun org-string<> (a b) (not (string= a b)))
  12752. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12753. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12754. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12755. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12756. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12757. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12758. (defun org-2ft (s)
  12759. "Convert S to a floating point time.
  12760. If S is already a number, just return it. If it is a string, parse
  12761. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12762. (cond
  12763. ((numberp s) s)
  12764. ((stringp s)
  12765. (condition-case nil
  12766. (float-time (apply 'encode-time (org-parse-time-string s)))
  12767. (error 0.)))
  12768. (t 0.)))
  12769. (defun org-time-today ()
  12770. "Time in seconds today at 0:00.
  12771. Returns the float number of seconds since the beginning of the
  12772. epoch to the beginning of today (00:00)."
  12773. (float-time (apply 'encode-time
  12774. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12775. (defun org-matcher-time (s)
  12776. "Interpret a time comparison value."
  12777. (save-match-data
  12778. (cond
  12779. ((string= s "<now>") (float-time))
  12780. ((string= s "<today>") (org-time-today))
  12781. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12782. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12783. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12784. (+ (org-time-today)
  12785. (* (string-to-number (match-string 1 s))
  12786. (cdr (assoc (match-string 2 s)
  12787. '(("d" . 86400.0) ("w" . 604800.0)
  12788. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12789. (t (org-2ft s)))))
  12790. (defun org-match-any-p (re list)
  12791. "Does re match any element of list?"
  12792. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12793. (delq nil list))
  12794. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12795. (defvar org-tags-overlay (make-overlay 1 1))
  12796. (org-detach-overlay org-tags-overlay)
  12797. (defun org-get-local-tags-at (&optional pos)
  12798. "Get a list of tags defined in the current headline."
  12799. (org-get-tags-at pos 'local))
  12800. (defun org-get-local-tags ()
  12801. "Get a list of tags defined in the current headline."
  12802. (org-get-tags-at nil 'local))
  12803. (defun org-get-tags-at (&optional pos local)
  12804. "Get a list of all headline tags applicable at POS.
  12805. POS defaults to point. If tags are inherited, the list contains
  12806. the targets in the same sequence as the headlines appear, i.e.
  12807. the tags of the current headline come last.
  12808. When LOCAL is non-nil, only return tags from the current headline,
  12809. ignore inherited ones."
  12810. (interactive)
  12811. (if (and org-trust-scanner-tags
  12812. (or (not pos) (equal pos (point)))
  12813. (not local))
  12814. org-scanner-tags
  12815. (let (tags ltags lastpos parent)
  12816. (save-excursion
  12817. (save-restriction
  12818. (widen)
  12819. (goto-char (or pos (point)))
  12820. (save-match-data
  12821. (catch 'done
  12822. (condition-case nil
  12823. (progn
  12824. (org-back-to-heading t)
  12825. (while (not (equal lastpos (point)))
  12826. (setq lastpos (point))
  12827. (when (looking-at
  12828. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12829. (setq ltags (org-split-string
  12830. (org-match-string-no-properties 1) ":"))
  12831. (when parent
  12832. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12833. (setq tags (append
  12834. (if parent
  12835. (org-remove-uninherited-tags ltags)
  12836. ltags)
  12837. tags)))
  12838. (or org-use-tag-inheritance (throw 'done t))
  12839. (if local (throw 'done t))
  12840. (or (org-up-heading-safe) (error nil))
  12841. (setq parent t)))
  12842. (error nil)))))
  12843. (if local
  12844. tags
  12845. (reverse (delete-dups
  12846. (reverse (append
  12847. (org-remove-uninherited-tags
  12848. org-file-tags) tags)))))))))
  12849. (defun org-add-prop-inherited (s)
  12850. (add-text-properties 0 (length s) '(inherited t) s)
  12851. s)
  12852. (defun org-toggle-tag (tag &optional onoff)
  12853. "Toggle the tag TAG for the current line.
  12854. If ONOFF is `on' or `off', don't toggle but set to this state."
  12855. (let (res current)
  12856. (save-excursion
  12857. (org-back-to-heading t)
  12858. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12859. (point-at-eol) t)
  12860. (progn
  12861. (setq current (match-string 1))
  12862. (replace-match ""))
  12863. (setq current ""))
  12864. (setq current (nreverse (org-split-string current ":")))
  12865. (cond
  12866. ((eq onoff 'on)
  12867. (setq res t)
  12868. (or (member tag current) (push tag current)))
  12869. ((eq onoff 'off)
  12870. (or (not (member tag current)) (setq current (delete tag current))))
  12871. (t (if (member tag current)
  12872. (setq current (delete tag current))
  12873. (setq res t)
  12874. (push tag current))))
  12875. (end-of-line 1)
  12876. (if current
  12877. (progn
  12878. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12879. (org-set-tags nil t))
  12880. (delete-horizontal-space))
  12881. (run-hooks 'org-after-tags-change-hook))
  12882. res))
  12883. (defun org-align-tags-here (to-col)
  12884. ;; Assumes that this is a headline
  12885. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12886. (beginning-of-line 1)
  12887. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12888. (< pos (match-beginning 2)))
  12889. (progn
  12890. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12891. (goto-char (match-beginning 1))
  12892. (insert " ")
  12893. (delete-region (point) (1+ (match-beginning 2)))
  12894. (setq ncol (max (current-column)
  12895. (1+ col)
  12896. (if (> to-col 0)
  12897. to-col
  12898. (- (abs to-col) tags-l))))
  12899. (setq p (point))
  12900. (insert (make-string (- ncol (current-column)) ?\ ))
  12901. (setq ncol (current-column))
  12902. (when indent-tabs-mode (tabify p (point-at-eol)))
  12903. (org-move-to-column (min ncol col) t))
  12904. (goto-char pos))))
  12905. (defun org-set-tags-command (&optional arg just-align)
  12906. "Call the set-tags command for the current entry."
  12907. (interactive "P")
  12908. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12909. (org-set-tags arg just-align)
  12910. (save-excursion
  12911. (unless (and (org-region-active-p)
  12912. org-loop-over-headlines-in-active-region)
  12913. (org-back-to-heading t))
  12914. (org-set-tags arg just-align))))
  12915. (defun org-set-tags-to (data)
  12916. "Set the tags of the current entry to DATA, replacing the current tags.
  12917. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12918. If DATA is nil or the empty string, any tags will be removed."
  12919. (interactive "sTags: ")
  12920. (setq data
  12921. (cond
  12922. ((eq data nil) "")
  12923. ((equal data "") "")
  12924. ((stringp data)
  12925. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12926. ":"))
  12927. ((listp data)
  12928. (concat ":" (mapconcat 'identity data ":") ":"))))
  12929. (when data
  12930. (save-excursion
  12931. (org-back-to-heading t)
  12932. (when (looking-at org-complex-heading-regexp)
  12933. (if (match-end 5)
  12934. (progn
  12935. (goto-char (match-beginning 5))
  12936. (insert data)
  12937. (delete-region (point) (point-at-eol))
  12938. (org-set-tags nil 'align))
  12939. (goto-char (point-at-eol))
  12940. (insert " " data)
  12941. (org-set-tags nil 'align)))
  12942. (beginning-of-line 1)
  12943. (if (looking-at ".*?\\([ \t]+\\)$")
  12944. (delete-region (match-beginning 1) (match-end 1))))))
  12945. (defun org-align-all-tags ()
  12946. "Align the tags i all headings."
  12947. (interactive)
  12948. (save-excursion
  12949. (or (ignore-errors (org-back-to-heading t))
  12950. (outline-next-heading))
  12951. (if (org-at-heading-p)
  12952. (org-set-tags t)
  12953. (message "No headings"))))
  12954. (defvar org-indent-indentation-per-level)
  12955. (defun org-set-tags (&optional arg just-align)
  12956. "Set the tags for the current headline.
  12957. With prefix ARG, realign all tags in headings in the current buffer."
  12958. (interactive "P")
  12959. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12960. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12961. 'region-start-level 'region))
  12962. org-loop-over-headlines-in-active-region)
  12963. (org-map-entries
  12964. ;; We don't use ARG and JUST-ALIGN here these args are not
  12965. ;; useful when looping over headlines
  12966. `(org-set-tags)
  12967. org-loop-over-headlines-in-active-region
  12968. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12969. (let* ((re org-outline-regexp-bol)
  12970. (current (unless arg (org-get-tags-string)))
  12971. (col (current-column))
  12972. (org-setting-tags t)
  12973. table current-tags inherited-tags ; computed below when needed
  12974. tags p0 c0 c1 rpl di tc level)
  12975. (if arg
  12976. (save-excursion
  12977. (goto-char (point-min))
  12978. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12979. (while (re-search-forward re nil t)
  12980. (org-set-tags nil t)
  12981. (end-of-line 1)))
  12982. (message "All tags realigned to column %d" org-tags-column))
  12983. (if just-align
  12984. (setq tags current)
  12985. ;; Get a new set of tags from the user
  12986. (save-excursion
  12987. (setq table (append org-tag-persistent-alist
  12988. (or org-tag-alist (org-get-buffer-tags))
  12989. (and
  12990. org-complete-tags-always-offer-all-agenda-tags
  12991. (org-global-tags-completion-table
  12992. (org-agenda-files))))
  12993. org-last-tags-completion-table table
  12994. current-tags (org-split-string current ":")
  12995. inherited-tags (nreverse
  12996. (nthcdr (length current-tags)
  12997. (nreverse (org-get-tags-at))))
  12998. tags
  12999. (if (or (eq t org-use-fast-tag-selection)
  13000. (and org-use-fast-tag-selection
  13001. (delq nil (mapcar 'cdr table))))
  13002. (org-fast-tag-selection
  13003. current-tags inherited-tags table
  13004. (if org-fast-tag-selection-include-todo
  13005. org-todo-key-alist))
  13006. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13007. (org-trim
  13008. (org-icompleting-read "Tags: "
  13009. 'org-tags-completion-function
  13010. nil nil current 'org-tags-history))))))
  13011. (while (string-match "[-+&]+" tags)
  13012. ;; No boolean logic, just a list
  13013. (setq tags (replace-match ":" t t tags))))
  13014. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13015. (if org-tags-sort-function
  13016. (setq tags (mapconcat 'identity
  13017. (sort (org-split-string
  13018. tags (org-re "[^[:alnum:]_@#%]+"))
  13019. org-tags-sort-function) ":")))
  13020. (if (string-match "\\`[\t ]*\\'" tags)
  13021. (setq tags "")
  13022. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13023. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13024. ;; Insert new tags at the correct column
  13025. (beginning-of-line 1)
  13026. (setq level (or (and (looking-at org-outline-regexp)
  13027. (- (match-end 0) (point) 1))
  13028. 1))
  13029. (cond
  13030. ((and (equal current "") (equal tags "")))
  13031. ((re-search-forward
  13032. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13033. (point-at-eol) t)
  13034. (if (equal tags "")
  13035. (setq rpl "")
  13036. (goto-char (match-beginning 0))
  13037. (setq c0 (current-column)
  13038. ;; compute offset for the case of org-indent-mode active
  13039. di (if org-indent-mode
  13040. (* (1- org-indent-indentation-per-level) (1- level))
  13041. 0)
  13042. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13043. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13044. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13045. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13046. (replace-match rpl t t)
  13047. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13048. tags)
  13049. (t (error "Tags alignment failed")))
  13050. (org-move-to-column col)
  13051. (unless just-align
  13052. (run-hooks 'org-after-tags-change-hook))))))
  13053. (defun org-change-tag-in-region (beg end tag off)
  13054. "Add or remove TAG for each entry in the region.
  13055. This works in the agenda, and also in an org-mode buffer."
  13056. (interactive
  13057. (list (region-beginning) (region-end)
  13058. (let ((org-last-tags-completion-table
  13059. (if (derived-mode-p 'org-mode)
  13060. (org-get-buffer-tags)
  13061. (org-global-tags-completion-table))))
  13062. (org-icompleting-read
  13063. "Tag: " 'org-tags-completion-function nil nil nil
  13064. 'org-tags-history))
  13065. (progn
  13066. (message "[s]et or [r]emove? ")
  13067. (equal (read-char-exclusive) ?r))))
  13068. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13069. (let ((agendap (equal major-mode 'org-agenda-mode))
  13070. l1 l2 m buf pos newhead (cnt 0))
  13071. (goto-char end)
  13072. (setq l2 (1- (org-current-line)))
  13073. (goto-char beg)
  13074. (setq l1 (org-current-line))
  13075. (loop for l from l1 to l2 do
  13076. (org-goto-line l)
  13077. (setq m (get-text-property (point) 'org-hd-marker))
  13078. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13079. (and agendap m))
  13080. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13081. pos (if agendap m (point)))
  13082. (with-current-buffer buf
  13083. (save-excursion
  13084. (save-restriction
  13085. (goto-char pos)
  13086. (setq cnt (1+ cnt))
  13087. (org-toggle-tag tag (if off 'off 'on))
  13088. (setq newhead (org-get-heading)))))
  13089. (and agendap (org-agenda-change-all-lines newhead m))))
  13090. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13091. (defun org-tags-completion-function (string predicate &optional flag)
  13092. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13093. (confirm (lambda (x) (stringp (car x)))))
  13094. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13095. (setq s1 (match-string 1 string)
  13096. s2 (match-string 2 string))
  13097. (setq s1 "" s2 string))
  13098. (cond
  13099. ((eq flag nil)
  13100. ;; try completion
  13101. (setq rtn (try-completion s2 ctable confirm))
  13102. (if (stringp rtn)
  13103. (setq rtn
  13104. (concat s1 s2 (substring rtn (length s2))
  13105. (if (and org-add-colon-after-tag-completion
  13106. (assoc rtn ctable))
  13107. ":" ""))))
  13108. rtn)
  13109. ((eq flag t)
  13110. ;; all-completions
  13111. (all-completions s2 ctable confirm)
  13112. )
  13113. ((eq flag 'lambda)
  13114. ;; exact match?
  13115. (assoc s2 ctable)))
  13116. ))
  13117. (defun org-fast-tag-insert (kwd tags face &optional end)
  13118. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  13119. (insert (format "%-12s" (concat kwd ":"))
  13120. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13121. (or end "")))
  13122. (defun org-fast-tag-show-exit (flag)
  13123. (save-excursion
  13124. (org-goto-line 3)
  13125. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13126. (replace-match ""))
  13127. (when flag
  13128. (end-of-line 1)
  13129. (org-move-to-column (- (window-width) 19) t)
  13130. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13131. (defun org-set-current-tags-overlay (current prefix)
  13132. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13133. (if (featurep 'xemacs)
  13134. (org-overlay-display org-tags-overlay (concat prefix s)
  13135. 'secondary-selection)
  13136. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13137. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13138. (defvar org-last-tag-selection-key nil)
  13139. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13140. "Fast tag selection with single keys.
  13141. CURRENT is the current list of tags in the headline, INHERITED is the
  13142. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13143. possibly with grouping information. TODO-TABLE is a similar table with
  13144. TODO keywords, should these have keys assigned to them.
  13145. If the keys are nil, a-z are automatically assigned.
  13146. Returns the new tags string, or nil to not change the current settings."
  13147. (let* ((fulltable (append table todo-table))
  13148. (maxlen (apply 'max (mapcar
  13149. (lambda (x)
  13150. (if (stringp (car x)) (string-width (car x)) 0))
  13151. fulltable)))
  13152. (buf (current-buffer))
  13153. (expert (eq org-fast-tag-selection-single-key 'expert))
  13154. (buffer-tags nil)
  13155. (fwidth (+ maxlen 3 1 3))
  13156. (ncol (/ (- (window-width) 4) fwidth))
  13157. (i-face 'org-done)
  13158. (c-face 'org-todo)
  13159. tg cnt e c char c1 c2 ntable tbl rtn
  13160. ov-start ov-end ov-prefix
  13161. (exit-after-next org-fast-tag-selection-single-key)
  13162. (done-keywords org-done-keywords)
  13163. groups ingroup)
  13164. (save-excursion
  13165. (beginning-of-line 1)
  13166. (if (looking-at
  13167. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13168. (setq ov-start (match-beginning 1)
  13169. ov-end (match-end 1)
  13170. ov-prefix "")
  13171. (setq ov-start (1- (point-at-eol))
  13172. ov-end (1+ ov-start))
  13173. (skip-chars-forward "^\n\r")
  13174. (setq ov-prefix
  13175. (concat
  13176. (buffer-substring (1- (point)) (point))
  13177. (if (> (current-column) org-tags-column)
  13178. " "
  13179. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13180. (move-overlay org-tags-overlay ov-start ov-end)
  13181. (save-window-excursion
  13182. (if expert
  13183. (set-buffer (get-buffer-create " *Org tags*"))
  13184. (delete-other-windows)
  13185. (split-window-vertically)
  13186. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13187. (erase-buffer)
  13188. (org-set-local 'org-done-keywords done-keywords)
  13189. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13190. (org-fast-tag-insert "Current" current c-face "\n\n")
  13191. (org-fast-tag-show-exit exit-after-next)
  13192. (org-set-current-tags-overlay current ov-prefix)
  13193. (setq tbl fulltable char ?a cnt 0)
  13194. (while (setq e (pop tbl))
  13195. (cond
  13196. ((equal (car e) :startgroup)
  13197. (push '() groups) (setq ingroup t)
  13198. (when (not (= cnt 0))
  13199. (setq cnt 0)
  13200. (insert "\n"))
  13201. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13202. ((equal (car e) :endgroup)
  13203. (setq ingroup nil cnt 0)
  13204. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13205. ((equal e '(:newline))
  13206. (when (not (= cnt 0))
  13207. (setq cnt 0)
  13208. (insert "\n")
  13209. (setq e (car tbl))
  13210. (while (equal (car tbl) '(:newline))
  13211. (insert "\n")
  13212. (setq tbl (cdr tbl)))))
  13213. (t
  13214. (setq tg (copy-sequence (car e)) c2 nil)
  13215. (if (cdr e)
  13216. (setq c (cdr e))
  13217. ;; automatically assign a character.
  13218. (setq c1 (string-to-char
  13219. (downcase (substring
  13220. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13221. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13222. (while (or (rassoc char ntable) (rassoc char table))
  13223. (setq char (1+ char)))
  13224. (setq c2 c1))
  13225. (setq c (or c2 char)))
  13226. (if ingroup (push tg (car groups)))
  13227. (setq tg (org-add-props tg nil 'face
  13228. (cond
  13229. ((not (assoc tg table))
  13230. (org-get-todo-face tg))
  13231. ((member tg current) c-face)
  13232. ((member tg inherited) i-face))))
  13233. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13234. (insert "[" c "] " tg (make-string
  13235. (- fwidth 4 (length tg)) ?\ ))
  13236. (push (cons tg c) ntable)
  13237. (when (= (setq cnt (1+ cnt)) ncol)
  13238. (insert "\n")
  13239. (if ingroup (insert " "))
  13240. (setq cnt 0)))))
  13241. (setq ntable (nreverse ntable))
  13242. (insert "\n")
  13243. (goto-char (point-min))
  13244. (if (not expert) (org-fit-window-to-buffer))
  13245. (setq rtn
  13246. (catch 'exit
  13247. (while t
  13248. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13249. (if (not groups) "no " "")
  13250. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13251. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13252. (setq org-last-tag-selection-key c)
  13253. (cond
  13254. ((= c ?\r) (throw 'exit t))
  13255. ((= c ?!)
  13256. (setq groups (not groups))
  13257. (goto-char (point-min))
  13258. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13259. ((= c ?\C-c)
  13260. (if (not expert)
  13261. (org-fast-tag-show-exit
  13262. (setq exit-after-next (not exit-after-next)))
  13263. (setq expert nil)
  13264. (delete-other-windows)
  13265. (set-window-buffer (split-window-vertically) " *Org tags*")
  13266. (org-switch-to-buffer-other-window " *Org tags*")
  13267. (org-fit-window-to-buffer)))
  13268. ((or (= c ?\C-g)
  13269. (and (= c ?q) (not (rassoc c ntable))))
  13270. (org-detach-overlay org-tags-overlay)
  13271. (setq quit-flag t))
  13272. ((= c ?\ )
  13273. (setq current nil)
  13274. (if exit-after-next (setq exit-after-next 'now)))
  13275. ((= c ?\t)
  13276. (condition-case nil
  13277. (setq tg (org-icompleting-read
  13278. "Tag: "
  13279. (or buffer-tags
  13280. (with-current-buffer buf
  13281. (org-get-buffer-tags)))))
  13282. (quit (setq tg "")))
  13283. (when (string-match "\\S-" tg)
  13284. (add-to-list 'buffer-tags (list tg))
  13285. (if (member tg current)
  13286. (setq current (delete tg current))
  13287. (push tg current)))
  13288. (if exit-after-next (setq exit-after-next 'now)))
  13289. ((setq e (rassoc c todo-table) tg (car e))
  13290. (with-current-buffer buf
  13291. (save-excursion (org-todo tg)))
  13292. (if exit-after-next (setq exit-after-next 'now)))
  13293. ((setq e (rassoc c ntable) tg (car e))
  13294. (if (member tg current)
  13295. (setq current (delete tg current))
  13296. (loop for g in groups do
  13297. (if (member tg g)
  13298. (mapc (lambda (x)
  13299. (setq current (delete x current)))
  13300. g)))
  13301. (push tg current))
  13302. (if exit-after-next (setq exit-after-next 'now))))
  13303. ;; Create a sorted list
  13304. (setq current
  13305. (sort current
  13306. (lambda (a b)
  13307. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13308. (if (eq exit-after-next 'now) (throw 'exit t))
  13309. (goto-char (point-min))
  13310. (beginning-of-line 2)
  13311. (delete-region (point) (point-at-eol))
  13312. (org-fast-tag-insert "Current" current c-face)
  13313. (org-set-current-tags-overlay current ov-prefix)
  13314. (while (re-search-forward
  13315. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13316. (setq tg (match-string 1))
  13317. (add-text-properties
  13318. (match-beginning 1) (match-end 1)
  13319. (list 'face
  13320. (cond
  13321. ((member tg current) c-face)
  13322. ((member tg inherited) i-face)
  13323. (t (get-text-property (match-beginning 1) 'face))))))
  13324. (goto-char (point-min)))))
  13325. (org-detach-overlay org-tags-overlay)
  13326. (if rtn
  13327. (mapconcat 'identity current ":")
  13328. nil))))
  13329. (defun org-get-tags-string ()
  13330. "Get the TAGS string in the current headline."
  13331. (unless (org-at-heading-p t)
  13332. (error "Not on a heading"))
  13333. (save-excursion
  13334. (beginning-of-line 1)
  13335. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13336. (org-match-string-no-properties 1)
  13337. "")))
  13338. (defun org-get-tags ()
  13339. "Get the list of tags specified in the current headline."
  13340. (org-split-string (org-get-tags-string) ":"))
  13341. (defun org-get-buffer-tags ()
  13342. "Get a table of all tags used in the buffer, for completion."
  13343. (let (tags)
  13344. (save-excursion
  13345. (goto-char (point-min))
  13346. (while (re-search-forward
  13347. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13348. (when (equal (char-after (point-at-bol 0)) ?*)
  13349. (mapc (lambda (x) (add-to-list 'tags x))
  13350. (org-split-string (org-match-string-no-properties 1) ":")))))
  13351. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13352. (mapcar 'list tags)))
  13353. ;;;; The mapping API
  13354. (defun org-map-entries (func &optional match scope &rest skip)
  13355. "Call FUNC at each headline selected by MATCH in SCOPE.
  13356. FUNC is a function or a lisp form. The function will be called without
  13357. arguments, with the cursor positioned at the beginning of the headline.
  13358. The return values of all calls to the function will be collected and
  13359. returned as a list.
  13360. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13361. does not need to preserve point. After evaluation, the cursor will be
  13362. moved to the end of the line (presumably of the headline of the
  13363. processed entry) and search continues from there. Under some
  13364. circumstances, this may not produce the wanted results. For example,
  13365. if you have removed (e.g. archived) the current (sub)tree it could
  13366. mean that the next entry will be skipped entirely. In such cases, you
  13367. can specify the position from where search should continue by making
  13368. FUNC set the variable `org-map-continue-from' to the desired buffer
  13369. position.
  13370. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13371. Only headlines that are matched by this query will be considered during
  13372. the iteration. When MATCH is nil or t, all headlines will be
  13373. visited by the iteration.
  13374. SCOPE determines the scope of this command. It can be any of:
  13375. nil The current buffer, respecting the restriction if any
  13376. tree The subtree started with the entry at point
  13377. region The entries within the active region, if any
  13378. region-start-level
  13379. The entries within the active region, but only those at
  13380. the same level than the first one.
  13381. file The current buffer, without restriction
  13382. file-with-archives
  13383. The current buffer, and any archives associated with it
  13384. agenda All agenda files
  13385. agenda-with-archives
  13386. All agenda files with any archive files associated with them
  13387. \(file1 file2 ...)
  13388. If this is a list, all files in the list will be scanned
  13389. The remaining args are treated as settings for the skipping facilities of
  13390. the scanner. The following items can be given here:
  13391. archive skip trees with the archive tag
  13392. comment skip trees with the COMMENT keyword
  13393. function or Emacs Lisp form:
  13394. will be used as value for `org-agenda-skip-function', so
  13395. whenever the function returns a position, FUNC will not be
  13396. called for that entry and search will continue from the
  13397. position returned
  13398. If your function needs to retrieve the tags including inherited tags
  13399. at the *current* entry, you can use the value of the variable
  13400. `org-scanner-tags' which will be much faster than getting the value
  13401. with `org-get-tags-at'. If your function gets properties with
  13402. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13403. to t around the call to `org-entry-properties' to get the same speedup.
  13404. Note that if your function moves around to retrieve tags and properties at
  13405. a *different* entry, you cannot use these techniques."
  13406. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13407. (not (org-region-active-p)))
  13408. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13409. (org-agenda-skip-archived-trees (memq 'archive skip))
  13410. (org-agenda-skip-comment-trees (memq 'comment skip))
  13411. (org-agenda-skip-function
  13412. (car (org-delete-all '(comment archive) skip)))
  13413. (org-tags-match-list-sublevels t)
  13414. (start-level (eq scope 'region-start-level))
  13415. matcher file res
  13416. org-todo-keywords-for-agenda
  13417. org-done-keywords-for-agenda
  13418. org-todo-keyword-alist-for-agenda
  13419. org-drawers-for-agenda
  13420. org-tag-alist-for-agenda
  13421. todo-only)
  13422. (cond
  13423. ((eq match t) (setq matcher t))
  13424. ((eq match nil) (setq matcher t))
  13425. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13426. (save-window-excursion
  13427. (save-restriction
  13428. (cond ((eq scope 'tree)
  13429. (org-back-to-heading t)
  13430. (org-narrow-to-subtree)
  13431. (setq scope nil))
  13432. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13433. (org-region-active-p))
  13434. ;; If needed, set start-level to a string like "2"
  13435. (when start-level
  13436. (save-excursion
  13437. (goto-char (region-beginning))
  13438. (unless (org-at-heading-p) (outline-next-heading))
  13439. (setq start-level (org-current-level))))
  13440. (narrow-to-region (region-beginning)
  13441. (save-excursion
  13442. (goto-char (region-end))
  13443. (unless (and (bolp) (org-at-heading-p))
  13444. (outline-next-heading))
  13445. (point)))
  13446. (setq scope nil)))
  13447. (if (not scope)
  13448. (progn
  13449. (org-agenda-prepare-buffers
  13450. (list (buffer-file-name (current-buffer))))
  13451. (setq res (org-scan-tags func matcher todo-only start-level)))
  13452. ;; Get the right scope
  13453. (cond
  13454. ((and scope (listp scope) (symbolp (car scope)))
  13455. (setq scope (eval scope)))
  13456. ((eq scope 'agenda)
  13457. (setq scope (org-agenda-files t)))
  13458. ((eq scope 'agenda-with-archives)
  13459. (setq scope (org-agenda-files t))
  13460. (setq scope (org-add-archive-files scope)))
  13461. ((eq scope 'file)
  13462. (setq scope (list (buffer-file-name))))
  13463. ((eq scope 'file-with-archives)
  13464. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13465. (org-agenda-prepare-buffers scope)
  13466. (while (setq file (pop scope))
  13467. (with-current-buffer (org-find-base-buffer-visiting file)
  13468. (save-excursion
  13469. (save-restriction
  13470. (widen)
  13471. (goto-char (point-min))
  13472. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13473. res)))
  13474. ;;;; Properties
  13475. ;;; Setting and retrieving properties
  13476. (defconst org-special-properties
  13477. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13478. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13479. "The special properties valid in Org-mode.
  13480. These are properties that are not defined in the property drawer,
  13481. but in some other way.")
  13482. (defconst org-default-properties
  13483. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13484. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13485. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13486. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13487. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13488. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13489. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13490. "Some properties that are used by Org-mode for various purposes.
  13491. Being in this list makes sure that they are offered for completion.")
  13492. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13493. "Regular expression matching the first line of a property drawer.")
  13494. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13495. "Regular expression matching the last line of a property drawer.")
  13496. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13497. "Regular expression matching the first line of a property drawer.")
  13498. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13499. "Regular expression matching the first line of a property drawer.")
  13500. (defconst org-property-drawer-re
  13501. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13502. org-property-end-re "\\)\n?")
  13503. "Matches an entire property drawer.")
  13504. (defconst org-clock-drawer-re
  13505. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13506. org-property-end-re "\\)\n?")
  13507. "Matches an entire clock drawer.")
  13508. (defsubst org-re-property (property)
  13509. "Return a regexp matching a PROPERTY line.
  13510. Match group 1 will be set to the value."
  13511. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13512. (defsubst org-re-property-keyword (property)
  13513. "Return a regexp matching a PROPERTY line, possibly with no
  13514. value for the property."
  13515. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13516. (defun org-property-action ()
  13517. "Do an action on properties."
  13518. (interactive)
  13519. (let (c)
  13520. (org-at-property-p)
  13521. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13522. (setq c (read-char-exclusive))
  13523. (cond
  13524. ((equal c ?s)
  13525. (call-interactively 'org-set-property))
  13526. ((equal c ?d)
  13527. (call-interactively 'org-delete-property))
  13528. ((equal c ?D)
  13529. (call-interactively 'org-delete-property-globally))
  13530. ((equal c ?c)
  13531. (call-interactively 'org-compute-property-at-point))
  13532. (t (error "No such property action %c" c)))))
  13533. (defun org-inc-effort ()
  13534. "Increment the value of the effort property in the current entry."
  13535. (interactive)
  13536. (org-set-effort nil t))
  13537. (defvar org-clock-effort) ;; Defined in org-clock.el
  13538. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13539. (defun org-set-effort (&optional value increment)
  13540. "Set the effort property of the current entry.
  13541. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13542. 10th allowed value.
  13543. When INCREMENT is non-nil, set the property to the next allowed value."
  13544. (interactive "P")
  13545. (if (equal value 0) (setq value 10))
  13546. (let* ((completion-ignore-case t)
  13547. (prop org-effort-property)
  13548. (cur (org-entry-get nil prop))
  13549. (allowed (org-property-get-allowed-values nil prop 'table))
  13550. (existing (mapcar 'list (org-property-values prop)))
  13551. (heading (nth 4 (org-heading-components)))
  13552. rpl
  13553. (val (cond
  13554. ((stringp value) value)
  13555. ((and allowed (integerp value))
  13556. (or (car (nth (1- value) allowed))
  13557. (car (org-last allowed))))
  13558. ((and allowed increment)
  13559. (or (caadr (member (list cur) allowed))
  13560. (error "Allowed effort values are not set")))
  13561. (allowed
  13562. (message "Select 1-9,0, [RET%s]: %s"
  13563. (if cur (concat "=" cur) "")
  13564. (mapconcat 'car allowed " "))
  13565. (setq rpl (read-char-exclusive))
  13566. (if (equal rpl ?\r)
  13567. cur
  13568. (setq rpl (- rpl ?0))
  13569. (if (equal rpl 0) (setq rpl 10))
  13570. (if (and (> rpl 0) (<= rpl (length allowed)))
  13571. (car (nth (1- rpl) allowed))
  13572. (org-completing-read "Effort: " allowed nil))))
  13573. (t
  13574. (let (org-completion-use-ido org-completion-use-iswitchb)
  13575. (org-completing-read
  13576. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13577. (concat "[" cur "]") "")
  13578. ": ")
  13579. existing nil nil "" nil cur))))))
  13580. (unless (equal (org-entry-get nil prop) val)
  13581. (org-entry-put nil prop val))
  13582. (save-excursion
  13583. (org-back-to-heading t)
  13584. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13585. (when (string= heading org-clock-current-task)
  13586. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13587. (org-clock-update-mode-line))
  13588. (message "%s is now %s" prop val)))
  13589. (defun org-at-property-p ()
  13590. "Is cursor inside a property drawer?"
  13591. (save-excursion
  13592. (beginning-of-line 1)
  13593. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13594. (save-match-data ;; Used by calling procedures
  13595. (let ((p (point))
  13596. (range (unless (org-before-first-heading-p)
  13597. (org-get-property-block))))
  13598. (and range (<= (car range) p) (< p (cdr range))))))))
  13599. (defun org-get-property-block (&optional beg end force)
  13600. "Return the (beg . end) range of the body of the property drawer.
  13601. BEG and END are the beginning and end of the current subtree, or of
  13602. the part before the first headline. If they are not given, they will
  13603. be found. If the drawer does not exist and FORCE is non-nil, create
  13604. the drawer."
  13605. (catch 'exit
  13606. (save-excursion
  13607. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13608. (progn (org-back-to-heading t) (point))))
  13609. (end (or end (and (not (outline-next-heading)) (point-max))
  13610. (point))))
  13611. (goto-char beg)
  13612. (if (re-search-forward org-property-start-re end t)
  13613. (setq beg (1+ (match-end 0)))
  13614. (if force
  13615. (save-excursion
  13616. (org-insert-property-drawer)
  13617. (setq end (progn (outline-next-heading) (point))))
  13618. (throw 'exit nil))
  13619. (goto-char beg)
  13620. (if (re-search-forward org-property-start-re end t)
  13621. (setq beg (1+ (match-end 0)))))
  13622. (if (re-search-forward org-property-end-re end t)
  13623. (setq end (match-beginning 0))
  13624. (or force (throw 'exit nil))
  13625. (goto-char beg)
  13626. (setq end beg)
  13627. (org-indent-line)
  13628. (insert ":END:\n"))
  13629. (cons beg end)))))
  13630. (defun org-entry-properties (&optional pom which specific)
  13631. "Get all properties of the entry at point-or-marker POM.
  13632. This includes the TODO keyword, the tags, time strings for deadline,
  13633. scheduled, and clocking, and any additional properties defined in the
  13634. entry. The return value is an alist, keys may occur multiple times
  13635. if the property key was used several times.
  13636. POM may also be nil, in which case the current entry is used.
  13637. If WHICH is nil or `all', get all properties. If WHICH is
  13638. `special' or `standard', only get that subclass. If WHICH
  13639. is a string only get exactly this property. SPECIFIC can be a string, the
  13640. specific property we are interested in. Specifying it can speed
  13641. things up because then unnecessary parsing is avoided."
  13642. (setq which (or which 'all))
  13643. (org-with-point-at pom
  13644. (let ((clockstr (substring org-clock-string 0 -1))
  13645. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13646. (case-fold-search nil)
  13647. beg end range props sum-props key key1 value string clocksum clocksumt)
  13648. (save-excursion
  13649. (when (condition-case nil
  13650. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13651. (error nil))
  13652. (setq beg (point))
  13653. (setq sum-props (get-text-property (point) 'org-summaries))
  13654. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13655. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13656. (outline-next-heading)
  13657. (setq end (point))
  13658. (when (memq which '(all special))
  13659. ;; Get the special properties, like TODO and tags
  13660. (goto-char beg)
  13661. (when (and (or (not specific) (string= specific "TODO"))
  13662. (looking-at org-todo-line-regexp) (match-end 2))
  13663. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13664. (when (and (or (not specific) (string= specific "PRIORITY"))
  13665. (looking-at org-priority-regexp))
  13666. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13667. (when (or (not specific) (string= specific "FILE"))
  13668. (push (cons "FILE" buffer-file-name) props))
  13669. (when (and (or (not specific) (string= specific "TAGS"))
  13670. (setq value (org-get-tags-string))
  13671. (string-match "\\S-" value))
  13672. (push (cons "TAGS" value) props))
  13673. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13674. (setq value (org-get-tags-at)))
  13675. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13676. ":"))
  13677. props))
  13678. (when (or (not specific) (string= specific "BLOCKED"))
  13679. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13680. (when (or (not specific)
  13681. (member specific
  13682. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13683. "TIMESTAMP" "TIMESTAMP_IA")))
  13684. (catch 'match
  13685. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13686. (setq key (if (match-end 1)
  13687. (substring (org-match-string-no-properties 1)
  13688. 0 -1))
  13689. string (if (equal key clockstr)
  13690. (org-trim
  13691. (buffer-substring-no-properties
  13692. (match-beginning 3) (goto-char
  13693. (point-at-eol))))
  13694. (substring (org-match-string-no-properties 3)
  13695. 1 -1)))
  13696. ;; Get the correct property name from the key. This is
  13697. ;; necessary if the user has configured time keywords.
  13698. (setq key1 (concat key ":"))
  13699. (cond
  13700. ((not key)
  13701. (setq key
  13702. (if (= (char-after (match-beginning 3)) ?\[)
  13703. "TIMESTAMP_IA" "TIMESTAMP")))
  13704. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13705. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13706. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13707. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13708. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13709. (progn
  13710. (push (cons key string) props)
  13711. ;; no need to search further if match is found
  13712. (throw 'match t))
  13713. (when (or (equal key "CLOCK") (not (assoc key props)))
  13714. (push (cons key string) props)))))))
  13715. (when (memq which '(all standard))
  13716. ;; Get the standard properties, like :PROP: ...
  13717. (setq range (org-get-property-block beg end))
  13718. (when range
  13719. (goto-char (car range))
  13720. (while (re-search-forward
  13721. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13722. (cdr range) t)
  13723. (setq key (org-match-string-no-properties 1)
  13724. value (org-trim (or (org-match-string-no-properties 2) "")))
  13725. (unless (member key excluded)
  13726. (push (cons key (or value "")) props)))))
  13727. (if clocksum
  13728. (push (cons "CLOCKSUM"
  13729. (org-columns-number-to-string (/ (float clocksum) 60.)
  13730. 'add_times))
  13731. props))
  13732. (if clocksumt
  13733. (push (cons "CLOCKSUM_T"
  13734. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13735. 'add_times))
  13736. props))
  13737. (unless (assoc "CATEGORY" props)
  13738. (push (cons "CATEGORY" (org-get-category)) props))
  13739. (append sum-props (nreverse props)))))))
  13740. (defun org-entry-get (pom property &optional inherit literal-nil)
  13741. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13742. If INHERIT is non-nil and the entry does not have the property,
  13743. then also check higher levels of the hierarchy.
  13744. If INHERIT is the symbol `selective', use inheritance only if the setting
  13745. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13746. If the property is present but empty, the return value is the empty string.
  13747. If the property is not present at all, nil is returned.
  13748. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13749. do not interpret it as the list atom nil. This is used for inheritance
  13750. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13751. (org-with-point-at pom
  13752. (if (and inherit (if (eq inherit 'selective)
  13753. (org-property-inherit-p property)
  13754. t))
  13755. (org-entry-get-with-inheritance property literal-nil)
  13756. (if (member property org-special-properties)
  13757. ;; We need a special property. Use `org-entry-properties' to
  13758. ;; retrieve it, but specify the wanted property
  13759. (cdr (assoc property (org-entry-properties nil 'special property)))
  13760. (let ((range (org-get-property-block)))
  13761. (when (and range (not (eq (car range) (cdr range))))
  13762. (let* ((props (list (or (assoc property org-file-properties)
  13763. (assoc property org-global-properties)
  13764. (assoc property org-global-properties-fixed))))
  13765. (ap (lambda (key)
  13766. (when (re-search-forward
  13767. (org-re-property key) (cdr range) t)
  13768. (setq props
  13769. (org-update-property-plist
  13770. key
  13771. (if (match-end 1)
  13772. (org-match-string-no-properties 1) "")
  13773. props)))))
  13774. val)
  13775. (goto-char (car range))
  13776. (funcall ap property)
  13777. (goto-char (car range))
  13778. (while (funcall ap (concat property "+")))
  13779. (setq val (cdr (assoc property props)))
  13780. (when val (if literal-nil val (org-not-nil val))))))))))
  13781. (defun org-property-or-variable-value (var &optional inherit)
  13782. "Check if there is a property fixing the value of VAR.
  13783. If yes, return this value. If not, return the current value of the variable."
  13784. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13785. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13786. (read prop)
  13787. (symbol-value var))))
  13788. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  13789. "Delete the property PROPERTY from entry at point-or-marker POM.
  13790. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  13791. an empty drawer to delete."
  13792. (org-with-point-at pom
  13793. (if (member property org-special-properties)
  13794. nil ; cannot delete these properties.
  13795. (let ((range (org-get-property-block)))
  13796. (if (and range
  13797. (goto-char (car range))
  13798. (re-search-forward
  13799. (org-re-property property)
  13800. (cdr range) t))
  13801. (progn
  13802. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13803. (and delete-empty-drawer
  13804. (org-remove-empty-drawer-at
  13805. delete-empty-drawer (car range)))
  13806. t)
  13807. nil)))))
  13808. ;; Multi-values properties are properties that contain multiple values
  13809. ;; These values are assumed to be single words, separated by whitespace.
  13810. (defun org-entry-add-to-multivalued-property (pom property value)
  13811. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13812. (let* ((old (org-entry-get pom property))
  13813. (values (and old (org-split-string old "[ \t]"))))
  13814. (setq value (org-entry-protect-space value))
  13815. (unless (member value values)
  13816. (setq values (cons value values))
  13817. (org-entry-put pom property
  13818. (mapconcat 'identity values " ")))))
  13819. (defun org-entry-remove-from-multivalued-property (pom property value)
  13820. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13821. (let* ((old (org-entry-get pom property))
  13822. (values (and old (org-split-string old "[ \t]"))))
  13823. (setq value (org-entry-protect-space value))
  13824. (when (member value values)
  13825. (setq values (delete value values))
  13826. (org-entry-put pom property
  13827. (mapconcat 'identity values " ")))))
  13828. (defun org-entry-member-in-multivalued-property (pom property value)
  13829. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13830. (let* ((old (org-entry-get pom property))
  13831. (values (and old (org-split-string old "[ \t]"))))
  13832. (setq value (org-entry-protect-space value))
  13833. (member value values)))
  13834. (defun org-entry-get-multivalued-property (pom property)
  13835. "Return a list of values in a multivalued property."
  13836. (let* ((value (org-entry-get pom property))
  13837. (values (and value (org-split-string value "[ \t]"))))
  13838. (mapcar 'org-entry-restore-space values)))
  13839. (defun org-entry-put-multivalued-property (pom property &rest values)
  13840. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13841. VALUES should be a list of strings. Spaces will be protected."
  13842. (org-entry-put pom property
  13843. (mapconcat 'org-entry-protect-space values " "))
  13844. (let* ((value (org-entry-get pom property))
  13845. (values (and value (org-split-string value "[ \t]"))))
  13846. (mapcar 'org-entry-restore-space values)))
  13847. (defun org-entry-protect-space (s)
  13848. "Protect spaces and newline in string S."
  13849. (while (string-match " " s)
  13850. (setq s (replace-match "%20" t t s)))
  13851. (while (string-match "\n" s)
  13852. (setq s (replace-match "%0A" t t s)))
  13853. s)
  13854. (defun org-entry-restore-space (s)
  13855. "Restore spaces and newline in string S."
  13856. (while (string-match "%20" s)
  13857. (setq s (replace-match " " t t s)))
  13858. (while (string-match "%0A" s)
  13859. (setq s (replace-match "\n" t t s)))
  13860. s)
  13861. (defvar org-entry-property-inherited-from (make-marker)
  13862. "Marker pointing to the entry from where a property was inherited.
  13863. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13864. location of the entry where the inheritance search matched. If there was
  13865. no match, the marker will point nowhere.
  13866. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13867. is set.")
  13868. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13869. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13870. The search will stop at the first ancestor which has the property defined.
  13871. If the value found is \"nil\", return nil to show that the property
  13872. should be considered as undefined (this is the meaning of nil here).
  13873. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13874. (move-marker org-entry-property-inherited-from nil)
  13875. (let (tmp)
  13876. (save-excursion
  13877. (save-restriction
  13878. (widen)
  13879. (catch 'ex
  13880. (while t
  13881. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13882. (or (ignore-errors (org-back-to-heading t))
  13883. (goto-char (point-min)))
  13884. (move-marker org-entry-property-inherited-from (point))
  13885. (throw 'ex tmp))
  13886. (or (ignore-errors (org-up-heading-safe))
  13887. (throw 'ex nil))))))
  13888. (setq tmp (or tmp
  13889. (cdr (assoc property org-file-properties))
  13890. (cdr (assoc property org-global-properties))
  13891. (cdr (assoc property org-global-properties-fixed))))
  13892. (if literal-nil tmp (org-not-nil tmp))))
  13893. (defvar org-property-changed-functions nil
  13894. "Hook called when the value of a property has changed.
  13895. Each hook function should accept two arguments, the name of the property
  13896. and the new value.")
  13897. (defun org-entry-put (pom property value)
  13898. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13899. (org-with-point-at pom
  13900. (org-back-to-heading t)
  13901. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13902. range)
  13903. (cond
  13904. ((equal property "TODO")
  13905. (when (and (stringp value) (string-match "\\S-" value)
  13906. (not (member value org-todo-keywords-1)))
  13907. (error "\"%s\" is not a valid TODO state" value))
  13908. (if (or (not value)
  13909. (not (string-match "\\S-" value)))
  13910. (setq value 'none))
  13911. (org-todo value)
  13912. (org-set-tags nil 'align))
  13913. ((equal property "PRIORITY")
  13914. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13915. (string-to-char value) ?\ ))
  13916. (org-set-tags nil 'align))
  13917. ((equal property "CLOCKSUM")
  13918. (if (not (re-search-forward
  13919. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  13920. (error "Cannot find a clock log")
  13921. (goto-char (- (match-end 1) 2))
  13922. (cond
  13923. ((eq value 'earlier) (org-timestamp-down))
  13924. ((eq value 'later) (org-timestamp-up)))
  13925. (org-clock-sum-current-item)))
  13926. ((equal property "SCHEDULED")
  13927. (if (re-search-forward org-scheduled-time-regexp end t)
  13928. (cond
  13929. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13930. ((eq value 'later) (org-timestamp-change 1 'day))
  13931. (t (call-interactively 'org-schedule)))
  13932. (call-interactively 'org-schedule)))
  13933. ((equal property "DEADLINE")
  13934. (if (re-search-forward org-deadline-time-regexp end t)
  13935. (cond
  13936. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13937. ((eq value 'later) (org-timestamp-change 1 'day))
  13938. (t (call-interactively 'org-deadline)))
  13939. (call-interactively 'org-deadline)))
  13940. ((member property org-special-properties)
  13941. (error "The %s property can not yet be set with `org-entry-put'"
  13942. property))
  13943. (t ; a non-special property
  13944. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13945. (setq range (org-get-property-block beg end 'force))
  13946. (goto-char (car range))
  13947. (if (re-search-forward
  13948. (org-re-property-keyword property) (cdr range) t)
  13949. (progn
  13950. (delete-region (match-beginning 0) (match-end 0))
  13951. (goto-char (match-beginning 0)))
  13952. (goto-char (cdr range))
  13953. (insert "\n")
  13954. (backward-char 1)
  13955. (org-indent-line))
  13956. (insert ":" property ":")
  13957. (and value (insert " " value))
  13958. (org-indent-line)))))
  13959. (run-hook-with-args 'org-property-changed-functions property value)))
  13960. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13961. "Get all property keys in the current buffer.
  13962. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13963. like tags and TODO state.
  13964. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13965. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13966. and others.
  13967. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13968. formats in the current buffer."
  13969. (let (rtn range cfmt s p)
  13970. (save-excursion
  13971. (save-restriction
  13972. (widen)
  13973. (goto-char (point-min))
  13974. (while (re-search-forward org-property-start-re nil t)
  13975. (setq range (org-get-property-block))
  13976. (goto-char (car range))
  13977. (while (re-search-forward
  13978. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13979. (cdr range) t)
  13980. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13981. (outline-next-heading))))
  13982. (when include-specials
  13983. (setq rtn (append org-special-properties rtn)))
  13984. (when include-defaults
  13985. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13986. (add-to-list 'rtn org-effort-property))
  13987. (when include-columns
  13988. (save-excursion
  13989. (save-restriction
  13990. (widen)
  13991. (goto-char (point-min))
  13992. (while (re-search-forward
  13993. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13994. nil t)
  13995. (setq cfmt (match-string 2) s 0)
  13996. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13997. cfmt s)
  13998. (setq s (match-end 0)
  13999. p (match-string 1 cfmt))
  14000. (unless (or (equal p "ITEM")
  14001. (member p org-special-properties))
  14002. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14003. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14004. (defun org-property-values (key)
  14005. "Return a list of all values of property KEY in the current buffer."
  14006. (save-excursion
  14007. (save-restriction
  14008. (widen)
  14009. (goto-char (point-min))
  14010. (let ((re (org-re-property key))
  14011. values)
  14012. (while (re-search-forward re nil t)
  14013. (add-to-list 'values (org-trim (match-string 1))))
  14014. (delete "" values)))))
  14015. (defun org-insert-property-drawer ()
  14016. "Insert a property drawer into the current entry."
  14017. (org-back-to-heading t)
  14018. (looking-at org-outline-regexp)
  14019. (let ((indent (if org-adapt-indentation
  14020. (- (match-end 0) (match-beginning 0))
  14021. 0))
  14022. (beg (point))
  14023. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14024. end hiddenp)
  14025. (outline-next-heading)
  14026. (setq end (point))
  14027. (goto-char beg)
  14028. (while (re-search-forward re end t))
  14029. (setq hiddenp (outline-invisible-p))
  14030. (end-of-line 1)
  14031. (and (equal (char-after) ?\n) (forward-char 1))
  14032. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14033. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14034. ;; just skip this line
  14035. (beginning-of-line 2)
  14036. ;; Drawer start, find the end
  14037. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14038. (beginning-of-line 1)))
  14039. (org-skip-over-state-notes)
  14040. (skip-chars-backward " \t\n\r")
  14041. (if (eq (char-before) ?*) (forward-char 1))
  14042. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14043. (beginning-of-line 0)
  14044. (org-indent-to-column indent)
  14045. (beginning-of-line 2)
  14046. (org-indent-to-column indent)
  14047. (beginning-of-line 0)
  14048. (if hiddenp
  14049. (save-excursion
  14050. (org-back-to-heading t)
  14051. (hide-entry))
  14052. (org-flag-drawer t))))
  14053. (defun org-insert-drawer (&optional arg drawer)
  14054. "Insert a drawer at point.
  14055. Optional argument DRAWER, when non-nil, is a string representing
  14056. drawer's name. Otherwise, the user is prompted for a name.
  14057. If a region is active, insert the drawer around that region
  14058. instead.
  14059. Point is left between drawer's boundaries."
  14060. (interactive "P")
  14061. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14062. "LOGBOOK"))
  14063. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14064. ;; internally by Org. They are meant to be inserted
  14065. ;; automatically.
  14066. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14067. ;; Remove system drawers from list. Note: For some reason,
  14068. ;; `org-completing-read' ignores the predicate while
  14069. ;; `completing-read' handles it fine.
  14070. (drawer (if arg "PROPERTIES"
  14071. (or drawer
  14072. (completing-read
  14073. "Drawer: " org-drawers
  14074. (lambda (d) (not (member d system-drawers))))))))
  14075. (cond
  14076. ;; With C-u, fall back on `org-insert-property-drawer'
  14077. (arg (org-insert-property-drawer))
  14078. ;; With an active region, insert a drawer at point.
  14079. ((not (org-region-active-p))
  14080. (progn
  14081. (unless (bolp) (insert "\n"))
  14082. (insert (format ":%s:\n\n:END:\n" drawer))
  14083. (forward-line -2)))
  14084. ;; Otherwise, insert the drawer at point
  14085. (t
  14086. (let ((rbeg (region-beginning))
  14087. (rend (copy-marker (region-end))))
  14088. (unwind-protect
  14089. (progn
  14090. (goto-char rbeg)
  14091. (beginning-of-line)
  14092. (when (save-excursion
  14093. (re-search-forward org-outline-regexp-bol rend t))
  14094. (error "Drawers cannot contain headlines"))
  14095. ;; Position point at the beginning of the first
  14096. ;; non-blank line in region. Insert drawer's opening
  14097. ;; there, then indent it.
  14098. (org-skip-whitespace)
  14099. (beginning-of-line)
  14100. (insert ":" drawer ":\n")
  14101. (forward-line -1)
  14102. (indent-for-tab-command)
  14103. ;; Move point to the beginning of the first blank line
  14104. ;; after the last non-blank line in region. Insert
  14105. ;; drawer's closing, then indent it.
  14106. (goto-char rend)
  14107. (skip-chars-backward " \r\t\n")
  14108. (insert "\n:END:")
  14109. (deactivate-mark t)
  14110. (indent-for-tab-command)
  14111. (unless (eolp) (insert "\n")))
  14112. ;; Clear marker, whatever the outcome of insertion is.
  14113. (set-marker rend nil)))))))
  14114. (defvar org-property-set-functions-alist nil
  14115. "Property set function alist.
  14116. Each entry should have the following format:
  14117. (PROPERTY . READ-FUNCTION)
  14118. The read function will be called with the same argument as
  14119. `org-completing-read'.")
  14120. (defun org-set-property-function (property)
  14121. "Get the function that should be used to set PROPERTY.
  14122. This is computed according to `org-property-set-functions-alist'."
  14123. (or (cdr (assoc property org-property-set-functions-alist))
  14124. 'org-completing-read))
  14125. (defun org-read-property-value (property)
  14126. "Read PROPERTY value from user."
  14127. (let* ((completion-ignore-case t)
  14128. (allowed (org-property-get-allowed-values nil property 'table))
  14129. (cur (org-entry-get nil property))
  14130. (prompt (concat property " value"
  14131. (if (and cur (string-match "\\S-" cur))
  14132. (concat " [" cur "]") "") ": "))
  14133. (set-function (org-set-property-function property))
  14134. (val (if allowed
  14135. (funcall set-function prompt allowed nil
  14136. (not (get-text-property 0 'org-unrestricted
  14137. (caar allowed))))
  14138. (let (org-completion-use-ido org-completion-use-iswitchb)
  14139. (funcall set-function prompt
  14140. (mapcar 'list (org-property-values property))
  14141. nil nil "" nil cur)))))
  14142. (if (equal val "")
  14143. cur
  14144. val)))
  14145. (defvar org-last-set-property nil)
  14146. (defvar org-last-set-property-value nil)
  14147. (defun org-read-property-name ()
  14148. "Read a property name."
  14149. (let* ((completion-ignore-case t)
  14150. (keys (org-buffer-property-keys nil t t))
  14151. (default-prop (or (save-excursion
  14152. (save-match-data
  14153. (beginning-of-line)
  14154. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14155. (null (string= (match-string 1) "END"))
  14156. (match-string 1))))
  14157. org-last-set-property))
  14158. (property (org-icompleting-read
  14159. (concat "Property"
  14160. (if default-prop (concat " [" default-prop "]") "")
  14161. ": ")
  14162. (mapcar 'list keys)
  14163. nil nil nil nil
  14164. default-prop)))
  14165. (if (member property keys)
  14166. property
  14167. (or (cdr (assoc (downcase property)
  14168. (mapcar (lambda (x) (cons (downcase x) x))
  14169. keys)))
  14170. property))))
  14171. (defun org-set-property-and-value (use-last)
  14172. "Allow to set [PROPERTY]: [value] direction from prompt.
  14173. When use-default, don't even ask, just use the last
  14174. \"[PROPERTY]: [value]\" string from the history."
  14175. (interactive "P")
  14176. (let* ((completion-ignore-case t)
  14177. (pv (or (and use-last org-last-set-property-value)
  14178. (org-completing-read
  14179. "Enter a \"[Property]: [value]\" pair: "
  14180. nil nil nil nil nil
  14181. org-last-set-property-value)))
  14182. prop val)
  14183. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14184. (setq prop (match-string 1 pv)
  14185. val (match-string 2 pv))
  14186. (org-set-property prop val))))
  14187. (defun org-set-property (property value)
  14188. "In the current entry, set PROPERTY to VALUE.
  14189. When called interactively, this will prompt for a property name, offering
  14190. completion on existing and default properties. And then it will prompt
  14191. for a value, offering completion either on allowed values (via an inherited
  14192. xxx_ALL property) or on existing values in other instances of this property
  14193. in the current file."
  14194. (interactive (list nil nil))
  14195. (let* ((property (or property (org-read-property-name)))
  14196. (value (or value (org-read-property-value property)))
  14197. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14198. (setq org-last-set-property property)
  14199. (setq org-last-set-property-value (concat property ": " value))
  14200. ;; Possibly postprocess the inserted value:
  14201. (when fn (setq value (funcall fn value)))
  14202. (unless (equal (org-entry-get nil property) value)
  14203. (org-entry-put nil property value))))
  14204. (defun org-delete-property (property &optional delete-empty-drawer)
  14205. "In the current entry, delete PROPERTY.
  14206. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14207. an empty drawer to delete."
  14208. (interactive
  14209. (let* ((completion-ignore-case t)
  14210. (prop (org-icompleting-read "Property: "
  14211. (org-entry-properties nil 'standard))))
  14212. (list prop)))
  14213. (message "Property %s %s" property
  14214. (if (org-entry-delete nil property delete-empty-drawer)
  14215. "deleted"
  14216. "was not present in the entry")))
  14217. (defun org-delete-property-globally (property)
  14218. "Remove PROPERTY globally, from all entries."
  14219. (interactive
  14220. (let* ((completion-ignore-case t)
  14221. (prop (org-icompleting-read
  14222. "Globally remove property: "
  14223. (mapcar 'list (org-buffer-property-keys)))))
  14224. (list prop)))
  14225. (save-excursion
  14226. (save-restriction
  14227. (widen)
  14228. (goto-char (point-min))
  14229. (let ((cnt 0))
  14230. (while (re-search-forward
  14231. (org-re-property property)
  14232. nil t)
  14233. (setq cnt (1+ cnt))
  14234. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14235. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14236. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14237. (defun org-compute-property-at-point ()
  14238. "Compute the property at point.
  14239. This looks for an enclosing column format, extracts the operator and
  14240. then applies it to the property in the column format's scope."
  14241. (interactive)
  14242. (unless (org-at-property-p)
  14243. (error "Not at a property"))
  14244. (let ((prop (org-match-string-no-properties 2)))
  14245. (org-columns-get-format-and-top-level)
  14246. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14247. (error "No operator defined for property %s" prop))
  14248. (org-columns-compute prop)))
  14249. (defvar org-property-allowed-value-functions nil
  14250. "Hook for functions supplying allowed values for a specific property.
  14251. The functions must take a single argument, the name of the property, and
  14252. return a flat list of allowed values. If \":ETC\" is one of
  14253. the values, this means that these values are intended as defaults for
  14254. completion, but that other values should be allowed too.
  14255. The functions must return nil if they are not responsible for this
  14256. property.")
  14257. (defun org-property-get-allowed-values (pom property &optional table)
  14258. "Get allowed values for the property PROPERTY.
  14259. When TABLE is non-nil, return an alist that can directly be used for
  14260. completion."
  14261. (let (vals)
  14262. (cond
  14263. ((equal property "TODO")
  14264. (setq vals (org-with-point-at pom
  14265. (append org-todo-keywords-1 '("")))))
  14266. ((equal property "PRIORITY")
  14267. (let ((n org-lowest-priority))
  14268. (while (>= n org-highest-priority)
  14269. (push (char-to-string n) vals)
  14270. (setq n (1- n)))))
  14271. ((member property org-special-properties))
  14272. ((setq vals (run-hook-with-args-until-success
  14273. 'org-property-allowed-value-functions property)))
  14274. (t
  14275. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14276. (when (and vals (string-match "\\S-" vals))
  14277. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14278. (setq vals (mapcar (lambda (x)
  14279. (cond ((stringp x) x)
  14280. ((numberp x) (number-to-string x))
  14281. ((symbolp x) (symbol-name x))
  14282. (t "???")))
  14283. vals)))))
  14284. (when (member ":ETC" vals)
  14285. (setq vals (remove ":ETC" vals))
  14286. (org-add-props (car vals) '(org-unrestricted t)))
  14287. (if table (mapcar 'list vals) vals)))
  14288. (defun org-property-previous-allowed-value (&optional previous)
  14289. "Switch to the next allowed value for this property."
  14290. (interactive)
  14291. (org-property-next-allowed-value t))
  14292. (defun org-property-next-allowed-value (&optional previous)
  14293. "Switch to the next allowed value for this property."
  14294. (interactive)
  14295. (unless (org-at-property-p)
  14296. (error "Not at a property"))
  14297. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14298. (key (match-string 2))
  14299. (value (match-string 3))
  14300. (allowed (or (org-property-get-allowed-values (point) key)
  14301. (and (member value '("[ ]" "[-]" "[X]"))
  14302. '("[ ]" "[X]"))))
  14303. (heading (save-match-data (nth 4 (org-heading-components))))
  14304. nval)
  14305. (unless allowed
  14306. (error "Allowed values for this property have not been defined"))
  14307. (if previous (setq allowed (reverse allowed)))
  14308. (if (member value allowed)
  14309. (setq nval (car (cdr (member value allowed)))))
  14310. (setq nval (or nval (car allowed)))
  14311. (if (equal nval value)
  14312. (error "Only one allowed value for this property"))
  14313. (org-at-property-p)
  14314. (replace-match (concat " :" key ": " nval) t t)
  14315. (org-indent-line)
  14316. (beginning-of-line 1)
  14317. (skip-chars-forward " \t")
  14318. (when (equal prop org-effort-property)
  14319. (save-excursion
  14320. (org-back-to-heading t)
  14321. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14322. (when (string= org-clock-current-task heading)
  14323. (setq org-clock-effort nval)
  14324. (org-clock-update-mode-line)))
  14325. (run-hook-with-args 'org-property-changed-functions key nval)))
  14326. (defun org-find-olp (path &optional this-buffer)
  14327. "Return a marker pointing to the entry at outline path OLP.
  14328. If anything goes wrong, throw an error.
  14329. You can wrap this call to catch the error like this:
  14330. (condition-case msg
  14331. (org-mobile-locate-entry (match-string 4))
  14332. (error (nth 1 msg)))
  14333. The return value will then be either a string with the error message,
  14334. or a marker if everything is OK.
  14335. If THIS-BUFFER is set, the outline path does not contain a file,
  14336. only headings."
  14337. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14338. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14339. (level 1)
  14340. (lmin 1)
  14341. (lmax 1)
  14342. limit re end found pos heading cnt flevel)
  14343. (unless buffer (error "File not found :%s" file))
  14344. (with-current-buffer buffer
  14345. (save-excursion
  14346. (save-restriction
  14347. (widen)
  14348. (setq limit (point-max))
  14349. (goto-char (point-min))
  14350. (while (setq heading (pop path))
  14351. (setq re (format org-complex-heading-regexp-format
  14352. (regexp-quote heading)))
  14353. (setq cnt 0 pos (point))
  14354. (while (re-search-forward re end t)
  14355. (setq level (- (match-end 1) (match-beginning 1)))
  14356. (if (and (>= level lmin) (<= level lmax))
  14357. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14358. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14359. lmax heading))
  14360. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14361. lmax heading))
  14362. (goto-char found)
  14363. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14364. (setq end (save-excursion (org-end-of-subtree t t))))
  14365. (when (org-at-heading-p)
  14366. (point-marker)))))))
  14367. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14368. "Find node HEADING in BUFFER.
  14369. Return a marker to the heading if it was found, or nil if not.
  14370. If POS-ONLY is set, return just the position instead of a marker.
  14371. The heading text must match exact, but it may have a TODO keyword,
  14372. a priority cookie and tags in the standard locations."
  14373. (with-current-buffer (or buffer (current-buffer))
  14374. (save-excursion
  14375. (save-restriction
  14376. (widen)
  14377. (goto-char (point-min))
  14378. (let (case-fold-search)
  14379. (if (re-search-forward
  14380. (format org-complex-heading-regexp-format
  14381. (regexp-quote heading)) nil t)
  14382. (if pos-only
  14383. (match-beginning 0)
  14384. (move-marker (make-marker) (match-beginning 0)))))))))
  14385. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14386. "Find Org node headline HEADING in all .org files in directory DIR.
  14387. When the target headline is found, return a marker to this location."
  14388. (let ((files (directory-files (or dir default-directory)
  14389. nil "\\`[^.#].*\\.org\\'"))
  14390. file visiting m buffer)
  14391. (catch 'found
  14392. (while (setq file (pop files))
  14393. (message "trying %s" file)
  14394. (setq visiting (org-find-base-buffer-visiting file))
  14395. (setq buffer (or visiting (find-file-noselect file)))
  14396. (setq m (org-find-exact-headline-in-buffer
  14397. heading buffer))
  14398. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14399. (and m (throw 'found m))))))
  14400. (defun org-find-entry-with-id (ident)
  14401. "Locate the entry that contains the ID property with exact value IDENT.
  14402. IDENT can be a string, a symbol or a number, this function will search for
  14403. the string representation of it.
  14404. Return the position where this entry starts, or nil if there is no such entry."
  14405. (interactive "sID: ")
  14406. (let ((id (cond
  14407. ((stringp ident) ident)
  14408. ((symbol-name ident) (symbol-name ident))
  14409. ((numberp ident) (number-to-string ident))
  14410. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14411. (case-fold-search nil))
  14412. (save-excursion
  14413. (save-restriction
  14414. (widen)
  14415. (goto-char (point-min))
  14416. (when (re-search-forward
  14417. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14418. nil t)
  14419. (org-back-to-heading t)
  14420. (point))))))
  14421. ;;;; Timestamps
  14422. (defvar org-last-changed-timestamp nil)
  14423. (defvar org-last-inserted-timestamp nil
  14424. "The last time stamp inserted with `org-insert-time-stamp'.")
  14425. (defvar org-time-was-given) ; dynamically scoped parameter
  14426. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14427. (defvar org-ts-what) ; dynamically scoped parameter
  14428. (defun org-time-stamp (arg &optional inactive)
  14429. "Prompt for a date/time and insert a time stamp.
  14430. If the user specifies a time like HH:MM or if this command is
  14431. called with at least one prefix argument, the time stamp contains
  14432. the date and the time. Otherwise, only the date is be included.
  14433. All parts of a date not specified by the user is filled in from
  14434. the current date/time. So if you just press return without
  14435. typing anything, the time stamp will represent the current
  14436. date/time.
  14437. If there is already a timestamp at the cursor, it will be
  14438. modified.
  14439. With two universal prefix arguments, insert an active timestamp
  14440. with the current time without prompting the user."
  14441. (interactive "P")
  14442. (let* ((ts nil)
  14443. (default-time
  14444. ;; Default time is either today, or, when entering a range,
  14445. ;; the range start.
  14446. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14447. (save-excursion
  14448. (re-search-backward
  14449. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14450. (- (point) 20) t)))
  14451. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14452. (current-time)))
  14453. (default-input (and ts (org-get-compact-tod ts)))
  14454. (repeater (save-excursion
  14455. (save-match-data
  14456. (beginning-of-line)
  14457. (when (re-search-forward
  14458. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14459. (save-excursion (progn (end-of-line) (point))) t)
  14460. (match-string 0)))))
  14461. org-time-was-given org-end-time-was-given time)
  14462. (cond
  14463. ((and (org-at-timestamp-p t)
  14464. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14465. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14466. (insert "--")
  14467. (setq time (let ((this-command this-command))
  14468. (org-read-date arg 'totime nil nil
  14469. default-time default-input inactive)))
  14470. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14471. ((org-at-timestamp-p t)
  14472. (setq time (let ((this-command this-command))
  14473. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14474. (when (org-at-timestamp-p t) ; just to get the match data
  14475. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14476. (replace-match "")
  14477. (setq org-last-changed-timestamp
  14478. (org-insert-time-stamp
  14479. time (or org-time-was-given arg)
  14480. inactive nil nil (list org-end-time-was-given)))
  14481. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14482. (setq org-last-changed-timestamp
  14483. (concat (substring org-last-inserted-timestamp 0 -1)
  14484. " " repeater ">"))))
  14485. (message "Timestamp updated"))
  14486. ((equal arg '(16))
  14487. (org-insert-time-stamp (current-time) t))
  14488. (t
  14489. (setq time (let ((this-command this-command))
  14490. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14491. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14492. nil nil (list org-end-time-was-given))))))
  14493. ;; FIXME: can we use this for something else, like computing time differences?
  14494. (defun org-get-compact-tod (s)
  14495. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14496. (let* ((t1 (match-string 1 s))
  14497. (h1 (string-to-number (match-string 2 s)))
  14498. (m1 (string-to-number (match-string 3 s)))
  14499. (t2 (and (match-end 4) (match-string 5 s)))
  14500. (h2 (and t2 (string-to-number (match-string 6 s))))
  14501. (m2 (and t2 (string-to-number (match-string 7 s))))
  14502. dh dm)
  14503. (if (not t2)
  14504. t1
  14505. (setq dh (- h2 h1) dm (- m2 m1))
  14506. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14507. (concat t1 "+" (number-to-string dh)
  14508. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14509. (defun org-time-stamp-inactive (&optional arg)
  14510. "Insert an inactive time stamp.
  14511. An inactive time stamp is enclosed in square brackets instead of angle
  14512. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14513. does not link to the calendar and cannot be changed with the S-cursor keys.
  14514. So these are more for recording a certain time/date."
  14515. (interactive "P")
  14516. (org-time-stamp arg 'inactive))
  14517. (defvar org-date-ovl (make-overlay 1 1))
  14518. (overlay-put org-date-ovl 'face 'org-date-selected)
  14519. (org-detach-overlay org-date-ovl)
  14520. (defvar org-ans1) ; dynamically scoped parameter
  14521. (defvar org-ans2) ; dynamically scoped parameter
  14522. (defvar org-plain-time-of-day-regexp) ; defined below
  14523. (defvar org-overriding-default-time nil) ; dynamically scoped
  14524. (defvar org-read-date-overlay nil)
  14525. (defvar org-dcst nil) ; dynamically scoped
  14526. (defvar org-read-date-history nil)
  14527. (defvar org-read-date-final-answer nil)
  14528. (defvar org-read-date-analyze-futurep nil)
  14529. (defvar org-read-date-analyze-forced-year nil)
  14530. (defvar org-read-date-inactive)
  14531. (defvar org-read-date-minibuffer-local-map
  14532. (let ((map (make-sparse-keymap)))
  14533. (set-keymap-parent map minibuffer-local-map)
  14534. (org-defkey map (kbd ".")
  14535. (lambda () (interactive)
  14536. (org-eval-in-calendar '(calendar-goto-today))))
  14537. (org-defkey map [(meta shift left)]
  14538. (lambda () (interactive)
  14539. (org-eval-in-calendar '(calendar-backward-month 1))))
  14540. (org-defkey map [(meta shift right)]
  14541. (lambda () (interactive)
  14542. (org-eval-in-calendar '(calendar-forward-month 1))))
  14543. (org-defkey map [(meta shift up)]
  14544. (lambda () (interactive)
  14545. (org-eval-in-calendar '(calendar-backward-year 1))))
  14546. (org-defkey map [(meta shift down)]
  14547. (lambda () (interactive)
  14548. (org-eval-in-calendar '(calendar-forward-year 1))))
  14549. (org-defkey map [?\e (shift left)]
  14550. (lambda () (interactive)
  14551. (org-eval-in-calendar '(calendar-backward-month 1))))
  14552. (org-defkey map [?\e (shift right)]
  14553. (lambda () (interactive)
  14554. (org-eval-in-calendar '(calendar-forward-month 1))))
  14555. (org-defkey map [?\e (shift up)]
  14556. (lambda () (interactive)
  14557. (org-eval-in-calendar '(calendar-backward-year 1))))
  14558. (org-defkey map [?\e (shift down)]
  14559. (lambda () (interactive)
  14560. (org-eval-in-calendar '(calendar-forward-year 1))))
  14561. (org-defkey map [(shift up)]
  14562. (lambda () (interactive)
  14563. (org-eval-in-calendar '(calendar-backward-week 1))))
  14564. (org-defkey map [(shift down)]
  14565. (lambda () (interactive)
  14566. (org-eval-in-calendar '(calendar-forward-week 1))))
  14567. (org-defkey map [(shift left)]
  14568. (lambda () (interactive)
  14569. (org-eval-in-calendar '(calendar-backward-day 1))))
  14570. (org-defkey map [(shift right)]
  14571. (lambda () (interactive)
  14572. (org-eval-in-calendar '(calendar-forward-day 1))))
  14573. (org-defkey map "!"
  14574. (lambda () (interactive)
  14575. (org-eval-in-calendar '(diary-view-entries))
  14576. (message "")))
  14577. (org-defkey map ">"
  14578. (lambda () (interactive)
  14579. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14580. (org-defkey map "<"
  14581. (lambda () (interactive)
  14582. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14583. (org-defkey map "\C-v"
  14584. (lambda () (interactive)
  14585. (org-eval-in-calendar
  14586. '(calendar-scroll-left-three-months 1))))
  14587. (org-defkey map "\M-v"
  14588. (lambda () (interactive)
  14589. (org-eval-in-calendar
  14590. '(calendar-scroll-right-three-months 1))))
  14591. map)
  14592. "Keymap for minibuffer commands when using `org-read-date'.")
  14593. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14594. default-time default-input inactive)
  14595. "Read a date, possibly a time, and make things smooth for the user.
  14596. The prompt will suggest to enter an ISO date, but you can also enter anything
  14597. which will at least partially be understood by `parse-time-string'.
  14598. Unrecognized parts of the date will default to the current day, month, year,
  14599. hour and minute. If this command is called to replace a timestamp at point,
  14600. or to enter the second timestamp of a range, the default time is taken
  14601. from the existing stamp. Furthermore, the command prefers the future,
  14602. so if you are giving a date where the year is not given, and the day-month
  14603. combination is already past in the current year, it will assume you
  14604. mean next year. For details, see the manual. A few examples:
  14605. 3-2-5 --> 2003-02-05
  14606. feb 15 --> currentyear-02-15
  14607. 2/15 --> currentyear-02-15
  14608. sep 12 9 --> 2009-09-12
  14609. 12:45 --> today 12:45
  14610. 22 sept 0:34 --> currentyear-09-22 0:34
  14611. 12 --> currentyear-currentmonth-12
  14612. Fri --> nearest Friday (today or later)
  14613. etc.
  14614. Furthermore you can specify a relative date by giving, as the *first* thing
  14615. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14616. change in days weeks, months, years.
  14617. With a single plus or minus, the date is relative to today. With a double
  14618. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14619. +4d --> four days from today
  14620. +4 --> same as above
  14621. +2w --> two weeks from today
  14622. ++5 --> five days from default date
  14623. The function understands only English month and weekday abbreviations.
  14624. While prompting, a calendar is popped up - you can also select the
  14625. date with the mouse (button 1). The calendar shows a period of three
  14626. months. To scroll it to other months, use the keys `>' and `<'.
  14627. If you don't like the calendar, turn it off with
  14628. \(setq org-read-date-popup-calendar nil)
  14629. With optional argument TO-TIME, the date will immediately be converted
  14630. to an internal time.
  14631. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14632. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14633. still enter a time, and this function will inform the calling routine
  14634. about this change. The calling routine may then choose to change the
  14635. format used to insert the time stamp into the buffer to include the time.
  14636. With optional argument FROM-STRING, read from this string instead from
  14637. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14638. the time/date that is used for everything that is not specified by the
  14639. user."
  14640. (require 'parse-time)
  14641. (let* ((org-time-stamp-rounding-minutes
  14642. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14643. (org-dcst org-display-custom-times)
  14644. (ct (org-current-time))
  14645. (org-def (or org-overriding-default-time default-time ct))
  14646. (org-defdecode (decode-time org-def))
  14647. (dummy (progn
  14648. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14649. (setcar (nthcdr 2 org-defdecode) -1)
  14650. (setcar (nthcdr 1 org-defdecode) 59)
  14651. (setq org-def (apply 'encode-time org-defdecode)
  14652. org-defdecode (decode-time org-def)))))
  14653. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14654. (calendar-frame-setup nil)
  14655. (calendar-setup nil)
  14656. (calendar-move-hook nil)
  14657. (calendar-view-diary-initially-flag nil)
  14658. (calendar-view-holidays-initially-flag nil)
  14659. (timestr (format-time-string
  14660. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14661. (prompt (concat (if prompt (concat prompt " ") "")
  14662. (format "Date+time [%s]: " timestr)))
  14663. ans (org-ans0 "") org-ans1 org-ans2 final)
  14664. (cond
  14665. (from-string (setq ans from-string))
  14666. (org-read-date-popup-calendar
  14667. (save-excursion
  14668. (save-window-excursion
  14669. (calendar)
  14670. (org-eval-in-calendar '(setq cursor-type nil) t)
  14671. (unwind-protect
  14672. (progn
  14673. (calendar-forward-day (- (time-to-days org-def)
  14674. (calendar-absolute-from-gregorian
  14675. (calendar-current-date))))
  14676. (org-eval-in-calendar nil t)
  14677. (let* ((old-map (current-local-map))
  14678. (map (copy-keymap calendar-mode-map))
  14679. (minibuffer-local-map
  14680. (copy-keymap org-read-date-minibuffer-local-map)))
  14681. (org-defkey map (kbd "RET") 'org-calendar-select)
  14682. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14683. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14684. (unwind-protect
  14685. (progn
  14686. (use-local-map map)
  14687. (setq org-read-date-inactive inactive)
  14688. (add-hook 'post-command-hook 'org-read-date-display)
  14689. (setq org-ans0 (read-string prompt default-input
  14690. 'org-read-date-history nil))
  14691. ;; org-ans0: from prompt
  14692. ;; org-ans1: from mouse click
  14693. ;; org-ans2: from calendar motion
  14694. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14695. (remove-hook 'post-command-hook 'org-read-date-display)
  14696. (use-local-map old-map)
  14697. (when org-read-date-overlay
  14698. (delete-overlay org-read-date-overlay)
  14699. (setq org-read-date-overlay nil)))))
  14700. (bury-buffer "*Calendar*")))))
  14701. (t ; Naked prompt only
  14702. (unwind-protect
  14703. (setq ans (read-string prompt default-input
  14704. 'org-read-date-history timestr))
  14705. (when org-read-date-overlay
  14706. (delete-overlay org-read-date-overlay)
  14707. (setq org-read-date-overlay nil)))))
  14708. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14709. (when org-read-date-analyze-forced-year
  14710. (message "Year was forced into %s"
  14711. (if org-read-date-force-compatible-dates
  14712. "compatible range (1970-2037)"
  14713. "range representable on this machine"))
  14714. (ding))
  14715. ;; One round trip to get rid of 34th of August and stuff like that....
  14716. (setq final (decode-time (apply 'encode-time final)))
  14717. (setq org-read-date-final-answer ans)
  14718. (if to-time
  14719. (apply 'encode-time final)
  14720. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14721. (format "%04d-%02d-%02d %02d:%02d"
  14722. (nth 5 final) (nth 4 final) (nth 3 final)
  14723. (nth 2 final) (nth 1 final))
  14724. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14725. (defvar org-def)
  14726. (defvar org-defdecode)
  14727. (defvar org-with-time)
  14728. (defun org-read-date-display ()
  14729. "Display the current date prompt interpretation in the minibuffer."
  14730. (when org-read-date-display-live
  14731. (when org-read-date-overlay
  14732. (delete-overlay org-read-date-overlay))
  14733. (when (minibufferp (current-buffer))
  14734. (save-excursion
  14735. (end-of-line 1)
  14736. (while (not (equal (buffer-substring
  14737. (max (point-min) (- (point) 4)) (point))
  14738. " "))
  14739. (insert " ")))
  14740. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14741. " " (or org-ans1 org-ans2)))
  14742. (org-end-time-was-given nil)
  14743. (f (org-read-date-analyze ans org-def org-defdecode))
  14744. (fmts (if org-dcst
  14745. org-time-stamp-custom-formats
  14746. org-time-stamp-formats))
  14747. (fmt (if (or org-with-time
  14748. (and (boundp 'org-time-was-given) org-time-was-given))
  14749. (cdr fmts)
  14750. (car fmts)))
  14751. (txt (format-time-string fmt (apply 'encode-time f)))
  14752. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14753. (txt (concat "=> " txt)))
  14754. (when (and org-end-time-was-given
  14755. (string-match org-plain-time-of-day-regexp txt))
  14756. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14757. org-end-time-was-given
  14758. (substring txt (match-end 0)))))
  14759. (when org-read-date-analyze-futurep
  14760. (setq txt (concat txt " (=>F)")))
  14761. (setq org-read-date-overlay
  14762. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14763. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14764. (defun org-read-date-analyze (ans org-def org-defdecode)
  14765. "Analyze the combined answer of the date prompt."
  14766. ;; FIXME: cleanup and comment
  14767. (let ((nowdecode (decode-time (current-time)))
  14768. delta deltan deltaw deltadef year month day
  14769. hour minute second wday pm h2 m2 tl wday1
  14770. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14771. (setq org-read-date-analyze-futurep nil
  14772. org-read-date-analyze-forced-year nil)
  14773. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14774. (setq ans "+0"))
  14775. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14776. (setq ans (replace-match "" t t ans)
  14777. deltan (car delta)
  14778. deltaw (nth 1 delta)
  14779. deltadef (nth 2 delta)))
  14780. ;; Check if there is an iso week date in there. If yes, store the
  14781. ;; info and postpone interpreting it until the rest of the parsing
  14782. ;; is done.
  14783. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14784. (setq iso-year (if (match-end 1)
  14785. (org-small-year-to-year
  14786. (string-to-number (match-string 1 ans))))
  14787. iso-weekday (if (match-end 3)
  14788. (string-to-number (match-string 3 ans)))
  14789. iso-week (string-to-number (match-string 2 ans)))
  14790. (setq ans (replace-match "" t t ans)))
  14791. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14792. (when (string-match
  14793. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14794. (setq year (if (match-end 2)
  14795. (string-to-number (match-string 2 ans))
  14796. (progn (setq kill-year t)
  14797. (string-to-number (format-time-string "%Y"))))
  14798. month (string-to-number (match-string 3 ans))
  14799. day (string-to-number (match-string 4 ans)))
  14800. (if (< year 100) (setq year (+ 2000 year)))
  14801. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14802. t nil ans)))
  14803. ;; Help matching dotted european dates
  14804. (when (string-match
  14805. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14806. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14807. (setq kill-year t)
  14808. (string-to-number (format-time-string "%Y")))
  14809. day (string-to-number (match-string 1 ans))
  14810. month (string-to-number (match-string 2 ans))
  14811. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14812. t nil ans)))
  14813. ;; Help matching american dates, like 5/30 or 5/30/7
  14814. (when (string-match
  14815. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14816. (setq year (if (match-end 4)
  14817. (string-to-number (match-string 4 ans))
  14818. (progn (setq kill-year t)
  14819. (string-to-number (format-time-string "%Y"))))
  14820. month (string-to-number (match-string 1 ans))
  14821. day (string-to-number (match-string 2 ans)))
  14822. (if (< year 100) (setq year (+ 2000 year)))
  14823. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14824. t nil ans)))
  14825. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14826. ;; If there is a time with am/pm, and *no* time without it, we convert
  14827. ;; so that matching will be successful.
  14828. (loop for i from 1 to 2 do ; twice, for end time as well
  14829. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14830. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14831. (setq hour (string-to-number (match-string 1 ans))
  14832. minute (if (match-end 3)
  14833. (string-to-number (match-string 3 ans))
  14834. 0)
  14835. pm (equal ?p
  14836. (string-to-char (downcase (match-string 4 ans)))))
  14837. (if (and (= hour 12) (not pm))
  14838. (setq hour 0)
  14839. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14840. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14841. t t ans))))
  14842. ;; Check if a time range is given as a duration
  14843. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14844. (setq hour (string-to-number (match-string 1 ans))
  14845. h2 (+ hour (string-to-number (match-string 3 ans)))
  14846. minute (string-to-number (match-string 2 ans))
  14847. m2 (+ minute (if (match-end 5) (string-to-number
  14848. (match-string 5 ans))0)))
  14849. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14850. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14851. t t ans)))
  14852. ;; Check if there is a time range
  14853. (when (boundp 'org-end-time-was-given)
  14854. (setq org-time-was-given nil)
  14855. (when (and (string-match org-plain-time-of-day-regexp ans)
  14856. (match-end 8))
  14857. (setq org-end-time-was-given (match-string 8 ans))
  14858. (setq ans (concat (substring ans 0 (match-beginning 7))
  14859. (substring ans (match-end 7))))))
  14860. (setq tl (parse-time-string ans)
  14861. day (or (nth 3 tl) (nth 3 org-defdecode))
  14862. month (or (nth 4 tl)
  14863. (if (and org-read-date-prefer-future
  14864. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14865. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14866. (nth 4 org-defdecode)))
  14867. year (or (and (not kill-year) (nth 5 tl))
  14868. (if (and org-read-date-prefer-future
  14869. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14870. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14871. (nth 5 org-defdecode)))
  14872. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14873. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14874. second (or (nth 0 tl) 0)
  14875. wday (nth 6 tl))
  14876. (when (and (eq org-read-date-prefer-future 'time)
  14877. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14878. (equal day (nth 3 nowdecode))
  14879. (equal month (nth 4 nowdecode))
  14880. (equal year (nth 5 nowdecode))
  14881. (nth 2 tl)
  14882. (or (< (nth 2 tl) (nth 2 nowdecode))
  14883. (and (= (nth 2 tl) (nth 2 nowdecode))
  14884. (nth 1 tl)
  14885. (< (nth 1 tl) (nth 1 nowdecode)))))
  14886. (setq day (1+ day)
  14887. futurep t))
  14888. ;; Special date definitions below
  14889. (cond
  14890. (iso-week
  14891. ;; There was an iso week
  14892. (require 'cal-iso)
  14893. (setq futurep nil)
  14894. (setq year (or iso-year year)
  14895. day (or iso-weekday wday 1)
  14896. wday nil ; to make sure that the trigger below does not match
  14897. iso-date (calendar-gregorian-from-absolute
  14898. (calendar-absolute-from-iso
  14899. (list iso-week day year))))
  14900. ; FIXME: Should we also push ISO weeks into the future?
  14901. ; (when (and org-read-date-prefer-future
  14902. ; (not iso-year)
  14903. ; (< (calendar-absolute-from-gregorian iso-date)
  14904. ; (time-to-days (current-time))))
  14905. ; (setq year (1+ year)
  14906. ; iso-date (calendar-gregorian-from-absolute
  14907. ; (calendar-absolute-from-iso
  14908. ; (list iso-week day year)))))
  14909. (setq month (car iso-date)
  14910. year (nth 2 iso-date)
  14911. day (nth 1 iso-date)))
  14912. (deltan
  14913. (setq futurep nil)
  14914. (unless deltadef
  14915. (let ((now (decode-time (current-time))))
  14916. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14917. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14918. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14919. ((equal deltaw "m") (setq month (+ month deltan)))
  14920. ((equal deltaw "y") (setq year (+ year deltan)))))
  14921. ((and wday (not (nth 3 tl)))
  14922. ;; Weekday was given, but no day, so pick that day in the week
  14923. ;; on or after the derived date.
  14924. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14925. (unless (equal wday wday1)
  14926. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14927. (if (and (boundp 'org-time-was-given)
  14928. (nth 2 tl))
  14929. (setq org-time-was-given t))
  14930. (if (< year 100) (setq year (+ 2000 year)))
  14931. ;; Check of the date is representable
  14932. (if org-read-date-force-compatible-dates
  14933. (progn
  14934. (if (< year 1970)
  14935. (setq year 1970 org-read-date-analyze-forced-year t))
  14936. (if (> year 2037)
  14937. (setq year 2037 org-read-date-analyze-forced-year t)))
  14938. (condition-case nil
  14939. (ignore (encode-time second minute hour day month year))
  14940. (error
  14941. (setq year (nth 5 org-defdecode))
  14942. (setq org-read-date-analyze-forced-year t))))
  14943. (setq org-read-date-analyze-futurep futurep)
  14944. (list second minute hour day month year)))
  14945. (defvar parse-time-weekdays)
  14946. (defun org-read-date-get-relative (s today default)
  14947. "Check string S for special relative date string.
  14948. TODAY and DEFAULT are internal times, for today and for a default.
  14949. Return shift list (N what def-flag)
  14950. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14951. N is the number of WHATs to shift.
  14952. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14953. the DEFAULT date rather than TODAY."
  14954. (require 'parse-time)
  14955. (when (and
  14956. (string-match
  14957. (concat
  14958. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14959. "\\([0-9]+\\)?"
  14960. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14961. "\\([ \t]\\|$\\)") s)
  14962. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14963. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14964. (string-to-char (substring (match-string 1 s) -1))
  14965. ?+))
  14966. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14967. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14968. (what (if (match-end 3) (match-string 3 s) "d"))
  14969. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14970. (date (if rel default today))
  14971. (wday (nth 6 (decode-time date)))
  14972. delta)
  14973. (if wday1
  14974. (progn
  14975. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14976. (if (= dir ?-) (setq delta (- delta 7)))
  14977. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14978. (list delta "d" rel))
  14979. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14980. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14981. "Turn a user-specified date into the internal representation.
  14982. The internal representation needed by the calendar is (month day year).
  14983. This is a wrapper to handle the brain-dead convention in calendar that
  14984. user function argument order change dependent on argument order."
  14985. (if (boundp 'calendar-date-style)
  14986. (cond
  14987. ((eq calendar-date-style 'american)
  14988. (list arg1 arg2 arg3))
  14989. ((eq calendar-date-style 'european)
  14990. (list arg2 arg1 arg3))
  14991. ((eq calendar-date-style 'iso)
  14992. (list arg2 arg3 arg1)))
  14993. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14994. (if (org-bound-and-true-p european-calendar-style)
  14995. (list arg2 arg1 arg3)
  14996. (list arg1 arg2 arg3)))))
  14997. (defun org-eval-in-calendar (form &optional keepdate)
  14998. "Eval FORM in the calendar window and return to current window.
  14999. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15000. otherwise stick to the current value of `org-ans2'."
  15001. (let ((sf (selected-frame))
  15002. (sw (selected-window)))
  15003. (select-window (get-buffer-window "*Calendar*" t))
  15004. (eval form)
  15005. (when (and (not keepdate) (calendar-cursor-to-date))
  15006. (let* ((date (calendar-cursor-to-date))
  15007. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15008. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15009. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15010. (select-window sw)
  15011. (org-select-frame-set-input-focus sf)))
  15012. (defun org-calendar-select ()
  15013. "Return to `org-read-date' with the date currently selected.
  15014. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15015. (interactive)
  15016. (when (calendar-cursor-to-date)
  15017. (let* ((date (calendar-cursor-to-date))
  15018. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15019. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15020. (if (active-minibuffer-window) (exit-minibuffer))))
  15021. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15022. "Insert a date stamp for the date given by the internal TIME.
  15023. WITH-HM means use the stamp format that includes the time of the day.
  15024. INACTIVE means use square brackets instead of angular ones, so that the
  15025. stamp will not contribute to the agenda.
  15026. PRE and POST are optional strings to be inserted before and after the
  15027. stamp.
  15028. The command returns the inserted time stamp."
  15029. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15030. stamp)
  15031. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15032. (insert-before-markers (or pre ""))
  15033. (when (listp extra)
  15034. (setq extra (car extra))
  15035. (if (and (stringp extra)
  15036. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15037. (setq extra (format "-%02d:%02d"
  15038. (string-to-number (match-string 1 extra))
  15039. (string-to-number (match-string 2 extra))))
  15040. (setq extra nil)))
  15041. (when extra
  15042. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15043. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15044. (insert-before-markers (or post ""))
  15045. (setq org-last-inserted-timestamp stamp)))
  15046. (defun org-toggle-time-stamp-overlays ()
  15047. "Toggle the use of custom time stamp formats."
  15048. (interactive)
  15049. (setq org-display-custom-times (not org-display-custom-times))
  15050. (unless org-display-custom-times
  15051. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15052. (while (setq p (next-single-property-change p 'display))
  15053. (if (and (get-text-property p 'display)
  15054. (eq (get-text-property p 'face) 'org-date))
  15055. (remove-text-properties
  15056. p (setq p (next-single-property-change p 'display))
  15057. '(display t))))
  15058. (set-buffer-modified-p bmp)))
  15059. (if (featurep 'xemacs)
  15060. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15061. (org-restart-font-lock)
  15062. (setq org-table-may-need-update t)
  15063. (if org-display-custom-times
  15064. (message "Time stamps are overlaid with custom format")
  15065. (message "Time stamp overlays removed")))
  15066. (defun org-display-custom-time (beg end)
  15067. "Overlay modified time stamp format over timestamp between BEG and END."
  15068. (let* ((ts (buffer-substring beg end))
  15069. t1 w1 with-hm tf time str w2 (off 0))
  15070. (save-match-data
  15071. (setq t1 (org-parse-time-string ts t))
  15072. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15073. (setq off (- (match-end 0) (match-beginning 0)))))
  15074. (setq end (- end off))
  15075. (setq w1 (- end beg)
  15076. with-hm (and (nth 1 t1) (nth 2 t1))
  15077. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15078. time (org-fix-decoded-time t1)
  15079. str (org-add-props
  15080. (format-time-string
  15081. (substring tf 1 -1) (apply 'encode-time time))
  15082. nil 'mouse-face 'highlight)
  15083. w2 (length str))
  15084. (if (not (= w2 w1))
  15085. (add-text-properties (1+ beg) (+ 2 beg)
  15086. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15087. (if (featurep 'xemacs)
  15088. (progn
  15089. (put-text-property beg end 'invisible t)
  15090. (put-text-property beg end 'end-glyph (make-glyph str)))
  15091. (put-text-property beg end 'display str))))
  15092. (defun org-translate-time (string)
  15093. "Translate all timestamps in STRING to custom format.
  15094. But do this only if the variable `org-display-custom-times' is set."
  15095. (when org-display-custom-times
  15096. (save-match-data
  15097. (let* ((start 0)
  15098. (re org-ts-regexp-both)
  15099. t1 with-hm inactive tf time str beg end)
  15100. (while (setq start (string-match re string start))
  15101. (setq beg (match-beginning 0)
  15102. end (match-end 0)
  15103. t1 (save-match-data
  15104. (org-parse-time-string (substring string beg end) t))
  15105. with-hm (and (nth 1 t1) (nth 2 t1))
  15106. inactive (equal (substring string beg (1+ beg)) "[")
  15107. tf (funcall (if with-hm 'cdr 'car)
  15108. org-time-stamp-custom-formats)
  15109. time (org-fix-decoded-time t1)
  15110. str (format-time-string
  15111. (concat
  15112. (if inactive "[" "<") (substring tf 1 -1)
  15113. (if inactive "]" ">"))
  15114. (apply 'encode-time time))
  15115. string (replace-match str t t string)
  15116. start (+ start (length str)))))))
  15117. string)
  15118. (defun org-fix-decoded-time (time)
  15119. "Set 0 instead of nil for the first 6 elements of time.
  15120. Don't touch the rest."
  15121. (let ((n 0))
  15122. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15123. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15124. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15125. "Difference between TIMESTAMP-STRING and now in days.
  15126. If SECONDS is non-nil, return the difference in seconds."
  15127. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15128. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15129. (funcall fdiff (current-time)))))
  15130. (defun org-deadline-close (timestamp-string &optional ndays)
  15131. "Is the time in TIMESTAMP-STRING close to the current date?"
  15132. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15133. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15134. (not (org-entry-is-done-p))))
  15135. (defun org-get-wdays (ts &optional delay zero-delay)
  15136. "Get the deadline lead time appropriate for timestring TS.
  15137. When DELAY is non-nil, get the delay time for scheduled items
  15138. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15139. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15140. don't try to find the delay cookie in the scheduled timestamp."
  15141. (let ((tv (if delay org-scheduled-delay-days
  15142. org-deadline-warning-days)))
  15143. (cond
  15144. ((or (and delay (< tv 0))
  15145. (and delay zero-delay (<= tv 0))
  15146. (and (not delay) (<= tv 0)))
  15147. ;; Enforce this value no matter what
  15148. (- tv))
  15149. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15150. ;; lead time is specified.
  15151. (floor (* (string-to-number (match-string 1 ts))
  15152. (cdr (assoc (match-string 2 ts)
  15153. '(("d" . 1) ("w" . 7)
  15154. ("m" . 30.4) ("y" . 365.25)
  15155. ("h" . 0.041667)))))))
  15156. ;; go for the default.
  15157. (t tv))))
  15158. (defun org-calendar-select-mouse (ev)
  15159. "Return to `org-read-date' with the date currently selected.
  15160. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15161. (interactive "e")
  15162. (mouse-set-point ev)
  15163. (when (calendar-cursor-to-date)
  15164. (let* ((date (calendar-cursor-to-date))
  15165. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15166. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15167. (if (active-minibuffer-window) (exit-minibuffer))))
  15168. (defun org-check-deadlines (ndays)
  15169. "Check if there are any deadlines due or past due.
  15170. A deadline is considered due if it happens within `org-deadline-warning-days'
  15171. days from today's date. If the deadline appears in an entry marked DONE,
  15172. it is not shown. The prefix arg NDAYS can be used to test that many
  15173. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15174. (interactive "P")
  15175. (let* ((org-warn-days
  15176. (cond
  15177. ((equal ndays '(4)) 100000)
  15178. (ndays (prefix-numeric-value ndays))
  15179. (t (abs org-deadline-warning-days))))
  15180. (case-fold-search nil)
  15181. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15182. (callback
  15183. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15184. (message "%d deadlines past-due or due within %d days"
  15185. (org-occur regexp nil callback)
  15186. org-warn-days)))
  15187. (defsubst org-re-timestamp (type)
  15188. "Return a regexp for timestamp TYPE.
  15189. Allowed values for TYPE are:
  15190. all: all timestamps
  15191. active: only active timestamps (<...>)
  15192. inactive: only inactive timestamps ([...])
  15193. scheduled: only scheduled timestamps
  15194. deadline: only deadline timestamps
  15195. When TYPE is nil, fall back on returning a regexp that matches
  15196. both scheduled and deadline timestamps."
  15197. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15198. ((eq type 'active) org-ts-regexp)
  15199. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15200. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15201. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15202. ((eq type 'scheduled-or-deadline)
  15203. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15204. (defun org-check-before-date (date)
  15205. "Check if there are deadlines or scheduled entries before DATE."
  15206. (interactive (list (org-read-date)))
  15207. (let ((case-fold-search nil)
  15208. (regexp (org-re-timestamp org-ts-type))
  15209. (callback
  15210. (lambda () (time-less-p
  15211. (org-time-string-to-time (match-string 1))
  15212. (org-time-string-to-time date)))))
  15213. (message "%d entries before %s"
  15214. (org-occur regexp nil callback) date)))
  15215. (defun org-check-after-date (date)
  15216. "Check if there are deadlines or scheduled entries after DATE."
  15217. (interactive (list (org-read-date)))
  15218. (let ((case-fold-search nil)
  15219. (regexp (org-re-timestamp org-ts-type))
  15220. (callback
  15221. (lambda () (not
  15222. (time-less-p
  15223. (org-time-string-to-time (match-string 1))
  15224. (org-time-string-to-time date))))))
  15225. (message "%d entries after %s"
  15226. (org-occur regexp nil callback) date)))
  15227. (defun org-check-dates-range (start-date end-date)
  15228. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15229. (interactive (list (org-read-date nil nil nil "Range starts")
  15230. (org-read-date nil nil nil "Range end")))
  15231. (let ((case-fold-search nil)
  15232. (regexp (org-re-timestamp org-ts-type))
  15233. (callback
  15234. (lambda ()
  15235. (let ((match (match-string 1)))
  15236. (and
  15237. (not (time-less-p
  15238. (org-time-string-to-time match)
  15239. (org-time-string-to-time start-date)))
  15240. (time-less-p
  15241. (org-time-string-to-time match)
  15242. (org-time-string-to-time end-date)))))))
  15243. (message "%d entries between %s and %s"
  15244. (org-occur regexp nil callback) start-date end-date)))
  15245. (defun org-evaluate-time-range (&optional to-buffer)
  15246. "Evaluate a time range by computing the difference between start and end.
  15247. Normally the result is just printed in the echo area, but with prefix arg
  15248. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15249. If the time range is actually in a table, the result is inserted into the
  15250. next column.
  15251. For time difference computation, a year is assumed to be exactly 365
  15252. days in order to avoid rounding problems."
  15253. (interactive "P")
  15254. (or
  15255. (org-clock-update-time-maybe)
  15256. (save-excursion
  15257. (unless (org-at-date-range-p t)
  15258. (goto-char (point-at-bol))
  15259. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15260. (if (not (org-at-date-range-p t))
  15261. (error "Not at a time-stamp range, and none found in current line")))
  15262. (let* ((ts1 (match-string 1))
  15263. (ts2 (match-string 2))
  15264. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15265. (match-end (match-end 0))
  15266. (time1 (org-time-string-to-time ts1))
  15267. (time2 (org-time-string-to-time ts2))
  15268. (t1 (org-float-time time1))
  15269. (t2 (org-float-time time2))
  15270. (diff (abs (- t2 t1)))
  15271. (negative (< (- t2 t1) 0))
  15272. ;; (ys (floor (* 365 24 60 60)))
  15273. (ds (* 24 60 60))
  15274. (hs (* 60 60))
  15275. (fy "%dy %dd %02d:%02d")
  15276. (fy1 "%dy %dd")
  15277. (fd "%dd %02d:%02d")
  15278. (fd1 "%dd")
  15279. (fh "%02d:%02d")
  15280. y d h m align)
  15281. (if havetime
  15282. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15283. y 0
  15284. d (floor (/ diff ds)) diff (mod diff ds)
  15285. h (floor (/ diff hs)) diff (mod diff hs)
  15286. m (floor (/ diff 60)))
  15287. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15288. y 0
  15289. d (floor (+ (/ diff ds) 0.5))
  15290. h 0 m 0))
  15291. (if (not to-buffer)
  15292. (message "%s" (org-make-tdiff-string y d h m))
  15293. (if (org-at-table-p)
  15294. (progn
  15295. (goto-char match-end)
  15296. (setq align t)
  15297. (and (looking-at " *|") (goto-char (match-end 0))))
  15298. (goto-char match-end))
  15299. (if (looking-at
  15300. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15301. (replace-match ""))
  15302. (if negative (insert " -"))
  15303. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15304. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15305. (insert " " (format fh h m))))
  15306. (if align (org-table-align))
  15307. (message "Time difference inserted")))))
  15308. (defun org-make-tdiff-string (y d h m)
  15309. (let ((fmt "")
  15310. (l nil))
  15311. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15312. l (push y l)))
  15313. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15314. l (push d l)))
  15315. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15316. l (push h l)))
  15317. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15318. l (push m l)))
  15319. (apply 'format fmt (nreverse l))))
  15320. (defun org-time-string-to-time (s &optional buffer pos)
  15321. "Convert a timestamp string into internal time."
  15322. (condition-case errdata
  15323. (apply 'encode-time (org-parse-time-string s))
  15324. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15325. s (if (not (and buffer pos))
  15326. ""
  15327. (format " at %d in buffer `%s'" pos buffer))
  15328. (cdr errdata)))))
  15329. (defun org-time-string-to-seconds (s)
  15330. "Convert a timestamp string to a number of seconds."
  15331. (org-float-time (org-time-string-to-time s)))
  15332. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15333. "Convert a time stamp to an absolute day number.
  15334. If there is a specifier for a cyclic time stamp, get the closest
  15335. date to DAYNR.
  15336. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15337. The variable `date' is bound by the calendar when this is called."
  15338. (cond
  15339. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15340. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15341. daynr
  15342. (+ daynr 1000)))
  15343. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15344. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15345. (time-to-days (current-time))) (match-string 0 s)
  15346. prefer show-all))
  15347. (t (time-to-days
  15348. (condition-case errdata
  15349. (apply 'encode-time (org-parse-time-string s))
  15350. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15351. s (if (not (and buffer pos))
  15352. ""
  15353. (format " at %d in buffer `%s'" pos buffer))
  15354. (cdr errdata))))))))
  15355. (defun org-days-to-iso-week (days)
  15356. "Return the iso week number."
  15357. (require 'cal-iso)
  15358. (car (calendar-iso-from-absolute days)))
  15359. (defun org-small-year-to-year (year)
  15360. "Convert 2-digit years into 4-digit years.
  15361. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  15362. The year 2000 cannot be abbreviated. Any year larger than 99
  15363. is returned unchanged."
  15364. (if (< year 38)
  15365. (setq year (+ 2000 year))
  15366. (if (< year 100)
  15367. (setq year (+ 1900 year))))
  15368. year)
  15369. (defun org-time-from-absolute (d)
  15370. "Return the time corresponding to date D.
  15371. D may be an absolute day number, or a calendar-type list (month day year)."
  15372. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15373. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15374. (defun org-calendar-holiday ()
  15375. "List of holidays, for Diary display in Org-mode."
  15376. (require 'holidays)
  15377. (let ((hl (funcall
  15378. (if (fboundp 'calendar-check-holidays)
  15379. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15380. (if hl (mapconcat 'identity hl "; "))))
  15381. (defun org-diary-sexp-entry (sexp entry date)
  15382. "Process a SEXP diary ENTRY for DATE."
  15383. (require 'diary-lib)
  15384. (let ((result (if calendar-debug-sexp
  15385. (let ((stack-trace-on-error t))
  15386. (eval (car (read-from-string sexp))))
  15387. (condition-case nil
  15388. (eval (car (read-from-string sexp)))
  15389. (error
  15390. (beep)
  15391. (message "Bad sexp at line %d in %s: %s"
  15392. (org-current-line)
  15393. (buffer-file-name) sexp)
  15394. (sleep-for 2))))))
  15395. (cond ((stringp result) (split-string result "; "))
  15396. ((and (consp result)
  15397. (not (consp (cdr result)))
  15398. (stringp (cdr result))) (cdr result))
  15399. ((and (consp result)
  15400. (stringp (car result))) result)
  15401. (result entry))))
  15402. (defun org-diary-to-ical-string (frombuf)
  15403. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15404. This uses the icalendar.el library."
  15405. (let* ((tmpdir (if (featurep 'xemacs)
  15406. (temp-directory)
  15407. temporary-file-directory))
  15408. (tmpfile (make-temp-name
  15409. (expand-file-name "orgics" tmpdir)))
  15410. buf rtn b e)
  15411. (with-current-buffer frombuf
  15412. (icalendar-export-region (point-min) (point-max) tmpfile)
  15413. (setq buf (find-buffer-visiting tmpfile))
  15414. (set-buffer buf)
  15415. (goto-char (point-min))
  15416. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15417. (setq b (match-beginning 0)))
  15418. (goto-char (point-max))
  15419. (if (re-search-backward "^END:VEVENT" nil t)
  15420. (setq e (match-end 0)))
  15421. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15422. (kill-buffer buf)
  15423. (delete-file tmpfile)
  15424. rtn))
  15425. (defun org-closest-date (start current change prefer show-all)
  15426. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15427. When PREFER is `past', return a date that is either CURRENT or past.
  15428. When PREFER is `future', return a date that is either CURRENT or future.
  15429. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15430. ;; Make the proper lists from the dates
  15431. (catch 'exit
  15432. (let ((a1 '(("h" . hour)
  15433. ("d" . day)
  15434. ("w" . week)
  15435. ("m" . month)
  15436. ("y" . year)))
  15437. (shour (nth 2 (org-parse-time-string start)))
  15438. dn dw sday cday n1 n2 n0
  15439. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15440. (setq start (org-date-to-gregorian start)
  15441. current (org-date-to-gregorian
  15442. (if show-all
  15443. current
  15444. (time-to-days (current-time))))
  15445. sday (calendar-absolute-from-gregorian start)
  15446. cday (calendar-absolute-from-gregorian current))
  15447. (if (<= cday sday) (throw 'exit sday))
  15448. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15449. (setq dn (string-to-number (match-string 1 change))
  15450. dw (cdr (assoc (match-string 2 change) a1)))
  15451. (error "Invalid change specifier: %s" change))
  15452. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15453. (cond
  15454. ((eq dw 'hour)
  15455. (let ((missing-hours
  15456. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15457. dn)))
  15458. (setq n1 (if (zerop missing-hours) cday
  15459. (- cday (1+ (floor (/ missing-hours 24)))))
  15460. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15461. ((eq dw 'day)
  15462. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15463. n2 (+ n1 dn)))
  15464. ((eq dw 'year)
  15465. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15466. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15467. (setq date1 (list m d y1)
  15468. n1 (calendar-absolute-from-gregorian date1)
  15469. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15470. n2 (calendar-absolute-from-gregorian date2)))
  15471. ((eq dw 'month)
  15472. ;; approx number of month between the two dates
  15473. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15474. ;; How often does dn fit in there?
  15475. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15476. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15477. m (+ m nm)
  15478. ny (floor (/ m 12))
  15479. y (+ y ny)
  15480. m (- m (* ny 12)))
  15481. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15482. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15483. (setq m2 (+ m dn) y2 y)
  15484. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15485. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15486. (while (<= n2 cday)
  15487. (setq n1 n2 m m2 y y2)
  15488. (setq m2 (+ m dn) y2 y)
  15489. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15490. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15491. ;; Make sure n1 is the earlier date
  15492. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15493. (if show-all
  15494. (cond
  15495. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15496. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15497. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15498. (cond
  15499. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15500. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15501. (t (if (= cday n1) n1 n2)))))))
  15502. (defun org-date-to-gregorian (date)
  15503. "Turn any specification of DATE into a Gregorian date for the calendar."
  15504. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15505. ((and (listp date) (= (length date) 3)) date)
  15506. ((stringp date)
  15507. (setq date (org-parse-time-string date))
  15508. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15509. ((listp date)
  15510. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15511. (defun org-parse-time-string (s &optional nodefault)
  15512. "Parse the standard Org-mode time string.
  15513. This should be a lot faster than the normal `parse-time-string'.
  15514. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15515. hour and minute fields will be nil if not given."
  15516. (cond ((string-match org-ts-regexp0 s)
  15517. (list 0
  15518. (if (or (match-beginning 8) (not nodefault))
  15519. (string-to-number (or (match-string 8 s) "0")))
  15520. (if (or (match-beginning 7) (not nodefault))
  15521. (string-to-number (or (match-string 7 s) "0")))
  15522. (string-to-number (match-string 4 s))
  15523. (string-to-number (match-string 3 s))
  15524. (string-to-number (match-string 2 s))
  15525. nil nil nil))
  15526. ((string-match "^<[^>]+>$" s)
  15527. (decode-time (seconds-to-time (org-matcher-time s))))
  15528. (t (error "Not a standard Org-mode time string: %s" s))))
  15529. (defun org-timestamp-up (&optional arg)
  15530. "Increase the date item at the cursor by one.
  15531. If the cursor is on the year, change the year. If it is on the month,
  15532. the day or the time, change that.
  15533. With prefix ARG, change by that many units."
  15534. (interactive "p")
  15535. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15536. (defun org-timestamp-down (&optional arg)
  15537. "Decrease the date item at the cursor by one.
  15538. If the cursor is on the year, change the year. If it is on the month,
  15539. the day or the time, change that.
  15540. With prefix ARG, change by that many units."
  15541. (interactive "p")
  15542. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15543. (defun org-timestamp-up-day (&optional arg)
  15544. "Increase the date in the time stamp by one day.
  15545. With prefix ARG, change that many days."
  15546. (interactive "p")
  15547. (if (and (not (org-at-timestamp-p t))
  15548. (org-at-heading-p))
  15549. (org-todo 'up)
  15550. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15551. (defun org-timestamp-down-day (&optional arg)
  15552. "Decrease the date in the time stamp by one day.
  15553. With prefix ARG, change that many days."
  15554. (interactive "p")
  15555. (if (and (not (org-at-timestamp-p t))
  15556. (org-at-heading-p))
  15557. (org-todo 'down)
  15558. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15559. (defun org-at-timestamp-p (&optional inactive-ok)
  15560. "Determine if the cursor is in or at a timestamp."
  15561. (interactive)
  15562. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15563. (pos (point))
  15564. (ans (or (looking-at tsr)
  15565. (save-excursion
  15566. (skip-chars-backward "^[<\n\r\t")
  15567. (if (> (point) (point-min)) (backward-char 1))
  15568. (and (looking-at tsr)
  15569. (> (- (match-end 0) pos) -1))))))
  15570. (and ans
  15571. (boundp 'org-ts-what)
  15572. (setq org-ts-what
  15573. (cond
  15574. ((= pos (match-beginning 0)) 'bracket)
  15575. ;; Point is considered to be "on the bracket" whether
  15576. ;; it's really on it or right after it.
  15577. ((= pos (1- (match-end 0))) 'bracket)
  15578. ((= pos (match-end 0)) 'after)
  15579. ((org-pos-in-match-range pos 2) 'year)
  15580. ((org-pos-in-match-range pos 3) 'month)
  15581. ((org-pos-in-match-range pos 7) 'hour)
  15582. ((org-pos-in-match-range pos 8) 'minute)
  15583. ((or (org-pos-in-match-range pos 4)
  15584. (org-pos-in-match-range pos 5)) 'day)
  15585. ((and (> pos (or (match-end 8) (match-end 5)))
  15586. (< pos (match-end 0)))
  15587. (- pos (or (match-end 8) (match-end 5))))
  15588. (t 'day))))
  15589. ans))
  15590. (defun org-toggle-timestamp-type ()
  15591. "Toggle the type (<active> or [inactive]) of a time stamp."
  15592. (interactive)
  15593. (when (org-at-timestamp-p t)
  15594. (let ((beg (match-beginning 0)) (end (match-end 0))
  15595. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15596. (save-excursion
  15597. (goto-char beg)
  15598. (while (re-search-forward "[][<>]" end t)
  15599. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15600. t t)))
  15601. (message "Timestamp is now %sactive"
  15602. (if (equal (char-after beg) ?<) "" "in")))))
  15603. (defun org-at-clock-log-p nil
  15604. "Is the cursor on the clock log line?"
  15605. (save-excursion
  15606. (move-beginning-of-line 1)
  15607. (looking-at "^[ \t]*CLOCK:")))
  15608. (defvar org-clock-history) ; defined in org-clock.el
  15609. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15610. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15611. "Change the date in the time stamp at point.
  15612. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15613. `year', `minute', `second'. If WHAT is not given, the cursor position
  15614. in the timestamp determines what will be changed.
  15615. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15616. (let ((origin (point)) origin-cat
  15617. with-hm inactive
  15618. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15619. org-ts-what
  15620. extra rem
  15621. ts time time0 fixnext clrgx)
  15622. (if (not (org-at-timestamp-p t))
  15623. (error "Not at a timestamp"))
  15624. (if (and (not what) (eq org-ts-what 'bracket))
  15625. (org-toggle-timestamp-type)
  15626. ;; Point isn't on brackets. Remember the part of the time-stamp
  15627. ;; the point was in. Indeed, size of time-stamps may change,
  15628. ;; but point must be kept in the same category nonetheless.
  15629. (setq origin-cat org-ts-what)
  15630. (if (and (not what) (not (eq org-ts-what 'day))
  15631. org-display-custom-times
  15632. (get-text-property (point) 'display)
  15633. (not (get-text-property (1- (point)) 'display)))
  15634. (setq org-ts-what 'day))
  15635. (setq org-ts-what (or what org-ts-what)
  15636. inactive (= (char-after (match-beginning 0)) ?\[)
  15637. ts (match-string 0))
  15638. (replace-match "")
  15639. (when (string-match
  15640. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15641. ts)
  15642. (setq extra (match-string 1 ts))
  15643. (if suppress-tmp-delay
  15644. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15645. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15646. (setq with-hm t))
  15647. (setq time0 (org-parse-time-string ts))
  15648. (when (and updown
  15649. (eq org-ts-what 'minute)
  15650. (not current-prefix-arg))
  15651. ;; This looks like s-up and s-down. Change by one rounding step.
  15652. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15653. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15654. (setcar (cdr time0) (+ (nth 1 time0)
  15655. (if (> n 0) (- rem) (- dm rem))))))
  15656. (setq time
  15657. (encode-time (or (car time0) 0)
  15658. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15659. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15660. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15661. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15662. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15663. (nthcdr 6 time0)))
  15664. (when (and (member org-ts-what '(hour minute))
  15665. extra
  15666. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15667. (setq extra (org-modify-ts-extra
  15668. extra
  15669. (if (eq org-ts-what 'hour) 2 5)
  15670. n dm)))
  15671. (when (integerp org-ts-what)
  15672. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15673. (if (eq what 'calendar)
  15674. (let ((cal-date (org-get-date-from-calendar)))
  15675. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15676. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15677. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15678. (setcar time0 (or (car time0) 0))
  15679. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15680. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15681. (setq time (apply 'encode-time time0))))
  15682. ;; Insert the new time-stamp, and ensure point stays in the same
  15683. ;; category as before (i.e. not after the last position in that
  15684. ;; category).
  15685. (let ((pos (point)))
  15686. ;; Stay before inserted string. `save-excursion' is of no use.
  15687. (setq org-last-changed-timestamp
  15688. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15689. (goto-char pos))
  15690. (save-match-data
  15691. (looking-at org-ts-regexp3)
  15692. (goto-char (cond
  15693. ;; `day' category ends before `hour' if any, or at
  15694. ;; the end of the day name.
  15695. ((eq origin-cat 'day)
  15696. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15697. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15698. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15699. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15700. ;; `year' and `month' have both fixed size: point
  15701. ;; couldn't have moved into another part.
  15702. (t origin))))
  15703. ;; Update clock if on a CLOCK line.
  15704. (org-clock-update-time-maybe)
  15705. ;; Maybe adjust the closest clock in `org-clock-history'
  15706. (when org-clock-adjust-closest
  15707. (if (not (and (org-at-clock-log-p)
  15708. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15709. org-clock-history))))))
  15710. (message "No clock to adjust")
  15711. (cond ((save-excursion ; fix previous clock?
  15712. (re-search-backward org-ts-regexp0 nil t)
  15713. (org-looking-back (concat org-clock-string " \\[")))
  15714. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15715. ((save-excursion ; fix next clock?
  15716. (re-search-backward org-ts-regexp0 nil t)
  15717. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15718. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15719. (save-window-excursion
  15720. ;; Find closest clock to point, adjust the previous/next one in history
  15721. (let* ((p (save-excursion (org-back-to-heading t)))
  15722. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15723. (clfixnth
  15724. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15725. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15726. (if (not clfixpos)
  15727. (message "No clock to adjust")
  15728. (save-excursion
  15729. (org-goto-marker-or-bmk clfixpos)
  15730. (org-show-subtree)
  15731. (when (re-search-forward clrgx nil t)
  15732. (goto-char (match-beginning 1))
  15733. (let (org-clock-adjust-closest)
  15734. (org-timestamp-change n org-ts-what updown))
  15735. (message "Clock adjusted in %s for heading: %s"
  15736. (file-name-nondirectory (buffer-file-name))
  15737. (org-get-heading t t)))))))))
  15738. ;; Try to recenter the calendar window, if any.
  15739. (if (and org-calendar-follow-timestamp-change
  15740. (get-buffer-window "*Calendar*" t)
  15741. (memq org-ts-what '(day month year)))
  15742. (org-recenter-calendar (time-to-days time))))))
  15743. (defun org-modify-ts-extra (s pos n dm)
  15744. "Change the different parts of the lead-time and repeat fields in timestamp."
  15745. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15746. ng h m new rem)
  15747. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15748. (cond
  15749. ((or (org-pos-in-match-range pos 2)
  15750. (org-pos-in-match-range pos 3))
  15751. (setq m (string-to-number (match-string 3 s))
  15752. h (string-to-number (match-string 2 s)))
  15753. (if (org-pos-in-match-range pos 2)
  15754. (setq h (+ h n))
  15755. (setq n (* dm (org-no-warnings (signum n))))
  15756. (when (not (= 0 (setq rem (% m dm))))
  15757. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15758. (setq m (+ m n)))
  15759. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15760. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15761. (setq h (min 24 (max 0 h)))
  15762. (setq ng 1 new (format "-%02d:%02d" h m)))
  15763. ((org-pos-in-match-range pos 6)
  15764. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15765. ((org-pos-in-match-range pos 5)
  15766. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15767. ((org-pos-in-match-range pos 9)
  15768. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15769. ((org-pos-in-match-range pos 8)
  15770. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15771. (when ng
  15772. (setq s (concat
  15773. (substring s 0 (match-beginning ng))
  15774. new
  15775. (substring s (match-end ng))))))
  15776. s))
  15777. (defun org-recenter-calendar (date)
  15778. "If the calendar is visible, recenter it to DATE."
  15779. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15780. (when cwin
  15781. (let ((calendar-move-hook nil))
  15782. (with-selected-window cwin
  15783. (calendar-goto-date (if (listp date) date
  15784. (calendar-gregorian-from-absolute date))))))))
  15785. (defun org-goto-calendar (&optional arg)
  15786. "Go to the Emacs calendar at the current date.
  15787. If there is a time stamp in the current line, go to that date.
  15788. A prefix ARG can be used to force the current date."
  15789. (interactive "P")
  15790. (let ((tsr org-ts-regexp) diff
  15791. (calendar-move-hook nil)
  15792. (calendar-view-holidays-initially-flag nil)
  15793. (calendar-view-diary-initially-flag nil))
  15794. (if (or (org-at-timestamp-p)
  15795. (save-excursion
  15796. (beginning-of-line 1)
  15797. (looking-at (concat ".*" tsr))))
  15798. (let ((d1 (time-to-days (current-time)))
  15799. (d2 (time-to-days
  15800. (org-time-string-to-time (match-string 1)))))
  15801. (setq diff (- d2 d1))))
  15802. (calendar)
  15803. (calendar-goto-today)
  15804. (if (and diff (not arg)) (calendar-forward-day diff))))
  15805. (defun org-get-date-from-calendar ()
  15806. "Return a list (month day year) of date at point in calendar."
  15807. (with-current-buffer "*Calendar*"
  15808. (save-match-data
  15809. (calendar-cursor-to-date))))
  15810. (defun org-date-from-calendar ()
  15811. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15812. If there is already a time stamp at the cursor position, update it."
  15813. (interactive)
  15814. (if (org-at-timestamp-p t)
  15815. (org-timestamp-change 0 'calendar)
  15816. (let ((cal-date (org-get-date-from-calendar)))
  15817. (org-insert-time-stamp
  15818. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15819. (defcustom org-effort-durations
  15820. `(("h" . 60)
  15821. ("d" . ,(* 60 8))
  15822. ("w" . ,(* 60 8 5))
  15823. ("m" . ,(* 60 8 5 4))
  15824. ("y" . ,(* 60 8 5 40)))
  15825. "Conversion factor to minutes for an effort modifier.
  15826. Each entry has the form (MODIFIER . MINUTES).
  15827. In an effort string, a number followed by MODIFIER is multiplied
  15828. by the specified number of MINUTES to obtain an effort in
  15829. minutes.
  15830. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15831. effort string \"2hours\" is equivalent to 120 minutes."
  15832. :group 'org-agenda
  15833. :version "24.1"
  15834. :type '(alist :key-type (string :tag "Modifier")
  15835. :value-type (number :tag "Minutes")))
  15836. (defun org-minutes-to-clocksum-string (m)
  15837. "Format number of minutes as a clocksum string.
  15838. The format is determined by `org-time-clocksum-format',
  15839. `org-time-clocksum-use-fractional' and
  15840. `org-time-clocksum-fractional-format' and
  15841. `org-time-clocksum-use-effort-durations'."
  15842. (let ((clocksum "") h d w mo y fmt n)
  15843. (setq h (if org-time-clocksum-use-effort-durations
  15844. (cdr (assoc "h" org-effort-durations)) 60)
  15845. d (if org-time-clocksum-use-effort-durations
  15846. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  15847. w (if org-time-clocksum-use-effort-durations
  15848. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  15849. mo (if org-time-clocksum-use-effort-durations
  15850. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  15851. y (if org-time-clocksum-use-effort-durations
  15852. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  15853. ;; fractional format
  15854. (if org-time-clocksum-use-fractional
  15855. (cond
  15856. ;; single format string
  15857. ((stringp org-time-clocksum-fractional-format)
  15858. (format org-time-clocksum-fractional-format (/ m (float h))))
  15859. ;; choice of fractional formats for different time units
  15860. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15861. (> (/ (truncate m) (* y d h)) 0))
  15862. (format fmt (/ m (* y d (float h)))))
  15863. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15864. (> (/ (truncate m) (* mo d h)) 0))
  15865. (format fmt (/ m (* mo d (float h)))))
  15866. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15867. (> (/ (truncate m) (* w d h)) 0))
  15868. (format fmt (/ m (* w d (float h)))))
  15869. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15870. (> (/ (truncate m) (* d h)) 0))
  15871. (format fmt (/ m (* d (float h)))))
  15872. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15873. (> (/ (truncate m) h) 0))
  15874. (format fmt (/ m (float h))))
  15875. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  15876. (format fmt m))
  15877. ;; fall back to smallest time unit with a format
  15878. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15879. (format fmt (/ m (float h))))
  15880. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15881. (format fmt (/ m (* d (float h)))))
  15882. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15883. (format fmt (/ m (* w d (float h)))))
  15884. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15885. (format fmt (/ m (* mo d (float h)))))
  15886. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15887. (format fmt (/ m (* y d (float h))))))
  15888. ;; standard (non-fractional) format, with single format string
  15889. (if (stringp org-time-clocksum-format)
  15890. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  15891. ;; separate formats components
  15892. (and (setq fmt (plist-get org-time-clocksum-format :years))
  15893. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  15894. (plist-get org-time-clocksum-format :require-years))
  15895. (setq clocksum (concat clocksum (format fmt n))
  15896. m (- m (* n y d h))))
  15897. (and (setq fmt (plist-get org-time-clocksum-format :months))
  15898. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  15899. (plist-get org-time-clocksum-format :require-months))
  15900. (setq clocksum (concat clocksum (format fmt n))
  15901. m (- m (* n mo d h))))
  15902. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  15903. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  15904. (plist-get org-time-clocksum-format :require-weeks))
  15905. (setq clocksum (concat clocksum (format fmt n))
  15906. m (- m (* n w d h))))
  15907. (and (setq fmt (plist-get org-time-clocksum-format :days))
  15908. (or (> (setq n (/ (truncate m) (* d h))) 0)
  15909. (plist-get org-time-clocksum-format :require-days))
  15910. (setq clocksum (concat clocksum (format fmt n))
  15911. m (- m (* n d h))))
  15912. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  15913. (or (> (setq n (/ (truncate m) h)) 0)
  15914. (plist-get org-time-clocksum-format :require-hours))
  15915. (setq clocksum (concat clocksum (format fmt n))
  15916. m (- m (* n h))))
  15917. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  15918. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  15919. (setq clocksum (concat clocksum (format fmt m))))
  15920. ;; return formatted time duration
  15921. clocksum))))
  15922. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  15923. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  15924. "Org mode version 8.0")
  15925. (defun org-hours-to-clocksum-string (n)
  15926. (org-minutes-to-clocksum-string (* n 60)))
  15927. (defun org-hh:mm-string-to-minutes (s)
  15928. "Convert a string H:MM to a number of minutes.
  15929. If the string is just a number, interpret it as minutes.
  15930. In fact, the first hh:mm or number in the string will be taken,
  15931. there can be extra stuff in the string.
  15932. If no number is found, the return value is 0."
  15933. (cond
  15934. ((integerp s) s)
  15935. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15936. (+ (* (string-to-number (match-string 1 s)) 60)
  15937. (string-to-number (match-string 2 s))))
  15938. ((string-match "\\([0-9]+\\)" s)
  15939. (string-to-number (match-string 1 s)))
  15940. (t 0)))
  15941. (defcustom org-image-actual-width t
  15942. "Should we use the actual width of images when inlining them?
  15943. When set to `t', always use the image width.
  15944. When set to a number, use imagemagick (when available) to set
  15945. the image's width to this value.
  15946. When set to a number in a list, try to get the width from the
  15947. #+ATTR.* keyword if it matches a width specification like
  15948. width=\"[0-9]+\", and fall back on that number if none is found.
  15949. When set to nil, try to get the width from an #+ATTR.* keyword
  15950. and fall back on the original width if none is found.
  15951. This requires Emacs >= 24.1, build with imagemagick support."
  15952. :group 'org-appearance
  15953. :version "24.4"
  15954. :package-version '(Org . "8.0")
  15955. :type '(choice
  15956. (const :tag "Use the image width" t)
  15957. (integer :tag "Use a number of pixels")
  15958. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  15959. (const :tag "Use #+ATTR* or don't resize" nil)))
  15960. (defcustom org-agenda-inhibit-startup nil
  15961. "Inhibit startup when preparing agenda buffers.
  15962. When this variable is `t' (the default), the initialization of
  15963. the Org agenda buffers is inhibited: e.g. the visibility state
  15964. is not set, the tables are not re-aligned, etc."
  15965. :type 'boolean
  15966. :version "24.3"
  15967. :group 'org-agenda)
  15968. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15969. "Convert a duration string S to minutes.
  15970. A bare number is interpreted as minutes, modifiers can be set by
  15971. customizing `org-effort-durations' (which see).
  15972. Entries containing a colon are interpreted as H:MM by
  15973. `org-hh:mm-string-to-minutes'."
  15974. (let ((result 0)
  15975. (re (concat "\\([0-9.]+\\) *\\("
  15976. (regexp-opt (mapcar 'car org-effort-durations))
  15977. "\\)")))
  15978. (while (string-match re s)
  15979. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15980. (string-to-number (match-string 1 s))))
  15981. (setq s (replace-match "" nil t s)))
  15982. (setq result (floor result))
  15983. (incf result (org-hh:mm-string-to-minutes s))
  15984. (if output-to-string (number-to-string result) result)))
  15985. ;;;; Files
  15986. (defun org-save-all-org-buffers ()
  15987. "Save all Org-mode buffers without user confirmation."
  15988. (interactive)
  15989. (message "Saving all Org-mode buffers...")
  15990. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15991. (when (featurep 'org-id) (org-id-locations-save))
  15992. (message "Saving all Org-mode buffers... done"))
  15993. (defun org-revert-all-org-buffers ()
  15994. "Revert all Org-mode buffers.
  15995. Prompt for confirmation when there are unsaved changes.
  15996. Be sure you know what you are doing before letting this function
  15997. overwrite your changes.
  15998. This function is useful in a setup where one tracks org files
  15999. with a version control system, to revert on one machine after pulling
  16000. changes from another. I believe the procedure must be like this:
  16001. 1. M-x org-save-all-org-buffers
  16002. 2. Pull changes from the other machine, resolve conflicts
  16003. 3. M-x org-revert-all-org-buffers"
  16004. (interactive)
  16005. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16006. (error "Abort"))
  16007. (save-excursion
  16008. (save-window-excursion
  16009. (mapc
  16010. (lambda (b)
  16011. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16012. (with-current-buffer b buffer-file-name))
  16013. (org-pop-to-buffer-same-window b)
  16014. (revert-buffer t 'no-confirm)))
  16015. (buffer-list))
  16016. (when (and (featurep 'org-id) org-id-track-globally)
  16017. (org-id-locations-load)))))
  16018. ;;;; Agenda files
  16019. ;;;###autoload
  16020. (defun org-switchb (&optional arg)
  16021. "Switch between Org buffers.
  16022. With one prefix argument, restrict available buffers to files.
  16023. With two prefix arguments, restrict available buffers to agenda files.
  16024. Defaults to `iswitchb' for buffer name completion.
  16025. Set `org-completion-use-ido' to make it use ido instead."
  16026. (interactive "P")
  16027. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16028. ((equal arg '(16)) (org-buffer-list 'agenda))
  16029. (t (org-buffer-list))))
  16030. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16031. (org-completion-use-ido org-completion-use-ido))
  16032. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16033. (setq org-completion-use-iswitchb t))
  16034. (org-pop-to-buffer-same-window
  16035. (org-icompleting-read "Org buffer: "
  16036. (mapcar 'list (mapcar 'buffer-name blist))
  16037. nil t))))
  16038. ;;; Define some older names previously used for this functionality
  16039. ;;;###autoload
  16040. (defalias 'org-ido-switchb 'org-switchb)
  16041. ;;;###autoload
  16042. (defalias 'org-iswitchb 'org-switchb)
  16043. (defun org-buffer-list (&optional predicate exclude-tmp)
  16044. "Return a list of Org buffers.
  16045. PREDICATE can be `export', `files' or `agenda'.
  16046. export restrict the list to Export buffers.
  16047. files restrict the list to buffers visiting Org files.
  16048. agenda restrict the list to buffers visiting agenda files.
  16049. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16050. (let* ((bfn nil)
  16051. (agenda-files (and (eq predicate 'agenda)
  16052. (mapcar 'file-truename (org-agenda-files t))))
  16053. (filter
  16054. (cond
  16055. ((eq predicate 'files)
  16056. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16057. ((eq predicate 'export)
  16058. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16059. ((eq predicate 'agenda)
  16060. (lambda (b)
  16061. (with-current-buffer b
  16062. (and (derived-mode-p 'org-mode)
  16063. (setq bfn (buffer-file-name b))
  16064. (member (file-truename bfn) agenda-files)))))
  16065. (t (lambda (b) (with-current-buffer b
  16066. (or (derived-mode-p 'org-mode)
  16067. (string-match "\*Org .*Export"
  16068. (buffer-name b)))))))))
  16069. (delq nil
  16070. (mapcar
  16071. (lambda(b)
  16072. (if (and (funcall filter b)
  16073. (or (not exclude-tmp)
  16074. (not (string-match "tmp" (buffer-name b)))))
  16075. b
  16076. nil))
  16077. (buffer-list)))))
  16078. (defun org-agenda-files (&optional unrestricted archives)
  16079. "Get the list of agenda files.
  16080. Optional UNRESTRICTED means return the full list even if a restriction
  16081. is currently in place.
  16082. When ARCHIVES is t, include all archive files that are really being
  16083. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16084. `org-agenda-archives-mode' is t."
  16085. (let ((files
  16086. (cond
  16087. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16088. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16089. ((listp org-agenda-files) org-agenda-files)
  16090. (t (error "Invalid value of `org-agenda-files'")))))
  16091. (setq files (apply 'append
  16092. (mapcar (lambda (f)
  16093. (if (file-directory-p f)
  16094. (directory-files
  16095. f t org-agenda-file-regexp)
  16096. (list f)))
  16097. files)))
  16098. (when org-agenda-skip-unavailable-files
  16099. (setq files (delq nil
  16100. (mapcar (function
  16101. (lambda (file)
  16102. (and (file-readable-p file) file)))
  16103. files))))
  16104. (when (or (eq archives t)
  16105. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16106. (setq files (org-add-archive-files files)))
  16107. files))
  16108. (defun org-agenda-file-p (&optional file)
  16109. "Return non-nil, if FILE is an agenda file.
  16110. If FILE is omitted, use the file associated with the current
  16111. buffer."
  16112. (member (or file (buffer-file-name))
  16113. (org-agenda-files t)))
  16114. (defun org-edit-agenda-file-list ()
  16115. "Edit the list of agenda files.
  16116. Depending on setup, this either uses customize to edit the variable
  16117. `org-agenda-files', or it visits the file that is holding the list. In the
  16118. latter case, the buffer is set up in a way that saving it automatically kills
  16119. the buffer and restores the previous window configuration."
  16120. (interactive)
  16121. (if (stringp org-agenda-files)
  16122. (let ((cw (current-window-configuration)))
  16123. (find-file org-agenda-files)
  16124. (org-set-local 'org-window-configuration cw)
  16125. (org-add-hook 'after-save-hook
  16126. (lambda ()
  16127. (set-window-configuration
  16128. (prog1 org-window-configuration
  16129. (kill-buffer (current-buffer))))
  16130. (org-install-agenda-files-menu)
  16131. (message "New agenda file list installed"))
  16132. nil 'local)
  16133. (message "%s" (substitute-command-keys
  16134. "Edit list and finish with \\[save-buffer]")))
  16135. (customize-variable 'org-agenda-files)))
  16136. (defun org-store-new-agenda-file-list (list)
  16137. "Set new value for the agenda file list and save it correctly."
  16138. (if (stringp org-agenda-files)
  16139. (let ((fe (org-read-agenda-file-list t)) b u)
  16140. (while (setq b (find-buffer-visiting org-agenda-files))
  16141. (kill-buffer b))
  16142. (with-temp-file org-agenda-files
  16143. (insert
  16144. (mapconcat
  16145. (lambda (f) ;; Keep un-expanded entries.
  16146. (if (setq u (assoc f fe))
  16147. (cdr u)
  16148. f))
  16149. list "\n")
  16150. "\n")))
  16151. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16152. (org-insert-mode-line-in-empty-file nil))
  16153. (setq org-agenda-files list)
  16154. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16155. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16156. "Read the list of agenda files from a file.
  16157. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16158. filenames, used by `org-store-new-agenda-file-list' to write back
  16159. un-expanded file names."
  16160. (when (file-directory-p org-agenda-files)
  16161. (error "`org-agenda-files' cannot be a single directory"))
  16162. (when (stringp org-agenda-files)
  16163. (with-temp-buffer
  16164. (insert-file-contents org-agenda-files)
  16165. (mapcar
  16166. (lambda (f)
  16167. (let ((e (expand-file-name (substitute-in-file-name f)
  16168. org-directory)))
  16169. (if pair-with-expansion
  16170. (cons e f)
  16171. e)))
  16172. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16173. ;;;###autoload
  16174. (defun org-cycle-agenda-files ()
  16175. "Cycle through the files in `org-agenda-files'.
  16176. If the current buffer visits an agenda file, find the next one in the list.
  16177. If the current buffer does not, find the first agenda file."
  16178. (interactive)
  16179. (let* ((fs (org-agenda-files t))
  16180. (files (append fs (list (car fs))))
  16181. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16182. file)
  16183. (unless files (error "No agenda files"))
  16184. (catch 'exit
  16185. (while (setq file (pop files))
  16186. (if (equal (file-truename file) tcf)
  16187. (when (car files)
  16188. (find-file (car files))
  16189. (throw 'exit t))))
  16190. (find-file (car fs)))
  16191. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16192. (defun org-agenda-file-to-front (&optional to-end)
  16193. "Move/add the current file to the top of the agenda file list.
  16194. If the file is not present in the list, it is added to the front. If it is
  16195. present, it is moved there. With optional argument TO-END, add/move to the
  16196. end of the list."
  16197. (interactive "P")
  16198. (let ((org-agenda-skip-unavailable-files nil)
  16199. (file-alist (mapcar (lambda (x)
  16200. (cons (file-truename x) x))
  16201. (org-agenda-files t)))
  16202. (ctf (file-truename
  16203. (or buffer-file-name
  16204. (error "Please save the current buffer to a file"))))
  16205. x had)
  16206. (setq x (assoc ctf file-alist) had x)
  16207. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16208. (if to-end
  16209. (setq file-alist (append (delq x file-alist) (list x)))
  16210. (setq file-alist (cons x (delq x file-alist))))
  16211. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16212. (org-install-agenda-files-menu)
  16213. (message "File %s to %s of agenda file list"
  16214. (if had "moved" "added") (if to-end "end" "front"))))
  16215. (defun org-remove-file (&optional file)
  16216. "Remove current file from the list of files in variable `org-agenda-files'.
  16217. These are the files which are being checked for agenda entries.
  16218. Optional argument FILE means use this file instead of the current."
  16219. (interactive)
  16220. (let* ((org-agenda-skip-unavailable-files nil)
  16221. (file (or file buffer-file-name
  16222. (error "Current buffer does not visit a file")))
  16223. (true-file (file-truename file))
  16224. (afile (abbreviate-file-name file))
  16225. (files (delq nil (mapcar
  16226. (lambda (x)
  16227. (if (equal true-file
  16228. (file-truename x))
  16229. nil x))
  16230. (org-agenda-files t)))))
  16231. (if (not (= (length files) (length (org-agenda-files t))))
  16232. (progn
  16233. (org-store-new-agenda-file-list files)
  16234. (org-install-agenda-files-menu)
  16235. (message "Removed file: %s" afile))
  16236. (message "File was not in list: %s (not removed)" afile))))
  16237. (defun org-file-menu-entry (file)
  16238. (vector file (list 'find-file file) t))
  16239. (defun org-check-agenda-file (file)
  16240. "Make sure FILE exists. If not, ask user what to do."
  16241. (when (not (file-exists-p file))
  16242. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16243. (abbreviate-file-name file))
  16244. (let ((r (downcase (read-char-exclusive))))
  16245. (cond
  16246. ((equal r ?r)
  16247. (org-remove-file file)
  16248. (throw 'nextfile t))
  16249. (t (error "Abort"))))))
  16250. (defun org-get-agenda-file-buffer (file)
  16251. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16252. it to the list of buffers which might be released later."
  16253. (let ((buf (org-find-base-buffer-visiting file)))
  16254. (if buf
  16255. buf ; just return it
  16256. ;; Make a new buffer and remember it
  16257. (setq buf (find-file-noselect file))
  16258. (if buf (push buf org-agenda-new-buffers))
  16259. buf)))
  16260. (defun org-release-buffers (blist)
  16261. "Release all buffers in list, asking the user for confirmation when needed.
  16262. When a buffer is unmodified, it is just killed. When modified, it is saved
  16263. \(if the user agrees) and then killed."
  16264. (let (buf file)
  16265. (while (setq buf (pop blist))
  16266. (setq file (buffer-file-name buf))
  16267. (when (and (buffer-modified-p buf)
  16268. file
  16269. (y-or-n-p (format "Save file %s? " file)))
  16270. (with-current-buffer buf (save-buffer)))
  16271. (kill-buffer buf))))
  16272. (defun org-agenda-prepare-buffers (files)
  16273. "Create buffers for all agenda files, protect archived trees and comments."
  16274. (interactive)
  16275. (let ((pa '(:org-archived t))
  16276. (pc '(:org-comment t))
  16277. (pall '(:org-archived t :org-comment t))
  16278. (inhibit-read-only t)
  16279. (org-inhibit-startup org-agenda-inhibit-startup)
  16280. (rea (concat ":" org-archive-tag ":"))
  16281. file re)
  16282. (save-excursion
  16283. (save-restriction
  16284. (while (setq file (pop files))
  16285. (catch 'nextfile
  16286. (if (bufferp file)
  16287. (set-buffer file)
  16288. (org-check-agenda-file file)
  16289. (set-buffer (org-get-agenda-file-buffer file)))
  16290. (widen)
  16291. (org-refresh-category-properties)
  16292. (org-refresh-properties org-effort-property 'org-effort)
  16293. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16294. (setq org-todo-keywords-for-agenda
  16295. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16296. (setq org-done-keywords-for-agenda
  16297. (append org-done-keywords-for-agenda org-done-keywords))
  16298. (setq org-todo-keyword-alist-for-agenda
  16299. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16300. (setq org-drawers-for-agenda
  16301. (append org-drawers-for-agenda org-drawers))
  16302. (setq org-tag-alist-for-agenda
  16303. (append org-tag-alist-for-agenda org-tag-alist))
  16304. (org-with-silent-modifications
  16305. (save-excursion
  16306. (remove-text-properties (point-min) (point-max) pall)
  16307. (when org-agenda-skip-archived-trees
  16308. (goto-char (point-min))
  16309. (while (re-search-forward rea nil t)
  16310. (if (org-at-heading-p t)
  16311. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16312. (goto-char (point-min))
  16313. (setq re (format org-heading-keyword-regexp-format
  16314. org-comment-string))
  16315. (while (re-search-forward re nil t)
  16316. (add-text-properties
  16317. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16318. (setq org-todo-keywords-for-agenda
  16319. (org-uniquify org-todo-keywords-for-agenda))
  16320. (setq org-todo-keyword-alist-for-agenda
  16321. (org-uniquify org-todo-keyword-alist-for-agenda)
  16322. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  16323. ;;;; CDLaTeX minor mode
  16324. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16325. "Keymap for the minor `org-cdlatex-mode'.")
  16326. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16327. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16328. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16329. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16330. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16331. (defvar org-cdlatex-texmathp-advice-is-done nil
  16332. "Flag remembering if we have applied the advice to texmathp already.")
  16333. (define-minor-mode org-cdlatex-mode
  16334. "Toggle the minor `org-cdlatex-mode'.
  16335. This mode supports entering LaTeX environment and math in LaTeX fragments
  16336. in Org-mode.
  16337. \\{org-cdlatex-mode-map}"
  16338. nil " OCDL" nil
  16339. (when org-cdlatex-mode
  16340. (require 'cdlatex)
  16341. (run-hooks 'cdlatex-mode-hook)
  16342. (cdlatex-compute-tables))
  16343. (unless org-cdlatex-texmathp-advice-is-done
  16344. (setq org-cdlatex-texmathp-advice-is-done t)
  16345. (defadvice texmathp (around org-math-always-on activate)
  16346. "Always return t in org-mode buffers.
  16347. This is because we want to insert math symbols without dollars even outside
  16348. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16349. an embedded LaTeX fragment, let texmathp do its job.
  16350. \\[org-cdlatex-mode-map]"
  16351. (interactive)
  16352. (let (p)
  16353. (cond
  16354. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16355. ((eq this-command 'cdlatex-math-symbol)
  16356. (setq ad-return-value t
  16357. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16358. (t
  16359. (let ((p (org-inside-LaTeX-fragment-p)))
  16360. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16361. (setq ad-return-value t
  16362. texmathp-why '("Org-mode embedded math" . 0))
  16363. (if p ad-do-it)))))))))
  16364. (defun turn-on-org-cdlatex ()
  16365. "Unconditionally turn on `org-cdlatex-mode'."
  16366. (org-cdlatex-mode 1))
  16367. (defun org-try-cdlatex-tab ()
  16368. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16369. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16370. - inside a LaTeX fragment, or
  16371. - after the first word in a line, where an abbreviation expansion could
  16372. insert a LaTeX environment."
  16373. (when org-cdlatex-mode
  16374. (cond
  16375. ;; Before any word on the line: No expansion possible.
  16376. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16377. ;; Just after first word on the line: Expand it. Make sure it
  16378. ;; cannot happen on headlines, though.
  16379. ((save-excursion
  16380. (skip-chars-backward "a-zA-Z0-9*")
  16381. (skip-chars-backward " \t")
  16382. (and (bolp) (not (org-at-heading-p))))
  16383. (cdlatex-tab) t)
  16384. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16385. (defun org-cdlatex-underscore-caret (&optional arg)
  16386. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16387. Revert to the normal definition outside of these fragments."
  16388. (interactive "P")
  16389. (if (org-inside-LaTeX-fragment-p)
  16390. (call-interactively 'cdlatex-sub-superscript)
  16391. (let (org-cdlatex-mode)
  16392. (call-interactively (key-binding (vector last-input-event))))))
  16393. (defun org-cdlatex-math-modify (&optional arg)
  16394. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16395. Revert to the normal definition outside of these fragments."
  16396. (interactive "P")
  16397. (if (org-inside-LaTeX-fragment-p)
  16398. (call-interactively 'cdlatex-math-modify)
  16399. (let (org-cdlatex-mode)
  16400. (call-interactively (key-binding (vector last-input-event))))))
  16401. ;;;; LaTeX fragments
  16402. (defvar org-latex-regexps
  16403. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16404. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16405. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16406. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16407. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16408. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16409. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16410. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16411. "Regular expressions for matching embedded LaTeX.")
  16412. (defun org-inside-LaTeX-fragment-p ()
  16413. "Test if point is inside a LaTeX fragment.
  16414. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16415. sequence appearing also before point.
  16416. Even though the matchers for math are configurable, this function assumes
  16417. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16418. delimiters are skipped when they have been removed by customization.
  16419. The return value is nil, or a cons cell with the delimiter and the
  16420. position of this delimiter.
  16421. This function does a reasonably good job, but can locally be fooled by
  16422. for example currency specifications. For example it will assume being in
  16423. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16424. fragments that are properly closed, but during editing, we have to live
  16425. with the uncertainty caused by missing closing delimiters. This function
  16426. looks only before point, not after."
  16427. (catch 'exit
  16428. (let ((pos (point))
  16429. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16430. (lim (progn
  16431. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16432. (point)))
  16433. dd-on str (start 0) m re)
  16434. (goto-char pos)
  16435. (when dodollar
  16436. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16437. re (nth 1 (assoc "$" org-latex-regexps)))
  16438. (while (string-match re str start)
  16439. (cond
  16440. ((= (match-end 0) (length str))
  16441. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16442. ((= (match-end 0) (- (length str) 5))
  16443. (throw 'exit nil))
  16444. (t (setq start (match-end 0))))))
  16445. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16446. (goto-char pos)
  16447. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16448. (and (match-beginning 2) (throw 'exit nil))
  16449. ;; count $$
  16450. (while (re-search-backward "\\$\\$" lim t)
  16451. (setq dd-on (not dd-on)))
  16452. (goto-char pos)
  16453. (if dd-on (cons "$$" m))))))
  16454. (defun org-inside-latex-macro-p ()
  16455. "Is point inside a LaTeX macro or its arguments?"
  16456. (save-match-data
  16457. (org-in-regexp
  16458. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16459. (defvar org-latex-fragment-image-overlays nil
  16460. "List of overlays carrying the images of latex fragments.")
  16461. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16462. (defun org-remove-latex-fragment-image-overlays ()
  16463. "Remove all overlays with LaTeX fragment images in current buffer."
  16464. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16465. (setq org-latex-fragment-image-overlays nil))
  16466. (defun org-preview-latex-fragment (&optional subtree)
  16467. "Preview the LaTeX fragment at point, or all locally or globally.
  16468. If the cursor is in a LaTeX fragment, create the image and overlay
  16469. it over the source code. If there is no fragment at point, display
  16470. all fragments in the current text, from one headline to the next. With
  16471. prefix SUBTREE, display all fragments in the current subtree. With a
  16472. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16473. the cursor is before the first headline,
  16474. display all fragments in the buffer.
  16475. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16476. (interactive "P")
  16477. (unless buffer-file-name
  16478. (error "Can't preview LaTeX fragment in a non-file buffer"))
  16479. (org-remove-latex-fragment-image-overlays)
  16480. (save-excursion
  16481. (save-restriction
  16482. (let (beg end at msg)
  16483. (cond
  16484. ((or (equal subtree '(16))
  16485. (not (save-excursion
  16486. (re-search-backward org-outline-regexp-bol nil t))))
  16487. (setq beg (point-min) end (point-max)
  16488. msg "Creating images for buffer...%s"))
  16489. ((equal subtree '(4))
  16490. (org-back-to-heading)
  16491. (setq beg (point) end (org-end-of-subtree t)
  16492. msg "Creating images for subtree...%s"))
  16493. (t
  16494. (if (setq at (org-inside-LaTeX-fragment-p))
  16495. (goto-char (max (point-min) (- (cdr at) 2)))
  16496. (org-back-to-heading))
  16497. (setq beg (point) end (progn (outline-next-heading) (point))
  16498. msg (if at "Creating image...%s"
  16499. "Creating images for entry...%s"))))
  16500. (message msg "")
  16501. (narrow-to-region beg end)
  16502. (goto-char beg)
  16503. (org-format-latex
  16504. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16505. (file-name-nondirectory
  16506. buffer-file-name)))
  16507. default-directory 'overlays msg at 'forbuffer
  16508. org-latex-create-formula-image-program)
  16509. (message msg "done. Use `C-c C-c' to remove images.")))))
  16510. (defun org-format-latex (prefix &optional dir overlays msg at
  16511. forbuffer processing-type)
  16512. "Replace LaTeX fragments with links to an image, and produce images.
  16513. Some of the options can be changed using the variable
  16514. `org-format-latex-options'."
  16515. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16516. (let* ((prefixnodir (file-name-nondirectory prefix))
  16517. (absprefix (expand-file-name prefix dir))
  16518. (todir (file-name-directory absprefix))
  16519. (opt org-format-latex-options)
  16520. (optnew org-format-latex-options)
  16521. (matchers (plist-get opt :matchers))
  16522. (re-list org-latex-regexps)
  16523. (cnt 0) txt hash link beg end re e checkdir
  16524. string
  16525. m n block-type block linkfile movefile ov)
  16526. ;; Check the different regular expressions
  16527. (while (setq e (pop re-list))
  16528. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16529. block (if block-type "\n\n" ""))
  16530. (when (member m matchers)
  16531. (goto-char (point-min))
  16532. (while (re-search-forward re nil t)
  16533. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16534. (or (not overlays)
  16535. (not (eq (get-char-property (match-beginning n)
  16536. 'org-overlay-type)
  16537. 'org-latex-overlay))))
  16538. (cond
  16539. ((eq processing-type 'verbatim))
  16540. ((eq processing-type 'mathjax)
  16541. ;; Prepare for MathJax processing.
  16542. (setq string (match-string n))
  16543. (when (member m '("$" "$1"))
  16544. (save-excursion
  16545. (delete-region (match-beginning n) (match-end n))
  16546. (goto-char (match-beginning n))
  16547. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16548. ((or (eq processing-type 'dvipng)
  16549. (eq processing-type 'imagemagick))
  16550. ;; Process to an image.
  16551. (setq txt (match-string n)
  16552. beg (match-beginning n) end (match-end n)
  16553. cnt (1+ cnt))
  16554. (let ((face (face-at-point))
  16555. (fg (plist-get opt :foreground))
  16556. (bg (plist-get opt :background))
  16557. ;; Ensure full list is printed.
  16558. print-length print-level)
  16559. (when forbuffer
  16560. ;; Get the colors from the face at point.
  16561. (goto-char beg)
  16562. (when (eq fg 'auto)
  16563. (setq fg (face-attribute face :foreground nil 'default)))
  16564. (when (eq bg 'auto)
  16565. (setq bg (face-attribute face :background nil 'default)))
  16566. (setq optnew (copy-sequence opt))
  16567. (plist-put optnew :foreground fg)
  16568. (plist-put optnew :background bg))
  16569. (setq hash (sha1 (prin1-to-string
  16570. (list org-format-latex-header
  16571. org-latex-default-packages-alist
  16572. org-latex-packages-alist
  16573. org-format-latex-options
  16574. forbuffer txt fg bg)))
  16575. linkfile (format "%s_%s.png" prefix hash)
  16576. movefile (format "%s_%s.png" absprefix hash)))
  16577. (setq link (concat block "[[file:" linkfile "]]" block))
  16578. (if msg (message msg cnt))
  16579. (goto-char beg)
  16580. (unless checkdir ; Ensure the directory exists.
  16581. (setq checkdir t)
  16582. (or (file-directory-p todir) (make-directory todir t)))
  16583. (unless (file-exists-p movefile)
  16584. (org-create-formula-image
  16585. txt movefile optnew forbuffer processing-type))
  16586. (if overlays
  16587. (progn
  16588. (mapc (lambda (o)
  16589. (if (eq (overlay-get o 'org-overlay-type)
  16590. 'org-latex-overlay)
  16591. (delete-overlay o)))
  16592. (overlays-in beg end))
  16593. (setq ov (make-overlay beg end))
  16594. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16595. (if (featurep 'xemacs)
  16596. (progn
  16597. (overlay-put ov 'invisible t)
  16598. (overlay-put
  16599. ov 'end-glyph
  16600. (make-glyph (vector 'png :file movefile))))
  16601. (overlay-put
  16602. ov 'display
  16603. (list 'image :type 'png :file movefile :ascent 'center)))
  16604. (push ov org-latex-fragment-image-overlays)
  16605. (goto-char end))
  16606. (delete-region beg end)
  16607. (insert (org-add-props link
  16608. (list 'org-latex-src
  16609. (replace-regexp-in-string
  16610. "\"" "" txt)
  16611. 'org-latex-src-embed-type
  16612. (if block-type 'paragraph 'character))))))
  16613. ((eq processing-type 'mathml)
  16614. ;; Process to MathML
  16615. (unless (save-match-data (org-format-latex-mathml-available-p))
  16616. (error "LaTeX to MathML converter not configured"))
  16617. (setq txt (match-string n)
  16618. beg (match-beginning n) end (match-end n)
  16619. cnt (1+ cnt))
  16620. (if msg (message msg cnt))
  16621. (goto-char beg)
  16622. (delete-region beg end)
  16623. (insert (org-format-latex-as-mathml
  16624. txt block-type prefix dir)))
  16625. (t
  16626. (error "Unknown conversion type %s for latex fragments"
  16627. processing-type)))))))))
  16628. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16629. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16630. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16631. sucessful, return the portion between \"<math...> </math>\"
  16632. elements otherwise return nil. When MATHML-FILE is specified,
  16633. write the results in to that file. When invoked as an
  16634. interactive command, prompt for LATEX-FRAG, with initial value
  16635. set to the current active region and echo the results for user
  16636. inspection."
  16637. (interactive (list (let ((frag (when (org-region-active-p)
  16638. (buffer-substring-no-properties
  16639. (region-beginning) (region-end)))))
  16640. (read-string "LaTeX Fragment: " frag nil frag))))
  16641. (unless latex-frag (error "Invalid latex-frag"))
  16642. (let* ((tmp-in-file (file-relative-name
  16643. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16644. (ignore (write-region latex-frag nil tmp-in-file))
  16645. (tmp-out-file (file-relative-name
  16646. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16647. (cmd (format-spec
  16648. org-latex-to-mathml-convert-command
  16649. `((?j . ,(shell-quote-argument
  16650. (expand-file-name org-latex-to-mathml-jar-file)))
  16651. (?I . ,(shell-quote-argument tmp-in-file))
  16652. (?o . ,(shell-quote-argument tmp-out-file)))))
  16653. mathml shell-command-output)
  16654. (when (org-called-interactively-p 'any)
  16655. (unless (org-format-latex-mathml-available-p)
  16656. (error "LaTeX to MathML converter not configured")))
  16657. (message "Running %s" cmd)
  16658. (setq shell-command-output (shell-command-to-string cmd))
  16659. (setq mathml
  16660. (when (file-readable-p tmp-out-file)
  16661. (with-current-buffer (find-file-noselect tmp-out-file t)
  16662. (goto-char (point-min))
  16663. (when (re-search-forward
  16664. (concat
  16665. (regexp-quote
  16666. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16667. "\\(.\\|\n\\)*"
  16668. (regexp-quote "</math>")) nil t)
  16669. (prog1 (match-string 0) (kill-buffer))))))
  16670. (cond
  16671. (mathml
  16672. (setq mathml
  16673. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16674. (when mathml-file
  16675. (write-region mathml nil mathml-file))
  16676. (when (org-called-interactively-p 'any)
  16677. (message mathml)))
  16678. ((message "LaTeX to MathML conversion failed")
  16679. (message shell-command-output)))
  16680. (delete-file tmp-in-file)
  16681. (when (file-exists-p tmp-out-file)
  16682. (delete-file tmp-out-file))
  16683. mathml))
  16684. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16685. prefix &optional dir)
  16686. "Use `org-create-math-formula' but check local cache first."
  16687. (let* ((absprefix (expand-file-name prefix dir))
  16688. (print-length nil) (print-level nil)
  16689. (formula-id (concat
  16690. "formula-"
  16691. (sha1
  16692. (prin1-to-string
  16693. (list latex-frag
  16694. org-latex-to-mathml-convert-command)))))
  16695. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16696. (formula-cache-dir (file-name-directory formula-cache)))
  16697. (unless (file-directory-p formula-cache-dir)
  16698. (make-directory formula-cache-dir t))
  16699. (unless (file-exists-p formula-cache)
  16700. (org-create-math-formula latex-frag formula-cache))
  16701. (if (file-exists-p formula-cache)
  16702. ;; Successful conversion. Return the link to MathML file.
  16703. (org-add-props
  16704. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16705. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16706. 'org-latex-src-embed-type (if latex-frag-type
  16707. 'paragraph 'character)))
  16708. ;; Failed conversion. Return the LaTeX fragment verbatim
  16709. latex-frag)))
  16710. (defun org-create-formula-image (string tofile options buffer &optional type)
  16711. "Create an image from LaTeX source using dvipng or convert.
  16712. This function calls either `org-create-formula-image-with-dvipng'
  16713. or `org-create-formula-image-with-imagemagick' depending on the
  16714. value of `org-latex-create-formula-image-program' or on the value
  16715. of the optional TYPE variable.
  16716. Note: ultimately these two function should be combined as they
  16717. share a good deal of logic."
  16718. (org-check-external-command
  16719. "latex" "needed to convert LaTeX fragments to images")
  16720. (funcall
  16721. (case (or type org-latex-create-formula-image-program)
  16722. ('dvipng
  16723. (org-check-external-command
  16724. "dvipng" "needed to convert LaTeX fragments to images")
  16725. #'org-create-formula-image-with-dvipng)
  16726. ('imagemagick
  16727. (org-check-external-command
  16728. "convert" "you need to install imagemagick")
  16729. #'org-create-formula-image-with-imagemagick)
  16730. (t (error
  16731. "invalid value of `org-latex-create-formula-image-program'")))
  16732. string tofile options buffer))
  16733. (declare-function org-export--get-global-options "ox" (&optional backend))
  16734. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  16735. (defun org-create-formula--latex-header ()
  16736. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  16737. (org-latex-guess-inputenc
  16738. (org-splice-latex-header
  16739. org-format-latex-header
  16740. org-latex-default-packages-alist
  16741. org-latex-packages-alist t
  16742. (plist-get
  16743. (org-combine-plists
  16744. (org-export--get-global-options 'latex)
  16745. (org-export--get-inbuffer-options 'latex))
  16746. :latex-header))))
  16747. ;; This function borrows from Ganesh Swami's latex2png.el
  16748. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16749. "This calls dvipng."
  16750. (require 'ox-latex)
  16751. (let* ((tmpdir (if (featurep 'xemacs)
  16752. (temp-directory)
  16753. temporary-file-directory))
  16754. (texfilebase (make-temp-name
  16755. (expand-file-name "orgtex" tmpdir)))
  16756. (texfile (concat texfilebase ".tex"))
  16757. (dvifile (concat texfilebase ".dvi"))
  16758. (pngfile (concat texfilebase ".png"))
  16759. (fnh (if (featurep 'xemacs)
  16760. (font-height (face-font 'default))
  16761. (face-attribute 'default :height nil)))
  16762. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16763. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16764. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16765. "Black"))
  16766. (bg (or (plist-get options (if buffer :background :html-background))
  16767. "Transparent")))
  16768. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16769. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16770. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16771. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16772. (let ((latex-header (org-create-formula--latex-header)))
  16773. (with-temp-file texfile
  16774. (insert latex-header)
  16775. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  16776. (let ((dir default-directory))
  16777. (condition-case nil
  16778. (progn
  16779. (cd tmpdir)
  16780. (call-process "latex" nil nil nil texfile))
  16781. (error nil))
  16782. (cd dir))
  16783. (if (not (file-exists-p dvifile))
  16784. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16785. (condition-case nil
  16786. (if (featurep 'xemacs)
  16787. (call-process "dvipng" nil nil nil
  16788. "-fg" fg "-bg" bg
  16789. "-T" "tight"
  16790. "-o" pngfile
  16791. dvifile)
  16792. (call-process "dvipng" nil nil nil
  16793. "-fg" fg "-bg" bg
  16794. "-D" dpi
  16795. ;;"-x" scale "-y" scale
  16796. "-T" "tight"
  16797. "-o" pngfile
  16798. dvifile))
  16799. (error nil))
  16800. (if (not (file-exists-p pngfile))
  16801. (if org-format-latex-signal-error
  16802. (error "Failed to create png file from %s" texfile)
  16803. (message "Failed to create png file from %s" texfile)
  16804. nil)
  16805. ;; Use the requested file name and clean up
  16806. (copy-file pngfile tofile 'replace)
  16807. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16808. (if (file-exists-p (concat texfilebase e))
  16809. (delete-file (concat texfilebase e))))
  16810. pngfile))))
  16811. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  16812. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16813. "This calls convert, which is included into imagemagick."
  16814. (require 'ox-latex)
  16815. (let* ((tmpdir (if (featurep 'xemacs)
  16816. (temp-directory)
  16817. temporary-file-directory))
  16818. (texfilebase (make-temp-name
  16819. (expand-file-name "orgtex" tmpdir)))
  16820. (texfile (concat texfilebase ".tex"))
  16821. (pdffile (concat texfilebase ".pdf"))
  16822. (pngfile (concat texfilebase ".png"))
  16823. (fnh (if (featurep 'xemacs)
  16824. (font-height (face-font 'default))
  16825. (face-attribute 'default :height nil)))
  16826. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16827. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16828. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16829. "black"))
  16830. (bg (or (plist-get options (if buffer :background :html-background))
  16831. "white")))
  16832. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16833. (setq fg (org-latex-color-format fg)))
  16834. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16835. (setq bg (org-latex-color-format
  16836. (if (string= bg "Transparent") "white" bg))))
  16837. (let ((latex-header (org-create-formula--latex-header)))
  16838. (with-temp-file texfile
  16839. (insert latex-header)
  16840. (insert "\n\\begin{document}\n"
  16841. "\\definecolor{fg}{rgb}{" fg "}\n"
  16842. "\\definecolor{bg}{rgb}{" bg "}\n"
  16843. "\n\\pagecolor{bg}\n"
  16844. "\n{\\color{fg}\n"
  16845. string
  16846. "\n}\n"
  16847. "\n\\end{document}\n")))
  16848. (org-latex-compile texfile t)
  16849. (if (not (file-exists-p pdffile))
  16850. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16851. (condition-case nil
  16852. (if (featurep 'xemacs)
  16853. (call-process "convert" nil nil nil
  16854. "-density" "96"
  16855. "-trim"
  16856. "-antialias"
  16857. pdffile
  16858. "-quality" "100"
  16859. ;; "-sharpen" "0x1.0"
  16860. pngfile)
  16861. (call-process "convert" nil nil nil
  16862. "-density" dpi
  16863. "-trim"
  16864. "-antialias"
  16865. pdffile
  16866. "-quality" "100"
  16867. ;; "-sharpen" "0x1.0"
  16868. pngfile))
  16869. (error nil))
  16870. (if (not (file-exists-p pngfile))
  16871. (if org-format-latex-signal-error
  16872. (error "Failed to create png file from %s" texfile)
  16873. (message "Failed to create png file from %s" texfile)
  16874. nil)
  16875. ;; Use the requested file name and clean up
  16876. (copy-file pngfile tofile 'replace)
  16877. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16878. (if (file-exists-p (concat texfilebase e))
  16879. (delete-file (concat texfilebase e))))
  16880. pngfile))))
  16881. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16882. "Fill a LaTeX header template TPL.
  16883. In the template, the following place holders will be recognized:
  16884. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16885. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16886. [PACKAGES] \\usepackage statements for PKG
  16887. [NO-PACKAGES] do not include PKG
  16888. [EXTRA] the string EXTRA
  16889. [NO-EXTRA] do not include EXTRA
  16890. For backward compatibility, if both the positive and the negative place
  16891. holder is missing, the positive one (without the \"NO-\") will be
  16892. assumed to be present at the end of the template.
  16893. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16894. EXTRA is a string.
  16895. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16896. (let (rpl (end ""))
  16897. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16898. (setq rpl (if (or (match-end 1) (not def-pkg))
  16899. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16900. tpl (replace-match rpl t t tpl))
  16901. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16902. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16903. (setq rpl (if (or (match-end 1) (not pkg))
  16904. "" (org-latex-packages-to-string pkg snippets-p t))
  16905. tpl (replace-match rpl t t tpl))
  16906. (if pkg (setq end
  16907. (concat end "\n"
  16908. (org-latex-packages-to-string pkg snippets-p)))))
  16909. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16910. (setq rpl (if (or (match-end 1) (not extra))
  16911. "" (concat extra "\n"))
  16912. tpl (replace-match rpl t t tpl))
  16913. (if (and extra (string-match "\\S-" extra))
  16914. (setq end (concat end "\n" extra))))
  16915. (if (string-match "\\S-" end)
  16916. (concat tpl "\n" end)
  16917. tpl)))
  16918. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16919. "Turn an alist of packages into a string with the \\usepackage macros."
  16920. (setq pkg (mapconcat (lambda(p)
  16921. (cond
  16922. ((stringp p) p)
  16923. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16924. (format "%% Package %s omitted" (cadr p)))
  16925. ((equal "" (car p))
  16926. (format "\\usepackage{%s}" (cadr p)))
  16927. (t
  16928. (format "\\usepackage[%s]{%s}"
  16929. (car p) (cadr p)))))
  16930. pkg
  16931. "\n"))
  16932. (if newline (concat pkg "\n") pkg))
  16933. (defun org-dvipng-color (attr)
  16934. "Return a RGB color specification for dvipng."
  16935. (apply 'format "rgb %s %s %s"
  16936. (mapcar 'org-normalize-color
  16937. (if (featurep 'xemacs)
  16938. (color-rgb-components
  16939. (face-property 'default
  16940. (cond ((eq attr :foreground) 'foreground)
  16941. ((eq attr :background) 'background))))
  16942. (color-values (face-attribute 'default attr nil))))))
  16943. (defun org-dvipng-color-format (color-name)
  16944. "Convert COLOR-NAME to a RGB color value for dvipng."
  16945. (apply 'format "rgb %s %s %s"
  16946. (mapcar 'org-normalize-color
  16947. (color-values color-name))))
  16948. (defun org-latex-color (attr)
  16949. "Return a RGB color for the LaTeX color package."
  16950. (apply 'format "%s,%s,%s"
  16951. (mapcar 'org-normalize-color
  16952. (if (featurep 'xemacs)
  16953. (color-rgb-components
  16954. (face-property 'default
  16955. (cond ((eq attr :foreground) 'foreground)
  16956. ((eq attr :background) 'background))))
  16957. (color-values (face-attribute 'default attr nil))))))
  16958. (defun org-latex-color-format (color-name)
  16959. "Convert COLOR-NAME to a RGB color value."
  16960. (apply 'format "%s,%s,%s"
  16961. (mapcar 'org-normalize-color
  16962. (color-values color-name))))
  16963. (defun org-normalize-color (value)
  16964. "Return string to be used as color value for an RGB component."
  16965. (format "%g" (/ value 65535.0)))
  16966. ;; Image display
  16967. (defvar org-inline-image-overlays nil)
  16968. (make-variable-buffer-local 'org-inline-image-overlays)
  16969. (defun org-toggle-inline-images (&optional include-linked)
  16970. "Toggle the display of inline images.
  16971. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16972. (interactive "P")
  16973. (if org-inline-image-overlays
  16974. (progn
  16975. (org-remove-inline-images)
  16976. (message "Inline image display turned off"))
  16977. (org-display-inline-images include-linked)
  16978. (if (and (org-called-interactively-p)
  16979. org-inline-image-overlays)
  16980. (message "%d images displayed inline"
  16981. (length org-inline-image-overlays))
  16982. (message "No images to display inline"))))
  16983. (defun org-redisplay-inline-images ()
  16984. "Refresh the display of inline images."
  16985. (interactive)
  16986. (if (not org-inline-image-overlays)
  16987. (org-toggle-inline-images)
  16988. (org-toggle-inline-images)
  16989. (org-toggle-inline-images)))
  16990. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16991. "Display inline images.
  16992. Normally only links without a description part are inlined, because this
  16993. is how it will work for export. When INCLUDE-LINKED is set, also links
  16994. with a description part will be inlined. This can be nice for a quick
  16995. look at those images, but it does not reflect what exported files will look
  16996. like.
  16997. When REFRESH is set, refresh existing images between BEG and END.
  16998. This will create new image displays only if necessary.
  16999. BEG and END default to the buffer boundaries."
  17000. (interactive "P")
  17001. (unless refresh
  17002. (org-remove-inline-images)
  17003. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  17004. (save-excursion
  17005. (save-restriction
  17006. (widen)
  17007. (setq beg (or beg (point-min)) end (or end (point-max)))
  17008. (goto-char beg)
  17009. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  17010. (substring (org-image-file-name-regexp) 0 -2)
  17011. "\\)\\]" (if include-linked "" "\\]")))
  17012. old file ov img type attrwidth width)
  17013. (while (re-search-forward re end t)
  17014. (setq old (get-char-property-and-overlay (match-beginning 1)
  17015. 'org-image-overlay)
  17016. file (expand-file-name
  17017. (concat (or (match-string 3) "") (match-string 4))))
  17018. (when (image-type-available-p 'imagemagick)
  17019. (setq attrwidth (if (or (listp org-image-actual-width)
  17020. (null org-image-actual-width))
  17021. (save-excursion
  17022. (save-match-data
  17023. (when (re-search-backward
  17024. "#\\+ATTR.*width=\"\\([^\"]+\\)\""
  17025. (save-excursion
  17026. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  17027. (string-to-number (match-string 1))))))
  17028. width (cond ((eq org-image-actual-width t) nil)
  17029. ((null org-image-actual-width) attrwidth)
  17030. ((numberp org-image-actual-width)
  17031. org-image-actual-width)
  17032. ((listp org-image-actual-width)
  17033. (or attrwidth (car org-image-actual-width))))
  17034. type (if width 'imagemagick)))
  17035. (when (file-exists-p file)
  17036. (if (and (car-safe old) refresh)
  17037. (image-refresh (overlay-get (cdr old) 'display))
  17038. (setq img (save-match-data (create-image file type nil :width width)))
  17039. (when img
  17040. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17041. (overlay-put ov 'display img)
  17042. (overlay-put ov 'face 'default)
  17043. (overlay-put ov 'org-image-overlay t)
  17044. (overlay-put ov 'modification-hooks
  17045. (list 'org-display-inline-remove-overlay))
  17046. (push ov org-inline-image-overlays)))))))))
  17047. (define-obsolete-function-alias
  17048. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17049. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17050. "Remove inline-display overlay if a corresponding region is modified."
  17051. (let ((inhibit-modification-hooks t))
  17052. (when (and ov after)
  17053. (delete ov org-inline-image-overlays)
  17054. (delete-overlay ov))))
  17055. (defun org-remove-inline-images ()
  17056. "Remove inline display of images."
  17057. (interactive)
  17058. (mapc 'delete-overlay org-inline-image-overlays)
  17059. (setq org-inline-image-overlays nil))
  17060. ;;;; Key bindings
  17061. ;; Outline functions from `outline-mode-prefix-map'
  17062. ;; that can be remapped in Org:
  17063. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17064. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17065. (define-key org-mode-map [remap outline-forward-same-level]
  17066. 'org-forward-heading-same-level)
  17067. (define-key org-mode-map [remap outline-backward-same-level]
  17068. 'org-backward-heading-same-level)
  17069. (define-key org-mode-map [remap show-branches]
  17070. 'org-kill-note-or-show-branches)
  17071. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17072. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17073. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17074. ;; Outline functions from `outline-mode-prefix-map' that can not
  17075. ;; be remapped in Org:
  17076. ;;
  17077. ;; - the column "key binding" shows whether the Outline function is still
  17078. ;; available in Org mode on the same key that it has been bound to in
  17079. ;; Outline mode:
  17080. ;; - "overridden": key used for a different functionality in Org mode
  17081. ;; - else: key still bound to the same Outline function in Org mode
  17082. ;;
  17083. ;; | Outline function | key binding | Org replacement |
  17084. ;; |------------------------------------+-------------+-----------------------|
  17085. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17086. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17087. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17088. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17089. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17090. ;; | `show-entry' | overridden | no replacement |
  17091. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17092. ;; | `show-branches' | `C-c C-k' | still same function |
  17093. ;; | `show-subtree' | overridden | visibility cycling |
  17094. ;; | `show-all' | overridden | no replacement |
  17095. ;; | `hide-subtree' | overridden | visibility cycling |
  17096. ;; | `hide-body' | overridden | no replacement |
  17097. ;; | `hide-entry' | overridden | visibility cycling |
  17098. ;; | `hide-leaves' | overridden | no replacement |
  17099. ;; | `hide-sublevels' | overridden | no replacement |
  17100. ;; | `hide-other' | overridden | no replacement |
  17101. ;; Make `C-c C-x' a prefix key
  17102. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17103. ;; TAB key with modifiers
  17104. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17105. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17106. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17107. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17108. ;; The following line is necessary under Suse GNU/Linux
  17109. (unless (featurep 'xemacs)
  17110. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17111. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17112. (define-key org-mode-map [backtab] 'org-shifttab)
  17113. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17114. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17115. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17116. ;; Cursor keys with modifiers
  17117. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17118. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17119. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17120. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17121. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17122. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17123. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17124. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17125. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17126. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17127. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17128. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17129. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17130. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17131. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17132. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17133. ;; Babel keys
  17134. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17135. (mapc (lambda (pair)
  17136. (define-key org-babel-map (car pair) (cdr pair)))
  17137. org-babel-key-bindings)
  17138. ;;; Extra keys for tty access.
  17139. ;; We only set them when really needed because otherwise the
  17140. ;; menus don't show the simple keys
  17141. (when (or org-use-extra-keys
  17142. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17143. (not window-system))
  17144. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17145. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17146. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17147. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17148. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17149. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17150. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17151. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17152. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17153. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17154. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17155. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17156. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17157. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17158. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17159. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17160. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17161. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17162. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17163. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17164. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17165. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17166. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17167. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17168. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17169. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17170. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17171. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17172. ;; All the other keys
  17173. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17174. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17175. (if (boundp 'narrow-map)
  17176. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17177. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17178. (if (boundp 'narrow-map)
  17179. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17180. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17181. (if (boundp 'narrow-map)
  17182. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17183. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17184. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17185. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17186. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17187. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17188. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17189. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17190. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17191. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17192. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17193. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17194. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17195. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17196. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17197. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17198. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17199. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17200. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17201. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17202. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17203. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17204. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17205. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17206. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17207. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17208. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17209. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17210. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17211. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17212. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17213. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17214. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17215. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17216. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17217. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17218. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17219. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17220. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17221. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17222. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17223. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17224. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17225. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17226. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17227. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17228. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17229. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17230. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17231. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17232. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17233. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17234. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17235. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17236. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17237. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17238. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17239. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17240. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17241. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17242. (org-defkey org-mode-map "\C-m" 'org-return)
  17243. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17244. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17245. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17246. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17247. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17248. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17249. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17250. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17251. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17252. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17253. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17254. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17255. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17256. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17257. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17258. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17259. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17260. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17261. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17262. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17263. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17264. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17265. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17266. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17267. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17268. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17269. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17270. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17271. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17272. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17273. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17274. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17275. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17276. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17277. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17278. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17279. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17280. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17281. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17282. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17283. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17284. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17285. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17286. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17287. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17288. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17289. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17290. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17291. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17292. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17293. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17294. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17295. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17296. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17297. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17298. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17299. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17300. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17301. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17302. (when (featurep 'xemacs)
  17303. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17304. (defconst org-speed-commands-default
  17305. '(
  17306. ("Outline Navigation")
  17307. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17308. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17309. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17310. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17311. ("F" . org-next-block)
  17312. ("B" . org-previous-block)
  17313. ("u" . (org-speed-move-safe 'outline-up-heading))
  17314. ("j" . org-goto)
  17315. ("g" . (org-refile t))
  17316. ("Outline Visibility")
  17317. ("c" . org-cycle)
  17318. ("C" . org-shifttab)
  17319. (" " . org-display-outline-path)
  17320. ("=" . org-columns)
  17321. ("Outline Structure Editing")
  17322. ("U" . org-shiftmetaup)
  17323. ("D" . org-shiftmetadown)
  17324. ("r" . org-metaright)
  17325. ("l" . org-metaleft)
  17326. ("R" . org-shiftmetaright)
  17327. ("L" . org-shiftmetaleft)
  17328. ("i" . (progn (forward-char 1) (call-interactively
  17329. 'org-insert-heading-respect-content)))
  17330. ("^" . org-sort)
  17331. ("w" . org-refile)
  17332. ("a" . org-archive-subtree-default-with-confirmation)
  17333. ("@" . org-mark-subtree)
  17334. ("#" . org-toggle-comment)
  17335. ("Clock Commands")
  17336. ("I" . org-clock-in)
  17337. ("O" . org-clock-out)
  17338. ("Meta Data Editing")
  17339. ("t" . org-todo)
  17340. ("," . (org-priority))
  17341. ("0" . (org-priority ?\ ))
  17342. ("1" . (org-priority ?A))
  17343. ("2" . (org-priority ?B))
  17344. ("3" . (org-priority ?C))
  17345. (":" . org-set-tags-command)
  17346. ("e" . org-set-effort)
  17347. ("E" . org-inc-effort)
  17348. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17349. (org-entry-put (point) "APPT_WARNTIME" m)))
  17350. ("Agenda Views etc")
  17351. ("v" . org-agenda)
  17352. ("/" . org-sparse-tree)
  17353. ("Misc")
  17354. ("o" . org-open-at-point)
  17355. ("?" . org-speed-command-help)
  17356. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17357. (">" . (org-agenda-remove-restriction-lock))
  17358. )
  17359. "The default speed commands.")
  17360. (defun org-print-speed-command (e)
  17361. (if (> (length (car e)) 1)
  17362. (progn
  17363. (princ "\n")
  17364. (princ (car e))
  17365. (princ "\n")
  17366. (princ (make-string (length (car e)) ?-))
  17367. (princ "\n"))
  17368. (princ (car e))
  17369. (princ " ")
  17370. (if (symbolp (cdr e))
  17371. (princ (symbol-name (cdr e)))
  17372. (prin1 (cdr e)))
  17373. (princ "\n")))
  17374. (defun org-speed-command-help ()
  17375. "Show the available speed commands."
  17376. (interactive)
  17377. (if (not org-use-speed-commands)
  17378. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  17379. (with-output-to-temp-buffer "*Help*"
  17380. (princ "User-defined Speed commands\n===========================\n")
  17381. (mapc 'org-print-speed-command org-speed-commands-user)
  17382. (princ "\n")
  17383. (princ "Built-in Speed commands\n=======================\n")
  17384. (mapc 'org-print-speed-command org-speed-commands-default))
  17385. (with-current-buffer "*Help*"
  17386. (setq truncate-lines t))))
  17387. (defun org-speed-move-safe (cmd)
  17388. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17389. If not, return to the original position and throw an error."
  17390. (interactive)
  17391. (let ((pos (point)))
  17392. (call-interactively cmd)
  17393. (unless (and (bolp) (org-at-heading-p))
  17394. (goto-char pos)
  17395. (error "Boundary reached while executing %s" cmd))))
  17396. (defvar org-self-insert-command-undo-counter 0)
  17397. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17398. (defvar org-speed-command nil)
  17399. (define-obsolete-function-alias
  17400. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17401. (defun org-speed-command-activate (keys)
  17402. "Hook for activating single-letter speed commands.
  17403. `org-speed-commands-default' specifies a minimal command set.
  17404. Use `org-speed-commands-user' for further customization."
  17405. (when (or (and (bolp) (looking-at org-outline-regexp))
  17406. (and (functionp org-use-speed-commands)
  17407. (funcall org-use-speed-commands)))
  17408. (cdr (assoc keys (append org-speed-commands-user
  17409. org-speed-commands-default)))))
  17410. (define-obsolete-function-alias
  17411. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17412. (defun org-babel-speed-command-activate (keys)
  17413. "Hook for activating single-letter code block commands."
  17414. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17415. (cdr (assoc keys org-babel-key-bindings))))
  17416. (defcustom org-speed-command-hook
  17417. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17418. "Hook for activating speed commands at strategic locations.
  17419. Hook functions are called in sequence until a valid handler is
  17420. found.
  17421. Each hook takes a single argument, a user-pressed command key
  17422. which is also a `self-insert-command' from the global map.
  17423. Within the hook, examine the cursor position and the command key
  17424. and return nil or a valid handler as appropriate. Handler could
  17425. be one of an interactive command, a function, or a form.
  17426. Set `org-use-speed-commands' to non-nil value to enable this
  17427. hook. The default setting is `org-speed-command-activate'."
  17428. :group 'org-structure
  17429. :version "24.1"
  17430. :type 'hook)
  17431. (defun org-self-insert-command (N)
  17432. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17433. If the cursor is in a table looking at whitespace, the whitespace is
  17434. overwritten, and the table is not marked as requiring realignment."
  17435. (interactive "p")
  17436. (org-check-before-invisible-edit 'insert)
  17437. (cond
  17438. ((and org-use-speed-commands
  17439. (setq org-speed-command
  17440. (run-hook-with-args-until-success
  17441. 'org-speed-command-hook (this-command-keys))))
  17442. (cond
  17443. ((commandp org-speed-command)
  17444. (setq this-command org-speed-command)
  17445. (call-interactively org-speed-command))
  17446. ((functionp org-speed-command)
  17447. (funcall org-speed-command))
  17448. ((and org-speed-command (listp org-speed-command))
  17449. (eval org-speed-command))
  17450. (t (let (org-use-speed-commands)
  17451. (call-interactively 'org-self-insert-command)))))
  17452. ((and
  17453. (org-table-p)
  17454. (progn
  17455. ;; check if we blank the field, and if that triggers align
  17456. (and (featurep 'org-table) org-table-auto-blank-field
  17457. (member last-command
  17458. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17459. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17460. ;; got extra space, this field does not determine column width
  17461. (let (org-table-may-need-update) (org-table-blank-field))
  17462. ;; no extra space, this field may determine column width
  17463. (org-table-blank-field)))
  17464. t)
  17465. (eq N 1)
  17466. (looking-at "[^|\n]* |"))
  17467. (let (org-table-may-need-update)
  17468. (goto-char (1- (match-end 0)))
  17469. (backward-delete-char 1)
  17470. (goto-char (match-beginning 0))
  17471. (self-insert-command N)))
  17472. (t
  17473. (setq org-table-may-need-update t)
  17474. (self-insert-command N)
  17475. (org-fix-tags-on-the-fly)
  17476. (if org-self-insert-cluster-for-undo
  17477. (if (not (eq last-command 'org-self-insert-command))
  17478. (setq org-self-insert-command-undo-counter 1)
  17479. (if (>= org-self-insert-command-undo-counter 20)
  17480. (setq org-self-insert-command-undo-counter 1)
  17481. (and (> org-self-insert-command-undo-counter 0)
  17482. buffer-undo-list (listp buffer-undo-list)
  17483. (not (cadr buffer-undo-list)) ; remove nil entry
  17484. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17485. (setq org-self-insert-command-undo-counter
  17486. (1+ org-self-insert-command-undo-counter))))))))
  17487. (defun org-check-before-invisible-edit (kind)
  17488. "Check is editing if kind KIND would be dangerous with invisible text around.
  17489. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17490. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17491. (if (and org-catch-invisible-edits
  17492. (or (not (boundp 'visible-mode)) (not visible-mode))
  17493. (or (get-char-property (point) 'invisible)
  17494. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17495. ;; OK, we need to take a closer look
  17496. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17497. (invisible-before-point (if (bobp) nil (get-char-property
  17498. (1- (point)) 'invisible)))
  17499. (border-and-ok-direction
  17500. (or
  17501. ;; Check if we are acting predictably before invisible text
  17502. (and invisible-at-point (not invisible-before-point)
  17503. (memq kind '(insert delete-backward)))
  17504. ;; Check if we are acting predictably after invisible text
  17505. ;; This works not well, and I have turned it off. It seems
  17506. ;; better to always show and stop after invisible text.
  17507. ;; (and (not invisible-at-point) invisible-before-point
  17508. ;; (memq kind '(insert delete)))
  17509. )))
  17510. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17511. (memq invisible-before-point '(outline org-hide-block t)))
  17512. (if (eq org-catch-invisible-edits 'error)
  17513. (error "Editing in invisible areas is prohibited - make visible first"))
  17514. (if (and org-custom-properties-overlays
  17515. (y-or-n-p "Display invisible properties in this buffer? "))
  17516. (org-toggle-custom-properties-visibility)
  17517. ;; Make the area visible
  17518. (save-excursion
  17519. (if invisible-before-point
  17520. (goto-char (previous-single-char-property-change
  17521. (point) 'invisible)))
  17522. (org-cycle))
  17523. (cond
  17524. ((eq org-catch-invisible-edits 'show)
  17525. ;; That's it, we do the edit after showing
  17526. (message
  17527. "Unfolding invisible region around point before editing")
  17528. (sit-for 1))
  17529. ((and (eq org-catch-invisible-edits 'smart)
  17530. border-and-ok-direction)
  17531. (message "Unfolding invisible region around point before editing"))
  17532. (t
  17533. ;; Don't do the edit, make the user repeat it in full visibility
  17534. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17535. (defun org-fix-tags-on-the-fly ()
  17536. (when (and (equal (char-after (point-at-bol)) ?*)
  17537. (org-at-heading-p))
  17538. (org-align-tags-here org-tags-column)))
  17539. (defun org-delete-backward-char (N)
  17540. "Like `delete-backward-char', insert whitespace at field end in tables.
  17541. When deleting backwards, in tables this function will insert whitespace in
  17542. front of the next \"|\" separator, to keep the table aligned. The table will
  17543. still be marked for re-alignment if the field did fill the entire column,
  17544. because, in this case the deletion might narrow the column."
  17545. (interactive "p")
  17546. (save-match-data
  17547. (org-check-before-invisible-edit 'delete-backward)
  17548. (if (and (org-table-p)
  17549. (eq N 1)
  17550. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17551. (looking-at ".*?|"))
  17552. (let ((pos (point))
  17553. (noalign (looking-at "[^|\n\r]* |"))
  17554. (c org-table-may-need-update))
  17555. (backward-delete-char N)
  17556. (if (not overwrite-mode)
  17557. (progn
  17558. (skip-chars-forward "^|")
  17559. (insert " ")
  17560. (goto-char (1- pos))))
  17561. ;; noalign: if there were two spaces at the end, this field
  17562. ;; does not determine the width of the column.
  17563. (if noalign (setq org-table-may-need-update c)))
  17564. (backward-delete-char N)
  17565. (org-fix-tags-on-the-fly))))
  17566. (defun org-delete-char (N)
  17567. "Like `delete-char', but insert whitespace at field end in tables.
  17568. When deleting characters, in tables this function will insert whitespace in
  17569. front of the next \"|\" separator, to keep the table aligned. The table will
  17570. still be marked for re-alignment if the field did fill the entire column,
  17571. because, in this case the deletion might narrow the column."
  17572. (interactive "p")
  17573. (save-match-data
  17574. (org-check-before-invisible-edit 'delete)
  17575. (if (and (org-table-p)
  17576. (not (bolp))
  17577. (not (= (char-after) ?|))
  17578. (eq N 1))
  17579. (if (looking-at ".*?|")
  17580. (let ((pos (point))
  17581. (noalign (looking-at "[^|\n\r]* |"))
  17582. (c org-table-may-need-update))
  17583. (replace-match
  17584. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17585. (goto-char pos)
  17586. ;; noalign: if there were two spaces at the end, this field
  17587. ;; does not determine the width of the column.
  17588. (if noalign (setq org-table-may-need-update c)))
  17589. (delete-char N))
  17590. (delete-char N)
  17591. (org-fix-tags-on-the-fly))))
  17592. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17593. (put 'org-self-insert-command 'delete-selection t)
  17594. (put 'orgtbl-self-insert-command 'delete-selection t)
  17595. (put 'org-delete-char 'delete-selection 'supersede)
  17596. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17597. (put 'org-yank 'delete-selection 'yank)
  17598. ;; Make `flyspell-mode' delay after some commands
  17599. (put 'org-self-insert-command 'flyspell-delayed t)
  17600. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17601. (put 'org-delete-char 'flyspell-delayed t)
  17602. (put 'org-delete-backward-char 'flyspell-delayed t)
  17603. ;; Make pabbrev-mode expand after org-mode commands
  17604. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17605. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17606. ;; How to do this: Measure non-white length of current string
  17607. ;; If equal to column width, we should realign.
  17608. (defun org-remap (map &rest commands)
  17609. "In MAP, remap the functions given in COMMANDS.
  17610. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17611. (let (new old)
  17612. (while commands
  17613. (setq old (pop commands) new (pop commands))
  17614. (if (fboundp 'command-remapping)
  17615. (org-defkey map (vector 'remap old) new)
  17616. (substitute-key-definition old new map global-map)))))
  17617. (defun org-transpose-words ()
  17618. "Transpose words for Org.
  17619. This uses the `org-mode-transpose-word-syntax-table' syntax
  17620. table, which interprets characters in `org-emphasis-alist' as
  17621. word constituants."
  17622. (interactive)
  17623. (with-syntax-table org-mode-transpose-word-syntax-table
  17624. (call-interactively 'transpose-words)))
  17625. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17626. (when (eq org-enable-table-editor 'optimized)
  17627. ;; If the user wants maximum table support, we need to hijack
  17628. ;; some standard editing functions
  17629. (org-remap org-mode-map
  17630. 'self-insert-command 'org-self-insert-command
  17631. 'delete-char 'org-delete-char
  17632. 'delete-backward-char 'org-delete-backward-char)
  17633. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17634. (defvar org-ctrl-c-ctrl-c-hook nil
  17635. "Hook for functions attaching themselves to `C-c C-c'.
  17636. This can be used to add additional functionality to the C-c C-c
  17637. key which executes context-dependent commands. This hook is run
  17638. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17639. run after the last test.
  17640. Each function will be called with no arguments. The function
  17641. must check if the context is appropriate for it to act. If yes,
  17642. it should do its thing and then return a non-nil value. If the
  17643. context is wrong, just do nothing and return nil.")
  17644. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17645. "Hook for functions attaching themselves to `C-c C-c'.
  17646. This can be used to add additional functionality to the C-c C-c
  17647. key which executes context-dependent commands. This hook is run
  17648. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17649. before the first test.
  17650. Each function will be called with no arguments. The function
  17651. must check if the context is appropriate for it to act. If yes,
  17652. it should do its thing and then return a non-nil value. If the
  17653. context is wrong, just do nothing and return nil.")
  17654. (defvar org-tab-first-hook nil
  17655. "Hook for functions to attach themselves to TAB.
  17656. See `org-ctrl-c-ctrl-c-hook' for more information.
  17657. This hook runs as the first action when TAB is pressed, even before
  17658. `org-cycle' messes around with the `outline-regexp' to cater for
  17659. inline tasks and plain list item folding.
  17660. If any function in this hook returns t, any other actions that
  17661. would have been caused by TAB (such as table field motion or visibility
  17662. cycling) will not occur.")
  17663. (defvar org-tab-after-check-for-table-hook nil
  17664. "Hook for functions to attach themselves to TAB.
  17665. See `org-ctrl-c-ctrl-c-hook' for more information.
  17666. This hook runs after it has been established that the cursor is not in a
  17667. table, but before checking if the cursor is in a headline or if global cycling
  17668. should be done.
  17669. If any function in this hook returns t, not other actions like visibility
  17670. cycling will be done.")
  17671. (defvar org-tab-after-check-for-cycling-hook nil
  17672. "Hook for functions to attach themselves to TAB.
  17673. See `org-ctrl-c-ctrl-c-hook' for more information.
  17674. This hook runs after it has been established that not table field motion and
  17675. not visibility should be done because of current context. This is probably
  17676. the place where a package like yasnippets can hook in.")
  17677. (defvar org-tab-before-tab-emulation-hook nil
  17678. "Hook for functions to attach themselves to TAB.
  17679. See `org-ctrl-c-ctrl-c-hook' for more information.
  17680. This hook runs after every other options for TAB have been exhausted, but
  17681. before indentation and \t insertion takes place.")
  17682. (defvar org-metaleft-hook nil
  17683. "Hook for functions attaching themselves to `M-left'.
  17684. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17685. (defvar org-metaright-hook nil
  17686. "Hook for functions attaching themselves to `M-right'.
  17687. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17688. (defvar org-metaup-hook nil
  17689. "Hook for functions attaching themselves to `M-up'.
  17690. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17691. (defvar org-metadown-hook nil
  17692. "Hook for functions attaching themselves to `M-down'.
  17693. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17694. (defvar org-shiftmetaleft-hook nil
  17695. "Hook for functions attaching themselves to `M-S-left'.
  17696. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17697. (defvar org-shiftmetaright-hook nil
  17698. "Hook for functions attaching themselves to `M-S-right'.
  17699. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17700. (defvar org-shiftmetaup-hook nil
  17701. "Hook for functions attaching themselves to `M-S-up'.
  17702. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17703. (defvar org-shiftmetadown-hook nil
  17704. "Hook for functions attaching themselves to `M-S-down'.
  17705. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17706. (defvar org-metareturn-hook nil
  17707. "Hook for functions attaching themselves to `M-RET'.
  17708. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17709. (defvar org-shiftup-hook nil
  17710. "Hook for functions attaching themselves to `S-up'.
  17711. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17712. (defvar org-shiftup-final-hook nil
  17713. "Hook for functions attaching themselves to `S-up'.
  17714. This one runs after all other options except shift-select have been excluded.
  17715. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17716. (defvar org-shiftdown-hook nil
  17717. "Hook for functions attaching themselves to `S-down'.
  17718. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17719. (defvar org-shiftdown-final-hook nil
  17720. "Hook for functions attaching themselves to `S-down'.
  17721. This one runs after all other options except shift-select have been excluded.
  17722. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17723. (defvar org-shiftleft-hook nil
  17724. "Hook for functions attaching themselves to `S-left'.
  17725. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17726. (defvar org-shiftleft-final-hook nil
  17727. "Hook for functions attaching themselves to `S-left'.
  17728. This one runs after all other options except shift-select have been excluded.
  17729. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17730. (defvar org-shiftright-hook nil
  17731. "Hook for functions attaching themselves to `S-right'.
  17732. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17733. (defvar org-shiftright-final-hook nil
  17734. "Hook for functions attaching themselves to `S-right'.
  17735. This one runs after all other options except shift-select have been excluded.
  17736. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17737. (defun org-modifier-cursor-error ()
  17738. "Throw an error, a modified cursor command was applied in wrong context."
  17739. (error "This command is active in special context like tables, headlines or items"))
  17740. (defun org-shiftselect-error ()
  17741. "Throw an error because Shift-Cursor command was applied in wrong context."
  17742. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17743. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17744. (error "This command works only in special context like headlines or timestamps")))
  17745. (defun org-call-for-shift-select (cmd)
  17746. (let ((this-command-keys-shift-translated t))
  17747. (call-interactively cmd)))
  17748. (defun org-shifttab (&optional arg)
  17749. "Global visibility cycling or move to previous table field.
  17750. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17751. on context.
  17752. See the individual commands for more information."
  17753. (interactive "P")
  17754. (cond
  17755. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17756. ((integerp arg)
  17757. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17758. (message "Content view to level: %d" arg)
  17759. (org-content (prefix-numeric-value arg2))
  17760. (setq org-cycle-global-status 'overview)))
  17761. (t (call-interactively 'org-global-cycle))))
  17762. (defun org-shiftmetaleft ()
  17763. "Promote subtree or delete table column.
  17764. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17765. `org-table-delete-column', depending on context. See the
  17766. individual commands for more information."
  17767. (interactive)
  17768. (cond
  17769. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17770. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17771. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17772. ((if (not (org-region-active-p)) (org-at-item-p)
  17773. (save-excursion (goto-char (region-beginning))
  17774. (org-at-item-p)))
  17775. (call-interactively 'org-outdent-item-tree))
  17776. (t (org-modifier-cursor-error))))
  17777. (defun org-shiftmetaright ()
  17778. "Demote subtree or insert table column.
  17779. Calls `org-demote-subtree', `org-indent-item-tree', or
  17780. `org-table-insert-column', depending on context. See the
  17781. individual commands for more information."
  17782. (interactive)
  17783. (cond
  17784. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17785. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17786. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17787. ((if (not (org-region-active-p)) (org-at-item-p)
  17788. (save-excursion (goto-char (region-beginning))
  17789. (org-at-item-p)))
  17790. (call-interactively 'org-indent-item-tree))
  17791. (t (org-modifier-cursor-error))))
  17792. (defun org-shiftmetaup (&optional arg)
  17793. "Move subtree up or kill table row.
  17794. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17795. `org-move-item-up' or `org-timestamp-up', depending on context.
  17796. See the individual commands for more information."
  17797. (interactive "P")
  17798. (cond
  17799. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17800. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17801. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17802. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17803. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17804. (call-interactively 'org-timestamp-up)))
  17805. (t (call-interactively 'org-drag-line-backward))))
  17806. (defun org-shiftmetadown (&optional arg)
  17807. "Move subtree down or insert table row.
  17808. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17809. `org-move-item-down' or `org-timestamp-up', depending on context.
  17810. See the individual commands for more information."
  17811. (interactive "P")
  17812. (cond
  17813. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17814. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17815. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17816. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17817. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17818. (call-interactively 'org-timestamp-down)))
  17819. (t (call-interactively 'org-drag-line-forward))))
  17820. (defsubst org-hidden-tree-error ()
  17821. (error
  17822. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17823. (defun org-metaleft (&optional arg)
  17824. "Promote heading or move table column to left.
  17825. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17826. With no specific context, calls the Emacs default `backward-word'.
  17827. See the individual commands for more information."
  17828. (interactive "P")
  17829. (cond
  17830. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17831. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17832. ((org-with-limited-levels
  17833. (or (org-at-heading-p)
  17834. (and (org-region-active-p)
  17835. (save-excursion
  17836. (goto-char (region-beginning))
  17837. (org-at-heading-p)))))
  17838. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17839. (call-interactively 'org-do-promote))
  17840. ;; At an inline task.
  17841. ((org-at-heading-p)
  17842. (call-interactively 'org-inlinetask-promote))
  17843. ((or (org-at-item-p)
  17844. (and (org-region-active-p)
  17845. (save-excursion
  17846. (goto-char (region-beginning))
  17847. (org-at-item-p))))
  17848. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17849. (call-interactively 'org-outdent-item))
  17850. (t (call-interactively 'backward-word))))
  17851. (defun org-metaright (&optional arg)
  17852. "Demote a subtree, a list item or move table column to right.
  17853. In front of a drawer or a block keyword, indent it correctly.
  17854. With no specific context, calls the Emacs default `forward-word'.
  17855. See the individual commands for more information."
  17856. (interactive "P")
  17857. (cond
  17858. ((run-hook-with-args-until-success 'org-metaright-hook))
  17859. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17860. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17861. ((org-at-block-p) (call-interactively 'org-indent-block))
  17862. ((org-with-limited-levels
  17863. (or (org-at-heading-p)
  17864. (and (org-region-active-p)
  17865. (save-excursion
  17866. (goto-char (region-beginning))
  17867. (org-at-heading-p)))))
  17868. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17869. (call-interactively 'org-do-demote))
  17870. ;; At an inline task.
  17871. ((org-at-heading-p)
  17872. (call-interactively 'org-inlinetask-demote))
  17873. ((or (org-at-item-p)
  17874. (and (org-region-active-p)
  17875. (save-excursion
  17876. (goto-char (region-beginning))
  17877. (org-at-item-p))))
  17878. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17879. (call-interactively 'org-indent-item))
  17880. (t (call-interactively 'forward-word))))
  17881. (defun org-check-for-hidden (what)
  17882. "Check if there are hidden headlines/items in the current visual line.
  17883. WHAT can be either `headlines' or `items'. If the current line is
  17884. an outline or item heading and it has a folded subtree below it,
  17885. this function returns t, nil otherwise."
  17886. (let ((re (cond
  17887. ((eq what 'headlines) org-outline-regexp-bol)
  17888. ((eq what 'items) (org-item-beginning-re))
  17889. (t (error "This should not happen"))))
  17890. beg end)
  17891. (save-excursion
  17892. (catch 'exit
  17893. (unless (org-region-active-p)
  17894. (setq beg (point-at-bol))
  17895. (beginning-of-line 2)
  17896. (while (and (not (eobp)) ;; this is like `next-line'
  17897. (get-char-property (1- (point)) 'invisible))
  17898. (beginning-of-line 2))
  17899. (setq end (point))
  17900. (goto-char beg)
  17901. (goto-char (point-at-eol))
  17902. (setq end (max end (point)))
  17903. (while (re-search-forward re end t)
  17904. (if (get-char-property (match-beginning 0) 'invisible)
  17905. (throw 'exit t))))
  17906. nil))))
  17907. (defun org-metaup (&optional arg)
  17908. "Move subtree up or move table row up.
  17909. Calls `org-move-subtree-up' or `org-table-move-row' or
  17910. `org-move-item-up', depending on context. See the individual commands
  17911. for more information."
  17912. (interactive "P")
  17913. (cond
  17914. ((run-hook-with-args-until-success 'org-metaup-hook))
  17915. ((org-region-active-p)
  17916. (let* ((a (min (region-beginning) (region-end)))
  17917. (b (1- (max (region-beginning) (region-end))))
  17918. (c (save-excursion (goto-char a)
  17919. (move-beginning-of-line 0)))
  17920. (d (save-excursion (goto-char a)
  17921. (move-end-of-line 0) (point))))
  17922. (transpose-regions a b c d)
  17923. (goto-char c)))
  17924. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17925. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17926. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17927. (t (org-drag-element-backward))))
  17928. (defun org-metadown (&optional arg)
  17929. "Move subtree down or move table row down.
  17930. Calls `org-move-subtree-down' or `org-table-move-row' or
  17931. `org-move-item-down', depending on context. See the individual
  17932. commands for more information."
  17933. (interactive "P")
  17934. (cond
  17935. ((run-hook-with-args-until-success 'org-metadown-hook))
  17936. ((org-region-active-p)
  17937. (let* ((a (min (region-beginning) (region-end)))
  17938. (b (max (region-beginning) (region-end)))
  17939. (c (save-excursion (goto-char b)
  17940. (move-beginning-of-line 1)))
  17941. (d (save-excursion (goto-char b)
  17942. (move-end-of-line 1) (1+ (point)))))
  17943. (transpose-regions a b c d)
  17944. (goto-char d)))
  17945. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17946. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17947. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17948. (t (org-drag-element-forward))))
  17949. (defun org-shiftup (&optional arg)
  17950. "Increase item in timestamp or increase priority of current headline.
  17951. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17952. depending on context. See the individual commands for more information."
  17953. (interactive "P")
  17954. (cond
  17955. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17956. ((and org-support-shift-select (org-region-active-p))
  17957. (org-call-for-shift-select 'previous-line))
  17958. ((org-at-timestamp-p t)
  17959. (call-interactively (if org-edit-timestamp-down-means-later
  17960. 'org-timestamp-down 'org-timestamp-up)))
  17961. ((and (not (eq org-support-shift-select 'always))
  17962. org-enable-priority-commands
  17963. (org-at-heading-p))
  17964. (call-interactively 'org-priority-up))
  17965. ((and (not org-support-shift-select) (org-at-item-p))
  17966. (call-interactively 'org-previous-item))
  17967. ((org-clocktable-try-shift 'up arg))
  17968. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17969. (org-support-shift-select
  17970. (org-call-for-shift-select 'previous-line))
  17971. (t (org-shiftselect-error))))
  17972. (defun org-shiftdown (&optional arg)
  17973. "Decrease item in timestamp or decrease priority of current headline.
  17974. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17975. depending on context. See the individual commands for more information."
  17976. (interactive "P")
  17977. (cond
  17978. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17979. ((and org-support-shift-select (org-region-active-p))
  17980. (org-call-for-shift-select 'next-line))
  17981. ((org-at-timestamp-p t)
  17982. (call-interactively (if org-edit-timestamp-down-means-later
  17983. 'org-timestamp-up 'org-timestamp-down)))
  17984. ((and (not (eq org-support-shift-select 'always))
  17985. org-enable-priority-commands
  17986. (org-at-heading-p))
  17987. (call-interactively 'org-priority-down))
  17988. ((and (not org-support-shift-select) (org-at-item-p))
  17989. (call-interactively 'org-next-item))
  17990. ((org-clocktable-try-shift 'down arg))
  17991. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17992. (org-support-shift-select
  17993. (org-call-for-shift-select 'next-line))
  17994. (t (org-shiftselect-error))))
  17995. (defun org-shiftright (&optional arg)
  17996. "Cycle the thing at point or in the current line, depending on context.
  17997. Depending on context, this does one of the following:
  17998. - switch a timestamp at point one day into the future
  17999. - on a headline, switch to the next TODO keyword.
  18000. - on an item, switch entire list to the next bullet type
  18001. - on a property line, switch to the next allowed value
  18002. - on a clocktable definition line, move time block into the future"
  18003. (interactive "P")
  18004. (cond
  18005. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18006. ((and org-support-shift-select (org-region-active-p))
  18007. (org-call-for-shift-select 'forward-char))
  18008. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18009. ((and (not (eq org-support-shift-select 'always))
  18010. (org-at-heading-p))
  18011. (let ((org-inhibit-logging
  18012. (not org-treat-S-cursor-todo-selection-as-state-change))
  18013. (org-inhibit-blocking
  18014. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18015. (org-call-with-arg 'org-todo 'right)))
  18016. ((or (and org-support-shift-select
  18017. (not (eq org-support-shift-select 'always))
  18018. (org-at-item-bullet-p))
  18019. (and (not org-support-shift-select) (org-at-item-p)))
  18020. (org-call-with-arg 'org-cycle-list-bullet nil))
  18021. ((and (not (eq org-support-shift-select 'always))
  18022. (org-at-property-p))
  18023. (call-interactively 'org-property-next-allowed-value))
  18024. ((org-clocktable-try-shift 'right arg))
  18025. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18026. (org-support-shift-select
  18027. (org-call-for-shift-select 'forward-char))
  18028. (t (org-shiftselect-error))))
  18029. (defun org-shiftleft (&optional arg)
  18030. "Cycle the thing at point or in the current line, depending on context.
  18031. Depending on context, this does one of the following:
  18032. - switch a timestamp at point one day into the past
  18033. - on a headline, switch to the previous TODO keyword.
  18034. - on an item, switch entire list to the previous bullet type
  18035. - on a property line, switch to the previous allowed value
  18036. - on a clocktable definition line, move time block into the past"
  18037. (interactive "P")
  18038. (cond
  18039. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18040. ((and org-support-shift-select (org-region-active-p))
  18041. (org-call-for-shift-select 'backward-char))
  18042. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18043. ((and (not (eq org-support-shift-select 'always))
  18044. (org-at-heading-p))
  18045. (let ((org-inhibit-logging
  18046. (not org-treat-S-cursor-todo-selection-as-state-change))
  18047. (org-inhibit-blocking
  18048. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18049. (org-call-with-arg 'org-todo 'left)))
  18050. ((or (and org-support-shift-select
  18051. (not (eq org-support-shift-select 'always))
  18052. (org-at-item-bullet-p))
  18053. (and (not org-support-shift-select) (org-at-item-p)))
  18054. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18055. ((and (not (eq org-support-shift-select 'always))
  18056. (org-at-property-p))
  18057. (call-interactively 'org-property-previous-allowed-value))
  18058. ((org-clocktable-try-shift 'left arg))
  18059. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18060. (org-support-shift-select
  18061. (org-call-for-shift-select 'backward-char))
  18062. (t (org-shiftselect-error))))
  18063. (defun org-shiftcontrolright ()
  18064. "Switch to next TODO set."
  18065. (interactive)
  18066. (cond
  18067. ((and org-support-shift-select (org-region-active-p))
  18068. (org-call-for-shift-select 'forward-word))
  18069. ((and (not (eq org-support-shift-select 'always))
  18070. (org-at-heading-p))
  18071. (org-call-with-arg 'org-todo 'nextset))
  18072. (org-support-shift-select
  18073. (org-call-for-shift-select 'forward-word))
  18074. (t (org-shiftselect-error))))
  18075. (defun org-shiftcontrolleft ()
  18076. "Switch to previous TODO set."
  18077. (interactive)
  18078. (cond
  18079. ((and org-support-shift-select (org-region-active-p))
  18080. (org-call-for-shift-select 'backward-word))
  18081. ((and (not (eq org-support-shift-select 'always))
  18082. (org-at-heading-p))
  18083. (org-call-with-arg 'org-todo 'previousset))
  18084. (org-support-shift-select
  18085. (org-call-for-shift-select 'backward-word))
  18086. (t (org-shiftselect-error))))
  18087. (defun org-shiftcontrolup (&optional n)
  18088. "Change timestamps synchronously up in CLOCK log lines.
  18089. Optional argument N tells to change by that many units."
  18090. (interactive "P")
  18091. (cond ((and (not org-support-shift-select)
  18092. (org-at-clock-log-p)
  18093. (org-at-timestamp-p t))
  18094. (org-clock-timestamps-up n))
  18095. (t (org-shiftselect-error))))
  18096. (defun org-shiftcontroldown (&optional n)
  18097. "Change timestamps synchronously down in CLOCK log lines.
  18098. Optional argument N tells to change by that many units."
  18099. (interactive "P")
  18100. (cond ((and (not org-support-shift-select)
  18101. (org-at-clock-log-p)
  18102. (org-at-timestamp-p t))
  18103. (org-clock-timestamps-down n))
  18104. (t (org-shiftselect-error))))
  18105. (defun org-ctrl-c-ret ()
  18106. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18107. (interactive)
  18108. (cond
  18109. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18110. (t (call-interactively 'org-insert-heading))))
  18111. (defun org-find-visible ()
  18112. (let ((s (point)))
  18113. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18114. (get-char-property s 'invisible)))
  18115. s))
  18116. (defun org-find-invisible ()
  18117. (let ((s (point)))
  18118. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18119. (not (get-char-property s 'invisible))))
  18120. s))
  18121. (defun org-copy-visible (beg end)
  18122. "Copy the visible parts of the region."
  18123. (interactive "r")
  18124. (let (snippets s)
  18125. (save-excursion
  18126. (save-restriction
  18127. (narrow-to-region beg end)
  18128. (setq s (goto-char (point-min)))
  18129. (while (not (= (point) (point-max)))
  18130. (goto-char (org-find-invisible))
  18131. (push (buffer-substring s (point)) snippets)
  18132. (setq s (goto-char (org-find-visible))))))
  18133. (kill-new (apply 'concat (nreverse snippets)))))
  18134. (defun org-copy-special ()
  18135. "Copy region in table or copy current subtree.
  18136. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18137. See the individual commands for more information."
  18138. (interactive)
  18139. (call-interactively
  18140. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18141. (defun org-cut-special ()
  18142. "Cut region in table or cut current subtree.
  18143. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18144. See the individual commands for more information."
  18145. (interactive)
  18146. (call-interactively
  18147. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18148. (defun org-paste-special (arg)
  18149. "Paste rectangular region into table, or past subtree relative to level.
  18150. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18151. See the individual commands for more information."
  18152. (interactive "P")
  18153. (if (org-at-table-p)
  18154. (org-table-paste-rectangle)
  18155. (org-paste-subtree arg)))
  18156. (defsubst org-in-fixed-width-region-p ()
  18157. "Is point in a fixed-width region?"
  18158. (save-match-data
  18159. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18160. (defun org-edit-special (&optional arg)
  18161. "Call a special editor for the element at point.
  18162. When at a table, call the formula editor with `org-table-edit-formulas'.
  18163. When in a source code block, call `org-edit-src-code'.
  18164. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18165. When at an #+INCLUDE keyword, visit the included file.
  18166. On a link, call `ffap' to visit the link at point.
  18167. Otherwise, return a user error."
  18168. (interactive)
  18169. (let ((element (org-element-at-point)))
  18170. (assert (not buffer-read-only) nil
  18171. "Buffer is read-only: %s" (buffer-name))
  18172. (case (org-element-type element)
  18173. (src-block
  18174. (if (not arg) (org-edit-src-code)
  18175. (let* ((info (org-babel-get-src-block-info))
  18176. (lang (nth 0 info))
  18177. (params (nth 2 info))
  18178. (session (cdr (assq :session params))))
  18179. (if (not session) (org-edit-src-code)
  18180. ;; At a src-block with a session and function called with
  18181. ;; an ARG: switch to the buffer related to the inferior
  18182. ;; process.
  18183. (funcall (intern (concat "org-babel-prep-session:" lang))
  18184. session params)))))
  18185. (keyword
  18186. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18187. (find-file
  18188. (org-remove-double-quotes
  18189. (car (org-split-string (org-element-property :value element)))))
  18190. (user-error "No special environment to edit here")))
  18191. (table
  18192. (if (eq (org-element-property :type element) 'table.el)
  18193. (org-edit-src-code)
  18194. (call-interactively 'org-table-edit-formulas)))
  18195. ;; Only Org tables contain `table-row' type elements.
  18196. (table-row (call-interactively 'org-table-edit-formulas))
  18197. ((example-block export-block) (org-edit-src-code))
  18198. (fixed-width (org-edit-fixed-width-region))
  18199. (otherwise
  18200. ;; No notable element at point. Though, we may be at a link,
  18201. ;; which is an object. Thus, scan deeper.
  18202. (if (eq (org-element-type (org-element-context element)) 'link)
  18203. (call-interactively 'ffap)
  18204. (user-error "No special environment to edit here"))))))
  18205. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18206. (defun org-ctrl-c-ctrl-c (&optional arg)
  18207. "Set tags in headline, or update according to changed information at point.
  18208. This command does many different things, depending on context:
  18209. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18210. this is what we do.
  18211. - If the cursor is on a statistics cookie, update it.
  18212. - If the cursor is in a headline, prompt for tags and insert them
  18213. into the current line, aligned to `org-tags-column'. When called
  18214. with prefix arg, realign all tags in the current buffer.
  18215. - If the cursor is in one of the special #+KEYWORD lines, this
  18216. triggers scanning the buffer for these lines and updating the
  18217. information.
  18218. - If the cursor is inside a table, realign the table. This command
  18219. works even if the automatic table editor has been turned off.
  18220. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18221. the entire table.
  18222. - If the cursor is at a footnote reference or definition, jump to
  18223. the corresponding definition or references, respectively.
  18224. - If the cursor is a the beginning of a dynamic block, update it.
  18225. - If the current buffer is a capture buffer, close note and file it.
  18226. - If the cursor is on a <<<target>>>, update radio targets and
  18227. corresponding links in this buffer.
  18228. - If the cursor is on a numbered item in a plain list, renumber the
  18229. ordered list.
  18230. - If the cursor is on a checkbox, toggle it.
  18231. - If the cursor is on a code block, evaluate it. The variable
  18232. `org-confirm-babel-evaluate' can be used to control prompting
  18233. before code block evaluation, by default every code block
  18234. evaluation requires confirmation. Code block evaluation can be
  18235. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18236. (interactive "P")
  18237. (cond
  18238. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18239. org-occur-highlights
  18240. org-latex-fragment-image-overlays)
  18241. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18242. (org-remove-occur-highlights)
  18243. (org-remove-latex-fragment-image-overlays)
  18244. (message "Temporary highlights/overlays removed from current buffer"))
  18245. ((and (local-variable-p 'org-finish-function (current-buffer))
  18246. (fboundp org-finish-function))
  18247. (funcall org-finish-function))
  18248. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18249. (t
  18250. (let* ((context (org-element-context)) (type (org-element-type context)))
  18251. ;; Test if point is within a blank line.
  18252. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18253. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18254. (user-error "C-c C-c can do nothing useful at this location"))
  18255. ;; For convenience: at the first line of a paragraph on the
  18256. ;; same line as an item, apply function on that item instead.
  18257. (when (eq type 'paragraph)
  18258. (let ((parent (org-element-property :parent context)))
  18259. (when (and (eq (org-element-type parent) 'item)
  18260. (= (point-at-bol) (org-element-property :begin parent)))
  18261. (setq context parent type 'item))))
  18262. ;; Act according to type of element or object at point.
  18263. (case type
  18264. (clock (org-clock-update-time-maybe))
  18265. (dynamic-block
  18266. (save-excursion
  18267. (goto-char (org-element-property :post-affiliated context))
  18268. (org-update-dblock)))
  18269. (footnote-definition
  18270. (goto-char (org-element-property :post-affiliated context))
  18271. (call-interactively 'org-footnote-action))
  18272. (footnote-reference (call-interactively 'org-footnote-action))
  18273. ((headline inlinetask)
  18274. (save-excursion (goto-char (org-element-property :begin context))
  18275. (call-interactively 'org-set-tags)))
  18276. (item
  18277. ;; At an item: a double C-u set checkbox to "[-]"
  18278. ;; unconditionally, whereas a single one will toggle its
  18279. ;; presence. Without an universal argument, if the item
  18280. ;; has a checkbox, toggle it. Otherwise repair the list.
  18281. (let* ((box (org-element-property :checkbox context))
  18282. (struct (org-element-property :structure context))
  18283. (old-struct (copy-tree struct))
  18284. (parents (org-list-parents-alist struct))
  18285. (prevs (org-list-prevs-alist struct))
  18286. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18287. (org-list-set-checkbox
  18288. (org-element-property :begin context) struct
  18289. (cond ((equal arg '(16)) "[-]")
  18290. ((and (not box) (equal arg '(4))) "[ ]")
  18291. ((or (not box) (equal arg '(4))) nil)
  18292. ((eq box 'on) "[ ]")
  18293. (t "[X]")))
  18294. ;; Mimic `org-list-write-struct' but with grabbing
  18295. ;; a return value from `org-list-struct-fix-box'.
  18296. (org-list-struct-fix-ind struct parents 2)
  18297. (org-list-struct-fix-item-end struct)
  18298. (org-list-struct-fix-bul struct prevs)
  18299. (org-list-struct-fix-ind struct parents)
  18300. (let ((block-item
  18301. (org-list-struct-fix-box struct parents prevs orderedp)))
  18302. (if (and box (equal struct old-struct))
  18303. (if (equal arg '(16))
  18304. (message "Checkboxes already reset")
  18305. (user-error "Cannot toggle this checkbox: %s"
  18306. (if (eq box 'on)
  18307. "all subitems checked"
  18308. "unchecked subitems")))
  18309. (org-list-struct-apply-struct struct old-struct)
  18310. (org-update-checkbox-count-maybe))
  18311. (when block-item
  18312. (message "Checkboxes were removed due to empty box at line %d"
  18313. (org-current-line block-item))))))
  18314. (keyword
  18315. (let ((org-inhibit-startup-visibility-stuff t)
  18316. (org-startup-align-all-tables nil))
  18317. (when (boundp 'org-table-coordinate-overlays)
  18318. (mapc 'delete-overlay org-table-coordinate-overlays)
  18319. (setq org-table-coordinate-overlays nil))
  18320. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18321. (message "Local setup has been refreshed"))
  18322. (plain-list
  18323. ;; At a plain list, with a double C-u argument, set
  18324. ;; checkboxes of each item to "[-]", whereas a single one
  18325. ;; will toggle their presence according to the state of the
  18326. ;; first item in the list. Without an argument, repair the
  18327. ;; list.
  18328. (let* ((begin (org-element-property :contents-begin context))
  18329. (struct (org-element-property :structure context))
  18330. (old-struct (copy-tree struct))
  18331. (first-box (save-excursion
  18332. (goto-char begin)
  18333. (looking-at org-list-full-item-re)
  18334. (match-string-no-properties 3)))
  18335. (new-box (cond ((equal arg '(16)) "[-]")
  18336. ((equal arg '(4)) (unless first-box "[ ]"))
  18337. ((equal first-box "[X]") "[ ]")
  18338. (t "[X]"))))
  18339. (cond
  18340. (arg
  18341. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18342. (org-list-get-all-items
  18343. begin struct (org-list-prevs-alist struct))))
  18344. ((and first-box (eq (point) begin))
  18345. ;; For convenience, when point is at bol on the first
  18346. ;; item of the list and no argument is provided, simply
  18347. ;; toggle checkbox of that item, if any.
  18348. (org-list-set-checkbox begin struct new-box)))
  18349. (org-list-write-struct
  18350. struct (org-list-parents-alist struct) old-struct)
  18351. (org-update-checkbox-count-maybe)
  18352. (save-excursion (goto-char begin) (org-list-send-list 'maybe))))
  18353. ((property-drawer node-property)
  18354. (call-interactively 'org-property-action))
  18355. ((radio-target target)
  18356. (call-interactively 'org-update-radio-target-regexp))
  18357. (statistics-cookie
  18358. (call-interactively 'org-update-statistics-cookies))
  18359. ((table table-cell table-row)
  18360. ;; At a table, recalculate every field and align it. Also
  18361. ;; send the table if necessary. If the table has
  18362. ;; a `table.el' type, just give up. At a table row or
  18363. ;; cell, maybe recalculate line but always align table.
  18364. (if (eq (org-element-property :type context) 'table.el)
  18365. (message "Use C-c ' to edit table.el tables")
  18366. (let ((org-enable-table-editor t))
  18367. (if (or (eq type 'table)
  18368. ;; Check if point is at a TBLFM line.
  18369. (and (eq type 'table-row)
  18370. (= (point) (org-element-property :end context))))
  18371. (save-excursion
  18372. (goto-char (org-element-property :contents-begin context))
  18373. (org-call-with-arg 'org-table-recalculate (or arg t))
  18374. (orgtbl-send-table 'maybe))
  18375. (org-table-maybe-eval-formula)
  18376. (cond (arg (call-interactively 'org-table-recalculate))
  18377. ((org-table-maybe-recalculate-line))
  18378. (t (org-table-align)))))))
  18379. (timestamp (org-timestamp-change 0 'day))
  18380. (otherwise
  18381. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18382. (user-error
  18383. "C-c C-c can do nothing useful at this location")))))))))
  18384. (defun org-mode-restart ()
  18385. "Restart Org-mode, to scan again for special lines.
  18386. Also updates the keyword regular expressions."
  18387. (interactive)
  18388. (org-mode)
  18389. (message "Org-mode restarted"))
  18390. (defun org-kill-note-or-show-branches ()
  18391. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18392. (interactive)
  18393. (if (not org-finish-function)
  18394. (progn
  18395. (hide-subtree)
  18396. (call-interactively 'show-branches))
  18397. (let ((org-note-abort t))
  18398. (funcall org-finish-function))))
  18399. (defun org-return (&optional indent)
  18400. "Goto next table row or insert a newline.
  18401. Calls `org-table-next-row' or `newline', depending on context.
  18402. See the individual commands for more information."
  18403. (interactive)
  18404. (let (org-ts-what)
  18405. (cond
  18406. ((or (bobp) (org-in-src-block-p))
  18407. (if indent (newline-and-indent) (newline)))
  18408. ((org-at-table-p)
  18409. (org-table-justify-field-maybe)
  18410. (call-interactively 'org-table-next-row))
  18411. ;; when `newline-and-indent' is called within a list, make sure
  18412. ;; text moved stays inside the item.
  18413. ((and (org-in-item-p) indent)
  18414. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18415. (progn
  18416. (save-match-data (newline))
  18417. (org-indent-line-to (length (match-string 0))))
  18418. (let ((ind (org-get-indentation)))
  18419. (newline)
  18420. (if (org-looking-back org-list-end-re)
  18421. (org-indent-line)
  18422. (org-indent-line-to ind)))))
  18423. ((and org-return-follows-link
  18424. (org-at-timestamp-p t)
  18425. (not (eq org-ts-what 'after)))
  18426. (org-follow-timestamp-link))
  18427. ((and org-return-follows-link
  18428. (let ((tprop (get-text-property (point) 'face)))
  18429. (or (eq tprop 'org-link)
  18430. (and (listp tprop) (memq 'org-link tprop)))))
  18431. (call-interactively 'org-open-at-point))
  18432. ((and (org-at-heading-p)
  18433. (looking-at
  18434. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18435. (org-show-entry)
  18436. (end-of-line 1)
  18437. (newline))
  18438. (t (if indent (newline-and-indent) (newline))))))
  18439. (defun org-return-indent ()
  18440. "Goto next table row or insert a newline and indent.
  18441. Calls `org-table-next-row' or `newline-and-indent', depending on
  18442. context. See the individual commands for more information."
  18443. (interactive)
  18444. (org-return t))
  18445. (defun org-ctrl-c-star ()
  18446. "Compute table, or change heading status of lines.
  18447. Calls `org-table-recalculate' or `org-toggle-heading',
  18448. depending on context."
  18449. (interactive)
  18450. (cond
  18451. ((org-at-table-p)
  18452. (call-interactively 'org-table-recalculate))
  18453. (t
  18454. ;; Convert all lines in region to list items
  18455. (call-interactively 'org-toggle-heading))))
  18456. (defun org-ctrl-c-minus ()
  18457. "Insert separator line in table or modify bullet status of line.
  18458. Also turns a plain line or a region of lines into list items.
  18459. Calls `org-table-insert-hline', `org-toggle-item', or
  18460. `org-cycle-list-bullet', depending on context."
  18461. (interactive)
  18462. (cond
  18463. ((org-at-table-p)
  18464. (call-interactively 'org-table-insert-hline))
  18465. ((org-region-active-p)
  18466. (call-interactively 'org-toggle-item))
  18467. ((org-in-item-p)
  18468. (call-interactively 'org-cycle-list-bullet))
  18469. (t
  18470. (call-interactively 'org-toggle-item))))
  18471. (defun org-toggle-item (arg)
  18472. "Convert headings or normal lines to items, items to normal lines.
  18473. If there is no active region, only the current line is considered.
  18474. If the first non blank line in the region is a headline, convert
  18475. all headlines to items, shifting text accordingly.
  18476. If it is an item, convert all items to normal lines.
  18477. If it is normal text, change region into a list of items.
  18478. With a prefix argument ARG, change the region in a single item."
  18479. (interactive "P")
  18480. (let ((shift-text
  18481. (function
  18482. ;; Shift text in current section to IND, from point to END.
  18483. ;; The function leaves point to END line.
  18484. (lambda (ind end)
  18485. (let ((min-i 1000) (end (copy-marker end)))
  18486. ;; First determine the minimum indentation (MIN-I) of
  18487. ;; the text.
  18488. (save-excursion
  18489. (catch 'exit
  18490. (while (< (point) end)
  18491. (let ((i (org-get-indentation)))
  18492. (cond
  18493. ;; Skip blank lines and inline tasks.
  18494. ((looking-at "^[ \t]*$"))
  18495. ((looking-at org-outline-regexp-bol))
  18496. ;; We can't find less than 0 indentation.
  18497. ((zerop i) (throw 'exit (setq min-i 0)))
  18498. ((< i min-i) (setq min-i i))))
  18499. (forward-line))))
  18500. ;; Then indent each line so that a line indented to
  18501. ;; MIN-I becomes indented to IND. Ignore blank lines
  18502. ;; and inline tasks in the process.
  18503. (let ((delta (- ind min-i)))
  18504. (while (< (point) end)
  18505. (unless (or (looking-at "^[ \t]*$")
  18506. (looking-at org-outline-regexp-bol))
  18507. (org-indent-line-to (+ (org-get-indentation) delta)))
  18508. (forward-line)))))))
  18509. (skip-blanks
  18510. (function
  18511. ;; Return beginning of first non-blank line, starting from
  18512. ;; line at POS.
  18513. (lambda (pos)
  18514. (save-excursion
  18515. (goto-char pos)
  18516. (skip-chars-forward " \r\t\n")
  18517. (point-at-bol)))))
  18518. beg end)
  18519. ;; Determine boundaries of changes.
  18520. (if (org-region-active-p)
  18521. (setq beg (funcall skip-blanks (region-beginning))
  18522. end (copy-marker (region-end)))
  18523. (setq beg (funcall skip-blanks (point-at-bol))
  18524. end (copy-marker (point-at-eol))))
  18525. ;; Depending on the starting line, choose an action on the text
  18526. ;; between BEG and END.
  18527. (org-with-limited-levels
  18528. (save-excursion
  18529. (goto-char beg)
  18530. (cond
  18531. ;; Case 1. Start at an item: de-itemize. Note that it only
  18532. ;; happens when a region is active: `org-ctrl-c-minus'
  18533. ;; would call `org-cycle-list-bullet' otherwise.
  18534. ((org-at-item-p)
  18535. (while (< (point) end)
  18536. (when (org-at-item-p)
  18537. (skip-chars-forward " \t")
  18538. (delete-region (point) (match-end 0)))
  18539. (forward-line)))
  18540. ;; Case 2. Start at an heading: convert to items.
  18541. ((org-at-heading-p)
  18542. (let* ((bul (org-list-bullet-string "-"))
  18543. (bul-len (length bul))
  18544. ;; Indentation of the first heading. It should be
  18545. ;; relative to the indentation of its parent, if any.
  18546. (start-ind (save-excursion
  18547. (cond
  18548. ((not org-adapt-indentation) 0)
  18549. ((not (outline-previous-heading)) 0)
  18550. (t (length (match-string 0))))))
  18551. ;; Level of first heading. Further headings will be
  18552. ;; compared to it to determine hierarchy in the list.
  18553. (ref-level (org-reduced-level (org-outline-level))))
  18554. (while (< (point) end)
  18555. (let* ((level (org-reduced-level (org-outline-level)))
  18556. (delta (max 0 (- level ref-level))))
  18557. ;; If current headline is less indented than the first
  18558. ;; one, set it as reference, in order to preserve
  18559. ;; subtrees.
  18560. (when (< level ref-level) (setq ref-level level))
  18561. (replace-match bul t t)
  18562. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18563. ;; Ensure all text down to END (or SECTION-END) belongs
  18564. ;; to the newly created item.
  18565. (let ((section-end (save-excursion
  18566. (or (outline-next-heading) (point)))))
  18567. (forward-line)
  18568. (funcall shift-text
  18569. (+ start-ind (* (1+ delta) bul-len))
  18570. (min end section-end)))))))
  18571. ;; Case 3. Normal line with ARG: make the first line of region
  18572. ;; an item, and shift indentation of others lines to
  18573. ;; set them as item's body.
  18574. (arg (let* ((bul (org-list-bullet-string "-"))
  18575. (bul-len (length bul))
  18576. (ref-ind (org-get-indentation)))
  18577. (skip-chars-forward " \t")
  18578. (insert bul)
  18579. (forward-line)
  18580. (while (< (point) end)
  18581. ;; Ensure that lines less indented than first one
  18582. ;; still get included in item body.
  18583. (funcall shift-text
  18584. (+ ref-ind bul-len)
  18585. (min end (save-excursion (or (outline-next-heading)
  18586. (point)))))
  18587. (forward-line))))
  18588. ;; Case 4. Normal line without ARG: turn each non-item line
  18589. ;; into an item.
  18590. (t
  18591. (while (< (point) end)
  18592. (unless (or (org-at-heading-p) (org-at-item-p))
  18593. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18594. (replace-match
  18595. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18596. (forward-line))))))))
  18597. (defun org-toggle-heading (&optional nstars)
  18598. "Convert headings to normal text, or items or text to headings.
  18599. If there is no active region, only convert the current line.
  18600. With a \\[universal-argument] prefix, convert the whole list at
  18601. point into heading.
  18602. In a region:
  18603. - If the first non blank line is a headline, remove the stars
  18604. from all headlines in the region.
  18605. - If it is a normal line, turn each and every normal line (i.e.,
  18606. not an heading or an item) in the region into headings. If you
  18607. want to convert only the first line of this region, use one
  18608. universal prefix argument.
  18609. - If it is a plain list item, turn all plain list items into headings.
  18610. When converting a line into a heading, the number of stars is chosen
  18611. such that the lines become children of the current entry. However,
  18612. when a numeric prefix argument is given, its value determines the
  18613. number of stars to add."
  18614. (interactive "P")
  18615. (let ((skip-blanks
  18616. (function
  18617. ;; Return beginning of first non-blank line, starting from
  18618. ;; line at POS.
  18619. (lambda (pos)
  18620. (save-excursion
  18621. (goto-char pos)
  18622. (while (org-at-comment-p) (forward-line))
  18623. (skip-chars-forward " \r\t\n")
  18624. (point-at-bol)))))
  18625. beg end toggled)
  18626. ;; Determine boundaries of changes. If a universal prefix has
  18627. ;; been given, put the list in a region. If region ends at a bol,
  18628. ;; do not consider the last line to be in the region.
  18629. (when (and current-prefix-arg (org-at-item-p))
  18630. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  18631. (org-mark-element))
  18632. (if (org-region-active-p)
  18633. (setq beg (funcall skip-blanks (region-beginning))
  18634. end (copy-marker (save-excursion
  18635. (goto-char (region-end))
  18636. (if (bolp) (point) (point-at-eol)))))
  18637. (setq beg (funcall skip-blanks (point-at-bol))
  18638. end (copy-marker (point-at-eol))))
  18639. ;; Ensure inline tasks don't count as headings.
  18640. (org-with-limited-levels
  18641. (save-excursion
  18642. (goto-char beg)
  18643. (cond
  18644. ;; Case 1. Started at an heading: de-star headings.
  18645. ((org-at-heading-p)
  18646. (while (< (point) end)
  18647. (when (org-at-heading-p t)
  18648. (looking-at org-outline-regexp) (replace-match "")
  18649. (setq toggled t))
  18650. (forward-line)))
  18651. ;; Case 2. Started at an item: change items into headlines.
  18652. ;; One star will be added by `org-list-to-subtree'.
  18653. ((org-at-item-p)
  18654. (let* ((stars (make-string
  18655. ;; subtract the star that will be added again by
  18656. ;; `org-list-to-subtree'
  18657. (if (numberp nstars) (1- nstars)
  18658. (or (org-current-level) 0))
  18659. ?*))
  18660. (add-stars
  18661. (cond (nstars "") ; stars from prefix only
  18662. ((equal stars "") "") ; before first heading
  18663. (org-odd-levels-only "*") ; inside heading, odd
  18664. (t "")))) ; inside heading, oddeven
  18665. (while (< (point) end)
  18666. (when (org-at-item-p)
  18667. ;; Pay attention to cases when region ends before list.
  18668. (let* ((struct (org-list-struct))
  18669. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18670. (save-restriction
  18671. (narrow-to-region (point) list-end)
  18672. (insert
  18673. (org-list-to-subtree
  18674. (org-list-parse-list t)
  18675. '(:istart (concat stars add-stars (funcall get-stars depth))
  18676. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18677. (setq toggled t))
  18678. (forward-line))))
  18679. ;; Case 3. Started at normal text: make every line an heading,
  18680. ;; skipping headlines and items.
  18681. (t (let* ((stars
  18682. (make-string
  18683. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  18684. (add-stars
  18685. (cond (nstars "") ; stars from prefix only
  18686. ((equal stars "") "*") ; before first heading
  18687. (org-odd-levels-only "**") ; inside heading, odd
  18688. (t "*"))) ; inside heading, oddeven
  18689. (rpl (concat stars add-stars " "))
  18690. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  18691. (while (< (point) (if (equal nstars '(4)) lend end))
  18692. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18693. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18694. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18695. (forward-line)))))))
  18696. (unless toggled (message "Cannot toggle heading from here"))))
  18697. (defun org-meta-return (&optional arg)
  18698. "Insert a new heading or wrap a region in a table.
  18699. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18700. See the individual commands for more information."
  18701. (interactive "P")
  18702. (cond
  18703. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18704. ((or (org-at-drawer-p) (org-at-property-p))
  18705. (newline-and-indent))
  18706. ((org-at-table-p)
  18707. (call-interactively 'org-table-wrap-region))
  18708. (t (call-interactively 'org-insert-heading))))
  18709. ;;; Menu entries
  18710. (defsubst org-in-subtree-not-table-p ()
  18711. "Are we in a subtree and not in a table?"
  18712. (and (not (org-before-first-heading-p))
  18713. (not (org-at-table-p))))
  18714. ;; Define the Org-mode menus
  18715. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18716. '("Tbl"
  18717. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18718. ["Next Field" org-cycle (org-at-table-p)]
  18719. ["Previous Field" org-shifttab (org-at-table-p)]
  18720. ["Next Row" org-return (org-at-table-p)]
  18721. "--"
  18722. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18723. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18724. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18725. "--"
  18726. ("Column"
  18727. ["Move Column Left" org-metaleft (org-at-table-p)]
  18728. ["Move Column Right" org-metaright (org-at-table-p)]
  18729. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18730. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18731. ("Row"
  18732. ["Move Row Up" org-metaup (org-at-table-p)]
  18733. ["Move Row Down" org-metadown (org-at-table-p)]
  18734. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18735. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18736. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18737. "--"
  18738. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18739. ("Rectangle"
  18740. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18741. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18742. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18743. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18744. "--"
  18745. ("Calculate"
  18746. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18747. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18748. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18749. "--"
  18750. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18751. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18752. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18753. "--"
  18754. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18755. "--"
  18756. ["Sum Column/Rectangle" org-table-sum
  18757. (or (org-at-table-p) (org-region-active-p))]
  18758. ["Which Column?" org-table-current-column (org-at-table-p)])
  18759. ["Debug Formulas"
  18760. org-table-toggle-formula-debugger
  18761. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18762. ["Show Col/Row Numbers"
  18763. org-table-toggle-coordinate-overlays
  18764. :style toggle
  18765. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18766. "--"
  18767. ["Create" org-table-create (and (not (org-at-table-p))
  18768. org-enable-table-editor)]
  18769. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18770. ["Import from File" org-table-import (not (org-at-table-p))]
  18771. ["Export to File" org-table-export (org-at-table-p)]
  18772. "--"
  18773. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18774. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18775. '("Org"
  18776. ("Show/Hide"
  18777. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18778. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18779. ["Sparse Tree..." org-sparse-tree t]
  18780. ["Reveal Context" org-reveal t]
  18781. ["Show All" show-all t]
  18782. "--"
  18783. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18784. "--"
  18785. ["New Heading" org-insert-heading t]
  18786. ("Navigate Headings"
  18787. ["Up" outline-up-heading t]
  18788. ["Next" outline-next-visible-heading t]
  18789. ["Previous" outline-previous-visible-heading t]
  18790. ["Next Same Level" outline-forward-same-level t]
  18791. ["Previous Same Level" outline-backward-same-level t]
  18792. "--"
  18793. ["Jump" org-goto t])
  18794. ("Edit Structure"
  18795. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18796. "--"
  18797. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18798. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18799. "--"
  18800. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18801. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18802. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18803. "--"
  18804. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18805. "--"
  18806. ["Copy visible text" org-copy-visible t]
  18807. "--"
  18808. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18809. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18810. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18811. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18812. "--"
  18813. ["Sort Region/Children" org-sort t]
  18814. "--"
  18815. ["Convert to odd levels" org-convert-to-odd-levels t]
  18816. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18817. ("Editing"
  18818. ["Emphasis..." org-emphasize t]
  18819. ["Edit Source Example" org-edit-special t]
  18820. "--"
  18821. ["Footnote new/jump" org-footnote-action t]
  18822. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18823. ("Archive"
  18824. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18825. "--"
  18826. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18827. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18828. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18829. )
  18830. "--"
  18831. ("Hyperlinks"
  18832. ["Store Link (Global)" org-store-link t]
  18833. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18834. ["Insert Link" org-insert-link t]
  18835. ["Follow Link" org-open-at-point t]
  18836. "--"
  18837. ["Next link" org-next-link t]
  18838. ["Previous link" org-previous-link t]
  18839. "--"
  18840. ["Descriptive Links"
  18841. org-toggle-link-display
  18842. :style radio
  18843. :selected org-descriptive-links
  18844. ]
  18845. ["Literal Links"
  18846. org-toggle-link-display
  18847. :style radio
  18848. :selected (not org-descriptive-links)])
  18849. "--"
  18850. ("TODO Lists"
  18851. ["TODO/DONE/-" org-todo t]
  18852. ("Select keyword"
  18853. ["Next keyword" org-shiftright (org-at-heading-p)]
  18854. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18855. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18856. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18857. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18858. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18859. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18860. "--"
  18861. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18862. :selected org-enforce-todo-dependencies :style toggle :active t]
  18863. "Settings for tree at point"
  18864. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18865. :selected (org-entry-get nil "ORDERED")
  18866. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18867. ["Do Children parallel" org-toggle-ordered-property :style radio
  18868. :selected (not (org-entry-get nil "ORDERED"))
  18869. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18870. "--"
  18871. ["Set Priority" org-priority t]
  18872. ["Priority Up" org-shiftup t]
  18873. ["Priority Down" org-shiftdown t]
  18874. "--"
  18875. ["Get news from all feeds" org-feed-update-all t]
  18876. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18877. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18878. ("TAGS and Properties"
  18879. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18880. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18881. "--"
  18882. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18883. ["Column view of properties" org-columns t]
  18884. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18885. ("Dates and Scheduling"
  18886. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18887. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18888. ("Change Date"
  18889. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18890. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18891. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18892. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18893. ["Compute Time Range" org-evaluate-time-range t]
  18894. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18895. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18896. "--"
  18897. ["Custom time format" org-toggle-time-stamp-overlays
  18898. :style radio :selected org-display-custom-times]
  18899. "--"
  18900. ["Goto Calendar" org-goto-calendar t]
  18901. ["Date from Calendar" org-date-from-calendar t]
  18902. "--"
  18903. ["Start/Restart Timer" org-timer-start t]
  18904. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18905. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18906. ["Insert Timer String" org-timer t]
  18907. ["Insert Timer Item" org-timer-item t])
  18908. ("Logging work"
  18909. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18910. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18911. ["Clock out" org-clock-out t]
  18912. ["Clock cancel" org-clock-cancel t]
  18913. "--"
  18914. ["Mark as default task" org-clock-mark-default-task t]
  18915. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18916. ["Goto running clock" org-clock-goto t]
  18917. "--"
  18918. ["Display times" org-clock-display t]
  18919. ["Create clock table" org-clock-report t]
  18920. "--"
  18921. ["Record DONE time"
  18922. (progn (setq org-log-done (not org-log-done))
  18923. (message "Switching to %s will %s record a timestamp"
  18924. (car org-done-keywords)
  18925. (if org-log-done "automatically" "not")))
  18926. :style toggle :selected org-log-done])
  18927. "--"
  18928. ["Agenda Command..." org-agenda t]
  18929. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18930. ("File List for Agenda")
  18931. ("Special views current file"
  18932. ["TODO Tree" org-show-todo-tree t]
  18933. ["Check Deadlines" org-check-deadlines t]
  18934. ["Timeline" org-timeline t]
  18935. ["Tags/Property tree" org-match-sparse-tree t])
  18936. "--"
  18937. ["Export/Publish..." org-export-dispatch t]
  18938. ("LaTeX"
  18939. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18940. :selected org-cdlatex-mode]
  18941. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18942. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18943. ["Modify math symbol" org-cdlatex-math-modify
  18944. (org-inside-LaTeX-fragment-p)]
  18945. ["Insert citation" org-reftex-citation t]
  18946. "--"
  18947. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  18948. "--"
  18949. ("MobileOrg"
  18950. ["Push Files and Views" org-mobile-push t]
  18951. ["Get Captured and Flagged" org-mobile-pull t]
  18952. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18953. "--"
  18954. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18955. "--"
  18956. ("Documentation"
  18957. ["Show Version" org-version t]
  18958. ["Info Documentation" org-info t])
  18959. ("Customize"
  18960. ["Browse Org Group" org-customize t]
  18961. "--"
  18962. ["Expand This Menu" org-create-customize-menu
  18963. (fboundp 'customize-menu-create)])
  18964. ["Send bug report" org-submit-bug-report t]
  18965. "--"
  18966. ("Refresh/Reload"
  18967. ["Refresh setup current buffer" org-mode-restart t]
  18968. ["Reload Org (after update)" org-reload t]
  18969. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  18970. ))
  18971. (defun org-info (&optional node)
  18972. "Read documentation for Org-mode in the info system.
  18973. With optional NODE, go directly to that node."
  18974. (interactive)
  18975. (info (format "(org)%s" (or node ""))))
  18976. ;;;###autoload
  18977. (defun org-submit-bug-report ()
  18978. "Submit a bug report on Org-mode via mail.
  18979. Don't hesitate to report any problems or inaccurate documentation.
  18980. If you don't have setup sending mail from (X)Emacs, please copy the
  18981. output buffer into your mail program, as it gives us important
  18982. information about your Org-mode version and configuration."
  18983. (interactive)
  18984. (require 'reporter)
  18985. (org-load-modules-maybe)
  18986. (org-require-autoloaded-modules)
  18987. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18988. (reporter-submit-bug-report
  18989. "emacs-orgmode@gnu.org"
  18990. (org-version nil 'full)
  18991. (let (list)
  18992. (save-window-excursion
  18993. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18994. (delete-other-windows)
  18995. (erase-buffer)
  18996. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18997. We would like to add your full Org-mode and Outline configuration to the
  18998. bug report. This greatly simplifies the work of the maintainer and
  18999. other experts on the mailing list.
  19000. HOWEVER, some variables you have customized may contain private
  19001. information. The names of customers, colleagues, or friends, might
  19002. appear in the form of file names, tags, todo states, or search strings.
  19003. If you answer yes to the prompt, you might want to check and remove
  19004. such private information before sending the email.")
  19005. (add-text-properties (point-min) (point-max) '(face org-warning))
  19006. (when (yes-or-no-p "Include your Org-mode configuration ")
  19007. (mapatoms
  19008. (lambda (v)
  19009. (and (boundp v)
  19010. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19011. (or (and (symbol-value v)
  19012. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19013. (and
  19014. (get v 'custom-type) (get v 'standard-value)
  19015. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19016. (push v list)))))
  19017. (kill-buffer (get-buffer "*Warn about privacy*"))
  19018. list))
  19019. nil nil
  19020. "Remember to cover the basics, that is, what you expected to happen and
  19021. what in fact did happen. You don't know how to make a good report? See
  19022. http://orgmode.org/manual/Feedback.html#Feedback
  19023. Your bug report will be posted to the Org-mode mailing list.
  19024. ------------------------------------------------------------------------")
  19025. (save-excursion
  19026. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19027. (replace-match "\\1Bug: \\3 [\\2]")))))
  19028. (defun org-install-agenda-files-menu ()
  19029. (let ((bl (buffer-list)))
  19030. (save-excursion
  19031. (while bl
  19032. (set-buffer (pop bl))
  19033. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19034. (when (derived-mode-p 'org-mode)
  19035. (easy-menu-change
  19036. '("Org") "File List for Agenda"
  19037. (append
  19038. (list
  19039. ["Edit File List" (org-edit-agenda-file-list) t]
  19040. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19041. ["Remove Current File from List" org-remove-file t]
  19042. ["Cycle through agenda files" org-cycle-agenda-files t]
  19043. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19044. "--")
  19045. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19046. ;;;; Documentation
  19047. (defun org-require-autoloaded-modules ()
  19048. (interactive)
  19049. (mapc 'require
  19050. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19051. org-remember org-table org-timer)))
  19052. ;;;###autoload
  19053. (defun org-reload (&optional uncompiled)
  19054. "Reload all org lisp files.
  19055. With prefix arg UNCOMPILED, load the uncompiled versions."
  19056. (interactive "P")
  19057. (require 'loadhist)
  19058. (let* ((org-dir (org-find-library-dir "org"))
  19059. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19060. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19061. (remove-re (mapconcat 'identity
  19062. (mapcar (lambda (f) (concat "^" f "$"))
  19063. (list (if (featurep 'xemacs)
  19064. "org-colview"
  19065. "org-colview-xemacs")
  19066. "org" "org-loaddefs" "org-version"))
  19067. "\\|"))
  19068. (feats (delete-dups
  19069. (mapcar 'file-name-sans-extension
  19070. (mapcar 'file-name-nondirectory
  19071. (delq nil
  19072. (mapcar 'feature-file
  19073. features))))))
  19074. (lfeat (append
  19075. (sort
  19076. (setq feats
  19077. (delq nil (mapcar
  19078. (lambda (f)
  19079. (if (and (string-match feature-re f)
  19080. (not (string-match remove-re f)))
  19081. f nil))
  19082. feats)))
  19083. 'string-lessp)
  19084. (list "org-version" "org")))
  19085. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19086. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19087. load-uncore load-misses)
  19088. (setq load-misses
  19089. (delq 't
  19090. (mapcar (lambda (f)
  19091. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19092. (and (string= org-dir contrib-dir)
  19093. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19094. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19095. (add-to-list 'load-uncore f 'append)
  19096. 't)
  19097. f))
  19098. lfeat)))
  19099. (if load-uncore
  19100. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19101. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19102. (if load-misses
  19103. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19104. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19105. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19106. ;;;###autoload
  19107. (defun org-customize ()
  19108. "Call the customize function with org as argument."
  19109. (interactive)
  19110. (org-load-modules-maybe)
  19111. (org-require-autoloaded-modules)
  19112. (customize-browse 'org))
  19113. (defun org-create-customize-menu ()
  19114. "Create a full customization menu for Org-mode, insert it into the menu."
  19115. (interactive)
  19116. (org-load-modules-maybe)
  19117. (org-require-autoloaded-modules)
  19118. (if (fboundp 'customize-menu-create)
  19119. (progn
  19120. (easy-menu-change
  19121. '("Org") "Customize"
  19122. `(["Browse Org group" org-customize t]
  19123. "--"
  19124. ,(customize-menu-create 'org)
  19125. ["Set" Custom-set t]
  19126. ["Save" Custom-save t]
  19127. ["Reset to Current" Custom-reset-current t]
  19128. ["Reset to Saved" Custom-reset-saved t]
  19129. ["Reset to Standard Settings" Custom-reset-standard t]))
  19130. (message "\"Org\"-menu now contains full customization menu"))
  19131. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19132. ;;;; Miscellaneous stuff
  19133. ;;; Generally useful functions
  19134. (defun org-get-at-bol (property)
  19135. "Get text property PROPERTY at beginning of line."
  19136. (get-text-property (point-at-bol) property))
  19137. (defun org-find-text-property-in-string (prop s)
  19138. "Return the first non-nil value of property PROP in string S."
  19139. (or (get-text-property 0 prop s)
  19140. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19141. prop s)))
  19142. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19143. "Display the given MESSAGE as a warning."
  19144. (if (fboundp 'display-warning)
  19145. (display-warning 'org message
  19146. (if (featurep 'xemacs) 'warning :warning))
  19147. (let ((buf (get-buffer-create "*Org warnings*")))
  19148. (with-current-buffer buf
  19149. (goto-char (point-max))
  19150. (insert "Warning (Org): " message)
  19151. (unless (bolp)
  19152. (newline)))
  19153. (display-buffer buf)
  19154. (sit-for 0))))
  19155. (defun org-eval (form)
  19156. "Eval FORM and return result."
  19157. (condition-case error
  19158. (eval form)
  19159. (error (format "%%![Error: %s]" error))))
  19160. (defun org-in-clocktable-p ()
  19161. "Check if the cursor is in a clocktable."
  19162. (let ((pos (point)) start)
  19163. (save-excursion
  19164. (end-of-line 1)
  19165. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19166. (setq start (match-beginning 0))
  19167. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19168. (>= (match-end 0) pos)
  19169. start))))
  19170. (defun org-in-commented-line ()
  19171. "Is point in a line starting with `#'?"
  19172. (equal (char-after (point-at-bol)) ?#))
  19173. (defun org-in-indented-comment-line ()
  19174. "Is point in a line starting with `#' after some white space?"
  19175. (save-excursion
  19176. (save-match-data
  19177. (goto-char (point-at-bol))
  19178. (looking-at "[ \t]*#"))))
  19179. (defun org-in-verbatim-emphasis ()
  19180. (save-match-data
  19181. (and (org-in-regexp org-emph-re 2)
  19182. (>= (point) (match-beginning 3))
  19183. (<= (point) (match-end 4))
  19184. (member (match-string 3) '("=" "~")))))
  19185. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19186. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19187. (if (and marker (marker-buffer marker)
  19188. (buffer-live-p (marker-buffer marker)))
  19189. (progn
  19190. (org-pop-to-buffer-same-window (marker-buffer marker))
  19191. (if (or (> marker (point-max)) (< marker (point-min)))
  19192. (widen))
  19193. (goto-char marker)
  19194. (org-show-context 'org-goto))
  19195. (if bookmark
  19196. (bookmark-jump bookmark)
  19197. (error "Cannot find location"))))
  19198. (defun org-quote-csv-field (s)
  19199. "Quote field for inclusion in CSV material."
  19200. (if (string-match "[\",]" s)
  19201. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19202. s))
  19203. (defun org-force-self-insert (N)
  19204. "Needed to enforce self-insert under remapping."
  19205. (interactive "p")
  19206. (self-insert-command N))
  19207. (defun org-string-width (s)
  19208. "Compute width of string, ignoring invisible characters.
  19209. This ignores character with invisibility property `org-link', and also
  19210. characters with property `org-cwidth', because these will become invisible
  19211. upon the next fontification round."
  19212. (let (b l)
  19213. (when (or (eq t buffer-invisibility-spec)
  19214. (assq 'org-link buffer-invisibility-spec))
  19215. (while (setq b (text-property-any 0 (length s)
  19216. 'invisible 'org-link s))
  19217. (setq s (concat (substring s 0 b)
  19218. (substring s (or (next-single-property-change
  19219. b 'invisible s) (length s)))))))
  19220. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19221. (setq s (concat (substring s 0 b)
  19222. (substring s (or (next-single-property-change
  19223. b 'org-cwidth s) (length s))))))
  19224. (setq l (string-width s) b -1)
  19225. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19226. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19227. l))
  19228. (defun org-shorten-string (s maxlength)
  19229. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19230. If the string is shorter or has length MAXLENGTH, just return the
  19231. original string. If it is longer, the functions finds a space in the
  19232. string, breaks this string off at that locations and adds three dots
  19233. as ellipsis. Including the ellipsis, the string will not be longer
  19234. than MAXLENGTH. If finding a good breaking point in the string does
  19235. not work, the string is just chopped off in the middle of a word
  19236. if necessary."
  19237. (if (<= (length s) maxlength)
  19238. s
  19239. (let* ((n (max (- maxlength 4) 1))
  19240. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19241. (if (string-match re s)
  19242. (concat (match-string 1 s) "...")
  19243. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19244. (defun org-get-indentation (&optional line)
  19245. "Get the indentation of the current line, interpreting tabs.
  19246. When LINE is given, assume it represents a line and compute its indentation."
  19247. (if line
  19248. (if (string-match "^ *" (org-remove-tabs line))
  19249. (match-end 0))
  19250. (save-excursion
  19251. (beginning-of-line 1)
  19252. (skip-chars-forward " \t")
  19253. (current-column))))
  19254. (defun org-get-string-indentation (s)
  19255. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19256. (let ((n -1) (i 0) (w tab-width) c)
  19257. (catch 'exit
  19258. (while (< (setq n (1+ n)) (length s))
  19259. (setq c (aref s n))
  19260. (cond ((= c ?\ ) (setq i (1+ i)))
  19261. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19262. (t (throw 'exit t)))))
  19263. i))
  19264. (defun org-remove-tabs (s &optional width)
  19265. "Replace tabulators in S with spaces.
  19266. Assumes that s is a single line, starting in column 0."
  19267. (setq width (or width tab-width))
  19268. (while (string-match "\t" s)
  19269. (setq s (replace-match
  19270. (make-string
  19271. (- (* width (/ (+ (match-beginning 0) width) width))
  19272. (match-beginning 0)) ?\ )
  19273. t t s)))
  19274. s)
  19275. (defun org-fix-indentation (line ind)
  19276. "Fix indentation in LINE.
  19277. IND is a cons cell with target and minimum indentation.
  19278. If the current indentation in LINE is smaller than the minimum,
  19279. leave it alone. If it is larger than ind, set it to the target."
  19280. (let* ((l (org-remove-tabs line))
  19281. (i (org-get-indentation l))
  19282. (i1 (car ind)) (i2 (cdr ind)))
  19283. (if (>= i i2) (setq l (substring line i2)))
  19284. (if (> i1 0)
  19285. (concat (make-string i1 ?\ ) l)
  19286. l)))
  19287. (defun org-remove-indentation (code &optional n)
  19288. "Remove the maximum common indentation from the lines in CODE.
  19289. N may optionally be the number of spaces to remove."
  19290. (with-temp-buffer
  19291. (insert code)
  19292. (org-do-remove-indentation n)
  19293. (buffer-string)))
  19294. (defun org-do-remove-indentation (&optional n)
  19295. "Remove the maximum common indentation from the buffer."
  19296. (untabify (point-min) (point-max))
  19297. (let ((min 10000) re)
  19298. (if n
  19299. (setq min n)
  19300. (goto-char (point-min))
  19301. (while (re-search-forward "^ *[^ \n]" nil t)
  19302. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19303. (unless (or (= min 0) (= min 10000))
  19304. (setq re (format "^ \\{%d\\}" min))
  19305. (goto-char (point-min))
  19306. (while (re-search-forward re nil t)
  19307. (replace-match "")
  19308. (end-of-line 1))
  19309. min)))
  19310. (defun org-fill-template (template alist)
  19311. "Find each %key of ALIST in TEMPLATE and replace it."
  19312. (let ((case-fold-search nil)
  19313. entry key value)
  19314. (setq alist (sort (copy-sequence alist)
  19315. (lambda (a b) (< (length (car a)) (length (car b))))))
  19316. (while (setq entry (pop alist))
  19317. (setq template
  19318. (replace-regexp-in-string
  19319. (concat "%" (regexp-quote (car entry)))
  19320. (or (cdr entry) "") template t t)))
  19321. template))
  19322. (defun org-base-buffer (buffer)
  19323. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19324. (if (not buffer)
  19325. buffer
  19326. (or (buffer-base-buffer buffer)
  19327. buffer)))
  19328. (defun org-trim (s)
  19329. "Remove whitespace at beginning and end of string."
  19330. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19331. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19332. s)
  19333. (defun org-wrap (string &optional width lines)
  19334. "Wrap string to either a number of lines, or a width in characters.
  19335. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19336. that costs. If there is a word longer than WIDTH, the text is actually
  19337. wrapped to the length of that word.
  19338. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19339. many lines, whatever width that takes.
  19340. The return value is a list of lines, without newlines at the end."
  19341. (let* ((words (org-split-string string "[ \t\n]+"))
  19342. (maxword (apply 'max (mapcar 'org-string-width words)))
  19343. w ll)
  19344. (cond (width
  19345. (org-do-wrap words (max maxword width)))
  19346. (lines
  19347. (setq w maxword)
  19348. (setq ll (org-do-wrap words maxword))
  19349. (if (<= (length ll) lines)
  19350. ll
  19351. (setq ll words)
  19352. (while (> (length ll) lines)
  19353. (setq w (1+ w))
  19354. (setq ll (org-do-wrap words w)))
  19355. ll))
  19356. (t (error "Cannot wrap this")))))
  19357. (defun org-do-wrap (words width)
  19358. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19359. (let (lines line)
  19360. (while words
  19361. (setq line (pop words))
  19362. (while (and words (< (+ (length line) (length (car words))) width))
  19363. (setq line (concat line " " (pop words))))
  19364. (setq lines (push line lines)))
  19365. (nreverse lines)))
  19366. (defun org-split-string (string &optional separators)
  19367. "Splits STRING into substrings at SEPARATORS.
  19368. No empty strings are returned if there are matches at the beginning
  19369. and end of string."
  19370. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19371. (start 0)
  19372. notfirst
  19373. (list nil))
  19374. (while (and (string-match rexp string
  19375. (if (and notfirst
  19376. (= start (match-beginning 0))
  19377. (< start (length string)))
  19378. (1+ start) start))
  19379. (< (match-beginning 0) (length string)))
  19380. (setq notfirst t)
  19381. (or (eq (match-beginning 0) 0)
  19382. (and (eq (match-beginning 0) (match-end 0))
  19383. (eq (match-beginning 0) start))
  19384. (setq list
  19385. (cons (substring string start (match-beginning 0))
  19386. list)))
  19387. (setq start (match-end 0)))
  19388. (or (eq start (length string))
  19389. (setq list
  19390. (cons (substring string start)
  19391. list)))
  19392. (nreverse list)))
  19393. (defun org-quote-vert (s)
  19394. "Replace \"|\" with \"\\vert\"."
  19395. (while (string-match "|" s)
  19396. (setq s (replace-match "\\vert" t t s)))
  19397. s)
  19398. (defun org-uuidgen-p (s)
  19399. "Is S an ID created by UUIDGEN?"
  19400. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19401. (defun org-in-src-block-p (&optional inside)
  19402. "Whether point is in a code source block.
  19403. When INSIDE is non-nil, don't consider we are within a src block
  19404. when point is at #+BEGIN_SRC or #+END_SRC."
  19405. (let ((case-fold-search t) ov)
  19406. (or (and (setq ov (overlays-at (point)))
  19407. (memq 'org-block-background
  19408. (overlay-properties (car ov))))
  19409. (and (not inside)
  19410. (save-match-data
  19411. (save-excursion
  19412. (beginning-of-line)
  19413. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19414. (defun org-context ()
  19415. "Return a list of contexts of the current cursor position.
  19416. If several contexts apply, all are returned.
  19417. Each context entry is a list with a symbol naming the context, and
  19418. two positions indicating start and end of the context. Possible
  19419. contexts are:
  19420. :headline anywhere in a headline
  19421. :headline-stars on the leading stars in a headline
  19422. :todo-keyword on a TODO keyword (including DONE) in a headline
  19423. :tags on the TAGS in a headline
  19424. :priority on the priority cookie in a headline
  19425. :item on the first line of a plain list item
  19426. :item-bullet on the bullet/number of a plain list item
  19427. :checkbox on the checkbox in a plain list item
  19428. :table in an org-mode table
  19429. :table-special on a special filed in a table
  19430. :table-table in a table.el table
  19431. :clocktable in a clocktable
  19432. :src-block in a source block
  19433. :link on a hyperlink
  19434. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19435. :target on a <<target>>
  19436. :radio-target on a <<<radio-target>>>
  19437. :latex-fragment on a LaTeX fragment
  19438. :latex-preview on a LaTeX fragment with overlaid preview image
  19439. This function expects the position to be visible because it uses font-lock
  19440. faces as a help to recognize the following contexts: :table-special, :link,
  19441. and :keyword."
  19442. (let* ((f (get-text-property (point) 'face))
  19443. (faces (if (listp f) f (list f)))
  19444. (case-fold-search t)
  19445. (p (point)) clist o)
  19446. ;; First the large context
  19447. (cond
  19448. ((org-at-heading-p t)
  19449. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19450. (when (progn
  19451. (beginning-of-line 1)
  19452. (looking-at org-todo-line-tags-regexp))
  19453. (push (org-point-in-group p 1 :headline-stars) clist)
  19454. (push (org-point-in-group p 2 :todo-keyword) clist)
  19455. (push (org-point-in-group p 4 :tags) clist))
  19456. (goto-char p)
  19457. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19458. (if (looking-at "\\[#[A-Z0-9]\\]")
  19459. (push (org-point-in-group p 0 :priority) clist)))
  19460. ((org-at-item-p)
  19461. (push (org-point-in-group p 2 :item-bullet) clist)
  19462. (push (list :item (point-at-bol)
  19463. (save-excursion (org-end-of-item) (point)))
  19464. clist)
  19465. (and (org-at-item-checkbox-p)
  19466. (push (org-point-in-group p 0 :checkbox) clist)))
  19467. ((org-at-table-p)
  19468. (push (list :table (org-table-begin) (org-table-end)) clist)
  19469. (if (memq 'org-formula faces)
  19470. (push (list :table-special
  19471. (previous-single-property-change p 'face)
  19472. (next-single-property-change p 'face)) clist)))
  19473. ((org-at-table-p 'any)
  19474. (push (list :table-table) clist)))
  19475. (goto-char p)
  19476. (let ((case-fold-search t))
  19477. ;; New the "medium" contexts: clocktables, source blocks
  19478. (cond ((org-in-clocktable-p)
  19479. (push (list :clocktable
  19480. (and (or (looking-at "#\\+BEGIN: clocktable")
  19481. (search-backward "#+BEGIN: clocktable" nil t))
  19482. (match-beginning 0))
  19483. (and (re-search-forward "#\\+END:?" nil t)
  19484. (match-end 0))) clist))
  19485. ((org-in-src-block-p)
  19486. (push (list :src-block
  19487. (and (or (looking-at "#\\+BEGIN_SRC")
  19488. (search-backward "#+BEGIN_SRC" nil t))
  19489. (match-beginning 0))
  19490. (and (search-forward "#+END_SRC" nil t)
  19491. (match-beginning 0))) clist))))
  19492. (goto-char p)
  19493. ;; Now the small context
  19494. (cond
  19495. ((org-at-timestamp-p)
  19496. (push (org-point-in-group p 0 :timestamp) clist))
  19497. ((memq 'org-link faces)
  19498. (push (list :link
  19499. (previous-single-property-change p 'face)
  19500. (next-single-property-change p 'face)) clist))
  19501. ((memq 'org-special-keyword faces)
  19502. (push (list :keyword
  19503. (previous-single-property-change p 'face)
  19504. (next-single-property-change p 'face)) clist))
  19505. ((org-at-target-p)
  19506. (push (org-point-in-group p 0 :target) clist)
  19507. (goto-char (1- (match-beginning 0)))
  19508. (if (looking-at org-radio-target-regexp)
  19509. (push (org-point-in-group p 0 :radio-target) clist))
  19510. (goto-char p))
  19511. ((setq o (car (delq nil
  19512. (mapcar
  19513. (lambda (x)
  19514. (if (memq x org-latex-fragment-image-overlays) x))
  19515. (overlays-at (point))))))
  19516. (push (list :latex-fragment
  19517. (overlay-start o) (overlay-end o)) clist)
  19518. (push (list :latex-preview
  19519. (overlay-start o) (overlay-end o)) clist))
  19520. ((org-inside-LaTeX-fragment-p)
  19521. ;; FIXME: positions wrong.
  19522. (push (list :latex-fragment (point) (point)) clist)))
  19523. (setq clist (nreverse (delq nil clist)))
  19524. clist))
  19525. ;; FIXME: Compare with at-regexp-p Do we need both?
  19526. (defun org-in-regexp (re &optional nlines visually)
  19527. "Check if point is inside a match of regexp.
  19528. Normally only the current line is checked, but you can include NLINES extra
  19529. lines both before and after point into the search.
  19530. If VISUALLY is set, require that the cursor is not after the match but
  19531. really on, so that the block visually is on the match."
  19532. (catch 'exit
  19533. (let ((pos (point))
  19534. (eol (point-at-eol (+ 1 (or nlines 0))))
  19535. (inc (if visually 1 0)))
  19536. (save-excursion
  19537. (beginning-of-line (- 1 (or nlines 0)))
  19538. (while (re-search-forward re eol t)
  19539. (if (and (<= (match-beginning 0) pos)
  19540. (>= (+ inc (match-end 0)) pos))
  19541. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19542. (defun org-at-regexp-p (regexp)
  19543. "Is point inside a match of REGEXP in the current line?"
  19544. (catch 'exit
  19545. (save-excursion
  19546. (let ((pos (point)) (end (point-at-eol)))
  19547. (beginning-of-line 1)
  19548. (while (re-search-forward regexp end t)
  19549. (if (and (<= (match-beginning 0) pos)
  19550. (>= (match-end 0) pos))
  19551. (throw 'exit t)))
  19552. nil))))
  19553. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19554. "Non-nil when point is between matches of START-RE and END-RE.
  19555. Also return a non-nil value when point is on one of the matches.
  19556. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19557. buffer positions. Default values are the positions of headlines
  19558. surrounding the point.
  19559. The functions returns a cons cell whose car (resp. cdr) is the
  19560. position before START-RE (resp. after END-RE)."
  19561. (save-match-data
  19562. (let ((pos (point))
  19563. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19564. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19565. beg end)
  19566. (save-excursion
  19567. ;; Point is on a block when on START-RE or if START-RE can be
  19568. ;; found before it...
  19569. (and (or (org-at-regexp-p start-re)
  19570. (re-search-backward start-re limit-up t))
  19571. (setq beg (match-beginning 0))
  19572. ;; ... and END-RE after it...
  19573. (goto-char (match-end 0))
  19574. (re-search-forward end-re limit-down t)
  19575. (> (setq end (match-end 0)) pos)
  19576. ;; ... without another START-RE in-between.
  19577. (goto-char (match-beginning 0))
  19578. (not (re-search-backward start-re (1+ beg) t))
  19579. ;; Return value.
  19580. (cons beg end))))))
  19581. (defun org-in-block-p (names)
  19582. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19583. NAMES is a list of strings containing names of blocks.
  19584. Return first block name matched, or nil. Beware that in case of
  19585. nested blocks, the returned name may not belong to the closest
  19586. block from point."
  19587. (save-match-data
  19588. (catch 'exit
  19589. (let ((case-fold-search t)
  19590. (lim-up (save-excursion (outline-previous-heading)))
  19591. (lim-down (save-excursion (outline-next-heading))))
  19592. (mapc (lambda (name)
  19593. (let ((n (regexp-quote name)))
  19594. (when (org-between-regexps-p
  19595. (concat "^[ \t]*#\\+begin_" n)
  19596. (concat "^[ \t]*#\\+end_" n)
  19597. lim-up lim-down)
  19598. (throw 'exit n))))
  19599. names))
  19600. nil)))
  19601. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19602. "Call `multi-occur' with buffers for all agenda files."
  19603. (interactive "sOrg-files matching: \np")
  19604. (let* ((files (org-agenda-files))
  19605. (tnames (mapcar 'file-truename files))
  19606. (extra org-agenda-text-search-extra-files)
  19607. f)
  19608. (when (eq (car extra) 'agenda-archives)
  19609. (setq extra (cdr extra))
  19610. (setq files (org-add-archive-files files)))
  19611. (while (setq f (pop extra))
  19612. (unless (member (file-truename f) tnames)
  19613. (add-to-list 'files f 'append)
  19614. (add-to-list 'tnames (file-truename f) 'append)))
  19615. (multi-occur
  19616. (mapcar (lambda (x)
  19617. (with-current-buffer
  19618. (or (get-file-buffer x) (find-file-noselect x))
  19619. (widen)
  19620. (current-buffer)))
  19621. files)
  19622. regexp)))
  19623. (if (boundp 'occur-mode-find-occurrence-hook)
  19624. ;; Emacs 23
  19625. (add-hook 'occur-mode-find-occurrence-hook
  19626. (lambda ()
  19627. (when (derived-mode-p 'org-mode)
  19628. (org-reveal))))
  19629. ;; Emacs 22
  19630. (defadvice occur-mode-goto-occurrence
  19631. (after org-occur-reveal activate)
  19632. (and (derived-mode-p 'org-mode) (org-reveal)))
  19633. (defadvice occur-mode-goto-occurrence-other-window
  19634. (after org-occur-reveal activate)
  19635. (and (derived-mode-p 'org-mode) (org-reveal)))
  19636. (defadvice occur-mode-display-occurrence
  19637. (after org-occur-reveal activate)
  19638. (when (derived-mode-p 'org-mode)
  19639. (let ((pos (occur-mode-find-occurrence)))
  19640. (with-current-buffer (marker-buffer pos)
  19641. (save-excursion
  19642. (goto-char pos)
  19643. (org-reveal)))))))
  19644. (defun org-occur-link-in-agenda-files ()
  19645. "Create a link and search for it in the agendas.
  19646. The link is not stored in `org-stored-links', it is just created
  19647. for the search purpose."
  19648. (interactive)
  19649. (let ((link (condition-case nil
  19650. (org-store-link nil)
  19651. (error "Unable to create a link to here"))))
  19652. (org-occur-in-agenda-files (regexp-quote link))))
  19653. (defun org-reverse-string (string)
  19654. "Return the reverse of STRING."
  19655. (apply 'string (reverse (string-to-list string))))
  19656. (defun org-uniquify (list)
  19657. "Remove duplicate elements from LIST."
  19658. (let (res)
  19659. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19660. res))
  19661. (defun org-delete-all (elts list)
  19662. "Remove all elements in ELTS from LIST."
  19663. (while elts
  19664. (setq list (delete (pop elts) list)))
  19665. list)
  19666. (defun org-count (cl-item cl-seq)
  19667. "Count the number of occurrences of ITEM in SEQ.
  19668. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19669. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19670. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19671. (while (< cl-start cl-end)
  19672. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19673. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19674. (setq cl-start (1+ cl-start)))
  19675. cl-count))
  19676. (defun org-remove-if (predicate seq)
  19677. "Remove everything from SEQ that fulfills PREDICATE."
  19678. (let (res e)
  19679. (while seq
  19680. (setq e (pop seq))
  19681. (if (not (funcall predicate e)) (push e res)))
  19682. (nreverse res)))
  19683. (defun org-remove-if-not (predicate seq)
  19684. "Remove everything from SEQ that does not fulfill PREDICATE."
  19685. (let (res e)
  19686. (while seq
  19687. (setq e (pop seq))
  19688. (if (funcall predicate e) (push e res)))
  19689. (nreverse res)))
  19690. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19691. "Reduce two-argument FUNCTION across SEQ.
  19692. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19693. \":initial-value\" removed."
  19694. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19695. (cadr (memq :initial-value cl-keys)))
  19696. (cl-seq (pop cl-seq))
  19697. (t (funcall cl-func)))))
  19698. (while cl-seq
  19699. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19700. cl-accum))
  19701. (defun org-back-over-empty-lines ()
  19702. "Move backwards over whitespace, to the beginning of the first empty line.
  19703. Returns the number of empty lines passed."
  19704. (let ((pos (point)))
  19705. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19706. (skip-chars-backward " \t\n\r")
  19707. (unless (eobp)
  19708. (forward-line -1)))
  19709. (beginning-of-line 2)
  19710. (goto-char (min (point) pos))
  19711. (count-lines (point) pos)))
  19712. (defun org-skip-whitespace ()
  19713. (skip-chars-forward " \t\n\r"))
  19714. (defun org-point-in-group (point group &optional context)
  19715. "Check if POINT is in match-group GROUP.
  19716. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19717. match. If the match group does not exist or point is not inside it,
  19718. return nil."
  19719. (and (match-beginning group)
  19720. (>= point (match-beginning group))
  19721. (<= point (match-end group))
  19722. (if context
  19723. (list context (match-beginning group) (match-end group))
  19724. t)))
  19725. (defun org-switch-to-buffer-other-window (&rest args)
  19726. "Switch to buffer in a second window on the current frame.
  19727. In particular, do not allow pop-up frames.
  19728. Returns the newly created buffer."
  19729. (org-no-popups
  19730. (apply 'switch-to-buffer-other-window args)))
  19731. (defun org-combine-plists (&rest plists)
  19732. "Create a single property list from all plists in PLISTS.
  19733. The process starts by copying the first list, and then setting properties
  19734. from the other lists. Settings in the last list are the most significant
  19735. ones and overrule settings in the other lists."
  19736. (let ((rtn (copy-sequence (pop plists)))
  19737. p v ls)
  19738. (while plists
  19739. (setq ls (pop plists))
  19740. (while ls
  19741. (setq p (pop ls) v (pop ls))
  19742. (setq rtn (plist-put rtn p v))))
  19743. rtn))
  19744. (defun org-replace-escapes (string table)
  19745. "Replace %-escapes in STRING with values in TABLE.
  19746. TABLE is an association list with keys like \"%a\" and string values.
  19747. The sequences in STRING may contain normal field width and padding information,
  19748. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19749. so values can contain further %-escapes if they are define later in TABLE."
  19750. (let ((tbl (copy-alist table))
  19751. (case-fold-search nil)
  19752. (pchg 0)
  19753. e re rpl)
  19754. (while (setq e (pop tbl))
  19755. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19756. (when (and (cdr e) (string-match re (cdr e)))
  19757. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19758. (safe "SREF"))
  19759. (add-text-properties 0 3 (list 'sref sref) safe)
  19760. (setcdr e (replace-match safe t t (cdr e)))))
  19761. (while (string-match re string)
  19762. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19763. (cdr e)))
  19764. (setq string (replace-match rpl t t string))))
  19765. (while (setq pchg (next-property-change pchg string))
  19766. (let ((sref (get-text-property pchg 'sref string)))
  19767. (when (and sref (string-match "SREF" string pchg))
  19768. (setq string (replace-match sref t t string)))))
  19769. string))
  19770. (defun org-sublist (list start end)
  19771. "Return a section of LIST, from START to END.
  19772. Counting starts at 1."
  19773. (let (rtn (c start))
  19774. (setq list (nthcdr (1- start) list))
  19775. (while (and list (<= c end))
  19776. (push (pop list) rtn)
  19777. (setq c (1+ c)))
  19778. (nreverse rtn)))
  19779. (defun org-find-base-buffer-visiting (file)
  19780. "Like `find-buffer-visiting' but always return the base buffer and
  19781. not an indirect buffer."
  19782. (let ((buf (or (get-file-buffer file)
  19783. (find-buffer-visiting file))))
  19784. (if buf
  19785. (or (buffer-base-buffer buf) buf)
  19786. nil)))
  19787. (defun org-image-file-name-regexp (&optional extensions)
  19788. "Return regexp matching the file names of images.
  19789. If EXTENSIONS is given, only match these."
  19790. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19791. (image-file-name-regexp)
  19792. (let ((image-file-name-extensions
  19793. (or extensions
  19794. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19795. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19796. (concat "\\."
  19797. (regexp-opt (nconc (mapcar 'upcase
  19798. image-file-name-extensions)
  19799. image-file-name-extensions)
  19800. t)
  19801. "\\'"))))
  19802. (defun org-file-image-p (file &optional extensions)
  19803. "Return non-nil if FILE is an image."
  19804. (save-match-data
  19805. (string-match (org-image-file-name-regexp extensions) file)))
  19806. (defun org-get-cursor-date (&optional with-time)
  19807. "Return the date at cursor in as a time.
  19808. This works in the calendar and in the agenda, anywhere else it just
  19809. returns the current time.
  19810. If WITH-TIME is non-nil, returns the time of the event at point (in
  19811. the agenda) or the current time of the day."
  19812. (let (date day defd tp tm hod mod)
  19813. (when with-time
  19814. (setq tp (get-text-property (point) 'time))
  19815. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  19816. (setq hod (string-to-number (match-string 1 tp))
  19817. mod (string-to-number (match-string 2 tp))))
  19818. (or tp (setq hod (nth 2 (decode-time (current-time)))
  19819. mod (nth 1 (decode-time (current-time))))))
  19820. (cond
  19821. ((eq major-mode 'calendar-mode)
  19822. (setq date (calendar-cursor-to-date)
  19823. defd (encode-time 0 (or mod 0) (or hod 0)
  19824. (nth 1 date) (nth 0 date) (nth 2 date))))
  19825. ((eq major-mode 'org-agenda-mode)
  19826. (setq day (get-text-property (point) 'day))
  19827. (if day
  19828. (setq date (calendar-gregorian-from-absolute day)
  19829. defd (encode-time 0 (or mod 0) (or hod 0)
  19830. (nth 1 date) (nth 0 date) (nth 2 date))))))
  19831. (or defd (current-time))))
  19832. (defun org-mark-subtree (&optional up)
  19833. "Mark the current subtree.
  19834. This puts point at the start of the current subtree, and mark at
  19835. the end. If a numeric prefix UP is given, move up into the
  19836. hierarchy of headlines by UP levels before marking the subtree."
  19837. (interactive "P")
  19838. (org-with-limited-levels
  19839. (cond ((org-at-heading-p) (beginning-of-line))
  19840. ((org-before-first-heading-p) (error "Not in a subtree"))
  19841. (t (outline-previous-visible-heading 1))))
  19842. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19843. (if (org-called-interactively-p 'any)
  19844. (call-interactively 'org-mark-element)
  19845. (org-mark-element)))
  19846. ;;; Indentation
  19847. (defun org-indent-line ()
  19848. "Indent line depending on context."
  19849. (interactive)
  19850. (let* ((pos (point))
  19851. (itemp (org-at-item-p))
  19852. (case-fold-search t)
  19853. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19854. (inline-task-p (and (featurep 'org-inlinetask)
  19855. (org-inlinetask-in-task-p)))
  19856. (inline-re (and inline-task-p
  19857. (org-inlinetask-outline-regexp)))
  19858. column)
  19859. (if (and orgstruct-is-++ (eq pos (point)))
  19860. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19861. (indent-according-to-mode))
  19862. (beginning-of-line 1)
  19863. (cond
  19864. ;; Headings
  19865. ((looking-at org-outline-regexp) (setq column 0))
  19866. ;; Footnote definition
  19867. ((looking-at org-footnote-definition-re) (setq column 0))
  19868. ;; Literal examples
  19869. ((looking-at "[ \t]*:\\( \\|$\\)")
  19870. (setq column (org-get-indentation))) ; do nothing
  19871. ;; Lists
  19872. ((ignore-errors (goto-char (org-in-item-p)))
  19873. (setq column (if itemp
  19874. (org-get-indentation)
  19875. (org-list-item-body-column (point))))
  19876. (goto-char pos))
  19877. ;; Drawers
  19878. ((and (looking-at "[ \t]*:END:")
  19879. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19880. (save-excursion
  19881. (goto-char (1- (match-beginning 1)))
  19882. (setq column (current-column))))
  19883. ;; Special blocks
  19884. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19885. (save-excursion
  19886. (re-search-backward
  19887. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19888. (setq column (org-get-indentation (match-string 0))))
  19889. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19890. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19891. (save-excursion
  19892. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19893. (setq column
  19894. (cond ((equal (downcase (match-string 1)) "src")
  19895. ;; src blocks: let `org-edit-src-exit' handle them
  19896. (org-get-indentation))
  19897. ((equal (downcase (match-string 1)) "example")
  19898. (max (org-get-indentation)
  19899. (org-get-indentation (match-string 0))))
  19900. (t
  19901. (org-get-indentation (match-string 0))))))
  19902. ;; This line has nothing special, look at the previous relevant
  19903. ;; line to compute indentation
  19904. (t
  19905. (beginning-of-line 0)
  19906. (while (and (not (bobp))
  19907. (not (looking-at org-table-line-regexp))
  19908. (not (looking-at org-drawer-regexp))
  19909. ;; When point started in an inline task, do not move
  19910. ;; above task starting line.
  19911. (not (and inline-task-p (looking-at inline-re)))
  19912. ;; Skip drawers, blocks, empty lines, verbatim,
  19913. ;; comments, tables, footnotes definitions, lists,
  19914. ;; inline tasks.
  19915. (or (and (looking-at "[ \t]*:END:")
  19916. (re-search-backward org-drawer-regexp nil t))
  19917. (and (looking-at "[ \t]*#\\+end_")
  19918. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19919. (looking-at "[ \t]*[\n:#|]")
  19920. (looking-at org-footnote-definition-re)
  19921. (and (ignore-errors (goto-char (org-in-item-p)))
  19922. (goto-char
  19923. (org-list-get-top-point (org-list-struct))))
  19924. (and (not inline-task-p)
  19925. (featurep 'org-inlinetask)
  19926. (org-inlinetask-in-task-p)
  19927. (or (org-inlinetask-goto-beginning) t))))
  19928. (beginning-of-line 0))
  19929. (cond
  19930. ;; There was an heading above.
  19931. ((looking-at "\\*+[ \t]+")
  19932. (if (not org-adapt-indentation)
  19933. (setq column 0)
  19934. (goto-char (match-end 0))
  19935. (setq column (current-column))))
  19936. ;; A drawer had started and is unfinished
  19937. ((looking-at org-drawer-regexp)
  19938. (goto-char (1- (match-beginning 1)))
  19939. (setq column (current-column)))
  19940. ;; Else, nothing noticeable found: get indentation and go on.
  19941. (t (setq column (org-get-indentation))))))
  19942. ;; Now apply indentation and move cursor accordingly
  19943. (goto-char pos)
  19944. (if (<= (current-column) (current-indentation))
  19945. (org-indent-line-to column)
  19946. (save-excursion (org-indent-line-to column)))
  19947. ;; Special polishing for properties, see `org-property-format'
  19948. (setq column (current-column))
  19949. (beginning-of-line 1)
  19950. (if (looking-at
  19951. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19952. (replace-match (concat (match-string 1)
  19953. (format org-property-format
  19954. (match-string 2) (match-string 3)))
  19955. t t))
  19956. (org-move-to-column column))))
  19957. (defun org-indent-drawer ()
  19958. "Indent the drawer at point."
  19959. (interactive)
  19960. (let ((p (point))
  19961. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19962. (match-end 0)))
  19963. (folded
  19964. (save-excursion
  19965. (end-of-line)
  19966. (when (overlays-at (point))
  19967. (member 'invisible (overlay-properties
  19968. (car (overlays-at (point)))))))))
  19969. (when folded (org-cycle))
  19970. (indent-for-tab-command)
  19971. (while (and (move-beginning-of-line 2) (< (point) e))
  19972. (indent-for-tab-command))
  19973. (goto-char p)
  19974. (when folded (org-cycle)))
  19975. (message "Drawer at point indented"))
  19976. (defun org-indent-block ()
  19977. "Indent the block at point."
  19978. (interactive)
  19979. (let ((p (point))
  19980. (case-fold-search t)
  19981. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19982. (match-end 0)))
  19983. (folded
  19984. (save-excursion
  19985. (end-of-line)
  19986. (when (overlays-at (point))
  19987. (member 'invisible (overlay-properties
  19988. (car (overlays-at (point)))))))))
  19989. (when folded (org-cycle))
  19990. (indent-for-tab-command)
  19991. (while (and (move-beginning-of-line 2) (< (point) e))
  19992. (indent-for-tab-command))
  19993. (goto-char p)
  19994. (when folded (org-cycle)))
  19995. (message "Block at point indented"))
  19996. (defun org-indent-region (start end)
  19997. "Indent region."
  19998. (interactive "r")
  19999. (save-excursion
  20000. (let ((line-end (org-current-line end)))
  20001. (goto-char start)
  20002. (while (< (org-current-line) line-end)
  20003. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  20004. (t (call-interactively 'org-indent-line)))
  20005. (move-beginning-of-line 2)))))
  20006. ;;; Filling
  20007. ;; We use our own fill-paragraph and auto-fill functions.
  20008. ;; `org-fill-paragraph' relies on adaptive filling and context
  20009. ;; checking. Appropriate `fill-prefix' is computed with
  20010. ;; `org-adaptive-fill-function'.
  20011. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20012. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20013. ;; `org-adaptive-fill-function' value. Internally,
  20014. ;; `org-comment-line-break-function' breaks the line.
  20015. ;; `org-setup-filling' installs filling and auto-filling related
  20016. ;; variables during `org-mode' initialization.
  20017. (defun org-setup-filling ()
  20018. (interactive)
  20019. ;; Prevent auto-fill from inserting unwanted new items.
  20020. (when (boundp 'fill-nobreak-predicate)
  20021. (org-set-local
  20022. 'fill-nobreak-predicate
  20023. (org-uniquify
  20024. (append fill-nobreak-predicate
  20025. '(org-fill-paragraph-separate-nobreak-p
  20026. org-fill-line-break-nobreak-p
  20027. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20028. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20029. (org-set-local 'auto-fill-inhibit-regexp nil)
  20030. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20031. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20032. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20033. (defvar org-element-paragraph-separate) ; org-element.el
  20034. (defun org-fill-paragraph-separate-nobreak-p ()
  20035. "Non-nil when a line break at point would insert a new item."
  20036. (looking-at (substring org-element-paragraph-separate 1)))
  20037. (defun org-fill-line-break-nobreak-p ()
  20038. "Non-nil when a line break at point would create an Org line break."
  20039. (save-excursion
  20040. (skip-chars-backward "[ \t]")
  20041. (skip-chars-backward "\\\\")
  20042. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20043. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20044. "Non-nil when a line break at point would insert a new item."
  20045. (and (org-at-timestamp-p t)
  20046. (not (looking-at org-ts-regexp-both))))
  20047. (declare-function message-in-body-p "message" ())
  20048. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20049. (defun org-adaptive-fill-function ()
  20050. "Compute a fill prefix for the current line.
  20051. Return fill prefix, as a string, or nil if current line isn't
  20052. meant to be filled."
  20053. (let (prefix)
  20054. (catch 'exit
  20055. (when (derived-mode-p 'message-mode)
  20056. (save-excursion
  20057. (beginning-of-line)
  20058. (cond ((or (not (message-in-body-p))
  20059. (looking-at orgtbl-line-start-regexp))
  20060. (throw 'exit nil))
  20061. ((looking-at message-cite-prefix-regexp)
  20062. (throw 'exit (match-string-no-properties 0)))
  20063. ((looking-at org-outline-regexp)
  20064. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20065. (org-with-wide-buffer
  20066. (let* ((p (line-beginning-position))
  20067. (element (save-excursion (beginning-of-line)
  20068. (org-element-at-point)))
  20069. (type (org-element-type element))
  20070. (post-affiliated (org-element-property :post-affiliated element)))
  20071. (unless (and post-affiliated (< p post-affiliated))
  20072. (case type
  20073. (comment (looking-at "[ \t]*# ?") (match-string 0))
  20074. (footnote-definition "")
  20075. ((item plain-list)
  20076. (make-string (org-list-item-body-column
  20077. (or post-affiliated
  20078. (org-element-property :begin element)))
  20079. ? ))
  20080. (paragraph
  20081. ;; Fill prefix is usually the same as the current line,
  20082. ;; except if the paragraph is at the beginning of an item.
  20083. (let ((parent (org-element-property :parent element)))
  20084. (cond ((eq (org-element-type parent) 'item)
  20085. (make-string (org-list-item-body-column
  20086. (org-element-property :begin parent))
  20087. ? ))
  20088. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20089. (match-string 0))
  20090. (t ""))))
  20091. (comment-block
  20092. ;; Only fill contents if P is within block boundaries.
  20093. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20094. (forward-line)
  20095. (point)))
  20096. (cend (save-excursion
  20097. (goto-char (org-element-property :end element))
  20098. (skip-chars-backward " \r\t\n")
  20099. (line-beginning-position))))
  20100. (when (and (>= p cbeg) (< p cend))
  20101. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20102. (match-string 0)
  20103. "")))))))))))
  20104. (declare-function message-goto-body "message" ())
  20105. (defvar message-cite-prefix-regexp) ; From message.el
  20106. (defun org-fill-paragraph (&optional justify)
  20107. "Fill element at point, when applicable.
  20108. This function only applies to comment blocks, comments, example
  20109. blocks and paragraphs. Also, as a special case, re-align table
  20110. when point is at one.
  20111. If JUSTIFY is non-nil (interactively, with prefix argument),
  20112. justify as well. If `sentence-end-double-space' is non-nil, then
  20113. period followed by one space does not end a sentence, so don't
  20114. break a line there. The variable `fill-column' controls the
  20115. width for filling.
  20116. For convenience, when point is at a plain list, an item or
  20117. a footnote definition, try to fill the first paragraph within."
  20118. (interactive)
  20119. (if (and (derived-mode-p 'message-mode)
  20120. (or (not (message-in-body-p))
  20121. (save-excursion (move-beginning-of-line 1)
  20122. (looking-at message-cite-prefix-regexp))))
  20123. ;; First ensure filling is correct in message-mode.
  20124. (let ((fill-paragraph-function
  20125. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20126. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20127. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20128. (paragraph-separate
  20129. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20130. (fill-paragraph nil))
  20131. (with-syntax-table org-mode-transpose-word-syntax-table
  20132. ;; Move to end of line in order to get the first paragraph
  20133. ;; within a plain list or a footnote definition.
  20134. (let ((element (save-excursion (end-of-line) (org-element-at-point))))
  20135. ;; First check if point is in a blank line at the beginning of
  20136. ;; the buffer. In that case, ignore filling.
  20137. (case (org-element-type element)
  20138. ;; Use major mode filling function is src blocks.
  20139. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20140. ;; Align Org tables, leave table.el tables as-is.
  20141. (table-row (org-table-align) t)
  20142. (table
  20143. (when (eq (org-element-property :type element) 'org)
  20144. (org-table-align))
  20145. t)
  20146. (paragraph
  20147. ;; Paragraphs may contain `line-break' type objects.
  20148. (let ((beg (max (point-min)
  20149. (org-element-property :contents-begin element)))
  20150. (end (min (point-max)
  20151. (org-element-property :contents-end element))))
  20152. ;; Do nothing if point is at an affiliated keyword.
  20153. (if (< (line-end-position) beg) t
  20154. (when (derived-mode-p 'message-mode)
  20155. ;; In `message-mode', do not fill following citation
  20156. ;; in current paragraph nor text before message body.
  20157. (let ((body-start (save-excursion (message-goto-body))))
  20158. (when body-start (setq beg (max body-start beg))))
  20159. (when (save-excursion
  20160. (re-search-forward
  20161. (concat "^" message-cite-prefix-regexp) end t))
  20162. (setq end (match-beginning 0))))
  20163. ;; Fill paragraph, taking line breaks into account.
  20164. ;; For that, slice the paragraph using line breaks as
  20165. ;; separators, and fill the parts in reverse order to
  20166. ;; avoid messing with markers.
  20167. (save-excursion
  20168. (goto-char end)
  20169. (mapc
  20170. (lambda (pos)
  20171. (fill-region-as-paragraph pos (point) justify)
  20172. (goto-char pos))
  20173. ;; Find the list of ending positions for line breaks
  20174. ;; in the current paragraph. Add paragraph
  20175. ;; beginning to include first slice.
  20176. (nreverse
  20177. (cons beg
  20178. (org-element-map
  20179. (org-element--parse-objects
  20180. beg end nil (org-element-restriction 'paragraph))
  20181. 'line-break
  20182. (lambda (lb) (org-element-property :end lb)))))))
  20183. t)))
  20184. ;; Contents of `comment-block' type elements should be
  20185. ;; filled as plain text, but only if point is within block
  20186. ;; markers.
  20187. (comment-block
  20188. (let* ((case-fold-search t)
  20189. (beg (save-excursion
  20190. (goto-char (org-element-property :begin element))
  20191. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20192. (forward-line)
  20193. (point)))
  20194. (end (save-excursion
  20195. (goto-char (org-element-property :end element))
  20196. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20197. (line-beginning-position))))
  20198. (when (and (>= (point) beg) (< (point) end))
  20199. (fill-region-as-paragraph
  20200. (save-excursion
  20201. (end-of-line)
  20202. (re-search-backward "^[ \t]*$" beg 'move)
  20203. (line-beginning-position))
  20204. (save-excursion
  20205. (beginning-of-line)
  20206. (re-search-forward "^[ \t]*$" end 'move)
  20207. (line-beginning-position))
  20208. justify)))
  20209. t)
  20210. ;; Fill comments.
  20211. (comment (fill-comment-paragraph justify))
  20212. ;; Ignore every other element.
  20213. (otherwise t))))))
  20214. (defun org-auto-fill-function ()
  20215. "Auto-fill function."
  20216. ;; Check if auto-filling is meaningful.
  20217. (let ((fc (current-fill-column)))
  20218. (when (and fc (> (current-column) fc))
  20219. (let* ((fill-prefix (org-adaptive-fill-function))
  20220. ;; Enforce empty fill prefix, if required. Otherwise, it
  20221. ;; will be computed again.
  20222. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20223. (when fill-prefix (do-auto-fill))))))
  20224. (defun org-comment-line-break-function (&optional soft)
  20225. "Break line at point and indent, continuing comment if within one.
  20226. The inserted newline is marked hard if variable
  20227. `use-hard-newlines' is true, unless optional argument SOFT is
  20228. non-nil."
  20229. (if soft (insert-and-inherit ?\n) (newline 1))
  20230. (save-excursion (forward-char -1) (delete-horizontal-space))
  20231. (delete-horizontal-space)
  20232. (indent-to-left-margin)
  20233. (insert-before-markers-and-inherit fill-prefix))
  20234. ;;; Comments
  20235. ;; Org comments syntax is quite complex. It requires the entire line
  20236. ;; to be just a comment. Also, even with the right syntax at the
  20237. ;; beginning of line, some some elements (i.e. verse-block or
  20238. ;; example-block) don't accept comments. Usual Emacs comment commands
  20239. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20240. ;; Org still relies on `comment-dwim', but cannot trust
  20241. ;; `comment-only-p'. So, `comment-region-function' and
  20242. ;; `uncomment-region-function' both point
  20243. ;; to`org-comment-or-uncomment-region'. Eventually,
  20244. ;; `org-insert-comment' takes care of insertion of comments at the
  20245. ;; beginning of line.
  20246. ;; `org-setup-comments-handling' install comments related variables
  20247. ;; during `org-mode' initialization.
  20248. (defun org-setup-comments-handling ()
  20249. (interactive)
  20250. (org-set-local 'comment-use-syntax nil)
  20251. (org-set-local 'comment-start "# ")
  20252. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20253. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20254. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20255. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20256. (defun org-insert-comment ()
  20257. "Insert an empty comment above current line.
  20258. If the line is empty, insert comment at its beginning."
  20259. (beginning-of-line)
  20260. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20261. (org-indent-line)
  20262. (insert "# "))
  20263. (defvar comment-empty-lines) ; From newcomment.el.
  20264. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20265. "Comment or uncomment each non-blank line in the region.
  20266. Uncomment each non-blank line between BEG and END if it only
  20267. contains commented lines. Otherwise, comment them."
  20268. (save-restriction
  20269. ;; Restrict region
  20270. (narrow-to-region (save-excursion (goto-char beg)
  20271. (skip-chars-forward " \r\t\n" end)
  20272. (line-beginning-position))
  20273. (save-excursion (goto-char end)
  20274. (skip-chars-backward " \r\t\n" beg)
  20275. (line-end-position)))
  20276. (let ((uncommentp
  20277. ;; UNCOMMENTP is non-nil when every non blank line between
  20278. ;; BEG and END is a comment.
  20279. (save-excursion
  20280. (goto-char (point-min))
  20281. (while (and (not (eobp))
  20282. (let ((element (org-element-at-point)))
  20283. (and (eq (org-element-type element) 'comment)
  20284. (goto-char (min (point-max)
  20285. (org-element-property
  20286. :end element)))))))
  20287. (eobp))))
  20288. (if uncommentp
  20289. ;; Only blank lines and comments in region: uncomment it.
  20290. (save-excursion
  20291. (goto-char (point-min))
  20292. (while (not (eobp))
  20293. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20294. (replace-match "" nil nil nil 1))
  20295. (forward-line)))
  20296. ;; Comment each line in region.
  20297. (let ((min-indent (point-max)))
  20298. ;; First find the minimum indentation across all lines.
  20299. (save-excursion
  20300. (goto-char (point-min))
  20301. (while (and (not (eobp)) (not (zerop min-indent)))
  20302. (unless (looking-at "[ \t]*$")
  20303. (setq min-indent (min min-indent (current-indentation))))
  20304. (forward-line)))
  20305. ;; Then loop over all lines.
  20306. (save-excursion
  20307. (goto-char (point-min))
  20308. (while (not (eobp))
  20309. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20310. ;; Don't get fooled by invisible text (e.g. link path)
  20311. ;; when moving to column MIN-INDENT.
  20312. (let ((buffer-invisibility-spec nil))
  20313. (org-move-to-column min-indent t))
  20314. (insert comment-start))
  20315. (forward-line))))))))
  20316. ;;; Planning
  20317. ;; This section contains tools to operate on timestamp objects, as
  20318. ;; returned by, e.g. `org-element-context'.
  20319. (defun org-timestamp-has-time-p (timestamp)
  20320. "Non-nil when TIMESTAMP has a time specified."
  20321. (org-element-property :hour-start timestamp))
  20322. (defun org-timestamp-format (timestamp format &optional end utc)
  20323. "Format a TIMESTAMP element into a string.
  20324. FORMAT is a format specifier to be passed to
  20325. `format-time-string'.
  20326. When optional argument END is non-nil, use end of date-range or
  20327. time-range, if possible.
  20328. When optional argument UTC is non-nil, time will be expressed as
  20329. Universal Time."
  20330. (format-time-string
  20331. format
  20332. (apply 'encode-time
  20333. (cons 0
  20334. (mapcar
  20335. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20336. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20337. '(:minute-start :hour-start :day-start :month-start
  20338. :year-start)))))
  20339. utc))
  20340. (defun org-timestamp-split-range (timestamp &optional end)
  20341. "Extract a timestamp object from a date or time range.
  20342. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20343. the end of the range. Otherwise, extract its start.
  20344. Return a new timestamp object sharing the same parent as
  20345. TIMESTAMP."
  20346. (let ((type (org-element-property :type timestamp)))
  20347. (if (memq type '(active inactive diary)) timestamp
  20348. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20349. ;; Set new type.
  20350. (org-element-put-property
  20351. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20352. ;; Copy start properties over end properties if END is
  20353. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20354. (let ((p-alist '((:minute-start . :minute-end)
  20355. (:hour-start . :hour-end)
  20356. (:day-start . :day-end)
  20357. (:month-start . :month-end)
  20358. (:year-start . :year-end))))
  20359. (dolist (p-cell p-alist)
  20360. (org-element-put-property
  20361. split-ts
  20362. (funcall (if end 'car 'cdr) p-cell)
  20363. (org-element-property
  20364. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20365. ;; Eventually refresh `:raw-value'.
  20366. (org-element-put-property split-ts :raw-value nil)
  20367. (org-element-put-property
  20368. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20369. (defun org-timestamp-translate (timestamp &optional boundary)
  20370. "Apply `org-translate-time' on a TIMESTAMP object.
  20371. When optional argument BOUNDARY is non-nil, it is either the
  20372. symbol `start' or `end'. In this case, only translate the
  20373. starting or ending part of TIMESTAMP if it is a date or time
  20374. range. Otherwise, translate both parts."
  20375. (if (and (not boundary)
  20376. (memq (org-element-property :type timestamp)
  20377. '(active-range inactive-range)))
  20378. (concat
  20379. (org-translate-time
  20380. (org-element-property :raw-value
  20381. (org-timestamp-split-range timestamp)))
  20382. "--"
  20383. (org-translate-time
  20384. (org-element-property :raw-value
  20385. (org-timestamp-split-range timestamp t))))
  20386. (org-translate-time
  20387. (org-element-property
  20388. :raw-value
  20389. (if (not boundary) timestamp
  20390. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20391. ;;; Other stuff.
  20392. (defun org-toggle-fixed-width-section (arg)
  20393. "Toggle the fixed-width export.
  20394. If there is no active region, the QUOTE keyword at the current headline is
  20395. inserted or removed. When present, it causes the text between this headline
  20396. and the next to be exported as fixed-width text, and unmodified.
  20397. If there is an active region, this command adds or removes a colon as the
  20398. first character of this line. If the first character of a line is a colon,
  20399. this line is also exported in fixed-width font."
  20400. (interactive "P")
  20401. (let* ((cc 0)
  20402. (regionp (org-region-active-p))
  20403. (beg (if regionp (region-beginning) (point)))
  20404. (end (if regionp (region-end)))
  20405. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20406. (case-fold-search nil)
  20407. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20408. off)
  20409. (if regionp
  20410. (save-excursion
  20411. (goto-char beg)
  20412. (setq cc (current-column))
  20413. (beginning-of-line 1)
  20414. (setq off (looking-at re))
  20415. (while (> nlines 0)
  20416. (setq nlines (1- nlines))
  20417. (beginning-of-line 1)
  20418. (cond
  20419. (arg
  20420. (org-move-to-column cc t)
  20421. (insert ": \n")
  20422. (forward-line -1))
  20423. ((and off (looking-at re))
  20424. (replace-match "" t t nil 1))
  20425. ((not off) (org-move-to-column cc t) (insert ": ")))
  20426. (forward-line 1)))
  20427. (save-excursion
  20428. (org-back-to-heading)
  20429. (cond
  20430. ((looking-at (format org-heading-keyword-regexp-format
  20431. org-quote-string))
  20432. (goto-char (match-end 1))
  20433. (looking-at (concat " +" org-quote-string))
  20434. (replace-match "" t t)
  20435. (when (eolp) (insert " ")))
  20436. ((looking-at org-outline-regexp)
  20437. (goto-char (match-end 0))
  20438. (insert org-quote-string " ")))))))
  20439. (defun org-reftex-citation ()
  20440. "Use reftex-citation to insert a citation into the buffer.
  20441. This looks for a line like
  20442. #+BIBLIOGRAPHY: foo plain option:-d
  20443. and derives from it that foo.bib is the bibliography file relevant
  20444. for this document. It then installs the necessary environment for RefTeX
  20445. to work in this buffer and calls `reftex-citation' to insert a citation
  20446. into the buffer.
  20447. Export of such citations to both LaTeX and HTML is handled by the contributed
  20448. package org-exp-bibtex by Taru Karttunen."
  20449. (interactive)
  20450. (let ((reftex-docstruct-symbol 'rds)
  20451. (reftex-cite-format "\\cite{%l}")
  20452. rds bib)
  20453. (save-excursion
  20454. (save-restriction
  20455. (widen)
  20456. (let ((case-fold-search t)
  20457. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20458. (if (not (save-excursion
  20459. (or (re-search-forward re nil t)
  20460. (re-search-backward re nil t))))
  20461. (error "No bibliography defined in file")
  20462. (setq bib (concat (match-string 1) ".bib")
  20463. rds (list (list 'bib bib)))))))
  20464. (call-interactively 'reftex-citation)))
  20465. ;;;; Functions extending outline functionality
  20466. (defun org-beginning-of-line (&optional arg)
  20467. "Go to the beginning of the current line. If that is invisible, continue
  20468. to a visible line beginning. This makes the function of C-a more intuitive.
  20469. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20470. first attempt, and only move to after the tags when the cursor is already
  20471. beyond the end of the headline."
  20472. (interactive "P")
  20473. (let ((pos (point))
  20474. (special (if (consp org-special-ctrl-a/e)
  20475. (car org-special-ctrl-a/e)
  20476. org-special-ctrl-a/e))
  20477. refpos)
  20478. (if (org-bound-and-true-p visual-line-mode)
  20479. (beginning-of-visual-line 1)
  20480. (beginning-of-line 1))
  20481. (if (and arg (fboundp 'move-beginning-of-line))
  20482. (call-interactively 'move-beginning-of-line)
  20483. (if (bobp)
  20484. nil
  20485. (backward-char 1)
  20486. (if (org-truely-invisible-p)
  20487. (while (and (not (bobp)) (org-truely-invisible-p))
  20488. (backward-char 1)
  20489. (beginning-of-line 1))
  20490. (forward-char 1))))
  20491. (when special
  20492. (cond
  20493. ((and (looking-at org-complex-heading-regexp)
  20494. (= (char-after (match-end 1)) ?\ ))
  20495. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20496. (point-at-eol)))
  20497. (goto-char
  20498. (if (eq special t)
  20499. (cond ((> pos refpos) refpos)
  20500. ((= pos (point)) refpos)
  20501. (t (point)))
  20502. (cond ((> pos (point)) (point))
  20503. ((not (eq last-command this-command)) (point))
  20504. (t refpos)))))
  20505. ((org-at-item-p)
  20506. ;; Being at an item and not looking at an the item means point
  20507. ;; was previously moved to beginning of a visual line, which
  20508. ;; doesn't contain the item. Therefore, do nothing special,
  20509. ;; just stay here.
  20510. (when (looking-at org-list-full-item-re)
  20511. ;; Set special position at first white space character after
  20512. ;; bullet, and check-box, if any.
  20513. (let ((after-bullet
  20514. (let ((box (match-end 3)))
  20515. (if (not box) (match-end 1)
  20516. (let ((after (char-after box)))
  20517. (if (and after (= after ? )) (1+ box) box))))))
  20518. ;; Special case: Move point to special position when
  20519. ;; currently after it or at beginning of line.
  20520. (if (eq special t)
  20521. (when (or (> pos after-bullet) (= (point) pos))
  20522. (goto-char after-bullet))
  20523. ;; Reversed case: Move point to special position when
  20524. ;; point was already at beginning of line and command is
  20525. ;; repeated.
  20526. (when (and (= (point) pos) (eq last-command this-command))
  20527. (goto-char after-bullet))))))))
  20528. (org-no-warnings
  20529. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20530. (defun org-end-of-line (&optional arg)
  20531. "Go to the end of the line.
  20532. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20533. tags on the first attempt, and only move to after the tags when
  20534. the cursor is already beyond the end of the headline."
  20535. (interactive "P")
  20536. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20537. org-special-ctrl-a/e))
  20538. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20539. 'end-of-visual-line)
  20540. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20541. (t 'end-of-line))))
  20542. (if (or (not special) arg) (call-interactively move-fun)
  20543. (let* ((element (save-excursion (beginning-of-line)
  20544. (org-element-at-point)))
  20545. (type (org-element-type element)))
  20546. (cond
  20547. ((memq type '(headline inlinetask))
  20548. (let ((pos (point)))
  20549. (beginning-of-line 1)
  20550. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20551. (if (eq special t)
  20552. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20553. (goto-char (match-beginning 1))
  20554. (goto-char (match-end 0)))
  20555. (if (or (< pos (match-end 0))
  20556. (not (eq this-command last-command)))
  20557. (goto-char (match-end 0))
  20558. (goto-char (match-beginning 1))))
  20559. (call-interactively move-fun))))
  20560. ((org-element-property :hiddenp element)
  20561. ;; If element is hidden, `move-end-of-line' would put point
  20562. ;; after it. Use `end-of-line' to stay on current line.
  20563. (call-interactively 'end-of-line))
  20564. (t (call-interactively move-fun)))))
  20565. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20566. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20567. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20568. (defun org-backward-sentence (&optional arg)
  20569. "Go to beginning of sentence, or beginning of table field.
  20570. This will call `backward-sentence' or `org-table-beginning-of-field',
  20571. depending on context."
  20572. (interactive "P")
  20573. (cond
  20574. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20575. (t (call-interactively 'backward-sentence))))
  20576. (defun org-forward-sentence (&optional arg)
  20577. "Go to end of sentence, or end of table field.
  20578. This will call `forward-sentence' or `org-table-end-of-field',
  20579. depending on context."
  20580. (interactive "P")
  20581. (cond
  20582. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20583. (t (call-interactively 'forward-sentence))))
  20584. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20585. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20586. (defun org-kill-line (&optional arg)
  20587. "Kill line, to tags or end of line."
  20588. (interactive "P")
  20589. (cond
  20590. ((or (not org-special-ctrl-k)
  20591. (bolp)
  20592. (not (org-at-heading-p)))
  20593. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20594. org-ctrl-k-protect-subtree)
  20595. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20596. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20597. (error "C-k aborted - would kill hidden subtree")))
  20598. (call-interactively
  20599. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20600. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20601. (kill-region (point) (match-beginning 1))
  20602. (org-set-tags nil t))
  20603. (t (kill-region (point) (point-at-eol)))))
  20604. (define-key org-mode-map "\C-k" 'org-kill-line)
  20605. (defun org-yank (&optional arg)
  20606. "Yank. If the kill is a subtree, treat it specially.
  20607. This command will look at the current kill and check if is a single
  20608. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20609. cursor is at the beginning of a line or after the stars of a currently
  20610. empty headline, then the yank is handled specially. How exactly depends
  20611. on the value of the following variables, both set by default.
  20612. org-yank-folded-subtrees
  20613. When set, the subtree(s) will be folded after insertion, but only
  20614. if doing so would now swallow text after the yanked text.
  20615. org-yank-adjusted-subtrees
  20616. When set, the subtree will be promoted or demoted in order to
  20617. fit into the local outline tree structure, which means that the level
  20618. will be adjusted so that it becomes the smaller one of the two
  20619. *visible* surrounding headings.
  20620. Any prefix to this command will cause `yank' to be called directly with
  20621. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20622. will just
  20623. plainly yank the text as it is.
  20624. \[1] The test checks if the first non-white line is a heading
  20625. and if there are no other headings with fewer stars."
  20626. (interactive "P")
  20627. (org-yank-generic 'yank arg))
  20628. (defun org-yank-generic (command arg)
  20629. "Perform some yank-like command.
  20630. This function implements the behavior described in the `org-yank'
  20631. documentation. However, it has been generalized to work for any
  20632. interactive command with similar behavior."
  20633. ;; pretend to be command COMMAND
  20634. (setq this-command command)
  20635. (if arg
  20636. (call-interactively command)
  20637. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20638. (and (org-kill-is-subtree-p)
  20639. (or (bolp)
  20640. (and (looking-at "[ \t]*$")
  20641. (string-match
  20642. "\\`\\*+\\'"
  20643. (buffer-substring (point-at-bol) (point)))))))
  20644. swallowp)
  20645. (cond
  20646. ((and subtreep org-yank-folded-subtrees)
  20647. (let ((beg (point))
  20648. end)
  20649. (if (and subtreep org-yank-adjusted-subtrees)
  20650. (org-paste-subtree nil nil 'for-yank)
  20651. (call-interactively command))
  20652. (setq end (point))
  20653. (goto-char beg)
  20654. (when (and (bolp) subtreep
  20655. (not (setq swallowp
  20656. (org-yank-folding-would-swallow-text beg end))))
  20657. (org-with-limited-levels
  20658. (or (looking-at org-outline-regexp)
  20659. (re-search-forward org-outline-regexp-bol end t))
  20660. (while (and (< (point) end) (looking-at org-outline-regexp))
  20661. (hide-subtree)
  20662. (org-cycle-show-empty-lines 'folded)
  20663. (condition-case nil
  20664. (outline-forward-same-level 1)
  20665. (error (goto-char end))))))
  20666. (when swallowp
  20667. (message
  20668. "Inserted text not folded because that would swallow text"))
  20669. (goto-char end)
  20670. (skip-chars-forward " \t\n\r")
  20671. (beginning-of-line 1)
  20672. (push-mark beg 'nomsg)))
  20673. ((and subtreep org-yank-adjusted-subtrees)
  20674. (let ((beg (point-at-bol)))
  20675. (org-paste-subtree nil nil 'for-yank)
  20676. (push-mark beg 'nomsg)))
  20677. (t
  20678. (call-interactively command))))))
  20679. (defun org-yank-folding-would-swallow-text (beg end)
  20680. "Would hide-subtree at BEG swallow any text after END?"
  20681. (let (level)
  20682. (org-with-limited-levels
  20683. (save-excursion
  20684. (goto-char beg)
  20685. (when (or (looking-at org-outline-regexp)
  20686. (re-search-forward org-outline-regexp-bol end t))
  20687. (setq level (org-outline-level)))
  20688. (goto-char end)
  20689. (skip-chars-forward " \t\r\n\v\f")
  20690. (if (or (eobp)
  20691. (and (bolp) (looking-at org-outline-regexp)
  20692. (<= (org-outline-level) level)))
  20693. nil ; Nothing would be swallowed
  20694. t))))) ; something would swallow
  20695. (define-key org-mode-map "\C-y" 'org-yank)
  20696. (defun org-truely-invisible-p ()
  20697. "Check if point is at a character currently not visible.
  20698. This version does not only check the character property, but also
  20699. `visible-mode'."
  20700. ;; Early versions of noutline don't have `outline-invisible-p'.
  20701. (if (org-bound-and-true-p visible-mode)
  20702. nil
  20703. (outline-invisible-p)))
  20704. (defun org-invisible-p2 ()
  20705. "Check if point is at a character currently not visible."
  20706. (save-excursion
  20707. (if (and (eolp) (not (bobp))) (backward-char 1))
  20708. ;; Early versions of noutline don't have `outline-invisible-p'.
  20709. (outline-invisible-p)))
  20710. (defun org-back-to-heading (&optional invisible-ok)
  20711. "Call `outline-back-to-heading', but provide a better error message."
  20712. (condition-case nil
  20713. (outline-back-to-heading invisible-ok)
  20714. (error (error "Before first headline at position %d in buffer %s"
  20715. (point) (current-buffer)))))
  20716. (defun org-before-first-heading-p ()
  20717. "Before first heading?"
  20718. (save-excursion
  20719. (end-of-line)
  20720. (null (re-search-backward org-outline-regexp-bol nil t))))
  20721. (defun org-at-heading-p (&optional ignored)
  20722. (outline-on-heading-p t))
  20723. ;; Compatibility alias with Org versions < 7.8.03
  20724. (defalias 'org-on-heading-p 'org-at-heading-p)
  20725. (defun org-at-comment-p nil
  20726. "Is cursor in a line starting with a # character?"
  20727. (save-excursion
  20728. (beginning-of-line)
  20729. (looking-at "^#")))
  20730. (defun org-at-drawer-p nil
  20731. "Is cursor at a drawer keyword?"
  20732. (save-excursion
  20733. (move-beginning-of-line 1)
  20734. (looking-at org-drawer-regexp)))
  20735. (defun org-at-block-p nil
  20736. "Is cursor at a block keyword?"
  20737. (save-excursion
  20738. (move-beginning-of-line 1)
  20739. (looking-at org-block-regexp)))
  20740. (defun org-point-at-end-of-empty-headline ()
  20741. "If point is at the end of an empty headline, return t, else nil.
  20742. If the heading only contains a TODO keyword, it is still still considered
  20743. empty."
  20744. (and (looking-at "[ \t]*$")
  20745. (when org-todo-line-regexp
  20746. (save-excursion
  20747. (beginning-of-line 1)
  20748. (let ((case-fold-search nil))
  20749. (looking-at org-todo-line-regexp)
  20750. (string= (match-string 3) ""))))))
  20751. (defun org-at-heading-or-item-p ()
  20752. (or (org-at-heading-p) (org-at-item-p)))
  20753. (defun org-at-target-p ()
  20754. (or (org-in-regexp org-radio-target-regexp)
  20755. (org-in-regexp org-target-regexp)))
  20756. ;; Compatibility alias with Org versions < 7.8.03
  20757. (defalias 'org-on-target-p 'org-at-target-p)
  20758. (defun org-up-heading-all (arg)
  20759. "Move to the heading line of which the present line is a subheading.
  20760. This function considers both visible and invisible heading lines.
  20761. With argument, move up ARG levels."
  20762. (if (fboundp 'outline-up-heading-all)
  20763. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20764. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20765. (defun org-up-heading-safe ()
  20766. "Move to the heading line of which the present line is a subheading.
  20767. This version will not throw an error. It will return the level of the
  20768. headline found, or nil if no higher level is found.
  20769. Also, this function will be a lot faster than `outline-up-heading',
  20770. because it relies on stars being the outline starters. This can really
  20771. make a significant difference in outlines with very many siblings."
  20772. (let (start-level re)
  20773. (org-back-to-heading t)
  20774. (setq start-level (funcall outline-level))
  20775. (if (equal start-level 1)
  20776. nil
  20777. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20778. (if (re-search-backward re nil t)
  20779. (funcall outline-level)))))
  20780. (defun org-first-sibling-p ()
  20781. "Is this heading the first child of its parents?"
  20782. (interactive)
  20783. (let ((re org-outline-regexp-bol)
  20784. level l)
  20785. (unless (org-at-heading-p t)
  20786. (error "Not at a heading"))
  20787. (setq level (funcall outline-level))
  20788. (save-excursion
  20789. (if (not (re-search-backward re nil t))
  20790. t
  20791. (setq l (funcall outline-level))
  20792. (< l level)))))
  20793. (defun org-goto-sibling (&optional previous)
  20794. "Goto the next sibling, even if it is invisible.
  20795. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20796. when a sibling was found. When none is found, return nil and don't
  20797. move point."
  20798. (let ((fun (if previous 're-search-backward 're-search-forward))
  20799. (pos (point))
  20800. (re org-outline-regexp-bol)
  20801. level l)
  20802. (when (condition-case nil (org-back-to-heading t) (error nil))
  20803. (setq level (funcall outline-level))
  20804. (catch 'exit
  20805. (or previous (forward-char 1))
  20806. (while (funcall fun re nil t)
  20807. (setq l (funcall outline-level))
  20808. (when (< l level) (goto-char pos) (throw 'exit nil))
  20809. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20810. (goto-char pos)
  20811. nil))))
  20812. (defun org-show-siblings ()
  20813. "Show all siblings of the current headline."
  20814. (save-excursion
  20815. (while (org-goto-sibling) (org-flag-heading nil)))
  20816. (save-excursion
  20817. (while (org-goto-sibling 'previous)
  20818. (org-flag-heading nil))))
  20819. (defun org-goto-first-child ()
  20820. "Goto the first child, even if it is invisible.
  20821. Return t when a child was found. Otherwise don't move point and
  20822. return nil."
  20823. (let (level (pos (point)) (re org-outline-regexp-bol))
  20824. (when (condition-case nil (org-back-to-heading t) (error nil))
  20825. (setq level (outline-level))
  20826. (forward-char 1)
  20827. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20828. (progn (goto-char (match-beginning 0)) t)
  20829. (goto-char pos) nil))))
  20830. (defun org-show-hidden-entry ()
  20831. "Show an entry where even the heading is hidden."
  20832. (save-excursion
  20833. (org-show-entry)))
  20834. (defun org-flag-heading (flag &optional entry)
  20835. "Flag the current heading. FLAG non-nil means make invisible.
  20836. When ENTRY is non-nil, show the entire entry."
  20837. (save-excursion
  20838. (org-back-to-heading t)
  20839. ;; Check if we should show the entire entry
  20840. (if entry
  20841. (progn
  20842. (org-show-entry)
  20843. (save-excursion
  20844. (and (outline-next-heading)
  20845. (org-flag-heading nil))))
  20846. (outline-flag-region (max (point-min) (1- (point)))
  20847. (save-excursion (outline-end-of-heading) (point))
  20848. flag))))
  20849. (defun org-get-next-sibling ()
  20850. "Move to next heading of the same level, and return point.
  20851. If there is no such heading, return nil.
  20852. This is like outline-next-sibling, but invisible headings are ok."
  20853. (let ((level (funcall outline-level)))
  20854. (outline-next-heading)
  20855. (while (and (not (eobp)) (> (funcall outline-level) level))
  20856. (outline-next-heading))
  20857. (if (or (eobp) (< (funcall outline-level) level))
  20858. nil
  20859. (point))))
  20860. (defun org-get-last-sibling ()
  20861. "Move to previous heading of the same level, and return point.
  20862. If there is no such heading, return nil."
  20863. (let ((opoint (point))
  20864. (level (funcall outline-level)))
  20865. (outline-previous-heading)
  20866. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20867. (while (and (> (funcall outline-level) level)
  20868. (not (bobp)))
  20869. (outline-previous-heading))
  20870. (if (< (funcall outline-level) level)
  20871. nil
  20872. (point)))))
  20873. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20874. "Goto to the end of a subtree."
  20875. ;; This contains an exact copy of the original function, but it uses
  20876. ;; `org-back-to-heading', to make it work also in invisible
  20877. ;; trees. And is uses an invisible-ok argument.
  20878. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20879. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20880. ;; with many children and grandchildren etc, this can be much faster
  20881. ;; than the outline version.
  20882. (org-back-to-heading invisible-ok)
  20883. (let ((first t)
  20884. (level (funcall outline-level)))
  20885. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20886. ;; A true heading (not a plain list item), in Org-mode
  20887. ;; This means we can easily find the end by looking
  20888. ;; only for the right number of stars. Using a regexp to do
  20889. ;; this is so much faster than using a Lisp loop.
  20890. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20891. (forward-char 1)
  20892. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20893. ;; something else, do it the slow way
  20894. (while (and (not (eobp))
  20895. (or first (> (funcall outline-level) level)))
  20896. (setq first nil)
  20897. (outline-next-heading)))
  20898. (unless to-heading
  20899. (if (memq (preceding-char) '(?\n ?\^M))
  20900. (progn
  20901. ;; Go to end of line before heading
  20902. (forward-char -1)
  20903. (if (memq (preceding-char) '(?\n ?\^M))
  20904. ;; leave blank line before heading
  20905. (forward-char -1))))))
  20906. (point))
  20907. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20908. "Use Org version in org-mode, for dramatic speed-up."
  20909. (if (derived-mode-p 'org-mode)
  20910. (progn
  20911. (org-end-of-subtree nil t)
  20912. (unless (eobp) (backward-char 1)))
  20913. ad-do-it))
  20914. (defun org-end-of-meta-data-and-drawers ()
  20915. "Jump to the first text after meta data and drawers in the current entry.
  20916. This will move over empty lines, lines with planning time stamps,
  20917. clocking lines, and drawers."
  20918. (org-back-to-heading t)
  20919. (let ((end (save-excursion (outline-next-heading) (point)))
  20920. (re (concat "\\(" org-drawer-regexp "\\)"
  20921. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20922. (forward-line 1)
  20923. (while (re-search-forward re end t)
  20924. (if (not (match-end 1))
  20925. ;; empty or planning line
  20926. (forward-line 1)
  20927. ;; a drawer, find the end
  20928. (re-search-forward "^[ \t]*:END:" end 'move)
  20929. (forward-line 1)))
  20930. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20931. (point)))
  20932. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20933. "Move forward to the ARG'th subheading at same level as this one.
  20934. Stop at the first and last subheadings of a superior heading.
  20935. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20936. non-nil it will also look at invisible ones."
  20937. (interactive "p")
  20938. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  20939. (if (and arg (< arg 0))
  20940. (goto-char (point-min))
  20941. (outline-next-heading))
  20942. (org-at-heading-p)
  20943. (let ((level (- (match-end 0) (match-beginning 0) 1))
  20944. (f (if (and arg (< arg 0))
  20945. 're-search-backward
  20946. 're-search-forward))
  20947. (count (if arg (abs arg) 1))
  20948. (result (point)))
  20949. (forward-char (if (and arg (< arg 0)) -1 1))
  20950. (while (and (> count 0)
  20951. (funcall f org-outline-regexp-bol nil 'move))
  20952. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  20953. (cond ((< l level) (setq count 0))
  20954. ((and (= l level)
  20955. (or invisible-ok
  20956. (progn
  20957. (goto-char (line-beginning-position))
  20958. (not (outline-invisible-p)))))
  20959. (setq count (1- count))
  20960. (when (eq l level)
  20961. (setq result (point)))))))
  20962. (goto-char result))
  20963. (beginning-of-line 1)))
  20964. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20965. "Move backward to the ARG'th subheading at same level as this one.
  20966. Stop at the first and last subheadings of a superior heading."
  20967. (interactive "p")
  20968. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  20969. (defun org-next-block (arg &optional backward block-regexp)
  20970. "Jump to the next block.
  20971. With a prefix argument ARG, jump forward ARG many source blocks.
  20972. When BACKWARD is non-nil, jump to the previous block.
  20973. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  20974. (interactive "p")
  20975. (let ((re (or block-regexp org-block-regexp))
  20976. (re-search-fn (or (and backward 're-search-backward)
  20977. 're-search-forward)))
  20978. (if (looking-at re) (forward-char 1))
  20979. (condition-case nil
  20980. (funcall re-search-fn re nil nil arg)
  20981. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  20982. (goto-char (match-beginning 0)) (org-show-context)))
  20983. (defun org-previous-block (arg &optional block-regexp)
  20984. "Jump to the previous block.
  20985. With a prefix argument ARG, jump backward ARG many source blocks.
  20986. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  20987. (interactive "p")
  20988. (org-next-block arg t block-regexp))
  20989. (defun org-forward-element ()
  20990. "Move forward by one element.
  20991. Move to the next element at the same level, when possible."
  20992. (interactive)
  20993. (cond ((eobp) (user-error "Cannot move further down"))
  20994. ((org-with-limited-levels (org-at-heading-p))
  20995. (let ((origin (point)))
  20996. (goto-char (org-end-of-subtree nil t))
  20997. (unless (org-with-limited-levels (org-at-heading-p))
  20998. (goto-char origin)
  20999. (user-error "Cannot move further down"))))
  21000. (t
  21001. (let* ((elem (org-element-at-point))
  21002. (end (org-element-property :end elem))
  21003. (parent (org-element-property :parent elem)))
  21004. (if (and parent (= (org-element-property :contents-end parent) end))
  21005. (goto-char (org-element-property :end parent))
  21006. (goto-char end))))))
  21007. (defun org-backward-element ()
  21008. "Move backward by one element.
  21009. Move to the previous element at the same level, when possible."
  21010. (interactive)
  21011. (cond ((bobp) (user-error "Cannot move further up"))
  21012. ((org-with-limited-levels (org-at-heading-p))
  21013. ;; At a headline, move to the previous one, if any, or stay
  21014. ;; here.
  21015. (let ((origin (point)))
  21016. (org-with-limited-levels (org-backward-heading-same-level 1))
  21017. ;; When current headline has no sibling above, move to its
  21018. ;; parent.
  21019. (when (= (point) origin)
  21020. (or (org-with-limited-levels (org-up-heading-safe))
  21021. (progn (goto-char origin)
  21022. (user-error "Cannot move further up"))))))
  21023. (t
  21024. (let* ((trail (org-element-at-point 'keep-trail))
  21025. (elem (car trail))
  21026. (prev-elem (nth 1 trail))
  21027. (beg (org-element-property :begin elem)))
  21028. (cond
  21029. ;; Move to beginning of current element if point isn't
  21030. ;; there already.
  21031. ((/= (point) beg) (goto-char beg))
  21032. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21033. ((org-before-first-heading-p) (goto-char (point-min)))
  21034. (t (org-back-to-heading)))))))
  21035. (defun org-up-element ()
  21036. "Move to upper element."
  21037. (interactive)
  21038. (if (org-with-limited-levels (org-at-heading-p))
  21039. (unless (org-up-heading-safe) (error "No surrounding element"))
  21040. (let* ((elem (org-element-at-point))
  21041. (parent (org-element-property :parent elem)))
  21042. (if parent (goto-char (org-element-property :begin parent))
  21043. (if (org-with-limited-levels (org-before-first-heading-p))
  21044. (error "No surrounding element")
  21045. (org-with-limited-levels (org-back-to-heading)))))))
  21046. (defvar org-element-greater-elements)
  21047. (defun org-down-element ()
  21048. "Move to inner element."
  21049. (interactive)
  21050. (let ((element (org-element-at-point)))
  21051. (cond
  21052. ((memq (org-element-type element) '(plain-list table))
  21053. (goto-char (org-element-property :contents-begin element))
  21054. (forward-char))
  21055. ((memq (org-element-type element) org-element-greater-elements)
  21056. ;; If contents are hidden, first disclose them.
  21057. (when (org-element-property :hiddenp element) (org-cycle))
  21058. (goto-char (or (org-element-property :contents-begin element)
  21059. (error "No content for this element"))))
  21060. (t (error "No inner element")))))
  21061. (defun org-drag-element-backward ()
  21062. "Move backward element at point."
  21063. (interactive)
  21064. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21065. (let* ((trail (org-element-at-point 'keep-trail))
  21066. (elem (car trail))
  21067. (prev-elem (nth 1 trail)))
  21068. ;; Error out if no previous element or previous element is
  21069. ;; a parent of the current one.
  21070. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21071. (error "Cannot drag element backward")
  21072. (let ((pos (point)))
  21073. (org-element-swap-A-B prev-elem elem)
  21074. (goto-char (+ (org-element-property :begin prev-elem)
  21075. (- pos (org-element-property :begin elem)))))))))
  21076. (defun org-drag-element-forward ()
  21077. "Move forward element at point."
  21078. (interactive)
  21079. (let* ((pos (point))
  21080. (elem (org-element-at-point)))
  21081. (when (= (point-max) (org-element-property :end elem))
  21082. (error "Cannot drag element forward"))
  21083. (goto-char (org-element-property :end elem))
  21084. (let ((next-elem (org-element-at-point)))
  21085. (when (or (org-element-nested-p elem next-elem)
  21086. (and (eq (org-element-type next-elem) 'headline)
  21087. (not (eq (org-element-type elem) 'headline))))
  21088. (goto-char pos)
  21089. (error "Cannot drag element forward"))
  21090. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21091. ;; body's length (without final blanks) and by the length of
  21092. ;; blanks between ELEM and NEXT-ELEM.
  21093. (let ((size-next (- (save-excursion
  21094. (goto-char (org-element-property :end next-elem))
  21095. (skip-chars-backward " \r\t\n")
  21096. (forward-line)
  21097. ;; Small correction if buffer doesn't end
  21098. ;; with a newline character.
  21099. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21100. (org-element-property :begin next-elem)))
  21101. (size-blank (- (org-element-property :end elem)
  21102. (save-excursion
  21103. (goto-char (org-element-property :end elem))
  21104. (skip-chars-backward " \r\t\n")
  21105. (forward-line)
  21106. (point)))))
  21107. (org-element-swap-A-B elem next-elem)
  21108. (goto-char (+ pos size-next size-blank))))))
  21109. (defun org-drag-line-forward (arg)
  21110. "Drag the line at point ARG lines forward."
  21111. (interactive "p")
  21112. (dotimes (n (abs arg))
  21113. (let ((c (current-column)))
  21114. (if (< 0 arg)
  21115. (progn
  21116. (beginning-of-line 2)
  21117. (transpose-lines 1)
  21118. (beginning-of-line 0))
  21119. (transpose-lines 1)
  21120. (beginning-of-line -1))
  21121. (org-move-to-column c))))
  21122. (defun org-drag-line-backward (arg)
  21123. "Drag the line at point ARG lines backward."
  21124. (interactive "p")
  21125. (org-drag-line-forward (- arg)))
  21126. (defun org-mark-element ()
  21127. "Put point at beginning of this element, mark at end.
  21128. Interactively, if this command is repeated or (in Transient Mark
  21129. mode) if the mark is active, it marks the next element after the
  21130. ones already marked."
  21131. (interactive)
  21132. (let (deactivate-mark)
  21133. (if (and (org-called-interactively-p 'any)
  21134. (or (and (eq last-command this-command) (mark t))
  21135. (and transient-mark-mode mark-active)))
  21136. (set-mark
  21137. (save-excursion
  21138. (goto-char (mark))
  21139. (goto-char (org-element-property :end (org-element-at-point)))))
  21140. (let ((element (org-element-at-point)))
  21141. (end-of-line)
  21142. (push-mark (org-element-property :end element) t t)
  21143. (goto-char (org-element-property :begin element))))))
  21144. (defun org-narrow-to-element ()
  21145. "Narrow buffer to current element."
  21146. (interactive)
  21147. (let ((elem (org-element-at-point)))
  21148. (cond
  21149. ((eq (car elem) 'headline)
  21150. (narrow-to-region
  21151. (org-element-property :begin elem)
  21152. (org-element-property :end elem)))
  21153. ((memq (car elem) org-element-greater-elements)
  21154. (narrow-to-region
  21155. (org-element-property :contents-begin elem)
  21156. (org-element-property :contents-end elem)))
  21157. (t
  21158. (narrow-to-region
  21159. (org-element-property :begin elem)
  21160. (org-element-property :end elem))))))
  21161. (defun org-transpose-element ()
  21162. "Transpose current and previous elements, keeping blank lines between.
  21163. Point is moved after both elements."
  21164. (interactive)
  21165. (org-skip-whitespace)
  21166. (let ((end (org-element-property :end (org-element-at-point))))
  21167. (org-drag-element-backward)
  21168. (goto-char end)))
  21169. (defun org-unindent-buffer ()
  21170. "Un-indent the visible part of the buffer.
  21171. Relative indentation (between items, inside blocks, etc.) isn't
  21172. modified."
  21173. (interactive)
  21174. (unless (eq major-mode 'org-mode)
  21175. (error "Cannot un-indent a buffer not in Org mode"))
  21176. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21177. unindent-tree ; For byte-compiler.
  21178. (unindent-tree
  21179. (function
  21180. (lambda (contents)
  21181. (mapc
  21182. (lambda (element)
  21183. (if (memq (org-element-type element) '(headline section))
  21184. (funcall unindent-tree (org-element-contents element))
  21185. (save-excursion
  21186. (save-restriction
  21187. (narrow-to-region
  21188. (org-element-property :begin element)
  21189. (org-element-property :end element))
  21190. (org-do-remove-indentation)))))
  21191. (reverse contents))))))
  21192. (funcall unindent-tree (org-element-contents parse-tree))))
  21193. (defun org-show-subtree ()
  21194. "Show everything after this heading at deeper levels."
  21195. (interactive)
  21196. (outline-flag-region
  21197. (point)
  21198. (save-excursion
  21199. (org-end-of-subtree t t))
  21200. nil))
  21201. (defun org-show-entry ()
  21202. "Show the body directly following this heading.
  21203. Show the heading too, if it is currently invisible."
  21204. (interactive)
  21205. (save-excursion
  21206. (condition-case nil
  21207. (progn
  21208. (org-back-to-heading t)
  21209. (outline-flag-region
  21210. (max (point-min) (1- (point)))
  21211. (save-excursion
  21212. (if (re-search-forward
  21213. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21214. (match-beginning 1)
  21215. (point-max)))
  21216. nil)
  21217. (org-cycle-hide-drawers 'children))
  21218. (error nil))))
  21219. (defun org-make-options-regexp (kwds &optional extra)
  21220. "Make a regular expression for keyword lines."
  21221. (concat
  21222. "^#\\+\\("
  21223. (mapconcat 'regexp-quote kwds "\\|")
  21224. (if extra (concat "\\|" extra))
  21225. "\\):[ \t]*\\(.*\\)"))
  21226. ;; Make isearch reveal the necessary context
  21227. (defun org-isearch-end ()
  21228. "Reveal context after isearch exits."
  21229. (when isearch-success ; only if search was successful
  21230. (if (featurep 'xemacs)
  21231. ;; Under XEmacs, the hook is run in the correct place,
  21232. ;; we directly show the context.
  21233. (org-show-context 'isearch)
  21234. ;; In Emacs the hook runs *before* restoring the overlays.
  21235. ;; So we have to use a one-time post-command-hook to do this.
  21236. ;; (Emacs 22 has a special variable, see function `org-mode')
  21237. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21238. isearch-mode-end-hook-quit)
  21239. ;; Only when the isearch was not quitted.
  21240. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21241. 'append 'local)))
  21242. (org-fix-ellipsis-at-bol)))
  21243. (defun org-isearch-post-command ()
  21244. "Remove self from hook, and show context."
  21245. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21246. (org-show-context 'isearch))
  21247. ;;;; Integration with and fixes for other packages
  21248. ;;; Imenu support
  21249. (defvar org-imenu-markers nil
  21250. "All markers currently used by Imenu.")
  21251. (make-variable-buffer-local 'org-imenu-markers)
  21252. (defun org-imenu-new-marker (&optional pos)
  21253. "Return a new marker for use by Imenu, and remember the marker."
  21254. (let ((m (make-marker)))
  21255. (move-marker m (or pos (point)))
  21256. (push m org-imenu-markers)
  21257. m))
  21258. (defun org-imenu-get-tree ()
  21259. "Produce the index for Imenu."
  21260. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21261. (setq org-imenu-markers nil)
  21262. (let* ((n org-imenu-depth)
  21263. (re (concat "^" (org-get-limited-outline-regexp)))
  21264. (subs (make-vector (1+ n) nil))
  21265. (last-level 0)
  21266. m level head0 head)
  21267. (save-excursion
  21268. (save-restriction
  21269. (widen)
  21270. (goto-char (point-max))
  21271. (while (re-search-backward re nil t)
  21272. (setq level (org-reduced-level (funcall outline-level)))
  21273. (when (and (<= level n)
  21274. (looking-at org-complex-heading-regexp)
  21275. (setq head0 (org-match-string-no-properties 4)))
  21276. (setq head (org-link-display-format head0)
  21277. m (org-imenu-new-marker))
  21278. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21279. (if (>= level last-level)
  21280. (push (cons head m) (aref subs level))
  21281. (push (cons head (aref subs (1+ level))) (aref subs level))
  21282. (loop for i from (1+ level) to n do (aset subs i nil)))
  21283. (setq last-level level)))))
  21284. (aref subs 1)))
  21285. (eval-after-load "imenu"
  21286. '(progn
  21287. (add-hook 'imenu-after-jump-hook
  21288. (lambda ()
  21289. (if (derived-mode-p 'org-mode)
  21290. (org-show-context 'org-goto))))))
  21291. (defun org-link-display-format (link)
  21292. "Replace a link with its the description.
  21293. If there is no description, use the link target."
  21294. (save-match-data
  21295. (if (string-match org-bracket-link-analytic-regexp link)
  21296. (replace-match (if (match-end 5)
  21297. (match-string 5 link)
  21298. (concat (match-string 1 link)
  21299. (match-string 3 link)))
  21300. nil t link)
  21301. link)))
  21302. (defun org-toggle-link-display ()
  21303. "Toggle the literal or descriptive display of links."
  21304. (interactive)
  21305. (if org-descriptive-links
  21306. (progn (org-remove-from-invisibility-spec '(org-link))
  21307. (org-restart-font-lock)
  21308. (setq org-descriptive-links nil))
  21309. (progn (add-to-invisibility-spec '(org-link))
  21310. (org-restart-font-lock)
  21311. (setq org-descriptive-links t))))
  21312. ;; Speedbar support
  21313. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21314. "Overlay marking the agenda restriction line in speedbar.")
  21315. (overlay-put org-speedbar-restriction-lock-overlay
  21316. 'face 'org-agenda-restriction-lock)
  21317. (overlay-put org-speedbar-restriction-lock-overlay
  21318. 'help-echo "Agendas are currently limited to this item.")
  21319. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21320. (defun org-speedbar-set-agenda-restriction ()
  21321. "Restrict future agenda commands to the location at point in speedbar.
  21322. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21323. (interactive)
  21324. (require 'org-agenda)
  21325. (let (p m tp np dir txt)
  21326. (cond
  21327. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21328. 'org-imenu t))
  21329. (setq m (get-text-property p 'org-imenu-marker))
  21330. (with-current-buffer (marker-buffer m)
  21331. (goto-char m)
  21332. (org-agenda-set-restriction-lock 'subtree)))
  21333. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21334. 'speedbar-function 'speedbar-find-file))
  21335. (setq tp (previous-single-property-change
  21336. (1+ p) 'speedbar-function)
  21337. np (next-single-property-change
  21338. tp 'speedbar-function)
  21339. dir (speedbar-line-directory)
  21340. txt (buffer-substring-no-properties (or tp (point-min))
  21341. (or np (point-max))))
  21342. (with-current-buffer (find-file-noselect
  21343. (let ((default-directory dir))
  21344. (expand-file-name txt)))
  21345. (unless (derived-mode-p 'org-mode)
  21346. (error "Cannot restrict to non-Org-mode file"))
  21347. (org-agenda-set-restriction-lock 'file)))
  21348. (t (error "Don't know how to restrict Org-mode's agenda")))
  21349. (move-overlay org-speedbar-restriction-lock-overlay
  21350. (point-at-bol) (point-at-eol))
  21351. (setq current-prefix-arg nil)
  21352. (org-agenda-maybe-redo)))
  21353. (eval-after-load "speedbar"
  21354. '(progn
  21355. (speedbar-add-supported-extension ".org")
  21356. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21357. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21358. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21359. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21360. (add-hook 'speedbar-visiting-tag-hook
  21361. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21362. ;;; Fixes and Hacks for problems with other packages
  21363. ;; Make flyspell not check words in links, to not mess up our keymap
  21364. (defvar org-element-affiliated-keywords) ; From org-element.el
  21365. (defvar org-element-block-name-alist) ; From org-element.el
  21366. (defun org-mode-flyspell-verify ()
  21367. "Don't let flyspell put overlays at active buttons, or on
  21368. {todo,all-time,additional-option-like}-keywords."
  21369. (let ((pos (max (1- (point)) (point-min)))
  21370. (word (thing-at-point 'word)))
  21371. (and (not (get-text-property pos 'keymap))
  21372. (not (get-text-property pos 'org-no-flyspell))
  21373. (not (member word org-todo-keywords-1))
  21374. (not (member word org-all-time-keywords))
  21375. (not (member word org-options-keywords))
  21376. (not (member word (mapcar 'car org-startup-options)))
  21377. (not (member-ignore-case word org-element-affiliated-keywords))
  21378. (not (member-ignore-case word (org-get-export-keywords)))
  21379. (not (member-ignore-case
  21380. word (mapcar 'car org-element-block-name-alist)))
  21381. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21382. (defun org-remove-flyspell-overlays-in (beg end)
  21383. "Remove flyspell overlays in region."
  21384. (and (org-bound-and-true-p flyspell-mode)
  21385. (fboundp 'flyspell-delete-region-overlays)
  21386. (flyspell-delete-region-overlays beg end))
  21387. (add-text-properties beg end '(org-no-flyspell t)))
  21388. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21389. (eval-after-load "bookmark"
  21390. '(if (boundp 'bookmark-after-jump-hook)
  21391. ;; We can use the hook
  21392. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21393. ;; Hook not available, use advice
  21394. (defadvice bookmark-jump (after org-make-visible activate)
  21395. "Make the position visible."
  21396. (org-bookmark-jump-unhide))))
  21397. ;; Make sure saveplace shows the location if it was hidden
  21398. (eval-after-load "saveplace"
  21399. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21400. "Make the position visible."
  21401. (org-bookmark-jump-unhide)))
  21402. ;; Make sure ecb shows the location if it was hidden
  21403. (eval-after-load "ecb"
  21404. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21405. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21406. (if (derived-mode-p 'org-mode)
  21407. (org-show-context))))
  21408. (defun org-bookmark-jump-unhide ()
  21409. "Unhide the current position, to show the bookmark location."
  21410. (and (derived-mode-p 'org-mode)
  21411. (or (outline-invisible-p)
  21412. (save-excursion (goto-char (max (point-min) (1- (point))))
  21413. (outline-invisible-p)))
  21414. (org-show-context 'bookmark-jump)))
  21415. ;; Make session.el ignore our circular variable
  21416. (eval-after-load "session"
  21417. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21418. ;;;; Experimental code
  21419. (defun org-closed-in-range ()
  21420. "Sparse tree of items closed in a certain time range.
  21421. Still experimental, may disappear in the future."
  21422. (interactive)
  21423. ;; Get the time interval from the user.
  21424. (let* ((time1 (org-float-time
  21425. (org-read-date nil 'to-time nil "Starting date: ")))
  21426. (time2 (org-float-time
  21427. (org-read-date nil 'to-time nil "End date:")))
  21428. ;; callback function
  21429. (callback (lambda ()
  21430. (let ((time
  21431. (org-float-time
  21432. (apply 'encode-time
  21433. (org-parse-time-string
  21434. (match-string 1))))))
  21435. ;; check if time in interval
  21436. (and (>= time time1) (<= time time2))))))
  21437. ;; make tree, check each match with the callback
  21438. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  21439. ;;;; Finish up
  21440. (provide 'org)
  21441. (run-hooks 'org-load-hook)
  21442. ;;; org.el ends here